blob: 9f220f8fe1e61c13133a868b90e2950106f0b6eb [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>
22
Laura Abbottabcb6f72011-10-04 16:26:49 -070023
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070024struct ion_handle;
25/**
26 * enum ion_heap_types - list of all possible types of heaps
Iliyan Malchevf22301562011-07-06 16:53:21 -070027 * @ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc
28 * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc
29 * @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved
30 * carveout heap, allocations are physically
31 * contiguous
32 * @ION_HEAP_END: helper for iterating over heaps
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070033 */
34enum ion_heap_type {
35 ION_HEAP_TYPE_SYSTEM,
36 ION_HEAP_TYPE_SYSTEM_CONTIG,
37 ION_HEAP_TYPE_CARVEOUT,
38 ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
39 are at the end of this enum */
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070040 ION_NUM_HEAPS,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070041};
42
Iliyan Malchevf22301562011-07-06 16:53:21 -070043#define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
44#define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
45#define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070046
Laura Abbotta2e93632011-08-19 13:36:32 -070047
48/**
49 * These are the only ids that should be used for Ion heap ids.
50 * The ids listed are the order in which allocation will be attempted
51 * if specified. Don't swap the order of heap ids unless you know what
52 * you are doing!
53 */
54
55enum ion_heap_ids {
56 ION_HEAP_SYSTEM_ID,
57 ION_HEAP_SYSTEM_CONTIG_ID,
58 ION_HEAP_EBI_ID,
59 ION_HEAP_SMI_ID,
Laura Abbott2d1760b2011-09-29 21:31:24 -070060 ION_HEAP_ADSP_ID,
61 ION_HEAP_AUDIO_ID,
Laura Abbotta2e93632011-08-19 13:36:32 -070062};
63
64#define ION_KMALLOC_HEAP_NAME "kmalloc"
65#define ION_VMALLOC_HEAP_NAME "vmalloc"
66#define ION_EBI1_HEAP_NAME "EBI1"
Laura Abbott2d1760b2011-09-29 21:31:24 -070067#define ION_ADSP_HEAP_NAME "adsp"
Laura Abbotta2e93632011-08-19 13:36:32 -070068
Laura Abbott894fd582011-08-19 13:33:56 -070069#define CACHED 1
70#define UNCACHED 0
71
72#define ION_CACHE_SHIFT 0
73
74#define ION_SET_CACHE(__cache) ((__cache) << ION_CACHE_SHIFT)
75
Laura Abbott35412032011-09-29 09:50:06 -070076#define ION_IS_CACHED(__flags) ((__flags) & (1 << ION_CACHE_SHIFT))
77
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070078#ifdef __KERNEL__
Laura Abbott65576962011-10-31 12:13:25 -070079#include <linux/err.h>
Laura Abbottcffdff52011-09-23 10:40:19 -070080#include <mach/ion.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070081struct ion_device;
82struct ion_heap;
83struct ion_mapper;
84struct ion_client;
85struct ion_buffer;
86
87/* This should be removed some day when phys_addr_t's are fully
88 plumbed in the kernel, and all instances of ion_phys_addr_t should
89 be converted to phys_addr_t. For the time being many kernel interfaces
90 do not accept phys_addr_t's that would have to */
91#define ion_phys_addr_t unsigned long
92
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070093/**
94 * struct ion_platform_heap - defines a heap in the given platform
95 * @type: type of the heap from ion_heap_type enum
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070096 * @id: unique identifier for heap. When allocating (lower numbers
97 * will be allocated from first)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070098 * @name: used for debug purposes
99 * @base: base address of heap in physical memory if applicable
100 * @size: size of the heap in bytes if applicable
101 *
102 * Provided by the board file.
103 */
104struct ion_platform_heap {
105 enum ion_heap_type type;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700106 unsigned int id;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700107 const char *name;
108 ion_phys_addr_t base;
109 size_t size;
Laura Abbotta2e93632011-08-19 13:36:32 -0700110 enum ion_memory_types memory_type;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700111};
112
113/**
114 * struct ion_platform_data - array of platform heaps passed from board file
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700115 * @nr: number of structures in the array
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700116 * @heaps: array of platform_heap structions
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700117 *
118 * Provided by the board file in the form of platform data to a platform device.
119 */
120struct ion_platform_data {
121 int nr;
122 struct ion_platform_heap heaps[];
123};
124
Jordan Crouse8cd48322011-10-12 17:05:19 -0600125#ifdef CONFIG_ION
126
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700127/**
128 * ion_client_create() - allocate a client and returns it
129 * @dev: the global ion device
130 * @heap_mask: mask of heaps this client can allocate from
131 * @name: used for debugging
132 */
133struct ion_client *ion_client_create(struct ion_device *dev,
134 unsigned int heap_mask, const char *name);
135
136/**
Laura Abbott302911d2011-08-15 17:12:57 -0700137 * msm_ion_client_create - allocate a client using the ion_device specified in
138 * drivers/gpu/ion/msm/msm_ion.c
139 *
140 * heap_mask and name are the same as ion_client_create, return values
141 * are the same as ion_client_create.
142 */
143
144struct ion_client *msm_ion_client_create(unsigned int heap_mask,
145 const char *name);
146
147/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700148 * ion_client_destroy() - free's a client and all it's handles
149 * @client: the client
150 *
151 * Free the provided client and all it's resources including
152 * any handles it is holding.
153 */
154void ion_client_destroy(struct ion_client *client);
155
156/**
157 * ion_alloc - allocate ion memory
158 * @client: the client
159 * @len: size of the allocation
160 * @align: requested allocation alignment, lots of hardware blocks have
161 * alignment requirements of some kind
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700162 * @flags: mask of heaps to allocate from, if multiple bits are set
163 * heaps will be tried in order from lowest to highest order bit
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700164 *
165 * Allocate memory in one of the heaps provided in heap mask and return
166 * an opaque handle to it.
167 */
168struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
169 size_t align, unsigned int flags);
170
171/**
172 * ion_free - free a handle
173 * @client: the client
174 * @handle: the handle to free
175 *
176 * Free the provided handle.
177 */
178void ion_free(struct ion_client *client, struct ion_handle *handle);
179
180/**
181 * ion_phys - returns the physical address and len of a handle
182 * @client: the client
183 * @handle: the handle
184 * @addr: a pointer to put the address in
185 * @len: a pointer to put the length in
186 *
187 * This function queries the heap for a particular handle to get the
188 * handle's physical address. It't output is only correct if
189 * a heap returns physically contiguous memory -- in other cases
190 * this api should not be implemented -- ion_map_dma should be used
191 * instead. Returns -EINVAL if the handle is invalid. This has
192 * no implications on the reference counting of the handle --
193 * the returned value may not be valid if the caller is not
194 * holding a reference.
195 */
196int ion_phys(struct ion_client *client, struct ion_handle *handle,
197 ion_phys_addr_t *addr, size_t *len);
198
199/**
200 * ion_map_kernel - create mapping for the given handle
201 * @client: the client
202 * @handle: handle to map
Laura Abbott894fd582011-08-19 13:33:56 -0700203 * @flags: flags for this mapping
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700204 *
205 * Map the given handle into the kernel and return a kernel address that
Laura Abbott894fd582011-08-19 13:33:56 -0700206 * can be used to access this address. If no flags are specified, this
207 * will return a non-secure uncached mapping.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700208 */
Laura Abbott894fd582011-08-19 13:33:56 -0700209void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle,
210 unsigned long flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700211
212/**
213 * ion_unmap_kernel() - destroy a kernel mapping for a handle
214 * @client: the client
215 * @handle: handle to unmap
216 */
217void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
218
219/**
220 * ion_map_dma - create a dma mapping for a given handle
221 * @client: the client
222 * @handle: handle to map
223 *
224 * Return an sglist describing the given handle
225 */
226struct scatterlist *ion_map_dma(struct ion_client *client,
Laura Abbott894fd582011-08-19 13:33:56 -0700227 struct ion_handle *handle,
228 unsigned long flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700229
230/**
231 * ion_unmap_dma() - destroy a dma mapping for a handle
232 * @client: the client
233 * @handle: handle to unmap
234 */
235void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle);
236
237/**
238 * ion_share() - given a handle, obtain a buffer to pass to other clients
239 * @client: the client
240 * @handle: the handle to share
241 *
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700242 * Given a handle, return a buffer, which exists in a global name
243 * space, and can be passed to other clients. Should be passed into ion_import
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700244 * to obtain a new handle for this buffer.
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700245 *
246 * NOTE: This function does do not an extra reference. The burden is on the
247 * caller to make sure the buffer doesn't go away while it's being passed to
248 * another client. That is, ion_free should not be called on this handle until
249 * the buffer has been imported into the other client.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700250 */
251struct ion_buffer *ion_share(struct ion_client *client,
252 struct ion_handle *handle);
253
254/**
255 * ion_import() - given an buffer in another client, import it
256 * @client: this blocks client
257 * @buffer: the buffer to import (as obtained from ion_share)
258 *
259 * Given a buffer, add it to the client and return the handle to use to refer
260 * to it further. This is called to share a handle from one kernel client to
261 * another.
262 */
263struct ion_handle *ion_import(struct ion_client *client,
264 struct ion_buffer *buffer);
265
266/**
267 * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it
268 * @client: this blocks client
269 * @fd: the fd
270 *
271 * A helper function for drivers that will be recieving ion buffers shared
272 * with them from userspace. These buffers are represented by a file
273 * descriptor obtained as the return from the ION_IOC_SHARE ioctl.
274 * This function coverts that fd into the underlying buffer, and returns
275 * the handle to use to refer to it further.
276 */
277struct ion_handle *ion_import_fd(struct ion_client *client, int fd);
Laura Abbott273dd8e2011-10-12 14:26:33 -0700278
Laura Abbott273dd8e2011-10-12 14:26:33 -0700279/**
280 * ion_handle_get_flags - get the flags for a given handle
281 *
282 * @client - client who allocated the handle
283 * @handle - handle to get the flags
284 * @flags - pointer to store the flags
285 *
286 * Gets the current flags for a handle. These flags indicate various options
287 * of the buffer (caching, security, etc.)
288 */
289int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
290 unsigned long *flags);
291
Jordan Crouse8cd48322011-10-12 17:05:19 -0600292#else
293static inline struct ion_client *ion_client_create(struct ion_device *dev,
294 unsigned int heap_mask, const char *name)
295{
296 return ERR_PTR(-ENODEV);
297}
Laura Abbott273dd8e2011-10-12 14:26:33 -0700298
Jordan Crouse8cd48322011-10-12 17:05:19 -0600299static inline struct ion_client *msm_ion_client_create(unsigned int heap_mask,
300 const char *name)
301{
302 return ERR_PTR(-ENODEV);
303}
304
305static inline void ion_client_destroy(struct ion_client *client) { }
306
307static inline struct ion_handle *ion_alloc(struct ion_client *client,
308 size_t len, size_t align, unsigned int flags)
309{
310 return ERR_PTR(-ENODEV);
311}
312
313static inline void ion_free(struct ion_client *client,
314 struct ion_handle *handle) { }
315
316
317static inline int ion_phys(struct ion_client *client,
318 struct ion_handle *handle, ion_phys_addr_t *addr, size_t *len)
319{
320 return -ENODEV;
321}
322
323static inline void *ion_map_kernel(struct ion_client *client,
324 struct ion_handle *handle, unsigned long flags)
325{
326 return ERR_PTR(-ENODEV);
327}
328
329static inline void ion_unmap_kernel(struct ion_client *client,
330 struct ion_handle *handle) { }
331
332static inline struct scatterlist *ion_map_dma(struct ion_client *client,
333 struct ion_handle *handle, unsigned long flags)
334{
335 return ERR_PTR(-ENODEV);
336}
337
338static inline void ion_unmap_dma(struct ion_client *client,
339 struct ion_handle *handle) { }
340
341static inline struct ion_buffer *ion_share(struct ion_client *client,
342 struct ion_handle *handle)
343{
344 return ERR_PTR(-ENODEV);
345}
346
347static inline struct ion_handle *ion_import(struct ion_client *client,
348 struct ion_buffer *buffer)
349{
350 return ERR_PTR(-ENODEV);
351}
352
353static inline struct ion_handle *ion_import_fd(struct ion_client *client,
354 int fd)
355{
356 return ERR_PTR(-ENODEV);
357}
358
359static inline int ion_handle_get_flags(struct ion_client *client,
360 struct ion_handle *handle, unsigned long *flags)
361{
362 return -ENODEV;
363}
364#endif /* CONFIG_ION */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700365#endif /* __KERNEL__ */
366
367/**
368 * DOC: Ion Userspace API
369 *
370 * create a client by opening /dev/ion
371 * most operations handled via following ioctls
372 *
373 */
374
375/**
376 * struct ion_allocation_data - metadata passed from userspace for allocations
377 * @len: size of the allocation
378 * @align: required alignment of the allocation
379 * @flags: flags passed to heap
380 * @handle: pointer that will be populated with a cookie to use to refer
381 * to this allocation
382 *
383 * Provided by userspace as an argument to the ioctl
384 */
385struct ion_allocation_data {
386 size_t len;
387 size_t align;
388 unsigned int flags;
389 struct ion_handle *handle;
390};
391
392/**
393 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
394 * @handle: a handle
395 * @fd: a file descriptor representing that handle
396 *
397 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
398 * the handle returned from ion alloc, and the kernel returns the file
399 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
400 * provides the file descriptor and the kernel returns the handle.
401 */
402struct ion_fd_data {
403 struct ion_handle *handle;
404 int fd;
405};
406
407/**
408 * struct ion_handle_data - a handle passed to/from the kernel
409 * @handle: a handle
410 */
411struct ion_handle_data {
412 struct ion_handle *handle;
413};
414
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700415/**
416 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
417 * @cmd: the custom ioctl function to call
418 * @arg: additional data to pass to the custom ioctl, typically a user
419 * pointer to a predefined structure
420 *
421 * This works just like the regular cmd and arg fields of an ioctl.
422 */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700423struct ion_custom_data {
424 unsigned int cmd;
425 unsigned long arg;
426};
427
Laura Abbottabcb6f72011-10-04 16:26:49 -0700428
429/* struct ion_flush_data - data passed to ion for flushing caches
430 *
431 * @handle: handle with data to flush
432 * @vaddr: userspace virtual address mapped with mmap
433 * @offset: offset into the handle to flush
434 * @length: length of handle to flush
435 *
436 * Performs cache operations on the handle. If p is the start address
437 * of the handle, p + offset through p + offset + length will have
438 * the cache operations performed
439 */
440struct ion_flush_data {
441 struct ion_handle *handle;
442 void *vaddr;
443 unsigned int offset;
444 unsigned int length;
445};
Laura Abbott273dd8e2011-10-12 14:26:33 -0700446
447/* struct ion_flag_data - information about flags for this buffer
448 *
449 * @handle: handle to get flags from
450 * @flags: flags of this handle
451 *
452 * Takes handle as an input and outputs the flags from the handle
453 * in the flag field.
454 */
455struct ion_flag_data {
456 struct ion_handle *handle;
457 unsigned long flags;
458};
459
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700460#define ION_IOC_MAGIC 'I'
461
462/**
463 * DOC: ION_IOC_ALLOC - allocate memory
464 *
465 * Takes an ion_allocation_data struct and returns it with the handle field
466 * populated with the opaque handle for the allocation.
467 */
468#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
469 struct ion_allocation_data)
470
471/**
472 * DOC: ION_IOC_FREE - free memory
473 *
474 * Takes an ion_handle_data struct and frees the handle.
475 */
476#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
477
478/**
479 * DOC: ION_IOC_MAP - get a file descriptor to mmap
480 *
481 * Takes an ion_fd_data struct with the handle field populated with a valid
482 * opaque handle. Returns the struct with the fd field set to a file
483 * descriptor open in the current address space. This file descriptor
484 * can then be used as an argument to mmap.
485 */
486#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
487
488/**
489 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
490 *
491 * Takes an ion_fd_data struct with the handle field populated with a valid
492 * opaque handle. Returns the struct with the fd field set to a file
493 * descriptor open in the current address space. This file descriptor
494 * can then be passed to another process. The corresponding opaque handle can
495 * be retrieved via ION_IOC_IMPORT.
496 */
497#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
498
499/**
500 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
501 *
502 * Takes an ion_fd_data struct with the fd field populated with a valid file
503 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
504 * filed set to the corresponding opaque handle.
505 */
506#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int)
507
508/**
509 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
510 *
511 * Takes the argument of the architecture specific ioctl to call and
512 * passes appropriate userdata for that ioctl
513 */
514#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
515
Laura Abbottabcb6f72011-10-04 16:26:49 -0700516
517/**
518 * DOC: ION_IOC_CLEAN_CACHES - clean the caches
519 *
520 * Clean the caches of the handle specified.
521 */
522#define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_MAGIC, 7, \
523 struct ion_flush_data)
524/**
525 * DOC: ION_MSM_IOC_INV_CACHES - invalidate the caches
526 *
527 * Invalidate the caches of the handle specified.
528 */
529#define ION_IOC_INV_CACHES _IOWR(ION_IOC_MAGIC, 8, \
530 struct ion_flush_data)
531/**
532 * DOC: ION_MSM_IOC_CLEAN_CACHES - clean and invalidate the caches
533 *
534 * Clean and invalidate the caches of the handle specified.
535 */
536#define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_MAGIC, 9, \
537 struct ion_flush_data)
Laura Abbott273dd8e2011-10-12 14:26:33 -0700538
539/**
540 * DOC: ION_IOC_GET_FLAGS - get the flags of the handle
541 *
542 * Gets the flags of the current handle which indicate cachability,
543 * secure state etc.
544 */
545#define ION_IOC_GET_FLAGS _IOWR(ION_IOC_MAGIC, 10, \
546 struct ion_flag_data)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700547#endif /* _LINUX_ION_H */