i915_gpu_error.c 28 KB

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