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