Something went wrong. Try again.
(I'm so sorry!) Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265// 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 (!strcmp(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);}#elsestatic 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; }}