blob: 873aa079d166d9a5769d551b67fde84321649b6d [file] [log] [blame]
Glauber Costa459121c92008-04-08 13:20:43 -03001#include <linux/dma-mapping.h>
Joerg Roedel2118d0c2009-01-09 15:13:15 +01002#include <linux/dma-debug.h>
Glauber Costacb5867a2008-04-08 13:20:51 -03003#include <linux/dmar.h>
Glauber Costa116890d2008-04-08 13:20:54 -03004#include <linux/bootmem.h>
Glauber Costabca5c092008-04-08 13:20:53 -03005#include <linux/pci.h>
Glauber Costacb5867a2008-04-08 13:20:51 -03006
Glauber Costa116890d2008-04-08 13:20:54 -03007#include <asm/proto.h>
8#include <asm/dma.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +09009#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010010#include <asm/gart.h>
Glauber Costacb5867a2008-04-08 13:20:51 -030011#include <asm/calgary.h>
Joerg Roedela69ca342008-06-26 21:28:08 +020012#include <asm/amd_iommu.h>
Glauber Costa459121c92008-04-08 13:20:43 -030013
Fenghua Yu3b15e582008-10-23 16:51:00 -070014static int forbid_dac __read_mostly;
15
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090016struct dma_map_ops *dma_ops;
Glauber Costa85c246e2008-04-08 13:20:50 -030017EXPORT_SYMBOL(dma_ops);
18
Dmitri Vorobievb4cdc432008-04-28 03:15:58 +040019static int iommu_sac_force __read_mostly;
Glauber Costa8e0c3792008-04-08 13:20:55 -030020
Glauber Costaf9c258d2008-04-08 13:20:52 -030021#ifdef CONFIG_IOMMU_DEBUG
22int panic_on_overflow __read_mostly = 1;
23int force_iommu __read_mostly = 1;
24#else
25int panic_on_overflow __read_mostly = 0;
26int force_iommu __read_mostly = 0;
27#endif
28
Glauber Costafae9a0d2008-04-08 13:20:56 -030029int iommu_merge __read_mostly = 0;
30
31int no_iommu __read_mostly;
32/* Set this to 1 if there is a HW IOMMU in the system */
33int iommu_detected __read_mostly = 0;
34
Joerg Roedelac0101d2009-09-01 16:00:35 +020035/*
36 * This variable becomes 1 if iommu=pt is passed on the kernel command line.
37 * If this variable is 1, IOMMU implementations do no DMA ranslation for
38 * devices and allow every device to access to whole physical memory. This is
39 * useful if a user want to use an IOMMU only for KVM device assignment to
40 * guests and not for driver dma translation.
41 */
42int iommu_pass_through __read_mostly;
Fenghua Yuaed5d5f2009-04-30 17:57:11 -070043
Glauber Costacac67872008-04-08 13:21:00 -030044dma_addr_t bad_dma_address __read_mostly = 0;
45EXPORT_SYMBOL(bad_dma_address);
Glauber Costafae9a0d2008-04-08 13:20:56 -030046
Glauber Costa098cb7f2008-04-09 13:18:10 -030047/* Dummy device used for NULL arguments (normally ISA). Better would
48 be probably a smaller DMA mask, but this is bug-to-bug compatible
49 to older i386. */
Joerg Roedel6c505ce2008-08-19 16:32:45 +020050struct device x86_dma_fallback_dev = {
Kay Sievers1a927132008-10-30 02:17:49 +010051 .init_name = "fallback device",
Yang Hongyang284901a2009-04-06 19:01:15 -070052 .coherent_dma_mask = DMA_BIT_MASK(32),
Joerg Roedel6c505ce2008-08-19 16:32:45 +020053 .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
Glauber Costa098cb7f2008-04-09 13:18:10 -030054};
Joerg Roedel6c505ce2008-08-19 16:32:45 +020055EXPORT_SYMBOL(x86_dma_fallback_dev);
Glauber Costa098cb7f2008-04-09 13:18:10 -030056
Joerg Roedel2118d0c2009-01-09 15:13:15 +010057/* Number of entries preallocated for DMA-API debugging */
58#define PREALLOC_DMA_DEBUG_ENTRIES 32768
59
Glauber Costa459121c92008-04-08 13:20:43 -030060int dma_set_mask(struct device *dev, u64 mask)
61{
62 if (!dev->dma_mask || !dma_supported(dev, mask))
63 return -EIO;
64
65 *dev->dma_mask = mask;
66
67 return 0;
68}
69EXPORT_SYMBOL(dma_set_mask);
70
Glauber Costa116890d2008-04-08 13:20:54 -030071#ifdef CONFIG_X86_64
72static __initdata void *dma32_bootmem_ptr;
73static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
74
75static int __init parse_dma32_size_opt(char *p)
76{
77 if (!p)
78 return -EINVAL;
79 dma32_bootmem_size = memparse(p, &p);
80 return 0;
81}
82early_param("dma32_size", parse_dma32_size_opt);
83
84void __init dma32_reserve_bootmem(void)
85{
86 unsigned long size, align;
Yinghai Luc987d122008-06-24 22:14:09 -070087 if (max_pfn <= MAX_DMA32_PFN)
Glauber Costa116890d2008-04-08 13:20:54 -030088 return;
89
Yinghai Lu7677b2e2008-04-14 20:40:37 -070090 /*
91 * check aperture_64.c allocate_aperture() for reason about
92 * using 512M as goal
93 */
Glauber Costa116890d2008-04-08 13:20:54 -030094 align = 64ULL<<20;
Joerg Roedel1ddb5512008-07-25 16:48:55 +020095 size = roundup(dma32_bootmem_size, align);
Glauber Costa116890d2008-04-08 13:20:54 -030096 dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
Yinghai Lu7677b2e2008-04-14 20:40:37 -070097 512ULL<<20);
Glauber Costa116890d2008-04-08 13:20:54 -030098 if (dma32_bootmem_ptr)
99 dma32_bootmem_size = size;
100 else
101 dma32_bootmem_size = 0;
102}
103static void __init dma32_free_bootmem(void)
104{
Glauber Costa116890d2008-04-08 13:20:54 -0300105
Yinghai Luc987d122008-06-24 22:14:09 -0700106 if (max_pfn <= MAX_DMA32_PFN)
Glauber Costa116890d2008-04-08 13:20:54 -0300107 return;
108
109 if (!dma32_bootmem_ptr)
110 return;
111
Yinghai Lu330fce22008-04-19 01:31:45 -0700112 free_bootmem(__pa(dma32_bootmem_ptr), dma32_bootmem_size);
Glauber Costa116890d2008-04-08 13:20:54 -0300113
114 dma32_bootmem_ptr = NULL;
115 dma32_bootmem_size = 0;
116}
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -0800117#endif
Glauber Costa116890d2008-04-08 13:20:54 -0300118
119void __init pci_iommu_alloc(void)
120{
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -0800121#ifdef CONFIG_X86_64
Glauber Costa116890d2008-04-08 13:20:54 -0300122 /* free the range so iommu could get some range less than 4G */
123 dma32_free_bootmem();
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -0800124#endif
125
Glauber Costa116890d2008-04-08 13:20:54 -0300126 /*
127 * The order of these functions is important for
128 * fall-back/fail-over reasons
129 */
Glauber Costa116890d2008-04-08 13:20:54 -0300130 gart_iommu_hole_init();
Glauber Costa116890d2008-04-08 13:20:54 -0300131
Glauber Costa116890d2008-04-08 13:20:54 -0300132 detect_calgary();
Glauber Costa116890d2008-04-08 13:20:54 -0300133
134 detect_intel_iommu();
135
Joerg Roedela69ca342008-06-26 21:28:08 +0200136 amd_iommu_detect();
137
Glauber Costa116890d2008-04-08 13:20:54 -0300138 pci_swiotlb_init();
Glauber Costa116890d2008-04-08 13:20:54 -0300139}
FUJITA Tomonori8978b742008-07-29 13:38:53 +0900140
FUJITA Tomonori9f6ac572008-09-24 20:48:35 +0900141void *dma_generic_alloc_coherent(struct device *dev, size_t size,
142 dma_addr_t *dma_addr, gfp_t flag)
143{
144 unsigned long dma_mask;
145 struct page *page;
146 dma_addr_t addr;
147
148 dma_mask = dma_alloc_coherent_mask(dev, flag);
149
150 flag |= __GFP_ZERO;
151again:
152 page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
153 if (!page)
154 return NULL;
155
156 addr = page_to_phys(page);
157 if (!is_buffer_dma_capable(dma_mask, addr, size)) {
158 __free_pages(page, get_order(size));
159
Yang Hongyang284901a2009-04-06 19:01:15 -0700160 if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
FUJITA Tomonori9f6ac572008-09-24 20:48:35 +0900161 flag = (flag & ~GFP_DMA32) | GFP_DMA;
162 goto again;
163 }
164
165 return NULL;
166 }
167
168 *dma_addr = addr;
169 return page_address(page);
170}
171
Glauber Costafae9a0d2008-04-08 13:20:56 -0300172/*
173 * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
174 * documentation.
175 */
176static __init int iommu_setup(char *p)
177{
178 iommu_merge = 1;
179
180 if (!p)
181 return -EINVAL;
182
183 while (*p) {
184 if (!strncmp(p, "off", 3))
185 no_iommu = 1;
186 /* gart_parse_options has more force support */
187 if (!strncmp(p, "force", 5))
188 force_iommu = 1;
189 if (!strncmp(p, "noforce", 7)) {
190 iommu_merge = 0;
191 force_iommu = 0;
192 }
193
194 if (!strncmp(p, "biomerge", 8)) {
Glauber Costafae9a0d2008-04-08 13:20:56 -0300195 iommu_merge = 1;
196 force_iommu = 1;
197 }
198 if (!strncmp(p, "panic", 5))
199 panic_on_overflow = 1;
200 if (!strncmp(p, "nopanic", 7))
201 panic_on_overflow = 0;
202 if (!strncmp(p, "merge", 5)) {
203 iommu_merge = 1;
204 force_iommu = 1;
205 }
206 if (!strncmp(p, "nomerge", 7))
207 iommu_merge = 0;
208 if (!strncmp(p, "forcesac", 8))
209 iommu_sac_force = 1;
210 if (!strncmp(p, "allowdac", 8))
211 forbid_dac = 0;
212 if (!strncmp(p, "nodac", 5))
213 forbid_dac = -1;
214 if (!strncmp(p, "usedac", 6)) {
215 forbid_dac = -1;
216 return 1;
217 }
218#ifdef CONFIG_SWIOTLB
219 if (!strncmp(p, "soft", 4))
220 swiotlb = 1;
David Woodhouse3238c0c2009-07-01 18:56:16 +0100221#endif
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700222 if (!strncmp(p, "pt", 2)) {
223 iommu_pass_through = 1;
224 return 1;
225 }
Glauber Costafae9a0d2008-04-08 13:20:56 -0300226
Glauber Costafae9a0d2008-04-08 13:20:56 -0300227 gart_parse_options(p);
Glauber Costafae9a0d2008-04-08 13:20:56 -0300228
229#ifdef CONFIG_CALGARY_IOMMU
230 if (!strncmp(p, "calgary", 7))
231 use_calgary = 1;
232#endif /* CONFIG_CALGARY_IOMMU */
233
234 p += strcspn(p, ",");
235 if (*p == ',')
236 ++p;
237 }
238 return 0;
239}
240early_param("iommu", iommu_setup);
241
Glauber Costa8e0c3792008-04-08 13:20:55 -0300242int dma_supported(struct device *dev, u64 mask)
243{
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900244 struct dma_map_ops *ops = get_dma_ops(dev);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700245
Glauber Costa8e0c3792008-04-08 13:20:55 -0300246#ifdef CONFIG_PCI
247 if (mask > 0xffffffff && forbid_dac > 0) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200248 dev_info(dev, "PCI: Disallowing DAC for device\n");
Glauber Costa8e0c3792008-04-08 13:20:55 -0300249 return 0;
250 }
251#endif
252
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700253 if (ops->dma_supported)
254 return ops->dma_supported(dev, mask);
Glauber Costa8e0c3792008-04-08 13:20:55 -0300255
256 /* Copied from i386. Doesn't make much sense, because it will
257 only work for pci_alloc_coherent.
258 The caller just has to use GFP_DMA in this case. */
Yang Hongyang2f4f27d2009-04-06 19:01:18 -0700259 if (mask < DMA_BIT_MASK(24))
Glauber Costa8e0c3792008-04-08 13:20:55 -0300260 return 0;
261
262 /* Tell the device to use SAC when IOMMU force is on. This
263 allows the driver to use cheaper accesses in some cases.
264
265 Problem with this is that if we overflow the IOMMU area and
266 return DAC as fallback address the device may not handle it
267 correctly.
268
269 As a special case some controllers have a 39bit address
270 mode that is as efficient as 32bit (aic79xx). Don't force
271 SAC for these. Assume all masks <= 40 bits are of this
272 type. Normally this doesn't make any difference, but gives
273 more gentle handling of IOMMU overflow. */
Yang Hongyang50cf1562009-04-06 19:01:14 -0700274 if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200275 dev_info(dev, "Force SAC with mask %Lx\n", mask);
Glauber Costa8e0c3792008-04-08 13:20:55 -0300276 return 0;
277 }
278
279 return 1;
280}
281EXPORT_SYMBOL(dma_supported);
282
Glauber Costacb5867a2008-04-08 13:20:51 -0300283static int __init pci_iommu_init(void)
284{
Joerg Roedel2118d0c2009-01-09 15:13:15 +0100285 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
286
Joerg Roedel86f31952009-03-16 17:50:28 +0100287#ifdef CONFIG_PCI
288 dma_debug_add_bus(&pci_bus_type);
289#endif
290
Glauber Costacb5867a2008-04-08 13:20:51 -0300291 calgary_iommu_init();
Glauber Costa459121c92008-04-08 13:20:43 -0300292
Glauber Costacb5867a2008-04-08 13:20:51 -0300293 intel_iommu_init();
294
Joerg Roedela69ca342008-06-26 21:28:08 +0200295 amd_iommu_init();
296
Glauber Costacb5867a2008-04-08 13:20:51 -0300297 gart_iommu_init();
Glauber Costacb5867a2008-04-08 13:20:51 -0300298
299 no_iommu_init();
300 return 0;
301}
302
303void pci_iommu_shutdown(void)
304{
305 gart_iommu_shutdown();
Joerg Roedel09759042009-06-09 17:52:27 +0200306
307 amd_iommu_shutdown();
Glauber Costacb5867a2008-04-08 13:20:51 -0300308}
309/* Must execute after PCI subsystem */
310fs_initcall(pci_iommu_init);
Fenghua Yu3b15e582008-10-23 16:51:00 -0700311
312#ifdef CONFIG_PCI
313/* Many VIA bridges seem to corrupt data for DAC. Disable it here */
314
315static __devinit void via_no_dac(struct pci_dev *dev)
316{
317 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
Bjorn Helgaas13bf7572009-02-24 10:38:22 -0700318 dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
Fenghua Yu3b15e582008-10-23 16:51:00 -0700319 forbid_dac = 1;
320 }
321}
322DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
323#endif