i915_gpu_error.c 25 KB

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