io.h 9.6 KB

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