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 |
Naseer Ahmed | 9eb5e09 | 2014-09-25 13:24:44 -0400 | [diff] [blame] | 31 | #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL) |
Arun Kumar K.R | a6f9187 | 2014-03-20 16:05:28 -0700 | [diff] [blame] | 32 | #include <sys/ioctl.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 33 | #include <sys/mman.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <cutils/log.h> |
| 37 | #include <errno.h> |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 38 | #include <utils/Trace.h> |
Rajavenu Kyatham | 038ee09 | 2016-08-19 17:41:53 +0530 | [diff] [blame] | 39 | #include <cutils/trace.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 40 | #include "gralloc_priv.h" |
| 41 | #include "ionalloc.h" |
| 42 | |
| 43 | using gralloc::IonAlloc; |
| 44 | |
| 45 | #define ION_DEVICE "/dev/ion" |
| 46 | |
| 47 | int IonAlloc::open_device() |
| 48 | { |
| 49 | if(mIonFd == FD_INIT) |
| 50 | mIonFd = open(ION_DEVICE, O_RDONLY); |
| 51 | |
| 52 | if(mIonFd < 0 ) { |
| 53 | ALOGE("%s: Failed to open ion device - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 54 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 55 | mIonFd = FD_INIT; |
| 56 | return -errno; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | void IonAlloc::close_device() |
| 62 | { |
| 63 | if(mIonFd >= 0) |
| 64 | close(mIonFd); |
| 65 | mIonFd = FD_INIT; |
| 66 | } |
| 67 | |
| 68 | int IonAlloc::alloc_buffer(alloc_data& data) |
| 69 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 70 | ATRACE_CALL(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 71 | Locker::Autolock _l(mLock); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 72 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 73 | struct ion_handle_data handle_data; |
| 74 | struct ion_fd_data fd_data; |
| 75 | struct ion_allocation_data ionAllocData; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 76 | void *base = 0; |
| 77 | |
| 78 | ionAllocData.len = data.size; |
| 79 | ionAllocData.align = data.align; |
Naseer Ahmed | 8d0d72a | 2014-12-19 16:25:09 -0500 | [diff] [blame] | 80 | ionAllocData.heap_id_mask = data.heapId; |
| 81 | ionAllocData.flags = data.flags; |
| 82 | ionAllocData.flags |= data.uncached ? 0 : ION_FLAG_CACHED; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 83 | err = open_device(); |
| 84 | if (err) |
| 85 | return err; |
Saurabh Shah | d95c408 | 2012-09-14 14:18:21 -0700 | [diff] [blame] | 86 | if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 87 | err = -errno; |
| 88 | ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 89 | return err; |
| 90 | } |
| 91 | |
| 92 | fd_data.handle = ionAllocData.handle; |
| 93 | handle_data.handle = ionAllocData.handle; |
Saurabh Shah | d95c408 | 2012-09-14 14:18:21 -0700 | [diff] [blame] | 94 | if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 95 | err = -errno; |
| 96 | ALOGE("%s: ION_IOC_MAP failed with error - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 97 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 98 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 99 | return err; |
| 100 | } |
| 101 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 102 | if(!(data.flags & ION_SECURE)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 103 | base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 104 | MAP_SHARED, fd_data.fd, 0); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 105 | if(base == MAP_FAILED) { |
| 106 | err = -errno; |
| 107 | ALOGE("%s: Failed to map the allocated memory: %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 108 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 109 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 110 | return err; |
| 111 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 114 | data.base = base; |
| 115 | data.fd = fd_data.fd; |
| 116 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Shalaj Jain | a70b435 | 2014-06-15 13:47:47 -0700 | [diff] [blame] | 117 | ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 118 | data.base, ionAllocData.len, data.fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 123 | int IonAlloc::free_buffer(void* base, unsigned int size, unsigned int offset, |
| 124 | int fd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 125 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 126 | ATRACE_CALL(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 127 | Locker::Autolock _l(mLock); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 128 | ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 129 | base, size, fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 130 | int err = 0; |
| 131 | err = open_device(); |
| 132 | if (err) |
| 133 | return err; |
| 134 | |
| 135 | if(base) |
| 136 | err = unmap_buffer(base, size, offset); |
| 137 | close(fd); |
| 138 | return err; |
| 139 | } |
| 140 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 141 | int IonAlloc::map_buffer(void **pBase, unsigned int size, unsigned int offset, |
| 142 | int fd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 143 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 144 | ATRACE_CALL(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 145 | int err = 0; |
| 146 | void *base = 0; |
| 147 | // It is a (quirky) requirement of ION to have opened the |
| 148 | // ion fd in the process that is doing the mapping |
| 149 | err = open_device(); |
| 150 | if (err) |
| 151 | return err; |
| 152 | |
| 153 | base = mmap(0, size, PROT_READ| PROT_WRITE, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 154 | MAP_SHARED, fd, 0); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 155 | *pBase = base; |
| 156 | if(base == MAP_FAILED) { |
| 157 | err = -errno; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 158 | ALOGE("ion: Failed to map memory in the client: %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 159 | strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 160 | } else { |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 161 | 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] | 162 | base, size, offset, fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 163 | } |
| 164 | return err; |
| 165 | } |
| 166 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 167 | int IonAlloc::unmap_buffer(void *base, unsigned int size, |
| 168 | unsigned int /*offset*/) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 169 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 170 | ATRACE_CALL(); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 171 | ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%u", base, size); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 172 | int err = 0; |
| 173 | if(munmap(base, size)) { |
| 174 | err = -errno; |
| 175 | ALOGE("ion: Failed to unmap memory at %p : %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 176 | base, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 177 | } |
| 178 | return err; |
| 179 | |
| 180 | } |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 181 | int IonAlloc::clean_buffer(void *base, unsigned int size, unsigned int offset, |
| 182 | int fd, int op) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 183 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 184 | ATRACE_CALL(); |
| 185 | ATRACE_INT("operation id", op); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 186 | struct ion_flush_data flush_data; |
| 187 | struct ion_fd_data fd_data; |
| 188 | struct ion_handle_data handle_data; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 189 | int err = 0; |
| 190 | |
| 191 | err = open_device(); |
| 192 | if (err) |
| 193 | return err; |
| 194 | |
| 195 | fd_data.fd = fd; |
| 196 | if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) { |
| 197 | err = -errno; |
| 198 | ALOGE("%s: ION_IOC_IMPORT failed with error - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 199 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 200 | return err; |
| 201 | } |
| 202 | |
| 203 | handle_data.handle = fd_data.handle; |
| 204 | flush_data.handle = fd_data.handle; |
| 205 | flush_data.vaddr = base; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 206 | // offset and length are unsigned int |
| 207 | flush_data.offset = offset; |
| 208 | flush_data.length = size; |
Saurabh Shah | ece296e | 2012-09-11 13:33:44 -0700 | [diff] [blame] | 209 | |
| 210 | struct ion_custom_data d; |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 211 | switch(op) { |
| 212 | case CACHE_CLEAN: |
| 213 | d.cmd = ION_IOC_CLEAN_CACHES; |
| 214 | break; |
| 215 | case CACHE_INVALIDATE: |
| 216 | d.cmd = ION_IOC_INV_CACHES; |
| 217 | break; |
| 218 | case CACHE_CLEAN_AND_INVALIDATE: |
| 219 | default: |
| 220 | d.cmd = ION_IOC_CLEAN_INV_CACHES; |
| 221 | } |
| 222 | |
Saurabh Shah | ece296e | 2012-09-11 13:33:44 -0700 | [diff] [blame] | 223 | d.arg = (unsigned long int)&flush_data; |
| 224 | |
| 225 | if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 226 | err = -errno; |
| 227 | ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", |
Saurabh Shah | ece296e | 2012-09-11 13:33:44 -0700 | [diff] [blame] | 228 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 229 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 230 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
| 231 | return err; |
| 232 | } |
| 233 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
| 234 | return 0; |
| 235 | } |
| 236 | |