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
23 #include "util/u_double_list.h"
25 #include "nouveau_screen.h"
26 #include "nouveau_fence.h"
28 #include "nouveau/nouveau_pushbuf.h"
35 #include "drm_aros_config.h" /* for MOCK_HARDWARE define */
39 nouveau_fence_new(struct nouveau_screen
*screen
, struct nouveau_fence
**fence
,
42 *fence
= CALLOC_STRUCT(nouveau_fence
);
46 (*fence
)->screen
= screen
;
48 LIST_INITHEAD(&(*fence
)->work
);
51 nouveau_fence_emit(*fence
);
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
);
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
) {
79 work
= CALLOC_STRUCT(nouveau_fence_work
);
84 LIST_ADD(&work
->list
, &fence
->work
);
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
;
102 if (screen
->fence
.tail
)
103 screen
->fence
.tail
->next
= fence
;
105 screen
->fence
.head
= fence
;
107 screen
->fence
.tail
= fence
;
109 screen
->fence
.emit(&screen
->base
, fence
->sequence
);
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
;
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
);
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
;
150 u32 sequence
= screen
->fence
.update(&screen
->base
);
153 if (screen
->fence
.sequence_ack
== sequence
)
155 screen
->fence
.sequence_ack
= sequence
;
157 for (fence
= screen
->fence
.head
; fence
; 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
)
169 screen
->fence
.head
= next
;
171 screen
->fence
.tail
= NULL
;
174 for (fence
= next
; fence
; fence
= fence
->next
)
175 fence
->state
= NOUVEAU_FENCE_STATE_FLUSHED
;
179 #define NOUVEAU_FENCE_MAX_SPINS (1 << 31)
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
;
193 nouveau_fence_wait(struct nouveau_fence
*fence
)
195 struct nouveau_screen
*screen
= fence
->screen
;
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
);
208 nouveau_fence_update(screen
, FALSE
);
210 if (fence
->state
== NOUVEAU_FENCE_STATE_SIGNALLED
)
214 if (!(spins
% 8)) /* donate a few cycles */
217 } while (spins
< NOUVEAU_FENCE_MAX_SPINS
);
219 debug_printf("Wait on fence %u (ack = %u, next = %u) timed out !\n",
221 screen
->fence
.sequence_ack
, screen
->fence
.sequence
);
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
);