📄 sba_iommu.c
字号:
/*** System Bus Adapter (SBA) I/O MMU manager**** (c) Copyright 2000 Grant Grundler** (c) Copyright 2000 Hewlett-Packard Company**** Portions (c) 1999 Dave S. Miller (from sparc64 I/O MMU code)**** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.****** This module initializes the IOC (I/O Controller) found on B1000/C3000/** J5000/J7000/N-class/L-class machines and their successors.**** FIXME: add DMA hint support programming in both sba and lba modules.*/#include <linux/config.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/spinlock.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/mm.h>#include <linux/string.h>#undef PCI_DEBUG /* for ASSERT */#include <linux/pci.h>#undef PCI_DEBUG#include <asm/byteorder.h>#include <asm/io.h>#include <asm/dma.h> /* for DMA_CHUNK_SIZE */#include <asm/hardware.h> /* for register_parisc_driver() stuff */#include <asm/gsc.h> /* FIXME: for gsc_read/gsc_write */#include <linux/proc_fs.h>#include <asm/runway.h> /* for proc_runway_root */#include <asm/pdc.h> /* for PDC_MODEL_* */#define MODULE_NAME "SBA"/*** The number of debug flags is a clue - this code is fragile.** Don't even think about messing with it unless you have** plenty of 710's to sacrifice to the computer gods. :^)*/#undef DEBUG_SBA_INIT#undef DEBUG_SBA_RUN#undef DEBUG_SBA_RUN_SG#undef DEBUG_SBA_RESOURCE#undef ASSERT_PDIR_SANITY#undef DEBUG_LARGE_SG_ENTRIES#undef DEBUG_DMB_TRAP#define SBA_INLINE __inline__#ifdef DEBUG_SBA_INIT#define DBG_INIT(x...) printk(x)#else#define DBG_INIT(x...)#endif#ifdef DEBUG_SBA_RUN#define DBG_RUN(x...) printk(x)#else#define DBG_RUN(x...)#endif#ifdef DEBUG_SBA_RUN_SG#define DBG_RUN_SG(x...) printk(x)#else#define DBG_RUN_SG(x...)#endif#ifdef DEBUG_SBA_RESOURCE#define DBG_RES(x...) printk(x)#else#define DBG_RES(x...)#endif/*** The number of pdir entries to "free" before issueing** a read to PCOM register to flush out PCOM writes.** Interacts with allocation granularity (ie 4 or 8 entries** allocated and free'd/purged at a time might make this** less interesting).*/#define DELAYED_RESOURCE_CNT 16#define DEFAULT_DMA_HINT_REG 0#define ASTRO_RUNWAY_PORT 0x582#define ASTRO_ROPES_PORT 0x780#define IKE_MERCED_PORT 0x803#define IKE_ROPES_PORT 0x781#define REO_MERCED_PORT 0x804#define REO_ROPES_PORT 0x782#define REOG_MERCED_PORT 0x805#define REOG_ROPES_PORT 0x783#define SBA_FUNC_ID 0x0000 /* function id */#define SBA_FCLASS 0x0008 /* function class, bist, header, rev... */#define IS_ASTRO(id) \(((id)->hversion == ASTRO_RUNWAY_PORT) || ((id)->hversion == ASTRO_ROPES_PORT))#define IS_IKE(id) \(((id)->hversion == IKE_MERCED_PORT) || ((id)->hversion == IKE_ROPES_PORT))#define SBA_FUNC_SIZE 4096 /* SBA configuration function reg set */#define ASTRO_IOC_OFFSET 0x20000/* Ike's IOC's occupy functions 2 and 3 (not 0 and 1) */#define IKE_IOC_OFFSET(p) ((p+2)*SBA_FUNC_SIZE)#define IOC_CTRL 0x8 /* IOC_CTRL offset */#define IOC_CTRL_TC (1 << 0) /* TOC Enable */#define IOC_CTRL_CE (1 << 1) /* Coalesce Enable */#define IOC_CTRL_DE (1 << 2) /* Dillon Enable */#define IOC_CTRL_RM (1 << 8) /* Real Mode */#define IOC_CTRL_NC (1 << 9) /* Non Coherent Mode */#define MAX_IOC 2 /* per Ike. Astro only has 1 *//*** Offsets into MBIB (Function 0 on Ike and hopefully Astro)** Firmware programs this stuff. Don't touch it.*/#define IOS_DIST_BASE 0x390#define IOS_DIST_MASK 0x398#define IOS_DIST_ROUTE 0x3A0#define IOS_DIRECT_BASE 0x3C0#define IOS_DIRECT_MASK 0x3C8#define IOS_DIRECT_ROUTE 0x3D0/*** Offsets into I/O TLB (Function 2 and 3 on Ike)*/#define ROPE0_CTL 0x200 /* "regbus pci0" */#define ROPE1_CTL 0x208#define ROPE2_CTL 0x210#define ROPE3_CTL 0x218#define ROPE4_CTL 0x220#define ROPE5_CTL 0x228#define ROPE6_CTL 0x230#define ROPE7_CTL 0x238#define HF_ENABLE 0x40#define IOC_IBASE 0x300 /* IO TLB */#define IOC_IMASK 0x308#define IOC_PCOM 0x310#define IOC_TCNFG 0x318#define IOC_PDIR_BASE 0x320#define IOC_IOVA_SPACE_BASE 0 /* IOVA ranges start at 0 *//*** IOC supports 4/8/16/64KB page sizes (see TCNFG register)** It's safer (avoid memory corruption) to keep DMA page mappings** equivalently sized to VM PAGE_SIZE.**** We really can't avoid generating a new mapping for each** page since the Virtual Coherence Index has to be generated** and updated for each page.**** IOVP_SIZE could only be greater than PAGE_SIZE if we are** confident the drivers really only touch the next physical** page iff that driver instance owns it.*/#define IOVP_SIZE PAGE_SIZE#define IOVP_SHIFT PAGE_SHIFT#define IOVP_MASK PAGE_MASK#define SBA_PERF_CFG 0x708 /* Performance Counter stuff */#define SBA_PERF_MASK1 0x718#define SBA_PERF_MASK2 0x730/*** Offsets into PCI Performance Counters (functions 12 and 13)** Controlled by PERF registers in function 2 & 3 respectively.*/#define SBA_PERF_CNT1 0x200#define SBA_PERF_CNT2 0x208#define SBA_PERF_CNT3 0x210struct ioc { unsigned long ioc_hpa; /* I/O MMU base address */ char *res_map; /* resource map, bit == pdir entry */ u64 *pdir_base; /* physical base address */ unsigned long *res_hint; /* next avail IOVP - circular search */ spinlock_t res_lock; unsigned long hint_mask_pdir; /* bits used for DMA hints */ unsigned int res_bitshift; /* from the LEFT! */ unsigned int res_size; /* size of resource map in bytes */ unsigned int hint_shift_pdir;#if DELAYED_RESOURCE_CNT > 0 int saved_cnt; struct sba_dma_pair { dma_addr_t iova; size_t size; } saved[DELAYED_RESOURCE_CNT];#endif#ifdef CONFIG_PROC_FS#define SBA_SEARCH_SAMPLE 0x100 unsigned long avg_search[SBA_SEARCH_SAMPLE]; unsigned long avg_idx; /* current index into avg_search */ unsigned long used_pages; unsigned long msingle_calls; unsigned long msingle_pages; unsigned long msg_calls; unsigned long msg_pages; unsigned long usingle_calls; unsigned long usingle_pages; unsigned long usg_calls; unsigned long usg_pages;#endif /* STUFF We don't need in performance path */ unsigned int pdir_size; /* in bytes, determined by IOV Space size */ unsigned long ibase; /* pdir IOV Space base - shared w/lba_pci */ unsigned long imask; /* pdir IOV Space mask - shared w/lba_pci */};struct sba_device { struct sba_device *next; /* list of SBA's in system */ struct parisc_device *dev; /* dev found in bus walk */ struct parisc_device_id *iodc; /* data about dev from firmware */ const char *name; unsigned long sba_hpa; /* base address */ spinlock_t sba_lock; unsigned int flags; /* state/functionality enabled */ unsigned int hw_rev; /* HW revision of chip */ unsigned int num_ioc; /* number of on-board IOC's */ struct ioc ioc[MAX_IOC];};static struct sba_device *sba_list;static unsigned long ioc_needs_fdc = 0;/* Ratio of Host MEM to IOV Space size */static unsigned long sba_mem_ratio = 8;/* global count of IOMMUs in the system */static unsigned int global_ioc_cnt = 0;/* PA8700 (Piranha 2.2) bug workaround */static unsigned long piranha_bad_128k = 0;/* Looks nice and keeps the compiler happy */#define SBA_DEV(d) ((struct sba_device *) (d))#define ROUNDUP(x,y) ((x + ((y)-1)) & ~((y)-1))/************************************** SBA register read and write support**** BE WARNED: register writes are posted.** (ie follow writes which must reach HW with a read)**** Superdome (in particular, REO) allows only 64-bit CSR accesses.*/#define READ_REG32(addr) le32_to_cpu(__raw_readl(addr))#define READ_REG64(addr) le64_to_cpu(__raw_readq(addr))#define WRITE_REG32(val, addr) __raw_writel(cpu_to_le32(val), addr)#define WRITE_REG64(val, addr) __raw_writeq(cpu_to_le64(val), addr)#ifdef __LP64__#define READ_REG(addr) READ_REG64(addr)#define WRITE_REG(value, addr) WRITE_REG64(value, addr)#else#define READ_REG(addr) READ_REG32(addr)#define WRITE_REG(value, addr) WRITE_REG32(value, addr)#endif#ifdef DEBUG_SBA_INIT/* NOTE: When __LP64__ isn't defined, READ_REG64() is two 32-bit reads *//** * sba_dump_ranges - debugging only - print ranges assigned to this IOA * @hpa: base address of the sba * * Print the MMIO and IO Port address ranges forwarded by an Astro/Ike/RIO * IO Adapter (aka Bus Converter). */static voidsba_dump_ranges(unsigned long hpa){ DBG_INIT("SBA at 0x%lx\n", hpa); DBG_INIT("IOS_DIST_BASE : %Lx\n", READ_REG64(hpa+IOS_DIST_BASE)); DBG_INIT("IOS_DIST_MASK : %Lx\n", READ_REG64(hpa+IOS_DIST_MASK)); DBG_INIT("IOS_DIST_ROUTE : %Lx\n", READ_REG64(hpa+IOS_DIST_ROUTE)); DBG_INIT("\n"); DBG_INIT("IOS_DIRECT_BASE : %Lx\n", READ_REG64(hpa+IOS_DIRECT_BASE)); DBG_INIT("IOS_DIRECT_MASK : %Lx\n", READ_REG64(hpa+IOS_DIRECT_MASK)); DBG_INIT("IOS_DIRECT_ROUTE: %Lx\n", READ_REG64(hpa+IOS_DIRECT_ROUTE));}/** * sba_dump_tlb - debugging only - print IOMMU operating parameters * @hpa: base address of the IOMMU * * Print the size/location of the IO MMU PDIR. */static voidsba_dump_tlb(unsigned long hpa){ DBG_INIT("IO TLB at 0x%lx\n", hpa); DBG_INIT("IOC_IBASE : %Lx\n", READ_REG64(hpa+IOC_IBASE)); DBG_INIT("IOC_IMASK : %Lx\n", READ_REG64(hpa+IOC_IMASK)); DBG_INIT("IOC_TCNFG : %Lx\n", READ_REG64(hpa+IOC_TCNFG)); DBG_INIT("IOC_PDIR_BASE: %Lx\n", READ_REG64(hpa+IOC_PDIR_BASE)); DBG_INIT("\n");}#endif#ifdef ASSERT_PDIR_SANITY/** * sba_dump_pdir_entry - debugging only - print one IOMMU PDIR entry * @ioc: IO MMU structure which owns the pdir we are interested in. * @msg: text to print ont the output line. * @pide: pdir index. * * Print one entry of the IO MMU PDIR in human readable form. */static voidsba_dump_pdir_entry(struct ioc *ioc, char *msg, uint pide){ /* start printing from lowest pde in rval */ u64 *ptr = &(ioc->pdir_base[pide & (~0U * BITS_PER_LONG)]); unsigned long *rptr = (unsigned long *) &(ioc->res_map[(pide >>3) & ~(sizeof(unsigned long) - 1)]); uint rcnt; printk(KERN_DEBUG "SBA: %s rp %p bit %d rval 0x%lx\n", msg, rptr, pide & (BITS_PER_LONG - 1), *rptr); rcnt = 0; while (rcnt < BITS_PER_LONG) { printk(KERN_DEBUG "%s %2d %p %016Lx\n", (rcnt == (pide & (BITS_PER_LONG - 1))) ? " -->" : " ", rcnt, ptr, *ptr ); rcnt++; ptr++; } printk(KERN_DEBUG "%s", msg);}/** * sba_check_pdir - debugging only - consistency checker * @ioc: IO MMU structure which owns the pdir we are interested in. * @msg: text to print ont the output line. * * Verify the resource map and pdir state is consistent */static intsba_check_pdir(struct ioc *ioc, char *msg){ u32 *rptr_end = (u32 *) &(ioc->res_map[ioc->res_size]); u32 *rptr = (u32 *) ioc->res_map; /* resource map ptr */ u64 *pptr = ioc->pdir_base; /* pdir ptr */ uint pide = 0; while (rptr < rptr_end) { u32 rval = *rptr; int rcnt = 32; /* number of bits we might check */ while (rcnt) { /* Get last byte and highest bit from that */ u32 pde = ((u32) (((char *)pptr)[7])) << 24; if ((rval ^ pde) & 0x80000000) { /* ** BUMMER! -- res_map != pdir -- ** Dump rval and matching pdir entries */ sba_dump_pdir_entry(ioc, msg, pide); return(1); } rcnt--; rval <<= 1; /* try the next bit */ pptr++; pide++; } rptr++; /* look at next word of res_map */ } /* It'd be nice if we always got here :^) */ return 0;}/** * sba_dump_sg - debugging only - print Scatter-Gather list * @ioc: IO MMU structure which owns the pdir we are interested in. * @startsg: head of the SG list * @nents: number of entries in SG list * * print the SG list so we can verify it's correct by hand. */static voidsba_dump_sg( struct ioc *ioc, struct scatterlist *startsg, int nents){ while (nents-- > 0) { printk(KERN_DEBUG " %d : %08lx/%05x %p/%05x\n", nents, (unsigned long) sg_dma_address(startsg), sg_dma_len(startsg), sg_virt_addr(startsg), startsg->length); startsg++; }}#endif /* ASSERT_PDIR_SANITY *//**************************************************************** I/O Pdir Resource Management** Bits set in the resource map are in use.* Each bit can represent a number of pages.* LSbs represent lower addresses (IOVA's).****************************************************************/#define PAGES_PER_RANGE 1 /* could increase this to 4 or 8 if needed *//* Convert from IOVP to IOVA and vice versa. */#define SBA_IOVA(ioc,iovp,offset,hint_reg) ((iovp) | (offset) | ((hint_reg)<<(ioc->hint_shift_pdir)))#define SBA_IOVP(ioc,iova) ((iova) & ioc->hint_mask_pdir)/* FIXME : review these macros to verify correctness and usage */#define PDIR_INDEX(iovp) ((iovp)>>IOVP_SHIFT)#define MKIOVP(dma_hint,pide) (dma_addr_t)((long)(dma_hint) | ((long)(pide) << IOVP_SHIFT))#define MKIOVA(iovp,offset) (dma_addr_t)((long)iovp | (long)offset)#define RESMAP_MASK(n) (~0UL << (BITS_PER_LONG - (n)))#define RESMAP_IDX_MASK (sizeof(unsigned long) - 1)/** * sba_search_bitmap - find free space in IO PDIR resource bitmap * @ioc: IO MMU structure which owns the pdir we are interested in. * @bits_wanted: number of entries we need. * * Find consecutive free bits in resource bitmap. * Each bit represents one entry in the IO Pdir. * Cool perf optimization: search for log2(size) bits at a time. */static SBA_INLINE unsigned longsba_search_bitmap(struct ioc *ioc, unsigned long bits_wanted){ unsigned long *res_ptr = ioc->res_hint; unsigned long *res_end = (unsigned long *) &(ioc->res_map[ioc->res_size]); unsigned long pide = ~0UL; ASSERT(((unsigned long) ioc->res_hint & (sizeof(unsigned long) - 1UL)) == 0); ASSERT(res_ptr < res_end); if (bits_wanted > (BITS_PER_LONG/2)) { /* Search word at a time - no mask needed */ for(; res_ptr < res_end; ++res_ptr) { if (*res_ptr == 0) { *res_ptr = RESMAP_MASK(bits_wanted); pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map); pide <<= 3; /* convert to bit address */ break; } } /* point to the next word on next pass */ res_ptr++; ioc->res_bitshift = 0; } else { /* ** Search the resource bit map on well-aligned values. ** "o" is the alignment. ** We need the alignment to invalidate I/O TLB using ** SBA HW features in the unmap path. */ unsigned long o = 1 << get_order(bits_wanted << PAGE_SHIFT); uint bitshiftcnt = ROUNDUP(ioc->res_bitshift, o); unsigned long mask; if (bitshiftcnt >= BITS_PER_LONG) { bitshiftcnt = 0; res_ptr++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -