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