i915_gpu_error.c 38 KB

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