xref: /openbmc/linux/arch/arm/mm/dma-mapping.c (revision c4ee0af3)
1 /*
2  *  linux/arch/arm/mm/dma-mapping.c
3  *
4  *  Copyright (C) 2000-2004 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  DMA uncached mapping support.
11  */
12 #include <linux/bootmem.h>
13 #include <linux/module.h>
14 #include <linux/mm.h>
15 #include <linux/gfp.h>
16 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/dma-contiguous.h>
22 #include <linux/highmem.h>
23 #include <linux/memblock.h>
24 #include <linux/slab.h>
25 #include <linux/iommu.h>
26 #include <linux/io.h>
27 #include <linux/vmalloc.h>
28 #include <linux/sizes.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 "mm.h"
41 
42 /*
43  * The DMA API is built upon the notion of "buffer ownership".  A buffer
44  * is either exclusively owned by the CPU (and therefore may be accessed
45  * by it) or exclusively owned by the DMA device.  These helper functions
46  * represent the transitions between these two ownership states.
47  *
48  * Note, however, that on later ARMs, this notion does not work due to
49  * speculative prefetches.  We model our approach on the assumption that
50  * the CPU does do speculative prefetches, which means we clean caches
51  * before transfers and delay cache invalidation until transfer completion.
52  *
53  */
54 static void __dma_page_cpu_to_dev(struct page *, unsigned long,
55 		size_t, enum dma_data_direction);
56 static void __dma_page_dev_to_cpu(struct page *, unsigned long,
57 		size_t, enum dma_data_direction);
58 
59 /**
60  * arm_dma_map_page - map a portion of a page for streaming DMA
61  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
62  * @page: page that buffer resides in
63  * @offset: offset into page for start of buffer
64  * @size: size of buffer to map
65  * @dir: DMA transfer direction
66  *
67  * Ensure that any data held in the cache is appropriately discarded
68  * or written back.
69  *
70  * The device owns this memory once this call has completed.  The CPU
71  * can regain ownership by calling dma_unmap_page().
72  */
73 static dma_addr_t arm_dma_map_page(struct device *dev, struct page *page,
74 	     unsigned long offset, size_t size, enum dma_data_direction dir,
75 	     struct dma_attrs *attrs)
76 {
77 	if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
78 		__dma_page_cpu_to_dev(page, offset, size, dir);
79 	return pfn_to_dma(dev, page_to_pfn(page)) + offset;
80 }
81 
82 static dma_addr_t arm_coherent_dma_map_page(struct device *dev, struct page *page,
83 	     unsigned long offset, size_t size, enum dma_data_direction dir,
84 	     struct dma_attrs *attrs)
85 {
86 	return pfn_to_dma(dev, page_to_pfn(page)) + offset;
87 }
88 
89 /**
90  * arm_dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
91  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
92  * @handle: DMA address of buffer
93  * @size: size of buffer (same as passed to dma_map_page)
94  * @dir: DMA transfer direction (same as passed to dma_map_page)
95  *
96  * Unmap a page streaming mode DMA translation.  The handle and size
97  * must match what was provided in the previous dma_map_page() call.
98  * All other usages are undefined.
99  *
100  * After this call, reads by the CPU to the buffer are guaranteed to see
101  * whatever the device wrote there.
102  */
103 static void arm_dma_unmap_page(struct device *dev, dma_addr_t handle,
104 		size_t size, enum dma_data_direction dir,
105 		struct dma_attrs *attrs)
106 {
107 	if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
108 		__dma_page_dev_to_cpu(pfn_to_page(dma_to_pfn(dev, handle)),
109 				      handle & ~PAGE_MASK, size, dir);
110 }
111 
112 static void arm_dma_sync_single_for_cpu(struct device *dev,
113 		dma_addr_t handle, size_t size, enum dma_data_direction dir)
114 {
115 	unsigned int offset = handle & (PAGE_SIZE - 1);
116 	struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
117 	__dma_page_dev_to_cpu(page, offset, size, dir);
118 }
119 
120 static void arm_dma_sync_single_for_device(struct device *dev,
121 		dma_addr_t handle, size_t size, enum dma_data_direction dir)
122 {
123 	unsigned int offset = handle & (PAGE_SIZE - 1);
124 	struct page *page = pfn_to_page(dma_to_pfn(dev, handle-offset));
125 	__dma_page_cpu_to_dev(page, offset, size, dir);
126 }
127 
128 struct dma_map_ops arm_dma_ops = {
129 	.alloc			= arm_dma_alloc,
130 	.free			= arm_dma_free,
131 	.mmap			= arm_dma_mmap,
132 	.get_sgtable		= arm_dma_get_sgtable,
133 	.map_page		= arm_dma_map_page,
134 	.unmap_page		= arm_dma_unmap_page,
135 	.map_sg			= arm_dma_map_sg,
136 	.unmap_sg		= arm_dma_unmap_sg,
137 	.sync_single_for_cpu	= arm_dma_sync_single_for_cpu,
138 	.sync_single_for_device	= arm_dma_sync_single_for_device,
139 	.sync_sg_for_cpu	= arm_dma_sync_sg_for_cpu,
140 	.sync_sg_for_device	= arm_dma_sync_sg_for_device,
141 	.set_dma_mask		= arm_dma_set_mask,
142 };
143 EXPORT_SYMBOL(arm_dma_ops);
144 
145 static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
146 	dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs);
147 static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
148 				  dma_addr_t handle, struct dma_attrs *attrs);
149 
150 struct dma_map_ops arm_coherent_dma_ops = {
151 	.alloc			= arm_coherent_dma_alloc,
152 	.free			= arm_coherent_dma_free,
153 	.mmap			= arm_dma_mmap,
154 	.get_sgtable		= arm_dma_get_sgtable,
155 	.map_page		= arm_coherent_dma_map_page,
156 	.map_sg			= arm_dma_map_sg,
157 	.set_dma_mask		= arm_dma_set_mask,
158 };
159 EXPORT_SYMBOL(arm_coherent_dma_ops);
160 
161 static int __dma_supported(struct device *dev, u64 mask, bool warn)
162 {
163 	unsigned long max_dma_pfn;
164 
165 	/*
166 	 * If the mask allows for more memory than we can address,
167 	 * and we actually have that much memory, then we must
168 	 * indicate that DMA to this device is not supported.
169 	 */
170 	if (sizeof(mask) != sizeof(dma_addr_t) &&
171 	    mask > (dma_addr_t)~0 &&
172 	    dma_to_pfn(dev, ~0) < max_pfn) {
173 		if (warn) {
174 			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
175 				 mask);
176 			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
177 		}
178 		return 0;
179 	}
180 
181 	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
182 
183 	/*
184 	 * Translate the device's DMA mask to a PFN limit.  This
185 	 * PFN number includes the page which we can DMA to.
186 	 */
187 	if (dma_to_pfn(dev, mask) < max_dma_pfn) {
188 		if (warn)
189 			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",
190 				 mask,
191 				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
192 				 max_dma_pfn + 1);
193 		return 0;
194 	}
195 
196 	return 1;
197 }
198 
199 static u64 get_coherent_dma_mask(struct device *dev)
200 {
201 	u64 mask = (u64)DMA_BIT_MASK(32);
202 
203 	if (dev) {
204 		mask = dev->coherent_dma_mask;
205 
206 		/*
207 		 * Sanity check the DMA mask - it must be non-zero, and
208 		 * must be able to be satisfied by a DMA allocation.
209 		 */
210 		if (mask == 0) {
211 			dev_warn(dev, "coherent DMA mask is unset\n");
212 			return 0;
213 		}
214 
215 		if (!__dma_supported(dev, mask, true))
216 			return 0;
217 	}
218 
219 	return mask;
220 }
221 
222 static void __dma_clear_buffer(struct page *page, size_t size)
223 {
224 	/*
225 	 * Ensure that the allocated pages are zeroed, and that any data
226 	 * lurking in the kernel direct-mapped region is invalidated.
227 	 */
228 	if (PageHighMem(page)) {
229 		phys_addr_t base = __pfn_to_phys(page_to_pfn(page));
230 		phys_addr_t end = base + size;
231 		while (size > 0) {
232 			void *ptr = kmap_atomic(page);
233 			memset(ptr, 0, PAGE_SIZE);
234 			dmac_flush_range(ptr, ptr + PAGE_SIZE);
235 			kunmap_atomic(ptr);
236 			page++;
237 			size -= PAGE_SIZE;
238 		}
239 		outer_flush_range(base, end);
240 	} else {
241 		void *ptr = page_address(page);
242 		memset(ptr, 0, size);
243 		dmac_flush_range(ptr, ptr + size);
244 		outer_flush_range(__pa(ptr), __pa(ptr) + size);
245 	}
246 }
247 
248 /*
249  * Allocate a DMA buffer for 'dev' of size 'size' using the
250  * specified gfp mask.  Note that 'size' must be page aligned.
251  */
252 static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp)
253 {
254 	unsigned long order = get_order(size);
255 	struct page *page, *p, *e;
256 
257 	page = alloc_pages(gfp, order);
258 	if (!page)
259 		return NULL;
260 
261 	/*
262 	 * Now split the huge page and free the excess pages
263 	 */
264 	split_page(page, order);
265 	for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++)
266 		__free_page(p);
267 
268 	__dma_clear_buffer(page, size);
269 
270 	return page;
271 }
272 
273 /*
274  * Free a DMA buffer.  'size' must be page aligned.
275  */
276 static void __dma_free_buffer(struct page *page, size_t size)
277 {
278 	struct page *e = page + (size >> PAGE_SHIFT);
279 
280 	while (page < e) {
281 		__free_page(page);
282 		page++;
283 	}
284 }
285 
286 #ifdef CONFIG_MMU
287 #ifdef CONFIG_HUGETLB_PAGE
288 #warning ARM Coherent DMA allocator does not (yet) support huge TLB
289 #endif
290 
291 static void *__alloc_from_contiguous(struct device *dev, size_t size,
292 				     pgprot_t prot, struct page **ret_page,
293 				     const void *caller);
294 
295 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
296 				 pgprot_t prot, struct page **ret_page,
297 				 const void *caller);
298 
299 static void *
300 __dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot,
301 	const void *caller)
302 {
303 	struct vm_struct *area;
304 	unsigned long addr;
305 
306 	/*
307 	 * DMA allocation can be mapped to user space, so lets
308 	 * set VM_USERMAP flags too.
309 	 */
310 	area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
311 				  caller);
312 	if (!area)
313 		return NULL;
314 	addr = (unsigned long)area->addr;
315 	area->phys_addr = __pfn_to_phys(page_to_pfn(page));
316 
317 	if (ioremap_page_range(addr, addr + size, area->phys_addr, prot)) {
318 		vunmap((void *)addr);
319 		return NULL;
320 	}
321 	return (void *)addr;
322 }
323 
324 static void __dma_free_remap(void *cpu_addr, size_t size)
325 {
326 	unsigned int flags = VM_ARM_DMA_CONSISTENT | VM_USERMAP;
327 	struct vm_struct *area = find_vm_area(cpu_addr);
328 	if (!area || (area->flags & flags) != flags) {
329 		WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
330 		return;
331 	}
332 	unmap_kernel_range((unsigned long)cpu_addr, size);
333 	vunmap(cpu_addr);
334 }
335 
336 #define DEFAULT_DMA_COHERENT_POOL_SIZE	SZ_256K
337 
338 struct dma_pool {
339 	size_t size;
340 	spinlock_t lock;
341 	unsigned long *bitmap;
342 	unsigned long nr_pages;
343 	void *vaddr;
344 	struct page **pages;
345 };
346 
347 static struct dma_pool atomic_pool = {
348 	.size = DEFAULT_DMA_COHERENT_POOL_SIZE,
349 };
350 
351 static int __init early_coherent_pool(char *p)
352 {
353 	atomic_pool.size = memparse(p, &p);
354 	return 0;
355 }
356 early_param("coherent_pool", early_coherent_pool);
357 
358 void __init init_dma_coherent_pool_size(unsigned long size)
359 {
360 	/*
361 	 * Catch any attempt to set the pool size too late.
362 	 */
363 	BUG_ON(atomic_pool.vaddr);
364 
365 	/*
366 	 * Set architecture specific coherent pool size only if
367 	 * it has not been changed by kernel command line parameter.
368 	 */
369 	if (atomic_pool.size == DEFAULT_DMA_COHERENT_POOL_SIZE)
370 		atomic_pool.size = size;
371 }
372 
373 /*
374  * Initialise the coherent pool for atomic allocations.
375  */
376 static int __init atomic_pool_init(void)
377 {
378 	struct dma_pool *pool = &atomic_pool;
379 	pgprot_t prot = pgprot_dmacoherent(pgprot_kernel);
380 	gfp_t gfp = GFP_KERNEL | GFP_DMA;
381 	unsigned long nr_pages = pool->size >> PAGE_SHIFT;
382 	unsigned long *bitmap;
383 	struct page *page;
384 	struct page **pages;
385 	void *ptr;
386 	int bitmap_size = BITS_TO_LONGS(nr_pages) * sizeof(long);
387 
388 	bitmap = kzalloc(bitmap_size, GFP_KERNEL);
389 	if (!bitmap)
390 		goto no_bitmap;
391 
392 	pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
393 	if (!pages)
394 		goto no_pages;
395 
396 	if (IS_ENABLED(CONFIG_DMA_CMA))
397 		ptr = __alloc_from_contiguous(NULL, pool->size, prot, &page,
398 					      atomic_pool_init);
399 	else
400 		ptr = __alloc_remap_buffer(NULL, pool->size, gfp, prot, &page,
401 					   atomic_pool_init);
402 	if (ptr) {
403 		int i;
404 
405 		for (i = 0; i < nr_pages; i++)
406 			pages[i] = page + i;
407 
408 		spin_lock_init(&pool->lock);
409 		pool->vaddr = ptr;
410 		pool->pages = pages;
411 		pool->bitmap = bitmap;
412 		pool->nr_pages = nr_pages;
413 		pr_info("DMA: preallocated %u KiB pool for atomic coherent allocations\n",
414 		       (unsigned)pool->size / 1024);
415 		return 0;
416 	}
417 
418 	kfree(pages);
419 no_pages:
420 	kfree(bitmap);
421 no_bitmap:
422 	pr_err("DMA: failed to allocate %u KiB pool for atomic coherent allocation\n",
423 	       (unsigned)pool->size / 1024);
424 	return -ENOMEM;
425 }
426 /*
427  * CMA is activated by core_initcall, so we must be called after it.
428  */
429 postcore_initcall(atomic_pool_init);
430 
431 struct dma_contig_early_reserve {
432 	phys_addr_t base;
433 	unsigned long size;
434 };
435 
436 static struct dma_contig_early_reserve dma_mmu_remap[MAX_CMA_AREAS] __initdata;
437 
438 static int dma_mmu_remap_num __initdata;
439 
440 void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size)
441 {
442 	dma_mmu_remap[dma_mmu_remap_num].base = base;
443 	dma_mmu_remap[dma_mmu_remap_num].size = size;
444 	dma_mmu_remap_num++;
445 }
446 
447 void __init dma_contiguous_remap(void)
448 {
449 	int i;
450 	for (i = 0; i < dma_mmu_remap_num; i++) {
451 		phys_addr_t start = dma_mmu_remap[i].base;
452 		phys_addr_t end = start + dma_mmu_remap[i].size;
453 		struct map_desc map;
454 		unsigned long addr;
455 
456 		if (end > arm_lowmem_limit)
457 			end = arm_lowmem_limit;
458 		if (start >= end)
459 			continue;
460 
461 		map.pfn = __phys_to_pfn(start);
462 		map.virtual = __phys_to_virt(start);
463 		map.length = end - start;
464 		map.type = MT_MEMORY_DMA_READY;
465 
466 		/*
467 		 * Clear previous low-memory mapping
468 		 */
469 		for (addr = __phys_to_virt(start); addr < __phys_to_virt(end);
470 		     addr += PMD_SIZE)
471 			pmd_clear(pmd_off_k(addr));
472 
473 		iotable_init(&map, 1);
474 	}
475 }
476 
477 static int __dma_update_pte(pte_t *pte, pgtable_t token, unsigned long addr,
478 			    void *data)
479 {
480 	struct page *page = virt_to_page(addr);
481 	pgprot_t prot = *(pgprot_t *)data;
482 
483 	set_pte_ext(pte, mk_pte(page, prot), 0);
484 	return 0;
485 }
486 
487 static void __dma_remap(struct page *page, size_t size, pgprot_t prot)
488 {
489 	unsigned long start = (unsigned long) page_address(page);
490 	unsigned end = start + size;
491 
492 	apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot);
493 	flush_tlb_kernel_range(start, end);
494 }
495 
496 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
497 				 pgprot_t prot, struct page **ret_page,
498 				 const void *caller)
499 {
500 	struct page *page;
501 	void *ptr;
502 	page = __dma_alloc_buffer(dev, size, gfp);
503 	if (!page)
504 		return NULL;
505 
506 	ptr = __dma_alloc_remap(page, size, gfp, prot, caller);
507 	if (!ptr) {
508 		__dma_free_buffer(page, size);
509 		return NULL;
510 	}
511 
512 	*ret_page = page;
513 	return ptr;
514 }
515 
516 static void *__alloc_from_pool(size_t size, struct page **ret_page)
517 {
518 	struct dma_pool *pool = &atomic_pool;
519 	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
520 	unsigned int pageno;
521 	unsigned long flags;
522 	void *ptr = NULL;
523 	unsigned long align_mask;
524 
525 	if (!pool->vaddr) {
526 		WARN(1, "coherent pool not initialised!\n");
527 		return NULL;
528 	}
529 
530 	/*
531 	 * Align the region allocation - allocations from pool are rather
532 	 * small, so align them to their order in pages, minimum is a page
533 	 * size. This helps reduce fragmentation of the DMA space.
534 	 */
535 	align_mask = (1 << get_order(size)) - 1;
536 
537 	spin_lock_irqsave(&pool->lock, flags);
538 	pageno = bitmap_find_next_zero_area(pool->bitmap, pool->nr_pages,
539 					    0, count, align_mask);
540 	if (pageno < pool->nr_pages) {
541 		bitmap_set(pool->bitmap, pageno, count);
542 		ptr = pool->vaddr + PAGE_SIZE * pageno;
543 		*ret_page = pool->pages[pageno];
544 	} else {
545 		pr_err_once("ERROR: %u KiB atomic DMA coherent pool is too small!\n"
546 			    "Please increase it with coherent_pool= kernel parameter!\n",
547 			    (unsigned)pool->size / 1024);
548 	}
549 	spin_unlock_irqrestore(&pool->lock, flags);
550 
551 	return ptr;
552 }
553 
554 static bool __in_atomic_pool(void *start, size_t size)
555 {
556 	struct dma_pool *pool = &atomic_pool;
557 	void *end = start + size;
558 	void *pool_start = pool->vaddr;
559 	void *pool_end = pool->vaddr + pool->size;
560 
561 	if (start < pool_start || start >= pool_end)
562 		return false;
563 
564 	if (end <= pool_end)
565 		return true;
566 
567 	WARN(1, "Wrong coherent size(%p-%p) from atomic pool(%p-%p)\n",
568 	     start, end - 1, pool_start, pool_end - 1);
569 
570 	return false;
571 }
572 
573 static int __free_from_pool(void *start, size_t size)
574 {
575 	struct dma_pool *pool = &atomic_pool;
576 	unsigned long pageno, count;
577 	unsigned long flags;
578 
579 	if (!__in_atomic_pool(start, size))
580 		return 0;
581 
582 	pageno = (start - pool->vaddr) >> PAGE_SHIFT;
583 	count = size >> PAGE_SHIFT;
584 
585 	spin_lock_irqsave(&pool->lock, flags);
586 	bitmap_clear(pool->bitmap, pageno, count);
587 	spin_unlock_irqrestore(&pool->lock, flags);
588 
589 	return 1;
590 }
591 
592 static void *__alloc_from_contiguous(struct device *dev, size_t size,
593 				     pgprot_t prot, struct page **ret_page,
594 				     const void *caller)
595 {
596 	unsigned long order = get_order(size);
597 	size_t count = size >> PAGE_SHIFT;
598 	struct page *page;
599 	void *ptr;
600 
601 	page = dma_alloc_from_contiguous(dev, count, order);
602 	if (!page)
603 		return NULL;
604 
605 	__dma_clear_buffer(page, size);
606 
607 	if (PageHighMem(page)) {
608 		ptr = __dma_alloc_remap(page, size, GFP_KERNEL, prot, caller);
609 		if (!ptr) {
610 			dma_release_from_contiguous(dev, page, count);
611 			return NULL;
612 		}
613 	} else {
614 		__dma_remap(page, size, prot);
615 		ptr = page_address(page);
616 	}
617 	*ret_page = page;
618 	return ptr;
619 }
620 
621 static void __free_from_contiguous(struct device *dev, struct page *page,
622 				   void *cpu_addr, size_t size)
623 {
624 	if (PageHighMem(page))
625 		__dma_free_remap(cpu_addr, size);
626 	else
627 		__dma_remap(page, size, pgprot_kernel);
628 	dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
629 }
630 
631 static inline pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot)
632 {
633 	prot = dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs) ?
634 			    pgprot_writecombine(prot) :
635 			    pgprot_dmacoherent(prot);
636 	return prot;
637 }
638 
639 #define nommu() 0
640 
641 #else	/* !CONFIG_MMU */
642 
643 #define nommu() 1
644 
645 #define __get_dma_pgprot(attrs, prot)	__pgprot(0)
646 #define __alloc_remap_buffer(dev, size, gfp, prot, ret, c)	NULL
647 #define __alloc_from_pool(size, ret_page)			NULL
648 #define __alloc_from_contiguous(dev, size, prot, ret, c)	NULL
649 #define __free_from_pool(cpu_addr, size)			0
650 #define __free_from_contiguous(dev, page, cpu_addr, size)	do { } while (0)
651 #define __dma_free_remap(cpu_addr, size)			do { } while (0)
652 
653 #endif	/* CONFIG_MMU */
654 
655 static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
656 				   struct page **ret_page)
657 {
658 	struct page *page;
659 	page = __dma_alloc_buffer(dev, size, gfp);
660 	if (!page)
661 		return NULL;
662 
663 	*ret_page = page;
664 	return page_address(page);
665 }
666 
667 
668 
669 static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
670 			 gfp_t gfp, pgprot_t prot, bool is_coherent, const void *caller)
671 {
672 	u64 mask = get_coherent_dma_mask(dev);
673 	struct page *page = NULL;
674 	void *addr;
675 
676 #ifdef CONFIG_DMA_API_DEBUG
677 	u64 limit = (mask + 1) & ~mask;
678 	if (limit && size >= limit) {
679 		dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n",
680 			size, mask);
681 		return NULL;
682 	}
683 #endif
684 
685 	if (!mask)
686 		return NULL;
687 
688 	if (mask < 0xffffffffULL)
689 		gfp |= GFP_DMA;
690 
691 	/*
692 	 * Following is a work-around (a.k.a. hack) to prevent pages
693 	 * with __GFP_COMP being passed to split_page() which cannot
694 	 * handle them.  The real problem is that this flag probably
695 	 * should be 0 on ARM as it is not supported on this
696 	 * platform; see CONFIG_HUGETLBFS.
697 	 */
698 	gfp &= ~(__GFP_COMP);
699 
700 	*handle = DMA_ERROR_CODE;
701 	size = PAGE_ALIGN(size);
702 
703 	if (is_coherent || nommu())
704 		addr = __alloc_simple_buffer(dev, size, gfp, &page);
705 	else if (!(gfp & __GFP_WAIT))
706 		addr = __alloc_from_pool(size, &page);
707 	else if (!IS_ENABLED(CONFIG_DMA_CMA))
708 		addr = __alloc_remap_buffer(dev, size, gfp, prot, &page, caller);
709 	else
710 		addr = __alloc_from_contiguous(dev, size, prot, &page, caller);
711 
712 	if (addr)
713 		*handle = pfn_to_dma(dev, page_to_pfn(page));
714 
715 	return addr;
716 }
717 
718 /*
719  * Allocate DMA-coherent memory space and return both the kernel remapped
720  * virtual and bus address for that space.
721  */
722 void *arm_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
723 		    gfp_t gfp, struct dma_attrs *attrs)
724 {
725 	pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
726 	void *memory;
727 
728 	if (dma_alloc_from_coherent(dev, size, handle, &memory))
729 		return memory;
730 
731 	return __dma_alloc(dev, size, handle, gfp, prot, false,
732 			   __builtin_return_address(0));
733 }
734 
735 static void *arm_coherent_dma_alloc(struct device *dev, size_t size,
736 	dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
737 {
738 	pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL);
739 	void *memory;
740 
741 	if (dma_alloc_from_coherent(dev, size, handle, &memory))
742 		return memory;
743 
744 	return __dma_alloc(dev, size, handle, gfp, prot, true,
745 			   __builtin_return_address(0));
746 }
747 
748 /*
749  * Create userspace mapping for the DMA-coherent memory.
750  */
751 int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
752 		 void *cpu_addr, dma_addr_t dma_addr, size_t size,
753 		 struct dma_attrs *attrs)
754 {
755 	int ret = -ENXIO;
756 #ifdef CONFIG_MMU
757 	unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
758 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
759 	unsigned long pfn = dma_to_pfn(dev, dma_addr);
760 	unsigned long off = vma->vm_pgoff;
761 
762 	vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
763 
764 	if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
765 		return ret;
766 
767 	if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
768 		ret = remap_pfn_range(vma, vma->vm_start,
769 				      pfn + off,
770 				      vma->vm_end - vma->vm_start,
771 				      vma->vm_page_prot);
772 	}
773 #endif	/* CONFIG_MMU */
774 
775 	return ret;
776 }
777 
778 /*
779  * Free a buffer as defined by the above mapping.
780  */
781 static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
782 			   dma_addr_t handle, struct dma_attrs *attrs,
783 			   bool is_coherent)
784 {
785 	struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
786 
787 	if (dma_release_from_coherent(dev, get_order(size), cpu_addr))
788 		return;
789 
790 	size = PAGE_ALIGN(size);
791 
792 	if (is_coherent || nommu()) {
793 		__dma_free_buffer(page, size);
794 	} else if (__free_from_pool(cpu_addr, size)) {
795 		return;
796 	} else if (!IS_ENABLED(CONFIG_DMA_CMA)) {
797 		__dma_free_remap(cpu_addr, size);
798 		__dma_free_buffer(page, size);
799 	} else {
800 		/*
801 		 * Non-atomic allocations cannot be freed with IRQs disabled
802 		 */
803 		WARN_ON(irqs_disabled());
804 		__free_from_contiguous(dev, page, cpu_addr, size);
805 	}
806 }
807 
808 void arm_dma_free(struct device *dev, size_t size, void *cpu_addr,
809 		  dma_addr_t handle, struct dma_attrs *attrs)
810 {
811 	__arm_dma_free(dev, size, cpu_addr, handle, attrs, false);
812 }
813 
814 static void arm_coherent_dma_free(struct device *dev, size_t size, void *cpu_addr,
815 				  dma_addr_t handle, struct dma_attrs *attrs)
816 {
817 	__arm_dma_free(dev, size, cpu_addr, handle, attrs, true);
818 }
819 
820 int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
821 		 void *cpu_addr, dma_addr_t handle, size_t size,
822 		 struct dma_attrs *attrs)
823 {
824 	struct page *page = pfn_to_page(dma_to_pfn(dev, handle));
825 	int ret;
826 
827 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
828 	if (unlikely(ret))
829 		return ret;
830 
831 	sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
832 	return 0;
833 }
834 
835 static void dma_cache_maint_page(struct page *page, unsigned long offset,
836 	size_t size, enum dma_data_direction dir,
837 	void (*op)(const void *, size_t, int))
838 {
839 	unsigned long pfn;
840 	size_t left = size;
841 
842 	pfn = page_to_pfn(page) + offset / PAGE_SIZE;
843 	offset %= PAGE_SIZE;
844 
845 	/*
846 	 * A single sg entry may refer to multiple physically contiguous
847 	 * pages.  But we still need to process highmem pages individually.
848 	 * If highmem is not configured then the bulk of this loop gets
849 	 * optimized out.
850 	 */
851 	do {
852 		size_t len = left;
853 		void *vaddr;
854 
855 		page = pfn_to_page(pfn);
856 
857 		if (PageHighMem(page)) {
858 			if (len + offset > PAGE_SIZE)
859 				len = PAGE_SIZE - offset;
860 
861 			if (cache_is_vipt_nonaliasing()) {
862 				vaddr = kmap_atomic(page);
863 				op(vaddr + offset, len, dir);
864 				kunmap_atomic(vaddr);
865 			} else {
866 				vaddr = kmap_high_get(page);
867 				if (vaddr) {
868 					op(vaddr + offset, len, dir);
869 					kunmap_high(page);
870 				}
871 			}
872 		} else {
873 			vaddr = page_address(page) + offset;
874 			op(vaddr, len, dir);
875 		}
876 		offset = 0;
877 		pfn++;
878 		left -= len;
879 	} while (left);
880 }
881 
882 /*
883  * Make an area consistent for devices.
884  * Note: Drivers should NOT use this function directly, as it will break
885  * platforms with CONFIG_DMABOUNCE.
886  * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
887  */
888 static void __dma_page_cpu_to_dev(struct page *page, unsigned long off,
889 	size_t size, enum dma_data_direction dir)
890 {
891 	unsigned long paddr;
892 
893 	dma_cache_maint_page(page, off, size, dir, dmac_map_area);
894 
895 	paddr = page_to_phys(page) + off;
896 	if (dir == DMA_FROM_DEVICE) {
897 		outer_inv_range(paddr, paddr + size);
898 	} else {
899 		outer_clean_range(paddr, paddr + size);
900 	}
901 	/* FIXME: non-speculating: flush on bidirectional mappings? */
902 }
903 
904 static void __dma_page_dev_to_cpu(struct page *page, unsigned long off,
905 	size_t size, enum dma_data_direction dir)
906 {
907 	unsigned long paddr = page_to_phys(page) + off;
908 
909 	/* FIXME: non-speculating: not required */
910 	/* don't bother invalidating if DMA to device */
911 	if (dir != DMA_TO_DEVICE)
912 		outer_inv_range(paddr, paddr + size);
913 
914 	dma_cache_maint_page(page, off, size, dir, dmac_unmap_area);
915 
916 	/*
917 	 * Mark the D-cache clean for these pages to avoid extra flushing.
918 	 */
919 	if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) {
920 		unsigned long pfn;
921 		size_t left = size;
922 
923 		pfn = page_to_pfn(page) + off / PAGE_SIZE;
924 		off %= PAGE_SIZE;
925 		if (off) {
926 			pfn++;
927 			left -= PAGE_SIZE - off;
928 		}
929 		while (left >= PAGE_SIZE) {
930 			page = pfn_to_page(pfn++);
931 			set_bit(PG_dcache_clean, &page->flags);
932 			left -= PAGE_SIZE;
933 		}
934 	}
935 }
936 
937 /**
938  * arm_dma_map_sg - map a set of SG buffers for streaming mode DMA
939  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
940  * @sg: list of buffers
941  * @nents: number of buffers to map
942  * @dir: DMA transfer direction
943  *
944  * Map a set of buffers described by scatterlist in streaming mode for DMA.
945  * This is the scatter-gather version of the dma_map_single interface.
946  * Here the scatter gather list elements are each tagged with the
947  * appropriate dma address and length.  They are obtained via
948  * sg_dma_{address,length}.
949  *
950  * Device ownership issues as mentioned for dma_map_single are the same
951  * here.
952  */
953 int arm_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
954 		enum dma_data_direction dir, struct dma_attrs *attrs)
955 {
956 	struct dma_map_ops *ops = get_dma_ops(dev);
957 	struct scatterlist *s;
958 	int i, j;
959 
960 	for_each_sg(sg, s, nents, i) {
961 #ifdef CONFIG_NEED_SG_DMA_LENGTH
962 		s->dma_length = s->length;
963 #endif
964 		s->dma_address = ops->map_page(dev, sg_page(s), s->offset,
965 						s->length, dir, attrs);
966 		if (dma_mapping_error(dev, s->dma_address))
967 			goto bad_mapping;
968 	}
969 	return nents;
970 
971  bad_mapping:
972 	for_each_sg(sg, s, i, j)
973 		ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
974 	return 0;
975 }
976 
977 /**
978  * arm_dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
979  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
980  * @sg: list of buffers
981  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
982  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
983  *
984  * Unmap a set of streaming mode DMA translations.  Again, CPU access
985  * rules concerning calls here are the same as for dma_unmap_single().
986  */
987 void arm_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
988 		enum dma_data_direction dir, struct dma_attrs *attrs)
989 {
990 	struct dma_map_ops *ops = get_dma_ops(dev);
991 	struct scatterlist *s;
992 
993 	int i;
994 
995 	for_each_sg(sg, s, nents, i)
996 		ops->unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, attrs);
997 }
998 
999 /**
1000  * arm_dma_sync_sg_for_cpu
1001  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1002  * @sg: list of buffers
1003  * @nents: number of buffers to map (returned from dma_map_sg)
1004  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1005  */
1006 void arm_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1007 			int nents, enum dma_data_direction dir)
1008 {
1009 	struct dma_map_ops *ops = get_dma_ops(dev);
1010 	struct scatterlist *s;
1011 	int i;
1012 
1013 	for_each_sg(sg, s, nents, i)
1014 		ops->sync_single_for_cpu(dev, sg_dma_address(s), s->length,
1015 					 dir);
1016 }
1017 
1018 /**
1019  * arm_dma_sync_sg_for_device
1020  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
1021  * @sg: list of buffers
1022  * @nents: number of buffers to map (returned from dma_map_sg)
1023  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1024  */
1025 void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1026 			int nents, enum dma_data_direction dir)
1027 {
1028 	struct dma_map_ops *ops = get_dma_ops(dev);
1029 	struct scatterlist *s;
1030 	int i;
1031 
1032 	for_each_sg(sg, s, nents, i)
1033 		ops->sync_single_for_device(dev, sg_dma_address(s), s->length,
1034 					    dir);
1035 }
1036 
1037 /*
1038  * Return whether the given device DMA address mask can be supported
1039  * properly.  For example, if your device can only drive the low 24-bits
1040  * during bus mastering, then you would pass 0x00ffffff as the mask
1041  * to this function.
1042  */
1043 int dma_supported(struct device *dev, u64 mask)
1044 {
1045 	return __dma_supported(dev, mask, false);
1046 }
1047 EXPORT_SYMBOL(dma_supported);
1048 
1049 int arm_dma_set_mask(struct device *dev, u64 dma_mask)
1050 {
1051 	if (!dev->dma_mask || !dma_supported(dev, dma_mask))
1052 		return -EIO;
1053 
1054 	*dev->dma_mask = dma_mask;
1055 
1056 	return 0;
1057 }
1058 
1059 #define PREALLOC_DMA_DEBUG_ENTRIES	4096
1060 
1061 static int __init dma_debug_do_init(void)
1062 {
1063 	dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
1064 	return 0;
1065 }
1066 fs_initcall(dma_debug_do_init);
1067 
1068 #ifdef CONFIG_ARM_DMA_USE_IOMMU
1069 
1070 /* IOMMU */
1071 
1072 static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
1073 				      size_t size)
1074 {
1075 	unsigned int order = get_order(size);
1076 	unsigned int align = 0;
1077 	unsigned int count, start;
1078 	unsigned long flags;
1079 
1080 	if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
1081 		order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
1082 
1083 	count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
1084 		 (1 << mapping->order) - 1) >> mapping->order;
1085 
1086 	if (order > mapping->order)
1087 		align = (1 << (order - mapping->order)) - 1;
1088 
1089 	spin_lock_irqsave(&mapping->lock, flags);
1090 	start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
1091 					   count, align);
1092 	if (start > mapping->bits) {
1093 		spin_unlock_irqrestore(&mapping->lock, flags);
1094 		return DMA_ERROR_CODE;
1095 	}
1096 
1097 	bitmap_set(mapping->bitmap, start, count);
1098 	spin_unlock_irqrestore(&mapping->lock, flags);
1099 
1100 	return mapping->base + (start << (mapping->order + PAGE_SHIFT));
1101 }
1102 
1103 static inline void __free_iova(struct dma_iommu_mapping *mapping,
1104 			       dma_addr_t addr, size_t size)
1105 {
1106 	unsigned int start = (addr - mapping->base) >>
1107 			     (mapping->order + PAGE_SHIFT);
1108 	unsigned int count = ((size >> PAGE_SHIFT) +
1109 			      (1 << mapping->order) - 1) >> mapping->order;
1110 	unsigned long flags;
1111 
1112 	spin_lock_irqsave(&mapping->lock, flags);
1113 	bitmap_clear(mapping->bitmap, start, count);
1114 	spin_unlock_irqrestore(&mapping->lock, flags);
1115 }
1116 
1117 static struct page **__iommu_alloc_buffer(struct device *dev, size_t size,
1118 					  gfp_t gfp, struct dma_attrs *attrs)
1119 {
1120 	struct page **pages;
1121 	int count = size >> PAGE_SHIFT;
1122 	int array_size = count * sizeof(struct page *);
1123 	int i = 0;
1124 
1125 	if (array_size <= PAGE_SIZE)
1126 		pages = kzalloc(array_size, gfp);
1127 	else
1128 		pages = vzalloc(array_size);
1129 	if (!pages)
1130 		return NULL;
1131 
1132 	if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs))
1133 	{
1134 		unsigned long order = get_order(size);
1135 		struct page *page;
1136 
1137 		page = dma_alloc_from_contiguous(dev, count, order);
1138 		if (!page)
1139 			goto error;
1140 
1141 		__dma_clear_buffer(page, size);
1142 
1143 		for (i = 0; i < count; i++)
1144 			pages[i] = page + i;
1145 
1146 		return pages;
1147 	}
1148 
1149 	/*
1150 	 * IOMMU can map any pages, so himem can also be used here
1151 	 */
1152 	gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
1153 
1154 	while (count) {
1155 		int j, order = __fls(count);
1156 
1157 		pages[i] = alloc_pages(gfp, order);
1158 		while (!pages[i] && order)
1159 			pages[i] = alloc_pages(gfp, --order);
1160 		if (!pages[i])
1161 			goto error;
1162 
1163 		if (order) {
1164 			split_page(pages[i], order);
1165 			j = 1 << order;
1166 			while (--j)
1167 				pages[i + j] = pages[i] + j;
1168 		}
1169 
1170 		__dma_clear_buffer(pages[i], PAGE_SIZE << order);
1171 		i += 1 << order;
1172 		count -= 1 << order;
1173 	}
1174 
1175 	return pages;
1176 error:
1177 	while (i--)
1178 		if (pages[i])
1179 			__free_pages(pages[i], 0);
1180 	if (array_size <= PAGE_SIZE)
1181 		kfree(pages);
1182 	else
1183 		vfree(pages);
1184 	return NULL;
1185 }
1186 
1187 static int __iommu_free_buffer(struct device *dev, struct page **pages,
1188 			       size_t size, struct dma_attrs *attrs)
1189 {
1190 	int count = size >> PAGE_SHIFT;
1191 	int array_size = count * sizeof(struct page *);
1192 	int i;
1193 
1194 	if (dma_get_attr(DMA_ATTR_FORCE_CONTIGUOUS, attrs)) {
1195 		dma_release_from_contiguous(dev, pages[0], count);
1196 	} else {
1197 		for (i = 0; i < count; i++)
1198 			if (pages[i])
1199 				__free_pages(pages[i], 0);
1200 	}
1201 
1202 	if (array_size <= PAGE_SIZE)
1203 		kfree(pages);
1204 	else
1205 		vfree(pages);
1206 	return 0;
1207 }
1208 
1209 /*
1210  * Create a CPU mapping for a specified pages
1211  */
1212 static void *
1213 __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot,
1214 		    const void *caller)
1215 {
1216 	unsigned int i, nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1217 	struct vm_struct *area;
1218 	unsigned long p;
1219 
1220 	area = get_vm_area_caller(size, VM_ARM_DMA_CONSISTENT | VM_USERMAP,
1221 				  caller);
1222 	if (!area)
1223 		return NULL;
1224 
1225 	area->pages = pages;
1226 	area->nr_pages = nr_pages;
1227 	p = (unsigned long)area->addr;
1228 
1229 	for (i = 0; i < nr_pages; i++) {
1230 		phys_addr_t phys = __pfn_to_phys(page_to_pfn(pages[i]));
1231 		if (ioremap_page_range(p, p + PAGE_SIZE, phys, prot))
1232 			goto err;
1233 		p += PAGE_SIZE;
1234 	}
1235 	return area->addr;
1236 err:
1237 	unmap_kernel_range((unsigned long)area->addr, size);
1238 	vunmap(area->addr);
1239 	return NULL;
1240 }
1241 
1242 /*
1243  * Create a mapping in device IO address space for specified pages
1244  */
1245 static dma_addr_t
1246 __iommu_create_mapping(struct device *dev, struct page **pages, size_t size)
1247 {
1248 	struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1249 	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1250 	dma_addr_t dma_addr, iova;
1251 	int i, ret = DMA_ERROR_CODE;
1252 
1253 	dma_addr = __alloc_iova(mapping, size);
1254 	if (dma_addr == DMA_ERROR_CODE)
1255 		return dma_addr;
1256 
1257 	iova = dma_addr;
1258 	for (i = 0; i < count; ) {
1259 		unsigned int next_pfn = page_to_pfn(pages[i]) + 1;
1260 		phys_addr_t phys = page_to_phys(pages[i]);
1261 		unsigned int len, j;
1262 
1263 		for (j = i + 1; j < count; j++, next_pfn++)
1264 			if (page_to_pfn(pages[j]) != next_pfn)
1265 				break;
1266 
1267 		len = (j - i) << PAGE_SHIFT;
1268 		ret = iommu_map(mapping->domain, iova, phys, len,
1269 				IOMMU_READ|IOMMU_WRITE);
1270 		if (ret < 0)
1271 			goto fail;
1272 		iova += len;
1273 		i = j;
1274 	}
1275 	return dma_addr;
1276 fail:
1277 	iommu_unmap(mapping->domain, dma_addr, iova-dma_addr);
1278 	__free_iova(mapping, dma_addr, size);
1279 	return DMA_ERROR_CODE;
1280 }
1281 
1282 static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size)
1283 {
1284 	struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1285 
1286 	/*
1287 	 * add optional in-page offset from iova to size and align
1288 	 * result to page size
1289 	 */
1290 	size = PAGE_ALIGN((iova & ~PAGE_MASK) + size);
1291 	iova &= PAGE_MASK;
1292 
1293 	iommu_unmap(mapping->domain, iova, size);
1294 	__free_iova(mapping, iova, size);
1295 	return 0;
1296 }
1297 
1298 static struct page **__atomic_get_pages(void *addr)
1299 {
1300 	struct dma_pool *pool = &atomic_pool;
1301 	struct page **pages = pool->pages;
1302 	int offs = (addr - pool->vaddr) >> PAGE_SHIFT;
1303 
1304 	return pages + offs;
1305 }
1306 
1307 static struct page **__iommu_get_pages(void *cpu_addr, struct dma_attrs *attrs)
1308 {
1309 	struct vm_struct *area;
1310 
1311 	if (__in_atomic_pool(cpu_addr, PAGE_SIZE))
1312 		return __atomic_get_pages(cpu_addr);
1313 
1314 	if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1315 		return cpu_addr;
1316 
1317 	area = find_vm_area(cpu_addr);
1318 	if (area && (area->flags & VM_ARM_DMA_CONSISTENT))
1319 		return area->pages;
1320 	return NULL;
1321 }
1322 
1323 static void *__iommu_alloc_atomic(struct device *dev, size_t size,
1324 				  dma_addr_t *handle)
1325 {
1326 	struct page *page;
1327 	void *addr;
1328 
1329 	addr = __alloc_from_pool(size, &page);
1330 	if (!addr)
1331 		return NULL;
1332 
1333 	*handle = __iommu_create_mapping(dev, &page, size);
1334 	if (*handle == DMA_ERROR_CODE)
1335 		goto err_mapping;
1336 
1337 	return addr;
1338 
1339 err_mapping:
1340 	__free_from_pool(addr, size);
1341 	return NULL;
1342 }
1343 
1344 static void __iommu_free_atomic(struct device *dev, void *cpu_addr,
1345 				dma_addr_t handle, size_t size)
1346 {
1347 	__iommu_remove_mapping(dev, handle, size);
1348 	__free_from_pool(cpu_addr, size);
1349 }
1350 
1351 static void *arm_iommu_alloc_attrs(struct device *dev, size_t size,
1352 	    dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs)
1353 {
1354 	pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel);
1355 	struct page **pages;
1356 	void *addr = NULL;
1357 
1358 	*handle = DMA_ERROR_CODE;
1359 	size = PAGE_ALIGN(size);
1360 
1361 	if (gfp & GFP_ATOMIC)
1362 		return __iommu_alloc_atomic(dev, size, handle);
1363 
1364 	/*
1365 	 * Following is a work-around (a.k.a. hack) to prevent pages
1366 	 * with __GFP_COMP being passed to split_page() which cannot
1367 	 * handle them.  The real problem is that this flag probably
1368 	 * should be 0 on ARM as it is not supported on this
1369 	 * platform; see CONFIG_HUGETLBFS.
1370 	 */
1371 	gfp &= ~(__GFP_COMP);
1372 
1373 	pages = __iommu_alloc_buffer(dev, size, gfp, attrs);
1374 	if (!pages)
1375 		return NULL;
1376 
1377 	*handle = __iommu_create_mapping(dev, pages, size);
1378 	if (*handle == DMA_ERROR_CODE)
1379 		goto err_buffer;
1380 
1381 	if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs))
1382 		return pages;
1383 
1384 	addr = __iommu_alloc_remap(pages, size, gfp, prot,
1385 				   __builtin_return_address(0));
1386 	if (!addr)
1387 		goto err_mapping;
1388 
1389 	return addr;
1390 
1391 err_mapping:
1392 	__iommu_remove_mapping(dev, *handle, size);
1393 err_buffer:
1394 	__iommu_free_buffer(dev, pages, size, attrs);
1395 	return NULL;
1396 }
1397 
1398 static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
1399 		    void *cpu_addr, dma_addr_t dma_addr, size_t size,
1400 		    struct dma_attrs *attrs)
1401 {
1402 	unsigned long uaddr = vma->vm_start;
1403 	unsigned long usize = vma->vm_end - vma->vm_start;
1404 	struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1405 
1406 	vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
1407 
1408 	if (!pages)
1409 		return -ENXIO;
1410 
1411 	do {
1412 		int ret = vm_insert_page(vma, uaddr, *pages++);
1413 		if (ret) {
1414 			pr_err("Remapping memory failed: %d\n", ret);
1415 			return ret;
1416 		}
1417 		uaddr += PAGE_SIZE;
1418 		usize -= PAGE_SIZE;
1419 	} while (usize > 0);
1420 
1421 	return 0;
1422 }
1423 
1424 /*
1425  * free a page as defined by the above mapping.
1426  * Must not be called with IRQs disabled.
1427  */
1428 void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
1429 			  dma_addr_t handle, struct dma_attrs *attrs)
1430 {
1431 	struct page **pages;
1432 	size = PAGE_ALIGN(size);
1433 
1434 	if (__in_atomic_pool(cpu_addr, size)) {
1435 		__iommu_free_atomic(dev, cpu_addr, handle, size);
1436 		return;
1437 	}
1438 
1439 	pages = __iommu_get_pages(cpu_addr, attrs);
1440 	if (!pages) {
1441 		WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
1442 		return;
1443 	}
1444 
1445 	if (!dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, attrs)) {
1446 		unmap_kernel_range((unsigned long)cpu_addr, size);
1447 		vunmap(cpu_addr);
1448 	}
1449 
1450 	__iommu_remove_mapping(dev, handle, size);
1451 	__iommu_free_buffer(dev, pages, size, attrs);
1452 }
1453 
1454 static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
1455 				 void *cpu_addr, dma_addr_t dma_addr,
1456 				 size_t size, struct dma_attrs *attrs)
1457 {
1458 	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1459 	struct page **pages = __iommu_get_pages(cpu_addr, attrs);
1460 
1461 	if (!pages)
1462 		return -ENXIO;
1463 
1464 	return sg_alloc_table_from_pages(sgt, pages, count, 0, size,
1465 					 GFP_KERNEL);
1466 }
1467 
1468 static int __dma_direction_to_prot(enum dma_data_direction dir)
1469 {
1470 	int prot;
1471 
1472 	switch (dir) {
1473 	case DMA_BIDIRECTIONAL:
1474 		prot = IOMMU_READ | IOMMU_WRITE;
1475 		break;
1476 	case DMA_TO_DEVICE:
1477 		prot = IOMMU_READ;
1478 		break;
1479 	case DMA_FROM_DEVICE:
1480 		prot = IOMMU_WRITE;
1481 		break;
1482 	default:
1483 		prot = 0;
1484 	}
1485 
1486 	return prot;
1487 }
1488 
1489 /*
1490  * Map a part of the scatter-gather list into contiguous io address space
1491  */
1492 static int __map_sg_chunk(struct device *dev, struct scatterlist *sg,
1493 			  size_t size, dma_addr_t *handle,
1494 			  enum dma_data_direction dir, struct dma_attrs *attrs,
1495 			  bool is_coherent)
1496 {
1497 	struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1498 	dma_addr_t iova, iova_base;
1499 	int ret = 0;
1500 	unsigned int count;
1501 	struct scatterlist *s;
1502 	int prot;
1503 
1504 	size = PAGE_ALIGN(size);
1505 	*handle = DMA_ERROR_CODE;
1506 
1507 	iova_base = iova = __alloc_iova(mapping, size);
1508 	if (iova == DMA_ERROR_CODE)
1509 		return -ENOMEM;
1510 
1511 	for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) {
1512 		phys_addr_t phys = page_to_phys(sg_page(s));
1513 		unsigned int len = PAGE_ALIGN(s->offset + s->length);
1514 
1515 		if (!is_coherent &&
1516 			!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1517 			__dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1518 
1519 		prot = __dma_direction_to_prot(dir);
1520 
1521 		ret = iommu_map(mapping->domain, iova, phys, len, prot);
1522 		if (ret < 0)
1523 			goto fail;
1524 		count += len >> PAGE_SHIFT;
1525 		iova += len;
1526 	}
1527 	*handle = iova_base;
1528 
1529 	return 0;
1530 fail:
1531 	iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE);
1532 	__free_iova(mapping, iova_base, size);
1533 	return ret;
1534 }
1535 
1536 static int __iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents,
1537 		     enum dma_data_direction dir, struct dma_attrs *attrs,
1538 		     bool is_coherent)
1539 {
1540 	struct scatterlist *s = sg, *dma = sg, *start = sg;
1541 	int i, count = 0;
1542 	unsigned int offset = s->offset;
1543 	unsigned int size = s->offset + s->length;
1544 	unsigned int max = dma_get_max_seg_size(dev);
1545 
1546 	for (i = 1; i < nents; i++) {
1547 		s = sg_next(s);
1548 
1549 		s->dma_address = DMA_ERROR_CODE;
1550 		s->dma_length = 0;
1551 
1552 		if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) {
1553 			if (__map_sg_chunk(dev, start, size, &dma->dma_address,
1554 			    dir, attrs, is_coherent) < 0)
1555 				goto bad_mapping;
1556 
1557 			dma->dma_address += offset;
1558 			dma->dma_length = size - offset;
1559 
1560 			size = offset = s->offset;
1561 			start = s;
1562 			dma = sg_next(dma);
1563 			count += 1;
1564 		}
1565 		size += s->length;
1566 	}
1567 	if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs,
1568 		is_coherent) < 0)
1569 		goto bad_mapping;
1570 
1571 	dma->dma_address += offset;
1572 	dma->dma_length = size - offset;
1573 
1574 	return count+1;
1575 
1576 bad_mapping:
1577 	for_each_sg(sg, s, count, i)
1578 		__iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s));
1579 	return 0;
1580 }
1581 
1582 /**
1583  * arm_coherent_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1584  * @dev: valid struct device pointer
1585  * @sg: list of buffers
1586  * @nents: number of buffers to map
1587  * @dir: DMA transfer direction
1588  *
1589  * Map a set of i/o coherent buffers described by scatterlist in streaming
1590  * mode for DMA. The scatter gather list elements are merged together (if
1591  * possible) and tagged with the appropriate dma address and length. They are
1592  * obtained via sg_dma_{address,length}.
1593  */
1594 int arm_coherent_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1595 		int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1596 {
1597 	return __iommu_map_sg(dev, sg, nents, dir, attrs, true);
1598 }
1599 
1600 /**
1601  * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1602  * @dev: valid struct device pointer
1603  * @sg: list of buffers
1604  * @nents: number of buffers to map
1605  * @dir: DMA transfer direction
1606  *
1607  * Map a set of buffers described by scatterlist in streaming mode for DMA.
1608  * The scatter gather list elements are merged together (if possible) and
1609  * tagged with the appropriate dma address and length. They are obtained via
1610  * sg_dma_{address,length}.
1611  */
1612 int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg,
1613 		int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1614 {
1615 	return __iommu_map_sg(dev, sg, nents, dir, attrs, false);
1616 }
1617 
1618 static void __iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1619 		int nents, enum dma_data_direction dir, struct dma_attrs *attrs,
1620 		bool is_coherent)
1621 {
1622 	struct scatterlist *s;
1623 	int i;
1624 
1625 	for_each_sg(sg, s, nents, i) {
1626 		if (sg_dma_len(s))
1627 			__iommu_remove_mapping(dev, sg_dma_address(s),
1628 					       sg_dma_len(s));
1629 		if (!is_coherent &&
1630 		    !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1631 			__dma_page_dev_to_cpu(sg_page(s), s->offset,
1632 					      s->length, dir);
1633 	}
1634 }
1635 
1636 /**
1637  * arm_coherent_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1638  * @dev: valid struct device pointer
1639  * @sg: list of buffers
1640  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1641  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1642  *
1643  * Unmap a set of streaming mode DMA translations.  Again, CPU access
1644  * rules concerning calls here are the same as for dma_unmap_single().
1645  */
1646 void arm_coherent_iommu_unmap_sg(struct device *dev, struct scatterlist *sg,
1647 		int nents, enum dma_data_direction dir, struct dma_attrs *attrs)
1648 {
1649 	__iommu_unmap_sg(dev, sg, nents, dir, attrs, true);
1650 }
1651 
1652 /**
1653  * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1654  * @dev: valid struct device pointer
1655  * @sg: list of buffers
1656  * @nents: number of buffers to unmap (same as was passed to dma_map_sg)
1657  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1658  *
1659  * Unmap a set of streaming mode DMA translations.  Again, CPU access
1660  * rules concerning calls here are the same as for dma_unmap_single().
1661  */
1662 void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
1663 			enum dma_data_direction dir, struct dma_attrs *attrs)
1664 {
1665 	__iommu_unmap_sg(dev, sg, nents, dir, attrs, false);
1666 }
1667 
1668 /**
1669  * arm_iommu_sync_sg_for_cpu
1670  * @dev: valid struct device pointer
1671  * @sg: list of buffers
1672  * @nents: number of buffers to map (returned from dma_map_sg)
1673  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1674  */
1675 void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
1676 			int nents, enum dma_data_direction dir)
1677 {
1678 	struct scatterlist *s;
1679 	int i;
1680 
1681 	for_each_sg(sg, s, nents, i)
1682 		__dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir);
1683 
1684 }
1685 
1686 /**
1687  * arm_iommu_sync_sg_for_device
1688  * @dev: valid struct device pointer
1689  * @sg: list of buffers
1690  * @nents: number of buffers to map (returned from dma_map_sg)
1691  * @dir: DMA transfer direction (same as was passed to dma_map_sg)
1692  */
1693 void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
1694 			int nents, enum dma_data_direction dir)
1695 {
1696 	struct scatterlist *s;
1697 	int i;
1698 
1699 	for_each_sg(sg, s, nents, i)
1700 		__dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir);
1701 }
1702 
1703 
1704 /**
1705  * arm_coherent_iommu_map_page
1706  * @dev: valid struct device pointer
1707  * @page: page that buffer resides in
1708  * @offset: offset into page for start of buffer
1709  * @size: size of buffer to map
1710  * @dir: DMA transfer direction
1711  *
1712  * Coherent IOMMU aware version of arm_dma_map_page()
1713  */
1714 static dma_addr_t arm_coherent_iommu_map_page(struct device *dev, struct page *page,
1715 	     unsigned long offset, size_t size, enum dma_data_direction dir,
1716 	     struct dma_attrs *attrs)
1717 {
1718 	struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1719 	dma_addr_t dma_addr;
1720 	int ret, prot, len = PAGE_ALIGN(size + offset);
1721 
1722 	dma_addr = __alloc_iova(mapping, len);
1723 	if (dma_addr == DMA_ERROR_CODE)
1724 		return dma_addr;
1725 
1726 	prot = __dma_direction_to_prot(dir);
1727 
1728 	ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot);
1729 	if (ret < 0)
1730 		goto fail;
1731 
1732 	return dma_addr + offset;
1733 fail:
1734 	__free_iova(mapping, dma_addr, len);
1735 	return DMA_ERROR_CODE;
1736 }
1737 
1738 /**
1739  * arm_iommu_map_page
1740  * @dev: valid struct device pointer
1741  * @page: page that buffer resides in
1742  * @offset: offset into page for start of buffer
1743  * @size: size of buffer to map
1744  * @dir: DMA transfer direction
1745  *
1746  * IOMMU aware version of arm_dma_map_page()
1747  */
1748 static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page,
1749 	     unsigned long offset, size_t size, enum dma_data_direction dir,
1750 	     struct dma_attrs *attrs)
1751 {
1752 	if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1753 		__dma_page_cpu_to_dev(page, offset, size, dir);
1754 
1755 	return arm_coherent_iommu_map_page(dev, page, offset, size, dir, attrs);
1756 }
1757 
1758 /**
1759  * arm_coherent_iommu_unmap_page
1760  * @dev: valid struct device pointer
1761  * @handle: DMA address of buffer
1762  * @size: size of buffer (same as passed to dma_map_page)
1763  * @dir: DMA transfer direction (same as passed to dma_map_page)
1764  *
1765  * Coherent IOMMU aware version of arm_dma_unmap_page()
1766  */
1767 static void arm_coherent_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1768 		size_t size, enum dma_data_direction dir,
1769 		struct dma_attrs *attrs)
1770 {
1771 	struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1772 	dma_addr_t iova = handle & PAGE_MASK;
1773 	int offset = handle & ~PAGE_MASK;
1774 	int len = PAGE_ALIGN(size + offset);
1775 
1776 	if (!iova)
1777 		return;
1778 
1779 	iommu_unmap(mapping->domain, iova, len);
1780 	__free_iova(mapping, iova, len);
1781 }
1782 
1783 /**
1784  * arm_iommu_unmap_page
1785  * @dev: valid struct device pointer
1786  * @handle: DMA address of buffer
1787  * @size: size of buffer (same as passed to dma_map_page)
1788  * @dir: DMA transfer direction (same as passed to dma_map_page)
1789  *
1790  * IOMMU aware version of arm_dma_unmap_page()
1791  */
1792 static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle,
1793 		size_t size, enum dma_data_direction dir,
1794 		struct dma_attrs *attrs)
1795 {
1796 	struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1797 	dma_addr_t iova = handle & PAGE_MASK;
1798 	struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1799 	int offset = handle & ~PAGE_MASK;
1800 	int len = PAGE_ALIGN(size + offset);
1801 
1802 	if (!iova)
1803 		return;
1804 
1805 	if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
1806 		__dma_page_dev_to_cpu(page, offset, size, dir);
1807 
1808 	iommu_unmap(mapping->domain, iova, len);
1809 	__free_iova(mapping, iova, len);
1810 }
1811 
1812 static void arm_iommu_sync_single_for_cpu(struct device *dev,
1813 		dma_addr_t handle, size_t size, enum dma_data_direction dir)
1814 {
1815 	struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1816 	dma_addr_t iova = handle & PAGE_MASK;
1817 	struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1818 	unsigned int offset = handle & ~PAGE_MASK;
1819 
1820 	if (!iova)
1821 		return;
1822 
1823 	__dma_page_dev_to_cpu(page, offset, size, dir);
1824 }
1825 
1826 static void arm_iommu_sync_single_for_device(struct device *dev,
1827 		dma_addr_t handle, size_t size, enum dma_data_direction dir)
1828 {
1829 	struct dma_iommu_mapping *mapping = dev->archdata.mapping;
1830 	dma_addr_t iova = handle & PAGE_MASK;
1831 	struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova));
1832 	unsigned int offset = handle & ~PAGE_MASK;
1833 
1834 	if (!iova)
1835 		return;
1836 
1837 	__dma_page_cpu_to_dev(page, offset, size, dir);
1838 }
1839 
1840 struct dma_map_ops iommu_ops = {
1841 	.alloc		= arm_iommu_alloc_attrs,
1842 	.free		= arm_iommu_free_attrs,
1843 	.mmap		= arm_iommu_mmap_attrs,
1844 	.get_sgtable	= arm_iommu_get_sgtable,
1845 
1846 	.map_page		= arm_iommu_map_page,
1847 	.unmap_page		= arm_iommu_unmap_page,
1848 	.sync_single_for_cpu	= arm_iommu_sync_single_for_cpu,
1849 	.sync_single_for_device	= arm_iommu_sync_single_for_device,
1850 
1851 	.map_sg			= arm_iommu_map_sg,
1852 	.unmap_sg		= arm_iommu_unmap_sg,
1853 	.sync_sg_for_cpu	= arm_iommu_sync_sg_for_cpu,
1854 	.sync_sg_for_device	= arm_iommu_sync_sg_for_device,
1855 
1856 	.set_dma_mask		= arm_dma_set_mask,
1857 };
1858 
1859 struct dma_map_ops iommu_coherent_ops = {
1860 	.alloc		= arm_iommu_alloc_attrs,
1861 	.free		= arm_iommu_free_attrs,
1862 	.mmap		= arm_iommu_mmap_attrs,
1863 	.get_sgtable	= arm_iommu_get_sgtable,
1864 
1865 	.map_page	= arm_coherent_iommu_map_page,
1866 	.unmap_page	= arm_coherent_iommu_unmap_page,
1867 
1868 	.map_sg		= arm_coherent_iommu_map_sg,
1869 	.unmap_sg	= arm_coherent_iommu_unmap_sg,
1870 
1871 	.set_dma_mask	= arm_dma_set_mask,
1872 };
1873 
1874 /**
1875  * arm_iommu_create_mapping
1876  * @bus: pointer to the bus holding the client device (for IOMMU calls)
1877  * @base: start address of the valid IO address space
1878  * @size: size of the valid IO address space
1879  * @order: accuracy of the IO addresses allocations
1880  *
1881  * Creates a mapping structure which holds information about used/unused
1882  * IO address ranges, which is required to perform memory allocation and
1883  * mapping with IOMMU aware functions.
1884  *
1885  * The client device need to be attached to the mapping with
1886  * arm_iommu_attach_device function.
1887  */
1888 struct dma_iommu_mapping *
1889 arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
1890 			 int order)
1891 {
1892 	unsigned int count = size >> (PAGE_SHIFT + order);
1893 	unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
1894 	struct dma_iommu_mapping *mapping;
1895 	int err = -ENOMEM;
1896 
1897 	if (!count)
1898 		return ERR_PTR(-EINVAL);
1899 
1900 	mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
1901 	if (!mapping)
1902 		goto err;
1903 
1904 	mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1905 	if (!mapping->bitmap)
1906 		goto err2;
1907 
1908 	mapping->base = base;
1909 	mapping->bits = BITS_PER_BYTE * bitmap_size;
1910 	mapping->order = order;
1911 	spin_lock_init(&mapping->lock);
1912 
1913 	mapping->domain = iommu_domain_alloc(bus);
1914 	if (!mapping->domain)
1915 		goto err3;
1916 
1917 	kref_init(&mapping->kref);
1918 	return mapping;
1919 err3:
1920 	kfree(mapping->bitmap);
1921 err2:
1922 	kfree(mapping);
1923 err:
1924 	return ERR_PTR(err);
1925 }
1926 EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
1927 
1928 static void release_iommu_mapping(struct kref *kref)
1929 {
1930 	struct dma_iommu_mapping *mapping =
1931 		container_of(kref, struct dma_iommu_mapping, kref);
1932 
1933 	iommu_domain_free(mapping->domain);
1934 	kfree(mapping->bitmap);
1935 	kfree(mapping);
1936 }
1937 
1938 void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
1939 {
1940 	if (mapping)
1941 		kref_put(&mapping->kref, release_iommu_mapping);
1942 }
1943 EXPORT_SYMBOL_GPL(arm_iommu_release_mapping);
1944 
1945 /**
1946  * arm_iommu_attach_device
1947  * @dev: valid struct device pointer
1948  * @mapping: io address space mapping structure (returned from
1949  *	arm_iommu_create_mapping)
1950  *
1951  * Attaches specified io address space mapping to the provided device,
1952  * this replaces the dma operations (dma_map_ops pointer) with the
1953  * IOMMU aware version. More than one client might be attached to
1954  * the same io address space mapping.
1955  */
1956 int arm_iommu_attach_device(struct device *dev,
1957 			    struct dma_iommu_mapping *mapping)
1958 {
1959 	int err;
1960 
1961 	err = iommu_attach_device(mapping->domain, dev);
1962 	if (err)
1963 		return err;
1964 
1965 	kref_get(&mapping->kref);
1966 	dev->archdata.mapping = mapping;
1967 	set_dma_ops(dev, &iommu_ops);
1968 
1969 	pr_debug("Attached IOMMU controller to %s device.\n", dev_name(dev));
1970 	return 0;
1971 }
1972 EXPORT_SYMBOL_GPL(arm_iommu_attach_device);
1973 
1974 /**
1975  * arm_iommu_detach_device
1976  * @dev: valid struct device pointer
1977  *
1978  * Detaches the provided device from a previously attached map.
1979  * This voids the dma operations (dma_map_ops pointer)
1980  */
1981 void arm_iommu_detach_device(struct device *dev)
1982 {
1983 	struct dma_iommu_mapping *mapping;
1984 
1985 	mapping = to_dma_iommu_mapping(dev);
1986 	if (!mapping) {
1987 		dev_warn(dev, "Not attached\n");
1988 		return;
1989 	}
1990 
1991 	iommu_detach_device(mapping->domain, dev);
1992 	kref_put(&mapping->kref, release_iommu_mapping);
1993 	dev->archdata.mapping = NULL;
1994 	set_dma_ops(dev, NULL);
1995 
1996 	pr_debug("Detached IOMMU controller from %s device.\n", dev_name(dev));
1997 }
1998 EXPORT_SYMBOL_GPL(arm_iommu_detach_device);
1999 
2000 #endif
2001