1// SPDX-License-Identifier: MIT
2/*
3 * Copyright �� 2023 Intel Corporation
4 */
5
6#include "xe_devcoredump.h"
7#include "xe_devcoredump_types.h"
8
9#include <linux/devcoredump.h>
10#include <generated/utsrelease.h>
11
12#include <drm/drm_managed.h>
13
14#include "xe_device.h"
15#include "xe_exec_queue.h"
16#include "xe_force_wake.h"
17#include "xe_gt.h"
18#include "xe_gt_printk.h"
19#include "xe_guc_ct.h"
20#include "xe_guc_submit.h"
21#include "xe_hw_engine.h"
22#include "xe_sched_job.h"
23#include "xe_vm.h"
24
25/**
26 * DOC: Xe device coredump
27 *
28 * Devices overview:
29 * Xe uses dev_coredump infrastructure for exposing the crash errors in a
30 * standardized way.
31 * devcoredump exposes a temporary device under /sys/class/devcoredump/
32 * which is linked with our card device directly.
33 * The core dump can be accessed either from
34 * /sys/class/drm/card<n>/device/devcoredump/ or from
35 * /sys/class/devcoredump/devcd<m> where
36 * /sys/class/devcoredump/devcd<m>/failing_device is a link to
37 * /sys/class/drm/card<n>/device/.
38 *
39 * Snapshot at hang:
40 * The 'data' file is printed with a drm_printer pointer at devcoredump read
41 * time. For this reason, we need to take snapshots from when the hang has
42 * happened, and not only when the user is reading the file. Otherwise the
43 * information is outdated since the resets might have happened in between.
44 *
45 * 'First' failure snapshot:
46 * In general, the first hang is the most critical one since the following hangs
47 * can be a consequence of the initial hang. For this reason we only take the
48 * snapshot of the 'first' failure and ignore subsequent calls of this function,
49 * at least while the coredump device is alive. Dev_coredump has a delayed work
50 * queue that will eventually delete the device and free all the dump
51 * information.
52 */
53
54#ifdef CONFIG_DEV_COREDUMP
55
56static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump)
57{
58	return container_of(coredump, struct xe_device, devcoredump);
59}
60
61static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q)
62{
63	return &q->gt->uc.guc;
64}
65
66static void xe_devcoredump_deferred_snap_work(struct work_struct *work)
67{
68	struct xe_devcoredump_snapshot *ss = container_of(work, typeof(*ss), work);
69
70	/* keep going if fw fails as we still want to save the memory and SW data */
71	if (xe_force_wake_get(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL))
72		xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n");
73	xe_vm_snapshot_capture_delayed(ss->vm);
74	xe_guc_exec_queue_snapshot_capture_delayed(ss->ge);
75	xe_force_wake_put(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL);
76}
77
78static ssize_t xe_devcoredump_read(char *buffer, loff_t offset,
79				   size_t count, void *data, size_t datalen)
80{
81	struct xe_devcoredump *coredump = data;
82	struct xe_device *xe;
83	struct xe_devcoredump_snapshot *ss;
84	struct drm_printer p;
85	struct drm_print_iterator iter;
86	struct timespec64 ts;
87	int i;
88
89	if (!coredump)
90		return -ENODEV;
91
92	xe = coredump_to_xe(coredump);
93	ss = &coredump->snapshot;
94
95	/* Ensure delayed work is captured before continuing */
96	flush_work(&ss->work);
97
98	iter.data = buffer;
99	iter.offset = 0;
100	iter.start = offset;
101	iter.remain = count;
102
103	p = drm_coredump_printer(&iter);
104
105	drm_printf(&p, "**** Xe Device Coredump ****\n");
106	drm_printf(&p, "kernel: " UTS_RELEASE "\n");
107	drm_printf(&p, "module: " KBUILD_MODNAME "\n");
108
109	ts = ktime_to_timespec64(ss->snapshot_time);
110	drm_printf(&p, "Snapshot time: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec);
111	ts = ktime_to_timespec64(ss->boot_time);
112	drm_printf(&p, "Uptime: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec);
113	xe_device_snapshot_print(xe, &p);
114
115	drm_printf(&p, "\n**** GuC CT ****\n");
116	xe_guc_ct_snapshot_print(coredump->snapshot.ct, &p);
117	xe_guc_exec_queue_snapshot_print(coredump->snapshot.ge, &p);
118
119	drm_printf(&p, "\n**** Job ****\n");
120	xe_sched_job_snapshot_print(coredump->snapshot.job, &p);
121
122	drm_printf(&p, "\n**** HW Engines ****\n");
123	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
124		if (coredump->snapshot.hwe[i])
125			xe_hw_engine_snapshot_print(coredump->snapshot.hwe[i],
126						    &p);
127	drm_printf(&p, "\n**** VM state ****\n");
128	xe_vm_snapshot_print(coredump->snapshot.vm, &p);
129
130	return count - iter.remain;
131}
132
133static void xe_devcoredump_free(void *data)
134{
135	struct xe_devcoredump *coredump = data;
136	int i;
137
138	/* Our device is gone. Nothing to do... */
139	if (!data || !coredump_to_xe(coredump))
140		return;
141
142	cancel_work_sync(&coredump->snapshot.work);
143
144	xe_guc_ct_snapshot_free(coredump->snapshot.ct);
145	xe_guc_exec_queue_snapshot_free(coredump->snapshot.ge);
146	xe_sched_job_snapshot_free(coredump->snapshot.job);
147	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
148		if (coredump->snapshot.hwe[i])
149			xe_hw_engine_snapshot_free(coredump->snapshot.hwe[i]);
150	xe_vm_snapshot_free(coredump->snapshot.vm);
151
152	/* To prevent stale data on next snapshot, clear everything */
153	memset(&coredump->snapshot, 0, sizeof(coredump->snapshot));
154	coredump->captured = false;
155	drm_info(&coredump_to_xe(coredump)->drm,
156		 "Xe device coredump has been deleted.\n");
157}
158
159static void devcoredump_snapshot(struct xe_devcoredump *coredump,
160				 struct xe_sched_job *job)
161{
162	struct xe_devcoredump_snapshot *ss = &coredump->snapshot;
163	struct xe_exec_queue *q = job->q;
164	struct xe_guc *guc = exec_queue_to_guc(q);
165	struct xe_hw_engine *hwe;
166	enum xe_hw_engine_id id;
167	u32 adj_logical_mask = q->logical_mask;
168	u32 width_mask = (0x1 << q->width) - 1;
169	int i;
170	bool cookie;
171
172	ss->snapshot_time = ktime_get_real();
173	ss->boot_time = ktime_get_boottime();
174
175	ss->gt = q->gt;
176	INIT_WORK(&ss->work, xe_devcoredump_deferred_snap_work);
177
178	cookie = dma_fence_begin_signalling();
179	for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) {
180		if (adj_logical_mask & BIT(i)) {
181			adj_logical_mask |= width_mask << i;
182			i += q->width;
183		} else {
184			++i;
185		}
186	}
187
188	/* keep going if fw fails as we still want to save the memory and SW data */
189	if (xe_force_wake_get(gt_to_fw(q->gt), XE_FORCEWAKE_ALL))
190		xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n");
191
192	coredump->snapshot.ct = xe_guc_ct_snapshot_capture(&guc->ct, true);
193	coredump->snapshot.ge = xe_guc_exec_queue_snapshot_capture(q);
194	coredump->snapshot.job = xe_sched_job_snapshot_capture(job);
195	coredump->snapshot.vm = xe_vm_snapshot_capture(q->vm);
196
197	for_each_hw_engine(hwe, q->gt, id) {
198		if (hwe->class != q->hwe->class ||
199		    !(BIT(hwe->logical_instance) & adj_logical_mask)) {
200			coredump->snapshot.hwe[id] = NULL;
201			continue;
202		}
203		coredump->snapshot.hwe[id] = xe_hw_engine_snapshot_capture(hwe);
204	}
205
206	queue_work(system_unbound_wq, &ss->work);
207
208	xe_force_wake_put(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
209	dma_fence_end_signalling(cookie);
210}
211
212/**
213 * xe_devcoredump - Take the required snapshots and initialize coredump device.
214 * @job: The faulty xe_sched_job, where the issue was detected.
215 *
216 * This function should be called at the crash time within the serialized
217 * gt_reset. It is skipped if we still have the core dump device available
218 * with the information of the 'first' snapshot.
219 */
220void xe_devcoredump(struct xe_sched_job *job)
221{
222	struct xe_device *xe = gt_to_xe(job->q->gt);
223	struct xe_devcoredump *coredump = &xe->devcoredump;
224
225	if (coredump->captured) {
226		drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n");
227		return;
228	}
229
230	coredump->captured = true;
231	devcoredump_snapshot(coredump, job);
232
233	drm_info(&xe->drm, "Xe device coredump has been created\n");
234	drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n",
235		 xe->drm.primary->index);
236
237	dev_coredumpm(xe->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL,
238		      xe_devcoredump_read, xe_devcoredump_free);
239}
240
241static void xe_driver_devcoredump_fini(struct drm_device *drm, void *arg)
242{
243	dev_coredump_put(drm->dev);
244}
245
246int xe_devcoredump_init(struct xe_device *xe)
247{
248	return drmm_add_action_or_reset(&xe->drm, xe_driver_devcoredump_fini, xe);
249}
250#endif
251