1 // SPDX-License-Identifier: GPL-2.0 or MIT 2 /* 3 * Copyright (c) 2023 Red Hat. 4 * Author: Jocelyn Falempe <jfalempe@redhat.com> 5 * inspired by the drm_log driver from David Herrmann <dh.herrmann@gmail.com> 6 * Tux Ascii art taken from cowsay written by Tony Monroe 7 */ 8 9 #include <linux/export.h> 10 #include <linux/init.h> 11 #include <linux/kdebug.h> 12 #include <linux/kmsg_dump.h> 13 #include <linux/list.h> 14 #include <linux/math.h> 15 #include <linux/module.h> 16 #include <linux/overflow.h> 17 #include <linux/printk.h> 18 #include <linux/types.h> 19 20 #include <drm/drm_drv.h> 21 #include <drm/drm_panic.h> 22 #include <drm/drm_plane.h> 23 #include <drm/drm_print.h> 24 25 #include "drm_crtc_internal.h" 26 #include "drm_panic_internal.h" 27 28 MODULE_AUTHOR("Jocelyn Falempe"); 29 MODULE_DESCRIPTION("DRM panic handler"); 30 MODULE_LICENSE("GPL"); 31 32 /** 33 * DOC: overview 34 * 35 * This module displays a user friendly message on screen when a kernel 36 * panic occurs. It's intended for end users and therefore have minimal 37 * technical/debug information. 38 * 39 * To enable DRM panic for a driver, the at least one primary plane must 40 * implement struct &drm_plane_funcs.display_panic_screen. The plane is 41 * then automatically registered to the drm panic handler. 42 * 43 * When a panic occurs, the DRM panic handler calls struct 44 * &drm_plane_funcs.display_panic_screen. See 45 * drm_plane_helper_display_panic_screen() for a generic implementation. 46 */ 47 48 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 49 static uint panic_qr_version = CONFIG_DRM_PANIC_SCREEN_QR_VERSION; 50 module_param(panic_qr_version, uint, 0644); 51 MODULE_PARM_DESC(panic_qr_version, "maximum version (size) of the QR code"); 52 #endif 53 54 static enum drm_panic_type drm_panic_type = -1; 55 56 static const char *drm_panic_type_map[] = { 57 [DRM_PANIC_TYPE_KMSG] = "kmsg", 58 [DRM_PANIC_TYPE_USER] = "user", 59 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 60 [DRM_PANIC_TYPE_QR] = "qr_code", 61 #endif 62 }; 63 64 static int drm_panic_type_set(const char *val, const struct kernel_param *kp) 65 { 66 unsigned int i; 67 68 for (i = 0; i < ARRAY_SIZE(drm_panic_type_map); i++) { 69 if (!strcmp(val, drm_panic_type_map[i])) { 70 drm_panic_type = i; 71 return 0; 72 } 73 } 74 75 return -EINVAL; 76 } 77 78 static int drm_panic_type_get(char *buffer, const struct kernel_param *kp) 79 { 80 return scnprintf(buffer, PAGE_SIZE, "%s\n", 81 drm_panic_type_map[drm_panic_type]); 82 } 83 84 static const struct kernel_param_ops drm_panic_ops = { 85 .set = drm_panic_type_set, 86 .get = drm_panic_type_get, 87 }; 88 89 module_param_cb(panic_screen, &drm_panic_ops, NULL, 0644); 90 MODULE_PARM_DESC(panic_screen, 91 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 92 "Choose what will be displayed by drm_panic, 'user', 'kmsg' or 'qr_code' [default=" 93 #else 94 "Choose what will be displayed by drm_panic, 'user' or 'kmsg' [default=" 95 #endif 96 CONFIG_DRM_PANIC_SCREEN "]"); 97 98 #define drm_panic_trylock(dev, flags) \ 99 raw_spin_trylock_irqsave(&(dev)->mode_config.panic_lock, flags) 100 101 static void drm_panic_display_panic_screen(struct drm_plane *plane, const char *description) 102 { 103 #if defined(CONFIG_DRM_PANIC_FOREGROUND_COLOR) 104 u32 fg_color = CONFIG_DRM_PANIC_FOREGROUND_COLOR; 105 #else 106 u32 fg_color = 0x00ffffff; 107 #endif 108 #if defined(CONFIG_DRM_PANIC_BACKGROUND_COLOR) 109 u32 bg_color = CONFIG_DRM_PANIC_BACKGROUND_COLOR; 110 #else 111 u32 bg_color = 0x00000000; 112 #endif 113 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 114 unsigned int qr_version = panic_qr_version; 115 #else 116 unsigned int qr_version = 0; 117 #endif 118 struct drm_device *dev = plane->dev; 119 unsigned long flags; 120 121 if (drm_panic_trylock(dev, flags)) { 122 plane->funcs->display_panic_screen(plane, description, drm_panic_type, 123 fg_color, bg_color, qr_version); 124 drm_panic_unlock(dev, flags); 125 } 126 } 127 128 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd) 129 { 130 return container_of(kd, struct drm_plane, kmsg_panic); 131 } 132 133 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail) 134 { 135 struct drm_plane *plane = to_drm_plane(dumper); 136 137 if (detail->reason == KMSG_DUMP_PANIC) 138 drm_panic_display_panic_screen(plane, detail->description); 139 } 140 141 /* 142 * DEBUG FS, This is currently unsafe. 143 * Create one file per plane, so it's possible to debug one plane at a time. 144 * TODO: It would be better to emulate an NMI context. 145 */ 146 #ifdef CONFIG_DRM_PANIC_DEBUG 147 #include <linux/debugfs.h> 148 149 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf, 150 size_t count, loff_t *ppos) 151 { 152 bool run; 153 154 if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) { 155 struct drm_plane *plane = file->private_data; 156 157 drm_panic_display_panic_screen(plane, "Test from debugfs"); 158 } 159 return count; 160 } 161 162 static const struct file_operations dbg_drm_panic_ops = { 163 .owner = THIS_MODULE, 164 .write = debugfs_trigger_write, 165 .open = simple_open, 166 }; 167 168 static void debugfs_register_plane(struct drm_plane *plane, int index) 169 { 170 char fname[32]; 171 172 snprintf(fname, 32, "drm_panic_plane_%d", index); 173 debugfs_create_file(fname, 0200, plane->dev->debugfs_root, 174 plane, &dbg_drm_panic_ops); 175 } 176 #else 177 static void debugfs_register_plane(struct drm_plane *plane, int index) {} 178 #endif /* CONFIG_DRM_PANIC_DEBUG */ 179 180 /** 181 * drm_panic_is_enabled 182 * @dev: the drm device that may supports drm_panic 183 * 184 * returns true if the drm device supports drm_panic 185 */ 186 bool drm_panic_is_enabled(struct drm_device *dev) 187 { 188 struct drm_plane *plane; 189 190 if (!dev->mode_config.num_total_plane) 191 return false; 192 193 drm_for_each_plane(plane, dev) { 194 if (plane->type != DRM_PLANE_TYPE_PRIMARY) 195 continue; 196 if (!plane->funcs || !plane->funcs->display_panic_screen) 197 continue; 198 return true; 199 } 200 return false; 201 } 202 EXPORT_SYMBOL(drm_panic_is_enabled); 203 204 /** 205 * drm_panic_register() - Initialize DRM panic for a device 206 * @dev: the drm device on which the panic screen will be displayed. 207 */ 208 void drm_panic_register(struct drm_device *dev) 209 { 210 struct drm_plane *plane; 211 int registered_plane = 0; 212 213 if (!dev->mode_config.num_total_plane) 214 return; 215 216 drm_for_each_plane(plane, dev) { 217 if (plane->type != DRM_PLANE_TYPE_PRIMARY) 218 continue; 219 if (!plane->funcs || !plane->funcs->display_panic_screen) 220 continue; 221 plane->kmsg_panic.dump = drm_panic; 222 plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC; 223 if (kmsg_dump_register(&plane->kmsg_panic)) 224 drm_warn(dev, "Failed to register panic handler\n"); 225 else { 226 debugfs_register_plane(plane, registered_plane); 227 registered_plane++; 228 } 229 } 230 if (registered_plane) 231 drm_info(dev, "Registered %d planes with drm panic\n", registered_plane); 232 } 233 234 /** 235 * drm_panic_unregister() 236 * @dev: the drm device previously registered. 237 */ 238 void drm_panic_unregister(struct drm_device *dev) 239 { 240 struct drm_plane *plane; 241 242 if (!dev->mode_config.num_total_plane) 243 return; 244 245 drm_for_each_plane(plane, dev) { 246 if (plane->type != DRM_PLANE_TYPE_PRIMARY) 247 continue; 248 if (!plane->funcs || !plane->funcs->display_panic_screen) 249 continue; 250 kmsg_dump_unregister(&plane->kmsg_panic); 251 } 252 } 253 254 /** 255 * drm_panic_init() - initialize DRM panic. 256 */ 257 void __init drm_panic_init(void) 258 { 259 if (drm_panic_type == -1 && drm_panic_type_set(CONFIG_DRM_PANIC_SCREEN, NULL)) { 260 pr_warn("Unsupported value for CONFIG_DRM_PANIC_SCREEN ('%s'), falling back to 'user'...\n", 261 CONFIG_DRM_PANIC_SCREEN); 262 drm_panic_type = DRM_PANIC_TYPE_USER; 263 } 264 } 265