blob: bc684d1fd42692293f8921c6f88566bfee8367f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dynamic DMA mapping support.
3 *
John W. Linville569c8bf2005-09-29 14:45:24 -07004 * This implementation is for IA-64 and EM64T platforms that do not support
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * I/O TLBs (aka DMA address translation hardware).
6 * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
7 * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
8 * Copyright (C) 2000, 2003 Hewlett-Packard Co
9 * David Mosberger-Tang <davidm@hpl.hp.com>
10 *
11 * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API.
12 * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
13 * unnecessary i-cache flushing.
John W. Linville569c8bf2005-09-29 14:45:24 -070014 * 04/07/.. ak Better overflow handling. Assorted fixes.
15 * 05/09/10 linville Add support for syncing ranges, support syncing for
16 * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/cache.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070020#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/spinlock.h>
24#include <linux/string.h>
25#include <linux/types.h>
26#include <linux/ctype.h>
27
28#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/dma.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070030#include <asm/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/init.h>
33#include <linux/bootmem.h>
34
35#define OFFSET(val,align) ((unsigned long) \
36 ( (val) & ( (align) - 1)))
37
38#define SG_ENT_VIRT_ADDRESS(sg) (page_address((sg)->page) + (sg)->offset)
Jan Beulich93fbff62007-02-05 18:49:45 -080039#define SG_ENT_PHYS_ADDRESS(sg) virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Maximum allowable number of contiguous slabs to map,
43 * must be a power of 2. What is the appropriate value ?
44 * The complexity of {map,unmap}_single is linearly dependent on this value.
45 */
46#define IO_TLB_SEGSIZE 128
47
48/*
49 * log of the size of each IO TLB slab. The number of slabs is command line
50 * controllable.
51 */
52#define IO_TLB_SHIFT 11
53
Alex Williamson0b9afed2005-09-06 11:20:49 -060054#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
55
56/*
57 * Minimum IO TLB size to bother booting with. Systems with mainly
58 * 64bit capable cards will only lightly use the swiotlb. If we can't
59 * allocate a contiguous 1MB, we're probably in trouble anyway.
60 */
61#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
62
John W. Linvillede69e0f2005-09-29 14:44:57 -070063/*
64 * Enumeration for sync targets
65 */
66enum dma_sync_target {
67 SYNC_FOR_CPU = 0,
68 SYNC_FOR_DEVICE = 1,
69};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071int swiotlb_force;
72
73/*
74 * Used to do a quick range check in swiotlb_unmap_single and
75 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
76 * API.
77 */
78static char *io_tlb_start, *io_tlb_end;
79
80/*
81 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
82 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
83 */
84static unsigned long io_tlb_nslabs;
85
86/*
87 * When the IOMMU overflows we return a fallback buffer. This sets the size.
88 */
89static unsigned long io_tlb_overflow = 32*1024;
90
91void *io_tlb_overflow_buffer;
92
93/*
94 * This is a free list describing the number of free entries available from
95 * each index
96 */
97static unsigned int *io_tlb_list;
98static unsigned int io_tlb_index;
99
100/*
101 * We need to save away the original address corresponding to a mapped entry
102 * for the sync operations.
103 */
104static unsigned char **io_tlb_orig_addr;
105
106/*
107 * Protect the above data structures in the map and unmap calls
108 */
109static DEFINE_SPINLOCK(io_tlb_lock);
110
111static int __init
112setup_io_tlb_npages(char *str)
113{
114 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700115 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* avoid tail segment of size < IO_TLB_SEGSIZE */
117 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
118 }
119 if (*str == ',')
120 ++str;
121 if (!strcmp(str, "force"))
122 swiotlb_force = 1;
123 return 1;
124}
125__setup("swiotlb=", setup_io_tlb_npages);
126/* make io_tlb_overflow tunable too? */
127
128/*
129 * Statically reserve bounce buffer space and initialize bounce buffer data
Tony Luck17e5ad62005-09-29 15:52:13 -0700130 * structures for the software IO TLB used to implement the DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
132void
133swiotlb_init_with_default_size (size_t default_size)
134{
135 unsigned long i;
136
137 if (!io_tlb_nslabs) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700138 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
140 }
141
142 /*
143 * Get IO TLB memory from the low pages
144 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800145 io_tlb_start = alloc_bootmem_low_pages(io_tlb_nslabs * (1 << IO_TLB_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!io_tlb_start)
147 panic("Cannot allocate SWIOTLB buffer");
148 io_tlb_end = io_tlb_start + io_tlb_nslabs * (1 << IO_TLB_SHIFT);
149
150 /*
151 * Allocate and initialize the free list array. This array is used
152 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
153 * between io_tlb_start and io_tlb_end.
154 */
155 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
156 for (i = 0; i < io_tlb_nslabs; i++)
157 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
158 io_tlb_index = 0;
159 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
160
161 /*
162 * Get the overflow emergency buffer
163 */
164 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
165 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
Jan Beulich93fbff62007-02-05 18:49:45 -0800166 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169void
170swiotlb_init (void)
171{
172 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
173}
174
Alex Williamson0b9afed2005-09-06 11:20:49 -0600175/*
176 * Systems with larger DMA zones (those that don't support ISA) can
177 * initialize the swiotlb later using the slab allocator if needed.
178 * This should be just like above, but with some error catching.
179 */
180int
181swiotlb_late_init_with_default_size (size_t default_size)
182{
183 unsigned long i, req_nslabs = io_tlb_nslabs;
184 unsigned int order;
185
186 if (!io_tlb_nslabs) {
187 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
188 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
189 }
190
191 /*
192 * Get IO TLB memory from the low pages
193 */
194 order = get_order(io_tlb_nslabs * (1 << IO_TLB_SHIFT));
195 io_tlb_nslabs = SLABS_PER_PAGE << order;
196
197 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
198 io_tlb_start = (char *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
199 order);
200 if (io_tlb_start)
201 break;
202 order--;
203 }
204
205 if (!io_tlb_start)
206 goto cleanup1;
207
208 if (order != get_order(io_tlb_nslabs * (1 << IO_TLB_SHIFT))) {
209 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
210 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
211 io_tlb_nslabs = SLABS_PER_PAGE << order;
212 }
213 io_tlb_end = io_tlb_start + io_tlb_nslabs * (1 << IO_TLB_SHIFT);
214 memset(io_tlb_start, 0, io_tlb_nslabs * (1 << IO_TLB_SHIFT));
215
216 /*
217 * Allocate and initialize the free list array. This array is used
218 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
219 * between io_tlb_start and io_tlb_end.
220 */
221 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
222 get_order(io_tlb_nslabs * sizeof(int)));
223 if (!io_tlb_list)
224 goto cleanup2;
225
226 for (i = 0; i < io_tlb_nslabs; i++)
227 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
228 io_tlb_index = 0;
229
230 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
231 get_order(io_tlb_nslabs * sizeof(char *)));
232 if (!io_tlb_orig_addr)
233 goto cleanup3;
234
235 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
236
237 /*
238 * Get the overflow emergency buffer
239 */
240 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
241 get_order(io_tlb_overflow));
242 if (!io_tlb_overflow_buffer)
243 goto cleanup4;
244
245 printk(KERN_INFO "Placing %ldMB software IO TLB between 0x%lx - "
246 "0x%lx\n", (io_tlb_nslabs * (1 << IO_TLB_SHIFT)) >> 20,
Jan Beulich93fbff62007-02-05 18:49:45 -0800247 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600248
249 return 0;
250
251cleanup4:
252 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
253 sizeof(char *)));
254 io_tlb_orig_addr = NULL;
255cleanup3:
256 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
257 sizeof(int)));
258 io_tlb_list = NULL;
259 io_tlb_end = NULL;
260cleanup2:
261 free_pages((unsigned long)io_tlb_start, order);
262 io_tlb_start = NULL;
263cleanup1:
264 io_tlb_nslabs = req_nslabs;
265 return -ENOMEM;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static inline int
269address_needs_mapping(struct device *hwdev, dma_addr_t addr)
270{
271 dma_addr_t mask = 0xffffffff;
272 /* If the device has a mask, use it, otherwise default to 32 bits */
273 if (hwdev && hwdev->dma_mask)
274 mask = *hwdev->dma_mask;
275 return (addr & ~mask) != 0;
276}
277
278/*
279 * Allocates bounce buffer and returns its kernel virtual address.
280 */
281static void *
282map_single(struct device *hwdev, char *buffer, size_t size, int dir)
283{
284 unsigned long flags;
285 char *dma_addr;
286 unsigned int nslots, stride, index, wrap;
287 int i;
288
289 /*
290 * For mappings greater than a page, we limit the stride (and
291 * hence alignment) to a page size.
292 */
293 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
294 if (size > PAGE_SIZE)
295 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
296 else
297 stride = 1;
298
Eric Sesterhenn34814542006-03-24 18:47:11 +0100299 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /*
302 * Find suitable number of IO TLB entries size that will fit this
303 * request and allocate a buffer from that IO TLB pool.
304 */
305 spin_lock_irqsave(&io_tlb_lock, flags);
306 {
307 wrap = index = ALIGN(io_tlb_index, stride);
308
309 if (index >= io_tlb_nslabs)
310 wrap = index = 0;
311
312 do {
313 /*
314 * If we find a slot that indicates we have 'nslots'
315 * number of contiguous buffers, we allocate the
316 * buffers from that slot and mark the entries as '0'
317 * indicating unavailable.
318 */
319 if (io_tlb_list[index] >= nslots) {
320 int count = 0;
321
322 for (i = index; i < (int) (index + nslots); i++)
323 io_tlb_list[i] = 0;
324 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
325 io_tlb_list[i] = ++count;
326 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
327
328 /*
329 * Update the indices to avoid searching in
330 * the next round.
331 */
332 io_tlb_index = ((index + nslots) < io_tlb_nslabs
333 ? (index + nslots) : 0);
334
335 goto found;
336 }
337 index += stride;
338 if (index >= io_tlb_nslabs)
339 index = 0;
340 } while (index != wrap);
341
342 spin_unlock_irqrestore(&io_tlb_lock, flags);
343 return NULL;
344 }
345 found:
346 spin_unlock_irqrestore(&io_tlb_lock, flags);
347
348 /*
349 * Save away the mapping from the original address to the DMA address.
350 * This is needed when we sync the memory. Then we sync the buffer if
351 * needed.
352 */
353 io_tlb_orig_addr[index] = buffer;
354 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
355 memcpy(dma_addr, buffer, size);
356
357 return dma_addr;
358}
359
360/*
361 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
362 */
363static void
364unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
365{
366 unsigned long flags;
367 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
368 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
369 char *buffer = io_tlb_orig_addr[index];
370
371 /*
372 * First, sync the memory before unmapping the entry
373 */
374 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
375 /*
376 * bounce... copy the data back into the original buffer * and
377 * delete the bounce buffer.
378 */
379 memcpy(buffer, dma_addr, size);
380
381 /*
382 * Return the buffer to the free list by setting the corresponding
383 * entries to indicate the number of contigous entries available.
384 * While returning the entries to the free list, we merge the entries
385 * with slots below and above the pool being returned.
386 */
387 spin_lock_irqsave(&io_tlb_lock, flags);
388 {
389 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
390 io_tlb_list[index + nslots] : 0);
391 /*
392 * Step 1: return the slots to the free list, merging the
393 * slots with superceeding slots
394 */
395 for (i = index + nslots - 1; i >= index; i--)
396 io_tlb_list[i] = ++count;
397 /*
398 * Step 2: merge the returned slots with the preceding slots,
399 * if available (non zero)
400 */
401 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
402 io_tlb_list[i] = ++count;
403 }
404 spin_unlock_irqrestore(&io_tlb_lock, flags);
405}
406
407static void
John W. Linvillede69e0f2005-09-29 14:44:57 -0700408sync_single(struct device *hwdev, char *dma_addr, size_t size,
409 int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
412 char *buffer = io_tlb_orig_addr[index];
413
John W. Linvillede69e0f2005-09-29 14:44:57 -0700414 switch (target) {
415 case SYNC_FOR_CPU:
416 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
417 memcpy(buffer, dma_addr, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100418 else
419 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700420 break;
421 case SYNC_FOR_DEVICE:
422 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
423 memcpy(dma_addr, buffer, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100424 else
425 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700426 break;
427 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432void *
433swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Al Viro06a54492005-10-21 03:21:03 -0400434 dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 unsigned long dev_addr;
437 void *ret;
438 int order = get_order(size);
439
440 /*
441 * XXX fix me: the DMA API should pass us an explicit DMA mask
442 * instead, or use ZONE_DMA32 (ia64 overloads ZONE_DMA to be a ~32
443 * bit range instead of a 16MB one).
444 */
445 flags |= GFP_DMA;
446
447 ret = (void *)__get_free_pages(flags, order);
Jan Beulich93fbff62007-02-05 18:49:45 -0800448 if (ret && address_needs_mapping(hwdev, virt_to_bus(ret))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * The allocated memory isn't reachable by the device.
451 * Fall back on swiotlb_map_single().
452 */
453 free_pages((unsigned long) ret, order);
454 ret = NULL;
455 }
456 if (!ret) {
457 /*
458 * We are either out of memory or the device can't DMA
459 * to GFP_DMA memory; fall back on
460 * swiotlb_map_single(), which will grab memory from
461 * the lowest available address range.
462 */
463 dma_addr_t handle;
464 handle = swiotlb_map_single(NULL, NULL, size, DMA_FROM_DEVICE);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100465 if (swiotlb_dma_mapping_error(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return NULL;
467
Jan Beulich93fbff62007-02-05 18:49:45 -0800468 ret = bus_to_virt(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
471 memset(ret, 0, size);
Jan Beulich93fbff62007-02-05 18:49:45 -0800472 dev_addr = virt_to_bus(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* Confirm address can be DMA'd by device */
475 if (address_needs_mapping(hwdev, dev_addr)) {
476 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016lx\n",
477 (unsigned long long)*hwdev->dma_mask, dev_addr);
478 panic("swiotlb_alloc_coherent: allocated memory is out of "
479 "range for device");
480 }
481 *dma_handle = dev_addr;
482 return ret;
483}
484
485void
486swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
487 dma_addr_t dma_handle)
488{
489 if (!(vaddr >= (void *)io_tlb_start
490 && vaddr < (void *)io_tlb_end))
491 free_pages((unsigned long) vaddr, get_order(size));
492 else
493 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
494 swiotlb_unmap_single (hwdev, dma_handle, size, DMA_TO_DEVICE);
495}
496
497static void
498swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
499{
500 /*
501 * Ran out of IOMMU space for this operation. This is very bad.
502 * Unfortunately the drivers cannot handle this operation properly.
Tony Luck17e5ad62005-09-29 15:52:13 -0700503 * unless they check for dma_mapping_error (most don't)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * When the mapping is small enough return a static buffer to limit
505 * the damage, or panic when the transfer is too big.
506 */
Tony Luck17e5ad62005-09-29 15:52:13 -0700507 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %lu bytes at "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 "device %s\n", size, dev ? dev->bus_id : "?");
509
510 if (size > io_tlb_overflow && do_panic) {
Tony Luck17e5ad62005-09-29 15:52:13 -0700511 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
512 panic("DMA: Memory would be corrupted\n");
513 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
514 panic("DMA: Random memory would be DMAed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516}
517
518/*
519 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700520 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *
522 * Once the device is given the dma address, the device owns this memory until
523 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
524 */
525dma_addr_t
526swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
527{
Jan Beulich93fbff62007-02-05 18:49:45 -0800528 unsigned long dev_addr = virt_to_bus(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 void *map;
530
Eric Sesterhenn34814542006-03-24 18:47:11 +0100531 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
533 * If the pointer passed in happens to be in the device's DMA window,
534 * we can safely return the device addr and not worry about bounce
535 * buffering it.
536 */
537 if (!address_needs_mapping(hwdev, dev_addr) && !swiotlb_force)
538 return dev_addr;
539
540 /*
541 * Oh well, have to allocate and map a bounce buffer.
542 */
543 map = map_single(hwdev, ptr, size, dir);
544 if (!map) {
545 swiotlb_full(hwdev, size, dir, 1);
546 map = io_tlb_overflow_buffer;
547 }
548
Jan Beulich93fbff62007-02-05 18:49:45 -0800549 dev_addr = virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * Ensure that the address returned is DMA'ble
553 */
554 if (address_needs_mapping(hwdev, dev_addr))
555 panic("map_single: bounce buffer is not DMA'ble");
556
557 return dev_addr;
558}
559
560/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * Unmap a single streaming mode DMA translation. The dma_addr and size must
562 * match what was provided for in a previous swiotlb_map_single call. All
563 * other usages are undefined.
564 *
565 * After this call, reads by the cpu to the buffer are guaranteed to see
566 * whatever the device wrote there.
567 */
568void
569swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
570 int dir)
571{
Jan Beulich93fbff62007-02-05 18:49:45 -0800572 char *dma_addr = bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Eric Sesterhenn34814542006-03-24 18:47:11 +0100574 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
576 unmap_single(hwdev, dma_addr, size, dir);
577 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800578 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
582 * Make physical memory consistent for a single streaming mode DMA translation
583 * after a transfer.
584 *
585 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700586 * using the cpu, yet do not wish to teardown the dma mapping, you must
587 * call this function before doing so. At the next point you give the dma
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * address back to the card, you must first perform a
589 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
590 */
John W. Linville8270f3f2005-09-29 14:43:32 -0700591static inline void
592swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700593 size_t size, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Jan Beulich93fbff62007-02-05 18:49:45 -0800595 char *dma_addr = bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Eric Sesterhenn34814542006-03-24 18:47:11 +0100597 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
John W. Linvillede69e0f2005-09-29 14:44:57 -0700599 sync_single(hwdev, dma_addr, size, dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800601 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604void
John W. Linville8270f3f2005-09-29 14:43:32 -0700605swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
606 size_t size, int dir)
607{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700608 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700609}
610
611void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
613 size_t size, int dir)
614{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700615 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618/*
John W. Linville878a97c2005-09-29 14:44:23 -0700619 * Same as above, but for a sub-range of the mapping.
620 */
621static inline void
622swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700623 unsigned long offset, size_t size,
624 int dir, int target)
John W. Linville878a97c2005-09-29 14:44:23 -0700625{
Jan Beulich93fbff62007-02-05 18:49:45 -0800626 char *dma_addr = bus_to_virt(dev_addr) + offset;
John W. Linville878a97c2005-09-29 14:44:23 -0700627
Eric Sesterhenn34814542006-03-24 18:47:11 +0100628 BUG_ON(dir == DMA_NONE);
John W. Linville878a97c2005-09-29 14:44:23 -0700629 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
John W. Linvillede69e0f2005-09-29 14:44:57 -0700630 sync_single(hwdev, dma_addr, size, dir, target);
John W. Linville878a97c2005-09-29 14:44:23 -0700631 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800632 dma_mark_clean(dma_addr, size);
John W. Linville878a97c2005-09-29 14:44:23 -0700633}
634
635void
636swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
637 unsigned long offset, size_t size, int dir)
638{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700639 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
640 SYNC_FOR_CPU);
John W. Linville878a97c2005-09-29 14:44:23 -0700641}
642
643void
644swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
645 unsigned long offset, size_t size, int dir)
646{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700647 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
648 SYNC_FOR_DEVICE);
John W. Linville878a97c2005-09-29 14:44:23 -0700649}
650
651/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * Map a set of buffers described by scatterlist in streaming mode for DMA.
653 * This is the scatter-gather version of the above swiotlb_map_single
654 * interface. Here the scatter gather list elements are each tagged with the
655 * appropriate dma address and length. They are obtained via
656 * sg_dma_{address,length}(SG).
657 *
658 * NOTE: An implementation may be able to use a smaller number of
659 * DMA address/length pairs than there are SG table elements.
660 * (for example via virtual mapping capabilities)
661 * The routine returns the number of addr/length pairs actually
662 * used, at most nents.
663 *
664 * Device ownership issues as mentioned above for swiotlb_map_single are the
665 * same here.
666 */
667int
668swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
669 int dir)
670{
671 void *addr;
672 unsigned long dev_addr;
673 int i;
674
Eric Sesterhenn34814542006-03-24 18:47:11 +0100675 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 for (i = 0; i < nelems; i++, sg++) {
678 addr = SG_ENT_VIRT_ADDRESS(sg);
Jan Beulich93fbff62007-02-05 18:49:45 -0800679 dev_addr = virt_to_bus(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
Andi Kleen7e870232005-12-20 14:45:19 +0100681 void *map = map_single(hwdev, addr, sg->length, dir);
Andi Kleen7e870232005-12-20 14:45:19 +0100682 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* Don't panic here, we expect map_sg users
684 to do proper error handling. */
685 swiotlb_full(hwdev, sg->length, dir, 0);
686 swiotlb_unmap_sg(hwdev, sg - i, i, dir);
687 sg[0].dma_length = 0;
688 return 0;
689 }
Jan Beulichcde14bb2007-02-05 18:46:40 -0800690 sg->dma_address = virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 } else
692 sg->dma_address = dev_addr;
693 sg->dma_length = sg->length;
694 }
695 return nelems;
696}
697
698/*
699 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
700 * concerning calls here are the same as for swiotlb_unmap_single() above.
701 */
702void
703swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
704 int dir)
705{
706 int i;
707
Eric Sesterhenn34814542006-03-24 18:47:11 +0100708 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 for (i = 0; i < nelems; i++, sg++)
711 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Jan Beulich93fbff62007-02-05 18:49:45 -0800712 unmap_single(hwdev, bus_to_virt(sg->dma_address),
713 sg->dma_length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800715 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/*
719 * Make physical memory consistent for a set of streaming mode DMA translations
720 * after a transfer.
721 *
722 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
723 * and usage.
724 */
John W. Linville8270f3f2005-09-29 14:43:32 -0700725static inline void
726swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sg,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700727 int nelems, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 int i;
730
Eric Sesterhenn34814542006-03-24 18:47:11 +0100731 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 for (i = 0; i < nelems; i++, sg++)
734 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Jan Beulich93fbff62007-02-05 18:49:45 -0800735 sync_single(hwdev, bus_to_virt(sg->dma_address),
John W. Linvillede69e0f2005-09-29 14:44:57 -0700736 sg->dma_length, dir, target);
Jan Beulichcde14bb2007-02-05 18:46:40 -0800737 else if (dir == DMA_FROM_DEVICE)
738 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741void
John W. Linville8270f3f2005-09-29 14:43:32 -0700742swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
743 int nelems, int dir)
744{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700745 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700746}
747
748void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
750 int nelems, int dir)
751{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700752 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755int
756swiotlb_dma_mapping_error(dma_addr_t dma_addr)
757{
Jan Beulich93fbff62007-02-05 18:49:45 -0800758 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700762 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700764 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * this function.
766 */
767int
768swiotlb_dma_supported (struct device *hwdev, u64 mask)
769{
Jan Beulich93fbff62007-02-05 18:49:45 -0800770 return virt_to_bus(io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773EXPORT_SYMBOL(swiotlb_init);
774EXPORT_SYMBOL(swiotlb_map_single);
775EXPORT_SYMBOL(swiotlb_unmap_single);
776EXPORT_SYMBOL(swiotlb_map_sg);
777EXPORT_SYMBOL(swiotlb_unmap_sg);
778EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
779EXPORT_SYMBOL(swiotlb_sync_single_for_device);
John W. Linville878a97c2005-09-29 14:44:23 -0700780EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
781EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
783EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
784EXPORT_SYMBOL(swiotlb_dma_mapping_error);
785EXPORT_SYMBOL(swiotlb_alloc_coherent);
786EXPORT_SYMBOL(swiotlb_free_coherent);
787EXPORT_SYMBOL(swiotlb_dma_supported);