i915_gpu_error.c 50 KB

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