blob: b955f7d7bd0f866f4b071324379333240b764997 [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 Wilsonb92326a2017-12-09 12:47:10 +000030#ifdef CONFIG_SMP
31#define task_asleep(tsk) ((tsk)->state & TASK_NORMAL && !(tsk)->on_cpu)
32#else
33#define task_asleep(tsk) ((tsk)->state & TASK_NORMAL)
34#endif
35
Chris Wilson67b807a82017-02-27 20:58:50 +000036static unsigned int __intel_breadcrumbs_wakeup(struct intel_breadcrumbs *b)
Chris Wilson8d769ea2017-02-27 20:58:47 +000037{
Chris Wilson56299fb2017-02-27 20:58:48 +000038 struct intel_wait *wait;
Chris Wilson8d769ea2017-02-27 20:58:47 +000039 unsigned int result = 0;
40
Chris Wilson61d3dc72017-03-03 19:08:24 +000041 lockdep_assert_held(&b->irq_lock);
42
43 wait = b->irq_wait;
Chris Wilson56299fb2017-02-27 20:58:48 +000044 if (wait) {
Chris Wilsonb92326a2017-12-09 12:47:10 +000045 /*
46 * N.B. Since task_asleep() and ttwu are not atomic, the
47 * waiter may actually go to sleep after the check, causing
48 * us to suppress a valid wakeup. We prefer to reduce the
49 * number of false positive missed_breadcrumb() warnings
50 * at the expense of a few false negatives, as it it easy
51 * to trigger a false positive under heavy load. Enough
52 * signal should remain from genuine missed_breadcrumb()
53 * for us to detect in CI.
54 */
55 bool was_asleep = task_asleep(wait->tsk);
56
Chris Wilson8d769ea2017-02-27 20:58:47 +000057 result = ENGINE_WAKEUP_WAITER;
Chris Wilsonb92326a2017-12-09 12:47:10 +000058 if (wake_up_process(wait->tsk) && was_asleep)
Chris Wilson67b807a82017-02-27 20:58:50 +000059 result |= ENGINE_WAKEUP_ASLEEP;
Chris Wilson8d769ea2017-02-27 20:58:47 +000060 }
Chris Wilson67b807a82017-02-27 20:58:50 +000061
62 return result;
63}
64
65unsigned int intel_engine_wakeup(struct intel_engine_cs *engine)
66{
67 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson467221b2017-03-20 14:31:33 +000068 unsigned long flags;
Chris Wilson67b807a82017-02-27 20:58:50 +000069 unsigned int result;
70
Chris Wilson467221b2017-03-20 14:31:33 +000071 spin_lock_irqsave(&b->irq_lock, flags);
Chris Wilson67b807a82017-02-27 20:58:50 +000072 result = __intel_breadcrumbs_wakeup(b);
Chris Wilson467221b2017-03-20 14:31:33 +000073 spin_unlock_irqrestore(&b->irq_lock, flags);
Chris Wilson8d769ea2017-02-27 20:58:47 +000074
75 return result;
76}
77
Chris Wilson2246bea2017-02-17 15:13:00 +000078static unsigned long wait_timeout(void)
79{
80 return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
81}
82
Chris Wilson80166e402017-02-28 08:50:18 +000083static noinline void missed_breadcrumb(struct intel_engine_cs *engine)
84{
Chris Wilson832265d2017-12-08 01:23:01 +000085 if (drm_debug & DRM_UT_DRIVER) {
86 struct drm_printer p = drm_debug_printer(__func__);
87
88 intel_engine_dump(engine, &p,
89 "%s missed breadcrumb at %pS\n",
90 engine->name, __builtin_return_address(0));
91 }
Chris Wilson80166e402017-02-28 08:50:18 +000092
93 set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
94}
95
Kees Cook39cbf2a2017-10-17 09:53:04 +030096static void intel_breadcrumbs_hangcheck(struct timer_list *t)
Chris Wilson83348ba2016-08-09 17:47:51 +010097{
Chris Wilsonb92326a2017-12-09 12:47:10 +000098 struct intel_engine_cs *engine =
99 from_timer(engine, t, breadcrumbs.hangcheck);
Chris Wilson83348ba2016-08-09 17:47:51 +0100100 struct intel_breadcrumbs *b = &engine->breadcrumbs;
101
Chris Wilson67b807a82017-02-27 20:58:50 +0000102 if (!b->irq_armed)
Chris Wilson83348ba2016-08-09 17:47:51 +0100103 return;
104
Chris Wilson2246bea2017-02-17 15:13:00 +0000105 if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) {
106 b->hangcheck_interrupts = atomic_read(&engine->irq_count);
107 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson83348ba2016-08-09 17:47:51 +0100108 return;
109 }
110
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000111 /* We keep the hangcheck timer alive until we disarm the irq, even
Chris Wilson67b807a82017-02-27 20:58:50 +0000112 * if there are no waiters at present.
113 *
114 * If the waiter was currently running, assume it hasn't had a chance
Chris Wilson89985672017-02-17 15:13:02 +0000115 * to process the pending interrupt (e.g, low priority task on a loaded
116 * system) and wait until it sleeps before declaring a missed interrupt.
Chris Wilson67b807a82017-02-27 20:58:50 +0000117 *
118 * If the waiter was asleep (and not even pending a wakeup), then we
119 * must have missed an interrupt as the GPU has stopped advancing
120 * but we still have a waiter. Assuming all batches complete within
121 * DRM_I915_HANGCHECK_JIFFIES [1.5s]!
Chris Wilson89985672017-02-17 15:13:02 +0000122 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000123 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) {
Chris Wilson80166e402017-02-28 08:50:18 +0000124 missed_breadcrumb(engine);
Chris Wilsonb92326a2017-12-09 12:47:10 +0000125 mod_timer(&b->fake_irq, jiffies + 1);
Chris Wilson67b807a82017-02-27 20:58:50 +0000126 } else {
Chris Wilson89985672017-02-17 15:13:02 +0000127 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson89985672017-02-17 15:13:02 +0000128 }
Chris Wilson83348ba2016-08-09 17:47:51 +0100129}
130
Kees Cook39cbf2a2017-10-17 09:53:04 +0300131static void intel_breadcrumbs_fake_irq(struct timer_list *t)
Chris Wilson688e6c72016-07-01 17:23:15 +0100132{
Kees Cook39cbf2a2017-10-17 09:53:04 +0300133 struct intel_engine_cs *engine = from_timer(engine, t,
134 breadcrumbs.fake_irq);
Chris Wilson67b807a82017-02-27 20:58:50 +0000135 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson688e6c72016-07-01 17:23:15 +0100136
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000137 /* The timer persists in case we cannot enable interrupts,
Chris Wilson688e6c72016-07-01 17:23:15 +0100138 * or if we have previously seen seqno/interrupt incoherency
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000139 * ("missed interrupt" syndrome, better known as a "missed breadcrumb").
140 * Here the worker will wake up every jiffie in order to kick the
141 * oldest waiter to do the coherent seqno check.
Chris Wilson688e6c72016-07-01 17:23:15 +0100142 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000143
Tvrtko Ursulina9e64932017-03-06 15:03:20 +0000144 spin_lock_irq(&b->irq_lock);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100145 if (b->irq_armed && !__intel_breadcrumbs_wakeup(b))
Chris Wilson67b807a82017-02-27 20:58:50 +0000146 __intel_engine_disarm_breadcrumbs(engine);
Tvrtko Ursulina9e64932017-03-06 15:03:20 +0000147 spin_unlock_irq(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000148 if (!b->irq_armed)
Chris Wilson19d0a572017-02-27 20:58:49 +0000149 return;
150
Chris Wilson67b807a82017-02-27 20:58:50 +0000151 mod_timer(&b->fake_irq, jiffies + 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100152}
153
154static void irq_enable(struct intel_engine_cs *engine)
155{
Chris Wilsonc16c4ba2017-11-07 10:20:03 +0000156 /*
157 * FIXME: Ideally we want this on the API boundary, but for the
158 * sake of testing with mock breadcrumbs (no HW so unable to
159 * enable irqs) we place it deep within the bowels, at the point
160 * of no return.
161 */
162 GEM_BUG_ON(!intel_irqs_enabled(engine->i915));
163
Chris Wilson3d5564e2016-07-01 17:23:23 +0100164 /* Enabling the IRQ may miss the generation of the interrupt, but
165 * we still need to force the barrier before reading the seqno,
166 * just in case.
167 */
Chris Wilson538b2572017-01-24 15:18:05 +0000168 set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100169
Chris Wilsonf6168e32016-10-28 13:58:55 +0100170 /* Caller disables interrupts */
171 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100172 engine->irq_enable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100173 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100174}
175
176static void irq_disable(struct intel_engine_cs *engine)
177{
Chris Wilsonf6168e32016-10-28 13:58:55 +0100178 /* Caller disables interrupts */
179 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100180 engine->irq_disable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100181 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100182}
183
Chris Wilson67b807a82017-02-27 20:58:50 +0000184void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
185{
186 struct intel_breadcrumbs *b = &engine->breadcrumbs;
187
Chris Wilson61d3dc72017-03-03 19:08:24 +0000188 lockdep_assert_held(&b->irq_lock);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000189 GEM_BUG_ON(b->irq_wait);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100190 GEM_BUG_ON(!b->irq_armed);
Chris Wilson67b807a82017-02-27 20:58:50 +0000191
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100192 GEM_BUG_ON(!b->irq_enabled);
193 if (!--b->irq_enabled)
Chris Wilson67b807a82017-02-27 20:58:50 +0000194 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000195
196 b->irq_armed = false;
197}
198
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100199void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
200{
201 struct intel_breadcrumbs *b = &engine->breadcrumbs;
202
203 spin_lock_irq(&b->irq_lock);
204 if (!b->irq_enabled++)
205 irq_enable(engine);
206 GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
207 spin_unlock_irq(&b->irq_lock);
208}
209
210void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
211{
212 struct intel_breadcrumbs *b = &engine->breadcrumbs;
213
214 spin_lock_irq(&b->irq_lock);
215 GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
216 if (!--b->irq_enabled)
217 irq_disable(engine);
218 spin_unlock_irq(&b->irq_lock);
219}
220
Chris Wilson67b807a82017-02-27 20:58:50 +0000221void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
222{
223 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson832265d2017-12-08 01:23:01 +0000224 struct intel_wait *wait, *n;
Chris Wilson67b807a82017-02-27 20:58:50 +0000225
226 if (!b->irq_armed)
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000227 return;
Chris Wilson67b807a82017-02-27 20:58:50 +0000228
Chris Wilson832265d2017-12-08 01:23:01 +0000229 /*
230 * We only disarm the irq when we are idle (all requests completed),
Chris Wilsone1c0c912017-03-06 09:29:15 +0000231 * so if the bottom-half remains asleep, it missed the request
Chris Wilson67b807a82017-02-27 20:58:50 +0000232 * completion.
233 */
Chris Wilson832265d2017-12-08 01:23:01 +0000234 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP)
235 missed_breadcrumb(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000236
Chris Wilsone1c0c912017-03-06 09:29:15 +0000237 spin_lock_irq(&b->rb_lock);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000238
239 spin_lock(&b->irq_lock);
Chris Wilson832265d2017-12-08 01:23:01 +0000240 b->irq_wait = NULL;
Chris Wilsone5330ac2017-10-31 12:22:35 +0000241 if (b->irq_armed)
242 __intel_engine_disarm_breadcrumbs(engine);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000243 spin_unlock(&b->irq_lock);
244
Chris Wilsone1c0c912017-03-06 09:29:15 +0000245 rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
246 RB_CLEAR_NODE(&wait->node);
Chris Wilson832265d2017-12-08 01:23:01 +0000247 wake_up_process(wait->tsk);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000248 }
249 b->waiters = RB_ROOT;
250
Chris Wilsone1c0c912017-03-06 09:29:15 +0000251 spin_unlock_irq(&b->rb_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000252}
253
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000254static bool use_fake_irq(const struct intel_breadcrumbs *b)
255{
256 const struct intel_engine_cs *engine =
257 container_of(b, struct intel_engine_cs, breadcrumbs);
258
259 if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
260 return false;
261
262 /* Only start with the heavy weight fake irq timer if we have not
263 * seen any interrupts since enabling it the first time. If the
264 * interrupts are still arriving, it means we made a mistake in our
265 * engine->seqno_barrier(), a timing error that should be transient
266 * and unlikely to reoccur.
267 */
268 return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
269}
270
Chris Wilson67b807a82017-02-27 20:58:50 +0000271static void enable_fake_irq(struct intel_breadcrumbs *b)
272{
273 /* Ensure we never sleep indefinitely */
274 if (!b->irq_enabled || use_fake_irq(b))
275 mod_timer(&b->fake_irq, jiffies + 1);
276 else
277 mod_timer(&b->hangcheck, wait_timeout());
278}
279
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100280static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
Chris Wilson688e6c72016-07-01 17:23:15 +0100281{
282 struct intel_engine_cs *engine =
283 container_of(b, struct intel_engine_cs, breadcrumbs);
284 struct drm_i915_private *i915 = engine->i915;
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100285 bool enabled;
286
Chris Wilson61d3dc72017-03-03 19:08:24 +0000287 lockdep_assert_held(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000288 if (b->irq_armed)
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100289 return false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100290
Chris Wilson67b807a82017-02-27 20:58:50 +0000291 /* The breadcrumb irq will be disarmed on the interrupt after the
292 * waiters are signaled. This gives us a single interrupt window in
293 * which we can add a new waiter and avoid the cost of re-enabling
294 * the irq.
295 */
296 b->irq_armed = true;
Chris Wilson67b807a82017-02-27 20:58:50 +0000297
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000298 if (I915_SELFTEST_ONLY(b->mock)) {
299 /* For our mock objects we want to avoid interaction
300 * with the real hardware (which is not set up). So
301 * we simply pretend we have enabled the powerwell
302 * and the irq, and leave it up to the mock
303 * implementation to call intel_engine_wakeup()
304 * itself when it wants to simulate a user interrupt,
305 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100306 return true;
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000307 }
308
Chris Wilson688e6c72016-07-01 17:23:15 +0100309 /* Since we are waiting on a request, the GPU should be busy
Chris Wilson67b807a82017-02-27 20:58:50 +0000310 * and should have its own rpm reference. This is tracked
311 * by i915->gt.awake, we can forgo holding our own wakref
312 * for the interrupt as before i915->gt.awake is released (when
313 * the driver is idle) we disarm the breadcrumbs.
Chris Wilson688e6c72016-07-01 17:23:15 +0100314 */
Chris Wilson688e6c72016-07-01 17:23:15 +0100315
316 /* No interrupts? Kick the waiter every jiffie! */
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100317 enabled = false;
318 if (!b->irq_enabled++ &&
319 !test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
320 irq_enable(engine);
321 enabled = true;
Chris Wilson688e6c72016-07-01 17:23:15 +0100322 }
323
Chris Wilson67b807a82017-02-27 20:58:50 +0000324 enable_fake_irq(b);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100325 return enabled;
Chris Wilson688e6c72016-07-01 17:23:15 +0100326}
327
328static inline struct intel_wait *to_wait(struct rb_node *node)
329{
Chris Wilsond8567862016-12-20 10:40:03 +0000330 return rb_entry(node, struct intel_wait, node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100331}
332
333static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
334 struct intel_wait *wait)
335{
Chris Wilson61d3dc72017-03-03 19:08:24 +0000336 lockdep_assert_held(&b->rb_lock);
Chris Wilson908a6cb2017-03-15 21:07:25 +0000337 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100338
339 /* This request is completed, so remove it from the tree, mark it as
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000340 * complete, and *then* wake up the associated task. N.B. when the
341 * task wakes up, it will find the empty rb_node, discern that it
342 * has already been removed from the tree and skip the serialisation
343 * of the b->rb_lock and b->irq_lock. This means that the destruction
344 * of the intel_wait is not serialised with the interrupt handler
345 * by the waiter - it must instead be serialised by the caller.
Chris Wilson688e6c72016-07-01 17:23:15 +0100346 */
347 rb_erase(&wait->node, &b->waiters);
348 RB_CLEAR_NODE(&wait->node);
349
350 wake_up_process(wait->tsk); /* implicit smp_wmb() */
351}
352
Chris Wilsonb66255f2017-03-03 17:14:22 +0000353static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
354 struct rb_node *next)
355{
356 struct intel_breadcrumbs *b = &engine->breadcrumbs;
357
Chris Wilson61d3dc72017-03-03 19:08:24 +0000358 spin_lock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000359 GEM_BUG_ON(!b->irq_armed);
Chris Wilson429732e2017-03-15 21:07:23 +0000360 GEM_BUG_ON(!b->irq_wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000361 b->irq_wait = to_wait(next);
362 spin_unlock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000363
364 /* We always wake up the next waiter that takes over as the bottom-half
365 * as we may delegate not only the irq-seqno barrier to the next waiter
366 * but also the task of waking up concurrent waiters.
367 */
368 if (next)
369 wake_up_process(to_wait(next)->tsk);
370}
371
Chris Wilson688e6c72016-07-01 17:23:15 +0100372static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
373 struct intel_wait *wait)
374{
375 struct intel_breadcrumbs *b = &engine->breadcrumbs;
376 struct rb_node **p, *parent, *completed;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100377 bool first, armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100378 u32 seqno;
379
Chris Wilsonc68ce692018-01-02 19:25:00 +0000380 GEM_BUG_ON(!wait->seqno);
381
Chris Wilson688e6c72016-07-01 17:23:15 +0100382 /* Insert the request into the retirement ordered list
383 * of waiters by walking the rbtree. If we are the oldest
384 * seqno in the tree (the first to be retired), then
385 * set ourselves as the bottom-half.
386 *
387 * As we descend the tree, prune completed branches since we hold the
388 * spinlock we know that the first_waiter must be delayed and can
389 * reduce some of the sequential wake up latency if we take action
390 * ourselves and wake up the completed tasks in parallel. Also, by
391 * removing stale elements in the tree, we may be able to reduce the
392 * ping-pong between the old bottom-half and ourselves as first-waiter.
393 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100394 armed = false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100395 first = true;
396 parent = NULL;
397 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100398 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100399
400 /* If the request completed before we managed to grab the spinlock,
401 * return now before adding ourselves to the rbtree. We let the
402 * current bottom-half handle any pending wakeups and instead
403 * try and get out of the way quickly.
404 */
405 if (i915_seqno_passed(seqno, wait->seqno)) {
406 RB_CLEAR_NODE(&wait->node);
407 return first;
408 }
409
410 p = &b->waiters.rb_node;
411 while (*p) {
412 parent = *p;
413 if (wait->seqno == to_wait(parent)->seqno) {
414 /* We have multiple waiters on the same seqno, select
415 * the highest priority task (that with the smallest
416 * task->prio) to serve as the bottom-half for this
417 * group.
418 */
419 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
420 p = &parent->rb_right;
421 first = false;
422 } else {
423 p = &parent->rb_left;
424 }
425 } else if (i915_seqno_passed(wait->seqno,
426 to_wait(parent)->seqno)) {
427 p = &parent->rb_right;
428 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
429 completed = parent;
430 else
431 first = false;
432 } else {
433 p = &parent->rb_left;
434 }
435 }
436 rb_link_node(&wait->node, parent, p);
437 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100438
Chris Wilson688e6c72016-07-01 17:23:15 +0100439 if (first) {
Chris Wilson61d3dc72017-03-03 19:08:24 +0000440 spin_lock(&b->irq_lock);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000441 b->irq_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100442 /* After assigning ourselves as the new bottom-half, we must
443 * perform a cursory check to prevent a missed interrupt.
444 * Either we miss the interrupt whilst programming the hardware,
445 * or if there was a previous waiter (for a later seqno) they
446 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100447 * in the unlocked read of b->irq_seqno_bh in the irq handler)
448 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100449 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100450 armed = __intel_breadcrumbs_enable_irq(b);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000451 spin_unlock(&b->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100452 }
Chris Wilson429732e2017-03-15 21:07:23 +0000453
454 if (completed) {
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000455 /* Advance the bottom-half (b->irq_wait) before we wake up
456 * the waiters who may scribble over their intel_wait
457 * just as the interrupt handler is dereferencing it via
458 * b->irq_wait.
459 */
Chris Wilson429732e2017-03-15 21:07:23 +0000460 if (!first) {
461 struct rb_node *next = rb_next(completed);
462 GEM_BUG_ON(next == &wait->node);
463 __intel_breadcrumbs_next(engine, next);
464 }
465
466 do {
467 struct intel_wait *crumb = to_wait(completed);
468 completed = rb_prev(completed);
469 __intel_breadcrumbs_finish(b, crumb);
470 } while (completed);
471 }
472
Chris Wilson61d3dc72017-03-03 19:08:24 +0000473 GEM_BUG_ON(!b->irq_wait);
Chris Wilson429732e2017-03-15 21:07:23 +0000474 GEM_BUG_ON(!b->irq_armed);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000475 GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100476
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100477 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100478}
479
480bool intel_engine_add_wait(struct intel_engine_cs *engine,
481 struct intel_wait *wait)
482{
483 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100484 bool armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100485
Chris Wilson61d3dc72017-03-03 19:08:24 +0000486 spin_lock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100487 armed = __intel_engine_add_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000488 spin_unlock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100489 if (armed)
490 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100491
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100492 /* Make the caller recheck if its request has already started. */
493 return i915_seqno_passed(intel_engine_get_seqno(engine),
494 wait->seqno - 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100495}
496
Chris Wilson688e6c72016-07-01 17:23:15 +0100497static inline bool chain_wakeup(struct rb_node *rb, int priority)
498{
499 return rb && to_wait(rb)->tsk->prio <= priority;
500}
501
Chris Wilsonc81d4612016-07-01 17:23:25 +0100502static inline int wakeup_priority(struct intel_breadcrumbs *b,
503 struct task_struct *tsk)
504{
505 if (tsk == b->signaler)
506 return INT_MIN;
507 else
508 return tsk->prio;
509}
510
Chris Wilson9eb143b2017-02-23 07:44:16 +0000511static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
512 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100513{
514 struct intel_breadcrumbs *b = &engine->breadcrumbs;
515
Chris Wilson61d3dc72017-03-03 19:08:24 +0000516 lockdep_assert_held(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100517
518 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000519 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100520
Chris Wilson61d3dc72017-03-03 19:08:24 +0000521 if (b->irq_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100522 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100523 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100524
Chris Wilson688e6c72016-07-01 17:23:15 +0100525 /* We are the current bottom-half. Find the next candidate,
526 * the first waiter in the queue on the remaining oldest
527 * request. As multiple seqnos may complete in the time it
528 * takes us to wake up and find the next waiter, we have to
529 * wake up that waiter for it to perform its own coherent
530 * completion check.
531 */
532 next = rb_next(&wait->node);
533 if (chain_wakeup(next, priority)) {
534 /* If the next waiter is already complete,
535 * wake it up and continue onto the next waiter. So
536 * if have a small herd, they will wake up in parallel
537 * rather than sequentially, which should reduce
538 * the overall latency in waking all the completed
539 * clients.
540 *
541 * However, waking up a chain adds extra latency to
542 * the first_waiter. This is undesirable if that
543 * waiter is a high priority task.
544 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100545 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100546
547 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
548 struct rb_node *n = rb_next(next);
549
550 __intel_breadcrumbs_finish(b, to_wait(next));
551 next = n;
552 if (!chain_wakeup(next, priority))
553 break;
554 }
555 }
556
Chris Wilsonb66255f2017-03-03 17:14:22 +0000557 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100558 } else {
559 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
560 }
561
562 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
563 rb_erase(&wait->node, &b->waiters);
Chris Wilsonc5346122017-11-15 12:14:58 +0000564 RB_CLEAR_NODE(&wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100565
Chris Wilson9eb143b2017-02-23 07:44:16 +0000566out:
Chris Wilson61d3dc72017-03-03 19:08:24 +0000567 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100568 GEM_BUG_ON(rb_first(&b->waiters) !=
Chris Wilson61d3dc72017-03-03 19:08:24 +0000569 (b->irq_wait ? &b->irq_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000570}
571
572void intel_engine_remove_wait(struct intel_engine_cs *engine,
573 struct intel_wait *wait)
574{
575 struct intel_breadcrumbs *b = &engine->breadcrumbs;
576
577 /* Quick check to see if this waiter was already decoupled from
578 * the tree by the bottom-half to avoid contention on the spinlock
579 * by the herd.
580 */
Chris Wilson908a6cb2017-03-15 21:07:25 +0000581 if (RB_EMPTY_NODE(&wait->node)) {
582 GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000583 return;
Chris Wilson908a6cb2017-03-15 21:07:25 +0000584 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000585
Chris Wilson61d3dc72017-03-03 19:08:24 +0000586 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000587 __intel_engine_remove_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000588 spin_unlock_irq(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100589}
590
Chris Wilsond6a22892017-02-23 07:44:17 +0000591static bool signal_complete(const struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100592{
Chris Wilsonb3850852016-07-01 17:23:26 +0100593 if (!request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100594 return false;
595
Chris Wilsonfd10e2c2018-02-06 09:46:33 +0000596 /*
597 * Carefully check if the request is complete, giving time for the
Chris Wilsonc81d4612016-07-01 17:23:25 +0100598 * seqno to be visible or if the GPU hung.
599 */
Chris Wilsonfd10e2c2018-02-06 09:46:33 +0000600 return __i915_request_irq_complete(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100601}
602
Chris Wilsonb3850852016-07-01 17:23:26 +0100603static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100604{
Chris Wilsond8567862016-12-20 10:40:03 +0000605 return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100606}
607
608static void signaler_set_rtpriority(void)
609{
610 struct sched_param param = { .sched_priority = 1 };
611
612 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
613}
614
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000615static void __intel_engine_remove_signal(struct intel_engine_cs *engine,
616 struct drm_i915_gem_request *request)
617{
618 struct intel_breadcrumbs *b = &engine->breadcrumbs;
619
620 lockdep_assert_held(&b->rb_lock);
621
622 /*
623 * Wake up all other completed waiters and select the
624 * next bottom-half for the next user interrupt.
625 */
626 __intel_engine_remove_wait(engine, &request->signaling.wait);
627
628 /*
629 * Find the next oldest signal. Note that as we have
630 * not been holding the lock, another client may
631 * have installed an even older signal than the one
632 * we just completed - so double check we are still
633 * the oldest before picking the next one.
634 */
635 if (request->signaling.wait.seqno) {
636 if (request == rcu_access_pointer(b->first_signal)) {
637 struct rb_node *rb = rb_next(&request->signaling.node);
638 rcu_assign_pointer(b->first_signal,
639 rb ? to_signaler(rb) : NULL);
640 }
641
642 rb_erase(&request->signaling.node, &b->signals);
643 request->signaling.wait.seqno = 0;
644 }
645}
646
647static struct drm_i915_gem_request *
648get_first_signal_rcu(struct intel_breadcrumbs *b)
649{
650 /*
651 * See the big warnings for i915_gem_active_get_rcu() and similarly
652 * for dma_fence_get_rcu_safe() that explain the intricacies involved
653 * here with defeating CPU/compiler speculation and enforcing
654 * the required memory barriers.
655 */
656 do {
657 struct drm_i915_gem_request *request;
658
659 request = rcu_dereference(b->first_signal);
660 if (request)
661 request = i915_gem_request_get_rcu(request);
662
663 barrier();
664
665 if (!request || request == rcu_access_pointer(b->first_signal))
666 return rcu_pointer_handoff(request);
667
668 i915_gem_request_put(request);
669 } while (1);
670}
671
Chris Wilsonc81d4612016-07-01 17:23:25 +0100672static int intel_breadcrumbs_signaler(void *arg)
673{
674 struct intel_engine_cs *engine = arg;
675 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonb3850852016-07-01 17:23:26 +0100676 struct drm_i915_gem_request *request;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100677
678 /* Install ourselves with high priority to reduce signalling latency */
679 signaler_set_rtpriority();
680
681 do {
Chris Wilsona7980a62017-04-04 13:05:31 +0100682 bool do_schedule = true;
683
Chris Wilsonc81d4612016-07-01 17:23:25 +0100684 set_current_state(TASK_INTERRUPTIBLE);
685
686 /* We are either woken up by the interrupt bottom-half,
687 * or by a client adding a new signaller. In both cases,
688 * the GPU seqno may have advanced beyond our oldest signal.
689 * If it has, propagate the signal, remove the waiter and
690 * check again with the next oldest signal. Otherwise we
691 * need to wait for a new interrupt from the GPU or for
692 * a new client.
693 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000694 rcu_read_lock();
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000695 request = get_first_signal_rcu(b);
Chris Wilsoncced5e22017-02-23 07:44:15 +0000696 rcu_read_unlock();
Chris Wilsonb3850852016-07-01 17:23:26 +0100697 if (signal_complete(request)) {
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000698 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
699 &request->fence.flags)) {
700 local_bh_disable();
701 dma_fence_signal(&request->fence);
Chris Wilsonfd10e2c2018-02-06 09:46:33 +0000702 GEM_BUG_ON(!i915_gem_request_completed(request));
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000703 local_bh_enable(); /* kick start the tasklets */
Chris Wilsonb3850852016-07-01 17:23:26 +0100704 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000705
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000706 if (READ_ONCE(request->signaling.wait.seqno)) {
707 spin_lock_irq(&b->rb_lock);
708 __intel_engine_remove_signal(engine, request);
709 spin_unlock_irq(&b->rb_lock);
710 }
Chris Wilsona7980a62017-04-04 13:05:31 +0100711
712 /* If the engine is saturated we may be continually
713 * processing completed requests. This angers the
714 * NMI watchdog if we never let anything else
715 * have access to the CPU. Let's pretend to be nice
716 * and relinquish the CPU if we burn through the
717 * entire RT timeslice!
718 */
719 do_schedule = need_resched();
720 }
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000721 i915_gem_request_put(request);
Chris Wilsona7980a62017-04-04 13:05:31 +0100722
723 if (unlikely(do_schedule)) {
Chris Wilsonb1becb82017-04-03 11:51:24 +0100724 if (kthread_should_park())
725 kthread_parkme();
726
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000727 if (unlikely(kthread_should_stop()))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100728 break;
729
730 schedule();
731 }
732 } while (1);
733 __set_current_state(TASK_RUNNING);
734
735 return 0;
736}
737
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100738void intel_engine_enable_signaling(struct drm_i915_gem_request *request,
739 bool wakeup)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100740{
741 struct intel_engine_cs *engine = request->engine;
742 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000743 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100744
Chris Wilsonf6168e32016-10-28 13:58:55 +0100745 /* Note that we may be called from an interrupt handler on another
746 * device (e.g. nouveau signaling a fence completion causing us
747 * to submit a request, and so enable signaling). As such,
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000748 * we need to make sure that all other users of b->rb_lock protect
Chris Wilsonf6168e32016-10-28 13:58:55 +0100749 * against interrupts, i.e. use spin_lock_irqsave.
750 */
751
752 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilsone60a8702017-03-02 11:51:30 +0000753 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000754 lockdep_assert_held(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000755
756 seqno = i915_gem_request_global_seqno(request);
757 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100758 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100759
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000760 spin_lock(&b->rb_lock);
761
762 GEM_BUG_ON(request->signaling.wait.seqno);
Chris Wilsonb3850852016-07-01 17:23:26 +0100763 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000764 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000765 request->signaling.wait.seqno = seqno;
Chris Wilson4a50d202016-07-26 12:01:50 +0100766
Chris Wilsonc81d4612016-07-01 17:23:25 +0100767 /* First add ourselves into the list of waiters, but register our
768 * bottom-half as the signaller thread. As per usual, only the oldest
769 * waiter (not just signaller) is tasked as the bottom-half waking
770 * up all completed waiters after the user interrupt.
771 *
772 * If we are the oldest waiter, enable the irq (after which we
773 * must double check that the seqno did not complete).
774 */
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100775 wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100776
Chris Wilson735e0eb2017-06-08 12:14:04 +0100777 if (!__i915_gem_request_completed(request, seqno)) {
778 struct rb_node *parent, **p;
779 bool first;
780
781 /* Now insert ourselves into the retirement ordered list of
782 * signals on this engine. We track the oldest seqno as that
783 * will be the first signal to complete.
784 */
785 parent = NULL;
786 first = true;
787 p = &b->signals.rb_node;
788 while (*p) {
789 parent = *p;
790 if (i915_seqno_passed(seqno,
791 to_signaler(parent)->signaling.wait.seqno)) {
792 p = &parent->rb_right;
793 first = false;
794 } else {
795 p = &parent->rb_left;
796 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100797 }
Chris Wilson735e0eb2017-06-08 12:14:04 +0100798 rb_link_node(&request->signaling.node, parent, p);
799 rb_insert_color(&request->signaling.node, &b->signals);
800 if (first)
801 rcu_assign_pointer(b->first_signal, request);
802 } else {
803 __intel_engine_remove_wait(engine, &request->signaling.wait);
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000804 request->signaling.wait.seqno = 0;
Chris Wilson735e0eb2017-06-08 12:14:04 +0100805 wakeup = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100806 }
Chris Wilsonb3850852016-07-01 17:23:26 +0100807
Chris Wilson61d3dc72017-03-03 19:08:24 +0000808 spin_unlock(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100809
810 if (wakeup)
811 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100812}
813
Chris Wilson9eb143b2017-02-23 07:44:16 +0000814void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
815{
Chris Wilsone60a8702017-03-02 11:51:30 +0000816 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000817 lockdep_assert_held(&request->lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000818
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000819 if (READ_ONCE(request->signaling.wait.seqno)) {
820 struct intel_engine_cs *engine = request->engine;
821 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson9eb143b2017-02-23 07:44:16 +0000822
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000823 spin_lock(&b->rb_lock);
824 __intel_engine_remove_signal(engine, request);
825 spin_unlock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000826 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000827}
828
Chris Wilson688e6c72016-07-01 17:23:15 +0100829int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
830{
831 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100832 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100833
Chris Wilson61d3dc72017-03-03 19:08:24 +0000834 spin_lock_init(&b->rb_lock);
835 spin_lock_init(&b->irq_lock);
836
Kees Cook39cbf2a2017-10-17 09:53:04 +0300837 timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
838 timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
Chris Wilson688e6c72016-07-01 17:23:15 +0100839
Chris Wilsonc81d4612016-07-01 17:23:25 +0100840 /* Spawn a thread to provide a common bottom-half for all signals.
841 * As this is an asynchronous interface we cannot steal the current
842 * task for handling the bottom-half to the user interrupt, therefore
843 * we create a thread to do the coherent seqno dance after the
844 * interrupt and then signal the waitqueue (via the dma-buf/fence).
845 */
846 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
847 "i915/signal:%d", engine->id);
848 if (IS_ERR(tsk))
849 return PTR_ERR(tsk);
850
851 b->signaler = tsk;
852
Chris Wilson688e6c72016-07-01 17:23:15 +0100853 return 0;
854}
855
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100856static void cancel_fake_irq(struct intel_engine_cs *engine)
857{
858 struct intel_breadcrumbs *b = &engine->breadcrumbs;
859
860 del_timer_sync(&b->hangcheck);
861 del_timer_sync(&b->fake_irq);
862 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
863}
864
865void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
866{
867 struct intel_breadcrumbs *b = &engine->breadcrumbs;
868
869 cancel_fake_irq(engine);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000870 spin_lock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100871
Chris Wilson67b807a82017-02-27 20:58:50 +0000872 if (b->irq_enabled)
873 irq_enable(engine);
874 else
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100875 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000876
877 /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
878 * GPU is active and may have already executed the MI_USER_INTERRUPT
879 * before the CPU is ready to receive. However, the engine is currently
880 * idle (we haven't started it yet), there is no possibility for a
881 * missed interrupt as we enabled the irq and so we can clear the
882 * immediate wakeup (until a real interrupt arrives for the waiter).
883 */
884 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
885
886 if (b->irq_armed)
887 enable_fake_irq(b);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100888
Chris Wilson61d3dc72017-03-03 19:08:24 +0000889 spin_unlock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100890}
891
Chris Wilson688e6c72016-07-01 17:23:15 +0100892void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
893{
894 struct intel_breadcrumbs *b = &engine->breadcrumbs;
895
Chris Wilson381744f2016-11-21 11:07:59 +0000896 /* The engines should be idle and all requests accounted for! */
Chris Wilson61d3dc72017-03-03 19:08:24 +0000897 WARN_ON(READ_ONCE(b->irq_wait));
Chris Wilson381744f2016-11-21 11:07:59 +0000898 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncced5e22017-02-23 07:44:15 +0000899 WARN_ON(rcu_access_pointer(b->first_signal));
Chris Wilson381744f2016-11-21 11:07:59 +0000900 WARN_ON(!RB_EMPTY_ROOT(&b->signals));
901
Chris Wilsonc81d4612016-07-01 17:23:25 +0100902 if (!IS_ERR_OR_NULL(b->signaler))
903 kthread_stop(b->signaler);
904
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100905 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100906}
907
Chris Wilson9b6586a2017-02-23 07:44:08 +0000908bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100909{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000910 struct intel_breadcrumbs *b = &engine->breadcrumbs;
911 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100912
Chris Wilson61d3dc72017-03-03 19:08:24 +0000913 spin_lock_irq(&b->rb_lock);
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000914
Chris Wilson61d3dc72017-03-03 19:08:24 +0000915 if (b->irq_wait) {
916 wake_up_process(b->irq_wait->tsk);
Chris Wilson4bd66392017-03-15 21:07:22 +0000917 busy = true;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100918 }
919
Chris Wilsoncced5e22017-02-23 07:44:15 +0000920 if (rcu_access_pointer(b->first_signal)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000921 wake_up_process(b->signaler);
Chris Wilson4bd66392017-03-15 21:07:22 +0000922 busy = true;
Chris Wilson9b6586a2017-02-23 07:44:08 +0000923 }
924
Chris Wilson61d3dc72017-03-03 19:08:24 +0000925 spin_unlock_irq(&b->rb_lock);
Chris Wilson9b6586a2017-02-23 07:44:08 +0000926
927 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100928}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000929
930#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
931#include "selftests/intel_breadcrumbs.c"
932#endif