1 /*
2  * Copyright (c) 2011-2018, 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 #include <linux/msm_ion.h>
35 #if TARGET_ION_ABI_VERSION >= 2
36 #include <linux/dma-buf.h>
37 #include <ion/ion.h>
38 #endif
39 #include <stdlib.h>
40 #include <fcntl.h>
41 #include <log/log.h>
42 #include <cutils/trace.h>
43 #include <errno.h>
44 #include <utils/Trace.h>
45 #include <string>
46 
47 #include "gr_utils.h"
48 #include "gralloc_priv.h"
49 #include "gr_ion_alloc.h"
50 
51 namespace gralloc {
52 
Init()53 bool IonAlloc::Init() {
54   if (ion_dev_fd_ == FD_INIT) {
55     ion_dev_fd_ = OpenIonDevice();
56   }
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 
67 #if TARGET_ION_ABI_VERSION >= 2  // Use libion APIs for new ion
68 
OpenIonDevice()69 int IonAlloc::OpenIonDevice() {
70   return ion_open();
71 }
72 
CloseIonDevice()73 void 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 
AllocBuffer(AllocData * data)81 int 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) {
98     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);
100     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 
FreeBuffer(void * base,unsigned int size,unsigned int offset,int fd,int)110 int 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 
ImportBuffer(int fd)124 int IonAlloc::ImportBuffer(int fd) {
125   // For new ion api ion_handle does not exists so reusing fd for now
126   return fd;
127 }
128 
CleanBuffer(void *,unsigned int,unsigned int,int,int op,int dma_buf_fd)129 int 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;
144     case CACHE_READ_DONE:
145       sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
146       break;
147     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 
OpenIonDevice()164 int IonAlloc::OpenIonDevice() {
165   return open(kIonDevice, O_RDONLY);
166 }
167 
CloseIonDevice()168 void IonAlloc::CloseIonDevice() {
169   if (ion_dev_fd_ > FD_INIT) {
170     close(ion_dev_fd_);
171   }
172 
173   ion_dev_fd_ = FD_INIT;
174 }
175 
AllocBuffer(AllocData * data)176 int 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;
182 
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;
188   std::string tag_name{};
189   if (ATRACE_ENABLED()) {
190     tag_name = "ION_IOC_ALLOC size: " + std::to_string(data->size);
191   }
192 
193   ATRACE_BEGIN(tag_name.c_str());
194   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   }
199   ATRACE_END();
200 
201   fd_data.handle = ion_alloc_data.handle;
202   handle_data.handle = ion_alloc_data.handle;
203   ATRACE_BEGIN("ION_IOC_MAP");
204   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   }
210   ATRACE_END();
211 
212   data->fd = fd_data.fd;
213   data->ion_handle = handle_data.handle;
214   ALOGD_IF(DEBUG, "ion: Allocated buffer size:%zu fd:%d handle:0x%x", ion_alloc_data.len, data->fd,
215            data->ion_handle);
216 
217   return 0;
218 }
219 
FreeBuffer(void * base,unsigned int size,unsigned int offset,int fd,int ion_handle)220 int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
221                          int ion_handle) {
222   ATRACE_CALL();
223   int err = 0;
224   ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d handle:0x%x", base, size, fd,
225            ion_handle);
226 
227   if (base) {
228     err = UnmapBuffer(base, size, offset);
229   }
230 
231   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);
237   return err;
238 }
239 
ImportBuffer(int fd)240 int 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 
CleanBuffer(void * base,unsigned int size,unsigned int offset,int handle,int op,int)252 int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
253                           int /*fd*/) {
254   if (op == CACHE_READ_DONE)  {
255     return 0;
256   }
257 
258   ATRACE_CALL();
259   ATRACE_INT("operation id", op);
260   struct ion_flush_data flush_data;
261   int err = 0;
262 
263   flush_data.handle = handle;
264   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));
286     return err;
287   }
288 
289   return 0;
290 }
291 
292 #else  // This ion version is not supported
293 
OpenIonDevice()294 int IonAlloc::OpenIonDevice() {
295   return -EINVAL;
296 }
297 
CloseIonDevice()298 void IonAlloc::CloseIonDevice() {
299 }
300 
AllocBuffer(AllocData *)301 int IonAlloc::AllocBuffer(AllocData * /*data*/) {
302   return -EINVAL;
303 }
304 
FreeBuffer(void *,unsigned int,unsigned int,int,int)305 int IonAlloc::FreeBuffer(void * /*base*/, unsigned int /*size*/, unsigned int /*offset*/,
306                          int /*fd*/, int /*ion_handle*/) {
307   return -EINVAL;
308 }
309 
ImportBuffer(int)310 int IonAlloc::ImportBuffer(int /*fd*/) {
311   return -EINVAL;
312 }
313 
CleanBuffer(void *,unsigned int,unsigned int,int,int,int)314 int 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 
MapBuffer(void ** base,unsigned int size,unsigned int offset,int fd)323 int 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 
UnmapBuffer(void * base,unsigned int size,unsigned int)340 int 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 
353 }  // namespace gralloc
354