1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/*
* Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define DEBUG 0
#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/msm_ion.h>
#if TARGET_ION_ABI_VERSION >= 2
#include <linux/dma-buf.h>
#include <ion/ion.h>
#endif
#include <stdlib.h>
#include <fcntl.h>
#include <log/log.h>
#include <cutils/trace.h>
#include <errno.h>
#include <utils/Trace.h>
#include <string>
#include "gr_utils.h"
#include "gralloc_priv.h"
#include "gr_ion_alloc.h"
namespace gralloc {
bool IonAlloc::Init() {
if (ion_dev_fd_ == FD_INIT) {
ion_dev_fd_ = OpenIonDevice();
}
if (ion_dev_fd_ < 0) {
ALOGE("%s: Failed to open ion device - %s", __FUNCTION__, strerror(errno));
ion_dev_fd_ = FD_INIT;
return false;
}
return true;
}
#if TARGET_ION_ABI_VERSION >= 2 // Use libion APIs for new ion
int IonAlloc::OpenIonDevice() {
return ion_open();
}
void IonAlloc::CloseIonDevice() {
if (ion_dev_fd_ > FD_INIT) {
ion_close(ion_dev_fd_);
}
ion_dev_fd_ = FD_INIT;
}
int IonAlloc::AllocBuffer(AllocData *data) {
ATRACE_CALL();
int err = 0;
int fd = -1;
unsigned int flags = data->flags;
flags |= data->uncached ? 0 : ION_FLAG_CACHED;
std::string tag_name{};
if (ATRACE_ENABLED()) {
tag_name = "libion alloc size: " + std::to_string(data->size);
}
ATRACE_BEGIN(tag_name.c_str());
err = ion_alloc_fd(ion_dev_fd_, data->size, data->align, data->heap_id, flags, &fd);
ATRACE_END();
if (err) {
ALOGE("libion alloc failed ion_fd %d size %d align %d heap_id %x flags %x",
ion_dev_fd_, data->size, data->align, data->heap_id, flags);
return err;
}
data->fd = fd;
data->ion_handle = fd; // For new ion api ion_handle does not exists so reusing fd for now
ALOGD_IF(DEBUG, "libion: Allocated buffer size:%u fd:%d", data->size, data->fd);
return 0;
}
int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
int /*ion_handle*/) {
ATRACE_CALL();
int err = 0;
ALOGD_IF(DEBUG, "libion: Freeing buffer base:%p size:%u fd:%d", base, size, fd);
if (base) {
err = UnmapBuffer(base, size, offset);
}
close(fd);
return err;
}
int IonAlloc::ImportBuffer(int fd) {
// For new ion api ion_handle does not exists so reusing fd for now
return fd;
}
int IonAlloc::CleanBuffer(void */*base*/, unsigned int /*size*/, unsigned int /*offset*/,
int /*handle*/, int op, int dma_buf_fd) {
ATRACE_CALL();
ATRACE_INT("operation id", op);
struct dma_buf_sync sync;
int err = 0;
switch (op) {
case CACHE_CLEAN:
sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
break;
case CACHE_INVALIDATE:
sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
break;
case CACHE_READ_DONE:
sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
break;
default:
ALOGE("%s: Invalid operation %d", __FUNCTION__, op);
return -1;
}
if (ioctl(dma_buf_fd, INT(DMA_BUF_IOCTL_SYNC), &sync)) {
err = -errno;
ALOGE("%s: DMA_BUF_IOCTL_SYNC failed with error - %s", __FUNCTION__, strerror(errno));
return err;
}
return 0;
}
#else
#ifndef TARGET_ION_ABI_VERSION // Use old ion apis directly
int IonAlloc::OpenIonDevice() {
return open(kIonDevice, O_RDONLY);
}
void IonAlloc::CloseIonDevice() {
if (ion_dev_fd_ > FD_INIT) {
close(ion_dev_fd_);
}
ion_dev_fd_ = FD_INIT;
}
int IonAlloc::AllocBuffer(AllocData *data) {
ATRACE_CALL();
int err = 0;
struct ion_handle_data handle_data;
struct ion_fd_data fd_data;
struct ion_allocation_data ion_alloc_data;
ion_alloc_data.len = data->size;
ion_alloc_data.align = data->align;
ion_alloc_data.heap_id_mask = data->heap_id;
ion_alloc_data.flags = data->flags;
ion_alloc_data.flags |= data->uncached ? 0 : ION_FLAG_CACHED;
std::string tag_name{};
if (ATRACE_ENABLED()) {
tag_name = "ION_IOC_ALLOC size: " + std::to_string(data->size);
}
ATRACE_BEGIN(tag_name.c_str());
if (ioctl(ion_dev_fd_, INT(ION_IOC_ALLOC), &ion_alloc_data)) {
err = -errno;
ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
return err;
}
ATRACE_END();
fd_data.handle = ion_alloc_data.handle;
handle_data.handle = ion_alloc_data.handle;
ATRACE_BEGIN("ION_IOC_MAP");
if (ioctl(ion_dev_fd_, INT(ION_IOC_MAP), &fd_data)) {
err = -errno;
ALOGE("%s: ION_IOC_MAP failed with error - %s", __FUNCTION__, strerror(errno));
ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
return err;
}
ATRACE_END();
data->fd = fd_data.fd;
data->ion_handle = handle_data.handle;
ALOGD_IF(DEBUG, "ion: Allocated buffer size:%zu fd:%d handle:0x%x", ion_alloc_data.len, data->fd,
data->ion_handle);
return 0;
}
int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
int ion_handle) {
ATRACE_CALL();
int err = 0;
ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d handle:0x%x", base, size, fd,
ion_handle);
if (base) {
err = UnmapBuffer(base, size, offset);
}
if (ion_handle > 0) {
struct ion_handle_data handle_data;
handle_data.handle = ion_handle;
ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
}
close(fd);
return err;
}
int IonAlloc::ImportBuffer(int fd) {
struct ion_fd_data fd_data;
int err = 0;
fd_data.fd = fd;
if (ioctl(ion_dev_fd_, INT(ION_IOC_IMPORT), &fd_data)) {
err = -errno;
ALOGE("%s: ION_IOC_IMPORT failed with error - %s", __FUNCTION__, strerror(errno));
return err;
}
return fd_data.handle;
}
int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
int /*fd*/) {
if (op == CACHE_READ_DONE) {
return 0;
}
ATRACE_CALL();
ATRACE_INT("operation id", op);
struct ion_flush_data flush_data;
int err = 0;
flush_data.handle = handle;
flush_data.vaddr = base;
// offset and length are unsigned int
flush_data.offset = offset;
flush_data.length = size;
struct ion_custom_data d;
switch (op) {
case CACHE_CLEAN:
d.cmd = ION_IOC_CLEAN_CACHES;
break;
case CACHE_INVALIDATE:
d.cmd = ION_IOC_INV_CACHES;
break;
case CACHE_CLEAN_AND_INVALIDATE:
default:
d.cmd = ION_IOC_CLEAN_INV_CACHES;
}
d.arg = (unsigned long)(&flush_data); // NOLINT
if (ioctl(ion_dev_fd_, INT(ION_IOC_CUSTOM), &d)) {
err = -errno;
ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", __FUNCTION__, strerror(errno));
return err;
}
return 0;
}
#else // This ion version is not supported
int IonAlloc::OpenIonDevice() {
return -EINVAL;
}
void IonAlloc::CloseIonDevice() {
}
int IonAlloc::AllocBuffer(AllocData * /*data*/) {
return -EINVAL;
}
int IonAlloc::FreeBuffer(void * /*base*/, unsigned int /*size*/, unsigned int /*offset*/,
int /*fd*/, int /*ion_handle*/) {
return -EINVAL;
}
int IonAlloc::ImportBuffer(int /*fd*/) {
return -EINVAL;
}
int IonAlloc::CleanBuffer(void * /*base*/, unsigned int /*size*/, unsigned int /*offset*/,
int /*handle*/, int /*op*/, int /*fd*/) {
return -EINVAL;
}
#endif
#endif // TARGET_ION_ABI_VERSION
int IonAlloc::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
ATRACE_CALL();
int err = 0;
void *addr = 0;
addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
*base = addr;
if (addr == MAP_FAILED) {
err = -errno;
ALOGE("ion: Failed to map memory in the client: %s", strerror(errno));
} else {
ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d", addr, size, offset, fd);
}
return err;
}
int IonAlloc::UnmapBuffer(void *base, unsigned int size, unsigned int /*offset*/) {
ATRACE_CALL();
ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%u", base, size);
int err = 0;
if (munmap(base, size)) {
err = -errno;
ALOGE("ion: Failed to unmap memory at %p : %s", base, strerror(errno));
}
return err;
}
} // namespace gralloc
|