1 // SPDX-License-Identifier: GPL-2.0 or MIT
3 * Copyright (c) 2023 Red Hat.
6 * Tux Ascii art taken from cowsay written by Tony Monroe
9 #include <linux/font.h>
10 #include <linux/init.h>
11 #include <linux/iosys-map.h>
12 #include <linux/kdebug.h>
13 #include <linux/kmsg_dump.h>
14 #include <linux/linux_logo.h>
15 #include <linux/list.h>
16 #include <linux/math.h>
17 #include <linux/module.h>
18 #include <linux/overflow.h>
19 #include <linux/printk.h>
20 #include <linux/types.h>
21 #include <linux/utsname.h>
22 #include <linux/zlib.h>
24 #include <drm/drm_drv.h>
25 #include <drm/drm_fourcc.h>
26 #include <drm/drm_framebuffer.h>
27 #include <drm/drm_modeset_helper_vtables.h>
28 #include <drm/drm_panic.h>
29 #include <drm/drm_plane.h>
30 #include <drm/drm_print.h>
31 #include <drm/drm_rect.h>
33 #include "drm_crtc_internal.h"
35 MODULE_AUTHOR("Jocelyn Falempe");
36 MODULE_DESCRIPTION("DRM panic handler");
37 MODULE_LICENSE("GPL");
39 static char drm_panic_screen[16] = CONFIG_DRM_PANIC_SCREEN;
40 module_param_string(panic_screen, drm_panic_screen, sizeof(drm_panic_screen), 0644);
41 MODULE_PARM_DESC(panic_screen,
42 "Choose what will be displayed by drm_panic, 'user' or 'kmsg' [default="
43 CONFIG_DRM_PANIC_SCREEN "]");
48 * To enable DRM panic for a driver, the primary plane must implement a
49 * &drm_plane_helper_funcs.get_scanout_buffer helper function. It is then
50 * automatically registered to the drm panic handler.
51 * When a panic occurs, the &drm_plane_helper_funcs.get_scanout_buffer will be
52 * called, and the driver can provide a framebuffer so the panic handler can
53 * draw the panic screen on it. Currently only linear buffer and a few color
54 * formats are supported.
55 * Optionally the driver can also provide a &drm_plane_helper_funcs.panic_flush
56 * callback, that will be called after that, to send additional commands to the
57 * hardware to make the scanout buffer visible.
61 * This module displays a user friendly message on screen when a kernel panic
62 * occurs. This is conflicting with fbcon, so you can only enable it when fbcon
64 * It's intended for end-user, so have minimal technical/debug information.
66 * Implementation details:
68 * It is a panic handler, so it can't take lock, allocate memory, run tasks/irq,
69 * or attempt to sleep. It's a best effort, and it may not be able to display
70 * the message in all situations (like if the panic occurs in the middle of a
72 * It will display only one static frame, so performance optimizations are low
73 * priority as the machine is already in an unusable state.
76 struct drm_panic_line {
81 #define PANIC_LINE(s) {.len = sizeof(s) - 1, .txt = s}
83 static struct drm_panic_line panic_msg[] = {
84 PANIC_LINE("KERNEL PANIC!"),
86 PANIC_LINE("Please reboot your computer."),
88 PANIC_LINE(""), /* will be replaced by the panic description */
91 static const size_t panic_msg_lines = ARRAY_SIZE(panic_msg);
93 static const struct drm_panic_line logo_ascii[] = {
94 PANIC_LINE(" .--. _"),
95 PANIC_LINE(" |o_o | | |"),
96 PANIC_LINE(" |:_/ | | |"),
97 PANIC_LINE(" // \\ \\ |_|"),
98 PANIC_LINE(" (| | ) _"),
99 PANIC_LINE(" /'\\_ _/`\\ (_)"),
100 PANIC_LINE(" \\___)=(___/"),
103 static const size_t logo_ascii_lines = ARRAY_SIZE(logo_ascii);
105 #if defined(CONFIG_LOGO) && !defined(MODULE)
106 static const struct linux_logo *logo_mono;
108 static int drm_panic_setup_logo(void)
110 const struct linux_logo *logo = fb_find_logo(1);
111 const unsigned char *logo_data;
112 struct linux_logo *logo_dup;
114 if (!logo || logo->type != LINUX_LOGO_MONO)
117 /* The logo is __init, so we must make a copy for later use */
118 logo_data = kmemdup(logo->data,
119 size_mul(DIV_ROUND_UP(logo->width, BITS_PER_BYTE), logo->height),
124 logo_dup = kmemdup(logo, sizeof(*logo), GFP_KERNEL);
130 logo_dup->data = logo_data;
131 logo_mono = logo_dup;
136 device_initcall(drm_panic_setup_logo);
138 #define logo_mono ((const struct linux_logo *)NULL)
145 static u16 convert_xrgb8888_to_rgb565(u32 pix)
147 return ((pix & 0x00F80000) >> 8) |
148 ((pix & 0x0000FC00) >> 5) |
149 ((pix & 0x000000F8) >> 3);
152 static u16 convert_xrgb8888_to_rgba5551(u32 pix)
154 return ((pix & 0x00f80000) >> 8) |
155 ((pix & 0x0000f800) >> 5) |
156 ((pix & 0x000000f8) >> 2) |
157 BIT(0); /* set alpha bit */
160 static u16 convert_xrgb8888_to_xrgb1555(u32 pix)
162 return ((pix & 0x00f80000) >> 9) |
163 ((pix & 0x0000f800) >> 6) |
164 ((pix & 0x000000f8) >> 3);
167 static u16 convert_xrgb8888_to_argb1555(u32 pix)
169 return BIT(15) | /* set alpha bit */
170 ((pix & 0x00f80000) >> 9) |
171 ((pix & 0x0000f800) >> 6) |
172 ((pix & 0x000000f8) >> 3);
175 static u32 convert_xrgb8888_to_argb8888(u32 pix)
177 return pix | GENMASK(31, 24); /* fill alpha bits */
180 static u32 convert_xrgb8888_to_xbgr8888(u32 pix)
182 return ((pix & 0x00ff0000) >> 16) << 0 |
183 ((pix & 0x0000ff00) >> 8) << 8 |
184 ((pix & 0x000000ff) >> 0) << 16 |
185 ((pix & 0xff000000) >> 24) << 24;
188 static u32 convert_xrgb8888_to_abgr8888(u32 pix)
190 return ((pix & 0x00ff0000) >> 16) << 0 |
191 ((pix & 0x0000ff00) >> 8) << 8 |
192 ((pix & 0x000000ff) >> 0) << 16 |
193 GENMASK(31, 24); /* fill alpha bits */
196 static u32 convert_xrgb8888_to_xrgb2101010(u32 pix)
198 pix = ((pix & 0x000000FF) << 2) |
199 ((pix & 0x0000FF00) << 4) |
200 ((pix & 0x00FF0000) << 6);
201 return pix | ((pix >> 8) & 0x00300C03);
204 static u32 convert_xrgb8888_to_argb2101010(u32 pix)
206 pix = ((pix & 0x000000FF) << 2) |
207 ((pix & 0x0000FF00) << 4) |
208 ((pix & 0x00FF0000) << 6);
209 return GENMASK(31, 30) /* set alpha bits */ | pix | ((pix >> 8) & 0x00300C03);
213 * convert_from_xrgb8888 - convert one pixel from xrgb8888 to the desired format
214 * @color: input color, in xrgb8888 format
215 * @format: output format
218 * Color in the format specified, casted to u32.
219 * Or 0 if the format is not supported.
221 static u32 convert_from_xrgb8888(u32 color, u32 format)
224 case DRM_FORMAT_RGB565:
225 return convert_xrgb8888_to_rgb565(color);
226 case DRM_FORMAT_RGBA5551:
227 return convert_xrgb8888_to_rgba5551(color);
228 case DRM_FORMAT_XRGB1555:
229 return convert_xrgb8888_to_xrgb1555(color);
230 case DRM_FORMAT_ARGB1555:
231 return convert_xrgb8888_to_argb1555(color);
232 case DRM_FORMAT_RGB888:
233 case DRM_FORMAT_XRGB8888:
235 case DRM_FORMAT_ARGB8888:
236 return convert_xrgb8888_to_argb8888(color);
237 case DRM_FORMAT_XBGR8888:
238 return convert_xrgb8888_to_xbgr8888(color);
239 case DRM_FORMAT_ABGR8888:
240 return convert_xrgb8888_to_abgr8888(color);
241 case DRM_FORMAT_XRGB2101010:
242 return convert_xrgb8888_to_xrgb2101010(color);
243 case DRM_FORMAT_ARGB2101010:
244 return convert_xrgb8888_to_argb2101010(color);
246 WARN_ONCE(1, "Can't convert to %p4cc\n", &format);
254 /* check if the pixel at coord x,y is 1 (foreground) or 0 (background) */
255 static bool drm_panic_is_pixel_fg(const u8 *sbuf8, unsigned int spitch, int x, int y)
257 return (sbuf8[(y * spitch) + x / 8] & (0x80 >> (x % 8))) != 0;
260 static void drm_panic_blit16(struct iosys_map *dmap, unsigned int dpitch,
261 const u8 *sbuf8, unsigned int spitch,
262 unsigned int height, unsigned int width,
263 unsigned int scale, u16 fg16)
267 for (y = 0; y < height; y++)
268 for (x = 0; x < width; x++)
269 if (drm_panic_is_pixel_fg(sbuf8, spitch, x / scale, y / scale))
270 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, fg16);
273 static void drm_panic_blit24(struct iosys_map *dmap, unsigned int dpitch,
274 const u8 *sbuf8, unsigned int spitch,
275 unsigned int height, unsigned int width,
276 unsigned int scale, u32 fg32)
280 for (y = 0; y < height; y++) {
281 for (x = 0; x < width; x++) {
282 u32 off = y * dpitch + x * 3;
284 if (drm_panic_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) {
285 /* write blue-green-red to output in little endianness */
286 iosys_map_wr(dmap, off, u8, (fg32 & 0x000000FF) >> 0);
287 iosys_map_wr(dmap, off + 1, u8, (fg32 & 0x0000FF00) >> 8);
288 iosys_map_wr(dmap, off + 2, u8, (fg32 & 0x00FF0000) >> 16);
294 static void drm_panic_blit32(struct iosys_map *dmap, unsigned int dpitch,
295 const u8 *sbuf8, unsigned int spitch,
296 unsigned int height, unsigned int width,
297 unsigned int scale, u32 fg32)
301 for (y = 0; y < height; y++)
302 for (x = 0; x < width; x++)
303 if (drm_panic_is_pixel_fg(sbuf8, spitch, x / scale, y / scale))
304 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, fg32);
307 static void drm_panic_blit_pixel(struct drm_scanout_buffer *sb, struct drm_rect *clip,
308 const u8 *sbuf8, unsigned int spitch, unsigned int scale,
313 for (y = 0; y < drm_rect_height(clip); y++)
314 for (x = 0; x < drm_rect_width(clip); x++)
315 if (drm_panic_is_pixel_fg(sbuf8, spitch, x / scale, y / scale))
316 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, fg_color);
320 * drm_panic_blit - convert a monochrome image to a linear framebuffer
321 * @sb: destination scanout buffer
322 * @clip: destination rectangle
323 * @sbuf8: source buffer, in monochrome format, 8 pixels per byte.
324 * @spitch: source pitch in bytes
325 * @scale: integer scale, source buffer is scale time smaller than destination
327 * @fg_color: foreground color, in destination format
329 * This can be used to draw a font character, which is a monochrome image, to a
330 * framebuffer in other supported format.
332 static void drm_panic_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip,
333 const u8 *sbuf8, unsigned int spitch,
334 unsigned int scale, u32 fg_color)
337 struct iosys_map map;
340 return drm_panic_blit_pixel(sb, clip, sbuf8, spitch, scale, fg_color);
343 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]);
345 switch (sb->format->cpp[0]) {
347 drm_panic_blit16(&map, sb->pitch[0], sbuf8, spitch,
348 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
351 drm_panic_blit24(&map, sb->pitch[0], sbuf8, spitch,
352 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
355 drm_panic_blit32(&map, sb->pitch[0], sbuf8, spitch,
356 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
359 WARN_ONCE(1, "Can't blit with pixel width %d\n", sb->format->cpp[0]);
363 static void drm_panic_fill16(struct iosys_map *dmap, unsigned int dpitch,
364 unsigned int height, unsigned int width,
369 for (y = 0; y < height; y++)
370 for (x = 0; x < width; x++)
371 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, color);
374 static void drm_panic_fill24(struct iosys_map *dmap, unsigned int dpitch,
375 unsigned int height, unsigned int width,
380 for (y = 0; y < height; y++) {
381 for (x = 0; x < width; x++) {
382 unsigned int off = y * dpitch + x * 3;
384 /* write blue-green-red to output in little endianness */
385 iosys_map_wr(dmap, off, u8, (color & 0x000000FF) >> 0);
386 iosys_map_wr(dmap, off + 1, u8, (color & 0x0000FF00) >> 8);
387 iosys_map_wr(dmap, off + 2, u8, (color & 0x00FF0000) >> 16);
392 static void drm_panic_fill32(struct iosys_map *dmap, unsigned int dpitch,
393 unsigned int height, unsigned int width,
398 for (y = 0; y < height; y++)
399 for (x = 0; x < width; x++)
400 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, color);
403 static void drm_panic_fill_pixel(struct drm_scanout_buffer *sb,
404 struct drm_rect *clip,
409 for (y = 0; y < drm_rect_height(clip); y++)
410 for (x = 0; x < drm_rect_width(clip); x++)
411 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, color);
415 * drm_panic_fill - Fill a rectangle with a color
416 * @sb: destination scanout buffer
417 * @clip: destination rectangle
418 * @color: foreground color, in destination format
420 * Fill a rectangle with a color, in a linear framebuffer.
422 static void drm_panic_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip,
425 struct iosys_map map;
428 return drm_panic_fill_pixel(sb, clip, color);
431 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]);
433 switch (sb->format->cpp[0]) {
435 drm_panic_fill16(&map, sb->pitch[0], drm_rect_height(clip),
436 drm_rect_width(clip), color);
439 drm_panic_fill24(&map, sb->pitch[0], drm_rect_height(clip),
440 drm_rect_width(clip), color);
443 drm_panic_fill32(&map, sb->pitch[0], drm_rect_height(clip),
444 drm_rect_width(clip), color);
447 WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]);
451 static const u8 *get_char_bitmap(const struct font_desc *font, char c, size_t font_pitch)
453 return font->data + (c * font->height) * font_pitch;
456 static unsigned int get_max_line_len(const struct drm_panic_line *lines, int len)
459 unsigned int max = 0;
461 for (i = 0; i < len; i++)
462 max = max(lines[i].len, max);
467 * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle
469 static void draw_txt_rectangle(struct drm_scanout_buffer *sb,
470 const struct font_desc *font,
471 const struct drm_panic_line *msg,
472 unsigned int msg_lines,
474 struct drm_rect *clip,
479 size_t font_pitch = DIV_ROUND_UP(font->width, 8);
482 msg_lines = min(msg_lines, drm_rect_height(clip) / font->height);
483 for (i = 0; i < msg_lines; i++) {
484 size_t line_len = min(msg[i].len, drm_rect_width(clip) / font->width);
486 rec.y1 = clip->y1 + i * font->height;
487 rec.y2 = rec.y1 + font->height;
491 rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2;
493 for (j = 0; j < line_len; j++) {
494 src = get_char_bitmap(font, msg[i].txt[j], font_pitch);
495 rec.x2 = rec.x1 + font->width;
496 drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
497 rec.x1 += font->width;
502 static void drm_panic_logo_rect(struct drm_rect *rect, const struct font_desc *font)
505 drm_rect_init(rect, 0, 0, logo_mono->width, logo_mono->height);
507 int logo_width = get_max_line_len(logo_ascii, logo_ascii_lines) * font->width;
509 drm_rect_init(rect, 0, 0, logo_width, logo_ascii_lines * font->height);
513 static void drm_panic_logo_draw(struct drm_scanout_buffer *sb, struct drm_rect *rect,
514 const struct font_desc *font, u32 fg_color)
517 drm_panic_blit(sb, rect, logo_mono->data,
518 DIV_ROUND_UP(drm_rect_width(rect), 8), 1, fg_color);
520 draw_txt_rectangle(sb, font, logo_ascii, logo_ascii_lines, false, rect,
524 static void draw_panic_static_user(struct drm_scanout_buffer *sb)
526 u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format);
527 u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format);
528 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
529 struct drm_rect r_screen, r_logo, r_msg;
530 unsigned int msg_width, msg_height;
535 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
536 drm_panic_logo_rect(&r_logo, font);
538 msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
539 msg_height = min(panic_msg_lines * font->height, sb->height);
540 r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
542 /* Center the panic message */
543 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2);
545 /* Fill with the background color, and draw text on top */
546 drm_panic_fill(sb, &r_screen, bg_color);
548 if (!drm_rect_overlap(&r_logo, &r_msg))
549 drm_panic_logo_draw(sb, &r_logo, font, fg_color);
551 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
555 * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width.
556 * Return the y-offset of the next line.
558 static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_desc *font,
559 struct drm_panic_line *line, int yoffset, u32 fg_color)
561 int chars_per_row = sb->width / font->width;
562 struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, sb->height);
563 struct drm_panic_line line_wrap;
565 if (line->len > chars_per_row) {
566 line_wrap.len = line->len % chars_per_row;
567 line_wrap.txt = line->txt + line->len - line_wrap.len;
568 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
569 r_txt.y1 -= font->height;
572 while (line_wrap.txt > line->txt) {
573 line_wrap.txt -= chars_per_row;
574 line_wrap.len = chars_per_row;
575 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
576 r_txt.y1 -= font->height;
581 draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color);
582 r_txt.y1 -= font->height;
588 * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom,
589 * and going up until reaching the top of the screen.
591 static void draw_panic_static_kmsg(struct drm_scanout_buffer *sb)
593 u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format);
594 u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format);
595 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
596 struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
597 struct kmsg_dump_iter iter;
600 struct drm_panic_line line;
606 yoffset = sb->height - font->height - (sb->height % font->height) / 2;
608 /* Fill with the background color, and draw text on top */
609 drm_panic_fill(sb, &r_screen, bg_color);
611 kmsg_dump_rewind(&iter);
612 while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) {
616 /* ignore terminating NUL and newline */
617 start = kmsg_buf + kmsg_len - 2;
618 end = kmsg_buf + kmsg_len - 1;
619 while (start > kmsg_buf && yoffset >= 0) {
620 while (start > kmsg_buf && *start != '\n')
622 /* don't count the newline character */
623 line.txt = start + (start == kmsg_buf ? 0 : 1);
624 line.len = end - line.txt;
626 yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color);
633 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
635 * It is unwise to allocate memory in the panic callback, so the buffers are
636 * pre-allocated. Only 2 buffers and the zlib workspace are needed.
637 * Two buffers are enough, using the following buffer usage:
638 * 1) kmsg messages are dumped in buffer1
639 * 2) kmsg is zlib-compressed into buffer2
640 * 3) compressed kmsg is encoded as QR-code Numeric stream in buffer1
641 * 4) QR-code image is generated in buffer2
642 * The Max QR code size is V40, 177x177, 4071 bytes for image, 2956 bytes for
645 * Typically, ~7500 bytes of kmsg, are compressed into 2800 bytes, which fits in
646 * a V40 QR-code (177x177).
648 * If CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL is not set, the kmsg data will be put
649 * directly in the QR code.
650 * 1) kmsg messages are dumped in buffer1
651 * 2) kmsg message is encoded as byte stream in buffer2
652 * 3) QR-code image is generated in buffer1
655 static uint panic_qr_version = CONFIG_DRM_PANIC_SCREEN_QR_VERSION;
656 module_param(panic_qr_version, uint, 0644);
657 MODULE_PARM_DESC(panic_qr_version, "maximum version (size) of the QR code");
659 #define MAX_QR_DATA 2956
660 #define MAX_ZLIB_RATIO 3
661 #define QR_BUFFER1_SIZE (MAX_ZLIB_RATIO * MAX_QR_DATA) /* Must also be > 4071 */
662 #define QR_BUFFER2_SIZE 4096
663 #define QR_MARGIN 4 /* 4 modules of foreground color around the qr code */
665 /* Compression parameters */
666 #define COMPR_LEVEL 6
667 #define WINDOW_BITS 12
672 static struct z_stream_s stream;
674 static void __init drm_panic_qr_init(void)
676 qrbuf1 = kmalloc(QR_BUFFER1_SIZE, GFP_KERNEL);
677 qrbuf2 = kmalloc(QR_BUFFER2_SIZE, GFP_KERNEL);
678 stream.workspace = kmalloc(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
682 static void drm_panic_qr_exit(void)
688 kfree(stream.workspace);
689 stream.workspace = NULL;
692 extern size_t drm_panic_qr_max_data_size(u8 version, size_t url_len);
694 extern u8 drm_panic_qr_generate(const char *url, u8 *data, size_t data_len, size_t data_size,
695 u8 *tmp, size_t tmp_size);
697 static int drm_panic_get_qr_code_url(u8 **qr_image)
699 struct kmsg_dump_iter iter;
701 size_t kmsg_len, max_kmsg_size;
703 int max_qr_data_size, url_len;
705 url_len = snprintf(url, sizeof(url), CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL "?a=%s&v=%s&zl=",
706 utsname()->machine, utsname()->release);
708 max_qr_data_size = drm_panic_qr_max_data_size(panic_qr_version, url_len);
709 max_kmsg_size = min(MAX_ZLIB_RATIO * max_qr_data_size, QR_BUFFER1_SIZE);
711 /* get kmsg to buffer 1 */
712 kmsg_dump_rewind(&iter);
713 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
720 if (zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
721 MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
724 stream.next_in = kmsg;
725 stream.avail_in = kmsg_len;
727 stream.next_out = qrbuf2;
728 stream.avail_out = QR_BUFFER2_SIZE;
729 stream.total_out = 0;
731 if (zlib_deflate(&stream, Z_FINISH) != Z_STREAM_END)
734 if (zlib_deflateEnd(&stream) != Z_OK)
737 if (stream.total_out > max_qr_data_size) {
738 /* too much data for the QR code, so skip the first line and try again */
739 kmsg = strchr(kmsg, '\n');
742 /* skip the first \n */
744 kmsg_len = strlen(kmsg);
749 /* generate qr code image in buffer2 */
750 return drm_panic_qr_generate(url, qrbuf2, stream.total_out, QR_BUFFER2_SIZE,
751 qrbuf1, QR_BUFFER1_SIZE);
754 static int drm_panic_get_qr_code_raw(u8 **qr_image)
756 struct kmsg_dump_iter iter;
758 size_t max_kmsg_size = min(drm_panic_qr_max_data_size(panic_qr_version, 0),
761 kmsg_dump_rewind(&iter);
762 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
767 return drm_panic_qr_generate(NULL, qrbuf1, kmsg_len, QR_BUFFER1_SIZE,
768 qrbuf2, QR_BUFFER2_SIZE);
771 static int drm_panic_get_qr_code(u8 **qr_image)
773 if (strlen(CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL) > 0)
774 return drm_panic_get_qr_code_url(qr_image);
776 return drm_panic_get_qr_code_raw(qr_image);
780 * Draw the panic message at the center of the screen, with a QR Code
782 static int _draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
784 u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format);
785 u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format);
786 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
787 struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
788 unsigned int max_qr_size, scale;
789 unsigned int msg_width, msg_height;
790 int qr_width, qr_canvas_width, qr_pitch, v_margin;
793 if (!font || !qrbuf1 || !qrbuf2 || !stream.workspace)
796 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
798 drm_panic_logo_rect(&r_logo, font);
800 msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
801 msg_height = min(panic_msg_lines * font->height, sb->height);
802 r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
804 max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4);
806 qr_width = drm_panic_get_qr_code(&qr_image);
810 qr_canvas_width = qr_width + QR_MARGIN * 2;
811 scale = max_qr_size / qr_canvas_width;
812 /* QR code is not readable if not scaled at least by 2 */
816 pr_debug("QR width %d and scale %d\n", qr_width, scale);
817 r_qr_canvas = DRM_RECT_INIT(0, 0, qr_canvas_width * scale, qr_canvas_width * scale);
819 v_margin = (sb->height - drm_rect_height(&r_qr_canvas) - drm_rect_height(&r_msg)) / 5;
821 drm_rect_translate(&r_qr_canvas, (sb->width - r_qr_canvas.x2) / 2, 2 * v_margin);
822 r_qr = DRM_RECT_INIT(r_qr_canvas.x1 + QR_MARGIN * scale, r_qr_canvas.y1 + QR_MARGIN * scale,
823 qr_width * scale, qr_width * scale);
825 /* Center the panic message */
826 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2,
827 3 * v_margin + drm_rect_height(&r_qr_canvas));
829 /* Fill with the background color, and draw text on top */
830 drm_panic_fill(sb, &r_screen, bg_color);
832 if (!drm_rect_overlap(&r_logo, &r_msg) && !drm_rect_overlap(&r_logo, &r_qr))
833 drm_panic_logo_draw(sb, &r_logo, font, fg_color);
835 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
837 /* Draw the qr code */
838 qr_pitch = DIV_ROUND_UP(qr_width, 8);
839 drm_panic_fill(sb, &r_qr_canvas, fg_color);
840 drm_panic_fill(sb, &r_qr, bg_color);
841 drm_panic_blit(sb, &r_qr, qr_image, qr_pitch, scale, fg_color);
845 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
847 if (_draw_panic_static_qr_code(sb))
848 draw_panic_static_user(sb);
851 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
853 draw_panic_static_user(sb);
856 static void drm_panic_qr_init(void) {};
857 static void drm_panic_qr_exit(void) {};
861 * drm_panic_is_format_supported()
862 * @format: a fourcc color code
863 * Returns: true if supported, false otherwise.
865 * Check if drm_panic will be able to use this color format.
867 static bool drm_panic_is_format_supported(const struct drm_format_info *format)
869 if (format->num_planes != 1)
871 return convert_from_xrgb8888(0xffffff, format->format) != 0;
874 static void draw_panic_dispatch(struct drm_scanout_buffer *sb)
876 if (!strcmp(drm_panic_screen, "kmsg")) {
877 draw_panic_static_kmsg(sb);
878 } else if (!strcmp(drm_panic_screen, "qr_code")) {
879 draw_panic_static_qr_code(sb);
881 draw_panic_static_user(sb);
885 static void drm_panic_set_description(const char *description)
890 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
892 desc_line->txt = description;
893 len = strlen(description);
894 /* ignore the last newline character */
895 if (len && description[len - 1] == '\n')
897 desc_line->len = len;
901 static void drm_panic_clear_description(void)
903 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
906 desc_line->txt = NULL;
909 static void draw_panic_plane(struct drm_plane *plane, const char *description)
911 struct drm_scanout_buffer sb = { };
915 if (!drm_panic_trylock(plane->dev, flags))
918 drm_panic_set_description(description);
920 ret = plane->helper_private->get_scanout_buffer(plane, &sb);
922 if (!ret && drm_panic_is_format_supported(sb.format)) {
923 draw_panic_dispatch(&sb);
924 if (plane->helper_private->panic_flush)
925 plane->helper_private->panic_flush(plane);
927 drm_panic_clear_description();
928 drm_panic_unlock(plane->dev, flags);
931 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd)
933 return container_of(kd, struct drm_plane, kmsg_panic);
936 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail)
938 struct drm_plane *plane = to_drm_plane(dumper);
940 if (detail->reason == KMSG_DUMP_PANIC)
941 draw_panic_plane(plane, detail->description);
946 * DEBUG FS, This is currently unsafe.
947 * Create one file per plane, so it's possible to debug one plane at a time.
948 * TODO: It would be better to emulate an NMI context.
950 #ifdef CONFIG_DRM_PANIC_DEBUG
951 #include <linux/debugfs.h>
953 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf,
954 size_t count, loff_t *ppos)
958 if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) {
959 struct drm_plane *plane = file->private_data;
961 draw_panic_plane(plane, "Test from debugfs");
966 static const struct file_operations dbg_drm_panic_ops = {
967 .owner = THIS_MODULE,
968 .write = debugfs_trigger_write,
972 static void debugfs_register_plane(struct drm_plane *plane, int index)
976 snprintf(fname, 32, "drm_panic_plane_%d", index);
977 debugfs_create_file(fname, 0200, plane->dev->debugfs_root,
978 plane, &dbg_drm_panic_ops);
981 static void debugfs_register_plane(struct drm_plane *plane, int index) {}
982 #endif /* CONFIG_DRM_PANIC_DEBUG */
985 * drm_panic_is_enabled
986 * @dev: the drm device that may supports drm_panic
988 * returns true if the drm device supports drm_panic
990 bool drm_panic_is_enabled(struct drm_device *dev)
992 struct drm_plane *plane;
994 if (!dev->mode_config.num_total_plane)
997 drm_for_each_plane(plane, dev)
998 if (plane->helper_private && plane->helper_private->get_scanout_buffer)
1002 EXPORT_SYMBOL(drm_panic_is_enabled);
1005 * drm_panic_register() - Initialize DRM panic for a device
1006 * @dev: the drm device on which the panic screen will be displayed.
1008 void drm_panic_register(struct drm_device *dev)
1010 struct drm_plane *plane;
1011 int registered_plane = 0;
1013 if (!dev->mode_config.num_total_plane)
1016 drm_for_each_plane(plane, dev) {
1017 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
1019 plane->kmsg_panic.dump = drm_panic;
1020 plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC;
1021 if (kmsg_dump_register(&plane->kmsg_panic))
1022 drm_warn(dev, "Failed to register panic handler\n");
1024 debugfs_register_plane(plane, registered_plane);
1028 if (registered_plane)
1029 drm_info(dev, "Registered %d planes with drm panic\n", registered_plane);
1033 * drm_panic_unregister()
1034 * @dev: the drm device previously registered.
1036 void drm_panic_unregister(struct drm_device *dev)
1038 struct drm_plane *plane;
1040 if (!dev->mode_config.num_total_plane)
1043 drm_for_each_plane(plane, dev) {
1044 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
1046 kmsg_dump_unregister(&plane->kmsg_panic);
1051 * drm_panic_init() - initialize DRM panic.
1053 void __init drm_panic_init(void)
1055 drm_panic_qr_init();
1059 * drm_panic_exit() - Free the resources taken by drm_panic_exit()
1061 void drm_panic_exit(void)
1063 drm_panic_qr_exit();