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> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Elliott Hughes | a744b05 | 2015-01-28 11:37:57 -0800 | [diff] [blame] | 26 | #include <string.h> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <sys/types.h> |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 31 | |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 32 | #include <android/log.h> |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 33 | #include <ion/ion.h> |
| 34 | |
| 35 | int ion_open() |
| 36 | { |
Nick Kralevich | 6b694a7 | 2016-11-29 15:20:18 -0800 | [diff] [blame] | 37 | int fd = open("/dev/ion", O_RDONLY | O_CLOEXEC); |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 38 | if (fd < 0) |
| 39 | ALOGE("open /dev/ion failed!\n"); |
| 40 | return fd; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int ion_close(int fd) |
| 44 | { |
Colin Cross | aab47b2 | 2013-12-18 15:17:21 -0800 | [diff] [blame] | 45 | int ret = close(fd); |
| 46 | if (ret < 0) |
| 47 | return -errno; |
| 48 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int ion_ioctl(int fd, int req, void *arg) |
| 52 | { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 53 | int ret = ioctl(fd, req, arg); |
| 54 | if (ret < 0) { |
| 55 | ALOGE("ioctl %x failed with code %d: %s\n", req, |
| 56 | ret, strerror(errno)); |
| 57 | return -errno; |
| 58 | } |
| 59 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Rebecca Schultz Zavin | a50fd55 | 2012-06-11 15:12:37 -0700 | [diff] [blame] | 62 | int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask, |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 63 | unsigned int flags, ion_user_handle_t *handle) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 64 | { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 65 | int ret; |
| 66 | struct ion_allocation_data data = { |
| 67 | .len = len, |
| 68 | .align = align, |
| 69 | .heap_id_mask = heap_mask, |
| 70 | .flags = flags, |
| 71 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 72 | |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 73 | if (handle == NULL) |
| 74 | return -EINVAL; |
| 75 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 76 | ret = ion_ioctl(fd, ION_IOC_ALLOC, &data); |
| 77 | if (ret < 0) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 78 | return ret; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 79 | *handle = data.handle; |
| 80 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Rom Lemarchand | 969eac8 | 2013-10-21 15:19:56 -0700 | [diff] [blame] | 83 | int ion_free(int fd, ion_user_handle_t handle) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 84 | { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 85 | struct ion_handle_data data = { |
| 86 | .handle = handle, |
| 87 | }; |
| 88 | return ion_ioctl(fd, ION_IOC_FREE, &data); |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Rom Lemarchand | 969eac8 | 2013-10-21 15:19:56 -0700 | [diff] [blame] | 91 | int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot, |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 92 | int flags, off_t offset, unsigned char **ptr, int *map_fd) |
| 93 | { |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 94 | int ret; |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 95 | unsigned char *tmp_ptr; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 96 | struct ion_fd_data data = { |
| 97 | .handle = handle, |
| 98 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 99 | |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 100 | if (map_fd == NULL) |
| 101 | return -EINVAL; |
| 102 | if (ptr == NULL) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | ret = ion_ioctl(fd, ION_IOC_MAP, &data); |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 106 | if (ret < 0) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 107 | return ret; |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 108 | if (data.fd < 0) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 109 | ALOGE("map ioctl returned negative fd\n"); |
| 110 | return -EINVAL; |
| 111 | } |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 112 | tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset); |
| 113 | if (tmp_ptr == MAP_FAILED) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 114 | ALOGE("mmap failed: %s\n", strerror(errno)); |
| 115 | return -errno; |
| 116 | } |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 117 | *map_fd = data.fd; |
| 118 | *ptr = tmp_ptr; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 119 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Rom Lemarchand | 969eac8 | 2013-10-21 15:19:56 -0700 | [diff] [blame] | 122 | int ion_share(int fd, ion_user_handle_t handle, int *share_fd) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 123 | { |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 124 | int ret; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 125 | struct ion_fd_data data = { |
| 126 | .handle = handle, |
| 127 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 128 | |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 129 | if (share_fd == NULL) |
| 130 | return -EINVAL; |
| 131 | |
| 132 | ret = ion_ioctl(fd, ION_IOC_SHARE, &data); |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 133 | if (ret < 0) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 134 | return ret; |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 135 | if (data.fd < 0) { |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 136 | ALOGE("share ioctl returned negative fd\n"); |
| 137 | return -EINVAL; |
| 138 | } |
Rom Lemarchand | 5cdcdce | 2014-10-27 16:42:10 -0700 | [diff] [blame] | 139 | *share_fd = data.fd; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 140 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Rebecca Schultz Zavin | a50fd55 | 2012-06-11 15:12:37 -0700 | [diff] [blame] | 143 | int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask, |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 144 | unsigned int flags, int *handle_fd) { |
| 145 | ion_user_handle_t handle; |
| 146 | int ret; |
Rebecca Schultz Zavin | a50fd55 | 2012-06-11 15:12:37 -0700 | [diff] [blame] | 147 | |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 148 | ret = ion_alloc(fd, len, align, heap_mask, flags, &handle); |
| 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | ret = ion_share(fd, handle, handle_fd); |
| 152 | ion_free(fd, handle); |
| 153 | return ret; |
Rebecca Schultz Zavin | a50fd55 | 2012-06-11 15:12:37 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Rom Lemarchand | 969eac8 | 2013-10-21 15:19:56 -0700 | [diff] [blame] | 156 | int ion_import(int fd, int share_fd, ion_user_handle_t *handle) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 157 | { |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 158 | int ret; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 159 | struct ion_fd_data data = { |
| 160 | .fd = share_fd, |
| 161 | }; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 162 | |
Colin Cross | 7496318 | 2013-11-08 19:07:38 -0800 | [diff] [blame] | 163 | if (handle == NULL) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | ret = ion_ioctl(fd, ION_IOC_IMPORT, &data); |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 167 | if (ret < 0) |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 168 | return ret; |
Colin Cross | 92d7ca6 | 2013-11-08 19:04:18 -0800 | [diff] [blame] | 169 | *handle = data.handle; |
| 170 | return ret; |
Rebecca Schultz Zavin | afd9123 | 2012-02-22 16:36:55 -0800 | [diff] [blame] | 171 | } |
Rebecca Schultz Zavin | 3cfcc30 | 2012-09-20 09:46:19 -0700 | [diff] [blame] | 172 | |
| 173 | int ion_sync_fd(int fd, int handle_fd) |
| 174 | { |
| 175 | struct ion_fd_data data = { |
| 176 | .fd = handle_fd, |
| 177 | }; |
| 178 | return ion_ioctl(fd, ION_IOC_SYNC, &data); |
| 179 | } |