of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / arch / arm64 / mm / dma-mapping.c
blob7963aa4b5d2869b70dfd9a2f3d0ef501a033480b
1 /*
2 * SWIOTLB-based DMA API implementation
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/gfp.h>
21 #include <linux/acpi.h>
22 #include <linux/export.h>
23 #include <linux/slab.h>
24 #include <linux/genalloc.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/dma-contiguous.h>
27 #include <linux/vmalloc.h>
28 #include <linux/swiotlb.h>
30 #include <asm/cacheflush.h>
32 static pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot,
33 bool coherent)
35 if (!coherent || dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs))
36 return pgprot_writecombine(prot);
37 return prot;
40 static struct gen_pool *atomic_pool;
42 #define DEFAULT_DMA_COHERENT_POOL_SIZE SZ_256K
43 static size_t atomic_pool_size = DEFAULT_DMA_COHERENT_POOL_SIZE;
45 static int __init early_coherent_pool(char *p)
47 atomic_pool_size = memparse(p, &p);
48 return 0;
50 early_param("coherent_pool", early_coherent_pool);
52 static void *__alloc_from_pool(size_t size, struct page **ret_page, gfp_t flags)
54 unsigned long val;
55 void *ptr = NULL;
57 if (!atomic_pool) {
58 WARN(1, "coherent pool not initialised!\n");
59 return NULL;
62 val = gen_pool_alloc(atomic_pool, size);
63 if (val) {
64 phys_addr_t phys = gen_pool_virt_to_phys(atomic_pool, val);
66 *ret_page = phys_to_page(phys);
67 ptr = (void *)val;
68 memset(ptr, 0, size);
71 return ptr;
74 static bool __in_atomic_pool(void *start, size_t size)
76 return addr_in_gen_pool(atomic_pool, (unsigned long)start, size);
79 static int __free_from_pool(void *start, size_t size)
81 if (!__in_atomic_pool(start, size))
82 return 0;
84 gen_pool_free(atomic_pool, (unsigned long)start, size);
86 return 1;
89 static void *__dma_alloc_coherent(struct device *dev, size_t size,
90 dma_addr_t *dma_handle, gfp_t flags,
91 struct dma_attrs *attrs)
93 if (dev == NULL) {
94 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
95 return NULL;
98 if (IS_ENABLED(CONFIG_ZONE_DMA) &&
99 dev->coherent_dma_mask <= DMA_BIT_MASK(32))
100 flags |= GFP_DMA;
101 if (dev_get_cma_area(dev) && gfpflags_allow_blocking(flags)) {
102 struct page *page;
103 void *addr;
105 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
106 get_order(size));
107 if (!page)
108 return NULL;
110 *dma_handle = phys_to_dma(dev, page_to_phys(page));
111 addr = page_address(page);
112 memset(addr, 0, size);
113 return addr;
114 } else {
115 return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
119 static void __dma_free_coherent(struct device *dev, size_t size,
120 void *vaddr, dma_addr_t dma_handle,
121 struct dma_attrs *attrs)
123 bool freed;
124 phys_addr_t paddr = dma_to_phys(dev, dma_handle);
126 if (dev == NULL) {
127 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
128 return;
131 freed = dma_release_from_contiguous(dev,
132 phys_to_page(paddr),
133 size >> PAGE_SHIFT);
134 if (!freed)
135 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
138 static void *__dma_alloc(struct device *dev, size_t size,
139 dma_addr_t *dma_handle, gfp_t flags,
140 struct dma_attrs *attrs)
142 struct page *page;
143 void *ptr, *coherent_ptr;
144 bool coherent = is_device_dma_coherent(dev);
145 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, false);
147 size = PAGE_ALIGN(size);
149 if (!coherent && !gfpflags_allow_blocking(flags)) {
150 struct page *page = NULL;
151 void *addr = __alloc_from_pool(size, &page, flags);
153 if (addr)
154 *dma_handle = phys_to_dma(dev, page_to_phys(page));
156 return addr;
159 ptr = __dma_alloc_coherent(dev, size, dma_handle, flags, attrs);
160 if (!ptr)
161 goto no_mem;
163 /* no need for non-cacheable mapping if coherent */
164 if (coherent)
165 return ptr;
167 /* remove any dirty cache lines on the kernel alias */
168 __dma_flush_range(ptr, ptr + size);
170 /* create a coherent mapping */
171 page = virt_to_page(ptr);
172 coherent_ptr = dma_common_contiguous_remap(page, size, VM_USERMAP,
173 prot, NULL);
174 if (!coherent_ptr)
175 goto no_map;
177 return coherent_ptr;
179 no_map:
180 __dma_free_coherent(dev, size, ptr, *dma_handle, attrs);
181 no_mem:
182 *dma_handle = DMA_ERROR_CODE;
183 return NULL;
186 static void __dma_free(struct device *dev, size_t size,
187 void *vaddr, dma_addr_t dma_handle,
188 struct dma_attrs *attrs)
190 void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
192 size = PAGE_ALIGN(size);
194 if (!is_device_dma_coherent(dev)) {
195 if (__free_from_pool(vaddr, size))
196 return;
197 vunmap(vaddr);
199 __dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
202 static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
203 unsigned long offset, size_t size,
204 enum dma_data_direction dir,
205 struct dma_attrs *attrs)
207 dma_addr_t dev_addr;
209 dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
210 if (!is_device_dma_coherent(dev))
211 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
213 return dev_addr;
217 static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
218 size_t size, enum dma_data_direction dir,
219 struct dma_attrs *attrs)
221 if (!is_device_dma_coherent(dev))
222 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
223 swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
226 static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
227 int nelems, enum dma_data_direction dir,
228 struct dma_attrs *attrs)
230 struct scatterlist *sg;
231 int i, ret;
233 ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
234 if (!is_device_dma_coherent(dev))
235 for_each_sg(sgl, sg, ret, i)
236 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
237 sg->length, dir);
239 return ret;
242 static void __swiotlb_unmap_sg_attrs(struct device *dev,
243 struct scatterlist *sgl, int nelems,
244 enum dma_data_direction dir,
245 struct dma_attrs *attrs)
247 struct scatterlist *sg;
248 int i;
250 if (!is_device_dma_coherent(dev))
251 for_each_sg(sgl, sg, nelems, i)
252 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
253 sg->length, dir);
254 swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
257 static void __swiotlb_sync_single_for_cpu(struct device *dev,
258 dma_addr_t dev_addr, size_t size,
259 enum dma_data_direction dir)
261 if (!is_device_dma_coherent(dev))
262 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
263 swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
266 static void __swiotlb_sync_single_for_device(struct device *dev,
267 dma_addr_t dev_addr, size_t size,
268 enum dma_data_direction dir)
270 swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
271 if (!is_device_dma_coherent(dev))
272 __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
275 static void __swiotlb_sync_sg_for_cpu(struct device *dev,
276 struct scatterlist *sgl, int nelems,
277 enum dma_data_direction dir)
279 struct scatterlist *sg;
280 int i;
282 if (!is_device_dma_coherent(dev))
283 for_each_sg(sgl, sg, nelems, i)
284 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
285 sg->length, dir);
286 swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
289 static void __swiotlb_sync_sg_for_device(struct device *dev,
290 struct scatterlist *sgl, int nelems,
291 enum dma_data_direction dir)
293 struct scatterlist *sg;
294 int i;
296 swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
297 if (!is_device_dma_coherent(dev))
298 for_each_sg(sgl, sg, nelems, i)
299 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
300 sg->length, dir);
303 static int __swiotlb_mmap(struct device *dev,
304 struct vm_area_struct *vma,
305 void *cpu_addr, dma_addr_t dma_addr, size_t size,
306 struct dma_attrs *attrs)
308 int ret = -ENXIO;
309 unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
310 PAGE_SHIFT;
311 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
312 unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
313 unsigned long off = vma->vm_pgoff;
315 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
316 is_device_dma_coherent(dev));
318 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
319 return ret;
321 if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
322 ret = remap_pfn_range(vma, vma->vm_start,
323 pfn + off,
324 vma->vm_end - vma->vm_start,
325 vma->vm_page_prot);
328 return ret;
331 static int __swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt,
332 void *cpu_addr, dma_addr_t handle, size_t size,
333 struct dma_attrs *attrs)
335 int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
337 if (!ret)
338 sg_set_page(sgt->sgl, phys_to_page(dma_to_phys(dev, handle)),
339 PAGE_ALIGN(size), 0);
341 return ret;
344 static struct dma_map_ops swiotlb_dma_ops = {
345 .alloc = __dma_alloc,
346 .free = __dma_free,
347 .mmap = __swiotlb_mmap,
348 .get_sgtable = __swiotlb_get_sgtable,
349 .map_page = __swiotlb_map_page,
350 .unmap_page = __swiotlb_unmap_page,
351 .map_sg = __swiotlb_map_sg_attrs,
352 .unmap_sg = __swiotlb_unmap_sg_attrs,
353 .sync_single_for_cpu = __swiotlb_sync_single_for_cpu,
354 .sync_single_for_device = __swiotlb_sync_single_for_device,
355 .sync_sg_for_cpu = __swiotlb_sync_sg_for_cpu,
356 .sync_sg_for_device = __swiotlb_sync_sg_for_device,
357 .dma_supported = swiotlb_dma_supported,
358 .mapping_error = swiotlb_dma_mapping_error,
361 static int __init atomic_pool_init(void)
363 pgprot_t prot = __pgprot(PROT_NORMAL_NC);
364 unsigned long nr_pages = atomic_pool_size >> PAGE_SHIFT;
365 struct page *page;
366 void *addr;
367 unsigned int pool_size_order = get_order(atomic_pool_size);
369 if (dev_get_cma_area(NULL))
370 page = dma_alloc_from_contiguous(NULL, nr_pages,
371 pool_size_order);
372 else
373 page = alloc_pages(GFP_DMA, pool_size_order);
375 if (page) {
376 int ret;
377 void *page_addr = page_address(page);
379 memset(page_addr, 0, atomic_pool_size);
380 __dma_flush_range(page_addr, page_addr + atomic_pool_size);
382 atomic_pool = gen_pool_create(PAGE_SHIFT, -1);
383 if (!atomic_pool)
384 goto free_page;
386 addr = dma_common_contiguous_remap(page, atomic_pool_size,
387 VM_USERMAP, prot, atomic_pool_init);
389 if (!addr)
390 goto destroy_genpool;
392 ret = gen_pool_add_virt(atomic_pool, (unsigned long)addr,
393 page_to_phys(page),
394 atomic_pool_size, -1);
395 if (ret)
396 goto remove_mapping;
398 gen_pool_set_algo(atomic_pool,
399 gen_pool_first_fit_order_align,
400 (void *)PAGE_SHIFT);
402 pr_info("DMA: preallocated %zu KiB pool for atomic allocations\n",
403 atomic_pool_size / 1024);
404 return 0;
406 goto out;
408 remove_mapping:
409 dma_common_free_remap(addr, atomic_pool_size, VM_USERMAP);
410 destroy_genpool:
411 gen_pool_destroy(atomic_pool);
412 atomic_pool = NULL;
413 free_page:
414 if (!dma_release_from_contiguous(NULL, page, nr_pages))
415 __free_pages(page, pool_size_order);
416 out:
417 pr_err("DMA: failed to allocate %zu KiB pool for atomic coherent allocation\n",
418 atomic_pool_size / 1024);
419 return -ENOMEM;
422 /********************************************
423 * The following APIs are for dummy DMA ops *
424 ********************************************/
426 static void *__dummy_alloc(struct device *dev, size_t size,
427 dma_addr_t *dma_handle, gfp_t flags,
428 struct dma_attrs *attrs)
430 return NULL;
433 static void __dummy_free(struct device *dev, size_t size,
434 void *vaddr, dma_addr_t dma_handle,
435 struct dma_attrs *attrs)
439 static int __dummy_mmap(struct device *dev,
440 struct vm_area_struct *vma,
441 void *cpu_addr, dma_addr_t dma_addr, size_t size,
442 struct dma_attrs *attrs)
444 return -ENXIO;
447 static dma_addr_t __dummy_map_page(struct device *dev, struct page *page,
448 unsigned long offset, size_t size,
449 enum dma_data_direction dir,
450 struct dma_attrs *attrs)
452 return DMA_ERROR_CODE;
455 static void __dummy_unmap_page(struct device *dev, dma_addr_t dev_addr,
456 size_t size, enum dma_data_direction dir,
457 struct dma_attrs *attrs)
461 static int __dummy_map_sg(struct device *dev, struct scatterlist *sgl,
462 int nelems, enum dma_data_direction dir,
463 struct dma_attrs *attrs)
465 return 0;
468 static void __dummy_unmap_sg(struct device *dev,
469 struct scatterlist *sgl, int nelems,
470 enum dma_data_direction dir,
471 struct dma_attrs *attrs)
475 static void __dummy_sync_single(struct device *dev,
476 dma_addr_t dev_addr, size_t size,
477 enum dma_data_direction dir)
481 static void __dummy_sync_sg(struct device *dev,
482 struct scatterlist *sgl, int nelems,
483 enum dma_data_direction dir)
487 static int __dummy_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
489 return 1;
492 static int __dummy_dma_supported(struct device *hwdev, u64 mask)
494 return 0;
497 struct dma_map_ops dummy_dma_ops = {
498 .alloc = __dummy_alloc,
499 .free = __dummy_free,
500 .mmap = __dummy_mmap,
501 .map_page = __dummy_map_page,
502 .unmap_page = __dummy_unmap_page,
503 .map_sg = __dummy_map_sg,
504 .unmap_sg = __dummy_unmap_sg,
505 .sync_single_for_cpu = __dummy_sync_single,
506 .sync_single_for_device = __dummy_sync_single,
507 .sync_sg_for_cpu = __dummy_sync_sg,
508 .sync_sg_for_device = __dummy_sync_sg,
509 .mapping_error = __dummy_mapping_error,
510 .dma_supported = __dummy_dma_supported,
512 EXPORT_SYMBOL(dummy_dma_ops);
514 static int __init arm64_dma_init(void)
516 return atomic_pool_init();
518 arch_initcall(arm64_dma_init);
520 #define PREALLOC_DMA_DEBUG_ENTRIES 4096
522 static int __init dma_debug_do_init(void)
524 dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
525 return 0;
527 fs_initcall(dma_debug_do_init);
530 #ifdef CONFIG_IOMMU_DMA
531 #include <linux/dma-iommu.h>
532 #include <linux/platform_device.h>
533 #include <linux/amba/bus.h>
535 /* Thankfully, all cache ops are by VA so we can ignore phys here */
536 static void flush_page(struct device *dev, const void *virt, phys_addr_t phys)
538 __dma_flush_range(virt, virt + PAGE_SIZE);
541 static void *__iommu_alloc_attrs(struct device *dev, size_t size,
542 dma_addr_t *handle, gfp_t gfp,
543 struct dma_attrs *attrs)
545 bool coherent = is_device_dma_coherent(dev);
546 int ioprot = dma_direction_to_prot(DMA_BIDIRECTIONAL, coherent);
547 size_t iosize = size;
548 void *addr;
550 if (WARN(!dev, "cannot create IOMMU mapping for unknown device\n"))
551 return NULL;
553 size = PAGE_ALIGN(size);
556 * Some drivers rely on this, and we probably don't want the
557 * possibility of stale kernel data being read by devices anyway.
559 gfp |= __GFP_ZERO;
561 if (gfpflags_allow_blocking(gfp)) {
562 struct page **pages;
563 pgprot_t prot = __get_dma_pgprot(attrs, PAGE_KERNEL, coherent);
565 pages = iommu_dma_alloc(dev, iosize, gfp, ioprot, handle,
566 flush_page);
567 if (!pages)
568 return NULL;
570 addr = dma_common_pages_remap(pages, size, VM_USERMAP, prot,
571 __builtin_return_address(0));
572 if (!addr)
573 iommu_dma_free(dev, pages, iosize, handle);
574 } else {
575 struct page *page;
577 * In atomic context we can't remap anything, so we'll only
578 * get the virtually contiguous buffer we need by way of a
579 * physically contiguous allocation.
581 if (coherent) {
582 page = alloc_pages(gfp, get_order(size));
583 addr = page ? page_address(page) : NULL;
584 } else {
585 addr = __alloc_from_pool(size, &page, gfp);
587 if (!addr)
588 return NULL;
590 *handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
591 if (iommu_dma_mapping_error(dev, *handle)) {
592 if (coherent)
593 __free_pages(page, get_order(size));
594 else
595 __free_from_pool(addr, size);
596 addr = NULL;
599 return addr;
602 static void __iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
603 dma_addr_t handle, struct dma_attrs *attrs)
605 size_t iosize = size;
607 size = PAGE_ALIGN(size);
609 * @cpu_addr will be one of 3 things depending on how it was allocated:
610 * - A remapped array of pages from iommu_dma_alloc(), for all
611 * non-atomic allocations.
612 * - A non-cacheable alias from the atomic pool, for atomic
613 * allocations by non-coherent devices.
614 * - A normal lowmem address, for atomic allocations by
615 * coherent devices.
616 * Hence how dodgy the below logic looks...
618 if (__in_atomic_pool(cpu_addr, size)) {
619 iommu_dma_unmap_page(dev, handle, iosize, 0, NULL);
620 __free_from_pool(cpu_addr, size);
621 } else if (is_vmalloc_addr(cpu_addr)){
622 struct vm_struct *area = find_vm_area(cpu_addr);
624 if (WARN_ON(!area || !area->pages))
625 return;
626 iommu_dma_free(dev, area->pages, iosize, &handle);
627 dma_common_free_remap(cpu_addr, size, VM_USERMAP);
628 } else {
629 iommu_dma_unmap_page(dev, handle, iosize, 0, NULL);
630 __free_pages(virt_to_page(cpu_addr), get_order(size));
634 static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
635 void *cpu_addr, dma_addr_t dma_addr, size_t size,
636 struct dma_attrs *attrs)
638 struct vm_struct *area;
639 int ret;
641 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
642 is_device_dma_coherent(dev));
644 if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
645 return ret;
647 area = find_vm_area(cpu_addr);
648 if (WARN_ON(!area || !area->pages))
649 return -ENXIO;
651 return iommu_dma_mmap(area->pages, size, vma);
654 static int __iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
655 void *cpu_addr, dma_addr_t dma_addr,
656 size_t size, struct dma_attrs *attrs)
658 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
659 struct vm_struct *area = find_vm_area(cpu_addr);
661 if (WARN_ON(!area || !area->pages))
662 return -ENXIO;
664 return sg_alloc_table_from_pages(sgt, area->pages, count, 0, size,
665 GFP_KERNEL);
668 static void __iommu_sync_single_for_cpu(struct device *dev,
669 dma_addr_t dev_addr, size_t size,
670 enum dma_data_direction dir)
672 phys_addr_t phys;
674 if (is_device_dma_coherent(dev))
675 return;
677 phys = iommu_iova_to_phys(iommu_get_domain_for_dev(dev), dev_addr);
678 __dma_unmap_area(phys_to_virt(phys), size, dir);
681 static void __iommu_sync_single_for_device(struct device *dev,
682 dma_addr_t dev_addr, size_t size,
683 enum dma_data_direction dir)
685 phys_addr_t phys;
687 if (is_device_dma_coherent(dev))
688 return;
690 phys = iommu_iova_to_phys(iommu_get_domain_for_dev(dev), dev_addr);
691 __dma_map_area(phys_to_virt(phys), size, dir);
694 static dma_addr_t __iommu_map_page(struct device *dev, struct page *page,
695 unsigned long offset, size_t size,
696 enum dma_data_direction dir,
697 struct dma_attrs *attrs)
699 bool coherent = is_device_dma_coherent(dev);
700 int prot = dma_direction_to_prot(dir, coherent);
701 dma_addr_t dev_addr = iommu_dma_map_page(dev, page, offset, size, prot);
703 if (!iommu_dma_mapping_error(dev, dev_addr) &&
704 !dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
705 __iommu_sync_single_for_device(dev, dev_addr, size, dir);
707 return dev_addr;
710 static void __iommu_unmap_page(struct device *dev, dma_addr_t dev_addr,
711 size_t size, enum dma_data_direction dir,
712 struct dma_attrs *attrs)
714 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
715 __iommu_sync_single_for_cpu(dev, dev_addr, size, dir);
717 iommu_dma_unmap_page(dev, dev_addr, size, dir, attrs);
720 static void __iommu_sync_sg_for_cpu(struct device *dev,
721 struct scatterlist *sgl, int nelems,
722 enum dma_data_direction dir)
724 struct scatterlist *sg;
725 int i;
727 if (is_device_dma_coherent(dev))
728 return;
730 for_each_sg(sgl, sg, nelems, i)
731 __dma_unmap_area(sg_virt(sg), sg->length, dir);
734 static void __iommu_sync_sg_for_device(struct device *dev,
735 struct scatterlist *sgl, int nelems,
736 enum dma_data_direction dir)
738 struct scatterlist *sg;
739 int i;
741 if (is_device_dma_coherent(dev))
742 return;
744 for_each_sg(sgl, sg, nelems, i)
745 __dma_map_area(sg_virt(sg), sg->length, dir);
748 static int __iommu_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
749 int nelems, enum dma_data_direction dir,
750 struct dma_attrs *attrs)
752 bool coherent = is_device_dma_coherent(dev);
754 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
755 __iommu_sync_sg_for_device(dev, sgl, nelems, dir);
757 return iommu_dma_map_sg(dev, sgl, nelems,
758 dma_direction_to_prot(dir, coherent));
761 static void __iommu_unmap_sg_attrs(struct device *dev,
762 struct scatterlist *sgl, int nelems,
763 enum dma_data_direction dir,
764 struct dma_attrs *attrs)
766 if (!dma_get_attr(DMA_ATTR_SKIP_CPU_SYNC, attrs))
767 __iommu_sync_sg_for_cpu(dev, sgl, nelems, dir);
769 iommu_dma_unmap_sg(dev, sgl, nelems, dir, attrs);
772 static struct dma_map_ops iommu_dma_ops = {
773 .alloc = __iommu_alloc_attrs,
774 .free = __iommu_free_attrs,
775 .mmap = __iommu_mmap_attrs,
776 .get_sgtable = __iommu_get_sgtable,
777 .map_page = __iommu_map_page,
778 .unmap_page = __iommu_unmap_page,
779 .map_sg = __iommu_map_sg_attrs,
780 .unmap_sg = __iommu_unmap_sg_attrs,
781 .sync_single_for_cpu = __iommu_sync_single_for_cpu,
782 .sync_single_for_device = __iommu_sync_single_for_device,
783 .sync_sg_for_cpu = __iommu_sync_sg_for_cpu,
784 .sync_sg_for_device = __iommu_sync_sg_for_device,
785 .dma_supported = iommu_dma_supported,
786 .mapping_error = iommu_dma_mapping_error,
790 * TODO: Right now __iommu_setup_dma_ops() gets called too early to do
791 * everything it needs to - the device is only partially created and the
792 * IOMMU driver hasn't seen it yet, so it can't have a group. Thus we
793 * need this delayed attachment dance. Once IOMMU probe ordering is sorted
794 * to move the arch_setup_dma_ops() call later, all the notifier bits below
795 * become unnecessary, and will go away.
797 struct iommu_dma_notifier_data {
798 struct list_head list;
799 struct device *dev;
800 const struct iommu_ops *ops;
801 u64 dma_base;
802 u64 size;
804 static LIST_HEAD(iommu_dma_masters);
805 static DEFINE_MUTEX(iommu_dma_notifier_lock);
808 * Temporarily "borrow" a domain feature flag to to tell if we had to resort
809 * to creating our own domain here, in case we need to clean it up again.
811 #define __IOMMU_DOMAIN_FAKE_DEFAULT (1U << 31)
813 static bool do_iommu_attach(struct device *dev, const struct iommu_ops *ops,
814 u64 dma_base, u64 size)
816 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
819 * Best case: The device is either part of a group which was
820 * already attached to a domain in a previous call, or it's
821 * been put in a default DMA domain by the IOMMU core.
823 if (!domain) {
825 * Urgh. The IOMMU core isn't going to do default domains
826 * for non-PCI devices anyway, until it has some means of
827 * abstracting the entirely implementation-specific
828 * sideband data/SoC topology/unicorn dust that may or
829 * may not differentiate upstream masters.
830 * So until then, HORRIBLE HACKS!
832 domain = ops->domain_alloc(IOMMU_DOMAIN_DMA);
833 if (!domain)
834 goto out_no_domain;
836 domain->ops = ops;
837 domain->type = IOMMU_DOMAIN_DMA | __IOMMU_DOMAIN_FAKE_DEFAULT;
839 if (iommu_attach_device(domain, dev))
840 goto out_put_domain;
843 if (iommu_dma_init_domain(domain, dma_base, size))
844 goto out_detach;
846 dev->archdata.dma_ops = &iommu_dma_ops;
847 return true;
849 out_detach:
850 iommu_detach_device(domain, dev);
851 out_put_domain:
852 if (domain->type & __IOMMU_DOMAIN_FAKE_DEFAULT)
853 iommu_domain_free(domain);
854 out_no_domain:
855 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
856 dev_name(dev));
857 return false;
860 static void queue_iommu_attach(struct device *dev, const struct iommu_ops *ops,
861 u64 dma_base, u64 size)
863 struct iommu_dma_notifier_data *iommudata;
865 iommudata = kzalloc(sizeof(*iommudata), GFP_KERNEL);
866 if (!iommudata)
867 return;
869 iommudata->dev = dev;
870 iommudata->ops = ops;
871 iommudata->dma_base = dma_base;
872 iommudata->size = size;
874 mutex_lock(&iommu_dma_notifier_lock);
875 list_add(&iommudata->list, &iommu_dma_masters);
876 mutex_unlock(&iommu_dma_notifier_lock);
879 static int __iommu_attach_notifier(struct notifier_block *nb,
880 unsigned long action, void *data)
882 struct iommu_dma_notifier_data *master, *tmp;
884 if (action != BUS_NOTIFY_ADD_DEVICE)
885 return 0;
887 mutex_lock(&iommu_dma_notifier_lock);
888 list_for_each_entry_safe(master, tmp, &iommu_dma_masters, list) {
889 if (do_iommu_attach(master->dev, master->ops,
890 master->dma_base, master->size)) {
891 list_del(&master->list);
892 kfree(master);
895 mutex_unlock(&iommu_dma_notifier_lock);
896 return 0;
899 static int register_iommu_dma_ops_notifier(struct bus_type *bus)
901 struct notifier_block *nb = kzalloc(sizeof(*nb), GFP_KERNEL);
902 int ret;
904 if (!nb)
905 return -ENOMEM;
907 * The device must be attached to a domain before the driver probe
908 * routine gets a chance to start allocating DMA buffers. However,
909 * the IOMMU driver also needs a chance to configure the iommu_group
910 * via its add_device callback first, so we need to make the attach
911 * happen between those two points. Since the IOMMU core uses a bus
912 * notifier with default priority for add_device, do the same but
913 * with a lower priority to ensure the appropriate ordering.
915 nb->notifier_call = __iommu_attach_notifier;
916 nb->priority = -100;
918 ret = bus_register_notifier(bus, nb);
919 if (ret) {
920 pr_warn("Failed to register DMA domain notifier; IOMMU DMA ops unavailable on bus '%s'\n",
921 bus->name);
922 kfree(nb);
924 return ret;
927 static int __init __iommu_dma_init(void)
929 int ret;
931 ret = iommu_dma_init();
932 if (!ret)
933 ret = register_iommu_dma_ops_notifier(&platform_bus_type);
934 if (!ret)
935 ret = register_iommu_dma_ops_notifier(&amba_bustype);
936 return ret;
938 arch_initcall(__iommu_dma_init);
940 static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
941 const struct iommu_ops *ops)
943 struct iommu_group *group;
945 if (!ops)
946 return;
948 * TODO: As a concession to the future, we're ready to handle being
949 * called both early and late (i.e. after bus_add_device). Once all
950 * the platform bus code is reworked to call us late and the notifier
951 * junk above goes away, move the body of do_iommu_attach here.
953 group = iommu_group_get(dev);
954 if (group) {
955 do_iommu_attach(dev, ops, dma_base, size);
956 iommu_group_put(group);
957 } else {
958 queue_iommu_attach(dev, ops, dma_base, size);
962 void arch_teardown_dma_ops(struct device *dev)
964 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
966 if (domain) {
967 iommu_detach_device(domain, dev);
968 if (domain->type & __IOMMU_DOMAIN_FAKE_DEFAULT)
969 iommu_domain_free(domain);
972 dev->archdata.dma_ops = NULL;
975 #else
977 static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
978 struct iommu_ops *iommu)
981 #endif /* CONFIG_IOMMU_DMA */
983 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
984 struct iommu_ops *iommu, bool coherent)
986 if (!dev->archdata.dma_ops)
987 dev->archdata.dma_ops = &swiotlb_dma_ops;
989 dev->archdata.dma_coherent = coherent;
990 __iommu_setup_dma_ops(dev, dma_base, size, iommu);