blob: a6160718964a82bd680c7042d029f2140ec26bc4 [file] [log] [blame]
Dezhi Kong288546e2019-05-16 17:02:38 +08001/*
2 * ion.c
3 *
4 * Memory Allocator functions for ion
5 *
6 * Copyright 2011 Google, Inc
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#define LOG_TAG "ion"
21
22#include <errno.h>
23#include <fcntl.h>
24#include <linux/ion.h>
25#include <stdatomic.h>
26#include <stdio.h>
27#include <stdarg.h>
28#include <stdbool.h>
29#include <stddef.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/ioctl.h>
33#include <sys/mman.h>
34#include <sys/types.h>
35#include <unistd.h>
36
37#include <ion/ion.h>
38#include "ion_4.12.h"
39
40#ifndef __ANDROID__
41#define ALOGE(fmt,...) printf("[%s - %s:%d]" fmt "\n", __FILE__,__FUNCTION__, __LINE__, ##__VA_ARGS__)
42#endif
43
44enum ion_version { ION_VERSION_UNKNOWN, ION_VERSION_MODERN, ION_VERSION_LEGACY };
45
46static atomic_int g_ion_version = ATOMIC_VAR_INIT(ION_VERSION_UNKNOWN);
Cao Jian510569c2019-08-16 14:01:44 +080047#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
48#define VERSION_FILE "/proc/version"
49
50static int get_kernel_version(void)
51{
52 char *version_str[128];
53 int ret = -1;
54 int fd, version, patchlevel, sublevel;
55
56 fd = open(VERSION_FILE, O_RDONLY);
57 if (fd < 0) {
58 ALOGE("%s open failed\n", VERSION_FILE);
59 return -1;
60 }
61 ret = read(fd, version_str, 128);
62 if (ret < 0) {
63 ALOGE("%s read failed\n", VERSION_FILE);
64 return -1;
65 }
66
67 close(fd);
68
69 ret = sscanf((const char *)version_str,
70 "Linux version %d.%d.%d", &version,
71 &patchlevel, &sublevel);
72 if (ret != 3) {
73 ALOGE("sscanf error\n");
74 return -1;
75 }
76
77 return KERNEL_VERSION(version, patchlevel, sublevel);
78}
Dezhi Kong288546e2019-05-16 17:02:38 +080079
80int ion_is_legacy(int fd) {
81 int version = atomic_load_explicit(&g_ion_version, memory_order_acquire);
Cao Jian510569c2019-08-16 14:01:44 +080082 int kernel_version;
83
Dezhi Kong288546e2019-05-16 17:02:38 +080084 if (version == ION_VERSION_UNKNOWN) {
Cao Jian510569c2019-08-16 14:01:44 +080085 kernel_version = get_kernel_version();
86 if (kernel_version < 0) {
87 /**
88 * Check for FREE IOCTL here; it is available only in the old
89 * kernels, not the new ones.
90 */
91 struct ion_handle_data data = {
92 .handle = 0,
93 };
94 int err = ioctl(fd, ION_IOC_FREE, &data);
95 if (err < 0)
96 err = -errno;
97 version = (err == -ENOTTY) ?
98 ION_VERSION_MODERN : ION_VERSION_LEGACY;
99 } else {
100 version = (kernel_version >= KERNEL_VERSION(4, 12, 0)) ?
101 ION_VERSION_MODERN : ION_VERSION_LEGACY;
102 }
Dezhi Kong288546e2019-05-16 17:02:38 +0800103 atomic_store_explicit(&g_ion_version, version, memory_order_release);
104 }
Cao Jian510569c2019-08-16 14:01:44 +0800105
Dezhi Kong288546e2019-05-16 17:02:38 +0800106 return version == ION_VERSION_LEGACY;
107}
108
109int ion_open() {
110 int fd = open("/dev/ion", O_RDONLY | O_CLOEXEC);
111 if (fd < 0) ALOGE("open /dev/ion failed: %s", strerror(errno));
112
113 return fd;
114}
115
116int ion_close(int fd) {
117 int ret = close(fd);
118 if (ret < 0) return -errno;
119 return ret;
120}
121
122static int ion_ioctl(int fd, int req, void* arg) {
123 int ret = ioctl(fd, req, arg);
124 if (ret < 0) {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800125 ALOGE("ioctl %x failed with code %d: %s\n", req, ret, strerror(errno));
Dezhi Kong288546e2019-05-16 17:02:38 +0800126 return -errno;
127 }
128 return ret;
129}
130
131int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
132 ion_user_handle_t* handle) {
133 int ret = 0;
134
135 if ((handle == NULL) || (!ion_is_legacy(fd))) return -EINVAL;
136
137 struct ion_allocation_data data = {
138 .len = len, .align = align, .heap_id_mask = heap_mask, .flags = flags,
139 };
140
141 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
142 if (ret < 0) return ret;
143
144 *handle = data.handle;
145
146 return ret;
147}
148
149int ion_free(int fd, ion_user_handle_t handle) {
150 struct ion_handle_data data = {
151 .handle = handle,
152 };
153 return ion_ioctl(fd, ION_IOC_FREE, &data);
154}
155
156int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot, int flags, off_t offset,
157 unsigned char** ptr, int* map_fd) {
158 if (!ion_is_legacy(fd)) return -EINVAL;
159 int ret;
160 unsigned char* tmp_ptr;
161 struct ion_fd_data data = {
162 .handle = handle,
163 };
164
165 if (map_fd == NULL) return -EINVAL;
166 if (ptr == NULL) return -EINVAL;
167
168 ret = ion_ioctl(fd, ION_IOC_MAP, &data);
169 if (ret < 0) return ret;
170 if (data.fd < 0) {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800171 ALOGE("map ioctl returned negative fd\n");
Dezhi Kong288546e2019-05-16 17:02:38 +0800172 return -EINVAL;
173 }
174 tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset);
175 if (tmp_ptr == MAP_FAILED) {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800176 ALOGE("mmap failed: %s\n", strerror(errno));
Dezhi Kong288546e2019-05-16 17:02:38 +0800177 return -errno;
178 }
179 *map_fd = data.fd;
180 *ptr = tmp_ptr;
181 return ret;
182}
183
184int ion_share(int fd, ion_user_handle_t handle, int* share_fd) {
185 int ret;
186 struct ion_fd_data data = {
187 .handle = handle,
188 };
189
190 if (!ion_is_legacy(fd)) return -EINVAL;
191 if (share_fd == NULL) return -EINVAL;
192
193 ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
194 if (ret < 0) return ret;
195 if (data.fd < 0) {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800196 ALOGE("share ioctl returned negative fd\n");
Dezhi Kong288546e2019-05-16 17:02:38 +0800197 return -EINVAL;
198 }
199 *share_fd = data.fd;
200 return ret;
201}
202
203int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
204 int* handle_fd) {
205 ion_user_handle_t handle;
206 int ret;
207
208 if (!ion_is_legacy(fd)) {
209 struct ion_new_allocation_data data = {
210 .len = len,
211 .heap_id_mask = heap_mask,
212 .flags = flags,
213 };
214
215 ret = ion_ioctl(fd, ION_IOC_NEW_ALLOC, &data);
216 if (ret < 0) return ret;
217 *handle_fd = data.fd;
218 } else {
219 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
220 if (ret < 0) return ret;
221 ret = ion_share(fd, handle, handle_fd);
222 ion_free(fd, handle);
223 }
224 return ret;
225}
226
227int ion_import(int fd, int share_fd, ion_user_handle_t* handle) {
228 int ret;
229 struct ion_fd_data data = {
230 .fd = share_fd,
231 };
232
233 if (!ion_is_legacy(fd)) return -EINVAL;
234
235 if (handle == NULL) return -EINVAL;
236
237 ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
238 if (ret < 0) return ret;
239 *handle = data.handle;
240 return ret;
241}
242
243int ion_sync_fd(int fd, int handle_fd) {
244 struct ion_fd_data data = {
245 .fd = handle_fd,
246 };
247
248 if (!ion_is_legacy(fd)) return -EINVAL;
249
250 return ion_ioctl(fd, ION_IOC_SYNC, &data);
251}
252
253int ion_cache_invalid(int fd, int handle_fd)
254{
255 struct ion_fd_data data;
256 data.fd = handle_fd;
257 return ion_ioctl(fd, ION_IOC_INVALID_CACHE, &data);
258}
259
260int ion_query_heap_cnt(int fd, int* cnt) {
261 int ret;
262 struct ion_heap_query query;
263
264 memset(&query, 0, sizeof(query));
265
266 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
267 if (ret < 0) return ret;
268
269 *cnt = query.cnt;
270 return ret;
271}
272
273int ion_query_get_heaps(int fd, int cnt, void* buffers) {
274 int ret;
275 struct ion_heap_query query = {
Dezhi Konga80ba5f2020-03-11 17:18:25 +0800276 .cnt = cnt, .heaps = (unsigned long)buffers,
Dezhi Kong288546e2019-05-16 17:02:38 +0800277 };
278
279 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
280 return ret;
281}