OSDN Git Service

f2fs: Revert rapid GC
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / include / linux / dma-mapping.h
1 #ifndef _LINUX_DMA_MAPPING_H
2 #define _LINUX_DMA_MAPPING_H
3
4 #include <linux/sizes.h>
5 #include <linux/string.h>
6 #include <linux/device.h>
7 #include <linux/err.h>
8 #include <linux/dma-attrs.h>
9 #include <linux/dma-direction.h>
10 #include <linux/scatterlist.h>
11
12 /*
13  * A dma_addr_t can hold any valid DMA or bus address for the platform.
14  * It can be given to a device to use as a DMA source or target.  A CPU cannot
15  * reference a dma_addr_t directly because there may be translation between
16  * its physical address space and the bus address space.
17  */
18 struct dma_map_ops {
19         void* (*alloc)(struct device *dev, size_t size,
20                                 dma_addr_t *dma_handle, gfp_t gfp,
21                                 struct dma_attrs *attrs);
22         void (*free)(struct device *dev, size_t size,
23                               void *vaddr, dma_addr_t dma_handle,
24                               struct dma_attrs *attrs);
25         int (*mmap)(struct device *, struct vm_area_struct *,
26                           void *, dma_addr_t, size_t, struct dma_attrs *attrs);
27
28         int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *,
29                            dma_addr_t, size_t, struct dma_attrs *attrs);
30
31         dma_addr_t (*map_page)(struct device *dev, struct page *page,
32                                unsigned long offset, size_t size,
33                                enum dma_data_direction dir,
34                                struct dma_attrs *attrs);
35         void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
36                            size_t size, enum dma_data_direction dir,
37                            struct dma_attrs *attrs);
38         /*
39          * map_sg returns 0 on error and a value > 0 on success.
40          * It should never return a value < 0.
41          */
42         int (*map_sg)(struct device *dev, struct scatterlist *sg,
43                       int nents, enum dma_data_direction dir,
44                       struct dma_attrs *attrs);
45         void (*unmap_sg)(struct device *dev,
46                          struct scatterlist *sg, int nents,
47                          enum dma_data_direction dir,
48                          struct dma_attrs *attrs);
49         void (*sync_single_for_cpu)(struct device *dev,
50                                     dma_addr_t dma_handle, size_t size,
51                                     enum dma_data_direction dir);
52         void (*sync_single_for_device)(struct device *dev,
53                                        dma_addr_t dma_handle, size_t size,
54                                        enum dma_data_direction dir);
55         void (*sync_sg_for_cpu)(struct device *dev,
56                                 struct scatterlist *sg, int nents,
57                                 enum dma_data_direction dir);
58         void (*sync_sg_for_device)(struct device *dev,
59                                    struct scatterlist *sg, int nents,
60                                    enum dma_data_direction dir);
61         int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
62         int (*dma_supported)(struct device *dev, u64 mask);
63         int (*set_dma_mask)(struct device *dev, u64 mask);
64         void *(*remap)(struct device *dev, void *cpu_addr, dma_addr_t handle,
65                         size_t size, struct dma_attrs *attrs);
66         void (*unremap)(struct device *dev, void *remapped_address,
67                         size_t size);
68 #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
69         u64 (*get_required_mask)(struct device *dev);
70 #endif
71         int is_phys;
72 };
73
74 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
75
76 #define DMA_MASK_NONE   0x0ULL
77
78 static inline int valid_dma_direction(int dma_direction)
79 {
80         return ((dma_direction == DMA_BIDIRECTIONAL) ||
81                 (dma_direction == DMA_TO_DEVICE) ||
82                 (dma_direction == DMA_FROM_DEVICE));
83 }
84
85 static inline int is_device_dma_capable(struct device *dev)
86 {
87         return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
88 }
89
90 #ifdef CONFIG_HAS_DMA
91 #include <asm/dma-mapping.h>
92 #else
93 #include <asm-generic/dma-mapping-broken.h>
94 #endif
95
96 #ifndef CONFIG_NO_DMA
97 static inline void *dma_remap(struct device *dev, void *cpu_addr,
98                 dma_addr_t dma_handle, size_t size, struct dma_attrs *attrs)
99 {
100         const struct dma_map_ops *ops = get_dma_ops(dev);
101         BUG_ON(!ops);
102
103         if (!ops->remap) {
104                 WARN_ONCE(1, "Remap function not implemented for %pS\n",
105                                 ops->remap);
106                 return NULL;
107         }
108
109         return ops->remap(dev, cpu_addr, dma_handle, size, attrs);
110 }
111
112
113 static inline void dma_unremap(struct device *dev, void *remapped_addr,
114                                 size_t size)
115 {
116         const struct dma_map_ops *ops = get_dma_ops(dev);
117         BUG_ON(!ops);
118
119         if (!ops->unremap) {
120                 WARN_ONCE(1, "unremap function not implemented for %pS\n",
121                                 ops->unremap);
122                 return;
123         }
124
125         return ops->unremap(dev, remapped_addr, size);
126 }
127 #endif
128
129
130 static inline u64 dma_get_mask(struct device *dev)
131 {
132         if (dev && dev->dma_mask && *dev->dma_mask)
133                 return *dev->dma_mask;
134         return DMA_BIT_MASK(32);
135 }
136
137 #ifdef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
138 int dma_set_coherent_mask(struct device *dev, u64 mask);
139 #else
140 static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
141 {
142         if (!dma_supported(dev, mask))
143                 return -EIO;
144         dev->coherent_dma_mask = mask;
145         return 0;
146 }
147 #endif
148
149 /*
150  * Set both the DMA mask and the coherent DMA mask to the same thing.
151  * Note that we don't check the return value from dma_set_coherent_mask()
152  * as the DMA API guarantees that the coherent DMA mask can be set to
153  * the same or smaller than the streaming DMA mask.
154  */
155 static inline int dma_set_mask_and_coherent(struct device *dev, u64 mask)
156 {
157         int rc = dma_set_mask(dev, mask);
158         if (rc == 0)
159                 dma_set_coherent_mask(dev, mask);
160         return rc;
161 }
162
163 /*
164  * Similar to the above, except it deals with the case where the device
165  * does not have dev->dma_mask appropriately setup.
166  */
167 static inline int dma_coerce_mask_and_coherent(struct device *dev, u64 mask)
168 {
169         dev->dma_mask = &dev->coherent_dma_mask;
170         return dma_set_mask_and_coherent(dev, mask);
171 }
172
173 extern u64 dma_get_required_mask(struct device *dev);
174
175 #ifndef arch_setup_dma_ops
176 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
177                                       u64 size, struct iommu_ops *iommu,
178                                       bool coherent) { }
179 #endif
180
181 #ifndef arch_teardown_dma_ops
182 static inline void arch_teardown_dma_ops(struct device *dev) { }
183 #endif
184
185 static inline unsigned int dma_get_max_seg_size(struct device *dev)
186 {
187         if (dev->dma_parms && dev->dma_parms->max_segment_size)
188                 return dev->dma_parms->max_segment_size;
189         return SZ_64K;
190 }
191
192 static inline unsigned int dma_set_max_seg_size(struct device *dev,
193                                                 unsigned int size)
194 {
195         if (dev->dma_parms) {
196                 dev->dma_parms->max_segment_size = size;
197                 return 0;
198         }
199         return -EIO;
200 }
201
202 static inline unsigned long dma_get_seg_boundary(struct device *dev)
203 {
204         if (dev->dma_parms && dev->dma_parms->segment_boundary_mask)
205                 return dev->dma_parms->segment_boundary_mask;
206         return DMA_BIT_MASK(32);
207 }
208
209 static inline int dma_set_seg_boundary(struct device *dev, unsigned long mask)
210 {
211         if (dev->dma_parms) {
212                 dev->dma_parms->segment_boundary_mask = mask;
213                 return 0;
214         }
215         return -EIO;
216 }
217
218 #ifndef dma_max_pfn
219 static inline unsigned long dma_max_pfn(struct device *dev)
220 {
221         return *dev->dma_mask >> PAGE_SHIFT;
222 }
223 #endif
224
225 static inline void *dma_zalloc_coherent(struct device *dev, size_t size,
226                                         dma_addr_t *dma_handle, gfp_t flag)
227 {
228         void *ret = dma_alloc_coherent(dev, size, dma_handle,
229                                        flag | __GFP_ZERO);
230         return ret;
231 }
232
233 #ifdef CONFIG_HAS_DMA
234 static inline int dma_get_cache_alignment(void)
235 {
236 #ifdef ARCH_DMA_MINALIGN
237         return ARCH_DMA_MINALIGN;
238 #endif
239         return 1;
240 }
241 #endif
242
243 /* flags for the coherent memory api */
244 #define DMA_MEMORY_MAP                  0x01
245 #define DMA_MEMORY_IO                   0x02
246 #define DMA_MEMORY_INCLUDES_CHILDREN    0x04
247 #define DMA_MEMORY_EXCLUSIVE            0x08
248
249 #ifndef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
250 static inline int
251 dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
252                             dma_addr_t device_addr, size_t size, int flags)
253 {
254         return 0;
255 }
256
257 static inline void
258 dma_release_declared_memory(struct device *dev)
259 {
260 }
261
262 static inline void *
263 dma_mark_declared_memory_occupied(struct device *dev,
264                                   dma_addr_t device_addr, size_t size)
265 {
266         return ERR_PTR(-EBUSY);
267 }
268 #endif
269
270 /*
271  * Managed DMA API
272  */
273 extern void *dmam_alloc_coherent(struct device *dev, size_t size,
274                                  dma_addr_t *dma_handle, gfp_t gfp);
275 extern void dmam_free_coherent(struct device *dev, size_t size, void *vaddr,
276                                dma_addr_t dma_handle);
277 extern void *dmam_alloc_noncoherent(struct device *dev, size_t size,
278                                     dma_addr_t *dma_handle, gfp_t gfp);
279 extern void dmam_free_noncoherent(struct device *dev, size_t size, void *vaddr,
280                                   dma_addr_t dma_handle);
281 #ifdef ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY
282 extern int dmam_declare_coherent_memory(struct device *dev,
283                                         phys_addr_t phys_addr,
284                                         dma_addr_t device_addr, size_t size,
285                                         int flags);
286 extern void dmam_release_declared_memory(struct device *dev);
287 #else /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
288 static inline int dmam_declare_coherent_memory(struct device *dev,
289                                 phys_addr_t phys_addr, dma_addr_t device_addr,
290                                 size_t size, gfp_t gfp)
291 {
292         return 0;
293 }
294
295 static inline void dmam_release_declared_memory(struct device *dev)
296 {
297 }
298 #endif /* ARCH_HAS_DMA_DECLARE_COHERENT_MEMORY */
299
300 #ifndef CONFIG_HAVE_DMA_ATTRS
301 struct dma_attrs;
302
303 #define dma_map_single_attrs(dev, cpu_addr, size, dir, attrs) \
304         dma_map_single(dev, cpu_addr, size, dir)
305
306 #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
307         dma_unmap_single(dev, dma_addr, size, dir)
308
309 #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
310         dma_map_sg(dev, sgl, nents, dir)
311
312 #define dma_unmap_sg_attrs(dev, sgl, nents, dir, attrs) \
313         dma_unmap_sg(dev, sgl, nents, dir)
314
315 #else
316 static inline void *dma_alloc_writecombine(struct device *dev, size_t size,
317                                            dma_addr_t *dma_addr, gfp_t gfp)
318 {
319         DEFINE_DMA_ATTRS(attrs);
320         dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
321         return dma_alloc_attrs(dev, size, dma_addr, gfp, &attrs);
322 }
323
324 static inline void dma_free_writecombine(struct device *dev, size_t size,
325                                          void *cpu_addr, dma_addr_t dma_addr)
326 {
327         DEFINE_DMA_ATTRS(attrs);
328         dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
329         return dma_free_attrs(dev, size, cpu_addr, dma_addr, &attrs);
330 }
331
332 static inline int dma_mmap_writecombine(struct device *dev,
333                                         struct vm_area_struct *vma,
334                                         void *cpu_addr, dma_addr_t dma_addr,
335                                         size_t size)
336 {
337         DEFINE_DMA_ATTRS(attrs);
338         dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
339         return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs);
340 }
341 #endif /* CONFIG_HAVE_DMA_ATTRS */
342
343 #ifdef CONFIG_NEED_DMA_MAP_STATE
344 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)        dma_addr_t ADDR_NAME
345 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)          __u32 LEN_NAME
346 #define dma_unmap_addr(PTR, ADDR_NAME)           ((PTR)->ADDR_NAME)
347 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL)  (((PTR)->ADDR_NAME) = (VAL))
348 #define dma_unmap_len(PTR, LEN_NAME)             ((PTR)->LEN_NAME)
349 #define dma_unmap_len_set(PTR, LEN_NAME, VAL)    (((PTR)->LEN_NAME) = (VAL))
350 #else
351 #define DEFINE_DMA_UNMAP_ADDR(ADDR_NAME)
352 #define DEFINE_DMA_UNMAP_LEN(LEN_NAME)
353 #define dma_unmap_addr(PTR, ADDR_NAME)           (0)
354 #define dma_unmap_addr_set(PTR, ADDR_NAME, VAL)  do { } while (0)
355 #define dma_unmap_len(PTR, LEN_NAME)             (0)
356 #define dma_unmap_len_set(PTR, LEN_NAME, VAL)    do { } while (0)
357 #endif
358
359 #endif