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 #include <sys/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 struct ion_handle_data handle_data;
70 struct ion_fd_data fd_data;
71 struct ion_allocation_data ionAllocData;
72 void *base = 0;
73
74 ionAllocData.len = data.size;
75 ionAllocData.align = data.align;
76 ionAllocData.heap_mask = data.flags & ~ION_SECURE;
77 ionAllocData.flags = data.uncached ? 0 : ION_FLAG_CACHED;
78 // ToDo: replace usage of alloc data structure with
79 // ionallocdata structure.
80 if (data.flags & ION_SECURE)
81 ionAllocData.flags |= ION_SECURE;
82
83 err = open_device();
84 if (err)
85 return err;
86 if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) {
87 err = -errno;
88 ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
89 return err;
90 }
91
92 fd_data.handle = ionAllocData.handle;
93 handle_data.handle = ionAllocData.handle;
94 if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) {
95 err = -errno;
96 ALOGE("%s: ION_IOC_MAP failed with error - %s",
97 __FUNCTION__, strerror(errno));
98 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
99 return err;
100 }
101
102 if(!(data.flags & ION_SECURE)) {
103 base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
104 MAP_SHARED, fd_data.fd, 0);
105 if(base == MAP_FAILED) {
106 err = -errno;
107 ALOGE("%s: Failed to map the allocated memory: %s",
108 __FUNCTION__, strerror(errno));
109 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
110 return err;
111 }
112 }
113
114 data.base = base;
115 data.fd = fd_data.fd;
116 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
117 ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d",
118 data.base, ionAllocData.len, data.fd);
119 return 0;
120 }
121
122
free_buffer(void * base,size_t size,size_t offset,int fd)123 int IonAlloc::free_buffer(void* base, size_t size, size_t offset, int fd)
124 {
125 Locker::Autolock _l(mLock);
126 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%zu fd:%d",
127 base, size, fd);
128 int err = 0;
129 err = open_device();
130 if (err)
131 return err;
132
133 if(base)
134 err = unmap_buffer(base, size, offset);
135 close(fd);
136 return err;
137 }
138
map_buffer(void ** pBase,size_t size,size_t offset,int fd)139 int IonAlloc::map_buffer(void **pBase, size_t size, size_t offset, int fd)
140 {
141 int err = 0;
142 void *base = 0;
143 // It is a (quirky) requirement of ION to have opened the
144 // ion fd in the process that is doing the mapping
145 err = open_device();
146 if (err)
147 return err;
148
149 base = mmap(0, size, PROT_READ| PROT_WRITE,
150 MAP_SHARED, fd, 0);
151 *pBase = base;
152 if(base == MAP_FAILED) {
153 err = -errno;
154 ALOGE("ion: Failed to map memory in the client: %s",
155 strerror(errno));
156 } else {
157 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%zu offset:%d fd:%d",
158 base, size, offset, fd);
159 }
160 return err;
161 }
162
unmap_buffer(void * base,size_t size,size_t)163 int IonAlloc::unmap_buffer(void *base, size_t size, size_t /*offset*/)
164 {
165 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%zu", base, size);
166 int err = 0;
167 if(munmap(base, size)) {
168 err = -errno;
169 ALOGE("ion: Failed to unmap memory at %p : %s",
170 base, strerror(errno));
171 }
172 return err;
173
174 }
clean_buffer(void * base,size_t size,size_t offset,int fd,int op)175 int IonAlloc::clean_buffer(void *base, size_t size, size_t offset, int fd, int op)
176 {
177 struct ion_flush_data flush_data;
178 struct ion_fd_data fd_data;
179 struct ion_handle_data handle_data;
180 int err = 0;
181
182 err = open_device();
183 if (err)
184 return err;
185
186 fd_data.fd = fd;
187 if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
188 err = -errno;
189 ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
190 __FUNCTION__, strerror(errno));
191 return err;
192 }
193
194 handle_data.handle = fd_data.handle;
195 flush_data.handle = fd_data.handle;
196 flush_data.vaddr = base;
197 // offset and length are uint32_t
198 flush_data.offset = (uint32_t) offset;
199 flush_data.length = (uint32_t) size;
200
201 struct ion_custom_data d;
202 switch(op) {
203 case CACHE_CLEAN:
204 d.cmd = ION_IOC_CLEAN_CACHES;
205 break;
206 case CACHE_INVALIDATE:
207 d.cmd = ION_IOC_INV_CACHES;
208 break;
209 case CACHE_CLEAN_AND_INVALIDATE:
210 default:
211 d.cmd = ION_IOC_CLEAN_INV_CACHES;
212 }
213
214 d.arg = (unsigned long int)&flush_data;
215
216 if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) {
217 err = -errno;
218 ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
219
220 __FUNCTION__, strerror(errno));
221 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
222 return err;
223 }
224 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
225 return 0;
226 }
227
228