// 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
*/
staticchar 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.
*/
staticstruct 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 */
};
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;
for (y = 0; y < drm_rect_height(clip); y++) for (x = 0; x < drm_rect_width(clip); x++) if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale))
sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, fg_color);
}
/* * The scanout buffer pages are not mapped, so for each pixel, * use kmap_local_page_try_from_panic() to map the page, and write the pixel. * Try to keep the map from the previous pixel, to avoid too much map/unmap.
*/ staticvoid drm_panic_blit_page(struct page **pages, unsignedint dpitch, unsignedint cpp, const u8 *sbuf8, unsignedint spitch, struct drm_rect *clip, unsignedint scale, u32 fg32)
{ unsignedint y, x; unsignedint page = ~0; unsignedint height = drm_rect_height(clip); unsignedint width = drm_rect_width(clip); void *vaddr = NULL;
for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) { unsignedint new_page; unsignedint offset;
// Special case for 24bit, as a pixel might cross page boundaries if (cpp == 3 && offset + 3 > PAGE_SIZE)
drm_panic_write_pixel24_xpage(vaddr, pages[page + 1],
offset, fg32); else
drm_panic_write_pixel(vaddr, offset, fg32, cpp);
}
}
} if (vaddr)
kunmap_local(vaddr);
}
/* * drm_panic_blit - convert a monochrome image to a linear framebuffer * @sb: destination scanout buffer * @clip: destination rectangle * @sbuf8: source buffer, in monochrome format, 8 pixels per byte. * @spitch: source pitch in bytes * @scale: integer scale, source buffer is scale time smaller than destination * rectangle * @fg_color: foreground color, in destination format * * This can be used to draw a font character, which is a monochrome image, to a * framebuffer in other supported format.
*/ staticvoid drm_panic_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip, const u8 *sbuf8, unsignedint spitch, unsignedint scale, u32 fg_color)
{ struct iosys_map map;
if (sb->set_pixel) return drm_panic_blit_pixel(sb, clip, sbuf8, spitch, scale, fg_color);
for (y = clip->y1; y < clip->y2; y++) { for (x = clip->x1; x < clip->x2; x++) { unsignedint new_page; unsignedint offset;
offset = y * dpitch + x * cpp;
new_page = offset >> PAGE_SHIFT;
offset = offset % PAGE_SIZE; if (new_page != page) { if (vaddr)
kunmap_local(vaddr);
page = new_page;
vaddr = kmap_local_page_try_from_panic(pages[page]);
} if (!vaddr) continue;
// Special case for 24bit, as a pixel might cross page boundaries if (cpp == 3 && offset + 3 > PAGE_SIZE)
drm_panic_write_pixel24_xpage(vaddr, pages[page + 1],
offset, color); else
drm_panic_write_pixel(vaddr, offset, color, cpp);
}
} if (vaddr)
kunmap_local(vaddr);
}
/* * drm_panic_fill - Fill a rectangle with a color * @sb: destination scanout buffer * @clip: destination rectangle * @color: foreground color, in destination format * * Fill a rectangle with a color, in a linear framebuffer.
*/ staticvoid drm_panic_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip,
u32 color)
{ struct iosys_map map;
if (sb->set_pixel) return drm_panic_fill_pixel(sb, clip, color);
if (sb->pages) return drm_panic_fill_page(sb->pages, sb->pitch[0], sb->format->cpp[0],
clip, color);
switch (sb->format->cpp[0]) { case 2:
drm_draw_fill16(&map, sb->pitch[0], drm_rect_height(clip),
drm_rect_width(clip), color); break; case 3:
drm_draw_fill24(&map, sb->pitch[0], drm_rect_height(clip),
drm_rect_width(clip), color); break; case 4:
drm_draw_fill32(&map, sb->pitch[0], drm_rect_height(clip),
drm_rect_width(clip), color); break; default:
WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]);
}
}
staticunsignedint get_max_line_len(conststruct drm_panic_line *lines, int len)
{ int i; unsignedint max = 0;
for (i = 0; i < len; i++)
max = max(lines[i].len, max); return max;
}
/* * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle
*/ staticvoid draw_txt_rectangle(struct drm_scanout_buffer *sb, conststruct font_desc *font, conststruct drm_panic_line *msg, unsignedint msg_lines, bool centered, struct drm_rect *clip,
u32 color)
{ int i, j; const u8 *src;
size_t font_pitch = DIV_ROUND_UP(font->width, 8); struct drm_rect rec;
msg_lines = min(msg_lines, drm_rect_height(clip) / font->height); for (i = 0; i < msg_lines; i++) {
size_t line_len = min(msg[i].len, drm_rect_width(clip) / font->width);
/* * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width. * Return the y-offset of the next line.
*/ staticint draw_line_with_wrap(struct drm_scanout_buffer *sb, conststruct font_desc *font, struct drm_panic_line *line, int yoffset, u32 fg_color)
{ int chars_per_row = sb->width / font->width; struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, sb->height); struct drm_panic_line line_wrap;
/* * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom, * and going up until reaching the top of the screen.
*/ staticvoid draw_panic_static_kmsg(struct drm_scanout_buffer *sb)
{
u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
sb->format->format);
u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
sb->format->format); conststruct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL); struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); struct kmsg_dump_iter iter; char kmsg_buf[512];
size_t kmsg_len; struct drm_panic_line line; int yoffset;
#ifdefined(CONFIG_DRM_PANIC_SCREEN_QR_CODE) /* * It is unwise to allocate memory in the panic callback, so the buffers are * pre-allocated. Only 2 buffers and the zlib workspace are needed. * Two buffers are enough, using the following buffer usage: * 1) kmsg messages are dumped in buffer1 * 2) kmsg is zlib-compressed into buffer2 * 3) compressed kmsg is encoded as QR-code Numeric stream in buffer1 * 4) QR-code image is generated in buffer2 * The Max QR code size is V40, 177x177, 4071 bytes for image, 2956 bytes for * data segments. * * Typically, ~7500 bytes of kmsg, are compressed into 2800 bytes, which fits in * a V40 QR-code (177x177). * * If CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL is not set, the kmsg data will be put * directly in the QR code. * 1) kmsg messages are dumped in buffer1 * 2) kmsg message is encoded as byte stream in buffer2 * 3) QR-code image is generated in buffer1
*/
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");
#define MAX_QR_DATA 2956 #define MAX_ZLIB_RATIO 3 #define QR_BUFFER1_SIZE (MAX_ZLIB_RATIO * MAX_QR_DATA) /* Must also be > 4071 */ #define QR_BUFFER2_SIZE 4096 #define QR_MARGIN 4 /* 4 modules of foreground color around the qr code */
if (zlib_deflate(&stream, Z_FINISH) != Z_STREAM_END) return -EINVAL;
if (zlib_deflateEnd(&stream) != Z_OK) return -EINVAL;
if (stream.total_out > max_qr_data_size) { /* too much data for the QR code, so skip the first line and try again */
kmsg = strchr(kmsg, '\n'); if (!kmsg) return -EINVAL; /* skip the first \n */
kmsg += 1;
kmsg_len = strlen(kmsg); goto try_again;
}
*qr_image = qrbuf2;
/* generate qr code image in buffer2 */ return drm_panic_qr_generate(url, qrbuf2, stream.total_out, QR_BUFFER2_SIZE,
qrbuf1, QR_BUFFER1_SIZE);
}
qr_width = drm_panic_get_qr_code(&qr_image); if (qr_width <= 0) return -ENOSPC;
qr_canvas_width = qr_width + QR_MARGIN * 2;
scale = max_qr_size / qr_canvas_width; /* QR code is not readable if not scaled at least by 2 */ if (scale < 2) return -ENOSPC;
/* * drm_panic_is_format_supported() * @format: a fourcc color code * Returns: true if supported, false otherwise. * * Check if drm_panic will be able to use this color format.
*/ staticbool drm_panic_is_format_supported(conststruct drm_format_info *format)
{ if (format->num_planes != 1) returnfalse; return drm_draw_color_from_xrgb8888(0xffffff, format->format) != 0;
}
if (description) { struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
desc_line->txt = description;
len = strlen(description); /* ignore the last newline character */ if (len && description[len - 1] == '\n')
len -= 1;
desc_line->len = len;
}
}
if (detail->reason == KMSG_DUMP_PANIC)
draw_panic_plane(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>
/** * 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) returnfalse;
drm_for_each_plane(plane, dev) if (plane->helper_private && plane->helper_private->get_scanout_buffer) returntrue; returnfalse;
}
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->helper_private || !plane->helper_private->get_scanout_buffer) 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);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.