blob: 7799e2bf0bbbe2f180ce9798c3038274fcfcb551 [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;
Yongjie Zhu7f547df2022-01-11 15:35:19 +080060 } else {
61 ret = read(fd, version_str, 128);
62 if (ret < 0) {
63 close(fd);
64 ALOGE("%s read failed\n", VERSION_FILE);
65 return -1;
66 }
Cao Jian510569c2019-08-16 14:01:44 +080067 }
Cao Jian510569c2019-08-16 14:01:44 +080068
69 ret = sscanf((const char *)version_str,
70 "Linux version %d.%d.%d", &version,
71 &patchlevel, &sublevel);
72 if (ret != 3) {
Yongjie Zhu7f547df2022-01-11 15:35:19 +080073 close(fd);
Cao Jian510569c2019-08-16 14:01:44 +080074 ALOGE("sscanf error\n");
75 return -1;
76 }
77
Yongjie Zhu7f547df2022-01-11 15:35:19 +080078 close(fd);
Cao Jian510569c2019-08-16 14:01:44 +080079 return KERNEL_VERSION(version, patchlevel, sublevel);
80}
Dezhi Kong288546e2019-05-16 17:02:38 +080081
82int ion_is_legacy(int fd) {
83 int version = atomic_load_explicit(&g_ion_version, memory_order_acquire);
Cao Jian510569c2019-08-16 14:01:44 +080084 int kernel_version;
85
Dezhi Kong288546e2019-05-16 17:02:38 +080086 if (version == ION_VERSION_UNKNOWN) {
Cao Jian510569c2019-08-16 14:01:44 +080087 kernel_version = get_kernel_version();
88 if (kernel_version < 0) {
89 /**
90 * Check for FREE IOCTL here; it is available only in the old
91 * kernels, not the new ones.
92 */
93 struct ion_handle_data data = {
94 .handle = 0,
95 };
96 int err = ioctl(fd, ION_IOC_FREE, &data);
97 if (err < 0)
98 err = -errno;
99 version = (err == -ENOTTY) ?
100 ION_VERSION_MODERN : ION_VERSION_LEGACY;
101 } else {
102 version = (kernel_version >= KERNEL_VERSION(4, 12, 0)) ?
103 ION_VERSION_MODERN : ION_VERSION_LEGACY;
104 }
Dezhi Kong288546e2019-05-16 17:02:38 +0800105 atomic_store_explicit(&g_ion_version, version, memory_order_release);
106 }
Cao Jian510569c2019-08-16 14:01:44 +0800107
Dezhi Kong288546e2019-05-16 17:02:38 +0800108 return version == ION_VERSION_LEGACY;
109}
110
111int ion_open() {
112 int fd = open("/dev/ion", O_RDONLY | O_CLOEXEC);
113 if (fd < 0) ALOGE("open /dev/ion failed: %s", strerror(errno));
114
115 return fd;
116}
117
118int ion_close(int fd) {
119 int ret = close(fd);
120 if (ret < 0) return -errno;
121 return ret;
122}
123
124static int ion_ioctl(int fd, int req, void* arg) {
125 int ret = ioctl(fd, req, arg);
126 if (ret < 0) {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800127 ALOGE("ioctl %x failed with code %d: %s\n", req, ret, strerror(errno));
Dezhi Kong288546e2019-05-16 17:02:38 +0800128 return -errno;
129 }
130 return ret;
131}
132
133int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
134 ion_user_handle_t* handle) {
135 int ret = 0;
136
137 if ((handle == NULL) || (!ion_is_legacy(fd))) return -EINVAL;
138
139 struct ion_allocation_data data = {
140 .len = len, .align = align, .heap_id_mask = heap_mask, .flags = flags,
141 };
142
143 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
144 if (ret < 0) return ret;
145
146 *handle = data.handle;
147
148 return ret;
149}
150
151int ion_free(int fd, ion_user_handle_t handle) {
152 struct ion_handle_data data = {
153 .handle = handle,
154 };
155 return ion_ioctl(fd, ION_IOC_FREE, &data);
156}
157
158int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot, int flags, off_t offset,
159 unsigned char** ptr, int* map_fd) {
160 if (!ion_is_legacy(fd)) return -EINVAL;
161 int ret;
162 unsigned char* tmp_ptr;
163 struct ion_fd_data data = {
164 .handle = handle,
165 };
166
167 if (map_fd == NULL) return -EINVAL;
168 if (ptr == NULL) return -EINVAL;
169
170 ret = ion_ioctl(fd, ION_IOC_MAP, &data);
171 if (ret < 0) return ret;
172 if (data.fd < 0) {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800173 ALOGE("map ioctl returned negative fd\n");
Dezhi Kong288546e2019-05-16 17:02:38 +0800174 return -EINVAL;
175 }
176 tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset);
177 if (tmp_ptr == MAP_FAILED) {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800178 ALOGE("mmap failed: %s\n", strerror(errno));
Dezhi Kong288546e2019-05-16 17:02:38 +0800179 return -errno;
180 }
181 *map_fd = data.fd;
182 *ptr = tmp_ptr;
183 return ret;
184}
185
186int ion_share(int fd, ion_user_handle_t handle, int* share_fd) {
187 int ret;
188 struct ion_fd_data data = {
189 .handle = handle,
190 };
191
192 if (!ion_is_legacy(fd)) return -EINVAL;
193 if (share_fd == NULL) return -EINVAL;
194
195 ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
196 if (ret < 0) return ret;
197 if (data.fd < 0) {
Dezhi Kong528d8cc2019-07-17 21:09:07 +0800198 ALOGE("share ioctl returned negative fd\n");
Dezhi Kong288546e2019-05-16 17:02:38 +0800199 return -EINVAL;
200 }
201 *share_fd = data.fd;
202 return ret;
203}
204
205int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
206 int* handle_fd) {
207 ion_user_handle_t handle;
208 int ret;
209
210 if (!ion_is_legacy(fd)) {
211 struct ion_new_allocation_data data = {
212 .len = len,
213 .heap_id_mask = heap_mask,
214 .flags = flags,
215 };
216
217 ret = ion_ioctl(fd, ION_IOC_NEW_ALLOC, &data);
218 if (ret < 0) return ret;
219 *handle_fd = data.fd;
220 } else {
221 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
222 if (ret < 0) return ret;
223 ret = ion_share(fd, handle, handle_fd);
224 ion_free(fd, handle);
225 }
226 return ret;
227}
228
229int ion_import(int fd, int share_fd, ion_user_handle_t* handle) {
230 int ret;
231 struct ion_fd_data data = {
232 .fd = share_fd,
233 };
234
235 if (!ion_is_legacy(fd)) return -EINVAL;
236
237 if (handle == NULL) return -EINVAL;
238
239 ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
240 if (ret < 0) return ret;
241 *handle = data.handle;
242 return ret;
243}
244
245int ion_sync_fd(int fd, int handle_fd) {
246 struct ion_fd_data data = {
247 .fd = handle_fd,
248 };
249
250 if (!ion_is_legacy(fd)) return -EINVAL;
251
252 return ion_ioctl(fd, ION_IOC_SYNC, &data);
253}
254
255int ion_cache_invalid(int fd, int handle_fd)
256{
257 struct ion_fd_data data;
258 data.fd = handle_fd;
259 return ion_ioctl(fd, ION_IOC_INVALID_CACHE, &data);
260}
261
262int ion_query_heap_cnt(int fd, int* cnt) {
263 int ret;
264 struct ion_heap_query query;
265
266 memset(&query, 0, sizeof(query));
267
268 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
269 if (ret < 0) return ret;
270
271 *cnt = query.cnt;
272 return ret;
273}
274
275int ion_query_get_heaps(int fd, int cnt, void* buffers) {
276 int ret;
277 struct ion_heap_query query = {
Dezhi Konga80ba5f2020-03-11 17:18:25 +0800278 .cnt = cnt, .heaps = (unsigned long)buffers,
Dezhi Kong288546e2019-05-16 17:02:38 +0800279 };
280
281 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
282 return ret;
283}