summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/drm_panic.c
blob: 7c32580a05b26b228399e61ff8055e3213426077 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0 or MIT
/*
 * Copyright (c) 2023 Red Hat.
 * Author: Jocelyn Falempe <jfalempe@redhat.com>
 * inspired by the drm_log driver from David Herrmann <dh.herrmann@gmail.com>
 * Tux Ascii art taken from cowsay written by Tony Monroe
 */

#include <linux/export.h>
#include <linux/init.h>
#include <linux/kdebug.h>
#include <linux/kmsg_dump.h>
#include <linux/list.h>
#include <linux/math.h>
#include <linux/module.h>
#include <linux/overflow.h>
#include <linux/printk.h>
#include <linux/types.h>

#include <drm/drm_drv.h>
#include <drm/drm_panic.h>
#include <drm/drm_plane.h>
#include <drm/drm_print.h>

#include "drm_crtc_internal.h"
#include "drm_panic_internal.h"

MODULE_AUTHOR("Jocelyn Falempe");
MODULE_DESCRIPTION("DRM panic handler");
MODULE_LICENSE("GPL");

/**
 * DOC: overview
 *
 * This module displays a user friendly message on screen when a kernel
 * panic occurs. It's intended for end users and therefore have minimal
 * technical/debug information.
 *
 * To enable DRM panic for a driver, the at least one primary plane must
 * implement struct &drm_plane_funcs.display_panic_screen. The plane is
 * then automatically registered to the drm panic handler.
 *
 * When a panic occurs, the DRM panic handler calls struct
 * &drm_plane_funcs.display_panic_screen. See
 * drm_plane_helper_display_panic_screen() for a generic implementation.
 */

#if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
static uint panic_qr_version = CONFIG_DRM_PANIC_SCREEN_QR_VERSION;
module_param(panic_qr_version, uint, 0644);
MODULE_PARM_DESC(panic_qr_version, "maximum version (size) of the QR code");
#endif

static enum drm_panic_type drm_panic_type = -1;

static const char *drm_panic_type_map[] = {
	[DRM_PANIC_TYPE_KMSG] = "kmsg",
	[DRM_PANIC_TYPE_USER] = "user",
#if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
	[DRM_PANIC_TYPE_QR] = "qr_code",
#endif
};

static int drm_panic_type_set(const char *val, const struct kernel_param *kp)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(drm_panic_type_map); i++) {
		if (sysfs_streq(val, drm_panic_type_map[i])) {
			drm_panic_type = i;
			return 0;
		}
	}

	return -EINVAL;
}

static int drm_panic_type_get(char *buffer, const struct kernel_param *kp)
{
	return scnprintf(buffer, PAGE_SIZE, "%s\n",
			 drm_panic_type_map[drm_panic_type]);
}

static const struct kernel_param_ops drm_panic_ops = {
	.set = drm_panic_type_set,
	.get = drm_panic_type_get,
};

module_param_cb(panic_screen, &drm_panic_ops, NULL, 0644);
MODULE_PARM_DESC(panic_screen,
#if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
		 "Choose what will be displayed by drm_panic, 'user', 'kmsg' or 'qr_code' [default="
#else
		 "Choose what will be displayed by drm_panic, 'user' or 'kmsg' [default="
#endif
		 CONFIG_DRM_PANIC_SCREEN "]");

#define drm_panic_trylock(dev, flags) \
	raw_spin_trylock_irqsave(&(dev)->mode_config.panic_lock, flags)

static void drm_panic_display_panic_screen(struct drm_plane *plane, const char *description)
{
#if defined(CONFIG_DRM_PANIC_FOREGROUND_COLOR)
	u32 fg_color = CONFIG_DRM_PANIC_FOREGROUND_COLOR;
#else
	u32 fg_color = 0x00ffffff;
#endif
#if defined(CONFIG_DRM_PANIC_BACKGROUND_COLOR)
	u32 bg_color = CONFIG_DRM_PANIC_BACKGROUND_COLOR;
#else
	u32 bg_color = 0x00000000;
#endif
#if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
	unsigned int qr_version = panic_qr_version;
#else
	unsigned int qr_version = 0;
#endif
	struct drm_device *dev = plane->dev;
	unsigned long flags;

	if (drm_panic_trylock(dev, flags)) {
		plane->funcs->display_panic_screen(plane, description, drm_panic_type,
						   fg_color, bg_color, qr_version);
		drm_panic_unlock(dev, flags);
	}
}

static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd)
{
	return container_of(kd, struct drm_plane, kmsg_panic);
}

static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail)
{
	struct drm_plane *plane = to_drm_plane(dumper);

	if (detail->reason == KMSG_DUMP_PANIC)
		drm_panic_display_panic_screen(plane, detail->description);
}

/*
 * DEBUG FS, This is currently unsafe.
 * Create one file per plane, so it's possible to debug one plane at a time.
 * TODO: It would be better to emulate an NMI context.
 */
#ifdef CONFIG_DRM_PANIC_DEBUG
#include <linux/debugfs.h>

static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf,
				     size_t count, loff_t *ppos)
{
	bool run;

	if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) {
		struct drm_plane *plane = file->private_data;

		drm_panic_display_panic_screen(plane, "Test from debugfs");
	}
	return count;
}

static const struct file_operations dbg_drm_panic_ops = {
	.owner = THIS_MODULE,
	.write = debugfs_trigger_write,
	.open = simple_open,
};

static void debugfs_register_plane(struct drm_plane *plane, int index)
{
	char fname[32];

	snprintf(fname, 32, "drm_panic_plane_%d", index);
	debugfs_create_file(fname, 0200, plane->dev->debugfs_root,
			    plane, &dbg_drm_panic_ops);
}
#else
static void debugfs_register_plane(struct drm_plane *plane, int index) {}
#endif /* CONFIG_DRM_PANIC_DEBUG */

/**
 * drm_panic_is_enabled
 * @dev: the drm device that may supports drm_panic
 *
 * returns true if the drm device supports drm_panic
 */
bool drm_panic_is_enabled(struct drm_device *dev)
{
	struct drm_plane *plane;

	if (!dev->mode_config.num_total_plane)
		return false;

	drm_for_each_plane(plane, dev) {
		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
			continue;
		if (!plane->funcs || !plane->funcs->display_panic_screen)
			continue;
		return true;
	}
	return false;
}
EXPORT_SYMBOL(drm_panic_is_enabled);

/**
 * drm_panic_register() - Initialize DRM panic for a device
 * @dev: the drm device on which the panic screen will be displayed.
 */
void drm_panic_register(struct drm_device *dev)
{
	struct drm_plane *plane;
	int registered_plane = 0;

	if (!dev->mode_config.num_total_plane)
		return;

	drm_for_each_plane(plane, dev) {
		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
			continue;
		if (!plane->funcs || !plane->funcs->display_panic_screen)
			continue;
		plane->kmsg_panic.dump = drm_panic;
		plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC;
		if (kmsg_dump_register(&plane->kmsg_panic))
			drm_warn(dev, "Failed to register panic handler\n");
		else {
			debugfs_register_plane(plane, registered_plane);
			registered_plane++;
		}
	}
	if (registered_plane)
		drm_info(dev, "Registered %d planes with drm panic\n", registered_plane);
}

/**
 * drm_panic_unregister()
 * @dev: the drm device previously registered.
 */
void drm_panic_unregister(struct drm_device *dev)
{
	struct drm_plane *plane;

	if (!dev->mode_config.num_total_plane)
		return;

	drm_for_each_plane(plane, dev) {
		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
			continue;
		if (!plane->funcs || !plane->funcs->display_panic_screen)
			continue;
		kmsg_dump_unregister(&plane->kmsg_panic);
	}
}

/**
 * drm_panic_init() - initialize DRM panic.
 */
void __init drm_panic_init(void)
{
	if (drm_panic_type == -1 && drm_panic_type_set(CONFIG_DRM_PANIC_SCREEN, NULL)) {
		pr_warn("Unsupported value for CONFIG_DRM_PANIC_SCREEN ('%s'), falling back to 'user'...\n",
			CONFIG_DRM_PANIC_SCREEN);
		drm_panic_type = DRM_PANIC_TYPE_USER;
	}
}