i915_gpu_error.c 43 KB

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