intel_breadcrumbs.c 25 KB

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