io.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 EF4_IO_H
  11. #define EF4_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. * ef4_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 EF4_USE_QWORD_IO 1
  63. #endif
  64. #ifdef EF4_USE_QWORD_IO
  65. static inline void _ef4_writeq(struct ef4_nic *efx, __le64 value,
  66. unsigned int reg)
  67. {
  68. __raw_writeq((__force u64)value, efx->membase + reg);
  69. }
  70. static inline __le64 _ef4_readq(struct ef4_nic *efx, unsigned int reg)
  71. {
  72. return (__force __le64)__raw_readq(efx->membase + reg);
  73. }
  74. #endif
  75. static inline void _ef4_writed(struct ef4_nic *efx, __le32 value,
  76. unsigned int reg)
  77. {
  78. __raw_writel((__force u32)value, efx->membase + reg);
  79. }
  80. static inline __le32 _ef4_readd(struct ef4_nic *efx, unsigned int reg)
  81. {
  82. return (__force __le32)__raw_readl(efx->membase + reg);
  83. }
  84. /* Write a normal 128-bit CSR, locking as appropriate. */
  85. static inline void ef4_writeo(struct ef4_nic *efx, const ef4_oword_t *value,
  86. unsigned int reg)
  87. {
  88. unsigned long flags __attribute__ ((unused));
  89. netif_vdbg(efx, hw, efx->net_dev,
  90. "writing register %x with " EF4_OWORD_FMT "\n", reg,
  91. EF4_OWORD_VAL(*value));
  92. spin_lock_irqsave(&efx->biu_lock, flags);
  93. #ifdef EF4_USE_QWORD_IO
  94. _ef4_writeq(efx, value->u64[0], reg + 0);
  95. _ef4_writeq(efx, value->u64[1], reg + 8);
  96. #else
  97. _ef4_writed(efx, value->u32[0], reg + 0);
  98. _ef4_writed(efx, value->u32[1], reg + 4);
  99. _ef4_writed(efx, value->u32[2], reg + 8);
  100. _ef4_writed(efx, value->u32[3], reg + 12);
  101. #endif
  102. mmiowb();
  103. spin_unlock_irqrestore(&efx->biu_lock, flags);
  104. }
  105. /* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
  106. static inline void ef4_sram_writeq(struct ef4_nic *efx, void __iomem *membase,
  107. const ef4_qword_t *value, unsigned int index)
  108. {
  109. unsigned int addr = index * sizeof(*value);
  110. unsigned long flags __attribute__ ((unused));
  111. netif_vdbg(efx, hw, efx->net_dev,
  112. "writing SRAM address %x with " EF4_QWORD_FMT "\n",
  113. addr, EF4_QWORD_VAL(*value));
  114. spin_lock_irqsave(&efx->biu_lock, flags);
  115. #ifdef EF4_USE_QWORD_IO
  116. __raw_writeq((__force u64)value->u64[0], membase + addr);
  117. #else
  118. __raw_writel((__force u32)value->u32[0], membase + addr);
  119. __raw_writel((__force u32)value->u32[1], membase + addr + 4);
  120. #endif
  121. mmiowb();
  122. spin_unlock_irqrestore(&efx->biu_lock, flags);
  123. }
  124. /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
  125. static inline void ef4_writed(struct ef4_nic *efx, const ef4_dword_t *value,
  126. unsigned int reg)
  127. {
  128. netif_vdbg(efx, hw, efx->net_dev,
  129. "writing register %x with "EF4_DWORD_FMT"\n",
  130. reg, EF4_DWORD_VAL(*value));
  131. /* No lock required */
  132. _ef4_writed(efx, value->u32[0], reg);
  133. }
  134. /* Read a 128-bit CSR, locking as appropriate. */
  135. static inline void ef4_reado(struct ef4_nic *efx, ef4_oword_t *value,
  136. unsigned int reg)
  137. {
  138. unsigned long flags __attribute__ ((unused));
  139. spin_lock_irqsave(&efx->biu_lock, flags);
  140. value->u32[0] = _ef4_readd(efx, reg + 0);
  141. value->u32[1] = _ef4_readd(efx, reg + 4);
  142. value->u32[2] = _ef4_readd(efx, reg + 8);
  143. value->u32[3] = _ef4_readd(efx, reg + 12);
  144. spin_unlock_irqrestore(&efx->biu_lock, flags);
  145. netif_vdbg(efx, hw, efx->net_dev,
  146. "read from register %x, got " EF4_OWORD_FMT "\n", reg,
  147. EF4_OWORD_VAL(*value));
  148. }
  149. /* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
  150. static inline void ef4_sram_readq(struct ef4_nic *efx, void __iomem *membase,
  151. ef4_qword_t *value, unsigned int index)
  152. {
  153. unsigned int addr = index * sizeof(*value);
  154. unsigned long flags __attribute__ ((unused));
  155. spin_lock_irqsave(&efx->biu_lock, flags);
  156. #ifdef EF4_USE_QWORD_IO
  157. value->u64[0] = (__force __le64)__raw_readq(membase + addr);
  158. #else
  159. value->u32[0] = (__force __le32)__raw_readl(membase + addr);
  160. value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
  161. #endif
  162. spin_unlock_irqrestore(&efx->biu_lock, flags);
  163. netif_vdbg(efx, hw, efx->net_dev,
  164. "read from SRAM address %x, got "EF4_QWORD_FMT"\n",
  165. addr, EF4_QWORD_VAL(*value));
  166. }
  167. /* Read a 32-bit CSR or SRAM */
  168. static inline void ef4_readd(struct ef4_nic *efx, ef4_dword_t *value,
  169. unsigned int reg)
  170. {
  171. value->u32[0] = _ef4_readd(efx, reg);
  172. netif_vdbg(efx, hw, efx->net_dev,
  173. "read from register %x, got "EF4_DWORD_FMT"\n",
  174. reg, EF4_DWORD_VAL(*value));
  175. }
  176. /* Write a 128-bit CSR forming part of a table */
  177. static inline void
  178. ef4_writeo_table(struct ef4_nic *efx, const ef4_oword_t *value,
  179. unsigned int reg, unsigned int index)
  180. {
  181. ef4_writeo(efx, value, reg + index * sizeof(ef4_oword_t));
  182. }
  183. /* Read a 128-bit CSR forming part of a table */
  184. static inline void ef4_reado_table(struct ef4_nic *efx, ef4_oword_t *value,
  185. unsigned int reg, unsigned int index)
  186. {
  187. ef4_reado(efx, value, reg + index * sizeof(ef4_oword_t));
  188. }
  189. /* Page size used as step between per-VI registers */
  190. #define EF4_VI_PAGE_SIZE 0x2000
  191. /* Calculate offset to page-mapped register */
  192. #define EF4_PAGED_REG(page, reg) \
  193. ((page) * EF4_VI_PAGE_SIZE + (reg))
  194. /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
  195. static inline void _ef4_writeo_page(struct ef4_nic *efx, ef4_oword_t *value,
  196. unsigned int reg, unsigned int page)
  197. {
  198. reg = EF4_PAGED_REG(page, reg);
  199. netif_vdbg(efx, hw, efx->net_dev,
  200. "writing register %x with " EF4_OWORD_FMT "\n", reg,
  201. EF4_OWORD_VAL(*value));
  202. #ifdef EF4_USE_QWORD_IO
  203. _ef4_writeq(efx, value->u64[0], reg + 0);
  204. _ef4_writeq(efx, value->u64[1], reg + 8);
  205. #else
  206. _ef4_writed(efx, value->u32[0], reg + 0);
  207. _ef4_writed(efx, value->u32[1], reg + 4);
  208. _ef4_writed(efx, value->u32[2], reg + 8);
  209. _ef4_writed(efx, value->u32[3], reg + 12);
  210. #endif
  211. }
  212. #define ef4_writeo_page(efx, value, reg, page) \
  213. _ef4_writeo_page(efx, value, \
  214. reg + \
  215. BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
  216. page)
  217. /* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the
  218. * high bits of RX_DESC_UPD or TX_DESC_UPD)
  219. */
  220. static inline void
  221. _ef4_writed_page(struct ef4_nic *efx, const ef4_dword_t *value,
  222. unsigned int reg, unsigned int page)
  223. {
  224. ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
  225. }
  226. #define ef4_writed_page(efx, value, reg, page) \
  227. _ef4_writed_page(efx, value, \
  228. reg + \
  229. BUILD_BUG_ON_ZERO((reg) != 0x400 && \
  230. (reg) != 0x420 && \
  231. (reg) != 0x830 && \
  232. (reg) != 0x83c && \
  233. (reg) != 0xa18 && \
  234. (reg) != 0xa1c), \
  235. page)
  236. /* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
  237. * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
  238. * collector register.
  239. */
  240. static inline void _ef4_writed_page_locked(struct ef4_nic *efx,
  241. const ef4_dword_t *value,
  242. unsigned int reg,
  243. unsigned int page)
  244. {
  245. unsigned long flags __attribute__ ((unused));
  246. if (page == 0) {
  247. spin_lock_irqsave(&efx->biu_lock, flags);
  248. ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
  249. spin_unlock_irqrestore(&efx->biu_lock, flags);
  250. } else {
  251. ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
  252. }
  253. }
  254. #define ef4_writed_page_locked(efx, value, reg, page) \
  255. _ef4_writed_page_locked(efx, value, \
  256. reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
  257. page)
  258. #endif /* EF4_IO_H */