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);
39 int dma_set_mask(struct device *dev, u64 mask)
41 if (!dev->dma_mask || !dma_supported(dev, mask))
44 *dev->dma_mask = mask;
48 EXPORT_SYMBOL(dma_set_mask);
51 static __initdata void *dma32_bootmem_ptr;
52 static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
54 static int __init parse_dma32_size_opt(char *p)
58 dma32_bootmem_size = memparse(p, &p);
61 early_param("dma32_size", parse_dma32_size_opt);
63 void __init dma32_reserve_bootmem(void)
65 unsigned long size, align;
66 if (end_pfn <= MAX_DMA32_PFN)
70 size = round_up(dma32_bootmem_size, align);
71 dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
72 __pa(MAX_DMA_ADDRESS));
73 if (dma32_bootmem_ptr)
74 dma32_bootmem_size = size;
76 dma32_bootmem_size = 0;
78 static void __init dma32_free_bootmem(void)
82 if (end_pfn <= MAX_DMA32_PFN)
85 if (!dma32_bootmem_ptr)
88 for_each_online_node(node)
89 free_bootmem_node(NODE_DATA(node), __pa(dma32_bootmem_ptr),
92 dma32_bootmem_ptr = NULL;
93 dma32_bootmem_size = 0;
96 void __init pci_iommu_alloc(void)
98 /* free the range so iommu could get some range less than 4G */
101 * The order of these functions is important for
102 * fall-back/fail-over reasons
104 #ifdef CONFIG_GART_IOMMU
105 gart_iommu_hole_init();
108 #ifdef CONFIG_CALGARY_IOMMU
112 detect_intel_iommu();
114 #ifdef CONFIG_SWIOTLB
121 * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
124 static __init int iommu_setup(char *p)
132 if (!strncmp(p, "off", 3))
134 /* gart_parse_options has more force support */
135 if (!strncmp(p, "force", 5))
137 if (!strncmp(p, "noforce", 7)) {
142 if (!strncmp(p, "biomerge", 8)) {
143 iommu_bio_merge = 4096;
147 if (!strncmp(p, "panic", 5))
148 panic_on_overflow = 1;
149 if (!strncmp(p, "nopanic", 7))
150 panic_on_overflow = 0;
151 if (!strncmp(p, "merge", 5)) {
155 if (!strncmp(p, "nomerge", 7))
157 if (!strncmp(p, "forcesac", 8))
159 if (!strncmp(p, "allowdac", 8))
161 if (!strncmp(p, "nodac", 5))
163 if (!strncmp(p, "usedac", 6)) {
167 #ifdef CONFIG_SWIOTLB
168 if (!strncmp(p, "soft", 4))
172 #ifdef CONFIG_GART_IOMMU
173 gart_parse_options(p);
176 #ifdef CONFIG_CALGARY_IOMMU
177 if (!strncmp(p, "calgary", 7))
179 #endif /* CONFIG_CALGARY_IOMMU */
181 p += strcspn(p, ",");
187 early_param("iommu", iommu_setup);
190 int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
191 dma_addr_t device_addr, size_t size, int flags)
193 void __iomem *mem_base = NULL;
194 int pages = size >> PAGE_SHIFT;
195 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
197 if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
204 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
206 mem_base = ioremap(bus_addr, size);
210 dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
213 dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
214 if (!dev->dma_mem->bitmap)
217 dev->dma_mem->virt_base = mem_base;
218 dev->dma_mem->device_base = device_addr;
219 dev->dma_mem->size = pages;
220 dev->dma_mem->flags = flags;
222 if (flags & DMA_MEMORY_MAP)
223 return DMA_MEMORY_MAP;
225 return DMA_MEMORY_IO;
234 EXPORT_SYMBOL(dma_declare_coherent_memory);
236 void dma_release_declared_memory(struct device *dev)
238 struct dma_coherent_mem *mem = dev->dma_mem;
243 iounmap(mem->virt_base);
247 EXPORT_SYMBOL(dma_release_declared_memory);
249 void *dma_mark_declared_memory_occupied(struct device *dev,
250 dma_addr_t device_addr, size_t size)
252 struct dma_coherent_mem *mem = dev->dma_mem;
254 int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1);
256 pages >>= PAGE_SHIFT;
259 return ERR_PTR(-EINVAL);
261 pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
262 err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
265 return mem->virt_base + (pos << PAGE_SHIFT);
267 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
268 #endif /* CONFIG_X86_32 */
270 int dma_supported(struct device *dev, u64 mask)
273 if (mask > 0xffffffff && forbid_dac > 0) {
274 printk(KERN_INFO "PCI: Disallowing DAC for device %s\n",
280 if (dma_ops->dma_supported)
281 return dma_ops->dma_supported(dev, mask);
283 /* Copied from i386. Doesn't make much sense, because it will
284 only work for pci_alloc_coherent.
285 The caller just has to use GFP_DMA in this case. */
286 if (mask < DMA_24BIT_MASK)
289 /* Tell the device to use SAC when IOMMU force is on. This
290 allows the driver to use cheaper accesses in some cases.
292 Problem with this is that if we overflow the IOMMU area and
293 return DAC as fallback address the device may not handle it
296 As a special case some controllers have a 39bit address
297 mode that is as efficient as 32bit (aic79xx). Don't force
298 SAC for these. Assume all masks <= 40 bits are of this
299 type. Normally this doesn't make any difference, but gives
300 more gentle handling of IOMMU overflow. */
301 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
302 printk(KERN_INFO "%s: Force SAC with mask %Lx\n",
309 EXPORT_SYMBOL(dma_supported);
312 static int __init pci_iommu_init(void)
314 #ifdef CONFIG_CALGARY_IOMMU
315 calgary_iommu_init();
320 #ifdef CONFIG_GART_IOMMU
328 void pci_iommu_shutdown(void)
330 gart_iommu_shutdown();
332 /* Must execute after PCI subsystem */
333 fs_initcall(pci_iommu_init);
336 /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
338 static __devinit void via_no_dac(struct pci_dev *dev)
340 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
341 printk(KERN_INFO "PCI: VIA PCI bridge detected."
346 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);