2 * Dynamic DMA mapping support.
5 #include <linux/types.h>
7 #include <linux/string.h>
9 #include <linux/module.h>
11 #include <asm/proto.h>
13 int iommu_merge __read_mostly = 0;
14 EXPORT_SYMBOL(iommu_merge);
16 dma_addr_t bad_dma_address __read_mostly;
17 EXPORT_SYMBOL(bad_dma_address);
19 /* This tells the BIO block layer to assume merging. Default to off
20 because we cannot guarantee merging later. */
21 int iommu_bio_merge __read_mostly = 0;
22 EXPORT_SYMBOL(iommu_bio_merge);
24 int iommu_sac_force __read_mostly = 0;
25 EXPORT_SYMBOL(iommu_sac_force);
27 int no_iommu __read_mostly;
28 #ifdef CONFIG_IOMMU_DEBUG
29 int panic_on_overflow __read_mostly = 1;
30 int force_iommu __read_mostly = 1;
32 int panic_on_overflow __read_mostly = 0;
33 int force_iommu __read_mostly= 0;
36 /* Dummy device used for NULL arguments (normally ISA). Better would
37 be probably a smaller DMA mask, but this is bug-to-bug compatible
39 struct device fallback_dev = {
40 .bus_id = "fallback device",
41 .coherent_dma_mask = 0xffffffff,
42 .dma_mask = &fallback_dev.coherent_dma_mask,
45 /* Allocate DMA memory on node near device */
46 noinline static void *
47 dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
52 if (dev->bus == &pci_bus_type)
53 node = pcibus_to_node(to_pci_dev(dev)->bus);
56 node = numa_node_id();
58 if (node < first_node(node_online_map))
59 node = first_node(node_online_map);
61 page = alloc_pages_node(node, gfp, order);
62 return page ? page_address(page) : NULL;
66 * Allocate memory for a coherent mapping.
69 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
73 unsigned long dma_mask = 0;
78 dma_mask = dev->coherent_dma_mask;
80 dma_mask = 0xffffffff;
82 /* Don't invoke OOM killer */
85 /* Kludge to make it bug-to-bug compatible with i386. i386
86 uses the normal dma_mask for alloc_coherent. */
87 dma_mask &= *dev->dma_mask;
89 /* Why <=? Even when the mask is smaller than 4GB it is often
90 larger than 16MB and in this case we have a chance of
91 finding fitting memory in the next higher zone first. If
92 not retry with true GFP_DMA. -AK */
93 if (dma_mask <= 0xffffffff)
97 memory = dma_alloc_pages(dev, gfp, get_order(size));
103 bus = virt_to_bus(memory);
104 high = (bus + size) >= dma_mask;
106 if (force_iommu && !(gfp & GFP_DMA))
109 free_pages((unsigned long)memory,
112 /* Don't use the 16MB ZONE_DMA unless absolutely
113 needed. It's better to use remapping first. */
114 if (dma_mask < 0xffffffff && !(gfp & GFP_DMA)) {
115 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
119 /* Let low level make its own zone decisions */
120 gfp &= ~(GFP_DMA32|GFP_DMA);
122 if (dma_ops->alloc_coherent)
123 return dma_ops->alloc_coherent(dev, size,
128 memset(memory, 0, size);
130 *dma_handle = virt_to_bus(memory);
135 if (dma_ops->alloc_coherent) {
136 free_pages((unsigned long)memory, get_order(size));
137 gfp &= ~(GFP_DMA|GFP_DMA32);
138 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
141 if (dma_ops->map_simple) {
142 *dma_handle = dma_ops->map_simple(dev, memory,
144 PCI_DMA_BIDIRECTIONAL);
145 if (*dma_handle != bad_dma_address)
149 if (panic_on_overflow)
150 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
151 free_pages((unsigned long)memory, get_order(size));
154 EXPORT_SYMBOL(dma_alloc_coherent);
157 * Unmap coherent memory.
158 * The caller must ensure that the device has finished accessing the mapping.
160 void dma_free_coherent(struct device *dev, size_t size,
161 void *vaddr, dma_addr_t bus)
163 if (dma_ops->unmap_single)
164 dma_ops->unmap_single(dev, bus, size, 0);
165 free_pages((unsigned long)vaddr, get_order(size));
167 EXPORT_SYMBOL(dma_free_coherent);
169 int dma_supported(struct device *dev, u64 mask)
171 if (dma_ops->dma_supported)
172 return dma_ops->dma_supported(dev, mask);
174 /* Copied from i386. Doesn't make much sense, because it will
175 only work for pci_alloc_coherent.
176 The caller just has to use GFP_DMA in this case. */
177 if (mask < 0x00ffffff)
180 /* Tell the device to use SAC when IOMMU force is on. This
181 allows the driver to use cheaper accesses in some cases.
183 Problem with this is that if we overflow the IOMMU area and
184 return DAC as fallback address the device may not handle it
187 As a special case some controllers have a 39bit address
188 mode that is as efficient as 32bit (aic79xx). Don't force
189 SAC for these. Assume all masks <= 40 bits are of this
190 type. Normally this doesn't make any difference, but gives
191 more gentle handling of IOMMU overflow. */
192 if (iommu_sac_force && (mask >= 0xffffffffffULL)) {
193 printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
199 EXPORT_SYMBOL(dma_supported);
201 int dma_set_mask(struct device *dev, u64 mask)
203 if (!dev->dma_mask || !dma_supported(dev, mask))
205 *dev->dma_mask = mask;
208 EXPORT_SYMBOL(dma_set_mask);
210 /* iommu=[size][,noagp][,off][,force][,noforce][,leak][,memaper[=order]][,merge]
211 [,forcesac][,fullflush][,nomerge][,biomerge]
212 size set size of iommu (in bytes)
213 noagp don't initialize the AGP driver and use full aperture.
214 off don't use the IOMMU
215 leak turn on simple iommu leak tracing (only when CONFIG_IOMMU_LEAK is on)
216 memaper[=order] allocate an own aperture over RAM with size 32MB^order.
217 noforce don't force IOMMU usage. Default.
219 merge Do lazy merging. This may improve performance on some block devices.
220 Implies force (experimental)
221 biomerge Do merging at the BIO layer. This is more efficient than merge,
222 but should be only done with very big IOMMUs. Implies merge,force.
223 nomerge Don't do SG merging.
224 forcesac For SAC mode for masks <40bits (experimental)
225 fullflush Flush IOMMU on each allocation (default)
226 nofullflush Don't use IOMMU fullflush
227 allowed overwrite iommu off workarounds for specific chipsets.
228 soft Use software bounce buffering (default for Intel machines)
229 noaperture Don't touch the aperture for AGP.
231 __init int iommu_setup(char *p)
236 if (!strncmp(p,"off",3))
238 /* gart_parse_options has more force support */
239 if (!strncmp(p,"force",5))
241 if (!strncmp(p,"noforce",7)) {
246 if (!strncmp(p, "biomerge",8)) {
247 iommu_bio_merge = 4096;
251 if (!strncmp(p, "panic",5))
252 panic_on_overflow = 1;
253 if (!strncmp(p, "nopanic",7))
254 panic_on_overflow = 0;
255 if (!strncmp(p, "merge",5)) {
259 if (!strncmp(p, "nomerge",7))
261 if (!strncmp(p, "forcesac",8))
264 #ifdef CONFIG_SWIOTLB
265 if (!strncmp(p, "soft",4))
269 #ifdef CONFIG_GART_IOMMU
270 gart_parse_options(p);
273 p += strcspn(p, ",");