i915_gpu_error.c 41 KB

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