xref: /linux/kernel/dma/swiotlb.c (revision 7fa8a8ee9400fe8ec188426e40e481717bc5e924)
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/cc_platform.h>
25 #include <linux/ctype.h>
26 #include <linux/debugfs.h>
27 #include <linux/dma-direct.h>
28 #include <linux/dma-map-ops.h>
29 #include <linux/export.h>
30 #include <linux/gfp.h>
31 #include <linux/highmem.h>
32 #include <linux/io.h>
33 #include <linux/iommu-helper.h>
34 #include <linux/init.h>
35 #include <linux/memblock.h>
36 #include <linux/mm.h>
37 #include <linux/pfn.h>
38 #include <linux/scatterlist.h>
39 #include <linux/set_memory.h>
40 #include <linux/spinlock.h>
41 #include <linux/string.h>
42 #include <linux/swiotlb.h>
43 #include <linux/types.h>
44 #ifdef CONFIG_DMA_RESTRICTED_POOL
45 #include <linux/of.h>
46 #include <linux/of_fdt.h>
47 #include <linux/of_reserved_mem.h>
48 #include <linux/slab.h>
49 #endif
50 
51 #define CREATE_TRACE_POINTS
52 #include <trace/events/swiotlb.h>
53 
54 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
55 
56 /*
57  * Minimum IO TLB size to bother booting with.  Systems with mainly
58  * 64bit capable cards will only lightly use the swiotlb.  If we can't
59  * allocate a contiguous 1MB, we're probably in trouble anyway.
60  */
61 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
62 
63 #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
64 
65 struct io_tlb_slot {
66 	phys_addr_t orig_addr;
67 	size_t alloc_size;
68 	unsigned int list;
69 };
70 
71 static bool swiotlb_force_bounce;
72 static bool swiotlb_force_disable;
73 
74 struct io_tlb_mem io_tlb_default_mem;
75 
76 static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT;
77 static unsigned long default_nareas;
78 
79 /**
80  * struct io_tlb_area - IO TLB memory area descriptor
81  *
82  * This is a single area with a single lock.
83  *
84  * @used:	The number of used IO TLB block.
85  * @index:	The slot index to start searching in this area for next round.
86  * @lock:	The lock to protect the above data structures in the map and
87  *		unmap calls.
88  */
89 struct io_tlb_area {
90 	unsigned long used;
91 	unsigned int index;
92 	spinlock_t lock;
93 };
94 
95 /*
96  * Round up number of slabs to the next power of 2. The last area is going
97  * be smaller than the rest if default_nslabs is not power of two.
98  * The number of slot in an area should be a multiple of IO_TLB_SEGSIZE,
99  * otherwise a segment may span two or more areas. It conflicts with free
100  * contiguous slots tracking: free slots are treated contiguous no matter
101  * whether they cross an area boundary.
102  *
103  * Return true if default_nslabs is rounded up.
104  */
105 static bool round_up_default_nslabs(void)
106 {
107 	if (!default_nareas)
108 		return false;
109 
110 	if (default_nslabs < IO_TLB_SEGSIZE * default_nareas)
111 		default_nslabs = IO_TLB_SEGSIZE * default_nareas;
112 	else if (is_power_of_2(default_nslabs))
113 		return false;
114 	default_nslabs = roundup_pow_of_two(default_nslabs);
115 	return true;
116 }
117 
118 static void swiotlb_adjust_nareas(unsigned int nareas)
119 {
120 	/* use a single area when non is specified */
121 	if (!nareas)
122 		nareas = 1;
123 	else if (!is_power_of_2(nareas))
124 		nareas = roundup_pow_of_two(nareas);
125 
126 	default_nareas = nareas;
127 
128 	pr_info("area num %d.\n", nareas);
129 	if (round_up_default_nslabs())
130 		pr_info("SWIOTLB bounce buffer size roundup to %luMB",
131 			(default_nslabs << IO_TLB_SHIFT) >> 20);
132 }
133 
134 static int __init
135 setup_io_tlb_npages(char *str)
136 {
137 	if (isdigit(*str)) {
138 		/* avoid tail segment of size < IO_TLB_SEGSIZE */
139 		default_nslabs =
140 			ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE);
141 	}
142 	if (*str == ',')
143 		++str;
144 	if (isdigit(*str))
145 		swiotlb_adjust_nareas(simple_strtoul(str, &str, 0));
146 	if (*str == ',')
147 		++str;
148 	if (!strcmp(str, "force"))
149 		swiotlb_force_bounce = true;
150 	else if (!strcmp(str, "noforce"))
151 		swiotlb_force_disable = true;
152 
153 	return 0;
154 }
155 early_param("swiotlb", setup_io_tlb_npages);
156 
157 unsigned long swiotlb_size_or_default(void)
158 {
159 	return default_nslabs << IO_TLB_SHIFT;
160 }
161 
162 void __init swiotlb_adjust_size(unsigned long size)
163 {
164 	/*
165 	 * If swiotlb parameter has not been specified, give a chance to
166 	 * architectures such as those supporting memory encryption to
167 	 * adjust/expand SWIOTLB size for their use.
168 	 */
169 	if (default_nslabs != IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT)
170 		return;
171 
172 	size = ALIGN(size, IO_TLB_SIZE);
173 	default_nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
174 	if (round_up_default_nslabs())
175 		size = default_nslabs << IO_TLB_SHIFT;
176 	pr_info("SWIOTLB bounce buffer size adjusted to %luMB", size >> 20);
177 }
178 
179 void swiotlb_print_info(void)
180 {
181 	struct io_tlb_mem *mem = &io_tlb_default_mem;
182 
183 	if (!mem->nslabs) {
184 		pr_warn("No low mem\n");
185 		return;
186 	}
187 
188 	pr_info("mapped [mem %pa-%pa] (%luMB)\n", &mem->start, &mem->end,
189 	       (mem->nslabs << IO_TLB_SHIFT) >> 20);
190 }
191 
192 static inline unsigned long io_tlb_offset(unsigned long val)
193 {
194 	return val & (IO_TLB_SEGSIZE - 1);
195 }
196 
197 static inline unsigned long nr_slots(u64 val)
198 {
199 	return DIV_ROUND_UP(val, IO_TLB_SIZE);
200 }
201 
202 /*
203  * Early SWIOTLB allocation may be too early to allow an architecture to
204  * perform the desired operations.  This function allows the architecture to
205  * call SWIOTLB when the operations are possible.  It needs to be called
206  * before the SWIOTLB memory is used.
207  */
208 void __init swiotlb_update_mem_attributes(void)
209 {
210 	struct io_tlb_mem *mem = &io_tlb_default_mem;
211 	unsigned long bytes;
212 
213 	if (!mem->nslabs || mem->late_alloc)
214 		return;
215 	bytes = PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT);
216 	set_memory_decrypted((unsigned long)mem->vaddr, bytes >> PAGE_SHIFT);
217 }
218 
219 static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
220 		unsigned long nslabs, unsigned int flags,
221 		bool late_alloc, unsigned int nareas)
222 {
223 	void *vaddr = phys_to_virt(start);
224 	unsigned long bytes = nslabs << IO_TLB_SHIFT, i;
225 
226 	mem->nslabs = nslabs;
227 	mem->start = start;
228 	mem->end = mem->start + bytes;
229 	mem->late_alloc = late_alloc;
230 	mem->nareas = nareas;
231 	mem->area_nslabs = nslabs / mem->nareas;
232 
233 	mem->force_bounce = swiotlb_force_bounce || (flags & SWIOTLB_FORCE);
234 
235 	for (i = 0; i < mem->nareas; i++) {
236 		spin_lock_init(&mem->areas[i].lock);
237 		mem->areas[i].index = 0;
238 		mem->areas[i].used = 0;
239 	}
240 
241 	for (i = 0; i < mem->nslabs; i++) {
242 		mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i);
243 		mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
244 		mem->slots[i].alloc_size = 0;
245 	}
246 
247 	memset(vaddr, 0, bytes);
248 	mem->vaddr = vaddr;
249 	return;
250 }
251 
252 static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
253 		unsigned int flags,
254 		int (*remap)(void *tlb, unsigned long nslabs))
255 {
256 	size_t bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT);
257 	void *tlb;
258 
259 	/*
260 	 * By default allocate the bounce buffer memory from low memory, but
261 	 * allow to pick a location everywhere for hypervisors with guest
262 	 * memory encryption.
263 	 */
264 	if (flags & SWIOTLB_ANY)
265 		tlb = memblock_alloc(bytes, PAGE_SIZE);
266 	else
267 		tlb = memblock_alloc_low(bytes, PAGE_SIZE);
268 
269 	if (!tlb) {
270 		pr_warn("%s: Failed to allocate %zu bytes tlb structure\n",
271 			__func__, bytes);
272 		return NULL;
273 	}
274 
275 	if (remap && remap(tlb, nslabs) < 0) {
276 		memblock_free(tlb, PAGE_ALIGN(bytes));
277 		pr_warn("%s: Failed to remap %zu bytes\n", __func__, bytes);
278 		return NULL;
279 	}
280 
281 	return tlb;
282 }
283 
284 /*
285  * Statically reserve bounce buffer space and initialize bounce buffer data
286  * structures for the software IO TLB used to implement the DMA API.
287  */
288 void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,
289 		int (*remap)(void *tlb, unsigned long nslabs))
290 {
291 	struct io_tlb_mem *mem = &io_tlb_default_mem;
292 	unsigned long nslabs;
293 	size_t alloc_size;
294 	void *tlb;
295 
296 	if (!addressing_limit && !swiotlb_force_bounce)
297 		return;
298 	if (swiotlb_force_disable)
299 		return;
300 
301 	/*
302 	 * default_nslabs maybe changed when adjust area number.
303 	 * So allocate bounce buffer after adjusting area number.
304 	 */
305 	if (!default_nareas)
306 		swiotlb_adjust_nareas(num_possible_cpus());
307 
308 	nslabs = default_nslabs;
309 	while ((tlb = swiotlb_memblock_alloc(nslabs, flags, remap)) == NULL) {
310 		if (nslabs <= IO_TLB_MIN_SLABS)
311 			return;
312 		nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
313 	}
314 
315 	if (default_nslabs != nslabs) {
316 		pr_info("SWIOTLB bounce buffer size adjusted %lu -> %lu slabs",
317 			default_nslabs, nslabs);
318 		default_nslabs = nslabs;
319 	}
320 
321 	alloc_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), nslabs));
322 	mem->slots = memblock_alloc(alloc_size, PAGE_SIZE);
323 	if (!mem->slots) {
324 		pr_warn("%s: Failed to allocate %zu bytes align=0x%lx\n",
325 			__func__, alloc_size, PAGE_SIZE);
326 		return;
327 	}
328 
329 	mem->areas = memblock_alloc(array_size(sizeof(struct io_tlb_area),
330 		default_nareas), SMP_CACHE_BYTES);
331 	if (!mem->areas) {
332 		pr_warn("%s: Failed to allocate mem->areas.\n", __func__);
333 		return;
334 	}
335 
336 	swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, flags, false,
337 				default_nareas);
338 
339 	if (flags & SWIOTLB_VERBOSE)
340 		swiotlb_print_info();
341 }
342 
343 void __init swiotlb_init(bool addressing_limit, unsigned int flags)
344 {
345 	swiotlb_init_remap(addressing_limit, flags, NULL);
346 }
347 
348 /*
349  * Systems with larger DMA zones (those that don't support ISA) can
350  * initialize the swiotlb later using the slab allocator if needed.
351  * This should be just like above, but with some error catching.
352  */
353 int swiotlb_init_late(size_t size, gfp_t gfp_mask,
354 		int (*remap)(void *tlb, unsigned long nslabs))
355 {
356 	struct io_tlb_mem *mem = &io_tlb_default_mem;
357 	unsigned long nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE);
358 	unsigned char *vstart = NULL;
359 	unsigned int order, area_order;
360 	bool retried = false;
361 	int rc = 0;
362 
363 	if (swiotlb_force_disable)
364 		return 0;
365 
366 retry:
367 	order = get_order(nslabs << IO_TLB_SHIFT);
368 	nslabs = SLABS_PER_PAGE << order;
369 
370 	while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
371 		vstart = (void *)__get_free_pages(gfp_mask | __GFP_NOWARN,
372 						  order);
373 		if (vstart)
374 			break;
375 		order--;
376 		nslabs = SLABS_PER_PAGE << order;
377 		retried = true;
378 	}
379 
380 	if (!vstart)
381 		return -ENOMEM;
382 
383 	if (remap)
384 		rc = remap(vstart, nslabs);
385 	if (rc) {
386 		free_pages((unsigned long)vstart, order);
387 
388 		nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE);
389 		if (nslabs < IO_TLB_MIN_SLABS)
390 			return rc;
391 		retried = true;
392 		goto retry;
393 	}
394 
395 	if (retried) {
396 		pr_warn("only able to allocate %ld MB\n",
397 			(PAGE_SIZE << order) >> 20);
398 	}
399 
400 	if (!default_nareas)
401 		swiotlb_adjust_nareas(num_possible_cpus());
402 
403 	area_order = get_order(array_size(sizeof(*mem->areas),
404 		default_nareas));
405 	mem->areas = (struct io_tlb_area *)
406 		__get_free_pages(GFP_KERNEL | __GFP_ZERO, area_order);
407 	if (!mem->areas)
408 		goto error_area;
409 
410 	mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
411 		get_order(array_size(sizeof(*mem->slots), nslabs)));
412 	if (!mem->slots)
413 		goto error_slots;
414 
415 	set_memory_decrypted((unsigned long)vstart,
416 			     (nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT);
417 	swiotlb_init_io_tlb_mem(mem, virt_to_phys(vstart), nslabs, 0, true,
418 				default_nareas);
419 
420 	swiotlb_print_info();
421 	return 0;
422 
423 error_slots:
424 	free_pages((unsigned long)mem->areas, area_order);
425 error_area:
426 	free_pages((unsigned long)vstart, order);
427 	return -ENOMEM;
428 }
429 
430 void __init swiotlb_exit(void)
431 {
432 	struct io_tlb_mem *mem = &io_tlb_default_mem;
433 	unsigned long tbl_vaddr;
434 	size_t tbl_size, slots_size;
435 	unsigned int area_order;
436 
437 	if (swiotlb_force_bounce)
438 		return;
439 
440 	if (!mem->nslabs)
441 		return;
442 
443 	pr_info("tearing down default memory pool\n");
444 	tbl_vaddr = (unsigned long)phys_to_virt(mem->start);
445 	tbl_size = PAGE_ALIGN(mem->end - mem->start);
446 	slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs));
447 
448 	set_memory_encrypted(tbl_vaddr, tbl_size >> PAGE_SHIFT);
449 	if (mem->late_alloc) {
450 		area_order = get_order(array_size(sizeof(*mem->areas),
451 			mem->nareas));
452 		free_pages((unsigned long)mem->areas, area_order);
453 		free_pages(tbl_vaddr, get_order(tbl_size));
454 		free_pages((unsigned long)mem->slots, get_order(slots_size));
455 	} else {
456 		memblock_free_late(__pa(mem->areas),
457 			array_size(sizeof(*mem->areas), mem->nareas));
458 		memblock_free_late(mem->start, tbl_size);
459 		memblock_free_late(__pa(mem->slots), slots_size);
460 	}
461 
462 	memset(mem, 0, sizeof(*mem));
463 }
464 
465 /*
466  * Return the offset into a iotlb slot required to keep the device happy.
467  */
468 static unsigned int swiotlb_align_offset(struct device *dev, u64 addr)
469 {
470 	return addr & dma_get_min_align_mask(dev) & (IO_TLB_SIZE - 1);
471 }
472 
473 /*
474  * Bounce: copy the swiotlb buffer from or back to the original dma location
475  */
476 static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size,
477 			   enum dma_data_direction dir)
478 {
479 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
480 	int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT;
481 	phys_addr_t orig_addr = mem->slots[index].orig_addr;
482 	size_t alloc_size = mem->slots[index].alloc_size;
483 	unsigned long pfn = PFN_DOWN(orig_addr);
484 	unsigned char *vaddr = mem->vaddr + tlb_addr - mem->start;
485 	unsigned int tlb_offset, orig_addr_offset;
486 
487 	if (orig_addr == INVALID_PHYS_ADDR)
488 		return;
489 
490 	tlb_offset = tlb_addr & (IO_TLB_SIZE - 1);
491 	orig_addr_offset = swiotlb_align_offset(dev, orig_addr);
492 	if (tlb_offset < orig_addr_offset) {
493 		dev_WARN_ONCE(dev, 1,
494 			"Access before mapping start detected. orig offset %u, requested offset %u.\n",
495 			orig_addr_offset, tlb_offset);
496 		return;
497 	}
498 
499 	tlb_offset -= orig_addr_offset;
500 	if (tlb_offset > alloc_size) {
501 		dev_WARN_ONCE(dev, 1,
502 			"Buffer overflow detected. Allocation size: %zu. Mapping size: %zu+%u.\n",
503 			alloc_size, size, tlb_offset);
504 		return;
505 	}
506 
507 	orig_addr += tlb_offset;
508 	alloc_size -= tlb_offset;
509 
510 	if (size > alloc_size) {
511 		dev_WARN_ONCE(dev, 1,
512 			"Buffer overflow detected. Allocation size: %zu. Mapping size: %zu.\n",
513 			alloc_size, size);
514 		size = alloc_size;
515 	}
516 
517 	if (PageHighMem(pfn_to_page(pfn))) {
518 		unsigned int offset = orig_addr & ~PAGE_MASK;
519 		struct page *page;
520 		unsigned int sz = 0;
521 		unsigned long flags;
522 
523 		while (size) {
524 			sz = min_t(size_t, PAGE_SIZE - offset, size);
525 
526 			local_irq_save(flags);
527 			page = pfn_to_page(pfn);
528 			if (dir == DMA_TO_DEVICE)
529 				memcpy_from_page(vaddr, page, offset, sz);
530 			else
531 				memcpy_to_page(page, offset, vaddr, sz);
532 			local_irq_restore(flags);
533 
534 			size -= sz;
535 			pfn++;
536 			vaddr += sz;
537 			offset = 0;
538 		}
539 	} else if (dir == DMA_TO_DEVICE) {
540 		memcpy(vaddr, phys_to_virt(orig_addr), size);
541 	} else {
542 		memcpy(phys_to_virt(orig_addr), vaddr, size);
543 	}
544 }
545 
546 static inline phys_addr_t slot_addr(phys_addr_t start, phys_addr_t idx)
547 {
548 	return start + (idx << IO_TLB_SHIFT);
549 }
550 
551 /*
552  * Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
553  */
554 static inline unsigned long get_max_slots(unsigned long boundary_mask)
555 {
556 	if (boundary_mask == ~0UL)
557 		return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
558 	return nr_slots(boundary_mask + 1);
559 }
560 
561 static unsigned int wrap_area_index(struct io_tlb_mem *mem, unsigned int index)
562 {
563 	if (index >= mem->area_nslabs)
564 		return 0;
565 	return index;
566 }
567 
568 /*
569  * Find a suitable number of IO TLB entries size that will fit this request and
570  * allocate a buffer from that IO TLB pool.
571  */
572 static int swiotlb_do_find_slots(struct device *dev, int area_index,
573 		phys_addr_t orig_addr, size_t alloc_size,
574 		unsigned int alloc_align_mask)
575 {
576 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
577 	struct io_tlb_area *area = mem->areas + area_index;
578 	unsigned long boundary_mask = dma_get_seg_boundary(dev);
579 	dma_addr_t tbl_dma_addr =
580 		phys_to_dma_unencrypted(dev, mem->start) & boundary_mask;
581 	unsigned long max_slots = get_max_slots(boundary_mask);
582 	unsigned int iotlb_align_mask =
583 		dma_get_min_align_mask(dev) | alloc_align_mask;
584 	unsigned int nslots = nr_slots(alloc_size), stride;
585 	unsigned int offset = swiotlb_align_offset(dev, orig_addr);
586 	unsigned int index, slots_checked, count = 0, i;
587 	unsigned long flags;
588 	unsigned int slot_base;
589 	unsigned int slot_index;
590 
591 	BUG_ON(!nslots);
592 	BUG_ON(area_index >= mem->nareas);
593 
594 	/*
595 	 * For allocations of PAGE_SIZE or larger only look for page aligned
596 	 * allocations.
597 	 */
598 	if (alloc_size >= PAGE_SIZE)
599 		iotlb_align_mask |= ~PAGE_MASK;
600 	iotlb_align_mask &= ~(IO_TLB_SIZE - 1);
601 
602 	/*
603 	 * For mappings with an alignment requirement don't bother looping to
604 	 * unaligned slots once we found an aligned one.
605 	 */
606 	stride = (iotlb_align_mask >> IO_TLB_SHIFT) + 1;
607 
608 	spin_lock_irqsave(&area->lock, flags);
609 	if (unlikely(nslots > mem->area_nslabs - area->used))
610 		goto not_found;
611 
612 	slot_base = area_index * mem->area_nslabs;
613 	index = area->index;
614 
615 	for (slots_checked = 0; slots_checked < mem->area_nslabs; ) {
616 		slot_index = slot_base + index;
617 
618 		if (orig_addr &&
619 		    (slot_addr(tbl_dma_addr, slot_index) &
620 		     iotlb_align_mask) != (orig_addr & iotlb_align_mask)) {
621 			index = wrap_area_index(mem, index + 1);
622 			slots_checked++;
623 			continue;
624 		}
625 
626 		/*
627 		 * If we find a slot that indicates we have 'nslots' number of
628 		 * contiguous buffers, we allocate the buffers from that slot
629 		 * and mark the entries as '0' indicating unavailable.
630 		 */
631 		if (!iommu_is_span_boundary(slot_index, nslots,
632 					    nr_slots(tbl_dma_addr),
633 					    max_slots)) {
634 			if (mem->slots[slot_index].list >= nslots)
635 				goto found;
636 		}
637 		index = wrap_area_index(mem, index + stride);
638 		slots_checked += stride;
639 	}
640 
641 not_found:
642 	spin_unlock_irqrestore(&area->lock, flags);
643 	return -1;
644 
645 found:
646 	for (i = slot_index; i < slot_index + nslots; i++) {
647 		mem->slots[i].list = 0;
648 		mem->slots[i].alloc_size = alloc_size - (offset +
649 				((i - slot_index) << IO_TLB_SHIFT));
650 	}
651 	for (i = slot_index - 1;
652 	     io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 &&
653 	     mem->slots[i].list; i--)
654 		mem->slots[i].list = ++count;
655 
656 	/*
657 	 * Update the indices to avoid searching in the next round.
658 	 */
659 	area->index = wrap_area_index(mem, index + nslots);
660 	area->used += nslots;
661 	spin_unlock_irqrestore(&area->lock, flags);
662 	return slot_index;
663 }
664 
665 static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
666 		size_t alloc_size, unsigned int alloc_align_mask)
667 {
668 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
669 	int start = raw_smp_processor_id() & (mem->nareas - 1);
670 	int i = start, index;
671 
672 	do {
673 		index = swiotlb_do_find_slots(dev, i, orig_addr, alloc_size,
674 					      alloc_align_mask);
675 		if (index >= 0)
676 			return index;
677 		if (++i >= mem->nareas)
678 			i = 0;
679 	} while (i != start);
680 
681 	return -1;
682 }
683 
684 static unsigned long mem_used(struct io_tlb_mem *mem)
685 {
686 	int i;
687 	unsigned long used = 0;
688 
689 	for (i = 0; i < mem->nareas; i++)
690 		used += mem->areas[i].used;
691 	return used;
692 }
693 
694 phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
695 		size_t mapping_size, size_t alloc_size,
696 		unsigned int alloc_align_mask, enum dma_data_direction dir,
697 		unsigned long attrs)
698 {
699 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
700 	unsigned int offset = swiotlb_align_offset(dev, orig_addr);
701 	unsigned int i;
702 	int index;
703 	phys_addr_t tlb_addr;
704 
705 	if (!mem || !mem->nslabs) {
706 		dev_warn_ratelimited(dev,
707 			"Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
708 		return (phys_addr_t)DMA_MAPPING_ERROR;
709 	}
710 
711 	if (cc_platform_has(CC_ATTR_MEM_ENCRYPT))
712 		pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
713 
714 	if (mapping_size > alloc_size) {
715 		dev_warn_once(dev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
716 			      mapping_size, alloc_size);
717 		return (phys_addr_t)DMA_MAPPING_ERROR;
718 	}
719 
720 	index = swiotlb_find_slots(dev, orig_addr,
721 				   alloc_size + offset, alloc_align_mask);
722 	if (index == -1) {
723 		if (!(attrs & DMA_ATTR_NO_WARN))
724 			dev_warn_ratelimited(dev,
725 	"swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n",
726 				 alloc_size, mem->nslabs, mem_used(mem));
727 		return (phys_addr_t)DMA_MAPPING_ERROR;
728 	}
729 
730 	/*
731 	 * Save away the mapping from the original address to the DMA address.
732 	 * This is needed when we sync the memory.  Then we sync the buffer if
733 	 * needed.
734 	 */
735 	for (i = 0; i < nr_slots(alloc_size + offset); i++)
736 		mem->slots[index + i].orig_addr = slot_addr(orig_addr, i);
737 	tlb_addr = slot_addr(mem->start, index) + offset;
738 	/*
739 	 * When dir == DMA_FROM_DEVICE we could omit the copy from the orig
740 	 * to the tlb buffer, if we knew for sure the device will
741 	 * overwrite the entire current content. But we don't. Thus
742 	 * unconditional bounce may prevent leaking swiotlb content (i.e.
743 	 * kernel memory) to user-space.
744 	 */
745 	swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE);
746 	return tlb_addr;
747 }
748 
749 static void swiotlb_release_slots(struct device *dev, phys_addr_t tlb_addr)
750 {
751 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
752 	unsigned long flags;
753 	unsigned int offset = swiotlb_align_offset(dev, tlb_addr);
754 	int index = (tlb_addr - offset - mem->start) >> IO_TLB_SHIFT;
755 	int nslots = nr_slots(mem->slots[index].alloc_size + offset);
756 	int aindex = index / mem->area_nslabs;
757 	struct io_tlb_area *area = &mem->areas[aindex];
758 	int count, i;
759 
760 	/*
761 	 * Return the buffer to the free list by setting the corresponding
762 	 * entries to indicate the number of contiguous entries available.
763 	 * While returning the entries to the free list, we merge the entries
764 	 * with slots below and above the pool being returned.
765 	 */
766 	BUG_ON(aindex >= mem->nareas);
767 
768 	spin_lock_irqsave(&area->lock, flags);
769 	if (index + nslots < ALIGN(index + 1, IO_TLB_SEGSIZE))
770 		count = mem->slots[index + nslots].list;
771 	else
772 		count = 0;
773 
774 	/*
775 	 * Step 1: return the slots to the free list, merging the slots with
776 	 * superceeding slots
777 	 */
778 	for (i = index + nslots - 1; i >= index; i--) {
779 		mem->slots[i].list = ++count;
780 		mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
781 		mem->slots[i].alloc_size = 0;
782 	}
783 
784 	/*
785 	 * Step 2: merge the returned slots with the preceding slots, if
786 	 * available (non zero)
787 	 */
788 	for (i = index - 1;
789 	     io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 && mem->slots[i].list;
790 	     i--)
791 		mem->slots[i].list = ++count;
792 	area->used -= nslots;
793 	spin_unlock_irqrestore(&area->lock, flags);
794 }
795 
796 /*
797  * tlb_addr is the physical address of the bounce buffer to unmap.
798  */
799 void swiotlb_tbl_unmap_single(struct device *dev, phys_addr_t tlb_addr,
800 			      size_t mapping_size, enum dma_data_direction dir,
801 			      unsigned long attrs)
802 {
803 	/*
804 	 * First, sync the memory before unmapping the entry
805 	 */
806 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
807 	    (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
808 		swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_FROM_DEVICE);
809 
810 	swiotlb_release_slots(dev, tlb_addr);
811 }
812 
813 void swiotlb_sync_single_for_device(struct device *dev, phys_addr_t tlb_addr,
814 		size_t size, enum dma_data_direction dir)
815 {
816 	if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
817 		swiotlb_bounce(dev, tlb_addr, size, DMA_TO_DEVICE);
818 	else
819 		BUG_ON(dir != DMA_FROM_DEVICE);
820 }
821 
822 void swiotlb_sync_single_for_cpu(struct device *dev, phys_addr_t tlb_addr,
823 		size_t size, enum dma_data_direction dir)
824 {
825 	if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
826 		swiotlb_bounce(dev, tlb_addr, size, DMA_FROM_DEVICE);
827 	else
828 		BUG_ON(dir != DMA_TO_DEVICE);
829 }
830 
831 /*
832  * Create a swiotlb mapping for the buffer at @paddr, and in case of DMAing
833  * to the device copy the data into it as well.
834  */
835 dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,
836 		enum dma_data_direction dir, unsigned long attrs)
837 {
838 	phys_addr_t swiotlb_addr;
839 	dma_addr_t dma_addr;
840 
841 	trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size);
842 
843 	swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir,
844 			attrs);
845 	if (swiotlb_addr == (phys_addr_t)DMA_MAPPING_ERROR)
846 		return DMA_MAPPING_ERROR;
847 
848 	/* Ensure that the address returned is DMA'ble */
849 	dma_addr = phys_to_dma_unencrypted(dev, swiotlb_addr);
850 	if (unlikely(!dma_capable(dev, dma_addr, size, true))) {
851 		swiotlb_tbl_unmap_single(dev, swiotlb_addr, size, dir,
852 			attrs | DMA_ATTR_SKIP_CPU_SYNC);
853 		dev_WARN_ONCE(dev, 1,
854 			"swiotlb addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
855 			&dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
856 		return DMA_MAPPING_ERROR;
857 	}
858 
859 	if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
860 		arch_sync_dma_for_device(swiotlb_addr, size, dir);
861 	return dma_addr;
862 }
863 
864 size_t swiotlb_max_mapping_size(struct device *dev)
865 {
866 	int min_align_mask = dma_get_min_align_mask(dev);
867 	int min_align = 0;
868 
869 	/*
870 	 * swiotlb_find_slots() skips slots according to
871 	 * min align mask. This affects max mapping size.
872 	 * Take it into acount here.
873 	 */
874 	if (min_align_mask)
875 		min_align = roundup(min_align_mask, IO_TLB_SIZE);
876 
877 	return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE - min_align;
878 }
879 
880 bool is_swiotlb_active(struct device *dev)
881 {
882 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
883 
884 	return mem && mem->nslabs;
885 }
886 EXPORT_SYMBOL_GPL(is_swiotlb_active);
887 
888 static int io_tlb_used_get(void *data, u64 *val)
889 {
890 	*val = mem_used(&io_tlb_default_mem);
891 	return 0;
892 }
893 DEFINE_DEBUGFS_ATTRIBUTE(fops_io_tlb_used, io_tlb_used_get, NULL, "%llu\n");
894 
895 static void swiotlb_create_debugfs_files(struct io_tlb_mem *mem,
896 					 const char *dirname)
897 {
898 	mem->debugfs = debugfs_create_dir(dirname, io_tlb_default_mem.debugfs);
899 	if (!mem->nslabs)
900 		return;
901 
902 	debugfs_create_ulong("io_tlb_nslabs", 0400, mem->debugfs, &mem->nslabs);
903 	debugfs_create_file("io_tlb_used", 0400, mem->debugfs, NULL,
904 			&fops_io_tlb_used);
905 }
906 
907 static int __init __maybe_unused swiotlb_create_default_debugfs(void)
908 {
909 	swiotlb_create_debugfs_files(&io_tlb_default_mem, "swiotlb");
910 	return 0;
911 }
912 
913 #ifdef CONFIG_DEBUG_FS
914 late_initcall(swiotlb_create_default_debugfs);
915 #endif
916 
917 #ifdef CONFIG_DMA_RESTRICTED_POOL
918 
919 struct page *swiotlb_alloc(struct device *dev, size_t size)
920 {
921 	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
922 	phys_addr_t tlb_addr;
923 	int index;
924 
925 	if (!mem)
926 		return NULL;
927 
928 	index = swiotlb_find_slots(dev, 0, size, 0);
929 	if (index == -1)
930 		return NULL;
931 
932 	tlb_addr = slot_addr(mem->start, index);
933 
934 	return pfn_to_page(PFN_DOWN(tlb_addr));
935 }
936 
937 bool swiotlb_free(struct device *dev, struct page *page, size_t size)
938 {
939 	phys_addr_t tlb_addr = page_to_phys(page);
940 
941 	if (!is_swiotlb_buffer(dev, tlb_addr))
942 		return false;
943 
944 	swiotlb_release_slots(dev, tlb_addr);
945 
946 	return true;
947 }
948 
949 static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
950 				    struct device *dev)
951 {
952 	struct io_tlb_mem *mem = rmem->priv;
953 	unsigned long nslabs = rmem->size >> IO_TLB_SHIFT;
954 
955 	/* Set Per-device io tlb area to one */
956 	unsigned int nareas = 1;
957 
958 	/*
959 	 * Since multiple devices can share the same pool, the private data,
960 	 * io_tlb_mem struct, will be initialized by the first device attached
961 	 * to it.
962 	 */
963 	if (!mem) {
964 		mem = kzalloc(sizeof(*mem), GFP_KERNEL);
965 		if (!mem)
966 			return -ENOMEM;
967 
968 		mem->slots = kcalloc(nslabs, sizeof(*mem->slots), GFP_KERNEL);
969 		if (!mem->slots) {
970 			kfree(mem);
971 			return -ENOMEM;
972 		}
973 
974 		mem->areas = kcalloc(nareas, sizeof(*mem->areas),
975 				GFP_KERNEL);
976 		if (!mem->areas) {
977 			kfree(mem->slots);
978 			kfree(mem);
979 			return -ENOMEM;
980 		}
981 
982 		set_memory_decrypted((unsigned long)phys_to_virt(rmem->base),
983 				     rmem->size >> PAGE_SHIFT);
984 		swiotlb_init_io_tlb_mem(mem, rmem->base, nslabs, SWIOTLB_FORCE,
985 					false, nareas);
986 		mem->for_alloc = true;
987 
988 		rmem->priv = mem;
989 
990 		swiotlb_create_debugfs_files(mem, rmem->name);
991 	}
992 
993 	dev->dma_io_tlb_mem = mem;
994 
995 	return 0;
996 }
997 
998 static void rmem_swiotlb_device_release(struct reserved_mem *rmem,
999 					struct device *dev)
1000 {
1001 	dev->dma_io_tlb_mem = &io_tlb_default_mem;
1002 }
1003 
1004 static const struct reserved_mem_ops rmem_swiotlb_ops = {
1005 	.device_init = rmem_swiotlb_device_init,
1006 	.device_release = rmem_swiotlb_device_release,
1007 };
1008 
1009 static int __init rmem_swiotlb_setup(struct reserved_mem *rmem)
1010 {
1011 	unsigned long node = rmem->fdt_node;
1012 
1013 	if (of_get_flat_dt_prop(node, "reusable", NULL) ||
1014 	    of_get_flat_dt_prop(node, "linux,cma-default", NULL) ||
1015 	    of_get_flat_dt_prop(node, "linux,dma-default", NULL) ||
1016 	    of_get_flat_dt_prop(node, "no-map", NULL))
1017 		return -EINVAL;
1018 
1019 	if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) {
1020 		pr_err("Restricted DMA pool must be accessible within the linear mapping.");
1021 		return -EINVAL;
1022 	}
1023 
1024 	rmem->ops = &rmem_swiotlb_ops;
1025 	pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld MiB\n",
1026 		&rmem->base, (unsigned long)rmem->size / SZ_1M);
1027 	return 0;
1028 }
1029 
1030 RESERVEDMEM_OF_DECLARE(dma, "restricted-dma-pool", rmem_swiotlb_setup);
1031 #endif /* CONFIG_DMA_RESTRICTED_POOL */
1032