drm/i915/guc: Drop comment on fwif autogeneration
[linux-block.git] / drivers / gpu / drm / i915 / i915_gpu_error.c
CommitLineData
84734a04
MK
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
30#include <generated/utsrelease.h>
9f267eb8 31#include <linux/stop_machine.h>
0a97015d 32#include <linux/zlib.h>
84734a04
MK
33#include "i915_drv.h"
34
6361f4ba 35static const char *engine_str(int engine)
84734a04 36{
6361f4ba 37 switch (engine) {
84734a04
MK
38 case RCS: return "render";
39 case VCS: return "bsd";
40 case BCS: return "blt";
41 case VECS: return "vebox";
845f74a7 42 case VCS2: return "bsd2";
84734a04
MK
43 default: return "";
44 }
45}
46
84734a04
MK
47static const char *tiling_flag(int tiling)
48{
49 switch (tiling) {
50 default:
51 case I915_TILING_NONE: return "";
52 case I915_TILING_X: return " X";
53 case I915_TILING_Y: return " Y";
54 }
55}
56
57static const char *dirty_flag(int dirty)
58{
59 return dirty ? " dirty" : "";
60}
61
62static const char *purgeable_flag(int purgeable)
63{
64 return purgeable ? " purgeable" : "";
65}
66
67static bool __i915_error_ok(struct drm_i915_error_state_buf *e)
68{
69
70 if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) {
71 e->err = -ENOSPC;
72 return false;
73 }
74
75 if (e->bytes == e->size - 1 || e->err)
76 return false;
77
78 return true;
79}
80
81static bool __i915_error_seek(struct drm_i915_error_state_buf *e,
82 unsigned len)
83{
84 if (e->pos + len <= e->start) {
85 e->pos += len;
86 return false;
87 }
88
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
95 return true;
96}
97
98static void __i915_error_advance(struct drm_i915_error_state_buf *e,
99 unsigned len)
100{
101 /* If this is first printf in this window, adjust it so that
102 * start position matches start of the buffer
103 */
104
105 if (e->pos < e->start) {
106 const size_t off = e->start - e->pos;
107
108 /* Should not happen but be paranoid */
109 if (off > len || e->bytes) {
110 e->err = -EIO;
111 return;
112 }
113
114 memmove(e->buf, e->buf + off, len - off);
115 e->bytes = len - off;
116 e->pos = e->start;
117 return;
118 }
119
120 e->bytes += len;
121 e->pos += len;
122}
123
124static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
125 const char *f, va_list args)
126{
127 unsigned len;
128
129 if (!__i915_error_ok(e))
130 return;
131
132 /* Seek the first printf which is hits start position */
133 if (e->pos < e->start) {
e29bb4eb
CW
134 va_list tmp;
135
136 va_copy(tmp, args);
1d2cb9a5
MK
137 len = vsnprintf(NULL, 0, f, tmp);
138 va_end(tmp);
139
140 if (!__i915_error_seek(e, len))
84734a04
MK
141 return;
142 }
143
144 len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args);
145 if (len >= e->size - e->bytes)
146 len = e->size - e->bytes - 1;
147
148 __i915_error_advance(e, len);
149}
150
151static void i915_error_puts(struct drm_i915_error_state_buf *e,
152 const char *str)
153{
154 unsigned len;
155
156 if (!__i915_error_ok(e))
157 return;
158
159 len = strlen(str);
160
161 /* Seek the first printf which is hits start position */
162 if (e->pos < e->start) {
163 if (!__i915_error_seek(e, len))
164 return;
165 }
166
167 if (len >= e->size - e->bytes)
168 len = e->size - e->bytes - 1;
169 memcpy(e->buf + e->bytes, str, len);
170
171 __i915_error_advance(e, len);
172}
173
174#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
175#define err_puts(e, s) i915_error_puts(e, s)
176
0a97015d
CW
177#ifdef CONFIG_DRM_I915_COMPRESS_ERROR
178
d637c178
CW
179struct compress {
180 struct z_stream_s zstream;
181 void *tmp;
182};
183
184static bool compress_init(struct compress *c)
0a97015d 185{
d637c178 186 struct z_stream_s *zstream = memset(&c->zstream, 0, sizeof(c->zstream));
0a97015d
CW
187
188 zstream->workspace =
189 kmalloc(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
190 GFP_ATOMIC | __GFP_NOWARN);
191 if (!zstream->workspace)
192 return false;
193
194 if (zlib_deflateInit(zstream, Z_DEFAULT_COMPRESSION) != Z_OK) {
195 kfree(zstream->workspace);
196 return false;
197 }
198
d637c178
CW
199 c->tmp = NULL;
200 if (i915_memcpy_from_wc(NULL, 0, 0))
201 c->tmp = (void *)__get_free_page(GFP_ATOMIC | __GFP_NOWARN);
202
0a97015d
CW
203 return true;
204}
205
d637c178 206static int compress_page(struct compress *c,
0a97015d
CW
207 void *src,
208 struct drm_i915_error_object *dst)
209{
d637c178
CW
210 struct z_stream_s *zstream = &c->zstream;
211
0a97015d 212 zstream->next_in = src;
d637c178
CW
213 if (c->tmp && i915_memcpy_from_wc(c->tmp, src, PAGE_SIZE))
214 zstream->next_in = c->tmp;
0a97015d
CW
215 zstream->avail_in = PAGE_SIZE;
216
217 do {
218 if (zstream->avail_out == 0) {
219 unsigned long page;
220
221 page = __get_free_page(GFP_ATOMIC | __GFP_NOWARN);
222 if (!page)
223 return -ENOMEM;
224
225 dst->pages[dst->page_count++] = (void *)page;
226
227 zstream->next_out = (void *)page;
228 zstream->avail_out = PAGE_SIZE;
229 }
230
231 if (zlib_deflate(zstream, Z_SYNC_FLUSH) != Z_OK)
232 return -EIO;
233 } while (zstream->avail_in);
234
235 /* Fallback to uncompressed if we increase size? */
236 if (0 && zstream->total_out > zstream->total_in)
237 return -E2BIG;
238
239 return 0;
240}
241
d637c178 242static void compress_fini(struct compress *c,
0a97015d
CW
243 struct drm_i915_error_object *dst)
244{
d637c178
CW
245 struct z_stream_s *zstream = &c->zstream;
246
0a97015d
CW
247 if (dst) {
248 zlib_deflate(zstream, Z_FINISH);
249 dst->unused = zstream->avail_out;
250 }
251
252 zlib_deflateEnd(zstream);
253 kfree(zstream->workspace);
d637c178
CW
254
255 if (c->tmp)
256 free_page((unsigned long)c->tmp);
0a97015d
CW
257}
258
259static void err_compression_marker(struct drm_i915_error_state_buf *m)
260{
261 err_puts(m, ":");
262}
263
264#else
265
d637c178
CW
266struct compress {
267};
268
269static bool compress_init(struct compress *c)
0a97015d
CW
270{
271 return true;
272}
273
d637c178 274static int compress_page(struct compress *c,
0a97015d
CW
275 void *src,
276 struct drm_i915_error_object *dst)
277{
278 unsigned long page;
d637c178 279 void *ptr;
0a97015d
CW
280
281 page = __get_free_page(GFP_ATOMIC | __GFP_NOWARN);
282 if (!page)
283 return -ENOMEM;
284
d637c178
CW
285 ptr = (void *)page;
286 if (!i915_memcpy_from_wc(ptr, src, PAGE_SIZE))
287 memcpy(ptr, src, PAGE_SIZE);
288 dst->pages[dst->page_count++] = ptr;
0a97015d
CW
289
290 return 0;
291}
292
d637c178 293static void compress_fini(struct compress *c,
0a97015d
CW
294 struct drm_i915_error_object *dst)
295{
296}
297
298static void err_compression_marker(struct drm_i915_error_state_buf *m)
299{
300 err_puts(m, "~");
301}
302
303#endif
304
84734a04
MK
305static void print_error_buffers(struct drm_i915_error_state_buf *m,
306 const char *name,
307 struct drm_i915_error_buffer *err,
308 int count)
309{
b4716185
CW
310 int i;
311
c0ce4663 312 err_printf(m, "%s [%d]:\n", name, count);
84734a04
MK
313
314 while (count--) {
e1f12325
MT
315 err_printf(m, " %08x_%08x %8u %02x %02x [ ",
316 upper_32_bits(err->gtt_offset),
317 lower_32_bits(err->gtt_offset),
84734a04
MK
318 err->size,
319 err->read_domains,
b4716185 320 err->write_domain);
666796da 321 for (i = 0; i < I915_NUM_ENGINES; i++)
b4716185
CW
322 err_printf(m, "%02x ", err->rseqno[i]);
323
324 err_printf(m, "] %02x", err->wseqno);
84734a04
MK
325 err_puts(m, tiling_flag(err->tiling));
326 err_puts(m, dirty_flag(err->dirty));
327 err_puts(m, purgeable_flag(err->purgeable));
5cc9ed4b 328 err_puts(m, err->userptr ? " userptr" : "");
6361f4ba
CW
329 err_puts(m, err->engine != -1 ? " " : "");
330 err_puts(m, engine_str(err->engine));
0a4cd7c8 331 err_puts(m, i915_cache_level_str(m->i915, err->cache_level));
84734a04
MK
332
333 if (err->name)
334 err_printf(m, " (name: %d)", err->name);
335 if (err->fence_reg != I915_FENCE_REG_NONE)
336 err_printf(m, " (fence: %d)", err->fence_reg);
337
338 err_puts(m, "\n");
339 err++;
340 }
341}
342
d636951e
BW
343static void error_print_instdone(struct drm_i915_error_state_buf *m,
344 struct drm_i915_error_engine *ee)
345{
f9e61372
BW
346 int slice;
347 int subslice;
348
d636951e
BW
349 err_printf(m, " INSTDONE: 0x%08x\n",
350 ee->instdone.instdone);
351
352 if (ee->engine_id != RCS || INTEL_GEN(m->i915) <= 3)
353 return;
354
355 err_printf(m, " SC_INSTDONE: 0x%08x\n",
356 ee->instdone.slice_common);
357
358 if (INTEL_GEN(m->i915) <= 6)
359 return;
360
f9e61372
BW
361 for_each_instdone_slice_subslice(m->i915, slice, subslice)
362 err_printf(m, " SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
363 slice, subslice,
364 ee->instdone.sampler[slice][subslice]);
365
366 for_each_instdone_slice_subslice(m->i915, slice, subslice)
367 err_printf(m, " ROW_INSTDONE[%d][%d]: 0x%08x\n",
368 slice, subslice,
369 ee->instdone.row[slice][subslice]);
d636951e
BW
370}
371
35ca039e
CW
372static void error_print_request(struct drm_i915_error_state_buf *m,
373 const char *prefix,
374 struct drm_i915_error_request *erq)
375{
376 if (!erq->seqno)
377 return;
378
84102171
MK
379 err_printf(m, "%s pid %d, ban score %d, seqno %8x:%08x, emitted %dms ago, head %08x, tail %08x\n",
380 prefix, erq->pid, erq->ban_score,
35ca039e
CW
381 erq->context, erq->seqno,
382 jiffies_to_msecs(jiffies - erq->jiffies),
383 erq->head, erq->tail);
384}
385
6361f4ba
CW
386static void error_print_engine(struct drm_i915_error_state_buf *m,
387 struct drm_i915_error_engine *ee)
84734a04 388{
6361f4ba
CW
389 err_printf(m, "%s command stream:\n", engine_str(ee->engine_id));
390 err_printf(m, " START: 0x%08x\n", ee->start);
06392e3b 391 err_printf(m, " HEAD: 0x%08x [0x%08x]\n", ee->head, ee->rq_head);
cdb324bd
CW
392 err_printf(m, " TAIL: 0x%08x [0x%08x, 0x%08x]\n",
393 ee->tail, ee->rq_post, ee->rq_tail);
6361f4ba 394 err_printf(m, " CTL: 0x%08x\n", ee->ctl);
21a2c58a 395 err_printf(m, " MODE: 0x%08x\n", ee->mode);
6361f4ba
CW
396 err_printf(m, " HWS: 0x%08x\n", ee->hws);
397 err_printf(m, " ACTHD: 0x%08x %08x\n",
398 (u32)(ee->acthd>>32), (u32)ee->acthd);
399 err_printf(m, " IPEIR: 0x%08x\n", ee->ipeir);
400 err_printf(m, " IPEHR: 0x%08x\n", ee->ipehr);
d636951e
BW
401
402 error_print_instdone(m, ee);
403
03382dfb
CW
404 if (ee->batchbuffer) {
405 u64 start = ee->batchbuffer->gtt_offset;
406 u64 end = start + ee->batchbuffer->gtt_size;
407
408 err_printf(m, " batch: [0x%08x_%08x, 0x%08x_%08x]\n",
409 upper_32_bits(start), lower_32_bits(start),
410 upper_32_bits(end), lower_32_bits(end));
411 }
6361f4ba 412 if (INTEL_GEN(m->i915) >= 4) {
03382dfb 413 err_printf(m, " BBADDR: 0x%08x_%08x\n",
6361f4ba
CW
414 (u32)(ee->bbaddr>>32), (u32)ee->bbaddr);
415 err_printf(m, " BB_STATE: 0x%08x\n", ee->bbstate);
416 err_printf(m, " INSTPS: 0x%08x\n", ee->instps);
3dda20a9 417 }
6361f4ba
CW
418 err_printf(m, " INSTPM: 0x%08x\n", ee->instpm);
419 err_printf(m, " FADDR: 0x%08x %08x\n", upper_32_bits(ee->faddr),
420 lower_32_bits(ee->faddr));
421 if (INTEL_GEN(m->i915) >= 6) {
422 err_printf(m, " RC PSMI: 0x%08x\n", ee->rc_psmi);
423 err_printf(m, " FAULT_REG: 0x%08x\n", ee->fault_reg);
85e17f59
CW
424 err_printf(m, " SYNC_0: 0x%08x\n",
425 ee->semaphore_mboxes[0]);
426 err_printf(m, " SYNC_1: 0x%08x\n",
427 ee->semaphore_mboxes[1]);
428 if (HAS_VEBOX(m->i915))
429 err_printf(m, " SYNC_2: 0x%08x\n",
430 ee->semaphore_mboxes[2]);
84734a04 431 }
6361f4ba
CW
432 if (USES_PPGTT(m->i915)) {
433 err_printf(m, " GFX_MODE: 0x%08x\n", ee->vm_info.gfx_mode);
6c7a01ec 434
6361f4ba 435 if (INTEL_GEN(m->i915) >= 8) {
6c7a01ec
BW
436 int i;
437 for (i = 0; i < 4; i++)
438 err_printf(m, " PDP%d: 0x%016llx\n",
6361f4ba 439 i, ee->vm_info.pdp[i]);
6c7a01ec
BW
440 } else {
441 err_printf(m, " PP_DIR_BASE: 0x%08x\n",
6361f4ba 442 ee->vm_info.pp_dir_base);
6c7a01ec
BW
443 }
444 }
6361f4ba
CW
445 err_printf(m, " seqno: 0x%08x\n", ee->seqno);
446 err_printf(m, " last_seqno: 0x%08x\n", ee->last_seqno);
447 err_printf(m, " waiting: %s\n", yesno(ee->waiting));
448 err_printf(m, " ring->head: 0x%08x\n", ee->cpu_ring_head);
449 err_printf(m, " ring->tail: 0x%08x\n", ee->cpu_ring_tail);
3fe3b030
MK
450 err_printf(m, " hangcheck stall: %s\n", yesno(ee->hangcheck_stalled));
451 err_printf(m, " hangcheck action: %s\n",
452 hangcheck_action_to_str(ee->hangcheck_action));
453 err_printf(m, " hangcheck action timestamp: %lu, %u ms ago\n",
454 ee->hangcheck_timestamp,
455 jiffies_to_msecs(jiffies - ee->hangcheck_timestamp));
456
35ca039e
CW
457 error_print_request(m, " ELSP[0]: ", &ee->execlist[0]);
458 error_print_request(m, " ELSP[1]: ", &ee->execlist[1]);
84734a04
MK
459}
460
461void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
462{
463 va_list args;
464
465 va_start(args, f);
466 i915_error_vprintf(e, f, args);
467 va_end(args);
468}
469
0a97015d
CW
470static int
471ascii85_encode_len(int len)
472{
473 return DIV_ROUND_UP(len, 4);
474}
475
476static bool
477ascii85_encode(u32 in, char *out)
478{
479 int i;
480
481 if (in == 0)
482 return false;
483
484 out[5] = '\0';
485 for (i = 5; i--; ) {
486 out[i] = '!' + in % 85;
487 in /= 85;
488 }
489
490 return true;
491}
492
ab0e7ff9 493static void print_error_obj(struct drm_i915_error_state_buf *m,
fc4c79c3
CW
494 struct intel_engine_cs *engine,
495 const char *name,
ab0e7ff9
CW
496 struct drm_i915_error_object *obj)
497{
0a97015d
CW
498 char out[6];
499 int page;
ab0e7ff9 500
fc4c79c3
CW
501 if (!obj)
502 return;
503
504 if (name) {
505 err_printf(m, "%s --- %s = 0x%08x %08x\n",
506 engine ? engine->name : "global", name,
507 upper_32_bits(obj->gtt_offset),
508 lower_32_bits(obj->gtt_offset));
509 }
510
0a97015d
CW
511 err_compression_marker(m);
512 for (page = 0; page < obj->page_count; page++) {
513 int i, len;
514
515 len = PAGE_SIZE;
516 if (page == obj->page_count - 1)
517 len -= obj->unused;
518 len = ascii85_encode_len(len);
519
520 for (i = 0; i < len; i++) {
521 if (ascii85_encode(obj->pages[page][i], out))
522 err_puts(m, out);
523 else
524 err_puts(m, "z");
ab0e7ff9
CW
525 }
526 }
0a97015d 527 err_puts(m, "\n");
ab0e7ff9
CW
528}
529
2bd160a1
CW
530static void err_print_capabilities(struct drm_i915_error_state_buf *m,
531 const struct intel_device_info *info)
532{
533#define PRINT_FLAG(x) err_printf(m, #x ": %s\n", yesno(info->x))
604db650 534 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG);
2bd160a1 535#undef PRINT_FLAG
2bd160a1
CW
536}
537
84734a04
MK
538int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
539 const struct i915_error_state_file_priv *error_priv)
540{
12ff05e7 541 struct drm_i915_private *dev_priv = error_priv->i915;
52a05c30 542 struct pci_dev *pdev = dev_priv->drm.pdev;
84734a04 543 struct drm_i915_error_state *error = error_priv->error;
0ca36d78 544 struct drm_i915_error_object *obj;
fc4c79c3 545 int i, j;
84734a04
MK
546
547 if (!error) {
548 err_printf(m, "no error state collected\n");
549 goto out;
550 }
551
cb383002 552 err_printf(m, "%s\n", error->error_msg);
84734a04 553 err_printf(m, "Kernel: " UTS_RELEASE "\n");
de867c20
CW
554 err_printf(m, "Time: %ld s %ld us\n",
555 error->time.tv_sec, error->time.tv_usec);
556 err_printf(m, "Boottime: %ld s %ld us\n",
557 error->boottime.tv_sec, error->boottime.tv_usec);
558 err_printf(m, "Uptime: %ld s %ld us\n",
559 error->uptime.tv_sec, error->uptime.tv_usec);
2bd160a1 560 err_print_capabilities(m, &error->device_info);
3fe3b030 561
6361f4ba 562 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
3fe3b030 563 if (error->engine[i].hangcheck_stalled &&
6361f4ba 564 error->engine[i].pid != -1) {
b083a087 565 err_printf(m, "Active process (on ring %s): %s [%d], context bans %d\n",
6361f4ba
CW
566 engine_str(i),
567 error->engine[i].comm,
b083a087
MK
568 error->engine[i].pid,
569 error->engine[i].context_bans);
ab0e7ff9
CW
570 }
571 }
48b031e3 572 err_printf(m, "Reset count: %u\n", error->reset_count);
62d5d69b 573 err_printf(m, "Suspend count: %u\n", error->suspend_count);
52a05c30
DW
574 err_printf(m, "PCI ID: 0x%04x\n", pdev->device);
575 err_printf(m, "PCI Revision: 0x%02x\n", pdev->revision);
06e6ff8f 576 err_printf(m, "PCI Subsystem: %04x:%04x\n",
52a05c30
DW
577 pdev->subsystem_vendor,
578 pdev->subsystem_device);
eb5be9d0 579 err_printf(m, "IOMMU enabled?: %d\n", error->iommu);
0ac7655c 580
56b857a5 581 if (HAS_CSR(dev_priv)) {
0ac7655c
MK
582 struct intel_csr *csr = &dev_priv->csr;
583
584 err_printf(m, "DMC loaded: %s\n",
585 yesno(csr->dmc_payload != NULL));
586 err_printf(m, "DMC fw version: %d.%d\n",
587 CSR_VERSION_MAJOR(csr->version),
588 CSR_VERSION_MINOR(csr->version));
589 }
590
84734a04
MK
591 err_printf(m, "EIR: 0x%08x\n", error->eir);
592 err_printf(m, "IER: 0x%08x\n", error->ier);
5f56d5f9 593 if (INTEL_GEN(dev_priv) >= 8) {
885ea5a8
RV
594 for (i = 0; i < 4; i++)
595 err_printf(m, "GTIER gt %d: 0x%08x\n", i,
596 error->gtier[i]);
6e266956 597 } else if (HAS_PCH_SPLIT(dev_priv) || IS_VALLEYVIEW(dev_priv))
885ea5a8 598 err_printf(m, "GTIER: 0x%08x\n", error->gtier[0]);
84734a04
MK
599 err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
600 err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
601 err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
602 err_printf(m, "CCID: 0x%08x\n", error->ccid);
094f9a54 603 err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
84734a04
MK
604
605 for (i = 0; i < dev_priv->num_fence_regs; i++)
606 err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
607
5f56d5f9 608 if (INTEL_GEN(dev_priv) >= 6) {
84734a04 609 err_printf(m, "ERROR: 0x%08x\n", error->error);
6c826f34 610
5f56d5f9 611 if (INTEL_GEN(dev_priv) >= 8)
6c826f34
MK
612 err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n",
613 error->fault_data1, error->fault_data0);
614
84734a04
MK
615 err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
616 }
617
5db94019 618 if (IS_GEN7(dev_priv))
84734a04
MK
619 err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
620
6361f4ba
CW
621 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
622 if (error->engine[i].engine_id != -1)
623 error_print_engine(m, &error->engine[i]);
624 }
84734a04 625
c0ce4663
CW
626 for (i = 0; i < ARRAY_SIZE(error->active_vm); i++) {
627 char buf[128];
628 int len, first = 1;
3a448734 629
c0ce4663
CW
630 if (!error->active_vm[i])
631 break;
632
633 len = scnprintf(buf, sizeof(buf), "Active (");
634 for (j = 0; j < ARRAY_SIZE(error->engine); j++) {
635 if (error->engine[j].vm != error->active_vm[i])
636 continue;
637
638 len += scnprintf(buf + len, sizeof(buf), "%s%s",
639 first ? "" : ", ",
3b3f1650 640 dev_priv->engine[j]->name);
c0ce4663
CW
641 first = 0;
642 }
643 scnprintf(buf + len, sizeof(buf), ")");
644 print_error_buffers(m, buf,
3a448734
CW
645 error->active_bo[i],
646 error->active_bo_count[i]);
3a448734 647 }
84734a04 648
c0ce4663
CW
649 print_error_buffers(m, "Pinned (global)",
650 error->pinned_bo,
651 error->pinned_bo_count);
652
6361f4ba
CW
653 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
654 struct drm_i915_error_engine *ee = &error->engine[i];
655
656 obj = ee->batchbuffer;
ab0e7ff9 657 if (obj) {
3b3f1650 658 err_puts(m, dev_priv->engine[i]->name);
6361f4ba 659 if (ee->pid != -1)
b083a087 660 err_printf(m, " (submitted by %s [%d], bans %d)",
6361f4ba 661 ee->comm,
b083a087
MK
662 ee->pid,
663 ee->context_bans);
e1f12325
MT
664 err_printf(m, " --- gtt_offset = 0x%08x %08x\n",
665 upper_32_bits(obj->gtt_offset),
666 lower_32_bits(obj->gtt_offset));
3b3f1650 667 print_error_obj(m, dev_priv->engine[i], NULL, obj);
84734a04
MK
668 }
669
6361f4ba 670 if (ee->num_requests) {
84734a04 671 err_printf(m, "%s --- %d requests\n",
3b3f1650 672 dev_priv->engine[i]->name,
6361f4ba 673 ee->num_requests);
35ca039e
CW
674 for (j = 0; j < ee->num_requests; j++)
675 error_print_request(m, " ", &ee->requests[j]);
84734a04
MK
676 }
677
19eb9189
CW
678 if (IS_ERR(ee->waiters)) {
679 err_printf(m, "%s --- ? waiters [unable to acquire spinlock]\n",
3b3f1650 680 dev_priv->engine[i]->name);
19eb9189 681 } else if (ee->num_waiters) {
688e6c72 682 err_printf(m, "%s --- %d waiters\n",
3b3f1650 683 dev_priv->engine[i]->name,
6361f4ba
CW
684 ee->num_waiters);
685 for (j = 0; j < ee->num_waiters; j++) {
688e6c72 686 err_printf(m, " seqno 0x%08x for %s [%d]\n",
6361f4ba
CW
687 ee->waiters[j].seqno,
688 ee->waiters[j].comm,
689 ee->waiters[j].pid);
688e6c72
CW
690 }
691 }
692
3b3f1650 693 print_error_obj(m, dev_priv->engine[i],
fc4c79c3 694 "ringbuffer", ee->ringbuffer);
84734a04 695
3b3f1650 696 print_error_obj(m, dev_priv->engine[i],
fc4c79c3 697 "HW Status", ee->hws_page);
3a5a0393 698
3b3f1650 699 print_error_obj(m, dev_priv->engine[i],
fc4c79c3 700 "HW context", ee->ctx);
f3ce3821 701
3b3f1650 702 print_error_obj(m, dev_priv->engine[i],
fc4c79c3 703 "WA context", ee->wa_ctx);
f85db059 704
3b3f1650 705 print_error_obj(m, dev_priv->engine[i],
fc4c79c3 706 "WA batchbuffer", ee->wa_batchbuffer);
84734a04
MK
707 }
708
fc4c79c3 709 print_error_obj(m, NULL, "Semaphores", error->semaphore);
0ca36d78 710
27b85bea
AG
711 print_error_obj(m, NULL, "GuC log buffer", error->guc_log);
712
84734a04
MK
713 if (error->overlay)
714 intel_overlay_print_error_state(m, error->overlay);
715
716 if (error->display)
5f56d5f9 717 intel_display_print_error_state(m, dev_priv, error->display);
84734a04
MK
718
719out:
720 if (m->bytes == 0 && m->err)
721 return m->err;
722
723 return 0;
724}
725
726int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
0a4cd7c8 727 struct drm_i915_private *i915,
84734a04
MK
728 size_t count, loff_t pos)
729{
730 memset(ebuf, 0, sizeof(*ebuf));
0a4cd7c8 731 ebuf->i915 = i915;
84734a04
MK
732
733 /* We need to have enough room to store any i915_error_state printf
734 * so that we can move it to start position.
735 */
736 ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
737 ebuf->buf = kmalloc(ebuf->size,
738 GFP_TEMPORARY | __GFP_NORETRY | __GFP_NOWARN);
739
740 if (ebuf->buf == NULL) {
741 ebuf->size = PAGE_SIZE;
742 ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
743 }
744
745 if (ebuf->buf == NULL) {
746 ebuf->size = 128;
747 ebuf->buf = kmalloc(ebuf->size, GFP_TEMPORARY);
748 }
749
750 if (ebuf->buf == NULL)
751 return -ENOMEM;
752
753 ebuf->start = pos;
754
755 return 0;
756}
757
758static void i915_error_object_free(struct drm_i915_error_object *obj)
759{
760 int page;
761
762 if (obj == NULL)
763 return;
764
765 for (page = 0; page < obj->page_count; page++)
95374d75 766 free_page((unsigned long)obj->pages[page]);
84734a04
MK
767
768 kfree(obj);
769}
770
771static void i915_error_state_free(struct kref *error_ref)
772{
773 struct drm_i915_error_state *error = container_of(error_ref,
774 typeof(*error), ref);
775 int i;
776
6361f4ba
CW
777 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
778 struct drm_i915_error_engine *ee = &error->engine[i];
779
780 i915_error_object_free(ee->batchbuffer);
781 i915_error_object_free(ee->wa_batchbuffer);
782 i915_error_object_free(ee->ringbuffer);
783 i915_error_object_free(ee->hws_page);
784 i915_error_object_free(ee->ctx);
785 i915_error_object_free(ee->wa_ctx);
786
787 kfree(ee->requests);
19eb9189
CW
788 if (!IS_ERR_OR_NULL(ee->waiters))
789 kfree(ee->waiters);
84734a04
MK
790 }
791
51d545d0 792 i915_error_object_free(error->semaphore);
27b85bea 793 i915_error_object_free(error->guc_log);
0b37a9a9 794
c0ce4663 795 for (i = 0; i < ARRAY_SIZE(error->active_bo); i++)
0b37a9a9 796 kfree(error->active_bo[i]);
0b37a9a9 797 kfree(error->pinned_bo);
c0ce4663 798
84734a04
MK
799 kfree(error->overlay);
800 kfree(error->display);
801 kfree(error);
802}
803
804static struct drm_i915_error_object *
95374d75 805i915_error_object_create(struct drm_i915_private *i915,
058d88c4 806 struct i915_vma *vma)
84734a04 807{
95374d75
CW
808 struct i915_ggtt *ggtt = &i915->ggtt;
809 const u64 slot = ggtt->error_capture.start;
84734a04 810 struct drm_i915_error_object *dst;
d637c178 811 struct compress compress;
95374d75
CW
812 unsigned long num_pages;
813 struct sgt_iter iter;
814 dma_addr_t dma;
84734a04 815
058d88c4
CW
816 if (!vma)
817 return NULL;
818
95374d75 819 num_pages = min_t(u64, vma->size, vma->obj->base.size) >> PAGE_SHIFT;
0a97015d 820 num_pages = DIV_ROUND_UP(10 * num_pages, 8); /* worstcase zlib growth */
95374d75
CW
821 dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *),
822 GFP_ATOMIC | __GFP_NOWARN);
058d88c4 823 if (!dst)
84734a04
MK
824 return NULL;
825
03382dfb
CW
826 dst->gtt_offset = vma->node.start;
827 dst->gtt_size = vma->node.size;
95374d75 828 dst->page_count = 0;
0a97015d
CW
829 dst->unused = 0;
830
d637c178 831 if (!compress_init(&compress)) {
0a97015d
CW
832 kfree(dst);
833 return NULL;
834 }
03382dfb 835
95374d75
CW
836 for_each_sgt_dma(dma, iter, vma->pages) {
837 void __iomem *s;
838 int ret;
b3c3f5e6 839
95374d75
CW
840 ggtt->base.insert_page(&ggtt->base, dma, slot,
841 I915_CACHE_NONE, 0);
b3c3f5e6 842
95374d75 843 s = io_mapping_map_atomic_wc(&ggtt->mappable, slot);
d637c178 844 ret = compress_page(&compress, (void __force *)s, dst);
95374d75 845 io_mapping_unmap_atomic(s);
84734a04 846
95374d75 847 if (ret)
84734a04 848 goto unwind;
84734a04 849 }
95374d75 850 goto out;
84734a04
MK
851
852unwind:
95374d75
CW
853 while (dst->page_count--)
854 free_page((unsigned long)dst->pages[dst->page_count]);
84734a04 855 kfree(dst);
95374d75
CW
856 dst = NULL;
857
858out:
d637c178 859 compress_fini(&compress, dst);
4fb84d99 860 ggtt->base.clear_range(&ggtt->base, slot, PAGE_SIZE);
95374d75 861 return dst;
84734a04 862}
84734a04 863
d72d908b
CW
864/* The error capture is special as tries to run underneath the normal
865 * locking rules - so we use the raw version of the i915_gem_active lookup.
866 */
867static inline uint32_t
868__active_get_seqno(struct i915_gem_active *active)
869{
24327f83
JL
870 struct drm_i915_gem_request *request;
871
872 request = __i915_gem_active_peek(active);
873 return request ? request->global_seqno : 0;
d72d908b
CW
874}
875
876static inline int
877__active_get_engine_id(struct i915_gem_active *active)
878{
24327f83 879 struct drm_i915_gem_request *request;
d72d908b 880
24327f83
JL
881 request = __i915_gem_active_peek(active);
882 return request ? request->engine->id : -1;
d72d908b
CW
883}
884
84734a04 885static void capture_bo(struct drm_i915_error_buffer *err,
3a448734 886 struct i915_vma *vma)
84734a04 887{
3a448734 888 struct drm_i915_gem_object *obj = vma->obj;
b4716185 889 int i;
3a448734 890
84734a04
MK
891 err->size = obj->base.size;
892 err->name = obj->base.name;
d72d908b 893
666796da 894 for (i = 0; i < I915_NUM_ENGINES; i++)
d07f0e59 895 err->rseqno[i] = __active_get_seqno(&vma->last_read[i]);
5b8c8aec
CW
896 err->wseqno = __active_get_seqno(&obj->frontbuffer_write);
897 err->engine = __active_get_engine_id(&obj->frontbuffer_write);
d72d908b 898
3a448734 899 err->gtt_offset = vma->node.start;
84734a04
MK
900 err->read_domains = obj->base.read_domains;
901 err->write_domain = obj->base.write_domain;
49ef5294 902 err->fence_reg = vma->fence ? vma->fence->id : -1;
3e510a8e 903 err->tiling = i915_gem_object_get_tiling(obj);
a4f5ea64
CW
904 err->dirty = obj->mm.dirty;
905 err->purgeable = obj->mm.madv != I915_MADV_WILLNEED;
5cc9ed4b 906 err->userptr = obj->userptr.mm != NULL;
84734a04
MK
907 err->cache_level = obj->cache_level;
908}
909
c0ce4663
CW
910static u32 capture_error_bo(struct drm_i915_error_buffer *err,
911 int count, struct list_head *head,
912 bool pinned_only)
84734a04 913{
ca191b13 914 struct i915_vma *vma;
84734a04
MK
915 int i = 0;
916
1c7f4bca 917 list_for_each_entry(vma, head, vm_link) {
c0ce4663
CW
918 if (pinned_only && !i915_vma_is_pinned(vma))
919 continue;
920
3a448734 921 capture_bo(err++, vma);
84734a04
MK
922 if (++i == count)
923 break;
924 }
925
926 return i;
927}
928
011cf577
BW
929/* Generate a semi-unique error code. The code is not meant to have meaning, The
930 * code's only purpose is to try to prevent false duplicated bug reports by
931 * grossly estimating a GPU error state.
932 *
933 * TODO Ideally, hashing the batchbuffer would be a very nice way to determine
934 * the hang if we could strip the GTT offset information from it.
935 *
936 * It's only a small step better than a random number in its current form.
937 */
938static uint32_t i915_error_generate_code(struct drm_i915_private *dev_priv,
cb383002 939 struct drm_i915_error_state *error,
6361f4ba 940 int *engine_id)
011cf577
BW
941{
942 uint32_t error_code = 0;
943 int i;
944
945 /* IPEHR would be an ideal way to detect errors, as it's the gross
946 * measure of "the command that hung." However, has some very common
947 * synchronization commands which almost always appear in the case
948 * strictly a client bug. Use instdone to differentiate those some.
949 */
666796da 950 for (i = 0; i < I915_NUM_ENGINES; i++) {
3fe3b030 951 if (error->engine[i].hangcheck_stalled) {
6361f4ba
CW
952 if (engine_id)
953 *engine_id = i;
cb383002 954
d636951e
BW
955 return error->engine[i].ipehr ^
956 error->engine[i].instdone.instdone;
cb383002
MK
957 }
958 }
011cf577
BW
959
960 return error_code;
961}
962
c033666a 963static void i915_gem_record_fences(struct drm_i915_private *dev_priv,
84734a04
MK
964 struct drm_i915_error_state *error)
965{
84734a04
MK
966 int i;
967
c033666a 968 if (IS_GEN3(dev_priv) || IS_GEN2(dev_priv)) {
ce38ab05 969 for (i = 0; i < dev_priv->num_fence_regs; i++)
eecf613a 970 error->fence[i] = I915_READ(FENCE_REG(i));
c033666a 971 } else if (IS_GEN5(dev_priv) || IS_GEN4(dev_priv)) {
eecf613a
VS
972 for (i = 0; i < dev_priv->num_fence_regs; i++)
973 error->fence[i] = I915_READ64(FENCE_REG_965_LO(i));
c033666a 974 } else if (INTEL_GEN(dev_priv) >= 6) {
eecf613a
VS
975 for (i = 0; i < dev_priv->num_fence_regs; i++)
976 error->fence[i] = I915_READ64(FENCE_REG_GEN6_LO(i));
977 }
84734a04
MK
978}
979
85e17f59
CW
980static inline u32
981gen8_engine_sync_index(struct intel_engine_cs *engine,
982 struct intel_engine_cs *other)
983{
984 int idx;
985
986 /*
987 * rcs -> 0 = vcs, 1 = bcs, 2 = vecs, 3 = vcs2;
988 * vcs -> 0 = bcs, 1 = vecs, 2 = vcs2, 3 = rcs;
989 * bcs -> 0 = vecs, 1 = vcs2. 2 = rcs, 3 = vcs;
990 * vecs -> 0 = vcs2, 1 = rcs, 2 = vcs, 3 = bcs;
991 * vcs2 -> 0 = rcs, 1 = vcs, 2 = bcs, 3 = vecs;
992 */
993
994 idx = (other - engine) - 1;
995 if (idx < 0)
996 idx += I915_NUM_ENGINES;
997
998 return idx;
999}
87f85ebc 1000
6361f4ba 1001static void gen8_record_semaphore_state(struct drm_i915_error_state *error,
0bc40be8 1002 struct intel_engine_cs *engine,
6361f4ba 1003 struct drm_i915_error_engine *ee)
0ca36d78 1004{
6361f4ba 1005 struct drm_i915_private *dev_priv = engine->i915;
b4558b46 1006 struct intel_engine_cs *to;
c3232b18 1007 enum intel_engine_id id;
0ca36d78 1008
51d545d0 1009 if (!error->semaphore)
6361f4ba 1010 return;
0ca36d78 1011
3b3f1650 1012 for_each_engine(to, dev_priv, id) {
b4558b46
RV
1013 int idx;
1014 u16 signal_offset;
1015 u32 *tmp;
0ca36d78 1016
0bc40be8 1017 if (engine == to)
b4558b46
RV
1018 continue;
1019
6361f4ba
CW
1020 signal_offset =
1021 (GEN8_SIGNAL_OFFSET(engine, id) & (PAGE_SIZE - 1)) / 4;
51d545d0 1022 tmp = error->semaphore->pages[0];
85e17f59 1023 idx = gen8_engine_sync_index(engine, to);
b4558b46 1024
6361f4ba 1025 ee->semaphore_mboxes[idx] = tmp[signal_offset];
0ca36d78
BW
1026 }
1027}
1028
6361f4ba
CW
1029static void gen6_record_semaphore_state(struct intel_engine_cs *engine,
1030 struct drm_i915_error_engine *ee)
87f85ebc 1031{
6361f4ba
CW
1032 struct drm_i915_private *dev_priv = engine->i915;
1033
1034 ee->semaphore_mboxes[0] = I915_READ(RING_SYNC_0(engine->mmio_base));
1035 ee->semaphore_mboxes[1] = I915_READ(RING_SYNC_1(engine->mmio_base));
85e17f59 1036 if (HAS_VEBOX(dev_priv))
6361f4ba 1037 ee->semaphore_mboxes[2] =
0bc40be8 1038 I915_READ(RING_SYNC_2(engine->mmio_base));
87f85ebc
BW
1039}
1040
6361f4ba
CW
1041static void error_record_engine_waiters(struct intel_engine_cs *engine,
1042 struct drm_i915_error_engine *ee)
688e6c72
CW
1043{
1044 struct intel_breadcrumbs *b = &engine->breadcrumbs;
1045 struct drm_i915_error_waiter *waiter;
1046 struct rb_node *rb;
1047 int count;
1048
6361f4ba
CW
1049 ee->num_waiters = 0;
1050 ee->waiters = NULL;
688e6c72 1051
19eb9189
CW
1052 if (RB_EMPTY_ROOT(&b->waiters))
1053 return;
1054
f6168e33 1055 if (!spin_trylock_irq(&b->lock)) {
19eb9189
CW
1056 ee->waiters = ERR_PTR(-EDEADLK);
1057 return;
1058 }
1059
688e6c72
CW
1060 count = 0;
1061 for (rb = rb_first(&b->waiters); rb != NULL; rb = rb_next(rb))
1062 count++;
f6168e33 1063 spin_unlock_irq(&b->lock);
688e6c72
CW
1064
1065 waiter = NULL;
1066 if (count)
1067 waiter = kmalloc_array(count,
1068 sizeof(struct drm_i915_error_waiter),
1069 GFP_ATOMIC);
1070 if (!waiter)
1071 return;
1072
f6168e33 1073 if (!spin_trylock_irq(&b->lock)) {
19eb9189
CW
1074 kfree(waiter);
1075 ee->waiters = ERR_PTR(-EDEADLK);
1076 return;
1077 }
688e6c72 1078
19eb9189 1079 ee->waiters = waiter;
688e6c72
CW
1080 for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
1081 struct intel_wait *w = container_of(rb, typeof(*w), node);
1082
1083 strcpy(waiter->comm, w->tsk->comm);
1084 waiter->pid = w->tsk->pid;
1085 waiter->seqno = w->seqno;
1086 waiter++;
1087
6361f4ba 1088 if (++ee->num_waiters == count)
688e6c72
CW
1089 break;
1090 }
f6168e33 1091 spin_unlock_irq(&b->lock);
688e6c72
CW
1092}
1093
6361f4ba
CW
1094static void error_record_engine_registers(struct drm_i915_error_state *error,
1095 struct intel_engine_cs *engine,
1096 struct drm_i915_error_engine *ee)
84734a04 1097{
6361f4ba
CW
1098 struct drm_i915_private *dev_priv = engine->i915;
1099
c033666a 1100 if (INTEL_GEN(dev_priv) >= 6) {
6361f4ba
CW
1101 ee->rc_psmi = I915_READ(RING_PSMI_CTL(engine->mmio_base));
1102 ee->fault_reg = I915_READ(RING_FAULT_REG(engine));
c033666a 1103 if (INTEL_GEN(dev_priv) >= 8)
6361f4ba 1104 gen8_record_semaphore_state(error, engine, ee);
0ca36d78 1105 else
6361f4ba 1106 gen6_record_semaphore_state(engine, ee);
4e5aabfd
BW
1107 }
1108
c033666a 1109 if (INTEL_GEN(dev_priv) >= 4) {
6361f4ba
CW
1110 ee->faddr = I915_READ(RING_DMA_FADD(engine->mmio_base));
1111 ee->ipeir = I915_READ(RING_IPEIR(engine->mmio_base));
1112 ee->ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
6361f4ba
CW
1113 ee->instps = I915_READ(RING_INSTPS(engine->mmio_base));
1114 ee->bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
c033666a 1115 if (INTEL_GEN(dev_priv) >= 8) {
6361f4ba
CW
1116 ee->faddr |= (u64) I915_READ(RING_DMA_FADD_UDW(engine->mmio_base)) << 32;
1117 ee->bbaddr |= (u64) I915_READ(RING_BBADDR_UDW(engine->mmio_base)) << 32;
13ffadd1 1118 }
6361f4ba 1119 ee->bbstate = I915_READ(RING_BBSTATE(engine->mmio_base));
84734a04 1120 } else {
6361f4ba
CW
1121 ee->faddr = I915_READ(DMA_FADD_I8XX);
1122 ee->ipeir = I915_READ(IPEIR);
1123 ee->ipehr = I915_READ(IPEHR);
84734a04
MK
1124 }
1125
0e704476 1126 intel_engine_get_instdone(engine, &ee->instdone);
d636951e 1127
6361f4ba
CW
1128 ee->waiting = intel_engine_has_waiter(engine);
1129 ee->instpm = I915_READ(RING_INSTPM(engine->mmio_base));
7e37f889 1130 ee->acthd = intel_engine_get_active_head(engine);
6361f4ba 1131 ee->seqno = intel_engine_get_seqno(engine);
cb399eab 1132 ee->last_seqno = intel_engine_last_submit(engine);
6361f4ba
CW
1133 ee->start = I915_READ_START(engine);
1134 ee->head = I915_READ_HEAD(engine);
1135 ee->tail = I915_READ_TAIL(engine);
1136 ee->ctl = I915_READ_CTL(engine);
21a2c58a
CW
1137 if (INTEL_GEN(dev_priv) > 2)
1138 ee->mode = I915_READ_MODE(engine);
84734a04 1139
3177659a 1140 if (!HWS_NEEDS_PHYSICAL(dev_priv)) {
f0f59a00 1141 i915_reg_t mmio;
f3ce3821 1142
c033666a 1143 if (IS_GEN7(dev_priv)) {
0bc40be8 1144 switch (engine->id) {
f3ce3821
CW
1145 default:
1146 case RCS:
1147 mmio = RENDER_HWS_PGA_GEN7;
1148 break;
1149 case BCS:
1150 mmio = BLT_HWS_PGA_GEN7;
1151 break;
1152 case VCS:
1153 mmio = BSD_HWS_PGA_GEN7;
1154 break;
1155 case VECS:
1156 mmio = VEBOX_HWS_PGA_GEN7;
1157 break;
1158 }
c033666a 1159 } else if (IS_GEN6(engine->i915)) {
0bc40be8 1160 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
f3ce3821
CW
1161 } else {
1162 /* XXX: gen8 returns to sanity */
0bc40be8 1163 mmio = RING_HWS_PGA(engine->mmio_base);
f3ce3821
CW
1164 }
1165
6361f4ba 1166 ee->hws = I915_READ(mmio);
f3ce3821
CW
1167 }
1168
3fe3b030 1169 ee->hangcheck_timestamp = engine->hangcheck.action_timestamp;
6361f4ba 1170 ee->hangcheck_action = engine->hangcheck.action;
3fe3b030 1171 ee->hangcheck_stalled = engine->hangcheck.stalled;
6c7a01ec 1172
c033666a 1173 if (USES_PPGTT(dev_priv)) {
6c7a01ec
BW
1174 int i;
1175
6361f4ba 1176 ee->vm_info.gfx_mode = I915_READ(RING_MODE_GEN7(engine));
6c7a01ec 1177
c033666a 1178 if (IS_GEN6(dev_priv))
6361f4ba 1179 ee->vm_info.pp_dir_base =
0bc40be8 1180 I915_READ(RING_PP_DIR_BASE_READ(engine));
c033666a 1181 else if (IS_GEN7(dev_priv))
6361f4ba 1182 ee->vm_info.pp_dir_base =
0bc40be8 1183 I915_READ(RING_PP_DIR_BASE(engine));
c033666a 1184 else if (INTEL_GEN(dev_priv) >= 8)
6c7a01ec 1185 for (i = 0; i < 4; i++) {
6361f4ba 1186 ee->vm_info.pdp[i] =
0bc40be8 1187 I915_READ(GEN8_RING_PDP_UDW(engine, i));
6361f4ba
CW
1188 ee->vm_info.pdp[i] <<= 32;
1189 ee->vm_info.pdp[i] |=
0bc40be8 1190 I915_READ(GEN8_RING_PDP_LDW(engine, i));
6c7a01ec 1191 }
6c7a01ec 1192 }
84734a04
MK
1193}
1194
35ca039e
CW
1195static void record_request(struct drm_i915_gem_request *request,
1196 struct drm_i915_error_request *erq)
1197{
1198 erq->context = request->ctx->hw_id;
bc1d53c6 1199 erq->ban_score = request->ctx->ban_score;
65e4760e 1200 erq->seqno = request->global_seqno;
35ca039e
CW
1201 erq->jiffies = request->emitted_jiffies;
1202 erq->head = request->head;
1203 erq->tail = request->tail;
1204
1205 rcu_read_lock();
1206 erq->pid = request->ctx->pid ? pid_nr(request->ctx->pid) : 0;
1207 rcu_read_unlock();
1208}
1209
57bc699d
CW
1210static void engine_record_requests(struct intel_engine_cs *engine,
1211 struct drm_i915_gem_request *first,
1212 struct drm_i915_error_engine *ee)
1213{
1214 struct drm_i915_gem_request *request;
1215 int count;
1216
1217 count = 0;
1218 request = first;
73cb9701 1219 list_for_each_entry_from(request, &engine->timeline->requests, link)
57bc699d
CW
1220 count++;
1221 if (!count)
1222 return;
1223
1224 ee->requests = kcalloc(count, sizeof(*ee->requests), GFP_ATOMIC);
1225 if (!ee->requests)
1226 return;
1227
1228 ee->num_requests = count;
1229
1230 count = 0;
1231 request = first;
73cb9701 1232 list_for_each_entry_from(request, &engine->timeline->requests, link) {
57bc699d
CW
1233 if (count >= ee->num_requests) {
1234 /*
1235 * If the ring request list was changed in
1236 * between the point where the error request
1237 * list was created and dimensioned and this
1238 * point then just exit early to avoid crashes.
1239 *
1240 * We don't need to communicate that the
1241 * request list changed state during error
1242 * state capture and that the error state is
1243 * slightly incorrect as a consequence since we
1244 * are typically only interested in the request
1245 * list state at the point of error state
1246 * capture, not in any changes happening during
1247 * the capture.
1248 */
1249 break;
1250 }
1251
35ca039e 1252 record_request(request, &ee->requests[count++]);
57bc699d
CW
1253 }
1254 ee->num_requests = count;
1255}
1256
35ca039e
CW
1257static void error_record_engine_execlists(struct intel_engine_cs *engine,
1258 struct drm_i915_error_engine *ee)
1259{
1260 unsigned int n;
1261
1262 for (n = 0; n < ARRAY_SIZE(engine->execlist_port); n++)
1263 if (engine->execlist_port[n].request)
1264 record_request(engine->execlist_port[n].request,
1265 &ee->execlist[n]);
1266}
1267
c033666a 1268static void i915_gem_record_rings(struct drm_i915_private *dev_priv,
84734a04
MK
1269 struct drm_i915_error_state *error)
1270{
72e96d64 1271 struct i915_ggtt *ggtt = &dev_priv->ggtt;
57bc699d 1272 int i;
84734a04 1273
51d545d0 1274 error->semaphore =
058d88c4 1275 i915_error_object_create(dev_priv, dev_priv->semaphore);
6361f4ba 1276
666796da 1277 for (i = 0; i < I915_NUM_ENGINES; i++) {
3b3f1650 1278 struct intel_engine_cs *engine = dev_priv->engine[i];
6361f4ba 1279 struct drm_i915_error_engine *ee = &error->engine[i];
57bc699d 1280 struct drm_i915_gem_request *request;
372fbb8e 1281
6361f4ba
CW
1282 ee->pid = -1;
1283 ee->engine_id = -1;
eee73b46 1284
3b3f1650 1285 if (!engine)
372fbb8e
CW
1286 continue;
1287
6361f4ba 1288 ee->engine_id = i;
372fbb8e 1289
6361f4ba
CW
1290 error_record_engine_registers(error, engine, ee);
1291 error_record_engine_waiters(engine, ee);
35ca039e 1292 error_record_engine_execlists(engine, ee);
84734a04 1293
e2f80391 1294 request = i915_gem_find_active_request(engine);
ab0e7ff9 1295 if (request) {
7e37f889 1296 struct intel_ring *ring;
c84455b4 1297 struct pid *pid;
ae6c4806 1298
c0ce4663 1299 ee->vm = request->ctx->ppgtt ?
bc3d6744 1300 &request->ctx->ppgtt->base : &ggtt->base;
ae6c4806 1301
ab0e7ff9
CW
1302 /* We need to copy these to an anonymous buffer
1303 * as the simplest method to avoid being overwritten
1304 * by userspace.
1305 */
6361f4ba 1306 ee->batchbuffer =
ab0e7ff9 1307 i915_error_object_create(dev_priv,
058d88c4 1308 request->batch);
ab0e7ff9 1309
2d1fe073 1310 if (HAS_BROKEN_CS_TLB(dev_priv))
6361f4ba 1311 ee->wa_batchbuffer =
058d88c4
CW
1312 i915_error_object_create(dev_priv,
1313 engine->scratch);
ab0e7ff9 1314
058d88c4
CW
1315 ee->ctx =
1316 i915_error_object_create(dev_priv,
1317 request->ctx->engine[i].state);
546b1b6a 1318
c84455b4
CW
1319 pid = request->ctx->pid;
1320 if (pid) {
ab0e7ff9
CW
1321 struct task_struct *task;
1322
1323 rcu_read_lock();
c84455b4 1324 task = pid_task(pid, PIDTYPE_PID);
ab0e7ff9 1325 if (task) {
6361f4ba
CW
1326 strcpy(ee->comm, task->comm);
1327 ee->pid = task->pid;
ab0e7ff9
CW
1328 }
1329 rcu_read_unlock();
1330 }
84734a04 1331
bc3d6744
CW
1332 error->simulated |=
1333 request->ctx->flags & CONTEXT_NO_ERROR_CAPTURE;
1334
cdb324bd
CW
1335 ee->rq_head = request->head;
1336 ee->rq_post = request->postfix;
1337 ee->rq_tail = request->tail;
1338
1dae2dfb
CW
1339 ring = request->ring;
1340 ee->cpu_ring_head = ring->head;
1341 ee->cpu_ring_tail = ring->tail;
6361f4ba 1342 ee->ringbuffer =
058d88c4 1343 i915_error_object_create(dev_priv, ring->vma);
57bc699d
CW
1344
1345 engine_record_requests(engine, request, ee);
ba6e0418 1346 }
84734a04 1347
6361f4ba 1348 ee->hws_page =
058d88c4
CW
1349 i915_error_object_create(dev_priv,
1350 engine->status_page.vma);
84734a04 1351
058d88c4
CW
1352 ee->wa_ctx =
1353 i915_error_object_create(dev_priv, engine->wa_ctx.vma);
84734a04
MK
1354 }
1355}
1356
95f5301d
BW
1357static void i915_gem_capture_vm(struct drm_i915_private *dev_priv,
1358 struct drm_i915_error_state *error,
1359 struct i915_address_space *vm,
c0ce4663 1360 int idx)
84734a04 1361{
c0ce4663 1362 struct drm_i915_error_buffer *active_bo;
95f5301d 1363 struct i915_vma *vma;
c0ce4663 1364 int count;
84734a04 1365
c0ce4663 1366 count = 0;
1c7f4bca 1367 list_for_each_entry(vma, &vm->active_list, vm_link)
c0ce4663 1368 count++;
84734a04 1369
c0ce4663
CW
1370 active_bo = NULL;
1371 if (count)
1372 active_bo = kcalloc(count, sizeof(*active_bo), GFP_ATOMIC);
95f5301d 1373 if (active_bo)
c0ce4663
CW
1374 count = capture_error_bo(active_bo, count, &vm->active_list, false);
1375 else
1376 count = 0;
1377
1378 error->active_vm[idx] = vm;
1379 error->active_bo[idx] = active_bo;
1380 error->active_bo_count[idx] = count;
95f5301d
BW
1381}
1382
c0ce4663
CW
1383static void i915_capture_active_buffers(struct drm_i915_private *dev_priv,
1384 struct drm_i915_error_state *error)
95f5301d 1385{
c0ce4663
CW
1386 int cnt = 0, i, j;
1387
1388 BUILD_BUG_ON(ARRAY_SIZE(error->engine) > ARRAY_SIZE(error->active_bo));
1389 BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_vm));
1390 BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_bo_count));
1391
1392 /* Scan each engine looking for unique active contexts/vm */
1393 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
1394 struct drm_i915_error_engine *ee = &error->engine[i];
1395 bool found;
1396
1397 if (!ee->vm)
1398 continue;
3a448734 1399
c0ce4663
CW
1400 found = false;
1401 for (j = 0; j < i && !found; j++)
1402 found = error->engine[j].vm == ee->vm;
1403 if (!found)
1404 i915_gem_capture_vm(dev_priv, error, ee->vm, cnt++);
3a448734 1405 }
84734a04
MK
1406}
1407
c0ce4663
CW
1408static void i915_capture_pinned_buffers(struct drm_i915_private *dev_priv,
1409 struct drm_i915_error_state *error)
1410{
1411 struct i915_address_space *vm = &dev_priv->ggtt.base;
1412 struct drm_i915_error_buffer *bo;
1413 struct i915_vma *vma;
1414 int count_inactive, count_active;
1415
1416 count_inactive = 0;
1417 list_for_each_entry(vma, &vm->active_list, vm_link)
1418 count_inactive++;
1419
1420 count_active = 0;
1421 list_for_each_entry(vma, &vm->inactive_list, vm_link)
1422 count_active++;
1423
1424 bo = NULL;
1425 if (count_inactive + count_active)
1426 bo = kcalloc(count_inactive + count_active,
1427 sizeof(*bo), GFP_ATOMIC);
1428 if (!bo)
1429 return;
1430
1431 count_inactive = capture_error_bo(bo, count_inactive,
1432 &vm->active_list, true);
1433 count_active = capture_error_bo(bo + count_inactive, count_active,
1434 &vm->inactive_list, true);
1435 error->pinned_bo_count = count_inactive + count_active;
1436 error->pinned_bo = bo;
1437}
1438
27b85bea
AG
1439static void i915_gem_capture_guc_log_buffer(struct drm_i915_private *dev_priv,
1440 struct drm_i915_error_state *error)
1441{
1442 /* Capturing log buf contents won't be useful if logging was disabled */
1443 if (!dev_priv->guc.log.vma || (i915.guc_log_level < 0))
1444 return;
1445
1446 error->guc_log = i915_error_object_create(dev_priv,
1447 dev_priv->guc.log.vma);
1448}
1449
1d762aad
BW
1450/* Capture all registers which don't fit into another category. */
1451static void i915_capture_reg_state(struct drm_i915_private *dev_priv,
1452 struct drm_i915_error_state *error)
84734a04 1453{
885ea5a8 1454 int i;
84734a04 1455
654c90c6
BW
1456 /* General organization
1457 * 1. Registers specific to a single generation
1458 * 2. Registers which belong to multiple generations
1459 * 3. Feature specific registers.
1460 * 4. Everything else
1461 * Please try to follow the order.
1462 */
84734a04 1463
654c90c6 1464 /* 1: Registers specific to a single generation */
11a914c2 1465 if (IS_VALLEYVIEW(dev_priv)) {
885ea5a8 1466 error->gtier[0] = I915_READ(GTIER);
843db716 1467 error->ier = I915_READ(VLV_IER);
40181697 1468 error->forcewake = I915_READ_FW(FORCEWAKE_VLV);
654c90c6 1469 }
84734a04 1470
5db94019 1471 if (IS_GEN7(dev_priv))
654c90c6 1472 error->err_int = I915_READ(GEN7_ERR_INT);
84734a04 1473
5f56d5f9 1474 if (INTEL_GEN(dev_priv) >= 8) {
6c826f34
MK
1475 error->fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
1476 error->fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
1477 }
1478
5db94019 1479 if (IS_GEN6(dev_priv)) {
40181697 1480 error->forcewake = I915_READ_FW(FORCEWAKE);
91ec5d11
BW
1481 error->gab_ctl = I915_READ(GAB_CTL);
1482 error->gfx_mode = I915_READ(GFX_MODE);
1483 }
84734a04 1484
654c90c6 1485 /* 2: Registers which belong to multiple generations */
5f56d5f9 1486 if (INTEL_GEN(dev_priv) >= 7)
40181697 1487 error->forcewake = I915_READ_FW(FORCEWAKE_MT);
84734a04 1488
5f56d5f9 1489 if (INTEL_GEN(dev_priv) >= 6) {
654c90c6 1490 error->derrmr = I915_READ(DERRMR);
84734a04
MK
1491 error->error = I915_READ(ERROR_GEN6);
1492 error->done_reg = I915_READ(DONE_REG);
1493 }
1494
654c90c6 1495 /* 3: Feature specific registers */
5db94019 1496 if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
91ec5d11
BW
1497 error->gam_ecochk = I915_READ(GAM_ECOCHK);
1498 error->gac_eco = I915_READ(GAC_ECO_BITS);
1499 }
1500
1501 /* 4: Everything else */
0031fb96 1502 if (HAS_HW_CONTEXTS(dev_priv))
654c90c6
BW
1503 error->ccid = I915_READ(CCID);
1504
5f56d5f9 1505 if (INTEL_GEN(dev_priv) >= 8) {
885ea5a8
RV
1506 error->ier = I915_READ(GEN8_DE_MISC_IER);
1507 for (i = 0; i < 4; i++)
1508 error->gtier[i] = I915_READ(GEN8_GT_IER(i));
6e266956 1509 } else if (HAS_PCH_SPLIT(dev_priv)) {
843db716 1510 error->ier = I915_READ(DEIER);
885ea5a8 1511 error->gtier[0] = I915_READ(GTIER);
5db94019 1512 } else if (IS_GEN2(dev_priv)) {
843db716 1513 error->ier = I915_READ16(IER);
11a914c2 1514 } else if (!IS_VALLEYVIEW(dev_priv)) {
843db716 1515 error->ier = I915_READ(IER);
654c90c6 1516 }
654c90c6
BW
1517 error->eir = I915_READ(EIR);
1518 error->pgtbl_er = I915_READ(PGTBL_ER);
1d762aad
BW
1519}
1520
c033666a 1521static void i915_error_capture_msg(struct drm_i915_private *dev_priv,
58174462 1522 struct drm_i915_error_state *error,
14b730fc 1523 u32 engine_mask,
58174462 1524 const char *error_msg)
cb383002 1525{
cb383002 1526 u32 ecode;
6361f4ba 1527 int engine_id = -1, len;
cb383002 1528
6361f4ba 1529 ecode = i915_error_generate_code(dev_priv, error, &engine_id);
cb383002 1530
58174462 1531 len = scnprintf(error->error_msg, sizeof(error->error_msg),
0b5492d6 1532 "GPU HANG: ecode %d:%d:0x%08x",
6361f4ba 1533 INTEL_GEN(dev_priv), engine_id, ecode);
58174462 1534
6361f4ba 1535 if (engine_id != -1 && error->engine[engine_id].pid != -1)
58174462
MK
1536 len += scnprintf(error->error_msg + len,
1537 sizeof(error->error_msg) - len,
1538 ", in %s [%d]",
6361f4ba
CW
1539 error->engine[engine_id].comm,
1540 error->engine[engine_id].pid);
58174462
MK
1541
1542 scnprintf(error->error_msg + len, sizeof(error->error_msg) - len,
1543 ", reason: %s, action: %s",
1544 error_msg,
14b730fc 1545 engine_mask ? "reset" : "continue");
cb383002
MK
1546}
1547
48b031e3
MK
1548static void i915_capture_gen_state(struct drm_i915_private *dev_priv,
1549 struct drm_i915_error_state *error)
1550{
eb5be9d0
CW
1551 error->iommu = -1;
1552#ifdef CONFIG_INTEL_IOMMU
1553 error->iommu = intel_iommu_gfx_mapped;
1554#endif
48b031e3 1555 error->reset_count = i915_reset_count(&dev_priv->gpu_error);
62d5d69b 1556 error->suspend_count = dev_priv->suspend_count;
2bd160a1
CW
1557
1558 memcpy(&error->device_info,
1559 INTEL_INFO(dev_priv),
1560 sizeof(error->device_info));
48b031e3
MK
1561}
1562
9f267eb8
CW
1563static int capture(void *data)
1564{
1565 struct drm_i915_error_state *error = data;
1566
9f267eb8
CW
1567 i915_capture_gen_state(error->i915, error);
1568 i915_capture_reg_state(error->i915, error);
1569 i915_gem_record_fences(error->i915, error);
1570 i915_gem_record_rings(error->i915, error);
1571 i915_capture_active_buffers(error->i915, error);
1572 i915_capture_pinned_buffers(error->i915, error);
27b85bea 1573 i915_gem_capture_guc_log_buffer(error->i915, error);
9f267eb8
CW
1574
1575 do_gettimeofday(&error->time);
de867c20
CW
1576 error->boottime = ktime_to_timeval(ktime_get_boottime());
1577 error->uptime =
1578 ktime_to_timeval(ktime_sub(ktime_get(),
1579 error->i915->gt.last_init_time));
9f267eb8
CW
1580
1581 error->overlay = intel_overlay_capture_error_state(error->i915);
1582 error->display = intel_display_capture_error_state(error->i915);
1583
9f267eb8
CW
1584 return 0;
1585}
1586
eafc4894
CW
1587#define DAY_AS_SECONDS(x) (24 * 60 * 60 * (x))
1588
1d762aad
BW
1589/**
1590 * i915_capture_error_state - capture an error record for later analysis
1591 * @dev: drm device
1592 *
1593 * Should be called when an error is detected (either a hang or an error
1594 * interrupt) to capture error state from the time of the error. Fills
1595 * out a structure which becomes available in debugfs for user level tools
1596 * to pick up.
1597 */
c033666a
CW
1598void i915_capture_error_state(struct drm_i915_private *dev_priv,
1599 u32 engine_mask,
58174462 1600 const char *error_msg)
1d762aad 1601{
53a4c6b2 1602 static bool warned;
1d762aad
BW
1603 struct drm_i915_error_state *error;
1604 unsigned long flags;
1d762aad 1605
98a2f411
CW
1606 if (!i915.error_capture)
1607 return;
1608
9777cca0
CW
1609 if (READ_ONCE(dev_priv->gpu_error.first_error))
1610 return;
1611
1d762aad
BW
1612 /* Account for pipe specific data like PIPE*STAT */
1613 error = kzalloc(sizeof(*error), GFP_ATOMIC);
1614 if (!error) {
1615 DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1616 return;
1617 }
1618
011cf577 1619 kref_init(&error->ref);
9f267eb8 1620 error->i915 = dev_priv;
011cf577 1621
9f267eb8 1622 stop_machine(capture, error, NULL);
84734a04 1623
c033666a 1624 i915_error_capture_msg(dev_priv, error, engine_mask, error_msg);
cb383002
MK
1625 DRM_INFO("%s\n", error->error_msg);
1626
bc3d6744
CW
1627 if (!error->simulated) {
1628 spin_lock_irqsave(&dev_priv->gpu_error.lock, flags);
1629 if (!dev_priv->gpu_error.first_error) {
1630 dev_priv->gpu_error.first_error = error;
1631 error = NULL;
1632 }
1633 spin_unlock_irqrestore(&dev_priv->gpu_error.lock, flags);
84734a04 1634 }
84734a04 1635
cb383002 1636 if (error) {
84734a04 1637 i915_error_state_free(&error->ref);
cb383002
MK
1638 return;
1639 }
1640
eafc4894
CW
1641 if (!warned &&
1642 ktime_get_real_seconds() - DRIVER_TIMESTAMP < DAY_AS_SECONDS(180)) {
cb383002
MK
1643 DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
1644 DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
1645 DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
1646 DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
91c8a326
CW
1647 DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n",
1648 dev_priv->drm.primary->index);
cb383002
MK
1649 warned = true;
1650 }
84734a04
MK
1651}
1652
1653void i915_error_state_get(struct drm_device *dev,
1654 struct i915_error_state_file_priv *error_priv)
1655{
fac5e23e 1656 struct drm_i915_private *dev_priv = to_i915(dev);
84734a04 1657
5b254c59 1658 spin_lock_irq(&dev_priv->gpu_error.lock);
84734a04
MK
1659 error_priv->error = dev_priv->gpu_error.first_error;
1660 if (error_priv->error)
1661 kref_get(&error_priv->error->ref);
5b254c59 1662 spin_unlock_irq(&dev_priv->gpu_error.lock);
84734a04
MK
1663}
1664
1665void i915_error_state_put(struct i915_error_state_file_priv *error_priv)
1666{
1667 if (error_priv->error)
1668 kref_put(&error_priv->error->ref, i915_error_state_free);
1669}
1670
12ff05e7 1671void i915_destroy_error_state(struct drm_i915_private *dev_priv)
84734a04 1672{
84734a04 1673 struct drm_i915_error_state *error;
84734a04 1674
5b254c59 1675 spin_lock_irq(&dev_priv->gpu_error.lock);
84734a04
MK
1676 error = dev_priv->gpu_error.first_error;
1677 dev_priv->gpu_error.first_error = NULL;
5b254c59 1678 spin_unlock_irq(&dev_priv->gpu_error.lock);
84734a04
MK
1679
1680 if (error)
1681 kref_put(&error->ref, i915_error_state_free);
1682}