blob: d0cb08092ba252f2df5be5a18e0214f4fbaff209 [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
Vishal Bhoj78e90492015-12-07 01:36:32 +0530138 unsigned char *mappedAddress;
139 size_t size = hnd->size;
140 hw_module_t *pmodule = NULL;
141 private_module_t *m = NULL;
142
143 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
144 {
145 m = reinterpret_cast<private_module_t *>(pmodule);
146 }
147 else
148 {
149 AERR("Could not get gralloc module for handle: 0x%p", hnd);
150 retval = -errno;
151 goto cleanup;
152 }
153
154 /* the test condition is set to m->ion_client <= 0 here, because:
155 * 1) module structure are initialized to 0 if no initial value is applied
156 * 2) a second user process should get a ion fd greater than 0.
157 */
158 if (m->ion_client <= 0)
159 {
160 /* a second user process must obtain a client handle first via ion_open before it can obtain the shared ion buffer*/
161 m->ion_client = ion_open();
162
163 if (m->ion_client < 0)
164 {
165 AERR("Could not open ion device for handle: 0x%p", hnd);
166 retval = -errno;
167 goto cleanup;
168 }
169 }
170
171 mappedAddress = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, hnd->share_fd, 0);
172
173 if (MAP_FAILED == mappedAddress)
174 {
175 AERR("mmap( share_fd:%d ) failed with %s", hnd->share_fd, strerror(errno));
176 retval = -errno;
177 goto cleanup;
178 }
179
180 hnd->base = mappedAddress + hnd->offset;
John Stultz37287452016-01-20 20:08:46 -0800181 hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530182 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
John Stultz37287452016-01-20 20:08:46 -0800196static void unmap_buffer(private_handle_t *hnd)
197{
198 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
199 {
200#if GRALLOC_ARM_UMP_MODULE
201 ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
202 ump_reference_release((ump_handle)hnd->ump_mem_handle);
203 hnd->ump_mem_handle = (int)UMP_INVALID_MEMORY_HANDLE;
204#else
205 AERR("Can't unregister UMP buffer for handle 0x%p. Not supported", hnd);
206#endif
207 }
208 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
209 {
210#if GRALLOC_ARM_DMA_BUF_MODULE
211 void *base = (void *)hnd->base;
212 size_t size = hnd->size;
213
214 if (munmap(base, size) < 0)
215 {
216 AERR("Could not munmap base:0x%p size:%lu '%s'", base, (unsigned long)size, strerror(errno));
217 }
218
219#else
220 AERR("Can't unregister DMA_BUF buffer for hnd %p. Not supported", hnd);
221#endif
222
223 }
224 else
225 {
226 AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags);
227 }
228
229 hnd->base = 0;
230 hnd->lockState = 0;
231 hnd->writeOwner = 0;
232}
233
Vishal Bhoj78e90492015-12-07 01:36:32 +0530234static int gralloc_unregister_buffer(gralloc_module_t const *module, buffer_handle_t handle)
235{
236 MALI_IGNORE(module);
237
238 if (private_handle_t::validate(handle) < 0)
239 {
240 AERR("unregistering invalid buffer 0x%p, returning error", handle);
241 return -EINVAL;
242 }
243
244 private_handle_t *hnd = (private_handle_t *)handle;
245
246 AERR_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK, "[unregister] handle %p still locked (state=%08x)", hnd, hnd->lockState);
247
248 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
249 {
250 AERR("Can't unregister buffer 0x%p as it is a framebuffer", handle);
251 }
252 else if (hnd->pid == getpid()) // never unmap buffers that were not registered in this process
253 {
254 pthread_mutex_lock(&s_map_lock);
255
John Stultz37287452016-01-20 20:08:46 -0800256 hnd->lockState &= ~(private_handle_t::LOCK_STATE_MAPPED);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530257
John Stultz37287452016-01-20 20:08:46 -0800258 /* if handle is still locked, the unmapping would not happen until unlocked*/
259 if (!(hnd->lockState & private_handle_t::LOCK_STATE_WRITE))
Vishal Bhoj78e90492015-12-07 01:36:32 +0530260 {
John Stultz37287452016-01-20 20:08:46 -0800261 unmap_buffer(hnd);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530262 }
263
John Stultz37287452016-01-20 20:08:46 -0800264 hnd->lockState |= private_handle_t::LOCK_STATE_UNREGISTERED;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530265
266 pthread_mutex_unlock(&s_map_lock);
267 }
268 else
269 {
270 AERR("Trying to unregister buffer 0x%p from process %d that was not created in current process: %d", hnd, hnd->pid, getpid());
271 }
272
273 return 0;
274}
275
276static int gralloc_lock(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void **vaddr)
277{
278 if (private_handle_t::validate(handle) < 0)
279 {
280 AERR("Locking invalid buffer 0x%p, returning error", handle);
281 return -EINVAL;
282 }
283
284 private_handle_t *hnd = (private_handle_t *)handle;
285
John Stultz37287452016-01-20 20:08:46 -0800286 pthread_mutex_lock(&s_map_lock);
287
288 if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED)
289 {
290 AERR("Locking on an unregistered buffer 0x%p, returning error", hnd);
291 pthread_mutex_unlock(&s_map_lock);
292 return -EINVAL;
293 }
294
Vishal Bhoj78e90492015-12-07 01:36:32 +0530295 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP || hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
296 {
297 hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK;
298 }
299
John Stultz37287452016-01-20 20:08:46 -0800300 hnd->lockState |= private_handle_t::LOCK_STATE_WRITE;
301
302 pthread_mutex_unlock(&s_map_lock);
303
Vishal Bhoj78e90492015-12-07 01:36:32 +0530304 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
305 {
306 *vaddr = (void *)hnd->base;
307 }
308
309 MALI_IGNORE(module);
310 MALI_IGNORE(l);
311 MALI_IGNORE(t);
312 MALI_IGNORE(w);
313 MALI_IGNORE(h);
314 return 0;
315}
316
317static int gralloc_unlock(gralloc_module_t const *module, buffer_handle_t handle)
318{
319 MALI_IGNORE(module);
320
321 if (private_handle_t::validate(handle) < 0)
322 {
323 AERR("Unlocking invalid buffer 0x%p, returning error", handle);
324 return -EINVAL;
325 }
326
327 private_handle_t *hnd = (private_handle_t *)handle;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530328
329 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP && hnd->writeOwner)
330 {
331#if GRALLOC_ARM_UMP_MODULE
332 ump_cpu_msync_now((ump_handle)hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, (void *)hnd->base, hnd->size);
333#else
334 AERR("Buffer 0x%p is UMP type but it is not supported", hnd);
335#endif
336 }
337 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION && hnd->writeOwner)
338 {
339#if GRALLOC_ARM_DMA_BUF_MODULE
340 hw_module_t *pmodule = NULL;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530341
Dmitry Shmidt04a610a2017-12-06 14:01:58 -0800342 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) != 0)
Vishal Bhoj78e90492015-12-07 01:36:32 +0530343 {
344 AERR("Couldnot get gralloc module for handle 0x%p\n", handle);
345 }
Vishal Bhoj78e90492015-12-07 01:36:32 +0530346#endif
347 }
348
John Stultz37287452016-01-20 20:08:46 -0800349 pthread_mutex_lock(&s_map_lock);
350
351 hnd->lockState &= ~(private_handle_t::LOCK_STATE_WRITE);
352
353 /* if the handle has already been unregistered, unmap it here*/
354 if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED)
355 {
356 unmap_buffer(hnd);
357 }
358
359 pthread_mutex_unlock(&s_map_lock);
360
Vishal Bhoj78e90492015-12-07 01:36:32 +0530361 return 0;
362}
363
364// There is one global instance of the module
365
366static struct hw_module_methods_t gralloc_module_methods =
367{
John Stultz37287452016-01-20 20:08:46 -0800368 .open = gralloc_device_open,
Vishal Bhoj78e90492015-12-07 01:36:32 +0530369};
370
371private_module_t::private_module_t()
372{
373#define INIT_ZERO(obj) (memset(&(obj),0,sizeof((obj))))
374
375 base.common.tag = HARDWARE_MODULE_TAG;
376 base.common.version_major = 1;
377 base.common.version_minor = 0;
378 base.common.id = GRALLOC_HARDWARE_MODULE_ID;
379 base.common.name = "Graphics Memory Allocator Module";
380 base.common.author = "ARM Ltd.";
381 base.common.methods = &gralloc_module_methods;
382 base.common.dso = NULL;
383 INIT_ZERO(base.common.reserved);
384
385 base.registerBuffer = gralloc_register_buffer;
386 base.unregisterBuffer = gralloc_unregister_buffer;
387 base.lock = gralloc_lock;
388 base.unlock = gralloc_unlock;
389 base.perform = NULL;
390 INIT_ZERO(base.reserved_proc);
391
392 framebuffer = NULL;
393 flags = 0;
394 numBuffers = 0;
395 bufferMask = 0;
396 pthread_mutex_init(&(lock), NULL);
397 currentBuffer = NULL;
398 INIT_ZERO(info);
399 INIT_ZERO(finfo);
400 xdpi = 0.0f;
401 ydpi = 0.0f;
402 fps = 0.0f;
403
404#undef INIT_ZERO
405};
406
407/*
408 * HAL_MODULE_INFO_SYM will be initialized using the default constructor
409 * implemented above
410 */
411struct private_module_t HAL_MODULE_INFO_SYM;
412