Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Dynamic DMA mapping support. |
| 4 | * |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 5 | * This implementation is a fallback for platforms that do not support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
John W. Linville | 569c8bf | 2005-09-29 14:45:24 -0700 | [diff] [blame] | 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. |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 18 | * 08/12/11 beckyb Add highmem support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 21 | #define pr_fmt(fmt) "software IO TLB: " fmt |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/cache.h> |
Christoph Hellwig | ea8c64a | 2018-01-10 16:21:13 +0100 | [diff] [blame] | 24 | #include <linux/dma-direct.h> |
Christoph Hellwig | 9f4df96 | 2020-09-22 15:36:11 +0200 | [diff] [blame] | 25 | #include <linux/dma-map-ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/mm.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 27 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/string.h> |
Ian Campbell | 0016fde | 2008-12-16 12:17:27 -0800 | [diff] [blame] | 30 | #include <linux/swiotlb.h> |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 31 | #include <linux/pfn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/types.h> |
| 33 | #include <linux/ctype.h> |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame] | 34 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/gfp.h> |
Christoph Hellwig | 84be456 | 2015-05-01 12:46:15 +0200 | [diff] [blame] | 36 | #include <linux/scatterlist.h> |
Tom Lendacky | e9d1d2b | 2021-09-08 17:58:39 -0500 | [diff] [blame] | 37 | #include <linux/cc_platform.h> |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 38 | #include <linux/set_memory.h> |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 39 | #ifdef CONFIG_DEBUG_FS |
| 40 | #include <linux/debugfs.h> |
| 41 | #endif |
Claire Chang | 0b84e4f | 2021-06-19 11:40:41 +0800 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <asm/dma.h> |
| 52 | |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 53 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <linux/init.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 55 | #include <linux/memblock.h> |
FUJITA Tomonori | a852250 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 56 | #include <linux/iommu-helper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Thierry Reding | ce5be5a | 2013-10-23 13:32:04 +0200 | [diff] [blame] | 58 | #define CREATE_TRACE_POINTS |
Zoltan Kiss | 2b2b614 | 2013-09-04 21:11:05 +0100 | [diff] [blame] | 59 | #include <trace/events/swiotlb.h> |
| 60 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 61 | #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT)) |
| 62 | |
| 63 | /* |
| 64 | * Minimum IO TLB size to bother booting with. Systems with mainly |
| 65 | * 64bit capable cards will only lightly use the swiotlb. If we can't |
| 66 | * allocate a contiguous 1MB, we're probably in trouble anyway. |
| 67 | */ |
| 68 | #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT) |
| 69 | |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 70 | #define INVALID_PHYS_ADDR (~(phys_addr_t)0) |
| 71 | |
Geert Uytterhoeven | ae7871b | 2016-12-16 14:28:41 +0100 | [diff] [blame] | 72 | enum swiotlb_force swiotlb_force; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 74 | struct io_tlb_mem io_tlb_default_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 76 | phys_addr_t swiotlb_unencrypted_base; |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 79 | * Max segment that we can provide which (if pages are contingous) will |
| 80 | * not be bounced (unless SWIOTLB_FORCE is set). |
| 81 | */ |
Andy Shevchenko | b51e627 | 2020-09-02 20:31:05 +0300 | [diff] [blame] | 82 | static unsigned int max_segment; |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 83 | |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 84 | static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT; |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static int __init |
| 87 | setup_io_tlb_npages(char *str) |
| 88 | { |
| 89 | if (isdigit(*str)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* avoid tail segment of size < IO_TLB_SEGSIZE */ |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 91 | default_nslabs = |
| 92 | ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | if (*str == ',') |
| 95 | ++str; |
Florian Fainelli | 2726bf3 | 2021-03-22 18:53:49 -0700 | [diff] [blame] | 96 | if (!strcmp(str, "force")) |
Geert Uytterhoeven | ae7871b | 2016-12-16 14:28:41 +0100 | [diff] [blame] | 97 | swiotlb_force = SWIOTLB_FORCE; |
Florian Fainelli | 2726bf3 | 2021-03-22 18:53:49 -0700 | [diff] [blame] | 98 | else if (!strcmp(str, "noforce")) |
Geert Uytterhoeven | fff5d99 | 2016-12-16 14:28:42 +0100 | [diff] [blame] | 99 | swiotlb_force = SWIOTLB_NO_FORCE; |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 100 | |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 101 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 103 | early_param("swiotlb", setup_io_tlb_npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 105 | unsigned int swiotlb_max_segment(void) |
| 106 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 107 | return io_tlb_default_mem.nslabs ? max_segment : 0; |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 108 | } |
| 109 | EXPORT_SYMBOL_GPL(swiotlb_max_segment); |
| 110 | |
| 111 | void swiotlb_set_max_segment(unsigned int val) |
| 112 | { |
| 113 | if (swiotlb_force == SWIOTLB_FORCE) |
| 114 | max_segment = 1; |
| 115 | else |
| 116 | max_segment = rounddown(val, PAGE_SIZE); |
| 117 | } |
| 118 | |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 119 | unsigned long swiotlb_size_or_default(void) |
| 120 | { |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 121 | return default_nslabs << IO_TLB_SHIFT; |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 124 | void __init swiotlb_adjust_size(unsigned long size) |
Ashish Kalra | e998879 | 2020-12-10 01:25:15 +0000 | [diff] [blame] | 125 | { |
Ashish Kalra | e998879 | 2020-12-10 01:25:15 +0000 | [diff] [blame] | 126 | /* |
| 127 | * If swiotlb parameter has not been specified, give a chance to |
| 128 | * architectures such as those supporting memory encryption to |
| 129 | * adjust/expand SWIOTLB size for their use. |
| 130 | */ |
Christoph Hellwig | dfc06b3 | 2021-04-29 08:28:59 +0200 | [diff] [blame] | 131 | if (default_nslabs != IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT) |
| 132 | return; |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 133 | size = ALIGN(size, IO_TLB_SIZE); |
| 134 | default_nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE); |
| 135 | pr_info("SWIOTLB bounce buffer size adjusted to %luMB", size >> 20); |
Ashish Kalra | e998879 | 2020-12-10 01:25:15 +0000 | [diff] [blame] | 136 | } |
| 137 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 138 | void swiotlb_print_info(void) |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 139 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 140 | struct io_tlb_mem *mem = &io_tlb_default_mem; |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 141 | |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 142 | if (!mem->nslabs) { |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 143 | pr_warn("No low mem\n"); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 147 | pr_info("mapped [mem %pa-%pa] (%luMB)\n", &mem->start, &mem->end, |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 148 | (mem->nslabs << IO_TLB_SHIFT) >> 20); |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Christoph Hellwig | c7fbeca | 2021-02-04 10:11:20 +0100 | [diff] [blame] | 151 | static inline unsigned long io_tlb_offset(unsigned long val) |
| 152 | { |
| 153 | return val & (IO_TLB_SEGSIZE - 1); |
| 154 | } |
| 155 | |
Christoph Hellwig | c32a77fd | 2021-02-05 11:19:34 +0100 | [diff] [blame] | 156 | static inline unsigned long nr_slots(u64 val) |
| 157 | { |
| 158 | return DIV_ROUND_UP(val, IO_TLB_SIZE); |
| 159 | } |
| 160 | |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 161 | /* |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 162 | * Remap swioltb memory in the unencrypted physical address space |
| 163 | * when swiotlb_unencrypted_base is set. (e.g. for Hyper-V AMD SEV-SNP |
| 164 | * Isolation VMs). |
| 165 | */ |
Wei Liu | 2deb55d | 2022-01-04 16:11:19 +0000 | [diff] [blame] | 166 | #ifdef CONFIG_HAS_IOMEM |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 167 | static void *swiotlb_mem_remap(struct io_tlb_mem *mem, unsigned long bytes) |
| 168 | { |
| 169 | void *vaddr = NULL; |
| 170 | |
| 171 | if (swiotlb_unencrypted_base) { |
| 172 | phys_addr_t paddr = mem->start + swiotlb_unencrypted_base; |
| 173 | |
| 174 | vaddr = memremap(paddr, bytes, MEMREMAP_WB); |
| 175 | if (!vaddr) |
| 176 | pr_err("Failed to map the unencrypted memory %pa size %lx.\n", |
| 177 | &paddr, bytes); |
| 178 | } |
| 179 | |
| 180 | return vaddr; |
| 181 | } |
Wei Liu | 2deb55d | 2022-01-04 16:11:19 +0000 | [diff] [blame] | 182 | #else |
| 183 | static void *swiotlb_mem_remap(struct io_tlb_mem *mem, unsigned long bytes) |
| 184 | { |
| 185 | return NULL; |
| 186 | } |
| 187 | #endif |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 188 | |
| 189 | /* |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 190 | * Early SWIOTLB allocation may be too early to allow an architecture to |
| 191 | * perform the desired operations. This function allows the architecture to |
| 192 | * call SWIOTLB when the operations are possible. It needs to be called |
| 193 | * before the SWIOTLB memory is used. |
| 194 | */ |
| 195 | void __init swiotlb_update_mem_attributes(void) |
| 196 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 197 | struct io_tlb_mem *mem = &io_tlb_default_mem; |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 198 | void *vaddr; |
| 199 | unsigned long bytes; |
| 200 | |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 201 | if (!mem->nslabs || mem->late_alloc) |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 202 | return; |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 203 | vaddr = phys_to_virt(mem->start); |
| 204 | bytes = PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT); |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 205 | set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT); |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 206 | |
| 207 | mem->vaddr = swiotlb_mem_remap(mem, bytes); |
| 208 | if (!mem->vaddr) |
| 209 | mem->vaddr = vaddr; |
| 210 | |
| 211 | memset(mem->vaddr, 0, bytes); |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Claire Chang | 0a65579 | 2021-06-19 11:40:32 +0800 | [diff] [blame] | 214 | static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start, |
| 215 | unsigned long nslabs, bool late_alloc) |
| 216 | { |
| 217 | void *vaddr = phys_to_virt(start); |
| 218 | unsigned long bytes = nslabs << IO_TLB_SHIFT, i; |
| 219 | |
| 220 | mem->nslabs = nslabs; |
| 221 | mem->start = start; |
| 222 | mem->end = mem->start + bytes; |
| 223 | mem->index = 0; |
| 224 | mem->late_alloc = late_alloc; |
Claire Chang | 903cd0f | 2021-06-24 23:55:20 +0800 | [diff] [blame] | 225 | |
| 226 | if (swiotlb_force == SWIOTLB_FORCE) |
| 227 | mem->force_bounce = true; |
| 228 | |
Claire Chang | 0a65579 | 2021-06-19 11:40:32 +0800 | [diff] [blame] | 229 | spin_lock_init(&mem->lock); |
| 230 | for (i = 0; i < mem->nslabs; i++) { |
| 231 | mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i); |
| 232 | mem->slots[i].orig_addr = INVALID_PHYS_ADDR; |
| 233 | mem->slots[i].alloc_size = 0; |
| 234 | } |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * If swiotlb_unencrypted_base is set, the bounce buffer memory will |
| 238 | * be remapped and cleared in swiotlb_update_mem_attributes. |
| 239 | */ |
| 240 | if (swiotlb_unencrypted_base) |
| 241 | return; |
| 242 | |
Claire Chang | 0a65579 | 2021-06-19 11:40:32 +0800 | [diff] [blame] | 243 | memset(vaddr, 0, bytes); |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 244 | mem->vaddr = vaddr; |
| 245 | return; |
Claire Chang | 0a65579 | 2021-06-19 11:40:32 +0800 | [diff] [blame] | 246 | } |
| 247 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 248 | int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 250 | struct io_tlb_mem *mem = &io_tlb_default_mem; |
Mike Rapoport | a0bf842 | 2019-03-11 23:30:26 -0700 | [diff] [blame] | 251 | size_t alloc_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Florian Fainelli | 2726bf3 | 2021-03-22 18:53:49 -0700 | [diff] [blame] | 253 | if (swiotlb_force == SWIOTLB_NO_FORCE) |
| 254 | return 0; |
| 255 | |
Christoph Hellwig | 5d0538b | 2021-03-01 08:44:31 +0100 | [diff] [blame] | 256 | /* protect against double initialization */ |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 257 | if (WARN_ON_ONCE(mem->nslabs)) |
Christoph Hellwig | 5d0538b | 2021-03-01 08:44:31 +0100 | [diff] [blame] | 258 | return -ENOMEM; |
| 259 | |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 260 | alloc_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), nslabs)); |
| 261 | mem->slots = memblock_alloc(alloc_size, PAGE_SIZE); |
| 262 | if (!mem->slots) |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 263 | panic("%s: Failed to allocate %zu bytes align=0x%lx\n", |
| 264 | __func__, alloc_size, PAGE_SIZE); |
Claire Chang | 0a65579 | 2021-06-19 11:40:32 +0800 | [diff] [blame] | 265 | |
| 266 | swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 268 | if (verbose) |
| 269 | swiotlb_print_info(); |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 270 | swiotlb_set_max_segment(mem->nslabs << IO_TLB_SHIFT); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 271 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 274 | /* |
| 275 | * Statically reserve bounce buffer space and initialize bounce buffer data |
| 276 | * structures for the software IO TLB used to implement the DMA API. |
| 277 | */ |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 278 | void __init |
| 279 | swiotlb_init(int verbose) |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 280 | { |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 281 | size_t bytes = PAGE_ALIGN(default_nslabs << IO_TLB_SHIFT); |
| 282 | void *tlb; |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 283 | |
Florian Fainelli | 2726bf3 | 2021-03-22 18:53:49 -0700 | [diff] [blame] | 284 | if (swiotlb_force == SWIOTLB_NO_FORCE) |
| 285 | return; |
| 286 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 287 | /* Get IO TLB memory from the low pages */ |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 288 | tlb = memblock_alloc_low(bytes, PAGE_SIZE); |
| 289 | if (!tlb) |
| 290 | goto fail; |
| 291 | if (swiotlb_init_with_tbl(tlb, default_nslabs, verbose)) |
| 292 | goto fail_free_mem; |
| 293 | return; |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 294 | |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 295 | fail_free_mem: |
Mike Rapoport | 4421cca | 2021-11-05 13:43:22 -0700 | [diff] [blame] | 296 | memblock_free(tlb, bytes); |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 297 | fail: |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 298 | pr_warn("Cannot allocate buffer"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 301 | /* |
| 302 | * Systems with larger DMA zones (those that don't support ISA) can |
| 303 | * initialize the swiotlb later using the slab allocator if needed. |
| 304 | * This should be just like above, but with some error catching. |
| 305 | */ |
| 306 | int |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 307 | swiotlb_late_init_with_default_size(size_t default_size) |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 308 | { |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 309 | unsigned long nslabs = |
| 310 | ALIGN(default_size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE); |
| 311 | unsigned long bytes; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 312 | unsigned char *vstart = NULL; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 313 | unsigned int order; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 314 | int rc = 0; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 315 | |
Florian Fainelli | 2726bf3 | 2021-03-22 18:53:49 -0700 | [diff] [blame] | 316 | if (swiotlb_force == SWIOTLB_NO_FORCE) |
| 317 | return 0; |
| 318 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 319 | /* |
| 320 | * Get IO TLB memory from the low pages |
| 321 | */ |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 322 | order = get_order(nslabs << IO_TLB_SHIFT); |
| 323 | nslabs = SLABS_PER_PAGE << order; |
| 324 | bytes = nslabs << IO_TLB_SHIFT; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 325 | |
| 326 | while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 327 | vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, |
| 328 | order); |
| 329 | if (vstart) |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 330 | break; |
| 331 | order--; |
| 332 | } |
| 333 | |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 334 | if (!vstart) |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 335 | return -ENOMEM; |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 336 | |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 337 | if (order != get_order(bytes)) { |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 338 | pr_warn("only able to allocate %ld MB\n", |
| 339 | (PAGE_SIZE << order) >> 20); |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 340 | nslabs = SLABS_PER_PAGE << order; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 341 | } |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 342 | rc = swiotlb_late_init_with_tbl(vstart, nslabs); |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 343 | if (rc) |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 344 | free_pages((unsigned long)vstart, order); |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 345 | |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 346 | return rc; |
| 347 | } |
| 348 | |
| 349 | int |
| 350 | swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) |
| 351 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 352 | struct io_tlb_mem *mem = &io_tlb_default_mem; |
Claire Chang | 0a65579 | 2021-06-19 11:40:32 +0800 | [diff] [blame] | 353 | unsigned long bytes = nslabs << IO_TLB_SHIFT; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 354 | |
Florian Fainelli | 2726bf3 | 2021-03-22 18:53:49 -0700 | [diff] [blame] | 355 | if (swiotlb_force == SWIOTLB_NO_FORCE) |
| 356 | return 0; |
| 357 | |
Christoph Hellwig | 5d0538b | 2021-03-01 08:44:31 +0100 | [diff] [blame] | 358 | /* protect against double initialization */ |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 359 | if (WARN_ON_ONCE(mem->nslabs)) |
Christoph Hellwig | 5d0538b | 2021-03-01 08:44:31 +0100 | [diff] [blame] | 360 | return -ENOMEM; |
| 361 | |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 362 | mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 363 | get_order(array_size(sizeof(*mem->slots), nslabs))); |
| 364 | if (!mem->slots) |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 365 | return -ENOMEM; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 366 | |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 367 | set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT); |
Claire Chang | 0a65579 | 2021-06-19 11:40:32 +0800 | [diff] [blame] | 368 | swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 369 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 370 | swiotlb_print_info(); |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 371 | swiotlb_set_max_segment(mem->nslabs << IO_TLB_SHIFT); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 372 | return 0; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 373 | } |
| 374 | |
Christoph Hellwig | 7f2c8bb | 2017-12-23 14:14:54 +0100 | [diff] [blame] | 375 | void __init swiotlb_exit(void) |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 376 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 377 | struct io_tlb_mem *mem = &io_tlb_default_mem; |
Will Deacon | ad6c002 | 2021-07-20 14:38:26 +0100 | [diff] [blame] | 378 | unsigned long tbl_vaddr; |
| 379 | size_t tbl_size, slots_size; |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 380 | |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 381 | if (!mem->nslabs) |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 382 | return; |
| 383 | |
Will Deacon | 1efd3fc | 2021-07-20 14:38:25 +0100 | [diff] [blame] | 384 | pr_info("tearing down default memory pool\n"); |
Will Deacon | ad6c002 | 2021-07-20 14:38:26 +0100 | [diff] [blame] | 385 | tbl_vaddr = (unsigned long)phys_to_virt(mem->start); |
| 386 | tbl_size = PAGE_ALIGN(mem->end - mem->start); |
| 387 | slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs)); |
| 388 | |
| 389 | set_memory_encrypted(tbl_vaddr, tbl_size >> PAGE_SHIFT); |
| 390 | if (mem->late_alloc) { |
| 391 | free_pages(tbl_vaddr, get_order(tbl_size)); |
| 392 | free_pages((unsigned long)mem->slots, get_order(slots_size)); |
| 393 | } else { |
| 394 | memblock_free_late(mem->start, tbl_size); |
| 395 | memblock_free_late(__pa(mem->slots), slots_size); |
| 396 | } |
| 397 | |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 398 | memset(mem, 0, sizeof(*mem)); |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 399 | } |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* |
Bumyong Lee | 5f89468 | 2021-05-10 18:10:04 +0900 | [diff] [blame] | 402 | * Return the offset into a iotlb slot required to keep the device happy. |
| 403 | */ |
| 404 | static unsigned int swiotlb_align_offset(struct device *dev, u64 addr) |
| 405 | { |
| 406 | return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1); |
| 407 | } |
| 408 | |
| 409 | /* |
Dongli Zhang | 6442ca2 | 2019-01-18 15:10:26 +0800 | [diff] [blame] | 410 | * Bounce: copy the swiotlb buffer from or back to the original dma location |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 411 | */ |
Christoph Hellwig | 2bdba62 | 2021-03-01 08:44:25 +0100 | [diff] [blame] | 412 | static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size, |
| 413 | enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Claire Chang | 69031f5 | 2021-06-19 11:40:34 +0800 | [diff] [blame] | 415 | struct io_tlb_mem *mem = dev->dma_io_tlb_mem; |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 416 | int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT; |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 417 | phys_addr_t orig_addr = mem->slots[index].orig_addr; |
| 418 | size_t alloc_size = mem->slots[index].alloc_size; |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 419 | unsigned long pfn = PFN_DOWN(orig_addr); |
Tianyu Lan | 1a5e91d | 2021-12-13 02:14:02 -0500 | [diff] [blame] | 420 | unsigned char *vaddr = mem->vaddr + tlb_addr - mem->start; |
Dominique Martinet | 868c9dd | 2021-07-07 14:12:54 +0900 | [diff] [blame] | 421 | unsigned int tlb_offset, orig_addr_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Christoph Hellwig | 2bdba62 | 2021-03-01 08:44:25 +0100 | [diff] [blame] | 423 | if (orig_addr == INVALID_PHYS_ADDR) |
| 424 | return; |
| 425 | |
Dominique Martinet | 868c9dd | 2021-07-07 14:12:54 +0900 | [diff] [blame] | 426 | tlb_offset = tlb_addr & (IO_TLB_SIZE - 1); |
| 427 | orig_addr_offset = swiotlb_align_offset(dev, orig_addr); |
| 428 | if (tlb_offset < orig_addr_offset) { |
| 429 | dev_WARN_ONCE(dev, 1, |
| 430 | "Access before mapping start detected. orig offset %u, requested offset %u.\n", |
| 431 | orig_addr_offset, tlb_offset); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | tlb_offset -= orig_addr_offset; |
| 436 | if (tlb_offset > alloc_size) { |
| 437 | dev_WARN_ONCE(dev, 1, |
| 438 | "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu+%u.\n", |
| 439 | alloc_size, size, tlb_offset); |
| 440 | return; |
| 441 | } |
Bumyong Lee | 5f89468 | 2021-05-10 18:10:04 +0900 | [diff] [blame] | 442 | |
| 443 | orig_addr += tlb_offset; |
| 444 | alloc_size -= tlb_offset; |
| 445 | |
Christoph Hellwig | 2bdba62 | 2021-03-01 08:44:25 +0100 | [diff] [blame] | 446 | if (size > alloc_size) { |
| 447 | dev_WARN_ONCE(dev, 1, |
| 448 | "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu.\n", |
| 449 | alloc_size, size); |
| 450 | size = alloc_size; |
| 451 | } |
| 452 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 453 | if (PageHighMem(pfn_to_page(pfn))) { |
| 454 | /* The buffer does not have a mapping. Map it in and copy */ |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 455 | unsigned int offset = orig_addr & ~PAGE_MASK; |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 456 | char *buffer; |
| 457 | unsigned int sz = 0; |
| 458 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 460 | while (size) { |
Becky Bruce | 67131ad | 2009-04-08 09:09:16 -0500 | [diff] [blame] | 461 | sz = min_t(size_t, PAGE_SIZE - offset, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 463 | local_irq_save(flags); |
Cong Wang | c3eede8 | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 464 | buffer = kmap_atomic(pfn_to_page(pfn)); |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 465 | if (dir == DMA_TO_DEVICE) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 466 | memcpy(vaddr, buffer + offset, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | else |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 468 | memcpy(buffer + offset, vaddr, sz); |
Cong Wang | c3eede8 | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 469 | kunmap_atomic(buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | local_irq_restore(flags); |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 471 | |
| 472 | size -= sz; |
| 473 | pfn++; |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 474 | vaddr += sz; |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 475 | offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 477 | } else if (dir == DMA_TO_DEVICE) { |
| 478 | memcpy(vaddr, phys_to_virt(orig_addr), size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } else { |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 480 | memcpy(phys_to_virt(orig_addr), vaddr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 484 | #define slot_addr(start, idx) ((start) + ((idx) << IO_TLB_SHIFT)) |
| 485 | |
| 486 | /* |
| 487 | * Carefully handle integer overflow which can occur when boundary_mask == ~0UL. |
| 488 | */ |
| 489 | static inline unsigned long get_max_slots(unsigned long boundary_mask) |
| 490 | { |
| 491 | if (boundary_mask == ~0UL) |
| 492 | return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT); |
| 493 | return nr_slots(boundary_mask + 1); |
| 494 | } |
| 495 | |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 496 | static unsigned int wrap_index(struct io_tlb_mem *mem, unsigned int index) |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 497 | { |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 498 | if (index >= mem->nslabs) |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 499 | return 0; |
| 500 | return index; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Find a suitable number of IO TLB entries size that will fit this request and |
| 505 | * allocate a buffer from that IO TLB pool. |
| 506 | */ |
Claire Chang | 36f7b2f | 2021-06-24 23:55:21 +0800 | [diff] [blame] | 507 | static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr, |
David Stevens | e81e99b | 2021-09-29 11:32:59 +0900 | [diff] [blame] | 508 | size_t alloc_size, unsigned int alloc_align_mask) |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 509 | { |
Claire Chang | 69031f5 | 2021-06-19 11:40:34 +0800 | [diff] [blame] | 510 | struct io_tlb_mem *mem = dev->dma_io_tlb_mem; |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 511 | unsigned long boundary_mask = dma_get_seg_boundary(dev); |
| 512 | dma_addr_t tbl_dma_addr = |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 513 | phys_to_dma_unencrypted(dev, mem->start) & boundary_mask; |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 514 | unsigned long max_slots = get_max_slots(boundary_mask); |
Christoph Hellwig | 1f221a0 | 2021-02-22 14:39:44 -0500 | [diff] [blame] | 515 | unsigned int iotlb_align_mask = |
| 516 | dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1); |
| 517 | unsigned int nslots = nr_slots(alloc_size), stride; |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 518 | unsigned int index, wrap, count = 0, i; |
Claire Chang | 36f7b2f | 2021-06-24 23:55:21 +0800 | [diff] [blame] | 519 | unsigned int offset = swiotlb_align_offset(dev, orig_addr); |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 520 | unsigned long flags; |
| 521 | |
| 522 | BUG_ON(!nslots); |
| 523 | |
| 524 | /* |
Christoph Hellwig | 1f221a0 | 2021-02-22 14:39:44 -0500 | [diff] [blame] | 525 | * For mappings with an alignment requirement don't bother looping to |
| 526 | * unaligned slots once we found an aligned one. For allocations of |
| 527 | * PAGE_SIZE or larger only look for page aligned allocations. |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 528 | */ |
Christoph Hellwig | 1f221a0 | 2021-02-22 14:39:44 -0500 | [diff] [blame] | 529 | stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1; |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 530 | if (alloc_size >= PAGE_SIZE) |
Christoph Hellwig | 1f221a0 | 2021-02-22 14:39:44 -0500 | [diff] [blame] | 531 | stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT)); |
David Stevens | e81e99b | 2021-09-29 11:32:59 +0900 | [diff] [blame] | 532 | stride = max(stride, (alloc_align_mask >> IO_TLB_SHIFT) + 1); |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 533 | |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 534 | spin_lock_irqsave(&mem->lock, flags); |
| 535 | if (unlikely(nslots > mem->nslabs - mem->used)) |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 536 | goto not_found; |
| 537 | |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 538 | index = wrap = wrap_index(mem, ALIGN(mem->index, stride)); |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 539 | do { |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 540 | if (orig_addr && |
| 541 | (slot_addr(tbl_dma_addr, index) & iotlb_align_mask) != |
| 542 | (orig_addr & iotlb_align_mask)) { |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 543 | index = wrap_index(mem, index + 1); |
Christoph Hellwig | 1f221a0 | 2021-02-22 14:39:44 -0500 | [diff] [blame] | 544 | continue; |
| 545 | } |
| 546 | |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 547 | /* |
| 548 | * If we find a slot that indicates we have 'nslots' number of |
| 549 | * contiguous buffers, we allocate the buffers from that slot |
| 550 | * and mark the entries as '0' indicating unavailable. |
| 551 | */ |
| 552 | if (!iommu_is_span_boundary(index, nslots, |
| 553 | nr_slots(tbl_dma_addr), |
| 554 | max_slots)) { |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 555 | if (mem->slots[index].list >= nslots) |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 556 | goto found; |
| 557 | } |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 558 | index = wrap_index(mem, index + stride); |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 559 | } while (index != wrap); |
| 560 | |
| 561 | not_found: |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 562 | spin_unlock_irqrestore(&mem->lock, flags); |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 563 | return -1; |
| 564 | |
| 565 | found: |
Claire Chang | 36f7b2f | 2021-06-24 23:55:21 +0800 | [diff] [blame] | 566 | for (i = index; i < index + nslots; i++) { |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 567 | mem->slots[i].list = 0; |
Claire Chang | 36f7b2f | 2021-06-24 23:55:21 +0800 | [diff] [blame] | 568 | mem->slots[i].alloc_size = |
| 569 | alloc_size - (offset + ((i - index) << IO_TLB_SHIFT)); |
| 570 | } |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 571 | for (i = index - 1; |
| 572 | io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 573 | mem->slots[i].list; i--) |
| 574 | mem->slots[i].list = ++count; |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * Update the indices to avoid searching in the next round. |
| 578 | */ |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 579 | if (index + nslots < mem->nslabs) |
| 580 | mem->index = index + nslots; |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 581 | else |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 582 | mem->index = 0; |
| 583 | mem->used += nslots; |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 584 | |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 585 | spin_unlock_irqrestore(&mem->lock, flags); |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 586 | return index; |
| 587 | } |
| 588 | |
| 589 | phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, |
Christoph Hellwig | fc0021a | 2020-10-23 08:33:09 +0200 | [diff] [blame] | 590 | size_t mapping_size, size_t alloc_size, |
David Stevens | e81e99b | 2021-09-29 11:32:59 +0900 | [diff] [blame] | 591 | unsigned int alloc_align_mask, enum dma_data_direction dir, |
| 592 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
Claire Chang | 69031f5 | 2021-06-19 11:40:34 +0800 | [diff] [blame] | 594 | struct io_tlb_mem *mem = dev->dma_io_tlb_mem; |
Christoph Hellwig | 1f221a0 | 2021-02-22 14:39:44 -0500 | [diff] [blame] | 595 | unsigned int offset = swiotlb_align_offset(dev, orig_addr); |
Claire Chang | 95b079d | 2021-04-22 16:14:53 +0800 | [diff] [blame] | 596 | unsigned int i; |
| 597 | int index; |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 598 | phys_addr_t tlb_addr; |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 599 | |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 600 | if (!mem) |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 601 | panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer"); |
| 602 | |
Tom Lendacky | e9d1d2b | 2021-09-08 17:58:39 -0500 | [diff] [blame] | 603 | if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) |
Thiago Jung Bauermann | 47e5d8f | 2019-08-06 01:49:15 -0300 | [diff] [blame] | 604 | pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n"); |
Tom Lendacky | 648babb | 2017-07-17 16:10:22 -0500 | [diff] [blame] | 605 | |
Lu Baolu | 3fc1ca0 | 2019-09-06 14:14:48 +0800 | [diff] [blame] | 606 | if (mapping_size > alloc_size) { |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 607 | dev_warn_once(dev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)", |
Lu Baolu | 3fc1ca0 | 2019-09-06 14:14:48 +0800 | [diff] [blame] | 608 | mapping_size, alloc_size); |
| 609 | return (phys_addr_t)DMA_MAPPING_ERROR; |
| 610 | } |
| 611 | |
David Stevens | e81e99b | 2021-09-29 11:32:59 +0900 | [diff] [blame] | 612 | index = swiotlb_find_slots(dev, orig_addr, |
| 613 | alloc_size + offset, alloc_align_mask); |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 614 | if (index == -1) { |
| 615 | if (!(attrs & DMA_ATTR_NO_WARN)) |
| 616 | dev_warn_ratelimited(dev, |
| 617 | "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n", |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 618 | alloc_size, mem->nslabs, mem->used); |
Christoph Hellwig | 26a7e09 | 2021-02-04 11:08:35 +0100 | [diff] [blame] | 619 | return (phys_addr_t)DMA_MAPPING_ERROR; |
| 620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Save away the mapping from the original address to the DMA address. |
| 624 | * This is needed when we sync the memory. Then we sync the buffer if |
| 625 | * needed. |
| 626 | */ |
Claire Chang | 36f7b2f | 2021-06-24 23:55:21 +0800 | [diff] [blame] | 627 | for (i = 0; i < nr_slots(alloc_size + offset); i++) |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 628 | mem->slots[index + i].orig_addr = slot_addr(orig_addr, i); |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 629 | tlb_addr = slot_addr(mem->start, index) + offset; |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 630 | if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && |
| 631 | (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Christoph Hellwig | 2bdba62 | 2021-03-01 08:44:25 +0100 | [diff] [blame] | 632 | swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE); |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 633 | return tlb_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Claire Chang | 7034787 | 2021-06-19 11:40:39 +0800 | [diff] [blame] | 636 | static void swiotlb_release_slots(struct device *dev, phys_addr_t tlb_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Claire Chang | 7034787 | 2021-06-19 11:40:39 +0800 | [diff] [blame] | 638 | struct io_tlb_mem *mem = dev->dma_io_tlb_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | unsigned long flags; |
Claire Chang | 7034787 | 2021-06-19 11:40:39 +0800 | [diff] [blame] | 640 | unsigned int offset = swiotlb_align_offset(dev, tlb_addr); |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 641 | int index = (tlb_addr - offset - mem->start) >> IO_TLB_SHIFT; |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 642 | int nslots = nr_slots(mem->slots[index].alloc_size + offset); |
Christoph Hellwig | 2bdba62 | 2021-03-01 08:44:25 +0100 | [diff] [blame] | 643 | int count, i; |
Martin Radev | daf9514 | 2021-01-12 16:07:29 +0100 | [diff] [blame] | 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | * Return the buffer to the free list by setting the corresponding |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 647 | * entries to indicate the number of contiguous entries available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | * While returning the entries to the free list, we merge the entries |
| 649 | * with slots below and above the pool being returned. |
| 650 | */ |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 651 | spin_lock_irqsave(&mem->lock, flags); |
Christoph Hellwig | ca10d0f | 2021-02-04 10:13:40 +0100 | [diff] [blame] | 652 | if (index + nslots < ALIGN(index + 1, IO_TLB_SEGSIZE)) |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 653 | count = mem->slots[index + nslots].list; |
Christoph Hellwig | ca10d0f | 2021-02-04 10:13:40 +0100 | [diff] [blame] | 654 | else |
| 655 | count = 0; |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 656 | |
Christoph Hellwig | ca10d0f | 2021-02-04 10:13:40 +0100 | [diff] [blame] | 657 | /* |
| 658 | * Step 1: return the slots to the free list, merging the slots with |
| 659 | * superceeding slots |
| 660 | */ |
| 661 | for (i = index + nslots - 1; i >= index; i--) { |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 662 | mem->slots[i].list = ++count; |
| 663 | mem->slots[i].orig_addr = INVALID_PHYS_ADDR; |
| 664 | mem->slots[i].alloc_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
Christoph Hellwig | ca10d0f | 2021-02-04 10:13:40 +0100 | [diff] [blame] | 666 | |
| 667 | /* |
| 668 | * Step 2: merge the returned slots with the preceding slots, if |
| 669 | * available (non zero) |
| 670 | */ |
| 671 | for (i = index - 1; |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 672 | io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && mem->slots[i].list; |
Christoph Hellwig | ca10d0f | 2021-02-04 10:13:40 +0100 | [diff] [blame] | 673 | i--) |
Christoph Hellwig | 2d29960 | 2021-03-18 17:14:23 +0100 | [diff] [blame] | 674 | mem->slots[i].list = ++count; |
Claire Chang | 73f6209 | 2021-03-18 17:14:22 +0100 | [diff] [blame] | 675 | mem->used -= nslots; |
| 676 | spin_unlock_irqrestore(&mem->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Claire Chang | 7034787 | 2021-06-19 11:40:39 +0800 | [diff] [blame] | 679 | /* |
| 680 | * tlb_addr is the physical address of the bounce buffer to unmap. |
| 681 | */ |
| 682 | void swiotlb_tbl_unmap_single(struct device *dev, phys_addr_t tlb_addr, |
| 683 | size_t mapping_size, enum dma_data_direction dir, |
| 684 | unsigned long attrs) |
| 685 | { |
| 686 | /* |
| 687 | * First, sync the memory before unmapping the entry |
| 688 | */ |
| 689 | if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && |
| 690 | (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)) |
| 691 | swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_FROM_DEVICE); |
| 692 | |
| 693 | swiotlb_release_slots(dev, tlb_addr); |
| 694 | } |
| 695 | |
Christoph Hellwig | 80808d2 | 2021-03-01 08:44:26 +0100 | [diff] [blame] | 696 | void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr, |
| 697 | size_t size, enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | { |
Christoph Hellwig | 80808d2 | 2021-03-01 08:44:26 +0100 | [diff] [blame] | 699 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 700 | swiotlb_bounce(dev, tlb_addr, size, DMA_TO_DEVICE); |
| 701 | else |
| 702 | BUG_ON(dir != DMA_FROM_DEVICE); |
| 703 | } |
| 704 | |
| 705 | void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr, |
| 706 | size_t size, enum dma_data_direction dir) |
| 707 | { |
| 708 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 709 | swiotlb_bounce(dev, tlb_addr, size, DMA_FROM_DEVICE); |
| 710 | else |
| 711 | BUG_ON(dir != DMA_TO_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 714 | /* |
Christoph Hellwig | 4a47cba | 2020-02-03 14:44:38 +0100 | [diff] [blame] | 715 | * Create a swiotlb mapping for the buffer at @paddr, and in case of DMAing |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 716 | * to the device copy the data into it as well. |
| 717 | */ |
Christoph Hellwig | 4a47cba | 2020-02-03 14:44:38 +0100 | [diff] [blame] | 718 | dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size, |
| 719 | enum dma_data_direction dir, unsigned long attrs) |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 720 | { |
Christoph Hellwig | 4a47cba | 2020-02-03 14:44:38 +0100 | [diff] [blame] | 721 | phys_addr_t swiotlb_addr; |
| 722 | dma_addr_t dma_addr; |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 723 | |
Christoph Hellwig | 4a47cba | 2020-02-03 14:44:38 +0100 | [diff] [blame] | 724 | trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size, |
| 725 | swiotlb_force); |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 726 | |
David Stevens | e81e99b | 2021-09-29 11:32:59 +0900 | [diff] [blame] | 727 | swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir, |
Christoph Hellwig | fc0021a | 2020-10-23 08:33:09 +0200 | [diff] [blame] | 728 | attrs); |
Christoph Hellwig | 4a47cba | 2020-02-03 14:44:38 +0100 | [diff] [blame] | 729 | if (swiotlb_addr == (phys_addr_t)DMA_MAPPING_ERROR) |
| 730 | return DMA_MAPPING_ERROR; |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 731 | |
| 732 | /* Ensure that the address returned is DMA'ble */ |
Christoph Hellwig | 5ceda74 | 2020-08-17 17:34:03 +0200 | [diff] [blame] | 733 | dma_addr = phys_to_dma_unencrypted(dev, swiotlb_addr); |
Christoph Hellwig | 4a47cba | 2020-02-03 14:44:38 +0100 | [diff] [blame] | 734 | if (unlikely(!dma_capable(dev, dma_addr, size, true))) { |
Christoph Hellwig | 2973073 | 2021-03-01 08:44:24 +0100 | [diff] [blame] | 735 | swiotlb_tbl_unmap_single(dev, swiotlb_addr, size, dir, |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 736 | attrs | DMA_ATTR_SKIP_CPU_SYNC); |
Christoph Hellwig | 4a47cba | 2020-02-03 14:44:38 +0100 | [diff] [blame] | 737 | dev_WARN_ONCE(dev, 1, |
| 738 | "swiotlb addr %pad+%zu overflow (mask %llx, bus limit %llx).\n", |
| 739 | &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit); |
| 740 | return DMA_MAPPING_ERROR; |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Christoph Hellwig | 4a47cba | 2020-02-03 14:44:38 +0100 | [diff] [blame] | 743 | if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) |
| 744 | arch_sync_dma_for_device(swiotlb_addr, size, dir); |
| 745 | return dma_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Joerg Roedel | abe420b | 2019-02-07 12:59:13 +0100 | [diff] [blame] | 748 | size_t swiotlb_max_mapping_size(struct device *dev) |
| 749 | { |
Christoph Hellwig | b5d7ccb | 2021-02-05 11:18:40 +0100 | [diff] [blame] | 750 | return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE; |
Joerg Roedel | abe420b | 2019-02-07 12:59:13 +0100 | [diff] [blame] | 751 | } |
Joerg Roedel | 492366f | 2019-02-07 12:59:14 +0100 | [diff] [blame] | 752 | |
Claire Chang | 6f2beb2 | 2021-06-19 11:40:36 +0800 | [diff] [blame] | 753 | bool is_swiotlb_active(struct device *dev) |
Joerg Roedel | 492366f | 2019-02-07 12:59:14 +0100 | [diff] [blame] | 754 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 755 | struct io_tlb_mem *mem = dev->dma_io_tlb_mem; |
| 756 | |
| 757 | return mem && mem->nslabs; |
Joerg Roedel | 492366f | 2019-02-07 12:59:14 +0100 | [diff] [blame] | 758 | } |
Christoph Hellwig | 2cbc277 | 2021-03-18 17:14:24 +0100 | [diff] [blame] | 759 | EXPORT_SYMBOL_GPL(is_swiotlb_active); |
Linus Torvalds | 45ba8d5 | 2019-03-10 12:47:57 -0700 | [diff] [blame] | 760 | |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 761 | #ifdef CONFIG_DEBUG_FS |
Claire Chang | 6e675a1 | 2021-06-19 11:40:33 +0800 | [diff] [blame] | 762 | static struct dentry *debugfs_dir; |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 763 | |
Claire Chang | 6e675a1 | 2021-06-19 11:40:33 +0800 | [diff] [blame] | 764 | static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem) |
| 765 | { |
| 766 | debugfs_create_ulong("io_tlb_nslabs", 0400, mem->debugfs, &mem->nslabs); |
| 767 | debugfs_create_ulong("io_tlb_used", 0400, mem->debugfs, &mem->used); |
| 768 | } |
| 769 | |
| 770 | static int __init swiotlb_create_default_debugfs(void) |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 771 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 772 | struct io_tlb_mem *mem = &io_tlb_default_mem; |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 773 | |
Claire Chang | 6e675a1 | 2021-06-19 11:40:33 +0800 | [diff] [blame] | 774 | debugfs_dir = debugfs_create_dir("swiotlb", NULL); |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 775 | if (mem->nslabs) { |
Claire Chang | 6e675a1 | 2021-06-19 11:40:33 +0800 | [diff] [blame] | 776 | mem->debugfs = debugfs_dir; |
| 777 | swiotlb_create_debugfs_files(mem); |
| 778 | } |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 779 | return 0; |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 780 | } |
| 781 | |
Claire Chang | 6e675a1 | 2021-06-19 11:40:33 +0800 | [diff] [blame] | 782 | late_initcall(swiotlb_create_default_debugfs); |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 783 | |
| 784 | #endif |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 785 | |
| 786 | #ifdef CONFIG_DMA_RESTRICTED_POOL |
Claire Chang | 09a4a79 | 2021-07-01 11:31:30 +0800 | [diff] [blame] | 787 | |
| 788 | #ifdef CONFIG_DEBUG_FS |
| 789 | static void rmem_swiotlb_debugfs_init(struct reserved_mem *rmem) |
| 790 | { |
| 791 | struct io_tlb_mem *mem = rmem->priv; |
| 792 | |
| 793 | mem->debugfs = debugfs_create_dir(rmem->name, debugfs_dir); |
| 794 | swiotlb_create_debugfs_files(mem); |
| 795 | } |
| 796 | #else |
| 797 | static void rmem_swiotlb_debugfs_init(struct reserved_mem *rmem) |
| 798 | { |
| 799 | } |
| 800 | #endif |
| 801 | |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 802 | struct page *swiotlb_alloc(struct device *dev, size_t size) |
| 803 | { |
| 804 | struct io_tlb_mem *mem = dev->dma_io_tlb_mem; |
| 805 | phys_addr_t tlb_addr; |
| 806 | int index; |
| 807 | |
| 808 | if (!mem) |
| 809 | return NULL; |
| 810 | |
David Stevens | e81e99b | 2021-09-29 11:32:59 +0900 | [diff] [blame] | 811 | index = swiotlb_find_slots(dev, 0, size, 0); |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 812 | if (index == -1) |
| 813 | return NULL; |
| 814 | |
| 815 | tlb_addr = slot_addr(mem->start, index); |
| 816 | |
| 817 | return pfn_to_page(PFN_DOWN(tlb_addr)); |
| 818 | } |
| 819 | |
| 820 | bool swiotlb_free(struct device *dev, struct page *page, size_t size) |
| 821 | { |
| 822 | phys_addr_t tlb_addr = page_to_phys(page); |
| 823 | |
| 824 | if (!is_swiotlb_buffer(dev, tlb_addr)) |
| 825 | return false; |
| 826 | |
| 827 | swiotlb_release_slots(dev, tlb_addr); |
| 828 | |
| 829 | return true; |
| 830 | } |
| 831 | |
Claire Chang | 0b84e4f | 2021-06-19 11:40:41 +0800 | [diff] [blame] | 832 | static int rmem_swiotlb_device_init(struct reserved_mem *rmem, |
| 833 | struct device *dev) |
| 834 | { |
| 835 | struct io_tlb_mem *mem = rmem->priv; |
| 836 | unsigned long nslabs = rmem->size >> IO_TLB_SHIFT; |
| 837 | |
| 838 | /* |
| 839 | * Since multiple devices can share the same pool, the private data, |
| 840 | * io_tlb_mem struct, will be initialized by the first device attached |
| 841 | * to it. |
| 842 | */ |
| 843 | if (!mem) { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 844 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
Claire Chang | 0b84e4f | 2021-06-19 11:40:41 +0800 | [diff] [blame] | 845 | if (!mem) |
| 846 | return -ENOMEM; |
| 847 | |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 848 | mem->slots = kzalloc(array_size(sizeof(*mem->slots), nslabs), |
| 849 | GFP_KERNEL); |
| 850 | if (!mem->slots) { |
| 851 | kfree(mem); |
| 852 | return -ENOMEM; |
| 853 | } |
| 854 | |
Claire Chang | 0b84e4f | 2021-06-19 11:40:41 +0800 | [diff] [blame] | 855 | set_memory_decrypted((unsigned long)phys_to_virt(rmem->base), |
| 856 | rmem->size >> PAGE_SHIFT); |
| 857 | swiotlb_init_io_tlb_mem(mem, rmem->base, nslabs, false); |
| 858 | mem->force_bounce = true; |
| 859 | mem->for_alloc = true; |
| 860 | |
| 861 | rmem->priv = mem; |
| 862 | |
Claire Chang | 09a4a79 | 2021-07-01 11:31:30 +0800 | [diff] [blame] | 863 | rmem_swiotlb_debugfs_init(rmem); |
Claire Chang | 0b84e4f | 2021-06-19 11:40:41 +0800 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | dev->dma_io_tlb_mem = mem; |
| 867 | |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static void rmem_swiotlb_device_release(struct reserved_mem *rmem, |
| 872 | struct device *dev) |
| 873 | { |
Will Deacon | 463e862 | 2021-07-20 14:38:24 +0100 | [diff] [blame] | 874 | dev->dma_io_tlb_mem = &io_tlb_default_mem; |
Claire Chang | 0b84e4f | 2021-06-19 11:40:41 +0800 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static const struct reserved_mem_ops rmem_swiotlb_ops = { |
| 878 | .device_init = rmem_swiotlb_device_init, |
| 879 | .device_release = rmem_swiotlb_device_release, |
| 880 | }; |
| 881 | |
| 882 | static int __init rmem_swiotlb_setup(struct reserved_mem *rmem) |
| 883 | { |
| 884 | unsigned long node = rmem->fdt_node; |
| 885 | |
| 886 | if (of_get_flat_dt_prop(node, "reusable", NULL) || |
| 887 | of_get_flat_dt_prop(node, "linux,cma-default", NULL) || |
| 888 | of_get_flat_dt_prop(node, "linux,dma-default", NULL) || |
| 889 | of_get_flat_dt_prop(node, "no-map", NULL)) |
| 890 | return -EINVAL; |
| 891 | |
| 892 | if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) { |
| 893 | pr_err("Restricted DMA pool must be accessible within the linear mapping."); |
| 894 | return -EINVAL; |
| 895 | } |
| 896 | |
| 897 | rmem->ops = &rmem_swiotlb_ops; |
| 898 | pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld MiB\n", |
| 899 | &rmem->base, (unsigned long)rmem->size / SZ_1M); |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | RESERVEDMEM_OF_DECLARE(dma, "restricted-dma-pool", rmem_swiotlb_setup); |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 904 | #endif /* CONFIG_DMA_RESTRICTED_POOL */ |