i915_gpu_error.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. default: return "";
  43. }
  44. }
  45. static const char *pin_flag(int pinned)
  46. {
  47. if (pinned > 0)
  48. return " P";
  49. else if (pinned < 0)
  50. return " p";
  51. else
  52. return "";
  53. }
  54. static const char *tiling_flag(int tiling)
  55. {
  56. switch (tiling) {
  57. default:
  58. case I915_TILING_NONE: return "";
  59. case I915_TILING_X: return " X";
  60. case I915_TILING_Y: return " Y";
  61. }
  62. }
  63. static const char *dirty_flag(int dirty)
  64. {
  65. return dirty ? " dirty" : "";
  66. }
  67. static const char *purgeable_flag(int purgeable)
  68. {
  69. return purgeable ? " purgeable" : "";
  70. }
  71. static bool __i915_error_ok(struct drm_i915_error_state_buf *e)
  72. {
  73. if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) {
  74. e->err = -ENOSPC;
  75. return false;
  76. }
  77. if (e->bytes == e->size - 1 || e->err)
  78. return false;
  79. return true;
  80. }
  81. static bool __i915_error_seek(struct drm_i915_error_state_buf *e,
  82. unsigned len)
  83. {
  84. if (e->pos + len <= e->start) {
  85. e->pos += len;
  86. return false;
  87. }
  88. /* First vsnprintf needs to fit in its entirety for memmove */
  89. if (len >= e->size) {
  90. e->err = -EIO;
  91. return false;
  92. }
  93. return true;
  94. }
  95. static void __i915_error_advance(struct drm_i915_error_state_buf *e,
  96. unsigned len)
  97. {
  98. /* If this is first printf in this window, adjust it so that
  99. * start position matches start of the buffer
  100. */
  101. if (e->pos < e->start) {
  102. const size_t off = e->start - e->pos;
  103. /* Should not happen but be paranoid */
  104. if (off > len || e->bytes) {
  105. e->err = -EIO;
  106. return;
  107. }
  108. memmove(e->buf, e->buf + off, len - off);
  109. e->bytes = len - off;
  110. e->pos = e->start;
  111. return;
  112. }
  113. e->bytes += len;
  114. e->pos += len;
  115. }
  116. static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
  117. const char *f, va_list args)
  118. {
  119. unsigned len;
  120. if (!__i915_error_ok(e))
  121. return;
  122. /* Seek the first printf which is hits start position */
  123. if (e->pos < e->start) {
  124. va_list tmp;
  125. va_copy(tmp, args);
  126. if (!__i915_error_seek(e, vsnprintf(NULL, 0, f, tmp)))
  127. return;
  128. }
  129. len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args);
  130. if (len >= e->size - e->bytes)
  131. len = e->size - e->bytes - 1;
  132. __i915_error_advance(e, len);
  133. }
  134. static void i915_error_puts(struct drm_i915_error_state_buf *e,
  135. const char *str)
  136. {
  137. unsigned len;
  138. if (!__i915_error_ok(e))
  139. return;
  140. len = strlen(str);
  141. /* Seek the first printf which is hits start position */
  142. if (e->pos < e->start) {
  143. if (!__i915_error_seek(e, len))
  144. return;
  145. }
  146. if (len >= e->size - e->bytes)
  147. len = e->size - e->bytes - 1;
  148. memcpy(e->buf + e->bytes, str, len);
  149. __i915_error_advance(e, len);
  150. }
  151. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  152. #define err_puts(e, s) i915_error_puts(e, s)
  153. static void print_error_buffers(struct drm_i915_error_state_buf *m,
  154. const char *name,
  155. struct drm_i915_error_buffer *err,
  156. int count)
  157. {
  158. err_printf(m, "%s [%d]:\n", name, count);
  159. while (count--) {
  160. err_printf(m, " %08x %8u %02x %02x %x %x",
  161. err->gtt_offset,
  162. err->size,
  163. err->read_domains,
  164. err->write_domain,
  165. err->rseqno, err->wseqno);
  166. err_puts(m, pin_flag(err->pinned));
  167. err_puts(m, tiling_flag(err->tiling));
  168. err_puts(m, dirty_flag(err->dirty));
  169. err_puts(m, purgeable_flag(err->purgeable));
  170. err_puts(m, err->ring != -1 ? " " : "");
  171. err_puts(m, ring_str(err->ring));
  172. err_puts(m, i915_cache_level_str(err->cache_level));
  173. if (err->name)
  174. err_printf(m, " (name: %d)", err->name);
  175. if (err->fence_reg != I915_FENCE_REG_NONE)
  176. err_printf(m, " (fence: %d)", err->fence_reg);
  177. err_puts(m, "\n");
  178. err++;
  179. }
  180. }
  181. static const char *hangcheck_action_to_str(enum intel_ring_hangcheck_action a)
  182. {
  183. switch (a) {
  184. case HANGCHECK_IDLE:
  185. return "idle";
  186. case HANGCHECK_WAIT:
  187. return "wait";
  188. case HANGCHECK_ACTIVE:
  189. return "active";
  190. case HANGCHECK_KICK:
  191. return "kick";
  192. case HANGCHECK_HUNG:
  193. return "hung";
  194. }
  195. return "unknown";
  196. }
  197. static void i915_ring_error_state(struct drm_i915_error_state_buf *m,
  198. struct drm_device *dev,
  199. struct drm_i915_error_state *error,
  200. unsigned ring)
  201. {
  202. BUG_ON(ring >= I915_NUM_RINGS); /* shut up confused gcc */
  203. if (!error->ring[ring].valid)
  204. return;
  205. err_printf(m, "%s command stream:\n", ring_str(ring));
  206. err_printf(m, " HEAD: 0x%08x\n", error->head[ring]);
  207. err_printf(m, " TAIL: 0x%08x\n", error->tail[ring]);
  208. err_printf(m, " CTL: 0x%08x\n", error->ctl[ring]);
  209. err_printf(m, " HWS: 0x%08x\n", error->hws[ring]);
  210. err_printf(m, " ACTHD: 0x%08x\n", error->acthd[ring]);
  211. err_printf(m, " IPEIR: 0x%08x\n", error->ipeir[ring]);
  212. err_printf(m, " IPEHR: 0x%08x\n", error->ipehr[ring]);
  213. err_printf(m, " INSTDONE: 0x%08x\n", error->instdone[ring]);
  214. if (INTEL_INFO(dev)->gen >= 4) {
  215. err_printf(m, " BBADDR: 0x%08llx\n", error->bbaddr[ring]);
  216. err_printf(m, " BB_STATE: 0x%08x\n", error->bbstate[ring]);
  217. err_printf(m, " INSTPS: 0x%08x\n", error->instps[ring]);
  218. }
  219. err_printf(m, " INSTPM: 0x%08x\n", error->instpm[ring]);
  220. err_printf(m, " FADDR: 0x%08x\n", error->faddr[ring]);
  221. if (INTEL_INFO(dev)->gen >= 6) {
  222. err_printf(m, " RC PSMI: 0x%08x\n", error->rc_psmi[ring]);
  223. err_printf(m, " FAULT_REG: 0x%08x\n", error->fault_reg[ring]);
  224. err_printf(m, " SYNC_0: 0x%08x [last synced 0x%08x]\n",
  225. error->semaphore_mboxes[ring][0],
  226. error->semaphore_seqno[ring][0]);
  227. err_printf(m, " SYNC_1: 0x%08x [last synced 0x%08x]\n",
  228. error->semaphore_mboxes[ring][1],
  229. error->semaphore_seqno[ring][1]);
  230. if (HAS_VEBOX(dev)) {
  231. err_printf(m, " SYNC_2: 0x%08x [last synced 0x%08x]\n",
  232. error->semaphore_mboxes[ring][2],
  233. error->semaphore_seqno[ring][2]);
  234. }
  235. }
  236. err_printf(m, " seqno: 0x%08x\n", error->seqno[ring]);
  237. err_printf(m, " waiting: %s\n", yesno(error->waiting[ring]));
  238. err_printf(m, " ring->head: 0x%08x\n", error->cpu_ring_head[ring]);
  239. err_printf(m, " ring->tail: 0x%08x\n", error->cpu_ring_tail[ring]);
  240. err_printf(m, " hangcheck: %s [%d]\n",
  241. hangcheck_action_to_str(error->hangcheck_action[ring]),
  242. error->hangcheck_score[ring]);
  243. }
  244. void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
  245. {
  246. va_list args;
  247. va_start(args, f);
  248. i915_error_vprintf(e, f, args);
  249. va_end(args);
  250. }
  251. int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
  252. const struct i915_error_state_file_priv *error_priv)
  253. {
  254. struct drm_device *dev = error_priv->dev;
  255. drm_i915_private_t *dev_priv = dev->dev_private;
  256. struct drm_i915_error_state *error = error_priv->error;
  257. int i, j, page, offset, elt;
  258. if (!error) {
  259. err_printf(m, "no error state collected\n");
  260. goto out;
  261. }
  262. err_printf(m, "Time: %ld s %ld us\n", error->time.tv_sec,
  263. error->time.tv_usec);
  264. err_printf(m, "Kernel: " UTS_RELEASE "\n");
  265. err_printf(m, "PCI ID: 0x%04x\n", dev->pdev->device);
  266. err_printf(m, "EIR: 0x%08x\n", error->eir);
  267. err_printf(m, "IER: 0x%08x\n", error->ier);
  268. err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
  269. err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
  270. err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
  271. err_printf(m, "CCID: 0x%08x\n", error->ccid);
  272. err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
  273. for (i = 0; i < dev_priv->num_fence_regs; i++)
  274. err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
  275. for (i = 0; i < ARRAY_SIZE(error->extra_instdone); i++)
  276. err_printf(m, " INSTDONE_%d: 0x%08x\n", i,
  277. error->extra_instdone[i]);
  278. if (INTEL_INFO(dev)->gen >= 6) {
  279. err_printf(m, "ERROR: 0x%08x\n", error->error);
  280. err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
  281. }
  282. if (INTEL_INFO(dev)->gen == 7)
  283. err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
  284. for (i = 0; i < ARRAY_SIZE(error->ring); i++)
  285. i915_ring_error_state(m, dev, error, i);
  286. if (error->active_bo)
  287. print_error_buffers(m, "Active",
  288. error->active_bo[0],
  289. error->active_bo_count[0]);
  290. if (error->pinned_bo)
  291. print_error_buffers(m, "Pinned",
  292. error->pinned_bo[0],
  293. error->pinned_bo_count[0]);
  294. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  295. struct drm_i915_error_object *obj;
  296. if ((obj = error->ring[i].batchbuffer)) {
  297. err_printf(m, "%s --- gtt_offset = 0x%08x\n",
  298. dev_priv->ring[i].name,
  299. obj->gtt_offset);
  300. offset = 0;
  301. for (page = 0; page < obj->page_count; page++) {
  302. for (elt = 0; elt < PAGE_SIZE/4; elt++) {
  303. err_printf(m, "%08x : %08x\n", offset,
  304. obj->pages[page][elt]);
  305. offset += 4;
  306. }
  307. }
  308. }
  309. if (error->ring[i].num_requests) {
  310. err_printf(m, "%s --- %d requests\n",
  311. dev_priv->ring[i].name,
  312. error->ring[i].num_requests);
  313. for (j = 0; j < error->ring[i].num_requests; j++) {
  314. err_printf(m, " seqno 0x%08x, emitted %ld, tail 0x%08x\n",
  315. error->ring[i].requests[j].seqno,
  316. error->ring[i].requests[j].jiffies,
  317. error->ring[i].requests[j].tail);
  318. }
  319. }
  320. if ((obj = error->ring[i].ringbuffer)) {
  321. err_printf(m, "%s --- ringbuffer = 0x%08x\n",
  322. dev_priv->ring[i].name,
  323. obj->gtt_offset);
  324. offset = 0;
  325. for (page = 0; page < obj->page_count; page++) {
  326. for (elt = 0; elt < PAGE_SIZE/4; elt++) {
  327. err_printf(m, "%08x : %08x\n",
  328. offset,
  329. obj->pages[page][elt]);
  330. offset += 4;
  331. }
  332. }
  333. }
  334. if ((obj = error->ring[i].hws)) {
  335. err_printf(m, "%s --- HW Status = 0x%08x\n",
  336. dev_priv->ring[i].name,
  337. obj->gtt_offset);
  338. offset = 0;
  339. for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
  340. err_printf(m, "[%04x] %08x %08x %08x %08x\n",
  341. offset,
  342. obj->pages[0][elt],
  343. obj->pages[0][elt+1],
  344. obj->pages[0][elt+2],
  345. obj->pages[0][elt+3]);
  346. offset += 16;
  347. }
  348. }
  349. if ((obj = error->ring[i].ctx)) {
  350. err_printf(m, "%s --- HW Context = 0x%08x\n",
  351. dev_priv->ring[i].name,
  352. obj->gtt_offset);
  353. offset = 0;
  354. for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
  355. err_printf(m, "[%04x] %08x %08x %08x %08x\n",
  356. offset,
  357. obj->pages[0][elt],
  358. obj->pages[0][elt+1],
  359. obj->pages[0][elt+2],
  360. obj->pages[0][elt+3]);
  361. offset += 16;
  362. }
  363. }
  364. }
  365. if (error->overlay)
  366. intel_overlay_print_error_state(m, error->overlay);
  367. if (error->display)
  368. intel_display_print_error_state(m, dev, error->display);
  369. out:
  370. if (m->bytes == 0 && m->err)
  371. return m->err;
  372. return 0;
  373. }
  374. int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
  375. size_t count, loff_t pos)
  376. {
  377. memset(ebuf, 0, sizeof(*ebuf));
  378. /* We need to have enough room to store any i915_error_state printf
  379. * so that we can move it to start position.
  380. */
  381. ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
  382. ebuf->buf = kmalloc(ebuf->size,
  383. GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
  384. if (ebuf->buf == NULL) {
  385. ebuf->size = PAGE_SIZE;
  386. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  387. }
  388. if (ebuf->buf == NULL) {
  389. ebuf->size = 128;
  390. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  391. }
  392. if (ebuf->buf == NULL)
  393. return -ENOMEM;
  394. ebuf->start = pos;
  395. return 0;
  396. }
  397. static void i915_error_object_free(struct drm_i915_error_object *obj)
  398. {
  399. int page;
  400. if (obj == NULL)
  401. return;
  402. for (page = 0; page < obj->page_count; page++)
  403. kfree(obj->pages[page]);
  404. kfree(obj);
  405. }
  406. static void i915_error_state_free(struct kref *error_ref)
  407. {
  408. struct drm_i915_error_state *error = container_of(error_ref,
  409. typeof(*error), ref);
  410. int i;
  411. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  412. i915_error_object_free(error->ring[i].batchbuffer);
  413. i915_error_object_free(error->ring[i].ringbuffer);
  414. i915_error_object_free(error->ring[i].hws);
  415. i915_error_object_free(error->ring[i].ctx);
  416. kfree(error->ring[i].requests);
  417. }
  418. kfree(error->active_bo);
  419. kfree(error->overlay);
  420. kfree(error->display);
  421. kfree(error);
  422. }
  423. static struct drm_i915_error_object *
  424. i915_error_object_create_sized(struct drm_i915_private *dev_priv,
  425. struct drm_i915_gem_object *src,
  426. struct i915_address_space *vm,
  427. const int num_pages)
  428. {
  429. struct drm_i915_error_object *dst;
  430. int i;
  431. u32 reloc_offset;
  432. if (src == NULL || src->pages == NULL)
  433. return NULL;
  434. dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
  435. if (dst == NULL)
  436. return NULL;
  437. reloc_offset = dst->gtt_offset = i915_gem_obj_offset(src, vm);
  438. for (i = 0; i < num_pages; i++) {
  439. unsigned long flags;
  440. void *d;
  441. d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
  442. if (d == NULL)
  443. goto unwind;
  444. local_irq_save(flags);
  445. if (reloc_offset < dev_priv->gtt.mappable_end &&
  446. src->has_global_gtt_mapping &&
  447. i915_is_ggtt(vm)) {
  448. void __iomem *s;
  449. /* Simply ignore tiling or any overlapping fence.
  450. * It's part of the error state, and this hopefully
  451. * captures what the GPU read.
  452. */
  453. s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  454. reloc_offset);
  455. memcpy_fromio(d, s, PAGE_SIZE);
  456. io_mapping_unmap_atomic(s);
  457. } else if (src->stolen) {
  458. unsigned long offset;
  459. offset = dev_priv->mm.stolen_base;
  460. offset += src->stolen->start;
  461. offset += i << PAGE_SHIFT;
  462. memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
  463. } else {
  464. struct page *page;
  465. void *s;
  466. page = i915_gem_object_get_page(src, i);
  467. drm_clflush_pages(&page, 1);
  468. s = kmap_atomic(page);
  469. memcpy(d, s, PAGE_SIZE);
  470. kunmap_atomic(s);
  471. drm_clflush_pages(&page, 1);
  472. }
  473. local_irq_restore(flags);
  474. dst->pages[i] = d;
  475. reloc_offset += PAGE_SIZE;
  476. }
  477. dst->page_count = num_pages;
  478. return dst;
  479. unwind:
  480. while (i--)
  481. kfree(dst->pages[i]);
  482. kfree(dst);
  483. return NULL;
  484. }
  485. #define i915_error_object_create(dev_priv, src, vm) \
  486. i915_error_object_create_sized((dev_priv), (src), (vm), \
  487. (src)->base.size>>PAGE_SHIFT)
  488. #define i915_error_ggtt_object_create(dev_priv, src) \
  489. i915_error_object_create_sized((dev_priv), (src), &(dev_priv)->gtt.base, \
  490. (src)->base.size>>PAGE_SHIFT)
  491. static void capture_bo(struct drm_i915_error_buffer *err,
  492. struct drm_i915_gem_object *obj)
  493. {
  494. err->size = obj->base.size;
  495. err->name = obj->base.name;
  496. err->rseqno = obj->last_read_seqno;
  497. err->wseqno = obj->last_write_seqno;
  498. err->gtt_offset = i915_gem_obj_ggtt_offset(obj);
  499. err->read_domains = obj->base.read_domains;
  500. err->write_domain = obj->base.write_domain;
  501. err->fence_reg = obj->fence_reg;
  502. err->pinned = 0;
  503. if (i915_gem_obj_is_pinned(obj))
  504. err->pinned = 1;
  505. if (obj->user_pin_count > 0)
  506. err->pinned = -1;
  507. err->tiling = obj->tiling_mode;
  508. err->dirty = obj->dirty;
  509. err->purgeable = obj->madv != I915_MADV_WILLNEED;
  510. err->ring = obj->ring ? obj->ring->id : -1;
  511. err->cache_level = obj->cache_level;
  512. }
  513. static u32 capture_active_bo(struct drm_i915_error_buffer *err,
  514. int count, struct list_head *head)
  515. {
  516. struct i915_vma *vma;
  517. int i = 0;
  518. list_for_each_entry(vma, head, mm_list) {
  519. capture_bo(err++, vma->obj);
  520. if (++i == count)
  521. break;
  522. }
  523. return i;
  524. }
  525. static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
  526. int count, struct list_head *head)
  527. {
  528. struct drm_i915_gem_object *obj;
  529. int i = 0;
  530. list_for_each_entry(obj, head, global_list) {
  531. if (!i915_gem_obj_is_pinned(obj))
  532. continue;
  533. capture_bo(err++, obj);
  534. if (++i == count)
  535. break;
  536. }
  537. return i;
  538. }
  539. static void i915_gem_record_fences(struct drm_device *dev,
  540. struct drm_i915_error_state *error)
  541. {
  542. struct drm_i915_private *dev_priv = dev->dev_private;
  543. int i;
  544. /* Fences */
  545. switch (INTEL_INFO(dev)->gen) {
  546. case 8:
  547. case 7:
  548. case 6:
  549. for (i = 0; i < dev_priv->num_fence_regs; i++)
  550. error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
  551. break;
  552. case 5:
  553. case 4:
  554. for (i = 0; i < 16; i++)
  555. error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
  556. break;
  557. case 3:
  558. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  559. for (i = 0; i < 8; i++)
  560. error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
  561. case 2:
  562. for (i = 0; i < 8; i++)
  563. error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
  564. break;
  565. default:
  566. BUG();
  567. }
  568. }
  569. /* This assumes all batchbuffers are executed from the PPGTT. It might have to
  570. * change in the future. */
  571. static bool is_active_vm(struct i915_address_space *vm,
  572. struct intel_ring_buffer *ring)
  573. {
  574. struct drm_device *dev = vm->dev;
  575. struct drm_i915_private *dev_priv = dev->dev_private;
  576. struct i915_hw_ppgtt *ppgtt;
  577. if (INTEL_INFO(dev)->gen < 7)
  578. return i915_is_ggtt(vm);
  579. /* FIXME: This ignores that the global gtt vm is also on this list. */
  580. ppgtt = container_of(vm, struct i915_hw_ppgtt, base);
  581. if (INTEL_INFO(dev)->gen >= 8) {
  582. u64 pdp0 = (u64)I915_READ(GEN8_RING_PDP_UDW(ring, 0)) << 32;
  583. pdp0 |= I915_READ(GEN8_RING_PDP_LDW(ring, 0));
  584. return pdp0 == ppgtt->pd_dma_addr[0];
  585. } else {
  586. u32 pp_db;
  587. pp_db = I915_READ(RING_PP_DIR_BASE(ring));
  588. return (pp_db >> 10) == ppgtt->pd_offset;
  589. }
  590. }
  591. static struct drm_i915_error_object *
  592. i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
  593. struct intel_ring_buffer *ring)
  594. {
  595. struct i915_address_space *vm;
  596. struct i915_vma *vma;
  597. struct drm_i915_gem_object *obj;
  598. bool found_active = false;
  599. u32 seqno;
  600. if (!ring->get_seqno)
  601. return NULL;
  602. if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
  603. u32 acthd = I915_READ(ACTHD);
  604. if (WARN_ON(ring->id != RCS))
  605. return NULL;
  606. obj = ring->scratch.obj;
  607. if (obj != NULL &&
  608. acthd >= i915_gem_obj_ggtt_offset(obj) &&
  609. acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size)
  610. return i915_error_ggtt_object_create(dev_priv, obj);
  611. }
  612. seqno = ring->get_seqno(ring, false);
  613. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  614. if (!is_active_vm(vm, ring))
  615. continue;
  616. found_active = true;
  617. list_for_each_entry(vma, &vm->active_list, mm_list) {
  618. obj = vma->obj;
  619. if (obj->ring != ring)
  620. continue;
  621. if (i915_seqno_passed(seqno, obj->last_read_seqno))
  622. continue;
  623. if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
  624. continue;
  625. /* We need to copy these to an anonymous buffer as the simplest
  626. * method to avoid being overwritten by userspace.
  627. */
  628. return i915_error_object_create(dev_priv, obj, vm);
  629. }
  630. }
  631. WARN_ON(!found_active);
  632. return NULL;
  633. }
  634. static void i915_record_ring_state(struct drm_device *dev,
  635. struct drm_i915_error_state *error,
  636. struct intel_ring_buffer *ring)
  637. {
  638. struct drm_i915_private *dev_priv = dev->dev_private;
  639. if (INTEL_INFO(dev)->gen >= 6) {
  640. error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
  641. error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
  642. error->semaphore_mboxes[ring->id][0]
  643. = I915_READ(RING_SYNC_0(ring->mmio_base));
  644. error->semaphore_mboxes[ring->id][1]
  645. = I915_READ(RING_SYNC_1(ring->mmio_base));
  646. error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
  647. error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
  648. }
  649. if (HAS_VEBOX(dev)) {
  650. error->semaphore_mboxes[ring->id][2] =
  651. I915_READ(RING_SYNC_2(ring->mmio_base));
  652. error->semaphore_seqno[ring->id][2] = ring->sync_seqno[2];
  653. }
  654. if (INTEL_INFO(dev)->gen >= 4) {
  655. error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
  656. error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
  657. error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
  658. error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
  659. error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
  660. error->bbaddr[ring->id] = I915_READ(RING_BBADDR(ring->mmio_base));
  661. if (INTEL_INFO(dev)->gen >= 8)
  662. error->bbaddr[ring->id] |= (u64) I915_READ(RING_BBADDR_UDW(ring->mmio_base)) << 32;
  663. error->bbstate[ring->id] = I915_READ(RING_BBSTATE(ring->mmio_base));
  664. } else {
  665. error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
  666. error->ipeir[ring->id] = I915_READ(IPEIR);
  667. error->ipehr[ring->id] = I915_READ(IPEHR);
  668. error->instdone[ring->id] = I915_READ(INSTDONE);
  669. }
  670. error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
  671. error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
  672. error->seqno[ring->id] = ring->get_seqno(ring, false);
  673. error->acthd[ring->id] = intel_ring_get_active_head(ring);
  674. error->head[ring->id] = I915_READ_HEAD(ring);
  675. error->tail[ring->id] = I915_READ_TAIL(ring);
  676. error->ctl[ring->id] = I915_READ_CTL(ring);
  677. if (I915_NEED_GFX_HWS(dev)) {
  678. int mmio;
  679. if (IS_GEN7(dev)) {
  680. switch (ring->id) {
  681. default:
  682. case RCS:
  683. mmio = RENDER_HWS_PGA_GEN7;
  684. break;
  685. case BCS:
  686. mmio = BLT_HWS_PGA_GEN7;
  687. break;
  688. case VCS:
  689. mmio = BSD_HWS_PGA_GEN7;
  690. break;
  691. case VECS:
  692. mmio = VEBOX_HWS_PGA_GEN7;
  693. break;
  694. }
  695. } else if (IS_GEN6(ring->dev)) {
  696. mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
  697. } else {
  698. /* XXX: gen8 returns to sanity */
  699. mmio = RING_HWS_PGA(ring->mmio_base);
  700. }
  701. error->hws[ring->id] = I915_READ(mmio);
  702. }
  703. error->cpu_ring_head[ring->id] = ring->head;
  704. error->cpu_ring_tail[ring->id] = ring->tail;
  705. error->hangcheck_score[ring->id] = ring->hangcheck.score;
  706. error->hangcheck_action[ring->id] = ring->hangcheck.action;
  707. }
  708. static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
  709. struct drm_i915_error_state *error,
  710. struct drm_i915_error_ring *ering)
  711. {
  712. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  713. struct drm_i915_gem_object *obj;
  714. /* Currently render ring is the only HW context user */
  715. if (ring->id != RCS || !error->ccid)
  716. return;
  717. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  718. if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
  719. ering->ctx = i915_error_object_create_sized(dev_priv,
  720. obj,
  721. &dev_priv->gtt.base,
  722. 1);
  723. break;
  724. }
  725. }
  726. }
  727. static void i915_gem_record_rings(struct drm_device *dev,
  728. struct drm_i915_error_state *error)
  729. {
  730. struct drm_i915_private *dev_priv = dev->dev_private;
  731. struct drm_i915_gem_request *request;
  732. int i, count;
  733. for (i = 0; i < I915_NUM_RINGS; i++) {
  734. struct intel_ring_buffer *ring = &dev_priv->ring[i];
  735. if (ring->dev == NULL)
  736. continue;
  737. error->ring[i].valid = true;
  738. i915_record_ring_state(dev, error, ring);
  739. error->ring[i].batchbuffer =
  740. i915_error_first_batchbuffer(dev_priv, ring);
  741. error->ring[i].ringbuffer =
  742. i915_error_ggtt_object_create(dev_priv, ring->obj);
  743. if (ring->status_page.obj)
  744. error->ring[i].hws =
  745. i915_error_ggtt_object_create(dev_priv, ring->status_page.obj);
  746. i915_gem_record_active_context(ring, error, &error->ring[i]);
  747. count = 0;
  748. list_for_each_entry(request, &ring->request_list, list)
  749. count++;
  750. error->ring[i].num_requests = count;
  751. error->ring[i].requests =
  752. kcalloc(count, sizeof(*error->ring[i].requests),
  753. GFP_ATOMIC);
  754. if (error->ring[i].requests == NULL) {
  755. error->ring[i].num_requests = 0;
  756. continue;
  757. }
  758. count = 0;
  759. list_for_each_entry(request, &ring->request_list, list) {
  760. struct drm_i915_error_request *erq;
  761. erq = &error->ring[i].requests[count++];
  762. erq->seqno = request->seqno;
  763. erq->jiffies = request->emitted_jiffies;
  764. erq->tail = request->tail;
  765. }
  766. }
  767. }
  768. /* FIXME: Since pin count/bound list is global, we duplicate what we capture per
  769. * VM.
  770. */
  771. static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
  772. struct drm_i915_error_state *error,
  773. struct i915_address_space *vm,
  774. const int ndx)
  775. {
  776. struct drm_i915_error_buffer *active_bo = NULL, *pinned_bo = NULL;
  777. struct drm_i915_gem_object *obj;
  778. struct i915_vma *vma;
  779. int i;
  780. i = 0;
  781. list_for_each_entry(vma, &vm->active_list, mm_list)
  782. i++;
  783. error->active_bo_count[ndx] = i;
  784. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  785. if (i915_gem_obj_is_pinned(obj))
  786. i++;
  787. error->pinned_bo_count[ndx] = i - error->active_bo_count[ndx];
  788. if (i) {
  789. active_bo = kcalloc(i, sizeof(*active_bo), GFP_ATOMIC);
  790. if (active_bo)
  791. pinned_bo = active_bo + error->active_bo_count[ndx];
  792. }
  793. if (active_bo)
  794. error->active_bo_count[ndx] =
  795. capture_active_bo(active_bo,
  796. error->active_bo_count[ndx],
  797. &vm->active_list);
  798. if (pinned_bo)
  799. error->pinned_bo_count[ndx] =
  800. capture_pinned_bo(pinned_bo,
  801. error->pinned_bo_count[ndx],
  802. &dev_priv->mm.bound_list);
  803. error->active_bo[ndx] = active_bo;
  804. error->pinned_bo[ndx] = pinned_bo;
  805. }
  806. static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
  807. struct drm_i915_error_state *error)
  808. {
  809. struct i915_address_space *vm;
  810. int cnt = 0, i = 0;
  811. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  812. cnt++;
  813. error->active_bo = kcalloc(cnt, sizeof(*error->active_bo), GFP_ATOMIC);
  814. error->pinned_bo = kcalloc(cnt, sizeof(*error->pinned_bo), GFP_ATOMIC);
  815. error->active_bo_count = kcalloc(cnt, sizeof(*error->active_bo_count),
  816. GFP_ATOMIC);
  817. error->pinned_bo_count = kcalloc(cnt, sizeof(*error->pinned_bo_count),
  818. GFP_ATOMIC);
  819. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  820. i915_gem_capture_vm(dev_priv, error, vm, i++);
  821. }
  822. /* Capture all registers which don't fit into another category. */
  823. static void i915_capture_reg_state(struct drm_i915_private *dev_priv,
  824. struct drm_i915_error_state *error)
  825. {
  826. struct drm_device *dev = dev_priv->dev;
  827. int pipe;
  828. /* General organization
  829. * 1. Registers specific to a single generation
  830. * 2. Registers which belong to multiple generations
  831. * 3. Feature specific registers.
  832. * 4. Everything else
  833. * Please try to follow the order.
  834. */
  835. /* 1: Registers specific to a single generation */
  836. if (IS_VALLEYVIEW(dev)) {
  837. error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
  838. error->forcewake = I915_READ(FORCEWAKE_VLV);
  839. }
  840. if (IS_GEN7(dev))
  841. error->err_int = I915_READ(GEN7_ERR_INT);
  842. if (IS_GEN6(dev))
  843. error->forcewake = I915_READ(FORCEWAKE);
  844. if (IS_GEN2(dev))
  845. error->ier = I915_READ16(IER);
  846. /* 2: Registers which belong to multiple generations */
  847. if (INTEL_INFO(dev)->gen >= 7)
  848. error->forcewake = I915_READ(FORCEWAKE_MT);
  849. if (INTEL_INFO(dev)->gen >= 6) {
  850. error->derrmr = I915_READ(DERRMR);
  851. error->error = I915_READ(ERROR_GEN6);
  852. error->done_reg = I915_READ(DONE_REG);
  853. }
  854. /* 3: Feature specific registers */
  855. if (HAS_HW_CONTEXTS(dev))
  856. error->ccid = I915_READ(CCID);
  857. if (HAS_PCH_SPLIT(dev))
  858. error->ier = I915_READ(DEIER) | I915_READ(GTIER);
  859. else {
  860. error->ier = I915_READ(IER);
  861. for_each_pipe(pipe)
  862. error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
  863. }
  864. /* 4: Everything else */
  865. error->eir = I915_READ(EIR);
  866. error->pgtbl_er = I915_READ(PGTBL_ER);
  867. i915_get_extra_instdone(dev, error->extra_instdone);
  868. }
  869. /**
  870. * i915_capture_error_state - capture an error record for later analysis
  871. * @dev: drm device
  872. *
  873. * Should be called when an error is detected (either a hang or an error
  874. * interrupt) to capture error state from the time of the error. Fills
  875. * out a structure which becomes available in debugfs for user level tools
  876. * to pick up.
  877. */
  878. void i915_capture_error_state(struct drm_device *dev)
  879. {
  880. struct drm_i915_private *dev_priv = dev->dev_private;
  881. struct drm_i915_error_state *error;
  882. unsigned long flags;
  883. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  884. error = dev_priv->gpu_error.first_error;
  885. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  886. if (error)
  887. return;
  888. /* Account for pipe specific data like PIPE*STAT */
  889. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  890. if (!error) {
  891. DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
  892. return;
  893. }
  894. DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n",
  895. dev->primary->index);
  896. DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
  897. DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
  898. DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
  899. DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
  900. kref_init(&error->ref);
  901. i915_capture_reg_state(dev_priv, error);
  902. i915_gem_capture_buffers(dev_priv, error);
  903. i915_gem_record_fences(dev, error);
  904. i915_gem_record_rings(dev, error);
  905. do_gettimeofday(&error->time);
  906. error->overlay = intel_overlay_capture_error_state(dev);
  907. error->display = intel_display_capture_error_state(dev);
  908. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  909. if (dev_priv->gpu_error.first_error == NULL) {
  910. dev_priv->gpu_error.first_error = error;
  911. error = NULL;
  912. }
  913. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  914. if (error)
  915. i915_error_state_free(&error->ref);
  916. }
  917. void i915_error_state_get(struct drm_device *dev,
  918. struct i915_error_state_file_priv *error_priv)
  919. {
  920. struct drm_i915_private *dev_priv = dev->dev_private;
  921. unsigned long flags;
  922. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  923. error_priv->error = dev_priv->gpu_error.first_error;
  924. if (error_priv->error)
  925. kref_get(&error_priv->error->ref);
  926. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  927. }
  928. void i915_error_state_put(struct i915_error_state_file_priv *error_priv)
  929. {
  930. if (error_priv->error)
  931. kref_put(&error_priv->error->ref, i915_error_state_free);
  932. }
  933. void i915_destroy_error_state(struct drm_device *dev)
  934. {
  935. struct drm_i915_private *dev_priv = dev->dev_private;
  936. struct drm_i915_error_state *error;
  937. unsigned long flags;
  938. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  939. error = dev_priv->gpu_error.first_error;
  940. dev_priv->gpu_error.first_error = NULL;
  941. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  942. if (error)
  943. kref_put(&error->ref, i915_error_state_free);
  944. }
  945. const char *i915_cache_level_str(int type)
  946. {
  947. switch (type) {
  948. case I915_CACHE_NONE: return " uncached";
  949. case I915_CACHE_LLC: return " snooped or LLC";
  950. case I915_CACHE_L3_LLC: return " L3+LLC";
  951. case I915_CACHE_WT: return " WT";
  952. default: return "";
  953. }
  954. }
  955. /* NB: please notice the memset */
  956. void i915_get_extra_instdone(struct drm_device *dev, uint32_t *instdone)
  957. {
  958. struct drm_i915_private *dev_priv = dev->dev_private;
  959. memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
  960. switch (INTEL_INFO(dev)->gen) {
  961. case 2:
  962. case 3:
  963. instdone[0] = I915_READ(INSTDONE);
  964. break;
  965. case 4:
  966. case 5:
  967. case 6:
  968. instdone[0] = I915_READ(INSTDONE_I965);
  969. instdone[1] = I915_READ(INSTDONE1);
  970. break;
  971. default:
  972. WARN_ONCE(1, "Unsupported platform\n");
  973. case 7:
  974. case 8:
  975. instdone[0] = I915_READ(GEN7_INSTDONE_1);
  976. instdone[1] = I915_READ(GEN7_SC_INSTDONE);
  977. instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
  978. instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
  979. break;
  980. }
  981. }