2 * Dynamic DMA mapping support.
5 #include <linux/types.h>
7 #include <linux/string.h>
9 #include <linux/module.h>
10 #include <linux/dmar.h>
11 #include <linux/bootmem.h>
12 #include <asm/proto.h>
15 #include <asm/calgary.h>
18 /* Dummy device used for NULL arguments (normally ISA). Better would
19 be probably a smaller DMA mask, but this is bug-to-bug compatible
21 struct device fallback_dev = {
22 .bus_id = "fallback device",
23 .coherent_dma_mask = DMA_32BIT_MASK,
24 .dma_mask = &fallback_dev.coherent_dma_mask,
27 /* Allocate DMA memory on node near device */
28 noinline static void *
29 dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
33 node = dev_to_node(dev);
35 return alloc_pages_node(node, gfp, order);
38 #define dma_alloc_from_coherent_mem(dev, size, handle, ret) (0)
39 #define dma_release_coherent(dev, order, vaddr) (0)
41 * Allocate memory for a coherent mapping.
44 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
49 unsigned long dma_mask = 0;
52 /* ignore region specifiers */
53 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
55 if (dma_alloc_from_coherent_mem(dev, size, dma_handle, &memory))
60 dma_mask = dev->coherent_dma_mask;
62 dma_mask = DMA_32BIT_MASK;
64 /* Device not DMA able */
65 if (dev->dma_mask == NULL)
68 /* Don't invoke OOM killer */
71 /* Why <=? Even when the mask is smaller than 4GB it is often
72 larger than 16MB and in this case we have a chance of
73 finding fitting memory in the next higher zone first. If
74 not retry with true GFP_DMA. -AK */
75 if (dma_mask <= DMA_32BIT_MASK)
79 page = dma_alloc_pages(dev, gfp, get_order(size));
85 bus = page_to_phys(page);
86 memory = page_address(page);
87 high = (bus + size) >= dma_mask;
89 if (force_iommu && !(gfp & GFP_DMA))
92 free_pages((unsigned long)memory,
95 /* Don't use the 16MB ZONE_DMA unless absolutely
96 needed. It's better to use remapping first. */
97 if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
98 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
102 /* Let low level make its own zone decisions */
103 gfp &= ~(GFP_DMA32|GFP_DMA);
105 if (dma_ops->alloc_coherent)
106 return dma_ops->alloc_coherent(dev, size,
111 memset(memory, 0, size);
118 if (dma_ops->alloc_coherent) {
119 free_pages((unsigned long)memory, get_order(size));
120 gfp &= ~(GFP_DMA|GFP_DMA32);
121 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
124 if (dma_ops->map_simple) {
125 *dma_handle = dma_ops->map_simple(dev, virt_to_phys(memory),
127 PCI_DMA_BIDIRECTIONAL);
128 if (*dma_handle != bad_dma_address)
132 if (panic_on_overflow)
133 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
134 free_pages((unsigned long)memory, get_order(size));
137 EXPORT_SYMBOL(dma_alloc_coherent);
140 * Unmap coherent memory.
141 * The caller must ensure that the device has finished accessing the mapping.
143 void dma_free_coherent(struct device *dev, size_t size,
144 void *vaddr, dma_addr_t bus)
146 int order = get_order(size);
147 WARN_ON(irqs_disabled()); /* for portability */
148 if (dma_release_coherent(dev, order, vaddr))
150 if (dma_ops->unmap_single)
151 dma_ops->unmap_single(dev, bus, size, 0);
152 free_pages((unsigned long)vaddr, order);
154 EXPORT_SYMBOL(dma_free_coherent);