1/*
2 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _LIB_ION_H_
18#define _LIB_ION_H_
19
20#include <unistd.h> /* size_t */
21
22#define ION_FLAG_CACHED 1
23#define ION_FLAG_CACHED_NEEDS_SYNC 2
24
25#define ION_HEAP_SYSTEM_MASK            (1 << 0)
26#define ION_HEAP_SYSTEM_CONTIG_MASK     (1 << 1)
27#define ION_HEAP_EXYNOS_CONTIG_MASK     (1 << 4)
28#define ION_HEAP_EXYNOS_MASK            (1 << 5)
29#define ION_EXYNOS_FIMD_VIDEO_MASK    (1 << 28)
30#define ION_EXYNOS_GSC_MASK		(1 << 27)
31#define ION_EXYNOS_MFC_OUTPUT_MASK    (1 << 26)
32#define ION_EXYNOS_MFC_INPUT_MASK    (1 << 25)
33
34
35/* ION_MSYNC_FLAGS
36 * values of @flags parameter to ion_msync()
37 *
38 * IMSYNC_DEV_TO_READ: Device only reads the buffer
39 * IMSYNC_DEV_TO_WRITE: Device may writes to the buffer
40 * IMSYNC_DEV_TO_RW: Device reads and writes to the buffer
41 *
42 * IMSYNC_SYNC_FOR_DEV: ion_msync() for device to access the buffer
43 * IMSYNC_SYNC_FOR_CPU: ion_msync() for CPU to access the buffer after device
44 *                      has accessed it.
45 *
46 * The values must be ORed with one of IMSYNC_DEV_* and one of IMSYNC_SYNC_*.
47 * Otherwise, ion_msync() will not effect.
48 */
49enum ION_MSYNC_FLAGS {
50    IMSYNC_DEV_TO_READ = 0,
51    IMSYNC_DEV_TO_WRITE = 1,
52    IMSYNC_DEV_TO_RW = 2,
53    IMSYNC_SYNC_FOR_DEV = 0x10000,
54    IMSYNC_SYNC_FOR_CPU = 0x20000,
55};
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61/* ion_client
62 * An ION client is an object or an entity that needs to use the service of
63 * ION and has unique address space. ion_client is an identifier of an ION
64 * client and it represents the ION client.
65 * All operations on ION needs a valid ion_client value and it can be obtained
66 * by ion_client_create().
67 */
68typedef int ion_client;
69
70/* ion_buffer
71 * An identifier of a buffer allocated from ION. You must obtain to access
72 * a buffer allocated from ION. If you have an effective ion_buffer, you have
73 * three options to work with it.
74 * - To access  the buffer, you can request an address (user virtual address)
75 *   of the buffer with ion_map().
76 * - To pass the buffer to the kernel, you can pass the ion_buffer to the
77 *   kernel driver directly, if the kernel driver can work with ION.
78 * - To pass the buffer to other processes, you can pass the ion_buffer to
79 *   other processes through RPC machanism such as socket communication or
80 *   Android Binder because ion_buffer is actually an open file descripotor
81 *   of the current process.
82 */
83typedef int ion_buffer;
84
85/* ion_client_create()
86 * @RETURN: new ion_client.
87 *          netative value if creating new ion_client is failed.
88 *
89 * A call to ion_client_create() must be paired with ion_client_destroy(),
90 * symmetrically. ion_client_destroy() needs a valid ion_client that
91 * is returned by ion_client_create().
92 */
93ion_client ion_client_create(void);
94
95/* ion_client_destroy()
96 * @client: An ion_client value to remove.
97 */
98void ion_client_destroy(ion_client client);
99
100/* ion_alloc() - Allocates new buffer from ION.
101 * @client: A valid ion_client value returned by ion_client_create().
102 * @len: Size of a buffer required in bytes.
103 * @align: Alignment requirements of @len and the start address of the allocated
104 *         buffer. If the @len is not aligned by @align, ION allocates a buffer
105 *         that is aligned by @align and the size of the buffer will be larger
106 *         than @len.
107 * @heap_mask: Mask of heaps which you want this allocation to be served from.
108 * @flags: Additional requirements about buffer. ION_FLAG_CACHED for a
109 * 	   buffer you want to have a cached mapping of
110 * @RETURN: An ion_buffer that represents the buffer allocated. It is only
111 *          unique in the context of the given client, @client.
112 *          -error if the allocation failed.
113 *          See the description of ion_buffer above for detailed information.
114 */
115ion_buffer ion_alloc(ion_client client, size_t len, size_t align,
116                     unsigned int heap_mask, unsigned int flags);
117
118/* ion_free() - Frees an existing buffer that is allocated by ION
119 * @buffer: An ion_buffer of the buffer to be released.
120 */
121void ion_free(ion_buffer buffer);
122
123/* ion_map() - Obtains a virtual address of the buffer identied by @buffer
124 * @buffer: The buffer to map. The virtual address returned is allocated by the
125 *          kernel.
126 * @len: The size of the buffer to map. This must not exceed the size of the
127 *       buffer represented by @fd_buf. Thus you need to know the size of it
128 *       before calling this function. If @len is less than the size of the
129 *       buffer, this function just map just the size requested (@len) not the
130 *       entire buffer.
131 * @offset: How many pages will be ignored while mapping.@offset number of
132 *       pages from the start of the buffer will not be mapped.
133 * @RETURN: The start virtual addres mapped.
134 *          MAP_FAILED if mapping fails.
135 *
136 * Note that @len + (@offset * PAGE_SIZE) must not exceed the size of the
137 * buffer.
138 */
139void *ion_map(ion_buffer buffer, size_t len, off_t offset);
140
141/* ion_unmap() - Frees the buffer mapped by ion_map()
142 * @addr: The address returned by ion_map().
143 * @len: The size of the buffer mapped by ion_map().
144 * @RETURN: 0 on success, and -1 on failure.
145 *          errno is also set on failure.
146 */
147int ion_unmap(void *addr, size_t len);
148
149/* ion_msync() - Makes sure that data in the buffer are visible to H/W peri.
150 * @client: A valid ion_client value returned by ion_client_create().
151 * @buffer: The buffer to perform ion_msync().
152 * @flags: Direction of access of H/W peri and CPU. See the description of
153 *         ION_MSYNC_FLAGS.
154 * @size: Size to ion_msync() in bytes.
155 * @offset: Where ion_msync() start in @buffer, size in bytes.
156 * @RETURN: 0 if successful. -error, otherwise.
157 *
158 * Note that @offset + @size must not exceed the size of @buffer.
159 */
160int ion_sync(ion_client client, ion_buffer buffer);
161
162int ion_incRef(int fd, int share_fd, unsigned long **handle);
163
164int ion_decRef(int fd, unsigned long *handle);
165
166#ifdef __cplusplus
167}
168#endif
169#endif /* _LIB_ION_H_ */
170