i915_gpu_error.c 42 KB

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