intel_hangcheck.c 13 KB

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