io.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 EFX_IO_H
  8. #define EFX_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. * efx_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 EFX_USE_QWORD_IO 1
  60. #endif
  61. /* Hardware issue requires that only 64-bit naturally aligned writes
  62. * are seen by hardware. Its not strictly necessary to restrict to
  63. * x86_64 arch, but done for safety since unusual write combining behaviour
  64. * can break PIO.
  65. */
  66. #ifdef CONFIG_X86_64
  67. /* PIO is a win only if write-combining is possible */
  68. #ifdef ARCH_HAS_IOREMAP_WC
  69. #define EFX_USE_PIO 1
  70. #endif
  71. #endif
  72. #ifdef EFX_USE_QWORD_IO
  73. static inline void _efx_writeq(struct efx_nic *efx, __le64 value,
  74. unsigned int reg)
  75. {
  76. __raw_writeq((__force u64)value, efx->membase + reg);
  77. }
  78. static inline __le64 _efx_readq(struct efx_nic *efx, unsigned int reg)
  79. {
  80. return (__force __le64)__raw_readq(efx->membase + reg);
  81. }
  82. #endif
  83. static inline void _efx_writed(struct efx_nic *efx, __le32 value,
  84. unsigned int reg)
  85. {
  86. __raw_writel((__force u32)value, efx->membase + reg);
  87. }
  88. static inline __le32 _efx_readd(struct efx_nic *efx, unsigned int reg)
  89. {
  90. return (__force __le32)__raw_readl(efx->membase + reg);
  91. }
  92. /* Write a normal 128-bit CSR, locking as appropriate. */
  93. static inline void efx_writeo(struct efx_nic *efx, const efx_oword_t *value,
  94. unsigned int reg)
  95. {
  96. unsigned long flags __attribute__ ((unused));
  97. netif_vdbg(efx, hw, efx->net_dev,
  98. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  99. EFX_OWORD_VAL(*value));
  100. spin_lock_irqsave(&efx->biu_lock, flags);
  101. #ifdef EFX_USE_QWORD_IO
  102. _efx_writeq(efx, value->u64[0], reg + 0);
  103. _efx_writeq(efx, value->u64[1], reg + 8);
  104. #else
  105. _efx_writed(efx, value->u32[0], reg + 0);
  106. _efx_writed(efx, value->u32[1], reg + 4);
  107. _efx_writed(efx, value->u32[2], reg + 8);
  108. _efx_writed(efx, value->u32[3], reg + 12);
  109. #endif
  110. spin_unlock_irqrestore(&efx->biu_lock, flags);
  111. }
  112. /* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
  113. static inline void efx_sram_writeq(struct efx_nic *efx, void __iomem *membase,
  114. const efx_qword_t *value, unsigned int index)
  115. {
  116. unsigned int addr = index * sizeof(*value);
  117. unsigned long flags __attribute__ ((unused));
  118. netif_vdbg(efx, hw, efx->net_dev,
  119. "writing SRAM address %x with " EFX_QWORD_FMT "\n",
  120. addr, EFX_QWORD_VAL(*value));
  121. spin_lock_irqsave(&efx->biu_lock, flags);
  122. #ifdef EFX_USE_QWORD_IO
  123. __raw_writeq((__force u64)value->u64[0], membase + addr);
  124. #else
  125. __raw_writel((__force u32)value->u32[0], membase + addr);
  126. __raw_writel((__force u32)value->u32[1], membase + addr + 4);
  127. #endif
  128. spin_unlock_irqrestore(&efx->biu_lock, flags);
  129. }
  130. /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
  131. static inline void efx_writed(struct efx_nic *efx, const efx_dword_t *value,
  132. unsigned int reg)
  133. {
  134. netif_vdbg(efx, hw, efx->net_dev,
  135. "writing register %x with "EFX_DWORD_FMT"\n",
  136. reg, EFX_DWORD_VAL(*value));
  137. /* No lock required */
  138. _efx_writed(efx, value->u32[0], reg);
  139. }
  140. /* Read a 128-bit CSR, locking as appropriate. */
  141. static inline void efx_reado(struct efx_nic *efx, efx_oword_t *value,
  142. unsigned int reg)
  143. {
  144. unsigned long flags __attribute__ ((unused));
  145. spin_lock_irqsave(&efx->biu_lock, flags);
  146. value->u32[0] = _efx_readd(efx, reg + 0);
  147. value->u32[1] = _efx_readd(efx, reg + 4);
  148. value->u32[2] = _efx_readd(efx, reg + 8);
  149. value->u32[3] = _efx_readd(efx, reg + 12);
  150. spin_unlock_irqrestore(&efx->biu_lock, flags);
  151. netif_vdbg(efx, hw, efx->net_dev,
  152. "read from register %x, got " EFX_OWORD_FMT "\n", reg,
  153. EFX_OWORD_VAL(*value));
  154. }
  155. /* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
  156. static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
  157. efx_qword_t *value, unsigned int index)
  158. {
  159. unsigned int addr = index * sizeof(*value);
  160. unsigned long flags __attribute__ ((unused));
  161. spin_lock_irqsave(&efx->biu_lock, flags);
  162. #ifdef EFX_USE_QWORD_IO
  163. value->u64[0] = (__force __le64)__raw_readq(membase + addr);
  164. #else
  165. value->u32[0] = (__force __le32)__raw_readl(membase + addr);
  166. value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
  167. #endif
  168. spin_unlock_irqrestore(&efx->biu_lock, flags);
  169. netif_vdbg(efx, hw, efx->net_dev,
  170. "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
  171. addr, EFX_QWORD_VAL(*value));
  172. }
  173. /* Read a 32-bit CSR or SRAM */
  174. static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
  175. unsigned int reg)
  176. {
  177. value->u32[0] = _efx_readd(efx, reg);
  178. netif_vdbg(efx, hw, efx->net_dev,
  179. "read from register %x, got "EFX_DWORD_FMT"\n",
  180. reg, EFX_DWORD_VAL(*value));
  181. }
  182. /* Write a 128-bit CSR forming part of a table */
  183. static inline void
  184. efx_writeo_table(struct efx_nic *efx, const efx_oword_t *value,
  185. unsigned int reg, unsigned int index)
  186. {
  187. efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
  188. }
  189. /* Read a 128-bit CSR forming part of a table */
  190. static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
  191. unsigned int reg, unsigned int index)
  192. {
  193. efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
  194. }
  195. /* default VI stride (step between per-VI registers) is 8K */
  196. #define EFX_DEFAULT_VI_STRIDE 0x2000
  197. /* Calculate offset to page-mapped register */
  198. static inline unsigned int efx_paged_reg(struct efx_nic *efx, unsigned int page,
  199. unsigned int reg)
  200. {
  201. return page * efx->vi_stride + reg;
  202. }
  203. /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
  204. static inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
  205. unsigned int reg, unsigned int page)
  206. {
  207. reg = efx_paged_reg(efx, page, reg);
  208. netif_vdbg(efx, hw, efx->net_dev,
  209. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  210. EFX_OWORD_VAL(*value));
  211. #ifdef EFX_USE_QWORD_IO
  212. _efx_writeq(efx, value->u64[0], reg + 0);
  213. _efx_writeq(efx, value->u64[1], reg + 8);
  214. #else
  215. _efx_writed(efx, value->u32[0], reg + 0);
  216. _efx_writed(efx, value->u32[1], reg + 4);
  217. _efx_writed(efx, value->u32[2], reg + 8);
  218. _efx_writed(efx, value->u32[3], reg + 12);
  219. #endif
  220. }
  221. #define efx_writeo_page(efx, value, reg, page) \
  222. _efx_writeo_page(efx, value, \
  223. reg + \
  224. BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
  225. page)
  226. /* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the
  227. * high bits of RX_DESC_UPD or TX_DESC_UPD)
  228. */
  229. static inline void
  230. _efx_writed_page(struct efx_nic *efx, const efx_dword_t *value,
  231. unsigned int reg, unsigned int page)
  232. {
  233. efx_writed(efx, value, efx_paged_reg(efx, page, reg));
  234. }
  235. #define efx_writed_page(efx, value, reg, page) \
  236. _efx_writed_page(efx, value, \
  237. reg + \
  238. BUILD_BUG_ON_ZERO((reg) != 0x400 && \
  239. (reg) != 0x420 && \
  240. (reg) != 0x830 && \
  241. (reg) != 0x83c && \
  242. (reg) != 0xa18 && \
  243. (reg) != 0xa1c), \
  244. page)
  245. /* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
  246. * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
  247. * collector register.
  248. */
  249. static inline void _efx_writed_page_locked(struct efx_nic *efx,
  250. const efx_dword_t *value,
  251. unsigned int reg,
  252. unsigned int page)
  253. {
  254. unsigned long flags __attribute__ ((unused));
  255. if (page == 0) {
  256. spin_lock_irqsave(&efx->biu_lock, flags);
  257. efx_writed(efx, value, efx_paged_reg(efx, page, reg));
  258. spin_unlock_irqrestore(&efx->biu_lock, flags);
  259. } else {
  260. efx_writed(efx, value, efx_paged_reg(efx, page, reg));
  261. }
  262. }
  263. #define efx_writed_page_locked(efx, value, reg, page) \
  264. _efx_writed_page_locked(efx, value, \
  265. reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
  266. page)
  267. #endif /* EFX_IO_H */