i915_gpu_error.c 40 KB

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