blob: 8dbffb846de9b66c5a1aec5cc542dc959acfa924 [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>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +09008#include <asm/iommu.h>
Glauber Costacb5867a2008-04-08 13:20:51 -03009#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
Jan Beulich08e1a132008-07-18 13:44:16 +010012static int forbid_dac __read_mostly;
Glauber Costabca5c092008-04-08 13:20:53 -030013
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -070014struct dma_mapping_ops *dma_ops;
Glauber Costa85c246e2008-04-08 13:20:50 -030015EXPORT_SYMBOL(dma_ops);
16
Dmitri Vorobievb4cdc432008-04-28 03:15:58 +040017static int iommu_sac_force __read_mostly;
Glauber Costa8e0c3792008-04-08 13:20:55 -030018
Glauber Costaf9c258d2008-04-08 13:20:52 -030019#ifdef CONFIG_IOMMU_DEBUG
20int panic_on_overflow __read_mostly = 1;
21int force_iommu __read_mostly = 1;
22#else
23int panic_on_overflow __read_mostly = 0;
24int force_iommu __read_mostly = 0;
25#endif
26
Glauber Costafae9a0d2008-04-08 13:20:56 -030027int iommu_merge __read_mostly = 0;
28
29int no_iommu __read_mostly;
30/* Set this to 1 if there is a HW IOMMU in the system */
31int 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. */
35int iommu_bio_merge __read_mostly = 0;
36EXPORT_SYMBOL(iommu_bio_merge);
37
Glauber Costacac67872008-04-08 13:21:00 -030038dma_addr_t bad_dma_address __read_mostly = 0;
39EXPORT_SYMBOL(bad_dma_address);
Glauber Costafae9a0d2008-04-08 13:20:56 -030040
Glauber Costa098cb7f2008-04-09 13:18:10 -030041/* 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. */
44struct 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
Glauber Costa459121c92008-04-08 13:20:43 -030050int 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}
59EXPORT_SYMBOL(dma_set_mask);
60
Glauber Costa116890d2008-04-08 13:20:54 -030061#ifdef CONFIG_X86_64
62static __initdata void *dma32_bootmem_ptr;
63static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
64
65static 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}
72early_param("dma32_size", parse_dma32_size_opt);
73
74void __init dma32_reserve_bootmem(void)
75{
76 unsigned long size, align;
Yinghai Luc987d122008-06-24 22:14:09 -070077 if (max_pfn <= MAX_DMA32_PFN)
Glauber Costa116890d2008-04-08 13:20:54 -030078 return;
79
Yinghai Lu7677b2e2008-04-14 20:40:37 -070080 /*
81 * check aperture_64.c allocate_aperture() for reason about
82 * using 512M as goal
83 */
Glauber Costa116890d2008-04-08 13:20:54 -030084 align = 64ULL<<20;
85 size = round_up(dma32_bootmem_size, align);
86 dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
Yinghai Lu7677b2e2008-04-14 20:40:37 -070087 512ULL<<20);
Glauber Costa116890d2008-04-08 13:20:54 -030088 if (dma32_bootmem_ptr)
89 dma32_bootmem_size = size;
90 else
91 dma32_bootmem_size = 0;
92}
93static void __init dma32_free_bootmem(void)
94{
Glauber Costa116890d2008-04-08 13:20:54 -030095
Yinghai Luc987d122008-06-24 22:14:09 -070096 if (max_pfn <= MAX_DMA32_PFN)
Glauber Costa116890d2008-04-08 13:20:54 -030097 return;
98
99 if (!dma32_bootmem_ptr)
100 return;
101
Yinghai Lu330fce22008-04-19 01:31:45 -0700102 free_bootmem(__pa(dma32_bootmem_ptr), dma32_bootmem_size);
Glauber Costa116890d2008-04-08 13:20:54 -0300103
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 */
Glauber Costa116890d2008-04-08 13:20:54 -0300116 gart_iommu_hole_init();
Glauber Costa116890d2008-04-08 13:20:54 -0300117
Glauber Costa116890d2008-04-08 13:20:54 -0300118 detect_calgary();
Glauber Costa116890d2008-04-08 13:20:54 -0300119
120 detect_intel_iommu();
121
Joerg Roedela69ca342008-06-26 21:28:08 +0200122 amd_iommu_detect();
123
Glauber Costa116890d2008-04-08 13:20:54 -0300124 pci_swiotlb_init();
Glauber Costa116890d2008-04-08 13:20:54 -0300125}
126#endif
127
Glauber Costafae9a0d2008-04-08 13:20:56 -0300128/*
129 * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
130 * documentation.
131 */
132static __init int iommu_setup(char *p)
133{
134 iommu_merge = 1;
135
136 if (!p)
137 return -EINVAL;
138
139 while (*p) {
140 if (!strncmp(p, "off", 3))
141 no_iommu = 1;
142 /* gart_parse_options has more force support */
143 if (!strncmp(p, "force", 5))
144 force_iommu = 1;
145 if (!strncmp(p, "noforce", 7)) {
146 iommu_merge = 0;
147 force_iommu = 0;
148 }
149
150 if (!strncmp(p, "biomerge", 8)) {
151 iommu_bio_merge = 4096;
152 iommu_merge = 1;
153 force_iommu = 1;
154 }
155 if (!strncmp(p, "panic", 5))
156 panic_on_overflow = 1;
157 if (!strncmp(p, "nopanic", 7))
158 panic_on_overflow = 0;
159 if (!strncmp(p, "merge", 5)) {
160 iommu_merge = 1;
161 force_iommu = 1;
162 }
163 if (!strncmp(p, "nomerge", 7))
164 iommu_merge = 0;
165 if (!strncmp(p, "forcesac", 8))
166 iommu_sac_force = 1;
167 if (!strncmp(p, "allowdac", 8))
168 forbid_dac = 0;
169 if (!strncmp(p, "nodac", 5))
170 forbid_dac = -1;
171 if (!strncmp(p, "usedac", 6)) {
172 forbid_dac = -1;
173 return 1;
174 }
175#ifdef CONFIG_SWIOTLB
176 if (!strncmp(p, "soft", 4))
177 swiotlb = 1;
178#endif
179
Glauber Costafae9a0d2008-04-08 13:20:56 -0300180 gart_parse_options(p);
Glauber Costafae9a0d2008-04-08 13:20:56 -0300181
182#ifdef CONFIG_CALGARY_IOMMU
183 if (!strncmp(p, "calgary", 7))
184 use_calgary = 1;
185#endif /* CONFIG_CALGARY_IOMMU */
186
187 p += strcspn(p, ",");
188 if (*p == ',')
189 ++p;
190 }
191 return 0;
192}
193early_param("iommu", iommu_setup);
194
Glauber Costa8e0c3792008-04-08 13:20:55 -0300195int dma_supported(struct device *dev, u64 mask)
196{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700197 struct dma_mapping_ops *ops = get_dma_ops(dev);
198
Glauber Costa8e0c3792008-04-08 13:20:55 -0300199#ifdef CONFIG_PCI
200 if (mask > 0xffffffff && forbid_dac > 0) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200201 dev_info(dev, "PCI: Disallowing DAC for device\n");
Glauber Costa8e0c3792008-04-08 13:20:55 -0300202 return 0;
203 }
204#endif
205
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700206 if (ops->dma_supported)
207 return ops->dma_supported(dev, mask);
Glauber Costa8e0c3792008-04-08 13:20:55 -0300208
209 /* Copied from i386. Doesn't make much sense, because it will
210 only work for pci_alloc_coherent.
211 The caller just has to use GFP_DMA in this case. */
212 if (mask < DMA_24BIT_MASK)
213 return 0;
214
215 /* Tell the device to use SAC when IOMMU force is on. This
216 allows the driver to use cheaper accesses in some cases.
217
218 Problem with this is that if we overflow the IOMMU area and
219 return DAC as fallback address the device may not handle it
220 correctly.
221
222 As a special case some controllers have a 39bit address
223 mode that is as efficient as 32bit (aic79xx). Don't force
224 SAC for these. Assume all masks <= 40 bits are of this
225 type. Normally this doesn't make any difference, but gives
226 more gentle handling of IOMMU overflow. */
227 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200228 dev_info(dev, "Force SAC with mask %Lx\n", mask);
Glauber Costa8e0c3792008-04-08 13:20:55 -0300229 return 0;
230 }
231
232 return 1;
233}
234EXPORT_SYMBOL(dma_supported);
235
Glauber Costa098cb7f2008-04-09 13:18:10 -0300236/* Allocate DMA memory on node near device */
Adrian Bunk311f8342008-05-12 15:43:37 +0200237static noinline struct page *
Glauber Costa098cb7f2008-04-09 13:18:10 -0300238dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
239{
240 int node;
241
242 node = dev_to_node(dev);
243
244 return alloc_pages_node(node, gfp, order);
245}
246
247/*
248 * Allocate memory for a coherent mapping.
249 */
250void *
251dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
252 gfp_t gfp)
253{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700254 struct dma_mapping_ops *ops = get_dma_ops(dev);
Glauber Costa098cb7f2008-04-09 13:18:10 -0300255 void *memory = NULL;
256 struct page *page;
257 unsigned long dma_mask = 0;
258 dma_addr_t bus;
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200259 int noretry = 0;
Glauber Costa098cb7f2008-04-09 13:18:10 -0300260
261 /* ignore region specifiers */
262 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
263
Dmitry Baryshkov323ec002008-06-29 14:19:31 +0400264 if (dma_alloc_from_coherent(dev, size, dma_handle, &memory))
Glauber Costa098cb7f2008-04-09 13:18:10 -0300265 return memory;
266
Takashi Iwai4a367f32008-05-09 08:06:55 +0200267 if (!dev) {
Glauber Costa098cb7f2008-04-09 13:18:10 -0300268 dev = &fallback_dev;
Takashi Iwai4a367f32008-05-09 08:06:55 +0200269 gfp |= GFP_DMA;
270 }
Glauber Costa098cb7f2008-04-09 13:18:10 -0300271 dma_mask = dev->coherent_dma_mask;
272 if (dma_mask == 0)
Takashi Iwai4a367f32008-05-09 08:06:55 +0200273 dma_mask = (gfp & GFP_DMA) ? DMA_24BIT_MASK : DMA_32BIT_MASK;
Glauber Costa098cb7f2008-04-09 13:18:10 -0300274
275 /* Device not DMA able */
276 if (dev->dma_mask == NULL)
277 return NULL;
278
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200279 /* Don't invoke OOM killer or retry in lower 16MB DMA zone */
280 if (gfp & __GFP_DMA)
281 noretry = 1;
282
Glauber Costa098cb7f2008-04-09 13:18:10 -0300283#ifdef CONFIG_X86_64
284 /* Why <=? Even when the mask is smaller than 4GB it is often
285 larger than 16MB and in this case we have a chance of
286 finding fitting memory in the next higher zone first. If
287 not retry with true GFP_DMA. -AK */
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200288 if (dma_mask <= DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
Glauber Costa098cb7f2008-04-09 13:18:10 -0300289 gfp |= GFP_DMA32;
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200290 if (dma_mask < DMA_32BIT_MASK)
291 noretry = 1;
292 }
Glauber Costa098cb7f2008-04-09 13:18:10 -0300293#endif
294
295 again:
Miquel van Smoorenburgdb9f6002008-05-28 10:31:25 +0200296 page = dma_alloc_pages(dev,
Miquel van Smoorenburgb7f09ae2008-06-05 18:14:44 +0200297 noretry ? gfp | __GFP_NORETRY : gfp, get_order(size));
Glauber Costa098cb7f2008-04-09 13:18:10 -0300298 if (page == NULL)
299 return NULL;
300
301 {
302 int high, mmu;
303 bus = page_to_phys(page);
304 memory = page_address(page);
305 high = (bus + size) >= dma_mask;
306 mmu = high;
307 if (force_iommu && !(gfp & GFP_DMA))
308 mmu = 1;
309 else if (high) {
310 free_pages((unsigned long)memory,
311 get_order(size));
312
313 /* Don't use the 16MB ZONE_DMA unless absolutely
314 needed. It's better to use remapping first. */
315 if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
316 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
317 goto again;
318 }
319
320 /* Let low level make its own zone decisions */
321 gfp &= ~(GFP_DMA32|GFP_DMA);
322
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700323 if (ops->alloc_coherent)
324 return ops->alloc_coherent(dev, size,
Glauber Costa098cb7f2008-04-09 13:18:10 -0300325 dma_handle, gfp);
326 return NULL;
327 }
328
329 memset(memory, 0, size);
330 if (!mmu) {
331 *dma_handle = bus;
332 return memory;
333 }
334 }
335
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700336 if (ops->alloc_coherent) {
Glauber Costa098cb7f2008-04-09 13:18:10 -0300337 free_pages((unsigned long)memory, get_order(size));
338 gfp &= ~(GFP_DMA|GFP_DMA32);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700339 return ops->alloc_coherent(dev, size, dma_handle, gfp);
Glauber Costa098cb7f2008-04-09 13:18:10 -0300340 }
341
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700342 if (ops->map_simple) {
343 *dma_handle = ops->map_simple(dev, virt_to_phys(memory),
Glauber Costa098cb7f2008-04-09 13:18:10 -0300344 size,
345 PCI_DMA_BIDIRECTIONAL);
346 if (*dma_handle != bad_dma_address)
347 return memory;
348 }
349
350 if (panic_on_overflow)
351 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",
352 (unsigned long)size);
353 free_pages((unsigned long)memory, get_order(size));
354 return NULL;
355}
356EXPORT_SYMBOL(dma_alloc_coherent);
357
358/*
359 * Unmap coherent memory.
360 * The caller must ensure that the device has finished accessing the mapping.
361 */
362void dma_free_coherent(struct device *dev, size_t size,
363 void *vaddr, dma_addr_t bus)
364{
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700365 struct dma_mapping_ops *ops = get_dma_ops(dev);
366
Glauber Costa098cb7f2008-04-09 13:18:10 -0300367 int order = get_order(size);
368 WARN_ON(irqs_disabled()); /* for portability */
Dmitry Baryshkov323ec002008-06-29 14:19:31 +0400369 if (dma_release_from_coherent(dev, order, vaddr))
Glauber Costa098cb7f2008-04-09 13:18:10 -0300370 return;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700371 if (ops->unmap_single)
372 ops->unmap_single(dev, bus, size, 0);
Glauber Costa098cb7f2008-04-09 13:18:10 -0300373 free_pages((unsigned long)vaddr, order);
374}
375EXPORT_SYMBOL(dma_free_coherent);
Glauber Costa8e0c3792008-04-08 13:20:55 -0300376
Glauber Costacb5867a2008-04-08 13:20:51 -0300377static int __init pci_iommu_init(void)
378{
Glauber Costacb5867a2008-04-08 13:20:51 -0300379 calgary_iommu_init();
Glauber Costa459121c92008-04-08 13:20:43 -0300380
Glauber Costacb5867a2008-04-08 13:20:51 -0300381 intel_iommu_init();
382
Joerg Roedela69ca342008-06-26 21:28:08 +0200383 amd_iommu_init();
384
Glauber Costacb5867a2008-04-08 13:20:51 -0300385 gart_iommu_init();
Glauber Costacb5867a2008-04-08 13:20:51 -0300386
387 no_iommu_init();
388 return 0;
389}
390
391void pci_iommu_shutdown(void)
392{
393 gart_iommu_shutdown();
394}
395/* Must execute after PCI subsystem */
396fs_initcall(pci_iommu_init);
Glauber Costabca5c092008-04-08 13:20:53 -0300397
398#ifdef CONFIG_PCI
399/* Many VIA bridges seem to corrupt data for DAC. Disable it here */
400
401static __devinit void via_no_dac(struct pci_dev *dev)
402{
403 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
404 printk(KERN_INFO "PCI: VIA PCI bridge detected."
405 "Disabling DAC.\n");
406 forbid_dac = 1;
407 }
408}
409DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
410#endif