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