intel_breadcrumbs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 (GEM_SHOW_DEBUG()) {
  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. unsigned int irq_count;
  86. if (!b->irq_armed)
  87. return;
  88. irq_count = READ_ONCE(b->irq_count);
  89. if (b->hangcheck_interrupts != irq_count) {
  90. b->hangcheck_interrupts = irq_count;
  91. mod_timer(&b->hangcheck, wait_timeout());
  92. return;
  93. }
  94. /* We keep the hangcheck timer alive until we disarm the irq, even
  95. * if there are no waiters at present.
  96. *
  97. * If the waiter was currently running, assume it hasn't had a chance
  98. * to process the pending interrupt (e.g, low priority task on a loaded
  99. * system) and wait until it sleeps before declaring a missed interrupt.
  100. *
  101. * If the waiter was asleep (and not even pending a wakeup), then we
  102. * must have missed an interrupt as the GPU has stopped advancing
  103. * but we still have a waiter. Assuming all batches complete within
  104. * DRM_I915_HANGCHECK_JIFFIES [1.5s]!
  105. */
  106. if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP) {
  107. missed_breadcrumb(engine);
  108. mod_timer(&b->fake_irq, jiffies + 1);
  109. } else {
  110. mod_timer(&b->hangcheck, wait_timeout());
  111. }
  112. }
  113. static void intel_breadcrumbs_fake_irq(struct timer_list *t)
  114. {
  115. struct intel_engine_cs *engine =
  116. from_timer(engine, t, breadcrumbs.fake_irq);
  117. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  118. /*
  119. * The timer persists in case we cannot enable interrupts,
  120. * or if we have previously seen seqno/interrupt incoherency
  121. * ("missed interrupt" syndrome, better known as a "missed breadcrumb").
  122. * Here the worker will wake up every jiffie in order to kick the
  123. * oldest waiter to do the coherent seqno check.
  124. */
  125. spin_lock_irq(&b->irq_lock);
  126. if (b->irq_armed && !__intel_breadcrumbs_wakeup(b))
  127. __intel_engine_disarm_breadcrumbs(engine);
  128. spin_unlock_irq(&b->irq_lock);
  129. if (!b->irq_armed)
  130. return;
  131. /* If the user has disabled the fake-irq, restore the hangchecking */
  132. if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings)) {
  133. mod_timer(&b->hangcheck, wait_timeout());
  134. return;
  135. }
  136. mod_timer(&b->fake_irq, jiffies + 1);
  137. }
  138. static void irq_enable(struct intel_engine_cs *engine)
  139. {
  140. /*
  141. * FIXME: Ideally we want this on the API boundary, but for the
  142. * sake of testing with mock breadcrumbs (no HW so unable to
  143. * enable irqs) we place it deep within the bowels, at the point
  144. * of no return.
  145. */
  146. GEM_BUG_ON(!intel_irqs_enabled(engine->i915));
  147. /* Enabling the IRQ may miss the generation of the interrupt, but
  148. * we still need to force the barrier before reading the seqno,
  149. * just in case.
  150. */
  151. set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
  152. /* Caller disables interrupts */
  153. if (engine->irq_enable) {
  154. spin_lock(&engine->i915->irq_lock);
  155. engine->irq_enable(engine);
  156. spin_unlock(&engine->i915->irq_lock);
  157. }
  158. }
  159. static void irq_disable(struct intel_engine_cs *engine)
  160. {
  161. /* Caller disables interrupts */
  162. if (engine->irq_disable) {
  163. spin_lock(&engine->i915->irq_lock);
  164. engine->irq_disable(engine);
  165. spin_unlock(&engine->i915->irq_lock);
  166. }
  167. }
  168. void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
  169. {
  170. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  171. lockdep_assert_held(&b->irq_lock);
  172. GEM_BUG_ON(b->irq_wait);
  173. GEM_BUG_ON(!b->irq_armed);
  174. GEM_BUG_ON(!b->irq_enabled);
  175. if (!--b->irq_enabled)
  176. irq_disable(engine);
  177. b->irq_armed = false;
  178. }
  179. void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
  180. {
  181. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  182. spin_lock_irq(&b->irq_lock);
  183. if (!b->irq_enabled++)
  184. irq_enable(engine);
  185. GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
  186. spin_unlock_irq(&b->irq_lock);
  187. }
  188. void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
  189. {
  190. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  191. spin_lock_irq(&b->irq_lock);
  192. GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
  193. if (!--b->irq_enabled)
  194. irq_disable(engine);
  195. spin_unlock_irq(&b->irq_lock);
  196. }
  197. void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
  198. {
  199. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  200. struct intel_wait *wait, *n;
  201. if (!b->irq_armed)
  202. return;
  203. /*
  204. * We only disarm the irq when we are idle (all requests completed),
  205. * so if the bottom-half remains asleep, it missed the request
  206. * completion.
  207. */
  208. if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP)
  209. missed_breadcrumb(engine);
  210. spin_lock_irq(&b->rb_lock);
  211. spin_lock(&b->irq_lock);
  212. b->irq_wait = NULL;
  213. if (b->irq_armed)
  214. __intel_engine_disarm_breadcrumbs(engine);
  215. spin_unlock(&b->irq_lock);
  216. rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
  217. GEM_BUG_ON(!intel_engine_signaled(engine, wait->seqno));
  218. RB_CLEAR_NODE(&wait->node);
  219. wake_up_process(wait->tsk);
  220. }
  221. b->waiters = RB_ROOT;
  222. spin_unlock_irq(&b->rb_lock);
  223. }
  224. static bool use_fake_irq(const struct intel_breadcrumbs *b)
  225. {
  226. const struct intel_engine_cs *engine =
  227. container_of(b, struct intel_engine_cs, breadcrumbs);
  228. if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
  229. return false;
  230. /*
  231. * Only start with the heavy weight fake irq timer if we have not
  232. * seen any interrupts since enabling it the first time. If the
  233. * interrupts are still arriving, it means we made a mistake in our
  234. * engine->seqno_barrier(), a timing error that should be transient
  235. * and unlikely to reoccur.
  236. */
  237. return READ_ONCE(b->irq_count) == b->hangcheck_interrupts;
  238. }
  239. static void enable_fake_irq(struct intel_breadcrumbs *b)
  240. {
  241. /* Ensure we never sleep indefinitely */
  242. if (!b->irq_enabled || use_fake_irq(b))
  243. mod_timer(&b->fake_irq, jiffies + 1);
  244. else
  245. mod_timer(&b->hangcheck, wait_timeout());
  246. }
  247. static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
  248. {
  249. struct intel_engine_cs *engine =
  250. container_of(b, struct intel_engine_cs, breadcrumbs);
  251. struct drm_i915_private *i915 = engine->i915;
  252. bool enabled;
  253. lockdep_assert_held(&b->irq_lock);
  254. if (b->irq_armed)
  255. return false;
  256. /* The breadcrumb irq will be disarmed on the interrupt after the
  257. * waiters are signaled. This gives us a single interrupt window in
  258. * which we can add a new waiter and avoid the cost of re-enabling
  259. * the irq.
  260. */
  261. b->irq_armed = true;
  262. if (I915_SELFTEST_ONLY(b->mock)) {
  263. /* For our mock objects we want to avoid interaction
  264. * with the real hardware (which is not set up). So
  265. * we simply pretend we have enabled the powerwell
  266. * and the irq, and leave it up to the mock
  267. * implementation to call intel_engine_wakeup()
  268. * itself when it wants to simulate a user interrupt,
  269. */
  270. return true;
  271. }
  272. /* Since we are waiting on a request, the GPU should be busy
  273. * and should have its own rpm reference. This is tracked
  274. * by i915->gt.awake, we can forgo holding our own wakref
  275. * for the interrupt as before i915->gt.awake is released (when
  276. * the driver is idle) we disarm the breadcrumbs.
  277. */
  278. /* No interrupts? Kick the waiter every jiffie! */
  279. enabled = false;
  280. if (!b->irq_enabled++ &&
  281. !test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
  282. irq_enable(engine);
  283. enabled = true;
  284. }
  285. enable_fake_irq(b);
  286. return enabled;
  287. }
  288. static inline struct intel_wait *to_wait(struct rb_node *node)
  289. {
  290. return rb_entry(node, struct intel_wait, node);
  291. }
  292. static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
  293. struct intel_wait *wait)
  294. {
  295. lockdep_assert_held(&b->rb_lock);
  296. GEM_BUG_ON(b->irq_wait == wait);
  297. /*
  298. * This request is completed, so remove it from the tree, mark it as
  299. * complete, and *then* wake up the associated task. N.B. when the
  300. * task wakes up, it will find the empty rb_node, discern that it
  301. * has already been removed from the tree and skip the serialisation
  302. * of the b->rb_lock and b->irq_lock. This means that the destruction
  303. * of the intel_wait is not serialised with the interrupt handler
  304. * by the waiter - it must instead be serialised by the caller.
  305. */
  306. rb_erase(&wait->node, &b->waiters);
  307. RB_CLEAR_NODE(&wait->node);
  308. if (wait->tsk->state != TASK_RUNNING)
  309. wake_up_process(wait->tsk); /* implicit smp_wmb() */
  310. }
  311. static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
  312. struct rb_node *next)
  313. {
  314. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  315. spin_lock(&b->irq_lock);
  316. GEM_BUG_ON(!b->irq_armed);
  317. GEM_BUG_ON(!b->irq_wait);
  318. b->irq_wait = to_wait(next);
  319. spin_unlock(&b->irq_lock);
  320. /* We always wake up the next waiter that takes over as the bottom-half
  321. * as we may delegate not only the irq-seqno barrier to the next waiter
  322. * but also the task of waking up concurrent waiters.
  323. */
  324. if (next)
  325. wake_up_process(to_wait(next)->tsk);
  326. }
  327. static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
  328. struct intel_wait *wait)
  329. {
  330. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  331. struct rb_node **p, *parent, *completed;
  332. bool first, armed;
  333. u32 seqno;
  334. GEM_BUG_ON(!wait->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 intel_engine_has_started(engine, wait->seqno);
  437. }
  438. static inline bool chain_wakeup(struct rb_node *rb, int priority)
  439. {
  440. return rb && to_wait(rb)->tsk->prio <= priority;
  441. }
  442. static inline int wakeup_priority(struct intel_breadcrumbs *b,
  443. struct task_struct *tsk)
  444. {
  445. if (tsk == b->signaler)
  446. return INT_MIN;
  447. else
  448. return tsk->prio;
  449. }
  450. static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
  451. struct intel_wait *wait)
  452. {
  453. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  454. lockdep_assert_held(&b->rb_lock);
  455. if (RB_EMPTY_NODE(&wait->node))
  456. goto out;
  457. if (b->irq_wait == wait) {
  458. const int priority = wakeup_priority(b, wait->tsk);
  459. struct rb_node *next;
  460. /* We are the current bottom-half. Find the next candidate,
  461. * the first waiter in the queue on the remaining oldest
  462. * request. As multiple seqnos may complete in the time it
  463. * takes us to wake up and find the next waiter, we have to
  464. * wake up that waiter for it to perform its own coherent
  465. * completion check.
  466. */
  467. next = rb_next(&wait->node);
  468. if (chain_wakeup(next, priority)) {
  469. /* If the next waiter is already complete,
  470. * wake it up and continue onto the next waiter. So
  471. * if have a small herd, they will wake up in parallel
  472. * rather than sequentially, which should reduce
  473. * the overall latency in waking all the completed
  474. * clients.
  475. *
  476. * However, waking up a chain adds extra latency to
  477. * the first_waiter. This is undesirable if that
  478. * waiter is a high priority task.
  479. */
  480. u32 seqno = intel_engine_get_seqno(engine);
  481. while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
  482. struct rb_node *n = rb_next(next);
  483. __intel_breadcrumbs_finish(b, to_wait(next));
  484. next = n;
  485. if (!chain_wakeup(next, priority))
  486. break;
  487. }
  488. }
  489. __intel_breadcrumbs_next(engine, next);
  490. } else {
  491. GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
  492. }
  493. GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
  494. rb_erase(&wait->node, &b->waiters);
  495. RB_CLEAR_NODE(&wait->node);
  496. out:
  497. GEM_BUG_ON(b->irq_wait == wait);
  498. GEM_BUG_ON(rb_first(&b->waiters) !=
  499. (b->irq_wait ? &b->irq_wait->node : NULL));
  500. }
  501. void intel_engine_remove_wait(struct intel_engine_cs *engine,
  502. struct intel_wait *wait)
  503. {
  504. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  505. /* Quick check to see if this waiter was already decoupled from
  506. * the tree by the bottom-half to avoid contention on the spinlock
  507. * by the herd.
  508. */
  509. if (RB_EMPTY_NODE(&wait->node)) {
  510. GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
  511. return;
  512. }
  513. spin_lock_irq(&b->rb_lock);
  514. __intel_engine_remove_wait(engine, wait);
  515. spin_unlock_irq(&b->rb_lock);
  516. }
  517. static void signaler_set_rtpriority(void)
  518. {
  519. struct sched_param param = { .sched_priority = 1 };
  520. sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
  521. }
  522. static int intel_breadcrumbs_signaler(void *arg)
  523. {
  524. struct intel_engine_cs *engine = arg;
  525. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  526. struct i915_request *rq, *n;
  527. /* Install ourselves with high priority to reduce signalling latency */
  528. signaler_set_rtpriority();
  529. do {
  530. bool do_schedule = true;
  531. LIST_HEAD(list);
  532. u32 seqno;
  533. set_current_state(TASK_INTERRUPTIBLE);
  534. if (list_empty(&b->signals))
  535. goto sleep;
  536. /*
  537. * We are either woken up by the interrupt bottom-half,
  538. * or by a client adding a new signaller. In both cases,
  539. * the GPU seqno may have advanced beyond our oldest signal.
  540. * If it has, propagate the signal, remove the waiter and
  541. * check again with the next oldest signal. Otherwise we
  542. * need to wait for a new interrupt from the GPU or for
  543. * a new client.
  544. */
  545. seqno = intel_engine_get_seqno(engine);
  546. spin_lock_irq(&b->rb_lock);
  547. list_for_each_entry_safe(rq, n, &b->signals, signaling.link) {
  548. u32 this = rq->signaling.wait.seqno;
  549. GEM_BUG_ON(!rq->signaling.wait.seqno);
  550. if (!i915_seqno_passed(seqno, this))
  551. break;
  552. if (likely(this == i915_request_global_seqno(rq))) {
  553. __intel_engine_remove_wait(engine,
  554. &rq->signaling.wait);
  555. rq->signaling.wait.seqno = 0;
  556. __list_del_entry(&rq->signaling.link);
  557. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  558. &rq->fence.flags)) {
  559. list_add_tail(&rq->signaling.link,
  560. &list);
  561. i915_request_get(rq);
  562. }
  563. }
  564. }
  565. spin_unlock_irq(&b->rb_lock);
  566. if (!list_empty(&list)) {
  567. local_bh_disable();
  568. list_for_each_entry_safe(rq, n, &list, signaling.link) {
  569. dma_fence_signal(&rq->fence);
  570. GEM_BUG_ON(!i915_request_completed(rq));
  571. i915_request_put(rq);
  572. }
  573. local_bh_enable(); /* kick start the tasklets */
  574. /*
  575. * If the engine is saturated we may be continually
  576. * processing completed requests. This angers the
  577. * NMI watchdog if we never let anything else
  578. * have access to the CPU. Let's pretend to be nice
  579. * and relinquish the CPU if we burn through the
  580. * entire RT timeslice!
  581. */
  582. do_schedule = need_resched();
  583. }
  584. if (unlikely(do_schedule)) {
  585. /* Before we sleep, check for a missed seqno */
  586. if (current->state & TASK_NORMAL &&
  587. !list_empty(&b->signals) &&
  588. engine->irq_seqno_barrier &&
  589. test_and_clear_bit(ENGINE_IRQ_BREADCRUMB,
  590. &engine->irq_posted)) {
  591. engine->irq_seqno_barrier(engine);
  592. intel_engine_wakeup(engine);
  593. }
  594. sleep:
  595. if (kthread_should_park())
  596. kthread_parkme();
  597. if (unlikely(kthread_should_stop()))
  598. break;
  599. schedule();
  600. }
  601. } while (1);
  602. __set_current_state(TASK_RUNNING);
  603. return 0;
  604. }
  605. static void insert_signal(struct intel_breadcrumbs *b,
  606. struct i915_request *request,
  607. const u32 seqno)
  608. {
  609. struct i915_request *iter;
  610. lockdep_assert_held(&b->rb_lock);
  611. /*
  612. * A reasonable assumption is that we are called to add signals
  613. * in sequence, as the requests are submitted for execution and
  614. * assigned a global_seqno. This will be the case for the majority
  615. * of internally generated signals (inter-engine signaling).
  616. *
  617. * Out of order waiters triggering random signaling enabling will
  618. * be more problematic, but hopefully rare enough and the list
  619. * small enough that the O(N) insertion sort is not an issue.
  620. */
  621. list_for_each_entry_reverse(iter, &b->signals, signaling.link)
  622. if (i915_seqno_passed(seqno, iter->signaling.wait.seqno))
  623. break;
  624. list_add(&request->signaling.link, &iter->signaling.link);
  625. }
  626. bool intel_engine_enable_signaling(struct i915_request *request, bool wakeup)
  627. {
  628. struct intel_engine_cs *engine = request->engine;
  629. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  630. struct intel_wait *wait = &request->signaling.wait;
  631. u32 seqno;
  632. /*
  633. * Note that we may be called from an interrupt handler on another
  634. * device (e.g. nouveau signaling a fence completion causing us
  635. * to submit a request, and so enable signaling). As such,
  636. * we need to make sure that all other users of b->rb_lock protect
  637. * against interrupts, i.e. use spin_lock_irqsave.
  638. */
  639. /* locked by dma_fence_enable_sw_signaling() (irqsafe fence->lock) */
  640. GEM_BUG_ON(!irqs_disabled());
  641. lockdep_assert_held(&request->lock);
  642. seqno = i915_request_global_seqno(request);
  643. if (!seqno) /* will be enabled later upon execution */
  644. return true;
  645. GEM_BUG_ON(wait->seqno);
  646. wait->tsk = b->signaler;
  647. wait->request = request;
  648. wait->seqno = seqno;
  649. /*
  650. * Add ourselves into the list of waiters, but registering our
  651. * bottom-half as the signaller thread. As per usual, only the oldest
  652. * waiter (not just signaller) is tasked as the bottom-half waking
  653. * up all completed waiters after the user interrupt.
  654. *
  655. * If we are the oldest waiter, enable the irq (after which we
  656. * must double check that the seqno did not complete).
  657. */
  658. spin_lock(&b->rb_lock);
  659. insert_signal(b, request, seqno);
  660. wakeup &= __intel_engine_add_wait(engine, wait);
  661. spin_unlock(&b->rb_lock);
  662. if (wakeup) {
  663. wake_up_process(b->signaler);
  664. return !intel_wait_complete(wait);
  665. }
  666. return true;
  667. }
  668. void intel_engine_cancel_signaling(struct i915_request *request)
  669. {
  670. struct intel_engine_cs *engine = request->engine;
  671. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  672. GEM_BUG_ON(!irqs_disabled());
  673. lockdep_assert_held(&request->lock);
  674. if (!READ_ONCE(request->signaling.wait.seqno))
  675. return;
  676. spin_lock(&b->rb_lock);
  677. __intel_engine_remove_wait(engine, &request->signaling.wait);
  678. if (fetch_and_zero(&request->signaling.wait.seqno))
  679. __list_del_entry(&request->signaling.link);
  680. spin_unlock(&b->rb_lock);
  681. }
  682. int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
  683. {
  684. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  685. struct task_struct *tsk;
  686. spin_lock_init(&b->rb_lock);
  687. spin_lock_init(&b->irq_lock);
  688. timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
  689. timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
  690. INIT_LIST_HEAD(&b->signals);
  691. /* Spawn a thread to provide a common bottom-half for all signals.
  692. * As this is an asynchronous interface we cannot steal the current
  693. * task for handling the bottom-half to the user interrupt, therefore
  694. * we create a thread to do the coherent seqno dance after the
  695. * interrupt and then signal the waitqueue (via the dma-buf/fence).
  696. */
  697. tsk = kthread_run(intel_breadcrumbs_signaler, engine,
  698. "i915/signal:%d", engine->id);
  699. if (IS_ERR(tsk))
  700. return PTR_ERR(tsk);
  701. b->signaler = tsk;
  702. return 0;
  703. }
  704. static void cancel_fake_irq(struct intel_engine_cs *engine)
  705. {
  706. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  707. del_timer_sync(&b->fake_irq); /* may queue b->hangcheck */
  708. del_timer_sync(&b->hangcheck);
  709. clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
  710. }
  711. void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
  712. {
  713. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  714. unsigned long flags;
  715. spin_lock_irqsave(&b->irq_lock, flags);
  716. /*
  717. * Leave the fake_irq timer enabled (if it is running), but clear the
  718. * bit so that it turns itself off on its next wake up and goes back
  719. * to the long hangcheck interval if still required.
  720. */
  721. clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
  722. if (b->irq_enabled)
  723. irq_enable(engine);
  724. else
  725. irq_disable(engine);
  726. /*
  727. * We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
  728. * GPU is active and may have already executed the MI_USER_INTERRUPT
  729. * before the CPU is ready to receive. However, the engine is currently
  730. * idle (we haven't started it yet), there is no possibility for a
  731. * missed interrupt as we enabled the irq and so we can clear the
  732. * immediate wakeup (until a real interrupt arrives for the waiter).
  733. */
  734. clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
  735. spin_unlock_irqrestore(&b->irq_lock, flags);
  736. }
  737. void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
  738. {
  739. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  740. /* The engines should be idle and all requests accounted for! */
  741. WARN_ON(READ_ONCE(b->irq_wait));
  742. WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
  743. WARN_ON(!list_empty(&b->signals));
  744. if (!IS_ERR_OR_NULL(b->signaler))
  745. kthread_stop(b->signaler);
  746. cancel_fake_irq(engine);
  747. }
  748. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  749. #include "selftests/intel_breadcrumbs.c"
  750. #endif