i915_gpu_error.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. err_printf(m, "%s command stream:\n", ring_str(ring));
  204. err_printf(m, " HEAD: 0x%08x\n", error->head[ring]);
  205. err_printf(m, " TAIL: 0x%08x\n", error->tail[ring]);
  206. err_printf(m, " CTL: 0x%08x\n", error->ctl[ring]);
  207. err_printf(m, " ACTHD: 0x%08x\n", error->acthd[ring]);
  208. err_printf(m, " IPEIR: 0x%08x\n", error->ipeir[ring]);
  209. err_printf(m, " IPEHR: 0x%08x\n", error->ipehr[ring]);
  210. err_printf(m, " INSTDONE: 0x%08x\n", error->instdone[ring]);
  211. if (ring == RCS && INTEL_INFO(dev)->gen >= 4)
  212. err_printf(m, " BBADDR: 0x%08llx\n", error->bbaddr);
  213. if (INTEL_INFO(dev)->gen >= 4)
  214. err_printf(m, " INSTPS: 0x%08x\n", error->instps[ring]);
  215. err_printf(m, " INSTPM: 0x%08x\n", error->instpm[ring]);
  216. err_printf(m, " FADDR: 0x%08x\n", error->faddr[ring]);
  217. if (INTEL_INFO(dev)->gen >= 6) {
  218. err_printf(m, " RC PSMI: 0x%08x\n", error->rc_psmi[ring]);
  219. err_printf(m, " FAULT_REG: 0x%08x\n", error->fault_reg[ring]);
  220. err_printf(m, " SYNC_0: 0x%08x [last synced 0x%08x]\n",
  221. error->semaphore_mboxes[ring][0],
  222. error->semaphore_seqno[ring][0]);
  223. err_printf(m, " SYNC_1: 0x%08x [last synced 0x%08x]\n",
  224. error->semaphore_mboxes[ring][1],
  225. error->semaphore_seqno[ring][1]);
  226. if (HAS_VEBOX(dev)) {
  227. err_printf(m, " SYNC_2: 0x%08x [last synced 0x%08x]\n",
  228. error->semaphore_mboxes[ring][2],
  229. error->semaphore_seqno[ring][2]);
  230. }
  231. }
  232. err_printf(m, " seqno: 0x%08x\n", error->seqno[ring]);
  233. err_printf(m, " waiting: %s\n", yesno(error->waiting[ring]));
  234. err_printf(m, " ring->head: 0x%08x\n", error->cpu_ring_head[ring]);
  235. err_printf(m, " ring->tail: 0x%08x\n", error->cpu_ring_tail[ring]);
  236. err_printf(m, " hangcheck: %s [%d]\n",
  237. hangcheck_action_to_str(error->hangcheck_action[ring]),
  238. error->hangcheck_score[ring]);
  239. }
  240. void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
  241. {
  242. va_list args;
  243. va_start(args, f);
  244. i915_error_vprintf(e, f, args);
  245. va_end(args);
  246. }
  247. int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
  248. const struct i915_error_state_file_priv *error_priv)
  249. {
  250. struct drm_device *dev = error_priv->dev;
  251. drm_i915_private_t *dev_priv = dev->dev_private;
  252. struct drm_i915_error_state *error = error_priv->error;
  253. struct intel_ring_buffer *ring;
  254. int i, j, page, offset, elt;
  255. if (!error) {
  256. err_printf(m, "no error state collected\n");
  257. goto out;
  258. }
  259. err_printf(m, "Time: %ld s %ld us\n", error->time.tv_sec,
  260. error->time.tv_usec);
  261. err_printf(m, "Kernel: " UTS_RELEASE "\n");
  262. err_printf(m, "PCI ID: 0x%04x\n", dev->pdev->device);
  263. err_printf(m, "EIR: 0x%08x\n", error->eir);
  264. err_printf(m, "IER: 0x%08x\n", error->ier);
  265. err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
  266. err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
  267. err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
  268. err_printf(m, "CCID: 0x%08x\n", error->ccid);
  269. err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
  270. for (i = 0; i < dev_priv->num_fence_regs; i++)
  271. err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
  272. for (i = 0; i < ARRAY_SIZE(error->extra_instdone); i++)
  273. err_printf(m, " INSTDONE_%d: 0x%08x\n", i,
  274. error->extra_instdone[i]);
  275. if (INTEL_INFO(dev)->gen >= 6) {
  276. err_printf(m, "ERROR: 0x%08x\n", error->error);
  277. err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
  278. }
  279. if (INTEL_INFO(dev)->gen == 7)
  280. err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
  281. for_each_ring(ring, dev_priv, i)
  282. i915_ring_error_state(m, dev, error, i);
  283. if (error->active_bo)
  284. print_error_buffers(m, "Active",
  285. error->active_bo[0],
  286. error->active_bo_count[0]);
  287. if (error->pinned_bo)
  288. print_error_buffers(m, "Pinned",
  289. error->pinned_bo[0],
  290. error->pinned_bo_count[0]);
  291. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  292. struct drm_i915_error_object *obj;
  293. if ((obj = error->ring[i].batchbuffer)) {
  294. err_printf(m, "%s --- gtt_offset = 0x%08x\n",
  295. dev_priv->ring[i].name,
  296. obj->gtt_offset);
  297. offset = 0;
  298. for (page = 0; page < obj->page_count; page++) {
  299. for (elt = 0; elt < PAGE_SIZE/4; elt++) {
  300. err_printf(m, "%08x : %08x\n", offset,
  301. obj->pages[page][elt]);
  302. offset += 4;
  303. }
  304. }
  305. }
  306. if (error->ring[i].num_requests) {
  307. err_printf(m, "%s --- %d requests\n",
  308. dev_priv->ring[i].name,
  309. error->ring[i].num_requests);
  310. for (j = 0; j < error->ring[i].num_requests; j++) {
  311. err_printf(m, " seqno 0x%08x, emitted %ld, tail 0x%08x\n",
  312. error->ring[i].requests[j].seqno,
  313. error->ring[i].requests[j].jiffies,
  314. error->ring[i].requests[j].tail);
  315. }
  316. }
  317. if ((obj = error->ring[i].ringbuffer)) {
  318. err_printf(m, "%s --- ringbuffer = 0x%08x\n",
  319. dev_priv->ring[i].name,
  320. obj->gtt_offset);
  321. offset = 0;
  322. for (page = 0; page < obj->page_count; page++) {
  323. for (elt = 0; elt < PAGE_SIZE/4; elt++) {
  324. err_printf(m, "%08x : %08x\n",
  325. offset,
  326. obj->pages[page][elt]);
  327. offset += 4;
  328. }
  329. }
  330. }
  331. obj = error->ring[i].ctx;
  332. if (obj) {
  333. err_printf(m, "%s --- HW Context = 0x%08x\n",
  334. dev_priv->ring[i].name,
  335. obj->gtt_offset);
  336. offset = 0;
  337. for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
  338. err_printf(m, "[%04x] %08x %08x %08x %08x\n",
  339. offset,
  340. obj->pages[0][elt],
  341. obj->pages[0][elt+1],
  342. obj->pages[0][elt+2],
  343. obj->pages[0][elt+3]);
  344. offset += 16;
  345. }
  346. }
  347. }
  348. if (error->overlay)
  349. intel_overlay_print_error_state(m, error->overlay);
  350. if (error->display)
  351. intel_display_print_error_state(m, dev, error->display);
  352. out:
  353. if (m->bytes == 0 && m->err)
  354. return m->err;
  355. return 0;
  356. }
  357. int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
  358. size_t count, loff_t pos)
  359. {
  360. memset(ebuf, 0, sizeof(*ebuf));
  361. /* We need to have enough room to store any i915_error_state printf
  362. * so that we can move it to start position.
  363. */
  364. ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
  365. ebuf->buf = kmalloc(ebuf->size,
  366. GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
  367. if (ebuf->buf == NULL) {
  368. ebuf->size = PAGE_SIZE;
  369. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  370. }
  371. if (ebuf->buf == NULL) {
  372. ebuf->size = 128;
  373. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  374. }
  375. if (ebuf->buf == NULL)
  376. return -ENOMEM;
  377. ebuf->start = pos;
  378. return 0;
  379. }
  380. static void i915_error_object_free(struct drm_i915_error_object *obj)
  381. {
  382. int page;
  383. if (obj == NULL)
  384. return;
  385. for (page = 0; page < obj->page_count; page++)
  386. kfree(obj->pages[page]);
  387. kfree(obj);
  388. }
  389. static void i915_error_state_free(struct kref *error_ref)
  390. {
  391. struct drm_i915_error_state *error = container_of(error_ref,
  392. typeof(*error), ref);
  393. int i;
  394. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  395. i915_error_object_free(error->ring[i].batchbuffer);
  396. i915_error_object_free(error->ring[i].ringbuffer);
  397. i915_error_object_free(error->ring[i].ctx);
  398. kfree(error->ring[i].requests);
  399. }
  400. kfree(error->active_bo);
  401. kfree(error->overlay);
  402. kfree(error->display);
  403. kfree(error);
  404. }
  405. static struct drm_i915_error_object *
  406. i915_error_object_create_sized(struct drm_i915_private *dev_priv,
  407. struct drm_i915_gem_object *src,
  408. const int num_pages)
  409. {
  410. struct drm_i915_error_object *dst;
  411. int i;
  412. u32 reloc_offset;
  413. if (src == NULL || src->pages == NULL)
  414. return NULL;
  415. dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
  416. if (dst == NULL)
  417. return NULL;
  418. reloc_offset = dst->gtt_offset = i915_gem_obj_ggtt_offset(src);
  419. for (i = 0; i < num_pages; i++) {
  420. unsigned long flags;
  421. void *d;
  422. d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
  423. if (d == NULL)
  424. goto unwind;
  425. local_irq_save(flags);
  426. if (reloc_offset < dev_priv->gtt.mappable_end &&
  427. src->has_global_gtt_mapping) {
  428. void __iomem *s;
  429. /* Simply ignore tiling or any overlapping fence.
  430. * It's part of the error state, and this hopefully
  431. * captures what the GPU read.
  432. */
  433. s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  434. reloc_offset);
  435. memcpy_fromio(d, s, PAGE_SIZE);
  436. io_mapping_unmap_atomic(s);
  437. } else if (src->stolen) {
  438. unsigned long offset;
  439. offset = dev_priv->mm.stolen_base;
  440. offset += src->stolen->start;
  441. offset += i << PAGE_SHIFT;
  442. memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
  443. } else {
  444. struct page *page;
  445. void *s;
  446. page = i915_gem_object_get_page(src, i);
  447. drm_clflush_pages(&page, 1);
  448. s = kmap_atomic(page);
  449. memcpy(d, s, PAGE_SIZE);
  450. kunmap_atomic(s);
  451. drm_clflush_pages(&page, 1);
  452. }
  453. local_irq_restore(flags);
  454. dst->pages[i] = d;
  455. reloc_offset += PAGE_SIZE;
  456. }
  457. dst->page_count = num_pages;
  458. return dst;
  459. unwind:
  460. while (i--)
  461. kfree(dst->pages[i]);
  462. kfree(dst);
  463. return NULL;
  464. }
  465. #define i915_error_object_create(dev_priv, src) \
  466. i915_error_object_create_sized((dev_priv), (src), \
  467. (src)->base.size>>PAGE_SHIFT)
  468. static void capture_bo(struct drm_i915_error_buffer *err,
  469. struct drm_i915_gem_object *obj)
  470. {
  471. err->size = obj->base.size;
  472. err->name = obj->base.name;
  473. err->rseqno = obj->last_read_seqno;
  474. err->wseqno = obj->last_write_seqno;
  475. err->gtt_offset = i915_gem_obj_ggtt_offset(obj);
  476. err->read_domains = obj->base.read_domains;
  477. err->write_domain = obj->base.write_domain;
  478. err->fence_reg = obj->fence_reg;
  479. err->pinned = 0;
  480. if (obj->pin_count > 0)
  481. err->pinned = 1;
  482. if (obj->user_pin_count > 0)
  483. err->pinned = -1;
  484. err->tiling = obj->tiling_mode;
  485. err->dirty = obj->dirty;
  486. err->purgeable = obj->madv != I915_MADV_WILLNEED;
  487. err->ring = obj->ring ? obj->ring->id : -1;
  488. err->cache_level = obj->cache_level;
  489. }
  490. static u32 capture_active_bo(struct drm_i915_error_buffer *err,
  491. int count, struct list_head *head)
  492. {
  493. struct i915_vma *vma;
  494. int i = 0;
  495. list_for_each_entry(vma, head, mm_list) {
  496. capture_bo(err++, vma->obj);
  497. if (++i == count)
  498. break;
  499. }
  500. return i;
  501. }
  502. static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
  503. int count, struct list_head *head)
  504. {
  505. struct drm_i915_gem_object *obj;
  506. int i = 0;
  507. list_for_each_entry(obj, head, global_list) {
  508. if (obj->pin_count == 0)
  509. continue;
  510. capture_bo(err++, obj);
  511. if (++i == count)
  512. break;
  513. }
  514. return i;
  515. }
  516. static void i915_gem_record_fences(struct drm_device *dev,
  517. struct drm_i915_error_state *error)
  518. {
  519. struct drm_i915_private *dev_priv = dev->dev_private;
  520. int i;
  521. /* Fences */
  522. switch (INTEL_INFO(dev)->gen) {
  523. case 7:
  524. case 6:
  525. for (i = 0; i < dev_priv->num_fence_regs; i++)
  526. error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
  527. break;
  528. case 5:
  529. case 4:
  530. for (i = 0; i < 16; i++)
  531. error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
  532. break;
  533. case 3:
  534. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  535. for (i = 0; i < 8; i++)
  536. error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
  537. case 2:
  538. for (i = 0; i < 8; i++)
  539. error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
  540. break;
  541. default:
  542. BUG();
  543. }
  544. }
  545. static struct drm_i915_error_object *
  546. i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
  547. struct intel_ring_buffer *ring)
  548. {
  549. struct i915_address_space *vm;
  550. struct i915_vma *vma;
  551. struct drm_i915_gem_object *obj;
  552. u32 seqno;
  553. if (!ring->get_seqno)
  554. return NULL;
  555. if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
  556. u32 acthd = I915_READ(ACTHD);
  557. if (WARN_ON(ring->id != RCS))
  558. return NULL;
  559. obj = ring->scratch.obj;
  560. if (acthd >= i915_gem_obj_ggtt_offset(obj) &&
  561. acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size)
  562. return i915_error_object_create(dev_priv, obj);
  563. }
  564. seqno = ring->get_seqno(ring, false);
  565. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  566. list_for_each_entry(vma, &vm->active_list, mm_list) {
  567. obj = vma->obj;
  568. if (obj->ring != ring)
  569. continue;
  570. if (i915_seqno_passed(seqno, obj->last_read_seqno))
  571. continue;
  572. if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
  573. continue;
  574. /* We need to copy these to an anonymous buffer as the simplest
  575. * method to avoid being overwritten by userspace.
  576. */
  577. return i915_error_object_create(dev_priv, obj);
  578. }
  579. }
  580. return NULL;
  581. }
  582. static void i915_record_ring_state(struct drm_device *dev,
  583. struct drm_i915_error_state *error,
  584. struct intel_ring_buffer *ring)
  585. {
  586. struct drm_i915_private *dev_priv = dev->dev_private;
  587. if (INTEL_INFO(dev)->gen >= 6) {
  588. error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
  589. error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
  590. error->semaphore_mboxes[ring->id][0]
  591. = I915_READ(RING_SYNC_0(ring->mmio_base));
  592. error->semaphore_mboxes[ring->id][1]
  593. = I915_READ(RING_SYNC_1(ring->mmio_base));
  594. error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
  595. error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
  596. }
  597. if (HAS_VEBOX(dev)) {
  598. error->semaphore_mboxes[ring->id][2] =
  599. I915_READ(RING_SYNC_2(ring->mmio_base));
  600. error->semaphore_seqno[ring->id][2] = ring->sync_seqno[2];
  601. }
  602. if (INTEL_INFO(dev)->gen >= 4) {
  603. error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
  604. error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
  605. error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
  606. error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
  607. error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
  608. if (ring->id == RCS)
  609. error->bbaddr = I915_READ64(BB_ADDR);
  610. } else {
  611. error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
  612. error->ipeir[ring->id] = I915_READ(IPEIR);
  613. error->ipehr[ring->id] = I915_READ(IPEHR);
  614. error->instdone[ring->id] = I915_READ(INSTDONE);
  615. }
  616. error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
  617. error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
  618. error->seqno[ring->id] = ring->get_seqno(ring, false);
  619. error->acthd[ring->id] = intel_ring_get_active_head(ring);
  620. error->head[ring->id] = I915_READ_HEAD(ring);
  621. error->tail[ring->id] = I915_READ_TAIL(ring);
  622. error->ctl[ring->id] = I915_READ_CTL(ring);
  623. error->cpu_ring_head[ring->id] = ring->head;
  624. error->cpu_ring_tail[ring->id] = ring->tail;
  625. error->hangcheck_score[ring->id] = ring->hangcheck.score;
  626. error->hangcheck_action[ring->id] = ring->hangcheck.action;
  627. }
  628. static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
  629. struct drm_i915_error_state *error,
  630. struct drm_i915_error_ring *ering)
  631. {
  632. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  633. struct drm_i915_gem_object *obj;
  634. /* Currently render ring is the only HW context user */
  635. if (ring->id != RCS || !error->ccid)
  636. return;
  637. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  638. if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
  639. ering->ctx = i915_error_object_create_sized(dev_priv,
  640. obj, 1);
  641. break;
  642. }
  643. }
  644. }
  645. static void i915_gem_record_rings(struct drm_device *dev,
  646. struct drm_i915_error_state *error)
  647. {
  648. struct drm_i915_private *dev_priv = dev->dev_private;
  649. struct intel_ring_buffer *ring;
  650. struct drm_i915_gem_request *request;
  651. int i, count;
  652. for_each_ring(ring, dev_priv, i) {
  653. i915_record_ring_state(dev, error, ring);
  654. error->ring[i].batchbuffer =
  655. i915_error_first_batchbuffer(dev_priv, ring);
  656. error->ring[i].ringbuffer =
  657. i915_error_object_create(dev_priv, ring->obj);
  658. i915_gem_record_active_context(ring, error, &error->ring[i]);
  659. count = 0;
  660. list_for_each_entry(request, &ring->request_list, list)
  661. count++;
  662. error->ring[i].num_requests = count;
  663. error->ring[i].requests =
  664. kcalloc(count, sizeof(*error->ring[i].requests),
  665. GFP_ATOMIC);
  666. if (error->ring[i].requests == NULL) {
  667. error->ring[i].num_requests = 0;
  668. continue;
  669. }
  670. count = 0;
  671. list_for_each_entry(request, &ring->request_list, list) {
  672. struct drm_i915_error_request *erq;
  673. erq = &error->ring[i].requests[count++];
  674. erq->seqno = request->seqno;
  675. erq->jiffies = request->emitted_jiffies;
  676. erq->tail = request->tail;
  677. }
  678. }
  679. }
  680. /* FIXME: Since pin count/bound list is global, we duplicate what we capture per
  681. * VM.
  682. */
  683. static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
  684. struct drm_i915_error_state *error,
  685. struct i915_address_space *vm,
  686. const int ndx)
  687. {
  688. struct drm_i915_error_buffer *active_bo = NULL, *pinned_bo = NULL;
  689. struct drm_i915_gem_object *obj;
  690. struct i915_vma *vma;
  691. int i;
  692. i = 0;
  693. list_for_each_entry(vma, &vm->active_list, mm_list)
  694. i++;
  695. error->active_bo_count[ndx] = i;
  696. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  697. if (obj->pin_count)
  698. i++;
  699. error->pinned_bo_count[ndx] = i - error->active_bo_count[ndx];
  700. if (i) {
  701. active_bo = kcalloc(i, sizeof(*active_bo), GFP_ATOMIC);
  702. if (active_bo)
  703. pinned_bo = active_bo + error->active_bo_count[ndx];
  704. }
  705. if (active_bo)
  706. error->active_bo_count[ndx] =
  707. capture_active_bo(active_bo,
  708. error->active_bo_count[ndx],
  709. &vm->active_list);
  710. if (pinned_bo)
  711. error->pinned_bo_count[ndx] =
  712. capture_pinned_bo(pinned_bo,
  713. error->pinned_bo_count[ndx],
  714. &dev_priv->mm.bound_list);
  715. error->active_bo[ndx] = active_bo;
  716. error->pinned_bo[ndx] = pinned_bo;
  717. }
  718. static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
  719. struct drm_i915_error_state *error)
  720. {
  721. struct i915_address_space *vm;
  722. int cnt = 0, i = 0;
  723. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  724. cnt++;
  725. if (WARN(cnt > 1, "Multiple VMs not yet supported\n"))
  726. cnt = 1;
  727. vm = &dev_priv->gtt.base;
  728. error->active_bo = kcalloc(cnt, sizeof(*error->active_bo), GFP_ATOMIC);
  729. error->pinned_bo = kcalloc(cnt, sizeof(*error->pinned_bo), GFP_ATOMIC);
  730. error->active_bo_count = kcalloc(cnt, sizeof(*error->active_bo_count),
  731. GFP_ATOMIC);
  732. error->pinned_bo_count = kcalloc(cnt, sizeof(*error->pinned_bo_count),
  733. GFP_ATOMIC);
  734. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  735. i915_gem_capture_vm(dev_priv, error, vm, i++);
  736. }
  737. /**
  738. * i915_capture_error_state - capture an error record for later analysis
  739. * @dev: drm device
  740. *
  741. * Should be called when an error is detected (either a hang or an error
  742. * interrupt) to capture error state from the time of the error. Fills
  743. * out a structure which becomes available in debugfs for user level tools
  744. * to pick up.
  745. */
  746. void i915_capture_error_state(struct drm_device *dev)
  747. {
  748. struct drm_i915_private *dev_priv = dev->dev_private;
  749. struct drm_i915_error_state *error;
  750. unsigned long flags;
  751. int pipe;
  752. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  753. error = dev_priv->gpu_error.first_error;
  754. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  755. if (error)
  756. return;
  757. /* Account for pipe specific data like PIPE*STAT */
  758. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  759. if (!error) {
  760. DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
  761. return;
  762. }
  763. DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n",
  764. dev->primary->index);
  765. DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
  766. DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
  767. DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
  768. DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
  769. kref_init(&error->ref);
  770. error->eir = I915_READ(EIR);
  771. error->pgtbl_er = I915_READ(PGTBL_ER);
  772. if (HAS_HW_CONTEXTS(dev))
  773. error->ccid = I915_READ(CCID);
  774. if (HAS_PCH_SPLIT(dev))
  775. error->ier = I915_READ(DEIER) | I915_READ(GTIER);
  776. else if (IS_VALLEYVIEW(dev))
  777. error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
  778. else if (IS_GEN2(dev))
  779. error->ier = I915_READ16(IER);
  780. else
  781. error->ier = I915_READ(IER);
  782. if (INTEL_INFO(dev)->gen >= 6)
  783. error->derrmr = I915_READ(DERRMR);
  784. if (IS_VALLEYVIEW(dev))
  785. error->forcewake = I915_READ(FORCEWAKE_VLV);
  786. else if (INTEL_INFO(dev)->gen >= 7)
  787. error->forcewake = I915_READ(FORCEWAKE_MT);
  788. else if (INTEL_INFO(dev)->gen == 6)
  789. error->forcewake = I915_READ(FORCEWAKE);
  790. if (!HAS_PCH_SPLIT(dev))
  791. for_each_pipe(pipe)
  792. error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
  793. if (INTEL_INFO(dev)->gen >= 6) {
  794. error->error = I915_READ(ERROR_GEN6);
  795. error->done_reg = I915_READ(DONE_REG);
  796. }
  797. if (INTEL_INFO(dev)->gen == 7)
  798. error->err_int = I915_READ(GEN7_ERR_INT);
  799. i915_get_extra_instdone(dev, error->extra_instdone);
  800. i915_gem_capture_buffers(dev_priv, error);
  801. i915_gem_record_fences(dev, error);
  802. i915_gem_record_rings(dev, error);
  803. do_gettimeofday(&error->time);
  804. error->overlay = intel_overlay_capture_error_state(dev);
  805. error->display = intel_display_capture_error_state(dev);
  806. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  807. if (dev_priv->gpu_error.first_error == NULL) {
  808. dev_priv->gpu_error.first_error = error;
  809. error = NULL;
  810. }
  811. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  812. if (error)
  813. i915_error_state_free(&error->ref);
  814. }
  815. void i915_error_state_get(struct drm_device *dev,
  816. struct i915_error_state_file_priv *error_priv)
  817. {
  818. struct drm_i915_private *dev_priv = dev->dev_private;
  819. unsigned long flags;
  820. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  821. error_priv->error = dev_priv->gpu_error.first_error;
  822. if (error_priv->error)
  823. kref_get(&error_priv->error->ref);
  824. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  825. }
  826. void i915_error_state_put(struct i915_error_state_file_priv *error_priv)
  827. {
  828. if (error_priv->error)
  829. kref_put(&error_priv->error->ref, i915_error_state_free);
  830. }
  831. void i915_destroy_error_state(struct drm_device *dev)
  832. {
  833. struct drm_i915_private *dev_priv = dev->dev_private;
  834. struct drm_i915_error_state *error;
  835. unsigned long flags;
  836. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  837. error = dev_priv->gpu_error.first_error;
  838. dev_priv->gpu_error.first_error = NULL;
  839. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  840. if (error)
  841. kref_put(&error->ref, i915_error_state_free);
  842. }
  843. const char *i915_cache_level_str(int type)
  844. {
  845. switch (type) {
  846. case I915_CACHE_NONE: return " uncached";
  847. case I915_CACHE_LLC: return " snooped or LLC";
  848. case I915_CACHE_L3_LLC: return " L3+LLC";
  849. case I915_CACHE_WT: return " WT";
  850. default: return "";
  851. }
  852. }
  853. /* NB: please notice the memset */
  854. void i915_get_extra_instdone(struct drm_device *dev, uint32_t *instdone)
  855. {
  856. struct drm_i915_private *dev_priv = dev->dev_private;
  857. memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
  858. switch (INTEL_INFO(dev)->gen) {
  859. case 2:
  860. case 3:
  861. instdone[0] = I915_READ(INSTDONE);
  862. break;
  863. case 4:
  864. case 5:
  865. case 6:
  866. instdone[0] = I915_READ(INSTDONE_I965);
  867. instdone[1] = I915_READ(INSTDONE1);
  868. break;
  869. default:
  870. WARN_ONCE(1, "Unsupported platform\n");
  871. case 7:
  872. instdone[0] = I915_READ(GEN7_INSTDONE_1);
  873. instdone[1] = I915_READ(GEN7_SC_INSTDONE);
  874. instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
  875. instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
  876. break;
  877. }
  878. }