xref: /freebsd/sys/compat/linuxkpi/common/include/linux/dma-mapping.h (revision d7ed56b0fcb310a466e75a47cac4619afaa7e9dd)
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 | BUS_DMASYNC_POSTWRITE;
266 		break;
267 	case DMA_TO_DEVICE:
268 		op = BUS_DMASYNC_POSTWRITE;
269 		break;
270 	case DMA_FROM_DEVICE:
271 		op = BUS_DMASYNC_POSTREAD;
272 		break;
273 	default:
274 		return;
275 	}
276 
277 	linuxkpi_dma_sync(dev, dma, size, op);
278 }
279 
280 static inline void
281 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
282     enum dma_data_direction dir)
283 {
284 	dma_sync_single_for_cpu(dev, addr, size, dir);
285 }
286 
287 static inline void
288 dma_sync_single_for_device(struct device *dev, dma_addr_t dma,
289     size_t size, enum dma_data_direction direction)
290 {
291 	bus_dmasync_op_t op;
292 
293 	switch (direction) {
294 	case DMA_BIDIRECTIONAL:
295 		op = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD;
296 		break;
297 	case DMA_TO_DEVICE:
298 		op = BUS_DMASYNC_PREWRITE;
299 		break;
300 	case DMA_FROM_DEVICE:
301 		op = BUS_DMASYNC_PREREAD;
302 		break;
303 	default:
304 		return;
305 	}
306 
307 	linuxkpi_dma_sync(dev, dma, size, op);
308 }
309 
310 /* (20250329) These four seem to be unused code. */
311 static inline void
312 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
313     enum dma_data_direction direction)
314 {
315 	pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
316 }
317 
318 static inline void
319 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
320     enum dma_data_direction direction)
321 {
322 	pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
323 }
324 
325 static inline void
326 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
327     unsigned long offset, size_t size, enum dma_data_direction direction)
328 {
329 	pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
330 }
331 
332 static inline void
333 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
334     unsigned long offset, size_t size, enum dma_data_direction direction)
335 {
336 	pr_debug("%s:%d: TODO dir %d\n", __func__, __LINE__, direction);
337 }
338 
339 #define	DMA_MAPPING_ERROR	(~(dma_addr_t)0)
340 
341 static inline int
342 dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
343 {
344 
345 	if (dma_addr == 0 || dma_addr == DMA_MAPPING_ERROR)
346 		return (-ENOMEM);
347 	return (0);
348 }
349 
350 static inline unsigned int dma_set_max_seg_size(struct device *dev,
351     unsigned int size)
352 {
353 	return (0);
354 }
355 
356 static inline dma_addr_t
357 _dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
358     enum dma_data_direction direction, unsigned long attrs)
359 {
360 	return (lkpi_dma_map_phys(dev, vtophys(ptr), size,
361 	    direction, attrs));
362 }
363 
364 static inline void
365 _dma_unmap_single_attrs(struct device *dev, dma_addr_t dma, size_t size,
366     enum dma_data_direction direction, unsigned long attrs)
367 {
368 	lkpi_dma_unmap(dev, dma, size, direction, attrs);
369 }
370 
371 static inline size_t
372 dma_max_mapping_size(struct device *dev)
373 {
374 
375 	return (SCATTERLIST_MAX_SEGMENT);
376 }
377 
378 #define	dma_map_single_attrs(dev, ptr, size, dir, attrs)	\
379 	_dma_map_single_attrs(dev, ptr, size, dir, attrs)
380 
381 #define	dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs)	\
382 	_dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs)
383 
384 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
385 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
386 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
387 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
388 
389 #define	DEFINE_DMA_UNMAP_ADDR(name)		dma_addr_t name
390 #define	DEFINE_DMA_UNMAP_LEN(name)		__u32 name
391 #define	dma_unmap_addr(p, name)			((p)->name)
392 #define	dma_unmap_addr_set(p, name, v)		(((p)->name) = (v))
393 #define	dma_unmap_len(p, name)			((p)->name)
394 #define	dma_unmap_len_set(p, name, v)		(((p)->name) = (v))
395 
396 #define	dma_get_cache_alignment()	(uma_get_cache_align_mask() + 1)
397 
398 
399 static inline int
400 dma_map_sgtable(struct device *dev, struct sg_table *sgt,
401     enum dma_data_direction dir,
402     unsigned long attrs)
403 {
404 	int nents;
405 
406 	nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
407 	if (nents < 0)
408 		return (nents);
409 	sgt->nents = nents;
410 	return (0);
411 }
412 
413 static inline void
414 dma_unmap_sgtable(struct device *dev, struct sg_table *sgt,
415     enum dma_data_direction dir,
416     unsigned long attrs)
417 {
418 
419 	dma_unmap_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
420 }
421 
422 
423 #endif	/* _LINUXKPI_LINUX_DMA_MAPPING_H_ */
424