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 #ifdef CONFIG_DMA_RESTRICTED_POOL
43 #include <linux/io.h>
44 #include <linux/of.h>
45 #include <linux/of_fdt.h>
46 #include <linux/of_reserved_mem.h>
47 #include <linux/slab.h>
48 #endif
49
50 #include <asm/io.h>
51 #include <asm/dma.h>
52
53 #include <linux/init.h>
54 #include <linux/memblock.h>
55 #include <linux/iommu-helper.h>
56
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/swiotlb.h>
59
60 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
61
62 /*
63 * Minimum IO TLB size to bother booting with. Systems with mainly
64 * 64bit capable cards will only lightly use the swiotlb. If we can't
65 * allocate a contiguous 1MB, we're probably in trouble anyway.
66 */
67 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
68
69 #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
70
71 enum swiotlb_force swiotlb_force;
72
73 struct io_tlb_mem io_tlb_default_mem;
74
75 /*
76 * Max segment that we can provide which (if pages are contingous) will
77 * not be bounced (unless SWIOTLB_FORCE is set).
78 */
79 static unsigned int max_segment;
80
81 static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT;
82
83 static int __init
setup_io_tlb_npages(char * str)84 setup_io_tlb_npages(char *str)
85 {
86 if (isdigit(*str)) {
87 /* avoid tail segment of size < IO_TLB_SEGSIZE */
88 default_nslabs =
89 ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE);
90 }
91 if (*str == ',')
92 ++str;
93 if (!strcmp(str, "force"))
94 swiotlb_force = SWIOTLB_FORCE;
95 else if (!strcmp(str, "noforce"))
96 swiotlb_force = SWIOTLB_NO_FORCE;
97
98 return 0;
99 }
100 early_param("swiotlb", setup_io_tlb_npages);
101
swiotlb_max_segment(void)102 unsigned int swiotlb_max_segment(void)
103 {
104 return io_tlb_default_mem.nslabs ? max_segment : 0;
105 }
106 EXPORT_SYMBOL_GPL(swiotlb_max_segment);
107
swiotlb_set_max_segment(unsigned int val)108 void swiotlb_set_max_segment(unsigned int val)
109 {
110 if (swiotlb_force == SWIOTLB_FORCE)
111 max_segment = 1;
112 else
113 max_segment = rounddown(val, PAGE_SIZE);
114 }
115
swiotlb_size_or_default(void)116 unsigned long swiotlb_size_or_default(void)
117 {
118 return default_nslabs << IO_TLB_SHIFT;
119 }
120
swiotlb_adjust_size(unsigned long size)121 void __init swiotlb_adjust_size(unsigned long size)
122 {
123 /*
124 * If swiotlb parameter has not been specified, give a chance to
125 * architectures such as those supporting memory encryption to
126 * adjust/expand SWIOTLB size for their use.
127 */
128 if (default_nslabs != IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT)
129 return;
130 size = ALIGN(size, IO_TLB_SIZE);
131 default_nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
132 pr_info("SWIOTLB bounce buffer size adjusted to %luMB", size >> 20);
133 }
134
swiotlb_print_info(void)135 void swiotlb_print_info(void)
136 {
137 struct io_tlb_mem *mem = &io_tlb_default_mem;
138
139 if (!mem->nslabs) {
140 pr_warn("No low mem\n");
141 return;
142 }
143
144 pr_info("mapped [mem %pa-%pa] (%luMB)\n", &mem->start, &mem->end,
145 (mem->nslabs << IO_TLB_SHIFT) >> 20);
146 }
147
io_tlb_offset(unsigned long val)148 static inline unsigned long io_tlb_offset(unsigned long val)
149 {
150 return val & (IO_TLB_SEGSIZE - 1);
151 }
152
nr_slots(u64 val)153 static inline unsigned long nr_slots(u64 val)
154 {
155 return DIV_ROUND_UP(val, IO_TLB_SIZE);
156 }
157
158 /*
159 * Early SWIOTLB allocation may be too early to allow an architecture to
160 * perform the desired operations. This function allows the architecture to
161 * call SWIOTLB when the operations are possible. It needs to be called
162 * before the SWIOTLB memory is used.
163 */
swiotlb_update_mem_attributes(void)164 void __init swiotlb_update_mem_attributes(void)
165 {
166 struct io_tlb_mem *mem = &io_tlb_default_mem;
167 void *vaddr;
168 unsigned long bytes;
169
170 if (!mem->nslabs || mem->late_alloc)
171 return;
172 vaddr = phys_to_virt(mem->start);
173 bytes = PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT);
174 set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
175 memset(vaddr, 0, bytes);
176 }
177
swiotlb_init_io_tlb_mem(struct io_tlb_mem * mem,phys_addr_t start,unsigned long nslabs,bool late_alloc)178 static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
179 unsigned long nslabs, bool late_alloc)
180 {
181 void *vaddr = phys_to_virt(start);
182 unsigned long bytes = nslabs << IO_TLB_SHIFT, i;
183
184 mem->nslabs = nslabs;
185 mem->start = start;
186 mem->end = mem->start + bytes;
187 mem->index = 0;
188 mem->late_alloc = late_alloc;
189
190 if (swiotlb_force == SWIOTLB_FORCE)
191 mem->force_bounce = true;
192
193 spin_lock_init(&mem->lock);
194 for (i = 0; i < mem->nslabs; i++) {
195 mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i);
196 mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
197 mem->slots[i].alloc_size = 0;
198 }
199 memset(vaddr, 0, bytes);
200 }
201
swiotlb_init_with_tbl(char * tlb,unsigned long nslabs,int verbose)202 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
203 {
204 struct io_tlb_mem *mem = &io_tlb_default_mem;
205 size_t alloc_size;
206
207 if (swiotlb_force == SWIOTLB_NO_FORCE)
208 return 0;
209
210 /* protect against double initialization */
211 if (WARN_ON_ONCE(mem->nslabs))
212 return -ENOMEM;
213
214 alloc_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), nslabs));
215 mem->slots = memblock_alloc(alloc_size, PAGE_SIZE);
216 if (!mem->slots)
217 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
218 __func__, alloc_size, PAGE_SIZE);
219
220 swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false);
221
222 if (verbose)
223 swiotlb_print_info();
224 swiotlb_set_max_segment(mem->nslabs << IO_TLB_SHIFT);
225 return 0;
226 }
227
228 /*
229 * Statically reserve bounce buffer space and initialize bounce buffer data
230 * structures for the software IO TLB used to implement the DMA API.
231 */
232 void __init
swiotlb_init(int verbose)233 swiotlb_init(int verbose)
234 {
235 size_t bytes = PAGE_ALIGN(default_nslabs << IO_TLB_SHIFT);
236 void *tlb;
237
238 if (swiotlb_force == SWIOTLB_NO_FORCE)
239 return;
240
241 /* Get IO TLB memory from the low pages */
242 tlb = memblock_alloc_low(bytes, PAGE_SIZE);
243 if (!tlb)
244 goto fail;
245 if (swiotlb_init_with_tbl(tlb, default_nslabs, verbose))
246 goto fail_free_mem;
247 return;
248
249 fail_free_mem:
250 memblock_free_early(__pa(tlb), bytes);
251 fail:
252 pr_warn("Cannot allocate buffer");
253 }
254
255 /*
256 * Systems with larger DMA zones (those that don't support ISA) can
257 * initialize the swiotlb later using the slab allocator if needed.
258 * This should be just like above, but with some error catching.
259 */
260 int
swiotlb_late_init_with_default_size(size_t default_size)261 swiotlb_late_init_with_default_size(size_t default_size)
262 {
263 unsigned long nslabs =
264 ALIGN(default_size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
265 unsigned long bytes;
266 unsigned char *vstart = NULL;
267 unsigned int order;
268 int rc = 0;
269
270 if (swiotlb_force == SWIOTLB_NO_FORCE)
271 return 0;
272
273 /*
274 * Get IO TLB memory from the low pages
275 */
276 order = get_order(nslabs << IO_TLB_SHIFT);
277 nslabs = SLABS_PER_PAGE << order;
278 bytes = nslabs << IO_TLB_SHIFT;
279
280 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
281 vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
282 order);
283 if (vstart)
284 break;
285 order--;
286 }
287
288 if (!vstart)
289 return -ENOMEM;
290
291 if (order != get_order(bytes)) {
292 pr_warn("only able to allocate %ld MB\n",
293 (PAGE_SIZE << order) >> 20);
294 nslabs = SLABS_PER_PAGE << order;
295 }
296 rc = swiotlb_late_init_with_tbl(vstart, nslabs);
297 if (rc)
298 free_pages((unsigned long)vstart, order);
299
300 return rc;
301 }
302
303 int
swiotlb_late_init_with_tbl(char * tlb,unsigned long nslabs)304 swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
305 {
306 struct io_tlb_mem *mem = &io_tlb_default_mem;
307 unsigned long bytes = nslabs << IO_TLB_SHIFT;
308
309 if (swiotlb_force == SWIOTLB_NO_FORCE)
310 return 0;
311
312 /* protect against double initialization */
313 if (WARN_ON_ONCE(mem->nslabs))
314 return -ENOMEM;
315
316 mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
317 get_order(array_size(sizeof(*mem->slots), nslabs)));
318 if (!mem->slots)
319 return -ENOMEM;
320
321 set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT);
322 swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true);
323
324 swiotlb_print_info();
325 swiotlb_set_max_segment(mem->nslabs << IO_TLB_SHIFT);
326 return 0;
327 }
328
swiotlb_exit(void)329 void __init swiotlb_exit(void)
330 {
331 struct io_tlb_mem *mem = &io_tlb_default_mem;
332 unsigned long tbl_vaddr;
333 size_t tbl_size, slots_size;
334
335 if (!mem->nslabs)
336 return;
337
338 pr_info("tearing down default memory pool\n");
339 tbl_vaddr = (unsigned long)phys_to_virt(mem->start);
340 tbl_size = PAGE_ALIGN(mem->end - mem->start);
341 slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs));
342
343 set_memory_encrypted(tbl_vaddr, tbl_size >> PAGE_SHIFT);
344 if (mem->late_alloc) {
345 free_pages(tbl_vaddr, get_order(tbl_size));
346 free_pages((unsigned long)mem->slots, get_order(slots_size));
347 } else {
348 memblock_free_late(mem->start, tbl_size);
349 memblock_free_late(__pa(mem->slots), slots_size);
350 }
351
352 memset(mem, 0, sizeof(*mem));
353 }
354
355 /*
356 * Return the offset into a iotlb slot required to keep the device happy.
357 */
swiotlb_align_offset(struct device * dev,u64 addr)358 static unsigned int swiotlb_align_offset(struct device *dev, u64 addr)
359 {
360 return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1);
361 }
362
363 /*
364 * Bounce: copy the swiotlb buffer from or back to the original dma location
365 */
swiotlb_bounce(struct device * dev,phys_addr_t tlb_addr,size_t size,enum dma_data_direction dir)366 static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size,
367 enum dma_data_direction dir)
368 {
369 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
370 int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT;
371 phys_addr_t orig_addr = mem->slots[index].orig_addr;
372 size_t alloc_size = mem->slots[index].alloc_size;
373 unsigned long pfn = PFN_DOWN(orig_addr);
374 unsigned char *vaddr = phys_to_virt(tlb_addr);
375 unsigned int tlb_offset, orig_addr_offset;
376
377 if (orig_addr == INVALID_PHYS_ADDR)
378 return;
379
380 tlb_offset = tlb_addr & (IO_TLB_SIZE - 1);
381 orig_addr_offset = swiotlb_align_offset(dev, orig_addr);
382 if (tlb_offset < orig_addr_offset) {
383 dev_WARN_ONCE(dev, 1,
384 "Access before mapping start detected. orig offset %u, requested offset %u.\n",
385 orig_addr_offset, tlb_offset);
386 return;
387 }
388
389 tlb_offset -= orig_addr_offset;
390 if (tlb_offset > alloc_size) {
391 dev_WARN_ONCE(dev, 1,
392 "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu+%u.\n",
393 alloc_size, size, tlb_offset);
394 return;
395 }
396
397 orig_addr += tlb_offset;
398 alloc_size -= tlb_offset;
399
400 if (size > alloc_size) {
401 dev_WARN_ONCE(dev, 1,
402 "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu.\n",
403 alloc_size, size);
404 size = alloc_size;
405 }
406
407 if (PageHighMem(pfn_to_page(pfn))) {
408 /* The buffer does not have a mapping. Map it in and copy */
409 unsigned int offset = orig_addr & ~PAGE_MASK;
410 char *buffer;
411 unsigned int sz = 0;
412 unsigned long flags;
413
414 while (size) {
415 sz = min_t(size_t, PAGE_SIZE - offset, size);
416
417 local_irq_save(flags);
418 buffer = kmap_atomic(pfn_to_page(pfn));
419 if (dir == DMA_TO_DEVICE)
420 memcpy(vaddr, buffer + offset, sz);
421 else
422 memcpy(buffer + offset, vaddr, sz);
423 kunmap_atomic(buffer);
424 local_irq_restore(flags);
425
426 size -= sz;
427 pfn++;
428 vaddr += sz;
429 offset = 0;
430 }
431 } else if (dir == DMA_TO_DEVICE) {
432 memcpy(vaddr, phys_to_virt(orig_addr), size);
433 } else {
434 memcpy(phys_to_virt(orig_addr), vaddr, size);
435 }
436 }
437
slot_addr(phys_addr_t start,phys_addr_t idx)438 static inline phys_addr_t slot_addr(phys_addr_t start, phys_addr_t idx)
439 {
440 return start + (idx << IO_TLB_SHIFT);
441 }
442
443 /*
444 * Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
445 */
get_max_slots(unsigned long boundary_mask)446 static inline unsigned long get_max_slots(unsigned long boundary_mask)
447 {
448 if (boundary_mask == ~0UL)
449 return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
450 return nr_slots(boundary_mask + 1);
451 }
452
wrap_index(struct io_tlb_mem * mem,unsigned int index)453 static unsigned int wrap_index(struct io_tlb_mem *mem, unsigned int index)
454 {
455 if (index >= mem->nslabs)
456 return 0;
457 return index;
458 }
459
460 /*
461 * Find a suitable number of IO TLB entries size that will fit this request and
462 * allocate a buffer from that IO TLB pool.
463 */
swiotlb_find_slots(struct device * dev,phys_addr_t orig_addr,size_t alloc_size,unsigned int alloc_align_mask)464 static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
465 size_t alloc_size, unsigned int alloc_align_mask)
466 {
467 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
468 unsigned long boundary_mask = dma_get_seg_boundary(dev);
469 dma_addr_t tbl_dma_addr =
470 phys_to_dma_unencrypted(dev, mem->start) & boundary_mask;
471 unsigned long max_slots = get_max_slots(boundary_mask);
472 unsigned int iotlb_align_mask =
473 dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1);
474 unsigned int nslots = nr_slots(alloc_size), stride;
475 unsigned int index, wrap, count = 0, i;
476 unsigned int offset = swiotlb_align_offset(dev, orig_addr);
477 unsigned long flags;
478
479 BUG_ON(!nslots);
480
481 /*
482 * For mappings with an alignment requirement don't bother looping to
483 * unaligned slots once we found an aligned one. For allocations of
484 * PAGE_SIZE or larger only look for page aligned allocations.
485 */
486 stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1;
487 if (alloc_size >= PAGE_SIZE)
488 stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT));
489 stride = max(stride, (alloc_align_mask >> IO_TLB_SHIFT) + 1);
490
491 spin_lock_irqsave(&mem->lock, flags);
492 if (unlikely(nslots > mem->nslabs - mem->used))
493 goto not_found;
494
495 index = wrap = wrap_index(mem, ALIGN(mem->index, stride));
496 do {
497 if (orig_addr &&
498 (slot_addr(tbl_dma_addr, index) & iotlb_align_mask) !=
499 (orig_addr & iotlb_align_mask)) {
500 index = wrap_index(mem, index + 1);
501 continue;
502 }
503
504 /*
505 * If we find a slot that indicates we have 'nslots' number of
506 * contiguous buffers, we allocate the buffers from that slot
507 * and mark the entries as '0' indicating unavailable.
508 */
509 if (!iommu_is_span_boundary(index, nslots,
510 nr_slots(tbl_dma_addr),
511 max_slots)) {
512 if (mem->slots[index].list >= nslots)
513 goto found;
514 }
515 index = wrap_index(mem, index + stride);
516 } while (index != wrap);
517
518 not_found:
519 spin_unlock_irqrestore(&mem->lock, flags);
520 return -1;
521
522 found:
523 for (i = index; i < index + nslots; i++) {
524 mem->slots[i].list = 0;
525 mem->slots[i].alloc_size =
526 alloc_size - (offset + ((i - index) << IO_TLB_SHIFT));
527 }
528 for (i = index - 1;
529 io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 &&
530 mem->slots[i].list; i--)
531 mem->slots[i].list = ++count;
532
533 /*
534 * Update the indices to avoid searching in the next round.
535 */
536 if (index + nslots < mem->nslabs)
537 mem->index = index + nslots;
538 else
539 mem->index = 0;
540 mem->used += nslots;
541
542 spin_unlock_irqrestore(&mem->lock, flags);
543 return index;
544 }
545
swiotlb_tbl_map_single(struct device * dev,phys_addr_t orig_addr,size_t mapping_size,size_t alloc_size,unsigned int alloc_align_mask,enum dma_data_direction dir,unsigned long attrs)546 phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
547 size_t mapping_size, size_t alloc_size,
548 unsigned int alloc_align_mask, enum dma_data_direction dir,
549 unsigned long attrs)
550 {
551 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
552 unsigned int offset = swiotlb_align_offset(dev, orig_addr);
553 unsigned int i;
554 int index;
555 phys_addr_t tlb_addr;
556
557 if (!mem || !mem->nslabs)
558 panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
559
560 if (mem_encrypt_active())
561 pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
562
563 if (mapping_size > alloc_size) {
564 dev_warn_once(dev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
565 mapping_size, alloc_size);
566 return (phys_addr_t)DMA_MAPPING_ERROR;
567 }
568
569 index = swiotlb_find_slots(dev, orig_addr,
570 alloc_size + offset, alloc_align_mask);
571 if (index == -1) {
572 if (!(attrs & DMA_ATTR_NO_WARN))
573 dev_warn_ratelimited(dev,
574 "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n",
575 alloc_size, mem->nslabs, mem->used);
576 return (phys_addr_t)DMA_MAPPING_ERROR;
577 }
578
579 /*
580 * Save away the mapping from the original address to the DMA address.
581 * This is needed when we sync the memory. Then we sync the buffer if
582 * needed.
583 */
584 for (i = 0; i < nr_slots(alloc_size + offset); i++)
585 mem->slots[index + i].orig_addr = slot_addr(orig_addr, i);
586 tlb_addr = slot_addr(mem->start, index) + offset;
587 /*
588 * When dir == DMA_FROM_DEVICE we could omit the copy from the orig
589 * to the tlb buffer, if we knew for sure the device will
590 * overwirte the entire current content. But we don't. Thus
591 * unconditional bounce may prevent leaking swiotlb content (i.e.
592 * kernel memory) to user-space.
593 */
594 swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE);
595 return tlb_addr;
596 }
597
swiotlb_release_slots(struct device * dev,phys_addr_t tlb_addr)598 static void swiotlb_release_slots(struct device *dev, phys_addr_t tlb_addr)
599 {
600 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
601 unsigned long flags;
602 unsigned int offset = swiotlb_align_offset(dev, tlb_addr);
603 int index = (tlb_addr - offset - mem->start) >> IO_TLB_SHIFT;
604 int nslots = nr_slots(mem->slots[index].alloc_size + offset);
605 int count, i;
606
607 /*
608 * Return the buffer to the free list by setting the corresponding
609 * entries to indicate the number of contiguous entries available.
610 * While returning the entries to the free list, we merge the entries
611 * with slots below and above the pool being returned.
612 */
613 spin_lock_irqsave(&mem->lock, flags);
614 if (index + nslots < ALIGN(index + 1, IO_TLB_SEGSIZE))
615 count = mem->slots[index + nslots].list;
616 else
617 count = 0;
618
619 /*
620 * Step 1: return the slots to the free list, merging the slots with
621 * superceeding slots
622 */
623 for (i = index + nslots - 1; i >= index; i--) {
624 mem->slots[i].list = ++count;
625 mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
626 mem->slots[i].alloc_size = 0;
627 }
628
629 /*
630 * Step 2: merge the returned slots with the preceding slots, if
631 * available (non zero)
632 */
633 for (i = index - 1;
634 io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && mem->slots[i].list;
635 i--)
636 mem->slots[i].list = ++count;
637 mem->used -= nslots;
638 spin_unlock_irqrestore(&mem->lock, flags);
639 }
640
641 /*
642 * tlb_addr is the physical address of the bounce buffer to unmap.
643 */
swiotlb_tbl_unmap_single(struct device * dev,phys_addr_t tlb_addr,size_t mapping_size,enum dma_data_direction dir,unsigned long attrs)644 void swiotlb_tbl_unmap_single(struct device *dev, phys_addr_t tlb_addr,
645 size_t mapping_size, enum dma_data_direction dir,
646 unsigned long attrs)
647 {
648 /*
649 * First, sync the memory before unmapping the entry
650 */
651 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
652 (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
653 swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_FROM_DEVICE);
654
655 swiotlb_release_slots(dev, tlb_addr);
656 }
657
swiotlb_sync_single_for_device(struct device * dev,phys_addr_t tlb_addr,size_t size,enum dma_data_direction dir)658 void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
659 size_t size, enum dma_data_direction dir)
660 {
661 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
662 swiotlb_bounce(dev, tlb_addr, size, DMA_TO_DEVICE);
663 else
664 BUG_ON(dir != DMA_FROM_DEVICE);
665 }
666
swiotlb_sync_single_for_cpu(struct device * dev,phys_addr_t tlb_addr,size_t size,enum dma_data_direction dir)667 void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
668 size_t size, enum dma_data_direction dir)
669 {
670 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
671 swiotlb_bounce(dev, tlb_addr, size, DMA_FROM_DEVICE);
672 else
673 BUG_ON(dir != DMA_TO_DEVICE);
674 }
675
676 /*
677 * Create a swiotlb mapping for the buffer at @paddr, and in case of DMAing
678 * to the device copy the data into it as well.
679 */
swiotlb_map(struct device * dev,phys_addr_t paddr,size_t size,enum dma_data_direction dir,unsigned long attrs)680 dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,
681 enum dma_data_direction dir, unsigned long attrs)
682 {
683 phys_addr_t swiotlb_addr;
684 dma_addr_t dma_addr;
685
686 trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size,
687 swiotlb_force);
688
689 swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir,
690 attrs);
691 if (swiotlb_addr == (phys_addr_t)DMA_MAPPING_ERROR)
692 return DMA_MAPPING_ERROR;
693
694 /* Ensure that the address returned is DMA'ble */
695 dma_addr = phys_to_dma_unencrypted(dev, swiotlb_addr);
696 if (unlikely(!dma_capable(dev, dma_addr, size, true))) {
697 swiotlb_tbl_unmap_single(dev, swiotlb_addr, size, dir,
698 attrs | DMA_ATTR_SKIP_CPU_SYNC);
699 dev_WARN_ONCE(dev, 1,
700 "swiotlb addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
701 &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
702 return DMA_MAPPING_ERROR;
703 }
704
705 if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
706 arch_sync_dma_for_device(swiotlb_addr, size, dir);
707 return dma_addr;
708 }
709
swiotlb_max_mapping_size(struct device * dev)710 size_t swiotlb_max_mapping_size(struct device *dev)
711 {
712 int min_align_mask = dma_get_min_align_mask(dev);
713 int min_align = 0;
714
715 /*
716 * swiotlb_find_slots() skips slots according to
717 * min align mask. This affects max mapping size.
718 * Take it into acount here.
719 */
720 if (min_align_mask)
721 min_align = roundup(min_align_mask, IO_TLB_SIZE);
722
723 return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE - min_align;
724 }
725
is_swiotlb_active(struct device * dev)726 bool is_swiotlb_active(struct device *dev)
727 {
728 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
729
730 return mem && mem->nslabs;
731 }
732 EXPORT_SYMBOL_GPL(is_swiotlb_active);
733
734 #ifdef CONFIG_DEBUG_FS
735 static struct dentry *debugfs_dir;
736
swiotlb_create_debugfs_files(struct io_tlb_mem * mem)737 static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem)
738 {
739 debugfs_create_ulong("io_tlb_nslabs", 0400, mem->debugfs, &mem->nslabs);
740 debugfs_create_ulong("io_tlb_used", 0400, mem->debugfs, &mem->used);
741 }
742
swiotlb_create_default_debugfs(void)743 static int __init swiotlb_create_default_debugfs(void)
744 {
745 struct io_tlb_mem *mem = &io_tlb_default_mem;
746
747 debugfs_dir = debugfs_create_dir("swiotlb", NULL);
748 if (mem->nslabs) {
749 mem->debugfs = debugfs_dir;
750 swiotlb_create_debugfs_files(mem);
751 }
752 return 0;
753 }
754
755 late_initcall(swiotlb_create_default_debugfs);
756
757 #endif
758
759 #ifdef CONFIG_DMA_RESTRICTED_POOL
760
761 #ifdef CONFIG_DEBUG_FS
rmem_swiotlb_debugfs_init(struct reserved_mem * rmem)762 static void rmem_swiotlb_debugfs_init(struct reserved_mem *rmem)
763 {
764 struct io_tlb_mem *mem = rmem->priv;
765
766 mem->debugfs = debugfs_create_dir(rmem->name, debugfs_dir);
767 swiotlb_create_debugfs_files(mem);
768 }
769 #else
rmem_swiotlb_debugfs_init(struct reserved_mem * rmem)770 static void rmem_swiotlb_debugfs_init(struct reserved_mem *rmem)
771 {
772 }
773 #endif
774
swiotlb_alloc(struct device * dev,size_t size)775 struct page *swiotlb_alloc(struct device *dev, size_t size)
776 {
777 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
778 phys_addr_t tlb_addr;
779 int index;
780
781 if (!mem)
782 return NULL;
783
784 index = swiotlb_find_slots(dev, 0, size, 0);
785 if (index == -1)
786 return NULL;
787
788 tlb_addr = slot_addr(mem->start, index);
789
790 return pfn_to_page(PFN_DOWN(tlb_addr));
791 }
792
swiotlb_free(struct device * dev,struct page * page,size_t size)793 bool swiotlb_free(struct device *dev, struct page *page, size_t size)
794 {
795 phys_addr_t tlb_addr = page_to_phys(page);
796
797 if (!is_swiotlb_buffer(dev, tlb_addr))
798 return false;
799
800 swiotlb_release_slots(dev, tlb_addr);
801
802 return true;
803 }
804
rmem_swiotlb_device_init(struct reserved_mem * rmem,struct device * dev)805 static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
806 struct device *dev)
807 {
808 struct io_tlb_mem *mem = rmem->priv;
809 unsigned long nslabs = rmem->size >> IO_TLB_SHIFT;
810
811 /*
812 * Since multiple devices can share the same pool, the private data,
813 * io_tlb_mem struct, will be initialized by the first device attached
814 * to it.
815 */
816 if (!mem) {
817 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
818 if (!mem)
819 return -ENOMEM;
820
821 mem->slots = kzalloc(array_size(sizeof(*mem->slots), nslabs),
822 GFP_KERNEL);
823 if (!mem->slots) {
824 kfree(mem);
825 return -ENOMEM;
826 }
827
828 set_memory_decrypted((unsigned long)phys_to_virt(rmem->base),
829 rmem->size >> PAGE_SHIFT);
830 swiotlb_init_io_tlb_mem(mem, rmem->base, nslabs, false);
831 mem->force_bounce = true;
832 mem->for_alloc = true;
833
834 rmem->priv = mem;
835
836 rmem_swiotlb_debugfs_init(rmem);
837 }
838
839 dev->dma_io_tlb_mem = mem;
840
841 return 0;
842 }
843
rmem_swiotlb_device_release(struct reserved_mem * rmem,struct device * dev)844 static void rmem_swiotlb_device_release(struct reserved_mem *rmem,
845 struct device *dev)
846 {
847 dev->dma_io_tlb_mem = &io_tlb_default_mem;
848 }
849
850 static const struct reserved_mem_ops rmem_swiotlb_ops = {
851 .device_init = rmem_swiotlb_device_init,
852 .device_release = rmem_swiotlb_device_release,
853 };
854
rmem_swiotlb_setup(struct reserved_mem * rmem)855 static int __init rmem_swiotlb_setup(struct reserved_mem *rmem)
856 {
857 unsigned long node = rmem->fdt_node;
858
859 if (of_get_flat_dt_prop(node, "reusable", NULL) ||
860 of_get_flat_dt_prop(node, "linux,cma-default", NULL) ||
861 of_get_flat_dt_prop(node, "linux,dma-default", NULL) ||
862 of_get_flat_dt_prop(node, "no-map", NULL))
863 return -EINVAL;
864
865 if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) {
866 pr_err("Restricted DMA pool must be accessible within the linear mapping.");
867 return -EINVAL;
868 }
869
870 rmem->ops = &rmem_swiotlb_ops;
871 pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld MiB\n",
872 &rmem->base, (unsigned long)rmem->size / SZ_1M);
873 return 0;
874 }
875
876 RESERVEDMEM_OF_DECLARE(dma, "restricted-dma-pool", rmem_swiotlb_setup);
877 #endif /* CONFIG_DMA_RESTRICTED_POOL */
878