blob: 2b26f84480cc8ef00d843589d0a5d209f5169339 [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>
26
Chris Wilson688e6c72016-07-01 17:23:15 +010027#include "i915_drv.h"
28
Chris Wilson67b807a82017-02-27 20:58:50 +000029static unsigned int __intel_breadcrumbs_wakeup(struct intel_breadcrumbs *b)
Chris Wilson8d769ea2017-02-27 20:58:47 +000030{
Chris Wilson56299fb2017-02-27 20:58:48 +000031 struct intel_wait *wait;
Chris Wilson8d769ea2017-02-27 20:58:47 +000032 unsigned int result = 0;
33
Chris Wilson67b807a82017-02-27 20:58:50 +000034 wait = b->first_wait;
Chris Wilson56299fb2017-02-27 20:58:48 +000035 if (wait) {
Chris Wilson8d769ea2017-02-27 20:58:47 +000036 result = ENGINE_WAKEUP_WAITER;
Chris Wilson67b807a82017-02-27 20:58:50 +000037 if (wake_up_process(wait->tsk))
38 result |= ENGINE_WAKEUP_ASLEEP;
Chris Wilson8d769ea2017-02-27 20:58:47 +000039 }
Chris Wilson67b807a82017-02-27 20:58:50 +000040
41 return result;
42}
43
44unsigned int intel_engine_wakeup(struct intel_engine_cs *engine)
45{
46 struct intel_breadcrumbs *b = &engine->breadcrumbs;
47 unsigned long flags;
48 unsigned int result;
49
50 spin_lock_irqsave(&b->lock, flags);
51 result = __intel_breadcrumbs_wakeup(b);
52 spin_unlock_irqrestore(&b->lock, flags);
Chris Wilson8d769ea2017-02-27 20:58:47 +000053
54 return result;
55}
56
Chris Wilson2246bea2017-02-17 15:13:00 +000057static unsigned long wait_timeout(void)
58{
59 return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
60}
61
Chris Wilson80166e402017-02-28 08:50:18 +000062static noinline void missed_breadcrumb(struct intel_engine_cs *engine)
63{
64 DRM_DEBUG_DRIVER("%s missed breadcrumb at %pF, irq posted? %s\n",
65 engine->name, __builtin_return_address(0),
66 yesno(test_bit(ENGINE_IRQ_BREADCRUMB,
67 &engine->irq_posted)));
68
69 set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
70}
71
Chris Wilson83348ba2016-08-09 17:47:51 +010072static void intel_breadcrumbs_hangcheck(unsigned long data)
73{
74 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
75 struct intel_breadcrumbs *b = &engine->breadcrumbs;
76
Chris Wilson67b807a82017-02-27 20:58:50 +000077 if (!b->irq_armed)
Chris Wilson83348ba2016-08-09 17:47:51 +010078 return;
79
Chris Wilson2246bea2017-02-17 15:13:00 +000080 if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) {
81 b->hangcheck_interrupts = atomic_read(&engine->irq_count);
82 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson83348ba2016-08-09 17:47:51 +010083 return;
84 }
85
Chris Wilson67b807a82017-02-27 20:58:50 +000086 /* We keep the hangcheck time alive until we disarm the irq, even
87 * if there are no waiters at present.
88 *
89 * If the waiter was currently running, assume it hasn't had a chance
Chris Wilson89985672017-02-17 15:13:02 +000090 * to process the pending interrupt (e.g, low priority task on a loaded
91 * system) and wait until it sleeps before declaring a missed interrupt.
Chris Wilson67b807a82017-02-27 20:58:50 +000092 *
93 * If the waiter was asleep (and not even pending a wakeup), then we
94 * must have missed an interrupt as the GPU has stopped advancing
95 * but we still have a waiter. Assuming all batches complete within
96 * DRM_I915_HANGCHECK_JIFFIES [1.5s]!
Chris Wilson89985672017-02-17 15:13:02 +000097 */
Chris Wilson67b807a82017-02-27 20:58:50 +000098 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) {
Chris Wilson80166e402017-02-28 08:50:18 +000099 missed_breadcrumb(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000100 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
101 } else {
Chris Wilson89985672017-02-17 15:13:02 +0000102 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson89985672017-02-17 15:13:02 +0000103 }
Chris Wilson83348ba2016-08-09 17:47:51 +0100104}
105
Chris Wilson688e6c72016-07-01 17:23:15 +0100106static void intel_breadcrumbs_fake_irq(unsigned long data)
107{
108 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
Chris Wilson67b807a82017-02-27 20:58:50 +0000109 struct intel_breadcrumbs *b = &engine->breadcrumbs;
110 unsigned long flags;
Chris Wilson688e6c72016-07-01 17:23:15 +0100111
112 /*
113 * The timer persists in case we cannot enable interrupts,
114 * or if we have previously seen seqno/interrupt incoherency
115 * ("missed interrupt" syndrome). Here the worker will wake up
116 * every jiffie in order to kick the oldest waiter to do the
117 * coherent seqno check.
118 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000119
120 spin_lock_irqsave(&b->lock, flags);
121 if (!__intel_breadcrumbs_wakeup(b))
122 __intel_engine_disarm_breadcrumbs(engine);
123 spin_unlock_irqrestore(&b->lock, flags);
124 if (!b->irq_armed)
Chris Wilson19d0a572017-02-27 20:58:49 +0000125 return;
126
Chris Wilson67b807a82017-02-27 20:58:50 +0000127 mod_timer(&b->fake_irq, jiffies + 1);
Chris Wilson19d0a572017-02-27 20:58:49 +0000128
129 /* Ensure that even if the GPU hangs, we get woken up.
130 *
131 * However, note that if no one is waiting, we never notice
132 * a gpu hang. Eventually, we will have to wait for a resource
133 * held by the GPU and so trigger a hangcheck. In the most
134 * pathological case, this will be upon memory starvation! To
135 * prevent this, we also queue the hangcheck from the retire
136 * worker.
137 */
138 i915_queue_hangcheck(engine->i915);
Chris Wilson688e6c72016-07-01 17:23:15 +0100139}
140
141static void irq_enable(struct intel_engine_cs *engine)
142{
Chris Wilson3d5564e2016-07-01 17:23:23 +0100143 /* Enabling the IRQ may miss the generation of the interrupt, but
144 * we still need to force the barrier before reading the seqno,
145 * just in case.
146 */
Chris Wilson538b2572017-01-24 15:18:05 +0000147 set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100148
Chris Wilsonf6168e32016-10-28 13:58:55 +0100149 /* Caller disables interrupts */
150 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100151 engine->irq_enable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100152 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100153}
154
155static void irq_disable(struct intel_engine_cs *engine)
156{
Chris Wilsonf6168e32016-10-28 13:58:55 +0100157 /* Caller disables interrupts */
158 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100159 engine->irq_disable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100160 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100161}
162
Chris Wilson67b807a82017-02-27 20:58:50 +0000163void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
164{
165 struct intel_breadcrumbs *b = &engine->breadcrumbs;
166
Chris Wilson67520412017-03-02 13:28:01 +0000167 lockdep_assert_held(&b->lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000168
169 if (b->irq_enabled) {
170 irq_disable(engine);
171 b->irq_enabled = false;
172 }
173
174 b->irq_armed = false;
175}
176
177void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
178{
179 struct intel_breadcrumbs *b = &engine->breadcrumbs;
180 unsigned long flags;
181
182 if (!b->irq_armed)
183 return;
184
185 spin_lock_irqsave(&b->lock, flags);
186
187 /* We only disarm the irq when we are idle (all requests completed),
188 * so if there remains a sleeping waiter, it missed the request
189 * completion.
190 */
191 if (__intel_breadcrumbs_wakeup(b) & ENGINE_WAKEUP_ASLEEP)
Chris Wilson80166e402017-02-28 08:50:18 +0000192 missed_breadcrumb(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000193
194 __intel_engine_disarm_breadcrumbs(engine);
195
196 spin_unlock_irqrestore(&b->lock, flags);
197}
198
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000199static bool use_fake_irq(const struct intel_breadcrumbs *b)
200{
201 const struct intel_engine_cs *engine =
202 container_of(b, struct intel_engine_cs, breadcrumbs);
203
204 if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
205 return false;
206
207 /* Only start with the heavy weight fake irq timer if we have not
208 * seen any interrupts since enabling it the first time. If the
209 * interrupts are still arriving, it means we made a mistake in our
210 * engine->seqno_barrier(), a timing error that should be transient
211 * and unlikely to reoccur.
212 */
213 return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
214}
215
Chris Wilson67b807a82017-02-27 20:58:50 +0000216static void enable_fake_irq(struct intel_breadcrumbs *b)
217{
218 /* Ensure we never sleep indefinitely */
219 if (!b->irq_enabled || use_fake_irq(b))
220 mod_timer(&b->fake_irq, jiffies + 1);
221 else
222 mod_timer(&b->hangcheck, wait_timeout());
223}
224
Chris Wilson04171312016-07-06 12:39:00 +0100225static void __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
Chris Wilson688e6c72016-07-01 17:23:15 +0100226{
227 struct intel_engine_cs *engine =
228 container_of(b, struct intel_engine_cs, breadcrumbs);
229 struct drm_i915_private *i915 = engine->i915;
Chris Wilson688e6c72016-07-01 17:23:15 +0100230
Chris Wilson67520412017-03-02 13:28:01 +0000231 lockdep_assert_held(&b->lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000232 if (b->irq_armed)
Chris Wilson04171312016-07-06 12:39:00 +0100233 return;
Chris Wilson688e6c72016-07-01 17:23:15 +0100234
Chris Wilson67b807a82017-02-27 20:58:50 +0000235 /* The breadcrumb irq will be disarmed on the interrupt after the
236 * waiters are signaled. This gives us a single interrupt window in
237 * which we can add a new waiter and avoid the cost of re-enabling
238 * the irq.
239 */
240 b->irq_armed = true;
241 GEM_BUG_ON(b->irq_enabled);
242
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000243 if (I915_SELFTEST_ONLY(b->mock)) {
244 /* For our mock objects we want to avoid interaction
245 * with the real hardware (which is not set up). So
246 * we simply pretend we have enabled the powerwell
247 * and the irq, and leave it up to the mock
248 * implementation to call intel_engine_wakeup()
249 * itself when it wants to simulate a user interrupt,
250 */
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000251 return;
252 }
253
Chris Wilson688e6c72016-07-01 17:23:15 +0100254 /* Since we are waiting on a request, the GPU should be busy
Chris Wilson67b807a82017-02-27 20:58:50 +0000255 * and should have its own rpm reference. This is tracked
256 * by i915->gt.awake, we can forgo holding our own wakref
257 * for the interrupt as before i915->gt.awake is released (when
258 * the driver is idle) we disarm the breadcrumbs.
Chris Wilson688e6c72016-07-01 17:23:15 +0100259 */
Chris Wilson688e6c72016-07-01 17:23:15 +0100260
261 /* No interrupts? Kick the waiter every jiffie! */
262 if (intel_irqs_enabled(i915)) {
Chris Wilson3d5564e2016-07-01 17:23:23 +0100263 if (!test_bit(engine->id, &i915->gpu_error.test_irq_rings))
Chris Wilson688e6c72016-07-01 17:23:15 +0100264 irq_enable(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100265 b->irq_enabled = true;
266 }
267
Chris Wilson67b807a82017-02-27 20:58:50 +0000268 enable_fake_irq(b);
Chris Wilson688e6c72016-07-01 17:23:15 +0100269}
270
271static inline struct intel_wait *to_wait(struct rb_node *node)
272{
Chris Wilsond8567862016-12-20 10:40:03 +0000273 return rb_entry(node, struct intel_wait, node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100274}
275
276static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
277 struct intel_wait *wait)
278{
Chris Wilson67520412017-03-02 13:28:01 +0000279 lockdep_assert_held(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100280
281 /* This request is completed, so remove it from the tree, mark it as
282 * complete, and *then* wake up the associated task.
283 */
284 rb_erase(&wait->node, &b->waiters);
285 RB_CLEAR_NODE(&wait->node);
286
287 wake_up_process(wait->tsk); /* implicit smp_wmb() */
288}
289
Chris Wilsonb66255f2017-03-03 17:14:22 +0000290static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
291 struct rb_node *next)
292{
293 struct intel_breadcrumbs *b = &engine->breadcrumbs;
294
295 GEM_BUG_ON(!b->irq_armed);
296 b->first_wait = to_wait(next);
297
298 /* We always wake up the next waiter that takes over as the bottom-half
299 * as we may delegate not only the irq-seqno barrier to the next waiter
300 * but also the task of waking up concurrent waiters.
301 */
302 if (next)
303 wake_up_process(to_wait(next)->tsk);
304}
305
Chris Wilson688e6c72016-07-01 17:23:15 +0100306static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
307 struct intel_wait *wait)
308{
309 struct intel_breadcrumbs *b = &engine->breadcrumbs;
310 struct rb_node **p, *parent, *completed;
311 bool first;
312 u32 seqno;
313
314 /* Insert the request into the retirement ordered list
315 * of waiters by walking the rbtree. If we are the oldest
316 * seqno in the tree (the first to be retired), then
317 * set ourselves as the bottom-half.
318 *
319 * As we descend the tree, prune completed branches since we hold the
320 * spinlock we know that the first_waiter must be delayed and can
321 * reduce some of the sequential wake up latency if we take action
322 * ourselves and wake up the completed tasks in parallel. Also, by
323 * removing stale elements in the tree, we may be able to reduce the
324 * ping-pong between the old bottom-half and ourselves as first-waiter.
325 */
326 first = true;
327 parent = NULL;
328 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100329 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100330
331 /* If the request completed before we managed to grab the spinlock,
332 * return now before adding ourselves to the rbtree. We let the
333 * current bottom-half handle any pending wakeups and instead
334 * try and get out of the way quickly.
335 */
336 if (i915_seqno_passed(seqno, wait->seqno)) {
337 RB_CLEAR_NODE(&wait->node);
338 return first;
339 }
340
341 p = &b->waiters.rb_node;
342 while (*p) {
343 parent = *p;
344 if (wait->seqno == to_wait(parent)->seqno) {
345 /* We have multiple waiters on the same seqno, select
346 * the highest priority task (that with the smallest
347 * task->prio) to serve as the bottom-half for this
348 * group.
349 */
350 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
351 p = &parent->rb_right;
352 first = false;
353 } else {
354 p = &parent->rb_left;
355 }
356 } else if (i915_seqno_passed(wait->seqno,
357 to_wait(parent)->seqno)) {
358 p = &parent->rb_right;
359 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
360 completed = parent;
361 else
362 first = false;
363 } else {
364 p = &parent->rb_left;
365 }
366 }
367 rb_link_node(&wait->node, parent, p);
368 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100369
370 if (completed) {
371 struct rb_node *next = rb_next(completed);
372
373 GEM_BUG_ON(!next && !first);
374 if (next && next != &wait->node) {
375 GEM_BUG_ON(first);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000376 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100377 }
378
379 do {
380 struct intel_wait *crumb = to_wait(completed);
381 completed = rb_prev(completed);
382 __intel_breadcrumbs_finish(b, crumb);
383 } while (completed);
384 }
385
386 if (first) {
387 GEM_BUG_ON(rb_first(&b->waiters) != &wait->node);
388 b->first_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100389 /* After assigning ourselves as the new bottom-half, we must
390 * perform a cursory check to prevent a missed interrupt.
391 * Either we miss the interrupt whilst programming the hardware,
392 * or if there was a previous waiter (for a later seqno) they
393 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100394 * in the unlocked read of b->irq_seqno_bh in the irq handler)
395 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100396 */
397 __intel_breadcrumbs_enable_irq(b);
Chris Wilson688e6c72016-07-01 17:23:15 +0100398 }
Chris Wilson688e6c72016-07-01 17:23:15 +0100399 GEM_BUG_ON(!b->first_wait);
400 GEM_BUG_ON(rb_first(&b->waiters) != &b->first_wait->node);
401
402 return first;
403}
404
405bool intel_engine_add_wait(struct intel_engine_cs *engine,
406 struct intel_wait *wait)
407{
408 struct intel_breadcrumbs *b = &engine->breadcrumbs;
409 bool first;
410
Chris Wilsonf6168e32016-10-28 13:58:55 +0100411 spin_lock_irq(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100412 first = __intel_engine_add_wait(engine, wait);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100413 spin_unlock_irq(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100414
415 return first;
416}
417
Chris Wilson688e6c72016-07-01 17:23:15 +0100418static inline bool chain_wakeup(struct rb_node *rb, int priority)
419{
420 return rb && to_wait(rb)->tsk->prio <= priority;
421}
422
Chris Wilsonc81d4612016-07-01 17:23:25 +0100423static inline int wakeup_priority(struct intel_breadcrumbs *b,
424 struct task_struct *tsk)
425{
426 if (tsk == b->signaler)
427 return INT_MIN;
428 else
429 return tsk->prio;
430}
431
Chris Wilson9eb143b2017-02-23 07:44:16 +0000432static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
433 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100434{
435 struct intel_breadcrumbs *b = &engine->breadcrumbs;
436
Chris Wilson67520412017-03-02 13:28:01 +0000437 lockdep_assert_held(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100438
439 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000440 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100441
442 if (b->first_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100443 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100444 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100445
Chris Wilson688e6c72016-07-01 17:23:15 +0100446 /* We are the current bottom-half. Find the next candidate,
447 * the first waiter in the queue on the remaining oldest
448 * request. As multiple seqnos may complete in the time it
449 * takes us to wake up and find the next waiter, we have to
450 * wake up that waiter for it to perform its own coherent
451 * completion check.
452 */
453 next = rb_next(&wait->node);
454 if (chain_wakeup(next, priority)) {
455 /* If the next waiter is already complete,
456 * wake it up and continue onto the next waiter. So
457 * if have a small herd, they will wake up in parallel
458 * rather than sequentially, which should reduce
459 * the overall latency in waking all the completed
460 * clients.
461 *
462 * However, waking up a chain adds extra latency to
463 * the first_waiter. This is undesirable if that
464 * waiter is a high priority task.
465 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100466 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100467
468 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
469 struct rb_node *n = rb_next(next);
470
471 __intel_breadcrumbs_finish(b, to_wait(next));
472 next = n;
473 if (!chain_wakeup(next, priority))
474 break;
475 }
476 }
477
Chris Wilsonb66255f2017-03-03 17:14:22 +0000478 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100479 } else {
480 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
481 }
482
483 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
484 rb_erase(&wait->node, &b->waiters);
485
Chris Wilson9eb143b2017-02-23 07:44:16 +0000486out:
Chris Wilson688e6c72016-07-01 17:23:15 +0100487 GEM_BUG_ON(b->first_wait == wait);
488 GEM_BUG_ON(rb_first(&b->waiters) !=
489 (b->first_wait ? &b->first_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000490}
491
492void intel_engine_remove_wait(struct intel_engine_cs *engine,
493 struct intel_wait *wait)
494{
495 struct intel_breadcrumbs *b = &engine->breadcrumbs;
496
497 /* Quick check to see if this waiter was already decoupled from
498 * the tree by the bottom-half to avoid contention on the spinlock
499 * by the herd.
500 */
501 if (RB_EMPTY_NODE(&wait->node))
502 return;
503
504 spin_lock_irq(&b->lock);
505 __intel_engine_remove_wait(engine, wait);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100506 spin_unlock_irq(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100507}
508
Chris Wilsond6a22892017-02-23 07:44:17 +0000509static bool signal_valid(const struct drm_i915_gem_request *request)
510{
511 return intel_wait_check_request(&request->signaling.wait, request);
512}
513
514static bool signal_complete(const struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100515{
Chris Wilsonb3850852016-07-01 17:23:26 +0100516 if (!request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100517 return false;
518
519 /* If another process served as the bottom-half it may have already
520 * signalled that this wait is already completed.
521 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100522 if (intel_wait_complete(&request->signaling.wait))
Chris Wilsond6a22892017-02-23 07:44:17 +0000523 return signal_valid(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100524
525 /* Carefully check if the request is complete, giving time for the
526 * seqno to be visible or if the GPU hung.
527 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100528 if (__i915_request_irq_complete(request))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100529 return true;
530
531 return false;
532}
533
Chris Wilsonb3850852016-07-01 17:23:26 +0100534static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100535{
Chris Wilsond8567862016-12-20 10:40:03 +0000536 return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100537}
538
539static void signaler_set_rtpriority(void)
540{
541 struct sched_param param = { .sched_priority = 1 };
542
543 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
544}
545
546static int intel_breadcrumbs_signaler(void *arg)
547{
548 struct intel_engine_cs *engine = arg;
549 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonb3850852016-07-01 17:23:26 +0100550 struct drm_i915_gem_request *request;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100551
552 /* Install ourselves with high priority to reduce signalling latency */
553 signaler_set_rtpriority();
554
555 do {
556 set_current_state(TASK_INTERRUPTIBLE);
557
558 /* We are either woken up by the interrupt bottom-half,
559 * or by a client adding a new signaller. In both cases,
560 * the GPU seqno may have advanced beyond our oldest signal.
561 * If it has, propagate the signal, remove the waiter and
562 * check again with the next oldest signal. Otherwise we
563 * need to wait for a new interrupt from the GPU or for
564 * a new client.
565 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000566 rcu_read_lock();
567 request = rcu_dereference(b->first_signal);
568 if (request)
569 request = i915_gem_request_get_rcu(request);
570 rcu_read_unlock();
Chris Wilsonb3850852016-07-01 17:23:26 +0100571 if (signal_complete(request)) {
Chris Wilson7c9e9342017-01-24 11:00:09 +0000572 local_bh_disable();
573 dma_fence_signal(&request->fence);
574 local_bh_enable(); /* kick start the tasklets */
575
Chris Wilson9eb143b2017-02-23 07:44:16 +0000576 spin_lock_irq(&b->lock);
577
Chris Wilsonc81d4612016-07-01 17:23:25 +0100578 /* Wake up all other completed waiters and select the
579 * next bottom-half for the next user interrupt.
580 */
Chris Wilson9eb143b2017-02-23 07:44:16 +0000581 __intel_engine_remove_wait(engine,
582 &request->signaling.wait);
Chris Wilson5590af32016-09-09 14:11:54 +0100583
Chris Wilsonc81d4612016-07-01 17:23:25 +0100584 /* Find the next oldest signal. Note that as we have
585 * not been holding the lock, another client may
586 * have installed an even older signal than the one
587 * we just completed - so double check we are still
588 * the oldest before picking the next one.
589 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000590 if (request == rcu_access_pointer(b->first_signal)) {
Chris Wilsonb3850852016-07-01 17:23:26 +0100591 struct rb_node *rb =
592 rb_next(&request->signaling.node);
Chris Wilsoncced5e22017-02-23 07:44:15 +0000593 rcu_assign_pointer(b->first_signal,
594 rb ? to_signaler(rb) : NULL);
Chris Wilsonb3850852016-07-01 17:23:26 +0100595 }
596 rb_erase(&request->signaling.node, &b->signals);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000597 RB_CLEAR_NODE(&request->signaling.node);
598
Chris Wilsonf6168e32016-10-28 13:58:55 +0100599 spin_unlock_irq(&b->lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100600
Chris Wilsone8a261e2016-07-20 13:31:49 +0100601 i915_gem_request_put(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100602 } else {
Chris Wilsond6a22892017-02-23 07:44:17 +0000603 DEFINE_WAIT(exec);
604
Chris Wilsoncced5e22017-02-23 07:44:15 +0000605 if (kthread_should_stop()) {
606 GEM_BUG_ON(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100607 break;
Chris Wilsoncced5e22017-02-23 07:44:15 +0000608 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100609
Chris Wilsond6a22892017-02-23 07:44:17 +0000610 if (request)
611 add_wait_queue(&request->execute, &exec);
612
Chris Wilsonc81d4612016-07-01 17:23:25 +0100613 schedule();
Chris Wilsonfe3288b2017-02-12 17:20:01 +0000614
Chris Wilsond6a22892017-02-23 07:44:17 +0000615 if (request)
616 remove_wait_queue(&request->execute, &exec);
617
Chris Wilsonfe3288b2017-02-12 17:20:01 +0000618 if (kthread_should_park())
619 kthread_parkme();
Chris Wilsonc81d4612016-07-01 17:23:25 +0100620 }
Chris Wilsoncced5e22017-02-23 07:44:15 +0000621 i915_gem_request_put(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100622 } while (1);
623 __set_current_state(TASK_RUNNING);
624
625 return 0;
626}
627
Chris Wilsonb3850852016-07-01 17:23:26 +0100628void intel_engine_enable_signaling(struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100629{
630 struct intel_engine_cs *engine = request->engine;
631 struct intel_breadcrumbs *b = &engine->breadcrumbs;
632 struct rb_node *parent, **p;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100633 bool first, wakeup;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000634 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100635
Chris Wilsonf6168e32016-10-28 13:58:55 +0100636 /* Note that we may be called from an interrupt handler on another
637 * device (e.g. nouveau signaling a fence completion causing us
638 * to submit a request, and so enable signaling). As such,
639 * we need to make sure that all other users of b->lock protect
640 * against interrupts, i.e. use spin_lock_irqsave.
641 */
642
643 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilsone60a8702017-03-02 11:51:30 +0000644 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000645 lockdep_assert_held(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000646
647 seqno = i915_gem_request_global_seqno(request);
648 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100649 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100650
Chris Wilsonb3850852016-07-01 17:23:26 +0100651 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000652 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000653 request->signaling.wait.seqno = seqno;
Chris Wilsone8a261e2016-07-20 13:31:49 +0100654 i915_gem_request_get(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100655
Chris Wilson4a50d202016-07-26 12:01:50 +0100656 spin_lock(&b->lock);
657
Chris Wilsonc81d4612016-07-01 17:23:25 +0100658 /* First add ourselves into the list of waiters, but register our
659 * bottom-half as the signaller thread. As per usual, only the oldest
660 * waiter (not just signaller) is tasked as the bottom-half waking
661 * up all completed waiters after the user interrupt.
662 *
663 * If we are the oldest waiter, enable the irq (after which we
664 * must double check that the seqno did not complete).
665 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100666 wakeup = __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100667
668 /* Now insert ourselves into the retirement ordered list of signals
669 * on this engine. We track the oldest seqno as that will be the
670 * first signal to complete.
671 */
Chris Wilsonc81d4612016-07-01 17:23:25 +0100672 parent = NULL;
673 first = true;
674 p = &b->signals.rb_node;
675 while (*p) {
676 parent = *p;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000677 if (i915_seqno_passed(seqno,
678 to_signaler(parent)->signaling.wait.seqno)) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100679 p = &parent->rb_right;
680 first = false;
681 } else {
682 p = &parent->rb_left;
683 }
684 }
Chris Wilsonb3850852016-07-01 17:23:26 +0100685 rb_link_node(&request->signaling.node, parent, p);
686 rb_insert_color(&request->signaling.node, &b->signals);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100687 if (first)
Chris Wilsoncced5e22017-02-23 07:44:15 +0000688 rcu_assign_pointer(b->first_signal, request);
Chris Wilsonb3850852016-07-01 17:23:26 +0100689
Chris Wilsonc81d4612016-07-01 17:23:25 +0100690 spin_unlock(&b->lock);
691
692 if (wakeup)
693 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100694}
695
Chris Wilson9eb143b2017-02-23 07:44:16 +0000696void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
697{
698 struct intel_engine_cs *engine = request->engine;
699 struct intel_breadcrumbs *b = &engine->breadcrumbs;
700
Chris Wilsone60a8702017-03-02 11:51:30 +0000701 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000702 lockdep_assert_held(&request->lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000703 GEM_BUG_ON(!request->signaling.wait.seqno);
704
705 spin_lock(&b->lock);
706
707 if (!RB_EMPTY_NODE(&request->signaling.node)) {
708 if (request == rcu_access_pointer(b->first_signal)) {
709 struct rb_node *rb =
710 rb_next(&request->signaling.node);
711 rcu_assign_pointer(b->first_signal,
712 rb ? to_signaler(rb) : NULL);
713 }
714 rb_erase(&request->signaling.node, &b->signals);
715 RB_CLEAR_NODE(&request->signaling.node);
716 i915_gem_request_put(request);
717 }
718
719 __intel_engine_remove_wait(engine, &request->signaling.wait);
720
721 spin_unlock(&b->lock);
722
723 request->signaling.wait.seqno = 0;
724}
725
Chris Wilson688e6c72016-07-01 17:23:15 +0100726int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
727{
728 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100729 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100730
731 spin_lock_init(&b->lock);
732 setup_timer(&b->fake_irq,
733 intel_breadcrumbs_fake_irq,
734 (unsigned long)engine);
Chris Wilson83348ba2016-08-09 17:47:51 +0100735 setup_timer(&b->hangcheck,
736 intel_breadcrumbs_hangcheck,
737 (unsigned long)engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100738
Chris Wilsonc81d4612016-07-01 17:23:25 +0100739 /* Spawn a thread to provide a common bottom-half for all signals.
740 * As this is an asynchronous interface we cannot steal the current
741 * task for handling the bottom-half to the user interrupt, therefore
742 * we create a thread to do the coherent seqno dance after the
743 * interrupt and then signal the waitqueue (via the dma-buf/fence).
744 */
745 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
746 "i915/signal:%d", engine->id);
747 if (IS_ERR(tsk))
748 return PTR_ERR(tsk);
749
750 b->signaler = tsk;
751
Chris Wilson688e6c72016-07-01 17:23:15 +0100752 return 0;
753}
754
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100755static void cancel_fake_irq(struct intel_engine_cs *engine)
756{
757 struct intel_breadcrumbs *b = &engine->breadcrumbs;
758
759 del_timer_sync(&b->hangcheck);
760 del_timer_sync(&b->fake_irq);
761 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
762}
763
764void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
765{
766 struct intel_breadcrumbs *b = &engine->breadcrumbs;
767
768 cancel_fake_irq(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100769 spin_lock_irq(&b->lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100770
Chris Wilson67b807a82017-02-27 20:58:50 +0000771 if (b->irq_enabled)
772 irq_enable(engine);
773 else
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100774 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000775
776 /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
777 * GPU is active and may have already executed the MI_USER_INTERRUPT
778 * before the CPU is ready to receive. However, the engine is currently
779 * idle (we haven't started it yet), there is no possibility for a
780 * missed interrupt as we enabled the irq and so we can clear the
781 * immediate wakeup (until a real interrupt arrives for the waiter).
782 */
783 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
784
785 if (b->irq_armed)
786 enable_fake_irq(b);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100787
Chris Wilsonf6168e32016-10-28 13:58:55 +0100788 spin_unlock_irq(&b->lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100789}
790
Chris Wilson688e6c72016-07-01 17:23:15 +0100791void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
792{
793 struct intel_breadcrumbs *b = &engine->breadcrumbs;
794
Chris Wilson381744f2016-11-21 11:07:59 +0000795 /* The engines should be idle and all requests accounted for! */
796 WARN_ON(READ_ONCE(b->first_wait));
797 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncced5e22017-02-23 07:44:15 +0000798 WARN_ON(rcu_access_pointer(b->first_signal));
Chris Wilson381744f2016-11-21 11:07:59 +0000799 WARN_ON(!RB_EMPTY_ROOT(&b->signals));
800
Chris Wilsonc81d4612016-07-01 17:23:25 +0100801 if (!IS_ERR_OR_NULL(b->signaler))
802 kthread_stop(b->signaler);
803
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100804 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100805}
806
Chris Wilson9b6586a2017-02-23 07:44:08 +0000807bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100808{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000809 struct intel_breadcrumbs *b = &engine->breadcrumbs;
810 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100811
Chris Wilson9b6586a2017-02-23 07:44:08 +0000812 spin_lock_irq(&b->lock);
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000813
Chris Wilson9b6586a2017-02-23 07:44:08 +0000814 if (b->first_wait) {
815 wake_up_process(b->first_wait->tsk);
816 busy |= intel_engine_flag(engine);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100817 }
818
Chris Wilsoncced5e22017-02-23 07:44:15 +0000819 if (rcu_access_pointer(b->first_signal)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000820 wake_up_process(b->signaler);
821 busy |= intel_engine_flag(engine);
822 }
823
824 spin_unlock_irq(&b->lock);
825
826 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100827}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000828
829#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
830#include "selftests/intel_breadcrumbs.c"
831#endif