📄 tioce_provider.c
字号:
/* * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 2003-2006 Silicon Graphics, Inc. All Rights Reserved. */#include <linux/types.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <asm/sn/sn_sal.h>#include <asm/sn/addrs.h>#include <asm/sn/io.h>#include <asm/sn/pcidev.h>#include <asm/sn/pcibus_provider_defs.h>#include <asm/sn/tioce_provider.h>/* * 1/26/2006 * * WAR for SGI PV 944642. For revA TIOCE, need to use the following recipe * (taken from the above PV) before and after accessing tioce internal MMR's * to avoid tioce lockups. * * The recipe as taken from the PV: * * if(mmr address < 0x45000) { * if(mmr address == 0 or 0x80) * mmr wrt or read address 0xc0 * else if(mmr address == 0x148 or 0x200) * mmr wrt or read address 0x28 * else * mmr wrt or read address 0x158 * * do desired mmr access (rd or wrt) * * if(mmr address == 0x100) * mmr wrt or read address 0x38 * mmr wrt or read address 0xb050 * } else * do desired mmr access * * According to hw, we can use reads instead of writes to the above address * * Note this WAR can only to be used for accessing internal MMR's in the * TIOCE Coretalk Address Range 0x0 - 0x07ff_ffff. This includes the * "Local CE Registers and Memories" and "PCI Compatible Config Space" address * spaces from table 2-1 of the "CE Programmer's Reference Overview" document. * * All registers defined in struct tioce will meet that criteria. */static void inlinetioce_mmr_war_pre(struct tioce_kernel *kern, void __iomem *mmr_addr){ u64 mmr_base; u64 mmr_offset; if (kern->ce_common->ce_rev != TIOCE_REV_A) return; mmr_base = kern->ce_common->ce_pcibus.bs_base; mmr_offset = (unsigned long)mmr_addr - mmr_base; if (mmr_offset < 0x45000) { u64 mmr_war_offset; if (mmr_offset == 0 || mmr_offset == 0x80) mmr_war_offset = 0xc0; else if (mmr_offset == 0x148 || mmr_offset == 0x200) mmr_war_offset = 0x28; else mmr_war_offset = 0x158; readq_relaxed((void __iomem *)(mmr_base + mmr_war_offset)); }}static void inlinetioce_mmr_war_post(struct tioce_kernel *kern, void __iomem *mmr_addr){ u64 mmr_base; u64 mmr_offset; if (kern->ce_common->ce_rev != TIOCE_REV_A) return; mmr_base = kern->ce_common->ce_pcibus.bs_base; mmr_offset = (unsigned long)mmr_addr - mmr_base; if (mmr_offset < 0x45000) { if (mmr_offset == 0x100) readq_relaxed((void __iomem *)(mmr_base + 0x38)); readq_relaxed((void __iomem *)(mmr_base + 0xb050)); }}/* load mmr contents into a variable */#define tioce_mmr_load(kern, mmrp, varp) do {\ tioce_mmr_war_pre(kern, mmrp); \ *(varp) = readq_relaxed(mmrp); \ tioce_mmr_war_post(kern, mmrp); \} while (0)/* store variable contents into mmr */#define tioce_mmr_store(kern, mmrp, varp) do {\ tioce_mmr_war_pre(kern, mmrp); \ writeq(*varp, mmrp); \ tioce_mmr_war_post(kern, mmrp); \} while (0)/* store immediate value into mmr */#define tioce_mmr_storei(kern, mmrp, val) do {\ tioce_mmr_war_pre(kern, mmrp); \ writeq(val, mmrp); \ tioce_mmr_war_post(kern, mmrp); \} while (0)/* set bits (immediate value) into mmr */#define tioce_mmr_seti(kern, mmrp, bits) do {\ u64 tmp; \ tioce_mmr_load(kern, mmrp, &tmp); \ tmp |= (bits); \ tioce_mmr_store(kern, mmrp, &tmp); \} while (0)/* clear bits (immediate value) into mmr */#define tioce_mmr_clri(kern, mmrp, bits) do { \ u64 tmp; \ tioce_mmr_load(kern, mmrp, &tmp); \ tmp &= ~(bits); \ tioce_mmr_store(kern, mmrp, &tmp); \} while (0)/** * Bus address ranges for the 5 flavors of TIOCE DMA */#define TIOCE_D64_MIN 0x8000000000000000UL#define TIOCE_D64_MAX 0xffffffffffffffffUL#define TIOCE_D64_ADDR(a) ((a) >= TIOCE_D64_MIN)#define TIOCE_D32_MIN 0x0000000080000000UL#define TIOCE_D32_MAX 0x00000000ffffffffUL#define TIOCE_D32_ADDR(a) ((a) >= TIOCE_D32_MIN && (a) <= TIOCE_D32_MAX)#define TIOCE_M32_MIN 0x0000000000000000UL#define TIOCE_M32_MAX 0x000000007fffffffUL#define TIOCE_M32_ADDR(a) ((a) >= TIOCE_M32_MIN && (a) <= TIOCE_M32_MAX)#define TIOCE_M40_MIN 0x0000004000000000UL#define TIOCE_M40_MAX 0x0000007fffffffffUL#define TIOCE_M40_ADDR(a) ((a) >= TIOCE_M40_MIN && (a) <= TIOCE_M40_MAX)#define TIOCE_M40S_MIN 0x0000008000000000UL#define TIOCE_M40S_MAX 0x000000ffffffffffUL#define TIOCE_M40S_ADDR(a) ((a) >= TIOCE_M40S_MIN && (a) <= TIOCE_M40S_MAX)/* * ATE manipulation macros. */#define ATE_PAGESHIFT(ps) (__ffs(ps))#define ATE_PAGEMASK(ps) ((ps)-1)#define ATE_PAGE(x, ps) ((x) >> ATE_PAGESHIFT(ps))#define ATE_NPAGES(start, len, pagesize) \ (ATE_PAGE((start)+(len)-1, pagesize) - ATE_PAGE(start, pagesize) + 1)#define ATE_VALID(ate) ((ate) & (1UL << 63))#define ATE_MAKE(addr, ps, msi) \ (((addr) & ~ATE_PAGEMASK(ps)) | (1UL << 63) | ((msi)?(1UL << 62):0))/* * Flavors of ate-based mapping supported by tioce_alloc_map() */#define TIOCE_ATE_M32 1#define TIOCE_ATE_M40 2#define TIOCE_ATE_M40S 3#define KB(x) ((u64)(x) << 10)#define MB(x) ((u64)(x) << 20)#define GB(x) ((u64)(x) << 30)/** * tioce_dma_d64 - create a DMA mapping using 64-bit direct mode * @ct_addr: system coretalk address * * Map @ct_addr into 64-bit CE bus space. No device context is necessary * and no CE mapping are consumed. * * Bits 53:0 come from the coretalk address. The remaining bits are set as * follows: * * 63 - must be 1 to indicate d64 mode to CE hardware * 62 - barrier bit ... controlled with tioce_dma_barrier() * 61 - msi bit ... specified through dma_flags * 60:54 - reserved, MBZ */static u64tioce_dma_d64(unsigned long ct_addr, int dma_flags){ u64 bus_addr; bus_addr = ct_addr | (1UL << 63); if (dma_flags & SN_DMA_MSI) bus_addr |= (1UL << 61); return bus_addr;}/** * pcidev_to_tioce - return misc ce related pointers given a pci_dev * @pci_dev: pci device context * @base: ptr to store struct tioce_mmr * for the CE holding this device * @kernel: ptr to store struct tioce_kernel * for the CE holding this device * @port: ptr to store the CE port number that this device is on * * Return pointers to various CE-related structures for the CE upstream of * @pci_dev. */static inline voidpcidev_to_tioce(struct pci_dev *pdev, struct tioce __iomem **base, struct tioce_kernel **kernel, int *port){ struct pcidev_info *pcidev_info; struct tioce_common *ce_common; struct tioce_kernel *ce_kernel; pcidev_info = SN_PCIDEV_INFO(pdev); ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info; ce_kernel = (struct tioce_kernel *)ce_common->ce_kernel_private; if (base) *base = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base; if (kernel) *kernel = ce_kernel; /* * we use port as a zero-based value internally, even though the * documentation is 1-based. */ if (port) *port = (pdev->bus->number < ce_kernel->ce_port1_secondary) ? 0 : 1;}/** * tioce_alloc_map - Given a coretalk address, map it to pcie bus address * space using one of the various ATE-based address modes. * @ce_kern: tioce context * @type: map mode to use * @port: 0-based port that the requesting device is downstream of * @ct_addr: the coretalk address to map * @len: number of bytes to map * * Given the addressing type, set up various parameters that define the * ATE pool to use. Search for a contiguous block of entries to cover the * length, and if enough resources exist, fill in the ATEs and construct a * tioce_dmamap struct to track the mapping. */static u64tioce_alloc_map(struct tioce_kernel *ce_kern, int type, int port, u64 ct_addr, int len, int dma_flags){ int i; int j; int first; int last; int entries; int nates; u64 pagesize; int msi_capable, msi_wanted; u64 *ate_shadow; u64 __iomem *ate_reg; u64 addr; struct tioce __iomem *ce_mmr; u64 bus_base; struct tioce_dmamap *map; ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base; switch (type) { case TIOCE_ATE_M32: /* * The first 64 entries of the ate3240 pool are dedicated to * super-page (TIOCE_ATE_M40S) mode. */ first = 64; entries = TIOCE_NUM_M3240_ATES - 64; ate_shadow = ce_kern->ce_ate3240_shadow; ate_reg = ce_mmr->ce_ure_ate3240; pagesize = ce_kern->ce_ate3240_pagesize; bus_base = TIOCE_M32_MIN; msi_capable = 1; break; case TIOCE_ATE_M40: first = 0; entries = TIOCE_NUM_M40_ATES; ate_shadow = ce_kern->ce_ate40_shadow; ate_reg = ce_mmr->ce_ure_ate40; pagesize = MB(64); bus_base = TIOCE_M40_MIN; msi_capable = 0; break; case TIOCE_ATE_M40S: /* * ate3240 entries 0-31 are dedicated to port1 super-page * mappings. ate3240 entries 32-63 are dedicated to port2. */ first = port * 32; entries = 32; ate_shadow = ce_kern->ce_ate3240_shadow; ate_reg = ce_mmr->ce_ure_ate3240; pagesize = GB(16); bus_base = TIOCE_M40S_MIN; msi_capable = 0; break; default: return 0; } msi_wanted = dma_flags & SN_DMA_MSI; if (msi_wanted && !msi_capable) return 0; nates = ATE_NPAGES(ct_addr, len, pagesize); if (nates > entries) return 0; last = first + entries - nates; for (i = first; i <= last; i++) { if (ATE_VALID(ate_shadow[i])) continue; for (j = i; j < i + nates; j++) if (ATE_VALID(ate_shadow[j])) break; if (j >= i + nates) break; } if (i > last) return 0; map = kzalloc(sizeof(struct tioce_dmamap), GFP_ATOMIC); if (!map) return 0; addr = ct_addr; for (j = 0; j < nates; j++) { u64 ate; ate = ATE_MAKE(addr, pagesize, msi_wanted); ate_shadow[i + j] = ate; tioce_mmr_storei(ce_kern, &ate_reg[i + j], ate); addr += pagesize; } map->refcnt = 1; map->nbytes = nates * pagesize; map->ct_start = ct_addr & ~ATE_PAGEMASK(pagesize); map->pci_start = bus_base + (i * pagesize); map->ate_hw = &ate_reg[i]; map->ate_shadow = &ate_shadow[i]; map->ate_count = nates; list_add(&map->ce_dmamap_list, &ce_kern->ce_dmamap_list); return (map->pci_start + (ct_addr - map->ct_start));}/** * tioce_dma_d32 - create a DMA mapping using 32-bit direct mode * @pdev: linux pci_dev representing the function * @paddr: system physical address * * Map @paddr into 32-bit bus space of the CE associated with @pcidev_info. */static u64tioce_dma_d32(struct pci_dev *pdev, u64 ct_addr, int dma_flags){ int dma_ok; int port; struct tioce __iomem *ce_mmr; struct tioce_kernel *ce_kern; u64 ct_upper; u64 ct_lower; dma_addr_t bus_addr; if (dma_flags & SN_DMA_MSI) return 0; ct_upper = ct_addr & ~0x3fffffffUL; ct_lower = ct_addr & 0x3fffffffUL; pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port); if (ce_kern->ce_port[port].dirmap_refcnt == 0) { u64 tmp; ce_kern->ce_port[port].dirmap_shadow = ct_upper; tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port], ct_upper); tmp = ce_mmr->ce_ure_dir_map[port]; dma_ok = 1; } else dma_ok = (ce_kern->ce_port[port].dirmap_shadow == ct_upper); if (dma_ok) { ce_kern->ce_port[port].dirmap_refcnt++; bus_addr = TIOCE_D32_MIN + ct_lower; } else bus_addr = 0; return bus_addr;}/** * tioce_dma_barrier - swizzle a TIOCE bus address to include or exclude * the barrier bit. * @bus_addr: bus address to swizzle * * Given a TIOCE bus address, set the appropriate bit to indicate barrier * attributes. */static u64tioce_dma_barrier(u64 bus_addr, int on){ u64 barrier_bit; /* barrier not supported in M40/M40S mode */ if (TIOCE_M40_ADDR(bus_addr) || TIOCE_M40S_ADDR(bus_addr)) return bus_addr; if (TIOCE_D64_ADDR(bus_addr)) barrier_bit = (1UL << 62); else /* must be m32 or d32 */ barrier_bit = (1UL << 30); return (on) ? (bus_addr | barrier_bit) : (bus_addr & ~barrier_bit);}/** * tioce_dma_unmap - release CE mapping resources * @pdev: linux pci_dev representing the function * @bus_addr: bus address returned by an earlier tioce_dma_map * @dir: mapping direction (unused) * * Locate mapping resources associated with @bus_addr and release them. * For mappings created using the direct modes there are no resources * to release. */voidtioce_dma_unmap(struct pci_dev *pdev, dma_addr_t bus_addr, int dir){ int i; int port; struct tioce_kernel *ce_kern; struct tioce __iomem *ce_mmr; unsigned long flags; bus_addr = tioce_dma_barrier(bus_addr, 0); pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port); /* nothing to do for D64 */ if (TIOCE_D64_ADDR(bus_addr)) return; spin_lock_irqsave(&ce_kern->ce_lock, flags); if (TIOCE_D32_ADDR(bus_addr)) { if (--ce_kern->ce_port[port].dirmap_refcnt == 0) { ce_kern->ce_port[port].dirmap_shadow = 0; tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port], 0); } } else { struct tioce_dmamap *map; list_for_each_entry(map, &ce_kern->ce_dmamap_list, ce_dmamap_list) { u64 last; last = map->pci_start + map->nbytes - 1; if (bus_addr >= map->pci_start && bus_addr <= last) break; } if (&map->ce_dmamap_list == &ce_kern->ce_dmamap_list) { printk(KERN_WARNING "%s: %s - no map found for bus_addr 0x%lx\n", __FUNCTION__, pci_name(pdev), bus_addr); } else if (--map->refcnt == 0) { for (i = 0; i < map->ate_count; i++) { map->ate_shadow[i] = 0; tioce_mmr_storei(ce_kern, &map->ate_hw[i], 0); } list_del(&map->ce_dmamap_list); kfree(map); } } spin_unlock_irqrestore(&ce_kern->ce_lock, flags);}/** * tioce_do_dma_map - map pages for PCI DMA * @pdev: linux pci_dev representing the function * @paddr: host physical address to map * @byte_count: bytes to map * * This is the main wrapper for mapping host physical pages to CE PCI space. * The mapping mode used is based on the device's dma_mask. */static u64tioce_do_dma_map(struct pci_dev *pdev, u64 paddr, size_t byte_count, int barrier, int dma_flags){ unsigned long flags; u64 ct_addr; u64 mapaddr = 0; struct tioce_kernel *ce_kern; struct tioce_dmamap *map; int port;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -