Merge tag 'f2fs-for-3.9' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk...
[linux-2.6-block.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.
fb05a379 17 * 08/12/11 beckyb Add highmem support
1da177e4
LT
18 */
19
20#include <linux/cache.h>
17e5ad6c 21#include <linux/dma-mapping.h>
1da177e4 22#include <linux/mm.h>
8bc3bcc9 23#include <linux/export.h>
1da177e4
LT
24#include <linux/spinlock.h>
25#include <linux/string.h>
0016fdee 26#include <linux/swiotlb.h>
fb05a379 27#include <linux/pfn.h>
1da177e4
LT
28#include <linux/types.h>
29#include <linux/ctype.h>
ef9b1893 30#include <linux/highmem.h>
5a0e3ad6 31#include <linux/gfp.h>
1da177e4
LT
32
33#include <asm/io.h>
1da177e4 34#include <asm/dma.h>
17e5ad6c 35#include <asm/scatterlist.h>
1da177e4
LT
36
37#include <linux/init.h>
38#include <linux/bootmem.h>
a8522509 39#include <linux/iommu-helper.h>
1da177e4
LT
40
41#define OFFSET(val,align) ((unsigned long) \
42 ( (val) & ( (align) - 1)))
43
0b9afede
AW
44#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
45
46/*
47 * Minimum IO TLB size to bother booting with. Systems with mainly
48 * 64bit capable cards will only lightly use the swiotlb. If we can't
49 * allocate a contiguous 1MB, we're probably in trouble anyway.
50 */
51#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
52
1da177e4
LT
53int swiotlb_force;
54
55/*
bfc5501f
KRW
56 * Used to do a quick range check in swiotlb_tbl_unmap_single and
57 * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this
1da177e4
LT
58 * API.
59 */
ff7204a7 60static phys_addr_t io_tlb_start, io_tlb_end;
1da177e4
LT
61
62/*
b595076a 63 * The number of IO TLB blocks (in groups of 64) between io_tlb_start and
1da177e4
LT
64 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
65 */
66static unsigned long io_tlb_nslabs;
67
68/*
69 * When the IOMMU overflows we return a fallback buffer. This sets the size.
70 */
71static unsigned long io_tlb_overflow = 32*1024;
72
ee3f6ba8 73static phys_addr_t io_tlb_overflow_buffer;
1da177e4
LT
74
75/*
76 * This is a free list describing the number of free entries available from
77 * each index
78 */
79static unsigned int *io_tlb_list;
80static unsigned int io_tlb_index;
81
82/*
83 * We need to save away the original address corresponding to a mapped entry
84 * for the sync operations.
85 */
bc40ac66 86static phys_addr_t *io_tlb_orig_addr;
1da177e4
LT
87
88/*
89 * Protect the above data structures in the map and unmap calls
90 */
91static DEFINE_SPINLOCK(io_tlb_lock);
92
5740afdb
FT
93static int late_alloc;
94
1da177e4
LT
95static int __init
96setup_io_tlb_npages(char *str)
97{
98 if (isdigit(*str)) {
e8579e72 99 io_tlb_nslabs = simple_strtoul(str, &str, 0);
1da177e4
LT
100 /* avoid tail segment of size < IO_TLB_SEGSIZE */
101 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
102 }
103 if (*str == ',')
104 ++str;
b18485e7 105 if (!strcmp(str, "force"))
1da177e4 106 swiotlb_force = 1;
b18485e7 107
1da177e4
LT
108 return 1;
109}
110__setup("swiotlb=", setup_io_tlb_npages);
111/* make io_tlb_overflow tunable too? */
112
f21ffe9f 113unsigned long swiotlb_nr_tbl(void)
5f98ecdb
FT
114{
115 return io_tlb_nslabs;
116}
f21ffe9f 117EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
02ca646e 118/* Note that this doesn't work with highmem page */
70a7d3cc
JF
119static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
120 volatile void *address)
e08e1f7a 121{
862d196b 122 return phys_to_dma(hwdev, virt_to_phys(address));
e08e1f7a
IC
123}
124
ac2cbab2
YL
125static bool no_iotlb_memory;
126
ad32e8cb 127void swiotlb_print_info(void)
2e5b2b86 128{
ad32e8cb 129 unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
ff7204a7 130 unsigned char *vstart, *vend;
2e5b2b86 131
ac2cbab2
YL
132 if (no_iotlb_memory) {
133 pr_warn("software IO TLB: No low mem\n");
134 return;
135 }
136
ff7204a7 137 vstart = phys_to_virt(io_tlb_start);
c40dba06 138 vend = phys_to_virt(io_tlb_end);
2e5b2b86 139
3af684c7 140 printk(KERN_INFO "software IO TLB [mem %#010llx-%#010llx] (%luMB) mapped at [%p-%p]\n",
ff7204a7 141 (unsigned long long)io_tlb_start,
c40dba06 142 (unsigned long long)io_tlb_end,
ff7204a7 143 bytes >> 20, vstart, vend - 1);
2e5b2b86
IC
144}
145
ac2cbab2 146int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
1da177e4 147{
ee3f6ba8 148 void *v_overflow_buffer;
563aaf06 149 unsigned long i, bytes;
1da177e4 150
abbceff7 151 bytes = nslabs << IO_TLB_SHIFT;
1da177e4 152
abbceff7 153 io_tlb_nslabs = nslabs;
ff7204a7
AD
154 io_tlb_start = __pa(tlb);
155 io_tlb_end = io_tlb_start + bytes;
1da177e4 156
ee3f6ba8
AD
157 /*
158 * Get the overflow emergency buffer
159 */
ac2cbab2
YL
160 v_overflow_buffer = alloc_bootmem_low_pages_nopanic(
161 PAGE_ALIGN(io_tlb_overflow));
ee3f6ba8 162 if (!v_overflow_buffer)
ac2cbab2 163 return -ENOMEM;
ee3f6ba8
AD
164
165 io_tlb_overflow_buffer = __pa(v_overflow_buffer);
166
1da177e4
LT
167 /*
168 * Allocate and initialize the free list array. This array is used
169 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
170 * between io_tlb_start and io_tlb_end.
171 */
e79f86b2 172 io_tlb_list = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_nslabs * sizeof(int)));
25667d67 173 for (i = 0; i < io_tlb_nslabs; i++)
1da177e4
LT
174 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
175 io_tlb_index = 0;
e79f86b2 176 io_tlb_orig_addr = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)));
1da177e4 177
ad32e8cb
FT
178 if (verbose)
179 swiotlb_print_info();
ac2cbab2
YL
180
181 return 0;
1da177e4
LT
182}
183
abbceff7
FT
184/*
185 * Statically reserve bounce buffer space and initialize bounce buffer data
186 * structures for the software IO TLB used to implement the DMA API.
187 */
ac2cbab2
YL
188void __init
189swiotlb_init(int verbose)
abbceff7 190{
ac2cbab2
YL
191 /* default to 64MB */
192 size_t default_size = 64UL<<20;
ff7204a7 193 unsigned char *vstart;
abbceff7
FT
194 unsigned long bytes;
195
196 if (!io_tlb_nslabs) {
197 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
198 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
199 }
200
201 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
202
ac2cbab2
YL
203 /* Get IO TLB memory from the low pages */
204 vstart = alloc_bootmem_low_pages_nopanic(PAGE_ALIGN(bytes));
205 if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose))
206 return;
abbceff7 207
ac2cbab2
YL
208 if (io_tlb_start)
209 free_bootmem(io_tlb_start,
210 PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
211 pr_warn("Cannot allocate SWIOTLB buffer");
212 no_iotlb_memory = true;
1da177e4
LT
213}
214
0b9afede
AW
215/*
216 * Systems with larger DMA zones (those that don't support ISA) can
217 * initialize the swiotlb later using the slab allocator if needed.
218 * This should be just like above, but with some error catching.
219 */
220int
563aaf06 221swiotlb_late_init_with_default_size(size_t default_size)
0b9afede 222{
74838b75 223 unsigned long bytes, req_nslabs = io_tlb_nslabs;
ff7204a7 224 unsigned char *vstart = NULL;
0b9afede 225 unsigned int order;
74838b75 226 int rc = 0;
0b9afede
AW
227
228 if (!io_tlb_nslabs) {
229 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
230 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
231 }
232
233 /*
234 * Get IO TLB memory from the low pages
235 */
563aaf06 236 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
0b9afede 237 io_tlb_nslabs = SLABS_PER_PAGE << order;
563aaf06 238 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
0b9afede
AW
239
240 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
ff7204a7
AD
241 vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
242 order);
243 if (vstart)
0b9afede
AW
244 break;
245 order--;
246 }
247
ff7204a7 248 if (!vstart) {
74838b75
KRW
249 io_tlb_nslabs = req_nslabs;
250 return -ENOMEM;
251 }
563aaf06 252 if (order != get_order(bytes)) {
0b9afede
AW
253 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
254 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
255 io_tlb_nslabs = SLABS_PER_PAGE << order;
256 }
ff7204a7 257 rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs);
74838b75 258 if (rc)
ff7204a7 259 free_pages((unsigned long)vstart, order);
74838b75
KRW
260 return rc;
261}
262
263int
264swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
265{
266 unsigned long i, bytes;
ee3f6ba8 267 unsigned char *v_overflow_buffer;
74838b75
KRW
268
269 bytes = nslabs << IO_TLB_SHIFT;
270
271 io_tlb_nslabs = nslabs;
ff7204a7
AD
272 io_tlb_start = virt_to_phys(tlb);
273 io_tlb_end = io_tlb_start + bytes;
74838b75 274
ff7204a7 275 memset(tlb, 0, bytes);
0b9afede 276
ee3f6ba8
AD
277 /*
278 * Get the overflow emergency buffer
279 */
280 v_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
281 get_order(io_tlb_overflow));
282 if (!v_overflow_buffer)
283 goto cleanup2;
284
285 io_tlb_overflow_buffer = virt_to_phys(v_overflow_buffer);
286
0b9afede
AW
287 /*
288 * Allocate and initialize the free list array. This array is used
289 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
290 * between io_tlb_start and io_tlb_end.
291 */
292 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
293 get_order(io_tlb_nslabs * sizeof(int)));
294 if (!io_tlb_list)
ee3f6ba8 295 goto cleanup3;
0b9afede
AW
296
297 for (i = 0; i < io_tlb_nslabs; i++)
298 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
299 io_tlb_index = 0;
300
bc40ac66
BB
301 io_tlb_orig_addr = (phys_addr_t *)
302 __get_free_pages(GFP_KERNEL,
303 get_order(io_tlb_nslabs *
304 sizeof(phys_addr_t)));
0b9afede 305 if (!io_tlb_orig_addr)
ee3f6ba8 306 goto cleanup4;
0b9afede 307
bc40ac66 308 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(phys_addr_t));
0b9afede 309
ad32e8cb 310 swiotlb_print_info();
0b9afede 311
5740afdb
FT
312 late_alloc = 1;
313
0b9afede
AW
314 return 0;
315
316cleanup4:
25667d67
TL
317 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
318 sizeof(int)));
0b9afede 319 io_tlb_list = NULL;
ee3f6ba8
AD
320cleanup3:
321 free_pages((unsigned long)v_overflow_buffer,
322 get_order(io_tlb_overflow));
323 io_tlb_overflow_buffer = 0;
0b9afede 324cleanup2:
c40dba06 325 io_tlb_end = 0;
ff7204a7 326 io_tlb_start = 0;
74838b75 327 io_tlb_nslabs = 0;
0b9afede
AW
328 return -ENOMEM;
329}
330
5740afdb
FT
331void __init swiotlb_free(void)
332{
ee3f6ba8 333 if (!io_tlb_orig_addr)
5740afdb
FT
334 return;
335
336 if (late_alloc) {
ee3f6ba8 337 free_pages((unsigned long)phys_to_virt(io_tlb_overflow_buffer),
5740afdb
FT
338 get_order(io_tlb_overflow));
339 free_pages((unsigned long)io_tlb_orig_addr,
340 get_order(io_tlb_nslabs * sizeof(phys_addr_t)));
341 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
342 sizeof(int)));
ff7204a7 343 free_pages((unsigned long)phys_to_virt(io_tlb_start),
5740afdb
FT
344 get_order(io_tlb_nslabs << IO_TLB_SHIFT));
345 } else {
ee3f6ba8 346 free_bootmem_late(io_tlb_overflow_buffer,
e79f86b2 347 PAGE_ALIGN(io_tlb_overflow));
5740afdb 348 free_bootmem_late(__pa(io_tlb_orig_addr),
e79f86b2 349 PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)));
5740afdb 350 free_bootmem_late(__pa(io_tlb_list),
e79f86b2 351 PAGE_ALIGN(io_tlb_nslabs * sizeof(int)));
ff7204a7 352 free_bootmem_late(io_tlb_start,
e79f86b2 353 PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
5740afdb 354 }
f21ffe9f 355 io_tlb_nslabs = 0;
5740afdb
FT
356}
357
02ca646e 358static int is_swiotlb_buffer(phys_addr_t paddr)
640aebfe 359{
ff7204a7 360 return paddr >= io_tlb_start && paddr < io_tlb_end;
640aebfe
FT
361}
362
fb05a379
BB
363/*
364 * Bounce: copy the swiotlb buffer back to the original dma location
365 */
af51a9f1
AD
366static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
367 size_t size, enum dma_data_direction dir)
fb05a379 368{
af51a9f1
AD
369 unsigned long pfn = PFN_DOWN(orig_addr);
370 unsigned char *vaddr = phys_to_virt(tlb_addr);
fb05a379
BB
371
372 if (PageHighMem(pfn_to_page(pfn))) {
373 /* The buffer does not have a mapping. Map it in and copy */
af51a9f1 374 unsigned int offset = orig_addr & ~PAGE_MASK;
fb05a379
BB
375 char *buffer;
376 unsigned int sz = 0;
377 unsigned long flags;
378
379 while (size) {
67131ad0 380 sz = min_t(size_t, PAGE_SIZE - offset, size);
fb05a379
BB
381
382 local_irq_save(flags);
c3eede8e 383 buffer = kmap_atomic(pfn_to_page(pfn));
fb05a379 384 if (dir == DMA_TO_DEVICE)
af51a9f1 385 memcpy(vaddr, buffer + offset, sz);
ef9b1893 386 else
af51a9f1 387 memcpy(buffer + offset, vaddr, sz);
c3eede8e 388 kunmap_atomic(buffer);
ef9b1893 389 local_irq_restore(flags);
fb05a379
BB
390
391 size -= sz;
392 pfn++;
af51a9f1 393 vaddr += sz;
fb05a379 394 offset = 0;
ef9b1893 395 }
af51a9f1
AD
396 } else if (dir == DMA_TO_DEVICE) {
397 memcpy(vaddr, phys_to_virt(orig_addr), size);
ef9b1893 398 } else {
af51a9f1 399 memcpy(phys_to_virt(orig_addr), vaddr, size);
ef9b1893 400 }
1b548f66
JF
401}
402
e05ed4d1
AD
403phys_addr_t swiotlb_tbl_map_single(struct device *hwdev,
404 dma_addr_t tbl_dma_addr,
405 phys_addr_t orig_addr, size_t size,
406 enum dma_data_direction dir)
1da177e4
LT
407{
408 unsigned long flags;
e05ed4d1 409 phys_addr_t tlb_addr;
1da177e4
LT
410 unsigned int nslots, stride, index, wrap;
411 int i;
681cc5cd
FT
412 unsigned long mask;
413 unsigned long offset_slots;
414 unsigned long max_slots;
415
ac2cbab2
YL
416 if (no_iotlb_memory)
417 panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
418
681cc5cd 419 mask = dma_get_seg_boundary(hwdev);
681cc5cd 420
eb605a57
FT
421 tbl_dma_addr &= mask;
422
423 offset_slots = ALIGN(tbl_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
a5ddde4a
IC
424
425 /*
426 * Carefully handle integer overflow which can occur when mask == ~0UL.
427 */
b15a3891
JB
428 max_slots = mask + 1
429 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
430 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
1da177e4
LT
431
432 /*
433 * For mappings greater than a page, we limit the stride (and
434 * hence alignment) to a page size.
435 */
436 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
437 if (size > PAGE_SIZE)
438 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
439 else
440 stride = 1;
441
34814545 442 BUG_ON(!nslots);
1da177e4
LT
443
444 /*
445 * Find suitable number of IO TLB entries size that will fit this
446 * request and allocate a buffer from that IO TLB pool.
447 */
448 spin_lock_irqsave(&io_tlb_lock, flags);
a7133a15
AM
449 index = ALIGN(io_tlb_index, stride);
450 if (index >= io_tlb_nslabs)
451 index = 0;
452 wrap = index;
453
454 do {
a8522509
FT
455 while (iommu_is_span_boundary(index, nslots, offset_slots,
456 max_slots)) {
b15a3891
JB
457 index += stride;
458 if (index >= io_tlb_nslabs)
459 index = 0;
a7133a15
AM
460 if (index == wrap)
461 goto not_found;
462 }
463
464 /*
465 * If we find a slot that indicates we have 'nslots' number of
466 * contiguous buffers, we allocate the buffers from that slot
467 * and mark the entries as '0' indicating unavailable.
468 */
469 if (io_tlb_list[index] >= nslots) {
470 int count = 0;
471
472 for (i = index; i < (int) (index + nslots); i++)
473 io_tlb_list[i] = 0;
474 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
475 io_tlb_list[i] = ++count;
e05ed4d1 476 tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT);
1da177e4 477
a7133a15
AM
478 /*
479 * Update the indices to avoid searching in the next
480 * round.
481 */
482 io_tlb_index = ((index + nslots) < io_tlb_nslabs
483 ? (index + nslots) : 0);
484
485 goto found;
486 }
487 index += stride;
488 if (index >= io_tlb_nslabs)
489 index = 0;
490 } while (index != wrap);
491
492not_found:
493 spin_unlock_irqrestore(&io_tlb_lock, flags);
e05ed4d1 494 return SWIOTLB_MAP_ERROR;
a7133a15 495found:
1da177e4
LT
496 spin_unlock_irqrestore(&io_tlb_lock, flags);
497
498 /*
499 * Save away the mapping from the original address to the DMA address.
500 * This is needed when we sync the memory. Then we sync the buffer if
501 * needed.
502 */
bc40ac66 503 for (i = 0; i < nslots; i++)
e05ed4d1 504 io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
1da177e4 505 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
af51a9f1 506 swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE);
1da177e4 507
e05ed4d1 508 return tlb_addr;
1da177e4 509}
d7ef1533 510EXPORT_SYMBOL_GPL(swiotlb_tbl_map_single);
1da177e4 511
eb605a57
FT
512/*
513 * Allocates bounce buffer and returns its kernel virtual address.
514 */
515
e05ed4d1
AD
516phys_addr_t map_single(struct device *hwdev, phys_addr_t phys, size_t size,
517 enum dma_data_direction dir)
eb605a57 518{
ff7204a7 519 dma_addr_t start_dma_addr = phys_to_dma(hwdev, io_tlb_start);
eb605a57
FT
520
521 return swiotlb_tbl_map_single(hwdev, start_dma_addr, phys, size, dir);
522}
523
1da177e4
LT
524/*
525 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
526 */
61ca08c3
AD
527void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
528 size_t size, enum dma_data_direction dir)
1da177e4
LT
529{
530 unsigned long flags;
531 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
61ca08c3
AD
532 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
533 phys_addr_t orig_addr = io_tlb_orig_addr[index];
1da177e4
LT
534
535 /*
536 * First, sync the memory before unmapping the entry
537 */
af51a9f1
AD
538 if (orig_addr && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
539 swiotlb_bounce(orig_addr, tlb_addr, size, DMA_FROM_DEVICE);
1da177e4
LT
540
541 /*
542 * Return the buffer to the free list by setting the corresponding
af901ca1 543 * entries to indicate the number of contiguous entries available.
1da177e4
LT
544 * While returning the entries to the free list, we merge the entries
545 * with slots below and above the pool being returned.
546 */
547 spin_lock_irqsave(&io_tlb_lock, flags);
548 {
549 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
550 io_tlb_list[index + nslots] : 0);
551 /*
552 * Step 1: return the slots to the free list, merging the
553 * slots with superceeding slots
554 */
555 for (i = index + nslots - 1; i >= index; i--)
556 io_tlb_list[i] = ++count;
557 /*
558 * Step 2: merge the returned slots with the preceding slots,
559 * if available (non zero)
560 */
561 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
562 io_tlb_list[i] = ++count;
563 }
564 spin_unlock_irqrestore(&io_tlb_lock, flags);
565}
d7ef1533 566EXPORT_SYMBOL_GPL(swiotlb_tbl_unmap_single);
1da177e4 567
fbfda893
AD
568void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr,
569 size_t size, enum dma_data_direction dir,
570 enum dma_sync_target target)
1da177e4 571{
fbfda893
AD
572 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
573 phys_addr_t orig_addr = io_tlb_orig_addr[index];
bc40ac66 574
fbfda893 575 orig_addr += (unsigned long)tlb_addr & ((1 << IO_TLB_SHIFT) - 1);
df336d1c 576
de69e0f0
JL
577 switch (target) {
578 case SYNC_FOR_CPU:
579 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
af51a9f1 580 swiotlb_bounce(orig_addr, tlb_addr,
fbfda893 581 size, DMA_FROM_DEVICE);
34814545
ES
582 else
583 BUG_ON(dir != DMA_TO_DEVICE);
de69e0f0
JL
584 break;
585 case SYNC_FOR_DEVICE:
586 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
af51a9f1 587 swiotlb_bounce(orig_addr, tlb_addr,
fbfda893 588 size, DMA_TO_DEVICE);
34814545
ES
589 else
590 BUG_ON(dir != DMA_FROM_DEVICE);
de69e0f0
JL
591 break;
592 default:
1da177e4 593 BUG();
de69e0f0 594 }
1da177e4 595}
d7ef1533 596EXPORT_SYMBOL_GPL(swiotlb_tbl_sync_single);
1da177e4
LT
597
598void *
599swiotlb_alloc_coherent(struct device *hwdev, size_t size,
06a54497 600 dma_addr_t *dma_handle, gfp_t flags)
1da177e4 601{
563aaf06 602 dma_addr_t dev_addr;
1da177e4
LT
603 void *ret;
604 int order = get_order(size);
284901a9 605 u64 dma_mask = DMA_BIT_MASK(32);
1e74f300
FT
606
607 if (hwdev && hwdev->coherent_dma_mask)
608 dma_mask = hwdev->coherent_dma_mask;
1da177e4 609
25667d67 610 ret = (void *)__get_free_pages(flags, order);
e05ed4d1
AD
611 if (ret) {
612 dev_addr = swiotlb_virt_to_bus(hwdev, ret);
613 if (dev_addr + size - 1 > dma_mask) {
614 /*
615 * The allocated memory isn't reachable by the device.
616 */
617 free_pages((unsigned long) ret, order);
618 ret = NULL;
619 }
1da177e4
LT
620 }
621 if (!ret) {
622 /*
bfc5501f
KRW
623 * We are either out of memory or the device can't DMA to
624 * GFP_DMA memory; fall back on map_single(), which
ceb5ac32 625 * will grab memory from the lowest available address range.
1da177e4 626 */
e05ed4d1
AD
627 phys_addr_t paddr = map_single(hwdev, 0, size, DMA_FROM_DEVICE);
628 if (paddr == SWIOTLB_MAP_ERROR)
1da177e4 629 return NULL;
1da177e4 630
e05ed4d1
AD
631 ret = phys_to_virt(paddr);
632 dev_addr = phys_to_dma(hwdev, paddr);
1da177e4 633
61ca08c3
AD
634 /* Confirm address can be DMA'd by device */
635 if (dev_addr + size - 1 > dma_mask) {
636 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
637 (unsigned long long)dma_mask,
638 (unsigned long long)dev_addr);
a2b89b59 639
61ca08c3
AD
640 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
641 swiotlb_tbl_unmap_single(hwdev, paddr,
642 size, DMA_TO_DEVICE);
643 return NULL;
644 }
1da177e4 645 }
e05ed4d1 646
1da177e4 647 *dma_handle = dev_addr;
e05ed4d1
AD
648 memset(ret, 0, size);
649
1da177e4
LT
650 return ret;
651}
874d6a95 652EXPORT_SYMBOL(swiotlb_alloc_coherent);
1da177e4
LT
653
654void
655swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
02ca646e 656 dma_addr_t dev_addr)
1da177e4 657{
862d196b 658 phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
02ca646e 659
aa24886e 660 WARN_ON(irqs_disabled());
02ca646e
FT
661 if (!is_swiotlb_buffer(paddr))
662 free_pages((unsigned long)vaddr, get_order(size));
1da177e4 663 else
bfc5501f 664 /* DMA_TO_DEVICE to avoid memcpy in swiotlb_tbl_unmap_single */
61ca08c3 665 swiotlb_tbl_unmap_single(hwdev, paddr, size, DMA_TO_DEVICE);
1da177e4 666}
874d6a95 667EXPORT_SYMBOL(swiotlb_free_coherent);
1da177e4
LT
668
669static void
22d48269
KRW
670swiotlb_full(struct device *dev, size_t size, enum dma_data_direction dir,
671 int do_panic)
1da177e4
LT
672{
673 /*
674 * Ran out of IOMMU space for this operation. This is very bad.
675 * Unfortunately the drivers cannot handle this operation properly.
17e5ad6c 676 * unless they check for dma_mapping_error (most don't)
1da177e4
LT
677 * When the mapping is small enough return a static buffer to limit
678 * the damage, or panic when the transfer is too big.
679 */
563aaf06 680 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
94b32486 681 "device %s\n", size, dev ? dev_name(dev) : "?");
1da177e4 682
c7084b35
CD
683 if (size <= io_tlb_overflow || !do_panic)
684 return;
685
686 if (dir == DMA_BIDIRECTIONAL)
687 panic("DMA: Random memory could be DMA accessed\n");
688 if (dir == DMA_FROM_DEVICE)
689 panic("DMA: Random memory could be DMA written\n");
690 if (dir == DMA_TO_DEVICE)
691 panic("DMA: Random memory could be DMA read\n");
1da177e4
LT
692}
693
694/*
695 * Map a single buffer of the indicated size for DMA in streaming mode. The
17e5ad6c 696 * physical address to use is returned.
1da177e4
LT
697 *
698 * Once the device is given the dma address, the device owns this memory until
ceb5ac32 699 * either swiotlb_unmap_page or swiotlb_dma_sync_single is performed.
1da177e4 700 */
f98eee8e
FT
701dma_addr_t swiotlb_map_page(struct device *dev, struct page *page,
702 unsigned long offset, size_t size,
703 enum dma_data_direction dir,
704 struct dma_attrs *attrs)
1da177e4 705{
e05ed4d1 706 phys_addr_t map, phys = page_to_phys(page) + offset;
862d196b 707 dma_addr_t dev_addr = phys_to_dma(dev, phys);
1da177e4 708
34814545 709 BUG_ON(dir == DMA_NONE);
1da177e4 710 /*
ceb5ac32 711 * If the address happens to be in the device's DMA window,
1da177e4
LT
712 * we can safely return the device addr and not worry about bounce
713 * buffering it.
714 */
b9394647 715 if (dma_capable(dev, dev_addr, size) && !swiotlb_force)
1da177e4
LT
716 return dev_addr;
717
e05ed4d1 718 /* Oh well, have to allocate and map a bounce buffer. */
f98eee8e 719 map = map_single(dev, phys, size, dir);
e05ed4d1 720 if (map == SWIOTLB_MAP_ERROR) {
f98eee8e 721 swiotlb_full(dev, size, dir, 1);
ee3f6ba8 722 return phys_to_dma(dev, io_tlb_overflow_buffer);
1da177e4
LT
723 }
724
e05ed4d1 725 dev_addr = phys_to_dma(dev, map);
1da177e4 726
e05ed4d1 727 /* Ensure that the address returned is DMA'ble */
fba99fa3 728 if (!dma_capable(dev, dev_addr, size)) {
61ca08c3 729 swiotlb_tbl_unmap_single(dev, map, size, dir);
ee3f6ba8 730 return phys_to_dma(dev, io_tlb_overflow_buffer);
fba99fa3 731 }
1da177e4
LT
732
733 return dev_addr;
734}
f98eee8e 735EXPORT_SYMBOL_GPL(swiotlb_map_page);
1da177e4 736
1da177e4
LT
737/*
738 * Unmap a single streaming mode DMA translation. The dma_addr and size must
ceb5ac32 739 * match what was provided for in a previous swiotlb_map_page call. All
1da177e4
LT
740 * other usages are undefined.
741 *
742 * After this call, reads by the cpu to the buffer are guaranteed to see
743 * whatever the device wrote there.
744 */
7fcebbd2 745static void unmap_single(struct device *hwdev, dma_addr_t dev_addr,
22d48269 746 size_t size, enum dma_data_direction dir)
1da177e4 747{
862d196b 748 phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
1da177e4 749
34814545 750 BUG_ON(dir == DMA_NONE);
7fcebbd2 751
02ca646e 752 if (is_swiotlb_buffer(paddr)) {
61ca08c3 753 swiotlb_tbl_unmap_single(hwdev, paddr, size, dir);
7fcebbd2
BB
754 return;
755 }
756
757 if (dir != DMA_FROM_DEVICE)
758 return;
759
02ca646e
FT
760 /*
761 * phys_to_virt doesn't work with hihgmem page but we could
762 * call dma_mark_clean() with hihgmem page here. However, we
763 * are fine since dma_mark_clean() is null on POWERPC. We can
764 * make dma_mark_clean() take a physical address if necessary.
765 */
766 dma_mark_clean(phys_to_virt(paddr), size);
7fcebbd2
BB
767}
768
769void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,
770 size_t size, enum dma_data_direction dir,
771 struct dma_attrs *attrs)
772{
773 unmap_single(hwdev, dev_addr, size, dir);
1da177e4 774}
f98eee8e 775EXPORT_SYMBOL_GPL(swiotlb_unmap_page);
874d6a95 776
1da177e4
LT
777/*
778 * Make physical memory consistent for a single streaming mode DMA translation
779 * after a transfer.
780 *
ceb5ac32 781 * If you perform a swiotlb_map_page() but wish to interrogate the buffer
17e5ad6c
TL
782 * using the cpu, yet do not wish to teardown the dma mapping, you must
783 * call this function before doing so. At the next point you give the dma
1da177e4
LT
784 * address back to the card, you must first perform a
785 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
786 */
be6b0267 787static void
8270f3f1 788swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
d7ef1533
KRW
789 size_t size, enum dma_data_direction dir,
790 enum dma_sync_target target)
1da177e4 791{
862d196b 792 phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
1da177e4 793
34814545 794 BUG_ON(dir == DMA_NONE);
380d6878 795
02ca646e 796 if (is_swiotlb_buffer(paddr)) {
fbfda893 797 swiotlb_tbl_sync_single(hwdev, paddr, size, dir, target);
380d6878
BB
798 return;
799 }
800
801 if (dir != DMA_FROM_DEVICE)
802 return;
803
02ca646e 804 dma_mark_clean(phys_to_virt(paddr), size);
1da177e4
LT
805}
806
8270f3f1
JL
807void
808swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
160c1d8e 809 size_t size, enum dma_data_direction dir)
8270f3f1 810{
de69e0f0 811 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
8270f3f1 812}
874d6a95 813EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
8270f3f1 814
1da177e4
LT
815void
816swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
160c1d8e 817 size_t size, enum dma_data_direction dir)
1da177e4 818{
de69e0f0 819 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
1da177e4 820}
874d6a95 821EXPORT_SYMBOL(swiotlb_sync_single_for_device);
1da177e4
LT
822
823/*
824 * Map a set of buffers described by scatterlist in streaming mode for DMA.
ceb5ac32 825 * This is the scatter-gather version of the above swiotlb_map_page
1da177e4
LT
826 * interface. Here the scatter gather list elements are each tagged with the
827 * appropriate dma address and length. They are obtained via
828 * sg_dma_{address,length}(SG).
829 *
830 * NOTE: An implementation may be able to use a smaller number of
831 * DMA address/length pairs than there are SG table elements.
832 * (for example via virtual mapping capabilities)
833 * The routine returns the number of addr/length pairs actually
834 * used, at most nents.
835 *
ceb5ac32 836 * Device ownership issues as mentioned above for swiotlb_map_page are the
1da177e4
LT
837 * same here.
838 */
839int
309df0c5 840swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
160c1d8e 841 enum dma_data_direction dir, struct dma_attrs *attrs)
1da177e4 842{
dbfd49fe 843 struct scatterlist *sg;
1da177e4
LT
844 int i;
845
34814545 846 BUG_ON(dir == DMA_NONE);
1da177e4 847
dbfd49fe 848 for_each_sg(sgl, sg, nelems, i) {
961d7d0e 849 phys_addr_t paddr = sg_phys(sg);
862d196b 850 dma_addr_t dev_addr = phys_to_dma(hwdev, paddr);
bc40ac66 851
cf56e3f2 852 if (swiotlb_force ||
b9394647 853 !dma_capable(hwdev, dev_addr, sg->length)) {
e05ed4d1
AD
854 phys_addr_t map = map_single(hwdev, sg_phys(sg),
855 sg->length, dir);
856 if (map == SWIOTLB_MAP_ERROR) {
1da177e4
LT
857 /* Don't panic here, we expect map_sg users
858 to do proper error handling. */
859 swiotlb_full(hwdev, sg->length, dir, 0);
309df0c5
AK
860 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
861 attrs);
dbfd49fe 862 sgl[0].dma_length = 0;
1da177e4
LT
863 return 0;
864 }
e05ed4d1 865 sg->dma_address = phys_to_dma(hwdev, map);
1da177e4
LT
866 } else
867 sg->dma_address = dev_addr;
868 sg->dma_length = sg->length;
869 }
870 return nelems;
871}
309df0c5
AK
872EXPORT_SYMBOL(swiotlb_map_sg_attrs);
873
874int
875swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
22d48269 876 enum dma_data_direction dir)
309df0c5
AK
877{
878 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
879}
874d6a95 880EXPORT_SYMBOL(swiotlb_map_sg);
1da177e4
LT
881
882/*
883 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
ceb5ac32 884 * concerning calls here are the same as for swiotlb_unmap_page() above.
1da177e4
LT
885 */
886void
309df0c5 887swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
160c1d8e 888 int nelems, enum dma_data_direction dir, struct dma_attrs *attrs)
1da177e4 889{
dbfd49fe 890 struct scatterlist *sg;
1da177e4
LT
891 int i;
892
34814545 893 BUG_ON(dir == DMA_NONE);
1da177e4 894
7fcebbd2
BB
895 for_each_sg(sgl, sg, nelems, i)
896 unmap_single(hwdev, sg->dma_address, sg->dma_length, dir);
897
1da177e4 898}
309df0c5
AK
899EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
900
901void
902swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
22d48269 903 enum dma_data_direction dir)
309df0c5
AK
904{
905 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
906}
874d6a95 907EXPORT_SYMBOL(swiotlb_unmap_sg);
1da177e4
LT
908
909/*
910 * Make physical memory consistent for a set of streaming mode DMA translations
911 * after a transfer.
912 *
913 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
914 * and usage.
915 */
be6b0267 916static void
dbfd49fe 917swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
d7ef1533
KRW
918 int nelems, enum dma_data_direction dir,
919 enum dma_sync_target target)
1da177e4 920{
dbfd49fe 921 struct scatterlist *sg;
1da177e4
LT
922 int i;
923
380d6878
BB
924 for_each_sg(sgl, sg, nelems, i)
925 swiotlb_sync_single(hwdev, sg->dma_address,
de69e0f0 926 sg->dma_length, dir, target);
1da177e4
LT
927}
928
8270f3f1
JL
929void
930swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
160c1d8e 931 int nelems, enum dma_data_direction dir)
8270f3f1 932{
de69e0f0 933 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
8270f3f1 934}
874d6a95 935EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
8270f3f1 936
1da177e4
LT
937void
938swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
160c1d8e 939 int nelems, enum dma_data_direction dir)
1da177e4 940{
de69e0f0 941 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
1da177e4 942}
874d6a95 943EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
1da177e4
LT
944
945int
8d8bb39b 946swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
1da177e4 947{
ee3f6ba8 948 return (dma_addr == phys_to_dma(hwdev, io_tlb_overflow_buffer));
1da177e4 949}
874d6a95 950EXPORT_SYMBOL(swiotlb_dma_mapping_error);
1da177e4
LT
951
952/*
17e5ad6c 953 * Return whether the given device DMA address mask can be supported
1da177e4 954 * properly. For example, if your device can only drive the low 24-bits
17e5ad6c 955 * during bus mastering, then you would pass 0x00ffffff as the mask to
1da177e4
LT
956 * this function.
957 */
958int
563aaf06 959swiotlb_dma_supported(struct device *hwdev, u64 mask)
1da177e4 960{
c40dba06 961 return phys_to_dma(hwdev, io_tlb_end - 1) <= mask;
1da177e4 962}
1da177e4 963EXPORT_SYMBOL(swiotlb_dma_supported);