i915_gpu_error.c 41 KB

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