blob: 4c4fbf5f20f9b439592b2f2eaafa10cd02113128 [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 Wilsonbcbd5c32017-10-25 15:39:42 +0100126 if (b->irq_armed && !__intel_breadcrumbs_wakeup(b))
Chris Wilson67b807a82017-02-27 20:58:50 +0000127 __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 Wilsonc16c4ba2017-11-07 10:20:03 +0000148 /*
149 * FIXME: Ideally we want this on the API boundary, but for the
150 * sake of testing with mock breadcrumbs (no HW so unable to
151 * enable irqs) we place it deep within the bowels, at the point
152 * of no return.
153 */
154 GEM_BUG_ON(!intel_irqs_enabled(engine->i915));
155
Chris Wilson3d5564e2016-07-01 17:23:23 +0100156 /* Enabling the IRQ may miss the generation of the interrupt, but
157 * we still need to force the barrier before reading the seqno,
158 * just in case.
159 */
Chris Wilson538b2572017-01-24 15:18:05 +0000160 set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100161
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_enable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100165 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100166}
167
168static void irq_disable(struct intel_engine_cs *engine)
169{
Chris Wilsonf6168e32016-10-28 13:58:55 +0100170 /* Caller disables interrupts */
171 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100172 engine->irq_disable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100173 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100174}
175
Chris Wilson67b807a82017-02-27 20:58:50 +0000176void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
177{
178 struct intel_breadcrumbs *b = &engine->breadcrumbs;
179
Chris Wilson61d3dc72017-03-03 19:08:24 +0000180 lockdep_assert_held(&b->irq_lock);
Chris Wilsone1c0c912017-03-06 09:29:15 +0000181 GEM_BUG_ON(b->irq_wait);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100182 GEM_BUG_ON(!b->irq_armed);
Chris Wilson67b807a82017-02-27 20:58:50 +0000183
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100184 GEM_BUG_ON(!b->irq_enabled);
185 if (!--b->irq_enabled)
Chris Wilson67b807a82017-02-27 20:58:50 +0000186 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000187
188 b->irq_armed = false;
189}
190
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100191void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
192{
193 struct intel_breadcrumbs *b = &engine->breadcrumbs;
194
195 spin_lock_irq(&b->irq_lock);
196 if (!b->irq_enabled++)
197 irq_enable(engine);
198 GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
199 spin_unlock_irq(&b->irq_lock);
200}
201
202void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
203{
204 struct intel_breadcrumbs *b = &engine->breadcrumbs;
205
206 spin_lock_irq(&b->irq_lock);
207 GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
208 if (!--b->irq_enabled)
209 irq_disable(engine);
210 spin_unlock_irq(&b->irq_lock);
211}
212
Chris Wilson67b807a82017-02-27 20:58:50 +0000213void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
214{
215 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000216 struct intel_wait *wait, *n, *first;
Chris Wilson67b807a82017-02-27 20:58:50 +0000217
218 if (!b->irq_armed)
219 return;
220
Chris Wilson67b807a82017-02-27 20:58:50 +0000221 /* We only disarm the irq when we are idle (all requests completed),
Chris Wilsone1c0c912017-03-06 09:29:15 +0000222 * so if the bottom-half remains asleep, it missed the request
Chris Wilson67b807a82017-02-27 20:58:50 +0000223 * completion.
224 */
Chris Wilson67b807a82017-02-27 20:58:50 +0000225
Chris Wilsone1c0c912017-03-06 09:29:15 +0000226 spin_lock_irq(&b->rb_lock);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000227
228 spin_lock(&b->irq_lock);
229 first = fetch_and_zero(&b->irq_wait);
Chris Wilsone5330ac2017-10-31 12:22:35 +0000230 if (b->irq_armed)
231 __intel_engine_disarm_breadcrumbs(engine);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000232 spin_unlock(&b->irq_lock);
233
Chris Wilsone1c0c912017-03-06 09:29:15 +0000234 rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
235 RB_CLEAR_NODE(&wait->node);
Chris Wilsona5cae7b2017-03-15 21:07:24 +0000236 if (wake_up_process(wait->tsk) && wait == first)
Chris Wilsone1c0c912017-03-06 09:29:15 +0000237 missed_breadcrumb(engine);
238 }
239 b->waiters = RB_ROOT;
240
Chris Wilsone1c0c912017-03-06 09:29:15 +0000241 spin_unlock_irq(&b->rb_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000242}
243
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000244static bool use_fake_irq(const struct intel_breadcrumbs *b)
245{
246 const struct intel_engine_cs *engine =
247 container_of(b, struct intel_engine_cs, breadcrumbs);
248
249 if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
250 return false;
251
252 /* Only start with the heavy weight fake irq timer if we have not
253 * seen any interrupts since enabling it the first time. If the
254 * interrupts are still arriving, it means we made a mistake in our
255 * engine->seqno_barrier(), a timing error that should be transient
256 * and unlikely to reoccur.
257 */
258 return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
259}
260
Chris Wilson67b807a82017-02-27 20:58:50 +0000261static void enable_fake_irq(struct intel_breadcrumbs *b)
262{
263 /* Ensure we never sleep indefinitely */
264 if (!b->irq_enabled || use_fake_irq(b))
265 mod_timer(&b->fake_irq, jiffies + 1);
266 else
267 mod_timer(&b->hangcheck, wait_timeout());
268}
269
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100270static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
Chris Wilson688e6c72016-07-01 17:23:15 +0100271{
272 struct intel_engine_cs *engine =
273 container_of(b, struct intel_engine_cs, breadcrumbs);
274 struct drm_i915_private *i915 = engine->i915;
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100275 bool enabled;
276
Chris Wilson61d3dc72017-03-03 19:08:24 +0000277 lockdep_assert_held(&b->irq_lock);
Chris Wilson67b807a82017-02-27 20:58:50 +0000278 if (b->irq_armed)
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100279 return false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100280
Chris Wilson67b807a82017-02-27 20:58:50 +0000281 /* The breadcrumb irq will be disarmed on the interrupt after the
282 * waiters are signaled. This gives us a single interrupt window in
283 * which we can add a new waiter and avoid the cost of re-enabling
284 * the irq.
285 */
286 b->irq_armed = true;
Chris Wilson67b807a82017-02-27 20:58:50 +0000287
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000288 if (I915_SELFTEST_ONLY(b->mock)) {
289 /* For our mock objects we want to avoid interaction
290 * with the real hardware (which is not set up). So
291 * we simply pretend we have enabled the powerwell
292 * and the irq, and leave it up to the mock
293 * implementation to call intel_engine_wakeup()
294 * itself when it wants to simulate a user interrupt,
295 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100296 return true;
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000297 }
298
Chris Wilson688e6c72016-07-01 17:23:15 +0100299 /* Since we are waiting on a request, the GPU should be busy
Chris Wilson67b807a82017-02-27 20:58:50 +0000300 * and should have its own rpm reference. This is tracked
301 * by i915->gt.awake, we can forgo holding our own wakref
302 * for the interrupt as before i915->gt.awake is released (when
303 * the driver is idle) we disarm the breadcrumbs.
Chris Wilson688e6c72016-07-01 17:23:15 +0100304 */
Chris Wilson688e6c72016-07-01 17:23:15 +0100305
306 /* No interrupts? Kick the waiter every jiffie! */
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100307 enabled = false;
308 if (!b->irq_enabled++ &&
309 !test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
310 irq_enable(engine);
311 enabled = true;
Chris Wilson688e6c72016-07-01 17:23:15 +0100312 }
313
Chris Wilson67b807a82017-02-27 20:58:50 +0000314 enable_fake_irq(b);
Chris Wilsonbcbd5c32017-10-25 15:39:42 +0100315 return enabled;
Chris Wilson688e6c72016-07-01 17:23:15 +0100316}
317
318static inline struct intel_wait *to_wait(struct rb_node *node)
319{
Chris Wilsond8567862016-12-20 10:40:03 +0000320 return rb_entry(node, struct intel_wait, node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100321}
322
323static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
324 struct intel_wait *wait)
325{
Chris Wilson61d3dc72017-03-03 19:08:24 +0000326 lockdep_assert_held(&b->rb_lock);
Chris Wilson908a6cb2017-03-15 21:07:25 +0000327 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100328
329 /* This request is completed, so remove it from the tree, mark it as
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000330 * complete, and *then* wake up the associated task. N.B. when the
331 * task wakes up, it will find the empty rb_node, discern that it
332 * has already been removed from the tree and skip the serialisation
333 * of the b->rb_lock and b->irq_lock. This means that the destruction
334 * of the intel_wait is not serialised with the interrupt handler
335 * by the waiter - it must instead be serialised by the caller.
Chris Wilson688e6c72016-07-01 17:23:15 +0100336 */
337 rb_erase(&wait->node, &b->waiters);
338 RB_CLEAR_NODE(&wait->node);
339
340 wake_up_process(wait->tsk); /* implicit smp_wmb() */
341}
342
Chris Wilsonb66255f2017-03-03 17:14:22 +0000343static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
344 struct rb_node *next)
345{
346 struct intel_breadcrumbs *b = &engine->breadcrumbs;
347
Chris Wilson61d3dc72017-03-03 19:08:24 +0000348 spin_lock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000349 GEM_BUG_ON(!b->irq_armed);
Chris Wilson429732e2017-03-15 21:07:23 +0000350 GEM_BUG_ON(!b->irq_wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000351 b->irq_wait = to_wait(next);
352 spin_unlock(&b->irq_lock);
Chris Wilsonb66255f2017-03-03 17:14:22 +0000353
354 /* We always wake up the next waiter that takes over as the bottom-half
355 * as we may delegate not only the irq-seqno barrier to the next waiter
356 * but also the task of waking up concurrent waiters.
357 */
358 if (next)
359 wake_up_process(to_wait(next)->tsk);
360}
361
Chris Wilson688e6c72016-07-01 17:23:15 +0100362static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
363 struct intel_wait *wait)
364{
365 struct intel_breadcrumbs *b = &engine->breadcrumbs;
366 struct rb_node **p, *parent, *completed;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100367 bool first, armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100368 u32 seqno;
369
370 /* Insert the request into the retirement ordered list
371 * of waiters by walking the rbtree. If we are the oldest
372 * seqno in the tree (the first to be retired), then
373 * set ourselves as the bottom-half.
374 *
375 * As we descend the tree, prune completed branches since we hold the
376 * spinlock we know that the first_waiter must be delayed and can
377 * reduce some of the sequential wake up latency if we take action
378 * ourselves and wake up the completed tasks in parallel. Also, by
379 * removing stale elements in the tree, we may be able to reduce the
380 * ping-pong between the old bottom-half and ourselves as first-waiter.
381 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100382 armed = false;
Chris Wilson688e6c72016-07-01 17:23:15 +0100383 first = true;
384 parent = NULL;
385 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100386 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100387
388 /* If the request completed before we managed to grab the spinlock,
389 * return now before adding ourselves to the rbtree. We let the
390 * current bottom-half handle any pending wakeups and instead
391 * try and get out of the way quickly.
392 */
393 if (i915_seqno_passed(seqno, wait->seqno)) {
394 RB_CLEAR_NODE(&wait->node);
395 return first;
396 }
397
398 p = &b->waiters.rb_node;
399 while (*p) {
400 parent = *p;
401 if (wait->seqno == to_wait(parent)->seqno) {
402 /* We have multiple waiters on the same seqno, select
403 * the highest priority task (that with the smallest
404 * task->prio) to serve as the bottom-half for this
405 * group.
406 */
407 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
408 p = &parent->rb_right;
409 first = false;
410 } else {
411 p = &parent->rb_left;
412 }
413 } else if (i915_seqno_passed(wait->seqno,
414 to_wait(parent)->seqno)) {
415 p = &parent->rb_right;
416 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
417 completed = parent;
418 else
419 first = false;
420 } else {
421 p = &parent->rb_left;
422 }
423 }
424 rb_link_node(&wait->node, parent, p);
425 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100426
Chris Wilson688e6c72016-07-01 17:23:15 +0100427 if (first) {
Chris Wilson61d3dc72017-03-03 19:08:24 +0000428 spin_lock(&b->irq_lock);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000429 b->irq_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100430 /* After assigning ourselves as the new bottom-half, we must
431 * perform a cursory check to prevent a missed interrupt.
432 * Either we miss the interrupt whilst programming the hardware,
433 * or if there was a previous waiter (for a later seqno) they
434 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100435 * in the unlocked read of b->irq_seqno_bh in the irq handler)
436 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100437 */
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100438 armed = __intel_breadcrumbs_enable_irq(b);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000439 spin_unlock(&b->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100440 }
Chris Wilson429732e2017-03-15 21:07:23 +0000441
442 if (completed) {
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000443 /* Advance the bottom-half (b->irq_wait) before we wake up
444 * the waiters who may scribble over their intel_wait
445 * just as the interrupt handler is dereferencing it via
446 * b->irq_wait.
447 */
Chris Wilson429732e2017-03-15 21:07:23 +0000448 if (!first) {
449 struct rb_node *next = rb_next(completed);
450 GEM_BUG_ON(next == &wait->node);
451 __intel_breadcrumbs_next(engine, next);
452 }
453
454 do {
455 struct intel_wait *crumb = to_wait(completed);
456 completed = rb_prev(completed);
457 __intel_breadcrumbs_finish(b, crumb);
458 } while (completed);
459 }
460
Chris Wilson61d3dc72017-03-03 19:08:24 +0000461 GEM_BUG_ON(!b->irq_wait);
Chris Wilson429732e2017-03-15 21:07:23 +0000462 GEM_BUG_ON(!b->irq_armed);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000463 GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100464
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100465 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100466}
467
468bool intel_engine_add_wait(struct intel_engine_cs *engine,
469 struct intel_wait *wait)
470{
471 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100472 bool armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100473
Chris Wilson61d3dc72017-03-03 19:08:24 +0000474 spin_lock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100475 armed = __intel_engine_add_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000476 spin_unlock_irq(&b->rb_lock);
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100477 if (armed)
478 return armed;
Chris Wilson688e6c72016-07-01 17:23:15 +0100479
Chris Wilsonbac2ef42017-06-08 12:14:03 +0100480 /* Make the caller recheck if its request has already started. */
481 return i915_seqno_passed(intel_engine_get_seqno(engine),
482 wait->seqno - 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100483}
484
Chris Wilson688e6c72016-07-01 17:23:15 +0100485static inline bool chain_wakeup(struct rb_node *rb, int priority)
486{
487 return rb && to_wait(rb)->tsk->prio <= priority;
488}
489
Chris Wilsonc81d4612016-07-01 17:23:25 +0100490static inline int wakeup_priority(struct intel_breadcrumbs *b,
491 struct task_struct *tsk)
492{
493 if (tsk == b->signaler)
494 return INT_MIN;
495 else
496 return tsk->prio;
497}
498
Chris Wilson9eb143b2017-02-23 07:44:16 +0000499static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
500 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100501{
502 struct intel_breadcrumbs *b = &engine->breadcrumbs;
503
Chris Wilson61d3dc72017-03-03 19:08:24 +0000504 lockdep_assert_held(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100505
506 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000507 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100508
Chris Wilson61d3dc72017-03-03 19:08:24 +0000509 if (b->irq_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100510 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100511 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100512
Chris Wilson688e6c72016-07-01 17:23:15 +0100513 /* We are the current bottom-half. Find the next candidate,
514 * the first waiter in the queue on the remaining oldest
515 * request. As multiple seqnos may complete in the time it
516 * takes us to wake up and find the next waiter, we have to
517 * wake up that waiter for it to perform its own coherent
518 * completion check.
519 */
520 next = rb_next(&wait->node);
521 if (chain_wakeup(next, priority)) {
522 /* If the next waiter is already complete,
523 * wake it up and continue onto the next waiter. So
524 * if have a small herd, they will wake up in parallel
525 * rather than sequentially, which should reduce
526 * the overall latency in waking all the completed
527 * clients.
528 *
529 * However, waking up a chain adds extra latency to
530 * the first_waiter. This is undesirable if that
531 * waiter is a high priority task.
532 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100533 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100534
535 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
536 struct rb_node *n = rb_next(next);
537
538 __intel_breadcrumbs_finish(b, to_wait(next));
539 next = n;
540 if (!chain_wakeup(next, priority))
541 break;
542 }
543 }
544
Chris Wilsonb66255f2017-03-03 17:14:22 +0000545 __intel_breadcrumbs_next(engine, next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100546 } else {
547 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
548 }
549
550 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
551 rb_erase(&wait->node, &b->waiters);
552
Chris Wilson9eb143b2017-02-23 07:44:16 +0000553out:
Chris Wilson61d3dc72017-03-03 19:08:24 +0000554 GEM_BUG_ON(b->irq_wait == wait);
Chris Wilson688e6c72016-07-01 17:23:15 +0100555 GEM_BUG_ON(rb_first(&b->waiters) !=
Chris Wilson61d3dc72017-03-03 19:08:24 +0000556 (b->irq_wait ? &b->irq_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000557}
558
559void intel_engine_remove_wait(struct intel_engine_cs *engine,
560 struct intel_wait *wait)
561{
562 struct intel_breadcrumbs *b = &engine->breadcrumbs;
563
564 /* Quick check to see if this waiter was already decoupled from
565 * the tree by the bottom-half to avoid contention on the spinlock
566 * by the herd.
567 */
Chris Wilson908a6cb2017-03-15 21:07:25 +0000568 if (RB_EMPTY_NODE(&wait->node)) {
569 GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000570 return;
Chris Wilson908a6cb2017-03-15 21:07:25 +0000571 }
Chris Wilson9eb143b2017-02-23 07:44:16 +0000572
Chris Wilson61d3dc72017-03-03 19:08:24 +0000573 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000574 __intel_engine_remove_wait(engine, wait);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000575 spin_unlock_irq(&b->rb_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100576}
577
Chris Wilsond6a22892017-02-23 07:44:17 +0000578static bool signal_valid(const struct drm_i915_gem_request *request)
579{
580 return intel_wait_check_request(&request->signaling.wait, request);
581}
582
583static bool signal_complete(const struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100584{
Chris Wilsonb3850852016-07-01 17:23:26 +0100585 if (!request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100586 return false;
587
588 /* If another process served as the bottom-half it may have already
589 * signalled that this wait is already completed.
590 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100591 if (intel_wait_complete(&request->signaling.wait))
Chris Wilsond6a22892017-02-23 07:44:17 +0000592 return signal_valid(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100593
594 /* Carefully check if the request is complete, giving time for the
595 * seqno to be visible or if the GPU hung.
596 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100597 if (__i915_request_irq_complete(request))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100598 return true;
599
600 return false;
601}
602
Chris Wilsonb3850852016-07-01 17:23:26 +0100603static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100604{
Chris Wilsond8567862016-12-20 10:40:03 +0000605 return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100606}
607
608static void signaler_set_rtpriority(void)
609{
610 struct sched_param param = { .sched_priority = 1 };
611
612 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
613}
614
615static int intel_breadcrumbs_signaler(void *arg)
616{
617 struct intel_engine_cs *engine = arg;
618 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonb3850852016-07-01 17:23:26 +0100619 struct drm_i915_gem_request *request;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100620
621 /* Install ourselves with high priority to reduce signalling latency */
622 signaler_set_rtpriority();
623
624 do {
Chris Wilsona7980a62017-04-04 13:05:31 +0100625 bool do_schedule = true;
626
Chris Wilsonc81d4612016-07-01 17:23:25 +0100627 set_current_state(TASK_INTERRUPTIBLE);
628
629 /* We are either woken up by the interrupt bottom-half,
630 * or by a client adding a new signaller. In both cases,
631 * the GPU seqno may have advanced beyond our oldest signal.
632 * If it has, propagate the signal, remove the waiter and
633 * check again with the next oldest signal. Otherwise we
634 * need to wait for a new interrupt from the GPU or for
635 * a new client.
636 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000637 rcu_read_lock();
638 request = rcu_dereference(b->first_signal);
639 if (request)
640 request = i915_gem_request_get_rcu(request);
641 rcu_read_unlock();
Chris Wilsonb3850852016-07-01 17:23:26 +0100642 if (signal_complete(request)) {
Chris Wilson7c9e9342017-01-24 11:00:09 +0000643 local_bh_disable();
644 dma_fence_signal(&request->fence);
645 local_bh_enable(); /* kick start the tasklets */
646
Chris Wilson61d3dc72017-03-03 19:08:24 +0000647 spin_lock_irq(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000648
Chris Wilsonc81d4612016-07-01 17:23:25 +0100649 /* Wake up all other completed waiters and select the
650 * next bottom-half for the next user interrupt.
651 */
Chris Wilson9eb143b2017-02-23 07:44:16 +0000652 __intel_engine_remove_wait(engine,
653 &request->signaling.wait);
Chris Wilson5590af32016-09-09 14:11:54 +0100654
Chris Wilsonc81d4612016-07-01 17:23:25 +0100655 /* Find the next oldest signal. Note that as we have
656 * not been holding the lock, another client may
657 * have installed an even older signal than the one
658 * we just completed - so double check we are still
659 * the oldest before picking the next one.
660 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000661 if (request == rcu_access_pointer(b->first_signal)) {
Chris Wilsonb3850852016-07-01 17:23:26 +0100662 struct rb_node *rb =
663 rb_next(&request->signaling.node);
Chris Wilsoncced5e22017-02-23 07:44:15 +0000664 rcu_assign_pointer(b->first_signal,
665 rb ? to_signaler(rb) : NULL);
Chris Wilsonb3850852016-07-01 17:23:26 +0100666 }
667 rb_erase(&request->signaling.node, &b->signals);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000668 RB_CLEAR_NODE(&request->signaling.node);
669
Chris Wilson61d3dc72017-03-03 19:08:24 +0000670 spin_unlock_irq(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100671
Chris Wilsone8a261e2016-07-20 13:31:49 +0100672 i915_gem_request_put(request);
Chris Wilsona7980a62017-04-04 13:05:31 +0100673
674 /* If the engine is saturated we may be continually
675 * processing completed requests. This angers the
676 * NMI watchdog if we never let anything else
677 * have access to the CPU. Let's pretend to be nice
678 * and relinquish the CPU if we burn through the
679 * entire RT timeslice!
680 */
681 do_schedule = need_resched();
682 }
683
684 if (unlikely(do_schedule)) {
Chris Wilsond6a22892017-02-23 07:44:17 +0000685 DEFINE_WAIT(exec);
686
Chris Wilsonb1becb82017-04-03 11:51:24 +0100687 if (kthread_should_park())
688 kthread_parkme();
689
Chris Wilsoncced5e22017-02-23 07:44:15 +0000690 if (kthread_should_stop()) {
691 GEM_BUG_ON(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100692 break;
Chris Wilsoncced5e22017-02-23 07:44:15 +0000693 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100694
Chris Wilsond6a22892017-02-23 07:44:17 +0000695 if (request)
696 add_wait_queue(&request->execute, &exec);
697
Chris Wilsonc81d4612016-07-01 17:23:25 +0100698 schedule();
Chris Wilsonfe3288b2017-02-12 17:20:01 +0000699
Chris Wilsond6a22892017-02-23 07:44:17 +0000700 if (request)
701 remove_wait_queue(&request->execute, &exec);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100702 }
Chris Wilsoncced5e22017-02-23 07:44:15 +0000703 i915_gem_request_put(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100704 } while (1);
705 __set_current_state(TASK_RUNNING);
706
707 return 0;
708}
709
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100710void intel_engine_enable_signaling(struct drm_i915_gem_request *request,
711 bool wakeup)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100712{
713 struct intel_engine_cs *engine = request->engine;
714 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000715 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100716
Chris Wilsonf6168e32016-10-28 13:58:55 +0100717 /* Note that we may be called from an interrupt handler on another
718 * device (e.g. nouveau signaling a fence completion causing us
719 * to submit a request, and so enable signaling). As such,
Chris Wilsona6b0a1412017-03-15 22:22:59 +0000720 * we need to make sure that all other users of b->rb_lock protect
Chris Wilsonf6168e32016-10-28 13:58:55 +0100721 * against interrupts, i.e. use spin_lock_irqsave.
722 */
723
724 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilsone60a8702017-03-02 11:51:30 +0000725 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000726 lockdep_assert_held(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000727
728 seqno = i915_gem_request_global_seqno(request);
729 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100730 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100731
Chris Wilsonb3850852016-07-01 17:23:26 +0100732 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000733 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000734 request->signaling.wait.seqno = seqno;
Chris Wilsone8a261e2016-07-20 13:31:49 +0100735 i915_gem_request_get(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100736
Chris Wilson61d3dc72017-03-03 19:08:24 +0000737 spin_lock(&b->rb_lock);
Chris Wilson4a50d202016-07-26 12:01:50 +0100738
Chris Wilsonc81d4612016-07-01 17:23:25 +0100739 /* First add ourselves into the list of waiters, but register our
740 * bottom-half as the signaller thread. As per usual, only the oldest
741 * waiter (not just signaller) is tasked as the bottom-half waking
742 * up all completed waiters after the user interrupt.
743 *
744 * If we are the oldest waiter, enable the irq (after which we
745 * must double check that the seqno did not complete).
746 */
Chris Wilsonf7b02a52017-04-26 09:06:59 +0100747 wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100748
Chris Wilson735e0eb2017-06-08 12:14:04 +0100749 if (!__i915_gem_request_completed(request, seqno)) {
750 struct rb_node *parent, **p;
751 bool first;
752
753 /* Now insert ourselves into the retirement ordered list of
754 * signals on this engine. We track the oldest seqno as that
755 * will be the first signal to complete.
756 */
757 parent = NULL;
758 first = true;
759 p = &b->signals.rb_node;
760 while (*p) {
761 parent = *p;
762 if (i915_seqno_passed(seqno,
763 to_signaler(parent)->signaling.wait.seqno)) {
764 p = &parent->rb_right;
765 first = false;
766 } else {
767 p = &parent->rb_left;
768 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100769 }
Chris Wilson735e0eb2017-06-08 12:14:04 +0100770 rb_link_node(&request->signaling.node, parent, p);
771 rb_insert_color(&request->signaling.node, &b->signals);
772 if (first)
773 rcu_assign_pointer(b->first_signal, request);
774 } else {
775 __intel_engine_remove_wait(engine, &request->signaling.wait);
776 i915_gem_request_put(request);
777 wakeup = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100778 }
Chris Wilsonb3850852016-07-01 17:23:26 +0100779
Chris Wilson61d3dc72017-03-03 19:08:24 +0000780 spin_unlock(&b->rb_lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100781
782 if (wakeup)
783 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100784}
785
Chris Wilson9eb143b2017-02-23 07:44:16 +0000786void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
787{
788 struct intel_engine_cs *engine = request->engine;
789 struct intel_breadcrumbs *b = &engine->breadcrumbs;
790
Chris Wilsone60a8702017-03-02 11:51:30 +0000791 GEM_BUG_ON(!irqs_disabled());
Chris Wilson67520412017-03-02 13:28:01 +0000792 lockdep_assert_held(&request->lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000793 GEM_BUG_ON(!request->signaling.wait.seqno);
794
Chris Wilson61d3dc72017-03-03 19:08:24 +0000795 spin_lock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000796
797 if (!RB_EMPTY_NODE(&request->signaling.node)) {
798 if (request == rcu_access_pointer(b->first_signal)) {
799 struct rb_node *rb =
800 rb_next(&request->signaling.node);
801 rcu_assign_pointer(b->first_signal,
802 rb ? to_signaler(rb) : NULL);
803 }
804 rb_erase(&request->signaling.node, &b->signals);
805 RB_CLEAR_NODE(&request->signaling.node);
806 i915_gem_request_put(request);
807 }
808
809 __intel_engine_remove_wait(engine, &request->signaling.wait);
810
Chris Wilson61d3dc72017-03-03 19:08:24 +0000811 spin_unlock(&b->rb_lock);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000812
813 request->signaling.wait.seqno = 0;
814}
815
Chris Wilson688e6c72016-07-01 17:23:15 +0100816int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
817{
818 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100819 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100820
Chris Wilson61d3dc72017-03-03 19:08:24 +0000821 spin_lock_init(&b->rb_lock);
822 spin_lock_init(&b->irq_lock);
823
Kees Cook39cbf2a2017-10-17 09:53:04 +0300824 timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
825 timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
Chris Wilson688e6c72016-07-01 17:23:15 +0100826
Chris Wilsonc81d4612016-07-01 17:23:25 +0100827 /* Spawn a thread to provide a common bottom-half for all signals.
828 * As this is an asynchronous interface we cannot steal the current
829 * task for handling the bottom-half to the user interrupt, therefore
830 * we create a thread to do the coherent seqno dance after the
831 * interrupt and then signal the waitqueue (via the dma-buf/fence).
832 */
833 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
834 "i915/signal:%d", engine->id);
835 if (IS_ERR(tsk))
836 return PTR_ERR(tsk);
837
838 b->signaler = tsk;
839
Chris Wilson688e6c72016-07-01 17:23:15 +0100840 return 0;
841}
842
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100843static void cancel_fake_irq(struct intel_engine_cs *engine)
844{
845 struct intel_breadcrumbs *b = &engine->breadcrumbs;
846
847 del_timer_sync(&b->hangcheck);
848 del_timer_sync(&b->fake_irq);
849 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
850}
851
852void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
853{
854 struct intel_breadcrumbs *b = &engine->breadcrumbs;
855
856 cancel_fake_irq(engine);
Chris Wilson61d3dc72017-03-03 19:08:24 +0000857 spin_lock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100858
Chris Wilson67b807a82017-02-27 20:58:50 +0000859 if (b->irq_enabled)
860 irq_enable(engine);
861 else
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100862 irq_disable(engine);
Chris Wilson67b807a82017-02-27 20:58:50 +0000863
864 /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
865 * GPU is active and may have already executed the MI_USER_INTERRUPT
866 * before the CPU is ready to receive. However, the engine is currently
867 * idle (we haven't started it yet), there is no possibility for a
868 * missed interrupt as we enabled the irq and so we can clear the
869 * immediate wakeup (until a real interrupt arrives for the waiter).
870 */
871 clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
872
873 if (b->irq_armed)
874 enable_fake_irq(b);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100875
Chris Wilson61d3dc72017-03-03 19:08:24 +0000876 spin_unlock_irq(&b->irq_lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100877}
878
Chris Wilson688e6c72016-07-01 17:23:15 +0100879void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
880{
881 struct intel_breadcrumbs *b = &engine->breadcrumbs;
882
Chris Wilson381744f2016-11-21 11:07:59 +0000883 /* The engines should be idle and all requests accounted for! */
Chris Wilson61d3dc72017-03-03 19:08:24 +0000884 WARN_ON(READ_ONCE(b->irq_wait));
Chris Wilson381744f2016-11-21 11:07:59 +0000885 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncced5e22017-02-23 07:44:15 +0000886 WARN_ON(rcu_access_pointer(b->first_signal));
Chris Wilson381744f2016-11-21 11:07:59 +0000887 WARN_ON(!RB_EMPTY_ROOT(&b->signals));
888
Chris Wilsonc81d4612016-07-01 17:23:25 +0100889 if (!IS_ERR_OR_NULL(b->signaler))
890 kthread_stop(b->signaler);
891
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100892 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100893}
894
Chris Wilson9b6586a2017-02-23 07:44:08 +0000895bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100896{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000897 struct intel_breadcrumbs *b = &engine->breadcrumbs;
898 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100899
Chris Wilson61d3dc72017-03-03 19:08:24 +0000900 spin_lock_irq(&b->rb_lock);
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000901
Chris Wilson61d3dc72017-03-03 19:08:24 +0000902 if (b->irq_wait) {
903 wake_up_process(b->irq_wait->tsk);
Chris Wilson4bd66392017-03-15 21:07:22 +0000904 busy = true;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100905 }
906
Chris Wilsoncced5e22017-02-23 07:44:15 +0000907 if (rcu_access_pointer(b->first_signal)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000908 wake_up_process(b->signaler);
Chris Wilson4bd66392017-03-15 21:07:22 +0000909 busy = true;
Chris Wilson9b6586a2017-02-23 07:44:08 +0000910 }
911
Chris Wilson61d3dc72017-03-03 19:08:24 +0000912 spin_unlock_irq(&b->rb_lock);
Chris Wilson9b6586a2017-02-23 07:44:08 +0000913
914 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100915}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000916
917#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
918#include "selftests/intel_breadcrumbs.c"
919#endif