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 #ifndef _LINUXKPI_LINUX_DMA_MAPPING_H_
30 #define _LINUXKPI_LINUX_DMA_MAPPING_H_
31
32 #include <linux/types.h>
33 #include <linux/device.h>
34 #include <linux/err.h>
35 #include <linux/dma-attrs.h>
36 #include <linux/scatterlist.h>
37 #include <linux/mm.h>
38 #include <linux/page.h>
39 #include <linux/sizes.h>
40
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_page.h>
46 #include <vm/uma_align_mask.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 unsigned long attrs);
66 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
67 size_t size, enum dma_data_direction dir, unsigned long attrs);
68 int (*map_sg)(struct device *dev, struct scatterlist *sg,
69 int nents, enum dma_data_direction dir, unsigned long attrs);
70 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
71 enum dma_data_direction dir, unsigned long 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 *, u64);
94 int linux_dma_tag_init_coherent(struct device *, u64);
95 void *linux_dma_alloc_coherent(struct device *dev, size_t size,
96 dma_addr_t *dma_handle, gfp_t flag);
97 void *linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size,
98 dma_addr_t *dma_handle, gfp_t flag);
99 dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len);
100 void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size);
101 int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
102 int nents, enum dma_data_direction dir __unused,
103 unsigned long attrs __unused);
104 void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
105 int nents __unused, enum dma_data_direction dir __unused,
106 unsigned long attrs __unused);
107 void linuxkpi_dma_sync(struct device *, dma_addr_t, size_t, bus_dmasync_op_t);
108
109 static inline int
dma_supported(struct device * dev,u64 dma_mask)110 dma_supported(struct device *dev, u64 dma_mask)
111 {
112
113 /* XXX busdma takes care of this elsewhere. */
114 return (1);
115 }
116
117 static inline int
dma_set_mask(struct device * dev,u64 dma_mask)118 dma_set_mask(struct device *dev, u64 dma_mask)
119 {
120
121 if (!dev->dma_priv || !dma_supported(dev, dma_mask))
122 return -EIO;
123
124 return (linux_dma_tag_init(dev, dma_mask));
125 }
126
127 static inline int
dma_set_coherent_mask(struct device * dev,u64 dma_mask)128 dma_set_coherent_mask(struct device *dev, u64 dma_mask)
129 {
130
131 if (!dev->dma_priv || !dma_supported(dev, dma_mask))
132 return -EIO;
133
134 return (linux_dma_tag_init_coherent(dev, dma_mask));
135 }
136
137 static inline int
dma_set_mask_and_coherent(struct device * dev,u64 dma_mask)138 dma_set_mask_and_coherent(struct device *dev, u64 dma_mask)
139 {
140 int r;
141
142 r = dma_set_mask(dev, dma_mask);
143 if (r == 0)
144 dma_set_coherent_mask(dev, dma_mask);
145 return (r);
146 }
147
148 static inline void *
dma_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag)149 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
150 gfp_t flag)
151 {
152 return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));
153 }
154
155 static inline void *
dma_zalloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag)156 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
157 gfp_t flag)
158 {
159
160 return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
161 }
162
163 static inline void *
dmam_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t flag)164 dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
165 gfp_t flag)
166 {
167
168 return (linuxkpi_dmam_alloc_coherent(dev, size, dma_handle, flag));
169 }
170
171 static inline void
dma_free_coherent(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_addr)172 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
173 dma_addr_t dma_addr)
174 {
175
176 linux_dma_unmap(dev, dma_addr, size);
177 kmem_free(cpu_addr, size);
178 }
179
180 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)181 dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,
182 size_t size, enum dma_data_direction dir, unsigned long attrs)
183 {
184
185 return (linux_dma_map_phys(dev, page_to_phys(page) + offset, size));
186 }
187
188 /* linux_dma_(un)map_sg_attrs does not support attrs yet */
189 #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
190 linux_dma_map_sg_attrs(dev, sgl, nents, dir, 0)
191
192 #define dma_unmap_sg_attrs(dev, sg, nents, dir, attrs) \
193 linux_dma_unmap_sg_attrs(dev, sg, nents, dir, 0)
194
195 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)196 dma_map_page(struct device *dev, struct page *page,
197 unsigned long offset, size_t size, enum dma_data_direction direction)
198 {
199
200 return (linux_dma_map_phys(dev, page_to_phys(page) + offset, size));
201 }
202
203 static inline void
dma_unmap_page(struct device * dev,dma_addr_t dma_address,size_t size,enum dma_data_direction direction)204 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
205 enum dma_data_direction direction)
206 {
207
208 linux_dma_unmap(dev, dma_address, size);
209 }
210
211 static inline void
dma_sync_single_for_cpu(struct device * dev,dma_addr_t dma,size_t size,enum dma_data_direction direction)212 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma, size_t size,
213 enum dma_data_direction direction)
214 {
215 bus_dmasync_op_t op;
216
217 switch (direction) {
218 case DMA_BIDIRECTIONAL:
219 op = BUS_DMASYNC_POSTREAD;
220 linuxkpi_dma_sync(dev, dma, size, op);
221 op = BUS_DMASYNC_PREREAD;
222 break;
223 case DMA_TO_DEVICE:
224 op = BUS_DMASYNC_POSTWRITE;
225 break;
226 case DMA_FROM_DEVICE:
227 op = BUS_DMASYNC_POSTREAD;
228 break;
229 default:
230 return;
231 }
232
233 linuxkpi_dma_sync(dev, dma, size, op);
234 }
235
236 static inline void
dma_sync_single(struct device * dev,dma_addr_t addr,size_t size,enum dma_data_direction dir)237 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
238 enum dma_data_direction dir)
239 {
240 dma_sync_single_for_cpu(dev, addr, size, dir);
241 }
242
243 static inline void
dma_sync_single_for_device(struct device * dev,dma_addr_t dma,size_t size,enum dma_data_direction direction)244 dma_sync_single_for_device(struct device *dev, dma_addr_t dma,
245 size_t size, enum dma_data_direction direction)
246 {
247 bus_dmasync_op_t op;
248
249 switch (direction) {
250 case DMA_BIDIRECTIONAL:
251 op = BUS_DMASYNC_PREWRITE;
252 break;
253 case DMA_TO_DEVICE:
254 op = BUS_DMASYNC_PREREAD;
255 break;
256 case DMA_FROM_DEVICE:
257 op = BUS_DMASYNC_PREWRITE;
258 break;
259 default:
260 return;
261 }
262
263 linuxkpi_dma_sync(dev, dma, size, op);
264 }
265
266 static inline void
dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction direction)267 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
268 enum dma_data_direction direction)
269 {
270 }
271
272 static inline void
dma_sync_sg_for_device(struct device * dev,struct scatterlist * sg,int nelems,enum dma_data_direction direction)273 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
274 enum dma_data_direction direction)
275 {
276 }
277
278 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)279 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
280 unsigned long offset, size_t size, int direction)
281 {
282 }
283
284 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)285 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
286 unsigned long offset, size_t size, int direction)
287 {
288 }
289
290 #define DMA_MAPPING_ERROR (~(dma_addr_t)0)
291
292 static inline int
dma_mapping_error(struct device * dev,dma_addr_t dma_addr)293 dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
294 {
295
296 if (dma_addr == 0 || dma_addr == DMA_MAPPING_ERROR)
297 return (-ENOMEM);
298 return (0);
299 }
300
dma_set_max_seg_size(struct device * dev,unsigned int size)301 static inline unsigned int dma_set_max_seg_size(struct device *dev,
302 unsigned int size)
303 {
304 return (0);
305 }
306
307 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)308 _dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
309 enum dma_data_direction direction, unsigned long attrs __unused)
310 {
311 dma_addr_t dma;
312
313 dma = linux_dma_map_phys(dev, vtophys(ptr), size);
314 if (!dma_mapping_error(dev, dma))
315 dma_sync_single_for_device(dev, dma, size, direction);
316
317 return (dma);
318 }
319
320 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)321 _dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size,
322 enum dma_data_direction direction, unsigned long attrs __unused)
323 {
324
325 dma_sync_single_for_cpu(dev, dma, size, direction);
326 linux_dma_unmap(dev, dma, size);
327 }
328
329 static inline size_t
dma_max_mapping_size(struct device * dev)330 dma_max_mapping_size(struct device *dev)
331 {
332
333 return (SCATTERLIST_MAX_SEGMENT);
334 }
335
336 #define dma_map_single_attrs(dev, ptr, size, dir, attrs) \
337 _dma_map_single_attrs(dev, ptr, size, dir, 0)
338
339 #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
340 _dma_unmap_single_attrs(dev, dma_addr, size, dir, 0)
341
342 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
343 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
344 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
345 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
346
347 #define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name
348 #define DEFINE_DMA_UNMAP_LEN(name) __u32 name
349 #define dma_unmap_addr(p, name) ((p)->name)
350 #define dma_unmap_addr_set(p, name, v) (((p)->name) = (v))
351 #define dma_unmap_len(p, name) ((p)->name)
352 #define dma_unmap_len_set(p, name, v) (((p)->name) = (v))
353
354 #define dma_get_cache_alignment() (uma_get_cache_align_mask() + 1)
355
356
357 static inline int
dma_map_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)358 dma_map_sgtable(struct device *dev, struct sg_table *sgt,
359 enum dma_data_direction dir,
360 unsigned long attrs)
361 {
362 int nents;
363
364 nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
365 if (nents < 0)
366 return (nents);
367 sgt->nents = nents;
368 return (0);
369 }
370
371 static inline void
dma_unmap_sgtable(struct device * dev,struct sg_table * sgt,enum dma_data_direction dir,unsigned long attrs)372 dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
373 enum dma_data_direction dir,
374 unsigned long attrs)
375 {
376
377 dma_unmap_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
378 }
379
380
381 #endif /* _LINUXKPI_LINUX_DMA_MAPPING_H_ */
382