]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-only | |
2 | /* | |
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/dma-map-ops.h> | |
26 | #include <linux/mm.h> | |
27 | #include <linux/export.h> | |
28 | #include <linux/spinlock.h> | |
29 | #include <linux/string.h> | |
30 | #include <linux/swiotlb.h> | |
31 | #include <linux/pfn.h> | |
32 | #include <linux/types.h> | |
33 | #include <linux/ctype.h> | |
34 | #include <linux/highmem.h> | |
35 | #include <linux/gfp.h> | |
36 | #include <linux/scatterlist.h> | |
37 | #include <linux/mem_encrypt.h> | |
38 | #include <linux/set_memory.h> | |
39 | #ifdef CONFIG_DEBUG_FS | |
40 | #include <linux/debugfs.h> | |
41 | #endif | |
42 | ||
43 | #include <asm/io.h> | |
44 | #include <asm/dma.h> | |
45 | ||
46 | #include <linux/init.h> | |
47 | #include <linux/memblock.h> | |
48 | #include <linux/iommu-helper.h> | |
49 | ||
50 | #define CREATE_TRACE_POINTS | |
51 | #include <trace/events/swiotlb.h> | |
52 | ||
53 | #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT)) | |
54 | ||
55 | /* | |
56 | * Minimum IO TLB size to bother booting with. Systems with mainly | |
57 | * 64bit capable cards will only lightly use the swiotlb. If we can't | |
58 | * allocate a contiguous 1MB, we're probably in trouble anyway. | |
59 | */ | |
60 | #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT) | |
61 | ||
62 | enum swiotlb_force swiotlb_force; | |
63 | ||
64 | /* | |
65 | * Used to do a quick range check in swiotlb_tbl_unmap_single and | |
66 | * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this | |
67 | * API. | |
68 | */ | |
69 | phys_addr_t io_tlb_start, io_tlb_end; | |
70 | ||
71 | /* | |
72 | * The number of IO TLB blocks (in groups of 64) between io_tlb_start and | |
73 | * io_tlb_end. This is command line adjustable via setup_io_tlb_npages. | |
74 | */ | |
75 | static unsigned long io_tlb_nslabs; | |
76 | ||
77 | /* | |
78 | * The number of used IO TLB block | |
79 | */ | |
80 | static unsigned long io_tlb_used; | |
81 | ||
82 | /* | |
83 | * This is a free list describing the number of free entries available from | |
84 | * each index | |
85 | */ | |
86 | static unsigned int *io_tlb_list; | |
87 | static unsigned int io_tlb_index; | |
88 | ||
89 | /* | |
90 | * Max segment that we can provide which (if pages are contingous) will | |
91 | * not be bounced (unless SWIOTLB_FORCE is set). | |
92 | */ | |
93 | static unsigned int max_segment; | |
94 | ||
95 | /* | |
96 | * We need to save away the original address corresponding to a mapped entry | |
97 | * for the sync operations. | |
98 | */ | |
99 | #define INVALID_PHYS_ADDR (~(phys_addr_t)0) | |
100 | static phys_addr_t *io_tlb_orig_addr; | |
101 | ||
102 | /* | |
103 | * The mapped buffer's size should be validated during a sync operation. | |
104 | */ | |
105 | static size_t *io_tlb_alloc_size; | |
106 | ||
107 | /* | |
108 | * Protect the above data structures in the map and unmap calls | |
109 | */ | |
110 | static DEFINE_SPINLOCK(io_tlb_lock); | |
111 | ||
112 | static int late_alloc; | |
113 | ||
114 | static int __init | |
115 | setup_io_tlb_npages(char *str) | |
116 | { | |
117 | if (isdigit(*str)) { | |
118 | io_tlb_nslabs = simple_strtoul(str, &str, 0); | |
119 | /* avoid tail segment of size < IO_TLB_SEGSIZE */ | |
120 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); | |
121 | } | |
122 | if (*str == ',') | |
123 | ++str; | |
124 | if (!strcmp(str, "force")) { | |
125 | swiotlb_force = SWIOTLB_FORCE; | |
126 | } else if (!strcmp(str, "noforce")) { | |
127 | swiotlb_force = SWIOTLB_NO_FORCE; | |
128 | io_tlb_nslabs = 1; | |
129 | } | |
130 | ||
131 | return 0; | |
132 | } | |
133 | early_param("swiotlb", setup_io_tlb_npages); | |
134 | ||
135 | static bool no_iotlb_memory; | |
136 | ||
137 | unsigned long swiotlb_nr_tbl(void) | |
138 | { | |
139 | return unlikely(no_iotlb_memory) ? 0 : io_tlb_nslabs; | |
140 | } | |
141 | EXPORT_SYMBOL_GPL(swiotlb_nr_tbl); | |
142 | ||
143 | unsigned int swiotlb_max_segment(void) | |
144 | { | |
145 | return unlikely(no_iotlb_memory) ? 0 : max_segment; | |
146 | } | |
147 | EXPORT_SYMBOL_GPL(swiotlb_max_segment); | |
148 | ||
149 | void swiotlb_set_max_segment(unsigned int val) | |
150 | { | |
151 | if (swiotlb_force == SWIOTLB_FORCE) | |
152 | max_segment = 1; | |
153 | else | |
154 | max_segment = rounddown(val, PAGE_SIZE); | |
155 | } | |
156 | ||
157 | unsigned long swiotlb_size_or_default(void) | |
158 | { | |
159 | unsigned long size; | |
160 | ||
161 | size = io_tlb_nslabs << IO_TLB_SHIFT; | |
162 | ||
163 | return size ? size : (IO_TLB_DEFAULT_SIZE); | |
164 | } | |
165 | ||
166 | void __init swiotlb_adjust_size(unsigned long new_size) | |
167 | { | |
168 | unsigned long size; | |
169 | ||
170 | /* | |
171 | * If swiotlb parameter has not been specified, give a chance to | |
172 | * architectures such as those supporting memory encryption to | |
173 | * adjust/expand SWIOTLB size for their use. | |
174 | */ | |
175 | if (!io_tlb_nslabs) { | |
176 | size = ALIGN(new_size, IO_TLB_SIZE); | |
177 | io_tlb_nslabs = size >> IO_TLB_SHIFT; | |
178 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); | |
179 | ||
180 | pr_info("SWIOTLB bounce buffer size adjusted to %luMB", size >> 20); | |
181 | } | |
182 | } | |
183 | ||
184 | void swiotlb_print_info(void) | |
185 | { | |
186 | unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT; | |
187 | ||
188 | if (no_iotlb_memory) { | |
189 | pr_warn("No low mem\n"); | |
190 | return; | |
191 | } | |
192 | ||
193 | pr_info("mapped [mem %pa-%pa] (%luMB)\n", &io_tlb_start, &io_tlb_end, | |
194 | bytes >> 20); | |
195 | } | |
196 | ||
197 | static inline unsigned long io_tlb_offset(unsigned long val) | |
198 | { | |
199 | return val & (IO_TLB_SEGSIZE - 1); | |
200 | } | |
201 | ||
202 | static inline unsigned long nr_slots(u64 val) | |
203 | { | |
204 | return DIV_ROUND_UP(val, IO_TLB_SIZE); | |
205 | } | |
206 | ||
207 | /* | |
208 | * Early SWIOTLB allocation may be too early to allow an architecture to | |
209 | * perform the desired operations. This function allows the architecture to | |
210 | * call SWIOTLB when the operations are possible. It needs to be called | |
211 | * before the SWIOTLB memory is used. | |
212 | */ | |
213 | void __init swiotlb_update_mem_attributes(void) | |
214 | { | |
215 | void *vaddr; | |
216 | unsigned long bytes; | |
217 | ||
218 | if (no_iotlb_memory || late_alloc) | |
219 | return; | |
220 | ||
221 | vaddr = phys_to_virt(io_tlb_start); | |
222 | bytes = PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT); | |
223 | set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT); | |
224 | memset(vaddr, 0, bytes); | |
225 | } | |
226 | ||
227 | int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) | |
228 | { | |
229 | unsigned long i, bytes; | |
230 | size_t alloc_size; | |
231 | ||
232 | /* protect against double initialization */ | |
233 | if (WARN_ON_ONCE(io_tlb_start)) | |
234 | return -ENOMEM; | |
235 | ||
236 | bytes = nslabs << IO_TLB_SHIFT; | |
237 | ||
238 | io_tlb_nslabs = nslabs; | |
239 | io_tlb_start = __pa(tlb); | |
240 | io_tlb_end = io_tlb_start + bytes; | |
241 | ||
242 | /* | |
243 | * Allocate and initialize the free list array. This array is used | |
244 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE | |
245 | * between io_tlb_start and io_tlb_end. | |
246 | */ | |
247 | alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(int)); | |
248 | io_tlb_list = memblock_alloc(alloc_size, PAGE_SIZE); | |
249 | if (!io_tlb_list) | |
250 | panic("%s: Failed to allocate %zu bytes align=0x%lx\n", | |
251 | __func__, alloc_size, PAGE_SIZE); | |
252 | ||
253 | alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)); | |
254 | io_tlb_orig_addr = memblock_alloc(alloc_size, PAGE_SIZE); | |
255 | if (!io_tlb_orig_addr) | |
256 | panic("%s: Failed to allocate %zu bytes align=0x%lx\n", | |
257 | __func__, alloc_size, PAGE_SIZE); | |
258 | ||
259 | alloc_size = PAGE_ALIGN(io_tlb_nslabs * sizeof(size_t)); | |
260 | io_tlb_alloc_size = memblock_alloc(alloc_size, PAGE_SIZE); | |
261 | if (!io_tlb_alloc_size) | |
262 | panic("%s: Failed to allocate %zu bytes align=0x%lx\n", | |
263 | __func__, alloc_size, PAGE_SIZE); | |
264 | ||
265 | for (i = 0; i < io_tlb_nslabs; i++) { | |
266 | io_tlb_list[i] = IO_TLB_SEGSIZE - io_tlb_offset(i); | |
267 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; | |
268 | io_tlb_alloc_size[i] = 0; | |
269 | } | |
270 | io_tlb_index = 0; | |
271 | no_iotlb_memory = false; | |
272 | ||
273 | if (verbose) | |
274 | swiotlb_print_info(); | |
275 | ||
276 | swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | /* | |
281 | * Statically reserve bounce buffer space and initialize bounce buffer data | |
282 | * structures for the software IO TLB used to implement the DMA API. | |
283 | */ | |
284 | void __init | |
285 | swiotlb_init(int verbose) | |
286 | { | |
287 | size_t default_size = IO_TLB_DEFAULT_SIZE; | |
288 | unsigned char *vstart; | |
289 | unsigned long bytes; | |
290 | ||
291 | if (!io_tlb_nslabs) { | |
292 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); | |
293 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); | |
294 | } | |
295 | ||
296 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; | |
297 | ||
298 | /* Get IO TLB memory from the low pages */ | |
299 | vstart = memblock_alloc_low(PAGE_ALIGN(bytes), PAGE_SIZE); | |
300 | if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose)) | |
301 | return; | |
302 | ||
303 | if (io_tlb_start) { | |
304 | memblock_free_early(io_tlb_start, | |
305 | PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); | |
306 | io_tlb_start = 0; | |
307 | } | |
308 | pr_warn("Cannot allocate buffer"); | |
309 | no_iotlb_memory = true; | |
310 | } | |
311 | ||
312 | /* | |
313 | * Systems with larger DMA zones (those that don't support ISA) can | |
314 | * initialize the swiotlb later using the slab allocator if needed. | |
315 | * This should be just like above, but with some error catching. | |
316 | */ | |
317 | int | |
318 | swiotlb_late_init_with_default_size(size_t default_size) | |
319 | { | |
320 | unsigned long bytes, req_nslabs = io_tlb_nslabs; | |
321 | unsigned char *vstart = NULL; | |
322 | unsigned int order; | |
323 | int rc = 0; | |
324 | ||
325 | if (!io_tlb_nslabs) { | |
326 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); | |
327 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); | |
328 | } | |
329 | ||
330 | /* | |
331 | * Get IO TLB memory from the low pages | |
332 | */ | |
333 | order = get_order(io_tlb_nslabs << IO_TLB_SHIFT); | |
334 | io_tlb_nslabs = SLABS_PER_PAGE << order; | |
335 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; | |
336 | ||
337 | while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { | |
338 | vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, | |
339 | order); | |
340 | if (vstart) | |
341 | break; | |
342 | order--; | |
343 | } | |
344 | ||
345 | if (!vstart) { | |
346 | io_tlb_nslabs = req_nslabs; | |
347 | return -ENOMEM; | |
348 | } | |
349 | if (order != get_order(bytes)) { | |
350 | pr_warn("only able to allocate %ld MB\n", | |
351 | (PAGE_SIZE << order) >> 20); | |
352 | io_tlb_nslabs = SLABS_PER_PAGE << order; | |
353 | } | |
354 | rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs); | |
355 | if (rc) | |
356 | free_pages((unsigned long)vstart, order); | |
357 | ||
358 | return rc; | |
359 | } | |
360 | ||
361 | static void swiotlb_cleanup(void) | |
362 | { | |
363 | io_tlb_end = 0; | |
364 | io_tlb_start = 0; | |
365 | io_tlb_nslabs = 0; | |
366 | max_segment = 0; | |
367 | } | |
368 | ||
369 | int | |
370 | swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) | |
371 | { | |
372 | unsigned long i, bytes; | |
373 | ||
374 | /* protect against double initialization */ | |
375 | if (WARN_ON_ONCE(io_tlb_start)) | |
376 | return -ENOMEM; | |
377 | ||
378 | bytes = nslabs << IO_TLB_SHIFT; | |
379 | ||
380 | io_tlb_nslabs = nslabs; | |
381 | io_tlb_start = virt_to_phys(tlb); | |
382 | io_tlb_end = io_tlb_start + bytes; | |
383 | ||
384 | set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT); | |
385 | memset(tlb, 0, bytes); | |
386 | ||
387 | /* | |
388 | * Allocate and initialize the free list array. This array is used | |
389 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE | |
390 | * between io_tlb_start and io_tlb_end. | |
391 | */ | |
392 | io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL, | |
393 | get_order(io_tlb_nslabs * sizeof(int))); | |
394 | if (!io_tlb_list) | |
395 | goto cleanup3; | |
396 | ||
397 | io_tlb_orig_addr = (phys_addr_t *) | |
398 | __get_free_pages(GFP_KERNEL, | |
399 | get_order(io_tlb_nslabs * | |
400 | sizeof(phys_addr_t))); | |
401 | if (!io_tlb_orig_addr) | |
402 | goto cleanup4; | |
403 | ||
404 | io_tlb_alloc_size = (size_t *) | |
405 | __get_free_pages(GFP_KERNEL, | |
406 | get_order(io_tlb_nslabs * | |
407 | sizeof(size_t))); | |
408 | if (!io_tlb_alloc_size) | |
409 | goto cleanup5; | |
410 | ||
411 | ||
412 | for (i = 0; i < io_tlb_nslabs; i++) { | |
413 | io_tlb_list[i] = IO_TLB_SEGSIZE - io_tlb_offset(i); | |
414 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; | |
415 | io_tlb_alloc_size[i] = 0; | |
416 | } | |
417 | io_tlb_index = 0; | |
418 | no_iotlb_memory = false; | |
419 | ||
420 | swiotlb_print_info(); | |
421 | ||
422 | late_alloc = 1; | |
423 | ||
424 | swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT); | |
425 | ||
426 | return 0; | |
427 | ||
428 | cleanup5: | |
429 | free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs * | |
430 | sizeof(phys_addr_t))); | |
431 | ||
432 | cleanup4: | |
433 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * | |
434 | sizeof(int))); | |
435 | io_tlb_list = NULL; | |
436 | cleanup3: | |
437 | swiotlb_cleanup(); | |
438 | return -ENOMEM; | |
439 | } | |
440 | ||
441 | void __init swiotlb_exit(void) | |
442 | { | |
443 | if (!io_tlb_orig_addr) | |
444 | return; | |
445 | ||
446 | if (late_alloc) { | |
447 | free_pages((unsigned long)io_tlb_alloc_size, | |
448 | get_order(io_tlb_nslabs * sizeof(size_t))); | |
449 | free_pages((unsigned long)io_tlb_orig_addr, | |
450 | get_order(io_tlb_nslabs * sizeof(phys_addr_t))); | |
451 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * | |
452 | sizeof(int))); | |
453 | free_pages((unsigned long)phys_to_virt(io_tlb_start), | |
454 | get_order(io_tlb_nslabs << IO_TLB_SHIFT)); | |
455 | } else { | |
456 | memblock_free_late(__pa(io_tlb_orig_addr), | |
457 | PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t))); | |
458 | memblock_free_late(__pa(io_tlb_alloc_size), | |
459 | PAGE_ALIGN(io_tlb_nslabs * sizeof(size_t))); | |
460 | memblock_free_late(__pa(io_tlb_list), | |
461 | PAGE_ALIGN(io_tlb_nslabs * sizeof(int))); | |
462 | memblock_free_late(io_tlb_start, | |
463 | PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); | |
464 | } | |
465 | swiotlb_cleanup(); | |
466 | } | |
467 | ||
468 | /* | |
469 | * Bounce: copy the swiotlb buffer from or back to the original dma location | |
470 | */ | |
471 | static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size, | |
472 | enum dma_data_direction dir) | |
473 | { | |
474 | int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT; | |
475 | size_t alloc_size = io_tlb_alloc_size[index]; | |
476 | phys_addr_t orig_addr = io_tlb_orig_addr[index]; | |
477 | unsigned long pfn = PFN_DOWN(orig_addr); | |
478 | unsigned char *vaddr = phys_to_virt(tlb_addr); | |
479 | ||
480 | if (orig_addr == INVALID_PHYS_ADDR) | |
481 | return; | |
482 | ||
483 | if (size > alloc_size) { | |
484 | dev_WARN_ONCE(dev, 1, | |
485 | "Buffer overflow detected. Allocation size: %zu. Mapping size: %zu.\n", | |
486 | alloc_size, size); | |
487 | size = alloc_size; | |
488 | } | |
489 | ||
490 | if (PageHighMem(pfn_to_page(pfn))) { | |
491 | /* The buffer does not have a mapping. Map it in and copy */ | |
492 | unsigned int offset = orig_addr & ~PAGE_MASK; | |
493 | char *buffer; | |
494 | unsigned int sz = 0; | |
495 | unsigned long flags; | |
496 | ||
497 | while (size) { | |
498 | sz = min_t(size_t, PAGE_SIZE - offset, size); | |
499 | ||
500 | local_irq_save(flags); | |
501 | buffer = kmap_atomic(pfn_to_page(pfn)); | |
502 | if (dir == DMA_TO_DEVICE) | |
503 | memcpy(vaddr, buffer + offset, sz); | |
504 | else | |
505 | memcpy(buffer + offset, vaddr, sz); | |
506 | kunmap_atomic(buffer); | |
507 | local_irq_restore(flags); | |
508 | ||
509 | size -= sz; | |
510 | pfn++; | |
511 | vaddr += sz; | |
512 | offset = 0; | |
513 | } | |
514 | } else if (dir == DMA_TO_DEVICE) { | |
515 | memcpy(vaddr, phys_to_virt(orig_addr), size); | |
516 | } else { | |
517 | memcpy(phys_to_virt(orig_addr), vaddr, size); | |
518 | } | |
519 | } | |
520 | ||
521 | #define slot_addr(start, idx) ((start) + ((idx) << IO_TLB_SHIFT)) | |
522 | ||
523 | /* | |
524 | * Return the offset into a iotlb slot required to keep the device happy. | |
525 | */ | |
526 | static unsigned int swiotlb_align_offset(struct device *dev, u64 addr) | |
527 | { | |
528 | return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1); | |
529 | } | |
530 | ||
531 | /* | |
532 | * Carefully handle integer overflow which can occur when boundary_mask == ~0UL. | |
533 | */ | |
534 | static inline unsigned long get_max_slots(unsigned long boundary_mask) | |
535 | { | |
536 | if (boundary_mask == ~0UL) | |
537 | return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT); | |
538 | return nr_slots(boundary_mask + 1); | |
539 | } | |
540 | ||
541 | static unsigned int wrap_index(unsigned int index) | |
542 | { | |
543 | if (index >= io_tlb_nslabs) | |
544 | return 0; | |
545 | return index; | |
546 | } | |
547 | ||
548 | /* | |
549 | * Find a suitable number of IO TLB entries size that will fit this request and | |
550 | * allocate a buffer from that IO TLB pool. | |
551 | */ | |
552 | static int find_slots(struct device *dev, phys_addr_t orig_addr, | |
553 | size_t alloc_size) | |
554 | { | |
555 | unsigned long boundary_mask = dma_get_seg_boundary(dev); | |
556 | dma_addr_t tbl_dma_addr = | |
557 | phys_to_dma_unencrypted(dev, io_tlb_start) & boundary_mask; | |
558 | unsigned long max_slots = get_max_slots(boundary_mask); | |
559 | unsigned int iotlb_align_mask = | |
560 | dma_get_min_align_mask(dev) & ~(IO_TLB_SIZE - 1); | |
561 | unsigned int nslots = nr_slots(alloc_size), stride; | |
562 | unsigned int index, wrap, count = 0, i; | |
563 | unsigned long flags; | |
564 | ||
565 | BUG_ON(!nslots); | |
566 | ||
567 | /* | |
568 | * For mappings with an alignment requirement don't bother looping to | |
569 | * unaligned slots once we found an aligned one. For allocations of | |
570 | * PAGE_SIZE or larger only look for page aligned allocations. | |
571 | */ | |
572 | stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1; | |
573 | if (alloc_size >= PAGE_SIZE) | |
574 | stride = max(stride, stride << (PAGE_SHIFT - IO_TLB_SHIFT)); | |
575 | ||
576 | spin_lock_irqsave(&io_tlb_lock, flags); | |
577 | if (unlikely(nslots > io_tlb_nslabs - io_tlb_used)) | |
578 | goto not_found; | |
579 | ||
580 | index = wrap = wrap_index(ALIGN(io_tlb_index, stride)); | |
581 | do { | |
582 | if ((slot_addr(tbl_dma_addr, index) & iotlb_align_mask) != | |
583 | (orig_addr & iotlb_align_mask)) { | |
584 | index = wrap_index(index + 1); | |
585 | continue; | |
586 | } | |
587 | ||
588 | /* | |
589 | * If we find a slot that indicates we have 'nslots' number of | |
590 | * contiguous buffers, we allocate the buffers from that slot | |
591 | * and mark the entries as '0' indicating unavailable. | |
592 | */ | |
593 | if (!iommu_is_span_boundary(index, nslots, | |
594 | nr_slots(tbl_dma_addr), | |
595 | max_slots)) { | |
596 | if (io_tlb_list[index] >= nslots) | |
597 | goto found; | |
598 | } | |
599 | index = wrap_index(index + stride); | |
600 | } while (index != wrap); | |
601 | ||
602 | not_found: | |
603 | spin_unlock_irqrestore(&io_tlb_lock, flags); | |
604 | return -1; | |
605 | ||
606 | found: | |
607 | for (i = index; i < index + nslots; i++) | |
608 | io_tlb_list[i] = 0; | |
609 | for (i = index - 1; | |
610 | io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && | |
611 | io_tlb_list[i]; i--) | |
612 | io_tlb_list[i] = ++count; | |
613 | ||
614 | /* | |
615 | * Update the indices to avoid searching in the next round. | |
616 | */ | |
617 | if (index + nslots < io_tlb_nslabs) | |
618 | io_tlb_index = index + nslots; | |
619 | else | |
620 | io_tlb_index = 0; | |
621 | io_tlb_used += nslots; | |
622 | ||
623 | spin_unlock_irqrestore(&io_tlb_lock, flags); | |
624 | return index; | |
625 | } | |
626 | ||
627 | phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, | |
628 | size_t mapping_size, size_t alloc_size, | |
629 | enum dma_data_direction dir, unsigned long attrs) | |
630 | { | |
631 | unsigned int offset = swiotlb_align_offset(dev, orig_addr); | |
632 | unsigned int index, i; | |
633 | phys_addr_t tlb_addr; | |
634 | ||
635 | if (no_iotlb_memory) | |
636 | panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer"); | |
637 | ||
638 | if (mem_encrypt_active()) | |
639 | pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n"); | |
640 | ||
641 | if (mapping_size > alloc_size) { | |
642 | dev_warn_once(dev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)", | |
643 | mapping_size, alloc_size); | |
644 | return (phys_addr_t)DMA_MAPPING_ERROR; | |
645 | } | |
646 | ||
647 | index = find_slots(dev, orig_addr, alloc_size + offset); | |
648 | if (index == -1) { | |
649 | if (!(attrs & DMA_ATTR_NO_WARN)) | |
650 | dev_warn_ratelimited(dev, | |
651 | "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n", | |
652 | alloc_size, io_tlb_nslabs, io_tlb_used); | |
653 | return (phys_addr_t)DMA_MAPPING_ERROR; | |
654 | } | |
655 | ||
656 | /* | |
657 | * Save away the mapping from the original address to the DMA address. | |
658 | * This is needed when we sync the memory. Then we sync the buffer if | |
659 | * needed. | |
660 | */ | |
661 | for (i = 0; i < nr_slots(alloc_size + offset); i++) { | |
662 | io_tlb_orig_addr[index + i] = slot_addr(orig_addr, i); | |
663 | io_tlb_alloc_size[index+i] = alloc_size - (i << IO_TLB_SHIFT); | |
664 | } | |
665 | tlb_addr = slot_addr(io_tlb_start, index) + offset; | |
666 | if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && | |
667 | (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) | |
668 | swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE); | |
669 | return tlb_addr; | |
670 | } | |
671 | ||
672 | /* | |
673 | * tlb_addr is the physical address of the bounce buffer to unmap. | |
674 | */ | |
675 | void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr, | |
676 | size_t mapping_size, enum dma_data_direction dir, | |
677 | unsigned long attrs) | |
678 | { | |
679 | unsigned long flags; | |
680 | unsigned int offset = swiotlb_align_offset(hwdev, tlb_addr); | |
681 | int index = (tlb_addr - offset - io_tlb_start) >> IO_TLB_SHIFT; | |
682 | int nslots = nr_slots(io_tlb_alloc_size[index] + offset); | |
683 | int count, i; | |
684 | ||
685 | /* | |
686 | * First, sync the memory before unmapping the entry | |
687 | */ | |
688 | if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && | |
689 | (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)) | |
690 | swiotlb_bounce(hwdev, tlb_addr, mapping_size, DMA_FROM_DEVICE); | |
691 | ||
692 | /* | |
693 | * Return the buffer to the free list by setting the corresponding | |
694 | * entries to indicate the number of contiguous entries available. | |
695 | * While returning the entries to the free list, we merge the entries | |
696 | * with slots below and above the pool being returned. | |
697 | */ | |
698 | spin_lock_irqsave(&io_tlb_lock, flags); | |
699 | if (index + nslots < ALIGN(index + 1, IO_TLB_SEGSIZE)) | |
700 | count = io_tlb_list[index + nslots]; | |
701 | else | |
702 | count = 0; | |
703 | ||
704 | /* | |
705 | * Step 1: return the slots to the free list, merging the slots with | |
706 | * superceeding slots | |
707 | */ | |
708 | for (i = index + nslots - 1; i >= index; i--) { | |
709 | io_tlb_list[i] = ++count; | |
710 | io_tlb_orig_addr[i] = INVALID_PHYS_ADDR; | |
711 | io_tlb_alloc_size[i] = 0; | |
712 | } | |
713 | ||
714 | /* | |
715 | * Step 2: merge the returned slots with the preceding slots, if | |
716 | * available (non zero) | |
717 | */ | |
718 | for (i = index - 1; | |
719 | io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && io_tlb_list[i]; | |
720 | i--) | |
721 | io_tlb_list[i] = ++count; | |
722 | io_tlb_used -= nslots; | |
723 | spin_unlock_irqrestore(&io_tlb_lock, flags); | |
724 | } | |
725 | ||
726 | void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr, | |
727 | size_t size, enum dma_data_direction dir) | |
728 | { | |
729 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) | |
730 | swiotlb_bounce(dev, tlb_addr, size, DMA_TO_DEVICE); | |
731 | else | |
732 | BUG_ON(dir != DMA_FROM_DEVICE); | |
733 | } | |
734 | ||
735 | void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr, | |
736 | size_t size, enum dma_data_direction dir) | |
737 | { | |
738 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) | |
739 | swiotlb_bounce(dev, tlb_addr, size, DMA_FROM_DEVICE); | |
740 | else | |
741 | BUG_ON(dir != DMA_TO_DEVICE); | |
742 | } | |
743 | ||
744 | /* | |
745 | * Create a swiotlb mapping for the buffer at @paddr, and in case of DMAing | |
746 | * to the device copy the data into it as well. | |
747 | */ | |
748 | dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size, | |
749 | enum dma_data_direction dir, unsigned long attrs) | |
750 | { | |
751 | phys_addr_t swiotlb_addr; | |
752 | dma_addr_t dma_addr; | |
753 | ||
754 | trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size, | |
755 | swiotlb_force); | |
756 | ||
757 | swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, dir, | |
758 | attrs); | |
759 | if (swiotlb_addr == (phys_addr_t)DMA_MAPPING_ERROR) | |
760 | return DMA_MAPPING_ERROR; | |
761 | ||
762 | /* Ensure that the address returned is DMA'ble */ | |
763 | dma_addr = phys_to_dma_unencrypted(dev, swiotlb_addr); | |
764 | if (unlikely(!dma_capable(dev, dma_addr, size, true))) { | |
765 | swiotlb_tbl_unmap_single(dev, swiotlb_addr, size, dir, | |
766 | attrs | DMA_ATTR_SKIP_CPU_SYNC); | |
767 | dev_WARN_ONCE(dev, 1, | |
768 | "swiotlb addr %pad+%zu overflow (mask %llx, bus limit %llx).\n", | |
769 | &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit); | |
770 | return DMA_MAPPING_ERROR; | |
771 | } | |
772 | ||
773 | if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) | |
774 | arch_sync_dma_for_device(swiotlb_addr, size, dir); | |
775 | return dma_addr; | |
776 | } | |
777 | ||
778 | size_t swiotlb_max_mapping_size(struct device *dev) | |
779 | { | |
780 | return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE; | |
781 | } | |
782 | ||
783 | bool is_swiotlb_active(void) | |
784 | { | |
785 | /* | |
786 | * When SWIOTLB is initialized, even if io_tlb_start points to physical | |
787 | * address zero, io_tlb_end surely doesn't. | |
788 | */ | |
789 | return io_tlb_end != 0; | |
790 | } | |
791 | ||
792 | #ifdef CONFIG_DEBUG_FS | |
793 | ||
794 | static int __init swiotlb_create_debugfs(void) | |
795 | { | |
796 | struct dentry *root; | |
797 | ||
798 | root = debugfs_create_dir("swiotlb", NULL); | |
799 | debugfs_create_ulong("io_tlb_nslabs", 0400, root, &io_tlb_nslabs); | |
800 | debugfs_create_ulong("io_tlb_used", 0400, root, &io_tlb_used); | |
801 | return 0; | |
802 | } | |
803 | ||
804 | late_initcall(swiotlb_create_debugfs); | |
805 | ||
806 | #endif |