xref: /freebsd/sys/compat/linuxkpi/common/include/linux/dma-mapping.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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	_LINUX_DMA_MAPPING_H_
32 #define _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 	    struct dma_attrs *attrs);
67 	void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
68 	    size_t size, enum dma_data_direction dir, struct dma_attrs *attrs);
69 	int (*map_sg)(struct device *dev, struct scatterlist *sg,
70 	    int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
71 	void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
72 	    enum dma_data_direction dir, struct dma_attrs *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 *dev, u64 mask);
95 void *linux_dma_alloc_coherent(struct device *dev, size_t size,
96     dma_addr_t *dma_handle, gfp_t flag);
97 dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len);
98 void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size);
99 int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
100     int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
101 void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
102     int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
103 
104 static inline int
105 dma_supported(struct device *dev, u64 mask)
106 {
107 
108 	/* XXX busdma takes care of this elsewhere. */
109 	return (1);
110 }
111 
112 static inline int
113 dma_set_mask(struct device *dev, u64 dma_mask)
114 {
115 
116 	if (!dev->dma_priv || !dma_supported(dev, dma_mask))
117 		return -EIO;
118 
119 	return (linux_dma_tag_init(dev, dma_mask));
120 }
121 
122 static inline int
123 dma_set_coherent_mask(struct device *dev, u64 mask)
124 {
125 
126 	if (!dma_supported(dev, mask))
127 		return -EIO;
128 	/* XXX Currently we don't support a separate coherent mask. */
129 	return 0;
130 }
131 
132 static inline int
133 dma_set_mask_and_coherent(struct device *dev, u64 mask)
134 {
135 	int r;
136 
137 	r = dma_set_mask(dev, mask);
138 	if (r == 0)
139 		dma_set_coherent_mask(dev, mask);
140 	return (r);
141 }
142 
143 static inline void *
144 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
145     gfp_t flag)
146 {
147 	return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));
148 }
149 
150 static inline void *
151 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
152     gfp_t flag)
153 {
154 
155 	return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
156 }
157 
158 static inline void
159 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
160     dma_addr_t dma_addr)
161 {
162 
163 	linux_dma_unmap(dev, dma_addr, size);
164 	kmem_free((vm_offset_t)cpu_addr, size);
165 }
166 
167 static inline dma_addr_t
168 dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
169     enum dma_data_direction dir, struct dma_attrs *attrs)
170 {
171 
172 	return (linux_dma_map_phys(dev, vtophys(ptr), size));
173 }
174 
175 static inline void
176 dma_unmap_single_attrs(struct device *dev, dma_addr_t dma_addr, size_t size,
177     enum dma_data_direction dir, struct dma_attrs *attrs)
178 {
179 
180 	linux_dma_unmap(dev, dma_addr, size);
181 }
182 
183 static inline dma_addr_t
184 dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,
185     size_t size, enum dma_data_direction dir, unsigned long attrs)
186 {
187 
188 	return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size));
189 }
190 
191 static inline int
192 dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
193     enum dma_data_direction dir, struct dma_attrs *attrs)
194 {
195 
196 	return (linux_dma_map_sg_attrs(dev, sgl, nents, dir, attrs));
197 }
198 
199 static inline void
200 dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
201     enum dma_data_direction dir, struct dma_attrs *attrs)
202 {
203 
204 	linux_dma_unmap_sg_attrs(dev, sg, nents, dir, attrs);
205 }
206 
207 static inline dma_addr_t
208 dma_map_page(struct device *dev, struct page *page,
209     unsigned long offset, size_t size, enum dma_data_direction direction)
210 {
211 
212 	return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size));
213 }
214 
215 static inline void
216 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
217     enum dma_data_direction direction)
218 {
219 
220 	linux_dma_unmap(dev, dma_address, size);
221 }
222 
223 static inline void
224 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
225     enum dma_data_direction direction)
226 {
227 }
228 
229 static inline void
230 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
231     enum dma_data_direction dir)
232 {
233 	dma_sync_single_for_cpu(dev, addr, size, dir);
234 }
235 
236 static inline void
237 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
238     size_t size, enum dma_data_direction direction)
239 {
240 }
241 
242 static inline void
243 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
244     enum dma_data_direction direction)
245 {
246 }
247 
248 static inline void
249 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
250     enum dma_data_direction direction)
251 {
252 }
253 
254 static inline void
255 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
256     unsigned long offset, size_t size, int direction)
257 {
258 }
259 
260 static inline void
261 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
262     unsigned long offset, size_t size, int direction)
263 {
264 }
265 
266 static inline int
267 dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
268 {
269 
270 	return (dma_addr == 0);
271 }
272 
273 static inline unsigned int dma_set_max_seg_size(struct device *dev,
274     unsigned int size)
275 {
276 	return (0);
277 }
278 
279 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL)
280 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL)
281 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL)
282 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL)
283 
284 #define	DEFINE_DMA_UNMAP_ADDR(name)		dma_addr_t name
285 #define	DEFINE_DMA_UNMAP_LEN(name)		__u32 name
286 #define	dma_unmap_addr(p, name)			((p)->name)
287 #define	dma_unmap_addr_set(p, name, v)		(((p)->name) = (v))
288 #define	dma_unmap_len(p, name)			((p)->name)
289 #define	dma_unmap_len_set(p, name, v)		(((p)->name) = (v))
290 
291 extern int uma_align_cache;
292 #define	dma_get_cache_alignment()	uma_align_cache
293 
294 #endif	/* _LINUX_DMA_MAPPING_H_ */
295