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);
189 int dma_supported(struct device *dev, u64 mask)
192 if (mask > 0xffffffff && forbid_dac > 0) {
193 printk(KERN_INFO "PCI: Disallowing DAC for device %s\n",
199 if (dma_ops->dma_supported)
200 return dma_ops->dma_supported(dev, mask);
202 /* Copied from i386. Doesn't make much sense, because it will
203 only work for pci_alloc_coherent.
204 The caller just has to use GFP_DMA in this case. */
205 if (mask < DMA_24BIT_MASK)
208 /* Tell the device to use SAC when IOMMU force is on. This
209 allows the driver to use cheaper accesses in some cases.
211 Problem with this is that if we overflow the IOMMU area and
212 return DAC as fallback address the device may not handle it
215 As a special case some controllers have a 39bit address
216 mode that is as efficient as 32bit (aic79xx). Don't force
217 SAC for these. Assume all masks <= 40 bits are of this
218 type. Normally this doesn't make any difference, but gives
219 more gentle handling of IOMMU overflow. */
220 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
221 printk(KERN_INFO "%s: Force SAC with mask %Lx\n",
228 EXPORT_SYMBOL(dma_supported);
231 static int __init pci_iommu_init(void)
233 #ifdef CONFIG_CALGARY_IOMMU
234 calgary_iommu_init();
239 #ifdef CONFIG_GART_IOMMU
247 void pci_iommu_shutdown(void)
249 gart_iommu_shutdown();
251 /* Must execute after PCI subsystem */
252 fs_initcall(pci_iommu_init);
255 /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
257 static __devinit void via_no_dac(struct pci_dev *dev)
259 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
260 printk(KERN_INFO "PCI: VIA PCI bridge detected."
265 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);