2 * Copyright 2007 Nouveau Project
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
27 #include "nouveau_drmif.h"
28 #include "nouveau_dma.h"
29 #include "nouveau_local.h"
32 nouveau_fence_del_unsignalled(struct nouveau_fence
*fence
)
34 struct nouveau_channel_priv
*nvchan
= nouveau_channel(fence
->channel
);
35 struct nouveau_fence
*le
;
37 if (nvchan
->fence_head
== fence
) {
38 nvchan
->fence_head
= nouveau_fence(fence
)->next
;
39 if (nvchan
->fence_head
== NULL
)
40 nvchan
->fence_tail
= NULL
;
44 le
= nvchan
->fence_head
;
45 while (le
&& nouveau_fence(le
)->next
!= fence
)
46 le
= nouveau_fence(le
)->next
;
47 assert(le
&& nouveau_fence(le
)->next
== fence
);
48 nouveau_fence(le
)->next
= nouveau_fence(fence
)->next
;
49 if (nvchan
->fence_tail
== fence
)
50 nvchan
->fence_tail
= le
;
54 nouveau_fence_del(struct nouveau_fence
**fence
)
56 struct nouveau_fence_priv
*nvfence
;
58 if (!fence
|| !*fence
)
60 nvfence
= nouveau_fence(*fence
);
63 if (--nvfence
->refcount
)
66 if (nvfence
->emitted
&& !nvfence
->signalled
) {
67 if (nvfence
->signal_cb
) {
69 nouveau_fence_wait((void *)&nvfence
);
73 nouveau_fence_del_unsignalled(&nvfence
->base
);
79 nouveau_fence_new(struct nouveau_channel
*chan
, struct nouveau_fence
**fence
)
81 struct nouveau_fence_priv
*nvfence
;
83 if (!chan
|| !fence
|| *fence
)
86 nvfence
= calloc(1, sizeof(struct nouveau_fence_priv
));
89 nvfence
->base
.channel
= chan
;
90 nvfence
->refcount
= 1;
92 *fence
= &nvfence
->base
;
97 nouveau_fence_ref(struct nouveau_fence
*ref
, struct nouveau_fence
**fence
)
99 struct nouveau_fence_priv
*nvfence
;
105 nouveau_fence_del(fence
);
110 nvfence
= nouveau_fence(ref
);
112 *fence
= &nvfence
->base
;
119 nouveau_fence_signal_cb(struct nouveau_fence
*fence
, void (*func
)(void *),
122 struct nouveau_fence_priv
*nvfence
= nouveau_fence(fence
);
123 struct nouveau_fence_cb
*cb
;
125 if (!nvfence
|| !func
)
128 cb
= malloc(sizeof(struct nouveau_fence_cb
));
134 cb
->next
= nvfence
->signal_cb
;
135 nvfence
->signal_cb
= cb
;
140 nouveau_fence_emit(struct nouveau_fence
*fence
)
142 struct nouveau_channel_priv
*nvchan
= nouveau_channel(fence
->channel
);
143 struct nouveau_fence_priv
*nvfence
= nouveau_fence(fence
);
145 nvfence
->emitted
= 1;
146 nvfence
->sequence
= ++nvchan
->fence_sequence
;
147 if (nvfence
->sequence
== 0xffffffff)
148 NOUVEAU_ERR("AII wrap unhandled\n");
150 /*XXX: assumes subc 0 is populated */
151 RING_SPACE_CH(fence
->channel
, 2);
152 OUT_RING_CH (fence
->channel
, 0x00040050);
153 OUT_RING_CH (fence
->channel
, nvfence
->sequence
);
155 if (nvchan
->fence_tail
) {
156 nouveau_fence(nvchan
->fence_tail
)->next
= fence
;
158 nvchan
->fence_head
= fence
;
160 nvchan
->fence_tail
= fence
;
164 nouveau_fence_flush(struct nouveau_channel
*chan
)
166 struct nouveau_channel_priv
*nvchan
= nouveau_channel(chan
);
167 uint32_t sequence
= *nvchan
->ref_cnt
;
169 while (nvchan
->fence_head
) {
170 struct nouveau_fence_priv
*nvfence
;
172 nvfence
= nouveau_fence(nvchan
->fence_head
);
173 if (nvfence
->sequence
> sequence
)
176 nouveau_fence_del_unsignalled(&nvfence
->base
);
177 nvfence
->signalled
= 1;
179 if (nvfence
->signal_cb
) {
180 struct nouveau_fence
*fence
= NULL
;
182 nouveau_fence_ref(nvchan
->fence_head
, &fence
);
184 while (nvfence
->signal_cb
) {
185 struct nouveau_fence_cb
*cb
;
187 cb
= nvfence
->signal_cb
;
188 nvfence
->signal_cb
= cb
->next
;
193 nouveau_fence_ref(NULL
, &fence
);
199 nouveau_fence_wait(struct nouveau_fence
**fence
)
201 struct nouveau_fence_priv
*nvfence
;
203 if (!fence
|| !*fence
)
205 nvfence
= nouveau_fence(*fence
);
207 if (nvfence
->emitted
) {
208 while (!nvfence
->signalled
)
209 nouveau_fence_flush(nvfence
->base
.channel
);
211 nouveau_fence_ref(NULL
, fence
);