i915_gpu_error.c 50 KB

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