2 * Dynamic DMA mapping support.
4 * This implementation is a fallback for platforms that do not support
5 * I/O TLBs (aka DMA address translation hardware).
6 * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
7 * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
8 * Copyright (C) 2000, 2003 Hewlett-Packard Co
9 * David Mosberger-Tang <davidm@hpl.hp.com>
11 * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API.
12 * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
13 * unnecessary i-cache flushing.
14 * 04/07/.. ak Better overflow handling. Assorted fixes.
15 * 05/09/10 linville Add support for syncing ranges, support syncing for
16 * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
19 #include <linux/cache.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/module.h>
23 #include <linux/spinlock.h>
24 #include <linux/swiotlb.h>
25 #include <linux/string.h>
26 #include <linux/swiotlb.h>
27 #include <linux/types.h>
28 #include <linux/ctype.h>
29 #include <linux/highmem.h>
33 #include <asm/scatterlist.h>
35 #include <linux/init.h>
36 #include <linux/bootmem.h>
37 #include <linux/iommu-helper.h>
39 #define OFFSET(val,align) ((unsigned long) \
40 ( (val) & ( (align) - 1)))
42 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
45 * Minimum IO TLB size to bother booting with. Systems with mainly
46 * 64bit capable cards will only lightly use the swiotlb. If we can't
47 * allocate a contiguous 1MB, we're probably in trouble anyway.
49 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
52 * Enumeration for sync targets
54 enum dma_sync_target {
62 * Used to do a quick range check in swiotlb_unmap_single and
63 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
66 static char *io_tlb_start, *io_tlb_end;
69 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
70 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
72 static unsigned long io_tlb_nslabs;
75 * When the IOMMU overflows we return a fallback buffer. This sets the size.
77 static unsigned long io_tlb_overflow = 32*1024;
79 void *io_tlb_overflow_buffer;
82 * This is a free list describing the number of free entries available from
85 static unsigned int *io_tlb_list;
86 static unsigned int io_tlb_index;
89 * We need to save away the original address corresponding to a mapped entry
90 * for the sync operations.
92 static phys_addr_t *io_tlb_orig_addr;
95 * Protect the above data structures in the map and unmap calls
97 static DEFINE_SPINLOCK(io_tlb_lock);
100 setup_io_tlb_npages(char *str)
103 io_tlb_nslabs = simple_strtoul(str, &str, 0);
104 /* avoid tail segment of size < IO_TLB_SEGSIZE */
105 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
109 if (!strcmp(str, "force"))
113 __setup("swiotlb=", setup_io_tlb_npages);
114 /* make io_tlb_overflow tunable too? */
116 void * __weak swiotlb_alloc_boot(size_t size, unsigned long nslabs)
118 return alloc_bootmem_low_pages(size);
121 void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs)
123 return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
126 dma_addr_t __weak swiotlb_phys_to_bus(struct device *hwdev, phys_addr_t paddr)
131 phys_addr_t __weak swiotlb_bus_to_phys(dma_addr_t baddr)
136 static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
137 volatile void *address)
139 return swiotlb_phys_to_bus(hwdev, virt_to_phys(address));
142 static void *swiotlb_bus_to_virt(dma_addr_t address)
144 return phys_to_virt(swiotlb_bus_to_phys(address));
147 int __weak swiotlb_arch_range_needs_mapping(void *ptr, size_t size)
152 static dma_addr_t swiotlb_sg_to_bus(struct device *hwdev, struct scatterlist *sg)
154 return swiotlb_phys_to_bus(hwdev, page_to_phys(sg_page(sg)) + sg->offset);
157 static void swiotlb_print_info(unsigned long bytes)
159 phys_addr_t pstart, pend;
161 pstart = virt_to_phys(io_tlb_start);
162 pend = virt_to_phys(io_tlb_end);
164 printk(KERN_INFO "Placing %luMB software IO TLB between %p - %p\n",
165 bytes >> 20, io_tlb_start, io_tlb_end);
166 printk(KERN_INFO "software IO TLB at phys %#llx - %#llx\n",
167 (unsigned long long)pstart,
168 (unsigned long long)pend);
172 * Statically reserve bounce buffer space and initialize bounce buffer data
173 * structures for the software IO TLB used to implement the DMA API.
176 swiotlb_init_with_default_size(size_t default_size)
178 unsigned long i, bytes;
180 if (!io_tlb_nslabs) {
181 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
182 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
185 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
188 * Get IO TLB memory from the low pages
190 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
192 panic("Cannot allocate SWIOTLB buffer");
193 io_tlb_end = io_tlb_start + bytes;
196 * Allocate and initialize the free list array. This array is used
197 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
198 * between io_tlb_start and io_tlb_end.
200 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
201 for (i = 0; i < io_tlb_nslabs; i++)
202 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
204 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(phys_addr_t));
207 * Get the overflow emergency buffer
209 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
210 if (!io_tlb_overflow_buffer)
211 panic("Cannot allocate SWIOTLB overflow buffer!\n");
213 swiotlb_print_info(bytes);
219 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
223 * Systems with larger DMA zones (those that don't support ISA) can
224 * initialize the swiotlb later using the slab allocator if needed.
225 * This should be just like above, but with some error catching.
228 swiotlb_late_init_with_default_size(size_t default_size)
230 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
233 if (!io_tlb_nslabs) {
234 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
235 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
239 * Get IO TLB memory from the low pages
241 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
242 io_tlb_nslabs = SLABS_PER_PAGE << order;
243 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
245 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
246 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
255 if (order != get_order(bytes)) {
256 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
257 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
258 io_tlb_nslabs = SLABS_PER_PAGE << order;
259 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
261 io_tlb_end = io_tlb_start + bytes;
262 memset(io_tlb_start, 0, bytes);
265 * Allocate and initialize the free list array. This array is used
266 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
267 * between io_tlb_start and io_tlb_end.
269 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
270 get_order(io_tlb_nslabs * sizeof(int)));
274 for (i = 0; i < io_tlb_nslabs; i++)
275 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
278 io_tlb_orig_addr = (phys_addr_t *)
279 __get_free_pages(GFP_KERNEL,
280 get_order(io_tlb_nslabs *
281 sizeof(phys_addr_t)));
282 if (!io_tlb_orig_addr)
285 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(phys_addr_t));
288 * Get the overflow emergency buffer
290 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
291 get_order(io_tlb_overflow));
292 if (!io_tlb_overflow_buffer)
295 swiotlb_print_info(bytes);
300 free_pages((unsigned long)io_tlb_orig_addr,
301 get_order(io_tlb_nslabs * sizeof(phys_addr_t)));
302 io_tlb_orig_addr = NULL;
304 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
309 free_pages((unsigned long)io_tlb_start, order);
312 io_tlb_nslabs = req_nslabs;
317 address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
319 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
322 static inline int range_needs_mapping(void *ptr, size_t size)
324 return swiotlb_force || swiotlb_arch_range_needs_mapping(ptr, size);
327 static int is_swiotlb_buffer(char *addr)
329 return addr >= io_tlb_start && addr < io_tlb_end;
333 * Allocates bounce buffer and returns its kernel virtual address.
336 map_single(struct device *hwdev, phys_addr_t phys, size_t size, int dir)
340 unsigned int nslots, stride, index, wrap;
342 unsigned long start_dma_addr;
344 unsigned long offset_slots;
345 unsigned long max_slots;
347 mask = dma_get_seg_boundary(hwdev);
348 start_dma_addr = swiotlb_virt_to_bus(hwdev, io_tlb_start) & mask;
350 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
353 * Carefully handle integer overflow which can occur when mask == ~0UL.
356 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
357 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
360 * For mappings greater than a page, we limit the stride (and
361 * hence alignment) to a page size.
363 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
364 if (size > PAGE_SIZE)
365 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
372 * Find suitable number of IO TLB entries size that will fit this
373 * request and allocate a buffer from that IO TLB pool.
375 spin_lock_irqsave(&io_tlb_lock, flags);
376 index = ALIGN(io_tlb_index, stride);
377 if (index >= io_tlb_nslabs)
382 while (iommu_is_span_boundary(index, nslots, offset_slots,
385 if (index >= io_tlb_nslabs)
392 * If we find a slot that indicates we have 'nslots' number of
393 * contiguous buffers, we allocate the buffers from that slot
394 * and mark the entries as '0' indicating unavailable.
396 if (io_tlb_list[index] >= nslots) {
399 for (i = index; i < (int) (index + nslots); i++)
401 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
402 io_tlb_list[i] = ++count;
403 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
406 * Update the indices to avoid searching in the next
409 io_tlb_index = ((index + nslots) < io_tlb_nslabs
410 ? (index + nslots) : 0);
415 if (index >= io_tlb_nslabs)
417 } while (index != wrap);
420 spin_unlock_irqrestore(&io_tlb_lock, flags);
423 spin_unlock_irqrestore(&io_tlb_lock, flags);
426 * Save away the mapping from the original address to the DMA address.
427 * This is needed when we sync the memory. Then we sync the buffer if
430 for (i = 0; i < nslots; i++)
431 io_tlb_orig_addr[index+i] = phys + (i << IO_TLB_SHIFT);
432 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
433 memcpy(dma_addr, phys_to_virt(phys), size);
439 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
442 unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
445 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
446 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
447 phys_addr_t phys = io_tlb_orig_addr[index];
450 * First, sync the memory before unmapping the entry
452 if (phys && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
454 * bounce... copy the data back into the original buffer * and
455 * delete the bounce buffer.
457 memcpy(phys_to_virt(phys), dma_addr, size);
460 * Return the buffer to the free list by setting the corresponding
461 * entries to indicate the number of contigous entries available.
462 * While returning the entries to the free list, we merge the entries
463 * with slots below and above the pool being returned.
465 spin_lock_irqsave(&io_tlb_lock, flags);
467 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
468 io_tlb_list[index + nslots] : 0);
470 * Step 1: return the slots to the free list, merging the
471 * slots with superceeding slots
473 for (i = index + nslots - 1; i >= index; i--)
474 io_tlb_list[i] = ++count;
476 * Step 2: merge the returned slots with the preceding slots,
477 * if available (non zero)
479 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
480 io_tlb_list[i] = ++count;
482 spin_unlock_irqrestore(&io_tlb_lock, flags);
486 sync_single(struct device *hwdev, char *dma_addr, size_t size,
489 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
490 phys_addr_t phys = io_tlb_orig_addr[index];
492 phys += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
496 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
497 memcpy(phys_to_virt(phys), dma_addr, size);
499 BUG_ON(dir != DMA_TO_DEVICE);
501 case SYNC_FOR_DEVICE:
502 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
503 memcpy(dma_addr, phys_to_virt(phys), size);
505 BUG_ON(dir != DMA_FROM_DEVICE);
513 swiotlb_alloc_coherent(struct device *hwdev, size_t size,
514 dma_addr_t *dma_handle, gfp_t flags)
518 int order = get_order(size);
519 u64 dma_mask = DMA_32BIT_MASK;
521 if (hwdev && hwdev->coherent_dma_mask)
522 dma_mask = hwdev->coherent_dma_mask;
524 ret = (void *)__get_free_pages(flags, order);
526 !is_buffer_dma_capable(dma_mask, swiotlb_virt_to_bus(hwdev, ret),
529 * The allocated memory isn't reachable by the device.
530 * Fall back on swiotlb_map_single().
532 free_pages((unsigned long) ret, order);
537 * We are either out of memory or the device can't DMA
538 * to GFP_DMA memory; fall back on
539 * swiotlb_map_single(), which will grab memory from
540 * the lowest available address range.
542 ret = map_single(hwdev, 0, size, DMA_FROM_DEVICE);
547 memset(ret, 0, size);
548 dev_addr = swiotlb_virt_to_bus(hwdev, ret);
550 /* Confirm address can be DMA'd by device */
551 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
552 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
553 (unsigned long long)dma_mask,
554 (unsigned long long)dev_addr);
556 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
557 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
560 *dma_handle = dev_addr;
565 swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
566 dma_addr_t dma_handle)
568 WARN_ON(irqs_disabled());
569 if (!is_swiotlb_buffer(vaddr))
570 free_pages((unsigned long) vaddr, get_order(size));
572 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
573 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
577 swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
580 * Ran out of IOMMU space for this operation. This is very bad.
581 * Unfortunately the drivers cannot handle this operation properly.
582 * unless they check for dma_mapping_error (most don't)
583 * When the mapping is small enough return a static buffer to limit
584 * the damage, or panic when the transfer is too big.
586 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
587 "device %s\n", size, dev ? dev->bus_id : "?");
589 if (size > io_tlb_overflow && do_panic) {
590 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
591 panic("DMA: Memory would be corrupted\n");
592 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
593 panic("DMA: Random memory would be DMAed\n");
598 * Map a single buffer of the indicated size for DMA in streaming mode. The
599 * physical address to use is returned.
601 * Once the device is given the dma address, the device owns this memory until
602 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
605 swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
606 int dir, struct dma_attrs *attrs)
608 dma_addr_t dev_addr = swiotlb_virt_to_bus(hwdev, ptr);
611 BUG_ON(dir == DMA_NONE);
613 * If the pointer passed in happens to be in the device's DMA window,
614 * we can safely return the device addr and not worry about bounce
617 if (!address_needs_mapping(hwdev, dev_addr, size) &&
618 !range_needs_mapping(ptr, size))
622 * Oh well, have to allocate and map a bounce buffer.
624 map = map_single(hwdev, virt_to_phys(ptr), size, dir);
626 swiotlb_full(hwdev, size, dir, 1);
627 map = io_tlb_overflow_buffer;
630 dev_addr = swiotlb_virt_to_bus(hwdev, map);
633 * Ensure that the address returned is DMA'ble
635 if (address_needs_mapping(hwdev, dev_addr, size))
636 panic("map_single: bounce buffer is not DMA'ble");
640 EXPORT_SYMBOL(swiotlb_map_single_attrs);
643 swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
645 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
649 * Unmap a single streaming mode DMA translation. The dma_addr and size must
650 * match what was provided for in a previous swiotlb_map_single call. All
651 * other usages are undefined.
653 * After this call, reads by the cpu to the buffer are guaranteed to see
654 * whatever the device wrote there.
657 swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
658 size_t size, int dir, struct dma_attrs *attrs)
660 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
662 BUG_ON(dir == DMA_NONE);
663 if (is_swiotlb_buffer(dma_addr))
664 unmap_single(hwdev, dma_addr, size, dir);
665 else if (dir == DMA_FROM_DEVICE)
666 dma_mark_clean(dma_addr, size);
668 EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
671 swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
674 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
677 * Make physical memory consistent for a single streaming mode DMA translation
680 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
681 * using the cpu, yet do not wish to teardown the dma mapping, you must
682 * call this function before doing so. At the next point you give the dma
683 * address back to the card, you must first perform a
684 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
687 swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
688 size_t size, int dir, int target)
690 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
692 BUG_ON(dir == DMA_NONE);
693 if (is_swiotlb_buffer(dma_addr))
694 sync_single(hwdev, dma_addr, size, dir, target);
695 else if (dir == DMA_FROM_DEVICE)
696 dma_mark_clean(dma_addr, size);
700 swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
701 size_t size, int dir)
703 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
707 swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
708 size_t size, int dir)
710 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
714 * Same as above, but for a sub-range of the mapping.
717 swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
718 unsigned long offset, size_t size,
721 char *dma_addr = swiotlb_bus_to_virt(dev_addr) + offset;
723 BUG_ON(dir == DMA_NONE);
724 if (is_swiotlb_buffer(dma_addr))
725 sync_single(hwdev, dma_addr, size, dir, target);
726 else if (dir == DMA_FROM_DEVICE)
727 dma_mark_clean(dma_addr, size);
731 swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
732 unsigned long offset, size_t size, int dir)
734 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
739 swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
740 unsigned long offset, size_t size, int dir)
742 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
746 void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
749 * Map a set of buffers described by scatterlist in streaming mode for DMA.
750 * This is the scatter-gather version of the above swiotlb_map_single
751 * interface. Here the scatter gather list elements are each tagged with the
752 * appropriate dma address and length. They are obtained via
753 * sg_dma_{address,length}(SG).
755 * NOTE: An implementation may be able to use a smaller number of
756 * DMA address/length pairs than there are SG table elements.
757 * (for example via virtual mapping capabilities)
758 * The routine returns the number of addr/length pairs actually
759 * used, at most nents.
761 * Device ownership issues as mentioned above for swiotlb_map_single are the
765 swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
766 int dir, struct dma_attrs *attrs)
768 struct scatterlist *sg;
771 BUG_ON(dir == DMA_NONE);
773 for_each_sg(sgl, sg, nelems, i) {
774 void *addr = sg_virt(sg);
775 dma_addr_t dev_addr = swiotlb_virt_to_bus(hwdev, addr);
777 if (range_needs_mapping(addr, sg->length) ||
778 address_needs_mapping(hwdev, dev_addr, sg->length)) {
779 void *map = map_single(hwdev, sg_phys(sg),
782 /* Don't panic here, we expect map_sg users
783 to do proper error handling. */
784 swiotlb_full(hwdev, sg->length, dir, 0);
785 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
787 sgl[0].dma_length = 0;
790 sg->dma_address = swiotlb_virt_to_bus(hwdev, map);
792 sg->dma_address = dev_addr;
793 sg->dma_length = sg->length;
797 EXPORT_SYMBOL(swiotlb_map_sg_attrs);
800 swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
803 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
807 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
808 * concerning calls here are the same as for swiotlb_unmap_single() above.
811 swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
812 int nelems, int dir, struct dma_attrs *attrs)
814 struct scatterlist *sg;
817 BUG_ON(dir == DMA_NONE);
819 for_each_sg(sgl, sg, nelems, i) {
820 if (sg->dma_address != swiotlb_sg_to_bus(hwdev, sg))
821 unmap_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
822 sg->dma_length, dir);
823 else if (dir == DMA_FROM_DEVICE)
824 dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length);
827 EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
830 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
833 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
837 * Make physical memory consistent for a set of streaming mode DMA translations
840 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
844 swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
845 int nelems, int dir, int target)
847 struct scatterlist *sg;
850 BUG_ON(dir == DMA_NONE);
852 for_each_sg(sgl, sg, nelems, i) {
853 if (sg->dma_address != swiotlb_sg_to_bus(hwdev, sg))
854 sync_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
855 sg->dma_length, dir, target);
856 else if (dir == DMA_FROM_DEVICE)
857 dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length);
862 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
865 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
869 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
872 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
876 swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
878 return (dma_addr == swiotlb_virt_to_bus(hwdev, io_tlb_overflow_buffer));
882 * Return whether the given device DMA address mask can be supported
883 * properly. For example, if your device can only drive the low 24-bits
884 * during bus mastering, then you would pass 0x00ffffff as the mask to
888 swiotlb_dma_supported(struct device *hwdev, u64 mask)
890 return swiotlb_virt_to_bus(hwdev, io_tlb_end - 1) <= mask;
893 EXPORT_SYMBOL(swiotlb_map_single);
894 EXPORT_SYMBOL(swiotlb_unmap_single);
895 EXPORT_SYMBOL(swiotlb_map_sg);
896 EXPORT_SYMBOL(swiotlb_unmap_sg);
897 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
898 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
899 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
900 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
901 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
902 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
903 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
904 EXPORT_SYMBOL(swiotlb_alloc_coherent);
905 EXPORT_SYMBOL(swiotlb_free_coherent);
906 EXPORT_SYMBOL(swiotlb_dma_supported);