blob: 88ad9a0d2f8c1580f4082ad268583ab316e9c246 [file] [log] [blame]
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001/*
2 * include/linux/ion.h
3 *
4 * Copyright (C) 2011 Google, Inc.
Mitchel Humpherysa75e4eb2012-12-14 16:12:23 -08005 * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07006 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#ifndef _LINUX_ION_H
19#define _LINUX_ION_H
20
Laura Abbottabcb6f72011-10-04 16:26:49 -070021#include <linux/ioctl.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070022#include <linux/types.h>
23
24struct 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
Olav Hauganb5be7992011-11-18 14:29:02 -080030 * carveout heap, allocations are physically
31 * contiguous
Olav Haugan0a852512012-01-09 10:20:55 -080032 * @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
Mitchel Humpherys227a6582012-09-11 15:59:11 -070047/**
48 * heap flags - the lower 16 bits are used by core ion, the upper 16
49 * bits are reserved for use by the heaps themselves.
50 */
51#define ION_FLAG_CACHED 1 /* mappings of this buffer should be
52 cached, ion will do cache
53 maintenance when the buffer is
54 mapped for dma */
Rebecca Schultz Zavin3edb9002012-09-19 23:31:05 -070055#define ION_FLAG_CACHED_NEEDS_SYNC 2 /* mappings of this buffer will created
56 at mmap time, if this is set
57 caches must be managed manually */
Laura Abbotta2e93632011-08-19 13:36:32 -070058
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070059#ifdef __KERNEL__
Laura Abbott65576962011-10-31 12:13:25 -070060#include <linux/err.h>
Laura Abbottcffdff52011-09-23 10:40:19 -070061#include <mach/ion.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070062struct ion_device;
63struct ion_heap;
64struct ion_mapper;
65struct ion_client;
66struct ion_buffer;
67
68/* This should be removed some day when phys_addr_t's are fully
69 plumbed in the kernel, and all instances of ion_phys_addr_t should
70 be converted to phys_addr_t. For the time being many kernel interfaces
71 do not accept phys_addr_t's that would have to */
72#define ion_phys_addr_t unsigned long
73
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070074/**
75 * struct ion_platform_heap - defines a heap in the given platform
76 * @type: type of the heap from ion_heap_type enum
Olav Hauganee0f7802011-12-19 13:28:57 -080077 * @id: unique identifier for heap. When allocating (lower numbers
Olav Hauganb5be7992011-11-18 14:29:02 -080078 * will be allocated from first)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070079 * @name: used for debug purposes
80 * @base: base address of heap in physical memory if applicable
81 * @size: size of the heap in bytes if applicable
Laura Abbottcaafeea2011-12-13 11:43:10 -080082 * @memory_type:Memory type used for the heap
Olav Haugan85c95402012-05-30 17:32:37 -070083 * @has_outer_cache: set to 1 if outer cache is used, 0 otherwise.
Laura Abbottcaafeea2011-12-13 11:43:10 -080084 * @extra_data: Extra data specific to each heap type
Benjamin Gaignard8dff0a62012-06-25 15:30:18 -070085 * @priv: heap private data
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070086 */
87struct ion_platform_heap {
88 enum ion_heap_type type;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070089 unsigned int id;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070090 const char *name;
91 ion_phys_addr_t base;
92 size_t size;
Laura Abbotta2e93632011-08-19 13:36:32 -070093 enum ion_memory_types memory_type;
Olav Haugan85c95402012-05-30 17:32:37 -070094 unsigned int has_outer_cache;
Olav Haugan0703dbf2011-12-19 17:53:38 -080095 void *extra_data;
Benjamin Gaignard8dff0a62012-06-25 15:30:18 -070096 void *priv;
Olav Haugan0703dbf2011-12-19 17:53:38 -080097};
98
Laura Abbottcaafeea2011-12-13 11:43:10 -080099/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700100 * struct ion_platform_data - array of platform heaps passed from board file
Olav Haugan85c95402012-05-30 17:32:37 -0700101 * @has_outer_cache: set to 1 if outer cache is used, 0 otherwise.
Alex Bird27ca6612011-11-01 14:40:06 -0700102 * @nr: number of structures in the array
Alex Bird27ca6612011-11-01 14:40:06 -0700103 * @heaps: array of platform_heap structions
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700104 *
105 * Provided by the board file in the form of platform data to a platform device.
106 */
107struct ion_platform_data {
Olav Haugan85c95402012-05-30 17:32:37 -0700108 unsigned int has_outer_cache;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700109 int nr;
Benjamin Gaignard63d81032012-06-25 15:27:30 -0700110 struct ion_platform_heap *heaps;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700111};
112
Jordan Crouse8cd48322011-10-12 17:05:19 -0600113#ifdef CONFIG_ION
114
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700115/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800116 * ion_reserve() - reserve memory for ion heaps if applicable
117 * @data: platform data specifying starting physical address and
118 * size
119 *
120 * Calls memblock reserve to set aside memory for heaps that are
121 * located at specific memory addresses or of specfic sizes not
122 * managed by the kernel
123 */
124void ion_reserve(struct ion_platform_data *data);
125
126/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700127 * ion_client_create() - allocate a client and returns it
128 * @dev: the global ion device
129 * @heap_mask: mask of heaps this client can allocate from
130 * @name: used for debugging
131 */
132struct ion_client *ion_client_create(struct ion_device *dev,
133 unsigned int heap_mask, const char *name);
134
135/**
136 * ion_client_destroy() - free's a client and all it's handles
137 * @client: the client
138 *
139 * Free the provided client and all it's resources including
140 * any handles it is holding.
141 */
142void ion_client_destroy(struct ion_client *client);
143
144/**
145 * ion_alloc - allocate ion memory
146 * @client: the client
147 * @len: size of the allocation
148 * @align: requested allocation alignment, lots of hardware blocks have
149 * alignment requirements of some kind
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700150 * @heap_mask: mask of heaps to allocate from, if multiple bits are set
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700151 * heaps will be tried in order from lowest to highest order bit
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700152 * @flags: heap flags, the low 16 bits are consumed by ion, the high 16
153 * bits are passed on to the respective heap and can be heap
154 * custom
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700155 *
156 * Allocate memory in one of the heaps provided in heap mask and return
157 * an opaque handle to it.
158 */
159struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700160 size_t align, unsigned int heap_mask,
161 unsigned int flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700162
163/**
164 * ion_free - free a handle
165 * @client: the client
166 * @handle: the handle to free
167 *
168 * Free the provided handle.
169 */
170void ion_free(struct ion_client *client, struct ion_handle *handle);
171
172/**
173 * ion_phys - returns the physical address and len of a handle
174 * @client: the client
175 * @handle: the handle
176 * @addr: a pointer to put the address in
177 * @len: a pointer to put the length in
178 *
179 * This function queries the heap for a particular handle to get the
180 * handle's physical address. It't output is only correct if
181 * a heap returns physically contiguous memory -- in other cases
Laura Abbottb14ed962012-01-30 14:18:08 -0800182 * this api should not be implemented -- ion_sg_table should be used
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700183 * instead. Returns -EINVAL if the handle is invalid. This has
184 * no implications on the reference counting of the handle --
185 * the returned value may not be valid if the caller is not
186 * holding a reference.
187 */
188int ion_phys(struct ion_client *client, struct ion_handle *handle,
189 ion_phys_addr_t *addr, size_t *len);
190
191/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800192 * ion_map_dma - return an sg_table describing a handle
193 * @client: the client
194 * @handle: the handle
195 *
196 * This function returns the sg_table describing
197 * a particular ion handle.
198 */
199struct sg_table *ion_sg_table(struct ion_client *client,
200 struct ion_handle *handle);
201
202/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700203 * ion_map_kernel - create mapping for the given handle
204 * @client: the client
205 * @handle: handle to map
206 *
207 * Map the given handle into the kernel and return a kernel address that
Mitchel Humpherysc4dba0a2012-11-05 14:06:18 -0800208 * can be used to access this address.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700209 */
Mitchel Humpherys911b4b72012-09-12 14:42:50 -0700210void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
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/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800220 * ion_share_dma_buf() - given an ion client, create a dma-buf fd
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700221 * @client: the client
Laura Abbottb14ed962012-01-30 14:18:08 -0800222 * @handle: the handle
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700223 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800224int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700225
226/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800227 * ion_import_dma_buf() - given an dma-buf fd from the ion exporter get handle
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700228 * @client: the client
Laura Abbottb14ed962012-01-30 14:18:08 -0800229 * @fd: the dma-buf fd
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700230 *
Laura Abbottb14ed962012-01-30 14:18:08 -0800231 * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf,
232 * import that fd and return a handle representing it. If a dma-buf from
233 * another exporter is passed in this function will return ERR_PTR(-EINVAL)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700234 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800235struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd);
Laura Abbott273dd8e2011-10-12 14:26:33 -0700236
Jordan Crouse8cd48322011-10-12 17:05:19 -0600237#else
Laura Abbottb14ed962012-01-30 14:18:08 -0800238static inline void ion_reserve(struct ion_platform_data *data)
239{
240
241}
242
Jordan Crouse8cd48322011-10-12 17:05:19 -0600243static inline struct ion_client *ion_client_create(struct ion_device *dev,
244 unsigned int heap_mask, const char *name)
245{
246 return ERR_PTR(-ENODEV);
247}
Laura Abbott273dd8e2011-10-12 14:26:33 -0700248
Jordan Crouse8cd48322011-10-12 17:05:19 -0600249static inline void ion_client_destroy(struct ion_client *client) { }
250
251static inline struct ion_handle *ion_alloc(struct ion_client *client,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700252 size_t len, size_t align,
253 unsigned int heap_mask,
254 unsigned int flags)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600255{
256 return ERR_PTR(-ENODEV);
257}
258
259static inline void ion_free(struct ion_client *client,
260 struct ion_handle *handle) { }
261
262
263static inline int ion_phys(struct ion_client *client,
264 struct ion_handle *handle, ion_phys_addr_t *addr, size_t *len)
265{
266 return -ENODEV;
267}
268
Laura Abbottb14ed962012-01-30 14:18:08 -0800269static inline struct sg_table *ion_sg_table(struct ion_client *client,
270 struct ion_handle *handle)
271{
272 return ERR_PTR(-ENODEV);
273}
274
Jordan Crouse8cd48322011-10-12 17:05:19 -0600275static inline void *ion_map_kernel(struct ion_client *client,
Mitchel Humpherysbaa86922012-11-02 16:35:39 -0700276 struct ion_handle *handle)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600277{
278 return ERR_PTR(-ENODEV);
279}
280
281static inline void ion_unmap_kernel(struct ion_client *client,
282 struct ion_handle *handle) { }
283
Laura Abbottb14ed962012-01-30 14:18:08 -0800284static inline int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600285{
Laura Abbottb14ed962012-01-30 14:18:08 -0800286 return -ENODEV;
Jordan Crouse8cd48322011-10-12 17:05:19 -0600287}
288
Laura Abbottb14ed962012-01-30 14:18:08 -0800289static inline struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600290{
291 return ERR_PTR(-ENODEV);
292}
293
294static inline int ion_handle_get_flags(struct ion_client *client,
295 struct ion_handle *handle, unsigned long *flags)
296{
297 return -ENODEV;
298}
Laura Abbott8c017362011-09-22 20:59:12 -0700299
Jordan Crouse8cd48322011-10-12 17:05:19 -0600300#endif /* CONFIG_ION */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700301#endif /* __KERNEL__ */
302
303/**
304 * DOC: Ion Userspace API
305 *
306 * create a client by opening /dev/ion
307 * most operations handled via following ioctls
308 *
309 */
310
311/**
312 * struct ion_allocation_data - metadata passed from userspace for allocations
313 * @len: size of the allocation
314 * @align: required alignment of the allocation
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700315 * @heap_mask: mask of heaps to allocate from
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700316 * @flags: flags passed to heap
317 * @handle: pointer that will be populated with a cookie to use to refer
318 * to this allocation
319 *
320 * Provided by userspace as an argument to the ioctl
321 */
322struct ion_allocation_data {
323 size_t len;
324 size_t align;
Laura Abbott0eec1512012-08-27 13:14:39 -0700325 unsigned int heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700326 unsigned int flags;
327 struct ion_handle *handle;
328};
329
330/**
331 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
332 * @handle: a handle
333 * @fd: a file descriptor representing that handle
334 *
335 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
336 * the handle returned from ion alloc, and the kernel returns the file
337 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
338 * provides the file descriptor and the kernel returns the handle.
339 */
340struct ion_fd_data {
341 struct ion_handle *handle;
342 int fd;
343};
344
345/**
346 * struct ion_handle_data - a handle passed to/from the kernel
347 * @handle: a handle
348 */
349struct ion_handle_data {
350 struct ion_handle *handle;
351};
352
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700353/**
354 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
355 * @cmd: the custom ioctl function to call
356 * @arg: additional data to pass to the custom ioctl, typically a user
357 * pointer to a predefined structure
358 *
359 * This works just like the regular cmd and arg fields of an ioctl.
360 */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700361struct ion_custom_data {
362 unsigned int cmd;
363 unsigned long arg;
364};
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700365#define ION_IOC_MAGIC 'I'
366
367/**
368 * DOC: ION_IOC_ALLOC - allocate memory
369 *
370 * Takes an ion_allocation_data struct and returns it with the handle field
371 * populated with the opaque handle for the allocation.
372 */
373#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
374 struct ion_allocation_data)
375
376/**
377 * DOC: ION_IOC_FREE - free memory
378 *
379 * Takes an ion_handle_data struct and frees the handle.
380 */
381#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
382
383/**
384 * DOC: ION_IOC_MAP - get a file descriptor to mmap
385 *
386 * Takes an ion_fd_data struct with the handle field populated with a valid
387 * opaque handle. Returns the struct with the fd field set to a file
388 * descriptor open in the current address space. This file descriptor
389 * can then be used as an argument to mmap.
390 */
391#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
392
393/**
394 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
395 *
396 * Takes an ion_fd_data struct with the handle field populated with a valid
397 * opaque handle. Returns the struct with the fd field set to a file
398 * descriptor open in the current address space. This file descriptor
399 * can then be passed to another process. The corresponding opaque handle can
400 * be retrieved via ION_IOC_IMPORT.
401 */
402#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
403
404/**
405 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
406 *
407 * Takes an ion_fd_data struct with the fd field populated with a valid file
408 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
409 * filed set to the corresponding opaque handle.
410 */
Laura Abbott0eec1512012-08-27 13:14:39 -0700411#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700412
413/**
Rebecca Schultz Zavinf4419222012-06-26 13:17:34 -0700414 * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory
415 *
416 * Deprecated in favor of using the dma_buf api's correctly (syncing
417 * will happend automatically when the buffer is mapped to a device).
418 * If necessary should be used after touching a cached buffer from the cpu,
419 * this will make the buffer in memory coherent.
420 */
421#define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
422
423/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700424 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
425 *
426 * Takes the argument of the architecture specific ioctl to call and
427 * passes appropriate userdata for that ioctl
428 */
429#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
430
Laura Abbottabcb6f72011-10-04 16:26:49 -0700431
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700432#endif /* _LINUX_ION_H */