swiotlb: allow architectures to override swiotlb pool allocation
[linux-2.6/mini2440.git] / lib / swiotlb.c
blobabecb2857556c9790ab23ee9eca3281716c90bca
1 /*
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>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/spinlock.h>
24 #include <linux/swiotlb.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
27 #include <linux/ctype.h>
29 #include <asm/io.h>
30 #include <asm/dma.h>
31 #include <asm/scatterlist.h>
33 #include <linux/init.h>
34 #include <linux/bootmem.h>
35 #include <linux/iommu-helper.h>
37 #define OFFSET(val,align) ((unsigned long) \
38 ( (val) & ( (align) - 1)))
40 #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
41 #define SG_ENT_PHYS_ADDRESS(sg) virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
44 * Maximum allowable number of contiguous slabs to map,
45 * must be a power of 2. What is the appropriate value ?
46 * The complexity of {map,unmap}_single is linearly dependent on this value.
48 #define IO_TLB_SEGSIZE 128
51 * log of the size of each IO TLB slab. The number of slabs is command line
52 * controllable.
54 #define IO_TLB_SHIFT 11
56 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
59 * Minimum IO TLB size to bother booting with. Systems with mainly
60 * 64bit capable cards will only lightly use the swiotlb. If we can't
61 * allocate a contiguous 1MB, we're probably in trouble anyway.
63 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
66 * Enumeration for sync targets
68 enum dma_sync_target {
69 SYNC_FOR_CPU = 0,
70 SYNC_FOR_DEVICE = 1,
73 int swiotlb_force;
76 * Used to do a quick range check in swiotlb_unmap_single and
77 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
78 * API.
80 static char *io_tlb_start, *io_tlb_end;
83 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
84 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
86 static unsigned long io_tlb_nslabs;
89 * When the IOMMU overflows we return a fallback buffer. This sets the size.
91 static unsigned long io_tlb_overflow = 32*1024;
93 void *io_tlb_overflow_buffer;
96 * This is a free list describing the number of free entries available from
97 * each index
99 static unsigned int *io_tlb_list;
100 static unsigned int io_tlb_index;
103 * We need to save away the original address corresponding to a mapped entry
104 * for the sync operations.
106 static unsigned char **io_tlb_orig_addr;
109 * Protect the above data structures in the map and unmap calls
111 static DEFINE_SPINLOCK(io_tlb_lock);
113 static int __init
114 setup_io_tlb_npages(char *str)
116 if (isdigit(*str)) {
117 io_tlb_nslabs = simple_strtoul(str, &str, 0);
118 /* avoid tail segment of size < IO_TLB_SEGSIZE */
119 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
121 if (*str == ',')
122 ++str;
123 if (!strcmp(str, "force"))
124 swiotlb_force = 1;
125 return 1;
127 __setup("swiotlb=", setup_io_tlb_npages);
128 /* make io_tlb_overflow tunable too? */
130 void * __weak swiotlb_alloc_boot(size_t size, unsigned long nslabs)
132 return alloc_bootmem_low_pages(size);
135 void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs)
137 return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
141 * Statically reserve bounce buffer space and initialize bounce buffer data
142 * structures for the software IO TLB used to implement the DMA API.
144 void __init
145 swiotlb_init_with_default_size(size_t default_size)
147 unsigned long i, bytes;
149 if (!io_tlb_nslabs) {
150 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
151 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
154 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
157 * Get IO TLB memory from the low pages
159 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
160 if (!io_tlb_start)
161 panic("Cannot allocate SWIOTLB buffer");
162 io_tlb_end = io_tlb_start + bytes;
165 * Allocate and initialize the free list array. This array is used
166 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
167 * between io_tlb_start and io_tlb_end.
169 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
170 for (i = 0; i < io_tlb_nslabs; i++)
171 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
172 io_tlb_index = 0;
173 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
176 * Get the overflow emergency buffer
178 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
179 if (!io_tlb_overflow_buffer)
180 panic("Cannot allocate SWIOTLB overflow buffer!\n");
182 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
183 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
186 void __init
187 swiotlb_init(void)
189 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
193 * Systems with larger DMA zones (those that don't support ISA) can
194 * initialize the swiotlb later using the slab allocator if needed.
195 * This should be just like above, but with some error catching.
198 swiotlb_late_init_with_default_size(size_t default_size)
200 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
201 unsigned int order;
203 if (!io_tlb_nslabs) {
204 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
205 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
209 * Get IO TLB memory from the low pages
211 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
212 io_tlb_nslabs = SLABS_PER_PAGE << order;
213 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
215 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
216 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
217 if (io_tlb_start)
218 break;
219 order--;
222 if (!io_tlb_start)
223 goto cleanup1;
225 if (order != get_order(bytes)) {
226 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
227 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
228 io_tlb_nslabs = SLABS_PER_PAGE << order;
229 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
231 io_tlb_end = io_tlb_start + bytes;
232 memset(io_tlb_start, 0, bytes);
235 * Allocate and initialize the free list array. This array is used
236 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
237 * between io_tlb_start and io_tlb_end.
239 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
240 get_order(io_tlb_nslabs * sizeof(int)));
241 if (!io_tlb_list)
242 goto cleanup2;
244 for (i = 0; i < io_tlb_nslabs; i++)
245 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
246 io_tlb_index = 0;
248 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
249 get_order(io_tlb_nslabs * sizeof(char *)));
250 if (!io_tlb_orig_addr)
251 goto cleanup3;
253 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
256 * Get the overflow emergency buffer
258 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
259 get_order(io_tlb_overflow));
260 if (!io_tlb_overflow_buffer)
261 goto cleanup4;
263 printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - "
264 "0x%lx\n", bytes >> 20,
265 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
267 return 0;
269 cleanup4:
270 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
271 sizeof(char *)));
272 io_tlb_orig_addr = NULL;
273 cleanup3:
274 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
275 sizeof(int)));
276 io_tlb_list = NULL;
277 cleanup2:
278 io_tlb_end = NULL;
279 free_pages((unsigned long)io_tlb_start, order);
280 io_tlb_start = NULL;
281 cleanup1:
282 io_tlb_nslabs = req_nslabs;
283 return -ENOMEM;
286 static int
287 address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
289 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
292 static int is_swiotlb_buffer(char *addr)
294 return addr >= io_tlb_start && addr < io_tlb_end;
298 * Allocates bounce buffer and returns its kernel virtual address.
300 static void *
301 map_single(struct device *hwdev, char *buffer, size_t size, int dir)
303 unsigned long flags;
304 char *dma_addr;
305 unsigned int nslots, stride, index, wrap;
306 int i;
307 unsigned long start_dma_addr;
308 unsigned long mask;
309 unsigned long offset_slots;
310 unsigned long max_slots;
312 mask = dma_get_seg_boundary(hwdev);
313 start_dma_addr = virt_to_bus(io_tlb_start) & mask;
315 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
316 max_slots = mask + 1
317 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
318 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
321 * For mappings greater than a page, we limit the stride (and
322 * hence alignment) to a page size.
324 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
325 if (size > PAGE_SIZE)
326 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
327 else
328 stride = 1;
330 BUG_ON(!nslots);
333 * Find suitable number of IO TLB entries size that will fit this
334 * request and allocate a buffer from that IO TLB pool.
336 spin_lock_irqsave(&io_tlb_lock, flags);
337 index = ALIGN(io_tlb_index, stride);
338 if (index >= io_tlb_nslabs)
339 index = 0;
340 wrap = index;
342 do {
343 while (iommu_is_span_boundary(index, nslots, offset_slots,
344 max_slots)) {
345 index += stride;
346 if (index >= io_tlb_nslabs)
347 index = 0;
348 if (index == wrap)
349 goto not_found;
353 * If we find a slot that indicates we have 'nslots' number of
354 * contiguous buffers, we allocate the buffers from that slot
355 * and mark the entries as '0' indicating unavailable.
357 if (io_tlb_list[index] >= nslots) {
358 int count = 0;
360 for (i = index; i < (int) (index + nslots); i++)
361 io_tlb_list[i] = 0;
362 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
363 io_tlb_list[i] = ++count;
364 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
367 * Update the indices to avoid searching in the next
368 * round.
370 io_tlb_index = ((index + nslots) < io_tlb_nslabs
371 ? (index + nslots) : 0);
373 goto found;
375 index += stride;
376 if (index >= io_tlb_nslabs)
377 index = 0;
378 } while (index != wrap);
380 not_found:
381 spin_unlock_irqrestore(&io_tlb_lock, flags);
382 return NULL;
383 found:
384 spin_unlock_irqrestore(&io_tlb_lock, flags);
387 * Save away the mapping from the original address to the DMA address.
388 * This is needed when we sync the memory. Then we sync the buffer if
389 * needed.
391 for (i = 0; i < nslots; i++)
392 io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
393 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
394 memcpy(dma_addr, buffer, size);
396 return dma_addr;
400 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
402 static void
403 unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
405 unsigned long flags;
406 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
407 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
408 char *buffer = io_tlb_orig_addr[index];
411 * First, sync the memory before unmapping the entry
413 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
415 * bounce... copy the data back into the original buffer * and
416 * delete the bounce buffer.
418 memcpy(buffer, dma_addr, size);
421 * Return the buffer to the free list by setting the corresponding
422 * entries to indicate the number of contigous entries available.
423 * While returning the entries to the free list, we merge the entries
424 * with slots below and above the pool being returned.
426 spin_lock_irqsave(&io_tlb_lock, flags);
428 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
429 io_tlb_list[index + nslots] : 0);
431 * Step 1: return the slots to the free list, merging the
432 * slots with superceeding slots
434 for (i = index + nslots - 1; i >= index; i--)
435 io_tlb_list[i] = ++count;
437 * Step 2: merge the returned slots with the preceding slots,
438 * if available (non zero)
440 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
441 io_tlb_list[i] = ++count;
443 spin_unlock_irqrestore(&io_tlb_lock, flags);
446 static void
447 sync_single(struct device *hwdev, char *dma_addr, size_t size,
448 int dir, int target)
450 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
451 char *buffer = io_tlb_orig_addr[index];
453 buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
455 switch (target) {
456 case SYNC_FOR_CPU:
457 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
458 memcpy(buffer, dma_addr, size);
459 else
460 BUG_ON(dir != DMA_TO_DEVICE);
461 break;
462 case SYNC_FOR_DEVICE:
463 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
464 memcpy(dma_addr, buffer, size);
465 else
466 BUG_ON(dir != DMA_FROM_DEVICE);
467 break;
468 default:
469 BUG();
473 void *
474 swiotlb_alloc_coherent(struct device *hwdev, size_t size,
475 dma_addr_t *dma_handle, gfp_t flags)
477 dma_addr_t dev_addr;
478 void *ret;
479 int order = get_order(size);
480 u64 dma_mask = DMA_32BIT_MASK;
482 if (hwdev && hwdev->coherent_dma_mask)
483 dma_mask = hwdev->coherent_dma_mask;
485 ret = (void *)__get_free_pages(flags, order);
486 if (ret && !is_buffer_dma_capable(dma_mask, virt_to_bus(ret), size)) {
488 * The allocated memory isn't reachable by the device.
489 * Fall back on swiotlb_map_single().
491 free_pages((unsigned long) ret, order);
492 ret = NULL;
494 if (!ret) {
496 * We are either out of memory or the device can't DMA
497 * to GFP_DMA memory; fall back on
498 * swiotlb_map_single(), which will grab memory from
499 * the lowest available address range.
501 ret = map_single(hwdev, NULL, size, DMA_FROM_DEVICE);
502 if (!ret)
503 return NULL;
506 memset(ret, 0, size);
507 dev_addr = virt_to_bus(ret);
509 /* Confirm address can be DMA'd by device */
510 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
511 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
512 (unsigned long long)dma_mask,
513 (unsigned long long)dev_addr);
515 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
516 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
517 return NULL;
519 *dma_handle = dev_addr;
520 return ret;
523 void
524 swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
525 dma_addr_t dma_handle)
527 WARN_ON(irqs_disabled());
528 if (!is_swiotlb_buffer(vaddr))
529 free_pages((unsigned long) vaddr, get_order(size));
530 else
531 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
532 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
535 static void
536 swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
539 * Ran out of IOMMU space for this operation. This is very bad.
540 * Unfortunately the drivers cannot handle this operation properly.
541 * unless they check for dma_mapping_error (most don't)
542 * When the mapping is small enough return a static buffer to limit
543 * the damage, or panic when the transfer is too big.
545 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
546 "device %s\n", size, dev ? dev->bus_id : "?");
548 if (size > io_tlb_overflow && do_panic) {
549 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
550 panic("DMA: Memory would be corrupted\n");
551 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
552 panic("DMA: Random memory would be DMAed\n");
557 * Map a single buffer of the indicated size for DMA in streaming mode. The
558 * physical address to use is returned.
560 * Once the device is given the dma address, the device owns this memory until
561 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
563 dma_addr_t
564 swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
565 int dir, struct dma_attrs *attrs)
567 dma_addr_t dev_addr = virt_to_bus(ptr);
568 void *map;
570 BUG_ON(dir == DMA_NONE);
572 * If the pointer passed in happens to be in the device's DMA window,
573 * we can safely return the device addr and not worry about bounce
574 * buffering it.
576 if (!address_needs_mapping(hwdev, dev_addr, size) && !swiotlb_force)
577 return dev_addr;
580 * Oh well, have to allocate and map a bounce buffer.
582 map = map_single(hwdev, ptr, size, dir);
583 if (!map) {
584 swiotlb_full(hwdev, size, dir, 1);
585 map = io_tlb_overflow_buffer;
588 dev_addr = virt_to_bus(map);
591 * Ensure that the address returned is DMA'ble
593 if (address_needs_mapping(hwdev, dev_addr, size))
594 panic("map_single: bounce buffer is not DMA'ble");
596 return dev_addr;
598 EXPORT_SYMBOL(swiotlb_map_single_attrs);
600 dma_addr_t
601 swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
603 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
607 * Unmap a single streaming mode DMA translation. The dma_addr and size must
608 * match what was provided for in a previous swiotlb_map_single call. All
609 * other usages are undefined.
611 * After this call, reads by the cpu to the buffer are guaranteed to see
612 * whatever the device wrote there.
614 void
615 swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
616 size_t size, int dir, struct dma_attrs *attrs)
618 char *dma_addr = bus_to_virt(dev_addr);
620 BUG_ON(dir == DMA_NONE);
621 if (is_swiotlb_buffer(dma_addr))
622 unmap_single(hwdev, dma_addr, size, dir);
623 else if (dir == DMA_FROM_DEVICE)
624 dma_mark_clean(dma_addr, size);
626 EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
628 void
629 swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
630 int dir)
632 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
635 * Make physical memory consistent for a single streaming mode DMA translation
636 * after a transfer.
638 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
639 * using the cpu, yet do not wish to teardown the dma mapping, you must
640 * call this function before doing so. At the next point you give the dma
641 * address back to the card, you must first perform a
642 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
644 static void
645 swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
646 size_t size, int dir, int target)
648 char *dma_addr = bus_to_virt(dev_addr);
650 BUG_ON(dir == DMA_NONE);
651 if (is_swiotlb_buffer(dma_addr))
652 sync_single(hwdev, dma_addr, size, dir, target);
653 else if (dir == DMA_FROM_DEVICE)
654 dma_mark_clean(dma_addr, size);
657 void
658 swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
659 size_t size, int dir)
661 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
664 void
665 swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
666 size_t size, int dir)
668 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
672 * Same as above, but for a sub-range of the mapping.
674 static void
675 swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
676 unsigned long offset, size_t size,
677 int dir, int target)
679 char *dma_addr = bus_to_virt(dev_addr) + offset;
681 BUG_ON(dir == DMA_NONE);
682 if (is_swiotlb_buffer(dma_addr))
683 sync_single(hwdev, dma_addr, size, dir, target);
684 else if (dir == DMA_FROM_DEVICE)
685 dma_mark_clean(dma_addr, size);
688 void
689 swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
690 unsigned long offset, size_t size, int dir)
692 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
693 SYNC_FOR_CPU);
696 void
697 swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
698 unsigned long offset, size_t size, int dir)
700 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
701 SYNC_FOR_DEVICE);
704 void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
705 struct dma_attrs *);
707 * Map a set of buffers described by scatterlist in streaming mode for DMA.
708 * This is the scatter-gather version of the above swiotlb_map_single
709 * interface. Here the scatter gather list elements are each tagged with the
710 * appropriate dma address and length. They are obtained via
711 * sg_dma_{address,length}(SG).
713 * NOTE: An implementation may be able to use a smaller number of
714 * DMA address/length pairs than there are SG table elements.
715 * (for example via virtual mapping capabilities)
716 * The routine returns the number of addr/length pairs actually
717 * used, at most nents.
719 * Device ownership issues as mentioned above for swiotlb_map_single are the
720 * same here.
723 swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
724 int dir, struct dma_attrs *attrs)
726 struct scatterlist *sg;
727 void *addr;
728 dma_addr_t dev_addr;
729 int i;
731 BUG_ON(dir == DMA_NONE);
733 for_each_sg(sgl, sg, nelems, i) {
734 addr = SG_ENT_VIRT_ADDRESS(sg);
735 dev_addr = virt_to_bus(addr);
736 if (swiotlb_force ||
737 address_needs_mapping(hwdev, dev_addr, sg->length)) {
738 void *map = map_single(hwdev, addr, sg->length, dir);
739 if (!map) {
740 /* Don't panic here, we expect map_sg users
741 to do proper error handling. */
742 swiotlb_full(hwdev, sg->length, dir, 0);
743 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
744 attrs);
745 sgl[0].dma_length = 0;
746 return 0;
748 sg->dma_address = virt_to_bus(map);
749 } else
750 sg->dma_address = dev_addr;
751 sg->dma_length = sg->length;
753 return nelems;
755 EXPORT_SYMBOL(swiotlb_map_sg_attrs);
758 swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
759 int dir)
761 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
765 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
766 * concerning calls here are the same as for swiotlb_unmap_single() above.
768 void
769 swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
770 int nelems, int dir, struct dma_attrs *attrs)
772 struct scatterlist *sg;
773 int i;
775 BUG_ON(dir == DMA_NONE);
777 for_each_sg(sgl, sg, nelems, i) {
778 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
779 unmap_single(hwdev, bus_to_virt(sg->dma_address),
780 sg->dma_length, dir);
781 else if (dir == DMA_FROM_DEVICE)
782 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
785 EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
787 void
788 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
789 int dir)
791 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
795 * Make physical memory consistent for a set of streaming mode DMA translations
796 * after a transfer.
798 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
799 * and usage.
801 static void
802 swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
803 int nelems, int dir, int target)
805 struct scatterlist *sg;
806 int i;
808 BUG_ON(dir == DMA_NONE);
810 for_each_sg(sgl, sg, nelems, i) {
811 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
812 sync_single(hwdev, bus_to_virt(sg->dma_address),
813 sg->dma_length, dir, target);
814 else if (dir == DMA_FROM_DEVICE)
815 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
819 void
820 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
821 int nelems, int dir)
823 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
826 void
827 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
828 int nelems, int dir)
830 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
834 swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
836 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
840 * Return whether the given device DMA address mask can be supported
841 * properly. For example, if your device can only drive the low 24-bits
842 * during bus mastering, then you would pass 0x00ffffff as the mask to
843 * this function.
846 swiotlb_dma_supported(struct device *hwdev, u64 mask)
848 return virt_to_bus(io_tlb_end - 1) <= mask;
851 EXPORT_SYMBOL(swiotlb_map_single);
852 EXPORT_SYMBOL(swiotlb_unmap_single);
853 EXPORT_SYMBOL(swiotlb_map_sg);
854 EXPORT_SYMBOL(swiotlb_unmap_sg);
855 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
856 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
857 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
858 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
859 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
860 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
861 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
862 EXPORT_SYMBOL(swiotlb_alloc_coherent);
863 EXPORT_SYMBOL(swiotlb_free_coherent);
864 EXPORT_SYMBOL(swiotlb_dma_supported);