4 * Copyright (C) 2011 Google, Inc.
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.
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.
20 #include <linux/types.h>
21 #define ION_VERSION "1.0"
25 * enum ion_heap_types - list of all possible types of heaps
26 * @ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc
27 * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc
28 * @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved
29 * carveout heap, allocations are physically
31 * @ION_HEAP_END: helper for iterating over heaps
35 ION_HEAP_TYPE_SYSTEM_CONTIG,
36 ION_HEAP_TYPE_CARVEOUT,
37 ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
38 are at the end of this enum */
50 #define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
51 #define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
52 #define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
61 /* This should be removed some day when phys_addr_t's are fully
62 plumbed in the kernel, and all instances of ion_phys_addr_t should
63 be converted to phys_addr_t. For the time being many kernel interfaces
64 do not accept phys_addr_t's that would have to */
65 #define ion_phys_addr_t unsigned long
68 * struct ion_platform_heap - defines a heap in the given platform
69 * @type: type of the heap from ion_heap_type enum
70 * @id: unique identifier for heap. When allocating (lower numbers
71 * will be allocated from first)
72 * @name: used for debug purposes
73 * @base: base address of heap in physical memory if applicable
74 * @size: size of the heap in bytes if applicable
76 * Provided by the board file.
78 struct ion_platform_heap {
79 enum ion_heap_type type;
87 * struct ion_platform_data - array of platform heaps passed from board file
88 * @nr: number of structures in the array
89 * @heaps: array of platform_heap structions
91 * Provided by the board file in the form of platform data to a platform device.
93 struct ion_platform_data {
95 struct ion_platform_heap heaps[];
99 * ion_client_create() - allocate a client and returns it
100 * @dev: the global ion device
101 * @heap_mask: mask of heaps this client can allocate from
102 * @name: used for debugging
104 struct ion_client *ion_client_create(struct ion_device *dev,
105 unsigned int heap_mask, const char *name);
108 * ion_client_destroy() - free's a client and all it's handles
109 * @client: the client
111 * Free the provided client and all it's resources including
112 * any handles it is holding.
114 void ion_client_destroy(struct ion_client *client);
117 * ion_alloc - allocate ion memory
118 * @client: the client
119 * @len: size of the allocation
120 * @align: requested allocation alignment, lots of hardware blocks have
121 * alignment requirements of some kind
122 * @flags: mask of heaps to allocate from, if multiple bits are set
123 * heaps will be tried in order from lowest to highest order bit
125 * Allocate memory in one of the heaps provided in heap mask and return
126 * an opaque handle to it.
128 struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
129 size_t align, unsigned int flags);
132 * ion_free - free a handle
133 * @client: the client
134 * @handle: the handle to free
136 * Free the provided handle.
138 void ion_free(struct ion_client *client, struct ion_handle *handle);
141 * ion_phys - returns the physical address and len of a handle
142 * @client: the client
143 * @handle: the handle
144 * @addr: a pointer to put the address in
145 * @len: a pointer to put the length in
147 * This function queries the heap for a particular handle to get the
148 * handle's physical address. It't output is only correct if
149 * a heap returns physically contiguous memory -- in other cases
150 * this api should not be implemented -- ion_map_dma should be used
151 * instead. Returns -EINVAL if the handle is invalid. This has
152 * no implications on the reference counting of the handle --
153 * the returned value may not be valid if the caller is not
154 * holding a reference.
156 int ion_phys(struct ion_client *client, struct ion_handle *handle,
157 ion_phys_addr_t *addr, size_t *len);
160 * ion_map_kernel - create mapping for the given handle
161 * @client: the client
162 * @handle: handle to map
164 * Map the given handle into the kernel and return a kernel address that
165 * can be used to access this address.
167 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
170 * ion_unmap_kernel() - destroy a kernel mapping for a handle
171 * @client: the client
172 * @handle: handle to unmap
174 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
177 * ion_map_dma - create a dma mapping for a given handle
178 * @client: the client
179 * @handle: handle to map
181 * Return an sglist describing the given handle
183 struct scatterlist *ion_map_dma(struct ion_client *client,
184 struct ion_handle *handle);
187 * ion_unmap_dma() - destroy a dma mapping for a handle
188 * @client: the client
189 * @handle: handle to unmap
191 void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle);
194 * ion_share() - given a handle, obtain a buffer to pass to other clients
195 * @client: the client
196 * @handle: the handle to share
198 * Given a handle, return a buffer, which exists in a global name
199 * space, and can be passed to other clients. Should be passed into ion_import
200 * to obtain a new handle for this buffer.
202 * NOTE: This function does do not an extra reference. The burden is on the
203 * caller to make sure the buffer doesn't go away while it's being passed to
204 * another client. That is, ion_free should not be called on this handle until
205 * the buffer has been imported into the other client.
207 struct ion_buffer *ion_share(struct ion_client *client,
208 struct ion_handle *handle);
211 * ion_import() - given an buffer in another client, import it
212 * @client: this blocks client
213 * @buffer: the buffer to import (as obtained from ion_share)
215 * Given a buffer, add it to the client and return the handle to use to refer
216 * to it further. This is called to share a handle from one kernel client to
219 struct ion_handle *ion_import(struct ion_client *client,
220 struct ion_buffer *buffer);
223 * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it
224 * @client: this blocks client
227 * A helper function for drivers that will be recieving ion buffers shared
228 * with them from userspace. These buffers are represented by a file
229 * descriptor obtained as the return from the ION_IOC_SHARE ioctl.
230 * This function coverts that fd into the underlying buffer, and returns
231 * the handle to use to refer to it further.
233 struct ion_handle *ion_import_fd(struct ion_client *client, int fd);
234 #endif /* __KERNEL__ */
237 * DOC: Ion Userspace API
239 * create a client by opening /dev/ion
240 * most operations handled via following ioctls
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 * @flags: flags passed to heap
249 * @handle: pointer that will be populated with a cookie to use to refer
252 * Provided by userspace as an argument to the ioctl
254 struct ion_allocation_data {
258 struct ion_handle *handle;
262 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
264 * @fd: a file descriptor representing that handle
266 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
267 * the handle returned from ion alloc, and the kernel returns the file
268 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
269 * provides the file descriptor and the kernel returns the handle.
272 struct ion_handle *handle;
277 * struct ion_handle_data - a handle passed to/from the kernel
280 struct ion_handle_data {
281 struct ion_handle *handle;
285 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
286 * @cmd: the custom ioctl function to call
287 * @arg: additional data to pass to the custom ioctl, typically a user
288 * pointer to a predefined structure
290 * This works just like the regular cmd and arg fields of an ioctl.
292 struct ion_custom_data {
297 struct ion_phys_data {
298 struct ion_handle *handle;
302 struct ion_cacheop_data {
303 #define ION_CACHE_FLUSH 0
304 #define ION_CACHE_CLEAN 1
305 #define ION_CACHE_INV 2
307 struct ion_handle *handle;
310 struct ion_buffer_info {
314 struct ion_client_info {
315 #define MAX_BUFFER_COUNT 127
317 unsigned long total_size;
318 struct ion_buffer_info buf[MAX_BUFFER_COUNT];
320 struct ion_heap_info {
322 unsigned long allocated_size;
323 unsigned long max_allocated;
324 unsigned long total_size;
327 #define ION_IOC_MAGIC 'I'
330 * DOC: ION_IOC_ALLOC - allocate memory
332 * Takes an ion_allocation_data struct and returns it with the handle field
333 * populated with the opaque handle for the allocation.
335 #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
336 struct ion_allocation_data)
339 * DOC: ION_IOC_FREE - free memory
341 * Takes an ion_handle_data struct and frees the handle.
343 #define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
346 * DOC: ION_IOC_MAP - get a file descriptor to mmap
348 * Takes an ion_fd_data struct with the handle field populated with a valid
349 * opaque handle. Returns the struct with the fd field set to a file
350 * descriptor open in the current address space. This file descriptor
351 * can then be used as an argument to mmap.
353 #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
356 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
358 * Takes an ion_fd_data struct with the handle field populated with a valid
359 * opaque handle. Returns the struct with the fd field set to a file
360 * descriptor open in the current address space. This file descriptor
361 * can then be passed to another process. The corresponding opaque handle can
362 * be retrieved via ION_IOC_IMPORT.
364 #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
367 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
369 * Takes an ion_fd_data struct with the fd field populated with a valid file
370 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
371 * filed set to the corresponding opaque handle.
373 #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int)
376 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
378 * Takes the argument of the architecture specific ioctl to call and
379 * passes appropriate userdata for that ioctl
381 #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
383 #define ION_CUSTOM_GET_PHYS _IOWR(ION_IOC_MAGIC, 7, \
384 struct ion_phys_data)
386 #define ION_CUSTOM_CACHE_OP _IOWR(ION_IOC_MAGIC, 8, \
387 struct ion_cacheop_data)
389 #define ION_CUSTOM_GET_CLIENT_INFO _IOWR(ION_IOC_MAGIC, 9, \
390 struct ion_client_info)
392 #define ION_CUSTOM_GET_HEAP_INFO _IOWR(ION_IOC_MAGIC, 10, \
393 struct ion_heap_info)
394 /* Compatible with pmem */
395 struct ion_pmem_region {
396 unsigned long offset;
399 #define ION_PMEM_GET_PHYS _IOW('p', 1, unsigned int)
400 #define ION_PMEM_CACHE_FLUSH _IOW('p', 8, unsigned int)
401 #endif /* _LINUX_ION_H */