1 /*
2 * Copyright (c) 2011-2014, 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 <stdlib.h>
35 #include <fcntl.h>
36 #include <cutils/log.h>
37 #include <errno.h>
38 #include <utils/Trace.h>
39 #include "gralloc_priv.h"
40 #include "ionalloc.h"
41
42 using gralloc::IonAlloc;
43
44 #define ION_DEVICE "/dev/ion"
45
open_device()46 int IonAlloc::open_device()
47 {
48 if(mIonFd == FD_INIT)
49 mIonFd = open(ION_DEVICE, O_RDONLY);
50
51 if(mIonFd < 0 ) {
52 ALOGE("%s: Failed to open ion device - %s",
53 __FUNCTION__, strerror(errno));
54 mIonFd = FD_INIT;
55 return -errno;
56 }
57 return 0;
58 }
59
close_device()60 void IonAlloc::close_device()
61 {
62 if(mIonFd >= 0)
63 close(mIonFd);
64 mIonFd = FD_INIT;
65 }
66
alloc_buffer(alloc_data & data)67 int IonAlloc::alloc_buffer(alloc_data& data)
68 {
69 ATRACE_CALL();
70 Locker::Autolock _l(mLock);
71 int err = 0;
72 struct ion_handle_data handle_data;
73 struct ion_fd_data fd_data;
74 struct ion_allocation_data ionAllocData;
75 void *base = 0;
76
77 ionAllocData.len = data.size;
78 ionAllocData.align = data.align;
79 ionAllocData.heap_id_mask = data.heapId;
80 ionAllocData.flags = data.flags;
81 ionAllocData.flags |= data.uncached ? 0 : ION_FLAG_CACHED;
82 err = open_device();
83 if (err)
84 return err;
85 if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) {
86 err = -errno;
87 ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
88 return err;
89 }
90
91 fd_data.handle = ionAllocData.handle;
92 handle_data.handle = ionAllocData.handle;
93 if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) {
94 err = -errno;
95 ALOGE("%s: ION_IOC_MAP failed with error - %s",
96 __FUNCTION__, strerror(errno));
97 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
98 return err;
99 }
100
101 if(!(data.flags & ION_SECURE)) {
102 base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
103 MAP_SHARED, fd_data.fd, 0);
104 if(base == MAP_FAILED) {
105 err = -errno;
106 ALOGE("%s: Failed to map the allocated memory: %s",
107 __FUNCTION__, strerror(errno));
108 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
109 return err;
110 }
111 }
112
113 data.base = base;
114 data.fd = fd_data.fd;
115 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
116 ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d",
117 data.base, ionAllocData.len, data.fd);
118 return 0;
119 }
120
121
free_buffer(void * base,unsigned int size,unsigned int offset,int fd)122 int IonAlloc::free_buffer(void* base, unsigned int size, unsigned int offset,
123 int fd)
124 {
125 ATRACE_CALL();
126 Locker::Autolock _l(mLock);
127 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d",
128 base, size, fd);
129 int err = 0;
130 err = open_device();
131 if (err)
132 return err;
133
134 if(base)
135 err = unmap_buffer(base, size, offset);
136 close(fd);
137 return err;
138 }
139
map_buffer(void ** pBase,unsigned int size,unsigned int offset,int fd)140 int IonAlloc::map_buffer(void **pBase, unsigned int size, unsigned int offset,
141 int fd)
142 {
143 ATRACE_CALL();
144 int err = 0;
145 void *base = 0;
146 // It is a (quirky) requirement of ION to have opened the
147 // ion fd in the process that is doing the mapping
148 err = open_device();
149 if (err)
150 return err;
151
152 base = mmap(0, size, PROT_READ| PROT_WRITE,
153 MAP_SHARED, fd, 0);
154 *pBase = base;
155 if(base == MAP_FAILED) {
156 err = -errno;
157 ALOGE("ion: Failed to map memory in the client: %s",
158 strerror(errno));
159 } else {
160 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d",
161 base, size, offset, fd);
162 }
163 return err;
164 }
165
unmap_buffer(void * base,unsigned int size,unsigned int)166 int IonAlloc::unmap_buffer(void *base, unsigned int size,
167 unsigned int /*offset*/)
168 {
169 ATRACE_CALL();
170 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%u", base, size);
171 int err = 0;
172 if(munmap(base, size)) {
173 err = -errno;
174 ALOGE("ion: Failed to unmap memory at %p : %s",
175 base, strerror(errno));
176 }
177 return err;
178
179 }
clean_buffer(void * base,unsigned int size,unsigned int offset,int fd,int op)180 int IonAlloc::clean_buffer(void *base, unsigned int size, unsigned int offset,
181 int fd, int op)
182 {
183 ATRACE_CALL();
184 ATRACE_INT("operation id", op);
185 struct ion_flush_data flush_data;
186 struct ion_fd_data fd_data;
187 struct ion_handle_data handle_data;
188 int err = 0;
189
190 err = open_device();
191 if (err)
192 return err;
193
194 fd_data.fd = fd;
195 if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
196 err = -errno;
197 ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
198 __FUNCTION__, strerror(errno));
199 return err;
200 }
201
202 handle_data.handle = fd_data.handle;
203 flush_data.handle = fd_data.handle;
204 flush_data.vaddr = base;
205 // offset and length are unsigned int
206 flush_data.offset = offset;
207 flush_data.length = size;
208
209 struct ion_custom_data d;
210 switch(op) {
211 case CACHE_CLEAN:
212 d.cmd = ION_IOC_CLEAN_CACHES;
213 break;
214 case CACHE_INVALIDATE:
215 d.cmd = ION_IOC_INV_CACHES;
216 break;
217 case CACHE_CLEAN_AND_INVALIDATE:
218 default:
219 d.cmd = ION_IOC_CLEAN_INV_CACHES;
220 }
221
222 d.arg = (unsigned long int)&flush_data;
223
224 if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) {
225 err = -errno;
226 ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
227
228 __FUNCTION__, strerror(errno));
229 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
230 return err;
231 }
232 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
233 return 0;
234 }
235
236