i915_gpu_error.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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. case VCS2: return "bsd2";
  43. default: return "";
  44. }
  45. }
  46. static const char *pin_flag(int pinned)
  47. {
  48. if (pinned > 0)
  49. return " P";
  50. else if (pinned < 0)
  51. return " p";
  52. else
  53. return "";
  54. }
  55. static const char *tiling_flag(int tiling)
  56. {
  57. switch (tiling) {
  58. default:
  59. case I915_TILING_NONE: return "";
  60. case I915_TILING_X: return " X";
  61. case I915_TILING_Y: return " Y";
  62. }
  63. }
  64. static const char *dirty_flag(int dirty)
  65. {
  66. return dirty ? " dirty" : "";
  67. }
  68. static const char *purgeable_flag(int purgeable)
  69. {
  70. return purgeable ? " purgeable" : "";
  71. }
  72. static bool __i915_error_ok(struct drm_i915_error_state_buf *e)
  73. {
  74. if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) {
  75. e->err = -ENOSPC;
  76. return false;
  77. }
  78. if (e->bytes == e->size - 1 || e->err)
  79. return false;
  80. return true;
  81. }
  82. static bool __i915_error_seek(struct drm_i915_error_state_buf *e,
  83. unsigned len)
  84. {
  85. if (e->pos + len <= e->start) {
  86. e->pos += len;
  87. return false;
  88. }
  89. /* First vsnprintf needs to fit in its entirety for memmove */
  90. if (len >= e->size) {
  91. e->err = -EIO;
  92. return false;
  93. }
  94. return true;
  95. }
  96. static void __i915_error_advance(struct drm_i915_error_state_buf *e,
  97. unsigned len)
  98. {
  99. /* If this is first printf in this window, adjust it so that
  100. * start position matches start of the buffer
  101. */
  102. if (e->pos < e->start) {
  103. const size_t off = e->start - e->pos;
  104. /* Should not happen but be paranoid */
  105. if (off > len || e->bytes) {
  106. e->err = -EIO;
  107. return;
  108. }
  109. memmove(e->buf, e->buf + off, len - off);
  110. e->bytes = len - off;
  111. e->pos = e->start;
  112. return;
  113. }
  114. e->bytes += len;
  115. e->pos += len;
  116. }
  117. static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
  118. const char *f, va_list args)
  119. {
  120. unsigned len;
  121. if (!__i915_error_ok(e))
  122. return;
  123. /* Seek the first printf which is hits start position */
  124. if (e->pos < e->start) {
  125. va_list tmp;
  126. va_copy(tmp, args);
  127. len = vsnprintf(NULL, 0, f, tmp);
  128. va_end(tmp);
  129. if (!__i915_error_seek(e, len))
  130. return;
  131. }
  132. len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args);
  133. if (len >= e->size - e->bytes)
  134. len = e->size - e->bytes - 1;
  135. __i915_error_advance(e, len);
  136. }
  137. static void i915_error_puts(struct drm_i915_error_state_buf *e,
  138. const char *str)
  139. {
  140. unsigned len;
  141. if (!__i915_error_ok(e))
  142. return;
  143. len = strlen(str);
  144. /* Seek the first printf which is hits start position */
  145. if (e->pos < e->start) {
  146. if (!__i915_error_seek(e, len))
  147. return;
  148. }
  149. if (len >= e->size - e->bytes)
  150. len = e->size - e->bytes - 1;
  151. memcpy(e->buf + e->bytes, str, len);
  152. __i915_error_advance(e, len);
  153. }
  154. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  155. #define err_puts(e, s) i915_error_puts(e, s)
  156. static void print_error_buffers(struct drm_i915_error_state_buf *m,
  157. const char *name,
  158. struct drm_i915_error_buffer *err,
  159. int count)
  160. {
  161. int i;
  162. err_printf(m, " %s [%d]:\n", name, count);
  163. while (count--) {
  164. err_printf(m, " %08x %8u %02x %02x [ ",
  165. err->gtt_offset,
  166. err->size,
  167. err->read_domains,
  168. err->write_domain);
  169. for (i = 0; i < I915_NUM_RINGS; i++)
  170. err_printf(m, "%02x ", err->rseqno[i]);
  171. err_printf(m, "] %02x", err->wseqno);
  172. err_puts(m, pin_flag(err->pinned));
  173. err_puts(m, tiling_flag(err->tiling));
  174. err_puts(m, dirty_flag(err->dirty));
  175. err_puts(m, purgeable_flag(err->purgeable));
  176. err_puts(m, err->userptr ? " userptr" : "");
  177. err_puts(m, err->ring != -1 ? " " : "");
  178. err_puts(m, ring_str(err->ring));
  179. err_puts(m, i915_cache_level_str(m->i915, err->cache_level));
  180. if (err->name)
  181. err_printf(m, " (name: %d)", err->name);
  182. if (err->fence_reg != I915_FENCE_REG_NONE)
  183. err_printf(m, " (fence: %d)", err->fence_reg);
  184. err_puts(m, "\n");
  185. err++;
  186. }
  187. }
  188. static const char *hangcheck_action_to_str(enum intel_ring_hangcheck_action a)
  189. {
  190. switch (a) {
  191. case HANGCHECK_IDLE:
  192. return "idle";
  193. case HANGCHECK_WAIT:
  194. return "wait";
  195. case HANGCHECK_ACTIVE:
  196. return "active";
  197. case HANGCHECK_ACTIVE_LOOP:
  198. return "active (loop)";
  199. case HANGCHECK_KICK:
  200. return "kick";
  201. case HANGCHECK_HUNG:
  202. return "hung";
  203. }
  204. return "unknown";
  205. }
  206. static void i915_ring_error_state(struct drm_i915_error_state_buf *m,
  207. struct drm_device *dev,
  208. struct drm_i915_error_state *error,
  209. int ring_idx)
  210. {
  211. struct drm_i915_error_ring *ring = &error->ring[ring_idx];
  212. if (!ring->valid)
  213. return;
  214. err_printf(m, "%s command stream:\n", ring_str(ring_idx));
  215. err_printf(m, " START: 0x%08x\n", ring->start);
  216. err_printf(m, " HEAD: 0x%08x\n", ring->head);
  217. err_printf(m, " TAIL: 0x%08x\n", ring->tail);
  218. err_printf(m, " CTL: 0x%08x\n", ring->ctl);
  219. err_printf(m, " HWS: 0x%08x\n", ring->hws);
  220. err_printf(m, " ACTHD: 0x%08x %08x\n", (u32)(ring->acthd>>32), (u32)ring->acthd);
  221. err_printf(m, " IPEIR: 0x%08x\n", ring->ipeir);
  222. err_printf(m, " IPEHR: 0x%08x\n", ring->ipehr);
  223. err_printf(m, " INSTDONE: 0x%08x\n", ring->instdone);
  224. if (INTEL_INFO(dev)->gen >= 4) {
  225. err_printf(m, " BBADDR: 0x%08x %08x\n", (u32)(ring->bbaddr>>32), (u32)ring->bbaddr);
  226. err_printf(m, " BB_STATE: 0x%08x\n", ring->bbstate);
  227. err_printf(m, " INSTPS: 0x%08x\n", ring->instps);
  228. }
  229. err_printf(m, " INSTPM: 0x%08x\n", ring->instpm);
  230. err_printf(m, " FADDR: 0x%08x %08x\n", upper_32_bits(ring->faddr),
  231. lower_32_bits(ring->faddr));
  232. if (INTEL_INFO(dev)->gen >= 6) {
  233. err_printf(m, " RC PSMI: 0x%08x\n", ring->rc_psmi);
  234. err_printf(m, " FAULT_REG: 0x%08x\n", ring->fault_reg);
  235. err_printf(m, " SYNC_0: 0x%08x [last synced 0x%08x]\n",
  236. ring->semaphore_mboxes[0],
  237. ring->semaphore_seqno[0]);
  238. err_printf(m, " SYNC_1: 0x%08x [last synced 0x%08x]\n",
  239. ring->semaphore_mboxes[1],
  240. ring->semaphore_seqno[1]);
  241. if (HAS_VEBOX(dev)) {
  242. err_printf(m, " SYNC_2: 0x%08x [last synced 0x%08x]\n",
  243. ring->semaphore_mboxes[2],
  244. ring->semaphore_seqno[2]);
  245. }
  246. }
  247. if (USES_PPGTT(dev)) {
  248. err_printf(m, " GFX_MODE: 0x%08x\n", ring->vm_info.gfx_mode);
  249. if (INTEL_INFO(dev)->gen >= 8) {
  250. int i;
  251. for (i = 0; i < 4; i++)
  252. err_printf(m, " PDP%d: 0x%016llx\n",
  253. i, ring->vm_info.pdp[i]);
  254. } else {
  255. err_printf(m, " PP_DIR_BASE: 0x%08x\n",
  256. ring->vm_info.pp_dir_base);
  257. }
  258. }
  259. err_printf(m, " seqno: 0x%08x\n", ring->seqno);
  260. err_printf(m, " waiting: %s\n", yesno(ring->waiting));
  261. err_printf(m, " ring->head: 0x%08x\n", ring->cpu_ring_head);
  262. err_printf(m, " ring->tail: 0x%08x\n", ring->cpu_ring_tail);
  263. err_printf(m, " hangcheck: %s [%d]\n",
  264. hangcheck_action_to_str(ring->hangcheck_action),
  265. ring->hangcheck_score);
  266. }
  267. void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
  268. {
  269. va_list args;
  270. va_start(args, f);
  271. i915_error_vprintf(e, f, args);
  272. va_end(args);
  273. }
  274. static void print_error_obj(struct drm_i915_error_state_buf *m,
  275. struct drm_i915_error_object *obj)
  276. {
  277. int page, offset, elt;
  278. for (page = offset = 0; page < obj->page_count; page++) {
  279. for (elt = 0; elt < PAGE_SIZE/4; elt++) {
  280. err_printf(m, "%08x : %08x\n", offset,
  281. obj->pages[page][elt]);
  282. offset += 4;
  283. }
  284. }
  285. }
  286. int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
  287. const struct i915_error_state_file_priv *error_priv)
  288. {
  289. struct drm_device *dev = error_priv->dev;
  290. struct drm_i915_private *dev_priv = dev->dev_private;
  291. struct drm_i915_error_state *error = error_priv->error;
  292. struct drm_i915_error_object *obj;
  293. int i, j, offset, elt;
  294. int max_hangcheck_score;
  295. if (!error) {
  296. err_printf(m, "no error state collected\n");
  297. goto out;
  298. }
  299. err_printf(m, "%s\n", error->error_msg);
  300. err_printf(m, "Time: %ld s %ld us\n", error->time.tv_sec,
  301. error->time.tv_usec);
  302. err_printf(m, "Kernel: " UTS_RELEASE "\n");
  303. max_hangcheck_score = 0;
  304. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  305. if (error->ring[i].hangcheck_score > max_hangcheck_score)
  306. max_hangcheck_score = error->ring[i].hangcheck_score;
  307. }
  308. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  309. if (error->ring[i].hangcheck_score == max_hangcheck_score &&
  310. error->ring[i].pid != -1) {
  311. err_printf(m, "Active process (on ring %s): %s [%d]\n",
  312. ring_str(i),
  313. error->ring[i].comm,
  314. error->ring[i].pid);
  315. }
  316. }
  317. err_printf(m, "Reset count: %u\n", error->reset_count);
  318. err_printf(m, "Suspend count: %u\n", error->suspend_count);
  319. err_printf(m, "PCI ID: 0x%04x\n", dev->pdev->device);
  320. err_printf(m, "IOMMU enabled?: %d\n", error->iommu);
  321. err_printf(m, "EIR: 0x%08x\n", error->eir);
  322. err_printf(m, "IER: 0x%08x\n", error->ier);
  323. if (INTEL_INFO(dev)->gen >= 8) {
  324. for (i = 0; i < 4; i++)
  325. err_printf(m, "GTIER gt %d: 0x%08x\n", i,
  326. error->gtier[i]);
  327. } else if (HAS_PCH_SPLIT(dev) || IS_VALLEYVIEW(dev))
  328. err_printf(m, "GTIER: 0x%08x\n", error->gtier[0]);
  329. err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
  330. err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
  331. err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
  332. err_printf(m, "CCID: 0x%08x\n", error->ccid);
  333. err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
  334. for (i = 0; i < dev_priv->num_fence_regs; i++)
  335. err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
  336. for (i = 0; i < ARRAY_SIZE(error->extra_instdone); i++)
  337. err_printf(m, " INSTDONE_%d: 0x%08x\n", i,
  338. error->extra_instdone[i]);
  339. if (INTEL_INFO(dev)->gen >= 6) {
  340. err_printf(m, "ERROR: 0x%08x\n", error->error);
  341. if (INTEL_INFO(dev)->gen >= 8)
  342. err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n",
  343. error->fault_data1, error->fault_data0);
  344. err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
  345. }
  346. if (INTEL_INFO(dev)->gen == 7)
  347. err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
  348. for (i = 0; i < ARRAY_SIZE(error->ring); i++)
  349. i915_ring_error_state(m, dev, error, i);
  350. for (i = 0; i < error->vm_count; i++) {
  351. err_printf(m, "vm[%d]\n", i);
  352. print_error_buffers(m, "Active",
  353. error->active_bo[i],
  354. error->active_bo_count[i]);
  355. print_error_buffers(m, "Pinned",
  356. error->pinned_bo[i],
  357. error->pinned_bo_count[i]);
  358. }
  359. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  360. obj = error->ring[i].batchbuffer;
  361. if (obj) {
  362. err_puts(m, dev_priv->ring[i].name);
  363. if (error->ring[i].pid != -1)
  364. err_printf(m, " (submitted by %s [%d])",
  365. error->ring[i].comm,
  366. error->ring[i].pid);
  367. err_printf(m, " --- gtt_offset = 0x%08x\n",
  368. obj->gtt_offset);
  369. print_error_obj(m, obj);
  370. }
  371. obj = error->ring[i].wa_batchbuffer;
  372. if (obj) {
  373. err_printf(m, "%s (w/a) --- gtt_offset = 0x%08x\n",
  374. dev_priv->ring[i].name, obj->gtt_offset);
  375. print_error_obj(m, obj);
  376. }
  377. if (error->ring[i].num_requests) {
  378. err_printf(m, "%s --- %d requests\n",
  379. dev_priv->ring[i].name,
  380. error->ring[i].num_requests);
  381. for (j = 0; j < error->ring[i].num_requests; j++) {
  382. err_printf(m, " seqno 0x%08x, emitted %ld, tail 0x%08x\n",
  383. error->ring[i].requests[j].seqno,
  384. error->ring[i].requests[j].jiffies,
  385. error->ring[i].requests[j].tail);
  386. }
  387. }
  388. if ((obj = error->ring[i].ringbuffer)) {
  389. err_printf(m, "%s --- ringbuffer = 0x%08x\n",
  390. dev_priv->ring[i].name,
  391. obj->gtt_offset);
  392. print_error_obj(m, obj);
  393. }
  394. if ((obj = error->ring[i].hws_page)) {
  395. err_printf(m, "%s --- HW Status = 0x%08x\n",
  396. dev_priv->ring[i].name,
  397. obj->gtt_offset);
  398. offset = 0;
  399. for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
  400. err_printf(m, "[%04x] %08x %08x %08x %08x\n",
  401. offset,
  402. obj->pages[0][elt],
  403. obj->pages[0][elt+1],
  404. obj->pages[0][elt+2],
  405. obj->pages[0][elt+3]);
  406. offset += 16;
  407. }
  408. }
  409. if ((obj = error->ring[i].ctx)) {
  410. err_printf(m, "%s --- HW Context = 0x%08x\n",
  411. dev_priv->ring[i].name,
  412. obj->gtt_offset);
  413. print_error_obj(m, obj);
  414. }
  415. }
  416. if ((obj = error->semaphore_obj)) {
  417. err_printf(m, "Semaphore page = 0x%08x\n", obj->gtt_offset);
  418. for (elt = 0; elt < PAGE_SIZE/16; elt += 4) {
  419. err_printf(m, "[%04x] %08x %08x %08x %08x\n",
  420. elt * 4,
  421. obj->pages[0][elt],
  422. obj->pages[0][elt+1],
  423. obj->pages[0][elt+2],
  424. obj->pages[0][elt+3]);
  425. }
  426. }
  427. if (error->overlay)
  428. intel_overlay_print_error_state(m, error->overlay);
  429. if (error->display)
  430. intel_display_print_error_state(m, dev, error->display);
  431. out:
  432. if (m->bytes == 0 && m->err)
  433. return m->err;
  434. return 0;
  435. }
  436. int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
  437. struct drm_i915_private *i915,
  438. size_t count, loff_t pos)
  439. {
  440. memset(ebuf, 0, sizeof(*ebuf));
  441. ebuf->i915 = i915;
  442. /* We need to have enough room to store any i915_error_state printf
  443. * so that we can move it to start position.
  444. */
  445. ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
  446. ebuf->buf = kmalloc(ebuf->size,
  447. GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
  448. if (ebuf->buf == NULL) {
  449. ebuf->size = PAGE_SIZE;
  450. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  451. }
  452. if (ebuf->buf == NULL) {
  453. ebuf->size = 128;
  454. ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
  455. }
  456. if (ebuf->buf == NULL)
  457. return -ENOMEM;
  458. ebuf->start = pos;
  459. return 0;
  460. }
  461. static void i915_error_object_free(struct drm_i915_error_object *obj)
  462. {
  463. int page;
  464. if (obj == NULL)
  465. return;
  466. for (page = 0; page < obj->page_count; page++)
  467. kfree(obj->pages[page]);
  468. kfree(obj);
  469. }
  470. static void i915_error_state_free(struct kref *error_ref)
  471. {
  472. struct drm_i915_error_state *error = container_of(error_ref,
  473. typeof(*error), ref);
  474. int i;
  475. for (i = 0; i < ARRAY_SIZE(error->ring); i++) {
  476. i915_error_object_free(error->ring[i].batchbuffer);
  477. i915_error_object_free(error->ring[i].wa_batchbuffer);
  478. i915_error_object_free(error->ring[i].ringbuffer);
  479. i915_error_object_free(error->ring[i].hws_page);
  480. i915_error_object_free(error->ring[i].ctx);
  481. kfree(error->ring[i].requests);
  482. }
  483. i915_error_object_free(error->semaphore_obj);
  484. for (i = 0; i < error->vm_count; i++)
  485. kfree(error->active_bo[i]);
  486. kfree(error->active_bo);
  487. kfree(error->active_bo_count);
  488. kfree(error->pinned_bo);
  489. kfree(error->pinned_bo_count);
  490. kfree(error->overlay);
  491. kfree(error->display);
  492. kfree(error);
  493. }
  494. static struct drm_i915_error_object *
  495. i915_error_object_create(struct drm_i915_private *dev_priv,
  496. struct drm_i915_gem_object *src,
  497. struct i915_address_space *vm)
  498. {
  499. struct drm_i915_error_object *dst;
  500. struct i915_vma *vma = NULL;
  501. int num_pages;
  502. bool use_ggtt;
  503. int i = 0;
  504. u32 reloc_offset;
  505. if (src == NULL || src->pages == NULL)
  506. return NULL;
  507. num_pages = src->base.size >> PAGE_SHIFT;
  508. dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), GFP_ATOMIC);
  509. if (dst == NULL)
  510. return NULL;
  511. if (i915_gem_obj_bound(src, vm))
  512. dst->gtt_offset = i915_gem_obj_offset(src, vm);
  513. else
  514. dst->gtt_offset = -1;
  515. reloc_offset = dst->gtt_offset;
  516. if (i915_is_ggtt(vm))
  517. vma = i915_gem_obj_to_ggtt(src);
  518. use_ggtt = (src->cache_level == I915_CACHE_NONE &&
  519. vma && (vma->bound & GLOBAL_BIND) &&
  520. reloc_offset + num_pages * PAGE_SIZE <= dev_priv->gtt.mappable_end);
  521. /* Cannot access stolen address directly, try to use the aperture */
  522. if (src->stolen) {
  523. use_ggtt = true;
  524. if (!(vma && vma->bound & GLOBAL_BIND))
  525. goto unwind;
  526. reloc_offset = i915_gem_obj_ggtt_offset(src);
  527. if (reloc_offset + num_pages * PAGE_SIZE > dev_priv->gtt.mappable_end)
  528. goto unwind;
  529. }
  530. /* Cannot access snooped pages through the aperture */
  531. if (use_ggtt && src->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv->dev))
  532. goto unwind;
  533. dst->page_count = num_pages;
  534. while (num_pages--) {
  535. unsigned long flags;
  536. void *d;
  537. d = kmalloc(PAGE_SIZE, GFP_ATOMIC);
  538. if (d == NULL)
  539. goto unwind;
  540. local_irq_save(flags);
  541. if (use_ggtt) {
  542. void __iomem *s;
  543. /* Simply ignore tiling or any overlapping fence.
  544. * It's part of the error state, and this hopefully
  545. * captures what the GPU read.
  546. */
  547. s = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  548. reloc_offset);
  549. memcpy_fromio(d, s, PAGE_SIZE);
  550. io_mapping_unmap_atomic(s);
  551. } else {
  552. struct page *page;
  553. void *s;
  554. page = i915_gem_object_get_page(src, i);
  555. drm_clflush_pages(&page, 1);
  556. s = kmap_atomic(page);
  557. memcpy(d, s, PAGE_SIZE);
  558. kunmap_atomic(s);
  559. drm_clflush_pages(&page, 1);
  560. }
  561. local_irq_restore(flags);
  562. dst->pages[i++] = d;
  563. reloc_offset += PAGE_SIZE;
  564. }
  565. return dst;
  566. unwind:
  567. while (i--)
  568. kfree(dst->pages[i]);
  569. kfree(dst);
  570. return NULL;
  571. }
  572. #define i915_error_ggtt_object_create(dev_priv, src) \
  573. i915_error_object_create((dev_priv), (src), &(dev_priv)->gtt.base)
  574. static void capture_bo(struct drm_i915_error_buffer *err,
  575. struct i915_vma *vma)
  576. {
  577. struct drm_i915_gem_object *obj = vma->obj;
  578. int i;
  579. err->size = obj->base.size;
  580. err->name = obj->base.name;
  581. for (i = 0; i < I915_NUM_RINGS; i++)
  582. err->rseqno[i] = i915_gem_request_get_seqno(obj->last_read_req[i]);
  583. err->wseqno = i915_gem_request_get_seqno(obj->last_write_req);
  584. err->gtt_offset = vma->node.start;
  585. err->read_domains = obj->base.read_domains;
  586. err->write_domain = obj->base.write_domain;
  587. err->fence_reg = obj->fence_reg;
  588. err->pinned = 0;
  589. if (i915_gem_obj_is_pinned(obj))
  590. err->pinned = 1;
  591. err->tiling = obj->tiling_mode;
  592. err->dirty = obj->dirty;
  593. err->purgeable = obj->madv != I915_MADV_WILLNEED;
  594. err->userptr = obj->userptr.mm != NULL;
  595. err->ring = obj->last_write_req ?
  596. i915_gem_request_get_ring(obj->last_write_req)->id : -1;
  597. err->cache_level = obj->cache_level;
  598. }
  599. static u32 capture_active_bo(struct drm_i915_error_buffer *err,
  600. int count, struct list_head *head)
  601. {
  602. struct i915_vma *vma;
  603. int i = 0;
  604. list_for_each_entry(vma, head, mm_list) {
  605. capture_bo(err++, vma);
  606. if (++i == count)
  607. break;
  608. }
  609. return i;
  610. }
  611. static u32 capture_pinned_bo(struct drm_i915_error_buffer *err,
  612. int count, struct list_head *head,
  613. struct i915_address_space *vm)
  614. {
  615. struct drm_i915_gem_object *obj;
  616. struct drm_i915_error_buffer * const first = err;
  617. struct drm_i915_error_buffer * const last = err + count;
  618. list_for_each_entry(obj, head, global_list) {
  619. struct i915_vma *vma;
  620. if (err == last)
  621. break;
  622. list_for_each_entry(vma, &obj->vma_list, vma_link)
  623. if (vma->vm == vm && vma->pin_count > 0)
  624. capture_bo(err++, vma);
  625. }
  626. return err - first;
  627. }
  628. /* Generate a semi-unique error code. The code is not meant to have meaning, The
  629. * code's only purpose is to try to prevent false duplicated bug reports by
  630. * grossly estimating a GPU error state.
  631. *
  632. * TODO Ideally, hashing the batchbuffer would be a very nice way to determine
  633. * the hang if we could strip the GTT offset information from it.
  634. *
  635. * It's only a small step better than a random number in its current form.
  636. */
  637. static uint32_t i915_error_generate_code(struct drm_i915_private *dev_priv,
  638. struct drm_i915_error_state *error,
  639. int *ring_id)
  640. {
  641. uint32_t error_code = 0;
  642. int i;
  643. /* IPEHR would be an ideal way to detect errors, as it's the gross
  644. * measure of "the command that hung." However, has some very common
  645. * synchronization commands which almost always appear in the case
  646. * strictly a client bug. Use instdone to differentiate those some.
  647. */
  648. for (i = 0; i < I915_NUM_RINGS; i++) {
  649. if (error->ring[i].hangcheck_action == HANGCHECK_HUNG) {
  650. if (ring_id)
  651. *ring_id = i;
  652. return error->ring[i].ipehr ^ error->ring[i].instdone;
  653. }
  654. }
  655. return error_code;
  656. }
  657. static void i915_gem_record_fences(struct drm_device *dev,
  658. struct drm_i915_error_state *error)
  659. {
  660. struct drm_i915_private *dev_priv = dev->dev_private;
  661. int i;
  662. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  663. for (i = 0; i < 8; i++)
  664. error->fence[i] = I915_READ(FENCE_REG_830_0 + (i * 4));
  665. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  666. for (i = 0; i < 8; i++)
  667. error->fence[i+8] = I915_READ(FENCE_REG_945_8 +
  668. (i * 4));
  669. } else if (IS_GEN5(dev) || IS_GEN4(dev))
  670. for (i = 0; i < 16; i++)
  671. error->fence[i] = I915_READ64(FENCE_REG_965_0 +
  672. (i * 8));
  673. else if (INTEL_INFO(dev)->gen >= 6)
  674. for (i = 0; i < dev_priv->num_fence_regs; i++)
  675. error->fence[i] = I915_READ64(FENCE_REG_SANDYBRIDGE_0 +
  676. (i * 8));
  677. }
  678. static void gen8_record_semaphore_state(struct drm_i915_private *dev_priv,
  679. struct drm_i915_error_state *error,
  680. struct intel_engine_cs *ring,
  681. struct drm_i915_error_ring *ering)
  682. {
  683. struct intel_engine_cs *to;
  684. int i;
  685. if (!i915_semaphore_is_enabled(dev_priv->dev))
  686. return;
  687. if (!error->semaphore_obj)
  688. error->semaphore_obj =
  689. i915_error_ggtt_object_create(dev_priv,
  690. dev_priv->semaphore_obj);
  691. for_each_ring(to, dev_priv, i) {
  692. int idx;
  693. u16 signal_offset;
  694. u32 *tmp;
  695. if (ring == to)
  696. continue;
  697. signal_offset = (GEN8_SIGNAL_OFFSET(ring, i) & (PAGE_SIZE - 1))
  698. / 4;
  699. tmp = error->semaphore_obj->pages[0];
  700. idx = intel_ring_sync_index(ring, to);
  701. ering->semaphore_mboxes[idx] = tmp[signal_offset];
  702. ering->semaphore_seqno[idx] = ring->semaphore.sync_seqno[idx];
  703. }
  704. }
  705. static void gen6_record_semaphore_state(struct drm_i915_private *dev_priv,
  706. struct intel_engine_cs *ring,
  707. struct drm_i915_error_ring *ering)
  708. {
  709. ering->semaphore_mboxes[0] = I915_READ(RING_SYNC_0(ring->mmio_base));
  710. ering->semaphore_mboxes[1] = I915_READ(RING_SYNC_1(ring->mmio_base));
  711. ering->semaphore_seqno[0] = ring->semaphore.sync_seqno[0];
  712. ering->semaphore_seqno[1] = ring->semaphore.sync_seqno[1];
  713. if (HAS_VEBOX(dev_priv->dev)) {
  714. ering->semaphore_mboxes[2] =
  715. I915_READ(RING_SYNC_2(ring->mmio_base));
  716. ering->semaphore_seqno[2] = ring->semaphore.sync_seqno[2];
  717. }
  718. }
  719. static void i915_record_ring_state(struct drm_device *dev,
  720. struct drm_i915_error_state *error,
  721. struct intel_engine_cs *ring,
  722. struct drm_i915_error_ring *ering)
  723. {
  724. struct drm_i915_private *dev_priv = dev->dev_private;
  725. if (INTEL_INFO(dev)->gen >= 6) {
  726. ering->rc_psmi = I915_READ(ring->mmio_base + 0x50);
  727. ering->fault_reg = I915_READ(RING_FAULT_REG(ring));
  728. if (INTEL_INFO(dev)->gen >= 8)
  729. gen8_record_semaphore_state(dev_priv, error, ring, ering);
  730. else
  731. gen6_record_semaphore_state(dev_priv, ring, ering);
  732. }
  733. if (INTEL_INFO(dev)->gen >= 4) {
  734. ering->faddr = I915_READ(RING_DMA_FADD(ring->mmio_base));
  735. ering->ipeir = I915_READ(RING_IPEIR(ring->mmio_base));
  736. ering->ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
  737. ering->instdone = I915_READ(RING_INSTDONE(ring->mmio_base));
  738. ering->instps = I915_READ(RING_INSTPS(ring->mmio_base));
  739. ering->bbaddr = I915_READ(RING_BBADDR(ring->mmio_base));
  740. if (INTEL_INFO(dev)->gen >= 8) {
  741. ering->faddr |= (u64) I915_READ(RING_DMA_FADD_UDW(ring->mmio_base)) << 32;
  742. ering->bbaddr |= (u64) I915_READ(RING_BBADDR_UDW(ring->mmio_base)) << 32;
  743. }
  744. ering->bbstate = I915_READ(RING_BBSTATE(ring->mmio_base));
  745. } else {
  746. ering->faddr = I915_READ(DMA_FADD_I8XX);
  747. ering->ipeir = I915_READ(IPEIR);
  748. ering->ipehr = I915_READ(IPEHR);
  749. ering->instdone = I915_READ(INSTDONE);
  750. }
  751. ering->waiting = waitqueue_active(&ring->irq_queue);
  752. ering->instpm = I915_READ(RING_INSTPM(ring->mmio_base));
  753. ering->seqno = ring->get_seqno(ring, false);
  754. ering->acthd = intel_ring_get_active_head(ring);
  755. ering->start = I915_READ_START(ring);
  756. ering->head = I915_READ_HEAD(ring);
  757. ering->tail = I915_READ_TAIL(ring);
  758. ering->ctl = I915_READ_CTL(ring);
  759. if (I915_NEED_GFX_HWS(dev)) {
  760. int mmio;
  761. if (IS_GEN7(dev)) {
  762. switch (ring->id) {
  763. default:
  764. case RCS:
  765. mmio = RENDER_HWS_PGA_GEN7;
  766. break;
  767. case BCS:
  768. mmio = BLT_HWS_PGA_GEN7;
  769. break;
  770. case VCS:
  771. mmio = BSD_HWS_PGA_GEN7;
  772. break;
  773. case VECS:
  774. mmio = VEBOX_HWS_PGA_GEN7;
  775. break;
  776. }
  777. } else if (IS_GEN6(ring->dev)) {
  778. mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
  779. } else {
  780. /* XXX: gen8 returns to sanity */
  781. mmio = RING_HWS_PGA(ring->mmio_base);
  782. }
  783. ering->hws = I915_READ(mmio);
  784. }
  785. ering->hangcheck_score = ring->hangcheck.score;
  786. ering->hangcheck_action = ring->hangcheck.action;
  787. if (USES_PPGTT(dev)) {
  788. int i;
  789. ering->vm_info.gfx_mode = I915_READ(RING_MODE_GEN7(ring));
  790. if (IS_GEN6(dev))
  791. ering->vm_info.pp_dir_base =
  792. I915_READ(RING_PP_DIR_BASE_READ(ring));
  793. else if (IS_GEN7(dev))
  794. ering->vm_info.pp_dir_base =
  795. I915_READ(RING_PP_DIR_BASE(ring));
  796. else if (INTEL_INFO(dev)->gen >= 8)
  797. for (i = 0; i < 4; i++) {
  798. ering->vm_info.pdp[i] =
  799. I915_READ(GEN8_RING_PDP_UDW(ring, i));
  800. ering->vm_info.pdp[i] <<= 32;
  801. ering->vm_info.pdp[i] |=
  802. I915_READ(GEN8_RING_PDP_LDW(ring, i));
  803. }
  804. }
  805. }
  806. static void i915_gem_record_active_context(struct intel_engine_cs *ring,
  807. struct drm_i915_error_state *error,
  808. struct drm_i915_error_ring *ering)
  809. {
  810. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  811. struct drm_i915_gem_object *obj;
  812. /* Currently render ring is the only HW context user */
  813. if (ring->id != RCS || !error->ccid)
  814. return;
  815. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  816. if (!i915_gem_obj_ggtt_bound(obj))
  817. continue;
  818. if ((error->ccid & PAGE_MASK) == i915_gem_obj_ggtt_offset(obj)) {
  819. ering->ctx = i915_error_ggtt_object_create(dev_priv, obj);
  820. break;
  821. }
  822. }
  823. }
  824. static void i915_gem_record_rings(struct drm_device *dev,
  825. struct drm_i915_error_state *error)
  826. {
  827. struct drm_i915_private *dev_priv = dev->dev_private;
  828. struct drm_i915_gem_request *request;
  829. int i, count;
  830. for (i = 0; i < I915_NUM_RINGS; i++) {
  831. struct intel_engine_cs *ring = &dev_priv->ring[i];
  832. struct intel_ringbuffer *rbuf;
  833. error->ring[i].pid = -1;
  834. if (ring->dev == NULL)
  835. continue;
  836. error->ring[i].valid = true;
  837. i915_record_ring_state(dev, error, ring, &error->ring[i]);
  838. request = i915_gem_find_active_request(ring);
  839. if (request) {
  840. struct i915_address_space *vm;
  841. vm = request->ctx && request->ctx->ppgtt ?
  842. &request->ctx->ppgtt->base :
  843. &dev_priv->gtt.base;
  844. /* We need to copy these to an anonymous buffer
  845. * as the simplest method to avoid being overwritten
  846. * by userspace.
  847. */
  848. error->ring[i].batchbuffer =
  849. i915_error_object_create(dev_priv,
  850. request->batch_obj,
  851. vm);
  852. if (HAS_BROKEN_CS_TLB(dev_priv->dev))
  853. error->ring[i].wa_batchbuffer =
  854. i915_error_ggtt_object_create(dev_priv,
  855. ring->scratch.obj);
  856. if (request->pid) {
  857. struct task_struct *task;
  858. rcu_read_lock();
  859. task = pid_task(request->pid, PIDTYPE_PID);
  860. if (task) {
  861. strcpy(error->ring[i].comm, task->comm);
  862. error->ring[i].pid = task->pid;
  863. }
  864. rcu_read_unlock();
  865. }
  866. }
  867. if (i915.enable_execlists) {
  868. /* TODO: This is only a small fix to keep basic error
  869. * capture working, but we need to add more information
  870. * for it to be useful (e.g. dump the context being
  871. * executed).
  872. */
  873. if (request)
  874. rbuf = request->ctx->engine[ring->id].ringbuf;
  875. else
  876. rbuf = ring->default_context->engine[ring->id].ringbuf;
  877. } else
  878. rbuf = ring->buffer;
  879. error->ring[i].cpu_ring_head = rbuf->head;
  880. error->ring[i].cpu_ring_tail = rbuf->tail;
  881. error->ring[i].ringbuffer =
  882. i915_error_ggtt_object_create(dev_priv, rbuf->obj);
  883. error->ring[i].hws_page =
  884. i915_error_ggtt_object_create(dev_priv, ring->status_page.obj);
  885. i915_gem_record_active_context(ring, error, &error->ring[i]);
  886. count = 0;
  887. list_for_each_entry(request, &ring->request_list, list)
  888. count++;
  889. error->ring[i].num_requests = count;
  890. error->ring[i].requests =
  891. kcalloc(count, sizeof(*error->ring[i].requests),
  892. GFP_ATOMIC);
  893. if (error->ring[i].requests == NULL) {
  894. error->ring[i].num_requests = 0;
  895. continue;
  896. }
  897. count = 0;
  898. list_for_each_entry(request, &ring->request_list, list) {
  899. struct drm_i915_error_request *erq;
  900. erq = &error->ring[i].requests[count++];
  901. erq->seqno = request->seqno;
  902. erq->jiffies = request->emitted_jiffies;
  903. erq->tail = request->postfix;
  904. }
  905. }
  906. }
  907. /* FIXME: Since pin count/bound list is global, we duplicate what we capture per
  908. * VM.
  909. */
  910. static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
  911. struct drm_i915_error_state *error,
  912. struct i915_address_space *vm,
  913. const int ndx)
  914. {
  915. struct drm_i915_error_buffer *active_bo = NULL, *pinned_bo = NULL;
  916. struct drm_i915_gem_object *obj;
  917. struct i915_vma *vma;
  918. int i;
  919. i = 0;
  920. list_for_each_entry(vma, &vm->active_list, mm_list)
  921. i++;
  922. error->active_bo_count[ndx] = i;
  923. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  924. list_for_each_entry(vma, &obj->vma_list, vma_link)
  925. if (vma->vm == vm && vma->pin_count > 0)
  926. i++;
  927. }
  928. error->pinned_bo_count[ndx] = i - error->active_bo_count[ndx];
  929. if (i) {
  930. active_bo = kcalloc(i, sizeof(*active_bo), GFP_ATOMIC);
  931. if (active_bo)
  932. pinned_bo = active_bo + error->active_bo_count[ndx];
  933. }
  934. if (active_bo)
  935. error->active_bo_count[ndx] =
  936. capture_active_bo(active_bo,
  937. error->active_bo_count[ndx],
  938. &vm->active_list);
  939. if (pinned_bo)
  940. error->pinned_bo_count[ndx] =
  941. capture_pinned_bo(pinned_bo,
  942. error->pinned_bo_count[ndx],
  943. &dev_priv->mm.bound_list, vm);
  944. error->active_bo[ndx] = active_bo;
  945. error->pinned_bo[ndx] = pinned_bo;
  946. }
  947. static void i915_gem_capture_buffers(struct drm_i915_private *dev_priv,
  948. struct drm_i915_error_state *error)
  949. {
  950. struct i915_address_space *vm;
  951. int cnt = 0, i = 0;
  952. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  953. cnt++;
  954. error->active_bo = kcalloc(cnt, sizeof(*error->active_bo), GFP_ATOMIC);
  955. error->pinned_bo = kcalloc(cnt, sizeof(*error->pinned_bo), GFP_ATOMIC);
  956. error->active_bo_count = kcalloc(cnt, sizeof(*error->active_bo_count),
  957. GFP_ATOMIC);
  958. error->pinned_bo_count = kcalloc(cnt, sizeof(*error->pinned_bo_count),
  959. GFP_ATOMIC);
  960. if (error->active_bo == NULL ||
  961. error->pinned_bo == NULL ||
  962. error->active_bo_count == NULL ||
  963. error->pinned_bo_count == NULL) {
  964. kfree(error->active_bo);
  965. kfree(error->active_bo_count);
  966. kfree(error->pinned_bo);
  967. kfree(error->pinned_bo_count);
  968. error->active_bo = NULL;
  969. error->active_bo_count = NULL;
  970. error->pinned_bo = NULL;
  971. error->pinned_bo_count = NULL;
  972. } else {
  973. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  974. i915_gem_capture_vm(dev_priv, error, vm, i++);
  975. error->vm_count = cnt;
  976. }
  977. }
  978. /* Capture all registers which don't fit into another category. */
  979. static void i915_capture_reg_state(struct drm_i915_private *dev_priv,
  980. struct drm_i915_error_state *error)
  981. {
  982. struct drm_device *dev = dev_priv->dev;
  983. int i;
  984. /* General organization
  985. * 1. Registers specific to a single generation
  986. * 2. Registers which belong to multiple generations
  987. * 3. Feature specific registers.
  988. * 4. Everything else
  989. * Please try to follow the order.
  990. */
  991. /* 1: Registers specific to a single generation */
  992. if (IS_VALLEYVIEW(dev)) {
  993. error->gtier[0] = I915_READ(GTIER);
  994. error->ier = I915_READ(VLV_IER);
  995. error->forcewake = I915_READ(FORCEWAKE_VLV);
  996. }
  997. if (IS_GEN7(dev))
  998. error->err_int = I915_READ(GEN7_ERR_INT);
  999. if (INTEL_INFO(dev)->gen >= 8) {
  1000. error->fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
  1001. error->fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
  1002. }
  1003. if (IS_GEN6(dev)) {
  1004. error->forcewake = I915_READ(FORCEWAKE);
  1005. error->gab_ctl = I915_READ(GAB_CTL);
  1006. error->gfx_mode = I915_READ(GFX_MODE);
  1007. }
  1008. /* 2: Registers which belong to multiple generations */
  1009. if (INTEL_INFO(dev)->gen >= 7)
  1010. error->forcewake = I915_READ(FORCEWAKE_MT);
  1011. if (INTEL_INFO(dev)->gen >= 6) {
  1012. error->derrmr = I915_READ(DERRMR);
  1013. error->error = I915_READ(ERROR_GEN6);
  1014. error->done_reg = I915_READ(DONE_REG);
  1015. }
  1016. /* 3: Feature specific registers */
  1017. if (IS_GEN6(dev) || IS_GEN7(dev)) {
  1018. error->gam_ecochk = I915_READ(GAM_ECOCHK);
  1019. error->gac_eco = I915_READ(GAC_ECO_BITS);
  1020. }
  1021. /* 4: Everything else */
  1022. if (HAS_HW_CONTEXTS(dev))
  1023. error->ccid = I915_READ(CCID);
  1024. if (INTEL_INFO(dev)->gen >= 8) {
  1025. error->ier = I915_READ(GEN8_DE_MISC_IER);
  1026. for (i = 0; i < 4; i++)
  1027. error->gtier[i] = I915_READ(GEN8_GT_IER(i));
  1028. } else if (HAS_PCH_SPLIT(dev)) {
  1029. error->ier = I915_READ(DEIER);
  1030. error->gtier[0] = I915_READ(GTIER);
  1031. } else if (IS_GEN2(dev)) {
  1032. error->ier = I915_READ16(IER);
  1033. } else if (!IS_VALLEYVIEW(dev)) {
  1034. error->ier = I915_READ(IER);
  1035. }
  1036. error->eir = I915_READ(EIR);
  1037. error->pgtbl_er = I915_READ(PGTBL_ER);
  1038. i915_get_extra_instdone(dev, error->extra_instdone);
  1039. }
  1040. static void i915_error_capture_msg(struct drm_device *dev,
  1041. struct drm_i915_error_state *error,
  1042. bool wedged,
  1043. const char *error_msg)
  1044. {
  1045. struct drm_i915_private *dev_priv = dev->dev_private;
  1046. u32 ecode;
  1047. int ring_id = -1, len;
  1048. ecode = i915_error_generate_code(dev_priv, error, &ring_id);
  1049. len = scnprintf(error->error_msg, sizeof(error->error_msg),
  1050. "GPU HANG: ecode %d:%d:0x%08x",
  1051. INTEL_INFO(dev)->gen, ring_id, ecode);
  1052. if (ring_id != -1 && error->ring[ring_id].pid != -1)
  1053. len += scnprintf(error->error_msg + len,
  1054. sizeof(error->error_msg) - len,
  1055. ", in %s [%d]",
  1056. error->ring[ring_id].comm,
  1057. error->ring[ring_id].pid);
  1058. scnprintf(error->error_msg + len, sizeof(error->error_msg) - len,
  1059. ", reason: %s, action: %s",
  1060. error_msg,
  1061. wedged ? "reset" : "continue");
  1062. }
  1063. static void i915_capture_gen_state(struct drm_i915_private *dev_priv,
  1064. struct drm_i915_error_state *error)
  1065. {
  1066. error->iommu = -1;
  1067. #ifdef CONFIG_INTEL_IOMMU
  1068. error->iommu = intel_iommu_gfx_mapped;
  1069. #endif
  1070. error->reset_count = i915_reset_count(&dev_priv->gpu_error);
  1071. error->suspend_count = dev_priv->suspend_count;
  1072. }
  1073. /**
  1074. * i915_capture_error_state - capture an error record for later analysis
  1075. * @dev: drm device
  1076. *
  1077. * Should be called when an error is detected (either a hang or an error
  1078. * interrupt) to capture error state from the time of the error. Fills
  1079. * out a structure which becomes available in debugfs for user level tools
  1080. * to pick up.
  1081. */
  1082. void i915_capture_error_state(struct drm_device *dev, bool wedged,
  1083. const char *error_msg)
  1084. {
  1085. static bool warned;
  1086. struct drm_i915_private *dev_priv = dev->dev_private;
  1087. struct drm_i915_error_state *error;
  1088. unsigned long flags;
  1089. /* Account for pipe specific data like PIPE*STAT */
  1090. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  1091. if (!error) {
  1092. DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
  1093. return;
  1094. }
  1095. kref_init(&error->ref);
  1096. i915_capture_gen_state(dev_priv, error);
  1097. i915_capture_reg_state(dev_priv, error);
  1098. i915_gem_capture_buffers(dev_priv, error);
  1099. i915_gem_record_fences(dev, error);
  1100. i915_gem_record_rings(dev, error);
  1101. do_gettimeofday(&error->time);
  1102. error->overlay = intel_overlay_capture_error_state(dev);
  1103. error->display = intel_display_capture_error_state(dev);
  1104. i915_error_capture_msg(dev, error, wedged, error_msg);
  1105. DRM_INFO("%s\n", error->error_msg);
  1106. spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
  1107. if (dev_priv->gpu_error.first_error == NULL) {
  1108. dev_priv->gpu_error.first_error = error;
  1109. error = NULL;
  1110. }
  1111. spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
  1112. if (error) {
  1113. i915_error_state_free(&error->ref);
  1114. return;
  1115. }
  1116. if (!warned) {
  1117. DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
  1118. DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
  1119. DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
  1120. DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
  1121. DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n", dev->primary->index);
  1122. warned = true;
  1123. }
  1124. }
  1125. void i915_error_state_get(struct drm_device *dev,
  1126. struct i915_error_state_file_priv *error_priv)
  1127. {
  1128. struct drm_i915_private *dev_priv = dev->dev_private;
  1129. spin_lock_irq(&dev_priv->gpu_error.lock);
  1130. error_priv->error = dev_priv->gpu_error.first_error;
  1131. if (error_priv->error)
  1132. kref_get(&error_priv->error->ref);
  1133. spin_unlock_irq(&dev_priv->gpu_error.lock);
  1134. }
  1135. void i915_error_state_put(struct i915_error_state_file_priv *error_priv)
  1136. {
  1137. if (error_priv->error)
  1138. kref_put(&error_priv->error->ref, i915_error_state_free);
  1139. }
  1140. void i915_destroy_error_state(struct drm_device *dev)
  1141. {
  1142. struct drm_i915_private *dev_priv = dev->dev_private;
  1143. struct drm_i915_error_state *error;
  1144. spin_lock_irq(&dev_priv->gpu_error.lock);
  1145. error = dev_priv->gpu_error.first_error;
  1146. dev_priv->gpu_error.first_error = NULL;
  1147. spin_unlock_irq(&dev_priv->gpu_error.lock);
  1148. if (error)
  1149. kref_put(&error->ref, i915_error_state_free);
  1150. }
  1151. const char *i915_cache_level_str(struct drm_i915_private *i915, int type)
  1152. {
  1153. switch (type) {
  1154. case I915_CACHE_NONE: return " uncached";
  1155. case I915_CACHE_LLC: return HAS_LLC(i915) ? " LLC" : " snooped";
  1156. case I915_CACHE_L3_LLC: return " L3+LLC";
  1157. case I915_CACHE_WT: return " WT";
  1158. default: return "";
  1159. }
  1160. }
  1161. /* NB: please notice the memset */
  1162. void i915_get_extra_instdone(struct drm_device *dev, uint32_t *instdone)
  1163. {
  1164. struct drm_i915_private *dev_priv = dev->dev_private;
  1165. memset(instdone, 0, sizeof(*instdone) * I915_NUM_INSTDONE_REG);
  1166. if (IS_GEN2(dev) || IS_GEN3(dev))
  1167. instdone[0] = I915_READ(INSTDONE);
  1168. else if (IS_GEN4(dev) || IS_GEN5(dev) || IS_GEN6(dev)) {
  1169. instdone[0] = I915_READ(INSTDONE_I965);
  1170. instdone[1] = I915_READ(INSTDONE1);
  1171. } else if (INTEL_INFO(dev)->gen >= 7) {
  1172. instdone[0] = I915_READ(GEN7_INSTDONE_1);
  1173. instdone[1] = I915_READ(GEN7_SC_INSTDONE);
  1174. instdone[2] = I915_READ(GEN7_SAMPLER_INSTDONE);
  1175. instdone[3] = I915_READ(GEN7_ROW_INSTDONE);
  1176. }
  1177. }