i915_gpu_error.c 49 KB

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