📄 sba_iommu.c
字号:
/*** System Bus Adapter (SBA) I/O MMU manager**** (c) Copyright 2000-2004 Grant Grundler <grundler @ parisc-linux x org>** (c) Copyright 2004 Naresh Kumar Inna <knaresh at india x hp x com>** (c) Copyright 2000-2004 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/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>#include <linux/pci.h>#include <linux/scatterlist.h>#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 <linux/proc_fs.h>#include <linux/seq_file.h>#include <asm/ropes.h>#include <asm/mckinley.h> /* for proc_mckinley_root */#include <asm/runway.h> /* for proc_runway_root */#include <asm/pdc.h> /* for PDC_MODEL_* */#include <asm/pdcpat.h> /* for is_pdc_pat() */#include <asm/parisc-device.h>#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#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#define SBA_INLINE __inline__#define DEFAULT_DMA_HINT_REG 0struct sba_device *sba_list;EXPORT_SYMBOL_GPL(sba_list);static unsigned long ioc_needs_fdc = 0;/* 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))#ifdef CONFIG_AGP_PARISC#define SBA_AGP_SUPPORT#endif /*CONFIG_AGP_PARISC*/#ifdef SBA_AGP_SUPPORTstatic int sba_reserve_agpgart = 1;module_param(sba_reserve_agpgart, int, 0444);MODULE_PARM_DESC(sba_reserve_agpgart, "Reserve half of IO pdir as AGPGART");#endif/************************************** 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) readl(addr)#define READ_REG64(addr) readq(addr)#define WRITE_REG32(val, addr) writel((val), (addr))#define WRITE_REG64(val, addr) writeq((val), (addr))#ifdef CONFIG_64BIT#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 CONFIG_64BIT 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(void __iomem *hpa){ DBG_INIT("SBA at 0x%p\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 void sba_dump_tlb(void __iomem *hpa){ DBG_INIT("IO TLB at 0x%p\n", hpa); DBG_INIT("IOC_IBASE : 0x%Lx\n", READ_REG64(hpa+IOC_IBASE)); DBG_INIT("IOC_IMASK : 0x%Lx\n", READ_REG64(hpa+IOC_IMASK)); DBG_INIT("IOC_TCNFG : 0x%Lx\n", READ_REG64(hpa+IOC_TCNFG)); DBG_INIT("IOC_PDIR_BASE: 0x%Lx\n", READ_REG64(hpa+IOC_PDIR_BASE)); DBG_INIT("\n");}#else#define sba_dump_ranges(x)#define sba_dump_tlb(x)#endif /* DEBUG_SBA_INIT */#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. */#ifdef ZX1_SUPPORT/* Pluto (aka ZX1) boxes need to set or clear the ibase bits appropriately */#define SBA_IOVA(ioc,iovp,offset,hint_reg) ((ioc->ibase) | (iovp) | (offset))#define SBA_IOVP(ioc,iova) ((iova) & (ioc)->iovp_mask)#else/* only support Astro and ancestors. Saves a few cycles in key places */#define SBA_IOVA(ioc,iovp,offset,hint_reg) ((iovp) | (offset))#define SBA_IOVP(ioc,iova) (iova)#endif#define PDIR_INDEX(iovp) ((iovp)>>IOVP_SHIFT)#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; 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 = ALIGN(ioc->res_bitshift, o); unsigned long mask; if (bitshiftcnt >= BITS_PER_LONG) { bitshiftcnt = 0; res_ptr++; } mask = RESMAP_MASK(bits_wanted) >> bitshiftcnt; DBG_RES("%s() o %ld %p", __FUNCTION__, o, res_ptr); while(res_ptr < res_end) { DBG_RES(" %p %lx %lx\n", res_ptr, mask, *res_ptr); WARN_ON(mask == 0); if(((*res_ptr) & mask) == 0) { *res_ptr |= mask; /* mark resources busy! */ pide = ((unsigned long)res_ptr - (unsigned long)ioc->res_map); pide <<= 3; /* convert to bit address */ pide += bitshiftcnt; break; } mask >>= o; bitshiftcnt += o; if (mask == 0) { mask = RESMAP_MASK(bits_wanted); bitshiftcnt=0; res_ptr++; } } /* look in the same word on the next pass */ ioc->res_bitshift = bitshiftcnt + bits_wanted; } /* wrapped ? */ if (res_end <= res_ptr) { ioc->res_hint = (unsigned long *) ioc->res_map; ioc->res_bitshift = 0; } else { ioc->res_hint = res_ptr; } return (pide);}/** * sba_alloc_range - find free bits and mark them in IO PDIR resource bitmap * @ioc: IO MMU structure which owns the pdir we are interested in. * @size: number of bytes to create a mapping for * * Given a size, find consecutive unmarked and then mark those bits in the * resource bit map. */static intsba_alloc_range(struct ioc *ioc, size_t size){ unsigned int pages_needed = size >> IOVP_SHIFT;#ifdef SBA_COLLECT_STATS unsigned long cr_start = mfctl(16);#endif unsigned long pide; pide = sba_search_bitmap(ioc, pages_needed); if (pide >= (ioc->res_size << 3)) { pide = sba_search_bitmap(ioc, pages_needed); if (pide >= (ioc->res_size << 3)) panic("%s: I/O MMU @ %p is out of mapping resources\n", __FILE__, ioc->ioc_hpa); }#ifdef ASSERT_PDIR_SANITY /* verify the first enable bit is clear */ if(0x00 != ((u8 *) ioc->pdir_base)[pide*sizeof(u64) + 7]) { sba_dump_pdir_entry(ioc, "sba_search_bitmap() botched it?", pide); }#endif DBG_RES("%s(%x) %d -> %lx hint %x/%x\n", __FUNCTION__, size, pages_needed, pide, (uint) ((unsigned long) ioc->res_hint - (unsigned long) ioc->res_map), ioc->res_bitshift );#ifdef SBA_COLLECT_STATS { unsigned long cr_end = mfctl(16); unsigned long tmp = cr_end - cr_start; /* check for roll over */ cr_start = (cr_end < cr_start) ? -(tmp) : (tmp); } ioc->avg_search[ioc->avg_idx++] = cr_start; ioc->avg_idx &= SBA_SEARCH_SAMPLE - 1; ioc->used_pages += pages_needed;#endif return (pide);}/** * sba_free_range - unmark bits in IO PDIR resource bitmap * @ioc: IO MMU structure which owns the pdir we are interested in. * @iova: IO virtual address which was previously allocated. * @size: number of bytes to create a mapping for * * clear bits in the ioc's resource map */static SBA_INLINE voidsba_free_range(struct ioc *ioc, dma_addr_t iova, size_t size){ unsigned long iovp = SBA_IOVP(ioc, iova); unsigned int pide = PDIR_INDEX(iovp); unsigned int ridx = pide >> 3; /* convert bit to byte address */ unsigned long *res_ptr = (unsigned long *) &((ioc)->res_map[ridx & ~RESMAP_IDX_MASK]); int bits_not_wanted = size >> IOVP_SHIFT; /* 3-bits "bit" address plus 2 (or 3) bits for "byte" == bit in word */ unsigned long m = RESMAP_MASK(bits_not_wanted) >> (pide & (BITS_PER_LONG - 1)); DBG_RES("%s( ,%x,%x) %x/%lx %x %p %lx\n", __FUNCTION__, (uint) iova, size, bits_not_wanted, m, pide, res_ptr, *res_ptr);#ifdef SBA_COLLECT_STATS ioc->used_pages -= bits_not_wanted;#endif *res_ptr &= ~m;}/**************************************************************** "Dynamic DMA Mapping" support (aka "Coherent I/O")****************************************************************/#ifdef SBA_HINT_SUPPORT#define SBA_DMA_HINT(ioc, val) ((val) << (ioc)->hint_shift_pdir)#endiftypedef unsigned long space_t;#define KERNEL_SPACE 0/** * sba_io_pdir_entry - fill in one IO PDIR entry * @pdir_ptr: pointer to IO PDIR entry * @sid: process Space ID - currently only support KERNEL_SPACE * @vba: Virtual CPU address of buffer to map * @hint: DMA hint set to use for this mapping * * SBA Mapping Routine * * Given a virtual address (vba, arg2) and space id, (sid, arg1) * sba_io_pdir_entry() loads the I/O PDIR entry pointed to by * pdir_ptr (arg0). * Using the bass-ackwards HP bit numbering, Each IO Pdir entry * for Astro/Ike looks like: * * * 0 19 51 55 63 * +-+---------------------+----------------------------------+----+--------+ * |V| U | PPN[43:12] | U | VI | * +-+---------------------+----------------------------------+----+--------+ *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -