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 MARK_RING (chan
, 5, 2);
59 BEGIN_RING(chan
, RING_3D(QUERY_ADDRESS_HIGH
), 4);
60 OUT_RELOCh(chan
, screen
->fence
.bo
, 0, NOUVEAU_BO_WR
);
61 OUT_RELOCl(chan
, screen
->fence
.bo
, 0, NOUVEAU_BO_WR
);
62 OUT_RING (chan
, fence
->sequence
);
63 OUT_RING (chan
, NVC0_3D_QUERY_GET_FENCE
| NVC0_3D_QUERY_GET_SHORT
|
64 (0xf << NVC0_3D_QUERY_GET_UNIT__SHIFT
));
68 if (screen
->fence
.tail
)
69 screen
->fence
.tail
->next
= fence
;
71 screen
->fence
.head
= fence
;
73 screen
->fence
.tail
= fence
;
75 fence
->state
= NVC0_FENCE_STATE_EMITTED
;
79 nvc0_fence_trigger_release_buffers(struct nvc0_fence
*fence
);
82 nvc0_fence_del(struct nvc0_fence
*fence
)
84 struct nvc0_fence
*it
;
85 struct nvc0_screen
*screen
= fence
->screen
;
87 if (fence
->state
== NVC0_FENCE_STATE_EMITTED
) {
88 if (fence
== screen
->fence
.head
) {
89 screen
->fence
.head
= fence
->next
;
90 if (!screen
->fence
.head
)
91 screen
->fence
.tail
= NULL
;
93 for (it
= screen
->fence
.head
; it
&& it
->next
!= fence
; it
= it
->next
);
94 it
->next
= fence
->next
;
95 if (screen
->fence
.tail
== fence
)
96 screen
->fence
.tail
= it
;
100 if (fence
->buffers
) {
101 debug_printf("WARNING: deleting fence with buffers "
102 "still hooked to it !\n");
103 nvc0_fence_trigger_release_buffers(fence
);
110 nvc0_fence_trigger_release_buffers(struct nvc0_fence
*fence
)
112 struct nvc0_mm_allocation
*alloc
= fence
->buffers
;
115 struct nvc0_mm_allocation
*next
= alloc
->next
;
119 fence
->buffers
= NULL
;
123 nvc0_screen_fence_update(struct nvc0_screen
*screen
)
125 struct nvc0_fence
*fence
;
126 struct nvc0_fence
*next
= NULL
;
127 uint32_t sequence
= screen
->fence
.map
[0];
129 if (screen
->fence
.sequence_ack
== sequence
)
131 screen
->fence
.sequence_ack
= sequence
;
133 for (fence
= screen
->fence
.head
; fence
; fence
= next
) {
135 sequence
= fence
->sequence
;
137 fence
->state
= NVC0_FENCE_STATE_SIGNALLED
;
140 nvc0_fence_trigger_release_buffers(fence
);
142 nvc0_fence_reference(&fence
, NULL
);
144 if (sequence
== screen
->fence
.sequence_ack
)
147 screen
->fence
.head
= next
;
149 screen
->fence
.tail
= NULL
;
152 #define NVC0_FENCE_MAX_SPINS (1 << 17)
155 nvc0_fence_signalled(struct nvc0_fence
*fence
)
157 struct nvc0_screen
*screen
= fence
->screen
;
159 if (fence
->state
== NVC0_FENCE_STATE_EMITTED
)
160 nvc0_screen_fence_update(screen
);
162 return fence
->state
== NVC0_FENCE_STATE_SIGNALLED
;
166 nvc0_fence_wait(struct nvc0_fence
*fence
)
168 struct nvc0_screen
*screen
= fence
->screen
;
171 if (fence
->state
== NVC0_FENCE_STATE_AVAILABLE
) {
172 nvc0_fence_emit(fence
);
174 FIRE_RING(screen
->base
.channel
);
176 if (fence
== screen
->fence
.current
)
177 nvc0_screen_fence_new(screen
, &screen
->fence
.current
, FALSE
);
181 nvc0_screen_fence_update(screen
);
183 if (fence
->state
== NVC0_FENCE_STATE_SIGNALLED
)
187 if (!(spins
% 8)) /* donate a few cycles */
190 } while (spins
< NVC0_FENCE_MAX_SPINS
);
193 NOUVEAU_ERR("fence %x: been spinning too long\n", fence
->sequence
);
199 nvc0_screen_fence_next(struct nvc0_screen
*screen
)
201 nvc0_fence_emit(screen
->fence
.current
);
202 nvc0_screen_fence_new(screen
, &screen
->fence
.current
, FALSE
);
203 nvc0_screen_fence_update(screen
);