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/interface/memory.h>
29 #include <asm-i386/mach-xen/asm/swiotlb.h>
32 EXPORT_SYMBOL(swiotlb);
34 #define OFFSET(val,align) ((unsigned long)((val) & ( (align) - 1)))
36 #define SG_ENT_PHYS_ADDRESS(sg) (page_to_bus((sg)->page) + (sg)->offset)
39 * Maximum allowable number of contiguous slabs to map,
40 * must be a power of 2. What is the appropriate value ?
41 * The complexity of {map,unmap}_single is linearly dependent on this value.
43 #define IO_TLB_SEGSIZE 128
46 * log of the size of each IO TLB slab. The number of slabs is command line
49 #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);
100 setup_io_tlb_npages(char *str)
102 /* Unlike ia64, the size is aperture in megabytes, not 'slabs'! */
104 iotlb_nslabs = simple_strtoul(str, &str, 0) <<
106 iotlb_nslabs = ALIGN(iotlb_nslabs, IO_TLB_SEGSIZE);
107 /* Round up to power of two (xen_create_contiguous_region). */
108 while (iotlb_nslabs & (iotlb_nslabs-1))
109 iotlb_nslabs += iotlb_nslabs & ~(iotlb_nslabs-1);
114 * NB. 'force' enables the swiotlb, but doesn't force its use for
115 * every DMA like it does on native Linux. 'off' forcibly disables
116 * use of the swiotlb.
118 if (!strcmp(str, "force"))
120 else if (!strcmp(str, "off"))
124 __setup("swiotlb=", setup_io_tlb_npages);
125 /* make io_tlb_overflow tunable too? */
128 * Statically reserve bounce buffer space and initialize bounce buffer data
129 * structures for the software IO TLB used to implement the PCI DMA API.
132 swiotlb_init_with_default_size (size_t default_size)
134 unsigned long i, bytes;
137 iotlb_nslabs = (default_size >> IO_TLB_SHIFT);
138 iotlb_nslabs = ALIGN(iotlb_nslabs, IO_TLB_SEGSIZE);
139 /* Round up to power of two (xen_create_contiguous_region). */
140 while (iotlb_nslabs & (iotlb_nslabs-1))
141 iotlb_nslabs += iotlb_nslabs & ~(iotlb_nslabs-1);
144 bytes = iotlb_nslabs * (1UL << IO_TLB_SHIFT);
147 * Get IO TLB memory from the low pages
149 iotlb_virt_start = alloc_bootmem_low_pages(bytes);
150 if (!iotlb_virt_start)
151 panic("Cannot allocate SWIOTLB buffer!\n"
152 "Use dom0_mem Xen boot parameter to reserve\n"
153 "some DMA memory (e.g., dom0_mem=-128M).\n");
155 for (i = 0; i < iotlb_nslabs; i += IO_TLB_SEGSIZE) {
156 int rc = xen_create_contiguous_region(
157 (unsigned long)iotlb_virt_start + (i << IO_TLB_SHIFT),
158 get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT),
164 * Allocate and initialize the free list array. This array is used
165 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE.
167 io_tlb_list = alloc_bootmem(iotlb_nslabs * sizeof(int));
168 for (i = 0; i < iotlb_nslabs; i++)
169 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
171 io_tlb_orig_addr = alloc_bootmem(
172 iotlb_nslabs * sizeof(*io_tlb_orig_addr));
175 * Get the overflow emergency buffer
177 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
179 iotlb_pfn_start = __pa(iotlb_virt_start) >> PAGE_SHIFT;
180 iotlb_pfn_end = iotlb_pfn_start + (bytes >> PAGE_SHIFT);
182 printk(KERN_INFO "Software IO TLB enabled: \n"
183 " Aperture: %lu megabytes\n"
184 " Kernel range: 0x%016lx - 0x%016lx\n",
186 (unsigned long)iotlb_virt_start,
187 (unsigned long)iotlb_virt_start + bytes);
194 size_t defsz = 64 * (1 << 20); /* 64MB default size */
196 if (swiotlb_force == 1) {
198 } else if ((swiotlb_force != -1) &&
199 is_running_on_xen() &&
200 is_initial_xendomain()) {
201 /* Domain 0 always has a swiotlb. */
202 ram_end = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
203 if (ram_end <= 0x7ffff)
204 defsz = 2 * (1 << 20); /* 2MB on <2GB on systems. */
209 swiotlb_init_with_default_size(defsz);
211 printk(KERN_INFO "Software IO TLB disabled\n");
215 * We use __copy_to_user_inatomic to transfer to the host buffer because the
216 * buffer may be mapped read-only (e.g, in blkback driver) but lower-level
217 * drivers map the buffer for DMA_BIDIRECTIONAL access. This causes an
218 * unnecessary copy from the aperture to the host buffer, and a page fault.
221 __sync_single(struct phys_addr buffer, char *dma_addr, size_t size, int dir)
223 if (PageHighMem(buffer.page)) {
225 char *dev, *host, *kmp;
228 if (((bytes = len) + buffer.offset) > PAGE_SIZE)
229 bytes = PAGE_SIZE - buffer.offset;
230 kmp = kmap_atomic(buffer.page, KM_SWIOTLB);
231 dev = dma_addr + size - len;
232 host = kmp + buffer.offset;
233 if (dir == DMA_FROM_DEVICE) {
234 if (__copy_to_user_inatomic(host, dev, bytes))
237 memcpy(dev, host, bytes);
238 kunmap_atomic(kmp, KM_SWIOTLB);
244 char *host = (char *)phys_to_virt(
245 page_to_pseudophys(buffer.page)) + buffer.offset;
246 if (dir == DMA_FROM_DEVICE) {
247 if (__copy_to_user_inatomic(host, dma_addr, size))
249 } else if (dir == DMA_TO_DEVICE)
250 memcpy(dma_addr, host, size);
255 * Allocates bounce buffer and returns its kernel virtual address.
258 map_single(struct device *hwdev, struct phys_addr buffer, size_t size, int dir)
262 unsigned int nslots, stride, index, wrap;
266 * For mappings greater than a page, we limit the stride (and
267 * hence alignment) to a page size.
269 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
270 if (size > PAGE_SIZE)
271 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
278 * Find suitable number of IO TLB entries size that will fit this
279 * request and allocate a buffer from that IO TLB pool.
281 spin_lock_irqsave(&io_tlb_lock, flags);
283 wrap = index = ALIGN(io_tlb_index, stride);
285 if (index >= iotlb_nslabs)
290 * If we find a slot that indicates we have 'nslots'
291 * number of contiguous buffers, we allocate the
292 * buffers from that slot and mark the entries as '0'
293 * indicating unavailable.
295 if (io_tlb_list[index] >= nslots) {
298 for (i = index; i < (int)(index + nslots); i++)
301 (OFFSET(i, IO_TLB_SEGSIZE) !=
302 IO_TLB_SEGSIZE -1) && io_tlb_list[i];
304 io_tlb_list[i] = ++count;
305 dma_addr = iotlb_virt_start +
306 (index << IO_TLB_SHIFT);
309 * Update the indices to avoid searching in
313 ((index + nslots) < iotlb_nslabs
314 ? (index + nslots) : 0);
319 if (index >= iotlb_nslabs)
321 } while (index != wrap);
323 spin_unlock_irqrestore(&io_tlb_lock, flags);
327 spin_unlock_irqrestore(&io_tlb_lock, flags);
330 * Save away the mapping from the original address to the DMA address.
331 * This is needed when we sync the memory. Then we sync the buffer if
334 io_tlb_orig_addr[index] = buffer;
335 if ((dir == DMA_TO_DEVICE) || (dir == DMA_BIDIRECTIONAL))
336 __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE);
342 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
345 unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
348 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
349 int index = (dma_addr - iotlb_virt_start) >> IO_TLB_SHIFT;
350 struct phys_addr buffer = io_tlb_orig_addr[index];
353 * First, sync the memory before unmapping the entry
355 if ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL))
356 __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE);
359 * Return the buffer to the free list by setting the corresponding
360 * entries to indicate the number of contigous entries available.
361 * While returning the entries to the free list, we merge the entries
362 * with slots below and above the pool being returned.
364 spin_lock_irqsave(&io_tlb_lock, flags);
366 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
367 io_tlb_list[index + nslots] : 0);
369 * Step 1: return the slots to the free list, merging the
370 * slots with superceeding slots
372 for (i = index + nslots - 1; i >= index; i--)
373 io_tlb_list[i] = ++count;
375 * Step 2: merge the returned slots with the preceding slots,
376 * if available (non zero)
379 (OFFSET(i, IO_TLB_SEGSIZE) !=
380 IO_TLB_SEGSIZE -1) && io_tlb_list[i];
382 io_tlb_list[i] = ++count;
384 spin_unlock_irqrestore(&io_tlb_lock, flags);
388 sync_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
390 int index = (dma_addr - iotlb_virt_start) >> IO_TLB_SHIFT;
391 struct phys_addr buffer = io_tlb_orig_addr[index];
392 BUG_ON((dir != DMA_FROM_DEVICE) && (dir != DMA_TO_DEVICE));
393 __sync_single(buffer, dma_addr, size, dir);
397 swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
400 * Ran out of IOMMU space for this operation. This is very bad.
401 * Unfortunately the drivers cannot handle this operation properly.
402 * unless they check for pci_dma_mapping_error (most don't)
403 * When the mapping is small enough return a static buffer to limit
404 * the damage, or panic when the transfer is too big.
406 printk(KERN_ERR "PCI-DMA: Out of SW-IOMMU space for %lu bytes at "
407 "device %s\n", (unsigned long)size, dev ? dev->bus_id : "?");
409 if (size > io_tlb_overflow && do_panic) {
410 if (dir == PCI_DMA_FROMDEVICE || dir == PCI_DMA_BIDIRECTIONAL)
411 panic("PCI-DMA: Memory would be corrupted\n");
412 if (dir == PCI_DMA_TODEVICE || dir == PCI_DMA_BIDIRECTIONAL)
413 panic("PCI-DMA: Random memory would be DMAed\n");
418 * Map a single buffer of the indicated size for DMA in streaming mode. The
419 * PCI address to use is returned.
421 * Once the device is given the dma address, the device owns this memory until
422 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
425 swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
427 dma_addr_t dev_addr = virt_to_bus(ptr);
429 struct phys_addr buffer;
431 BUG_ON(dir == DMA_NONE);
434 * If the pointer passed in happens to be in the device's DMA window,
435 * we can safely return the device addr and not worry about bounce
438 if (!range_straddles_page_boundary(ptr, size) &&
439 !address_needs_mapping(hwdev, dev_addr))
443 * Oh well, have to allocate and map a bounce buffer.
445 buffer.page = virt_to_page(ptr);
446 buffer.offset = (unsigned long)ptr & ~PAGE_MASK;
447 map = map_single(hwdev, buffer, size, dir);
449 swiotlb_full(hwdev, size, dir, 1);
450 map = io_tlb_overflow_buffer;
453 dev_addr = virt_to_bus(map);
458 * Unmap a single streaming mode DMA translation. The dma_addr and size must
459 * match what was provided for in a previous swiotlb_map_single call. All
460 * other usages are undefined.
462 * After this call, reads by the cpu to the buffer are guaranteed to see
463 * whatever the device wrote there.
466 swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
469 BUG_ON(dir == DMA_NONE);
470 if (in_swiotlb_aperture(dev_addr))
471 unmap_single(hwdev, bus_to_virt(dev_addr), size, dir);
475 * Make physical memory consistent for a single streaming mode DMA translation
478 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
479 * using the cpu, yet do not wish to teardown the PCI dma mapping, you must
480 * call this function before doing so. At the next point you give the PCI dma
481 * address back to the card, you must first perform a
482 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
485 swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
486 size_t size, int dir)
488 BUG_ON(dir == DMA_NONE);
489 if (in_swiotlb_aperture(dev_addr))
490 sync_single(hwdev, bus_to_virt(dev_addr), size, dir);
494 swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
495 size_t size, int dir)
497 BUG_ON(dir == DMA_NONE);
498 if (in_swiotlb_aperture(dev_addr))
499 sync_single(hwdev, bus_to_virt(dev_addr), size, dir);
503 * Map a set of buffers described by scatterlist in streaming mode for DMA.
504 * This is the scatter-gather version of the above swiotlb_map_single
505 * interface. Here the scatter gather list elements are each tagged with the
506 * appropriate dma address and length. They are obtained via
507 * sg_dma_{address,length}(SG).
509 * NOTE: An implementation may be able to use a smaller number of
510 * DMA address/length pairs than there are SG table elements.
511 * (for example via virtual mapping capabilities)
512 * The routine returns the number of addr/length pairs actually
513 * used, at most nents.
515 * Device ownership issues as mentioned above for swiotlb_map_single are the
519 swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
522 struct phys_addr buffer;
527 BUG_ON(dir == DMA_NONE);
529 for (i = 0; i < nelems; i++, sg++) {
530 dev_addr = SG_ENT_PHYS_ADDRESS(sg);
531 if (address_needs_mapping(hwdev, dev_addr)) {
532 buffer.page = sg->page;
533 buffer.offset = sg->offset;
534 map = map_single(hwdev, buffer, sg->length, dir);
536 /* Don't panic here, we expect map_sg users
537 to do proper error handling. */
538 swiotlb_full(hwdev, sg->length, dir, 0);
539 swiotlb_unmap_sg(hwdev, sg - i, i, dir);
540 sg[0].dma_length = 0;
543 sg->dma_address = (dma_addr_t)virt_to_bus(map);
545 sg->dma_address = dev_addr;
546 sg->dma_length = sg->length;
552 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
553 * concerning calls here are the same as for swiotlb_unmap_single() above.
556 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
561 BUG_ON(dir == DMA_NONE);
563 for (i = 0; i < nelems; i++, sg++)
564 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
566 (void *)bus_to_virt(sg->dma_address),
567 sg->dma_length, dir);
571 * Make physical memory consistent for a set of streaming mode DMA translations
574 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
578 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
583 BUG_ON(dir == DMA_NONE);
585 for (i = 0; i < nelems; i++, sg++)
586 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
588 (void *)bus_to_virt(sg->dma_address),
589 sg->dma_length, dir);
593 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
598 BUG_ON(dir == DMA_NONE);
600 for (i = 0; i < nelems; i++, sg++)
601 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
603 (void *)bus_to_virt(sg->dma_address),
604 sg->dma_length, dir);
608 swiotlb_map_page(struct device *hwdev, struct page *page,
609 unsigned long offset, size_t size,
610 enum dma_data_direction direction)
612 struct phys_addr buffer;
616 dev_addr = page_to_bus(page) + offset;
617 if (address_needs_mapping(hwdev, dev_addr)) {
619 buffer.offset = offset;
620 map = map_single(hwdev, buffer, size, direction);
622 swiotlb_full(hwdev, size, direction, 1);
623 map = io_tlb_overflow_buffer;
625 dev_addr = (dma_addr_t)virt_to_bus(map);
632 swiotlb_unmap_page(struct device *hwdev, dma_addr_t dma_address,
633 size_t size, enum dma_data_direction direction)
635 BUG_ON(!valid_dma_direction(direction));
636 if (in_swiotlb_aperture(dma_address))
637 unmap_single(hwdev, bus_to_virt(dma_address), size, direction);
641 swiotlb_dma_mapping_error(dma_addr_t dma_addr)
643 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
647 * Return whether the given PCI device DMA address mask can be supported
648 * properly. For example, if your device can only drive the low 24-bits
649 * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
653 swiotlb_dma_supported (struct device *hwdev, u64 mask)
655 return (mask >= ((1UL << IO_TLB_DMA_BITS) - 1));
658 EXPORT_SYMBOL(swiotlb_init);
659 EXPORT_SYMBOL(swiotlb_map_single);
660 EXPORT_SYMBOL(swiotlb_unmap_single);
661 EXPORT_SYMBOL(swiotlb_map_sg);
662 EXPORT_SYMBOL(swiotlb_unmap_sg);
663 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
664 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
665 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
666 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
667 EXPORT_SYMBOL(swiotlb_map_page);
668 EXPORT_SYMBOL(swiotlb_unmap_page);
669 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
670 EXPORT_SYMBOL(swiotlb_dma_supported);