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>
12 void *(*alloc)(struct device *dev, size_t size,
13 dma_addr_t *dma_handle, gfp_t gfp,
15 void (*free)(struct device *dev, size_t size, void *vaddr,
16 dma_addr_t dma_handle, unsigned long attrs);
17 struct page *(*alloc_pages)(struct device *dev, size_t size,
18 dma_addr_t *dma_handle, enum dma_data_direction dir,
20 void (*free_pages)(struct device *dev, size_t size, struct page *vaddr,
21 dma_addr_t dma_handle, enum dma_data_direction dir);
22 void *(*alloc_noncoherent)(struct device *dev, size_t size,
23 dma_addr_t *dma_handle, enum dma_data_direction dir,
25 void (*free_noncoherent)(struct device *dev, size_t size, void *vaddr,
26 dma_addr_t dma_handle, enum dma_data_direction dir);
27 int (*mmap)(struct device *, struct vm_area_struct *,
28 void *, dma_addr_t, size_t, unsigned long attrs);
30 int (*get_sgtable)(struct device *dev, struct sg_table *sgt,
31 void *cpu_addr, dma_addr_t dma_addr, size_t size,
34 dma_addr_t (*map_page)(struct device *dev, struct page *page,
35 unsigned long offset, size_t size,
36 enum dma_data_direction dir, unsigned long attrs);
37 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
38 size_t size, enum dma_data_direction dir,
41 * map_sg returns 0 on error and a value > 0 on success.
42 * It should never return a value < 0.
44 int (*map_sg)(struct device *dev, struct scatterlist *sg, int nents,
45 enum dma_data_direction dir, unsigned long attrs);
46 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
47 enum dma_data_direction dir, unsigned long attrs);
48 dma_addr_t (*map_resource)(struct device *dev, phys_addr_t phys_addr,
49 size_t size, enum dma_data_direction dir,
51 void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
52 size_t size, enum dma_data_direction dir,
54 void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
55 size_t size, enum dma_data_direction dir);
56 void (*sync_single_for_device)(struct device *dev,
57 dma_addr_t dma_handle, size_t size,
58 enum dma_data_direction dir);
59 void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
60 int nents, enum dma_data_direction dir);
61 void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
62 int nents, enum dma_data_direction dir);
63 void (*cache_sync)(struct device *dev, void *vaddr, size_t size,
64 enum dma_data_direction direction);
65 int (*dma_supported)(struct device *dev, u64 mask);
66 u64 (*get_required_mask)(struct device *dev);
67 size_t (*max_mapping_size)(struct device *dev);
68 unsigned long (*get_merge_boundary)(struct device *dev);
72 #include <asm/dma-mapping.h>
74 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
78 return get_arch_dma_ops(dev->bus);
81 static inline void set_dma_ops(struct device *dev,
82 const struct dma_map_ops *dma_ops)
84 dev->dma_ops = dma_ops;
86 #else /* CONFIG_DMA_OPS */
87 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
91 static inline void set_dma_ops(struct device *dev,
92 const struct dma_map_ops *dma_ops)
95 #endif /* CONFIG_DMA_OPS */
97 #ifdef CONFIG_DMA_DECLARE_COHERENT
98 int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
99 dma_addr_t device_addr, size_t size);
100 int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
101 dma_addr_t *dma_handle, void **ret);
102 int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr);
103 int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
104 void *cpu_addr, size_t size, int *ret);
106 void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size,
107 dma_addr_t *dma_handle);
108 int dma_release_from_global_coherent(int order, void *vaddr);
109 int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *cpu_addr,
110 size_t size, int *ret);
113 static inline int dma_declare_coherent_memory(struct device *dev,
114 phys_addr_t phys_addr, dma_addr_t device_addr, size_t size)
118 #define dma_alloc_from_dev_coherent(dev, size, handle, ret) (0)
119 #define dma_release_from_dev_coherent(dev, order, vaddr) (0)
120 #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0)
122 static inline void *dma_alloc_from_global_coherent(struct device *dev,
123 ssize_t size, dma_addr_t *dma_handle)
127 static inline int dma_release_from_global_coherent(int order, void *vaddr)
131 static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
132 void *cpu_addr, size_t size, int *ret)
136 #endif /* CONFIG_DMA_DECLARE_COHERENT */
138 #ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
139 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
140 const struct iommu_ops *iommu, bool coherent);
142 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
143 u64 size, const struct iommu_ops *iommu, bool coherent)
146 #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
148 #ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS
149 void arch_teardown_dma_ops(struct device *dev);
151 static inline void arch_teardown_dma_ops(struct device *dev)
154 #endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */
156 extern const struct dma_map_ops dma_dummy_ops;
158 #endif /* _LINUX_DMA_MAP_OPS_H */