• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	struct sg_table *(*alloc_noncontiguous)(struct device *dev, size_t size,
27 			enum dma_data_direction dir, gfp_t gfp,
28 			unsigned long attrs);
29 	void (*free_noncontiguous)(struct device *dev, size_t size,
30 			struct sg_table *sgt, 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 should return a negative error code on error. See
46 	 * dma_map_sgtable() for a list of appropriate error codes
47 	 * and their meanings.
48 	 */
49 	int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents,
50 			enum dma_data_direction dir, unsigned long attrs);
51 	void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
52 			enum dma_data_direction dir, unsigned long attrs);
53 	dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
54 			size_t size, enum dma_data_direction dir,
55 			unsigned long attrs);
56 	void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
57 			size_t size, enum dma_data_direction dir,
58 			unsigned long attrs);
59 	void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
60 			size_t size, enum dma_data_direction dir);
61 	void (*sync_single_for_device)(struct device *dev,
62 			dma_addr_t dma_handle, size_t size,
63 			enum dma_data_direction dir);
64 	void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
65 			int nents, enum dma_data_direction dir);
66 	void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
67 			int nents, enum dma_data_direction dir);
68 	void (*cache_sync)(struct device *dev, void *vaddr, size_t size,
69 			enum dma_data_direction direction);
70 	int (*dma_supported)(struct device *dev, u64 mask);
71 	u64 (*get_required_mask)(struct device *dev);
72 	size_t (*max_mapping_size)(struct device *dev);
73 	unsigned long (*get_merge_boundary)(struct device *dev);
74 
75 	ANDROID_KABI_RESERVE(1);
76 	ANDROID_KABI_RESERVE(2);
77 	ANDROID_KABI_RESERVE(3);
78 	ANDROID_KABI_RESERVE(4);
79 };
80 
81 #ifdef CONFIG_DMA_OPS
82 #include <asm/dma-mapping.h>
83 
get_dma_ops(struct device * dev)84 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
85 {
86 	if (dev->dma_ops)
87 		return dev->dma_ops;
88 	return get_arch_dma_ops(dev->bus);
89 }
90 
set_dma_ops(struct device * dev,const struct dma_map_ops * dma_ops)91 static inline void set_dma_ops(struct device *dev,
92 			       const struct dma_map_ops *dma_ops)
93 {
94 	dev->dma_ops = dma_ops;
95 }
96 #else /* CONFIG_DMA_OPS */
get_dma_ops(struct device * dev)97 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
98 {
99 	return NULL;
100 }
set_dma_ops(struct device * dev,const struct dma_map_ops * dma_ops)101 static inline void set_dma_ops(struct device *dev,
102 			       const struct dma_map_ops *dma_ops)
103 {
104 }
105 #endif /* CONFIG_DMA_OPS */
106 
107 #ifdef CONFIG_DMA_CMA
108 extern struct cma *dma_contiguous_default_area;
109 
dev_get_cma_area(struct device * dev)110 static inline struct cma *dev_get_cma_area(struct device *dev)
111 {
112 	if (dev && dev->cma_area)
113 		return dev->cma_area;
114 	return dma_contiguous_default_area;
115 }
116 
117 void dma_contiguous_reserve(phys_addr_t addr_limit);
118 int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
119 		phys_addr_t limit, struct cma **res_cma, bool fixed);
120 
121 struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
122 				       unsigned int order, bool no_warn);
123 bool dma_release_from_contiguous(struct device *dev, struct page *pages,
124 				 int count);
125 struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
126 void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
127 
128 void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size);
129 #else /* CONFIG_DMA_CMA */
dev_get_cma_area(struct device * dev)130 static inline struct cma *dev_get_cma_area(struct device *dev)
131 {
132 	return NULL;
133 }
dma_contiguous_reserve(phys_addr_t limit)134 static inline void dma_contiguous_reserve(phys_addr_t limit)
135 {
136 }
dma_contiguous_reserve_area(phys_addr_t size,phys_addr_t base,phys_addr_t limit,struct cma ** res_cma,bool fixed)137 static inline int dma_contiguous_reserve_area(phys_addr_t size,
138 		phys_addr_t base, phys_addr_t limit, struct cma **res_cma,
139 		bool fixed)
140 {
141 	return -ENOSYS;
142 }
dma_alloc_from_contiguous(struct device * dev,size_t count,unsigned int order,bool no_warn)143 static inline struct page *dma_alloc_from_contiguous(struct device *dev,
144 		size_t count, unsigned int order, bool no_warn)
145 {
146 	return NULL;
147 }
dma_release_from_contiguous(struct device * dev,struct page * pages,int count)148 static inline bool dma_release_from_contiguous(struct device *dev,
149 		struct page *pages, int count)
150 {
151 	return false;
152 }
153 /* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */
dma_alloc_contiguous(struct device * dev,size_t size,gfp_t gfp)154 static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,
155 		gfp_t gfp)
156 {
157 	return NULL;
158 }
dma_free_contiguous(struct device * dev,struct page * page,size_t size)159 static inline void dma_free_contiguous(struct device *dev, struct page *page,
160 		size_t size)
161 {
162 	__free_pages(page, get_order(size));
163 }
164 #endif /* CONFIG_DMA_CMA*/
165 
166 #ifdef CONFIG_DMA_PERNUMA_CMA
167 void dma_pernuma_cma_reserve(void);
168 #else
dma_pernuma_cma_reserve(void)169 static inline void dma_pernuma_cma_reserve(void) { }
170 #endif /* CONFIG_DMA_PERNUMA_CMA */
171 
172 #ifdef CONFIG_DMA_DECLARE_COHERENT
173 int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
174 		dma_addr_t device_addr, size_t size);
175 void dma_release_coherent_memory(struct device *dev);
176 int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
177 		dma_addr_t *dma_handle, void **ret);
178 int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
179 int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
180 		void *cpu_addr, size_t size, int *ret);
181 #else
dma_declare_coherent_memory(struct device * dev,phys_addr_t phys_addr,dma_addr_t device_addr,size_t size)182 static inline int dma_declare_coherent_memory(struct device *dev,
183 		phys_addr_t phys_addr, dma_addr_t device_addr, size_t size)
184 {
185 	return -ENOSYS;
186 }
187 
188 #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0)
189 #define dma_release_from_dev_coherent(dev, order, vaddr) (0)
190 #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0)
dma_release_coherent_memory(struct device * dev)191 static inline void dma_release_coherent_memory(struct device *dev) { }
192 #endif /* CONFIG_DMA_DECLARE_COHERENT */
193 
194 #ifdef CONFIG_DMA_GLOBAL_POOL
195 void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size,
196 		dma_addr_t *dma_handle);
197 int dma_release_from_global_coherent(int order, void *vaddr);
198 int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
199 		size_t size, int *ret);
200 int dma_init_global_coherent(phys_addr_t phys_addr, size_t size);
201 #else
dma_alloc_from_global_coherent(struct device * dev,ssize_t size,dma_addr_t * dma_handle)202 static inline void *dma_alloc_from_global_coherent(struct device *dev,
203 		ssize_t size, dma_addr_t *dma_handle)
204 {
205 	return NULL;
206 }
dma_release_from_global_coherent(int order,void * vaddr)207 static inline int dma_release_from_global_coherent(int order, void *vaddr)
208 {
209 	return 0;
210 }
dma_mmap_from_global_coherent(struct vm_area_struct * vma,void * cpu_addr,size_t size,int * ret)211 static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
212 		void *cpu_addr, size_t size, int *ret)
213 {
214 	return 0;
215 }
216 #endif /* CONFIG_DMA_GLOBAL_POOL */
217 
218 /*
219  * This is the actual return value from the ->alloc_noncontiguous method.
220  * The users of the DMA API should only care about the sg_table, but to make
221  * the DMA-API internal vmaping and freeing easier we stash away the page
222  * array as well (except for the fallback case).  This can go away any time,
223  * e.g. when a vmap-variant that takes a scatterlist comes along.
224  */
225 struct dma_sgt_handle {
226 	struct sg_table sgt;
227 	struct page **pages;
228 };
229 #define sgt_handle(sgt) \
230 	container_of((sgt), struct dma_sgt_handle, sgt)
231 
232 int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
233 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
234 		unsigned long attrs);
235 int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
236 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
237 		unsigned long attrs);
238 struct page *dma_common_alloc_pages(struct device *dev, size_t size,
239 		dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
240 void dma_common_free_pages(struct device *dev, size_t size, struct page *vaddr,
241 		dma_addr_t dma_handle, enum dma_data_direction dir);
242 
243 struct page **dma_common_find_pages(void *cpu_addr);
244 void *dma_common_contiguous_remap(struct page *page, size_t size, pgprot_t prot,
245 		const void *caller);
246 void *dma_common_pages_remap(struct page **pages, size_t size, pgprot_t prot,
247 		const void *caller);
248 void dma_common_free_remap(void *cpu_addr, size_t size);
249 
250 struct page *dma_alloc_from_pool(struct device *dev, size_t size,
251 		void **cpu_addr, gfp_t flags,
252 		bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t));
253 bool dma_free_from_pool(struct device *dev, void *start, size_t size);
254 
255 int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start,
256 		dma_addr_t dma_start, u64 size);
257 
258 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
259 	defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
260 	defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
261 extern bool dma_default_coherent;
dev_is_dma_coherent(struct device * dev)262 static inline bool dev_is_dma_coherent(struct device *dev)
263 {
264 	return dev->dma_coherent;
265 }
266 #else
dev_is_dma_coherent(struct device * dev)267 static inline bool dev_is_dma_coherent(struct device *dev)
268 {
269 	return true;
270 }
271 #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
272 
273 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
274 		gfp_t gfp, unsigned long attrs);
275 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
276 		dma_addr_t dma_addr, unsigned long attrs);
277 
278 #ifdef CONFIG_MMU
279 /*
280  * Page protection so that devices that can't snoop CPU caches can use the
281  * memory coherently.  We default to pgprot_noncached which is usually used
282  * for ioremap as a safe bet, but architectures can override this with less
283  * strict semantics if possible.
284  */
285 #ifndef pgprot_dmacoherent
286 #define pgprot_dmacoherent(prot)	pgprot_noncached(prot)
287 #endif
288 
289 /*
290  * If there is no system cache pgprot, then fallback to dmacoherent
291  * pgprot, as the expectation is that the device is not coherent.
292  */
293 #ifndef pgprot_syscached
294 #define pgprot_syscached(prot)		pgprot_dmacoherent(prot)
295 #endif
296 
297 pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs);
298 #else
dma_pgprot(struct device * dev,pgprot_t prot,unsigned long attrs)299 static inline pgprot_t dma_pgprot(struct device *dev, pgprot_t prot,
300 		unsigned long attrs)
301 {
302 	return prot;	/* no protection bits supported without page tables */
303 }
304 #endif /* CONFIG_MMU */
305 
306 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE
307 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
308 		enum dma_data_direction dir);
309 #else
arch_sync_dma_for_device(phys_addr_t paddr,size_t size,enum dma_data_direction dir)310 static inline void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
311 		enum dma_data_direction dir)
312 {
313 }
314 #endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */
315 
316 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
317 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
318 		enum dma_data_direction dir);
319 #else
arch_sync_dma_for_cpu(phys_addr_t paddr,size_t size,enum dma_data_direction dir)320 static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
321 		enum dma_data_direction dir)
322 {
323 }
324 #endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */
325 
326 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
327 void arch_sync_dma_for_cpu_all(void);
328 #else
arch_sync_dma_for_cpu_all(void)329 static inline void arch_sync_dma_for_cpu_all(void)
330 {
331 }
332 #endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */
333 
334 #ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT
335 void arch_dma_prep_coherent(struct page *page, size_t size);
336 #else
arch_dma_prep_coherent(struct page * page,size_t size)337 static inline void arch_dma_prep_coherent(struct page *page, size_t size)
338 {
339 }
340 #endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */
341 
342 #ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN
343 void arch_dma_mark_clean(phys_addr_t paddr, size_t size);
344 #else
arch_dma_mark_clean(phys_addr_t paddr,size_t size)345 static inline void arch_dma_mark_clean(phys_addr_t paddr, size_t size)
346 {
347 }
348 #endif /* ARCH_HAS_DMA_MARK_CLEAN */
349 
350 void *arch_dma_set_uncached(void *addr, size_t size);
351 void arch_dma_clear_uncached(void *addr, size_t size);
352 
353 #ifdef CONFIG_ARCH_HAS_DMA_MAP_DIRECT
354 bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr);
355 bool arch_dma_unmap_page_direct(struct device *dev, dma_addr_t dma_handle);
356 bool arch_dma_map_sg_direct(struct device *dev, struct scatterlist *sg,
357 		int nents);
358 bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg,
359 		int nents);
360 #else
361 #define arch_dma_map_page_direct(d, a)		(false)
362 #define arch_dma_unmap_page_direct(d, a)	(false)
363 #define arch_dma_map_sg_direct(d, s, n)		(false)
364 #define arch_dma_unmap_sg_direct(d, s, n)	(false)
365 #endif
366 
367 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
368 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
369 		const struct iommu_ops *iommu, bool coherent);
370 #else
arch_setup_dma_ops(struct device * dev,u64 dma_base,u64 size,const struct iommu_ops * iommu,bool coherent)371 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
372 		u64 size, const struct iommu_ops *iommu, bool coherent)
373 {
374 }
375 #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
376 
377 #ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS
378 void arch_teardown_dma_ops(struct device *dev);
379 #else
arch_teardown_dma_ops(struct device * dev)380 static inline void arch_teardown_dma_ops(struct device *dev)
381 {
382 }
383 #endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */
384 
385 #ifdef CONFIG_DMA_API_DEBUG
386 void dma_debug_add_bus(struct bus_type *bus);
387 void debug_dma_dump_mappings(struct device *dev);
388 #else
dma_debug_add_bus(struct bus_type * bus)389 static inline void dma_debug_add_bus(struct bus_type *bus)
390 {
391 }
debug_dma_dump_mappings(struct device * dev)392 static inline void debug_dma_dump_mappings(struct device *dev)
393 {
394 }
395 #endif /* CONFIG_DMA_API_DEBUG */
396 
397 extern const struct dma_map_ops dma_dummy_ops;
398 
399 #endif /* _LINUX_DMA_MAP_OPS_H */
400