blob: 57507b18caa4022c07404b0b0bc149a0a9384303 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dynamic DMA mapping support.
3 *
Jan Beulich563aaf02007-02-05 18:51:25 -08004 * This implementation is a fallback for platforms that do not support
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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. Linville569c8bf2005-09-29 14:45:24 -070014 * 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 Brucefb05a372008-12-22 10:26:09 -080017 * 08/12/11 beckyb Add highmem support
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Kees Cook7d63fb32018-07-10 16:22:22 -070020#define pr_fmt(fmt) "software IO TLB: " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/cache.h>
Christoph Hellwigea8c64a2018-01-10 16:21:13 +010023#include <linux/dma-direct.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/mm.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050025#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/spinlock.h>
27#include <linux/string.h>
Ian Campbell0016fde2008-12-16 12:17:27 -080028#include <linux/swiotlb.h>
Becky Brucefb05a372008-12-22 10:26:09 -080029#include <linux/pfn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/ctype.h>
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -080032#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/gfp.h>
Christoph Hellwig84be4562015-05-01 12:46:15 +020034#include <linux/scatterlist.h>
Tom Lendackyc7753202017-07-17 16:10:21 -050035#include <linux/mem_encrypt.h>
Christoph Hellwige7de6c72018-03-19 11:38:23 +010036#include <linux/set_memory.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/dma.h>
40
41#include <linux/init.h>
42#include <linux/bootmem.h>
FUJITA Tomonoria8522502008-04-29 00:59:36 -070043#include <linux/iommu-helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Thierry Redingce5be5a2013-10-23 13:32:04 +020045#define CREATE_TRACE_POINTS
Zoltan Kiss2b2b6142013-09-04 21:11:05 +010046#include <trace/events/swiotlb.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define OFFSET(val,align) ((unsigned long) \
49 ( (val) & ( (align) - 1)))
50
Alex Williamson0b9afed2005-09-06 11:20:49 -060051#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 Uytterhoevenae7871b2016-12-16 14:28:41 +010060enum swiotlb_force swiotlb_force;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
Konrad Rzeszutek Wilkbfc55012010-05-10 15:54:20 -040063 * 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 Torvalds1da177e2005-04-16 15:20:36 -070065 * API.
66 */
Alexander Duyckff7204a2012-10-15 10:19:28 -070067static phys_addr_t io_tlb_start, io_tlb_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040070 * The number of IO TLB blocks (in groups of 64) between io_tlb_start and
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
72 */
73static unsigned long io_tlb_nslabs;
74
75/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * This is a free list describing the number of free entries available from
77 * each index
78 */
79static unsigned int *io_tlb_list;
80static unsigned int io_tlb_index;
81
82/*
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -050083 * Max segment that we can provide which (if pages are contingous) will
84 * not be bounced (unless SWIOTLB_FORCE is set).
85 */
86unsigned int max_segment;
87
88/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * We need to save away the original address corresponding to a mapped entry
90 * for the sync operations.
91 */
Jan Beulich8e0629c2014-06-02 14:58:25 +010092#define INVALID_PHYS_ADDR (~(phys_addr_t)0)
Becky Brucebc40ac62008-12-22 10:26:08 -080093static phys_addr_t *io_tlb_orig_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * Protect the above data structures in the map and unmap calls
97 */
98static DEFINE_SPINLOCK(io_tlb_lock);
99
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900100static int late_alloc;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int __init
103setup_io_tlb_npages(char *str)
104{
105 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700106 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* 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 Uytterhoevenfff5d992016-12-16 14:28:42 +0100112 if (!strcmp(str, "force")) {
Geert Uytterhoevenae7871b2016-12-16 14:28:41 +0100113 swiotlb_force = SWIOTLB_FORCE;
Geert Uytterhoevenfff5d992016-12-16 14:28:42 +0100114 } else if (!strcmp(str, "noforce")) {
115 swiotlb_force = SWIOTLB_NO_FORCE;
116 io_tlb_nslabs = 1;
117 }
FUJITA Tomonorib18485e2009-11-12 00:03:28 +0900118
Yinghai Luc729de82013-04-15 22:23:45 -0700119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
Yinghai Luc729de82013-04-15 22:23:45 -0700121early_param("swiotlb", setup_io_tlb_npages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Konrad Rzeszutek Wilkf21ffe92011-08-11 16:50:56 -0400123unsigned long swiotlb_nr_tbl(void)
FUJITA Tomonori5f98ecd2011-06-05 11:47:29 +0900124{
125 return io_tlb_nslabs;
126}
Konrad Rzeszutek Wilkf21ffe92011-08-11 16:50:56 -0400127EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
Yinghai Luc729de82013-04-15 22:23:45 -0700128
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500129unsigned int swiotlb_max_segment(void)
130{
131 return max_segment;
132}
133EXPORT_SYMBOL_GPL(swiotlb_max_segment);
134
135void 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 Luc729de82013-04-15 22:23:45 -0700143/* default to 64MB */
144#define IO_TLB_DEFAULT_SIZE (64UL<<20)
145unsigned 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 Luac2cbab2013-01-24 12:20:16 -0800154static bool no_iotlb_memory;
155
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900156void swiotlb_print_info(void)
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800157{
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900158 unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800159
Yinghai Luac2cbab2013-01-24 12:20:16 -0800160 if (no_iotlb_memory) {
Kees Cook7d63fb32018-07-10 16:22:22 -0700161 pr_warn("No low mem\n");
Yinghai Luac2cbab2013-01-24 12:20:16 -0800162 return;
163 }
164
Kees Cook7d63fb32018-07-10 16:22:22 -0700165 pr_info("mapped [mem %#010llx-%#010llx] (%luMB)\n",
Alexander Duyckff7204a2012-10-15 10:19:28 -0700166 (unsigned long long)io_tlb_start,
Alexander Duyckc40dba02012-10-15 10:19:22 -0700167 (unsigned long long)io_tlb_end,
Kees Cook7d63fb32018-07-10 16:22:22 -0700168 bytes >> 20);
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800169}
170
Tom Lendackyc7753202017-07-17 16:10:21 -0500171/*
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 */
177void __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 Hellwige7de6c72018-03-19 11:38:23 +0100187 set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
Tom Lendackyc7753202017-07-17 16:10:21 -0500188 memset(vaddr, 0, bytes);
Tom Lendackyc7753202017-07-17 16:10:21 -0500189}
190
Yinghai Luac2cbab2013-01-24 12:20:16 -0800191int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Jan Beulich563aaf02007-02-05 18:51:25 -0800193 unsigned long i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400195 bytes = nslabs << IO_TLB_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400197 io_tlb_nslabs = nslabs;
Alexander Duyckff7204a2012-10-15 10:19:28 -0700198 io_tlb_start = __pa(tlb);
199 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
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 Shilimkar457ff1d2014-01-21 15:50:30 -0800206 io_tlb_list = memblock_virt_alloc(
207 PAGE_ALIGN(io_tlb_nslabs * sizeof(int)),
208 PAGE_SIZE);
Santosh Shilimkar457ff1d2014-01-21 15:50:30 -0800209 io_tlb_orig_addr = memblock_virt_alloc(
210 PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)),
211 PAGE_SIZE);
Jan Beulich8e0629c2014-06-02 14:58:25 +0100212 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 Torvalds1da177e2005-04-16 15:20:36 -0700217
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900218 if (verbose)
219 swiotlb_print_info();
Yinghai Luac2cbab2013-01-24 12:20:16 -0800220
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500221 swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
Yinghai Luac2cbab2013-01-24 12:20:16 -0800222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400225/*
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 Luac2cbab2013-01-24 12:20:16 -0800229void __init
230swiotlb_init(int verbose)
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400231{
Yinghai Luc729de82013-04-15 22:23:45 -0700232 size_t default_size = IO_TLB_DEFAULT_SIZE;
Alexander Duyckff7204a2012-10-15 10:19:28 -0700233 unsigned char *vstart;
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400234 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 Luac2cbab2013-01-24 12:20:16 -0800243 /* Get IO TLB memory from the low pages */
Yinghai Luad6492b2014-01-27 17:06:49 -0800244 vstart = memblock_virt_alloc_low_nopanic(PAGE_ALIGN(bytes), PAGE_SIZE);
Yinghai Luac2cbab2013-01-24 12:20:16 -0800245 if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose))
246 return;
FUJITA Tomonoriabbceff2010-05-10 15:15:12 -0400247
Yinghai Luac2cbab2013-01-24 12:20:16 -0800248 if (io_tlb_start)
Santosh Shilimkar457ff1d2014-01-21 15:50:30 -0800249 memblock_free_early(io_tlb_start,
250 PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
Kees Cook7d63fb32018-07-10 16:22:22 -0700251 pr_warn("Cannot allocate buffer");
Yinghai Luac2cbab2013-01-24 12:20:16 -0800252 no_iotlb_memory = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Alex Williamson0b9afed2005-09-06 11:20:49 -0600255/*
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 */
260int
Jan Beulich563aaf02007-02-05 18:51:25 -0800261swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600262{
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400263 unsigned long bytes, req_nslabs = io_tlb_nslabs;
Alexander Duyckff7204a2012-10-15 10:19:28 -0700264 unsigned char *vstart = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600265 unsigned int order;
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400266 int rc = 0;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600267
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 Beulich563aaf02007-02-05 18:51:25 -0800276 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600277 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800278 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600279
280 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
Alexander Duyckff7204a2012-10-15 10:19:28 -0700281 vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
282 order);
283 if (vstart)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600284 break;
285 order--;
286 }
287
Alexander Duyckff7204a2012-10-15 10:19:28 -0700288 if (!vstart) {
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400289 io_tlb_nslabs = req_nslabs;
290 return -ENOMEM;
291 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800292 if (order != get_order(bytes)) {
Kees Cook7d63fb32018-07-10 16:22:22 -0700293 pr_warn("only able to allocate %ld MB\n",
294 (PAGE_SIZE << order) >> 20);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600295 io_tlb_nslabs = SLABS_PER_PAGE << order;
296 }
Alexander Duyckff7204a2012-10-15 10:19:28 -0700297 rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs);
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400298 if (rc)
Alexander Duyckff7204a2012-10-15 10:19:28 -0700299 free_pages((unsigned long)vstart, order);
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500300
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400301 return rc;
302}
303
304int
305swiotlb_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 Duyckff7204a2012-10-15 10:19:28 -0700312 io_tlb_start = virt_to_phys(tlb);
313 io_tlb_end = io_tlb_start + bytes;
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400314
Christoph Hellwige7de6c72018-03-19 11:38:23 +0100315 set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT);
Alexander Duyckff7204a2012-10-15 10:19:28 -0700316 memset(tlb, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600317
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 Duyckee3f6ba2012-10-15 10:19:34 -0700326 goto cleanup3;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600327
Becky Brucebc40ac62008-12-22 10:26:08 -0800328 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 Williamson0b9afed2005-09-06 11:20:49 -0600332 if (!io_tlb_orig_addr)
Alexander Duyckee3f6ba2012-10-15 10:19:34 -0700333 goto cleanup4;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600334
Jan Beulich8e0629c2014-06-02 14:58:25 +0100335 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 Williamson0b9afed2005-09-06 11:20:49 -0600340
FUJITA Tomonoriad32e8c2009-11-10 19:46:19 +0900341 swiotlb_print_info();
Alex Williamson0b9afed2005-09-06 11:20:49 -0600342
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900343 late_alloc = 1;
344
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500345 swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
346
Alex Williamson0b9afed2005-09-06 11:20:49 -0600347 return 0;
348
349cleanup4:
Tony Luck25667d62007-03-06 13:31:45 -0800350 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
351 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600352 io_tlb_list = NULL;
Alexander Duyckee3f6ba2012-10-15 10:19:34 -0700353cleanup3:
Alexander Duyckc40dba02012-10-15 10:19:22 -0700354 io_tlb_end = 0;
Alexander Duyckff7204a2012-10-15 10:19:28 -0700355 io_tlb_start = 0;
Konrad Rzeszutek Wilk74838b72012-07-27 20:55:27 -0400356 io_tlb_nslabs = 0;
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500357 max_segment = 0;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600358 return -ENOMEM;
359}
360
Christoph Hellwig7f2c8bb2017-12-23 14:14:54 +0100361void __init swiotlb_exit(void)
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900362{
Alexander Duyckee3f6ba2012-10-15 10:19:34 -0700363 if (!io_tlb_orig_addr)
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900364 return;
365
366 if (late_alloc) {
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900367 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 Duyckff7204a2012-10-15 10:19:28 -0700371 free_pages((unsigned long)phys_to_virt(io_tlb_start),
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900372 get_order(io_tlb_nslabs << IO_TLB_SHIFT));
373 } else {
Santosh Shilimkar457ff1d2014-01-21 15:50:30 -0800374 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 Tomonori5740afd2009-11-10 19:46:18 +0900380 }
Konrad Rzeszutek Wilkf21ffe92011-08-11 16:50:56 -0400381 io_tlb_nslabs = 0;
Konrad Rzeszutek Wilk7453c542016-12-20 10:02:02 -0500382 max_segment = 0;
FUJITA Tomonori5740afd2009-11-10 19:46:18 +0900383}
384
Christoph Hellwigb65125c2018-04-12 14:49:23 +0200385static int is_swiotlb_buffer(phys_addr_t paddr)
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900386{
Alexander Duyckff7204a2012-10-15 10:19:28 -0700387 return paddr >= io_tlb_start && paddr < io_tlb_end;
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
Becky Brucefb05a372008-12-22 10:26:09 -0800391 * Bounce: copy the swiotlb buffer back to the original dma location
392 */
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700393static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
394 size_t size, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700396 unsigned long pfn = PFN_DOWN(orig_addr);
397 unsigned char *vaddr = phys_to_virt(tlb_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Becky Brucefb05a372008-12-22 10:26:09 -0800399 if (PageHighMem(pfn_to_page(pfn))) {
400 /* The buffer does not have a mapping. Map it in and copy */
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700401 unsigned int offset = orig_addr & ~PAGE_MASK;
Becky Brucefb05a372008-12-22 10:26:09 -0800402 char *buffer;
403 unsigned int sz = 0;
404 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Becky Brucefb05a372008-12-22 10:26:09 -0800406 while (size) {
Becky Bruce67131ad2009-04-08 09:09:16 -0500407 sz = min_t(size_t, PAGE_SIZE - offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Becky Brucefb05a372008-12-22 10:26:09 -0800409 local_irq_save(flags);
Cong Wangc3eede82011-11-25 23:14:39 +0800410 buffer = kmap_atomic(pfn_to_page(pfn));
Becky Brucefb05a372008-12-22 10:26:09 -0800411 if (dir == DMA_TO_DEVICE)
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700412 memcpy(vaddr, buffer + offset, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 else
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700414 memcpy(buffer + offset, vaddr, sz);
Cong Wangc3eede82011-11-25 23:14:39 +0800415 kunmap_atomic(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 local_irq_restore(flags);
Becky Brucefb05a372008-12-22 10:26:09 -0800417
418 size -= sz;
419 pfn++;
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700420 vaddr += sz;
Becky Brucefb05a372008-12-22 10:26:09 -0800421 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700423 } else if (dir == DMA_TO_DEVICE) {
424 memcpy(vaddr, phys_to_virt(orig_addr), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 } else {
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700426 memcpy(phys_to_virt(orig_addr), vaddr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428}
429
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700430phys_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 Duyck0443fa02016-11-02 07:13:02 -0400433 enum dma_data_direction dir,
434 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 unsigned long flags;
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700437 phys_addr_t tlb_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned int nslots, stride, index, wrap;
439 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800440 unsigned long mask;
441 unsigned long offset_slots;
442 unsigned long max_slots;
443
Yinghai Luac2cbab2013-01-24 12:20:16 -0800444 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 Lendackyd7b417f2017-10-20 09:30:53 -0500447 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 Lendacky648babb2017-07-17 16:10:22 -0500450
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800451 mask = dma_get_seg_boundary(hwdev);
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800452
FUJITA Tomonorieb605a52010-05-10 15:14:54 -0400453 tbl_dma_addr &= mask;
454
455 offset_slots = ALIGN(tbl_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Ian Campbella5ddde4a2008-12-16 12:17:29 -0800456
457 /*
458 * Carefully handle integer overflow which can occur when mask == ~0UL.
459 */
Jan Beulichb15a38912008-03-13 09:13:30 +0000460 max_slots = mask + 1
461 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
462 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /*
Nikita Yushchenko602d9852017-01-11 21:56:31 +0300465 * For mappings greater than or equal to a page, we limit the stride
466 * (and hence alignment) to a page size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
468 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Nikita Yushchenko602d9852017-01-11 21:56:31 +0300469 if (size >= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
471 else
472 stride = 1;
473
Eric Sesterhenn34814542006-03-24 18:47:11 +0100474 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
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 Mortona7133a12008-04-29 00:59:36 -0700481 index = ALIGN(io_tlb_index, stride);
482 if (index >= io_tlb_nslabs)
483 index = 0;
484 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Andrew Mortona7133a12008-04-29 00:59:36 -0700486 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700487 while (iommu_is_span_boundary(index, nslots, offset_slots,
488 max_slots)) {
Jan Beulichb15a38912008-03-13 09:13:30 +0000489 index += stride;
490 if (index >= io_tlb_nslabs)
491 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700492 if (index == wrap)
493 goto not_found;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Andrew Mortona7133a12008-04-29 00:59:36 -0700496 /*
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 Duycke05ed4d2012-10-15 10:19:39 -0700508 tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT);
Andrew Mortona7133a12008-04-29 00:59:36 -0700509
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
524not_found:
525 spin_unlock_irqrestore(&io_tlb_lock, flags);
Christian Königd0bc0c22018-01-04 14:24:19 +0100526 if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit())
Konrad Rzeszutek Wilk0cb637b2013-12-16 14:05:01 -0500527 dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes)\n", size);
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700528 return SWIOTLB_MAP_ERROR;
Andrew Mortona7133a12008-04-29 00:59:36 -0700529found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 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 Brucebc40ac62008-12-22 10:26:08 -0800537 for (i = 0; i < nslots; i++)
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700538 io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
Alexander Duyck0443fa02016-11-02 07:13:02 -0400539 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
540 (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700541 swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Alexander Duycke05ed4d2012-10-15 10:19:39 -0700543 return tlb_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546/*
Yisheng Xied0c8ba42018-05-07 19:06:25 +0800547 * tlb_addr is the physical address of the bounce buffer to unmap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Alexander Duyck61ca08c2012-10-15 10:19:44 -0700549void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
Alexander Duyck0443fa02016-11-02 07:13:02 -0400550 size_t size, enum dma_data_direction dir,
551 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 unsigned long flags;
554 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Alexander Duyck61ca08c2012-10-15 10:19:44 -0700555 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
556 phys_addr_t orig_addr = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /*
559 * First, sync the memory before unmapping the entry
560 */
Jan Beulich8e0629c2014-06-02 14:58:25 +0100561 if (orig_addr != INVALID_PHYS_ADDR &&
Alexander Duyck0443fa02016-11-02 07:13:02 -0400562 !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
Jan Beulich8e0629c2014-06-02 14:58:25 +0100563 ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700564 swiotlb_bounce(orig_addr, tlb_addr, size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /*
567 * Return the buffer to the free list by setting the corresponding
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200568 * entries to indicate the number of contiguous entries available.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * 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 Beulich8e0629c2014-06-02 14:58:25 +0100580 for (i = index + nslots - 1; i >= index; i--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 io_tlb_list[i] = ++count;
Jan Beulich8e0629c2014-06-02 14:58:25 +0100582 io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
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 Duyckfbfda892012-10-15 10:19:49 -0700594void 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 Torvalds1da177e2005-04-16 15:20:36 -0700597{
Alexander Duyckfbfda892012-10-15 10:19:49 -0700598 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
599 phys_addr_t orig_addr = io_tlb_orig_addr[index];
Becky Brucebc40ac62008-12-22 10:26:08 -0800600
Jan Beulich8e0629c2014-06-02 14:58:25 +0100601 if (orig_addr == INVALID_PHYS_ADDR)
602 return;
Alexander Duyckfbfda892012-10-15 10:19:49 -0700603 orig_addr += (unsigned long)tlb_addr & ((1 << IO_TLB_SHIFT) - 1);
Keir Fraserdf336d12007-07-21 04:37:24 -0700604
John W. Linvillede69e0f2005-09-29 14:44:57 -0700605 switch (target) {
606 case SYNC_FOR_CPU:
607 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700608 swiotlb_bounce(orig_addr, tlb_addr,
Alexander Duyckfbfda892012-10-15 10:19:49 -0700609 size, DMA_FROM_DEVICE);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100610 else
611 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700612 break;
613 case SYNC_FOR_DEVICE:
614 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Alexander Duyckaf51a9f2012-10-15 10:19:55 -0700615 swiotlb_bounce(orig_addr, tlb_addr,
Alexander Duyckfbfda892012-10-15 10:19:49 -0700616 size, DMA_TO_DEVICE);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100617 else
618 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700619 break;
620 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Christoph Hellwig0176adb2018-01-09 22:15:30 +0100625static 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
635static void *
636swiotlb_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 Hellwigb6e05472018-03-19 11:38:24 +0100645 __phys_to_dma(dev, io_tlb_start),
Jean Delvare05e13bb2018-05-12 11:57:37 +0200646 0, size, DMA_FROM_DEVICE, attrs);
Christoph Hellwig0176adb2018-01-09 22:15:30 +0100647 if (phys_addr == SWIOTLB_MAP_ERROR)
648 goto out_warn;
649
Christoph Hellwigb6e05472018-03-19 11:38:24 +0100650 *dma_handle = __phys_to_dma(dev, phys_addr);
Takashi Iwai9e7f06c2018-04-10 19:05:13 +0200651 if (!dma_coherent_ok(dev, *dma_handle, size))
Christoph Hellwig0176adb2018-01-09 22:15:30 +0100652 goto out_unmap;
653
654 memset(phys_to_virt(phys_addr), 0, size);
655 return phys_to_virt(phys_addr);
656
657out_unmap:
658 dev_warn(dev, "hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
Dan Carpenter698733f2018-04-05 14:19:11 +0300659 (unsigned long long)dev->coherent_dma_mask,
Christoph Hellwig0176adb2018-01-09 22:15:30 +0100660 (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);
668out_warn:
Michel Dänzer892a0be2018-05-01 15:24:11 +0200669 if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit()) {
Christoph Hellwig0176adb2018-01-09 22:15:30 +0100670 dev_warn(dev,
671 "swiotlb: coherent allocation failed, size=%zu\n",
672 size);
673 dump_stack();
674 }
675 return NULL;
676}
677
Christoph Hellwiga25381a2017-12-23 20:56:02 +0100678static 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 Hellwigc4dae362018-08-20 16:21:10 +0200697static 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 Torvalds1da177e2005-04-16 15:20:36 -0700725/*
726 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700727 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *
729 * Once the device is given the dma address, the device owns this memory until
Becky Bruceceb5ac32009-04-08 09:09:15 -0500730 * either swiotlb_unmap_page or swiotlb_dma_sync_single is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
FUJITA Tomonorif98eee82009-01-05 23:59:03 +0900732dma_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 Kozlowski00085f12016-08-03 13:46:00 -0700735 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Christoph Hellwigc4dae362018-08-20 16:21:10 +0200737 phys_addr_t phys = page_to_phys(page) + offset;
FUJITA Tomonori862d1962009-07-10 10:05:02 +0900738 dma_addr_t dev_addr = phys_to_dma(dev, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Eric Sesterhenn34814542006-03-24 18:47:11 +0100740 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /*
Becky Bruceceb5ac32009-04-08 09:09:15 -0500742 * If the address happens to be in the device's DMA window,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * we can safely return the device addr and not worry about bounce
744 * buffering it.
745 */
Geert Uytterhoevenae7871b2016-12-16 14:28:41 +0100746 if (dma_capable(dev, dev_addr, size) && swiotlb_force != SWIOTLB_FORCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return dev_addr;
748
Zoltan Kiss2b2b6142013-09-04 21:11:05 +0100749 trace_swiotlb_bounced(dev, dev_addr, size, swiotlb_force);
Christoph Hellwigc4dae362018-08-20 16:21:10 +0200750 return swiotlb_bounce_page(dev, &phys, size, dir, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * Unmap a single streaming mode DMA translation. The dma_addr and size must
Becky Bruceceb5ac32009-04-08 09:09:15 -0500755 * match what was provided for in a previous swiotlb_map_page call. All
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * 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 Hellwig27744e02018-04-12 09:56:56 +0200761void 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 Torvalds1da177e2005-04-16 15:20:36 -0700764{
FUJITA Tomonori862d1962009-07-10 10:05:02 +0900765 phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Eric Sesterhenn34814542006-03-24 18:47:11 +0100767 BUG_ON(dir == DMA_NONE);
Becky Bruce7fcebbd2009-04-08 09:09:19 -0500768
FUJITA Tomonori02ca6462009-07-23 11:18:49 +0900769 if (is_swiotlb_buffer(paddr)) {
Alexander Duyck0443fa02016-11-02 07:13:02 -0400770 swiotlb_tbl_unmap_single(hwdev, paddr, size, dir, attrs);
Becky Bruce7fcebbd2009-04-08 09:09:19 -0500771 return;
772 }
773
774 if (dir != DMA_FROM_DEVICE)
775 return;
776
FUJITA Tomonori02ca6462009-07-23 11:18:49 +0900777 /*
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 Bruce7fcebbd2009-04-08 09:09:19 -0500784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/*
787 * Make physical memory consistent for a single streaming mode DMA translation
788 * after a transfer.
789 *
Becky Bruceceb5ac32009-04-08 09:09:15 -0500790 * If you perform a swiotlb_map_page() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700791 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700793 * 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 Mortonbe6b0262007-02-12 00:52:17 -0800796static void
John W. Linville8270f3f2005-09-29 14:43:32 -0700797swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
Konrad Rzeszutek Wilkd7ef1532010-05-28 11:37:10 -0400798 size_t size, enum dma_data_direction dir,
799 enum dma_sync_target target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
FUJITA Tomonori862d1962009-07-10 10:05:02 +0900801 phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Eric Sesterhenn34814542006-03-24 18:47:11 +0100803 BUG_ON(dir == DMA_NONE);
Becky Bruce380d6872009-04-08 09:09:20 -0500804
FUJITA Tomonori02ca6462009-07-23 11:18:49 +0900805 if (is_swiotlb_buffer(paddr)) {
Alexander Duyckfbfda892012-10-15 10:19:49 -0700806 swiotlb_tbl_sync_single(hwdev, paddr, size, dir, target);
Becky Bruce380d6872009-04-08 09:09:20 -0500807 return;
808 }
809
810 if (dir != DMA_FROM_DEVICE)
811 return;
812
FUJITA Tomonori02ca6462009-07-23 11:18:49 +0900813 dma_mark_clean(phys_to_virt(paddr), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816void
John W. Linville8270f3f2005-09-29 14:43:32 -0700817swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900818 size_t size, enum dma_data_direction dir)
John W. Linville8270f3f2005-09-29 14:43:32 -0700819{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700820 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700821}
822
823void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900825 size_t size, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700827 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830/*
831 * Map a set of buffers described by scatterlist in streaming mode for DMA.
Becky Bruceceb5ac32009-04-08 09:09:15 -0500832 * This is the scatter-gather version of the above swiotlb_map_page
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 * 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 Bruceceb5ac32009-04-08 09:09:15 -0500837 * Device ownership issues as mentioned above for swiotlb_map_page are the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 * same here.
839 */
840int
Christoph Hellwig4803b442018-08-20 15:56:05 +0200841swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nelems,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700842 enum dma_data_direction dir, unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200844 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 int i;
846
Jens Axboedbfd49f2007-05-11 14:56:18 +0200847 for_each_sg(sgl, sg, nelems, i) {
Christoph Hellwig4803b442018-08-20 15:56:05 +0200848 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 Song4d86ec72013-08-05 17:30:47 +0100852 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Christoph Hellwig4803b442018-08-20 15:56:05 +0200854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return nelems;
Christoph Hellwig4803b442018-08-20 15:56:05 +0200856
857out_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 Torvalds1da177e2005-04-16 15:20:36 -0700862}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/*
865 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
Becky Bruceceb5ac32009-04-08 09:09:15 -0500866 * concerning calls here are the same as for swiotlb_unmap_page() above.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
868void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700869swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700870 int nelems, enum dma_data_direction dir,
871 unsigned long attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200873 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int i;
875
Eric Sesterhenn34814542006-03-24 18:47:11 +0100876 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Becky Bruce7fcebbd2009-04-08 09:09:19 -0500878 for_each_sg(sgl, sg, nelems, i)
Christoph Hellwig27744e02018-04-12 09:56:56 +0200879 swiotlb_unmap_page(hwdev, sg->dma_address, sg_dma_len(sg), dir,
Alexander Duyck0443fa02016-11-02 07:13:02 -0400880 attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/*
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 Mortonbe6b0262007-02-12 00:52:17 -0800890static void
Jens Axboedbfd49f2007-05-11 14:56:18 +0200891swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
Konrad Rzeszutek Wilkd7ef1532010-05-28 11:37:10 -0400892 int nelems, enum dma_data_direction dir,
893 enum dma_sync_target target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200895 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int i;
897
Becky Bruce380d6872009-04-08 09:09:20 -0500898 for_each_sg(sgl, sg, nelems, i)
899 swiotlb_sync_single(hwdev, sg->dma_address,
EunBong Song4d86ec72013-08-05 17:30:47 +0100900 sg_dma_len(sg), dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903void
John W. Linville8270f3f2005-09-29 14:43:32 -0700904swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900905 int nelems, enum dma_data_direction dir)
John W. Linville8270f3f2005-09-29 14:43:32 -0700906{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700907 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700908}
909
910void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900912 int nelems, enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700914 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700918 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700920 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * this function.
922 */
923int
Jan Beulich563aaf02007-02-05 18:51:25 -0800924swiotlb_dma_supported(struct device *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Christoph Hellwigb6e05472018-03-19 11:38:24 +0100926 return __phys_to_dma(hwdev, io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
Christoph Hellwig251533e2018-01-09 16:44:16 +0100928
Christoph Hellwig251533e2018-01-09 16:44:16 +0100929void *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 Hellwig0176adb2018-01-09 22:15:30 +0100934 /* temporary workaround: */
935 if (gfp & __GFP_NOWARN)
936 attrs |= DMA_ATTR_NO_WARN;
937
Christoph Hellwig251533e2018-01-09 16:44:16 +0100938 /*
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 Hellwig0176adb2018-01-09 22:15:30 +0100947 vaddr = swiotlb_alloc_buffer(dev, size, dma_handle, attrs);
Christoph Hellwig251533e2018-01-09 16:44:16 +0100948 return vaddr;
949}
950
951void swiotlb_free(struct device *dev, size_t size, void *vaddr,
952 dma_addr_t dma_addr, unsigned long attrs)
953{
Christoph Hellwiga25381a2017-12-23 20:56:02 +0100954 if (!swiotlb_free_buffer(dev, size, dma_addr))
Christoph Hellwig251533e2018-01-09 16:44:16 +0100955 dma_direct_free(dev, size, vaddr, dma_addr, attrs);
956}
957
958const struct dma_map_ops swiotlb_dma_ops = {
Christoph Hellwigdff8d6c2018-08-16 15:30:39 +0300959 .mapping_error = dma_direct_mapping_error,
Christoph Hellwig251533e2018-01-09 16:44:16 +0100960 .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 Hellwig66bdb142018-04-09 11:15:17 +0200970 .dma_supported = dma_direct_supported,
Christoph Hellwig251533e2018-01-09 16:44:16 +0100971};
Christoph Hellwig210d0792018-06-28 13:59:25 +0200972EXPORT_SYMBOL(swiotlb_dma_ops);