i915_gpu_error.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  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_request(struct drm_i915_error_state_buf *m,
  295. const char *prefix,
  296. struct drm_i915_error_request *erq)
  297. {
  298. if (!erq->seqno)
  299. return;
  300. err_printf(m, "%s pid %d, seqno %8x:%08x, emitted %dms ago, head %08x, tail %08x\n",
  301. prefix, erq->pid,
  302. erq->context, erq->seqno,
  303. jiffies_to_msecs(jiffies - erq->jiffies),
  304. erq->head, erq->tail);
  305. }
  306. static void error_print_engine(struct drm_i915_error_state_buf *m,
  307. struct drm_i915_error_engine *ee)
  308. {
  309. err_printf(m, "%s command stream:\n", engine_str(ee->engine_id));
  310. err_printf(m, " START: 0x%08x\n", ee->start);
  311. err_printf(m, " HEAD: 0x%08x [0x%08x]\n", ee->head, ee->rq_head);
  312. err_printf(m, " TAIL: 0x%08x [0x%08x, 0x%08x]\n",
  313. ee->tail, ee->rq_post, ee->rq_tail);
  314. err_printf(m, " CTL: 0x%08x\n", ee->ctl);
  315. err_printf(m, " MODE: 0x%08x\n", ee->mode);
  316. err_printf(m, " HWS: 0x%08x\n", ee->hws);
  317. err_printf(m, " ACTHD: 0x%08x %08x\n",
  318. (u32)(ee->acthd>>32), (u32)ee->acthd);
  319. err_printf(m, " IPEIR: 0x%08x\n", ee->ipeir);
  320. err_printf(m, " IPEHR: 0x%08x\n", ee->ipehr);
  321. error_print_instdone(m, ee);
  322. if (ee->batchbuffer) {
  323. u64 start = ee->batchbuffer->gtt_offset;
  324. u64 end = start + ee->batchbuffer->gtt_size;
  325. err_printf(m, " batch: [0x%08x_%08x, 0x%08x_%08x]\n",
  326. upper_32_bits(start), lower_32_bits(start),
  327. upper_32_bits(end), lower_32_bits(end));
  328. }
  329. if (INTEL_GEN(m->i915) >= 4) {
  330. err_printf(m, " BBADDR: 0x%08x_%08x\n",
  331. (u32)(ee->bbaddr>>32), (u32)ee->bbaddr);
  332. err_printf(m, " BB_STATE: 0x%08x\n", ee->bbstate);
  333. err_printf(m, " INSTPS: 0x%08x\n", ee->instps);
  334. }
  335. err_printf(m, " INSTPM: 0x%08x\n", ee->instpm);
  336. err_printf(m, " FADDR: 0x%08x %08x\n", upper_32_bits(ee->faddr),
  337. lower_32_bits(ee->faddr));
  338. if (INTEL_GEN(m->i915) >= 6) {
  339. err_printf(m, " RC PSMI: 0x%08x\n", ee->rc_psmi);
  340. err_printf(m, " FAULT_REG: 0x%08x\n", ee->fault_reg);
  341. err_printf(m, " SYNC_0: 0x%08x\n",
  342. ee->semaphore_mboxes[0]);
  343. err_printf(m, " SYNC_1: 0x%08x\n",
  344. ee->semaphore_mboxes[1]);
  345. if (HAS_VEBOX(m->i915))
  346. err_printf(m, " SYNC_2: 0x%08x\n",
  347. ee->semaphore_mboxes[2]);
  348. }
  349. if (USES_PPGTT(m->i915)) {
  350. err_printf(m, " GFX_MODE: 0x%08x\n", ee->vm_info.gfx_mode);
  351. if (INTEL_GEN(m->i915) >= 8) {
  352. int i;
  353. for (i = 0; i < 4; i++)
  354. err_printf(m, " PDP%d: 0x%016llx\n",
  355. i, ee->vm_info.pdp[i]);
  356. } else {
  357. err_printf(m, " PP_DIR_BASE: 0x%08x\n",
  358. ee->vm_info.pp_dir_base);
  359. }
  360. }
  361. err_printf(m, " seqno: 0x%08x\n", ee->seqno);
  362. err_printf(m, " last_seqno: 0x%08x\n", ee->last_seqno);
  363. err_printf(m, " waiting: %s\n", yesno(ee->waiting));
  364. err_printf(m, " ring->head: 0x%08x\n", ee->cpu_ring_head);
  365. err_printf(m, " ring->tail: 0x%08x\n", ee->cpu_ring_tail);
  366. err_printf(m, " hangcheck: %s [%d]\n",
  367. hangcheck_action_to_str(ee->hangcheck_action),
  368. ee->hangcheck_score);
  369. error_print_request(m, " ELSP[0]: ", &ee->execlist[0]);
  370. error_print_request(m, " ELSP[1]: ", &ee->execlist[1]);
  371. }
  372. void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
  373. {
  374. va_list args;
  375. va_start(args, f);
  376. i915_error_vprintf(e, f, args);
  377. va_end(args);
  378. }
  379. static int
  380. ascii85_encode_len(int len)
  381. {
  382. return DIV_ROUND_UP(len, 4);
  383. }
  384. static bool
  385. ascii85_encode(u32 in, char *out)
  386. {
  387. int i;
  388. if (in == 0)
  389. return false;
  390. out[5] = '\0';
  391. for (i = 5; i--; ) {
  392. out[i] = '!' + in % 85;
  393. in /= 85;
  394. }
  395. return true;
  396. }
  397. static void print_error_obj(struct drm_i915_error_state_buf *m,
  398. struct intel_engine_cs *engine,
  399. const char *name,
  400. struct drm_i915_error_object *obj)
  401. {
  402. char out[6];
  403. int page;
  404. if (!obj)
  405. return;
  406. if (name) {
  407. err_printf(m, "%s --- %s = 0x%08x %08x\n",
  408. engine ? engine->name : "global", name,
  409. upper_32_bits(obj->gtt_offset),
  410. lower_32_bits(obj->gtt_offset));
  411. }
  412. err_compression_marker(m);
  413. for (page = 0; page < obj->page_count; page++) {
  414. int i, len;
  415. len = PAGE_SIZE;
  416. if (page == obj->page_count - 1)
  417. len -= obj->unused;
  418. len = ascii85_encode_len(len);
  419. for (i = 0; i < len; i++) {
  420. if (ascii85_encode(obj->pages[page][i], out))
  421. err_puts(m, out);
  422. else
  423. err_puts(m, "z");
  424. }
  425. }
  426. err_puts(m, "\n");
  427. }
  428. static void err_print_capabilities(struct drm_i915_error_state_buf *m,
  429. const struct intel_device_info *info)
  430. {
  431. #define PRINT_FLAG(x) err_printf(m, #x ": %s\n", yesno(info->x))
  432. DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG);
  433. #undef PRINT_FLAG
  434. }
  435. int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
  436. const struct i915_error_state_file_priv *error_priv)
  437. {
  438. struct drm_i915_private *dev_priv = to_i915(error_priv->dev);
  439. struct pci_dev *pdev = dev_priv->drm.pdev;
  440. struct drm_i915_error_state *error = error_priv->error;
  441. struct drm_i915_error_object *obj;
  442. int max_hangcheck_score;
  443. int i, j;
  444. if (!error) {
  445. err_printf(m, "no error state collected\n");
  446. goto out;
  447. }
  448. err_printf(m, "%s\n", error->error_msg);
  449. err_printf(m, "Kernel: " UTS_RELEASE "\n");
  450. err_printf(m, "Time: %ld s %ld us\n",
  451. error->time.tv_sec, error->time.tv_usec);
  452. err_printf(m, "Boottime: %ld s %ld us\n",
  453. error->boottime.tv_sec, error->boottime.tv_usec);
  454. err_printf(m, "Uptime: %ld s %ld us\n",
  455. error->uptime.tv_sec, error->uptime.tv_usec);
  456. err_print_capabilities(m, &error->device_info);
  457. max_hangcheck_score = 0;
  458. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  459. if (error->engine[i].hangcheck_score > max_hangcheck_score)
  460. max_hangcheck_score = error->engine[i].hangcheck_score;
  461. }
  462. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  463. if (error->engine[i].hangcheck_score == max_hangcheck_score &&
  464. error->engine[i].pid != -1) {
  465. err_printf(m, "Active process (on ring %s): %s [%d]\n",
  466. engine_str(i),
  467. error->engine[i].comm,
  468. error->engine[i].pid);
  469. }
  470. }
  471. err_printf(m, "Reset count: %u\n", error->reset_count);
  472. err_printf(m, "Suspend count: %u\n", error->suspend_count);
  473. err_printf(m, "PCI ID: 0x%04x\n", pdev->device);
  474. err_printf(m, "PCI Revision: 0x%02x\n", pdev->revision);
  475. err_printf(m, "PCI Subsystem: %04x:%04x\n",
  476. pdev->subsystem_vendor,
  477. pdev->subsystem_device);
  478. err_printf(m, "IOMMU enabled?: %d\n", error->iommu);
  479. if (HAS_CSR(dev_priv)) {
  480. struct intel_csr *csr = &dev_priv->csr;
  481. err_printf(m, "DMC loaded: %s\n",
  482. yesno(csr->dmc_payload != NULL));
  483. err_printf(m, "DMC fw version: %d.%d\n",
  484. CSR_VERSION_MAJOR(csr->version),
  485. CSR_VERSION_MINOR(csr->version));
  486. }
  487. err_printf(m, "EIR: 0x%08x\n", error->eir);
  488. err_printf(m, "IER: 0x%08x\n", error->ier);
  489. if (INTEL_GEN(dev_priv) >= 8) {
  490. for (i = 0; i < 4; i++)
  491. err_printf(m, "GTIER gt %d: 0x%08x\n", i,
  492. error->gtier[i]);
  493. } else if (HAS_PCH_SPLIT(dev_priv) || IS_VALLEYVIEW(dev_priv))
  494. err_printf(m, "GTIER: 0x%08x\n", error->gtier[0]);
  495. err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
  496. err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
  497. err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
  498. err_printf(m, "CCID: 0x%08x\n", error->ccid);
  499. err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
  500. for (i = 0; i < dev_priv->num_fence_regs; i++)
  501. err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
  502. if (INTEL_GEN(dev_priv) >= 6) {
  503. err_printf(m, "ERROR: 0x%08x\n", error->error);
  504. if (INTEL_GEN(dev_priv) >= 8)
  505. err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n",
  506. error->fault_data1, error->fault_data0);
  507. err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
  508. }
  509. if (IS_GEN7(dev_priv))
  510. err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
  511. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  512. if (error->engine[i].engine_id != -1)
  513. error_print_engine(m, &error->engine[i]);
  514. }
  515. for (i = 0; i < ARRAY_SIZE(error->active_vm); i++) {
  516. char buf[128];
  517. int len, first = 1;
  518. if (!error->active_vm[i])
  519. break;
  520. len = scnprintf(buf, sizeof(buf), "Active (");
  521. for (j = 0; j < ARRAY_SIZE(error->engine); j++) {
  522. if (error->engine[j].vm != error->active_vm[i])
  523. continue;
  524. len += scnprintf(buf + len, sizeof(buf), "%s%s",
  525. first ? "" : ", ",
  526. dev_priv->engine[j]->name);
  527. first = 0;
  528. }
  529. scnprintf(buf + len, sizeof(buf), ")");
  530. print_error_buffers(m, buf,
  531. error->active_bo[i],
  532. error->active_bo_count[i]);
  533. }
  534. print_error_buffers(m, "Pinned (global)",
  535. error->pinned_bo,
  536. error->pinned_bo_count);
  537. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  538. struct drm_i915_error_engine *ee = &error->engine[i];
  539. obj = ee->batchbuffer;
  540. if (obj) {
  541. err_puts(m, dev_priv->engine[i]->name);
  542. if (ee->pid != -1)
  543. err_printf(m, " (submitted by %s [%d])",
  544. ee->comm,
  545. ee->pid);
  546. err_printf(m, " --- gtt_offset = 0x%08x %08x\n",
  547. upper_32_bits(obj->gtt_offset),
  548. lower_32_bits(obj->gtt_offset));
  549. print_error_obj(m, dev_priv->engine[i], NULL, obj);
  550. }
  551. if (ee->num_requests) {
  552. err_printf(m, "%s --- %d requests\n",
  553. dev_priv->engine[i]->name,
  554. ee->num_requests);
  555. for (j = 0; j < ee->num_requests; j++)
  556. error_print_request(m, " ", &ee->requests[j]);
  557. }
  558. if (IS_ERR(ee->waiters)) {
  559. err_printf(m, "%s --- ? waiters [unable to acquire spinlock]\n",
  560. dev_priv->engine[i]->name);
  561. } else if (ee->num_waiters) {
  562. err_printf(m, "%s --- %d waiters\n",
  563. dev_priv->engine[i]->name,
  564. ee->num_waiters);
  565. for (j = 0; j < ee->num_waiters; j++) {
  566. err_printf(m, " seqno 0x%08x for %s [%d]\n",
  567. ee->waiters[j].seqno,
  568. ee->waiters[j].comm,
  569. ee->waiters[j].pid);
  570. }
  571. }
  572. print_error_obj(m, dev_priv->engine[i],
  573. "ringbuffer", ee->ringbuffer);
  574. print_error_obj(m, dev_priv->engine[i],
  575. "HW Status", ee->hws_page);
  576. print_error_obj(m, dev_priv->engine[i],
  577. "HW context", ee->ctx);
  578. print_error_obj(m, dev_priv->engine[i],
  579. "WA context", ee->wa_ctx);
  580. print_error_obj(m, dev_priv->engine[i],
  581. "WA batchbuffer", ee->wa_batchbuffer);
  582. }
  583. print_error_obj(m, NULL, "Semaphores", error->semaphore);
  584. print_error_obj(m, NULL, "GuC log buffer", error->guc_log);
  585. if (error->overlay)
  586. intel_overlay_print_error_state(m, error->overlay);
  587. if (error->display)
  588. intel_display_print_error_state(m, dev_priv, error->display);
  589. out:
  590. if (m->bytes == 0 && m->err)
  591. return m->err;
  592. return 0;
  593. }
  594. int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
  595. struct drm_i915_private *i915,
  596. size_t count, loff_t pos)
  597. {
  598. memset(ebuf, 0, sizeof(*ebuf));
  599. ebuf->i915 = i915;
  600. /* We need to have enough room to store any i915_error_state printf
  601. * so that we can move it to start position.
  602. */
  603. ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
  604. ebuf->buf = kmalloc(ebuf->size,
  605. GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
  606. if (ebuf->buf == NULL) {
  607. ebuf->size = PAGE_SIZE;
  608. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  609. }
  610. if (ebuf->buf == NULL) {
  611. ebuf->size = 128;
  612. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  613. }
  614. if (ebuf->buf == NULL)
  615. return -ENOMEM;
  616. ebuf->start = pos;
  617. return 0;
  618. }
  619. static void i915_error_object_free(struct drm_i915_error_object *obj)
  620. {
  621. int page;
  622. if (obj == NULL)
  623. return;
  624. for (page = 0; page < obj->page_count; page++)
  625. free_page((unsigned long)obj->pages[page]);
  626. kfree(obj);
  627. }
  628. static void i915_error_state_free(struct kref *error_ref)
  629. {
  630. struct drm_i915_error_state *error = container_of(error_ref,
  631. typeof(*error), ref);
  632. int i;
  633. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  634. struct drm_i915_error_engine *ee = &error->engine[i];
  635. i915_error_object_free(ee->batchbuffer);
  636. i915_error_object_free(ee->wa_batchbuffer);
  637. i915_error_object_free(ee->ringbuffer);
  638. i915_error_object_free(ee->hws_page);
  639. i915_error_object_free(ee->ctx);
  640. i915_error_object_free(ee->wa_ctx);
  641. kfree(ee->requests);
  642. if (!IS_ERR_OR_NULL(ee->waiters))
  643. kfree(ee->waiters);
  644. }
  645. i915_error_object_free(error->semaphore);
  646. i915_error_object_free(error->guc_log);
  647. for (i = 0; i < ARRAY_SIZE(error->active_bo); i++)
  648. kfree(error->active_bo[i]);
  649. kfree(error->pinned_bo);
  650. kfree(error->overlay);
  651. kfree(error->display);
  652. kfree(error);
  653. }
  654. static struct drm_i915_error_object *
  655. i915_error_object_create(struct drm_i915_private *i915,
  656. struct i915_vma *vma)
  657. {
  658. struct i915_ggtt *ggtt = &i915->ggtt;
  659. const u64 slot = ggtt->error_capture.start;
  660. struct drm_i915_error_object *dst;
  661. struct z_stream_s zstream;
  662. unsigned long num_pages;
  663. struct sgt_iter iter;
  664. dma_addr_t dma;
  665. if (!vma)
  666. return NULL;
  667. num_pages = min_t(u64, vma->size, vma->obj->base.size) >> PAGE_SHIFT;
  668. num_pages = DIV_ROUND_UP(10 * num_pages, 8); /* worstcase zlib growth */
  669. dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *),
  670. GFP_ATOMIC | __GFP_NOWARN);
  671. if (!dst)
  672. return NULL;
  673. dst->gtt_offset = vma->node.start;
  674. dst->gtt_size = vma->node.size;
  675. dst->page_count = 0;
  676. dst->unused = 0;
  677. if (!compress_init(&zstream)) {
  678. kfree(dst);
  679. return NULL;
  680. }
  681. for_each_sgt_dma(dma, iter, vma->pages) {
  682. void __iomem *s;
  683. int ret;
  684. ggtt->base.insert_page(&ggtt->base, dma, slot,
  685. I915_CACHE_NONE, 0);
  686. s = io_mapping_map_atomic_wc(&ggtt->mappable, slot);
  687. ret = compress_page(&zstream, (void __force *)s, dst);
  688. io_mapping_unmap_atomic(s);
  689. if (ret)
  690. goto unwind;
  691. }
  692. goto out;
  693. unwind:
  694. while (dst->page_count--)
  695. free_page((unsigned long)dst->pages[dst->page_count]);
  696. kfree(dst);
  697. dst = NULL;
  698. out:
  699. compress_fini(&zstream, dst);
  700. ggtt->base.clear_range(&ggtt->base, slot, PAGE_SIZE);
  701. return dst;
  702. }
  703. /* The error capture is special as tries to run underneath the normal
  704. * locking rules - so we use the raw version of the i915_gem_active lookup.
  705. */
  706. static inline uint32_t
  707. __active_get_seqno(struct i915_gem_active *active)
  708. {
  709. struct drm_i915_gem_request *request;
  710. request = __i915_gem_active_peek(active);
  711. return request ? request->global_seqno : 0;
  712. }
  713. static inline int
  714. __active_get_engine_id(struct i915_gem_active *active)
  715. {
  716. struct drm_i915_gem_request *request;
  717. request = __i915_gem_active_peek(active);
  718. return request ? request->engine->id : -1;
  719. }
  720. static void capture_bo(struct drm_i915_error_buffer *err,
  721. struct i915_vma *vma)
  722. {
  723. struct drm_i915_gem_object *obj = vma->obj;
  724. int i;
  725. err->size = obj->base.size;
  726. err->name = obj->base.name;
  727. for (i = 0; i < I915_NUM_ENGINES; i++)
  728. err->rseqno[i] = __active_get_seqno(&vma->last_read[i]);
  729. err->wseqno = __active_get_seqno(&obj->frontbuffer_write);
  730. err->engine = __active_get_engine_id(&obj->frontbuffer_write);
  731. err->gtt_offset = vma->node.start;
  732. err->read_domains = obj->base.read_domains;
  733. err->write_domain = obj->base.write_domain;
  734. err->fence_reg = vma->fence ? vma->fence->id : -1;
  735. err->tiling = i915_gem_object_get_tiling(obj);
  736. err->dirty = obj->mm.dirty;
  737. err->purgeable = obj->mm.madv != I915_MADV_WILLNEED;
  738. err->userptr = obj->userptr.mm != NULL;
  739. err->cache_level = obj->cache_level;
  740. }
  741. static u32 capture_error_bo(struct drm_i915_error_buffer *err,
  742. int count, struct list_head *head,
  743. bool pinned_only)
  744. {
  745. struct i915_vma *vma;
  746. int i = 0;
  747. list_for_each_entry(vma, head, vm_link) {
  748. if (pinned_only && !i915_vma_is_pinned(vma))
  749. continue;
  750. capture_bo(err++, vma);
  751. if (++i == count)
  752. break;
  753. }
  754. return i;
  755. }
  756. /* Generate a semi-unique error code. The code is not meant to have meaning, The
  757. * code's only purpose is to try to prevent false duplicated bug reports by
  758. * grossly estimating a GPU error state.
  759. *
  760. * TODO Ideally, hashing the batchbuffer would be a very nice way to determine
  761. * the hang if we could strip the GTT offset information from it.
  762. *
  763. * It's only a small step better than a random number in its current form.
  764. */
  765. static uint32_t i915_error_generate_code(struct drm_i915_private *dev_priv,
  766. struct drm_i915_error_state *error,
  767. int *engine_id)
  768. {
  769. uint32_t error_code = 0;
  770. int i;
  771. /* IPEHR would be an ideal way to detect errors, as it's the gross
  772. * measure of "the command that hung." However, has some very common
  773. * synchronization commands which almost always appear in the case
  774. * strictly a client bug. Use instdone to differentiate those some.
  775. */
  776. for (i = 0; i < I915_NUM_ENGINES; i++) {
  777. if (error->engine[i].hangcheck_action == HANGCHECK_HUNG) {
  778. if (engine_id)
  779. *engine_id = i;
  780. return error->engine[i].ipehr ^
  781. error->engine[i].instdone.instdone;
  782. }
  783. }
  784. return error_code;
  785. }
  786. static void i915_gem_record_fences(struct drm_i915_private *dev_priv,
  787. struct drm_i915_error_state *error)
  788. {
  789. int i;
  790. if (IS_GEN3(dev_priv) || IS_GEN2(dev_priv)) {
  791. for (i = 0; i < dev_priv->num_fence_regs; i++)
  792. error->fence[i] = I915_READ(FENCE_REG(i));
  793. } else if (IS_GEN5(dev_priv) || IS_GEN4(dev_priv)) {
  794. for (i = 0; i < dev_priv->num_fence_regs; i++)
  795. error->fence[i] = I915_READ64(FENCE_REG_965_LO(i));
  796. } else if (INTEL_GEN(dev_priv) >= 6) {
  797. for (i = 0; i < dev_priv->num_fence_regs; i++)
  798. error->fence[i] = I915_READ64(FENCE_REG_GEN6_LO(i));
  799. }
  800. }
  801. static inline u32
  802. gen8_engine_sync_index(struct intel_engine_cs *engine,
  803. struct intel_engine_cs *other)
  804. {
  805. int idx;
  806. /*
  807. * rcs -> 0 = vcs, 1 = bcs, 2 = vecs, 3 = vcs2;
  808. * vcs -> 0 = bcs, 1 = vecs, 2 = vcs2, 3 = rcs;
  809. * bcs -> 0 = vecs, 1 = vcs2. 2 = rcs, 3 = vcs;
  810. * vecs -> 0 = vcs2, 1 = rcs, 2 = vcs, 3 = bcs;
  811. * vcs2 -> 0 = rcs, 1 = vcs, 2 = bcs, 3 = vecs;
  812. */
  813. idx = (other - engine) - 1;
  814. if (idx < 0)
  815. idx += I915_NUM_ENGINES;
  816. return idx;
  817. }
  818. static void gen8_record_semaphore_state(struct drm_i915_error_state *error,
  819. struct intel_engine_cs *engine,
  820. struct drm_i915_error_engine *ee)
  821. {
  822. struct drm_i915_private *dev_priv = engine->i915;
  823. struct intel_engine_cs *to;
  824. enum intel_engine_id id;
  825. if (!error->semaphore)
  826. return;
  827. for_each_engine(to, dev_priv, id) {
  828. int idx;
  829. u16 signal_offset;
  830. u32 *tmp;
  831. if (engine == to)
  832. continue;
  833. signal_offset =
  834. (GEN8_SIGNAL_OFFSET(engine, id) & (PAGE_SIZE - 1)) / 4;
  835. tmp = error->semaphore->pages[0];
  836. idx = gen8_engine_sync_index(engine, to);
  837. ee->semaphore_mboxes[idx] = tmp[signal_offset];
  838. }
  839. }
  840. static void gen6_record_semaphore_state(struct intel_engine_cs *engine,
  841. struct drm_i915_error_engine *ee)
  842. {
  843. struct drm_i915_private *dev_priv = engine->i915;
  844. ee->semaphore_mboxes[0] = I915_READ(RING_SYNC_0(engine->mmio_base));
  845. ee->semaphore_mboxes[1] = I915_READ(RING_SYNC_1(engine->mmio_base));
  846. if (HAS_VEBOX(dev_priv))
  847. ee->semaphore_mboxes[2] =
  848. I915_READ(RING_SYNC_2(engine->mmio_base));
  849. }
  850. static void error_record_engine_waiters(struct intel_engine_cs *engine,
  851. struct drm_i915_error_engine *ee)
  852. {
  853. struct intel_breadcrumbs *b = &engine->breadcrumbs;
  854. struct drm_i915_error_waiter *waiter;
  855. struct rb_node *rb;
  856. int count;
  857. ee->num_waiters = 0;
  858. ee->waiters = NULL;
  859. if (RB_EMPTY_ROOT(&b->waiters))
  860. return;
  861. if (!spin_trylock_irq(&b->lock)) {
  862. ee->waiters = ERR_PTR(-EDEADLK);
  863. return;
  864. }
  865. count = 0;
  866. for (rb = rb_first(&b->waiters); rb != NULL; rb = rb_next(rb))
  867. count++;
  868. spin_unlock_irq(&b->lock);
  869. waiter = NULL;
  870. if (count)
  871. waiter = kmalloc_array(count,
  872. sizeof(struct drm_i915_error_waiter),
  873. GFP_ATOMIC);
  874. if (!waiter)
  875. return;
  876. if (!spin_trylock_irq(&b->lock)) {
  877. kfree(waiter);
  878. ee->waiters = ERR_PTR(-EDEADLK);
  879. return;
  880. }
  881. ee->waiters = waiter;
  882. for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
  883. struct intel_wait *w = container_of(rb, typeof(*w), node);
  884. strcpy(waiter->comm, w->tsk->comm);
  885. waiter->pid = w->tsk->pid;
  886. waiter->seqno = w->seqno;
  887. waiter++;
  888. if (++ee->num_waiters == count)
  889. break;
  890. }
  891. spin_unlock_irq(&b->lock);
  892. }
  893. static void error_record_engine_registers(struct drm_i915_error_state *error,
  894. struct intel_engine_cs *engine,
  895. struct drm_i915_error_engine *ee)
  896. {
  897. struct drm_i915_private *dev_priv = engine->i915;
  898. if (INTEL_GEN(dev_priv) >= 6) {
  899. ee->rc_psmi = I915_READ(RING_PSMI_CTL(engine->mmio_base));
  900. ee->fault_reg = I915_READ(RING_FAULT_REG(engine));
  901. if (INTEL_GEN(dev_priv) >= 8)
  902. gen8_record_semaphore_state(error, engine, ee);
  903. else
  904. gen6_record_semaphore_state(engine, ee);
  905. }
  906. if (INTEL_GEN(dev_priv) >= 4) {
  907. ee->faddr = I915_READ(RING_DMA_FADD(engine->mmio_base));
  908. ee->ipeir = I915_READ(RING_IPEIR(engine->mmio_base));
  909. ee->ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
  910. ee->instps = I915_READ(RING_INSTPS(engine->mmio_base));
  911. ee->bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
  912. if (INTEL_GEN(dev_priv) >= 8) {
  913. ee->faddr |= (u64) I915_READ(RING_DMA_FADD_UDW(engine->mmio_base)) << 32;
  914. ee->bbaddr |= (u64) I915_READ(RING_BBADDR_UDW(engine->mmio_base)) << 32;
  915. }
  916. ee->bbstate = I915_READ(RING_BBSTATE(engine->mmio_base));
  917. } else {
  918. ee->faddr = I915_READ(DMA_FADD_I8XX);
  919. ee->ipeir = I915_READ(IPEIR);
  920. ee->ipehr = I915_READ(IPEHR);
  921. }
  922. intel_engine_get_instdone(engine, &ee->instdone);
  923. ee->waiting = intel_engine_has_waiter(engine);
  924. ee->instpm = I915_READ(RING_INSTPM(engine->mmio_base));
  925. ee->acthd = intel_engine_get_active_head(engine);
  926. ee->seqno = intel_engine_get_seqno(engine);
  927. ee->last_seqno = intel_engine_last_submit(engine);
  928. ee->start = I915_READ_START(engine);
  929. ee->head = I915_READ_HEAD(engine);
  930. ee->tail = I915_READ_TAIL(engine);
  931. ee->ctl = I915_READ_CTL(engine);
  932. if (INTEL_GEN(dev_priv) > 2)
  933. ee->mode = I915_READ_MODE(engine);
  934. if (!HWS_NEEDS_PHYSICAL(dev_priv)) {
  935. i915_reg_t mmio;
  936. if (IS_GEN7(dev_priv)) {
  937. switch (engine->id) {
  938. default:
  939. case RCS:
  940. mmio = RENDER_HWS_PGA_GEN7;
  941. break;
  942. case BCS:
  943. mmio = BLT_HWS_PGA_GEN7;
  944. break;
  945. case VCS:
  946. mmio = BSD_HWS_PGA_GEN7;
  947. break;
  948. case VECS:
  949. mmio = VEBOX_HWS_PGA_GEN7;
  950. break;
  951. }
  952. } else if (IS_GEN6(engine->i915)) {
  953. mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
  954. } else {
  955. /* XXX: gen8 returns to sanity */
  956. mmio = RING_HWS_PGA(engine->mmio_base);
  957. }
  958. ee->hws = I915_READ(mmio);
  959. }
  960. ee->hangcheck_score = engine->hangcheck.score;
  961. ee->hangcheck_action = engine->hangcheck.action;
  962. if (USES_PPGTT(dev_priv)) {
  963. int i;
  964. ee->vm_info.gfx_mode = I915_READ(RING_MODE_GEN7(engine));
  965. if (IS_GEN6(dev_priv))
  966. ee->vm_info.pp_dir_base =
  967. I915_READ(RING_PP_DIR_BASE_READ(engine));
  968. else if (IS_GEN7(dev_priv))
  969. ee->vm_info.pp_dir_base =
  970. I915_READ(RING_PP_DIR_BASE(engine));
  971. else if (INTEL_GEN(dev_priv) >= 8)
  972. for (i = 0; i < 4; i++) {
  973. ee->vm_info.pdp[i] =
  974. I915_READ(GEN8_RING_PDP_UDW(engine, i));
  975. ee->vm_info.pdp[i] <<= 32;
  976. ee->vm_info.pdp[i] |=
  977. I915_READ(GEN8_RING_PDP_LDW(engine, i));
  978. }
  979. }
  980. }
  981. static void record_request(struct drm_i915_gem_request *request,
  982. struct drm_i915_error_request *erq)
  983. {
  984. erq->context = request->ctx->hw_id;
  985. erq->seqno = request->global_seqno;
  986. erq->jiffies = request->emitted_jiffies;
  987. erq->head = request->head;
  988. erq->tail = request->tail;
  989. rcu_read_lock();
  990. erq->pid = request->ctx->pid ? pid_nr(request->ctx->pid) : 0;
  991. rcu_read_unlock();
  992. }
  993. static void engine_record_requests(struct intel_engine_cs *engine,
  994. struct drm_i915_gem_request *first,
  995. struct drm_i915_error_engine *ee)
  996. {
  997. struct drm_i915_gem_request *request;
  998. int count;
  999. count = 0;
  1000. request = first;
  1001. list_for_each_entry_from(request, &engine->timeline->requests, link)
  1002. count++;
  1003. if (!count)
  1004. return;
  1005. ee->requests = kcalloc(count, sizeof(*ee->requests), GFP_ATOMIC);
  1006. if (!ee->requests)
  1007. return;
  1008. ee->num_requests = count;
  1009. count = 0;
  1010. request = first;
  1011. list_for_each_entry_from(request, &engine->timeline->requests, link) {
  1012. if (count >= ee->num_requests) {
  1013. /*
  1014. * If the ring request list was changed in
  1015. * between the point where the error request
  1016. * list was created and dimensioned and this
  1017. * point then just exit early to avoid crashes.
  1018. *
  1019. * We don't need to communicate that the
  1020. * request list changed state during error
  1021. * state capture and that the error state is
  1022. * slightly incorrect as a consequence since we
  1023. * are typically only interested in the request
  1024. * list state at the point of error state
  1025. * capture, not in any changes happening during
  1026. * the capture.
  1027. */
  1028. break;
  1029. }
  1030. record_request(request, &ee->requests[count++]);
  1031. }
  1032. ee->num_requests = count;
  1033. }
  1034. static void error_record_engine_execlists(struct intel_engine_cs *engine,
  1035. struct drm_i915_error_engine *ee)
  1036. {
  1037. unsigned int n;
  1038. for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++)
  1039. if (engine->execlist_port[n].request)
  1040. record_request(engine->execlist_port[n].request,
  1041. &ee->execlist[n]);
  1042. }
  1043. static void i915_gem_record_rings(struct drm_i915_private *dev_priv,
  1044. struct drm_i915_error_state *error)
  1045. {
  1046. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1047. int i;
  1048. error->semaphore =
  1049. i915_error_object_create(dev_priv, dev_priv->semaphore);
  1050. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1051. struct intel_engine_cs *engine = dev_priv->engine[i];
  1052. struct drm_i915_error_engine *ee = &error->engine[i];
  1053. struct drm_i915_gem_request *request;
  1054. ee->pid = -1;
  1055. ee->engine_id = -1;
  1056. if (!engine)
  1057. continue;
  1058. ee->engine_id = i;
  1059. error_record_engine_registers(error, engine, ee);
  1060. error_record_engine_waiters(engine, ee);
  1061. error_record_engine_execlists(engine, ee);
  1062. request = i915_gem_find_active_request(engine);
  1063. if (request) {
  1064. struct intel_ring *ring;
  1065. struct pid *pid;
  1066. ee->vm = request->ctx->ppgtt ?
  1067. &request->ctx->ppgtt->base : &ggtt->base;
  1068. /* We need to copy these to an anonymous buffer
  1069. * as the simplest method to avoid being overwritten
  1070. * by userspace.
  1071. */
  1072. ee->batchbuffer =
  1073. i915_error_object_create(dev_priv,
  1074. request->batch);
  1075. if (HAS_BROKEN_CS_TLB(dev_priv))
  1076. ee->wa_batchbuffer =
  1077. i915_error_object_create(dev_priv,
  1078. engine->scratch);
  1079. ee->ctx =
  1080. i915_error_object_create(dev_priv,
  1081. request->ctx->engine[i].state);
  1082. pid = request->ctx->pid;
  1083. if (pid) {
  1084. struct task_struct *task;
  1085. rcu_read_lock();
  1086. task = pid_task(pid, PIDTYPE_PID);
  1087. if (task) {
  1088. strcpy(ee->comm, task->comm);
  1089. ee->pid = task->pid;
  1090. }
  1091. rcu_read_unlock();
  1092. }
  1093. error->simulated |=
  1094. request->ctx->flags & CONTEXT_NO_ERROR_CAPTURE;
  1095. ee->rq_head = request->head;
  1096. ee->rq_post = request->postfix;
  1097. ee->rq_tail = request->tail;
  1098. ring = request->ring;
  1099. ee->cpu_ring_head = ring->head;
  1100. ee->cpu_ring_tail = ring->tail;
  1101. ee->ringbuffer =
  1102. i915_error_object_create(dev_priv, ring->vma);
  1103. engine_record_requests(engine, request, ee);
  1104. }
  1105. ee->hws_page =
  1106. i915_error_object_create(dev_priv,
  1107. engine->status_page.vma);
  1108. ee->wa_ctx =
  1109. i915_error_object_create(dev_priv, engine->wa_ctx.vma);
  1110. }
  1111. }
  1112. static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
  1113. struct drm_i915_error_state *error,
  1114. struct i915_address_space *vm,
  1115. int idx)
  1116. {
  1117. struct drm_i915_error_buffer *active_bo;
  1118. struct i915_vma *vma;
  1119. int count;
  1120. count = 0;
  1121. list_for_each_entry(vma, &vm->active_list, vm_link)
  1122. count++;
  1123. active_bo = NULL;
  1124. if (count)
  1125. active_bo = kcalloc(count, sizeof(*active_bo), GFP_ATOMIC);
  1126. if (active_bo)
  1127. count = capture_error_bo(active_bo, count, &vm->active_list, false);
  1128. else
  1129. count = 0;
  1130. error->active_vm[idx] = vm;
  1131. error->active_bo[idx] = active_bo;
  1132. error->active_bo_count[idx] = count;
  1133. }
  1134. static void i915_capture_active_buffers(struct drm_i915_private *dev_priv,
  1135. struct drm_i915_error_state *error)
  1136. {
  1137. int cnt = 0, i, j;
  1138. BUILD_BUG_ON(ARRAY_SIZE(error->engine) > ARRAY_SIZE(error->active_bo));
  1139. BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_vm));
  1140. BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_bo_count));
  1141. /* Scan each engine looking for unique active contexts/vm */
  1142. for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
  1143. struct drm_i915_error_engine *ee = &error->engine[i];
  1144. bool found;
  1145. if (!ee->vm)
  1146. continue;
  1147. found = false;
  1148. for (j = 0; j < i && !found; j++)
  1149. found = error->engine[j].vm == ee->vm;
  1150. if (!found)
  1151. i915_gem_capture_vm(dev_priv, error, ee->vm, cnt++);
  1152. }
  1153. }
  1154. static void i915_capture_pinned_buffers(struct drm_i915_private *dev_priv,
  1155. struct drm_i915_error_state *error)
  1156. {
  1157. struct i915_address_space *vm = &dev_priv->ggtt.base;
  1158. struct drm_i915_error_buffer *bo;
  1159. struct i915_vma *vma;
  1160. int count_inactive, count_active;
  1161. count_inactive = 0;
  1162. list_for_each_entry(vma, &vm->active_list, vm_link)
  1163. count_inactive++;
  1164. count_active = 0;
  1165. list_for_each_entry(vma, &vm->inactive_list, vm_link)
  1166. count_active++;
  1167. bo = NULL;
  1168. if (count_inactive + count_active)
  1169. bo = kcalloc(count_inactive + count_active,
  1170. sizeof(*bo), GFP_ATOMIC);
  1171. if (!bo)
  1172. return;
  1173. count_inactive = capture_error_bo(bo, count_inactive,
  1174. &vm->active_list, true);
  1175. count_active = capture_error_bo(bo + count_inactive, count_active,
  1176. &vm->inactive_list, true);
  1177. error->pinned_bo_count = count_inactive + count_active;
  1178. error->pinned_bo = bo;
  1179. }
  1180. static void i915_gem_capture_guc_log_buffer(struct drm_i915_private *dev_priv,
  1181. struct drm_i915_error_state *error)
  1182. {
  1183. /* Capturing log buf contents won't be useful if logging was disabled */
  1184. if (!dev_priv->guc.log.vma || (i915.guc_log_level < 0))
  1185. return;
  1186. error->guc_log = i915_error_object_create(dev_priv,
  1187. dev_priv->guc.log.vma);
  1188. }
  1189. /* Capture all registers which don't fit into another category. */
  1190. static void i915_capture_reg_state(struct drm_i915_private *dev_priv,
  1191. struct drm_i915_error_state *error)
  1192. {
  1193. int i;
  1194. /* General organization
  1195. * 1. Registers specific to a single generation
  1196. * 2. Registers which belong to multiple generations
  1197. * 3. Feature specific registers.
  1198. * 4. Everything else
  1199. * Please try to follow the order.
  1200. */
  1201. /* 1: Registers specific to a single generation */
  1202. if (IS_VALLEYVIEW(dev_priv)) {
  1203. error->gtier[0] = I915_READ(GTIER);
  1204. error->ier = I915_READ(VLV_IER);
  1205. error->forcewake = I915_READ_FW(FORCEWAKE_VLV);
  1206. }
  1207. if (IS_GEN7(dev_priv))
  1208. error->err_int = I915_READ(GEN7_ERR_INT);
  1209. if (INTEL_GEN(dev_priv) >= 8) {
  1210. error->fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
  1211. error->fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
  1212. }
  1213. if (IS_GEN6(dev_priv)) {
  1214. error->forcewake = I915_READ_FW(FORCEWAKE);
  1215. error->gab_ctl = I915_READ(GAB_CTL);
  1216. error->gfx_mode = I915_READ(GFX_MODE);
  1217. }
  1218. /* 2: Registers which belong to multiple generations */
  1219. if (INTEL_GEN(dev_priv) >= 7)
  1220. error->forcewake = I915_READ_FW(FORCEWAKE_MT);
  1221. if (INTEL_GEN(dev_priv) >= 6) {
  1222. error->derrmr = I915_READ(DERRMR);
  1223. error->error = I915_READ(ERROR_GEN6);
  1224. error->done_reg = I915_READ(DONE_REG);
  1225. }
  1226. /* 3: Feature specific registers */
  1227. if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
  1228. error->gam_ecochk = I915_READ(GAM_ECOCHK);
  1229. error->gac_eco = I915_READ(GAC_ECO_BITS);
  1230. }
  1231. /* 4: Everything else */
  1232. if (HAS_HW_CONTEXTS(dev_priv))
  1233. error->ccid = I915_READ(CCID);
  1234. if (INTEL_GEN(dev_priv) >= 8) {
  1235. error->ier = I915_READ(GEN8_DE_MISC_IER);
  1236. for (i = 0; i < 4; i++)
  1237. error->gtier[i] = I915_READ(GEN8_GT_IER(i));
  1238. } else if (HAS_PCH_SPLIT(dev_priv)) {
  1239. error->ier = I915_READ(DEIER);
  1240. error->gtier[0] = I915_READ(GTIER);
  1241. } else if (IS_GEN2(dev_priv)) {
  1242. error->ier = I915_READ16(IER);
  1243. } else if (!IS_VALLEYVIEW(dev_priv)) {
  1244. error->ier = I915_READ(IER);
  1245. }
  1246. error->eir = I915_READ(EIR);
  1247. error->pgtbl_er = I915_READ(PGTBL_ER);
  1248. }
  1249. static void i915_error_capture_msg(struct drm_i915_private *dev_priv,
  1250. struct drm_i915_error_state *error,
  1251. u32 engine_mask,
  1252. const char *error_msg)
  1253. {
  1254. u32 ecode;
  1255. int engine_id = -1, len;
  1256. ecode = i915_error_generate_code(dev_priv, error, &engine_id);
  1257. len = scnprintf(error->error_msg, sizeof(error->error_msg),
  1258. "GPU HANG: ecode %d:%d:0x%08x",
  1259. INTEL_GEN(dev_priv), engine_id, ecode);
  1260. if (engine_id != -1 && error->engine[engine_id].pid != -1)
  1261. len += scnprintf(error->error_msg + len,
  1262. sizeof(error->error_msg) - len,
  1263. ", in %s [%d]",
  1264. error->engine[engine_id].comm,
  1265. error->engine[engine_id].pid);
  1266. scnprintf(error->error_msg + len, sizeof(error->error_msg) - len,
  1267. ", reason: %s, action: %s",
  1268. error_msg,
  1269. engine_mask ? "reset" : "continue");
  1270. }
  1271. static void i915_capture_gen_state(struct drm_i915_private *dev_priv,
  1272. struct drm_i915_error_state *error)
  1273. {
  1274. error->iommu = -1;
  1275. #ifdef CONFIG_INTEL_IOMMU
  1276. error->iommu = intel_iommu_gfx_mapped;
  1277. #endif
  1278. error->reset_count = i915_reset_count(&dev_priv->gpu_error);
  1279. error->suspend_count = dev_priv->suspend_count;
  1280. memcpy(&error->device_info,
  1281. INTEL_INFO(dev_priv),
  1282. sizeof(error->device_info));
  1283. }
  1284. static int capture(void *data)
  1285. {
  1286. struct drm_i915_error_state *error = data;
  1287. i915_capture_gen_state(error->i915, error);
  1288. i915_capture_reg_state(error->i915, error);
  1289. i915_gem_record_fences(error->i915, error);
  1290. i915_gem_record_rings(error->i915, error);
  1291. i915_capture_active_buffers(error->i915, error);
  1292. i915_capture_pinned_buffers(error->i915, error);
  1293. i915_gem_capture_guc_log_buffer(error->i915, error);
  1294. do_gettimeofday(&error->time);
  1295. error->boottime = ktime_to_timeval(ktime_get_boottime());
  1296. error->uptime =
  1297. ktime_to_timeval(ktime_sub(ktime_get(),
  1298. error->i915->gt.last_init_time));
  1299. error->overlay = intel_overlay_capture_error_state(error->i915);
  1300. error->display = intel_display_capture_error_state(error->i915);
  1301. return 0;
  1302. }
  1303. #define DAY_AS_SECONDS(x) (24 * 60 * 60 * (x))
  1304. /**
  1305. * i915_capture_error_state - capture an error record for later analysis
  1306. * @dev: drm device
  1307. *
  1308. * Should be called when an error is detected (either a hang or an error
  1309. * interrupt) to capture error state from the time of the error. Fills
  1310. * out a structure which becomes available in debugfs for user level tools
  1311. * to pick up.
  1312. */
  1313. void i915_capture_error_state(struct drm_i915_private *dev_priv,
  1314. u32 engine_mask,
  1315. const char *error_msg)
  1316. {
  1317. static bool warned;
  1318. struct drm_i915_error_state *error;
  1319. unsigned long flags;
  1320. if (!i915.error_capture)
  1321. return;
  1322. if (READ_ONCE(dev_priv->gpu_error.first_error))
  1323. return;
  1324. /* Account for pipe specific data like PIPE*STAT */
  1325. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  1326. if (!error) {
  1327. DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
  1328. return;
  1329. }
  1330. kref_init(&error->ref);
  1331. error->i915 = dev_priv;
  1332. stop_machine(capture, error, NULL);
  1333. i915_error_capture_msg(dev_priv, error, engine_mask, error_msg);
  1334. DRM_INFO("%s\n", error->error_msg);
  1335. if (!error->simulated) {
  1336. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  1337. if (!dev_priv->gpu_error.first_error) {
  1338. dev_priv->gpu_error.first_error = error;
  1339. error = NULL;
  1340. }
  1341. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  1342. }
  1343. if (error) {
  1344. i915_error_state_free(&error->ref);
  1345. return;
  1346. }
  1347. if (!warned &&
  1348. ktime_get_real_seconds() - DRIVER_TIMESTAMP < DAY_AS_SECONDS(180)) {
  1349. DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
  1350. DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
  1351. DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
  1352. DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
  1353. DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n",
  1354. dev_priv->drm.primary->index);
  1355. warned = true;
  1356. }
  1357. }
  1358. void i915_error_state_get(struct drm_device *dev,
  1359. struct i915_error_state_file_priv *error_priv)
  1360. {
  1361. struct drm_i915_private *dev_priv = to_i915(dev);
  1362. spin_lock_irq(&dev_priv->gpu_error.lock);
  1363. error_priv->error = dev_priv->gpu_error.first_error;
  1364. if (error_priv->error)
  1365. kref_get(&error_priv->error->ref);
  1366. spin_unlock_irq(&dev_priv->gpu_error.lock);
  1367. }
  1368. void i915_error_state_put(struct i915_error_state_file_priv *error_priv)
  1369. {
  1370. if (error_priv->error)
  1371. kref_put(&error_priv->error->ref, i915_error_state_free);
  1372. }
  1373. void i915_destroy_error_state(struct drm_device *dev)
  1374. {
  1375. struct drm_i915_private *dev_priv = to_i915(dev);
  1376. struct drm_i915_error_state *error;
  1377. spin_lock_irq(&dev_priv->gpu_error.lock);
  1378. error = dev_priv->gpu_error.first_error;
  1379. dev_priv->gpu_error.first_error = NULL;
  1380. spin_unlock_irq(&dev_priv->gpu_error.lock);
  1381. if (error)
  1382. kref_put(&error->ref, i915_error_state_free);
  1383. }