1 /*
2 * include/linux/ion.h
3 *
4 * Copyright (C) 2011 Google, Inc.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
17 #ifndef _LINUX_ION_H
18 #define _LINUX_ION_H
20 #include <linux/types.h>
22 struct ion_handle;
23 /**
24 * enum ion_heap_types - list of all possible types of heaps
25 * @ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc
26 * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc
27 * @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved
28 * carveout heap, allocations are physically
29 * contiguous
30 * @ION_NUM_HEAPS: helper for iterating over heaps, a bit mask
31 * is used to identify the heaps, so only 32
32 * total heap types are supported
33 */
34 enum ion_heap_type {
35 ION_HEAP_TYPE_SYSTEM,
36 ION_HEAP_TYPE_SYSTEM_CONTIG,
37 ION_HEAP_TYPE_CARVEOUT,
38 ION_HEAP_TYPE_CHUNK,
39 ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
40 are at the end of this enum */
41 ION_NUM_HEAPS = 16,
42 };
44 /**
45 * enum ion_data_direction - sync operation arguments
46 * @ION_BIDIRECTIONAL: memory written to & read from device
47 * @ION_TO_DEVICE: memory going to be transferred to device
48 * @ION_FROM_DEVICE: memory populated by device
49 * @ION_NONE: None of the above
50 */
51 enum ion_data_direction {
52 ION_BIDIRECTIONAL = 0,
53 ION_TO_DEVICE = 1,
54 ION_FROM_DEVICE = 2,
55 ION_NONE = 3,
56 };
59 #define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
60 #define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
61 #define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
63 #define ION_NUM_HEAP_IDS sizeof(unsigned int) * 8
65 /**
66 * heap flags - the lower 16 bits are used by core ion, the upper 16
67 * bits are reserved for use by the heaps themselves.
68 */
69 #define ION_FLAG_CACHED 1 /* mappings of this buffer should be
70 cached, ion will do cache
71 maintenance when the buffer is
72 mapped for dma */
73 #define ION_FLAG_CACHED_NEEDS_SYNC 2 /* mappings of this buffer will created
74 at mmap time, if this is set
75 caches must be managed manually */
77 #ifdef __KERNEL__
78 struct ion_device;
79 struct ion_heap;
80 struct ion_mapper;
81 struct ion_client;
82 struct ion_buffer;
84 /* This should be removed some day when phys_addr_t's are fully
85 plumbed in the kernel, and all instances of ion_phys_addr_t should
86 be converted to phys_addr_t. For the time being many kernel interfaces
87 do not accept phys_addr_t's that would have to */
88 #define ion_phys_addr_t unsigned long
90 /**
91 * struct ion_platform_heap - defines a heap in the given platform
92 * @type: type of the heap from ion_heap_type enum
93 * @id: unique identifier for heap. When allocating higher numbers
94 * will be allocated from first. At allocation these are passed
95 * as a bit mask and therefore can not exceed ION_NUM_HEAP_IDS.
96 * @name: used for debug purposes
97 * @base: base address of heap in physical memory if applicable
98 * @size: size of the heap in bytes if applicable
99 * @align: required alignment in physical memory if applicable
100 * @priv: private info passed from the board file
101 *
102 * Provided by the board file.
103 */
104 struct ion_platform_heap {
105 enum ion_heap_type type;
106 unsigned int id;
107 const char *name;
108 ion_phys_addr_t base;
109 size_t size;
110 ion_phys_addr_t align;
111 void *priv;
112 };
114 /**
115 * struct ion_platform_data - array of platform heaps passed from board file
116 * @nr: number of structures in the array
117 * @heaps: array of platform_heap structions
118 *
119 * Provided by the board file in the form of platform data to a platform device.
120 */
121 struct ion_platform_data {
122 int nr;
123 struct ion_platform_heap heaps[];
124 };
126 /**
127 * ion_reserve() - reserve memory for ion heaps if applicable
128 * @data: platform data specifying starting physical address and
129 * size
130 *
131 * Calls memblock reserve to set aside memory for heaps that are
132 * located at specific memory addresses or of specfic sizes not
133 * managed by the kernel
134 */
135 void ion_reserve(struct ion_platform_data *data);
137 /**
138 * ion_client_create() - allocate a client and returns it
139 * @dev: the global ion device
140 * @heap_type_mask: mask of heaps this client can allocate from
141 * @name: used for debugging
142 */
143 struct ion_client *ion_client_create(struct ion_device *dev,
144 const char *name);
146 /**
147 * ion_client_destroy() - free's a client and all it's handles
148 * @client: the client
149 *
150 * Free the provided client and all it's resources including
151 * any handles it is holding.
152 */
153 void ion_client_destroy(struct ion_client *client);
155 /**
156 * ion_alloc - allocate ion memory
157 * @client: the client
158 * @len: size of the allocation
159 * @align: requested allocation alignment, lots of hardware blocks
160 * have alignment requirements of some kind
161 * @heap_id_mask: mask of heaps to allocate from, if multiple bits are set
162 * heaps will be tried in order from highest to lowest
163 * id
164 * @flags: heap flags, the low 16 bits are consumed by ion, the
165 * high 16 bits are passed on to the respective heap and
166 * can be heap custom
167 *
168 * Allocate memory in one of the heaps provided in heap mask and return
169 * an opaque handle to it.
170 */
171 struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
172 size_t align, unsigned int heap_id_mask,
173 unsigned int flags);
175 /**
176 * ion_free - free a handle
177 * @client: the client
178 * @handle: the handle to free
179 *
180 * Free the provided handle.
181 */
182 void ion_free(struct ion_client *client, struct ion_handle *handle);
184 /**
185 * ion_phys - returns the physical address and len of a handle
186 * @client: the client
187 * @handle: the handle
188 * @addr: a pointer to put the address in
189 * @len: a pointer to put the length in
190 *
191 * This function queries the heap for a particular handle to get the
192 * handle's physical address. It't output is only correct if
193 * a heap returns physically contiguous memory -- in other cases
194 * this api should not be implemented -- ion_sg_table should be used
195 * instead. Returns -EINVAL if the handle is invalid. This has
196 * no implications on the reference counting of the handle --
197 * the returned value may not be valid if the caller is not
198 * holding a reference.
199 */
200 int ion_phys(struct ion_client *client, struct ion_handle *handle,
201 ion_phys_addr_t *addr, size_t *len);
203 /**
204 * ion_map_dma - return an sg_table describing a handle
205 * @client: the client
206 * @handle: the handle
207 *
208 * This function returns the sg_table describing
209 * a particular ion handle.
210 */
211 struct sg_table *ion_sg_table(struct ion_client *client,
212 struct ion_handle *handle);
214 /**
215 * ion_map_kernel - create mapping for the given handle
216 * @client: the client
217 * @handle: handle to map
218 *
219 * Map the given handle into the kernel and return a kernel address that
220 * can be used to access this address.
221 */
222 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
224 /**
225 * ion_unmap_kernel() - destroy a kernel mapping for a handle
226 * @client: the client
227 * @handle: handle to unmap
228 */
229 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
231 /**
232 * ion_share_dma_buf() - given an ion client, create a dma-buf fd
233 * @client: the client
234 * @handle: the handle
235 */
236 int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle);
238 /**
239 * ion_import_dma_buf() - given an dma-buf fd from the ion exporter get handle
240 * @client: the client
241 * @fd: the dma-buf fd
242 *
243 * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf,
244 * import that fd and return a handle representing it. If a dma-buf from
245 * another exporter is passed in this function will return ERR_PTR(-EINVAL)
246 */
247 struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd);
249 #endif /* __KERNEL__ */
251 /**
252 * DOC: Ion Userspace API
253 *
254 * create a client by opening /dev/ion
255 * most operations handled via following ioctls
256 *
257 */
259 /**
260 * struct ion_allocation_data - metadata passed from userspace for allocations
261 * @len: size of the allocation
262 * @align: required alignment of the allocation
263 * @heap_id_mask: mask of heap ids to allocate from
264 * @flags: flags passed to heap
265 * @handle: pointer that will be populated with a cookie to use to
266 * refer to this allocation
267 *
268 * Provided by userspace as an argument to the ioctl
269 */
270 struct ion_allocation_data {
271 size_t len;
272 size_t align;
273 unsigned int heap_id_mask;
274 unsigned int flags;
275 struct ion_handle *handle;
276 };
278 /**
279 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
280 * @handle: a handle
281 * @fd: a file descriptor representing that handle
282 *
283 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
284 * the handle returned from ion alloc, and the kernel returns the file
285 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
286 * provides the file descriptor and the kernel returns the handle.
287 */
288 struct ion_fd_data {
289 struct ion_handle *handle;
290 int fd;
291 enum ion_data_direction dir;
292 };
294 /**
295 * struct ion_handle_data - a handle passed to/from the kernel
296 * @handle: a handle
297 */
298 struct ion_handle_data {
299 struct ion_handle *handle;
300 };
302 /**
303 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
304 * @cmd: the custom ioctl function to call
305 * @arg: additional data to pass to the custom ioctl, typically a user
306 * pointer to a predefined structure
307 *
308 * This works just like the regular cmd and arg fields of an ioctl.
309 */
310 struct ion_custom_data {
311 unsigned int cmd;
312 unsigned long arg;
313 };
315 #define ION_IOC_MAGIC 'I'
317 /**
318 * DOC: ION_IOC_ALLOC - allocate memory
319 *
320 * Takes an ion_allocation_data struct and returns it with the handle field
321 * populated with the opaque handle for the allocation.
322 */
323 #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
324 struct ion_allocation_data)
326 /**
327 * DOC: ION_IOC_FREE - free memory
328 *
329 * Takes an ion_handle_data struct and frees the handle.
330 */
331 #define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
333 /**
334 * DOC: ION_IOC_MAP - get a file descriptor to mmap
335 *
336 * Takes an ion_fd_data struct with the handle field populated with a valid
337 * opaque handle. Returns the struct with the fd field set to a file
338 * descriptor open in the current address space. This file descriptor
339 * can then be used as an argument to mmap.
340 */
341 #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
343 /**
344 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
345 *
346 * Takes an ion_fd_data struct with the handle field populated with a valid
347 * opaque handle. Returns the struct with the fd field set to a file
348 * descriptor open in the current address space. This file descriptor
349 * can then be passed to another process. The corresponding opaque handle can
350 * be retrieved via ION_IOC_IMPORT.
351 */
352 #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
354 /**
355 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
356 *
357 * Takes an ion_fd_data struct with the fd field populated with a valid file
358 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
359 * filed set to the corresponding opaque handle.
360 */
361 #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
363 /**
364 * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory
365 *
366 * Deprecated in favor of using the dma_buf api's correctly (syncing
367 * will happend automatically when the buffer is mapped to a device).
368 * If necessary should be used after touching a cached buffer from the cpu,
369 * this will make the buffer in memory coherent.
370 */
371 #define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
373 /**
374 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
375 *
376 * Takes the argument of the architecture specific ioctl to call and
377 * passes appropriate userdata for that ioctl
378 */
379 #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
381 #endif /* _LINUX_ION_H */