blob: 583612854de327f62c8a5e984c006f61570065d9 [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>
Laura Abbott30313f82017-06-30 11:25:50 +053034#include "ion_4.12.h"
35
Mark Salyzyn30f991f2017-01-10 13:19:54 -080036#include <log/log.h>
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080037
Laura Abbott30313f82017-06-30 11:25:50 +053038enum ion_version { ION_VERSION_UNKNOWN, ION_VERSION_MODERN, ION_VERSION_LEGACY };
39
40static atomic_int g_ion_version = ATOMIC_VAR_INIT(ION_VERSION_UNKNOWN);
41
42int ion_is_legacy(int fd) {
43 int version = atomic_load_explicit(&g_ion_version, memory_order_acquire);
44 if (version == ION_VERSION_UNKNOWN) {
45 /**
46 * Check for FREE IOCTL here; it is available only in the old
47 * kernels, not the new ones.
48 */
49 int err = ion_free(fd, (ion_user_handle_t)NULL);
50 version = (err == -ENOTTY) ? ION_VERSION_MODERN : ION_VERSION_LEGACY;
51 atomic_store_explicit(&g_ion_version, version, memory_order_release);
52 }
53 return version == ION_VERSION_LEGACY;
54}
55
56int ion_open() {
Nick Kralevich6b694a72016-11-29 15:20:18 -080057 int fd = open("/dev/ion", O_RDONLY | O_CLOEXEC);
Laura Abbott30313f82017-06-30 11:25:50 +053058 if (fd < 0) ALOGE("open /dev/ion failed!\n");
59
Colin Cross92d7ca62013-11-08 19:04:18 -080060 return fd;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080061}
62
Laura Abbott30313f82017-06-30 11:25:50 +053063int ion_close(int fd) {
Colin Crossaab47b22013-12-18 15:17:21 -080064 int ret = close(fd);
Laura Abbott30313f82017-06-30 11:25:50 +053065 if (ret < 0) return -errno;
Colin Crossaab47b22013-12-18 15:17:21 -080066 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080067}
68
Laura Abbott30313f82017-06-30 11:25:50 +053069static int ion_ioctl(int fd, int req, void* arg) {
Colin Cross92d7ca62013-11-08 19:04:18 -080070 int ret = ioctl(fd, req, arg);
71 if (ret < 0) {
Laura Abbott30313f82017-06-30 11:25:50 +053072 ALOGE("ioctl %x failed with code %d: %s\n", req, ret, strerror(errno));
Colin Cross92d7ca62013-11-08 19:04:18 -080073 return -errno;
74 }
75 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080076}
77
Laura Abbott30313f82017-06-30 11:25:50 +053078int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
79 ion_user_handle_t* handle) {
80 int ret = 0;
81
82 if ((handle == NULL) || (!ion_is_legacy(fd))) return -EINVAL;
83
Colin Cross92d7ca62013-11-08 19:04:18 -080084 struct ion_allocation_data data = {
Laura Abbott30313f82017-06-30 11:25:50 +053085 .len = len, .align = align, .heap_id_mask = heap_mask, .flags = flags,
Colin Cross92d7ca62013-11-08 19:04:18 -080086 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080087
Colin Cross92d7ca62013-11-08 19:04:18 -080088 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
Laura Abbott30313f82017-06-30 11:25:50 +053089 if (ret < 0) return ret;
90
Colin Cross92d7ca62013-11-08 19:04:18 -080091 *handle = data.handle;
Laura Abbott30313f82017-06-30 11:25:50 +053092
Colin Cross92d7ca62013-11-08 19:04:18 -080093 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080094}
95
Laura Abbott30313f82017-06-30 11:25:50 +053096int ion_free(int fd, ion_user_handle_t handle) {
Colin Cross92d7ca62013-11-08 19:04:18 -080097 struct ion_handle_data data = {
98 .handle = handle,
99 };
100 return ion_ioctl(fd, ION_IOC_FREE, &data);
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800101}
102
Laura Abbott30313f82017-06-30 11:25:50 +0530103int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot, int flags, off_t offset,
104 unsigned char** ptr, int* map_fd) {
105 if (!ion_is_legacy(fd)) return -EINVAL;
Colin Cross74963182013-11-08 19:07:38 -0800106 int ret;
Laura Abbott30313f82017-06-30 11:25:50 +0530107 unsigned char* tmp_ptr;
Colin Cross92d7ca62013-11-08 19:04:18 -0800108 struct ion_fd_data data = {
109 .handle = handle,
110 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800111
Laura Abbott30313f82017-06-30 11:25:50 +0530112 if (map_fd == NULL) return -EINVAL;
113 if (ptr == NULL) return -EINVAL;
Colin Cross74963182013-11-08 19:07:38 -0800114
115 ret = ion_ioctl(fd, ION_IOC_MAP, &data);
Laura Abbott30313f82017-06-30 11:25:50 +0530116 if (ret < 0) return ret;
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700117 if (data.fd < 0) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800118 ALOGE("map ioctl returned negative fd\n");
119 return -EINVAL;
120 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700121 tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset);
122 if (tmp_ptr == MAP_FAILED) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800123 ALOGE("mmap failed: %s\n", strerror(errno));
124 return -errno;
125 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700126 *map_fd = data.fd;
127 *ptr = tmp_ptr;
Colin Cross92d7ca62013-11-08 19:04:18 -0800128 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800129}
130
Laura Abbott30313f82017-06-30 11:25:50 +0530131int ion_share(int fd, ion_user_handle_t handle, int* share_fd) {
Colin Cross74963182013-11-08 19:07:38 -0800132 int ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800133 struct ion_fd_data data = {
134 .handle = handle,
135 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800136
Laura Abbott30313f82017-06-30 11:25:50 +0530137 if (!ion_is_legacy(fd)) return -EINVAL;
138 if (share_fd == NULL) return -EINVAL;
Colin Cross74963182013-11-08 19:07:38 -0800139
140 ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
Laura Abbott30313f82017-06-30 11:25:50 +0530141 if (ret < 0) return ret;
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700142 if (data.fd < 0) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800143 ALOGE("share ioctl returned negative fd\n");
144 return -EINVAL;
145 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700146 *share_fd = data.fd;
Colin Cross92d7ca62013-11-08 19:04:18 -0800147 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800148}
149
Laura Abbott30313f82017-06-30 11:25:50 +0530150int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags,
151 int* handle_fd) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800152 ion_user_handle_t handle;
153 int ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700154
Laura Abbott30313f82017-06-30 11:25:50 +0530155 if (!ion_is_legacy(fd)) {
156 struct ion_new_allocation_data data = {
157 .len = len,
158 .heap_id_mask = heap_mask,
159 .flags = flags,
160 };
161
162 ret = ion_ioctl(fd, ION_IOC_NEW_ALLOC, &data);
163 if (ret < 0) return ret;
164 *handle_fd = data.fd;
165 } else {
166 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
167 if (ret < 0) return ret;
168 ret = ion_share(fd, handle, handle_fd);
169 ion_free(fd, handle);
170 }
Colin Cross92d7ca62013-11-08 19:04:18 -0800171 return ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700172}
173
Laura Abbott30313f82017-06-30 11:25:50 +0530174int ion_import(int fd, int share_fd, ion_user_handle_t* handle) {
Colin Cross74963182013-11-08 19:07:38 -0800175 int ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800176 struct ion_fd_data data = {
177 .fd = share_fd,
178 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800179
Laura Abbott30313f82017-06-30 11:25:50 +0530180 if (!ion_is_legacy(fd)) return -EINVAL;
181
182 if (handle == NULL) return -EINVAL;
Colin Cross74963182013-11-08 19:07:38 -0800183
184 ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
Laura Abbott30313f82017-06-30 11:25:50 +0530185 if (ret < 0) return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800186 *handle = data.handle;
187 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800188}
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700189
Laura Abbott30313f82017-06-30 11:25:50 +0530190int ion_sync_fd(int fd, int handle_fd) {
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700191 struct ion_fd_data data = {
192 .fd = handle_fd,
193 };
Laura Abbott30313f82017-06-30 11:25:50 +0530194
195 if (!ion_is_legacy(fd)) return -EINVAL;
196
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700197 return ion_ioctl(fd, ION_IOC_SYNC, &data);
198}
Laura Abbott30313f82017-06-30 11:25:50 +0530199
200int ion_query_heap_cnt(int fd, int* cnt) {
201 int ret;
202 struct ion_heap_query query;
203
204 memset(&query, 0, sizeof(query));
205
206 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
207 if (ret < 0) return ret;
208
209 *cnt = query.cnt;
210 return ret;
211}
212
213int ion_query_get_heaps(int fd, int cnt, void* buffers) {
214 int ret;
215 struct ion_heap_query query = {
216 .cnt = cnt, .heaps = (uintptr_t)buffers,
217 };
218
219 ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
220 return ret;
221}