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

📄 swiotlb.c

📁 h内核
💻 C
📖 第 1 页 / 共 2 页
字号:
	ret = (void *)__get_free_pages(flags, order);	if (ret && address_needs_mapping(hwdev, virt_to_phys(ret))) {		/*		 * The allocated memory isn't reachable by the device.		 * Fall back on swiotlb_map_single().		 */		free_pages((unsigned long) ret, order);		ret = NULL;	}	if (!ret) {		/*		 * We are either out of memory or the device can't DMA		 * to GFP_DMA memory; fall back on		 * swiotlb_map_single(), which will grab memory from		 * the lowest available address range.		 */		dma_addr_t handle;		handle = swiotlb_map_single(NULL, NULL, size, DMA_FROM_DEVICE);		if (dma_mapping_error(handle))			return NULL;		ret = phys_to_virt(handle);	}	memset(ret, 0, size);	dev_addr = virt_to_phys(ret);	/* Confirm address can be DMA'd by device */	if (address_needs_mapping(hwdev, dev_addr)) {		printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016lx\n",		       (unsigned long long)*hwdev->dma_mask, dev_addr);		panic("swiotlb_alloc_coherent: allocated memory is out of "		      "range for device");	}	*dma_handle = dev_addr;	return ret;}voidswiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,		      dma_addr_t dma_handle){	if (!(vaddr >= (void *)io_tlb_start                    && vaddr < (void *)io_tlb_end))		free_pages((unsigned long) vaddr, get_order(size));	else		/* DMA_TO_DEVICE to avoid memcpy in unmap_single */		swiotlb_unmap_single (hwdev, dma_handle, size, DMA_TO_DEVICE);}static voidswiotlb_full(struct device *dev, size_t size, int dir, int do_panic){	/*	 * Ran out of IOMMU space for this operation. This is very bad.	 * Unfortunately the drivers cannot handle this operation properly.	 * unless they check for pci_dma_mapping_error (most don't)	 * When the mapping is small enough return a static buffer to limit	 * the damage, or panic when the transfer is too big.	 */	printk(KERN_ERR "PCI-DMA: Out of SW-IOMMU space for %lu bytes at "	       "device %s\n", size, dev ? dev->bus_id : "?");	if (size > io_tlb_overflow && do_panic) {		if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)			panic("PCI-DMA: Memory would be corrupted\n");		if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)			panic("PCI-DMA: Random memory would be DMAed\n");	}}/* * Map a single buffer of the indicated size for DMA in streaming mode.  The * PCI address to use is returned. * * Once the device is given the dma address, the device owns this memory until * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed. */dma_addr_tswiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir){	unsigned long dev_addr = virt_to_phys(ptr);	void *map;	if (dir == DMA_NONE)		BUG();	/*	 * If the pointer passed in happens to be in the device's DMA window,	 * we can safely return the device addr and not worry about bounce	 * buffering it.	 */	if (!address_needs_mapping(hwdev, dev_addr) && !swiotlb_force)		return dev_addr;	/*	 * Oh well, have to allocate and map a bounce buffer.	 */	map = map_single(hwdev, ptr, size, dir);	if (!map) {		swiotlb_full(hwdev, size, dir, 1);		map = io_tlb_overflow_buffer;	}	dev_addr = virt_to_phys(map);	/*	 * Ensure that the address returned is DMA'ble	 */	if (address_needs_mapping(hwdev, dev_addr))		panic("map_single: bounce buffer is not DMA'ble");	return dev_addr;}/* * Since DMA is i-cache coherent, any (complete) pages that were written via * DMA can be marked as "clean" so that update_mmu_cache() doesn't have to * flush them when they get mapped into an executable vm-area. */static voidmark_clean(void *addr, size_t size){	unsigned long pg_addr, end;	pg_addr = PAGE_ALIGN((unsigned long) addr);	end = (unsigned long) addr + size;	while (pg_addr + PAGE_SIZE <= end) {		struct page *page = virt_to_page(pg_addr);		set_bit(PG_arch_1, &page->flags);		pg_addr += PAGE_SIZE;	}}/* * Unmap a single streaming mode DMA translation.  The dma_addr and size must * match what was provided for in a previous swiotlb_map_single call.  All * other usages are undefined. * * After this call, reads by the cpu to the buffer are guaranteed to see * whatever the device wrote there. */voidswiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,		     int dir){	char *dma_addr = phys_to_virt(dev_addr);	if (dir == DMA_NONE)		BUG();	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)		unmap_single(hwdev, dma_addr, size, dir);	else if (dir == DMA_FROM_DEVICE)		mark_clean(dma_addr, size);}/* * Make physical memory consistent for a single streaming mode DMA translation * after a transfer. * * If you perform a swiotlb_map_single() but wish to interrogate the buffer * using the cpu, yet do not wish to teardown the PCI dma mapping, you must * call this function before doing so.  At the next point you give the PCI dma * address back to the card, you must first perform a * swiotlb_dma_sync_for_device, and then the device again owns the buffer */voidswiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,			    size_t size, int dir){	char *dma_addr = phys_to_virt(dev_addr);	if (dir == DMA_NONE)		BUG();	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)		sync_single(hwdev, dma_addr, size, dir);	else if (dir == DMA_FROM_DEVICE)		mark_clean(dma_addr, size);}voidswiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,			       size_t size, int dir){	char *dma_addr = phys_to_virt(dev_addr);	if (dir == DMA_NONE)		BUG();	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)		sync_single(hwdev, dma_addr, size, dir);	else if (dir == DMA_FROM_DEVICE)		mark_clean(dma_addr, size);}/* * Map a set of buffers described by scatterlist in streaming mode for DMA. * This is the scatter-gather version of the above swiotlb_map_single * interface.  Here the scatter gather list elements are each tagged with the * appropriate dma address and length.  They are obtained via * sg_dma_{address,length}(SG). * * NOTE: An implementation may be able to use a smaller number of *       DMA address/length pairs than there are SG table elements. *       (for example via virtual mapping capabilities) *       The routine returns the number of addr/length pairs actually *       used, at most nents. * * Device ownership issues as mentioned above for swiotlb_map_single are the * same here. */intswiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,	       int dir){	void *addr;	unsigned long dev_addr;	int i;	if (dir == DMA_NONE)		BUG();	for (i = 0; i < nelems; i++, sg++) {		addr = SG_ENT_VIRT_ADDRESS(sg);		dev_addr = virt_to_phys(addr);		if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {			sg->dma_address = (dma_addr_t) virt_to_phys(map_single(hwdev, addr, sg->length, dir));			if (!sg->dma_address) {				/* Don't panic here, we expect map_sg users				   to do proper error handling. */				swiotlb_full(hwdev, sg->length, dir, 0);				swiotlb_unmap_sg(hwdev, sg - i, i, dir);				sg[0].dma_length = 0;				return 0;			}		} else			sg->dma_address = dev_addr;		sg->dma_length = sg->length;	}	return nelems;}/* * Unmap a set of streaming mode DMA translations.  Again, cpu read rules * concerning calls here are the same as for swiotlb_unmap_single() above. */voidswiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,		 int dir){	int i;	if (dir == DMA_NONE)		BUG();	for (i = 0; i < nelems; i++, sg++)		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))			unmap_single(hwdev, (void *) phys_to_virt(sg->dma_address), sg->dma_length, dir);		else if (dir == DMA_FROM_DEVICE)			mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);}/* * Make physical memory consistent for a set of streaming mode DMA translations * after a transfer. * * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules * and usage. */voidswiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,			int nelems, int dir){	int i;	if (dir == DMA_NONE)		BUG();	for (i = 0; i < nelems; i++, sg++)		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))			sync_single(hwdev, (void *) sg->dma_address,				    sg->dma_length, dir);}voidswiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,			   int nelems, int dir){	int i;	if (dir == DMA_NONE)		BUG();	for (i = 0; i < nelems; i++, sg++)		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))			sync_single(hwdev, (void *) sg->dma_address,				    sg->dma_length, dir);}intswiotlb_dma_mapping_error(dma_addr_t dma_addr){	return (dma_addr == virt_to_phys(io_tlb_overflow_buffer));}/* * Return whether the given PCI device DMA address mask can be supported * properly.  For example, if your device can only drive the low 24-bits * during PCI bus mastering, then you would pass 0x00ffffff as the mask to * this function. */intswiotlb_dma_supported (struct device *hwdev, u64 mask){	return (virt_to_phys (io_tlb_end) - 1) <= mask;}EXPORT_SYMBOL(swiotlb_init);EXPORT_SYMBOL(swiotlb_map_single);EXPORT_SYMBOL(swiotlb_unmap_single);EXPORT_SYMBOL(swiotlb_map_sg);EXPORT_SYMBOL(swiotlb_unmap_sg);EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);EXPORT_SYMBOL(swiotlb_sync_single_for_device);EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);EXPORT_SYMBOL(swiotlb_sync_sg_for_device);EXPORT_SYMBOL(swiotlb_dma_mapping_error);EXPORT_SYMBOL(swiotlb_alloc_coherent);EXPORT_SYMBOL(swiotlb_free_coherent);EXPORT_SYMBOL(swiotlb_dma_supported);

⌨️ 快捷键说明

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