blob: 03bbc1dfbc51bd4a12e0c5dea4f8750c4f784c50 [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 */
Tvrtko Ursulind4ccceb2018-03-02 18:14:56 +0200171 if (engine->irq_enable) {
172 spin_lock(&engine->i915->irq_lock);
173 engine->irq_enable(engine);
174 spin_unlock(&engine->i915->irq_lock);
175 }
Chris Wilson688e6c72016-07-01 17:23:15 +0100176}
177
178static void irq_disable(struct intel_engine_cs *engine)
179{
Chris Wilsonf6168e32016-10-28 13:58:55 +0100180 /* Caller disables interrupts */
Tvrtko Ursulind4ccceb2018-03-02 18:14:56 +0200181 if (engine->irq_disable) {
182 spin_lock(&engine->i915->irq_lock);
183 engine->irq_disable(engine);
184 spin_unlock(&engine->i915->irq_lock);
185 }
Chris Wilson688e6c72016-07-01 17:23:15 +0100186}
187
Chris Wilson67b807a82017-02-27 20:58:50 +0000188void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
189{
190 struct intel_breadcrumbs *b = &engine->breadcrumbs;
191
Chris Wilson61d3dc72017-03-03 19:08:24 +0000192 lockdep_assert_held(&b->irq_lock);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000193 GEM_BUG_ON(b->irq_wait);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100194 GEM_BUG_ON(!b->irq_armed);
Chris Wilson67b807a82017-02-27 20:58:50 +0000195
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100196 GEM_BUG_ON(!b->irq_enabled);
197 if (!--b->irq_enabled)
Chris Wilson67b807a82017-02-27 20:58:50 +0000198 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000199
200 b->irq_armed = false;
201}
202
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100203void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
204{
205 struct intel_breadcrumbs *b = &engine->breadcrumbs;
206
207 spin_lock_irq(&b->irq_lock);
208 if (!b->irq_enabled++)
209 irq_enable(engine);
210 GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
211 spin_unlock_irq(&b->irq_lock);
212}
213
214void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
215{
216 struct intel_breadcrumbs *b = &engine->breadcrumbs;
217
218 spin_lock_irq(&b->irq_lock);
219 GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
220 if (!--b->irq_enabled)
221 irq_disable(engine);
222 spin_unlock_irq(&b->irq_lock);
223}
224
Chris Wilson67b807a82017-02-27 20:58:50 +0000225void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
226{
227 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson832265d2017-12-08 01:23:01 +0000228 struct intel_wait *wait, *n;
Chris Wilson67b807a82017-02-27 20:58:50 +0000229
230 if (!b->irq_armed)
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000231 return;
Chris Wilson67b807a82017-02-27 20:58:50 +0000232
Chris Wilson832265d2017-12-08 01:23:01 +0000233 /*
234 * We only disarm the irq when we are idle (all requests completed),
Chris Wilsone1c0c912017-03-06 09:29:15 +0000235 * so if the bottom-half remains asleep, it missed the request
Chris Wilson67b807a82017-02-27 20:58:50 +0000236 * completion.
237 */
Chris Wilson832265d2017-12-08 01:23:01 +0000238 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP)
239 missed_breadcrumb(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000240
Chris Wilsone1c0c912017-03-06 09:29:15 +0000241 spin_lock_irq(&b->rb_lock);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000242
243 spin_lock(&b->irq_lock);
Chris Wilson832265d2017-12-08 01:23:01 +0000244 b->irq_wait = NULL;
Chris Wilsone5330ac2017-10-31 12:22:35 +0000245 if (b->irq_armed)
246 __intel_engine_disarm_breadcrumbs(engine);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000247 spin_unlock(&b->irq_lock);
248
Chris Wilsone1c0c912017-03-06 09:29:15 +0000249 rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
250 RB_CLEAR_NODE(&wait->node);
Chris Wilson832265d2017-12-08 01:23:01 +0000251 wake_up_process(wait->tsk);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000252 }
253 b->waiters = RB_ROOT;
254
Chris Wilsone1c0c912017-03-06 09:29:15 +0000255 spin_unlock_irq(&b->rb_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000256}
257
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000258static bool use_fake_irq(const struct intel_breadcrumbs *b)
259{
260 const struct intel_engine_cs *engine =
261 container_of(b, struct intel_engine_cs, breadcrumbs);
262
263 if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
264 return false;
265
266 /* Only start with the heavy weight fake irq timer if we have not
267 * seen any interrupts since enabling it the first time. If the
268 * interrupts are still arriving, it means we made a mistake in our
269 * engine->seqno_barrier(), a timing error that should be transient
270 * and unlikely to reoccur.
271 */
272 return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
273}
274
Chris Wilson67b807a82017-02-27 20:58:50 +0000275static void enable_fake_irq(struct intel_breadcrumbs *b)
276{
277 /* Ensure we never sleep indefinitely */
278 if (!b->irq_enabled || use_fake_irq(b))
279 mod_timer(&b->fake_irq, jiffies + 1);
280 else
281 mod_timer(&b->hangcheck, wait_timeout());
282}
283
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100284static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
Chris Wilson688e6c72016-07-01 17:23:15 +0100285{
286 struct intel_engine_cs *engine =
287 container_of(b, struct intel_engine_cs, breadcrumbs);
288 struct drm_i915_private *i915 = engine->i915;
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100289 bool enabled;
290
Chris Wilson61d3dc72017-03-03 19:08:24 +0000291 lockdep_assert_held(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000292 if (b->irq_armed)
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100293 return false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100294
Chris Wilson67b807a82017-02-27 20:58:50 +0000295 /* The breadcrumb irq will be disarmed on the interrupt after the
296 * waiters are signaled. This gives us a single interrupt window in
297 * which we can add a new waiter and avoid the cost of re-enabling
298 * the irq.
299 */
300 b->irq_armed = true;
Chris Wilson67b807a82017-02-27 20:58:50 +0000301
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000302 if (I915_SELFTEST_ONLY(b->mock)) {
303 /* For our mock objects we want to avoid interaction
304 * with the real hardware (which is not set up). So
305 * we simply pretend we have enabled the powerwell
306 * and the irq, and leave it up to the mock
307 * implementation to call intel_engine_wakeup()
308 * itself when it wants to simulate a user interrupt,
309 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100310 return true;
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000311 }
312
Chris Wilson688e6c72016-07-01 17:23:15 +0100313 /* Since we are waiting on a request, the GPU should be busy
Chris Wilson67b807a82017-02-27 20:58:50 +0000314 * and should have its own rpm reference. This is tracked
315 * by i915->gt.awake, we can forgo holding our own wakref
316 * for the interrupt as before i915->gt.awake is released (when
317 * the driver is idle) we disarm the breadcrumbs.
Chris Wilson688e6c72016-07-01 17:23:15 +0100318 */
Chris Wilson688e6c72016-07-01 17:23:15 +0100319
320 /* No interrupts? Kick the waiter every jiffie! */
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100321 enabled = false;
322 if (!b->irq_enabled++ &&
323 !test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
324 irq_enable(engine);
325 enabled = true;
Chris Wilson688e6c72016-07-01 17:23:15 +0100326 }
327
Chris Wilson67b807a82017-02-27 20:58:50 +0000328 enable_fake_irq(b);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100329 return enabled;
Chris Wilson688e6c72016-07-01 17:23:15 +0100330}
331
332static inline struct intel_wait *to_wait(struct rb_node *node)
333{
Chris Wilsond8567862016-12-20 10:40:03 +0000334 return rb_entry(node, struct intel_wait, node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100335}
336
337static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
338 struct intel_wait *wait)
339{
Chris Wilson61d3dc72017-03-03 19:08:24 +0000340 lockdep_assert_held(&b->rb_lock);
Chris Wilson908a6cb2017-03-15 21:07:25 +0000341 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100342
Chris Wilsoncd46c542018-02-22 09:25:44 +0000343 /*
344 * This request is completed, so remove it from the tree, mark it as
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000345 * complete, and *then* wake up the associated task. N.B. when the
346 * task wakes up, it will find the empty rb_node, discern that it
347 * has already been removed from the tree and skip the serialisation
348 * of the b->rb_lock and b->irq_lock. This means that the destruction
349 * of the intel_wait is not serialised with the interrupt handler
350 * by the waiter - it must instead be serialised by the caller.
Chris Wilson688e6c72016-07-01 17:23:15 +0100351 */
352 rb_erase(&wait->node, &b->waiters);
353 RB_CLEAR_NODE(&wait->node);
354
Chris Wilsoncd46c542018-02-22 09:25:44 +0000355 if (wait->tsk->state != TASK_RUNNING)
356 wake_up_process(wait->tsk); /* implicit smp_wmb() */
Chris Wilson688e6c72016-07-01 17:23:15 +0100357}
358
Chris Wilsonb66255f2017-03-03 17:14:22 +0000359static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
360 struct rb_node *next)
361{
362 struct intel_breadcrumbs *b = &engine->breadcrumbs;
363
Chris Wilson61d3dc72017-03-03 19:08:24 +0000364 spin_lock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000365 GEM_BUG_ON(!b->irq_armed);
Chris Wilson429732e2017-03-15 21:07:23 +0000366 GEM_BUG_ON(!b->irq_wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000367 b->irq_wait = to_wait(next);
368 spin_unlock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000369
370 /* We always wake up the next waiter that takes over as the bottom-half
371 * as we may delegate not only the irq-seqno barrier to the next waiter
372 * but also the task of waking up concurrent waiters.
373 */
374 if (next)
375 wake_up_process(to_wait(next)->tsk);
376}
377
Chris Wilson688e6c72016-07-01 17:23:15 +0100378static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
379 struct intel_wait *wait)
380{
381 struct intel_breadcrumbs *b = &engine->breadcrumbs;
382 struct rb_node **p, *parent, *completed;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100383 bool first, armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100384 u32 seqno;
385
Chris Wilsonc68ce692018-01-02 19:25:00 +0000386 GEM_BUG_ON(!wait->seqno);
387
Chris Wilson688e6c72016-07-01 17:23:15 +0100388 /* Insert the request into the retirement ordered list
389 * of waiters by walking the rbtree. If we are the oldest
390 * seqno in the tree (the first to be retired), then
391 * set ourselves as the bottom-half.
392 *
393 * As we descend the tree, prune completed branches since we hold the
394 * spinlock we know that the first_waiter must be delayed and can
395 * reduce some of the sequential wake up latency if we take action
396 * ourselves and wake up the completed tasks in parallel. Also, by
397 * removing stale elements in the tree, we may be able to reduce the
398 * ping-pong between the old bottom-half and ourselves as first-waiter.
399 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100400 armed = false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100401 first = true;
402 parent = NULL;
403 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100404 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100405
406 /* If the request completed before we managed to grab the spinlock,
407 * return now before adding ourselves to the rbtree. We let the
408 * current bottom-half handle any pending wakeups and instead
409 * try and get out of the way quickly.
410 */
411 if (i915_seqno_passed(seqno, wait->seqno)) {
412 RB_CLEAR_NODE(&wait->node);
413 return first;
414 }
415
416 p = &b->waiters.rb_node;
417 while (*p) {
418 parent = *p;
419 if (wait->seqno == to_wait(parent)->seqno) {
420 /* We have multiple waiters on the same seqno, select
421 * the highest priority task (that with the smallest
422 * task->prio) to serve as the bottom-half for this
423 * group.
424 */
425 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
426 p = &parent->rb_right;
427 first = false;
428 } else {
429 p = &parent->rb_left;
430 }
431 } else if (i915_seqno_passed(wait->seqno,
432 to_wait(parent)->seqno)) {
433 p = &parent->rb_right;
434 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
435 completed = parent;
436 else
437 first = false;
438 } else {
439 p = &parent->rb_left;
440 }
441 }
442 rb_link_node(&wait->node, parent, p);
443 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100444
Chris Wilson688e6c72016-07-01 17:23:15 +0100445 if (first) {
Chris Wilson61d3dc72017-03-03 19:08:24 +0000446 spin_lock(&b->irq_lock);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000447 b->irq_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100448 /* After assigning ourselves as the new bottom-half, we must
449 * perform a cursory check to prevent a missed interrupt.
450 * Either we miss the interrupt whilst programming the hardware,
451 * or if there was a previous waiter (for a later seqno) they
452 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100453 * in the unlocked read of b->irq_seqno_bh in the irq handler)
454 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100455 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100456 armed = __intel_breadcrumbs_enable_irq(b);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000457 spin_unlock(&b->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100458 }
Chris Wilson429732e2017-03-15 21:07:23 +0000459
460 if (completed) {
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000461 /* Advance the bottom-half (b->irq_wait) before we wake up
462 * the waiters who may scribble over their intel_wait
463 * just as the interrupt handler is dereferencing it via
464 * b->irq_wait.
465 */
Chris Wilson429732e2017-03-15 21:07:23 +0000466 if (!first) {
467 struct rb_node *next = rb_next(completed);
468 GEM_BUG_ON(next == &wait->node);
469 __intel_breadcrumbs_next(engine, next);
470 }
471
472 do {
473 struct intel_wait *crumb = to_wait(completed);
474 completed = rb_prev(completed);
475 __intel_breadcrumbs_finish(b, crumb);
476 } while (completed);
477 }
478
Chris Wilson61d3dc72017-03-03 19:08:24 +0000479 GEM_BUG_ON(!b->irq_wait);
Chris Wilson429732e2017-03-15 21:07:23 +0000480 GEM_BUG_ON(!b->irq_armed);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000481 GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100482
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100483 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100484}
485
486bool intel_engine_add_wait(struct intel_engine_cs *engine,
487 struct intel_wait *wait)
488{
489 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100490 bool armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100491
Chris Wilson61d3dc72017-03-03 19:08:24 +0000492 spin_lock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100493 armed = __intel_engine_add_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000494 spin_unlock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100495 if (armed)
496 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100497
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100498 /* Make the caller recheck if its request has already started. */
499 return i915_seqno_passed(intel_engine_get_seqno(engine),
500 wait->seqno - 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100501}
502
Chris Wilson688e6c72016-07-01 17:23:15 +0100503static inline bool chain_wakeup(struct rb_node *rb, int priority)
504{
505 return rb && to_wait(rb)->tsk->prio <= priority;
506}
507
Chris Wilsonc81d4612016-07-01 17:23:25 +0100508static inline int wakeup_priority(struct intel_breadcrumbs *b,
509 struct task_struct *tsk)
510{
511 if (tsk == b->signaler)
512 return INT_MIN;
513 else
514 return tsk->prio;
515}
516
Chris Wilson9eb143b2017-02-23 07:44:16 +0000517static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
518 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100519{
520 struct intel_breadcrumbs *b = &engine->breadcrumbs;
521
Chris Wilson61d3dc72017-03-03 19:08:24 +0000522 lockdep_assert_held(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100523
524 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000525 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100526
Chris Wilson61d3dc72017-03-03 19:08:24 +0000527 if (b->irq_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100528 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100529 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100530
Chris Wilson688e6c72016-07-01 17:23:15 +0100531 /* We are the current bottom-half. Find the next candidate,
532 * the first waiter in the queue on the remaining oldest
533 * request. As multiple seqnos may complete in the time it
534 * takes us to wake up and find the next waiter, we have to
535 * wake up that waiter for it to perform its own coherent
536 * completion check.
537 */
538 next = rb_next(&wait->node);
539 if (chain_wakeup(next, priority)) {
540 /* If the next waiter is already complete,
541 * wake it up and continue onto the next waiter. So
542 * if have a small herd, they will wake up in parallel
543 * rather than sequentially, which should reduce
544 * the overall latency in waking all the completed
545 * clients.
546 *
547 * However, waking up a chain adds extra latency to
548 * the first_waiter. This is undesirable if that
549 * waiter is a high priority task.
550 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100551 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100552
553 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
554 struct rb_node *n = rb_next(next);
555
556 __intel_breadcrumbs_finish(b, to_wait(next));
557 next = n;
558 if (!chain_wakeup(next, priority))
559 break;
560 }
561 }
562
Chris Wilsonb66255f2017-03-03 17:14:22 +0000563 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100564 } else {
565 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
566 }
567
568 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
569 rb_erase(&wait->node, &b->waiters);
Chris Wilsonc5346122017-11-15 12:14:58 +0000570 RB_CLEAR_NODE(&wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100571
Chris Wilson9eb143b2017-02-23 07:44:16 +0000572out:
Chris Wilson61d3dc72017-03-03 19:08:24 +0000573 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100574 GEM_BUG_ON(rb_first(&b->waiters) !=
Chris Wilson61d3dc72017-03-03 19:08:24 +0000575 (b->irq_wait ? &b->irq_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000576}
577
578void intel_engine_remove_wait(struct intel_engine_cs *engine,
579 struct intel_wait *wait)
580{
581 struct intel_breadcrumbs *b = &engine->breadcrumbs;
582
583 /* Quick check to see if this waiter was already decoupled from
584 * the tree by the bottom-half to avoid contention on the spinlock
585 * by the herd.
586 */
Chris Wilson908a6cb2017-03-15 21:07:25 +0000587 if (RB_EMPTY_NODE(&wait->node)) {
588 GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000589 return;
Chris Wilson908a6cb2017-03-15 21:07:25 +0000590 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000591
Chris Wilson61d3dc72017-03-03 19:08:24 +0000592 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000593 __intel_engine_remove_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000594 spin_unlock_irq(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100595}
596
Chris Wilsonc81d4612016-07-01 17:23:25 +0100597static void signaler_set_rtpriority(void)
598{
599 struct sched_param param = { .sched_priority = 1 };
600
601 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
602}
603
604static int intel_breadcrumbs_signaler(void *arg)
605{
606 struct intel_engine_cs *engine = arg;
607 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsoncd46c542018-02-22 09:25:44 +0000608 struct i915_request *rq, *n;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100609
610 /* Install ourselves with high priority to reduce signalling latency */
611 signaler_set_rtpriority();
612
613 do {
Chris Wilsona7980a62017-04-04 13:05:31 +0100614 bool do_schedule = true;
Chris Wilsoncd46c542018-02-22 09:25:44 +0000615 LIST_HEAD(list);
616 u32 seqno;
Chris Wilsona7980a62017-04-04 13:05:31 +0100617
Chris Wilsonc81d4612016-07-01 17:23:25 +0100618 set_current_state(TASK_INTERRUPTIBLE);
Chris Wilsoncd46c542018-02-22 09:25:44 +0000619 if (list_empty(&b->signals))
620 goto sleep;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100621
Chris Wilsoncd46c542018-02-22 09:25:44 +0000622 /*
623 * We are either woken up by the interrupt bottom-half,
Chris Wilsonc81d4612016-07-01 17:23:25 +0100624 * 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 Wilsoncd46c542018-02-22 09:25:44 +0000631 seqno = intel_engine_get_seqno(engine);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000632
Chris Wilsoncd46c542018-02-22 09:25:44 +0000633 spin_lock_irq(&b->rb_lock);
634 list_for_each_entry_safe(rq, n, &b->signals, signaling.link) {
635 u32 this = rq->signaling.wait.seqno;
Chris Wilsona7980a62017-04-04 13:05:31 +0100636
Chris Wilsoncd46c542018-02-22 09:25:44 +0000637 GEM_BUG_ON(!rq->signaling.wait.seqno);
638
639 if (!i915_seqno_passed(seqno, this))
640 break;
641
642 if (likely(this == i915_request_global_seqno(rq))) {
643 __intel_engine_remove_wait(engine,
644 &rq->signaling.wait);
645
646 rq->signaling.wait.seqno = 0;
647 __list_del_entry(&rq->signaling.link);
648
649 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
650 &rq->fence.flags)) {
651 list_add_tail(&rq->signaling.link,
652 &list);
653 i915_request_get(rq);
654 }
655 }
656 }
657 spin_unlock_irq(&b->rb_lock);
658
659 if (!list_empty(&list)) {
660 local_bh_disable();
661 list_for_each_entry_safe(rq, n, &list, signaling.link) {
662 dma_fence_signal(&rq->fence);
663 GEM_BUG_ON(!i915_request_completed(rq));
664 i915_request_put(rq);
665 }
666 local_bh_enable(); /* kick start the tasklets */
667
668 /*
669 * If the engine is saturated we may be continually
Chris Wilsona7980a62017-04-04 13:05:31 +0100670 * processing completed requests. This angers the
671 * NMI watchdog if we never let anything else
672 * have access to the CPU. Let's pretend to be nice
673 * and relinquish the CPU if we burn through the
674 * entire RT timeslice!
675 */
676 do_schedule = need_resched();
677 }
678
679 if (unlikely(do_schedule)) {
Chris Wilsoncd46c542018-02-22 09:25:44 +0000680 /* Before we sleep, check for a missed seqno */
681 if (current->state & TASK_NORMAL &&
682 !list_empty(&b->signals) &&
683 engine->irq_seqno_barrier &&
684 test_and_clear_bit(ENGINE_IRQ_BREADCRUMB,
685 &engine->irq_posted)) {
686 engine->irq_seqno_barrier(engine);
687 intel_engine_wakeup(engine);
688 }
689
690sleep:
Chris Wilsonb1becb82017-04-03 11:51:24 +0100691 if (kthread_should_park())
692 kthread_parkme();
693
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000694 if (unlikely(kthread_should_stop()))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100695 break;
696
697 schedule();
698 }
699 } while (1);
700 __set_current_state(TASK_RUNNING);
701
702 return 0;
703}
704
Chris Wilsoncd46c542018-02-22 09:25:44 +0000705static void insert_signal(struct intel_breadcrumbs *b,
706 struct i915_request *request,
707 const u32 seqno)
708{
709 struct i915_request *iter;
710
711 lockdep_assert_held(&b->rb_lock);
712
713 /*
714 * A reasonable assumption is that we are called to add signals
715 * in sequence, as the requests are submitted for execution and
716 * assigned a global_seqno. This will be the case for the majority
717 * of internally generated signals (inter-engine signaling).
718 *
719 * Out of order waiters triggering random signaling enabling will
720 * be more problematic, but hopefully rare enough and the list
721 * small enough that the O(N) insertion sort is not an issue.
722 */
723
724 list_for_each_entry_reverse(iter, &b->signals, signaling.link)
725 if (i915_seqno_passed(seqno, iter->signaling.wait.seqno))
726 break;
727
728 list_add(&request->signaling.link, &iter->signaling.link);
729}
730
Chris Wilsone61e0f52018-02-21 09:56:36 +0000731void intel_engine_enable_signaling(struct i915_request *request, bool wakeup)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100732{
733 struct intel_engine_cs *engine = request->engine;
734 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000735 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100736
Chris Wilsoncd46c542018-02-22 09:25:44 +0000737 /*
738 * Note that we may be called from an interrupt handler on another
Chris Wilsonf6168e32016-10-28 13:58:55 +0100739 * device (e.g. nouveau signaling a fence completion causing us
740 * to submit a request, and so enable signaling). As such,
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000741 * we need to make sure that all other users of b->rb_lock protect
Chris Wilsonf6168e32016-10-28 13:58:55 +0100742 * against interrupts, i.e. use spin_lock_irqsave.
743 */
744
745 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilsone60a8702017-03-02 11:51:30 +0000746 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000747 lockdep_assert_held(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000748
Chris Wilsone61e0f52018-02-21 09:56:36 +0000749 seqno = i915_request_global_seqno(request);
Chris Wilsoncd46c542018-02-22 09:25:44 +0000750 if (!seqno) /* will be enabled later upon execution */
Chris Wilson65e47602016-10-28 13:58:49 +0100751 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100752
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000753 GEM_BUG_ON(request->signaling.wait.seqno);
Chris Wilsonb3850852016-07-01 17:23:26 +0100754 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000755 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000756 request->signaling.wait.seqno = seqno;
Chris Wilson4a50d202016-07-26 12:01:50 +0100757
Chris Wilsoncd46c542018-02-22 09:25:44 +0000758 /*
759 * Add ourselves into the list of waiters, but registering our
Chris Wilsonc81d4612016-07-01 17:23:25 +0100760 * bottom-half as the signaller thread. As per usual, only the oldest
761 * waiter (not just signaller) is tasked as the bottom-half waking
762 * up all completed waiters after the user interrupt.
763 *
764 * If we are the oldest waiter, enable the irq (after which we
765 * must double check that the seqno did not complete).
766 */
Chris Wilsoncd46c542018-02-22 09:25:44 +0000767 spin_lock(&b->rb_lock);
768 insert_signal(b, request, seqno);
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100769 wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000770 spin_unlock(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100771
772 if (wakeup)
773 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100774}
775
Chris Wilsone61e0f52018-02-21 09:56:36 +0000776void intel_engine_cancel_signaling(struct i915_request *request)
Chris Wilson9eb143b2017-02-23 07:44:16 +0000777{
Chris Wilsoncd46c542018-02-22 09:25:44 +0000778 struct intel_engine_cs *engine = request->engine;
779 struct intel_breadcrumbs *b = &engine->breadcrumbs;
780
Chris Wilsone60a8702017-03-02 11:51:30 +0000781 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000782 lockdep_assert_held(&request->lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000783
Chris Wilsoncd46c542018-02-22 09:25:44 +0000784 if (!READ_ONCE(request->signaling.wait.seqno))
785 return;
Chris Wilson9eb143b2017-02-23 07:44:16 +0000786
Chris Wilsoncd46c542018-02-22 09:25:44 +0000787 spin_lock(&b->rb_lock);
788 __intel_engine_remove_wait(engine, &request->signaling.wait);
789 if (fetch_and_zero(&request->signaling.wait.seqno))
790 __list_del_entry(&request->signaling.link);
791 spin_unlock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000792}
793
Chris Wilson688e6c72016-07-01 17:23:15 +0100794int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
795{
796 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100797 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100798
Chris Wilson61d3dc72017-03-03 19:08:24 +0000799 spin_lock_init(&b->rb_lock);
800 spin_lock_init(&b->irq_lock);
801
Kees Cook39cbf2a2017-10-17 09:53:04 +0300802 timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
803 timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
Chris Wilson688e6c72016-07-01 17:23:15 +0100804
Chris Wilsoncd46c542018-02-22 09:25:44 +0000805 INIT_LIST_HEAD(&b->signals);
806
Chris Wilsonc81d4612016-07-01 17:23:25 +0100807 /* Spawn a thread to provide a common bottom-half for all signals.
808 * As this is an asynchronous interface we cannot steal the current
809 * task for handling the bottom-half to the user interrupt, therefore
810 * we create a thread to do the coherent seqno dance after the
811 * interrupt and then signal the waitqueue (via the dma-buf/fence).
812 */
813 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
814 "i915/signal:%d", engine->id);
815 if (IS_ERR(tsk))
816 return PTR_ERR(tsk);
817
818 b->signaler = tsk;
819
Chris Wilson688e6c72016-07-01 17:23:15 +0100820 return 0;
821}
822
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100823static void cancel_fake_irq(struct intel_engine_cs *engine)
824{
825 struct intel_breadcrumbs *b = &engine->breadcrumbs;
826
827 del_timer_sync(&b->hangcheck);
828 del_timer_sync(&b->fake_irq);
829 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
830}
831
832void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
833{
834 struct intel_breadcrumbs *b = &engine->breadcrumbs;
835
836 cancel_fake_irq(engine);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000837 spin_lock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100838
Chris Wilson67b807a82017-02-27 20:58:50 +0000839 if (b->irq_enabled)
840 irq_enable(engine);
841 else
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100842 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000843
844 /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
845 * GPU is active and may have already executed the MI_USER_INTERRUPT
846 * before the CPU is ready to receive. However, the engine is currently
847 * idle (we haven't started it yet), there is no possibility for a
848 * missed interrupt as we enabled the irq and so we can clear the
849 * immediate wakeup (until a real interrupt arrives for the waiter).
850 */
851 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
852
853 if (b->irq_armed)
854 enable_fake_irq(b);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100855
Chris Wilson61d3dc72017-03-03 19:08:24 +0000856 spin_unlock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100857}
858
Chris Wilson688e6c72016-07-01 17:23:15 +0100859void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
860{
861 struct intel_breadcrumbs *b = &engine->breadcrumbs;
862
Chris Wilson381744f2016-11-21 11:07:59 +0000863 /* The engines should be idle and all requests accounted for! */
Chris Wilson61d3dc72017-03-03 19:08:24 +0000864 WARN_ON(READ_ONCE(b->irq_wait));
Chris Wilson381744f2016-11-21 11:07:59 +0000865 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncd46c542018-02-22 09:25:44 +0000866 WARN_ON(!list_empty(&b->signals));
Chris Wilson381744f2016-11-21 11:07:59 +0000867
Chris Wilsonc81d4612016-07-01 17:23:25 +0100868 if (!IS_ERR_OR_NULL(b->signaler))
869 kthread_stop(b->signaler);
870
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100871 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100872}
873
Chris Wilson9b6586a2017-02-23 07:44:08 +0000874bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100875{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000876 struct intel_breadcrumbs *b = &engine->breadcrumbs;
877 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100878
Chris Wilson61d3dc72017-03-03 19:08:24 +0000879 if (b->irq_wait) {
Chris Wilsoncd46c542018-02-22 09:25:44 +0000880 spin_lock_irq(&b->irq_lock);
881
882 if (b->irq_wait) {
883 wake_up_process(b->irq_wait->tsk);
884 busy = true;
885 }
886
887 spin_unlock_irq(&b->irq_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100888 }
889
Chris Wilsoncd46c542018-02-22 09:25:44 +0000890 if (!busy && !list_empty(&b->signals)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000891 wake_up_process(b->signaler);
Chris Wilson4bd66392017-03-15 21:07:22 +0000892 busy = true;
Chris Wilson9b6586a2017-02-23 07:44:08 +0000893 }
894
Chris Wilson9b6586a2017-02-23 07:44:08 +0000895 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100896}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000897
898#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
899#include "selftests/intel_breadcrumbs.c"
900#endif