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