1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 ARM Ltd.
4 * Author: Catalin Marinas <catalin.marinas@arm.com>
5 */
6
7 #include <linux/gfp.h>
8 #include <linux/cache.h>
9 #include <linux/dma-map-ops.h>
10 #include <linux/dma-iommu.h>
11 #include <xen/xen.h>
12 #include <xen/swiotlb-xen.h>
13 #include <trace/hooks/iommu.h>
14 #include <trace/hooks/dma_noalias.h>
15
16 #include <asm/cacheflush.h>
17
arch_sync_dma_for_device(phys_addr_t paddr,size_t size,enum dma_data_direction dir)18 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
19 enum dma_data_direction dir)
20 {
21 __dma_map_area(phys_to_virt(paddr), size, dir);
22 }
23
arch_sync_dma_for_cpu(phys_addr_t paddr,size_t size,enum dma_data_direction dir)24 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
25 enum dma_data_direction dir)
26 {
27 __dma_unmap_area(phys_to_virt(paddr), size, dir);
28 }
29
arch_dma_prep_coherent(struct page * page,size_t size)30 void arch_dma_prep_coherent(struct page *page, size_t size)
31 {
32 __dma_flush_area(page_address(page), size);
33 }
34
35 #ifdef CONFIG_IOMMU_DMA
arch_teardown_dma_ops(struct device * dev)36 void arch_teardown_dma_ops(struct device *dev)
37 {
38 dev->dma_ops = NULL;
39 }
40 #endif
41
arch_setup_dma_ops(struct device * dev,u64 dma_base,u64 size,const struct iommu_ops * iommu,bool coherent)42 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
43 const struct iommu_ops *iommu, bool coherent)
44 {
45 int cls = cache_line_size_of_cpu();
46
47 WARN_TAINT(!coherent && cls > ARCH_DMA_MINALIGN,
48 TAINT_CPU_OUT_OF_SPEC,
49 "%s %s: ARCH_DMA_MINALIGN smaller than CTR_EL0.CWG (%d < %d)",
50 dev_driver_string(dev), dev_name(dev),
51 ARCH_DMA_MINALIGN, cls);
52
53 dev->dma_coherent = coherent;
54 if (iommu) {
55 iommu_setup_dma_ops(dev, dma_base, size);
56 trace_android_vh_iommu_setup_dma_ops(dev, dma_base, size);
57 trace_android_rvh_iommu_setup_dma_ops(dev, dma_base, size);
58 }
59
60 /* Allow vendor modules to opt-in for the 2454944 erratum workaround */
61 trace_android_rvh_setup_dma_ops(dev);
62
63 #ifdef CONFIG_XEN
64 if (xen_initial_domain())
65 dev->dma_ops = &xen_swiotlb_dma_ops;
66 #endif
67 }
68
69 #ifdef CONFIG_NO_GKI
70 EXPORT_SYMBOL(__dma_map_area);
71 EXPORT_SYMBOL(__dma_unmap_area);
72 #endif
73