i915_gpu_error.c 42 KB

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