⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iommu.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
			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 = sg_next(outs);				DBG("    can't merge, new segment.\n");			} else {				outs->dma_length += s->length;				DBG("    merged, new len: %ux\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 = sg_next(outs);		outs->dma_address = DMA_ERROR_CODE;		outs->dma_length = 0;	}	/* Make sure updates are seen by hardware */	mb();	return outcount; failure:	for_each_sg(sglist, s, nelems, i) {		if (s->dma_length != 0) {			unsigned long vaddr, npages;			vaddr = s->dma_address & IOMMU_PAGE_MASK;			npages = iommu_num_pages(s->dma_address, s->dma_length);			__iommu_free(tbl, vaddr, npages);			s->dma_address = DMA_ERROR_CODE;			s->dma_length = 0;		}		if (s == outs)			break;	}	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){	struct scatterlist *sg;	unsigned long flags;	BUG_ON(direction == DMA_NONE);	if (!tbl)		return;	spin_lock_irqsave(&(tbl->it_lock), flags);	sg = sglist;	while (nelems--) {		unsigned int npages;		dma_addr_t dma_handle = sg->dma_address;		if (sg->dma_length == 0)			break;		npages = iommu_num_pages(dma_handle, sg->dma_length);		__iommu_free(tbl, dma_handle, npages);		sg = sg_next(sg);	}	/* 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, int nid){	unsigned long sz;	unsigned long start_index, end_index;	unsigned long entries_per_4g;	unsigned long index;	static int welcomed = 0;	struct page *page;	/* 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;	page = alloc_pages_node(nid, GFP_ATOMIC, get_order(sz));	if (!page)		panic("iommu_init_table: Can't allocate %ld bytes\n", sz);	tbl->it_map = page_address(page);	memset(tbl->it_map, 0, sz);	tbl->it_hint = 0;	tbl->it_largehint = tbl->it_halfpoint;	spin_lock_init(&tbl->it_lock);#ifdef CONFIG_CRASH_DUMP	if (ppc_md.tce_get) {		unsigned long tceval;		unsigned long tcecount = 0;		/*		 * Reserve the existing mappings left by the first kernel.		 */		for (index = 0; index < tbl->it_size; index++) {			tceval = ppc_md.tce_get(tbl, index + tbl->it_offset);			/*			 * Freed TCE entry contains 0x7fffffffffffffff on JS20			 */			if (tceval && (tceval != 0x7fffffffffffffffUL)) {				__set_bit(index, tbl->it_map);				tcecount++;			}		}		if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {			printk(KERN_WARNING "TCE table is full; ");			printk(KERN_WARNING "freeing %d entries for the kdump boot\n",				KDUMP_MIN_TCE_ENTRIES);			for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;				index < tbl->it_size; index++)				__clear_bit(index, tbl->it_map);		}	}#else	/* Clear the hardware table in case firmware left allocations in it */	ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);#endif	/*	 * DMA cannot cross 4 GB boundary.  Mark last entry of each 4	 * GB chunk as reserved.	 */	if (protect4gb) {		entries_per_4g = 0x100000000l >> IOMMU_PAGE_SHIFT;		/* Mark the last bit before a 4GB boundary as used */		start_index = tbl->it_offset | (entries_per_4g - 1);		start_index -= tbl->it_offset;		end_index = tbl->it_size;		for (index = start_index; index < end_index - 1; index += entries_per_4g)			__set_bit(index, tbl->it_map);	}	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, align;	BUG_ON(direction == DMA_NONE);	uaddr = (unsigned long)vaddr;	npages = iommu_num_pages(uaddr, size);	if (tbl) {		align = 0;		if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && size >= PAGE_SIZE &&		    ((unsigned long)vaddr & ~PAGE_MASK) == 0)			align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;		dma_handle = iommu_alloc(tbl, vaddr, npages, direction,					 mask >> IOMMU_PAGE_SHIFT, align);		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 & ~IOMMU_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){	unsigned int npages;	BUG_ON(direction == DMA_NONE);	if (tbl) {		npages = iommu_num_pages(dma_handle, size);		iommu_free(tbl, dma_handle, npages);	}}/* 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, int node){	void *ret = NULL;	dma_addr_t mapping;	unsigned int order;	unsigned int nio_pages, io_order;	struct page *page;	size = PAGE_ALIGN(size);	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) */	page = alloc_pages_node(node, flag, order);	if (!page)		return NULL;	ret = page_address(page);	memset(ret, 0, size);	/* Set up tces to cover the allocated range */	nio_pages = size >> IOMMU_PAGE_SHIFT;	io_order = get_iommu_order(size);	mapping = iommu_alloc(tbl, ret, nio_pages, DMA_BIDIRECTIONAL,			      mask >> IOMMU_PAGE_SHIFT, io_order);	if (mapping == DMA_ERROR_CODE) {		free_pages((unsigned long)ret, order);		return NULL;	}	*dma_handle = mapping;	return ret;}void iommu_free_coherent(struct iommu_table *tbl, size_t size,			 void *vaddr, dma_addr_t dma_handle){	if (tbl) {		unsigned int nio_pages;		size = PAGE_ALIGN(size);		nio_pages = size >> IOMMU_PAGE_SHIFT;		iommu_free(tbl, dma_handle, nio_pages);		size = PAGE_ALIGN(size);		free_pages((unsigned long)vaddr, get_order(size));	}}

⌨️ 快捷键说明

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