intel_hangcheck.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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 "i915_random.h"
  27. #include "igt_flush_test.h"
  28. #include "igt_wedge_me.h"
  29. #include "mock_context.h"
  30. #include "mock_drm.h"
  31. #define IGT_IDLE_TIMEOUT 50 /* ms; time to wait after flushing between tests */
  32. struct hang {
  33. struct drm_i915_private *i915;
  34. struct drm_i915_gem_object *hws;
  35. struct drm_i915_gem_object *obj;
  36. struct i915_gem_context *ctx;
  37. u32 *seqno;
  38. u32 *batch;
  39. };
  40. static int hang_init(struct hang *h, struct drm_i915_private *i915)
  41. {
  42. void *vaddr;
  43. int err;
  44. memset(h, 0, sizeof(*h));
  45. h->i915 = i915;
  46. h->ctx = kernel_context(i915);
  47. if (IS_ERR(h->ctx))
  48. return PTR_ERR(h->ctx);
  49. h->hws = i915_gem_object_create_internal(i915, PAGE_SIZE);
  50. if (IS_ERR(h->hws)) {
  51. err = PTR_ERR(h->hws);
  52. goto err_ctx;
  53. }
  54. h->obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  55. if (IS_ERR(h->obj)) {
  56. err = PTR_ERR(h->obj);
  57. goto err_hws;
  58. }
  59. i915_gem_object_set_cache_level(h->hws, I915_CACHE_LLC);
  60. vaddr = i915_gem_object_pin_map(h->hws, I915_MAP_WB);
  61. if (IS_ERR(vaddr)) {
  62. err = PTR_ERR(vaddr);
  63. goto err_obj;
  64. }
  65. h->seqno = memset(vaddr, 0xff, PAGE_SIZE);
  66. vaddr = i915_gem_object_pin_map(h->obj,
  67. HAS_LLC(i915) ? I915_MAP_WB : I915_MAP_WC);
  68. if (IS_ERR(vaddr)) {
  69. err = PTR_ERR(vaddr);
  70. goto err_unpin_hws;
  71. }
  72. h->batch = vaddr;
  73. return 0;
  74. err_unpin_hws:
  75. i915_gem_object_unpin_map(h->hws);
  76. err_obj:
  77. i915_gem_object_put(h->obj);
  78. err_hws:
  79. i915_gem_object_put(h->hws);
  80. err_ctx:
  81. kernel_context_close(h->ctx);
  82. return err;
  83. }
  84. static u64 hws_address(const struct i915_vma *hws,
  85. const struct i915_request *rq)
  86. {
  87. return hws->node.start + offset_in_page(sizeof(u32)*rq->fence.context);
  88. }
  89. static int emit_recurse_batch(struct hang *h,
  90. struct i915_request *rq)
  91. {
  92. struct drm_i915_private *i915 = h->i915;
  93. struct i915_address_space *vm =
  94. rq->gem_context->ppgtt ?
  95. &rq->gem_context->ppgtt->vm :
  96. &i915->ggtt.vm;
  97. struct i915_vma *hws, *vma;
  98. unsigned int flags;
  99. u32 *batch;
  100. int err;
  101. vma = i915_vma_instance(h->obj, vm, NULL);
  102. if (IS_ERR(vma))
  103. return PTR_ERR(vma);
  104. hws = i915_vma_instance(h->hws, vm, NULL);
  105. if (IS_ERR(hws))
  106. return PTR_ERR(hws);
  107. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  108. if (err)
  109. return err;
  110. err = i915_vma_pin(hws, 0, 0, PIN_USER);
  111. if (err)
  112. goto unpin_vma;
  113. err = i915_vma_move_to_active(vma, rq, 0);
  114. if (err)
  115. goto unpin_hws;
  116. if (!i915_gem_object_has_active_reference(vma->obj)) {
  117. i915_gem_object_get(vma->obj);
  118. i915_gem_object_set_active_reference(vma->obj);
  119. }
  120. err = i915_vma_move_to_active(hws, rq, 0);
  121. if (err)
  122. goto unpin_hws;
  123. if (!i915_gem_object_has_active_reference(hws->obj)) {
  124. i915_gem_object_get(hws->obj);
  125. i915_gem_object_set_active_reference(hws->obj);
  126. }
  127. batch = h->batch;
  128. if (INTEL_GEN(i915) >= 8) {
  129. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  130. *batch++ = lower_32_bits(hws_address(hws, rq));
  131. *batch++ = upper_32_bits(hws_address(hws, rq));
  132. *batch++ = rq->fence.seqno;
  133. *batch++ = MI_ARB_CHECK;
  134. memset(batch, 0, 1024);
  135. batch += 1024 / sizeof(*batch);
  136. *batch++ = MI_ARB_CHECK;
  137. *batch++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
  138. *batch++ = lower_32_bits(vma->node.start);
  139. *batch++ = upper_32_bits(vma->node.start);
  140. } else if (INTEL_GEN(i915) >= 6) {
  141. *batch++ = MI_STORE_DWORD_IMM_GEN4;
  142. *batch++ = 0;
  143. *batch++ = lower_32_bits(hws_address(hws, rq));
  144. *batch++ = rq->fence.seqno;
  145. *batch++ = MI_ARB_CHECK;
  146. memset(batch, 0, 1024);
  147. batch += 1024 / sizeof(*batch);
  148. *batch++ = MI_ARB_CHECK;
  149. *batch++ = MI_BATCH_BUFFER_START | 1 << 8;
  150. *batch++ = lower_32_bits(vma->node.start);
  151. } else if (INTEL_GEN(i915) >= 4) {
  152. *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
  153. *batch++ = 0;
  154. *batch++ = lower_32_bits(hws_address(hws, rq));
  155. *batch++ = rq->fence.seqno;
  156. *batch++ = MI_ARB_CHECK;
  157. memset(batch, 0, 1024);
  158. batch += 1024 / sizeof(*batch);
  159. *batch++ = MI_ARB_CHECK;
  160. *batch++ = MI_BATCH_BUFFER_START | 2 << 6;
  161. *batch++ = lower_32_bits(vma->node.start);
  162. } else {
  163. *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
  164. *batch++ = lower_32_bits(hws_address(hws, rq));
  165. *batch++ = rq->fence.seqno;
  166. *batch++ = MI_ARB_CHECK;
  167. memset(batch, 0, 1024);
  168. batch += 1024 / sizeof(*batch);
  169. *batch++ = MI_ARB_CHECK;
  170. *batch++ = MI_BATCH_BUFFER_START | 2 << 6;
  171. *batch++ = lower_32_bits(vma->node.start);
  172. }
  173. *batch++ = MI_BATCH_BUFFER_END; /* not reached */
  174. i915_gem_chipset_flush(h->i915);
  175. flags = 0;
  176. if (INTEL_GEN(vm->i915) <= 5)
  177. flags |= I915_DISPATCH_SECURE;
  178. err = rq->engine->emit_bb_start(rq, vma->node.start, PAGE_SIZE, flags);
  179. unpin_hws:
  180. i915_vma_unpin(hws);
  181. unpin_vma:
  182. i915_vma_unpin(vma);
  183. return err;
  184. }
  185. static struct i915_request *
  186. hang_create_request(struct hang *h, struct intel_engine_cs *engine)
  187. {
  188. struct i915_request *rq;
  189. int err;
  190. if (i915_gem_object_is_active(h->obj)) {
  191. struct drm_i915_gem_object *obj;
  192. void *vaddr;
  193. obj = i915_gem_object_create_internal(h->i915, PAGE_SIZE);
  194. if (IS_ERR(obj))
  195. return ERR_CAST(obj);
  196. vaddr = i915_gem_object_pin_map(obj,
  197. HAS_LLC(h->i915) ? I915_MAP_WB : I915_MAP_WC);
  198. if (IS_ERR(vaddr)) {
  199. i915_gem_object_put(obj);
  200. return ERR_CAST(vaddr);
  201. }
  202. i915_gem_object_unpin_map(h->obj);
  203. i915_gem_object_put(h->obj);
  204. h->obj = obj;
  205. h->batch = vaddr;
  206. }
  207. rq = i915_request_alloc(engine, h->ctx);
  208. if (IS_ERR(rq))
  209. return rq;
  210. err = emit_recurse_batch(h, rq);
  211. if (err) {
  212. i915_request_add(rq);
  213. return ERR_PTR(err);
  214. }
  215. return rq;
  216. }
  217. static u32 hws_seqno(const struct hang *h, const struct i915_request *rq)
  218. {
  219. return READ_ONCE(h->seqno[rq->fence.context % (PAGE_SIZE/sizeof(u32))]);
  220. }
  221. static void hang_fini(struct hang *h)
  222. {
  223. *h->batch = MI_BATCH_BUFFER_END;
  224. i915_gem_chipset_flush(h->i915);
  225. i915_gem_object_unpin_map(h->obj);
  226. i915_gem_object_put(h->obj);
  227. i915_gem_object_unpin_map(h->hws);
  228. i915_gem_object_put(h->hws);
  229. kernel_context_close(h->ctx);
  230. igt_flush_test(h->i915, I915_WAIT_LOCKED);
  231. }
  232. static bool wait_until_running(struct hang *h, struct i915_request *rq)
  233. {
  234. return !(wait_for_us(i915_seqno_passed(hws_seqno(h, rq),
  235. rq->fence.seqno),
  236. 10) &&
  237. wait_for(i915_seqno_passed(hws_seqno(h, rq),
  238. rq->fence.seqno),
  239. 1000));
  240. }
  241. static int igt_hang_sanitycheck(void *arg)
  242. {
  243. struct drm_i915_private *i915 = arg;
  244. struct i915_request *rq;
  245. struct intel_engine_cs *engine;
  246. enum intel_engine_id id;
  247. struct hang h;
  248. int err;
  249. /* Basic check that we can execute our hanging batch */
  250. mutex_lock(&i915->drm.struct_mutex);
  251. err = hang_init(&h, i915);
  252. if (err)
  253. goto unlock;
  254. for_each_engine(engine, i915, id) {
  255. long timeout;
  256. if (!intel_engine_can_store_dword(engine))
  257. continue;
  258. rq = hang_create_request(&h, engine);
  259. if (IS_ERR(rq)) {
  260. err = PTR_ERR(rq);
  261. pr_err("Failed to create request for %s, err=%d\n",
  262. engine->name, err);
  263. goto fini;
  264. }
  265. i915_request_get(rq);
  266. *h.batch = MI_BATCH_BUFFER_END;
  267. i915_gem_chipset_flush(i915);
  268. i915_request_add(rq);
  269. timeout = i915_request_wait(rq,
  270. I915_WAIT_LOCKED,
  271. MAX_SCHEDULE_TIMEOUT);
  272. i915_request_put(rq);
  273. if (timeout < 0) {
  274. err = timeout;
  275. pr_err("Wait for request failed on %s, err=%d\n",
  276. engine->name, err);
  277. goto fini;
  278. }
  279. }
  280. fini:
  281. hang_fini(&h);
  282. unlock:
  283. mutex_unlock(&i915->drm.struct_mutex);
  284. return err;
  285. }
  286. static void global_reset_lock(struct drm_i915_private *i915)
  287. {
  288. struct intel_engine_cs *engine;
  289. enum intel_engine_id id;
  290. pr_debug("%s: current gpu_error=%08lx\n",
  291. __func__, i915->gpu_error.flags);
  292. while (test_and_set_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags))
  293. wait_event(i915->gpu_error.reset_queue,
  294. !test_bit(I915_RESET_BACKOFF,
  295. &i915->gpu_error.flags));
  296. for_each_engine(engine, i915, id) {
  297. while (test_and_set_bit(I915_RESET_ENGINE + id,
  298. &i915->gpu_error.flags))
  299. wait_on_bit(&i915->gpu_error.flags,
  300. I915_RESET_ENGINE + id,
  301. TASK_UNINTERRUPTIBLE);
  302. }
  303. }
  304. static void global_reset_unlock(struct drm_i915_private *i915)
  305. {
  306. struct intel_engine_cs *engine;
  307. enum intel_engine_id id;
  308. for_each_engine(engine, i915, id)
  309. clear_bit(I915_RESET_ENGINE + id, &i915->gpu_error.flags);
  310. clear_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
  311. wake_up_all(&i915->gpu_error.reset_queue);
  312. }
  313. static int igt_global_reset(void *arg)
  314. {
  315. struct drm_i915_private *i915 = arg;
  316. unsigned int reset_count;
  317. int err = 0;
  318. /* Check that we can issue a global GPU reset */
  319. global_reset_lock(i915);
  320. set_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags);
  321. mutex_lock(&i915->drm.struct_mutex);
  322. reset_count = i915_reset_count(&i915->gpu_error);
  323. i915_reset(i915, ALL_ENGINES, NULL);
  324. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  325. pr_err("No GPU reset recorded!\n");
  326. err = -EINVAL;
  327. }
  328. mutex_unlock(&i915->drm.struct_mutex);
  329. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags));
  330. global_reset_unlock(i915);
  331. if (i915_terminally_wedged(&i915->gpu_error))
  332. err = -EIO;
  333. return err;
  334. }
  335. static bool wait_for_idle(struct intel_engine_cs *engine)
  336. {
  337. return wait_for(intel_engine_is_idle(engine), IGT_IDLE_TIMEOUT) == 0;
  338. }
  339. static int __igt_reset_engine(struct drm_i915_private *i915, bool active)
  340. {
  341. struct intel_engine_cs *engine;
  342. enum intel_engine_id id;
  343. struct hang h;
  344. int err = 0;
  345. /* Check that we can issue an engine reset on an idle engine (no-op) */
  346. if (!intel_has_reset_engine(i915))
  347. return 0;
  348. if (active) {
  349. mutex_lock(&i915->drm.struct_mutex);
  350. err = hang_init(&h, i915);
  351. mutex_unlock(&i915->drm.struct_mutex);
  352. if (err)
  353. return err;
  354. }
  355. for_each_engine(engine, i915, id) {
  356. unsigned int reset_count, reset_engine_count;
  357. IGT_TIMEOUT(end_time);
  358. if (active && !intel_engine_can_store_dword(engine))
  359. continue;
  360. if (!wait_for_idle(engine)) {
  361. pr_err("%s failed to idle before reset\n",
  362. engine->name);
  363. err = -EIO;
  364. break;
  365. }
  366. reset_count = i915_reset_count(&i915->gpu_error);
  367. reset_engine_count = i915_reset_engine_count(&i915->gpu_error,
  368. engine);
  369. set_bit(I915_RESET_ENGINE + id, &i915->gpu_error.flags);
  370. do {
  371. u32 seqno = intel_engine_get_seqno(engine);
  372. if (active) {
  373. struct i915_request *rq;
  374. mutex_lock(&i915->drm.struct_mutex);
  375. rq = hang_create_request(&h, engine);
  376. if (IS_ERR(rq)) {
  377. err = PTR_ERR(rq);
  378. mutex_unlock(&i915->drm.struct_mutex);
  379. break;
  380. }
  381. i915_request_get(rq);
  382. i915_request_add(rq);
  383. mutex_unlock(&i915->drm.struct_mutex);
  384. if (!wait_until_running(&h, rq)) {
  385. struct drm_printer p = drm_info_printer(i915->drm.dev);
  386. pr_err("%s: Failed to start request %x, at %x\n",
  387. __func__, rq->fence.seqno, hws_seqno(&h, rq));
  388. intel_engine_dump(engine, &p,
  389. "%s\n", engine->name);
  390. i915_request_put(rq);
  391. err = -EIO;
  392. break;
  393. }
  394. GEM_BUG_ON(!rq->global_seqno);
  395. seqno = rq->global_seqno - 1;
  396. i915_request_put(rq);
  397. }
  398. err = i915_reset_engine(engine, NULL);
  399. if (err) {
  400. pr_err("i915_reset_engine failed\n");
  401. break;
  402. }
  403. if (i915_reset_count(&i915->gpu_error) != reset_count) {
  404. pr_err("Full GPU reset recorded! (engine reset expected)\n");
  405. err = -EINVAL;
  406. break;
  407. }
  408. reset_engine_count += active;
  409. if (i915_reset_engine_count(&i915->gpu_error, engine) !=
  410. reset_engine_count) {
  411. pr_err("%s engine reset %srecorded!\n",
  412. engine->name, active ? "not " : "");
  413. err = -EINVAL;
  414. break;
  415. }
  416. if (!wait_for_idle(engine)) {
  417. struct drm_printer p =
  418. drm_info_printer(i915->drm.dev);
  419. pr_err("%s failed to idle after reset\n",
  420. engine->name);
  421. intel_engine_dump(engine, &p,
  422. "%s\n", engine->name);
  423. err = -EIO;
  424. break;
  425. }
  426. } while (time_before(jiffies, end_time));
  427. clear_bit(I915_RESET_ENGINE + id, &i915->gpu_error.flags);
  428. if (err)
  429. break;
  430. err = igt_flush_test(i915, 0);
  431. if (err)
  432. break;
  433. }
  434. if (i915_terminally_wedged(&i915->gpu_error))
  435. err = -EIO;
  436. if (active) {
  437. mutex_lock(&i915->drm.struct_mutex);
  438. hang_fini(&h);
  439. mutex_unlock(&i915->drm.struct_mutex);
  440. }
  441. return err;
  442. }
  443. static int igt_reset_idle_engine(void *arg)
  444. {
  445. return __igt_reset_engine(arg, false);
  446. }
  447. static int igt_reset_active_engine(void *arg)
  448. {
  449. return __igt_reset_engine(arg, true);
  450. }
  451. struct active_engine {
  452. struct task_struct *task;
  453. struct intel_engine_cs *engine;
  454. unsigned long resets;
  455. unsigned int flags;
  456. };
  457. #define TEST_ACTIVE BIT(0)
  458. #define TEST_OTHERS BIT(1)
  459. #define TEST_SELF BIT(2)
  460. #define TEST_PRIORITY BIT(3)
  461. static int active_request_put(struct i915_request *rq)
  462. {
  463. int err = 0;
  464. if (!rq)
  465. return 0;
  466. if (i915_request_wait(rq, 0, 5 * HZ) < 0) {
  467. GEM_TRACE("%s timed out waiting for completion of fence %llx:%d, seqno %d.\n",
  468. rq->engine->name,
  469. rq->fence.context,
  470. rq->fence.seqno,
  471. i915_request_global_seqno(rq));
  472. GEM_TRACE_DUMP();
  473. i915_gem_set_wedged(rq->i915);
  474. err = -EIO;
  475. }
  476. i915_request_put(rq);
  477. return err;
  478. }
  479. static int active_engine(void *data)
  480. {
  481. I915_RND_STATE(prng);
  482. struct active_engine *arg = data;
  483. struct intel_engine_cs *engine = arg->engine;
  484. struct i915_request *rq[8] = {};
  485. struct i915_gem_context *ctx[ARRAY_SIZE(rq)];
  486. struct drm_file *file;
  487. unsigned long count = 0;
  488. int err = 0;
  489. file = mock_file(engine->i915);
  490. if (IS_ERR(file))
  491. return PTR_ERR(file);
  492. for (count = 0; count < ARRAY_SIZE(ctx); count++) {
  493. mutex_lock(&engine->i915->drm.struct_mutex);
  494. ctx[count] = live_context(engine->i915, file);
  495. mutex_unlock(&engine->i915->drm.struct_mutex);
  496. if (IS_ERR(ctx[count])) {
  497. err = PTR_ERR(ctx[count]);
  498. while (--count)
  499. i915_gem_context_put(ctx[count]);
  500. goto err_file;
  501. }
  502. }
  503. while (!kthread_should_stop()) {
  504. unsigned int idx = count++ & (ARRAY_SIZE(rq) - 1);
  505. struct i915_request *old = rq[idx];
  506. struct i915_request *new;
  507. mutex_lock(&engine->i915->drm.struct_mutex);
  508. new = i915_request_alloc(engine, ctx[idx]);
  509. if (IS_ERR(new)) {
  510. mutex_unlock(&engine->i915->drm.struct_mutex);
  511. err = PTR_ERR(new);
  512. break;
  513. }
  514. if (arg->flags & TEST_PRIORITY)
  515. ctx[idx]->sched.priority =
  516. i915_prandom_u32_max_state(512, &prng);
  517. rq[idx] = i915_request_get(new);
  518. i915_request_add(new);
  519. mutex_unlock(&engine->i915->drm.struct_mutex);
  520. err = active_request_put(old);
  521. if (err)
  522. break;
  523. cond_resched();
  524. }
  525. for (count = 0; count < ARRAY_SIZE(rq); count++) {
  526. int err__ = active_request_put(rq[count]);
  527. /* Keep the first error */
  528. if (!err)
  529. err = err__;
  530. }
  531. err_file:
  532. mock_file_free(engine->i915, file);
  533. return err;
  534. }
  535. static int __igt_reset_engines(struct drm_i915_private *i915,
  536. const char *test_name,
  537. unsigned int flags)
  538. {
  539. struct intel_engine_cs *engine, *other;
  540. enum intel_engine_id id, tmp;
  541. struct hang h;
  542. int err = 0;
  543. /* Check that issuing a reset on one engine does not interfere
  544. * with any other engine.
  545. */
  546. if (!intel_has_reset_engine(i915))
  547. return 0;
  548. if (flags & TEST_ACTIVE) {
  549. mutex_lock(&i915->drm.struct_mutex);
  550. err = hang_init(&h, i915);
  551. mutex_unlock(&i915->drm.struct_mutex);
  552. if (err)
  553. return err;
  554. if (flags & TEST_PRIORITY)
  555. h.ctx->sched.priority = 1024;
  556. }
  557. for_each_engine(engine, i915, id) {
  558. struct active_engine threads[I915_NUM_ENGINES] = {};
  559. unsigned long global = i915_reset_count(&i915->gpu_error);
  560. unsigned long count = 0, reported;
  561. IGT_TIMEOUT(end_time);
  562. if (flags & TEST_ACTIVE &&
  563. !intel_engine_can_store_dword(engine))
  564. continue;
  565. if (!wait_for_idle(engine)) {
  566. pr_err("i915_reset_engine(%s:%s): failed to idle before reset\n",
  567. engine->name, test_name);
  568. err = -EIO;
  569. break;
  570. }
  571. memset(threads, 0, sizeof(threads));
  572. for_each_engine(other, i915, tmp) {
  573. struct task_struct *tsk;
  574. threads[tmp].resets =
  575. i915_reset_engine_count(&i915->gpu_error,
  576. other);
  577. if (!(flags & TEST_OTHERS))
  578. continue;
  579. if (other == engine && !(flags & TEST_SELF))
  580. continue;
  581. threads[tmp].engine = other;
  582. threads[tmp].flags = flags;
  583. tsk = kthread_run(active_engine, &threads[tmp],
  584. "igt/%s", other->name);
  585. if (IS_ERR(tsk)) {
  586. err = PTR_ERR(tsk);
  587. goto unwind;
  588. }
  589. threads[tmp].task = tsk;
  590. get_task_struct(tsk);
  591. }
  592. set_bit(I915_RESET_ENGINE + id, &i915->gpu_error.flags);
  593. do {
  594. u32 seqno = intel_engine_get_seqno(engine);
  595. struct i915_request *rq = NULL;
  596. if (flags & TEST_ACTIVE) {
  597. mutex_lock(&i915->drm.struct_mutex);
  598. rq = hang_create_request(&h, engine);
  599. if (IS_ERR(rq)) {
  600. err = PTR_ERR(rq);
  601. mutex_unlock(&i915->drm.struct_mutex);
  602. break;
  603. }
  604. i915_request_get(rq);
  605. i915_request_add(rq);
  606. mutex_unlock(&i915->drm.struct_mutex);
  607. if (!wait_until_running(&h, rq)) {
  608. struct drm_printer p = drm_info_printer(i915->drm.dev);
  609. pr_err("%s: Failed to start request %x, at %x\n",
  610. __func__, rq->fence.seqno, hws_seqno(&h, rq));
  611. intel_engine_dump(engine, &p,
  612. "%s\n", engine->name);
  613. i915_request_put(rq);
  614. err = -EIO;
  615. break;
  616. }
  617. GEM_BUG_ON(!rq->global_seqno);
  618. seqno = rq->global_seqno - 1;
  619. }
  620. err = i915_reset_engine(engine, NULL);
  621. if (err) {
  622. pr_err("i915_reset_engine(%s:%s): failed, err=%d\n",
  623. engine->name, test_name, err);
  624. break;
  625. }
  626. count++;
  627. if (rq) {
  628. i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT);
  629. i915_request_put(rq);
  630. }
  631. if (!(flags & TEST_SELF) && !wait_for_idle(engine)) {
  632. struct drm_printer p =
  633. drm_info_printer(i915->drm.dev);
  634. pr_err("i915_reset_engine(%s:%s):"
  635. " failed to idle after reset\n",
  636. engine->name, test_name);
  637. intel_engine_dump(engine, &p,
  638. "%s\n", engine->name);
  639. err = -EIO;
  640. break;
  641. }
  642. } while (time_before(jiffies, end_time));
  643. clear_bit(I915_RESET_ENGINE + id, &i915->gpu_error.flags);
  644. pr_info("i915_reset_engine(%s:%s): %lu resets\n",
  645. engine->name, test_name, count);
  646. reported = i915_reset_engine_count(&i915->gpu_error, engine);
  647. reported -= threads[engine->id].resets;
  648. if (reported != (flags & TEST_ACTIVE ? count : 0)) {
  649. pr_err("i915_reset_engine(%s:%s): reset %lu times, but reported %lu, expected %lu reported\n",
  650. engine->name, test_name, count, reported,
  651. (flags & TEST_ACTIVE ? count : 0));
  652. if (!err)
  653. err = -EINVAL;
  654. }
  655. unwind:
  656. for_each_engine(other, i915, tmp) {
  657. int ret;
  658. if (!threads[tmp].task)
  659. continue;
  660. ret = kthread_stop(threads[tmp].task);
  661. if (ret) {
  662. pr_err("kthread for other engine %s failed, err=%d\n",
  663. other->name, ret);
  664. if (!err)
  665. err = ret;
  666. }
  667. put_task_struct(threads[tmp].task);
  668. if (other != engine &&
  669. threads[tmp].resets !=
  670. i915_reset_engine_count(&i915->gpu_error, other)) {
  671. pr_err("Innocent engine %s was reset (count=%ld)\n",
  672. other->name,
  673. i915_reset_engine_count(&i915->gpu_error,
  674. other) -
  675. threads[tmp].resets);
  676. if (!err)
  677. err = -EINVAL;
  678. }
  679. }
  680. if (global != i915_reset_count(&i915->gpu_error)) {
  681. pr_err("Global reset (count=%ld)!\n",
  682. i915_reset_count(&i915->gpu_error) - global);
  683. if (!err)
  684. err = -EINVAL;
  685. }
  686. if (err)
  687. break;
  688. err = igt_flush_test(i915, 0);
  689. if (err)
  690. break;
  691. }
  692. if (i915_terminally_wedged(&i915->gpu_error))
  693. err = -EIO;
  694. if (flags & TEST_ACTIVE) {
  695. mutex_lock(&i915->drm.struct_mutex);
  696. hang_fini(&h);
  697. mutex_unlock(&i915->drm.struct_mutex);
  698. }
  699. return err;
  700. }
  701. static int igt_reset_engines(void *arg)
  702. {
  703. static const struct {
  704. const char *name;
  705. unsigned int flags;
  706. } phases[] = {
  707. { "idle", 0 },
  708. { "active", TEST_ACTIVE },
  709. { "others-idle", TEST_OTHERS },
  710. { "others-active", TEST_OTHERS | TEST_ACTIVE },
  711. {
  712. "others-priority",
  713. TEST_OTHERS | TEST_ACTIVE | TEST_PRIORITY
  714. },
  715. {
  716. "self-priority",
  717. TEST_OTHERS | TEST_ACTIVE | TEST_PRIORITY | TEST_SELF,
  718. },
  719. { }
  720. };
  721. struct drm_i915_private *i915 = arg;
  722. typeof(*phases) *p;
  723. int err;
  724. for (p = phases; p->name; p++) {
  725. if (p->flags & TEST_PRIORITY) {
  726. if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_PRIORITY))
  727. continue;
  728. }
  729. err = __igt_reset_engines(arg, p->name, p->flags);
  730. if (err)
  731. return err;
  732. }
  733. return 0;
  734. }
  735. static u32 fake_hangcheck(struct i915_request *rq, u32 mask)
  736. {
  737. struct i915_gpu_error *error = &rq->i915->gpu_error;
  738. u32 reset_count = i915_reset_count(error);
  739. error->stalled_mask = mask;
  740. /* set_bit() must be after we have setup the backchannel (mask) */
  741. smp_mb__before_atomic();
  742. set_bit(I915_RESET_HANDOFF, &error->flags);
  743. wake_up_all(&error->wait_queue);
  744. return reset_count;
  745. }
  746. static int igt_reset_wait(void *arg)
  747. {
  748. struct drm_i915_private *i915 = arg;
  749. struct i915_request *rq;
  750. unsigned int reset_count;
  751. struct hang h;
  752. long timeout;
  753. int err;
  754. if (!intel_engine_can_store_dword(i915->engine[RCS]))
  755. return 0;
  756. /* Check that we detect a stuck waiter and issue a reset */
  757. global_reset_lock(i915);
  758. mutex_lock(&i915->drm.struct_mutex);
  759. err = hang_init(&h, i915);
  760. if (err)
  761. goto unlock;
  762. rq = hang_create_request(&h, i915->engine[RCS]);
  763. if (IS_ERR(rq)) {
  764. err = PTR_ERR(rq);
  765. goto fini;
  766. }
  767. i915_request_get(rq);
  768. i915_request_add(rq);
  769. if (!wait_until_running(&h, rq)) {
  770. struct drm_printer p = drm_info_printer(i915->drm.dev);
  771. pr_err("%s: Failed to start request %x, at %x\n",
  772. __func__, rq->fence.seqno, hws_seqno(&h, rq));
  773. intel_engine_dump(rq->engine, &p, "%s\n", rq->engine->name);
  774. i915_gem_set_wedged(i915);
  775. err = -EIO;
  776. goto out_rq;
  777. }
  778. reset_count = fake_hangcheck(rq, ALL_ENGINES);
  779. timeout = i915_request_wait(rq, I915_WAIT_LOCKED, 10);
  780. if (timeout < 0) {
  781. pr_err("i915_request_wait failed on a stuck request: err=%ld\n",
  782. timeout);
  783. err = timeout;
  784. goto out_rq;
  785. }
  786. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF, &i915->gpu_error.flags));
  787. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  788. pr_err("No GPU reset recorded!\n");
  789. err = -EINVAL;
  790. goto out_rq;
  791. }
  792. out_rq:
  793. i915_request_put(rq);
  794. fini:
  795. hang_fini(&h);
  796. unlock:
  797. mutex_unlock(&i915->drm.struct_mutex);
  798. global_reset_unlock(i915);
  799. if (i915_terminally_wedged(&i915->gpu_error))
  800. return -EIO;
  801. return err;
  802. }
  803. struct evict_vma {
  804. struct completion completion;
  805. struct i915_vma *vma;
  806. };
  807. static int evict_vma(void *data)
  808. {
  809. struct evict_vma *arg = data;
  810. struct i915_address_space *vm = arg->vma->vm;
  811. struct drm_i915_private *i915 = vm->i915;
  812. struct drm_mm_node evict = arg->vma->node;
  813. int err;
  814. complete(&arg->completion);
  815. mutex_lock(&i915->drm.struct_mutex);
  816. err = i915_gem_evict_for_node(vm, &evict, 0);
  817. mutex_unlock(&i915->drm.struct_mutex);
  818. return err;
  819. }
  820. static int evict_fence(void *data)
  821. {
  822. struct evict_vma *arg = data;
  823. struct drm_i915_private *i915 = arg->vma->vm->i915;
  824. int err;
  825. complete(&arg->completion);
  826. mutex_lock(&i915->drm.struct_mutex);
  827. /* Mark the fence register as dirty to force the mmio update. */
  828. err = i915_gem_object_set_tiling(arg->vma->obj, I915_TILING_Y, 512);
  829. if (err) {
  830. pr_err("Invalid Y-tiling settings; err:%d\n", err);
  831. goto out_unlock;
  832. }
  833. err = i915_vma_pin_fence(arg->vma);
  834. if (err) {
  835. pr_err("Unable to pin Y-tiled fence; err:%d\n", err);
  836. goto out_unlock;
  837. }
  838. i915_vma_unpin_fence(arg->vma);
  839. out_unlock:
  840. mutex_unlock(&i915->drm.struct_mutex);
  841. return err;
  842. }
  843. static int __igt_reset_evict_vma(struct drm_i915_private *i915,
  844. struct i915_address_space *vm,
  845. int (*fn)(void *),
  846. unsigned int flags)
  847. {
  848. struct drm_i915_gem_object *obj;
  849. struct task_struct *tsk = NULL;
  850. struct i915_request *rq;
  851. struct evict_vma arg;
  852. struct hang h;
  853. int err;
  854. if (!intel_engine_can_store_dword(i915->engine[RCS]))
  855. return 0;
  856. /* Check that we can recover an unbind stuck on a hanging request */
  857. global_reset_lock(i915);
  858. mutex_lock(&i915->drm.struct_mutex);
  859. err = hang_init(&h, i915);
  860. if (err)
  861. goto unlock;
  862. obj = i915_gem_object_create_internal(i915, SZ_1M);
  863. if (IS_ERR(obj)) {
  864. err = PTR_ERR(obj);
  865. goto fini;
  866. }
  867. if (flags & EXEC_OBJECT_NEEDS_FENCE) {
  868. err = i915_gem_object_set_tiling(obj, I915_TILING_X, 512);
  869. if (err) {
  870. pr_err("Invalid X-tiling settings; err:%d\n", err);
  871. goto out_obj;
  872. }
  873. }
  874. arg.vma = i915_vma_instance(obj, vm, NULL);
  875. if (IS_ERR(arg.vma)) {
  876. err = PTR_ERR(arg.vma);
  877. goto out_obj;
  878. }
  879. rq = hang_create_request(&h, i915->engine[RCS]);
  880. if (IS_ERR(rq)) {
  881. err = PTR_ERR(rq);
  882. goto out_obj;
  883. }
  884. err = i915_vma_pin(arg.vma, 0, 0,
  885. i915_vma_is_ggtt(arg.vma) ?
  886. PIN_GLOBAL | PIN_MAPPABLE :
  887. PIN_USER);
  888. if (err) {
  889. i915_request_add(rq);
  890. goto out_obj;
  891. }
  892. if (flags & EXEC_OBJECT_NEEDS_FENCE) {
  893. err = i915_vma_pin_fence(arg.vma);
  894. if (err) {
  895. pr_err("Unable to pin X-tiled fence; err:%d\n", err);
  896. i915_vma_unpin(arg.vma);
  897. i915_request_add(rq);
  898. goto out_obj;
  899. }
  900. }
  901. err = i915_vma_move_to_active(arg.vma, rq, flags);
  902. if (flags & EXEC_OBJECT_NEEDS_FENCE)
  903. i915_vma_unpin_fence(arg.vma);
  904. i915_vma_unpin(arg.vma);
  905. i915_request_get(rq);
  906. i915_request_add(rq);
  907. if (err)
  908. goto out_rq;
  909. mutex_unlock(&i915->drm.struct_mutex);
  910. if (!wait_until_running(&h, rq)) {
  911. struct drm_printer p = drm_info_printer(i915->drm.dev);
  912. pr_err("%s: Failed to start request %x, at %x\n",
  913. __func__, rq->fence.seqno, hws_seqno(&h, rq));
  914. intel_engine_dump(rq->engine, &p, "%s\n", rq->engine->name);
  915. i915_gem_set_wedged(i915);
  916. goto out_reset;
  917. }
  918. init_completion(&arg.completion);
  919. tsk = kthread_run(fn, &arg, "igt/evict_vma");
  920. if (IS_ERR(tsk)) {
  921. err = PTR_ERR(tsk);
  922. tsk = NULL;
  923. goto out_reset;
  924. }
  925. wait_for_completion(&arg.completion);
  926. if (wait_for(waitqueue_active(&rq->execute), 10)) {
  927. struct drm_printer p = drm_info_printer(i915->drm.dev);
  928. pr_err("igt/evict_vma kthread did not wait\n");
  929. intel_engine_dump(rq->engine, &p, "%s\n", rq->engine->name);
  930. i915_gem_set_wedged(i915);
  931. goto out_reset;
  932. }
  933. out_reset:
  934. fake_hangcheck(rq, intel_engine_flag(rq->engine));
  935. if (tsk) {
  936. struct igt_wedge_me w;
  937. /* The reset, even indirectly, should take less than 10ms. */
  938. igt_wedge_on_timeout(&w, i915, HZ / 10 /* 100ms timeout*/)
  939. err = kthread_stop(tsk);
  940. }
  941. mutex_lock(&i915->drm.struct_mutex);
  942. out_rq:
  943. i915_request_put(rq);
  944. out_obj:
  945. i915_gem_object_put(obj);
  946. fini:
  947. hang_fini(&h);
  948. unlock:
  949. mutex_unlock(&i915->drm.struct_mutex);
  950. global_reset_unlock(i915);
  951. if (i915_terminally_wedged(&i915->gpu_error))
  952. return -EIO;
  953. return err;
  954. }
  955. static int igt_reset_evict_ggtt(void *arg)
  956. {
  957. struct drm_i915_private *i915 = arg;
  958. return __igt_reset_evict_vma(i915, &i915->ggtt.vm,
  959. evict_vma, EXEC_OBJECT_WRITE);
  960. }
  961. static int igt_reset_evict_ppgtt(void *arg)
  962. {
  963. struct drm_i915_private *i915 = arg;
  964. struct i915_gem_context *ctx;
  965. struct drm_file *file;
  966. int err;
  967. file = mock_file(i915);
  968. if (IS_ERR(file))
  969. return PTR_ERR(file);
  970. mutex_lock(&i915->drm.struct_mutex);
  971. ctx = live_context(i915, file);
  972. mutex_unlock(&i915->drm.struct_mutex);
  973. if (IS_ERR(ctx)) {
  974. err = PTR_ERR(ctx);
  975. goto out;
  976. }
  977. err = 0;
  978. if (ctx->ppgtt) /* aliasing == global gtt locking, covered above */
  979. err = __igt_reset_evict_vma(i915, &ctx->ppgtt->vm,
  980. evict_vma, EXEC_OBJECT_WRITE);
  981. out:
  982. mock_file_free(i915, file);
  983. return err;
  984. }
  985. static int igt_reset_evict_fence(void *arg)
  986. {
  987. struct drm_i915_private *i915 = arg;
  988. return __igt_reset_evict_vma(i915, &i915->ggtt.vm,
  989. evict_fence, EXEC_OBJECT_NEEDS_FENCE);
  990. }
  991. static int wait_for_others(struct drm_i915_private *i915,
  992. struct intel_engine_cs *exclude)
  993. {
  994. struct intel_engine_cs *engine;
  995. enum intel_engine_id id;
  996. for_each_engine(engine, i915, id) {
  997. if (engine == exclude)
  998. continue;
  999. if (!wait_for_idle(engine))
  1000. return -EIO;
  1001. }
  1002. return 0;
  1003. }
  1004. static int igt_reset_queue(void *arg)
  1005. {
  1006. struct drm_i915_private *i915 = arg;
  1007. struct intel_engine_cs *engine;
  1008. enum intel_engine_id id;
  1009. struct hang h;
  1010. int err;
  1011. /* Check that we replay pending requests following a hang */
  1012. global_reset_lock(i915);
  1013. mutex_lock(&i915->drm.struct_mutex);
  1014. err = hang_init(&h, i915);
  1015. if (err)
  1016. goto unlock;
  1017. for_each_engine(engine, i915, id) {
  1018. struct i915_request *prev;
  1019. IGT_TIMEOUT(end_time);
  1020. unsigned int count;
  1021. if (!intel_engine_can_store_dword(engine))
  1022. continue;
  1023. prev = hang_create_request(&h, engine);
  1024. if (IS_ERR(prev)) {
  1025. err = PTR_ERR(prev);
  1026. goto fini;
  1027. }
  1028. i915_request_get(prev);
  1029. i915_request_add(prev);
  1030. count = 0;
  1031. do {
  1032. struct i915_request *rq;
  1033. unsigned int reset_count;
  1034. rq = hang_create_request(&h, engine);
  1035. if (IS_ERR(rq)) {
  1036. err = PTR_ERR(rq);
  1037. goto fini;
  1038. }
  1039. i915_request_get(rq);
  1040. i915_request_add(rq);
  1041. /*
  1042. * XXX We don't handle resetting the kernel context
  1043. * very well. If we trigger a device reset twice in
  1044. * quick succession while the kernel context is
  1045. * executing, we may end up skipping the breadcrumb.
  1046. * This is really only a problem for the selftest as
  1047. * normally there is a large interlude between resets
  1048. * (hangcheck), or we focus on resetting just one
  1049. * engine and so avoid repeatedly resetting innocents.
  1050. */
  1051. err = wait_for_others(i915, engine);
  1052. if (err) {
  1053. pr_err("%s(%s): Failed to idle other inactive engines after device reset\n",
  1054. __func__, engine->name);
  1055. i915_request_put(rq);
  1056. i915_request_put(prev);
  1057. GEM_TRACE_DUMP();
  1058. i915_gem_set_wedged(i915);
  1059. goto fini;
  1060. }
  1061. if (!wait_until_running(&h, prev)) {
  1062. struct drm_printer p = drm_info_printer(i915->drm.dev);
  1063. pr_err("%s(%s): Failed to start request %x, at %x\n",
  1064. __func__, engine->name,
  1065. prev->fence.seqno, hws_seqno(&h, prev));
  1066. intel_engine_dump(engine, &p,
  1067. "%s\n", engine->name);
  1068. i915_request_put(rq);
  1069. i915_request_put(prev);
  1070. i915_gem_set_wedged(i915);
  1071. err = -EIO;
  1072. goto fini;
  1073. }
  1074. reset_count = fake_hangcheck(prev, ENGINE_MASK(id));
  1075. i915_reset(i915, ENGINE_MASK(id), NULL);
  1076. GEM_BUG_ON(test_bit(I915_RESET_HANDOFF,
  1077. &i915->gpu_error.flags));
  1078. if (prev->fence.error != -EIO) {
  1079. pr_err("GPU reset not recorded on hanging request [fence.error=%d]!\n",
  1080. prev->fence.error);
  1081. i915_request_put(rq);
  1082. i915_request_put(prev);
  1083. err = -EINVAL;
  1084. goto fini;
  1085. }
  1086. if (rq->fence.error) {
  1087. pr_err("Fence error status not zero [%d] after unrelated reset\n",
  1088. rq->fence.error);
  1089. i915_request_put(rq);
  1090. i915_request_put(prev);
  1091. err = -EINVAL;
  1092. goto fini;
  1093. }
  1094. if (i915_reset_count(&i915->gpu_error) == reset_count) {
  1095. pr_err("No GPU reset recorded!\n");
  1096. i915_request_put(rq);
  1097. i915_request_put(prev);
  1098. err = -EINVAL;
  1099. goto fini;
  1100. }
  1101. i915_request_put(prev);
  1102. prev = rq;
  1103. count++;
  1104. } while (time_before(jiffies, end_time));
  1105. pr_info("%s: Completed %d resets\n", engine->name, count);
  1106. *h.batch = MI_BATCH_BUFFER_END;
  1107. i915_gem_chipset_flush(i915);
  1108. i915_request_put(prev);
  1109. err = igt_flush_test(i915, I915_WAIT_LOCKED);
  1110. if (err)
  1111. break;
  1112. }
  1113. fini:
  1114. hang_fini(&h);
  1115. unlock:
  1116. mutex_unlock(&i915->drm.struct_mutex);
  1117. global_reset_unlock(i915);
  1118. if (i915_terminally_wedged(&i915->gpu_error))
  1119. return -EIO;
  1120. return err;
  1121. }
  1122. static int igt_handle_error(void *arg)
  1123. {
  1124. struct drm_i915_private *i915 = arg;
  1125. struct intel_engine_cs *engine = i915->engine[RCS];
  1126. struct hang h;
  1127. struct i915_request *rq;
  1128. struct i915_gpu_state *error;
  1129. int err;
  1130. /* Check that we can issue a global GPU and engine reset */
  1131. if (!intel_has_reset_engine(i915))
  1132. return 0;
  1133. if (!engine || !intel_engine_can_store_dword(engine))
  1134. return 0;
  1135. mutex_lock(&i915->drm.struct_mutex);
  1136. err = hang_init(&h, i915);
  1137. if (err)
  1138. goto err_unlock;
  1139. rq = hang_create_request(&h, engine);
  1140. if (IS_ERR(rq)) {
  1141. err = PTR_ERR(rq);
  1142. goto err_fini;
  1143. }
  1144. i915_request_get(rq);
  1145. i915_request_add(rq);
  1146. if (!wait_until_running(&h, rq)) {
  1147. struct drm_printer p = drm_info_printer(i915->drm.dev);
  1148. pr_err("%s: Failed to start request %x, at %x\n",
  1149. __func__, rq->fence.seqno, hws_seqno(&h, rq));
  1150. intel_engine_dump(rq->engine, &p, "%s\n", rq->engine->name);
  1151. i915_gem_set_wedged(i915);
  1152. err = -EIO;
  1153. goto err_request;
  1154. }
  1155. mutex_unlock(&i915->drm.struct_mutex);
  1156. /* Temporarily disable error capture */
  1157. error = xchg(&i915->gpu_error.first_error, (void *)-1);
  1158. i915_handle_error(i915, ENGINE_MASK(engine->id), 0, NULL);
  1159. xchg(&i915->gpu_error.first_error, error);
  1160. mutex_lock(&i915->drm.struct_mutex);
  1161. if (rq->fence.error != -EIO) {
  1162. pr_err("Guilty request not identified!\n");
  1163. err = -EINVAL;
  1164. goto err_request;
  1165. }
  1166. err_request:
  1167. i915_request_put(rq);
  1168. err_fini:
  1169. hang_fini(&h);
  1170. err_unlock:
  1171. mutex_unlock(&i915->drm.struct_mutex);
  1172. return err;
  1173. }
  1174. int intel_hangcheck_live_selftests(struct drm_i915_private *i915)
  1175. {
  1176. static const struct i915_subtest tests[] = {
  1177. SUBTEST(igt_global_reset), /* attempt to recover GPU first */
  1178. SUBTEST(igt_hang_sanitycheck),
  1179. SUBTEST(igt_reset_idle_engine),
  1180. SUBTEST(igt_reset_active_engine),
  1181. SUBTEST(igt_reset_engines),
  1182. SUBTEST(igt_reset_queue),
  1183. SUBTEST(igt_reset_wait),
  1184. SUBTEST(igt_reset_evict_ggtt),
  1185. SUBTEST(igt_reset_evict_ppgtt),
  1186. SUBTEST(igt_reset_evict_fence),
  1187. SUBTEST(igt_handle_error),
  1188. };
  1189. bool saved_hangcheck;
  1190. int err;
  1191. if (!intel_has_gpu_reset(i915))
  1192. return 0;
  1193. if (i915_terminally_wedged(&i915->gpu_error))
  1194. return -EIO; /* we're long past hope of a successful reset */
  1195. intel_runtime_pm_get(i915);
  1196. saved_hangcheck = fetch_and_zero(&i915_modparams.enable_hangcheck);
  1197. err = i915_subtests(tests, i915);
  1198. mutex_lock(&i915->drm.struct_mutex);
  1199. igt_flush_test(i915, I915_WAIT_LOCKED);
  1200. mutex_unlock(&i915->drm.struct_mutex);
  1201. i915_modparams.enable_hangcheck = saved_hangcheck;
  1202. intel_runtime_pm_put(i915);
  1203. return err;
  1204. }