blob: b30209ec8c6e25a844fe933f13d254041217d7bd [file] [log] [blame]
Fenghua Yu62fdd762008-10-17 12:14:13 -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>
10#include <linux/dmar.h>
11#include <asm/iommu.h>
12#include <asm/machvec.h>
13#include <linux/dma-mapping.h>
14
Fenghua Yu62fdd762008-10-17 12:14:13 -070015#include <asm/system.h>
16
17#ifdef CONFIG_DMAR
18
19#include <linux/kernel.h>
Fenghua Yu62fdd762008-10-17 12:14:13 -070020
21#include <asm/page.h>
Fenghua Yu62fdd762008-10-17 12:14:13 -070022
23dma_addr_t bad_dma_address __read_mostly;
24EXPORT_SYMBOL(bad_dma_address);
25
26static int iommu_sac_force __read_mostly;
27
28int no_iommu __read_mostly;
29#ifdef CONFIG_IOMMU_DEBUG
30int force_iommu __read_mostly = 1;
31#else
32int force_iommu __read_mostly;
33#endif
34
Fenghua Yu62fdd762008-10-17 12:14:13 -070035/* Dummy device used for NULL arguments (normally ISA). Better would
36 be probably a smaller DMA mask, but this is bug-to-bug compatible
37 to i386. */
38struct device fallback_dev = {
39 .bus_id = "fallback device",
40 .coherent_dma_mask = DMA_32BIT_MASK,
41 .dma_mask = &fallback_dev.coherent_dma_mask,
42};
43
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090044extern struct dma_map_ops intel_dma_ops;
Fenghua Yu62fdd762008-10-17 12:14:13 -070045
46static int __init pci_iommu_init(void)
47{
48 if (iommu_detected)
49 intel_iommu_init();
50
51 return 0;
52}
53
54/* Must execute after PCI subsystem */
55fs_initcall(pci_iommu_init);
56
57void pci_iommu_shutdown(void)
58{
59 return;
60}
61
62void __init
63iommu_dma_init(void)
64{
65 return;
66}
67
Fenghua Yu62fdd762008-10-17 12:14:13 -070068int iommu_dma_supported(struct device *dev, u64 mask)
69{
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090070 struct dma_map_ops *ops = platform_dma_get_ops(dev);
Fenghua Yu62fdd762008-10-17 12:14:13 -070071
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090072 if (ops->dma_supported)
73 return ops->dma_supported(dev, mask);
Fenghua Yu62fdd762008-10-17 12:14:13 -070074
75 /* Copied from i386. Doesn't make much sense, because it will
76 only work for pci_alloc_coherent.
77 The caller just has to use GFP_DMA in this case. */
78 if (mask < DMA_24BIT_MASK)
79 return 0;
80
81 /* Tell the device to use SAC when IOMMU force is on. This
82 allows the driver to use cheaper accesses in some cases.
83
84 Problem with this is that if we overflow the IOMMU area and
85 return DAC as fallback address the device may not handle it
86 correctly.
87
88 As a special case some controllers have a 39bit address
89 mode that is as efficient as 32bit (aic79xx). Don't force
90 SAC for these. Assume all masks <= 40 bits are of this
91 type. Normally this doesn't make any difference, but gives
92 more gentle handling of IOMMU overflow. */
93 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
94 dev_info(dev, "Force SAC with mask %lx\n", mask);
95 return 0;
96 }
97
98 return 1;
99}
100EXPORT_SYMBOL(iommu_dma_supported);
101
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900102static int vtd_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
103{
104 return 0;
105}
106
107void __init pci_iommu_alloc(void)
108{
109 dma_ops = &intel_dma_ops;
110
111 dma_ops->sync_single_for_cpu = machvec_dma_sync_single;
112 dma_ops->sync_sg_for_cpu = machvec_dma_sync_sg;
113 dma_ops->sync_single_for_device = machvec_dma_sync_single;
114 dma_ops->sync_sg_for_device = machvec_dma_sync_sg;
115 dma_ops->dma_supported = iommu_dma_supported;
116 dma_ops->mapping_error = vtd_dma_mapping_error;
117
118 /*
119 * The order of these functions is important for
120 * fall-back/fail-over reasons
121 */
122 detect_intel_iommu();
123
124#ifdef CONFIG_SWIOTLB
125 pci_swiotlb_init();
126#endif
127}
128
Fenghua Yu62fdd762008-10-17 12:14:13 -0700129#endif