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