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