dma-mapping: split <linux/dma-mapping.h>
[linux-block.git] / include / linux / dma-map-ops.h
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
11 struct dma_map_ops {
12         void *(*alloc)(struct device *dev, size_t size,
13                         dma_addr_t *dma_handle, gfp_t gfp,
14                         unsigned long attrs);
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,
19                         gfp_t gfp);
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,
24                         gfp_t gfp);
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);
29
30         int (*get_sgtable)(struct device *dev, struct sg_table *sgt,
31                         void *cpu_addr, dma_addr_t dma_addr, size_t size,
32                         unsigned long attrs);
33
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,
39                         unsigned long attrs);
40         /*
41          * map_sg returns 0 on error and a value > 0 on success.
42          * It should never return a value < 0.
43          */
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,
50                         unsigned long attrs);
51         void (*unmap_resource)(struct device *dev, dma_addr_t dma_handle,
52                         size_t size, enum dma_data_direction dir,
53                         unsigned long attrs);
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);
69 };
70
71 #ifdef CONFIG_DMA_OPS
72 #include <asm/dma-mapping.h>
73
74 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
75 {
76         if (dev->dma_ops)
77                 return dev->dma_ops;
78         return get_arch_dma_ops(dev->bus);
79 }
80
81 static inline void set_dma_ops(struct device *dev,
82                                const struct dma_map_ops *dma_ops)
83 {
84         dev->dma_ops = dma_ops;
85 }
86 #else /* CONFIG_DMA_OPS */
87 static inline const struct dma_map_ops *get_dma_ops(struct device *dev)
88 {
89         return NULL;
90 }
91 static inline void set_dma_ops(struct device *dev,
92                                const struct dma_map_ops *dma_ops)
93 {
94 }
95 #endif /* CONFIG_DMA_OPS */
96
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);
105
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);
111
112 #else
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)
115 {
116         return -ENOSYS;
117 }
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)
121
122 static inline void *dma_alloc_from_global_coherent(struct device *dev,
123                 ssize_t size, dma_addr_t *dma_handle)
124 {
125         return NULL;
126 }
127 static inline int dma_release_from_global_coherent(int order, void *vaddr)
128 {
129         return 0;
130 }
131 static inline int dma_mmap_from_global_coherent(struct vm_area_struct *vma,
132                 void *cpu_addr, size_t size, int *ret)
133 {
134         return 0;
135 }
136 #endif /* CONFIG_DMA_DECLARE_COHERENT */
137
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);
141 #else
142 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
143                 u64 size, const struct iommu_ops *iommu, bool coherent)
144 {
145 }
146 #endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
147
148 #ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS
149 void arch_teardown_dma_ops(struct device *dev);
150 #else
151 static inline void arch_teardown_dma_ops(struct device *dev)
152 {
153 }
154 #endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */
155
156 extern const struct dma_map_ops dma_dummy_ops;
157
158 #endif /* _LINUX_DMA_MAP_OPS_H */