1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/dma-direct.h>
3 #include <linux/dma-debug.h>
4 #include <linux/iommu.h>
5 #include <linux/dmar.h>
6 #include <linux/export.h>
7 #include <linux/memblock.h>
11 #include <asm/proto.h>
13 #include <asm/iommu.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
;
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;
37 extern struct iommu_table_entry __iommu_table
[], __iommu_table_end
[];
39 void __init
pci_iommu_alloc(void)
41 struct iommu_table_entry
*p
;
43 sort_iommu_table(__iommu_table
, __iommu_table_end
);
44 check_iommu_entries(__iommu_table
, __iommu_table_end
);
46 for (p
= __iommu_table
; p
< __iommu_table_end
; p
++) {
47 if (p
&& p
->detect
&& p
->detect() > 0) {
48 p
->flags
|= IOMMU_DETECTED
;
51 if (p
->flags
& IOMMU_FINISH_IF_DETECTED
)
58 * See <Documentation/x86/x86_64/boot-options.rst> for the iommu kernel
59 * parameter documentation.
61 static __init
int iommu_setup(char *p
)
69 if (!strncmp(p
, "off", 3))
71 /* gart_parse_options has more force support */
72 if (!strncmp(p
, "force", 5))
74 if (!strncmp(p
, "noforce", 7)) {
79 if (!strncmp(p
, "biomerge", 8)) {
83 if (!strncmp(p
, "panic", 5))
84 panic_on_overflow
= 1;
85 if (!strncmp(p
, "nopanic", 7))
86 panic_on_overflow
= 0;
87 if (!strncmp(p
, "merge", 5)) {
91 if (!strncmp(p
, "nomerge", 7))
93 if (!strncmp(p
, "forcesac", 8))
94 pr_warn("forcesac option ignored.\n");
95 if (!strncmp(p
, "allowdac", 8))
96 pr_warn("allowdac option ignored.\n");
97 if (!strncmp(p
, "nodac", 5))
98 pr_warn("nodac option ignored.\n");
99 if (!strncmp(p
, "usedac", 6)) {
100 disable_dac_quirk
= true;
103 #ifdef CONFIG_SWIOTLB
104 if (!strncmp(p
, "soft", 4))
107 if (!strncmp(p
, "pt", 2))
108 iommu_set_default_passthrough(true);
109 if (!strncmp(p
, "nopt", 4))
110 iommu_set_default_translated(true);
112 gart_parse_options(p
);
114 p
+= strcspn(p
, ",");
120 early_param("iommu", iommu_setup
);
122 static int __init
pci_iommu_init(void)
124 struct iommu_table_entry
*p
;
126 x86_init
.iommu
.iommu_init();
128 for (p
= __iommu_table
; p
< __iommu_table_end
; p
++) {
129 if (p
&& (p
->flags
& IOMMU_DETECTED
) && p
->late_init
)
135 /* Must execute after PCI subsystem */
136 rootfs_initcall(pci_iommu_init
);
139 /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
141 static int via_no_dac_cb(struct pci_dev
*pdev
, void *data
)
143 pdev
->dev
.bus_dma_limit
= DMA_BIT_MASK(32);
147 static void via_no_dac(struct pci_dev
*dev
)
149 if (!disable_dac_quirk
) {
150 dev_info(&dev
->dev
, "disabling DAC on VIA PCI bridge\n");
151 pci_walk_bus(dev
->subordinate
, via_no_dac_cb
, NULL
);
154 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA
, PCI_ANY_ID
,
155 PCI_CLASS_BRIDGE_PCI
, 8, via_no_dac
);