i915_gpu_error.c 27 KB

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