blob: 7edd1a40fab38661cdc7aa38c828ff2d8f4014e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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 <asm/io.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010011#include <asm/proto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010013int iommu_merge __read_mostly = 0;
14EXPORT_SYMBOL(iommu_merge);
15
16dma_addr_t bad_dma_address __read_mostly;
17EXPORT_SYMBOL(bad_dma_address);
18
19/* This tells the BIO block layer to assume merging. Default to off
20 because we cannot guarantee merging later. */
21int iommu_bio_merge __read_mostly = 0;
22EXPORT_SYMBOL(iommu_bio_merge);
23
24int iommu_sac_force __read_mostly = 0;
25EXPORT_SYMBOL(iommu_sac_force);
26
27int no_iommu __read_mostly;
28#ifdef CONFIG_IOMMU_DEBUG
29int panic_on_overflow __read_mostly = 1;
30int force_iommu __read_mostly = 1;
31#else
32int panic_on_overflow __read_mostly = 0;
33int force_iommu __read_mostly= 0;
34#endif
35
Jon Mason8d4f6b92006-06-26 13:58:05 +020036/* Set this to 1 if there is a HW IOMMU in the system */
37int iommu_detected __read_mostly = 0;
38
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010039/* Dummy device used for NULL arguments (normally ISA). Better would
40 be probably a smaller DMA mask, but this is bug-to-bug compatible
41 to i386. */
42struct device fallback_dev = {
43 .bus_id = "fallback device",
Jon Mason9f2036f2006-06-26 13:56:19 +020044 .coherent_dma_mask = DMA_32BIT_MASK,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010045 .dma_mask = &fallback_dev.coherent_dma_mask,
46};
47
48/* Allocate DMA memory on node near device */
49noinline static void *
50dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010052 struct page *page;
53 int node;
Andi Kleenfa47dd02006-04-07 19:49:33 +020054#ifdef CONFIG_PCI
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010055 if (dev->bus == &pci_bus_type)
56 node = pcibus_to_node(to_pci_dev(dev)->bus);
57 else
Andi Kleenfa47dd02006-04-07 19:49:33 +020058#endif
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010059 node = numa_node_id();
Daniel Yeisley0d015322006-05-30 22:47:57 +020060
61 if (node < first_node(node_online_map))
62 node = first_node(node_online_map);
63
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010064 page = alloc_pages_node(node, gfp, order);
65 return page ? page_address(page) : NULL;
66}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010068/*
69 * Allocate memory for a coherent mapping.
70 */
71void *
72dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
73 gfp_t gfp)
74{
75 void *memory;
76 unsigned long dma_mask = 0;
77 u64 bus;
78
79 if (!dev)
80 dev = &fallback_dev;
81 dma_mask = dev->coherent_dma_mask;
82 if (dma_mask == 0)
Jon Mason9f2036f2006-06-26 13:56:19 +020083 dma_mask = DMA_32BIT_MASK;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010084
Andi Kleen3056d6b2006-03-25 16:30:43 +010085 /* Don't invoke OOM killer */
86 gfp |= __GFP_NORETRY;
87
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010088 /* Kludge to make it bug-to-bug compatible with i386. i386
89 uses the normal dma_mask for alloc_coherent. */
90 dma_mask &= *dev->dma_mask;
91
92 /* Why <=? Even when the mask is smaller than 4GB it is often
93 larger than 16MB and in this case we have a chance of
94 finding fitting memory in the next higher zone first. If
95 not retry with true GFP_DMA. -AK */
Jon Mason9f2036f2006-06-26 13:56:19 +020096 if (dma_mask <= DMA_32BIT_MASK)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010097 gfp |= GFP_DMA32;
98
99 again:
100 memory = dma_alloc_pages(dev, gfp, get_order(size));
101 if (memory == NULL)
102 return NULL;
103
104 {
105 int high, mmu;
106 bus = virt_to_bus(memory);
107 high = (bus + size) >= dma_mask;
108 mmu = high;
109 if (force_iommu && !(gfp & GFP_DMA))
110 mmu = 1;
111 else if (high) {
112 free_pages((unsigned long)memory,
113 get_order(size));
114
115 /* Don't use the 16MB ZONE_DMA unless absolutely
116 needed. It's better to use remapping first. */
Jon Mason9f2036f2006-06-26 13:56:19 +0200117 if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100118 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
119 goto again;
120 }
121
Andi Kleen6bca52b2006-02-03 21:50:59 +0100122 /* Let low level make its own zone decisions */
123 gfp &= ~(GFP_DMA32|GFP_DMA);
124
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100125 if (dma_ops->alloc_coherent)
126 return dma_ops->alloc_coherent(dev, size,
127 dma_handle, gfp);
128 return NULL;
129 }
130
131 memset(memory, 0, size);
132 if (!mmu) {
133 *dma_handle = virt_to_bus(memory);
134 return memory;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100137
138 if (dma_ops->alloc_coherent) {
139 free_pages((unsigned long)memory, get_order(size));
140 gfp &= ~(GFP_DMA|GFP_DMA32);
141 return dma_ops->alloc_coherent(dev, size, dma_handle, gfp);
142 }
143
144 if (dma_ops->map_simple) {
145 *dma_handle = dma_ops->map_simple(dev, memory,
146 size,
147 PCI_DMA_BIDIRECTIONAL);
148 if (*dma_handle != bad_dma_address)
149 return memory;
150 }
151
152 if (panic_on_overflow)
153 panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size);
154 free_pages((unsigned long)memory, get_order(size));
155 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100157EXPORT_SYMBOL(dma_alloc_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100159/*
160 * Unmap coherent memory.
161 * The caller must ensure that the device has finished accessing the mapping.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100163void dma_free_coherent(struct device *dev, size_t size,
164 void *vaddr, dma_addr_t bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100166 if (dma_ops->unmap_single)
167 dma_ops->unmap_single(dev, bus, size, 0);
168 free_pages((unsigned long)vaddr, get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100170EXPORT_SYMBOL(dma_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100172int dma_supported(struct device *dev, u64 mask)
173{
174 if (dma_ops->dma_supported)
175 return dma_ops->dma_supported(dev, mask);
176
177 /* Copied from i386. Doesn't make much sense, because it will
178 only work for pci_alloc_coherent.
179 The caller just has to use GFP_DMA in this case. */
Jon Mason9f2036f2006-06-26 13:56:19 +0200180 if (mask < DMA_24BIT_MASK)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100181 return 0;
182
183 /* Tell the device to use SAC when IOMMU force is on. This
184 allows the driver to use cheaper accesses in some cases.
185
186 Problem with this is that if we overflow the IOMMU area and
187 return DAC as fallback address the device may not handle it
188 correctly.
189
190 As a special case some controllers have a 39bit address
191 mode that is as efficient as 32bit (aic79xx). Don't force
192 SAC for these. Assume all masks <= 40 bits are of this
193 type. Normally this doesn't make any difference, but gives
194 more gentle handling of IOMMU overflow. */
Jon Mason9f2036f2006-06-26 13:56:19 +0200195 if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) {
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100196 printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask);
197 return 0;
198 }
199
200 return 1;
201}
202EXPORT_SYMBOL(dma_supported);
203
204int dma_set_mask(struct device *dev, u64 mask)
205{
206 if (!dev->dma_mask || !dma_supported(dev, mask))
207 return -EIO;
208 *dev->dma_mask = mask;
209 return 0;
210}
211EXPORT_SYMBOL(dma_set_mask);
212
213/* iommu=[size][,noagp][,off][,force][,noforce][,leak][,memaper[=order]][,merge]
214 [,forcesac][,fullflush][,nomerge][,biomerge]
215 size set size of iommu (in bytes)
216 noagp don't initialize the AGP driver and use full aperture.
217 off don't use the IOMMU
218 leak turn on simple iommu leak tracing (only when CONFIG_IOMMU_LEAK is on)
219 memaper[=order] allocate an own aperture over RAM with size 32MB^order.
220 noforce don't force IOMMU usage. Default.
221 force Force IOMMU.
222 merge Do lazy merging. This may improve performance on some block devices.
223 Implies force (experimental)
224 biomerge Do merging at the BIO layer. This is more efficient than merge,
225 but should be only done with very big IOMMUs. Implies merge,force.
226 nomerge Don't do SG merging.
227 forcesac For SAC mode for masks <40bits (experimental)
228 fullflush Flush IOMMU on each allocation (default)
229 nofullflush Don't use IOMMU fullflush
230 allowed overwrite iommu off workarounds for specific chipsets.
231 soft Use software bounce buffering (default for Intel machines)
232 noaperture Don't touch the aperture for AGP.
233*/
234__init int iommu_setup(char *p)
235{
236 iommu_merge = 1;
237
238 while (*p) {
239 if (!strncmp(p,"off",3))
240 no_iommu = 1;
241 /* gart_parse_options has more force support */
242 if (!strncmp(p,"force",5))
243 force_iommu = 1;
244 if (!strncmp(p,"noforce",7)) {
245 iommu_merge = 0;
246 force_iommu = 0;
247 }
248
249 if (!strncmp(p, "biomerge",8)) {
250 iommu_bio_merge = 4096;
251 iommu_merge = 1;
252 force_iommu = 1;
253 }
254 if (!strncmp(p, "panic",5))
255 panic_on_overflow = 1;
256 if (!strncmp(p, "nopanic",7))
257 panic_on_overflow = 0;
258 if (!strncmp(p, "merge",5)) {
259 iommu_merge = 1;
260 force_iommu = 1;
261 }
262 if (!strncmp(p, "nomerge",7))
263 iommu_merge = 0;
264 if (!strncmp(p, "forcesac",8))
265 iommu_sac_force = 1;
266
267#ifdef CONFIG_SWIOTLB
268 if (!strncmp(p, "soft",4))
269 swiotlb = 1;
270#endif
271
Andi Kleena813ce42006-06-26 13:57:22 +0200272#ifdef CONFIG_IOMMU
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100273 gart_parse_options(p);
274#endif
275
276 p += strcspn(p, ",");
277 if (*p == ',')
278 ++p;
279 }
280 return 1;
281}