swiotlb.c

来自「Lib files of linux kernel」· C语言 代码 · 共 858 行 · 第 1/2 页

C
858
字号
			io_tlb_list[i] = ++count;	}	spin_unlock_irqrestore(&io_tlb_lock, flags);}static voidsync_single(struct device *hwdev, char *dma_addr, size_t size,	    int dir, int target){	int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;	char *buffer = io_tlb_orig_addr[index];	buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));	switch (target) {	case SYNC_FOR_CPU:		if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))			memcpy(buffer, dma_addr, size);		else			BUG_ON(dir != DMA_TO_DEVICE);		break;	case SYNC_FOR_DEVICE:		if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))			memcpy(dma_addr, buffer, size);		else			BUG_ON(dir != DMA_FROM_DEVICE);		break;	default:		BUG();	}}void *swiotlb_alloc_coherent(struct device *hwdev, size_t size,		       dma_addr_t *dma_handle, gfp_t flags){	dma_addr_t dev_addr;	void *ret;	int order = get_order(size);	/*	 * XXX fix me: the DMA API should pass us an explicit DMA mask	 * instead, or use ZONE_DMA32 (ia64 overloads ZONE_DMA to be a ~32	 * bit range instead of a 16MB one).	 */	flags |= GFP_DMA;	ret = (void *)__get_free_pages(flags, order);	if (ret && address_needs_mapping(hwdev, virt_to_bus(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(hwdev, NULL, size, DMA_FROM_DEVICE);		if (swiotlb_dma_mapping_error(hwdev, handle))			return NULL;		ret = bus_to_virt(handle);	}	memset(ret, 0, size);	dev_addr = virt_to_bus(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,		       (unsigned long long)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){	WARN_ON(irqs_disabled());	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 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 "DMA: Out of SW-IOMMU space for %zu bytes at "	       "device %s\n", size, dev ? dev->bus_id : "?");	if (size > io_tlb_overflow && do_panic) {		if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)			panic("DMA: Memory would be corrupted\n");		if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)			panic("DMA: Random memory would be DMAed\n");	}}/* * Map a single buffer of the indicated size for DMA in streaming mode.  The * physical 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_attrs(struct device *hwdev, void *ptr, size_t size,			 int dir, struct dma_attrs *attrs){	dma_addr_t dev_addr = virt_to_bus(ptr);	void *map;	BUG_ON(dir == DMA_NONE);	/*	 * 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_bus(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;}EXPORT_SYMBOL(swiotlb_map_single_attrs);dma_addr_tswiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir){	return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);}/* * 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_attrs(struct device *hwdev, dma_addr_t dev_addr,			   size_t size, int dir, struct dma_attrs *attrs){	char *dma_addr = bus_to_virt(dev_addr);	BUG_ON(dir == DMA_NONE);	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)		unmap_single(hwdev, dma_addr, size, dir);	else if (dir == DMA_FROM_DEVICE)		dma_mark_clean(dma_addr, size);}EXPORT_SYMBOL(swiotlb_unmap_single_attrs);voidswiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,		     int dir){	return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);}/* * 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 dma mapping, you must * call this function before doing so.  At the next point you give the dma * address back to the card, you must first perform a * swiotlb_dma_sync_for_device, and then the device again owns the buffer */static voidswiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,		    size_t size, int dir, int target){	char *dma_addr = bus_to_virt(dev_addr);	BUG_ON(dir == DMA_NONE);	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)		sync_single(hwdev, dma_addr, size, dir, target);	else if (dir == DMA_FROM_DEVICE)		dma_mark_clean(dma_addr, size);}voidswiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,			    size_t size, int dir){	swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);}voidswiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,			       size_t size, int dir){	swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);}/* * Same as above, but for a sub-range of the mapping. */static voidswiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,			  unsigned long offset, size_t size,			  int dir, int target){	char *dma_addr = bus_to_virt(dev_addr) + offset;	BUG_ON(dir == DMA_NONE);	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)		sync_single(hwdev, dma_addr, size, dir, target);	else if (dir == DMA_FROM_DEVICE)		dma_mark_clean(dma_addr, size);}voidswiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,				  unsigned long offset, size_t size, int dir){	swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,				  SYNC_FOR_CPU);}voidswiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,				     unsigned long offset, size_t size, int dir){	swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,				  SYNC_FOR_DEVICE);}void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,			    struct dma_attrs *);/* * 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_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,		     int dir, struct dma_attrs *attrs){	struct scatterlist *sg;	void *addr;	dma_addr_t dev_addr;	int i;	BUG_ON(dir == DMA_NONE);	for_each_sg(sgl, sg, nelems, i) {		addr = SG_ENT_VIRT_ADDRESS(sg);		dev_addr = virt_to_bus(addr);		if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {			void *map = map_single(hwdev, addr, sg->length, dir);			if (!map) {				/* Don't panic here, we expect map_sg users				   to do proper error handling. */				swiotlb_full(hwdev, sg->length, dir, 0);				swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,						       attrs);				sgl[0].dma_length = 0;				return 0;			}			sg->dma_address = virt_to_bus(map);		} else			sg->dma_address = dev_addr;		sg->dma_length = sg->length;	}	return nelems;}EXPORT_SYMBOL(swiotlb_map_sg_attrs);intswiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,	       int dir){	return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);}/* * 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_attrs(struct device *hwdev, struct scatterlist *sgl,		       int nelems, int dir, struct dma_attrs *attrs){	struct scatterlist *sg;	int i;	BUG_ON(dir == DMA_NONE);	for_each_sg(sgl, sg, nelems, i) {		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))			unmap_single(hwdev, bus_to_virt(sg->dma_address),				     sg->dma_length, dir);		else if (dir == DMA_FROM_DEVICE)			dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);	}}EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);voidswiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,		 int dir){	return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);}/* * 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. */static voidswiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,		int nelems, int dir, int target){	struct scatterlist *sg;	int i;	BUG_ON(dir == DMA_NONE);	for_each_sg(sgl, sg, nelems, i) {		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))			sync_single(hwdev, bus_to_virt(sg->dma_address),				    sg->dma_length, dir, target);		else if (dir == DMA_FROM_DEVICE)			dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);	}}voidswiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,			int nelems, int dir){	swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);}voidswiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,			   int nelems, int dir){	swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);}intswiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr){	return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));}/* * Return whether the given device DMA address mask can be supported * properly.  For example, if your device can only drive the low 24-bits * during bus mastering, then you would pass 0x00ffffff as the mask to * this function. */intswiotlb_dma_supported(struct device *hwdev, u64 mask){	return virt_to_bus(io_tlb_end - 1) <= mask;}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_GPL(swiotlb_sync_single_range_for_cpu);EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_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 + =
减小字号Ctrl + -
显示快捷键?