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 _LINUXKPI_LINUX_DMA_MAPPING_H_
32 #define _LINUXKPI_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>
41 #include <linux/sizes.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
47 #include <vm/vm_page.h>
50 #include <machine/bus.h>
52 enum dma_data_direction {
53 DMA_BIDIRECTIONAL = 0,
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,
67 void (*unmap_page)(struct device *dev, dma_addr_t dma_handle,
68 size_t size, enum dma_data_direction dir, unsigned long attrs);
69 int (*map_sg)(struct device *dev, struct scatterlist *sg,
70 int nents, enum dma_data_direction dir, unsigned long attrs);
71 void (*unmap_sg)(struct device *dev, struct scatterlist *sg, int nents,
72 enum dma_data_direction dir, unsigned long 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);
92 #define DMA_BIT_MASK(n) ((2ULL << ((n) - 1)) - 1ULL)
94 int linux_dma_tag_init(struct device *, u64);
95 int linux_dma_tag_init_coherent(struct device *, u64);
96 void *linux_dma_alloc_coherent(struct device *dev, size_t size,
97 dma_addr_t *dma_handle, gfp_t flag);
98 dma_addr_t linux_dma_map_phys(struct device *dev, vm_paddr_t phys, size_t len);
99 void linux_dma_unmap(struct device *dev, dma_addr_t dma_addr, size_t size);
100 int linux_dma_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
101 int nents, enum dma_data_direction dir __unused,
102 unsigned long attrs __unused);
103 void linux_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
104 int nents __unused, enum dma_data_direction dir __unused,
105 unsigned long attrs __unused);
108 dma_supported(struct device *dev, u64 dma_mask)
111 /* XXX busdma takes care of this elsewhere. */
116 dma_set_mask(struct device *dev, u64 dma_mask)
119 if (!dev->dma_priv || !dma_supported(dev, dma_mask))
122 return (linux_dma_tag_init(dev, dma_mask));
126 dma_set_coherent_mask(struct device *dev, u64 dma_mask)
129 if (!dev->dma_priv || !dma_supported(dev, dma_mask))
132 return (linux_dma_tag_init_coherent(dev, dma_mask));
136 dma_set_mask_and_coherent(struct device *dev, u64 dma_mask)
140 r = dma_set_mask(dev, dma_mask);
142 dma_set_coherent_mask(dev, dma_mask);
147 dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
150 return (linux_dma_alloc_coherent(dev, size, dma_handle, flag));
154 dma_zalloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
158 return (dma_alloc_coherent(dev, size, dma_handle, flag | __GFP_ZERO));
162 dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
166 linux_dma_unmap(dev, dma_addr, size);
167 kmem_free((vm_offset_t)cpu_addr, size);
170 #define dma_map_single_attrs(dev, ptr, size, dir, attrs) \
171 linux_dma_map_phys(dev, vtophys(ptr), size)
173 #define dma_unmap_single_attrs(dev, dma_addr, size, dir, attrs) \
174 linux_dma_unmap(dev, dma_addr, size)
176 static inline dma_addr_t
177 dma_map_page_attrs(struct device *dev, struct page *page, size_t offset,
178 size_t size, enum dma_data_direction dir, unsigned long attrs)
181 return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size));
184 /* linux_dma_(un)map_sg_attrs does not support attrs yet */
185 #define dma_map_sg_attrs(dev, sgl, nents, dir, attrs) \
186 linux_dma_map_sg_attrs(dev, sgl, nents, dir, 0)
188 #define dma_unmap_sg_attrs(dev, sg, nents, dir, attrs) \
189 linux_dma_unmap_sg_attrs(dev, sg, nents, dir, 0)
191 static inline dma_addr_t
192 dma_map_page(struct device *dev, struct page *page,
193 unsigned long offset, size_t size, enum dma_data_direction direction)
196 return (linux_dma_map_phys(dev, VM_PAGE_TO_PHYS(page) + offset, size));
200 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
201 enum dma_data_direction direction)
204 linux_dma_unmap(dev, dma_address, size);
208 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
209 enum dma_data_direction direction)
214 dma_sync_single(struct device *dev, dma_addr_t addr, size_t size,
215 enum dma_data_direction dir)
217 dma_sync_single_for_cpu(dev, addr, size, dir);
221 dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
222 size_t size, enum dma_data_direction direction)
227 dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
228 enum dma_data_direction direction)
233 dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
234 enum dma_data_direction direction)
239 dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
240 unsigned long offset, size_t size, int direction)
245 dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
246 unsigned long offset, size_t size, int direction)
251 dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
254 return (dma_addr == 0);
257 static inline unsigned int dma_set_max_seg_size(struct device *dev,
263 #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, 0)
264 #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, 0)
265 #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, 0)
266 #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, 0)
268 #define DEFINE_DMA_UNMAP_ADDR(name) dma_addr_t name
269 #define DEFINE_DMA_UNMAP_LEN(name) __u32 name
270 #define dma_unmap_addr(p, name) ((p)->name)
271 #define dma_unmap_addr_set(p, name, v) (((p)->name) = (v))
272 #define dma_unmap_len(p, name) ((p)->name)
273 #define dma_unmap_len_set(p, name, v) (((p)->name) = (v))
275 extern int uma_align_cache;
276 #define dma_get_cache_alignment() uma_align_cache
278 #endif /* _LINUXKPI_LINUX_DMA_MAPPING_H_ */