2 * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
4 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
7 * A copy of the licence is included with the program, and can also be obtained from Free Software
8 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
11 #include "mali_timeline_fence_wait.h"
14 #include "mali_kernel_common.h"
15 #include "mali_spinlock_reentrant.h"
18 * Allocate a fence waiter tracker.
20 * @return New fence waiter if successful, NULL if not.
22 static struct mali_timeline_fence_wait_tracker *mali_timeline_fence_wait_tracker_alloc(void)
24 return (struct mali_timeline_fence_wait_tracker *) _mali_osk_calloc(1, sizeof(struct mali_timeline_fence_wait_tracker));
28 * Free fence waiter tracker.
30 * @param wait Fence wait tracker to free.
32 static void mali_timeline_fence_wait_tracker_free(struct mali_timeline_fence_wait_tracker *wait)
34 MALI_DEBUG_ASSERT_POINTER(wait);
35 _mali_osk_atomic_term(&wait->refcount);
40 * Check if fence wait tracker has been activated. Used as a wait queue condition.
42 * @param data Fence waiter.
43 * @return MALI_TRUE if tracker has been activated, MALI_FALSE if not.
45 static mali_bool mali_timeline_fence_wait_tracker_is_activated(void *data)
47 struct mali_timeline_fence_wait_tracker *wait;
49 wait = (struct mali_timeline_fence_wait_tracker *) data;
50 MALI_DEBUG_ASSERT_POINTER(wait);
52 return wait->activated;
56 * Check if fence has been signaled.
58 * @param system Timeline system.
59 * @param fence Timeline fence.
60 * @return MALI_TRUE if fence is signaled, MALI_FALSE if not.
62 static mali_bool mali_timeline_fence_wait_check_status(struct mali_timeline_system *system, struct mali_timeline_fence *fence)
65 u32 tid = _mali_osk_get_tid();
66 mali_bool ret = MALI_TRUE;
67 #if defined(CONFIG_SYNC)
68 struct sync_fence *sync_fence = NULL;
71 MALI_DEBUG_ASSERT_POINTER(system);
72 MALI_DEBUG_ASSERT_POINTER(fence);
74 mali_spinlock_reentrant_wait(system->spinlock, tid);
76 for (i = 0; i < MALI_TIMELINE_MAX; ++i) {
77 struct mali_timeline *timeline;
78 mali_timeline_point point;
80 point = fence->points[i];
82 if (likely(MALI_TIMELINE_NO_POINT == point)) {
83 /* Fence contains no point on this timeline. */
87 timeline = system->timelines[i];
88 MALI_DEBUG_ASSERT_POINTER(timeline);
90 if (unlikely(!mali_timeline_is_point_valid(timeline, point))) {
91 MALI_PRINT_ERROR(("Mali Timeline: point %d is not valid (oldest=%d, next=%d)\n", point, timeline->point_oldest, timeline->point_next));
94 if (!mali_timeline_is_point_released(timeline, point)) {
100 #if defined(CONFIG_SYNC)
101 if (-1 != fence->sync_fd) {
102 sync_fence = sync_fence_fdget(fence->sync_fd);
103 if (likely(NULL != sync_fence)) {
104 if (0 == sync_fence->status) {
108 MALI_PRINT_ERROR(("Mali Timeline: failed to get sync fence from fd %d\n", fence->sync_fd));
111 #endif /* defined(CONFIG_SYNC) */
114 mali_spinlock_reentrant_signal(system->spinlock, tid);
116 #if defined(CONFIG_SYNC)
117 if (NULL != sync_fence) {
118 sync_fence_put(sync_fence);
120 #endif /* defined(CONFIG_SYNC) */
125 mali_bool mali_timeline_fence_wait(struct mali_timeline_system *system, struct mali_timeline_fence *fence, u32 timeout)
127 struct mali_timeline_fence_wait_tracker *wait;
128 mali_timeline_point point;
131 MALI_DEBUG_ASSERT_POINTER(system);
132 MALI_DEBUG_ASSERT_POINTER(fence);
134 MALI_DEBUG_PRINT(4, ("Mali Timeline: wait on fence\n"));
136 if (MALI_TIMELINE_FENCE_WAIT_TIMEOUT_IMMEDIATELY == timeout) {
137 return mali_timeline_fence_wait_check_status(system, fence);
140 wait = mali_timeline_fence_wait_tracker_alloc();
141 if (unlikely(NULL == wait)) {
142 MALI_PRINT_ERROR(("Mali Timeline: failed to allocate data for fence wait\n"));
146 wait->activated = MALI_FALSE;
147 wait->system = system;
149 /* Initialize refcount to two references. The reference first will be released by this
150 * function after the wait is over. The second reference will be released when the tracker
152 _mali_osk_atomic_init(&wait->refcount, 2);
154 /* Add tracker to timeline system, but not to a timeline. */
155 mali_timeline_tracker_init(&wait->tracker, MALI_TIMELINE_TRACKER_WAIT, fence, wait);
156 point = mali_timeline_system_add_tracker(system, &wait->tracker, MALI_TIMELINE_NONE);
157 MALI_DEBUG_ASSERT(MALI_TIMELINE_NO_POINT == point);
160 /* Wait for the tracker to be activated or time out. */
161 if (MALI_TIMELINE_FENCE_WAIT_TIMEOUT_NEVER == timeout) {
162 _mali_osk_wait_queue_wait_event(system->wait_queue, mali_timeline_fence_wait_tracker_is_activated, (void *) wait);
164 _mali_osk_wait_queue_wait_event_timeout(system->wait_queue, mali_timeline_fence_wait_tracker_is_activated, (void *) wait, timeout);
167 ret = wait->activated;
169 if (0 == _mali_osk_atomic_dec_return(&wait->refcount)) {
170 mali_timeline_fence_wait_tracker_free(wait);
176 void mali_timeline_fence_wait_activate(struct mali_timeline_fence_wait_tracker *wait)
178 mali_scheduler_mask schedule_mask = MALI_SCHEDULER_MASK_EMPTY;
180 MALI_DEBUG_ASSERT_POINTER(wait);
181 MALI_DEBUG_ASSERT_POINTER(wait->system);
183 MALI_DEBUG_PRINT(4, ("Mali Timeline: activation for fence wait tracker\n"));
185 MALI_DEBUG_ASSERT(MALI_FALSE == wait->activated);
186 wait->activated = MALI_TRUE;
188 _mali_osk_wait_queue_wake_up(wait->system->wait_queue);
190 /* Nothing can wait on this tracker, so nothing to schedule after release. */
191 schedule_mask = mali_timeline_tracker_release(&wait->tracker);
192 MALI_DEBUG_ASSERT(MALI_SCHEDULER_MASK_EMPTY == schedule_mask);
193 MALI_IGNORE(schedule_mask);
195 if (0 == _mali_osk_atomic_dec_return(&wait->refcount)) {
196 mali_timeline_fence_wait_tracker_free(wait);