i915_gpu_error.c 27 KB

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