blob: 48cccbe51aa5b31170b5d17b50bb222d62bb515c [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>
Glauber Costa459121c92008-04-08 13:20:43 -030010
Glauber Costabca5c092008-04-08 13:20:53 -030011int forbid_dac __read_mostly;
12EXPORT_SYMBOL(forbid_dac);
13
Glauber Costa85c246e2008-04-08 13:20:50 -030014const struct dma_mapping_ops *dma_ops;
15EXPORT_SYMBOL(dma_ops);
16
Glauber Costaf9c258d2008-04-08 13:20:52 -030017#ifdef CONFIG_IOMMU_DEBUG
18int panic_on_overflow __read_mostly = 1;
19int force_iommu __read_mostly = 1;
20#else
21int panic_on_overflow __read_mostly = 0;
22int force_iommu __read_mostly = 0;
23#endif
24
Glauber Costa459121c92008-04-08 13:20:43 -030025int dma_set_mask(struct device *dev, u64 mask)
26{
27 if (!dev->dma_mask || !dma_supported(dev, mask))
28 return -EIO;
29
30 *dev->dma_mask = mask;
31
32 return 0;
33}
34EXPORT_SYMBOL(dma_set_mask);
35
Glauber Costa116890d2008-04-08 13:20:54 -030036#ifdef CONFIG_X86_64
37static __initdata void *dma32_bootmem_ptr;
38static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
39
40static int __init parse_dma32_size_opt(char *p)
41{
42 if (!p)
43 return -EINVAL;
44 dma32_bootmem_size = memparse(p, &p);
45 return 0;
46}
47early_param("dma32_size", parse_dma32_size_opt);
48
49void __init dma32_reserve_bootmem(void)
50{
51 unsigned long size, align;
52 if (end_pfn <= MAX_DMA32_PFN)
53 return;
54
55 align = 64ULL<<20;
56 size = round_up(dma32_bootmem_size, align);
57 dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
58 __pa(MAX_DMA_ADDRESS));
59 if (dma32_bootmem_ptr)
60 dma32_bootmem_size = size;
61 else
62 dma32_bootmem_size = 0;
63}
64static void __init dma32_free_bootmem(void)
65{
66 int node;
67
68 if (end_pfn <= MAX_DMA32_PFN)
69 return;
70
71 if (!dma32_bootmem_ptr)
72 return;
73
74 for_each_online_node(node)
75 free_bootmem_node(NODE_DATA(node), __pa(dma32_bootmem_ptr),
76 dma32_bootmem_size);
77
78 dma32_bootmem_ptr = NULL;
79 dma32_bootmem_size = 0;
80}
81
82void __init pci_iommu_alloc(void)
83{
84 /* free the range so iommu could get some range less than 4G */
85 dma32_free_bootmem();
86 /*
87 * The order of these functions is important for
88 * fall-back/fail-over reasons
89 */
90#ifdef CONFIG_GART_IOMMU
91 gart_iommu_hole_init();
92#endif
93
94#ifdef CONFIG_CALGARY_IOMMU
95 detect_calgary();
96#endif
97
98 detect_intel_iommu();
99
100#ifdef CONFIG_SWIOTLB
101 pci_swiotlb_init();
102#endif
103}
104#endif
105
Glauber Costacb5867a2008-04-08 13:20:51 -0300106static int __init pci_iommu_init(void)
107{
108#ifdef CONFIG_CALGARY_IOMMU
109 calgary_iommu_init();
110#endif
Glauber Costa459121c92008-04-08 13:20:43 -0300111
Glauber Costacb5867a2008-04-08 13:20:51 -0300112 intel_iommu_init();
113
114#ifdef CONFIG_GART_IOMMU
115 gart_iommu_init();
116#endif
117
118 no_iommu_init();
119 return 0;
120}
121
122void pci_iommu_shutdown(void)
123{
124 gart_iommu_shutdown();
125}
126/* Must execute after PCI subsystem */
127fs_initcall(pci_iommu_init);
Glauber Costabca5c092008-04-08 13:20:53 -0300128
129#ifdef CONFIG_PCI
130/* Many VIA bridges seem to corrupt data for DAC. Disable it here */
131
132static __devinit void via_no_dac(struct pci_dev *dev)
133{
134 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
135 printk(KERN_INFO "PCI: VIA PCI bridge detected."
136 "Disabling DAC.\n");
137 forbid_dac = 1;
138 }
139}
140DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
141#endif