pci_io.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_S390_PCI_IO_H
  3. #define _ASM_S390_PCI_IO_H
  4. #ifdef CONFIG_PCI
  5. #include <linux/kernel.h>
  6. #include <linux/slab.h>
  7. #include <asm/pci_insn.h>
  8. /* I/O Map */
  9. #define ZPCI_IOMAP_SHIFT 48
  10. #define ZPCI_IOMAP_ADDR_BASE 0x8000000000000000UL
  11. #define ZPCI_IOMAP_ADDR_OFF_MASK ((1UL << ZPCI_IOMAP_SHIFT) - 1)
  12. #define ZPCI_IOMAP_MAX_ENTRIES \
  13. ((ULONG_MAX - ZPCI_IOMAP_ADDR_BASE + 1) / (1UL << ZPCI_IOMAP_SHIFT))
  14. #define ZPCI_IOMAP_ADDR_IDX_MASK \
  15. (~ZPCI_IOMAP_ADDR_OFF_MASK - ZPCI_IOMAP_ADDR_BASE)
  16. struct zpci_iomap_entry {
  17. u32 fh;
  18. u8 bar;
  19. u16 count;
  20. };
  21. extern struct zpci_iomap_entry *zpci_iomap_start;
  22. #define ZPCI_ADDR(idx) (ZPCI_IOMAP_ADDR_BASE | ((u64) idx << ZPCI_IOMAP_SHIFT))
  23. #define ZPCI_IDX(addr) \
  24. (((__force u64) addr & ZPCI_IOMAP_ADDR_IDX_MASK) >> ZPCI_IOMAP_SHIFT)
  25. #define ZPCI_OFFSET(addr) \
  26. ((__force u64) addr & ZPCI_IOMAP_ADDR_OFF_MASK)
  27. #define ZPCI_CREATE_REQ(handle, space, len) \
  28. ((u64) handle << 32 | space << 16 | len)
  29. #define zpci_read(LENGTH, RETTYPE) \
  30. static inline RETTYPE zpci_read_##RETTYPE(const volatile void __iomem *addr) \
  31. { \
  32. struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
  33. u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
  34. u64 data; \
  35. int rc; \
  36. \
  37. rc = zpci_load(&data, req, ZPCI_OFFSET(addr)); \
  38. if (rc) \
  39. data = -1ULL; \
  40. return (RETTYPE) data; \
  41. }
  42. #define zpci_write(LENGTH, VALTYPE) \
  43. static inline void zpci_write_##VALTYPE(VALTYPE val, \
  44. const volatile void __iomem *addr) \
  45. { \
  46. struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(addr)]; \
  47. u64 req = ZPCI_CREATE_REQ(entry->fh, entry->bar, LENGTH); \
  48. u64 data = (VALTYPE) val; \
  49. \
  50. zpci_store(data, req, ZPCI_OFFSET(addr)); \
  51. }
  52. zpci_read(8, u64)
  53. zpci_read(4, u32)
  54. zpci_read(2, u16)
  55. zpci_read(1, u8)
  56. zpci_write(8, u64)
  57. zpci_write(4, u32)
  58. zpci_write(2, u16)
  59. zpci_write(1, u8)
  60. static inline int zpci_write_single(u64 req, const u64 *data, u64 offset, u8 len)
  61. {
  62. u64 val;
  63. switch (len) {
  64. case 1:
  65. val = (u64) *((u8 *) data);
  66. break;
  67. case 2:
  68. val = (u64) *((u16 *) data);
  69. break;
  70. case 4:
  71. val = (u64) *((u32 *) data);
  72. break;
  73. case 8:
  74. val = (u64) *((u64 *) data);
  75. break;
  76. default:
  77. val = 0; /* let FW report error */
  78. break;
  79. }
  80. return zpci_store(val, req, offset);
  81. }
  82. static inline int zpci_read_single(u64 req, u64 *dst, u64 offset, u8 len)
  83. {
  84. u64 data;
  85. int cc;
  86. cc = zpci_load(&data, req, offset);
  87. if (cc)
  88. goto out;
  89. switch (len) {
  90. case 1:
  91. *((u8 *) dst) = (u8) data;
  92. break;
  93. case 2:
  94. *((u16 *) dst) = (u16) data;
  95. break;
  96. case 4:
  97. *((u32 *) dst) = (u32) data;
  98. break;
  99. case 8:
  100. *((u64 *) dst) = (u64) data;
  101. break;
  102. }
  103. out:
  104. return cc;
  105. }
  106. static inline int zpci_write_block(u64 req, const u64 *data, u64 offset)
  107. {
  108. return zpci_store_block(data, req, offset);
  109. }
  110. static inline u8 zpci_get_max_write_size(u64 src, u64 dst, int len, int max)
  111. {
  112. int count = len > max ? max : len, size = 1;
  113. while (!(src & 0x1) && !(dst & 0x1) && ((size << 1) <= count)) {
  114. dst = dst >> 1;
  115. src = src >> 1;
  116. size = size << 1;
  117. }
  118. return size;
  119. }
  120. static inline int zpci_memcpy_fromio(void *dst,
  121. const volatile void __iomem *src,
  122. unsigned long n)
  123. {
  124. struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(src)];
  125. u64 req, offset = ZPCI_OFFSET(src);
  126. int size, rc = 0;
  127. while (n > 0) {
  128. size = zpci_get_max_write_size((u64 __force) src,
  129. (u64) dst, n, 8);
  130. req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
  131. rc = zpci_read_single(req, dst, offset, size);
  132. if (rc)
  133. break;
  134. offset += size;
  135. dst += size;
  136. n -= size;
  137. }
  138. return rc;
  139. }
  140. static inline int zpci_memcpy_toio(volatile void __iomem *dst,
  141. const void *src, unsigned long n)
  142. {
  143. struct zpci_iomap_entry *entry = &zpci_iomap_start[ZPCI_IDX(dst)];
  144. u64 req, offset = ZPCI_OFFSET(dst);
  145. int size, rc = 0;
  146. if (!src)
  147. return -EINVAL;
  148. while (n > 0) {
  149. size = zpci_get_max_write_size((u64 __force) dst,
  150. (u64) src, n, 128);
  151. req = ZPCI_CREATE_REQ(entry->fh, entry->bar, size);
  152. if (size > 8) /* main path */
  153. rc = zpci_write_block(req, src, offset);
  154. else
  155. rc = zpci_write_single(req, src, offset, size);
  156. if (rc)
  157. break;
  158. offset += size;
  159. src += size;
  160. n -= size;
  161. }
  162. return rc;
  163. }
  164. static inline int zpci_memset_io(volatile void __iomem *dst,
  165. unsigned char val, size_t count)
  166. {
  167. u8 *src = kmalloc(count, GFP_KERNEL);
  168. int rc;
  169. if (src == NULL)
  170. return -ENOMEM;
  171. memset(src, val, count);
  172. rc = zpci_memcpy_toio(dst, src, count);
  173. kfree(src);
  174. return rc;
  175. }
  176. #endif /* CONFIG_PCI */
  177. #endif /* _ASM_S390_PCI_IO_H */