]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob
2437504
[thirdparty/kernel/stable.git] /
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Dynamic DMA mapping support.
4 *
5 * This implementation is a fallback for platforms that do not support
6 * I/O TLBs (aka DMA address translation hardware).
7 * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
8 * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
9 * Copyright (C) 2000, 2003 Hewlett-Packard Co
10 * David Mosberger-Tang <davidm@hpl.hp.com>
11 *
12 * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API.
13 * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
14 * unnecessary i-cache flushing.
15 * 04/07/.. ak Better overflow handling. Assorted fixes.
16 * 05/09/10 linville Add support for syncing ranges, support syncing for
17 * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
18 * 08/12/11 beckyb Add highmem support
19 */
20
21 #define pr_fmt(fmt) "software IO TLB: " fmt
22
23 #include <linux/cache.h>
24 #include <linux/dma-direct.h>
25 #include <linux/dma-map-ops.h>
26 #include <linux/mm.h>
27 #include <linux/export.h>
28 #include <linux/spinlock.h>
29 #include <linux/string.h>
30 #include <linux/swiotlb.h>
31 #include <linux/pfn.h>
32 #include <linux/types.h>
33 #include <linux/ctype.h>
34 #include <linux/highmem.h>
35 #include <linux/gfp.h>
36 #include <linux/scatterlist.h>
37 #include <linux/mem_encrypt.h>
38 #include <linux/set_memory.h>
39 #ifdef CONFIG_DEBUG_FS
40 #include <linux/debugfs.h>
41 #endif
42
43 #include <asm/io.h>
44 #include <asm/dma.h>
45
46 #include <linux/init.h>
47 #include <linux/memblock.h>
48 #include <linux/iommu-helper.h>
49
50 #define CREATE_TRACE_POINTS
51 #include <trace/events/swiotlb.h>
52
53 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
54
55 /*
56 * Minimum IO TLB size to bother booting with. Systems with mainly
57 * 64bit capable cards will only lightly use the swiotlb. If we can't
58 * allocate a contiguous 1MB, we're probably in trouble anyway.
59 */
60 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
61
62 enum swiotlb_force swiotlb_force;
63
64 /*
65 * Used to do a quick range check in swiotlb_tbl_unmap_single and
66 * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this
67 * API.
68 */
69 phys_addr_t io_tlb_start, io_tlb_end;
70
71 /*
72 * The number of IO TLB blocks (in groups of 64) between io_tlb_start and
73 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
74 */
75 static unsigned long io_tlb_nslabs;
76
77 /*
78 * The number of used IO TLB block
79 */
80 static unsigned long io_tlb_used;
81
82 /*
83 * This is a free list describing the number of free entries available from
84 * each index
85 */
86 static unsigned int *io_tlb_list;
87 static unsigned int io_tlb_index;
88
89 /*
90 * Max segment that we can provide which (if pages are contingous) will
91 * not be bounced (unless SWIOTLB_FORCE is set).
92 */
93 static unsigned int max_segment;
94
95 /*
96 * We need to save away the original address corresponding to a mapped entry
97 * for the sync operations.
98 */
99 #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
100 static phys_addr_t *io_tlb_orig_addr;
101
102 /*
103 * Protect the above data structures in the map and unmap calls
104 */
105 static DEFINE_SPINLOCK(io_tlb_lock);
106
107 static int late_alloc;
108
109 static int __init
110 setup_io_tlb_npages(char *str)
111 {
112 if (isdigit(*str)) {
113 io_tlb_nslabs = simple_strtoul(str, &str, 0);
114 /* avoid tail segment of size < IO_TLB_SEGSIZE */
115 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
116 }
117 if (*str == ',')
118 ++str;
119 if (!strcmp(str, "force")) {
120 swiotlb_force = SWIOTLB_FORCE;
121 } else if (!strcmp(str, "noforce")) {
122 swiotlb_force = SWIOTLB_NO_FORCE;
123 io_tlb_nslabs = 1;
124 }
125
126 return 0;
127 }
128 early_param("swiotlb", setup_io_tlb_npages);
129
130 static bool no_iotlb_memory;
131
132 unsigned long swiotlb_nr_tbl(void)
133 {
134 return unlikely(no_iotlb_memory) ? 0 : io_tlb_nslabs;
135 }
136 EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
137
138 unsigned int swiotlb_max_segment(void)
139 {
140 return unlikely(no_iotlb_memory) ? 0 : max_segment;
141 }
142 EXPORT_SYMBOL_GPL(swiotlb_max_segment);
143
144 void swiotlb_set_max_segment(unsigned int val)
145 {
146 if (swiotlb_force == SWIOTLB_FORCE)
147 max_segment = 1;
148 else
149 max_segment = rounddown(val, PAGE_SIZE);
150 }
151
152 /* default to 64MB */
153 #define IO_TLB_DEFAULT_SIZE (64UL<<20)
154 unsigned long swiotlb_size_or_default(void)
155 {
156 unsigned long size;
157
158 size = io_tlb_nslabs << IO_TLB_SHIFT;
159
160 return size ? size : (IO_TLB_DEFAULT_SIZE);
161 }
162
163 void swiotlb_print_info(void)
164 {
165 unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
166
167 if (no_iotlb_memory) {
168 pr_warn("No low mem\n");
169 return;
170 }
171
172 pr_info("mapped [mem %pa-%pa] (%luMB)\n", &io_tlb_start, &io_tlb_end,
173 bytes >> 20);
174 }
175
176 static inline unsigned long io_tlb_offset(unsigned long val)
177 {
178 return val & (IO_TLB_SEGSIZE - 1);
179 }
180
181 static inline unsigned long nr_slots(u64 val)
182 {
183 return DIV_ROUND_UP(val, IO_TLB_SIZE);
184 }
185
186 /*
187 * Early SWIOTLB allocation may be too early to allow an architecture to
188 * perform the desired operations. This function allows the architecture to
189 * call SWIOTLB when the operations are possible. It needs to be called
190 * before the SWIOTLB memory is used.
191 */
192 void __init swiotlb_update_mem_attributes(void)
193 {
194 void *vaddr;
195 unsigned long bytes;
196
197 if (no_iotlb_memory || late_alloc)
198 return;
199
200 vaddr = phys_to_virt(io_tlb_start);
201 bytes = PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT);
202 set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
203 memset(vaddr, 0, bytes);
204 }
205
206 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
207 {
208 unsigned long i, bytes;
209 size_t alloc_size;
210
211 bytes = nslabs << IO_TLB_SHIFT;
212
213 io_tlb_nslabs = nslabs;
214 io_tlb_start = __pa(tlb);
215 io_tlb_end = io_tlb_start + bytes;
216
217 /*
218 * Allocate and initialize the free list array. This array is used
219 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
220 * between io_tlb_start and io_tlb_end.
221 */
222 alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(int));
223 io_tlb_list = memblock_alloc(alloc_size, PAGE_SIZE);
224 if (!io_tlb_list)
225 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
226 __func__, alloc_size, PAGE_SIZE);
227
228 alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t));
229 io_tlb_orig_addr = memblock_alloc(alloc_size, PAGE_SIZE);
230 if (!io_tlb_orig_addr)
231 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
232 __func__, alloc_size, PAGE_SIZE);
233
234 for (i = 0; i < io_tlb_nslabs; i++) {
235 io_tlb_list[i] = IO_TLB_SEGSIZE - io_tlb_offset(i);
236 io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
237 }
238 io_tlb_index = 0;
239 no_iotlb_memory = false;
240
241 if (verbose)
242 swiotlb_print_info();
243
244 swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
245 return 0;
246 }
247
248 /*
249 * Statically reserve bounce buffer space and initialize bounce buffer data
250 * structures for the software IO TLB used to implement the DMA API.
251 */
252 void __init
253 swiotlb_init(int verbose)
254 {
255 size_t default_size = IO_TLB_DEFAULT_SIZE;
256 unsigned char *vstart;
257 unsigned long bytes;
258
259 if (!io_tlb_nslabs) {
260 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
261 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
262 }
263
264 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
265
266 /* Get IO TLB memory from the low pages */
267 vstart = memblock_alloc_low(PAGE_ALIGN(bytes), PAGE_SIZE);
268 if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose))
269 return;
270
271 if (io_tlb_start) {
272 memblock_free_early(io_tlb_start,
273 PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
274 io_tlb_start = 0;
275 }
276 pr_warn("Cannot allocate buffer");
277 no_iotlb_memory = true;
278 }
279
280 /*
281 * Systems with larger DMA zones (those that don't support ISA) can
282 * initialize the swiotlb later using the slab allocator if needed.
283 * This should be just like above, but with some error catching.
284 */
285 int
286 swiotlb_late_init_with_default_size(size_t default_size)
287 {
288 unsigned long bytes, req_nslabs = io_tlb_nslabs;
289 unsigned char *vstart = NULL;
290 unsigned int order;
291 int rc = 0;
292
293 if (!io_tlb_nslabs) {
294 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
295 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
296 }
297
298 /*
299 * Get IO TLB memory from the low pages
300 */
301 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
302 io_tlb_nslabs = SLABS_PER_PAGE << order;
303 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
304
305 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
306 vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
307 order);
308 if (vstart)
309 break;
310 order--;
311 }
312
313 if (!vstart) {
314 io_tlb_nslabs = req_nslabs;
315 return -ENOMEM;
316 }
317 if (order != get_order(bytes)) {
318 pr_warn("only able to allocate %ld MB\n",
319 (PAGE_SIZE << order) >> 20);
320 io_tlb_nslabs = SLABS_PER_PAGE << order;
321 }
322 rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs);
323 if (rc)
324 free_pages((unsigned long)vstart, order);
325
326 return rc;
327 }
328
329 static void swiotlb_cleanup(void)
330 {
331 io_tlb_end = 0;
332 io_tlb_start = 0;
333 io_tlb_nslabs = 0;
334 max_segment = 0;
335 }
336
337 int
338 swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
339 {
340 unsigned long i, bytes;
341
342 bytes = nslabs << IO_TLB_SHIFT;
343
344 io_tlb_nslabs = nslabs;
345 io_tlb_start = virt_to_phys(tlb);
346 io_tlb_end = io_tlb_start + bytes;
347
348 set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT);
349 memset(tlb, 0, bytes);
350
351 /*
352 * Allocate and initialize the free list array. This array is used
353 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
354 * between io_tlb_start and io_tlb_end.
355 */
356 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
357 get_order(io_tlb_nslabs * sizeof(int)));
358 if (!io_tlb_list)
359 goto cleanup3;
360
361 io_tlb_orig_addr = (phys_addr_t *)
362 __get_free_pages(GFP_KERNEL,
363 get_order(io_tlb_nslabs *
364 sizeof(phys_addr_t)));
365 if (!io_tlb_orig_addr)
366 goto cleanup4;
367
368 for (i = 0; i < io_tlb_nslabs; i++) {
369 io_tlb_list[i] = IO_TLB_SEGSIZE - io_tlb_offset(i);
370 io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
371 }
372 io_tlb_index = 0;
373 no_iotlb_memory = false;
374
375 swiotlb_print_info();
376
377 late_alloc = 1;
378
379 swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
380
381 return 0;
382
383 cleanup4:
384 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
385 sizeof(int)));
386 io_tlb_list = NULL;
387 cleanup3:
388 swiotlb_cleanup();
389 return -ENOMEM;
390 }
391
392 void __init swiotlb_exit(void)
393 {
394 if (!io_tlb_orig_addr)
395 return;
396
397 if (late_alloc) {
398 free_pages((unsigned long)io_tlb_orig_addr,
399 get_order(io_tlb_nslabs * sizeof(phys_addr_t)));
400 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
401 sizeof(int)));
402 free_pages((unsigned long)phys_to_virt(io_tlb_start),
403 get_order(io_tlb_nslabs << IO_TLB_SHIFT));
404 } else {
405 memblock_free_late(__pa(io_tlb_orig_addr),
406 PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)));
407 memblock_free_late(__pa(io_tlb_list),
408 PAGE_ALIGN(io_tlb_nslabs * sizeof(int)));
409 memblock_free_late(io_tlb_start,
410 PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
411 }
412 swiotlb_cleanup();
413 }
414
415 /*
416 * Bounce: copy the swiotlb buffer from or back to the original dma location
417 */
418 static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
419 size_t size, enum dma_data_direction dir)
420 {
421 unsigned long pfn = PFN_DOWN(orig_addr);
422 unsigned char *vaddr = phys_to_virt(tlb_addr);
423
424 if (PageHighMem(pfn_to_page(pfn))) {
425 /* The buffer does not have a mapping. Map it in and copy */
426 unsigned int offset = orig_addr & ~PAGE_MASK;
427 char *buffer;
428 unsigned int sz = 0;
429 unsigned long flags;
430
431 while (size) {
432 sz = min_t(size_t, PAGE_SIZE - offset, size);
433
434 local_irq_save(flags);
435 buffer = kmap_atomic(pfn_to_page(pfn));
436 if (dir == DMA_TO_DEVICE)
437 memcpy(vaddr, buffer + offset, sz);
438 else
439 memcpy(buffer + offset, vaddr, sz);
440 kunmap_atomic(buffer);
441 local_irq_restore(flags);
442
443 size -= sz;
444 pfn++;
445 vaddr += sz;
446 offset = 0;
447 }
448 } else if (dir == DMA_TO_DEVICE) {
449 memcpy(vaddr, phys_to_virt(orig_addr), size);
450 } else {
451 memcpy(phys_to_virt(orig_addr), vaddr, size);
452 }
453 }
454
455 #define slot_addr(start, idx) ((start) + ((idx) << IO_TLB_SHIFT))
456
457 /*
458 * Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
459 */
460 static inline unsigned long get_max_slots(unsigned long boundary_mask)
461 {
462 if (boundary_mask == ~0UL)
463 return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
464 return nr_slots(boundary_mask + 1);
465 }
466
467 static unsigned int wrap_index(unsigned int index)
468 {
469 if (index >= io_tlb_nslabs)
470 return 0;
471 return index;
472 }
473
474 /*
475 * Find a suitable number of IO TLB entries size that will fit this request and
476 * allocate a buffer from that IO TLB pool.
477 */
478 static int find_slots(struct device *dev, size_t alloc_size)
479 {
480 unsigned long boundary_mask = dma_get_seg_boundary(dev);
481 dma_addr_t tbl_dma_addr =
482 phys_to_dma_unencrypted(dev, io_tlb_start) & boundary_mask;
483 unsigned long max_slots = get_max_slots(boundary_mask);
484 unsigned int nslots = nr_slots(alloc_size), stride = 1;
485 unsigned int index, wrap, count = 0, i;
486 unsigned long flags;
487
488 BUG_ON(!nslots);
489
490 /*
491 * For mappings greater than or equal to a page, we limit the stride
492 * (and hence alignment) to a page size.
493 */
494 if (alloc_size >= PAGE_SIZE)
495 stride <<= (PAGE_SHIFT - IO_TLB_SHIFT);
496
497 spin_lock_irqsave(&io_tlb_lock, flags);
498 if (unlikely(nslots > io_tlb_nslabs - io_tlb_used))
499 goto not_found;
500
501 index = wrap = wrap_index(ALIGN(io_tlb_index, stride));
502 do {
503 /*
504 * If we find a slot that indicates we have 'nslots' number of
505 * contiguous buffers, we allocate the buffers from that slot
506 * and mark the entries as '0' indicating unavailable.
507 */
508 if (!iommu_is_span_boundary(index, nslots,
509 nr_slots(tbl_dma_addr),
510 max_slots)) {
511 if (io_tlb_list[index] >= nslots)
512 goto found;
513 }
514 index = wrap_index(index + stride);
515 } while (index != wrap);
516
517 not_found:
518 spin_unlock_irqrestore(&io_tlb_lock, flags);
519 return -1;
520
521 found:
522 for (i = index; i < index + nslots; i++)
523 io_tlb_list[i] = 0;
524 for (i = index - 1;
525 io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 &&
526 io_tlb_list[i]; i--)
527 io_tlb_list[i] = ++count;
528
529 /*
530 * Update the indices to avoid searching in the next round.
531 */
532 if (index + nslots < io_tlb_nslabs)
533 io_tlb_index = index + nslots;
534 else
535 io_tlb_index = 0;
536 io_tlb_used += nslots;
537
538 spin_unlock_irqrestore(&io_tlb_lock, flags);
539 return index;
540 }
541
542 phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
543 size_t mapping_size, size_t alloc_size,
544 enum dma_data_direction dir, unsigned long attrs)
545 {
546 unsigned int index, i;
547 phys_addr_t tlb_addr;
548
549 if (no_iotlb_memory)
550 panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
551
552 if (mem_encrypt_active())
553 pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
554
555 if (mapping_size > alloc_size) {
556 dev_warn_once(dev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
557 mapping_size, alloc_size);
558 return (phys_addr_t)DMA_MAPPING_ERROR;
559 }
560
561 index = find_slots(dev, alloc_size);
562 if (index == -1) {
563 if (!(attrs & DMA_ATTR_NO_WARN))
564 dev_warn_ratelimited(dev,
565 "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n",
566 alloc_size, io_tlb_nslabs, io_tlb_used);
567 return (phys_addr_t)DMA_MAPPING_ERROR;
568 }
569
570 /*
571 * Save away the mapping from the original address to the DMA address.
572 * This is needed when we sync the memory. Then we sync the buffer if
573 * needed.
574 */
575 for (i = 0; i < nr_slots(alloc_size); i++)
576 io_tlb_orig_addr[index + i] = slot_addr(orig_addr, i);
577
578 tlb_addr = slot_addr(io_tlb_start, index);
579 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
580 (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
581 swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_TO_DEVICE);
582 return tlb_addr;
583 }
584
585 /*
586 * tlb_addr is the physical address of the bounce buffer to unmap.
587 */
588 void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
589 size_t mapping_size, size_t alloc_size,
590 enum dma_data_direction dir, unsigned long attrs)
591 {
592 unsigned long flags;
593 int i, count, nslots = nr_slots(alloc_size);
594 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
595 phys_addr_t orig_addr = io_tlb_orig_addr[index];
596
597 /*
598 * First, sync the memory before unmapping the entry
599 */
600 if (orig_addr != INVALID_PHYS_ADDR &&
601 !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
602 ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
603 swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_FROM_DEVICE);
604
605 /*
606 * Return the buffer to the free list by setting the corresponding
607 * entries to indicate the number of contiguous entries available.
608 * While returning the entries to the free list, we merge the entries
609 * with slots below and above the pool being returned.
610 */
611 spin_lock_irqsave(&io_tlb_lock, flags);
612 if (index + nslots < ALIGN(index + 1, IO_TLB_SEGSIZE))
613 count = io_tlb_list[index + nslots];
614 else
615 count = 0;
616
617 /*
618 * Step 1: return the slots to the free list, merging the slots with
619 * superceeding slots
620 */
621 for (i = index + nslots - 1; i >= index; i--) {
622 io_tlb_list[i] = ++count;
623 io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
624 }
625
626 /*
627 * Step 2: merge the returned slots with the preceding slots, if
628 * available (non zero)
629 */
630 for (i = index - 1;
631 io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && io_tlb_list[i];
632 i--)
633 io_tlb_list[i] = ++count;
634 io_tlb_used -= nslots;
635 spin_unlock_irqrestore(&io_tlb_lock, flags);
636 }
637
638 void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr,
639 size_t size, enum dma_data_direction dir,
640 enum dma_sync_target target)
641 {
642 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
643 phys_addr_t orig_addr = io_tlb_orig_addr[index];
644
645 if (orig_addr == INVALID_PHYS_ADDR)
646 return;
647 orig_addr += (unsigned long)tlb_addr & (IO_TLB_SIZE - 1);
648
649 switch (target) {
650 case SYNC_FOR_CPU:
651 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
652 swiotlb_bounce(orig_addr, tlb_addr,
653 size, DMA_FROM_DEVICE);
654 else
655 BUG_ON(dir != DMA_TO_DEVICE);
656 break;
657 case SYNC_FOR_DEVICE:
658 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
659 swiotlb_bounce(orig_addr, tlb_addr,
660 size, DMA_TO_DEVICE);
661 else
662 BUG_ON(dir != DMA_FROM_DEVICE);
663 break;
664 default:
665 BUG();
666 }
667 }
668
669 /*
670 * Create a swiotlb mapping for the buffer at @paddr, and in case of DMAing
671 * to the device copy the data into it as well.
672 */
673 dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,
674 enum dma_data_direction dir, unsigned long attrs)
675 {
676 phys_addr_t swiotlb_addr;
677 dma_addr_t dma_addr;
678
679 trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size,
680 swiotlb_force);
681
682 swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, dir,
683 attrs);
684 if (swiotlb_addr == (phys_addr_t)DMA_MAPPING_ERROR)
685 return DMA_MAPPING_ERROR;
686
687 /* Ensure that the address returned is DMA'ble */
688 dma_addr = phys_to_dma_unencrypted(dev, swiotlb_addr);
689 if (unlikely(!dma_capable(dev, dma_addr, size, true))) {
690 swiotlb_tbl_unmap_single(dev, swiotlb_addr, size, size, dir,
691 attrs | DMA_ATTR_SKIP_CPU_SYNC);
692 dev_WARN_ONCE(dev, 1,
693 "swiotlb addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
694 &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
695 return DMA_MAPPING_ERROR;
696 }
697
698 if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
699 arch_sync_dma_for_device(swiotlb_addr, size, dir);
700 return dma_addr;
701 }
702
703 size_t swiotlb_max_mapping_size(struct device *dev)
704 {
705 return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE;
706 }
707
708 bool is_swiotlb_active(void)
709 {
710 /*
711 * When SWIOTLB is initialized, even if io_tlb_start points to physical
712 * address zero, io_tlb_end surely doesn't.
713 */
714 return io_tlb_end != 0;
715 }
716
717 #ifdef CONFIG_DEBUG_FS
718
719 static int __init swiotlb_create_debugfs(void)
720 {
721 struct dentry *root;
722
723 root = debugfs_create_dir("swiotlb", NULL);
724 debugfs_create_ulong("io_tlb_nslabs", 0400, root, &io_tlb_nslabs);
725 debugfs_create_ulong("io_tlb_used", 0400, root, &io_tlb_used);
726 return 0;
727 }
728
729 late_initcall(swiotlb_create_debugfs);
730
731 #endif