1 #include <linux/dma-mapping.h>
2 #include <linux/dmar.h>
3 #include <linux/bootmem.h>
9 #include <asm/calgary.h>
11 int forbid_dac __read_mostly;
12 EXPORT_SYMBOL(forbid_dac);
14 const struct dma_mapping_ops *dma_ops;
15 EXPORT_SYMBOL(dma_ops);
17 int iommu_sac_force __read_mostly = 0;
19 #ifdef CONFIG_IOMMU_DEBUG
20 int panic_on_overflow __read_mostly = 1;
21 int force_iommu __read_mostly = 1;
23 int panic_on_overflow __read_mostly = 0;
24 int force_iommu __read_mostly = 0;
27 int iommu_merge __read_mostly = 0;
29 int no_iommu __read_mostly;
30 /* Set this to 1 if there is a HW IOMMU in the system */
31 int iommu_detected __read_mostly = 0;
33 /* This tells the BIO block layer to assume merging. Default to off
34 because we cannot guarantee merging later. */
35 int iommu_bio_merge __read_mostly = 0;
36 EXPORT_SYMBOL(iommu_bio_merge);
38 dma_addr_t bad_dma_address __read_mostly = 0;
39 EXPORT_SYMBOL(bad_dma_address);
41 int dma_set_mask(struct device *dev, u64 mask)
43 if (!dev->dma_mask || !dma_supported(dev, mask))
46 *dev->dma_mask = mask;
50 EXPORT_SYMBOL(dma_set_mask);
53 static __initdata void *dma32_bootmem_ptr;
54 static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
56 static int __init parse_dma32_size_opt(char *p)
60 dma32_bootmem_size = memparse(p, &p);
63 early_param("dma32_size", parse_dma32_size_opt);
65 void __init dma32_reserve_bootmem(void)
67 unsigned long size, align;
68 if (end_pfn <= MAX_DMA32_PFN)
72 size = round_up(dma32_bootmem_size, align);
73 dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
74 __pa(MAX_DMA_ADDRESS));
75 if (dma32_bootmem_ptr)
76 dma32_bootmem_size = size;
78 dma32_bootmem_size = 0;
80 static void __init dma32_free_bootmem(void)
84 if (end_pfn <= MAX_DMA32_PFN)
87 if (!dma32_bootmem_ptr)
90 for_each_online_node(node)
91 free_bootmem_node(NODE_DATA(node), __pa(dma32_bootmem_ptr),
94 dma32_bootmem_ptr = NULL;
95 dma32_bootmem_size = 0;
98 void __init pci_iommu_alloc(void)
100 /* free the range so iommu could get some range less than 4G */
101 dma32_free_bootmem();
103 * The order of these functions is important for
104 * fall-back/fail-over reasons
106 #ifdef CONFIG_GART_IOMMU
107 gart_iommu_hole_init();
110 #ifdef CONFIG_CALGARY_IOMMU
114 detect_intel_iommu();
116 #ifdef CONFIG_SWIOTLB
123 * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
126 static __init int iommu_setup(char *p)
134 if (!strncmp(p, "off", 3))
136 /* gart_parse_options has more force support */
137 if (!strncmp(p, "force", 5))
139 if (!strncmp(p, "noforce", 7)) {
144 if (!strncmp(p, "biomerge", 8)) {
145 iommu_bio_merge = 4096;
149 if (!strncmp(p, "panic", 5))
150 panic_on_overflow = 1;
151 if (!strncmp(p, "nopanic", 7))
152 panic_on_overflow = 0;
153 if (!strncmp(p, "merge", 5)) {
157 if (!strncmp(p, "nomerge", 7))
159 if (!strncmp(p, "forcesac", 8))
161 if (!strncmp(p, "allowdac", 8))
163 if (!strncmp(p, "nodac", 5))
165 if (!strncmp(p, "usedac", 6)) {
169 #ifdef CONFIG_SWIOTLB
170 if (!strncmp(p, "soft", 4))
174 #ifdef CONFIG_GART_IOMMU
175 gart_parse_options(p);
178 #ifdef CONFIG_CALGARY_IOMMU
179 if (!strncmp(p, "calgary", 7))
181 #endif /* CONFIG_CALGARY_IOMMU */
183 p += strcspn(p, ",");
189 early_param("iommu", iommu_setup);
192 int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
193 dma_addr_t device_addr, size_t size, int flags)
195 void __iomem *mem_base = NULL;
196 int pages = size >> PAGE_SHIFT;
197 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
199 if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
206 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
208 mem_base = ioremap(bus_addr, size);
212 dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
215 dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
216 if (!dev->dma_mem->bitmap)
219 dev->dma_mem->virt_base = mem_base;
220 dev->dma_mem->device_base = device_addr;
221 dev->dma_mem->size = pages;
222 dev->dma_mem->flags = flags;
224 if (flags & DMA_MEMORY_MAP)
225 return DMA_MEMORY_MAP;
227 return DMA_MEMORY_IO;
236 EXPORT_SYMBOL(dma_declare_coherent_memory);
238 void dma_release_declared_memory(struct device *dev)
240 struct dma_coherent_mem *mem = dev->dma_mem;
245 iounmap(mem->virt_base);
249 EXPORT_SYMBOL(dma_release_declared_memory);
251 void *dma_mark_declared_memory_occupied(struct device *dev,
252 dma_addr_t device_addr, size_t size)
254 struct dma_coherent_mem *mem = dev->dma_mem;
256 int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1);
258 pages >>= PAGE_SHIFT;
261 return ERR_PTR(-EINVAL);
263 pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
264 err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
267 return mem->virt_base + (pos << PAGE_SHIFT);
269 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
270 #endif /* CONFIG_X86_32 */
272 int dma_supported(struct device *dev, u64 mask)
275 if (mask > 0xffffffff && forbid_dac > 0) {
276 printk(KERN_INFO "PCI: Disallowing DAC for device %s\n",
282 if (dma_ops->dma_supported)
283 return dma_ops->dma_supported(dev, mask);
285 /* Copied from i386. Doesn't make much sense, because it will
286 only work for pci_alloc_coherent.
287 The caller just has to use GFP_DMA in this case. */
288 if (mask < DMA_24BIT_MASK)
291 /* Tell the device to use SAC when IOMMU force is on. This
292 allows the driver to use cheaper accesses in some cases.
294 Problem with this is that if we overflow the IOMMU area and
295 return DAC as fallback address the device may not handle it
298 As a special case some controllers have a 39bit address
299 mode that is as efficient as 32bit (aic79xx). Don't force
300 SAC for these. Assume all masks <= 40 bits are of this
301 type. Normally this doesn't make any difference, but gives
302 more gentle handling of IOMMU overflow. */
303 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
304 printk(KERN_INFO "%s: Force SAC with mask %Lx\n",
311 EXPORT_SYMBOL(dma_supported);
314 static int __init pci_iommu_init(void)
316 #ifdef CONFIG_CALGARY_IOMMU
317 calgary_iommu_init();
322 #ifdef CONFIG_GART_IOMMU
330 void pci_iommu_shutdown(void)
332 gart_iommu_shutdown();
334 /* Must execute after PCI subsystem */
335 fs_initcall(pci_iommu_init);
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."
348 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);