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 _LINUXKPI_LINUX_DMA_MAPPING_H_
32 #define _LINUXKPI_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 #include <linux/sizes.h>
42
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45
46 #include <vm/vm.h>
47 #include <vm/vm_page.h>
48 #include <vm/pmap.h>
49
50 #include <machine/bus.h>
51
52 enum dma_data_direction {
53 DMA_BIDIRECTIONAL = 0,
54 DMA_TO_DEVICE = 1,
55 DMA_FROM_DEVICE = 2,
56 DMA_NONE = 3,
57 };
58
59 struct dma_map_ops {
60 void* (*alloc_coherent)(struct device *dev, size_t size,
61 dma_addr_t *dma_handle, gfp_t gfp);
62 void (*free_coherent)(struct device *dev, size_t size,
63 void *vaddr, dma_addr_t dma_handle);
64 dma_addr_t (*map_page)(struct device *dev, struct page *page,
65 unsigned long offset, size_t size, enum dma_data_direction dir,
66 unsigned long attrs);
67 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
68 size_t size, enum dma_data_direction dir, unsigned long attrs);
69 int (*map_sg)(struct device *dev, struct scatterlist *sg,
70 int nents, enum dma_data_direction dir, unsigned long attrs);
71 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
72 enum dma_data_direction dir, unsigned long attrs);
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, enum dma_data_direction dir);
77 void (*sync_single_range_for_cpu)(struct device *dev,
78 dma_addr_t dma_handle, unsigned long offset, size_t size,
79 enum dma_data_direction dir);
80 void (*sync_single_range_for_device)(struct device *dev,
81 dma_addr_t dma_handle, unsigned long offset, size_t size,
82 enum dma_data_direction dir);
83 void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
84 int nents, enum dma_data_direction dir);
85 void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
86 int nents, enum dma_data_direction dir);
87 int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
88 int (*dma_supported)(struct device *dev, u64 mask);
89 int is_phys;
90 };
91
92 #define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL)
93
94 int linux_dma_tag_init(struct device *, u64);
95 int linux_dma_tag_init_coherent(struct device *, u64);
96 void *linux_dma_alloc_coherent(struct device *dev, size_t size,
97 dma_addr_t *dma_handle, gfp_t flag);
98 dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len);
99 void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size);
100 int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
101 int nents, enum dma_data_direction dir __unused,
102 unsigned long attrs __unused);
103 void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
104 int nents __unused, enum dma_data_direction dir __unused,
105 unsigned long attrs __unused);
106 void linuxkpi_dma_sync(struct device *, dma_addr_t, size_t, bus_dmasync_op_t);
107
108 static inline int
dma_supported(struct device * dev,u64 dma_mask)109 dma_supported(struct device *dev, u64 dma_mask)
110 {
111
112 /* XXX busdma takes care of this elsewhere. */
113 return (1);
114 }
115
116 static inline int
dma_set_mask(struct device * dev,u64 dma_mask)117 dma_set_mask(struct device *dev, u64 dma_mask)
118 {
119
120 if (!dev->dma_priv || !dma_supported(dev, dma_mask))
121 return -EIO;
122
123 return (linux_dma_tag_init(dev, dma_mask));
124 }
125
126 static inline int
dma_set_coherent_mask(struct device * dev,u64 dma_mask)127 dma_set_coherent_mask(struct device *dev, u64 dma_mask)
128 {
129
130 if (!dev->dma_priv || !dma_supported(dev, dma_mask))
131 return -EIO;
132
133 return (linux_dma_tag_init_coherent(dev, dma_mask));
134 }
135
136 static inline int
dma_set_mask_and_coherent(struct device * dev,u64 dma_mask)137 dma_set_mask_and_coherent(struct device *dev, u64 dma_mask)
138 {
139 int r;
140
141 r = dma_set_mask(dev, dma_mask);
142 if (r == 0)
143 dma_set_coherent_mask(dev, dma_mask);
144 return (r);
145 }
146
147 static inline void *
dma_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag)148 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
149 gfp_t flag)
150 {
151 return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));
152 }
153
154 static inline void *
dma_zalloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag)155 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
156 gfp_t flag)
157 {
158
159 return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
160 }
161
162 static inline void
dma_free_coherent(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_addr)163 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
164 dma_addr_t dma_addr)
165 {
166
167 linux_dma_unmap(dev, dma_addr, size);
168 kmem_free((vm_offset_t)cpu_addr, size);
169 }
170
171 static inline dma_addr_t
dma_map_page_attrs(struct device * dev,struct page * page,size_t offset,size_t size,enum dma_data_direction dir,unsigned long attrs)172 dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,
173 size_t size, enum dma_data_direction dir, unsigned long attrs)
174 {
175
176 return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size));
177 }
178
179 /* linux_dma_(un)map_sg_attrs does not support attrs yet */
180 #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
181 linux_dma_map_sg_attrs(dev, sgl, nents, dir, 0)
182
183 #define dma_unmap_sg_attrs(dev, sg, nents, dir, attrs) \
184 linux_dma_unmap_sg_attrs(dev, sg, nents, dir, 0)
185
186 static inline dma_addr_t
dma_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction direction)187 dma_map_page(struct device *dev, struct page *page,
188 unsigned long offset, size_t size, enum dma_data_direction direction)
189 {
190
191 return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size));
192 }
193
194 static inline void
dma_unmap_page(struct device * dev,dma_addr_t dma_address,size_t size,enum dma_data_direction direction)195 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
196 enum dma_data_direction direction)
197 {
198
199 linux_dma_unmap(dev, dma_address, size);
200 }
201
202 static inline void
dma_sync_single_for_cpu(struct device * dev,dma_addr_t dma,size_t size,enum dma_data_direction direction)203 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma, size_t size,
204 enum dma_data_direction direction)
205 {
206 bus_dmasync_op_t op;
207
208 switch (direction) {
209 case DMA_BIDIRECTIONAL:
210 op = BUS_DMASYNC_POSTREAD;
211 linuxkpi_dma_sync(dev, dma, size, op);
212 op = BUS_DMASYNC_PREREAD;
213 break;
214 case DMA_TO_DEVICE:
215 op = BUS_DMASYNC_POSTWRITE;
216 break;
217 case DMA_FROM_DEVICE:
218 op = BUS_DMASYNC_POSTREAD;
219 break;
220 default:
221 return;
222 }
223
224 linuxkpi_dma_sync(dev, dma, size, op);
225 }
226
227 static inline void
dma_sync_single(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)228 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
229 enum dma_data_direction dir)
230 {
231 dma_sync_single_for_cpu(dev, addr, size, dir);
232 }
233
234 static inline void
dma_sync_single_for_device(struct device * dev,dma_addr_t dma,size_t size,enum dma_data_direction direction)235 dma_sync_single_for_device(struct device *dev, dma_addr_t dma,
236 size_t size, enum dma_data_direction direction)
237 {
238 bus_dmasync_op_t op;
239
240 switch (direction) {
241 case DMA_BIDIRECTIONAL:
242 op = BUS_DMASYNC_PREWRITE;
243 break;
244 case DMA_TO_DEVICE:
245 op = BUS_DMASYNC_PREREAD;
246 break;
247 case DMA_FROM_DEVICE:
248 op = BUS_DMASYNC_PREWRITE;
249 break;
250 default:
251 return;
252 }
253
254 linuxkpi_dma_sync(dev, dma, size, op);
255 }
256
257 static inline void
dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction direction)258 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
259 enum dma_data_direction direction)
260 {
261 }
262
263 static inline void
dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction direction)264 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
265 enum dma_data_direction direction)
266 {
267 }
268
269 static inline void
dma_sync_single_range_for_cpu(struct device * dev,dma_addr_t dma_handle,unsigned long offset,size_t size,int direction)270 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
271 unsigned long offset, size_t size, int direction)
272 {
273 }
274
275 static inline void
dma_sync_single_range_for_device(struct device * dev,dma_addr_t dma_handle,unsigned long offset,size_t size,int direction)276 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
277 unsigned long offset, size_t size, int direction)
278 {
279 }
280
281 static inline int
dma_mapping_error(struct device * dev,dma_addr_t dma_addr)282 dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
283 {
284
285 return (dma_addr == 0);
286 }
287
dma_set_max_seg_size(struct device * dev,unsigned int size)288 static inline unsigned int dma_set_max_seg_size(struct device *dev,
289 unsigned int size)
290 {
291 return (0);
292 }
293
294 static inline dma_addr_t
_dma_map_single_attrs(struct device * dev,void * ptr,size_t size,enum dma_data_direction direction,unsigned long attrs __unused)295 _dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
296 enum dma_data_direction direction, unsigned long attrs __unused)
297 {
298 dma_addr_t dma;
299
300 dma = linux_dma_map_phys(dev, vtophys(ptr), size);
301 if (!dma_mapping_error(dev, dma))
302 dma_sync_single_for_device(dev, dma, size, direction);
303
304 return (dma);
305 }
306
307 static inline void
_dma_unmap_single_attrs(struct device * dev,dma_addr_t dma,size_t size,enum dma_data_direction direction,unsigned long attrs __unused)308 _dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size,
309 enum dma_data_direction direction, unsigned long attrs __unused)
310 {
311
312 dma_sync_single_for_cpu(dev, dma, size, direction);
313 linux_dma_unmap(dev, dma, size);
314 }
315
316 #define dma_map_single_attrs(dev, ptr, size, dir, attrs) \
317 _dma_map_single_attrs(dev, ptr, size, dir, 0)
318
319 #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
320 _dma_unmap_single_attrs(dev, dma_addr, size, dir, 0)
321
322 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
323 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
324 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
325 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
326
327 #define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name
328 #define DEFINE_DMA_UNMAP_LEN(name) __u32 name
329 #define dma_unmap_addr(p, name) ((p)->name)
330 #define dma_unmap_addr_set(p, name, v) (((p)->name) = (v))
331 #define dma_unmap_len(p, name) ((p)->name)
332 #define dma_unmap_len_set(p, name, v) (((p)->name) = (v))
333
334 extern int uma_align_cache;
335 #define dma_get_cache_alignment() uma_align_cache
336
337 #endif /* _LINUXKPI_LINUX_DMA_MAPPING_H_ */
338