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>
6 #include <asm/pgtable.h>
8 #ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H
9 #include <asm/dma-coherence.h>
10 #elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
11 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
12 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
13 static inline bool dev_is_dma_coherent(struct device *dev)
15 return dev->dma_coherent;
18 static inline bool dev_is_dma_coherent(struct device *dev)
22 #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
25 * Check if an allocation needs to be marked uncached to be coherent.
27 static __always_inline bool dma_alloc_need_uncached(struct device *dev,
30 if (dev_is_dma_coherent(dev))
32 if (attrs & DMA_ATTR_NO_KERNEL_MAPPING)
34 if (IS_ENABLED(CONFIG_DMA_NONCOHERENT_CACHE_SYNC) &&
35 (attrs & DMA_ATTR_NON_CONSISTENT))
40 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
41 gfp_t gfp, unsigned long attrs);
42 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
43 dma_addr_t dma_addr, unsigned long attrs);
44 long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
49 * Page protection so that devices that can't snoop CPU caches can use the
50 * memory coherently. We default to pgprot_noncached which is usually used
51 * for ioremap as a safe bet, but architectures can override this with less
52 * strict semantics if possible.
54 #ifndef pgprot_dmacoherent
55 #define pgprot_dmacoherent(prot) pgprot_noncached(prot)
58 pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs);
60 static inline pgprot_t dma_pgprot(struct device *dev, pgprot_t prot,
63 return prot; /* no protection bits supported without page tables */
65 #endif /* CONFIG_MMU */
67 #ifdef CONFIG_DMA_NONCOHERENT_CACHE_SYNC
68 void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
69 enum dma_data_direction direction);
71 static inline void arch_dma_cache_sync(struct device *dev, void *vaddr,
72 size_t size, enum dma_data_direction direction)
75 #endif /* CONFIG_DMA_NONCOHERENT_CACHE_SYNC */
77 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE
78 void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
79 size_t size, enum dma_data_direction dir);
81 static inline void arch_sync_dma_for_device(struct device *dev,
82 phys_addr_t paddr, size_t size, enum dma_data_direction dir)
85 #endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */
87 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
88 void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
89 size_t size, enum dma_data_direction dir);
91 static inline void arch_sync_dma_for_cpu(struct device *dev,
92 phys_addr_t paddr, size_t size, enum dma_data_direction dir)
95 #endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */
97 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
98 void arch_sync_dma_for_cpu_all(struct device *dev);
100 static inline void arch_sync_dma_for_cpu_all(struct device *dev)
103 #endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */
105 #ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT
106 void arch_dma_prep_coherent(struct page *page, size_t size);
108 static inline void arch_dma_prep_coherent(struct page *page, size_t size)
111 #endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */
113 void *uncached_kernel_address(void *addr);
114 void *cached_kernel_address(void *addr);
116 #endif /* _LINUX_DMA_NONCOHERENT_H */