intel_hangcheck.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_selftest.h"
  25. struct hang {
  26. struct drm_i915_private *i915;
  27. struct drm_i915_gem_object *hws;
  28. struct drm_i915_gem_object *obj;
  29. u32 *seqno;
  30. u32 *batch;
  31. };
  32. static int hang_init(struct hang *h, struct drm_i915_private *i915)
  33. {
  34. void *vaddr;
  35. int err;
  36. memset(h, 0, sizeof(*h));
  37. h->i915 = i915;
  38. h->hws = i915_gem_object_create_internal(i915, PAGE_SIZE);
  39. if (IS_ERR(h->hws))
  40. return PTR_ERR(h->hws);
  41. h->obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  42. if (IS_ERR(h->obj)) {
  43. err = PTR_ERR(h->obj);
  44. goto err_hws;
  45. }
  46. i915_gem_object_set_cache_level(h->hws, I915_CACHE_LLC);
  47. vaddr = i915_gem_object_pin_map(h->hws, I915_MAP_WB);
  48. if (IS_ERR(vaddr)) {
  49. err = PTR_ERR(vaddr);
  50. goto err_obj;
  51. }
  52. h->seqno = memset(vaddr, 0xff, PAGE_SIZE);
  53. vaddr = i915_gem_object_pin_map(h->obj,
  54. HAS_LLC(i915) ? I915_MAP_WB : I915_MAP_WC);
  55. if (IS_ERR(vaddr)) {
  56. err = PTR_ERR(vaddr);
  57. goto err_unpin_hws;
  58. }
  59. h->batch = vaddr;
  60. return 0;
  61. err_unpin_hws:
  62. i915_gem_object_unpin_map(h->hws);
  63. err_obj:
  64. i915_gem_object_put(h->obj);
  65. err_hws:
  66. i915_gem_object_put(h->hws);
  67. return err;
  68. }
  69. static u64 hws_address(const struct i915_vma *hws,
  70. const struct drm_i915_gem_request *rq)
  71. {
  72. return hws->node.start + offset_in_page(sizeof(u32)*rq->fence.context);
  73. }
  74. static int emit_recurse_batch(struct hang *h,
  75. struct drm_i915_gem_request *rq)
  76. {
  77. struct drm_i915_private *i915 = h->i915;
  78. struct i915_address_space *vm = rq->ctx->ppgtt ? &rq->ctx->ppgtt->base : &i915->ggtt.base;
  79. struct i915_vma *hws, *vma;
  80. unsigned int flags;
  81. u32 *batch;
  82. int err;
  83. vma = i915_vma_instance(h->obj, vm, NULL);
  84. if (IS_ERR(vma))
  85. return PTR_ERR(vma);
  86. hws = i915_vma_instance(h->hws, vm, NULL);
  87. if (IS_ERR(hws))
  88. return PTR_ERR(hws);
  89. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  90. if (err)
  91. return err;
  92. err = i915_vma_pin(hws, 0, 0, PIN_USER);
  93. if (err)
  94. goto unpin_vma;
  95. err = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
  96. if (err)
  97. goto unpin_hws;
  98. err = i915_switch_context(rq);
  99. if (err)
  100. goto unpin_hws;
  101. i915_vma_move_to_active(vma, rq, 0);
  102. if (!i915_gem_object_has_active_reference(vma->obj)) {
  103. i915_gem_object_get(vma->obj);
  104. i915_gem_object_set_active_reference(vma->obj);
  105. }
  106. i915_vma_move_to_active(hws, rq, 0);
  107. if (!i915_gem_object_has_active_reference(hws->obj)) {
  108. i915_gem_object_get(hws->obj);
  109. i915_gem_object_set_active_reference(hws->obj);
  110. }
  111. batch = h->batch;
  112. if (INTEL_GEN(i915) >= 8) {
  113. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  114. *batch++ = lower_32_bits(hws_address(hws, rq));
  115. *batch++ = upper_32_bits(hws_address(hws, rq));
  116. *batch++ = rq->fence.seqno;
  117. *batch++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
  118. *batch++ = lower_32_bits(vma->node.start);
  119. *batch++ = upper_32_bits(vma->node.start);
  120. } else if (INTEL_GEN(i915) >= 6) {
  121. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  122. *batch++ = 0;
  123. *batch++ = lower_32_bits(hws_address(hws, rq));
  124. *batch++ = rq->fence.seqno;
  125. *batch++ = MI_BATCH_BUFFER_START | 1 << 8;
  126. *batch++ = lower_32_bits(vma->node.start);
  127. } else if (INTEL_GEN(i915) >= 4) {
  128. *batch++ = MI_STORE_DWORD_IMM_GEN4 | 1 << 22;
  129. *batch++ = 0;
  130. *batch++ = lower_32_bits(hws_address(hws, rq));
  131. *batch++ = rq->fence.seqno;
  132. *batch++ = MI_BATCH_BUFFER_START | 2 << 6;
  133. *batch++ = lower_32_bits(vma->node.start);
  134. } else {
  135. *batch++ = MI_STORE_DWORD_IMM;
  136. *batch++ = lower_32_bits(hws_address(hws, rq));
  137. *batch++ = rq->fence.seqno;
  138. *batch++ = MI_BATCH_BUFFER_START | 2 << 6 | 1;
  139. *batch++ = lower_32_bits(vma->node.start);
  140. }
  141. *batch++ = MI_BATCH_BUFFER_END; /* not reached */
  142. flags = 0;
  143. if (INTEL_GEN(vm->i915) <= 5)
  144. flags |= I915_DISPATCH_SECURE;
  145. err = rq->engine->emit_bb_start(rq, vma->node.start, PAGE_SIZE, flags);
  146. unpin_hws:
  147. i915_vma_unpin(hws);
  148. unpin_vma:
  149. i915_vma_unpin(vma);
  150. return err;
  151. }
  152. static struct drm_i915_gem_request *
  153. hang_create_request(struct hang *h,
  154. struct intel_engine_cs *engine,
  155. struct i915_gem_context *ctx)
  156. {
  157. struct drm_i915_gem_request *rq;
  158. int err;
  159. if (i915_gem_object_is_active(h->obj)) {
  160. struct drm_i915_gem_object *obj;
  161. void *vaddr;
  162. obj = i915_gem_object_create_internal(h->i915, PAGE_SIZE);
  163. if (IS_ERR(obj))
  164. return ERR_CAST(obj);
  165. vaddr = i915_gem_object_pin_map(obj,
  166. HAS_LLC(h->i915) ? I915_MAP_WB : I915_MAP_WC);
  167. if (IS_ERR(vaddr)) {
  168. i915_gem_object_put(obj);
  169. return ERR_CAST(vaddr);
  170. }
  171. i915_gem_object_unpin_map(h->obj);
  172. i915_gem_object_put(h->obj);
  173. h->obj = obj;
  174. h->batch = vaddr;
  175. }
  176. rq = i915_gem_request_alloc(engine, ctx);
  177. if (IS_ERR(rq))
  178. return rq;
  179. err = emit_recurse_batch(h, rq);
  180. if (err) {
  181. __i915_add_request(rq, false);
  182. return ERR_PTR(err);
  183. }
  184. return rq;
  185. }
  186. static u32 hws_seqno(const struct hang *h,
  187. const struct drm_i915_gem_request *rq)
  188. {
  189. return READ_ONCE(h->seqno[rq->fence.context % (PAGE_SIZE/sizeof(u32))]);
  190. }
  191. static void hang_fini(struct hang *h)
  192. {
  193. *h->batch = MI_BATCH_BUFFER_END;
  194. wmb();
  195. i915_gem_object_unpin_map(h->obj);
  196. i915_gem_object_put(h->obj);
  197. i915_gem_object_unpin_map(h->hws);
  198. i915_gem_object_put(h->hws);
  199. i915_gem_wait_for_idle(h->i915, I915_WAIT_LOCKED);
  200. }
  201. static int igt_hang_sanitycheck(void *arg)
  202. {
  203. struct drm_i915_private *i915 = arg;
  204. struct drm_i915_gem_request *rq;
  205. struct intel_engine_cs *engine;
  206. enum intel_engine_id id;
  207. struct hang h;
  208. int err;
  209. /* Basic check that we can execute our hanging batch */
  210. if (!igt_can_mi_store_dword_imm(i915))
  211. return 0;
  212. mutex_lock(&i915->drm.struct_mutex);
  213. err = hang_init(&h, i915);
  214. if (err)
  215. goto unlock;
  216. for_each_engine(engine, i915, id) {
  217. long timeout;
  218. rq = hang_create_request(&h, engine, i915->kernel_context);
  219. if (IS_ERR(rq)) {
  220. err = PTR_ERR(rq);
  221. pr_err("Failed to create request for %s, err=%d\n",
  222. engine->name, err);
  223. goto fini;
  224. }
  225. i915_gem_request_get(rq);
  226. *h.batch = MI_BATCH_BUFFER_END;
  227. __i915_add_request(rq, true);
  228. timeout = i915_wait_request(rq,
  229. I915_WAIT_LOCKED,
  230. MAX_SCHEDULE_TIMEOUT);
  231. i915_gem_request_put(rq);
  232. if (timeout < 0) {
  233. err = timeout;
  234. pr_err("Wait for request failed on %s, err=%d\n",
  235. engine->name, err);
  236. goto fini;
  237. }
  238. }
  239. fini:
  240. hang_fini(&h);
  241. unlock:
  242. mutex_unlock(&i915->drm.struct_mutex);
  243. return err;
  244. }
  245. static int igt_global_reset(void *arg)
  246. {
  247. struct drm_i915_private *i915 = arg;
  248. unsigned int reset_count;
  249. int err = 0;
  250. /* Check that we can issue a global GPU reset */
  251. set_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
  252. set_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags);
  253. mutex_lock(&i915->drm.struct_mutex);
  254. reset_count = i915_reset_count(&i915->gpu_error);
  255. i915_reset(i915);
  256. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  257. pr_err("No GPU reset recorded!\n");
  258. err = -EINVAL;
  259. }
  260. mutex_unlock(&i915->drm.struct_mutex);
  261. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags));
  262. clear_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
  263. if (i915_terminally_wedged(&i915->gpu_error))
  264. err = -EIO;
  265. return err;
  266. }
  267. static u32 fake_hangcheck(struct drm_i915_gem_request *rq)
  268. {
  269. u32 reset_count;
  270. rq->engine->hangcheck.stalled = true;
  271. rq->engine->hangcheck.seqno = intel_engine_get_seqno(rq->engine);
  272. reset_count = i915_reset_count(&rq->i915->gpu_error);
  273. set_bit(I915_RESET_HANDOFF, &rq->i915->gpu_error.flags);
  274. wake_up_all(&rq->i915->gpu_error.wait_queue);
  275. return reset_count;
  276. }
  277. static bool wait_for_hang(struct hang *h, struct drm_i915_gem_request *rq)
  278. {
  279. return !(wait_for_us(i915_seqno_passed(hws_seqno(h, rq),
  280. rq->fence.seqno),
  281. 10) &&
  282. wait_for(i915_seqno_passed(hws_seqno(h, rq),
  283. rq->fence.seqno),
  284. 1000));
  285. }
  286. static int igt_wait_reset(void *arg)
  287. {
  288. struct drm_i915_private *i915 = arg;
  289. struct drm_i915_gem_request *rq;
  290. unsigned int reset_count;
  291. struct hang h;
  292. long timeout;
  293. int err;
  294. /* Check that we detect a stuck waiter and issue a reset */
  295. set_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
  296. mutex_lock(&i915->drm.struct_mutex);
  297. err = hang_init(&h, i915);
  298. if (err)
  299. goto unlock;
  300. rq = hang_create_request(&h, i915->engine[RCS], i915->kernel_context);
  301. if (IS_ERR(rq)) {
  302. err = PTR_ERR(rq);
  303. goto fini;
  304. }
  305. i915_gem_request_get(rq);
  306. __i915_add_request(rq, true);
  307. if (!wait_for_hang(&h, rq)) {
  308. pr_err("Failed to start request %x\n", rq->fence.seqno);
  309. err = -EIO;
  310. goto out_rq;
  311. }
  312. reset_count = fake_hangcheck(rq);
  313. timeout = i915_wait_request(rq, I915_WAIT_LOCKED, 10);
  314. if (timeout < 0) {
  315. pr_err("i915_wait_request failed on a stuck request: err=%ld\n",
  316. timeout);
  317. err = timeout;
  318. goto out_rq;
  319. }
  320. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags));
  321. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  322. pr_err("No GPU reset recorded!\n");
  323. err = -EINVAL;
  324. goto out_rq;
  325. }
  326. out_rq:
  327. i915_gem_request_put(rq);
  328. fini:
  329. hang_fini(&h);
  330. unlock:
  331. mutex_unlock(&i915->drm.struct_mutex);
  332. clear_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
  333. if (i915_terminally_wedged(&i915->gpu_error))
  334. return -EIO;
  335. return err;
  336. }
  337. static int igt_reset_queue(void *arg)
  338. {
  339. struct drm_i915_private *i915 = arg;
  340. struct intel_engine_cs *engine;
  341. enum intel_engine_id id;
  342. struct hang h;
  343. int err;
  344. /* Check that we replay pending requests following a hang */
  345. if (!igt_can_mi_store_dword_imm(i915))
  346. return 0;
  347. set_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
  348. mutex_lock(&i915->drm.struct_mutex);
  349. err = hang_init(&h, i915);
  350. if (err)
  351. goto unlock;
  352. for_each_engine(engine, i915, id) {
  353. struct drm_i915_gem_request *prev;
  354. IGT_TIMEOUT(end_time);
  355. unsigned int count;
  356. prev = hang_create_request(&h, engine, i915->kernel_context);
  357. if (IS_ERR(prev)) {
  358. err = PTR_ERR(prev);
  359. goto fini;
  360. }
  361. i915_gem_request_get(prev);
  362. __i915_add_request(prev, true);
  363. count = 0;
  364. do {
  365. struct drm_i915_gem_request *rq;
  366. unsigned int reset_count;
  367. rq = hang_create_request(&h,
  368. engine,
  369. i915->kernel_context);
  370. if (IS_ERR(rq)) {
  371. err = PTR_ERR(rq);
  372. goto fini;
  373. }
  374. i915_gem_request_get(rq);
  375. __i915_add_request(rq, true);
  376. if (!wait_for_hang(&h, prev)) {
  377. pr_err("Failed to start request %x\n",
  378. prev->fence.seqno);
  379. i915_gem_request_put(rq);
  380. i915_gem_request_put(prev);
  381. err = -EIO;
  382. goto fini;
  383. }
  384. reset_count = fake_hangcheck(prev);
  385. i915_reset(i915);
  386. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF,
  387. &i915->gpu_error.flags));
  388. if (prev->fence.error != -EIO) {
  389. pr_err("GPU reset not recorded on hanging request [fence.error=%d]!\n",
  390. prev->fence.error);
  391. i915_gem_request_put(rq);
  392. i915_gem_request_put(prev);
  393. err = -EINVAL;
  394. goto fini;
  395. }
  396. if (rq->fence.error) {
  397. pr_err("Fence error status not zero [%d] after unrelated reset\n",
  398. rq->fence.error);
  399. i915_gem_request_put(rq);
  400. i915_gem_request_put(prev);
  401. err = -EINVAL;
  402. goto fini;
  403. }
  404. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  405. pr_err("No GPU reset recorded!\n");
  406. i915_gem_request_put(rq);
  407. i915_gem_request_put(prev);
  408. err = -EINVAL;
  409. goto fini;
  410. }
  411. i915_gem_request_put(prev);
  412. prev = rq;
  413. count++;
  414. } while (time_before(jiffies, end_time));
  415. pr_info("%s: Completed %d resets\n", engine->name, count);
  416. *h.batch = MI_BATCH_BUFFER_END;
  417. wmb();
  418. i915_gem_request_put(prev);
  419. }
  420. fini:
  421. hang_fini(&h);
  422. unlock:
  423. mutex_unlock(&i915->drm.struct_mutex);
  424. clear_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
  425. if (i915_terminally_wedged(&i915->gpu_error))
  426. return -EIO;
  427. return err;
  428. }
  429. int intel_hangcheck_live_selftests(struct drm_i915_private *i915)
  430. {
  431. static const struct i915_subtest tests[] = {
  432. SUBTEST(igt_hang_sanitycheck),
  433. SUBTEST(igt_global_reset),
  434. SUBTEST(igt_wait_reset),
  435. SUBTEST(igt_reset_queue),
  436. };
  437. if (!intel_has_gpu_reset(i915))
  438. return 0;
  439. return i915_subtests(tests, i915);
  440. }