1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/kthread.h>
26
27#include "i915_drv.h"
28
29static void intel_breadcrumbs_hangcheck(unsigned long data)
30{
31 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
32 struct intel_breadcrumbs *b = &engine->breadcrumbs;
33
34 if (!b->irq_enabled)
35 return;
36
37 if (time_before(jiffies, b->timeout)) {
38 mod_timer(&b->hangcheck, b->timeout);
39 return;
40 }
41
42 DRM_DEBUG("Hangcheck timer elapsed... %s idle\n", engine->name);
43 set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
44 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
45
46
47
48
49
50
51
52
53
54
55 i915_queue_hangcheck(engine->i915);
56}
57
58static unsigned long wait_timeout(void)
59{
60 return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
61}
62
63static void intel_breadcrumbs_fake_irq(unsigned long data)
64{
65 struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
66
67
68
69
70
71
72
73
74 if (intel_engine_wakeup(engine))
75 mod_timer(&engine->breadcrumbs.fake_irq, jiffies + 1);
76}
77
78static void irq_enable(struct intel_engine_cs *engine)
79{
80
81
82
83
84 engine->breadcrumbs.irq_posted = true;
85
86 spin_lock_irq(&engine->i915->irq_lock);
87 engine->irq_enable(engine);
88 spin_unlock_irq(&engine->i915->irq_lock);
89}
90
91static void irq_disable(struct intel_engine_cs *engine)
92{
93 spin_lock_irq(&engine->i915->irq_lock);
94 engine->irq_disable(engine);
95 spin_unlock_irq(&engine->i915->irq_lock);
96
97 engine->breadcrumbs.irq_posted = false;
98}
99
100static void __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
101{
102 struct intel_engine_cs *engine =
103 container_of(b, struct intel_engine_cs, breadcrumbs);
104 struct drm_i915_private *i915 = engine->i915;
105
106 assert_spin_locked(&b->lock);
107 if (b->rpm_wakelock)
108 return;
109
110
111
112
113
114
115 intel_runtime_pm_get_noresume(i915);
116 b->rpm_wakelock = true;
117
118
119 if (intel_irqs_enabled(i915)) {
120 if (!test_bit(engine->id, &i915->gpu_error.test_irq_rings))
121 irq_enable(engine);
122 b->irq_enabled = true;
123 }
124
125 if (!b->irq_enabled ||
126 test_bit(engine->id, &i915->gpu_error.missed_irq_rings)) {
127 mod_timer(&b->fake_irq, jiffies + 1);
128 } else {
129
130 GEM_BUG_ON(!time_after(b->timeout, jiffies));
131 mod_timer(&b->hangcheck, b->timeout);
132 }
133}
134
135static void __intel_breadcrumbs_disable_irq(struct intel_breadcrumbs *b)
136{
137 struct intel_engine_cs *engine =
138 container_of(b, struct intel_engine_cs, breadcrumbs);
139
140 assert_spin_locked(&b->lock);
141 if (!b->rpm_wakelock)
142 return;
143
144 if (b->irq_enabled) {
145 irq_disable(engine);
146 b->irq_enabled = false;
147 }
148
149 intel_runtime_pm_put(engine->i915);
150 b->rpm_wakelock = false;
151}
152
153static inline struct intel_wait *to_wait(struct rb_node *node)
154{
155 return container_of(node, struct intel_wait, node);
156}
157
158static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
159 struct intel_wait *wait)
160{
161 assert_spin_locked(&b->lock);
162
163
164
165
166 rb_erase(&wait->node, &b->waiters);
167 RB_CLEAR_NODE(&wait->node);
168
169 wake_up_process(wait->tsk);
170}
171
172static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
173 struct intel_wait *wait)
174{
175 struct intel_breadcrumbs *b = &engine->breadcrumbs;
176 struct rb_node **p, *parent, *completed;
177 bool first;
178 u32 seqno;
179
180
181
182
183
184
185
186
187
188
189
190
191
192 first = true;
193 parent = NULL;
194 completed = NULL;
195 seqno = intel_engine_get_seqno(engine);
196
197
198
199
200
201
202 if (i915_seqno_passed(seqno, wait->seqno)) {
203 RB_CLEAR_NODE(&wait->node);
204 return first;
205 }
206
207 p = &b->waiters.rb_node;
208 while (*p) {
209 parent = *p;
210 if (wait->seqno == to_wait(parent)->seqno) {
211
212
213
214
215
216 if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
217 p = &parent->rb_right;
218 first = false;
219 } else {
220 p = &parent->rb_left;
221 }
222 } else if (i915_seqno_passed(wait->seqno,
223 to_wait(parent)->seqno)) {
224 p = &parent->rb_right;
225 if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
226 completed = parent;
227 else
228 first = false;
229 } else {
230 p = &parent->rb_left;
231 }
232 }
233 rb_link_node(&wait->node, parent, p);
234 rb_insert_color(&wait->node, &b->waiters);
235 GEM_BUG_ON(!first && !rcu_access_pointer(b->irq_seqno_bh));
236
237 if (completed) {
238 struct rb_node *next = rb_next(completed);
239
240 GEM_BUG_ON(!next && !first);
241 if (next && next != &wait->node) {
242 GEM_BUG_ON(first);
243 b->timeout = wait_timeout();
244 b->first_wait = to_wait(next);
245 rcu_assign_pointer(b->irq_seqno_bh, b->first_wait->tsk);
246
247
248
249
250
251
252
253
254
255
256 __intel_breadcrumbs_enable_irq(b);
257 if (READ_ONCE(b->irq_posted))
258 wake_up_process(to_wait(next)->tsk);
259 }
260
261 do {
262 struct intel_wait *crumb = to_wait(completed);
263 completed = rb_prev(completed);
264 __intel_breadcrumbs_finish(b, crumb);
265 } while (completed);
266 }
267
268 if (first) {
269 GEM_BUG_ON(rb_first(&b->waiters) != &wait->node);
270 b->timeout = wait_timeout();
271 b->first_wait = wait;
272 rcu_assign_pointer(b->irq_seqno_bh, wait->tsk);
273
274
275
276
277
278
279
280
281 __intel_breadcrumbs_enable_irq(b);
282 }
283 GEM_BUG_ON(!rcu_access_pointer(b->irq_seqno_bh));
284 GEM_BUG_ON(!b->first_wait);
285 GEM_BUG_ON(rb_first(&b->waiters) != &b->first_wait->node);
286
287 return first;
288}
289
290bool intel_engine_add_wait(struct intel_engine_cs *engine,
291 struct intel_wait *wait)
292{
293 struct intel_breadcrumbs *b = &engine->breadcrumbs;
294 bool first;
295
296 spin_lock(&b->lock);
297 first = __intel_engine_add_wait(engine, wait);
298 spin_unlock(&b->lock);
299
300 return first;
301}
302
303static inline bool chain_wakeup(struct rb_node *rb, int priority)
304{
305 return rb && to_wait(rb)->tsk->prio <= priority;
306}
307
308static inline int wakeup_priority(struct intel_breadcrumbs *b,
309 struct task_struct *tsk)
310{
311 if (tsk == b->signaler)
312 return INT_MIN;
313 else
314 return tsk->prio;
315}
316
317void intel_engine_remove_wait(struct intel_engine_cs *engine,
318 struct intel_wait *wait)
319{
320 struct intel_breadcrumbs *b = &engine->breadcrumbs;
321
322
323
324
325
326 if (RB_EMPTY_NODE(&wait->node))
327 return;
328
329 spin_lock(&b->lock);
330
331 if (RB_EMPTY_NODE(&wait->node))
332 goto out_unlock;
333
334 if (b->first_wait == wait) {
335 const int priority = wakeup_priority(b, wait->tsk);
336 struct rb_node *next;
337
338 GEM_BUG_ON(rcu_access_pointer(b->irq_seqno_bh) != wait->tsk);
339
340
341
342
343
344
345
346
347 next = rb_next(&wait->node);
348 if (chain_wakeup(next, priority)) {
349
350
351
352
353
354
355
356
357
358
359
360 u32 seqno = intel_engine_get_seqno(engine);
361
362 while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
363 struct rb_node *n = rb_next(next);
364
365 __intel_breadcrumbs_finish(b, to_wait(next));
366 next = n;
367 if (!chain_wakeup(next, priority))
368 break;
369 }
370 }
371
372 if (next) {
373
374
375
376
377
378
379
380 b->timeout = wait_timeout();
381 b->first_wait = to_wait(next);
382 rcu_assign_pointer(b->irq_seqno_bh, b->first_wait->tsk);
383 if (b->first_wait->seqno != wait->seqno)
384 __intel_breadcrumbs_enable_irq(b);
385 wake_up_process(b->first_wait->tsk);
386 } else {
387 b->first_wait = NULL;
388 rcu_assign_pointer(b->irq_seqno_bh, NULL);
389 __intel_breadcrumbs_disable_irq(b);
390 }
391 } else {
392 GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
393 }
394
395 GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
396 rb_erase(&wait->node, &b->waiters);
397
398out_unlock:
399 GEM_BUG_ON(b->first_wait == wait);
400 GEM_BUG_ON(rb_first(&b->waiters) !=
401 (b->first_wait ? &b->first_wait->node : NULL));
402 GEM_BUG_ON(!rcu_access_pointer(b->irq_seqno_bh) ^ RB_EMPTY_ROOT(&b->waiters));
403 spin_unlock(&b->lock);
404}
405
406static bool signal_complete(struct drm_i915_gem_request *request)
407{
408 if (!request)
409 return false;
410
411
412
413
414 if (intel_wait_complete(&request->signaling.wait))
415 return true;
416
417
418
419
420 if (__i915_request_irq_complete(request))
421 return true;
422
423 return false;
424}
425
426static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
427{
428 return container_of(rb, struct drm_i915_gem_request, signaling.node);
429}
430
431static void signaler_set_rtpriority(void)
432{
433 struct sched_param param = { .sched_priority = 1 };
434
435 sched_setscheduler_nocheck(current, SCHED_FIFO, ¶m);
436}
437
438static int intel_breadcrumbs_signaler(void *arg)
439{
440 struct intel_engine_cs *engine = arg;
441 struct intel_breadcrumbs *b = &engine->breadcrumbs;
442 struct drm_i915_gem_request *request;
443
444
445 signaler_set_rtpriority();
446
447 do {
448 set_current_state(TASK_INTERRUPTIBLE);
449
450
451
452
453
454
455
456
457
458 request = READ_ONCE(b->first_signal);
459 if (signal_complete(request)) {
460
461
462
463 intel_engine_remove_wait(engine,
464 &request->signaling.wait);
465
466 local_bh_disable();
467 fence_signal(&request->fence);
468 local_bh_enable();
469
470
471
472
473
474
475
476 spin_lock(&b->lock);
477 if (request == b->first_signal) {
478 struct rb_node *rb =
479 rb_next(&request->signaling.node);
480 b->first_signal = rb ? to_signaler(rb) : NULL;
481 }
482 rb_erase(&request->signaling.node, &b->signals);
483 spin_unlock(&b->lock);
484
485 i915_gem_request_put(request);
486 } else {
487 if (kthread_should_stop())
488 break;
489
490 schedule();
491 }
492 } while (1);
493 __set_current_state(TASK_RUNNING);
494
495 return 0;
496}
497
498void intel_engine_enable_signaling(struct drm_i915_gem_request *request)
499{
500 struct intel_engine_cs *engine = request->engine;
501 struct intel_breadcrumbs *b = &engine->breadcrumbs;
502 struct rb_node *parent, **p;
503 bool first, wakeup;
504
505
506 assert_spin_locked(&request->lock);
507
508 request->signaling.wait.tsk = b->signaler;
509 request->signaling.wait.seqno = request->fence.seqno;
510 i915_gem_request_get(request);
511
512 spin_lock(&b->lock);
513
514
515
516
517
518
519
520
521
522 wakeup = __intel_engine_add_wait(engine, &request->signaling.wait);
523
524
525
526
527
528 parent = NULL;
529 first = true;
530 p = &b->signals.rb_node;
531 while (*p) {
532 parent = *p;
533 if (i915_seqno_passed(request->fence.seqno,
534 to_signaler(parent)->fence.seqno)) {
535 p = &parent->rb_right;
536 first = false;
537 } else {
538 p = &parent->rb_left;
539 }
540 }
541 rb_link_node(&request->signaling.node, parent, p);
542 rb_insert_color(&request->signaling.node, &b->signals);
543 if (first)
544 smp_store_mb(b->first_signal, request);
545
546 spin_unlock(&b->lock);
547
548 if (wakeup)
549 wake_up_process(b->signaler);
550}
551
552int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
553{
554 struct intel_breadcrumbs *b = &engine->breadcrumbs;
555 struct task_struct *tsk;
556
557 spin_lock_init(&b->lock);
558 setup_timer(&b->fake_irq,
559 intel_breadcrumbs_fake_irq,
560 (unsigned long)engine);
561 setup_timer(&b->hangcheck,
562 intel_breadcrumbs_hangcheck,
563 (unsigned long)engine);
564
565
566
567
568
569
570
571 tsk = kthread_run(intel_breadcrumbs_signaler, engine,
572 "i915/signal:%d", engine->id);
573 if (IS_ERR(tsk))
574 return PTR_ERR(tsk);
575
576 b->signaler = tsk;
577
578 return 0;
579}
580
581static void cancel_fake_irq(struct intel_engine_cs *engine)
582{
583 struct intel_breadcrumbs *b = &engine->breadcrumbs;
584
585 del_timer_sync(&b->hangcheck);
586 del_timer_sync(&b->fake_irq);
587 clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
588}
589
590void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
591{
592 struct intel_breadcrumbs *b = &engine->breadcrumbs;
593
594 cancel_fake_irq(engine);
595 spin_lock(&b->lock);
596
597 __intel_breadcrumbs_disable_irq(b);
598 if (intel_engine_has_waiter(engine)) {
599 b->timeout = wait_timeout();
600 __intel_breadcrumbs_enable_irq(b);
601 if (READ_ONCE(b->irq_posted))
602 wake_up_process(b->first_wait->tsk);
603 } else {
604
605 irq_disable(engine);
606 }
607
608 spin_unlock(&b->lock);
609}
610
611void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
612{
613 struct intel_breadcrumbs *b = &engine->breadcrumbs;
614
615 if (!IS_ERR_OR_NULL(b->signaler))
616 kthread_stop(b->signaler);
617
618 cancel_fake_irq(engine);
619}
620
621unsigned int intel_kick_waiters(struct drm_i915_private *i915)
622{
623 struct intel_engine_cs *engine;
624 unsigned int mask = 0;
625
626
627
628
629
630
631 for_each_engine(engine, i915)
632 if (unlikely(intel_engine_wakeup(engine)))
633 mask |= intel_engine_flag(engine);
634
635 return mask;
636}
637
638unsigned int intel_kick_signalers(struct drm_i915_private *i915)
639{
640 struct intel_engine_cs *engine;
641 unsigned int mask = 0;
642
643 for_each_engine(engine, i915) {
644 if (unlikely(READ_ONCE(engine->breadcrumbs.first_signal))) {
645 wake_up_process(engine->breadcrumbs.signaler);
646 mask |= intel_engine_flag(engine);
647 }
648 }
649
650 return mask;
651}
652