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/if0020.h>
34 #include "nouveau_drv.h"
35 #include "nouveau_dma.h"
36 #include "nouveau_fence.h"
38 static const struct dma_fence_ops nouveau_fence_ops_uevent;
39 static const struct dma_fence_ops nouveau_fence_ops_legacy;
41 static inline struct nouveau_fence *
42 from_fence(struct dma_fence *fence)
44 return container_of(fence, struct nouveau_fence, base);
47 static inline struct nouveau_fence_chan *
48 nouveau_fctx(struct nouveau_fence *fence)
50 return container_of(fence->base.lock, struct nouveau_fence_chan, lock);
54 nouveau_fence_signal(struct nouveau_fence *fence)
58 dma_fence_signal_locked(&fence->base);
59 list_del(&fence->head);
60 rcu_assign_pointer(fence->channel, NULL);
62 if (test_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags)) {
63 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
65 if (!--fctx->notify_ref)
69 dma_fence_put(&fence->base);
73 static struct nouveau_fence *
74 nouveau_local_fence(struct dma_fence *fence, struct nouveau_drm *drm)
76 if (fence->ops != &nouveau_fence_ops_legacy &&
77 fence->ops != &nouveau_fence_ops_uevent)
80 return from_fence(fence);
84 nouveau_fence_context_kill(struct nouveau_fence_chan *fctx, int error)
86 struct nouveau_fence *fence;
89 spin_lock_irqsave(&fctx->lock, flags);
90 while (!list_empty(&fctx->pending)) {
91 fence = list_entry(fctx->pending.next, typeof(*fence), head);
94 dma_fence_set_error(&fence->base, error);
96 if (nouveau_fence_signal(fence))
97 nvif_event_block(&fctx->event);
100 spin_unlock_irqrestore(&fctx->lock, flags);
104 nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
106 cancel_work_sync(&fctx->uevent_work);
107 nouveau_fence_context_kill(fctx, 0);
108 nvif_event_dtor(&fctx->event);
112 * Ensure that all accesses to fence->channel complete before freeing
119 nouveau_fence_context_put(struct kref *fence_ref)
121 kfree(container_of(fence_ref, struct nouveau_fence_chan, fence_ref));
125 nouveau_fence_context_free(struct nouveau_fence_chan *fctx)
127 kref_put(&fctx->fence_ref, nouveau_fence_context_put);
131 nouveau_fence_update(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx)
133 struct nouveau_fence *fence;
135 u32 seq = fctx->read(chan);
137 while (!list_empty(&fctx->pending)) {
138 fence = list_entry(fctx->pending.next, typeof(*fence), head);
140 if ((int)(seq - fence->base.seqno) < 0)
143 drop |= nouveau_fence_signal(fence);
150 nouveau_fence_uevent_work(struct work_struct *work)
152 struct nouveau_fence_chan *fctx = container_of(work, struct nouveau_fence_chan,
157 spin_lock_irqsave(&fctx->lock, flags);
158 if (!list_empty(&fctx->pending)) {
159 struct nouveau_fence *fence;
160 struct nouveau_channel *chan;
162 fence = list_entry(fctx->pending.next, typeof(*fence), head);
163 chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
164 if (nouveau_fence_update(chan, fctx))
168 nvif_event_block(&fctx->event);
170 spin_unlock_irqrestore(&fctx->lock, flags);
174 nouveau_fence_wait_uevent_handler(struct nvif_event *event, void *repv, u32 repc)
176 struct nouveau_fence_chan *fctx = container_of(event, typeof(*fctx), event);
177 schedule_work(&fctx->uevent_work);
178 return NVIF_EVENT_KEEP;
182 nouveau_fence_context_new(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx)
184 struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
185 struct nouveau_cli *cli = (void *)chan->user.client;
187 struct nvif_event_v0 base;
188 struct nvif_chan_event_v0 host;
192 INIT_WORK(&fctx->uevent_work, nouveau_fence_uevent_work);
193 INIT_LIST_HEAD(&fctx->flip);
194 INIT_LIST_HEAD(&fctx->pending);
195 spin_lock_init(&fctx->lock);
196 fctx->context = chan->drm->runl[chan->runlist].context_base + chan->chid;
198 if (chan == chan->drm->cechan)
199 strcpy(fctx->name, "copy engine channel");
200 else if (chan == chan->drm->channel)
201 strcpy(fctx->name, "generic kernel channel");
203 strcpy(fctx->name, nvxx_client(&cli->base)->name);
205 kref_init(&fctx->fence_ref);
209 args.host.version = 0;
210 args.host.type = NVIF_CHAN_EVENT_V0_NON_STALL_INTR;
212 ret = nvif_event_ctor(&chan->user, "fenceNonStallIntr", (chan->runlist << 16) | chan->chid,
213 nouveau_fence_wait_uevent_handler, false,
214 &args.base, sizeof(args), &fctx->event);
220 nouveau_fence_emit(struct nouveau_fence *fence)
222 struct nouveau_channel *chan = unrcu_pointer(fence->channel);
223 struct nouveau_fence_chan *fctx = chan->fence;
224 struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
227 fence->timeout = jiffies + (15 * HZ);
230 dma_fence_init(&fence->base, &nouveau_fence_ops_uevent,
231 &fctx->lock, fctx->context, ++fctx->sequence);
233 dma_fence_init(&fence->base, &nouveau_fence_ops_legacy,
234 &fctx->lock, fctx->context, ++fctx->sequence);
235 kref_get(&fctx->fence_ref);
237 ret = fctx->emit(fence);
239 dma_fence_get(&fence->base);
240 spin_lock_irq(&fctx->lock);
242 if (unlikely(fctx->killed)) {
243 spin_unlock_irq(&fctx->lock);
244 dma_fence_put(&fence->base);
248 if (nouveau_fence_update(chan, fctx))
249 nvif_event_block(&fctx->event);
251 list_add_tail(&fence->head, &fctx->pending);
252 spin_unlock_irq(&fctx->lock);
259 nouveau_fence_done(struct nouveau_fence *fence)
261 if (fence->base.ops == &nouveau_fence_ops_legacy ||
262 fence->base.ops == &nouveau_fence_ops_uevent) {
263 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
264 struct nouveau_channel *chan;
267 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
270 spin_lock_irqsave(&fctx->lock, flags);
271 chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
272 if (chan && nouveau_fence_update(chan, fctx))
273 nvif_event_block(&fctx->event);
274 spin_unlock_irqrestore(&fctx->lock, flags);
276 return dma_fence_is_signaled(&fence->base);
280 nouveau_fence_wait_legacy(struct dma_fence *f, bool intr, long wait)
282 struct nouveau_fence *fence = from_fence(f);
283 unsigned long sleep_time = NSEC_PER_MSEC / 1000;
284 unsigned long t = jiffies, timeout = t + wait;
286 while (!nouveau_fence_done(fence)) {
291 if (wait != MAX_SCHEDULE_TIMEOUT && time_after_eq(t, timeout)) {
292 __set_current_state(TASK_RUNNING);
296 __set_current_state(intr ? TASK_INTERRUPTIBLE :
297 TASK_UNINTERRUPTIBLE);
300 schedule_hrtimeout(&kt, HRTIMER_MODE_REL);
302 if (sleep_time > NSEC_PER_MSEC)
303 sleep_time = NSEC_PER_MSEC;
305 if (intr && signal_pending(current))
309 __set_current_state(TASK_RUNNING);
315 nouveau_fence_wait_busy(struct nouveau_fence *fence, bool intr)
319 while (!nouveau_fence_done(fence)) {
320 if (time_after_eq(jiffies, fence->timeout)) {
325 __set_current_state(intr ?
327 TASK_UNINTERRUPTIBLE);
329 if (intr && signal_pending(current)) {
335 __set_current_state(TASK_RUNNING);
340 nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
345 return nouveau_fence_wait_busy(fence, intr);
347 ret = dma_fence_wait_timeout(&fence->base, intr, 15 * HZ);
357 nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan,
358 bool exclusive, bool intr)
360 struct nouveau_fence_chan *fctx = chan->fence;
361 struct dma_resv *resv = nvbo->bo.base.resv;
364 ret = dma_resv_reserve_fences(resv, 1);
368 /* Waiting for the writes first causes performance regressions
369 * under some circumstances. So manually wait for the reads first.
371 for (i = 0; i < 2; ++i) {
372 struct dma_resv_iter cursor;
373 struct dma_fence *fence;
375 dma_resv_for_each_fence(&cursor, resv,
376 dma_resv_usage_rw(exclusive),
378 enum dma_resv_usage usage;
379 struct nouveau_fence *f;
381 usage = dma_resv_iter_usage(&cursor);
382 if (i == 0 && usage == DMA_RESV_USAGE_WRITE)
385 f = nouveau_local_fence(fence, chan->drm);
387 struct nouveau_channel *prev;
388 bool must_wait = true;
391 prev = rcu_dereference(f->channel);
392 if (prev && (prev == chan ||
393 fctx->sync(f, prev, chan) == 0))
400 ret = dma_fence_wait(fence, intr);
410 nouveau_fence_unref(struct nouveau_fence **pfence)
413 dma_fence_put(&(*pfence)->base);
418 nouveau_fence_create(struct nouveau_fence **pfence,
419 struct nouveau_channel *chan)
421 struct nouveau_fence *fence;
423 if (unlikely(!chan->fence))
426 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
430 fence->channel = chan;
437 nouveau_fence_new(struct nouveau_fence **pfence,
438 struct nouveau_channel *chan)
442 ret = nouveau_fence_create(pfence, chan);
446 ret = nouveau_fence_emit(*pfence);
448 nouveau_fence_unref(pfence);
453 static const char *nouveau_fence_get_get_driver_name(struct dma_fence *fence)
458 static const char *nouveau_fence_get_timeline_name(struct dma_fence *f)
460 struct nouveau_fence *fence = from_fence(f);
461 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
463 return !fctx->dead ? fctx->name : "dead channel";
467 * In an ideal world, read would not assume the channel context is still alive.
468 * This function may be called from another device, running into free memory as a
469 * result. The drm node should still be there, so we can derive the index from
472 static bool nouveau_fence_is_signaled(struct dma_fence *f)
474 struct nouveau_fence *fence = from_fence(f);
475 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
476 struct nouveau_channel *chan;
480 chan = rcu_dereference(fence->channel);
482 ret = (int)(fctx->read(chan) - fence->base.seqno) >= 0;
488 static bool nouveau_fence_no_signaling(struct dma_fence *f)
490 struct nouveau_fence *fence = from_fence(f);
493 * caller should have a reference on the fence,
494 * else fence could get freed here
496 WARN_ON(kref_read(&fence->base.refcount) <= 1);
499 * This needs uevents to work correctly, but dma_fence_add_callback relies on
500 * being able to enable signaling. It will still get signaled eventually,
501 * just not right away.
503 if (nouveau_fence_is_signaled(f)) {
504 list_del(&fence->head);
506 dma_fence_put(&fence->base);
513 static void nouveau_fence_release(struct dma_fence *f)
515 struct nouveau_fence *fence = from_fence(f);
516 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
518 kref_put(&fctx->fence_ref, nouveau_fence_context_put);
519 dma_fence_free(&fence->base);
522 static const struct dma_fence_ops nouveau_fence_ops_legacy = {
523 .get_driver_name = nouveau_fence_get_get_driver_name,
524 .get_timeline_name = nouveau_fence_get_timeline_name,
525 .enable_signaling = nouveau_fence_no_signaling,
526 .signaled = nouveau_fence_is_signaled,
527 .wait = nouveau_fence_wait_legacy,
528 .release = nouveau_fence_release
531 static bool nouveau_fence_enable_signaling(struct dma_fence *f)
533 struct nouveau_fence *fence = from_fence(f);
534 struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
537 if (!fctx->notify_ref++)
538 nvif_event_allow(&fctx->event);
540 ret = nouveau_fence_no_signaling(f);
542 set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
543 else if (!--fctx->notify_ref)
544 nvif_event_block(&fctx->event);
549 static const struct dma_fence_ops nouveau_fence_ops_uevent = {
550 .get_driver_name = nouveau_fence_get_get_driver_name,
551 .get_timeline_name = nouveau_fence_get_timeline_name,
552 .enable_signaling = nouveau_fence_enable_signaling,
553 .signaled = nouveau_fence_is_signaled,
554 .release = nouveau_fence_release