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 <stdio.h>
24#include <stdlib.h>
25#include <errno.h>
26#include <assert.h>
27
28#include "nouveau_private.h"
29#include "nouveau_dma.h"
30
31static void
32nouveau_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
53static void
54nouveau_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
78int
79nouveau_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
96int
97nouveau_fence_ref(struct nouveau_fence *ref, struct nouveau_fence **fence)
98{
99	if (!fence)
100		return -EINVAL;
101
102	if (ref)
103		nouveau_fence(ref)->refcount++;
104
105	if (*fence)
106		nouveau_fence_del(fence);
107
108	*fence = ref;
109	return 0;
110}
111
112int
113nouveau_fence_signal_cb(struct nouveau_fence *fence, void (*func)(void *),
114			void *priv)
115{
116	struct nouveau_fence_priv *nvfence = nouveau_fence(fence);
117	struct nouveau_fence_cb *cb;
118
119	if (!nvfence || !func)
120		return -EINVAL;
121
122	cb = malloc(sizeof(struct nouveau_fence_cb));
123	if (!cb)
124		return -ENOMEM;
125
126	cb->func = func;
127	cb->priv = priv;
128	cb->next = nvfence->signal_cb;
129	nvfence->signal_cb = cb;
130	return 0;
131}
132
133void
134nouveau_fence_emit(struct nouveau_fence *fence)
135{
136	struct nouveau_channel_priv *nvchan = nouveau_channel(fence->channel);
137	struct nouveau_fence_priv *nvfence = nouveau_fence(fence);
138
139	nvfence->emitted = 1;
140	nvfence->sequence = ++nvchan->fence_sequence;
141	if (nvfence->sequence == 0xffffffff)
142		printf("AII wrap unhandled\n");
143
144	if (!nvchan->fence_ntfy) {
145		/*XXX: assumes subc 0 is populated */
146		nouveau_dma_space(fence->channel, 2);
147		nouveau_dma_out  (fence->channel, 0x00040050);
148		nouveau_dma_out  (fence->channel, nvfence->sequence);
149	}
150	nouveau_dma_kickoff(fence->channel);
151
152	if (nvchan->fence_tail) {
153		nouveau_fence(nvchan->fence_tail)->next = fence;
154	} else {
155		nvchan->fence_head = fence;
156	}
157	nvchan->fence_tail = fence;
158}
159
160static void
161nouveau_fence_flush_seq(struct nouveau_channel *chan, uint32_t sequence)
162{
163	struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
164
165	while (nvchan->fence_head) {
166		struct nouveau_fence_priv *nvfence;
167
168		nvfence = nouveau_fence(nvchan->fence_head);
169		if (nvfence->sequence > sequence)
170			break;
171		nouveau_fence_del_unsignalled(&nvfence->base);
172		nvfence->signalled = 1;
173
174		if (nvfence->signal_cb) {
175			struct nouveau_fence *fence = NULL;
176
177			nouveau_fence_ref(&nvfence->base, &fence);
178
179			while (nvfence->signal_cb) {
180				struct nouveau_fence_cb *cb;
181
182				cb = nvfence->signal_cb;
183				nvfence->signal_cb = cb->next;
184				cb->func(cb->priv);
185				free(cb);
186			}
187
188			nouveau_fence_ref(NULL, &fence);
189		}
190	}
191}
192
193void
194nouveau_fence_flush(struct nouveau_channel *chan)
195{
196	struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
197
198	if (!nvchan->fence_ntfy)
199		nouveau_fence_flush_seq(chan, *nvchan->ref_cnt);
200}
201
202int
203nouveau_fence_wait(struct nouveau_fence **fence)
204{
205	struct nouveau_fence_priv *nvfence;
206	struct nouveau_channel_priv *nvchan;
207
208	if (!fence)
209		return -EINVAL;
210
211	nvfence = nouveau_fence(*fence);
212	if (!nvfence)
213		return 0;
214	nvchan = nouveau_channel(nvfence->base.channel);
215
216	if (nvfence->emitted) {
217		if (!nvfence->signalled && nvchan->fence_ntfy) {
218			struct nouveau_channel *chan = &nvchan->base;
219			int ret;
220
221			/*XXX: NV04/NV05: Full sync + flush all fences */
222			nouveau_notifier_reset(nvchan->fence_ntfy, 0);
223			BEGIN_RING(chan, nvchan->fence_grobj, 0x0104, 1);
224			OUT_RING  (chan, 0);
225			BEGIN_RING(chan, nvchan->fence_grobj, 0x0100, 1);
226			OUT_RING  (chan, 0);
227			FIRE_RING (chan);
228			ret = nouveau_notifier_wait_status(nvchan->fence_ntfy,
229							   0, 0, 2.0);
230			if (ret)
231				return ret;
232
233			nouveau_fence_flush_seq(chan, nvchan->fence_sequence);
234		}
235
236		while (!nvfence->signalled)
237			nouveau_fence_flush(nvfence->base.channel);
238	}
239
240	nouveau_fence_ref(NULL, fence);
241	return 0;
242}
243
244