intel_hangcheck.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 <linux/kthread.h>
  25. #include "../i915_selftest.h"
  26. #include "mock_context.h"
  27. #include "mock_drm.h"
  28. struct hang {
  29. struct drm_i915_private *i915;
  30. struct drm_i915_gem_object *hws;
  31. struct drm_i915_gem_object *obj;
  32. u32 *seqno;
  33. u32 *batch;
  34. };
  35. static int hang_init(struct hang *h, struct drm_i915_private *i915)
  36. {
  37. void *vaddr;
  38. int err;
  39. memset(h, 0, sizeof(*h));
  40. h->i915 = i915;
  41. h->hws = i915_gem_object_create_internal(i915, PAGE_SIZE);
  42. if (IS_ERR(h->hws))
  43. return PTR_ERR(h->hws);
  44. h->obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  45. if (IS_ERR(h->obj)) {
  46. err = PTR_ERR(h->obj);
  47. goto err_hws;
  48. }
  49. i915_gem_object_set_cache_level(h->hws, I915_CACHE_LLC);
  50. vaddr = i915_gem_object_pin_map(h->hws, I915_MAP_WB);
  51. if (IS_ERR(vaddr)) {
  52. err = PTR_ERR(vaddr);
  53. goto err_obj;
  54. }
  55. h->seqno = memset(vaddr, 0xff, PAGE_SIZE);
  56. vaddr = i915_gem_object_pin_map(h->obj,
  57. HAS_LLC(i915) ? I915_MAP_WB : I915_MAP_WC);
  58. if (IS_ERR(vaddr)) {
  59. err = PTR_ERR(vaddr);
  60. goto err_unpin_hws;
  61. }
  62. h->batch = vaddr;
  63. return 0;
  64. err_unpin_hws:
  65. i915_gem_object_unpin_map(h->hws);
  66. err_obj:
  67. i915_gem_object_put(h->obj);
  68. err_hws:
  69. i915_gem_object_put(h->hws);
  70. return err;
  71. }
  72. static u64 hws_address(const struct i915_vma *hws,
  73. const struct drm_i915_gem_request *rq)
  74. {
  75. return hws->node.start + offset_in_page(sizeof(u32)*rq->fence.context);
  76. }
  77. static int emit_recurse_batch(struct hang *h,
  78. struct drm_i915_gem_request *rq)
  79. {
  80. struct drm_i915_private *i915 = h->i915;
  81. struct i915_address_space *vm = rq->ctx->ppgtt ? &rq->ctx->ppgtt->base : &i915->ggtt.base;
  82. struct i915_vma *hws, *vma;
  83. unsigned int flags;
  84. u32 *batch;
  85. int err;
  86. vma = i915_vma_instance(h->obj, vm, NULL);
  87. if (IS_ERR(vma))
  88. return PTR_ERR(vma);
  89. hws = i915_vma_instance(h->hws, vm, NULL);
  90. if (IS_ERR(hws))
  91. return PTR_ERR(hws);
  92. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  93. if (err)
  94. return err;
  95. err = i915_vma_pin(hws, 0, 0, PIN_USER);
  96. if (err)
  97. goto unpin_vma;
  98. err = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
  99. if (err)
  100. goto unpin_hws;
  101. err = i915_switch_context(rq);
  102. if (err)
  103. goto unpin_hws;
  104. i915_vma_move_to_active(vma, rq, 0);
  105. if (!i915_gem_object_has_active_reference(vma->obj)) {
  106. i915_gem_object_get(vma->obj);
  107. i915_gem_object_set_active_reference(vma->obj);
  108. }
  109. i915_vma_move_to_active(hws, rq, 0);
  110. if (!i915_gem_object_has_active_reference(hws->obj)) {
  111. i915_gem_object_get(hws->obj);
  112. i915_gem_object_set_active_reference(hws->obj);
  113. }
  114. batch = h->batch;
  115. if (INTEL_GEN(i915) >= 8) {
  116. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  117. *batch++ = lower_32_bits(hws_address(hws, rq));
  118. *batch++ = upper_32_bits(hws_address(hws, rq));
  119. *batch++ = rq->fence.seqno;
  120. *batch++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
  121. *batch++ = lower_32_bits(vma->node.start);
  122. *batch++ = upper_32_bits(vma->node.start);
  123. } else if (INTEL_GEN(i915) >= 6) {
  124. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  125. *batch++ = 0;
  126. *batch++ = lower_32_bits(hws_address(hws, rq));
  127. *batch++ = rq->fence.seqno;
  128. *batch++ = MI_BATCH_BUFFER_START | 1 << 8;
  129. *batch++ = lower_32_bits(vma->node.start);
  130. } else if (INTEL_GEN(i915) >= 4) {
  131. *batch++ = MI_STORE_DWORD_IMM_GEN4 | 1 << 22;
  132. *batch++ = 0;
  133. *batch++ = lower_32_bits(hws_address(hws, rq));
  134. *batch++ = rq->fence.seqno;
  135. *batch++ = MI_BATCH_BUFFER_START | 2 << 6;
  136. *batch++ = lower_32_bits(vma->node.start);
  137. } else {
  138. *batch++ = MI_STORE_DWORD_IMM;
  139. *batch++ = lower_32_bits(hws_address(hws, rq));
  140. *batch++ = rq->fence.seqno;
  141. *batch++ = MI_BATCH_BUFFER_START | 2 << 6 | 1;
  142. *batch++ = lower_32_bits(vma->node.start);
  143. }
  144. *batch++ = MI_BATCH_BUFFER_END; /* not reached */
  145. flags = 0;
  146. if (INTEL_GEN(vm->i915) <= 5)
  147. flags |= I915_DISPATCH_SECURE;
  148. err = rq->engine->emit_bb_start(rq, vma->node.start, PAGE_SIZE, flags);
  149. unpin_hws:
  150. i915_vma_unpin(hws);
  151. unpin_vma:
  152. i915_vma_unpin(vma);
  153. return err;
  154. }
  155. static struct drm_i915_gem_request *
  156. hang_create_request(struct hang *h,
  157. struct intel_engine_cs *engine,
  158. struct i915_gem_context *ctx)
  159. {
  160. struct drm_i915_gem_request *rq;
  161. int err;
  162. if (i915_gem_object_is_active(h->obj)) {
  163. struct drm_i915_gem_object *obj;
  164. void *vaddr;
  165. obj = i915_gem_object_create_internal(h->i915, PAGE_SIZE);
  166. if (IS_ERR(obj))
  167. return ERR_CAST(obj);
  168. vaddr = i915_gem_object_pin_map(obj,
  169. HAS_LLC(h->i915) ? I915_MAP_WB : I915_MAP_WC);
  170. if (IS_ERR(vaddr)) {
  171. i915_gem_object_put(obj);
  172. return ERR_CAST(vaddr);
  173. }
  174. i915_gem_object_unpin_map(h->obj);
  175. i915_gem_object_put(h->obj);
  176. h->obj = obj;
  177. h->batch = vaddr;
  178. }
  179. rq = i915_gem_request_alloc(engine, ctx);
  180. if (IS_ERR(rq))
  181. return rq;
  182. err = emit_recurse_batch(h, rq);
  183. if (err) {
  184. __i915_add_request(rq, false);
  185. return ERR_PTR(err);
  186. }
  187. return rq;
  188. }
  189. static u32 hws_seqno(const struct hang *h,
  190. const struct drm_i915_gem_request *rq)
  191. {
  192. return READ_ONCE(h->seqno[rq->fence.context % (PAGE_SIZE/sizeof(u32))]);
  193. }
  194. static void hang_fini(struct hang *h)
  195. {
  196. *h->batch = MI_BATCH_BUFFER_END;
  197. wmb();
  198. i915_gem_object_unpin_map(h->obj);
  199. i915_gem_object_put(h->obj);
  200. i915_gem_object_unpin_map(h->hws);
  201. i915_gem_object_put(h->hws);
  202. i915_gem_wait_for_idle(h->i915, I915_WAIT_LOCKED);
  203. }
  204. static int igt_hang_sanitycheck(void *arg)
  205. {
  206. struct drm_i915_private *i915 = arg;
  207. struct drm_i915_gem_request *rq;
  208. struct intel_engine_cs *engine;
  209. enum intel_engine_id id;
  210. struct hang h;
  211. int err;
  212. /* Basic check that we can execute our hanging batch */
  213. if (!igt_can_mi_store_dword_imm(i915))
  214. return 0;
  215. mutex_lock(&i915->drm.struct_mutex);
  216. err = hang_init(&h, i915);
  217. if (err)
  218. goto unlock;
  219. for_each_engine(engine, i915, id) {
  220. long timeout;
  221. rq = hang_create_request(&h, engine, i915->kernel_context);
  222. if (IS_ERR(rq)) {
  223. err = PTR_ERR(rq);
  224. pr_err("Failed to create request for %s, err=%d\n",
  225. engine->name, err);
  226. goto fini;
  227. }
  228. i915_gem_request_get(rq);
  229. *h.batch = MI_BATCH_BUFFER_END;
  230. __i915_add_request(rq, true);
  231. timeout = i915_wait_request(rq,
  232. I915_WAIT_LOCKED,
  233. MAX_SCHEDULE_TIMEOUT);
  234. i915_gem_request_put(rq);
  235. if (timeout < 0) {
  236. err = timeout;
  237. pr_err("Wait for request failed on %s, err=%d\n",
  238. engine->name, err);
  239. goto fini;
  240. }
  241. }
  242. fini:
  243. hang_fini(&h);
  244. unlock:
  245. mutex_unlock(&i915->drm.struct_mutex);
  246. return err;
  247. }
  248. static void global_reset_lock(struct drm_i915_private *i915)
  249. {
  250. struct intel_engine_cs *engine;
  251. enum intel_engine_id id;
  252. while (test_and_set_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags))
  253. wait_event(i915->gpu_error.reset_queue,
  254. !test_bit(I915_RESET_BACKOFF,
  255. &i915->gpu_error.flags));
  256. for_each_engine(engine, i915, id) {
  257. while (test_and_set_bit(I915_RESET_ENGINE + id,
  258. &i915->gpu_error.flags))
  259. wait_on_bit(&i915->gpu_error.flags,
  260. I915_RESET_ENGINE + id,
  261. TASK_UNINTERRUPTIBLE);
  262. }
  263. }
  264. static void global_reset_unlock(struct drm_i915_private *i915)
  265. {
  266. struct intel_engine_cs *engine;
  267. enum intel_engine_id id;
  268. for_each_engine(engine, i915, id)
  269. clear_bit(I915_RESET_ENGINE + id, &i915->gpu_error.flags);
  270. clear_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
  271. wake_up_all(&i915->gpu_error.reset_queue);
  272. }
  273. static int igt_global_reset(void *arg)
  274. {
  275. struct drm_i915_private *i915 = arg;
  276. unsigned int reset_count;
  277. int err = 0;
  278. /* Check that we can issue a global GPU reset */
  279. global_reset_lock(i915);
  280. set_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags);
  281. mutex_lock(&i915->drm.struct_mutex);
  282. reset_count = i915_reset_count(&i915->gpu_error);
  283. i915_reset(i915, I915_RESET_QUIET);
  284. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  285. pr_err("No GPU reset recorded!\n");
  286. err = -EINVAL;
  287. }
  288. mutex_unlock(&i915->drm.struct_mutex);
  289. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags));
  290. global_reset_unlock(i915);
  291. if (i915_terminally_wedged(&i915->gpu_error))
  292. err = -EIO;
  293. return err;
  294. }
  295. static int igt_reset_engine(void *arg)
  296. {
  297. struct drm_i915_private *i915 = arg;
  298. struct intel_engine_cs *engine;
  299. enum intel_engine_id id;
  300. unsigned int reset_count, reset_engine_count;
  301. int err = 0;
  302. /* Check that we can issue a global GPU and engine reset */
  303. if (!intel_has_reset_engine(i915))
  304. return 0;
  305. for_each_engine(engine, i915, id) {
  306. set_bit(I915_RESET_ENGINE + engine->id, &i915->gpu_error.flags);
  307. reset_count = i915_reset_count(&i915->gpu_error);
  308. reset_engine_count = i915_reset_engine_count(&i915->gpu_error,
  309. engine);
  310. err = i915_reset_engine(engine, I915_RESET_QUIET);
  311. if (err) {
  312. pr_err("i915_reset_engine failed\n");
  313. break;
  314. }
  315. if (i915_reset_count(&i915->gpu_error) != reset_count) {
  316. pr_err("Full GPU reset recorded! (engine reset expected)\n");
  317. err = -EINVAL;
  318. break;
  319. }
  320. if (i915_reset_engine_count(&i915->gpu_error, engine) ==
  321. reset_engine_count) {
  322. pr_err("No %s engine reset recorded!\n", engine->name);
  323. err = -EINVAL;
  324. break;
  325. }
  326. clear_bit(I915_RESET_ENGINE + engine->id,
  327. &i915->gpu_error.flags);
  328. }
  329. if (i915_terminally_wedged(&i915->gpu_error))
  330. err = -EIO;
  331. return err;
  332. }
  333. static int active_engine(void *data)
  334. {
  335. struct intel_engine_cs *engine = data;
  336. struct drm_i915_gem_request *rq[2] = {};
  337. struct i915_gem_context *ctx[2];
  338. struct drm_file *file;
  339. unsigned long count = 0;
  340. int err = 0;
  341. file = mock_file(engine->i915);
  342. if (IS_ERR(file))
  343. return PTR_ERR(file);
  344. mutex_lock(&engine->i915->drm.struct_mutex);
  345. ctx[0] = live_context(engine->i915, file);
  346. mutex_unlock(&engine->i915->drm.struct_mutex);
  347. if (IS_ERR(ctx[0])) {
  348. err = PTR_ERR(ctx[0]);
  349. goto err_file;
  350. }
  351. mutex_lock(&engine->i915->drm.struct_mutex);
  352. ctx[1] = live_context(engine->i915, file);
  353. mutex_unlock(&engine->i915->drm.struct_mutex);
  354. if (IS_ERR(ctx[1])) {
  355. err = PTR_ERR(ctx[1]);
  356. i915_gem_context_put(ctx[0]);
  357. goto err_file;
  358. }
  359. while (!kthread_should_stop()) {
  360. unsigned int idx = count++ & 1;
  361. struct drm_i915_gem_request *old = rq[idx];
  362. struct drm_i915_gem_request *new;
  363. mutex_lock(&engine->i915->drm.struct_mutex);
  364. new = i915_gem_request_alloc(engine, ctx[idx]);
  365. if (IS_ERR(new)) {
  366. mutex_unlock(&engine->i915->drm.struct_mutex);
  367. err = PTR_ERR(new);
  368. break;
  369. }
  370. rq[idx] = i915_gem_request_get(new);
  371. i915_add_request(new);
  372. mutex_unlock(&engine->i915->drm.struct_mutex);
  373. if (old) {
  374. i915_wait_request(old, 0, MAX_SCHEDULE_TIMEOUT);
  375. i915_gem_request_put(old);
  376. }
  377. }
  378. for (count = 0; count < ARRAY_SIZE(rq); count++)
  379. i915_gem_request_put(rq[count]);
  380. err_file:
  381. mock_file_free(engine->i915, file);
  382. return err;
  383. }
  384. static int igt_reset_active_engines(void *arg)
  385. {
  386. struct drm_i915_private *i915 = arg;
  387. struct intel_engine_cs *engine, *active;
  388. enum intel_engine_id id, tmp;
  389. int err = 0;
  390. /* Check that issuing a reset on one engine does not interfere
  391. * with any other engine.
  392. */
  393. if (!intel_has_reset_engine(i915))
  394. return 0;
  395. for_each_engine(engine, i915, id) {
  396. struct task_struct *threads[I915_NUM_ENGINES];
  397. unsigned long resets[I915_NUM_ENGINES];
  398. unsigned long global = i915_reset_count(&i915->gpu_error);
  399. IGT_TIMEOUT(end_time);
  400. memset(threads, 0, sizeof(threads));
  401. for_each_engine(active, i915, tmp) {
  402. struct task_struct *tsk;
  403. if (active == engine)
  404. continue;
  405. resets[tmp] = i915_reset_engine_count(&i915->gpu_error,
  406. active);
  407. tsk = kthread_run(active_engine, active,
  408. "igt/%s", active->name);
  409. if (IS_ERR(tsk)) {
  410. err = PTR_ERR(tsk);
  411. goto unwind;
  412. }
  413. threads[tmp] = tsk;
  414. get_task_struct(tsk);
  415. }
  416. set_bit(I915_RESET_ENGINE + engine->id, &i915->gpu_error.flags);
  417. do {
  418. err = i915_reset_engine(engine, I915_RESET_QUIET);
  419. if (err) {
  420. pr_err("i915_reset_engine(%s) failed, err=%d\n",
  421. engine->name, err);
  422. break;
  423. }
  424. } while (time_before(jiffies, end_time));
  425. clear_bit(I915_RESET_ENGINE + engine->id,
  426. &i915->gpu_error.flags);
  427. unwind:
  428. for_each_engine(active, i915, tmp) {
  429. int ret;
  430. if (!threads[tmp])
  431. continue;
  432. ret = kthread_stop(threads[tmp]);
  433. if (ret) {
  434. pr_err("kthread for active engine %s failed, err=%d\n",
  435. active->name, ret);
  436. if (!err)
  437. err = ret;
  438. }
  439. put_task_struct(threads[tmp]);
  440. if (resets[tmp] != i915_reset_engine_count(&i915->gpu_error,
  441. active)) {
  442. pr_err("Innocent engine %s was reset (count=%ld)\n",
  443. active->name,
  444. i915_reset_engine_count(&i915->gpu_error,
  445. active) - resets[tmp]);
  446. err = -EIO;
  447. }
  448. }
  449. if (global != i915_reset_count(&i915->gpu_error)) {
  450. pr_err("Global reset (count=%ld)!\n",
  451. i915_reset_count(&i915->gpu_error) - global);
  452. err = -EIO;
  453. }
  454. if (err)
  455. break;
  456. cond_resched();
  457. }
  458. if (i915_terminally_wedged(&i915->gpu_error))
  459. err = -EIO;
  460. return err;
  461. }
  462. static u32 fake_hangcheck(struct drm_i915_gem_request *rq)
  463. {
  464. u32 reset_count;
  465. rq->engine->hangcheck.stalled = true;
  466. rq->engine->hangcheck.seqno = intel_engine_get_seqno(rq->engine);
  467. reset_count = i915_reset_count(&rq->i915->gpu_error);
  468. set_bit(I915_RESET_HANDOFF, &rq->i915->gpu_error.flags);
  469. wake_up_all(&rq->i915->gpu_error.wait_queue);
  470. return reset_count;
  471. }
  472. static bool wait_for_hang(struct hang *h, struct drm_i915_gem_request *rq)
  473. {
  474. return !(wait_for_us(i915_seqno_passed(hws_seqno(h, rq),
  475. rq->fence.seqno),
  476. 10) &&
  477. wait_for(i915_seqno_passed(hws_seqno(h, rq),
  478. rq->fence.seqno),
  479. 1000));
  480. }
  481. static int igt_wait_reset(void *arg)
  482. {
  483. struct drm_i915_private *i915 = arg;
  484. struct drm_i915_gem_request *rq;
  485. unsigned int reset_count;
  486. struct hang h;
  487. long timeout;
  488. int err;
  489. /* Check that we detect a stuck waiter and issue a reset */
  490. global_reset_lock(i915);
  491. mutex_lock(&i915->drm.struct_mutex);
  492. err = hang_init(&h, i915);
  493. if (err)
  494. goto unlock;
  495. rq = hang_create_request(&h, i915->engine[RCS], i915->kernel_context);
  496. if (IS_ERR(rq)) {
  497. err = PTR_ERR(rq);
  498. goto fini;
  499. }
  500. i915_gem_request_get(rq);
  501. __i915_add_request(rq, true);
  502. if (!wait_for_hang(&h, rq)) {
  503. pr_err("Failed to start request %x\n", rq->fence.seqno);
  504. err = -EIO;
  505. goto out_rq;
  506. }
  507. reset_count = fake_hangcheck(rq);
  508. timeout = i915_wait_request(rq, I915_WAIT_LOCKED, 10);
  509. if (timeout < 0) {
  510. pr_err("i915_wait_request failed on a stuck request: err=%ld\n",
  511. timeout);
  512. err = timeout;
  513. goto out_rq;
  514. }
  515. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags));
  516. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  517. pr_err("No GPU reset recorded!\n");
  518. err = -EINVAL;
  519. goto out_rq;
  520. }
  521. out_rq:
  522. i915_gem_request_put(rq);
  523. fini:
  524. hang_fini(&h);
  525. unlock:
  526. mutex_unlock(&i915->drm.struct_mutex);
  527. global_reset_unlock(i915);
  528. if (i915_terminally_wedged(&i915->gpu_error))
  529. return -EIO;
  530. return err;
  531. }
  532. static int igt_reset_queue(void *arg)
  533. {
  534. struct drm_i915_private *i915 = arg;
  535. struct intel_engine_cs *engine;
  536. enum intel_engine_id id;
  537. struct hang h;
  538. int err;
  539. /* Check that we replay pending requests following a hang */
  540. if (!igt_can_mi_store_dword_imm(i915))
  541. return 0;
  542. global_reset_lock(i915);
  543. mutex_lock(&i915->drm.struct_mutex);
  544. err = hang_init(&h, i915);
  545. if (err)
  546. goto unlock;
  547. for_each_engine(engine, i915, id) {
  548. struct drm_i915_gem_request *prev;
  549. IGT_TIMEOUT(end_time);
  550. unsigned int count;
  551. prev = hang_create_request(&h, engine, i915->kernel_context);
  552. if (IS_ERR(prev)) {
  553. err = PTR_ERR(prev);
  554. goto fini;
  555. }
  556. i915_gem_request_get(prev);
  557. __i915_add_request(prev, true);
  558. count = 0;
  559. do {
  560. struct drm_i915_gem_request *rq;
  561. unsigned int reset_count;
  562. rq = hang_create_request(&h,
  563. engine,
  564. i915->kernel_context);
  565. if (IS_ERR(rq)) {
  566. err = PTR_ERR(rq);
  567. goto fini;
  568. }
  569. i915_gem_request_get(rq);
  570. __i915_add_request(rq, true);
  571. if (!wait_for_hang(&h, prev)) {
  572. pr_err("Failed to start request %x\n",
  573. prev->fence.seqno);
  574. i915_gem_request_put(rq);
  575. i915_gem_request_put(prev);
  576. err = -EIO;
  577. goto fini;
  578. }
  579. reset_count = fake_hangcheck(prev);
  580. i915_reset(i915, I915_RESET_QUIET);
  581. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF,
  582. &i915->gpu_error.flags));
  583. if (prev->fence.error != -EIO) {
  584. pr_err("GPU reset not recorded on hanging request [fence.error=%d]!\n",
  585. prev->fence.error);
  586. i915_gem_request_put(rq);
  587. i915_gem_request_put(prev);
  588. err = -EINVAL;
  589. goto fini;
  590. }
  591. if (rq->fence.error) {
  592. pr_err("Fence error status not zero [%d] after unrelated reset\n",
  593. rq->fence.error);
  594. i915_gem_request_put(rq);
  595. i915_gem_request_put(prev);
  596. err = -EINVAL;
  597. goto fini;
  598. }
  599. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  600. pr_err("No GPU reset recorded!\n");
  601. i915_gem_request_put(rq);
  602. i915_gem_request_put(prev);
  603. err = -EINVAL;
  604. goto fini;
  605. }
  606. i915_gem_request_put(prev);
  607. prev = rq;
  608. count++;
  609. } while (time_before(jiffies, end_time));
  610. pr_info("%s: Completed %d resets\n", engine->name, count);
  611. *h.batch = MI_BATCH_BUFFER_END;
  612. wmb();
  613. i915_gem_request_put(prev);
  614. }
  615. fini:
  616. hang_fini(&h);
  617. unlock:
  618. mutex_unlock(&i915->drm.struct_mutex);
  619. global_reset_unlock(i915);
  620. if (i915_terminally_wedged(&i915->gpu_error))
  621. return -EIO;
  622. return err;
  623. }
  624. static int igt_handle_error(void *arg)
  625. {
  626. struct drm_i915_private *i915 = arg;
  627. struct intel_engine_cs *engine = i915->engine[RCS];
  628. struct hang h;
  629. struct drm_i915_gem_request *rq;
  630. struct i915_gpu_state *error;
  631. int err;
  632. /* Check that we can issue a global GPU and engine reset */
  633. if (!intel_has_reset_engine(i915))
  634. return 0;
  635. mutex_lock(&i915->drm.struct_mutex);
  636. err = hang_init(&h, i915);
  637. if (err)
  638. goto err_unlock;
  639. rq = hang_create_request(&h, engine, i915->kernel_context);
  640. if (IS_ERR(rq)) {
  641. err = PTR_ERR(rq);
  642. goto err_fini;
  643. }
  644. i915_gem_request_get(rq);
  645. __i915_add_request(rq, true);
  646. if (!wait_for_hang(&h, rq)) {
  647. pr_err("Failed to start request %x\n", rq->fence.seqno);
  648. err = -EIO;
  649. goto err_request;
  650. }
  651. mutex_unlock(&i915->drm.struct_mutex);
  652. /* Temporarily disable error capture */
  653. error = xchg(&i915->gpu_error.first_error, (void *)-1);
  654. engine->hangcheck.stalled = true;
  655. engine->hangcheck.seqno = intel_engine_get_seqno(engine);
  656. i915_handle_error(i915, intel_engine_flag(engine), "%s", __func__);
  657. xchg(&i915->gpu_error.first_error, error);
  658. mutex_lock(&i915->drm.struct_mutex);
  659. if (rq->fence.error != -EIO) {
  660. pr_err("Guilty request not identified!\n");
  661. err = -EINVAL;
  662. goto err_request;
  663. }
  664. err_request:
  665. i915_gem_request_put(rq);
  666. err_fini:
  667. hang_fini(&h);
  668. err_unlock:
  669. mutex_unlock(&i915->drm.struct_mutex);
  670. return err;
  671. }
  672. int intel_hangcheck_live_selftests(struct drm_i915_private *i915)
  673. {
  674. static const struct i915_subtest tests[] = {
  675. SUBTEST(igt_hang_sanitycheck),
  676. SUBTEST(igt_global_reset),
  677. SUBTEST(igt_reset_engine),
  678. SUBTEST(igt_reset_active_engines),
  679. SUBTEST(igt_wait_reset),
  680. SUBTEST(igt_reset_queue),
  681. SUBTEST(igt_handle_error),
  682. };
  683. if (!intel_has_gpu_reset(i915))
  684. return 0;
  685. return i915_subtests(tests, i915);
  686. }