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>
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
30 * @ION_HEAP_END: helper for iterating over heaps
34 ION_HEAP_TYPE_SYSTEM_CONTIG,
35 ION_HEAP_TYPE_CARVEOUT,
36 ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
37 are at the end of this enum */
41 #define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
42 #define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
43 #define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
52 /* This should be removed some day when phys_addr_t's are fully
53 plumbed in the kernel, and all instances of ion_phys_addr_t should
54 be converted to phys_addr_t. For the time being many kernel interfaces
55 do not accept phys_addr_t's that would have to */
56 #define ion_phys_addr_t unsigned long
59 * struct ion_platform_heap - defines a heap in the given platform
60 * @type: type of the heap from ion_heap_type enum
61 * @id: unique identifier for heap. When allocating (lower numbers
62 * will be allocated from first)
63 * @name: used for debug purposes
64 * @base: base address of heap in physical memory if applicable
65 * @size: size of the heap in bytes if applicable
67 * Provided by the board file.
69 struct ion_platform_heap {
70 enum ion_heap_type type;
78 * struct ion_platform_data - array of platform heaps passed from board file
79 * @nr: number of structures in the array
80 * @heaps: array of platform_heap structions
82 * Provided by the board file in the form of platform data to a platform device.
84 struct ion_platform_data {
86 struct ion_platform_heap heaps[];
90 * ion_client_create() - allocate a client and returns it
91 * @dev: the global ion device
92 * @heap_mask: mask of heaps this client can allocate from
93 * @name: used for debugging
95 struct ion_client *ion_client_create(struct ion_device *dev,
96 unsigned int heap_mask, const char *name);
99 * ion_client_destroy() - free's a client and all it's handles
100 * @client: the client
102 * Free the provided client and all it's resources including
103 * any handles it is holding.
105 void ion_client_destroy(struct ion_client *client);
108 * ion_alloc - allocate ion memory
109 * @client: the client
110 * @len: size of the allocation
111 * @align: requested allocation alignment, lots of hardware blocks have
112 * alignment requirements of some kind
113 * @flags: mask of heaps to allocate from, if multiple bits are set
114 * heaps will be tried in order from lowest to highest order bit
116 * Allocate memory in one of the heaps provided in heap mask and return
117 * an opaque handle to it.
119 struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
120 size_t align, unsigned int flags);
123 * ion_free - free a handle
124 * @client: the client
125 * @handle: the handle to free
127 * Free the provided handle.
129 void ion_free(struct ion_client *client, struct ion_handle *handle);
132 * ion_phys - returns the physical address and len of a handle
133 * @client: the client
134 * @handle: the handle
135 * @addr: a pointer to put the address in
136 * @len: a pointer to put the length in
138 * This function queries the heap for a particular handle to get the
139 * handle's physical address. It't output is only correct if
140 * a heap returns physically contiguous memory -- in other cases
141 * this api should not be implemented -- ion_map_dma should be used
142 * instead. Returns -EINVAL if the handle is invalid. This has
143 * no implications on the reference counting of the handle --
144 * the returned value may not be valid if the caller is not
145 * holding a reference.
147 int ion_phys(struct ion_client *client, struct ion_handle *handle,
148 ion_phys_addr_t *addr, size_t *len);
151 * ion_map_kernel - create mapping for the given handle
152 * @client: the client
153 * @handle: handle to map
155 * Map the given handle into the kernel and return a kernel address that
156 * can be used to access this address.
158 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
161 * ion_unmap_kernel() - destroy a kernel mapping for a handle
162 * @client: the client
163 * @handle: handle to unmap
165 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
168 * ion_map_dma - create a dma mapping for a given handle
169 * @client: the client
170 * @handle: handle to map
172 * Return an sglist describing the given handle
174 struct scatterlist *ion_map_dma(struct ion_client *client,
175 struct ion_handle *handle);
178 * ion_unmap_dma() - destroy a dma mapping for a handle
179 * @client: the client
180 * @handle: handle to unmap
182 void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle);
185 * ion_share() - given a handle, obtain a buffer to pass to other clients
186 * @client: the client
187 * @handle: the handle to share
189 * Given a handle, return a buffer, which exists in a global name
190 * space, and can be passed to other clients. Should be passed into ion_import
191 * to obtain a new handle for this buffer.
193 * NOTE: This function does do not an extra reference. The burden is on the
194 * caller to make sure the buffer doesn't go away while it's being passed to
195 * another client. That is, ion_free should not be called on this handle until
196 * the buffer has been imported into the other client.
198 struct ion_buffer *ion_share(struct ion_client *client,
199 struct ion_handle *handle);
202 * ion_import() - given an buffer in another client, import it
203 * @client: this blocks client
204 * @buffer: the buffer to import (as obtained from ion_share)
206 * Given a buffer, add it to the client and return the handle to use to refer
207 * to it further. This is called to share a handle from one kernel client to
210 struct ion_handle *ion_import(struct ion_client *client,
211 struct ion_buffer *buffer);
214 * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it
215 * @client: this blocks client
218 * A helper function for drivers that will be recieving ion buffers shared
219 * with them from userspace. These buffers are represented by a file
220 * descriptor obtained as the return from the ION_IOC_SHARE ioctl.
221 * This function coverts that fd into the underlying buffer, and returns
222 * the handle to use to refer to it further.
224 struct ion_handle *ion_import_fd(struct ion_client *client, int fd);
225 #endif /* __KERNEL__ */
228 * DOC: Ion Userspace API
230 * create a client by opening /dev/ion
231 * most operations handled via following ioctls
236 * struct ion_allocation_data - metadata passed from userspace for allocations
237 * @len: size of the allocation
238 * @align: required alignment of the allocation
239 * @flags: flags passed to heap
240 * @handle: pointer that will be populated with a cookie to use to refer
243 * Provided by userspace as an argument to the ioctl
245 struct ion_allocation_data {
249 struct ion_handle *handle;
253 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
255 * @fd: a file descriptor representing that handle
257 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
258 * the handle returned from ion alloc, and the kernel returns the file
259 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
260 * provides the file descriptor and the kernel returns the handle.
263 struct ion_handle *handle;
268 * struct ion_handle_data - a handle passed to/from the kernel
271 struct ion_handle_data {
272 struct ion_handle *handle;
276 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
277 * @cmd: the custom ioctl function to call
278 * @arg: additional data to pass to the custom ioctl, typically a user
279 * pointer to a predefined structure
281 * This works just like the regular cmd and arg fields of an ioctl.
283 struct ion_custom_data {
288 #define ION_IOC_MAGIC 'I'
291 * DOC: ION_IOC_ALLOC - allocate memory
293 * Takes an ion_allocation_data struct and returns it with the handle field
294 * populated with the opaque handle for the allocation.
296 #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
297 struct ion_allocation_data)
300 * DOC: ION_IOC_FREE - free memory
302 * Takes an ion_handle_data struct and frees the handle.
304 #define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
307 * DOC: ION_IOC_MAP - get a file descriptor to mmap
309 * Takes an ion_fd_data struct with the handle field populated with a valid
310 * opaque handle. Returns the struct with the fd field set to a file
311 * descriptor open in the current address space. This file descriptor
312 * can then be used as an argument to mmap.
314 #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
317 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
319 * Takes an ion_fd_data struct with the handle field populated with a valid
320 * opaque handle. Returns the struct with the fd field set to a file
321 * descriptor open in the current address space. This file descriptor
322 * can then be passed to another process. The corresponding opaque handle can
323 * be retrieved via ION_IOC_IMPORT.
325 #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
328 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
330 * Takes an ion_fd_data struct with the fd field populated with a valid file
331 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
332 * filed set to the corresponding opaque handle.
334 #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int)
337 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
339 * Takes the argument of the architecture specific ioctl to call and
340 * passes appropriate userdata for that ioctl
342 #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
344 #endif /* _LINUX_ION_H */