blob: 70d2c69b19346721cd0eac2c25148f5cde8fb6d4 [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 {
Chia-I Wufba69b52017-05-08 13:10:08 -070099 AINF("Register framebuffer 0x%p is no-op", handle);
100 retval = 0;
101
Vishal Bhoj78e90492015-12-07 01:36:32 +0530102 }
103 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
104 {
105#if GRALLOC_ARM_UMP_MODULE
106 hnd->ump_mem_handle = (int)ump_handle_create_from_secure_id(hnd->ump_id);
107
108 if (UMP_INVALID_MEMORY_HANDLE != (ump_handle)hnd->ump_mem_handle)
109 {
110 hnd->base = ump_mapped_pointer_get((ump_handle)hnd->ump_mem_handle);
111
112 if (0 != hnd->base)
113 {
Vishal Bhoj78e90492015-12-07 01:36:32 +0530114 hnd->writeOwner = 0;
John Stultz37287452016-01-20 20:08:46 -0800115 hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530116 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;
John Stultz37287452016-01-20 20:08:46 -0800182 hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530183 pthread_mutex_unlock(&s_map_lock);
184 return 0;
185#endif
186 }
187 else
188 {
189 AERR("registering non-UMP buffer not supported. flags = %d", hnd->flags);
190 }
191
192cleanup:
193 pthread_mutex_unlock(&s_map_lock);
194 return retval;
195}
196
John Stultz37287452016-01-20 20:08:46 -0800197static void unmap_buffer(private_handle_t *hnd)
198{
199 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
200 {
201#if GRALLOC_ARM_UMP_MODULE
202 ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
203 ump_reference_release((ump_handle)hnd->ump_mem_handle);
204 hnd->ump_mem_handle = (int)UMP_INVALID_MEMORY_HANDLE;
205#else
206 AERR("Can't unregister UMP buffer for handle 0x%p. Not supported", hnd);
207#endif
208 }
209 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
210 {
211#if GRALLOC_ARM_DMA_BUF_MODULE
212 void *base = (void *)hnd->base;
213 size_t size = hnd->size;
214
215 if (munmap(base, size) < 0)
216 {
217 AERR("Could not munmap base:0x%p size:%lu '%s'", base, (unsigned long)size, strerror(errno));
218 }
219
220#else
221 AERR("Can't unregister DMA_BUF buffer for hnd %p. Not supported", hnd);
222#endif
223
224 }
225 else
226 {
227 AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags);
228 }
229
230 hnd->base = 0;
231 hnd->lockState = 0;
232 hnd->writeOwner = 0;
233}
234
Vishal Bhoj78e90492015-12-07 01:36:32 +0530235static int gralloc_unregister_buffer(gralloc_module_t const *module, buffer_handle_t handle)
236{
237 MALI_IGNORE(module);
238
239 if (private_handle_t::validate(handle) < 0)
240 {
241 AERR("unregistering invalid buffer 0x%p, returning error", handle);
242 return -EINVAL;
243 }
244
245 private_handle_t *hnd = (private_handle_t *)handle;
246
247 AERR_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK, "[unregister] handle %p still locked (state=%08x)", hnd, hnd->lockState);
248
249 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
250 {
251 AERR("Can't unregister buffer 0x%p as it is a framebuffer", handle);
252 }
253 else if (hnd->pid == getpid()) // never unmap buffers that were not registered in this process
254 {
255 pthread_mutex_lock(&s_map_lock);
256
John Stultz37287452016-01-20 20:08:46 -0800257 hnd->lockState &= ~(private_handle_t::LOCK_STATE_MAPPED);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530258
John Stultz37287452016-01-20 20:08:46 -0800259 /* if handle is still locked, the unmapping would not happen until unlocked*/
260 if (!(hnd->lockState & private_handle_t::LOCK_STATE_WRITE))
Vishal Bhoj78e90492015-12-07 01:36:32 +0530261 {
John Stultz37287452016-01-20 20:08:46 -0800262 unmap_buffer(hnd);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530263 }
264
John Stultz37287452016-01-20 20:08:46 -0800265 hnd->lockState |= private_handle_t::LOCK_STATE_UNREGISTERED;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530266
267 pthread_mutex_unlock(&s_map_lock);
268 }
269 else
270 {
271 AERR("Trying to unregister buffer 0x%p from process %d that was not created in current process: %d", hnd, hnd->pid, getpid());
272 }
273
274 return 0;
275}
276
277static int gralloc_lock(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void **vaddr)
278{
279 if (private_handle_t::validate(handle) < 0)
280 {
281 AERR("Locking invalid buffer 0x%p, returning error", handle);
282 return -EINVAL;
283 }
284
285 private_handle_t *hnd = (private_handle_t *)handle;
286
John Stultz37287452016-01-20 20:08:46 -0800287 pthread_mutex_lock(&s_map_lock);
288
289 if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED)
290 {
291 AERR("Locking on an unregistered buffer 0x%p, returning error", hnd);
292 pthread_mutex_unlock(&s_map_lock);
293 return -EINVAL;
294 }
295
Vishal Bhoj78e90492015-12-07 01:36:32 +0530296 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP || hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
297 {
298 hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK;
299 }
300
John Stultz37287452016-01-20 20:08:46 -0800301 hnd->lockState |= private_handle_t::LOCK_STATE_WRITE;
302
303 pthread_mutex_unlock(&s_map_lock);
304
Vishal Bhoj78e90492015-12-07 01:36:32 +0530305 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
306 {
307 *vaddr = (void *)hnd->base;
308 }
309
310 MALI_IGNORE(module);
311 MALI_IGNORE(l);
312 MALI_IGNORE(t);
313 MALI_IGNORE(w);
314 MALI_IGNORE(h);
315 return 0;
316}
317
318static int gralloc_unlock(gralloc_module_t const *module, buffer_handle_t handle)
319{
320 MALI_IGNORE(module);
321
322 if (private_handle_t::validate(handle) < 0)
323 {
324 AERR("Unlocking invalid buffer 0x%p, returning error", handle);
325 return -EINVAL;
326 }
327
328 private_handle_t *hnd = (private_handle_t *)handle;
329 int32_t current_value;
330 int32_t new_value;
331 int retry;
332
333 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP && hnd->writeOwner)
334 {
335#if GRALLOC_ARM_UMP_MODULE
336 ump_cpu_msync_now((ump_handle)hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, (void *)hnd->base, hnd->size);
337#else
338 AERR("Buffer 0x%p is UMP type but it is not supported", hnd);
339#endif
340 }
341 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION && hnd->writeOwner)
342 {
343#if GRALLOC_ARM_DMA_BUF_MODULE
344 hw_module_t *pmodule = NULL;
345 private_module_t *m = NULL;
346
347 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
348 {
349 m = reinterpret_cast<private_module_t *>(pmodule);
350 //ion_sync_fd(m->ion_client, hnd->share_fd);
351 }
352 else
353 {
354 AERR("Couldnot get gralloc module for handle 0x%p\n", handle);
355 }
356
357#endif
358 }
359
John Stultz37287452016-01-20 20:08:46 -0800360 pthread_mutex_lock(&s_map_lock);
361
362 hnd->lockState &= ~(private_handle_t::LOCK_STATE_WRITE);
363
364 /* if the handle has already been unregistered, unmap it here*/
365 if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED)
366 {
367 unmap_buffer(hnd);
368 }
369
370 pthread_mutex_unlock(&s_map_lock);
371
Vishal Bhoj78e90492015-12-07 01:36:32 +0530372 return 0;
373}
374
375// There is one global instance of the module
376
377static struct hw_module_methods_t gralloc_module_methods =
378{
John Stultz37287452016-01-20 20:08:46 -0800379 .open = gralloc_device_open,
Vishal Bhoj78e90492015-12-07 01:36:32 +0530380};
381
382private_module_t::private_module_t()
383{
384#define INIT_ZERO(obj) (memset(&(obj),0,sizeof((obj))))
385
386 base.common.tag = HARDWARE_MODULE_TAG;
387 base.common.version_major = 1;
388 base.common.version_minor = 0;
389 base.common.id = GRALLOC_HARDWARE_MODULE_ID;
390 base.common.name = "Graphics Memory Allocator Module";
391 base.common.author = "ARM Ltd.";
392 base.common.methods = &gralloc_module_methods;
393 base.common.dso = NULL;
394 INIT_ZERO(base.common.reserved);
395
396 base.registerBuffer = gralloc_register_buffer;
397 base.unregisterBuffer = gralloc_unregister_buffer;
398 base.lock = gralloc_lock;
399 base.unlock = gralloc_unlock;
400 base.perform = NULL;
401 INIT_ZERO(base.reserved_proc);
402
403 framebuffer = NULL;
404 flags = 0;
405 numBuffers = 0;
406 bufferMask = 0;
407 pthread_mutex_init(&(lock), NULL);
408 currentBuffer = NULL;
409 INIT_ZERO(info);
410 INIT_ZERO(finfo);
411 xdpi = 0.0f;
412 ydpi = 0.0f;
413 fps = 0.0f;
414
415#undef INIT_ZERO
416};
417
418/*
419 * HAL_MODULE_INFO_SYM will be initialized using the default constructor
420 * implemented above
421 */
422struct private_module_t HAL_MODULE_INFO_SYM;
423