swapfile: swapon needs larger size type
[linux-2.6/linux-2.6-openrd.git] / lib / swiotlb.c
blob7f5e21b9c16b90ffaa7fffd3cee96a65a511302b
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>
28 #include <linux/highmem.h>
30 #include <asm/io.h>
31 #include <asm/dma.h>
32 #include <asm/scatterlist.h>
34 #include <linux/init.h>
35 #include <linux/bootmem.h>
36 #include <linux/iommu-helper.h>
38 #define OFFSET(val,align) ((unsigned long) \
39 ( (val) & ( (align) - 1)))
41 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
44 * Minimum IO TLB size to bother booting with. Systems with mainly
45 * 64bit capable cards will only lightly use the swiotlb. If we can't
46 * allocate a contiguous 1MB, we're probably in trouble anyway.
48 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
51 * Enumeration for sync targets
53 enum dma_sync_target {
54 SYNC_FOR_CPU = 0,
55 SYNC_FOR_DEVICE = 1,
58 int swiotlb_force;
61 * Used to do a quick range check in swiotlb_unmap_single and
62 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
63 * API.
65 static char *io_tlb_start, *io_tlb_end;
68 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
69 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
71 static unsigned long io_tlb_nslabs;
74 * When the IOMMU overflows we return a fallback buffer. This sets the size.
76 static unsigned long io_tlb_overflow = 32*1024;
78 void *io_tlb_overflow_buffer;
81 * This is a free list describing the number of free entries available from
82 * each index
84 static unsigned int *io_tlb_list;
85 static unsigned int io_tlb_index;
88 * We need to save away the original address corresponding to a mapped entry
89 * for the sync operations.
91 static struct swiotlb_phys_addr {
92 struct page *page;
93 unsigned int offset;
94 } *io_tlb_orig_addr;
97 * Protect the above data structures in the map and unmap calls
99 static DEFINE_SPINLOCK(io_tlb_lock);
101 static int __init
102 setup_io_tlb_npages(char *str)
104 if (isdigit(*str)) {
105 io_tlb_nslabs = simple_strtoul(str, &str, 0);
106 /* avoid tail segment of size < IO_TLB_SEGSIZE */
107 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
109 if (*str == ',')
110 ++str;
111 if (!strcmp(str, "force"))
112 swiotlb_force = 1;
113 return 1;
115 __setup("swiotlb=", setup_io_tlb_npages);
116 /* make io_tlb_overflow tunable too? */
118 void * __weak __init swiotlb_alloc_boot(size_t size, unsigned long nslabs)
120 return alloc_bootmem_low_pages(size);
123 void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs)
125 return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
128 dma_addr_t __weak swiotlb_phys_to_bus(phys_addr_t paddr)
130 return paddr;
133 phys_addr_t __weak swiotlb_bus_to_phys(dma_addr_t baddr)
135 return baddr;
138 static dma_addr_t swiotlb_virt_to_bus(volatile void *address)
140 return swiotlb_phys_to_bus(virt_to_phys(address));
143 static void *swiotlb_bus_to_virt(dma_addr_t address)
145 return phys_to_virt(swiotlb_bus_to_phys(address));
148 int __weak swiotlb_arch_range_needs_mapping(void *ptr, size_t size)
150 return 0;
153 static dma_addr_t swiotlb_sg_to_bus(struct scatterlist *sg)
155 return swiotlb_phys_to_bus(page_to_phys(sg_page(sg)) + sg->offset);
158 static void swiotlb_print_info(unsigned long bytes)
160 phys_addr_t pstart, pend;
161 dma_addr_t bstart, bend;
163 pstart = virt_to_phys(io_tlb_start);
164 pend = virt_to_phys(io_tlb_end);
166 bstart = swiotlb_phys_to_bus(pstart);
167 bend = swiotlb_phys_to_bus(pend);
169 printk(KERN_INFO "Placing %luMB software IO TLB between %p - %p\n",
170 bytes >> 20, io_tlb_start, io_tlb_end);
171 if (pstart != bstart || pend != bend)
172 printk(KERN_INFO "software IO TLB at phys %#llx - %#llx"
173 " bus %#llx - %#llx\n",
174 (unsigned long long)pstart,
175 (unsigned long long)pend,
176 (unsigned long long)bstart,
177 (unsigned long long)bend);
178 else
179 printk(KERN_INFO "software IO TLB at phys %#llx - %#llx\n",
180 (unsigned long long)pstart,
181 (unsigned long long)pend);
185 * Statically reserve bounce buffer space and initialize bounce buffer data
186 * structures for the software IO TLB used to implement the DMA API.
188 void __init
189 swiotlb_init_with_default_size(size_t default_size)
191 unsigned long i, bytes;
193 if (!io_tlb_nslabs) {
194 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
195 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
198 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
201 * Get IO TLB memory from the low pages
203 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
204 if (!io_tlb_start)
205 panic("Cannot allocate SWIOTLB buffer");
206 io_tlb_end = io_tlb_start + bytes;
209 * Allocate and initialize the free list array. This array is used
210 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
211 * between io_tlb_start and io_tlb_end.
213 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
214 for (i = 0; i < io_tlb_nslabs; i++)
215 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
216 io_tlb_index = 0;
217 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(struct swiotlb_phys_addr));
220 * Get the overflow emergency buffer
222 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
223 if (!io_tlb_overflow_buffer)
224 panic("Cannot allocate SWIOTLB overflow buffer!\n");
226 swiotlb_print_info(bytes);
229 void __init
230 swiotlb_init(void)
232 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
236 * Systems with larger DMA zones (those that don't support ISA) can
237 * initialize the swiotlb later using the slab allocator if needed.
238 * This should be just like above, but with some error catching.
241 swiotlb_late_init_with_default_size(size_t default_size)
243 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
244 unsigned int order;
246 if (!io_tlb_nslabs) {
247 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
248 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
252 * Get IO TLB memory from the low pages
254 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
255 io_tlb_nslabs = SLABS_PER_PAGE << order;
256 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
258 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
259 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
260 if (io_tlb_start)
261 break;
262 order--;
265 if (!io_tlb_start)
266 goto cleanup1;
268 if (order != get_order(bytes)) {
269 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
270 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
271 io_tlb_nslabs = SLABS_PER_PAGE << order;
272 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
274 io_tlb_end = io_tlb_start + bytes;
275 memset(io_tlb_start, 0, bytes);
278 * Allocate and initialize the free list array. This array is used
279 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
280 * between io_tlb_start and io_tlb_end.
282 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
283 get_order(io_tlb_nslabs * sizeof(int)));
284 if (!io_tlb_list)
285 goto cleanup2;
287 for (i = 0; i < io_tlb_nslabs; i++)
288 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
289 io_tlb_index = 0;
291 io_tlb_orig_addr = (struct swiotlb_phys_addr *)__get_free_pages(GFP_KERNEL,
292 get_order(io_tlb_nslabs * sizeof(struct swiotlb_phys_addr)));
293 if (!io_tlb_orig_addr)
294 goto cleanup3;
296 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(struct swiotlb_phys_addr));
299 * Get the overflow emergency buffer
301 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
302 get_order(io_tlb_overflow));
303 if (!io_tlb_overflow_buffer)
304 goto cleanup4;
306 swiotlb_print_info(bytes);
308 return 0;
310 cleanup4:
311 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
312 sizeof(char *)));
313 io_tlb_orig_addr = NULL;
314 cleanup3:
315 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
316 sizeof(int)));
317 io_tlb_list = NULL;
318 cleanup2:
319 io_tlb_end = NULL;
320 free_pages((unsigned long)io_tlb_start, order);
321 io_tlb_start = NULL;
322 cleanup1:
323 io_tlb_nslabs = req_nslabs;
324 return -ENOMEM;
327 static int
328 address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
330 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
333 static inline int range_needs_mapping(void *ptr, size_t size)
335 return swiotlb_force || swiotlb_arch_range_needs_mapping(ptr, size);
338 static int is_swiotlb_buffer(char *addr)
340 return addr >= io_tlb_start && addr < io_tlb_end;
343 static struct swiotlb_phys_addr swiotlb_bus_to_phys_addr(char *dma_addr)
345 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
346 struct swiotlb_phys_addr buffer = io_tlb_orig_addr[index];
347 buffer.offset += (long)dma_addr & ((1 << IO_TLB_SHIFT) - 1);
348 buffer.page += buffer.offset >> PAGE_SHIFT;
349 buffer.offset &= PAGE_SIZE - 1;
350 return buffer;
353 static void
354 __sync_single(struct swiotlb_phys_addr buffer, char *dma_addr, size_t size, int dir)
356 if (PageHighMem(buffer.page)) {
357 size_t len, bytes;
358 char *dev, *host, *kmp;
360 len = size;
361 while (len != 0) {
362 unsigned long flags;
364 bytes = len;
365 if ((bytes + buffer.offset) > PAGE_SIZE)
366 bytes = PAGE_SIZE - buffer.offset;
367 local_irq_save(flags); /* protects KM_BOUNCE_READ */
368 kmp = kmap_atomic(buffer.page, KM_BOUNCE_READ);
369 dev = dma_addr + size - len;
370 host = kmp + buffer.offset;
371 if (dir == DMA_FROM_DEVICE)
372 memcpy(host, dev, bytes);
373 else
374 memcpy(dev, host, bytes);
375 kunmap_atomic(kmp, KM_BOUNCE_READ);
376 local_irq_restore(flags);
377 len -= bytes;
378 buffer.page++;
379 buffer.offset = 0;
381 } else {
382 void *v = page_address(buffer.page) + buffer.offset;
384 if (dir == DMA_TO_DEVICE)
385 memcpy(dma_addr, v, size);
386 else
387 memcpy(v, dma_addr, size);
392 * Allocates bounce buffer and returns its kernel virtual address.
394 static void *
395 map_single(struct device *hwdev, struct swiotlb_phys_addr buffer, size_t size, int dir)
397 unsigned long flags;
398 char *dma_addr;
399 unsigned int nslots, stride, index, wrap;
400 int i;
401 unsigned long start_dma_addr;
402 unsigned long mask;
403 unsigned long offset_slots;
404 unsigned long max_slots;
405 struct swiotlb_phys_addr slot_buf;
407 mask = dma_get_seg_boundary(hwdev);
408 start_dma_addr = swiotlb_virt_to_bus(io_tlb_start) & mask;
410 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
413 * Carefully handle integer overflow which can occur when mask == ~0UL.
415 max_slots = mask + 1
416 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
417 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
420 * For mappings greater than a page, we limit the stride (and
421 * hence alignment) to a page size.
423 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
424 if (size > PAGE_SIZE)
425 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
426 else
427 stride = 1;
429 BUG_ON(!nslots);
432 * Find suitable number of IO TLB entries size that will fit this
433 * request and allocate a buffer from that IO TLB pool.
435 spin_lock_irqsave(&io_tlb_lock, flags);
436 index = ALIGN(io_tlb_index, stride);
437 if (index >= io_tlb_nslabs)
438 index = 0;
439 wrap = index;
441 do {
442 while (iommu_is_span_boundary(index, nslots, offset_slots,
443 max_slots)) {
444 index += stride;
445 if (index >= io_tlb_nslabs)
446 index = 0;
447 if (index == wrap)
448 goto not_found;
452 * If we find a slot that indicates we have 'nslots' number of
453 * contiguous buffers, we allocate the buffers from that slot
454 * and mark the entries as '0' indicating unavailable.
456 if (io_tlb_list[index] >= nslots) {
457 int count = 0;
459 for (i = index; i < (int) (index + nslots); i++)
460 io_tlb_list[i] = 0;
461 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
462 io_tlb_list[i] = ++count;
463 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
466 * Update the indices to avoid searching in the next
467 * round.
469 io_tlb_index = ((index + nslots) < io_tlb_nslabs
470 ? (index + nslots) : 0);
472 goto found;
474 index += stride;
475 if (index >= io_tlb_nslabs)
476 index = 0;
477 } while (index != wrap);
479 not_found:
480 spin_unlock_irqrestore(&io_tlb_lock, flags);
481 return NULL;
482 found:
483 spin_unlock_irqrestore(&io_tlb_lock, flags);
486 * Save away the mapping from the original address to the DMA address.
487 * This is needed when we sync the memory. Then we sync the buffer if
488 * needed.
490 slot_buf = buffer;
491 for (i = 0; i < nslots; i++) {
492 slot_buf.page += slot_buf.offset >> PAGE_SHIFT;
493 slot_buf.offset &= PAGE_SIZE - 1;
494 io_tlb_orig_addr[index+i] = slot_buf;
495 slot_buf.offset += 1 << IO_TLB_SHIFT;
497 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
498 __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE);
500 return dma_addr;
504 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
506 static void
507 unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
509 unsigned long flags;
510 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
511 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
512 struct swiotlb_phys_addr buffer = swiotlb_bus_to_phys_addr(dma_addr);
515 * First, sync the memory before unmapping the entry
517 if ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL))
519 * bounce... copy the data back into the original buffer * and
520 * delete the bounce buffer.
522 __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE);
525 * Return the buffer to the free list by setting the corresponding
526 * entries to indicate the number of contigous entries available.
527 * While returning the entries to the free list, we merge the entries
528 * with slots below and above the pool being returned.
530 spin_lock_irqsave(&io_tlb_lock, flags);
532 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
533 io_tlb_list[index + nslots] : 0);
535 * Step 1: return the slots to the free list, merging the
536 * slots with superceeding slots
538 for (i = index + nslots - 1; i >= index; i--)
539 io_tlb_list[i] = ++count;
541 * Step 2: merge the returned slots with the preceding slots,
542 * if available (non zero)
544 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
545 io_tlb_list[i] = ++count;
547 spin_unlock_irqrestore(&io_tlb_lock, flags);
550 static void
551 sync_single(struct device *hwdev, char *dma_addr, size_t size,
552 int dir, int target)
554 struct swiotlb_phys_addr buffer = swiotlb_bus_to_phys_addr(dma_addr);
556 switch (target) {
557 case SYNC_FOR_CPU:
558 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
559 __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE);
560 else
561 BUG_ON(dir != DMA_TO_DEVICE);
562 break;
563 case SYNC_FOR_DEVICE:
564 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
565 __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE);
566 else
567 BUG_ON(dir != DMA_FROM_DEVICE);
568 break;
569 default:
570 BUG();
574 void *
575 swiotlb_alloc_coherent(struct device *hwdev, size_t size,
576 dma_addr_t *dma_handle, gfp_t flags)
578 dma_addr_t dev_addr;
579 void *ret;
580 int order = get_order(size);
581 u64 dma_mask = DMA_32BIT_MASK;
583 if (hwdev && hwdev->coherent_dma_mask)
584 dma_mask = hwdev->coherent_dma_mask;
586 ret = (void *)__get_free_pages(flags, order);
587 if (ret && !is_buffer_dma_capable(dma_mask, swiotlb_virt_to_bus(ret), size)) {
589 * The allocated memory isn't reachable by the device.
590 * Fall back on swiotlb_map_single().
592 free_pages((unsigned long) ret, order);
593 ret = NULL;
595 if (!ret) {
597 * We are either out of memory or the device can't DMA
598 * to GFP_DMA memory; fall back on
599 * swiotlb_map_single(), which will grab memory from
600 * the lowest available address range.
602 struct swiotlb_phys_addr buffer;
603 buffer.page = virt_to_page(NULL);
604 buffer.offset = 0;
605 ret = map_single(hwdev, buffer, size, DMA_FROM_DEVICE);
606 if (!ret)
607 return NULL;
610 memset(ret, 0, size);
611 dev_addr = swiotlb_virt_to_bus(ret);
613 /* Confirm address can be DMA'd by device */
614 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
615 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
616 (unsigned long long)dma_mask,
617 (unsigned long long)dev_addr);
619 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
620 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
621 return NULL;
623 *dma_handle = dev_addr;
624 return ret;
627 void
628 swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
629 dma_addr_t dma_handle)
631 WARN_ON(irqs_disabled());
632 if (!is_swiotlb_buffer(vaddr))
633 free_pages((unsigned long) vaddr, get_order(size));
634 else
635 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
636 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
639 static void
640 swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
643 * Ran out of IOMMU space for this operation. This is very bad.
644 * Unfortunately the drivers cannot handle this operation properly.
645 * unless they check for dma_mapping_error (most don't)
646 * When the mapping is small enough return a static buffer to limit
647 * the damage, or panic when the transfer is too big.
649 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
650 "device %s\n", size, dev ? dev->bus_id : "?");
652 if (size > io_tlb_overflow && do_panic) {
653 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
654 panic("DMA: Memory would be corrupted\n");
655 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
656 panic("DMA: Random memory would be DMAed\n");
661 * Map a single buffer of the indicated size for DMA in streaming mode. The
662 * physical address to use is returned.
664 * Once the device is given the dma address, the device owns this memory until
665 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
667 dma_addr_t
668 swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
669 int dir, struct dma_attrs *attrs)
671 dma_addr_t dev_addr = swiotlb_virt_to_bus(ptr);
672 void *map;
673 struct swiotlb_phys_addr buffer;
675 BUG_ON(dir == DMA_NONE);
677 * If the pointer passed in happens to be in the device's DMA window,
678 * we can safely return the device addr and not worry about bounce
679 * buffering it.
681 if (!address_needs_mapping(hwdev, dev_addr, size) &&
682 !range_needs_mapping(ptr, size))
683 return dev_addr;
686 * Oh well, have to allocate and map a bounce buffer.
688 buffer.page = virt_to_page(ptr);
689 buffer.offset = (unsigned long)ptr & ~PAGE_MASK;
690 map = map_single(hwdev, buffer, size, dir);
691 if (!map) {
692 swiotlb_full(hwdev, size, dir, 1);
693 map = io_tlb_overflow_buffer;
696 dev_addr = swiotlb_virt_to_bus(map);
699 * Ensure that the address returned is DMA'ble
701 if (address_needs_mapping(hwdev, dev_addr, size))
702 panic("map_single: bounce buffer is not DMA'ble");
704 return dev_addr;
706 EXPORT_SYMBOL(swiotlb_map_single_attrs);
708 dma_addr_t
709 swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
711 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
715 * Unmap a single streaming mode DMA translation. The dma_addr and size must
716 * match what was provided for in a previous swiotlb_map_single call. All
717 * other usages are undefined.
719 * After this call, reads by the cpu to the buffer are guaranteed to see
720 * whatever the device wrote there.
722 void
723 swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
724 size_t size, int dir, struct dma_attrs *attrs)
726 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
728 BUG_ON(dir == DMA_NONE);
729 if (is_swiotlb_buffer(dma_addr))
730 unmap_single(hwdev, dma_addr, size, dir);
731 else if (dir == DMA_FROM_DEVICE)
732 dma_mark_clean(dma_addr, size);
734 EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
736 void
737 swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
738 int dir)
740 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
743 * Make physical memory consistent for a single streaming mode DMA translation
744 * after a transfer.
746 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
747 * using the cpu, yet do not wish to teardown the dma mapping, you must
748 * call this function before doing so. At the next point you give the dma
749 * address back to the card, you must first perform a
750 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
752 static void
753 swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
754 size_t size, int dir, int target)
756 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
758 BUG_ON(dir == DMA_NONE);
759 if (is_swiotlb_buffer(dma_addr))
760 sync_single(hwdev, dma_addr, size, dir, target);
761 else if (dir == DMA_FROM_DEVICE)
762 dma_mark_clean(dma_addr, size);
765 void
766 swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
767 size_t size, int dir)
769 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
772 void
773 swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
774 size_t size, int dir)
776 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
780 * Same as above, but for a sub-range of the mapping.
782 static void
783 swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
784 unsigned long offset, size_t size,
785 int dir, int target)
787 char *dma_addr = swiotlb_bus_to_virt(dev_addr) + offset;
789 BUG_ON(dir == DMA_NONE);
790 if (is_swiotlb_buffer(dma_addr))
791 sync_single(hwdev, dma_addr, size, dir, target);
792 else if (dir == DMA_FROM_DEVICE)
793 dma_mark_clean(dma_addr, size);
796 void
797 swiotlb_sync_single_range_for_cpu(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,
801 SYNC_FOR_CPU);
804 void
805 swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
806 unsigned long offset, size_t size, int dir)
808 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
809 SYNC_FOR_DEVICE);
812 void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
813 struct dma_attrs *);
815 * Map a set of buffers described by scatterlist in streaming mode for DMA.
816 * This is the scatter-gather version of the above swiotlb_map_single
817 * interface. Here the scatter gather list elements are each tagged with the
818 * appropriate dma address and length. They are obtained via
819 * sg_dma_{address,length}(SG).
821 * NOTE: An implementation may be able to use a smaller number of
822 * DMA address/length pairs than there are SG table elements.
823 * (for example via virtual mapping capabilities)
824 * The routine returns the number of addr/length pairs actually
825 * used, at most nents.
827 * Device ownership issues as mentioned above for swiotlb_map_single are the
828 * same here.
831 swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
832 int dir, struct dma_attrs *attrs)
834 struct scatterlist *sg;
835 struct swiotlb_phys_addr buffer;
836 dma_addr_t dev_addr;
837 int i;
839 BUG_ON(dir == DMA_NONE);
841 for_each_sg(sgl, sg, nelems, i) {
842 dev_addr = swiotlb_sg_to_bus(sg);
843 if (range_needs_mapping(sg_virt(sg), sg->length) ||
844 address_needs_mapping(hwdev, dev_addr, sg->length)) {
845 void *map;
846 buffer.page = sg_page(sg);
847 buffer.offset = sg->offset;
848 map = map_single(hwdev, buffer, sg->length, dir);
849 if (!map) {
850 /* Don't panic here, we expect map_sg users
851 to do proper error handling. */
852 swiotlb_full(hwdev, sg->length, dir, 0);
853 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
854 attrs);
855 sgl[0].dma_length = 0;
856 return 0;
858 sg->dma_address = swiotlb_virt_to_bus(map);
859 } else
860 sg->dma_address = dev_addr;
861 sg->dma_length = sg->length;
863 return nelems;
865 EXPORT_SYMBOL(swiotlb_map_sg_attrs);
868 swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
869 int dir)
871 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
875 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
876 * concerning calls here are the same as for swiotlb_unmap_single() above.
878 void
879 swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
880 int nelems, int dir, struct dma_attrs *attrs)
882 struct scatterlist *sg;
883 int i;
885 BUG_ON(dir == DMA_NONE);
887 for_each_sg(sgl, sg, nelems, i) {
888 if (sg->dma_address != swiotlb_sg_to_bus(sg))
889 unmap_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
890 sg->dma_length, dir);
891 else if (dir == DMA_FROM_DEVICE)
892 dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length);
895 EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
897 void
898 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
899 int dir)
901 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
905 * Make physical memory consistent for a set of streaming mode DMA translations
906 * after a transfer.
908 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
909 * and usage.
911 static void
912 swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
913 int nelems, int dir, int target)
915 struct scatterlist *sg;
916 int i;
918 BUG_ON(dir == DMA_NONE);
920 for_each_sg(sgl, sg, nelems, i) {
921 if (sg->dma_address != swiotlb_sg_to_bus(sg))
922 sync_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
923 sg->dma_length, dir, target);
924 else if (dir == DMA_FROM_DEVICE)
925 dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length);
929 void
930 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
931 int nelems, int dir)
933 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
936 void
937 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
938 int nelems, int dir)
940 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
944 swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
946 return (dma_addr == swiotlb_virt_to_bus(io_tlb_overflow_buffer));
950 * Return whether the given device DMA address mask can be supported
951 * properly. For example, if your device can only drive the low 24-bits
952 * during bus mastering, then you would pass 0x00ffffff as the mask to
953 * this function.
956 swiotlb_dma_supported(struct device *hwdev, u64 mask)
958 return swiotlb_virt_to_bus(io_tlb_end - 1) <= mask;
961 EXPORT_SYMBOL(swiotlb_map_single);
962 EXPORT_SYMBOL(swiotlb_unmap_single);
963 EXPORT_SYMBOL(swiotlb_map_sg);
964 EXPORT_SYMBOL(swiotlb_unmap_sg);
965 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
966 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
967 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
968 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
969 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
970 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
971 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
972 EXPORT_SYMBOL(swiotlb_alloc_coherent);
973 EXPORT_SYMBOL(swiotlb_free_coherent);
974 EXPORT_SYMBOL(swiotlb_dma_supported);