dma-mapping.h revision 271127
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	_LINUX_DMA_MAPPING_H_
30#define _LINUX_DMA_MAPPING_H_
31
32#include <linux/types.h>
33#include <linux/device.h>
34#include <linux/err.h>
35#include <linux/dma-attrs.h>
36#include <linux/scatterlist.h>
37#include <linux/mm.h>
38#include <linux/page.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/pmap.h>
46
47#include <machine/bus.h>
48#include <machine/pmap.h>
49
50enum dma_data_direction {
51	DMA_BIDIRECTIONAL = 0,
52	DMA_TO_DEVICE = 1,
53	DMA_FROM_DEVICE = 2,
54	DMA_NONE = 3,
55};
56
57struct dma_map_ops {
58	void* (*alloc_coherent)(struct device *dev, size_t size,
59	    dma_addr_t *dma_handle, gfp_t gfp);
60	void (*free_coherent)(struct device *dev, size_t size,
61	    void *vaddr, dma_addr_t dma_handle);
62	dma_addr_t (*map_page)(struct device *dev, struct page *page,
63	    unsigned long offset, size_t size, enum dma_data_direction dir,
64	    struct dma_attrs *attrs);
65	void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
66	    size_t size, enum dma_data_direction dir, struct dma_attrs *attrs);
67	int (*map_sg)(struct device *dev, struct scatterlist *sg,
68	    int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
69	void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
70	    enum dma_data_direction dir, struct dma_attrs *attrs);
71	void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
72	    size_t size, enum dma_data_direction dir);
73	void (*sync_single_for_device)(struct device *dev,
74	    dma_addr_t dma_handle, size_t size, enum dma_data_direction dir);
75	void (*sync_single_range_for_cpu)(struct device *dev,
76	    dma_addr_t dma_handle, unsigned long offset, size_t size,
77	    enum dma_data_direction dir);
78	void (*sync_single_range_for_device)(struct device *dev,
79	    dma_addr_t dma_handle, unsigned long offset, size_t size,
80	    enum dma_data_direction dir);
81	void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
82	    int nents, enum dma_data_direction dir);
83	void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
84	    int nents, enum dma_data_direction dir);
85	int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
86	int (*dma_supported)(struct device *dev, u64 mask);
87	int is_phys;
88};
89
90#define	DMA_BIT_MASK(n)	(((n) == 64) ? ~0ULL : ((1ULL << (n)) - 1))
91
92static inline int
93dma_supported(struct device *dev, u64 mask)
94{
95
96	/* XXX busdma takes care of this elsewhere. */
97	return (1);
98}
99
100static inline int
101dma_set_mask(struct device *dev, u64 dma_mask)
102{
103
104	if (!dev->dma_mask || !dma_supported(dev, dma_mask))
105		return -EIO;
106
107	*dev->dma_mask = dma_mask;
108	return (0);
109}
110
111static inline int
112dma_set_coherent_mask(struct device *dev, u64 mask)
113{
114
115	if (!dma_supported(dev, mask))
116		return -EIO;
117	/* XXX Currently we don't support a seperate coherent mask. */
118	return 0;
119}
120
121static inline void *
122dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
123    gfp_t flag)
124{
125	vm_paddr_t high;
126	size_t align;
127	void *mem;
128
129	if (dev->dma_mask)
130		high = *dev->dma_mask;
131	else
132		high = BUS_SPACE_MAXADDR_32BIT;
133	align = PAGE_SIZE << get_order(size);
134	mem = (void *)kmem_alloc_contig(kmem_arena, size, flag, 0, high, align,
135	    0, VM_MEMATTR_DEFAULT);
136	if (mem)
137		*dma_handle = vtophys(mem);
138	else
139		*dma_handle = 0;
140	return (mem);
141}
142
143static inline void
144dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
145    dma_addr_t dma_handle)
146{
147
148	kmem_free(kmem_arena, (vm_offset_t)cpu_addr, size);
149}
150
151/* XXX This only works with no iommu. */
152static inline dma_addr_t
153dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
154    enum dma_data_direction dir, struct dma_attrs *attrs)
155{
156
157	return vtophys(ptr);
158}
159
160static inline void
161dma_unmap_single_attrs(struct device *dev, dma_addr_t addr, size_t size,
162    enum dma_data_direction dir, struct dma_attrs *attrs)
163{
164}
165
166static inline int
167dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
168    enum dma_data_direction dir, struct dma_attrs *attrs)
169{
170	struct scatterlist *sg;
171	int i;
172
173	for_each_sg(sgl, sg, nents, i)
174		sg_dma_address(sg) = sg_phys(sg);
175
176	return (nents);
177}
178
179static inline void
180dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
181    enum dma_data_direction dir, struct dma_attrs *attrs)
182{
183}
184
185static inline dma_addr_t
186dma_map_page(struct device *dev, struct page *page,
187    unsigned long offset, size_t size, enum dma_data_direction direction)
188{
189
190	return VM_PAGE_TO_PHYS(page) + offset;
191}
192
193static inline void
194dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
195    enum dma_data_direction direction)
196{
197}
198
199static inline void
200dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
201    enum dma_data_direction direction)
202{
203}
204
205static inline void
206dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
207    enum dma_data_direction dir)
208{
209	dma_sync_single_for_cpu(dev, addr, size, dir);
210}
211
212static inline void
213dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
214    size_t size, enum dma_data_direction direction)
215{
216}
217
218static inline void
219dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
220    enum dma_data_direction direction)
221{
222}
223
224static inline void
225dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
226    enum dma_data_direction direction)
227{
228}
229
230static inline void
231dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
232    unsigned long offset, size_t size, int direction)
233{
234}
235
236static inline void
237dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
238    unsigned long offset, size_t size, int direction)
239{
240}
241
242static inline int
243dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
244{
245
246	return (0);
247}
248
249static inline unsigned int dma_set_max_seg_size(struct device *dev,
250                                                 unsigned int size)
251{
252        return (0);
253}
254
255
256#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL)
257#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL)
258#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL)
259#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL)
260
261#define	DEFINE_DMA_UNMAP_ADDR(name)		dma_addr_t name
262#define	DEFINE_DMA_UNMAP_LEN(name)		__u32 name
263#define	dma_unmap_addr(p, name)			((p)->name)
264#define	dma_unmap_addr_set(p, name, v)		(((p)->name) = (v))
265#define	dma_unmap_len(p, name)			((p)->name)
266#define	dma_unmap_len_set(p, name, v)		(((p)->name) = (v))
267
268extern int uma_align_cache;
269#define	dma_get_cache_alignment()	uma_align_cache
270
271#endif	/* _LINUX_DMA_MAPPING_H_ */
272