i915_gpu_error.c 50 KB

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