blob: e657610f61ad29ebc2ed5016c2c9d6e5ea17d0e2 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
Arun Kumar K.R6c85f052014-01-21 21:47:41 -08002 * Copyright (c) 2011-2014, 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
Arun Kumar K.Ra6f91872014-03-20 16:05:28 -070031#include <sys/ioctl.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032#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;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070072 void *base = 0;
73
74 ionAllocData.len = data.size;
75 ionAllocData.align = data.align;
Naseer Ahmed68ee7c02014-04-11 19:59:39 -040076#ifdef QCOM_BSP
Amara Venkata Mastan Manoj Kumarc180c4e2012-10-09 10:13:55 -070077 ionAllocData.heap_mask = data.flags & ~ION_SECURE;
Naseer Ahmed68ee7c02014-04-11 19:59:39 -040078#else
79 ionAllocData.heap_id_mask = data.flags & ~ION_SECURE;
80#endif
Saurabh Shahd95c4082012-09-14 14:18:21 -070081 ionAllocData.flags = data.uncached ? 0 : ION_FLAG_CACHED;
Amara Venkata Mastan Manoj Kumarc180c4e2012-10-09 10:13:55 -070082 // ToDo: replace usage of alloc data structure with
83 // ionallocdata structure.
84 if (data.flags & ION_SECURE)
85 ionAllocData.flags |= ION_SECURE;
86
Iliyan Malchev202a77d2012-06-11 14:41:12 -070087 err = open_device();
88 if (err)
89 return err;
Saurabh Shahd95c4082012-09-14 14:18:21 -070090 if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070091 err = -errno;
92 ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070093 return err;
94 }
95
96 fd_data.handle = ionAllocData.handle;
97 handle_data.handle = ionAllocData.handle;
Saurabh Shahd95c4082012-09-14 14:18:21 -070098 if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -070099 err = -errno;
100 ALOGE("%s: ION_IOC_MAP failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700101 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700102 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700103 return err;
104 }
105
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700106 if(!(data.flags & ION_SECURE)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700107 base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700108 MAP_SHARED, fd_data.fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700109 if(base == MAP_FAILED) {
110 err = -errno;
111 ALOGE("%s: Failed to map the allocated memory: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700112 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700113 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700114 return err;
115 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700116 }
117
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700118 data.base = base;
119 data.fd = fd_data.fd;
120 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800121 ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700122 data.base, ionAllocData.len, data.fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700123 return 0;
124}
125
126
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800127int IonAlloc::free_buffer(void* base, size_t size, size_t offset, int fd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700128{
Naseer Ahmed29a26812012-06-14 00:56:20 -0700129 Locker::Autolock _l(mLock);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800130 ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%zu fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131 base, size, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700132 int err = 0;
133 err = open_device();
134 if (err)
135 return err;
136
137 if(base)
138 err = unmap_buffer(base, size, offset);
139 close(fd);
140 return err;
141}
142
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800143int IonAlloc::map_buffer(void **pBase, size_t size, size_t offset, int fd)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700144{
145 int err = 0;
146 void *base = 0;
147 // It is a (quirky) requirement of ION to have opened the
148 // ion fd in the process that is doing the mapping
149 err = open_device();
150 if (err)
151 return err;
152
153 base = mmap(0, size, PROT_READ| PROT_WRITE,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700154 MAP_SHARED, fd, 0);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700155 *pBase = base;
156 if(base == MAP_FAILED) {
157 err = -errno;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700158 ALOGE("ion: Failed to map memory in the client: %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700159 strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700160 } else {
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800161 ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%zu offset:%d fd:%d",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700162 base, size, offset, fd);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700163 }
164 return err;
165}
166
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800167int IonAlloc::unmap_buffer(void *base, size_t size, size_t /*offset*/)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700168{
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800169 ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%zu", base, size);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700170 int err = 0;
171 if(munmap(base, size)) {
172 err = -errno;
173 ALOGE("ion: Failed to unmap memory at %p : %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700174 base, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700175 }
176 return err;
177
178}
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800179int IonAlloc::clean_buffer(void *base, size_t size, size_t offset, int fd, int op)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700180{
181 struct ion_flush_data flush_data;
182 struct ion_fd_data fd_data;
183 struct ion_handle_data handle_data;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700184 int err = 0;
185
186 err = open_device();
187 if (err)
188 return err;
189
190 fd_data.fd = fd;
191 if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
192 err = -errno;
193 ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
Naseer Ahmed29a26812012-06-14 00:56:20 -0700194 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700195 return err;
196 }
197
198 handle_data.handle = fd_data.handle;
199 flush_data.handle = fd_data.handle;
200 flush_data.vaddr = base;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800201 // offset and length are uint32_t
202 flush_data.offset = (uint32_t) offset;
203 flush_data.length = (uint32_t) size;
Saurabh Shahece296e2012-09-11 13:33:44 -0700204
205 struct ion_custom_data d;
Naseer Ahmedaeab91f2013-03-20 21:01:19 -0400206 switch(op) {
207 case CACHE_CLEAN:
208 d.cmd = ION_IOC_CLEAN_CACHES;
209 break;
210 case CACHE_INVALIDATE:
211 d.cmd = ION_IOC_INV_CACHES;
212 break;
213 case CACHE_CLEAN_AND_INVALIDATE:
214 default:
215 d.cmd = ION_IOC_CLEAN_INV_CACHES;
216 }
217
Saurabh Shahece296e2012-09-11 13:33:44 -0700218 d.arg = (unsigned long int)&flush_data;
219
220 if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700221 err = -errno;
222 ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
Saurabh Shahece296e2012-09-11 13:33:44 -0700223
Naseer Ahmed29a26812012-06-14 00:56:20 -0700224 __FUNCTION__, strerror(errno));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700225 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
226 return err;
227 }
228 ioctl(mIonFd, ION_IOC_FREE, &handle_data);
229 return 0;
230}
231