blob: 8480f98bdc8c484afacf7a2157ce7b47b5d12ed0 [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
148 clean_buffer(base, data.size, data.offset, fd_data.fd);
149 }
150
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700151 data.base = base;
152 data.fd = fd_data.fd;
153 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700154 ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700155 data.base, ionAllocData.len, data.fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700156 return 0;
157}
158
159
160int IonAlloc::free_buffer(void* base, size_t size, int offset, int fd)
161{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700162 Locker::Autolock _l(mLock);
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700163 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700164 base, size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700165 int err = 0;
166 err = open_device();
167 if (err)
168 return err;
169
170 if(base)
171 err = unmap_buffer(base, size, offset);
172 close(fd);
173 return err;
174}
175
176int IonAlloc::map_buffer(void **pBase, size_t size, int offset, int fd)
177{
178 int err = 0;
179 void *base = 0;
180 // It is a (quirky) requirement of ION to have opened the
181 // ion fd in the process that is doing the mapping
182 err = open_device();
183 if (err)
184 return err;
185
186 base = mmap(0, size, PROT_READ| PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700187 MAP_SHARED, fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700188 *pBase = base;
189 if(base == MAP_FAILED) {
190 err = -errno;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700191 ALOGE("ion: Failed to map memory in the client: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700192 strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700193 } else {
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700194 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%d offset:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700195 base, size, offset, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700196 }
197 return err;
198}
199
200int IonAlloc::unmap_buffer(void *base, size_t size, int offset)
201{
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700202 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%d", base, size);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700203 int err = 0;
204 if(munmap(base, size)) {
205 err = -errno;
206 ALOGE("ion: Failed to unmap memory at %p : %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700207 base, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700208 }
209 return err;
210
211}
212int IonAlloc::clean_buffer(void *base, size_t size, int offset, int fd)
213{
214 struct ion_flush_data flush_data;
215 struct ion_fd_data fd_data;
216 struct ion_handle_data handle_data;
217 struct ion_handle* handle;
218 int err = 0;
219
220 err = open_device();
221 if (err)
222 return err;
223
224 fd_data.fd = fd;
225 if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
226 err = -errno;
227 ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700228 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700229 return err;
230 }
231
232 handle_data.handle = fd_data.handle;
233 flush_data.handle = fd_data.handle;
234 flush_data.vaddr = base;
235 flush_data.offset = offset;
236 flush_data.length = size;
Saurabh Shahece296e2012-09-11 13:33:44 -0700237
Naseer Ahmed74214722013-02-09 08:11:36 -0500238#ifdef NEW_ION_API
Saurabh Shahece296e2012-09-11 13:33:44 -0700239 struct ion_custom_data d;
240 d.cmd = ION_IOC_CLEAN_INV_CACHES;
241 d.arg = (unsigned long int)&flush_data;
242
243 if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) {
Naseer Ahmed74214722013-02-09 08:11:36 -0500244#else
245 if(ioctl(mIonFd, ION_IOC_CLEAN_INV_CACHES, &flush_data)) {
246#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700247 err = -errno;
248 ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
Saurabh Shahece296e2012-09-11 13:33:44 -0700249
Naseer Ahmed29a26812012-06-14 00:56:20 -0700250 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700251 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
252 return err;
253 }
254 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
255 return 0;
256}
257