pci-dma.c 4.62 KB
Newer Older
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/memblock.h>
7
#include <linux/gfp.h>
8
#include <linux/pci.h>
9

10 11
#include <asm/proto.h>
#include <asm/dma.h>
12
#include <asm/iommu.h>
13
#include <asm/gart.h>
14
#include <asm/calgary.h>
15
#include <asm/x86_init.h>
16
#include <asm/iommu_table.h>
Glauber Costa's avatar
Glauber Costa committed
17

18
static bool disable_dac_quirk __read_mostly;
19

20
const struct dma_map_ops *dma_ops;
21 22
EXPORT_SYMBOL(dma_ops);

Glauber Costa's avatar
Glauber Costa committed
23 24 25 26 27 28 29 30
#ifdef CONFIG_IOMMU_DEBUG
int panic_on_overflow __read_mostly = 1;
int force_iommu __read_mostly = 1;
#else
int panic_on_overflow __read_mostly = 0;
int force_iommu __read_mostly = 0;
#endif

31 32 33 34 35 36
int iommu_merge __read_mostly = 0;

int no_iommu __read_mostly;
/* Set this to 1 if there is a HW IOMMU in the system */
int iommu_detected __read_mostly = 0;

37 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 44
 * It is also possible to disable by default in kernel config, and enable with
 * iommu=nopt at boot time.
45
 */
46 47 48
#ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
int iommu_pass_through __read_mostly = 1;
#else
49
int iommu_pass_through __read_mostly;
50
#endif
51

52 53
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,
Glauber Costa's avatar
Glauber Costa committed
59
};
60
EXPORT_SYMBOL(x86_dma_fallback_dev);
Glauber Costa's avatar
Glauber Costa committed
61

62 63
void __init pci_iommu_alloc(void)
{
64 65 66 67
	struct iommu_table_entry *p;

	sort_iommu_table(__iommu_table, __iommu_table_end);
	check_iommu_entries(__iommu_table, __iommu_table_end);
68

69 70 71 72 73 74 75 76 77
	for (p = __iommu_table; p < __iommu_table_end; p++) {
		if (p && p->detect && p->detect() > 0) {
			p->flags |= IOMMU_DETECTED;
			if (p->early_init)
				p->early_init();
			if (p->flags & IOMMU_FINISH_IF_DETECTED)
				break;
		}
	}
78
}
79

80
bool arch_dma_alloc_attrs(struct device **dev)
81
{
82 83 84
	if (!*dev)
		*dev = &x86_dma_fallback_dev;

85 86 87
	if (!is_device_dma_capable(*dev))
		return false;
	return true;
88

89
}
90
EXPORT_SYMBOL(arch_dma_alloc_attrs);
91

92
/*
Paul Bolle's avatar
Paul Bolle committed
93 94
 * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
 * parameter documentation.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
 */
static __init int iommu_setup(char *p)
{
	iommu_merge = 1;

	if (!p)
		return -EINVAL;

	while (*p) {
		if (!strncmp(p, "off", 3))
			no_iommu = 1;
		/* gart_parse_options has more force support */
		if (!strncmp(p, "force", 5))
			force_iommu = 1;
		if (!strncmp(p, "noforce", 7)) {
			iommu_merge = 0;
			force_iommu = 0;
		}

		if (!strncmp(p, "biomerge", 8)) {
			iommu_merge = 1;
			force_iommu = 1;
		}
		if (!strncmp(p, "panic", 5))
			panic_on_overflow = 1;
		if (!strncmp(p, "nopanic", 7))
			panic_on_overflow = 0;
		if (!strncmp(p, "merge", 5)) {
			iommu_merge = 1;
			force_iommu = 1;
		}
		if (!strncmp(p, "nomerge", 7))
			iommu_merge = 0;
		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;
136 137 138 139 140
			return 1;
		}
#ifdef CONFIG_SWIOTLB
		if (!strncmp(p, "soft", 4))
			swiotlb = 1;
141
#endif
142
		if (!strncmp(p, "pt", 2))
143
			iommu_pass_through = 1;
144 145
		if (!strncmp(p, "nopt", 4))
			iommu_pass_through = 0;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

		gart_parse_options(p);

#ifdef CONFIG_CALGARY_IOMMU
		if (!strncmp(p, "calgary", 7))
			use_calgary = 1;
#endif /* CONFIG_CALGARY_IOMMU */

		p += strcspn(p, ",");
		if (*p == ',')
			++p;
	}
	return 0;
}
early_param("iommu", iommu_setup);

162 163
static int __init pci_iommu_init(void)
{
164
	struct iommu_table_entry *p;
165

166 167
	x86_init.iommu.iommu_init();

168 169 170 171
	for (p = __iommu_table; p < __iommu_table_end; p++) {
		if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
			p->late_init();
	}
172

173 174 175
	return 0;
}
/* Must execute after PCI subsystem */
176
rootfs_initcall(pci_iommu_init);
177 178 179 180

#ifdef CONFIG_PCI
/* Many VIA bridges seem to corrupt data for DAC. Disable it here */

181 182
static int via_no_dac_cb(struct pci_dev *pdev, void *data)
{
183
	pdev->dev.bus_dma_mask = DMA_BIT_MASK(32);
184 185 186
	return 0;
}

187
static void via_no_dac(struct pci_dev *dev)
188
{
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);
192 193
	}
}
194 195
DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
				PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
196
#endif