blob: 84c14dbcdb66dd7f74aca8768d9c8941965f8b5d [file] [log] [blame]
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +02001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * This header is for implementations of dma_map_ops and related code.
4 * It should not be included in drivers just using the DMA API.
5 */
6#ifndef _LINUX_DMA_MAP_OPS_H
7#define _LINUX_DMA_MAP_OPS_H
8
9#include <linux/dma-mapping.h>
Christoph Hellwig9f4df962020-09-22 15:36:11 +020010#include <linux/pgtable.h>
Greg Kroah-Hartman774168d2020-05-02 09:36:40 +020011#include <linux/android_kabi.h>
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +020012
Christoph Hellwig0b1abd12020-09-11 10:56:52 +020013struct cma;
14
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +020015struct dma_map_ops {
16 void *(*alloc)(struct device *dev, size_t size,
17 dma_addr_t *dma_handle, gfp_t gfp,
18 unsigned long attrs);
19 void (*free)(struct device *dev, size_t size, void *vaddr,
20 dma_addr_t dma_handle, unsigned long attrs);
21 struct page *(*alloc_pages)(struct device *dev, size_t size,
22 dma_addr_t *dma_handle, enum dma_data_direction dir,
23 gfp_t gfp);
24 void (*free_pages)(struct device *dev, size_t size, struct page *vaddr,
25 dma_addr_t dma_handle, enum dma_data_direction dir);
26 void *(*alloc_noncoherent)(struct device *dev, size_t size,
27 dma_addr_t *dma_handle, enum dma_data_direction dir,
28 gfp_t gfp);
29 void (*free_noncoherent)(struct device *dev, size_t size, void *vaddr,
30 dma_addr_t dma_handle, enum dma_data_direction dir);
31 int (*mmap)(struct device *, struct vm_area_struct *,
32 void *, dma_addr_t, size_t, unsigned long attrs);
33
34 int (*get_sgtable)(struct device *dev, struct sg_table *sgt,
35 void *cpu_addr, dma_addr_t dma_addr, size_t size,
36 unsigned long attrs);
37
38 dma_addr_t (*map_page)(struct device *dev, struct page *page,
39 unsigned long offset, size_t size,
40 enum dma_data_direction dir, unsigned long attrs);
41 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
42 size_t size, enum dma_data_direction dir,
43 unsigned long attrs);
44 /*
45 * map_sg returns 0 on error and a value > 0 on success.
46 * It should never return a value < 0.
47 */
48 int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents,
49 enum dma_data_direction dir, unsigned long attrs);
50 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
51 enum dma_data_direction dir, unsigned long attrs);
52 dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
53 size_t size, enum dma_data_direction dir,
54 unsigned long attrs);
55 void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
56 size_t size, enum dma_data_direction dir,
57 unsigned long attrs);
58 void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
59 size_t size, enum dma_data_direction dir);
60 void (*sync_single_for_device)(struct device *dev,
61 dma_addr_t dma_handle, size_t size,
62 enum dma_data_direction dir);
63 void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
64 int nents, enum dma_data_direction dir);
65 void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
66 int nents, enum dma_data_direction dir);
67 void (*cache_sync)(struct device *dev, void *vaddr, size_t size,
68 enum dma_data_direction direction);
69 int (*dma_supported)(struct device *dev, u64 mask);
70 u64 (*get_required_mask)(struct device *dev);
71 size_t (*max_mapping_size)(struct device *dev);
72 unsigned long (*get_merge_boundary)(struct device *dev);
Greg Kroah-Hartman774168d2020-05-02 09:36:40 +020073
74 ANDROID_KABI_RESERVE(1);
75 ANDROID_KABI_RESERVE(2);
76 ANDROID_KABI_RESERVE(3);
77 ANDROID_KABI_RESERVE(4);
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +020078};
79
80#ifdef CONFIG_DMA_OPS
81#include <asm/dma-mapping.h>
82
83static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
84{
85 if (dev->dma_ops)
86 return dev->dma_ops;
87 return get_arch_dma_ops(dev->bus);
88}
89
90static inline void set_dma_ops(struct device *dev,
91 const struct dma_map_ops *dma_ops)
92{
93 dev->dma_ops = dma_ops;
94}
95#else /* CONFIG_DMA_OPS */
96static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
97{
98 return NULL;
99}
100static inline void set_dma_ops(struct device *dev,
101 const struct dma_map_ops *dma_ops)
102{
103}
104#endif /* CONFIG_DMA_OPS */
105
Christoph Hellwig0b1abd12020-09-11 10:56:52 +0200106#ifdef CONFIG_DMA_CMA
107extern struct cma *dma_contiguous_default_area;
108
109static inline struct cma *dev_get_cma_area(struct device *dev)
110{
111 if (dev && dev->cma_area)
112 return dev->cma_area;
113 return dma_contiguous_default_area;
114}
115
116void dma_contiguous_reserve(phys_addr_t addr_limit);
117int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
118 phys_addr_t limit, struct cma **res_cma, bool fixed);
119
120struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
121 unsigned int order, bool no_warn);
122bool dma_release_from_contiguous(struct device *dev, struct page *pages,
123 int count);
124struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
125void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
Christoph Hellwig5db5d932020-09-11 11:04:43 +0200126
127void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size);
Christoph Hellwig0b1abd12020-09-11 10:56:52 +0200128#else /* CONFIG_DMA_CMA */
129static inline struct cma *dev_get_cma_area(struct device *dev)
130{
131 return NULL;
132}
133static inline void dma_contiguous_reserve(phys_addr_t limit)
134{
135}
136static inline int dma_contiguous_reserve_area(phys_addr_t size,
137 phys_addr_t base, phys_addr_t limit, struct cma **res_cma,
138 bool fixed)
139{
140 return -ENOSYS;
141}
142static inline struct page *dma_alloc_from_contiguous(struct device *dev,
143 size_t count, unsigned int order, bool no_warn)
144{
145 return NULL;
146}
147static inline bool dma_release_from_contiguous(struct device *dev,
148 struct page *pages, int count)
149{
150 return false;
151}
152/* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */
153static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,
154 gfp_t gfp)
155{
156 return NULL;
157}
158static inline void dma_free_contiguous(struct device *dev, struct page *page,
159 size_t size)
160{
161 __free_pages(page, get_order(size));
162}
163#endif /* CONFIG_DMA_CMA*/
164
165#ifdef CONFIG_DMA_PERNUMA_CMA
166void dma_pernuma_cma_reserve(void);
167#else
168static inline void dma_pernuma_cma_reserve(void) { }
169#endif /* CONFIG_DMA_PERNUMA_CMA */
170
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +0200171#ifdef CONFIG_DMA_DECLARE_COHERENT
172int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
173 dma_addr_t device_addr, size_t size);
174int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
175 dma_addr_t *dma_handle, void **ret);
176int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
177int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
178 void *cpu_addr, size_t size, int *ret);
179
180void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size,
181 dma_addr_t *dma_handle);
182int dma_release_from_global_coherent(int order, void *vaddr);
183int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
184 size_t size, int *ret);
185
186#else
187static inline int dma_declare_coherent_memory(struct device *dev,
188 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size)
189{
190 return -ENOSYS;
191}
192#define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0)
193#define dma_release_from_dev_coherent(dev, order, vaddr) (0)
194#define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0)
195
196static inline void *dma_alloc_from_global_coherent(struct device *dev,
197 ssize_t size, dma_addr_t *dma_handle)
198{
199 return NULL;
200}
201static inline int dma_release_from_global_coherent(int order, void *vaddr)
202{
203 return 0;
204}
205static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
206 void *cpu_addr, size_t size, int *ret)
207{
208 return 0;
209}
210#endif /* CONFIG_DMA_DECLARE_COHERENT */
211
Christoph Hellwig695cebe2020-10-20 10:41:07 +0200212int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
213 void *cpu_addr, dma_addr_t dma_addr, size_t size,
214 unsigned long attrs);
215int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
216 void *cpu_addr, dma_addr_t dma_addr, size_t size,
217 unsigned long attrs);
218struct page *dma_common_alloc_pages(struct device *dev, size_t size,
219 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
220void dma_common_free_pages(struct device *dev, size_t size, struct page *vaddr,
221 dma_addr_t dma_handle, enum dma_data_direction dir);
222
223struct page **dma_common_find_pages(void *cpu_addr);
224void *dma_common_contiguous_remap(struct page *page, size_t size, pgprot_t prot,
225 const void *caller);
226void *dma_common_pages_remap(struct page **pages, size_t size, pgprot_t prot,
227 const void *caller);
228void dma_common_free_remap(void *cpu_addr, size_t size);
229
230struct page *dma_alloc_from_pool(struct device *dev, size_t size,
231 void **cpu_addr, gfp_t flags,
232 bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t));
233bool dma_free_from_pool(struct device *dev, void *start, size_t size);
234
Christoph Hellwig9f4df962020-09-22 15:36:11 +0200235#ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H
236#include <asm/dma-coherence.h>
237#elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
238 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
239 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
240static inline bool dev_is_dma_coherent(struct device *dev)
241{
242 return dev->dma_coherent;
243}
244#else
245static inline bool dev_is_dma_coherent(struct device *dev)
246{
247 return true;
248}
249#endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
250
Christoph Hellwig9f4df962020-09-22 15:36:11 +0200251void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
252 gfp_t gfp, unsigned long attrs);
253void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
254 dma_addr_t dma_addr, unsigned long attrs);
255
256#ifdef CONFIG_MMU
257/*
258 * Page protection so that devices that can't snoop CPU caches can use the
259 * memory coherently. We default to pgprot_noncached which is usually used
260 * for ioremap as a safe bet, but architectures can override this with less
261 * strict semantics if possible.
262 */
263#ifndef pgprot_dmacoherent
264#define pgprot_dmacoherent(prot) pgprot_noncached(prot)
265#endif
266
Isaac J. Manjarres261c5082021-01-12 14:17:41 -0800267/*
268 * If there is no system cache pgprot, then fallback to dmacoherent
269 * pgprot, as the expectation is that the device is not coherent.
270 */
271#ifndef pgprot_syscached
272#define pgprot_syscached(prot) pgprot_dmacoherent(prot)
273#endif
274
Christoph Hellwig9f4df962020-09-22 15:36:11 +0200275pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs);
276#else
277static inline pgprot_t dma_pgprot(struct device *dev, pgprot_t prot,
278 unsigned long attrs)
279{
280 return prot; /* no protection bits supported without page tables */
281}
282#endif /* CONFIG_MMU */
283
284#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE
285void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
286 enum dma_data_direction dir);
287#else
288static inline void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
289 enum dma_data_direction dir)
290{
291}
292#endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */
293
294#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
295void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
296 enum dma_data_direction dir);
297#else
298static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
299 enum dma_data_direction dir)
300{
301}
302#endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */
303
304#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
305void arch_sync_dma_for_cpu_all(void);
306#else
307static inline void arch_sync_dma_for_cpu_all(void)
308{
309}
310#endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */
311
312#ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT
313void arch_dma_prep_coherent(struct page *page, size_t size);
314#else
315static inline void arch_dma_prep_coherent(struct page *page, size_t size)
316{
317}
318#endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */
319
320#ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN
321void arch_dma_mark_clean(phys_addr_t paddr, size_t size);
322#else
323static inline void arch_dma_mark_clean(phys_addr_t paddr, size_t size)
324{
325}
326#endif /* ARCH_HAS_DMA_MARK_CLEAN */
327
328void *arch_dma_set_uncached(void *addr, size_t size);
329void arch_dma_clear_uncached(void *addr, size_t size);
330
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +0200331#ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
332void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
333 const struct iommu_ops *iommu, bool coherent);
334#else
335static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
336 u64 size, const struct iommu_ops *iommu, bool coherent)
337{
338}
339#endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
340
341#ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS
342void arch_teardown_dma_ops(struct device *dev);
343#else
344static inline void arch_teardown_dma_ops(struct device *dev)
345{
346}
347#endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */
348
Christoph Hellwiga1fd09e2020-09-11 10:12:44 +0200349#ifdef CONFIG_DMA_API_DEBUG
350void dma_debug_add_bus(struct bus_type *bus);
351void debug_dma_dump_mappings(struct device *dev);
352#else
353static inline void dma_debug_add_bus(struct bus_type *bus)
354{
355}
356static inline void debug_dma_dump_mappings(struct device *dev)
357{
358}
359#endif /* CONFIG_DMA_API_DEBUG */
360
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +0200361extern const struct dma_map_ops dma_dummy_ops;
362
363#endif /* _LINUX_DMA_MAP_OPS_H */