1 // SPDX-License-Identifier: GPL-2.0-only
3 * Dynamic DMA mapping support.
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>
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
21 #define pr_fmt(fmt) "software IO TLB: " fmt
23 #include <linux/cache.h>
24 #include <linux/cc_platform.h>
25 #include <linux/ctype.h>
26 #include <linux/debugfs.h>
27 #include <linux/dma-direct.h>
28 #include <linux/dma-map-ops.h>
29 #include <linux/export.h>
30 #include <linux/gfp.h>
31 #include <linux/highmem.h>
33 #include <linux/iommu-helper.h>
34 #include <linux/init.h>
35 #include <linux/memblock.h>
37 #include <linux/pfn.h>
38 #include <linux/scatterlist.h>
39 #include <linux/set_memory.h>
40 #include <linux/spinlock.h>
41 #include <linux/string.h>
42 #include <linux/swiotlb.h>
43 #include <linux/types.h>
44 #ifdef CONFIG_DMA_RESTRICTED_POOL
46 #include <linux/of_fdt.h>
47 #include <linux/of_reserved_mem.h>
48 #include <linux/slab.h>
51 #define CREATE_TRACE_POINTS
52 #include <trace/events/swiotlb.h>
54 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
57 * Minimum IO TLB size to bother booting with. Systems with mainly
58 * 64bit capable cards will only lightly use the swiotlb. If we can't
59 * allocate a contiguous 1MB, we're probably in trouble anyway.
61 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
63 #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
65 static bool swiotlb_force_bounce;
66 static bool swiotlb_force_disable;
68 struct io_tlb_mem io_tlb_default_mem;
70 phys_addr_t swiotlb_unencrypted_base;
72 static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT;
75 setup_io_tlb_npages(char *str)
78 /* avoid tail segment of size < IO_TLB_SEGSIZE */
80 ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE);
84 if (!strcmp(str, "force"))
85 swiotlb_force_bounce = true;
86 else if (!strcmp(str, "noforce"))
87 swiotlb_force_disable = true;
91 early_param("swiotlb", setup_io_tlb_npages);
93 unsigned int swiotlb_max_segment(void)
95 if (!io_tlb_default_mem.nslabs)
97 return rounddown(io_tlb_default_mem.nslabs << IO_TLB_SHIFT, PAGE_SIZE);
99 EXPORT_SYMBOL_GPL(swiotlb_max_segment);
101 unsigned long swiotlb_size_or_default(void)
103 return default_nslabs << IO_TLB_SHIFT;
106 void __init swiotlb_adjust_size(unsigned long size)
109 * If swiotlb parameter has not been specified, give a chance to
110 * architectures such as those supporting memory encryption to
111 * adjust/expand SWIOTLB size for their use.
113 if (default_nslabs != IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT)
115 size = ALIGN(size, IO_TLB_SIZE);
116 default_nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
117 pr_info("SWIOTLB bounce buffer size adjusted to %luMB", size >> 20);
120 void swiotlb_print_info(void)
122 struct io_tlb_mem *mem = &io_tlb_default_mem;
125 pr_warn("No low mem\n");
129 pr_info("mapped [mem %pa-%pa] (%luMB)\n", &mem->start, &mem->end,
130 (mem->nslabs << IO_TLB_SHIFT) >> 20);
133 static inline unsigned long io_tlb_offset(unsigned long val)
135 return val & (IO_TLB_SEGSIZE - 1);
138 static inline unsigned long nr_slots(u64 val)
140 return DIV_ROUND_UP(val, IO_TLB_SIZE);
144 * Remap swioltb memory in the unencrypted physical address space
145 * when swiotlb_unencrypted_base is set. (e.g. for Hyper-V AMD SEV-SNP
148 #ifdef CONFIG_HAS_IOMEM
149 static void *swiotlb_mem_remap(struct io_tlb_mem *mem, unsigned long bytes)
153 if (swiotlb_unencrypted_base) {
154 phys_addr_t paddr = mem->start + swiotlb_unencrypted_base;
156 vaddr = memremap(paddr, bytes, MEMREMAP_WB);
158 pr_err("Failed to map the unencrypted memory %pa size %lx.\n",
165 static void *swiotlb_mem_remap(struct io_tlb_mem *mem, unsigned long bytes)
172 * Early SWIOTLB allocation may be too early to allow an architecture to
173 * perform the desired operations. This function allows the architecture to
174 * call SWIOTLB when the operations are possible. It needs to be called
175 * before the SWIOTLB memory is used.
177 void __init swiotlb_update_mem_attributes(void)
179 struct io_tlb_mem *mem = &io_tlb_default_mem;
183 if (!mem->nslabs || mem->late_alloc)
185 vaddr = phys_to_virt(mem->start);
186 bytes = PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT);
187 set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
189 mem->vaddr = swiotlb_mem_remap(mem, bytes);
194 static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
195 unsigned long nslabs, bool late_alloc)
197 void *vaddr = phys_to_virt(start);
198 unsigned long bytes = nslabs << IO_TLB_SHIFT, i;
200 mem->nslabs = nslabs;
202 mem->end = mem->start + bytes;
204 mem->late_alloc = late_alloc;
206 if (swiotlb_force_bounce)
207 mem->force_bounce = true;
209 spin_lock_init(&mem->lock);
210 for (i = 0; i < mem->nslabs; i++) {
211 mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i);
212 mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
213 mem->slots[i].alloc_size = 0;
217 * If swiotlb_unencrypted_base is set, the bounce buffer memory will
218 * be remapped and cleared in swiotlb_update_mem_attributes.
220 if (swiotlb_unencrypted_base)
223 memset(vaddr, 0, bytes);
229 * Statically reserve bounce buffer space and initialize bounce buffer data
230 * structures for the software IO TLB used to implement the DMA API.
232 void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,
233 int (*remap)(void *tlb, unsigned long nslabs))
235 struct io_tlb_mem *mem = &io_tlb_default_mem;
236 unsigned long nslabs = default_nslabs;
241 if (!addressing_limit && !swiotlb_force_bounce)
243 if (swiotlb_force_disable)
247 * By default allocate the bounce buffer memory from low memory, but
248 * allow to pick a location everywhere for hypervisors with guest
252 bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT);
253 if (flags & SWIOTLB_ANY)
254 tlb = memblock_alloc(bytes, PAGE_SIZE);
256 tlb = memblock_alloc_low(bytes, PAGE_SIZE);
258 pr_warn("%s: failed to allocate tlb structure\n", __func__);
262 if (remap && remap(tlb, nslabs) < 0) {
263 memblock_free(tlb, PAGE_ALIGN(bytes));
265 nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
266 if (nslabs < IO_TLB_MIN_SLABS)
267 panic("%s: Failed to remap %zu bytes\n",
272 alloc_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), nslabs));
273 mem->slots = memblock_alloc(alloc_size, PAGE_SIZE);
275 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
276 __func__, alloc_size, PAGE_SIZE);
278 swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false);
279 mem->force_bounce = flags & SWIOTLB_FORCE;
281 if (flags & SWIOTLB_VERBOSE)
282 swiotlb_print_info();
285 void __init swiotlb_init(bool addressing_limit, unsigned int flags)
287 return swiotlb_init_remap(addressing_limit, flags, NULL);
291 * Systems with larger DMA zones (those that don't support ISA) can
292 * initialize the swiotlb later using the slab allocator if needed.
293 * This should be just like above, but with some error catching.
295 int swiotlb_init_late(size_t size, gfp_t gfp_mask,
296 int (*remap)(void *tlb, unsigned long nslabs))
298 struct io_tlb_mem *mem = &io_tlb_default_mem;
299 unsigned long nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
300 unsigned char *vstart = NULL;
302 bool retried = false;
305 if (swiotlb_force_disable)
309 order = get_order(nslabs << IO_TLB_SHIFT);
310 nslabs = SLABS_PER_PAGE << order;
312 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
313 vstart = (void *)__get_free_pages(gfp_mask | __GFP_NOWARN,
318 nslabs = SLABS_PER_PAGE << order;
326 rc = remap(vstart, nslabs);
328 free_pages((unsigned long)vstart, order);
330 nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
331 if (nslabs < IO_TLB_MIN_SLABS)
338 pr_warn("only able to allocate %ld MB\n",
339 (PAGE_SIZE << order) >> 20);
342 mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
343 get_order(array_size(sizeof(*mem->slots), nslabs)));
345 free_pages((unsigned long)vstart, order);
349 set_memory_decrypted((unsigned long)vstart,
350 (nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT);
351 swiotlb_init_io_tlb_mem(mem, virt_to_phys(vstart), nslabs, true);
353 swiotlb_print_info();
357 void __init swiotlb_exit(void)
359 struct io_tlb_mem *mem = &io_tlb_default_mem;
360 unsigned long tbl_vaddr;
361 size_t tbl_size, slots_size;
363 if (swiotlb_force_bounce)
369 pr_info("tearing down default memory pool\n");
370 tbl_vaddr = (unsigned long)phys_to_virt(mem->start);
371 tbl_size = PAGE_ALIGN(mem->end - mem->start);
372 slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs));
374 set_memory_encrypted(tbl_vaddr, tbl_size >> PAGE_SHIFT);
375 if (mem->late_alloc) {
376 free_pages(tbl_vaddr, get_order(tbl_size));
377 free_pages((unsigned long)mem->slots, get_order(slots_size));
379 memblock_free_late(mem->start, tbl_size);
380 memblock_free_late(__pa(mem->slots), slots_size);
383 memset(mem, 0, sizeof(*mem));
387 * Return the offset into a iotlb slot required to keep the device happy.
389 static unsigned int swiotlb_align_offset(struct device *dev, u64 addr)
391 return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1);
395 * Bounce: copy the swiotlb buffer from or back to the original dma location
397 static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size,
398 enum dma_data_direction dir)
400 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
401 int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT;
402 phys_addr_t orig_addr = mem->slots[index].orig_addr;
403 size_t alloc_size = mem->slots[index].alloc_size;
404 unsigned long pfn = PFN_DOWN(orig_addr);
405 unsigned char *vaddr = mem->vaddr + tlb_addr - mem->start;
406 unsigned int tlb_offset, orig_addr_offset;
408 if (orig_addr == INVALID_PHYS_ADDR)
411 tlb_offset = tlb_addr & (IO_TLB_SIZE - 1);
412 orig_addr_offset = swiotlb_align_offset(dev, orig_addr);
413 if (tlb_offset < orig_addr_offset) {
414 dev_WARN_ONCE(dev, 1,
415 "Access before mapping start detected. orig offset %u, requested offset %u.\n",
416 orig_addr_offset, tlb_offset);
420 tlb_offset -= orig_addr_offset;
421 if (tlb_offset > alloc_size) {
422 dev_WARN_ONCE(dev, 1,
423 "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu+%u.\n",
424 alloc_size, size, tlb_offset);
428 orig_addr += tlb_offset;
429 alloc_size -= tlb_offset;
431 if (size > alloc_size) {
432 dev_WARN_ONCE(dev, 1,
433 "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu.\n",
438 if (PageHighMem(pfn_to_page(pfn))) {
439 /* The buffer does not have a mapping. Map it in and copy */
440 unsigned int offset = orig_addr & ~PAGE_MASK;
446 sz = min_t(size_t, PAGE_SIZE - offset, size);
448 local_irq_save(flags);
449 buffer = kmap_atomic(pfn_to_page(pfn));
450 if (dir == DMA_TO_DEVICE)
451 memcpy(vaddr, buffer + offset, sz);
453 memcpy(buffer + offset, vaddr, sz);
454 kunmap_atomic(buffer);
455 local_irq_restore(flags);
462 } else if (dir == DMA_TO_DEVICE) {
463 memcpy(vaddr, phys_to_virt(orig_addr), size);
465 memcpy(phys_to_virt(orig_addr), vaddr, size);
469 #define slot_addr(start, idx) ((start) + ((idx) << IO_TLB_SHIFT))
472 * Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
474 static inline unsigned long get_max_slots(unsigned long boundary_mask)
476 if (boundary_mask == ~0UL)
477 return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
478 return nr_slots(boundary_mask + 1);
481 static unsigned int wrap_index(struct io_tlb_mem *mem, unsigned int index)
483 if (index >= mem->nslabs)
489 * Find a suitable number of IO TLB entries size that will fit this request and
490 * allocate a buffer from that IO TLB pool.
492 static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
493 size_t alloc_size, unsigned int alloc_align_mask)
495 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
496 unsigned long boundary_mask = dma_get_seg_boundary(dev);
497 dma_addr_t tbl_dma_addr =
498 phys_to_dma_unencrypted(dev, mem->start) & boundary_mask;
499 unsigned long max_slots = get_max_slots(boundary_mask);
500 unsigned int iotlb_align_mask =
501 dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1);
502 unsigned int nslots = nr_slots(alloc_size), stride;
503 unsigned int index, wrap, count = 0, i;
504 unsigned int offset = swiotlb_align_offset(dev, orig_addr);
510 * For mappings with an alignment requirement don't bother looping to
511 * unaligned slots once we found an aligned one. For allocations of
512 * PAGE_SIZE or larger only look for page aligned allocations.
514 stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1;
515 if (alloc_size >= PAGE_SIZE)
516 stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT));
517 stride = max(stride, (alloc_align_mask >> IO_TLB_SHIFT) + 1);
519 spin_lock_irqsave(&mem->lock, flags);
520 if (unlikely(nslots > mem->nslabs - mem->used))
523 index = wrap = wrap_index(mem, ALIGN(mem->index, stride));
526 (slot_addr(tbl_dma_addr, index) & iotlb_align_mask) !=
527 (orig_addr & iotlb_align_mask)) {
528 index = wrap_index(mem, index + 1);
533 * If we find a slot that indicates we have 'nslots' number of
534 * contiguous buffers, we allocate the buffers from that slot
535 * and mark the entries as '0' indicating unavailable.
537 if (!iommu_is_span_boundary(index, nslots,
538 nr_slots(tbl_dma_addr),
540 if (mem->slots[index].list >= nslots)
543 index = wrap_index(mem, index + stride);
544 } while (index != wrap);
547 spin_unlock_irqrestore(&mem->lock, flags);
551 for (i = index; i < index + nslots; i++) {
552 mem->slots[i].list = 0;
553 mem->slots[i].alloc_size =
554 alloc_size - (offset + ((i - index) << IO_TLB_SHIFT));
557 io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 &&
558 mem->slots[i].list; i--)
559 mem->slots[i].list = ++count;
562 * Update the indices to avoid searching in the next round.
564 if (index + nslots < mem->nslabs)
565 mem->index = index + nslots;
570 spin_unlock_irqrestore(&mem->lock, flags);
574 phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
575 size_t mapping_size, size_t alloc_size,
576 unsigned int alloc_align_mask, enum dma_data_direction dir,
579 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
580 unsigned int offset = swiotlb_align_offset(dev, orig_addr);
583 phys_addr_t tlb_addr;
586 panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
588 if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
589 pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
591 if (mapping_size > alloc_size) {
592 dev_warn_once(dev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
593 mapping_size, alloc_size);
594 return (phys_addr_t)DMA_MAPPING_ERROR;
597 index = swiotlb_find_slots(dev, orig_addr,
598 alloc_size + offset, alloc_align_mask);
600 if (!(attrs & DMA_ATTR_NO_WARN))
601 dev_warn_ratelimited(dev,
602 "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n",
603 alloc_size, mem->nslabs, mem->used);
604 return (phys_addr_t)DMA_MAPPING_ERROR;
608 * Save away the mapping from the original address to the DMA address.
609 * This is needed when we sync the memory. Then we sync the buffer if
612 for (i = 0; i < nr_slots(alloc_size + offset); i++)
613 mem->slots[index + i].orig_addr = slot_addr(orig_addr, i);
614 tlb_addr = slot_addr(mem->start, index) + offset;
616 * When dir == DMA_FROM_DEVICE we could omit the copy from the orig
617 * to the tlb buffer, if we knew for sure the device will
618 * overwirte the entire current content. But we don't. Thus
619 * unconditional bounce may prevent leaking swiotlb content (i.e.
620 * kernel memory) to user-space.
622 swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE);
626 static void swiotlb_release_slots(struct device *dev, phys_addr_t tlb_addr)
628 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
630 unsigned int offset = swiotlb_align_offset(dev, tlb_addr);
631 int index = (tlb_addr - offset - mem->start) >> IO_TLB_SHIFT;
632 int nslots = nr_slots(mem->slots[index].alloc_size + offset);
636 * Return the buffer to the free list by setting the corresponding
637 * entries to indicate the number of contiguous entries available.
638 * While returning the entries to the free list, we merge the entries
639 * with slots below and above the pool being returned.
641 spin_lock_irqsave(&mem->lock, flags);
642 if (index + nslots < ALIGN(index + 1, IO_TLB_SEGSIZE))
643 count = mem->slots[index + nslots].list;
648 * Step 1: return the slots to the free list, merging the slots with
651 for (i = index + nslots - 1; i >= index; i--) {
652 mem->slots[i].list = ++count;
653 mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
654 mem->slots[i].alloc_size = 0;
658 * Step 2: merge the returned slots with the preceding slots, if
659 * available (non zero)
662 io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && mem->slots[i].list;
664 mem->slots[i].list = ++count;
666 spin_unlock_irqrestore(&mem->lock, flags);
670 * tlb_addr is the physical address of the bounce buffer to unmap.
672 void swiotlb_tbl_unmap_single(struct device *dev, phys_addr_t tlb_addr,
673 size_t mapping_size, enum dma_data_direction dir,
677 * First, sync the memory before unmapping the entry
679 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
680 (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
681 swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_FROM_DEVICE);
683 swiotlb_release_slots(dev, tlb_addr);
686 void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
687 size_t size, enum dma_data_direction dir)
689 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
690 swiotlb_bounce(dev, tlb_addr, size, DMA_TO_DEVICE);
692 BUG_ON(dir != DMA_FROM_DEVICE);
695 void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
696 size_t size, enum dma_data_direction dir)
698 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
699 swiotlb_bounce(dev, tlb_addr, size, DMA_FROM_DEVICE);
701 BUG_ON(dir != DMA_TO_DEVICE);
705 * Create a swiotlb mapping for the buffer at @paddr, and in case of DMAing
706 * to the device copy the data into it as well.
708 dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,
709 enum dma_data_direction dir, unsigned long attrs)
711 phys_addr_t swiotlb_addr;
714 trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size);
716 swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir,
718 if (swiotlb_addr == (phys_addr_t)DMA_MAPPING_ERROR)
719 return DMA_MAPPING_ERROR;
721 /* Ensure that the address returned is DMA'ble */
722 dma_addr = phys_to_dma_unencrypted(dev, swiotlb_addr);
723 if (unlikely(!dma_capable(dev, dma_addr, size, true))) {
724 swiotlb_tbl_unmap_single(dev, swiotlb_addr, size, dir,
725 attrs | DMA_ATTR_SKIP_CPU_SYNC);
726 dev_WARN_ONCE(dev, 1,
727 "swiotlb addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
728 &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
729 return DMA_MAPPING_ERROR;
732 if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
733 arch_sync_dma_for_device(swiotlb_addr, size, dir);
737 size_t swiotlb_max_mapping_size(struct device *dev)
739 int min_align_mask = dma_get_min_align_mask(dev);
743 * swiotlb_find_slots() skips slots according to
744 * min align mask. This affects max mapping size.
745 * Take it into acount here.
748 min_align = roundup(min_align_mask, IO_TLB_SIZE);
750 return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE - min_align;
753 bool is_swiotlb_active(struct device *dev)
755 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
757 return mem && mem->nslabs;
759 EXPORT_SYMBOL_GPL(is_swiotlb_active);
761 static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem,
764 mem->debugfs = debugfs_create_dir(dirname, io_tlb_default_mem.debugfs);
768 debugfs_create_ulong("io_tlb_nslabs", 0400, mem->debugfs, &mem->nslabs);
769 debugfs_create_ulong("io_tlb_used", 0400, mem->debugfs, &mem->used);
772 static int __init __maybe_unused swiotlb_create_default_debugfs(void)
774 swiotlb_create_debugfs_files(&io_tlb_default_mem, "swiotlb");
778 #ifdef CONFIG_DEBUG_FS
779 late_initcall(swiotlb_create_default_debugfs);
782 #ifdef CONFIG_DMA_RESTRICTED_POOL
784 struct page *swiotlb_alloc(struct device *dev, size_t size)
786 struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
787 phys_addr_t tlb_addr;
793 index = swiotlb_find_slots(dev, 0, size, 0);
797 tlb_addr = slot_addr(mem->start, index);
799 return pfn_to_page(PFN_DOWN(tlb_addr));
802 bool swiotlb_free(struct device *dev, struct page *page, size_t size)
804 phys_addr_t tlb_addr = page_to_phys(page);
806 if (!is_swiotlb_buffer(dev, tlb_addr))
809 swiotlb_release_slots(dev, tlb_addr);
814 static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
817 struct io_tlb_mem *mem = rmem->priv;
818 unsigned long nslabs = rmem->size >> IO_TLB_SHIFT;
821 * Since multiple devices can share the same pool, the private data,
822 * io_tlb_mem struct, will be initialized by the first device attached
826 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
830 mem->slots = kcalloc(nslabs, sizeof(*mem->slots), GFP_KERNEL);
836 set_memory_decrypted((unsigned long)phys_to_virt(rmem->base),
837 rmem->size >> PAGE_SHIFT);
838 swiotlb_init_io_tlb_mem(mem, rmem->base, nslabs, false);
839 mem->force_bounce = true;
840 mem->for_alloc = true;
844 swiotlb_create_debugfs_files(mem, rmem->name);
847 dev->dma_io_tlb_mem = mem;
852 static void rmem_swiotlb_device_release(struct reserved_mem *rmem,
855 dev->dma_io_tlb_mem = &io_tlb_default_mem;
858 static const struct reserved_mem_ops rmem_swiotlb_ops = {
859 .device_init = rmem_swiotlb_device_init,
860 .device_release = rmem_swiotlb_device_release,
863 static int __init rmem_swiotlb_setup(struct reserved_mem *rmem)
865 unsigned long node = rmem->fdt_node;
867 if (of_get_flat_dt_prop(node, "reusable", NULL) ||
868 of_get_flat_dt_prop(node, "linux,cma-default", NULL) ||
869 of_get_flat_dt_prop(node, "linux,dma-default", NULL) ||
870 of_get_flat_dt_prop(node, "no-map", NULL))
873 if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) {
874 pr_err("Restricted DMA pool must be accessible within the linear mapping.");
878 rmem->ops = &rmem_swiotlb_ops;
879 pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld MiB\n",
880 &rmem->base, (unsigned long)rmem->size / SZ_1M);
884 RESERVEDMEM_OF_DECLARE(dma, "restricted-dma-pool", rmem_swiotlb_setup);
885 #endif /* CONFIG_DMA_RESTRICTED_POOL */