blob: b783132f76664618698662f45318ab700d0d2d02 [file] [log] [blame]
Rebecca Schultz Zavin1c538d82011-06-29 19:44:29 -07001/*
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 */
16
17#ifndef _LINUX_ION_H
18#define _LINUX_ION_H
19
20#include <linux/types.h>
21
22struct ion_handle;
23/**
24 * enum ion_heap_types - list of all possible types of heaps
25 * @ION_HEAP_SYSTEM: memory allocated via vmalloc
26 * @ION_HEAP_SYSTEM_CONTIG: memory allocated via kmalloc
27 * @ION_HEAP_END: helper for iterating over heaps
28 */
29enum ion_heap_type {
30 ION_HEAP_TYPE_SYSTEM,
31 ION_HEAP_TYPE_SYSTEM_CONTIG,
32 ION_HEAP_TYPE_CARVEOUT,
33 ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
34 are at the end of this enum */
35 ION_HEAP_END,
36};
37
38#define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_SYSTEM)
39#define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_SYSTEM_CONTIG)
40#define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_CARVEOUT)
41
42#ifdef __KERNEL__
43struct ion_device;
44struct ion_heap;
45struct ion_mapper;
46struct ion_client;
47struct ion_buffer;
48
49/* This should be removed some day when phys_addr_t's are fully
50 plumbed in the kernel, and all instances of ion_phys_addr_t should
51 be converted to phys_addr_t. For the time being many kernel interfaces
52 do not accept phys_addr_t's that would have to */
53#define ion_phys_addr_t unsigned long
54
55#define ION_NUM_HEAPS ION_HEAP_END
56
57/**
58 * struct ion_platform_heap - defines a heap in the given platform
59 * @type: type of the heap from ion_heap_type enum
60 * @prio: priority of the heap when allocating (lower numbers will be
61 * allocated from first), MUST be unique
62 * @name: used for debug purposes
63 * @base: base address of heap in physical memory if applicable
64 * @size: size of the heap in bytes if applicable
65 *
66 * Provided by the board file.
67 */
68struct ion_platform_heap {
69 enum ion_heap_type type;
70 unsigned int prio;
71 const char *name;
72 ion_phys_addr_t base;
73 size_t size;
74};
75
76/**
77 * struct ion_platform_data - array of platform heaps passed from board file
78 * @heaps: array of platform_heap structions
79 * @nr: number of structures in the array
80 *
81 * Provided by the board file in the form of platform data to a platform device.
82 */
83struct ion_platform_data {
84 int nr;
85 struct ion_platform_heap heaps[];
86};
87
88/**
89 * ion_client_create() - allocate a client and returns it
90 * @dev: the global ion device
91 * @heap_mask: mask of heaps this client can allocate from
92 * @name: used for debugging
93 */
94struct ion_client *ion_client_create(struct ion_device *dev,
95 unsigned int heap_mask, const char *name);
96
97/**
98 * ion_client_destroy() - free's a client and all it's handles
99 * @client: the client
100 *
101 * Free the provided client and all it's resources including
102 * any handles it is holding.
103 */
104void ion_client_destroy(struct ion_client *client);
105
106/**
107 * ion_alloc - allocate ion memory
108 * @client: the client
109 * @len: size of the allocation
110 * @align: requested allocation alignment, lots of hardware blocks have
111 * alignment requirements of some kind
112 * @flags: flags to pass along to heaps
113 *
114 * Allocate memory in one of the heaps provided in heap mask and return
115 * an opaque handle to it.
116 */
117struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
118 size_t align, unsigned int flags);
119
120/**
121 * ion_free - free a handle
122 * @client: the client
123 * @handle: the handle to free
124 *
125 * Free the provided handle.
126 */
127void ion_free(struct ion_client *client, struct ion_handle *handle);
128
129/**
130 * ion_phys - returns the physical address and len of a handle
131 * @client: the client
132 * @handle: the handle
133 * @addr: a pointer to put the address in
134 * @len: a pointer to put the length in
135 *
136 * This function queries the heap for a particular handle to get the
137 * handle's physical address. It't output is only correct if
138 * a heap returns physically contiguous memory -- in other cases
139 * this api should not be implemented -- ion_map_dma should be used
140 * instead. Returns -EINVAL if the handle is invalid. This has
141 * no implications on the reference counting of the handle --
142 * the returned value may not be valid if the caller is not
143 * holding a reference.
144 */
145int ion_phys(struct ion_client *client, struct ion_handle *handle,
146 ion_phys_addr_t *addr, size_t *len);
147
148/**
149 * ion_map_kernel - create mapping for the given handle
150 * @client: the client
151 * @handle: handle to map
152 *
153 * Map the given handle into the kernel and return a kernel address that
154 * can be used to access this address.
155 */
156void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
157
158/**
159 * ion_unmap_kernel() - destroy a kernel mapping for a handle
160 * @client: the client
161 * @handle: handle to unmap
162 */
163void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
164
165/**
166 * ion_map_dma - create a dma mapping for a given handle
167 * @client: the client
168 * @handle: handle to map
169 *
170 * Return an sglist describing the given handle
171 */
172struct scatterlist *ion_map_dma(struct ion_client *client,
173 struct ion_handle *handle);
174
175/**
176 * ion_unmap_dma() - destroy a dma mapping for a handle
177 * @client: the client
178 * @handle: handle to unmap
179 */
180void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle);
181
182/**
183 * ion_share() - given a handle, obtain a buffer to pass to other clients
184 * @client: the client
185 * @handle: the handle to share
186 *
187 * Given a handle, return a buffer which exists in a global name
188 * space and can be passed to other clients. Should be passed into ion_import
189 * to obtain a new handle for this buffer.
190 */
191struct ion_buffer *ion_share(struct ion_client *client,
192 struct ion_handle *handle);
193
194/**
195 * ion_import() - given an buffer in another client, import it
196 * @client: this blocks client
197 * @buffer: the buffer to import (as obtained from ion_share)
198 *
199 * Given a buffer, add it to the client and return the handle to use to refer
200 * to it further. This is called to share a handle from one kernel client to
201 * another.
202 */
203struct ion_handle *ion_import(struct ion_client *client,
204 struct ion_buffer *buffer);
205
206/**
207 * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it
208 * @client: this blocks client
209 * @fd: the fd
210 *
211 * A helper function for drivers that will be recieving ion buffers shared
212 * with them from userspace. These buffers are represented by a file
213 * descriptor obtained as the return from the ION_IOC_SHARE ioctl.
214 * This function coverts that fd into the underlying buffer, and returns
215 * the handle to use to refer to it further.
216 */
217struct ion_handle *ion_import_fd(struct ion_client *client, int fd);
218#endif /* __KERNEL__ */
219
220/**
221 * DOC: Ion Userspace API
222 *
223 * create a client by opening /dev/ion
224 * most operations handled via following ioctls
225 *
226 */
227
228/**
229 * struct ion_allocation_data - metadata passed from userspace for allocations
230 * @len: size of the allocation
231 * @align: required alignment of the allocation
232 * @flags: flags passed to heap
233 * @handle: pointer that will be populated with a cookie to use to refer
234 * to this allocation
235 *
236 * Provided by userspace as an argument to the ioctl
237 */
238struct ion_allocation_data {
239 size_t len;
240 size_t align;
241 unsigned int flags;
242 struct ion_handle *handle;
243};
244
245/**
246 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
247 * @handle: a handle
248 * @fd: a file descriptor representing that handle
249 *
250 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
251 * the handle returned from ion alloc, and the kernel returns the file
252 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
253 * provides the file descriptor and the kernel returns the handle.
254 */
255struct ion_fd_data {
256 struct ion_handle *handle;
257 int fd;
258};
259
260/**
261 * struct ion_handle_data - a handle passed to/from the kernel
262 * @handle: a handle
263 */
264struct ion_handle_data {
265 struct ion_handle *handle;
266};
267
268struct ion_custom_data {
269 unsigned int cmd;
270 unsigned long arg;
271};
272
273#define ION_IOC_MAGIC 'I'
274
275/**
276 * DOC: ION_IOC_ALLOC - allocate memory
277 *
278 * Takes an ion_allocation_data struct and returns it with the handle field
279 * populated with the opaque handle for the allocation.
280 */
281#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
282 struct ion_allocation_data)
283
284/**
285 * DOC: ION_IOC_FREE - free memory
286 *
287 * Takes an ion_handle_data struct and frees the handle.
288 */
289#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
290
291/**
292 * DOC: ION_IOC_MAP - get a file descriptor to mmap
293 *
294 * Takes an ion_fd_data struct with the handle field populated with a valid
295 * opaque handle. Returns the struct with the fd field set to a file
296 * descriptor open in the current address space. This file descriptor
297 * can then be used as an argument to mmap.
298 */
299#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
300
301/**
302 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
303 *
304 * Takes an ion_fd_data struct with the handle field populated with a valid
305 * opaque handle. Returns the struct with the fd field set to a file
306 * descriptor open in the current address space. This file descriptor
307 * can then be passed to another process. The corresponding opaque handle can
308 * be retrieved via ION_IOC_IMPORT.
309 */
310#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
311
312/**
313 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
314 *
315 * Takes an ion_fd_data struct with the fd field populated with a valid file
316 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
317 * filed set to the corresponding opaque handle.
318 */
319#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int)
320
321/**
322 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
323 *
324 * Takes the argument of the architecture specific ioctl to call and
325 * passes appropriate userdata for that ioctl
326 */
327#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
328
329#endif /* _LINUX_ION_H */