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