blob: 33a13abf7070244866022b20b9a97d0d50ca7b2e [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmeda422f352017-12-01 15:33:56 -05002 * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303
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)
Naseer Ahmed04a804a2018-03-06 20:41:14 -050032#include <log/log.h>
Naseer Ahmeda422f352017-12-01 15:33:56 -050033#include <cutils/trace.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <stdlib.h>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053037#include <sys/ioctl.h>
38#include <sys/mman.h>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053039#include <utils/Trace.h>
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040040#include <string>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053041
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053042#include "gr_ion_alloc.h"
Naseer Ahmeda422f352017-12-01 15:33:56 -050043#include "gr_utils.h"
44#include "gralloc_priv.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053045
Naseer Ahmeda422f352017-12-01 15:33:56 -050046namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053047
48bool IonAlloc::Init() {
49 if (ion_dev_fd_ == FD_INIT) {
50 ion_dev_fd_ = open(kIonDevice, O_RDONLY);
51 }
52
53 if (ion_dev_fd_ < 0) {
54 ALOGE("%s: Failed to open ion device - %s", __FUNCTION__, strerror(errno));
55 ion_dev_fd_ = FD_INIT;
56 return false;
57 }
58
59 return true;
60}
61
62void IonAlloc::CloseIonDevice() {
63 if (ion_dev_fd_ > FD_INIT) {
64 close(ion_dev_fd_);
65 }
66
67 ion_dev_fd_ = FD_INIT;
68}
69
70int IonAlloc::AllocBuffer(AllocData *data) {
71 ATRACE_CALL();
72 int err = 0;
73 struct ion_handle_data handle_data;
74 struct ion_fd_data fd_data;
75 struct ion_allocation_data ion_alloc_data;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053076
77 ion_alloc_data.len = data->size;
78 ion_alloc_data.align = data->align;
79 ion_alloc_data.heap_id_mask = data->heap_id;
80 ion_alloc_data.flags = data->flags;
81 ion_alloc_data.flags |= data->uncached ? 0 : ION_FLAG_CACHED;
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040082 std::string tag_name{};
83 if (ATRACE_ENABLED()) {
84 tag_name = "ION_IOC_ALLOC size: " + std::to_string(data->size);
85 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053086
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040087 ATRACE_BEGIN(tag_name.c_str());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053088 if (ioctl(ion_dev_fd_, INT(ION_IOC_ALLOC), &ion_alloc_data)) {
89 err = -errno;
90 ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
91 return err;
92 }
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040093 ATRACE_END();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053094
95 fd_data.handle = ion_alloc_data.handle;
96 handle_data.handle = ion_alloc_data.handle;
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040097 ATRACE_BEGIN("ION_IOC_MAP");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053098 if (ioctl(ion_dev_fd_, INT(ION_IOC_MAP), &fd_data)) {
99 err = -errno;
100 ALOGE("%s: ION_IOC_MAP failed with error - %s", __FUNCTION__, strerror(errno));
101 ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
102 return err;
103 }
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400104 ATRACE_END();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530105
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530106 data->fd = fd_data.fd;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500107 data->ion_handle = handle_data.handle;
Naseer Ahmeda422f352017-12-01 15:33:56 -0500108 ALOGD_IF(DEBUG, "ion: Allocated buffer size:%zu fd:%d handle:0x%x", ion_alloc_data.len, data->fd,
109 data->ion_handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530110
111 return 0;
112}
113
Naseer Ahmede69031e2016-11-22 20:05:16 -0500114int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
115 int ion_handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530116 ATRACE_CALL();
117 int err = 0;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500118 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d handle:0x%x", base, size, fd,
119 ion_handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530120
121 if (base) {
122 err = UnmapBuffer(base, size, offset);
123 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530124
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400125 if (ion_handle > 0) {
126 struct ion_handle_data handle_data;
127 handle_data.handle = ion_handle;
128 ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
129 }
130 close(fd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530131 return err;
132}
133
134int IonAlloc::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
135 ATRACE_CALL();
136 int err = 0;
137 void *addr = 0;
138
139 // It is a (quirky) requirement of ION to have opened the
140 // ion fd in the process that is doing the mapping
141 addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
142 *base = addr;
143 if (addr == MAP_FAILED) {
144 err = -errno;
145 ALOGE("ion: Failed to map memory in the client: %s", strerror(errno));
146 } else {
147 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d", addr, size, offset, fd);
148 }
149
150 return err;
151}
152
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400153int IonAlloc::ImportBuffer(int fd) {
154 struct ion_fd_data fd_data;
155 int err = 0;
156 fd_data.fd = fd;
157 if (ioctl(ion_dev_fd_, INT(ION_IOC_IMPORT), &fd_data)) {
158 err = -errno;
159 ALOGE("%s: ION_IOC_IMPORT failed with error - %s", __FUNCTION__, strerror(errno));
160 return err;
161 }
162 return fd_data.handle;
163}
164
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530165int IonAlloc::UnmapBuffer(void *base, unsigned int size, unsigned int /*offset*/) {
166 ATRACE_CALL();
167 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%u", base, size);
168
169 int err = 0;
170 if (munmap(base, size)) {
171 err = -errno;
172 ALOGE("ion: Failed to unmap memory at %p : %s", base, strerror(errno));
173 }
174
175 return err;
176}
177
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400178int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530179 ATRACE_CALL();
180 ATRACE_INT("operation id", op);
181 struct ion_flush_data flush_data;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530182 int err = 0;
183
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400184 flush_data.handle = handle;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530185 flush_data.vaddr = base;
186 // offset and length are unsigned int
187 flush_data.offset = offset;
188 flush_data.length = size;
189
190 struct ion_custom_data d;
191 switch (op) {
192 case CACHE_CLEAN:
193 d.cmd = ION_IOC_CLEAN_CACHES;
194 break;
195 case CACHE_INVALIDATE:
196 d.cmd = ION_IOC_INV_CACHES;
197 break;
198 case CACHE_CLEAN_AND_INVALIDATE:
199 default:
200 d.cmd = ION_IOC_CLEAN_INV_CACHES;
201 }
202
203 d.arg = (unsigned long)(&flush_data); // NOLINT
204 if (ioctl(ion_dev_fd_, INT(ION_IOC_CUSTOM), &d)) {
205 err = -errno;
206 ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", __FUNCTION__, strerror(errno));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530207 return err;
208 }
209
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530210 return 0;
211}
212
Naseer Ahmeda422f352017-12-01 15:33:56 -0500213} // namespace gralloc