Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 2 | * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved. |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [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. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 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 | |
| 30 | #define DEBUG 0 |
| 31 | #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL) |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <sys/mman.h> |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 34 | #include <linux/msm_ion.h> |
| 35 | #if TARGET_ION_ABI_VERSION >= 2 |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 36 | #include <linux/dma-buf.h> |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 37 | #include <ion/ion.h> |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 38 | #endif |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <fcntl.h> |
Naseer Ahmed | fcad05e | 2018-03-06 20:41:14 -0500 | [diff] [blame] | 41 | #include <log/log.h> |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 42 | #include <cutils/trace.h> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 43 | #include <errno.h> |
| 44 | #include <utils/Trace.h> |
Naseer Ahmed | 7a2b09c | 2017-05-11 13:03:17 -0400 | [diff] [blame] | 45 | #include <string> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 46 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 47 | #include "gr_utils.h" |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 48 | #include "gralloc_priv.h" |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 49 | #include "gr_ion_alloc.h" |
| 50 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 51 | namespace gralloc { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 52 | |
| 53 | bool IonAlloc::Init() { |
| 54 | if (ion_dev_fd_ == FD_INIT) { |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 55 | ion_dev_fd_ = OpenIonDevice(); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (ion_dev_fd_ < 0) { |
| 59 | ALOGE("%s: Failed to open ion device - %s", __FUNCTION__, strerror(errno)); |
| 60 | ion_dev_fd_ = FD_INIT; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 67 | #if TARGET_ION_ABI_VERSION >= 2 // Use libion APIs for new ion |
| 68 | |
| 69 | int IonAlloc::OpenIonDevice() { |
| 70 | return ion_open(); |
| 71 | } |
| 72 | |
| 73 | void IonAlloc::CloseIonDevice() { |
| 74 | if (ion_dev_fd_ > FD_INIT) { |
| 75 | ion_close(ion_dev_fd_); |
| 76 | } |
| 77 | |
| 78 | ion_dev_fd_ = FD_INIT; |
| 79 | } |
| 80 | |
| 81 | int IonAlloc::AllocBuffer(AllocData *data) { |
| 82 | ATRACE_CALL(); |
| 83 | int err = 0; |
| 84 | int fd = -1; |
| 85 | unsigned int flags = data->flags; |
| 86 | |
| 87 | flags |= data->uncached ? 0 : ION_FLAG_CACHED; |
| 88 | |
| 89 | std::string tag_name{}; |
| 90 | if (ATRACE_ENABLED()) { |
| 91 | tag_name = "libion alloc size: " + std::to_string(data->size); |
| 92 | } |
| 93 | |
| 94 | ATRACE_BEGIN(tag_name.c_str()); |
| 95 | err = ion_alloc_fd(ion_dev_fd_, data->size, data->align, data->heap_id, flags, &fd); |
| 96 | ATRACE_END(); |
| 97 | if (err) { |
Ramkumar Radhakrishnan | 30ea0f0 | 2018-10-11 19:25:10 -0700 | [diff] [blame] | 98 | ALOGE("libion alloc failed ion_fd %d size %d align %d heap_id %x flags %x", |
| 99 | ion_dev_fd_, data->size, data->align, data->heap_id, flags); |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 100 | return err; |
| 101 | } |
| 102 | |
| 103 | data->fd = fd; |
| 104 | data->ion_handle = fd; // For new ion api ion_handle does not exists so reusing fd for now |
| 105 | ALOGD_IF(DEBUG, "libion: Allocated buffer size:%u fd:%d", data->size, data->fd); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, |
| 111 | int /*ion_handle*/) { |
| 112 | ATRACE_CALL(); |
| 113 | int err = 0; |
| 114 | ALOGD_IF(DEBUG, "libion: Freeing buffer base:%p size:%u fd:%d", base, size, fd); |
| 115 | |
| 116 | if (base) { |
| 117 | err = UnmapBuffer(base, size, offset); |
| 118 | } |
| 119 | |
| 120 | close(fd); |
| 121 | return err; |
| 122 | } |
| 123 | |
| 124 | int IonAlloc::ImportBuffer(int fd) { |
| 125 | // For new ion api ion_handle does not exists so reusing fd for now |
| 126 | return fd; |
| 127 | } |
| 128 | |
| 129 | int IonAlloc::CleanBuffer(void */*base*/, unsigned int /*size*/, unsigned int /*offset*/, |
| 130 | int /*handle*/, int op, int dma_buf_fd) { |
| 131 | ATRACE_CALL(); |
| 132 | ATRACE_INT("operation id", op); |
| 133 | |
| 134 | struct dma_buf_sync sync; |
| 135 | int err = 0; |
| 136 | |
| 137 | switch (op) { |
| 138 | case CACHE_CLEAN: |
| 139 | sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW; |
| 140 | break; |
| 141 | case CACHE_INVALIDATE: |
| 142 | sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW; |
| 143 | break; |
Rohit Kulkarni | b20abe7 | 2018-03-13 16:55:10 -0700 | [diff] [blame] | 144 | case CACHE_READ_DONE: |
| 145 | sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ; |
| 146 | break; |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 147 | default: |
| 148 | ALOGE("%s: Invalid operation %d", __FUNCTION__, op); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | if (ioctl(dma_buf_fd, INT(DMA_BUF_IOCTL_SYNC), &sync)) { |
| 153 | err = -errno; |
| 154 | ALOGE("%s: DMA_BUF_IOCTL_SYNC failed with error - %s", __FUNCTION__, strerror(errno)); |
| 155 | return err; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | #else |
| 162 | #ifndef TARGET_ION_ABI_VERSION // Use old ion apis directly |
| 163 | |
| 164 | int IonAlloc::OpenIonDevice() { |
| 165 | return open(kIonDevice, O_RDONLY); |
| 166 | } |
| 167 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 168 | void IonAlloc::CloseIonDevice() { |
| 169 | if (ion_dev_fd_ > FD_INIT) { |
| 170 | close(ion_dev_fd_); |
| 171 | } |
| 172 | |
| 173 | ion_dev_fd_ = FD_INIT; |
| 174 | } |
| 175 | |
| 176 | int IonAlloc::AllocBuffer(AllocData *data) { |
| 177 | ATRACE_CALL(); |
| 178 | int err = 0; |
| 179 | struct ion_handle_data handle_data; |
| 180 | struct ion_fd_data fd_data; |
| 181 | struct ion_allocation_data ion_alloc_data; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 182 | |
| 183 | ion_alloc_data.len = data->size; |
| 184 | ion_alloc_data.align = data->align; |
| 185 | ion_alloc_data.heap_id_mask = data->heap_id; |
| 186 | ion_alloc_data.flags = data->flags; |
| 187 | ion_alloc_data.flags |= data->uncached ? 0 : ION_FLAG_CACHED; |
Naseer Ahmed | 7a2b09c | 2017-05-11 13:03:17 -0400 | [diff] [blame] | 188 | std::string tag_name{}; |
| 189 | if (ATRACE_ENABLED()) { |
| 190 | tag_name = "ION_IOC_ALLOC size: " + std::to_string(data->size); |
| 191 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 192 | |
Naseer Ahmed | 7a2b09c | 2017-05-11 13:03:17 -0400 | [diff] [blame] | 193 | ATRACE_BEGIN(tag_name.c_str()); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 194 | if (ioctl(ion_dev_fd_, INT(ION_IOC_ALLOC), &ion_alloc_data)) { |
| 195 | err = -errno; |
| 196 | ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno)); |
| 197 | return err; |
| 198 | } |
Naseer Ahmed | 7a2b09c | 2017-05-11 13:03:17 -0400 | [diff] [blame] | 199 | ATRACE_END(); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 200 | |
| 201 | fd_data.handle = ion_alloc_data.handle; |
| 202 | handle_data.handle = ion_alloc_data.handle; |
Naseer Ahmed | 7a2b09c | 2017-05-11 13:03:17 -0400 | [diff] [blame] | 203 | ATRACE_BEGIN("ION_IOC_MAP"); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 204 | if (ioctl(ion_dev_fd_, INT(ION_IOC_MAP), &fd_data)) { |
| 205 | err = -errno; |
| 206 | ALOGE("%s: ION_IOC_MAP failed with error - %s", __FUNCTION__, strerror(errno)); |
| 207 | ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data); |
| 208 | return err; |
| 209 | } |
Naseer Ahmed | 7a2b09c | 2017-05-11 13:03:17 -0400 | [diff] [blame] | 210 | ATRACE_END(); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 211 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 212 | data->fd = fd_data.fd; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 213 | data->ion_handle = handle_data.handle; |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 214 | ALOGD_IF(DEBUG, "ion: Allocated buffer size:%zu fd:%d handle:0x%x", ion_alloc_data.len, data->fd, |
| 215 | data->ion_handle); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 220 | int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, |
| 221 | int ion_handle) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 222 | ATRACE_CALL(); |
| 223 | int err = 0; |
Naseer Ahmed | e69031e | 2016-11-22 20:05:16 -0500 | [diff] [blame] | 224 | ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d handle:0x%x", base, size, fd, |
| 225 | ion_handle); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 226 | |
| 227 | if (base) { |
| 228 | err = UnmapBuffer(base, size, offset); |
| 229 | } |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 230 | |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 231 | if (ion_handle > 0) { |
| 232 | struct ion_handle_data handle_data; |
| 233 | handle_data.handle = ion_handle; |
| 234 | ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data); |
| 235 | } |
| 236 | close(fd); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 237 | return err; |
| 238 | } |
| 239 | |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 240 | int IonAlloc::ImportBuffer(int fd) { |
| 241 | struct ion_fd_data fd_data; |
| 242 | int err = 0; |
| 243 | fd_data.fd = fd; |
| 244 | if (ioctl(ion_dev_fd_, INT(ION_IOC_IMPORT), &fd_data)) { |
| 245 | err = -errno; |
| 246 | ALOGE("%s: ION_IOC_IMPORT failed with error - %s", __FUNCTION__, strerror(errno)); |
| 247 | return err; |
| 248 | } |
| 249 | return fd_data.handle; |
| 250 | } |
| 251 | |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 252 | int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op, |
| 253 | int /*fd*/) { |
Rohit Kulkarni | b20abe7 | 2018-03-13 16:55:10 -0700 | [diff] [blame] | 254 | if (op == CACHE_READ_DONE) { |
| 255 | return 0; |
| 256 | } |
| 257 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 258 | ATRACE_CALL(); |
| 259 | ATRACE_INT("operation id", op); |
| 260 | struct ion_flush_data flush_data; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 261 | int err = 0; |
| 262 | |
Naseer Ahmed | 3a9d53a | 2017-03-15 19:21:40 -0400 | [diff] [blame] | 263 | flush_data.handle = handle; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 264 | flush_data.vaddr = base; |
| 265 | // offset and length are unsigned int |
| 266 | flush_data.offset = offset; |
| 267 | flush_data.length = size; |
| 268 | |
| 269 | struct ion_custom_data d; |
| 270 | switch (op) { |
| 271 | case CACHE_CLEAN: |
| 272 | d.cmd = ION_IOC_CLEAN_CACHES; |
| 273 | break; |
| 274 | case CACHE_INVALIDATE: |
| 275 | d.cmd = ION_IOC_INV_CACHES; |
| 276 | break; |
| 277 | case CACHE_CLEAN_AND_INVALIDATE: |
| 278 | default: |
| 279 | d.cmd = ION_IOC_CLEAN_INV_CACHES; |
| 280 | } |
| 281 | |
| 282 | d.arg = (unsigned long)(&flush_data); // NOLINT |
| 283 | if (ioctl(ion_dev_fd_, INT(ION_IOC_CUSTOM), &d)) { |
| 284 | err = -errno; |
| 285 | ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", __FUNCTION__, strerror(errno)); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 286 | return err; |
| 287 | } |
| 288 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
Rohit Kulkarni | 3ac9839 | 2017-10-20 12:04:34 -0700 | [diff] [blame] | 292 | #else // This ion version is not supported |
| 293 | |
| 294 | int IonAlloc::OpenIonDevice() { |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | void IonAlloc::CloseIonDevice() { |
| 299 | } |
| 300 | |
| 301 | int IonAlloc::AllocBuffer(AllocData * /*data*/) { |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | int IonAlloc::FreeBuffer(void * /*base*/, unsigned int /*size*/, unsigned int /*offset*/, |
| 306 | int /*fd*/, int /*ion_handle*/) { |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | |
| 310 | int IonAlloc::ImportBuffer(int /*fd*/) { |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
| 314 | int IonAlloc::CleanBuffer(void * /*base*/, unsigned int /*size*/, unsigned int /*offset*/, |
| 315 | int /*handle*/, int /*op*/, int /*fd*/) { |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | |
| 319 | #endif |
| 320 | #endif // TARGET_ION_ABI_VERSION |
| 321 | |
| 322 | |
| 323 | int IonAlloc::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) { |
| 324 | ATRACE_CALL(); |
| 325 | int err = 0; |
| 326 | void *addr = 0; |
| 327 | |
| 328 | addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 329 | *base = addr; |
| 330 | if (addr == MAP_FAILED) { |
| 331 | err = -errno; |
| 332 | ALOGE("ion: Failed to map memory in the client: %s", strerror(errno)); |
| 333 | } else { |
| 334 | ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d", addr, size, offset, fd); |
| 335 | } |
| 336 | |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | int IonAlloc::UnmapBuffer(void *base, unsigned int size, unsigned int /*offset*/) { |
| 341 | ATRACE_CALL(); |
| 342 | ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%u", base, size); |
| 343 | |
| 344 | int err = 0; |
| 345 | if (munmap(base, size)) { |
| 346 | err = -errno; |
| 347 | ALOGE("ion: Failed to unmap memory at %p : %s", base, strerror(errno)); |
| 348 | } |
| 349 | |
| 350 | return err; |
| 351 | } |
| 352 | |
Naseer Ahmed | e36f224 | 2017-12-01 15:33:56 -0500 | [diff] [blame] | 353 | } // namespace gralloc |