blob: 6dcba157755a220ac3c71e113d98e92086b1afb3 [file] [log] [blame]
Vishal Bhoj78e90492015-12-07 01:36:32 +05301/*
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
3 *
4 * Copyright (C) 2008 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <errno.h>
20#include <pthread.h>
21
22#include <cutils/log.h>
23#include <cutils/atomic.h>
24#include <hardware/hardware.h>
25#include <hardware/gralloc.h>
26
27#include "gralloc_priv.h"
28#include "alloc_device.h"
29#include "framebuffer_device.h"
30
31#if GRALLOC_ARM_UMP_MODULE
32#include <ump/ump_ref_drv.h>
33static int s_ump_is_open = 0;
34#endif
35
36#if GRALLOC_ARM_DMA_BUF_MODULE
37#include <linux/ion.h>
38#include <ion/ion.h>
39#include <sys/mman.h>
40#endif
41
42static pthread_mutex_t s_map_lock = PTHREAD_MUTEX_INITIALIZER;
43
44static int gralloc_device_open(const hw_module_t *module, const char *name, hw_device_t **device)
45{
46 int status = -EINVAL;
47
48 if (!strncmp(name, GRALLOC_HARDWARE_GPU0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
49 {
50 status = alloc_device_open(module, name, device);
51 }
52 else if (!strncmp(name, GRALLOC_HARDWARE_FB0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
53 {
54 status = framebuffer_device_open(module, name, device);
55 }
56
57 return status;
58}
59
60static int gralloc_register_buffer(gralloc_module_t const *module, buffer_handle_t handle)
61{
62 MALI_IGNORE(module);
63
64 if (private_handle_t::validate(handle) < 0)
65 {
66 AERR("Registering invalid buffer 0x%p, returning error", handle);
67 return -EINVAL;
68 }
69
70 // if this handle was created in this process, then we keep it as is.
71 private_handle_t *hnd = (private_handle_t *)handle;
72
73 int retval = -EINVAL;
74
75 pthread_mutex_lock(&s_map_lock);
76
77#if GRALLOC_ARM_UMP_MODULE
78
79 if (!s_ump_is_open)
80 {
81 ump_result res = ump_open(); // MJOLL-4012: UMP implementation needs a ump_close() for each ump_open
82
83 if (res != UMP_OK)
84 {
85 pthread_mutex_unlock(&s_map_lock);
86 AERR("Failed to open UMP library with res=%d", res);
87 return retval;
88 }
89
90 s_ump_is_open = 1;
91 }
92
93#endif
94
95 hnd->pid = getpid();
96
97 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
98 {
99 AERR("Can't register buffer 0x%p as it is a framebuffer", handle);
100 }
101 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
102 {
103#if GRALLOC_ARM_UMP_MODULE
104 hnd->ump_mem_handle = (int)ump_handle_create_from_secure_id(hnd->ump_id);
105
106 if (UMP_INVALID_MEMORY_HANDLE != (ump_handle)hnd->ump_mem_handle)
107 {
108 hnd->base = ump_mapped_pointer_get((ump_handle)hnd->ump_mem_handle);
109
110 if (0 != hnd->base)
111 {
112 hnd->lockState = private_handle_t::LOCK_STATE_MAPPED;
113 hnd->writeOwner = 0;
114 hnd->lockState = 0;
115
116 pthread_mutex_unlock(&s_map_lock);
117 return 0;
118 }
119 else
120 {
121 AERR("Failed to map UMP handle 0x%x", hnd->ump_mem_handle);
122 }
123
124 ump_reference_release((ump_handle)hnd->ump_mem_handle);
125 }
126 else
127 {
128 AERR("Failed to create UMP handle 0x%x", hnd->ump_mem_handle);
129 }
130
131#else
132 AERR("Gralloc does not support UMP. Unable to register UMP memory for handle 0x%p", hnd);
133#endif
134 }
135 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
136 {
137#if GRALLOC_ARM_DMA_BUF_MODULE
138 int ret;
139 unsigned char *mappedAddress;
140 size_t size = hnd->size;
141 hw_module_t *pmodule = NULL;
142 private_module_t *m = NULL;
143
144 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
145 {
146 m = reinterpret_cast<private_module_t *>(pmodule);
147 }
148 else
149 {
150 AERR("Could not get gralloc module for handle: 0x%p", hnd);
151 retval = -errno;
152 goto cleanup;
153 }
154
155 /* the test condition is set to m->ion_client <= 0 here, because:
156 * 1) module structure are initialized to 0 if no initial value is applied
157 * 2) a second user process should get a ion fd greater than 0.
158 */
159 if (m->ion_client <= 0)
160 {
161 /* a second user process must obtain a client handle first via ion_open before it can obtain the shared ion buffer*/
162 m->ion_client = ion_open();
163
164 if (m->ion_client < 0)
165 {
166 AERR("Could not open ion device for handle: 0x%p", hnd);
167 retval = -errno;
168 goto cleanup;
169 }
170 }
171
172 mappedAddress = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, hnd->share_fd, 0);
173
174 if (MAP_FAILED == mappedAddress)
175 {
176 AERR("mmap( share_fd:%d ) failed with %s", hnd->share_fd, strerror(errno));
177 retval = -errno;
178 goto cleanup;
179 }
180
181 hnd->base = mappedAddress + hnd->offset;
182 pthread_mutex_unlock(&s_map_lock);
183 return 0;
184#endif
185 }
186 else
187 {
188 AERR("registering non-UMP buffer not supported. flags = %d", hnd->flags);
189 }
190
191cleanup:
192 pthread_mutex_unlock(&s_map_lock);
193 return retval;
194}
195
196static int gralloc_unregister_buffer(gralloc_module_t const *module, buffer_handle_t handle)
197{
198 MALI_IGNORE(module);
199
200 if (private_handle_t::validate(handle) < 0)
201 {
202 AERR("unregistering invalid buffer 0x%p, returning error", handle);
203 return -EINVAL;
204 }
205
206 private_handle_t *hnd = (private_handle_t *)handle;
207
208 AERR_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK, "[unregister] handle %p still locked (state=%08x)", hnd, hnd->lockState);
209
210 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
211 {
212 AERR("Can't unregister buffer 0x%p as it is a framebuffer", handle);
213 }
214 else if (hnd->pid == getpid()) // never unmap buffers that were not registered in this process
215 {
216 pthread_mutex_lock(&s_map_lock);
217
218 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
219 {
220#if GRALLOC_ARM_UMP_MODULE
221 ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
222 ump_reference_release((ump_handle)hnd->ump_mem_handle);
223 hnd->ump_mem_handle = (int)UMP_INVALID_MEMORY_HANDLE;
224#else
225 AERR("Can't unregister UMP buffer for handle 0x%p. Not supported", handle);
226#endif
227 }
228 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
229 {
230#if GRALLOC_ARM_DMA_BUF_MODULE
231 void *base = (void *)hnd->base;
232 size_t size = hnd->size;
233
234 if (munmap(base, size) < 0)
235 {
236 AERR("Could not munmap base:0x%p size:%lu '%s'", base, (unsigned long)size, strerror(errno));
237 }
238
239#else
240 AERR("Can't unregister DMA_BUF buffer for hnd %p. Not supported", hnd);
241#endif
242
243 }
244 else
245 {
246 AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags);
247 }
248
249 hnd->base = 0;
250 hnd->lockState = 0;
251 hnd->writeOwner = 0;
252
253 pthread_mutex_unlock(&s_map_lock);
254 }
255 else
256 {
257 AERR("Trying to unregister buffer 0x%p from process %d that was not created in current process: %d", hnd, hnd->pid, getpid());
258 }
259
260 return 0;
261}
262
263static int gralloc_lock(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void **vaddr)
264{
265 if (private_handle_t::validate(handle) < 0)
266 {
267 AERR("Locking invalid buffer 0x%p, returning error", handle);
268 return -EINVAL;
269 }
270
271 private_handle_t *hnd = (private_handle_t *)handle;
272
273 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP || hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
274 {
275 hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK;
276 }
277
278 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
279 {
280 *vaddr = (void *)hnd->base;
281 }
282
283 MALI_IGNORE(module);
284 MALI_IGNORE(l);
285 MALI_IGNORE(t);
286 MALI_IGNORE(w);
287 MALI_IGNORE(h);
288 return 0;
289}
290
291static int gralloc_unlock(gralloc_module_t const *module, buffer_handle_t handle)
292{
293 MALI_IGNORE(module);
294
295 if (private_handle_t::validate(handle) < 0)
296 {
297 AERR("Unlocking invalid buffer 0x%p, returning error", handle);
298 return -EINVAL;
299 }
300
301 private_handle_t *hnd = (private_handle_t *)handle;
302 int32_t current_value;
303 int32_t new_value;
304 int retry;
305
306 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP && hnd->writeOwner)
307 {
308#if GRALLOC_ARM_UMP_MODULE
309 ump_cpu_msync_now((ump_handle)hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, (void *)hnd->base, hnd->size);
310#else
311 AERR("Buffer 0x%p is UMP type but it is not supported", hnd);
312#endif
313 }
314 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION && hnd->writeOwner)
315 {
316#if GRALLOC_ARM_DMA_BUF_MODULE
317 hw_module_t *pmodule = NULL;
318 private_module_t *m = NULL;
319
320 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
321 {
322 m = reinterpret_cast<private_module_t *>(pmodule);
323 //ion_sync_fd(m->ion_client, hnd->share_fd);
324 }
325 else
326 {
327 AERR("Couldnot get gralloc module for handle 0x%p\n", handle);
328 }
329
330#endif
331 }
332
333 return 0;
334}
335
336// There is one global instance of the module
337
338static struct hw_module_methods_t gralloc_module_methods =
339{
340open:
341 gralloc_device_open
342};
343
344private_module_t::private_module_t()
345{
346#define INIT_ZERO(obj) (memset(&(obj),0,sizeof((obj))))
347
348 base.common.tag = HARDWARE_MODULE_TAG;
349 base.common.version_major = 1;
350 base.common.version_minor = 0;
351 base.common.id = GRALLOC_HARDWARE_MODULE_ID;
352 base.common.name = "Graphics Memory Allocator Module";
353 base.common.author = "ARM Ltd.";
354 base.common.methods = &gralloc_module_methods;
355 base.common.dso = NULL;
356 INIT_ZERO(base.common.reserved);
357
358 base.registerBuffer = gralloc_register_buffer;
359 base.unregisterBuffer = gralloc_unregister_buffer;
360 base.lock = gralloc_lock;
361 base.unlock = gralloc_unlock;
362 base.perform = NULL;
363 INIT_ZERO(base.reserved_proc);
364
365 framebuffer = NULL;
366 flags = 0;
367 numBuffers = 0;
368 bufferMask = 0;
369 pthread_mutex_init(&(lock), NULL);
370 currentBuffer = NULL;
371 INIT_ZERO(info);
372 INIT_ZERO(finfo);
373 xdpi = 0.0f;
374 ydpi = 0.0f;
375 fps = 0.0f;
376
377#undef INIT_ZERO
378};
379
380/*
381 * HAL_MODULE_INFO_SYM will be initialized using the default constructor
382 * implemented above
383 */
384struct private_module_t HAL_MODULE_INFO_SYM;
385