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