1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/dma-direct.h>
3 #include <linux/dma-debug.h>
4 #include <linux/dmar.h>
5 #include <linux/export.h>
6 #include <linux/bootmem.h>
10 #include <asm/proto.h>
12 #include <asm/iommu.h>
14 #include <asm/calgary.h>
15 #include <asm/x86_init.h>
16 #include <asm/iommu_table.h>
18 static bool disable_dac_quirk __read_mostly
;
20 const struct dma_map_ops
*dma_ops
= &dma_direct_ops
;
21 EXPORT_SYMBOL(dma_ops
);
23 #ifdef CONFIG_IOMMU_DEBUG
24 int panic_on_overflow __read_mostly
= 1;
25 int force_iommu __read_mostly
= 1;
27 int panic_on_overflow __read_mostly
= 0;
28 int force_iommu __read_mostly
= 0;
31 int iommu_merge __read_mostly
= 0;
33 int no_iommu __read_mostly
;
34 /* Set this to 1 if there is a HW IOMMU in the system */
35 int iommu_detected __read_mostly
= 0;
38 * This variable becomes 1 if iommu=pt is passed on the kernel command line.
39 * If this variable is 1, IOMMU implementations do no DMA translation for
40 * devices and allow every device to access to whole physical memory. This is
41 * useful if a user wants to use an IOMMU only for KVM device assignment to
42 * guests and not for driver dma translation.
43 * It is also possible to disable by default in kernel config, and enable with
44 * iommu=nopt at boot time.
46 #ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
47 int iommu_pass_through __read_mostly
= 1;
49 int iommu_pass_through __read_mostly
;
52 extern struct iommu_table_entry __iommu_table
[], __iommu_table_end
[];
54 /* Dummy device used for NULL arguments (normally ISA). */
55 struct device x86_dma_fallback_dev
= {
56 .init_name
= "fallback device",
57 .coherent_dma_mask
= ISA_DMA_BIT_MASK
,
58 .dma_mask
= &x86_dma_fallback_dev
.coherent_dma_mask
,
60 EXPORT_SYMBOL(x86_dma_fallback_dev
);
62 void __init
pci_iommu_alloc(void)
64 struct iommu_table_entry
*p
;
66 sort_iommu_table(__iommu_table
, __iommu_table_end
);
67 check_iommu_entries(__iommu_table
, __iommu_table_end
);
69 for (p
= __iommu_table
; p
< __iommu_table_end
; p
++) {
70 if (p
&& p
->detect
&& p
->detect() > 0) {
71 p
->flags
|= IOMMU_DETECTED
;
74 if (p
->flags
& IOMMU_FINISH_IF_DETECTED
)
80 bool arch_dma_alloc_attrs(struct device
**dev
)
83 *dev
= &x86_dma_fallback_dev
;
85 if (!is_device_dma_capable(*dev
))
90 EXPORT_SYMBOL(arch_dma_alloc_attrs
);
93 * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
94 * parameter documentation.
96 static __init
int iommu_setup(char *p
)
104 if (!strncmp(p
, "off", 3))
106 /* gart_parse_options has more force support */
107 if (!strncmp(p
, "force", 5))
109 if (!strncmp(p
, "noforce", 7)) {
114 if (!strncmp(p
, "biomerge", 8)) {
118 if (!strncmp(p
, "panic", 5))
119 panic_on_overflow
= 1;
120 if (!strncmp(p
, "nopanic", 7))
121 panic_on_overflow
= 0;
122 if (!strncmp(p
, "merge", 5)) {
126 if (!strncmp(p
, "nomerge", 7))
128 if (!strncmp(p
, "forcesac", 8))
129 pr_warn("forcesac option ignored.\n");
130 if (!strncmp(p
, "allowdac", 8))
131 pr_warn("allowdac option ignored.\n");
132 if (!strncmp(p
, "nodac", 5))
133 pr_warn("nodac option ignored.\n");
134 if (!strncmp(p
, "usedac", 6)) {
135 disable_dac_quirk
= true;
138 #ifdef CONFIG_SWIOTLB
139 if (!strncmp(p
, "soft", 4))
142 if (!strncmp(p
, "pt", 2))
143 iommu_pass_through
= 1;
144 if (!strncmp(p
, "nopt", 4))
145 iommu_pass_through
= 0;
147 gart_parse_options(p
);
149 #ifdef CONFIG_CALGARY_IOMMU
150 if (!strncmp(p
, "calgary", 7))
152 #endif /* CONFIG_CALGARY_IOMMU */
154 p
+= strcspn(p
, ",");
160 early_param("iommu", iommu_setup
);
162 static int __init
pci_iommu_init(void)
164 struct iommu_table_entry
*p
;
166 x86_init
.iommu
.iommu_init();
168 for (p
= __iommu_table
; p
< __iommu_table_end
; p
++) {
169 if (p
&& (p
->flags
& IOMMU_DETECTED
) && p
->late_init
)
175 /* Must execute after PCI subsystem */
176 rootfs_initcall(pci_iommu_init
);
179 /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
181 static int via_no_dac_cb(struct pci_dev
*pdev
, void *data
)
183 pdev
->dev
.bus_dma_mask
= DMA_BIT_MASK(32);
187 static void via_no_dac(struct pci_dev
*dev
)
189 if (!disable_dac_quirk
) {
190 dev_info(&dev
->dev
, "disabling DAC on VIA PCI bridge\n");
191 pci_walk_bus(dev
->subordinate
, via_no_dac_cb
, NULL
);
194 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA
, PCI_ANY_ID
,
195 PCI_CLASS_BRIDGE_PCI
, 8, via_no_dac
);