xref: /linux/drivers/gpu/drm/xe/xe_devcoredump.c (revision 1623bc27a85a93e82194c8d077eccc464efa67db)
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/ascii85.h>
10 #include <linux/devcoredump.h>
11 #include <generated/utsrelease.h>
12 
13 #include <drm/drm_managed.h>
14 
15 #include "xe_device.h"
16 #include "xe_exec_queue.h"
17 #include "xe_force_wake.h"
18 #include "xe_gt.h"
19 #include "xe_gt_printk.h"
20 #include "xe_guc_capture.h"
21 #include "xe_guc_ct.h"
22 #include "xe_guc_log.h"
23 #include "xe_guc_submit.h"
24 #include "xe_hw_engine.h"
25 #include "xe_module.h"
26 #include "xe_pm.h"
27 #include "xe_sched_job.h"
28 #include "xe_vm.h"
29 
30 /**
31  * DOC: Xe device coredump
32  *
33  * Devices overview:
34  * Xe uses dev_coredump infrastructure for exposing the crash errors in a
35  * standardized way.
36  * devcoredump exposes a temporary device under /sys/class/devcoredump/
37  * which is linked with our card device directly.
38  * The core dump can be accessed either from
39  * /sys/class/drm/card<n>/device/devcoredump/ or from
40  * /sys/class/devcoredump/devcd<m> where
41  * /sys/class/devcoredump/devcd<m>/failing_device is a link to
42  * /sys/class/drm/card<n>/device/.
43  *
44  * Snapshot at hang:
45  * The 'data' file is printed with a drm_printer pointer at devcoredump read
46  * time. For this reason, we need to take snapshots from when the hang has
47  * happened, and not only when the user is reading the file. Otherwise the
48  * information is outdated since the resets might have happened in between.
49  *
50  * 'First' failure snapshot:
51  * In general, the first hang is the most critical one since the following hangs
52  * can be a consequence of the initial hang. For this reason we only take the
53  * snapshot of the 'first' failure and ignore subsequent calls of this function,
54  * at least while the coredump device is alive. Dev_coredump has a delayed work
55  * queue that will eventually delete the device and free all the dump
56  * information.
57  */
58 
59 #ifdef CONFIG_DEV_COREDUMP
60 
61 /* 1 hour timeout */
62 #define XE_COREDUMP_TIMEOUT_JIFFIES (60 * 60 * HZ)
63 
64 static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump)
65 {
66 	return container_of(coredump, struct xe_device, devcoredump);
67 }
68 
69 static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q)
70 {
71 	return &q->gt->uc.guc;
72 }
73 
74 static ssize_t __xe_devcoredump_read(char *buffer, size_t count,
75 				     struct xe_devcoredump *coredump)
76 {
77 	struct xe_device *xe;
78 	struct xe_devcoredump_snapshot *ss;
79 	struct drm_printer p;
80 	struct drm_print_iterator iter;
81 	struct timespec64 ts;
82 	int i;
83 
84 	xe = coredump_to_xe(coredump);
85 	ss = &coredump->snapshot;
86 
87 	iter.data = buffer;
88 	iter.start = 0;
89 	iter.remain = count;
90 
91 	p = drm_coredump_printer(&iter);
92 
93 	drm_puts(&p, "**** Xe Device Coredump ****\n");
94 	drm_puts(&p, "kernel: " UTS_RELEASE "\n");
95 	drm_puts(&p, "module: " KBUILD_MODNAME "\n");
96 
97 	ts = ktime_to_timespec64(ss->snapshot_time);
98 	drm_printf(&p, "Snapshot time: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec);
99 	ts = ktime_to_timespec64(ss->boot_time);
100 	drm_printf(&p, "Uptime: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec);
101 	drm_printf(&p, "Process: %s\n", ss->process_name);
102 	xe_device_snapshot_print(xe, &p);
103 
104 	drm_printf(&p, "\n**** GT #%d ****\n", ss->gt->info.id);
105 	drm_printf(&p, "\tTile: %d\n", ss->gt->tile->id);
106 
107 	drm_puts(&p, "\n**** GuC Log ****\n");
108 	xe_guc_log_snapshot_print(ss->guc.log, &p);
109 	drm_puts(&p, "\n**** GuC CT ****\n");
110 	xe_guc_ct_snapshot_print(ss->guc.ct, &p);
111 
112 	/*
113 	 * Don't add a new section header here because the mesa debug decoder
114 	 * tool expects the context information to be in the 'GuC CT' section.
115 	 */
116 	/* drm_puts(&p, "\n**** Contexts ****\n"); */
117 	xe_guc_exec_queue_snapshot_print(ss->ge, &p);
118 
119 	drm_puts(&p, "\n**** Job ****\n");
120 	xe_sched_job_snapshot_print(ss->job, &p);
121 
122 	drm_puts(&p, "\n**** HW Engines ****\n");
123 	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
124 		if (ss->hwe[i])
125 			xe_engine_snapshot_print(ss->hwe[i], &p);
126 
127 	drm_puts(&p, "\n**** VM state ****\n");
128 	xe_vm_snapshot_print(ss->vm, &p);
129 
130 	return count - iter.remain;
131 }
132 
133 static void xe_devcoredump_snapshot_free(struct xe_devcoredump_snapshot *ss)
134 {
135 	int i;
136 
137 	xe_guc_log_snapshot_free(ss->guc.log);
138 	ss->guc.log = NULL;
139 
140 	xe_guc_ct_snapshot_free(ss->guc.ct);
141 	ss->guc.ct = NULL;
142 
143 	xe_guc_capture_put_matched_nodes(&ss->gt->uc.guc);
144 	ss->matched_node = NULL;
145 
146 	xe_guc_exec_queue_snapshot_free(ss->ge);
147 	ss->ge = NULL;
148 
149 	xe_sched_job_snapshot_free(ss->job);
150 	ss->job = NULL;
151 
152 	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
153 		if (ss->hwe[i]) {
154 			xe_hw_engine_snapshot_free(ss->hwe[i]);
155 			ss->hwe[i] = NULL;
156 		}
157 
158 	xe_vm_snapshot_free(ss->vm);
159 	ss->vm = NULL;
160 }
161 
162 static ssize_t xe_devcoredump_read(char *buffer, loff_t offset,
163 				   size_t count, void *data, size_t datalen)
164 {
165 	struct xe_devcoredump *coredump = data;
166 	struct xe_devcoredump_snapshot *ss;
167 	ssize_t byte_copied;
168 
169 	if (!coredump)
170 		return -ENODEV;
171 
172 	ss = &coredump->snapshot;
173 
174 	/* Ensure delayed work is captured before continuing */
175 	flush_work(&ss->work);
176 
177 	if (!ss->read.buffer)
178 		return -ENODEV;
179 
180 	if (offset >= ss->read.size)
181 		return 0;
182 
183 	byte_copied = count < ss->read.size - offset ? count :
184 		ss->read.size - offset;
185 	memcpy(buffer, ss->read.buffer + offset, byte_copied);
186 
187 	return byte_copied;
188 }
189 
190 static void xe_devcoredump_free(void *data)
191 {
192 	struct xe_devcoredump *coredump = data;
193 
194 	/* Our device is gone. Nothing to do... */
195 	if (!data || !coredump_to_xe(coredump))
196 		return;
197 
198 	cancel_work_sync(&coredump->snapshot.work);
199 
200 	xe_devcoredump_snapshot_free(&coredump->snapshot);
201 	kvfree(coredump->snapshot.read.buffer);
202 
203 	/* To prevent stale data on next snapshot, clear everything */
204 	memset(&coredump->snapshot, 0, sizeof(coredump->snapshot));
205 	coredump->captured = false;
206 	coredump->job = NULL;
207 	drm_info(&coredump_to_xe(coredump)->drm,
208 		 "Xe device coredump has been deleted.\n");
209 }
210 
211 static void xe_devcoredump_deferred_snap_work(struct work_struct *work)
212 {
213 	struct xe_devcoredump_snapshot *ss = container_of(work, typeof(*ss), work);
214 	struct xe_devcoredump *coredump = container_of(ss, typeof(*coredump), snapshot);
215 	struct xe_device *xe = coredump_to_xe(coredump);
216 	unsigned int fw_ref;
217 
218 	/*
219 	 * NB: Despite passing a GFP_ flags parameter here, more allocations are done
220 	 * internally using GFP_KERNEL expliictly. Hence this call must be in the worker
221 	 * thread and not in the initial capture call.
222 	 */
223 	dev_coredumpm_timeout(gt_to_xe(ss->gt)->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL,
224 			      xe_devcoredump_read, xe_devcoredump_free,
225 			      XE_COREDUMP_TIMEOUT_JIFFIES);
226 
227 	xe_pm_runtime_get(xe);
228 
229 	/* keep going if fw fails as we still want to save the memory and SW data */
230 	fw_ref = xe_force_wake_get(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL);
231 	if (!xe_force_wake_ref_has_domain(fw_ref, XE_FORCEWAKE_ALL))
232 		xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n");
233 	xe_vm_snapshot_capture_delayed(ss->vm);
234 	xe_guc_exec_queue_snapshot_capture_delayed(ss->ge);
235 	xe_force_wake_put(gt_to_fw(ss->gt), fw_ref);
236 
237 	xe_pm_runtime_put(xe);
238 
239 	/* Calculate devcoredump size */
240 	ss->read.size = __xe_devcoredump_read(NULL, INT_MAX, coredump);
241 
242 	ss->read.buffer = kvmalloc(ss->read.size, GFP_USER);
243 	if (!ss->read.buffer)
244 		return;
245 
246 	__xe_devcoredump_read(ss->read.buffer, ss->read.size, coredump);
247 	xe_devcoredump_snapshot_free(ss);
248 }
249 
250 static void devcoredump_snapshot(struct xe_devcoredump *coredump,
251 				 struct xe_sched_job *job)
252 {
253 	struct xe_devcoredump_snapshot *ss = &coredump->snapshot;
254 	struct xe_exec_queue *q = job->q;
255 	struct xe_guc *guc = exec_queue_to_guc(q);
256 	u32 adj_logical_mask = q->logical_mask;
257 	u32 width_mask = (0x1 << q->width) - 1;
258 	const char *process_name = "no process";
259 
260 	unsigned int fw_ref;
261 	bool cookie;
262 	int i;
263 
264 	ss->snapshot_time = ktime_get_real();
265 	ss->boot_time = ktime_get_boottime();
266 
267 	if (q->vm && q->vm->xef)
268 		process_name = q->vm->xef->process_name;
269 	strscpy(ss->process_name, process_name);
270 
271 	ss->gt = q->gt;
272 	coredump->job = job;
273 	INIT_WORK(&ss->work, xe_devcoredump_deferred_snap_work);
274 
275 	cookie = dma_fence_begin_signalling();
276 	for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) {
277 		if (adj_logical_mask & BIT(i)) {
278 			adj_logical_mask |= width_mask << i;
279 			i += q->width;
280 		} else {
281 			++i;
282 		}
283 	}
284 
285 	/* keep going if fw fails as we still want to save the memory and SW data */
286 	fw_ref = xe_force_wake_get(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
287 
288 	ss->guc.log = xe_guc_log_snapshot_capture(&guc->log, true);
289 	ss->guc.ct = xe_guc_ct_snapshot_capture(&guc->ct);
290 	ss->ge = xe_guc_exec_queue_snapshot_capture(q);
291 	ss->job = xe_sched_job_snapshot_capture(job);
292 	ss->vm = xe_vm_snapshot_capture(q->vm);
293 
294 	xe_engine_snapshot_capture_for_job(job);
295 
296 	queue_work(system_unbound_wq, &ss->work);
297 
298 	xe_force_wake_put(gt_to_fw(q->gt), fw_ref);
299 	dma_fence_end_signalling(cookie);
300 }
301 
302 /**
303  * xe_devcoredump - Take the required snapshots and initialize coredump device.
304  * @job: The faulty xe_sched_job, where the issue was detected.
305  *
306  * This function should be called at the crash time within the serialized
307  * gt_reset. It is skipped if we still have the core dump device available
308  * with the information of the 'first' snapshot.
309  */
310 void xe_devcoredump(struct xe_sched_job *job)
311 {
312 	struct xe_device *xe = gt_to_xe(job->q->gt);
313 	struct xe_devcoredump *coredump = &xe->devcoredump;
314 
315 	if (coredump->captured) {
316 		drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n");
317 		return;
318 	}
319 
320 	coredump->captured = true;
321 	devcoredump_snapshot(coredump, job);
322 
323 	drm_info(&xe->drm, "Xe device coredump has been created\n");
324 	drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n",
325 		 xe->drm.primary->index);
326 }
327 
328 static void xe_driver_devcoredump_fini(void *arg)
329 {
330 	struct drm_device *drm = arg;
331 
332 	dev_coredump_put(drm->dev);
333 }
334 
335 int xe_devcoredump_init(struct xe_device *xe)
336 {
337 	return devm_add_action_or_reset(xe->drm.dev, xe_driver_devcoredump_fini, &xe->drm);
338 }
339 
340 #endif
341 
342 /**
343  * xe_print_blob_ascii85 - print a BLOB to some useful location in ASCII85
344  *
345  * The output is split to multiple lines because some print targets, e.g. dmesg
346  * cannot handle arbitrarily long lines. Note also that printing to dmesg in
347  * piece-meal fashion is not possible, each separate call to drm_puts() has a
348  * line-feed automatically added! Therefore, the entire output line must be
349  * constructed in a local buffer first, then printed in one atomic output call.
350  *
351  * There is also a scheduler yield call to prevent the 'task has been stuck for
352  * 120s' kernel hang check feature from firing when printing to a slow target
353  * such as dmesg over a serial port.
354  *
355  * TODO: Add compression prior to the ASCII85 encoding to shrink huge buffers down.
356  *
357  * @p: the printer object to output to
358  * @prefix: optional prefix to add to output string
359  * @blob: the Binary Large OBject to dump out
360  * @offset: offset in bytes to skip from the front of the BLOB, must be a multiple of sizeof(u32)
361  * @size: the size in bytes of the BLOB, must be a multiple of sizeof(u32)
362  */
363 void xe_print_blob_ascii85(struct drm_printer *p, const char *prefix,
364 			   const void *blob, size_t offset, size_t size)
365 {
366 	const u32 *blob32 = (const u32 *)blob;
367 	char buff[ASCII85_BUFSZ], *line_buff;
368 	size_t line_pos = 0;
369 
370 	/*
371 	 * Splitting blobs across multiple lines is not compatible with the mesa
372 	 * debug decoder tool. Note that even dropping the explicit '\n' below
373 	 * doesn't help because the GuC log is so big some underlying implementation
374 	 * still splits the lines at 512K characters. So just bail completely for
375 	 * the moment.
376 	 */
377 	return;
378 
379 #define DMESG_MAX_LINE_LEN	800
380 #define MIN_SPACE		(ASCII85_BUFSZ + 2)		/* 85 + "\n\0" */
381 
382 	if (size & 3)
383 		drm_printf(p, "Size not word aligned: %zu", size);
384 	if (offset & 3)
385 		drm_printf(p, "Offset not word aligned: %zu", size);
386 
387 	line_buff = kzalloc(DMESG_MAX_LINE_LEN, GFP_KERNEL);
388 	if (IS_ERR_OR_NULL(line_buff)) {
389 		drm_printf(p, "Failed to allocate line buffer: %pe", line_buff);
390 		return;
391 	}
392 
393 	blob32 += offset / sizeof(*blob32);
394 	size /= sizeof(*blob32);
395 
396 	if (prefix) {
397 		strscpy(line_buff, prefix, DMESG_MAX_LINE_LEN - MIN_SPACE - 2);
398 		line_pos = strlen(line_buff);
399 
400 		line_buff[line_pos++] = ':';
401 		line_buff[line_pos++] = ' ';
402 	}
403 
404 	while (size--) {
405 		u32 val = *(blob32++);
406 
407 		strscpy(line_buff + line_pos, ascii85_encode(val, buff),
408 			DMESG_MAX_LINE_LEN - line_pos);
409 		line_pos += strlen(line_buff + line_pos);
410 
411 		if ((line_pos + MIN_SPACE) >= DMESG_MAX_LINE_LEN) {
412 			line_buff[line_pos++] = '\n';
413 			line_buff[line_pos++] = 0;
414 
415 			drm_puts(p, line_buff);
416 
417 			line_pos = 0;
418 
419 			/* Prevent 'stuck thread' time out errors */
420 			cond_resched();
421 		}
422 	}
423 
424 	if (line_pos) {
425 		line_buff[line_pos++] = '\n';
426 		line_buff[line_pos++] = 0;
427 
428 		drm_puts(p, line_buff);
429 	}
430 
431 	kfree(line_buff);
432 
433 #undef MIN_SPACE
434 #undef DMESG_MAX_LINE_LEN
435 }
436