Merge branch 'upstream-gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / mesa / drivers / dri / nouveau_winsys / nouveau_fence.c
1 /*
2 * Copyright 2007 Nouveau Project
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 */
22
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <assert.h>
26
27 #include "nouveau_drmif.h"
28 #include "nouveau_dma.h"
29 #include "nouveau_local.h"
30
31 static void
32 nouveau_fence_del_unsignalled(struct nouveau_fence *fence)
33 {
34 struct nouveau_channel_priv *nvchan = nouveau_channel(fence->channel);
35 struct nouveau_fence *le;
36
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;
41 return;
42 }
43
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;
51 }
52
53 static void
54 nouveau_fence_del(struct nouveau_fence **fence)
55 {
56 struct nouveau_fence_priv *nvfence;
57
58 if (!fence || !*fence)
59 return;
60 nvfence = nouveau_fence(*fence);
61 *fence = NULL;
62
63 if (--nvfence->refcount)
64 return;
65
66 if (nvfence->emitted && !nvfence->signalled) {
67 if (nvfence->signal_cb) {
68 nvfence->refcount++;
69 nouveau_fence_wait((void *)&nvfence);
70 return;
71 }
72
73 nouveau_fence_del_unsignalled(&nvfence->base);
74 }
75 free(nvfence);
76 }
77
78 int
79 nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **fence)
80 {
81 struct nouveau_fence_priv *nvfence;
82
83 if (!chan || !fence || *fence)
84 return -EINVAL;
85
86 nvfence = calloc(1, sizeof(struct nouveau_fence_priv));
87 if (!nvfence)
88 return -ENOMEM;
89 nvfence->base.channel = chan;
90 nvfence->refcount = 1;
91
92 *fence = &nvfence->base;
93 return 0;
94 }
95
96 int
97 nouveau_fence_ref(struct nouveau_fence *ref, struct nouveau_fence **fence)
98 {
99 struct nouveau_fence_priv *nvfence;
100
101 if (!fence)
102 return -EINVAL;
103
104 if (*fence) {
105 nouveau_fence_del(fence);
106 *fence = NULL;
107 }
108
109 if (ref) {
110 nvfence = nouveau_fence(ref);
111 nvfence->refcount++;
112 *fence = &nvfence->base;
113 }
114
115 return 0;
116 }
117
118 int
119 nouveau_fence_signal_cb(struct nouveau_fence *fence, void (*func)(void *),
120 void *priv)
121 {
122 struct nouveau_fence_priv *nvfence = nouveau_fence(fence);
123 struct nouveau_fence_cb *cb;
124
125 if (!nvfence || !func)
126 return -EINVAL;
127
128 cb = malloc(sizeof(struct nouveau_fence_cb));
129 if (!cb)
130 return -ENOMEM;
131
132 cb->func = func;
133 cb->priv = priv;
134 cb->next = nvfence->signal_cb;
135 nvfence->signal_cb = cb;
136 return 0;
137 }
138
139 void
140 nouveau_fence_emit(struct nouveau_fence *fence)
141 {
142 struct nouveau_channel_priv *nvchan = nouveau_channel(fence->channel);
143 struct nouveau_fence_priv *nvfence = nouveau_fence(fence);
144
145 nvfence->emitted = 1;
146 nvfence->sequence = ++nvchan->fence_sequence;
147 if (nvfence->sequence == 0xffffffff)
148 NOUVEAU_ERR("AII wrap unhandled\n");
149
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);
154
155 if (nvchan->fence_tail) {
156 nouveau_fence(nvchan->fence_tail)->next = fence;
157 } else {
158 nvchan->fence_head = fence;
159 }
160 nvchan->fence_tail = fence;
161 }
162
163 void
164 nouveau_fence_flush(struct nouveau_channel *chan)
165 {
166 struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
167 uint32_t sequence = *nvchan->ref_cnt;
168
169 while (nvchan->fence_head) {
170 struct nouveau_fence_priv *nvfence;
171
172 nvfence = nouveau_fence(nvchan->fence_head);
173 if (nvfence->sequence > sequence)
174 break;
175
176 nouveau_fence_del_unsignalled(&nvfence->base);
177 nvfence->signalled = 1;
178
179 if (nvfence->signal_cb) {
180 struct nouveau_fence *fence = NULL;
181
182 nouveau_fence_ref(nvchan->fence_head, &fence);
183
184 while (nvfence->signal_cb) {
185 struct nouveau_fence_cb *cb;
186
187 cb = nvfence->signal_cb;
188 nvfence->signal_cb = cb->next;
189 cb->func(cb->priv);
190 free(cb);
191 }
192
193 nouveau_fence_ref(NULL, &fence);
194 }
195 }
196 }
197
198 int
199 nouveau_fence_wait(struct nouveau_fence **fence)
200 {
201 struct nouveau_fence_priv *nvfence;
202
203 if (!fence || !*fence)
204 return -EINVAL;
205 nvfence = nouveau_fence(*fence);
206
207 if (nvfence->emitted) {
208 while (!nvfence->signalled)
209 nouveau_fence_flush(nvfence->base.channel);
210 }
211 nouveau_fence_ref(NULL, fence);
212
213 return 0;
214 }
215