blob: 7c8fbf40ba3ef474ef87f89fcc952be4bb8ab7a6 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Naseer Ahmed29a26812012-06-14 00:56:20 -07002 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07003
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",
Naseer Ahmed29a26812012-06-14 00:56:20 -070051 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070052 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{
Naseer Ahmed29a26812012-06-14 00:56:20 -070067 Locker::Autolock _l(mLock);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070068 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
Naseer Ahmed29a26812012-06-14 00:56:20 -070088 // XXX: Change O_SYNC to O_DSYNC when available in bionic
89 ionSyncFd = open(ION_DEVICE, O_RDONLY|O_SYNC);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070090 if(ionSyncFd < 0) {
91 ALOGE("%s: Failed to open ion device - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -070092 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070093 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",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700114 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700115 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
116 if(ionSyncFd >= 0)
117 close(ionSyncFd);
118 ionSyncFd = FD_INIT;
119 return err;
120 }
121
Naseer Ahmed29a26812012-06-14 00:56:20 -0700122 if(!(data.flags & ION_SECURE) &&
123 !(data.allocType & private_handle_t::PRIV_FLAGS_NOT_MAPPED)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700124
125 base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700126 MAP_SHARED, fd_data.fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700127 if(base == MAP_FAILED) {
128 err = -errno;
129 ALOGE("%s: Failed to map the allocated memory: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700130 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700131 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",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700149 data.base, ionAllocData.len, data.fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700150 return 0;
151}
152
153
154int IonAlloc::free_buffer(void* base, size_t size, int offset, int fd)
155{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700156 Locker::Autolock _l(mLock);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700157 ALOGD("ion: Freeing buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700158 base, size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700159 int err = 0;
160 err = open_device();
161 if (err)
162 return err;
163
164 if(base)
165 err = unmap_buffer(base, size, offset);
166 close(fd);
167 return err;
168}
169
170int IonAlloc::map_buffer(void **pBase, size_t size, int offset, int fd)
171{
172 int err = 0;
173 void *base = 0;
174 // It is a (quirky) requirement of ION to have opened the
175 // ion fd in the process that is doing the mapping
176 err = open_device();
177 if (err)
178 return err;
179
180 base = mmap(0, size, PROT_READ| PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700181 MAP_SHARED, fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700182 *pBase = base;
183 if(base == MAP_FAILED) {
184 err = -errno;
185 ALOGD("ion: Failed to map memory in the client: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700186 strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700187 } else {
188 ALOGD("ion: Mapped buffer base:%p size:%d offset:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700189 base, size, offset, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700190 }
191 return err;
192}
193
194int IonAlloc::unmap_buffer(void *base, size_t size, int offset)
195{
196 ALOGD("ion: Unmapping buffer base:%p size:%d", base, size);
197 int err = 0;
198 if(munmap(base, size)) {
199 err = -errno;
200 ALOGE("ion: Failed to unmap memory at %p : %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700201 base, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700202 }
203 return err;
204
205}
206int IonAlloc::clean_buffer(void *base, size_t size, int offset, int fd)
207{
208 struct ion_flush_data flush_data;
209 struct ion_fd_data fd_data;
210 struct ion_handle_data handle_data;
211 struct ion_handle* handle;
212 int err = 0;
213
214 err = open_device();
215 if (err)
216 return err;
217
218 fd_data.fd = fd;
219 if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
220 err = -errno;
221 ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700222 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700223 return err;
224 }
225
226 handle_data.handle = fd_data.handle;
227 flush_data.handle = fd_data.handle;
228 flush_data.vaddr = base;
229 flush_data.offset = offset;
230 flush_data.length = size;
231 if(ioctl(mIonFd, ION_IOC_CLEAN_INV_CACHES, &flush_data)) {
232 err = -errno;
233 ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700234 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700235 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
236 return err;
237 }
238 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
239 return 0;
240}
241