xref: /openbmc/linux/drivers/net/ethernet/sfc/siena/io.h (revision 0b1f77e7)
16e173d3bSMartin Habets /* SPDX-License-Identifier: GPL-2.0-only */
26e173d3bSMartin Habets /****************************************************************************
36e173d3bSMartin Habets  * Driver for Solarflare network controllers and boards
46e173d3bSMartin Habets  * Copyright 2005-2006 Fen Systems Ltd.
56e173d3bSMartin Habets  * Copyright 2006-2013 Solarflare Communications Inc.
66e173d3bSMartin Habets  */
76e173d3bSMartin Habets 
86e173d3bSMartin Habets #ifndef EFX_IO_H
96e173d3bSMartin Habets #define EFX_IO_H
106e173d3bSMartin Habets 
116e173d3bSMartin Habets #include <linux/io.h>
126e173d3bSMartin Habets #include <linux/spinlock.h>
136e173d3bSMartin Habets 
146e173d3bSMartin Habets /**************************************************************************
156e173d3bSMartin Habets  *
166e173d3bSMartin Habets  * NIC register I/O
176e173d3bSMartin Habets  *
186e173d3bSMartin Habets  **************************************************************************
196e173d3bSMartin Habets  *
206e173d3bSMartin Habets  * Notes on locking strategy for the Falcon architecture:
216e173d3bSMartin Habets  *
226e173d3bSMartin Habets  * Many CSRs are very wide and cannot be read or written atomically.
236e173d3bSMartin Habets  * Writes from the host are buffered by the Bus Interface Unit (BIU)
246e173d3bSMartin Habets  * up to 128 bits.  Whenever the host writes part of such a register,
256e173d3bSMartin Habets  * the BIU collects the written value and does not write to the
266e173d3bSMartin Habets  * underlying register until all 4 dwords have been written.  A
276e173d3bSMartin Habets  * similar buffering scheme applies to host access to the NIC's 64-bit
286e173d3bSMartin Habets  * SRAM.
296e173d3bSMartin Habets  *
306e173d3bSMartin Habets  * Writes to different CSRs and 64-bit SRAM words must be serialised,
316e173d3bSMartin Habets  * since interleaved access can result in lost writes.  We use
326e173d3bSMartin Habets  * efx_nic::biu_lock for this.
336e173d3bSMartin Habets  *
346e173d3bSMartin Habets  * We also serialise reads from 128-bit CSRs and SRAM with the same
356e173d3bSMartin Habets  * spinlock.  This may not be necessary, but it doesn't really matter
366e173d3bSMartin Habets  * as there are no such reads on the fast path.
376e173d3bSMartin Habets  *
386e173d3bSMartin Habets  * The DMA descriptor pointers (RX_DESC_UPD and TX_DESC_UPD) are
396e173d3bSMartin Habets  * 128-bit but are special-cased in the BIU to avoid the need for
406e173d3bSMartin Habets  * locking in the host:
416e173d3bSMartin Habets  *
426e173d3bSMartin Habets  * - They are write-only.
436e173d3bSMartin Habets  * - The semantics of writing to these registers are such that
446e173d3bSMartin Habets  *   replacing the low 96 bits with zero does not affect functionality.
456e173d3bSMartin Habets  * - If the host writes to the last dword address of such a register
466e173d3bSMartin Habets  *   (i.e. the high 32 bits) the underlying register will always be
476e173d3bSMartin Habets  *   written.  If the collector and the current write together do not
486e173d3bSMartin Habets  *   provide values for all 128 bits of the register, the low 96 bits
496e173d3bSMartin Habets  *   will be written as zero.
506e173d3bSMartin Habets  * - If the host writes to the address of any other part of such a
516e173d3bSMartin Habets  *   register while the collector already holds values for some other
526e173d3bSMartin Habets  *   register, the write is discarded and the collector maintains its
536e173d3bSMartin Habets  *   current state.
546e173d3bSMartin Habets  *
556e173d3bSMartin Habets  * The EF10 architecture exposes very few registers to the host and
566e173d3bSMartin Habets  * most of them are only 32 bits wide.  The only exceptions are the MC
576e173d3bSMartin Habets  * doorbell register pair, which has its own latching, and
586e173d3bSMartin Habets  * TX_DESC_UPD, which works in a similar way to the Falcon
596e173d3bSMartin Habets  * architecture.
606e173d3bSMartin Habets  */
616e173d3bSMartin Habets 
626e173d3bSMartin Habets #if BITS_PER_LONG == 64
636e173d3bSMartin Habets #define EFX_USE_QWORD_IO 1
646e173d3bSMartin Habets #endif
656e173d3bSMartin Habets 
666e173d3bSMartin Habets /* Hardware issue requires that only 64-bit naturally aligned writes
676e173d3bSMartin Habets  * are seen by hardware. Its not strictly necessary to restrict to
686e173d3bSMartin Habets  * x86_64 arch, but done for safety since unusual write combining behaviour
696e173d3bSMartin Habets  * can break PIO.
706e173d3bSMartin Habets  */
716e173d3bSMartin Habets #ifdef CONFIG_X86_64
726e173d3bSMartin Habets /* PIO is a win only if write-combining is possible */
73*0b1f77e7SBaoquan He #ifdef ioremap_wc
746e173d3bSMartin Habets #define EFX_USE_PIO 1
756e173d3bSMartin Habets #endif
766e173d3bSMartin Habets #endif
776e173d3bSMartin Habets 
efx_reg(struct efx_nic * efx,unsigned int reg)786e173d3bSMartin Habets static inline u32 efx_reg(struct efx_nic *efx, unsigned int reg)
796e173d3bSMartin Habets {
806e173d3bSMartin Habets 	return efx->reg_base + reg;
816e173d3bSMartin Habets }
826e173d3bSMartin Habets 
836e173d3bSMartin Habets #ifdef EFX_USE_QWORD_IO
_efx_writeq(struct efx_nic * efx,__le64 value,unsigned int reg)846e173d3bSMartin Habets static inline void _efx_writeq(struct efx_nic *efx, __le64 value,
856e173d3bSMartin Habets 				  unsigned int reg)
866e173d3bSMartin Habets {
876e173d3bSMartin Habets 	__raw_writeq((__force u64)value, efx->membase + reg);
886e173d3bSMartin Habets }
_efx_readq(struct efx_nic * efx,unsigned int reg)896e173d3bSMartin Habets static inline __le64 _efx_readq(struct efx_nic *efx, unsigned int reg)
906e173d3bSMartin Habets {
916e173d3bSMartin Habets 	return (__force __le64)__raw_readq(efx->membase + reg);
926e173d3bSMartin Habets }
936e173d3bSMartin Habets #endif
946e173d3bSMartin Habets 
_efx_writed(struct efx_nic * efx,__le32 value,unsigned int reg)956e173d3bSMartin Habets static inline void _efx_writed(struct efx_nic *efx, __le32 value,
966e173d3bSMartin Habets 				  unsigned int reg)
976e173d3bSMartin Habets {
986e173d3bSMartin Habets 	__raw_writel((__force u32)value, efx->membase + reg);
996e173d3bSMartin Habets }
_efx_readd(struct efx_nic * efx,unsigned int reg)1006e173d3bSMartin Habets static inline __le32 _efx_readd(struct efx_nic *efx, unsigned int reg)
1016e173d3bSMartin Habets {
1026e173d3bSMartin Habets 	return (__force __le32)__raw_readl(efx->membase + reg);
1036e173d3bSMartin Habets }
1046e173d3bSMartin Habets 
1056e173d3bSMartin Habets /* Write a normal 128-bit CSR, locking as appropriate. */
efx_writeo(struct efx_nic * efx,const efx_oword_t * value,unsigned int reg)1066e173d3bSMartin Habets static inline void efx_writeo(struct efx_nic *efx, const efx_oword_t *value,
1076e173d3bSMartin Habets 			      unsigned int reg)
1086e173d3bSMartin Habets {
1096e173d3bSMartin Habets 	unsigned long flags __attribute__ ((unused));
1106e173d3bSMartin Habets 
1116e173d3bSMartin Habets 	netif_vdbg(efx, hw, efx->net_dev,
1126e173d3bSMartin Habets 		   "writing register %x with " EFX_OWORD_FMT "\n", reg,
1136e173d3bSMartin Habets 		   EFX_OWORD_VAL(*value));
1146e173d3bSMartin Habets 
1156e173d3bSMartin Habets 	spin_lock_irqsave(&efx->biu_lock, flags);
1166e173d3bSMartin Habets #ifdef EFX_USE_QWORD_IO
1176e173d3bSMartin Habets 	_efx_writeq(efx, value->u64[0], reg + 0);
1186e173d3bSMartin Habets 	_efx_writeq(efx, value->u64[1], reg + 8);
1196e173d3bSMartin Habets #else
1206e173d3bSMartin Habets 	_efx_writed(efx, value->u32[0], reg + 0);
1216e173d3bSMartin Habets 	_efx_writed(efx, value->u32[1], reg + 4);
1226e173d3bSMartin Habets 	_efx_writed(efx, value->u32[2], reg + 8);
1236e173d3bSMartin Habets 	_efx_writed(efx, value->u32[3], reg + 12);
1246e173d3bSMartin Habets #endif
1256e173d3bSMartin Habets 	spin_unlock_irqrestore(&efx->biu_lock, flags);
1266e173d3bSMartin Habets }
1276e173d3bSMartin Habets 
1286e173d3bSMartin Habets /* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
efx_sram_writeq(struct efx_nic * efx,void __iomem * membase,const efx_qword_t * value,unsigned int index)1296e173d3bSMartin Habets static inline void efx_sram_writeq(struct efx_nic *efx, void __iomem *membase,
1306e173d3bSMartin Habets 				   const efx_qword_t *value, unsigned int index)
1316e173d3bSMartin Habets {
1326e173d3bSMartin Habets 	unsigned int addr = index * sizeof(*value);
1336e173d3bSMartin Habets 	unsigned long flags __attribute__ ((unused));
1346e173d3bSMartin Habets 
1356e173d3bSMartin Habets 	netif_vdbg(efx, hw, efx->net_dev,
1366e173d3bSMartin Habets 		   "writing SRAM address %x with " EFX_QWORD_FMT "\n",
1376e173d3bSMartin Habets 		   addr, EFX_QWORD_VAL(*value));
1386e173d3bSMartin Habets 
1396e173d3bSMartin Habets 	spin_lock_irqsave(&efx->biu_lock, flags);
1406e173d3bSMartin Habets #ifdef EFX_USE_QWORD_IO
1416e173d3bSMartin Habets 	__raw_writeq((__force u64)value->u64[0], membase + addr);
1426e173d3bSMartin Habets #else
1436e173d3bSMartin Habets 	__raw_writel((__force u32)value->u32[0], membase + addr);
1446e173d3bSMartin Habets 	__raw_writel((__force u32)value->u32[1], membase + addr + 4);
1456e173d3bSMartin Habets #endif
1466e173d3bSMartin Habets 	spin_unlock_irqrestore(&efx->biu_lock, flags);
1476e173d3bSMartin Habets }
1486e173d3bSMartin Habets 
1496e173d3bSMartin Habets /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
efx_writed(struct efx_nic * efx,const efx_dword_t * value,unsigned int reg)1506e173d3bSMartin Habets static inline void efx_writed(struct efx_nic *efx, const efx_dword_t *value,
1516e173d3bSMartin Habets 			      unsigned int reg)
1526e173d3bSMartin Habets {
1536e173d3bSMartin Habets 	netif_vdbg(efx, hw, efx->net_dev,
1546e173d3bSMartin Habets 		   "writing register %x with "EFX_DWORD_FMT"\n",
1556e173d3bSMartin Habets 		   reg, EFX_DWORD_VAL(*value));
1566e173d3bSMartin Habets 
1576e173d3bSMartin Habets 	/* No lock required */
1586e173d3bSMartin Habets 	_efx_writed(efx, value->u32[0], reg);
1596e173d3bSMartin Habets }
1606e173d3bSMartin Habets 
1616e173d3bSMartin Habets /* Read a 128-bit CSR, locking as appropriate. */
efx_reado(struct efx_nic * efx,efx_oword_t * value,unsigned int reg)1626e173d3bSMartin Habets static inline void efx_reado(struct efx_nic *efx, efx_oword_t *value,
1636e173d3bSMartin Habets 			     unsigned int reg)
1646e173d3bSMartin Habets {
1656e173d3bSMartin Habets 	unsigned long flags __attribute__ ((unused));
1666e173d3bSMartin Habets 
1676e173d3bSMartin Habets 	spin_lock_irqsave(&efx->biu_lock, flags);
1686e173d3bSMartin Habets 	value->u32[0] = _efx_readd(efx, reg + 0);
1696e173d3bSMartin Habets 	value->u32[1] = _efx_readd(efx, reg + 4);
1706e173d3bSMartin Habets 	value->u32[2] = _efx_readd(efx, reg + 8);
1716e173d3bSMartin Habets 	value->u32[3] = _efx_readd(efx, reg + 12);
1726e173d3bSMartin Habets 	spin_unlock_irqrestore(&efx->biu_lock, flags);
1736e173d3bSMartin Habets 
1746e173d3bSMartin Habets 	netif_vdbg(efx, hw, efx->net_dev,
1756e173d3bSMartin Habets 		   "read from register %x, got " EFX_OWORD_FMT "\n", reg,
1766e173d3bSMartin Habets 		   EFX_OWORD_VAL(*value));
1776e173d3bSMartin Habets }
1786e173d3bSMartin Habets 
1796e173d3bSMartin Habets /* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
efx_sram_readq(struct efx_nic * efx,void __iomem * membase,efx_qword_t * value,unsigned int index)1806e173d3bSMartin Habets static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
1816e173d3bSMartin Habets 				  efx_qword_t *value, unsigned int index)
1826e173d3bSMartin Habets {
1836e173d3bSMartin Habets 	unsigned int addr = index * sizeof(*value);
1846e173d3bSMartin Habets 	unsigned long flags __attribute__ ((unused));
1856e173d3bSMartin Habets 
1866e173d3bSMartin Habets 	spin_lock_irqsave(&efx->biu_lock, flags);
1876e173d3bSMartin Habets #ifdef EFX_USE_QWORD_IO
1886e173d3bSMartin Habets 	value->u64[0] = (__force __le64)__raw_readq(membase + addr);
1896e173d3bSMartin Habets #else
1906e173d3bSMartin Habets 	value->u32[0] = (__force __le32)__raw_readl(membase + addr);
1916e173d3bSMartin Habets 	value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
1926e173d3bSMartin Habets #endif
1936e173d3bSMartin Habets 	spin_unlock_irqrestore(&efx->biu_lock, flags);
1946e173d3bSMartin Habets 
1956e173d3bSMartin Habets 	netif_vdbg(efx, hw, efx->net_dev,
1966e173d3bSMartin Habets 		   "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
1976e173d3bSMartin Habets 		   addr, EFX_QWORD_VAL(*value));
1986e173d3bSMartin Habets }
1996e173d3bSMartin Habets 
2006e173d3bSMartin Habets /* Read a 32-bit CSR or SRAM */
efx_readd(struct efx_nic * efx,efx_dword_t * value,unsigned int reg)2016e173d3bSMartin Habets static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
2026e173d3bSMartin Habets 				unsigned int reg)
2036e173d3bSMartin Habets {
2046e173d3bSMartin Habets 	value->u32[0] = _efx_readd(efx, reg);
2056e173d3bSMartin Habets 	netif_vdbg(efx, hw, efx->net_dev,
2066e173d3bSMartin Habets 		   "read from register %x, got "EFX_DWORD_FMT"\n",
2076e173d3bSMartin Habets 		   reg, EFX_DWORD_VAL(*value));
2086e173d3bSMartin Habets }
2096e173d3bSMartin Habets 
2106e173d3bSMartin Habets /* Write a 128-bit CSR forming part of a table */
2116e173d3bSMartin Habets static inline void
efx_writeo_table(struct efx_nic * efx,const efx_oword_t * value,unsigned int reg,unsigned int index)2126e173d3bSMartin Habets efx_writeo_table(struct efx_nic *efx, const efx_oword_t *value,
2136e173d3bSMartin Habets 		 unsigned int reg, unsigned int index)
2146e173d3bSMartin Habets {
2156e173d3bSMartin Habets 	efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
2166e173d3bSMartin Habets }
2176e173d3bSMartin Habets 
2186e173d3bSMartin Habets /* Read a 128-bit CSR forming part of a table */
efx_reado_table(struct efx_nic * efx,efx_oword_t * value,unsigned int reg,unsigned int index)2196e173d3bSMartin Habets static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
2206e173d3bSMartin Habets 				     unsigned int reg, unsigned int index)
2216e173d3bSMartin Habets {
2226e173d3bSMartin Habets 	efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
2236e173d3bSMartin Habets }
2246e173d3bSMartin Habets 
2256e173d3bSMartin Habets /* default VI stride (step between per-VI registers) is 8K on EF10 and
2266e173d3bSMartin Habets  * 64K on EF100
2276e173d3bSMartin Habets  */
2286e173d3bSMartin Habets #define EFX_DEFAULT_VI_STRIDE		0x2000
2296e173d3bSMartin Habets #define EF100_DEFAULT_VI_STRIDE		0x10000
2306e173d3bSMartin Habets 
2316e173d3bSMartin Habets /* Calculate offset to page-mapped register */
efx_paged_reg(struct efx_nic * efx,unsigned int page,unsigned int reg)2326e173d3bSMartin Habets static inline unsigned int efx_paged_reg(struct efx_nic *efx, unsigned int page,
2336e173d3bSMartin Habets 					 unsigned int reg)
2346e173d3bSMartin Habets {
2356e173d3bSMartin Habets 	return page * efx->vi_stride + reg;
2366e173d3bSMartin Habets }
2376e173d3bSMartin Habets 
2386e173d3bSMartin Habets /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
_efx_writeo_page(struct efx_nic * efx,efx_oword_t * value,unsigned int reg,unsigned int page)2396e173d3bSMartin Habets static inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
2406e173d3bSMartin Habets 				    unsigned int reg, unsigned int page)
2416e173d3bSMartin Habets {
2426e173d3bSMartin Habets 	reg = efx_paged_reg(efx, page, reg);
2436e173d3bSMartin Habets 
2446e173d3bSMartin Habets 	netif_vdbg(efx, hw, efx->net_dev,
2456e173d3bSMartin Habets 		   "writing register %x with " EFX_OWORD_FMT "\n", reg,
2466e173d3bSMartin Habets 		   EFX_OWORD_VAL(*value));
2476e173d3bSMartin Habets 
2486e173d3bSMartin Habets #ifdef EFX_USE_QWORD_IO
2496e173d3bSMartin Habets 	_efx_writeq(efx, value->u64[0], reg + 0);
2506e173d3bSMartin Habets 	_efx_writeq(efx, value->u64[1], reg + 8);
2516e173d3bSMartin Habets #else
2526e173d3bSMartin Habets 	_efx_writed(efx, value->u32[0], reg + 0);
2536e173d3bSMartin Habets 	_efx_writed(efx, value->u32[1], reg + 4);
2546e173d3bSMartin Habets 	_efx_writed(efx, value->u32[2], reg + 8);
2556e173d3bSMartin Habets 	_efx_writed(efx, value->u32[3], reg + 12);
2566e173d3bSMartin Habets #endif
2576e173d3bSMartin Habets }
2586e173d3bSMartin Habets #define efx_writeo_page(efx, value, reg, page)				\
2596e173d3bSMartin Habets 	_efx_writeo_page(efx, value,					\
2606e173d3bSMartin Habets 			 reg +						\
2616e173d3bSMartin Habets 			 BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
2626e173d3bSMartin Habets 			 page)
2636e173d3bSMartin Habets 
2646e173d3bSMartin Habets /* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the
2656e173d3bSMartin Habets  * high bits of RX_DESC_UPD or TX_DESC_UPD)
2666e173d3bSMartin Habets  */
2676e173d3bSMartin Habets static inline void
_efx_writed_page(struct efx_nic * efx,const efx_dword_t * value,unsigned int reg,unsigned int page)2686e173d3bSMartin Habets _efx_writed_page(struct efx_nic *efx, const efx_dword_t *value,
2696e173d3bSMartin Habets 		 unsigned int reg, unsigned int page)
2706e173d3bSMartin Habets {
2716e173d3bSMartin Habets 	efx_writed(efx, value, efx_paged_reg(efx, page, reg));
2726e173d3bSMartin Habets }
2736e173d3bSMartin Habets #define efx_writed_page(efx, value, reg, page)				\
2746e173d3bSMartin Habets 	_efx_writed_page(efx, value,					\
2756e173d3bSMartin Habets 			 reg +						\
2766e173d3bSMartin Habets 			 BUILD_BUG_ON_ZERO((reg) != 0x180 &&		\
2776e173d3bSMartin Habets 					   (reg) != 0x200 &&		\
2786e173d3bSMartin Habets 					   (reg) != 0x400 &&		\
2796e173d3bSMartin Habets 					   (reg) != 0x420 &&		\
2806e173d3bSMartin Habets 					   (reg) != 0x830 &&		\
2816e173d3bSMartin Habets 					   (reg) != 0x83c &&		\
2826e173d3bSMartin Habets 					   (reg) != 0xa18 &&		\
2836e173d3bSMartin Habets 					   (reg) != 0xa1c),		\
2846e173d3bSMartin Habets 			 page)
2856e173d3bSMartin Habets 
2866e173d3bSMartin Habets /* Write TIMER_COMMAND.  This is a page-mapped 32-bit CSR, but a bug
2876e173d3bSMartin Habets  * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
2886e173d3bSMartin Habets  * collector register.
2896e173d3bSMartin Habets  */
_efx_writed_page_locked(struct efx_nic * efx,const efx_dword_t * value,unsigned int reg,unsigned int page)2906e173d3bSMartin Habets static inline void _efx_writed_page_locked(struct efx_nic *efx,
2916e173d3bSMartin Habets 					   const efx_dword_t *value,
2926e173d3bSMartin Habets 					   unsigned int reg,
2936e173d3bSMartin Habets 					   unsigned int page)
2946e173d3bSMartin Habets {
2956e173d3bSMartin Habets 	unsigned long flags __attribute__ ((unused));
2966e173d3bSMartin Habets 
2976e173d3bSMartin Habets 	if (page == 0) {
2986e173d3bSMartin Habets 		spin_lock_irqsave(&efx->biu_lock, flags);
2996e173d3bSMartin Habets 		efx_writed(efx, value, efx_paged_reg(efx, page, reg));
3006e173d3bSMartin Habets 		spin_unlock_irqrestore(&efx->biu_lock, flags);
3016e173d3bSMartin Habets 	} else {
3026e173d3bSMartin Habets 		efx_writed(efx, value, efx_paged_reg(efx, page, reg));
3036e173d3bSMartin Habets 	}
3046e173d3bSMartin Habets }
3056e173d3bSMartin Habets #define efx_writed_page_locked(efx, value, reg, page)			\
3066e173d3bSMartin Habets 	_efx_writed_page_locked(efx, value,				\
3076e173d3bSMartin Habets 				reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
3086e173d3bSMartin Habets 				page)
3096e173d3bSMartin Habets 
3106e173d3bSMartin Habets #endif /* EFX_IO_H */
311