📄 sba_iommu.c
字号:
/** * sba_fill_pdir - write allocated SG entries into IO PDIR * @ioc: IO MMU structure which owns the pdir we are interested in. * @startsg: list of IOVA/size pairs * @nents: number of entries in startsg list * * Take preprocessed SG list and write corresponding entries * in the IO PDIR. */static SBA_INLINE intsba_fill_pdir( struct ioc *ioc, struct scatterlist *startsg, int nents){ struct scatterlist *dma_sg = startsg; /* pointer to current DMA */ int n_mappings = 0; u64 *pdirp = 0; unsigned long dma_offset = 0; dma_sg--; while (nents-- > 0) { int cnt = sg_dma_len(startsg); sg_dma_len(startsg) = 0;#ifdef DEBUG_LARGE_SG_ENTRIES if (dump_run_sg) printk(KERN_DEBUG " %2d : %08lx/%05x %p/%05x\n", nents, (unsigned long) sg_dma_address(startsg), cnt, sg_virt_addr(startsg), startsg->length );#else DBG_RUN_SG(" %d : %08lx/%05x %p/%05x\n", nents, (unsigned long) sg_dma_address(startsg), cnt, sg_virt_addr(startsg), startsg->length );#endif /* ** Look for the start of a new DMA stream */ if (sg_dma_address(startsg) & PIDE_FLAG) { u32 pide = sg_dma_address(startsg) & ~PIDE_FLAG; dma_offset = (unsigned long) pide & ~IOVP_MASK; sg_dma_address(startsg) = 0; dma_sg++; sg_dma_address(dma_sg) = pide; pdirp = &(ioc->pdir_base[pide >> IOVP_SHIFT]); n_mappings++; } /* ** Look for a VCONTIG chunk */ if (cnt) { unsigned long vaddr = (unsigned long) sg_virt_addr(startsg); ASSERT(pdirp); /* Since multiple Vcontig blocks could make up ** one DMA stream, *add* cnt to dma_len. */ sg_dma_len(dma_sg) += cnt; cnt += dma_offset; dma_offset=0; /* only want offset on first chunk */ cnt = ROUNDUP(cnt, IOVP_SIZE);#ifdef CONFIG_PROC_FS ioc->msg_pages += cnt >> IOVP_SHIFT;#endif do { sba_io_pdir_entry(pdirp, KERNEL_SPACE, vaddr); vaddr += IOVP_SIZE; cnt -= IOVP_SIZE; pdirp++; } while (cnt > 0); } startsg++; }#ifdef DEBUG_LARGE_SG_ENTRIES dump_run_sg = 0;#endif return(n_mappings);}/*** Two address ranges are DMA contiguous *iff* "end of prev" and** "start of next" are both on a page boundry.**** (shift left is a quick trick to mask off upper bits)*/#define DMA_CONTIG(__X, __Y) \ (((((unsigned long) __X) | ((unsigned long) __Y)) << (BITS_PER_LONG - PAGE_SHIFT)) == 0UL)/** * sba_coalesce_chunks - preprocess the SG list * @ioc: IO MMU structure which owns the pdir we are interested in. * @startsg: list of IOVA/size pairs * @nents: number of entries in startsg list * * First pass is to walk the SG list and determine where the breaks are * in the DMA stream. Allocates PDIR entries but does not fill them. * Returns the number of DMA chunks. * * Doing the fill seperate from the coalescing/allocation keeps the * code simpler. Future enhancement could make one pass through * the sglist do both. */static SBA_INLINE intsba_coalesce_chunks( struct ioc *ioc, struct scatterlist *startsg, int nents){ struct scatterlist *vcontig_sg; /* VCONTIG chunk head */ unsigned long vcontig_len; /* len of VCONTIG chunk */ unsigned long vcontig_end; struct scatterlist *dma_sg; /* next DMA stream head */ unsigned long dma_offset, dma_len; /* start/len of DMA stream */ int n_mappings = 0; while (nents > 0) { unsigned long vaddr = (unsigned long) sg_virt_addr(startsg); /* ** Prepare for first/next DMA stream */ dma_sg = vcontig_sg = startsg; dma_len = vcontig_len = vcontig_end = startsg->length; vcontig_end += vaddr; dma_offset = vaddr & ~IOVP_MASK; /* PARANOID: clear entries */ sg_dma_address(startsg) = 0; sg_dma_len(startsg) = 0; /* ** This loop terminates one iteration "early" since ** it's always looking one "ahead". */ while (--nents > 0) { unsigned long vaddr; /* tmp */ startsg++; /* PARANOID: clear entries */ sg_dma_address(startsg) = 0; sg_dma_len(startsg) = 0; /* catch brokenness in SCSI layer */ ASSERT(startsg->length <= DMA_CHUNK_SIZE); /* ** First make sure current dma stream won't ** exceed DMA_CHUNK_SIZE if we coalesce the ** next entry. */ if (((dma_len + dma_offset + startsg->length + ~IOVP_MASK) & IOVP_MASK) > DMA_CHUNK_SIZE) break; /* ** Then look for virtually contiguous blocks. ** PARISC needs to associate a virtual address ** with each IO address mapped. The CPU cache is ** virtually tagged and the IOMMU uses part ** of the virtual address to participate in ** CPU cache coherency. ** ** append the next transaction? */ vaddr = (unsigned long) sg_virt_addr(startsg); if (vcontig_end == vaddr) { vcontig_len += startsg->length; vcontig_end += startsg->length; dma_len += startsg->length; continue; }#ifdef DEBUG_LARGE_SG_ENTRIES dump_run_sg = (vcontig_len > IOVP_SIZE);#endif /* ** Not virtually contigous. ** Terminate prev chunk. ** Start a new chunk. ** ** Once we start a new VCONTIG chunk, dma_offset ** can't change. And we need the offset from the first ** chunk - not the last one. Ergo Successive chunks ** must start on page boundaries and dove tail ** with it's predecessor. */ sg_dma_len(vcontig_sg) = vcontig_len; vcontig_sg = startsg; vcontig_len = startsg->length; /* ** 3) do the entries end/start on page boundaries? ** Don't update vcontig_end until we've checked. */ if (DMA_CONTIG(vcontig_end, vaddr)) { vcontig_end = vcontig_len + vaddr; dma_len += vcontig_len; continue; } else { break; } } /* ** End of DMA Stream ** Terminate last VCONTIG block. ** Allocate space for DMA stream. */ sg_dma_len(vcontig_sg) = vcontig_len; dma_len = (dma_len + dma_offset + ~IOVP_MASK) & IOVP_MASK; ASSERT(dma_len <= DMA_CHUNK_SIZE); sg_dma_address(dma_sg) = PIDE_FLAG | (sba_alloc_range(ioc, dma_len) << IOVP_SHIFT) | dma_offset; n_mappings++; } return n_mappings;}/** * sba_map_sg - map Scatter/Gather list * @dev: instance of PCI owned by the driver that's asking. * @sglist: array of buffer/length pairs * @nents: number of entries in list * @direction: R/W or both. * * See Documentation/DMA-mapping.txt */static intsba_map_sg(struct pci_dev *dev, struct scatterlist *sglist, int nents, int direction){ struct ioc *ioc; int coalesced, filled = 0; unsigned long flags; DBG_RUN_SG("%s() START %d entries\n", __FUNCTION__, nents); ASSERT(dev->sysdata); ioc = GET_IOC(dev); ASSERT(ioc); /* Fast path single entry scatterlists. */ if (nents == 1) { sg_dma_address(sglist) = sba_map_single(dev, sg_virt_addr(sglist), sglist->length, direction); sg_dma_len(sglist) = sglist->length; return 1; } spin_lock_irqsave(&ioc->res_lock, flags);#ifdef ASSERT_PDIR_SANITY if (sba_check_pdir(ioc,"Check before sba_map_sg()")) { sba_dump_sg(ioc, sglist, nents); panic("Check before sba_map_sg()"); }#endif#ifdef CONFIG_PROC_FS ioc->msg_calls++;#endif /* ** First coalesce the chunks and allocate I/O pdir space ** ** If this is one DMA stream, we can properly map using the ** correct virtual address associated with each DMA page. ** w/o this association, we wouldn't have coherent DMA! ** Access to the virtual address is what forces a two pass algorithm. */ coalesced = sba_coalesce_chunks(ioc, sglist, nents); /* ** Program the I/O Pdir ** ** map the virtual addresses to the I/O Pdir ** o dma_address will contain the pdir index ** o dma_len will contain the number of bytes to map ** o address contains the virtual address. */ filled = sba_fill_pdir(ioc, sglist, nents);#ifdef ASSERT_PDIR_SANITY if (sba_check_pdir(ioc,"Check after sba_map_sg()")) { sba_dump_sg(ioc, sglist, nents); panic("Check after sba_map_sg()\n"); }#endif spin_unlock_irqrestore(&ioc->res_lock, flags); ASSERT(coalesced == filled); DBG_RUN_SG("%s() DONE %d mappings\n", __FUNCTION__, filled); return filled;}/** * sba_unmap_sg - unmap Scatter/Gather list * @dev: instance of PCI owned by the driver that's asking. * @sglist: array of buffer/length pairs * @nents: number of entries in list * @direction: R/W or both. * * See Documentation/DMA-mapping.txt */static void sba_unmap_sg(struct pci_dev *dev, struct scatterlist *sglist, int nents, int direction){ struct ioc *ioc;#ifdef ASSERT_PDIR_SANITY unsigned long flags;#endif DBG_RUN_SG("%s() START %d entries, %p,%x\n", __FUNCTION__, nents, sg_virt_addr(sglist), sglist->length); ASSERT(dev->sysdata); ioc = GET_IOC(dev); ASSERT(ioc);#ifdef CONFIG_PROC_FS ioc->usg_calls++;#endif#ifdef ASSERT_PDIR_SANITY spin_lock_irqsave(&ioc->res_lock, flags); sba_check_pdir(ioc,"Check before sba_unmap_sg()"); spin_unlock_irqrestore(&ioc->res_lock, flags);#endif while (sg_dma_len(sglist) && nents--) { sba_unmap_single(dev, sg_dma_address(sglist), sg_dma_len(sglist), direction);#ifdef CONFIG_PROC_FS ioc->usg_pages += ((sg_dma_address(sglist) & ~IOVP_MASK) + sg_dma_len(sglist) + IOVP_SIZE - 1) >> PAGE_SHIFT; ioc->usingle_calls--; /* kluge since call is unmap_sg() */#endif ++sglist; } DBG_RUN_SG("%s() DONE (nents %d)\n", __FUNCTION__, nents);#ifdef ASSERT_PDIR_SANITY spin_lock_irqsave(&ioc->res_lock, flags); sba_check_pdir(ioc,"Check after sba_unmap_sg()"); spin_unlock_irqrestore(&ioc->res_lock, flags);#endif}static struct pci_dma_ops sba_ops = { sba_dma_supported, sba_alloc_consistent, /* allocate cacheable host mem */ sba_free_consistent, /* release cacheable host mem */ sba_map_single, sba_unmap_single, sba_map_sg, sba_unmap_sg, NULL, /* dma_sync_single */ NULL /* dma_sync_sg */};/****************************************************************************** SBA PAT PDC support**** o call pdc_pat_cell_module()** o store ranges in PCI "resource" structures****************************************************************************/static voidsba_get_pat_resources(struct sba_device *sba_dev){#if 0/*** TODO/REVISIT/FIXME: support for directed ranges requires calls to** PAT PDC to program the SBA/LBA directed range registers...this** burden may fall on the LBA code since it directly supports the** PCI subsystem. It's not clear yet. - ggg*/PAT_MOD(mod)->mod_info.mod_pages = PAT_GET_MOD_PAGES(temp); FIXME : ???PAT_MOD(mod)->mod_info.dvi = PAT_GET_DVI(temp); Tells where the dvi bits are located in the address.PAT_MOD(mod)->mod_info.ioc = PAT_GET_IOC(temp); FIXME : ???#endif}/**************************************************************** Initialization and claim****************************************************************/#define PIRANHA_ADDR_MASK 0x00160000UL /* bit 17,18,20 */#define PIRANHA_ADDR_VAL 0x00060000UL /* bit 17,18 on */static void *sba_alloc_pdir(unsigned int pdir_size){ unsigned long pdir_base; unsigned long pdir_order = get_order(pdir_size); pdir_base = __get_free_pages(GFP_KERNEL, pdir_order); if (NULL == (void *) pdir_base) panic("sba_ioc_init() could not allocate I/O Page Table\n"); /* If this is not PA8700 (PCX-W2) ** OR newer than ver 2.2 ** OR in a system that doesn't need VINDEX bits from SBA, ** ** then we aren't exposed to the HW bug. */ if ( ((boot_cpu_data.pdc.cpuid >> 5) & 0x7f) != 0x13 || (boot_cpu_data.pdc.versions > 0x202) || (boot_cpu_data.pdc.capabilities & 0x08L) ) return (void *) pdir_base; /* * PA8700 (PCX-W2, aka piranha) silent data corruption fix * * An interaction between PA8700 CPU (Ver 2.2 or older) and * Ike/Astro can cause silent data corruption. This is only * a problem if the I/O PDIR is located in memory such that * (little-endian) bits 17 and 18 are on and bit 20 is off. * * Since the max IO Pdir size is 2MB, by cleverly allocating the * right physical address, we can either avoid (IOPDIR <= 1MB) * or minimize (2MB IO Pdir) the problem if we restrict the * IO Pdir to a maximum size of 2MB-128K (1902K). * * Because we always allocate 2^N sized IO pdirs, either of the * "bad" regions will be the last 128K if at all. That's easy * to test for. * */ if (pdir_order <= (19-12)) { if (((virt_to_phys(pdir_base)+pdir_size-1) & PIRANHA_ADDR_MASK) == PIRANHA_ADDR_VAL) { /* allocate a new one on 512k alignment */ unsigned long new_pdir = __get_free_pages(GFP_KERNEL, (19-12)); /* release original */ free_pages(pdir_base, pdir_order); pdir_base = new_pdir; /* release excess */ while (pdir_order < (19-12)) { new_pdir += pdir_size; free_pages(new_pdir, pdir_order); pdir_order +=1; pdir_size <<=1; } } } else { /* ** 1MB or 2MB Pdir ** Needs to be aligned on an "odd" 1MB boundary. */ unsigned long new_pdir = __get_free_pages(GFP_KERNEL, pdir_order+1); /* 2 or 4MB */ /* release original */ free_pages( pdir_base, pdir_order); /* release first 1MB */ free_pages(new_pdir, 20-12); pdir_base = new_pdir + 1024*1024; if (pdir_order > (20-12)) { /* ** 2MB Pdir. ** ** Flag tells init_bitmap() to mark bad 128k as used ** and to reduce the size by 128k. */ piranha_bad_128k = 1; new_pdir += 3*1024*1024; /* release last 1MB */ free_pages(new_pdir, 20-12); /* release unusable 128KB */ free_pages(new_pdir - 128*1024 , 17-12); pdir_size -= 128*1024; } } memset((void *) pdir_base, 0, pdir_size); return (void *) pdir_base;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -