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