1 // SPDX-License-Identifier: MIT
3 * Copyright © 2023 Intel Corporation
6 #include "xe_devcoredump.h"
7 #include "xe_devcoredump_types.h"
9 #include <linux/devcoredump.h>
10 #include <generated/utsrelease.h>
12 #include "xe_device.h"
13 #include "xe_exec_queue.h"
14 #include "xe_force_wake.h"
16 #include "xe_guc_ct.h"
17 #include "xe_guc_submit.h"
18 #include "xe_hw_engine.h"
21 * DOC: Xe device coredump
24 * Xe uses dev_coredump infrastructure for exposing the crash errors in a
26 * devcoredump exposes a temporary device under /sys/class/devcoredump/
27 * which is linked with our card device directly.
28 * The core dump can be accessed either from
29 * /sys/class/drm/card<n>/device/devcoredump/ or from
30 * /sys/class/devcoredump/devcd<m> where
31 * /sys/class/devcoredump/devcd<m>/failing_device is a link to
32 * /sys/class/drm/card<n>/device/.
35 * The 'data' file is printed with a drm_printer pointer at devcoredump read
36 * time. For this reason, we need to take snapshots from when the hang has
37 * happened, and not only when the user is reading the file. Otherwise the
38 * information is outdated since the resets might have happened in between.
40 * 'First' failure snapshot:
41 * In general, the first hang is the most critical one since the following hangs
42 * can be a consequence of the initial hang. For this reason we only take the
43 * snapshot of the 'first' failure and ignore subsequent calls of this function,
44 * at least while the coredump device is alive. Dev_coredump has a delayed work
45 * queue that will eventually delete the device and free all the dump
49 #ifdef CONFIG_DEV_COREDUMP
51 static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump)
53 return container_of(coredump, struct xe_device, devcoredump);
56 static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q)
58 return &q->gt->uc.guc;
61 static ssize_t xe_devcoredump_read(char *buffer, loff_t offset,
62 size_t count, void *data, size_t datalen)
64 struct xe_devcoredump *coredump = data;
65 struct xe_devcoredump_snapshot *ss;
67 struct drm_print_iterator iter;
71 /* Our device is gone already... */
72 if (!data || !coredump_to_xe(coredump))
80 ss = &coredump->snapshot;
81 p = drm_coredump_printer(&iter);
83 drm_printf(&p, "**** Xe Device Coredump ****\n");
84 drm_printf(&p, "kernel: " UTS_RELEASE "\n");
85 drm_printf(&p, "module: " KBUILD_MODNAME "\n");
87 ts = ktime_to_timespec64(ss->snapshot_time);
88 drm_printf(&p, "Snapshot time: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec);
89 ts = ktime_to_timespec64(ss->boot_time);
90 drm_printf(&p, "Uptime: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec);
92 drm_printf(&p, "\n**** GuC CT ****\n");
93 xe_guc_ct_snapshot_print(coredump->snapshot.ct, &p);
94 xe_guc_exec_queue_snapshot_print(coredump->snapshot.ge, &p);
96 drm_printf(&p, "\n**** HW Engines ****\n");
97 for (i = 0; i < XE_NUM_HW_ENGINES; i++)
98 if (coredump->snapshot.hwe[i])
99 xe_hw_engine_snapshot_print(coredump->snapshot.hwe[i],
102 return count - iter.remain;
105 static void xe_devcoredump_free(void *data)
107 struct xe_devcoredump *coredump = data;
110 /* Our device is gone. Nothing to do... */
111 if (!data || !coredump_to_xe(coredump))
114 xe_guc_ct_snapshot_free(coredump->snapshot.ct);
115 xe_guc_exec_queue_snapshot_free(coredump->snapshot.ge);
116 for (i = 0; i < XE_NUM_HW_ENGINES; i++)
117 if (coredump->snapshot.hwe[i])
118 xe_hw_engine_snapshot_free(coredump->snapshot.hwe[i]);
120 coredump->captured = false;
121 drm_info(&coredump_to_xe(coredump)->drm,
122 "Xe device coredump has been deleted.\n");
125 static void devcoredump_snapshot(struct xe_devcoredump *coredump,
126 struct xe_exec_queue *q)
128 struct xe_devcoredump_snapshot *ss = &coredump->snapshot;
129 struct xe_guc *guc = exec_queue_to_guc(q);
130 struct xe_hw_engine *hwe;
131 enum xe_hw_engine_id id;
132 u32 adj_logical_mask = q->logical_mask;
133 u32 width_mask = (0x1 << q->width) - 1;
137 ss->snapshot_time = ktime_get_real();
138 ss->boot_time = ktime_get_boottime();
140 cookie = dma_fence_begin_signalling();
141 for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) {
142 if (adj_logical_mask & BIT(i)) {
143 adj_logical_mask |= width_mask << i;
150 xe_force_wake_get(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
152 coredump->snapshot.ct = xe_guc_ct_snapshot_capture(&guc->ct, true);
153 coredump->snapshot.ge = xe_guc_exec_queue_snapshot_capture(q);
155 for_each_hw_engine(hwe, q->gt, id) {
156 if (hwe->class != q->hwe->class ||
157 !(BIT(hwe->logical_instance) & adj_logical_mask)) {
158 coredump->snapshot.hwe[id] = NULL;
161 coredump->snapshot.hwe[id] = xe_hw_engine_snapshot_capture(hwe);
164 xe_force_wake_put(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
165 dma_fence_end_signalling(cookie);
169 * xe_devcoredump - Take the required snapshots and initialize coredump device.
170 * @q: The faulty xe_exec_queue, where the issue was detected.
172 * This function should be called at the crash time within the serialized
173 * gt_reset. It is skipped if we still have the core dump device available
174 * with the information of the 'first' snapshot.
176 void xe_devcoredump(struct xe_exec_queue *q)
178 struct xe_device *xe = gt_to_xe(q->gt);
179 struct xe_devcoredump *coredump = &xe->devcoredump;
181 if (coredump->captured) {
182 drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n");
186 coredump->captured = true;
187 devcoredump_snapshot(coredump, q);
189 drm_info(&xe->drm, "Xe device coredump has been created\n");
190 drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n",
191 xe->drm.primary->index);
193 dev_coredumpm(xe->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL,
194 xe_devcoredump_read, xe_devcoredump_free);