// 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/font.h>
#include <linux/init.h>
#include <linux/iosys-map.h>
#include <linux/kdebug.h>
#include <linux/kmsg_dump.h>
#include <linux/linux_logo.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 <linux/utsname.h>
#include <linux/zlib.h>
#include <drm/drm_drv.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_modeset_helper_vtables.h>
#include <drm/drm_panic.h>
#include <drm/drm_plane.h>
#include <drm/drm_print.h>
#include <drm/drm_rect.h>
#include "drm_crtc_internal.h"
MODULE_AUTHOR("Jocelyn Falempe");
MODULE_DESCRIPTION("DRM panic handler");
MODULE_LICENSE("GPL");
static char drm_panic_screen[16] = CONFIG_DRM_PANIC_SCREEN;
module_param_string(panic_screen, drm_panic_screen, sizeof(drm_panic_screen), 0644);
MODULE_PARM_DESC(panic_screen,
"Choose what will be displayed by drm_panic, 'user' or 'kmsg' [default="
CONFIG_DRM_PANIC_SCREEN "]");
/**
* DOC: overview
*
* To enable DRM panic for a driver, the primary plane must implement a
* &drm_plane_helper_funcs.get_scanout_buffer helper function. It is then
* automatically registered to the drm panic handler.
* When a panic occurs, the &drm_plane_helper_funcs.get_scanout_buffer will be
* called, and the driver can provide a framebuffer so the panic handler can
* draw the panic screen on it. Currently only linear buffer and a few color
* formats are supported.
* Optionally the driver can also provide a &drm_plane_helper_funcs.panic_flush
* callback, that will be called after that, to send additional commands to the
* hardware to make the scanout buffer visible.
*/
/*
* This module displays a user friendly message on screen when a kernel panic
* occurs. This is conflicting with fbcon, so you can only enable it when fbcon
* is disabled.
* It's intended for end-user, so have minimal technical/debug information.
*
* Implementation details:
*
* It is a panic handler, so it can't take lock, allocate memory, run tasks/irq,
* or attempt to sleep. It's a best effort, and it may not be able to display
* the message in all situations (like if the panic occurs in the middle of a
* modesetting).
* It will display only one static frame, so performance optimizations are low
* priority as the machine is already in an unusable state.
*/
struct drm_panic_line {
u32 len;
const char *txt;
};
#define PANIC_LINE(s) {.len = sizeof(s) - 1, .txt = s}
static struct drm_panic_line panic_msg[] = {
PANIC_LINE("KERNEL PANIC!"),
PANIC_LINE(""),
PANIC_LINE("Please reboot your computer."),
PANIC_LINE(""),
PANIC_LINE(""), /* will be replaced by the panic description */
};
static const size_t panic_msg_lines = ARRAY_SIZE(panic_msg);
static const struct drm_panic_line logo_ascii[] = {
PANIC_LINE(" .--. _"),
PANIC_LINE(" |o_o | | |"),
PANIC_LINE(" |:_/ | | |"),
PANIC_LINE(" // \\ \\ |_|"),
PANIC_LINE(" (| | ) _"),
PANIC_LINE(" /'\\_ _/`\\ (_)"),
PANIC_LINE(" \\___)=(___/"),
};
static const size_t logo_ascii_lines = ARRAY_SIZE(logo_ascii);
#if defined(CONFIG_LOGO) && !defined(MODULE)
static const struct linux_logo *logo_mono;
static int drm_panic_setup_logo(void)
{
const struct linux_logo *logo = fb_find_logo(1);
const unsigned char *logo_data;
struct linux_logo *logo_dup;
if (!logo || logo->type != LINUX_LOGO_MONO)
return 0;
/* The logo is __init, so we must make a copy for later use */
logo_data = kmemdup(logo->data,
size_mul(DIV_ROUND_UP(logo->width, BITS_PER_BYTE), logo->height),
GFP_KERNEL);
if (!logo_data)
return -ENOMEM;
logo_dup = kmemdup(logo, sizeof(*logo), GFP_KERNEL);
if (!logo_dup) {
kfree(logo_data);
return -ENOMEM;
}
logo_dup->data = logo_data;
logo_mono = logo_dup;
return 0;
}
device_initcall(drm_panic_setup_logo);
#else
#define logo_mono ((const struct linux_logo *)NULL)
#endif
/*
* Color conversion
*/
static u16 convert_xrgb8888_to_rgb565(u32 pix)
{
return ((pix & 0x00F80000) >> 8) |
((pix & 0x0000FC00) >> 5) |
((pix & 0x000000F8) >> 3);
}
static u16 convert_xrgb8888_to_rgba5551(u32 pix)
{
return ((pix & 0x00f80000) >> 8) |
((pix & 0x0000f800) >> 5) |
((pix & 0x000000f8) >> 2) |
BIT(0); /* set alpha bit */
}
static u16 convert_xrgb8888_to_xrgb1555(u32 pix)
{
return ((pix & 0x00f80000) >> 9) |
((pix & 0x0000f800) >> 6) |
((pix & 0x000000f8) >> 3);
}
static u16 convert_xrgb8888_to_argb1555(u32 pix)
{
return BIT(15) | /* set alpha bit */
((pix & 0x00f80000) >> 9) |
((pix & 0x0000f800) >> 6) |
((pix & 0x000000f8) >> 3);
}
static u32 convert_xrgb8888_to_argb8888(u32 pix)
{
return pix | GENMASK(31, 24); /* fill alpha bits */
}
static u32 convert_xrgb8888_to_xbgr8888(u32 pix)
{
return ((pix & 0x00ff0000) >> 16) << 0 |
((pix & 0x0000ff00) >> 8) << 8 |
((pix & 0x000000ff) >> 0) << 16 |
((pix & 0xff000000) >> 24) << 24;
}
static u32 convert_xrgb8888_to_abgr8888