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 struct swiotlb_phys_addr
{
98 * Protect the above data structures in the map and unmap calls
100 static DEFINE_SPINLOCK(io_tlb_lock
);
103 setup_io_tlb_npages(char *str
)
106 io_tlb_nslabs
= simple_strtoul(str
, &str
, 0);
107 /* avoid tail segment of size < IO_TLB_SEGSIZE */
108 io_tlb_nslabs
= ALIGN(io_tlb_nslabs
, IO_TLB_SEGSIZE
);
112 if (!strcmp(str
, "force"))
116 __setup("swiotlb=", setup_io_tlb_npages
);
117 /* make io_tlb_overflow tunable too? */
119 void * __weak
swiotlb_alloc_boot(size_t size
, unsigned long nslabs
)
121 return alloc_bootmem_low_pages(size
);
124 void * __weak
swiotlb_alloc(unsigned order
, unsigned long nslabs
)
126 return (void *)__get_free_pages(GFP_DMA
| __GFP_NOWARN
, order
);
129 dma_addr_t __weak
swiotlb_phys_to_bus(struct device
*hwdev
, phys_addr_t paddr
)
134 phys_addr_t __weak
swiotlb_bus_to_phys(dma_addr_t baddr
)
139 static dma_addr_t
swiotlb_virt_to_bus(struct device
*hwdev
,
140 volatile void *address
)
142 return swiotlb_phys_to_bus(hwdev
, virt_to_phys(address
));
145 static void *swiotlb_bus_to_virt(dma_addr_t address
)
147 return phys_to_virt(swiotlb_bus_to_phys(address
));
150 int __weak
swiotlb_arch_range_needs_mapping(void *ptr
, size_t size
)
155 static dma_addr_t
swiotlb_sg_to_bus(struct device
*hwdev
, struct scatterlist
*sg
)
157 return swiotlb_phys_to_bus(hwdev
, page_to_phys(sg_page(sg
)) + sg
->offset
);
160 static void swiotlb_print_info(unsigned long bytes
)
162 phys_addr_t pstart
, pend
;
164 pstart
= virt_to_phys(io_tlb_start
);
165 pend
= virt_to_phys(io_tlb_end
);
167 printk(KERN_INFO
"Placing %luMB software IO TLB between %p - %p\n",
168 bytes
>> 20, io_tlb_start
, io_tlb_end
);
169 printk(KERN_INFO
"software IO TLB at phys %#llx - %#llx\n",
170 (unsigned long long)pstart
,
171 (unsigned long long)pend
);
175 * Statically reserve bounce buffer space and initialize bounce buffer data
176 * structures for the software IO TLB used to implement the DMA API.
179 swiotlb_init_with_default_size(size_t default_size
)
181 unsigned long i
, bytes
;
183 if (!io_tlb_nslabs
) {
184 io_tlb_nslabs
= (default_size
>> IO_TLB_SHIFT
);
185 io_tlb_nslabs
= ALIGN(io_tlb_nslabs
, IO_TLB_SEGSIZE
);
188 bytes
= io_tlb_nslabs
<< IO_TLB_SHIFT
;
191 * Get IO TLB memory from the low pages
193 io_tlb_start
= swiotlb_alloc_boot(bytes
, io_tlb_nslabs
);
195 panic("Cannot allocate SWIOTLB buffer");
196 io_tlb_end
= io_tlb_start
+ bytes
;
199 * Allocate and initialize the free list array. This array is used
200 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
201 * between io_tlb_start and io_tlb_end.
203 io_tlb_list
= alloc_bootmem(io_tlb_nslabs
* sizeof(int));
204 for (i
= 0; i
< io_tlb_nslabs
; i
++)
205 io_tlb_list
[i
] = IO_TLB_SEGSIZE
- OFFSET(i
, IO_TLB_SEGSIZE
);
207 io_tlb_orig_addr
= alloc_bootmem(io_tlb_nslabs
* sizeof(struct swiotlb_phys_addr
));
210 * Get the overflow emergency buffer
212 io_tlb_overflow_buffer
= alloc_bootmem_low(io_tlb_overflow
);
213 if (!io_tlb_overflow_buffer
)
214 panic("Cannot allocate SWIOTLB overflow buffer!\n");
216 swiotlb_print_info(bytes
);
222 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
226 * Systems with larger DMA zones (those that don't support ISA) can
227 * initialize the swiotlb later using the slab allocator if needed.
228 * This should be just like above, but with some error catching.
231 swiotlb_late_init_with_default_size(size_t default_size
)
233 unsigned long i
, bytes
, req_nslabs
= io_tlb_nslabs
;
236 if (!io_tlb_nslabs
) {
237 io_tlb_nslabs
= (default_size
>> IO_TLB_SHIFT
);
238 io_tlb_nslabs
= ALIGN(io_tlb_nslabs
, IO_TLB_SEGSIZE
);
242 * Get IO TLB memory from the low pages
244 order
= get_order(io_tlb_nslabs
<< IO_TLB_SHIFT
);
245 io_tlb_nslabs
= SLABS_PER_PAGE
<< order
;
246 bytes
= io_tlb_nslabs
<< IO_TLB_SHIFT
;
248 while ((SLABS_PER_PAGE
<< order
) > IO_TLB_MIN_SLABS
) {
249 io_tlb_start
= swiotlb_alloc(order
, io_tlb_nslabs
);
258 if (order
!= get_order(bytes
)) {
259 printk(KERN_WARNING
"Warning: only able to allocate %ld MB "
260 "for software IO TLB\n", (PAGE_SIZE
<< order
) >> 20);
261 io_tlb_nslabs
= SLABS_PER_PAGE
<< order
;
262 bytes
= io_tlb_nslabs
<< IO_TLB_SHIFT
;
264 io_tlb_end
= io_tlb_start
+ bytes
;
265 memset(io_tlb_start
, 0, bytes
);
268 * Allocate and initialize the free list array. This array is used
269 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
270 * between io_tlb_start and io_tlb_end.
272 io_tlb_list
= (unsigned int *)__get_free_pages(GFP_KERNEL
,
273 get_order(io_tlb_nslabs
* sizeof(int)));
277 for (i
= 0; i
< io_tlb_nslabs
; i
++)
278 io_tlb_list
[i
] = IO_TLB_SEGSIZE
- OFFSET(i
, IO_TLB_SEGSIZE
);
281 io_tlb_orig_addr
= (struct swiotlb_phys_addr
*)__get_free_pages(GFP_KERNEL
,
282 get_order(io_tlb_nslabs
* sizeof(struct swiotlb_phys_addr
)));
283 if (!io_tlb_orig_addr
)
286 memset(io_tlb_orig_addr
, 0, io_tlb_nslabs
* sizeof(struct swiotlb_phys_addr
));
289 * Get the overflow emergency buffer
291 io_tlb_overflow_buffer
= (void *)__get_free_pages(GFP_DMA
,
292 get_order(io_tlb_overflow
));
293 if (!io_tlb_overflow_buffer
)
296 swiotlb_print_info(bytes
);
301 free_pages((unsigned long)io_tlb_orig_addr
, get_order(io_tlb_nslabs
*
303 io_tlb_orig_addr
= NULL
;
305 free_pages((unsigned long)io_tlb_list
, get_order(io_tlb_nslabs
*
310 free_pages((unsigned long)io_tlb_start
, order
);
313 io_tlb_nslabs
= req_nslabs
;
318 address_needs_mapping(struct device
*hwdev
, dma_addr_t addr
, size_t size
)
320 return !is_buffer_dma_capable(dma_get_mask(hwdev
), addr
, size
);
323 static inline int range_needs_mapping(void *ptr
, size_t size
)
325 return swiotlb_force
|| swiotlb_arch_range_needs_mapping(ptr
, size
);
328 static int is_swiotlb_buffer(char *addr
)
330 return addr
>= io_tlb_start
&& addr
< io_tlb_end
;
333 static struct swiotlb_phys_addr
swiotlb_bus_to_phys_addr(char *dma_addr
)
335 int index
= (dma_addr
- io_tlb_start
) >> IO_TLB_SHIFT
;
336 struct swiotlb_phys_addr buffer
= io_tlb_orig_addr
[index
];
337 buffer
.offset
+= (long)dma_addr
& ((1 << IO_TLB_SHIFT
) - 1);
338 buffer
.page
+= buffer
.offset
>> PAGE_SHIFT
;
339 buffer
.offset
&= PAGE_SIZE
- 1;
344 __sync_single(struct swiotlb_phys_addr buffer
, char *dma_addr
, size_t size
, int dir
)
346 if (PageHighMem(buffer
.page
)) {
348 char *dev
, *host
, *kmp
;
355 if ((bytes
+ buffer
.offset
) > PAGE_SIZE
)
356 bytes
= PAGE_SIZE
- buffer
.offset
;
357 local_irq_save(flags
); /* protects KM_BOUNCE_READ */
358 kmp
= kmap_atomic(buffer
.page
, KM_BOUNCE_READ
);
359 dev
= dma_addr
+ size
- len
;
360 host
= kmp
+ buffer
.offset
;
361 if (dir
== DMA_FROM_DEVICE
)
362 memcpy(host
, dev
, bytes
);
364 memcpy(dev
, host
, bytes
);
365 kunmap_atomic(kmp
, KM_BOUNCE_READ
);
366 local_irq_restore(flags
);
372 void *v
= page_address(buffer
.page
) + buffer
.offset
;
374 if (dir
== DMA_TO_DEVICE
)
375 memcpy(dma_addr
, v
, size
);
377 memcpy(v
, dma_addr
, size
);
382 * Allocates bounce buffer and returns its kernel virtual address.
385 map_single(struct device
*hwdev
, struct swiotlb_phys_addr buffer
, size_t size
, int dir
)
389 unsigned int nslots
, stride
, index
, wrap
;
391 unsigned long start_dma_addr
;
393 unsigned long offset_slots
;
394 unsigned long max_slots
;
395 struct swiotlb_phys_addr slot_buf
;
397 mask
= dma_get_seg_boundary(hwdev
);
398 start_dma_addr
= swiotlb_virt_to_bus(hwdev
, io_tlb_start
) & mask
;
400 offset_slots
= ALIGN(start_dma_addr
, 1 << IO_TLB_SHIFT
) >> IO_TLB_SHIFT
;
403 * Carefully handle integer overflow which can occur when mask == ~0UL.
406 ? ALIGN(mask
+ 1, 1 << IO_TLB_SHIFT
) >> IO_TLB_SHIFT
407 : 1UL << (BITS_PER_LONG
- IO_TLB_SHIFT
);
410 * For mappings greater than a page, we limit the stride (and
411 * hence alignment) to a page size.
413 nslots
= ALIGN(size
, 1 << IO_TLB_SHIFT
) >> IO_TLB_SHIFT
;
414 if (size
> PAGE_SIZE
)
415 stride
= (1 << (PAGE_SHIFT
- IO_TLB_SHIFT
));
422 * Find suitable number of IO TLB entries size that will fit this
423 * request and allocate a buffer from that IO TLB pool.
425 spin_lock_irqsave(&io_tlb_lock
, flags
);
426 index
= ALIGN(io_tlb_index
, stride
);
427 if (index
>= io_tlb_nslabs
)
432 while (iommu_is_span_boundary(index
, nslots
, offset_slots
,
435 if (index
>= io_tlb_nslabs
)
442 * If we find a slot that indicates we have 'nslots' number of
443 * contiguous buffers, we allocate the buffers from that slot
444 * and mark the entries as '0' indicating unavailable.
446 if (io_tlb_list
[index
] >= nslots
) {
449 for (i
= index
; i
< (int) (index
+ nslots
); i
++)
451 for (i
= index
- 1; (OFFSET(i
, IO_TLB_SEGSIZE
) != IO_TLB_SEGSIZE
- 1) && io_tlb_list
[i
]; i
--)
452 io_tlb_list
[i
] = ++count
;
453 dma_addr
= io_tlb_start
+ (index
<< IO_TLB_SHIFT
);
456 * Update the indices to avoid searching in the next
459 io_tlb_index
= ((index
+ nslots
) < io_tlb_nslabs
460 ? (index
+ nslots
) : 0);
465 if (index
>= io_tlb_nslabs
)
467 } while (index
!= wrap
);
470 spin_unlock_irqrestore(&io_tlb_lock
, flags
);
473 spin_unlock_irqrestore(&io_tlb_lock
, flags
);
476 * Save away the mapping from the original address to the DMA address.
477 * This is needed when we sync the memory. Then we sync the buffer if
481 for (i
= 0; i
< nslots
; i
++) {
482 slot_buf
.page
+= slot_buf
.offset
>> PAGE_SHIFT
;
483 slot_buf
.offset
&= PAGE_SIZE
- 1;
484 io_tlb_orig_addr
[index
+i
] = slot_buf
;
485 slot_buf
.offset
+= 1 << IO_TLB_SHIFT
;
487 if (dir
== DMA_TO_DEVICE
|| dir
== DMA_BIDIRECTIONAL
)
488 __sync_single(buffer
, dma_addr
, size
, DMA_TO_DEVICE
);
494 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
497 unmap_single(struct device
*hwdev
, char *dma_addr
, size_t size
, int dir
)
500 int i
, count
, nslots
= ALIGN(size
, 1 << IO_TLB_SHIFT
) >> IO_TLB_SHIFT
;
501 int index
= (dma_addr
- io_tlb_start
) >> IO_TLB_SHIFT
;
502 struct swiotlb_phys_addr buffer
= swiotlb_bus_to_phys_addr(dma_addr
);
505 * First, sync the memory before unmapping the entry
507 if ((dir
== DMA_FROM_DEVICE
) || (dir
== DMA_BIDIRECTIONAL
))
509 * bounce... copy the data back into the original buffer * and
510 * delete the bounce buffer.
512 __sync_single(buffer
, dma_addr
, size
, DMA_FROM_DEVICE
);
515 * Return the buffer to the free list by setting the corresponding
516 * entries to indicate the number of contigous entries available.
517 * While returning the entries to the free list, we merge the entries
518 * with slots below and above the pool being returned.
520 spin_lock_irqsave(&io_tlb_lock
, flags
);
522 count
= ((index
+ nslots
) < ALIGN(index
+ 1, IO_TLB_SEGSIZE
) ?
523 io_tlb_list
[index
+ nslots
] : 0);
525 * Step 1: return the slots to the free list, merging the
526 * slots with superceeding slots
528 for (i
= index
+ nslots
- 1; i
>= index
; i
--)
529 io_tlb_list
[i
] = ++count
;
531 * Step 2: merge the returned slots with the preceding slots,
532 * if available (non zero)
534 for (i
= index
- 1; (OFFSET(i
, IO_TLB_SEGSIZE
) != IO_TLB_SEGSIZE
-1) && io_tlb_list
[i
]; i
--)
535 io_tlb_list
[i
] = ++count
;
537 spin_unlock_irqrestore(&io_tlb_lock
, flags
);
541 sync_single(struct device
*hwdev
, char *dma_addr
, size_t size
,
544 struct swiotlb_phys_addr buffer
= swiotlb_bus_to_phys_addr(dma_addr
);
548 if (likely(dir
== DMA_FROM_DEVICE
|| dir
== DMA_BIDIRECTIONAL
))
549 __sync_single(buffer
, dma_addr
, size
, DMA_FROM_DEVICE
);
551 BUG_ON(dir
!= DMA_TO_DEVICE
);
553 case SYNC_FOR_DEVICE
:
554 if (likely(dir
== DMA_TO_DEVICE
|| dir
== DMA_BIDIRECTIONAL
))
555 __sync_single(buffer
, dma_addr
, size
, DMA_TO_DEVICE
);
557 BUG_ON(dir
!= DMA_FROM_DEVICE
);
565 swiotlb_alloc_coherent(struct device
*hwdev
, size_t size
,
566 dma_addr_t
*dma_handle
, gfp_t flags
)
570 int order
= get_order(size
);
571 u64 dma_mask
= DMA_32BIT_MASK
;
573 if (hwdev
&& hwdev
->coherent_dma_mask
)
574 dma_mask
= hwdev
->coherent_dma_mask
;
576 ret
= (void *)__get_free_pages(flags
, order
);
578 !is_buffer_dma_capable(dma_mask
, swiotlb_virt_to_bus(hwdev
, ret
),
581 * The allocated memory isn't reachable by the device.
582 * Fall back on swiotlb_map_single().
584 free_pages((unsigned long) ret
, order
);
589 * We are either out of memory or the device can't DMA
590 * to GFP_DMA memory; fall back on
591 * swiotlb_map_single(), which will grab memory from
592 * the lowest available address range.
594 struct swiotlb_phys_addr buffer
;
595 buffer
.page
= virt_to_page(NULL
);
597 ret
= map_single(hwdev
, buffer
, size
, DMA_FROM_DEVICE
);
602 memset(ret
, 0, size
);
603 dev_addr
= swiotlb_virt_to_bus(hwdev
, ret
);
605 /* Confirm address can be DMA'd by device */
606 if (!is_buffer_dma_capable(dma_mask
, dev_addr
, size
)) {
607 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
608 (unsigned long long)dma_mask
,
609 (unsigned long long)dev_addr
);
611 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
612 unmap_single(hwdev
, ret
, size
, DMA_TO_DEVICE
);
615 *dma_handle
= dev_addr
;
620 swiotlb_free_coherent(struct device
*hwdev
, size_t size
, void *vaddr
,
621 dma_addr_t dma_handle
)
623 WARN_ON(irqs_disabled());
624 if (!is_swiotlb_buffer(vaddr
))
625 free_pages((unsigned long) vaddr
, get_order(size
));
627 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
628 unmap_single(hwdev
, vaddr
, size
, DMA_TO_DEVICE
);
632 swiotlb_full(struct device
*dev
, size_t size
, int dir
, int do_panic
)
635 * Ran out of IOMMU space for this operation. This is very bad.
636 * Unfortunately the drivers cannot handle this operation properly.
637 * unless they check for dma_mapping_error (most don't)
638 * When the mapping is small enough return a static buffer to limit
639 * the damage, or panic when the transfer is too big.
641 printk(KERN_ERR
"DMA: Out of SW-IOMMU space for %zu bytes at "
642 "device %s\n", size
, dev
? dev
->bus_id
: "?");
644 if (size
> io_tlb_overflow
&& do_panic
) {
645 if (dir
== DMA_FROM_DEVICE
|| dir
== DMA_BIDIRECTIONAL
)
646 panic("DMA: Memory would be corrupted\n");
647 if (dir
== DMA_TO_DEVICE
|| dir
== DMA_BIDIRECTIONAL
)
648 panic("DMA: Random memory would be DMAed\n");
653 * Map a single buffer of the indicated size for DMA in streaming mode. The
654 * physical address to use is returned.
656 * Once the device is given the dma address, the device owns this memory until
657 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
660 swiotlb_map_single_attrs(struct device
*hwdev
, void *ptr
, size_t size
,
661 int dir
, struct dma_attrs
*attrs
)
663 dma_addr_t dev_addr
= swiotlb_virt_to_bus(hwdev
, ptr
);
665 struct swiotlb_phys_addr buffer
;
667 BUG_ON(dir
== DMA_NONE
);
669 * If the pointer passed in happens to be in the device's DMA window,
670 * we can safely return the device addr and not worry about bounce
673 if (!address_needs_mapping(hwdev
, dev_addr
, size
) &&
674 !range_needs_mapping(ptr
, size
))
678 * Oh well, have to allocate and map a bounce buffer.
680 buffer
.page
= virt_to_page(ptr
);
681 buffer
.offset
= (unsigned long)ptr
& ~PAGE_MASK
;
682 map
= map_single(hwdev
, buffer
, size
, dir
);
684 swiotlb_full(hwdev
, size
, dir
, 1);
685 map
= io_tlb_overflow_buffer
;
688 dev_addr
= swiotlb_virt_to_bus(hwdev
, map
);
691 * Ensure that the address returned is DMA'ble
693 if (address_needs_mapping(hwdev
, dev_addr
, size
))
694 panic("map_single: bounce buffer is not DMA'ble");
698 EXPORT_SYMBOL(swiotlb_map_single_attrs
);
701 swiotlb_map_single(struct device
*hwdev
, void *ptr
, size_t size
, int dir
)
703 return swiotlb_map_single_attrs(hwdev
, ptr
, size
, dir
, NULL
);
707 * Unmap a single streaming mode DMA translation. The dma_addr and size must
708 * match what was provided for in a previous swiotlb_map_single call. All
709 * other usages are undefined.
711 * After this call, reads by the cpu to the buffer are guaranteed to see
712 * whatever the device wrote there.
715 swiotlb_unmap_single_attrs(struct device
*hwdev
, dma_addr_t dev_addr
,
716 size_t size
, int dir
, struct dma_attrs
*attrs
)
718 char *dma_addr
= swiotlb_bus_to_virt(dev_addr
);
720 BUG_ON(dir
== DMA_NONE
);
721 if (is_swiotlb_buffer(dma_addr
))
722 unmap_single(hwdev
, dma_addr
, size
, dir
);
723 else if (dir
== DMA_FROM_DEVICE
)
724 dma_mark_clean(dma_addr
, size
);
726 EXPORT_SYMBOL(swiotlb_unmap_single_attrs
);
729 swiotlb_unmap_single(struct device
*hwdev
, dma_addr_t dev_addr
, size_t size
,
732 return swiotlb_unmap_single_attrs(hwdev
, dev_addr
, size
, dir
, NULL
);
735 * Make physical memory consistent for a single streaming mode DMA translation
738 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
739 * using the cpu, yet do not wish to teardown the dma mapping, you must
740 * call this function before doing so. At the next point you give the dma
741 * address back to the card, you must first perform a
742 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
745 swiotlb_sync_single(struct device
*hwdev
, dma_addr_t dev_addr
,
746 size_t size
, int dir
, int target
)
748 char *dma_addr
= swiotlb_bus_to_virt(dev_addr
);
750 BUG_ON(dir
== DMA_NONE
);
751 if (is_swiotlb_buffer(dma_addr
))
752 sync_single(hwdev
, dma_addr
, size
, dir
, target
);
753 else if (dir
== DMA_FROM_DEVICE
)
754 dma_mark_clean(dma_addr
, size
);
758 swiotlb_sync_single_for_cpu(struct device
*hwdev
, dma_addr_t dev_addr
,
759 size_t size
, int dir
)
761 swiotlb_sync_single(hwdev
, dev_addr
, size
, dir
, SYNC_FOR_CPU
);
765 swiotlb_sync_single_for_device(struct device
*hwdev
, dma_addr_t dev_addr
,
766 size_t size
, int dir
)
768 swiotlb_sync_single(hwdev
, dev_addr
, size
, dir
, SYNC_FOR_DEVICE
);
772 * Same as above, but for a sub-range of the mapping.
775 swiotlb_sync_single_range(struct device
*hwdev
, dma_addr_t dev_addr
,
776 unsigned long offset
, size_t size
,
779 char *dma_addr
= swiotlb_bus_to_virt(dev_addr
) + offset
;
781 BUG_ON(dir
== DMA_NONE
);
782 if (is_swiotlb_buffer(dma_addr
))
783 sync_single(hwdev
, dma_addr
, size
, dir
, target
);
784 else if (dir
== DMA_FROM_DEVICE
)
785 dma_mark_clean(dma_addr
, size
);
789 swiotlb_sync_single_range_for_cpu(struct device
*hwdev
, dma_addr_t dev_addr
,
790 unsigned long offset
, size_t size
, int dir
)
792 swiotlb_sync_single_range(hwdev
, dev_addr
, offset
, size
, dir
,
797 swiotlb_sync_single_range_for_device(struct device
*hwdev
, dma_addr_t dev_addr
,
798 unsigned long offset
, size_t size
, int dir
)
800 swiotlb_sync_single_range(hwdev
, dev_addr
, offset
, size
, dir
,
804 void swiotlb_unmap_sg_attrs(struct device
*, struct scatterlist
*, int, int,
807 * Map a set of buffers described by scatterlist in streaming mode for DMA.
808 * This is the scatter-gather version of the above swiotlb_map_single
809 * interface. Here the scatter gather list elements are each tagged with the
810 * appropriate dma address and length. They are obtained via
811 * sg_dma_{address,length}(SG).
813 * NOTE: An implementation may be able to use a smaller number of
814 * DMA address/length pairs than there are SG table elements.
815 * (for example via virtual mapping capabilities)
816 * The routine returns the number of addr/length pairs actually
817 * used, at most nents.
819 * Device ownership issues as mentioned above for swiotlb_map_single are the
823 swiotlb_map_sg_attrs(struct device
*hwdev
, struct scatterlist
*sgl
, int nelems
,
824 int dir
, struct dma_attrs
*attrs
)
826 struct scatterlist
*sg
;
827 struct swiotlb_phys_addr buffer
;
831 BUG_ON(dir
== DMA_NONE
);
833 for_each_sg(sgl
, sg
, nelems
, i
) {
834 dev_addr
= swiotlb_sg_to_bus(hwdev
, sg
);
835 if (range_needs_mapping(sg_virt(sg
), sg
->length
) ||
836 address_needs_mapping(hwdev
, dev_addr
, sg
->length
)) {
838 buffer
.page
= sg_page(sg
);
839 buffer
.offset
= sg
->offset
;
840 map
= map_single(hwdev
, buffer
, sg
->length
, dir
);
842 /* Don't panic here, we expect map_sg users
843 to do proper error handling. */
844 swiotlb_full(hwdev
, sg
->length
, dir
, 0);
845 swiotlb_unmap_sg_attrs(hwdev
, sgl
, i
, dir
,
847 sgl
[0].dma_length
= 0;
850 sg
->dma_address
= swiotlb_virt_to_bus(hwdev
, map
);
852 sg
->dma_address
= dev_addr
;
853 sg
->dma_length
= sg
->length
;
857 EXPORT_SYMBOL(swiotlb_map_sg_attrs
);
860 swiotlb_map_sg(struct device
*hwdev
, struct scatterlist
*sgl
, int nelems
,
863 return swiotlb_map_sg_attrs(hwdev
, sgl
, nelems
, dir
, NULL
);
867 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
868 * concerning calls here are the same as for swiotlb_unmap_single() above.
871 swiotlb_unmap_sg_attrs(struct device
*hwdev
, struct scatterlist
*sgl
,
872 int nelems
, int dir
, struct dma_attrs
*attrs
)
874 struct scatterlist
*sg
;
877 BUG_ON(dir
== DMA_NONE
);
879 for_each_sg(sgl
, sg
, nelems
, i
) {
880 if (sg
->dma_address
!= swiotlb_sg_to_bus(hwdev
, sg
))
881 unmap_single(hwdev
, swiotlb_bus_to_virt(sg
->dma_address
),
882 sg
->dma_length
, dir
);
883 else if (dir
== DMA_FROM_DEVICE
)
884 dma_mark_clean(swiotlb_bus_to_virt(sg
->dma_address
), sg
->dma_length
);
887 EXPORT_SYMBOL(swiotlb_unmap_sg_attrs
);
890 swiotlb_unmap_sg(struct device
*hwdev
, struct scatterlist
*sgl
, int nelems
,
893 return swiotlb_unmap_sg_attrs(hwdev
, sgl
, nelems
, dir
, NULL
);
897 * Make physical memory consistent for a set of streaming mode DMA translations
900 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
904 swiotlb_sync_sg(struct device
*hwdev
, struct scatterlist
*sgl
,
905 int nelems
, int dir
, int target
)
907 struct scatterlist
*sg
;
910 BUG_ON(dir
== DMA_NONE
);
912 for_each_sg(sgl
, sg
, nelems
, i
) {
913 if (sg
->dma_address
!= swiotlb_sg_to_bus(hwdev
, sg
))
914 sync_single(hwdev
, swiotlb_bus_to_virt(sg
->dma_address
),
915 sg
->dma_length
, dir
, target
);
916 else if (dir
== DMA_FROM_DEVICE
)
917 dma_mark_clean(swiotlb_bus_to_virt(sg
->dma_address
), sg
->dma_length
);
922 swiotlb_sync_sg_for_cpu(struct device
*hwdev
, struct scatterlist
*sg
,
925 swiotlb_sync_sg(hwdev
, sg
, nelems
, dir
, SYNC_FOR_CPU
);
929 swiotlb_sync_sg_for_device(struct device
*hwdev
, struct scatterlist
*sg
,
932 swiotlb_sync_sg(hwdev
, sg
, nelems
, dir
, SYNC_FOR_DEVICE
);
936 swiotlb_dma_mapping_error(struct device
*hwdev
, dma_addr_t dma_addr
)
938 return (dma_addr
== swiotlb_virt_to_bus(hwdev
, io_tlb_overflow_buffer
));
942 * Return whether the given device DMA address mask can be supported
943 * properly. For example, if your device can only drive the low 24-bits
944 * during bus mastering, then you would pass 0x00ffffff as the mask to
948 swiotlb_dma_supported(struct device
*hwdev
, u64 mask
)
950 return swiotlb_virt_to_bus(hwdev
, io_tlb_end
- 1) <= mask
;
953 EXPORT_SYMBOL(swiotlb_map_single
);
954 EXPORT_SYMBOL(swiotlb_unmap_single
);
955 EXPORT_SYMBOL(swiotlb_map_sg
);
956 EXPORT_SYMBOL(swiotlb_unmap_sg
);
957 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu
);
958 EXPORT_SYMBOL(swiotlb_sync_single_for_device
);
959 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu
);
960 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device
);
961 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu
);
962 EXPORT_SYMBOL(swiotlb_sync_sg_for_device
);
963 EXPORT_SYMBOL(swiotlb_dma_mapping_error
);
964 EXPORT_SYMBOL(swiotlb_alloc_coherent
);
965 EXPORT_SYMBOL(swiotlb_free_coherent
);
966 EXPORT_SYMBOL(swiotlb_dma_supported
);