1 /* 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>
10 #include <linux/pgtable.h>
11 #include <linux/android_kabi.h>
12
13 struct cma;
14
15 struct 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);
73
74 ANDROID_KABI_RESERVE(1);
75 ANDROID_KABI_RESERVE(2);
76 ANDROID_KABI_RESERVE(3);
77 ANDROID_KABI_RESERVE(4);
78 };
79
80 #ifdef CONFIG_DMA_OPS
81 #include <asm/dma-mapping.h>
82
get_dma_ops(struct device * dev)83 static 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
set_dma_ops(struct device * dev,const struct dma_map_ops * dma_ops)90 static 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 */
get_dma_ops(struct device * dev)96 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
97 {
98 return NULL;
99 }
set_dma_ops(struct device * dev,const struct dma_map_ops * dma_ops)100 static inline void set_dma_ops(struct device *dev,
101 const struct dma_map_ops *dma_ops)
102 {
103 }
104 #endif /* CONFIG_DMA_OPS */
105
106 #ifdef CONFIG_DMA_CMA
107 extern struct cma *dma_contiguous_default_area;
108
dev_get_cma_area(struct device * dev)109 static 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
116 void dma_contiguous_reserve(phys_addr_t addr_limit);
117 int __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
120 struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
121 unsigned int order, bool no_warn);
122 bool dma_release_from_contiguous(struct device *dev, struct page *pages,
123 int count);
124 struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
125 void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
126
127 void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size);
128 #else /* CONFIG_DMA_CMA */
dev_get_cma_area(struct device * dev)129 static inline struct cma *dev_get_cma_area(struct device *dev)
130 {
131 return NULL;
132 }
dma_contiguous_reserve(phys_addr_t limit)133 static inline void dma_contiguous_reserve(phys_addr_t limit)
134 {
135 }
dma_contiguous_reserve_area(phys_addr_t size,phys_addr_t base,phys_addr_t limit,struct cma ** res_cma,bool fixed)136 static 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 }
dma_alloc_from_contiguous(struct device * dev,size_t count,unsigned int order,bool no_warn)142 static 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 }
dma_release_from_contiguous(struct device * dev,struct page * pages,int count)147 static 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 */
dma_alloc_contiguous(struct device * dev,size_t size,gfp_t gfp)153 static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,
154 gfp_t gfp)
155 {
156 return NULL;
157 }
dma_free_contiguous(struct device * dev,struct page * page,size_t size)158 static 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
166 void dma_pernuma_cma_reserve(void);
167 #else
dma_pernuma_cma_reserve(void)168 static inline void dma_pernuma_cma_reserve(void) { }
169 #endif /* CONFIG_DMA_PERNUMA_CMA */
170
171 #ifdef CONFIG_DMA_DECLARE_COHERENT
172 int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
173 dma_addr_t device_addr, size_t size);
174 void dma_release_coherent_memory(struct device *dev);
175 int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
176 dma_addr_t *dma_handle, void **ret);
177 int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
178 int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
179 void *cpu_addr, size_t size, int *ret);
180
181 void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size,
182 dma_addr_t *dma_handle);
183 int dma_release_from_global_coherent(int order, void *vaddr);
184 int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
185 size_t size, int *ret);
186
187 #else
dma_declare_coherent_memory(struct device * dev,phys_addr_t phys_addr,dma_addr_t device_addr,size_t size)188 static inline int dma_declare_coherent_memory(struct device *dev,
189 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size)
190 {
191 return -ENOSYS;
192 }
193
194 #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0)
195 #define dma_release_from_dev_coherent(dev, order, vaddr) (0)
196 #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0)
dma_release_coherent_memory(struct device * dev)197 static inline void dma_release_coherent_memory(struct device *dev) { }
198
dma_alloc_from_global_coherent(struct device * dev,ssize_t size,dma_addr_t * dma_handle)199 static inline void *dma_alloc_from_global_coherent(struct device *dev,
200 ssize_t size, dma_addr_t *dma_handle)
201 {
202 return NULL;
203 }
dma_release_from_global_coherent(int order,void * vaddr)204 static inline int dma_release_from_global_coherent(int order, void *vaddr)
205 {
206 return 0;
207 }
dma_mmap_from_global_coherent(struct vm_area_struct * vma,void * cpu_addr,size_t size,int * ret)208 static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
209 void *cpu_addr, size_t size, int *ret)
210 {
211 return 0;
212 }
213 #endif /* CONFIG_DMA_DECLARE_COHERENT */
214
215 int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
216 void *cpu_addr, dma_addr_t dma_addr, size_t size,
217 unsigned long attrs);
218 int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
219 void *cpu_addr, dma_addr_t dma_addr, size_t size,
220 unsigned long attrs);
221 struct page *dma_common_alloc_pages(struct device *dev, size_t size,
222 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
223 void dma_common_free_pages(struct device *dev, size_t size, struct page *vaddr,
224 dma_addr_t dma_handle, enum dma_data_direction dir);
225
226 struct page **dma_common_find_pages(void *cpu_addr);
227 void *dma_common_contiguous_remap(struct page *page, size_t size, pgprot_t prot,
228 const void *caller);
229 void *dma_common_pages_remap(struct page **pages, size_t size, pgprot_t prot,
230 const void *caller);
231 void dma_common_free_remap(void *cpu_addr, size_t size);
232
233 struct page *dma_alloc_from_pool(struct device *dev, size_t size,
234 void **cpu_addr, gfp_t flags,
235 bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t));
236 bool dma_free_from_pool(struct device *dev, void *start, size_t size);
237
238 #ifdef CONFIG_ARCH_HAS_DMA_COHERENCE_H
239 #include <asm/dma-coherence.h>
240 #elif defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
241 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
242 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
dev_is_dma_coherent(struct device * dev)243 static inline bool dev_is_dma_coherent(struct device *dev)
244 {
245 return dev->dma_coherent;
246 }
247 #else
dev_is_dma_coherent(struct device * dev)248 static inline bool dev_is_dma_coherent(struct device *dev)
249 {
250 return true;
251 }
252 #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
253
254 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
255 gfp_t gfp, unsigned long attrs);
256 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
257 dma_addr_t dma_addr, unsigned long attrs);
258
259 #ifdef CONFIG_MMU
260 /*
261 * Page protection so that devices that can't snoop CPU caches can use the
262 * memory coherently. We default to pgprot_noncached which is usually used
263 * for ioremap as a safe bet, but architectures can override this with less
264 * strict semantics if possible.
265 */
266 #ifndef pgprot_dmacoherent
267 #define pgprot_dmacoherent(prot) pgprot_noncached(prot)
268 #endif
269
270 /*
271 * If there is no system cache pgprot, then fallback to dmacoherent
272 * pgprot, as the expectation is that the device is not coherent.
273 */
274 #ifndef pgprot_syscached
275 #define pgprot_syscached(prot) pgprot_dmacoherent(prot)
276 #endif
277
278 pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs);
279 #else
dma_pgprot(struct device * dev,pgprot_t prot,unsigned long attrs)280 static inline pgprot_t dma_pgprot(struct device *dev, pgprot_t prot,
281 unsigned long attrs)
282 {
283 return prot; /* no protection bits supported without page tables */
284 }
285 #endif /* CONFIG_MMU */
286
287 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE
288 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
289 enum dma_data_direction dir);
290 #else
arch_sync_dma_for_device(phys_addr_t paddr,size_t size,enum dma_data_direction dir)291 static inline void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
292 enum dma_data_direction dir)
293 {
294 }
295 #endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */
296
297 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
298 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
299 enum dma_data_direction dir);
300 #else
arch_sync_dma_for_cpu(phys_addr_t paddr,size_t size,enum dma_data_direction dir)301 static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
302 enum dma_data_direction dir)
303 {
304 }
305 #endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */
306
307 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
308 void arch_sync_dma_for_cpu_all(void);
309 #else
arch_sync_dma_for_cpu_all(void)310 static inline void arch_sync_dma_for_cpu_all(void)
311 {
312 }
313 #endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */
314
315 #ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT
316 void arch_dma_prep_coherent(struct page *page, size_t size);
317 #else
arch_dma_prep_coherent(struct page * page,size_t size)318 static inline void arch_dma_prep_coherent(struct page *page, size_t size)
319 {
320 }
321 #endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */
322
323 #ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN
324 void arch_dma_mark_clean(phys_addr_t paddr, size_t size);
325 #else
arch_dma_mark_clean(phys_addr_t paddr,size_t size)326 static inline void arch_dma_mark_clean(phys_addr_t paddr, size_t size)
327 {
328 }
329 #endif /* ARCH_HAS_DMA_MARK_CLEAN */
330
331 void *arch_dma_set_uncached(void *addr, size_t size);
332 void arch_dma_clear_uncached(void *addr, size_t size);
333
334 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
335 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
336 const struct iommu_ops *iommu, bool coherent);
337 #else
arch_setup_dma_ops(struct device * dev,u64 dma_base,u64 size,const struct iommu_ops * iommu,bool coherent)338 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
339 u64 size, const struct iommu_ops *iommu, bool coherent)
340 {
341 }
342 #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
343
344 #ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS
345 void arch_teardown_dma_ops(struct device *dev);
346 #else
arch_teardown_dma_ops(struct device * dev)347 static inline void arch_teardown_dma_ops(struct device *dev)
348 {
349 }
350 #endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */
351
352 #ifdef CONFIG_DMA_API_DEBUG
353 void dma_debug_add_bus(struct bus_type *bus);
354 void debug_dma_dump_mappings(struct device *dev);
355 #else
dma_debug_add_bus(struct bus_type * bus)356 static inline void dma_debug_add_bus(struct bus_type *bus)
357 {
358 }
debug_dma_dump_mappings(struct device * dev)359 static inline void debug_dma_dump_mappings(struct device *dev)
360 {
361 }
362 #endif /* CONFIG_DMA_API_DEBUG */
363
364 extern const struct dma_map_ops dma_dummy_ops;
365
366 #endif /* _LINUX_DMA_MAP_OPS_H */
367