swiotlb.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 593 行 · 第 1/2 页

C
593
字号
void *swiotlb_alloc_coherent (struct device *hwdev, size_t size, dma_addr_t *dma_handle, int flags){	unsigned long dev_addr;	void *ret;	/* XXX fix me: the DMA API should pass us an explicit DMA mask instead: */	flags |= GFP_DMA;	ret = (void *)__get_free_pages(flags, get_order(size));	if (!ret)		return NULL;	memset(ret, 0, size);	dev_addr = virt_to_phys(ret);	if (address_needs_mapping(hwdev,dev_addr))		panic("swiotlb_alloc_consistent: 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){	free_pages((unsigned long) vaddr, get_order(size));}static void swiotlb_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();	/*	 * Check if the PCI device can DMA to ptr... if so, just return ptr	 */	if (!address_needs_mapping(hwdev, dev_addr) && !swiotlb_force) 		/*		 * Device is bit capable of DMA'ing to the buffer... just return the PCI		 * address of ptr		 */		return dev_addr;	/*	 * get 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 pci_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 1;}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 + =
减小字号Ctrl + -
显示快捷键?