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>
12 #include <asm/calgary.h>
14 int iommu_merge __read_mostly = 0;
15 EXPORT_SYMBOL(iommu_merge);
17 dma_addr_t bad_dma_address __read_mostly;
18 EXPORT_SYMBOL(bad_dma_address);
20 /* This tells the BIO block layer to assume merging. Default to off
21 because we cannot guarantee merging later. */
22 int iommu_bio_merge __read_mostly = 0;
23 EXPORT_SYMBOL(iommu_bio_merge);
25 int iommu_sac_force __read_mostly = 0;
26 EXPORT_SYMBOL(iommu_sac_force);
28 int no_iommu __read_mostly;
29 #ifdef CONFIG_IOMMU_DEBUG
30 int panic_on_overflow __read_mostly = 1;
31 int force_iommu __read_mostly = 1;
33 int panic_on_overflow __read_mostly = 0;
34 int force_iommu __read_mostly= 0;
37 /* Set this to 1 if there is a HW IOMMU in the system */
38 int iommu_detected __read_mostly = 0;
40 /* Dummy device used for NULL arguments (normally ISA). Better would
41 be probably a smaller DMA mask, but this is bug-to-bug compatible
43 struct device fallback_dev = {
44 .bus_id = "fallback device",
45 .coherent_dma_mask = DMA_32BIT_MASK,
46 .dma_mask = &fallback_dev.coherent_dma_mask,
49 /* Allocate DMA memory on node near device */
50 noinline static void *
51 dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
56 if (dev->bus == &pci_bus_type)
57 node = pcibus_to_node(to_pci_dev(dev)->bus);
60 node = numa_node_id();
62 if (node < first_node(node_online_map))
63 node = first_node(node_online_map);
65 page = alloc_pages_node(node, gfp, order);
66 return page ? page_address(page) : NULL;
70 * Allocate memory for a coherent mapping.
73 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
77 unsigned long dma_mask = 0;
82 dma_mask = dev->coherent_dma_mask;
84 dma_mask = DMA_32BIT_MASK;
86 /* Don't invoke OOM killer */
89 /* Kludge to make it bug-to-bug compatible with i386. i386
90 uses the normal dma_mask for alloc_coherent. */
91 dma_mask &= *dev->dma_mask;
93 /* Why <=? Even when the mask is smaller than 4GB it is often
94 larger than 16MB and in this case we have a chance of
95 finding fitting memory in the next higher zone first. If
96 not retry with true GFP_DMA. -AK */
97 if (dma_mask <= DMA_32BIT_MASK)
101 memory = dma_alloc_pages(dev, gfp, get_order(size));
107 bus = virt_to_bus(memory);
108 high = (bus + size) >= dma_mask;
110 if (force_iommu && !(gfp & GFP_DMA))
113 free_pages((unsigned long)memory,
116 /* Don't use the 16MB ZONE_DMA unless absolutely
117 needed. It's better to use remapping first. */
118 if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
119 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
123 /* Let low level make its own zone decisions */
124 gfp &= ~(GFP_DMA32|GFP_DMA);
126 if (dma_ops->alloc_coherent)
127 return dma_ops->alloc_coherent(dev, size,
132 memset(memory, 0, size);
134 *dma_handle = virt_to_bus(memory);
139 if (dma_ops->alloc_coherent) {
140 free_pages((unsigned long)memory, get_order(size));
141 gfp &= ~(GFP_DMA|GFP_DMA32);
142 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
145 if (dma_ops->map_simple) {
146 *dma_handle = dma_ops->map_simple(dev, memory,
148 PCI_DMA_BIDIRECTIONAL);
149 if (*dma_handle != bad_dma_address)
153 if (panic_on_overflow)
154 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
155 free_pages((unsigned long)memory, get_order(size));
158 EXPORT_SYMBOL(dma_alloc_coherent);
161 * Unmap coherent memory.
162 * The caller must ensure that the device has finished accessing the mapping.
164 void dma_free_coherent(struct device *dev, size_t size,
165 void *vaddr, dma_addr_t bus)
167 if (dma_ops->unmap_single)
168 dma_ops->unmap_single(dev, bus, size, 0);
169 free_pages((unsigned long)vaddr, get_order(size));
171 EXPORT_SYMBOL(dma_free_coherent);
173 static int forbid_dac __read_mostly;
175 int dma_supported(struct device *dev, u64 mask)
178 if (mask > 0xffffffff && forbid_dac > 0) {
182 printk(KERN_INFO "PCI: Disallowing DAC for device %s\n", dev->bus_id);
187 if (dma_ops->dma_supported)
188 return dma_ops->dma_supported(dev, mask);
190 /* Copied from i386. Doesn't make much sense, because it will
191 only work for pci_alloc_coherent.
192 The caller just has to use GFP_DMA in this case. */
193 if (mask < DMA_24BIT_MASK)
196 /* Tell the device to use SAC when IOMMU force is on. This
197 allows the driver to use cheaper accesses in some cases.
199 Problem with this is that if we overflow the IOMMU area and
200 return DAC as fallback address the device may not handle it
203 As a special case some controllers have a 39bit address
204 mode that is as efficient as 32bit (aic79xx). Don't force
205 SAC for these. Assume all masks <= 40 bits are of this
206 type. Normally this doesn't make any difference, but gives
207 more gentle handling of IOMMU overflow. */
208 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
209 printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
215 EXPORT_SYMBOL(dma_supported);
217 int dma_set_mask(struct device *dev, u64 mask)
219 if (!dev->dma_mask || !dma_supported(dev, mask))
221 *dev->dma_mask = mask;
224 EXPORT_SYMBOL(dma_set_mask);
226 /* iommu=[size][,noagp][,off][,force][,noforce][,leak][,memaper[=order]][,merge]
227 [,forcesac][,fullflush][,nomerge][,biomerge]
228 size set size of iommu (in bytes)
229 noagp don't initialize the AGP driver and use full aperture.
230 off don't use the IOMMU
231 leak turn on simple iommu leak tracing (only when CONFIG_IOMMU_LEAK is on)
232 memaper[=order] allocate an own aperture over RAM with size 32MB^order.
233 noforce don't force IOMMU usage. Default.
235 merge Do lazy merging. This may improve performance on some block devices.
236 Implies force (experimental)
237 biomerge Do merging at the BIO layer. This is more efficient than merge,
238 but should be only done with very big IOMMUs. Implies merge,force.
239 nomerge Don't do SG merging.
240 forcesac For SAC mode for masks <40bits (experimental)
241 fullflush Flush IOMMU on each allocation (default)
242 nofullflush Don't use IOMMU fullflush
243 allowed overwrite iommu off workarounds for specific chipsets.
244 soft Use software bounce buffering (default for Intel machines)
245 noaperture Don't touch the aperture for AGP.
246 allowdac Allow DMA >4GB
247 nodac Forbid DMA >4GB
248 panic Force panic when IOMMU overflows
250 __init int iommu_setup(char *p)
258 if (!strncmp(p,"off",3))
260 /* gart_parse_options has more force support */
261 if (!strncmp(p,"force",5))
263 if (!strncmp(p,"noforce",7)) {
268 if (!strncmp(p, "biomerge",8)) {
269 iommu_bio_merge = 4096;
273 if (!strncmp(p, "panic",5))
274 panic_on_overflow = 1;
275 if (!strncmp(p, "nopanic",7))
276 panic_on_overflow = 0;
277 if (!strncmp(p, "merge",5)) {
281 if (!strncmp(p, "nomerge",7))
283 if (!strncmp(p, "forcesac",8))
285 if (!strncmp(p, "allowdac", 8))
287 if (!strncmp(p, "nodac", 5))
290 #ifdef CONFIG_SWIOTLB
291 if (!strncmp(p, "soft",4))
296 gart_parse_options(p);
299 p += strcspn(p, ",");
305 early_param("iommu", iommu_setup);
307 void __init pci_iommu_alloc(void)
310 * The order of these functions is important for
311 * fall-back/fail-over reasons
317 #ifdef CONFIG_CALGARY_IOMMU
321 #ifdef CONFIG_SWIOTLB
326 static int __init pci_iommu_init(void)
328 #ifdef CONFIG_CALGARY_IOMMU
329 calgary_iommu_init();
340 /* Must execute after PCI subsystem */
341 fs_initcall(pci_iommu_init);