dma-mapping.h revision 254025
1/*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28#ifndef	_LINUX_DMA_MAPPING_H_
29#define _LINUX_DMA_MAPPING_H_
30
31#include <linux/types.h>
32#include <linux/device.h>
33#include <linux/err.h>
34#include <linux/dma-attrs.h>
35#include <linux/scatterlist.h>
36#include <linux/mm.h>
37#include <linux/page.h>
38
39#include <sys/systm.h>
40#include <sys/malloc.h>
41
42#include <vm/vm.h>
43#include <vm/vm_page.h>
44#include <vm/pmap.h>
45
46#include <machine/bus.h>
47#include <machine/pmap.h>
48
49enum dma_data_direction {
50	DMA_BIDIRECTIONAL = 0,
51	DMA_TO_DEVICE = 1,
52	DMA_FROM_DEVICE = 2,
53	DMA_NONE = 3,
54};
55
56struct dma_map_ops {
57	void* (*alloc_coherent)(struct device *dev, size_t size,
58	    dma_addr_t *dma_handle, gfp_t gfp);
59	void (*free_coherent)(struct device *dev, size_t size,
60	    void *vaddr, dma_addr_t dma_handle);
61	dma_addr_t (*map_page)(struct device *dev, struct page *page,
62	    unsigned long offset, size_t size, enum dma_data_direction dir,
63	    struct dma_attrs *attrs);
64	void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
65	    size_t size, enum dma_data_direction dir, struct dma_attrs *attrs);
66	int (*map_sg)(struct device *dev, struct scatterlist *sg,
67	    int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
68	void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
69	    enum dma_data_direction dir, struct dma_attrs *attrs);
70	void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
71	    size_t size, enum dma_data_direction dir);
72	void (*sync_single_for_device)(struct device *dev,
73	    dma_addr_t dma_handle, size_t size, enum dma_data_direction dir);
74	void (*sync_single_range_for_cpu)(struct device *dev,
75	    dma_addr_t dma_handle, unsigned long offset, size_t size,
76	    enum dma_data_direction dir);
77	void (*sync_single_range_for_device)(struct device *dev,
78	    dma_addr_t dma_handle, unsigned long offset, size_t size,
79	    enum dma_data_direction dir);
80	void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
81	    int nents, enum dma_data_direction dir);
82	void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
83	    int nents, enum dma_data_direction dir);
84	int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
85	int (*dma_supported)(struct device *dev, u64 mask);
86	int is_phys;
87};
88
89#define	DMA_BIT_MASK(n)	(((n) == 64) ? ~0ULL : ((1ULL << (n)) - 1))
90
91static inline int
92dma_supported(struct device *dev, u64 mask)
93{
94
95	/* XXX busdma takes care of this elsewhere. */
96	return (1);
97}
98
99static inline int
100dma_set_mask(struct device *dev, u64 dma_mask)
101{
102
103	if (!dev->dma_mask || !dma_supported(dev, dma_mask))
104		return -EIO;
105
106	*dev->dma_mask = dma_mask;
107	return (0);
108}
109
110static inline int
111dma_set_coherent_mask(struct device *dev, u64 mask)
112{
113
114	if (!dma_supported(dev, mask))
115		return -EIO;
116	/* XXX Currently we don't support a seperate coherent mask. */
117	return 0;
118}
119
120static inline void *
121dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
122    gfp_t flag)
123{
124	vm_paddr_t high;
125	size_t align;
126	void *mem;
127
128	if (dev->dma_mask)
129		high = *dev->dma_mask;
130	else
131		high = BUS_SPACE_MAXADDR_32BIT;
132	align = PAGE_SIZE << get_order(size);
133	mem = (void *)kmem_alloc_contig(kmem_arena, size, flag, 0, high, align,
134	    0, VM_MEMATTR_DEFAULT);
135	if (mem)
136		*dma_handle = vtophys(mem);
137	else
138		*dma_handle = 0;
139	return (mem);
140}
141
142static inline void
143dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
144    dma_addr_t dma_handle)
145{
146
147	kmem_free(kmem_arena, (vm_offset_t)cpu_addr, size);
148}
149
150/* XXX This only works with no iommu. */
151static inline dma_addr_t
152dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
153    enum dma_data_direction dir, struct dma_attrs *attrs)
154{
155
156	return vtophys(ptr);
157}
158
159static inline void
160dma_unmap_single_attrs(struct device *dev, dma_addr_t addr, size_t size,
161    enum dma_data_direction dir, struct dma_attrs *attrs)
162{
163}
164
165static inline int
166dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
167    enum dma_data_direction dir, struct dma_attrs *attrs)
168{
169	struct scatterlist *sg;
170	int i;
171
172	for_each_sg(sgl, sg, nents, i)
173		sg_dma_address(sg) = sg_phys(sg);
174
175	return (nents);
176}
177
178static inline void
179dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
180    enum dma_data_direction dir, struct dma_attrs *attrs)
181{
182}
183
184static inline dma_addr_t
185dma_map_page(struct device *dev, struct page *page,
186    unsigned long offset, size_t size, enum dma_data_direction direction)
187{
188
189	return VM_PAGE_TO_PHYS(page) + offset;
190}
191
192static inline void
193dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
194    enum dma_data_direction direction)
195{
196}
197
198static inline void
199dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
200    enum dma_data_direction direction)
201{
202}
203
204static inline void
205dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
206    enum dma_data_direction dir)
207{
208	dma_sync_single_for_cpu(dev, addr, size, dir);
209}
210
211static inline void
212dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
213    size_t size, enum dma_data_direction direction)
214{
215}
216
217static inline void
218dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
219    enum dma_data_direction direction)
220{
221}
222
223static inline void
224dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
225    enum dma_data_direction direction)
226{
227}
228
229static inline void
230dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
231    unsigned long offset, size_t size, int direction)
232{
233}
234
235static inline void
236dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
237    unsigned long offset, size_t size, int direction)
238{
239}
240
241static inline int
242dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
243{
244
245	return (0);
246}
247
248#define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL)
249#define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL)
250#define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL)
251#define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL)
252
253#define	DEFINE_DMA_UNMAP_ADDR(name)		dma_addr_t name
254#define	DEFINE_DMA_UNMAP_LEN(name)		__u32 name
255#define	dma_unmap_addr(p, name)			((p)->name)
256#define	dma_unmap_addr_set(p, name, v)		(((p)->name) = (v))
257#define	dma_unmap_len(p, name)			((p)->name)
258#define	dma_unmap_len_set(p, name, v)		(((p)->name) = (v))
259
260extern int uma_align_cache;
261#define	dma_get_cache_alignment()	uma_align_cache
262
263#endif	/* _LINUX_DMA_MAPPING_H_ */
264