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