162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 262306a36Sopenharmony_ci/**************************************************************************** 362306a36Sopenharmony_ci * Driver for Solarflare network controllers and boards 462306a36Sopenharmony_ci * Copyright 2005-2006 Fen Systems Ltd. 562306a36Sopenharmony_ci * Copyright 2006-2013 Solarflare Communications Inc. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#ifndef EFX_IO_H 962306a36Sopenharmony_ci#define EFX_IO_H 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/io.h> 1262306a36Sopenharmony_ci#include <linux/spinlock.h> 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci/************************************************************************** 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * NIC register I/O 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci ************************************************************************** 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * Notes on locking strategy for the Falcon architecture: 2162306a36Sopenharmony_ci * 2262306a36Sopenharmony_ci * Many CSRs are very wide and cannot be read or written atomically. 2362306a36Sopenharmony_ci * Writes from the host are buffered by the Bus Interface Unit (BIU) 2462306a36Sopenharmony_ci * up to 128 bits. Whenever the host writes part of such a register, 2562306a36Sopenharmony_ci * the BIU collects the written value and does not write to the 2662306a36Sopenharmony_ci * underlying register until all 4 dwords have been written. A 2762306a36Sopenharmony_ci * similar buffering scheme applies to host access to the NIC's 64-bit 2862306a36Sopenharmony_ci * SRAM. 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * Writes to different CSRs and 64-bit SRAM words must be serialised, 3162306a36Sopenharmony_ci * since interleaved access can result in lost writes. We use 3262306a36Sopenharmony_ci * efx_nic::biu_lock for this. 3362306a36Sopenharmony_ci * 3462306a36Sopenharmony_ci * We also serialise reads from 128-bit CSRs and SRAM with the same 3562306a36Sopenharmony_ci * spinlock. This may not be necessary, but it doesn't really matter 3662306a36Sopenharmony_ci * as there are no such reads on the fast path. 3762306a36Sopenharmony_ci * 3862306a36Sopenharmony_ci * The DMA descriptor pointers (RX_DESC_UPD and TX_DESC_UPD) are 3962306a36Sopenharmony_ci * 128-bit but are special-cased in the BIU to avoid the need for 4062306a36Sopenharmony_ci * locking in the host: 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * - They are write-only. 4362306a36Sopenharmony_ci * - The semantics of writing to these registers are such that 4462306a36Sopenharmony_ci * replacing the low 96 bits with zero does not affect functionality. 4562306a36Sopenharmony_ci * - If the host writes to the last dword address of such a register 4662306a36Sopenharmony_ci * (i.e. the high 32 bits) the underlying register will always be 4762306a36Sopenharmony_ci * written. If the collector and the current write together do not 4862306a36Sopenharmony_ci * provide values for all 128 bits of the register, the low 96 bits 4962306a36Sopenharmony_ci * will be written as zero. 5062306a36Sopenharmony_ci * - If the host writes to the address of any other part of such a 5162306a36Sopenharmony_ci * register while the collector already holds values for some other 5262306a36Sopenharmony_ci * register, the write is discarded and the collector maintains its 5362306a36Sopenharmony_ci * current state. 5462306a36Sopenharmony_ci * 5562306a36Sopenharmony_ci * The EF10 architecture exposes very few registers to the host and 5662306a36Sopenharmony_ci * most of them are only 32 bits wide. The only exceptions are the MC 5762306a36Sopenharmony_ci * doorbell register pair, which has its own latching, and 5862306a36Sopenharmony_ci * TX_DESC_UPD, which works in a similar way to the Falcon 5962306a36Sopenharmony_ci * architecture. 6062306a36Sopenharmony_ci */ 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci#if BITS_PER_LONG == 64 6362306a36Sopenharmony_ci#define EFX_USE_QWORD_IO 1 6462306a36Sopenharmony_ci#endif 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci/* Hardware issue requires that only 64-bit naturally aligned writes 6762306a36Sopenharmony_ci * are seen by hardware. Its not strictly necessary to restrict to 6862306a36Sopenharmony_ci * x86_64 arch, but done for safety since unusual write combining behaviour 6962306a36Sopenharmony_ci * can break PIO. 7062306a36Sopenharmony_ci */ 7162306a36Sopenharmony_ci#ifdef CONFIG_X86_64 7262306a36Sopenharmony_ci/* PIO is a win only if write-combining is possible */ 7362306a36Sopenharmony_ci#ifdef ioremap_wc 7462306a36Sopenharmony_ci#define EFX_USE_PIO 1 7562306a36Sopenharmony_ci#endif 7662306a36Sopenharmony_ci#endif 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_cistatic inline u32 efx_reg(struct efx_nic *efx, unsigned int reg) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci return efx->reg_base + reg; 8162306a36Sopenharmony_ci} 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci#ifdef EFX_USE_QWORD_IO 8462306a36Sopenharmony_cistatic inline void _efx_writeq(struct efx_nic *efx, __le64 value, 8562306a36Sopenharmony_ci unsigned int reg) 8662306a36Sopenharmony_ci{ 8762306a36Sopenharmony_ci __raw_writeq((__force u64)value, efx->membase + reg); 8862306a36Sopenharmony_ci} 8962306a36Sopenharmony_cistatic inline __le64 _efx_readq(struct efx_nic *efx, unsigned int reg) 9062306a36Sopenharmony_ci{ 9162306a36Sopenharmony_ci return (__force __le64)__raw_readq(efx->membase + reg); 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci#endif 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_cistatic inline void _efx_writed(struct efx_nic *efx, __le32 value, 9662306a36Sopenharmony_ci unsigned int reg) 9762306a36Sopenharmony_ci{ 9862306a36Sopenharmony_ci __raw_writel((__force u32)value, efx->membase + reg); 9962306a36Sopenharmony_ci} 10062306a36Sopenharmony_cistatic inline __le32 _efx_readd(struct efx_nic *efx, unsigned int reg) 10162306a36Sopenharmony_ci{ 10262306a36Sopenharmony_ci return (__force __le32)__raw_readl(efx->membase + reg); 10362306a36Sopenharmony_ci} 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci/* Write a normal 128-bit CSR, locking as appropriate. */ 10662306a36Sopenharmony_cistatic inline void efx_writeo(struct efx_nic *efx, const efx_oword_t *value, 10762306a36Sopenharmony_ci unsigned int reg) 10862306a36Sopenharmony_ci{ 10962306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 11262306a36Sopenharmony_ci "writing register %x with " EFX_OWORD_FMT "\n", reg, 11362306a36Sopenharmony_ci EFX_OWORD_VAL(*value)); 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 11662306a36Sopenharmony_ci#ifdef EFX_USE_QWORD_IO 11762306a36Sopenharmony_ci _efx_writeq(efx, value->u64[0], reg + 0); 11862306a36Sopenharmony_ci _efx_writeq(efx, value->u64[1], reg + 8); 11962306a36Sopenharmony_ci#else 12062306a36Sopenharmony_ci _efx_writed(efx, value->u32[0], reg + 0); 12162306a36Sopenharmony_ci _efx_writed(efx, value->u32[1], reg + 4); 12262306a36Sopenharmony_ci _efx_writed(efx, value->u32[2], reg + 8); 12362306a36Sopenharmony_ci _efx_writed(efx, value->u32[3], reg + 12); 12462306a36Sopenharmony_ci#endif 12562306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 12662306a36Sopenharmony_ci} 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci/* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */ 12962306a36Sopenharmony_cistatic inline void efx_sram_writeq(struct efx_nic *efx, void __iomem *membase, 13062306a36Sopenharmony_ci const efx_qword_t *value, unsigned int index) 13162306a36Sopenharmony_ci{ 13262306a36Sopenharmony_ci unsigned int addr = index * sizeof(*value); 13362306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 13662306a36Sopenharmony_ci "writing SRAM address %x with " EFX_QWORD_FMT "\n", 13762306a36Sopenharmony_ci addr, EFX_QWORD_VAL(*value)); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 14062306a36Sopenharmony_ci#ifdef EFX_USE_QWORD_IO 14162306a36Sopenharmony_ci __raw_writeq((__force u64)value->u64[0], membase + addr); 14262306a36Sopenharmony_ci#else 14362306a36Sopenharmony_ci __raw_writel((__force u32)value->u32[0], membase + addr); 14462306a36Sopenharmony_ci __raw_writel((__force u32)value->u32[1], membase + addr + 4); 14562306a36Sopenharmony_ci#endif 14662306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci/* Write a 32-bit CSR or the last dword of a special 128-bit CSR */ 15062306a36Sopenharmony_cistatic inline void efx_writed(struct efx_nic *efx, const efx_dword_t *value, 15162306a36Sopenharmony_ci unsigned int reg) 15262306a36Sopenharmony_ci{ 15362306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 15462306a36Sopenharmony_ci "writing register %x with "EFX_DWORD_FMT"\n", 15562306a36Sopenharmony_ci reg, EFX_DWORD_VAL(*value)); 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci /* No lock required */ 15862306a36Sopenharmony_ci _efx_writed(efx, value->u32[0], reg); 15962306a36Sopenharmony_ci} 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci/* Read a 128-bit CSR, locking as appropriate. */ 16262306a36Sopenharmony_cistatic inline void efx_reado(struct efx_nic *efx, efx_oword_t *value, 16362306a36Sopenharmony_ci unsigned int reg) 16462306a36Sopenharmony_ci{ 16562306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 16862306a36Sopenharmony_ci value->u32[0] = _efx_readd(efx, reg + 0); 16962306a36Sopenharmony_ci value->u32[1] = _efx_readd(efx, reg + 4); 17062306a36Sopenharmony_ci value->u32[2] = _efx_readd(efx, reg + 8); 17162306a36Sopenharmony_ci value->u32[3] = _efx_readd(efx, reg + 12); 17262306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 17562306a36Sopenharmony_ci "read from register %x, got " EFX_OWORD_FMT "\n", reg, 17662306a36Sopenharmony_ci EFX_OWORD_VAL(*value)); 17762306a36Sopenharmony_ci} 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci/* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */ 18062306a36Sopenharmony_cistatic inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase, 18162306a36Sopenharmony_ci efx_qword_t *value, unsigned int index) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci unsigned int addr = index * sizeof(*value); 18462306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 18762306a36Sopenharmony_ci#ifdef EFX_USE_QWORD_IO 18862306a36Sopenharmony_ci value->u64[0] = (__force __le64)__raw_readq(membase + addr); 18962306a36Sopenharmony_ci#else 19062306a36Sopenharmony_ci value->u32[0] = (__force __le32)__raw_readl(membase + addr); 19162306a36Sopenharmony_ci value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4); 19262306a36Sopenharmony_ci#endif 19362306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 19662306a36Sopenharmony_ci "read from SRAM address %x, got "EFX_QWORD_FMT"\n", 19762306a36Sopenharmony_ci addr, EFX_QWORD_VAL(*value)); 19862306a36Sopenharmony_ci} 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci/* Read a 32-bit CSR or SRAM */ 20162306a36Sopenharmony_cistatic inline void efx_readd(struct efx_nic *efx, efx_dword_t *value, 20262306a36Sopenharmony_ci unsigned int reg) 20362306a36Sopenharmony_ci{ 20462306a36Sopenharmony_ci value->u32[0] = _efx_readd(efx, reg); 20562306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 20662306a36Sopenharmony_ci "read from register %x, got "EFX_DWORD_FMT"\n", 20762306a36Sopenharmony_ci reg, EFX_DWORD_VAL(*value)); 20862306a36Sopenharmony_ci} 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci/* Write a 128-bit CSR forming part of a table */ 21162306a36Sopenharmony_cistatic inline void 21262306a36Sopenharmony_ciefx_writeo_table(struct efx_nic *efx, const efx_oword_t *value, 21362306a36Sopenharmony_ci unsigned int reg, unsigned int index) 21462306a36Sopenharmony_ci{ 21562306a36Sopenharmony_ci efx_writeo(efx, value, reg + index * sizeof(efx_oword_t)); 21662306a36Sopenharmony_ci} 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci/* Read a 128-bit CSR forming part of a table */ 21962306a36Sopenharmony_cistatic inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value, 22062306a36Sopenharmony_ci unsigned int reg, unsigned int index) 22162306a36Sopenharmony_ci{ 22262306a36Sopenharmony_ci efx_reado(efx, value, reg + index * sizeof(efx_oword_t)); 22362306a36Sopenharmony_ci} 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci/* default VI stride (step between per-VI registers) is 8K on EF10 and 22662306a36Sopenharmony_ci * 64K on EF100 22762306a36Sopenharmony_ci */ 22862306a36Sopenharmony_ci#define EFX_DEFAULT_VI_STRIDE 0x2000 22962306a36Sopenharmony_ci#define EF100_DEFAULT_VI_STRIDE 0x10000 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci/* Calculate offset to page-mapped register */ 23262306a36Sopenharmony_cistatic inline unsigned int efx_paged_reg(struct efx_nic *efx, unsigned int page, 23362306a36Sopenharmony_ci unsigned int reg) 23462306a36Sopenharmony_ci{ 23562306a36Sopenharmony_ci return page * efx->vi_stride + reg; 23662306a36Sopenharmony_ci} 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci/* Write the whole of RX_DESC_UPD or TX_DESC_UPD */ 23962306a36Sopenharmony_cistatic inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value, 24062306a36Sopenharmony_ci unsigned int reg, unsigned int page) 24162306a36Sopenharmony_ci{ 24262306a36Sopenharmony_ci reg = efx_paged_reg(efx, page, reg); 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 24562306a36Sopenharmony_ci "writing register %x with " EFX_OWORD_FMT "\n", reg, 24662306a36Sopenharmony_ci EFX_OWORD_VAL(*value)); 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci#ifdef EFX_USE_QWORD_IO 24962306a36Sopenharmony_ci _efx_writeq(efx, value->u64[0], reg + 0); 25062306a36Sopenharmony_ci _efx_writeq(efx, value->u64[1], reg + 8); 25162306a36Sopenharmony_ci#else 25262306a36Sopenharmony_ci _efx_writed(efx, value->u32[0], reg + 0); 25362306a36Sopenharmony_ci _efx_writed(efx, value->u32[1], reg + 4); 25462306a36Sopenharmony_ci _efx_writed(efx, value->u32[2], reg + 8); 25562306a36Sopenharmony_ci _efx_writed(efx, value->u32[3], reg + 12); 25662306a36Sopenharmony_ci#endif 25762306a36Sopenharmony_ci} 25862306a36Sopenharmony_ci#define efx_writeo_page(efx, value, reg, page) \ 25962306a36Sopenharmony_ci _efx_writeo_page(efx, value, \ 26062306a36Sopenharmony_ci reg + \ 26162306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \ 26262306a36Sopenharmony_ci page) 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci/* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the 26562306a36Sopenharmony_ci * high bits of RX_DESC_UPD or TX_DESC_UPD) 26662306a36Sopenharmony_ci */ 26762306a36Sopenharmony_cistatic inline void 26862306a36Sopenharmony_ci_efx_writed_page(struct efx_nic *efx, const efx_dword_t *value, 26962306a36Sopenharmony_ci unsigned int reg, unsigned int page) 27062306a36Sopenharmony_ci{ 27162306a36Sopenharmony_ci efx_writed(efx, value, efx_paged_reg(efx, page, reg)); 27262306a36Sopenharmony_ci} 27362306a36Sopenharmony_ci#define efx_writed_page(efx, value, reg, page) \ 27462306a36Sopenharmony_ci _efx_writed_page(efx, value, \ 27562306a36Sopenharmony_ci reg + \ 27662306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((reg) != 0x180 && \ 27762306a36Sopenharmony_ci (reg) != 0x200 && \ 27862306a36Sopenharmony_ci (reg) != 0x400 && \ 27962306a36Sopenharmony_ci (reg) != 0x420 && \ 28062306a36Sopenharmony_ci (reg) != 0x830 && \ 28162306a36Sopenharmony_ci (reg) != 0x83c && \ 28262306a36Sopenharmony_ci (reg) != 0xa18 && \ 28362306a36Sopenharmony_ci (reg) != 0xa1c), \ 28462306a36Sopenharmony_ci page) 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci/* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug 28762306a36Sopenharmony_ci * in the BIU means that writes to TIMER_COMMAND[0] invalidate the 28862306a36Sopenharmony_ci * collector register. 28962306a36Sopenharmony_ci */ 29062306a36Sopenharmony_cistatic inline void _efx_writed_page_locked(struct efx_nic *efx, 29162306a36Sopenharmony_ci const efx_dword_t *value, 29262306a36Sopenharmony_ci unsigned int reg, 29362306a36Sopenharmony_ci unsigned int page) 29462306a36Sopenharmony_ci{ 29562306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci if (page == 0) { 29862306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 29962306a36Sopenharmony_ci efx_writed(efx, value, efx_paged_reg(efx, page, reg)); 30062306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 30162306a36Sopenharmony_ci } else { 30262306a36Sopenharmony_ci efx_writed(efx, value, efx_paged_reg(efx, page, reg)); 30362306a36Sopenharmony_ci } 30462306a36Sopenharmony_ci} 30562306a36Sopenharmony_ci#define efx_writed_page_locked(efx, value, reg, page) \ 30662306a36Sopenharmony_ci _efx_writed_page_locked(efx, value, \ 30762306a36Sopenharmony_ci reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \ 30862306a36Sopenharmony_ci page) 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci#endif /* EFX_IO_H */ 311