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 "nvc0_fence.h"
24 #include "nvc0_context.h"
25 #include "nvc0_screen.h"
32 nvc0_screen_fence_new(struct nvc0_screen
*screen
, struct nvc0_fence
**fence
,
35 *fence
= CALLOC_STRUCT(nvc0_fence
);
39 (*fence
)->screen
= screen
;
43 nvc0_fence_emit(*fence
);
49 nvc0_fence_emit(struct nvc0_fence
*fence
)
51 struct nvc0_screen
*screen
= fence
->screen
;
52 struct nouveau_channel
*chan
= screen
->base
.channel
;
54 fence
->sequence
= ++screen
->fence
.sequence
;
56 assert(fence
->state
== NVC0_FENCE_STATE_AVAILABLE
);
58 BEGIN_RING(chan
, RING_3D(QUERY_ADDRESS_HIGH
), 4);
59 OUT_RELOCh(chan
, screen
->fence
.bo
, 0, NOUVEAU_BO_WR
);
60 OUT_RELOCl(chan
, screen
->fence
.bo
, 0, NOUVEAU_BO_WR
);
61 OUT_RING (chan
, fence
->sequence
);
62 OUT_RING (chan
, NVC0_3D_QUERY_GET_FENCE
);
66 if (screen
->fence
.tail
)
67 screen
->fence
.tail
->next
= fence
;
69 screen
->fence
.head
= fence
;
71 screen
->fence
.tail
= fence
;
73 fence
->state
= NVC0_FENCE_STATE_EMITTED
;
77 nvc0_fence_trigger_release_buffers(struct nvc0_fence
*fence
);
80 nvc0_fence_del(struct nvc0_fence
*fence
)
82 struct nvc0_fence
*it
;
83 struct nvc0_screen
*screen
= fence
->screen
;
85 if (fence
->state
== NVC0_FENCE_STATE_EMITTED
) {
86 if (fence
== screen
->fence
.head
) {
87 screen
->fence
.head
= fence
->next
;
88 if (!screen
->fence
.head
)
89 screen
->fence
.tail
= NULL
;
91 for (it
= screen
->fence
.head
; it
&& it
->next
!= fence
; it
= it
->next
);
92 it
->next
= fence
->next
;
93 if (screen
->fence
.tail
== fence
)
94 screen
->fence
.tail
= it
;
99 debug_printf("WARNING: deleting fence with buffers "
100 "still hooked to it !\n");
101 nvc0_fence_trigger_release_buffers(fence
);
108 nvc0_fence_trigger_release_buffers(struct nvc0_fence
*fence
)
110 struct nvc0_mm_allocation
*alloc
= fence
->buffers
;
113 struct nvc0_mm_allocation
*next
= alloc
->next
;
117 fence
->buffers
= NULL
;
121 nvc0_screen_fence_update(struct nvc0_screen
*screen
)
123 struct nvc0_fence
*fence
;
124 struct nvc0_fence
*next
= NULL
;
125 uint32_t sequence
= screen
->fence
.map
[0];
127 if (screen
->fence
.sequence_ack
== sequence
)
129 screen
->fence
.sequence_ack
= sequence
;
131 for (fence
= screen
->fence
.head
; fence
; fence
= next
) {
133 sequence
= fence
->sequence
;
135 fence
->state
= NVC0_FENCE_STATE_SIGNALLED
;
138 nvc0_fence_trigger_release_buffers(fence
);
140 nvc0_fence_reference(&fence
, NULL
);
142 if (sequence
== screen
->fence
.sequence_ack
)
145 screen
->fence
.head
= next
;
147 screen
->fence
.tail
= NULL
;
150 #define NVC0_FENCE_MAX_SPINS (1 << 17)
153 nvc0_fence_signalled(struct nvc0_fence
*fence
)
155 struct nvc0_screen
*screen
= fence
->screen
;
157 if (fence
->state
== NVC0_FENCE_STATE_EMITTED
)
158 nvc0_screen_fence_update(screen
);
160 return fence
->state
== NVC0_FENCE_STATE_SIGNALLED
;
164 nvc0_fence_wait(struct nvc0_fence
*fence
)
166 struct nvc0_screen
*screen
= fence
->screen
;
169 if (fence
->state
== NVC0_FENCE_STATE_AVAILABLE
) {
170 nvc0_fence_emit(fence
);
172 FIRE_RING(screen
->base
.channel
);
174 if (fence
== screen
->fence
.current
)
175 nvc0_screen_fence_new(screen
, &screen
->fence
.current
, FALSE
);
179 nvc0_screen_fence_update(screen
);
181 if (fence
->state
== NVC0_FENCE_STATE_SIGNALLED
)
185 if (!(spins
% 8)) /* donate a few cycles */
188 } while (spins
< NVC0_FENCE_MAX_SPINS
);
191 NOUVEAU_ERR("fence %x: been spinning too long\n", fence
->sequence
);
197 nvc0_screen_fence_next(struct nvc0_screen
*screen
)
199 nvc0_fence_emit(screen
->fence
.current
);
200 nvc0_screen_fence_new(screen
, &screen
->fence
.current
, FALSE
);
201 nvc0_screen_fence_update(screen
);