244f085b5895da6a2c9bcac43cd7f54e8a4643a3
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / android / ion / ion.h
1 /*
2  * drivers/staging/android/ion/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
20 #include <linux/types.h>
21
22 struct ion_handle;
23 typedef struct ion_handle *ion_user_handle_t;
24
25 /**
26  * enum ion_heap_types - list of all possible types of heaps
27  * @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_TYPE_DMA:           memory allocated via DMA API
33  * @ION_NUM_HEAPS:               helper for iterating over heaps, a bit mask
34  *                               is used to identify the heaps, so only 32
35  *                               total heap types are supported
36  */
37 enum ion_heap_type {
38         ION_HEAP_TYPE_SYSTEM,
39         ION_HEAP_TYPE_SYSTEM_CONTIG,
40         ION_HEAP_TYPE_CARVEOUT,
41         ION_HEAP_TYPE_CHUNK,
42         ION_HEAP_TYPE_DMA,
43         ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
44                                  are at the end of this enum */
45         ION_NUM_HEAPS,
46 };
47
48 #define ION_HEAP_SYSTEM_MASK            (1 << ION_HEAP_TYPE_SYSTEM)
49 #define ION_HEAP_SYSTEM_CONTIG_MASK     (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
50 #define ION_HEAP_CARVEOUT_MASK          (1 << ION_HEAP_TYPE_CARVEOUT)
51 #define ION_HEAP_TYPE_DMA_MASK          (1 << ION_HEAP_TYPE_DMA)
52
53 #define ION_NUM_HEAP_IDS                sizeof(unsigned int) * 8
54
55 /**
56  * allocation flags - the lower 16 bits are used by core ion, the upper 16
57  * bits are reserved for use by the heaps themselves.
58  */
59 #define ION_FLAG_CACHED 1               /* mappings of this buffer should be
60                                            cached, ion will do cache
61                                            maintenance when the buffer is
62                                            mapped for dma */
63 #define ION_FLAG_CACHED_NEEDS_SYNC 2    /* mappings of this buffer will created
64                                            at mmap time, if this is set
65                                            caches must be managed manually */
66
67 #ifdef __KERNEL__
68 struct ion_device;
69 struct ion_heap;
70 struct ion_mapper;
71 struct ion_client;
72 struct ion_buffer;
73
74 /* This should be removed some day when phys_addr_t's are fully
75    plumbed in the kernel, and all instances of ion_phys_addr_t should
76    be converted to phys_addr_t.  For the time being many kernel interfaces
77    do not accept phys_addr_t's that would have to */
78 #define ion_phys_addr_t unsigned long
79
80 /**
81  * struct ion_platform_heap - defines a heap in the given platform
82  * @type:       type of the heap from ion_heap_type enum
83  * @id:         unique identifier for heap.  When allocating higher numbers
84  *              will be allocated from first.  At allocation these are passed
85  *              as a bit mask and therefore can not exceed ION_NUM_HEAP_IDS.
86  * @name:       used for debug purposes
87  * @base:       base address of heap in physical memory if applicable
88  * @size:       size of the heap in bytes if applicable
89  * @align:      required alignment in physical memory if applicable
90  * @priv:       private info passed from the board file
91  *
92  * Provided by the board file.
93  */
94 struct ion_platform_heap {
95         enum ion_heap_type type;
96         unsigned int id;
97         const char *name;
98         ion_phys_addr_t base;
99         size_t size;
100         ion_phys_addr_t align;
101         void *priv;
102 };
103
104 /**
105  * struct ion_platform_data - array of platform heaps passed from board file
106  * @nr:         number of structures in the array
107  * @heaps:      array of platform_heap structions
108  *
109  * Provided by the board file in the form of platform data to a platform device.
110  */
111 struct ion_platform_data {
112         int nr;
113         struct ion_platform_heap *heaps;
114 };
115
116 /**
117  * ion_reserve() - reserve memory for ion heaps if applicable
118  * @data:       platform data specifying starting physical address and
119  *              size
120  *
121  * Calls memblock reserve to set aside memory for heaps that are
122  * located at specific memory addresses or of specfic sizes not
123  * managed by the kernel
124  */
125 void ion_reserve(struct ion_platform_data *data);
126
127 /**
128  * ion_client_create() -  allocate a client and returns it
129  * @dev:                the global ion device
130  * @heap_type_mask:     mask of heaps this client can allocate from
131  * @name:               used for debugging
132  */
133 struct ion_client *ion_client_create(struct ion_device *dev,
134                                      const char *name);
135
136 /**
137  * ion_client_destroy() -  free's a client and all it's handles
138  * @client:     the client
139  *
140  * Free the provided client and all it's resources including
141  * any handles it is holding.
142  */
143 void ion_client_destroy(struct ion_client *client);
144
145 /**
146  * ion_alloc - allocate ion memory
147  * @client:             the client
148  * @len:                size of the allocation
149  * @align:              requested allocation alignment, lots of hardware blocks
150  *                      have alignment requirements of some kind
151  * @heap_id_mask:       mask of heaps to allocate from, if multiple bits are set
152  *                      heaps will be tried in order from highest to lowest
153  *                      id
154  * @flags:              heap flags, the low 16 bits are consumed by ion, the
155  *                      high 16 bits are passed on to the respective heap and
156  *                      can be heap custom
157  *
158  * Allocate memory in one of the heaps provided in heap mask and return
159  * an opaque handle to it.
160  */
161 struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
162                              size_t align, unsigned int heap_id_mask,
163                              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  */
172 void 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_sg_table 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  */
190 int ion_phys(struct ion_client *client, struct ion_handle *handle,
191              ion_phys_addr_t *addr, size_t *len);
192
193 /**
194  * ion_map_dma - return an sg_table describing a handle
195  * @client:     the client
196  * @handle:     the handle
197  *
198  * This function returns the sg_table describing
199  * a particular ion handle.
200  */
201 struct sg_table *ion_sg_table(struct ion_client *client,
202                               struct ion_handle *handle);
203
204 /**
205  * ion_map_kernel - create mapping for the given handle
206  * @client:     the client
207  * @handle:     handle to map
208  *
209  * Map the given handle into the kernel and return a kernel address that
210  * can be used to access this address.
211  */
212 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
213
214 /**
215  * ion_unmap_kernel() - destroy a kernel mapping for a handle
216  * @client:     the client
217  * @handle:     handle to unmap
218  */
219 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
220
221 /**
222  * ion_share_dma_buf() - share buffer as dma-buf
223  * @client:     the client
224  * @handle:     the handle
225  */
226 struct dma_buf *ion_share_dma_buf(struct ion_client *client,
227                                                 struct ion_handle *handle);
228
229 /**
230  * ion_share_dma_buf_fd() - given an ion client, create a dma-buf fd
231  * @client:     the client
232  * @handle:     the handle
233  */
234 int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle);
235
236 /**
237  * ion_import_dma_buf() - given an dma-buf fd from the ion exporter get handle
238  * @client:     the client
239  * @fd:         the dma-buf fd
240  *
241  * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf,
242  * import that fd and return a handle representing it.  If a dma-buf from
243  * another exporter is passed in this function will return ERR_PTR(-EINVAL)
244  */
245 struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd);
246
247 #endif /* __KERNEL__ */
248
249 /**
250  * DOC: Ion Userspace API
251  *
252  * create a client by opening /dev/ion
253  * most operations handled via following ioctls
254  *
255  */
256
257 /**
258  * struct ion_allocation_data - metadata passed from userspace for allocations
259  * @len:                size of the allocation
260  * @align:              required alignment of the allocation
261  * @heap_id_mask:       mask of heap ids to allocate from
262  * @flags:              flags passed to heap
263  * @handle:             pointer that will be populated with a cookie to use to 
264  *                      refer to this allocation
265  *
266  * Provided by userspace as an argument to the ioctl
267  */
268 struct ion_allocation_data {
269         size_t len;
270         size_t align;
271         unsigned int heap_id_mask;
272         unsigned int flags;
273         ion_user_handle_t handle;
274 };
275
276 /**
277  * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
278  * @handle:     a handle
279  * @fd:         a file descriptor representing that handle
280  *
281  * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
282  * the handle returned from ion alloc, and the kernel returns the file
283  * descriptor to share or map in the fd field.  For ION_IOC_IMPORT, userspace
284  * provides the file descriptor and the kernel returns the handle.
285  */
286 struct ion_fd_data {
287         ion_user_handle_t handle;
288         int fd;
289 };
290
291 /**
292  * struct ion_handle_data - a handle passed to/from the kernel
293  * @handle:     a handle
294  */
295 struct ion_handle_data {
296         ion_user_handle_t handle;
297 };
298
299 /**
300  * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
301  * @cmd:        the custom ioctl function to call
302  * @arg:        additional data to pass to the custom ioctl, typically a user
303  *              pointer to a predefined structure
304  *
305  * This works just like the regular cmd and arg fields of an ioctl.
306  */
307 struct ion_custom_data {
308         unsigned int cmd;
309         unsigned long arg;
310 };
311
312 #define ION_IOC_MAGIC           'I'
313
314 /**
315  * DOC: ION_IOC_ALLOC - allocate memory
316  *
317  * Takes an ion_allocation_data struct and returns it with the handle field
318  * populated with the opaque handle for the allocation.
319  */
320 #define ION_IOC_ALLOC           _IOWR(ION_IOC_MAGIC, 0, \
321                                       struct ion_allocation_data)
322
323 /**
324  * DOC: ION_IOC_FREE - free memory
325  *
326  * Takes an ion_handle_data struct and frees the handle.
327  */
328 #define ION_IOC_FREE            _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
329
330 /**
331  * DOC: ION_IOC_MAP - get a file descriptor to mmap
332  *
333  * Takes an ion_fd_data struct with the handle field populated with a valid
334  * opaque handle.  Returns the struct with the fd field set to a file
335  * descriptor open in the current address space.  This file descriptor
336  * can then be used as an argument to mmap.
337  */
338 #define ION_IOC_MAP             _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
339
340 /**
341  * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
342  *
343  * Takes an ion_fd_data struct with the handle field populated with a valid
344  * opaque handle.  Returns the struct with the fd field set to a file
345  * descriptor open in the current address space.  This file descriptor
346  * can then be passed to another process.  The corresponding opaque handle can
347  * be retrieved via ION_IOC_IMPORT.
348  */
349 #define ION_IOC_SHARE           _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
350
351 /**
352  * DOC: ION_IOC_IMPORT - imports a shared file descriptor
353  *
354  * Takes an ion_fd_data struct with the fd field populated with a valid file
355  * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
356  * filed set to the corresponding opaque handle.
357  */
358 #define ION_IOC_IMPORT          _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
359
360 /**
361  * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory
362  *
363  * Deprecated in favor of using the dma_buf api's correctly (syncing
364  * will happend automatically when the buffer is mapped to a device).
365  * If necessary should be used after touching a cached buffer from the cpu,
366  * this will make the buffer in memory coherent.
367  */
368 #define ION_IOC_SYNC            _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
369
370 /**
371  * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
372  *
373  * Takes the argument of the architecture specific ioctl to call and
374  * passes appropriate userdata for that ioctl
375  */
376 #define ION_IOC_CUSTOM          _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
377
378 #endif /* _LINUX_ION_H */