blob: a9e06da833872b673f7f8d3fde27251343495f23 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Rohit Kulkarni3ac98392017-10-20 12:04:34 -07002 * 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)
32#include <sys/ioctl.h>
33#include <sys/mman.h>
Rohit Kulkarni3ac98392017-10-20 12:04:34 -070034#include <linux/msm_ion.h>
35#if TARGET_ION_ABI_VERSION >= 2
Rohit Kulkarni3ac98392017-10-20 12:04:34 -070036#include <linux/dma-buf.h>
Naseer Ahmede36f2242017-12-01 15:33:56 -050037#include <ion/ion.h>
Rohit Kulkarni3ac98392017-10-20 12:04:34 -070038#endif
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053039#include <stdlib.h>
40#include <fcntl.h>
Naseer Ahmedfcad05e2018-03-06 20:41:14 -050041#include <log/log.h>
Naseer Ahmede36f2242017-12-01 15:33:56 -050042#include <cutils/trace.h>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053043#include <errno.h>
44#include <utils/Trace.h>
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040045#include <string>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053046
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053047#include "gr_utils.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050048#include "gralloc_priv.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053049#include "gr_ion_alloc.h"
50
Naseer Ahmede36f2242017-12-01 15:33:56 -050051namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053052
53bool IonAlloc::Init() {
54 if (ion_dev_fd_ == FD_INIT) {
Rohit Kulkarni3ac98392017-10-20 12:04:34 -070055 ion_dev_fd_ = OpenIonDevice();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053056 }
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 Kulkarni3ac98392017-10-20 12:04:34 -070067#if TARGET_ION_ABI_VERSION >= 2 // Use libion APIs for new ion
68
69int IonAlloc::OpenIonDevice() {
70 return ion_open();
71}
72
73void 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
81int 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 Radhakrishnan30ea0f02018-10-11 19:25:10 -070098 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 Kulkarni3ac98392017-10-20 12:04:34 -0700100 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
110int 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
124int IonAlloc::ImportBuffer(int fd) {
125 // For new ion api ion_handle does not exists so reusing fd for now
126 return fd;
127}
128
129int 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 Kulkarnib20abe72018-03-13 16:55:10 -0700144 case CACHE_READ_DONE:
145 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
146 break;
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700147 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
164int IonAlloc::OpenIonDevice() {
165 return open(kIonDevice, O_RDONLY);
166}
167
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530168void IonAlloc::CloseIonDevice() {
169 if (ion_dev_fd_ > FD_INIT) {
170 close(ion_dev_fd_);
171 }
172
173 ion_dev_fd_ = FD_INIT;
174}
175
176int 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 Kandula96e92342016-03-24 21:03:35 +0530182
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 Ahmed7a2b09c2017-05-11 13:03:17 -0400188 std::string tag_name{};
189 if (ATRACE_ENABLED()) {
190 tag_name = "ION_IOC_ALLOC size: " + std::to_string(data->size);
191 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530192
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400193 ATRACE_BEGIN(tag_name.c_str());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530194 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 Ahmed7a2b09c2017-05-11 13:03:17 -0400199 ATRACE_END();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530200
201 fd_data.handle = ion_alloc_data.handle;
202 handle_data.handle = ion_alloc_data.handle;
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400203 ATRACE_BEGIN("ION_IOC_MAP");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530204 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 Ahmed7a2b09c2017-05-11 13:03:17 -0400210 ATRACE_END();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530211
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530212 data->fd = fd_data.fd;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500213 data->ion_handle = handle_data.handle;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500214 ALOGD_IF(DEBUG, "ion: Allocated buffer size:%zu fd:%d handle:0x%x", ion_alloc_data.len, data->fd,
215 data->ion_handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530216
217 return 0;
218}
219
Naseer Ahmede69031e2016-11-22 20:05:16 -0500220int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
221 int ion_handle) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530222 ATRACE_CALL();
223 int err = 0;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500224 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d handle:0x%x", base, size, fd,
225 ion_handle);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530226
227 if (base) {
228 err = UnmapBuffer(base, size, offset);
229 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530230
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400231 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 Kandula96e92342016-03-24 21:03:35 +0530237 return err;
238}
239
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400240int 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 Kulkarni3ac98392017-10-20 12:04:34 -0700252int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
253 int /*fd*/) {
Rohit Kulkarnib20abe72018-03-13 16:55:10 -0700254 if (op == CACHE_READ_DONE) {
255 return 0;
256 }
257
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530258 ATRACE_CALL();
259 ATRACE_INT("operation id", op);
260 struct ion_flush_data flush_data;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530261 int err = 0;
262
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -0400263 flush_data.handle = handle;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530264 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 Kandula96e92342016-03-24 21:03:35 +0530286 return err;
287 }
288
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530289 return 0;
290}
291
Rohit Kulkarni3ac98392017-10-20 12:04:34 -0700292#else // This ion version is not supported
293
294int IonAlloc::OpenIonDevice() {
295 return -EINVAL;
296}
297
298void IonAlloc::CloseIonDevice() {
299}
300
301int IonAlloc::AllocBuffer(AllocData * /*data*/) {
302 return -EINVAL;
303}
304
305int IonAlloc::FreeBuffer(void * /*base*/, unsigned int /*size*/, unsigned int /*offset*/,
306 int /*fd*/, int /*ion_handle*/) {
307 return -EINVAL;
308}
309
310int IonAlloc::ImportBuffer(int /*fd*/) {
311 return -EINVAL;
312}
313
314int 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
323int 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
340int 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 Ahmede36f2242017-12-01 15:33:56 -0500353} // namespace gralloc