summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/xe/xe_devcoredump.c
blob: bdb76e834e4c369e0b3515c6b176bcac23f9cd8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2023 Intel Corporation
 */

#include "xe_devcoredump.h"
#include "xe_devcoredump_types.h"

#include <linux/devcoredump.h>
#include <generated/utsrelease.h>

#include <drm/drm_managed.h>

#include "xe_device.h"
#include "xe_exec_queue.h"
#include "xe_force_wake.h"
#include "xe_gt.h"
#include "xe_gt_printk.h"
#include "xe_guc_ct.h"
#include "xe_guc_submit.h"
#include "xe_hw_engine.h"
#include "xe_sched_job.h"
#include "xe_vm.h"

/**
 * DOC: Xe device coredump
 *
 * Devices overview:
 * Xe uses dev_coredump infrastructure for exposing the crash errors in a
 * standardized way.
 * devcoredump exposes a temporary device under /sys/class/devcoredump/
 * which is linked with our card device directly.
 * The core dump can be accessed either from
 * /sys/class/drm/card<n>/device/devcoredump/ or from
 * /sys/class/devcoredump/devcd<m> where
 * /sys/class/devcoredump/devcd<m>/failing_device is a link to
 * /sys/class/drm/card<n>/device/.
 *
 * Snapshot at hang:
 * The 'data' file is printed with a drm_printer pointer at devcoredump read
 * time. For this reason, we need to take snapshots from when the hang has
 * happened, and not only when the user is reading the file. Otherwise the
 * information is outdated since the resets might have happened in between.
 *
 * 'First' failure snapshot:
 * In general, the first hang is the most critical one since the following hangs
 * can be a consequence of the initial hang. For this reason we only take the
 * snapshot of the 'first' failure and ignore subsequent calls of this function,
 * at least while the coredump device is alive. Dev_coredump has a delayed work
 * queue that will eventually delete the device and free all the dump
 * information.
 */

#ifdef CONFIG_DEV_COREDUMP

/* 1 hour timeout */
#define XE_COREDUMP_TIMEOUT_JIFFIES (60 * 60 * HZ)

static struct xe_device *coredump_to_xe(const struct xe_devcoredump *coredump)
{
	return container_of(coredump, struct xe_device, devcoredump);
}

static struct xe_guc *exec_queue_to_guc(struct xe_exec_queue *q)
{
	return &q->gt->uc.guc;
}

static ssize_t __xe_devcoredump_read(char *buffer, size_t count,
				     struct xe_devcoredump *coredump)
{
	struct xe_device *xe;
	struct xe_devcoredump_snapshot *ss;
	struct drm_printer p;
	struct drm_print_iterator iter;
	struct timespec64 ts;
	int i;

	xe = coredump_to_xe(coredump);
	ss = &coredump->snapshot;

	iter.data = buffer;
	iter.start = 0;
	iter.remain = count;

	p = drm_coredump_printer(&iter);

	drm_printf(&p, "**** Xe Device Coredump ****\n");
	drm_printf(&p, "kernel: " UTS_RELEASE "\n");
	drm_printf(&p, "module: " KBUILD_MODNAME "\n");

	ts = ktime_to_timespec64(ss->snapshot_time);
	drm_printf(&p, "Snapshot time: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec);
	ts = ktime_to_timespec64(ss->boot_time);
	drm_printf(&p, "Uptime: %lld.%09ld\n", ts.tv_sec, ts.tv_nsec);
	drm_printf(&p, "Process: %s\n", ss->process_name);
	xe_device_snapshot_print(xe, &p);

	drm_printf(&p, "\n**** GuC CT ****\n");
	xe_guc_ct_snapshot_print(coredump->snapshot.ct, &p);
	xe_guc_exec_queue_snapshot_print(coredump->snapshot.ge, &p);

	drm_printf(&p, "\n**** Job ****\n");
	xe_sched_job_snapshot_print(coredump->snapshot.job, &p);

	drm_printf(&p, "\n**** HW Engines ****\n");
	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
		if (coredump->snapshot.hwe[i])
			xe_hw_engine_snapshot_print(coredump->snapshot.hwe[i],
						    &p);
	drm_printf(&p, "\n**** VM state ****\n");
	xe_vm_snapshot_print(coredump->snapshot.vm, &p);

	return count - iter.remain;
}

static void xe_devcoredump_snapshot_free(struct xe_devcoredump_snapshot *ss)
{
	int i;

	xe_guc_ct_snapshot_free(ss->ct);
	ss->ct = NULL;

	xe_guc_exec_queue_snapshot_free(ss->ge);
	ss->ge = NULL;

	xe_sched_job_snapshot_free(ss->job);
	ss->job = NULL;

	for (i = 0; i < XE_NUM_HW_ENGINES; i++)
		if (ss->hwe[i]) {
			xe_hw_engine_snapshot_free(ss->hwe[i]);
			ss->hwe[i] = NULL;
		}

	xe_vm_snapshot_free(ss->vm);
	ss->vm = NULL;
}

static void xe_devcoredump_deferred_snap_work(struct work_struct *work)
{
	struct xe_devcoredump_snapshot *ss = container_of(work, typeof(*ss), work);
	struct xe_devcoredump *coredump = container_of(ss, typeof(*coredump), snapshot);

	/* keep going if fw fails as we still want to save the memory and SW data */
	if (xe_force_wake_get(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL))
		xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n");
	xe_vm_snapshot_capture_delayed(ss->vm);
	xe_guc_exec_queue_snapshot_capture_delayed(ss->ge);
	xe_force_wake_put(gt_to_fw(ss->gt), XE_FORCEWAKE_ALL);

	/* Calculate devcoredump size */
	ss->read.size = __xe_devcoredump_read(NULL, INT_MAX, coredump);

	ss->read.buffer = kvmalloc(ss->read.size, GFP_USER);
	if (!ss->read.buffer)
		return;

	__xe_devcoredump_read(ss->read.buffer, ss->read.size, coredump);
	xe_devcoredump_snapshot_free(ss);
}

static ssize_t xe_devcoredump_read(char *buffer, loff_t offset,
				   size_t count, void *data, size_t datalen)
{
	struct xe_devcoredump *coredump = data;
	struct xe_devcoredump_snapshot *ss;
	ssize_t byte_copied;

	if (!coredump)
		return -ENODEV;

	ss = &coredump->snapshot;

	/* Ensure delayed work is captured before continuing */
	flush_work(&ss->work);

	if (!ss->read.buffer)
		return -ENODEV;

	if (offset >= ss->read.size)
		return 0;

	byte_copied = count < ss->read.size - offset ? count :
		ss->read.size - offset;
	memcpy(buffer, ss->read.buffer + offset, byte_copied);

	return byte_copied;
}

static void xe_devcoredump_free(void *data)
{
	struct xe_devcoredump *coredump = data;

	/* Our device is gone. Nothing to do... */
	if (!data || !coredump_to_xe(coredump))
		return;

	cancel_work_sync(&coredump->snapshot.work);

	xe_devcoredump_snapshot_free(&coredump->snapshot);
	kvfree(coredump->snapshot.read.buffer);

	/* To prevent stale data on next snapshot, clear everything */
	memset(&coredump->snapshot, 0, sizeof(coredump->snapshot));
	coredump->captured = false;
	drm_info(&coredump_to_xe(coredump)->drm,
		 "Xe device coredump has been deleted.\n");
}

static void devcoredump_snapshot(struct xe_devcoredump *coredump,
				 struct xe_sched_job *job)
{
	struct xe_devcoredump_snapshot *ss = &coredump->snapshot;
	struct xe_exec_queue *q = job->q;
	struct xe_guc *guc = exec_queue_to_guc(q);
	struct xe_hw_engine *hwe;
	enum xe_hw_engine_id id;
	u32 adj_logical_mask = q->logical_mask;
	u32 width_mask = (0x1 << q->width) - 1;
	const char *process_name = "no process";

	int i;
	bool cookie;

	ss->snapshot_time = ktime_get_real();
	ss->boot_time = ktime_get_boottime();

	if (q->vm && q->vm->xef)
		process_name = q->vm->xef->process_name;
	strscpy(ss->process_name, process_name);

	ss->gt = q->gt;
	INIT_WORK(&ss->work, xe_devcoredump_deferred_snap_work);

	cookie = dma_fence_begin_signalling();
	for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) {
		if (adj_logical_mask & BIT(i)) {
			adj_logical_mask |= width_mask << i;
			i += q->width;
		} else {
			++i;
		}
	}

	/* keep going if fw fails as we still want to save the memory and SW data */
	if (xe_force_wake_get(gt_to_fw(q->gt), XE_FORCEWAKE_ALL))
		xe_gt_info(ss->gt, "failed to get forcewake for coredump capture\n");

	coredump->snapshot.ct = xe_guc_ct_snapshot_capture(&guc->ct, true);
	coredump->snapshot.ge = xe_guc_exec_queue_snapshot_capture(q);
	coredump->snapshot.job = xe_sched_job_snapshot_capture(job);
	coredump->snapshot.vm = xe_vm_snapshot_capture(q->vm);

	for_each_hw_engine(hwe, q->gt, id) {
		if (hwe->class != q->hwe->class ||
		    !(BIT(hwe->logical_instance) & adj_logical_mask)) {
			coredump->snapshot.hwe[id] = NULL;
			continue;
		}
		coredump->snapshot.hwe[id] = xe_hw_engine_snapshot_capture(hwe);
	}

	queue_work(system_unbound_wq, &ss->work);

	xe_force_wake_put(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
	dma_fence_end_signalling(cookie);
}

/**
 * xe_devcoredump - Take the required snapshots and initialize coredump device.
 * @job: The faulty xe_sched_job, where the issue was detected.
 *
 * This function should be called at the crash time within the serialized
 * gt_reset. It is skipped if we still have the core dump device available
 * with the information of the 'first' snapshot.
 */
void xe_devcoredump(struct xe_sched_job *job)
{
	struct xe_device *xe = gt_to_xe(job->q->gt);
	struct xe_devcoredump *coredump = &xe->devcoredump;

	if (coredump->captured) {
		drm_dbg(&xe->drm, "Multiple hangs are occurring, but only the first snapshot was taken\n");
		return;
	}

	coredump->captured = true;
	devcoredump_snapshot(coredump, job);

	drm_info(&xe->drm, "Xe device coredump has been created\n");
	drm_info(&xe->drm, "Check your /sys/class/drm/card%d/device/devcoredump/data\n",
		 xe->drm.primary->index);

	dev_coredumpm_timeout(xe->drm.dev, THIS_MODULE, coredump, 0, GFP_KERNEL,
			      xe_devcoredump_read, xe_devcoredump_free,
			      XE_COREDUMP_TIMEOUT_JIFFIES);
}

static void xe_driver_devcoredump_fini(void *arg)
{
	struct drm_device *drm = arg;

	dev_coredump_put(drm->dev);
}

int xe_devcoredump_init(struct xe_device *xe)
{
	return devm_add_action_or_reset(xe->drm.dev, xe_driver_devcoredump_fini, &xe->drm);
}

#endif