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