libhwjpeg: resolve compilation errors
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos.git] / libion_exynos / libion.cpp
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 #include <ion.h>
18 #include <fcntl.h>
19 #include <sys/mman.h>
20 #include <sys/ioctl.h>
21 #include <cutils/log.h>
22
23 struct ion_allocation_data {
24 size_t len;
25 size_t align;
26 unsigned int heap_mask;
27 unsigned int flags;
28 ion_handle handle;
29 };
30
31 struct ion_fd_data {
32 ion_handle handle;
33 int fd;
34 };
35
36 struct ion_handle_data {
37 ion_handle handle;
38 };
39
40 struct ion_custom_data {
41 unsigned int cmd;
42 unsigned long arg;
43 };
44
45 struct ion_preload_data {
46 unsigned int heap_id_mask;
47 unsigned int flags;
48 unsigned int count;
49 struct ion_preload_object *obj; /* ion.h */
50 };
51
52 #define ION_IOC_MAGIC 'I'
53 #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, struct ion_allocation_data)
54 #define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
55 #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
56 #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
57 #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
58 #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
59 #define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
60 #define ION_IOC_PRELOAD _IOW (ION_IOC_MAGIC, 8, struct ion_preload_data)
61
62 struct ion_msync_data {
63 long flags;
64 ion_buffer buf;
65 size_t size;
66 off_t offset;
67 };
68
69 enum ION_EXYNOS_CUSTOM_CMD {
70 ION_EXYNOS_CUSTOM_MSYNC
71 };
72
73 ion_client ion_client_create(void)
74 {
75 return open("/dev/ion", O_RDWR);
76 }
77
78 void ion_client_destroy(ion_client client)
79 {
80 close(client);
81 }
82
83 ion_buffer ion_alloc(ion_client client, size_t len, size_t align,
84 unsigned int heap_mask, unsigned int flags)
85 {
86 int ret;
87 struct ion_handle_data arg_free;
88 struct ion_fd_data arg_share;
89 struct ion_allocation_data arg_alloc;
90
91 arg_alloc.len = len;
92 arg_alloc.align = align;
93 arg_alloc.heap_mask = heap_mask;
94 arg_alloc.flags = flags;
95
96 ret = ioctl(client, ION_IOC_ALLOC, &arg_alloc);
97 if (ret < 0)
98 return ret;
99
100 arg_share.handle = arg_alloc.handle;
101 ret = ioctl(client, ION_IOC_SHARE, &arg_share);
102
103 if ((ret >= 0) && (!arg_share.fd)) {
104 ret = ioctl(client, ION_IOC_SHARE, &arg_share);
105 if (ret >= 0)
106 close(0);
107 else
108 ret = 0;
109 }
110
111 arg_free.handle = arg_alloc.handle;
112 ioctl(client, ION_IOC_FREE, &arg_free);
113
114 if (ret < 0)
115 return ret;
116
117 return arg_share.fd;
118 }
119
120 void ion_free(ion_buffer buffer)
121 {
122 close(buffer);
123 }
124
125 void *ion_map(ion_buffer buffer, size_t len, off_t offset)
126 {
127 return mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
128 buffer, offset);
129 }
130
131 int ion_unmap(void *addr, size_t len)
132 {
133 return munmap(addr, len);
134 }
135
136 int ion_sync(ion_client client, ion_buffer buffer)
137 {
138 struct ion_fd_data data;
139
140 data.fd = buffer;
141
142 return ioctl(client, ION_IOC_SYNC, &data);
143 }
144
145 int ion_preload(ion_client client, unsigned int heap_mask, unsigned int flags,
146 int count, struct ion_preload_object objs[])
147 {
148 struct ion_preload_data data;
149
150 data.heap_id_mask = heap_mask;
151 data.flags = flags;
152 data.count = count;
153 data.obj = objs;
154
155 return ioctl(client, ION_IOC_PRELOAD, &data);
156 }
157
158 #define ION_EXYNOS_SYNC_BY_HANDLE 0x01 /* otherwise, by fd */
159 #define ION_EXYNOS_SYNC_INV 0x10 /* otherwise, clean */
160
161 struct ion_exynos_sync_data {
162 unsigned int flags;
163 union {
164 int dmabuf_fd;
165 ion_handle handle;
166 };
167 void *addr;
168 size_t size;
169 };
170 #define ION_IOC_EXYNOS_MAGIC 'E'
171
172 #define ION_IOC_EXYNOS_SYNC _IOW(ION_IOC_EXYNOS_MAGIC, 0, struct ion_exynos_sync_data)
173
174 int ion_sync_range(ion_client client, int dmabuf_fd, void *addr, size_t size)
175 {
176 ion_exynos_sync_data data;
177 ion_custom_data custom;
178
179 data.flags = 0;
180 data.dmabuf_fd = dmabuf_fd;
181 data.addr = addr;
182 data.size = size;
183
184 custom.cmd = ION_IOC_EXYNOS_SYNC;
185 custom.arg = reinterpret_cast<unsigned long>(&data);
186
187 return ioctl(client, ION_IOC_CUSTOM, &custom);
188 }
189
190 int ion_incRef(int fd, int share_fd, ion_handle *handle)
191 {
192 struct ion_fd_data data;
193
194 data.fd = share_fd;
195
196 int ret = ioctl(fd, ION_IOC_IMPORT, &data);
197 if (ret < 0)
198 return ret;
199
200 *handle = data.handle;
201 return ret;
202 }
203
204 int ion_decRef(int fd, ion_handle handle)
205 {
206 struct ion_handle_data data;
207 data.handle = handle;
208
209 return ioctl(fd, ION_IOC_FREE, &data);
210 }