blob: dcd272dbd0a93308892ad967c5834ec0efd68208 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigea8c64a2018-01-10 16:21:13 +01002#include <linux/dma-direct.h>
Joerg Roedel2118d0c2009-01-09 15:13:15 +01003#include <linux/dma-debug.h>
Glauber Costacb5867a2008-04-08 13:20:51 -03004#include <linux/dmar.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -04005#include <linux/export.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -07006#include <linux/memblock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/gfp.h>
Glauber Costabca5c092008-04-08 13:20:53 -03008#include <linux/pci.h>
Glauber Costacb5867a2008-04-08 13:20:51 -03009
Glauber Costa116890d2008-04-08 13:20:54 -030010#include <asm/proto.h>
11#include <asm/dma.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090012#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010013#include <asm/gart.h>
Glauber Costacb5867a2008-04-08 13:20:51 -030014#include <asm/calgary.h>
Ingo Molnarb4941a92009-11-10 14:37:58 +010015#include <asm/x86_init.h>
Konrad Rzeszutek Wilkee1f2842010-08-26 13:58:05 -040016#include <asm/iommu_table.h>
Glauber Costa459121c92008-04-08 13:20:43 -030017
Christoph Hellwig0ead51c2018-05-28 12:47:57 +020018static bool disable_dac_quirk __read_mostly;
Fenghua Yu3b15e582008-10-23 16:51:00 -070019
Christoph Hellwig356da6d2018-12-06 13:39:32 -080020const struct dma_map_ops *dma_ops;
Glauber Costa85c246e2008-04-08 13:20:50 -030021EXPORT_SYMBOL(dma_ops);
22
Glauber Costaf9c258d2008-04-08 13:20:52 -030023#ifdef CONFIG_IOMMU_DEBUG
24int panic_on_overflow __read_mostly = 1;
25int force_iommu __read_mostly = 1;
26#else
27int panic_on_overflow __read_mostly = 0;
28int force_iommu __read_mostly = 0;
29#endif
30
Glauber Costafae9a0d2008-04-08 13:20:56 -030031int iommu_merge __read_mostly = 0;
32
33int no_iommu __read_mostly;
34/* Set this to 1 if there is a HW IOMMU in the system */
35int iommu_detected __read_mostly = 0;
36
Joerg Roedelac0101d2009-09-01 16:00:35 +020037/*
38 * This variable becomes 1 if iommu=pt is passed on the kernel command line.
Marin Mitove3be7852009-10-03 20:45:02 +030039 * If this variable is 1, IOMMU implementations do no DMA translation for
Joerg Roedelac0101d2009-09-01 16:00:35 +020040 * devices and allow every device to access to whole physical memory. This is
Justin P. Mattockfb637f32010-01-14 22:16:16 -080041 * useful if a user wants to use an IOMMU only for KVM device assignment to
Joerg Roedelac0101d2009-09-01 16:00:35 +020042 * guests and not for driver dma translation.
Olof Johansson58d11312018-07-20 11:02:23 -070043 * It is also possible to disable by default in kernel config, and enable with
44 * iommu=nopt at boot time.
Joerg Roedelac0101d2009-09-01 16:00:35 +020045 */
Olof Johansson58d11312018-07-20 11:02:23 -070046#ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
47int iommu_pass_through __read_mostly = 1;
48#else
Joerg Roedelac0101d2009-09-01 16:00:35 +020049int iommu_pass_through __read_mostly;
Olof Johansson58d11312018-07-20 11:02:23 -070050#endif
Fenghua Yuaed5d5f2009-04-30 17:57:11 -070051
Konrad Rzeszutek Wilkee1f2842010-08-26 13:58:05 -040052extern struct iommu_table_entry __iommu_table[], __iommu_table_end[];
53
Glauber Costa116890d2008-04-08 13:20:54 -030054void __init pci_iommu_alloc(void)
55{
Konrad Rzeszutek Wilkee1f2842010-08-26 13:58:05 -040056 struct iommu_table_entry *p;
57
Konrad Rzeszutek Wilkee1f2842010-08-26 13:58:05 -040058 sort_iommu_table(__iommu_table, __iommu_table_end);
59 check_iommu_entries(__iommu_table, __iommu_table_end);
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -080060
Konrad Rzeszutek Wilkee1f2842010-08-26 13:58:05 -040061 for (p = __iommu_table; p < __iommu_table_end; p++) {
62 if (p && p->detect && p->detect() > 0) {
63 p->flags |= IOMMU_DETECTED;
64 if (p->early_init)
65 p->early_init();
66 if (p->flags & IOMMU_FINISH_IF_DETECTED)
67 break;
68 }
69 }
Glauber Costa116890d2008-04-08 13:20:54 -030070}
Marek Szyprowski0a2b9a62011-12-29 13:09:51 +010071
Glauber Costafae9a0d2008-04-08 13:20:56 -030072/*
Paul Bolle395cf962011-08-15 02:02:26 +020073 * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
74 * parameter documentation.
Glauber Costafae9a0d2008-04-08 13:20:56 -030075 */
76static __init int iommu_setup(char *p)
77{
78 iommu_merge = 1;
79
80 if (!p)
81 return -EINVAL;
82
83 while (*p) {
84 if (!strncmp(p, "off", 3))
85 no_iommu = 1;
86 /* gart_parse_options has more force support */
87 if (!strncmp(p, "force", 5))
88 force_iommu = 1;
89 if (!strncmp(p, "noforce", 7)) {
90 iommu_merge = 0;
91 force_iommu = 0;
92 }
93
94 if (!strncmp(p, "biomerge", 8)) {
Glauber Costafae9a0d2008-04-08 13:20:56 -030095 iommu_merge = 1;
96 force_iommu = 1;
97 }
98 if (!strncmp(p, "panic", 5))
99 panic_on_overflow = 1;
100 if (!strncmp(p, "nopanic", 7))
101 panic_on_overflow = 0;
102 if (!strncmp(p, "merge", 5)) {
103 iommu_merge = 1;
104 force_iommu = 1;
105 }
106 if (!strncmp(p, "nomerge", 7))
107 iommu_merge = 0;
108 if (!strncmp(p, "forcesac", 8))
Christoph Hellwig06e95522018-04-27 09:13:24 +0200109 pr_warn("forcesac option ignored.\n");
Glauber Costafae9a0d2008-04-08 13:20:56 -0300110 if (!strncmp(p, "allowdac", 8))
Christoph Hellwig098afd92018-04-27 09:31:47 +0200111 pr_warn("allowdac option ignored.\n");
Glauber Costafae9a0d2008-04-08 13:20:56 -0300112 if (!strncmp(p, "nodac", 5))
Christoph Hellwig098afd92018-04-27 09:31:47 +0200113 pr_warn("nodac option ignored.\n");
Glauber Costafae9a0d2008-04-08 13:20:56 -0300114 if (!strncmp(p, "usedac", 6)) {
Christoph Hellwig0ead51c2018-05-28 12:47:57 +0200115 disable_dac_quirk = true;
Glauber Costafae9a0d2008-04-08 13:20:56 -0300116 return 1;
117 }
118#ifdef CONFIG_SWIOTLB
119 if (!strncmp(p, "soft", 4))
120 swiotlb = 1;
David Woodhouse3238c0c2009-07-01 18:56:16 +0100121#endif
Alex Williamson80286872009-07-30 16:15:18 -0600122 if (!strncmp(p, "pt", 2))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700123 iommu_pass_through = 1;
Olof Johansson58d11312018-07-20 11:02:23 -0700124 if (!strncmp(p, "nopt", 4))
125 iommu_pass_through = 0;
Glauber Costafae9a0d2008-04-08 13:20:56 -0300126
Glauber Costafae9a0d2008-04-08 13:20:56 -0300127 gart_parse_options(p);
Glauber Costafae9a0d2008-04-08 13:20:56 -0300128
129#ifdef CONFIG_CALGARY_IOMMU
130 if (!strncmp(p, "calgary", 7))
131 use_calgary = 1;
132#endif /* CONFIG_CALGARY_IOMMU */
133
134 p += strcspn(p, ",");
135 if (*p == ',')
136 ++p;
137 }
138 return 0;
139}
140early_param("iommu", iommu_setup);
141
Glauber Costacb5867a2008-04-08 13:20:51 -0300142static int __init pci_iommu_init(void)
143{
Konrad Rzeszutek Wilkee1f2842010-08-26 13:58:05 -0400144 struct iommu_table_entry *p;
Joerg Roedel2118d0c2009-01-09 15:13:15 +0100145
FUJITA Tomonorid07c1be2009-11-10 19:46:12 +0900146 x86_init.iommu.iommu_init();
147
Konrad Rzeszutek Wilkee1f2842010-08-26 13:58:05 -0400148 for (p = __iommu_table; p < __iommu_table_end; p++) {
149 if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
150 p->late_init();
151 }
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +0900152
Glauber Costacb5867a2008-04-08 13:20:51 -0300153 return 0;
154}
Glauber Costacb5867a2008-04-08 13:20:51 -0300155/* Must execute after PCI subsystem */
David Woodhouse9a821b22009-10-12 12:59:29 +0100156rootfs_initcall(pci_iommu_init);
Fenghua Yu3b15e582008-10-23 16:51:00 -0700157
158#ifdef CONFIG_PCI
159/* Many VIA bridges seem to corrupt data for DAC. Disable it here */
160
Christoph Hellwig0ead51c2018-05-28 12:47:57 +0200161static int via_no_dac_cb(struct pci_dev *pdev, void *data)
162{
Robin Murphyf07d1412018-07-23 23:16:07 +0100163 pdev->dev.bus_dma_mask = DMA_BIT_MASK(32);
Christoph Hellwig0ead51c2018-05-28 12:47:57 +0200164 return 0;
165}
166
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -0800167static void via_no_dac(struct pci_dev *dev)
Fenghua Yu3b15e582008-10-23 16:51:00 -0700168{
Christoph Hellwig0ead51c2018-05-28 12:47:57 +0200169 if (!disable_dac_quirk) {
Bjorn Helgaas13bf7572009-02-24 10:38:22 -0700170 dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
Christoph Hellwig0ead51c2018-05-28 12:47:57 +0200171 pci_walk_bus(dev->subordinate, via_no_dac_cb, NULL);
Fenghua Yu3b15e582008-10-23 16:51:00 -0700172 }
173}
Yinghai Luc484b242012-02-23 23:46:50 -0800174DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
175 PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
Fenghua Yu3b15e582008-10-23 16:51:00 -0700176#endif