Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 24 | #include <linux/ion.h> |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 25 | #include <stdatomic.h> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
Elliott Hughes | a744b05 | 2015-01-28 11:37:57 -0800 | [diff] [blame] | 27 | #include <string.h> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/types.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 32 | |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 33 | #include <ion/ion.h> |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 34 | #include "ion_4.12.h" |
| 35 | |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 36 | #include <log/log.h> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 37 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 38 | enum ion_version { ION_VERSION_UNKNOWN, ION_VERSION_MODERN, ION_VERSION_LEGACY }; |
| 39 | |
| 40 | static atomic_int g_ion_version = ATOMIC_VAR_INIT(ION_VERSION_UNKNOWN); |
| 41 | |
| 42 | int 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 | |
| 56 | int ion_open() { |
Nick Kralevich | 6b694a7 | 2016-11-29 15:20:18 -0800 | [diff] [blame] | 57 | int fd = open("/dev/ion", O_RDONLY | O_CLOEXEC); |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 58 | if (fd < 0) ALOGE("open /dev/ion failed!\n"); |
| 59 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 60 | return fd; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 63 | int ion_close(int fd) { |
Colin Cross | aab47b2 | 2013-12-18 15:17:21 -0800 | [diff] [blame] | 64 | int ret = close(fd); |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 65 | if (ret < 0) return -errno; |
Colin Cross | aab47b2 | 2013-12-18 15:17:21 -0800 | [diff] [blame] | 66 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 69 | static int ion_ioctl(int fd, int req, void* arg) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 70 | int ret = ioctl(fd, req, arg); |
| 71 | if (ret < 0) { |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 72 | ALOGE("ioctl %x failed with code %d: %s\n", req, ret, strerror(errno)); |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 73 | return -errno; |
| 74 | } |
| 75 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 78 | int 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 Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 84 | struct ion_allocation_data data = { |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 85 | .len = len, .align = align, .heap_id_mask = heap_mask, .flags = flags, |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 86 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 87 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 88 | ret = ion_ioctl(fd, ION_IOC_ALLOC, &data); |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 89 | if (ret < 0) return ret; |
| 90 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 91 | *handle = data.handle; |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 92 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 93 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 96 | int ion_free(int fd, ion_user_handle_t handle) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 97 | struct ion_handle_data data = { |
| 98 | .handle = handle, |
| 99 | }; |
| 100 | return ion_ioctl(fd, ION_IOC_FREE, &data); |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 103 | int 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 Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 106 | int ret; |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 107 | unsigned char* tmp_ptr; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 108 | struct ion_fd_data data = { |
| 109 | .handle = handle, |
| 110 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 111 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 112 | if (map_fd == NULL) return -EINVAL; |
| 113 | if (ptr == NULL) return -EINVAL; |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 114 | |
| 115 | ret = ion_ioctl(fd, ION_IOC_MAP, &data); |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 116 | if (ret < 0) return ret; |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 117 | if (data.fd < 0) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 118 | ALOGE("map ioctl returned negative fd\n"); |
| 119 | return -EINVAL; |
| 120 | } |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 121 | tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset); |
| 122 | if (tmp_ptr == MAP_FAILED) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 123 | ALOGE("mmap failed: %s\n", strerror(errno)); |
| 124 | return -errno; |
| 125 | } |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 126 | *map_fd = data.fd; |
| 127 | *ptr = tmp_ptr; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 128 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 131 | int ion_share(int fd, ion_user_handle_t handle, int* share_fd) { |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 132 | int ret; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 133 | struct ion_fd_data data = { |
| 134 | .handle = handle, |
| 135 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 136 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 137 | if (!ion_is_legacy(fd)) return -EINVAL; |
| 138 | if (share_fd == NULL) return -EINVAL; |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 139 | |
| 140 | ret = ion_ioctl(fd, ION_IOC_SHARE, &data); |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 141 | if (ret < 0) return ret; |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 142 | if (data.fd < 0) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 143 | ALOGE("share ioctl returned negative fd\n"); |
| 144 | return -EINVAL; |
| 145 | } |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 146 | *share_fd = data.fd; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 147 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 150 | int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags, |
| 151 | int* handle_fd) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 152 | ion_user_handle_t handle; |
| 153 | int ret; |
Rebecca Schultz Zavin | a50fd55 | 2012-06-11 15:12:37 -0700 | [diff] [blame] | 154 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 155 | 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 Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 171 | return ret; |
Rebecca Schultz Zavin | a50fd55 | 2012-06-11 15:12:37 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 174 | int ion_import(int fd, int share_fd, ion_user_handle_t* handle) { |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 175 | int ret; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 176 | struct ion_fd_data data = { |
| 177 | .fd = share_fd, |
| 178 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 179 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 180 | if (!ion_is_legacy(fd)) return -EINVAL; |
| 181 | |
| 182 | if (handle == NULL) return -EINVAL; |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 183 | |
| 184 | ret = ion_ioctl(fd, ION_IOC_IMPORT, &data); |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 185 | if (ret < 0) return ret; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 186 | *handle = data.handle; |
| 187 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 188 | } |
Rebecca Schultz Zavin | 3cfcc30 | 2012-09-20 09:46:19 -0700 | [diff] [blame] | 189 | |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 190 | int ion_sync_fd(int fd, int handle_fd) { |
Rebecca Schultz Zavin | 3cfcc30 | 2012-09-20 09:46:19 -0700 | [diff] [blame] | 191 | struct ion_fd_data data = { |
| 192 | .fd = handle_fd, |
| 193 | }; |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 194 | |
| 195 | if (!ion_is_legacy(fd)) return -EINVAL; |
| 196 | |
Rebecca Schultz Zavin | 3cfcc30 | 2012-09-20 09:46:19 -0700 | [diff] [blame] | 197 | return ion_ioctl(fd, ION_IOC_SYNC, &data); |
| 198 | } |
Laura Abbott | 30313f8 | 2017-06-30 11:25:50 +0530 | [diff] [blame] | 199 | |
| 200 | int 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 | |
| 213 | int 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 | } |