2 * Copyright (c) 2008 Intel Corporation
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:
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
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
30 #include <linux/ascii85.h>
31 #include <linux/nmi.h>
32 #include <linux/scatterlist.h>
33 #include <linux/stop_machine.h>
34 #include <linux/utsname.h>
35 #include <linux/zlib.h>
37 #include <drm/drm_print.h>
39 #include "display/intel_atomic.h"
40 #include "display/intel_overlay.h"
42 #include "gem/i915_gem_context.h"
45 #include "i915_gpu_error.h"
46 #include "i915_scatterlist.h"
47 #include "intel_csr.h"
49 static inline const struct intel_engine_cs *
50 engine_lookup(const struct drm_i915_private *i915, unsigned int id)
52 if (id >= I915_NUM_ENGINES)
55 return i915->engine[id];
58 static inline const char *
59 __engine_name(const struct intel_engine_cs *engine)
61 return engine ? engine->name : "";
65 engine_name(const struct drm_i915_private *i915, unsigned int id)
67 return __engine_name(engine_lookup(i915, id));
70 static const char *tiling_flag(int tiling)
74 case I915_TILING_NONE: return "";
75 case I915_TILING_X: return " X";
76 case I915_TILING_Y: return " Y";
80 static const char *dirty_flag(int dirty)
82 return dirty ? " dirty" : "";
85 static const char *purgeable_flag(int purgeable)
87 return purgeable ? " purgeable" : "";
90 static void __sg_set_buf(struct scatterlist *sg,
91 void *addr, unsigned int len, loff_t it)
93 sg->page_link = (unsigned long)virt_to_page(addr);
94 sg->offset = offset_in_page(addr);
99 static bool __i915_error_grow(struct drm_i915_error_state_buf *e, size_t len)
104 if (e->bytes + len + 1 <= e->size)
108 __sg_set_buf(e->cur++, e->buf, e->bytes, e->iter);
114 if (e->cur == e->end) {
115 struct scatterlist *sgl;
117 sgl = (typeof(sgl))__get_free_page(GFP_KERNEL);
127 (unsigned long)sgl | SG_CHAIN;
133 e->end = sgl + SG_MAX_SINGLE_ALLOC - 1;
136 e->size = ALIGN(len + 1, SZ_64K);
137 e->buf = kmalloc(e->size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
139 e->size = PAGE_ALIGN(len + 1);
140 e->buf = kmalloc(e->size, GFP_KERNEL);
151 static void i915_error_vprintf(struct drm_i915_error_state_buf *e,
152 const char *fmt, va_list args)
161 len = vsnprintf(NULL, 0, fmt, ap);
168 if (!__i915_error_grow(e, len))
171 GEM_BUG_ON(e->bytes >= e->size);
172 len = vscnprintf(e->buf + e->bytes, e->size - e->bytes, fmt, args);
180 static void i915_error_puts(struct drm_i915_error_state_buf *e, const char *str)
188 if (!__i915_error_grow(e, len))
191 GEM_BUG_ON(e->bytes + len > e->size);
192 memcpy(e->buf + e->bytes, str, len);
196 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
197 #define err_puts(e, s) i915_error_puts(e, s)
199 static void __i915_printfn_error(struct drm_printer *p, struct va_format *vaf)
201 i915_error_vprintf(p->arg, vaf->fmt, *vaf->va);
204 static inline struct drm_printer
205 i915_error_printer(struct drm_i915_error_state_buf *e)
207 struct drm_printer p = {
208 .printfn = __i915_printfn_error,
214 #ifdef CONFIG_DRM_I915_COMPRESS_ERROR
217 struct z_stream_s zstream;
221 static bool compress_init(struct compress *c)
223 struct z_stream_s *zstream = memset(&c->zstream, 0, sizeof(c->zstream));
226 kmalloc(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
227 GFP_ATOMIC | __GFP_NOWARN);
228 if (!zstream->workspace)
231 if (zlib_deflateInit(zstream, Z_DEFAULT_COMPRESSION) != Z_OK) {
232 kfree(zstream->workspace);
237 if (i915_has_memcpy_from_wc())
238 c->tmp = (void *)__get_free_page(GFP_ATOMIC | __GFP_NOWARN);
243 static void *compress_next_page(struct drm_i915_error_object *dst)
247 if (dst->page_count >= dst->num_pages)
248 return ERR_PTR(-ENOSPC);
250 page = __get_free_page(GFP_ATOMIC | __GFP_NOWARN);
252 return ERR_PTR(-ENOMEM);
254 return dst->pages[dst->page_count++] = (void *)page;
257 static int compress_page(struct compress *c,
259 struct drm_i915_error_object *dst)
261 struct z_stream_s *zstream = &c->zstream;
263 zstream->next_in = src;
264 if (c->tmp && i915_memcpy_from_wc(c->tmp, src, PAGE_SIZE))
265 zstream->next_in = c->tmp;
266 zstream->avail_in = PAGE_SIZE;
269 if (zstream->avail_out == 0) {
270 zstream->next_out = compress_next_page(dst);
271 if (IS_ERR(zstream->next_out))
272 return PTR_ERR(zstream->next_out);
274 zstream->avail_out = PAGE_SIZE;
277 if (zlib_deflate(zstream, Z_NO_FLUSH) != Z_OK)
280 touch_nmi_watchdog();
281 } while (zstream->avail_in);
283 /* Fallback to uncompressed if we increase size? */
284 if (0 && zstream->total_out > zstream->total_in)
290 static int compress_flush(struct compress *c,
291 struct drm_i915_error_object *dst)
293 struct z_stream_s *zstream = &c->zstream;
296 switch (zlib_deflate(zstream, Z_FINISH)) {
297 case Z_OK: /* more space requested */
298 zstream->next_out = compress_next_page(dst);
299 if (IS_ERR(zstream->next_out))
300 return PTR_ERR(zstream->next_out);
302 zstream->avail_out = PAGE_SIZE;
308 default: /* any error */
314 memset(zstream->next_out, 0, zstream->avail_out);
315 dst->unused = zstream->avail_out;
319 static void compress_fini(struct compress *c,
320 struct drm_i915_error_object *dst)
322 struct z_stream_s *zstream = &c->zstream;
324 zlib_deflateEnd(zstream);
325 kfree(zstream->workspace);
327 free_page((unsigned long)c->tmp);
330 static void err_compression_marker(struct drm_i915_error_state_buf *m)
340 static bool compress_init(struct compress *c)
345 static int compress_page(struct compress *c,
347 struct drm_i915_error_object *dst)
352 page = __get_free_page(GFP_ATOMIC | __GFP_NOWARN);
357 if (!i915_memcpy_from_wc(ptr, src, PAGE_SIZE))
358 memcpy(ptr, src, PAGE_SIZE);
359 dst->pages[dst->page_count++] = ptr;
364 static int compress_flush(struct compress *c,
365 struct drm_i915_error_object *dst)
370 static void compress_fini(struct compress *c,
371 struct drm_i915_error_object *dst)
375 static void err_compression_marker(struct drm_i915_error_state_buf *m)
382 static void print_error_buffers(struct drm_i915_error_state_buf *m,
384 struct drm_i915_error_buffer *err,
387 err_printf(m, "%s [%d]:\n", name, count);
390 err_printf(m, " %08x_%08x %8u %02x %02x",
391 upper_32_bits(err->gtt_offset),
392 lower_32_bits(err->gtt_offset),
396 err_puts(m, tiling_flag(err->tiling));
397 err_puts(m, dirty_flag(err->dirty));
398 err_puts(m, purgeable_flag(err->purgeable));
399 err_puts(m, err->userptr ? " userptr" : "");
400 err_puts(m, i915_cache_level_str(m->i915, err->cache_level));
403 err_printf(m, " (name: %d)", err->name);
404 if (err->fence_reg != I915_FENCE_REG_NONE)
405 err_printf(m, " (fence: %d)", err->fence_reg);
412 static void error_print_instdone(struct drm_i915_error_state_buf *m,
413 const struct drm_i915_error_engine *ee)
418 err_printf(m, " INSTDONE: 0x%08x\n",
419 ee->instdone.instdone);
421 if (ee->engine_id != RCS0 || INTEL_GEN(m->i915) <= 3)
424 err_printf(m, " SC_INSTDONE: 0x%08x\n",
425 ee->instdone.slice_common);
427 if (INTEL_GEN(m->i915) <= 6)
430 for_each_instdone_slice_subslice(m->i915, slice, subslice)
431 err_printf(m, " SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
433 ee->instdone.sampler[slice][subslice]);
435 for_each_instdone_slice_subslice(m->i915, slice, subslice)
436 err_printf(m, " ROW_INSTDONE[%d][%d]: 0x%08x\n",
438 ee->instdone.row[slice][subslice]);
441 static void error_print_request(struct drm_i915_error_state_buf *m,
443 const struct drm_i915_error_request *erq,
444 const unsigned long epoch)
449 err_printf(m, "%s pid %d, seqno %8x:%08x%s%s, prio %d, emitted %dms, start %08x, head %08x, tail %08x\n",
450 prefix, erq->pid, erq->context, erq->seqno,
451 test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
452 &erq->flags) ? "!" : "",
453 test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
454 &erq->flags) ? "+" : "",
455 erq->sched_attr.priority,
456 jiffies_to_msecs(erq->jiffies - epoch),
457 erq->start, erq->head, erq->tail);
460 static void error_print_context(struct drm_i915_error_state_buf *m,
462 const struct drm_i915_error_context *ctx)
464 err_printf(m, "%s%s[%d] hw_id %d, prio %d, guilty %d active %d\n",
465 header, ctx->comm, ctx->pid, ctx->hw_id,
466 ctx->sched_attr.priority, ctx->guilty, ctx->active);
469 static void error_print_engine(struct drm_i915_error_state_buf *m,
470 const struct drm_i915_error_engine *ee,
471 const unsigned long epoch)
475 err_printf(m, "%s command stream:\n",
476 engine_name(m->i915, ee->engine_id));
477 err_printf(m, " IDLE?: %s\n", yesno(ee->idle));
478 err_printf(m, " START: 0x%08x\n", ee->start);
479 err_printf(m, " HEAD: 0x%08x [0x%08x]\n", ee->head, ee->rq_head);
480 err_printf(m, " TAIL: 0x%08x [0x%08x, 0x%08x]\n",
481 ee->tail, ee->rq_post, ee->rq_tail);
482 err_printf(m, " CTL: 0x%08x\n", ee->ctl);
483 err_printf(m, " MODE: 0x%08x\n", ee->mode);
484 err_printf(m, " HWS: 0x%08x\n", ee->hws);
485 err_printf(m, " ACTHD: 0x%08x %08x\n",
486 (u32)(ee->acthd>>32), (u32)ee->acthd);
487 err_printf(m, " IPEIR: 0x%08x\n", ee->ipeir);
488 err_printf(m, " IPEHR: 0x%08x\n", ee->ipehr);
490 error_print_instdone(m, ee);
492 if (ee->batchbuffer) {
493 u64 start = ee->batchbuffer->gtt_offset;
494 u64 end = start + ee->batchbuffer->gtt_size;
496 err_printf(m, " batch: [0x%08x_%08x, 0x%08x_%08x]\n",
497 upper_32_bits(start), lower_32_bits(start),
498 upper_32_bits(end), lower_32_bits(end));
500 if (INTEL_GEN(m->i915) >= 4) {
501 err_printf(m, " BBADDR: 0x%08x_%08x\n",
502 (u32)(ee->bbaddr>>32), (u32)ee->bbaddr);
503 err_printf(m, " BB_STATE: 0x%08x\n", ee->bbstate);
504 err_printf(m, " INSTPS: 0x%08x\n", ee->instps);
506 err_printf(m, " INSTPM: 0x%08x\n", ee->instpm);
507 err_printf(m, " FADDR: 0x%08x %08x\n", upper_32_bits(ee->faddr),
508 lower_32_bits(ee->faddr));
509 if (INTEL_GEN(m->i915) >= 6) {
510 err_printf(m, " RC PSMI: 0x%08x\n", ee->rc_psmi);
511 err_printf(m, " FAULT_REG: 0x%08x\n", ee->fault_reg);
513 if (HAS_PPGTT(m->i915)) {
514 err_printf(m, " GFX_MODE: 0x%08x\n", ee->vm_info.gfx_mode);
516 if (INTEL_GEN(m->i915) >= 8) {
518 for (i = 0; i < 4; i++)
519 err_printf(m, " PDP%d: 0x%016llx\n",
520 i, ee->vm_info.pdp[i]);
522 err_printf(m, " PP_DIR_BASE: 0x%08x\n",
523 ee->vm_info.pp_dir_base);
526 err_printf(m, " ring->head: 0x%08x\n", ee->cpu_ring_head);
527 err_printf(m, " ring->tail: 0x%08x\n", ee->cpu_ring_tail);
528 err_printf(m, " hangcheck timestamp: %dms (%lu%s)\n",
529 jiffies_to_msecs(ee->hangcheck_timestamp - epoch),
530 ee->hangcheck_timestamp,
531 ee->hangcheck_timestamp == epoch ? "; epoch" : "");
532 err_printf(m, " engine reset count: %u\n", ee->reset_count);
534 for (n = 0; n < ee->num_ports; n++) {
535 err_printf(m, " ELSP[%d]:", n);
536 error_print_request(m, " ", &ee->execlist[n], epoch);
539 error_print_context(m, " Active context: ", &ee->context);
542 void i915_error_printf(struct drm_i915_error_state_buf *e, const char *f, ...)
547 i915_error_vprintf(e, f, args);
551 static void print_error_obj(struct drm_i915_error_state_buf *m,
552 struct intel_engine_cs *engine,
554 struct drm_i915_error_object *obj)
556 char out[ASCII85_BUFSZ];
563 err_printf(m, "%s --- %s = 0x%08x %08x\n",
564 engine ? engine->name : "global", name,
565 upper_32_bits(obj->gtt_offset),
566 lower_32_bits(obj->gtt_offset));
569 err_compression_marker(m);
570 for (page = 0; page < obj->page_count; page++) {
574 if (page == obj->page_count - 1)
576 len = ascii85_encode_len(len);
578 for (i = 0; i < len; i++)
579 err_puts(m, ascii85_encode(obj->pages[page][i], out));
584 static void err_print_capabilities(struct drm_i915_error_state_buf *m,
585 const struct intel_device_info *info,
586 const struct intel_runtime_info *runtime,
587 const struct intel_driver_caps *caps)
589 struct drm_printer p = i915_error_printer(m);
591 intel_device_info_dump_flags(info, &p);
592 intel_driver_caps_print(caps, &p);
593 intel_device_info_dump_topology(&runtime->sseu, &p);
596 static void err_print_params(struct drm_i915_error_state_buf *m,
597 const struct i915_params *params)
599 struct drm_printer p = i915_error_printer(m);
601 i915_params_dump(params, &p);
604 static void err_print_pciid(struct drm_i915_error_state_buf *m,
605 struct drm_i915_private *i915)
607 struct pci_dev *pdev = i915->drm.pdev;
609 err_printf(m, "PCI ID: 0x%04x\n", pdev->device);
610 err_printf(m, "PCI Revision: 0x%02x\n", pdev->revision);
611 err_printf(m, "PCI Subsystem: %04x:%04x\n",
612 pdev->subsystem_vendor,
613 pdev->subsystem_device);
616 static void err_print_uc(struct drm_i915_error_state_buf *m,
617 const struct i915_error_uc *error_uc)
619 struct drm_printer p = i915_error_printer(m);
620 const struct i915_gpu_state *error =
621 container_of(error_uc, typeof(*error), uc);
623 if (!error->device_info.has_guc)
626 intel_uc_fw_dump(&error_uc->guc_fw, &p);
627 intel_uc_fw_dump(&error_uc->huc_fw, &p);
628 print_error_obj(m, NULL, "GuC log buffer", error_uc->guc_log);
631 static void err_free_sgl(struct scatterlist *sgl)
634 struct scatterlist *sg;
636 for (sg = sgl; !sg_is_chain(sg); sg++) {
642 sg = sg_is_last(sg) ? NULL : sg_chain_ptr(sg);
643 free_page((unsigned long)sgl);
648 static void __err_print_to_sgl(struct drm_i915_error_state_buf *m,
649 struct i915_gpu_state *error)
651 struct drm_i915_error_object *obj;
652 struct timespec64 ts;
655 if (*error->error_msg)
656 err_printf(m, "%s\n", error->error_msg);
657 err_printf(m, "Kernel: %s %s\n",
658 init_utsname()->release,
659 init_utsname()->machine);
660 ts = ktime_to_timespec64(error->time);
661 err_printf(m, "Time: %lld s %ld us\n",
662 (s64)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
663 ts = ktime_to_timespec64(error->boottime);
664 err_printf(m, "Boottime: %lld s %ld us\n",
665 (s64)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
666 ts = ktime_to_timespec64(error->uptime);
667 err_printf(m, "Uptime: %lld s %ld us\n",
668 (s64)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
669 err_printf(m, "Epoch: %lu jiffies (%u HZ)\n", error->epoch, HZ);
670 err_printf(m, "Capture: %lu jiffies; %d ms ago, %d ms after epoch\n",
672 jiffies_to_msecs(jiffies - error->capture),
673 jiffies_to_msecs(error->capture - error->epoch));
675 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
676 if (!error->engine[i].context.pid)
679 err_printf(m, "Active process (on ring %s): %s [%d]\n",
680 engine_name(m->i915, i),
681 error->engine[i].context.comm,
682 error->engine[i].context.pid);
684 err_printf(m, "Reset count: %u\n", error->reset_count);
685 err_printf(m, "Suspend count: %u\n", error->suspend_count);
686 err_printf(m, "Platform: %s\n", intel_platform_name(error->device_info.platform));
687 err_printf(m, "Subplatform: 0x%x\n",
688 intel_subplatform(&error->runtime_info,
689 error->device_info.platform));
690 err_print_pciid(m, m->i915);
692 err_printf(m, "IOMMU enabled?: %d\n", error->iommu);
694 if (HAS_CSR(m->i915)) {
695 struct intel_csr *csr = &m->i915->csr;
697 err_printf(m, "DMC loaded: %s\n",
698 yesno(csr->dmc_payload != NULL));
699 err_printf(m, "DMC fw version: %d.%d\n",
700 CSR_VERSION_MAJOR(csr->version),
701 CSR_VERSION_MINOR(csr->version));
704 err_printf(m, "GT awake: %s\n", yesno(error->awake));
705 err_printf(m, "RPM wakelock: %s\n", yesno(error->wakelock));
706 err_printf(m, "PM suspended: %s\n", yesno(error->suspended));
707 err_printf(m, "EIR: 0x%08x\n", error->eir);
708 err_printf(m, "IER: 0x%08x\n", error->ier);
709 for (i = 0; i < error->ngtier; i++)
710 err_printf(m, "GTIER[%d]: 0x%08x\n", i, error->gtier[i]);
711 err_printf(m, "PGTBL_ER: 0x%08x\n", error->pgtbl_er);
712 err_printf(m, "FORCEWAKE: 0x%08x\n", error->forcewake);
713 err_printf(m, "DERRMR: 0x%08x\n", error->derrmr);
714 err_printf(m, "CCID: 0x%08x\n", error->ccid);
716 for (i = 0; i < error->nfence; i++)
717 err_printf(m, " fence[%d] = %08llx\n", i, error->fence[i]);
719 if (INTEL_GEN(m->i915) >= 6) {
720 err_printf(m, "ERROR: 0x%08x\n", error->error);
722 if (INTEL_GEN(m->i915) >= 8)
723 err_printf(m, "FAULT_TLB_DATA: 0x%08x 0x%08x\n",
724 error->fault_data1, error->fault_data0);
726 err_printf(m, "DONE_REG: 0x%08x\n", error->done_reg);
729 if (IS_GEN(m->i915, 7))
730 err_printf(m, "ERR_INT: 0x%08x\n", error->err_int);
732 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
733 if (error->engine[i].engine_id != -1)
734 error_print_engine(m, &error->engine[i], error->epoch);
737 for (i = 0; i < ARRAY_SIZE(error->active_vm); i++) {
741 if (!error->active_vm[i])
744 len = scnprintf(buf, sizeof(buf), "Active (");
745 for (j = 0; j < ARRAY_SIZE(error->engine); j++) {
746 if (error->engine[j].vm != error->active_vm[i])
749 len += scnprintf(buf + len, sizeof(buf), "%s%s",
751 m->i915->engine[j]->name);
754 scnprintf(buf + len, sizeof(buf), ")");
755 print_error_buffers(m, buf,
757 error->active_bo_count[i]);
760 print_error_buffers(m, "Pinned (global)",
762 error->pinned_bo_count);
764 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
765 const struct drm_i915_error_engine *ee = &error->engine[i];
767 obj = ee->batchbuffer;
769 err_puts(m, m->i915->engine[i]->name);
771 err_printf(m, " (submitted by %s [%d])",
774 err_printf(m, " --- gtt_offset = 0x%08x %08x\n",
775 upper_32_bits(obj->gtt_offset),
776 lower_32_bits(obj->gtt_offset));
777 print_error_obj(m, m->i915->engine[i], NULL, obj);
780 for (j = 0; j < ee->user_bo_count; j++)
781 print_error_obj(m, m->i915->engine[i],
782 "user", ee->user_bo[j]);
784 if (ee->num_requests) {
785 err_printf(m, "%s --- %d requests\n",
786 m->i915->engine[i]->name,
788 for (j = 0; j < ee->num_requests; j++)
789 error_print_request(m, " ",
794 print_error_obj(m, m->i915->engine[i],
795 "ringbuffer", ee->ringbuffer);
797 print_error_obj(m, m->i915->engine[i],
798 "HW Status", ee->hws_page);
800 print_error_obj(m, m->i915->engine[i],
801 "HW context", ee->ctx);
803 print_error_obj(m, m->i915->engine[i],
804 "WA context", ee->wa_ctx);
806 print_error_obj(m, m->i915->engine[i],
807 "WA batchbuffer", ee->wa_batchbuffer);
809 print_error_obj(m, m->i915->engine[i],
810 "NULL context", ee->default_state);
814 intel_overlay_print_error_state(m, error->overlay);
817 intel_display_print_error_state(m, error->display);
819 err_print_capabilities(m, &error->device_info, &error->runtime_info,
820 &error->driver_caps);
821 err_print_params(m, &error->params);
822 err_print_uc(m, &error->uc);
825 static int err_print_to_sgl(struct i915_gpu_state *error)
827 struct drm_i915_error_state_buf m;
830 return PTR_ERR(error);
832 if (READ_ONCE(error->sgl))
835 memset(&m, 0, sizeof(m));
836 m.i915 = error->i915;
838 __err_print_to_sgl(&m, error);
841 __sg_set_buf(m.cur++, m.buf, m.bytes, m.iter);
846 GEM_BUG_ON(m.end < m.cur);
847 sg_mark_end(m.cur - 1);
849 GEM_BUG_ON(m.sgl && !m.cur);
856 if (cmpxchg(&error->sgl, NULL, m.sgl))
862 ssize_t i915_gpu_state_copy_to_buffer(struct i915_gpu_state *error,
863 char *buf, loff_t off, size_t rem)
865 struct scatterlist *sg;
873 err = err_print_to_sgl(error);
877 sg = READ_ONCE(error->fit);
878 if (!sg || off < sg->dma_address)
883 pos = sg->dma_address;
888 if (sg_is_chain(sg)) {
889 sg = sg_chain_ptr(sg);
890 GEM_BUG_ON(sg_is_chain(sg));
894 if (pos + len <= off) {
901 GEM_BUG_ON(off - pos > len);
908 GEM_BUG_ON(!len || len > sg->length);
910 memcpy(buf, page_address(sg_page(sg)) + start, len);
918 WRITE_ONCE(error->fit, sg);
921 } while (!sg_is_last(sg++));
926 static void i915_error_object_free(struct drm_i915_error_object *obj)
933 for (page = 0; page < obj->page_count; page++)
934 free_page((unsigned long)obj->pages[page]);
940 static void cleanup_params(struct i915_gpu_state *error)
942 i915_params_free(&error->params);
945 static void cleanup_uc_state(struct i915_gpu_state *error)
947 struct i915_error_uc *error_uc = &error->uc;
949 kfree(error_uc->guc_fw.path);
950 kfree(error_uc->huc_fw.path);
951 i915_error_object_free(error_uc->guc_log);
954 void __i915_gpu_state_free(struct kref *error_ref)
956 struct i915_gpu_state *error =
957 container_of(error_ref, typeof(*error), ref);
960 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
961 struct drm_i915_error_engine *ee = &error->engine[i];
963 for (j = 0; j < ee->user_bo_count; j++)
964 i915_error_object_free(ee->user_bo[j]);
967 i915_error_object_free(ee->batchbuffer);
968 i915_error_object_free(ee->wa_batchbuffer);
969 i915_error_object_free(ee->ringbuffer);
970 i915_error_object_free(ee->hws_page);
971 i915_error_object_free(ee->ctx);
972 i915_error_object_free(ee->wa_ctx);
977 for (i = 0; i < ARRAY_SIZE(error->active_bo); i++)
978 kfree(error->active_bo[i]);
979 kfree(error->pinned_bo);
981 kfree(error->overlay);
982 kfree(error->display);
984 cleanup_params(error);
985 cleanup_uc_state(error);
987 err_free_sgl(error->sgl);
991 static struct drm_i915_error_object *
992 i915_error_object_create(struct drm_i915_private *i915,
993 struct i915_vma *vma)
995 struct i915_ggtt *ggtt = &i915->ggtt;
996 const u64 slot = ggtt->error_capture.start;
997 struct drm_i915_error_object *dst;
998 struct compress compress;
999 unsigned long num_pages;
1000 struct sgt_iter iter;
1004 if (!vma || !vma->pages)
1007 num_pages = min_t(u64, vma->size, vma->obj->base.size) >> PAGE_SHIFT;
1008 num_pages = DIV_ROUND_UP(10 * num_pages, 8); /* worstcase zlib growth */
1009 dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *),
1010 GFP_ATOMIC | __GFP_NOWARN);
1014 dst->gtt_offset = vma->node.start;
1015 dst->gtt_size = vma->node.size;
1016 dst->num_pages = num_pages;
1017 dst->page_count = 0;
1020 if (!compress_init(&compress)) {
1026 for_each_sgt_dma(dma, iter, vma->pages) {
1029 ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0);
1031 s = io_mapping_map_atomic_wc(&ggtt->iomap, slot);
1032 ret = compress_page(&compress, (void __force *)s, dst);
1033 io_mapping_unmap_atomic(s);
1038 if (ret || compress_flush(&compress, dst)) {
1039 while (dst->page_count--)
1040 free_page((unsigned long)dst->pages[dst->page_count]);
1045 compress_fini(&compress, dst);
1049 static void capture_bo(struct drm_i915_error_buffer *err,
1050 struct i915_vma *vma)
1052 struct drm_i915_gem_object *obj = vma->obj;
1054 err->size = obj->base.size;
1055 err->name = obj->base.name;
1057 err->gtt_offset = vma->node.start;
1058 err->read_domains = obj->read_domains;
1059 err->write_domain = obj->write_domain;
1060 err->fence_reg = vma->fence ? vma->fence->id : -1;
1061 err->tiling = i915_gem_object_get_tiling(obj);
1062 err->dirty = obj->mm.dirty;
1063 err->purgeable = obj->mm.madv != I915_MADV_WILLNEED;
1064 err->userptr = obj->userptr.mm != NULL;
1065 err->cache_level = obj->cache_level;
1068 static u32 capture_error_bo(struct drm_i915_error_buffer *err,
1069 int count, struct list_head *head,
1071 #define ACTIVE_ONLY BIT(0)
1072 #define PINNED_ONLY BIT(1)
1074 struct i915_vma *vma;
1077 list_for_each_entry(vma, head, vm_link) {
1081 if (flags & ACTIVE_ONLY && !i915_vma_is_active(vma))
1084 if (flags & PINNED_ONLY && !i915_vma_is_pinned(vma))
1087 capture_bo(err++, vma);
1096 * Generate a semi-unique error code. The code is not meant to have meaning, The
1097 * code's only purpose is to try to prevent false duplicated bug reports by
1098 * grossly estimating a GPU error state.
1100 * TODO Ideally, hashing the batchbuffer would be a very nice way to determine
1101 * the hang if we could strip the GTT offset information from it.
1103 * It's only a small step better than a random number in its current form.
1105 static u32 i915_error_generate_code(struct i915_gpu_state *error,
1106 intel_engine_mask_t engine_mask)
1109 * IPEHR would be an ideal way to detect errors, as it's the gross
1110 * measure of "the command that hung." However, has some very common
1111 * synchronization commands which almost always appear in the case
1112 * strictly a client bug. Use instdone to differentiate those some.
1115 struct drm_i915_error_engine *ee =
1116 &error->engine[ffs(engine_mask)];
1118 return ee->ipehr ^ ee->instdone.instdone;
1124 static void gem_record_fences(struct i915_gpu_state *error)
1126 struct drm_i915_private *dev_priv = error->i915;
1127 struct intel_uncore *uncore = &dev_priv->uncore;
1130 if (INTEL_GEN(dev_priv) >= 6) {
1131 for (i = 0; i < dev_priv->ggtt.num_fences; i++)
1133 intel_uncore_read64(uncore,
1134 FENCE_REG_GEN6_LO(i));
1135 } else if (INTEL_GEN(dev_priv) >= 4) {
1136 for (i = 0; i < dev_priv->ggtt.num_fences; i++)
1138 intel_uncore_read64(uncore,
1139 FENCE_REG_965_LO(i));
1141 for (i = 0; i < dev_priv->ggtt.num_fences; i++)
1143 intel_uncore_read(uncore, FENCE_REG(i));
1148 static void error_record_engine_registers(struct i915_gpu_state *error,
1149 struct intel_engine_cs *engine,
1150 struct drm_i915_error_engine *ee)
1152 struct drm_i915_private *dev_priv = engine->i915;
1154 if (INTEL_GEN(dev_priv) >= 6) {
1155 ee->rc_psmi = ENGINE_READ(engine, RING_PSMI_CTL);
1156 if (INTEL_GEN(dev_priv) >= 8)
1157 ee->fault_reg = I915_READ(GEN8_RING_FAULT_REG);
1159 ee->fault_reg = GEN6_RING_FAULT_REG_READ(engine);
1162 if (INTEL_GEN(dev_priv) >= 4) {
1163 ee->faddr = ENGINE_READ(engine, RING_DMA_FADD);
1164 ee->ipeir = ENGINE_READ(engine, RING_IPEIR);
1165 ee->ipehr = ENGINE_READ(engine, RING_IPEHR);
1166 ee->instps = ENGINE_READ(engine, RING_INSTPS);
1167 ee->bbaddr = ENGINE_READ(engine, RING_BBADDR);
1168 if (INTEL_GEN(dev_priv) >= 8) {
1169 ee->faddr |= (u64)ENGINE_READ(engine, RING_DMA_FADD_UDW) << 32;
1170 ee->bbaddr |= (u64)ENGINE_READ(engine, RING_BBADDR_UDW) << 32;
1172 ee->bbstate = ENGINE_READ(engine, RING_BBSTATE);
1174 ee->faddr = ENGINE_READ(engine, DMA_FADD_I8XX);
1175 ee->ipeir = ENGINE_READ(engine, IPEIR);
1176 ee->ipehr = ENGINE_READ(engine, IPEHR);
1179 intel_engine_get_instdone(engine, &ee->instdone);
1181 ee->instpm = ENGINE_READ(engine, RING_INSTPM);
1182 ee->acthd = intel_engine_get_active_head(engine);
1183 ee->start = ENGINE_READ(engine, RING_START);
1184 ee->head = ENGINE_READ(engine, RING_HEAD);
1185 ee->tail = ENGINE_READ(engine, RING_TAIL);
1186 ee->ctl = ENGINE_READ(engine, RING_CTL);
1187 if (INTEL_GEN(dev_priv) > 2)
1188 ee->mode = ENGINE_READ(engine, RING_MI_MODE);
1190 if (!HWS_NEEDS_PHYSICAL(dev_priv)) {
1193 if (IS_GEN(dev_priv, 7)) {
1194 switch (engine->id) {
1196 MISSING_CASE(engine->id);
1198 mmio = RENDER_HWS_PGA_GEN7;
1201 mmio = BLT_HWS_PGA_GEN7;
1204 mmio = BSD_HWS_PGA_GEN7;
1207 mmio = VEBOX_HWS_PGA_GEN7;
1210 } else if (IS_GEN(engine->i915, 6)) {
1211 mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
1213 /* XXX: gen8 returns to sanity */
1214 mmio = RING_HWS_PGA(engine->mmio_base);
1217 ee->hws = I915_READ(mmio);
1220 ee->idle = intel_engine_is_idle(engine);
1222 ee->hangcheck_timestamp = engine->hangcheck.action_timestamp;
1223 ee->reset_count = i915_reset_engine_count(&dev_priv->gpu_error,
1226 if (HAS_PPGTT(dev_priv)) {
1229 ee->vm_info.gfx_mode = ENGINE_READ(engine, RING_MODE_GEN7);
1231 if (IS_GEN(dev_priv, 6)) {
1232 ee->vm_info.pp_dir_base =
1233 ENGINE_READ(engine, RING_PP_DIR_BASE_READ);
1234 } else if (IS_GEN(dev_priv, 7)) {
1235 ee->vm_info.pp_dir_base =
1236 ENGINE_READ(engine, RING_PP_DIR_BASE);
1237 } else if (INTEL_GEN(dev_priv) >= 8) {
1238 u32 base = engine->mmio_base;
1240 for (i = 0; i < 4; i++) {
1241 ee->vm_info.pdp[i] =
1242 I915_READ(GEN8_RING_PDP_UDW(base, i));
1243 ee->vm_info.pdp[i] <<= 32;
1244 ee->vm_info.pdp[i] |=
1245 I915_READ(GEN8_RING_PDP_LDW(base, i));
1251 static void record_request(struct i915_request *request,
1252 struct drm_i915_error_request *erq)
1254 struct i915_gem_context *ctx = request->gem_context;
1256 erq->flags = request->fence.flags;
1257 erq->context = request->fence.context;
1258 erq->seqno = request->fence.seqno;
1259 erq->sched_attr = request->sched.attr;
1260 erq->jiffies = request->emitted_jiffies;
1261 erq->start = i915_ggtt_offset(request->ring->vma);
1262 erq->head = request->head;
1263 erq->tail = request->tail;
1266 erq->pid = ctx->pid ? pid_nr(ctx->pid) : 0;
1270 static void engine_record_requests(struct intel_engine_cs *engine,
1271 struct i915_request *first,
1272 struct drm_i915_error_engine *ee)
1274 struct i915_request *request;
1279 list_for_each_entry_from(request, &engine->active.requests, sched.link)
1284 ee->requests = kcalloc(count, sizeof(*ee->requests), GFP_ATOMIC);
1288 ee->num_requests = count;
1292 list_for_each_entry_from(request,
1293 &engine->active.requests, sched.link) {
1294 if (count >= ee->num_requests) {
1296 * If the ring request list was changed in
1297 * between the point where the error request
1298 * list was created and dimensioned and this
1299 * point then just exit early to avoid crashes.
1301 * We don't need to communicate that the
1302 * request list changed state during error
1303 * state capture and that the error state is
1304 * slightly incorrect as a consequence since we
1305 * are typically only interested in the request
1306 * list state at the point of error state
1307 * capture, not in any changes happening during
1313 record_request(request, &ee->requests[count++]);
1315 ee->num_requests = count;
1318 static void error_record_engine_execlists(struct intel_engine_cs *engine,
1319 struct drm_i915_error_engine *ee)
1321 const struct intel_engine_execlists * const execlists = &engine->execlists;
1324 for (n = 0; n < execlists_num_ports(execlists); n++) {
1325 struct i915_request *rq = port_request(&execlists->port[n]);
1330 record_request(rq, &ee->execlist[n]);
1336 static void record_context(struct drm_i915_error_context *e,
1337 struct i915_gem_context *ctx)
1340 struct task_struct *task;
1343 task = pid_task(ctx->pid, PIDTYPE_PID);
1345 strcpy(e->comm, task->comm);
1351 e->hw_id = ctx->hw_id;
1352 e->sched_attr = ctx->sched;
1353 e->guilty = atomic_read(&ctx->guilty_count);
1354 e->active = atomic_read(&ctx->active_count);
1357 static void request_record_user_bo(struct i915_request *request,
1358 struct drm_i915_error_engine *ee)
1360 struct i915_capture_list *c;
1361 struct drm_i915_error_object **bo;
1365 for (c = request->capture_list; c; c = c->next)
1370 bo = kmalloc_array(max, sizeof(*bo), GFP_ATOMIC);
1372 /* If we can't capture everything, try to capture something. */
1373 max = min_t(long, max, PAGE_SIZE / sizeof(*bo));
1374 bo = kmalloc_array(max, sizeof(*bo), GFP_ATOMIC);
1380 for (c = request->capture_list; c; c = c->next) {
1381 bo[count] = i915_error_object_create(request->i915, c->vma);
1389 ee->user_bo_count = count;
1392 static struct drm_i915_error_object *
1393 capture_object(struct drm_i915_private *dev_priv,
1394 struct drm_i915_gem_object *obj)
1396 if (obj && i915_gem_object_has_pages(obj)) {
1397 struct i915_vma fake = {
1398 .node = { .start = U64_MAX, .size = obj->base.size },
1399 .size = obj->base.size,
1400 .pages = obj->mm.pages,
1404 return i915_error_object_create(dev_priv, &fake);
1410 static void gem_record_rings(struct i915_gpu_state *error)
1412 struct drm_i915_private *i915 = error->i915;
1413 struct i915_ggtt *ggtt = &i915->ggtt;
1416 for (i = 0; i < I915_NUM_ENGINES; i++) {
1417 struct intel_engine_cs *engine = i915->engine[i];
1418 struct drm_i915_error_engine *ee = &error->engine[i];
1419 struct i915_request *request;
1428 error_record_engine_registers(error, engine, ee);
1429 error_record_engine_execlists(engine, ee);
1431 request = intel_engine_find_active_request(engine);
1433 struct i915_gem_context *ctx = request->gem_context;
1434 struct intel_ring *ring;
1436 ee->vm = ctx->vm ?: &ggtt->vm;
1438 record_context(&ee->context, ctx);
1440 /* We need to copy these to an anonymous buffer
1441 * as the simplest method to avoid being overwritten
1445 i915_error_object_create(i915, request->batch);
1447 if (HAS_BROKEN_CS_TLB(i915))
1448 ee->wa_batchbuffer =
1449 i915_error_object_create(i915,
1451 request_record_user_bo(request, ee);
1454 i915_error_object_create(i915,
1455 request->hw_context->state);
1458 i915_gem_context_no_error_capture(ctx);
1460 ee->rq_head = request->head;
1461 ee->rq_post = request->postfix;
1462 ee->rq_tail = request->tail;
1464 ring = request->ring;
1465 ee->cpu_ring_head = ring->head;
1466 ee->cpu_ring_tail = ring->tail;
1468 i915_error_object_create(i915, ring->vma);
1470 engine_record_requests(engine, request, ee);
1474 i915_error_object_create(i915,
1475 engine->status_page.vma);
1477 ee->wa_ctx = i915_error_object_create(i915, engine->wa_ctx.vma);
1479 ee->default_state = capture_object(i915, engine->default_state);
1483 static void gem_capture_vm(struct i915_gpu_state *error,
1484 struct i915_address_space *vm,
1487 struct drm_i915_error_buffer *active_bo;
1488 struct i915_vma *vma;
1492 list_for_each_entry(vma, &vm->bound_list, vm_link)
1493 if (i915_vma_is_active(vma))
1498 active_bo = kcalloc(count, sizeof(*active_bo), GFP_ATOMIC);
1500 count = capture_error_bo(active_bo,
1501 count, &vm->bound_list,
1506 error->active_vm[idx] = vm;
1507 error->active_bo[idx] = active_bo;
1508 error->active_bo_count[idx] = count;
1511 static void capture_active_buffers(struct i915_gpu_state *error)
1515 BUILD_BUG_ON(ARRAY_SIZE(error->engine) > ARRAY_SIZE(error->active_bo));
1516 BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_vm));
1517 BUILD_BUG_ON(ARRAY_SIZE(error->active_bo) != ARRAY_SIZE(error->active_bo_count));
1519 /* Scan each engine looking for unique active contexts/vm */
1520 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
1521 struct drm_i915_error_engine *ee = &error->engine[i];
1528 for (j = 0; j < i && !found; j++)
1529 found = error->engine[j].vm == ee->vm;
1531 gem_capture_vm(error, ee->vm, cnt++);
1535 static void capture_pinned_buffers(struct i915_gpu_state *error)
1537 struct i915_address_space *vm = &error->i915->ggtt.vm;
1538 struct drm_i915_error_buffer *bo;
1539 struct i915_vma *vma;
1543 list_for_each_entry(vma, &vm->bound_list, vm_link)
1548 bo = kcalloc(count, sizeof(*bo), GFP_ATOMIC);
1552 error->pinned_bo_count =
1553 capture_error_bo(bo, count, &vm->bound_list, PINNED_ONLY);
1554 error->pinned_bo = bo;
1557 static void capture_uc_state(struct i915_gpu_state *error)
1559 struct drm_i915_private *i915 = error->i915;
1560 struct i915_error_uc *error_uc = &error->uc;
1562 /* Capturing uC state won't be useful if there is no GuC */
1563 if (!error->device_info.has_guc)
1566 error_uc->guc_fw = i915->guc.fw;
1567 error_uc->huc_fw = i915->huc.fw;
1569 /* Non-default firmware paths will be specified by the modparam.
1570 * As modparams are generally accesible from the userspace make
1571 * explicit copies of the firmware paths.
1573 error_uc->guc_fw.path = kstrdup(i915->guc.fw.path, GFP_ATOMIC);
1574 error_uc->huc_fw.path = kstrdup(i915->huc.fw.path, GFP_ATOMIC);
1575 error_uc->guc_log = i915_error_object_create(i915, i915->guc.log.vma);
1578 /* Capture all registers which don't fit into another category. */
1579 static void capture_reg_state(struct i915_gpu_state *error)
1581 struct drm_i915_private *i915 = error->i915;
1582 struct intel_uncore *uncore = &i915->uncore;
1585 /* General organization
1586 * 1. Registers specific to a single generation
1587 * 2. Registers which belong to multiple generations
1588 * 3. Feature specific registers.
1589 * 4. Everything else
1590 * Please try to follow the order.
1593 /* 1: Registers specific to a single generation */
1594 if (IS_VALLEYVIEW(i915)) {
1595 error->gtier[0] = intel_uncore_read(uncore, GTIER);
1596 error->ier = intel_uncore_read(uncore, VLV_IER);
1597 error->forcewake = intel_uncore_read_fw(uncore, FORCEWAKE_VLV);
1600 if (IS_GEN(i915, 7))
1601 error->err_int = intel_uncore_read(uncore, GEN7_ERR_INT);
1603 if (INTEL_GEN(i915) >= 8) {
1604 error->fault_data0 = intel_uncore_read(uncore,
1605 GEN8_FAULT_TLB_DATA0);
1606 error->fault_data1 = intel_uncore_read(uncore,
1607 GEN8_FAULT_TLB_DATA1);
1610 if (IS_GEN(i915, 6)) {
1611 error->forcewake = intel_uncore_read_fw(uncore, FORCEWAKE);
1612 error->gab_ctl = intel_uncore_read(uncore, GAB_CTL);
1613 error->gfx_mode = intel_uncore_read(uncore, GFX_MODE);
1616 /* 2: Registers which belong to multiple generations */
1617 if (INTEL_GEN(i915) >= 7)
1618 error->forcewake = intel_uncore_read_fw(uncore, FORCEWAKE_MT);
1620 if (INTEL_GEN(i915) >= 6) {
1621 error->derrmr = intel_uncore_read(uncore, DERRMR);
1622 error->error = intel_uncore_read(uncore, ERROR_GEN6);
1623 error->done_reg = intel_uncore_read(uncore, DONE_REG);
1626 if (INTEL_GEN(i915) >= 5)
1627 error->ccid = intel_uncore_read(uncore, CCID(RENDER_RING_BASE));
1629 /* 3: Feature specific registers */
1630 if (IS_GEN_RANGE(i915, 6, 7)) {
1631 error->gam_ecochk = intel_uncore_read(uncore, GAM_ECOCHK);
1632 error->gac_eco = intel_uncore_read(uncore, GAC_ECO_BITS);
1635 /* 4: Everything else */
1636 if (INTEL_GEN(i915) >= 11) {
1637 error->ier = intel_uncore_read(uncore, GEN8_DE_MISC_IER);
1639 intel_uncore_read(uncore,
1640 GEN11_RENDER_COPY_INTR_ENABLE);
1642 intel_uncore_read(uncore, GEN11_VCS_VECS_INTR_ENABLE);
1644 intel_uncore_read(uncore, GEN11_GUC_SG_INTR_ENABLE);
1646 intel_uncore_read(uncore,
1647 GEN11_GPM_WGBOXPERF_INTR_ENABLE);
1649 intel_uncore_read(uncore,
1650 GEN11_CRYPTO_RSVD_INTR_ENABLE);
1652 intel_uncore_read(uncore,
1653 GEN11_GUNIT_CSME_INTR_ENABLE);
1655 } else if (INTEL_GEN(i915) >= 8) {
1656 error->ier = intel_uncore_read(uncore, GEN8_DE_MISC_IER);
1657 for (i = 0; i < 4; i++)
1658 error->gtier[i] = intel_uncore_read(uncore,
1661 } else if (HAS_PCH_SPLIT(i915)) {
1662 error->ier = intel_uncore_read(uncore, DEIER);
1663 error->gtier[0] = intel_uncore_read(uncore, GTIER);
1665 } else if (IS_GEN(i915, 2)) {
1666 error->ier = intel_uncore_read16(uncore, GEN2_IER);
1667 } else if (!IS_VALLEYVIEW(i915)) {
1668 error->ier = intel_uncore_read(uncore, GEN2_IER);
1670 error->eir = intel_uncore_read(uncore, EIR);
1671 error->pgtbl_er = intel_uncore_read(uncore, PGTBL_ER);
1675 error_msg(struct i915_gpu_state *error,
1676 intel_engine_mask_t engines, const char *msg)
1681 for (i = 0; i < ARRAY_SIZE(error->engine); i++)
1682 if (!error->engine[i].context.pid)
1685 len = scnprintf(error->error_msg, sizeof(error->error_msg),
1686 "GPU HANG: ecode %d:%x:0x%08x",
1687 INTEL_GEN(error->i915), engines,
1688 i915_error_generate_code(error, engines));
1690 /* Just show the first executing process, more is confusing */
1692 len += scnprintf(error->error_msg + len,
1693 sizeof(error->error_msg) - len,
1695 error->engine[i].context.comm,
1696 error->engine[i].context.pid);
1699 len += scnprintf(error->error_msg + len,
1700 sizeof(error->error_msg) - len,
1703 return error->error_msg;
1706 static void capture_gen_state(struct i915_gpu_state *error)
1708 struct drm_i915_private *i915 = error->i915;
1710 error->awake = i915->gt.awake;
1711 error->wakelock = atomic_read(&i915->runtime_pm.wakeref_count);
1712 error->suspended = i915->runtime_pm.suspended;
1715 #ifdef CONFIG_INTEL_IOMMU
1716 error->iommu = intel_iommu_gfx_mapped;
1718 error->reset_count = i915_reset_count(&i915->gpu_error);
1719 error->suspend_count = i915->suspend_count;
1721 memcpy(&error->device_info,
1723 sizeof(error->device_info));
1724 memcpy(&error->runtime_info,
1726 sizeof(error->runtime_info));
1727 error->driver_caps = i915->caps;
1730 static void capture_params(struct i915_gpu_state *error)
1732 i915_params_copy(&error->params, &i915_modparams);
1735 static unsigned long capture_find_epoch(const struct i915_gpu_state *error)
1737 unsigned long epoch = error->capture;
1740 for (i = 0; i < ARRAY_SIZE(error->engine); i++) {
1741 const struct drm_i915_error_engine *ee = &error->engine[i];
1743 if (ee->hangcheck_timestamp &&
1744 time_before(ee->hangcheck_timestamp, epoch))
1745 epoch = ee->hangcheck_timestamp;
1751 static void capture_finish(struct i915_gpu_state *error)
1753 struct i915_ggtt *ggtt = &error->i915->ggtt;
1754 const u64 slot = ggtt->error_capture.start;
1756 ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
1759 static int capture(void *data)
1761 struct i915_gpu_state *error = data;
1763 error->time = ktime_get_real();
1764 error->boottime = ktime_get_boottime();
1765 error->uptime = ktime_sub(ktime_get(),
1766 error->i915->gt.last_init_time);
1767 error->capture = jiffies;
1769 capture_params(error);
1770 capture_gen_state(error);
1771 capture_uc_state(error);
1772 capture_reg_state(error);
1773 gem_record_fences(error);
1774 gem_record_rings(error);
1775 capture_active_buffers(error);
1776 capture_pinned_buffers(error);
1778 error->overlay = intel_overlay_capture_error_state(error->i915);
1779 error->display = intel_display_capture_error_state(error->i915);
1781 error->epoch = capture_find_epoch(error);
1783 capture_finish(error);
1787 #define DAY_AS_SECONDS(x) (24 * 60 * 60 * (x))
1789 struct i915_gpu_state *
1790 i915_capture_gpu_state(struct drm_i915_private *i915)
1792 struct i915_gpu_state *error;
1794 /* Check if GPU capture has been disabled */
1795 error = READ_ONCE(i915->gpu_error.first_error);
1799 error = kzalloc(sizeof(*error), GFP_ATOMIC);
1801 i915_disable_error_state(i915, -ENOMEM);
1802 return ERR_PTR(-ENOMEM);
1805 kref_init(&error->ref);
1808 stop_machine(capture, error, NULL);
1814 * i915_capture_error_state - capture an error record for later analysis
1815 * @i915: i915 device
1816 * @engine_mask: the mask of engines triggering the hang
1817 * @msg: a message to insert into the error capture header
1819 * Should be called when an error is detected (either a hang or an error
1820 * interrupt) to capture error state from the time of the error. Fills
1821 * out a structure which becomes available in debugfs for user level tools
1824 void i915_capture_error_state(struct drm_i915_private *i915,
1825 intel_engine_mask_t engine_mask,
1829 struct i915_gpu_state *error;
1830 unsigned long flags;
1832 if (!i915_modparams.error_capture)
1835 if (READ_ONCE(i915->gpu_error.first_error))
1838 error = i915_capture_gpu_state(i915);
1842 dev_info(i915->drm.dev, "%s\n", error_msg(error, engine_mask, msg));
1844 if (!error->simulated) {
1845 spin_lock_irqsave(&i915->gpu_error.lock, flags);
1846 if (!i915->gpu_error.first_error) {
1847 i915->gpu_error.first_error = error;
1850 spin_unlock_irqrestore(&i915->gpu_error.lock, flags);
1854 __i915_gpu_state_free(&error->ref);
1859 ktime_get_real_seconds() - DRIVER_TIMESTAMP < DAY_AS_SECONDS(180)) {
1860 DRM_INFO("GPU hangs can indicate a bug anywhere in the entire gfx stack, including userspace.\n");
1861 DRM_INFO("Please file a _new_ bug report on bugs.freedesktop.org against DRI -> DRM/Intel\n");
1862 DRM_INFO("drm/i915 developers can then reassign to the right component if it's not a kernel issue.\n");
1863 DRM_INFO("The gpu crash dump is required to analyze gpu hangs, so please always attach it.\n");
1864 DRM_INFO("GPU crash dump saved to /sys/class/drm/card%d/error\n",
1865 i915->drm.primary->index);
1870 struct i915_gpu_state *
1871 i915_first_error_state(struct drm_i915_private *i915)
1873 struct i915_gpu_state *error;
1875 spin_lock_irq(&i915->gpu_error.lock);
1876 error = i915->gpu_error.first_error;
1877 if (!IS_ERR_OR_NULL(error))
1878 i915_gpu_state_get(error);
1879 spin_unlock_irq(&i915->gpu_error.lock);
1884 void i915_reset_error_state(struct drm_i915_private *i915)
1886 struct i915_gpu_state *error;
1888 spin_lock_irq(&i915->gpu_error.lock);
1889 error = i915->gpu_error.first_error;
1890 if (error != ERR_PTR(-ENODEV)) /* if disabled, always disabled */
1891 i915->gpu_error.first_error = NULL;
1892 spin_unlock_irq(&i915->gpu_error.lock);
1894 if (!IS_ERR_OR_NULL(error))
1895 i915_gpu_state_put(error);
1898 void i915_disable_error_state(struct drm_i915_private *i915, int err)
1900 spin_lock_irq(&i915->gpu_error.lock);
1901 if (!i915->gpu_error.first_error)
1902 i915->gpu_error.first_error = ERR_PTR(err);
1903 spin_unlock_irq(&i915->gpu_error.lock);