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