2 * Copyright (C) 2007 Ben Skeggs.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <linux/ktime.h>
30 #include <linux/hrtimer.h>
32 #include "nouveau_drm.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_fence.h"
36 #include <engine/fifo.h>
39 nouveau_fence_context_del(struct nouveau_fence_chan
*fctx
)
41 struct nouveau_fence
*fence
, *fnext
;
42 spin_lock(&fctx
->lock
);
43 list_for_each_entry_safe(fence
, fnext
, &fctx
->pending
, head
) {
44 fence
->channel
= NULL
;
45 list_del(&fence
->head
);
46 nouveau_fence_unref(&fence
);
48 spin_unlock(&fctx
->lock
);
52 nouveau_fence_context_new(struct nouveau_fence_chan
*fctx
)
54 INIT_LIST_HEAD(&fctx
->flip
);
55 INIT_LIST_HEAD(&fctx
->pending
);
56 spin_lock_init(&fctx
->lock
);
60 nouveau_fence_update(struct nouveau_channel
*chan
)
62 struct nouveau_fence_chan
*fctx
= chan
->fence
;
63 struct nouveau_fence
*fence
, *fnext
;
65 spin_lock(&fctx
->lock
);
66 list_for_each_entry_safe(fence
, fnext
, &fctx
->pending
, head
) {
67 if (fctx
->read(chan
) < fence
->sequence
)
70 fence
->channel
= NULL
;
71 list_del(&fence
->head
);
72 nouveau_fence_unref(&fence
);
74 spin_unlock(&fctx
->lock
);
78 nouveau_fence_emit(struct nouveau_fence
*fence
, struct nouveau_channel
*chan
)
80 struct nouveau_fence_chan
*fctx
= chan
->fence
;
83 fence
->channel
= chan
;
84 fence
->timeout
= jiffies
+ (3 * DRM_HZ
);
85 fence
->sequence
= ++fctx
->sequence
;
87 ret
= fctx
->emit(fence
);
89 kref_get(&fence
->kref
);
90 spin_lock(&fctx
->lock
);
91 list_add_tail(&fence
->head
, &fctx
->pending
);
92 spin_unlock(&fctx
->lock
);
99 nouveau_fence_done(struct nouveau_fence
*fence
)
102 nouveau_fence_update(fence
->channel
);
103 return !fence
->channel
;
106 struct nouveau_fence_uevent
{
107 struct nouveau_eventh handler
;
108 struct nouveau_fence_priv
*priv
;
112 nouveau_fence_wait_uevent_handler(struct nouveau_eventh
*event
, int index
)
114 struct nouveau_fence_uevent
*uevent
=
115 container_of(event
, struct nouveau_fence_uevent
, handler
);
116 wake_up_all(&uevent
->priv
->waiting
);
117 return NVKM_EVENT_KEEP
;
121 nouveau_fence_wait_uevent(struct nouveau_fence
*fence
, bool intr
)
124 struct nouveau_channel
*chan
= fence
->channel
;
125 struct nouveau_fifo
*pfifo
= nouveau_fifo(chan
->drm
->device
);
126 struct nouveau_fence_priv
*priv
= chan
->drm
->fence
;
127 struct nouveau_fence_uevent uevent
= {
128 .handler
.func
= nouveau_fence_wait_uevent_handler
,
133 nouveau_event_get(pfifo
->uevent
, 0, &uevent
.handler
);
135 if (fence
->timeout
) {
136 unsigned long timeout
= fence
->timeout
- jiffies
;
138 if (time_before(jiffies
, fence
->timeout
)) {
140 ret
= wait_event_interruptible_timeout(
142 nouveau_fence_done(fence
),
145 ret
= wait_event_timeout(priv
->waiting
,
146 nouveau_fence_done(fence
),
152 fence
->timeout
= jiffies
+ ret
;
153 if (time_after_eq(jiffies
, fence
->timeout
))
158 ret
= wait_event_interruptible(priv
->waiting
,
159 nouveau_fence_done(fence
));
161 wait_event(priv
->waiting
, nouveau_fence_done(fence
));
165 nouveau_event_put(pfifo
->uevent
, 0, &uevent
.handler
);
166 if (unlikely(ret
< 0))
173 nouveau_fence_wait(struct nouveau_fence
*fence
, bool lazy
, bool intr
)
175 struct nouveau_channel
*chan
= fence
->channel
;
176 struct nouveau_fence_priv
*priv
= chan
? chan
->drm
->fence
: NULL
;
177 unsigned long sleep_time
= NSEC_PER_MSEC
/ 1000;
181 while (priv
&& priv
->uevent
&& lazy
&& !nouveau_fence_done(fence
)) {
182 ret
= nouveau_fence_wait_uevent(fence
, intr
);
187 while (!nouveau_fence_done(fence
)) {
188 if (fence
->timeout
&& time_after_eq(jiffies
, fence
->timeout
)) {
193 __set_current_state(intr
? TASK_INTERRUPTIBLE
:
194 TASK_UNINTERRUPTIBLE
);
196 t
= ktime_set(0, sleep_time
);
197 schedule_hrtimeout(&t
, HRTIMER_MODE_REL
);
199 if (sleep_time
> NSEC_PER_MSEC
)
200 sleep_time
= NSEC_PER_MSEC
;
203 if (intr
&& signal_pending(current
)) {
209 __set_current_state(TASK_RUNNING
);
214 nouveau_fence_sync(struct nouveau_fence
*fence
, struct nouveau_channel
*chan
)
216 struct nouveau_fence_chan
*fctx
= chan
->fence
;
217 struct nouveau_channel
*prev
;
220 prev
= fence
? fence
->channel
: NULL
;
222 if (unlikely(prev
!= chan
&& !nouveau_fence_done(fence
))) {
223 ret
= fctx
->sync(fence
, prev
, chan
);
225 ret
= nouveau_fence_wait(fence
, true, false);
233 nouveau_fence_del(struct kref
*kref
)
235 struct nouveau_fence
*fence
= container_of(kref
, typeof(*fence
), kref
);
240 nouveau_fence_unref(struct nouveau_fence
**pfence
)
243 kref_put(&(*pfence
)->kref
, nouveau_fence_del
);
247 struct nouveau_fence
*
248 nouveau_fence_ref(struct nouveau_fence
*fence
)
250 kref_get(&fence
->kref
);
255 nouveau_fence_new(struct nouveau_channel
*chan
, bool sysmem
,
256 struct nouveau_fence
**pfence
)
258 struct nouveau_fence
*fence
;
261 if (unlikely(!chan
->fence
))
264 fence
= kzalloc(sizeof(*fence
), GFP_KERNEL
);
268 fence
->sysmem
= sysmem
;
269 kref_init(&fence
->kref
);
271 ret
= nouveau_fence_emit(fence
, chan
);
273 nouveau_fence_unref(&fence
);