io.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2005-2006 Fen Systems Ltd.
  5. * Copyright 2006-2013 Solarflare Communications Inc.
  6. */
  7. #ifndef EF4_IO_H
  8. #define EF4_IO_H
  9. #include <linux/io.h>
  10. #include <linux/spinlock.h>
  11. /**************************************************************************
  12. *
  13. * NIC register I/O
  14. *
  15. **************************************************************************
  16. *
  17. * Notes on locking strategy for the Falcon architecture:
  18. *
  19. * Many CSRs are very wide and cannot be read or written atomically.
  20. * Writes from the host are buffered by the Bus Interface Unit (BIU)
  21. * up to 128 bits. Whenever the host writes part of such a register,
  22. * the BIU collects the written value and does not write to the
  23. * underlying register until all 4 dwords have been written. A
  24. * similar buffering scheme applies to host access to the NIC's 64-bit
  25. * SRAM.
  26. *
  27. * Writes to different CSRs and 64-bit SRAM words must be serialised,
  28. * since interleaved access can result in lost writes. We use
  29. * ef4_nic::biu_lock for this.
  30. *
  31. * We also serialise reads from 128-bit CSRs and SRAM with the same
  32. * spinlock. This may not be necessary, but it doesn't really matter
  33. * as there are no such reads on the fast path.
  34. *
  35. * The DMA descriptor pointers (RX_DESC_UPD and TX_DESC_UPD) are
  36. * 128-bit but are special-cased in the BIU to avoid the need for
  37. * locking in the host:
  38. *
  39. * - They are write-only.
  40. * - The semantics of writing to these registers are such that
  41. * replacing the low 96 bits with zero does not affect functionality.
  42. * - If the host writes to the last dword address of such a register
  43. * (i.e. the high 32 bits) the underlying register will always be
  44. * written. If the collector and the current write together do not
  45. * provide values for all 128 bits of the register, the low 96 bits
  46. * will be written as zero.
  47. * - If the host writes to the address of any other part of such a
  48. * register while the collector already holds values for some other
  49. * register, the write is discarded and the collector maintains its
  50. * current state.
  51. *
  52. * The EF10 architecture exposes very few registers to the host and
  53. * most of them are only 32 bits wide. The only exceptions are the MC
  54. * doorbell register pair, which has its own latching, and
  55. * TX_DESC_UPD, which works in a similar way to the Falcon
  56. * architecture.
  57. */
  58. #if BITS_PER_LONG == 64
  59. #define EF4_USE_QWORD_IO 1
  60. #endif
  61. #ifdef EF4_USE_QWORD_IO
  62. static inline void _ef4_writeq(struct ef4_nic *efx, __le64 value,
  63. unsigned int reg)
  64. {
  65. __raw_writeq((__force u64)value, efx->membase + reg);
  66. }
  67. static inline __le64 _ef4_readq(struct ef4_nic *efx, unsigned int reg)
  68. {
  69. return (__force __le64)__raw_readq(efx->membase + reg);
  70. }
  71. #endif
  72. static inline void _ef4_writed(struct ef4_nic *efx, __le32 value,
  73. unsigned int reg)
  74. {
  75. __raw_writel((__force u32)value, efx->membase + reg);
  76. }
  77. static inline __le32 _ef4_readd(struct ef4_nic *efx, unsigned int reg)
  78. {
  79. return (__force __le32)__raw_readl(efx->membase + reg);
  80. }
  81. /* Write a normal 128-bit CSR, locking as appropriate. */
  82. static inline void ef4_writeo(struct ef4_nic *efx, const ef4_oword_t *value,
  83. unsigned int reg)
  84. {
  85. unsigned long flags __attribute__ ((unused));
  86. netif_vdbg(efx, hw, efx->net_dev,
  87. "writing register %x with " EF4_OWORD_FMT "\n", reg,
  88. EF4_OWORD_VAL(*value));
  89. spin_lock_irqsave(&efx->biu_lock, flags);
  90. #ifdef EF4_USE_QWORD_IO
  91. _ef4_writeq(efx, value->u64[0], reg + 0);
  92. _ef4_writeq(efx, value->u64[1], reg + 8);
  93. #else
  94. _ef4_writed(efx, value->u32[0], reg + 0);
  95. _ef4_writed(efx, value->u32[1], reg + 4);
  96. _ef4_writed(efx, value->u32[2], reg + 8);
  97. _ef4_writed(efx, value->u32[3], reg + 12);
  98. #endif
  99. spin_unlock_irqrestore(&efx->biu_lock, flags);
  100. }
  101. /* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
  102. static inline void ef4_sram_writeq(struct ef4_nic *efx, void __iomem *membase,
  103. const ef4_qword_t *value, unsigned int index)
  104. {
  105. unsigned int addr = index * sizeof(*value);
  106. unsigned long flags __attribute__ ((unused));
  107. netif_vdbg(efx, hw, efx->net_dev,
  108. "writing SRAM address %x with " EF4_QWORD_FMT "\n",
  109. addr, EF4_QWORD_VAL(*value));
  110. spin_lock_irqsave(&efx->biu_lock, flags);
  111. #ifdef EF4_USE_QWORD_IO
  112. __raw_writeq((__force u64)value->u64[0], membase + addr);
  113. #else
  114. __raw_writel((__force u32)value->u32[0], membase + addr);
  115. __raw_writel((__force u32)value->u32[1], membase + addr + 4);
  116. #endif
  117. spin_unlock_irqrestore(&efx->biu_lock, flags);
  118. }
  119. /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
  120. static inline void ef4_writed(struct ef4_nic *efx, const ef4_dword_t *value,
  121. unsigned int reg)
  122. {
  123. netif_vdbg(efx, hw, efx->net_dev,
  124. "writing register %x with "EF4_DWORD_FMT"\n",
  125. reg, EF4_DWORD_VAL(*value));
  126. /* No lock required */
  127. _ef4_writed(efx, value->u32[0], reg);
  128. }
  129. /* Read a 128-bit CSR, locking as appropriate. */
  130. static inline void ef4_reado(struct ef4_nic *efx, ef4_oword_t *value,
  131. unsigned int reg)
  132. {
  133. unsigned long flags __attribute__ ((unused));
  134. spin_lock_irqsave(&efx->biu_lock, flags);
  135. value->u32[0] = _ef4_readd(efx, reg + 0);
  136. value->u32[1] = _ef4_readd(efx, reg + 4);
  137. value->u32[2] = _ef4_readd(efx, reg + 8);
  138. value->u32[3] = _ef4_readd(efx, reg + 12);
  139. spin_unlock_irqrestore(&efx->biu_lock, flags);
  140. netif_vdbg(efx, hw, efx->net_dev,
  141. "read from register %x, got " EF4_OWORD_FMT "\n", reg,
  142. EF4_OWORD_VAL(*value));
  143. }
  144. /* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
  145. static inline void ef4_sram_readq(struct ef4_nic *efx, void __iomem *membase,
  146. ef4_qword_t *value, unsigned int index)
  147. {
  148. unsigned int addr = index * sizeof(*value);
  149. unsigned long flags __attribute__ ((unused));
  150. spin_lock_irqsave(&efx->biu_lock, flags);
  151. #ifdef EF4_USE_QWORD_IO
  152. value->u64[0] = (__force __le64)__raw_readq(membase + addr);
  153. #else
  154. value->u32[0] = (__force __le32)__raw_readl(membase + addr);
  155. value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
  156. #endif
  157. spin_unlock_irqrestore(&efx->biu_lock, flags);
  158. netif_vdbg(efx, hw, efx->net_dev,
  159. "read from SRAM address %x, got "EF4_QWORD_FMT"\n",
  160. addr, EF4_QWORD_VAL(*value));
  161. }
  162. /* Read a 32-bit CSR or SRAM */
  163. static inline void ef4_readd(struct ef4_nic *efx, ef4_dword_t *value,
  164. unsigned int reg)
  165. {
  166. value->u32[0] = _ef4_readd(efx, reg);
  167. netif_vdbg(efx, hw, efx->net_dev,
  168. "read from register %x, got "EF4_DWORD_FMT"\n",
  169. reg, EF4_DWORD_VAL(*value));
  170. }
  171. /* Write a 128-bit CSR forming part of a table */
  172. static inline void
  173. ef4_writeo_table(struct ef4_nic *efx, const ef4_oword_t *value,
  174. unsigned int reg, unsigned int index)
  175. {
  176. ef4_writeo(efx, value, reg + index * sizeof(ef4_oword_t));
  177. }
  178. /* Read a 128-bit CSR forming part of a table */
  179. static inline void ef4_reado_table(struct ef4_nic *efx, ef4_oword_t *value,
  180. unsigned int reg, unsigned int index)
  181. {
  182. ef4_reado(efx, value, reg + index * sizeof(ef4_oword_t));
  183. }
  184. /* Page size used as step between per-VI registers */
  185. #define EF4_VI_PAGE_SIZE 0x2000
  186. /* Calculate offset to page-mapped register */
  187. #define EF4_PAGED_REG(page, reg) \
  188. ((page) * EF4_VI_PAGE_SIZE + (reg))
  189. /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
  190. static inline void _ef4_writeo_page(struct ef4_nic *efx, ef4_oword_t *value,
  191. unsigned int reg, unsigned int page)
  192. {
  193. reg = EF4_PAGED_REG(page, reg);
  194. netif_vdbg(efx, hw, efx->net_dev,
  195. "writing register %x with " EF4_OWORD_FMT "\n", reg,
  196. EF4_OWORD_VAL(*value));
  197. #ifdef EF4_USE_QWORD_IO
  198. _ef4_writeq(efx, value->u64[0], reg + 0);
  199. _ef4_writeq(efx, value->u64[1], reg + 8);
  200. #else
  201. _ef4_writed(efx, value->u32[0], reg + 0);
  202. _ef4_writed(efx, value->u32[1], reg + 4);
  203. _ef4_writed(efx, value->u32[2], reg + 8);
  204. _ef4_writed(efx, value->u32[3], reg + 12);
  205. #endif
  206. }
  207. #define ef4_writeo_page(efx, value, reg, page) \
  208. _ef4_writeo_page(efx, value, \
  209. reg + \
  210. BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
  211. page)
  212. /* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the
  213. * high bits of RX_DESC_UPD or TX_DESC_UPD)
  214. */
  215. static inline void
  216. _ef4_writed_page(struct ef4_nic *efx, const ef4_dword_t *value,
  217. unsigned int reg, unsigned int page)
  218. {
  219. ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
  220. }
  221. #define ef4_writed_page(efx, value, reg, page) \
  222. _ef4_writed_page(efx, value, \
  223. reg + \
  224. BUILD_BUG_ON_ZERO((reg) != 0x400 && \
  225. (reg) != 0x420 && \
  226. (reg) != 0x830 && \
  227. (reg) != 0x83c && \
  228. (reg) != 0xa18 && \
  229. (reg) != 0xa1c), \
  230. page)
  231. /* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
  232. * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
  233. * collector register.
  234. */
  235. static inline void _ef4_writed_page_locked(struct ef4_nic *efx,
  236. const ef4_dword_t *value,
  237. unsigned int reg,
  238. unsigned int page)
  239. {
  240. unsigned long flags __attribute__ ((unused));
  241. if (page == 0) {
  242. spin_lock_irqsave(&efx->biu_lock, flags);
  243. ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
  244. spin_unlock_irqrestore(&efx->biu_lock, flags);
  245. } else {
  246. ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
  247. }
  248. }
  249. #define ef4_writed_page_locked(efx, value, reg, page) \
  250. _ef4_writed_page_locked(efx, value, \
  251. reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
  252. page)
  253. #endif /* EF4_IO_H */