Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / mesa / src / gallium / drivers / nouveau / nouveau_fence.c
blobbb468a832f98c1639658f3308436bfbe98292a4c
1 /*
2 * Copyright 2010 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
23 #include "util/u_double_list.h"
25 #include "nouveau_screen.h"
26 #include "nouveau_fence.h"
28 #include "nouveau/nouveau_pushbuf.h"
30 #ifdef PIPE_OS_UNIX
31 #include <sched.h>
32 #endif
34 #ifdef PIPE_OS_AROS
35 #include "drm_aros_config.h" /* for MOCK_HARDWARE define */
36 #endif
38 boolean
39 nouveau_fence_new(struct nouveau_screen *screen, struct nouveau_fence **fence,
40 boolean emit)
42 *fence = CALLOC_STRUCT(nouveau_fence);
43 if (!*fence)
44 return FALSE;
46 (*fence)->screen = screen;
47 (*fence)->ref = 1;
48 LIST_INITHEAD(&(*fence)->work);
50 if (emit)
51 nouveau_fence_emit(*fence);
53 return TRUE;
56 static void
57 nouveau_fence_trigger_work(struct nouveau_fence *fence)
59 struct nouveau_fence_work *work, *tmp;
61 LIST_FOR_EACH_ENTRY_SAFE(work, tmp, &fence->work, list) {
62 work->func(work->data);
63 LIST_DEL(&work->list);
64 FREE(work);
68 boolean
69 nouveau_fence_work(struct nouveau_fence *fence,
70 void (*func)(void *), void *data)
72 struct nouveau_fence_work *work;
74 if (!fence || fence->state == NOUVEAU_FENCE_STATE_SIGNALLED) {
75 func(data);
76 return TRUE;
79 work = CALLOC_STRUCT(nouveau_fence_work);
80 if (!work)
81 return FALSE;
82 work->func = func;
83 work->data = data;
84 LIST_ADD(&work->list, &fence->work);
85 return TRUE;
88 void
89 nouveau_fence_emit(struct nouveau_fence *fence)
91 struct nouveau_screen *screen = fence->screen;
93 fence->sequence = ++screen->fence.sequence;
95 assert(fence->state == NOUVEAU_FENCE_STATE_AVAILABLE);
97 /* set this now, so that if fence.emit triggers a flush we don't recurse */
98 fence->state = NOUVEAU_FENCE_STATE_EMITTED;
100 ++fence->ref;
102 if (screen->fence.tail)
103 screen->fence.tail->next = fence;
104 else
105 screen->fence.head = fence;
107 screen->fence.tail = fence;
109 screen->fence.emit(&screen->base, fence->sequence);
112 void
113 nouveau_fence_del(struct nouveau_fence *fence)
115 struct nouveau_fence *it;
116 struct nouveau_screen *screen = fence->screen;
118 if (fence->state == NOUVEAU_FENCE_STATE_EMITTED ||
119 fence->state == NOUVEAU_FENCE_STATE_FLUSHED) {
120 if (fence == screen->fence.head) {
121 screen->fence.head = fence->next;
122 if (!screen->fence.head)
123 screen->fence.tail = NULL;
124 } else {
125 for (it = screen->fence.head; it && it->next != fence; it = it->next);
126 it->next = fence->next;
127 if (screen->fence.tail == fence)
128 screen->fence.tail = it;
132 if (!LIST_IS_EMPTY(&fence->work)) {
133 debug_printf("WARNING: deleting fence with work still pending !\n");
134 nouveau_fence_trigger_work(fence);
137 FREE(fence);
140 void
141 nouveau_fence_update(struct nouveau_screen *screen, boolean flushed)
143 struct nouveau_fence *fence;
144 struct nouveau_fence *next = NULL;
146 #if defined(PIPE_OS_AROS) && defined(MOCK_HARDWARE)
147 /* For purpose of simulation, assume all fences are signalled */
148 u32 sequence = screen->fence.tail->sequence;
149 #else
150 u32 sequence = screen->fence.update(&screen->base);
151 #endif
153 if (screen->fence.sequence_ack == sequence)
154 return;
155 screen->fence.sequence_ack = sequence;
157 for (fence = screen->fence.head; fence; fence = next) {
158 next = fence->next;
159 sequence = fence->sequence;
161 fence->state = NOUVEAU_FENCE_STATE_SIGNALLED;
163 nouveau_fence_trigger_work(fence);
164 nouveau_fence_ref(NULL, &fence);
166 if (sequence == screen->fence.sequence_ack)
167 break;
169 screen->fence.head = next;
170 if (!next)
171 screen->fence.tail = NULL;
173 if (flushed) {
174 for (fence = next; fence; fence = fence->next)
175 fence->state = NOUVEAU_FENCE_STATE_FLUSHED;
179 #define NOUVEAU_FENCE_MAX_SPINS (1 << 31)
181 boolean
182 nouveau_fence_signalled(struct nouveau_fence *fence)
184 struct nouveau_screen *screen = fence->screen;
186 if (fence->state >= NOUVEAU_FENCE_STATE_EMITTED)
187 nouveau_fence_update(screen, FALSE);
189 return fence->state == NOUVEAU_FENCE_STATE_SIGNALLED;
192 boolean
193 nouveau_fence_wait(struct nouveau_fence *fence)
195 struct nouveau_screen *screen = fence->screen;
196 uint32_t spins = 0;
198 if (fence->state < NOUVEAU_FENCE_STATE_EMITTED) {
199 nouveau_fence_emit(fence);
201 if (fence == screen->fence.current)
202 nouveau_fence_new(screen, &screen->fence.current, FALSE);
204 if (fence->state < NOUVEAU_FENCE_STATE_FLUSHED)
205 FIRE_RING(screen->channel);
207 do {
208 nouveau_fence_update(screen, FALSE);
210 if (fence->state == NOUVEAU_FENCE_STATE_SIGNALLED)
211 return TRUE;
212 spins++;
213 #ifdef PIPE_OS_UNIX
214 if (!(spins % 8)) /* donate a few cycles */
215 sched_yield();
216 #endif
217 } while (spins < NOUVEAU_FENCE_MAX_SPINS);
219 debug_printf("Wait on fence %u (ack = %u, next = %u) timed out !\n",
220 fence->sequence,
221 screen->fence.sequence_ack, screen->fence.sequence);
223 return FALSE;
226 void
227 nouveau_fence_next(struct nouveau_screen *screen)
229 if (screen->fence.current->state < NOUVEAU_FENCE_STATE_EMITTED)
230 nouveau_fence_emit(screen->fence.current);
232 nouveau_fence_ref(NULL, &screen->fence.current);
234 nouveau_fence_new(screen, &screen->fence.current, FALSE);