intel_hangcheck.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright © 2016 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 "i915_drv.h"
  25. static bool
  26. ipehr_is_semaphore_wait(struct intel_engine_cs *engine, u32 ipehr)
  27. {
  28. if (INTEL_GEN(engine->i915) >= 8) {
  29. return (ipehr >> 23) == 0x1c;
  30. } else {
  31. ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
  32. return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
  33. MI_SEMAPHORE_REGISTER);
  34. }
  35. }
  36. static struct intel_engine_cs *
  37. semaphore_wait_to_signaller_ring(struct intel_engine_cs *engine, u32 ipehr,
  38. u64 offset)
  39. {
  40. struct drm_i915_private *dev_priv = engine->i915;
  41. struct intel_engine_cs *signaller;
  42. enum intel_engine_id id;
  43. if (INTEL_GEN(dev_priv) >= 8) {
  44. for_each_engine(signaller, dev_priv, id) {
  45. if (engine == signaller)
  46. continue;
  47. if (offset == signaller->semaphore.signal_ggtt[engine->hw_id])
  48. return signaller;
  49. }
  50. } else {
  51. u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
  52. for_each_engine(signaller, dev_priv, id) {
  53. if(engine == signaller)
  54. continue;
  55. if (sync_bits == signaller->semaphore.mbox.wait[engine->hw_id])
  56. return signaller;
  57. }
  58. }
  59. DRM_DEBUG_DRIVER("No signaller ring found for %s, ipehr 0x%08x, offset 0x%016llx\n",
  60. engine->name, ipehr, offset);
  61. return ERR_PTR(-ENODEV);
  62. }
  63. static struct intel_engine_cs *
  64. semaphore_waits_for(struct intel_engine_cs *engine, u32 *seqno)
  65. {
  66. struct drm_i915_private *dev_priv = engine->i915;
  67. void __iomem *vaddr;
  68. u32 cmd, ipehr, head;
  69. u64 offset = 0;
  70. int i, backwards;
  71. /*
  72. * This function does not support execlist mode - any attempt to
  73. * proceed further into this function will result in a kernel panic
  74. * when dereferencing ring->buffer, which is not set up in execlist
  75. * mode.
  76. *
  77. * The correct way of doing it would be to derive the currently
  78. * executing ring buffer from the current context, which is derived
  79. * from the currently running request. Unfortunately, to get the
  80. * current request we would have to grab the struct_mutex before doing
  81. * anything else, which would be ill-advised since some other thread
  82. * might have grabbed it already and managed to hang itself, causing
  83. * the hang checker to deadlock.
  84. *
  85. * Therefore, this function does not support execlist mode in its
  86. * current form. Just return NULL and move on.
  87. */
  88. if (engine->buffer == NULL)
  89. return NULL;
  90. ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
  91. if (!ipehr_is_semaphore_wait(engine, ipehr))
  92. return NULL;
  93. /*
  94. * HEAD is likely pointing to the dword after the actual command,
  95. * so scan backwards until we find the MBOX. But limit it to just 3
  96. * or 4 dwords depending on the semaphore wait command size.
  97. * Note that we don't care about ACTHD here since that might
  98. * point at at batch, and semaphores are always emitted into the
  99. * ringbuffer itself.
  100. */
  101. head = I915_READ_HEAD(engine) & HEAD_ADDR;
  102. backwards = (INTEL_GEN(dev_priv) >= 8) ? 5 : 4;
  103. vaddr = (void __iomem *)engine->buffer->vaddr;
  104. for (i = backwards; i; --i) {
  105. /*
  106. * Be paranoid and presume the hw has gone off into the wild -
  107. * our ring is smaller than what the hardware (and hence
  108. * HEAD_ADDR) allows. Also handles wrap-around.
  109. */
  110. head &= engine->buffer->size - 1;
  111. /* This here seems to blow up */
  112. cmd = ioread32(vaddr + head);
  113. if (cmd == ipehr)
  114. break;
  115. head -= 4;
  116. }
  117. if (!i)
  118. return NULL;
  119. *seqno = ioread32(vaddr + head + 4) + 1;
  120. if (INTEL_GEN(dev_priv) >= 8) {
  121. offset = ioread32(vaddr + head + 12);
  122. offset <<= 32;
  123. offset |= ioread32(vaddr + head + 8);
  124. }
  125. return semaphore_wait_to_signaller_ring(engine, ipehr, offset);
  126. }
  127. static int semaphore_passed(struct intel_engine_cs *engine)
  128. {
  129. struct drm_i915_private *dev_priv = engine->i915;
  130. struct intel_engine_cs *signaller;
  131. u32 seqno;
  132. engine->hangcheck.deadlock++;
  133. signaller = semaphore_waits_for(engine, &seqno);
  134. if (signaller == NULL)
  135. return -1;
  136. if (IS_ERR(signaller))
  137. return 0;
  138. /* Prevent pathological recursion due to driver bugs */
  139. if (signaller->hangcheck.deadlock >= I915_NUM_ENGINES)
  140. return -1;
  141. if (i915_seqno_passed(intel_engine_get_seqno(signaller), seqno))
  142. return 1;
  143. /* cursory check for an unkickable deadlock */
  144. if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
  145. semaphore_passed(signaller) < 0)
  146. return -1;
  147. return 0;
  148. }
  149. static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
  150. {
  151. struct intel_engine_cs *engine;
  152. enum intel_engine_id id;
  153. for_each_engine(engine, dev_priv, id)
  154. engine->hangcheck.deadlock = 0;
  155. }
  156. static bool instdone_unchanged(u32 current_instdone, u32 *old_instdone)
  157. {
  158. u32 tmp = current_instdone | *old_instdone;
  159. bool unchanged;
  160. unchanged = tmp == *old_instdone;
  161. *old_instdone |= tmp;
  162. return unchanged;
  163. }
  164. static bool subunits_stuck(struct intel_engine_cs *engine)
  165. {
  166. struct drm_i915_private *dev_priv = engine->i915;
  167. struct intel_instdone instdone;
  168. struct intel_instdone *accu_instdone = &engine->hangcheck.instdone;
  169. bool stuck;
  170. int slice;
  171. int subslice;
  172. if (engine->id != RCS)
  173. return true;
  174. intel_engine_get_instdone(engine, &instdone);
  175. /* There might be unstable subunit states even when
  176. * actual head is not moving. Filter out the unstable ones by
  177. * accumulating the undone -> done transitions and only
  178. * consider those as progress.
  179. */
  180. stuck = instdone_unchanged(instdone.instdone,
  181. &accu_instdone->instdone);
  182. stuck &= instdone_unchanged(instdone.slice_common,
  183. &accu_instdone->slice_common);
  184. for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
  185. stuck &= instdone_unchanged(instdone.sampler[slice][subslice],
  186. &accu_instdone->sampler[slice][subslice]);
  187. stuck &= instdone_unchanged(instdone.row[slice][subslice],
  188. &accu_instdone->row[slice][subslice]);
  189. }
  190. return stuck;
  191. }
  192. static enum intel_engine_hangcheck_action
  193. head_stuck(struct intel_engine_cs *engine, u64 acthd)
  194. {
  195. if (acthd != engine->hangcheck.acthd) {
  196. /* Clear subunit states on head movement */
  197. memset(&engine->hangcheck.instdone, 0,
  198. sizeof(engine->hangcheck.instdone));
  199. return ENGINE_ACTIVE_HEAD;
  200. }
  201. if (!subunits_stuck(engine))
  202. return ENGINE_ACTIVE_SUBUNITS;
  203. return ENGINE_DEAD;
  204. }
  205. static enum intel_engine_hangcheck_action
  206. engine_stuck(struct intel_engine_cs *engine, u64 acthd)
  207. {
  208. struct drm_i915_private *dev_priv = engine->i915;
  209. enum intel_engine_hangcheck_action ha;
  210. u32 tmp;
  211. ha = head_stuck(engine, acthd);
  212. if (ha != ENGINE_DEAD)
  213. return ha;
  214. if (IS_GEN2(dev_priv))
  215. return ENGINE_DEAD;
  216. /* Is the chip hanging on a WAIT_FOR_EVENT?
  217. * If so we can simply poke the RB_WAIT bit
  218. * and break the hang. This should work on
  219. * all but the second generation chipsets.
  220. */
  221. tmp = I915_READ_CTL(engine);
  222. if (tmp & RING_WAIT) {
  223. i915_handle_error(dev_priv, 0,
  224. "Kicking stuck wait on %s",
  225. engine->name);
  226. I915_WRITE_CTL(engine, tmp);
  227. return ENGINE_WAIT_KICK;
  228. }
  229. if (INTEL_GEN(dev_priv) >= 6 && tmp & RING_WAIT_SEMAPHORE) {
  230. switch (semaphore_passed(engine)) {
  231. default:
  232. return ENGINE_DEAD;
  233. case 1:
  234. i915_handle_error(dev_priv, 0,
  235. "Kicking stuck semaphore on %s",
  236. engine->name);
  237. I915_WRITE_CTL(engine, tmp);
  238. return ENGINE_WAIT_KICK;
  239. case 0:
  240. return ENGINE_WAIT;
  241. }
  242. }
  243. return ENGINE_DEAD;
  244. }
  245. static void hangcheck_load_sample(struct intel_engine_cs *engine,
  246. struct intel_engine_hangcheck *hc)
  247. {
  248. /* We don't strictly need an irq-barrier here, as we are not
  249. * serving an interrupt request, be paranoid in case the
  250. * barrier has side-effects (such as preventing a broken
  251. * cacheline snoop) and so be sure that we can see the seqno
  252. * advance. If the seqno should stick, due to a stale
  253. * cacheline, we would erroneously declare the GPU hung.
  254. */
  255. if (engine->irq_seqno_barrier)
  256. engine->irq_seqno_barrier(engine);
  257. hc->acthd = intel_engine_get_active_head(engine);
  258. hc->seqno = intel_engine_get_seqno(engine);
  259. }
  260. static void hangcheck_store_sample(struct intel_engine_cs *engine,
  261. const struct intel_engine_hangcheck *hc)
  262. {
  263. engine->hangcheck.acthd = hc->acthd;
  264. engine->hangcheck.seqno = hc->seqno;
  265. engine->hangcheck.action = hc->action;
  266. engine->hangcheck.stalled = hc->stalled;
  267. }
  268. static enum intel_engine_hangcheck_action
  269. hangcheck_get_action(struct intel_engine_cs *engine,
  270. const struct intel_engine_hangcheck *hc)
  271. {
  272. if (engine->hangcheck.seqno != hc->seqno)
  273. return ENGINE_ACTIVE_SEQNO;
  274. if (i915_seqno_passed(hc->seqno, intel_engine_last_submit(engine)))
  275. return ENGINE_IDLE;
  276. return engine_stuck(engine, hc->acthd);
  277. }
  278. static void hangcheck_accumulate_sample(struct intel_engine_cs *engine,
  279. struct intel_engine_hangcheck *hc)
  280. {
  281. unsigned long timeout = I915_ENGINE_DEAD_TIMEOUT;
  282. hc->action = hangcheck_get_action(engine, hc);
  283. /* We always increment the progress
  284. * if the engine is busy and still processing
  285. * the same request, so that no single request
  286. * can run indefinitely (such as a chain of
  287. * batches). The only time we do not increment
  288. * the hangcheck score on this ring, if this
  289. * engine is in a legitimate wait for another
  290. * engine. In that case the waiting engine is a
  291. * victim and we want to be sure we catch the
  292. * right culprit. Then every time we do kick
  293. * the ring, make it as a progress as the seqno
  294. * advancement might ensure and if not, it
  295. * will catch the hanging engine.
  296. */
  297. switch (hc->action) {
  298. case ENGINE_IDLE:
  299. case ENGINE_ACTIVE_SEQNO:
  300. /* Clear head and subunit states on seqno movement */
  301. hc->acthd = 0;
  302. memset(&engine->hangcheck.instdone, 0,
  303. sizeof(engine->hangcheck.instdone));
  304. /* Intentional fall through */
  305. case ENGINE_WAIT_KICK:
  306. case ENGINE_WAIT:
  307. engine->hangcheck.action_timestamp = jiffies;
  308. break;
  309. case ENGINE_ACTIVE_HEAD:
  310. case ENGINE_ACTIVE_SUBUNITS:
  311. /* Seqno stuck with still active engine gets leeway,
  312. * in hopes that it is just a long shader.
  313. */
  314. timeout = I915_SEQNO_DEAD_TIMEOUT;
  315. break;
  316. case ENGINE_DEAD:
  317. break;
  318. default:
  319. MISSING_CASE(hc->action);
  320. }
  321. hc->stalled = time_after(jiffies,
  322. engine->hangcheck.action_timestamp + timeout);
  323. }
  324. static void hangcheck_declare_hang(struct drm_i915_private *i915,
  325. unsigned int hung,
  326. unsigned int stuck)
  327. {
  328. struct intel_engine_cs *engine;
  329. char msg[80];
  330. unsigned int tmp;
  331. int len;
  332. /* If some rings hung but others were still busy, only
  333. * blame the hanging rings in the synopsis.
  334. */
  335. if (stuck != hung)
  336. hung &= ~stuck;
  337. len = scnprintf(msg, sizeof(msg),
  338. "%s on ", stuck == hung ? "No progress" : "Hang");
  339. for_each_engine_masked(engine, i915, hung, tmp)
  340. len += scnprintf(msg + len, sizeof(msg) - len,
  341. "%s, ", engine->name);
  342. msg[len-2] = '\0';
  343. return i915_handle_error(i915, hung, msg);
  344. }
  345. /*
  346. * This is called when the chip hasn't reported back with completed
  347. * batchbuffers in a long time. We keep track per ring seqno progress and
  348. * if there are no progress, hangcheck score for that ring is increased.
  349. * Further, acthd is inspected to see if the ring is stuck. On stuck case
  350. * we kick the ring. If we see no progress on three subsequent calls
  351. * we assume chip is wedged and try to fix it by resetting the chip.
  352. */
  353. static void i915_hangcheck_elapsed(struct work_struct *work)
  354. {
  355. struct drm_i915_private *dev_priv =
  356. container_of(work, typeof(*dev_priv),
  357. gpu_error.hangcheck_work.work);
  358. struct intel_engine_cs *engine;
  359. enum intel_engine_id id;
  360. unsigned int hung = 0, stuck = 0;
  361. int busy_count = 0;
  362. if (!i915.enable_hangcheck)
  363. return;
  364. if (!READ_ONCE(dev_priv->gt.awake))
  365. return;
  366. if (i915_terminally_wedged(&dev_priv->gpu_error))
  367. return;
  368. /* As enabling the GPU requires fairly extensive mmio access,
  369. * periodically arm the mmio checker to see if we are triggering
  370. * any invalid access.
  371. */
  372. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  373. for_each_engine(engine, dev_priv, id) {
  374. struct intel_engine_hangcheck cur_state, *hc = &cur_state;
  375. const bool busy = intel_engine_has_waiter(engine);
  376. semaphore_clear_deadlocks(dev_priv);
  377. hangcheck_load_sample(engine, hc);
  378. hangcheck_accumulate_sample(engine, hc);
  379. hangcheck_store_sample(engine, hc);
  380. if (engine->hangcheck.stalled) {
  381. hung |= intel_engine_flag(engine);
  382. if (hc->action != ENGINE_DEAD)
  383. stuck |= intel_engine_flag(engine);
  384. }
  385. busy_count += busy;
  386. }
  387. if (hung)
  388. hangcheck_declare_hang(dev_priv, hung, stuck);
  389. /* Reset timer in case GPU hangs without another request being added */
  390. if (busy_count)
  391. i915_queue_hangcheck(dev_priv);
  392. }
  393. void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
  394. {
  395. memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
  396. }
  397. void intel_hangcheck_init(struct drm_i915_private *i915)
  398. {
  399. INIT_DELAYED_WORK(&i915->gpu_error.hangcheck_work,
  400. i915_hangcheck_elapsed);
  401. }