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 EF4_IO_H 962306a36Sopenharmony_ci#define EF4_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 * ef4_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 EF4_USE_QWORD_IO 1 6462306a36Sopenharmony_ci#endif 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci#ifdef EF4_USE_QWORD_IO 6762306a36Sopenharmony_cistatic inline void _ef4_writeq(struct ef4_nic *efx, __le64 value, 6862306a36Sopenharmony_ci unsigned int reg) 6962306a36Sopenharmony_ci{ 7062306a36Sopenharmony_ci __raw_writeq((__force u64)value, efx->membase + reg); 7162306a36Sopenharmony_ci} 7262306a36Sopenharmony_cistatic inline __le64 _ef4_readq(struct ef4_nic *efx, unsigned int reg) 7362306a36Sopenharmony_ci{ 7462306a36Sopenharmony_ci return (__force __le64)__raw_readq(efx->membase + reg); 7562306a36Sopenharmony_ci} 7662306a36Sopenharmony_ci#endif 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_cistatic inline void _ef4_writed(struct ef4_nic *efx, __le32 value, 7962306a36Sopenharmony_ci unsigned int reg) 8062306a36Sopenharmony_ci{ 8162306a36Sopenharmony_ci __raw_writel((__force u32)value, efx->membase + reg); 8262306a36Sopenharmony_ci} 8362306a36Sopenharmony_cistatic inline __le32 _ef4_readd(struct ef4_nic *efx, unsigned int reg) 8462306a36Sopenharmony_ci{ 8562306a36Sopenharmony_ci return (__force __le32)__raw_readl(efx->membase + reg); 8662306a36Sopenharmony_ci} 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci/* Write a normal 128-bit CSR, locking as appropriate. */ 8962306a36Sopenharmony_cistatic inline void ef4_writeo(struct ef4_nic *efx, const ef4_oword_t *value, 9062306a36Sopenharmony_ci unsigned int reg) 9162306a36Sopenharmony_ci{ 9262306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 9562306a36Sopenharmony_ci "writing register %x with " EF4_OWORD_FMT "\n", reg, 9662306a36Sopenharmony_ci EF4_OWORD_VAL(*value)); 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 9962306a36Sopenharmony_ci#ifdef EF4_USE_QWORD_IO 10062306a36Sopenharmony_ci _ef4_writeq(efx, value->u64[0], reg + 0); 10162306a36Sopenharmony_ci _ef4_writeq(efx, value->u64[1], reg + 8); 10262306a36Sopenharmony_ci#else 10362306a36Sopenharmony_ci _ef4_writed(efx, value->u32[0], reg + 0); 10462306a36Sopenharmony_ci _ef4_writed(efx, value->u32[1], reg + 4); 10562306a36Sopenharmony_ci _ef4_writed(efx, value->u32[2], reg + 8); 10662306a36Sopenharmony_ci _ef4_writed(efx, value->u32[3], reg + 12); 10762306a36Sopenharmony_ci#endif 10862306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci/* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */ 11262306a36Sopenharmony_cistatic inline void ef4_sram_writeq(struct ef4_nic *efx, void __iomem *membase, 11362306a36Sopenharmony_ci const ef4_qword_t *value, unsigned int index) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci unsigned int addr = index * sizeof(*value); 11662306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 11962306a36Sopenharmony_ci "writing SRAM address %x with " EF4_QWORD_FMT "\n", 12062306a36Sopenharmony_ci addr, EF4_QWORD_VAL(*value)); 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 12362306a36Sopenharmony_ci#ifdef EF4_USE_QWORD_IO 12462306a36Sopenharmony_ci __raw_writeq((__force u64)value->u64[0], membase + addr); 12562306a36Sopenharmony_ci#else 12662306a36Sopenharmony_ci __raw_writel((__force u32)value->u32[0], membase + addr); 12762306a36Sopenharmony_ci __raw_writel((__force u32)value->u32[1], membase + addr + 4); 12862306a36Sopenharmony_ci#endif 12962306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 13062306a36Sopenharmony_ci} 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci/* Write a 32-bit CSR or the last dword of a special 128-bit CSR */ 13362306a36Sopenharmony_cistatic inline void ef4_writed(struct ef4_nic *efx, const ef4_dword_t *value, 13462306a36Sopenharmony_ci unsigned int reg) 13562306a36Sopenharmony_ci{ 13662306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 13762306a36Sopenharmony_ci "writing register %x with "EF4_DWORD_FMT"\n", 13862306a36Sopenharmony_ci reg, EF4_DWORD_VAL(*value)); 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci /* No lock required */ 14162306a36Sopenharmony_ci _ef4_writed(efx, value->u32[0], reg); 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci/* Read a 128-bit CSR, locking as appropriate. */ 14562306a36Sopenharmony_cistatic inline void ef4_reado(struct ef4_nic *efx, ef4_oword_t *value, 14662306a36Sopenharmony_ci unsigned int reg) 14762306a36Sopenharmony_ci{ 14862306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 15162306a36Sopenharmony_ci value->u32[0] = _ef4_readd(efx, reg + 0); 15262306a36Sopenharmony_ci value->u32[1] = _ef4_readd(efx, reg + 4); 15362306a36Sopenharmony_ci value->u32[2] = _ef4_readd(efx, reg + 8); 15462306a36Sopenharmony_ci value->u32[3] = _ef4_readd(efx, reg + 12); 15562306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 15862306a36Sopenharmony_ci "read from register %x, got " EF4_OWORD_FMT "\n", reg, 15962306a36Sopenharmony_ci EF4_OWORD_VAL(*value)); 16062306a36Sopenharmony_ci} 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci/* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */ 16362306a36Sopenharmony_cistatic inline void ef4_sram_readq(struct ef4_nic *efx, void __iomem *membase, 16462306a36Sopenharmony_ci ef4_qword_t *value, unsigned int index) 16562306a36Sopenharmony_ci{ 16662306a36Sopenharmony_ci unsigned int addr = index * sizeof(*value); 16762306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 17062306a36Sopenharmony_ci#ifdef EF4_USE_QWORD_IO 17162306a36Sopenharmony_ci value->u64[0] = (__force __le64)__raw_readq(membase + addr); 17262306a36Sopenharmony_ci#else 17362306a36Sopenharmony_ci value->u32[0] = (__force __le32)__raw_readl(membase + addr); 17462306a36Sopenharmony_ci value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4); 17562306a36Sopenharmony_ci#endif 17662306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 17962306a36Sopenharmony_ci "read from SRAM address %x, got "EF4_QWORD_FMT"\n", 18062306a36Sopenharmony_ci addr, EF4_QWORD_VAL(*value)); 18162306a36Sopenharmony_ci} 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci/* Read a 32-bit CSR or SRAM */ 18462306a36Sopenharmony_cistatic inline void ef4_readd(struct ef4_nic *efx, ef4_dword_t *value, 18562306a36Sopenharmony_ci unsigned int reg) 18662306a36Sopenharmony_ci{ 18762306a36Sopenharmony_ci value->u32[0] = _ef4_readd(efx, reg); 18862306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 18962306a36Sopenharmony_ci "read from register %x, got "EF4_DWORD_FMT"\n", 19062306a36Sopenharmony_ci reg, EF4_DWORD_VAL(*value)); 19162306a36Sopenharmony_ci} 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci/* Write a 128-bit CSR forming part of a table */ 19462306a36Sopenharmony_cistatic inline void 19562306a36Sopenharmony_cief4_writeo_table(struct ef4_nic *efx, const ef4_oword_t *value, 19662306a36Sopenharmony_ci unsigned int reg, unsigned int index) 19762306a36Sopenharmony_ci{ 19862306a36Sopenharmony_ci ef4_writeo(efx, value, reg + index * sizeof(ef4_oword_t)); 19962306a36Sopenharmony_ci} 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci/* Read a 128-bit CSR forming part of a table */ 20262306a36Sopenharmony_cistatic inline void ef4_reado_table(struct ef4_nic *efx, ef4_oword_t *value, 20362306a36Sopenharmony_ci unsigned int reg, unsigned int index) 20462306a36Sopenharmony_ci{ 20562306a36Sopenharmony_ci ef4_reado(efx, value, reg + index * sizeof(ef4_oword_t)); 20662306a36Sopenharmony_ci} 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci/* Page size used as step between per-VI registers */ 20962306a36Sopenharmony_ci#define EF4_VI_PAGE_SIZE 0x2000 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci/* Calculate offset to page-mapped register */ 21262306a36Sopenharmony_ci#define EF4_PAGED_REG(page, reg) \ 21362306a36Sopenharmony_ci ((page) * EF4_VI_PAGE_SIZE + (reg)) 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci/* Write the whole of RX_DESC_UPD or TX_DESC_UPD */ 21662306a36Sopenharmony_cistatic inline void _ef4_writeo_page(struct ef4_nic *efx, ef4_oword_t *value, 21762306a36Sopenharmony_ci unsigned int reg, unsigned int page) 21862306a36Sopenharmony_ci{ 21962306a36Sopenharmony_ci reg = EF4_PAGED_REG(page, reg); 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci netif_vdbg(efx, hw, efx->net_dev, 22262306a36Sopenharmony_ci "writing register %x with " EF4_OWORD_FMT "\n", reg, 22362306a36Sopenharmony_ci EF4_OWORD_VAL(*value)); 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci#ifdef EF4_USE_QWORD_IO 22662306a36Sopenharmony_ci _ef4_writeq(efx, value->u64[0], reg + 0); 22762306a36Sopenharmony_ci _ef4_writeq(efx, value->u64[1], reg + 8); 22862306a36Sopenharmony_ci#else 22962306a36Sopenharmony_ci _ef4_writed(efx, value->u32[0], reg + 0); 23062306a36Sopenharmony_ci _ef4_writed(efx, value->u32[1], reg + 4); 23162306a36Sopenharmony_ci _ef4_writed(efx, value->u32[2], reg + 8); 23262306a36Sopenharmony_ci _ef4_writed(efx, value->u32[3], reg + 12); 23362306a36Sopenharmony_ci#endif 23462306a36Sopenharmony_ci} 23562306a36Sopenharmony_ci#define ef4_writeo_page(efx, value, reg, page) \ 23662306a36Sopenharmony_ci _ef4_writeo_page(efx, value, \ 23762306a36Sopenharmony_ci reg + \ 23862306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \ 23962306a36Sopenharmony_ci page) 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci/* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the 24262306a36Sopenharmony_ci * high bits of RX_DESC_UPD or TX_DESC_UPD) 24362306a36Sopenharmony_ci */ 24462306a36Sopenharmony_cistatic inline void 24562306a36Sopenharmony_ci_ef4_writed_page(struct ef4_nic *efx, const ef4_dword_t *value, 24662306a36Sopenharmony_ci unsigned int reg, unsigned int page) 24762306a36Sopenharmony_ci{ 24862306a36Sopenharmony_ci ef4_writed(efx, value, EF4_PAGED_REG(page, reg)); 24962306a36Sopenharmony_ci} 25062306a36Sopenharmony_ci#define ef4_writed_page(efx, value, reg, page) \ 25162306a36Sopenharmony_ci _ef4_writed_page(efx, value, \ 25262306a36Sopenharmony_ci reg + \ 25362306a36Sopenharmony_ci BUILD_BUG_ON_ZERO((reg) != 0x400 && \ 25462306a36Sopenharmony_ci (reg) != 0x420 && \ 25562306a36Sopenharmony_ci (reg) != 0x830 && \ 25662306a36Sopenharmony_ci (reg) != 0x83c && \ 25762306a36Sopenharmony_ci (reg) != 0xa18 && \ 25862306a36Sopenharmony_ci (reg) != 0xa1c), \ 25962306a36Sopenharmony_ci page) 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci/* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug 26262306a36Sopenharmony_ci * in the BIU means that writes to TIMER_COMMAND[0] invalidate the 26362306a36Sopenharmony_ci * collector register. 26462306a36Sopenharmony_ci */ 26562306a36Sopenharmony_cistatic inline void _ef4_writed_page_locked(struct ef4_nic *efx, 26662306a36Sopenharmony_ci const ef4_dword_t *value, 26762306a36Sopenharmony_ci unsigned int reg, 26862306a36Sopenharmony_ci unsigned int page) 26962306a36Sopenharmony_ci{ 27062306a36Sopenharmony_ci unsigned long flags __attribute__ ((unused)); 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci if (page == 0) { 27362306a36Sopenharmony_ci spin_lock_irqsave(&efx->biu_lock, flags); 27462306a36Sopenharmony_ci ef4_writed(efx, value, EF4_PAGED_REG(page, reg)); 27562306a36Sopenharmony_ci spin_unlock_irqrestore(&efx->biu_lock, flags); 27662306a36Sopenharmony_ci } else { 27762306a36Sopenharmony_ci ef4_writed(efx, value, EF4_PAGED_REG(page, reg)); 27862306a36Sopenharmony_ci } 27962306a36Sopenharmony_ci} 28062306a36Sopenharmony_ci#define ef4_writed_page_locked(efx, value, reg, page) \ 28162306a36Sopenharmony_ci _ef4_writed_page_locked(efx, value, \ 28262306a36Sopenharmony_ci reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \ 28362306a36Sopenharmony_ci page) 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci#endif /* EF4_IO_H */ 286