• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011-2017, 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 
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <sys/ioctl.h>
37 #include <sys/mman.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 
41 #include <string>
42 
43 #include <log/log.h>
44 #include <utils/Trace.h>
45 #include <cutils/trace.h>
46 
47 #include "gralloc_priv.h"
48 #include "gr_utils.h"
49 #include "gr_ion_alloc.h"
50 
51 namespace gralloc1 {
52 
Init()53 bool IonAlloc::Init() {
54   if (ion_dev_fd_ == FD_INIT) {
55     ion_dev_fd_ = open(kIonDevice, O_RDONLY);
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 
CloseIonDevice()67 void IonAlloc::CloseIonDevice() {
68   if (ion_dev_fd_ > FD_INIT) {
69     close(ion_dev_fd_);
70   }
71 
72   ion_dev_fd_ = FD_INIT;
73 }
74 
AllocBuffer(AllocData * data)75 int IonAlloc::AllocBuffer(AllocData *data) {
76   ATRACE_CALL();
77   int err = 0;
78   struct ion_handle_data handle_data;
79   struct ion_fd_data fd_data;
80   struct ion_allocation_data ion_alloc_data;
81 
82   ion_alloc_data.len = data->size;
83   ion_alloc_data.align = data->align;
84   ion_alloc_data.heap_id_mask = data->heap_id;
85   ion_alloc_data.flags = data->flags;
86   ion_alloc_data.flags |= data->uncached ? 0 : ION_FLAG_CACHED;
87   std::string tag_name{};
88   if (ATRACE_ENABLED()) {
89     tag_name = "ION_IOC_ALLOC size: " + std::to_string(data->size);
90   }
91 
92   ATRACE_BEGIN(tag_name.c_str());
93   if (ioctl(ion_dev_fd_, INT(ION_IOC_ALLOC), &ion_alloc_data)) {
94     err = -errno;
95     ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
96     return err;
97   }
98   ATRACE_END();
99 
100   fd_data.handle = ion_alloc_data.handle;
101   handle_data.handle = ion_alloc_data.handle;
102   ATRACE_BEGIN("ION_IOC_MAP");
103   if (ioctl(ion_dev_fd_, INT(ION_IOC_MAP), &fd_data)) {
104     err = -errno;
105     ALOGE("%s: ION_IOC_MAP failed with error - %s", __FUNCTION__, strerror(errno));
106     ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
107     return err;
108   }
109   ATRACE_END();
110 
111   data->fd = fd_data.fd;
112   data->ion_handle = handle_data.handle;
113   ALOGD_IF(DEBUG, "ion: Allocated buffer size:%zu fd:%d handle:0x%x",
114           ion_alloc_data.len, data->fd, data->ion_handle);
115 
116   return 0;
117 }
118 
FreeBuffer(void * base,unsigned int size,unsigned int offset,int fd,int ion_handle)119 int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd,
120                          int ion_handle) {
121   ATRACE_CALL();
122   int err = 0;
123   ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d handle:0x%x", base, size, fd,
124            ion_handle);
125 
126   if (base) {
127     err = UnmapBuffer(base, size, offset);
128   }
129 
130   if (ion_handle > 0) {
131     struct ion_handle_data handle_data;
132     handle_data.handle = ion_handle;
133     ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
134   }
135   close(fd);
136   return err;
137 }
138 
MapBuffer(void ** base,unsigned int size,unsigned int offset,int fd)139 int IonAlloc::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
140   ATRACE_CALL();
141   int err = 0;
142   void *addr = 0;
143 
144   // It is a (quirky) requirement of ION to have opened the
145   // ion fd in the process that is doing the mapping
146   addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
147   *base = addr;
148   if (addr == MAP_FAILED) {
149     err = -errno;
150     ALOGE("ion: Failed to map memory in the client: %s", strerror(errno));
151   } else {
152     ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d", addr, size, offset, fd);
153   }
154 
155   return err;
156 }
157 
ImportBuffer(int fd)158 int IonAlloc::ImportBuffer(int fd) {
159   struct ion_fd_data fd_data;
160   int err = 0;
161   fd_data.fd = fd;
162   if (ioctl(ion_dev_fd_, INT(ION_IOC_IMPORT), &fd_data)) {
163     err = -errno;
164     ALOGE("%s: ION_IOC_IMPORT failed with error - %s", __FUNCTION__, strerror(errno));
165     return err;
166   }
167   return fd_data.handle;
168 }
169 
UnmapBuffer(void * base,unsigned int size,unsigned int)170 int IonAlloc::UnmapBuffer(void *base, unsigned int size, unsigned int /*offset*/) {
171   ATRACE_CALL();
172   ALOGD_IF(DEBUG, "ion: Unmapping buffer  base:%p size:%u", base, size);
173 
174   int err = 0;
175   if (munmap(base, size)) {
176     err = -errno;
177     ALOGE("ion: Failed to unmap memory at %p : %s", base, strerror(errno));
178   }
179 
180   return err;
181 }
182 
CleanBuffer(void * base,unsigned int size,unsigned int offset,int handle,int op)183 int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op) {
184   ATRACE_CALL();
185   ATRACE_INT("operation id", op);
186   struct ion_flush_data flush_data;
187   int err = 0;
188 
189   flush_data.handle = handle;
190   flush_data.vaddr = base;
191   // offset and length are unsigned int
192   flush_data.offset = offset;
193   flush_data.length = size;
194 
195   struct ion_custom_data d;
196   switch (op) {
197     case CACHE_CLEAN:
198       d.cmd = ION_IOC_CLEAN_CACHES;
199       break;
200     case CACHE_INVALIDATE:
201       d.cmd = ION_IOC_INV_CACHES;
202       break;
203     case CACHE_CLEAN_AND_INVALIDATE:
204     default:
205       d.cmd = ION_IOC_CLEAN_INV_CACHES;
206   }
207 
208   d.arg = (unsigned long)(&flush_data);  // NOLINT
209   if (ioctl(ion_dev_fd_, INT(ION_IOC_CUSTOM), &d)) {
210     err = -errno;
211     ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", __FUNCTION__, strerror(errno));
212     return err;
213   }
214 
215   return 0;
216 }
217 
218 }  // namespace gralloc1
219