blob: b9b971780a684dfede5ea6bc11d7b85a53ae9863 [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;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070069 struct ion_handle_data handle_data;
70 struct ion_fd_data fd_data;
71 struct ion_allocation_data ionAllocData;
72
73 void *base = 0;
74
75 ionAllocData.len = data.size;
76 ionAllocData.align = data.align;
Saurabh Shahd95c4082012-09-14 14:18:21 -070077 ionAllocData.heap_mask = data.flags;
78 ionAllocData.flags = data.uncached ? 0 : ION_FLAG_CACHED;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079
80 err = open_device();
81 if (err)
82 return err;
83
Saurabh Shahd95c4082012-09-14 14:18:21 -070084 if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070085 err = -errno;
86 ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070087 return err;
88 }
89
90 fd_data.handle = ionAllocData.handle;
91 handle_data.handle = ionAllocData.handle;
Saurabh Shahd95c4082012-09-14 14:18:21 -070092 if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070093 err = -errno;
94 ALOGE("%s: ION_IOC_MAP failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -070095 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070096 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070097 return err;
98 }
99
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700100 if(!(data.flags & ION_SECURE)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700101 base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700102 MAP_SHARED, fd_data.fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700103 if(base == MAP_FAILED) {
104 err = -errno;
105 ALOGE("%s: Failed to map the allocated memory: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700106 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700107 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700108 return err;
109 }
110 memset(base, 0, ionAllocData.len);
111 // Clean cache after memset
112 clean_buffer(base, data.size, data.offset, fd_data.fd);
113 }
114
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700115 data.base = base;
116 data.fd = fd_data.fd;
117 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700118 ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700119 data.base, ionAllocData.len, data.fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700120 return 0;
121}
122
123
124int IonAlloc::free_buffer(void* base, size_t size, int offset, int fd)
125{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700126 Locker::Autolock _l(mLock);
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700127 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700128 base, size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700129 int err = 0;
130 err = open_device();
131 if (err)
132 return err;
133
134 if(base)
135 err = unmap_buffer(base, size, offset);
136 close(fd);
137 return err;
138}
139
140int IonAlloc::map_buffer(void **pBase, size_t size, int offset, int fd)
141{
142 int err = 0;
143 void *base = 0;
144 // It is a (quirky) requirement of ION to have opened the
145 // ion fd in the process that is doing the mapping
146 err = open_device();
147 if (err)
148 return err;
149
150 base = mmap(0, size, PROT_READ| PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700151 MAP_SHARED, fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700152 *pBase = base;
153 if(base == MAP_FAILED) {
154 err = -errno;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700155 ALOGE("ion: Failed to map memory in the client: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700156 strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700157 } else {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700158 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%d offset:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700159 base, size, offset, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700160 }
161 return err;
162}
163
164int IonAlloc::unmap_buffer(void *base, size_t size, int offset)
165{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700166 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%d", base, size);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700167 int err = 0;
168 if(munmap(base, size)) {
169 err = -errno;
170 ALOGE("ion: Failed to unmap memory at %p : %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700171 base, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700172 }
173 return err;
174
175}
176int IonAlloc::clean_buffer(void *base, size_t size, int offset, int fd)
177{
178 struct ion_flush_data flush_data;
179 struct ion_fd_data fd_data;
180 struct ion_handle_data handle_data;
181 struct ion_handle* handle;
182 int err = 0;
183
184 err = open_device();
185 if (err)
186 return err;
187
188 fd_data.fd = fd;
189 if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
190 err = -errno;
191 ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700192 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700193 return err;
194 }
195
196 handle_data.handle = fd_data.handle;
197 flush_data.handle = fd_data.handle;
198 flush_data.vaddr = base;
199 flush_data.offset = offset;
200 flush_data.length = size;
Saurabh Shahece296e2012-09-11 13:33:44 -0700201
202 struct ion_custom_data d;
203 d.cmd = ION_IOC_CLEAN_INV_CACHES;
204 d.arg = (unsigned long int)&flush_data;
205
206 if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700207 err = -errno;
208 ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
Saurabh Shahece296e2012-09-11 13:33:44 -0700209
Naseer Ahmed29a26812012-06-14 00:56:20 -0700210 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700211 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
212 return err;
213 }
214 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
215 return 0;
216}
217