1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_DMA_NONCOHERENT_H
3 #define _LINUX_DMA_NONCOHERENT_H 1
5 #include <linux/dma-mapping.h>
7 #ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H
8 #include <asm/dma-coherence.h>
9 #elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
10 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
11 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
12 static inline bool dev_is_dma_coherent(struct device *dev)
14 return dev->dma_coherent;
17 static inline bool dev_is_dma_coherent(struct device *dev)
21 #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
24 * Check if an allocation needs to be marked uncached to be coherent.
26 static __always_inline bool dma_alloc_need_uncached(struct device *dev,
29 if (dev_is_dma_coherent(dev))
31 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
33 if (IS_ENABLED(CONFIG_DMA_NONCOHERENT_CACHE_SYNC) &&
34 (attrs & DMA_ATTR_NON_CONSISTENT))
39 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
40 gfp_t gfp, unsigned long attrs);
41 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
42 dma_addr_t dma_addr, unsigned long attrs);
43 long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
46 #ifdef CONFIG_ARCH_HAS_DMA_MMAP_PGPROT
47 pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
50 # define arch_dma_mmap_pgprot(dev, prot, attrs) pgprot_noncached(prot)
53 #ifdef CONFIG_DMA_NONCOHERENT_CACHE_SYNC
54 void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
55 enum dma_data_direction direction);
57 static inline void arch_dma_cache_sync(struct device *dev, void *vaddr,
58 size_t size, enum dma_data_direction direction)
61 #endif /* CONFIG_DMA_NONCOHERENT_CACHE_SYNC */
63 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE
64 void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
65 size_t size, enum dma_data_direction dir);
67 static inline void arch_sync_dma_for_device(struct device *dev,
68 phys_addr_t paddr, size_t size, enum dma_data_direction dir)
71 #endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */
73 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
74 void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
75 size_t size, enum dma_data_direction dir);
77 static inline void arch_sync_dma_for_cpu(struct device *dev,
78 phys_addr_t paddr, size_t size, enum dma_data_direction dir)
81 #endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */
83 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
84 void arch_sync_dma_for_cpu_all(struct device *dev);
86 static inline void arch_sync_dma_for_cpu_all(struct device *dev)
89 #endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */
91 #ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT
92 void arch_dma_prep_coherent(struct page *page, size_t size);
94 static inline void arch_dma_prep_coherent(struct page *page, size_t size)
97 #endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */
99 void *uncached_kernel_address(void *addr);
100 void *cached_kernel_address(void *addr);
102 #endif /* _LINUX_DMA_NONCOHERENT_H */