intel_hangcheck.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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, 0,
  206. "Kicking stuck wait on %s",
  207. engine->name);
  208. I915_WRITE_CTL(engine, tmp);
  209. return ENGINE_WAIT_KICK;
  210. }
  211. if (IS_GEN(dev_priv, 6, 7) && tmp & RING_WAIT_SEMAPHORE) {
  212. switch (semaphore_passed(engine)) {
  213. default:
  214. return ENGINE_DEAD;
  215. case 1:
  216. i915_handle_error(dev_priv, 0,
  217. "Kicking stuck semaphore on %s",
  218. engine->name);
  219. I915_WRITE_CTL(engine, tmp);
  220. return ENGINE_WAIT_KICK;
  221. case 0:
  222. return ENGINE_WAIT;
  223. }
  224. }
  225. return ENGINE_DEAD;
  226. }
  227. static void hangcheck_load_sample(struct intel_engine_cs *engine,
  228. struct intel_engine_hangcheck *hc)
  229. {
  230. /* We don't strictly need an irq-barrier here, as we are not
  231. * serving an interrupt request, be paranoid in case the
  232. * barrier has side-effects (such as preventing a broken
  233. * cacheline snoop) and so be sure that we can see the seqno
  234. * advance. If the seqno should stick, due to a stale
  235. * cacheline, we would erroneously declare the GPU hung.
  236. */
  237. if (engine->irq_seqno_barrier)
  238. engine->irq_seqno_barrier(engine);
  239. hc->acthd = intel_engine_get_active_head(engine);
  240. hc->seqno = intel_engine_get_seqno(engine);
  241. }
  242. static void hangcheck_store_sample(struct intel_engine_cs *engine,
  243. const struct intel_engine_hangcheck *hc)
  244. {
  245. engine->hangcheck.acthd = hc->acthd;
  246. engine->hangcheck.seqno = hc->seqno;
  247. engine->hangcheck.action = hc->action;
  248. engine->hangcheck.stalled = hc->stalled;
  249. }
  250. static enum intel_engine_hangcheck_action
  251. hangcheck_get_action(struct intel_engine_cs *engine,
  252. const struct intel_engine_hangcheck *hc)
  253. {
  254. if (engine->hangcheck.seqno != hc->seqno)
  255. return ENGINE_ACTIVE_SEQNO;
  256. if (intel_engine_is_idle(engine))
  257. return ENGINE_IDLE;
  258. return engine_stuck(engine, hc->acthd);
  259. }
  260. static void hangcheck_accumulate_sample(struct intel_engine_cs *engine,
  261. struct intel_engine_hangcheck *hc)
  262. {
  263. unsigned long timeout = I915_ENGINE_DEAD_TIMEOUT;
  264. hc->action = hangcheck_get_action(engine, hc);
  265. /* We always increment the progress
  266. * if the engine is busy and still processing
  267. * the same request, so that no single request
  268. * can run indefinitely (such as a chain of
  269. * batches). The only time we do not increment
  270. * the hangcheck score on this ring, if this
  271. * engine is in a legitimate wait for another
  272. * engine. In that case the waiting engine is a
  273. * victim and we want to be sure we catch the
  274. * right culprit. Then every time we do kick
  275. * the ring, make it as a progress as the seqno
  276. * advancement might ensure and if not, it
  277. * will catch the hanging engine.
  278. */
  279. switch (hc->action) {
  280. case ENGINE_IDLE:
  281. case ENGINE_ACTIVE_SEQNO:
  282. /* Clear head and subunit states on seqno movement */
  283. hc->acthd = 0;
  284. memset(&engine->hangcheck.instdone, 0,
  285. sizeof(engine->hangcheck.instdone));
  286. /* Intentional fall through */
  287. case ENGINE_WAIT_KICK:
  288. case ENGINE_WAIT:
  289. engine->hangcheck.action_timestamp = jiffies;
  290. break;
  291. case ENGINE_ACTIVE_HEAD:
  292. case ENGINE_ACTIVE_SUBUNITS:
  293. /*
  294. * Seqno stuck with still active engine gets leeway,
  295. * in hopes that it is just a long shader.
  296. */
  297. timeout = I915_SEQNO_DEAD_TIMEOUT;
  298. break;
  299. case ENGINE_DEAD:
  300. if (drm_debug & DRM_UT_DRIVER) {
  301. struct drm_printer p = drm_debug_printer("hangcheck");
  302. intel_engine_dump(engine, &p, "%s", engine->name);
  303. }
  304. break;
  305. default:
  306. MISSING_CASE(hc->action);
  307. }
  308. hc->stalled = time_after(jiffies,
  309. engine->hangcheck.action_timestamp + timeout);
  310. }
  311. static void hangcheck_declare_hang(struct drm_i915_private *i915,
  312. unsigned int hung,
  313. unsigned int stuck)
  314. {
  315. struct intel_engine_cs *engine;
  316. char msg[80];
  317. unsigned int tmp;
  318. int len;
  319. /* If some rings hung but others were still busy, only
  320. * blame the hanging rings in the synopsis.
  321. */
  322. if (stuck != hung)
  323. hung &= ~stuck;
  324. len = scnprintf(msg, sizeof(msg),
  325. "%s on ", stuck == hung ? "No progress" : "Hang");
  326. for_each_engine_masked(engine, i915, hung, tmp)
  327. len += scnprintf(msg + len, sizeof(msg) - len,
  328. "%s, ", engine->name);
  329. msg[len-2] = '\0';
  330. return i915_handle_error(i915, hung, "%s", msg);
  331. }
  332. /*
  333. * This is called when the chip hasn't reported back with completed
  334. * batchbuffers in a long time. We keep track per ring seqno progress and
  335. * if there are no progress, hangcheck score for that ring is increased.
  336. * Further, acthd is inspected to see if the ring is stuck. On stuck case
  337. * we kick the ring. If we see no progress on three subsequent calls
  338. * we assume chip is wedged and try to fix it by resetting the chip.
  339. */
  340. static void i915_hangcheck_elapsed(struct work_struct *work)
  341. {
  342. struct drm_i915_private *dev_priv =
  343. container_of(work, typeof(*dev_priv),
  344. gpu_error.hangcheck_work.work);
  345. struct intel_engine_cs *engine;
  346. enum intel_engine_id id;
  347. unsigned int hung = 0, stuck = 0;
  348. int busy_count = 0;
  349. if (!i915_modparams.enable_hangcheck)
  350. return;
  351. if (!READ_ONCE(dev_priv->gt.awake))
  352. return;
  353. if (i915_terminally_wedged(&dev_priv->gpu_error))
  354. return;
  355. /* As enabling the GPU requires fairly extensive mmio access,
  356. * periodically arm the mmio checker to see if we are triggering
  357. * any invalid access.
  358. */
  359. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  360. for_each_engine(engine, dev_priv, id) {
  361. const bool busy = intel_engine_has_waiter(engine);
  362. struct intel_engine_hangcheck hc;
  363. semaphore_clear_deadlocks(dev_priv);
  364. hangcheck_load_sample(engine, &hc);
  365. hangcheck_accumulate_sample(engine, &hc);
  366. hangcheck_store_sample(engine, &hc);
  367. if (engine->hangcheck.stalled) {
  368. hung |= intel_engine_flag(engine);
  369. if (hc.action != ENGINE_DEAD)
  370. stuck |= intel_engine_flag(engine);
  371. }
  372. busy_count += busy;
  373. }
  374. if (hung)
  375. hangcheck_declare_hang(dev_priv, hung, stuck);
  376. /* Reset timer in case GPU hangs without another request being added */
  377. if (busy_count)
  378. i915_queue_hangcheck(dev_priv);
  379. }
  380. void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
  381. {
  382. memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
  383. }
  384. void intel_hangcheck_init(struct drm_i915_private *i915)
  385. {
  386. INIT_DELAYED_WORK(&i915->gpu_error.hangcheck_work,
  387. i915_hangcheck_elapsed);
  388. }
  389. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  390. #include "selftests/intel_hangcheck.c"
  391. #endif