blob: 8af5a96baa057f0d1e853edd56182d3e94ab100d [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
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070030#define DEBUG 0
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031#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 Ahmed01d3fd32012-07-14 21:08:13 -070088 ionSyncFd = open(ION_DEVICE, O_RDONLY|O_DSYNC);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070089 if(ionSyncFd < 0) {
90 ALOGE("%s: Failed to open ion device - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -070091 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070092 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",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700114 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
115 if(ionSyncFd >= 0)
116 close(ionSyncFd);
117 ionSyncFd = FD_INIT;
118 return err;
119 }
120
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700121 if(!(data.flags & ION_SECURE)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700122 base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700123 MAP_SHARED, fd_data.fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700124 if(base == MAP_FAILED) {
125 err = -errno;
126 ALOGE("%s: Failed to map the allocated memory: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700128 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);
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700145 ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700146 data.base, ionAllocData.len, data.fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700147 return 0;
148}
149
150
151int IonAlloc::free_buffer(void* base, size_t size, int offset, int fd)
152{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700153 Locker::Autolock _l(mLock);
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700154 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700155 base, size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700156 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
167int 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,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700178 MAP_SHARED, fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700179 *pBase = base;
180 if(base == MAP_FAILED) {
181 err = -errno;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700182 ALOGE("ion: Failed to map memory in the client: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700183 strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700184 } else {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700185 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%d offset:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700186 base, size, offset, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700187 }
188 return err;
189}
190
191int IonAlloc::unmap_buffer(void *base, size_t size, int offset)
192{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700193 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%d", base, size);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700194 int err = 0;
195 if(munmap(base, size)) {
196 err = -errno;
197 ALOGE("ion: Failed to unmap memory at %p : %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700198 base, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700199 }
200 return err;
201
202}
203int 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",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700219 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700220 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",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700231 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700232 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
233 return err;
234 }
235 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
236 return 0;
237}
238