xref: /linux/arch/arm/mm/dma-mapping.c (revision bef4d2037d2143a4df6430bbe1e970fc7e616f6c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/arch/arm/mm/dma-mapping.c
4  *
5  *  Copyright (C) 2000-2004 Russell King
6  *
7  *  DMA uncached mapping support.
8  */
9 #include <linux/module.h>
10 #include <linux/mm.h>
11 #include <linux/genalloc.h>
12 #include <linux/gfp.h>
13 #include <linux/errno.h>
14 #include <linux/list.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/dma-direct.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/dma-noncoherent.h>
20 #include <linux/dma-contiguous.h>
21 #include <linux/highmem.h>
22 #include <linux/memblock.h>
23 #include <linux/slab.h>
24 #include <linux/iommu.h>
25 #include <linux/io.h>
26 #include <linux/vmalloc.h>
27 #include <linux/sizes.h>
28 #include <linux/cma.h>
29 
30 #include <asm/memory.h>
31 #include <asm/highmem.h>
32 #include <asm/cacheflush.h>
33 #include <asm/tlbflush.h>
34 #include <asm/mach/arch.h>
35 #include <asm/dma-iommu.h>
36 #include <asm/mach/map.h>
37 #include <asm/system_info.h>
38 #include <asm/dma-contiguous.h>
39 
40 #include "dma.h"
41 #include "mm.h"
42 
43 struct arm_dma_alloc_args {
44 	struct device *dev;
45 	size_t size;
46 	gfp_t gfp;
47 	pgprot_t prot;
48 	const void *caller;
49 	bool want_vaddr;
50 	int coherent_flag;
51 };
52 
53 struct arm_dma_free_args {
54 	struct device *dev;
55 	size_t size;
56 	void *cpu_addr;
57 	struct page *page;
58 	bool want_vaddr;
59 };
60 
61 #define NORMAL	    0
62 #define COHERENT    1
63 
64 struct arm_dma_allocator {
65 	void *(*alloc)(struct arm_dma_alloc_args *args,
66 		       struct page **ret_page);
67 	void (*free)(struct arm_dma_free_args *args);
68 };
69 
70 struct arm_dma_buffer {
71 	struct list_head list;
72 	void *virt;
73 	struct arm_dma_allocator *allocator;
74 };
75 
76 static LIST_HEAD(arm_dma_bufs);
77 static DEFINE_SPINLOCK(arm_dma_bufs_lock);
78 
79 static struct arm_dma_buffer *arm_dma_buffer_find(void *virt)
80 {
81 	struct arm_dma_buffer *buf, *found = NULL;
82 	unsigned long flags;
83 
84 	spin_lock_irqsave(&arm_dma_bufs_lock, flags);
85 	list_for_each_entry(buf, &arm_dma_bufs, list) {
86 		if (buf->virt == virt) {
87 			list_del(&buf->list);
88 			found = buf;
89 			break;
90 		}
91 	}
92 	spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
93 	return found;
94 }
95 
96 /*
97  * The DMA API is built upon the notion of "buffer ownership".  A buffer
98  * is either exclusively owned by the CPU (and therefore may be accessed
99  * by it) or exclusively owned by the DMA device.  These helper functions
100  * represent the transitions between these two ownership states.
101  *
102  * Note, however, that on later ARMs, this notion does not work due to
103  * speculative prefetches.  We model our approach on the assumption that
104  * the CPU does do speculative prefetches, which means we clean caches
105  * before transfers and delay cache invalidation until transfer completion.
106  *
107  */
108 static void __dma_page_cpu_to_dev(struct page *, unsigned long,
109 		size_t, enum dma_data_direction);
110 static void __dma_page_dev_to_cpu(struct page *, unsigned long,
111 		size_t, enum dma_data_direction);
112 
113 /**
114  * arm_dma_map_page - map a portion of a page for streaming DMA
115  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
116  * @page: page that buffer resides in
117  * @offset: offset into page for start of buffer
118  * @size: size of buffer to map
119  * @dir: DMA transfer direction
120  *
121  * Ensure that any data held in the cache is appropriately discarded
122  * or written back.
123  *
124  * The device owns this memory once this call has completed.  The CPU
125  * can regain ownership by calling dma_unmap_page().
126  */
127 static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
128 	     unsigned long offset, size_t size, enum dma_data_direction dir,
129 	     unsigned long attrs)
130 {
131 	if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
132 		__dma_page_cpu_to_dev(page, offset, size, dir);
133 	return pfn_to_dma(dev, page_to_pfn(page)) + offset;
134 }
135 
136 static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
137 	     unsigned long offset, size_t size, enum dma_data_direction dir,
138 	     unsigned long attrs)
139 {
140 	return pfn_to_dma(dev, page_to_pfn(page)) + offset;
141 }
142 
143 /**
144  * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
145  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
146  * @handle: DMA address of buffer
147  * @size: size of buffer (same as passed to dma_map_page)
148  * @dir: DMA transfer direction (same as passed to dma_map_page)
149  *
150  * Unmap a page streaming mode DMA translation.  The handle and size
151  * must match what was provided in the previous dma_map_page() call.
152  * All other usages are undefined.
153  *
154  * After this call, reads by the CPU to the buffer are guaranteed to see
155  * whatever the device wrote there.
156  */
157 static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
158 		size_t size, enum dma_data_direction dir, unsigned long attrs)
159 {
160 	if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
161 		__dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
162 				      handle & ~PAGE_MASK, size, dir);
163 }
164 
165 static void arm_dma_sync_single_for_cpu(struct device *dev,
166 		dma_addr_t handle, size_t size, enum dma_data_direction dir)
167 {
168 	unsigned int offset = handle & (PAGE_SIZE - 1);
169 	struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
170 	__dma_page_dev_to_cpu(page, offset, size, dir);
171 }
172 
173 static void arm_dma_sync_single_for_device(struct device *dev,
174 		dma_addr_t handle, size_t size, enum dma_data_direction dir)
175 {
176 	unsigned int offset = handle & (PAGE_SIZE - 1);
177 	struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
178 	__dma_page_cpu_to_dev(page, offset, size, dir);
179 }
180 
181 const struct dma_map_ops arm_dma_ops = {
182 	.alloc			= arm_dma_alloc,
183 	.free			= arm_dma_free,
184 	.mmap			= arm_dma_mmap,
185 	.get_sgtable		= arm_dma_get_sgtable,
186 	.map_page		= arm_dma_map_page,
187 	.unmap_page		= arm_dma_unmap_page,
188 	.map_sg			= arm_dma_map_sg,
189 	.unmap_sg		= arm_dma_unmap_sg,
190 	.map_resource		= dma_direct_map_resource,
191 	.sync_single_for_cpu	= arm_dma_sync_single_for_cpu,
192 	.sync_single_for_device	= arm_dma_sync_single_for_device,
193 	.sync_sg_for_cpu	= arm_dma_sync_sg_for_cpu,
194 	.sync_sg_for_device	= arm_dma_sync_sg_for_device,
195 	.dma_supported		= arm_dma_supported,
196 	.get_required_mask	= dma_direct_get_required_mask,
197 };
198 EXPORT_SYMBOL(arm_dma_ops);
199 
200 static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
201 	dma_addr_t *handle, gfp_t gfp, unsigned long attrs);
202 static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
203 				  dma_addr_t handle, unsigned long attrs);
204 static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
205 		 void *cpu_addr, dma_addr_t dma_addr, size_t size,
206 		 unsigned long attrs);
207 
208 const struct dma_map_ops arm_coherent_dma_ops = {
209 	.alloc			= arm_coherent_dma_alloc,
210 	.free			= arm_coherent_dma_free,
211 	.mmap			= arm_coherent_dma_mmap,
212 	.get_sgtable		= arm_dma_get_sgtable,
213 	.map_page		= arm_coherent_dma_map_page,
214 	.map_sg			= arm_dma_map_sg,
215 	.map_resource		= dma_direct_map_resource,
216 	.dma_supported		= arm_dma_supported,
217 	.get_required_mask	= dma_direct_get_required_mask,
218 };
219 EXPORT_SYMBOL(arm_coherent_dma_ops);
220 
221 static int __dma_supported(struct device *dev, u64 mask, bool warn)
222 {
223 	unsigned long max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
224 
225 	/*
226 	 * Translate the device's DMA mask to a PFN limit.  This
227 	 * PFN number includes the page which we can DMA to.
228 	 */
229 	if (dma_to_pfn(dev, mask) < max_dma_pfn) {
230 		if (warn)
231 			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
232 				 mask,
233 				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
234 				 max_dma_pfn + 1);
235 		return 0;
236 	}
237 
238 	return 1;
239 }
240 
241 static u64 get_coherent_dma_mask(struct device *dev)
242 {
243 	u64 mask = (u64)DMA_BIT_MASK(32);
244 
245 	if (dev) {
246 		mask = dev->coherent_dma_mask;
247 
248 		/*
249 		 * Sanity check the DMA mask - it must be non-zero, and
250 		 * must be able to be satisfied by a DMA allocation.
251 		 */
252 		if (mask == 0) {
253 			dev_warn(dev, "coherent DMA mask is unset\n");
254 			return 0;
255 		}
256 
257 		if (!__dma_supported(dev, mask, true))
258 			return 0;
259 	}
260 
261 	return mask;
262 }
263 
264 static void __dma_clear_buffer(struct page *page, size_t size, int coherent_flag)
265 {
266 	/*
267 	 * Ensure that the allocated pages are zeroed, and that any data
268 	 * lurking in the kernel direct-mapped region is invalidated.
269 	 */
270 	if (PageHighMem(page)) {
271 		phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
272 		phys_addr_t end = base + size;
273 		while (size > 0) {
274 			void *ptr = kmap_atomic(page);
275 			memset(ptr, 0, PAGE_SIZE);
276 			if (coherent_flag != COHERENT)
277 				dmac_flush_range(ptr, ptr + PAGE_SIZE);
278 			kunmap_atomic(ptr);
279 			page++;
280 			size -= PAGE_SIZE;
281 		}
282 		if (coherent_flag != COHERENT)
283 			outer_flush_range(base, end);
284 	} else {
285 		void *ptr = page_address(page);
286 		memset(ptr, 0, size);
287 		if (coherent_flag != COHERENT) {
288 			dmac_flush_range(ptr, ptr + size);
289 			outer_flush_range(__pa(ptr), __pa(ptr) + size);
290 		}
291 	}
292 }
293 
294 /*
295  * Allocate a DMA buffer for 'dev' of size 'size' using the
296  * specified gfp mask.  Note that 'size' must be page aligned.
297  */
298 static struct page *__dma_alloc_buffer(struct device *dev, size_t size,
299 				       gfp_t gfp, int coherent_flag)
300 {
301 	unsigned long order = get_order(size);
302 	struct page *page, *p, *e;
303 
304 	page = alloc_pages(gfp, order);
305 	if (!page)
306 		return NULL;
307 
308 	/*
309 	 * Now split the huge page and free the excess pages
310 	 */
311 	split_page(page, order);
312 	for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
313 		__free_page(p);
314 
315 	__dma_clear_buffer(page, size, coherent_flag);
316 
317 	return page;
318 }
319 
320 /*
321  * Free a DMA buffer.  'size' must be page aligned.
322  */
323 static void __dma_free_buffer(struct page *page, size_t size)
324 {
325 	struct page *e = page + (size >> PAGE_SHIFT);
326 
327 	while (page < e) {
328 		__free_page(page);
329 		page++;
330 	}
331 }
332 
333 static void *__alloc_from_contiguous(struct device *dev, size_t size,
334 				     pgprot_t prot, struct page **ret_page,
335 				     const void *caller, bool want_vaddr,
336 				     int coherent_flag, gfp_t gfp);
337 
338 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
339 				 pgprot_t prot, struct page **ret_page,
340 				 const void *caller, bool want_vaddr);
341 
342 #define DEFAULT_DMA_COHERENT_POOL_SIZE	SZ_256K
343 static struct gen_pool *atomic_pool __ro_after_init;
344 
345 static size_t atomic_pool_size __initdata = DEFAULT_DMA_COHERENT_POOL_SIZE;
346 
347 static int __init early_coherent_pool(char *p)
348 {
349 	atomic_pool_size = memparse(p, &p);
350 	return 0;
351 }
352 early_param("coherent_pool", early_coherent_pool);
353 
354 /*
355  * Initialise the coherent pool for atomic allocations.
356  */
357 static int __init atomic_pool_init(void)
358 {
359 	pgprot_t prot = pgprot_dmacoherent(PAGE_KERNEL);
360 	gfp_t gfp = GFP_KERNEL | GFP_DMA;
361 	struct page *page;
362 	void *ptr;
363 
364 	atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
365 	if (!atomic_pool)
366 		goto out;
367 	/*
368 	 * The atomic pool is only used for non-coherent allocations
369 	 * so we must pass NORMAL for coherent_flag.
370 	 */
371 	if (dev_get_cma_area(NULL))
372 		ptr = __alloc_from_contiguous(NULL, atomic_pool_size, prot,
373 				      &page, atomic_pool_init, true, NORMAL,
374 				      GFP_KERNEL);
375 	else
376 		ptr = __alloc_remap_buffer(NULL, atomic_pool_size, gfp, prot,
377 					   &page, atomic_pool_init, true);
378 	if (ptr) {
379 		int ret;
380 
381 		ret = gen_pool_add_virt(atomic_pool, (unsigned long)ptr,
382 					page_to_phys(page),
383 					atomic_pool_size, -1);
384 		if (ret)
385 			goto destroy_genpool;
386 
387 		gen_pool_set_algo(atomic_pool,
388 				gen_pool_first_fit_order_align,
389 				NULL);
390 		pr_info("DMA: preallocated %zu KiB pool for atomic coherent allocations\n",
391 		       atomic_pool_size / 1024);
392 		return 0;
393 	}
394 
395 destroy_genpool:
396 	gen_pool_destroy(atomic_pool);
397 	atomic_pool = NULL;
398 out:
399 	pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
400 	       atomic_pool_size / 1024);
401 	return -ENOMEM;
402 }
403 /*
404  * CMA is activated by core_initcall, so we must be called after it.
405  */
406 postcore_initcall(atomic_pool_init);
407 
408 struct dma_contig_early_reserve {
409 	phys_addr_t base;
410 	unsigned long size;
411 };
412 
413 static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
414 
415 static int dma_mmu_remap_num __initdata;
416 
417 void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
418 {
419 	dma_mmu_remap[dma_mmu_remap_num].base = base;
420 	dma_mmu_remap[dma_mmu_remap_num].size = size;
421 	dma_mmu_remap_num++;
422 }
423 
424 void __init dma_contiguous_remap(void)
425 {
426 	int i;
427 	for (i = 0; i < dma_mmu_remap_num; i++) {
428 		phys_addr_t start = dma_mmu_remap[i].base;
429 		phys_addr_t end = start + dma_mmu_remap[i].size;
430 		struct map_desc map;
431 		unsigned long addr;
432 
433 		if (end > arm_lowmem_limit)
434 			end = arm_lowmem_limit;
435 		if (start >= end)
436 			continue;
437 
438 		map.pfn = __phys_to_pfn(start);
439 		map.virtual = __phys_to_virt(start);
440 		map.length = end - start;
441 		map.type = MT_MEMORY_DMA_READY;
442 
443 		/*
444 		 * Clear previous low-memory mapping to ensure that the
445 		 * TLB does not see any conflicting entries, then flush
446 		 * the TLB of the old entries before creating new mappings.
447 		 *
448 		 * This ensures that any speculatively loaded TLB entries
449 		 * (even though they may be rare) can not cause any problems,
450 		 * and ensures that this code is architecturally compliant.
451 		 */
452 		for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
453 		     addr += PMD_SIZE)
454 			pmd_clear(pmd_off_k(addr));
455 
456 		flush_tlb_kernel_range(__phys_to_virt(start),
457 				       __phys_to_virt(end));
458 
459 		iotable_init(&map, 1);
460 	}
461 }
462 
463 static int __dma_update_pte(pte_t *pte, unsigned long addr, void *data)
464 {
465 	struct page *page = virt_to_page(addr);
466 	pgprot_t prot = *(pgprot_t *)data;
467 
468 	set_pte_ext(pte, mk_pte(page, prot), 0);
469 	return 0;
470 }
471 
472 static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
473 {
474 	unsigned long start = (unsigned long) page_address(page);
475 	unsigned end = start + size;
476 
477 	apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
478 	flush_tlb_kernel_range(start, end);
479 }
480 
481 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
482 				 pgprot_t prot, struct page **ret_page,
483 				 const void *caller, bool want_vaddr)
484 {
485 	struct page *page;
486 	void *ptr = NULL;
487 	/*
488 	 * __alloc_remap_buffer is only called when the device is
489 	 * non-coherent
490 	 */
491 	page = __dma_alloc_buffer(dev, size, gfp, NORMAL);
492 	if (!page)
493 		return NULL;
494 	if (!want_vaddr)
495 		goto out;
496 
497 	ptr = dma_common_contiguous_remap(page, size, prot, caller);
498 	if (!ptr) {
499 		__dma_free_buffer(page, size);
500 		return NULL;
501 	}
502 
503  out:
504 	*ret_page = page;
505 	return ptr;
506 }
507 
508 static void *__alloc_from_pool(size_t size, struct page **ret_page)
509 {
510 	unsigned long val;
511 	void *ptr = NULL;
512 
513 	if (!atomic_pool) {
514 		WARN(1, "coherent pool not initialised!\n");
515 		return NULL;
516 	}
517 
518 	val = gen_pool_alloc(atomic_pool, size);
519 	if (val) {
520 		phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
521 
522 		*ret_page = phys_to_page(phys);
523 		ptr = (void *)val;
524 	}
525 
526 	return ptr;
527 }
528 
529 static bool __in_atomic_pool(void *start, size_t size)
530 {
531 	return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
532 }
533 
534 static int __free_from_pool(void *start, size_t size)
535 {
536 	if (!__in_atomic_pool(start, size))
537 		return 0;
538 
539 	gen_pool_free(atomic_pool, (unsigned long)start, size);
540 
541 	return 1;
542 }
543 
544 static void *__alloc_from_contiguous(struct device *dev, size_t size,
545 				     pgprot_t prot, struct page **ret_page,
546 				     const void *caller, bool want_vaddr,
547 				     int coherent_flag, gfp_t gfp)
548 {
549 	unsigned long order = get_order(size);
550 	size_t count = size >> PAGE_SHIFT;
551 	struct page *page;
552 	void *ptr = NULL;
553 
554 	page = dma_alloc_from_contiguous(dev, count, order, gfp & __GFP_NOWARN);
555 	if (!page)
556 		return NULL;
557 
558 	__dma_clear_buffer(page, size, coherent_flag);
559 
560 	if (!want_vaddr)
561 		goto out;
562 
563 	if (PageHighMem(page)) {
564 		ptr = dma_common_contiguous_remap(page, size, prot, caller);
565 		if (!ptr) {
566 			dma_release_from_contiguous(dev, page, count);
567 			return NULL;
568 		}
569 	} else {
570 		__dma_remap(page, size, prot);
571 		ptr = page_address(page);
572 	}
573 
574  out:
575 	*ret_page = page;
576 	return ptr;
577 }
578 
579 static void __free_from_contiguous(struct device *dev, struct page *page,
580 				   void *cpu_addr, size_t size, bool want_vaddr)
581 {
582 	if (want_vaddr) {
583 		if (PageHighMem(page))
584 			dma_common_free_remap(cpu_addr, size);
585 		else
586 			__dma_remap(page, size, PAGE_KERNEL);
587 	}
588 	dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
589 }
590 
591 static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
592 {
593 	prot = (attrs & DMA_ATTR_WRITE_COMBINE) ?
594 			pgprot_writecombine(prot) :
595 			pgprot_dmacoherent(prot);
596 	return prot;
597 }
598 
599 static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
600 				   struct page **ret_page)
601 {
602 	struct page *page;
603 	/* __alloc_simple_buffer is only called when the device is coherent */
604 	page = __dma_alloc_buffer(dev, size, gfp, COHERENT);
605 	if (!page)
606 		return NULL;
607 
608 	*ret_page = page;
609 	return page_address(page);
610 }
611 
612 static void *simple_allocator_alloc(struct arm_dma_alloc_args *args,
613 				    struct page **ret_page)
614 {
615 	return __alloc_simple_buffer(args->dev, args->size, args->gfp,
616 				     ret_page);
617 }
618 
619 static void simple_allocator_free(struct arm_dma_free_args *args)
620 {
621 	__dma_free_buffer(args->page, args->size);
622 }
623 
624 static struct arm_dma_allocator simple_allocator = {
625 	.alloc = simple_allocator_alloc,
626 	.free = simple_allocator_free,
627 };
628 
629 static void *cma_allocator_alloc(struct arm_dma_alloc_args *args,
630 				 struct page **ret_page)
631 {
632 	return __alloc_from_contiguous(args->dev, args->size, args->prot,
633 				       ret_page, args->caller,
634 				       args->want_vaddr, args->coherent_flag,
635 				       args->gfp);
636 }
637 
638 static void cma_allocator_free(struct arm_dma_free_args *args)
639 {
640 	__free_from_contiguous(args->dev, args->page, args->cpu_addr,
641 			       args->size, args->want_vaddr);
642 }
643 
644 static struct arm_dma_allocator cma_allocator = {
645 	.alloc = cma_allocator_alloc,
646 	.free = cma_allocator_free,
647 };
648 
649 static void *pool_allocator_alloc(struct arm_dma_alloc_args *args,
650 				  struct page **ret_page)
651 {
652 	return __alloc_from_pool(args->size, ret_page);
653 }
654 
655 static void pool_allocator_free(struct arm_dma_free_args *args)
656 {
657 	__free_from_pool(args->cpu_addr, args->size);
658 }
659 
660 static struct arm_dma_allocator pool_allocator = {
661 	.alloc = pool_allocator_alloc,
662 	.free = pool_allocator_free,
663 };
664 
665 static void *remap_allocator_alloc(struct arm_dma_alloc_args *args,
666 				   struct page **ret_page)
667 {
668 	return __alloc_remap_buffer(args->dev, args->size, args->gfp,
669 				    args->prot, ret_page, args->caller,
670 				    args->want_vaddr);
671 }
672 
673 static void remap_allocator_free(struct arm_dma_free_args *args)
674 {
675 	if (args->want_vaddr)
676 		dma_common_free_remap(args->cpu_addr, args->size);
677 
678 	__dma_free_buffer(args->page, args->size);
679 }
680 
681 static struct arm_dma_allocator remap_allocator = {
682 	.alloc = remap_allocator_alloc,
683 	.free = remap_allocator_free,
684 };
685 
686 static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
687 			 gfp_t gfp, pgprot_t prot, bool is_coherent,
688 			 unsigned long attrs, const void *caller)
689 {
690 	u64 mask = get_coherent_dma_mask(dev);
691 	struct page *page = NULL;
692 	void *addr;
693 	bool allowblock, cma;
694 	struct arm_dma_buffer *buf;
695 	struct arm_dma_alloc_args args = {
696 		.dev = dev,
697 		.size = PAGE_ALIGN(size),
698 		.gfp = gfp,
699 		.prot = prot,
700 		.caller = caller,
701 		.want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
702 		.coherent_flag = is_coherent ? COHERENT : NORMAL,
703 	};
704 
705 #ifdef CONFIG_DMA_API_DEBUG
706 	u64 limit = (mask + 1) & ~mask;
707 	if (limit && size >= limit) {
708 		dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
709 			size, mask);
710 		return NULL;
711 	}
712 #endif
713 
714 	if (!mask)
715 		return NULL;
716 
717 	buf = kzalloc(sizeof(*buf),
718 		      gfp & ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM));
719 	if (!buf)
720 		return NULL;
721 
722 	if (mask < 0xffffffffULL)
723 		gfp |= GFP_DMA;
724 
725 	/*
726 	 * Following is a work-around (a.k.a. hack) to prevent pages
727 	 * with __GFP_COMP being passed to split_page() which cannot
728 	 * handle them.  The real problem is that this flag probably
729 	 * should be 0 on ARM as it is not supported on this
730 	 * platform; see CONFIG_HUGETLBFS.
731 	 */
732 	gfp &= ~(__GFP_COMP);
733 	args.gfp = gfp;
734 
735 	*handle = DMA_MAPPING_ERROR;
736 	allowblock = gfpflags_allow_blocking(gfp);
737 	cma = allowblock ? dev_get_cma_area(dev) : false;
738 
739 	if (cma)
740 		buf->allocator = &cma_allocator;
741 	else if (is_coherent)
742 		buf->allocator = &simple_allocator;
743 	else if (allowblock)
744 		buf->allocator = &remap_allocator;
745 	else
746 		buf->allocator = &pool_allocator;
747 
748 	addr = buf->allocator->alloc(&args, &page);
749 
750 	if (page) {
751 		unsigned long flags;
752 
753 		*handle = pfn_to_dma(dev, page_to_pfn(page));
754 		buf->virt = args.want_vaddr ? addr : page;
755 
756 		spin_lock_irqsave(&arm_dma_bufs_lock, flags);
757 		list_add(&buf->list, &arm_dma_bufs);
758 		spin_unlock_irqrestore(&arm_dma_bufs_lock, flags);
759 	} else {
760 		kfree(buf);
761 	}
762 
763 	return args.want_vaddr ? addr : page;
764 }
765 
766 /*
767  * Allocate DMA-coherent memory space and return both the kernel remapped
768  * virtual and bus address for that space.
769  */
770 void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
771 		    gfp_t gfp, unsigned long attrs)
772 {
773 	pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
774 
775 	return __dma_alloc(dev, size, handle, gfp, prot, false,
776 			   attrs, __builtin_return_address(0));
777 }
778 
779 static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
780 	dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
781 {
782 	return __dma_alloc(dev, size, handle, gfp, PAGE_KERNEL, true,
783 			   attrs, __builtin_return_address(0));
784 }
785 
786 static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
787 		 void *cpu_addr, dma_addr_t dma_addr, size_t size,
788 		 unsigned long attrs)
789 {
790 	int ret = -ENXIO;
791 	unsigned long nr_vma_pages = vma_pages(vma);
792 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
793 	unsigned long pfn = dma_to_pfn(dev, dma_addr);
794 	unsigned long off = vma->vm_pgoff;
795 
796 	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
797 		return ret;
798 
799 	if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
800 		ret = remap_pfn_range(vma, vma->vm_start,
801 				      pfn + off,
802 				      vma->vm_end - vma->vm_start,
803 				      vma->vm_page_prot);
804 	}
805 
806 	return ret;
807 }
808 
809 /*
810  * Create userspace mapping for the DMA-coherent memory.
811  */
812 static int arm_coherent_dma_mmap(struct device *dev, struct vm_area_struct *vma,
813 		 void *cpu_addr, dma_addr_t dma_addr, size_t size,
814 		 unsigned long attrs)
815 {
816 	return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
817 }
818 
819 int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
820 		 void *cpu_addr, dma_addr_t dma_addr, size_t size,
821 		 unsigned long attrs)
822 {
823 	vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
824 	return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
825 }
826 
827 /*
828  * Free a buffer as defined by the above mapping.
829  */
830 static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
831 			   dma_addr_t handle, unsigned long attrs,
832 			   bool is_coherent)
833 {
834 	struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
835 	struct arm_dma_buffer *buf;
836 	struct arm_dma_free_args args = {
837 		.dev = dev,
838 		.size = PAGE_ALIGN(size),
839 		.cpu_addr = cpu_addr,
840 		.page = page,
841 		.want_vaddr = ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0),
842 	};
843 
844 	buf = arm_dma_buffer_find(cpu_addr);
845 	if (WARN(!buf, "Freeing invalid buffer %p\n", cpu_addr))
846 		return;
847 
848 	buf->allocator->free(&args);
849 	kfree(buf);
850 }
851 
852 void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
853 		  dma_addr_t handle, unsigned long attrs)
854 {
855 	__arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
856 }
857 
858 static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
859 				  dma_addr_t handle, unsigned long attrs)
860 {
861 	__arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
862 }
863 
864 int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
865 		 void *cpu_addr, dma_addr_t handle, size_t size,
866 		 unsigned long attrs)
867 {
868 	unsigned long pfn = dma_to_pfn(dev, handle);
869 	struct page *page;
870 	int ret;
871 
872 	/* If the PFN is not valid, we do not have a struct page */
873 	if (!pfn_valid(pfn))
874 		return -ENXIO;
875 
876 	page = pfn_to_page(pfn);
877 
878 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
879 	if (unlikely(ret))
880 		return ret;
881 
882 	sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
883 	return 0;
884 }
885 
886 static void dma_cache_maint_page(struct page *page, unsigned long offset,
887 	size_t size, enum dma_data_direction dir,
888 	void (*op)(const void *, size_t, int))
889 {
890 	unsigned long pfn;
891 	size_t left = size;
892 
893 	pfn = page_to_pfn(page) + offset / PAGE_SIZE;
894 	offset %= PAGE_SIZE;
895 
896 	/*
897 	 * A single sg entry may refer to multiple physically contiguous
898 	 * pages.  But we still need to process highmem pages individually.
899 	 * If highmem is not configured then the bulk of this loop gets
900 	 * optimized out.
901 	 */
902 	do {
903 		size_t len = left;
904 		void *vaddr;
905 
906 		page = pfn_to_page(pfn);
907 
908 		if (PageHighMem(page)) {
909 			if (len + offset > PAGE_SIZE)
910 				len = PAGE_SIZE - offset;
911 
912 			if (cache_is_vipt_nonaliasing()) {
913 				vaddr = kmap_atomic(page);
914 				op(vaddr + offset, len, dir);
915 				kunmap_atomic(vaddr);
916 			} else {
917 				vaddr = kmap_high_get(page);
918 				if (vaddr) {
919 					op(vaddr + offset, len, dir);
920 					kunmap_high(page);
921 				}
922 			}
923 		} else {
924 			vaddr = page_address(page) + offset;
925 			op(vaddr, len, dir);
926 		}
927 		offset = 0;
928 		pfn++;
929 		left -= len;
930 	} while (left);
931 }
932 
933 /*
934  * Make an area consistent for devices.
935  * Note: Drivers should NOT use this function directly, as it will break
936  * platforms with CONFIG_DMABOUNCE.
937  * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
938  */
939 static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
940 	size_t size, enum dma_data_direction dir)
941 {
942 	phys_addr_t paddr;
943 
944 	dma_cache_maint_page(page, off, size, dir, dmac_map_area);
945 
946 	paddr = page_to_phys(page) + off;
947 	if (dir == DMA_FROM_DEVICE) {
948 		outer_inv_range(paddr, paddr + size);
949 	} else {
950 		outer_clean_range(paddr, paddr + size);
951 	}
952 	/* FIXME: non-speculating: flush on bidirectional mappings? */
953 }
954 
955 static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
956 	size_t size, enum dma_data_direction dir)
957 {
958 	phys_addr_t paddr = page_to_phys(page) + off;
959 
960 	/* FIXME: non-speculating: not required */
961 	/* in any case, don't bother invalidating if DMA to device */
962 	if (dir != DMA_TO_DEVICE) {
963 		outer_inv_range(paddr, paddr + size);
964 
965 		dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
966 	}
967 
968 	/*
969 	 * Mark the D-cache clean for these pages to avoid extra flushing.
970 	 */
971 	if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
972 		unsigned long pfn;
973 		size_t left = size;
974 
975 		pfn = page_to_pfn(page) + off / PAGE_SIZE;
976 		off %= PAGE_SIZE;
977 		if (off) {
978 			pfn++;
979 			left -= PAGE_SIZE - off;
980 		}
981 		while (left >= PAGE_SIZE) {
982 			page = pfn_to_page(pfn++);
983 			set_bit(PG_dcache_clean, &page->flags);
984 			left -= PAGE_SIZE;
985 		}
986 	}
987 }
988 
989 /**
990  * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
991  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
992  * @sg: list of buffers
993  * @nents: number of buffers to map
994  * @dir: DMA transfer direction
995  *
996  * Map a set of buffers described by scatterlist in streaming mode for DMA.
997  * This is the scatter-gather version of the dma_map_single interface.
998  * Here the scatter gather list elements are each tagged with the
999  * appropriate dma address and length.  They are obtained via
1000  * sg_dma_{address,length}.
1001  *
1002  * Device ownership issues as mentioned for dma_map_single are the same
1003  * here.
1004  */
1005 int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1006 		enum dma_data_direction dir, unsigned long attrs)
1007 {
1008 	const struct dma_map_ops *ops = get_dma_ops(dev);
1009 	struct scatterlist *s;
1010 	int i, j;
1011 
1012 	for_each_sg(sg, s, nents, i) {
1013 #ifdef CONFIG_NEED_SG_DMA_LENGTH
1014 		s->dma_length = s->length;
1015 #endif
1016 		s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
1017 						s->length, dir, attrs);
1018 		if (dma_mapping_error(dev, s->dma_address))
1019 			goto bad_mapping;
1020 	}
1021 	return nents;
1022 
1023  bad_mapping:
1024 	for_each_sg(sg, s, i, j)
1025 		ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
1026 	return 0;
1027 }
1028 
1029 /**
1030  * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1031  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1032  * @sg: list of buffers
1033  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1034  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1035  *
1036  * Unmap a set of streaming mode DMA translations.  Again, CPU access
1037  * rules concerning calls here are the same as for dma_unmap_single().
1038  */
1039 void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1040 		enum dma_data_direction dir, unsigned long attrs)
1041 {
1042 	const struct dma_map_ops *ops = get_dma_ops(dev);
1043 	struct scatterlist *s;
1044 
1045 	int i;
1046 
1047 	for_each_sg(sg, s, nents, i)
1048 		ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
1049 }
1050 
1051 /**
1052  * arm_dma_sync_sg_for_cpu
1053  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1054  * @sg: list of buffers
1055  * @nents: number of buffers to map (returned from dma_map_sg)
1056  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1057  */
1058 void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1059 			int nents, enum dma_data_direction dir)
1060 {
1061 	const struct dma_map_ops *ops = get_dma_ops(dev);
1062 	struct scatterlist *s;
1063 	int i;
1064 
1065 	for_each_sg(sg, s, nents, i)
1066 		ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1067 					 dir);
1068 }
1069 
1070 /**
1071  * arm_dma_sync_sg_for_device
1072  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1073  * @sg: list of buffers
1074  * @nents: number of buffers to map (returned from dma_map_sg)
1075  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1076  */
1077 void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1078 			int nents, enum dma_data_direction dir)
1079 {
1080 	const struct dma_map_ops *ops = get_dma_ops(dev);
1081 	struct scatterlist *s;
1082 	int i;
1083 
1084 	for_each_sg(sg, s, nents, i)
1085 		ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1086 					    dir);
1087 }
1088 
1089 /*
1090  * Return whether the given device DMA address mask can be supported
1091  * properly.  For example, if your device can only drive the low 24-bits
1092  * during bus mastering, then you would pass 0x00ffffff as the mask
1093  * to this function.
1094  */
1095 int arm_dma_supported(struct device *dev, u64 mask)
1096 {
1097 	return __dma_supported(dev, mask, false);
1098 }
1099 
1100 static const struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
1101 {
1102 	/*
1103 	 * When CONFIG_ARM_LPAE is set, physical address can extend above
1104 	 * 32-bits, which then can't be addressed by devices that only support
1105 	 * 32-bit DMA.
1106 	 * Use the generic dma-direct / swiotlb ops code in that case, as that
1107 	 * handles bounce buffering for us.
1108 	 */
1109 	if (IS_ENABLED(CONFIG_ARM_LPAE))
1110 		return NULL;
1111 	return coherent ? &arm_coherent_dma_ops : &arm_dma_ops;
1112 }
1113 
1114 #ifdef CONFIG_ARM_DMA_USE_IOMMU
1115 
1116 static int __dma_info_to_prot(enum dma_data_direction dir, unsigned long attrs)
1117 {
1118 	int prot = 0;
1119 
1120 	if (attrs & DMA_ATTR_PRIVILEGED)
1121 		prot |= IOMMU_PRIV;
1122 
1123 	switch (dir) {
1124 	case DMA_BIDIRECTIONAL:
1125 		return prot | IOMMU_READ | IOMMU_WRITE;
1126 	case DMA_TO_DEVICE:
1127 		return prot | IOMMU_READ;
1128 	case DMA_FROM_DEVICE:
1129 		return prot | IOMMU_WRITE;
1130 	default:
1131 		return prot;
1132 	}
1133 }
1134 
1135 /* IOMMU */
1136 
1137 static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1138 
1139 static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1140 				      size_t size)
1141 {
1142 	unsigned int order = get_order(size);
1143 	unsigned int align = 0;
1144 	unsigned int count, start;
1145 	size_t mapping_size = mapping->bits << PAGE_SHIFT;
1146 	unsigned long flags;
1147 	dma_addr_t iova;
1148 	int i;
1149 
1150 	if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1151 		order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1152 
1153 	count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1154 	align = (1 << order) - 1;
1155 
1156 	spin_lock_irqsave(&mapping->lock, flags);
1157 	for (i = 0; i < mapping->nr_bitmaps; i++) {
1158 		start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1159 				mapping->bits, 0, count, align);
1160 
1161 		if (start > mapping->bits)
1162 			continue;
1163 
1164 		bitmap_set(mapping->bitmaps[i], start, count);
1165 		break;
1166 	}
1167 
1168 	/*
1169 	 * No unused range found. Try to extend the existing mapping
1170 	 * and perform a second attempt to reserve an IO virtual
1171 	 * address range of size bytes.
1172 	 */
1173 	if (i == mapping->nr_bitmaps) {
1174 		if (extend_iommu_mapping(mapping)) {
1175 			spin_unlock_irqrestore(&mapping->lock, flags);
1176 			return DMA_MAPPING_ERROR;
1177 		}
1178 
1179 		start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1180 				mapping->bits, 0, count, align);
1181 
1182 		if (start > mapping->bits) {
1183 			spin_unlock_irqrestore(&mapping->lock, flags);
1184 			return DMA_MAPPING_ERROR;
1185 		}
1186 
1187 		bitmap_set(mapping->bitmaps[i], start, count);
1188 	}
1189 	spin_unlock_irqrestore(&mapping->lock, flags);
1190 
1191 	iova = mapping->base + (mapping_size * i);
1192 	iova += start << PAGE_SHIFT;
1193 
1194 	return iova;
1195 }
1196 
1197 static inline void __free_iova(struct dma_iommu_mapping *mapping,
1198 			       dma_addr_t addr, size_t size)
1199 {
1200 	unsigned int start, count;
1201 	size_t mapping_size = mapping->bits << PAGE_SHIFT;
1202 	unsigned long flags;
1203 	dma_addr_t bitmap_base;
1204 	u32 bitmap_index;
1205 
1206 	if (!size)
1207 		return;
1208 
1209 	bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size;
1210 	BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1211 
1212 	bitmap_base = mapping->base + mapping_size * bitmap_index;
1213 
1214 	start = (addr - bitmap_base) >>	PAGE_SHIFT;
1215 
1216 	if (addr + size > bitmap_base + mapping_size) {
1217 		/*
1218 		 * The address range to be freed reaches into the iova
1219 		 * range of the next bitmap. This should not happen as
1220 		 * we don't allow this in __alloc_iova (at the
1221 		 * moment).
1222 		 */
1223 		BUG();
1224 	} else
1225 		count = size >> PAGE_SHIFT;
1226 
1227 	spin_lock_irqsave(&mapping->lock, flags);
1228 	bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
1229 	spin_unlock_irqrestore(&mapping->lock, flags);
1230 }
1231 
1232 /* We'll try 2M, 1M, 64K, and finally 4K; array must end with 0! */
1233 static const int iommu_order_array[] = { 9, 8, 4, 0 };
1234 
1235 static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
1236 					  gfp_t gfp, unsigned long attrs,
1237 					  int coherent_flag)
1238 {
1239 	struct page **pages;
1240 	int count = size >> PAGE_SHIFT;
1241 	int array_size = count * sizeof(struct page *);
1242 	int i = 0;
1243 	int order_idx = 0;
1244 
1245 	if (array_size <= PAGE_SIZE)
1246 		pages = kzalloc(array_size, GFP_KERNEL);
1247 	else
1248 		pages = vzalloc(array_size);
1249 	if (!pages)
1250 		return NULL;
1251 
1252 	if (attrs & DMA_ATTR_FORCE_CONTIGUOUS)
1253 	{
1254 		unsigned long order = get_order(size);
1255 		struct page *page;
1256 
1257 		page = dma_alloc_from_contiguous(dev, count, order,
1258 						 gfp & __GFP_NOWARN);
1259 		if (!page)
1260 			goto error;
1261 
1262 		__dma_clear_buffer(page, size, coherent_flag);
1263 
1264 		for (i = 0; i < count; i++)
1265 			pages[i] = page + i;
1266 
1267 		return pages;
1268 	}
1269 
1270 	/* Go straight to 4K chunks if caller says it's OK. */
1271 	if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
1272 		order_idx = ARRAY_SIZE(iommu_order_array) - 1;
1273 
1274 	/*
1275 	 * IOMMU can map any pages, so himem can also be used here
1276 	 */
1277 	gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1278 
1279 	while (count) {
1280 		int j, order;
1281 
1282 		order = iommu_order_array[order_idx];
1283 
1284 		/* Drop down when we get small */
1285 		if (__fls(count) < order) {
1286 			order_idx++;
1287 			continue;
1288 		}
1289 
1290 		if (order) {
1291 			/* See if it's easy to allocate a high-order chunk */
1292 			pages[i] = alloc_pages(gfp | __GFP_NORETRY, order);
1293 
1294 			/* Go down a notch at first sign of pressure */
1295 			if (!pages[i]) {
1296 				order_idx++;
1297 				continue;
1298 			}
1299 		} else {
1300 			pages[i] = alloc_pages(gfp, 0);
1301 			if (!pages[i])
1302 				goto error;
1303 		}
1304 
1305 		if (order) {
1306 			split_page(pages[i], order);
1307 			j = 1 << order;
1308 			while (--j)
1309 				pages[i + j] = pages[i] + j;
1310 		}
1311 
1312 		__dma_clear_buffer(pages[i], PAGE_SIZE << order, coherent_flag);
1313 		i += 1 << order;
1314 		count -= 1 << order;
1315 	}
1316 
1317 	return pages;
1318 error:
1319 	while (i--)
1320 		if (pages[i])
1321 			__free_pages(pages[i], 0);
1322 	kvfree(pages);
1323 	return NULL;
1324 }
1325 
1326 static int __iommu_free_buffer(struct device *dev, struct page **pages,
1327 			       size_t size, unsigned long attrs)
1328 {
1329 	int count = size >> PAGE_SHIFT;
1330 	int i;
1331 
1332 	if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
1333 		dma_release_from_contiguous(dev, pages[0], count);
1334 	} else {
1335 		for (i = 0; i < count; i++)
1336 			if (pages[i])
1337 				__free_pages(pages[i], 0);
1338 	}
1339 
1340 	kvfree(pages);
1341 	return 0;
1342 }
1343 
1344 /*
1345  * Create a mapping in device IO address space for specified pages
1346  */
1347 static dma_addr_t
1348 __iommu_create_mapping(struct device *dev, struct page **pages, size_t size,
1349 		       unsigned long attrs)
1350 {
1351 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1352 	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1353 	dma_addr_t dma_addr, iova;
1354 	int i;
1355 
1356 	dma_addr = __alloc_iova(mapping, size);
1357 	if (dma_addr == DMA_MAPPING_ERROR)
1358 		return dma_addr;
1359 
1360 	iova = dma_addr;
1361 	for (i = 0; i < count; ) {
1362 		int ret;
1363 
1364 		unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1365 		phys_addr_t phys = page_to_phys(pages[i]);
1366 		unsigned int len, j;
1367 
1368 		for (j = i + 1; j < count; j++, next_pfn++)
1369 			if (page_to_pfn(pages[j]) != next_pfn)
1370 				break;
1371 
1372 		len = (j - i) << PAGE_SHIFT;
1373 		ret = iommu_map(mapping->domain, iova, phys, len,
1374 				__dma_info_to_prot(DMA_BIDIRECTIONAL, attrs));
1375 		if (ret < 0)
1376 			goto fail;
1377 		iova += len;
1378 		i = j;
1379 	}
1380 	return dma_addr;
1381 fail:
1382 	iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1383 	__free_iova(mapping, dma_addr, size);
1384 	return DMA_MAPPING_ERROR;
1385 }
1386 
1387 static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1388 {
1389 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1390 
1391 	/*
1392 	 * add optional in-page offset from iova to size and align
1393 	 * result to page size
1394 	 */
1395 	size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1396 	iova &= PAGE_MASK;
1397 
1398 	iommu_unmap(mapping->domain, iova, size);
1399 	__free_iova(mapping, iova, size);
1400 	return 0;
1401 }
1402 
1403 static struct page **__atomic_get_pages(void *addr)
1404 {
1405 	struct page *page;
1406 	phys_addr_t phys;
1407 
1408 	phys = gen_pool_virt_to_phys(atomic_pool, (unsigned long)addr);
1409 	page = phys_to_page(phys);
1410 
1411 	return (struct page **)page;
1412 }
1413 
1414 static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs)
1415 {
1416 	if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1417 		return __atomic_get_pages(cpu_addr);
1418 
1419 	if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
1420 		return cpu_addr;
1421 
1422 	return dma_common_find_pages(cpu_addr);
1423 }
1424 
1425 static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp,
1426 				  dma_addr_t *handle, int coherent_flag,
1427 				  unsigned long attrs)
1428 {
1429 	struct page *page;
1430 	void *addr;
1431 
1432 	if (coherent_flag  == COHERENT)
1433 		addr = __alloc_simple_buffer(dev, size, gfp, &page);
1434 	else
1435 		addr = __alloc_from_pool(size, &page);
1436 	if (!addr)
1437 		return NULL;
1438 
1439 	*handle = __iommu_create_mapping(dev, &page, size, attrs);
1440 	if (*handle == DMA_MAPPING_ERROR)
1441 		goto err_mapping;
1442 
1443 	return addr;
1444 
1445 err_mapping:
1446 	__free_from_pool(addr, size);
1447 	return NULL;
1448 }
1449 
1450 static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
1451 			dma_addr_t handle, size_t size, int coherent_flag)
1452 {
1453 	__iommu_remove_mapping(dev, handle, size);
1454 	if (coherent_flag == COHERENT)
1455 		__dma_free_buffer(virt_to_page(cpu_addr), size);
1456 	else
1457 		__free_from_pool(cpu_addr, size);
1458 }
1459 
1460 static void *__arm_iommu_alloc_attrs(struct device *dev, size_t size,
1461 	    dma_addr_t *handle, gfp_t gfp, unsigned long attrs,
1462 	    int coherent_flag)
1463 {
1464 	pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
1465 	struct page **pages;
1466 	void *addr = NULL;
1467 
1468 	*handle = DMA_MAPPING_ERROR;
1469 	size = PAGE_ALIGN(size);
1470 
1471 	if (coherent_flag  == COHERENT || !gfpflags_allow_blocking(gfp))
1472 		return __iommu_alloc_simple(dev, size, gfp, handle,
1473 					    coherent_flag, attrs);
1474 
1475 	/*
1476 	 * Following is a work-around (a.k.a. hack) to prevent pages
1477 	 * with __GFP_COMP being passed to split_page() which cannot
1478 	 * handle them.  The real problem is that this flag probably
1479 	 * should be 0 on ARM as it is not supported on this
1480 	 * platform; see CONFIG_HUGETLBFS.
1481 	 */
1482 	gfp &= ~(__GFP_COMP);
1483 
1484 	pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag);
1485 	if (!pages)
1486 		return NULL;
1487 
1488 	*handle = __iommu_create_mapping(dev, pages, size, attrs);
1489 	if (*handle == DMA_MAPPING_ERROR)
1490 		goto err_buffer;
1491 
1492 	if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
1493 		return pages;
1494 
1495 	addr = dma_common_pages_remap(pages, size, prot,
1496 				   __builtin_return_address(0));
1497 	if (!addr)
1498 		goto err_mapping;
1499 
1500 	return addr;
1501 
1502 err_mapping:
1503 	__iommu_remove_mapping(dev, *handle, size);
1504 err_buffer:
1505 	__iommu_free_buffer(dev, pages, size, attrs);
1506 	return NULL;
1507 }
1508 
1509 static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1510 	    dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
1511 {
1512 	return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, NORMAL);
1513 }
1514 
1515 static void *arm_coherent_iommu_alloc_attrs(struct device *dev, size_t size,
1516 		    dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
1517 {
1518 	return __arm_iommu_alloc_attrs(dev, size, handle, gfp, attrs, COHERENT);
1519 }
1520 
1521 static int __arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1522 		    void *cpu_addr, dma_addr_t dma_addr, size_t size,
1523 		    unsigned long attrs)
1524 {
1525 	struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1526 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1527 	int err;
1528 
1529 	if (!pages)
1530 		return -ENXIO;
1531 
1532 	if (vma->vm_pgoff >= nr_pages)
1533 		return -ENXIO;
1534 
1535 	err = vm_map_pages(vma, pages, nr_pages);
1536 	if (err)
1537 		pr_err("Remapping memory failed: %d\n", err);
1538 
1539 	return err;
1540 }
1541 static int arm_iommu_mmap_attrs(struct device *dev,
1542 		struct vm_area_struct *vma, void *cpu_addr,
1543 		dma_addr_t dma_addr, size_t size, unsigned long attrs)
1544 {
1545 	vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1546 
1547 	return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1548 }
1549 
1550 static int arm_coherent_iommu_mmap_attrs(struct device *dev,
1551 		struct vm_area_struct *vma, void *cpu_addr,
1552 		dma_addr_t dma_addr, size_t size, unsigned long attrs)
1553 {
1554 	return __arm_iommu_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, attrs);
1555 }
1556 
1557 /*
1558  * free a page as defined by the above mapping.
1559  * Must not be called with IRQs disabled.
1560  */
1561 void __arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1562 	dma_addr_t handle, unsigned long attrs, int coherent_flag)
1563 {
1564 	struct page **pages;
1565 	size = PAGE_ALIGN(size);
1566 
1567 	if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) {
1568 		__iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag);
1569 		return;
1570 	}
1571 
1572 	pages = __iommu_get_pages(cpu_addr, attrs);
1573 	if (!pages) {
1574 		WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1575 		return;
1576 	}
1577 
1578 	if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0)
1579 		dma_common_free_remap(cpu_addr, size);
1580 
1581 	__iommu_remove_mapping(dev, handle, size);
1582 	__iommu_free_buffer(dev, pages, size, attrs);
1583 }
1584 
1585 void arm_iommu_free_attrs(struct device *dev, size_t size,
1586 		    void *cpu_addr, dma_addr_t handle, unsigned long attrs)
1587 {
1588 	__arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, NORMAL);
1589 }
1590 
1591 void arm_coherent_iommu_free_attrs(struct device *dev, size_t size,
1592 		    void *cpu_addr, dma_addr_t handle, unsigned long attrs)
1593 {
1594 	__arm_iommu_free_attrs(dev, size, cpu_addr, handle, attrs, COHERENT);
1595 }
1596 
1597 static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1598 				 void *cpu_addr, dma_addr_t dma_addr,
1599 				 size_t size, unsigned long attrs)
1600 {
1601 	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1602 	struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1603 
1604 	if (!pages)
1605 		return -ENXIO;
1606 
1607 	return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1608 					 GFP_KERNEL);
1609 }
1610 
1611 /*
1612  * Map a part of the scatter-gather list into contiguous io address space
1613  */
1614 static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1615 			  size_t size, dma_addr_t *handle,
1616 			  enum dma_data_direction dir, unsigned long attrs,
1617 			  bool is_coherent)
1618 {
1619 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1620 	dma_addr_t iova, iova_base;
1621 	int ret = 0;
1622 	unsigned int count;
1623 	struct scatterlist *s;
1624 	int prot;
1625 
1626 	size = PAGE_ALIGN(size);
1627 	*handle = DMA_MAPPING_ERROR;
1628 
1629 	iova_base = iova = __alloc_iova(mapping, size);
1630 	if (iova == DMA_MAPPING_ERROR)
1631 		return -ENOMEM;
1632 
1633 	for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
1634 		phys_addr_t phys = page_to_phys(sg_page(s));
1635 		unsigned int len = PAGE_ALIGN(s->offset + s->length);
1636 
1637 		if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
1638 			__dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1639 
1640 		prot = __dma_info_to_prot(dir, attrs);
1641 
1642 		ret = iommu_map(mapping->domain, iova, phys, len, prot);
1643 		if (ret < 0)
1644 			goto fail;
1645 		count += len >> PAGE_SHIFT;
1646 		iova += len;
1647 	}
1648 	*handle = iova_base;
1649 
1650 	return 0;
1651 fail:
1652 	iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1653 	__free_iova(mapping, iova_base, size);
1654 	return ret;
1655 }
1656 
1657 static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1658 		     enum dma_data_direction dir, unsigned long attrs,
1659 		     bool is_coherent)
1660 {
1661 	struct scatterlist *s = sg, *dma = sg, *start = sg;
1662 	int i, count = 0;
1663 	unsigned int offset = s->offset;
1664 	unsigned int size = s->offset + s->length;
1665 	unsigned int max = dma_get_max_seg_size(dev);
1666 
1667 	for (i = 1; i < nents; i++) {
1668 		s = sg_next(s);
1669 
1670 		s->dma_address = DMA_MAPPING_ERROR;
1671 		s->dma_length = 0;
1672 
1673 		if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1674 			if (__map_sg_chunk(dev, start, size, &dma->dma_address,
1675 			    dir, attrs, is_coherent) < 0)
1676 				goto bad_mapping;
1677 
1678 			dma->dma_address += offset;
1679 			dma->dma_length = size - offset;
1680 
1681 			size = offset = s->offset;
1682 			start = s;
1683 			dma = sg_next(dma);
1684 			count += 1;
1685 		}
1686 		size += s->length;
1687 	}
1688 	if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1689 		is_coherent) < 0)
1690 		goto bad_mapping;
1691 
1692 	dma->dma_address += offset;
1693 	dma->dma_length = size - offset;
1694 
1695 	return count+1;
1696 
1697 bad_mapping:
1698 	for_each_sg(sg, s, count, i)
1699 		__iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1700 	return 0;
1701 }
1702 
1703 /**
1704  * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1705  * @dev: valid struct device pointer
1706  * @sg: list of buffers
1707  * @nents: number of buffers to map
1708  * @dir: DMA transfer direction
1709  *
1710  * Map a set of i/o coherent buffers described by scatterlist in streaming
1711  * mode for DMA. The scatter gather list elements are merged together (if
1712  * possible) and tagged with the appropriate dma address and length. They are
1713  * obtained via sg_dma_{address,length}.
1714  */
1715 int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1716 		int nents, enum dma_data_direction dir, unsigned long attrs)
1717 {
1718 	return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1719 }
1720 
1721 /**
1722  * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1723  * @dev: valid struct device pointer
1724  * @sg: list of buffers
1725  * @nents: number of buffers to map
1726  * @dir: DMA transfer direction
1727  *
1728  * Map a set of buffers described by scatterlist in streaming mode for DMA.
1729  * The scatter gather list elements are merged together (if possible) and
1730  * tagged with the appropriate dma address and length. They are obtained via
1731  * sg_dma_{address,length}.
1732  */
1733 int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1734 		int nents, enum dma_data_direction dir, unsigned long attrs)
1735 {
1736 	return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1737 }
1738 
1739 static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1740 		int nents, enum dma_data_direction dir,
1741 		unsigned long attrs, bool is_coherent)
1742 {
1743 	struct scatterlist *s;
1744 	int i;
1745 
1746 	for_each_sg(sg, s, nents, i) {
1747 		if (sg_dma_len(s))
1748 			__iommu_remove_mapping(dev, sg_dma_address(s),
1749 					       sg_dma_len(s));
1750 		if (!is_coherent && (attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
1751 			__dma_page_dev_to_cpu(sg_page(s), s->offset,
1752 					      s->length, dir);
1753 	}
1754 }
1755 
1756 /**
1757  * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1758  * @dev: valid struct device pointer
1759  * @sg: list of buffers
1760  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1761  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1762  *
1763  * Unmap a set of streaming mode DMA translations.  Again, CPU access
1764  * rules concerning calls here are the same as for dma_unmap_single().
1765  */
1766 void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1767 		int nents, enum dma_data_direction dir,
1768 		unsigned long attrs)
1769 {
1770 	__iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1771 }
1772 
1773 /**
1774  * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1775  * @dev: valid struct device pointer
1776  * @sg: list of buffers
1777  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1778  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1779  *
1780  * Unmap a set of streaming mode DMA translations.  Again, CPU access
1781  * rules concerning calls here are the same as for dma_unmap_single().
1782  */
1783 void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1784 			enum dma_data_direction dir,
1785 			unsigned long attrs)
1786 {
1787 	__iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
1788 }
1789 
1790 /**
1791  * arm_iommu_sync_sg_for_cpu
1792  * @dev: valid struct device pointer
1793  * @sg: list of buffers
1794  * @nents: number of buffers to map (returned from dma_map_sg)
1795  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1796  */
1797 void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1798 			int nents, enum dma_data_direction dir)
1799 {
1800 	struct scatterlist *s;
1801 	int i;
1802 
1803 	for_each_sg(sg, s, nents, i)
1804 		__dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
1805 
1806 }
1807 
1808 /**
1809  * arm_iommu_sync_sg_for_device
1810  * @dev: valid struct device pointer
1811  * @sg: list of buffers
1812  * @nents: number of buffers to map (returned from dma_map_sg)
1813  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1814  */
1815 void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1816 			int nents, enum dma_data_direction dir)
1817 {
1818 	struct scatterlist *s;
1819 	int i;
1820 
1821 	for_each_sg(sg, s, nents, i)
1822 		__dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1823 }
1824 
1825 
1826 /**
1827  * arm_coherent_iommu_map_page
1828  * @dev: valid struct device pointer
1829  * @page: page that buffer resides in
1830  * @offset: offset into page for start of buffer
1831  * @size: size of buffer to map
1832  * @dir: DMA transfer direction
1833  *
1834  * Coherent IOMMU aware version of arm_dma_map_page()
1835  */
1836 static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1837 	     unsigned long offset, size_t size, enum dma_data_direction dir,
1838 	     unsigned long attrs)
1839 {
1840 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1841 	dma_addr_t dma_addr;
1842 	int ret, prot, len = PAGE_ALIGN(size + offset);
1843 
1844 	dma_addr = __alloc_iova(mapping, len);
1845 	if (dma_addr == DMA_MAPPING_ERROR)
1846 		return dma_addr;
1847 
1848 	prot = __dma_info_to_prot(dir, attrs);
1849 
1850 	ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
1851 	if (ret < 0)
1852 		goto fail;
1853 
1854 	return dma_addr + offset;
1855 fail:
1856 	__free_iova(mapping, dma_addr, len);
1857 	return DMA_MAPPING_ERROR;
1858 }
1859 
1860 /**
1861  * arm_iommu_map_page
1862  * @dev: valid struct device pointer
1863  * @page: page that buffer resides in
1864  * @offset: offset into page for start of buffer
1865  * @size: size of buffer to map
1866  * @dir: DMA transfer direction
1867  *
1868  * IOMMU aware version of arm_dma_map_page()
1869  */
1870 static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1871 	     unsigned long offset, size_t size, enum dma_data_direction dir,
1872 	     unsigned long attrs)
1873 {
1874 	if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
1875 		__dma_page_cpu_to_dev(page, offset, size, dir);
1876 
1877 	return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1878 }
1879 
1880 /**
1881  * arm_coherent_iommu_unmap_page
1882  * @dev: valid struct device pointer
1883  * @handle: DMA address of buffer
1884  * @size: size of buffer (same as passed to dma_map_page)
1885  * @dir: DMA transfer direction (same as passed to dma_map_page)
1886  *
1887  * Coherent IOMMU aware version of arm_dma_unmap_page()
1888  */
1889 static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1890 		size_t size, enum dma_data_direction dir, unsigned long attrs)
1891 {
1892 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1893 	dma_addr_t iova = handle & PAGE_MASK;
1894 	int offset = handle & ~PAGE_MASK;
1895 	int len = PAGE_ALIGN(size + offset);
1896 
1897 	if (!iova)
1898 		return;
1899 
1900 	iommu_unmap(mapping->domain, iova, len);
1901 	__free_iova(mapping, iova, len);
1902 }
1903 
1904 /**
1905  * arm_iommu_unmap_page
1906  * @dev: valid struct device pointer
1907  * @handle: DMA address of buffer
1908  * @size: size of buffer (same as passed to dma_map_page)
1909  * @dir: DMA transfer direction (same as passed to dma_map_page)
1910  *
1911  * IOMMU aware version of arm_dma_unmap_page()
1912  */
1913 static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1914 		size_t size, enum dma_data_direction dir, unsigned long attrs)
1915 {
1916 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1917 	dma_addr_t iova = handle & PAGE_MASK;
1918 	struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1919 	int offset = handle & ~PAGE_MASK;
1920 	int len = PAGE_ALIGN(size + offset);
1921 
1922 	if (!iova)
1923 		return;
1924 
1925 	if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
1926 		__dma_page_dev_to_cpu(page, offset, size, dir);
1927 
1928 	iommu_unmap(mapping->domain, iova, len);
1929 	__free_iova(mapping, iova, len);
1930 }
1931 
1932 /**
1933  * arm_iommu_map_resource - map a device resource for DMA
1934  * @dev: valid struct device pointer
1935  * @phys_addr: physical address of resource
1936  * @size: size of resource to map
1937  * @dir: DMA transfer direction
1938  */
1939 static dma_addr_t arm_iommu_map_resource(struct device *dev,
1940 		phys_addr_t phys_addr, size_t size,
1941 		enum dma_data_direction dir, unsigned long attrs)
1942 {
1943 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1944 	dma_addr_t dma_addr;
1945 	int ret, prot;
1946 	phys_addr_t addr = phys_addr & PAGE_MASK;
1947 	unsigned int offset = phys_addr & ~PAGE_MASK;
1948 	size_t len = PAGE_ALIGN(size + offset);
1949 
1950 	dma_addr = __alloc_iova(mapping, len);
1951 	if (dma_addr == DMA_MAPPING_ERROR)
1952 		return dma_addr;
1953 
1954 	prot = __dma_info_to_prot(dir, attrs) | IOMMU_MMIO;
1955 
1956 	ret = iommu_map(mapping->domain, dma_addr, addr, len, prot);
1957 	if (ret < 0)
1958 		goto fail;
1959 
1960 	return dma_addr + offset;
1961 fail:
1962 	__free_iova(mapping, dma_addr, len);
1963 	return DMA_MAPPING_ERROR;
1964 }
1965 
1966 /**
1967  * arm_iommu_unmap_resource - unmap a device DMA resource
1968  * @dev: valid struct device pointer
1969  * @dma_handle: DMA address to resource
1970  * @size: size of resource to map
1971  * @dir: DMA transfer direction
1972  */
1973 static void arm_iommu_unmap_resource(struct device *dev, dma_addr_t dma_handle,
1974 		size_t size, enum dma_data_direction dir,
1975 		unsigned long attrs)
1976 {
1977 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1978 	dma_addr_t iova = dma_handle & PAGE_MASK;
1979 	unsigned int offset = dma_handle & ~PAGE_MASK;
1980 	size_t len = PAGE_ALIGN(size + offset);
1981 
1982 	if (!iova)
1983 		return;
1984 
1985 	iommu_unmap(mapping->domain, iova, len);
1986 	__free_iova(mapping, iova, len);
1987 }
1988 
1989 static void arm_iommu_sync_single_for_cpu(struct device *dev,
1990 		dma_addr_t handle, size_t size, enum dma_data_direction dir)
1991 {
1992 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
1993 	dma_addr_t iova = handle & PAGE_MASK;
1994 	struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1995 	unsigned int offset = handle & ~PAGE_MASK;
1996 
1997 	if (!iova)
1998 		return;
1999 
2000 	__dma_page_dev_to_cpu(page, offset, size, dir);
2001 }
2002 
2003 static void arm_iommu_sync_single_for_device(struct device *dev,
2004 		dma_addr_t handle, size_t size, enum dma_data_direction dir)
2005 {
2006 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2007 	dma_addr_t iova = handle & PAGE_MASK;
2008 	struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
2009 	unsigned int offset = handle & ~PAGE_MASK;
2010 
2011 	if (!iova)
2012 		return;
2013 
2014 	__dma_page_cpu_to_dev(page, offset, size, dir);
2015 }
2016 
2017 const struct dma_map_ops iommu_ops = {
2018 	.alloc		= arm_iommu_alloc_attrs,
2019 	.free		= arm_iommu_free_attrs,
2020 	.mmap		= arm_iommu_mmap_attrs,
2021 	.get_sgtable	= arm_iommu_get_sgtable,
2022 
2023 	.map_page		= arm_iommu_map_page,
2024 	.unmap_page		= arm_iommu_unmap_page,
2025 	.sync_single_for_cpu	= arm_iommu_sync_single_for_cpu,
2026 	.sync_single_for_device	= arm_iommu_sync_single_for_device,
2027 
2028 	.map_sg			= arm_iommu_map_sg,
2029 	.unmap_sg		= arm_iommu_unmap_sg,
2030 	.sync_sg_for_cpu	= arm_iommu_sync_sg_for_cpu,
2031 	.sync_sg_for_device	= arm_iommu_sync_sg_for_device,
2032 
2033 	.map_resource		= arm_iommu_map_resource,
2034 	.unmap_resource		= arm_iommu_unmap_resource,
2035 
2036 	.dma_supported		= arm_dma_supported,
2037 };
2038 
2039 const struct dma_map_ops iommu_coherent_ops = {
2040 	.alloc		= arm_coherent_iommu_alloc_attrs,
2041 	.free		= arm_coherent_iommu_free_attrs,
2042 	.mmap		= arm_coherent_iommu_mmap_attrs,
2043 	.get_sgtable	= arm_iommu_get_sgtable,
2044 
2045 	.map_page	= arm_coherent_iommu_map_page,
2046 	.unmap_page	= arm_coherent_iommu_unmap_page,
2047 
2048 	.map_sg		= arm_coherent_iommu_map_sg,
2049 	.unmap_sg	= arm_coherent_iommu_unmap_sg,
2050 
2051 	.map_resource	= arm_iommu_map_resource,
2052 	.unmap_resource	= arm_iommu_unmap_resource,
2053 
2054 	.dma_supported		= arm_dma_supported,
2055 };
2056 
2057 /**
2058  * arm_iommu_create_mapping
2059  * @bus: pointer to the bus holding the client device (for IOMMU calls)
2060  * @base: start address of the valid IO address space
2061  * @size: maximum size of the valid IO address space
2062  *
2063  * Creates a mapping structure which holds information about used/unused
2064  * IO address ranges, which is required to perform memory allocation and
2065  * mapping with IOMMU aware functions.
2066  *
2067  * The client device need to be attached to the mapping with
2068  * arm_iommu_attach_device function.
2069  */
2070 struct dma_iommu_mapping *
2071 arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size)
2072 {
2073 	unsigned int bits = size >> PAGE_SHIFT;
2074 	unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
2075 	struct dma_iommu_mapping *mapping;
2076 	int extensions = 1;
2077 	int err = -ENOMEM;
2078 
2079 	/* currently only 32-bit DMA address space is supported */
2080 	if (size > DMA_BIT_MASK(32) + 1)
2081 		return ERR_PTR(-ERANGE);
2082 
2083 	if (!bitmap_size)
2084 		return ERR_PTR(-EINVAL);
2085 
2086 	if (bitmap_size > PAGE_SIZE) {
2087 		extensions = bitmap_size / PAGE_SIZE;
2088 		bitmap_size = PAGE_SIZE;
2089 	}
2090 
2091 	mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
2092 	if (!mapping)
2093 		goto err;
2094 
2095 	mapping->bitmap_size = bitmap_size;
2096 	mapping->bitmaps = kcalloc(extensions, sizeof(unsigned long *),
2097 				   GFP_KERNEL);
2098 	if (!mapping->bitmaps)
2099 		goto err2;
2100 
2101 	mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
2102 	if (!mapping->bitmaps[0])
2103 		goto err3;
2104 
2105 	mapping->nr_bitmaps = 1;
2106 	mapping->extensions = extensions;
2107 	mapping->base = base;
2108 	mapping->bits = BITS_PER_BYTE * bitmap_size;
2109 
2110 	spin_lock_init(&mapping->lock);
2111 
2112 	mapping->domain = iommu_domain_alloc(bus);
2113 	if (!mapping->domain)
2114 		goto err4;
2115 
2116 	kref_init(&mapping->kref);
2117 	return mapping;
2118 err4:
2119 	kfree(mapping->bitmaps[0]);
2120 err3:
2121 	kfree(mapping->bitmaps);
2122 err2:
2123 	kfree(mapping);
2124 err:
2125 	return ERR_PTR(err);
2126 }
2127 EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
2128 
2129 static void release_iommu_mapping(struct kref *kref)
2130 {
2131 	int i;
2132 	struct dma_iommu_mapping *mapping =
2133 		container_of(kref, struct dma_iommu_mapping, kref);
2134 
2135 	iommu_domain_free(mapping->domain);
2136 	for (i = 0; i < mapping->nr_bitmaps; i++)
2137 		kfree(mapping->bitmaps[i]);
2138 	kfree(mapping->bitmaps);
2139 	kfree(mapping);
2140 }
2141 
2142 static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2143 {
2144 	int next_bitmap;
2145 
2146 	if (mapping->nr_bitmaps >= mapping->extensions)
2147 		return -EINVAL;
2148 
2149 	next_bitmap = mapping->nr_bitmaps;
2150 	mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2151 						GFP_ATOMIC);
2152 	if (!mapping->bitmaps[next_bitmap])
2153 		return -ENOMEM;
2154 
2155 	mapping->nr_bitmaps++;
2156 
2157 	return 0;
2158 }
2159 
2160 void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
2161 {
2162 	if (mapping)
2163 		kref_put(&mapping->kref, release_iommu_mapping);
2164 }
2165 EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
2166 
2167 static int __arm_iommu_attach_device(struct device *dev,
2168 				     struct dma_iommu_mapping *mapping)
2169 {
2170 	int err;
2171 
2172 	err = iommu_attach_device(mapping->domain, dev);
2173 	if (err)
2174 		return err;
2175 
2176 	kref_get(&mapping->kref);
2177 	to_dma_iommu_mapping(dev) = mapping;
2178 
2179 	pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
2180 	return 0;
2181 }
2182 
2183 /**
2184  * arm_iommu_attach_device
2185  * @dev: valid struct device pointer
2186  * @mapping: io address space mapping structure (returned from
2187  *	arm_iommu_create_mapping)
2188  *
2189  * Attaches specified io address space mapping to the provided device.
2190  * This replaces the dma operations (dma_map_ops pointer) with the
2191  * IOMMU aware version.
2192  *
2193  * More than one client might be attached to the same io address space
2194  * mapping.
2195  */
2196 int arm_iommu_attach_device(struct device *dev,
2197 			    struct dma_iommu_mapping *mapping)
2198 {
2199 	int err;
2200 
2201 	err = __arm_iommu_attach_device(dev, mapping);
2202 	if (err)
2203 		return err;
2204 
2205 	set_dma_ops(dev, &iommu_ops);
2206 	return 0;
2207 }
2208 EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
2209 
2210 /**
2211  * arm_iommu_detach_device
2212  * @dev: valid struct device pointer
2213  *
2214  * Detaches the provided device from a previously attached map.
2215  * This overwrites the dma_ops pointer with appropriate non-IOMMU ops.
2216  */
2217 void arm_iommu_detach_device(struct device *dev)
2218 {
2219 	struct dma_iommu_mapping *mapping;
2220 
2221 	mapping = to_dma_iommu_mapping(dev);
2222 	if (!mapping) {
2223 		dev_warn(dev, "Not attached\n");
2224 		return;
2225 	}
2226 
2227 	iommu_detach_device(mapping->domain, dev);
2228 	kref_put(&mapping->kref, release_iommu_mapping);
2229 	to_dma_iommu_mapping(dev) = NULL;
2230 	set_dma_ops(dev, arm_get_dma_map_ops(dev->archdata.dma_coherent));
2231 
2232 	pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
2233 }
2234 EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
2235 
2236 static const struct dma_map_ops *arm_get_iommu_dma_map_ops(bool coherent)
2237 {
2238 	return coherent ? &iommu_coherent_ops : &iommu_ops;
2239 }
2240 
2241 static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
2242 				    const struct iommu_ops *iommu)
2243 {
2244 	struct dma_iommu_mapping *mapping;
2245 
2246 	if (!iommu)
2247 		return false;
2248 
2249 	mapping = arm_iommu_create_mapping(dev->bus, dma_base, size);
2250 	if (IS_ERR(mapping)) {
2251 		pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
2252 				size, dev_name(dev));
2253 		return false;
2254 	}
2255 
2256 	if (__arm_iommu_attach_device(dev, mapping)) {
2257 		pr_warn("Failed to attached device %s to IOMMU_mapping\n",
2258 				dev_name(dev));
2259 		arm_iommu_release_mapping(mapping);
2260 		return false;
2261 	}
2262 
2263 	return true;
2264 }
2265 
2266 static void arm_teardown_iommu_dma_ops(struct device *dev)
2267 {
2268 	struct dma_iommu_mapping *mapping = to_dma_iommu_mapping(dev);
2269 
2270 	if (!mapping)
2271 		return;
2272 
2273 	arm_iommu_detach_device(dev);
2274 	arm_iommu_release_mapping(mapping);
2275 }
2276 
2277 #else
2278 
2279 static bool arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size,
2280 				    const struct iommu_ops *iommu)
2281 {
2282 	return false;
2283 }
2284 
2285 static void arm_teardown_iommu_dma_ops(struct device *dev) { }
2286 
2287 #define arm_get_iommu_dma_map_ops arm_get_dma_map_ops
2288 
2289 #endif	/* CONFIG_ARM_DMA_USE_IOMMU */
2290 
2291 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
2292 			const struct iommu_ops *iommu, bool coherent)
2293 {
2294 	const struct dma_map_ops *dma_ops;
2295 
2296 	dev->archdata.dma_coherent = coherent;
2297 #ifdef CONFIG_SWIOTLB
2298 	dev->dma_coherent = coherent;
2299 #endif
2300 
2301 	/*
2302 	 * Don't override the dma_ops if they have already been set. Ideally
2303 	 * this should be the only location where dma_ops are set, remove this
2304 	 * check when all other callers of set_dma_ops will have disappeared.
2305 	 */
2306 	if (dev->dma_ops)
2307 		return;
2308 
2309 	if (arm_setup_iommu_dma_ops(dev, dma_base, size, iommu))
2310 		dma_ops = arm_get_iommu_dma_map_ops(coherent);
2311 	else
2312 		dma_ops = arm_get_dma_map_ops(coherent);
2313 
2314 	set_dma_ops(dev, dma_ops);
2315 
2316 #ifdef CONFIG_XEN
2317 	if (xen_initial_domain())
2318 		dev->dma_ops = xen_dma_ops;
2319 #endif
2320 	dev->archdata.dma_ops_setup = true;
2321 }
2322 
2323 void arch_teardown_dma_ops(struct device *dev)
2324 {
2325 	if (!dev->archdata.dma_ops_setup)
2326 		return;
2327 
2328 	arm_teardown_iommu_dma_ops(dev);
2329 	/* Let arch_setup_dma_ops() start again from scratch upon re-probe */
2330 	set_dma_ops(dev, NULL);
2331 }
2332 
2333 #ifdef CONFIG_SWIOTLB
2334 void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
2335 		size_t size, enum dma_data_direction dir)
2336 {
2337 	__dma_page_cpu_to_dev(phys_to_page(paddr), paddr & (PAGE_SIZE - 1),
2338 			      size, dir);
2339 }
2340 
2341 void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
2342 		size_t size, enum dma_data_direction dir)
2343 {
2344 	__dma_page_dev_to_cpu(phys_to_page(paddr), paddr & (PAGE_SIZE - 1),
2345 			      size, dir);
2346 }
2347 
2348 long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
2349 		dma_addr_t dma_addr)
2350 {
2351 	return dma_to_pfn(dev, dma_addr);
2352 }
2353 
2354 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
2355 		gfp_t gfp, unsigned long attrs)
2356 {
2357 	return __dma_alloc(dev, size, dma_handle, gfp,
2358 			   __get_dma_pgprot(attrs, PAGE_KERNEL), false,
2359 			   attrs, __builtin_return_address(0));
2360 }
2361 
2362 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
2363 		dma_addr_t dma_handle, unsigned long attrs)
2364 {
2365 	__arm_dma_free(dev, size, cpu_addr, dma_handle, attrs, false);
2366 }
2367 #endif /* CONFIG_SWIOTLB */
2368