blob: 66b3dc5b3f45a7e0b3bfe0f6f8e2a7acaf12740e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dynamic DMA mapping support.
3 */
4
5#include <linux/types.h>
6#include <linux/mm.h>
7#include <linux/string.h>
8#include <linux/pci.h>
9#include <linux/module.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070010#include <linux/dmar.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/io.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020012#include <asm/gart.h>
Jon Masone4650582006-06-26 13:58:14 +020013#include <asm/calgary.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Andi Kleen94806262007-10-19 20:35:03 +020015int iommu_merge __read_mostly = 1;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010016EXPORT_SYMBOL(iommu_merge);
17
18dma_addr_t bad_dma_address __read_mostly;
19EXPORT_SYMBOL(bad_dma_address);
20
21/* This tells the BIO block layer to assume merging. Default to off
22 because we cannot guarantee merging later. */
23int iommu_bio_merge __read_mostly = 0;
24EXPORT_SYMBOL(iommu_bio_merge);
25
Jan Beulichcaa51712007-07-09 11:55:51 -070026static int iommu_sac_force __read_mostly = 0;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010027
28int no_iommu __read_mostly;
29#ifdef CONFIG_IOMMU_DEBUG
30int panic_on_overflow __read_mostly = 1;
31int force_iommu __read_mostly = 1;
32#else
33int panic_on_overflow __read_mostly = 0;
34int force_iommu __read_mostly= 0;
35#endif
36
Jon Mason8d4f6b92006-06-26 13:58:05 +020037/* Set this to 1 if there is a HW IOMMU in the system */
38int iommu_detected __read_mostly = 0;
39
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010040/* Dummy device used for NULL arguments (normally ISA). Better would
41 be probably a smaller DMA mask, but this is bug-to-bug compatible
42 to i386. */
43struct device fallback_dev = {
44 .bus_id = "fallback device",
Jon Mason9f2036f2006-06-26 13:56:19 +020045 .coherent_dma_mask = DMA_32BIT_MASK,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010046 .dma_mask = &fallback_dev.coherent_dma_mask,
47};
48
49/* Allocate DMA memory on node near device */
50noinline static void *
51dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010053 struct page *page;
54 int node;
Yinghai Luf6855f72007-10-17 18:04:35 +020055
56 node = dev_to_node(dev);
57 if (node == -1)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010058 node = numa_node_id();
Daniel Yeisley0d015322006-05-30 22:47:57 +020059
60 if (node < first_node(node_online_map))
61 node = first_node(node_online_map);
62
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010063 page = alloc_pages_node(node, gfp, order);
64 return page ? page_address(page) : NULL;
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010067/*
68 * Allocate memory for a coherent mapping.
69 */
70void *
71dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
72 gfp_t gfp)
73{
74 void *memory;
75 unsigned long dma_mask = 0;
76 u64 bus;
77
78 if (!dev)
79 dev = &fallback_dev;
80 dma_mask = dev->coherent_dma_mask;
81 if (dma_mask == 0)
Jon Mason9f2036f2006-06-26 13:56:19 +020082 dma_mask = DMA_32BIT_MASK;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010083
Andi Kleen81545492007-08-15 02:40:34 +020084 /* Device not DMA able */
85 if (dev->dma_mask == NULL)
86 return NULL;
87
Andi Kleen3056d6b2006-03-25 16:30:43 +010088 /* Don't invoke OOM killer */
89 gfp |= __GFP_NORETRY;
90
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010091 /* Kludge to make it bug-to-bug compatible with i386. i386
92 uses the normal dma_mask for alloc_coherent. */
93 dma_mask &= *dev->dma_mask;
94
95 /* Why <=? Even when the mask is smaller than 4GB it is often
96 larger than 16MB and in this case we have a chance of
97 finding fitting memory in the next higher zone first. If
98 not retry with true GFP_DMA. -AK */
Jon Mason9f2036f2006-06-26 13:56:19 +020099 if (dma_mask <= DMA_32BIT_MASK)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100100 gfp |= GFP_DMA32;
101
102 again:
103 memory = dma_alloc_pages(dev, gfp, get_order(size));
104 if (memory == NULL)
105 return NULL;
106
107 {
108 int high, mmu;
109 bus = virt_to_bus(memory);
110 high = (bus + size) >= dma_mask;
111 mmu = high;
112 if (force_iommu && !(gfp & GFP_DMA))
113 mmu = 1;
114 else if (high) {
115 free_pages((unsigned long)memory,
116 get_order(size));
117
118 /* Don't use the 16MB ZONE_DMA unless absolutely
119 needed. It's better to use remapping first. */
Jon Mason9f2036f2006-06-26 13:56:19 +0200120 if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100121 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
122 goto again;
123 }
124
Andi Kleen6bca52b2006-02-03 21:50:59 +0100125 /* Let low level make its own zone decisions */
126 gfp &= ~(GFP_DMA32|GFP_DMA);
127
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100128 if (dma_ops->alloc_coherent)
129 return dma_ops->alloc_coherent(dev, size,
130 dma_handle, gfp);
131 return NULL;
132 }
133
134 memset(memory, 0, size);
135 if (!mmu) {
136 *dma_handle = virt_to_bus(memory);
137 return memory;
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100140
141 if (dma_ops->alloc_coherent) {
142 free_pages((unsigned long)memory, get_order(size));
143 gfp &= ~(GFP_DMA|GFP_DMA32);
144 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
145 }
146
147 if (dma_ops->map_simple) {
148 *dma_handle = dma_ops->map_simple(dev, memory,
149 size,
150 PCI_DMA_BIDIRECTIONAL);
151 if (*dma_handle != bad_dma_address)
152 return memory;
153 }
154
155 if (panic_on_overflow)
156 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
157 free_pages((unsigned long)memory, get_order(size));
158 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100160EXPORT_SYMBOL(dma_alloc_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100162/*
163 * Unmap coherent memory.
164 * The caller must ensure that the device has finished accessing the mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100166void dma_free_coherent(struct device *dev, size_t size,
167 void *vaddr, dma_addr_t bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
David Brownellaa248862007-08-10 13:10:27 -0700169 WARN_ON(irqs_disabled()); /* for portability */
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100170 if (dma_ops->unmap_single)
171 dma_ops->unmap_single(dev, bus, size, 0);
172 free_pages((unsigned long)vaddr, get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100174EXPORT_SYMBOL(dma_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Andi Kleenece66842006-09-30 01:47:55 +0200176static int forbid_dac __read_mostly;
177
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100178int dma_supported(struct device *dev, u64 mask)
179{
Andi Kleenece66842006-09-30 01:47:55 +0200180#ifdef CONFIG_PCI
181 if (mask > 0xffffffff && forbid_dac > 0) {
182
183
184
185 printk(KERN_INFO "PCI: Disallowing DAC for device %s\n", dev->bus_id);
186 return 0;
187 }
188#endif
189
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100190 if (dma_ops->dma_supported)
191 return dma_ops->dma_supported(dev, mask);
192
193 /* Copied from i386. Doesn't make much sense, because it will
194 only work for pci_alloc_coherent.
195 The caller just has to use GFP_DMA in this case. */
Jon Mason9f2036f2006-06-26 13:56:19 +0200196 if (mask < DMA_24BIT_MASK)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100197 return 0;
198
199 /* Tell the device to use SAC when IOMMU force is on. This
200 allows the driver to use cheaper accesses in some cases.
201
202 Problem with this is that if we overflow the IOMMU area and
203 return DAC as fallback address the device may not handle it
204 correctly.
205
206 As a special case some controllers have a 39bit address
207 mode that is as efficient as 32bit (aic79xx). Don't force
208 SAC for these. Assume all masks <= 40 bits are of this
209 type. Normally this doesn't make any difference, but gives
210 more gentle handling of IOMMU overflow. */
Jon Mason9f2036f2006-06-26 13:56:19 +0200211 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100212 printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
213 return 0;
214 }
215
216 return 1;
217}
218EXPORT_SYMBOL(dma_supported);
219
220int dma_set_mask(struct device *dev, u64 mask)
221{
222 if (!dev->dma_mask || !dma_supported(dev, mask))
223 return -EIO;
224 *dev->dma_mask = mask;
225 return 0;
226}
227EXPORT_SYMBOL(dma_set_mask);
228
Karsten Weiss55588702007-02-13 13:26:21 +0100229/*
230 * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
231 * documentation.
232 */
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100233__init int iommu_setup(char *p)
234{
Andi Kleended318e2006-09-30 01:47:55 +0200235 iommu_merge = 1;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100236
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200237 if (!p)
238 return -EINVAL;
239
Andi Kleended318e2006-09-30 01:47:55 +0200240 while (*p) {
241 if (!strncmp(p,"off",3))
242 no_iommu = 1;
243 /* gart_parse_options has more force support */
244 if (!strncmp(p,"force",5))
245 force_iommu = 1;
246 if (!strncmp(p,"noforce",7)) {
247 iommu_merge = 0;
248 force_iommu = 0;
249 }
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100250
Andi Kleended318e2006-09-30 01:47:55 +0200251 if (!strncmp(p, "biomerge",8)) {
252 iommu_bio_merge = 4096;
253 iommu_merge = 1;
254 force_iommu = 1;
255 }
256 if (!strncmp(p, "panic",5))
257 panic_on_overflow = 1;
258 if (!strncmp(p, "nopanic",7))
259 panic_on_overflow = 0;
260 if (!strncmp(p, "merge",5)) {
261 iommu_merge = 1;
262 force_iommu = 1;
263 }
264 if (!strncmp(p, "nomerge",7))
265 iommu_merge = 0;
266 if (!strncmp(p, "forcesac",8))
267 iommu_sac_force = 1;
268 if (!strncmp(p, "allowdac", 8))
269 forbid_dac = 0;
270 if (!strncmp(p, "nodac", 5))
271 forbid_dac = -1;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100272
273#ifdef CONFIG_SWIOTLB
Andi Kleended318e2006-09-30 01:47:55 +0200274 if (!strncmp(p, "soft",4))
275 swiotlb = 1;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100276#endif
277
Joerg Roedel966396d2007-10-24 12:49:48 +0200278#ifdef CONFIG_GART_IOMMU
Andi Kleended318e2006-09-30 01:47:55 +0200279 gart_parse_options(p);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100280#endif
281
Muli Ben-Yehudabff65472006-12-07 02:14:07 +0100282#ifdef CONFIG_CALGARY_IOMMU
283 if (!strncmp(p, "calgary", 7))
284 use_calgary = 1;
285#endif /* CONFIG_CALGARY_IOMMU */
286
Andi Kleended318e2006-09-30 01:47:55 +0200287 p += strcspn(p, ",");
288 if (*p == ',')
289 ++p;
290 }
291 return 0;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100292}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200293early_param("iommu", iommu_setup);
Jon Mason0dc243a2006-06-26 13:58:11 +0200294
295void __init pci_iommu_alloc(void)
296{
297 /*
298 * The order of these functions is important for
299 * fall-back/fail-over reasons
300 */
Joerg Roedel966396d2007-10-24 12:49:48 +0200301#ifdef CONFIG_GART_IOMMU
Jon Mason0dc243a2006-06-26 13:58:11 +0200302 iommu_hole_init();
303#endif
304
Jon Masone4650582006-06-26 13:58:14 +0200305#ifdef CONFIG_CALGARY_IOMMU
306 detect_calgary();
307#endif
308
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700309 detect_intel_iommu();
310
Jon Mason0dc243a2006-06-26 13:58:11 +0200311#ifdef CONFIG_SWIOTLB
312 pci_swiotlb_init();
313#endif
314}
315
316static int __init pci_iommu_init(void)
317{
Jon Masone4650582006-06-26 13:58:14 +0200318#ifdef CONFIG_CALGARY_IOMMU
319 calgary_iommu_init();
320#endif
321
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700322 intel_iommu_init();
323
Joerg Roedel966396d2007-10-24 12:49:48 +0200324#ifdef CONFIG_GART_IOMMU
Jon Mason0dc243a2006-06-26 13:58:11 +0200325 gart_iommu_init();
326#endif
327
328 no_iommu_init();
329 return 0;
330}
331
Yinghai Lubc2cea62007-07-21 17:11:28 +0200332void pci_iommu_shutdown(void)
333{
334 gart_iommu_shutdown();
335}
336
Andi Kleen388c19e2007-06-20 12:23:32 +0200337#ifdef CONFIG_PCI
338/* Many VIA bridges seem to corrupt data for DAC. Disable it here */
339
340static __devinit void via_no_dac(struct pci_dev *dev)
341{
342 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
343 printk(KERN_INFO "PCI: VIA PCI bridge detected. Disabling DAC.\n");
344 forbid_dac = 1;
345 }
346}
347DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
348#endif
Jon Mason0dc243a2006-06-26 13:58:11 +0200349/* Must execute after PCI subsystem */
350fs_initcall(pci_iommu_init);