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>
10 * Copyright (C) 2005 Keir Fraser <keir@xensource.com>
13 #include <linux/cache.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/spinlock.h>
18 #include <linux/string.h>
19 #include <linux/types.h>
20 #include <linux/ctype.h>
21 #include <linux/init.h>
22 #include <linux/bootmem.h>
23 #include <linux/highmem.h>
27 #include <asm/uaccess.h>
28 #include <xen/gnttab.h>
29 #include <xen/interface/memory.h>
30 #include <asm/gnttab_dma.h>
33 EXPORT_SYMBOL(swiotlb);
35 #define OFFSET(val,align) ((unsigned long)((val) & ( (align) - 1)))
38 * Maximum allowable number of contiguous slabs to map,
39 * must be a power of 2. What is the appropriate value ?
40 * The complexity of {map,unmap}_single is linearly dependent on this value.
42 #define IO_TLB_SEGSIZE 128
45 * log of the size of each IO TLB slab. The number of slabs is command line
48 #define IO_TLB_SHIFT 11
52 static char *iotlb_virt_start;
53 static unsigned long iotlb_nslabs;
56 * Used to do a quick range check in swiotlb_unmap_single and
57 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
60 static unsigned long iotlb_pfn_start, iotlb_pfn_end;
62 /* Does the given dma address reside within the swiotlb aperture? */
63 static inline int in_swiotlb_aperture(dma_addr_t dev_addr)
65 unsigned long pfn = mfn_to_local_pfn(dev_addr >> PAGE_SHIFT);
66 return (pfn_valid(pfn)
67 && (pfn >= iotlb_pfn_start)
68 && (pfn < iotlb_pfn_end));
72 * When the IOMMU overflows we return a fallback buffer. This sets the size.
74 static unsigned long io_tlb_overflow = 32*1024;
76 void *io_tlb_overflow_buffer;
79 * This is a free list describing the number of free entries available from
82 static unsigned int *io_tlb_list;
83 static unsigned int io_tlb_index;
86 * We need to save away the original address corresponding to a mapped entry
87 * for the sync operations.
89 static struct phys_addr {
95 * Protect the above data structures in the map and unmap calls
97 static DEFINE_SPINLOCK(io_tlb_lock);
99 static unsigned int dma_bits;
100 static unsigned int __initdata max_dma_bits = 32;
102 setup_dma_bits(char *str)
104 max_dma_bits = simple_strtoul(str, NULL, 0);
107 __setup("dma_bits=", setup_dma_bits);
110 setup_io_tlb_npages(char *str)
112 /* Unlike ia64, the size is aperture in megabytes, not 'slabs'! */
114 iotlb_nslabs = simple_strtoul(str, &str, 0) <<
116 iotlb_nslabs = ALIGN(iotlb_nslabs, IO_TLB_SEGSIZE);
117 /* Round up to power of two (xen_create_contiguous_region). */
118 while (iotlb_nslabs & (iotlb_nslabs-1))
119 iotlb_nslabs += iotlb_nslabs & ~(iotlb_nslabs-1);
124 * NB. 'force' enables the swiotlb, but doesn't force its use for
125 * every DMA like it does on native Linux. 'off' forcibly disables
126 * use of the swiotlb.
128 if (!strcmp(str, "force"))
130 else if (!strcmp(str, "off"))
134 __setup("swiotlb=", setup_io_tlb_npages);
135 /* make io_tlb_overflow tunable too? */
138 * Statically reserve bounce buffer space and initialize bounce buffer data
139 * structures for the software IO TLB used to implement the PCI DMA API.
142 swiotlb_init_with_default_size(size_t default_size)
144 unsigned long i, bytes;
148 iotlb_nslabs = (default_size >> IO_TLB_SHIFT);
149 iotlb_nslabs = ALIGN(iotlb_nslabs, IO_TLB_SEGSIZE);
150 /* Round up to power of two (xen_create_contiguous_region). */
151 while (iotlb_nslabs & (iotlb_nslabs-1))
152 iotlb_nslabs += iotlb_nslabs & ~(iotlb_nslabs-1);
155 bytes = iotlb_nslabs * (1UL << IO_TLB_SHIFT);
158 * Get IO TLB memory from the low pages
160 iotlb_virt_start = alloc_bootmem_low_pages(bytes);
161 if (!iotlb_virt_start)
162 panic("Cannot allocate SWIOTLB buffer!\n");
164 dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT;
165 for (i = 0; i < iotlb_nslabs; i += IO_TLB_SEGSIZE) {
167 rc = xen_create_contiguous_region(
168 (unsigned long)iotlb_virt_start + (i << IO_TLB_SHIFT),
169 get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT),
171 } while (rc && dma_bits++ < max_dma_bits);
174 panic("No suitable physical memory available for SWIOTLB buffer!\n"
175 "Use dom0_mem Xen boot parameter to reserve\n"
176 "some DMA memory (e.g., dom0_mem=-128M).\n");
179 free_bootmem(__pa(iotlb_virt_start + i), bytes - i);
181 for (dma_bits = 0; i > 0; i -= IO_TLB_SEGSIZE << IO_TLB_SHIFT) {
182 unsigned int bits = fls64(virt_to_bus(iotlb_virt_start + i - 1));
192 * Allocate and initialize the free list array. This array is used
193 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE.
195 io_tlb_list = alloc_bootmem(iotlb_nslabs * sizeof(int));
196 for (i = 0; i < iotlb_nslabs; i++)
197 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
199 io_tlb_orig_addr = alloc_bootmem(
200 iotlb_nslabs * sizeof(*io_tlb_orig_addr));
203 * Get the overflow emergency buffer
205 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
206 if (!io_tlb_overflow_buffer)
207 panic("Cannot allocate SWIOTLB overflow buffer!\n");
210 rc = xen_create_contiguous_region(
211 (unsigned long)io_tlb_overflow_buffer,
212 get_order(io_tlb_overflow),
214 } while (rc && dma_bits++ < max_dma_bits);
216 panic("No suitable physical memory available for SWIOTLB overflow buffer!\n");
218 iotlb_pfn_start = __pa(iotlb_virt_start) >> PAGE_SHIFT;
219 iotlb_pfn_end = iotlb_pfn_start + (bytes >> PAGE_SHIFT);
221 printk(KERN_INFO "Software IO TLB enabled: \n"
222 " Aperture: %lu megabytes\n"
223 " Kernel range: %p - %p\n"
224 " Address size: %u bits\n",
226 iotlb_virt_start, iotlb_virt_start + bytes,
234 size_t defsz = 64 * (1 << 20); /* 64MB default size */
236 if (swiotlb_force == 1) {
238 } else if ((swiotlb_force != -1) &&
239 is_running_on_xen() &&
240 is_initial_xendomain()) {
241 /* Domain 0 always has a swiotlb. */
242 ram_end = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
243 if (ram_end <= 0x7ffff)
244 defsz = 2 * (1 << 20); /* 2MB on <2GB on systems. */
249 swiotlb_init_with_default_size(defsz);
251 printk(KERN_INFO "Software IO TLB disabled\n");
255 * We use __copy_to_user_inatomic to transfer to the host buffer because the
256 * buffer may be mapped read-only (e.g, in blkback driver) but lower-level
257 * drivers map the buffer for DMA_BIDIRECTIONAL access. This causes an
258 * unnecessary copy from the aperture to the host buffer, and a page fault.
261 __sync_single(struct phys_addr buffer, char *dma_addr, size_t size, int dir)
263 if (PageHighMem(buffer.page)) {
265 char *dev, *host, *kmp;
270 if (((bytes = len) + buffer.offset) > PAGE_SIZE)
271 bytes = PAGE_SIZE - buffer.offset;
272 local_irq_save(flags); /* protects KM_BOUNCE_READ */
273 kmp = kmap_atomic(buffer.page, KM_BOUNCE_READ);
274 dev = dma_addr + size - len;
275 host = kmp + buffer.offset;
276 if (dir == DMA_FROM_DEVICE) {
277 if (__copy_to_user_inatomic(host, dev, bytes))
280 memcpy(dev, host, bytes);
281 kunmap_atomic(kmp, KM_BOUNCE_READ);
282 local_irq_restore(flags);
288 char *host = (char *)phys_to_virt(
289 page_to_pseudophys(buffer.page)) + buffer.offset;
290 if (dir == DMA_FROM_DEVICE) {
291 if (__copy_to_user_inatomic(host, dma_addr, size))
293 } else if (dir == DMA_TO_DEVICE)
294 memcpy(dma_addr, host, size);
299 * Allocates bounce buffer and returns its kernel virtual address.
302 map_single(struct device *hwdev, struct phys_addr buffer, size_t size, int dir)
306 unsigned int nslots, stride, index, wrap;
307 struct phys_addr slot_buf;
311 * For mappings greater than a page, we limit the stride (and
312 * hence alignment) to a page size.
314 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
315 if (size > PAGE_SIZE)
316 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
323 * Find suitable number of IO TLB entries size that will fit this
324 * request and allocate a buffer from that IO TLB pool.
326 spin_lock_irqsave(&io_tlb_lock, flags);
328 wrap = index = ALIGN(io_tlb_index, stride);
330 if (index >= iotlb_nslabs)
335 * If we find a slot that indicates we have 'nslots'
336 * number of contiguous buffers, we allocate the
337 * buffers from that slot and mark the entries as '0'
338 * indicating unavailable.
340 if (io_tlb_list[index] >= nslots) {
343 for (i = index; i < (int)(index + nslots); i++)
346 (OFFSET(i, IO_TLB_SEGSIZE) !=
347 IO_TLB_SEGSIZE -1) && io_tlb_list[i];
349 io_tlb_list[i] = ++count;
350 dma_addr = iotlb_virt_start +
351 (index << IO_TLB_SHIFT);
354 * Update the indices to avoid searching in
358 ((index + nslots) < iotlb_nslabs
359 ? (index + nslots) : 0);
364 if (index >= iotlb_nslabs)
366 } while (index != wrap);
368 spin_unlock_irqrestore(&io_tlb_lock, flags);
372 spin_unlock_irqrestore(&io_tlb_lock, flags);
375 * Save away the mapping from the original address to the DMA address.
376 * This is needed when we sync the memory. Then we sync the buffer if
380 for (i = 0; i < nslots; i++) {
381 slot_buf.page += slot_buf.offset >> PAGE_SHIFT;
382 slot_buf.offset &= PAGE_SIZE - 1;
383 io_tlb_orig_addr[index+i] = slot_buf;
384 slot_buf.offset += 1 << IO_TLB_SHIFT;
386 if ((dir == DMA_TO_DEVICE) || (dir == DMA_BIDIRECTIONAL))
387 __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE);
392 static struct phys_addr dma_addr_to_phys_addr(char *dma_addr)
394 int index = (dma_addr - iotlb_virt_start) >> IO_TLB_SHIFT;
395 struct phys_addr buffer = io_tlb_orig_addr[index];
396 buffer.offset += (long)dma_addr & ((1 << IO_TLB_SHIFT) - 1);
397 buffer.page += buffer.offset >> PAGE_SHIFT;
398 buffer.offset &= PAGE_SIZE - 1;
403 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
406 unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
409 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
410 int index = (dma_addr - iotlb_virt_start) >> IO_TLB_SHIFT;
411 struct phys_addr buffer = dma_addr_to_phys_addr(dma_addr);
414 * First, sync the memory before unmapping the entry
416 if ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL))
417 __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE);
420 * Return the buffer to the free list by setting the corresponding
421 * entries to indicate the number of contigous entries available.
422 * While returning the entries to the free list, we merge the entries
423 * with slots below and above the pool being returned.
425 spin_lock_irqsave(&io_tlb_lock, flags);
427 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
428 io_tlb_list[index + nslots] : 0);
430 * Step 1: return the slots to the free list, merging the
431 * slots with superceeding slots
433 for (i = index + nslots - 1; i >= index; i--)
434 io_tlb_list[i] = ++count;
436 * Step 2: merge the returned slots with the preceding slots,
437 * if available (non zero)
440 (OFFSET(i, IO_TLB_SEGSIZE) !=
441 IO_TLB_SEGSIZE -1) && io_tlb_list[i];
443 io_tlb_list[i] = ++count;
445 spin_unlock_irqrestore(&io_tlb_lock, flags);
449 sync_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
451 struct phys_addr buffer = dma_addr_to_phys_addr(dma_addr);
452 BUG_ON((dir != DMA_FROM_DEVICE) && (dir != DMA_TO_DEVICE));
453 __sync_single(buffer, dma_addr, size, dir);
457 swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
460 * Ran out of IOMMU space for this operation. This is very bad.
461 * Unfortunately the drivers cannot handle this operation properly.
462 * unless they check for pci_dma_mapping_error (most don't)
463 * When the mapping is small enough return a static buffer to limit
464 * the damage, or panic when the transfer is too big.
466 printk(KERN_ERR "PCI-DMA: Out of SW-IOMMU space for %zu bytes at "
467 "device %s\n", (unsigned long)size, dev ? dev->bus_id : "?");
469 if (size > io_tlb_overflow && do_panic) {
470 if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
471 panic("PCI-DMA: Memory would be corrupted\n");
472 if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
473 panic("PCI-DMA: Random memory would be DMAed\n");
478 * Map a single buffer of the indicated size for DMA in streaming mode. The
479 * PCI address to use is returned.
481 * Once the device is given the dma address, the device owns this memory until
482 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
485 swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
487 dma_addr_t dev_addr = gnttab_dma_map_page(virt_to_page(ptr)) +
490 struct phys_addr buffer;
492 BUG_ON(dir == DMA_NONE);
495 * If the pointer passed in happens to be in the device's DMA window,
496 * we can safely return the device addr and not worry about bounce
499 if (!range_straddles_page_boundary(__pa(ptr), size) &&
500 !address_needs_mapping(hwdev, dev_addr))
504 * Oh well, have to allocate and map a bounce buffer.
506 gnttab_dma_unmap_page(dev_addr);
507 buffer.page = virt_to_page(ptr);
508 buffer.offset = (unsigned long)ptr & ~PAGE_MASK;
509 map = map_single(hwdev, buffer, size, dir);
511 swiotlb_full(hwdev, size, dir, 1);
512 map = io_tlb_overflow_buffer;
515 dev_addr = virt_to_bus(map);
520 * Unmap a single streaming mode DMA translation. The dma_addr and size must
521 * match what was provided for in a previous swiotlb_map_single call. All
522 * other usages are undefined.
524 * After this call, reads by the cpu to the buffer are guaranteed to see
525 * whatever the device wrote there.
528 swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
531 BUG_ON(dir == DMA_NONE);
532 if (in_swiotlb_aperture(dev_addr))
533 unmap_single(hwdev, bus_to_virt(dev_addr), size, dir);
535 gnttab_dma_unmap_page(dev_addr);
539 * Make physical memory consistent for a single streaming mode DMA translation
542 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
543 * using the cpu, yet do not wish to teardown the PCI dma mapping, you must
544 * call this function before doing so. At the next point you give the PCI dma
545 * address back to the card, you must first perform a
546 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
549 swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
550 size_t size, int dir)
552 BUG_ON(dir == DMA_NONE);
553 if (in_swiotlb_aperture(dev_addr))
554 sync_single(hwdev, bus_to_virt(dev_addr), size, dir);
558 swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
559 size_t size, int dir)
561 BUG_ON(dir == DMA_NONE);
562 if (in_swiotlb_aperture(dev_addr))
563 sync_single(hwdev, bus_to_virt(dev_addr), size, dir);
567 * Map a set of buffers described by scatterlist in streaming mode for DMA.
568 * This is the scatter-gather version of the above swiotlb_map_single
569 * interface. Here the scatter gather list elements are each tagged with the
570 * appropriate dma address and length. They are obtained via
571 * sg_dma_{address,length}(SG).
573 * NOTE: An implementation may be able to use a smaller number of
574 * DMA address/length pairs than there are SG table elements.
575 * (for example via virtual mapping capabilities)
576 * The routine returns the number of addr/length pairs actually
577 * used, at most nents.
579 * Device ownership issues as mentioned above for swiotlb_map_single are the
583 swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
586 struct scatterlist *sg;
587 struct phys_addr buffer;
592 BUG_ON(dir == DMA_NONE);
594 for_each_sg(sgl, sg, nelems, i) {
595 dev_addr = gnttab_dma_map_page(sg_page(sg)) + sg->offset;
597 if (range_straddles_page_boundary(page_to_pseudophys(sg_page(sg))
598 + sg->offset, sg->length)
599 || address_needs_mapping(hwdev, dev_addr)) {
600 gnttab_dma_unmap_page(dev_addr);
601 buffer.page = sg_page(sg);
602 buffer.offset = sg->offset;
603 map = map_single(hwdev, buffer, sg->length, dir);
605 /* Don't panic here, we expect map_sg users
606 to do proper error handling. */
607 swiotlb_full(hwdev, sg->length, dir, 0);
608 swiotlb_unmap_sg(hwdev, sgl, i, dir);
609 sgl[0].dma_length = 0;
612 sg->dma_address = virt_to_bus(map);
614 sg->dma_address = dev_addr;
615 sg->dma_length = sg->length;
621 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
622 * concerning calls here are the same as for swiotlb_unmap_single() above.
625 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
628 struct scatterlist *sg;
631 BUG_ON(dir == DMA_NONE);
633 for_each_sg(sgl, sg, nelems, i) {
634 if (in_swiotlb_aperture(sg->dma_address))
635 unmap_single(hwdev, bus_to_virt(sg->dma_address),
636 sg->dma_length, dir);
638 gnttab_dma_unmap_page(sg->dma_address);
643 * Make physical memory consistent for a set of streaming mode DMA translations
646 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
650 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sgl,
653 struct scatterlist *sg;
656 BUG_ON(dir == DMA_NONE);
658 for_each_sg(sgl, sg, nelems, i) {
659 if (in_swiotlb_aperture(sg->dma_address))
660 sync_single(hwdev, bus_to_virt(sg->dma_address),
661 sg->dma_length, dir);
666 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sgl,
669 struct scatterlist *sg;
672 BUG_ON(dir == DMA_NONE);
674 for_each_sg(sgl, sg, nelems, i) {
675 if (in_swiotlb_aperture(sg->dma_address))
676 sync_single(hwdev, bus_to_virt(sg->dma_address),
677 sg->dma_length, dir);
681 #ifdef CONFIG_HIGHMEM
684 swiotlb_map_page(struct device *hwdev, struct page *page,
685 unsigned long offset, size_t size,
686 enum dma_data_direction direction)
688 struct phys_addr buffer;
692 dev_addr = gnttab_dma_map_page(page) + offset;
693 if (address_needs_mapping(hwdev, dev_addr)) {
694 gnttab_dma_unmap_page(dev_addr);
696 buffer.offset = offset;
697 map = map_single(hwdev, buffer, size, direction);
699 swiotlb_full(hwdev, size, direction, 1);
700 map = io_tlb_overflow_buffer;
702 dev_addr = (dma_addr_t)virt_to_bus(map);
709 swiotlb_unmap_page(struct device *hwdev, dma_addr_t dma_address,
710 size_t size, enum dma_data_direction direction)
712 BUG_ON(direction == DMA_NONE);
713 if (in_swiotlb_aperture(dma_address))
714 unmap_single(hwdev, bus_to_virt(dma_address), size, direction);
716 gnttab_dma_unmap_page(dma_address);
722 swiotlb_dma_mapping_error(dma_addr_t dma_addr)
724 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
728 * Return whether the given PCI device DMA address mask can be supported
729 * properly. For example, if your device can only drive the low 24-bits
730 * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
734 swiotlb_dma_supported (struct device *hwdev, u64 mask)
736 return (mask >= ((1UL << dma_bits) - 1));
739 EXPORT_SYMBOL(swiotlb_map_single);
740 EXPORT_SYMBOL(swiotlb_unmap_single);
741 EXPORT_SYMBOL(swiotlb_map_sg);
742 EXPORT_SYMBOL(swiotlb_unmap_sg);
743 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
744 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
745 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
746 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
747 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
748 EXPORT_SYMBOL(swiotlb_dma_supported);