ion.h revision ce1f147a2ed5ed468ad3a2f8418dddfd205d1fd9
1/*
2 * drivers/staging/android/ion/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_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_HEAP_END:		 helper for iterating over heaps
31 */
32enum ion_heap_type {
33	ION_HEAP_TYPE_SYSTEM,
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 */
38	ION_NUM_HEAPS,
39};
40
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)
44
45#ifdef __KERNEL__
46struct ion_device;
47struct ion_heap;
48struct ion_mapper;
49struct ion_client;
50struct ion_buffer;
51
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
57
58/**
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
66 *
67 * Provided by the board file.
68 */
69struct ion_platform_heap {
70	enum ion_heap_type type;
71	unsigned int id;
72	const char *name;
73	ion_phys_addr_t base;
74	size_t size;
75};
76
77/**
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
81 *
82 * Provided by the board file in the form of platform data to a platform device.
83 */
84struct ion_platform_data {
85	int nr;
86	struct ion_platform_heap heaps[];
87};
88
89/**
90 * ion_reserve() - reserve memory for ion heaps if applicable
91 * @data:	platform data specifying starting physical address and
92 *		size
93 *
94 * Calls memblock reserve to set aside memory for heaps that are
95 * located at specific memory addresses or of specfic sizes not
96 * managed by the kernel
97 */
98void ion_reserve(struct ion_platform_data *data);
99
100/**
101 * ion_client_create() -  allocate a client and returns it
102 * @dev:	the global ion device
103 * @heap_mask:	mask of heaps this client can allocate from
104 * @name:	used for debugging
105 */
106struct ion_client *ion_client_create(struct ion_device *dev,
107				     unsigned int heap_mask, const char *name);
108
109/**
110 * ion_client_destroy() -  free's a client and all it's handles
111 * @client:	the client
112 *
113 * Free the provided client and all it's resources including
114 * any handles it is holding.
115 */
116void ion_client_destroy(struct ion_client *client);
117
118/**
119 * ion_alloc - allocate ion memory
120 * @client:	the client
121 * @len:	size of the allocation
122 * @align:	requested allocation alignment, lots of hardware blocks have
123 *		alignment requirements of some kind
124 * @flags:	mask of heaps to allocate from, if multiple bits are set
125 *		heaps will be tried in order from lowest to highest order bit
126 *
127 * Allocate memory in one of the heaps provided in heap mask and return
128 * an opaque handle to it.
129 */
130struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
131			     size_t align, unsigned int flags);
132
133/**
134 * ion_free - free a handle
135 * @client:	the client
136 * @handle:	the handle to free
137 *
138 * Free the provided handle.
139 */
140void ion_free(struct ion_client *client, struct ion_handle *handle);
141
142/**
143 * ion_phys - returns the physical address and len of a handle
144 * @client:	the client
145 * @handle:	the handle
146 * @addr:	a pointer to put the address in
147 * @len:	a pointer to put the length in
148 *
149 * This function queries the heap for a particular handle to get the
150 * handle's physical address.  It't output is only correct if
151 * a heap returns physically contiguous memory -- in other cases
152 * this api should not be implemented -- ion_sg_table should be used
153 * instead.  Returns -EINVAL if the handle is invalid.  This has
154 * no implications on the reference counting of the handle --
155 * the returned value may not be valid if the caller is not
156 * holding a reference.
157 */
158int ion_phys(struct ion_client *client, struct ion_handle *handle,
159	     ion_phys_addr_t *addr, size_t *len);
160
161/**
162 * ion_map_dma - return an sg_table describing a handle
163 * @client:	the client
164 * @handle:	the handle
165 *
166 * This function returns the sg_table describing
167 * a particular ion handle.
168 */
169struct sg_table *ion_sg_table(struct ion_client *client,
170			      struct ion_handle *handle);
171
172/**
173 * ion_map_kernel - create mapping for the given handle
174 * @client:	the client
175 * @handle:	handle to map
176 *
177 * Map the given handle into the kernel and return a kernel address that
178 * can be used to access this address.
179 */
180void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
181
182/**
183 * ion_unmap_kernel() - destroy a kernel mapping for a handle
184 * @client:	the client
185 * @handle:	handle to unmap
186 */
187void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
188
189/**
190 * ion_share_dma_buf() - given an ion client, create a dma-buf fd
191 * @client:	the client
192 * @handle:	the handle
193 */
194int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle);
195
196/**
197 * ion_import_dma_buf() - given an dma-buf fd from the ion exporter get handle
198 * @client:	the client
199 * @fd:		the dma-buf fd
200 *
201 * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf,
202 * import that fd and return a handle representing it.  If a dma-buf from
203 * another exporter is passed in this function will return ERR_PTR(-EINVAL)
204 */
205struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd);
206
207#endif /* __KERNEL__ */
208
209/**
210 * DOC: Ion Userspace API
211 *
212 * create a client by opening /dev/ion
213 * most operations handled via following ioctls
214 *
215 */
216
217/**
218 * struct ion_allocation_data - metadata passed from userspace for allocations
219 * @len:	size of the allocation
220 * @align:	required alignment of the allocation
221 * @flags:	flags passed to heap
222 * @handle:	pointer that will be populated with a cookie to use to refer
223 *		to this allocation
224 *
225 * Provided by userspace as an argument to the ioctl
226 */
227struct ion_allocation_data {
228	size_t len;
229	size_t align;
230	unsigned int flags;
231	struct ion_handle *handle;
232};
233
234/**
235 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
236 * @handle:	a handle
237 * @fd:		a file descriptor representing that handle
238 *
239 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
240 * the handle returned from ion alloc, and the kernel returns the file
241 * descriptor to share or map in the fd field.  For ION_IOC_IMPORT, userspace
242 * provides the file descriptor and the kernel returns the handle.
243 */
244struct ion_fd_data {
245	struct ion_handle *handle;
246	int fd;
247};
248
249/**
250 * struct ion_handle_data - a handle passed to/from the kernel
251 * @handle:	a handle
252 */
253struct ion_handle_data {
254	struct ion_handle *handle;
255};
256
257/**
258 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
259 * @cmd:	the custom ioctl function to call
260 * @arg:	additional data to pass to the custom ioctl, typically a user
261 *		pointer to a predefined structure
262 *
263 * This works just like the regular cmd and arg fields of an ioctl.
264 */
265struct ion_custom_data {
266	unsigned int cmd;
267	unsigned long arg;
268};
269
270#define ION_IOC_MAGIC		'I'
271
272/**
273 * DOC: ION_IOC_ALLOC - allocate memory
274 *
275 * Takes an ion_allocation_data struct and returns it with the handle field
276 * populated with the opaque handle for the allocation.
277 */
278#define ION_IOC_ALLOC		_IOWR(ION_IOC_MAGIC, 0, \
279				      struct ion_allocation_data)
280
281/**
282 * DOC: ION_IOC_FREE - free memory
283 *
284 * Takes an ion_handle_data struct and frees the handle.
285 */
286#define ION_IOC_FREE		_IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
287
288/**
289 * DOC: ION_IOC_MAP - get a file descriptor to mmap
290 *
291 * Takes an ion_fd_data struct with the handle field populated with a valid
292 * opaque handle.  Returns the struct with the fd field set to a file
293 * descriptor open in the current address space.  This file descriptor
294 * can then be used as an argument to mmap.
295 */
296#define ION_IOC_MAP		_IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
297
298/**
299 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
300 *
301 * Takes an ion_fd_data struct with the handle field populated with a valid
302 * opaque handle.  Returns the struct with the fd field set to a file
303 * descriptor open in the current address space.  This file descriptor
304 * can then be passed to another process.  The corresponding opaque handle can
305 * be retrieved via ION_IOC_IMPORT.
306 */
307#define ION_IOC_SHARE		_IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
308
309/**
310 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
311 *
312 * Takes an ion_fd_data struct with the fd field populated with a valid file
313 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
314 * filed set to the corresponding opaque handle.
315 */
316#define ION_IOC_IMPORT		_IOWR(ION_IOC_MAGIC, 5, int)
317
318/**
319 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
320 *
321 * Takes the argument of the architecture specific ioctl to call and
322 * passes appropriate userdata for that ioctl
323 */
324#define ION_IOC_CUSTOM		_IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
325
326#endif /* _LINUX_ION_H */
327