pci_insn.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * s390 specific pci instructions
  4. *
  5. * Copyright IBM Corp. 2013
  6. */
  7. #include <linux/export.h>
  8. #include <linux/errno.h>
  9. #include <linux/delay.h>
  10. #include <asm/facility.h>
  11. #include <asm/pci_insn.h>
  12. #include <asm/pci_debug.h>
  13. #include <asm/processor.h>
  14. #define ZPCI_INSN_BUSY_DELAY 1 /* 1 microsecond */
  15. static inline void zpci_err_insn(u8 cc, u8 status, u64 req, u64 offset)
  16. {
  17. struct {
  18. u64 req;
  19. u64 offset;
  20. u8 cc;
  21. u8 status;
  22. } __packed data = {req, offset, cc, status};
  23. zpci_err_hex(&data, sizeof(data));
  24. }
  25. /* Modify PCI Function Controls */
  26. static inline u8 __mpcifc(u64 req, struct zpci_fib *fib, u8 *status)
  27. {
  28. u8 cc;
  29. asm volatile (
  30. " .insn rxy,0xe300000000d0,%[req],%[fib]\n"
  31. " ipm %[cc]\n"
  32. " srl %[cc],28\n"
  33. : [cc] "=d" (cc), [req] "+d" (req), [fib] "+Q" (*fib)
  34. : : "cc");
  35. *status = req >> 24 & 0xff;
  36. return cc;
  37. }
  38. u8 zpci_mod_fc(u64 req, struct zpci_fib *fib, u8 *status)
  39. {
  40. u8 cc;
  41. do {
  42. cc = __mpcifc(req, fib, status);
  43. if (cc == 2)
  44. msleep(ZPCI_INSN_BUSY_DELAY);
  45. } while (cc == 2);
  46. if (cc)
  47. zpci_err_insn(cc, *status, req, 0);
  48. return cc;
  49. }
  50. /* Refresh PCI Translations */
  51. static inline u8 __rpcit(u64 fn, u64 addr, u64 range, u8 *status)
  52. {
  53. register u64 __addr asm("2") = addr;
  54. register u64 __range asm("3") = range;
  55. u8 cc;
  56. asm volatile (
  57. " .insn rre,0xb9d30000,%[fn],%[addr]\n"
  58. " ipm %[cc]\n"
  59. " srl %[cc],28\n"
  60. : [cc] "=d" (cc), [fn] "+d" (fn)
  61. : [addr] "d" (__addr), "d" (__range)
  62. : "cc");
  63. *status = fn >> 24 & 0xff;
  64. return cc;
  65. }
  66. int zpci_refresh_trans(u64 fn, u64 addr, u64 range)
  67. {
  68. u8 cc, status;
  69. do {
  70. cc = __rpcit(fn, addr, range, &status);
  71. if (cc == 2)
  72. udelay(ZPCI_INSN_BUSY_DELAY);
  73. } while (cc == 2);
  74. if (cc)
  75. zpci_err_insn(cc, status, addr, range);
  76. if (cc == 1 && (status == 4 || status == 16))
  77. return -ENOMEM;
  78. return (cc) ? -EIO : 0;
  79. }
  80. /* Set Interruption Controls */
  81. int zpci_set_irq_ctrl(u16 ctl, char *unused, u8 isc)
  82. {
  83. if (!test_facility(72))
  84. return -EIO;
  85. asm volatile (
  86. " .insn rsy,0xeb00000000d1,%[ctl],%[isc],%[u]\n"
  87. : : [ctl] "d" (ctl), [isc] "d" (isc << 27), [u] "Q" (*unused));
  88. return 0;
  89. }
  90. /* PCI Load */
  91. static inline int ____pcilg(u64 *data, u64 req, u64 offset, u8 *status)
  92. {
  93. register u64 __req asm("2") = req;
  94. register u64 __offset asm("3") = offset;
  95. int cc = -ENXIO;
  96. u64 __data;
  97. asm volatile (
  98. " .insn rre,0xb9d20000,%[data],%[req]\n"
  99. "0: ipm %[cc]\n"
  100. " srl %[cc],28\n"
  101. "1:\n"
  102. EX_TABLE(0b, 1b)
  103. : [cc] "+d" (cc), [data] "=d" (__data), [req] "+d" (__req)
  104. : "d" (__offset)
  105. : "cc");
  106. *status = __req >> 24 & 0xff;
  107. *data = __data;
  108. return cc;
  109. }
  110. static inline int __pcilg(u64 *data, u64 req, u64 offset, u8 *status)
  111. {
  112. u64 __data;
  113. int cc;
  114. cc = ____pcilg(&__data, req, offset, status);
  115. if (!cc)
  116. *data = __data;
  117. return cc;
  118. }
  119. int zpci_load(u64 *data, u64 req, u64 offset)
  120. {
  121. u8 status;
  122. int cc;
  123. do {
  124. cc = __pcilg(data, req, offset, &status);
  125. if (cc == 2)
  126. udelay(ZPCI_INSN_BUSY_DELAY);
  127. } while (cc == 2);
  128. if (cc)
  129. zpci_err_insn(cc, status, req, offset);
  130. return (cc > 0) ? -EIO : cc;
  131. }
  132. EXPORT_SYMBOL_GPL(zpci_load);
  133. /* PCI Store */
  134. static inline int __pcistg(u64 data, u64 req, u64 offset, u8 *status)
  135. {
  136. register u64 __req asm("2") = req;
  137. register u64 __offset asm("3") = offset;
  138. int cc = -ENXIO;
  139. asm volatile (
  140. " .insn rre,0xb9d00000,%[data],%[req]\n"
  141. "0: ipm %[cc]\n"
  142. " srl %[cc],28\n"
  143. "1:\n"
  144. EX_TABLE(0b, 1b)
  145. : [cc] "+d" (cc), [req] "+d" (__req)
  146. : "d" (__offset), [data] "d" (data)
  147. : "cc");
  148. *status = __req >> 24 & 0xff;
  149. return cc;
  150. }
  151. int zpci_store(u64 data, u64 req, u64 offset)
  152. {
  153. u8 status;
  154. int cc;
  155. do {
  156. cc = __pcistg(data, req, offset, &status);
  157. if (cc == 2)
  158. udelay(ZPCI_INSN_BUSY_DELAY);
  159. } while (cc == 2);
  160. if (cc)
  161. zpci_err_insn(cc, status, req, offset);
  162. return (cc > 0) ? -EIO : cc;
  163. }
  164. EXPORT_SYMBOL_GPL(zpci_store);
  165. /* PCI Store Block */
  166. static inline int __pcistb(const u64 *data, u64 req, u64 offset, u8 *status)
  167. {
  168. int cc = -ENXIO;
  169. asm volatile (
  170. " .insn rsy,0xeb00000000d0,%[req],%[offset],%[data]\n"
  171. "0: ipm %[cc]\n"
  172. " srl %[cc],28\n"
  173. "1:\n"
  174. EX_TABLE(0b, 1b)
  175. : [cc] "+d" (cc), [req] "+d" (req)
  176. : [offset] "d" (offset), [data] "Q" (*data)
  177. : "cc");
  178. *status = req >> 24 & 0xff;
  179. return cc;
  180. }
  181. int zpci_store_block(const u64 *data, u64 req, u64 offset)
  182. {
  183. u8 status;
  184. int cc;
  185. do {
  186. cc = __pcistb(data, req, offset, &status);
  187. if (cc == 2)
  188. udelay(ZPCI_INSN_BUSY_DELAY);
  189. } while (cc == 2);
  190. if (cc)
  191. zpci_err_insn(cc, status, req, offset);
  192. return (cc > 0) ? -EIO : cc;
  193. }
  194. EXPORT_SYMBOL_GPL(zpci_store_block);