i915_gpu_error.c 41 KB

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