io.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. * The EF10 architecture exposes very few registers to the host and
  18. * most of them are only 32 bits wide. The only exceptions are the MC
  19. * doorbell register pair, which has its own latching, and
  20. * TX_DESC_UPD.
  21. *
  22. * The TX_DESC_UPD DMA descriptor pointer is 128-bits but is a special
  23. * case in the BIU to avoid the need for locking in the host:
  24. *
  25. * - It is write-only.
  26. * - The semantics of writing to this register is such that
  27. * replacing the low 96 bits with zero does not affect functionality.
  28. * - If the host writes to the last dword address of the register
  29. * (i.e. the high 32 bits) the underlying register will always be
  30. * written. If the collector and the current write together do not
  31. * provide values for all 128 bits of the register, the low 96 bits
  32. * will be written as zero.
  33. */
  34. #if BITS_PER_LONG == 64
  35. #define EFX_USE_QWORD_IO 1
  36. #endif
  37. /* Hardware issue requires that only 64-bit naturally aligned writes
  38. * are seen by hardware. Its not strictly necessary to restrict to
  39. * x86_64 arch, but done for safety since unusual write combining behaviour
  40. * can break PIO.
  41. */
  42. #ifdef CONFIG_X86_64
  43. /* PIO is a win only if write-combining is possible */
  44. #ifdef ioremap_wc
  45. #define EFX_USE_PIO 1
  46. #endif
  47. #endif
  48. static inline u32 efx_reg(struct efx_nic *efx, unsigned int reg)
  49. {
  50. return efx->reg_base + reg;
  51. }
  52. #ifdef EFX_USE_QWORD_IO
  53. static inline void _efx_writeq(struct efx_nic *efx, __le64 value,
  54. unsigned int reg)
  55. {
  56. __raw_writeq((__force u64)value, efx->membase + reg);
  57. }
  58. static inline __le64 _efx_readq(struct efx_nic *efx, unsigned int reg)
  59. {
  60. return (__force __le64)__raw_readq(efx->membase + reg);
  61. }
  62. #endif
  63. static inline void _efx_writed(struct efx_nic *efx, __le32 value,
  64. unsigned int reg)
  65. {
  66. __raw_writel((__force u32)value, efx->membase + reg);
  67. }
  68. static inline __le32 _efx_readd(struct efx_nic *efx, unsigned int reg)
  69. {
  70. return (__force __le32)__raw_readl(efx->membase + reg);
  71. }
  72. /* Write a normal 128-bit CSR, locking as appropriate. */
  73. static inline void efx_writeo(struct efx_nic *efx, const efx_oword_t *value,
  74. unsigned int reg)
  75. {
  76. unsigned long flags __attribute__ ((unused));
  77. netif_vdbg(efx, hw, efx->net_dev,
  78. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  79. EFX_OWORD_VAL(*value));
  80. spin_lock_irqsave(&efx->biu_lock, flags);
  81. #ifdef EFX_USE_QWORD_IO
  82. _efx_writeq(efx, value->u64[0], reg + 0);
  83. _efx_writeq(efx, value->u64[1], reg + 8);
  84. #else
  85. _efx_writed(efx, value->u32[0], reg + 0);
  86. _efx_writed(efx, value->u32[1], reg + 4);
  87. _efx_writed(efx, value->u32[2], reg + 8);
  88. _efx_writed(efx, value->u32[3], reg + 12);
  89. #endif
  90. spin_unlock_irqrestore(&efx->biu_lock, flags);
  91. }
  92. /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
  93. static inline void efx_writed(struct efx_nic *efx, const efx_dword_t *value,
  94. unsigned int reg)
  95. {
  96. netif_vdbg(efx, hw, efx->net_dev,
  97. "writing register %x with "EFX_DWORD_FMT"\n",
  98. reg, EFX_DWORD_VAL(*value));
  99. /* No lock required */
  100. _efx_writed(efx, value->u32[0], reg);
  101. }
  102. /* Read a 128-bit CSR, locking as appropriate. */
  103. static inline void efx_reado(struct efx_nic *efx, efx_oword_t *value,
  104. unsigned int reg)
  105. {
  106. unsigned long flags __attribute__ ((unused));
  107. spin_lock_irqsave(&efx->biu_lock, flags);
  108. value->u32[0] = _efx_readd(efx, reg + 0);
  109. value->u32[1] = _efx_readd(efx, reg + 4);
  110. value->u32[2] = _efx_readd(efx, reg + 8);
  111. value->u32[3] = _efx_readd(efx, reg + 12);
  112. spin_unlock_irqrestore(&efx->biu_lock, flags);
  113. netif_vdbg(efx, hw, efx->net_dev,
  114. "read from register %x, got " EFX_OWORD_FMT "\n", reg,
  115. EFX_OWORD_VAL(*value));
  116. }
  117. /* Read a 32-bit CSR or SRAM */
  118. static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
  119. unsigned int reg)
  120. {
  121. value->u32[0] = _efx_readd(efx, reg);
  122. netif_vdbg(efx, hw, efx->net_dev,
  123. "read from register %x, got "EFX_DWORD_FMT"\n",
  124. reg, EFX_DWORD_VAL(*value));
  125. }
  126. /* Write a 128-bit CSR forming part of a table */
  127. static inline void
  128. efx_writeo_table(struct efx_nic *efx, const efx_oword_t *value,
  129. unsigned int reg, unsigned int index)
  130. {
  131. efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
  132. }
  133. /* Read a 128-bit CSR forming part of a table */
  134. static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
  135. unsigned int reg, unsigned int index)
  136. {
  137. efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
  138. }
  139. /* default VI stride (step between per-VI registers) is 8K on EF10 and
  140. * 64K on EF100
  141. */
  142. #define EFX_DEFAULT_VI_STRIDE 0x2000
  143. #define EF100_DEFAULT_VI_STRIDE 0x10000
  144. /* Calculate offset to page-mapped register */
  145. static inline unsigned int efx_paged_reg(struct efx_nic *efx, unsigned int page,
  146. unsigned int reg)
  147. {
  148. return page * efx->vi_stride + reg;
  149. }
  150. /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
  151. static inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
  152. unsigned int reg, unsigned int page)
  153. {
  154. reg = efx_paged_reg(efx, page, reg);
  155. netif_vdbg(efx, hw, efx->net_dev,
  156. "writing register %x with " EFX_OWORD_FMT "\n", reg,
  157. EFX_OWORD_VAL(*value));
  158. #ifdef EFX_USE_QWORD_IO
  159. _efx_writeq(efx, value->u64[0], reg + 0);
  160. _efx_writeq(efx, value->u64[1], reg + 8);
  161. #else
  162. _efx_writed(efx, value->u32[0], reg + 0);
  163. _efx_writed(efx, value->u32[1], reg + 4);
  164. _efx_writed(efx, value->u32[2], reg + 8);
  165. _efx_writed(efx, value->u32[3], reg + 12);
  166. #endif
  167. }
  168. #define efx_writeo_page(efx, value, reg, page) \
  169. _efx_writeo_page(efx, value, \
  170. reg + \
  171. BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
  172. page)
  173. /* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the
  174. * high bits of RX_DESC_UPD or TX_DESC_UPD)
  175. */
  176. static inline void
  177. _efx_writed_page(struct efx_nic *efx, const efx_dword_t *value,
  178. unsigned int reg, unsigned int page)
  179. {
  180. efx_writed(efx, value, efx_paged_reg(efx, page, reg));
  181. }
  182. #define efx_writed_page(efx, value, reg, page) \
  183. _efx_writed_page(efx, value, \
  184. reg + \
  185. BUILD_BUG_ON_ZERO((reg) != 0x180 && \
  186. (reg) != 0x200 && \
  187. (reg) != 0x400 && \
  188. (reg) != 0x420 && \
  189. (reg) != 0x830 && \
  190. (reg) != 0x83c && \
  191. (reg) != 0xa18 && \
  192. (reg) != 0xa1c), \
  193. page)
  194. #endif /* EFX_IO_H */