blob: 73e5c17f79c7fdf3d21237ebb5667bebe3c061af [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>
Dan Albert43968442017-10-12 13:26:46 -070021#include <string.h>
Vishal Bhoj78e90492015-12-07 01:36:32 +053022
23#include <cutils/log.h>
24#include <cutils/atomic.h>
25#include <hardware/hardware.h>
26#include <hardware/gralloc.h>
27
28#include "gralloc_priv.h"
29#include "alloc_device.h"
30#include "framebuffer_device.h"
31
32#if GRALLOC_ARM_UMP_MODULE
33#include <ump/ump_ref_drv.h>
34static int s_ump_is_open = 0;
35#endif
36
37#if GRALLOC_ARM_DMA_BUF_MODULE
38#include <linux/ion.h>
39#include <ion/ion.h>
40#include <sys/mman.h>
41#endif
42
43static pthread_mutex_t s_map_lock = PTHREAD_MUTEX_INITIALIZER;
44
45static int gralloc_device_open(const hw_module_t *module, const char *name, hw_device_t **device)
46{
47 int status = -EINVAL;
48
49 if (!strncmp(name, GRALLOC_HARDWARE_GPU0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
50 {
51 status = alloc_device_open(module, name, device);
52 }
53 else if (!strncmp(name, GRALLOC_HARDWARE_FB0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN))
54 {
55 status = framebuffer_device_open(module, name, device);
56 }
57
58 return status;
59}
60
61static int gralloc_register_buffer(gralloc_module_t const *module, buffer_handle_t handle)
62{
63 MALI_IGNORE(module);
64
65 if (private_handle_t::validate(handle) < 0)
66 {
67 AERR("Registering invalid buffer 0x%p, returning error", handle);
68 return -EINVAL;
69 }
70
71 // if this handle was created in this process, then we keep it as is.
72 private_handle_t *hnd = (private_handle_t *)handle;
73
74 int retval = -EINVAL;
75
76 pthread_mutex_lock(&s_map_lock);
77
78#if GRALLOC_ARM_UMP_MODULE
79
80 if (!s_ump_is_open)
81 {
82 ump_result res = ump_open(); // MJOLL-4012: UMP implementation needs a ump_close() for each ump_open
83
84 if (res != UMP_OK)
85 {
86 pthread_mutex_unlock(&s_map_lock);
87 AERR("Failed to open UMP library with res=%d", res);
88 return retval;
89 }
90
91 s_ump_is_open = 1;
92 }
93
94#endif
95
96 hnd->pid = getpid();
97
98 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
99 {
Chia-I Wufba69b52017-05-08 13:10:08 -0700100 AINF("Register framebuffer 0x%p is no-op", handle);
101 retval = 0;
102
Vishal Bhoj78e90492015-12-07 01:36:32 +0530103 }
104 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
105 {
106#if GRALLOC_ARM_UMP_MODULE
107 hnd->ump_mem_handle = (int)ump_handle_create_from_secure_id(hnd->ump_id);
108
109 if (UMP_INVALID_MEMORY_HANDLE != (ump_handle)hnd->ump_mem_handle)
110 {
111 hnd->base = ump_mapped_pointer_get((ump_handle)hnd->ump_mem_handle);
112
113 if (0 != hnd->base)
114 {
Vishal Bhoj78e90492015-12-07 01:36:32 +0530115 hnd->writeOwner = 0;
John Stultz37287452016-01-20 20:08:46 -0800116 hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530117 pthread_mutex_unlock(&s_map_lock);
118 return 0;
119 }
120 else
121 {
122 AERR("Failed to map UMP handle 0x%x", hnd->ump_mem_handle);
123 }
124
125 ump_reference_release((ump_handle)hnd->ump_mem_handle);
126 }
127 else
128 {
129 AERR("Failed to create UMP handle 0x%x", hnd->ump_mem_handle);
130 }
131
132#else
133 AERR("Gralloc does not support UMP. Unable to register UMP memory for handle 0x%p", hnd);
134#endif
135 }
136 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
137 {
138#if GRALLOC_ARM_DMA_BUF_MODULE
139 int ret;
140 unsigned char *mappedAddress;
141 size_t size = hnd->size;
142 hw_module_t *pmodule = NULL;
143 private_module_t *m = NULL;
144
145 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
146 {
147 m = reinterpret_cast<private_module_t *>(pmodule);
148 }
149 else
150 {
151 AERR("Could not get gralloc module for handle: 0x%p", hnd);
152 retval = -errno;
153 goto cleanup;
154 }
155
156 /* the test condition is set to m->ion_client <= 0 here, because:
157 * 1) module structure are initialized to 0 if no initial value is applied
158 * 2) a second user process should get a ion fd greater than 0.
159 */
160 if (m->ion_client <= 0)
161 {
162 /* a second user process must obtain a client handle first via ion_open before it can obtain the shared ion buffer*/
163 m->ion_client = ion_open();
164
165 if (m->ion_client < 0)
166 {
167 AERR("Could not open ion device for handle: 0x%p", hnd);
168 retval = -errno;
169 goto cleanup;
170 }
171 }
172
173 mappedAddress = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, hnd->share_fd, 0);
174
175 if (MAP_FAILED == mappedAddress)
176 {
177 AERR("mmap( share_fd:%d ) failed with %s", hnd->share_fd, strerror(errno));
178 retval = -errno;
179 goto cleanup;
180 }
181
182 hnd->base = mappedAddress + hnd->offset;
John Stultz37287452016-01-20 20:08:46 -0800183 hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530184 pthread_mutex_unlock(&s_map_lock);
185 return 0;
186#endif
187 }
188 else
189 {
190 AERR("registering non-UMP buffer not supported. flags = %d", hnd->flags);
191 }
192
193cleanup:
194 pthread_mutex_unlock(&s_map_lock);
195 return retval;
196}
197
John Stultz37287452016-01-20 20:08:46 -0800198static void unmap_buffer(private_handle_t *hnd)
199{
200 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP)
201 {
202#if GRALLOC_ARM_UMP_MODULE
203 ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
204 ump_reference_release((ump_handle)hnd->ump_mem_handle);
205 hnd->ump_mem_handle = (int)UMP_INVALID_MEMORY_HANDLE;
206#else
207 AERR("Can't unregister UMP buffer for handle 0x%p. Not supported", hnd);
208#endif
209 }
210 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
211 {
212#if GRALLOC_ARM_DMA_BUF_MODULE
213 void *base = (void *)hnd->base;
214 size_t size = hnd->size;
215
216 if (munmap(base, size) < 0)
217 {
218 AERR("Could not munmap base:0x%p size:%lu '%s'", base, (unsigned long)size, strerror(errno));
219 }
220
221#else
222 AERR("Can't unregister DMA_BUF buffer for hnd %p. Not supported", hnd);
223#endif
224
225 }
226 else
227 {
228 AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags);
229 }
230
231 hnd->base = 0;
232 hnd->lockState = 0;
233 hnd->writeOwner = 0;
234}
235
Vishal Bhoj78e90492015-12-07 01:36:32 +0530236static int gralloc_unregister_buffer(gralloc_module_t const *module, buffer_handle_t handle)
237{
238 MALI_IGNORE(module);
239
240 if (private_handle_t::validate(handle) < 0)
241 {
242 AERR("unregistering invalid buffer 0x%p, returning error", handle);
243 return -EINVAL;
244 }
245
246 private_handle_t *hnd = (private_handle_t *)handle;
247
248 AERR_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK, "[unregister] handle %p still locked (state=%08x)", hnd, hnd->lockState);
249
250 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)
251 {
252 AERR("Can't unregister buffer 0x%p as it is a framebuffer", handle);
253 }
254 else if (hnd->pid == getpid()) // never unmap buffers that were not registered in this process
255 {
256 pthread_mutex_lock(&s_map_lock);
257
John Stultz37287452016-01-20 20:08:46 -0800258 hnd->lockState &= ~(private_handle_t::LOCK_STATE_MAPPED);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530259
John Stultz37287452016-01-20 20:08:46 -0800260 /* if handle is still locked, the unmapping would not happen until unlocked*/
261 if (!(hnd->lockState & private_handle_t::LOCK_STATE_WRITE))
Vishal Bhoj78e90492015-12-07 01:36:32 +0530262 {
John Stultz37287452016-01-20 20:08:46 -0800263 unmap_buffer(hnd);
Vishal Bhoj78e90492015-12-07 01:36:32 +0530264 }
265
John Stultz37287452016-01-20 20:08:46 -0800266 hnd->lockState |= private_handle_t::LOCK_STATE_UNREGISTERED;
Vishal Bhoj78e90492015-12-07 01:36:32 +0530267
268 pthread_mutex_unlock(&s_map_lock);
269 }
270 else
271 {
272 AERR("Trying to unregister buffer 0x%p from process %d that was not created in current process: %d", hnd, hnd->pid, getpid());
273 }
274
275 return 0;
276}
277
278static int gralloc_lock(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void **vaddr)
279{
280 if (private_handle_t::validate(handle) < 0)
281 {
282 AERR("Locking invalid buffer 0x%p, returning error", handle);
283 return -EINVAL;
284 }
285
286 private_handle_t *hnd = (private_handle_t *)handle;
287
John Stultz37287452016-01-20 20:08:46 -0800288 pthread_mutex_lock(&s_map_lock);
289
290 if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED)
291 {
292 AERR("Locking on an unregistered buffer 0x%p, returning error", hnd);
293 pthread_mutex_unlock(&s_map_lock);
294 return -EINVAL;
295 }
296
Vishal Bhoj78e90492015-12-07 01:36:32 +0530297 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP || hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
298 {
299 hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK;
300 }
301
John Stultz37287452016-01-20 20:08:46 -0800302 hnd->lockState |= private_handle_t::LOCK_STATE_WRITE;
303
304 pthread_mutex_unlock(&s_map_lock);
305
Vishal Bhoj78e90492015-12-07 01:36:32 +0530306 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
307 {
308 *vaddr = (void *)hnd->base;
309 }
310
311 MALI_IGNORE(module);
312 MALI_IGNORE(l);
313 MALI_IGNORE(t);
314 MALI_IGNORE(w);
315 MALI_IGNORE(h);
316 return 0;
317}
318
319static int gralloc_unlock(gralloc_module_t const *module, buffer_handle_t handle)
320{
321 MALI_IGNORE(module);
322
323 if (private_handle_t::validate(handle) < 0)
324 {
325 AERR("Unlocking invalid buffer 0x%p, returning error", handle);
326 return -EINVAL;
327 }
328
329 private_handle_t *hnd = (private_handle_t *)handle;
330 int32_t current_value;
331 int32_t new_value;
332 int retry;
333
334 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP && hnd->writeOwner)
335 {
336#if GRALLOC_ARM_UMP_MODULE
337 ump_cpu_msync_now((ump_handle)hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, (void *)hnd->base, hnd->size);
338#else
339 AERR("Buffer 0x%p is UMP type but it is not supported", hnd);
340#endif
341 }
342 else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION && hnd->writeOwner)
343 {
344#if GRALLOC_ARM_DMA_BUF_MODULE
345 hw_module_t *pmodule = NULL;
346 private_module_t *m = NULL;
347
348 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0)
349 {
350 m = reinterpret_cast<private_module_t *>(pmodule);
351 //ion_sync_fd(m->ion_client, hnd->share_fd);
352 }
353 else
354 {
355 AERR("Couldnot get gralloc module for handle 0x%p\n", handle);
356 }
357
358#endif
359 }
360
John Stultz37287452016-01-20 20:08:46 -0800361 pthread_mutex_lock(&s_map_lock);
362
363 hnd->lockState &= ~(private_handle_t::LOCK_STATE_WRITE);
364
365 /* if the handle has already been unregistered, unmap it here*/
366 if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED)
367 {
368 unmap_buffer(hnd);
369 }
370
371 pthread_mutex_unlock(&s_map_lock);
372
Vishal Bhoj78e90492015-12-07 01:36:32 +0530373 return 0;
374}
375
376// There is one global instance of the module
377
378static struct hw_module_methods_t gralloc_module_methods =
379{
John Stultz37287452016-01-20 20:08:46 -0800380 .open = gralloc_device_open,
Vishal Bhoj78e90492015-12-07 01:36:32 +0530381};
382
383private_module_t::private_module_t()
384{
385#define INIT_ZERO(obj) (memset(&(obj),0,sizeof((obj))))
386
387 base.common.tag = HARDWARE_MODULE_TAG;
388 base.common.version_major = 1;
389 base.common.version_minor = 0;
390 base.common.id = GRALLOC_HARDWARE_MODULE_ID;
391 base.common.name = "Graphics Memory Allocator Module";
392 base.common.author = "ARM Ltd.";
393 base.common.methods = &gralloc_module_methods;
394 base.common.dso = NULL;
395 INIT_ZERO(base.common.reserved);
396
397 base.registerBuffer = gralloc_register_buffer;
398 base.unregisterBuffer = gralloc_unregister_buffer;
399 base.lock = gralloc_lock;
400 base.unlock = gralloc_unlock;
401 base.perform = NULL;
402 INIT_ZERO(base.reserved_proc);
403
404 framebuffer = NULL;
405 flags = 0;
406 numBuffers = 0;
407 bufferMask = 0;
408 pthread_mutex_init(&(lock), NULL);
409 currentBuffer = NULL;
410 INIT_ZERO(info);
411 INIT_ZERO(finfo);
412 xdpi = 0.0f;
413 ydpi = 0.0f;
414 fps = 0.0f;
415
416#undef INIT_ZERO
417};
418
419/*
420 * HAL_MODULE_INFO_SYM will be initialized using the default constructor
421 * implemented above
422 */
423struct private_module_t HAL_MODULE_INFO_SYM;
424