blob: 13f0cb080a4dc960a1552ddb555155bb97c763f8 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Dynamic DMA mapping support.
4 *
Jan Beulich563aaf02007-02-05 18:51:25 -08005 * This implementation is a fallback for platforms that do not support
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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. Linville569c8bf2005-09-29 14:45:24 -070015 * 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 Brucefb05a372008-12-22 10:26:09 -080018 * 08/12/11 beckyb Add highmem support
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Kees Cook7d63fb32018-07-10 16:22:22 -070021#define pr_fmt(fmt) "software IO TLB: " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/cache.h>
Christoph Hellwigea8c64a2018-01-10 16:21:13 +010024#include <linux/dma-direct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mm.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/spinlock.h>
28#include <linux/string.h>
Ian Campbell0016fde2008-12-16 12:17:27 -080029#include <linux/swiotlb.h>
Becky Brucefb05a372008-12-22 10:26:09 -080030#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/types.h>
32#include <linux/ctype.h>
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -080033#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Christoph Hellwig84be4562015-05-01 12:46:15 +020035#include <linux/scatterlist.h>
Tom Lendackyc7753202017-07-17 16:10:21 -050036#include <linux/mem_encrypt.h>
Christoph Hellwige7de6c72018-03-19 11:38:23 +010037#include <linux/set_memory.h>
Dongli Zhang71602fe2019-01-18 15:10:27 +080038#ifdef CONFIG_DEBUG_FS
39#include <linux/debugfs.h>
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/dma.h>
44
45#include <linux/init.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070046#include <linux/memblock.h>
FUJITA Tomonoria8522502008-04-29 00:59:36 -070047#include <linux/iommu-helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Thierry Redingce5be5a2013-10-23 13:32:04 +020049#define CREATE_TRACE_POINTS
Zoltan Kiss2b2b6142013-09-04 21:11:05 +010050#include <trace/events/swiotlb.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define OFFSET(val,align) ((unsigned long) \
53 ( (val) & ( (align) - 1)))
54
Alex Williamson0b9afed2005-09-06 11:20:49 -060055#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 Uytterhoevenae7871b2016-12-16 14:28:41 +010064enum swiotlb_force swiotlb_force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
Konrad Rzeszutek Wilkbfc55012010-05-10 15:54:20 -040067 * 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 Torvalds1da177e2005-04-16 15:20:36 -070069 * API.
70 */
Christoph Hellwig55897af2018-12-03 11:43:54 +010071phys_addr_t io_tlb_start, io_tlb_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040074 * The number of IO TLB blocks (in groups of 64) between io_tlb_start and
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
76 */
77static unsigned long io_tlb_nslabs;
78
79/*
Dongli Zhang71602fe2019-01-18 15:10:27 +080080 * The number of used IO TLB block
81 */
82static unsigned long io_tlb_used;
83
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * This is a free list describing the number of free entries available from
86 * each index
87 */
88static unsigned int *io_tlb_list;
89static unsigned int io_tlb_index;
90
91/*
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -050092 * Max segment that we can provide which (if pages are contingous) will
93 * not be bounced (unless SWIOTLB_FORCE is set).
94 */
95unsigned int max_segment;
96
97/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * We need to save away the original address corresponding to a mapped entry
99 * for the sync operations.
100 */
Jan Beulich8e0629c2014-06-02 14:58:25 +0100101#define INVALID_PHYS_ADDR (~(phys_addr_t)0)
Becky Brucebc40ac62008-12-22 10:26:08 -0800102static phys_addr_t *io_tlb_orig_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/*
105 * Protect the above data structures in the map and unmap calls
106 */
107static DEFINE_SPINLOCK(io_tlb_lock);
108
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900109static int late_alloc;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int __init
112setup_io_tlb_npages(char *str)
113{
114 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700115 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* 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 Uytterhoevenfff5d992016-12-16 14:28:42 +0100121 if (!strcmp(str, "force")) {
Geert Uytterhoevenae7871b2016-12-16 14:28:41 +0100122 swiotlb_force = SWIOTLB_FORCE;
Geert Uytterhoevenfff5d992016-12-16 14:28:42 +0100123 } else if (!strcmp(str, "noforce")) {
124 swiotlb_force = SWIOTLB_NO_FORCE;
125 io_tlb_nslabs = 1;
126 }
FUJITA Tomonorib18485e2009-11-12 00:03:28 +0900127
Yinghai Luc729de82013-04-15 22:23:45 -0700128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
Yinghai Luc729de82013-04-15 22:23:45 -0700130early_param("swiotlb", setup_io_tlb_npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Konrad Rzeszutek Wilkf21ffe92011-08-11 16:50:56 -0400132unsigned long swiotlb_nr_tbl(void)
FUJITA Tomonori5f98ecd2011-06-05 11:47:29 +0900133{
134 return io_tlb_nslabs;
135}
Konrad Rzeszutek Wilkf21ffe92011-08-11 16:50:56 -0400136EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
Yinghai Luc729de82013-04-15 22:23:45 -0700137
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500138unsigned int swiotlb_max_segment(void)
139{
140 return max_segment;
141}
142EXPORT_SYMBOL_GPL(swiotlb_max_segment);
143
144void 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 Luc729de82013-04-15 22:23:45 -0700152/* default to 64MB */
153#define IO_TLB_DEFAULT_SIZE (64UL<<20)
154unsigned 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 Luac2cbab2013-01-24 12:20:16 -0800163static bool no_iotlb_memory;
164
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900165void swiotlb_print_info(void)
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800166{
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900167 unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800168
Yinghai Luac2cbab2013-01-24 12:20:16 -0800169 if (no_iotlb_memory) {
Kees Cook7d63fb32018-07-10 16:22:22 -0700170 pr_warn("No low mem\n");
Yinghai Luac2cbab2013-01-24 12:20:16 -0800171 return;
172 }
173
Kees Cook7d63fb32018-07-10 16:22:22 -0700174 pr_info("mapped [mem %#010llx-%#010llx] (%luMB)\n",
Alexander Duyckff7204a2012-10-15 10:19:28 -0700175 (unsigned long long)io_tlb_start,
Alexander Duyckc40dba02012-10-15 10:19:22 -0700176 (unsigned long long)io_tlb_end,
Kees Cook7d63fb32018-07-10 16:22:22 -0700177 bytes >> 20);
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800178}
179
Tom Lendackyc7753202017-07-17 16:10:21 -0500180/*
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 */
186void __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 Hellwige7de6c72018-03-19 11:38:23 +0100196 set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
Tom Lendackyc7753202017-07-17 16:10:21 -0500197 memset(vaddr, 0, bytes);
Tom Lendackyc7753202017-07-17 16:10:21 -0500198}
199
Yinghai Luac2cbab2013-01-24 12:20:16 -0800200int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Jan Beulich563aaf02007-02-05 18:51:25 -0800202 unsigned long i, bytes;
Mike Rapoporta0bf8422019-03-11 23:30:26 -0700203 size_t alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400205 bytes = nslabs << IO_TLB_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400207 io_tlb_nslabs = nslabs;
Alexander Duyckff7204a2012-10-15 10:19:28 -0700208 io_tlb_start = __pa(tlb);
209 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
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 Rapoporta0bf8422019-03-11 23:30:26 -0700216 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 Rapoport8a7f97b2019-03-11 23:30:31 -0700219 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
Mike Rapoporta0bf8422019-03-11 23:30:26 -0700220 __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 Rapoport8a7f97b2019-03-11 23:30:31 -0700225 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
Mike Rapoporta0bf8422019-03-11 23:30:26 -0700226 __func__, alloc_size, PAGE_SIZE);
227
Jan Beulich8e0629c2014-06-02 14:58:25 +0100228 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 Torvalds1da177e2005-04-16 15:20:36 -0700233
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900234 if (verbose)
235 swiotlb_print_info();
Yinghai Luac2cbab2013-01-24 12:20:16 -0800236
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500237 swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
Yinghai Luac2cbab2013-01-24 12:20:16 -0800238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400241/*
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 Luac2cbab2013-01-24 12:20:16 -0800245void __init
246swiotlb_init(int verbose)
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400247{
Yinghai Luc729de82013-04-15 22:23:45 -0700248 size_t default_size = IO_TLB_DEFAULT_SIZE;
Alexander Duyckff7204a2012-10-15 10:19:28 -0700249 unsigned char *vstart;
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400250 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 Luac2cbab2013-01-24 12:20:16 -0800259 /* Get IO TLB memory from the low pages */
Mike Rapoport26fb3da2019-03-11 23:30:42 -0700260 vstart = memblock_alloc_low(PAGE_ALIGN(bytes), PAGE_SIZE);
Yinghai Luac2cbab2013-01-24 12:20:16 -0800261 if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose))
262 return;
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400263
Yinghai Luac2cbab2013-01-24 12:20:16 -0800264 if (io_tlb_start)
Santosh Shilimkar457ff1d2014-01-21 15:50:30 -0800265 memblock_free_early(io_tlb_start,
266 PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
Kees Cook7d63fb32018-07-10 16:22:22 -0700267 pr_warn("Cannot allocate buffer");
Yinghai Luac2cbab2013-01-24 12:20:16 -0800268 no_iotlb_memory = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Alex Williamson0b9afed2005-09-06 11:20:49 -0600271/*
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 */
276int
Jan Beulich563aaf02007-02-05 18:51:25 -0800277swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600278{
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400279 unsigned long bytes, req_nslabs = io_tlb_nslabs;
Alexander Duyckff7204a2012-10-15 10:19:28 -0700280 unsigned char *vstart = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600281 unsigned int order;
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400282 int rc = 0;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600283
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 Beulich563aaf02007-02-05 18:51:25 -0800292 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600293 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800294 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600295
296 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
Alexander Duyckff7204a2012-10-15 10:19:28 -0700297 vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
298 order);
299 if (vstart)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600300 break;
301 order--;
302 }
303
Alexander Duyckff7204a2012-10-15 10:19:28 -0700304 if (!vstart) {
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400305 io_tlb_nslabs = req_nslabs;
306 return -ENOMEM;
307 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800308 if (order != get_order(bytes)) {
Kees Cook7d63fb32018-07-10 16:22:22 -0700309 pr_warn("only able to allocate %ld MB\n",
310 (PAGE_SIZE << order) >> 20);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600311 io_tlb_nslabs = SLABS_PER_PAGE << order;
312 }
Alexander Duyckff7204a2012-10-15 10:19:28 -0700313 rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs);
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400314 if (rc)
Alexander Duyckff7204a2012-10-15 10:19:28 -0700315 free_pages((unsigned long)vstart, order);
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500316
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400317 return rc;
318}
319
320int
321swiotlb_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 Duyckff7204a2012-10-15 10:19:28 -0700328 io_tlb_start = virt_to_phys(tlb);
329 io_tlb_end = io_tlb_start + bytes;
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400330
Christoph Hellwige7de6c72018-03-19 11:38:23 +0100331 set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT);
Alexander Duyckff7204a2012-10-15 10:19:28 -0700332 memset(tlb, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600333
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 Duyckee3f6ba2012-10-15 10:19:34 -0700342 goto cleanup3;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600343
Becky Brucebc40ac62008-12-22 10:26:08 -0800344 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 Williamson0b9afed2005-09-06 11:20:49 -0600348 if (!io_tlb_orig_addr)
Alexander Duyckee3f6ba2012-10-15 10:19:34 -0700349 goto cleanup4;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600350
Jan Beulich8e0629c2014-06-02 14:58:25 +0100351 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 Williamson0b9afed2005-09-06 11:20:49 -0600356
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900357 swiotlb_print_info();
Alex Williamson0b9afed2005-09-06 11:20:49 -0600358
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900359 late_alloc = 1;
360
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500361 swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
362
Alex Williamson0b9afed2005-09-06 11:20:49 -0600363 return 0;
364
365cleanup4:
Tony Luck25667d62007-03-06 13:31:45 -0800366 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
367 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600368 io_tlb_list = NULL;
Alexander Duyckee3f6ba2012-10-15 10:19:34 -0700369cleanup3:
Alexander Duyckc40dba02012-10-15 10:19:22 -0700370 io_tlb_end = 0;
Alexander Duyckff7204a2012-10-15 10:19:28 -0700371 io_tlb_start = 0;
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400372 io_tlb_nslabs = 0;
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500373 max_segment = 0;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600374 return -ENOMEM;
375}
376
Christoph Hellwig7f2c8bb2017-12-23 14:14:54 +0100377void __init swiotlb_exit(void)
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900378{
Alexander Duyckee3f6ba2012-10-15 10:19:34 -0700379 if (!io_tlb_orig_addr)
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900380 return;
381
382 if (late_alloc) {
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900383 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 Duyckff7204a2012-10-15 10:19:28 -0700387 free_pages((unsigned long)phys_to_virt(io_tlb_start),
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900388 get_order(io_tlb_nslabs << IO_TLB_SHIFT));
389 } else {
Santosh Shilimkar457ff1d2014-01-21 15:50:30 -0800390 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 Tomonori5740afd2009-11-10 19:46:18 +0900396 }
Christoph Hellwig227a76b2019-01-14 21:14:08 +0100397 io_tlb_start = 0;
398 io_tlb_end = 0;
Konrad Rzeszutek Wilkf21ffe92011-08-11 16:50:56 -0400399 io_tlb_nslabs = 0;
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500400 max_segment = 0;
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/*
Dongli Zhang6442ca22019-01-18 15:10:26 +0800404 * Bounce: copy the swiotlb buffer from or back to the original dma location
Becky Brucefb05a372008-12-22 10:26:09 -0800405 */
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700406static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
407 size_t size, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700409 unsigned long pfn = PFN_DOWN(orig_addr);
410 unsigned char *vaddr = phys_to_virt(tlb_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Becky Brucefb05a372008-12-22 10:26:09 -0800412 if (PageHighMem(pfn_to_page(pfn))) {
413 /* The buffer does not have a mapping. Map it in and copy */
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700414 unsigned int offset = orig_addr & ~PAGE_MASK;
Becky Brucefb05a372008-12-22 10:26:09 -0800415 char *buffer;
416 unsigned int sz = 0;
417 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Becky Brucefb05a372008-12-22 10:26:09 -0800419 while (size) {
Becky Bruce67131ad2009-04-08 09:09:16 -0500420 sz = min_t(size_t, PAGE_SIZE - offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Becky Brucefb05a372008-12-22 10:26:09 -0800422 local_irq_save(flags);
Cong Wangc3eede82011-11-25 23:14:39 +0800423 buffer = kmap_atomic(pfn_to_page(pfn));
Becky Brucefb05a372008-12-22 10:26:09 -0800424 if (dir == DMA_TO_DEVICE)
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700425 memcpy(vaddr, buffer + offset, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 else
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700427 memcpy(buffer + offset, vaddr, sz);
Cong Wangc3eede82011-11-25 23:14:39 +0800428 kunmap_atomic(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 local_irq_restore(flags);
Becky Brucefb05a372008-12-22 10:26:09 -0800430
431 size -= sz;
432 pfn++;
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700433 vaddr += sz;
Becky Brucefb05a372008-12-22 10:26:09 -0800434 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700436 } else if (dir == DMA_TO_DEVICE) {
437 memcpy(vaddr, phys_to_virt(orig_addr), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 } else {
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700439 memcpy(phys_to_virt(orig_addr), vaddr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441}
442
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700443phys_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 Duyck0443fa02016-11-02 07:13:02 -0400446 enum dma_data_direction dir,
447 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 unsigned long flags;
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700450 phys_addr_t tlb_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 unsigned int nslots, stride, index, wrap;
452 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800453 unsigned long mask;
454 unsigned long offset_slots;
455 unsigned long max_slots;
Dongli Zhang53b29c32019-04-12 19:38:26 +0800456 unsigned long tmp_io_tlb_used;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800457
Yinghai Luac2cbab2013-01-24 12:20:16 -0800458 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 Lendackyd7b417f2017-10-20 09:30:53 -0500461 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 Lendacky648babb2017-07-17 16:10:22 -0500464
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800465 mask = dma_get_seg_boundary(hwdev);
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800466
FUJITA Tomonorieb605a52010-05-10 15:14:54 -0400467 tbl_dma_addr &= mask;
468
469 offset_slots = ALIGN(tbl_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Ian Campbella5ddde4a2008-12-16 12:17:29 -0800470
471 /*
472 * Carefully handle integer overflow which can occur when mask == ~0UL.
473 */
Jan Beulichb15a38912008-03-13 09:13:30 +0000474 max_slots = mask + 1
475 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
476 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /*
Nikita Yushchenko602d9852017-01-11 21:56:31 +0300479 * For mappings greater than or equal to a page, we limit the stride
480 * (and hence alignment) to a page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
482 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Nikita Yushchenko602d9852017-01-11 21:56:31 +0300483 if (size >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
485 else
486 stride = 1;
487
Eric Sesterhenn34814542006-03-24 18:47:11 +0100488 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
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 Zhang60513ed2019-01-18 15:10:28 +0800495
496 if (unlikely(nslots > io_tlb_nslabs - io_tlb_used))
497 goto not_found;
498
Andrew Mortona7133a12008-04-29 00:59:36 -0700499 index = ALIGN(io_tlb_index, stride);
500 if (index >= io_tlb_nslabs)
501 index = 0;
502 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Andrew Mortona7133a12008-04-29 00:59:36 -0700504 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700505 while (iommu_is_span_boundary(index, nslots, offset_slots,
506 max_slots)) {
Jan Beulichb15a38912008-03-13 09:13:30 +0000507 index += stride;
508 if (index >= io_tlb_nslabs)
509 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700510 if (index == wrap)
511 goto not_found;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Andrew Mortona7133a12008-04-29 00:59:36 -0700514 /*
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 Duycke05ed4d2012-10-15 10:19:39 -0700526 tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT);
Andrew Mortona7133a12008-04-29 00:59:36 -0700527
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
542not_found:
Dongli Zhang53b29c32019-04-12 19:38:26 +0800543 tmp_io_tlb_used = io_tlb_used;
544
Andrew Mortona7133a12008-04-29 00:59:36 -0700545 spin_unlock_irqrestore(&io_tlb_lock, flags);
Christian Königd0bc0c22018-01-04 14:24:19 +0100546 if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit())
Dongli Zhang53b29c32019-04-12 19:38:26 +0800547 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 Hellwigb907e202018-12-03 11:42:52 +0100549 return DMA_MAPPING_ERROR;
Andrew Mortona7133a12008-04-29 00:59:36 -0700550found:
Dongli Zhang71602fe2019-01-18 15:10:27 +0800551 io_tlb_used += nslots;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 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 Brucebc40ac62008-12-22 10:26:08 -0800559 for (i = 0; i < nslots; i++)
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700560 io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
Alexander Duyck0443fa02016-11-02 07:13:02 -0400561 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
562 (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700563 swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700565 return tlb_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/*
Yisheng Xied0c8ba42018-05-07 19:06:25 +0800569 * tlb_addr is the physical address of the bounce buffer to unmap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
Alexander Duyck61ca08c2012-10-15 10:19:44 -0700571void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
Alexander Duyck0443fa02016-11-02 07:13:02 -0400572 size_t size, enum dma_data_direction dir,
573 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 unsigned long flags;
576 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Alexander Duyck61ca08c2012-10-15 10:19:44 -0700577 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
578 phys_addr_t orig_addr = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /*
581 * First, sync the memory before unmapping the entry
582 */
Jan Beulich8e0629c2014-06-02 14:58:25 +0100583 if (orig_addr != INVALID_PHYS_ADDR &&
Alexander Duyck0443fa02016-11-02 07:13:02 -0400584 !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
Jan Beulich8e0629c2014-06-02 14:58:25 +0100585 ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700586 swiotlb_bounce(orig_addr, tlb_addr, size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /*
589 * Return the buffer to the free list by setting the corresponding
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200590 * entries to indicate the number of contiguous entries available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * 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 Beulich8e0629c2014-06-02 14:58:25 +0100602 for (i = index + nslots - 1; i >= index; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 io_tlb_list[i] = ++count;
Jan Beulich8e0629c2014-06-02 14:58:25 +0100604 io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
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 Zhang71602fe2019-01-18 15:10:27 +0800612
613 io_tlb_used -= nslots;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 spin_unlock_irqrestore(&io_tlb_lock, flags);
616}
617
Alexander Duyckfbfda892012-10-15 10:19:49 -0700618void 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 Torvalds1da177e2005-04-16 15:20:36 -0700621{
Alexander Duyckfbfda892012-10-15 10:19:49 -0700622 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
623 phys_addr_t orig_addr = io_tlb_orig_addr[index];
Becky Brucebc40ac62008-12-22 10:26:08 -0800624
Jan Beulich8e0629c2014-06-02 14:58:25 +0100625 if (orig_addr == INVALID_PHYS_ADDR)
626 return;
Alexander Duyckfbfda892012-10-15 10:19:49 -0700627 orig_addr += (unsigned long)tlb_addr & ((1 << IO_TLB_SHIFT) - 1);
Keir Fraserdf336d12007-07-21 04:37:24 -0700628
John W. Linvillede69e0f2005-09-29 14:44:57 -0700629 switch (target) {
630 case SYNC_FOR_CPU:
631 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700632 swiotlb_bounce(orig_addr, tlb_addr,
Alexander Duyckfbfda892012-10-15 10:19:49 -0700633 size, DMA_FROM_DEVICE);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100634 else
635 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700636 break;
637 case SYNC_FOR_DEVICE:
638 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700639 swiotlb_bounce(orig_addr, tlb_addr,
Alexander Duyckfbfda892012-10-15 10:19:49 -0700640 size, DMA_TO_DEVICE);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100641 else
642 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700643 break;
644 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Christoph Hellwig55897af2018-12-03 11:43:54 +0100649/*
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 */
653bool swiotlb_map(struct device *dev, phys_addr_t *phys, dma_addr_t *dma_addr,
Christoph Hellwigc4dae362018-08-20 16:21:10 +0200654 size_t size, enum dma_data_direction dir, unsigned long attrs)
655{
Christoph Hellwig55897af2018-12-03 11:43:54 +0100656 trace_swiotlb_bounced(dev, *dma_addr, size, swiotlb_force);
Christoph Hellwigc4dae362018-08-20 16:21:10 +0200657
658 if (unlikely(swiotlb_force == SWIOTLB_NO_FORCE)) {
659 dev_warn_ratelimited(dev,
660 "Cannot do DMA to address %pa\n", phys);
Christoph Hellwig55897af2018-12-03 11:43:54 +0100661 return false;
Christoph Hellwigc4dae362018-08-20 16:21:10 +0200662 }
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 Hellwigb907e202018-12-03 11:42:52 +0100667 if (*phys == DMA_MAPPING_ERROR)
Christoph Hellwig55897af2018-12-03 11:43:54 +0100668 return false;
Christoph Hellwigc4dae362018-08-20 16:21:10 +0200669
670 /* Ensure that the address returned is DMA'ble */
Christoph Hellwig55897af2018-12-03 11:43:54 +0100671 *dma_addr = __phys_to_dma(dev, *phys);
672 if (unlikely(!dma_capable(dev, *dma_addr, size))) {
Christoph Hellwigc4dae362018-08-20 16:21:10 +0200673 swiotlb_tbl_unmap_single(dev, *phys, size, dir,
674 attrs | DMA_ATTR_SKIP_CPU_SYNC);
Christoph Hellwig55897af2018-12-03 11:43:54 +0100675 return false;
Christoph Hellwigc4dae362018-08-20 16:21:10 +0200676 }
677
Christoph Hellwig55897af2018-12-03 11:43:54 +0100678 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Joerg Roedelabe420b2019-02-07 12:59:13 +0100681size_t swiotlb_max_mapping_size(struct device *dev)
682{
683 return ((size_t)1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
684}
Joerg Roedel492366f2019-02-07 12:59:14 +0100685
686bool 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 Torvalds45ba8d52019-03-10 12:47:57 -0700694
Dongli Zhang71602fe2019-01-18 15:10:27 +0800695#ifdef CONFIG_DEBUG_FS
696
697static int __init swiotlb_create_debugfs(void)
698{
YueHaibing22cb45d2019-02-14 09:04:08 +0000699 struct dentry *d_swiotlb_usage;
Dongli Zhang71602fe2019-01-18 15:10:27 +0800700 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
719fail:
720 debugfs_remove_recursive(d_swiotlb_usage);
721 return -ENOMEM;
722}
723
724late_initcall(swiotlb_create_debugfs);
725
726#endif