i915_gpu_error.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * Copyright (c) 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Keith Packard <keithp@keithp.com>
  26. * Mika Kuoppala <mika.kuoppala@intel.com>
  27. *
  28. */
  29. #include <generated/utsrelease.h>
  30. #include "i915_drv.h"
  31. static const char *yesno(int v)
  32. {
  33. return v ? "yes" : "no";
  34. }
  35. static const char *ring_str(int ring)
  36. {
  37. switch (ring) {
  38. case RCS: return "render";
  39. case VCS: return "bsd";
  40. case BCS: return "blt";
  41. case VECS: return "vebox";
  42. default: return "";
  43. }
  44. }
  45. static const char *pin_flag(int pinned)
  46. {
  47. if (pinned > 0)
  48. return " P";
  49. else if (pinned < 0)
  50. return " p";
  51. else
  52. return "";
  53. }
  54. static const char *tiling_flag(int tiling)
  55. {
  56. switch (tiling) {
  57. default:
  58. case I915_TILING_NONE: return "";
  59. case I915_TILING_X: return " X";
  60. case I915_TILING_Y: return " Y";
  61. }
  62. }
  63. static const char *dirty_flag(int dirty)
  64. {
  65. return dirty ? " dirty" : "";
  66. }
  67. static const char *purgeable_flag(int purgeable)
  68. {
  69. return purgeable ? " purgeable" : "";
  70. }
  71. static bool __i915_error_ok(struct drm_i915_error_state_buf *e)
  72. {
  73. if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) {
  74. e->err = -ENOSPC;
  75. return false;
  76. }
  77. if (e->bytes == e->size - 1 || e->err)
  78. return false;
  79. return true;
  80. }
  81. static bool __i915_error_seek(struct drm_i915_error_state_buf *e,
  82. unsigned len)
  83. {
  84. if (e->pos + len <= e->start) {
  85. e->pos += len;
  86. return false;
  87. }
  88. /* First vsnprintf needs to fit in its entirety for memmove */
  89. if (len >= e->size) {
  90. e->err = -EIO;
  91. return false;
  92. }
  93. return true;
  94. }
  95. static void __i915_error_advance(struct drm_i915_error_state_buf *e,
  96. unsigned len)
  97. {
  98. /* If this is first printf in this window, adjust it so that
  99. * start position matches start of the buffer
  100. */
  101. if (e->pos < e->start) {
  102. const size_t off = e->start - e->pos;
  103. /* Should not happen but be paranoid */
  104. if (off > len || e->bytes) {
  105. e->err = -EIO;
  106. return;
  107. }
  108. memmove(e->buf, e->buf + off, len - off);
  109. e->bytes = len - off;
  110. e->pos = e->start;
  111. return;
  112. }
  113. e->bytes += len;
  114. e->pos += len;
  115. }
  116. static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
  117. const char *f, va_list args)
  118. {
  119. unsigned len;
  120. if (!__i915_error_ok(e))
  121. return;
  122. /* Seek the first printf which is hits start position */
  123. if (e->pos < e->start) {
  124. va_list tmp;
  125. va_copy(tmp, args);
  126. if (!__i915_error_seek(e, vsnprintf(NULL, 0, f, tmp)))
  127. return;
  128. }
  129. len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args);
  130. if (len >= e->size - e->bytes)
  131. len = e->size - e->bytes - 1;
  132. __i915_error_advance(e, len);
  133. }
  134. static void i915_error_puts(struct drm_i915_error_state_buf *e,
  135. const char *str)
  136. {
  137. unsigned len;
  138. if (!__i915_error_ok(e))
  139. return;
  140. len = strlen(str);
  141. /* Seek the first printf which is hits start position */
  142. if (e->pos < e->start) {
  143. if (!__i915_error_seek(e, len))
  144. return;
  145. }
  146. if (len >= e->size - e->bytes)
  147. len = e->size - e->bytes - 1;
  148. memcpy(e->buf + e->bytes, str, len);
  149. __i915_error_advance(e, len);
  150. }
  151. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  152. #define err_puts(e, s) i915_error_puts(e, s)
  153. static void print_error_buffers(struct drm_i915_error_state_buf *m,
  154. const char *name,
  155. struct drm_i915_error_buffer *err,
  156. int count)
  157. {
  158. err_printf(m, "%s [%d]:\n", name, count);
  159. while (count--) {
  160. err_printf(m, " %08x %8u %02x %02x %x %x",
  161. err->gtt_offset,
  162. err->size,
  163. err->read_domains,
  164. err->write_domain,
  165. err->rseqno, err->wseqno);
  166. err_puts(m, pin_flag(err->pinned));
  167. err_puts(m, tiling_flag(err->tiling));
  168. err_puts(m, dirty_flag(err->dirty));
  169. err_puts(m, purgeable_flag(err->purgeable));
  170. err_puts(m, err->ring != -1 ? " " : "");
  171. err_puts(m, ring_str(err->ring));
  172. err_puts(m, i915_cache_level_str(err->cache_level));
  173. if (err->name)
  174. err_printf(m, " (name: %d)", err->name);
  175. if (err->fence_reg != I915_FENCE_REG_NONE)
  176. err_printf(m, " (fence: %d)", err->fence_reg);
  177. err_puts(m, "\n");
  178. err++;
  179. }
  180. }
  181. static const char *hangcheck_action_to_str(enum intel_ring_hangcheck_action a)
  182. {
  183. switch (a) {
  184. case HANGCHECK_IDLE:
  185. return "idle";
  186. case HANGCHECK_WAIT:
  187. return "wait";
  188. case HANGCHECK_ACTIVE:
  189. return "active";
  190. case HANGCHECK_KICK:
  191. return "kick";
  192. case HANGCHECK_HUNG:
  193. return "hung";
  194. }
  195. return "unknown";
  196. }
  197. static void i915_ring_error_state(struct drm_i915_error_state_buf *m,
  198. struct drm_device *dev,
  199. struct drm_i915_error_state *error,
  200. unsigned ring)
  201. {
  202. BUG_ON(ring >= I915_NUM_RINGS); /* shut up confused gcc */
  203. err_printf(m, "%s command stream:\n", ring_str(ring));
  204. err_printf(m, " HEAD: 0x%08x\n", error->head[ring]);
  205. err_printf(m, " TAIL: 0x%08x\n", error->tail[ring]);
  206. err_printf(m, " CTL: 0x%08x\n", error->ctl[ring]);
  207. err_printf(m, " ACTHD: 0x%08x\n", error->acthd[ring]);
  208. err_printf(m, " IPEIR: 0x%08x\n", error->ipeir[ring]);
  209. err_printf(m, " IPEHR: 0x%08x\n", error->ipehr[ring]);
  210. err_printf(m, " INSTDONE: 0x%08x\n", error->instdone[ring]);
  211. if (ring == RCS && INTEL_INFO(dev)->gen >= 4)
  212. err_printf(m, " BBADDR: 0x%08llx\n", error->bbaddr);
  213. if (INTEL_INFO(dev)->gen >= 4)
  214. err_printf(m, " INSTPS: 0x%08x\n", error->instps[ring]);
  215. err_printf(m, " INSTPM: 0x%08x\n", error->instpm[ring]);
  216. err_printf(m, " FADDR: 0x%08x\n", error->faddr[ring]);
  217. if (INTEL_INFO(dev)->gen >= 6) {
  218. err_printf(m, " RC PSMI: 0x%08x\n", error->rc_psmi[ring]);
  219. err_printf(m, " FAULT_REG: 0x%08x\n", error->fault_reg[ring]);
  220. err_printf(m, " SYNC_0: 0x%08x [last synced 0x%08x]\n",
  221. error->semaphore_mboxes[ring][0],
  222. error->semaphore_seqno[ring][0]);
  223. err_printf(m, " SYNC_1: 0x%08x [last synced 0x%08x]\n",
  224. error->semaphore_mboxes[ring][1],
  225. error->semaphore_seqno[ring][1]);
  226. if (HAS_VEBOX(dev)) {
  227. err_printf(m, " SYNC_2: 0x%08x [last synced 0x%08x]\n",
  228. error->semaphore_mboxes[ring][2],
  229. error->semaphore_seqno[ring][2]);
  230. }
  231. }
  232. err_printf(m, " seqno: 0x%08x\n", error->seqno[ring]);
  233. err_printf(m, " waiting: %s\n", yesno(error->waiting[ring]));
  234. err_printf(m, " ring->head: 0x%08x\n", error->cpu_ring_head[ring]);
  235. err_printf(m, " ring->tail: 0x%08x\n", error->cpu_ring_tail[ring]);
  236. err_printf(m, " hangcheck: %s [%d]\n",
  237. hangcheck_action_to_str(error->hangcheck_action[ring]),
  238. error->hangcheck_score[ring]);
  239. }
  240. void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
  241. {
  242. va_list args;
  243. va_start(args, f);
  244. i915_error_vprintf(e, f, args);
  245. va_end(args);
  246. }
  247. int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
  248. const struct i915_error_state_file_priv *error_priv)
  249. {
  250. struct drm_device *dev = error_priv->dev;
  251. drm_i915_private_t *dev_priv = dev->dev_private;
  252. struct drm_i915_error_state *error = error_priv->error;
  253. struct intel_ring_buffer *ring;
  254. int i, j, page, offset, elt;
  255. if (!error) {
  256. err_printf(m, "no error state collected\n");
  257. goto out;
  258. }
  259. err_printf(m, "Time: %ld s %ld us\n", error->time.tv_sec,
  260. error->time.tv_usec);
  261. err_printf(m, "Kernel: " UTS_RELEASE "\n");
  262. err_printf(m, "PCI ID: 0x%04x\n", dev->pci_device);
  263. err_printf(m, "EIR: 0x%08x\n", error->eir);
  264. err_printf(m, "IER: 0x%08x\n", error->ier);
  265. err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
  266. err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
  267. err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
  268. err_printf(m, "CCID: 0x%08x\n", error->ccid);
  269. for (i = 0; i < dev_priv->num_fence_regs; i++)
  270. err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
  271. for (i = 0; i < ARRAY_SIZE(error->extra_instdone); i++)
  272. err_printf(m, " INSTDONE_%d: 0x%08x\n", i,
  273. error->extra_instdone[i]);
  274. if (INTEL_INFO(dev)->gen >= 6) {
  275. err_printf(m, "ERROR: 0x%08x\n", error->error);
  276. err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
  277. }
  278. if (INTEL_INFO(dev)->gen == 7)
  279. err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
  280. for_each_ring(ring, dev_priv, i)
  281. i915_ring_error_state(m, dev, error, i);
  282. if (error->active_bo)
  283. print_error_buffers(m, "Active",
  284. error->active_bo[0],
  285. error->active_bo_count[0]);
  286. if (error->pinned_bo)
  287. print_error_buffers(m, "Pinned",
  288. error->pinned_bo[0],
  289. error->pinned_bo_count[0]);
  290. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  291. struct drm_i915_error_object *obj;
  292. if ((obj = error->ring[i].batchbuffer)) {
  293. err_printf(m, "%s --- gtt_offset = 0x%08x\n",
  294. dev_priv->ring[i].name,
  295. obj->gtt_offset);
  296. offset = 0;
  297. for (page = 0; page < obj->page_count; page++) {
  298. for (elt = 0; elt < PAGE_SIZE/4; elt++) {
  299. err_printf(m, "%08x : %08x\n", offset,
  300. obj->pages[page][elt]);
  301. offset += 4;
  302. }
  303. }
  304. }
  305. if (error->ring[i].num_requests) {
  306. err_printf(m, "%s --- %d requests\n",
  307. dev_priv->ring[i].name,
  308. error->ring[i].num_requests);
  309. for (j = 0; j < error->ring[i].num_requests; j++) {
  310. err_printf(m, " seqno 0x%08x, emitted %ld, tail 0x%08x\n",
  311. error->ring[i].requests[j].seqno,
  312. error->ring[i].requests[j].jiffies,
  313. error->ring[i].requests[j].tail);
  314. }
  315. }
  316. if ((obj = error->ring[i].ringbuffer)) {
  317. err_printf(m, "%s --- ringbuffer = 0x%08x\n",
  318. dev_priv->ring[i].name,
  319. obj->gtt_offset);
  320. offset = 0;
  321. for (page = 0; page < obj->page_count; page++) {
  322. for (elt = 0; elt < PAGE_SIZE/4; elt++) {
  323. err_printf(m, "%08x : %08x\n",
  324. offset,
  325. obj->pages[page][elt]);
  326. offset += 4;
  327. }
  328. }
  329. }
  330. obj = error->ring[i].ctx;
  331. if (obj) {
  332. err_printf(m, "%s --- HW Context = 0x%08x\n",
  333. dev_priv->ring[i].name,
  334. obj->gtt_offset);
  335. offset = 0;
  336. for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
  337. err_printf(m, "[%04x] %08x %08x %08x %08x\n",
  338. offset,
  339. obj->pages[0][elt],
  340. obj->pages[0][elt+1],
  341. obj->pages[0][elt+2],
  342. obj->pages[0][elt+3]);
  343. offset += 16;
  344. }
  345. }
  346. }
  347. if (error->overlay)
  348. intel_overlay_print_error_state(m, error->overlay);
  349. if (error->display)
  350. intel_display_print_error_state(m, dev, error->display);
  351. out:
  352. if (m->bytes == 0 && m->err)
  353. return m->err;
  354. return 0;
  355. }
  356. int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
  357. size_t count, loff_t pos)
  358. {
  359. memset(ebuf, 0, sizeof(*ebuf));
  360. /* We need to have enough room to store any i915_error_state printf
  361. * so that we can move it to start position.
  362. */
  363. ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
  364. ebuf->buf = kmalloc(ebuf->size,
  365. GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
  366. if (ebuf->buf == NULL) {
  367. ebuf->size = PAGE_SIZE;
  368. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  369. }
  370. if (ebuf->buf == NULL) {
  371. ebuf->size = 128;
  372. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  373. }
  374. if (ebuf->buf == NULL)
  375. return -ENOMEM;
  376. ebuf->start = pos;
  377. return 0;
  378. }
  379. static void i915_error_object_free(struct drm_i915_error_object *obj)
  380. {
  381. int page;
  382. if (obj == NULL)
  383. return;
  384. for (page = 0; page < obj->page_count; page++)
  385. kfree(obj->pages[page]);
  386. kfree(obj);
  387. }
  388. static void i915_error_state_free(struct kref *error_ref)
  389. {
  390. struct drm_i915_error_state *error = container_of(error_ref,
  391. typeof(*error), ref);
  392. int i;
  393. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  394. i915_error_object_free(error->ring[i].batchbuffer);
  395. i915_error_object_free(error->ring[i].ringbuffer);
  396. i915_error_object_free(error->ring[i].ctx);
  397. kfree(error->ring[i].requests);
  398. }
  399. kfree(error->active_bo);
  400. kfree(error->overlay);
  401. kfree(error->display);
  402. kfree(error);
  403. }
  404. static struct drm_i915_error_object *
  405. i915_error_object_create_sized(struct drm_i915_private *dev_priv,
  406. struct drm_i915_gem_object *src,
  407. const int num_pages)
  408. {
  409. struct drm_i915_error_object *dst;
  410. int i;
  411. u32 reloc_offset;
  412. if (src == NULL || src->pages == NULL)
  413. return NULL;
  414. dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
  415. if (dst == NULL)
  416. return NULL;
  417. reloc_offset = dst->gtt_offset = i915_gem_obj_ggtt_offset(src);
  418. for (i = 0; i < num_pages; i++) {
  419. unsigned long flags;
  420. void *d;
  421. d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
  422. if (d == NULL)
  423. goto unwind;
  424. local_irq_save(flags);
  425. if (reloc_offset < dev_priv->gtt.mappable_end &&
  426. src->has_global_gtt_mapping) {
  427. void __iomem *s;
  428. /* Simply ignore tiling or any overlapping fence.
  429. * It's part of the error state, and this hopefully
  430. * captures what the GPU read.
  431. */
  432. s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  433. reloc_offset);
  434. memcpy_fromio(d, s, PAGE_SIZE);
  435. io_mapping_unmap_atomic(s);
  436. } else if (src->stolen) {
  437. unsigned long offset;
  438. offset = dev_priv->mm.stolen_base;
  439. offset += src->stolen->start;
  440. offset += i << PAGE_SHIFT;
  441. memcpy_fromio(d, (void __iomem *) offset, PAGE_SIZE);
  442. } else {
  443. struct page *page;
  444. void *s;
  445. page = i915_gem_object_get_page(src, i);
  446. drm_clflush_pages(&page, 1);
  447. s = kmap_atomic(page);
  448. memcpy(d, s, PAGE_SIZE);
  449. kunmap_atomic(s);
  450. drm_clflush_pages(&page, 1);
  451. }
  452. local_irq_restore(flags);
  453. dst->pages[i] = d;
  454. reloc_offset += PAGE_SIZE;
  455. }
  456. dst->page_count = num_pages;
  457. return dst;
  458. unwind:
  459. while (i--)
  460. kfree(dst->pages[i]);
  461. kfree(dst);
  462. return NULL;
  463. }
  464. #define i915_error_object_create(dev_priv, src) \
  465. i915_error_object_create_sized((dev_priv), (src), \
  466. (src)->base.size>>PAGE_SHIFT)
  467. static void capture_bo(struct drm_i915_error_buffer *err,
  468. struct drm_i915_gem_object *obj)
  469. {
  470. err->size = obj->base.size;
  471. err->name = obj->base.name;
  472. err->rseqno = obj->last_read_seqno;
  473. err->wseqno = obj->last_write_seqno;
  474. err->gtt_offset = i915_gem_obj_ggtt_offset(obj);
  475. err->read_domains = obj->base.read_domains;
  476. err->write_domain = obj->base.write_domain;
  477. err->fence_reg = obj->fence_reg;
  478. err->pinned = 0;
  479. if (obj->pin_count > 0)
  480. err->pinned = 1;
  481. if (obj->user_pin_count > 0)
  482. err->pinned = -1;
  483. err->tiling = obj->tiling_mode;
  484. err->dirty = obj->dirty;
  485. err->purgeable = obj->madv != I915_MADV_WILLNEED;
  486. err->ring = obj->ring ? obj->ring->id : -1;
  487. err->cache_level = obj->cache_level;
  488. }
  489. static u32 capture_active_bo(struct drm_i915_error_buffer *err,
  490. int count, struct list_head *head)
  491. {
  492. struct i915_vma *vma;
  493. int i = 0;
  494. list_for_each_entry(vma, head, mm_list) {
  495. capture_bo(err++, vma->obj);
  496. if (++i == count)
  497. break;
  498. }
  499. return i;
  500. }
  501. static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
  502. int count, struct list_head *head)
  503. {
  504. struct drm_i915_gem_object *obj;
  505. int i = 0;
  506. list_for_each_entry(obj, head, global_list) {
  507. if (obj->pin_count == 0)
  508. continue;
  509. capture_bo(err++, obj);
  510. if (++i == count)
  511. break;
  512. }
  513. return i;
  514. }
  515. static void i915_gem_record_fences(struct drm_device *dev,
  516. struct drm_i915_error_state *error)
  517. {
  518. struct drm_i915_private *dev_priv = dev->dev_private;
  519. int i;
  520. /* Fences */
  521. switch (INTEL_INFO(dev)->gen) {
  522. case 7:
  523. case 6:
  524. for (i = 0; i < dev_priv->num_fence_regs; i++)
  525. error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 + (i * 8));
  526. break;
  527. case 5:
  528. case 4:
  529. for (i = 0; i < 16; i++)
  530. error->fence[i] = I915_READ64(FENCE_REG_965_0 + (i * 8));
  531. break;
  532. case 3:
  533. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  534. for (i = 0; i < 8; i++)
  535. error->fence[i+8] = I915_READ(FENCE_REG_945_8 + (i * 4));
  536. case 2:
  537. for (i = 0; i < 8; i++)
  538. error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
  539. break;
  540. default:
  541. BUG();
  542. }
  543. }
  544. static struct drm_i915_error_object *
  545. i915_error_first_batchbuffer(struct drm_i915_private *dev_priv,
  546. struct intel_ring_buffer *ring)
  547. {
  548. struct i915_address_space *vm;
  549. struct i915_vma *vma;
  550. struct drm_i915_gem_object *obj;
  551. u32 seqno;
  552. if (!ring->get_seqno)
  553. return NULL;
  554. if (HAS_BROKEN_CS_TLB(dev_priv->dev)) {
  555. u32 acthd = I915_READ(ACTHD);
  556. if (WARN_ON(ring->id != RCS))
  557. return NULL;
  558. obj = ring->scratch.obj;
  559. if (acthd >= i915_gem_obj_ggtt_offset(obj) &&
  560. acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size)
  561. return i915_error_object_create(dev_priv, obj);
  562. }
  563. seqno = ring->get_seqno(ring, false);
  564. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  565. list_for_each_entry(vma, &vm->active_list, mm_list) {
  566. obj = vma->obj;
  567. if (obj->ring != ring)
  568. continue;
  569. if (i915_seqno_passed(seqno, obj->last_read_seqno))
  570. continue;
  571. if ((obj->base.read_domains & I915_GEM_DOMAIN_COMMAND) == 0)
  572. continue;
  573. /* We need to copy these to an anonymous buffer as the simplest
  574. * method to avoid being overwritten by userspace.
  575. */
  576. return i915_error_object_create(dev_priv, obj);
  577. }
  578. }
  579. return NULL;
  580. }
  581. static void i915_record_ring_state(struct drm_device *dev,
  582. struct drm_i915_error_state *error,
  583. struct intel_ring_buffer *ring)
  584. {
  585. struct drm_i915_private *dev_priv = dev->dev_private;
  586. if (INTEL_INFO(dev)->gen >= 6) {
  587. error->rc_psmi[ring->id] = I915_READ(ring->mmio_base + 0x50);
  588. error->fault_reg[ring->id] = I915_READ(RING_FAULT_REG(ring));
  589. error->semaphore_mboxes[ring->id][0]
  590. = I915_READ(RING_SYNC_0(ring->mmio_base));
  591. error->semaphore_mboxes[ring->id][1]
  592. = I915_READ(RING_SYNC_1(ring->mmio_base));
  593. error->semaphore_seqno[ring->id][0] = ring->sync_seqno[0];
  594. error->semaphore_seqno[ring->id][1] = ring->sync_seqno[1];
  595. }
  596. if (HAS_VEBOX(dev)) {
  597. error->semaphore_mboxes[ring->id][2] =
  598. I915_READ(RING_SYNC_2(ring->mmio_base));
  599. error->semaphore_seqno[ring->id][2] = ring->sync_seqno[2];
  600. }
  601. if (INTEL_INFO(dev)->gen >= 4) {
  602. error->faddr[ring->id] = I915_READ(RING_DMA_FADD(ring->mmio_base));
  603. error->ipeir[ring->id] = I915_READ(RING_IPEIR(ring->mmio_base));
  604. error->ipehr[ring->id] = I915_READ(RING_IPEHR(ring->mmio_base));
  605. error->instdone[ring->id] = I915_READ(RING_INSTDONE(ring->mmio_base));
  606. error->instps[ring->id] = I915_READ(RING_INSTPS(ring->mmio_base));
  607. if (ring->id == RCS)
  608. error->bbaddr = I915_READ64(BB_ADDR);
  609. } else {
  610. error->faddr[ring->id] = I915_READ(DMA_FADD_I8XX);
  611. error->ipeir[ring->id] = I915_READ(IPEIR);
  612. error->ipehr[ring->id] = I915_READ(IPEHR);
  613. error->instdone[ring->id] = I915_READ(INSTDONE);
  614. }
  615. error->waiting[ring->id] = waitqueue_active(&ring->irq_queue);
  616. error->instpm[ring->id] = I915_READ(RING_INSTPM(ring->mmio_base));
  617. error->seqno[ring->id] = ring->get_seqno(ring, false);
  618. error->acthd[ring->id] = intel_ring_get_active_head(ring);
  619. error->head[ring->id] = I915_READ_HEAD(ring);
  620. error->tail[ring->id] = I915_READ_TAIL(ring);
  621. error->ctl[ring->id] = I915_READ_CTL(ring);
  622. error->cpu_ring_head[ring->id] = ring->head;
  623. error->cpu_ring_tail[ring->id] = ring->tail;
  624. error->hangcheck_score[ring->id] = ring->hangcheck.score;
  625. error->hangcheck_action[ring->id] = ring->hangcheck.action;
  626. }
  627. static void i915_gem_record_active_context(struct intel_ring_buffer *ring,
  628. struct drm_i915_error_state *error,
  629. struct drm_i915_error_ring *ering)
  630. {
  631. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  632. struct drm_i915_gem_object *obj;
  633. /* Currently render ring is the only HW context user */
  634. if (ring->id != RCS || !error->ccid)
  635. return;
  636. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  637. if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
  638. ering->ctx = i915_error_object_create_sized(dev_priv,
  639. obj, 1);
  640. break;
  641. }
  642. }
  643. }
  644. static void i915_gem_record_rings(struct drm_device *dev,
  645. struct drm_i915_error_state *error)
  646. {
  647. struct drm_i915_private *dev_priv = dev->dev_private;
  648. struct intel_ring_buffer *ring;
  649. struct drm_i915_gem_request *request;
  650. int i, count;
  651. for_each_ring(ring, dev_priv, i) {
  652. i915_record_ring_state(dev, error, ring);
  653. error->ring[i].batchbuffer =
  654. i915_error_first_batchbuffer(dev_priv, ring);
  655. error->ring[i].ringbuffer =
  656. i915_error_object_create(dev_priv, ring->obj);
  657. i915_gem_record_active_context(ring, error, &error->ring[i]);
  658. count = 0;
  659. list_for_each_entry(request, &ring->request_list, list)
  660. count++;
  661. error->ring[i].num_requests = count;
  662. error->ring[i].requests =
  663. kcalloc(count, sizeof(*error->ring[i].requests),
  664. GFP_ATOMIC);
  665. if (error->ring[i].requests == NULL) {
  666. error->ring[i].num_requests = 0;
  667. continue;
  668. }
  669. count = 0;
  670. list_for_each_entry(request, &ring->request_list, list) {
  671. struct drm_i915_error_request *erq;
  672. erq = &error->ring[i].requests[count++];
  673. erq->seqno = request->seqno;
  674. erq->jiffies = request->emitted_jiffies;
  675. erq->tail = request->tail;
  676. }
  677. }
  678. }
  679. /* FIXME: Since pin count/bound list is global, we duplicate what we capture per
  680. * VM.
  681. */
  682. static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
  683. struct drm_i915_error_state *error,
  684. struct i915_address_space *vm,
  685. const int ndx)
  686. {
  687. struct drm_i915_error_buffer *active_bo = NULL, *pinned_bo = NULL;
  688. struct drm_i915_gem_object *obj;
  689. struct i915_vma *vma;
  690. int i;
  691. i = 0;
  692. list_for_each_entry(vma, &vm->active_list, mm_list)
  693. i++;
  694. error->active_bo_count[ndx] = i;
  695. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  696. if (obj->pin_count)
  697. i++;
  698. error->pinned_bo_count[ndx] = i - error->active_bo_count[ndx];
  699. if (i) {
  700. active_bo = kcalloc(i, sizeof(*active_bo), GFP_ATOMIC);
  701. if (active_bo)
  702. pinned_bo = active_bo + error->active_bo_count[ndx];
  703. }
  704. if (active_bo)
  705. error->active_bo_count[ndx] =
  706. capture_active_bo(active_bo,
  707. error->active_bo_count[ndx],
  708. &vm->active_list);
  709. if (pinned_bo)
  710. error->pinned_bo_count[ndx] =
  711. capture_pinned_bo(pinned_bo,
  712. error->pinned_bo_count[ndx],
  713. &dev_priv->mm.bound_list);
  714. error->active_bo[ndx] = active_bo;
  715. error->pinned_bo[ndx] = pinned_bo;
  716. }
  717. static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
  718. struct drm_i915_error_state *error)
  719. {
  720. struct i915_address_space *vm;
  721. int cnt = 0, i = 0;
  722. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  723. cnt++;
  724. if (WARN(cnt > 1, "Multiple VMs not yet supported\n"))
  725. cnt = 1;
  726. vm = &dev_priv->gtt.base;
  727. error->active_bo = kcalloc(cnt, sizeof(*error->active_bo), GFP_ATOMIC);
  728. error->pinned_bo = kcalloc(cnt, sizeof(*error->pinned_bo), GFP_ATOMIC);
  729. error->active_bo_count = kcalloc(cnt, sizeof(*error->active_bo_count),
  730. GFP_ATOMIC);
  731. error->pinned_bo_count = kcalloc(cnt, sizeof(*error->pinned_bo_count),
  732. GFP_ATOMIC);
  733. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  734. i915_gem_capture_vm(dev_priv, error, vm, i++);
  735. }
  736. /**
  737. * i915_capture_error_state - capture an error record for later analysis
  738. * @dev: drm device
  739. *
  740. * Should be called when an error is detected (either a hang or an error
  741. * interrupt) to capture error state from the time of the error. Fills
  742. * out a structure which becomes available in debugfs for user level tools
  743. * to pick up.
  744. */
  745. void i915_capture_error_state(struct drm_device *dev)
  746. {
  747. struct drm_i915_private *dev_priv = dev->dev_private;
  748. struct drm_i915_error_state *error;
  749. unsigned long flags;
  750. int pipe;
  751. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  752. error = dev_priv->gpu_error.first_error;
  753. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  754. if (error)
  755. return;
  756. /* Account for pipe specific data like PIPE*STAT */
  757. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  758. if (!error) {
  759. DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
  760. return;
  761. }
  762. DRM_INFO("capturing error event; look for more information in "
  763. "/sys/class/drm/card%d/error\n", dev->primary->index);
  764. kref_init(&error->ref);
  765. error->eir = I915_READ(EIR);
  766. error->pgtbl_er = I915_READ(PGTBL_ER);
  767. if (HAS_HW_CONTEXTS(dev))
  768. error->ccid = I915_READ(CCID);
  769. if (HAS_PCH_SPLIT(dev))
  770. error->ier = I915_READ(DEIER) | I915_READ(GTIER);
  771. else if (IS_VALLEYVIEW(dev))
  772. error->ier = I915_READ(GTIER) | I915_READ(VLV_IER);
  773. else if (IS_GEN2(dev))
  774. error->ier = I915_READ16(IER);
  775. else
  776. error->ier = I915_READ(IER);
  777. if (INTEL_INFO(dev)->gen >= 6)
  778. error->derrmr = I915_READ(DERRMR);
  779. if (IS_VALLEYVIEW(dev))
  780. error->forcewake = I915_READ(FORCEWAKE_VLV);
  781. else if (INTEL_INFO(dev)->gen >= 7)
  782. error->forcewake = I915_READ(FORCEWAKE_MT);
  783. else if (INTEL_INFO(dev)->gen == 6)
  784. error->forcewake = I915_READ(FORCEWAKE);
  785. if (!HAS_PCH_SPLIT(dev))
  786. for_each_pipe(pipe)
  787. error->pipestat[pipe] = I915_READ(PIPESTAT(pipe));
  788. if (INTEL_INFO(dev)->gen >= 6) {
  789. error->error = I915_READ(ERROR_GEN6);
  790. error->done_reg = I915_READ(DONE_REG);
  791. }
  792. if (INTEL_INFO(dev)->gen == 7)
  793. error->err_int = I915_READ(GEN7_ERR_INT);
  794. i915_get_extra_instdone(dev, error->extra_instdone);
  795. i915_gem_capture_buffers(dev_priv, error);
  796. i915_gem_record_fences(dev, error);
  797. i915_gem_record_rings(dev, error);
  798. do_gettimeofday(&error->time);
  799. error->overlay = intel_overlay_capture_error_state(dev);
  800. error->display = intel_display_capture_error_state(dev);
  801. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  802. if (dev_priv->gpu_error.first_error == NULL) {
  803. dev_priv->gpu_error.first_error = error;
  804. error = NULL;
  805. }
  806. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  807. if (error)
  808. i915_error_state_free(&error->ref);
  809. }
  810. void i915_error_state_get(struct drm_device *dev,
  811. struct i915_error_state_file_priv *error_priv)
  812. {
  813. struct drm_i915_private *dev_priv = dev->dev_private;
  814. unsigned long flags;
  815. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  816. error_priv->error = dev_priv->gpu_error.first_error;
  817. if (error_priv->error)
  818. kref_get(&error_priv->error->ref);
  819. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  820. }
  821. void i915_error_state_put(struct i915_error_state_file_priv *error_priv)
  822. {
  823. if (error_priv->error)
  824. kref_put(&error_priv->error->ref, i915_error_state_free);
  825. }
  826. void i915_destroy_error_state(struct drm_device *dev)
  827. {
  828. struct drm_i915_private *dev_priv = dev->dev_private;
  829. struct drm_i915_error_state *error;
  830. unsigned long flags;
  831. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  832. error = dev_priv->gpu_error.first_error;
  833. dev_priv->gpu_error.first_error = NULL;
  834. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  835. if (error)
  836. kref_put(&error->ref, i915_error_state_free);
  837. }
  838. const char *i915_cache_level_str(int type)
  839. {
  840. switch (type) {
  841. case I915_CACHE_NONE: return " uncached";
  842. case I915_CACHE_LLC: return " snooped or LLC";
  843. case I915_CACHE_L3_LLC: return " L3+LLC";
  844. case I915_CACHE_WT: return " WT";
  845. default: return "";
  846. }
  847. }
  848. /* NB: please notice the memset */
  849. void i915_get_extra_instdone(struct drm_device *dev, uint32_t *instdone)
  850. {
  851. struct drm_i915_private *dev_priv = dev->dev_private;
  852. memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
  853. switch (INTEL_INFO(dev)->gen) {
  854. case 2:
  855. case 3:
  856. instdone[0] = I915_READ(INSTDONE);
  857. break;
  858. case 4:
  859. case 5:
  860. case 6:
  861. instdone[0] = I915_READ(INSTDONE_I965);
  862. instdone[1] = I915_READ(INSTDONE1);
  863. break;
  864. default:
  865. WARN_ONCE(1, "Unsupported platform\n");
  866. case 7:
  867. instdone[0] = I915_READ(GEN7_INSTDONE_1);
  868. instdone[1] = I915_READ(GEN7_SC_INSTDONE);
  869. instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
  870. instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
  871. break;
  872. }
  873. }