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