x86, VisWS: turn into generic arch, eliminate leftover files
[cascardo/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/gart.h>
9 #include <asm/calgary.h>
10 #include <asm/amd_iommu.h>
11
12 static int forbid_dac __read_mostly;
13
14 const struct dma_mapping_ops *dma_ops;
15 EXPORT_SYMBOL(dma_ops);
16
17 static int iommu_sac_force __read_mostly;
18
19 #ifdef CONFIG_IOMMU_DEBUG
20 int panic_on_overflow __read_mostly = 1;
21 int force_iommu __read_mostly = 1;
22 #else
23 int panic_on_overflow __read_mostly = 0;
24 int force_iommu __read_mostly = 0;
25 #endif
26
27 int iommu_merge __read_mostly = 0;
28
29 int no_iommu __read_mostly;
30 /* Set this to 1 if there is a HW IOMMU in the system */
31 int iommu_detected __read_mostly = 0;
32
33 /* This tells the BIO block layer to assume merging. Default to off
34    because we cannot guarantee merging later. */
35 int iommu_bio_merge __read_mostly = 0;
36 EXPORT_SYMBOL(iommu_bio_merge);
37
38 dma_addr_t bad_dma_address __read_mostly = 0;
39 EXPORT_SYMBOL(bad_dma_address);
40
41 /* Dummy device used for NULL arguments (normally ISA). Better would
42    be probably a smaller DMA mask, but this is bug-to-bug compatible
43    to older i386. */
44 struct device fallback_dev = {
45         .bus_id = "fallback device",
46         .coherent_dma_mask = DMA_32BIT_MASK,
47         .dma_mask = &fallback_dev.coherent_dma_mask,
48 };
49
50 int dma_set_mask(struct device *dev, u64 mask)
51 {
52         if (!dev->dma_mask || !dma_supported(dev, mask))
53                 return -EIO;
54
55         *dev->dma_mask = mask;
56
57         return 0;
58 }
59 EXPORT_SYMBOL(dma_set_mask);
60
61 #ifdef CONFIG_X86_64
62 static __initdata void *dma32_bootmem_ptr;
63 static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
64
65 static int __init parse_dma32_size_opt(char *p)
66 {
67         if (!p)
68                 return -EINVAL;
69         dma32_bootmem_size = memparse(p, &p);
70         return 0;
71 }
72 early_param("dma32_size", parse_dma32_size_opt);
73
74 void __init dma32_reserve_bootmem(void)
75 {
76         unsigned long size, align;
77         if (max_pfn <= MAX_DMA32_PFN)
78                 return;
79
80         /*
81          * check aperture_64.c allocate_aperture() for reason about
82          * using 512M as goal
83          */
84         align = 64ULL<<20;
85         size = round_up(dma32_bootmem_size, align);
86         dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
87                                  512ULL<<20);
88         if (dma32_bootmem_ptr)
89                 dma32_bootmem_size = size;
90         else
91                 dma32_bootmem_size = 0;
92 }
93 static void __init dma32_free_bootmem(void)
94 {
95
96         if (max_pfn <= MAX_DMA32_PFN)
97                 return;
98
99         if (!dma32_bootmem_ptr)
100                 return;
101
102         free_bootmem(__pa(dma32_bootmem_ptr), dma32_bootmem_size);
103
104         dma32_bootmem_ptr = NULL;
105         dma32_bootmem_size = 0;
106 }
107
108 void __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
126         amd_iommu_detect();
127
128 #ifdef CONFIG_SWIOTLB
129         pci_swiotlb_init();
130 #endif
131 }
132 #endif
133
134 /*
135  * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
136  * documentation.
137  */
138 static __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 }
201 early_param("iommu", iommu_setup);
202
203 #ifdef CONFIG_X86_32
204 int 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 }
248 EXPORT_SYMBOL(dma_declare_coherent_memory);
249
250 void 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 }
261 EXPORT_SYMBOL(dma_release_declared_memory);
262
263 void *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 }
281 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
282
283 static 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
303 static 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)
319 #endif /* CONFIG_X86_32 */
320
321 int 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 }
360 EXPORT_SYMBOL(dma_supported);
361
362 /* Allocate DMA memory on node near device */
363 static noinline struct page *
364 dma_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  */
376 void *
377 dma_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;
384         int noretry = 0;
385
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
392         if (!dev) {
393                 dev = &fallback_dev;
394                 gfp |= GFP_DMA;
395         }
396         dma_mask = dev->coherent_dma_mask;
397         if (dma_mask == 0)
398                 dma_mask = (gfp & GFP_DMA) ? DMA_24BIT_MASK : DMA_32BIT_MASK;
399
400         /* Device not DMA able */
401         if (dev->dma_mask == NULL)
402                 return NULL;
403
404         /* Don't invoke OOM killer or retry in lower 16MB DMA zone */
405         if (gfp & __GFP_DMA)
406                 noretry = 1;
407
408 #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 */
413         if (dma_mask <= DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
414                 gfp |= GFP_DMA32;
415                 if (dma_mask < DMA_32BIT_MASK)
416                         noretry = 1;
417         }
418 #endif
419
420  again:
421         page = dma_alloc_pages(dev,
422                 noretry ? gfp | __GFP_NORETRY : gfp, get_order(size));
423         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 }
481 EXPORT_SYMBOL(dma_alloc_coherent);
482
483 /*
484  * Unmap coherent memory.
485  * The caller must ensure that the device has finished accessing the mapping.
486  */
487 void 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 }
498 EXPORT_SYMBOL(dma_free_coherent);
499
500 static int __init pci_iommu_init(void)
501 {
502 #ifdef CONFIG_CALGARY_IOMMU
503         calgary_iommu_init();
504 #endif
505
506         intel_iommu_init();
507
508         amd_iommu_init();
509
510 #ifdef CONFIG_GART_IOMMU
511         gart_iommu_init();
512 #endif
513
514         no_iommu_init();
515         return 0;
516 }
517
518 void pci_iommu_shutdown(void)
519 {
520         gart_iommu_shutdown();
521 }
522 /* Must execute after PCI subsystem */
523 fs_initcall(pci_iommu_init);
524
525 #ifdef CONFIG_PCI
526 /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
527
528 static __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 }
536 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
537 #endif