blob: e7f4a4c923a2608308f55bc3e9d97d5627f1c820 [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 Wilson8d769ea2017-02-27 20:58:47 +000029unsigned int intel_engine_wakeup(struct intel_engine_cs *engine)
30{
Chris Wilson56299fb2017-02-27 20:58:48 +000031 struct intel_wait *wait;
32 unsigned long flags;
Chris Wilson8d769ea2017-02-27 20:58:47 +000033 unsigned int result = 0;
34
Chris Wilson56299fb2017-02-27 20:58:48 +000035 spin_lock_irqsave(&engine->breadcrumbs.lock, flags);
36 wait = engine->breadcrumbs.first_wait;
37 if (wait) {
Chris Wilson8d769ea2017-02-27 20:58:47 +000038 result = ENGINE_WAKEUP_WAITER;
Chris Wilson56299fb2017-02-27 20:58:48 +000039 if (!wake_up_process(wait->tsk))
Chris Wilson8d769ea2017-02-27 20:58:47 +000040 result |= ENGINE_WAKEUP_ACTIVE;
Chris Wilson8d769ea2017-02-27 20:58:47 +000041 }
Chris Wilson56299fb2017-02-27 20:58:48 +000042 spin_unlock_irqrestore(&engine->breadcrumbs.lock, flags);
Chris Wilson8d769ea2017-02-27 20:58:47 +000043
44 return result;
45}
46
Chris Wilson2246bea2017-02-17 15:13:00 +000047static unsigned long wait_timeout(void)
48{
49 return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
50}
51
Chris Wilson83348ba2016-08-09 17:47:51 +010052static void intel_breadcrumbs_hangcheck(unsigned long data)
53{
54 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
55 struct intel_breadcrumbs *b = &engine->breadcrumbs;
56
57 if (!b->irq_enabled)
58 return;
59
Chris Wilson2246bea2017-02-17 15:13:00 +000060 if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) {
61 b->hangcheck_interrupts = atomic_read(&engine->irq_count);
62 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson83348ba2016-08-09 17:47:51 +010063 return;
64 }
65
Chris Wilson89985672017-02-17 15:13:02 +000066 /* If the waiter was currently running, assume it hasn't had a chance
67 * to process the pending interrupt (e.g, low priority task on a loaded
68 * system) and wait until it sleeps before declaring a missed interrupt.
69 */
Chris Wilson8d769ea2017-02-27 20:58:47 +000070 if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ACTIVE) {
Chris Wilson89985672017-02-17 15:13:02 +000071 mod_timer(&b->hangcheck, wait_timeout());
72 return;
73 }
74
Chris Wilson83348ba2016-08-09 17:47:51 +010075 DRM_DEBUG("Hangcheck timer elapsed... %s idle\n", engine->name);
76 set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
77 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
78
79 /* Ensure that even if the GPU hangs, we get woken up.
80 *
81 * However, note that if no one is waiting, we never notice
82 * a gpu hang. Eventually, we will have to wait for a resource
83 * held by the GPU and so trigger a hangcheck. In the most
84 * pathological case, this will be upon memory starvation! To
85 * prevent this, we also queue the hangcheck from the retire
86 * worker.
87 */
88 i915_queue_hangcheck(engine->i915);
89}
90
Chris Wilson688e6c72016-07-01 17:23:15 +010091static void intel_breadcrumbs_fake_irq(unsigned long data)
92{
93 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
94
95 /*
96 * The timer persists in case we cannot enable interrupts,
97 * or if we have previously seen seqno/interrupt incoherency
98 * ("missed interrupt" syndrome). Here the worker will wake up
99 * every jiffie in order to kick the oldest waiter to do the
100 * coherent seqno check.
101 */
Chris Wilson688e6c72016-07-01 17:23:15 +0100102 if (intel_engine_wakeup(engine))
103 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
Chris Wilson688e6c72016-07-01 17:23:15 +0100104}
105
106static void irq_enable(struct intel_engine_cs *engine)
107{
Chris Wilson3d5564e2016-07-01 17:23:23 +0100108 /* Enabling the IRQ may miss the generation of the interrupt, but
109 * we still need to force the barrier before reading the seqno,
110 * just in case.
111 */
Chris Wilson538b2572017-01-24 15:18:05 +0000112 set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100113
Chris Wilsonf6168e32016-10-28 13:58:55 +0100114 /* Caller disables interrupts */
115 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100116 engine->irq_enable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100117 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100118}
119
120static void irq_disable(struct intel_engine_cs *engine)
121{
Chris Wilsonf6168e32016-10-28 13:58:55 +0100122 /* Caller disables interrupts */
123 spin_lock(&engine->i915->irq_lock);
Chris Wilson31bb59c2016-07-01 17:23:27 +0100124 engine->irq_disable(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100125 spin_unlock(&engine->i915->irq_lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100126}
127
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000128static bool use_fake_irq(const struct intel_breadcrumbs *b)
129{
130 const struct intel_engine_cs *engine =
131 container_of(b, struct intel_engine_cs, breadcrumbs);
132
133 if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
134 return false;
135
136 /* Only start with the heavy weight fake irq timer if we have not
137 * seen any interrupts since enabling it the first time. If the
138 * interrupts are still arriving, it means we made a mistake in our
139 * engine->seqno_barrier(), a timing error that should be transient
140 * and unlikely to reoccur.
141 */
142 return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
143}
144
Chris Wilson04171312016-07-06 12:39:00 +0100145static void __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
Chris Wilson688e6c72016-07-01 17:23:15 +0100146{
147 struct intel_engine_cs *engine =
148 container_of(b, struct intel_engine_cs, breadcrumbs);
149 struct drm_i915_private *i915 = engine->i915;
Chris Wilson688e6c72016-07-01 17:23:15 +0100150
151 assert_spin_locked(&b->lock);
152 if (b->rpm_wakelock)
Chris Wilson04171312016-07-06 12:39:00 +0100153 return;
Chris Wilson688e6c72016-07-01 17:23:15 +0100154
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000155 if (I915_SELFTEST_ONLY(b->mock)) {
156 /* For our mock objects we want to avoid interaction
157 * with the real hardware (which is not set up). So
158 * we simply pretend we have enabled the powerwell
159 * and the irq, and leave it up to the mock
160 * implementation to call intel_engine_wakeup()
161 * itself when it wants to simulate a user interrupt,
162 */
163 b->rpm_wakelock = true;
164 return;
165 }
166
Chris Wilson688e6c72016-07-01 17:23:15 +0100167 /* Since we are waiting on a request, the GPU should be busy
168 * and should have its own rpm reference. For completeness,
169 * record an rpm reference for ourselves to cover the
170 * interrupt we unmask.
171 */
172 intel_runtime_pm_get_noresume(i915);
173 b->rpm_wakelock = true;
174
175 /* No interrupts? Kick the waiter every jiffie! */
176 if (intel_irqs_enabled(i915)) {
Chris Wilson3d5564e2016-07-01 17:23:23 +0100177 if (!test_bit(engine->id, &i915->gpu_error.test_irq_rings))
Chris Wilson688e6c72016-07-01 17:23:15 +0100178 irq_enable(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100179 b->irq_enabled = true;
180 }
181
Chris Wilson6ef98ea2017-02-17 15:13:03 +0000182 if (!b->irq_enabled || use_fake_irq(b)) {
Chris Wilson688e6c72016-07-01 17:23:15 +0100183 mod_timer(&b->fake_irq, jiffies + 1);
Chris Wilson2f1ac9c2017-01-23 09:37:24 +0000184 i915_queue_hangcheck(i915);
Chris Wilson83348ba2016-08-09 17:47:51 +0100185 } else {
186 /* Ensure we never sleep indefinitely */
Chris Wilson2246bea2017-02-17 15:13:00 +0000187 mod_timer(&b->hangcheck, wait_timeout());
Chris Wilson83348ba2016-08-09 17:47:51 +0100188 }
Chris Wilson688e6c72016-07-01 17:23:15 +0100189}
190
191static void __intel_breadcrumbs_disable_irq(struct intel_breadcrumbs *b)
192{
193 struct intel_engine_cs *engine =
194 container_of(b, struct intel_engine_cs, breadcrumbs);
195
196 assert_spin_locked(&b->lock);
197 if (!b->rpm_wakelock)
198 return;
199
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000200 if (I915_SELFTEST_ONLY(b->mock)) {
201 b->rpm_wakelock = false;
202 return;
203 }
204
Chris Wilson688e6c72016-07-01 17:23:15 +0100205 if (b->irq_enabled) {
206 irq_disable(engine);
207 b->irq_enabled = false;
208 }
209
210 intel_runtime_pm_put(engine->i915);
211 b->rpm_wakelock = false;
212}
213
214static inline struct intel_wait *to_wait(struct rb_node *node)
215{
Chris Wilsond8567862016-12-20 10:40:03 +0000216 return rb_entry(node, struct intel_wait, node);
Chris Wilson688e6c72016-07-01 17:23:15 +0100217}
218
219static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
220 struct intel_wait *wait)
221{
222 assert_spin_locked(&b->lock);
223
224 /* This request is completed, so remove it from the tree, mark it as
225 * complete, and *then* wake up the associated task.
226 */
227 rb_erase(&wait->node, &b->waiters);
228 RB_CLEAR_NODE(&wait->node);
229
230 wake_up_process(wait->tsk); /* implicit smp_wmb() */
231}
232
233static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
234 struct intel_wait *wait)
235{
236 struct intel_breadcrumbs *b = &engine->breadcrumbs;
237 struct rb_node **p, *parent, *completed;
238 bool first;
239 u32 seqno;
240
241 /* Insert the request into the retirement ordered list
242 * of waiters by walking the rbtree. If we are the oldest
243 * seqno in the tree (the first to be retired), then
244 * set ourselves as the bottom-half.
245 *
246 * As we descend the tree, prune completed branches since we hold the
247 * spinlock we know that the first_waiter must be delayed and can
248 * reduce some of the sequential wake up latency if we take action
249 * ourselves and wake up the completed tasks in parallel. Also, by
250 * removing stale elements in the tree, we may be able to reduce the
251 * ping-pong between the old bottom-half and ourselves as first-waiter.
252 */
253 first = true;
254 parent = NULL;
255 completed = NULL;
Chris Wilson1b7744e2016-07-01 17:23:17 +0100256 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100257
258 /* If the request completed before we managed to grab the spinlock,
259 * return now before adding ourselves to the rbtree. We let the
260 * current bottom-half handle any pending wakeups and instead
261 * try and get out of the way quickly.
262 */
263 if (i915_seqno_passed(seqno, wait->seqno)) {
264 RB_CLEAR_NODE(&wait->node);
265 return first;
266 }
267
268 p = &b->waiters.rb_node;
269 while (*p) {
270 parent = *p;
271 if (wait->seqno == to_wait(parent)->seqno) {
272 /* We have multiple waiters on the same seqno, select
273 * the highest priority task (that with the smallest
274 * task->prio) to serve as the bottom-half for this
275 * group.
276 */
277 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
278 p = &parent->rb_right;
279 first = false;
280 } else {
281 p = &parent->rb_left;
282 }
283 } else if (i915_seqno_passed(wait->seqno,
284 to_wait(parent)->seqno)) {
285 p = &parent->rb_right;
286 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
287 completed = parent;
288 else
289 first = false;
290 } else {
291 p = &parent->rb_left;
292 }
293 }
294 rb_link_node(&wait->node, parent, p);
295 rb_insert_color(&wait->node, &b->waiters);
Chris Wilson688e6c72016-07-01 17:23:15 +0100296
297 if (completed) {
298 struct rb_node *next = rb_next(completed);
299
300 GEM_BUG_ON(!next && !first);
301 if (next && next != &wait->node) {
302 GEM_BUG_ON(first);
303 b->first_wait = to_wait(next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100304 /* As there is a delay between reading the current
305 * seqno, processing the completed tasks and selecting
306 * the next waiter, we may have missed the interrupt
307 * and so need for the next bottom-half to wakeup.
308 *
309 * Also as we enable the IRQ, we may miss the
310 * interrupt for that seqno, so we have to wake up
311 * the next bottom-half in order to do a coherent check
312 * in case the seqno passed.
313 */
314 __intel_breadcrumbs_enable_irq(b);
Chris Wilson538b2572017-01-24 15:18:05 +0000315 if (test_bit(ENGINE_IRQ_BREADCRUMB,
316 &engine->irq_posted))
Chris Wilson3d5564e2016-07-01 17:23:23 +0100317 wake_up_process(to_wait(next)->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100318 }
319
320 do {
321 struct intel_wait *crumb = to_wait(completed);
322 completed = rb_prev(completed);
323 __intel_breadcrumbs_finish(b, crumb);
324 } while (completed);
325 }
326
327 if (first) {
328 GEM_BUG_ON(rb_first(&b->waiters) != &wait->node);
329 b->first_wait = wait;
Chris Wilson04171312016-07-06 12:39:00 +0100330 /* After assigning ourselves as the new bottom-half, we must
331 * perform a cursory check to prevent a missed interrupt.
332 * Either we miss the interrupt whilst programming the hardware,
333 * or if there was a previous waiter (for a later seqno) they
334 * may be woken instead of us (due to the inherent race
Chris Wilsonaca34b62016-07-06 12:39:02 +0100335 * in the unlocked read of b->irq_seqno_bh in the irq handler)
336 * and so we miss the wake up.
Chris Wilson04171312016-07-06 12:39:00 +0100337 */
338 __intel_breadcrumbs_enable_irq(b);
Chris Wilson688e6c72016-07-01 17:23:15 +0100339 }
Chris Wilson688e6c72016-07-01 17:23:15 +0100340 GEM_BUG_ON(!b->first_wait);
341 GEM_BUG_ON(rb_first(&b->waiters) != &b->first_wait->node);
342
343 return first;
344}
345
346bool intel_engine_add_wait(struct intel_engine_cs *engine,
347 struct intel_wait *wait)
348{
349 struct intel_breadcrumbs *b = &engine->breadcrumbs;
350 bool first;
351
Chris Wilsonf6168e32016-10-28 13:58:55 +0100352 spin_lock_irq(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100353 first = __intel_engine_add_wait(engine, wait);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100354 spin_unlock_irq(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100355
356 return first;
357}
358
Chris Wilson688e6c72016-07-01 17:23:15 +0100359static inline bool chain_wakeup(struct rb_node *rb, int priority)
360{
361 return rb && to_wait(rb)->tsk->prio <= priority;
362}
363
Chris Wilsonc81d4612016-07-01 17:23:25 +0100364static inline int wakeup_priority(struct intel_breadcrumbs *b,
365 struct task_struct *tsk)
366{
367 if (tsk == b->signaler)
368 return INT_MIN;
369 else
370 return tsk->prio;
371}
372
Chris Wilson9eb143b2017-02-23 07:44:16 +0000373static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
374 struct intel_wait *wait)
Chris Wilson688e6c72016-07-01 17:23:15 +0100375{
376 struct intel_breadcrumbs *b = &engine->breadcrumbs;
377
Chris Wilson9eb143b2017-02-23 07:44:16 +0000378 assert_spin_locked(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100379
380 if (RB_EMPTY_NODE(&wait->node))
Chris Wilson9eb143b2017-02-23 07:44:16 +0000381 goto out;
Chris Wilson688e6c72016-07-01 17:23:15 +0100382
383 if (b->first_wait == wait) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100384 const int priority = wakeup_priority(b, wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100385 struct rb_node *next;
Chris Wilson688e6c72016-07-01 17:23:15 +0100386
Chris Wilson688e6c72016-07-01 17:23:15 +0100387 /* We are the current bottom-half. Find the next candidate,
388 * the first waiter in the queue on the remaining oldest
389 * request. As multiple seqnos may complete in the time it
390 * takes us to wake up and find the next waiter, we have to
391 * wake up that waiter for it to perform its own coherent
392 * completion check.
393 */
394 next = rb_next(&wait->node);
395 if (chain_wakeup(next, priority)) {
396 /* If the next waiter is already complete,
397 * wake it up and continue onto the next waiter. So
398 * if have a small herd, they will wake up in parallel
399 * rather than sequentially, which should reduce
400 * the overall latency in waking all the completed
401 * clients.
402 *
403 * However, waking up a chain adds extra latency to
404 * the first_waiter. This is undesirable if that
405 * waiter is a high priority task.
406 */
Chris Wilson1b7744e2016-07-01 17:23:17 +0100407 u32 seqno = intel_engine_get_seqno(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100408
409 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
410 struct rb_node *n = rb_next(next);
411
412 __intel_breadcrumbs_finish(b, to_wait(next));
413 next = n;
414 if (!chain_wakeup(next, priority))
415 break;
416 }
417 }
418
419 if (next) {
420 /* In our haste, we may have completed the first waiter
421 * before we enabled the interrupt. Do so now as we
422 * have a second waiter for a future seqno. Afterwards,
423 * we have to wake up that waiter in case we missed
424 * the interrupt, or if we have to handle an
425 * exception rather than a seqno completion.
426 */
427 b->first_wait = to_wait(next);
Chris Wilson688e6c72016-07-01 17:23:15 +0100428 if (b->first_wait->seqno != wait->seqno)
429 __intel_breadcrumbs_enable_irq(b);
Chris Wilsondbd6ef22016-08-09 17:47:52 +0100430 wake_up_process(b->first_wait->tsk);
Chris Wilson688e6c72016-07-01 17:23:15 +0100431 } else {
432 b->first_wait = NULL;
Chris Wilson688e6c72016-07-01 17:23:15 +0100433 __intel_breadcrumbs_disable_irq(b);
434 }
435 } else {
436 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
437 }
438
439 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
440 rb_erase(&wait->node, &b->waiters);
441
Chris Wilson9eb143b2017-02-23 07:44:16 +0000442out:
Chris Wilson688e6c72016-07-01 17:23:15 +0100443 GEM_BUG_ON(b->first_wait == wait);
444 GEM_BUG_ON(rb_first(&b->waiters) !=
445 (b->first_wait ? &b->first_wait->node : NULL));
Chris Wilson9eb143b2017-02-23 07:44:16 +0000446}
447
448void intel_engine_remove_wait(struct intel_engine_cs *engine,
449 struct intel_wait *wait)
450{
451 struct intel_breadcrumbs *b = &engine->breadcrumbs;
452
453 /* Quick check to see if this waiter was already decoupled from
454 * the tree by the bottom-half to avoid contention on the spinlock
455 * by the herd.
456 */
457 if (RB_EMPTY_NODE(&wait->node))
458 return;
459
460 spin_lock_irq(&b->lock);
461 __intel_engine_remove_wait(engine, wait);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100462 spin_unlock_irq(&b->lock);
Chris Wilson688e6c72016-07-01 17:23:15 +0100463}
464
Chris Wilsond6a22892017-02-23 07:44:17 +0000465static bool signal_valid(const struct drm_i915_gem_request *request)
466{
467 return intel_wait_check_request(&request->signaling.wait, request);
468}
469
470static bool signal_complete(const struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100471{
Chris Wilsonb3850852016-07-01 17:23:26 +0100472 if (!request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100473 return false;
474
475 /* If another process served as the bottom-half it may have already
476 * signalled that this wait is already completed.
477 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100478 if (intel_wait_complete(&request->signaling.wait))
Chris Wilsond6a22892017-02-23 07:44:17 +0000479 return signal_valid(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100480
481 /* Carefully check if the request is complete, giving time for the
482 * seqno to be visible or if the GPU hung.
483 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100484 if (__i915_request_irq_complete(request))
Chris Wilsonc81d4612016-07-01 17:23:25 +0100485 return true;
486
487 return false;
488}
489
Chris Wilsonb3850852016-07-01 17:23:26 +0100490static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100491{
Chris Wilsond8567862016-12-20 10:40:03 +0000492 return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100493}
494
495static void signaler_set_rtpriority(void)
496{
497 struct sched_param param = { .sched_priority = 1 };
498
499 sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
500}
501
502static int intel_breadcrumbs_signaler(void *arg)
503{
504 struct intel_engine_cs *engine = arg;
505 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonb3850852016-07-01 17:23:26 +0100506 struct drm_i915_gem_request *request;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100507
508 /* Install ourselves with high priority to reduce signalling latency */
509 signaler_set_rtpriority();
510
511 do {
512 set_current_state(TASK_INTERRUPTIBLE);
513
514 /* We are either woken up by the interrupt bottom-half,
515 * or by a client adding a new signaller. In both cases,
516 * the GPU seqno may have advanced beyond our oldest signal.
517 * If it has, propagate the signal, remove the waiter and
518 * check again with the next oldest signal. Otherwise we
519 * need to wait for a new interrupt from the GPU or for
520 * a new client.
521 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000522 rcu_read_lock();
523 request = rcu_dereference(b->first_signal);
524 if (request)
525 request = i915_gem_request_get_rcu(request);
526 rcu_read_unlock();
Chris Wilsonb3850852016-07-01 17:23:26 +0100527 if (signal_complete(request)) {
Chris Wilson7c9e9342017-01-24 11:00:09 +0000528 local_bh_disable();
529 dma_fence_signal(&request->fence);
530 local_bh_enable(); /* kick start the tasklets */
531
Chris Wilson9eb143b2017-02-23 07:44:16 +0000532 spin_lock_irq(&b->lock);
533
Chris Wilsonc81d4612016-07-01 17:23:25 +0100534 /* Wake up all other completed waiters and select the
535 * next bottom-half for the next user interrupt.
536 */
Chris Wilson9eb143b2017-02-23 07:44:16 +0000537 __intel_engine_remove_wait(engine,
538 &request->signaling.wait);
Chris Wilson5590af32016-09-09 14:11:54 +0100539
Chris Wilsonc81d4612016-07-01 17:23:25 +0100540 /* Find the next oldest signal. Note that as we have
541 * not been holding the lock, another client may
542 * have installed an even older signal than the one
543 * we just completed - so double check we are still
544 * the oldest before picking the next one.
545 */
Chris Wilsoncced5e22017-02-23 07:44:15 +0000546 if (request == rcu_access_pointer(b->first_signal)) {
Chris Wilsonb3850852016-07-01 17:23:26 +0100547 struct rb_node *rb =
548 rb_next(&request->signaling.node);
Chris Wilsoncced5e22017-02-23 07:44:15 +0000549 rcu_assign_pointer(b->first_signal,
550 rb ? to_signaler(rb) : NULL);
Chris Wilsonb3850852016-07-01 17:23:26 +0100551 }
552 rb_erase(&request->signaling.node, &b->signals);
Chris Wilson9eb143b2017-02-23 07:44:16 +0000553 RB_CLEAR_NODE(&request->signaling.node);
554
Chris Wilsonf6168e32016-10-28 13:58:55 +0100555 spin_unlock_irq(&b->lock);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100556
Chris Wilsone8a261e2016-07-20 13:31:49 +0100557 i915_gem_request_put(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100558 } else {
Chris Wilsond6a22892017-02-23 07:44:17 +0000559 DEFINE_WAIT(exec);
560
Chris Wilsoncced5e22017-02-23 07:44:15 +0000561 if (kthread_should_stop()) {
562 GEM_BUG_ON(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100563 break;
Chris Wilsoncced5e22017-02-23 07:44:15 +0000564 }
Chris Wilsonc81d4612016-07-01 17:23:25 +0100565
Chris Wilsond6a22892017-02-23 07:44:17 +0000566 if (request)
567 add_wait_queue(&request->execute, &exec);
568
Chris Wilsonc81d4612016-07-01 17:23:25 +0100569 schedule();
Chris Wilsonfe3288b2017-02-12 17:20:01 +0000570
Chris Wilsond6a22892017-02-23 07:44:17 +0000571 if (request)
572 remove_wait_queue(&request->execute, &exec);
573
Chris Wilsonfe3288b2017-02-12 17:20:01 +0000574 if (kthread_should_park())
575 kthread_parkme();
Chris Wilsonc81d4612016-07-01 17:23:25 +0100576 }
Chris Wilsoncced5e22017-02-23 07:44:15 +0000577 i915_gem_request_put(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100578 } while (1);
579 __set_current_state(TASK_RUNNING);
580
581 return 0;
582}
583
Chris Wilsonb3850852016-07-01 17:23:26 +0100584void intel_engine_enable_signaling(struct drm_i915_gem_request *request)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100585{
586 struct intel_engine_cs *engine = request->engine;
587 struct intel_breadcrumbs *b = &engine->breadcrumbs;
588 struct rb_node *parent, **p;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100589 bool first, wakeup;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000590 u32 seqno;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100591
Chris Wilsonf6168e32016-10-28 13:58:55 +0100592 /* Note that we may be called from an interrupt handler on another
593 * device (e.g. nouveau signaling a fence completion causing us
594 * to submit a request, and so enable signaling). As such,
595 * we need to make sure that all other users of b->lock protect
596 * against interrupts, i.e. use spin_lock_irqsave.
597 */
598
599 /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
Chris Wilson4a50d202016-07-26 12:01:50 +0100600 assert_spin_locked(&request->lock);
Chris Wilson754c9fd2017-02-23 07:44:14 +0000601
602 seqno = i915_gem_request_global_seqno(request);
603 if (!seqno)
Chris Wilson65e47602016-10-28 13:58:49 +0100604 return;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100605
Chris Wilsonb3850852016-07-01 17:23:26 +0100606 request->signaling.wait.tsk = b->signaler;
Chris Wilson56299fb2017-02-27 20:58:48 +0000607 request->signaling.wait.request = request;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000608 request->signaling.wait.seqno = seqno;
Chris Wilsone8a261e2016-07-20 13:31:49 +0100609 i915_gem_request_get(request);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100610
Chris Wilson4a50d202016-07-26 12:01:50 +0100611 spin_lock(&b->lock);
612
Chris Wilsonc81d4612016-07-01 17:23:25 +0100613 /* First add ourselves into the list of waiters, but register our
614 * bottom-half as the signaller thread. As per usual, only the oldest
615 * waiter (not just signaller) is tasked as the bottom-half waking
616 * up all completed waiters after the user interrupt.
617 *
618 * If we are the oldest waiter, enable the irq (after which we
619 * must double check that the seqno did not complete).
620 */
Chris Wilsonb3850852016-07-01 17:23:26 +0100621 wakeup = __intel_engine_add_wait(engine, &request->signaling.wait);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100622
623 /* Now insert ourselves into the retirement ordered list of signals
624 * on this engine. We track the oldest seqno as that will be the
625 * first signal to complete.
626 */
Chris Wilsonc81d4612016-07-01 17:23:25 +0100627 parent = NULL;
628 first = true;
629 p = &b->signals.rb_node;
630 while (*p) {
631 parent = *p;
Chris Wilson754c9fd2017-02-23 07:44:14 +0000632 if (i915_seqno_passed(seqno,
633 to_signaler(parent)->signaling.wait.seqno)) {
Chris Wilsonc81d4612016-07-01 17:23:25 +0100634 p = &parent->rb_right;
635 first = false;
636 } else {
637 p = &parent->rb_left;
638 }
639 }
Chris Wilsonb3850852016-07-01 17:23:26 +0100640 rb_link_node(&request->signaling.node, parent, p);
641 rb_insert_color(&request->signaling.node, &b->signals);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100642 if (first)
Chris Wilsoncced5e22017-02-23 07:44:15 +0000643 rcu_assign_pointer(b->first_signal, request);
Chris Wilsonb3850852016-07-01 17:23:26 +0100644
Chris Wilsonc81d4612016-07-01 17:23:25 +0100645 spin_unlock(&b->lock);
646
647 if (wakeup)
648 wake_up_process(b->signaler);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100649}
650
Chris Wilson9eb143b2017-02-23 07:44:16 +0000651void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
652{
653 struct intel_engine_cs *engine = request->engine;
654 struct intel_breadcrumbs *b = &engine->breadcrumbs;
655
656 assert_spin_locked(&request->lock);
657 GEM_BUG_ON(!request->signaling.wait.seqno);
658
659 spin_lock(&b->lock);
660
661 if (!RB_EMPTY_NODE(&request->signaling.node)) {
662 if (request == rcu_access_pointer(b->first_signal)) {
663 struct rb_node *rb =
664 rb_next(&request->signaling.node);
665 rcu_assign_pointer(b->first_signal,
666 rb ? to_signaler(rb) : NULL);
667 }
668 rb_erase(&request->signaling.node, &b->signals);
669 RB_CLEAR_NODE(&request->signaling.node);
670 i915_gem_request_put(request);
671 }
672
673 __intel_engine_remove_wait(engine, &request->signaling.wait);
674
675 spin_unlock(&b->lock);
676
677 request->signaling.wait.seqno = 0;
678}
679
Chris Wilson688e6c72016-07-01 17:23:15 +0100680int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
681{
682 struct intel_breadcrumbs *b = &engine->breadcrumbs;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100683 struct task_struct *tsk;
Chris Wilson688e6c72016-07-01 17:23:15 +0100684
685 spin_lock_init(&b->lock);
686 setup_timer(&b->fake_irq,
687 intel_breadcrumbs_fake_irq,
688 (unsigned long)engine);
Chris Wilson83348ba2016-08-09 17:47:51 +0100689 setup_timer(&b->hangcheck,
690 intel_breadcrumbs_hangcheck,
691 (unsigned long)engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100692
Chris Wilsonc81d4612016-07-01 17:23:25 +0100693 /* Spawn a thread to provide a common bottom-half for all signals.
694 * As this is an asynchronous interface we cannot steal the current
695 * task for handling the bottom-half to the user interrupt, therefore
696 * we create a thread to do the coherent seqno dance after the
697 * interrupt and then signal the waitqueue (via the dma-buf/fence).
698 */
699 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
700 "i915/signal:%d", engine->id);
701 if (IS_ERR(tsk))
702 return PTR_ERR(tsk);
703
704 b->signaler = tsk;
705
Chris Wilson688e6c72016-07-01 17:23:15 +0100706 return 0;
707}
708
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100709static void cancel_fake_irq(struct intel_engine_cs *engine)
710{
711 struct intel_breadcrumbs *b = &engine->breadcrumbs;
712
713 del_timer_sync(&b->hangcheck);
714 del_timer_sync(&b->fake_irq);
715 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
716}
717
718void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
719{
720 struct intel_breadcrumbs *b = &engine->breadcrumbs;
721
722 cancel_fake_irq(engine);
Chris Wilsonf6168e32016-10-28 13:58:55 +0100723 spin_lock_irq(&b->lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100724
725 __intel_breadcrumbs_disable_irq(b);
726 if (intel_engine_has_waiter(engine)) {
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100727 __intel_breadcrumbs_enable_irq(b);
Chris Wilson538b2572017-01-24 15:18:05 +0000728 if (test_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted))
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100729 wake_up_process(b->first_wait->tsk);
730 } else {
731 /* sanitize the IMR and unmask any auxiliary interrupts */
732 irq_disable(engine);
733 }
734
Chris Wilsonf6168e32016-10-28 13:58:55 +0100735 spin_unlock_irq(&b->lock);
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100736}
737
Chris Wilson688e6c72016-07-01 17:23:15 +0100738void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
739{
740 struct intel_breadcrumbs *b = &engine->breadcrumbs;
741
Chris Wilson381744f2016-11-21 11:07:59 +0000742 /* The engines should be idle and all requests accounted for! */
743 WARN_ON(READ_ONCE(b->first_wait));
744 WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
Chris Wilsoncced5e22017-02-23 07:44:15 +0000745 WARN_ON(rcu_access_pointer(b->first_signal));
Chris Wilson381744f2016-11-21 11:07:59 +0000746 WARN_ON(!RB_EMPTY_ROOT(&b->signals));
747
Chris Wilsonc81d4612016-07-01 17:23:25 +0100748 if (!IS_ERR_OR_NULL(b->signaler))
749 kthread_stop(b->signaler);
750
Chris Wilsonad07dfc2016-10-07 07:53:26 +0100751 cancel_fake_irq(engine);
Chris Wilson688e6c72016-07-01 17:23:15 +0100752}
753
Chris Wilson9b6586a2017-02-23 07:44:08 +0000754bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
Chris Wilsonc81d4612016-07-01 17:23:25 +0100755{
Chris Wilson9b6586a2017-02-23 07:44:08 +0000756 struct intel_breadcrumbs *b = &engine->breadcrumbs;
757 bool busy = false;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100758
Chris Wilson9b6586a2017-02-23 07:44:08 +0000759 spin_lock_irq(&b->lock);
Chris Wilson6a5d1db2016-11-08 14:37:19 +0000760
Chris Wilson9b6586a2017-02-23 07:44:08 +0000761 if (b->first_wait) {
762 wake_up_process(b->first_wait->tsk);
763 busy |= intel_engine_flag(engine);
Chris Wilsonc81d4612016-07-01 17:23:25 +0100764 }
765
Chris Wilsoncced5e22017-02-23 07:44:15 +0000766 if (rcu_access_pointer(b->first_signal)) {
Chris Wilson9b6586a2017-02-23 07:44:08 +0000767 wake_up_process(b->signaler);
768 busy |= intel_engine_flag(engine);
769 }
770
771 spin_unlock_irq(&b->lock);
772
773 return busy;
Chris Wilsonc81d4612016-07-01 17:23:25 +0100774}
Chris Wilsonf97fbf92017-02-13 17:15:14 +0000775
776#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
777#include "selftests/intel_breadcrumbs.c"
778#endif