Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
Duy Truong | 73d36df | 2013-02-09 20:33:23 -0800 | [diff] [blame^] | 2 | * Copyright (c) 2011-2012, 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 |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 31 | #include <linux/ioctl.h> |
| 32 | #include <sys/mman.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <cutils/log.h> |
| 36 | #include <errno.h> |
| 37 | #include "gralloc_priv.h" |
| 38 | #include "ionalloc.h" |
| 39 | |
| 40 | using gralloc::IonAlloc; |
| 41 | |
| 42 | #define ION_DEVICE "/dev/ion" |
| 43 | |
| 44 | int IonAlloc::open_device() |
| 45 | { |
| 46 | if(mIonFd == FD_INIT) |
| 47 | mIonFd = open(ION_DEVICE, O_RDONLY); |
| 48 | |
| 49 | if(mIonFd < 0 ) { |
| 50 | ALOGE("%s: Failed to open ion device - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 51 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 52 | mIonFd = FD_INIT; |
| 53 | return -errno; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | void IonAlloc::close_device() |
| 59 | { |
| 60 | if(mIonFd >= 0) |
| 61 | close(mIonFd); |
| 62 | mIonFd = FD_INIT; |
| 63 | } |
| 64 | |
| 65 | int IonAlloc::alloc_buffer(alloc_data& data) |
| 66 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 67 | Locker::Autolock _l(mLock); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 68 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 69 | struct ion_handle_data handle_data; |
| 70 | struct ion_fd_data fd_data; |
| 71 | struct ion_allocation_data ionAllocData; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 72 | void *base = 0; |
| 73 | |
| 74 | ionAllocData.len = data.size; |
| 75 | ionAllocData.align = data.align; |
Amara Venkata Mastan Manoj Kumar | c180c4e | 2012-10-09 10:13:55 -0700 | [diff] [blame] | 76 | ionAllocData.heap_mask = data.flags & ~ION_SECURE; |
Saurabh Shah | d95c408 | 2012-09-14 14:18:21 -0700 | [diff] [blame] | 77 | ionAllocData.flags = data.uncached ? 0 : ION_FLAG_CACHED; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 78 | |
Amara Venkata Mastan Manoj Kumar | c180c4e | 2012-10-09 10:13:55 -0700 | [diff] [blame] | 79 | // ToDo: replace usage of alloc data structure with |
| 80 | // ionallocdata structure. |
| 81 | if (data.flags & ION_SECURE) |
| 82 | ionAllocData.flags |= ION_SECURE; |
| 83 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 84 | err = open_device(); |
| 85 | if (err) |
| 86 | return err; |
| 87 | |
Saurabh Shah | d95c408 | 2012-09-14 14:18:21 -0700 | [diff] [blame] | 88 | if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 89 | err = -errno; |
| 90 | ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 91 | return err; |
| 92 | } |
| 93 | |
| 94 | fd_data.handle = ionAllocData.handle; |
| 95 | handle_data.handle = ionAllocData.handle; |
Saurabh Shah | d95c408 | 2012-09-14 14:18:21 -0700 | [diff] [blame] | 96 | if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 97 | err = -errno; |
| 98 | ALOGE("%s: ION_IOC_MAP failed with error - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 99 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 100 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 101 | return err; |
| 102 | } |
| 103 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 104 | if(!(data.flags & ION_SECURE)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 105 | base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 106 | MAP_SHARED, fd_data.fd, 0); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 107 | if(base == MAP_FAILED) { |
| 108 | err = -errno; |
| 109 | ALOGE("%s: Failed to map the allocated memory: %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 110 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 111 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 112 | return err; |
| 113 | } |
| 114 | memset(base, 0, ionAllocData.len); |
| 115 | // Clean cache after memset |
| 116 | clean_buffer(base, data.size, data.offset, fd_data.fd); |
| 117 | } |
| 118 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 119 | data.base = base; |
| 120 | data.fd = fd_data.fd; |
| 121 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 122 | ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%d fd:%d", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 123 | data.base, ionAllocData.len, data.fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | int IonAlloc::free_buffer(void* base, size_t size, int offset, int fd) |
| 129 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 130 | Locker::Autolock _l(mLock); |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 131 | ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%d 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 | |
| 144 | int IonAlloc::map_buffer(void **pBase, size_t size, int offset, int fd) |
| 145 | { |
| 146 | int err = 0; |
| 147 | void *base = 0; |
| 148 | // It is a (quirky) requirement of ION to have opened the |
| 149 | // ion fd in the process that is doing the mapping |
| 150 | err = open_device(); |
| 151 | if (err) |
| 152 | return err; |
| 153 | |
| 154 | base = mmap(0, size, PROT_READ| PROT_WRITE, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 155 | MAP_SHARED, fd, 0); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 156 | *pBase = base; |
| 157 | if(base == MAP_FAILED) { |
| 158 | err = -errno; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 159 | ALOGE("ion: Failed to map memory in the client: %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 160 | strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 161 | } else { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 162 | ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%d offset:%d fd:%d", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 163 | base, size, offset, fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 164 | } |
| 165 | return err; |
| 166 | } |
| 167 | |
| 168 | int IonAlloc::unmap_buffer(void *base, size_t size, int offset) |
| 169 | { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 170 | ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%d", base, size); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 171 | int err = 0; |
| 172 | if(munmap(base, size)) { |
| 173 | err = -errno; |
| 174 | ALOGE("ion: Failed to unmap memory at %p : %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 175 | base, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 176 | } |
| 177 | return err; |
| 178 | |
| 179 | } |
| 180 | int IonAlloc::clean_buffer(void *base, size_t size, int offset, int fd) |
| 181 | { |
| 182 | struct ion_flush_data flush_data; |
| 183 | struct ion_fd_data fd_data; |
| 184 | struct ion_handle_data handle_data; |
| 185 | struct ion_handle* handle; |
| 186 | int err = 0; |
| 187 | |
| 188 | err = open_device(); |
| 189 | if (err) |
| 190 | return err; |
| 191 | |
| 192 | fd_data.fd = fd; |
| 193 | if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) { |
| 194 | err = -errno; |
| 195 | ALOGE("%s: ION_IOC_IMPORT failed with error - %s", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 196 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 197 | return err; |
| 198 | } |
| 199 | |
| 200 | handle_data.handle = fd_data.handle; |
| 201 | flush_data.handle = fd_data.handle; |
| 202 | flush_data.vaddr = base; |
| 203 | flush_data.offset = offset; |
| 204 | flush_data.length = size; |
Saurabh Shah | ece296e | 2012-09-11 13:33:44 -0700 | [diff] [blame] | 205 | |
| 206 | struct ion_custom_data d; |
| 207 | d.cmd = ION_IOC_CLEAN_INV_CACHES; |
| 208 | d.arg = (unsigned long int)&flush_data; |
| 209 | |
| 210 | if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 211 | err = -errno; |
| 212 | ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", |
Saurabh Shah | ece296e | 2012-09-11 13:33:44 -0700 | [diff] [blame] | 213 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 214 | __FUNCTION__, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 215 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
| 216 | return err; |
| 217 | } |
| 218 | ioctl(mIonFd, ION_IOC_FREE, &handle_data); |
| 219 | return 0; |
| 220 | } |
| 221 | |