blob: 07b4caf33a0c35785954f7e4f4aefd89f8563a6f [file] [log] [blame]
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -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
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080022#include <errno.h>
23#include <fcntl.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070024#include <linux/ion.h>
Laura Abbott30313f82017-06-30 11:25:50 +053025#include <stdatomic.h>
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080026#include <stdio.h>
Elliott Hughesa744b052015-01-28 11:37:57 -080027#include <string.h>
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080028#include <sys/ioctl.h>
29#include <sys/mman.h>
30#include <sys/types.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070031#include <unistd.h>
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080032
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080033#include <ion/ion.h>
Hridya Valsarajuf142c4d2019-10-08 12:54:13 -070034#include <linux/ion_4.19.h>
Laura Abbott30313f82017-06-30 11:25:50 +053035
Mark Salyzyn30f991f2017-01-10 13:19:54 -080036#include <log/log.h>
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080037
Hridya Valsarajud43e7982019-09-06 12:20:32 -070038#define ION_ABI_VERSION_MODULAR_HEAPS 2
39
Laura Abbott30313f82017-06-30 11:25:50 +053040enum ion_version { ION_VERSION_UNKNOWN, ION_VERSION_MODERN, ION_VERSION_LEGACY };
41
42static atomic_int g_ion_version = ATOMIC_VAR_INIT(ION_VERSION_UNKNOWN);
43
44int ion_is_legacy(int fd) {
45 int version = atomic_load_explicit(&g_ion_version, memory_order_acquire);
46 if (version == ION_VERSION_UNKNOWN) {
47 /**
48 * Check for FREE IOCTL here; it is available only in the old
49 * kernels, not the new ones.
50 */
51 int err = ion_free(fd, (ion_user_handle_t)NULL);
52 version = (err == -ENOTTY) ? ION_VERSION_MODERN : ION_VERSION_LEGACY;
53 atomic_store_explicit(&g_ion_version, version, memory_order_release);
54 }
55 return version == ION_VERSION_LEGACY;
56}
57
58int ion_open() {
Nick Kralevich6b694a72016-11-29 15:20:18 -080059 int fd = open("/dev/ion", O_RDONLY | O_CLOEXEC);
Mark Salyzyn5be32c32018-03-07 06:51:03 -080060 if (fd < 0) ALOGE("open /dev/ion failed: %s", strerror(errno));
Laura Abbott30313f82017-06-30 11:25:50 +053061
Colin Cross92d7ca62013-11-08 19:04:18 -080062 return fd;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080063}
64
Laura Abbott30313f82017-06-30 11:25:50 +053065int ion_close(int fd) {
Colin Crossaab47b22013-12-18 15:17:21 -080066 int ret = close(fd);
Laura Abbott30313f82017-06-30 11:25:50 +053067 if (ret < 0) return -errno;
Colin Crossaab47b22013-12-18 15:17:21 -080068 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080069}
70
Laura Abbott30313f82017-06-30 11:25:50 +053071static int ion_ioctl(int fd, int req, void* arg) {
Colin Cross92d7ca62013-11-08 19:04:18 -080072 int ret = ioctl(fd, req, arg);
73 if (ret < 0) {
Mark Salyzyn5be32c32018-03-07 06:51:03 -080074 ALOGE("ioctl %x failed with code %d: %s", req, ret, strerror(errno));
Colin Cross92d7ca62013-11-08 19:04:18 -080075 return -errno;
76 }
77 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080078}
79
Hridya Valsarajud43e7982019-09-06 12:20:32 -070080int ion_is_using_modular_heaps(int fd) {
81 int ion_abi_version = 0;
82 int ret = 0;
83
84 ret = ion_ioctl(fd, ION_IOC_ABI_VERSION, &ion_abi_version);
85 return (ret == 0 && ion_abi_version >= ION_ABI_VERSION_MODULAR_HEAPS);
86}
87
Laura Abbott30313f82017-06-30 11:25:50 +053088int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
89 ion_user_handle_t* handle) {
90 int ret = 0;
91
92 if ((handle == NULL) || (!ion_is_legacy(fd))) return -EINVAL;
93
Colin Cross92d7ca62013-11-08 19:04:18 -080094 struct ion_allocation_data data = {
Laura Abbott30313f82017-06-30 11:25:50 +053095 .len = len, .align = align, .heap_id_mask = heap_mask, .flags = flags,
Colin Cross92d7ca62013-11-08 19:04:18 -080096 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080097
Colin Cross92d7ca62013-11-08 19:04:18 -080098 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
Laura Abbott30313f82017-06-30 11:25:50 +053099 if (ret < 0) return ret;
100
Colin Cross92d7ca62013-11-08 19:04:18 -0800101 *handle = data.handle;
Laura Abbott30313f82017-06-30 11:25:50 +0530102
Colin Cross92d7ca62013-11-08 19:04:18 -0800103 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800104}
105
Laura Abbott30313f82017-06-30 11:25:50 +0530106int ion_free(int fd, ion_user_handle_t handle) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800107 struct ion_handle_data data = {
108 .handle = handle,
109 };
110 return ion_ioctl(fd, ION_IOC_FREE, &data);
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800111}
112
Laura Abbott30313f82017-06-30 11:25:50 +0530113int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot, int flags, off_t offset,
114 unsigned char** ptr, int* map_fd) {
115 if (!ion_is_legacy(fd)) return -EINVAL;
Colin Cross74963182013-11-08 19:07:38 -0800116 int ret;
Laura Abbott30313f82017-06-30 11:25:50 +0530117 unsigned char* tmp_ptr;
Colin Cross92d7ca62013-11-08 19:04:18 -0800118 struct ion_fd_data data = {
119 .handle = handle,
120 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800121
Laura Abbott30313f82017-06-30 11:25:50 +0530122 if (map_fd == NULL) return -EINVAL;
123 if (ptr == NULL) return -EINVAL;
Colin Cross74963182013-11-08 19:07:38 -0800124
125 ret = ion_ioctl(fd, ION_IOC_MAP, &data);
Laura Abbott30313f82017-06-30 11:25:50 +0530126 if (ret < 0) return ret;
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700127 if (data.fd < 0) {
Mark Salyzyn5be32c32018-03-07 06:51:03 -0800128 ALOGE("map ioctl returned negative fd");
Colin Cross92d7ca62013-11-08 19:04:18 -0800129 return -EINVAL;
130 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700131 tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset);
132 if (tmp_ptr == MAP_FAILED) {
Mark Salyzyn5be32c32018-03-07 06:51:03 -0800133 ALOGE("mmap failed: %s", strerror(errno));
Colin Cross92d7ca62013-11-08 19:04:18 -0800134 return -errno;
135 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700136 *map_fd = data.fd;
137 *ptr = tmp_ptr;
Colin Cross92d7ca62013-11-08 19:04:18 -0800138 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800139}
140
Laura Abbott30313f82017-06-30 11:25:50 +0530141int ion_share(int fd, ion_user_handle_t handle, int* share_fd) {
Colin Cross74963182013-11-08 19:07:38 -0800142 int ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800143 struct ion_fd_data data = {
144 .handle = handle,
145 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800146
Laura Abbott30313f82017-06-30 11:25:50 +0530147 if (!ion_is_legacy(fd)) return -EINVAL;
148 if (share_fd == NULL) return -EINVAL;
Colin Cross74963182013-11-08 19:07:38 -0800149
150 ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
Laura Abbott30313f82017-06-30 11:25:50 +0530151 if (ret < 0) return ret;
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700152 if (data.fd < 0) {
Mark Salyzyn5be32c32018-03-07 06:51:03 -0800153 ALOGE("share ioctl returned negative fd");
Colin Cross92d7ca62013-11-08 19:04:18 -0800154 return -EINVAL;
155 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700156 *share_fd = data.fd;
Colin Cross92d7ca62013-11-08 19:04:18 -0800157 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800158}
159
Laura Abbott30313f82017-06-30 11:25:50 +0530160int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
161 int* handle_fd) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800162 ion_user_handle_t handle;
163 int ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700164
Sandeep Patil89ea2812019-07-15 17:29:33 -0700165 if (!handle_fd) return -EINVAL;
166
Laura Abbott30313f82017-06-30 11:25:50 +0530167 if (!ion_is_legacy(fd)) {
168 struct ion_new_allocation_data data = {
169 .len = len,
170 .heap_id_mask = heap_mask,
171 .flags = flags,
172 };
173
174 ret = ion_ioctl(fd, ION_IOC_NEW_ALLOC, &data);
175 if (ret < 0) return ret;
176 *handle_fd = data.fd;
177 } else {
178 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
179 if (ret < 0) return ret;
180 ret = ion_share(fd, handle, handle_fd);
181 ion_free(fd, handle);
182 }
Colin Cross92d7ca62013-11-08 19:04:18 -0800183 return ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700184}
185
Laura Abbott30313f82017-06-30 11:25:50 +0530186int ion_import(int fd, int share_fd, ion_user_handle_t* handle) {
Colin Cross74963182013-11-08 19:07:38 -0800187 int ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800188 struct ion_fd_data data = {
189 .fd = share_fd,
190 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800191
Laura Abbott30313f82017-06-30 11:25:50 +0530192 if (!ion_is_legacy(fd)) return -EINVAL;
193
194 if (handle == NULL) return -EINVAL;
Colin Cross74963182013-11-08 19:07:38 -0800195
196 ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
Laura Abbott30313f82017-06-30 11:25:50 +0530197 if (ret < 0) return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800198 *handle = data.handle;
199 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800200}
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700201
Laura Abbott30313f82017-06-30 11:25:50 +0530202int ion_sync_fd(int fd, int handle_fd) {
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700203 struct ion_fd_data data = {
204 .fd = handle_fd,
205 };
Laura Abbott30313f82017-06-30 11:25:50 +0530206
207 if (!ion_is_legacy(fd)) return -EINVAL;
208
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700209 return ion_ioctl(fd, ION_IOC_SYNC, &data);
210}
Laura Abbott30313f82017-06-30 11:25:50 +0530211
212int ion_query_heap_cnt(int fd, int* cnt) {
213 int ret;
214 struct ion_heap_query query;
215
Sandeep Patil89ea2812019-07-15 17:29:33 -0700216 if (!cnt) return -EINVAL;
Laura Abbott30313f82017-06-30 11:25:50 +0530217 memset(&query, 0, sizeof(query));
218
219 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
220 if (ret < 0) return ret;
221
222 *cnt = query.cnt;
223 return ret;
224}
225
226int ion_query_get_heaps(int fd, int cnt, void* buffers) {
227 int ret;
228 struct ion_heap_query query = {
229 .cnt = cnt, .heaps = (uintptr_t)buffers,
230 };
231
232 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
233 return ret;
234}