Initial import from qualcomm sources
[gearbox.git] / usr / uio-dma.h
blob366a859c6f30fcdec876ce219effcffa382dd0a3
1 /**
2 UIO DMA library.
3 Copyright (C) 2009 Qualcomm Inc. All rights reserved.
4 Written by Max Krasnyansky <maxk@qualcommm.com>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the QUALCOMM Incorporated nor the
17 names of any contributors may be used to endorse or promote products
18 derived from this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY QUALCOMM AND ANY OTHER CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL QUALCOMM
24 AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED
26 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /**
34 * @file uio-dma.h
35 * UIO-DMA - DMA support for UIO
38 #ifndef UIO_DMA_H
39 #define UIO_DMA_H
41 #include <stdint.h>
42 #include <sys/types.h>
43 #include <sys/user.h>
44 #include <sys/uio.h>
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
50 /**
51 * Macro for construction DMA masks
52 * @param n number of non-zero bits
54 #define UIO_DMA_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
56 /* Caching modes */
57 enum {
58 UIO_DMA_CACHE_DEFAULT = 0,
59 UIO_DMA_CACHE_DISABLE,
60 UIO_DMA_CACHE_WRITECOMBINE
63 /* DMA mapping direction */
64 enum {
65 UIO_DMA_BIDIRECTIONAL = 0,
66 UIO_DMA_TODEVICE,
67 UIO_DMA_FROMDEVICE
70 /**
71 * UIO DMA area.
73 struct uio_dma_area {
74 uint8_t *addr;
75 unsigned long mmap_offset;
76 unsigned long size;
77 unsigned long chunk_size;
78 unsigned int chunk_count;
81 /**
82 * UIO DMA mapping.
84 struct uio_dma_mapping {
85 uint8_t *addr;
86 unsigned long mmap_offset;
87 unsigned long size;
88 unsigned int chunk_count;
89 unsigned int chunk_shift;
90 uint32_t devid;
91 uint8_t direction;
92 uint64_t dmaddr[0];
95 /**
96 * Open and initialize UIO DMA file descriptor.
97 * @return file descriptor
99 int uio_dma_open();
102 * Close UIO DMA file descriptor.
103 * @param fd file descriptor
105 void uio_dma_close(int fd);
108 * Allocate new DMA area.
109 * @param fd UIO DMA file descriptor
110 * @param size of the area
111 * @param cache caching mode
112 * @param dma_mask mask of the DMA address range
113 * @param memnode memory node to allocate the memory area on
115 struct uio_dma_area *uio_dma_alloc(int fd, unsigned int size,
116 unsigned int cache, uint64_t dma_mask, unsigned int memnode);
118 * Free DMA area.
119 * @param fd UIO DMA file descriptor
120 * @param pointer to @see uio_dma_area
122 int uio_dma_free(int fd, struct uio_dma_area *a);
125 * Map DMA area to a device.
126 * @param fd UIO DMA file descriptor
127 * @param pointer to @see uio_dma_area
128 * @param devid uio dma device id
129 * @param dir direction
130 * @return pointer to new @see uio_dma_mapping
132 struct uio_dma_mapping * uio_dma_map(int fd, struct uio_dma_area *area, uint32_t devid, unsigned int dir);
135 * Unmap DMA area from a device.
136 * @param fd UIO DMA file descriptor
137 * @param pointer to @see uio_dma_mapping
138 * @return 0 on success, <0 otherwise (sets errno)
140 int uio_dma_unmap(int fd, struct uio_dma_mapping *m);
143 * Map user-space region to the DMA address. Region must belong to the specified
144 * dma mapping. Use this function only when size of the memory region is guaranteed
145 * to be smaller than the chunk_size.
146 * @param m pointer to @see uio_dma_mapping
147 * @param addr pointer to the user-space memory region
148 * @param len length of the memory region
149 * @return dma_addr or 0 if address could not be mapped
151 static inline uint64_t uio_dma_addr(struct uio_dma_mapping *m, uint8_t *addr, unsigned int len)
153 unsigned long chunk, offset, chunk_size;
155 chunk_size = (1 << m->chunk_shift);
156 if (len > chunk_size || addr < m->addr || (addr + len) > (m->addr + m->size))
157 return 0;
159 offset = addr - m->addr;
160 chunk = offset >> m->chunk_shift;
161 offset = offset & (chunk_size - 1);
163 return m->dmaddr[chunk] + offset;
166 #ifdef __cplusplus
168 #endif
170 #endif /* UIO_DMA_H */