blob: d6dcf3803f44ac7556159888a0fc3d3c1645db20 [file] [log] [blame]
Rebecca Schultz Zavinc80005a2011-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>
Laura Abbotta2e93632011-08-19 13:36:32 -070021#include <mach/ion.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070022
23struct ion_handle;
24/**
25 * enum ion_heap_types - list of all possible types of heaps
Iliyan Malchevf22301562011-07-06 16:53:21 -070026 * @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
30 * contiguous
31 * @ION_HEAP_END: helper for iterating over heaps
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070032 */
33enum ion_heap_type {
34 ION_HEAP_TYPE_SYSTEM,
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 */
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070039 ION_NUM_HEAPS,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070040};
41
Iliyan Malchevf22301562011-07-06 16:53:21 -070042#define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
43#define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
44#define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070045
Laura Abbotta2e93632011-08-19 13:36:32 -070046
47/**
48 * These are the only ids that should be used for Ion heap ids.
49 * The ids listed are the order in which allocation will be attempted
50 * if specified. Don't swap the order of heap ids unless you know what
51 * you are doing!
52 */
53
54enum ion_heap_ids {
55 ION_HEAP_SYSTEM_ID,
56 ION_HEAP_SYSTEM_CONTIG_ID,
57 ION_HEAP_EBI_ID,
58 ION_HEAP_SMI_ID,
59};
60
61#define ION_KMALLOC_HEAP_NAME "kmalloc"
62#define ION_VMALLOC_HEAP_NAME "vmalloc"
63#define ION_EBI1_HEAP_NAME "EBI1"
64
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070065#ifdef __KERNEL__
66struct ion_device;
67struct ion_heap;
68struct ion_mapper;
69struct ion_client;
70struct ion_buffer;
71
72/* This should be removed some day when phys_addr_t's are fully
73 plumbed in the kernel, and all instances of ion_phys_addr_t should
74 be converted to phys_addr_t. For the time being many kernel interfaces
75 do not accept phys_addr_t's that would have to */
76#define ion_phys_addr_t unsigned long
77
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070078/**
79 * struct ion_platform_heap - defines a heap in the given platform
80 * @type: type of the heap from ion_heap_type enum
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070081 * @id: unique identifier for heap. When allocating (lower numbers
82 * will be allocated from first)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070083 * @name: used for debug purposes
84 * @base: base address of heap in physical memory if applicable
85 * @size: size of the heap in bytes if applicable
86 *
87 * Provided by the board file.
88 */
89struct ion_platform_heap {
90 enum ion_heap_type type;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070091 unsigned int id;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070092 const char *name;
93 ion_phys_addr_t base;
94 size_t size;
Laura Abbotta2e93632011-08-19 13:36:32 -070095 enum ion_memory_types memory_type;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070096};
97
98/**
99 * struct ion_platform_data - array of platform heaps passed from board file
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700100 * @nr: number of structures in the array
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700101 * @heaps: array of platform_heap structions
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700102 *
103 * Provided by the board file in the form of platform data to a platform device.
104 */
105struct ion_platform_data {
106 int nr;
107 struct ion_platform_heap heaps[];
108};
109
110/**
111 * ion_client_create() - allocate a client and returns it
112 * @dev: the global ion device
113 * @heap_mask: mask of heaps this client can allocate from
114 * @name: used for debugging
115 */
116struct ion_client *ion_client_create(struct ion_device *dev,
117 unsigned int heap_mask, const char *name);
118
119/**
Laura Abbott302911d2011-08-15 17:12:57 -0700120 * msm_ion_client_create - allocate a client using the ion_device specified in
121 * drivers/gpu/ion/msm/msm_ion.c
122 *
123 * heap_mask and name are the same as ion_client_create, return values
124 * are the same as ion_client_create.
125 */
126
127struct ion_client *msm_ion_client_create(unsigned int heap_mask,
128 const char *name);
129
130/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700131 * ion_client_destroy() - free's a client and all it's handles
132 * @client: the client
133 *
134 * Free the provided client and all it's resources including
135 * any handles it is holding.
136 */
137void ion_client_destroy(struct ion_client *client);
138
139/**
140 * ion_alloc - allocate ion memory
141 * @client: the client
142 * @len: size of the allocation
143 * @align: requested allocation alignment, lots of hardware blocks have
144 * alignment requirements of some kind
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700145 * @flags: mask of heaps to allocate from, if multiple bits are set
146 * heaps will be tried in order from lowest to highest order bit
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700147 *
148 * Allocate memory in one of the heaps provided in heap mask and return
149 * an opaque handle to it.
150 */
151struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
152 size_t align, unsigned int flags);
153
154/**
155 * ion_free - free a handle
156 * @client: the client
157 * @handle: the handle to free
158 *
159 * Free the provided handle.
160 */
161void ion_free(struct ion_client *client, struct ion_handle *handle);
162
163/**
164 * ion_phys - returns the physical address and len of a handle
165 * @client: the client
166 * @handle: the handle
167 * @addr: a pointer to put the address in
168 * @len: a pointer to put the length in
169 *
170 * This function queries the heap for a particular handle to get the
171 * handle's physical address. It't output is only correct if
172 * a heap returns physically contiguous memory -- in other cases
173 * this api should not be implemented -- ion_map_dma should be used
174 * instead. Returns -EINVAL if the handle is invalid. This has
175 * no implications on the reference counting of the handle --
176 * the returned value may not be valid if the caller is not
177 * holding a reference.
178 */
179int ion_phys(struct ion_client *client, struct ion_handle *handle,
180 ion_phys_addr_t *addr, size_t *len);
181
182/**
183 * ion_map_kernel - create mapping for the given handle
184 * @client: the client
185 * @handle: handle to map
186 *
187 * Map the given handle into the kernel and return a kernel address that
188 * can be used to access this address.
189 */
190void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
191
192/**
193 * ion_unmap_kernel() - destroy a kernel mapping for a handle
194 * @client: the client
195 * @handle: handle to unmap
196 */
197void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
198
199/**
200 * ion_map_dma - create a dma mapping for a given handle
201 * @client: the client
202 * @handle: handle to map
203 *
204 * Return an sglist describing the given handle
205 */
206struct scatterlist *ion_map_dma(struct ion_client *client,
207 struct ion_handle *handle);
208
209/**
210 * ion_unmap_dma() - destroy a dma mapping for a handle
211 * @client: the client
212 * @handle: handle to unmap
213 */
214void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle);
215
216/**
217 * ion_share() - given a handle, obtain a buffer to pass to other clients
218 * @client: the client
219 * @handle: the handle to share
220 *
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700221 * Given a handle, return a buffer, which exists in a global name
222 * space, and can be passed to other clients. Should be passed into ion_import
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700223 * to obtain a new handle for this buffer.
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700224 *
225 * NOTE: This function does do not an extra reference. The burden is on the
226 * caller to make sure the buffer doesn't go away while it's being passed to
227 * another client. That is, ion_free should not be called on this handle until
228 * the buffer has been imported into the other client.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700229 */
230struct ion_buffer *ion_share(struct ion_client *client,
231 struct ion_handle *handle);
232
233/**
234 * ion_import() - given an buffer in another client, import it
235 * @client: this blocks client
236 * @buffer: the buffer to import (as obtained from ion_share)
237 *
238 * Given a buffer, add it to the client and return the handle to use to refer
239 * to it further. This is called to share a handle from one kernel client to
240 * another.
241 */
242struct ion_handle *ion_import(struct ion_client *client,
243 struct ion_buffer *buffer);
244
245/**
246 * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it
247 * @client: this blocks client
248 * @fd: the fd
249 *
250 * A helper function for drivers that will be recieving ion buffers shared
251 * with them from userspace. These buffers are represented by a file
252 * descriptor obtained as the return from the ION_IOC_SHARE ioctl.
253 * This function coverts that fd into the underlying buffer, and returns
254 * the handle to use to refer to it further.
255 */
256struct ion_handle *ion_import_fd(struct ion_client *client, int fd);
257#endif /* __KERNEL__ */
258
259/**
260 * DOC: Ion Userspace API
261 *
262 * create a client by opening /dev/ion
263 * most operations handled via following ioctls
264 *
265 */
266
267/**
268 * struct ion_allocation_data - metadata passed from userspace for allocations
269 * @len: size of the allocation
270 * @align: required alignment of the allocation
271 * @flags: flags passed to heap
272 * @handle: pointer that will be populated with a cookie to use to refer
273 * to this allocation
274 *
275 * Provided by userspace as an argument to the ioctl
276 */
277struct ion_allocation_data {
278 size_t len;
279 size_t align;
280 unsigned int flags;
281 struct ion_handle *handle;
282};
283
284/**
285 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
286 * @handle: a handle
287 * @fd: a file descriptor representing that handle
288 *
289 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
290 * the handle returned from ion alloc, and the kernel returns the file
291 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
292 * provides the file descriptor and the kernel returns the handle.
293 */
294struct ion_fd_data {
295 struct ion_handle *handle;
296 int fd;
297};
298
299/**
300 * struct ion_handle_data - a handle passed to/from the kernel
301 * @handle: a handle
302 */
303struct ion_handle_data {
304 struct ion_handle *handle;
305};
306
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700307/**
308 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
309 * @cmd: the custom ioctl function to call
310 * @arg: additional data to pass to the custom ioctl, typically a user
311 * pointer to a predefined structure
312 *
313 * This works just like the regular cmd and arg fields of an ioctl.
314 */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700315struct ion_custom_data {
316 unsigned int cmd;
317 unsigned long arg;
318};
319
320#define ION_IOC_MAGIC 'I'
321
322/**
323 * DOC: ION_IOC_ALLOC - allocate memory
324 *
325 * Takes an ion_allocation_data struct and returns it with the handle field
326 * populated with the opaque handle for the allocation.
327 */
328#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
329 struct ion_allocation_data)
330
331/**
332 * DOC: ION_IOC_FREE - free memory
333 *
334 * Takes an ion_handle_data struct and frees the handle.
335 */
336#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
337
338/**
339 * DOC: ION_IOC_MAP - get a file descriptor to mmap
340 *
341 * Takes an ion_fd_data struct with the handle field populated with a valid
342 * opaque handle. Returns the struct with the fd field set to a file
343 * descriptor open in the current address space. This file descriptor
344 * can then be used as an argument to mmap.
345 */
346#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
347
348/**
349 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
350 *
351 * Takes an ion_fd_data struct with the handle field populated with a valid
352 * opaque handle. Returns the struct with the fd field set to a file
353 * descriptor open in the current address space. This file descriptor
354 * can then be passed to another process. The corresponding opaque handle can
355 * be retrieved via ION_IOC_IMPORT.
356 */
357#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
358
359/**
360 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
361 *
362 * Takes an ion_fd_data struct with the fd field populated with a valid file
363 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
364 * filed set to the corresponding opaque handle.
365 */
366#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int)
367
368/**
369 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
370 *
371 * Takes the argument of the architecture specific ioctl to call and
372 * passes appropriate userdata for that ioctl
373 */
374#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
375
376#endif /* _LINUX_ION_H */