i915_gpu_error.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  1. /*
  2. * Copyright (c) 2008 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. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Keith Packard <keithp@keithp.com>
  26. * Mika Kuoppala <mika.kuoppala@intel.com>
  27. *
  28. */
  29. #include <generated/utsrelease.h>
  30. #include <linux/stop_machine.h>
  31. #include <linux/zlib.h>
  32. #include "i915_drv.h"
  33. static const char *engine_str(int engine)
  34. {
  35. switch (engine) {
  36. case RCS: return "render";
  37. case VCS: return "bsd";
  38. case BCS: return "blt";
  39. case VECS: return "vebox";
  40. case VCS2: return "bsd2";
  41. default: return "";
  42. }
  43. }
  44. static const char *tiling_flag(int tiling)
  45. {
  46. switch (tiling) {
  47. default:
  48. case I915_TILING_NONE: return "";
  49. case I915_TILING_X: return " X";
  50. case I915_TILING_Y: return " Y";
  51. }
  52. }
  53. static const char *dirty_flag(int dirty)
  54. {
  55. return dirty ? " dirty" : "";
  56. }
  57. static const char *purgeable_flag(int purgeable)
  58. {
  59. return purgeable ? " purgeable" : "";
  60. }
  61. static bool __i915_error_ok(struct drm_i915_error_state_buf *e)
  62. {
  63. if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) {
  64. e->err = -ENOSPC;
  65. return false;
  66. }
  67. if (e->bytes == e->size - 1 || e->err)
  68. return false;
  69. return true;
  70. }
  71. static bool __i915_error_seek(struct drm_i915_error_state_buf *e,
  72. unsigned len)
  73. {
  74. if (e->pos + len <= e->start) {
  75. e->pos += len;
  76. return false;
  77. }
  78. /* First vsnprintf needs to fit in its entirety for memmove */
  79. if (len >= e->size) {
  80. e->err = -EIO;
  81. return false;
  82. }
  83. return true;
  84. }
  85. static void __i915_error_advance(struct drm_i915_error_state_buf *e,
  86. unsigned len)
  87. {
  88. /* If this is first printf in this window, adjust it so that
  89. * start position matches start of the buffer
  90. */
  91. if (e->pos < e->start) {
  92. const size_t off = e->start - e->pos;
  93. /* Should not happen but be paranoid */
  94. if (off > len || e->bytes) {
  95. e->err = -EIO;
  96. return;
  97. }
  98. memmove(e->buf, e->buf + off, len - off);
  99. e->bytes = len - off;
  100. e->pos = e->start;
  101. return;
  102. }
  103. e->bytes += len;
  104. e->pos += len;
  105. }
  106. static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
  107. const char *f, va_list args)
  108. {
  109. unsigned len;
  110. if (!__i915_error_ok(e))
  111. return;
  112. /* Seek the first printf which is hits start position */
  113. if (e->pos < e->start) {
  114. va_list tmp;
  115. va_copy(tmp, args);
  116. len = vsnprintf(NULL, 0, f, tmp);
  117. va_end(tmp);
  118. if (!__i915_error_seek(e, len))
  119. return;
  120. }
  121. len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args);
  122. if (len >= e->size - e->bytes)
  123. len = e->size - e->bytes - 1;
  124. __i915_error_advance(e, len);
  125. }
  126. static void i915_error_puts(struct drm_i915_error_state_buf *e,
  127. const char *str)
  128. {
  129. unsigned len;
  130. if (!__i915_error_ok(e))
  131. return;
  132. len = strlen(str);
  133. /* Seek the first printf which is hits start position */
  134. if (e->pos < e->start) {
  135. if (!__i915_error_seek(e, len))
  136. return;
  137. }
  138. if (len >= e->size - e->bytes)
  139. len = e->size - e->bytes - 1;
  140. memcpy(e->buf + e->bytes, str, len);
  141. __i915_error_advance(e, len);
  142. }
  143. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  144. #define err_puts(e, s) i915_error_puts(e, s)
  145. #ifdef CONFIG_DRM_I915_COMPRESS_ERROR
  146. static bool compress_init(struct z_stream_s *zstream)
  147. {
  148. memset(zstream, 0, sizeof(*zstream));
  149. zstream->workspace =
  150. kmalloc(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
  151. GFP_ATOMIC | __GFP_NOWARN);
  152. if (!zstream->workspace)
  153. return false;
  154. if (zlib_deflateInit(zstream, Z_DEFAULT_COMPRESSION) != Z_OK) {
  155. kfree(zstream->workspace);
  156. return false;
  157. }
  158. return true;
  159. }
  160. static int compress_page(struct z_stream_s *zstream,
  161. void *src,
  162. struct drm_i915_error_object *dst)
  163. {
  164. zstream->next_in = src;
  165. zstream->avail_in = PAGE_SIZE;
  166. do {
  167. if (zstream->avail_out == 0) {
  168. unsigned long page;
  169. page = __get_free_page(GFP_ATOMIC | __GFP_NOWARN);
  170. if (!page)
  171. return -ENOMEM;
  172. dst->pages[dst->page_count++] = (void *)page;
  173. zstream->next_out = (void *)page;
  174. zstream->avail_out = PAGE_SIZE;
  175. }
  176. if (zlib_deflate(zstream, Z_SYNC_FLUSH) != Z_OK)
  177. return -EIO;
  178. } while (zstream->avail_in);
  179. /* Fallback to uncompressed if we increase size? */
  180. if (0 && zstream->total_out > zstream->total_in)
  181. return -E2BIG;
  182. return 0;
  183. }
  184. static void compress_fini(struct z_stream_s *zstream,
  185. struct drm_i915_error_object *dst)
  186. {
  187. if (dst) {
  188. zlib_deflate(zstream, Z_FINISH);
  189. dst->unused = zstream->avail_out;
  190. }
  191. zlib_deflateEnd(zstream);
  192. kfree(zstream->workspace);
  193. }
  194. static void err_compression_marker(struct drm_i915_error_state_buf *m)
  195. {
  196. err_puts(m, ":");
  197. }
  198. #else
  199. static bool compress_init(struct z_stream_s *zstream)
  200. {
  201. return true;
  202. }
  203. static int compress_page(struct z_stream_s *zstream,
  204. void *src,
  205. struct drm_i915_error_object *dst)
  206. {
  207. unsigned long page;
  208. page = __get_free_page(GFP_ATOMIC | __GFP_NOWARN);
  209. if (!page)
  210. return -ENOMEM;
  211. dst->pages[dst->page_count++] =
  212. memcpy((void *)page, src, PAGE_SIZE);
  213. return 0;
  214. }
  215. static void compress_fini(struct z_stream_s *zstream,
  216. struct drm_i915_error_object *dst)
  217. {
  218. }
  219. static void err_compression_marker(struct drm_i915_error_state_buf *m)
  220. {
  221. err_puts(m, "~");
  222. }
  223. #endif
  224. static void print_error_buffers(struct drm_i915_error_state_buf *m,
  225. const char *name,
  226. struct drm_i915_error_buffer *err,
  227. int count)
  228. {
  229. int i;
  230. err_printf(m, "%s [%d]:\n", name, count);
  231. while (count--) {
  232. err_printf(m, " %08x_%08x %8u %02x %02x [ ",
  233. upper_32_bits(err->gtt_offset),
  234. lower_32_bits(err->gtt_offset),
  235. err->size,
  236. err->read_domains,
  237. err->write_domain);
  238. for (i = 0; i < I915_NUM_ENGINES; i++)
  239. err_printf(m, "%02x ", err->rseqno[i]);
  240. err_printf(m, "] %02x", err->wseqno);
  241. err_puts(m, tiling_flag(err->tiling));
  242. err_puts(m, dirty_flag(err->dirty));
  243. err_puts(m, purgeable_flag(err->purgeable));
  244. err_puts(m, err->userptr ? " userptr" : "");
  245. err_puts(m, err->engine != -1 ? " " : "");
  246. err_puts(m, engine_str(err->engine));
  247. err_puts(m, i915_cache_level_str(m->i915, err->cache_level));
  248. if (err->name)
  249. err_printf(m, " (name: %d)", err->name);
  250. if (err->fence_reg != I915_FENCE_REG_NONE)
  251. err_printf(m, " (fence: %d)", err->fence_reg);
  252. err_puts(m, "\n");
  253. err++;
  254. }
  255. }
  256. static const char *hangcheck_action_to_str(enum intel_engine_hangcheck_action a)
  257. {
  258. switch (a) {
  259. case HANGCHECK_IDLE:
  260. return "idle";
  261. case HANGCHECK_WAIT:
  262. return "wait";
  263. case HANGCHECK_ACTIVE:
  264. return "active";
  265. case HANGCHECK_KICK:
  266. return "kick";
  267. case HANGCHECK_HUNG:
  268. return "hung";
  269. }
  270. return "unknown";
  271. }
  272. static void error_print_instdone(struct drm_i915_error_state_buf *m,
  273. struct drm_i915_error_engine *ee)
  274. {
  275. int slice;
  276. int subslice;
  277. err_printf(m, " INSTDONE: 0x%08x\n",
  278. ee->instdone.instdone);
  279. if (ee->engine_id != RCS || INTEL_GEN(m->i915) <= 3)
  280. return;
  281. err_printf(m, " SC_INSTDONE: 0x%08x\n",
  282. ee->instdone.slice_common);
  283. if (INTEL_GEN(m->i915) <= 6)
  284. return;
  285. for_each_instdone_slice_subslice(m->i915, slice, subslice)
  286. err_printf(m, " SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
  287. slice, subslice,
  288. ee->instdone.sampler[slice][subslice]);
  289. for_each_instdone_slice_subslice(m->i915, slice, subslice)
  290. err_printf(m, " ROW_INSTDONE[%d][%d]: 0x%08x\n",
  291. slice, subslice,
  292. ee->instdone.row[slice][subslice]);
  293. }
  294. static void error_print_request(struct drm_i915_error_state_buf *m,
  295. const char *prefix,
  296. struct drm_i915_error_request *erq)
  297. {
  298. if (!erq->seqno)
  299. return;
  300. err_printf(m, "%s pid %d, seqno %8x:%08x, emitted %dms ago, head %08x, tail %08x\n",
  301. prefix, erq->pid,
  302. erq->context, erq->seqno,
  303. jiffies_to_msecs(jiffies - erq->jiffies),
  304. erq->head, erq->tail);
  305. }
  306. static void error_print_engine(struct drm_i915_error_state_buf *m,
  307. struct drm_i915_error_engine *ee)
  308. {
  309. err_printf(m, "%s command stream:\n", engine_str(ee->engine_id));
  310. err_printf(m, " START: 0x%08x\n", ee->start);
  311. err_printf(m, " HEAD: 0x%08x [0x%08x]\n", ee->head, ee->rq_head);
  312. err_printf(m, " TAIL: 0x%08x [0x%08x, 0x%08x]\n",
  313. ee->tail, ee->rq_post, ee->rq_tail);
  314. err_printf(m, " CTL: 0x%08x\n", ee->ctl);
  315. err_printf(m, " MODE: 0x%08x\n", ee->mode);
  316. err_printf(m, " HWS: 0x%08x\n", ee->hws);
  317. err_printf(m, " ACTHD: 0x%08x %08x\n",
  318. (u32)(ee->acthd>>32), (u32)ee->acthd);
  319. err_printf(m, " IPEIR: 0x%08x\n", ee->ipeir);
  320. err_printf(m, " IPEHR: 0x%08x\n", ee->ipehr);
  321. error_print_instdone(m, ee);
  322. if (ee->batchbuffer) {
  323. u64 start = ee->batchbuffer->gtt_offset;
  324. u64 end = start + ee->batchbuffer->gtt_size;
  325. err_printf(m, " batch: [0x%08x_%08x, 0x%08x_%08x]\n",
  326. upper_32_bits(start), lower_32_bits(start),
  327. upper_32_bits(end), lower_32_bits(end));
  328. }
  329. if (INTEL_GEN(m->i915) >= 4) {
  330. err_printf(m, " BBADDR: 0x%08x_%08x\n",
  331. (u32)(ee->bbaddr>>32), (u32)ee->bbaddr);
  332. err_printf(m, " BB_STATE: 0x%08x\n", ee->bbstate);
  333. err_printf(m, " INSTPS: 0x%08x\n", ee->instps);
  334. }
  335. err_printf(m, " INSTPM: 0x%08x\n", ee->instpm);
  336. err_printf(m, " FADDR: 0x%08x %08x\n", upper_32_bits(ee->faddr),
  337. lower_32_bits(ee->faddr));
  338. if (INTEL_GEN(m->i915) >= 6) {
  339. err_printf(m, " RC PSMI: 0x%08x\n", ee->rc_psmi);
  340. err_printf(m, " FAULT_REG: 0x%08x\n", ee->fault_reg);
  341. err_printf(m, " SYNC_0: 0x%08x [last synced 0x%08x]\n",
  342. ee->semaphore_mboxes[0],
  343. ee->semaphore_seqno[0]);
  344. err_printf(m, " SYNC_1: 0x%08x [last synced 0x%08x]\n",
  345. ee->semaphore_mboxes[1],
  346. ee->semaphore_seqno[1]);
  347. if (HAS_VEBOX(m->i915)) {
  348. err_printf(m, " SYNC_2: 0x%08x [last synced 0x%08x]\n",
  349. ee->semaphore_mboxes[2],
  350. ee->semaphore_seqno[2]);
  351. }
  352. }
  353. if (USES_PPGTT(m->i915)) {
  354. err_printf(m, " GFX_MODE: 0x%08x\n", ee->vm_info.gfx_mode);
  355. if (INTEL_GEN(m->i915) >= 8) {
  356. int i;
  357. for (i = 0; i < 4; i++)
  358. err_printf(m, " PDP%d: 0x%016llx\n",
  359. i, ee->vm_info.pdp[i]);
  360. } else {
  361. err_printf(m, " PP_DIR_BASE: 0x%08x\n",
  362. ee->vm_info.pp_dir_base);
  363. }
  364. }
  365. err_printf(m, " seqno: 0x%08x\n", ee->seqno);
  366. err_printf(m, " last_seqno: 0x%08x\n", ee->last_seqno);
  367. err_printf(m, " waiting: %s\n", yesno(ee->waiting));
  368. err_printf(m, " ring->head: 0x%08x\n", ee->cpu_ring_head);
  369. err_printf(m, " ring->tail: 0x%08x\n", ee->cpu_ring_tail);
  370. err_printf(m, " hangcheck: %s [%d]\n",
  371. hangcheck_action_to_str(ee->hangcheck_action),
  372. ee->hangcheck_score);
  373. error_print_request(m, " ELSP[0]: ", &ee->execlist[0]);
  374. error_print_request(m, " ELSP[1]: ", &ee->execlist[1]);
  375. }
  376. void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
  377. {
  378. va_list args;
  379. va_start(args, f);
  380. i915_error_vprintf(e, f, args);
  381. va_end(args);
  382. }
  383. static int
  384. ascii85_encode_len(int len)
  385. {
  386. return DIV_ROUND_UP(len, 4);
  387. }
  388. static bool
  389. ascii85_encode(u32 in, char *out)
  390. {
  391. int i;
  392. if (in == 0)
  393. return false;
  394. out[5] = '\0';
  395. for (i = 5; i--; ) {
  396. out[i] = '!' + in % 85;
  397. in /= 85;
  398. }
  399. return true;
  400. }
  401. static void print_error_obj(struct drm_i915_error_state_buf *m,
  402. struct intel_engine_cs *engine,
  403. const char *name,
  404. struct drm_i915_error_object *obj)
  405. {
  406. char out[6];
  407. int page;
  408. if (!obj)
  409. return;
  410. if (name) {
  411. err_printf(m, "%s --- %s = 0x%08x %08x\n",
  412. engine ? engine->name : "global", name,
  413. upper_32_bits(obj->gtt_offset),
  414. lower_32_bits(obj->gtt_offset));
  415. }
  416. err_compression_marker(m);
  417. for (page = 0; page < obj->page_count; page++) {
  418. int i, len;
  419. len = PAGE_SIZE;
  420. if (page == obj->page_count - 1)
  421. len -= obj->unused;
  422. len = ascii85_encode_len(len);
  423. for (i = 0; i < len; i++) {
  424. if (ascii85_encode(obj->pages[page][i], out))
  425. err_puts(m, out);
  426. else
  427. err_puts(m, "z");
  428. }
  429. }
  430. err_puts(m, "\n");
  431. }
  432. static void err_print_capabilities(struct drm_i915_error_state_buf *m,
  433. const struct intel_device_info *info)
  434. {
  435. #define PRINT_FLAG(x) err_printf(m, #x ": %s\n", yesno(info->x))
  436. DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG);
  437. #undef PRINT_FLAG
  438. }
  439. int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
  440. const struct i915_error_state_file_priv *error_priv)
  441. {
  442. struct drm_device *dev = error_priv->dev;
  443. struct drm_i915_private *dev_priv = to_i915(dev);
  444. struct pci_dev *pdev = dev_priv->drm.pdev;
  445. struct drm_i915_error_state *error = error_priv->error;
  446. struct drm_i915_error_object *obj;
  447. int max_hangcheck_score;
  448. int i, j;
  449. if (!error) {
  450. err_printf(m, "no error state collected\n");
  451. goto out;
  452. }
  453. err_printf(m, "%s\n", error->error_msg);
  454. err_printf(m, "Kernel: " UTS_RELEASE "\n");
  455. err_printf(m, "Time: %ld s %ld us\n",
  456. error->time.tv_sec, error->time.tv_usec);
  457. err_printf(m, "Boottime: %ld s %ld us\n",
  458. error->boottime.tv_sec, error->boottime.tv_usec);
  459. err_printf(m, "Uptime: %ld s %ld us\n",
  460. error->uptime.tv_sec, error->uptime.tv_usec);
  461. err_print_capabilities(m, &error->device_info);
  462. max_hangcheck_score = 0;
  463. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  464. if (error->engine[i].hangcheck_score > max_hangcheck_score)
  465. max_hangcheck_score = error->engine[i].hangcheck_score;
  466. }
  467. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  468. if (error->engine[i].hangcheck_score == max_hangcheck_score &&
  469. error->engine[i].pid != -1) {
  470. err_printf(m, "Active process (on ring %s): %s [%d]\n",
  471. engine_str(i),
  472. error->engine[i].comm,
  473. error->engine[i].pid);
  474. }
  475. }
  476. err_printf(m, "Reset count: %u\n", error->reset_count);
  477. err_printf(m, "Suspend count: %u\n", error->suspend_count);
  478. err_printf(m, "PCI ID: 0x%04x\n", pdev->device);
  479. err_printf(m, "PCI Revision: 0x%02x\n", pdev->revision);
  480. err_printf(m, "PCI Subsystem: %04x:%04x\n",
  481. pdev->subsystem_vendor,
  482. pdev->subsystem_device);
  483. err_printf(m, "IOMMU enabled?: %d\n", error->iommu);
  484. if (HAS_CSR(dev)) {
  485. struct intel_csr *csr = &dev_priv->csr;
  486. err_printf(m, "DMC loaded: %s\n",
  487. yesno(csr->dmc_payload != NULL));
  488. err_printf(m, "DMC fw version: %d.%d\n",
  489. CSR_VERSION_MAJOR(csr->version),
  490. CSR_VERSION_MINOR(csr->version));
  491. }
  492. err_printf(m, "EIR: 0x%08x\n", error->eir);
  493. err_printf(m, "IER: 0x%08x\n", error->ier);
  494. if (INTEL_INFO(dev)->gen >= 8) {
  495. for (i = 0; i < 4; i++)
  496. err_printf(m, "GTIER gt %d: 0x%08x\n", i,
  497. error->gtier[i]);
  498. } else if (HAS_PCH_SPLIT(dev_priv) || IS_VALLEYVIEW(dev_priv))
  499. err_printf(m, "GTIER: 0x%08x\n", error->gtier[0]);
  500. err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
  501. err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
  502. err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
  503. err_printf(m, "CCID: 0x%08x\n", error->ccid);
  504. err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
  505. for (i = 0; i < dev_priv->num_fence_regs; i++)
  506. err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
  507. if (INTEL_INFO(dev)->gen >= 6) {
  508. err_printf(m, "ERROR: 0x%08x\n", error->error);
  509. if (INTEL_INFO(dev)->gen >= 8)
  510. err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n",
  511. error->fault_data1, error->fault_data0);
  512. err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
  513. }
  514. if (IS_GEN7(dev_priv))
  515. err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
  516. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  517. if (error->engine[i].engine_id != -1)
  518. error_print_engine(m, &error->engine[i]);
  519. }
  520. for (i = 0; i < ARRAY_SIZE(error->active_vm); i++) {
  521. char buf[128];
  522. int len, first = 1;
  523. if (!error->active_vm[i])
  524. break;
  525. len = scnprintf(buf, sizeof(buf), "Active (");
  526. for (j = 0; j < ARRAY_SIZE(error->engine); j++) {
  527. if (error->engine[j].vm != error->active_vm[i])
  528. continue;
  529. len += scnprintf(buf + len, sizeof(buf), "%s%s",
  530. first ? "" : ", ",
  531. dev_priv->engine[j]->name);
  532. first = 0;
  533. }
  534. scnprintf(buf + len, sizeof(buf), ")");
  535. print_error_buffers(m, buf,
  536. error->active_bo[i],
  537. error->active_bo_count[i]);
  538. }
  539. print_error_buffers(m, "Pinned (global)",
  540. error->pinned_bo,
  541. error->pinned_bo_count);
  542. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  543. struct drm_i915_error_engine *ee = &error->engine[i];
  544. obj = ee->batchbuffer;
  545. if (obj) {
  546. err_puts(m, dev_priv->engine[i]->name);
  547. if (ee->pid != -1)
  548. err_printf(m, " (submitted by %s [%d])",
  549. ee->comm,
  550. ee->pid);
  551. err_printf(m, " --- gtt_offset = 0x%08x %08x\n",
  552. upper_32_bits(obj->gtt_offset),
  553. lower_32_bits(obj->gtt_offset));
  554. print_error_obj(m, dev_priv->engine[i], NULL, obj);
  555. }
  556. if (ee->num_requests) {
  557. err_printf(m, "%s --- %d requests\n",
  558. dev_priv->engine[i]->name,
  559. ee->num_requests);
  560. for (j = 0; j < ee->num_requests; j++)
  561. error_print_request(m, " ", &ee->requests[j]);
  562. }
  563. if (IS_ERR(ee->waiters)) {
  564. err_printf(m, "%s --- ? waiters [unable to acquire spinlock]\n",
  565. dev_priv->engine[i]->name);
  566. } else if (ee->num_waiters) {
  567. err_printf(m, "%s --- %d waiters\n",
  568. dev_priv->engine[i]->name,
  569. ee->num_waiters);
  570. for (j = 0; j < ee->num_waiters; j++) {
  571. err_printf(m, " seqno 0x%08x for %s [%d]\n",
  572. ee->waiters[j].seqno,
  573. ee->waiters[j].comm,
  574. ee->waiters[j].pid);
  575. }
  576. }
  577. print_error_obj(m, dev_priv->engine[i],
  578. "ringbuffer", ee->ringbuffer);
  579. print_error_obj(m, dev_priv->engine[i],
  580. "HW Status", ee->hws_page);
  581. print_error_obj(m, dev_priv->engine[i],
  582. "HW context", ee->ctx);
  583. print_error_obj(m, dev_priv->engine[i],
  584. "WA context", ee->wa_ctx);
  585. print_error_obj(m, dev_priv->engine[i],
  586. "WA batchbuffer", ee->wa_batchbuffer);
  587. }
  588. print_error_obj(m, NULL, "Semaphores", error->semaphore);
  589. print_error_obj(m, NULL, "GuC log buffer", error->guc_log);
  590. if (error->overlay)
  591. intel_overlay_print_error_state(m, error->overlay);
  592. if (error->display)
  593. intel_display_print_error_state(m, dev, error->display);
  594. out:
  595. if (m->bytes == 0 && m->err)
  596. return m->err;
  597. return 0;
  598. }
  599. int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
  600. struct drm_i915_private *i915,
  601. size_t count, loff_t pos)
  602. {
  603. memset(ebuf, 0, sizeof(*ebuf));
  604. ebuf->i915 = i915;
  605. /* We need to have enough room to store any i915_error_state printf
  606. * so that we can move it to start position.
  607. */
  608. ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
  609. ebuf->buf = kmalloc(ebuf->size,
  610. GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
  611. if (ebuf->buf == NULL) {
  612. ebuf->size = PAGE_SIZE;
  613. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  614. }
  615. if (ebuf->buf == NULL) {
  616. ebuf->size = 128;
  617. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  618. }
  619. if (ebuf->buf == NULL)
  620. return -ENOMEM;
  621. ebuf->start = pos;
  622. return 0;
  623. }
  624. static void i915_error_object_free(struct drm_i915_error_object *obj)
  625. {
  626. int page;
  627. if (obj == NULL)
  628. return;
  629. for (page = 0; page < obj->page_count; page++)
  630. free_page((unsigned long)obj->pages[page]);
  631. kfree(obj);
  632. }
  633. static void i915_error_state_free(struct kref *error_ref)
  634. {
  635. struct drm_i915_error_state *error = container_of(error_ref,
  636. typeof(*error), ref);
  637. int i;
  638. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  639. struct drm_i915_error_engine *ee = &error->engine[i];
  640. i915_error_object_free(ee->batchbuffer);
  641. i915_error_object_free(ee->wa_batchbuffer);
  642. i915_error_object_free(ee->ringbuffer);
  643. i915_error_object_free(ee->hws_page);
  644. i915_error_object_free(ee->ctx);
  645. i915_error_object_free(ee->wa_ctx);
  646. kfree(ee->requests);
  647. if (!IS_ERR_OR_NULL(ee->waiters))
  648. kfree(ee->waiters);
  649. }
  650. i915_error_object_free(error->semaphore);
  651. i915_error_object_free(error->guc_log);
  652. for (i = 0; i < ARRAY_SIZE(error->active_bo); i++)
  653. kfree(error->active_bo[i]);
  654. kfree(error->pinned_bo);
  655. kfree(error->overlay);
  656. kfree(error->display);
  657. kfree(error);
  658. }
  659. static struct drm_i915_error_object *
  660. i915_error_object_create(struct drm_i915_private *i915,
  661. struct i915_vma *vma)
  662. {
  663. struct i915_ggtt *ggtt = &i915->ggtt;
  664. const u64 slot = ggtt->error_capture.start;
  665. struct drm_i915_error_object *dst;
  666. struct z_stream_s zstream;
  667. unsigned long num_pages;
  668. struct sgt_iter iter;
  669. dma_addr_t dma;
  670. if (!vma)
  671. return NULL;
  672. num_pages = min_t(u64, vma->size, vma->obj->base.size) >> PAGE_SHIFT;
  673. num_pages = DIV_ROUND_UP(10 * num_pages, 8); /* worstcase zlib growth */
  674. dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *),
  675. GFP_ATOMIC | __GFP_NOWARN);
  676. if (!dst)
  677. return NULL;
  678. dst->gtt_offset = vma->node.start;
  679. dst->gtt_size = vma->node.size;
  680. dst->page_count = 0;
  681. dst->unused = 0;
  682. if (!compress_init(&zstream)) {
  683. kfree(dst);
  684. return NULL;
  685. }
  686. for_each_sgt_dma(dma, iter, vma->pages) {
  687. void __iomem *s;
  688. int ret;
  689. ggtt->base.insert_page(&ggtt->base, dma, slot,
  690. I915_CACHE_NONE, 0);
  691. s = io_mapping_map_atomic_wc(&ggtt->mappable, slot);
  692. ret = compress_page(&zstream, (void __force *)s, dst);
  693. io_mapping_unmap_atomic(s);
  694. if (ret)
  695. goto unwind;
  696. }
  697. goto out;
  698. unwind:
  699. while (dst->page_count--)
  700. free_page((unsigned long)dst->pages[dst->page_count]);
  701. kfree(dst);
  702. dst = NULL;
  703. out:
  704. compress_fini(&zstream, dst);
  705. ggtt->base.clear_range(&ggtt->base, slot, PAGE_SIZE);
  706. return dst;
  707. }
  708. /* The error capture is special as tries to run underneath the normal
  709. * locking rules - so we use the raw version of the i915_gem_active lookup.
  710. */
  711. static inline uint32_t
  712. __active_get_seqno(struct i915_gem_active *active)
  713. {
  714. return i915_gem_request_get_seqno(__i915_gem_active_peek(active));
  715. }
  716. static inline int
  717. __active_get_engine_id(struct i915_gem_active *active)
  718. {
  719. struct intel_engine_cs *engine;
  720. engine = i915_gem_request_get_engine(__i915_gem_active_peek(active));
  721. return engine ? engine->id : -1;
  722. }
  723. static void capture_bo(struct drm_i915_error_buffer *err,
  724. struct i915_vma *vma)
  725. {
  726. struct drm_i915_gem_object *obj = vma->obj;
  727. int i;
  728. err->size = obj->base.size;
  729. err->name = obj->base.name;
  730. for (i = 0; i < I915_NUM_ENGINES; i++)
  731. err->rseqno[i] = __active_get_seqno(&vma->last_read[i]);
  732. err->wseqno = __active_get_seqno(&vma->last_write);
  733. err->engine = __active_get_engine_id(&vma->last_write);
  734. err->gtt_offset = vma->node.start;
  735. err->read_domains = obj->base.read_domains;
  736. err->write_domain = obj->base.write_domain;
  737. err->fence_reg = vma->fence ? vma->fence->id : -1;
  738. err->tiling = i915_gem_object_get_tiling(obj);
  739. err->dirty = obj->mm.dirty;
  740. err->purgeable = obj->mm.madv != I915_MADV_WILLNEED;
  741. err->userptr = obj->userptr.mm != NULL;
  742. err->cache_level = obj->cache_level;
  743. }
  744. static u32 capture_error_bo(struct drm_i915_error_buffer *err,
  745. int count, struct list_head *head,
  746. bool pinned_only)
  747. {
  748. struct i915_vma *vma;
  749. int i = 0;
  750. list_for_each_entry(vma, head, vm_link) {
  751. if (pinned_only && !i915_vma_is_pinned(vma))
  752. continue;
  753. capture_bo(err++, vma);
  754. if (++i == count)
  755. break;
  756. }
  757. return i;
  758. }
  759. /* Generate a semi-unique error code. The code is not meant to have meaning, The
  760. * code's only purpose is to try to prevent false duplicated bug reports by
  761. * grossly estimating a GPU error state.
  762. *
  763. * TODO Ideally, hashing the batchbuffer would be a very nice way to determine
  764. * the hang if we could strip the GTT offset information from it.
  765. *
  766. * It's only a small step better than a random number in its current form.
  767. */
  768. static uint32_t i915_error_generate_code(struct drm_i915_private *dev_priv,
  769. struct drm_i915_error_state *error,
  770. int *engine_id)
  771. {
  772. uint32_t error_code = 0;
  773. int i;
  774. /* IPEHR would be an ideal way to detect errors, as it's the gross
  775. * measure of "the command that hung." However, has some very common
  776. * synchronization commands which almost always appear in the case
  777. * strictly a client bug. Use instdone to differentiate those some.
  778. */
  779. for (i = 0; i < I915_NUM_ENGINES; i++) {
  780. if (error->engine[i].hangcheck_action == HANGCHECK_HUNG) {
  781. if (engine_id)
  782. *engine_id = i;
  783. return error->engine[i].ipehr ^
  784. error->engine[i].instdone.instdone;
  785. }
  786. }
  787. return error_code;
  788. }
  789. static void i915_gem_record_fences(struct drm_i915_private *dev_priv,
  790. struct drm_i915_error_state *error)
  791. {
  792. int i;
  793. if (IS_GEN3(dev_priv) || IS_GEN2(dev_priv)) {
  794. for (i = 0; i < dev_priv->num_fence_regs; i++)
  795. error->fence[i] = I915_READ(FENCE_REG(i));
  796. } else if (IS_GEN5(dev_priv) || IS_GEN4(dev_priv)) {
  797. for (i = 0; i < dev_priv->num_fence_regs; i++)
  798. error->fence[i] = I915_READ64(FENCE_REG_965_LO(i));
  799. } else if (INTEL_GEN(dev_priv) >= 6) {
  800. for (i = 0; i < dev_priv->num_fence_regs; i++)
  801. error->fence[i] = I915_READ64(FENCE_REG_GEN6_LO(i));
  802. }
  803. }
  804. static void gen8_record_semaphore_state(struct drm_i915_error_state *error,
  805. struct intel_engine_cs *engine,
  806. struct drm_i915_error_engine *ee)
  807. {
  808. struct drm_i915_private *dev_priv = engine->i915;
  809. struct intel_engine_cs *to;
  810. enum intel_engine_id id;
  811. if (!error->semaphore)
  812. return;
  813. for_each_engine(to, dev_priv, id) {
  814. int idx;
  815. u16 signal_offset;
  816. u32 *tmp;
  817. if (engine == to)
  818. continue;
  819. signal_offset =
  820. (GEN8_SIGNAL_OFFSET(engine, id) & (PAGE_SIZE - 1)) / 4;
  821. tmp = error->semaphore->pages[0];
  822. idx = intel_engine_sync_index(engine, to);
  823. ee->semaphore_mboxes[idx] = tmp[signal_offset];
  824. ee->semaphore_seqno[idx] = engine->semaphore.sync_seqno[idx];
  825. }
  826. }
  827. static void gen6_record_semaphore_state(struct intel_engine_cs *engine,
  828. struct drm_i915_error_engine *ee)
  829. {
  830. struct drm_i915_private *dev_priv = engine->i915;
  831. ee->semaphore_mboxes[0] = I915_READ(RING_SYNC_0(engine->mmio_base));
  832. ee->semaphore_mboxes[1] = I915_READ(RING_SYNC_1(engine->mmio_base));
  833. ee->semaphore_seqno[0] = engine->semaphore.sync_seqno[0];
  834. ee->semaphore_seqno[1] = engine->semaphore.sync_seqno[1];
  835. if (HAS_VEBOX(dev_priv)) {
  836. ee->semaphore_mboxes[2] =
  837. I915_READ(RING_SYNC_2(engine->mmio_base));
  838. ee->semaphore_seqno[2] = engine->semaphore.sync_seqno[2];
  839. }
  840. }
  841. static void error_record_engine_waiters(struct intel_engine_cs *engine,
  842. struct drm_i915_error_engine *ee)
  843. {
  844. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  845. struct drm_i915_error_waiter *waiter;
  846. struct rb_node *rb;
  847. int count;
  848. ee->num_waiters = 0;
  849. ee->waiters = NULL;
  850. if (RB_EMPTY_ROOT(&b->waiters))
  851. return;
  852. if (!spin_trylock(&b->lock)) {
  853. ee->waiters = ERR_PTR(-EDEADLK);
  854. return;
  855. }
  856. count = 0;
  857. for (rb = rb_first(&b->waiters); rb != NULL; rb = rb_next(rb))
  858. count++;
  859. spin_unlock(&b->lock);
  860. waiter = NULL;
  861. if (count)
  862. waiter = kmalloc_array(count,
  863. sizeof(struct drm_i915_error_waiter),
  864. GFP_ATOMIC);
  865. if (!waiter)
  866. return;
  867. if (!spin_trylock(&b->lock)) {
  868. kfree(waiter);
  869. ee->waiters = ERR_PTR(-EDEADLK);
  870. return;
  871. }
  872. ee->waiters = waiter;
  873. for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
  874. struct intel_wait *w = container_of(rb, typeof(*w), node);
  875. strcpy(waiter->comm, w->tsk->comm);
  876. waiter->pid = w->tsk->pid;
  877. waiter->seqno = w->seqno;
  878. waiter++;
  879. if (++ee->num_waiters == count)
  880. break;
  881. }
  882. spin_unlock(&b->lock);
  883. }
  884. static void error_record_engine_registers(struct drm_i915_error_state *error,
  885. struct intel_engine_cs *engine,
  886. struct drm_i915_error_engine *ee)
  887. {
  888. struct drm_i915_private *dev_priv = engine->i915;
  889. if (INTEL_GEN(dev_priv) >= 6) {
  890. ee->rc_psmi = I915_READ(RING_PSMI_CTL(engine->mmio_base));
  891. ee->fault_reg = I915_READ(RING_FAULT_REG(engine));
  892. if (INTEL_GEN(dev_priv) >= 8)
  893. gen8_record_semaphore_state(error, engine, ee);
  894. else
  895. gen6_record_semaphore_state(engine, ee);
  896. }
  897. if (INTEL_GEN(dev_priv) >= 4) {
  898. ee->faddr = I915_READ(RING_DMA_FADD(engine->mmio_base));
  899. ee->ipeir = I915_READ(RING_IPEIR(engine->mmio_base));
  900. ee->ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
  901. ee->instps = I915_READ(RING_INSTPS(engine->mmio_base));
  902. ee->bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
  903. if (INTEL_GEN(dev_priv) >= 8) {
  904. ee->faddr |= (u64) I915_READ(RING_DMA_FADD_UDW(engine->mmio_base)) << 32;
  905. ee->bbaddr |= (u64) I915_READ(RING_BBADDR_UDW(engine->mmio_base)) << 32;
  906. }
  907. ee->bbstate = I915_READ(RING_BBSTATE(engine->mmio_base));
  908. } else {
  909. ee->faddr = I915_READ(DMA_FADD_I8XX);
  910. ee->ipeir = I915_READ(IPEIR);
  911. ee->ipehr = I915_READ(IPEHR);
  912. }
  913. intel_engine_get_instdone(engine, &ee->instdone);
  914. ee->waiting = intel_engine_has_waiter(engine);
  915. ee->instpm = I915_READ(RING_INSTPM(engine->mmio_base));
  916. ee->acthd = intel_engine_get_active_head(engine);
  917. ee->seqno = intel_engine_get_seqno(engine);
  918. ee->last_seqno = engine->timeline->last_submitted_seqno;
  919. ee->start = I915_READ_START(engine);
  920. ee->head = I915_READ_HEAD(engine);
  921. ee->tail = I915_READ_TAIL(engine);
  922. ee->ctl = I915_READ_CTL(engine);
  923. if (INTEL_GEN(dev_priv) > 2)
  924. ee->mode = I915_READ_MODE(engine);
  925. if (!HWS_NEEDS_PHYSICAL(dev_priv)) {
  926. i915_reg_t mmio;
  927. if (IS_GEN7(dev_priv)) {
  928. switch (engine->id) {
  929. default:
  930. case RCS:
  931. mmio = RENDER_HWS_PGA_GEN7;
  932. break;
  933. case BCS:
  934. mmio = BLT_HWS_PGA_GEN7;
  935. break;
  936. case VCS:
  937. mmio = BSD_HWS_PGA_GEN7;
  938. break;
  939. case VECS:
  940. mmio = VEBOX_HWS_PGA_GEN7;
  941. break;
  942. }
  943. } else if (IS_GEN6(engine->i915)) {
  944. mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
  945. } else {
  946. /* XXX: gen8 returns to sanity */
  947. mmio = RING_HWS_PGA(engine->mmio_base);
  948. }
  949. ee->hws = I915_READ(mmio);
  950. }
  951. ee->hangcheck_score = engine->hangcheck.score;
  952. ee->hangcheck_action = engine->hangcheck.action;
  953. if (USES_PPGTT(dev_priv)) {
  954. int i;
  955. ee->vm_info.gfx_mode = I915_READ(RING_MODE_GEN7(engine));
  956. if (IS_GEN6(dev_priv))
  957. ee->vm_info.pp_dir_base =
  958. I915_READ(RING_PP_DIR_BASE_READ(engine));
  959. else if (IS_GEN7(dev_priv))
  960. ee->vm_info.pp_dir_base =
  961. I915_READ(RING_PP_DIR_BASE(engine));
  962. else if (INTEL_GEN(dev_priv) >= 8)
  963. for (i = 0; i < 4; i++) {
  964. ee->vm_info.pdp[i] =
  965. I915_READ(GEN8_RING_PDP_UDW(engine, i));
  966. ee->vm_info.pdp[i] <<= 32;
  967. ee->vm_info.pdp[i] |=
  968. I915_READ(GEN8_RING_PDP_LDW(engine, i));
  969. }
  970. }
  971. }
  972. static void record_request(struct drm_i915_gem_request *request,
  973. struct drm_i915_error_request *erq)
  974. {
  975. erq->context = request->ctx->hw_id;
  976. erq->seqno = request->global_seqno;
  977. erq->jiffies = request->emitted_jiffies;
  978. erq->head = request->head;
  979. erq->tail = request->tail;
  980. rcu_read_lock();
  981. erq->pid = request->ctx->pid ? pid_nr(request->ctx->pid) : 0;
  982. rcu_read_unlock();
  983. }
  984. static void engine_record_requests(struct intel_engine_cs *engine,
  985. struct drm_i915_gem_request *first,
  986. struct drm_i915_error_engine *ee)
  987. {
  988. struct drm_i915_gem_request *request;
  989. int count;
  990. count = 0;
  991. request = first;
  992. list_for_each_entry_from(request, &engine->timeline->requests, link)
  993. count++;
  994. if (!count)
  995. return;
  996. ee->requests = kcalloc(count, sizeof(*ee->requests), GFP_ATOMIC);
  997. if (!ee->requests)
  998. return;
  999. ee->num_requests = count;
  1000. count = 0;
  1001. request = first;
  1002. list_for_each_entry_from(request, &engine->timeline->requests, link) {
  1003. if (count >= ee->num_requests) {
  1004. /*
  1005. * If the ring request list was changed in
  1006. * between the point where the error request
  1007. * list was created and dimensioned and this
  1008. * point then just exit early to avoid crashes.
  1009. *
  1010. * We don't need to communicate that the
  1011. * request list changed state during error
  1012. * state capture and that the error state is
  1013. * slightly incorrect as a consequence since we
  1014. * are typically only interested in the request
  1015. * list state at the point of error state
  1016. * capture, not in any changes happening during
  1017. * the capture.
  1018. */
  1019. break;
  1020. }
  1021. record_request(request, &ee->requests[count++]);
  1022. }
  1023. ee->num_requests = count;
  1024. }
  1025. static void error_record_engine_execlists(struct intel_engine_cs *engine,
  1026. struct drm_i915_error_engine *ee)
  1027. {
  1028. unsigned int n;
  1029. for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++)
  1030. if (engine->execlist_port[n].request)
  1031. record_request(engine->execlist_port[n].request,
  1032. &ee->execlist[n]);
  1033. }
  1034. static void i915_gem_record_rings(struct drm_i915_private *dev_priv,
  1035. struct drm_i915_error_state *error)
  1036. {
  1037. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1038. int i;
  1039. error->semaphore =
  1040. i915_error_object_create(dev_priv, dev_priv->semaphore);
  1041. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1042. struct intel_engine_cs *engine = dev_priv->engine[i];
  1043. struct drm_i915_error_engine *ee = &error->engine[i];
  1044. struct drm_i915_gem_request *request;
  1045. ee->pid = -1;
  1046. ee->engine_id = -1;
  1047. if (!engine)
  1048. continue;
  1049. ee->engine_id = i;
  1050. error_record_engine_registers(error, engine, ee);
  1051. error_record_engine_waiters(engine, ee);
  1052. error_record_engine_execlists(engine, ee);
  1053. request = i915_gem_find_active_request(engine);
  1054. if (request) {
  1055. struct intel_ring *ring;
  1056. struct pid *pid;
  1057. ee->vm = request->ctx->ppgtt ?
  1058. &request->ctx->ppgtt->base : &ggtt->base;
  1059. /* We need to copy these to an anonymous buffer
  1060. * as the simplest method to avoid being overwritten
  1061. * by userspace.
  1062. */
  1063. ee->batchbuffer =
  1064. i915_error_object_create(dev_priv,
  1065. request->batch);
  1066. if (HAS_BROKEN_CS_TLB(dev_priv))
  1067. ee->wa_batchbuffer =
  1068. i915_error_object_create(dev_priv,
  1069. engine->scratch);
  1070. ee->ctx =
  1071. i915_error_object_create(dev_priv,
  1072. request->ctx->engine[i].state);
  1073. pid = request->ctx->pid;
  1074. if (pid) {
  1075. struct task_struct *task;
  1076. rcu_read_lock();
  1077. task = pid_task(pid, PIDTYPE_PID);
  1078. if (task) {
  1079. strcpy(ee->comm, task->comm);
  1080. ee->pid = task->pid;
  1081. }
  1082. rcu_read_unlock();
  1083. }
  1084. error->simulated |=
  1085. request->ctx->flags & CONTEXT_NO_ERROR_CAPTURE;
  1086. ee->rq_head = request->head;
  1087. ee->rq_post = request->postfix;
  1088. ee->rq_tail = request->tail;
  1089. ring = request->ring;
  1090. ee->cpu_ring_head = ring->head;
  1091. ee->cpu_ring_tail = ring->tail;
  1092. ee->ringbuffer =
  1093. i915_error_object_create(dev_priv, ring->vma);
  1094. engine_record_requests(engine, request, ee);
  1095. }
  1096. ee->hws_page =
  1097. i915_error_object_create(dev_priv,
  1098. engine->status_page.vma);
  1099. ee->wa_ctx =
  1100. i915_error_object_create(dev_priv, engine->wa_ctx.vma);
  1101. }
  1102. }
  1103. static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
  1104. struct drm_i915_error_state *error,
  1105. struct i915_address_space *vm,
  1106. int idx)
  1107. {
  1108. struct drm_i915_error_buffer *active_bo;
  1109. struct i915_vma *vma;
  1110. int count;
  1111. count = 0;
  1112. list_for_each_entry(vma, &vm->active_list, vm_link)
  1113. count++;
  1114. active_bo = NULL;
  1115. if (count)
  1116. active_bo = kcalloc(count, sizeof(*active_bo), GFP_ATOMIC);
  1117. if (active_bo)
  1118. count = capture_error_bo(active_bo, count, &vm->active_list, false);
  1119. else
  1120. count = 0;
  1121. error->active_vm[idx] = vm;
  1122. error->active_bo[idx] = active_bo;
  1123. error->active_bo_count[idx] = count;
  1124. }
  1125. static void i915_capture_active_buffers(struct drm_i915_private *dev_priv,
  1126. struct drm_i915_error_state *error)
  1127. {
  1128. int cnt = 0, i, j;
  1129. BUILD_BUG_ON(ARRAY_SIZE(error->engine) > ARRAY_SIZE(error->active_bo));
  1130. BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_vm));
  1131. BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_bo_count));
  1132. /* Scan each engine looking for unique active contexts/vm */
  1133. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  1134. struct drm_i915_error_engine *ee = &error->engine[i];
  1135. bool found;
  1136. if (!ee->vm)
  1137. continue;
  1138. found = false;
  1139. for (j = 0; j < i && !found; j++)
  1140. found = error->engine[j].vm == ee->vm;
  1141. if (!found)
  1142. i915_gem_capture_vm(dev_priv, error, ee->vm, cnt++);
  1143. }
  1144. }
  1145. static void i915_capture_pinned_buffers(struct drm_i915_private *dev_priv,
  1146. struct drm_i915_error_state *error)
  1147. {
  1148. struct i915_address_space *vm = &dev_priv->ggtt.base;
  1149. struct drm_i915_error_buffer *bo;
  1150. struct i915_vma *vma;
  1151. int count_inactive, count_active;
  1152. count_inactive = 0;
  1153. list_for_each_entry(vma, &vm->active_list, vm_link)
  1154. count_inactive++;
  1155. count_active = 0;
  1156. list_for_each_entry(vma, &vm->inactive_list, vm_link)
  1157. count_active++;
  1158. bo = NULL;
  1159. if (count_inactive + count_active)
  1160. bo = kcalloc(count_inactive + count_active,
  1161. sizeof(*bo), GFP_ATOMIC);
  1162. if (!bo)
  1163. return;
  1164. count_inactive = capture_error_bo(bo, count_inactive,
  1165. &vm->active_list, true);
  1166. count_active = capture_error_bo(bo + count_inactive, count_active,
  1167. &vm->inactive_list, true);
  1168. error->pinned_bo_count = count_inactive + count_active;
  1169. error->pinned_bo = bo;
  1170. }
  1171. static void i915_gem_capture_guc_log_buffer(struct drm_i915_private *dev_priv,
  1172. struct drm_i915_error_state *error)
  1173. {
  1174. /* Capturing log buf contents won't be useful if logging was disabled */
  1175. if (!dev_priv->guc.log.vma || (i915.guc_log_level < 0))
  1176. return;
  1177. error->guc_log = i915_error_object_create(dev_priv,
  1178. dev_priv->guc.log.vma);
  1179. }
  1180. /* Capture all registers which don't fit into another category. */
  1181. static void i915_capture_reg_state(struct drm_i915_private *dev_priv,
  1182. struct drm_i915_error_state *error)
  1183. {
  1184. struct drm_device *dev = &dev_priv->drm;
  1185. int i;
  1186. /* General organization
  1187. * 1. Registers specific to a single generation
  1188. * 2. Registers which belong to multiple generations
  1189. * 3. Feature specific registers.
  1190. * 4. Everything else
  1191. * Please try to follow the order.
  1192. */
  1193. /* 1: Registers specific to a single generation */
  1194. if (IS_VALLEYVIEW(dev_priv)) {
  1195. error->gtier[0] = I915_READ(GTIER);
  1196. error->ier = I915_READ(VLV_IER);
  1197. error->forcewake = I915_READ_FW(FORCEWAKE_VLV);
  1198. }
  1199. if (IS_GEN7(dev_priv))
  1200. error->err_int = I915_READ(GEN7_ERR_INT);
  1201. if (INTEL_INFO(dev)->gen >= 8) {
  1202. error->fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
  1203. error->fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
  1204. }
  1205. if (IS_GEN6(dev_priv)) {
  1206. error->forcewake = I915_READ_FW(FORCEWAKE);
  1207. error->gab_ctl = I915_READ(GAB_CTL);
  1208. error->gfx_mode = I915_READ(GFX_MODE);
  1209. }
  1210. /* 2: Registers which belong to multiple generations */
  1211. if (INTEL_INFO(dev)->gen >= 7)
  1212. error->forcewake = I915_READ_FW(FORCEWAKE_MT);
  1213. if (INTEL_INFO(dev)->gen >= 6) {
  1214. error->derrmr = I915_READ(DERRMR);
  1215. error->error = I915_READ(ERROR_GEN6);
  1216. error->done_reg = I915_READ(DONE_REG);
  1217. }
  1218. /* 3: Feature specific registers */
  1219. if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
  1220. error->gam_ecochk = I915_READ(GAM_ECOCHK);
  1221. error->gac_eco = I915_READ(GAC_ECO_BITS);
  1222. }
  1223. /* 4: Everything else */
  1224. if (HAS_HW_CONTEXTS(dev))
  1225. error->ccid = I915_READ(CCID);
  1226. if (INTEL_INFO(dev)->gen >= 8) {
  1227. error->ier = I915_READ(GEN8_DE_MISC_IER);
  1228. for (i = 0; i < 4; i++)
  1229. error->gtier[i] = I915_READ(GEN8_GT_IER(i));
  1230. } else if (HAS_PCH_SPLIT(dev_priv)) {
  1231. error->ier = I915_READ(DEIER);
  1232. error->gtier[0] = I915_READ(GTIER);
  1233. } else if (IS_GEN2(dev_priv)) {
  1234. error->ier = I915_READ16(IER);
  1235. } else if (!IS_VALLEYVIEW(dev_priv)) {
  1236. error->ier = I915_READ(IER);
  1237. }
  1238. error->eir = I915_READ(EIR);
  1239. error->pgtbl_er = I915_READ(PGTBL_ER);
  1240. }
  1241. static void i915_error_capture_msg(struct drm_i915_private *dev_priv,
  1242. struct drm_i915_error_state *error,
  1243. u32 engine_mask,
  1244. const char *error_msg)
  1245. {
  1246. u32 ecode;
  1247. int engine_id = -1, len;
  1248. ecode = i915_error_generate_code(dev_priv, error, &engine_id);
  1249. len = scnprintf(error->error_msg, sizeof(error->error_msg),
  1250. "GPU HANG: ecode %d:%d:0x%08x",
  1251. INTEL_GEN(dev_priv), engine_id, ecode);
  1252. if (engine_id != -1 && error->engine[engine_id].pid != -1)
  1253. len += scnprintf(error->error_msg + len,
  1254. sizeof(error->error_msg) - len,
  1255. ", in %s [%d]",
  1256. error->engine[engine_id].comm,
  1257. error->engine[engine_id].pid);
  1258. scnprintf(error->error_msg + len, sizeof(error->error_msg) - len,
  1259. ", reason: %s, action: %s",
  1260. error_msg,
  1261. engine_mask ? "reset" : "continue");
  1262. }
  1263. static void i915_capture_gen_state(struct drm_i915_private *dev_priv,
  1264. struct drm_i915_error_state *error)
  1265. {
  1266. error->iommu = -1;
  1267. #ifdef CONFIG_INTEL_IOMMU
  1268. error->iommu = intel_iommu_gfx_mapped;
  1269. #endif
  1270. error->reset_count = i915_reset_count(&dev_priv->gpu_error);
  1271. error->suspend_count = dev_priv->suspend_count;
  1272. memcpy(&error->device_info,
  1273. INTEL_INFO(dev_priv),
  1274. sizeof(error->device_info));
  1275. }
  1276. static int capture(void *data)
  1277. {
  1278. struct drm_i915_error_state *error = data;
  1279. i915_capture_gen_state(error->i915, error);
  1280. i915_capture_reg_state(error->i915, error);
  1281. i915_gem_record_fences(error->i915, error);
  1282. i915_gem_record_rings(error->i915, error);
  1283. i915_capture_active_buffers(error->i915, error);
  1284. i915_capture_pinned_buffers(error->i915, error);
  1285. i915_gem_capture_guc_log_buffer(error->i915, error);
  1286. do_gettimeofday(&error->time);
  1287. error->boottime = ktime_to_timeval(ktime_get_boottime());
  1288. error->uptime =
  1289. ktime_to_timeval(ktime_sub(ktime_get(),
  1290. error->i915->gt.last_init_time));
  1291. error->overlay = intel_overlay_capture_error_state(error->i915);
  1292. error->display = intel_display_capture_error_state(error->i915);
  1293. return 0;
  1294. }
  1295. #define DAY_AS_SECONDS(x) (24 * 60 * 60 * (x))
  1296. /**
  1297. * i915_capture_error_state - capture an error record for later analysis
  1298. * @dev: drm device
  1299. *
  1300. * Should be called when an error is detected (either a hang or an error
  1301. * interrupt) to capture error state from the time of the error. Fills
  1302. * out a structure which becomes available in debugfs for user level tools
  1303. * to pick up.
  1304. */
  1305. void i915_capture_error_state(struct drm_i915_private *dev_priv,
  1306. u32 engine_mask,
  1307. const char *error_msg)
  1308. {
  1309. static bool warned;
  1310. struct drm_i915_error_state *error;
  1311. unsigned long flags;
  1312. if (!i915.error_capture)
  1313. return;
  1314. if (READ_ONCE(dev_priv->gpu_error.first_error))
  1315. return;
  1316. /* Account for pipe specific data like PIPE*STAT */
  1317. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  1318. if (!error) {
  1319. DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
  1320. return;
  1321. }
  1322. kref_init(&error->ref);
  1323. error->i915 = dev_priv;
  1324. stop_machine(capture, error, NULL);
  1325. i915_error_capture_msg(dev_priv, error, engine_mask, error_msg);
  1326. DRM_INFO("%s\n", error->error_msg);
  1327. if (!error->simulated) {
  1328. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  1329. if (!dev_priv->gpu_error.first_error) {
  1330. dev_priv->gpu_error.first_error = error;
  1331. error = NULL;
  1332. }
  1333. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  1334. }
  1335. if (error) {
  1336. i915_error_state_free(&error->ref);
  1337. return;
  1338. }
  1339. if (!warned &&
  1340. ktime_get_real_seconds() - DRIVER_TIMESTAMP < DAY_AS_SECONDS(180)) {
  1341. DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
  1342. DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
  1343. DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
  1344. DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
  1345. DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n",
  1346. dev_priv->drm.primary->index);
  1347. warned = true;
  1348. }
  1349. }
  1350. void i915_error_state_get(struct drm_device *dev,
  1351. struct i915_error_state_file_priv *error_priv)
  1352. {
  1353. struct drm_i915_private *dev_priv = to_i915(dev);
  1354. spin_lock_irq(&dev_priv->gpu_error.lock);
  1355. error_priv->error = dev_priv->gpu_error.first_error;
  1356. if (error_priv->error)
  1357. kref_get(&error_priv->error->ref);
  1358. spin_unlock_irq(&dev_priv->gpu_error.lock);
  1359. }
  1360. void i915_error_state_put(struct i915_error_state_file_priv *error_priv)
  1361. {
  1362. if (error_priv->error)
  1363. kref_put(&error_priv->error->ref, i915_error_state_free);
  1364. }
  1365. void i915_destroy_error_state(struct drm_device *dev)
  1366. {
  1367. struct drm_i915_private *dev_priv = to_i915(dev);
  1368. struct drm_i915_error_state *error;
  1369. spin_lock_irq(&dev_priv->gpu_error.lock);
  1370. error = dev_priv->gpu_error.first_error;
  1371. dev_priv->gpu_error.first_error = NULL;
  1372. spin_unlock_irq(&dev_priv->gpu_error.lock);
  1373. if (error)
  1374. kref_put(&error->ref, i915_error_state_free);
  1375. }