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 #define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
45 #define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
46 #define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
48 #define ION_NUM_HEAP_IDS sizeof(unsigned int) * 8
50 /**
51 * heap flags - the lower 16 bits are used by core ion, the upper 16
52 * bits are reserved for use by the heaps themselves.
53 */
54 #define ION_FLAG_CACHED 1 /* mappings of this buffer should be
55 cached, ion will do cache
56 maintenance when the buffer is
57 mapped for dma */
58 #define ION_FLAG_CACHED_NEEDS_SYNC 2 /* mappings of this buffer will created
59 at mmap time, if this is set
60 caches must be managed manually */
62 #ifdef __KERNEL__
63 struct ion_device;
64 struct ion_heap;
65 struct ion_mapper;
66 struct ion_client;
67 struct ion_buffer;
69 /* This should be removed some day when phys_addr_t's are fully
70 plumbed in the kernel, and all instances of ion_phys_addr_t should
71 be converted to phys_addr_t. For the time being many kernel interfaces
72 do not accept phys_addr_t's that would have to */
73 #define ion_phys_addr_t unsigned long
75 /**
76 * struct ion_platform_heap - defines a heap in the given platform
77 * @type: type of the heap from ion_heap_type enum
78 * @id: unique identifier for heap. When allocating higher numbers
79 * will be allocated from first. At allocation these are passed
80 * as a bit mask and therefore can not exceed ION_NUM_HEAP_IDS.
81 * @name: used for debug purposes
82 * @base: base address of heap in physical memory if applicable
83 * @size: size of the heap in bytes if applicable
84 * @align: required alignment in physical memory if applicable
85 * @priv: private info passed from the board file
86 *
87 * Provided by the board file.
88 */
89 struct ion_platform_heap {
90 enum ion_heap_type type;
91 unsigned int id;
92 const char *name;
93 ion_phys_addr_t base;
94 size_t size;
95 ion_phys_addr_t align;
96 void *priv;
97 };
99 /**
100 * struct ion_platform_data - array of platform heaps passed from board file
101 * @nr: number of structures in the array
102 * @heaps: array of platform_heap structions
103 *
104 * Provided by the board file in the form of platform data to a platform device.
105 */
106 struct ion_platform_data {
107 int nr;
108 struct ion_platform_heap heaps[];
109 };
111 /**
112 * ion_reserve() - reserve memory for ion heaps if applicable
113 * @data: platform data specifying starting physical address and
114 * size
115 *
116 * Calls memblock reserve to set aside memory for heaps that are
117 * located at specific memory addresses or of specfic sizes not
118 * managed by the kernel
119 */
120 void ion_reserve(struct ion_platform_data *data);
122 /**
123 * ion_client_create() - allocate a client and returns it
124 * @dev: the global ion device
125 * @heap_type_mask: mask of heaps this client can allocate from
126 * @name: used for debugging
127 */
128 struct ion_client *ion_client_create(struct ion_device *dev,
129 const char *name);
131 /**
132 * ion_client_destroy() - free's a client and all it's handles
133 * @client: the client
134 *
135 * Free the provided client and all it's resources including
136 * any handles it is holding.
137 */
138 void ion_client_destroy(struct ion_client *client);
140 /**
141 * ion_alloc - allocate ion memory
142 * @client: the client
143 * @len: size of the allocation
144 * @align: requested allocation alignment, lots of hardware blocks
145 * have alignment requirements of some kind
146 * @heap_id_mask: mask of heaps to allocate from, if multiple bits are set
147 * heaps will be tried in order from highest to lowest
148 * id
149 * @flags: heap flags, the low 16 bits are consumed by ion, the
150 * high 16 bits are passed on to the respective heap and
151 * can be heap custom
152 *
153 * Allocate memory in one of the heaps provided in heap mask and return
154 * an opaque handle to it.
155 */
156 struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
157 size_t align, unsigned int heap_id_mask,
158 unsigned int flags);
160 /**
161 * ion_free - free a handle
162 * @client: the client
163 * @handle: the handle to free
164 *
165 * Free the provided handle.
166 */
167 void ion_free(struct ion_client *client, struct ion_handle *handle);
169 /**
170 * ion_phys - returns the physical address and len of a handle
171 * @client: the client
172 * @handle: the handle
173 * @addr: a pointer to put the address in
174 * @len: a pointer to put the length in
175 *
176 * This function queries the heap for a particular handle to get the
177 * handle's physical address. It't output is only correct if
178 * a heap returns physically contiguous memory -- in other cases
179 * this api should not be implemented -- ion_sg_table should be used
180 * instead. Returns -EINVAL if the handle is invalid. This has
181 * no implications on the reference counting of the handle --
182 * the returned value may not be valid if the caller is not
183 * holding a reference.
184 */
185 int ion_phys(struct ion_client *client, struct ion_handle *handle,
186 ion_phys_addr_t *addr, size_t *len);
188 /**
189 * ion_map_dma - return an sg_table describing a handle
190 * @client: the client
191 * @handle: the handle
192 *
193 * This function returns the sg_table describing
194 * a particular ion handle.
195 */
196 struct sg_table *ion_sg_table(struct ion_client *client,
197 struct ion_handle *handle);
199 /**
200 * ion_map_kernel - create mapping for the given handle
201 * @client: the client
202 * @handle: handle to map
203 *
204 * Map the given handle into the kernel and return a kernel address that
205 * can be used to access this address.
206 */
207 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
209 /**
210 * ion_unmap_kernel() - destroy a kernel mapping for a handle
211 * @client: the client
212 * @handle: handle to unmap
213 */
214 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
216 /**
217 * ion_share_dma_buf() - given an ion client, create a dma-buf fd
218 * @client: the client
219 * @handle: the handle
220 */
221 int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle);
223 /**
224 * ion_import_dma_buf() - given an dma-buf fd from the ion exporter get handle
225 * @client: the client
226 * @fd: the dma-buf fd
227 *
228 * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf,
229 * import that fd and return a handle representing it. If a dma-buf from
230 * another exporter is passed in this function will return ERR_PTR(-EINVAL)
231 */
232 struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd);
234 #endif /* __KERNEL__ */
236 /**
237 * DOC: Ion Userspace API
238 *
239 * create a client by opening /dev/ion
240 * most operations handled via following ioctls
241 *
242 */
244 /**
245 * struct ion_allocation_data - metadata passed from userspace for allocations
246 * @len: size of the allocation
247 * @align: required alignment of the allocation
248 * @heap_id_mask: mask of heap ids to allocate from
249 * @flags: flags passed to heap
250 * @handle: pointer that will be populated with a cookie to use to
251 * refer to this allocation
252 *
253 * Provided by userspace as an argument to the ioctl
254 */
255 struct ion_allocation_data {
256 size_t len;
257 size_t align;
258 unsigned int heap_id_mask;
259 unsigned int flags;
260 struct ion_handle *handle;
261 };
263 /**
264 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
265 * @handle: a handle
266 * @fd: a file descriptor representing that handle
267 *
268 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
269 * the handle returned from ion alloc, and the kernel returns the file
270 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
271 * provides the file descriptor and the kernel returns the handle.
272 */
273 struct ion_fd_data {
274 struct ion_handle *handle;
275 int fd;
276 };
278 /**
279 * struct ion_handle_data - a handle passed to/from the kernel
280 * @handle: a handle
281 */
282 struct ion_handle_data {
283 struct ion_handle *handle;
284 };
286 /**
287 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
288 * @cmd: the custom ioctl function to call
289 * @arg: additional data to pass to the custom ioctl, typically a user
290 * pointer to a predefined structure
291 *
292 * This works just like the regular cmd and arg fields of an ioctl.
293 */
294 struct ion_custom_data {
295 unsigned int cmd;
296 unsigned long arg;
297 };
299 #define ION_IOC_MAGIC 'I'
301 /**
302 * DOC: ION_IOC_ALLOC - allocate memory
303 *
304 * Takes an ion_allocation_data struct and returns it with the handle field
305 * populated with the opaque handle for the allocation.
306 */
307 #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
308 struct ion_allocation_data)
310 /**
311 * DOC: ION_IOC_FREE - free memory
312 *
313 * Takes an ion_handle_data struct and frees the handle.
314 */
315 #define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
317 /**
318 * DOC: ION_IOC_MAP - get a file descriptor to mmap
319 *
320 * Takes an ion_fd_data struct with the handle field populated with a valid
321 * opaque handle. Returns the struct with the fd field set to a file
322 * descriptor open in the current address space. This file descriptor
323 * can then be used as an argument to mmap.
324 */
325 #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
327 /**
328 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
329 *
330 * Takes an ion_fd_data struct with the handle field populated with a valid
331 * opaque handle. Returns the struct with the fd field set to a file
332 * descriptor open in the current address space. This file descriptor
333 * can then be passed to another process. The corresponding opaque handle can
334 * be retrieved via ION_IOC_IMPORT.
335 */
336 #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
338 /**
339 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
340 *
341 * Takes an ion_fd_data struct with the fd field populated with a valid file
342 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
343 * filed set to the corresponding opaque handle.
344 */
345 #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
347 /**
348 * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory
349 *
350 * Deprecated in favor of using the dma_buf api's correctly (syncing
351 * will happend automatically when the buffer is mapped to a device).
352 * If necessary should be used after touching a cached buffer from the cpu,
353 * this will make the buffer in memory coherent.
354 */
355 #define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
357 /**
358 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
359 *
360 * Takes the argument of the architecture specific ioctl to call and
361 * passes appropriate userdata for that ioctl
362 */
363 #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
365 #endif /* _LINUX_ION_H */