xref: /openbmc/linux/kernel/dma/swiotlb.c (revision d0e22329)
1 /*
2  * Dynamic DMA mapping support.
3  *
4  * This implementation is a fallback for platforms that do not support
5  * 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.
14  * 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.
17  * 08/12/11 beckyb	Add highmem support
18  */
19 
20 #define pr_fmt(fmt) "software IO TLB: " fmt
21 
22 #include <linux/cache.h>
23 #include <linux/dma-direct.h>
24 #include <linux/mm.h>
25 #include <linux/export.h>
26 #include <linux/spinlock.h>
27 #include <linux/string.h>
28 #include <linux/swiotlb.h>
29 #include <linux/pfn.h>
30 #include <linux/types.h>
31 #include <linux/ctype.h>
32 #include <linux/highmem.h>
33 #include <linux/gfp.h>
34 #include <linux/scatterlist.h>
35 #include <linux/mem_encrypt.h>
36 #include <linux/set_memory.h>
37 
38 #include <asm/io.h>
39 #include <asm/dma.h>
40 
41 #include <linux/init.h>
42 #include <linux/memblock.h>
43 #include <linux/iommu-helper.h>
44 
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/swiotlb.h>
47 
48 #define OFFSET(val,align) ((unsigned long)	\
49 	                   ( (val) & ( (align) - 1)))
50 
51 #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 
60 enum swiotlb_force swiotlb_force;
61 
62 /*
63  * 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
65  * API.
66  */
67 phys_addr_t io_tlb_start, io_tlb_end;
68 
69 /*
70  * The number of IO TLB blocks (in groups of 64) between io_tlb_start and
71  * io_tlb_end.  This is command line adjustable via setup_io_tlb_npages.
72  */
73 static unsigned long io_tlb_nslabs;
74 
75 /*
76  * This is a free list describing the number of free entries available from
77  * each index
78  */
79 static unsigned int *io_tlb_list;
80 static unsigned int io_tlb_index;
81 
82 /*
83  * Max segment that we can provide which (if pages are contingous) will
84  * not be bounced (unless SWIOTLB_FORCE is set).
85  */
86 unsigned int max_segment;
87 
88 /*
89  * We need to save away the original address corresponding to a mapped entry
90  * for the sync operations.
91  */
92 #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
93 static phys_addr_t *io_tlb_orig_addr;
94 
95 /*
96  * Protect the above data structures in the map and unmap calls
97  */
98 static DEFINE_SPINLOCK(io_tlb_lock);
99 
100 static int late_alloc;
101 
102 static int __init
103 setup_io_tlb_npages(char *str)
104 {
105 	if (isdigit(*str)) {
106 		io_tlb_nslabs = simple_strtoul(str, &str, 0);
107 		/* 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;
112 	if (!strcmp(str, "force")) {
113 		swiotlb_force = SWIOTLB_FORCE;
114 	} else if (!strcmp(str, "noforce")) {
115 		swiotlb_force = SWIOTLB_NO_FORCE;
116 		io_tlb_nslabs = 1;
117 	}
118 
119 	return 0;
120 }
121 early_param("swiotlb", setup_io_tlb_npages);
122 
123 unsigned long swiotlb_nr_tbl(void)
124 {
125 	return io_tlb_nslabs;
126 }
127 EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
128 
129 unsigned int swiotlb_max_segment(void)
130 {
131 	return max_segment;
132 }
133 EXPORT_SYMBOL_GPL(swiotlb_max_segment);
134 
135 void 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 
143 /* default to 64MB */
144 #define IO_TLB_DEFAULT_SIZE (64UL<<20)
145 unsigned 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 
154 static bool no_iotlb_memory;
155 
156 void swiotlb_print_info(void)
157 {
158 	unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
159 
160 	if (no_iotlb_memory) {
161 		pr_warn("No low mem\n");
162 		return;
163 	}
164 
165 	pr_info("mapped [mem %#010llx-%#010llx] (%luMB)\n",
166 	       (unsigned long long)io_tlb_start,
167 	       (unsigned long long)io_tlb_end,
168 	       bytes >> 20);
169 }
170 
171 /*
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  */
177 void __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);
187 	set_memory_decrypted((unsigned long)vaddr, bytes >> PAGE_SHIFT);
188 	memset(vaddr, 0, bytes);
189 }
190 
191 int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
192 {
193 	unsigned long i, bytes;
194 
195 	bytes = nslabs << IO_TLB_SHIFT;
196 
197 	io_tlb_nslabs = nslabs;
198 	io_tlb_start = __pa(tlb);
199 	io_tlb_end = io_tlb_start + bytes;
200 
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 	 */
206 	io_tlb_list = memblock_alloc(
207 				PAGE_ALIGN(io_tlb_nslabs * sizeof(int)),
208 				PAGE_SIZE);
209 	io_tlb_orig_addr = memblock_alloc(
210 				PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)),
211 				PAGE_SIZE);
212 	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;
217 
218 	if (verbose)
219 		swiotlb_print_info();
220 
221 	swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
222 	return 0;
223 }
224 
225 /*
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  */
229 void  __init
230 swiotlb_init(int verbose)
231 {
232 	size_t default_size = IO_TLB_DEFAULT_SIZE;
233 	unsigned char *vstart;
234 	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 
243 	/* Get IO TLB memory from the low pages */
244 	vstart = memblock_alloc_low_nopanic(PAGE_ALIGN(bytes), PAGE_SIZE);
245 	if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose))
246 		return;
247 
248 	if (io_tlb_start)
249 		memblock_free_early(io_tlb_start,
250 				    PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
251 	pr_warn("Cannot allocate buffer");
252 	no_iotlb_memory = true;
253 }
254 
255 /*
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  */
260 int
261 swiotlb_late_init_with_default_size(size_t default_size)
262 {
263 	unsigned long bytes, req_nslabs = io_tlb_nslabs;
264 	unsigned char *vstart = NULL;
265 	unsigned int order;
266 	int rc = 0;
267 
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 	 */
276 	order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
277 	io_tlb_nslabs = SLABS_PER_PAGE << order;
278 	bytes = io_tlb_nslabs << IO_TLB_SHIFT;
279 
280 	while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
281 		vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
282 						  order);
283 		if (vstart)
284 			break;
285 		order--;
286 	}
287 
288 	if (!vstart) {
289 		io_tlb_nslabs = req_nslabs;
290 		return -ENOMEM;
291 	}
292 	if (order != get_order(bytes)) {
293 		pr_warn("only able to allocate %ld MB\n",
294 			(PAGE_SIZE << order) >> 20);
295 		io_tlb_nslabs = SLABS_PER_PAGE << order;
296 	}
297 	rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs);
298 	if (rc)
299 		free_pages((unsigned long)vstart, order);
300 
301 	return rc;
302 }
303 
304 int
305 swiotlb_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;
312 	io_tlb_start = virt_to_phys(tlb);
313 	io_tlb_end = io_tlb_start + bytes;
314 
315 	set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT);
316 	memset(tlb, 0, bytes);
317 
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)
326 		goto cleanup3;
327 
328 	io_tlb_orig_addr = (phys_addr_t *)
329 		__get_free_pages(GFP_KERNEL,
330 				 get_order(io_tlb_nslabs *
331 					   sizeof(phys_addr_t)));
332 	if (!io_tlb_orig_addr)
333 		goto cleanup4;
334 
335 	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;
340 
341 	swiotlb_print_info();
342 
343 	late_alloc = 1;
344 
345 	swiotlb_set_max_segment(io_tlb_nslabs << IO_TLB_SHIFT);
346 
347 	return 0;
348 
349 cleanup4:
350 	free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
351 	                                                 sizeof(int)));
352 	io_tlb_list = NULL;
353 cleanup3:
354 	io_tlb_end = 0;
355 	io_tlb_start = 0;
356 	io_tlb_nslabs = 0;
357 	max_segment = 0;
358 	return -ENOMEM;
359 }
360 
361 void __init swiotlb_exit(void)
362 {
363 	if (!io_tlb_orig_addr)
364 		return;
365 
366 	if (late_alloc) {
367 		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)));
371 		free_pages((unsigned long)phys_to_virt(io_tlb_start),
372 			   get_order(io_tlb_nslabs << IO_TLB_SHIFT));
373 	} else {
374 		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));
380 	}
381 	io_tlb_nslabs = 0;
382 	max_segment = 0;
383 }
384 
385 /*
386  * Bounce: copy the swiotlb buffer back to the original dma location
387  */
388 static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
389 			   size_t size, enum dma_data_direction dir)
390 {
391 	unsigned long pfn = PFN_DOWN(orig_addr);
392 	unsigned char *vaddr = phys_to_virt(tlb_addr);
393 
394 	if (PageHighMem(pfn_to_page(pfn))) {
395 		/* The buffer does not have a mapping.  Map it in and copy */
396 		unsigned int offset = orig_addr & ~PAGE_MASK;
397 		char *buffer;
398 		unsigned int sz = 0;
399 		unsigned long flags;
400 
401 		while (size) {
402 			sz = min_t(size_t, PAGE_SIZE - offset, size);
403 
404 			local_irq_save(flags);
405 			buffer = kmap_atomic(pfn_to_page(pfn));
406 			if (dir == DMA_TO_DEVICE)
407 				memcpy(vaddr, buffer + offset, sz);
408 			else
409 				memcpy(buffer + offset, vaddr, sz);
410 			kunmap_atomic(buffer);
411 			local_irq_restore(flags);
412 
413 			size -= sz;
414 			pfn++;
415 			vaddr += sz;
416 			offset = 0;
417 		}
418 	} else if (dir == DMA_TO_DEVICE) {
419 		memcpy(vaddr, phys_to_virt(orig_addr), size);
420 	} else {
421 		memcpy(phys_to_virt(orig_addr), vaddr, size);
422 	}
423 }
424 
425 phys_addr_t swiotlb_tbl_map_single(struct device *hwdev,
426 				   dma_addr_t tbl_dma_addr,
427 				   phys_addr_t orig_addr, size_t size,
428 				   enum dma_data_direction dir,
429 				   unsigned long attrs)
430 {
431 	unsigned long flags;
432 	phys_addr_t tlb_addr;
433 	unsigned int nslots, stride, index, wrap;
434 	int i;
435 	unsigned long mask;
436 	unsigned long offset_slots;
437 	unsigned long max_slots;
438 
439 	if (no_iotlb_memory)
440 		panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
441 
442 	if (mem_encrypt_active())
443 		pr_warn_once("%s is active and system is using DMA bounce buffers\n",
444 			     sme_active() ? "SME" : "SEV");
445 
446 	mask = dma_get_seg_boundary(hwdev);
447 
448 	tbl_dma_addr &= mask;
449 
450 	offset_slots = ALIGN(tbl_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
451 
452 	/*
453  	 * Carefully handle integer overflow which can occur when mask == ~0UL.
454  	 */
455 	max_slots = mask + 1
456 		    ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
457 		    : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
458 
459 	/*
460 	 * For mappings greater than or equal to a page, we limit the stride
461 	 * (and hence alignment) to a page size.
462 	 */
463 	nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
464 	if (size >= PAGE_SIZE)
465 		stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
466 	else
467 		stride = 1;
468 
469 	BUG_ON(!nslots);
470 
471 	/*
472 	 * Find suitable number of IO TLB entries size that will fit this
473 	 * request and allocate a buffer from that IO TLB pool.
474 	 */
475 	spin_lock_irqsave(&io_tlb_lock, flags);
476 	index = ALIGN(io_tlb_index, stride);
477 	if (index >= io_tlb_nslabs)
478 		index = 0;
479 	wrap = index;
480 
481 	do {
482 		while (iommu_is_span_boundary(index, nslots, offset_slots,
483 					      max_slots)) {
484 			index += stride;
485 			if (index >= io_tlb_nslabs)
486 				index = 0;
487 			if (index == wrap)
488 				goto not_found;
489 		}
490 
491 		/*
492 		 * If we find a slot that indicates we have 'nslots' number of
493 		 * contiguous buffers, we allocate the buffers from that slot
494 		 * and mark the entries as '0' indicating unavailable.
495 		 */
496 		if (io_tlb_list[index] >= nslots) {
497 			int count = 0;
498 
499 			for (i = index; i < (int) (index + nslots); i++)
500 				io_tlb_list[i] = 0;
501 			for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
502 				io_tlb_list[i] = ++count;
503 			tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT);
504 
505 			/*
506 			 * Update the indices to avoid searching in the next
507 			 * round.
508 			 */
509 			io_tlb_index = ((index + nslots) < io_tlb_nslabs
510 					? (index + nslots) : 0);
511 
512 			goto found;
513 		}
514 		index += stride;
515 		if (index >= io_tlb_nslabs)
516 			index = 0;
517 	} while (index != wrap);
518 
519 not_found:
520 	spin_unlock_irqrestore(&io_tlb_lock, flags);
521 	if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit())
522 		dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes)\n", size);
523 	return DMA_MAPPING_ERROR;
524 found:
525 	spin_unlock_irqrestore(&io_tlb_lock, flags);
526 
527 	/*
528 	 * Save away the mapping from the original address to the DMA address.
529 	 * This is needed when we sync the memory.  Then we sync the buffer if
530 	 * needed.
531 	 */
532 	for (i = 0; i < nslots; i++)
533 		io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
534 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
535 	    (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
536 		swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE);
537 
538 	return tlb_addr;
539 }
540 
541 /*
542  * tlb_addr is the physical address of the bounce buffer to unmap.
543  */
544 void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
545 			      size_t size, enum dma_data_direction dir,
546 			      unsigned long attrs)
547 {
548 	unsigned long flags;
549 	int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
550 	int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
551 	phys_addr_t orig_addr = io_tlb_orig_addr[index];
552 
553 	/*
554 	 * First, sync the memory before unmapping the entry
555 	 */
556 	if (orig_addr != INVALID_PHYS_ADDR &&
557 	    !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
558 	    ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
559 		swiotlb_bounce(orig_addr, tlb_addr, size, DMA_FROM_DEVICE);
560 
561 	/*
562 	 * Return the buffer to the free list by setting the corresponding
563 	 * entries to indicate the number of contiguous entries available.
564 	 * While returning the entries to the free list, we merge the entries
565 	 * with slots below and above the pool being returned.
566 	 */
567 	spin_lock_irqsave(&io_tlb_lock, flags);
568 	{
569 		count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
570 			 io_tlb_list[index + nslots] : 0);
571 		/*
572 		 * Step 1: return the slots to the free list, merging the
573 		 * slots with superceeding slots
574 		 */
575 		for (i = index + nslots - 1; i >= index; i--) {
576 			io_tlb_list[i] = ++count;
577 			io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
578 		}
579 		/*
580 		 * Step 2: merge the returned slots with the preceding slots,
581 		 * if available (non zero)
582 		 */
583 		for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
584 			io_tlb_list[i] = ++count;
585 	}
586 	spin_unlock_irqrestore(&io_tlb_lock, flags);
587 }
588 
589 void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr,
590 			     size_t size, enum dma_data_direction dir,
591 			     enum dma_sync_target target)
592 {
593 	int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
594 	phys_addr_t orig_addr = io_tlb_orig_addr[index];
595 
596 	if (orig_addr == INVALID_PHYS_ADDR)
597 		return;
598 	orig_addr += (unsigned long)tlb_addr & ((1 << IO_TLB_SHIFT) - 1);
599 
600 	switch (target) {
601 	case SYNC_FOR_CPU:
602 		if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
603 			swiotlb_bounce(orig_addr, tlb_addr,
604 				       size, DMA_FROM_DEVICE);
605 		else
606 			BUG_ON(dir != DMA_TO_DEVICE);
607 		break;
608 	case SYNC_FOR_DEVICE:
609 		if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
610 			swiotlb_bounce(orig_addr, tlb_addr,
611 				       size, DMA_TO_DEVICE);
612 		else
613 			BUG_ON(dir != DMA_FROM_DEVICE);
614 		break;
615 	default:
616 		BUG();
617 	}
618 }
619 
620 /*
621  * Create a swiotlb mapping for the buffer at @phys, and in case of DMAing
622  * to the device copy the data into it as well.
623  */
624 bool swiotlb_map(struct device *dev, phys_addr_t *phys, dma_addr_t *dma_addr,
625 		size_t size, enum dma_data_direction dir, unsigned long attrs)
626 {
627 	trace_swiotlb_bounced(dev, *dma_addr, size, swiotlb_force);
628 
629 	if (unlikely(swiotlb_force == SWIOTLB_NO_FORCE)) {
630 		dev_warn_ratelimited(dev,
631 			"Cannot do DMA to address %pa\n", phys);
632 		return false;
633 	}
634 
635 	/* Oh well, have to allocate and map a bounce buffer. */
636 	*phys = swiotlb_tbl_map_single(dev, __phys_to_dma(dev, io_tlb_start),
637 			*phys, size, dir, attrs);
638 	if (*phys == DMA_MAPPING_ERROR)
639 		return false;
640 
641 	/* Ensure that the address returned is DMA'ble */
642 	*dma_addr = __phys_to_dma(dev, *phys);
643 	if (unlikely(!dma_capable(dev, *dma_addr, size))) {
644 		swiotlb_tbl_unmap_single(dev, *phys, size, dir,
645 			attrs | DMA_ATTR_SKIP_CPU_SYNC);
646 		return false;
647 	}
648 
649 	return true;
650 }
651 
652 /*
653  * Return whether the given device DMA address mask can be supported
654  * properly.  For example, if your device can only drive the low 24-bits
655  * during bus mastering, then you would pass 0x00ffffff as the mask to
656  * this function.
657  */
658 int
659 swiotlb_dma_supported(struct device *hwdev, u64 mask)
660 {
661 	return __phys_to_dma(hwdev, io_tlb_end - 1) <= mask;
662 }
663