1 /* SPDX-License-Identifier: GPL-2.0 */
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.
6 #ifndef _LINUX_DMA_MAP_OPS_H
7 #define _LINUX_DMA_MAP_OPS_H
9 #include <linux/dma-mapping.h>
10 #include <linux/pgtable.h>
11 #include <linux/slab.h>
17 * Values for struct dma_map_ops.flags:
19 * DMA_F_PCI_P2PDMA_SUPPORTED: Indicates the dma_map_ops implementation can
20 * handle PCI P2PDMA pages in the map_sg/unmap_sg operation.
21 * DMA_F_CAN_SKIP_SYNC: DMA sync operations can be skipped if the device is
22 * coherent and it's not an SWIOTLB buffer.
24 #define DMA_F_PCI_P2PDMA_SUPPORTED (1 << 0)
25 #define DMA_F_CAN_SKIP_SYNC (1 << 1)
30 void *(*alloc)(struct device *dev, size_t size,
31 dma_addr_t *dma_handle, gfp_t gfp,
33 void (*free)(struct device *dev, size_t size, void *vaddr,
34 dma_addr_t dma_handle, unsigned long attrs);
35 struct page *(*alloc_pages_op)(struct device *dev, size_t size,
36 dma_addr_t *dma_handle, enum dma_data_direction dir,
38 void (*free_pages)(struct device *dev, size_t size, struct page *vaddr,
39 dma_addr_t dma_handle, enum dma_data_direction dir);
40 struct sg_table *(*alloc_noncontiguous)(struct device *dev, size_t size,
41 enum dma_data_direction dir, gfp_t gfp,
43 void (*free_noncontiguous)(struct device *dev, size_t size,
44 struct sg_table *sgt, enum dma_data_direction dir);
45 int (*mmap)(struct device *, struct vm_area_struct *,
46 void *, dma_addr_t, size_t, unsigned long attrs);
48 int (*get_sgtable)(struct device *dev, struct sg_table *sgt,
49 void *cpu_addr, dma_addr_t dma_addr, size_t size,
52 dma_addr_t (*map_page)(struct device *dev, struct page *page,
53 unsigned long offset, size_t size,
54 enum dma_data_direction dir, unsigned long attrs);
55 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
56 size_t size, enum dma_data_direction dir,
59 * map_sg should return a negative error code on error. See
60 * dma_map_sgtable() for a list of appropriate error codes
63 int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents,
64 enum dma_data_direction dir, unsigned long attrs);
65 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
66 enum dma_data_direction dir, unsigned long attrs);
67 dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
68 size_t size, enum dma_data_direction dir,
70 void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
71 size_t size, enum dma_data_direction dir,
73 void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
74 size_t size, enum dma_data_direction dir);
75 void (*sync_single_for_device)(struct device *dev,
76 dma_addr_t dma_handle, size_t size,
77 enum dma_data_direction dir);
78 void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
79 int nents, enum dma_data_direction dir);
80 void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
81 int nents, enum dma_data_direction dir);
82 void (*cache_sync)(struct device *dev, void *vaddr, size_t size,
83 enum dma_data_direction direction);
84 int (*dma_supported)(struct device *dev, u64 mask);
85 u64 (*get_required_mask)(struct device *dev);
86 size_t (*max_mapping_size)(struct device *dev);
87 size_t (*opt_mapping_size)(void);
88 unsigned long (*get_merge_boundary)(struct device *dev);
92 #include <asm/dma-mapping.h>
94 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
98 return get_arch_dma_ops();
101 static inline void set_dma_ops(struct device *dev,
102 const struct dma_map_ops *dma_ops)
104 dev->dma_ops = dma_ops;
106 #else /* CONFIG_DMA_OPS */
107 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
111 static inline void set_dma_ops(struct device *dev,
112 const struct dma_map_ops *dma_ops)
115 #endif /* CONFIG_DMA_OPS */
117 #ifdef CONFIG_DMA_CMA
118 extern struct cma *dma_contiguous_default_area;
120 static inline struct cma *dev_get_cma_area(struct device *dev)
122 if (dev && dev->cma_area)
123 return dev->cma_area;
124 return dma_contiguous_default_area;
127 void dma_contiguous_reserve(phys_addr_t addr_limit);
128 int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
129 phys_addr_t limit, struct cma **res_cma, bool fixed);
131 struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
132 unsigned int order, bool no_warn);
133 bool dma_release_from_contiguous(struct device *dev, struct page *pages,
135 struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
136 void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
138 void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size);
139 #else /* CONFIG_DMA_CMA */
140 static inline struct cma *dev_get_cma_area(struct device *dev)
144 static inline void dma_contiguous_reserve(phys_addr_t limit)
147 static inline int dma_contiguous_reserve_area(phys_addr_t size,
148 phys_addr_t base, phys_addr_t limit, struct cma **res_cma,
153 static inline struct page *dma_alloc_from_contiguous(struct device *dev,
154 size_t count, unsigned int order, bool no_warn)
158 static inline bool dma_release_from_contiguous(struct device *dev,
159 struct page *pages, int count)
163 /* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */
164 static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,
169 static inline void dma_free_contiguous(struct device *dev, struct page *page,
172 __free_pages(page, get_order(size));
174 #endif /* CONFIG_DMA_CMA*/
176 #ifdef CONFIG_DMA_DECLARE_COHERENT
177 int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
178 dma_addr_t device_addr, size_t size);
179 void dma_release_coherent_memory(struct device *dev);
180 int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
181 dma_addr_t *dma_handle, void **ret);
182 int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
183 int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
184 void *cpu_addr, size_t size, int *ret);
186 static inline int dma_declare_coherent_memory(struct device *dev,
187 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size)
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 static inline void dma_release_coherent_memory(struct device *dev) { }
196 #endif /* CONFIG_DMA_DECLARE_COHERENT */
198 #ifdef CONFIG_DMA_GLOBAL_POOL
199 void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size,
200 dma_addr_t *dma_handle);
201 int dma_release_from_global_coherent(int order, void *vaddr);
202 int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
203 size_t size, int *ret);
204 int dma_init_global_coherent(phys_addr_t phys_addr, size_t size);
206 static inline void *dma_alloc_from_global_coherent(struct device *dev,
207 ssize_t size, dma_addr_t *dma_handle)
211 static inline int dma_release_from_global_coherent(int order, void *vaddr)
215 static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
216 void *cpu_addr, size_t size, int *ret)
220 #endif /* CONFIG_DMA_GLOBAL_POOL */
223 * This is the actual return value from the ->alloc_noncontiguous method.
224 * The users of the DMA API should only care about the sg_table, but to make
225 * the DMA-API internal vmaping and freeing easier we stash away the page
226 * array as well (except for the fallback case). This can go away any time,
227 * e.g. when a vmap-variant that takes a scatterlist comes along.
229 struct dma_sgt_handle {
233 #define sgt_handle(sgt) \
234 container_of((sgt), struct dma_sgt_handle, sgt)
236 int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
237 void *cpu_addr, dma_addr_t dma_addr, size_t size,
238 unsigned long attrs);
239 int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
240 void *cpu_addr, dma_addr_t dma_addr, size_t size,
241 unsigned long attrs);
242 struct page *dma_common_alloc_pages(struct device *dev, size_t size,
243 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
244 void dma_common_free_pages(struct device *dev, size_t size, struct page *vaddr,
245 dma_addr_t dma_handle, enum dma_data_direction dir);
247 struct page **dma_common_find_pages(void *cpu_addr);
248 void *dma_common_contiguous_remap(struct page *page, size_t size, pgprot_t prot,
250 void *dma_common_pages_remap(struct page **pages, size_t size, pgprot_t prot,
252 void dma_common_free_remap(void *cpu_addr, size_t size);
254 struct page *dma_alloc_from_pool(struct device *dev, size_t size,
255 void **cpu_addr, gfp_t flags,
256 bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t));
257 bool dma_free_from_pool(struct device *dev, void *start, size_t size);
259 int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start,
260 dma_addr_t dma_start, u64 size);
262 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
263 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
264 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
265 extern bool dma_default_coherent;
266 static inline bool dev_is_dma_coherent(struct device *dev)
268 return dev->dma_coherent;
271 #define dma_default_coherent true
273 static inline bool dev_is_dma_coherent(struct device *dev)
277 #endif /* CONFIG_ARCH_HAS_DMA_COHERENCE_H */
279 static inline void dma_reset_need_sync(struct device *dev)
281 #ifdef CONFIG_DMA_NEED_SYNC
282 /* Reset it only once so that the function can be called on hotpath */
283 if (unlikely(dev->dma_skip_sync))
284 dev->dma_skip_sync = false;
289 * Check whether potential kmalloc() buffers are safe for non-coherent DMA.
291 static inline bool dma_kmalloc_safe(struct device *dev,
292 enum dma_data_direction dir)
295 * If DMA bouncing of kmalloc() buffers is disabled, the kmalloc()
296 * caches have already been aligned to a DMA-safe size.
298 if (!IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC))
302 * kmalloc() buffers are DMA-safe irrespective of size if the device
303 * is coherent or the direction is DMA_TO_DEVICE (non-desctructive
304 * cache maintenance and benign cache line evictions).
306 if (dev_is_dma_coherent(dev) || dir == DMA_TO_DEVICE)
313 * Check whether the given size, assuming it is for a kmalloc()'ed buffer, is
314 * sufficiently aligned for non-coherent DMA.
316 static inline bool dma_kmalloc_size_aligned(size_t size)
319 * Larger kmalloc() sizes are guaranteed to be aligned to
322 if (size >= 2 * ARCH_DMA_MINALIGN ||
323 IS_ALIGNED(kmalloc_size_roundup(size), dma_get_cache_alignment()))
330 * Check whether the given object size may have originated from a kmalloc()
331 * buffer with a slab alignment below the DMA-safe alignment and needs
332 * bouncing for non-coherent DMA. The pointer alignment is not considered and
333 * in-structure DMA-safe offsets are the responsibility of the caller. Such
334 * code should use the static ARCH_DMA_MINALIGN for compiler annotations.
336 * The heuristics can have false positives, bouncing unnecessarily, though the
337 * buffers would be small. False negatives are theoretically possible if, for
338 * example, multiple small kmalloc() buffers are coalesced into a larger
339 * buffer that passes the alignment check. There are no such known constructs
342 static inline bool dma_kmalloc_needs_bounce(struct device *dev, size_t size,
343 enum dma_data_direction dir)
345 return !dma_kmalloc_safe(dev, dir) && !dma_kmalloc_size_aligned(size);
348 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
349 gfp_t gfp, unsigned long attrs);
350 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
351 dma_addr_t dma_addr, unsigned long attrs);
353 #ifdef CONFIG_ARCH_HAS_DMA_SET_MASK
354 void arch_dma_set_mask(struct device *dev, u64 mask);
356 #define arch_dma_set_mask(dev, mask) do { } while (0)
361 * Page protection so that devices that can't snoop CPU caches can use the
362 * memory coherently. We default to pgprot_noncached which is usually used
363 * for ioremap as a safe bet, but architectures can override this with less
364 * strict semantics if possible.
366 #ifndef pgprot_dmacoherent
367 #define pgprot_dmacoherent(prot) pgprot_noncached(prot)
370 pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsigned long attrs);
372 static inline pgprot_t dma_pgprot(struct device *dev, pgprot_t prot,
375 return prot; /* no protection bits supported without page tables */
377 #endif /* CONFIG_MMU */
379 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE
380 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
381 enum dma_data_direction dir);
383 static inline void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
384 enum dma_data_direction dir)
387 #endif /* ARCH_HAS_SYNC_DMA_FOR_DEVICE */
389 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU
390 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
391 enum dma_data_direction dir);
393 static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
394 enum dma_data_direction dir)
397 #endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */
399 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
400 void arch_sync_dma_for_cpu_all(void);
402 static inline void arch_sync_dma_for_cpu_all(void)
405 #endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */
407 #ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT
408 void arch_dma_prep_coherent(struct page *page, size_t size);
410 static inline void arch_dma_prep_coherent(struct page *page, size_t size)
413 #endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */
415 #ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN
416 void arch_dma_mark_clean(phys_addr_t paddr, size_t size);
418 static inline void arch_dma_mark_clean(phys_addr_t paddr, size_t size)
421 #endif /* ARCH_HAS_DMA_MARK_CLEAN */
423 void *arch_dma_set_uncached(void *addr, size_t size);
424 void arch_dma_clear_uncached(void *addr, size_t size);
426 #ifdef CONFIG_ARCH_HAS_DMA_MAP_DIRECT
427 bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr);
428 bool arch_dma_unmap_page_direct(struct device *dev, dma_addr_t dma_handle);
429 bool arch_dma_map_sg_direct(struct device *dev, struct scatterlist *sg,
431 bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg,
434 #define arch_dma_map_page_direct(d, a) (false)
435 #define arch_dma_unmap_page_direct(d, a) (false)
436 #define arch_dma_map_sg_direct(d, s, n) (false)
437 #define arch_dma_unmap_sg_direct(d, s, n) (false)
440 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
441 void arch_setup_dma_ops(struct device *dev, bool coherent);
443 static inline void arch_setup_dma_ops(struct device *dev, bool coherent)
446 #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
448 #ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS
449 void arch_teardown_dma_ops(struct device *dev);
451 static inline void arch_teardown_dma_ops(struct device *dev)
454 #endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */
456 #ifdef CONFIG_DMA_API_DEBUG
457 void dma_debug_add_bus(const struct bus_type *bus);
458 void debug_dma_dump_mappings(struct device *dev);
460 static inline void dma_debug_add_bus(const struct bus_type *bus)
463 static inline void debug_dma_dump_mappings(struct device *dev)
466 #endif /* CONFIG_DMA_API_DEBUG */
468 extern const struct dma_map_ops dma_dummy_ops;
470 enum pci_p2pdma_map_type {
472 * PCI_P2PDMA_MAP_UNKNOWN: Used internally for indicating the mapping
473 * type hasn't been calculated yet. Functions that return this enum
474 * never return this value.
476 PCI_P2PDMA_MAP_UNKNOWN = 0,
479 * PCI_P2PDMA_MAP_NOT_SUPPORTED: Indicates the transaction will
480 * traverse the host bridge and the host bridge is not in the
481 * allowlist. DMA Mapping routines should return an error when
484 PCI_P2PDMA_MAP_NOT_SUPPORTED,
487 * PCI_P2PDMA_BUS_ADDR: Indicates that two devices can talk to
488 * each other directly through a PCI switch and the transaction will
489 * not traverse the host bridge. Such a mapping should program
490 * the DMA engine with PCI bus addresses.
492 PCI_P2PDMA_MAP_BUS_ADDR,
495 * PCI_P2PDMA_MAP_THRU_HOST_BRIDGE: Indicates two devices can talk
496 * to each other, but the transaction traverses a host bridge on the
497 * allowlist. In this case, a normal mapping either with CPU physical
498 * addresses (in the case of dma-direct) or IOVA addresses (in the
499 * case of IOMMUs) should be used to program the DMA engine.
501 PCI_P2PDMA_MAP_THRU_HOST_BRIDGE,
504 struct pci_p2pdma_map_state {
505 struct dev_pagemap *pgmap;
510 #ifdef CONFIG_PCI_P2PDMA
511 enum pci_p2pdma_map_type
512 pci_p2pdma_map_segment(struct pci_p2pdma_map_state *state, struct device *dev,
513 struct scatterlist *sg);
514 #else /* CONFIG_PCI_P2PDMA */
515 static inline enum pci_p2pdma_map_type
516 pci_p2pdma_map_segment(struct pci_p2pdma_map_state *state, struct device *dev,
517 struct scatterlist *sg)
519 return PCI_P2PDMA_MAP_NOT_SUPPORTED;
521 #endif /* CONFIG_PCI_P2PDMA */
523 #endif /* _LINUX_DMA_MAP_OPS_H */