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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/mm.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 26 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/string.h> |
Ian Campbell | 0016fde | 2008-12-16 12:17:27 -0800 | [diff] [blame] | 29 | #include <linux/swiotlb.h> |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 30 | #include <linux/pfn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/types.h> |
| 32 | #include <linux/ctype.h> |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame] | 33 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/gfp.h> |
Christoph Hellwig | 84be456 | 2015-05-01 12:46:15 +0200 | [diff] [blame] | 35 | #include <linux/scatterlist.h> |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 36 | #include <linux/mem_encrypt.h> |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 37 | #include <linux/set_memory.h> |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 38 | #ifdef CONFIG_DEBUG_FS |
| 39 | #include <linux/debugfs.h> |
| 40 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/dma.h> |
| 44 | |
| 45 | #include <linux/init.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 46 | #include <linux/memblock.h> |
FUJITA Tomonori | a852250 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 47 | #include <linux/iommu-helper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Thierry Reding | ce5be5a | 2013-10-23 13:32:04 +0200 | [diff] [blame] | 49 | #define CREATE_TRACE_POINTS |
Zoltan Kiss | 2b2b614 | 2013-09-04 21:11:05 +0100 | [diff] [blame] | 50 | #include <trace/events/swiotlb.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define OFFSET(val,align) ((unsigned long) \ |
| 53 | ( (val) & ( (align) - 1))) |
| 54 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 55 | #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT)) |
| 56 | |
| 57 | /* |
| 58 | * Minimum IO TLB size to bother booting with. Systems with mainly |
| 59 | * 64bit capable cards will only lightly use the swiotlb. If we can't |
| 60 | * allocate a contiguous 1MB, we're probably in trouble anyway. |
| 61 | */ |
| 62 | #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT) |
| 63 | |
Geert Uytterhoeven | ae7871b | 2016-12-16 14:28:41 +0100 | [diff] [blame] | 64 | enum swiotlb_force swiotlb_force; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
Konrad Rzeszutek Wilk | bfc5501 | 2010-05-10 15:54:20 -0400 | [diff] [blame] | 67 | * Used to do a quick range check in swiotlb_tbl_unmap_single and |
| 68 | * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | * API. |
| 70 | */ |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 71 | phys_addr_t io_tlb_start, io_tlb_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 74 | * The number of IO TLB blocks (in groups of 64) between io_tlb_start and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * io_tlb_end. This is command line adjustable via setup_io_tlb_npages. |
| 76 | */ |
| 77 | static unsigned long io_tlb_nslabs; |
| 78 | |
| 79 | /* |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 80 | * The number of used IO TLB block |
| 81 | */ |
| 82 | static unsigned long io_tlb_used; |
| 83 | |
| 84 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | * This is a free list describing the number of free entries available from |
| 86 | * each index |
| 87 | */ |
| 88 | static unsigned int *io_tlb_list; |
| 89 | static unsigned int io_tlb_index; |
| 90 | |
| 91 | /* |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 92 | * Max segment that we can provide which (if pages are contingous) will |
| 93 | * not be bounced (unless SWIOTLB_FORCE is set). |
| 94 | */ |
| 95 | unsigned int max_segment; |
| 96 | |
| 97 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * We need to save away the original address corresponding to a mapped entry |
| 99 | * for the sync operations. |
| 100 | */ |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 101 | #define INVALID_PHYS_ADDR (~(phys_addr_t)0) |
Becky Bruce | bc40ac6 | 2008-12-22 10:26:08 -0800 | [diff] [blame] | 102 | static phys_addr_t *io_tlb_orig_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Protect the above data structures in the map and unmap calls |
| 106 | */ |
| 107 | static DEFINE_SPINLOCK(io_tlb_lock); |
| 108 | |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 109 | static int late_alloc; |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | static int __init |
| 112 | setup_io_tlb_npages(char *str) |
| 113 | { |
| 114 | if (isdigit(*str)) { |
Alex Williamson | e8579e7 | 2005-08-04 13:06:00 -0700 | [diff] [blame] | 115 | io_tlb_nslabs = simple_strtoul(str, &str, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* avoid tail segment of size < IO_TLB_SEGSIZE */ |
| 117 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); |
| 118 | } |
| 119 | if (*str == ',') |
| 120 | ++str; |
Geert Uytterhoeven | fff5d99 | 2016-12-16 14:28:42 +0100 | [diff] [blame] | 121 | if (!strcmp(str, "force")) { |
Geert Uytterhoeven | ae7871b | 2016-12-16 14:28:41 +0100 | [diff] [blame] | 122 | swiotlb_force = SWIOTLB_FORCE; |
Geert Uytterhoeven | fff5d99 | 2016-12-16 14:28:42 +0100 | [diff] [blame] | 123 | } else if (!strcmp(str, "noforce")) { |
| 124 | swiotlb_force = SWIOTLB_NO_FORCE; |
| 125 | io_tlb_nslabs = 1; |
| 126 | } |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 127 | |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 128 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 130 | early_param("swiotlb", setup_io_tlb_npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Konrad Rzeszutek Wilk | f21ffe9 | 2011-08-11 16:50:56 -0400 | [diff] [blame] | 132 | unsigned long swiotlb_nr_tbl(void) |
FUJITA Tomonori | 5f98ecd | 2011-06-05 11:47:29 +0900 | [diff] [blame] | 133 | { |
| 134 | return io_tlb_nslabs; |
| 135 | } |
Konrad Rzeszutek Wilk | f21ffe9 | 2011-08-11 16:50:56 -0400 | [diff] [blame] | 136 | EXPORT_SYMBOL_GPL(swiotlb_nr_tbl); |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 137 | |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 138 | unsigned int swiotlb_max_segment(void) |
| 139 | { |
| 140 | return max_segment; |
| 141 | } |
| 142 | EXPORT_SYMBOL_GPL(swiotlb_max_segment); |
| 143 | |
| 144 | void swiotlb_set_max_segment(unsigned int val) |
| 145 | { |
| 146 | if (swiotlb_force == SWIOTLB_FORCE) |
| 147 | max_segment = 1; |
| 148 | else |
| 149 | max_segment = rounddown(val, PAGE_SIZE); |
| 150 | } |
| 151 | |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 152 | /* default to 64MB */ |
| 153 | #define IO_TLB_DEFAULT_SIZE (64UL<<20) |
| 154 | unsigned long swiotlb_size_or_default(void) |
| 155 | { |
| 156 | unsigned long size; |
| 157 | |
| 158 | size = io_tlb_nslabs << IO_TLB_SHIFT; |
| 159 | |
| 160 | return size ? size : (IO_TLB_DEFAULT_SIZE); |
| 161 | } |
| 162 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 163 | static bool no_iotlb_memory; |
| 164 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 165 | void swiotlb_print_info(void) |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 166 | { |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 167 | unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 168 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 169 | if (no_iotlb_memory) { |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 170 | pr_warn("No low mem\n"); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 174 | pr_info("mapped [mem %#010llx-%#010llx] (%luMB)\n", |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 175 | (unsigned long long)io_tlb_start, |
Alexander Duyck | c40dba0 | 2012-10-15 10:19:22 -0700 | [diff] [blame] | 176 | (unsigned long long)io_tlb_end, |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 177 | bytes >> 20); |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 180 | /* |
| 181 | * Early SWIOTLB allocation may be too early to allow an architecture to |
| 182 | * perform the desired operations. This function allows the architecture to |
| 183 | * call SWIOTLB when the operations are possible. It needs to be called |
| 184 | * before the SWIOTLB memory is used. |
| 185 | */ |
| 186 | void __init swiotlb_update_mem_attributes(void) |
| 187 | { |
| 188 | void *vaddr; |
| 189 | unsigned long bytes; |
| 190 | |
| 191 | if (no_iotlb_memory || late_alloc) |
| 192 | return; |
| 193 | |
| 194 | vaddr = phys_to_virt(io_tlb_start); |
| 195 | bytes = PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT); |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 196 | set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT); |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 197 | memset(vaddr, 0, bytes); |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 200 | 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] | 201 | { |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 202 | unsigned long i, bytes; |
Mike Rapoport | a0bf842 | 2019-03-11 23:30:26 -0700 | [diff] [blame] | 203 | size_t alloc_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 205 | bytes = nslabs << IO_TLB_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 207 | io_tlb_nslabs = nslabs; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 208 | io_tlb_start = __pa(tlb); |
| 209 | io_tlb_end = io_tlb_start + bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * Allocate and initialize the free list array. This array is used |
| 213 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE |
| 214 | * between io_tlb_start and io_tlb_end. |
| 215 | */ |
Mike Rapoport | a0bf842 | 2019-03-11 23:30:26 -0700 | [diff] [blame] | 216 | alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(int)); |
| 217 | io_tlb_list = memblock_alloc(alloc_size, PAGE_SIZE); |
| 218 | if (!io_tlb_list) |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 219 | panic("%s: Failed to allocate %zu bytes align=0x%lx\n", |
Mike Rapoport | a0bf842 | 2019-03-11 23:30:26 -0700 | [diff] [blame] | 220 | __func__, alloc_size, PAGE_SIZE); |
| 221 | |
| 222 | alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)); |
| 223 | io_tlb_orig_addr = memblock_alloc(alloc_size, PAGE_SIZE); |
| 224 | if (!io_tlb_orig_addr) |
Mike Rapoport | 8a7f97b | 2019-03-11 23:30:31 -0700 | [diff] [blame] | 225 | panic("%s: Failed to allocate %zu bytes align=0x%lx\n", |
Mike Rapoport | a0bf842 | 2019-03-11 23:30:26 -0700 | [diff] [blame] | 226 | __func__, alloc_size, PAGE_SIZE); |
| 227 | |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 228 | for (i = 0; i < io_tlb_nslabs; i++) { |
| 229 | io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE); |
| 230 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; |
| 231 | } |
| 232 | io_tlb_index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 234 | if (verbose) |
| 235 | swiotlb_print_info(); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 236 | |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 237 | swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 238 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 241 | /* |
| 242 | * Statically reserve bounce buffer space and initialize bounce buffer data |
| 243 | * structures for the software IO TLB used to implement the DMA API. |
| 244 | */ |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 245 | void __init |
| 246 | swiotlb_init(int verbose) |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 247 | { |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 248 | size_t default_size = IO_TLB_DEFAULT_SIZE; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 249 | unsigned char *vstart; |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 250 | unsigned long bytes; |
| 251 | |
| 252 | if (!io_tlb_nslabs) { |
| 253 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); |
| 254 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); |
| 255 | } |
| 256 | |
| 257 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
| 258 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 259 | /* Get IO TLB memory from the low pages */ |
Mike Rapoport | 26fb3da | 2019-03-11 23:30:42 -0700 | [diff] [blame] | 260 | vstart = memblock_alloc_low(PAGE_ALIGN(bytes), PAGE_SIZE); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 261 | if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose)) |
| 262 | return; |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 263 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 264 | if (io_tlb_start) |
Santosh Shilimkar | 457ff1d | 2014-01-21 15:50:30 -0800 | [diff] [blame] | 265 | memblock_free_early(io_tlb_start, |
| 266 | PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 267 | pr_warn("Cannot allocate buffer"); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 268 | no_iotlb_memory = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 271 | /* |
| 272 | * Systems with larger DMA zones (those that don't support ISA) can |
| 273 | * initialize the swiotlb later using the slab allocator if needed. |
| 274 | * This should be just like above, but with some error catching. |
| 275 | */ |
| 276 | int |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 277 | swiotlb_late_init_with_default_size(size_t default_size) |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 278 | { |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 279 | unsigned long bytes, req_nslabs = io_tlb_nslabs; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 280 | unsigned char *vstart = NULL; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 281 | unsigned int order; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 282 | int rc = 0; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 283 | |
| 284 | if (!io_tlb_nslabs) { |
| 285 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); |
| 286 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Get IO TLB memory from the low pages |
| 291 | */ |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 292 | order = get_order(io_tlb_nslabs << IO_TLB_SHIFT); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 293 | io_tlb_nslabs = SLABS_PER_PAGE << order; |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 294 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 295 | |
| 296 | while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 297 | vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, |
| 298 | order); |
| 299 | if (vstart) |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 300 | break; |
| 301 | order--; |
| 302 | } |
| 303 | |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 304 | if (!vstart) { |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 305 | io_tlb_nslabs = req_nslabs; |
| 306 | return -ENOMEM; |
| 307 | } |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 308 | if (order != get_order(bytes)) { |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 309 | pr_warn("only able to allocate %ld MB\n", |
| 310 | (PAGE_SIZE << order) >> 20); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 311 | io_tlb_nslabs = SLABS_PER_PAGE << order; |
| 312 | } |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 313 | rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs); |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 314 | if (rc) |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 315 | free_pages((unsigned long)vstart, order); |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 316 | |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 317 | return rc; |
| 318 | } |
| 319 | |
| 320 | int |
| 321 | swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) |
| 322 | { |
| 323 | unsigned long i, bytes; |
| 324 | |
| 325 | bytes = nslabs << IO_TLB_SHIFT; |
| 326 | |
| 327 | io_tlb_nslabs = nslabs; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 328 | io_tlb_start = virt_to_phys(tlb); |
| 329 | io_tlb_end = io_tlb_start + bytes; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 330 | |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 331 | set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT); |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 332 | memset(tlb, 0, bytes); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * Allocate and initialize the free list array. This array is used |
| 336 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE |
| 337 | * between io_tlb_start and io_tlb_end. |
| 338 | */ |
| 339 | io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL, |
| 340 | get_order(io_tlb_nslabs * sizeof(int))); |
| 341 | if (!io_tlb_list) |
Alexander Duyck | ee3f6ba | 2012-10-15 10:19:34 -0700 | [diff] [blame] | 342 | goto cleanup3; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 343 | |
Becky Bruce | bc40ac6 | 2008-12-22 10:26:08 -0800 | [diff] [blame] | 344 | io_tlb_orig_addr = (phys_addr_t *) |
| 345 | __get_free_pages(GFP_KERNEL, |
| 346 | get_order(io_tlb_nslabs * |
| 347 | sizeof(phys_addr_t))); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 348 | if (!io_tlb_orig_addr) |
Alexander Duyck | ee3f6ba | 2012-10-15 10:19:34 -0700 | [diff] [blame] | 349 | goto cleanup4; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 350 | |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 351 | for (i = 0; i < io_tlb_nslabs; i++) { |
| 352 | io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE); |
| 353 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; |
| 354 | } |
| 355 | io_tlb_index = 0; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 356 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 357 | swiotlb_print_info(); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 358 | |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 359 | late_alloc = 1; |
| 360 | |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 361 | swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT); |
| 362 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 363 | return 0; |
| 364 | |
| 365 | cleanup4: |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 366 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * |
| 367 | sizeof(int))); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 368 | io_tlb_list = NULL; |
Alexander Duyck | ee3f6ba | 2012-10-15 10:19:34 -0700 | [diff] [blame] | 369 | cleanup3: |
Alexander Duyck | c40dba0 | 2012-10-15 10:19:22 -0700 | [diff] [blame] | 370 | io_tlb_end = 0; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 371 | io_tlb_start = 0; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 372 | io_tlb_nslabs = 0; |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 373 | max_segment = 0; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 374 | return -ENOMEM; |
| 375 | } |
| 376 | |
Christoph Hellwig | 7f2c8bb | 2017-12-23 14:14:54 +0100 | [diff] [blame] | 377 | void __init swiotlb_exit(void) |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 378 | { |
Alexander Duyck | ee3f6ba | 2012-10-15 10:19:34 -0700 | [diff] [blame] | 379 | if (!io_tlb_orig_addr) |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 380 | return; |
| 381 | |
| 382 | if (late_alloc) { |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 383 | free_pages((unsigned long)io_tlb_orig_addr, |
| 384 | get_order(io_tlb_nslabs * sizeof(phys_addr_t))); |
| 385 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * |
| 386 | sizeof(int))); |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 387 | free_pages((unsigned long)phys_to_virt(io_tlb_start), |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 388 | get_order(io_tlb_nslabs << IO_TLB_SHIFT)); |
| 389 | } else { |
Santosh Shilimkar | 457ff1d | 2014-01-21 15:50:30 -0800 | [diff] [blame] | 390 | memblock_free_late(__pa(io_tlb_orig_addr), |
| 391 | PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t))); |
| 392 | memblock_free_late(__pa(io_tlb_list), |
| 393 | PAGE_ALIGN(io_tlb_nslabs * sizeof(int))); |
| 394 | memblock_free_late(io_tlb_start, |
| 395 | PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 396 | } |
Christoph Hellwig | 227a76b | 2019-01-14 21:14:08 +0100 | [diff] [blame] | 397 | io_tlb_start = 0; |
| 398 | io_tlb_end = 0; |
Konrad Rzeszutek Wilk | f21ffe9 | 2011-08-11 16:50:56 -0400 | [diff] [blame] | 399 | io_tlb_nslabs = 0; |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 400 | max_segment = 0; |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 401 | } |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | /* |
Dongli Zhang | 6442ca2 | 2019-01-18 15:10:26 +0800 | [diff] [blame] | 404 | * 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] | 405 | */ |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 406 | static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr, |
| 407 | size_t size, enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 409 | unsigned long pfn = PFN_DOWN(orig_addr); |
| 410 | unsigned char *vaddr = phys_to_virt(tlb_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 412 | if (PageHighMem(pfn_to_page(pfn))) { |
| 413 | /* The buffer does not have a mapping. Map it in and copy */ |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 414 | unsigned int offset = orig_addr & ~PAGE_MASK; |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 415 | char *buffer; |
| 416 | unsigned int sz = 0; |
| 417 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 419 | while (size) { |
Becky Bruce | 67131ad | 2009-04-08 09:09:16 -0500 | [diff] [blame] | 420 | sz = min_t(size_t, PAGE_SIZE - offset, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 422 | local_irq_save(flags); |
Cong Wang | c3eede8 | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 423 | buffer = kmap_atomic(pfn_to_page(pfn)); |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 424 | if (dir == DMA_TO_DEVICE) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 425 | memcpy(vaddr, buffer + offset, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | else |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 427 | memcpy(buffer + offset, vaddr, sz); |
Cong Wang | c3eede8 | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 428 | kunmap_atomic(buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | local_irq_restore(flags); |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 430 | |
| 431 | size -= sz; |
| 432 | pfn++; |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 433 | vaddr += sz; |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 434 | offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 436 | } else if (dir == DMA_TO_DEVICE) { |
| 437 | memcpy(vaddr, phys_to_virt(orig_addr), size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } else { |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 439 | memcpy(phys_to_virt(orig_addr), vaddr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 443 | phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, |
| 444 | dma_addr_t tbl_dma_addr, |
| 445 | phys_addr_t orig_addr, size_t size, |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 446 | enum dma_data_direction dir, |
| 447 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
| 449 | unsigned long flags; |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 450 | phys_addr_t tlb_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | unsigned int nslots, stride, index, wrap; |
| 452 | int i; |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 453 | unsigned long mask; |
| 454 | unsigned long offset_slots; |
| 455 | unsigned long max_slots; |
Dongli Zhang | 53b29c3 | 2019-04-12 19:38:26 +0800 | [diff] [blame] | 456 | unsigned long tmp_io_tlb_used; |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 457 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 458 | if (no_iotlb_memory) |
| 459 | panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer"); |
| 460 | |
Tom Lendacky | d7b417f | 2017-10-20 09:30:53 -0500 | [diff] [blame] | 461 | if (mem_encrypt_active()) |
| 462 | pr_warn_once("%s is active and system is using DMA bounce buffers\n", |
| 463 | sme_active() ? "SME" : "SEV"); |
Tom Lendacky | 648babb | 2017-07-17 16:10:22 -0500 | [diff] [blame] | 464 | |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 465 | mask = dma_get_seg_boundary(hwdev); |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 466 | |
FUJITA Tomonori | eb605a5 | 2010-05-10 15:14:54 -0400 | [diff] [blame] | 467 | tbl_dma_addr &= mask; |
| 468 | |
| 469 | offset_slots = ALIGN(tbl_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; |
Ian Campbell | a5ddde4a | 2008-12-16 12:17:29 -0800 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * Carefully handle integer overflow which can occur when mask == ~0UL. |
| 473 | */ |
Jan Beulich | b15a3891 | 2008-03-13 09:13:30 +0000 | [diff] [blame] | 474 | max_slots = mask + 1 |
| 475 | ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT |
| 476 | : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | /* |
Nikita Yushchenko | 602d985 | 2017-01-11 21:56:31 +0300 | [diff] [blame] | 479 | * For mappings greater than or equal to a page, we limit the stride |
| 480 | * (and hence alignment) to a page size. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | */ |
| 482 | nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; |
Nikita Yushchenko | 602d985 | 2017-01-11 21:56:31 +0300 | [diff] [blame] | 483 | if (size >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT)); |
| 485 | else |
| 486 | stride = 1; |
| 487 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 488 | BUG_ON(!nslots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Find suitable number of IO TLB entries size that will fit this |
| 492 | * request and allocate a buffer from that IO TLB pool. |
| 493 | */ |
| 494 | spin_lock_irqsave(&io_tlb_lock, flags); |
Dongli Zhang | 60513ed | 2019-01-18 15:10:28 +0800 | [diff] [blame] | 495 | |
| 496 | if (unlikely(nslots > io_tlb_nslabs - io_tlb_used)) |
| 497 | goto not_found; |
| 498 | |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 499 | index = ALIGN(io_tlb_index, stride); |
| 500 | if (index >= io_tlb_nslabs) |
| 501 | index = 0; |
| 502 | wrap = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 504 | do { |
FUJITA Tomonori | a852250 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 505 | while (iommu_is_span_boundary(index, nslots, offset_slots, |
| 506 | max_slots)) { |
Jan Beulich | b15a3891 | 2008-03-13 09:13:30 +0000 | [diff] [blame] | 507 | index += stride; |
| 508 | if (index >= io_tlb_nslabs) |
| 509 | index = 0; |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 510 | if (index == wrap) |
| 511 | goto not_found; |
| 512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 514 | /* |
| 515 | * If we find a slot that indicates we have 'nslots' number of |
| 516 | * contiguous buffers, we allocate the buffers from that slot |
| 517 | * and mark the entries as '0' indicating unavailable. |
| 518 | */ |
| 519 | if (io_tlb_list[index] >= nslots) { |
| 520 | int count = 0; |
| 521 | |
| 522 | for (i = index; i < (int) (index + nslots); i++) |
| 523 | io_tlb_list[i] = 0; |
| 524 | for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--) |
| 525 | io_tlb_list[i] = ++count; |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 526 | tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT); |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 527 | |
| 528 | /* |
| 529 | * Update the indices to avoid searching in the next |
| 530 | * round. |
| 531 | */ |
| 532 | io_tlb_index = ((index + nslots) < io_tlb_nslabs |
| 533 | ? (index + nslots) : 0); |
| 534 | |
| 535 | goto found; |
| 536 | } |
| 537 | index += stride; |
| 538 | if (index >= io_tlb_nslabs) |
| 539 | index = 0; |
| 540 | } while (index != wrap); |
| 541 | |
| 542 | not_found: |
Dongli Zhang | 53b29c3 | 2019-04-12 19:38:26 +0800 | [diff] [blame] | 543 | tmp_io_tlb_used = io_tlb_used; |
| 544 | |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 545 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
Christian König | d0bc0c2 | 2018-01-04 14:24:19 +0100 | [diff] [blame] | 546 | if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit()) |
Dongli Zhang | 53b29c3 | 2019-04-12 19:38:26 +0800 | [diff] [blame] | 547 | dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n", |
| 548 | size, io_tlb_nslabs, tmp_io_tlb_used); |
Christoph Hellwig | b907e20 | 2018-12-03 11:42:52 +0100 | [diff] [blame] | 549 | return DMA_MAPPING_ERROR; |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 550 | found: |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 551 | io_tlb_used += nslots; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
| 553 | |
| 554 | /* |
| 555 | * Save away the mapping from the original address to the DMA address. |
| 556 | * This is needed when we sync the memory. Then we sync the buffer if |
| 557 | * needed. |
| 558 | */ |
Becky Bruce | bc40ac6 | 2008-12-22 10:26:08 -0800 | [diff] [blame] | 559 | for (i = 0; i < nslots; i++) |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 560 | io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT); |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 561 | if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && |
| 562 | (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 563 | swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 565 | return tlb_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* |
Yisheng Xie | d0c8ba4 | 2018-05-07 19:06:25 +0800 | [diff] [blame] | 569 | * tlb_addr is the physical address of the bounce buffer to unmap. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | */ |
Alexander Duyck | 61ca08c | 2012-10-15 10:19:44 -0700 | [diff] [blame] | 571 | void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr, |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 572 | size_t size, enum dma_data_direction dir, |
| 573 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | { |
| 575 | unsigned long flags; |
| 576 | int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; |
Alexander Duyck | 61ca08c | 2012-10-15 10:19:44 -0700 | [diff] [blame] | 577 | int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT; |
| 578 | phys_addr_t orig_addr = io_tlb_orig_addr[index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * First, sync the memory before unmapping the entry |
| 582 | */ |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 583 | if (orig_addr != INVALID_PHYS_ADDR && |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 584 | !(attrs & DMA_ATTR_SKIP_CPU_SYNC) && |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 585 | ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL))) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 586 | swiotlb_bounce(orig_addr, tlb_addr, size, DMA_FROM_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * Return the buffer to the free list by setting the corresponding |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 590 | * entries to indicate the number of contiguous entries available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | * While returning the entries to the free list, we merge the entries |
| 592 | * with slots below and above the pool being returned. |
| 593 | */ |
| 594 | spin_lock_irqsave(&io_tlb_lock, flags); |
| 595 | { |
| 596 | count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ? |
| 597 | io_tlb_list[index + nslots] : 0); |
| 598 | /* |
| 599 | * Step 1: return the slots to the free list, merging the |
| 600 | * slots with superceeding slots |
| 601 | */ |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 602 | for (i = index + nslots - 1; i >= index; i--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | io_tlb_list[i] = ++count; |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 604 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; |
| 605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | /* |
| 607 | * Step 2: merge the returned slots with the preceding slots, |
| 608 | * if available (non zero) |
| 609 | */ |
| 610 | for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--) |
| 611 | io_tlb_list[i] = ++count; |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 612 | |
| 613 | io_tlb_used -= nslots; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
| 616 | } |
| 617 | |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 618 | void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr, |
| 619 | size_t size, enum dma_data_direction dir, |
| 620 | enum dma_sync_target target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 622 | int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT; |
| 623 | phys_addr_t orig_addr = io_tlb_orig_addr[index]; |
Becky Bruce | bc40ac6 | 2008-12-22 10:26:08 -0800 | [diff] [blame] | 624 | |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 625 | if (orig_addr == INVALID_PHYS_ADDR) |
| 626 | return; |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 627 | orig_addr += (unsigned long)tlb_addr & ((1 << IO_TLB_SHIFT) - 1); |
Keir Fraser | df336d1 | 2007-07-21 04:37:24 -0700 | [diff] [blame] | 628 | |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 629 | switch (target) { |
| 630 | case SYNC_FOR_CPU: |
| 631 | if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 632 | swiotlb_bounce(orig_addr, tlb_addr, |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 633 | size, DMA_FROM_DEVICE); |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 634 | else |
| 635 | BUG_ON(dir != DMA_TO_DEVICE); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 636 | break; |
| 637 | case SYNC_FOR_DEVICE: |
| 638 | if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 639 | swiotlb_bounce(orig_addr, tlb_addr, |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 640 | size, DMA_TO_DEVICE); |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 641 | else |
| 642 | BUG_ON(dir != DMA_FROM_DEVICE); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 643 | break; |
| 644 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | BUG(); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 649 | /* |
| 650 | * Create a swiotlb mapping for the buffer at @phys, and in case of DMAing |
| 651 | * to the device copy the data into it as well. |
| 652 | */ |
| 653 | bool swiotlb_map(struct device *dev, phys_addr_t *phys, dma_addr_t *dma_addr, |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 654 | size_t size, enum dma_data_direction dir, unsigned long attrs) |
| 655 | { |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 656 | trace_swiotlb_bounced(dev, *dma_addr, size, swiotlb_force); |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 657 | |
| 658 | if (unlikely(swiotlb_force == SWIOTLB_NO_FORCE)) { |
| 659 | dev_warn_ratelimited(dev, |
| 660 | "Cannot do DMA to address %pa\n", phys); |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 661 | return false; |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* Oh well, have to allocate and map a bounce buffer. */ |
| 665 | *phys = swiotlb_tbl_map_single(dev, __phys_to_dma(dev, io_tlb_start), |
| 666 | *phys, size, dir, attrs); |
Christoph Hellwig | b907e20 | 2018-12-03 11:42:52 +0100 | [diff] [blame] | 667 | if (*phys == DMA_MAPPING_ERROR) |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 668 | return false; |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 669 | |
| 670 | /* Ensure that the address returned is DMA'ble */ |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 671 | *dma_addr = __phys_to_dma(dev, *phys); |
| 672 | if (unlikely(!dma_capable(dev, *dma_addr, size))) { |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 673 | swiotlb_tbl_unmap_single(dev, *phys, size, dir, |
| 674 | attrs | DMA_ATTR_SKIP_CPU_SYNC); |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 675 | return false; |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame] | 676 | } |
| 677 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 678 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Joerg Roedel | abe420b | 2019-02-07 12:59:13 +0100 | [diff] [blame] | 681 | size_t swiotlb_max_mapping_size(struct device *dev) |
| 682 | { |
| 683 | return ((size_t)1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE; |
| 684 | } |
Joerg Roedel | 492366f | 2019-02-07 12:59:14 +0100 | [diff] [blame] | 685 | |
| 686 | bool is_swiotlb_active(void) |
| 687 | { |
| 688 | /* |
| 689 | * When SWIOTLB is initialized, even if io_tlb_start points to physical |
| 690 | * address zero, io_tlb_end surely doesn't. |
| 691 | */ |
| 692 | return io_tlb_end != 0; |
| 693 | } |
Linus Torvalds | 45ba8d5 | 2019-03-10 12:47:57 -0700 | [diff] [blame] | 694 | |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 695 | #ifdef CONFIG_DEBUG_FS |
| 696 | |
| 697 | static int __init swiotlb_create_debugfs(void) |
| 698 | { |
YueHaibing | 22cb45d | 2019-02-14 09:04:08 +0000 | [diff] [blame] | 699 | struct dentry *d_swiotlb_usage; |
Dongli Zhang | 71602fe | 2019-01-18 15:10:27 +0800 | [diff] [blame] | 700 | struct dentry *ent; |
| 701 | |
| 702 | d_swiotlb_usage = debugfs_create_dir("swiotlb", NULL); |
| 703 | |
| 704 | if (!d_swiotlb_usage) |
| 705 | return -ENOMEM; |
| 706 | |
| 707 | ent = debugfs_create_ulong("io_tlb_nslabs", 0400, |
| 708 | d_swiotlb_usage, &io_tlb_nslabs); |
| 709 | if (!ent) |
| 710 | goto fail; |
| 711 | |
| 712 | ent = debugfs_create_ulong("io_tlb_used", 0400, |
| 713 | d_swiotlb_usage, &io_tlb_used); |
| 714 | if (!ent) |
| 715 | goto fail; |
| 716 | |
| 717 | return 0; |
| 718 | |
| 719 | fail: |
| 720 | debugfs_remove_recursive(d_swiotlb_usage); |
| 721 | return -ENOMEM; |
| 722 | } |
| 723 | |
| 724 | late_initcall(swiotlb_create_debugfs); |
| 725 | |
| 726 | #endif |