i915_gpu_error.c 47 KB

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