blob: 16618453a54d6452c1753f2db646be0696dc18a2 [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);
47
48int ion_is_legacy(int fd) {
49 int version = atomic_load_explicit(&g_ion_version, memory_order_acquire);
50 if (version == ION_VERSION_UNKNOWN) {
51 /**
52 * Check for FREE IOCTL here; it is available only in the old
53 * kernels, not the new ones.
54 */
55 struct ion_handle_data data = {
56 .handle = 0,
57 };
58 int err = ioctl(fd, ION_IOC_FREE, &data);
59 version = (err == -ENOTTY) ? ION_VERSION_MODERN : ION_VERSION_LEGACY;
60 atomic_store_explicit(&g_ion_version, version, memory_order_release);
61 }
62 return version == ION_VERSION_LEGACY;
63}
64
65int ion_open() {
66 int fd = open("/dev/ion", O_RDONLY | O_CLOEXEC);
67 if (fd < 0) ALOGE("open /dev/ion failed: %s", strerror(errno));
68
69 return fd;
70}
71
72int ion_close(int fd) {
73 int ret = close(fd);
74 if (ret < 0) return -errno;
75 return ret;
76}
77
78static int ion_ioctl(int fd, int req, void* arg) {
79 int ret = ioctl(fd, req, arg);
80 if (ret < 0) {
81 ALOGE("ioctl %x failed with code %d: %s", req, ret, strerror(errno));
82 return -errno;
83 }
84 return ret;
85}
86
87int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
88 ion_user_handle_t* handle) {
89 int ret = 0;
90
91 if ((handle == NULL) || (!ion_is_legacy(fd))) return -EINVAL;
92
93 struct ion_allocation_data data = {
94 .len = len, .align = align, .heap_id_mask = heap_mask, .flags = flags,
95 };
96
97 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
98 if (ret < 0) return ret;
99
100 *handle = data.handle;
101
102 return ret;
103}
104
105int ion_free(int fd, ion_user_handle_t handle) {
106 struct ion_handle_data data = {
107 .handle = handle,
108 };
109 return ion_ioctl(fd, ION_IOC_FREE, &data);
110}
111
112int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot, int flags, off_t offset,
113 unsigned char** ptr, int* map_fd) {
114 if (!ion_is_legacy(fd)) return -EINVAL;
115 int ret;
116 unsigned char* tmp_ptr;
117 struct ion_fd_data data = {
118 .handle = handle,
119 };
120
121 if (map_fd == NULL) return -EINVAL;
122 if (ptr == NULL) return -EINVAL;
123
124 ret = ion_ioctl(fd, ION_IOC_MAP, &data);
125 if (ret < 0) return ret;
126 if (data.fd < 0) {
127 ALOGE("map ioctl returned negative fd");
128 return -EINVAL;
129 }
130 tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset);
131 if (tmp_ptr == MAP_FAILED) {
132 ALOGE("mmap failed: %s", strerror(errno));
133 return -errno;
134 }
135 *map_fd = data.fd;
136 *ptr = tmp_ptr;
137 return ret;
138}
139
140int ion_share(int fd, ion_user_handle_t handle, int* share_fd) {
141 int ret;
142 struct ion_fd_data data = {
143 .handle = handle,
144 };
145
146 if (!ion_is_legacy(fd)) return -EINVAL;
147 if (share_fd == NULL) return -EINVAL;
148
149 ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
150 if (ret < 0) return ret;
151 if (data.fd < 0) {
152 ALOGE("share ioctl returned negative fd");
153 return -EINVAL;
154 }
155 *share_fd = data.fd;
156 return ret;
157}
158
159int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
160 int* handle_fd) {
161 ion_user_handle_t handle;
162 int ret;
163
164 if (!ion_is_legacy(fd)) {
165 struct ion_new_allocation_data data = {
166 .len = len,
167 .heap_id_mask = heap_mask,
168 .flags = flags,
169 };
170
171 ret = ion_ioctl(fd, ION_IOC_NEW_ALLOC, &data);
172 if (ret < 0) return ret;
173 *handle_fd = data.fd;
174 } else {
175 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
176 if (ret < 0) return ret;
177 ret = ion_share(fd, handle, handle_fd);
178 ion_free(fd, handle);
179 }
180 return ret;
181}
182
183int ion_import(int fd, int share_fd, ion_user_handle_t* handle) {
184 int ret;
185 struct ion_fd_data data = {
186 .fd = share_fd,
187 };
188
189 if (!ion_is_legacy(fd)) return -EINVAL;
190
191 if (handle == NULL) return -EINVAL;
192
193 ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
194 if (ret < 0) return ret;
195 *handle = data.handle;
196 return ret;
197}
198
199int ion_sync_fd(int fd, int handle_fd) {
200 struct ion_fd_data data = {
201 .fd = handle_fd,
202 };
203
204 if (!ion_is_legacy(fd)) return -EINVAL;
205
206 return ion_ioctl(fd, ION_IOC_SYNC, &data);
207}
208
209int ion_cache_invalid(int fd, int handle_fd)
210{
211 struct ion_fd_data data;
212 data.fd = handle_fd;
213 return ion_ioctl(fd, ION_IOC_INVALID_CACHE, &data);
214}
215
216int ion_query_heap_cnt(int fd, int* cnt) {
217 int ret;
218 struct ion_heap_query query;
219
220 memset(&query, 0, sizeof(query));
221
222 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
223 if (ret < 0) return ret;
224
225 *cnt = query.cnt;
226 return ret;
227}
228
229int ion_query_get_heaps(int fd, int cnt, void* buffers) {
230 int ret;
231 struct ion_heap_query query = {
232 .cnt = cnt, .heaps = (long)buffers,
233 };
234
235 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
236 return ret;
237}