blob: 24c6fefdd0b172d5861dd793960dc356d8664fa2 [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{
Chris Wilson832265d2017-12-08 01:23:01 +000067 if (drm_debug & DRM_UT_DRIVER) {
68 struct drm_printer p = drm_debug_printer(__func__);
69
70 intel_engine_dump(engine, &p,
71 "%s missed breadcrumb at %pS\n",
72 engine->name, __builtin_return_address(0));
73 }
Chris Wilson80166e402017-02-28 08:50:18 +000074
75 set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
76}
77
Kees Cook39cbf2a2017-10-17 09:53:04 +030078static void intel_breadcrumbs_hangcheck(struct timer_list *t)
Chris Wilson83348ba2016-08-09 17:47:51 +010079{
Kees Cook39cbf2a2017-10-17 09:53:04 +030080 struct intel_engine_cs *engine = from_timer(engine, t,
81 breadcrumbs.hangcheck);
Chris Wilson83348ba2016-08-09 17:47:51 +010082 struct intel_breadcrumbs *b = &engine->breadcrumbs;
83
Chris Wilson67b807a82017-02-27 20:58:50 +000084 if (!b->irq_armed)
Chris Wilson83348ba2016-08-09 17:47:51 +010085 return;
86
Chris Wilson2246bea2017-02-17 15:13:00 +000087 if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) {
88 b->hangcheck_interrupts = atomic_read(&engine->irq_count);
89 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson83348ba2016-08-09 17:47:51 +010090 return;
91 }
92
Chris Wilsona6b0a1412017-03-15 22:22:59 +000093 /* We keep the hangcheck timer alive until we disarm the irq, even
Chris Wilson67b807a82017-02-27 20:58:50 +000094 * if there are no waiters at present.
95 *
96 * If the waiter was currently running, assume it hasn't had a chance
Chris Wilson89985672017-02-17 15:13:02 +000097 * to process the pending interrupt (e.g, low priority task on a loaded
98 * system) and wait until it sleeps before declaring a missed interrupt.
Chris Wilson67b807a82017-02-27 20:58:50 +000099 *
100 * If the waiter was asleep (and not even pending a wakeup), then we
101 * must have missed an interrupt as the GPU has stopped advancing
102 * but we still have a waiter. Assuming all batches complete within
103 * DRM_I915_HANGCHECK_JIFFIES [1.5s]!
Chris Wilson89985672017-02-17 15:13:02 +0000104 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000105 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) {
Chris Wilson80166e402017-02-28 08:50:18 +0000106 missed_breadcrumb(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000107 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
108 } else {
Chris Wilson89985672017-02-17 15:13:02 +0000109 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson89985672017-02-17 15:13:02 +0000110 }
Chris Wilson83348ba2016-08-09 17:47:51 +0100111}
112
Kees Cook39cbf2a2017-10-17 09:53:04 +0300113static void intel_breadcrumbs_fake_irq(struct timer_list *t)
Chris Wilson688e6c72016-07-01 17:23:15 +0100114{
Kees Cook39cbf2a2017-10-17 09:53:04 +0300115 struct intel_engine_cs *engine = from_timer(engine, t,
116 breadcrumbs.fake_irq);
Chris Wilson67b807a82017-02-27 20:58:50 +0000117 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson688e6c72016-07-01 17:23:15 +0100118
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000119 /* The timer persists in case we cannot enable interrupts,
Chris Wilson688e6c72016-07-01 17:23:15 +0100120 * or if we have previously seen seqno/interrupt incoherency
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000121 * ("missed interrupt" syndrome, better known as a "missed breadcrumb").
122 * Here the worker will wake up every jiffie in order to kick the
123 * oldest waiter to do the coherent seqno check.
Chris Wilson688e6c72016-07-01 17:23:15 +0100124 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000125
Tvrtko Ursulina9e64932017-03-06 15:03:20 +0000126 spin_lock_irq(&b->irq_lock);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100127 if (b->irq_armed && !__intel_breadcrumbs_wakeup(b))
Chris Wilson67b807a82017-02-27 20:58:50 +0000128 __intel_engine_disarm_breadcrumbs(engine);
Tvrtko Ursulina9e64932017-03-06 15:03:20 +0000129 spin_unlock_irq(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000130 if (!b->irq_armed)
Chris Wilson19d0a572017-02-27 20:58:49 +0000131 return;
132
Chris Wilson67b807a82017-02-27 20:58:50 +0000133 mod_timer(&b->fake_irq, jiffies + 1);
Chris Wilson19d0a572017-02-27 20:58:49 +0000134
135 /* Ensure that even if the GPU hangs, we get woken up.
136 *
137 * However, note that if no one is waiting, we never notice
138 * a gpu hang. Eventually, we will have to wait for a resource
139 * held by the GPU and so trigger a hangcheck. In the most
140 * pathological case, this will be upon memory starvation! To
141 * prevent this, we also queue the hangcheck from the retire
142 * worker.
143 */
144 i915_queue_hangcheck(engine->i915);
Chris Wilson688e6c72016-07-01 17:23:15 +0100145}
146
147static void irq_enable(struct intel_engine_cs *engine)
148{
Chris Wilsonc16c4ba2017-11-07 10:20:03 +0000149 /*
150 * FIXME: Ideally we want this on the API boundary, but for the
151 * sake of testing with mock breadcrumbs (no HW so unable to
152 * enable irqs) we place it deep within the bowels, at the point
153 * of no return.
154 */
155 GEM_BUG_ON(!intel_irqs_enabled(engine->i915));
156
Chris Wilson3d5564e2016-07-01 17:23:23 +0100157 /* Enabling the IRQ may miss the generation of the interrupt, but
158 * we still need to force the barrier before reading the seqno,
159 * just in case.
160 */
Chris Wilson538b2572017-01-24 15:18:05 +0000161 set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100162
Chris Wilsonf6168e32016-10-28 13:58:55 +0100163 /* Caller disables interrupts */
164 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100165 engine->irq_enable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100166 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100167}
168
169static void irq_disable(struct intel_engine_cs *engine)
170{
Chris Wilsonf6168e32016-10-28 13:58:55 +0100171 /* Caller disables interrupts */
172 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100173 engine->irq_disable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100174 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100175}
176
Chris Wilson67b807a82017-02-27 20:58:50 +0000177void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
178{
179 struct intel_breadcrumbs *b = &engine->breadcrumbs;
180
Chris Wilson61d3dc72017-03-03 19:08:24 +0000181 lockdep_assert_held(&b->irq_lock);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000182 GEM_BUG_ON(b->irq_wait);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100183 GEM_BUG_ON(!b->irq_armed);
Chris Wilson67b807a82017-02-27 20:58:50 +0000184
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100185 GEM_BUG_ON(!b->irq_enabled);
186 if (!--b->irq_enabled)
Chris Wilson67b807a82017-02-27 20:58:50 +0000187 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000188
189 b->irq_armed = false;
190}
191
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100192void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
193{
194 struct intel_breadcrumbs *b = &engine->breadcrumbs;
195
196 spin_lock_irq(&b->irq_lock);
197 if (!b->irq_enabled++)
198 irq_enable(engine);
199 GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
200 spin_unlock_irq(&b->irq_lock);
201}
202
203void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
204{
205 struct intel_breadcrumbs *b = &engine->breadcrumbs;
206
207 spin_lock_irq(&b->irq_lock);
208 GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
209 if (!--b->irq_enabled)
210 irq_disable(engine);
211 spin_unlock_irq(&b->irq_lock);
212}
213
Chris Wilson67b807a82017-02-27 20:58:50 +0000214void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
215{
216 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson832265d2017-12-08 01:23:01 +0000217 struct intel_wait *wait, *n;
Chris Wilson67b807a82017-02-27 20:58:50 +0000218
219 if (!b->irq_armed)
220 return;
221
Chris Wilson832265d2017-12-08 01:23:01 +0000222 /*
223 * We only disarm the irq when we are idle (all requests completed),
Chris Wilsone1c0c912017-03-06 09:29:15 +0000224 * so if the bottom-half remains asleep, it missed the request
Chris Wilson67b807a82017-02-27 20:58:50 +0000225 * completion.
226 */
Chris Wilson832265d2017-12-08 01:23:01 +0000227 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP)
228 missed_breadcrumb(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000229
Chris Wilsone1c0c912017-03-06 09:29:15 +0000230 spin_lock_irq(&b->rb_lock);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000231
232 spin_lock(&b->irq_lock);
Chris Wilson832265d2017-12-08 01:23:01 +0000233 b->irq_wait = NULL;
Chris Wilsone5330ac2017-10-31 12:22:35 +0000234 if (b->irq_armed)
235 __intel_engine_disarm_breadcrumbs(engine);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000236 spin_unlock(&b->irq_lock);
237
Chris Wilsone1c0c912017-03-06 09:29:15 +0000238 rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
239 RB_CLEAR_NODE(&wait->node);
Chris Wilson832265d2017-12-08 01:23:01 +0000240 wake_up_process(wait->tsk);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000241 }
242 b->waiters = RB_ROOT;
243
Chris Wilsone1c0c912017-03-06 09:29:15 +0000244 spin_unlock_irq(&b->rb_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000245}
246
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000247static bool use_fake_irq(const struct intel_breadcrumbs *b)
248{
249 const struct intel_engine_cs *engine =
250 container_of(b, struct intel_engine_cs, breadcrumbs);
251
252 if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
253 return false;
254
255 /* Only start with the heavy weight fake irq timer if we have not
256 * seen any interrupts since enabling it the first time. If the
257 * interrupts are still arriving, it means we made a mistake in our
258 * engine->seqno_barrier(), a timing error that should be transient
259 * and unlikely to reoccur.
260 */
261 return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
262}
263
Chris Wilson67b807a82017-02-27 20:58:50 +0000264static void enable_fake_irq(struct intel_breadcrumbs *b)
265{
266 /* Ensure we never sleep indefinitely */
267 if (!b->irq_enabled || use_fake_irq(b))
268 mod_timer(&b->fake_irq, jiffies + 1);
269 else
270 mod_timer(&b->hangcheck, wait_timeout());
271}
272
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100273static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
Chris Wilson688e6c72016-07-01 17:23:15 +0100274{
275 struct intel_engine_cs *engine =
276 container_of(b, struct intel_engine_cs, breadcrumbs);
277 struct drm_i915_private *i915 = engine->i915;
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100278 bool enabled;
279
Chris Wilson61d3dc72017-03-03 19:08:24 +0000280 lockdep_assert_held(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000281 if (b->irq_armed)
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100282 return false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100283
Chris Wilson67b807a82017-02-27 20:58:50 +0000284 /* The breadcrumb irq will be disarmed on the interrupt after the
285 * waiters are signaled. This gives us a single interrupt window in
286 * which we can add a new waiter and avoid the cost of re-enabling
287 * the irq.
288 */
289 b->irq_armed = true;
Chris Wilson67b807a82017-02-27 20:58:50 +0000290
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000291 if (I915_SELFTEST_ONLY(b->mock)) {
292 /* For our mock objects we want to avoid interaction
293 * with the real hardware (which is not set up). So
294 * we simply pretend we have enabled the powerwell
295 * and the irq, and leave it up to the mock
296 * implementation to call intel_engine_wakeup()
297 * itself when it wants to simulate a user interrupt,
298 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100299 return true;
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000300 }
301
Chris Wilson688e6c72016-07-01 17:23:15 +0100302 /* Since we are waiting on a request, the GPU should be busy
Chris Wilson67b807a82017-02-27 20:58:50 +0000303 * and should have its own rpm reference. This is tracked
304 * by i915->gt.awake, we can forgo holding our own wakref
305 * for the interrupt as before i915->gt.awake is released (when
306 * the driver is idle) we disarm the breadcrumbs.
Chris Wilson688e6c72016-07-01 17:23:15 +0100307 */
Chris Wilson688e6c72016-07-01 17:23:15 +0100308
309 /* No interrupts? Kick the waiter every jiffie! */
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100310 enabled = false;
311 if (!b->irq_enabled++ &&
312 !test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
313 irq_enable(engine);
314 enabled = true;
Chris Wilson688e6c72016-07-01 17:23:15 +0100315 }
316
Chris Wilson67b807a82017-02-27 20:58:50 +0000317 enable_fake_irq(b);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100318 return enabled;
Chris Wilson688e6c72016-07-01 17:23:15 +0100319}
320
321static inline struct intel_wait *to_wait(struct rb_node *node)
322{
Chris Wilsond8567862016-12-20 10:40:03 +0000323 return rb_entry(node, struct intel_wait, node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100324}
325
326static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
327 struct intel_wait *wait)
328{
Chris Wilson61d3dc72017-03-03 19:08:24 +0000329 lockdep_assert_held(&b->rb_lock);
Chris Wilson908a6cb2017-03-15 21:07:25 +0000330 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100331
332 /* This request is completed, so remove it from the tree, mark it as
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000333 * complete, and *then* wake up the associated task. N.B. when the
334 * task wakes up, it will find the empty rb_node, discern that it
335 * has already been removed from the tree and skip the serialisation
336 * of the b->rb_lock and b->irq_lock. This means that the destruction
337 * of the intel_wait is not serialised with the interrupt handler
338 * by the waiter - it must instead be serialised by the caller.
Chris Wilson688e6c72016-07-01 17:23:15 +0100339 */
340 rb_erase(&wait->node, &b->waiters);
341 RB_CLEAR_NODE(&wait->node);
342
343 wake_up_process(wait->tsk); /* implicit smp_wmb() */
344}
345
Chris Wilsonb66255f2017-03-03 17:14:22 +0000346static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
347 struct rb_node *next)
348{
349 struct intel_breadcrumbs *b = &engine->breadcrumbs;
350
Chris Wilson61d3dc72017-03-03 19:08:24 +0000351 spin_lock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000352 GEM_BUG_ON(!b->irq_armed);
Chris Wilson429732e2017-03-15 21:07:23 +0000353 GEM_BUG_ON(!b->irq_wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000354 b->irq_wait = to_wait(next);
355 spin_unlock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000356
357 /* We always wake up the next waiter that takes over as the bottom-half
358 * as we may delegate not only the irq-seqno barrier to the next waiter
359 * but also the task of waking up concurrent waiters.
360 */
361 if (next)
362 wake_up_process(to_wait(next)->tsk);
363}
364
Chris Wilson688e6c72016-07-01 17:23:15 +0100365static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
366 struct intel_wait *wait)
367{
368 struct intel_breadcrumbs *b = &engine->breadcrumbs;
369 struct rb_node **p, *parent, *completed;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100370 bool first, armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100371 u32 seqno;
372
373 /* Insert the request into the retirement ordered list
374 * of waiters by walking the rbtree. If we are the oldest
375 * seqno in the tree (the first to be retired), then
376 * set ourselves as the bottom-half.
377 *
378 * As we descend the tree, prune completed branches since we hold the
379 * spinlock we know that the first_waiter must be delayed and can
380 * reduce some of the sequential wake up latency if we take action
381 * ourselves and wake up the completed tasks in parallel. Also, by
382 * removing stale elements in the tree, we may be able to reduce the
383 * ping-pong between the old bottom-half and ourselves as first-waiter.
384 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100385 armed = false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100386 first = true;
387 parent = NULL;
388 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100389 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100390
391 /* If the request completed before we managed to grab the spinlock,
392 * return now before adding ourselves to the rbtree. We let the
393 * current bottom-half handle any pending wakeups and instead
394 * try and get out of the way quickly.
395 */
396 if (i915_seqno_passed(seqno, wait->seqno)) {
397 RB_CLEAR_NODE(&wait->node);
398 return first;
399 }
400
401 p = &b->waiters.rb_node;
402 while (*p) {
403 parent = *p;
404 if (wait->seqno == to_wait(parent)->seqno) {
405 /* We have multiple waiters on the same seqno, select
406 * the highest priority task (that with the smallest
407 * task->prio) to serve as the bottom-half for this
408 * group.
409 */
410 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
411 p = &parent->rb_right;
412 first = false;
413 } else {
414 p = &parent->rb_left;
415 }
416 } else if (i915_seqno_passed(wait->seqno,
417 to_wait(parent)->seqno)) {
418 p = &parent->rb_right;
419 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
420 completed = parent;
421 else
422 first = false;
423 } else {
424 p = &parent->rb_left;
425 }
426 }
427 rb_link_node(&wait->node, parent, p);
428 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100429
Chris Wilson688e6c72016-07-01 17:23:15 +0100430 if (first) {
Chris Wilson61d3dc72017-03-03 19:08:24 +0000431 spin_lock(&b->irq_lock);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000432 b->irq_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100433 /* After assigning ourselves as the new bottom-half, we must
434 * perform a cursory check to prevent a missed interrupt.
435 * Either we miss the interrupt whilst programming the hardware,
436 * or if there was a previous waiter (for a later seqno) they
437 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100438 * in the unlocked read of b->irq_seqno_bh in the irq handler)
439 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100440 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100441 armed = __intel_breadcrumbs_enable_irq(b);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000442 spin_unlock(&b->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100443 }
Chris Wilson429732e2017-03-15 21:07:23 +0000444
445 if (completed) {
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000446 /* Advance the bottom-half (b->irq_wait) before we wake up
447 * the waiters who may scribble over their intel_wait
448 * just as the interrupt handler is dereferencing it via
449 * b->irq_wait.
450 */
Chris Wilson429732e2017-03-15 21:07:23 +0000451 if (!first) {
452 struct rb_node *next = rb_next(completed);
453 GEM_BUG_ON(next == &wait->node);
454 __intel_breadcrumbs_next(engine, next);
455 }
456
457 do {
458 struct intel_wait *crumb = to_wait(completed);
459 completed = rb_prev(completed);
460 __intel_breadcrumbs_finish(b, crumb);
461 } while (completed);
462 }
463
Chris Wilson61d3dc72017-03-03 19:08:24 +0000464 GEM_BUG_ON(!b->irq_wait);
Chris Wilson429732e2017-03-15 21:07:23 +0000465 GEM_BUG_ON(!b->irq_armed);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000466 GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100467
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100468 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100469}
470
471bool intel_engine_add_wait(struct intel_engine_cs *engine,
472 struct intel_wait *wait)
473{
474 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100475 bool armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100476
Chris Wilson61d3dc72017-03-03 19:08:24 +0000477 spin_lock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100478 armed = __intel_engine_add_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000479 spin_unlock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100480 if (armed)
481 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100482
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100483 /* Make the caller recheck if its request has already started. */
484 return i915_seqno_passed(intel_engine_get_seqno(engine),
485 wait->seqno - 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100486}
487
Chris Wilson688e6c72016-07-01 17:23:15 +0100488static inline bool chain_wakeup(struct rb_node *rb, int priority)
489{
490 return rb && to_wait(rb)->tsk->prio <= priority;
491}
492
Chris Wilsonc81d4612016-07-01 17:23:25 +0100493static inline int wakeup_priority(struct intel_breadcrumbs *b,
494 struct task_struct *tsk)
495{
496 if (tsk == b->signaler)
497 return INT_MIN;
498 else
499 return tsk->prio;
500}
501
Chris Wilson9eb143b2017-02-23 07:44:16 +0000502static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
503 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100504{
505 struct intel_breadcrumbs *b = &engine->breadcrumbs;
506
Chris Wilson61d3dc72017-03-03 19:08:24 +0000507 lockdep_assert_held(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100508
509 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000510 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100511
Chris Wilson61d3dc72017-03-03 19:08:24 +0000512 if (b->irq_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100513 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100514 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100515
Chris Wilson688e6c72016-07-01 17:23:15 +0100516 /* We are the current bottom-half. Find the next candidate,
517 * the first waiter in the queue on the remaining oldest
518 * request. As multiple seqnos may complete in the time it
519 * takes us to wake up and find the next waiter, we have to
520 * wake up that waiter for it to perform its own coherent
521 * completion check.
522 */
523 next = rb_next(&wait->node);
524 if (chain_wakeup(next, priority)) {
525 /* If the next waiter is already complete,
526 * wake it up and continue onto the next waiter. So
527 * if have a small herd, they will wake up in parallel
528 * rather than sequentially, which should reduce
529 * the overall latency in waking all the completed
530 * clients.
531 *
532 * However, waking up a chain adds extra latency to
533 * the first_waiter. This is undesirable if that
534 * waiter is a high priority task.
535 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100536 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100537
538 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
539 struct rb_node *n = rb_next(next);
540
541 __intel_breadcrumbs_finish(b, to_wait(next));
542 next = n;
543 if (!chain_wakeup(next, priority))
544 break;
545 }
546 }
547
Chris Wilsonb66255f2017-03-03 17:14:22 +0000548 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100549 } else {
550 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
551 }
552
553 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
554 rb_erase(&wait->node, &b->waiters);
Chris Wilsonc5346122017-11-15 12:14:58 +0000555 RB_CLEAR_NODE(&wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100556
Chris Wilson9eb143b2017-02-23 07:44:16 +0000557out:
Chris Wilson61d3dc72017-03-03 19:08:24 +0000558 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100559 GEM_BUG_ON(rb_first(&b->waiters) !=
Chris Wilson61d3dc72017-03-03 19:08:24 +0000560 (b->irq_wait ? &b->irq_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000561}
562
563void intel_engine_remove_wait(struct intel_engine_cs *engine,
564 struct intel_wait *wait)
565{
566 struct intel_breadcrumbs *b = &engine->breadcrumbs;
567
568 /* Quick check to see if this waiter was already decoupled from
569 * the tree by the bottom-half to avoid contention on the spinlock
570 * by the herd.
571 */
Chris Wilson908a6cb2017-03-15 21:07:25 +0000572 if (RB_EMPTY_NODE(&wait->node)) {
573 GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000574 return;
Chris Wilson908a6cb2017-03-15 21:07:25 +0000575 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000576
Chris Wilson61d3dc72017-03-03 19:08:24 +0000577 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000578 __intel_engine_remove_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000579 spin_unlock_irq(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100580}
581
Chris Wilsond6a22892017-02-23 07:44:17 +0000582static bool signal_valid(const struct drm_i915_gem_request *request)
583{
584 return intel_wait_check_request(&request->signaling.wait, request);
585}
586
587static bool signal_complete(const struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100588{
Chris Wilsonb3850852016-07-01 17:23:26 +0100589 if (!request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100590 return false;
591
592 /* If another process served as the bottom-half it may have already
593 * signalled that this wait is already completed.
594 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100595 if (intel_wait_complete(&request->signaling.wait))
Chris Wilsond6a22892017-02-23 07:44:17 +0000596 return signal_valid(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100597
598 /* Carefully check if the request is complete, giving time for the
599 * seqno to be visible or if the GPU hung.
600 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100601 if (__i915_request_irq_complete(request))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100602 return true;
603
604 return false;
605}
606
Chris Wilsonb3850852016-07-01 17:23:26 +0100607static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100608{
Chris Wilsond8567862016-12-20 10:40:03 +0000609 return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100610}
611
612static void signaler_set_rtpriority(void)
613{
614 struct sched_param param = { .sched_priority = 1 };
615
616 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
617}
618
619static int intel_breadcrumbs_signaler(void *arg)
620{
621 struct intel_engine_cs *engine = arg;
622 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonb3850852016-07-01 17:23:26 +0100623 struct drm_i915_gem_request *request;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100624
625 /* Install ourselves with high priority to reduce signalling latency */
626 signaler_set_rtpriority();
627
628 do {
Chris Wilsona7980a62017-04-04 13:05:31 +0100629 bool do_schedule = true;
630
Chris Wilsonc81d4612016-07-01 17:23:25 +0100631 set_current_state(TASK_INTERRUPTIBLE);
632
633 /* We are either woken up by the interrupt bottom-half,
634 * or by a client adding a new signaller. In both cases,
635 * the GPU seqno may have advanced beyond our oldest signal.
636 * If it has, propagate the signal, remove the waiter and
637 * check again with the next oldest signal. Otherwise we
638 * need to wait for a new interrupt from the GPU or for
639 * a new client.
640 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000641 rcu_read_lock();
642 request = rcu_dereference(b->first_signal);
643 if (request)
644 request = i915_gem_request_get_rcu(request);
645 rcu_read_unlock();
Chris Wilsonb3850852016-07-01 17:23:26 +0100646 if (signal_complete(request)) {
Chris Wilson7c9e9342017-01-24 11:00:09 +0000647 local_bh_disable();
648 dma_fence_signal(&request->fence);
649 local_bh_enable(); /* kick start the tasklets */
650
Chris Wilson61d3dc72017-03-03 19:08:24 +0000651 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000652
Chris Wilsonc81d4612016-07-01 17:23:25 +0100653 /* Wake up all other completed waiters and select the
654 * next bottom-half for the next user interrupt.
655 */
Chris Wilson9eb143b2017-02-23 07:44:16 +0000656 __intel_engine_remove_wait(engine,
657 &request->signaling.wait);
Chris Wilson5590af32016-09-09 14:11:54 +0100658
Chris Wilsonc81d4612016-07-01 17:23:25 +0100659 /* Find the next oldest signal. Note that as we have
660 * not been holding the lock, another client may
661 * have installed an even older signal than the one
662 * we just completed - so double check we are still
663 * the oldest before picking the next one.
664 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000665 if (request == rcu_access_pointer(b->first_signal)) {
Chris Wilsonb3850852016-07-01 17:23:26 +0100666 struct rb_node *rb =
667 rb_next(&request->signaling.node);
Chris Wilsoncced5e22017-02-23 07:44:15 +0000668 rcu_assign_pointer(b->first_signal,
669 rb ? to_signaler(rb) : NULL);
Chris Wilsonb3850852016-07-01 17:23:26 +0100670 }
671 rb_erase(&request->signaling.node, &b->signals);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000672 RB_CLEAR_NODE(&request->signaling.node);
673
Chris Wilson61d3dc72017-03-03 19:08:24 +0000674 spin_unlock_irq(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100675
Chris Wilsone8a261e2016-07-20 13:31:49 +0100676 i915_gem_request_put(request);
Chris Wilsona7980a62017-04-04 13:05:31 +0100677
678 /* If the engine is saturated we may be continually
679 * processing completed requests. This angers the
680 * NMI watchdog if we never let anything else
681 * have access to the CPU. Let's pretend to be nice
682 * and relinquish the CPU if we burn through the
683 * entire RT timeslice!
684 */
685 do_schedule = need_resched();
686 }
687
688 if (unlikely(do_schedule)) {
Chris Wilsond6a22892017-02-23 07:44:17 +0000689 DEFINE_WAIT(exec);
690
Chris Wilsonb1becb82017-04-03 11:51:24 +0100691 if (kthread_should_park())
692 kthread_parkme();
693
Chris Wilsoncced5e22017-02-23 07:44:15 +0000694 if (kthread_should_stop()) {
695 GEM_BUG_ON(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100696 break;
Chris Wilsoncced5e22017-02-23 07:44:15 +0000697 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100698
Chris Wilsond6a22892017-02-23 07:44:17 +0000699 if (request)
700 add_wait_queue(&request->execute, &exec);
701
Chris Wilsonc81d4612016-07-01 17:23:25 +0100702 schedule();
Chris Wilsonfe3288b2017-02-12 17:20:01 +0000703
Chris Wilsond6a22892017-02-23 07:44:17 +0000704 if (request)
705 remove_wait_queue(&request->execute, &exec);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100706 }
Chris Wilsoncced5e22017-02-23 07:44:15 +0000707 i915_gem_request_put(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100708 } while (1);
709 __set_current_state(TASK_RUNNING);
710
711 return 0;
712}
713
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100714void intel_engine_enable_signaling(struct drm_i915_gem_request *request,
715 bool wakeup)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100716{
717 struct intel_engine_cs *engine = request->engine;
718 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000719 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100720
Chris Wilsonf6168e32016-10-28 13:58:55 +0100721 /* Note that we may be called from an interrupt handler on another
722 * device (e.g. nouveau signaling a fence completion causing us
723 * to submit a request, and so enable signaling). As such,
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000724 * we need to make sure that all other users of b->rb_lock protect
Chris Wilsonf6168e32016-10-28 13:58:55 +0100725 * against interrupts, i.e. use spin_lock_irqsave.
726 */
727
728 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilsone60a8702017-03-02 11:51:30 +0000729 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000730 lockdep_assert_held(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000731
732 seqno = i915_gem_request_global_seqno(request);
733 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100734 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100735
Chris Wilsonb3850852016-07-01 17:23:26 +0100736 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000737 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000738 request->signaling.wait.seqno = seqno;
Chris Wilsone8a261e2016-07-20 13:31:49 +0100739 i915_gem_request_get(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100740
Chris Wilson61d3dc72017-03-03 19:08:24 +0000741 spin_lock(&b->rb_lock);
Chris Wilson4a50d202016-07-26 12:01:50 +0100742
Chris Wilsonc81d4612016-07-01 17:23:25 +0100743 /* First add ourselves into the list of waiters, but register our
744 * bottom-half as the signaller thread. As per usual, only the oldest
745 * waiter (not just signaller) is tasked as the bottom-half waking
746 * up all completed waiters after the user interrupt.
747 *
748 * If we are the oldest waiter, enable the irq (after which we
749 * must double check that the seqno did not complete).
750 */
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100751 wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100752
Chris Wilson735e0eb2017-06-08 12:14:04 +0100753 if (!__i915_gem_request_completed(request, seqno)) {
754 struct rb_node *parent, **p;
755 bool first;
756
757 /* Now insert ourselves into the retirement ordered list of
758 * signals on this engine. We track the oldest seqno as that
759 * will be the first signal to complete.
760 */
761 parent = NULL;
762 first = true;
763 p = &b->signals.rb_node;
764 while (*p) {
765 parent = *p;
766 if (i915_seqno_passed(seqno,
767 to_signaler(parent)->signaling.wait.seqno)) {
768 p = &parent->rb_right;
769 first = false;
770 } else {
771 p = &parent->rb_left;
772 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100773 }
Chris Wilson735e0eb2017-06-08 12:14:04 +0100774 rb_link_node(&request->signaling.node, parent, p);
775 rb_insert_color(&request->signaling.node, &b->signals);
776 if (first)
777 rcu_assign_pointer(b->first_signal, request);
778 } else {
779 __intel_engine_remove_wait(engine, &request->signaling.wait);
780 i915_gem_request_put(request);
781 wakeup = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100782 }
Chris Wilsonb3850852016-07-01 17:23:26 +0100783
Chris Wilson61d3dc72017-03-03 19:08:24 +0000784 spin_unlock(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100785
786 if (wakeup)
787 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100788}
789
Chris Wilson9eb143b2017-02-23 07:44:16 +0000790void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
791{
792 struct intel_engine_cs *engine = request->engine;
793 struct intel_breadcrumbs *b = &engine->breadcrumbs;
794
Chris Wilsone60a8702017-03-02 11:51:30 +0000795 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000796 lockdep_assert_held(&request->lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000797 GEM_BUG_ON(!request->signaling.wait.seqno);
798
Chris Wilson61d3dc72017-03-03 19:08:24 +0000799 spin_lock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000800
801 if (!RB_EMPTY_NODE(&request->signaling.node)) {
802 if (request == rcu_access_pointer(b->first_signal)) {
803 struct rb_node *rb =
804 rb_next(&request->signaling.node);
805 rcu_assign_pointer(b->first_signal,
806 rb ? to_signaler(rb) : NULL);
807 }
808 rb_erase(&request->signaling.node, &b->signals);
809 RB_CLEAR_NODE(&request->signaling.node);
810 i915_gem_request_put(request);
811 }
812
813 __intel_engine_remove_wait(engine, &request->signaling.wait);
814
Chris Wilson61d3dc72017-03-03 19:08:24 +0000815 spin_unlock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000816
817 request->signaling.wait.seqno = 0;
818}
819
Chris Wilson688e6c72016-07-01 17:23:15 +0100820int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
821{
822 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100823 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100824
Chris Wilson61d3dc72017-03-03 19:08:24 +0000825 spin_lock_init(&b->rb_lock);
826 spin_lock_init(&b->irq_lock);
827
Kees Cook39cbf2a2017-10-17 09:53:04 +0300828 timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
829 timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
Chris Wilson688e6c72016-07-01 17:23:15 +0100830
Chris Wilsonc81d4612016-07-01 17:23:25 +0100831 /* Spawn a thread to provide a common bottom-half for all signals.
832 * As this is an asynchronous interface we cannot steal the current
833 * task for handling the bottom-half to the user interrupt, therefore
834 * we create a thread to do the coherent seqno dance after the
835 * interrupt and then signal the waitqueue (via the dma-buf/fence).
836 */
837 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
838 "i915/signal:%d", engine->id);
839 if (IS_ERR(tsk))
840 return PTR_ERR(tsk);
841
842 b->signaler = tsk;
843
Chris Wilson688e6c72016-07-01 17:23:15 +0100844 return 0;
845}
846
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100847static void cancel_fake_irq(struct intel_engine_cs *engine)
848{
849 struct intel_breadcrumbs *b = &engine->breadcrumbs;
850
851 del_timer_sync(&b->hangcheck);
852 del_timer_sync(&b->fake_irq);
853 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
854}
855
856void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
857{
858 struct intel_breadcrumbs *b = &engine->breadcrumbs;
859
860 cancel_fake_irq(engine);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000861 spin_lock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100862
Chris Wilson67b807a82017-02-27 20:58:50 +0000863 if (b->irq_enabled)
864 irq_enable(engine);
865 else
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100866 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000867
868 /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
869 * GPU is active and may have already executed the MI_USER_INTERRUPT
870 * before the CPU is ready to receive. However, the engine is currently
871 * idle (we haven't started it yet), there is no possibility for a
872 * missed interrupt as we enabled the irq and so we can clear the
873 * immediate wakeup (until a real interrupt arrives for the waiter).
874 */
875 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
876
877 if (b->irq_armed)
878 enable_fake_irq(b);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100879
Chris Wilson61d3dc72017-03-03 19:08:24 +0000880 spin_unlock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100881}
882
Chris Wilson688e6c72016-07-01 17:23:15 +0100883void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
884{
885 struct intel_breadcrumbs *b = &engine->breadcrumbs;
886
Chris Wilson381744f2016-11-21 11:07:59 +0000887 /* The engines should be idle and all requests accounted for! */
Chris Wilson61d3dc72017-03-03 19:08:24 +0000888 WARN_ON(READ_ONCE(b->irq_wait));
Chris Wilson381744f2016-11-21 11:07:59 +0000889 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncced5e22017-02-23 07:44:15 +0000890 WARN_ON(rcu_access_pointer(b->first_signal));
Chris Wilson381744f2016-11-21 11:07:59 +0000891 WARN_ON(!RB_EMPTY_ROOT(&b->signals));
892
Chris Wilsonc81d4612016-07-01 17:23:25 +0100893 if (!IS_ERR_OR_NULL(b->signaler))
894 kthread_stop(b->signaler);
895
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100896 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100897}
898
Chris Wilson9b6586a2017-02-23 07:44:08 +0000899bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100900{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000901 struct intel_breadcrumbs *b = &engine->breadcrumbs;
902 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100903
Chris Wilson61d3dc72017-03-03 19:08:24 +0000904 spin_lock_irq(&b->rb_lock);
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000905
Chris Wilson61d3dc72017-03-03 19:08:24 +0000906 if (b->irq_wait) {
907 wake_up_process(b->irq_wait->tsk);
Chris Wilson4bd66392017-03-15 21:07:22 +0000908 busy = true;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100909 }
910
Chris Wilsoncced5e22017-02-23 07:44:15 +0000911 if (rcu_access_pointer(b->first_signal)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000912 wake_up_process(b->signaler);
Chris Wilson4bd66392017-03-15 21:07:22 +0000913 busy = true;
Chris Wilson9b6586a2017-02-23 07:44:08 +0000914 }
915
Chris Wilson61d3dc72017-03-03 19:08:24 +0000916 spin_unlock_irq(&b->rb_lock);
Chris Wilson9b6586a2017-02-23 07:44:08 +0000917
918 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100919}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000920
921#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
922#include "selftests/intel_breadcrumbs.c"
923#endif