Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / lib / swiotlb.c
blob3b482052f4036c29338ecdf9aae91efcaa5ec2f8
1 /*
2 * Dynamic DMA mapping support.
4 * This implementation is for IA-64 and EM64T 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/string.h>
25 #include <linux/types.h>
26 #include <linux/ctype.h>
28 #include <asm/io.h>
29 #include <asm/dma.h>
30 #include <asm/scatterlist.h>
32 #include <linux/init.h>
33 #include <linux/bootmem.h>
35 #define OFFSET(val,align) ((unsigned long) \
36 ( (val) & ( (align) - 1)))
38 #define SG_ENT_VIRT_ADDRESS(sg) (page_address((sg)->page) + (sg)->offset)
39 #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
42 * Maximum allowable number of contiguous slabs to map,
43 * must be a power of 2. What is the appropriate value ?
44 * The complexity of {map,unmap}_single is linearly dependent on this value.
46 #define IO_TLB_SEGSIZE 128
49 * log of the size of each IO TLB slab. The number of slabs is command line
50 * controllable.
52 #define IO_TLB_SHIFT 11
54 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
57 * Minimum IO TLB size to bother booting with. Systems with mainly
58 * 64bit capable cards will only lightly use the swiotlb. If we can't
59 * allocate a contiguous 1MB, we're probably in trouble anyway.
61 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
64 * Enumeration for sync targets
66 enum dma_sync_target {
67 SYNC_FOR_CPU = 0,
68 SYNC_FOR_DEVICE = 1,
71 int swiotlb_force;
74 * Used to do a quick range check in swiotlb_unmap_single and
75 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
76 * API.
78 static char *io_tlb_start, *io_tlb_end;
81 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
82 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
84 static unsigned long io_tlb_nslabs;
87 * When the IOMMU overflows we return a fallback buffer. This sets the size.
89 static unsigned long io_tlb_overflow = 32*1024;
91 void *io_tlb_overflow_buffer;
94 * This is a free list describing the number of free entries available from
95 * each index
97 static unsigned int *io_tlb_list;
98 static unsigned int io_tlb_index;
101 * We need to save away the original address corresponding to a mapped entry
102 * for the sync operations.
104 static unsigned char **io_tlb_orig_addr;
107 * Protect the above data structures in the map and unmap calls
109 static DEFINE_SPINLOCK(io_tlb_lock);
111 static int __init
112 setup_io_tlb_npages(char *str)
114 if (isdigit(*str)) {
115 io_tlb_nslabs = simple_strtoul(str, &str, 0);
116 /* avoid tail segment of size < IO_TLB_SEGSIZE */
117 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
119 if (*str == ',')
120 ++str;
121 if (!strcmp(str, "force"))
122 swiotlb_force = 1;
123 return 1;
125 __setup("swiotlb=", setup_io_tlb_npages);
126 /* make io_tlb_overflow tunable too? */
129 * Statically reserve bounce buffer space and initialize bounce buffer data
130 * structures for the software IO TLB used to implement the DMA API.
132 void
133 swiotlb_init_with_default_size (size_t default_size)
135 unsigned long i;
137 if (!io_tlb_nslabs) {
138 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
139 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
143 * Get IO TLB memory from the low pages
145 io_tlb_start = alloc_bootmem_low_pages(io_tlb_nslabs * (1 << IO_TLB_SHIFT));
146 if (!io_tlb_start)
147 panic("Cannot allocate SWIOTLB buffer");
148 io_tlb_end = io_tlb_start + io_tlb_nslabs * (1 << IO_TLB_SHIFT);
151 * Allocate and initialize the free list array. This array is used
152 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
153 * between io_tlb_start and io_tlb_end.
155 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
156 for (i = 0; i < io_tlb_nslabs; i++)
157 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
158 io_tlb_index = 0;
159 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
162 * Get the overflow emergency buffer
164 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
165 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
166 virt_to_phys(io_tlb_start), virt_to_phys(io_tlb_end));
169 void
170 swiotlb_init (void)
172 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
176 * Systems with larger DMA zones (those that don't support ISA) can
177 * initialize the swiotlb later using the slab allocator if needed.
178 * This should be just like above, but with some error catching.
181 swiotlb_late_init_with_default_size (size_t default_size)
183 unsigned long i, req_nslabs = io_tlb_nslabs;
184 unsigned int order;
186 if (!io_tlb_nslabs) {
187 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
188 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
192 * Get IO TLB memory from the low pages
194 order = get_order(io_tlb_nslabs * (1 << IO_TLB_SHIFT));
195 io_tlb_nslabs = SLABS_PER_PAGE << order;
197 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
198 io_tlb_start = (char *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
199 order);
200 if (io_tlb_start)
201 break;
202 order--;
205 if (!io_tlb_start)
206 goto cleanup1;
208 if (order != get_order(io_tlb_nslabs * (1 << IO_TLB_SHIFT))) {
209 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
210 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
211 io_tlb_nslabs = SLABS_PER_PAGE << order;
213 io_tlb_end = io_tlb_start + io_tlb_nslabs * (1 << IO_TLB_SHIFT);
214 memset(io_tlb_start, 0, io_tlb_nslabs * (1 << IO_TLB_SHIFT));
217 * Allocate and initialize the free list array. This array is used
218 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
219 * between io_tlb_start and io_tlb_end.
221 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
222 get_order(io_tlb_nslabs * sizeof(int)));
223 if (!io_tlb_list)
224 goto cleanup2;
226 for (i = 0; i < io_tlb_nslabs; i++)
227 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
228 io_tlb_index = 0;
230 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
231 get_order(io_tlb_nslabs * sizeof(char *)));
232 if (!io_tlb_orig_addr)
233 goto cleanup3;
235 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
238 * Get the overflow emergency buffer
240 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
241 get_order(io_tlb_overflow));
242 if (!io_tlb_overflow_buffer)
243 goto cleanup4;
245 printk(KERN_INFO "Placing %ldMB software IO TLB between 0x%lx - "
246 "0x%lx\n", (io_tlb_nslabs * (1 << IO_TLB_SHIFT)) >> 20,
247 virt_to_phys(io_tlb_start), virt_to_phys(io_tlb_end));
249 return 0;
251 cleanup4:
252 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
253 sizeof(char *)));
254 io_tlb_orig_addr = NULL;
255 cleanup3:
256 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
257 sizeof(int)));
258 io_tlb_list = NULL;
259 io_tlb_end = NULL;
260 cleanup2:
261 free_pages((unsigned long)io_tlb_start, order);
262 io_tlb_start = NULL;
263 cleanup1:
264 io_tlb_nslabs = req_nslabs;
265 return -ENOMEM;
268 static inline int
269 address_needs_mapping(struct device *hwdev, dma_addr_t addr)
271 dma_addr_t mask = 0xffffffff;
272 /* If the device has a mask, use it, otherwise default to 32 bits */
273 if (hwdev && hwdev->dma_mask)
274 mask = *hwdev->dma_mask;
275 return (addr & ~mask) != 0;
279 * Allocates bounce buffer and returns its kernel virtual address.
281 static void *
282 map_single(struct device *hwdev, char *buffer, size_t size, int dir)
284 unsigned long flags;
285 char *dma_addr;
286 unsigned int nslots, stride, index, wrap;
287 int i;
290 * For mappings greater than a page, we limit the stride (and
291 * hence alignment) to a page size.
293 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
294 if (size > PAGE_SIZE)
295 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
296 else
297 stride = 1;
299 if (!nslots)
300 BUG();
303 * Find suitable number of IO TLB entries size that will fit this
304 * request and allocate a buffer from that IO TLB pool.
306 spin_lock_irqsave(&io_tlb_lock, flags);
308 wrap = index = ALIGN(io_tlb_index, stride);
310 if (index >= io_tlb_nslabs)
311 wrap = index = 0;
313 do {
315 * If we find a slot that indicates we have 'nslots'
316 * number of contiguous buffers, we allocate the
317 * buffers from that slot and mark the entries as '0'
318 * indicating unavailable.
320 if (io_tlb_list[index] >= nslots) {
321 int count = 0;
323 for (i = index; i < (int) (index + nslots); i++)
324 io_tlb_list[i] = 0;
325 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
326 io_tlb_list[i] = ++count;
327 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
330 * Update the indices to avoid searching in
331 * the next round.
333 io_tlb_index = ((index + nslots) < io_tlb_nslabs
334 ? (index + nslots) : 0);
336 goto found;
338 index += stride;
339 if (index >= io_tlb_nslabs)
340 index = 0;
341 } while (index != wrap);
343 spin_unlock_irqrestore(&io_tlb_lock, flags);
344 return NULL;
346 found:
347 spin_unlock_irqrestore(&io_tlb_lock, flags);
350 * Save away the mapping from the original address to the DMA address.
351 * This is needed when we sync the memory. Then we sync the buffer if
352 * needed.
354 io_tlb_orig_addr[index] = buffer;
355 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
356 memcpy(dma_addr, buffer, size);
358 return dma_addr;
362 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
364 static void
365 unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
367 unsigned long flags;
368 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
369 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
370 char *buffer = io_tlb_orig_addr[index];
373 * First, sync the memory before unmapping the entry
375 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
377 * bounce... copy the data back into the original buffer * and
378 * delete the bounce buffer.
380 memcpy(buffer, dma_addr, size);
383 * Return the buffer to the free list by setting the corresponding
384 * entries to indicate the number of contigous entries available.
385 * While returning the entries to the free list, we merge the entries
386 * with slots below and above the pool being returned.
388 spin_lock_irqsave(&io_tlb_lock, flags);
390 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
391 io_tlb_list[index + nslots] : 0);
393 * Step 1: return the slots to the free list, merging the
394 * slots with superceeding slots
396 for (i = index + nslots - 1; i >= index; i--)
397 io_tlb_list[i] = ++count;
399 * Step 2: merge the returned slots with the preceding slots,
400 * if available (non zero)
402 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
403 io_tlb_list[i] = ++count;
405 spin_unlock_irqrestore(&io_tlb_lock, flags);
408 static void
409 sync_single(struct device *hwdev, char *dma_addr, size_t size,
410 int dir, int target)
412 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
413 char *buffer = io_tlb_orig_addr[index];
415 switch (target) {
416 case SYNC_FOR_CPU:
417 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
418 memcpy(buffer, dma_addr, size);
419 else if (dir != DMA_TO_DEVICE)
420 BUG();
421 break;
422 case SYNC_FOR_DEVICE:
423 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
424 memcpy(dma_addr, buffer, size);
425 else if (dir != DMA_FROM_DEVICE)
426 BUG();
427 break;
428 default:
429 BUG();
433 void *
434 swiotlb_alloc_coherent(struct device *hwdev, size_t size,
435 dma_addr_t *dma_handle, gfp_t flags)
437 unsigned long dev_addr;
438 void *ret;
439 int order = get_order(size);
442 * XXX fix me: the DMA API should pass us an explicit DMA mask
443 * instead, or use ZONE_DMA32 (ia64 overloads ZONE_DMA to be a ~32
444 * bit range instead of a 16MB one).
446 flags |= GFP_DMA;
448 ret = (void *)__get_free_pages(flags, order);
449 if (ret && address_needs_mapping(hwdev, virt_to_phys(ret))) {
451 * The allocated memory isn't reachable by the device.
452 * Fall back on swiotlb_map_single().
454 free_pages((unsigned long) ret, order);
455 ret = NULL;
457 if (!ret) {
459 * We are either out of memory or the device can't DMA
460 * to GFP_DMA memory; fall back on
461 * swiotlb_map_single(), which will grab memory from
462 * the lowest available address range.
464 dma_addr_t handle;
465 handle = swiotlb_map_single(NULL, NULL, size, DMA_FROM_DEVICE);
466 if (dma_mapping_error(handle))
467 return NULL;
469 ret = phys_to_virt(handle);
472 memset(ret, 0, size);
473 dev_addr = virt_to_phys(ret);
475 /* Confirm address can be DMA'd by device */
476 if (address_needs_mapping(hwdev, dev_addr)) {
477 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016lx\n",
478 (unsigned long long)*hwdev->dma_mask, dev_addr);
479 panic("swiotlb_alloc_coherent: allocated memory is out of "
480 "range for device");
482 *dma_handle = dev_addr;
483 return ret;
486 void
487 swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
488 dma_addr_t dma_handle)
490 if (!(vaddr >= (void *)io_tlb_start
491 && vaddr < (void *)io_tlb_end))
492 free_pages((unsigned long) vaddr, get_order(size));
493 else
494 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
495 swiotlb_unmap_single (hwdev, dma_handle, size, DMA_TO_DEVICE);
498 static void
499 swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
502 * Ran out of IOMMU space for this operation. This is very bad.
503 * Unfortunately the drivers cannot handle this operation properly.
504 * unless they check for dma_mapping_error (most don't)
505 * When the mapping is small enough return a static buffer to limit
506 * the damage, or panic when the transfer is too big.
508 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %lu bytes at "
509 "device %s\n", size, dev ? dev->bus_id : "?");
511 if (size > io_tlb_overflow && do_panic) {
512 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
513 panic("DMA: Memory would be corrupted\n");
514 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
515 panic("DMA: Random memory would be DMAed\n");
520 * Map a single buffer of the indicated size for DMA in streaming mode. The
521 * physical address to use is returned.
523 * Once the device is given the dma address, the device owns this memory until
524 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
526 dma_addr_t
527 swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
529 unsigned long dev_addr = virt_to_phys(ptr);
530 void *map;
532 if (dir == DMA_NONE)
533 BUG();
535 * If the pointer passed in happens to be in the device's DMA window,
536 * we can safely return the device addr and not worry about bounce
537 * buffering it.
539 if (!address_needs_mapping(hwdev, dev_addr) && !swiotlb_force)
540 return dev_addr;
543 * Oh well, have to allocate and map a bounce buffer.
545 map = map_single(hwdev, ptr, size, dir);
546 if (!map) {
547 swiotlb_full(hwdev, size, dir, 1);
548 map = io_tlb_overflow_buffer;
551 dev_addr = virt_to_phys(map);
554 * Ensure that the address returned is DMA'ble
556 if (address_needs_mapping(hwdev, dev_addr))
557 panic("map_single: bounce buffer is not DMA'ble");
559 return dev_addr;
563 * Since DMA is i-cache coherent, any (complete) pages that were written via
564 * DMA can be marked as "clean" so that lazy_mmu_prot_update() doesn't have to
565 * flush them when they get mapped into an executable vm-area.
567 static void
568 mark_clean(void *addr, size_t size)
570 unsigned long pg_addr, end;
572 pg_addr = PAGE_ALIGN((unsigned long) addr);
573 end = (unsigned long) addr + size;
574 while (pg_addr + PAGE_SIZE <= end) {
575 struct page *page = virt_to_page(pg_addr);
576 set_bit(PG_arch_1, &page->flags);
577 pg_addr += PAGE_SIZE;
582 * Unmap a single streaming mode DMA translation. The dma_addr and size must
583 * match what was provided for in a previous swiotlb_map_single call. All
584 * other usages are undefined.
586 * After this call, reads by the cpu to the buffer are guaranteed to see
587 * whatever the device wrote there.
589 void
590 swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
591 int dir)
593 char *dma_addr = phys_to_virt(dev_addr);
595 if (dir == DMA_NONE)
596 BUG();
597 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
598 unmap_single(hwdev, dma_addr, size, dir);
599 else if (dir == DMA_FROM_DEVICE)
600 mark_clean(dma_addr, size);
604 * Make physical memory consistent for a single streaming mode DMA translation
605 * after a transfer.
607 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
608 * using the cpu, yet do not wish to teardown the dma mapping, you must
609 * call this function before doing so. At the next point you give the dma
610 * address back to the card, you must first perform a
611 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
613 static inline void
614 swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
615 size_t size, int dir, int target)
617 char *dma_addr = phys_to_virt(dev_addr);
619 if (dir == DMA_NONE)
620 BUG();
621 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
622 sync_single(hwdev, dma_addr, size, dir, target);
623 else if (dir == DMA_FROM_DEVICE)
624 mark_clean(dma_addr, size);
627 void
628 swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
629 size_t size, int dir)
631 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
634 void
635 swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
636 size_t size, int dir)
638 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
642 * Same as above, but for a sub-range of the mapping.
644 static inline void
645 swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
646 unsigned long offset, size_t size,
647 int dir, int target)
649 char *dma_addr = phys_to_virt(dev_addr) + offset;
651 if (dir == DMA_NONE)
652 BUG();
653 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
654 sync_single(hwdev, dma_addr, size, dir, target);
655 else if (dir == DMA_FROM_DEVICE)
656 mark_clean(dma_addr, size);
659 void
660 swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
661 unsigned long offset, size_t size, int dir)
663 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
664 SYNC_FOR_CPU);
667 void
668 swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
669 unsigned long offset, size_t size, int dir)
671 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
672 SYNC_FOR_DEVICE);
676 * Map a set of buffers described by scatterlist in streaming mode for DMA.
677 * This is the scatter-gather version of the above swiotlb_map_single
678 * interface. Here the scatter gather list elements are each tagged with the
679 * appropriate dma address and length. They are obtained via
680 * sg_dma_{address,length}(SG).
682 * NOTE: An implementation may be able to use a smaller number of
683 * DMA address/length pairs than there are SG table elements.
684 * (for example via virtual mapping capabilities)
685 * The routine returns the number of addr/length pairs actually
686 * used, at most nents.
688 * Device ownership issues as mentioned above for swiotlb_map_single are the
689 * same here.
692 swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
693 int dir)
695 void *addr;
696 unsigned long dev_addr;
697 int i;
699 if (dir == DMA_NONE)
700 BUG();
702 for (i = 0; i < nelems; i++, sg++) {
703 addr = SG_ENT_VIRT_ADDRESS(sg);
704 dev_addr = virt_to_phys(addr);
705 if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
706 void *map = map_single(hwdev, addr, sg->length, dir);
707 sg->dma_address = virt_to_bus(map);
708 if (!map) {
709 /* Don't panic here, we expect map_sg users
710 to do proper error handling. */
711 swiotlb_full(hwdev, sg->length, dir, 0);
712 swiotlb_unmap_sg(hwdev, sg - i, i, dir);
713 sg[0].dma_length = 0;
714 return 0;
716 } else
717 sg->dma_address = dev_addr;
718 sg->dma_length = sg->length;
720 return nelems;
724 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
725 * concerning calls here are the same as for swiotlb_unmap_single() above.
727 void
728 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
729 int dir)
731 int i;
733 if (dir == DMA_NONE)
734 BUG();
736 for (i = 0; i < nelems; i++, sg++)
737 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
738 unmap_single(hwdev, (void *) phys_to_virt(sg->dma_address), sg->dma_length, dir);
739 else if (dir == DMA_FROM_DEVICE)
740 mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
744 * Make physical memory consistent for a set of streaming mode DMA translations
745 * after a transfer.
747 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
748 * and usage.
750 static inline void
751 swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sg,
752 int nelems, int dir, int target)
754 int i;
756 if (dir == DMA_NONE)
757 BUG();
759 for (i = 0; i < nelems; i++, sg++)
760 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
761 sync_single(hwdev, (void *) sg->dma_address,
762 sg->dma_length, dir, target);
765 void
766 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
767 int nelems, int dir)
769 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
772 void
773 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
774 int nelems, int dir)
776 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
780 swiotlb_dma_mapping_error(dma_addr_t dma_addr)
782 return (dma_addr == virt_to_phys(io_tlb_overflow_buffer));
786 * Return whether the given device DMA address mask can be supported
787 * properly. For example, if your device can only drive the low 24-bits
788 * during bus mastering, then you would pass 0x00ffffff as the mask to
789 * this function.
792 swiotlb_dma_supported (struct device *hwdev, u64 mask)
794 return (virt_to_phys (io_tlb_end) - 1) <= mask;
797 EXPORT_SYMBOL(swiotlb_init);
798 EXPORT_SYMBOL(swiotlb_map_single);
799 EXPORT_SYMBOL(swiotlb_unmap_single);
800 EXPORT_SYMBOL(swiotlb_map_sg);
801 EXPORT_SYMBOL(swiotlb_unmap_sg);
802 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
803 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
804 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
805 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
806 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
807 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
808 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
809 EXPORT_SYMBOL(swiotlb_alloc_coherent);
810 EXPORT_SYMBOL(swiotlb_free_coherent);
811 EXPORT_SYMBOL(swiotlb_dma_supported);