blob: 4de054f8c1bafe60bca7466edf1af5e181b4db39 [file] [log] [blame]
Chris Wilson688e6c72016-07-01 17:23:15 +01001/*
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
Chris Wilsonc81d4612016-07-01 17:23:25 +010025#include <linux/kthread.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010026#include <uapi/linux/sched/types.h>
Chris Wilsonc81d4612016-07-01 17:23:25 +010027
Chris Wilson688e6c72016-07-01 17:23:15 +010028#include "i915_drv.h"
29
Chris Wilson67b807a82017-02-27 20:58:50 +000030static unsigned int __intel_breadcrumbs_wakeup(struct intel_breadcrumbs *b)
Chris Wilson8d769ea2017-02-27 20:58:47 +000031{
Chris Wilson56299fb2017-02-27 20:58:48 +000032 struct intel_wait *wait;
Chris Wilson8d769ea2017-02-27 20:58:47 +000033 unsigned int result = 0;
34
Chris Wilson61d3dc72017-03-03 19:08:24 +000035 lockdep_assert_held(&b->irq_lock);
36
37 wait = b->irq_wait;
Chris Wilson56299fb2017-02-27 20:58:48 +000038 if (wait) {
Chris Wilson8d769ea2017-02-27 20:58:47 +000039 result = ENGINE_WAKEUP_WAITER;
Chris Wilson67b807a82017-02-27 20:58:50 +000040 if (wake_up_process(wait->tsk))
41 result |= ENGINE_WAKEUP_ASLEEP;
Chris Wilson8d769ea2017-02-27 20:58:47 +000042 }
Chris Wilson67b807a82017-02-27 20:58:50 +000043
44 return result;
45}
46
47unsigned int intel_engine_wakeup(struct intel_engine_cs *engine)
48{
49 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson467221b2017-03-20 14:31:33 +000050 unsigned long flags;
Chris Wilson67b807a82017-02-27 20:58:50 +000051 unsigned int result;
52
Chris Wilson467221b2017-03-20 14:31:33 +000053 spin_lock_irqsave(&b->irq_lock, flags);
Chris Wilson67b807a82017-02-27 20:58:50 +000054 result = __intel_breadcrumbs_wakeup(b);
Chris Wilson467221b2017-03-20 14:31:33 +000055 spin_unlock_irqrestore(&b->irq_lock, flags);
Chris Wilson8d769ea2017-02-27 20:58:47 +000056
57 return result;
58}
59
Chris Wilson2246bea2017-02-17 15:13:00 +000060static unsigned long wait_timeout(void)
61{
62 return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
63}
64
Chris Wilson80166e402017-02-28 08:50:18 +000065static noinline void missed_breadcrumb(struct intel_engine_cs *engine)
66{
Helge Deller516726d2017-09-06 22:27:52 +020067 DRM_DEBUG_DRIVER("%s missed breadcrumb at %pS, irq posted? %s, current seqno=%x, last=%x\n",
Chris Wilson80166e402017-02-28 08:50:18 +000068 engine->name, __builtin_return_address(0),
69 yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
Chris Wilson695eaa32017-04-23 18:06:19 +010070 &engine->irq_posted)),
71 intel_engine_get_seqno(engine),
72 intel_engine_last_submit(engine));
Chris Wilson80166e402017-02-28 08:50:18 +000073
74 set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
75}
76
Kees Cook39cbf2a2017-10-17 09:53:04 +030077static void intel_breadcrumbs_hangcheck(struct timer_list *t)
Chris Wilson83348ba2016-08-09 17:47:51 +010078{
Kees Cook39cbf2a2017-10-17 09:53:04 +030079 struct intel_engine_cs *engine = from_timer(engine, t,
80 breadcrumbs.hangcheck);
Chris Wilson83348ba2016-08-09 17:47:51 +010081 struct intel_breadcrumbs *b = &engine->breadcrumbs;
82
Chris Wilson67b807a82017-02-27 20:58:50 +000083 if (!b->irq_armed)
Chris Wilson83348ba2016-08-09 17:47:51 +010084 return;
85
Chris Wilson2246bea2017-02-17 15:13:00 +000086 if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) {
87 b->hangcheck_interrupts = atomic_read(&engine->irq_count);
88 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson83348ba2016-08-09 17:47:51 +010089 return;
90 }
91
Chris Wilsona6b0a1412017-03-15 22:22:59 +000092 /* We keep the hangcheck timer alive until we disarm the irq, even
Chris Wilson67b807a82017-02-27 20:58:50 +000093 * if there are no waiters at present.
94 *
95 * If the waiter was currently running, assume it hasn't had a chance
Chris Wilson89985672017-02-17 15:13:02 +000096 * to process the pending interrupt (e.g, low priority task on a loaded
97 * system) and wait until it sleeps before declaring a missed interrupt.
Chris Wilson67b807a82017-02-27 20:58:50 +000098 *
99 * If the waiter was asleep (and not even pending a wakeup), then we
100 * must have missed an interrupt as the GPU has stopped advancing
101 * but we still have a waiter. Assuming all batches complete within
102 * DRM_I915_HANGCHECK_JIFFIES [1.5s]!
Chris Wilson89985672017-02-17 15:13:02 +0000103 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000104 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) {
Chris Wilson80166e402017-02-28 08:50:18 +0000105 missed_breadcrumb(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000106 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
107 } else {
Chris Wilson89985672017-02-17 15:13:02 +0000108 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson89985672017-02-17 15:13:02 +0000109 }
Chris Wilson83348ba2016-08-09 17:47:51 +0100110}
111
Kees Cook39cbf2a2017-10-17 09:53:04 +0300112static void intel_breadcrumbs_fake_irq(struct timer_list *t)
Chris Wilson688e6c72016-07-01 17:23:15 +0100113{
Kees Cook39cbf2a2017-10-17 09:53:04 +0300114 struct intel_engine_cs *engine = from_timer(engine, t,
115 breadcrumbs.fake_irq);
Chris Wilson67b807a82017-02-27 20:58:50 +0000116 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson688e6c72016-07-01 17:23:15 +0100117
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000118 /* The timer persists in case we cannot enable interrupts,
Chris Wilson688e6c72016-07-01 17:23:15 +0100119 * or if we have previously seen seqno/interrupt incoherency
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000120 * ("missed interrupt" syndrome, better known as a "missed breadcrumb").
121 * Here the worker will wake up every jiffie in order to kick the
122 * oldest waiter to do the coherent seqno check.
Chris Wilson688e6c72016-07-01 17:23:15 +0100123 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000124
Tvrtko Ursulina9e64932017-03-06 15:03:20 +0000125 spin_lock_irq(&b->irq_lock);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100126 if (b->irq_armed && !__intel_breadcrumbs_wakeup(b))
Chris Wilson67b807a82017-02-27 20:58:50 +0000127 __intel_engine_disarm_breadcrumbs(engine);
Tvrtko Ursulina9e64932017-03-06 15:03:20 +0000128 spin_unlock_irq(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000129 if (!b->irq_armed)
Chris Wilson19d0a572017-02-27 20:58:49 +0000130 return;
131
Chris Wilson67b807a82017-02-27 20:58:50 +0000132 mod_timer(&b->fake_irq, jiffies + 1);
Chris Wilson19d0a572017-02-27 20:58:49 +0000133
134 /* Ensure that even if the GPU hangs, we get woken up.
135 *
136 * However, note that if no one is waiting, we never notice
137 * a gpu hang. Eventually, we will have to wait for a resource
138 * held by the GPU and so trigger a hangcheck. In the most
139 * pathological case, this will be upon memory starvation! To
140 * prevent this, we also queue the hangcheck from the retire
141 * worker.
142 */
143 i915_queue_hangcheck(engine->i915);
Chris Wilson688e6c72016-07-01 17:23:15 +0100144}
145
146static void irq_enable(struct intel_engine_cs *engine)
147{
Chris Wilson3d5564e2016-07-01 17:23:23 +0100148 /* Enabling the IRQ may miss the generation of the interrupt, but
149 * we still need to force the barrier before reading the seqno,
150 * just in case.
151 */
Chris Wilson538b2572017-01-24 15:18:05 +0000152 set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100153
Chris Wilsonf6168e32016-10-28 13:58:55 +0100154 /* Caller disables interrupts */
155 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100156 engine->irq_enable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100157 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100158}
159
160static void irq_disable(struct intel_engine_cs *engine)
161{
Chris Wilsonf6168e32016-10-28 13:58:55 +0100162 /* Caller disables interrupts */
163 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100164 engine->irq_disable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100165 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100166}
167
Chris Wilson67b807a82017-02-27 20:58:50 +0000168void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
169{
170 struct intel_breadcrumbs *b = &engine->breadcrumbs;
171
Chris Wilson61d3dc72017-03-03 19:08:24 +0000172 lockdep_assert_held(&b->irq_lock);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000173 GEM_BUG_ON(b->irq_wait);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100174 GEM_BUG_ON(!b->irq_armed);
Chris Wilson67b807a82017-02-27 20:58:50 +0000175
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100176 GEM_BUG_ON(!b->irq_enabled);
177 if (!--b->irq_enabled)
Chris Wilson67b807a82017-02-27 20:58:50 +0000178 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000179
180 b->irq_armed = false;
181}
182
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100183void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
184{
185 struct intel_breadcrumbs *b = &engine->breadcrumbs;
186
187 spin_lock_irq(&b->irq_lock);
188 if (!b->irq_enabled++)
189 irq_enable(engine);
190 GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
191 spin_unlock_irq(&b->irq_lock);
192}
193
194void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
195{
196 struct intel_breadcrumbs *b = &engine->breadcrumbs;
197
198 spin_lock_irq(&b->irq_lock);
199 GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
200 if (!--b->irq_enabled)
201 irq_disable(engine);
202 spin_unlock_irq(&b->irq_lock);
203}
204
Chris Wilson67b807a82017-02-27 20:58:50 +0000205void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
206{
207 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000208 struct intel_wait *wait, *n, *first;
Chris Wilson67b807a82017-02-27 20:58:50 +0000209
210 if (!b->irq_armed)
211 return;
212
Chris Wilson67b807a82017-02-27 20:58:50 +0000213 /* We only disarm the irq when we are idle (all requests completed),
Chris Wilsone1c0c912017-03-06 09:29:15 +0000214 * so if the bottom-half remains asleep, it missed the request
Chris Wilson67b807a82017-02-27 20:58:50 +0000215 * completion.
216 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000217
Chris Wilsone1c0c912017-03-06 09:29:15 +0000218 spin_lock_irq(&b->rb_lock);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000219
220 spin_lock(&b->irq_lock);
221 first = fetch_and_zero(&b->irq_wait);
Chris Wilsone5330ac2017-10-31 12:22:35 +0000222 if (b->irq_armed)
223 __intel_engine_disarm_breadcrumbs(engine);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000224 spin_unlock(&b->irq_lock);
225
Chris Wilsone1c0c912017-03-06 09:29:15 +0000226 rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
227 RB_CLEAR_NODE(&wait->node);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000228 if (wake_up_process(wait->tsk) && wait == first)
Chris Wilsone1c0c912017-03-06 09:29:15 +0000229 missed_breadcrumb(engine);
230 }
231 b->waiters = RB_ROOT;
232
Chris Wilsone1c0c912017-03-06 09:29:15 +0000233 spin_unlock_irq(&b->rb_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000234}
235
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000236static bool use_fake_irq(const struct intel_breadcrumbs *b)
237{
238 const struct intel_engine_cs *engine =
239 container_of(b, struct intel_engine_cs, breadcrumbs);
240
241 if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
242 return false;
243
244 /* Only start with the heavy weight fake irq timer if we have not
245 * seen any interrupts since enabling it the first time. If the
246 * interrupts are still arriving, it means we made a mistake in our
247 * engine->seqno_barrier(), a timing error that should be transient
248 * and unlikely to reoccur.
249 */
250 return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
251}
252
Chris Wilson67b807a82017-02-27 20:58:50 +0000253static void enable_fake_irq(struct intel_breadcrumbs *b)
254{
255 /* Ensure we never sleep indefinitely */
256 if (!b->irq_enabled || use_fake_irq(b))
257 mod_timer(&b->fake_irq, jiffies + 1);
258 else
259 mod_timer(&b->hangcheck, wait_timeout());
260}
261
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100262static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
Chris Wilson688e6c72016-07-01 17:23:15 +0100263{
264 struct intel_engine_cs *engine =
265 container_of(b, struct intel_engine_cs, breadcrumbs);
266 struct drm_i915_private *i915 = engine->i915;
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100267 bool enabled;
268
269 GEM_BUG_ON(!intel_irqs_enabled(i915));
Chris Wilson688e6c72016-07-01 17:23:15 +0100270
Chris Wilson61d3dc72017-03-03 19:08:24 +0000271 lockdep_assert_held(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000272 if (b->irq_armed)
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100273 return false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100274
Chris Wilson67b807a82017-02-27 20:58:50 +0000275 /* The breadcrumb irq will be disarmed on the interrupt after the
276 * waiters are signaled. This gives us a single interrupt window in
277 * which we can add a new waiter and avoid the cost of re-enabling
278 * the irq.
279 */
280 b->irq_armed = true;
Chris Wilson67b807a82017-02-27 20:58:50 +0000281
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000282 if (I915_SELFTEST_ONLY(b->mock)) {
283 /* For our mock objects we want to avoid interaction
284 * with the real hardware (which is not set up). So
285 * we simply pretend we have enabled the powerwell
286 * and the irq, and leave it up to the mock
287 * implementation to call intel_engine_wakeup()
288 * itself when it wants to simulate a user interrupt,
289 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100290 return true;
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000291 }
292
Chris Wilson688e6c72016-07-01 17:23:15 +0100293 /* Since we are waiting on a request, the GPU should be busy
Chris Wilson67b807a82017-02-27 20:58:50 +0000294 * and should have its own rpm reference. This is tracked
295 * by i915->gt.awake, we can forgo holding our own wakref
296 * for the interrupt as before i915->gt.awake is released (when
297 * the driver is idle) we disarm the breadcrumbs.
Chris Wilson688e6c72016-07-01 17:23:15 +0100298 */
Chris Wilson688e6c72016-07-01 17:23:15 +0100299
300 /* No interrupts? Kick the waiter every jiffie! */
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100301 enabled = false;
302 if (!b->irq_enabled++ &&
303 !test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
304 irq_enable(engine);
305 enabled = true;
Chris Wilson688e6c72016-07-01 17:23:15 +0100306 }
307
Chris Wilson67b807a82017-02-27 20:58:50 +0000308 enable_fake_irq(b);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100309 return enabled;
Chris Wilson688e6c72016-07-01 17:23:15 +0100310}
311
312static inline struct intel_wait *to_wait(struct rb_node *node)
313{
Chris Wilsond8567862016-12-20 10:40:03 +0000314 return rb_entry(node, struct intel_wait, node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100315}
316
317static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
318 struct intel_wait *wait)
319{
Chris Wilson61d3dc72017-03-03 19:08:24 +0000320 lockdep_assert_held(&b->rb_lock);
Chris Wilson908a6cb2017-03-15 21:07:25 +0000321 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100322
323 /* This request is completed, so remove it from the tree, mark it as
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000324 * complete, and *then* wake up the associated task. N.B. when the
325 * task wakes up, it will find the empty rb_node, discern that it
326 * has already been removed from the tree and skip the serialisation
327 * of the b->rb_lock and b->irq_lock. This means that the destruction
328 * of the intel_wait is not serialised with the interrupt handler
329 * by the waiter - it must instead be serialised by the caller.
Chris Wilson688e6c72016-07-01 17:23:15 +0100330 */
331 rb_erase(&wait->node, &b->waiters);
332 RB_CLEAR_NODE(&wait->node);
333
334 wake_up_process(wait->tsk); /* implicit smp_wmb() */
335}
336
Chris Wilsonb66255f2017-03-03 17:14:22 +0000337static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
338 struct rb_node *next)
339{
340 struct intel_breadcrumbs *b = &engine->breadcrumbs;
341
Chris Wilson61d3dc72017-03-03 19:08:24 +0000342 spin_lock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000343 GEM_BUG_ON(!b->irq_armed);
Chris Wilson429732e2017-03-15 21:07:23 +0000344 GEM_BUG_ON(!b->irq_wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000345 b->irq_wait = to_wait(next);
346 spin_unlock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000347
348 /* We always wake up the next waiter that takes over as the bottom-half
349 * as we may delegate not only the irq-seqno barrier to the next waiter
350 * but also the task of waking up concurrent waiters.
351 */
352 if (next)
353 wake_up_process(to_wait(next)->tsk);
354}
355
Chris Wilson688e6c72016-07-01 17:23:15 +0100356static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
357 struct intel_wait *wait)
358{
359 struct intel_breadcrumbs *b = &engine->breadcrumbs;
360 struct rb_node **p, *parent, *completed;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100361 bool first, armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100362 u32 seqno;
363
364 /* Insert the request into the retirement ordered list
365 * of waiters by walking the rbtree. If we are the oldest
366 * seqno in the tree (the first to be retired), then
367 * set ourselves as the bottom-half.
368 *
369 * As we descend the tree, prune completed branches since we hold the
370 * spinlock we know that the first_waiter must be delayed and can
371 * reduce some of the sequential wake up latency if we take action
372 * ourselves and wake up the completed tasks in parallel. Also, by
373 * removing stale elements in the tree, we may be able to reduce the
374 * ping-pong between the old bottom-half and ourselves as first-waiter.
375 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100376 armed = false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100377 first = true;
378 parent = NULL;
379 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100380 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100381
382 /* If the request completed before we managed to grab the spinlock,
383 * return now before adding ourselves to the rbtree. We let the
384 * current bottom-half handle any pending wakeups and instead
385 * try and get out of the way quickly.
386 */
387 if (i915_seqno_passed(seqno, wait->seqno)) {
388 RB_CLEAR_NODE(&wait->node);
389 return first;
390 }
391
392 p = &b->waiters.rb_node;
393 while (*p) {
394 parent = *p;
395 if (wait->seqno == to_wait(parent)->seqno) {
396 /* We have multiple waiters on the same seqno, select
397 * the highest priority task (that with the smallest
398 * task->prio) to serve as the bottom-half for this
399 * group.
400 */
401 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
402 p = &parent->rb_right;
403 first = false;
404 } else {
405 p = &parent->rb_left;
406 }
407 } else if (i915_seqno_passed(wait->seqno,
408 to_wait(parent)->seqno)) {
409 p = &parent->rb_right;
410 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
411 completed = parent;
412 else
413 first = false;
414 } else {
415 p = &parent->rb_left;
416 }
417 }
418 rb_link_node(&wait->node, parent, p);
419 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100420
Chris Wilson688e6c72016-07-01 17:23:15 +0100421 if (first) {
Chris Wilson61d3dc72017-03-03 19:08:24 +0000422 spin_lock(&b->irq_lock);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000423 b->irq_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100424 /* After assigning ourselves as the new bottom-half, we must
425 * perform a cursory check to prevent a missed interrupt.
426 * Either we miss the interrupt whilst programming the hardware,
427 * or if there was a previous waiter (for a later seqno) they
428 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100429 * in the unlocked read of b->irq_seqno_bh in the irq handler)
430 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100431 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100432 armed = __intel_breadcrumbs_enable_irq(b);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000433 spin_unlock(&b->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100434 }
Chris Wilson429732e2017-03-15 21:07:23 +0000435
436 if (completed) {
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000437 /* Advance the bottom-half (b->irq_wait) before we wake up
438 * the waiters who may scribble over their intel_wait
439 * just as the interrupt handler is dereferencing it via
440 * b->irq_wait.
441 */
Chris Wilson429732e2017-03-15 21:07:23 +0000442 if (!first) {
443 struct rb_node *next = rb_next(completed);
444 GEM_BUG_ON(next == &wait->node);
445 __intel_breadcrumbs_next(engine, next);
446 }
447
448 do {
449 struct intel_wait *crumb = to_wait(completed);
450 completed = rb_prev(completed);
451 __intel_breadcrumbs_finish(b, crumb);
452 } while (completed);
453 }
454
Chris Wilson61d3dc72017-03-03 19:08:24 +0000455 GEM_BUG_ON(!b->irq_wait);
Chris Wilson429732e2017-03-15 21:07:23 +0000456 GEM_BUG_ON(!b->irq_armed);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000457 GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100458
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100459 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100460}
461
462bool intel_engine_add_wait(struct intel_engine_cs *engine,
463 struct intel_wait *wait)
464{
465 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100466 bool armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100467
Chris Wilson61d3dc72017-03-03 19:08:24 +0000468 spin_lock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100469 armed = __intel_engine_add_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000470 spin_unlock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100471 if (armed)
472 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100473
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100474 /* Make the caller recheck if its request has already started. */
475 return i915_seqno_passed(intel_engine_get_seqno(engine),
476 wait->seqno - 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100477}
478
Chris Wilson688e6c72016-07-01 17:23:15 +0100479static inline bool chain_wakeup(struct rb_node *rb, int priority)
480{
481 return rb && to_wait(rb)->tsk->prio <= priority;
482}
483
Chris Wilsonc81d4612016-07-01 17:23:25 +0100484static inline int wakeup_priority(struct intel_breadcrumbs *b,
485 struct task_struct *tsk)
486{
487 if (tsk == b->signaler)
488 return INT_MIN;
489 else
490 return tsk->prio;
491}
492
Chris Wilson9eb143b2017-02-23 07:44:16 +0000493static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
494 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100495{
496 struct intel_breadcrumbs *b = &engine->breadcrumbs;
497
Chris Wilson61d3dc72017-03-03 19:08:24 +0000498 lockdep_assert_held(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100499
500 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000501 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100502
Chris Wilson61d3dc72017-03-03 19:08:24 +0000503 if (b->irq_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100504 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100505 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100506
Chris Wilson688e6c72016-07-01 17:23:15 +0100507 /* We are the current bottom-half. Find the next candidate,
508 * the first waiter in the queue on the remaining oldest
509 * request. As multiple seqnos may complete in the time it
510 * takes us to wake up and find the next waiter, we have to
511 * wake up that waiter for it to perform its own coherent
512 * completion check.
513 */
514 next = rb_next(&wait->node);
515 if (chain_wakeup(next, priority)) {
516 /* If the next waiter is already complete,
517 * wake it up and continue onto the next waiter. So
518 * if have a small herd, they will wake up in parallel
519 * rather than sequentially, which should reduce
520 * the overall latency in waking all the completed
521 * clients.
522 *
523 * However, waking up a chain adds extra latency to
524 * the first_waiter. This is undesirable if that
525 * waiter is a high priority task.
526 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100527 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100528
529 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
530 struct rb_node *n = rb_next(next);
531
532 __intel_breadcrumbs_finish(b, to_wait(next));
533 next = n;
534 if (!chain_wakeup(next, priority))
535 break;
536 }
537 }
538
Chris Wilsonb66255f2017-03-03 17:14:22 +0000539 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100540 } else {
541 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
542 }
543
544 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
545 rb_erase(&wait->node, &b->waiters);
546
Chris Wilson9eb143b2017-02-23 07:44:16 +0000547out:
Chris Wilson61d3dc72017-03-03 19:08:24 +0000548 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100549 GEM_BUG_ON(rb_first(&b->waiters) !=
Chris Wilson61d3dc72017-03-03 19:08:24 +0000550 (b->irq_wait ? &b->irq_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000551}
552
553void intel_engine_remove_wait(struct intel_engine_cs *engine,
554 struct intel_wait *wait)
555{
556 struct intel_breadcrumbs *b = &engine->breadcrumbs;
557
558 /* Quick check to see if this waiter was already decoupled from
559 * the tree by the bottom-half to avoid contention on the spinlock
560 * by the herd.
561 */
Chris Wilson908a6cb2017-03-15 21:07:25 +0000562 if (RB_EMPTY_NODE(&wait->node)) {
563 GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000564 return;
Chris Wilson908a6cb2017-03-15 21:07:25 +0000565 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000566
Chris Wilson61d3dc72017-03-03 19:08:24 +0000567 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000568 __intel_engine_remove_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000569 spin_unlock_irq(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100570}
571
Chris Wilsond6a22892017-02-23 07:44:17 +0000572static bool signal_valid(const struct drm_i915_gem_request *request)
573{
574 return intel_wait_check_request(&request->signaling.wait, request);
575}
576
577static bool signal_complete(const struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100578{
Chris Wilsonb3850852016-07-01 17:23:26 +0100579 if (!request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100580 return false;
581
582 /* If another process served as the bottom-half it may have already
583 * signalled that this wait is already completed.
584 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100585 if (intel_wait_complete(&request->signaling.wait))
Chris Wilsond6a22892017-02-23 07:44:17 +0000586 return signal_valid(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100587
588 /* Carefully check if the request is complete, giving time for the
589 * seqno to be visible or if the GPU hung.
590 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100591 if (__i915_request_irq_complete(request))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100592 return true;
593
594 return false;
595}
596
Chris Wilsonb3850852016-07-01 17:23:26 +0100597static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100598{
Chris Wilsond8567862016-12-20 10:40:03 +0000599 return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100600}
601
602static void signaler_set_rtpriority(void)
603{
604 struct sched_param param = { .sched_priority = 1 };
605
606 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
607}
608
609static int intel_breadcrumbs_signaler(void *arg)
610{
611 struct intel_engine_cs *engine = arg;
612 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonb3850852016-07-01 17:23:26 +0100613 struct drm_i915_gem_request *request;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100614
615 /* Install ourselves with high priority to reduce signalling latency */
616 signaler_set_rtpriority();
617
618 do {
Chris Wilsona7980a62017-04-04 13:05:31 +0100619 bool do_schedule = true;
620
Chris Wilsonc81d4612016-07-01 17:23:25 +0100621 set_current_state(TASK_INTERRUPTIBLE);
622
623 /* We are either woken up by the interrupt bottom-half,
624 * or by a client adding a new signaller. In both cases,
625 * the GPU seqno may have advanced beyond our oldest signal.
626 * If it has, propagate the signal, remove the waiter and
627 * check again with the next oldest signal. Otherwise we
628 * need to wait for a new interrupt from the GPU or for
629 * a new client.
630 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000631 rcu_read_lock();
632 request = rcu_dereference(b->first_signal);
633 if (request)
634 request = i915_gem_request_get_rcu(request);
635 rcu_read_unlock();
Chris Wilsonb3850852016-07-01 17:23:26 +0100636 if (signal_complete(request)) {
Chris Wilson7c9e9342017-01-24 11:00:09 +0000637 local_bh_disable();
638 dma_fence_signal(&request->fence);
639 local_bh_enable(); /* kick start the tasklets */
640
Chris Wilson61d3dc72017-03-03 19:08:24 +0000641 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000642
Chris Wilsonc81d4612016-07-01 17:23:25 +0100643 /* Wake up all other completed waiters and select the
644 * next bottom-half for the next user interrupt.
645 */
Chris Wilson9eb143b2017-02-23 07:44:16 +0000646 __intel_engine_remove_wait(engine,
647 &request->signaling.wait);
Chris Wilson5590af32016-09-09 14:11:54 +0100648
Chris Wilsonc81d4612016-07-01 17:23:25 +0100649 /* Find the next oldest signal. Note that as we have
650 * not been holding the lock, another client may
651 * have installed an even older signal than the one
652 * we just completed - so double check we are still
653 * the oldest before picking the next one.
654 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000655 if (request == rcu_access_pointer(b->first_signal)) {
Chris Wilsonb3850852016-07-01 17:23:26 +0100656 struct rb_node *rb =
657 rb_next(&request->signaling.node);
Chris Wilsoncced5e22017-02-23 07:44:15 +0000658 rcu_assign_pointer(b->first_signal,
659 rb ? to_signaler(rb) : NULL);
Chris Wilsonb3850852016-07-01 17:23:26 +0100660 }
661 rb_erase(&request->signaling.node, &b->signals);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000662 RB_CLEAR_NODE(&request->signaling.node);
663
Chris Wilson61d3dc72017-03-03 19:08:24 +0000664 spin_unlock_irq(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100665
Chris Wilsone8a261e2016-07-20 13:31:49 +0100666 i915_gem_request_put(request);
Chris Wilsona7980a62017-04-04 13:05:31 +0100667
668 /* If the engine is saturated we may be continually
669 * processing completed requests. This angers the
670 * NMI watchdog if we never let anything else
671 * have access to the CPU. Let's pretend to be nice
672 * and relinquish the CPU if we burn through the
673 * entire RT timeslice!
674 */
675 do_schedule = need_resched();
676 }
677
678 if (unlikely(do_schedule)) {
Chris Wilsond6a22892017-02-23 07:44:17 +0000679 DEFINE_WAIT(exec);
680
Chris Wilsonb1becb82017-04-03 11:51:24 +0100681 if (kthread_should_park())
682 kthread_parkme();
683
Chris Wilsoncced5e22017-02-23 07:44:15 +0000684 if (kthread_should_stop()) {
685 GEM_BUG_ON(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100686 break;
Chris Wilsoncced5e22017-02-23 07:44:15 +0000687 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100688
Chris Wilsond6a22892017-02-23 07:44:17 +0000689 if (request)
690 add_wait_queue(&request->execute, &exec);
691
Chris Wilsonc81d4612016-07-01 17:23:25 +0100692 schedule();
Chris Wilsonfe3288b2017-02-12 17:20:01 +0000693
Chris Wilsond6a22892017-02-23 07:44:17 +0000694 if (request)
695 remove_wait_queue(&request->execute, &exec);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100696 }
Chris Wilsoncced5e22017-02-23 07:44:15 +0000697 i915_gem_request_put(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100698 } while (1);
699 __set_current_state(TASK_RUNNING);
700
701 return 0;
702}
703
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100704void intel_engine_enable_signaling(struct drm_i915_gem_request *request,
705 bool wakeup)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100706{
707 struct intel_engine_cs *engine = request->engine;
708 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000709 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100710
Chris Wilsonf6168e32016-10-28 13:58:55 +0100711 /* Note that we may be called from an interrupt handler on another
712 * device (e.g. nouveau signaling a fence completion causing us
713 * to submit a request, and so enable signaling). As such,
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000714 * we need to make sure that all other users of b->rb_lock protect
Chris Wilsonf6168e32016-10-28 13:58:55 +0100715 * against interrupts, i.e. use spin_lock_irqsave.
716 */
717
718 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilsone60a8702017-03-02 11:51:30 +0000719 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000720 lockdep_assert_held(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000721
722 seqno = i915_gem_request_global_seqno(request);
723 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100724 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100725
Chris Wilsonb3850852016-07-01 17:23:26 +0100726 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000727 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000728 request->signaling.wait.seqno = seqno;
Chris Wilsone8a261e2016-07-20 13:31:49 +0100729 i915_gem_request_get(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100730
Chris Wilson61d3dc72017-03-03 19:08:24 +0000731 spin_lock(&b->rb_lock);
Chris Wilson4a50d202016-07-26 12:01:50 +0100732
Chris Wilsonc81d4612016-07-01 17:23:25 +0100733 /* First add ourselves into the list of waiters, but register our
734 * bottom-half as the signaller thread. As per usual, only the oldest
735 * waiter (not just signaller) is tasked as the bottom-half waking
736 * up all completed waiters after the user interrupt.
737 *
738 * If we are the oldest waiter, enable the irq (after which we
739 * must double check that the seqno did not complete).
740 */
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100741 wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100742
Chris Wilson735e0eb2017-06-08 12:14:04 +0100743 if (!__i915_gem_request_completed(request, seqno)) {
744 struct rb_node *parent, **p;
745 bool first;
746
747 /* Now insert ourselves into the retirement ordered list of
748 * signals on this engine. We track the oldest seqno as that
749 * will be the first signal to complete.
750 */
751 parent = NULL;
752 first = true;
753 p = &b->signals.rb_node;
754 while (*p) {
755 parent = *p;
756 if (i915_seqno_passed(seqno,
757 to_signaler(parent)->signaling.wait.seqno)) {
758 p = &parent->rb_right;
759 first = false;
760 } else {
761 p = &parent->rb_left;
762 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100763 }
Chris Wilson735e0eb2017-06-08 12:14:04 +0100764 rb_link_node(&request->signaling.node, parent, p);
765 rb_insert_color(&request->signaling.node, &b->signals);
766 if (first)
767 rcu_assign_pointer(b->first_signal, request);
768 } else {
769 __intel_engine_remove_wait(engine, &request->signaling.wait);
770 i915_gem_request_put(request);
771 wakeup = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100772 }
Chris Wilsonb3850852016-07-01 17:23:26 +0100773
Chris Wilson61d3dc72017-03-03 19:08:24 +0000774 spin_unlock(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100775
776 if (wakeup)
777 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100778}
779
Chris Wilson9eb143b2017-02-23 07:44:16 +0000780void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
781{
782 struct intel_engine_cs *engine = request->engine;
783 struct intel_breadcrumbs *b = &engine->breadcrumbs;
784
Chris Wilsone60a8702017-03-02 11:51:30 +0000785 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000786 lockdep_assert_held(&request->lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000787 GEM_BUG_ON(!request->signaling.wait.seqno);
788
Chris Wilson61d3dc72017-03-03 19:08:24 +0000789 spin_lock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000790
791 if (!RB_EMPTY_NODE(&request->signaling.node)) {
792 if (request == rcu_access_pointer(b->first_signal)) {
793 struct rb_node *rb =
794 rb_next(&request->signaling.node);
795 rcu_assign_pointer(b->first_signal,
796 rb ? to_signaler(rb) : NULL);
797 }
798 rb_erase(&request->signaling.node, &b->signals);
799 RB_CLEAR_NODE(&request->signaling.node);
800 i915_gem_request_put(request);
801 }
802
803 __intel_engine_remove_wait(engine, &request->signaling.wait);
804
Chris Wilson61d3dc72017-03-03 19:08:24 +0000805 spin_unlock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000806
807 request->signaling.wait.seqno = 0;
808}
809
Chris Wilson688e6c72016-07-01 17:23:15 +0100810int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
811{
812 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100813 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100814
Chris Wilson61d3dc72017-03-03 19:08:24 +0000815 spin_lock_init(&b->rb_lock);
816 spin_lock_init(&b->irq_lock);
817
Kees Cook39cbf2a2017-10-17 09:53:04 +0300818 timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
819 timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
Chris Wilson688e6c72016-07-01 17:23:15 +0100820
Chris Wilsonc81d4612016-07-01 17:23:25 +0100821 /* Spawn a thread to provide a common bottom-half for all signals.
822 * As this is an asynchronous interface we cannot steal the current
823 * task for handling the bottom-half to the user interrupt, therefore
824 * we create a thread to do the coherent seqno dance after the
825 * interrupt and then signal the waitqueue (via the dma-buf/fence).
826 */
827 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
828 "i915/signal:%d", engine->id);
829 if (IS_ERR(tsk))
830 return PTR_ERR(tsk);
831
832 b->signaler = tsk;
833
Chris Wilson688e6c72016-07-01 17:23:15 +0100834 return 0;
835}
836
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100837static void cancel_fake_irq(struct intel_engine_cs *engine)
838{
839 struct intel_breadcrumbs *b = &engine->breadcrumbs;
840
841 del_timer_sync(&b->hangcheck);
842 del_timer_sync(&b->fake_irq);
843 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
844}
845
846void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
847{
848 struct intel_breadcrumbs *b = &engine->breadcrumbs;
849
850 cancel_fake_irq(engine);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000851 spin_lock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100852
Chris Wilson67b807a82017-02-27 20:58:50 +0000853 if (b->irq_enabled)
854 irq_enable(engine);
855 else
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100856 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000857
858 /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
859 * GPU is active and may have already executed the MI_USER_INTERRUPT
860 * before the CPU is ready to receive. However, the engine is currently
861 * idle (we haven't started it yet), there is no possibility for a
862 * missed interrupt as we enabled the irq and so we can clear the
863 * immediate wakeup (until a real interrupt arrives for the waiter).
864 */
865 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
866
867 if (b->irq_armed)
868 enable_fake_irq(b);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100869
Chris Wilson61d3dc72017-03-03 19:08:24 +0000870 spin_unlock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100871}
872
Chris Wilson688e6c72016-07-01 17:23:15 +0100873void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
874{
875 struct intel_breadcrumbs *b = &engine->breadcrumbs;
876
Chris Wilson381744f2016-11-21 11:07:59 +0000877 /* The engines should be idle and all requests accounted for! */
Chris Wilson61d3dc72017-03-03 19:08:24 +0000878 WARN_ON(READ_ONCE(b->irq_wait));
Chris Wilson381744f2016-11-21 11:07:59 +0000879 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncced5e22017-02-23 07:44:15 +0000880 WARN_ON(rcu_access_pointer(b->first_signal));
Chris Wilson381744f2016-11-21 11:07:59 +0000881 WARN_ON(!RB_EMPTY_ROOT(&b->signals));
882
Chris Wilsonc81d4612016-07-01 17:23:25 +0100883 if (!IS_ERR_OR_NULL(b->signaler))
884 kthread_stop(b->signaler);
885
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100886 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100887}
888
Chris Wilson9b6586a2017-02-23 07:44:08 +0000889bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100890{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000891 struct intel_breadcrumbs *b = &engine->breadcrumbs;
892 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100893
Chris Wilson61d3dc72017-03-03 19:08:24 +0000894 spin_lock_irq(&b->rb_lock);
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000895
Chris Wilson61d3dc72017-03-03 19:08:24 +0000896 if (b->irq_wait) {
897 wake_up_process(b->irq_wait->tsk);
Chris Wilson4bd66392017-03-15 21:07:22 +0000898 busy = true;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100899 }
900
Chris Wilsoncced5e22017-02-23 07:44:15 +0000901 if (rcu_access_pointer(b->first_signal)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000902 wake_up_process(b->signaler);
Chris Wilson4bd66392017-03-15 21:07:22 +0000903 busy = true;
Chris Wilson9b6586a2017-02-23 07:44:08 +0000904 }
905
Chris Wilson61d3dc72017-03-03 19:08:24 +0000906 spin_unlock_irq(&b->rb_lock);
Chris Wilson9b6586a2017-02-23 07:44:08 +0000907
908 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100909}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000910
911#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
912#include "selftests/intel_breadcrumbs.c"
913#endif