]> bbs.cooldavid.org Git - net-next-2.6.git/blame - lib/swiotlb.c
swiotlb: use unmap_single instead of swiotlb_unmap_single in swiotlb_free_coherent
[net-next-2.6.git] / lib / swiotlb.c
CommitLineData
1da177e4
LT
1/*
2 * Dynamic DMA mapping support.
3 *
563aaf06 4 * This implementation is a fallback for platforms that do not support
1da177e4
LT
5 * 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.
569c8bf5
JL
14 * 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.
1da177e4
LT
17 */
18
19#include <linux/cache.h>
17e5ad6c 20#include <linux/dma-mapping.h>
1da177e4
LT
21#include <linux/mm.h>
22#include <linux/module.h>
1da177e4
LT
23#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>
1da177e4 29#include <asm/dma.h>
17e5ad6c 30#include <asm/scatterlist.h>
1da177e4
LT
31
32#include <linux/init.h>
33#include <linux/bootmem.h>
a8522509 34#include <linux/iommu-helper.h>
1da177e4
LT
35
36#define OFFSET(val,align) ((unsigned long) \
37 ( (val) & ( (align) - 1)))
38
f9527f12 39#define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
93fbff63 40#define SG_ENT_PHYS_ADDRESS(sg) virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
1da177e4
LT
41
42/*
43 * Maximum allowable number of contiguous slabs to map,
44 * must be a power of 2. What is the appropriate value ?
45 * The complexity of {map,unmap}_single is linearly dependent on this value.
46 */
47#define IO_TLB_SEGSIZE 128
48
49/*
50 * log of the size of each IO TLB slab. The number of slabs is command line
51 * controllable.
52 */
53#define IO_TLB_SHIFT 11
54
0b9afede
AW
55#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
56
57/*
58 * Minimum IO TLB size to bother booting with. Systems with mainly
59 * 64bit capable cards will only lightly use the swiotlb. If we can't
60 * allocate a contiguous 1MB, we're probably in trouble anyway.
61 */
62#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
63
de69e0f0
JL
64/*
65 * Enumeration for sync targets
66 */
67enum dma_sync_target {
68 SYNC_FOR_CPU = 0,
69 SYNC_FOR_DEVICE = 1,
70};
71
1da177e4
LT
72int swiotlb_force;
73
74/*
75 * Used to do a quick range check in swiotlb_unmap_single and
76 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
77 * API.
78 */
79static char *io_tlb_start, *io_tlb_end;
80
81/*
82 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
83 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
84 */
85static unsigned long io_tlb_nslabs;
86
87/*
88 * When the IOMMU overflows we return a fallback buffer. This sets the size.
89 */
90static unsigned long io_tlb_overflow = 32*1024;
91
92void *io_tlb_overflow_buffer;
93
94/*
95 * This is a free list describing the number of free entries available from
96 * each index
97 */
98static unsigned int *io_tlb_list;
99static unsigned int io_tlb_index;
100
101/*
102 * We need to save away the original address corresponding to a mapped entry
103 * for the sync operations.
104 */
25667d67 105static unsigned char **io_tlb_orig_addr;
1da177e4
LT
106
107/*
108 * Protect the above data structures in the map and unmap calls
109 */
110static DEFINE_SPINLOCK(io_tlb_lock);
111
112static int __init
113setup_io_tlb_npages(char *str)
114{
115 if (isdigit(*str)) {
e8579e72 116 io_tlb_nslabs = simple_strtoul(str, &str, 0);
1da177e4
LT
117 /* avoid tail segment of size < IO_TLB_SEGSIZE */
118 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
119 }
120 if (*str == ',')
121 ++str;
122 if (!strcmp(str, "force"))
123 swiotlb_force = 1;
124 return 1;
125}
126__setup("swiotlb=", setup_io_tlb_npages);
127/* make io_tlb_overflow tunable too? */
128
129/*
130 * Statically reserve bounce buffer space and initialize bounce buffer data
17e5ad6c 131 * structures for the software IO TLB used to implement the DMA API.
1da177e4 132 */
563aaf06
JB
133void __init
134swiotlb_init_with_default_size(size_t default_size)
1da177e4 135{
563aaf06 136 unsigned long i, bytes;
1da177e4
LT
137
138 if (!io_tlb_nslabs) {
e8579e72 139 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
1da177e4
LT
140 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
141 }
142
563aaf06
JB
143 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
144
1da177e4
LT
145 /*
146 * Get IO TLB memory from the low pages
147 */
563aaf06 148 io_tlb_start = alloc_bootmem_low_pages(bytes);
1da177e4
LT
149 if (!io_tlb_start)
150 panic("Cannot allocate SWIOTLB buffer");
563aaf06 151 io_tlb_end = io_tlb_start + bytes;
1da177e4
LT
152
153 /*
154 * Allocate and initialize the free list array. This array is used
155 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
156 * between io_tlb_start and io_tlb_end.
157 */
158 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
25667d67 159 for (i = 0; i < io_tlb_nslabs; i++)
1da177e4
LT
160 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
161 io_tlb_index = 0;
25667d67 162 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
1da177e4
LT
163
164 /*
165 * Get the overflow emergency buffer
166 */
167 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
563aaf06
JB
168 if (!io_tlb_overflow_buffer)
169 panic("Cannot allocate SWIOTLB overflow buffer!\n");
170
25667d67
TL
171 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
172 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
1da177e4
LT
173}
174
563aaf06
JB
175void __init
176swiotlb_init(void)
1da177e4 177{
25667d67 178 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
1da177e4
LT
179}
180
0b9afede
AW
181/*
182 * Systems with larger DMA zones (those that don't support ISA) can
183 * initialize the swiotlb later using the slab allocator if needed.
184 * This should be just like above, but with some error catching.
185 */
186int
563aaf06 187swiotlb_late_init_with_default_size(size_t default_size)
0b9afede 188{
563aaf06 189 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
0b9afede
AW
190 unsigned int order;
191
192 if (!io_tlb_nslabs) {
193 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
194 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
195 }
196
197 /*
198 * Get IO TLB memory from the low pages
199 */
563aaf06 200 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
0b9afede 201 io_tlb_nslabs = SLABS_PER_PAGE << order;
563aaf06 202 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
0b9afede
AW
203
204 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
205 io_tlb_start = (char *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
206 order);
207 if (io_tlb_start)
208 break;
209 order--;
210 }
211
212 if (!io_tlb_start)
213 goto cleanup1;
214
563aaf06 215 if (order != get_order(bytes)) {
0b9afede
AW
216 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
217 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
218 io_tlb_nslabs = SLABS_PER_PAGE << order;
563aaf06 219 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
0b9afede 220 }
563aaf06
JB
221 io_tlb_end = io_tlb_start + bytes;
222 memset(io_tlb_start, 0, bytes);
0b9afede
AW
223
224 /*
225 * Allocate and initialize the free list array. This array is used
226 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
227 * between io_tlb_start and io_tlb_end.
228 */
229 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
230 get_order(io_tlb_nslabs * sizeof(int)));
231 if (!io_tlb_list)
232 goto cleanup2;
233
234 for (i = 0; i < io_tlb_nslabs; i++)
235 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
236 io_tlb_index = 0;
237
25667d67
TL
238 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
239 get_order(io_tlb_nslabs * sizeof(char *)));
0b9afede
AW
240 if (!io_tlb_orig_addr)
241 goto cleanup3;
242
25667d67 243 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
0b9afede
AW
244
245 /*
246 * Get the overflow emergency buffer
247 */
248 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
249 get_order(io_tlb_overflow));
250 if (!io_tlb_overflow_buffer)
251 goto cleanup4;
252
25667d67
TL
253 printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - "
254 "0x%lx\n", bytes >> 20,
255 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
0b9afede
AW
256
257 return 0;
258
259cleanup4:
25667d67
TL
260 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
261 sizeof(char *)));
0b9afede
AW
262 io_tlb_orig_addr = NULL;
263cleanup3:
25667d67
TL
264 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
265 sizeof(int)));
0b9afede 266 io_tlb_list = NULL;
0b9afede 267cleanup2:
563aaf06 268 io_tlb_end = NULL;
0b9afede
AW
269 free_pages((unsigned long)io_tlb_start, order);
270 io_tlb_start = NULL;
271cleanup1:
272 io_tlb_nslabs = req_nslabs;
273 return -ENOMEM;
274}
275
be6b0267 276static int
1da177e4
LT
277address_needs_mapping(struct device *hwdev, dma_addr_t addr)
278{
279 dma_addr_t mask = 0xffffffff;
280 /* If the device has a mask, use it, otherwise default to 32 bits */
281 if (hwdev && hwdev->dma_mask)
282 mask = *hwdev->dma_mask;
283 return (addr & ~mask) != 0;
284}
285
286/*
287 * Allocates bounce buffer and returns its kernel virtual address.
288 */
289static void *
25667d67 290map_single(struct device *hwdev, char *buffer, size_t size, int dir)
1da177e4
LT
291{
292 unsigned long flags;
293 char *dma_addr;
294 unsigned int nslots, stride, index, wrap;
295 int i;
681cc5cd
FT
296 unsigned long start_dma_addr;
297 unsigned long mask;
298 unsigned long offset_slots;
299 unsigned long max_slots;
300
301 mask = dma_get_seg_boundary(hwdev);
302 start_dma_addr = virt_to_bus(io_tlb_start) & mask;
303
304 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
b15a3891
JB
305 max_slots = mask + 1
306 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
307 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
1da177e4
LT
308
309 /*
310 * For mappings greater than a page, we limit the stride (and
311 * hence alignment) to a page size.
312 */
313 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
314 if (size > PAGE_SIZE)
315 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
316 else
317 stride = 1;
318
34814545 319 BUG_ON(!nslots);
1da177e4
LT
320
321 /*
322 * Find suitable number of IO TLB entries size that will fit this
323 * request and allocate a buffer from that IO TLB pool.
324 */
325 spin_lock_irqsave(&io_tlb_lock, flags);
a7133a15
AM
326 index = ALIGN(io_tlb_index, stride);
327 if (index >= io_tlb_nslabs)
328 index = 0;
329 wrap = index;
330
331 do {
a8522509
FT
332 while (iommu_is_span_boundary(index, nslots, offset_slots,
333 max_slots)) {
b15a3891
JB
334 index += stride;
335 if (index >= io_tlb_nslabs)
336 index = 0;
a7133a15
AM
337 if (index == wrap)
338 goto not_found;
339 }
340
341 /*
342 * If we find a slot that indicates we have 'nslots' number of
343 * contiguous buffers, we allocate the buffers from that slot
344 * and mark the entries as '0' indicating unavailable.
345 */
346 if (io_tlb_list[index] >= nslots) {
347 int count = 0;
348
349 for (i = index; i < (int) (index + nslots); i++)
350 io_tlb_list[i] = 0;
351 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
352 io_tlb_list[i] = ++count;
353 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
1da177e4 354
a7133a15
AM
355 /*
356 * Update the indices to avoid searching in the next
357 * round.
358 */
359 io_tlb_index = ((index + nslots) < io_tlb_nslabs
360 ? (index + nslots) : 0);
361
362 goto found;
363 }
364 index += stride;
365 if (index >= io_tlb_nslabs)
366 index = 0;
367 } while (index != wrap);
368
369not_found:
370 spin_unlock_irqrestore(&io_tlb_lock, flags);
371 return NULL;
372found:
1da177e4
LT
373 spin_unlock_irqrestore(&io_tlb_lock, flags);
374
375 /*
376 * Save away the mapping from the original address to the DMA address.
377 * This is needed when we sync the memory. Then we sync the buffer if
378 * needed.
379 */
df336d1c
KF
380 for (i = 0; i < nslots; i++)
381 io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
1da177e4 382 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
25667d67 383 memcpy(dma_addr, buffer, size);
1da177e4
LT
384
385 return dma_addr;
386}
387
388/*
389 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
390 */
391static void
392unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
393{
394 unsigned long flags;
395 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
396 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
25667d67 397 char *buffer = io_tlb_orig_addr[index];
1da177e4
LT
398
399 /*
400 * First, sync the memory before unmapping the entry
401 */
25667d67 402 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
1da177e4
LT
403 /*
404 * bounce... copy the data back into the original buffer * and
405 * delete the bounce buffer.
406 */
25667d67 407 memcpy(buffer, dma_addr, size);
1da177e4
LT
408
409 /*
410 * Return the buffer to the free list by setting the corresponding
411 * entries to indicate the number of contigous entries available.
412 * While returning the entries to the free list, we merge the entries
413 * with slots below and above the pool being returned.
414 */
415 spin_lock_irqsave(&io_tlb_lock, flags);
416 {
417 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
418 io_tlb_list[index + nslots] : 0);
419 /*
420 * Step 1: return the slots to the free list, merging the
421 * slots with superceeding slots
422 */
423 for (i = index + nslots - 1; i >= index; i--)
424 io_tlb_list[i] = ++count;
425 /*
426 * Step 2: merge the returned slots with the preceding slots,
427 * if available (non zero)
428 */
429 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
430 io_tlb_list[i] = ++count;
431 }
432 spin_unlock_irqrestore(&io_tlb_lock, flags);
433}
434
435static void
de69e0f0
JL
436sync_single(struct device *hwdev, char *dma_addr, size_t size,
437 int dir, int target)
1da177e4
LT
438{
439 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
25667d67 440 char *buffer = io_tlb_orig_addr[index];
1da177e4 441
df336d1c
KF
442 buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
443
de69e0f0
JL
444 switch (target) {
445 case SYNC_FOR_CPU:
446 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
25667d67 447 memcpy(buffer, dma_addr, size);
34814545
ES
448 else
449 BUG_ON(dir != DMA_TO_DEVICE);
de69e0f0
JL
450 break;
451 case SYNC_FOR_DEVICE:
452 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
25667d67 453 memcpy(dma_addr, buffer, size);
34814545
ES
454 else
455 BUG_ON(dir != DMA_FROM_DEVICE);
de69e0f0
JL
456 break;
457 default:
1da177e4 458 BUG();
de69e0f0 459 }
1da177e4
LT
460}
461
462void *
463swiotlb_alloc_coherent(struct device *hwdev, size_t size,
06a54497 464 dma_addr_t *dma_handle, gfp_t flags)
1da177e4 465{
563aaf06 466 dma_addr_t dev_addr;
1da177e4
LT
467 void *ret;
468 int order = get_order(size);
469
25667d67 470 ret = (void *)__get_free_pages(flags, order);
93fbff63 471 if (ret && address_needs_mapping(hwdev, virt_to_bus(ret))) {
1da177e4
LT
472 /*
473 * The allocated memory isn't reachable by the device.
474 * Fall back on swiotlb_map_single().
475 */
476 free_pages((unsigned long) ret, order);
477 ret = NULL;
478 }
479 if (!ret) {
480 /*
481 * We are either out of memory or the device can't DMA
482 * to GFP_DMA memory; fall back on
483 * swiotlb_map_single(), which will grab memory from
484 * the lowest available address range.
485 */
9dfda12b
FT
486 ret = map_single(hwdev, NULL, size, DMA_FROM_DEVICE);
487 if (!ret)
1da177e4 488 return NULL;
1da177e4
LT
489 }
490
491 memset(ret, 0, size);
93fbff63 492 dev_addr = virt_to_bus(ret);
1da177e4
LT
493
494 /* Confirm address can be DMA'd by device */
495 if (address_needs_mapping(hwdev, dev_addr)) {
563aaf06
JB
496 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
497 (unsigned long long)*hwdev->dma_mask,
498 (unsigned long long)dev_addr);
1da177e4
LT
499 panic("swiotlb_alloc_coherent: allocated memory is out of "
500 "range for device");
501 }
502 *dma_handle = dev_addr;
503 return ret;
504}
505
506void
507swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
508 dma_addr_t dma_handle)
509{
aa24886e 510 WARN_ON(irqs_disabled());
1da177e4
LT
511 if (!(vaddr >= (void *)io_tlb_start
512 && vaddr < (void *)io_tlb_end))
513 free_pages((unsigned long) vaddr, get_order(size));
514 else
515 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
21f6c4de 516 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
1da177e4
LT
517}
518
519static void
520swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
521{
522 /*
523 * Ran out of IOMMU space for this operation. This is very bad.
524 * Unfortunately the drivers cannot handle this operation properly.
17e5ad6c 525 * unless they check for dma_mapping_error (most don't)
1da177e4
LT
526 * When the mapping is small enough return a static buffer to limit
527 * the damage, or panic when the transfer is too big.
528 */
563aaf06 529 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
1da177e4
LT
530 "device %s\n", size, dev ? dev->bus_id : "?");
531
532 if (size > io_tlb_overflow && do_panic) {
17e5ad6c
TL
533 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
534 panic("DMA: Memory would be corrupted\n");
535 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
536 panic("DMA: Random memory would be DMAed\n");
1da177e4
LT
537 }
538}
539
540/*
541 * Map a single buffer of the indicated size for DMA in streaming mode. The
17e5ad6c 542 * physical address to use is returned.
1da177e4
LT
543 *
544 * Once the device is given the dma address, the device owns this memory until
545 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
546 */
547dma_addr_t
309df0c5
AK
548swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
549 int dir, struct dma_attrs *attrs)
1da177e4 550{
563aaf06 551 dma_addr_t dev_addr = virt_to_bus(ptr);
1da177e4
LT
552 void *map;
553
34814545 554 BUG_ON(dir == DMA_NONE);
1da177e4
LT
555 /*
556 * If the pointer passed in happens to be in the device's DMA window,
557 * we can safely return the device addr and not worry about bounce
558 * buffering it.
559 */
25667d67 560 if (!address_needs_mapping(hwdev, dev_addr) && !swiotlb_force)
1da177e4
LT
561 return dev_addr;
562
563 /*
564 * Oh well, have to allocate and map a bounce buffer.
565 */
25667d67 566 map = map_single(hwdev, ptr, size, dir);
1da177e4
LT
567 if (!map) {
568 swiotlb_full(hwdev, size, dir, 1);
569 map = io_tlb_overflow_buffer;
570 }
571
93fbff63 572 dev_addr = virt_to_bus(map);
1da177e4
LT
573
574 /*
575 * Ensure that the address returned is DMA'ble
576 */
577 if (address_needs_mapping(hwdev, dev_addr))
578 panic("map_single: bounce buffer is not DMA'ble");
579
580 return dev_addr;
581}
309df0c5
AK
582EXPORT_SYMBOL(swiotlb_map_single_attrs);
583
584dma_addr_t
585swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
586{
587 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
588}
1da177e4 589
1da177e4
LT
590/*
591 * Unmap a single streaming mode DMA translation. The dma_addr and size must
592 * match what was provided for in a previous swiotlb_map_single call. All
593 * other usages are undefined.
594 *
595 * After this call, reads by the cpu to the buffer are guaranteed to see
596 * whatever the device wrote there.
597 */
598void
309df0c5
AK
599swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
600 size_t size, int dir, struct dma_attrs *attrs)
1da177e4 601{
93fbff63 602 char *dma_addr = bus_to_virt(dev_addr);
1da177e4 603
34814545 604 BUG_ON(dir == DMA_NONE);
1da177e4
LT
605 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
606 unmap_single(hwdev, dma_addr, size, dir);
607 else if (dir == DMA_FROM_DEVICE)
cde14bbf 608 dma_mark_clean(dma_addr, size);
1da177e4 609}
309df0c5 610EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
1da177e4 611
309df0c5
AK
612void
613swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
614 int dir)
615{
616 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
617}
1da177e4
LT
618/*
619 * Make physical memory consistent for a single streaming mode DMA translation
620 * after a transfer.
621 *
622 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
17e5ad6c
TL
623 * using the cpu, yet do not wish to teardown the dma mapping, you must
624 * call this function before doing so. At the next point you give the dma
1da177e4
LT
625 * address back to the card, you must first perform a
626 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
627 */
be6b0267 628static void
8270f3f1 629swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
de69e0f0 630 size_t size, int dir, int target)
1da177e4 631{
93fbff63 632 char *dma_addr = bus_to_virt(dev_addr);
1da177e4 633
34814545 634 BUG_ON(dir == DMA_NONE);
1da177e4 635 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
de69e0f0 636 sync_single(hwdev, dma_addr, size, dir, target);
1da177e4 637 else if (dir == DMA_FROM_DEVICE)
cde14bbf 638 dma_mark_clean(dma_addr, size);
1da177e4
LT
639}
640
8270f3f1
JL
641void
642swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
643 size_t size, int dir)
644{
de69e0f0 645 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
8270f3f1
JL
646}
647
1da177e4
LT
648void
649swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
650 size_t size, int dir)
651{
de69e0f0 652 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
1da177e4
LT
653}
654
878a97cf
JL
655/*
656 * Same as above, but for a sub-range of the mapping.
657 */
be6b0267 658static void
878a97cf 659swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
de69e0f0
JL
660 unsigned long offset, size_t size,
661 int dir, int target)
878a97cf 662{
93fbff63 663 char *dma_addr = bus_to_virt(dev_addr) + offset;
878a97cf 664
34814545 665 BUG_ON(dir == DMA_NONE);
878a97cf 666 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
de69e0f0 667 sync_single(hwdev, dma_addr, size, dir, target);
878a97cf 668 else if (dir == DMA_FROM_DEVICE)
cde14bbf 669 dma_mark_clean(dma_addr, size);
878a97cf
JL
670}
671
672void
673swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
674 unsigned long offset, size_t size, int dir)
675{
de69e0f0
JL
676 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
677 SYNC_FOR_CPU);
878a97cf
JL
678}
679
680void
681swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
682 unsigned long offset, size_t size, int dir)
683{
de69e0f0
JL
684 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
685 SYNC_FOR_DEVICE);
878a97cf
JL
686}
687
309df0c5
AK
688void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
689 struct dma_attrs *);
1da177e4
LT
690/*
691 * Map a set of buffers described by scatterlist in streaming mode for DMA.
692 * This is the scatter-gather version of the above swiotlb_map_single
693 * interface. Here the scatter gather list elements are each tagged with the
694 * appropriate dma address and length. They are obtained via
695 * sg_dma_{address,length}(SG).
696 *
697 * NOTE: An implementation may be able to use a smaller number of
698 * DMA address/length pairs than there are SG table elements.
699 * (for example via virtual mapping capabilities)
700 * The routine returns the number of addr/length pairs actually
701 * used, at most nents.
702 *
703 * Device ownership issues as mentioned above for swiotlb_map_single are the
704 * same here.
705 */
706int
309df0c5
AK
707swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
708 int dir, struct dma_attrs *attrs)
1da177e4 709{
dbfd49fe 710 struct scatterlist *sg;
25667d67 711 void *addr;
563aaf06 712 dma_addr_t dev_addr;
1da177e4
LT
713 int i;
714
34814545 715 BUG_ON(dir == DMA_NONE);
1da177e4 716
dbfd49fe 717 for_each_sg(sgl, sg, nelems, i) {
25667d67
TL
718 addr = SG_ENT_VIRT_ADDRESS(sg);
719 dev_addr = virt_to_bus(addr);
720 if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
721 void *map = map_single(hwdev, addr, sg->length, dir);
7e870233 722 if (!map) {
1da177e4
LT
723 /* Don't panic here, we expect map_sg users
724 to do proper error handling. */
725 swiotlb_full(hwdev, sg->length, dir, 0);
309df0c5
AK
726 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
727 attrs);
dbfd49fe 728 sgl[0].dma_length = 0;
1da177e4
LT
729 return 0;
730 }
cde14bbf 731 sg->dma_address = virt_to_bus(map);
1da177e4
LT
732 } else
733 sg->dma_address = dev_addr;
734 sg->dma_length = sg->length;
735 }
736 return nelems;
737}
309df0c5
AK
738EXPORT_SYMBOL(swiotlb_map_sg_attrs);
739
740int
741swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
742 int dir)
743{
744 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
745}
1da177e4
LT
746
747/*
748 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
749 * concerning calls here are the same as for swiotlb_unmap_single() above.
750 */
751void
309df0c5
AK
752swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
753 int nelems, int dir, struct dma_attrs *attrs)
1da177e4 754{
dbfd49fe 755 struct scatterlist *sg;
1da177e4
LT
756 int i;
757
34814545 758 BUG_ON(dir == DMA_NONE);
1da177e4 759
dbfd49fe 760 for_each_sg(sgl, sg, nelems, i) {
1da177e4 761 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
93fbff63
JB
762 unmap_single(hwdev, bus_to_virt(sg->dma_address),
763 sg->dma_length, dir);
1da177e4 764 else if (dir == DMA_FROM_DEVICE)
cde14bbf 765 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
dbfd49fe 766 }
1da177e4 767}
309df0c5
AK
768EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
769
770void
771swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
772 int dir)
773{
774 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
775}
1da177e4
LT
776
777/*
778 * Make physical memory consistent for a set of streaming mode DMA translations
779 * after a transfer.
780 *
781 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
782 * and usage.
783 */
be6b0267 784static void
dbfd49fe 785swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
de69e0f0 786 int nelems, int dir, int target)
1da177e4 787{
dbfd49fe 788 struct scatterlist *sg;
1da177e4
LT
789 int i;
790
34814545 791 BUG_ON(dir == DMA_NONE);
1da177e4 792
dbfd49fe 793 for_each_sg(sgl, sg, nelems, i) {
1da177e4 794 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
93fbff63 795 sync_single(hwdev, bus_to_virt(sg->dma_address),
de69e0f0 796 sg->dma_length, dir, target);
cde14bbf
JB
797 else if (dir == DMA_FROM_DEVICE)
798 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
dbfd49fe 799 }
1da177e4
LT
800}
801
8270f3f1
JL
802void
803swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
804 int nelems, int dir)
805{
de69e0f0 806 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
8270f3f1
JL
807}
808
1da177e4
LT
809void
810swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
811 int nelems, int dir)
812{
de69e0f0 813 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
1da177e4
LT
814}
815
816int
8d8bb39b 817swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
1da177e4 818{
93fbff63 819 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
1da177e4
LT
820}
821
822/*
17e5ad6c 823 * Return whether the given device DMA address mask can be supported
1da177e4 824 * properly. For example, if your device can only drive the low 24-bits
17e5ad6c 825 * during bus mastering, then you would pass 0x00ffffff as the mask to
1da177e4
LT
826 * this function.
827 */
828int
563aaf06 829swiotlb_dma_supported(struct device *hwdev, u64 mask)
1da177e4 830{
25667d67 831 return virt_to_bus(io_tlb_end - 1) <= mask;
1da177e4
LT
832}
833
1da177e4
LT
834EXPORT_SYMBOL(swiotlb_map_single);
835EXPORT_SYMBOL(swiotlb_unmap_single);
836EXPORT_SYMBOL(swiotlb_map_sg);
837EXPORT_SYMBOL(swiotlb_unmap_sg);
838EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
839EXPORT_SYMBOL(swiotlb_sync_single_for_device);
878a97cf
JL
840EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
841EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
1da177e4
LT
842EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
843EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
844EXPORT_SYMBOL(swiotlb_dma_mapping_error);
25667d67
TL
845EXPORT_SYMBOL(swiotlb_alloc_coherent);
846EXPORT_SYMBOL(swiotlb_free_coherent);
1da177e4 847EXPORT_SYMBOL(swiotlb_dma_supported);