]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/include/linux/dma-mapping.h
MFV r349454:
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / include / linux / dma-mapping.h
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef _LINUX_DMA_MAPPING_H_
32 #define _LINUX_DMA_MAPPING_H_
33
34 #include <linux/types.h>
35 #include <linux/device.h>
36 #include <linux/err.h>
37 #include <linux/dma-attrs.h>
38 #include <linux/scatterlist.h>
39 #include <linux/mm.h>
40 #include <linux/page.h>
41
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44
45 #include <vm/vm.h>
46 #include <vm/vm_page.h>
47 #include <vm/pmap.h>
48
49 #include <machine/bus.h>
50
51 enum dma_data_direction {
52         DMA_BIDIRECTIONAL = 0,
53         DMA_TO_DEVICE = 1,
54         DMA_FROM_DEVICE = 2,
55         DMA_NONE = 3,
56 };
57
58 struct dma_map_ops {
59         void* (*alloc_coherent)(struct device *dev, size_t size,
60             dma_addr_t *dma_handle, gfp_t gfp);
61         void (*free_coherent)(struct device *dev, size_t size,
62             void *vaddr, dma_addr_t dma_handle);
63         dma_addr_t (*map_page)(struct device *dev, struct page *page,
64             unsigned long offset, size_t size, enum dma_data_direction dir,
65             struct dma_attrs *attrs);
66         void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
67             size_t size, enum dma_data_direction dir, struct dma_attrs *attrs);
68         int (*map_sg)(struct device *dev, struct scatterlist *sg,
69             int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
70         void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
71             enum dma_data_direction dir, struct dma_attrs *attrs);
72         void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
73             size_t size, enum dma_data_direction dir);
74         void (*sync_single_for_device)(struct device *dev,
75             dma_addr_t dma_handle, size_t size, enum dma_data_direction dir);
76         void (*sync_single_range_for_cpu)(struct device *dev,
77             dma_addr_t dma_handle, unsigned long offset, size_t size,
78             enum dma_data_direction dir);
79         void (*sync_single_range_for_device)(struct device *dev,
80             dma_addr_t dma_handle, unsigned long offset, size_t size,
81             enum dma_data_direction dir);
82         void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
83             int nents, enum dma_data_direction dir);
84         void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
85             int nents, enum dma_data_direction dir);
86         int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
87         int (*dma_supported)(struct device *dev, u64 mask);
88         int is_phys;
89 };
90
91 #define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL)
92
93 int linux_dma_tag_init(struct device *dev, u64 mask);
94 void *linux_dma_alloc_coherent(struct device *dev, size_t size,
95     dma_addr_t *dma_handle, gfp_t flag);
96 dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len);
97 void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size);
98 int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
99     int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
100 void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
101     int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
102
103 static inline int
104 dma_supported(struct device *dev, u64 mask)
105 {
106
107         /* XXX busdma takes care of this elsewhere. */
108         return (1);
109 }
110
111 static inline int
112 dma_set_mask(struct device *dev, u64 dma_mask)
113 {
114
115         if (!dev->dma_priv || !dma_supported(dev, dma_mask))
116                 return -EIO;
117
118         return (linux_dma_tag_init(dev, dma_mask));
119 }
120
121 static inline int
122 dma_set_coherent_mask(struct device *dev, u64 mask)
123 {
124
125         if (!dma_supported(dev, mask))
126                 return -EIO;
127         /* XXX Currently we don't support a separate coherent mask. */
128         return 0;
129 }
130
131 static inline int
132 dma_set_mask_and_coherent(struct device *dev, u64 mask)
133 {
134         int r;
135
136         r = dma_set_mask(dev, mask);
137         if (r == 0)
138                 dma_set_coherent_mask(dev, mask);
139         return (r);
140 }
141
142 static inline void *
143 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
144     gfp_t flag)
145 {
146         return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));
147 }
148
149 static inline void *
150 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
151     gfp_t flag)
152 {
153
154         return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
155 }
156
157 static inline void
158 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
159     dma_addr_t dma_addr)
160 {
161
162         linux_dma_unmap(dev, dma_addr, size);
163         kmem_free((vm_offset_t)cpu_addr, size);
164 }
165
166 static inline dma_addr_t
167 dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
168     enum dma_data_direction dir, struct dma_attrs *attrs)
169 {
170
171         return (linux_dma_map_phys(dev, vtophys(ptr), size));
172 }
173
174 static inline void
175 dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr, size_t size,
176     enum dma_data_direction dir, struct dma_attrs *attrs)
177 {
178
179         linux_dma_unmap(dev, dma_addr, size);
180 }
181
182 static inline dma_addr_t
183 dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,
184     size_t size, enum dma_data_direction dir, unsigned long attrs)
185 {
186
187         return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size));
188 }
189
190 static inline int
191 dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
192     enum dma_data_direction dir, struct dma_attrs *attrs)
193 {
194
195         return (linux_dma_map_sg_attrs(dev, sgl, nents, dir, attrs));
196 }
197
198 static inline void
199 dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
200     enum dma_data_direction dir, struct dma_attrs *attrs)
201 {
202
203         linux_dma_unmap_sg_attrs(dev, sg, nents, dir, attrs);
204 }
205
206 static inline dma_addr_t
207 dma_map_page(struct device *dev, struct page *page,
208     unsigned long offset, size_t size, enum dma_data_direction direction)
209 {
210
211         return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size));
212 }
213
214 static inline void
215 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
216     enum dma_data_direction direction)
217 {
218
219         linux_dma_unmap(dev, dma_address, size);
220 }
221
222 static inline void
223 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
224     enum dma_data_direction direction)
225 {
226 }
227
228 static inline void
229 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
230     enum dma_data_direction dir)
231 {
232         dma_sync_single_for_cpu(dev, addr, size, dir);
233 }
234
235 static inline void
236 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
237     size_t size, enum dma_data_direction direction)
238 {
239 }
240
241 static inline void
242 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
243     enum dma_data_direction direction)
244 {
245 }
246
247 static inline void
248 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
249     enum dma_data_direction direction)
250 {
251 }
252
253 static inline void
254 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
255     unsigned long offset, size_t size, int direction)
256 {
257 }
258
259 static inline void
260 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
261     unsigned long offset, size_t size, int direction)
262 {
263 }
264
265 static inline int
266 dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
267 {
268
269         return (dma_addr == 0);
270 }
271
272 static inline unsigned int dma_set_max_seg_size(struct device *dev,
273     unsigned int size)
274 {
275         return (0);
276 }
277
278
279 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL)
280 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL)
281 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL)
282 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL)
283
284 #define DEFINE_DMA_UNMAP_ADDR(name)             dma_addr_t name
285 #define DEFINE_DMA_UNMAP_LEN(name)              __u32 name
286 #define dma_unmap_addr(p, name)                 ((p)->name)
287 #define dma_unmap_addr_set(p, name, v)          (((p)->name) = (v))
288 #define dma_unmap_len(p, name)                  ((p)->name)
289 #define dma_unmap_len_set(p, name, v)           (((p)->name) = (v))
290
291 extern int uma_align_cache;
292 #define dma_get_cache_alignment()       uma_align_cache
293
294 #endif  /* _LINUX_DMA_MAPPING_H_ */