io.h 9.8 KB

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