iommu.c

来自「LINUX 2.6.17.4的源码」· C语言 代码 · 共 591 行 · 第 1/2 页

C
591
字号
				printk(KERN_INFO "iommu_alloc failed, tbl %p vaddr %lx"				       " npages %lx\n", tbl, vaddr, npages);			goto failure;		}		/* Convert entry to a dma_addr_t */		entry += tbl->it_offset;		dma_addr = entry << PAGE_SHIFT;		dma_addr |= s->offset;		DBG("  - %lx pages, entry: %lx, dma_addr: %lx\n",			    npages, entry, dma_addr);		/* Insert into HW table */		ppc_md.tce_build(tbl, entry, npages, vaddr & PAGE_MASK, direction);		/* If we are in an open segment, try merging */		if (segstart != s) {			DBG("  - trying merge...\n");			/* We cannot merge if:			 * - allocated dma_addr isn't contiguous to previous allocation			 */			if (novmerge || (dma_addr != dma_next)) {				/* Can't merge: create a new segment */				segstart = s;				outcount++; outs++;				DBG("    can't merge, new segment.\n");			} else {				outs->dma_length += s->length;				DBG("    merged, new len: %lx\n", outs->dma_length);			}		}		if (segstart == s) {			/* This is a new segment, fill entries */			DBG("  - filling new segment.\n");			outs->dma_address = dma_addr;			outs->dma_length = slen;		}		/* Calculate next page pointer for contiguous check */		dma_next = dma_addr + slen;		DBG("  - dma next is: %lx\n", dma_next);	}	/* Flush/invalidate TLB caches if necessary */	if (ppc_md.tce_flush)		ppc_md.tce_flush(tbl);	spin_unlock_irqrestore(&(tbl->it_lock), flags);	DBG("mapped %d elements:\n", outcount);	/* For the sake of iommu_unmap_sg, we clear out the length in the	 * next entry of the sglist if we didn't fill the list completely	 */	if (outcount < incount) {		outs++;		outs->dma_address = DMA_ERROR_CODE;		outs->dma_length = 0;	}	/* Make sure updates are seen by hardware */	mb();	return outcount; failure:	for (s = &sglist[0]; s <= outs; s++) {		if (s->dma_length != 0) {			unsigned long vaddr, npages;			vaddr = s->dma_address & PAGE_MASK;			npages = (PAGE_ALIGN(s->dma_address + s->dma_length) - vaddr)				>> PAGE_SHIFT;			__iommu_free(tbl, vaddr, npages);			s->dma_address = DMA_ERROR_CODE;			s->dma_length = 0;		}	}	spin_unlock_irqrestore(&(tbl->it_lock), flags);	return 0;}void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,		int nelems, enum dma_data_direction direction){	unsigned long flags;	BUG_ON(direction == DMA_NONE);	if (!tbl)		return;	spin_lock_irqsave(&(tbl->it_lock), flags);	while (nelems--) {		unsigned int npages;		dma_addr_t dma_handle = sglist->dma_address;		if (sglist->dma_length == 0)			break;		npages = (PAGE_ALIGN(dma_handle + sglist->dma_length)			  - (dma_handle & PAGE_MASK)) >> PAGE_SHIFT;		__iommu_free(tbl, dma_handle, npages);		sglist++;	}	/* Flush/invalidate TLBs if necessary. As for iommu_free(), we	 * do not do an mb() here, the affected platforms do not need it	 * when freeing.	 */	if (ppc_md.tce_flush)		ppc_md.tce_flush(tbl);	spin_unlock_irqrestore(&(tbl->it_lock), flags);}/* * Build a iommu_table structure.  This contains a bit map which * is used to manage allocation of the tce space. */struct iommu_table *iommu_init_table(struct iommu_table *tbl){	unsigned long sz;	static int welcomed = 0;	/* Set aside 1/4 of the table for large allocations. */	tbl->it_halfpoint = tbl->it_size * 3 / 4;	/* number of bytes needed for the bitmap */	sz = (tbl->it_size + 7) >> 3;	tbl->it_map = (unsigned long *)__get_free_pages(GFP_ATOMIC, get_order(sz));	if (!tbl->it_map)		panic("iommu_init_table: Can't allocate %ld bytes\n", sz);	memset(tbl->it_map, 0, sz);	tbl->it_hint = 0;	tbl->it_largehint = tbl->it_halfpoint;	spin_lock_init(&tbl->it_lock);	/* Clear the hardware table in case firmware left allocations in it */	ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);	if (!welcomed) {		printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",		       novmerge ? "disabled" : "enabled");		welcomed = 1;	}	return tbl;}void iommu_free_table(struct device_node *dn){	struct pci_dn *pdn = dn->data;	struct iommu_table *tbl = pdn->iommu_table;	unsigned long bitmap_sz, i;	unsigned int order;	if (!tbl || !tbl->it_map) {		printk(KERN_ERR "%s: expected TCE map for %s\n", __FUNCTION__,				dn->full_name);		return;	}	/* verify that table contains no entries */	/* it_size is in entries, and we're examining 64 at a time */	for (i = 0; i < (tbl->it_size/64); i++) {		if (tbl->it_map[i] != 0) {			printk(KERN_WARNING "%s: Unexpected TCEs for %s\n",				__FUNCTION__, dn->full_name);			break;		}	}	/* calculate bitmap size in bytes */	bitmap_sz = (tbl->it_size + 7) / 8;	/* free bitmap */	order = get_order(bitmap_sz);	free_pages((unsigned long) tbl->it_map, order);	/* free table */	kfree(tbl);}/* Creates TCEs for a user provided buffer.  The user buffer must be * contiguous real kernel storage (not vmalloc).  The address of the buffer * passed here is the kernel (virtual) address of the buffer.  The buffer * need not be page aligned, the dma_addr_t returned will point to the same * byte within the page as vaddr. */dma_addr_t iommu_map_single(struct iommu_table *tbl, void *vaddr,		size_t size, unsigned long mask,		enum dma_data_direction direction){	dma_addr_t dma_handle = DMA_ERROR_CODE;	unsigned long uaddr;	unsigned int npages;	BUG_ON(direction == DMA_NONE);	uaddr = (unsigned long)vaddr;	npages = PAGE_ALIGN(uaddr + size) - (uaddr & PAGE_MASK);	npages >>= PAGE_SHIFT;	if (tbl) {		dma_handle = iommu_alloc(tbl, vaddr, npages, direction,					 mask >> PAGE_SHIFT, 0);		if (dma_handle == DMA_ERROR_CODE) {			if (printk_ratelimit())  {				printk(KERN_INFO "iommu_alloc failed, "						"tbl %p vaddr %p npages %d\n",						tbl, vaddr, npages);			}		} else			dma_handle |= (uaddr & ~PAGE_MASK);	}	return dma_handle;}void iommu_unmap_single(struct iommu_table *tbl, dma_addr_t dma_handle,		size_t size, enum dma_data_direction direction){	BUG_ON(direction == DMA_NONE);	if (tbl)		iommu_free(tbl, dma_handle, (PAGE_ALIGN(dma_handle + size) -					(dma_handle & PAGE_MASK)) >> PAGE_SHIFT);}/* Allocates a contiguous real buffer and creates mappings over it. * Returns the virtual address of the buffer and sets dma_handle * to the dma address (mapping) of the first page. */void *iommu_alloc_coherent(struct iommu_table *tbl, size_t size,		dma_addr_t *dma_handle, unsigned long mask, gfp_t flag){	void *ret = NULL;	dma_addr_t mapping;	unsigned int npages, order;	size = PAGE_ALIGN(size);	npages = size >> PAGE_SHIFT;	order = get_order(size); 	/*	 * Client asked for way too much space.  This is checked later	 * anyway.  It is easier to debug here for the drivers than in	 * the tce tables.	 */	if (order >= IOMAP_MAX_ORDER) {		printk("iommu_alloc_consistent size too large: 0x%lx\n", size);		return NULL;	}	if (!tbl)		return NULL;	/* Alloc enough pages (and possibly more) */	ret = (void *)__get_free_pages(flag, order);	if (!ret)		return NULL;	memset(ret, 0, size);	/* Set up tces to cover the allocated range */	mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL,			      mask >> PAGE_SHIFT, order);	if (mapping == DMA_ERROR_CODE) {		free_pages((unsigned long)ret, order);		ret = NULL;	} else		*dma_handle = mapping;	return ret;}void iommu_free_coherent(struct iommu_table *tbl, size_t size,			 void *vaddr, dma_addr_t dma_handle){	unsigned int npages;	if (tbl) {		size = PAGE_ALIGN(size);		npages = size >> PAGE_SHIFT;		iommu_free(tbl, dma_handle, npages);		free_pages((unsigned long)vaddr, get_order(size));	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?