]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/kernel/pci-dma.c
x86: move GART TLB flushing options to generic code
[karo-tx-linux.git] / arch / x86 / kernel / pci-dma.c
1 #include <linux/dma-mapping.h>
2 #include <linux/dmar.h>
3 #include <linux/bootmem.h>
4 #include <linux/pci.h>
5
6 #include <asm/proto.h>
7 #include <asm/dma.h>
8 #include <asm/iommu.h>
9 #include <asm/calgary.h>
10 #include <asm/amd_iommu.h>
11
12 static int forbid_dac __read_mostly;
13
14 struct dma_mapping_ops *dma_ops;
15 EXPORT_SYMBOL(dma_ops);
16
17 static int iommu_sac_force __read_mostly;
18
19 /*
20  * If this is disabled the IOMMU will use an optimized flushing strategy
21  * of only flushing when an mapping is reused. With it true the GART is
22  * flushed for every mapping. Problem is that doing the lazy flush seems
23  * to trigger bugs with some popular PCI cards, in particular 3ware (but
24  * has been also also seen with Qlogic at least).
25  */
26 int iommu_fullflush;
27
28 #ifdef CONFIG_IOMMU_DEBUG
29 int panic_on_overflow __read_mostly = 1;
30 int force_iommu __read_mostly = 1;
31 #else
32 int panic_on_overflow __read_mostly = 0;
33 int force_iommu __read_mostly = 0;
34 #endif
35
36 int iommu_merge __read_mostly = 0;
37
38 int no_iommu __read_mostly;
39 /* Set this to 1 if there is a HW IOMMU in the system */
40 int iommu_detected __read_mostly = 0;
41
42 /* This tells the BIO block layer to assume merging. Default to off
43    because we cannot guarantee merging later. */
44 int iommu_bio_merge __read_mostly = 0;
45 EXPORT_SYMBOL(iommu_bio_merge);
46
47 dma_addr_t bad_dma_address __read_mostly = 0;
48 EXPORT_SYMBOL(bad_dma_address);
49
50 /* Dummy device used for NULL arguments (normally ISA). Better would
51    be probably a smaller DMA mask, but this is bug-to-bug compatible
52    to older i386. */
53 struct device x86_dma_fallback_dev = {
54         .bus_id = "fallback device",
55         .coherent_dma_mask = DMA_32BIT_MASK,
56         .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
57 };
58 EXPORT_SYMBOL(x86_dma_fallback_dev);
59
60 int dma_set_mask(struct device *dev, u64 mask)
61 {
62         if (!dev->dma_mask || !dma_supported(dev, mask))
63                 return -EIO;
64
65         *dev->dma_mask = mask;
66
67         return 0;
68 }
69 EXPORT_SYMBOL(dma_set_mask);
70
71 #ifdef CONFIG_X86_64
72 static __initdata void *dma32_bootmem_ptr;
73 static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
74
75 static int __init parse_dma32_size_opt(char *p)
76 {
77         if (!p)
78                 return -EINVAL;
79         dma32_bootmem_size = memparse(p, &p);
80         return 0;
81 }
82 early_param("dma32_size", parse_dma32_size_opt);
83
84 void __init dma32_reserve_bootmem(void)
85 {
86         unsigned long size, align;
87         if (max_pfn <= MAX_DMA32_PFN)
88                 return;
89
90         /*
91          * check aperture_64.c allocate_aperture() for reason about
92          * using 512M as goal
93          */
94         align = 64ULL<<20;
95         size = round_up(dma32_bootmem_size, align);
96         dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
97                                  512ULL<<20);
98         if (dma32_bootmem_ptr)
99                 dma32_bootmem_size = size;
100         else
101                 dma32_bootmem_size = 0;
102 }
103 static void __init dma32_free_bootmem(void)
104 {
105
106         if (max_pfn <= MAX_DMA32_PFN)
107                 return;
108
109         if (!dma32_bootmem_ptr)
110                 return;
111
112         free_bootmem(__pa(dma32_bootmem_ptr), dma32_bootmem_size);
113
114         dma32_bootmem_ptr = NULL;
115         dma32_bootmem_size = 0;
116 }
117
118 void __init pci_iommu_alloc(void)
119 {
120         /* free the range so iommu could get some range less than 4G */
121         dma32_free_bootmem();
122         /*
123          * The order of these functions is important for
124          * fall-back/fail-over reasons
125          */
126         gart_iommu_hole_init();
127
128         detect_calgary();
129
130         detect_intel_iommu();
131
132         amd_iommu_detect();
133
134         pci_swiotlb_init();
135 }
136
137 unsigned long iommu_num_pages(unsigned long addr, unsigned long len)
138 {
139         unsigned long size = roundup((addr & ~PAGE_MASK) + len, PAGE_SIZE);
140
141         return size >> PAGE_SHIFT;
142 }
143 EXPORT_SYMBOL(iommu_num_pages);
144 #endif
145
146 /*
147  * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
148  * documentation.
149  */
150 static __init int iommu_setup(char *p)
151 {
152         iommu_merge = 1;
153
154         if (!p)
155                 return -EINVAL;
156
157         while (*p) {
158                 if (!strncmp(p, "off", 3))
159                         no_iommu = 1;
160                 /* gart_parse_options has more force support */
161                 if (!strncmp(p, "force", 5))
162                         force_iommu = 1;
163                 if (!strncmp(p, "noforce", 7)) {
164                         iommu_merge = 0;
165                         force_iommu = 0;
166                 }
167
168                 if (!strncmp(p, "biomerge", 8)) {
169                         iommu_bio_merge = 4096;
170                         iommu_merge = 1;
171                         force_iommu = 1;
172                 }
173                 if (!strncmp(p, "panic", 5))
174                         panic_on_overflow = 1;
175                 if (!strncmp(p, "nopanic", 7))
176                         panic_on_overflow = 0;
177                 if (!strncmp(p, "merge", 5)) {
178                         iommu_merge = 1;
179                         force_iommu = 1;
180                 }
181                 if (!strncmp(p, "nomerge", 7))
182                         iommu_merge = 0;
183                 if (!strncmp(p, "fullflush", 8))
184                         iommu_fullflush = 1;
185                 if (!strncmp(p, "nofullflush", 11))
186                         iommu_fullflush = 0;
187                 if (!strncmp(p, "forcesac", 8))
188                         iommu_sac_force = 1;
189                 if (!strncmp(p, "allowdac", 8))
190                         forbid_dac = 0;
191                 if (!strncmp(p, "nodac", 5))
192                         forbid_dac = -1;
193                 if (!strncmp(p, "usedac", 6)) {
194                         forbid_dac = -1;
195                         return 1;
196                 }
197 #ifdef CONFIG_SWIOTLB
198                 if (!strncmp(p, "soft", 4))
199                         swiotlb = 1;
200 #endif
201
202                 gart_parse_options(p);
203
204 #ifdef CONFIG_CALGARY_IOMMU
205                 if (!strncmp(p, "calgary", 7))
206                         use_calgary = 1;
207 #endif /* CONFIG_CALGARY_IOMMU */
208
209                 p += strcspn(p, ",");
210                 if (*p == ',')
211                         ++p;
212         }
213         return 0;
214 }
215 early_param("iommu", iommu_setup);
216
217 int dma_supported(struct device *dev, u64 mask)
218 {
219         struct dma_mapping_ops *ops = get_dma_ops(dev);
220
221 #ifdef CONFIG_PCI
222         if (mask > 0xffffffff && forbid_dac > 0) {
223                 dev_info(dev, "PCI: Disallowing DAC for device\n");
224                 return 0;
225         }
226 #endif
227
228         if (ops->dma_supported)
229                 return ops->dma_supported(dev, mask);
230
231         /* Copied from i386. Doesn't make much sense, because it will
232            only work for pci_alloc_coherent.
233            The caller just has to use GFP_DMA in this case. */
234         if (mask < DMA_24BIT_MASK)
235                 return 0;
236
237         /* Tell the device to use SAC when IOMMU force is on.  This
238            allows the driver to use cheaper accesses in some cases.
239
240            Problem with this is that if we overflow the IOMMU area and
241            return DAC as fallback address the device may not handle it
242            correctly.
243
244            As a special case some controllers have a 39bit address
245            mode that is as efficient as 32bit (aic79xx). Don't force
246            SAC for these.  Assume all masks <= 40 bits are of this
247            type. Normally this doesn't make any difference, but gives
248            more gentle handling of IOMMU overflow. */
249         if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
250                 dev_info(dev, "Force SAC with mask %Lx\n", mask);
251                 return 0;
252         }
253
254         return 1;
255 }
256 EXPORT_SYMBOL(dma_supported);
257
258 static int __init pci_iommu_init(void)
259 {
260         calgary_iommu_init();
261
262         intel_iommu_init();
263
264         amd_iommu_init();
265
266         gart_iommu_init();
267
268         no_iommu_init();
269         return 0;
270 }
271
272 void pci_iommu_shutdown(void)
273 {
274         gart_iommu_shutdown();
275 }
276 /* Must execute after PCI subsystem */
277 fs_initcall(pci_iommu_init);
278
279 #ifdef CONFIG_PCI
280 /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
281
282 static __devinit void via_no_dac(struct pci_dev *dev)
283 {
284         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
285                 printk(KERN_INFO "PCI: VIA PCI bridge detected."
286                                  "Disabling DAC.\n");
287                 forbid_dac = 1;
288         }
289 }
290 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
291 #endif