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