blob: dea01672f1f4d56cc530aee2f7c1ca1fc86ef78f [file] [log] [blame]
Glauber Costa459121c92008-04-08 13:20:43 -03001#include <linux/dma-mapping.h>
Glauber Costacb5867a2008-04-08 13:20:51 -03002#include <linux/dmar.h>
Glauber Costa116890d2008-04-08 13:20:54 -03003#include <linux/bootmem.h>
Glauber Costabca5c092008-04-08 13:20:53 -03004#include <linux/pci.h>
Glauber Costacb5867a2008-04-08 13:20:51 -03005
Glauber Costa116890d2008-04-08 13:20:54 -03006#include <asm/proto.h>
7#include <asm/dma.h>
Glauber Costacb5867a2008-04-08 13:20:51 -03008#include <asm/gart.h>
9#include <asm/calgary.h>
Joerg Roedela69ca342008-06-26 21:28:08 +020010#include <asm/amd_iommu.h>
Glauber Costa459121c92008-04-08 13:20:43 -030011
Glauber Costabca5c092008-04-08 13:20:53 -030012int forbid_dac __read_mostly;
13EXPORT_SYMBOL(forbid_dac);
14
Glauber Costa85c246e2008-04-08 13:20:50 -030015const struct dma_mapping_ops *dma_ops;
16EXPORT_SYMBOL(dma_ops);
17
Dmitri Vorobievb4cdc432008-04-28 03:15:58 +040018static int iommu_sac_force __read_mostly;
Glauber Costa8e0c3792008-04-08 13:20:55 -030019
Glauber Costaf9c258d2008-04-08 13:20:52 -030020#ifdef CONFIG_IOMMU_DEBUG
21int panic_on_overflow __read_mostly = 1;
22int force_iommu __read_mostly = 1;
23#else
24int panic_on_overflow __read_mostly = 0;
25int force_iommu __read_mostly = 0;
26#endif
27
Glauber Costafae9a0d2008-04-08 13:20:56 -030028int iommu_merge __read_mostly = 0;
29
30int no_iommu __read_mostly;
31/* Set this to 1 if there is a HW IOMMU in the system */
32int iommu_detected __read_mostly = 0;
33
34/* This tells the BIO block layer to assume merging. Default to off
35 because we cannot guarantee merging later. */
36int iommu_bio_merge __read_mostly = 0;
37EXPORT_SYMBOL(iommu_bio_merge);
38
Glauber Costacac67872008-04-08 13:21:00 -030039dma_addr_t bad_dma_address __read_mostly = 0;
40EXPORT_SYMBOL(bad_dma_address);
Glauber Costafae9a0d2008-04-08 13:20:56 -030041
Glauber Costa098cb7f2008-04-09 13:18:10 -030042/* Dummy device used for NULL arguments (normally ISA). Better would
43 be probably a smaller DMA mask, but this is bug-to-bug compatible
44 to older i386. */
45struct device fallback_dev = {
46 .bus_id = "fallback device",
47 .coherent_dma_mask = DMA_32BIT_MASK,
48 .dma_mask = &fallback_dev.coherent_dma_mask,
49};
50
Glauber Costa459121c92008-04-08 13:20:43 -030051int dma_set_mask(struct device *dev, u64 mask)
52{
53 if (!dev->dma_mask || !dma_supported(dev, mask))
54 return -EIO;
55
56 *dev->dma_mask = mask;
57
58 return 0;
59}
60EXPORT_SYMBOL(dma_set_mask);
61
Glauber Costa116890d2008-04-08 13:20:54 -030062#ifdef CONFIG_X86_64
63static __initdata void *dma32_bootmem_ptr;
64static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
65
66static int __init parse_dma32_size_opt(char *p)
67{
68 if (!p)
69 return -EINVAL;
70 dma32_bootmem_size = memparse(p, &p);
71 return 0;
72}
73early_param("dma32_size", parse_dma32_size_opt);
74
75void __init dma32_reserve_bootmem(void)
76{
77 unsigned long size, align;
78 if (end_pfn <= MAX_DMA32_PFN)
79 return;
80
81 align = 64ULL<<20;
82 size = round_up(dma32_bootmem_size, align);
83 dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
84 __pa(MAX_DMA_ADDRESS));
85 if (dma32_bootmem_ptr)
86 dma32_bootmem_size = size;
87 else
88 dma32_bootmem_size = 0;
89}
90static void __init dma32_free_bootmem(void)
91{
92 int node;
93
94 if (end_pfn <= MAX_DMA32_PFN)
95 return;
96
97 if (!dma32_bootmem_ptr)
98 return;
99
100 for_each_online_node(node)
101 free_bootmem_node(NODE_DATA(node), __pa(dma32_bootmem_ptr),
102 dma32_bootmem_size);
103
104 dma32_bootmem_ptr = NULL;
105 dma32_bootmem_size = 0;
106}
107
108void __init pci_iommu_alloc(void)
109{
110 /* free the range so iommu could get some range less than 4G */
111 dma32_free_bootmem();
112 /*
113 * The order of these functions is important for
114 * fall-back/fail-over reasons
115 */
116#ifdef CONFIG_GART_IOMMU
117 gart_iommu_hole_init();
118#endif
119
120#ifdef CONFIG_CALGARY_IOMMU
121 detect_calgary();
122#endif
123
124 detect_intel_iommu();
125
Joerg Roedela69ca342008-06-26 21:28:08 +0200126 amd_iommu_detect();
127
Glauber Costa116890d2008-04-08 13:20:54 -0300128#ifdef CONFIG_SWIOTLB
129 pci_swiotlb_init();
130#endif
131}
132#endif
133
Glauber Costafae9a0d2008-04-08 13:20:56 -0300134/*
135 * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
136 * documentation.
137 */
138static __init int iommu_setup(char *p)
139{
140 iommu_merge = 1;
141
142 if (!p)
143 return -EINVAL;
144
145 while (*p) {
146 if (!strncmp(p, "off", 3))
147 no_iommu = 1;
148 /* gart_parse_options has more force support */
149 if (!strncmp(p, "force", 5))
150 force_iommu = 1;
151 if (!strncmp(p, "noforce", 7)) {
152 iommu_merge = 0;
153 force_iommu = 0;
154 }
155
156 if (!strncmp(p, "biomerge", 8)) {
157 iommu_bio_merge = 4096;
158 iommu_merge = 1;
159 force_iommu = 1;
160 }
161 if (!strncmp(p, "panic", 5))
162 panic_on_overflow = 1;
163 if (!strncmp(p, "nopanic", 7))
164 panic_on_overflow = 0;
165 if (!strncmp(p, "merge", 5)) {
166 iommu_merge = 1;
167 force_iommu = 1;
168 }
169 if (!strncmp(p, "nomerge", 7))
170 iommu_merge = 0;
171 if (!strncmp(p, "forcesac", 8))
172 iommu_sac_force = 1;
173 if (!strncmp(p, "allowdac", 8))
174 forbid_dac = 0;
175 if (!strncmp(p, "nodac", 5))
176 forbid_dac = -1;
177 if (!strncmp(p, "usedac", 6)) {
178 forbid_dac = -1;
179 return 1;
180 }
181#ifdef CONFIG_SWIOTLB
182 if (!strncmp(p, "soft", 4))
183 swiotlb = 1;
184#endif
185
186#ifdef CONFIG_GART_IOMMU
187 gart_parse_options(p);
188#endif
189
190#ifdef CONFIG_CALGARY_IOMMU
191 if (!strncmp(p, "calgary", 7))
192 use_calgary = 1;
193#endif /* CONFIG_CALGARY_IOMMU */
194
195 p += strcspn(p, ",");
196 if (*p == ',')
197 ++p;
198 }
199 return 0;
200}
201early_param("iommu", iommu_setup);
202
Glauber Costa8e8edc62008-04-08 13:20:57 -0300203#ifdef CONFIG_X86_32
204int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
205 dma_addr_t device_addr, size_t size, int flags)
206{
207 void __iomem *mem_base = NULL;
208 int pages = size >> PAGE_SHIFT;
209 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
210
211 if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
212 goto out;
213 if (!size)
214 goto out;
215 if (dev->dma_mem)
216 goto out;
217
218 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
219
220 mem_base = ioremap(bus_addr, size);
221 if (!mem_base)
222 goto out;
223
224 dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
225 if (!dev->dma_mem)
226 goto out;
227 dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
228 if (!dev->dma_mem->bitmap)
229 goto free1_out;
230
231 dev->dma_mem->virt_base = mem_base;
232 dev->dma_mem->device_base = device_addr;
233 dev->dma_mem->size = pages;
234 dev->dma_mem->flags = flags;
235
236 if (flags & DMA_MEMORY_MAP)
237 return DMA_MEMORY_MAP;
238
239 return DMA_MEMORY_IO;
240
241 free1_out:
242 kfree(dev->dma_mem);
243 out:
244 if (mem_base)
245 iounmap(mem_base);
246 return 0;
247}
248EXPORT_SYMBOL(dma_declare_coherent_memory);
249
250void dma_release_declared_memory(struct device *dev)
251{
252 struct dma_coherent_mem *mem = dev->dma_mem;
253
254 if (!mem)
255 return;
256 dev->dma_mem = NULL;
257 iounmap(mem->virt_base);
258 kfree(mem->bitmap);
259 kfree(mem);
260}
261EXPORT_SYMBOL(dma_release_declared_memory);
262
263void *dma_mark_declared_memory_occupied(struct device *dev,
264 dma_addr_t device_addr, size_t size)
265{
266 struct dma_coherent_mem *mem = dev->dma_mem;
267 int pos, err;
268 int pages = (size + (device_addr & ~PAGE_MASK) + PAGE_SIZE - 1);
269
270 pages >>= PAGE_SHIFT;
271
272 if (!mem)
273 return ERR_PTR(-EINVAL);
274
275 pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
276 err = bitmap_allocate_region(mem->bitmap, pos, get_order(pages));
277 if (err != 0)
278 return ERR_PTR(err);
279 return mem->virt_base + (pos << PAGE_SHIFT);
280}
281EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
Glauber Costa098cb7f2008-04-09 13:18:10 -0300282
283static int dma_alloc_from_coherent_mem(struct device *dev, ssize_t size,
284 dma_addr_t *dma_handle, void **ret)
285{
286 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
287 int order = get_order(size);
288
289 if (mem) {
290 int page = bitmap_find_free_region(mem->bitmap, mem->size,
291 order);
292 if (page >= 0) {
293 *dma_handle = mem->device_base + (page << PAGE_SHIFT);
294 *ret = mem->virt_base + (page << PAGE_SHIFT);
295 memset(*ret, 0, size);
296 }
297 if (mem->flags & DMA_MEMORY_EXCLUSIVE)
298 *ret = NULL;
299 }
300 return (mem != NULL);
301}
302
303static int dma_release_coherent(struct device *dev, int order, void *vaddr)
304{
305 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
306
307 if (mem && vaddr >= mem->virt_base && vaddr <
308 (mem->virt_base + (mem->size << PAGE_SHIFT))) {
309 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
310
311 bitmap_release_region(mem->bitmap, page, order);
312 return 1;
313 }
314 return 0;
315}
316#else
317#define dma_alloc_from_coherent_mem(dev, size, handle, ret) (0)
318#define dma_release_coherent(dev, order, vaddr) (0)
Glauber Costa8e8edc62008-04-08 13:20:57 -0300319#endif /* CONFIG_X86_32 */
320
Glauber Costa8e0c3792008-04-08 13:20:55 -0300321int dma_supported(struct device *dev, u64 mask)
322{
323#ifdef CONFIG_PCI
324 if (mask > 0xffffffff && forbid_dac > 0) {
325 printk(KERN_INFO "PCI: Disallowing DAC for device %s\n",
326 dev->bus_id);
327 return 0;
328 }
329#endif
330
331 if (dma_ops->dma_supported)
332 return dma_ops->dma_supported(dev, mask);
333
334 /* Copied from i386. Doesn't make much sense, because it will
335 only work for pci_alloc_coherent.
336 The caller just has to use GFP_DMA in this case. */
337 if (mask < DMA_24BIT_MASK)
338 return 0;
339
340 /* Tell the device to use SAC when IOMMU force is on. This
341 allows the driver to use cheaper accesses in some cases.
342
343 Problem with this is that if we overflow the IOMMU area and
344 return DAC as fallback address the device may not handle it
345 correctly.
346
347 As a special case some controllers have a 39bit address
348 mode that is as efficient as 32bit (aic79xx). Don't force
349 SAC for these. Assume all masks <= 40 bits are of this
350 type. Normally this doesn't make any difference, but gives
351 more gentle handling of IOMMU overflow. */
352 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
353 printk(KERN_INFO "%s: Force SAC with mask %Lx\n",
354 dev->bus_id, mask);
355 return 0;
356 }
357
358 return 1;
359}
360EXPORT_SYMBOL(dma_supported);
361
Glauber Costa098cb7f2008-04-09 13:18:10 -0300362/* Allocate DMA memory on node near device */
363noinline struct page *
364dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
365{
366 int node;
367
368 node = dev_to_node(dev);
369
370 return alloc_pages_node(node, gfp, order);
371}
372
373/*
374 * Allocate memory for a coherent mapping.
375 */
376void *
377dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
378 gfp_t gfp)
379{
380 void *memory = NULL;
381 struct page *page;
382 unsigned long dma_mask = 0;
383 dma_addr_t bus;
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200384 int noretry = 0;
Glauber Costa098cb7f2008-04-09 13:18:10 -0300385
386 /* ignore region specifiers */
387 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
388
389 if (dma_alloc_from_coherent_mem(dev, size, dma_handle, &memory))
390 return memory;
391
Takashi Iwai4a367f32008-05-09 08:06:55 +0200392 if (!dev) {
Glauber Costa098cb7f2008-04-09 13:18:10 -0300393 dev = &fallback_dev;
Takashi Iwai4a367f32008-05-09 08:06:55 +0200394 gfp |= GFP_DMA;
395 }
Glauber Costa098cb7f2008-04-09 13:18:10 -0300396 dma_mask = dev->coherent_dma_mask;
397 if (dma_mask == 0)
Takashi Iwai4a367f32008-05-09 08:06:55 +0200398 dma_mask = (gfp & GFP_DMA) ? DMA_24BIT_MASK : DMA_32BIT_MASK;
Glauber Costa098cb7f2008-04-09 13:18:10 -0300399
400 /* Device not DMA able */
401 if (dev->dma_mask == NULL)
402 return NULL;
403
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200404 /* Don't invoke OOM killer or retry in lower 16MB DMA zone */
405 if (gfp & __GFP_DMA)
406 noretry = 1;
407
Glauber Costa098cb7f2008-04-09 13:18:10 -0300408#ifdef CONFIG_X86_64
409 /* Why <=? Even when the mask is smaller than 4GB it is often
410 larger than 16MB and in this case we have a chance of
411 finding fitting memory in the next higher zone first. If
412 not retry with true GFP_DMA. -AK */
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200413 if (dma_mask <= DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
Glauber Costa098cb7f2008-04-09 13:18:10 -0300414 gfp |= GFP_DMA32;
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200415 if (dma_mask < DMA_32BIT_MASK)
416 noretry = 1;
417 }
Glauber Costa098cb7f2008-04-09 13:18:10 -0300418#endif
419
420 again:
Miquel van Smoorenburgdb9f6002008-05-28 10:31:25 +0200421 page = dma_alloc_pages(dev,
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200422 noretry ? gfp | __GFP_NORETRY : gfp, get_order(size));
Glauber Costa098cb7f2008-04-09 13:18:10 -0300423 if (page == NULL)
424 return NULL;
425
426 {
427 int high, mmu;
428 bus = page_to_phys(page);
429 memory = page_address(page);
430 high = (bus + size) >= dma_mask;
431 mmu = high;
432 if (force_iommu && !(gfp & GFP_DMA))
433 mmu = 1;
434 else if (high) {
435 free_pages((unsigned long)memory,
436 get_order(size));
437
438 /* Don't use the 16MB ZONE_DMA unless absolutely
439 needed. It's better to use remapping first. */
440 if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
441 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
442 goto again;
443 }
444
445 /* Let low level make its own zone decisions */
446 gfp &= ~(GFP_DMA32|GFP_DMA);
447
448 if (dma_ops->alloc_coherent)
449 return dma_ops->alloc_coherent(dev, size,
450 dma_handle, gfp);
451 return NULL;
452 }
453
454 memset(memory, 0, size);
455 if (!mmu) {
456 *dma_handle = bus;
457 return memory;
458 }
459 }
460
461 if (dma_ops->alloc_coherent) {
462 free_pages((unsigned long)memory, get_order(size));
463 gfp &= ~(GFP_DMA|GFP_DMA32);
464 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
465 }
466
467 if (dma_ops->map_simple) {
468 *dma_handle = dma_ops->map_simple(dev, virt_to_phys(memory),
469 size,
470 PCI_DMA_BIDIRECTIONAL);
471 if (*dma_handle != bad_dma_address)
472 return memory;
473 }
474
475 if (panic_on_overflow)
476 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",
477 (unsigned long)size);
478 free_pages((unsigned long)memory, get_order(size));
479 return NULL;
480}
481EXPORT_SYMBOL(dma_alloc_coherent);
482
483/*
484 * Unmap coherent memory.
485 * The caller must ensure that the device has finished accessing the mapping.
486 */
487void dma_free_coherent(struct device *dev, size_t size,
488 void *vaddr, dma_addr_t bus)
489{
490 int order = get_order(size);
491 WARN_ON(irqs_disabled()); /* for portability */
492 if (dma_release_coherent(dev, order, vaddr))
493 return;
494 if (dma_ops->unmap_single)
495 dma_ops->unmap_single(dev, bus, size, 0);
496 free_pages((unsigned long)vaddr, order);
497}
498EXPORT_SYMBOL(dma_free_coherent);
Glauber Costa8e0c3792008-04-08 13:20:55 -0300499
Glauber Costacb5867a2008-04-08 13:20:51 -0300500static int __init pci_iommu_init(void)
501{
502#ifdef CONFIG_CALGARY_IOMMU
503 calgary_iommu_init();
504#endif
Glauber Costa459121c92008-04-08 13:20:43 -0300505
Glauber Costacb5867a2008-04-08 13:20:51 -0300506 intel_iommu_init();
507
Joerg Roedela69ca342008-06-26 21:28:08 +0200508 amd_iommu_init();
509
Glauber Costacb5867a2008-04-08 13:20:51 -0300510#ifdef CONFIG_GART_IOMMU
511 gart_iommu_init();
512#endif
513
514 no_iommu_init();
515 return 0;
516}
517
518void pci_iommu_shutdown(void)
519{
520 gart_iommu_shutdown();
521}
522/* Must execute after PCI subsystem */
523fs_initcall(pci_iommu_init);
Glauber Costabca5c092008-04-08 13:20:53 -0300524
525#ifdef CONFIG_PCI
526/* Many VIA bridges seem to corrupt data for DAC. Disable it here */
527
528static __devinit void via_no_dac(struct pci_dev *dev)
529{
530 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
531 printk(KERN_INFO "PCI: VIA PCI bridge detected."
532 "Disabling DAC.\n");
533 forbid_dac = 1;
534 }
535}
536DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
537#endif