blob: 094f010908b8b7e39eed52243a7b10b9ecff3e95 [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
343 /* This request is completed, so remove it from the tree, mark it as
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000344 * complete, and *then* wake up the associated task. N.B. when the
345 * task wakes up, it will find the empty rb_node, discern that it
346 * has already been removed from the tree and skip the serialisation
347 * of the b->rb_lock and b->irq_lock. This means that the destruction
348 * of the intel_wait is not serialised with the interrupt handler
349 * by the waiter - it must instead be serialised by the caller.
Chris Wilson688e6c72016-07-01 17:23:15 +0100350 */
351 rb_erase(&wait->node, &b->waiters);
352 RB_CLEAR_NODE(&wait->node);
353
354 wake_up_process(wait->tsk); /* implicit smp_wmb() */
355}
356
Chris Wilsonb66255f2017-03-03 17:14:22 +0000357static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
358 struct rb_node *next)
359{
360 struct intel_breadcrumbs *b = &engine->breadcrumbs;
361
Chris Wilson61d3dc72017-03-03 19:08:24 +0000362 spin_lock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000363 GEM_BUG_ON(!b->irq_armed);
Chris Wilson429732e2017-03-15 21:07:23 +0000364 GEM_BUG_ON(!b->irq_wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000365 b->irq_wait = to_wait(next);
366 spin_unlock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000367
368 /* We always wake up the next waiter that takes over as the bottom-half
369 * as we may delegate not only the irq-seqno barrier to the next waiter
370 * but also the task of waking up concurrent waiters.
371 */
372 if (next)
373 wake_up_process(to_wait(next)->tsk);
374}
375
Chris Wilson688e6c72016-07-01 17:23:15 +0100376static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
377 struct intel_wait *wait)
378{
379 struct intel_breadcrumbs *b = &engine->breadcrumbs;
380 struct rb_node **p, *parent, *completed;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100381 bool first, armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100382 u32 seqno;
383
Chris Wilsonc68ce692018-01-02 19:25:00 +0000384 GEM_BUG_ON(!wait->seqno);
385
Chris Wilson688e6c72016-07-01 17:23:15 +0100386 /* Insert the request into the retirement ordered list
387 * of waiters by walking the rbtree. If we are the oldest
388 * seqno in the tree (the first to be retired), then
389 * set ourselves as the bottom-half.
390 *
391 * As we descend the tree, prune completed branches since we hold the
392 * spinlock we know that the first_waiter must be delayed and can
393 * reduce some of the sequential wake up latency if we take action
394 * ourselves and wake up the completed tasks in parallel. Also, by
395 * removing stale elements in the tree, we may be able to reduce the
396 * ping-pong between the old bottom-half and ourselves as first-waiter.
397 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100398 armed = false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100399 first = true;
400 parent = NULL;
401 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100402 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100403
404 /* If the request completed before we managed to grab the spinlock,
405 * return now before adding ourselves to the rbtree. We let the
406 * current bottom-half handle any pending wakeups and instead
407 * try and get out of the way quickly.
408 */
409 if (i915_seqno_passed(seqno, wait->seqno)) {
410 RB_CLEAR_NODE(&wait->node);
411 return first;
412 }
413
414 p = &b->waiters.rb_node;
415 while (*p) {
416 parent = *p;
417 if (wait->seqno == to_wait(parent)->seqno) {
418 /* We have multiple waiters on the same seqno, select
419 * the highest priority task (that with the smallest
420 * task->prio) to serve as the bottom-half for this
421 * group.
422 */
423 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
424 p = &parent->rb_right;
425 first = false;
426 } else {
427 p = &parent->rb_left;
428 }
429 } else if (i915_seqno_passed(wait->seqno,
430 to_wait(parent)->seqno)) {
431 p = &parent->rb_right;
432 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
433 completed = parent;
434 else
435 first = false;
436 } else {
437 p = &parent->rb_left;
438 }
439 }
440 rb_link_node(&wait->node, parent, p);
441 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100442
Chris Wilson688e6c72016-07-01 17:23:15 +0100443 if (first) {
Chris Wilson61d3dc72017-03-03 19:08:24 +0000444 spin_lock(&b->irq_lock);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000445 b->irq_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100446 /* After assigning ourselves as the new bottom-half, we must
447 * perform a cursory check to prevent a missed interrupt.
448 * Either we miss the interrupt whilst programming the hardware,
449 * or if there was a previous waiter (for a later seqno) they
450 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100451 * in the unlocked read of b->irq_seqno_bh in the irq handler)
452 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100453 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100454 armed = __intel_breadcrumbs_enable_irq(b);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000455 spin_unlock(&b->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100456 }
Chris Wilson429732e2017-03-15 21:07:23 +0000457
458 if (completed) {
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000459 /* Advance the bottom-half (b->irq_wait) before we wake up
460 * the waiters who may scribble over their intel_wait
461 * just as the interrupt handler is dereferencing it via
462 * b->irq_wait.
463 */
Chris Wilson429732e2017-03-15 21:07:23 +0000464 if (!first) {
465 struct rb_node *next = rb_next(completed);
466 GEM_BUG_ON(next == &wait->node);
467 __intel_breadcrumbs_next(engine, next);
468 }
469
470 do {
471 struct intel_wait *crumb = to_wait(completed);
472 completed = rb_prev(completed);
473 __intel_breadcrumbs_finish(b, crumb);
474 } while (completed);
475 }
476
Chris Wilson61d3dc72017-03-03 19:08:24 +0000477 GEM_BUG_ON(!b->irq_wait);
Chris Wilson429732e2017-03-15 21:07:23 +0000478 GEM_BUG_ON(!b->irq_armed);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000479 GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100480
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100481 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100482}
483
484bool intel_engine_add_wait(struct intel_engine_cs *engine,
485 struct intel_wait *wait)
486{
487 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100488 bool armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100489
Chris Wilson61d3dc72017-03-03 19:08:24 +0000490 spin_lock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100491 armed = __intel_engine_add_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000492 spin_unlock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100493 if (armed)
494 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100495
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100496 /* Make the caller recheck if its request has already started. */
497 return i915_seqno_passed(intel_engine_get_seqno(engine),
498 wait->seqno - 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100499}
500
Chris Wilson688e6c72016-07-01 17:23:15 +0100501static inline bool chain_wakeup(struct rb_node *rb, int priority)
502{
503 return rb && to_wait(rb)->tsk->prio <= priority;
504}
505
Chris Wilsonc81d4612016-07-01 17:23:25 +0100506static inline int wakeup_priority(struct intel_breadcrumbs *b,
507 struct task_struct *tsk)
508{
509 if (tsk == b->signaler)
510 return INT_MIN;
511 else
512 return tsk->prio;
513}
514
Chris Wilson9eb143b2017-02-23 07:44:16 +0000515static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
516 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100517{
518 struct intel_breadcrumbs *b = &engine->breadcrumbs;
519
Chris Wilson61d3dc72017-03-03 19:08:24 +0000520 lockdep_assert_held(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100521
522 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000523 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100524
Chris Wilson61d3dc72017-03-03 19:08:24 +0000525 if (b->irq_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100526 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100527 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100528
Chris Wilson688e6c72016-07-01 17:23:15 +0100529 /* We are the current bottom-half. Find the next candidate,
530 * the first waiter in the queue on the remaining oldest
531 * request. As multiple seqnos may complete in the time it
532 * takes us to wake up and find the next waiter, we have to
533 * wake up that waiter for it to perform its own coherent
534 * completion check.
535 */
536 next = rb_next(&wait->node);
537 if (chain_wakeup(next, priority)) {
538 /* If the next waiter is already complete,
539 * wake it up and continue onto the next waiter. So
540 * if have a small herd, they will wake up in parallel
541 * rather than sequentially, which should reduce
542 * the overall latency in waking all the completed
543 * clients.
544 *
545 * However, waking up a chain adds extra latency to
546 * the first_waiter. This is undesirable if that
547 * waiter is a high priority task.
548 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100549 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100550
551 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
552 struct rb_node *n = rb_next(next);
553
554 __intel_breadcrumbs_finish(b, to_wait(next));
555 next = n;
556 if (!chain_wakeup(next, priority))
557 break;
558 }
559 }
560
Chris Wilsonb66255f2017-03-03 17:14:22 +0000561 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100562 } else {
563 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
564 }
565
566 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
567 rb_erase(&wait->node, &b->waiters);
Chris Wilsonc5346122017-11-15 12:14:58 +0000568 RB_CLEAR_NODE(&wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100569
Chris Wilson9eb143b2017-02-23 07:44:16 +0000570out:
Chris Wilson61d3dc72017-03-03 19:08:24 +0000571 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100572 GEM_BUG_ON(rb_first(&b->waiters) !=
Chris Wilson61d3dc72017-03-03 19:08:24 +0000573 (b->irq_wait ? &b->irq_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000574}
575
576void intel_engine_remove_wait(struct intel_engine_cs *engine,
577 struct intel_wait *wait)
578{
579 struct intel_breadcrumbs *b = &engine->breadcrumbs;
580
581 /* Quick check to see if this waiter was already decoupled from
582 * the tree by the bottom-half to avoid contention on the spinlock
583 * by the herd.
584 */
Chris Wilson908a6cb2017-03-15 21:07:25 +0000585 if (RB_EMPTY_NODE(&wait->node)) {
586 GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000587 return;
Chris Wilson908a6cb2017-03-15 21:07:25 +0000588 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000589
Chris Wilson61d3dc72017-03-03 19:08:24 +0000590 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000591 __intel_engine_remove_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000592 spin_unlock_irq(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100593}
594
Chris Wilsone61e0f52018-02-21 09:56:36 +0000595static bool signal_complete(const struct i915_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100596{
Chris Wilsonb3850852016-07-01 17:23:26 +0100597 if (!request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100598 return false;
599
Chris Wilsonfd10e2c2018-02-06 09:46:33 +0000600 /*
601 * Carefully check if the request is complete, giving time for the
Chris Wilsonc81d4612016-07-01 17:23:25 +0100602 * seqno to be visible or if the GPU hung.
603 */
Chris Wilsonfd10e2c2018-02-06 09:46:33 +0000604 return __i915_request_irq_complete(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100605}
606
Chris Wilsone61e0f52018-02-21 09:56:36 +0000607static struct i915_request *to_signaler(struct rb_node *rb)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100608{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000609 return rb_entry(rb, struct i915_request, signaling.node);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100610}
611
612static void signaler_set_rtpriority(void)
613{
614 struct sched_param param = { .sched_priority = 1 };
615
616 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
617}
618
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000619static void __intel_engine_remove_signal(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +0000620 struct i915_request *request)
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000621{
622 struct intel_breadcrumbs *b = &engine->breadcrumbs;
623
624 lockdep_assert_held(&b->rb_lock);
625
626 /*
627 * Wake up all other completed waiters and select the
628 * next bottom-half for the next user interrupt.
629 */
630 __intel_engine_remove_wait(engine, &request->signaling.wait);
631
632 /*
633 * Find the next oldest signal. Note that as we have
634 * not been holding the lock, another client may
635 * have installed an even older signal than the one
636 * we just completed - so double check we are still
637 * the oldest before picking the next one.
638 */
639 if (request->signaling.wait.seqno) {
640 if (request == rcu_access_pointer(b->first_signal)) {
641 struct rb_node *rb = rb_next(&request->signaling.node);
642 rcu_assign_pointer(b->first_signal,
643 rb ? to_signaler(rb) : NULL);
644 }
645
646 rb_erase(&request->signaling.node, &b->signals);
647 request->signaling.wait.seqno = 0;
648 }
649}
650
Chris Wilsone61e0f52018-02-21 09:56:36 +0000651static struct i915_request *
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000652get_first_signal_rcu(struct intel_breadcrumbs *b)
653{
654 /*
655 * See the big warnings for i915_gem_active_get_rcu() and similarly
656 * for dma_fence_get_rcu_safe() that explain the intricacies involved
657 * here with defeating CPU/compiler speculation and enforcing
658 * the required memory barriers.
659 */
660 do {
Chris Wilsone61e0f52018-02-21 09:56:36 +0000661 struct i915_request *request;
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000662
663 request = rcu_dereference(b->first_signal);
664 if (request)
Chris Wilsone61e0f52018-02-21 09:56:36 +0000665 request = i915_request_get_rcu(request);
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000666
667 barrier();
668
669 if (!request || request == rcu_access_pointer(b->first_signal))
670 return rcu_pointer_handoff(request);
671
Chris Wilsone61e0f52018-02-21 09:56:36 +0000672 i915_request_put(request);
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000673 } while (1);
674}
675
Chris Wilsonc81d4612016-07-01 17:23:25 +0100676static int intel_breadcrumbs_signaler(void *arg)
677{
678 struct intel_engine_cs *engine = arg;
679 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsone61e0f52018-02-21 09:56:36 +0000680 struct i915_request *request;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100681
682 /* Install ourselves with high priority to reduce signalling latency */
683 signaler_set_rtpriority();
684
685 do {
Chris Wilsona7980a62017-04-04 13:05:31 +0100686 bool do_schedule = true;
687
Chris Wilsonc81d4612016-07-01 17:23:25 +0100688 set_current_state(TASK_INTERRUPTIBLE);
689
690 /* We are either woken up by the interrupt bottom-half,
691 * or by a client adding a new signaller. In both cases,
692 * the GPU seqno may have advanced beyond our oldest signal.
693 * If it has, propagate the signal, remove the waiter and
694 * check again with the next oldest signal. Otherwise we
695 * need to wait for a new interrupt from the GPU or for
696 * a new client.
697 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000698 rcu_read_lock();
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000699 request = get_first_signal_rcu(b);
Chris Wilsoncced5e22017-02-23 07:44:15 +0000700 rcu_read_unlock();
Chris Wilsonb3850852016-07-01 17:23:26 +0100701 if (signal_complete(request)) {
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000702 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
703 &request->fence.flags)) {
704 local_bh_disable();
705 dma_fence_signal(&request->fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000706 GEM_BUG_ON(!i915_request_completed(request));
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000707 local_bh_enable(); /* kick start the tasklets */
Chris Wilsonb3850852016-07-01 17:23:26 +0100708 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000709
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000710 if (READ_ONCE(request->signaling.wait.seqno)) {
711 spin_lock_irq(&b->rb_lock);
712 __intel_engine_remove_signal(engine, request);
713 spin_unlock_irq(&b->rb_lock);
714 }
Chris Wilsona7980a62017-04-04 13:05:31 +0100715
716 /* If the engine is saturated we may be continually
717 * processing completed requests. This angers the
718 * NMI watchdog if we never let anything else
719 * have access to the CPU. Let's pretend to be nice
720 * and relinquish the CPU if we burn through the
721 * entire RT timeslice!
722 */
723 do_schedule = need_resched();
724 }
Chris Wilsone61e0f52018-02-21 09:56:36 +0000725 i915_request_put(request);
Chris Wilsona7980a62017-04-04 13:05:31 +0100726
727 if (unlikely(do_schedule)) {
Chris Wilsonb1becb82017-04-03 11:51:24 +0100728 if (kthread_should_park())
729 kthread_parkme();
730
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000731 if (unlikely(kthread_should_stop()))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100732 break;
733
734 schedule();
735 }
736 } while (1);
737 __set_current_state(TASK_RUNNING);
738
739 return 0;
740}
741
Chris Wilsone61e0f52018-02-21 09:56:36 +0000742void intel_engine_enable_signaling(struct i915_request *request, bool wakeup)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100743{
744 struct intel_engine_cs *engine = request->engine;
745 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000746 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100747
Chris Wilsonf6168e32016-10-28 13:58:55 +0100748 /* Note that we may be called from an interrupt handler on another
749 * device (e.g. nouveau signaling a fence completion causing us
750 * to submit a request, and so enable signaling). As such,
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000751 * we need to make sure that all other users of b->rb_lock protect
Chris Wilsonf6168e32016-10-28 13:58:55 +0100752 * against interrupts, i.e. use spin_lock_irqsave.
753 */
754
755 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilsone60a8702017-03-02 11:51:30 +0000756 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000757 lockdep_assert_held(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000758
Chris Wilsone61e0f52018-02-21 09:56:36 +0000759 seqno = i915_request_global_seqno(request);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000760 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100761 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100762
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000763 spin_lock(&b->rb_lock);
764
765 GEM_BUG_ON(request->signaling.wait.seqno);
Chris Wilsonb3850852016-07-01 17:23:26 +0100766 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000767 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000768 request->signaling.wait.seqno = seqno;
Chris Wilson4a50d202016-07-26 12:01:50 +0100769
Chris Wilsonc81d4612016-07-01 17:23:25 +0100770 /* First add ourselves into the list of waiters, but register our
771 * bottom-half as the signaller thread. As per usual, only the oldest
772 * waiter (not just signaller) is tasked as the bottom-half waking
773 * up all completed waiters after the user interrupt.
774 *
775 * If we are the oldest waiter, enable the irq (after which we
776 * must double check that the seqno did not complete).
777 */
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100778 wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100779
Chris Wilsone61e0f52018-02-21 09:56:36 +0000780 if (!__i915_request_completed(request, seqno)) {
Chris Wilson735e0eb2017-06-08 12:14:04 +0100781 struct rb_node *parent, **p;
782 bool first;
783
784 /* Now insert ourselves into the retirement ordered list of
785 * signals on this engine. We track the oldest seqno as that
786 * will be the first signal to complete.
787 */
788 parent = NULL;
789 first = true;
790 p = &b->signals.rb_node;
791 while (*p) {
792 parent = *p;
793 if (i915_seqno_passed(seqno,
794 to_signaler(parent)->signaling.wait.seqno)) {
795 p = &parent->rb_right;
796 first = false;
797 } else {
798 p = &parent->rb_left;
799 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100800 }
Chris Wilson735e0eb2017-06-08 12:14:04 +0100801 rb_link_node(&request->signaling.node, parent, p);
802 rb_insert_color(&request->signaling.node, &b->signals);
803 if (first)
804 rcu_assign_pointer(b->first_signal, request);
805 } else {
806 __intel_engine_remove_wait(engine, &request->signaling.wait);
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000807 request->signaling.wait.seqno = 0;
Chris Wilson735e0eb2017-06-08 12:14:04 +0100808 wakeup = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100809 }
Chris Wilsonb3850852016-07-01 17:23:26 +0100810
Chris Wilson61d3dc72017-03-03 19:08:24 +0000811 spin_unlock(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100812
813 if (wakeup)
814 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100815}
816
Chris Wilsone61e0f52018-02-21 09:56:36 +0000817void intel_engine_cancel_signaling(struct i915_request *request)
Chris Wilson9eb143b2017-02-23 07:44:16 +0000818{
Chris Wilsone60a8702017-03-02 11:51:30 +0000819 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000820 lockdep_assert_held(&request->lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000821
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000822 if (READ_ONCE(request->signaling.wait.seqno)) {
823 struct intel_engine_cs *engine = request->engine;
824 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson9eb143b2017-02-23 07:44:16 +0000825
Chris Wilsonb7a3f332018-02-03 10:19:14 +0000826 spin_lock(&b->rb_lock);
827 __intel_engine_remove_signal(engine, request);
828 spin_unlock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000829 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000830}
831
Chris Wilson688e6c72016-07-01 17:23:15 +0100832int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
833{
834 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100835 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100836
Chris Wilson61d3dc72017-03-03 19:08:24 +0000837 spin_lock_init(&b->rb_lock);
838 spin_lock_init(&b->irq_lock);
839
Kees Cook39cbf2a2017-10-17 09:53:04 +0300840 timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
841 timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
Chris Wilson688e6c72016-07-01 17:23:15 +0100842
Chris Wilsonc81d4612016-07-01 17:23:25 +0100843 /* Spawn a thread to provide a common bottom-half for all signals.
844 * As this is an asynchronous interface we cannot steal the current
845 * task for handling the bottom-half to the user interrupt, therefore
846 * we create a thread to do the coherent seqno dance after the
847 * interrupt and then signal the waitqueue (via the dma-buf/fence).
848 */
849 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
850 "i915/signal:%d", engine->id);
851 if (IS_ERR(tsk))
852 return PTR_ERR(tsk);
853
854 b->signaler = tsk;
855
Chris Wilson688e6c72016-07-01 17:23:15 +0100856 return 0;
857}
858
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100859static void cancel_fake_irq(struct intel_engine_cs *engine)
860{
861 struct intel_breadcrumbs *b = &engine->breadcrumbs;
862
863 del_timer_sync(&b->hangcheck);
864 del_timer_sync(&b->fake_irq);
865 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
866}
867
868void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
869{
870 struct intel_breadcrumbs *b = &engine->breadcrumbs;
871
872 cancel_fake_irq(engine);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000873 spin_lock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100874
Chris Wilson67b807a82017-02-27 20:58:50 +0000875 if (b->irq_enabled)
876 irq_enable(engine);
877 else
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100878 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000879
880 /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
881 * GPU is active and may have already executed the MI_USER_INTERRUPT
882 * before the CPU is ready to receive. However, the engine is currently
883 * idle (we haven't started it yet), there is no possibility for a
884 * missed interrupt as we enabled the irq and so we can clear the
885 * immediate wakeup (until a real interrupt arrives for the waiter).
886 */
887 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
888
889 if (b->irq_armed)
890 enable_fake_irq(b);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100891
Chris Wilson61d3dc72017-03-03 19:08:24 +0000892 spin_unlock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100893}
894
Chris Wilson688e6c72016-07-01 17:23:15 +0100895void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
896{
897 struct intel_breadcrumbs *b = &engine->breadcrumbs;
898
Chris Wilson381744f2016-11-21 11:07:59 +0000899 /* The engines should be idle and all requests accounted for! */
Chris Wilson61d3dc72017-03-03 19:08:24 +0000900 WARN_ON(READ_ONCE(b->irq_wait));
Chris Wilson381744f2016-11-21 11:07:59 +0000901 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncced5e22017-02-23 07:44:15 +0000902 WARN_ON(rcu_access_pointer(b->first_signal));
Chris Wilson381744f2016-11-21 11:07:59 +0000903 WARN_ON(!RB_EMPTY_ROOT(&b->signals));
904
Chris Wilsonc81d4612016-07-01 17:23:25 +0100905 if (!IS_ERR_OR_NULL(b->signaler))
906 kthread_stop(b->signaler);
907
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100908 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100909}
910
Chris Wilson9b6586a2017-02-23 07:44:08 +0000911bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100912{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000913 struct intel_breadcrumbs *b = &engine->breadcrumbs;
914 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100915
Chris Wilson61d3dc72017-03-03 19:08:24 +0000916 spin_lock_irq(&b->rb_lock);
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000917
Chris Wilson61d3dc72017-03-03 19:08:24 +0000918 if (b->irq_wait) {
919 wake_up_process(b->irq_wait->tsk);
Chris Wilson4bd66392017-03-15 21:07:22 +0000920 busy = true;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100921 }
922
Chris Wilsoncced5e22017-02-23 07:44:15 +0000923 if (rcu_access_pointer(b->first_signal)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000924 wake_up_process(b->signaler);
Chris Wilson4bd66392017-03-15 21:07:22 +0000925 busy = true;
Chris Wilson9b6586a2017-02-23 07:44:08 +0000926 }
927
Chris Wilson61d3dc72017-03-03 19:08:24 +0000928 spin_unlock_irq(&b->rb_lock);
Chris Wilson9b6586a2017-02-23 07:44:08 +0000929
930 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100931}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000932
933#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
934#include "selftests/intel_breadcrumbs.c"
935#endif