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>
12 #include <asm/iommu.h>
13 #include <asm/calgary.h>
15 int iommu_merge __read_mostly = 1;
16 EXPORT_SYMBOL(iommu_merge);
18 dma_addr_t bad_dma_address __read_mostly;
19 EXPORT_SYMBOL(bad_dma_address);
21 /* This tells the BIO block layer to assume merging. Default to off
22 because we cannot guarantee merging later. */
23 int iommu_bio_merge __read_mostly = 0;
24 EXPORT_SYMBOL(iommu_bio_merge);
26 static int iommu_sac_force __read_mostly = 0;
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 node = dev_to_node(dev);
58 node = numa_node_id();
60 if (node < first_node(node_online_map))
61 node = first_node(node_online_map);
63 page = alloc_pages_node(node, gfp, order);
64 return page ? page_address(page) : NULL;
68 * Allocate memory for a coherent mapping.
71 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
75 unsigned long dma_mask = 0;
80 dma_mask = dev->coherent_dma_mask;
82 dma_mask = DMA_32BIT_MASK;
84 /* Device not DMA able */
85 if (dev->dma_mask == NULL)
88 /* Don't invoke OOM killer */
91 /* Kludge to make it bug-to-bug compatible with i386. i386
92 uses the normal dma_mask for alloc_coherent. */
93 dma_mask &= *dev->dma_mask;
95 /* Why <=? Even when the mask is smaller than 4GB it is often
96 larger than 16MB and in this case we have a chance of
97 finding fitting memory in the next higher zone first. If
98 not retry with true GFP_DMA. -AK */
99 if (dma_mask <= DMA_32BIT_MASK)
103 memory = dma_alloc_pages(dev, gfp, get_order(size));
109 bus = virt_to_bus(memory);
110 high = (bus + size) >= dma_mask;
112 if (force_iommu && !(gfp & GFP_DMA))
115 free_pages((unsigned long)memory,
118 /* Don't use the 16MB ZONE_DMA unless absolutely
119 needed. It's better to use remapping first. */
120 if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
121 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
125 /* Let low level make its own zone decisions */
126 gfp &= ~(GFP_DMA32|GFP_DMA);
128 if (dma_ops->alloc_coherent)
129 return dma_ops->alloc_coherent(dev, size,
134 memset(memory, 0, size);
136 *dma_handle = virt_to_bus(memory);
141 if (dma_ops->alloc_coherent) {
142 free_pages((unsigned long)memory, get_order(size));
143 gfp &= ~(GFP_DMA|GFP_DMA32);
144 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
147 if (dma_ops->map_simple) {
148 *dma_handle = dma_ops->map_simple(dev, memory,
150 PCI_DMA_BIDIRECTIONAL);
151 if (*dma_handle != bad_dma_address)
155 if (panic_on_overflow)
156 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
157 free_pages((unsigned long)memory, get_order(size));
160 EXPORT_SYMBOL(dma_alloc_coherent);
163 * Unmap coherent memory.
164 * The caller must ensure that the device has finished accessing the mapping.
166 void dma_free_coherent(struct device *dev, size_t size,
167 void *vaddr, dma_addr_t bus)
169 WARN_ON(irqs_disabled()); /* for portability */
170 if (dma_ops->unmap_single)
171 dma_ops->unmap_single(dev, bus, size, 0);
172 free_pages((unsigned long)vaddr, get_order(size));
174 EXPORT_SYMBOL(dma_free_coherent);
176 static int forbid_dac __read_mostly;
178 int dma_supported(struct device *dev, u64 mask)
181 if (mask > 0xffffffff && forbid_dac > 0) {
185 printk(KERN_INFO "PCI: Disallowing DAC for device %s\n", dev->bus_id);
190 if (dma_ops->dma_supported)
191 return dma_ops->dma_supported(dev, mask);
193 /* Copied from i386. Doesn't make much sense, because it will
194 only work for pci_alloc_coherent.
195 The caller just has to use GFP_DMA in this case. */
196 if (mask < DMA_24BIT_MASK)
199 /* Tell the device to use SAC when IOMMU force is on. This
200 allows the driver to use cheaper accesses in some cases.
202 Problem with this is that if we overflow the IOMMU area and
203 return DAC as fallback address the device may not handle it
206 As a special case some controllers have a 39bit address
207 mode that is as efficient as 32bit (aic79xx). Don't force
208 SAC for these. Assume all masks <= 40 bits are of this
209 type. Normally this doesn't make any difference, but gives
210 more gentle handling of IOMMU overflow. */
211 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
212 printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
218 EXPORT_SYMBOL(dma_supported);
220 int dma_set_mask(struct device *dev, u64 mask)
222 if (!dev->dma_mask || !dma_supported(dev, mask))
224 *dev->dma_mask = mask;
227 EXPORT_SYMBOL(dma_set_mask);
230 * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
233 __init int iommu_setup(char *p)
241 if (!strncmp(p,"off",3))
243 /* gart_parse_options has more force support */
244 if (!strncmp(p,"force",5))
246 if (!strncmp(p,"noforce",7)) {
251 if (!strncmp(p, "biomerge",8)) {
252 iommu_bio_merge = 4096;
256 if (!strncmp(p, "panic",5))
257 panic_on_overflow = 1;
258 if (!strncmp(p, "nopanic",7))
259 panic_on_overflow = 0;
260 if (!strncmp(p, "merge",5)) {
264 if (!strncmp(p, "nomerge",7))
266 if (!strncmp(p, "forcesac",8))
268 if (!strncmp(p, "allowdac", 8))
270 if (!strncmp(p, "nodac", 5))
273 #ifdef CONFIG_SWIOTLB
274 if (!strncmp(p, "soft",4))
279 gart_parse_options(p);
282 #ifdef CONFIG_CALGARY_IOMMU
283 if (!strncmp(p, "calgary", 7))
285 #endif /* CONFIG_CALGARY_IOMMU */
287 p += strcspn(p, ",");
293 early_param("iommu", iommu_setup);
295 void __init pci_iommu_alloc(void)
298 * The order of these functions is important for
299 * fall-back/fail-over reasons
305 #ifdef CONFIG_CALGARY_IOMMU
309 detect_intel_iommu();
311 #ifdef CONFIG_SWIOTLB
316 static int __init pci_iommu_init(void)
318 #ifdef CONFIG_CALGARY_IOMMU
319 calgary_iommu_init();
332 void pci_iommu_shutdown(void)
334 gart_iommu_shutdown();
338 /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
340 static __devinit void via_no_dac(struct pci_dev *dev)
342 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
343 printk(KERN_INFO "PCI: VIA PCI bridge detected. Disabling DAC.\n");
347 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
349 /* Must execute after PCI subsystem */
350 fs_initcall(pci_iommu_init);