blob: 83e62e740170c8586f661820471bdbcae08222ac [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Duy Truong73d36df2013-02-09 20:33:23 -08002 * Copyright (c) 2011-2012, The Linux Foundation. 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.
Duy Truong73d36df2013-02-09 20:33:23 -080013 * * Neither the name of The Linux Foundation nor the names of its
Iliyan Malchev202a77d2012-06-11 14:41:12 -070014 * 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"
Naseer Ahmed74214722013-02-09 08:11:36 -050043#ifdef QCOM_BSP
44#define NEW_ION_API
45#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -070046
47int IonAlloc::open_device()
48{
49 if(mIonFd == FD_INIT)
50 mIonFd = open(ION_DEVICE, O_RDONLY);
51
52 if(mIonFd < 0 ) {
53 ALOGE("%s: Failed to open ion device - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -070054 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070055 mIonFd = FD_INIT;
56 return -errno;
57 }
58 return 0;
59}
60
61void IonAlloc::close_device()
62{
63 if(mIonFd >= 0)
64 close(mIonFd);
65 mIonFd = FD_INIT;
66}
67
68int IonAlloc::alloc_buffer(alloc_data& data)
69{
Naseer Ahmed29a26812012-06-14 00:56:20 -070070 Locker::Autolock _l(mLock);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071 int err = 0;
Naseer Ahmed74214722013-02-09 08:11:36 -050072#ifndef NEW_ION_API
73 int ionSyncFd = FD_INIT;
74 int iFd = FD_INIT;
75#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076 struct ion_handle_data handle_data;
77 struct ion_fd_data fd_data;
78 struct ion_allocation_data ionAllocData;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070079 void *base = 0;
80
81 ionAllocData.len = data.size;
82 ionAllocData.align = data.align;
Naseer Ahmed74214722013-02-09 08:11:36 -050083#ifndef NEW_ION_API
84 ionAllocData.flags = data.flags;
85#else
Amara Venkata Mastan Manoj Kumarc180c4e2012-10-09 10:13:55 -070086 ionAllocData.heap_mask = data.flags & ~ION_SECURE;
Saurabh Shahd95c4082012-09-14 14:18:21 -070087 ionAllocData.flags = data.uncached ? 0 : ION_FLAG_CACHED;
Amara Venkata Mastan Manoj Kumarc180c4e2012-10-09 10:13:55 -070088 // ToDo: replace usage of alloc data structure with
89 // ionallocdata structure.
90 if (data.flags & ION_SECURE)
91 ionAllocData.flags |= ION_SECURE;
Naseer Ahmedeb2e1052013-03-05 21:17:29 -050092#endif
Amara Venkata Mastan Manoj Kumarc180c4e2012-10-09 10:13:55 -070093
Iliyan Malchev202a77d2012-06-11 14:41:12 -070094 err = open_device();
95 if (err)
96 return err;
Naseer Ahmed74214722013-02-09 08:11:36 -050097#ifndef NEW_ION_API
98 if(data.uncached) {
99 // Use the sync FD to alloc and map 93
100 // when we need uncached memory 94
101 ionSyncFd = open(ION_DEVICE, O_RDONLY|O_DSYNC);
102 if(ionSyncFd < 0) {
103 ALOGE("%s: Failed to open ion device - %s",
104 __FUNCTION__, strerror(errno));
105 return -errno;
106 }
107 iFd = ionSyncFd;
108 } else {
109 iFd = mIonFd;
110 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700111
Naseer Ahmed74214722013-02-09 08:11:36 -0500112 if(ioctl(iFd, ION_IOC_ALLOC, &ionAllocData)) {
113#else
Saurabh Shahd95c4082012-09-14 14:18:21 -0700114 if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500115#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700116 err = -errno;
117 ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
Naseer Ahmed74214722013-02-09 08:11:36 -0500118#ifndef NEW_ION_API
119 if(ionSyncFd >= 0)
120 close(ionSyncFd);
121 ionSyncFd = FD_INIT;
122#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700123 return err;
124 }
125
126 fd_data.handle = ionAllocData.handle;
127 handle_data.handle = ionAllocData.handle;
Saurabh Shahd95c4082012-09-14 14:18:21 -0700128 if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700129 err = -errno;
130 ALOGE("%s: ION_IOC_MAP failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700132 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700133 return err;
134 }
135
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700136 if(!(data.flags & ION_SECURE)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700137 base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138 MAP_SHARED, fd_data.fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700139 if(base == MAP_FAILED) {
140 err = -errno;
141 ALOGE("%s: Failed to map the allocated memory: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700142 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700143 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700144 return err;
145 }
146 memset(base, 0, ionAllocData.len);
147 // Clean cache after memset
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400148 clean_buffer(base, data.size, data.offset, fd_data.fd,
149 CACHE_CLEAN_AND_INVALIDATE);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700150 }
151
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700152 data.base = base;
153 data.fd = fd_data.fd;
154 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700155 ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700156 data.base, ionAllocData.len, data.fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700157 return 0;
158}
159
160
161int IonAlloc::free_buffer(void* base, size_t size, int offset, int fd)
162{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700163 Locker::Autolock _l(mLock);
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700164 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700165 base, size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700166 int err = 0;
167 err = open_device();
168 if (err)
169 return err;
170
171 if(base)
172 err = unmap_buffer(base, size, offset);
173 close(fd);
174 return err;
175}
176
177int IonAlloc::map_buffer(void **pBase, size_t size, int offset, int fd)
178{
179 int err = 0;
180 void *base = 0;
181 // It is a (quirky) requirement of ION to have opened the
182 // ion fd in the process that is doing the mapping
183 err = open_device();
184 if (err)
185 return err;
186
187 base = mmap(0, size, PROT_READ| PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700188 MAP_SHARED, fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700189 *pBase = base;
190 if(base == MAP_FAILED) {
191 err = -errno;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700192 ALOGE("ion: Failed to map memory in the client: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700193 strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700194 } else {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700195 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%d offset:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700196 base, size, offset, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700197 }
198 return err;
199}
200
201int IonAlloc::unmap_buffer(void *base, size_t size, int offset)
202{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700203 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%d", base, size);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 int err = 0;
205 if(munmap(base, size)) {
206 err = -errno;
207 ALOGE("ion: Failed to unmap memory at %p : %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700208 base, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700209 }
210 return err;
211
212}
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400213int IonAlloc::clean_buffer(void *base, size_t size, int offset, int fd, int op)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700214{
215 struct ion_flush_data flush_data;
216 struct ion_fd_data fd_data;
217 struct ion_handle_data handle_data;
218 struct ion_handle* handle;
219 int err = 0;
220
221 err = open_device();
222 if (err)
223 return err;
224
225 fd_data.fd = fd;
226 if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
227 err = -errno;
228 ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700229 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700230 return err;
231 }
232
233 handle_data.handle = fd_data.handle;
234 flush_data.handle = fd_data.handle;
235 flush_data.vaddr = base;
236 flush_data.offset = offset;
237 flush_data.length = size;
Saurabh Shahece296e2012-09-11 13:33:44 -0700238
Naseer Ahmed74214722013-02-09 08:11:36 -0500239#ifdef NEW_ION_API
Saurabh Shahece296e2012-09-11 13:33:44 -0700240 struct ion_custom_data d;
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400241 switch(op) {
242 case CACHE_CLEAN:
243 d.cmd = ION_IOC_CLEAN_CACHES;
244 break;
245 case CACHE_INVALIDATE:
246 d.cmd = ION_IOC_INV_CACHES;
247 break;
248 case CACHE_CLEAN_AND_INVALIDATE:
249 default:
250 d.cmd = ION_IOC_CLEAN_INV_CACHES;
251 }
252
Saurabh Shahece296e2012-09-11 13:33:44 -0700253 d.arg = (unsigned long int)&flush_data;
254
255 if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500256#else
257 if(ioctl(mIonFd, ION_IOC_CLEAN_INV_CACHES, &flush_data)) {
258#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700259 err = -errno;
260 ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
Saurabh Shahece296e2012-09-11 13:33:44 -0700261
Naseer Ahmed29a26812012-06-14 00:56:20 -0700262 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700263 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
264 return err;
265 }
266 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
267 return 0;
268}
269