i915_gpu_error.c 27 KB

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