blob: 498331660c5c6d12aff60807354df33929c5a568 [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/**
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -080051 * allocation flags - the lower 16 bits are used by core ion, the upper 16
Mitchel Humpherys227a6582012-09-11 15:59:11 -070052 * 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 const char *name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700143
144/**
145 * ion_client_destroy() - free's a client and all it's handles
146 * @client: the client
147 *
148 * Free the provided client and all it's resources including
149 * any handles it is holding.
150 */
151void ion_client_destroy(struct ion_client *client);
152
153/**
154 * ion_alloc - allocate ion memory
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -0800155 * @client: the client
156 * @len: size of the allocation
157 * @align: requested allocation alignment, lots of hardware blocks
158 * have alignment requirements of some kind
159 * @heap_id_mask: mask of heaps to allocate from, if multiple bits are set
160 * heaps will be tried in order from highest to lowest
161 * id
162 * @flags: heap flags, the low 16 bits are consumed by ion, the
163 * high 16 bits are passed on to the respective heap and
164 * can be heap custom
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700165 *
166 * Allocate memory in one of the heaps provided in heap mask and return
167 * an opaque handle to it.
168 */
169struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -0800170 size_t align, unsigned int heap_id_mask,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700171 unsigned int flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700172
173/**
174 * ion_free - free a handle
175 * @client: the client
176 * @handle: the handle to free
177 *
178 * Free the provided handle.
179 */
180void ion_free(struct ion_client *client, struct ion_handle *handle);
181
182/**
183 * ion_phys - returns the physical address and len of a handle
184 * @client: the client
185 * @handle: the handle
186 * @addr: a pointer to put the address in
187 * @len: a pointer to put the length in
188 *
189 * This function queries the heap for a particular handle to get the
190 * handle's physical address. It't output is only correct if
191 * a heap returns physically contiguous memory -- in other cases
Laura Abbottb14ed962012-01-30 14:18:08 -0800192 * this api should not be implemented -- ion_sg_table should be used
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700193 * instead. Returns -EINVAL if the handle is invalid. This has
194 * no implications on the reference counting of the handle --
195 * the returned value may not be valid if the caller is not
196 * holding a reference.
197 */
198int ion_phys(struct ion_client *client, struct ion_handle *handle,
199 ion_phys_addr_t *addr, size_t *len);
200
201/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800202 * ion_map_dma - return an sg_table describing a handle
203 * @client: the client
204 * @handle: the handle
205 *
206 * This function returns the sg_table describing
207 * a particular ion handle.
208 */
209struct sg_table *ion_sg_table(struct ion_client *client,
210 struct ion_handle *handle);
211
212/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700213 * ion_map_kernel - create mapping for the given handle
214 * @client: the client
215 * @handle: handle to map
216 *
217 * Map the given handle into the kernel and return a kernel address that
Mitchel Humpherysc4dba0a2012-11-05 14:06:18 -0800218 * can be used to access this address.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700219 */
Mitchel Humpherys911b4b72012-09-12 14:42:50 -0700220void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700221
222/**
223 * ion_unmap_kernel() - destroy a kernel mapping for a handle
224 * @client: the client
225 * @handle: handle to unmap
226 */
227void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
228
229/**
Johan Mossberg748c11d2013-01-11 13:38:13 +0100230 * ion_share_dma_buf() - share buffer as dma-buf
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700231 * @client: the client
Laura Abbottb14ed962012-01-30 14:18:08 -0800232 * @handle: the handle
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700233 */
Johan Mossberg748c11d2013-01-11 13:38:13 +0100234struct dma_buf *ion_share_dma_buf(struct ion_client *client,
235 struct ion_handle *handle);
236
237/**
238 * ion_share_dma_buf_fd() - given an ion client, create a dma-buf fd
239 * @client: the client
240 * @handle: the handle
241 */
242int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700243
244/**
Laura Abbottb14ed962012-01-30 14:18:08 -0800245 * ion_import_dma_buf() - given an dma-buf fd from the ion exporter get handle
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700246 * @client: the client
Laura Abbottb14ed962012-01-30 14:18:08 -0800247 * @fd: the dma-buf fd
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700248 *
Laura Abbottb14ed962012-01-30 14:18:08 -0800249 * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf,
250 * import that fd and return a handle representing it. If a dma-buf from
251 * another exporter is passed in this function will return ERR_PTR(-EINVAL)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700252 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800253struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd);
Laura Abbott273dd8e2011-10-12 14:26:33 -0700254
Jordan Crouse8cd48322011-10-12 17:05:19 -0600255#else
Laura Abbottb14ed962012-01-30 14:18:08 -0800256static inline void ion_reserve(struct ion_platform_data *data)
257{
258
259}
260
Jordan Crouse8cd48322011-10-12 17:05:19 -0600261static inline struct ion_client *ion_client_create(struct ion_device *dev,
262 unsigned int heap_mask, const char *name)
263{
264 return ERR_PTR(-ENODEV);
265}
Laura Abbott273dd8e2011-10-12 14:26:33 -0700266
Jordan Crouse8cd48322011-10-12 17:05:19 -0600267static inline void ion_client_destroy(struct ion_client *client) { }
268
269static inline struct ion_handle *ion_alloc(struct ion_client *client,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700270 size_t len, size_t align,
271 unsigned int heap_mask,
272 unsigned int flags)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600273{
274 return ERR_PTR(-ENODEV);
275}
276
277static inline void ion_free(struct ion_client *client,
278 struct ion_handle *handle) { }
279
280
281static inline int ion_phys(struct ion_client *client,
282 struct ion_handle *handle, ion_phys_addr_t *addr, size_t *len)
283{
284 return -ENODEV;
285}
286
Laura Abbottb14ed962012-01-30 14:18:08 -0800287static inline struct sg_table *ion_sg_table(struct ion_client *client,
288 struct ion_handle *handle)
289{
290 return ERR_PTR(-ENODEV);
291}
292
Jordan Crouse8cd48322011-10-12 17:05:19 -0600293static inline void *ion_map_kernel(struct ion_client *client,
Mitchel Humpherysbaa86922012-11-02 16:35:39 -0700294 struct ion_handle *handle)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600295{
296 return ERR_PTR(-ENODEV);
297}
298
299static inline void ion_unmap_kernel(struct ion_client *client,
300 struct ion_handle *handle) { }
301
Laura Abbottb14ed962012-01-30 14:18:08 -0800302static inline int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600303{
Laura Abbottb14ed962012-01-30 14:18:08 -0800304 return -ENODEV;
Jordan Crouse8cd48322011-10-12 17:05:19 -0600305}
306
Laura Abbottb14ed962012-01-30 14:18:08 -0800307static inline struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
Jordan Crouse8cd48322011-10-12 17:05:19 -0600308{
309 return ERR_PTR(-ENODEV);
310}
311
312static inline int ion_handle_get_flags(struct ion_client *client,
313 struct ion_handle *handle, unsigned long *flags)
314{
315 return -ENODEV;
316}
Laura Abbott8c017362011-09-22 20:59:12 -0700317
Jordan Crouse8cd48322011-10-12 17:05:19 -0600318#endif /* CONFIG_ION */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700319#endif /* __KERNEL__ */
320
321/**
322 * DOC: Ion Userspace API
323 *
324 * create a client by opening /dev/ion
325 * most operations handled via following ioctls
326 *
327 */
328
329/**
330 * struct ion_allocation_data - metadata passed from userspace for allocations
Rebecca Schultz Zavin2536d602012-12-11 11:45:59 -0800331 * @len: size of the allocation
332 * @align: required alignment of the allocation
333 * @heap_id_mask: mask of heap ids to allocate from
334 * @flags: flags passed to heap
335 * @handle: pointer that will be populated with a cookie to use to
336 * refer to this allocation
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700337 *
338 * Provided by userspace as an argument to the ioctl
339 */
340struct ion_allocation_data {
341 size_t len;
342 size_t align;
Laura Abbott0eec1512012-08-27 13:14:39 -0700343 unsigned int heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700344 unsigned int flags;
345 struct ion_handle *handle;
346};
347
348/**
349 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
350 * @handle: a handle
351 * @fd: a file descriptor representing that handle
352 *
353 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
354 * the handle returned from ion alloc, and the kernel returns the file
355 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
356 * provides the file descriptor and the kernel returns the handle.
357 */
358struct ion_fd_data {
359 struct ion_handle *handle;
360 int fd;
361};
362
363/**
364 * struct ion_handle_data - a handle passed to/from the kernel
365 * @handle: a handle
366 */
367struct ion_handle_data {
368 struct ion_handle *handle;
369};
370
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700371/**
372 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
373 * @cmd: the custom ioctl function to call
374 * @arg: additional data to pass to the custom ioctl, typically a user
375 * pointer to a predefined structure
376 *
377 * This works just like the regular cmd and arg fields of an ioctl.
378 */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700379struct ion_custom_data {
380 unsigned int cmd;
381 unsigned long arg;
382};
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700383#define ION_IOC_MAGIC 'I'
384
385/**
386 * DOC: ION_IOC_ALLOC - allocate memory
387 *
388 * Takes an ion_allocation_data struct and returns it with the handle field
389 * populated with the opaque handle for the allocation.
390 */
391#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
392 struct ion_allocation_data)
393
394/**
395 * DOC: ION_IOC_FREE - free memory
396 *
397 * Takes an ion_handle_data struct and frees the handle.
398 */
399#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
400
401/**
402 * DOC: ION_IOC_MAP - get a file descriptor to mmap
403 *
404 * Takes an ion_fd_data struct with the handle field populated with a valid
405 * opaque handle. Returns the struct with the fd field set to a file
406 * descriptor open in the current address space. This file descriptor
407 * can then be used as an argument to mmap.
408 */
409#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
410
411/**
412 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
413 *
414 * Takes an ion_fd_data struct with the handle field populated with a valid
415 * opaque handle. Returns the struct with the fd field set to a file
416 * descriptor open in the current address space. This file descriptor
417 * can then be passed to another process. The corresponding opaque handle can
418 * be retrieved via ION_IOC_IMPORT.
419 */
420#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
421
422/**
423 * DOC: ION_IOC_IMPORT - imports a shared file descriptor
424 *
425 * Takes an ion_fd_data struct with the fd field populated with a valid file
426 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
427 * filed set to the corresponding opaque handle.
428 */
Laura Abbott0eec1512012-08-27 13:14:39 -0700429#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700430
431/**
Rebecca Schultz Zavinf4419222012-06-26 13:17:34 -0700432 * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory
433 *
434 * Deprecated in favor of using the dma_buf api's correctly (syncing
435 * will happend automatically when the buffer is mapped to a device).
436 * If necessary should be used after touching a cached buffer from the cpu,
437 * this will make the buffer in memory coherent.
438 */
439#define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
440
441/**
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700442 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
443 *
444 * Takes the argument of the architecture specific ioctl to call and
445 * passes appropriate userdata for that ioctl
446 */
447#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
448
Laura Abbottabcb6f72011-10-04 16:26:49 -0700449
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700450#endif /* _LINUX_ION_H */