blob: 5f8b9f1f40f19e84968c18e5fbd229731b392dea [file] [log] [blame]
Chris Wilson688e6c72016-07-01 17:23:15 +01001/*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
Chris Wilsonc81d4612016-07-01 17:23:25 +010025#include <linux/kthread.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010026#include <uapi/linux/sched/types.h>
Chris Wilsonc81d4612016-07-01 17:23:25 +010027
Chris Wilson688e6c72016-07-01 17:23:15 +010028#include "i915_drv.h"
29
Chris Wilson67b807a82017-02-27 20:58:50 +000030static unsigned int __intel_breadcrumbs_wakeup(struct intel_breadcrumbs *b)
Chris Wilson8d769ea2017-02-27 20:58:47 +000031{
Chris Wilson56299fb2017-02-27 20:58:48 +000032 struct intel_wait *wait;
Chris Wilson8d769ea2017-02-27 20:58:47 +000033 unsigned int result = 0;
34
Chris Wilson61d3dc72017-03-03 19:08:24 +000035 lockdep_assert_held(&b->irq_lock);
36
37 wait = b->irq_wait;
Chris Wilson56299fb2017-02-27 20:58:48 +000038 if (wait) {
Chris Wilson8d769ea2017-02-27 20:58:47 +000039 result = ENGINE_WAKEUP_WAITER;
Chris Wilson67b807a82017-02-27 20:58:50 +000040 if (wake_up_process(wait->tsk))
41 result |= ENGINE_WAKEUP_ASLEEP;
Chris Wilson8d769ea2017-02-27 20:58:47 +000042 }
Chris Wilson67b807a82017-02-27 20:58:50 +000043
44 return result;
45}
46
47unsigned int intel_engine_wakeup(struct intel_engine_cs *engine)
48{
49 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson467221b2017-03-20 14:31:33 +000050 unsigned long flags;
Chris Wilson67b807a82017-02-27 20:58:50 +000051 unsigned int result;
52
Chris Wilson467221b2017-03-20 14:31:33 +000053 spin_lock_irqsave(&b->irq_lock, flags);
Chris Wilson67b807a82017-02-27 20:58:50 +000054 result = __intel_breadcrumbs_wakeup(b);
Chris Wilson467221b2017-03-20 14:31:33 +000055 spin_unlock_irqrestore(&b->irq_lock, flags);
Chris Wilson8d769ea2017-02-27 20:58:47 +000056
57 return result;
58}
59
Chris Wilson2246bea2017-02-17 15:13:00 +000060static unsigned long wait_timeout(void)
61{
62 return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
63}
64
Chris Wilson80166e402017-02-28 08:50:18 +000065static noinline void missed_breadcrumb(struct intel_engine_cs *engine)
66{
Helge Deller516726d2017-09-06 22:27:52 +020067 DRM_DEBUG_DRIVER("%s missed breadcrumb at %pS, irq posted? %s, current seqno=%x, last=%x\n",
Chris Wilson80166e402017-02-28 08:50:18 +000068 engine->name, __builtin_return_address(0),
69 yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
Chris Wilson695eaa32017-04-23 18:06:19 +010070 &engine->irq_posted)),
71 intel_engine_get_seqno(engine),
72 intel_engine_last_submit(engine));
Chris Wilson80166e402017-02-28 08:50:18 +000073
74 set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
75}
76
Kees Cook39cbf2a2017-10-17 09:53:04 +030077static void intel_breadcrumbs_hangcheck(struct timer_list *t)
Chris Wilson83348ba2016-08-09 17:47:51 +010078{
Kees Cook39cbf2a2017-10-17 09:53:04 +030079 struct intel_engine_cs *engine = from_timer(engine, t,
80 breadcrumbs.hangcheck);
Chris Wilson83348ba2016-08-09 17:47:51 +010081 struct intel_breadcrumbs *b = &engine->breadcrumbs;
82
Chris Wilson67b807a82017-02-27 20:58:50 +000083 if (!b->irq_armed)
Chris Wilson83348ba2016-08-09 17:47:51 +010084 return;
85
Chris Wilson2246bea2017-02-17 15:13:00 +000086 if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) {
87 b->hangcheck_interrupts = atomic_read(&engine->irq_count);
88 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson83348ba2016-08-09 17:47:51 +010089 return;
90 }
91
Chris Wilsona6b0a1412017-03-15 22:22:59 +000092 /* We keep the hangcheck timer alive until we disarm the irq, even
Chris Wilson67b807a82017-02-27 20:58:50 +000093 * if there are no waiters at present.
94 *
95 * If the waiter was currently running, assume it hasn't had a chance
Chris Wilson89985672017-02-17 15:13:02 +000096 * to process the pending interrupt (e.g, low priority task on a loaded
97 * system) and wait until it sleeps before declaring a missed interrupt.
Chris Wilson67b807a82017-02-27 20:58:50 +000098 *
99 * If the waiter was asleep (and not even pending a wakeup), then we
100 * must have missed an interrupt as the GPU has stopped advancing
101 * but we still have a waiter. Assuming all batches complete within
102 * DRM_I915_HANGCHECK_JIFFIES [1.5s]!
Chris Wilson89985672017-02-17 15:13:02 +0000103 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000104 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) {
Chris Wilson80166e402017-02-28 08:50:18 +0000105 missed_breadcrumb(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000106 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
107 } else {
Chris Wilson89985672017-02-17 15:13:02 +0000108 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson89985672017-02-17 15:13:02 +0000109 }
Chris Wilson83348ba2016-08-09 17:47:51 +0100110}
111
Kees Cook39cbf2a2017-10-17 09:53:04 +0300112static void intel_breadcrumbs_fake_irq(struct timer_list *t)
Chris Wilson688e6c72016-07-01 17:23:15 +0100113{
Kees Cook39cbf2a2017-10-17 09:53:04 +0300114 struct intel_engine_cs *engine = from_timer(engine, t,
115 breadcrumbs.fake_irq);
Chris Wilson67b807a82017-02-27 20:58:50 +0000116 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson688e6c72016-07-01 17:23:15 +0100117
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000118 /* The timer persists in case we cannot enable interrupts,
Chris Wilson688e6c72016-07-01 17:23:15 +0100119 * or if we have previously seen seqno/interrupt incoherency
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000120 * ("missed interrupt" syndrome, better known as a "missed breadcrumb").
121 * Here the worker will wake up every jiffie in order to kick the
122 * oldest waiter to do the coherent seqno check.
Chris Wilson688e6c72016-07-01 17:23:15 +0100123 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000124
Tvrtko Ursulina9e64932017-03-06 15:03:20 +0000125 spin_lock_irq(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000126 if (!__intel_breadcrumbs_wakeup(b))
127 __intel_engine_disarm_breadcrumbs(engine);
Tvrtko Ursulina9e64932017-03-06 15:03:20 +0000128 spin_unlock_irq(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000129 if (!b->irq_armed)
Chris Wilson19d0a572017-02-27 20:58:49 +0000130 return;
131
Chris Wilson67b807a82017-02-27 20:58:50 +0000132 mod_timer(&b->fake_irq, jiffies + 1);
Chris Wilson19d0a572017-02-27 20:58:49 +0000133
134 /* Ensure that even if the GPU hangs, we get woken up.
135 *
136 * However, note that if no one is waiting, we never notice
137 * a gpu hang. Eventually, we will have to wait for a resource
138 * held by the GPU and so trigger a hangcheck. In the most
139 * pathological case, this will be upon memory starvation! To
140 * prevent this, we also queue the hangcheck from the retire
141 * worker.
142 */
143 i915_queue_hangcheck(engine->i915);
Chris Wilson688e6c72016-07-01 17:23:15 +0100144}
145
146static void irq_enable(struct intel_engine_cs *engine)
147{
Chris Wilson3d5564e2016-07-01 17:23:23 +0100148 /* Enabling the IRQ may miss the generation of the interrupt, but
149 * we still need to force the barrier before reading the seqno,
150 * just in case.
151 */
Chris Wilson538b2572017-01-24 15:18:05 +0000152 set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100153
Chris Wilsonf6168e32016-10-28 13:58:55 +0100154 /* Caller disables interrupts */
155 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100156 engine->irq_enable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100157 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100158}
159
160static void irq_disable(struct intel_engine_cs *engine)
161{
Chris Wilsonf6168e32016-10-28 13:58:55 +0100162 /* Caller disables interrupts */
163 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100164 engine->irq_disable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100165 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100166}
167
Chris Wilson67b807a82017-02-27 20:58:50 +0000168void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
169{
170 struct intel_breadcrumbs *b = &engine->breadcrumbs;
171
Chris Wilson61d3dc72017-03-03 19:08:24 +0000172 lockdep_assert_held(&b->irq_lock);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000173 GEM_BUG_ON(b->irq_wait);
Chris Wilson67b807a82017-02-27 20:58:50 +0000174
175 if (b->irq_enabled) {
176 irq_disable(engine);
177 b->irq_enabled = false;
178 }
179
180 b->irq_armed = false;
181}
182
183void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
184{
185 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000186 struct intel_wait *wait, *n, *first;
Chris Wilson67b807a82017-02-27 20:58:50 +0000187
188 if (!b->irq_armed)
189 return;
190
Chris Wilson67b807a82017-02-27 20:58:50 +0000191 /* We only disarm the irq when we are idle (all requests completed),
Chris Wilsone1c0c912017-03-06 09:29:15 +0000192 * so if the bottom-half remains asleep, it missed the request
Chris Wilson67b807a82017-02-27 20:58:50 +0000193 * completion.
194 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000195
Chris Wilsone1c0c912017-03-06 09:29:15 +0000196 spin_lock_irq(&b->rb_lock);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000197
198 spin_lock(&b->irq_lock);
199 first = fetch_and_zero(&b->irq_wait);
200 __intel_engine_disarm_breadcrumbs(engine);
201 spin_unlock(&b->irq_lock);
202
Chris Wilsone1c0c912017-03-06 09:29:15 +0000203 rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
204 RB_CLEAR_NODE(&wait->node);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000205 if (wake_up_process(wait->tsk) && wait == first)
Chris Wilsone1c0c912017-03-06 09:29:15 +0000206 missed_breadcrumb(engine);
207 }
208 b->waiters = RB_ROOT;
209
Chris Wilsone1c0c912017-03-06 09:29:15 +0000210 spin_unlock_irq(&b->rb_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000211}
212
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000213static bool use_fake_irq(const struct intel_breadcrumbs *b)
214{
215 const struct intel_engine_cs *engine =
216 container_of(b, struct intel_engine_cs, breadcrumbs);
217
218 if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
219 return false;
220
221 /* Only start with the heavy weight fake irq timer if we have not
222 * seen any interrupts since enabling it the first time. If the
223 * interrupts are still arriving, it means we made a mistake in our
224 * engine->seqno_barrier(), a timing error that should be transient
225 * and unlikely to reoccur.
226 */
227 return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
228}
229
Chris Wilson67b807a82017-02-27 20:58:50 +0000230static void enable_fake_irq(struct intel_breadcrumbs *b)
231{
232 /* Ensure we never sleep indefinitely */
233 if (!b->irq_enabled || use_fake_irq(b))
234 mod_timer(&b->fake_irq, jiffies + 1);
235 else
236 mod_timer(&b->hangcheck, wait_timeout());
237}
238
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100239static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
Chris Wilson688e6c72016-07-01 17:23:15 +0100240{
241 struct intel_engine_cs *engine =
242 container_of(b, struct intel_engine_cs, breadcrumbs);
243 struct drm_i915_private *i915 = engine->i915;
Chris Wilson688e6c72016-07-01 17:23:15 +0100244
Chris Wilson61d3dc72017-03-03 19:08:24 +0000245 lockdep_assert_held(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000246 if (b->irq_armed)
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100247 return false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100248
Chris Wilson67b807a82017-02-27 20:58:50 +0000249 /* The breadcrumb irq will be disarmed on the interrupt after the
250 * waiters are signaled. This gives us a single interrupt window in
251 * which we can add a new waiter and avoid the cost of re-enabling
252 * the irq.
253 */
254 b->irq_armed = true;
255 GEM_BUG_ON(b->irq_enabled);
256
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000257 if (I915_SELFTEST_ONLY(b->mock)) {
258 /* For our mock objects we want to avoid interaction
259 * with the real hardware (which is not set up). So
260 * we simply pretend we have enabled the powerwell
261 * and the irq, and leave it up to the mock
262 * implementation to call intel_engine_wakeup()
263 * itself when it wants to simulate a user interrupt,
264 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100265 return true;
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000266 }
267
Chris Wilson688e6c72016-07-01 17:23:15 +0100268 /* Since we are waiting on a request, the GPU should be busy
Chris Wilson67b807a82017-02-27 20:58:50 +0000269 * and should have its own rpm reference. This is tracked
270 * by i915->gt.awake, we can forgo holding our own wakref
271 * for the interrupt as before i915->gt.awake is released (when
272 * the driver is idle) we disarm the breadcrumbs.
Chris Wilson688e6c72016-07-01 17:23:15 +0100273 */
Chris Wilson688e6c72016-07-01 17:23:15 +0100274
275 /* No interrupts? Kick the waiter every jiffie! */
276 if (intel_irqs_enabled(i915)) {
Chris Wilson3d5564e2016-07-01 17:23:23 +0100277 if (!test_bit(engine->id, &i915->gpu_error.test_irq_rings))
Chris Wilson688e6c72016-07-01 17:23:15 +0100278 irq_enable(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100279 b->irq_enabled = true;
280 }
281
Chris Wilson67b807a82017-02-27 20:58:50 +0000282 enable_fake_irq(b);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100283 return true;
Chris Wilson688e6c72016-07-01 17:23:15 +0100284}
285
286static inline struct intel_wait *to_wait(struct rb_node *node)
287{
Chris Wilsond8567862016-12-20 10:40:03 +0000288 return rb_entry(node, struct intel_wait, node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100289}
290
291static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
292 struct intel_wait *wait)
293{
Chris Wilson61d3dc72017-03-03 19:08:24 +0000294 lockdep_assert_held(&b->rb_lock);
Chris Wilson908a6cb2017-03-15 21:07:25 +0000295 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100296
297 /* This request is completed, so remove it from the tree, mark it as
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000298 * complete, and *then* wake up the associated task. N.B. when the
299 * task wakes up, it will find the empty rb_node, discern that it
300 * has already been removed from the tree and skip the serialisation
301 * of the b->rb_lock and b->irq_lock. This means that the destruction
302 * of the intel_wait is not serialised with the interrupt handler
303 * by the waiter - it must instead be serialised by the caller.
Chris Wilson688e6c72016-07-01 17:23:15 +0100304 */
305 rb_erase(&wait->node, &b->waiters);
306 RB_CLEAR_NODE(&wait->node);
307
308 wake_up_process(wait->tsk); /* implicit smp_wmb() */
309}
310
Chris Wilsonb66255f2017-03-03 17:14:22 +0000311static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
312 struct rb_node *next)
313{
314 struct intel_breadcrumbs *b = &engine->breadcrumbs;
315
Chris Wilson61d3dc72017-03-03 19:08:24 +0000316 spin_lock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000317 GEM_BUG_ON(!b->irq_armed);
Chris Wilson429732e2017-03-15 21:07:23 +0000318 GEM_BUG_ON(!b->irq_wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000319 b->irq_wait = to_wait(next);
320 spin_unlock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000321
322 /* We always wake up the next waiter that takes over as the bottom-half
323 * as we may delegate not only the irq-seqno barrier to the next waiter
324 * but also the task of waking up concurrent waiters.
325 */
326 if (next)
327 wake_up_process(to_wait(next)->tsk);
328}
329
Chris Wilson688e6c72016-07-01 17:23:15 +0100330static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
331 struct intel_wait *wait)
332{
333 struct intel_breadcrumbs *b = &engine->breadcrumbs;
334 struct rb_node **p, *parent, *completed;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100335 bool first, armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100336 u32 seqno;
337
338 /* Insert the request into the retirement ordered list
339 * of waiters by walking the rbtree. If we are the oldest
340 * seqno in the tree (the first to be retired), then
341 * set ourselves as the bottom-half.
342 *
343 * As we descend the tree, prune completed branches since we hold the
344 * spinlock we know that the first_waiter must be delayed and can
345 * reduce some of the sequential wake up latency if we take action
346 * ourselves and wake up the completed tasks in parallel. Also, by
347 * removing stale elements in the tree, we may be able to reduce the
348 * ping-pong between the old bottom-half and ourselves as first-waiter.
349 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100350 armed = false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100351 first = true;
352 parent = NULL;
353 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100354 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100355
356 /* If the request completed before we managed to grab the spinlock,
357 * return now before adding ourselves to the rbtree. We let the
358 * current bottom-half handle any pending wakeups and instead
359 * try and get out of the way quickly.
360 */
361 if (i915_seqno_passed(seqno, wait->seqno)) {
362 RB_CLEAR_NODE(&wait->node);
363 return first;
364 }
365
366 p = &b->waiters.rb_node;
367 while (*p) {
368 parent = *p;
369 if (wait->seqno == to_wait(parent)->seqno) {
370 /* We have multiple waiters on the same seqno, select
371 * the highest priority task (that with the smallest
372 * task->prio) to serve as the bottom-half for this
373 * group.
374 */
375 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
376 p = &parent->rb_right;
377 first = false;
378 } else {
379 p = &parent->rb_left;
380 }
381 } else if (i915_seqno_passed(wait->seqno,
382 to_wait(parent)->seqno)) {
383 p = &parent->rb_right;
384 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
385 completed = parent;
386 else
387 first = false;
388 } else {
389 p = &parent->rb_left;
390 }
391 }
392 rb_link_node(&wait->node, parent, p);
393 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100394
Chris Wilson688e6c72016-07-01 17:23:15 +0100395 if (first) {
Chris Wilson61d3dc72017-03-03 19:08:24 +0000396 spin_lock(&b->irq_lock);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000397 b->irq_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100398 /* After assigning ourselves as the new bottom-half, we must
399 * perform a cursory check to prevent a missed interrupt.
400 * Either we miss the interrupt whilst programming the hardware,
401 * or if there was a previous waiter (for a later seqno) they
402 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100403 * in the unlocked read of b->irq_seqno_bh in the irq handler)
404 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100405 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100406 armed = __intel_breadcrumbs_enable_irq(b);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000407 spin_unlock(&b->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100408 }
Chris Wilson429732e2017-03-15 21:07:23 +0000409
410 if (completed) {
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000411 /* Advance the bottom-half (b->irq_wait) before we wake up
412 * the waiters who may scribble over their intel_wait
413 * just as the interrupt handler is dereferencing it via
414 * b->irq_wait.
415 */
Chris Wilson429732e2017-03-15 21:07:23 +0000416 if (!first) {
417 struct rb_node *next = rb_next(completed);
418 GEM_BUG_ON(next == &wait->node);
419 __intel_breadcrumbs_next(engine, next);
420 }
421
422 do {
423 struct intel_wait *crumb = to_wait(completed);
424 completed = rb_prev(completed);
425 __intel_breadcrumbs_finish(b, crumb);
426 } while (completed);
427 }
428
Chris Wilson61d3dc72017-03-03 19:08:24 +0000429 GEM_BUG_ON(!b->irq_wait);
Chris Wilson429732e2017-03-15 21:07:23 +0000430 GEM_BUG_ON(!b->irq_armed);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000431 GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100432
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100433 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100434}
435
436bool intel_engine_add_wait(struct intel_engine_cs *engine,
437 struct intel_wait *wait)
438{
439 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100440 bool armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100441
Chris Wilson61d3dc72017-03-03 19:08:24 +0000442 spin_lock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100443 armed = __intel_engine_add_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000444 spin_unlock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100445 if (armed)
446 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100447
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100448 /* Make the caller recheck if its request has already started. */
449 return i915_seqno_passed(intel_engine_get_seqno(engine),
450 wait->seqno - 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100451}
452
Chris Wilson688e6c72016-07-01 17:23:15 +0100453static inline bool chain_wakeup(struct rb_node *rb, int priority)
454{
455 return rb && to_wait(rb)->tsk->prio <= priority;
456}
457
Chris Wilsonc81d4612016-07-01 17:23:25 +0100458static inline int wakeup_priority(struct intel_breadcrumbs *b,
459 struct task_struct *tsk)
460{
461 if (tsk == b->signaler)
462 return INT_MIN;
463 else
464 return tsk->prio;
465}
466
Chris Wilson9eb143b2017-02-23 07:44:16 +0000467static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
468 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100469{
470 struct intel_breadcrumbs *b = &engine->breadcrumbs;
471
Chris Wilson61d3dc72017-03-03 19:08:24 +0000472 lockdep_assert_held(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100473
474 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000475 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100476
Chris Wilson61d3dc72017-03-03 19:08:24 +0000477 if (b->irq_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100478 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100479 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100480
Chris Wilson688e6c72016-07-01 17:23:15 +0100481 /* We are the current bottom-half. Find the next candidate,
482 * the first waiter in the queue on the remaining oldest
483 * request. As multiple seqnos may complete in the time it
484 * takes us to wake up and find the next waiter, we have to
485 * wake up that waiter for it to perform its own coherent
486 * completion check.
487 */
488 next = rb_next(&wait->node);
489 if (chain_wakeup(next, priority)) {
490 /* If the next waiter is already complete,
491 * wake it up and continue onto the next waiter. So
492 * if have a small herd, they will wake up in parallel
493 * rather than sequentially, which should reduce
494 * the overall latency in waking all the completed
495 * clients.
496 *
497 * However, waking up a chain adds extra latency to
498 * the first_waiter. This is undesirable if that
499 * waiter is a high priority task.
500 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100501 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100502
503 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
504 struct rb_node *n = rb_next(next);
505
506 __intel_breadcrumbs_finish(b, to_wait(next));
507 next = n;
508 if (!chain_wakeup(next, priority))
509 break;
510 }
511 }
512
Chris Wilsonb66255f2017-03-03 17:14:22 +0000513 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100514 } else {
515 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
516 }
517
518 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
519 rb_erase(&wait->node, &b->waiters);
Chris Wilson6e068272017-11-15 12:14:58 +0000520 RB_CLEAR_NODE(&wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100521
Chris Wilson9eb143b2017-02-23 07:44:16 +0000522out:
Chris Wilson61d3dc72017-03-03 19:08:24 +0000523 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100524 GEM_BUG_ON(rb_first(&b->waiters) !=
Chris Wilson61d3dc72017-03-03 19:08:24 +0000525 (b->irq_wait ? &b->irq_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000526}
527
528void intel_engine_remove_wait(struct intel_engine_cs *engine,
529 struct intel_wait *wait)
530{
531 struct intel_breadcrumbs *b = &engine->breadcrumbs;
532
533 /* Quick check to see if this waiter was already decoupled from
534 * the tree by the bottom-half to avoid contention on the spinlock
535 * by the herd.
536 */
Chris Wilson908a6cb2017-03-15 21:07:25 +0000537 if (RB_EMPTY_NODE(&wait->node)) {
538 GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000539 return;
Chris Wilson908a6cb2017-03-15 21:07:25 +0000540 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000541
Chris Wilson61d3dc72017-03-03 19:08:24 +0000542 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000543 __intel_engine_remove_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000544 spin_unlock_irq(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100545}
546
Chris Wilsond6a22892017-02-23 07:44:17 +0000547static bool signal_valid(const struct drm_i915_gem_request *request)
548{
549 return intel_wait_check_request(&request->signaling.wait, request);
550}
551
552static bool signal_complete(const struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100553{
Chris Wilsonb3850852016-07-01 17:23:26 +0100554 if (!request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100555 return false;
556
557 /* If another process served as the bottom-half it may have already
558 * signalled that this wait is already completed.
559 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100560 if (intel_wait_complete(&request->signaling.wait))
Chris Wilsond6a22892017-02-23 07:44:17 +0000561 return signal_valid(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100562
563 /* Carefully check if the request is complete, giving time for the
564 * seqno to be visible or if the GPU hung.
565 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100566 if (__i915_request_irq_complete(request))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100567 return true;
568
569 return false;
570}
571
Chris Wilsonb3850852016-07-01 17:23:26 +0100572static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100573{
Chris Wilsond8567862016-12-20 10:40:03 +0000574 return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100575}
576
577static void signaler_set_rtpriority(void)
578{
579 struct sched_param param = { .sched_priority = 1 };
580
581 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
582}
583
584static int intel_breadcrumbs_signaler(void *arg)
585{
586 struct intel_engine_cs *engine = arg;
587 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonb3850852016-07-01 17:23:26 +0100588 struct drm_i915_gem_request *request;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100589
590 /* Install ourselves with high priority to reduce signalling latency */
591 signaler_set_rtpriority();
592
593 do {
Chris Wilsona7980a62017-04-04 13:05:31 +0100594 bool do_schedule = true;
595
Chris Wilsonc81d4612016-07-01 17:23:25 +0100596 set_current_state(TASK_INTERRUPTIBLE);
597
598 /* We are either woken up by the interrupt bottom-half,
599 * or by a client adding a new signaller. In both cases,
600 * the GPU seqno may have advanced beyond our oldest signal.
601 * If it has, propagate the signal, remove the waiter and
602 * check again with the next oldest signal. Otherwise we
603 * need to wait for a new interrupt from the GPU or for
604 * a new client.
605 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000606 rcu_read_lock();
607 request = rcu_dereference(b->first_signal);
608 if (request)
609 request = i915_gem_request_get_rcu(request);
610 rcu_read_unlock();
Chris Wilsonb3850852016-07-01 17:23:26 +0100611 if (signal_complete(request)) {
Chris Wilson7c9e9342017-01-24 11:00:09 +0000612 local_bh_disable();
613 dma_fence_signal(&request->fence);
614 local_bh_enable(); /* kick start the tasklets */
615
Chris Wilson61d3dc72017-03-03 19:08:24 +0000616 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000617
Chris Wilsonc81d4612016-07-01 17:23:25 +0100618 /* Wake up all other completed waiters and select the
619 * next bottom-half for the next user interrupt.
620 */
Chris Wilson9eb143b2017-02-23 07:44:16 +0000621 __intel_engine_remove_wait(engine,
622 &request->signaling.wait);
Chris Wilson5590af32016-09-09 14:11:54 +0100623
Chris Wilsonc81d4612016-07-01 17:23:25 +0100624 /* Find the next oldest signal. Note that as we have
625 * not been holding the lock, another client may
626 * have installed an even older signal than the one
627 * we just completed - so double check we are still
628 * the oldest before picking the next one.
629 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000630 if (request == rcu_access_pointer(b->first_signal)) {
Chris Wilsonb3850852016-07-01 17:23:26 +0100631 struct rb_node *rb =
632 rb_next(&request->signaling.node);
Chris Wilsoncced5e22017-02-23 07:44:15 +0000633 rcu_assign_pointer(b->first_signal,
634 rb ? to_signaler(rb) : NULL);
Chris Wilsonb3850852016-07-01 17:23:26 +0100635 }
636 rb_erase(&request->signaling.node, &b->signals);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000637 RB_CLEAR_NODE(&request->signaling.node);
638
Chris Wilson61d3dc72017-03-03 19:08:24 +0000639 spin_unlock_irq(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100640
Chris Wilsone8a261e2016-07-20 13:31:49 +0100641 i915_gem_request_put(request);
Chris Wilsona7980a62017-04-04 13:05:31 +0100642
643 /* If the engine is saturated we may be continually
644 * processing completed requests. This angers the
645 * NMI watchdog if we never let anything else
646 * have access to the CPU. Let's pretend to be nice
647 * and relinquish the CPU if we burn through the
648 * entire RT timeslice!
649 */
650 do_schedule = need_resched();
651 }
652
653 if (unlikely(do_schedule)) {
Chris Wilsond6a22892017-02-23 07:44:17 +0000654 DEFINE_WAIT(exec);
655
Chris Wilsonb1becb82017-04-03 11:51:24 +0100656 if (kthread_should_park())
657 kthread_parkme();
658
Chris Wilsoncced5e22017-02-23 07:44:15 +0000659 if (kthread_should_stop()) {
660 GEM_BUG_ON(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100661 break;
Chris Wilsoncced5e22017-02-23 07:44:15 +0000662 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100663
Chris Wilsond6a22892017-02-23 07:44:17 +0000664 if (request)
665 add_wait_queue(&request->execute, &exec);
666
Chris Wilsonc81d4612016-07-01 17:23:25 +0100667 schedule();
Chris Wilsonfe3288b2017-02-12 17:20:01 +0000668
Chris Wilsond6a22892017-02-23 07:44:17 +0000669 if (request)
670 remove_wait_queue(&request->execute, &exec);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100671 }
Chris Wilsoncced5e22017-02-23 07:44:15 +0000672 i915_gem_request_put(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100673 } while (1);
674 __set_current_state(TASK_RUNNING);
675
676 return 0;
677}
678
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100679void intel_engine_enable_signaling(struct drm_i915_gem_request *request,
680 bool wakeup)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100681{
682 struct intel_engine_cs *engine = request->engine;
683 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000684 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100685
Chris Wilsonf6168e32016-10-28 13:58:55 +0100686 /* Note that we may be called from an interrupt handler on another
687 * device (e.g. nouveau signaling a fence completion causing us
688 * to submit a request, and so enable signaling). As such,
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000689 * we need to make sure that all other users of b->rb_lock protect
Chris Wilsonf6168e32016-10-28 13:58:55 +0100690 * against interrupts, i.e. use spin_lock_irqsave.
691 */
692
693 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilsone60a8702017-03-02 11:51:30 +0000694 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000695 lockdep_assert_held(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000696
697 seqno = i915_gem_request_global_seqno(request);
698 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100699 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100700
Chris Wilsonb3850852016-07-01 17:23:26 +0100701 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000702 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000703 request->signaling.wait.seqno = seqno;
Chris Wilsone8a261e2016-07-20 13:31:49 +0100704 i915_gem_request_get(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100705
Chris Wilson61d3dc72017-03-03 19:08:24 +0000706 spin_lock(&b->rb_lock);
Chris Wilson4a50d202016-07-26 12:01:50 +0100707
Chris Wilsonc81d4612016-07-01 17:23:25 +0100708 /* First add ourselves into the list of waiters, but register our
709 * bottom-half as the signaller thread. As per usual, only the oldest
710 * waiter (not just signaller) is tasked as the bottom-half waking
711 * up all completed waiters after the user interrupt.
712 *
713 * If we are the oldest waiter, enable the irq (after which we
714 * must double check that the seqno did not complete).
715 */
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100716 wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100717
Chris Wilson735e0eb2017-06-08 12:14:04 +0100718 if (!__i915_gem_request_completed(request, seqno)) {
719 struct rb_node *parent, **p;
720 bool first;
721
722 /* Now insert ourselves into the retirement ordered list of
723 * signals on this engine. We track the oldest seqno as that
724 * will be the first signal to complete.
725 */
726 parent = NULL;
727 first = true;
728 p = &b->signals.rb_node;
729 while (*p) {
730 parent = *p;
731 if (i915_seqno_passed(seqno,
732 to_signaler(parent)->signaling.wait.seqno)) {
733 p = &parent->rb_right;
734 first = false;
735 } else {
736 p = &parent->rb_left;
737 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100738 }
Chris Wilson735e0eb2017-06-08 12:14:04 +0100739 rb_link_node(&request->signaling.node, parent, p);
740 rb_insert_color(&request->signaling.node, &b->signals);
741 if (first)
742 rcu_assign_pointer(b->first_signal, request);
743 } else {
744 __intel_engine_remove_wait(engine, &request->signaling.wait);
745 i915_gem_request_put(request);
746 wakeup = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100747 }
Chris Wilsonb3850852016-07-01 17:23:26 +0100748
Chris Wilson61d3dc72017-03-03 19:08:24 +0000749 spin_unlock(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100750
751 if (wakeup)
752 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100753}
754
Chris Wilson9eb143b2017-02-23 07:44:16 +0000755void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
756{
757 struct intel_engine_cs *engine = request->engine;
758 struct intel_breadcrumbs *b = &engine->breadcrumbs;
759
Chris Wilsone60a8702017-03-02 11:51:30 +0000760 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000761 lockdep_assert_held(&request->lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000762 GEM_BUG_ON(!request->signaling.wait.seqno);
763
Chris Wilson61d3dc72017-03-03 19:08:24 +0000764 spin_lock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000765
766 if (!RB_EMPTY_NODE(&request->signaling.node)) {
767 if (request == rcu_access_pointer(b->first_signal)) {
768 struct rb_node *rb =
769 rb_next(&request->signaling.node);
770 rcu_assign_pointer(b->first_signal,
771 rb ? to_signaler(rb) : NULL);
772 }
773 rb_erase(&request->signaling.node, &b->signals);
774 RB_CLEAR_NODE(&request->signaling.node);
775 i915_gem_request_put(request);
776 }
777
778 __intel_engine_remove_wait(engine, &request->signaling.wait);
779
Chris Wilson61d3dc72017-03-03 19:08:24 +0000780 spin_unlock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000781
782 request->signaling.wait.seqno = 0;
783}
784
Chris Wilson688e6c72016-07-01 17:23:15 +0100785int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
786{
787 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100788 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100789
Chris Wilson61d3dc72017-03-03 19:08:24 +0000790 spin_lock_init(&b->rb_lock);
791 spin_lock_init(&b->irq_lock);
792
Kees Cook39cbf2a2017-10-17 09:53:04 +0300793 timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
794 timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
Chris Wilson688e6c72016-07-01 17:23:15 +0100795
Chris Wilsonc81d4612016-07-01 17:23:25 +0100796 /* Spawn a thread to provide a common bottom-half for all signals.
797 * As this is an asynchronous interface we cannot steal the current
798 * task for handling the bottom-half to the user interrupt, therefore
799 * we create a thread to do the coherent seqno dance after the
800 * interrupt and then signal the waitqueue (via the dma-buf/fence).
801 */
802 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
803 "i915/signal:%d", engine->id);
804 if (IS_ERR(tsk))
805 return PTR_ERR(tsk);
806
807 b->signaler = tsk;
808
Chris Wilson688e6c72016-07-01 17:23:15 +0100809 return 0;
810}
811
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100812static void cancel_fake_irq(struct intel_engine_cs *engine)
813{
814 struct intel_breadcrumbs *b = &engine->breadcrumbs;
815
816 del_timer_sync(&b->hangcheck);
817 del_timer_sync(&b->fake_irq);
818 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
819}
820
821void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
822{
823 struct intel_breadcrumbs *b = &engine->breadcrumbs;
824
825 cancel_fake_irq(engine);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000826 spin_lock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100827
Chris Wilson67b807a82017-02-27 20:58:50 +0000828 if (b->irq_enabled)
829 irq_enable(engine);
830 else
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100831 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000832
833 /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
834 * GPU is active and may have already executed the MI_USER_INTERRUPT
835 * before the CPU is ready to receive. However, the engine is currently
836 * idle (we haven't started it yet), there is no possibility for a
837 * missed interrupt as we enabled the irq and so we can clear the
838 * immediate wakeup (until a real interrupt arrives for the waiter).
839 */
840 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
841
842 if (b->irq_armed)
843 enable_fake_irq(b);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100844
Chris Wilson61d3dc72017-03-03 19:08:24 +0000845 spin_unlock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100846}
847
Chris Wilson688e6c72016-07-01 17:23:15 +0100848void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
849{
850 struct intel_breadcrumbs *b = &engine->breadcrumbs;
851
Chris Wilson381744f2016-11-21 11:07:59 +0000852 /* The engines should be idle and all requests accounted for! */
Chris Wilson61d3dc72017-03-03 19:08:24 +0000853 WARN_ON(READ_ONCE(b->irq_wait));
Chris Wilson381744f2016-11-21 11:07:59 +0000854 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncced5e22017-02-23 07:44:15 +0000855 WARN_ON(rcu_access_pointer(b->first_signal));
Chris Wilson381744f2016-11-21 11:07:59 +0000856 WARN_ON(!RB_EMPTY_ROOT(&b->signals));
857
Chris Wilsonc81d4612016-07-01 17:23:25 +0100858 if (!IS_ERR_OR_NULL(b->signaler))
859 kthread_stop(b->signaler);
860
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100861 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100862}
863
Chris Wilson9b6586a2017-02-23 07:44:08 +0000864bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100865{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000866 struct intel_breadcrumbs *b = &engine->breadcrumbs;
867 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100868
Chris Wilson61d3dc72017-03-03 19:08:24 +0000869 spin_lock_irq(&b->rb_lock);
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000870
Chris Wilson61d3dc72017-03-03 19:08:24 +0000871 if (b->irq_wait) {
872 wake_up_process(b->irq_wait->tsk);
Chris Wilson4bd66392017-03-15 21:07:22 +0000873 busy = true;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100874 }
875
Chris Wilsoncced5e22017-02-23 07:44:15 +0000876 if (rcu_access_pointer(b->first_signal)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000877 wake_up_process(b->signaler);
Chris Wilson4bd66392017-03-15 21:07:22 +0000878 busy = true;
Chris Wilson9b6586a2017-02-23 07:44:08 +0000879 }
880
Chris Wilson61d3dc72017-03-03 19:08:24 +0000881 spin_unlock_irq(&b->rb_lock);
Chris Wilson9b6586a2017-02-23 07:44:08 +0000882
883 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100884}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000885
886#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
887#include "selftests/intel_breadcrumbs.c"
888#endif