blob: df44376ff62509b79585a0daf46c7dd02ceeabd7 [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
Laura Abbottabcb6f72011-10-04 16:26:49 -070020#include <linux/ioctl.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070021#include <linux/types.h>
Laura Abbotta2e93632011-08-19 13:36:32 -070022#include <mach/ion.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070023
Laura Abbottabcb6f72011-10-04 16:26:49 -070024
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070025struct ion_handle;
26/**
27 * enum ion_heap_types - list of all possible types of heaps
Iliyan Malchevf22301562011-07-06 16:53:21 -070028 * @ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc
29 * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc
30 * @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved
31 * carveout heap, allocations are physically
32 * contiguous
33 * @ION_HEAP_END: helper for iterating over heaps
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070034 */
35enum ion_heap_type {
36 ION_HEAP_TYPE_SYSTEM,
37 ION_HEAP_TYPE_SYSTEM_CONTIG,
38 ION_HEAP_TYPE_CARVEOUT,
39 ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
40 are at the end of this enum */
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070041 ION_NUM_HEAPS,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070042};
43
Iliyan Malchevf22301562011-07-06 16:53:21 -070044#define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
45#define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
46#define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070047
Laura Abbotta2e93632011-08-19 13:36:32 -070048
49/**
50 * These are the only ids that should be used for Ion heap ids.
51 * The ids listed are the order in which allocation will be attempted
52 * if specified. Don't swap the order of heap ids unless you know what
53 * you are doing!
54 */
55
56enum ion_heap_ids {
57 ION_HEAP_SYSTEM_ID,
58 ION_HEAP_SYSTEM_CONTIG_ID,
59 ION_HEAP_EBI_ID,
60 ION_HEAP_SMI_ID,
61};
62
63#define ION_KMALLOC_HEAP_NAME "kmalloc"
64#define ION_VMALLOC_HEAP_NAME "vmalloc"
65#define ION_EBI1_HEAP_NAME "EBI1"
66
Laura Abbott894fd582011-08-19 13:33:56 -070067#define CACHED 1
68#define UNCACHED 0
69
70#define ION_CACHE_SHIFT 0
71
72#define ION_SET_CACHE(__cache) ((__cache) << ION_CACHE_SHIFT)
73
Laura Abbott35412032011-09-29 09:50:06 -070074#define ION_IS_CACHED(__flags) ((__flags) & (1 << ION_CACHE_SHIFT))
75
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070076#ifdef __KERNEL__
77struct ion_device;
78struct ion_heap;
79struct ion_mapper;
80struct ion_client;
81struct ion_buffer;
82
83/* This should be removed some day when phys_addr_t's are fully
84 plumbed in the kernel, and all instances of ion_phys_addr_t should
85 be converted to phys_addr_t. For the time being many kernel interfaces
86 do not accept phys_addr_t's that would have to */
87#define ion_phys_addr_t unsigned long
88
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070089/**
90 * struct ion_platform_heap - defines a heap in the given platform
91 * @type: type of the heap from ion_heap_type enum
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070092 * @id: unique identifier for heap. When allocating (lower numbers
93 * will be allocated from first)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070094 * @name: used for debug purposes
95 * @base: base address of heap in physical memory if applicable
96 * @size: size of the heap in bytes if applicable
97 *
98 * Provided by the board file.
99 */
100struct ion_platform_heap {
101 enum ion_heap_type type;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700102 unsigned int id;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700103 const char *name;
104 ion_phys_addr_t base;
105 size_t size;
Laura Abbotta2e93632011-08-19 13:36:32 -0700106 enum ion_memory_types memory_type;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700107};
108
109/**
110 * struct ion_platform_data - array of platform heaps passed from board file
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700111 * @nr: number of structures in the array
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700112 * @heaps: array of platform_heap structions
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700113 *
114 * Provided by the board file in the form of platform data to a platform device.
115 */
116struct ion_platform_data {
117 int nr;
118 struct ion_platform_heap heaps[];
119};
120
121/**
122 * ion_client_create() - allocate a client and returns it
123 * @dev: the global ion device
124 * @heap_mask: mask of heaps this client can allocate from
125 * @name: used for debugging
126 */
127struct ion_client *ion_client_create(struct ion_device *dev,
128 unsigned int heap_mask, const char *name);
129
130/**
Laura Abbott302911d2011-08-15 17:12:57 -0700131 * msm_ion_client_create - allocate a client using the ion_device specified in
132 * drivers/gpu/ion/msm/msm_ion.c
133 *
134 * heap_mask and name are the same as ion_client_create, return values
135 * are the same as ion_client_create.
136 */
137
138struct ion_client *msm_ion_client_create(unsigned int heap_mask,
139 const char *name);
140
141/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700142 * ion_client_destroy() - free's a client and all it's handles
143 * @client: the client
144 *
145 * Free the provided client and all it's resources including
146 * any handles it is holding.
147 */
148void ion_client_destroy(struct ion_client *client);
149
150/**
151 * ion_alloc - allocate ion memory
152 * @client: the client
153 * @len: size of the allocation
154 * @align: requested allocation alignment, lots of hardware blocks have
155 * alignment requirements of some kind
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700156 * @flags: mask of heaps to allocate from, if multiple bits are set
157 * heaps will be tried in order from lowest to highest order bit
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700158 *
159 * Allocate memory in one of the heaps provided in heap mask and return
160 * an opaque handle to it.
161 */
162struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
163 size_t align, unsigned int flags);
164
165/**
166 * ion_free - free a handle
167 * @client: the client
168 * @handle: the handle to free
169 *
170 * Free the provided handle.
171 */
172void ion_free(struct ion_client *client, struct ion_handle *handle);
173
174/**
175 * ion_phys - returns the physical address and len of a handle
176 * @client: the client
177 * @handle: the handle
178 * @addr: a pointer to put the address in
179 * @len: a pointer to put the length in
180 *
181 * This function queries the heap for a particular handle to get the
182 * handle's physical address. It't output is only correct if
183 * a heap returns physically contiguous memory -- in other cases
184 * this api should not be implemented -- ion_map_dma should be used
185 * instead. Returns -EINVAL if the handle is invalid. This has
186 * no implications on the reference counting of the handle --
187 * the returned value may not be valid if the caller is not
188 * holding a reference.
189 */
190int ion_phys(struct ion_client *client, struct ion_handle *handle,
191 ion_phys_addr_t *addr, size_t *len);
192
193/**
194 * ion_map_kernel - create mapping for the given handle
195 * @client: the client
196 * @handle: handle to map
Laura Abbott894fd582011-08-19 13:33:56 -0700197 * @flags: flags for this mapping
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700198 *
199 * Map the given handle into the kernel and return a kernel address that
Laura Abbott894fd582011-08-19 13:33:56 -0700200 * can be used to access this address. If no flags are specified, this
201 * will return a non-secure uncached mapping.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700202 */
Laura Abbott894fd582011-08-19 13:33:56 -0700203void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle,
204 unsigned long flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700205
206/**
207 * ion_unmap_kernel() - destroy a kernel mapping for a handle
208 * @client: the client
209 * @handle: handle to unmap
210 */
211void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
212
213/**
214 * ion_map_dma - create a dma mapping for a given handle
215 * @client: the client
216 * @handle: handle to map
217 *
218 * Return an sglist describing the given handle
219 */
220struct scatterlist *ion_map_dma(struct ion_client *client,
Laura Abbott894fd582011-08-19 13:33:56 -0700221 struct ion_handle *handle,
222 unsigned long flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700223
224/**
225 * ion_unmap_dma() - destroy a dma mapping for a handle
226 * @client: the client
227 * @handle: handle to unmap
228 */
229void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle);
230
231/**
232 * ion_share() - given a handle, obtain a buffer to pass to other clients
233 * @client: the client
234 * @handle: the handle to share
235 *
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700236 * Given a handle, return a buffer, which exists in a global name
237 * space, and can be passed to other clients. Should be passed into ion_import
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700238 * to obtain a new handle for this buffer.
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700239 *
240 * NOTE: This function does do not an extra reference. The burden is on the
241 * caller to make sure the buffer doesn't go away while it's being passed to
242 * another client. That is, ion_free should not be called on this handle until
243 * the buffer has been imported into the other client.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700244 */
245struct ion_buffer *ion_share(struct ion_client *client,
246 struct ion_handle *handle);
247
248/**
249 * ion_import() - given an buffer in another client, import it
250 * @client: this blocks client
251 * @buffer: the buffer to import (as obtained from ion_share)
252 *
253 * Given a buffer, add it to the client and return the handle to use to refer
254 * to it further. This is called to share a handle from one kernel client to
255 * another.
256 */
257struct ion_handle *ion_import(struct ion_client *client,
258 struct ion_buffer *buffer);
259
260/**
261 * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it
262 * @client: this blocks client
263 * @fd: the fd
264 *
265 * A helper function for drivers that will be recieving ion buffers shared
266 * with them from userspace. These buffers are represented by a file
267 * descriptor obtained as the return from the ION_IOC_SHARE ioctl.
268 * This function coverts that fd into the underlying buffer, and returns
269 * the handle to use to refer to it further.
270 */
271struct ion_handle *ion_import_fd(struct ion_client *client, int fd);
272#endif /* __KERNEL__ */
273
274/**
275 * DOC: Ion Userspace API
276 *
277 * create a client by opening /dev/ion
278 * most operations handled via following ioctls
279 *
280 */
281
282/**
283 * struct ion_allocation_data - metadata passed from userspace for allocations
284 * @len: size of the allocation
285 * @align: required alignment of the allocation
286 * @flags: flags passed to heap
287 * @handle: pointer that will be populated with a cookie to use to refer
288 * to this allocation
289 *
290 * Provided by userspace as an argument to the ioctl
291 */
292struct ion_allocation_data {
293 size_t len;
294 size_t align;
295 unsigned int flags;
296 struct ion_handle *handle;
297};
298
299/**
300 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
301 * @handle: a handle
302 * @fd: a file descriptor representing that handle
303 *
304 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
305 * the handle returned from ion alloc, and the kernel returns the file
306 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
307 * provides the file descriptor and the kernel returns the handle.
308 */
309struct ion_fd_data {
310 struct ion_handle *handle;
311 int fd;
312};
313
314/**
315 * struct ion_handle_data - a handle passed to/from the kernel
316 * @handle: a handle
317 */
318struct ion_handle_data {
319 struct ion_handle *handle;
320};
321
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700322/**
323 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
324 * @cmd: the custom ioctl function to call
325 * @arg: additional data to pass to the custom ioctl, typically a user
326 * pointer to a predefined structure
327 *
328 * This works just like the regular cmd and arg fields of an ioctl.
329 */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700330struct ion_custom_data {
331 unsigned int cmd;
332 unsigned long arg;
333};
334
Laura Abbottabcb6f72011-10-04 16:26:49 -0700335
336/* struct ion_flush_data - data passed to ion for flushing caches
337 *
338 * @handle: handle with data to flush
339 * @vaddr: userspace virtual address mapped with mmap
340 * @offset: offset into the handle to flush
341 * @length: length of handle to flush
342 *
343 * Performs cache operations on the handle. If p is the start address
344 * of the handle, p + offset through p + offset + length will have
345 * the cache operations performed
346 */
347struct ion_flush_data {
348 struct ion_handle *handle;
349 void *vaddr;
350 unsigned int offset;
351 unsigned int length;
352};
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700353#define ION_IOC_MAGIC 'I'
354
355/**
356 * DOC: ION_IOC_ALLOC - allocate memory
357 *
358 * Takes an ion_allocation_data struct and returns it with the handle field
359 * populated with the opaque handle for the allocation.
360 */
361#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
362 struct ion_allocation_data)
363
364/**
365 * DOC: ION_IOC_FREE - free memory
366 *
367 * Takes an ion_handle_data struct and frees the handle.
368 */
369#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
370
371/**
372 * DOC: ION_IOC_MAP - get a file descriptor to mmap
373 *
374 * Takes an ion_fd_data struct with the handle field populated with a valid
375 * opaque handle. Returns the struct with the fd field set to a file
376 * descriptor open in the current address space. This file descriptor
377 * can then be used as an argument to mmap.
378 */
379#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
380
381/**
382 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
383 *
384 * Takes an ion_fd_data struct with the handle field populated with a valid
385 * opaque handle. Returns the struct with the fd field set to a file
386 * descriptor open in the current address space. This file descriptor
387 * can then be passed to another process. The corresponding opaque handle can
388 * be retrieved via ION_IOC_IMPORT.
389 */
390#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
391
392/**
393 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
394 *
395 * Takes an ion_fd_data struct with the fd field populated with a valid file
396 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
397 * filed set to the corresponding opaque handle.
398 */
399#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int)
400
401/**
402 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
403 *
404 * Takes the argument of the architecture specific ioctl to call and
405 * passes appropriate userdata for that ioctl
406 */
407#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
408
Laura Abbottabcb6f72011-10-04 16:26:49 -0700409
410/**
411 * DOC: ION_IOC_CLEAN_CACHES - clean the caches
412 *
413 * Clean the caches of the handle specified.
414 */
415#define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_MAGIC, 7, \
416 struct ion_flush_data)
417/**
418 * DOC: ION_MSM_IOC_INV_CACHES - invalidate the caches
419 *
420 * Invalidate the caches of the handle specified.
421 */
422#define ION_IOC_INV_CACHES _IOWR(ION_IOC_MAGIC, 8, \
423 struct ion_flush_data)
424/**
425 * DOC: ION_MSM_IOC_CLEAN_CACHES - clean and invalidate the caches
426 *
427 * Clean and invalidate the caches of the handle specified.
428 */
429#define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_MAGIC, 9, \
430 struct ion_flush_data)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700431#endif /* _LINUX_ION_H */