18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/****************************************************************************
38c2ecf20Sopenharmony_ci * Driver for Solarflare network controllers and boards
48c2ecf20Sopenharmony_ci * Copyright 2005-2006 Fen Systems Ltd.
58c2ecf20Sopenharmony_ci * Copyright 2006-2013 Solarflare Communications Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef EF4_IO_H
98c2ecf20Sopenharmony_ci#define EF4_IO_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/**************************************************************************
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * NIC register I/O
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci **************************************************************************
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Notes on locking strategy for the Falcon architecture:
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Many CSRs are very wide and cannot be read or written atomically.
238c2ecf20Sopenharmony_ci * Writes from the host are buffered by the Bus Interface Unit (BIU)
248c2ecf20Sopenharmony_ci * up to 128 bits.  Whenever the host writes part of such a register,
258c2ecf20Sopenharmony_ci * the BIU collects the written value and does not write to the
268c2ecf20Sopenharmony_ci * underlying register until all 4 dwords have been written.  A
278c2ecf20Sopenharmony_ci * similar buffering scheme applies to host access to the NIC's 64-bit
288c2ecf20Sopenharmony_ci * SRAM.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * Writes to different CSRs and 64-bit SRAM words must be serialised,
318c2ecf20Sopenharmony_ci * since interleaved access can result in lost writes.  We use
328c2ecf20Sopenharmony_ci * ef4_nic::biu_lock for this.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * We also serialise reads from 128-bit CSRs and SRAM with the same
358c2ecf20Sopenharmony_ci * spinlock.  This may not be necessary, but it doesn't really matter
368c2ecf20Sopenharmony_ci * as there are no such reads on the fast path.
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * The DMA descriptor pointers (RX_DESC_UPD and TX_DESC_UPD) are
398c2ecf20Sopenharmony_ci * 128-bit but are special-cased in the BIU to avoid the need for
408c2ecf20Sopenharmony_ci * locking in the host:
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * - They are write-only.
438c2ecf20Sopenharmony_ci * - The semantics of writing to these registers are such that
448c2ecf20Sopenharmony_ci *   replacing the low 96 bits with zero does not affect functionality.
458c2ecf20Sopenharmony_ci * - If the host writes to the last dword address of such a register
468c2ecf20Sopenharmony_ci *   (i.e. the high 32 bits) the underlying register will always be
478c2ecf20Sopenharmony_ci *   written.  If the collector and the current write together do not
488c2ecf20Sopenharmony_ci *   provide values for all 128 bits of the register, the low 96 bits
498c2ecf20Sopenharmony_ci *   will be written as zero.
508c2ecf20Sopenharmony_ci * - If the host writes to the address of any other part of such a
518c2ecf20Sopenharmony_ci *   register while the collector already holds values for some other
528c2ecf20Sopenharmony_ci *   register, the write is discarded and the collector maintains its
538c2ecf20Sopenharmony_ci *   current state.
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * The EF10 architecture exposes very few registers to the host and
568c2ecf20Sopenharmony_ci * most of them are only 32 bits wide.  The only exceptions are the MC
578c2ecf20Sopenharmony_ci * doorbell register pair, which has its own latching, and
588c2ecf20Sopenharmony_ci * TX_DESC_UPD, which works in a similar way to the Falcon
598c2ecf20Sopenharmony_ci * architecture.
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64
638c2ecf20Sopenharmony_ci#define EF4_USE_QWORD_IO 1
648c2ecf20Sopenharmony_ci#endif
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#ifdef EF4_USE_QWORD_IO
678c2ecf20Sopenharmony_cistatic inline void _ef4_writeq(struct ef4_nic *efx, __le64 value,
688c2ecf20Sopenharmony_ci				  unsigned int reg)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	__raw_writeq((__force u64)value, efx->membase + reg);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_cistatic inline __le64 _ef4_readq(struct ef4_nic *efx, unsigned int reg)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	return (__force __le64)__raw_readq(efx->membase + reg);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci#endif
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic inline void _ef4_writed(struct ef4_nic *efx, __le32 value,
798c2ecf20Sopenharmony_ci				  unsigned int reg)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	__raw_writel((__force u32)value, efx->membase + reg);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_cistatic inline __le32 _ef4_readd(struct ef4_nic *efx, unsigned int reg)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	return (__force __le32)__raw_readl(efx->membase + reg);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* Write a normal 128-bit CSR, locking as appropriate. */
898c2ecf20Sopenharmony_cistatic inline void ef4_writeo(struct ef4_nic *efx, const ef4_oword_t *value,
908c2ecf20Sopenharmony_ci			      unsigned int reg)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	unsigned long flags __attribute__ ((unused));
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
958c2ecf20Sopenharmony_ci		   "writing register %x with " EF4_OWORD_FMT "\n", reg,
968c2ecf20Sopenharmony_ci		   EF4_OWORD_VAL(*value));
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	spin_lock_irqsave(&efx->biu_lock, flags);
998c2ecf20Sopenharmony_ci#ifdef EF4_USE_QWORD_IO
1008c2ecf20Sopenharmony_ci	_ef4_writeq(efx, value->u64[0], reg + 0);
1018c2ecf20Sopenharmony_ci	_ef4_writeq(efx, value->u64[1], reg + 8);
1028c2ecf20Sopenharmony_ci#else
1038c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[0], reg + 0);
1048c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[1], reg + 4);
1058c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[2], reg + 8);
1068c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[3], reg + 12);
1078c2ecf20Sopenharmony_ci#endif
1088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&efx->biu_lock, flags);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
1128c2ecf20Sopenharmony_cistatic inline void ef4_sram_writeq(struct ef4_nic *efx, void __iomem *membase,
1138c2ecf20Sopenharmony_ci				   const ef4_qword_t *value, unsigned int index)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	unsigned int addr = index * sizeof(*value);
1168c2ecf20Sopenharmony_ci	unsigned long flags __attribute__ ((unused));
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
1198c2ecf20Sopenharmony_ci		   "writing SRAM address %x with " EF4_QWORD_FMT "\n",
1208c2ecf20Sopenharmony_ci		   addr, EF4_QWORD_VAL(*value));
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	spin_lock_irqsave(&efx->biu_lock, flags);
1238c2ecf20Sopenharmony_ci#ifdef EF4_USE_QWORD_IO
1248c2ecf20Sopenharmony_ci	__raw_writeq((__force u64)value->u64[0], membase + addr);
1258c2ecf20Sopenharmony_ci#else
1268c2ecf20Sopenharmony_ci	__raw_writel((__force u32)value->u32[0], membase + addr);
1278c2ecf20Sopenharmony_ci	__raw_writel((__force u32)value->u32[1], membase + addr + 4);
1288c2ecf20Sopenharmony_ci#endif
1298c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&efx->biu_lock, flags);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
1338c2ecf20Sopenharmony_cistatic inline void ef4_writed(struct ef4_nic *efx, const ef4_dword_t *value,
1348c2ecf20Sopenharmony_ci			      unsigned int reg)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
1378c2ecf20Sopenharmony_ci		   "writing register %x with "EF4_DWORD_FMT"\n",
1388c2ecf20Sopenharmony_ci		   reg, EF4_DWORD_VAL(*value));
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* No lock required */
1418c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[0], reg);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/* Read a 128-bit CSR, locking as appropriate. */
1458c2ecf20Sopenharmony_cistatic inline void ef4_reado(struct ef4_nic *efx, ef4_oword_t *value,
1468c2ecf20Sopenharmony_ci			     unsigned int reg)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	unsigned long flags __attribute__ ((unused));
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	spin_lock_irqsave(&efx->biu_lock, flags);
1518c2ecf20Sopenharmony_ci	value->u32[0] = _ef4_readd(efx, reg + 0);
1528c2ecf20Sopenharmony_ci	value->u32[1] = _ef4_readd(efx, reg + 4);
1538c2ecf20Sopenharmony_ci	value->u32[2] = _ef4_readd(efx, reg + 8);
1548c2ecf20Sopenharmony_ci	value->u32[3] = _ef4_readd(efx, reg + 12);
1558c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&efx->biu_lock, flags);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
1588c2ecf20Sopenharmony_ci		   "read from register %x, got " EF4_OWORD_FMT "\n", reg,
1598c2ecf20Sopenharmony_ci		   EF4_OWORD_VAL(*value));
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
1638c2ecf20Sopenharmony_cistatic inline void ef4_sram_readq(struct ef4_nic *efx, void __iomem *membase,
1648c2ecf20Sopenharmony_ci				  ef4_qword_t *value, unsigned int index)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	unsigned int addr = index * sizeof(*value);
1678c2ecf20Sopenharmony_ci	unsigned long flags __attribute__ ((unused));
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	spin_lock_irqsave(&efx->biu_lock, flags);
1708c2ecf20Sopenharmony_ci#ifdef EF4_USE_QWORD_IO
1718c2ecf20Sopenharmony_ci	value->u64[0] = (__force __le64)__raw_readq(membase + addr);
1728c2ecf20Sopenharmony_ci#else
1738c2ecf20Sopenharmony_ci	value->u32[0] = (__force __le32)__raw_readl(membase + addr);
1748c2ecf20Sopenharmony_ci	value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
1758c2ecf20Sopenharmony_ci#endif
1768c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&efx->biu_lock, flags);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
1798c2ecf20Sopenharmony_ci		   "read from SRAM address %x, got "EF4_QWORD_FMT"\n",
1808c2ecf20Sopenharmony_ci		   addr, EF4_QWORD_VAL(*value));
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* Read a 32-bit CSR or SRAM */
1848c2ecf20Sopenharmony_cistatic inline void ef4_readd(struct ef4_nic *efx, ef4_dword_t *value,
1858c2ecf20Sopenharmony_ci				unsigned int reg)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	value->u32[0] = _ef4_readd(efx, reg);
1888c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
1898c2ecf20Sopenharmony_ci		   "read from register %x, got "EF4_DWORD_FMT"\n",
1908c2ecf20Sopenharmony_ci		   reg, EF4_DWORD_VAL(*value));
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/* Write a 128-bit CSR forming part of a table */
1948c2ecf20Sopenharmony_cistatic inline void
1958c2ecf20Sopenharmony_cief4_writeo_table(struct ef4_nic *efx, const ef4_oword_t *value,
1968c2ecf20Sopenharmony_ci		 unsigned int reg, unsigned int index)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	ef4_writeo(efx, value, reg + index * sizeof(ef4_oword_t));
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/* Read a 128-bit CSR forming part of a table */
2028c2ecf20Sopenharmony_cistatic inline void ef4_reado_table(struct ef4_nic *efx, ef4_oword_t *value,
2038c2ecf20Sopenharmony_ci				     unsigned int reg, unsigned int index)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	ef4_reado(efx, value, reg + index * sizeof(ef4_oword_t));
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci/* Page size used as step between per-VI registers */
2098c2ecf20Sopenharmony_ci#define EF4_VI_PAGE_SIZE 0x2000
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/* Calculate offset to page-mapped register */
2128c2ecf20Sopenharmony_ci#define EF4_PAGED_REG(page, reg) \
2138c2ecf20Sopenharmony_ci	((page) * EF4_VI_PAGE_SIZE + (reg))
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci/* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
2168c2ecf20Sopenharmony_cistatic inline void _ef4_writeo_page(struct ef4_nic *efx, ef4_oword_t *value,
2178c2ecf20Sopenharmony_ci				    unsigned int reg, unsigned int page)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	reg = EF4_PAGED_REG(page, reg);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	netif_vdbg(efx, hw, efx->net_dev,
2228c2ecf20Sopenharmony_ci		   "writing register %x with " EF4_OWORD_FMT "\n", reg,
2238c2ecf20Sopenharmony_ci		   EF4_OWORD_VAL(*value));
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci#ifdef EF4_USE_QWORD_IO
2268c2ecf20Sopenharmony_ci	_ef4_writeq(efx, value->u64[0], reg + 0);
2278c2ecf20Sopenharmony_ci	_ef4_writeq(efx, value->u64[1], reg + 8);
2288c2ecf20Sopenharmony_ci#else
2298c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[0], reg + 0);
2308c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[1], reg + 4);
2318c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[2], reg + 8);
2328c2ecf20Sopenharmony_ci	_ef4_writed(efx, value->u32[3], reg + 12);
2338c2ecf20Sopenharmony_ci#endif
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci#define ef4_writeo_page(efx, value, reg, page)				\
2368c2ecf20Sopenharmony_ci	_ef4_writeo_page(efx, value,					\
2378c2ecf20Sopenharmony_ci			 reg +						\
2388c2ecf20Sopenharmony_ci			 BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
2398c2ecf20Sopenharmony_ci			 page)
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci/* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the
2428c2ecf20Sopenharmony_ci * high bits of RX_DESC_UPD or TX_DESC_UPD)
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_cistatic inline void
2458c2ecf20Sopenharmony_ci_ef4_writed_page(struct ef4_nic *efx, const ef4_dword_t *value,
2468c2ecf20Sopenharmony_ci		 unsigned int reg, unsigned int page)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci#define ef4_writed_page(efx, value, reg, page)				\
2518c2ecf20Sopenharmony_ci	_ef4_writed_page(efx, value,					\
2528c2ecf20Sopenharmony_ci			 reg +						\
2538c2ecf20Sopenharmony_ci			 BUILD_BUG_ON_ZERO((reg) != 0x400 &&		\
2548c2ecf20Sopenharmony_ci					   (reg) != 0x420 &&		\
2558c2ecf20Sopenharmony_ci					   (reg) != 0x830 &&		\
2568c2ecf20Sopenharmony_ci					   (reg) != 0x83c &&		\
2578c2ecf20Sopenharmony_ci					   (reg) != 0xa18 &&		\
2588c2ecf20Sopenharmony_ci					   (reg) != 0xa1c),		\
2598c2ecf20Sopenharmony_ci			 page)
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci/* Write TIMER_COMMAND.  This is a page-mapped 32-bit CSR, but a bug
2628c2ecf20Sopenharmony_ci * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
2638c2ecf20Sopenharmony_ci * collector register.
2648c2ecf20Sopenharmony_ci */
2658c2ecf20Sopenharmony_cistatic inline void _ef4_writed_page_locked(struct ef4_nic *efx,
2668c2ecf20Sopenharmony_ci					   const ef4_dword_t *value,
2678c2ecf20Sopenharmony_ci					   unsigned int reg,
2688c2ecf20Sopenharmony_ci					   unsigned int page)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	unsigned long flags __attribute__ ((unused));
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if (page == 0) {
2738c2ecf20Sopenharmony_ci		spin_lock_irqsave(&efx->biu_lock, flags);
2748c2ecf20Sopenharmony_ci		ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
2758c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&efx->biu_lock, flags);
2768c2ecf20Sopenharmony_ci	} else {
2778c2ecf20Sopenharmony_ci		ef4_writed(efx, value, EF4_PAGED_REG(page, reg));
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci#define ef4_writed_page_locked(efx, value, reg, page)			\
2818c2ecf20Sopenharmony_ci	_ef4_writed_page_locked(efx, value,				\
2828c2ecf20Sopenharmony_ci				reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
2838c2ecf20Sopenharmony_ci				page)
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci#endif /* EF4_IO_H */
286