i915_gpu_error.c 46 KB

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