Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Dynamic DMA mapping support. |
| 3 | * |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 4 | * This implementation is a fallback for platforms that do not support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * I/O TLBs (aka DMA address translation hardware). |
| 6 | * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com> |
| 7 | * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com> |
| 8 | * Copyright (C) 2000, 2003 Hewlett-Packard Co |
| 9 | * David Mosberger-Tang <davidm@hpl.hp.com> |
| 10 | * |
| 11 | * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API. |
| 12 | * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid |
| 13 | * unnecessary i-cache flushing. |
John W. Linville | 569c8bf | 2005-09-29 14:45:24 -0700 | [diff] [blame] | 14 | * 04/07/.. ak Better overflow handling. Assorted fixes. |
| 15 | * 05/09/10 linville Add support for syncing ranges, support syncing for |
| 16 | * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup. |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 17 | * 08/12/11 beckyb Add highmem support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 20 | #define pr_fmt(fmt) "software IO TLB: " fmt |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/cache.h> |
Christoph Hellwig | ea8c64a | 2018-01-10 16:21:13 +0100 | [diff] [blame] | 23 | #include <linux/dma-direct.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/mm.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 25 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/string.h> |
Ian Campbell | 0016fde | 2008-12-16 12:17:27 -0800 | [diff] [blame] | 28 | #include <linux/swiotlb.h> |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 29 | #include <linux/pfn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/ctype.h> |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame] | 32 | #include <linux/highmem.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/gfp.h> |
Christoph Hellwig | 84be456 | 2015-05-01 12:46:15 +0200 | [diff] [blame] | 34 | #include <linux/scatterlist.h> |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 35 | #include <linux/mem_encrypt.h> |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 36 | #include <linux/set_memory.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/dma.h> |
| 40 | |
| 41 | #include <linux/init.h> |
| 42 | #include <linux/bootmem.h> |
FUJITA Tomonori | a852250 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 43 | #include <linux/iommu-helper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Thierry Reding | ce5be5a | 2013-10-23 13:32:04 +0200 | [diff] [blame] | 45 | #define CREATE_TRACE_POINTS |
Zoltan Kiss | 2b2b614 | 2013-09-04 21:11:05 +0100 | [diff] [blame] | 46 | #include <trace/events/swiotlb.h> |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define OFFSET(val,align) ((unsigned long) \ |
| 49 | ( (val) & ( (align) - 1))) |
| 50 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 51 | #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT)) |
| 52 | |
| 53 | /* |
| 54 | * Minimum IO TLB size to bother booting with. Systems with mainly |
| 55 | * 64bit capable cards will only lightly use the swiotlb. If we can't |
| 56 | * allocate a contiguous 1MB, we're probably in trouble anyway. |
| 57 | */ |
| 58 | #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT) |
| 59 | |
Geert Uytterhoeven | ae7871b | 2016-12-16 14:28:41 +0100 | [diff] [blame] | 60 | enum swiotlb_force swiotlb_force; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
Konrad Rzeszutek Wilk | bfc5501 | 2010-05-10 15:54:20 -0400 | [diff] [blame] | 63 | * Used to do a quick range check in swiotlb_tbl_unmap_single and |
| 64 | * 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] | 65 | * API. |
| 66 | */ |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 67 | static phys_addr_t io_tlb_start, io_tlb_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 70 | * 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] | 71 | * io_tlb_end. This is command line adjustable via setup_io_tlb_npages. |
| 72 | */ |
| 73 | static unsigned long io_tlb_nslabs; |
| 74 | |
| 75 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | * This is a free list describing the number of free entries available from |
| 77 | * each index |
| 78 | */ |
| 79 | static unsigned int *io_tlb_list; |
| 80 | static unsigned int io_tlb_index; |
| 81 | |
| 82 | /* |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 83 | * Max segment that we can provide which (if pages are contingous) will |
| 84 | * not be bounced (unless SWIOTLB_FORCE is set). |
| 85 | */ |
| 86 | unsigned int max_segment; |
| 87 | |
| 88 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * We need to save away the original address corresponding to a mapped entry |
| 90 | * for the sync operations. |
| 91 | */ |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 92 | #define INVALID_PHYS_ADDR (~(phys_addr_t)0) |
Becky Bruce | bc40ac6 | 2008-12-22 10:26:08 -0800 | [diff] [blame] | 93 | static phys_addr_t *io_tlb_orig_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Protect the above data structures in the map and unmap calls |
| 97 | */ |
| 98 | static DEFINE_SPINLOCK(io_tlb_lock); |
| 99 | |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 100 | static int late_alloc; |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | static int __init |
| 103 | setup_io_tlb_npages(char *str) |
| 104 | { |
| 105 | if (isdigit(*str)) { |
Alex Williamson | e8579e7 | 2005-08-04 13:06:00 -0700 | [diff] [blame] | 106 | io_tlb_nslabs = simple_strtoul(str, &str, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* avoid tail segment of size < IO_TLB_SEGSIZE */ |
| 108 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); |
| 109 | } |
| 110 | if (*str == ',') |
| 111 | ++str; |
Geert Uytterhoeven | fff5d99 | 2016-12-16 14:28:42 +0100 | [diff] [blame] | 112 | if (!strcmp(str, "force")) { |
Geert Uytterhoeven | ae7871b | 2016-12-16 14:28:41 +0100 | [diff] [blame] | 113 | swiotlb_force = SWIOTLB_FORCE; |
Geert Uytterhoeven | fff5d99 | 2016-12-16 14:28:42 +0100 | [diff] [blame] | 114 | } else if (!strcmp(str, "noforce")) { |
| 115 | swiotlb_force = SWIOTLB_NO_FORCE; |
| 116 | io_tlb_nslabs = 1; |
| 117 | } |
FUJITA Tomonori | b18485e | 2009-11-12 00:03:28 +0900 | [diff] [blame] | 118 | |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 119 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 121 | early_param("swiotlb", setup_io_tlb_npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Konrad Rzeszutek Wilk | f21ffe9 | 2011-08-11 16:50:56 -0400 | [diff] [blame] | 123 | unsigned long swiotlb_nr_tbl(void) |
FUJITA Tomonori | 5f98ecd | 2011-06-05 11:47:29 +0900 | [diff] [blame] | 124 | { |
| 125 | return io_tlb_nslabs; |
| 126 | } |
Konrad Rzeszutek Wilk | f21ffe9 | 2011-08-11 16:50:56 -0400 | [diff] [blame] | 127 | EXPORT_SYMBOL_GPL(swiotlb_nr_tbl); |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 128 | |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 129 | unsigned int swiotlb_max_segment(void) |
| 130 | { |
| 131 | return max_segment; |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(swiotlb_max_segment); |
| 134 | |
| 135 | void swiotlb_set_max_segment(unsigned int val) |
| 136 | { |
| 137 | if (swiotlb_force == SWIOTLB_FORCE) |
| 138 | max_segment = 1; |
| 139 | else |
| 140 | max_segment = rounddown(val, PAGE_SIZE); |
| 141 | } |
| 142 | |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 143 | /* default to 64MB */ |
| 144 | #define IO_TLB_DEFAULT_SIZE (64UL<<20) |
| 145 | unsigned long swiotlb_size_or_default(void) |
| 146 | { |
| 147 | unsigned long size; |
| 148 | |
| 149 | size = io_tlb_nslabs << IO_TLB_SHIFT; |
| 150 | |
| 151 | return size ? size : (IO_TLB_DEFAULT_SIZE); |
| 152 | } |
| 153 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 154 | static bool no_iotlb_memory; |
| 155 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 156 | void swiotlb_print_info(void) |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 157 | { |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 158 | unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 159 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 160 | if (no_iotlb_memory) { |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 161 | pr_warn("No low mem\n"); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 165 | pr_info("mapped [mem %#010llx-%#010llx] (%luMB)\n", |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 166 | (unsigned long long)io_tlb_start, |
Alexander Duyck | c40dba0 | 2012-10-15 10:19:22 -0700 | [diff] [blame] | 167 | (unsigned long long)io_tlb_end, |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 168 | bytes >> 20); |
Ian Campbell | 2e5b2b8 | 2008-12-16 12:17:34 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 171 | /* |
| 172 | * Early SWIOTLB allocation may be too early to allow an architecture to |
| 173 | * perform the desired operations. This function allows the architecture to |
| 174 | * call SWIOTLB when the operations are possible. It needs to be called |
| 175 | * before the SWIOTLB memory is used. |
| 176 | */ |
| 177 | void __init swiotlb_update_mem_attributes(void) |
| 178 | { |
| 179 | void *vaddr; |
| 180 | unsigned long bytes; |
| 181 | |
| 182 | if (no_iotlb_memory || late_alloc) |
| 183 | return; |
| 184 | |
| 185 | vaddr = phys_to_virt(io_tlb_start); |
| 186 | bytes = PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT); |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 187 | set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT); |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 188 | memset(vaddr, 0, bytes); |
Tom Lendacky | c775320 | 2017-07-17 16:10:21 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 191 | 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] | 192 | { |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 193 | unsigned long i, bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 195 | bytes = nslabs << IO_TLB_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 197 | io_tlb_nslabs = nslabs; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 198 | io_tlb_start = __pa(tlb); |
| 199 | io_tlb_end = io_tlb_start + bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * Allocate and initialize the free list array. This array is used |
| 203 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE |
| 204 | * between io_tlb_start and io_tlb_end. |
| 205 | */ |
Santosh Shilimkar | 457ff1d | 2014-01-21 15:50:30 -0800 | [diff] [blame] | 206 | io_tlb_list = memblock_virt_alloc( |
| 207 | PAGE_ALIGN(io_tlb_nslabs * sizeof(int)), |
| 208 | PAGE_SIZE); |
Santosh Shilimkar | 457ff1d | 2014-01-21 15:50:30 -0800 | [diff] [blame] | 209 | io_tlb_orig_addr = memblock_virt_alloc( |
| 210 | PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)), |
| 211 | PAGE_SIZE); |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 212 | for (i = 0; i < io_tlb_nslabs; i++) { |
| 213 | io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE); |
| 214 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; |
| 215 | } |
| 216 | io_tlb_index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 218 | if (verbose) |
| 219 | swiotlb_print_info(); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 220 | |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 221 | swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 222 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 225 | /* |
| 226 | * Statically reserve bounce buffer space and initialize bounce buffer data |
| 227 | * structures for the software IO TLB used to implement the DMA API. |
| 228 | */ |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 229 | void __init |
| 230 | swiotlb_init(int verbose) |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 231 | { |
Yinghai Lu | c729de8 | 2013-04-15 22:23:45 -0700 | [diff] [blame] | 232 | size_t default_size = IO_TLB_DEFAULT_SIZE; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 233 | unsigned char *vstart; |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 234 | unsigned long bytes; |
| 235 | |
| 236 | if (!io_tlb_nslabs) { |
| 237 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); |
| 238 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); |
| 239 | } |
| 240 | |
| 241 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
| 242 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 243 | /* Get IO TLB memory from the low pages */ |
Yinghai Lu | ad6492b | 2014-01-27 17:06:49 -0800 | [diff] [blame] | 244 | vstart = memblock_virt_alloc_low_nopanic(PAGE_ALIGN(bytes), PAGE_SIZE); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 245 | if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose)) |
| 246 | return; |
FUJITA Tomonori | abbceff | 2010-05-10 15:15:12 -0400 | [diff] [blame] | 247 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 248 | if (io_tlb_start) |
Santosh Shilimkar | 457ff1d | 2014-01-21 15:50:30 -0800 | [diff] [blame] | 249 | memblock_free_early(io_tlb_start, |
| 250 | PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 251 | pr_warn("Cannot allocate buffer"); |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 252 | no_iotlb_memory = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 255 | /* |
| 256 | * Systems with larger DMA zones (those that don't support ISA) can |
| 257 | * initialize the swiotlb later using the slab allocator if needed. |
| 258 | * This should be just like above, but with some error catching. |
| 259 | */ |
| 260 | int |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 261 | swiotlb_late_init_with_default_size(size_t default_size) |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 262 | { |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 263 | unsigned long bytes, req_nslabs = io_tlb_nslabs; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 264 | unsigned char *vstart = NULL; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 265 | unsigned int order; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 266 | int rc = 0; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 267 | |
| 268 | if (!io_tlb_nslabs) { |
| 269 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); |
| 270 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Get IO TLB memory from the low pages |
| 275 | */ |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 276 | order = get_order(io_tlb_nslabs << IO_TLB_SHIFT); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 277 | io_tlb_nslabs = SLABS_PER_PAGE << order; |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 278 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 279 | |
| 280 | while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 281 | vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, |
| 282 | order); |
| 283 | if (vstart) |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 284 | break; |
| 285 | order--; |
| 286 | } |
| 287 | |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 288 | if (!vstart) { |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 289 | io_tlb_nslabs = req_nslabs; |
| 290 | return -ENOMEM; |
| 291 | } |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 292 | if (order != get_order(bytes)) { |
Kees Cook | 7d63fb3 | 2018-07-10 16:22:22 -0700 | [diff] [blame] | 293 | pr_warn("only able to allocate %ld MB\n", |
| 294 | (PAGE_SIZE << order) >> 20); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 295 | io_tlb_nslabs = SLABS_PER_PAGE << order; |
| 296 | } |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 297 | rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs); |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 298 | if (rc) |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 299 | free_pages((unsigned long)vstart, order); |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 300 | |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 301 | return rc; |
| 302 | } |
| 303 | |
| 304 | int |
| 305 | swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) |
| 306 | { |
| 307 | unsigned long i, bytes; |
| 308 | |
| 309 | bytes = nslabs << IO_TLB_SHIFT; |
| 310 | |
| 311 | io_tlb_nslabs = nslabs; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 312 | io_tlb_start = virt_to_phys(tlb); |
| 313 | io_tlb_end = io_tlb_start + bytes; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 314 | |
Christoph Hellwig | e7de6c7 | 2018-03-19 11:38:23 +0100 | [diff] [blame] | 315 | set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT); |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 316 | memset(tlb, 0, bytes); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * Allocate and initialize the free list array. This array is used |
| 320 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE |
| 321 | * between io_tlb_start and io_tlb_end. |
| 322 | */ |
| 323 | io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL, |
| 324 | get_order(io_tlb_nslabs * sizeof(int))); |
| 325 | if (!io_tlb_list) |
Alexander Duyck | ee3f6ba | 2012-10-15 10:19:34 -0700 | [diff] [blame] | 326 | goto cleanup3; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 327 | |
Becky Bruce | bc40ac6 | 2008-12-22 10:26:08 -0800 | [diff] [blame] | 328 | io_tlb_orig_addr = (phys_addr_t *) |
| 329 | __get_free_pages(GFP_KERNEL, |
| 330 | get_order(io_tlb_nslabs * |
| 331 | sizeof(phys_addr_t))); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 332 | if (!io_tlb_orig_addr) |
Alexander Duyck | ee3f6ba | 2012-10-15 10:19:34 -0700 | [diff] [blame] | 333 | goto cleanup4; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 334 | |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 335 | for (i = 0; i < io_tlb_nslabs; i++) { |
| 336 | io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE); |
| 337 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; |
| 338 | } |
| 339 | io_tlb_index = 0; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 340 | |
FUJITA Tomonori | ad32e8c | 2009-11-10 19:46:19 +0900 | [diff] [blame] | 341 | swiotlb_print_info(); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 342 | |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 343 | late_alloc = 1; |
| 344 | |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 345 | swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT); |
| 346 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 347 | return 0; |
| 348 | |
| 349 | cleanup4: |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 350 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * |
| 351 | sizeof(int))); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 352 | io_tlb_list = NULL; |
Alexander Duyck | ee3f6ba | 2012-10-15 10:19:34 -0700 | [diff] [blame] | 353 | cleanup3: |
Alexander Duyck | c40dba0 | 2012-10-15 10:19:22 -0700 | [diff] [blame] | 354 | io_tlb_end = 0; |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 355 | io_tlb_start = 0; |
Konrad Rzeszutek Wilk | 74838b7 | 2012-07-27 20:55:27 -0400 | [diff] [blame] | 356 | io_tlb_nslabs = 0; |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 357 | max_segment = 0; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 358 | return -ENOMEM; |
| 359 | } |
| 360 | |
Christoph Hellwig | 7f2c8bb | 2017-12-23 14:14:54 +0100 | [diff] [blame] | 361 | void __init swiotlb_exit(void) |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 362 | { |
Alexander Duyck | ee3f6ba | 2012-10-15 10:19:34 -0700 | [diff] [blame] | 363 | if (!io_tlb_orig_addr) |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 364 | return; |
| 365 | |
| 366 | if (late_alloc) { |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 367 | free_pages((unsigned long)io_tlb_orig_addr, |
| 368 | get_order(io_tlb_nslabs * sizeof(phys_addr_t))); |
| 369 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * |
| 370 | sizeof(int))); |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 371 | free_pages((unsigned long)phys_to_virt(io_tlb_start), |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 372 | get_order(io_tlb_nslabs << IO_TLB_SHIFT)); |
| 373 | } else { |
Santosh Shilimkar | 457ff1d | 2014-01-21 15:50:30 -0800 | [diff] [blame] | 374 | memblock_free_late(__pa(io_tlb_orig_addr), |
| 375 | PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t))); |
| 376 | memblock_free_late(__pa(io_tlb_list), |
| 377 | PAGE_ALIGN(io_tlb_nslabs * sizeof(int))); |
| 378 | memblock_free_late(io_tlb_start, |
| 379 | PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 380 | } |
Konrad Rzeszutek Wilk | f21ffe9 | 2011-08-11 16:50:56 -0400 | [diff] [blame] | 381 | io_tlb_nslabs = 0; |
Konrad Rzeszutek Wilk | 7453c54 | 2016-12-20 10:02:02 -0500 | [diff] [blame] | 382 | max_segment = 0; |
FUJITA Tomonori | 5740afd | 2009-11-10 19:46:18 +0900 | [diff] [blame] | 383 | } |
| 384 | |
Christoph Hellwig | b65125c | 2018-04-12 14:49:23 +0200 | [diff] [blame] | 385 | static int is_swiotlb_buffer(phys_addr_t paddr) |
FUJITA Tomonori | 640aebf | 2008-09-08 18:53:50 +0900 | [diff] [blame] | 386 | { |
Alexander Duyck | ff7204a | 2012-10-15 10:19:28 -0700 | [diff] [blame] | 387 | return paddr >= io_tlb_start && paddr < io_tlb_end; |
FUJITA Tomonori | 640aebf | 2008-09-08 18:53:50 +0900 | [diff] [blame] | 388 | } |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | /* |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 391 | * Bounce: copy the swiotlb buffer back to the original dma location |
| 392 | */ |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 393 | static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr, |
| 394 | size_t size, enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 396 | unsigned long pfn = PFN_DOWN(orig_addr); |
| 397 | unsigned char *vaddr = phys_to_virt(tlb_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 399 | if (PageHighMem(pfn_to_page(pfn))) { |
| 400 | /* The buffer does not have a mapping. Map it in and copy */ |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 401 | unsigned int offset = orig_addr & ~PAGE_MASK; |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 402 | char *buffer; |
| 403 | unsigned int sz = 0; |
| 404 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 406 | while (size) { |
Becky Bruce | 67131ad | 2009-04-08 09:09:16 -0500 | [diff] [blame] | 407 | sz = min_t(size_t, PAGE_SIZE - offset, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 409 | local_irq_save(flags); |
Cong Wang | c3eede8 | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 410 | buffer = kmap_atomic(pfn_to_page(pfn)); |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 411 | if (dir == DMA_TO_DEVICE) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 412 | memcpy(vaddr, buffer + offset, sz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | else |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 414 | memcpy(buffer + offset, vaddr, sz); |
Cong Wang | c3eede8 | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 415 | kunmap_atomic(buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | local_irq_restore(flags); |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 417 | |
| 418 | size -= sz; |
| 419 | pfn++; |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 420 | vaddr += sz; |
Becky Bruce | fb05a37 | 2008-12-22 10:26:09 -0800 | [diff] [blame] | 421 | offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 423 | } else if (dir == DMA_TO_DEVICE) { |
| 424 | memcpy(vaddr, phys_to_virt(orig_addr), size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } else { |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 426 | memcpy(phys_to_virt(orig_addr), vaddr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 430 | phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, |
| 431 | dma_addr_t tbl_dma_addr, |
| 432 | phys_addr_t orig_addr, size_t size, |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 433 | enum dma_data_direction dir, |
| 434 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
| 436 | unsigned long flags; |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 437 | phys_addr_t tlb_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | unsigned int nslots, stride, index, wrap; |
| 439 | int i; |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 440 | unsigned long mask; |
| 441 | unsigned long offset_slots; |
| 442 | unsigned long max_slots; |
| 443 | |
Yinghai Lu | ac2cbab | 2013-01-24 12:20:16 -0800 | [diff] [blame] | 444 | if (no_iotlb_memory) |
| 445 | panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer"); |
| 446 | |
Tom Lendacky | d7b417f | 2017-10-20 09:30:53 -0500 | [diff] [blame] | 447 | if (mem_encrypt_active()) |
| 448 | pr_warn_once("%s is active and system is using DMA bounce buffers\n", |
| 449 | sme_active() ? "SME" : "SEV"); |
Tom Lendacky | 648babb | 2017-07-17 16:10:22 -0500 | [diff] [blame] | 450 | |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 451 | mask = dma_get_seg_boundary(hwdev); |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 452 | |
FUJITA Tomonori | eb605a5 | 2010-05-10 15:14:54 -0400 | [diff] [blame] | 453 | tbl_dma_addr &= mask; |
| 454 | |
| 455 | 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] | 456 | |
| 457 | /* |
| 458 | * Carefully handle integer overflow which can occur when mask == ~0UL. |
| 459 | */ |
Jan Beulich | b15a3891 | 2008-03-13 09:13:30 +0000 | [diff] [blame] | 460 | max_slots = mask + 1 |
| 461 | ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT |
| 462 | : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | /* |
Nikita Yushchenko | 602d985 | 2017-01-11 21:56:31 +0300 | [diff] [blame] | 465 | * For mappings greater than or equal to a page, we limit the stride |
| 466 | * (and hence alignment) to a page size. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | */ |
| 468 | nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; |
Nikita Yushchenko | 602d985 | 2017-01-11 21:56:31 +0300 | [diff] [blame] | 469 | if (size >= PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT)); |
| 471 | else |
| 472 | stride = 1; |
| 473 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 474 | BUG_ON(!nslots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | /* |
| 477 | * Find suitable number of IO TLB entries size that will fit this |
| 478 | * request and allocate a buffer from that IO TLB pool. |
| 479 | */ |
| 480 | spin_lock_irqsave(&io_tlb_lock, flags); |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 481 | index = ALIGN(io_tlb_index, stride); |
| 482 | if (index >= io_tlb_nslabs) |
| 483 | index = 0; |
| 484 | wrap = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 486 | do { |
FUJITA Tomonori | a852250 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 487 | while (iommu_is_span_boundary(index, nslots, offset_slots, |
| 488 | max_slots)) { |
Jan Beulich | b15a3891 | 2008-03-13 09:13:30 +0000 | [diff] [blame] | 489 | index += stride; |
| 490 | if (index >= io_tlb_nslabs) |
| 491 | index = 0; |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 492 | if (index == wrap) |
| 493 | goto not_found; |
| 494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 496 | /* |
| 497 | * If we find a slot that indicates we have 'nslots' number of |
| 498 | * contiguous buffers, we allocate the buffers from that slot |
| 499 | * and mark the entries as '0' indicating unavailable. |
| 500 | */ |
| 501 | if (io_tlb_list[index] >= nslots) { |
| 502 | int count = 0; |
| 503 | |
| 504 | for (i = index; i < (int) (index + nslots); i++) |
| 505 | io_tlb_list[i] = 0; |
| 506 | for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--) |
| 507 | io_tlb_list[i] = ++count; |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 508 | tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT); |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | * Update the indices to avoid searching in the next |
| 512 | * round. |
| 513 | */ |
| 514 | io_tlb_index = ((index + nslots) < io_tlb_nslabs |
| 515 | ? (index + nslots) : 0); |
| 516 | |
| 517 | goto found; |
| 518 | } |
| 519 | index += stride; |
| 520 | if (index >= io_tlb_nslabs) |
| 521 | index = 0; |
| 522 | } while (index != wrap); |
| 523 | |
| 524 | not_found: |
| 525 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
Christian König | d0bc0c2 | 2018-01-04 14:24:19 +0100 | [diff] [blame] | 526 | if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit()) |
Konrad Rzeszutek Wilk | 0cb637b | 2013-12-16 14:05:01 -0500 | [diff] [blame] | 527 | dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes)\n", size); |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 528 | return SWIOTLB_MAP_ERROR; |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 529 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
| 531 | |
| 532 | /* |
| 533 | * Save away the mapping from the original address to the DMA address. |
| 534 | * This is needed when we sync the memory. Then we sync the buffer if |
| 535 | * needed. |
| 536 | */ |
Becky Bruce | bc40ac6 | 2008-12-22 10:26:08 -0800 | [diff] [blame] | 537 | for (i = 0; i < nslots; i++) |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 538 | io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT); |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 539 | if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && |
| 540 | (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 541 | swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Alexander Duyck | e05ed4d | 2012-10-15 10:19:39 -0700 | [diff] [blame] | 543 | return tlb_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* |
Yisheng Xie | d0c8ba4 | 2018-05-07 19:06:25 +0800 | [diff] [blame] | 547 | * tlb_addr is the physical address of the bounce buffer to unmap. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | */ |
Alexander Duyck | 61ca08c | 2012-10-15 10:19:44 -0700 | [diff] [blame] | 549 | 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] | 550 | size_t size, enum dma_data_direction dir, |
| 551 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | unsigned long flags; |
| 554 | 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] | 555 | int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT; |
| 556 | phys_addr_t orig_addr = io_tlb_orig_addr[index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
| 558 | /* |
| 559 | * First, sync the memory before unmapping the entry |
| 560 | */ |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 561 | if (orig_addr != INVALID_PHYS_ADDR && |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 562 | !(attrs & DMA_ATTR_SKIP_CPU_SYNC) && |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 563 | ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL))) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 564 | swiotlb_bounce(orig_addr, tlb_addr, size, DMA_FROM_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | /* |
| 567 | * Return the buffer to the free list by setting the corresponding |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 568 | * entries to indicate the number of contiguous entries available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | * While returning the entries to the free list, we merge the entries |
| 570 | * with slots below and above the pool being returned. |
| 571 | */ |
| 572 | spin_lock_irqsave(&io_tlb_lock, flags); |
| 573 | { |
| 574 | count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ? |
| 575 | io_tlb_list[index + nslots] : 0); |
| 576 | /* |
| 577 | * Step 1: return the slots to the free list, merging the |
| 578 | * slots with superceeding slots |
| 579 | */ |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 580 | for (i = index + nslots - 1; i >= index; i--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | io_tlb_list[i] = ++count; |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 582 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; |
| 583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | /* |
| 585 | * Step 2: merge the returned slots with the preceding slots, |
| 586 | * if available (non zero) |
| 587 | */ |
| 588 | for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--) |
| 589 | io_tlb_list[i] = ++count; |
| 590 | } |
| 591 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
| 592 | } |
| 593 | |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 594 | void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr, |
| 595 | size_t size, enum dma_data_direction dir, |
| 596 | enum dma_sync_target target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 598 | int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT; |
| 599 | phys_addr_t orig_addr = io_tlb_orig_addr[index]; |
Becky Bruce | bc40ac6 | 2008-12-22 10:26:08 -0800 | [diff] [blame] | 600 | |
Jan Beulich | 8e0629c | 2014-06-02 14:58:25 +0100 | [diff] [blame] | 601 | if (orig_addr == INVALID_PHYS_ADDR) |
| 602 | return; |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 603 | orig_addr += (unsigned long)tlb_addr & ((1 << IO_TLB_SHIFT) - 1); |
Keir Fraser | df336d1 | 2007-07-21 04:37:24 -0700 | [diff] [blame] | 604 | |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 605 | switch (target) { |
| 606 | case SYNC_FOR_CPU: |
| 607 | if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 608 | swiotlb_bounce(orig_addr, tlb_addr, |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 609 | size, DMA_FROM_DEVICE); |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 610 | else |
| 611 | BUG_ON(dir != DMA_TO_DEVICE); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 612 | break; |
| 613 | case SYNC_FOR_DEVICE: |
| 614 | if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Alexander Duyck | af51a9f | 2012-10-15 10:19:55 -0700 | [diff] [blame] | 615 | swiotlb_bounce(orig_addr, tlb_addr, |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 616 | size, DMA_TO_DEVICE); |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 617 | else |
| 618 | BUG_ON(dir != DMA_FROM_DEVICE); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 619 | break; |
| 620 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | BUG(); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 622 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Christoph Hellwig | 0176adb | 2018-01-09 22:15:30 +0100 | [diff] [blame] | 625 | static inline bool dma_coherent_ok(struct device *dev, dma_addr_t addr, |
| 626 | size_t size) |
| 627 | { |
| 628 | u64 mask = DMA_BIT_MASK(32); |
| 629 | |
| 630 | if (dev && dev->coherent_dma_mask) |
| 631 | mask = dev->coherent_dma_mask; |
| 632 | return addr + size - 1 <= mask; |
| 633 | } |
| 634 | |
| 635 | static void * |
| 636 | swiotlb_alloc_buffer(struct device *dev, size_t size, dma_addr_t *dma_handle, |
| 637 | unsigned long attrs) |
| 638 | { |
| 639 | phys_addr_t phys_addr; |
| 640 | |
| 641 | if (swiotlb_force == SWIOTLB_NO_FORCE) |
| 642 | goto out_warn; |
| 643 | |
| 644 | phys_addr = swiotlb_tbl_map_single(dev, |
Christoph Hellwig | b6e0547 | 2018-03-19 11:38:24 +0100 | [diff] [blame] | 645 | __phys_to_dma(dev, io_tlb_start), |
Jean Delvare | 05e13bb | 2018-05-12 11:57:37 +0200 | [diff] [blame] | 646 | 0, size, DMA_FROM_DEVICE, attrs); |
Christoph Hellwig | 0176adb | 2018-01-09 22:15:30 +0100 | [diff] [blame] | 647 | if (phys_addr == SWIOTLB_MAP_ERROR) |
| 648 | goto out_warn; |
| 649 | |
Christoph Hellwig | b6e0547 | 2018-03-19 11:38:24 +0100 | [diff] [blame] | 650 | *dma_handle = __phys_to_dma(dev, phys_addr); |
Takashi Iwai | 9e7f06c | 2018-04-10 19:05:13 +0200 | [diff] [blame] | 651 | if (!dma_coherent_ok(dev, *dma_handle, size)) |
Christoph Hellwig | 0176adb | 2018-01-09 22:15:30 +0100 | [diff] [blame] | 652 | goto out_unmap; |
| 653 | |
| 654 | memset(phys_to_virt(phys_addr), 0, size); |
| 655 | return phys_to_virt(phys_addr); |
| 656 | |
| 657 | out_unmap: |
| 658 | dev_warn(dev, "hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n", |
Dan Carpenter | 698733f | 2018-04-05 14:19:11 +0300 | [diff] [blame] | 659 | (unsigned long long)dev->coherent_dma_mask, |
Christoph Hellwig | 0176adb | 2018-01-09 22:15:30 +0100 | [diff] [blame] | 660 | (unsigned long long)*dma_handle); |
| 661 | |
| 662 | /* |
| 663 | * DMA_TO_DEVICE to avoid memcpy in unmap_single. |
| 664 | * DMA_ATTR_SKIP_CPU_SYNC is optional. |
| 665 | */ |
| 666 | swiotlb_tbl_unmap_single(dev, phys_addr, size, DMA_TO_DEVICE, |
| 667 | DMA_ATTR_SKIP_CPU_SYNC); |
| 668 | out_warn: |
Michel Dänzer | 892a0be | 2018-05-01 15:24:11 +0200 | [diff] [blame] | 669 | if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit()) { |
Christoph Hellwig | 0176adb | 2018-01-09 22:15:30 +0100 | [diff] [blame] | 670 | dev_warn(dev, |
| 671 | "swiotlb: coherent allocation failed, size=%zu\n", |
| 672 | size); |
| 673 | dump_stack(); |
| 674 | } |
| 675 | return NULL; |
| 676 | } |
| 677 | |
Christoph Hellwig | a25381a | 2017-12-23 20:56:02 +0100 | [diff] [blame] | 678 | static bool swiotlb_free_buffer(struct device *dev, size_t size, |
| 679 | dma_addr_t dma_addr) |
| 680 | { |
| 681 | phys_addr_t phys_addr = dma_to_phys(dev, dma_addr); |
| 682 | |
| 683 | WARN_ON_ONCE(irqs_disabled()); |
| 684 | |
| 685 | if (!is_swiotlb_buffer(phys_addr)) |
| 686 | return false; |
| 687 | |
| 688 | /* |
| 689 | * DMA_TO_DEVICE to avoid memcpy in swiotlb_tbl_unmap_single. |
| 690 | * DMA_ATTR_SKIP_CPU_SYNC is optional. |
| 691 | */ |
| 692 | swiotlb_tbl_unmap_single(dev, phys_addr, size, DMA_TO_DEVICE, |
| 693 | DMA_ATTR_SKIP_CPU_SYNC); |
| 694 | return true; |
| 695 | } |
| 696 | |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame^] | 697 | static dma_addr_t swiotlb_bounce_page(struct device *dev, phys_addr_t *phys, |
| 698 | size_t size, enum dma_data_direction dir, unsigned long attrs) |
| 699 | { |
| 700 | dma_addr_t dma_addr; |
| 701 | |
| 702 | if (unlikely(swiotlb_force == SWIOTLB_NO_FORCE)) { |
| 703 | dev_warn_ratelimited(dev, |
| 704 | "Cannot do DMA to address %pa\n", phys); |
| 705 | return DIRECT_MAPPING_ERROR; |
| 706 | } |
| 707 | |
| 708 | /* Oh well, have to allocate and map a bounce buffer. */ |
| 709 | *phys = swiotlb_tbl_map_single(dev, __phys_to_dma(dev, io_tlb_start), |
| 710 | *phys, size, dir, attrs); |
| 711 | if (*phys == SWIOTLB_MAP_ERROR) |
| 712 | return DIRECT_MAPPING_ERROR; |
| 713 | |
| 714 | /* Ensure that the address returned is DMA'ble */ |
| 715 | dma_addr = __phys_to_dma(dev, *phys); |
| 716 | if (unlikely(!dma_capable(dev, dma_addr, size))) { |
| 717 | swiotlb_tbl_unmap_single(dev, *phys, size, dir, |
| 718 | attrs | DMA_ATTR_SKIP_CPU_SYNC); |
| 719 | return DIRECT_MAPPING_ERROR; |
| 720 | } |
| 721 | |
| 722 | return dma_addr; |
| 723 | } |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
| 726 | * Map a single buffer of the indicated size for DMA in streaming mode. The |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 727 | * physical address to use is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | * |
| 729 | * Once the device is given the dma address, the device owns this memory until |
Becky Bruce | ceb5ac3 | 2009-04-08 09:09:15 -0500 | [diff] [blame] | 730 | * either swiotlb_unmap_page or swiotlb_dma_sync_single is performed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | */ |
FUJITA Tomonori | f98eee8 | 2009-01-05 23:59:03 +0900 | [diff] [blame] | 732 | dma_addr_t swiotlb_map_page(struct device *dev, struct page *page, |
| 733 | unsigned long offset, size_t size, |
| 734 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 735 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | { |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame^] | 737 | phys_addr_t phys = page_to_phys(page) + offset; |
FUJITA Tomonori | 862d196 | 2009-07-10 10:05:02 +0900 | [diff] [blame] | 738 | dma_addr_t dev_addr = phys_to_dma(dev, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 740 | BUG_ON(dir == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | /* |
Becky Bruce | ceb5ac3 | 2009-04-08 09:09:15 -0500 | [diff] [blame] | 742 | * If the address happens to be in the device's DMA window, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | * we can safely return the device addr and not worry about bounce |
| 744 | * buffering it. |
| 745 | */ |
Geert Uytterhoeven | ae7871b | 2016-12-16 14:28:41 +0100 | [diff] [blame] | 746 | if (dma_capable(dev, dev_addr, size) && swiotlb_force != SWIOTLB_FORCE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return dev_addr; |
| 748 | |
Zoltan Kiss | 2b2b614 | 2013-09-04 21:11:05 +0100 | [diff] [blame] | 749 | trace_swiotlb_bounced(dev, dev_addr, size, swiotlb_force); |
Christoph Hellwig | c4dae36 | 2018-08-20 16:21:10 +0200 | [diff] [blame^] | 750 | return swiotlb_bounce_page(dev, &phys, size, dir, attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | * Unmap a single streaming mode DMA translation. The dma_addr and size must |
Becky Bruce | ceb5ac3 | 2009-04-08 09:09:15 -0500 | [diff] [blame] | 755 | * match what was provided for in a previous swiotlb_map_page call. All |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | * other usages are undefined. |
| 757 | * |
| 758 | * After this call, reads by the cpu to the buffer are guaranteed to see |
| 759 | * whatever the device wrote there. |
| 760 | */ |
Christoph Hellwig | 27744e0 | 2018-04-12 09:56:56 +0200 | [diff] [blame] | 761 | void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr, |
| 762 | size_t size, enum dma_data_direction dir, |
| 763 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
FUJITA Tomonori | 862d196 | 2009-07-10 10:05:02 +0900 | [diff] [blame] | 765 | phys_addr_t paddr = dma_to_phys(hwdev, dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 767 | BUG_ON(dir == DMA_NONE); |
Becky Bruce | 7fcebbd | 2009-04-08 09:09:19 -0500 | [diff] [blame] | 768 | |
FUJITA Tomonori | 02ca646 | 2009-07-23 11:18:49 +0900 | [diff] [blame] | 769 | if (is_swiotlb_buffer(paddr)) { |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 770 | swiotlb_tbl_unmap_single(hwdev, paddr, size, dir, attrs); |
Becky Bruce | 7fcebbd | 2009-04-08 09:09:19 -0500 | [diff] [blame] | 771 | return; |
| 772 | } |
| 773 | |
| 774 | if (dir != DMA_FROM_DEVICE) |
| 775 | return; |
| 776 | |
FUJITA Tomonori | 02ca646 | 2009-07-23 11:18:49 +0900 | [diff] [blame] | 777 | /* |
| 778 | * phys_to_virt doesn't work with hihgmem page but we could |
| 779 | * call dma_mark_clean() with hihgmem page here. However, we |
| 780 | * are fine since dma_mark_clean() is null on POWERPC. We can |
| 781 | * make dma_mark_clean() take a physical address if necessary. |
| 782 | */ |
| 783 | dma_mark_clean(phys_to_virt(paddr), size); |
Becky Bruce | 7fcebbd | 2009-04-08 09:09:19 -0500 | [diff] [blame] | 784 | } |
| 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | /* |
| 787 | * Make physical memory consistent for a single streaming mode DMA translation |
| 788 | * after a transfer. |
| 789 | * |
Becky Bruce | ceb5ac3 | 2009-04-08 09:09:15 -0500 | [diff] [blame] | 790 | * If you perform a swiotlb_map_page() but wish to interrogate the buffer |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 791 | * using the cpu, yet do not wish to teardown the dma mapping, you must |
| 792 | * call this function before doing so. At the next point you give the dma |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | * address back to the card, you must first perform a |
| 794 | * swiotlb_dma_sync_for_device, and then the device again owns the buffer |
| 795 | */ |
Andrew Morton | be6b026 | 2007-02-12 00:52:17 -0800 | [diff] [blame] | 796 | static void |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 797 | swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr, |
Konrad Rzeszutek Wilk | d7ef153 | 2010-05-28 11:37:10 -0400 | [diff] [blame] | 798 | size_t size, enum dma_data_direction dir, |
| 799 | enum dma_sync_target target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
FUJITA Tomonori | 862d196 | 2009-07-10 10:05:02 +0900 | [diff] [blame] | 801 | phys_addr_t paddr = dma_to_phys(hwdev, dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 803 | BUG_ON(dir == DMA_NONE); |
Becky Bruce | 380d687 | 2009-04-08 09:09:20 -0500 | [diff] [blame] | 804 | |
FUJITA Tomonori | 02ca646 | 2009-07-23 11:18:49 +0900 | [diff] [blame] | 805 | if (is_swiotlb_buffer(paddr)) { |
Alexander Duyck | fbfda89 | 2012-10-15 10:19:49 -0700 | [diff] [blame] | 806 | swiotlb_tbl_sync_single(hwdev, paddr, size, dir, target); |
Becky Bruce | 380d687 | 2009-04-08 09:09:20 -0500 | [diff] [blame] | 807 | return; |
| 808 | } |
| 809 | |
| 810 | if (dir != DMA_FROM_DEVICE) |
| 811 | return; |
| 812 | |
FUJITA Tomonori | 02ca646 | 2009-07-23 11:18:49 +0900 | [diff] [blame] | 813 | dma_mark_clean(phys_to_virt(paddr), size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | void |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 817 | swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 818 | size_t size, enum dma_data_direction dir) |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 819 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 820 | swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU); |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 825 | size_t size, enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 827 | swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Map a set of buffers described by scatterlist in streaming mode for DMA. |
Becky Bruce | ceb5ac3 | 2009-04-08 09:09:15 -0500 | [diff] [blame] | 832 | * This is the scatter-gather version of the above swiotlb_map_page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | * interface. Here the scatter gather list elements are each tagged with the |
| 834 | * appropriate dma address and length. They are obtained via |
| 835 | * sg_dma_{address,length}(SG). |
| 836 | * |
Becky Bruce | ceb5ac3 | 2009-04-08 09:09:15 -0500 | [diff] [blame] | 837 | * Device ownership issues as mentioned above for swiotlb_map_page are the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | * same here. |
| 839 | */ |
| 840 | int |
Christoph Hellwig | 4803b44 | 2018-08-20 15:56:05 +0200 | [diff] [blame] | 841 | swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nelems, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 842 | enum dma_data_direction dir, unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | { |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 844 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | int i; |
| 846 | |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 847 | for_each_sg(sgl, sg, nelems, i) { |
Christoph Hellwig | 4803b44 | 2018-08-20 15:56:05 +0200 | [diff] [blame] | 848 | sg->dma_address = swiotlb_map_page(dev, sg_page(sg), sg->offset, |
| 849 | sg->length, dir, attrs); |
| 850 | if (sg->dma_address == DIRECT_MAPPING_ERROR) |
| 851 | goto out_error; |
EunBong Song | 4d86ec7 | 2013-08-05 17:30:47 +0100 | [diff] [blame] | 852 | sg_dma_len(sg) = sg->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
Christoph Hellwig | 4803b44 | 2018-08-20 15:56:05 +0200 | [diff] [blame] | 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | return nelems; |
Christoph Hellwig | 4803b44 | 2018-08-20 15:56:05 +0200 | [diff] [blame] | 856 | |
| 857 | out_error: |
| 858 | swiotlb_unmap_sg_attrs(dev, sgl, i, dir, |
| 859 | attrs | DMA_ATTR_SKIP_CPU_SYNC); |
| 860 | sg_dma_len(sgl) = 0; |
| 861 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | /* |
| 865 | * Unmap a set of streaming mode DMA translations. Again, cpu read rules |
Becky Bruce | ceb5ac3 | 2009-04-08 09:09:15 -0500 | [diff] [blame] | 866 | * concerning calls here are the same as for swiotlb_unmap_page() above. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | */ |
| 868 | void |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 869 | swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 870 | int nelems, enum dma_data_direction dir, |
| 871 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 873 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | int i; |
| 875 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 876 | BUG_ON(dir == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Becky Bruce | 7fcebbd | 2009-04-08 09:09:19 -0500 | [diff] [blame] | 878 | for_each_sg(sgl, sg, nelems, i) |
Christoph Hellwig | 27744e0 | 2018-04-12 09:56:56 +0200 | [diff] [blame] | 879 | swiotlb_unmap_page(hwdev, sg->dma_address, sg_dma_len(sg), dir, |
Alexander Duyck | 0443fa0 | 2016-11-02 07:13:02 -0400 | [diff] [blame] | 880 | attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | /* |
| 884 | * Make physical memory consistent for a set of streaming mode DMA translations |
| 885 | * after a transfer. |
| 886 | * |
| 887 | * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules |
| 888 | * and usage. |
| 889 | */ |
Andrew Morton | be6b026 | 2007-02-12 00:52:17 -0800 | [diff] [blame] | 890 | static void |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 891 | swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl, |
Konrad Rzeszutek Wilk | d7ef153 | 2010-05-28 11:37:10 -0400 | [diff] [blame] | 892 | int nelems, enum dma_data_direction dir, |
| 893 | enum dma_sync_target target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | { |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 895 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | int i; |
| 897 | |
Becky Bruce | 380d687 | 2009-04-08 09:09:20 -0500 | [diff] [blame] | 898 | for_each_sg(sgl, sg, nelems, i) |
| 899 | swiotlb_sync_single(hwdev, sg->dma_address, |
EunBong Song | 4d86ec7 | 2013-08-05 17:30:47 +0100 | [diff] [blame] | 900 | sg_dma_len(sg), dir, target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | void |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 904 | swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 905 | int nelems, enum dma_data_direction dir) |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 906 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 907 | swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU); |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 912 | int nelems, enum dma_data_direction dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 914 | swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | /* |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 918 | * Return whether the given device DMA address mask can be supported |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | * properly. For example, if your device can only drive the low 24-bits |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 920 | * during bus mastering, then you would pass 0x00ffffff as the mask to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | * this function. |
| 922 | */ |
| 923 | int |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 924 | swiotlb_dma_supported(struct device *hwdev, u64 mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | { |
Christoph Hellwig | b6e0547 | 2018-03-19 11:38:24 +0100 | [diff] [blame] | 926 | return __phys_to_dma(hwdev, io_tlb_end - 1) <= mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | } |
Christoph Hellwig | 251533e | 2018-01-09 16:44:16 +0100 | [diff] [blame] | 928 | |
Christoph Hellwig | 251533e | 2018-01-09 16:44:16 +0100 | [diff] [blame] | 929 | void *swiotlb_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, |
| 930 | gfp_t gfp, unsigned long attrs) |
| 931 | { |
| 932 | void *vaddr; |
| 933 | |
Christoph Hellwig | 0176adb | 2018-01-09 22:15:30 +0100 | [diff] [blame] | 934 | /* temporary workaround: */ |
| 935 | if (gfp & __GFP_NOWARN) |
| 936 | attrs |= DMA_ATTR_NO_WARN; |
| 937 | |
Christoph Hellwig | 251533e | 2018-01-09 16:44:16 +0100 | [diff] [blame] | 938 | /* |
| 939 | * Don't print a warning when the first allocation attempt fails. |
| 940 | * swiotlb_alloc_coherent() will print a warning when the DMA memory |
| 941 | * allocation ultimately failed. |
| 942 | */ |
| 943 | gfp |= __GFP_NOWARN; |
| 944 | |
| 945 | vaddr = dma_direct_alloc(dev, size, dma_handle, gfp, attrs); |
| 946 | if (!vaddr) |
Christoph Hellwig | 0176adb | 2018-01-09 22:15:30 +0100 | [diff] [blame] | 947 | vaddr = swiotlb_alloc_buffer(dev, size, dma_handle, attrs); |
Christoph Hellwig | 251533e | 2018-01-09 16:44:16 +0100 | [diff] [blame] | 948 | return vaddr; |
| 949 | } |
| 950 | |
| 951 | void swiotlb_free(struct device *dev, size_t size, void *vaddr, |
| 952 | dma_addr_t dma_addr, unsigned long attrs) |
| 953 | { |
Christoph Hellwig | a25381a | 2017-12-23 20:56:02 +0100 | [diff] [blame] | 954 | if (!swiotlb_free_buffer(dev, size, dma_addr)) |
Christoph Hellwig | 251533e | 2018-01-09 16:44:16 +0100 | [diff] [blame] | 955 | dma_direct_free(dev, size, vaddr, dma_addr, attrs); |
| 956 | } |
| 957 | |
| 958 | const struct dma_map_ops swiotlb_dma_ops = { |
Christoph Hellwig | dff8d6c | 2018-08-16 15:30:39 +0300 | [diff] [blame] | 959 | .mapping_error = dma_direct_mapping_error, |
Christoph Hellwig | 251533e | 2018-01-09 16:44:16 +0100 | [diff] [blame] | 960 | .alloc = swiotlb_alloc, |
| 961 | .free = swiotlb_free, |
| 962 | .sync_single_for_cpu = swiotlb_sync_single_for_cpu, |
| 963 | .sync_single_for_device = swiotlb_sync_single_for_device, |
| 964 | .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, |
| 965 | .sync_sg_for_device = swiotlb_sync_sg_for_device, |
| 966 | .map_sg = swiotlb_map_sg_attrs, |
| 967 | .unmap_sg = swiotlb_unmap_sg_attrs, |
| 968 | .map_page = swiotlb_map_page, |
| 969 | .unmap_page = swiotlb_unmap_page, |
Christoph Hellwig | 66bdb14 | 2018-04-09 11:15:17 +0200 | [diff] [blame] | 970 | .dma_supported = dma_direct_supported, |
Christoph Hellwig | 251533e | 2018-01-09 16:44:16 +0100 | [diff] [blame] | 971 | }; |
Christoph Hellwig | 210d079 | 2018-06-28 13:59:25 +0200 | [diff] [blame] | 972 | EXPORT_SYMBOL(swiotlb_dma_ops); |