intel_breadcrumbs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. }
  130. static void irq_enable(struct intel_engine_cs *engine)
  131. {
  132. /*
  133. * FIXME: Ideally we want this on the API boundary, but for the
  134. * sake of testing with mock breadcrumbs (no HW so unable to
  135. * enable irqs) we place it deep within the bowels, at the point
  136. * of no return.
  137. */
  138. GEM_BUG_ON(!intel_irqs_enabled(engine->i915));
  139. /* Enabling the IRQ may miss the generation of the interrupt, but
  140. * we still need to force the barrier before reading the seqno,
  141. * just in case.
  142. */
  143. set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
  144. /* Caller disables interrupts */
  145. if (engine->irq_enable) {
  146. spin_lock(&engine->i915->irq_lock);
  147. engine->irq_enable(engine);
  148. spin_unlock(&engine->i915->irq_lock);
  149. }
  150. }
  151. static void irq_disable(struct intel_engine_cs *engine)
  152. {
  153. /* Caller disables interrupts */
  154. if (engine->irq_disable) {
  155. spin_lock(&engine->i915->irq_lock);
  156. engine->irq_disable(engine);
  157. spin_unlock(&engine->i915->irq_lock);
  158. }
  159. }
  160. void __intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
  161. {
  162. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  163. lockdep_assert_held(&b->irq_lock);
  164. GEM_BUG_ON(b->irq_wait);
  165. GEM_BUG_ON(!b->irq_armed);
  166. GEM_BUG_ON(!b->irq_enabled);
  167. if (!--b->irq_enabled)
  168. irq_disable(engine);
  169. b->irq_armed = false;
  170. }
  171. void intel_engine_pin_breadcrumbs_irq(struct intel_engine_cs *engine)
  172. {
  173. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  174. spin_lock_irq(&b->irq_lock);
  175. if (!b->irq_enabled++)
  176. irq_enable(engine);
  177. GEM_BUG_ON(!b->irq_enabled); /* no overflow! */
  178. spin_unlock_irq(&b->irq_lock);
  179. }
  180. void intel_engine_unpin_breadcrumbs_irq(struct intel_engine_cs *engine)
  181. {
  182. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  183. spin_lock_irq(&b->irq_lock);
  184. GEM_BUG_ON(!b->irq_enabled); /* no underflow! */
  185. if (!--b->irq_enabled)
  186. irq_disable(engine);
  187. spin_unlock_irq(&b->irq_lock);
  188. }
  189. void intel_engine_disarm_breadcrumbs(struct intel_engine_cs *engine)
  190. {
  191. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  192. struct intel_wait *wait, *n;
  193. if (!b->irq_armed)
  194. return;
  195. /*
  196. * We only disarm the irq when we are idle (all requests completed),
  197. * so if the bottom-half remains asleep, it missed the request
  198. * completion.
  199. */
  200. if (intel_engine_wakeup(engine) & ENGINE_WAKEUP_ASLEEP)
  201. missed_breadcrumb(engine);
  202. spin_lock_irq(&b->rb_lock);
  203. spin_lock(&b->irq_lock);
  204. b->irq_wait = NULL;
  205. if (b->irq_armed)
  206. __intel_engine_disarm_breadcrumbs(engine);
  207. spin_unlock(&b->irq_lock);
  208. rbtree_postorder_for_each_entry_safe(wait, n, &b->waiters, node) {
  209. GEM_BUG_ON(!i915_seqno_passed(intel_engine_get_seqno(engine),
  210. wait->seqno));
  211. RB_CLEAR_NODE(&wait->node);
  212. wake_up_process(wait->tsk);
  213. }
  214. b->waiters = RB_ROOT;
  215. spin_unlock_irq(&b->rb_lock);
  216. }
  217. static bool use_fake_irq(const struct intel_breadcrumbs *b)
  218. {
  219. const struct intel_engine_cs *engine =
  220. container_of(b, struct intel_engine_cs, breadcrumbs);
  221. if (!test_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings))
  222. return false;
  223. /* Only start with the heavy weight fake irq timer if we have not
  224. * seen any interrupts since enabling it the first time. If the
  225. * interrupts are still arriving, it means we made a mistake in our
  226. * engine->seqno_barrier(), a timing error that should be transient
  227. * and unlikely to reoccur.
  228. */
  229. return atomic_read(&engine->irq_count) == b->hangcheck_interrupts;
  230. }
  231. static void enable_fake_irq(struct intel_breadcrumbs *b)
  232. {
  233. /* Ensure we never sleep indefinitely */
  234. if (!b->irq_enabled || use_fake_irq(b))
  235. mod_timer(&b->fake_irq, jiffies + 1);
  236. else
  237. mod_timer(&b->hangcheck, wait_timeout());
  238. }
  239. static bool __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
  240. {
  241. struct intel_engine_cs *engine =
  242. container_of(b, struct intel_engine_cs, breadcrumbs);
  243. struct drm_i915_private *i915 = engine->i915;
  244. bool enabled;
  245. lockdep_assert_held(&b->irq_lock);
  246. if (b->irq_armed)
  247. return false;
  248. /* The breadcrumb irq will be disarmed on the interrupt after the
  249. * waiters are signaled. This gives us a single interrupt window in
  250. * which we can add a new waiter and avoid the cost of re-enabling
  251. * the irq.
  252. */
  253. b->irq_armed = true;
  254. if (I915_SELFTEST_ONLY(b->mock)) {
  255. /* For our mock objects we want to avoid interaction
  256. * with the real hardware (which is not set up). So
  257. * we simply pretend we have enabled the powerwell
  258. * and the irq, and leave it up to the mock
  259. * implementation to call intel_engine_wakeup()
  260. * itself when it wants to simulate a user interrupt,
  261. */
  262. return true;
  263. }
  264. /* Since we are waiting on a request, the GPU should be busy
  265. * and should have its own rpm reference. This is tracked
  266. * by i915->gt.awake, we can forgo holding our own wakref
  267. * for the interrupt as before i915->gt.awake is released (when
  268. * the driver is idle) we disarm the breadcrumbs.
  269. */
  270. /* No interrupts? Kick the waiter every jiffie! */
  271. enabled = false;
  272. if (!b->irq_enabled++ &&
  273. !test_bit(engine->id, &i915->gpu_error.test_irq_rings)) {
  274. irq_enable(engine);
  275. enabled = true;
  276. }
  277. enable_fake_irq(b);
  278. return enabled;
  279. }
  280. static inline struct intel_wait *to_wait(struct rb_node *node)
  281. {
  282. return rb_entry(node, struct intel_wait, node);
  283. }
  284. static inline void __intel_breadcrumbs_finish(struct intel_breadcrumbs *b,
  285. struct intel_wait *wait)
  286. {
  287. lockdep_assert_held(&b->rb_lock);
  288. GEM_BUG_ON(b->irq_wait == wait);
  289. /*
  290. * This request is completed, so remove it from the tree, mark it as
  291. * complete, and *then* wake up the associated task. N.B. when the
  292. * task wakes up, it will find the empty rb_node, discern that it
  293. * has already been removed from the tree and skip the serialisation
  294. * of the b->rb_lock and b->irq_lock. This means that the destruction
  295. * of the intel_wait is not serialised with the interrupt handler
  296. * by the waiter - it must instead be serialised by the caller.
  297. */
  298. rb_erase(&wait->node, &b->waiters);
  299. RB_CLEAR_NODE(&wait->node);
  300. if (wait->tsk->state != TASK_RUNNING)
  301. wake_up_process(wait->tsk); /* implicit smp_wmb() */
  302. }
  303. static inline void __intel_breadcrumbs_next(struct intel_engine_cs *engine,
  304. struct rb_node *next)
  305. {
  306. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  307. spin_lock(&b->irq_lock);
  308. GEM_BUG_ON(!b->irq_armed);
  309. GEM_BUG_ON(!b->irq_wait);
  310. b->irq_wait = to_wait(next);
  311. spin_unlock(&b->irq_lock);
  312. /* We always wake up the next waiter that takes over as the bottom-half
  313. * as we may delegate not only the irq-seqno barrier to the next waiter
  314. * but also the task of waking up concurrent waiters.
  315. */
  316. if (next)
  317. wake_up_process(to_wait(next)->tsk);
  318. }
  319. static bool __intel_engine_add_wait(struct intel_engine_cs *engine,
  320. struct intel_wait *wait)
  321. {
  322. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  323. struct rb_node **p, *parent, *completed;
  324. bool first, armed;
  325. u32 seqno;
  326. GEM_BUG_ON(!wait->seqno);
  327. /* Insert the request into the retirement ordered list
  328. * of waiters by walking the rbtree. If we are the oldest
  329. * seqno in the tree (the first to be retired), then
  330. * set ourselves as the bottom-half.
  331. *
  332. * As we descend the tree, prune completed branches since we hold the
  333. * spinlock we know that the first_waiter must be delayed and can
  334. * reduce some of the sequential wake up latency if we take action
  335. * ourselves and wake up the completed tasks in parallel. Also, by
  336. * removing stale elements in the tree, we may be able to reduce the
  337. * ping-pong between the old bottom-half and ourselves as first-waiter.
  338. */
  339. armed = false;
  340. first = true;
  341. parent = NULL;
  342. completed = NULL;
  343. seqno = intel_engine_get_seqno(engine);
  344. /* If the request completed before we managed to grab the spinlock,
  345. * return now before adding ourselves to the rbtree. We let the
  346. * current bottom-half handle any pending wakeups and instead
  347. * try and get out of the way quickly.
  348. */
  349. if (i915_seqno_passed(seqno, wait->seqno)) {
  350. RB_CLEAR_NODE(&wait->node);
  351. return first;
  352. }
  353. p = &b->waiters.rb_node;
  354. while (*p) {
  355. parent = *p;
  356. if (wait->seqno == to_wait(parent)->seqno) {
  357. /* We have multiple waiters on the same seqno, select
  358. * the highest priority task (that with the smallest
  359. * task->prio) to serve as the bottom-half for this
  360. * group.
  361. */
  362. if (wait->tsk->prio > to_wait(parent)->tsk->prio) {
  363. p = &parent->rb_right;
  364. first = false;
  365. } else {
  366. p = &parent->rb_left;
  367. }
  368. } else if (i915_seqno_passed(wait->seqno,
  369. to_wait(parent)->seqno)) {
  370. p = &parent->rb_right;
  371. if (i915_seqno_passed(seqno, to_wait(parent)->seqno))
  372. completed = parent;
  373. else
  374. first = false;
  375. } else {
  376. p = &parent->rb_left;
  377. }
  378. }
  379. rb_link_node(&wait->node, parent, p);
  380. rb_insert_color(&wait->node, &b->waiters);
  381. if (first) {
  382. spin_lock(&b->irq_lock);
  383. b->irq_wait = wait;
  384. /* After assigning ourselves as the new bottom-half, we must
  385. * perform a cursory check to prevent a missed interrupt.
  386. * Either we miss the interrupt whilst programming the hardware,
  387. * or if there was a previous waiter (for a later seqno) they
  388. * may be woken instead of us (due to the inherent race
  389. * in the unlocked read of b->irq_seqno_bh in the irq handler)
  390. * and so we miss the wake up.
  391. */
  392. armed = __intel_breadcrumbs_enable_irq(b);
  393. spin_unlock(&b->irq_lock);
  394. }
  395. if (completed) {
  396. /* Advance the bottom-half (b->irq_wait) before we wake up
  397. * the waiters who may scribble over their intel_wait
  398. * just as the interrupt handler is dereferencing it via
  399. * b->irq_wait.
  400. */
  401. if (!first) {
  402. struct rb_node *next = rb_next(completed);
  403. GEM_BUG_ON(next == &wait->node);
  404. __intel_breadcrumbs_next(engine, next);
  405. }
  406. do {
  407. struct intel_wait *crumb = to_wait(completed);
  408. completed = rb_prev(completed);
  409. __intel_breadcrumbs_finish(b, crumb);
  410. } while (completed);
  411. }
  412. GEM_BUG_ON(!b->irq_wait);
  413. GEM_BUG_ON(!b->irq_armed);
  414. GEM_BUG_ON(rb_first(&b->waiters) != &b->irq_wait->node);
  415. return armed;
  416. }
  417. bool intel_engine_add_wait(struct intel_engine_cs *engine,
  418. struct intel_wait *wait)
  419. {
  420. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  421. bool armed;
  422. spin_lock_irq(&b->rb_lock);
  423. armed = __intel_engine_add_wait(engine, wait);
  424. spin_unlock_irq(&b->rb_lock);
  425. if (armed)
  426. return armed;
  427. /* Make the caller recheck if its request has already started. */
  428. return i915_seqno_passed(intel_engine_get_seqno(engine),
  429. wait->seqno - 1);
  430. }
  431. static inline bool chain_wakeup(struct rb_node *rb, int priority)
  432. {
  433. return rb && to_wait(rb)->tsk->prio <= priority;
  434. }
  435. static inline int wakeup_priority(struct intel_breadcrumbs *b,
  436. struct task_struct *tsk)
  437. {
  438. if (tsk == b->signaler)
  439. return INT_MIN;
  440. else
  441. return tsk->prio;
  442. }
  443. static void __intel_engine_remove_wait(struct intel_engine_cs *engine,
  444. struct intel_wait *wait)
  445. {
  446. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  447. lockdep_assert_held(&b->rb_lock);
  448. if (RB_EMPTY_NODE(&wait->node))
  449. goto out;
  450. if (b->irq_wait == wait) {
  451. const int priority = wakeup_priority(b, wait->tsk);
  452. struct rb_node *next;
  453. /* We are the current bottom-half. Find the next candidate,
  454. * the first waiter in the queue on the remaining oldest
  455. * request. As multiple seqnos may complete in the time it
  456. * takes us to wake up and find the next waiter, we have to
  457. * wake up that waiter for it to perform its own coherent
  458. * completion check.
  459. */
  460. next = rb_next(&wait->node);
  461. if (chain_wakeup(next, priority)) {
  462. /* If the next waiter is already complete,
  463. * wake it up and continue onto the next waiter. So
  464. * if have a small herd, they will wake up in parallel
  465. * rather than sequentially, which should reduce
  466. * the overall latency in waking all the completed
  467. * clients.
  468. *
  469. * However, waking up a chain adds extra latency to
  470. * the first_waiter. This is undesirable if that
  471. * waiter is a high priority task.
  472. */
  473. u32 seqno = intel_engine_get_seqno(engine);
  474. while (i915_seqno_passed(seqno, to_wait(next)->seqno)) {
  475. struct rb_node *n = rb_next(next);
  476. __intel_breadcrumbs_finish(b, to_wait(next));
  477. next = n;
  478. if (!chain_wakeup(next, priority))
  479. break;
  480. }
  481. }
  482. __intel_breadcrumbs_next(engine, next);
  483. } else {
  484. GEM_BUG_ON(rb_first(&b->waiters) == &wait->node);
  485. }
  486. GEM_BUG_ON(RB_EMPTY_NODE(&wait->node));
  487. rb_erase(&wait->node, &b->waiters);
  488. RB_CLEAR_NODE(&wait->node);
  489. out:
  490. GEM_BUG_ON(b->irq_wait == wait);
  491. GEM_BUG_ON(rb_first(&b->waiters) !=
  492. (b->irq_wait ? &b->irq_wait->node : NULL));
  493. }
  494. void intel_engine_remove_wait(struct intel_engine_cs *engine,
  495. struct intel_wait *wait)
  496. {
  497. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  498. /* Quick check to see if this waiter was already decoupled from
  499. * the tree by the bottom-half to avoid contention on the spinlock
  500. * by the herd.
  501. */
  502. if (RB_EMPTY_NODE(&wait->node)) {
  503. GEM_BUG_ON(READ_ONCE(b->irq_wait) == wait);
  504. return;
  505. }
  506. spin_lock_irq(&b->rb_lock);
  507. __intel_engine_remove_wait(engine, wait);
  508. spin_unlock_irq(&b->rb_lock);
  509. }
  510. static void signaler_set_rtpriority(void)
  511. {
  512. struct sched_param param = { .sched_priority = 1 };
  513. sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
  514. }
  515. static int intel_breadcrumbs_signaler(void *arg)
  516. {
  517. struct intel_engine_cs *engine = arg;
  518. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  519. struct i915_request *rq, *n;
  520. /* Install ourselves with high priority to reduce signalling latency */
  521. signaler_set_rtpriority();
  522. do {
  523. bool do_schedule = true;
  524. LIST_HEAD(list);
  525. u32 seqno;
  526. set_current_state(TASK_INTERRUPTIBLE);
  527. if (list_empty(&b->signals))
  528. goto sleep;
  529. /*
  530. * We are either woken up by the interrupt bottom-half,
  531. * or by a client adding a new signaller. In both cases,
  532. * the GPU seqno may have advanced beyond our oldest signal.
  533. * If it has, propagate the signal, remove the waiter and
  534. * check again with the next oldest signal. Otherwise we
  535. * need to wait for a new interrupt from the GPU or for
  536. * a new client.
  537. */
  538. seqno = intel_engine_get_seqno(engine);
  539. spin_lock_irq(&b->rb_lock);
  540. list_for_each_entry_safe(rq, n, &b->signals, signaling.link) {
  541. u32 this = rq->signaling.wait.seqno;
  542. GEM_BUG_ON(!rq->signaling.wait.seqno);
  543. if (!i915_seqno_passed(seqno, this))
  544. break;
  545. if (likely(this == i915_request_global_seqno(rq))) {
  546. __intel_engine_remove_wait(engine,
  547. &rq->signaling.wait);
  548. rq->signaling.wait.seqno = 0;
  549. __list_del_entry(&rq->signaling.link);
  550. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  551. &rq->fence.flags)) {
  552. list_add_tail(&rq->signaling.link,
  553. &list);
  554. i915_request_get(rq);
  555. }
  556. }
  557. }
  558. spin_unlock_irq(&b->rb_lock);
  559. if (!list_empty(&list)) {
  560. local_bh_disable();
  561. list_for_each_entry_safe(rq, n, &list, signaling.link) {
  562. dma_fence_signal(&rq->fence);
  563. GEM_BUG_ON(!i915_request_completed(rq));
  564. i915_request_put(rq);
  565. }
  566. local_bh_enable(); /* kick start the tasklets */
  567. /*
  568. * If the engine is saturated we may be continually
  569. * processing completed requests. This angers the
  570. * NMI watchdog if we never let anything else
  571. * have access to the CPU. Let's pretend to be nice
  572. * and relinquish the CPU if we burn through the
  573. * entire RT timeslice!
  574. */
  575. do_schedule = need_resched();
  576. }
  577. if (unlikely(do_schedule)) {
  578. /* Before we sleep, check for a missed seqno */
  579. if (current->state & TASK_NORMAL &&
  580. !list_empty(&b->signals) &&
  581. engine->irq_seqno_barrier &&
  582. test_and_clear_bit(ENGINE_IRQ_BREADCRUMB,
  583. &engine->irq_posted)) {
  584. engine->irq_seqno_barrier(engine);
  585. intel_engine_wakeup(engine);
  586. }
  587. sleep:
  588. if (kthread_should_park())
  589. kthread_parkme();
  590. if (unlikely(kthread_should_stop()))
  591. break;
  592. schedule();
  593. }
  594. } while (1);
  595. __set_current_state(TASK_RUNNING);
  596. return 0;
  597. }
  598. static void insert_signal(struct intel_breadcrumbs *b,
  599. struct i915_request *request,
  600. const u32 seqno)
  601. {
  602. struct i915_request *iter;
  603. lockdep_assert_held(&b->rb_lock);
  604. /*
  605. * A reasonable assumption is that we are called to add signals
  606. * in sequence, as the requests are submitted for execution and
  607. * assigned a global_seqno. This will be the case for the majority
  608. * of internally generated signals (inter-engine signaling).
  609. *
  610. * Out of order waiters triggering random signaling enabling will
  611. * be more problematic, but hopefully rare enough and the list
  612. * small enough that the O(N) insertion sort is not an issue.
  613. */
  614. list_for_each_entry_reverse(iter, &b->signals, signaling.link)
  615. if (i915_seqno_passed(seqno, iter->signaling.wait.seqno))
  616. break;
  617. list_add(&request->signaling.link, &iter->signaling.link);
  618. }
  619. void intel_engine_enable_signaling(struct i915_request *request, bool wakeup)
  620. {
  621. struct intel_engine_cs *engine = request->engine;
  622. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  623. u32 seqno;
  624. /*
  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_request_global_seqno(request);
  635. if (!seqno) /* will be enabled later upon execution */
  636. return;
  637. GEM_BUG_ON(request->signaling.wait.seqno);
  638. request->signaling.wait.tsk = b->signaler;
  639. request->signaling.wait.request = request;
  640. request->signaling.wait.seqno = seqno;
  641. /*
  642. * Add ourselves into the list of waiters, but registering 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. spin_lock(&b->rb_lock);
  651. insert_signal(b, request, seqno);
  652. wakeup &= __intel_engine_add_wait(engine, &request->signaling.wait);
  653. spin_unlock(&b->rb_lock);
  654. if (wakeup)
  655. wake_up_process(b->signaler);
  656. }
  657. void intel_engine_cancel_signaling(struct i915_request *request)
  658. {
  659. struct intel_engine_cs *engine = request->engine;
  660. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  661. GEM_BUG_ON(!irqs_disabled());
  662. lockdep_assert_held(&request->lock);
  663. if (!READ_ONCE(request->signaling.wait.seqno))
  664. return;
  665. spin_lock(&b->rb_lock);
  666. __intel_engine_remove_wait(engine, &request->signaling.wait);
  667. if (fetch_and_zero(&request->signaling.wait.seqno))
  668. __list_del_entry(&request->signaling.link);
  669. spin_unlock(&b->rb_lock);
  670. }
  671. int intel_engine_init_breadcrumbs(struct intel_engine_cs *engine)
  672. {
  673. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  674. struct task_struct *tsk;
  675. spin_lock_init(&b->rb_lock);
  676. spin_lock_init(&b->irq_lock);
  677. timer_setup(&b->fake_irq, intel_breadcrumbs_fake_irq, 0);
  678. timer_setup(&b->hangcheck, intel_breadcrumbs_hangcheck, 0);
  679. INIT_LIST_HEAD(&b->signals);
  680. /* Spawn a thread to provide a common bottom-half for all signals.
  681. * As this is an asynchronous interface we cannot steal the current
  682. * task for handling the bottom-half to the user interrupt, therefore
  683. * we create a thread to do the coherent seqno dance after the
  684. * interrupt and then signal the waitqueue (via the dma-buf/fence).
  685. */
  686. tsk = kthread_run(intel_breadcrumbs_signaler, engine,
  687. "i915/signal:%d", engine->id);
  688. if (IS_ERR(tsk))
  689. return PTR_ERR(tsk);
  690. b->signaler = tsk;
  691. return 0;
  692. }
  693. static void cancel_fake_irq(struct intel_engine_cs *engine)
  694. {
  695. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  696. del_timer_sync(&b->hangcheck);
  697. del_timer_sync(&b->fake_irq);
  698. clear_bit(engine->id, &engine->i915->gpu_error.missed_irq_rings);
  699. }
  700. void intel_engine_reset_breadcrumbs(struct intel_engine_cs *engine)
  701. {
  702. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  703. cancel_fake_irq(engine);
  704. spin_lock_irq(&b->irq_lock);
  705. if (b->irq_enabled)
  706. irq_enable(engine);
  707. else
  708. irq_disable(engine);
  709. /* We set the IRQ_BREADCRUMB bit when we enable the irq presuming the
  710. * GPU is active and may have already executed the MI_USER_INTERRUPT
  711. * before the CPU is ready to receive. However, the engine is currently
  712. * idle (we haven't started it yet), there is no possibility for a
  713. * missed interrupt as we enabled the irq and so we can clear the
  714. * immediate wakeup (until a real interrupt arrives for the waiter).
  715. */
  716. clear_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
  717. if (b->irq_armed)
  718. enable_fake_irq(b);
  719. spin_unlock_irq(&b->irq_lock);
  720. }
  721. void intel_engine_fini_breadcrumbs(struct intel_engine_cs *engine)
  722. {
  723. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  724. /* The engines should be idle and all requests accounted for! */
  725. WARN_ON(READ_ONCE(b->irq_wait));
  726. WARN_ON(!RB_EMPTY_ROOT(&b->waiters));
  727. WARN_ON(!list_empty(&b->signals));
  728. if (!IS_ERR_OR_NULL(b->signaler))
  729. kthread_stop(b->signaler);
  730. cancel_fake_irq(engine);
  731. }
  732. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  733. #include "selftests/intel_breadcrumbs.c"
  734. #endif