]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/i915/intel_breadcrumbs.c
Merge remote-tracking branch 'airlied/drm-next' into drm-intel-next-queued
[karo-tx-linux.git] / drivers / gpu / drm / i915 / intel_breadcrumbs.c
1 /*
2  * Copyright © 2015 Intel Corporation
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #include <linux/kthread.h>
26 #include <uapi/linux/sched/types.h>
27
28 #include "i915_drv.h"
29
30 static unsigned int __intel_breadcrumbs_wakeup(struct intel_breadcrumbs *b)
31 {
32         struct intel_wait *wait;
33         unsigned int result = 0;
34
35         lockdep_assert_held(&b->irq_lock);
36
37         wait = b->irq_wait;
38         if (wait) {
39                 result = ENGINE_WAKEUP_WAITER;
40                 if (wake_up_process(wait->tsk))
41                         result |= ENGINE_WAKEUP_ASLEEP;
42         }
43
44         return result;
45 }
46
47 unsigned int intel_engine_wakeup(struct intel_engine_cs *engine)
48 {
49         struct intel_breadcrumbs *b = &engine->breadcrumbs;
50         unsigned int result;
51
52         spin_lock_irq(&b->irq_lock);
53         result = __intel_breadcrumbs_wakeup(b);
54         spin_unlock_irq(&b->irq_lock);
55
56         return result;
57 }
58
59 static unsigned long wait_timeout(void)
60 {
61         return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
62 }
63
64 static noinline void missed_breadcrumb(struct intel_engine_cs *engine)
65 {
66         DRM_DEBUG_DRIVER("%s missed breadcrumb at %pF, irq posted? %s\n",
67                          engine->name, __builtin_return_address(0),
68                          yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
69                                         &engine->irq_posted)));
70
71         set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
72 }
73
74 static void intel_breadcrumbs_hangcheck(unsigned long data)
75 {
76         struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
77         struct intel_breadcrumbs *b = &engine->breadcrumbs;
78
79         if (!b->irq_armed)
80                 return;
81
82         if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) {
83                 b->hangcheck_interrupts = atomic_read(&engine->irq_count);
84                 mod_timer(&b->hangcheck, wait_timeout());
85                 return;
86         }
87
88         /* We keep the hangcheck time alive until we disarm the irq, even
89          * if there are no waiters at present.
90          *
91          * If the waiter was currently running, assume it hasn't had a chance
92          * to process the pending interrupt (e.g, low priority task on a loaded
93          * system) and wait until it sleeps before declaring a missed interrupt.
94          *
95          * If the waiter was asleep (and not even pending a wakeup), then we
96          * must have missed an interrupt as the GPU has stopped advancing
97          * but we still have a waiter. Assuming all batches complete within
98          * DRM_I915_HANGCHECK_JIFFIES [1.5s]!
99          */
100         if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) {
101                 missed_breadcrumb(engine);
102                 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
103         } else {
104                 mod_timer(&b->hangcheck, wait_timeout());
105         }
106 }
107
108 static void intel_breadcrumbs_fake_irq(unsigned long data)
109 {
110         struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
111         struct intel_breadcrumbs *b = &engine->breadcrumbs;
112
113         /*
114          * The timer persists in case we cannot enable interrupts,
115          * or if we have previously seen seqno/interrupt incoherency
116          * ("missed interrupt" syndrome). Here the worker will wake up
117          * every jiffie in order to kick the oldest waiter to do the
118          * coherent seqno check.
119          */
120
121         spin_lock_irq(&b->irq_lock);
122         if (!__intel_breadcrumbs_wakeup(b))
123                 __intel_engine_disarm_breadcrumbs(engine);
124         spin_unlock_irq(&b->irq_lock);
125         if (!b->irq_armed)
126                 return;
127
128         mod_timer(&b->fake_irq, jiffies + 1);
129
130         /* Ensure that even if the GPU hangs, we get woken up.
131          *
132          * However, note that if no one is waiting, we never notice
133          * a gpu hang. Eventually, we will have to wait for a resource
134          * held by the GPU and so trigger a hangcheck. In the most
135          * pathological case, this will be upon memory starvation! To
136          * prevent this, we also queue the hangcheck from the retire
137          * worker.
138          */
139         i915_queue_hangcheck(engine->i915);
140 }
141
142 static void irq_enable(struct intel_engine_cs *engine)
143 {
144         /* Enabling the IRQ may miss the generation of the interrupt, but
145          * we still need to force the barrier before reading the seqno,
146          * just in case.
147          */
148         set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
149
150         /* Caller disables interrupts */
151         spin_lock(&engine->i915->irq_lock);
152         engine->irq_enable(engine);
153         spin_unlock(&engine->i915->irq_lock);
154 }
155
156 static void irq_disable(struct intel_engine_cs *engine)
157 {
158         /* Caller disables interrupts */
159         spin_lock(&engine->i915->irq_lock);
160         engine->irq_disable(engine);
161         spin_unlock(&engine->i915->irq_lock);
162 }
163
164 void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
165 {
166         struct intel_breadcrumbs *b = &engine->breadcrumbs;
167
168         lockdep_assert_held(&b->irq_lock);
169         GEM_BUG_ON(b->irq_wait);
170
171         if (b->irq_enabled) {
172                 irq_disable(engine);
173                 b->irq_enabled = false;
174         }
175
176         b->irq_armed = false;
177 }
178
179 void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
180 {
181         struct intel_breadcrumbs *b = &engine->breadcrumbs;
182         struct intel_wait *wait, *n;
183
184         if (!b->irq_armed)
185                 return;
186
187         /* We only disarm the irq when we are idle (all requests completed),
188          * so if the bottom-half remains asleep, it missed the request
189          * completion.
190          */
191
192         spin_lock_irq(&b->rb_lock);
193         rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
194                 RB_CLEAR_NODE(&wait->node);
195                 if (wake_up_process(wait->tsk) && wait == b->irq_wait)
196                         missed_breadcrumb(engine);
197         }
198         b->waiters = RB_ROOT;
199
200         spin_lock(&b->irq_lock);
201         b->irq_wait = NULL;
202         __intel_engine_disarm_breadcrumbs(engine);
203         spin_unlock(&b->irq_lock);
204
205         spin_unlock_irq(&b->rb_lock);
206 }
207
208 static bool use_fake_irq(const struct intel_breadcrumbs *b)
209 {
210         const struct intel_engine_cs *engine =
211                 container_of(b, struct intel_engine_cs, breadcrumbs);
212
213         if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
214                 return false;
215
216         /* Only start with the heavy weight fake irq timer if we have not
217          * seen any interrupts since enabling it the first time. If the
218          * interrupts are still arriving, it means we made a mistake in our
219          * engine->seqno_barrier(), a timing error that should be transient
220          * and unlikely to reoccur.
221          */
222         return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
223 }
224
225 static void enable_fake_irq(struct intel_breadcrumbs *b)
226 {
227         /* Ensure we never sleep indefinitely */
228         if (!b->irq_enabled || use_fake_irq(b))
229                 mod_timer(&b->fake_irq, jiffies + 1);
230         else
231                 mod_timer(&b->hangcheck, wait_timeout());
232 }
233
234 static void __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
235 {
236         struct intel_engine_cs *engine =
237                 container_of(b, struct intel_engine_cs, breadcrumbs);
238         struct drm_i915_private *i915 = engine->i915;
239
240         lockdep_assert_held(&b->irq_lock);
241         if (b->irq_armed)
242                 return;
243
244         /* The breadcrumb irq will be disarmed on the interrupt after the
245          * waiters are signaled. This gives us a single interrupt window in
246          * which we can add a new waiter and avoid the cost of re-enabling
247          * the irq.
248          */
249         b->irq_armed = true;
250         GEM_BUG_ON(b->irq_enabled);
251
252         if (I915_SELFTEST_ONLY(b->mock)) {
253                 /* For our mock objects we want to avoid interaction
254                  * with the real hardware (which is not set up). So
255                  * we simply pretend we have enabled the powerwell
256                  * and the irq, and leave it up to the mock
257                  * implementation to call intel_engine_wakeup()
258                  * itself when it wants to simulate a user interrupt,
259                  */
260                 return;
261         }
262
263         /* Since we are waiting on a request, the GPU should be busy
264          * and should have its own rpm reference. This is tracked
265          * by i915->gt.awake, we can forgo holding our own wakref
266          * for the interrupt as before i915->gt.awake is released (when
267          * the driver is idle) we disarm the breadcrumbs.
268          */
269
270         /* No interrupts? Kick the waiter every jiffie! */
271         if (intel_irqs_enabled(i915)) {
272                 if (!test_bit(engine->id, &i915->gpu_error.test_irq_rings))
273                         irq_enable(engine);
274                 b->irq_enabled = true;
275         }
276
277         enable_fake_irq(b);
278 }
279
280 static inline struct intel_wait *to_wait(struct rb_node *node)
281 {
282         return rb_entry(node, struct intel_wait, node);
283 }
284
285 static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
286                                               struct intel_wait *wait)
287 {
288         lockdep_assert_held(&b->rb_lock);
289
290         /* This request is completed, so remove it from the tree, mark it as
291          * complete, and *then* wake up the associated task.
292          */
293         rb_erase(&wait->node, &b->waiters);
294         RB_CLEAR_NODE(&wait->node);
295
296         wake_up_process(wait->tsk); /* implicit smp_wmb() */
297 }
298
299 static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
300                                             struct rb_node *next)
301 {
302         struct intel_breadcrumbs *b = &engine->breadcrumbs;
303
304         spin_lock(&b->irq_lock);
305         GEM_BUG_ON(!b->irq_armed);
306         b->irq_wait = to_wait(next);
307         spin_unlock(&b->irq_lock);
308
309         /* We always wake up the next waiter that takes over as the bottom-half
310          * as we may delegate not only the irq-seqno barrier to the next waiter
311          * but also the task of waking up concurrent waiters.
312          */
313         if (next)
314                 wake_up_process(to_wait(next)->tsk);
315 }
316
317 static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
318                                     struct intel_wait *wait)
319 {
320         struct intel_breadcrumbs *b = &engine->breadcrumbs;
321         struct rb_node **p, *parent, *completed;
322         bool first;
323         u32 seqno;
324
325         /* Insert the request into the retirement ordered list
326          * of waiters by walking the rbtree. If we are the oldest
327          * seqno in the tree (the first to be retired), then
328          * set ourselves as the bottom-half.
329          *
330          * As we descend the tree, prune completed branches since we hold the
331          * spinlock we know that the first_waiter must be delayed and can
332          * reduce some of the sequential wake up latency if we take action
333          * ourselves and wake up the completed tasks in parallel. Also, by
334          * removing stale elements in the tree, we may be able to reduce the
335          * ping-pong between the old bottom-half and ourselves as first-waiter.
336          */
337         first = true;
338         parent = NULL;
339         completed = NULL;
340         seqno = intel_engine_get_seqno(engine);
341
342          /* If the request completed before we managed to grab the spinlock,
343           * return now before adding ourselves to the rbtree. We let the
344           * current bottom-half handle any pending wakeups and instead
345           * try and get out of the way quickly.
346           */
347         if (i915_seqno_passed(seqno, wait->seqno)) {
348                 RB_CLEAR_NODE(&wait->node);
349                 return first;
350         }
351
352         p = &b->waiters.rb_node;
353         while (*p) {
354                 parent = *p;
355                 if (wait->seqno == to_wait(parent)->seqno) {
356                         /* We have multiple waiters on the same seqno, select
357                          * the highest priority task (that with the smallest
358                          * task->prio) to serve as the bottom-half for this
359                          * group.
360                          */
361                         if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
362                                 p = &parent->rb_right;
363                                 first = false;
364                         } else {
365                                 p = &parent->rb_left;
366                         }
367                 } else if (i915_seqno_passed(wait->seqno,
368                                              to_wait(parent)->seqno)) {
369                         p = &parent->rb_right;
370                         if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
371                                 completed = parent;
372                         else
373                                 first = false;
374                 } else {
375                         p = &parent->rb_left;
376                 }
377         }
378         rb_link_node(&wait->node, parent, p);
379         rb_insert_color(&wait->node, &b->waiters);
380
381         if (completed) {
382                 struct rb_node *next = rb_next(completed);
383
384                 GEM_BUG_ON(!next && !first);
385                 if (next && next != &wait->node) {
386                         GEM_BUG_ON(first);
387                         __intel_breadcrumbs_next(engine, next);
388                 }
389
390                 do {
391                         struct intel_wait *crumb = to_wait(completed);
392                         completed = rb_prev(completed);
393                         __intel_breadcrumbs_finish(b, crumb);
394                 } while (completed);
395         }
396
397         if (first) {
398                 spin_lock(&b->irq_lock);
399                 GEM_BUG_ON(rb_first(&b->waiters) != &wait->node);
400                 b->irq_wait = wait;
401                 /* After assigning ourselves as the new bottom-half, we must
402                  * perform a cursory check to prevent a missed interrupt.
403                  * Either we miss the interrupt whilst programming the hardware,
404                  * or if there was a previous waiter (for a later seqno) they
405                  * may be woken instead of us (due to the inherent race
406                  * in the unlocked read of b->irq_seqno_bh in the irq handler)
407                  * and so we miss the wake up.
408                  */
409                 __intel_breadcrumbs_enable_irq(b);
410                 spin_unlock(&b->irq_lock);
411         }
412         GEM_BUG_ON(!b->irq_wait);
413         GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
414
415         return first;
416 }
417
418 bool intel_engine_add_wait(struct intel_engine_cs *engine,
419                            struct intel_wait *wait)
420 {
421         struct intel_breadcrumbs *b = &engine->breadcrumbs;
422         bool first;
423
424         spin_lock_irq(&b->rb_lock);
425         first = __intel_engine_add_wait(engine, wait);
426         spin_unlock_irq(&b->rb_lock);
427
428         return first;
429 }
430
431 static inline bool chain_wakeup(struct rb_node *rb, int priority)
432 {
433         return rb && to_wait(rb)->tsk->prio <= priority;
434 }
435
436 static inline int wakeup_priority(struct intel_breadcrumbs *b,
437                                   struct task_struct *tsk)
438 {
439         if (tsk == b->signaler)
440                 return INT_MIN;
441         else
442                 return tsk->prio;
443 }
444
445 static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
446                                        struct intel_wait *wait)
447 {
448         struct intel_breadcrumbs *b = &engine->breadcrumbs;
449
450         lockdep_assert_held(&b->rb_lock);
451
452         if (RB_EMPTY_NODE(&wait->node))
453                 goto out;
454
455         if (b->irq_wait == wait) {
456                 const int priority = wakeup_priority(b, wait->tsk);
457                 struct rb_node *next;
458
459                 /* We are the current bottom-half. Find the next candidate,
460                  * the first waiter in the queue on the remaining oldest
461                  * request. As multiple seqnos may complete in the time it
462                  * takes us to wake up and find the next waiter, we have to
463                  * wake up that waiter for it to perform its own coherent
464                  * completion check.
465                  */
466                 next = rb_next(&wait->node);
467                 if (chain_wakeup(next, priority)) {
468                         /* If the next waiter is already complete,
469                          * wake it up and continue onto the next waiter. So
470                          * if have a small herd, they will wake up in parallel
471                          * rather than sequentially, which should reduce
472                          * the overall latency in waking all the completed
473                          * clients.
474                          *
475                          * However, waking up a chain adds extra latency to
476                          * the first_waiter. This is undesirable if that
477                          * waiter is a high priority task.
478                          */
479                         u32 seqno = intel_engine_get_seqno(engine);
480
481                         while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
482                                 struct rb_node *n = rb_next(next);
483
484                                 __intel_breadcrumbs_finish(b, to_wait(next));
485                                 next = n;
486                                 if (!chain_wakeup(next, priority))
487                                         break;
488                         }
489                 }
490
491                 __intel_breadcrumbs_next(engine, next);
492         } else {
493                 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
494         }
495
496         GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
497         rb_erase(&wait->node, &b->waiters);
498
499 out:
500         GEM_BUG_ON(b->irq_wait == wait);
501         GEM_BUG_ON(rb_first(&b->waiters) !=
502                    (b->irq_wait ? &b->irq_wait->node : NULL));
503 }
504
505 void intel_engine_remove_wait(struct intel_engine_cs *engine,
506                               struct intel_wait *wait)
507 {
508         struct intel_breadcrumbs *b = &engine->breadcrumbs;
509
510         /* Quick check to see if this waiter was already decoupled from
511          * the tree by the bottom-half to avoid contention on the spinlock
512          * by the herd.
513          */
514         if (RB_EMPTY_NODE(&wait->node))
515                 return;
516
517         spin_lock_irq(&b->rb_lock);
518         __intel_engine_remove_wait(engine, wait);
519         spin_unlock_irq(&b->rb_lock);
520 }
521
522 static bool signal_valid(const struct drm_i915_gem_request *request)
523 {
524         return intel_wait_check_request(&request->signaling.wait, request);
525 }
526
527 static bool signal_complete(const struct drm_i915_gem_request *request)
528 {
529         if (!request)
530                 return false;
531
532         /* If another process served as the bottom-half it may have already
533          * signalled that this wait is already completed.
534          */
535         if (intel_wait_complete(&request->signaling.wait))
536                 return signal_valid(request);
537
538         /* Carefully check if the request is complete, giving time for the
539          * seqno to be visible or if the GPU hung.
540          */
541         if (__i915_request_irq_complete(request))
542                 return true;
543
544         return false;
545 }
546
547 static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
548 {
549         return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
550 }
551
552 static void signaler_set_rtpriority(void)
553 {
554          struct sched_param param = { .sched_priority = 1 };
555
556          sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
557 }
558
559 static int intel_breadcrumbs_signaler(void *arg)
560 {
561         struct intel_engine_cs *engine = arg;
562         struct intel_breadcrumbs *b = &engine->breadcrumbs;
563         struct drm_i915_gem_request *request;
564
565         /* Install ourselves with high priority to reduce signalling latency */
566         signaler_set_rtpriority();
567
568         do {
569                 set_current_state(TASK_INTERRUPTIBLE);
570
571                 /* We are either woken up by the interrupt bottom-half,
572                  * or by a client adding a new signaller. In both cases,
573                  * the GPU seqno may have advanced beyond our oldest signal.
574                  * If it has, propagate the signal, remove the waiter and
575                  * check again with the next oldest signal. Otherwise we
576                  * need to wait for a new interrupt from the GPU or for
577                  * a new client.
578                  */
579                 rcu_read_lock();
580                 request = rcu_dereference(b->first_signal);
581                 if (request)
582                         request = i915_gem_request_get_rcu(request);
583                 rcu_read_unlock();
584                 if (signal_complete(request)) {
585                         local_bh_disable();
586                         dma_fence_signal(&request->fence);
587                         local_bh_enable(); /* kick start the tasklets */
588
589                         spin_lock_irq(&b->rb_lock);
590
591                         /* Wake up all other completed waiters and select the
592                          * next bottom-half for the next user interrupt.
593                          */
594                         __intel_engine_remove_wait(engine,
595                                                    &request->signaling.wait);
596
597                         /* Find the next oldest signal. Note that as we have
598                          * not been holding the lock, another client may
599                          * have installed an even older signal than the one
600                          * we just completed - so double check we are still
601                          * the oldest before picking the next one.
602                          */
603                         if (request == rcu_access_pointer(b->first_signal)) {
604                                 struct rb_node *rb =
605                                         rb_next(&request->signaling.node);
606                                 rcu_assign_pointer(b->first_signal,
607                                                    rb ? to_signaler(rb) : NULL);
608                         }
609                         rb_erase(&request->signaling.node, &b->signals);
610                         RB_CLEAR_NODE(&request->signaling.node);
611
612                         spin_unlock_irq(&b->rb_lock);
613
614                         i915_gem_request_put(request);
615                 } else {
616                         DEFINE_WAIT(exec);
617
618                         if (kthread_should_stop()) {
619                                 GEM_BUG_ON(request);
620                                 break;
621                         }
622
623                         if (request)
624                                 add_wait_queue(&request->execute, &exec);
625
626                         schedule();
627
628                         if (request)
629                                 remove_wait_queue(&request->execute, &exec);
630
631                         if (kthread_should_park())
632                                 kthread_parkme();
633                 }
634                 i915_gem_request_put(request);
635         } while (1);
636         __set_current_state(TASK_RUNNING);
637
638         return 0;
639 }
640
641 void intel_engine_enable_signaling(struct drm_i915_gem_request *request)
642 {
643         struct intel_engine_cs *engine = request->engine;
644         struct intel_breadcrumbs *b = &engine->breadcrumbs;
645         struct rb_node *parent, **p;
646         bool first, wakeup;
647         u32 seqno;
648
649         /* Note that we may be called from an interrupt handler on another
650          * device (e.g. nouveau signaling a fence completion causing us
651          * to submit a request, and so enable signaling). As such,
652          * we need to make sure that all other users of b->lock protect
653          * against interrupts, i.e. use spin_lock_irqsave.
654          */
655
656         /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
657         GEM_BUG_ON(!irqs_disabled());
658         lockdep_assert_held(&request->lock);
659
660         seqno = i915_gem_request_global_seqno(request);
661         if (!seqno)
662                 return;
663
664         request->signaling.wait.tsk = b->signaler;
665         request->signaling.wait.request = request;
666         request->signaling.wait.seqno = seqno;
667         i915_gem_request_get(request);
668
669         spin_lock(&b->rb_lock);
670
671         /* First add ourselves into the list of waiters, but register our
672          * bottom-half as the signaller thread. As per usual, only the oldest
673          * waiter (not just signaller) is tasked as the bottom-half waking
674          * up all completed waiters after the user interrupt.
675          *
676          * If we are the oldest waiter, enable the irq (after which we
677          * must double check that the seqno did not complete).
678          */
679         wakeup = __intel_engine_add_wait(engine, &request->signaling.wait);
680
681         /* Now insert ourselves into the retirement ordered list of signals
682          * on this engine. We track the oldest seqno as that will be the
683          * first signal to complete.
684          */
685         parent = NULL;
686         first = true;
687         p = &b->signals.rb_node;
688         while (*p) {
689                 parent = *p;
690                 if (i915_seqno_passed(seqno,
691                                       to_signaler(parent)->signaling.wait.seqno)) {
692                         p = &parent->rb_right;
693                         first = false;
694                 } else {
695                         p = &parent->rb_left;
696                 }
697         }
698         rb_link_node(&request->signaling.node, parent, p);
699         rb_insert_color(&request->signaling.node, &b->signals);
700         if (first)
701                 rcu_assign_pointer(b->first_signal, request);
702
703         spin_unlock(&b->rb_lock);
704
705         if (wakeup)
706                 wake_up_process(b->signaler);
707 }
708
709 void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
710 {
711         struct intel_engine_cs *engine = request->engine;
712         struct intel_breadcrumbs *b = &engine->breadcrumbs;
713
714         GEM_BUG_ON(!irqs_disabled());
715         lockdep_assert_held(&request->lock);
716         GEM_BUG_ON(!request->signaling.wait.seqno);
717
718         spin_lock(&b->rb_lock);
719
720         if (!RB_EMPTY_NODE(&request->signaling.node)) {
721                 if (request == rcu_access_pointer(b->first_signal)) {
722                         struct rb_node *rb =
723                                 rb_next(&request->signaling.node);
724                         rcu_assign_pointer(b->first_signal,
725                                            rb ? to_signaler(rb) : NULL);
726                 }
727                 rb_erase(&request->signaling.node, &b->signals);
728                 RB_CLEAR_NODE(&request->signaling.node);
729                 i915_gem_request_put(request);
730         }
731
732         __intel_engine_remove_wait(engine, &request->signaling.wait);
733
734         spin_unlock(&b->rb_lock);
735
736         request->signaling.wait.seqno = 0;
737 }
738
739 int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
740 {
741         struct intel_breadcrumbs *b = &engine->breadcrumbs;
742         struct task_struct *tsk;
743
744         spin_lock_init(&b->rb_lock);
745         spin_lock_init(&b->irq_lock);
746
747         setup_timer(&b->fake_irq,
748                     intel_breadcrumbs_fake_irq,
749                     (unsigned long)engine);
750         setup_timer(&b->hangcheck,
751                     intel_breadcrumbs_hangcheck,
752                     (unsigned long)engine);
753
754         /* Spawn a thread to provide a common bottom-half for all signals.
755          * As this is an asynchronous interface we cannot steal the current
756          * task for handling the bottom-half to the user interrupt, therefore
757          * we create a thread to do the coherent seqno dance after the
758          * interrupt and then signal the waitqueue (via the dma-buf/fence).
759          */
760         tsk = kthread_run(intel_breadcrumbs_signaler, engine,
761                           "i915/signal:%d", engine->id);
762         if (IS_ERR(tsk))
763                 return PTR_ERR(tsk);
764
765         b->signaler = tsk;
766
767         return 0;
768 }
769
770 static void cancel_fake_irq(struct intel_engine_cs *engine)
771 {
772         struct intel_breadcrumbs *b = &engine->breadcrumbs;
773
774         del_timer_sync(&b->hangcheck);
775         del_timer_sync(&b->fake_irq);
776         clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
777 }
778
779 void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
780 {
781         struct intel_breadcrumbs *b = &engine->breadcrumbs;
782
783         cancel_fake_irq(engine);
784         spin_lock_irq(&b->irq_lock);
785
786         if (b->irq_enabled)
787                 irq_enable(engine);
788         else
789                 irq_disable(engine);
790
791         /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
792          * GPU is active and may have already executed the MI_USER_INTERRUPT
793          * before the CPU is ready to receive. However, the engine is currently
794          * idle (we haven't started it yet), there is no possibility for a
795          * missed interrupt as we enabled the irq and so we can clear the
796          * immediate wakeup (until a real interrupt arrives for the waiter).
797          */
798         clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
799
800         if (b->irq_armed)
801                 enable_fake_irq(b);
802
803         spin_unlock_irq(&b->irq_lock);
804 }
805
806 void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
807 {
808         struct intel_breadcrumbs *b = &engine->breadcrumbs;
809
810         /* The engines should be idle and all requests accounted for! */
811         WARN_ON(READ_ONCE(b->irq_wait));
812         WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
813         WARN_ON(rcu_access_pointer(b->first_signal));
814         WARN_ON(!RB_EMPTY_ROOT(&b->signals));
815
816         if (!IS_ERR_OR_NULL(b->signaler))
817                 kthread_stop(b->signaler);
818
819         cancel_fake_irq(engine);
820 }
821
822 bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
823 {
824         struct intel_breadcrumbs *b = &engine->breadcrumbs;
825         bool busy = false;
826
827         spin_lock_irq(&b->rb_lock);
828
829         if (b->irq_wait) {
830                 wake_up_process(b->irq_wait->tsk);
831                 busy |= intel_engine_flag(engine);
832         }
833
834         if (rcu_access_pointer(b->first_signal)) {
835                 wake_up_process(b->signaler);
836                 busy |= intel_engine_flag(engine);
837         }
838
839         spin_unlock_irq(&b->rb_lock);
840
841         return busy;
842 }
843
844 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
845 #include "selftests/intel_breadcrumbs.c"
846 #endif