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