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