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.
27 #include <linux/ktime.h>
28 #include <linux/hrtimer.h>
29 #include <linux/sched/signal.h>
30 #include <trace/events/dma_fence.h>
32 #include <nvif/cl826e.h>
33 #include <nvif/notify.h>
34 #include <nvif/event.h>
36 #include "nouveau_drv.h"
37 #include "nouveau_dma.h"
38 #include "nouveau_fence.h"
40 static const struct dma_fence_ops nouveau_fence_ops_uevent;
41 static const struct dma_fence_ops nouveau_fence_ops_legacy;
43 static inline struct nouveau_fence *
44 from_fence(struct dma_fence *fence)
46 return container_of(fence, struct nouveau_fence, base);
49 static inline struct nouveau_fence_chan *
50 nouveau_fctx(struct nouveau_fence *fence)
52 return container_of(fence->base.lock, struct nouveau_fence_chan, lock);
56 nouveau_fence_signal(struct nouveau_fence *fence)
60 dma_fence_signal_locked(&fence->base);
61 list_del(&fence->head);
62 rcu_assign_pointer(fence->channel, NULL);
64 if (test_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags)) {
65 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
67 if (!--fctx->notify_ref)
71 dma_fence_put(&fence->base);
75 static struct nouveau_fence *
76 nouveau_local_fence(struct dma_fence *fence, struct nouveau_drm *drm)
78 if (fence->ops != &nouveau_fence_ops_legacy &&
79 fence->ops != &nouveau_fence_ops_uevent)
82 if (fence->context < drm->chan.context_base ||
83 fence->context >= drm->chan.context_base + drm->chan.nr)
86 return from_fence(fence);
90 nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
92 struct nouveau_fence *fence;
94 spin_lock_irq(&fctx->lock);
95 while (!list_empty(&fctx->pending)) {
96 fence = list_entry(fctx->pending.next, typeof(*fence), head);
98 if (nouveau_fence_signal(fence))
99 nvif_notify_put(&fctx->notify);
101 spin_unlock_irq(&fctx->lock);
103 nvif_notify_fini(&fctx->notify);
107 * Ensure that all accesses to fence->channel complete before freeing
114 nouveau_fence_context_put(struct kref *fence_ref)
116 kfree(container_of(fence_ref, struct nouveau_fence_chan, fence_ref));
120 nouveau_fence_context_free(struct nouveau_fence_chan *fctx)
122 kref_put(&fctx->fence_ref, nouveau_fence_context_put);
126 nouveau_fence_update(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx)
128 struct nouveau_fence *fence;
130 u32 seq = fctx->read(chan);
132 while (!list_empty(&fctx->pending)) {
133 fence = list_entry(fctx->pending.next, typeof(*fence), head);
135 if ((int)(seq - fence->base.seqno) < 0)
138 drop |= nouveau_fence_signal(fence);
145 nouveau_fence_wait_uevent_handler(struct nvif_notify *notify)
147 struct nouveau_fence_chan *fctx =
148 container_of(notify, typeof(*fctx), notify);
150 int ret = NVIF_NOTIFY_KEEP;
152 spin_lock_irqsave(&fctx->lock, flags);
153 if (!list_empty(&fctx->pending)) {
154 struct nouveau_fence *fence;
155 struct nouveau_channel *chan;
157 fence = list_entry(fctx->pending.next, typeof(*fence), head);
158 chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
159 if (nouveau_fence_update(fence->channel, fctx))
160 ret = NVIF_NOTIFY_DROP;
162 spin_unlock_irqrestore(&fctx->lock, flags);
168 nouveau_fence_context_new(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx)
170 struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
171 struct nouveau_cli *cli = (void *)chan->user.client;
174 INIT_LIST_HEAD(&fctx->flip);
175 INIT_LIST_HEAD(&fctx->pending);
176 spin_lock_init(&fctx->lock);
177 fctx->context = chan->drm->chan.context_base + chan->chid;
179 if (chan == chan->drm->cechan)
180 strcpy(fctx->name, "copy engine channel");
181 else if (chan == chan->drm->channel)
182 strcpy(fctx->name, "generic kernel channel");
184 strcpy(fctx->name, nvxx_client(&cli->base)->name);
186 kref_init(&fctx->fence_ref);
190 ret = nvif_notify_init(&chan->user, nouveau_fence_wait_uevent_handler,
191 false, NV826E_V0_NTFY_NON_STALL_INTERRUPT,
192 &(struct nvif_notify_uevent_req) { },
193 sizeof(struct nvif_notify_uevent_req),
194 sizeof(struct nvif_notify_uevent_rep),
201 nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
203 struct nouveau_fence_chan *fctx = chan->fence;
204 struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
207 fence->channel = chan;
208 fence->timeout = jiffies + (15 * HZ);
211 dma_fence_init(&fence->base, &nouveau_fence_ops_uevent,
212 &fctx->lock, fctx->context, ++fctx->sequence);
214 dma_fence_init(&fence->base, &nouveau_fence_ops_legacy,
215 &fctx->lock, fctx->context, ++fctx->sequence);
216 kref_get(&fctx->fence_ref);
218 trace_dma_fence_emit(&fence->base);
219 ret = fctx->emit(fence);
221 dma_fence_get(&fence->base);
222 spin_lock_irq(&fctx->lock);
224 if (nouveau_fence_update(chan, fctx))
225 nvif_notify_put(&fctx->notify);
227 list_add_tail(&fence->head, &fctx->pending);
228 spin_unlock_irq(&fctx->lock);
235 nouveau_fence_done(struct nouveau_fence *fence)
237 if (fence->base.ops == &nouveau_fence_ops_legacy ||
238 fence->base.ops == &nouveau_fence_ops_uevent) {
239 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
240 struct nouveau_channel *chan;
243 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
246 spin_lock_irqsave(&fctx->lock, flags);
247 chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
248 if (chan && nouveau_fence_update(chan, fctx))
249 nvif_notify_put(&fctx->notify);
250 spin_unlock_irqrestore(&fctx->lock, flags);
252 return dma_fence_is_signaled(&fence->base);
256 nouveau_fence_wait_legacy(struct dma_fence *f, bool intr, long wait)
258 struct nouveau_fence *fence = from_fence(f);
259 unsigned long sleep_time = NSEC_PER_MSEC / 1000;
260 unsigned long t = jiffies, timeout = t + wait;
262 while (!nouveau_fence_done(fence)) {
267 if (wait != MAX_SCHEDULE_TIMEOUT && time_after_eq(t, timeout)) {
268 __set_current_state(TASK_RUNNING);
272 __set_current_state(intr ? TASK_INTERRUPTIBLE :
273 TASK_UNINTERRUPTIBLE);
276 schedule_hrtimeout(&kt, HRTIMER_MODE_REL);
278 if (sleep_time > NSEC_PER_MSEC)
279 sleep_time = NSEC_PER_MSEC;
281 if (intr && signal_pending(current))
285 __set_current_state(TASK_RUNNING);
291 nouveau_fence_wait_busy(struct nouveau_fence *fence, bool intr)
295 while (!nouveau_fence_done(fence)) {
296 if (time_after_eq(jiffies, fence->timeout)) {
301 __set_current_state(intr ?
303 TASK_UNINTERRUPTIBLE);
305 if (intr && signal_pending(current)) {
311 __set_current_state(TASK_RUNNING);
316 nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
321 return nouveau_fence_wait_busy(fence, intr);
323 ret = dma_fence_wait_timeout(&fence->base, intr, 15 * HZ);
333 nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool exclusive, bool intr)
335 struct nouveau_fence_chan *fctx = chan->fence;
336 struct dma_fence *fence;
337 struct dma_resv *resv = nvbo->bo.base.resv;
338 struct dma_resv_list *fobj;
339 struct nouveau_fence *f;
343 ret = dma_resv_reserve_shared(resv, 1);
349 fobj = dma_resv_get_list(resv);
350 fence = dma_resv_get_excl(resv);
352 if (fence && (!exclusive || !fobj || !fobj->shared_count)) {
353 struct nouveau_channel *prev = NULL;
354 bool must_wait = true;
356 f = nouveau_local_fence(fence, chan->drm);
359 prev = rcu_dereference(f->channel);
360 if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0))
366 ret = dma_fence_wait(fence, intr);
371 if (!exclusive || !fobj)
374 for (i = 0; i < fobj->shared_count && !ret; ++i) {
375 struct nouveau_channel *prev = NULL;
376 bool must_wait = true;
378 fence = rcu_dereference_protected(fobj->shared[i],
379 dma_resv_held(resv));
381 f = nouveau_local_fence(fence, chan->drm);
384 prev = rcu_dereference(f->channel);
385 if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0))
391 ret = dma_fence_wait(fence, intr);
398 nouveau_fence_unref(struct nouveau_fence **pfence)
401 dma_fence_put(&(*pfence)->base);
406 nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
407 struct nouveau_fence **pfence)
409 struct nouveau_fence *fence;
412 if (unlikely(!chan->fence))
415 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
419 ret = nouveau_fence_emit(fence, chan);
421 nouveau_fence_unref(&fence);
427 static const char *nouveau_fence_get_get_driver_name(struct dma_fence *fence)
432 static const char *nouveau_fence_get_timeline_name(struct dma_fence *f)
434 struct nouveau_fence *fence = from_fence(f);
435 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
437 return !fctx->dead ? fctx->name : "dead channel";
441 * In an ideal world, read would not assume the channel context is still alive.
442 * This function may be called from another device, running into free memory as a
443 * result. The drm node should still be there, so we can derive the index from
446 static bool nouveau_fence_is_signaled(struct dma_fence *f)
448 struct nouveau_fence *fence = from_fence(f);
449 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
450 struct nouveau_channel *chan;
454 chan = rcu_dereference(fence->channel);
456 ret = (int)(fctx->read(chan) - fence->base.seqno) >= 0;
462 static bool nouveau_fence_no_signaling(struct dma_fence *f)
464 struct nouveau_fence *fence = from_fence(f);
467 * caller should have a reference on the fence,
468 * else fence could get freed here
470 WARN_ON(kref_read(&fence->base.refcount) <= 1);
473 * This needs uevents to work correctly, but dma_fence_add_callback relies on
474 * being able to enable signaling. It will still get signaled eventually,
475 * just not right away.
477 if (nouveau_fence_is_signaled(f)) {
478 list_del(&fence->head);
480 dma_fence_put(&fence->base);
487 static void nouveau_fence_release(struct dma_fence *f)
489 struct nouveau_fence *fence = from_fence(f);
490 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
492 kref_put(&fctx->fence_ref, nouveau_fence_context_put);
493 dma_fence_free(&fence->base);
496 static const struct dma_fence_ops nouveau_fence_ops_legacy = {
497 .get_driver_name = nouveau_fence_get_get_driver_name,
498 .get_timeline_name = nouveau_fence_get_timeline_name,
499 .enable_signaling = nouveau_fence_no_signaling,
500 .signaled = nouveau_fence_is_signaled,
501 .wait = nouveau_fence_wait_legacy,
502 .release = nouveau_fence_release
505 static bool nouveau_fence_enable_signaling(struct dma_fence *f)
507 struct nouveau_fence *fence = from_fence(f);
508 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
511 if (!fctx->notify_ref++)
512 nvif_notify_get(&fctx->notify);
514 ret = nouveau_fence_no_signaling(f);
516 set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
517 else if (!--fctx->notify_ref)
518 nvif_notify_put(&fctx->notify);
523 static const struct dma_fence_ops nouveau_fence_ops_uevent = {
524 .get_driver_name = nouveau_fence_get_get_driver_name,
525 .get_timeline_name = nouveau_fence_get_timeline_name,
526 .enable_signaling = nouveau_fence_enable_signaling,
527 .signaled = nouveau_fence_is_signaled,
528 .release = nouveau_fence_release