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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
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.
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.
31 #ifndef _LINUX_DMA_MAPPING_H_
32 #define _LINUX_DMA_MAPPING_H_
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>
40 #include <linux/page.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
46 #include <vm/vm_page.h>
49 #include <machine/bus.h>
51 enum dma_data_direction {
52 DMA_BIDIRECTIONAL = 0,
59 void* (*alloc_coherent)(struct device *dev, size_t size,
60 dma_addr_t *dma_handle, gfp_t gfp);
61 void (*free_coherent)(struct device *dev, size_t size,
62 void *vaddr, dma_addr_t dma_handle);
63 dma_addr_t (*map_page)(struct device *dev, struct page *page,
64 unsigned long offset, size_t size, enum dma_data_direction dir,
65 struct dma_attrs *attrs);
66 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
67 size_t size, enum dma_data_direction dir, struct dma_attrs *attrs);
68 int (*map_sg)(struct device *dev, struct scatterlist *sg,
69 int nents, enum dma_data_direction dir, struct dma_attrs *attrs);
70 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
71 enum dma_data_direction dir, struct dma_attrs *attrs);
72 void (*sync_single_for_cpu)(struct device *dev, dma_addr_t dma_handle,
73 size_t size, enum dma_data_direction dir);
74 void (*sync_single_for_device)(struct device *dev,
75 dma_addr_t dma_handle, size_t size, enum dma_data_direction dir);
76 void (*sync_single_range_for_cpu)(struct device *dev,
77 dma_addr_t dma_handle, unsigned long offset, size_t size,
78 enum dma_data_direction dir);
79 void (*sync_single_range_for_device)(struct device *dev,
80 dma_addr_t dma_handle, unsigned long offset, size_t size,
81 enum dma_data_direction dir);
82 void (*sync_sg_for_cpu)(struct device *dev, struct scatterlist *sg,
83 int nents, enum dma_data_direction dir);
84 void (*sync_sg_for_device)(struct device *dev, struct scatterlist *sg,
85 int nents, enum dma_data_direction dir);
86 int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
87 int (*dma_supported)(struct device *dev, u64 mask);
91 #define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL)
94 dma_supported(struct device *dev, u64 mask)
97 /* XXX busdma takes care of this elsewhere. */
102 dma_set_mask(struct device *dev, u64 dma_mask)
105 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
108 *dev->dma_mask = dma_mask;
113 dma_set_coherent_mask(struct device *dev, u64 mask)
116 if (!dma_supported(dev, mask))
118 /* XXX Currently we don't support a separate coherent mask. */
123 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
131 high = *dev->dma_mask;
132 else if (flag & GFP_DMA32)
133 high = BUS_SPACE_MAXADDR_32BIT;
135 high = BUS_SPACE_MAXADDR;
136 align = PAGE_SIZE << get_order(size);
137 mem = (void *)kmem_alloc_contig(kmem_arena, size, flag, 0, high, align,
138 0, VM_MEMATTR_DEFAULT);
140 *dma_handle = vtophys(mem);
147 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
151 return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
155 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
156 dma_addr_t dma_handle)
159 kmem_free(kmem_arena, (vm_offset_t)cpu_addr, size);
162 /* XXX This only works with no iommu. */
163 static inline dma_addr_t
164 dma_map_single_attrs(struct device *dev, void *ptr, size_t size,
165 enum dma_data_direction dir, struct dma_attrs *attrs)
172 dma_unmap_single_attrs(struct device *dev, dma_addr_t addr, size_t size,
173 enum dma_data_direction dir, struct dma_attrs *attrs)
178 dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl, int nents,
179 enum dma_data_direction dir, struct dma_attrs *attrs)
181 struct scatterlist *sg;
184 for_each_sg(sgl, sg, nents, i)
185 sg_dma_address(sg) = sg_phys(sg);
191 dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
192 enum dma_data_direction dir, struct dma_attrs *attrs)
196 static inline dma_addr_t
197 dma_map_page(struct device *dev, struct page *page,
198 unsigned long offset, size_t size, enum dma_data_direction direction)
201 return VM_PAGE_TO_PHYS(page) + offset;
205 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
206 enum dma_data_direction direction)
211 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
212 enum dma_data_direction direction)
217 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
218 enum dma_data_direction dir)
220 dma_sync_single_for_cpu(dev, addr, size, dir);
224 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
225 size_t size, enum dma_data_direction direction)
230 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
231 enum dma_data_direction direction)
236 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
237 enum dma_data_direction direction)
242 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
243 unsigned long offset, size_t size, int direction)
248 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
249 unsigned long offset, size_t size, int direction)
254 dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
260 static inline unsigned int dma_set_max_seg_size(struct device *dev,
267 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL)
268 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL)
269 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL)
270 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL)
272 #define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name
273 #define DEFINE_DMA_UNMAP_LEN(name) __u32 name
274 #define dma_unmap_addr(p, name) ((p)->name)
275 #define dma_unmap_addr_set(p, name, v) (((p)->name) = (v))
276 #define dma_unmap_len(p, name) ((p)->name)
277 #define dma_unmap_len_set(p, name, v) (((p)->name) = (v))
279 extern int uma_align_cache;
280 #define dma_get_cache_alignment() uma_align_cache
282 #endif /* _LINUX_DMA_MAPPING_H_ */