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>
31 #include <linux/stop_machine.h>
32 #include <linux/zlib.h>
33 #include <drm/drm_print.h>
34 #include <linux/ascii85.h>
35 
36 #include "i915_gpu_error.h"
37 #include "i915_drv.h"
38 
39 static inline const struct intel_engine_cs *
40 engine_lookup(const struct drm_i915_private *i915, unsigned int id)
41 {
42 	if (id >= I915_NUM_ENGINES)
43 		return NULL;
44 
45 	return i915->engine[id];
46 }
47 
48 static inline const char *
49 __engine_name(const struct intel_engine_cs *engine)
50 {
51 	return engine ? engine->name : "";
52 }
53 
54 static const char *
55 engine_name(const struct drm_i915_private *i915, unsigned int id)
56 {
57 	return __engine_name(engine_lookup(i915, id));
58 }
59 
60 static const char *tiling_flag(int tiling)
61 {
62 	switch (tiling) {
63 	default:
64 	case I915_TILING_NONE: return "";
65 	case I915_TILING_X: return " X";
66 	case I915_TILING_Y: return " Y";
67 	}
68 }
69 
70 static const char *dirty_flag(int dirty)
71 {
72 	return dirty ? " dirty" : "";
73 }
74 
75 static const char *purgeable_flag(int purgeable)
76 {
77 	return purgeable ? " purgeable" : "";
78 }
79 
80 static bool __i915_error_ok(struct drm_i915_error_state_buf *e)
81 {
82 
83 	if (!e->err && WARN(e->bytes > (e->size - 1), "overflow")) {
84 		e->err = -ENOSPC;
85 		return false;
86 	}
87 
88 	if (e->bytes == e->size - 1 || e->err)
89 		return false;
90 
91 	return true;
92 }
93 
94 static bool __i915_error_seek(struct drm_i915_error_state_buf *e,
95 			      unsigned len)
96 {
97 	if (e->pos + len <= e->start) {
98 		e->pos += len;
99 		return false;
100 	}
101 
102 	/* First vsnprintf needs to fit in its entirety for memmove */
103 	if (len >= e->size) {
104 		e->err = -EIO;
105 		return false;
106 	}
107 
108 	return true;
109 }
110 
111 static void __i915_error_advance(struct drm_i915_error_state_buf *e,
112 				 unsigned len)
113 {
114 	/* If this is first printf in this window, adjust it so that
115 	 * start position matches start of the buffer
116 	 */
117 
118 	if (e->pos < e->start) {
119 		const size_t off = e->start - e->pos;
120 
121 		/* Should not happen but be paranoid */
122 		if (off > len || e->bytes) {
123 			e->err = -EIO;
124 			return;
125 		}
126 
127 		memmove(e->buf, e->buf + off, len - off);
128 		e->bytes = len - off;
129 		e->pos = e->start;
130 		return;
131 	}
132 
133 	e->bytes += len;
134 	e->pos += len;
135 }
136 
137 __printf(2, 0)
138 static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
139 			       const char *f, va_list args)
140 {
141 	unsigned len;
142 
143 	if (!__i915_error_ok(e))
144 		return;
145 
146 	/* Seek the first printf which is hits start position */
147 	if (e->pos < e->start) {
148 		va_list tmp;
149 
150 		va_copy(tmp, args);
151 		len = vsnprintf(NULL, 0, f, tmp);
152 		va_end(tmp);
153 
154 		if (!__i915_error_seek(e, len))
155 			return;
156 	}
157 
158 	len = vsnprintf(e->buf + e->bytes, e->size - e->bytes, f, args);
159 	if (len >= e->size - e->bytes)
160 		len = e->size - e->bytes - 1;
161 
162 	__i915_error_advance(e, len);
163 }
164 
165 static void i915_error_puts(struct drm_i915_error_state_buf *e,
166 			    const char *str)
167 {
168 	unsigned len;
169 
170 	if (!__i915_error_ok(e))
171 		return;
172 
173 	len = strlen(str);
174 
175 	/* Seek the first printf which is hits start position */
176 	if (e->pos < e->start) {
177 		if (!__i915_error_seek(e, len))
178 			return;
179 	}
180 
181 	if (len >= e->size - e->bytes)
182 		len = e->size - e->bytes - 1;
183 	memcpy(e->buf + e->bytes, str, len);
184 
185 	__i915_error_advance(e, len);
186 }
187 
188 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
189 #define err_puts(e, s) i915_error_puts(e, s)
190 
191 static void __i915_printfn_error(struct drm_printer *p, struct va_format *vaf)
192 {
193 	i915_error_vprintf(p->arg, vaf->fmt, *vaf->va);
194 }
195 
196 static inline struct drm_printer
197 i915_error_printer(struct drm_i915_error_state_buf *e)
198 {
199 	struct drm_printer p = {
200 		.printfn = __i915_printfn_error,
201 		.arg = e,
202 	};
203 	return p;
204 }
205 
206 #ifdef CONFIG_DRM_I915_COMPRESS_ERROR
207 
208 struct compress {
209 	struct z_stream_s zstream;
210 	void *tmp;
211 };
212 
213 static bool compress_init(struct compress *c)
214 {
215 	struct z_stream_s *zstream = memset(&c->zstream, 0, sizeof(c->zstream));
216 
217 	zstream->workspace =
218 		kmalloc(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
219 			GFP_ATOMIC | __GFP_NOWARN);
220 	if (!zstream->workspace)
221 		return false;
222 
223 	if (zlib_deflateInit(zstream, Z_DEFAULT_COMPRESSION) != Z_OK) {
224 		kfree(zstream->workspace);
225 		return false;
226 	}
227 
228 	c->tmp = NULL;
229 	if (i915_has_memcpy_from_wc())
230 		c->tmp = (void *)__get_free_page(GFP_ATOMIC | __GFP_NOWARN);
231 
232 	return true;
233 }
234 
235 static void *compress_next_page(struct drm_i915_error_object *dst)
236 {
237 	unsigned long page;
238 
239 	if (dst->page_count >= dst->num_pages)
240 		return ERR_PTR(-ENOSPC);
241 
242 	page = __get_free_page(GFP_ATOMIC | __GFP_NOWARN);
243 	if (!page)
244 		return ERR_PTR(-ENOMEM);
245 
246 	return dst->pages[dst->page_count++] = (void *)page;
247 }
248 
249 static int compress_page(struct compress *c,
250 			 void *src,
251 			 struct drm_i915_error_object *dst)
252 {
253 	struct z_stream_s *zstream = &c->zstream;
254 
255 	zstream->next_in = src;
256 	if (c->tmp && i915_memcpy_from_wc(c->tmp, src, PAGE_SIZE))
257 		zstream->next_in = c->tmp;
258 	zstream->avail_in = PAGE_SIZE;
259 
260 	do {
261 		if (zstream->avail_out == 0) {
262 			zstream->next_out = compress_next_page(dst);
263 			if (IS_ERR(zstream->next_out))
264 				return PTR_ERR(zstream->next_out);
265 
266 			zstream->avail_out = PAGE_SIZE;
267 		}
268 
269 		if (zlib_deflate(zstream, Z_NO_FLUSH) != Z_OK)
270 			return -EIO;
271 	} while (zstream->avail_in);
272 
273 	/* Fallback to uncompressed if we increase size? */
274 	if (0 && zstream->total_out > zstream->total_in)
275 		return -E2BIG;
276 
277 	return 0;
278 }
279 
280 static int compress_flush(struct compress *c,
281 			  struct drm_i915_error_object *dst)
282 {
283 	struct z_stream_s *zstream = &c->zstream;
284 
285 	do {
286 		switch (zlib_deflate(zstream, Z_FINISH)) {
287 		case Z_OK: /* more space requested */
288 			zstream->next_out = compress_next_page(dst);
289 			if (IS_ERR(zstream->next_out))
290 				return PTR_ERR(zstream->next_out);
291 
292 			zstream->avail_out = PAGE_SIZE;
293 			break;
294 
295 		case Z_STREAM_END:
296 			goto end;
297 
298 		default: /* any error */
299 			return -EIO;
300 		}
301 	} while (1);
302 
303 end:
304 	memset(zstream->next_out, 0, zstream->avail_out);
305 	dst->unused = zstream->avail_out;
306 	return 0;
307 }
308 
309 static void compress_fini(struct compress *c,
310 			  struct drm_i915_error_object *dst)
311 {
312 	struct z_stream_s *zstream = &c->zstream;
313 
314 	zlib_deflateEnd(zstream);
315 	kfree(zstream->workspace);
316 	if (c->tmp)
317 		free_page((unsigned long)c->tmp);
318 }
319 
320 static void err_compression_marker(struct drm_i915_error_state_buf *m)
321 {
322 	err_puts(m, ":");
323 }
324 
325 #else
326 
327 struct compress {
328 };
329 
330 static bool compress_init(struct compress *c)
331 {
332 	return true;
333 }
334 
335 static int compress_page(struct compress *c,
336 			 void *src,
337 			 struct drm_i915_error_object *dst)
338 {
339 	unsigned long page;
340 	void *ptr;
341 
342 	page = __get_free_page(GFP_ATOMIC | __GFP_NOWARN);
343 	if (!page)
344 		return -ENOMEM;
345 
346 	ptr = (void *)page;
347 	if (!i915_memcpy_from_wc(ptr, src, PAGE_SIZE))
348 		memcpy(ptr, src, PAGE_SIZE);
349 	dst->pages[dst->page_count++] = ptr;
350 
351 	return 0;
352 }
353 
354 static int compress_flush(struct compress *c,
355 			  struct drm_i915_error_object *dst)
356 {
357 	return 0;
358 }
359 
360 static void compress_fini(struct compress *c,
361 			  struct drm_i915_error_object *dst)
362 {
363 }
364 
365 static void err_compression_marker(struct drm_i915_error_state_buf *m)
366 {
367 	err_puts(m, "~");
368 }
369 
370 #endif
371 
372 static void print_error_buffers(struct drm_i915_error_state_buf *m,
373 				const char *name,
374 				struct drm_i915_error_buffer *err,
375 				int count)
376 {
377 	err_printf(m, "%s [%d]:\n", name, count);
378 
379 	while (count--) {
380 		err_printf(m, "    %08x_%08x %8u %02x %02x %02x",
381 			   upper_32_bits(err->gtt_offset),
382 			   lower_32_bits(err->gtt_offset),
383 			   err->size,
384 			   err->read_domains,
385 			   err->write_domain,
386 			   err->wseqno);
387 		err_puts(m, tiling_flag(err->tiling));
388 		err_puts(m, dirty_flag(err->dirty));
389 		err_puts(m, purgeable_flag(err->purgeable));
390 		err_puts(m, err->userptr ? " userptr" : "");
391 		err_puts(m, err->engine != -1 ? " " : "");
392 		err_puts(m, engine_name(m->i915, err->engine));
393 		err_puts(m, i915_cache_level_str(m->i915, err->cache_level));
394 
395 		if (err->name)
396 			err_printf(m, " (name: %d)", err->name);
397 		if (err->fence_reg != I915_FENCE_REG_NONE)
398 			err_printf(m, " (fence: %d)", err->fence_reg);
399 
400 		err_puts(m, "\n");
401 		err++;
402 	}
403 }
404 
405 static void error_print_instdone(struct drm_i915_error_state_buf *m,
406 				 const struct drm_i915_error_engine *ee)
407 {
408 	int slice;
409 	int subslice;
410 
411 	err_printf(m, "  INSTDONE: 0x%08x\n",
412 		   ee->instdone.instdone);
413 
414 	if (ee->engine_id != RCS || INTEL_GEN(m->i915) <= 3)
415 		return;
416 
417 	err_printf(m, "  SC_INSTDONE: 0x%08x\n",
418 		   ee->instdone.slice_common);
419 
420 	if (INTEL_GEN(m->i915) <= 6)
421 		return;
422 
423 	for_each_instdone_slice_subslice(m->i915, slice, subslice)
424 		err_printf(m, "  SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
425 			   slice, subslice,
426 			   ee->instdone.sampler[slice][subslice]);
427 
428 	for_each_instdone_slice_subslice(m->i915, slice, subslice)
429 		err_printf(m, "  ROW_INSTDONE[%d][%d]: 0x%08x\n",
430 			   slice, subslice,
431 			   ee->instdone.row[slice][subslice]);
432 }
433 
434 static const char *bannable(const struct drm_i915_error_context *ctx)
435 {
436 	return ctx->bannable ? "" : " (unbannable)";
437 }
438 
439 static void error_print_request(struct drm_i915_error_state_buf *m,
440 				const char *prefix,
441 				const struct drm_i915_error_request *erq,
442 				const unsigned long epoch)
443 {
444 	if (!erq->seqno)
445 		return;
446 
447 	err_printf(m, "%s pid %d, ban score %d, seqno %8x:%08x, prio %d, emitted %dms, start %08x, head %08x, tail %08x\n",
448 		   prefix, erq->pid, erq->ban_score,
449 		   erq->context, erq->seqno, erq->sched_attr.priority,
450 		   jiffies_to_msecs(erq->jiffies - epoch),
451 		   erq->start, erq->head, erq->tail);
452 }
453 
454 static void error_print_context(struct drm_i915_error_state_buf *m,
455 				const char *header,
456 				const struct drm_i915_error_context *ctx)
457 {
458 	err_printf(m, "%s%s[%d] user_handle %d hw_id %d, prio %d, ban score %d%s guilty %d active %d\n",
459 		   header, ctx->comm, ctx->pid, ctx->handle, ctx->hw_id,
460 		   ctx->sched_attr.priority, ctx->ban_score, bannable(ctx),
461 		   ctx->guilty, ctx->active);
462 }
463 
464 static void error_print_engine(struct drm_i915_error_state_buf *m,
465 			       const struct drm_i915_error_engine *ee,
466 			       const unsigned long epoch)
467 {
468 	int n;
469 
470 	err_printf(m, "%s command stream:\n",
471 		   engine_name(m->i915, ee->engine_id));
472 	err_printf(m, "  IDLE?: %s\n", yesno(ee->idle));
473 	err_printf(m, "  START: 0x%08x\n", ee->start);
474 	err_printf(m, "  HEAD:  0x%08x [0x%08x]\n", ee->head, ee->rq_head);
475 	err_printf(m, "  TAIL:  0x%08x [0x%08x, 0x%08x]\n",
476 		   ee->tail, ee->rq_post, ee->rq_tail);
477 	err_printf(m, "  CTL:   0x%08x\n", ee->ctl);
478 	err_printf(m, "  MODE:  0x%08x\n", ee->mode);
479 	err_printf(m, "  HWS:   0x%08x\n", ee->hws);
480 	err_printf(m, "  ACTHD: 0x%08x %08x\n",
481 		   (u32)(ee->acthd>>32), (u32)ee->acthd);
482 	err_printf(m, "  IPEIR: 0x%08x\n", ee->ipeir);
483 	err_printf(m, "  IPEHR: 0x%08x\n", ee->ipehr);
484 
485 	error_print_instdone(m, ee);
486 
487 	if (ee->batchbuffer) {
488 		u64 start = ee->batchbuffer->gtt_offset;
489 		u64 end = start + ee->batchbuffer->gtt_size;
490 
491 		err_printf(m, "  batch: [0x%08x_%08x, 0x%08x_%08x]\n",
492 			   upper_32_bits(start), lower_32_bits(start),
493 			   upper_32_bits(end), lower_32_bits(end));
494 	}
495 	if (INTEL_GEN(m->i915) >= 4) {
496 		err_printf(m, "  BBADDR: 0x%08x_%08x\n",
497 			   (u32)(ee->bbaddr>>32), (u32)ee->bbaddr);
498 		err_printf(m, "  BB_STATE: 0x%08x\n", ee->bbstate);
499 		err_printf(m, "  INSTPS: 0x%08x\n", ee->instps);
500 	}
501 	err_printf(m, "  INSTPM: 0x%08x\n", ee->instpm);
502 	err_printf(m, "  FADDR: 0x%08x %08x\n", upper_32_bits(ee->faddr),
503 		   lower_32_bits(ee->faddr));
504 	if (INTEL_GEN(m->i915) >= 6) {
505 		err_printf(m, "  RC PSMI: 0x%08x\n", ee->rc_psmi);
506 		err_printf(m, "  FAULT_REG: 0x%08x\n", ee->fault_reg);
507 		err_printf(m, "  SYNC_0: 0x%08x\n",
508 			   ee->semaphore_mboxes[0]);
509 		err_printf(m, "  SYNC_1: 0x%08x\n",
510 			   ee->semaphore_mboxes[1]);
511 		if (HAS_VEBOX(m->i915))
512 			err_printf(m, "  SYNC_2: 0x%08x\n",
513 				   ee->semaphore_mboxes[2]);
514 	}
515 	if (HAS_PPGTT(m->i915)) {
516 		err_printf(m, "  GFX_MODE: 0x%08x\n", ee->vm_info.gfx_mode);
517 
518 		if (INTEL_GEN(m->i915) >= 8) {
519 			int i;
520 			for (i = 0; i < 4; i++)
521 				err_printf(m, "  PDP%d: 0x%016llx\n",
522 					   i, ee->vm_info.pdp[i]);
523 		} else {
524 			err_printf(m, "  PP_DIR_BASE: 0x%08x\n",
525 				   ee->vm_info.pp_dir_base);
526 		}
527 	}
528 	err_printf(m, "  seqno: 0x%08x\n", ee->seqno);
529 	err_printf(m, "  last_seqno: 0x%08x\n", ee->last_seqno);
530 	err_printf(m, "  waiting: %s\n", yesno(ee->waiting));
531 	err_printf(m, "  ring->head: 0x%08x\n", ee->cpu_ring_head);
532 	err_printf(m, "  ring->tail: 0x%08x\n", ee->cpu_ring_tail);
533 	err_printf(m, "  hangcheck stall: %s\n", yesno(ee->hangcheck_stalled));
534 	err_printf(m, "  hangcheck action: %s\n",
535 		   hangcheck_action_to_str(ee->hangcheck_action));
536 	err_printf(m, "  hangcheck action timestamp: %dms (%lu%s)\n",
537 		   jiffies_to_msecs(ee->hangcheck_timestamp - epoch),
538 		   ee->hangcheck_timestamp,
539 		   ee->hangcheck_timestamp == epoch ? "; epoch" : "");
540 	err_printf(m, "  engine reset count: %u\n", ee->reset_count);
541 
542 	for (n = 0; n < ee->num_ports; n++) {
543 		err_printf(m, "  ELSP[%d]:", n);
544 		error_print_request(m, " ", &ee->execlist[n], epoch);
545 	}
546 
547 	error_print_context(m, "  Active context: ", &ee->context);
548 }
549 
550 void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
551 {
552 	va_list args;
553 
554 	va_start(args, f);
555 	i915_error_vprintf(e, f, args);
556 	va_end(args);
557 }
558 
559 static void print_error_obj(struct drm_i915_error_state_buf *m,
560 			    struct intel_engine_cs *engine,
561 			    const char *name,
562 			    struct drm_i915_error_object *obj)
563 {
564 	char out[ASCII85_BUFSZ];
565 	int page;
566 
567 	if (!obj)
568 		return;
569 
570 	if (name) {
571 		err_printf(m, "%s --- %s = 0x%08x %08x\n",
572 			   engine ? engine->name : "global", name,
573 			   upper_32_bits(obj->gtt_offset),
574 			   lower_32_bits(obj->gtt_offset));
575 	}
576 
577 	err_compression_marker(m);
578 	for (page = 0; page < obj->page_count; page++) {
579 		int i, len;
580 
581 		len = PAGE_SIZE;
582 		if (page == obj->page_count - 1)
583 			len -= obj->unused;
584 		len = ascii85_encode_len(len);
585 
586 		for (i = 0; i < len; i++)
587 			err_puts(m, ascii85_encode(obj->pages[page][i], out));
588 	}
589 	err_puts(m, "\n");
590 }
591 
592 static void err_print_capabilities(struct drm_i915_error_state_buf *m,
593 				   const struct intel_device_info *info,
594 				   const struct intel_driver_caps *caps)
595 {
596 	struct drm_printer p = i915_error_printer(m);
597 
598 	intel_device_info_dump_flags(info, &p);
599 	intel_driver_caps_print(caps, &p);
600 	intel_device_info_dump_topology(&info->sseu, &p);
601 }
602 
603 static void err_print_params(struct drm_i915_error_state_buf *m,
604 			     const struct i915_params *params)
605 {
606 	struct drm_printer p = i915_error_printer(m);
607 
608 	i915_params_dump(params, &p);
609 }
610 
611 static void err_print_pciid(struct drm_i915_error_state_buf *m,
612 			    struct drm_i915_private *i915)
613 {
614 	struct pci_dev *pdev = i915->drm.pdev;
615 
616 	err_printf(m, "PCI ID: 0x%04x\n", pdev->device);
617 	err_printf(m, "PCI Revision: 0x%02x\n", pdev->revision);
618 	err_printf(m, "PCI Subsystem: %04x:%04x\n",
619 		   pdev->subsystem_vendor,
620 		   pdev->subsystem_device);
621 }
622 
623 static void err_print_uc(struct drm_i915_error_state_buf *m,
624 			 const struct i915_error_uc *error_uc)
625 {
626 	struct drm_printer p = i915_error_printer(m);
627 	const struct i915_gpu_state *error =
628 		container_of(error_uc, typeof(*error), uc);
629 
630 	if (!error->device_info.has_guc)
631 		return;
632 
633 	intel_uc_fw_dump(&error_uc->guc_fw, &p);
634 	intel_uc_fw_dump(&error_uc->huc_fw, &p);
635 	print_error_obj(m, NULL, "GuC log buffer", error_uc->guc_log);
636 }
637 
638 int i915_error_state_to_str(struct drm_i915_error_state_buf *m,
639 			    const struct i915_gpu_state *error)
640 {
641 	struct drm_i915_private *dev_priv = m->i915;
642 	struct drm_i915_error_object *obj;
643 	struct timespec64 ts;
644 	int i, j;
645 
646 	if (!error) {
647 		err_printf(m, "No error state collected\n");
648 		return 0;
649 	}
650 
651 	if (*error->error_msg)
652 		err_printf(m, "%s\n", error->error_msg);
653 	err_printf(m, "Kernel: " UTS_RELEASE "\n");
654 	ts = ktime_to_timespec64(error->time);
655 	err_printf(m, "Time: %lld s %ld us\n",
656 		   (s64)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
657 	ts = ktime_to_timespec64(error->boottime);
658 	err_printf(m, "Boottime: %lld s %ld us\n",
659 		   (s64)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
660 	ts = ktime_to_timespec64(error->uptime);
661 	err_printf(m, "Uptime: %lld s %ld us\n",
662 		   (s64)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
663 	err_printf(m, "Epoch: %lu jiffies (%u HZ)\n", error->epoch, HZ);
664 	err_printf(m, "Capture: %lu jiffies; %d ms ago, %d ms after epoch\n",
665 		   error->capture,
666 		   jiffies_to_msecs(jiffies - error->capture),
667 		   jiffies_to_msecs(error->capture - error->epoch));
668 
669 	for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
670 		if (error->engine[i].hangcheck_stalled &&
671 		    error->engine[i].context.pid) {
672 			err_printf(m, "Active process (on ring %s): %s [%d], score %d%s\n",
673 				   engine_name(m->i915, i),
674 				   error->engine[i].context.comm,
675 				   error->engine[i].context.pid,
676 				   error->engine[i].context.ban_score,
677 				   bannable(&error->engine[i].context));
678 		}
679 	}
680 	err_printf(m, "Reset count: %u\n", error->reset_count);
681 	err_printf(m, "Suspend count: %u\n", error->suspend_count);
682 	err_printf(m, "Platform: %s\n", intel_platform_name(error->device_info.platform));
683 	err_print_pciid(m, error->i915);
684 
685 	err_printf(m, "IOMMU enabled?: %d\n", error->iommu);
686 
687 	if (HAS_CSR(dev_priv)) {
688 		struct intel_csr *csr = &dev_priv->csr;
689 
690 		err_printf(m, "DMC loaded: %s\n",
691 			   yesno(csr->dmc_payload != NULL));
692 		err_printf(m, "DMC fw version: %d.%d\n",
693 			   CSR_VERSION_MAJOR(csr->version),
694 			   CSR_VERSION_MINOR(csr->version));
695 	}
696 
697 	err_printf(m, "GT awake: %s\n", yesno(error->awake));
698 	err_printf(m, "RPM wakelock: %s\n", yesno(error->wakelock));
699 	err_printf(m, "PM suspended: %s\n", yesno(error->suspended));
700 	err_printf(m, "EIR: 0x%08x\n", error->eir);
701 	err_printf(m, "IER: 0x%08x\n", error->ier);
702 	for (i = 0; i < error->ngtier; i++)
703 		err_printf(m, "GTIER[%d]: 0x%08x\n", i, error->gtier[i]);
704 	err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
705 	err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
706 	err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
707 	err_printf(m, "CCID: 0x%08x\n", error->ccid);
708 	err_printf(m, "Missed interrupts: 0x%08lx\n", dev_priv->gpu_error.missed_irq_rings);
709 
710 	for (i = 0; i < error->nfence; i++)
711 		err_printf(m, "  fence[%d] = %08llx\n", i, error->fence[i]);
712 
713 	if (INTEL_GEN(dev_priv) >= 6) {
714 		err_printf(m, "ERROR: 0x%08x\n", error->error);
715 
716 		if (INTEL_GEN(dev_priv) >= 8)
717 			err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n",
718 				   error->fault_data1, error->fault_data0);
719 
720 		err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
721 	}
722 
723 	if (IS_GEN7(dev_priv))
724 		err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
725 
726 	for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
727 		if (error->engine[i].engine_id != -1)
728 			error_print_engine(m, &error->engine[i], error->epoch);
729 	}
730 
731 	for (i = 0; i < ARRAY_SIZE(error->active_vm); i++) {
732 		char buf[128];
733 		int len, first = 1;
734 
735 		if (!error->active_vm[i])
736 			break;
737 
738 		len = scnprintf(buf, sizeof(buf), "Active (");
739 		for (j = 0; j < ARRAY_SIZE(error->engine); j++) {
740 			if (error->engine[j].vm != error->active_vm[i])
741 				continue;
742 
743 			len += scnprintf(buf + len, sizeof(buf), "%s%s",
744 					 first ? "" : ", ",
745 					 dev_priv->engine[j]->name);
746 			first = 0;
747 		}
748 		scnprintf(buf + len, sizeof(buf), ")");
749 		print_error_buffers(m, buf,
750 				    error->active_bo[i],
751 				    error->active_bo_count[i]);
752 	}
753 
754 	print_error_buffers(m, "Pinned (global)",
755 			    error->pinned_bo,
756 			    error->pinned_bo_count);
757 
758 	for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
759 		const struct drm_i915_error_engine *ee = &error->engine[i];
760 
761 		obj = ee->batchbuffer;
762 		if (obj) {
763 			err_puts(m, dev_priv->engine[i]->name);
764 			if (ee->context.pid)
765 				err_printf(m, " (submitted by %s [%d], ctx %d [%d], score %d%s)",
766 					   ee->context.comm,
767 					   ee->context.pid,
768 					   ee->context.handle,
769 					   ee->context.hw_id,
770 					   ee->context.ban_score,
771 					   bannable(&ee->context));
772 			err_printf(m, " --- gtt_offset = 0x%08x %08x\n",
773 				   upper_32_bits(obj->gtt_offset),
774 				   lower_32_bits(obj->gtt_offset));
775 			print_error_obj(m, dev_priv->engine[i], NULL, obj);
776 		}
777 
778 		for (j = 0; j < ee->user_bo_count; j++)
779 			print_error_obj(m, dev_priv->engine[i],
780 					"user", ee->user_bo[j]);
781 
782 		if (ee->num_requests) {
783 			err_printf(m, "%s --- %d requests\n",
784 				   dev_priv->engine[i]->name,
785 				   ee->num_requests);
786 			for (j = 0; j < ee->num_requests; j++)
787 				error_print_request(m, " ",
788 						    &ee->requests[j],
789 						    error->epoch);
790 		}
791 
792 		if (IS_ERR(ee->waiters)) {
793 			err_printf(m, "%s --- ? waiters [unable to acquire spinlock]\n",
794 				   dev_priv->engine[i]->name);
795 		} else if (ee->num_waiters) {
796 			err_printf(m, "%s --- %d waiters\n",
797 				   dev_priv->engine[i]->name,
798 				   ee->num_waiters);
799 			for (j = 0; j < ee->num_waiters; j++) {
800 				err_printf(m, " seqno 0x%08x for %s [%d]\n",
801 					   ee->waiters[j].seqno,
802 					   ee->waiters[j].comm,
803 					   ee->waiters[j].pid);
804 			}
805 		}
806 
807 		print_error_obj(m, dev_priv->engine[i],
808 				"ringbuffer", ee->ringbuffer);
809 
810 		print_error_obj(m, dev_priv->engine[i],
811 				"HW Status", ee->hws_page);
812 
813 		print_error_obj(m, dev_priv->engine[i],
814 				"HW context", ee->ctx);
815 
816 		print_error_obj(m, dev_priv->engine[i],
817 				"WA context", ee->wa_ctx);
818 
819 		print_error_obj(m, dev_priv->engine[i],
820 				"WA batchbuffer", ee->wa_batchbuffer);
821 
822 		print_error_obj(m, dev_priv->engine[i],
823 				"NULL context", ee->default_state);
824 	}
825 
826 	if (error->overlay)
827 		intel_overlay_print_error_state(m, error->overlay);
828 
829 	if (error->display)
830 		intel_display_print_error_state(m, error->display);
831 
832 	err_print_capabilities(m, &error->device_info, &error->driver_caps);
833 	err_print_params(m, &error->params);
834 	err_print_uc(m, &error->uc);
835 
836 	if (m->bytes == 0 && m->err)
837 		return m->err;
838 
839 	return 0;
840 }
841 
842 int i915_error_state_buf_init(struct drm_i915_error_state_buf *ebuf,
843 			      struct drm_i915_private *i915,
844 			      size_t count, loff_t pos)
845 {
846 	memset(ebuf, 0, sizeof(*ebuf));
847 	ebuf->i915 = i915;
848 
849 	/* We need to have enough room to store any i915_error_state printf
850 	 * so that we can move it to start position.
851 	 */
852 	ebuf->size = count + 1 > PAGE_SIZE ? count + 1 : PAGE_SIZE;
853 	ebuf->buf = kmalloc(ebuf->size,
854 				GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
855 
856 	if (ebuf->buf == NULL) {
857 		ebuf->size = PAGE_SIZE;
858 		ebuf->buf = kmalloc(ebuf->size, GFP_KERNEL);
859 	}
860 
861 	if (ebuf->buf == NULL) {
862 		ebuf->size = 128;
863 		ebuf->buf = kmalloc(ebuf->size, GFP_KERNEL);
864 	}
865 
866 	if (ebuf->buf == NULL)
867 		return -ENOMEM;
868 
869 	ebuf->start = pos;
870 
871 	return 0;
872 }
873 
874 static void i915_error_object_free(struct drm_i915_error_object *obj)
875 {
876 	int page;
877 
878 	if (obj == NULL)
879 		return;
880 
881 	for (page = 0; page < obj->page_count; page++)
882 		free_page((unsigned long)obj->pages[page]);
883 
884 	kfree(obj);
885 }
886 
887 static __always_inline void free_param(const char *type, void *x)
888 {
889 	if (!__builtin_strcmp(type, "char *"))
890 		kfree(*(void **)x);
891 }
892 
893 static void cleanup_params(struct i915_gpu_state *error)
894 {
895 #define FREE(T, x, ...) free_param(#T, &error->params.x);
896 	I915_PARAMS_FOR_EACH(FREE);
897 #undef FREE
898 }
899 
900 static void cleanup_uc_state(struct i915_gpu_state *error)
901 {
902 	struct i915_error_uc *error_uc = &error->uc;
903 
904 	kfree(error_uc->guc_fw.path);
905 	kfree(error_uc->huc_fw.path);
906 	i915_error_object_free(error_uc->guc_log);
907 }
908 
909 void __i915_gpu_state_free(struct kref *error_ref)
910 {
911 	struct i915_gpu_state *error =
912 		container_of(error_ref, typeof(*error), ref);
913 	long i, j;
914 
915 	for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
916 		struct drm_i915_error_engine *ee = &error->engine[i];
917 
918 		for (j = 0; j < ee->user_bo_count; j++)
919 			i915_error_object_free(ee->user_bo[j]);
920 		kfree(ee->user_bo);
921 
922 		i915_error_object_free(ee->batchbuffer);
923 		i915_error_object_free(ee->wa_batchbuffer);
924 		i915_error_object_free(ee->ringbuffer);
925 		i915_error_object_free(ee->hws_page);
926 		i915_error_object_free(ee->ctx);
927 		i915_error_object_free(ee->wa_ctx);
928 
929 		kfree(ee->requests);
930 		if (!IS_ERR_OR_NULL(ee->waiters))
931 			kfree(ee->waiters);
932 	}
933 
934 	for (i = 0; i < ARRAY_SIZE(error->active_bo); i++)
935 		kfree(error->active_bo[i]);
936 	kfree(error->pinned_bo);
937 
938 	kfree(error->overlay);
939 	kfree(error->display);
940 
941 	cleanup_params(error);
942 	cleanup_uc_state(error);
943 
944 	kfree(error);
945 }
946 
947 static struct drm_i915_error_object *
948 i915_error_object_create(struct drm_i915_private *i915,
949 			 struct i915_vma *vma)
950 {
951 	struct i915_ggtt *ggtt = &i915->ggtt;
952 	const u64 slot = ggtt->error_capture.start;
953 	struct drm_i915_error_object *dst;
954 	struct compress compress;
955 	unsigned long num_pages;
956 	struct sgt_iter iter;
957 	dma_addr_t dma;
958 	int ret;
959 
960 	if (!vma)
961 		return NULL;
962 
963 	num_pages = min_t(u64, vma->size, vma->obj->base.size) >> PAGE_SHIFT;
964 	num_pages = DIV_ROUND_UP(10 * num_pages, 8); /* worstcase zlib growth */
965 	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *),
966 		      GFP_ATOMIC | __GFP_NOWARN);
967 	if (!dst)
968 		return NULL;
969 
970 	dst->gtt_offset = vma->node.start;
971 	dst->gtt_size = vma->node.size;
972 	dst->num_pages = num_pages;
973 	dst->page_count = 0;
974 	dst->unused = 0;
975 
976 	if (!compress_init(&compress)) {
977 		kfree(dst);
978 		return NULL;
979 	}
980 
981 	ret = -EINVAL;
982 	for_each_sgt_dma(dma, iter, vma->pages) {
983 		void __iomem *s;
984 
985 		ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0);
986 
987 		s = io_mapping_map_atomic_wc(&ggtt->iomap, slot);
988 		ret = compress_page(&compress, (void  __force *)s, dst);
989 		io_mapping_unmap_atomic(s);
990 		if (ret)
991 			break;
992 	}
993 
994 	if (ret || compress_flush(&compress, dst)) {
995 		while (dst->page_count--)
996 			free_page((unsigned long)dst->pages[dst->page_count]);
997 		kfree(dst);
998 		dst = NULL;
999 	}
1000 
1001 	compress_fini(&compress, dst);
1002 	return dst;
1003 }
1004 
1005 /* The error capture is special as tries to run underneath the normal
1006  * locking rules - so we use the raw version of the i915_gem_active lookup.
1007  */
1008 static inline uint32_t
1009 __active_get_seqno(struct i915_gem_active *active)
1010 {
1011 	struct i915_request *request;
1012 
1013 	request = __i915_gem_active_peek(active);
1014 	return request ? request->global_seqno : 0;
1015 }
1016 
1017 static inline int
1018 __active_get_engine_id(struct i915_gem_active *active)
1019 {
1020 	struct i915_request *request;
1021 
1022 	request = __i915_gem_active_peek(active);
1023 	return request ? request->engine->id : -1;
1024 }
1025 
1026 static void capture_bo(struct drm_i915_error_buffer *err,
1027 		       struct i915_vma *vma)
1028 {
1029 	struct drm_i915_gem_object *obj = vma->obj;
1030 
1031 	err->size = obj->base.size;
1032 	err->name = obj->base.name;
1033 
1034 	err->wseqno = __active_get_seqno(&obj->frontbuffer_write);
1035 	err->engine = __active_get_engine_id(&obj->frontbuffer_write);
1036 
1037 	err->gtt_offset = vma->node.start;
1038 	err->read_domains = obj->read_domains;
1039 	err->write_domain = obj->write_domain;
1040 	err->fence_reg = vma->fence ? vma->fence->id : -1;
1041 	err->tiling = i915_gem_object_get_tiling(obj);
1042 	err->dirty = obj->mm.dirty;
1043 	err->purgeable = obj->mm.madv != I915_MADV_WILLNEED;
1044 	err->userptr = obj->userptr.mm != NULL;
1045 	err->cache_level = obj->cache_level;
1046 }
1047 
1048 static u32 capture_error_bo(struct drm_i915_error_buffer *err,
1049 			    int count, struct list_head *head,
1050 			    bool pinned_only)
1051 {
1052 	struct i915_vma *vma;
1053 	int i = 0;
1054 
1055 	list_for_each_entry(vma, head, vm_link) {
1056 		if (!vma->obj)
1057 			continue;
1058 
1059 		if (pinned_only && !i915_vma_is_pinned(vma))
1060 			continue;
1061 
1062 		capture_bo(err++, vma);
1063 		if (++i == count)
1064 			break;
1065 	}
1066 
1067 	return i;
1068 }
1069 
1070 /* Generate a semi-unique error code. The code is not meant to have meaning, The
1071  * code's only purpose is to try to prevent false duplicated bug reports by
1072  * grossly estimating a GPU error state.
1073  *
1074  * TODO Ideally, hashing the batchbuffer would be a very nice way to determine
1075  * the hang if we could strip the GTT offset information from it.
1076  *
1077  * It's only a small step better than a random number in its current form.
1078  */
1079 static uint32_t i915_error_generate_code(struct drm_i915_private *dev_priv,
1080 					 struct i915_gpu_state *error,
1081 					 int *engine_id)
1082 {
1083 	uint32_t error_code = 0;
1084 	int i;
1085 
1086 	/* IPEHR would be an ideal way to detect errors, as it's the gross
1087 	 * measure of "the command that hung." However, has some very common
1088 	 * synchronization commands which almost always appear in the case
1089 	 * strictly a client bug. Use instdone to differentiate those some.
1090 	 */
1091 	for (i = 0; i < I915_NUM_ENGINES; i++) {
1092 		if (error->engine[i].hangcheck_stalled) {
1093 			if (engine_id)
1094 				*engine_id = i;
1095 
1096 			return error->engine[i].ipehr ^
1097 			       error->engine[i].instdone.instdone;
1098 		}
1099 	}
1100 
1101 	return error_code;
1102 }
1103 
1104 static void gem_record_fences(struct i915_gpu_state *error)
1105 {
1106 	struct drm_i915_private *dev_priv = error->i915;
1107 	int i;
1108 
1109 	if (INTEL_GEN(dev_priv) >= 6) {
1110 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1111 			error->fence[i] = I915_READ64(FENCE_REG_GEN6_LO(i));
1112 	} else if (INTEL_GEN(dev_priv) >= 4) {
1113 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1114 			error->fence[i] = I915_READ64(FENCE_REG_965_LO(i));
1115 	} else {
1116 		for (i = 0; i < dev_priv->num_fence_regs; i++)
1117 			error->fence[i] = I915_READ(FENCE_REG(i));
1118 	}
1119 	error->nfence = i;
1120 }
1121 
1122 static void gen6_record_semaphore_state(struct intel_engine_cs *engine,
1123 					struct drm_i915_error_engine *ee)
1124 {
1125 	struct drm_i915_private *dev_priv = engine->i915;
1126 
1127 	ee->semaphore_mboxes[0] = I915_READ(RING_SYNC_0(engine->mmio_base));
1128 	ee->semaphore_mboxes[1] = I915_READ(RING_SYNC_1(engine->mmio_base));
1129 	if (HAS_VEBOX(dev_priv))
1130 		ee->semaphore_mboxes[2] =
1131 			I915_READ(RING_SYNC_2(engine->mmio_base));
1132 }
1133 
1134 static void error_record_engine_waiters(struct intel_engine_cs *engine,
1135 					struct drm_i915_error_engine *ee)
1136 {
1137 	struct intel_breadcrumbs *b = &engine->breadcrumbs;
1138 	struct drm_i915_error_waiter *waiter;
1139 	struct rb_node *rb;
1140 	int count;
1141 
1142 	ee->num_waiters = 0;
1143 	ee->waiters = NULL;
1144 
1145 	if (RB_EMPTY_ROOT(&b->waiters))
1146 		return;
1147 
1148 	if (!spin_trylock_irq(&b->rb_lock)) {
1149 		ee->waiters = ERR_PTR(-EDEADLK);
1150 		return;
1151 	}
1152 
1153 	count = 0;
1154 	for (rb = rb_first(&b->waiters); rb != NULL; rb = rb_next(rb))
1155 		count++;
1156 	spin_unlock_irq(&b->rb_lock);
1157 
1158 	waiter = NULL;
1159 	if (count)
1160 		waiter = kmalloc_array(count,
1161 				       sizeof(struct drm_i915_error_waiter),
1162 				       GFP_ATOMIC);
1163 	if (!waiter)
1164 		return;
1165 
1166 	if (!spin_trylock_irq(&b->rb_lock)) {
1167 		kfree(waiter);
1168 		ee->waiters = ERR_PTR(-EDEADLK);
1169 		return;
1170 	}
1171 
1172 	ee->waiters = waiter;
1173 	for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
1174 		struct intel_wait *w = rb_entry(rb, typeof(*w), node);
1175 
1176 		strcpy(waiter->comm, w->tsk->comm);
1177 		waiter->pid = w->tsk->pid;
1178 		waiter->seqno = w->seqno;
1179 		waiter++;
1180 
1181 		if (++ee->num_waiters == count)
1182 			break;
1183 	}
1184 	spin_unlock_irq(&b->rb_lock);
1185 }
1186 
1187 static void error_record_engine_registers(struct i915_gpu_state *error,
1188 					  struct intel_engine_cs *engine,
1189 					  struct drm_i915_error_engine *ee)
1190 {
1191 	struct drm_i915_private *dev_priv = engine->i915;
1192 
1193 	if (INTEL_GEN(dev_priv) >= 6) {
1194 		ee->rc_psmi = I915_READ(RING_PSMI_CTL(engine->mmio_base));
1195 		if (INTEL_GEN(dev_priv) >= 8) {
1196 			ee->fault_reg = I915_READ(GEN8_RING_FAULT_REG);
1197 		} else {
1198 			gen6_record_semaphore_state(engine, ee);
1199 			ee->fault_reg = I915_READ(RING_FAULT_REG(engine));
1200 		}
1201 	}
1202 
1203 	if (INTEL_GEN(dev_priv) >= 4) {
1204 		ee->faddr = I915_READ(RING_DMA_FADD(engine->mmio_base));
1205 		ee->ipeir = I915_READ(RING_IPEIR(engine->mmio_base));
1206 		ee->ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
1207 		ee->instps = I915_READ(RING_INSTPS(engine->mmio_base));
1208 		ee->bbaddr = I915_READ(RING_BBADDR(engine->mmio_base));
1209 		if (INTEL_GEN(dev_priv) >= 8) {
1210 			ee->faddr |= (u64) I915_READ(RING_DMA_FADD_UDW(engine->mmio_base)) << 32;
1211 			ee->bbaddr |= (u64) I915_READ(RING_BBADDR_UDW(engine->mmio_base)) << 32;
1212 		}
1213 		ee->bbstate = I915_READ(RING_BBSTATE(engine->mmio_base));
1214 	} else {
1215 		ee->faddr = I915_READ(DMA_FADD_I8XX);
1216 		ee->ipeir = I915_READ(IPEIR);
1217 		ee->ipehr = I915_READ(IPEHR);
1218 	}
1219 
1220 	intel_engine_get_instdone(engine, &ee->instdone);
1221 
1222 	ee->waiting = intel_engine_has_waiter(engine);
1223 	ee->instpm = I915_READ(RING_INSTPM(engine->mmio_base));
1224 	ee->acthd = intel_engine_get_active_head(engine);
1225 	ee->seqno = intel_engine_get_seqno(engine);
1226 	ee->last_seqno = intel_engine_last_submit(engine);
1227 	ee->start = I915_READ_START(engine);
1228 	ee->head = I915_READ_HEAD(engine);
1229 	ee->tail = I915_READ_TAIL(engine);
1230 	ee->ctl = I915_READ_CTL(engine);
1231 	if (INTEL_GEN(dev_priv) > 2)
1232 		ee->mode = I915_READ_MODE(engine);
1233 
1234 	if (!HWS_NEEDS_PHYSICAL(dev_priv)) {
1235 		i915_reg_t mmio;
1236 
1237 		if (IS_GEN7(dev_priv)) {
1238 			switch (engine->id) {
1239 			default:
1240 			case RCS:
1241 				mmio = RENDER_HWS_PGA_GEN7;
1242 				break;
1243 			case BCS:
1244 				mmio = BLT_HWS_PGA_GEN7;
1245 				break;
1246 			case VCS:
1247 				mmio = BSD_HWS_PGA_GEN7;
1248 				break;
1249 			case VECS:
1250 				mmio = VEBOX_HWS_PGA_GEN7;
1251 				break;
1252 			}
1253 		} else if (IS_GEN6(engine->i915)) {
1254 			mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
1255 		} else {
1256 			/* XXX: gen8 returns to sanity */
1257 			mmio = RING_HWS_PGA(engine->mmio_base);
1258 		}
1259 
1260 		ee->hws = I915_READ(mmio);
1261 	}
1262 
1263 	ee->idle = intel_engine_is_idle(engine);
1264 	ee->hangcheck_timestamp = engine->hangcheck.action_timestamp;
1265 	ee->hangcheck_action = engine->hangcheck.action;
1266 	ee->hangcheck_stalled = engine->hangcheck.stalled;
1267 	ee->reset_count = i915_reset_engine_count(&dev_priv->gpu_error,
1268 						  engine);
1269 
1270 	if (HAS_PPGTT(dev_priv)) {
1271 		int i;
1272 
1273 		ee->vm_info.gfx_mode = I915_READ(RING_MODE_GEN7(engine));
1274 
1275 		if (IS_GEN6(dev_priv))
1276 			ee->vm_info.pp_dir_base =
1277 				I915_READ(RING_PP_DIR_BASE_READ(engine));
1278 		else if (IS_GEN7(dev_priv))
1279 			ee->vm_info.pp_dir_base =
1280 				I915_READ(RING_PP_DIR_BASE(engine));
1281 		else if (INTEL_GEN(dev_priv) >= 8)
1282 			for (i = 0; i < 4; i++) {
1283 				ee->vm_info.pdp[i] =
1284 					I915_READ(GEN8_RING_PDP_UDW(engine, i));
1285 				ee->vm_info.pdp[i] <<= 32;
1286 				ee->vm_info.pdp[i] |=
1287 					I915_READ(GEN8_RING_PDP_LDW(engine, i));
1288 			}
1289 	}
1290 }
1291 
1292 static void record_request(struct i915_request *request,
1293 			   struct drm_i915_error_request *erq)
1294 {
1295 	struct i915_gem_context *ctx = request->gem_context;
1296 
1297 	erq->context = ctx->hw_id;
1298 	erq->sched_attr = request->sched.attr;
1299 	erq->ban_score = atomic_read(&ctx->ban_score);
1300 	erq->seqno = request->global_seqno;
1301 	erq->jiffies = request->emitted_jiffies;
1302 	erq->start = i915_ggtt_offset(request->ring->vma);
1303 	erq->head = request->head;
1304 	erq->tail = request->tail;
1305 
1306 	rcu_read_lock();
1307 	erq->pid = ctx->pid ? pid_nr(ctx->pid) : 0;
1308 	rcu_read_unlock();
1309 }
1310 
1311 static void engine_record_requests(struct intel_engine_cs *engine,
1312 				   struct i915_request *first,
1313 				   struct drm_i915_error_engine *ee)
1314 {
1315 	struct i915_request *request;
1316 	int count;
1317 
1318 	count = 0;
1319 	request = first;
1320 	list_for_each_entry_from(request, &engine->timeline.requests, link)
1321 		count++;
1322 	if (!count)
1323 		return;
1324 
1325 	ee->requests = kcalloc(count, sizeof(*ee->requests), GFP_ATOMIC);
1326 	if (!ee->requests)
1327 		return;
1328 
1329 	ee->num_requests = count;
1330 
1331 	count = 0;
1332 	request = first;
1333 	list_for_each_entry_from(request, &engine->timeline.requests, link) {
1334 		if (count >= ee->num_requests) {
1335 			/*
1336 			 * If the ring request list was changed in
1337 			 * between the point where the error request
1338 			 * list was created and dimensioned and this
1339 			 * point then just exit early to avoid crashes.
1340 			 *
1341 			 * We don't need to communicate that the
1342 			 * request list changed state during error
1343 			 * state capture and that the error state is
1344 			 * slightly incorrect as a consequence since we
1345 			 * are typically only interested in the request
1346 			 * list state at the point of error state
1347 			 * capture, not in any changes happening during
1348 			 * the capture.
1349 			 */
1350 			break;
1351 		}
1352 
1353 		record_request(request, &ee->requests[count++]);
1354 	}
1355 	ee->num_requests = count;
1356 }
1357 
1358 static void error_record_engine_execlists(struct intel_engine_cs *engine,
1359 					  struct drm_i915_error_engine *ee)
1360 {
1361 	const struct intel_engine_execlists * const execlists = &engine->execlists;
1362 	unsigned int n;
1363 
1364 	for (n = 0; n < execlists_num_ports(execlists); n++) {
1365 		struct i915_request *rq = port_request(&execlists->port[n]);
1366 
1367 		if (!rq)
1368 			break;
1369 
1370 		record_request(rq, &ee->execlist[n]);
1371 	}
1372 
1373 	ee->num_ports = n;
1374 }
1375 
1376 static void record_context(struct drm_i915_error_context *e,
1377 			   struct i915_gem_context *ctx)
1378 {
1379 	if (ctx->pid) {
1380 		struct task_struct *task;
1381 
1382 		rcu_read_lock();
1383 		task = pid_task(ctx->pid, PIDTYPE_PID);
1384 		if (task) {
1385 			strcpy(e->comm, task->comm);
1386 			e->pid = task->pid;
1387 		}
1388 		rcu_read_unlock();
1389 	}
1390 
1391 	e->handle = ctx->user_handle;
1392 	e->hw_id = ctx->hw_id;
1393 	e->sched_attr = ctx->sched;
1394 	e->ban_score = atomic_read(&ctx->ban_score);
1395 	e->bannable = i915_gem_context_is_bannable(ctx);
1396 	e->guilty = atomic_read(&ctx->guilty_count);
1397 	e->active = atomic_read(&ctx->active_count);
1398 }
1399 
1400 static void request_record_user_bo(struct i915_request *request,
1401 				   struct drm_i915_error_engine *ee)
1402 {
1403 	struct i915_capture_list *c;
1404 	struct drm_i915_error_object **bo;
1405 	long count, max;
1406 
1407 	max = 0;
1408 	for (c = request->capture_list; c; c = c->next)
1409 		max++;
1410 	if (!max)
1411 		return;
1412 
1413 	bo = kmalloc_array(max, sizeof(*bo), GFP_ATOMIC);
1414 	if (!bo) {
1415 		/* If we can't capture everything, try to capture something. */
1416 		max = min_t(long, max, PAGE_SIZE / sizeof(*bo));
1417 		bo = kmalloc_array(max, sizeof(*bo), GFP_ATOMIC);
1418 	}
1419 	if (!bo)
1420 		return;
1421 
1422 	count = 0;
1423 	for (c = request->capture_list; c; c = c->next) {
1424 		bo[count] = i915_error_object_create(request->i915, c->vma);
1425 		if (!bo[count])
1426 			break;
1427 		if (++count == max)
1428 			break;
1429 	}
1430 
1431 	ee->user_bo = bo;
1432 	ee->user_bo_count = count;
1433 }
1434 
1435 static struct drm_i915_error_object *
1436 capture_object(struct drm_i915_private *dev_priv,
1437 	       struct drm_i915_gem_object *obj)
1438 {
1439 	if (obj && i915_gem_object_has_pages(obj)) {
1440 		struct i915_vma fake = {
1441 			.node = { .start = U64_MAX, .size = obj->base.size },
1442 			.size = obj->base.size,
1443 			.pages = obj->mm.pages,
1444 			.obj = obj,
1445 		};
1446 
1447 		return i915_error_object_create(dev_priv, &fake);
1448 	} else {
1449 		return NULL;
1450 	}
1451 }
1452 
1453 static void gem_record_rings(struct i915_gpu_state *error)
1454 {
1455 	struct drm_i915_private *i915 = error->i915;
1456 	struct i915_ggtt *ggtt = &i915->ggtt;
1457 	int i;
1458 
1459 	for (i = 0; i < I915_NUM_ENGINES; i++) {
1460 		struct intel_engine_cs *engine = i915->engine[i];
1461 		struct drm_i915_error_engine *ee = &error->engine[i];
1462 		struct i915_request *request;
1463 
1464 		ee->engine_id = -1;
1465 
1466 		if (!engine)
1467 			continue;
1468 
1469 		ee->engine_id = i;
1470 
1471 		error_record_engine_registers(error, engine, ee);
1472 		error_record_engine_waiters(engine, ee);
1473 		error_record_engine_execlists(engine, ee);
1474 
1475 		request = i915_gem_find_active_request(engine);
1476 		if (request) {
1477 			struct i915_gem_context *ctx = request->gem_context;
1478 			struct intel_ring *ring;
1479 
1480 			ee->vm = ctx->ppgtt ? &ctx->ppgtt->vm : &ggtt->vm;
1481 
1482 			record_context(&ee->context, ctx);
1483 
1484 			/* We need to copy these to an anonymous buffer
1485 			 * as the simplest method to avoid being overwritten
1486 			 * by userspace.
1487 			 */
1488 			ee->batchbuffer =
1489 				i915_error_object_create(i915, request->batch);
1490 
1491 			if (HAS_BROKEN_CS_TLB(i915))
1492 				ee->wa_batchbuffer =
1493 					i915_error_object_create(i915,
1494 								 engine->scratch);
1495 			request_record_user_bo(request, ee);
1496 
1497 			ee->ctx =
1498 				i915_error_object_create(i915,
1499 							 request->hw_context->state);
1500 
1501 			error->simulated |=
1502 				i915_gem_context_no_error_capture(ctx);
1503 
1504 			ee->rq_head = request->head;
1505 			ee->rq_post = request->postfix;
1506 			ee->rq_tail = request->tail;
1507 
1508 			ring = request->ring;
1509 			ee->cpu_ring_head = ring->head;
1510 			ee->cpu_ring_tail = ring->tail;
1511 			ee->ringbuffer =
1512 				i915_error_object_create(i915, ring->vma);
1513 
1514 			engine_record_requests(engine, request, ee);
1515 		}
1516 
1517 		ee->hws_page =
1518 			i915_error_object_create(i915,
1519 						 engine->status_page.vma);
1520 
1521 		ee->wa_ctx = i915_error_object_create(i915, engine->wa_ctx.vma);
1522 
1523 		ee->default_state = capture_object(i915, engine->default_state);
1524 	}
1525 }
1526 
1527 static void gem_capture_vm(struct i915_gpu_state *error,
1528 			   struct i915_address_space *vm,
1529 			   int idx)
1530 {
1531 	struct drm_i915_error_buffer *active_bo;
1532 	struct i915_vma *vma;
1533 	int count;
1534 
1535 	count = 0;
1536 	list_for_each_entry(vma, &vm->active_list, vm_link)
1537 		count++;
1538 
1539 	active_bo = NULL;
1540 	if (count)
1541 		active_bo = kcalloc(count, sizeof(*active_bo), GFP_ATOMIC);
1542 	if (active_bo)
1543 		count = capture_error_bo(active_bo, count, &vm->active_list, false);
1544 	else
1545 		count = 0;
1546 
1547 	error->active_vm[idx] = vm;
1548 	error->active_bo[idx] = active_bo;
1549 	error->active_bo_count[idx] = count;
1550 }
1551 
1552 static void capture_active_buffers(struct i915_gpu_state *error)
1553 {
1554 	int cnt = 0, i, j;
1555 
1556 	BUILD_BUG_ON(ARRAY_SIZE(error->engine) > ARRAY_SIZE(error->active_bo));
1557 	BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_vm));
1558 	BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_bo_count));
1559 
1560 	/* Scan each engine looking for unique active contexts/vm */
1561 	for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
1562 		struct drm_i915_error_engine *ee = &error->engine[i];
1563 		bool found;
1564 
1565 		if (!ee->vm)
1566 			continue;
1567 
1568 		found = false;
1569 		for (j = 0; j < i && !found; j++)
1570 			found = error->engine[j].vm == ee->vm;
1571 		if (!found)
1572 			gem_capture_vm(error, ee->vm, cnt++);
1573 	}
1574 }
1575 
1576 static void capture_pinned_buffers(struct i915_gpu_state *error)
1577 {
1578 	struct i915_address_space *vm = &error->i915->ggtt.vm;
1579 	struct drm_i915_error_buffer *bo;
1580 	struct i915_vma *vma;
1581 	int count_inactive, count_active;
1582 
1583 	count_inactive = 0;
1584 	list_for_each_entry(vma, &vm->inactive_list, vm_link)
1585 		count_inactive++;
1586 
1587 	count_active = 0;
1588 	list_for_each_entry(vma, &vm->active_list, vm_link)
1589 		count_active++;
1590 
1591 	bo = NULL;
1592 	if (count_inactive + count_active)
1593 		bo = kcalloc(count_inactive + count_active,
1594 			     sizeof(*bo), GFP_ATOMIC);
1595 	if (!bo)
1596 		return;
1597 
1598 	count_inactive = capture_error_bo(bo, count_inactive,
1599 					  &vm->active_list, true);
1600 	count_active = capture_error_bo(bo + count_inactive, count_active,
1601 					&vm->inactive_list, true);
1602 	error->pinned_bo_count = count_inactive + count_active;
1603 	error->pinned_bo = bo;
1604 }
1605 
1606 static void capture_uc_state(struct i915_gpu_state *error)
1607 {
1608 	struct drm_i915_private *i915 = error->i915;
1609 	struct i915_error_uc *error_uc = &error->uc;
1610 
1611 	/* Capturing uC state won't be useful if there is no GuC */
1612 	if (!error->device_info.has_guc)
1613 		return;
1614 
1615 	error_uc->guc_fw = i915->guc.fw;
1616 	error_uc->huc_fw = i915->huc.fw;
1617 
1618 	/* Non-default firmware paths will be specified by the modparam.
1619 	 * As modparams are generally accesible from the userspace make
1620 	 * explicit copies of the firmware paths.
1621 	 */
1622 	error_uc->guc_fw.path = kstrdup(i915->guc.fw.path, GFP_ATOMIC);
1623 	error_uc->huc_fw.path = kstrdup(i915->huc.fw.path, GFP_ATOMIC);
1624 	error_uc->guc_log = i915_error_object_create(i915, i915->guc.log.vma);
1625 }
1626 
1627 /* Capture all registers which don't fit into another category. */
1628 static void capture_reg_state(struct i915_gpu_state *error)
1629 {
1630 	struct drm_i915_private *dev_priv = error->i915;
1631 	int i;
1632 
1633 	/* General organization
1634 	 * 1. Registers specific to a single generation
1635 	 * 2. Registers which belong to multiple generations
1636 	 * 3. Feature specific registers.
1637 	 * 4. Everything else
1638 	 * Please try to follow the order.
1639 	 */
1640 
1641 	/* 1: Registers specific to a single generation */
1642 	if (IS_VALLEYVIEW(dev_priv)) {
1643 		error->gtier[0] = I915_READ(GTIER);
1644 		error->ier = I915_READ(VLV_IER);
1645 		error->forcewake = I915_READ_FW(FORCEWAKE_VLV);
1646 	}
1647 
1648 	if (IS_GEN7(dev_priv))
1649 		error->err_int = I915_READ(GEN7_ERR_INT);
1650 
1651 	if (INTEL_GEN(dev_priv) >= 8) {
1652 		error->fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
1653 		error->fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
1654 	}
1655 
1656 	if (IS_GEN6(dev_priv)) {
1657 		error->forcewake = I915_READ_FW(FORCEWAKE);
1658 		error->gab_ctl = I915_READ(GAB_CTL);
1659 		error->gfx_mode = I915_READ(GFX_MODE);
1660 	}
1661 
1662 	/* 2: Registers which belong to multiple generations */
1663 	if (INTEL_GEN(dev_priv) >= 7)
1664 		error->forcewake = I915_READ_FW(FORCEWAKE_MT);
1665 
1666 	if (INTEL_GEN(dev_priv) >= 6) {
1667 		error->derrmr = I915_READ(DERRMR);
1668 		error->error = I915_READ(ERROR_GEN6);
1669 		error->done_reg = I915_READ(DONE_REG);
1670 	}
1671 
1672 	if (INTEL_GEN(dev_priv) >= 5)
1673 		error->ccid = I915_READ(CCID);
1674 
1675 	/* 3: Feature specific registers */
1676 	if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
1677 		error->gam_ecochk = I915_READ(GAM_ECOCHK);
1678 		error->gac_eco = I915_READ(GAC_ECO_BITS);
1679 	}
1680 
1681 	/* 4: Everything else */
1682 	if (INTEL_GEN(dev_priv) >= 11) {
1683 		error->ier = I915_READ(GEN8_DE_MISC_IER);
1684 		error->gtier[0] = I915_READ(GEN11_RENDER_COPY_INTR_ENABLE);
1685 		error->gtier[1] = I915_READ(GEN11_VCS_VECS_INTR_ENABLE);
1686 		error->gtier[2] = I915_READ(GEN11_GUC_SG_INTR_ENABLE);
1687 		error->gtier[3] = I915_READ(GEN11_GPM_WGBOXPERF_INTR_ENABLE);
1688 		error->gtier[4] = I915_READ(GEN11_CRYPTO_RSVD_INTR_ENABLE);
1689 		error->gtier[5] = I915_READ(GEN11_GUNIT_CSME_INTR_ENABLE);
1690 		error->ngtier = 6;
1691 	} else if (INTEL_GEN(dev_priv) >= 8) {
1692 		error->ier = I915_READ(GEN8_DE_MISC_IER);
1693 		for (i = 0; i < 4; i++)
1694 			error->gtier[i] = I915_READ(GEN8_GT_IER(i));
1695 		error->ngtier = 4;
1696 	} else if (HAS_PCH_SPLIT(dev_priv)) {
1697 		error->ier = I915_READ(DEIER);
1698 		error->gtier[0] = I915_READ(GTIER);
1699 		error->ngtier = 1;
1700 	} else if (IS_GEN2(dev_priv)) {
1701 		error->ier = I915_READ16(IER);
1702 	} else if (!IS_VALLEYVIEW(dev_priv)) {
1703 		error->ier = I915_READ(IER);
1704 	}
1705 	error->eir = I915_READ(EIR);
1706 	error->pgtbl_er = I915_READ(PGTBL_ER);
1707 }
1708 
1709 static void i915_error_capture_msg(struct drm_i915_private *dev_priv,
1710 				   struct i915_gpu_state *error,
1711 				   u32 engine_mask,
1712 				   const char *error_msg)
1713 {
1714 	u32 ecode;
1715 	int engine_id = -1, len;
1716 
1717 	ecode = i915_error_generate_code(dev_priv, error, &engine_id);
1718 
1719 	len = scnprintf(error->error_msg, sizeof(error->error_msg),
1720 			"GPU HANG: ecode %d:%d:0x%08x",
1721 			INTEL_GEN(dev_priv), engine_id, ecode);
1722 
1723 	if (engine_id != -1 && error->engine[engine_id].context.pid)
1724 		len += scnprintf(error->error_msg + len,
1725 				 sizeof(error->error_msg) - len,
1726 				 ", in %s [%d]",
1727 				 error->engine[engine_id].context.comm,
1728 				 error->engine[engine_id].context.pid);
1729 
1730 	scnprintf(error->error_msg + len, sizeof(error->error_msg) - len,
1731 		  ", reason: %s, action: %s",
1732 		  error_msg,
1733 		  engine_mask ? "reset" : "continue");
1734 }
1735 
1736 static void capture_gen_state(struct i915_gpu_state *error)
1737 {
1738 	struct drm_i915_private *i915 = error->i915;
1739 
1740 	error->awake = i915->gt.awake;
1741 	error->wakelock = atomic_read(&i915->runtime_pm.wakeref_count);
1742 	error->suspended = i915->runtime_pm.suspended;
1743 
1744 	error->iommu = -1;
1745 #ifdef CONFIG_INTEL_IOMMU
1746 	error->iommu = intel_iommu_gfx_mapped;
1747 #endif
1748 	error->reset_count = i915_reset_count(&i915->gpu_error);
1749 	error->suspend_count = i915->suspend_count;
1750 
1751 	memcpy(&error->device_info,
1752 	       INTEL_INFO(i915),
1753 	       sizeof(error->device_info));
1754 	error->driver_caps = i915->caps;
1755 }
1756 
1757 static __always_inline void dup_param(const char *type, void *x)
1758 {
1759 	if (!__builtin_strcmp(type, "char *"))
1760 		*(void **)x = kstrdup(*(void **)x, GFP_ATOMIC);
1761 }
1762 
1763 static void capture_params(struct i915_gpu_state *error)
1764 {
1765 	error->params = i915_modparams;
1766 #define DUP(T, x, ...) dup_param(#T, &error->params.x);
1767 	I915_PARAMS_FOR_EACH(DUP);
1768 #undef DUP
1769 }
1770 
1771 static unsigned long capture_find_epoch(const struct i915_gpu_state *error)
1772 {
1773 	unsigned long epoch = error->capture;
1774 	int i;
1775 
1776 	for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
1777 		const struct drm_i915_error_engine *ee = &error->engine[i];
1778 
1779 		if (ee->hangcheck_stalled &&
1780 		    time_before(ee->hangcheck_timestamp, epoch))
1781 			epoch = ee->hangcheck_timestamp;
1782 	}
1783 
1784 	return epoch;
1785 }
1786 
1787 static void capture_finish(struct i915_gpu_state *error)
1788 {
1789 	struct i915_ggtt *ggtt = &error->i915->ggtt;
1790 	const u64 slot = ggtt->error_capture.start;
1791 
1792 	ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
1793 }
1794 
1795 static int capture(void *data)
1796 {
1797 	struct i915_gpu_state *error = data;
1798 
1799 	error->time = ktime_get_real();
1800 	error->boottime = ktime_get_boottime();
1801 	error->uptime = ktime_sub(ktime_get(),
1802 				  error->i915->gt.last_init_time);
1803 	error->capture = jiffies;
1804 
1805 	capture_params(error);
1806 	capture_gen_state(error);
1807 	capture_uc_state(error);
1808 	capture_reg_state(error);
1809 	gem_record_fences(error);
1810 	gem_record_rings(error);
1811 	capture_active_buffers(error);
1812 	capture_pinned_buffers(error);
1813 
1814 	error->overlay = intel_overlay_capture_error_state(error->i915);
1815 	error->display = intel_display_capture_error_state(error->i915);
1816 
1817 	error->epoch = capture_find_epoch(error);
1818 
1819 	capture_finish(error);
1820 	return 0;
1821 }
1822 
1823 #define DAY_AS_SECONDS(x) (24 * 60 * 60 * (x))
1824 
1825 struct i915_gpu_state *
1826 i915_capture_gpu_state(struct drm_i915_private *i915)
1827 {
1828 	struct i915_gpu_state *error;
1829 
1830 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
1831 	if (!error)
1832 		return NULL;
1833 
1834 	kref_init(&error->ref);
1835 	error->i915 = i915;
1836 
1837 	stop_machine(capture, error, NULL);
1838 
1839 	return error;
1840 }
1841 
1842 /**
1843  * i915_capture_error_state - capture an error record for later analysis
1844  * @i915: i915 device
1845  * @engine_mask: the mask of engines triggering the hang
1846  * @error_msg: a message to insert into the error capture header
1847  *
1848  * Should be called when an error is detected (either a hang or an error
1849  * interrupt) to capture error state from the time of the error.  Fills
1850  * out a structure which becomes available in debugfs for user level tools
1851  * to pick up.
1852  */
1853 void i915_capture_error_state(struct drm_i915_private *i915,
1854 			      u32 engine_mask,
1855 			      const char *error_msg)
1856 {
1857 	static bool warned;
1858 	struct i915_gpu_state *error;
1859 	unsigned long flags;
1860 
1861 	if (!i915_modparams.error_capture)
1862 		return;
1863 
1864 	if (READ_ONCE(i915->gpu_error.first_error))
1865 		return;
1866 
1867 	error = i915_capture_gpu_state(i915);
1868 	if (!error) {
1869 		DRM_DEBUG_DRIVER("out of memory, not capturing error state\n");
1870 		return;
1871 	}
1872 
1873 	i915_error_capture_msg(i915, error, engine_mask, error_msg);
1874 	DRM_INFO("%s\n", error->error_msg);
1875 
1876 	if (!error->simulated) {
1877 		spin_lock_irqsave(&i915->gpu_error.lock, flags);
1878 		if (!i915->gpu_error.first_error) {
1879 			i915->gpu_error.first_error = error;
1880 			error = NULL;
1881 		}
1882 		spin_unlock_irqrestore(&i915->gpu_error.lock, flags);
1883 	}
1884 
1885 	if (error) {
1886 		__i915_gpu_state_free(&error->ref);
1887 		return;
1888 	}
1889 
1890 	if (!warned &&
1891 	    ktime_get_real_seconds() - DRIVER_TIMESTAMP < DAY_AS_SECONDS(180)) {
1892 		DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
1893 		DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
1894 		DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
1895 		DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
1896 		DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n",
1897 			 i915->drm.primary->index);
1898 		warned = true;
1899 	}
1900 }
1901 
1902 struct i915_gpu_state *
1903 i915_first_error_state(struct drm_i915_private *i915)
1904 {
1905 	struct i915_gpu_state *error;
1906 
1907 	spin_lock_irq(&i915->gpu_error.lock);
1908 	error = i915->gpu_error.first_error;
1909 	if (error)
1910 		i915_gpu_state_get(error);
1911 	spin_unlock_irq(&i915->gpu_error.lock);
1912 
1913 	return error;
1914 }
1915 
1916 void i915_reset_error_state(struct drm_i915_private *i915)
1917 {
1918 	struct i915_gpu_state *error;
1919 
1920 	spin_lock_irq(&i915->gpu_error.lock);
1921 	error = i915->gpu_error.first_error;
1922 	i915->gpu_error.first_error = NULL;
1923 	spin_unlock_irq(&i915->gpu_error.lock);
1924 
1925 	i915_gpu_state_put(error);
1926 }
1927