blob: f99b79d7e123539f1fd8e310a09d0013cfb45a6f [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Dynamic DMA mapping support.
4 *
5 * This implementation is a fallback for platforms that do not support
6 * I/O TLBs (aka DMA address translation hardware).
7 * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
8 * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
9 * Copyright (C) 2000, 2003 Hewlett-Packard Co
10 * David Mosberger-Tang <davidm@hpl.hp.com>
11 *
12 * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API.
13 * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
14 * unnecessary i-cache flushing.
15 * 04/07/.. ak Better overflow handling. Assorted fixes.
16 * 05/09/10 linville Add support for syncing ranges, support syncing for
17 * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
18 * 08/12/11 beckyb Add highmem support
19 */
20
21#define pr_fmt(fmt) "software IO TLB: " fmt
22
23#include <linux/cache.h>
24#include <linux/dma-direct.h>
25#include <linux/mm.h>
26#include <linux/export.h>
27#include <linux/spinlock.h>
28#include <linux/string.h>
29#include <linux/swiotlb.h>
30#include <linux/pfn.h>
31#include <linux/types.h>
32#include <linux/ctype.h>
33#include <linux/highmem.h>
34#include <linux/gfp.h>
35#include <linux/scatterlist.h>
36#include <linux/mem_encrypt.h>
37#include <linux/set_memory.h>
David Brazdil0f672f62019-12-10 10:32:29 +000038#ifdef CONFIG_DEBUG_FS
39#include <linux/debugfs.h>
40#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041
42#include <asm/io.h>
43#include <asm/dma.h>
44
45#include <linux/init.h>
David Brazdil0f672f62019-12-10 10:32:29 +000046#include <linux/memblock.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047#include <linux/iommu-helper.h>
48
49#define CREATE_TRACE_POINTS
50#include <trace/events/swiotlb.h>
51
52#define OFFSET(val,align) ((unsigned long) \
53 ( (val) & ( (align) - 1)))
54
55#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
56
57/*
58 * Minimum IO TLB size to bother booting with. Systems with mainly
59 * 64bit capable cards will only lightly use the swiotlb. If we can't
60 * allocate a contiguous 1MB, we're probably in trouble anyway.
61 */
62#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
63
64enum swiotlb_force swiotlb_force;
65
66/*
67 * Used to do a quick range check in swiotlb_tbl_unmap_single and
68 * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this
69 * API.
70 */
David Brazdil0f672f62019-12-10 10:32:29 +000071phys_addr_t io_tlb_start, io_tlb_end;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000072
73/*
74 * The number of IO TLB blocks (in groups of 64) between io_tlb_start and
75 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
76 */
77static unsigned long io_tlb_nslabs;
78
79/*
David Brazdil0f672f62019-12-10 10:32:29 +000080 * The number of used IO TLB block
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081 */
David Brazdil0f672f62019-12-10 10:32:29 +000082static unsigned long io_tlb_used;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083
84/*
85 * 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/*
92 * 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/*
98 * We need to save away the original address corresponding to a mapped entry
99 * for the sync operations.
100 */
101#define INVALID_PHYS_ADDR (~(phys_addr_t)0)
102static phys_addr_t *io_tlb_orig_addr;
103
104/*
105 * Protect the above data structures in the map and unmap calls
106 */
107static DEFINE_SPINLOCK(io_tlb_lock);
108
109static int late_alloc;
110
111static int __init
112setup_io_tlb_npages(char *str)
113{
114 if (isdigit(*str)) {
115 io_tlb_nslabs = simple_strtoul(str, &str, 0);
116 /* avoid tail segment of size < IO_TLB_SEGSIZE */
117 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
118 }
119 if (*str == ',')
120 ++str;
121 if (!strcmp(str, "force")) {
122 swiotlb_force = SWIOTLB_FORCE;
123 } else if (!strcmp(str, "noforce")) {
124 swiotlb_force = SWIOTLB_NO_FORCE;
125 io_tlb_nslabs = 1;
126 }
127
128 return 0;
129}
130early_param("swiotlb", setup_io_tlb_npages);
David Brazdil0f672f62019-12-10 10:32:29 +0000131
132static bool no_iotlb_memory;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000133
134unsigned long swiotlb_nr_tbl(void)
135{
David Brazdil0f672f62019-12-10 10:32:29 +0000136 return unlikely(no_iotlb_memory) ? 0 : io_tlb_nslabs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137}
138EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
139
140unsigned int swiotlb_max_segment(void)
141{
David Brazdil0f672f62019-12-10 10:32:29 +0000142 return unlikely(no_iotlb_memory) ? 0 : max_segment;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143}
144EXPORT_SYMBOL_GPL(swiotlb_max_segment);
145
146void swiotlb_set_max_segment(unsigned int val)
147{
148 if (swiotlb_force == SWIOTLB_FORCE)
149 max_segment = 1;
150 else
151 max_segment = rounddown(val, PAGE_SIZE);
152}
153
154/* default to 64MB */
155#define IO_TLB_DEFAULT_SIZE (64UL<<20)
156unsigned long swiotlb_size_or_default(void)
157{
158 unsigned long size;
159
160 size = io_tlb_nslabs << IO_TLB_SHIFT;
161
162 return size ? size : (IO_TLB_DEFAULT_SIZE);
163}
164
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165void swiotlb_print_info(void)
166{
167 unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
168
169 if (no_iotlb_memory) {
170 pr_warn("No low mem\n");
171 return;
172 }
173
174 pr_info("mapped [mem %#010llx-%#010llx] (%luMB)\n",
175 (unsigned long long)io_tlb_start,
176 (unsigned long long)io_tlb_end,
177 bytes >> 20);
178}
179
180/*
181 * Early SWIOTLB allocation may be too early to allow an architecture to
182 * perform the desired operations. This function allows the architecture to
183 * call SWIOTLB when the operations are possible. It needs to be called
184 * before the SWIOTLB memory is used.
185 */
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);
196 set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
197 memset(vaddr, 0, bytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198}
199
200int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
201{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000202 unsigned long i, bytes;
David Brazdil0f672f62019-12-10 10:32:29 +0000203 size_t alloc_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204
205 bytes = nslabs << IO_TLB_SHIFT;
206
207 io_tlb_nslabs = nslabs;
208 io_tlb_start = __pa(tlb);
209 io_tlb_end = io_tlb_start + bytes;
210
211 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212 * 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 */
David Brazdil0f672f62019-12-10 10:32:29 +0000216 alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(int));
217 io_tlb_list = memblock_alloc(alloc_size, PAGE_SIZE);
218 if (!io_tlb_list)
219 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
220 __func__, alloc_size, PAGE_SIZE);
221
222 alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t));
223 io_tlb_orig_addr = memblock_alloc(alloc_size, PAGE_SIZE);
224 if (!io_tlb_orig_addr)
225 panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
226 __func__, alloc_size, PAGE_SIZE);
227
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000228 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;
Olivier Deprez0e641232021-09-23 10:07:05 +0200233 no_iotlb_memory = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234
235 if (verbose)
236 swiotlb_print_info();
237
238 swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
239 return 0;
240}
241
242/*
243 * Statically reserve bounce buffer space and initialize bounce buffer data
244 * structures for the software IO TLB used to implement the DMA API.
245 */
246void __init
247swiotlb_init(int verbose)
248{
249 size_t default_size = IO_TLB_DEFAULT_SIZE;
250 unsigned char *vstart;
251 unsigned long bytes;
252
253 if (!io_tlb_nslabs) {
254 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
255 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
256 }
257
258 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
259
260 /* Get IO TLB memory from the low pages */
David Brazdil0f672f62019-12-10 10:32:29 +0000261 vstart = memblock_alloc_low(PAGE_ALIGN(bytes), PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262 if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose))
263 return;
264
Olivier Deprez0e641232021-09-23 10:07:05 +0200265 if (io_tlb_start) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 memblock_free_early(io_tlb_start,
267 PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
Olivier Deprez0e641232021-09-23 10:07:05 +0200268 io_tlb_start = 0;
269 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270 pr_warn("Cannot allocate buffer");
271 no_iotlb_memory = true;
272}
273
274/*
275 * Systems with larger DMA zones (those that don't support ISA) can
276 * initialize the swiotlb later using the slab allocator if needed.
277 * This should be just like above, but with some error catching.
278 */
279int
280swiotlb_late_init_with_default_size(size_t default_size)
281{
282 unsigned long bytes, req_nslabs = io_tlb_nslabs;
283 unsigned char *vstart = NULL;
284 unsigned int order;
285 int rc = 0;
286
287 if (!io_tlb_nslabs) {
288 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
289 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
290 }
291
292 /*
293 * Get IO TLB memory from the low pages
294 */
295 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
296 io_tlb_nslabs = SLABS_PER_PAGE << order;
297 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
298
299 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
300 vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
301 order);
302 if (vstart)
303 break;
304 order--;
305 }
306
307 if (!vstart) {
308 io_tlb_nslabs = req_nslabs;
309 return -ENOMEM;
310 }
311 if (order != get_order(bytes)) {
312 pr_warn("only able to allocate %ld MB\n",
313 (PAGE_SIZE << order) >> 20);
314 io_tlb_nslabs = SLABS_PER_PAGE << order;
315 }
316 rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs);
317 if (rc)
318 free_pages((unsigned long)vstart, order);
319
320 return rc;
321}
322
David Brazdil0f672f62019-12-10 10:32:29 +0000323static void swiotlb_cleanup(void)
324{
325 io_tlb_end = 0;
326 io_tlb_start = 0;
327 io_tlb_nslabs = 0;
328 max_segment = 0;
329}
330
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000331int
332swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
333{
334 unsigned long i, bytes;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335
336 bytes = nslabs << IO_TLB_SHIFT;
337
338 io_tlb_nslabs = nslabs;
339 io_tlb_start = virt_to_phys(tlb);
340 io_tlb_end = io_tlb_start + bytes;
341
342 set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT);
343 memset(tlb, 0, bytes);
344
345 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000346 * Allocate and initialize the free list array. This array is used
347 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
348 * between io_tlb_start and io_tlb_end.
349 */
350 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
351 get_order(io_tlb_nslabs * sizeof(int)));
352 if (!io_tlb_list)
353 goto cleanup3;
354
355 io_tlb_orig_addr = (phys_addr_t *)
356 __get_free_pages(GFP_KERNEL,
357 get_order(io_tlb_nslabs *
358 sizeof(phys_addr_t)));
359 if (!io_tlb_orig_addr)
360 goto cleanup4;
361
362 for (i = 0; i < io_tlb_nslabs; i++) {
363 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
364 io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
365 }
366 io_tlb_index = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +0200367 no_iotlb_memory = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368
369 swiotlb_print_info();
370
371 late_alloc = 1;
372
373 swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
374
375 return 0;
376
377cleanup4:
378 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
379 sizeof(int)));
380 io_tlb_list = NULL;
381cleanup3:
David Brazdil0f672f62019-12-10 10:32:29 +0000382 swiotlb_cleanup();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000383 return -ENOMEM;
384}
385
386void __init swiotlb_exit(void)
387{
388 if (!io_tlb_orig_addr)
389 return;
390
391 if (late_alloc) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000392 free_pages((unsigned long)io_tlb_orig_addr,
393 get_order(io_tlb_nslabs * sizeof(phys_addr_t)));
394 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
395 sizeof(int)));
396 free_pages((unsigned long)phys_to_virt(io_tlb_start),
397 get_order(io_tlb_nslabs << IO_TLB_SHIFT));
398 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 memblock_free_late(__pa(io_tlb_orig_addr),
400 PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)));
401 memblock_free_late(__pa(io_tlb_list),
402 PAGE_ALIGN(io_tlb_nslabs * sizeof(int)));
403 memblock_free_late(io_tlb_start,
404 PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
405 }
David Brazdil0f672f62019-12-10 10:32:29 +0000406 swiotlb_cleanup();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000407}
408
409/*
David Brazdil0f672f62019-12-10 10:32:29 +0000410 * Bounce: copy the swiotlb buffer from or back to the original dma location
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000411 */
412static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
413 size_t size, enum dma_data_direction dir)
414{
415 unsigned long pfn = PFN_DOWN(orig_addr);
416 unsigned char *vaddr = phys_to_virt(tlb_addr);
417
418 if (PageHighMem(pfn_to_page(pfn))) {
419 /* The buffer does not have a mapping. Map it in and copy */
420 unsigned int offset = orig_addr & ~PAGE_MASK;
421 char *buffer;
422 unsigned int sz = 0;
423 unsigned long flags;
424
425 while (size) {
426 sz = min_t(size_t, PAGE_SIZE - offset, size);
427
428 local_irq_save(flags);
429 buffer = kmap_atomic(pfn_to_page(pfn));
430 if (dir == DMA_TO_DEVICE)
431 memcpy(vaddr, buffer + offset, sz);
432 else
433 memcpy(buffer + offset, vaddr, sz);
434 kunmap_atomic(buffer);
435 local_irq_restore(flags);
436
437 size -= sz;
438 pfn++;
439 vaddr += sz;
440 offset = 0;
441 }
442 } else if (dir == DMA_TO_DEVICE) {
443 memcpy(vaddr, phys_to_virt(orig_addr), size);
444 } else {
445 memcpy(phys_to_virt(orig_addr), vaddr, size);
446 }
447}
448
449phys_addr_t swiotlb_tbl_map_single(struct device *hwdev,
450 dma_addr_t tbl_dma_addr,
David Brazdil0f672f62019-12-10 10:32:29 +0000451 phys_addr_t orig_addr,
452 size_t mapping_size,
453 size_t alloc_size,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000454 enum dma_data_direction dir,
455 unsigned long attrs)
456{
457 unsigned long flags;
458 phys_addr_t tlb_addr;
459 unsigned int nslots, stride, index, wrap;
460 int i;
461 unsigned long mask;
462 unsigned long offset_slots;
463 unsigned long max_slots;
David Brazdil0f672f62019-12-10 10:32:29 +0000464 unsigned long tmp_io_tlb_used;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000465
466 if (no_iotlb_memory)
467 panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
468
469 if (mem_encrypt_active())
David Brazdil0f672f62019-12-10 10:32:29 +0000470 pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
471
472 if (mapping_size > alloc_size) {
473 dev_warn_once(hwdev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
474 mapping_size, alloc_size);
475 return (phys_addr_t)DMA_MAPPING_ERROR;
476 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000477
478 mask = dma_get_seg_boundary(hwdev);
479
480 tbl_dma_addr &= mask;
481
482 offset_slots = ALIGN(tbl_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
483
484 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000485 * Carefully handle integer overflow which can occur when mask == ~0UL.
486 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000487 max_slots = mask + 1
488 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
489 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
490
491 /*
492 * For mappings greater than or equal to a page, we limit the stride
493 * (and hence alignment) to a page size.
494 */
David Brazdil0f672f62019-12-10 10:32:29 +0000495 nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
496 if (alloc_size >= PAGE_SIZE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
498 else
499 stride = 1;
500
501 BUG_ON(!nslots);
502
503 /*
504 * Find suitable number of IO TLB entries size that will fit this
505 * request and allocate a buffer from that IO TLB pool.
506 */
507 spin_lock_irqsave(&io_tlb_lock, flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000508
509 if (unlikely(nslots > io_tlb_nslabs - io_tlb_used))
510 goto not_found;
511
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000512 index = ALIGN(io_tlb_index, stride);
513 if (index >= io_tlb_nslabs)
514 index = 0;
515 wrap = index;
516
517 do {
518 while (iommu_is_span_boundary(index, nslots, offset_slots,
519 max_slots)) {
520 index += stride;
521 if (index >= io_tlb_nslabs)
522 index = 0;
523 if (index == wrap)
524 goto not_found;
525 }
526
527 /*
528 * If we find a slot that indicates we have 'nslots' number of
529 * contiguous buffers, we allocate the buffers from that slot
530 * and mark the entries as '0' indicating unavailable.
531 */
532 if (io_tlb_list[index] >= nslots) {
533 int count = 0;
534
535 for (i = index; i < (int) (index + nslots); i++)
536 io_tlb_list[i] = 0;
537 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
538 io_tlb_list[i] = ++count;
539 tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT);
540
541 /*
542 * Update the indices to avoid searching in the next
543 * round.
544 */
545 io_tlb_index = ((index + nslots) < io_tlb_nslabs
546 ? (index + nslots) : 0);
547
548 goto found;
549 }
550 index += stride;
551 if (index >= io_tlb_nslabs)
552 index = 0;
553 } while (index != wrap);
554
555not_found:
David Brazdil0f672f62019-12-10 10:32:29 +0000556 tmp_io_tlb_used = io_tlb_used;
557
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000558 spin_unlock_irqrestore(&io_tlb_lock, flags);
559 if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit())
David Brazdil0f672f62019-12-10 10:32:29 +0000560 dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n",
561 alloc_size, io_tlb_nslabs, tmp_io_tlb_used);
562 return (phys_addr_t)DMA_MAPPING_ERROR;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563found:
David Brazdil0f672f62019-12-10 10:32:29 +0000564 io_tlb_used += nslots;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565 spin_unlock_irqrestore(&io_tlb_lock, flags);
566
567 /*
568 * Save away the mapping from the original address to the DMA address.
569 * This is needed when we sync the memory. Then we sync the buffer if
570 * needed.
571 */
572 for (i = 0; i < nslots; i++)
573 io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
574 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
575 (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
David Brazdil0f672f62019-12-10 10:32:29 +0000576 swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_TO_DEVICE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000577
578 return tlb_addr;
579}
580
581/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000582 * tlb_addr is the physical address of the bounce buffer to unmap.
583 */
584void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
David Brazdil0f672f62019-12-10 10:32:29 +0000585 size_t mapping_size, size_t alloc_size,
586 enum dma_data_direction dir, unsigned long attrs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000587{
588 unsigned long flags;
David Brazdil0f672f62019-12-10 10:32:29 +0000589 int i, count, nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
591 phys_addr_t orig_addr = io_tlb_orig_addr[index];
592
593 /*
594 * First, sync the memory before unmapping the entry
595 */
596 if (orig_addr != INVALID_PHYS_ADDR &&
597 !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
598 ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
David Brazdil0f672f62019-12-10 10:32:29 +0000599 swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_FROM_DEVICE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000600
601 /*
602 * Return the buffer to the free list by setting the corresponding
603 * entries to indicate the number of contiguous entries available.
604 * While returning the entries to the free list, we merge the entries
605 * with slots below and above the pool being returned.
606 */
607 spin_lock_irqsave(&io_tlb_lock, flags);
608 {
609 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
610 io_tlb_list[index + nslots] : 0);
611 /*
612 * Step 1: return the slots to the free list, merging the
613 * slots with superceeding slots
614 */
615 for (i = index + nslots - 1; i >= index; i--) {
616 io_tlb_list[i] = ++count;
617 io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
618 }
619 /*
620 * Step 2: merge the returned slots with the preceding slots,
621 * if available (non zero)
622 */
623 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
624 io_tlb_list[i] = ++count;
David Brazdil0f672f62019-12-10 10:32:29 +0000625
626 io_tlb_used -= nslots;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000627 }
628 spin_unlock_irqrestore(&io_tlb_lock, flags);
629}
630
631void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr,
632 size_t size, enum dma_data_direction dir,
633 enum dma_sync_target target)
634{
635 int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
636 phys_addr_t orig_addr = io_tlb_orig_addr[index];
637
638 if (orig_addr == INVALID_PHYS_ADDR)
639 return;
640 orig_addr += (unsigned long)tlb_addr & ((1 << IO_TLB_SHIFT) - 1);
641
642 switch (target) {
643 case SYNC_FOR_CPU:
644 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
645 swiotlb_bounce(orig_addr, tlb_addr,
646 size, DMA_FROM_DEVICE);
647 else
648 BUG_ON(dir != DMA_TO_DEVICE);
649 break;
650 case SYNC_FOR_DEVICE:
651 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
652 swiotlb_bounce(orig_addr, tlb_addr,
653 size, DMA_TO_DEVICE);
654 else
655 BUG_ON(dir != DMA_FROM_DEVICE);
656 break;
657 default:
658 BUG();
659 }
660}
661
David Brazdil0f672f62019-12-10 10:32:29 +0000662/*
663 * Create a swiotlb mapping for the buffer at @phys, and in case of DMAing
664 * to the device copy the data into it as well.
665 */
666bool swiotlb_map(struct device *dev, phys_addr_t *phys, dma_addr_t *dma_addr,
667 size_t size, enum dma_data_direction dir, unsigned long attrs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668{
David Brazdil0f672f62019-12-10 10:32:29 +0000669 trace_swiotlb_bounced(dev, *dma_addr, size, swiotlb_force);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000670
David Brazdil0f672f62019-12-10 10:32:29 +0000671 if (unlikely(swiotlb_force == SWIOTLB_NO_FORCE)) {
672 dev_warn_ratelimited(dev,
673 "Cannot do DMA to address %pa\n", phys);
674 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000675 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000676
David Brazdil0f672f62019-12-10 10:32:29 +0000677 /* Oh well, have to allocate and map a bounce buffer. */
678 *phys = swiotlb_tbl_map_single(dev, __phys_to_dma(dev, io_tlb_start),
679 *phys, size, size, dir, attrs);
680 if (*phys == (phys_addr_t)DMA_MAPPING_ERROR)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681 return false;
682
David Brazdil0f672f62019-12-10 10:32:29 +0000683 /* Ensure that the address returned is DMA'ble */
684 *dma_addr = __phys_to_dma(dev, *phys);
685 if (unlikely(!dma_capable(dev, *dma_addr, size))) {
686 swiotlb_tbl_unmap_single(dev, *phys, size, size, dir,
687 attrs | DMA_ATTR_SKIP_CPU_SYNC);
688 return false;
689 }
690
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000691 return true;
692}
693
David Brazdil0f672f62019-12-10 10:32:29 +0000694size_t swiotlb_max_mapping_size(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000695{
David Brazdil0f672f62019-12-10 10:32:29 +0000696 return ((size_t)1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
697}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000698
David Brazdil0f672f62019-12-10 10:32:29 +0000699bool is_swiotlb_active(void)
700{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000701 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000702 * When SWIOTLB is initialized, even if io_tlb_start points to physical
703 * address zero, io_tlb_end surely doesn't.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000704 */
David Brazdil0f672f62019-12-10 10:32:29 +0000705 return io_tlb_end != 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000706}
707
David Brazdil0f672f62019-12-10 10:32:29 +0000708#ifdef CONFIG_DEBUG_FS
709
710static int __init swiotlb_create_debugfs(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000711{
David Brazdil0f672f62019-12-10 10:32:29 +0000712 struct dentry *root;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000713
David Brazdil0f672f62019-12-10 10:32:29 +0000714 root = debugfs_create_dir("swiotlb", NULL);
715 debugfs_create_ulong("io_tlb_nslabs", 0400, root, &io_tlb_nslabs);
716 debugfs_create_ulong("io_tlb_used", 0400, root, &io_tlb_used);
717 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000718}
719
David Brazdil0f672f62019-12-10 10:32:29 +0000720late_initcall(swiotlb_create_debugfs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721
David Brazdil0f672f62019-12-10 10:32:29 +0000722#endif