1 /*
2 * Copyright (c) 2011-2019, The Linux Foundation. All rights reserved.
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>
34
35 #include <linux/dma-buf.h>
36 #include <ion/ion.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <log/log.h>
40 #include <cutils/trace.h>
41 #include <errno.h>
42 #include <utils/Trace.h>
43 #include <string>
44
45 #include "gr_utils.h"
46 #include "gralloc_priv.h"
47 #include "gr_ion_alloc.h"
48
49 namespace gralloc {
50
Init()51 bool IonAlloc::Init() {
52 if (ion_dev_fd_ == FD_INIT) {
53 ion_dev_fd_ = OpenIonDevice();
54 }
55
56 if (ion_dev_fd_ < 0) {
57 ALOGE("%s: Failed to open ion device - %s", __FUNCTION__, strerror(errno));
58 ion_dev_fd_ = FD_INIT;
59 return false;
60 }
61
62 return true;
63 }
64
OpenIonDevice()65 int IonAlloc::OpenIonDevice() {
66 return ion_open();
67 }
68
CloseIonDevice()69 void IonAlloc::CloseIonDevice() {
70 if (ion_dev_fd_ > FD_INIT) {
71 ion_close(ion_dev_fd_);
72 }
73
74 ion_dev_fd_ = FD_INIT;
75 }
76
AllocBuffer(AllocData * data)77 int IonAlloc::AllocBuffer(AllocData *data) {
78 ATRACE_CALL();
79 int err = 0;
80 int fd = -1;
81 unsigned int flags = data->flags;
82
83 flags |= data->uncached ? 0 : ION_FLAG_CACHED;
84
85 std::string tag_name{};
86 if (ATRACE_ENABLED()) {
87 tag_name = "libion alloc size: " + std::to_string(data->size);
88 }
89
90 ATRACE_BEGIN(tag_name.c_str());
91 err = ion_alloc_fd(ion_dev_fd_, data->size, data->align, data->heap_id, flags, &fd);
92 ATRACE_END();
93 if (err) {
94 ALOGE("libion alloc failed ion_fd %d size %d align %d heap_id %x flags %x",
95 ion_dev_fd_, data->size, data->align, data->heap_id, flags);
96 return err;
97 }
98
99 data->fd = fd;
100 data->ion_handle = fd; // For new ion api ion_handle does not exists so reusing fd for now
101 ALOGD_IF(DEBUG, "libion: Allocated buffer size:%u fd:%d", data->size, data->fd);
102
103 return 0;
104 }
105
FreeBuffer(void * base,unsigned int size,unsigned int offset,int fd,int)106 int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
107 int /*ion_handle*/) {
108 ATRACE_CALL();
109 int err = 0;
110 ALOGD_IF(DEBUG, "libion: Freeing buffer base:%p size:%u fd:%d", base, size, fd);
111
112 if (base) {
113 err = UnmapBuffer(base, size, offset);
114 }
115
116 close(fd);
117 return err;
118 }
119
ImportBuffer(int fd)120 int IonAlloc::ImportBuffer(int fd) {
121 // For new ion api ion_handle does not exists so reusing fd for now
122 return fd;
123 }
124
CleanBuffer(void *,unsigned int,unsigned int,int,int op,int dma_buf_fd)125 int IonAlloc::CleanBuffer(void */*base*/, unsigned int /*size*/, unsigned int /*offset*/,
126 int /*handle*/, int op, int dma_buf_fd) {
127 ATRACE_CALL();
128 ATRACE_INT("operation id", op);
129
130 struct dma_buf_sync sync;
131 int err = 0;
132
133 switch (op) {
134 case CACHE_CLEAN:
135 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
136 break;
137 case CACHE_INVALIDATE:
138 sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
139 break;
140 case CACHE_READ_DONE:
141 sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
142 break;
143 default:
144 ALOGE("%s: Invalid operation %d", __FUNCTION__, op);
145 return -1;
146 }
147
148 if (ioctl(dma_buf_fd, INT(DMA_BUF_IOCTL_SYNC), &sync)) {
149 err = -errno;
150 ALOGE("%s: DMA_BUF_IOCTL_SYNC failed with error - %s", __FUNCTION__, strerror(errno));
151 return err;
152 }
153
154 return 0;
155 }
156
MapBuffer(void ** base,unsigned int size,unsigned int offset,int fd)157 int IonAlloc::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
158 ATRACE_CALL();
159 int err = 0;
160 void *addr = 0;
161
162 addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
163 *base = addr;
164 if (addr == MAP_FAILED) {
165 err = -errno;
166 ALOGE("ion: Failed to map memory in the client: %s", strerror(errno));
167 } else {
168 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d", addr, size, offset, fd);
169 }
170
171 return err;
172 }
173
UnmapBuffer(void * base,unsigned int size,unsigned int)174 int IonAlloc::UnmapBuffer(void *base, unsigned int size, unsigned int /*offset*/) {
175 ATRACE_CALL();
176 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%u", base, size);
177
178 int err = 0;
179 if (munmap(base, size)) {
180 err = -errno;
181 ALOGE("ion: Failed to unmap memory at %p : %s", base, strerror(errno));
182 }
183
184 return err;
185 }
186
187 } // namespace gralloc
188