1063072b28fb21229fd2644c343c5d3d5996953d
[firefly-linux-kernel-4.4.55.git] / include / linux / ion.h
1 /*
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
20 #include <linux/types.h>
21
22
23 #define CACHED          1
24 #define UNCACHED        0
25
26 #define ION_CACHE_SHIFT 0
27
28 #define ION_SET_CACHE(__cache)  ((__cache) << ION_CACHE_SHIFT)
29
30 #define ION_IS_CACHED(__flags)  ((__flags) & (1 << ION_CACHE_SHIFT))
31 struct ion_handle;
32 /**
33  * enum ion_heap_types - list of all possible types of heaps
34  * @ION_HEAP_TYPE_SYSTEM:        memory allocated via vmalloc
35  * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc
36  * @ION_HEAP_TYPE_CARVEOUT:      memory allocated from a prereserved
37  *                               carveout heap, allocations are physically
38  *                               contiguous
39  * @ION_HEAP_END:                helper for iterating over heaps
40  */
41 enum ion_heap_type {
42         ION_HEAP_TYPE_SYSTEM,
43         ION_HEAP_TYPE_SYSTEM_CONTIG,
44         ION_HEAP_TYPE_CARVEOUT,
45         ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
46                                  are at the end of this enum */
47         ION_NUM_HEAPS,
48 };
49 enum ion_heap_ids {
50         ION_NOR_HEAP_ID = 0,
51         ION_CMA_HEAP_ID = 1,
52         
53         ION_VPU_ID = 16,
54         ION_CAM_ID = 17,
55         ION_UI_ID = 18,
56 };
57
58 #define ION_HEAP_SYSTEM_MASK            (1 << ION_HEAP_TYPE_SYSTEM)
59 #define ION_HEAP_SYSTEM_CONTIG_MASK     (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
60 #define ION_HEAP_CARVEOUT_MASK          (1 << ION_HEAP_TYPE_CARVEOUT)
61
62 #ifdef __KERNEL__
63 struct ion_device;
64 struct ion_heap;
65 struct ion_mapper;
66 struct ion_client;
67 struct ion_buffer;
68
69 /* This should be removed some day when phys_addr_t's are fully
70    plumbed in the kernel, and all instances of ion_phys_addr_t should
71    be converted to phys_addr_t.  For the time being many kernel interfaces
72    do not accept phys_addr_t's that would have to */
73 #define ion_phys_addr_t unsigned long
74
75 /**
76  * struct ion_platform_heap - defines a heap in the given platform
77  * @type:       type of the heap from ion_heap_type enum
78  * @id:         unique identifier for heap.  When allocating (lower numbers 
79  *              will be allocated from first)
80  * @name:       used for debug purposes
81  * @base:       base address of heap in physical memory if applicable
82  * @size:       size of the heap in bytes if applicable
83  *
84  * Provided by the board file.
85  */
86 struct ion_platform_heap {
87         enum ion_heap_type type;
88         unsigned int id;
89         const char *name;
90         ion_phys_addr_t base;
91         size_t size;
92 };
93
94 /**
95  * struct ion_platform_data - array of platform heaps passed from board file
96  * @nr:         number of structures in the array
97  * @heaps:      array of platform_heap structions
98  *
99  * Provided by the board file in the form of platform data to a platform device.
100  */
101 struct ion_platform_data {
102         int nr;
103         struct ion_platform_heap heaps[];
104 };
105
106 /**
107  * ion_client_create() -  allocate a client and returns it
108  * @dev:        the global ion device
109  * @heap_mask:  mask of heaps this client can allocate from
110  * @name:       used for debugging
111  */
112 struct ion_client *ion_client_create(struct ion_device *dev,
113                                      unsigned int heap_mask, const char *name);
114
115 /**
116  * ion_client_destroy() -  free's a client and all it's handles
117  * @client:     the client
118  *
119  * Free the provided client and all it's resources including
120  * any handles it is holding.
121  */
122 void ion_client_destroy(struct ion_client *client);
123
124 /**
125  * ion_alloc - allocate ion memory
126  * @client:     the client
127  * @len:        size of the allocation
128  * @align:      requested allocation alignment, lots of hardware blocks have
129  *              alignment requirements of some kind
130  * @flags:      mask of heaps to allocate from, if multiple bits are set
131  *              heaps will be tried in order from lowest to highest order bit
132  *
133  * Allocate memory in one of the heaps provided in heap mask and return
134  * an opaque handle to it.
135  */
136 struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
137                              size_t align, unsigned int flags);
138
139 /**
140  * ion_free - free a handle
141  * @client:     the client
142  * @handle:     the handle to free
143  *
144  * Free the provided handle.
145  */
146 void ion_free(struct ion_client *client, struct ion_handle *handle);
147
148 /**
149  * ion_phys - returns the physical address and len of a handle
150  * @client:     the client
151  * @handle:     the handle
152  * @addr:       a pointer to put the address in
153  * @len:        a pointer to put the length in
154  *
155  * This function queries the heap for a particular handle to get the
156  * handle's physical address.  It't output is only correct if
157  * a heap returns physically contiguous memory -- in other cases
158  * this api should not be implemented -- ion_map_dma should be used
159  * instead.  Returns -EINVAL if the handle is invalid.  This has
160  * no implications on the reference counting of the handle --
161  * the returned value may not be valid if the caller is not
162  * holding a reference.
163  */
164 int ion_phys(struct ion_client *client, struct ion_handle *handle,
165              ion_phys_addr_t *addr, size_t *len);
166
167 /**
168  * ion_map_kernel - create mapping for the given handle
169  * @client:     the client
170  * @handle:     handle to map
171  *
172  * Map the given handle into the kernel and return a kernel address that
173  * can be used to access this address.
174  */
175 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
176
177 /**
178  * ion_unmap_kernel() - destroy a kernel mapping for a handle
179  * @client:     the client
180  * @handle:     handle to unmap
181  */
182 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
183
184 /**
185  * ion_map_dma - create a dma mapping for a given handle
186  * @client:     the client
187  * @handle:     handle to map
188  *
189  * Return an sglist describing the given handle
190  */
191 struct scatterlist *ion_map_dma(struct ion_client *client,
192                                 struct ion_handle *handle);
193
194 /**
195  * ion_unmap_dma() - destroy a dma mapping for a handle
196  * @client:     the client
197  * @handle:     handle to unmap
198  */
199 void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle);
200
201 /**
202  * ion_share() - given a handle, obtain a buffer to pass to other clients
203  * @client:     the client
204  * @handle:     the handle to share
205  *
206  * Given a handle, return a buffer, which exists in a global name
207  * space, and can be passed to other clients.  Should be passed into ion_import
208  * to obtain a new handle for this buffer.
209  *
210  * NOTE: This function does do not an extra reference.  The burden is on the
211  * caller to make sure the buffer doesn't go away while it's being passed to
212  * another client.  That is, ion_free should not be called on this handle until
213  * the buffer has been imported into the other client.
214  */
215 struct ion_buffer *ion_share(struct ion_client *client,
216                              struct ion_handle *handle);
217
218 /**
219  * ion_import() - given an buffer in another client, import it
220  * @client:     this blocks client
221  * @buffer:     the buffer to import (as obtained from ion_share)
222  *
223  * Given a buffer, add it to the client and return the handle to use to refer
224  * to it further.  This is called to share a handle from one kernel client to
225  * another.
226  */
227 struct ion_handle *ion_import(struct ion_client *client,
228                               struct ion_buffer *buffer);
229
230 /**
231  * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it
232  * @client:     this blocks client
233  * @fd:         the fd
234  *
235  * A helper function for drivers that will be recieving ion buffers shared
236  * with them from userspace.  These buffers are represented by a file
237  * descriptor obtained as the return from the ION_IOC_SHARE ioctl.
238  * This function coverts that fd into the underlying buffer, and returns
239  * the handle to use to refer to it further.
240  */
241 struct ion_handle *ion_import_fd(struct ion_client *client, int fd);
242 #endif /* __KERNEL__ */
243
244 /**
245  * DOC: Ion Userspace API
246  *
247  * create a client by opening /dev/ion
248  * most operations handled via following ioctls
249  *
250  */
251
252 /**
253  * struct ion_allocation_data - metadata passed from userspace for allocations
254  * @len:        size of the allocation
255  * @align:      required alignment of the allocation
256  * @flags:      flags passed to heap
257  * @handle:     pointer that will be populated with a cookie to use to refer
258  *              to this allocation
259  *
260  * Provided by userspace as an argument to the ioctl
261  */
262 struct ion_allocation_data {
263         size_t len;
264         size_t align;
265         unsigned int flags;
266         struct ion_handle *handle;
267 };
268
269 /**
270  * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
271  * @handle:     a handle
272  * @fd:         a file descriptor representing that handle
273  *
274  * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
275  * the handle returned from ion alloc, and the kernel returns the file
276  * descriptor to share or map in the fd field.  For ION_IOC_IMPORT, userspace
277  * provides the file descriptor and the kernel returns the handle.
278  */
279 struct ion_fd_data {
280         struct ion_handle *handle;
281         int fd;
282 };
283
284 /**
285  * struct ion_handle_data - a handle passed to/from the kernel
286  * @handle:     a handle
287  */
288 struct ion_handle_data {
289         struct ion_handle *handle;
290 };
291
292 /**
293  * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
294  * @cmd:        the custom ioctl function to call
295  * @arg:        additional data to pass to the custom ioctl, typically a user
296  *              pointer to a predefined structure
297  *
298  * This works just like the regular cmd and arg fields of an ioctl.
299  */
300 struct ion_custom_data {
301         unsigned int cmd;
302         unsigned long arg;
303 };
304
305 struct ion_phys_data {
306         struct ion_handle *handle;
307         unsigned long phys;
308         size_t size;
309 };
310 struct ion_flush_data {
311         struct ion_handle *handle;
312         void *virt;
313         size_t size;
314 };
315 struct ion_client_data {
316 #define ION_TYPE_GET_TOTAL_SIZE  0
317 #define ION_TYPE_SIZE_GET_COUNT  1
318         unsigned int type;
319         union {
320                 size_t size;
321                 size_t total_size;
322         };
323         unsigned int count;
324 };
325 #define ION_IOC_MAGIC           'I'
326
327 /**
328  * DOC: ION_IOC_ALLOC - allocate memory
329  *
330  * Takes an ion_allocation_data struct and returns it with the handle field
331  * populated with the opaque handle for the allocation.
332  */
333 #define ION_IOC_ALLOC           _IOWR(ION_IOC_MAGIC, 0, \
334                                       struct ion_allocation_data)
335
336 /**
337  * DOC: ION_IOC_FREE - free memory
338  *
339  * Takes an ion_handle_data struct and frees the handle.
340  */
341 #define ION_IOC_FREE            _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
342
343 /**
344  * DOC: ION_IOC_MAP - get a file descriptor to mmap
345  *
346  * Takes an ion_fd_data struct with the handle field populated with a valid
347  * opaque handle.  Returns the struct with the fd field set to a file
348  * descriptor open in the current address space.  This file descriptor
349  * can then be used as an argument to mmap.
350  */
351 #define ION_IOC_MAP             _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
352
353 /**
354  * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
355  *
356  * Takes an ion_fd_data struct with the handle field populated with a valid
357  * opaque handle.  Returns the struct with the fd field set to a file
358  * descriptor open in the current address space.  This file descriptor
359  * can then be passed to another process.  The corresponding opaque handle can
360  * be retrieved via ION_IOC_IMPORT.
361  */
362 #define ION_IOC_SHARE           _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
363
364 /**
365  * DOC: ION_IOC_IMPORT - imports a shared file descriptor
366  *
367  * Takes an ion_fd_data struct with the fd field populated with a valid file
368  * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
369  * filed set to the corresponding opaque handle.
370  */
371 #define ION_IOC_IMPORT          _IOWR(ION_IOC_MAGIC, 5, int)
372
373 /**
374  * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
375  *
376  * Takes the argument of the architecture specific ioctl to call and
377  * passes appropriate userdata for that ioctl
378  */
379 #define ION_IOC_CUSTOM          _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
380 #define ION_CACHE_FLUSH         _IOWR(ION_IOC_MAGIC, 7, struct ion_flush_data)
381 #define ION_CACHE_CLEAN         _IOWR(ION_IOC_MAGIC, 8, struct ion_flush_data)
382 #define ION_CACHE_INVALID       _IOWR(ION_IOC_MAGIC, 9, struct ion_flush_data)
383 #define ION_GET_PHYS            _IOWR(ION_IOC_MAGIC, 10, unsigned long)
384 #define ION_GET_CLIENT          _IOWR(ION_IOC_MAGIC, 11, struct ion_client_data)
385 #endif /* _LINUX_ION_H */