intel_breadcrumbs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  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. #include <linux/kthread.h>
  25. #include <uapi/linux/sched/types.h>
  26. #include "i915_drv.h"
  27. #ifdef CONFIG_SMP
  28. #define task_asleep(tsk) ((tsk)->state & TASK_NORMAL && !(tsk)->on_cpu)
  29. #else
  30. #define task_asleep(tsk) ((tsk)->state & TASK_NORMAL)
  31. #endif
  32. static unsigned int __intel_breadcrumbs_wakeup(struct intel_breadcrumbs *b)
  33. {
  34. struct intel_wait *wait;
  35. unsigned int result = 0;
  36. lockdep_assert_held(&b->irq_lock);
  37. wait = b->irq_wait;
  38. if (wait) {
  39. /*
  40. * N.B. Since task_asleep() and ttwu are not atomic, the
  41. * waiter may actually go to sleep after the check, causing
  42. * us to suppress a valid wakeup. We prefer to reduce the
  43. * number of false positive missed_breadcrumb() warnings
  44. * at the expense of a few false negatives, as it it easy
  45. * to trigger a false positive under heavy load. Enough
  46. * signal should remain from genuine missed_breadcrumb()
  47. * for us to detect in CI.
  48. */
  49. bool was_asleep = task_asleep(wait->tsk);
  50. result = ENGINE_WAKEUP_WAITER;
  51. if (wake_up_process(wait->tsk) && was_asleep)
  52. result |= ENGINE_WAKEUP_ASLEEP;
  53. }
  54. return result;
  55. }
  56. unsigned int intel_engine_wakeup(struct intel_engine_cs *engine)
  57. {
  58. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  59. unsigned long flags;
  60. unsigned int result;
  61. spin_lock_irqsave(&b->irq_lock, flags);
  62. result = __intel_breadcrumbs_wakeup(b);
  63. spin_unlock_irqrestore(&b->irq_lock, flags);
  64. return result;
  65. }
  66. static unsigned long wait_timeout(void)
  67. {
  68. return round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES);
  69. }
  70. static noinline void missed_breadcrumb(struct intel_engine_cs *engine)
  71. {
  72. if (drm_debug & DRM_UT_DRIVER) {
  73. struct drm_printer p = drm_debug_printer(__func__);
  74. intel_engine_dump(engine, &p,
  75. "%s missed breadcrumb at %pS\n",
  76. engine->name, __builtin_return_address(0));
  77. }
  78. set_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
  79. }
  80. static void intel_breadcrumbs_hangcheck(struct timer_list *t)
  81. {
  82. struct intel_engine_cs *engine =
  83. from_timer(engine, t, breadcrumbs.hangcheck);
  84. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  85. if (!b->irq_armed)
  86. return;
  87. if (b->hangcheck_interrupts != atomic_read(&engine->irq_count)) {
  88. b->hangcheck_interrupts = atomic_read(&engine->irq_count);
  89. mod_timer(&b->hangcheck, wait_timeout());
  90. return;
  91. }
  92. /* We keep the hangcheck timer alive until we disarm the irq, even
  93. * if there are no waiters at present.
  94. *
  95. * If the waiter was currently running, assume it hasn't had a chance
  96. * 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.
  98. *
  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]!
  103. */
  104. if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) {
  105. missed_breadcrumb(engine);
  106. mod_timer(&b->fake_irq, jiffies + 1);
  107. } else {
  108. mod_timer(&b->hangcheck, wait_timeout());
  109. }
  110. }
  111. static void intel_breadcrumbs_fake_irq(struct timer_list *t)
  112. {
  113. struct intel_engine_cs *engine = from_timer(engine, t,
  114. breadcrumbs.fake_irq);
  115. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  116. /* The timer persists in case we cannot enable interrupts,
  117. * or if we have previously seen seqno/interrupt incoherency
  118. * ("missed interrupt" syndrome, better known as a "missed breadcrumb").
  119. * Here the worker will wake up every jiffie in order to kick the
  120. * oldest waiter to do the coherent seqno check.
  121. */
  122. spin_lock_irq(&b->irq_lock);
  123. if (b->irq_armed && !__intel_breadcrumbs_wakeup(b))
  124. __intel_engine_disarm_breadcrumbs(engine);
  125. spin_unlock_irq(&b->irq_lock);
  126. if (!b->irq_armed)
  127. return;
  128. mod_timer(&b->fake_irq, jiffies + 1);
  129. /* Ensure that even if the GPU hangs, we get woken up.
  130. *
  131. * However, note that if no one is waiting, we never notice
  132. * a gpu hang. Eventually, we will have to wait for a resource
  133. * held by the GPU and so trigger a hangcheck. In the most
  134. * pathological case, this will be upon memory starvation! To
  135. * prevent this, we also queue the hangcheck from the retire
  136. * worker.
  137. */
  138. i915_queue_hangcheck(engine->i915);
  139. }
  140. static void irq_enable(struct intel_engine_cs *engine)
  141. {
  142. /*
  143. * FIXME: Ideally we want this on the API boundary, but for the
  144. * sake of testing with mock breadcrumbs (no HW so unable to
  145. * enable irqs) we place it deep within the bowels, at the point
  146. * of no return.
  147. */
  148. GEM_BUG_ON(!intel_irqs_enabled(engine->i915));
  149. /* Enabling the IRQ may miss the generation of the interrupt, but
  150. * we still need to force the barrier before reading the seqno,
  151. * just in case.
  152. */
  153. set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
  154. /* Caller disables interrupts */
  155. spin_lock(&engine->i915->irq_lock);
  156. engine->irq_enable(engine);
  157. spin_unlock(&engine->i915->irq_lock);
  158. }
  159. static void irq_disable(struct intel_engine_cs *engine)
  160. {
  161. /* Caller disables interrupts */
  162. spin_lock(&engine->i915->irq_lock);
  163. engine->irq_disable(engine);
  164. spin_unlock(&engine->i915->irq_lock);
  165. }
  166. void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
  167. {
  168. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  169. lockdep_assert_held(&b->irq_lock);
  170. GEM_BUG_ON(b->irq_wait);
  171. GEM_BUG_ON(!b->irq_armed);
  172. GEM_BUG_ON(!b->irq_enabled);
  173. if (!--b->irq_enabled)
  174. irq_disable(engine);
  175. b->irq_armed = false;
  176. }
  177. void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
  178. {
  179. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  180. spin_lock_irq(&b->irq_lock);
  181. if (!b->irq_enabled++)
  182. irq_enable(engine);
  183. GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
  184. spin_unlock_irq(&b->irq_lock);
  185. }
  186. void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
  187. {
  188. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  189. spin_lock_irq(&b->irq_lock);
  190. GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
  191. if (!--b->irq_enabled)
  192. irq_disable(engine);
  193. spin_unlock_irq(&b->irq_lock);
  194. }
  195. void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
  196. {
  197. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  198. struct intel_wait *wait, *n;
  199. if (!b->irq_armed)
  200. goto wakeup_signaler;
  201. /*
  202. * We only disarm the irq when we are idle (all requests completed),
  203. * so if the bottom-half remains asleep, it missed the request
  204. * completion.
  205. */
  206. if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP)
  207. missed_breadcrumb(engine);
  208. spin_lock_irq(&b->rb_lock);
  209. spin_lock(&b->irq_lock);
  210. b->irq_wait = NULL;
  211. if (b->irq_armed)
  212. __intel_engine_disarm_breadcrumbs(engine);
  213. spin_unlock(&b->irq_lock);
  214. rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
  215. RB_CLEAR_NODE(&wait->node);
  216. wake_up_process(wait->tsk);
  217. }
  218. b->waiters = RB_ROOT;
  219. spin_unlock_irq(&b->rb_lock);
  220. /*
  221. * The signaling thread may be asleep holding a reference to a request,
  222. * that had its signaling cancelled prior to being preempted. We need
  223. * to kick the signaler, just in case, to release any such reference.
  224. */
  225. wakeup_signaler:
  226. wake_up_process(b->signaler);
  227. }
  228. static bool use_fake_irq(const struct intel_breadcrumbs *b)
  229. {
  230. const struct intel_engine_cs *engine =
  231. container_of(b, struct intel_engine_cs, breadcrumbs);
  232. if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
  233. return false;
  234. /* Only start with the heavy weight fake irq timer if we have not
  235. * seen any interrupts since enabling it the first time. If the
  236. * interrupts are still arriving, it means we made a mistake in our
  237. * engine->seqno_barrier(), a timing error that should be transient
  238. * and unlikely to reoccur.
  239. */
  240. return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
  241. }
  242. static void enable_fake_irq(struct intel_breadcrumbs *b)
  243. {
  244. /* Ensure we never sleep indefinitely */
  245. if (!b->irq_enabled || use_fake_irq(b))
  246. mod_timer(&b->fake_irq, jiffies + 1);
  247. else
  248. mod_timer(&b->hangcheck, wait_timeout());
  249. }
  250. static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
  251. {
  252. struct intel_engine_cs *engine =
  253. container_of(b, struct intel_engine_cs, breadcrumbs);
  254. struct drm_i915_private *i915 = engine->i915;
  255. bool enabled;
  256. lockdep_assert_held(&b->irq_lock);
  257. if (b->irq_armed)
  258. return false;
  259. /* The breadcrumb irq will be disarmed on the interrupt after the
  260. * waiters are signaled. This gives us a single interrupt window in
  261. * which we can add a new waiter and avoid the cost of re-enabling
  262. * the irq.
  263. */
  264. b->irq_armed = true;
  265. if (I915_SELFTEST_ONLY(b->mock)) {
  266. /* For our mock objects we want to avoid interaction
  267. * with the real hardware (which is not set up). So
  268. * we simply pretend we have enabled the powerwell
  269. * and the irq, and leave it up to the mock
  270. * implementation to call intel_engine_wakeup()
  271. * itself when it wants to simulate a user interrupt,
  272. */
  273. return true;
  274. }
  275. /* Since we are waiting on a request, the GPU should be busy
  276. * and should have its own rpm reference. This is tracked
  277. * by i915->gt.awake, we can forgo holding our own wakref
  278. * for the interrupt as before i915->gt.awake is released (when
  279. * the driver is idle) we disarm the breadcrumbs.
  280. */
  281. /* No interrupts? Kick the waiter every jiffie! */
  282. enabled = false;
  283. if (!b->irq_enabled++ &&
  284. !test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
  285. irq_enable(engine);
  286. enabled = true;
  287. }
  288. enable_fake_irq(b);
  289. return enabled;
  290. }
  291. static inline struct intel_wait *to_wait(struct rb_node *node)
  292. {
  293. return rb_entry(node, struct intel_wait, node);
  294. }
  295. static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
  296. struct intel_wait *wait)
  297. {
  298. lockdep_assert_held(&b->rb_lock);
  299. GEM_BUG_ON(b->irq_wait == wait);
  300. /* This request is completed, so remove it from the tree, mark it as
  301. * complete, and *then* wake up the associated task. N.B. when the
  302. * task wakes up, it will find the empty rb_node, discern that it
  303. * has already been removed from the tree and skip the serialisation
  304. * of the b->rb_lock and b->irq_lock. This means that the destruction
  305. * of the intel_wait is not serialised with the interrupt handler
  306. * by the waiter - it must instead be serialised by the caller.
  307. */
  308. rb_erase(&wait->node, &b->waiters);
  309. RB_CLEAR_NODE(&wait->node);
  310. wake_up_process(wait->tsk); /* implicit smp_wmb() */
  311. }
  312. static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
  313. struct rb_node *next)
  314. {
  315. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  316. spin_lock(&b->irq_lock);
  317. GEM_BUG_ON(!b->irq_armed);
  318. GEM_BUG_ON(!b->irq_wait);
  319. b->irq_wait = to_wait(next);
  320. spin_unlock(&b->irq_lock);
  321. /* We always wake up the next waiter that takes over as the bottom-half
  322. * as we may delegate not only the irq-seqno barrier to the next waiter
  323. * but also the task of waking up concurrent waiters.
  324. */
  325. if (next)
  326. wake_up_process(to_wait(next)->tsk);
  327. }
  328. static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
  329. struct intel_wait *wait)
  330. {
  331. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  332. struct rb_node **p, *parent, *completed;
  333. bool first, armed;
  334. u32 seqno;
  335. /* Insert the request into the retirement ordered list
  336. * of waiters by walking the rbtree. If we are the oldest
  337. * seqno in the tree (the first to be retired), then
  338. * set ourselves as the bottom-half.
  339. *
  340. * As we descend the tree, prune completed branches since we hold the
  341. * spinlock we know that the first_waiter must be delayed and can
  342. * reduce some of the sequential wake up latency if we take action
  343. * ourselves and wake up the completed tasks in parallel. Also, by
  344. * removing stale elements in the tree, we may be able to reduce the
  345. * ping-pong between the old bottom-half and ourselves as first-waiter.
  346. */
  347. armed = false;
  348. first = true;
  349. parent = NULL;
  350. completed = NULL;
  351. seqno = intel_engine_get_seqno(engine);
  352. /* If the request completed before we managed to grab the spinlock,
  353. * return now before adding ourselves to the rbtree. We let the
  354. * current bottom-half handle any pending wakeups and instead
  355. * try and get out of the way quickly.
  356. */
  357. if (i915_seqno_passed(seqno, wait->seqno)) {
  358. RB_CLEAR_NODE(&wait->node);
  359. return first;
  360. }
  361. p = &b->waiters.rb_node;
  362. while (*p) {
  363. parent = *p;
  364. if (wait->seqno == to_wait(parent)->seqno) {
  365. /* We have multiple waiters on the same seqno, select
  366. * the highest priority task (that with the smallest
  367. * task->prio) to serve as the bottom-half for this
  368. * group.
  369. */
  370. if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
  371. p = &parent->rb_right;
  372. first = false;
  373. } else {
  374. p = &parent->rb_left;
  375. }
  376. } else if (i915_seqno_passed(wait->seqno,
  377. to_wait(parent)->seqno)) {
  378. p = &parent->rb_right;
  379. if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
  380. completed = parent;
  381. else
  382. first = false;
  383. } else {
  384. p = &parent->rb_left;
  385. }
  386. }
  387. rb_link_node(&wait->node, parent, p);
  388. rb_insert_color(&wait->node, &b->waiters);
  389. if (first) {
  390. spin_lock(&b->irq_lock);
  391. b->irq_wait = wait;
  392. /* After assigning ourselves as the new bottom-half, we must
  393. * perform a cursory check to prevent a missed interrupt.
  394. * Either we miss the interrupt whilst programming the hardware,
  395. * or if there was a previous waiter (for a later seqno) they
  396. * may be woken instead of us (due to the inherent race
  397. * in the unlocked read of b->irq_seqno_bh in the irq handler)
  398. * and so we miss the wake up.
  399. */
  400. armed = __intel_breadcrumbs_enable_irq(b);
  401. spin_unlock(&b->irq_lock);
  402. }
  403. if (completed) {
  404. /* Advance the bottom-half (b->irq_wait) before we wake up
  405. * the waiters who may scribble over their intel_wait
  406. * just as the interrupt handler is dereferencing it via
  407. * b->irq_wait.
  408. */
  409. if (!first) {
  410. struct rb_node *next = rb_next(completed);
  411. GEM_BUG_ON(next == &wait->node);
  412. __intel_breadcrumbs_next(engine, next);
  413. }
  414. do {
  415. struct intel_wait *crumb = to_wait(completed);
  416. completed = rb_prev(completed);
  417. __intel_breadcrumbs_finish(b, crumb);
  418. } while (completed);
  419. }
  420. GEM_BUG_ON(!b->irq_wait);
  421. GEM_BUG_ON(!b->irq_armed);
  422. GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
  423. return armed;
  424. }
  425. bool intel_engine_add_wait(struct intel_engine_cs *engine,
  426. struct intel_wait *wait)
  427. {
  428. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  429. bool armed;
  430. spin_lock_irq(&b->rb_lock);
  431. armed = __intel_engine_add_wait(engine, wait);
  432. spin_unlock_irq(&b->rb_lock);
  433. if (armed)
  434. return armed;
  435. /* Make the caller recheck if its request has already started. */
  436. return i915_seqno_passed(intel_engine_get_seqno(engine),
  437. wait->seqno - 1);
  438. }
  439. static inline bool chain_wakeup(struct rb_node *rb, int priority)
  440. {
  441. return rb && to_wait(rb)->tsk->prio <= priority;
  442. }
  443. static inline int wakeup_priority(struct intel_breadcrumbs *b,
  444. struct task_struct *tsk)
  445. {
  446. if (tsk == b->signaler)
  447. return INT_MIN;
  448. else
  449. return tsk->prio;
  450. }
  451. static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
  452. struct intel_wait *wait)
  453. {
  454. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  455. lockdep_assert_held(&b->rb_lock);
  456. if (RB_EMPTY_NODE(&wait->node))
  457. goto out;
  458. if (b->irq_wait == wait) {
  459. const int priority = wakeup_priority(b, wait->tsk);
  460. struct rb_node *next;
  461. /* We are the current bottom-half. Find the next candidate,
  462. * the first waiter in the queue on the remaining oldest
  463. * request. As multiple seqnos may complete in the time it
  464. * takes us to wake up and find the next waiter, we have to
  465. * wake up that waiter for it to perform its own coherent
  466. * completion check.
  467. */
  468. next = rb_next(&wait->node);
  469. if (chain_wakeup(next, priority)) {
  470. /* If the next waiter is already complete,
  471. * wake it up and continue onto the next waiter. So
  472. * if have a small herd, they will wake up in parallel
  473. * rather than sequentially, which should reduce
  474. * the overall latency in waking all the completed
  475. * clients.
  476. *
  477. * However, waking up a chain adds extra latency to
  478. * the first_waiter. This is undesirable if that
  479. * waiter is a high priority task.
  480. */
  481. u32 seqno = intel_engine_get_seqno(engine);
  482. while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
  483. struct rb_node *n = rb_next(next);
  484. __intel_breadcrumbs_finish(b, to_wait(next));
  485. next = n;
  486. if (!chain_wakeup(next, priority))
  487. break;
  488. }
  489. }
  490. __intel_breadcrumbs_next(engine, next);
  491. } else {
  492. GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
  493. }
  494. GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
  495. rb_erase(&wait->node, &b->waiters);
  496. RB_CLEAR_NODE(&wait->node);
  497. out:
  498. GEM_BUG_ON(b->irq_wait == wait);
  499. GEM_BUG_ON(rb_first(&b->waiters) !=
  500. (b->irq_wait ? &b->irq_wait->node : NULL));
  501. }
  502. void intel_engine_remove_wait(struct intel_engine_cs *engine,
  503. struct intel_wait *wait)
  504. {
  505. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  506. /* Quick check to see if this waiter was already decoupled from
  507. * the tree by the bottom-half to avoid contention on the spinlock
  508. * by the herd.
  509. */
  510. if (RB_EMPTY_NODE(&wait->node)) {
  511. GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
  512. return;
  513. }
  514. spin_lock_irq(&b->rb_lock);
  515. __intel_engine_remove_wait(engine, wait);
  516. spin_unlock_irq(&b->rb_lock);
  517. }
  518. static bool signal_valid(const struct drm_i915_gem_request *request)
  519. {
  520. return intel_wait_check_request(&request->signaling.wait, request);
  521. }
  522. static bool signal_complete(const struct drm_i915_gem_request *request)
  523. {
  524. if (!request)
  525. return false;
  526. /* If another process served as the bottom-half it may have already
  527. * signalled that this wait is already completed.
  528. */
  529. if (intel_wait_complete(&request->signaling.wait))
  530. return signal_valid(request);
  531. /* Carefully check if the request is complete, giving time for the
  532. * seqno to be visible or if the GPU hung.
  533. */
  534. if (__i915_request_irq_complete(request))
  535. return true;
  536. return false;
  537. }
  538. static struct drm_i915_gem_request *to_signaler(struct rb_node *rb)
  539. {
  540. return rb_entry(rb, struct drm_i915_gem_request, signaling.node);
  541. }
  542. static void signaler_set_rtpriority(void)
  543. {
  544. struct sched_param param = { .sched_priority = 1 };
  545. sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
  546. }
  547. static int intel_breadcrumbs_signaler(void *arg)
  548. {
  549. struct intel_engine_cs *engine = arg;
  550. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  551. struct drm_i915_gem_request *request;
  552. /* Install ourselves with high priority to reduce signalling latency */
  553. signaler_set_rtpriority();
  554. do {
  555. bool do_schedule = true;
  556. set_current_state(TASK_INTERRUPTIBLE);
  557. /* We are either woken up by the interrupt bottom-half,
  558. * or by a client adding a new signaller. In both cases,
  559. * the GPU seqno may have advanced beyond our oldest signal.
  560. * If it has, propagate the signal, remove the waiter and
  561. * check again with the next oldest signal. Otherwise we
  562. * need to wait for a new interrupt from the GPU or for
  563. * a new client.
  564. */
  565. rcu_read_lock();
  566. request = rcu_dereference(b->first_signal);
  567. if (request)
  568. request = i915_gem_request_get_rcu(request);
  569. rcu_read_unlock();
  570. if (signal_complete(request)) {
  571. local_bh_disable();
  572. dma_fence_signal(&request->fence);
  573. local_bh_enable(); /* kick start the tasklets */
  574. spin_lock_irq(&b->rb_lock);
  575. /* Wake up all other completed waiters and select the
  576. * next bottom-half for the next user interrupt.
  577. */
  578. __intel_engine_remove_wait(engine,
  579. &request->signaling.wait);
  580. /* Find the next oldest signal. Note that as we have
  581. * not been holding the lock, another client may
  582. * have installed an even older signal than the one
  583. * we just completed - so double check we are still
  584. * the oldest before picking the next one.
  585. */
  586. if (request == rcu_access_pointer(b->first_signal)) {
  587. struct rb_node *rb =
  588. rb_next(&request->signaling.node);
  589. rcu_assign_pointer(b->first_signal,
  590. rb ? to_signaler(rb) : NULL);
  591. }
  592. rb_erase(&request->signaling.node, &b->signals);
  593. RB_CLEAR_NODE(&request->signaling.node);
  594. spin_unlock_irq(&b->rb_lock);
  595. i915_gem_request_put(request);
  596. /* If the engine is saturated we may be continually
  597. * processing completed requests. This angers the
  598. * NMI watchdog if we never let anything else
  599. * have access to the CPU. Let's pretend to be nice
  600. * and relinquish the CPU if we burn through the
  601. * entire RT timeslice!
  602. */
  603. do_schedule = need_resched();
  604. }
  605. if (unlikely(do_schedule)) {
  606. if (kthread_should_park())
  607. kthread_parkme();
  608. if (unlikely(kthread_should_stop())) {
  609. i915_gem_request_put(request);
  610. break;
  611. }
  612. schedule();
  613. }
  614. i915_gem_request_put(request);
  615. } while (1);
  616. __set_current_state(TASK_RUNNING);
  617. return 0;
  618. }
  619. void intel_engine_enable_signaling(struct drm_i915_gem_request *request,
  620. bool wakeup)
  621. {
  622. struct intel_engine_cs *engine = request->engine;
  623. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  624. u32 seqno;
  625. /* Note that we may be called from an interrupt handler on another
  626. * device (e.g. nouveau signaling a fence completion causing us
  627. * to submit a request, and so enable signaling). As such,
  628. * we need to make sure that all other users of b->rb_lock protect
  629. * against interrupts, i.e. use spin_lock_irqsave.
  630. */
  631. /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
  632. GEM_BUG_ON(!irqs_disabled());
  633. lockdep_assert_held(&request->lock);
  634. seqno = i915_gem_request_global_seqno(request);
  635. if (!seqno)
  636. return;
  637. request->signaling.wait.tsk = b->signaler;
  638. request->signaling.wait.request = request;
  639. request->signaling.wait.seqno = seqno;
  640. i915_gem_request_get(request);
  641. spin_lock(&b->rb_lock);
  642. /* First add ourselves into the list of waiters, but register our
  643. * bottom-half as the signaller thread. As per usual, only the oldest
  644. * waiter (not just signaller) is tasked as the bottom-half waking
  645. * up all completed waiters after the user interrupt.
  646. *
  647. * If we are the oldest waiter, enable the irq (after which we
  648. * must double check that the seqno did not complete).
  649. */
  650. wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
  651. if (!__i915_gem_request_completed(request, seqno)) {
  652. struct rb_node *parent, **p;
  653. bool first;
  654. /* Now insert ourselves into the retirement ordered list of
  655. * signals on this engine. We track the oldest seqno as that
  656. * will be the first signal to complete.
  657. */
  658. parent = NULL;
  659. first = true;
  660. p = &b->signals.rb_node;
  661. while (*p) {
  662. parent = *p;
  663. if (i915_seqno_passed(seqno,
  664. to_signaler(parent)->signaling.wait.seqno)) {
  665. p = &parent->rb_right;
  666. first = false;
  667. } else {
  668. p = &parent->rb_left;
  669. }
  670. }
  671. rb_link_node(&request->signaling.node, parent, p);
  672. rb_insert_color(&request->signaling.node, &b->signals);
  673. if (first)
  674. rcu_assign_pointer(b->first_signal, request);
  675. } else {
  676. __intel_engine_remove_wait(engine, &request->signaling.wait);
  677. i915_gem_request_put(request);
  678. wakeup = false;
  679. }
  680. spin_unlock(&b->rb_lock);
  681. if (wakeup)
  682. wake_up_process(b->signaler);
  683. }
  684. void intel_engine_cancel_signaling(struct drm_i915_gem_request *request)
  685. {
  686. struct intel_engine_cs *engine = request->engine;
  687. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  688. GEM_BUG_ON(!irqs_disabled());
  689. lockdep_assert_held(&request->lock);
  690. GEM_BUG_ON(!request->signaling.wait.seqno);
  691. spin_lock(&b->rb_lock);
  692. if (!RB_EMPTY_NODE(&request->signaling.node)) {
  693. if (request == rcu_access_pointer(b->first_signal)) {
  694. struct rb_node *rb =
  695. rb_next(&request->signaling.node);
  696. rcu_assign_pointer(b->first_signal,
  697. rb ? to_signaler(rb) : NULL);
  698. }
  699. rb_erase(&request->signaling.node, &b->signals);
  700. RB_CLEAR_NODE(&request->signaling.node);
  701. i915_gem_request_put(request);
  702. }
  703. __intel_engine_remove_wait(engine, &request->signaling.wait);
  704. spin_unlock(&b->rb_lock);
  705. request->signaling.wait.seqno = 0;
  706. }
  707. int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
  708. {
  709. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  710. struct task_struct *tsk;
  711. spin_lock_init(&b->rb_lock);
  712. spin_lock_init(&b->irq_lock);
  713. timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
  714. timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
  715. /* Spawn a thread to provide a common bottom-half for all signals.
  716. * As this is an asynchronous interface we cannot steal the current
  717. * task for handling the bottom-half to the user interrupt, therefore
  718. * we create a thread to do the coherent seqno dance after the
  719. * interrupt and then signal the waitqueue (via the dma-buf/fence).
  720. */
  721. tsk = kthread_run(intel_breadcrumbs_signaler, engine,
  722. "i915/signal:%d", engine->id);
  723. if (IS_ERR(tsk))
  724. return PTR_ERR(tsk);
  725. b->signaler = tsk;
  726. return 0;
  727. }
  728. static void cancel_fake_irq(struct intel_engine_cs *engine)
  729. {
  730. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  731. del_timer_sync(&b->hangcheck);
  732. del_timer_sync(&b->fake_irq);
  733. clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
  734. }
  735. void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
  736. {
  737. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  738. cancel_fake_irq(engine);
  739. spin_lock_irq(&b->irq_lock);
  740. if (b->irq_enabled)
  741. irq_enable(engine);
  742. else
  743. irq_disable(engine);
  744. /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
  745. * GPU is active and may have already executed the MI_USER_INTERRUPT
  746. * before the CPU is ready to receive. However, the engine is currently
  747. * idle (we haven't started it yet), there is no possibility for a
  748. * missed interrupt as we enabled the irq and so we can clear the
  749. * immediate wakeup (until a real interrupt arrives for the waiter).
  750. */
  751. clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
  752. if (b->irq_armed)
  753. enable_fake_irq(b);
  754. spin_unlock_irq(&b->irq_lock);
  755. }
  756. void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
  757. {
  758. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  759. /* The engines should be idle and all requests accounted for! */
  760. WARN_ON(READ_ONCE(b->irq_wait));
  761. WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
  762. WARN_ON(rcu_access_pointer(b->first_signal));
  763. WARN_ON(!RB_EMPTY_ROOT(&b->signals));
  764. if (!IS_ERR_OR_NULL(b->signaler))
  765. kthread_stop(b->signaler);
  766. cancel_fake_irq(engine);
  767. }
  768. bool intel_breadcrumbs_busy(struct intel_engine_cs *engine)
  769. {
  770. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  771. bool busy = false;
  772. spin_lock_irq(&b->rb_lock);
  773. if (b->irq_wait) {
  774. wake_up_process(b->irq_wait->tsk);
  775. busy = true;
  776. }
  777. if (rcu_access_pointer(b->first_signal)) {
  778. wake_up_process(b->signaler);
  779. busy = true;
  780. }
  781. spin_unlock_irq(&b->rb_lock);
  782. return busy;
  783. }
  784. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  785. #include "selftests/intel_breadcrumbs.c"
  786. #endif