xref: /freebsd/sys/compat/linuxkpi/common/include/linux/dma-mapping.h (revision c3fa17a6a331db8aad03e9cb89ba97e1811ab381)
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/scatterlist.h>
36 #include <linux/mm.h>
37 #include <linux/page.h>
38 #include <linux/sizes.h>
39 
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 
43 #include <vm/vm.h>
44 #include <vm/vm_page.h>
45 #include <vm/uma_align_mask.h>
46 #include <vm/pmap.h>
47 
48 #include <machine/bus.h>
49 
50 #define	DMA_ATTR_WRITE_BARRIER		(1 << 0)
51 #define	DMA_ATTR_WEAK_ORDERING		(1 << 1)
52 #define	DMA_ATTR_WRITE_COMBINE		(1 << 2)
53 #define	DMA_ATTR_NON_CONSISTENT		(1 << 3)
54 #define	DMA_ATTR_NO_KERNEL_MAPPING	(1 << 4)
55 #define	DMA_ATTR_SKIP_CPU_SYNC		(1 << 5)
56 #define	DMA_ATTR_FORCE_CONTIGUOUS	(1 << 6)
57 #define	DMA_ATTR_ALLOC_SINGLE_PAGES	(1 << 7)
58 #define	DMA_ATTR_NO_WARN		(1 << 8)
59 #define	DMA_ATTR_PRIVILEGED		(1 << 9)
60 
61 enum dma_data_direction {
62 	DMA_BIDIRECTIONAL = 0,
63 	DMA_TO_DEVICE = 1,
64 	DMA_FROM_DEVICE = 2,
65 	DMA_NONE = 3,
66 };
67 
68 struct dma_map_ops {
69 	void* (*alloc_coherent)(struct device *dev, size_t size,
70 	    dma_addr_t *dma_handle, gfp_t gfp);
71 	void (*free_coherent)(struct device *dev, size_t size,
72 	    void *vaddr, dma_addr_t dma_handle);
73 	dma_addr_t (*map_page)(struct device *dev, struct page *page,
74 	    unsigned long offset, size_t size, enum dma_data_direction dir,
75 	    unsigned long attrs);
76 	void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
77 	    size_t size, enum dma_data_direction dir, unsigned long attrs);
78 	int (*map_sg)(struct device *dev, struct scatterlist *sg,
79 	    int nents, enum dma_data_direction dir, unsigned long attrs);
80 	void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
81 	    enum dma_data_direction dir, unsigned long attrs);
82 	void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
83 	    size_t size, enum dma_data_direction dir);
84 	void (*sync_single_for_device)(struct device *dev,
85 	    dma_addr_t dma_handle, size_t size, enum dma_data_direction dir);
86 	void (*sync_single_range_for_cpu)(struct device *dev,
87 	    dma_addr_t dma_handle, unsigned long offset, size_t size,
88 	    enum dma_data_direction dir);
89 	void (*sync_single_range_for_device)(struct device *dev,
90 	    dma_addr_t dma_handle, unsigned long offset, size_t size,
91 	    enum dma_data_direction dir);
92 	void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
93 	    int nents, enum dma_data_direction dir);
94 	void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
95 	    int nents, enum dma_data_direction dir);
96 	int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
97 	int (*dma_supported)(struct device *dev, u64 mask);
98 	int is_phys;
99 };
100 
101 #define	DMA_BIT_MASK(n)	((2ULL << ((n) - 1)) - 1ULL)
102 
103 int linux_dma_tag_init(struct device *, u64);
104 int linux_dma_tag_init_coherent(struct device *, u64);
105 void *linux_dma_alloc_coherent(struct device *dev, size_t size,
106     dma_addr_t *dma_handle, gfp_t flag);
107 void *linuxkpi_dmam_alloc_coherent(struct device *dev, size_t size,
108     dma_addr_t *dma_handle, gfp_t flag);
109 void linuxkpi_dmam_free_coherent(struct device *dev, size_t size,
110     void *addr, dma_addr_t dma_handle);
111 void *linuxkpi_dma_alloc_noncoherent(struct device *, size_t, dma_addr_t *,
112     enum dma_data_direction, gfp_t);
113 void linuxkpi_dma_free_noncoherent(struct device *, size_t, void *,
114     dma_addr_t, enum dma_data_direction);
115 void *linuxkpi_dma_alloc_attrs(struct device *, size_t, dma_addr_t *,
116     gfp_t, unsigned long);
117 void linuxkpi_dma_free_attrs(struct device *, size_t, void *,
118     dma_addr_t, unsigned long);
119 dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len);	/* backward compat */
120 dma_addr_t lkpi_dma_map_phys(struct device *, vm_paddr_t, size_t,
121     enum dma_data_direction, unsigned long);
122 void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size);	/* backward compat */
123 void lkpi_dma_unmap(struct device *, dma_addr_t, size_t,
124     enum dma_data_direction, unsigned long);
125 int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
126     int nents, enum dma_data_direction direction,
127     unsigned long attrs);
128 void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
129     int nents __unused, enum dma_data_direction direction,
130     unsigned long attrs);
131 void linuxkpi_dma_sync(struct device *, dma_addr_t, size_t, bus_dmasync_op_t);
132 void lkpi_dma_sync_sg(struct device *, struct scatterlist *, bus_dmasync_op_t);
133 
134 static inline int
135 dma_supported(struct device *dev, u64 dma_mask)
136 {
137 
138 	/* XXX busdma takes care of this elsewhere. */
139 	return (1);
140 }
141 
142 static inline int
143 dma_set_mask(struct device *dev, u64 dma_mask)
144 {
145 
146 	if (!dev->dma_priv || !dma_supported(dev, dma_mask))
147 		return -EIO;
148 
149 	return (linux_dma_tag_init(dev, dma_mask));
150 }
151 
152 static inline int
153 dma_set_coherent_mask(struct device *dev, u64 dma_mask)
154 {
155 
156 	if (!dev->dma_priv || !dma_supported(dev, dma_mask))
157 		return -EIO;
158 
159 	return (linux_dma_tag_init_coherent(dev, dma_mask));
160 }
161 
162 static inline int
163 dma_set_mask_and_coherent(struct device *dev, u64 dma_mask)
164 {
165 	int r;
166 
167 	r = dma_set_mask(dev, dma_mask);
168 	if (r == 0)
169 		dma_set_coherent_mask(dev, dma_mask);
170 	return (r);
171 }
172 
173 static inline void *
174 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
175     gfp_t flag)
176 {
177 	return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));
178 }
179 
180 static inline void *
181 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
182     gfp_t flag)
183 {
184 
185 	return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
186 }
187 
188 static inline void *
189 dmam_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
190     gfp_t flag)
191 {
192 
193 	return (linuxkpi_dmam_alloc_coherent(dev, size, dma_handle, flag));
194 }
195 
196 static inline void
197 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
198     dma_addr_t dma_addr)
199 {
200 
201 	lkpi_dma_unmap(dev, dma_addr, size, DMA_BIDIRECTIONAL, 0);
202 	kmem_free(cpu_addr, size);
203 }
204 
205 static inline void
206 dmam_free_coherent(struct device *dev, size_t size, void *addr,
207     dma_addr_t dma_handle)
208 {
209 	linuxkpi_dmam_free_coherent(dev, size, addr, dma_handle);
210 }
211 
212 static inline void *
213 dma_alloc_noncoherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
214     enum dma_data_direction direction, gfp_t gfp)
215 {
216 	return (linuxkpi_dma_alloc_noncoherent(dev, size, dma_handle, direction, gfp));
217 }
218 
219 static inline void
220 dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
221     dma_addr_t dma_handle, enum dma_data_direction direction)
222 {
223 	linuxkpi_dma_free_noncoherent(dev, size, vaddr, dma_handle, direction);
224 }
225 
226 static inline void *
227 dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
228     gfp_t gfp, unsigned long attrs)
229 {
230 	return (linuxkpi_dma_alloc_attrs(dev, size, dma_handle, gfp, attrs));
231 }
232 
233 static inline void
234 dma_free_attrs(struct device *dev, size_t size, void *vaddr,
235     dma_addr_t dma_handle, unsigned long attrs)
236 {
237 	linuxkpi_dma_free_attrs(dev, size, vaddr, dma_handle, attrs);
238 }
239 
240 static inline dma_addr_t
241 dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,
242     size_t size, enum dma_data_direction direction, unsigned long attrs)
243 {
244 
245 	return (lkpi_dma_map_phys(dev, page_to_phys(page) + offset, size,
246 	    direction, attrs));
247 }
248 
249 static inline void
250 dma_unmap_page_attrs(struct device *dev, dma_addr_t dma_address, size_t size,
251     enum dma_data_direction direction, unsigned long attrs)
252 {
253        lkpi_dma_unmap(dev, dma_address, size, direction, attrs);
254 }
255 
256 /* linux_dma_(un)map_sg_attrs does not support attrs yet */
257 #define	dma_map_sg_attrs(dev, sgl, nents, dir, attrs)	\
258 	linux_dma_map_sg_attrs(dev, sgl, nents, dir, attrs)
259 
260 #define	dma_unmap_sg_attrs(dev, sg, nents, dir, attrs)	\
261 	linux_dma_unmap_sg_attrs(dev, sg, nents, dir, attrs)
262 
263 static inline dma_addr_t
264 dma_map_page(struct device *dev, struct page *page,
265     unsigned long offset, size_t size, enum dma_data_direction direction)
266 {
267 
268 	return (lkpi_dma_map_phys(dev, page_to_phys(page) + offset, size,
269 	    direction, 0));
270 }
271 
272 static inline void
273 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
274     enum dma_data_direction direction)
275 {
276 
277 	lkpi_dma_unmap(dev, dma_address, size, direction, 0);
278 }
279 
280 static inline dma_addr_t
281 dma_map_resource(struct device *dev, phys_addr_t paddr, size_t size,
282     enum dma_data_direction direction, unsigned long attrs)
283 {
284 	return (lkpi_dma_map_phys(dev, paddr, size, direction, attrs));
285 }
286 
287 static inline void
288 dma_unmap_resource(struct device *dev, dma_addr_t dma, size_t size,
289     enum dma_data_direction direction, unsigned long attrs)
290 {
291 	lkpi_dma_unmap(dev, dma, size, direction, attrs);
292 }
293 
294 static inline void
295 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma, size_t size,
296     enum dma_data_direction direction)
297 {
298 	bus_dmasync_op_t op;
299 
300 	switch (direction) {
301 	case DMA_BIDIRECTIONAL:
302 		op = BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE;
303 		break;
304 	case DMA_TO_DEVICE:
305 		op = BUS_DMASYNC_POSTWRITE;
306 		break;
307 	case DMA_FROM_DEVICE:
308 		op = BUS_DMASYNC_POSTREAD;
309 		break;
310 	default:
311 		return;
312 	}
313 
314 	linuxkpi_dma_sync(dev, dma, size, op);
315 }
316 
317 static inline void
318 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
319     enum dma_data_direction dir)
320 {
321 	dma_sync_single_for_cpu(dev, addr, size, dir);
322 }
323 
324 static inline void
325 dma_sync_single_for_device(struct device *dev, dma_addr_t dma,
326     size_t size, enum dma_data_direction direction)
327 {
328 	bus_dmasync_op_t op;
329 
330 	switch (direction) {
331 	case DMA_BIDIRECTIONAL:
332 		op = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD;
333 		break;
334 	case DMA_TO_DEVICE:
335 		op = BUS_DMASYNC_PREWRITE;
336 		break;
337 	case DMA_FROM_DEVICE:
338 		op = BUS_DMASYNC_PREREAD;
339 		break;
340 	default:
341 		return;
342 	}
343 
344 	linuxkpi_dma_sync(dev, dma, size, op);
345 }
346 
347 static inline void
348 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
349     enum dma_data_direction direction)
350 {
351 	bus_dmasync_op_t op;
352 
353 	switch (direction) {
354 	case DMA_BIDIRECTIONAL:
355 		op = BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE;
356 		break;
357 	case DMA_TO_DEVICE:
358 		op = BUS_DMASYNC_POSTWRITE;
359 		break;
360 	case DMA_FROM_DEVICE:
361 		op = BUS_DMASYNC_POSTREAD;
362 		break;
363 	default:
364 		return;
365 	}
366 
367 	lkpi_dma_sync_sg(dev, sg, op);
368 }
369 
370 static inline void dma_sync_sgtable_for_cpu(struct device *dev,
371 		struct sg_table *sgt, enum dma_data_direction dir)
372 {
373 	dma_sync_sg_for_cpu(dev, sgt->sgl, sgt->orig_nents, dir);
374 }
375 
376 static inline void
377 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
378     enum dma_data_direction direction)
379 {
380 	bus_dmasync_op_t op;
381 
382 	switch (direction) {
383 	case DMA_BIDIRECTIONAL:
384 		op = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD;
385 		break;
386 	case DMA_TO_DEVICE:
387 		op = BUS_DMASYNC_PREWRITE;
388 		break;
389 	case DMA_FROM_DEVICE:
390 		op = BUS_DMASYNC_PREREAD;
391 		break;
392 	default:
393 		return;
394 	}
395 
396 	lkpi_dma_sync_sg(dev, sg, op);
397 }
398 
399 static inline void dma_sync_sgtable_for_device(struct device *dev,
400 		struct sg_table *sgt, enum dma_data_direction dir)
401 {
402 	dma_sync_sg_for_device(dev, sgt->sgl, sgt->orig_nents, dir);
403 }
404 
405 /* (20250329) These two seem to be unused code. */
406 static inline void
407 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
408     unsigned long offset, size_t size, enum dma_data_direction direction)
409 {
410 	pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
411 }
412 
413 static inline void
414 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
415     unsigned long offset, size_t size, enum dma_data_direction direction)
416 {
417 	pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
418 }
419 
420 #define	DMA_MAPPING_ERROR	(~(dma_addr_t)0)
421 
422 static inline int
423 dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
424 {
425 
426 	if (dma_addr == 0 || dma_addr == DMA_MAPPING_ERROR)
427 		return (-ENOMEM);
428 	return (0);
429 }
430 
431 static inline unsigned int dma_set_max_seg_size(struct device *dev,
432     unsigned int size)
433 {
434 	return (0);
435 }
436 
437 static inline dma_addr_t
438 _dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
439     enum dma_data_direction direction, unsigned long attrs)
440 {
441 	return (lkpi_dma_map_phys(dev, vtophys(ptr), size,
442 	    direction, attrs));
443 }
444 
445 static inline void
446 _dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size,
447     enum dma_data_direction direction, unsigned long attrs)
448 {
449 	lkpi_dma_unmap(dev, dma, size, direction, attrs);
450 }
451 
452 static inline size_t
453 dma_max_mapping_size(struct device *dev)
454 {
455 
456 	return (SCATTERLIST_MAX_SEGMENT);
457 }
458 
459 #define	dma_map_single_attrs(dev, ptr, size, dir, attrs)	\
460 	_dma_map_single_attrs(dev, ptr, size, dir, attrs)
461 
462 #define	dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs)	\
463 	_dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs)
464 
465 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
466 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
467 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
468 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
469 
470 #define	DEFINE_DMA_UNMAP_ADDR(name)		dma_addr_t name
471 #define	DEFINE_DMA_UNMAP_LEN(name)		__u32 name
472 #define	dma_unmap_addr(p, name)			((p)->name)
473 #define	dma_unmap_addr_set(p, name, v)		(((p)->name) = (v))
474 #define	dma_unmap_len(p, name)			((p)->name)
475 #define	dma_unmap_len_set(p, name, v)		(((p)->name) = (v))
476 
477 #define	dma_get_cache_alignment()	(uma_get_cache_align_mask() + 1)
478 
479 
480 static inline int
481 dma_map_sgtable(struct device *dev, struct sg_table *sgt,
482     enum dma_data_direction dir,
483     unsigned long attrs)
484 {
485 	int nents;
486 
487 	nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
488 	if (nents < 0)
489 		return (nents);
490 	sgt->nents = nents;
491 	return (0);
492 }
493 
494 static inline void
495 dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
496     enum dma_data_direction dir,
497     unsigned long attrs)
498 {
499 
500 	dma_unmap_sg_attrs(dev, sgt->sgl, sgt->orig_nents, dir, attrs);
501 }
502 
503 
504 #endif	/* _LINUXKPI_LINUX_DMA_MAPPING_H_ */
505