blob: 15c11dffe8758480e894d33640062587952fb425 [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,
Rebecca Schultz Zavind2ce6f82012-11-15 10:52:45 -080038 ION_HEAP_TYPE_CHUNK,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070039 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
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -080048#define ION_NUM_HEAP_IDS sizeof(unsigned int) * 8
49
Mitchel Humpherys227a6582012-09-11 15:59:11 -070050/**
51 * heap flags - the lower 16 bits are used by core ion, the upper 16
52 * bits are reserved for use by the heaps themselves.
53 */
54#define ION_FLAG_CACHED 1 /* mappings of this buffer should be
55 cached, ion will do cache
56 maintenance when the buffer is
57 mapped for dma */
Rebecca Schultz Zavin3edb9002012-09-19 23:31:05 -070058#define ION_FLAG_CACHED_NEEDS_SYNC 2 /* mappings of this buffer will created
59 at mmap time, if this is set
60 caches must be managed manually */
Laura Abbotta2e93632011-08-19 13:36:32 -070061
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070062#ifdef __KERNEL__
Laura Abbott65576962011-10-31 12:13:25 -070063#include <linux/err.h>
Laura Abbottcffdff52011-09-23 10:40:19 -070064#include <mach/ion.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070065struct ion_device;
66struct ion_heap;
67struct ion_mapper;
68struct ion_client;
69struct ion_buffer;
70
71/* This should be removed some day when phys_addr_t's are fully
72 plumbed in the kernel, and all instances of ion_phys_addr_t should
73 be converted to phys_addr_t. For the time being many kernel interfaces
74 do not accept phys_addr_t's that would have to */
75#define ion_phys_addr_t unsigned long
76
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070077/**
78 * struct ion_platform_heap - defines a heap in the given platform
79 * @type: type of the heap from ion_heap_type enum
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -080080 * @id: unique identifier for heap. When allocating higher numbers
81 * will be allocated from first. At allocation these are passed
82 * as a bit mask and therefore can not exceed ION_NUM_HEAP_IDS.
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
Laura Abbottcaafeea2011-12-13 11:43:10 -080086 * @memory_type:Memory type used for the heap
Olav Haugan85c95402012-05-30 17:32:37 -070087 * @has_outer_cache: set to 1 if outer cache is used, 0 otherwise.
Laura Abbottcaafeea2011-12-13 11:43:10 -080088 * @extra_data: Extra data specific to each heap type
Benjamin Gaignard8dff0a62012-06-25 15:30:18 -070089 * @priv: heap private data
Rebecca Schultz Zavind2ce6f82012-11-15 10:52:45 -080090 * @align: required alignment in physical memory if applicable
91 * @priv: private info passed from the board file
92 *
93 * Provided by the board file.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070094 */
95struct ion_platform_heap {
96 enum ion_heap_type type;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070097 unsigned int id;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070098 const char *name;
99 ion_phys_addr_t base;
100 size_t size;
Laura Abbotta2e93632011-08-19 13:36:32 -0700101 enum ion_memory_types memory_type;
Olav Haugan85c95402012-05-30 17:32:37 -0700102 unsigned int has_outer_cache;
Olav Haugan0703dbf2011-12-19 17:53:38 -0800103 void *extra_data;
Rebecca Schultz Zavind2ce6f82012-11-15 10:52:45 -0800104 ion_phys_addr_t align;
Benjamin Gaignard8dff0a62012-06-25 15:30:18 -0700105 void *priv;
Olav Haugan0703dbf2011-12-19 17:53:38 -0800106};
107
Laura Abbottcaafeea2011-12-13 11:43:10 -0800108/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700109 * struct ion_platform_data - array of platform heaps passed from board file
Olav Haugan85c95402012-05-30 17:32:37 -0700110 * @has_outer_cache: set to 1 if outer cache is used, 0 otherwise.
Alex Bird27ca6612011-11-01 14:40:06 -0700111 * @nr: number of structures in the array
Alex Bird27ca6612011-11-01 14:40:06 -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 {
Olav Haugan85c95402012-05-30 17:32:37 -0700117 unsigned int has_outer_cache;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700118 int nr;
Benjamin Gaignard63d81032012-06-25 15:27:30 -0700119 struct ion_platform_heap *heaps;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700120};
121
Jordan Crouse8cd48322011-10-12 17:05:19 -0600122#ifdef CONFIG_ION
123
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700124/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800125 * ion_reserve() - reserve memory for ion heaps if applicable
126 * @data: platform data specifying starting physical address and
127 * size
128 *
129 * Calls memblock reserve to set aside memory for heaps that are
130 * located at specific memory addresses or of specfic sizes not
131 * managed by the kernel
132 */
133void ion_reserve(struct ion_platform_data *data);
134
135/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700136 * ion_client_create() - allocate a client and returns it
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -0800137 * @dev: the global ion device
138 * @heap_type_mask: mask of heaps this client can allocate from
139 * @name: used for debugging
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700140 */
141struct ion_client *ion_client_create(struct ion_device *dev,
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -0800142 unsigned int heap_type_mask,
143 const char *name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700144
145/**
146 * ion_client_destroy() - free's a client and all it's handles
147 * @client: the client
148 *
149 * Free the provided client and all it's resources including
150 * any handles it is holding.
151 */
152void ion_client_destroy(struct ion_client *client);
153
154/**
155 * ion_alloc - allocate ion memory
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -0800156 * @client: the client
157 * @len: size of the allocation
158 * @align: requested allocation alignment, lots of hardware blocks
159 * have alignment requirements of some kind
160 * @heap_id_mask: mask of heaps to allocate from, if multiple bits are set
161 * heaps will be tried in order from highest to lowest
162 * id
163 * @flags: heap flags, the low 16 bits are consumed by ion, the
164 * high 16 bits are passed on to the respective heap and
165 * can be heap custom
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700166 *
167 * Allocate memory in one of the heaps provided in heap mask and return
168 * an opaque handle to it.
169 */
170struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -0800171 size_t align, unsigned int heap_id_mask,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700172 unsigned int flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700173
174/**
175 * ion_free - free a handle
176 * @client: the client
177 * @handle: the handle to free
178 *
179 * Free the provided handle.
180 */
181void ion_free(struct ion_client *client, struct ion_handle *handle);
182
183/**
184 * ion_phys - returns the physical address and len of a handle
185 * @client: the client
186 * @handle: the handle
187 * @addr: a pointer to put the address in
188 * @len: a pointer to put the length in
189 *
190 * This function queries the heap for a particular handle to get the
191 * handle's physical address. It't output is only correct if
192 * a heap returns physically contiguous memory -- in other cases
Laura Abbottb14ed962012-01-30 14:18:08 -0800193 * this api should not be implemented -- ion_sg_table should be used
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700194 * instead. Returns -EINVAL if the handle is invalid. This has
195 * no implications on the reference counting of the handle --
196 * the returned value may not be valid if the caller is not
197 * holding a reference.
198 */
199int ion_phys(struct ion_client *client, struct ion_handle *handle,
200 ion_phys_addr_t *addr, size_t *len);
201
202/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800203 * ion_map_dma - return an sg_table describing a handle
204 * @client: the client
205 * @handle: the handle
206 *
207 * This function returns the sg_table describing
208 * a particular ion handle.
209 */
210struct sg_table *ion_sg_table(struct ion_client *client,
211 struct ion_handle *handle);
212
213/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700214 * ion_map_kernel - create mapping for the given handle
215 * @client: the client
216 * @handle: handle to map
217 *
218 * Map the given handle into the kernel and return a kernel address that
Mitchel Humpherysc4dba0a2012-11-05 14:06:18 -0800219 * can be used to access this address.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700220 */
Mitchel Humpherys911b4b72012-09-12 14:42:50 -0700221void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700222
223/**
224 * ion_unmap_kernel() - destroy a kernel mapping for a handle
225 * @client: the client
226 * @handle: handle to unmap
227 */
228void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
229
230/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800231 * ion_share_dma_buf() - given an ion client, create a dma-buf fd
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700232 * @client: the client
Laura Abbottb14ed962012-01-30 14:18:08 -0800233 * @handle: the handle
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700234 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800235int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700236
237/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800238 * ion_import_dma_buf() - given an dma-buf fd from the ion exporter get handle
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700239 * @client: the client
Laura Abbottb14ed962012-01-30 14:18:08 -0800240 * @fd: the dma-buf fd
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700241 *
Laura Abbottb14ed962012-01-30 14:18:08 -0800242 * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf,
243 * import that fd and return a handle representing it. If a dma-buf from
244 * another exporter is passed in this function will return ERR_PTR(-EINVAL)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700245 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800246struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd);
Laura Abbott273dd8e2011-10-12 14:26:33 -0700247
Jordan Crouse8cd48322011-10-12 17:05:19 -0600248#else
Laura Abbottb14ed962012-01-30 14:18:08 -0800249static inline void ion_reserve(struct ion_platform_data *data)
250{
251
252}
253
Jordan Crouse8cd48322011-10-12 17:05:19 -0600254static inline struct ion_client *ion_client_create(struct ion_device *dev,
255 unsigned int heap_mask, const char *name)
256{
257 return ERR_PTR(-ENODEV);
258}
Laura Abbott273dd8e2011-10-12 14:26:33 -0700259
Jordan Crouse8cd48322011-10-12 17:05:19 -0600260static inline void ion_client_destroy(struct ion_client *client) { }
261
262static inline struct ion_handle *ion_alloc(struct ion_client *client,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700263 size_t len, size_t align,
264 unsigned int heap_mask,
265 unsigned int flags)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600266{
267 return ERR_PTR(-ENODEV);
268}
269
270static inline void ion_free(struct ion_client *client,
271 struct ion_handle *handle) { }
272
273
274static inline int ion_phys(struct ion_client *client,
275 struct ion_handle *handle, ion_phys_addr_t *addr, size_t *len)
276{
277 return -ENODEV;
278}
279
Laura Abbottb14ed962012-01-30 14:18:08 -0800280static inline struct sg_table *ion_sg_table(struct ion_client *client,
281 struct ion_handle *handle)
282{
283 return ERR_PTR(-ENODEV);
284}
285
Jordan Crouse8cd48322011-10-12 17:05:19 -0600286static inline void *ion_map_kernel(struct ion_client *client,
Mitchel Humpherysbaa86922012-11-02 16:35:39 -0700287 struct ion_handle *handle)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600288{
289 return ERR_PTR(-ENODEV);
290}
291
292static inline void ion_unmap_kernel(struct ion_client *client,
293 struct ion_handle *handle) { }
294
Laura Abbottb14ed962012-01-30 14:18:08 -0800295static inline int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600296{
Laura Abbottb14ed962012-01-30 14:18:08 -0800297 return -ENODEV;
Jordan Crouse8cd48322011-10-12 17:05:19 -0600298}
299
Laura Abbottb14ed962012-01-30 14:18:08 -0800300static inline struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600301{
302 return ERR_PTR(-ENODEV);
303}
304
305static inline int ion_handle_get_flags(struct ion_client *client,
306 struct ion_handle *handle, unsigned long *flags)
307{
308 return -ENODEV;
309}
Laura Abbott8c017362011-09-22 20:59:12 -0700310
Jordan Crouse8cd48322011-10-12 17:05:19 -0600311#endif /* CONFIG_ION */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700312#endif /* __KERNEL__ */
313
314/**
315 * DOC: Ion Userspace API
316 *
317 * create a client by opening /dev/ion
318 * most operations handled via following ioctls
319 *
320 */
321
322/**
323 * struct ion_allocation_data - metadata passed from userspace for allocations
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -0800324 * @len: size of the allocation
325 * @align: required alignment of the allocation
326 * @heap_id_mask: mask of heap ids to allocate from
327 * @flags: flags passed to heap
328 * @handle: pointer that will be populated with a cookie to use to
329 * refer to this allocation
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700330 *
331 * Provided by userspace as an argument to the ioctl
332 */
333struct ion_allocation_data {
334 size_t len;
335 size_t align;
Laura Abbott0eec1512012-08-27 13:14:39 -0700336 unsigned int heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700337 unsigned int flags;
338 struct ion_handle *handle;
339};
340
341/**
342 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
343 * @handle: a handle
344 * @fd: a file descriptor representing that handle
345 *
346 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
347 * the handle returned from ion alloc, and the kernel returns the file
348 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
349 * provides the file descriptor and the kernel returns the handle.
350 */
351struct ion_fd_data {
352 struct ion_handle *handle;
353 int fd;
354};
355
356/**
357 * struct ion_handle_data - a handle passed to/from the kernel
358 * @handle: a handle
359 */
360struct ion_handle_data {
361 struct ion_handle *handle;
362};
363
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700364/**
365 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
366 * @cmd: the custom ioctl function to call
367 * @arg: additional data to pass to the custom ioctl, typically a user
368 * pointer to a predefined structure
369 *
370 * This works just like the regular cmd and arg fields of an ioctl.
371 */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700372struct ion_custom_data {
373 unsigned int cmd;
374 unsigned long arg;
375};
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700376#define ION_IOC_MAGIC 'I'
377
378/**
379 * DOC: ION_IOC_ALLOC - allocate memory
380 *
381 * Takes an ion_allocation_data struct and returns it with the handle field
382 * populated with the opaque handle for the allocation.
383 */
384#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
385 struct ion_allocation_data)
386
387/**
388 * DOC: ION_IOC_FREE - free memory
389 *
390 * Takes an ion_handle_data struct and frees the handle.
391 */
392#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
393
394/**
395 * DOC: ION_IOC_MAP - get a file descriptor to mmap
396 *
397 * Takes an ion_fd_data struct with the handle field populated with a valid
398 * opaque handle. Returns the struct with the fd field set to a file
399 * descriptor open in the current address space. This file descriptor
400 * can then be used as an argument to mmap.
401 */
402#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
403
404/**
405 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
406 *
407 * Takes an ion_fd_data struct with the handle field populated with a valid
408 * opaque handle. Returns the struct with the fd field set to a file
409 * descriptor open in the current address space. This file descriptor
410 * can then be passed to another process. The corresponding opaque handle can
411 * be retrieved via ION_IOC_IMPORT.
412 */
413#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
414
415/**
416 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
417 *
418 * Takes an ion_fd_data struct with the fd field populated with a valid file
419 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
420 * filed set to the corresponding opaque handle.
421 */
Laura Abbott0eec1512012-08-27 13:14:39 -0700422#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700423
424/**
Rebecca Schultz Zavinf4419222012-06-26 13:17:34 -0700425 * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory
426 *
427 * Deprecated in favor of using the dma_buf api's correctly (syncing
428 * will happend automatically when the buffer is mapped to a device).
429 * If necessary should be used after touching a cached buffer from the cpu,
430 * this will make the buffer in memory coherent.
431 */
432#define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
433
434/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700435 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
436 *
437 * Takes the argument of the architecture specific ioctl to call and
438 * passes appropriate userdata for that ioctl
439 */
440#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
441
Laura Abbottabcb6f72011-10-04 16:26:49 -0700442
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700443#endif /* _LINUX_ION_H */