blob: ae13e34f724874469a4565a5bcfce7858b7cf4a3 [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
Fenghua Yuaed5d5f2009-04-30 17:57:11 -070035int iommu_pass_through;
36
Glauber Costacac67872008-04-08 13:21:00 -030037dma_addr_t bad_dma_address __read_mostly = 0;
38EXPORT_SYMBOL(bad_dma_address);
Glauber Costafae9a0d2008-04-08 13:20:56 -030039
Glauber Costa098cb7f2008-04-09 13:18:10 -030040/* Dummy device used for NULL arguments (normally ISA). Better would
41 be probably a smaller DMA mask, but this is bug-to-bug compatible
42 to older i386. */
Joerg Roedel6c505ce2008-08-19 16:32:45 +020043struct device x86_dma_fallback_dev = {
Kay Sievers1a927132008-10-30 02:17:49 +010044 .init_name = "fallback device",
Yang Hongyang284901a2009-04-06 19:01:15 -070045 .coherent_dma_mask = DMA_BIT_MASK(32),
Joerg Roedel6c505ce2008-08-19 16:32:45 +020046 .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
Glauber Costa098cb7f2008-04-09 13:18:10 -030047};
Joerg Roedel6c505ce2008-08-19 16:32:45 +020048EXPORT_SYMBOL(x86_dma_fallback_dev);
Glauber Costa098cb7f2008-04-09 13:18:10 -030049
Joerg Roedel2118d0c2009-01-09 15:13:15 +010050/* Number of entries preallocated for DMA-API debugging */
51#define PREALLOC_DMA_DEBUG_ENTRIES 32768
52
Glauber Costa459121c92008-04-08 13:20:43 -030053int dma_set_mask(struct device *dev, u64 mask)
54{
55 if (!dev->dma_mask || !dma_supported(dev, mask))
56 return -EIO;
57
58 *dev->dma_mask = mask;
59
60 return 0;
61}
62EXPORT_SYMBOL(dma_set_mask);
63
Glauber Costa116890d2008-04-08 13:20:54 -030064#ifdef CONFIG_X86_64
65static __initdata void *dma32_bootmem_ptr;
66static unsigned long dma32_bootmem_size __initdata = (128ULL<<20);
67
68static int __init parse_dma32_size_opt(char *p)
69{
70 if (!p)
71 return -EINVAL;
72 dma32_bootmem_size = memparse(p, &p);
73 return 0;
74}
75early_param("dma32_size", parse_dma32_size_opt);
76
77void __init dma32_reserve_bootmem(void)
78{
79 unsigned long size, align;
Yinghai Luc987d122008-06-24 22:14:09 -070080 if (max_pfn <= MAX_DMA32_PFN)
Glauber Costa116890d2008-04-08 13:20:54 -030081 return;
82
Yinghai Lu7677b2e2008-04-14 20:40:37 -070083 /*
84 * check aperture_64.c allocate_aperture() for reason about
85 * using 512M as goal
86 */
Glauber Costa116890d2008-04-08 13:20:54 -030087 align = 64ULL<<20;
Joerg Roedel1ddb5512008-07-25 16:48:55 +020088 size = roundup(dma32_bootmem_size, align);
Glauber Costa116890d2008-04-08 13:20:54 -030089 dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align,
Yinghai Lu7677b2e2008-04-14 20:40:37 -070090 512ULL<<20);
Glauber Costa116890d2008-04-08 13:20:54 -030091 if (dma32_bootmem_ptr)
92 dma32_bootmem_size = size;
93 else
94 dma32_bootmem_size = 0;
95}
96static void __init dma32_free_bootmem(void)
97{
Glauber Costa116890d2008-04-08 13:20:54 -030098
Yinghai Luc987d122008-06-24 22:14:09 -070099 if (max_pfn <= MAX_DMA32_PFN)
Glauber Costa116890d2008-04-08 13:20:54 -0300100 return;
101
102 if (!dma32_bootmem_ptr)
103 return;
104
Yinghai Lu330fce22008-04-19 01:31:45 -0700105 free_bootmem(__pa(dma32_bootmem_ptr), dma32_bootmem_size);
Glauber Costa116890d2008-04-08 13:20:54 -0300106
107 dma32_bootmem_ptr = NULL;
108 dma32_bootmem_size = 0;
109}
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -0800110#endif
Glauber Costa116890d2008-04-08 13:20:54 -0300111
112void __init pci_iommu_alloc(void)
113{
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -0800114#ifdef CONFIG_X86_64
Glauber Costa116890d2008-04-08 13:20:54 -0300115 /* free the range so iommu could get some range less than 4G */
116 dma32_free_bootmem();
Jeremy Fitzhardingecfb80c92008-12-16 12:17:36 -0800117#endif
118
Glauber Costa116890d2008-04-08 13:20:54 -0300119 /*
120 * The order of these functions is important for
121 * fall-back/fail-over reasons
122 */
Glauber Costa116890d2008-04-08 13:20:54 -0300123 gart_iommu_hole_init();
Glauber Costa116890d2008-04-08 13:20:54 -0300124
Glauber Costa116890d2008-04-08 13:20:54 -0300125 detect_calgary();
Glauber Costa116890d2008-04-08 13:20:54 -0300126
127 detect_intel_iommu();
128
Joerg Roedela69ca342008-06-26 21:28:08 +0200129 amd_iommu_detect();
130
Glauber Costa116890d2008-04-08 13:20:54 -0300131 pci_swiotlb_init();
Glauber Costa116890d2008-04-08 13:20:54 -0300132}
FUJITA Tomonori8978b742008-07-29 13:38:53 +0900133
FUJITA Tomonori9f6ac572008-09-24 20:48:35 +0900134void *dma_generic_alloc_coherent(struct device *dev, size_t size,
135 dma_addr_t *dma_addr, gfp_t flag)
136{
137 unsigned long dma_mask;
138 struct page *page;
139 dma_addr_t addr;
140
141 dma_mask = dma_alloc_coherent_mask(dev, flag);
142
143 flag |= __GFP_ZERO;
144again:
145 page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
146 if (!page)
147 return NULL;
148
149 addr = page_to_phys(page);
150 if (!is_buffer_dma_capable(dma_mask, addr, size)) {
151 __free_pages(page, get_order(size));
152
Yang Hongyang284901a2009-04-06 19:01:15 -0700153 if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
FUJITA Tomonori9f6ac572008-09-24 20:48:35 +0900154 flag = (flag & ~GFP_DMA32) | GFP_DMA;
155 goto again;
156 }
157
158 return NULL;
159 }
160
161 *dma_addr = addr;
162 return page_address(page);
163}
164
Glauber Costafae9a0d2008-04-08 13:20:56 -0300165/*
166 * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter
167 * documentation.
168 */
169static __init int iommu_setup(char *p)
170{
171 iommu_merge = 1;
172
173 if (!p)
174 return -EINVAL;
175
176 while (*p) {
177 if (!strncmp(p, "off", 3))
178 no_iommu = 1;
179 /* gart_parse_options has more force support */
180 if (!strncmp(p, "force", 5))
181 force_iommu = 1;
182 if (!strncmp(p, "noforce", 7)) {
183 iommu_merge = 0;
184 force_iommu = 0;
185 }
186
187 if (!strncmp(p, "biomerge", 8)) {
Glauber Costafae9a0d2008-04-08 13:20:56 -0300188 iommu_merge = 1;
189 force_iommu = 1;
190 }
191 if (!strncmp(p, "panic", 5))
192 panic_on_overflow = 1;
193 if (!strncmp(p, "nopanic", 7))
194 panic_on_overflow = 0;
195 if (!strncmp(p, "merge", 5)) {
196 iommu_merge = 1;
197 force_iommu = 1;
198 }
199 if (!strncmp(p, "nomerge", 7))
200 iommu_merge = 0;
201 if (!strncmp(p, "forcesac", 8))
202 iommu_sac_force = 1;
203 if (!strncmp(p, "allowdac", 8))
204 forbid_dac = 0;
205 if (!strncmp(p, "nodac", 5))
206 forbid_dac = -1;
207 if (!strncmp(p, "usedac", 6)) {
208 forbid_dac = -1;
209 return 1;
210 }
211#ifdef CONFIG_SWIOTLB
212 if (!strncmp(p, "soft", 4))
213 swiotlb = 1;
David Woodhouse3238c0c2009-07-01 18:56:16 +0100214#endif
Alex Williamson80286872009-07-30 16:15:18 -0600215 if (!strncmp(p, "pt", 2))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700216 iommu_pass_through = 1;
Glauber Costafae9a0d2008-04-08 13:20:56 -0300217
Glauber Costafae9a0d2008-04-08 13:20:56 -0300218 gart_parse_options(p);
Glauber Costafae9a0d2008-04-08 13:20:56 -0300219
220#ifdef CONFIG_CALGARY_IOMMU
221 if (!strncmp(p, "calgary", 7))
222 use_calgary = 1;
223#endif /* CONFIG_CALGARY_IOMMU */
224
225 p += strcspn(p, ",");
226 if (*p == ',')
227 ++p;
228 }
229 return 0;
230}
231early_param("iommu", iommu_setup);
232
Glauber Costa8e0c3792008-04-08 13:20:55 -0300233int dma_supported(struct device *dev, u64 mask)
234{
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900235 struct dma_map_ops *ops = get_dma_ops(dev);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700236
Glauber Costa8e0c3792008-04-08 13:20:55 -0300237#ifdef CONFIG_PCI
238 if (mask > 0xffffffff && forbid_dac > 0) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200239 dev_info(dev, "PCI: Disallowing DAC for device\n");
Glauber Costa8e0c3792008-04-08 13:20:55 -0300240 return 0;
241 }
242#endif
243
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700244 if (ops->dma_supported)
245 return ops->dma_supported(dev, mask);
Glauber Costa8e0c3792008-04-08 13:20:55 -0300246
247 /* Copied from i386. Doesn't make much sense, because it will
248 only work for pci_alloc_coherent.
249 The caller just has to use GFP_DMA in this case. */
Yang Hongyang2f4f27d2009-04-06 19:01:18 -0700250 if (mask < DMA_BIT_MASK(24))
Glauber Costa8e0c3792008-04-08 13:20:55 -0300251 return 0;
252
253 /* Tell the device to use SAC when IOMMU force is on. This
254 allows the driver to use cheaper accesses in some cases.
255
256 Problem with this is that if we overflow the IOMMU area and
257 return DAC as fallback address the device may not handle it
258 correctly.
259
260 As a special case some controllers have a 39bit address
261 mode that is as efficient as 32bit (aic79xx). Don't force
262 SAC for these. Assume all masks <= 40 bits are of this
263 type. Normally this doesn't make any difference, but gives
264 more gentle handling of IOMMU overflow. */
Yang Hongyang50cf1562009-04-06 19:01:14 -0700265 if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200266 dev_info(dev, "Force SAC with mask %Lx\n", mask);
Glauber Costa8e0c3792008-04-08 13:20:55 -0300267 return 0;
268 }
269
270 return 1;
271}
272EXPORT_SYMBOL(dma_supported);
273
Glauber Costacb5867a2008-04-08 13:20:51 -0300274static int __init pci_iommu_init(void)
275{
Joerg Roedel2118d0c2009-01-09 15:13:15 +0100276 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
277
Joerg Roedel86f31952009-03-16 17:50:28 +0100278#ifdef CONFIG_PCI
279 dma_debug_add_bus(&pci_bus_type);
280#endif
281
Glauber Costacb5867a2008-04-08 13:20:51 -0300282 calgary_iommu_init();
Glauber Costa459121c92008-04-08 13:20:43 -0300283
Glauber Costacb5867a2008-04-08 13:20:51 -0300284 intel_iommu_init();
285
Joerg Roedela69ca342008-06-26 21:28:08 +0200286 amd_iommu_init();
287
Glauber Costacb5867a2008-04-08 13:20:51 -0300288 gart_iommu_init();
Glauber Costacb5867a2008-04-08 13:20:51 -0300289
290 no_iommu_init();
291 return 0;
292}
293
294void pci_iommu_shutdown(void)
295{
296 gart_iommu_shutdown();
Joerg Roedel09759042009-06-09 17:52:27 +0200297
298 amd_iommu_shutdown();
Glauber Costacb5867a2008-04-08 13:20:51 -0300299}
300/* Must execute after PCI subsystem */
301fs_initcall(pci_iommu_init);
Fenghua Yu3b15e582008-10-23 16:51:00 -0700302
303#ifdef CONFIG_PCI
304/* Many VIA bridges seem to corrupt data for DAC. Disable it here */
305
306static __devinit void via_no_dac(struct pci_dev *dev)
307{
308 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) {
Bjorn Helgaas13bf7572009-02-24 10:38:22 -0700309 dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
Fenghua Yu3b15e582008-10-23 16:51:00 -0700310 forbid_dac = 1;
311 }
312}
313DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac);
314#endif