Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 3 | |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
Duy Truong | 73d36df | 2013-02-09 20:33:23 -0800 | [diff] [blame] | 13 | * * Neither the name of The Linux Foundation nor the names of its |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 30 | #define DEBUG 0 |
Arun Kumar K.R | a6f9187 | 2014-03-20 16:05:28 -0700 | [diff] [blame] | 31 | #include <sys/ioctl.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 32 | #include <sys/mman.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <cutils/log.h> |
| 36 | #include <errno.h> |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame^] | 37 | #include <utils/Trace.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 38 | #include "gralloc_priv.h" |
| 39 | #include "ionalloc.h" |
| 40 | |
| 41 | using gralloc::IonAlloc; |
| 42 | |
| 43 | #define ION_DEVICE "/dev/ion" |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame^] | 44 | #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 45 | |
| 46 | int IonAlloc::open_device() |
| 47 | { |
| 48 | if(mIonFd == FD_INIT) |
| 49 | mIonFd = open(ION_DEVICE, O_RDONLY); |
| 50 | |
| 51 | if(mIonFd < 0 ) { |
| 52 | ALOGE("%s: Failed to open ion device - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 53 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 54 | mIonFd = FD_INIT; |
| 55 | return -errno; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | void IonAlloc::close_device() |
| 61 | { |
| 62 | if(mIonFd >= 0) |
| 63 | close(mIonFd); |
| 64 | mIonFd = FD_INIT; |
| 65 | } |
| 66 | |
| 67 | int IonAlloc::alloc_buffer(alloc_data& data) |
| 68 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame^] | 69 | ATRACE_CALL(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 70 | Locker::Autolock _l(mLock); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 71 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 72 | struct ion_handle_data handle_data; |
| 73 | struct ion_fd_data fd_data; |
| 74 | struct ion_allocation_data ionAllocData; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 75 | void *base = 0; |
| 76 | |
| 77 | ionAllocData.len = data.size; |
| 78 | ionAllocData.align = data.align; |
Naseer Ahmed | 68ee7c0 | 2014-04-11 19:59:39 -0400 | [diff] [blame] | 79 | ionAllocData.heap_id_mask = data.flags & ~ION_SECURE; |
Saurabh Shah | d95c408 | 2012-09-14 14:18:21 -0700 | [diff] [blame] | 80 | ionAllocData.flags = data.uncached ? 0 : ION_FLAG_CACHED; |
Amara Venkata Mastan Manoj Kumar | c180c4e | 2012-10-09 10:13:55 -0700 | [diff] [blame] | 81 | // ToDo: replace usage of alloc data structure with |
| 82 | // ionallocdata structure. |
| 83 | if (data.flags & ION_SECURE) |
| 84 | ionAllocData.flags |= ION_SECURE; |
| 85 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 86 | err = open_device(); |
| 87 | if (err) |
| 88 | return err; |
Saurabh Shah | d95c408 | 2012-09-14 14:18:21 -0700 | [diff] [blame] | 89 | if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 90 | err = -errno; |
| 91 | ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 92 | return err; |
| 93 | } |
| 94 | |
| 95 | fd_data.handle = ionAllocData.handle; |
| 96 | handle_data.handle = ionAllocData.handle; |
Saurabh Shah | d95c408 | 2012-09-14 14:18:21 -0700 | [diff] [blame] | 97 | if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 98 | err = -errno; |
| 99 | ALOGE("%s: ION_IOC_MAP failed with error - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 100 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 101 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 102 | return err; |
| 103 | } |
| 104 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 105 | if(!(data.flags & ION_SECURE)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 106 | base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 107 | MAP_SHARED, fd_data.fd, 0); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 108 | if(base == MAP_FAILED) { |
| 109 | err = -errno; |
| 110 | ALOGE("%s: Failed to map the allocated memory: %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 111 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 112 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 113 | return err; |
| 114 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 117 | data.base = base; |
| 118 | data.fd = fd_data.fd; |
| 119 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Shalaj Jain | a70b435 | 2014-06-15 13:47:47 -0700 | [diff] [blame] | 120 | ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 121 | data.base, ionAllocData.len, data.fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 126 | int IonAlloc::free_buffer(void* base, unsigned int size, unsigned int offset, |
| 127 | int fd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 128 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame^] | 129 | ATRACE_CALL(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 130 | Locker::Autolock _l(mLock); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 131 | ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 132 | base, size, fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 133 | int err = 0; |
| 134 | err = open_device(); |
| 135 | if (err) |
| 136 | return err; |
| 137 | |
| 138 | if(base) |
| 139 | err = unmap_buffer(base, size, offset); |
| 140 | close(fd); |
| 141 | return err; |
| 142 | } |
| 143 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 144 | int IonAlloc::map_buffer(void **pBase, unsigned int size, unsigned int offset, |
| 145 | int fd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 146 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame^] | 147 | ATRACE_CALL(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 148 | int err = 0; |
| 149 | void *base = 0; |
| 150 | // It is a (quirky) requirement of ION to have opened the |
| 151 | // ion fd in the process that is doing the mapping |
| 152 | err = open_device(); |
| 153 | if (err) |
| 154 | return err; |
| 155 | |
| 156 | base = mmap(0, size, PROT_READ| PROT_WRITE, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 157 | MAP_SHARED, fd, 0); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 158 | *pBase = base; |
| 159 | if(base == MAP_FAILED) { |
| 160 | err = -errno; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 161 | ALOGE("ion: Failed to map memory in the client: %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 162 | strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 163 | } else { |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 164 | ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 165 | base, size, offset, fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 166 | } |
| 167 | return err; |
| 168 | } |
| 169 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 170 | int IonAlloc::unmap_buffer(void *base, unsigned int size, |
| 171 | unsigned int /*offset*/) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 172 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame^] | 173 | ATRACE_CALL(); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 174 | ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%u", base, size); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 175 | int err = 0; |
| 176 | if(munmap(base, size)) { |
| 177 | err = -errno; |
| 178 | ALOGE("ion: Failed to unmap memory at %p : %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 179 | base, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 180 | } |
| 181 | return err; |
| 182 | |
| 183 | } |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 184 | int IonAlloc::clean_buffer(void *base, unsigned int size, unsigned int offset, |
| 185 | int fd, int op) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 186 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame^] | 187 | ATRACE_CALL(); |
| 188 | ATRACE_INT("operation id", op); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 189 | struct ion_flush_data flush_data; |
| 190 | struct ion_fd_data fd_data; |
| 191 | struct ion_handle_data handle_data; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 192 | int err = 0; |
| 193 | |
| 194 | err = open_device(); |
| 195 | if (err) |
| 196 | return err; |
| 197 | |
| 198 | fd_data.fd = fd; |
| 199 | if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) { |
| 200 | err = -errno; |
| 201 | ALOGE("%s: ION_IOC_IMPORT failed with error - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 202 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 203 | return err; |
| 204 | } |
| 205 | |
| 206 | handle_data.handle = fd_data.handle; |
| 207 | flush_data.handle = fd_data.handle; |
| 208 | flush_data.vaddr = base; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 209 | // offset and length are unsigned int |
| 210 | flush_data.offset = offset; |
| 211 | flush_data.length = size; |
Saurabh Shah | ece296e | 2012-09-11 13:33:44 -0700 | [diff] [blame] | 212 | |
| 213 | struct ion_custom_data d; |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 214 | switch(op) { |
| 215 | case CACHE_CLEAN: |
| 216 | d.cmd = ION_IOC_CLEAN_CACHES; |
| 217 | break; |
| 218 | case CACHE_INVALIDATE: |
| 219 | d.cmd = ION_IOC_INV_CACHES; |
| 220 | break; |
| 221 | case CACHE_CLEAN_AND_INVALIDATE: |
| 222 | default: |
| 223 | d.cmd = ION_IOC_CLEAN_INV_CACHES; |
| 224 | } |
| 225 | |
Saurabh Shah | ece296e | 2012-09-11 13:33:44 -0700 | [diff] [blame] | 226 | d.arg = (unsigned long int)&flush_data; |
| 227 | |
| 228 | if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 229 | err = -errno; |
| 230 | ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", |
Saurabh Shah | ece296e | 2012-09-11 13:33:44 -0700 | [diff] [blame] | 231 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 232 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 233 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
| 234 | return err; |
| 235 | } |
| 236 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
| 237 | return 0; |
| 238 | } |
| 239 | |