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/font.h> 11 #include <linux/highmem.h> 12 #include <linux/init.h> 13 #include <linux/iosys-map.h> 14 #include <linux/kdebug.h> 15 #include <linux/kmsg_dump.h> 16 #include <linux/linux_logo.h> 17 #include <linux/list.h> 18 #include <linux/math.h> 19 #include <linux/module.h> 20 #include <linux/overflow.h> 21 #include <linux/printk.h> 22 #include <linux/types.h> 23 #include <linux/utsname.h> 24 #include <linux/zlib.h> 25 26 #include <drm/drm_drv.h> 27 #include <drm/drm_fourcc.h> 28 #include <drm/drm_framebuffer.h> 29 #include <drm/drm_modeset_helper_vtables.h> 30 #include <drm/drm_panic.h> 31 #include <drm/drm_plane.h> 32 #include <drm/drm_print.h> 33 #include <drm/drm_rect.h> 34 35 #include "drm_crtc_internal.h" 36 #include "drm_draw_internal.h" 37 38 MODULE_AUTHOR("Jocelyn Falempe"); 39 MODULE_DESCRIPTION("DRM panic handler"); 40 MODULE_LICENSE("GPL"); 41 42 /** 43 * DOC: overview 44 * 45 * To enable DRM panic for a driver, the primary plane must implement a 46 * &drm_plane_helper_funcs.get_scanout_buffer helper function. It is then 47 * automatically registered to the drm panic handler. 48 * When a panic occurs, the &drm_plane_helper_funcs.get_scanout_buffer will be 49 * called, and the driver can provide a framebuffer so the panic handler can 50 * draw the panic screen on it. Currently only linear buffer and a few color 51 * formats are supported. 52 * Optionally the driver can also provide a &drm_plane_helper_funcs.panic_flush 53 * callback, that will be called after that, to send additional commands to the 54 * hardware to make the scanout buffer visible. 55 */ 56 57 /* 58 * This module displays a user friendly message on screen when a kernel panic 59 * occurs. This is conflicting with fbcon, so you can only enable it when fbcon 60 * is disabled. 61 * It's intended for end-user, so have minimal technical/debug information. 62 * 63 * Implementation details: 64 * 65 * It is a panic handler, so it can't take lock, allocate memory, run tasks/irq, 66 * or attempt to sleep. It's a best effort, and it may not be able to display 67 * the message in all situations (like if the panic occurs in the middle of a 68 * modesetting). 69 * It will display only one static frame, so performance optimizations are low 70 * priority as the machine is already in an unusable state. 71 */ 72 73 struct drm_panic_line { 74 u32 len; 75 const char *txt; 76 }; 77 78 #define PANIC_LINE(s) {.len = sizeof(s) - 1, .txt = s} 79 80 static struct drm_panic_line panic_msg[] = { 81 PANIC_LINE("KERNEL PANIC!"), 82 PANIC_LINE(""), 83 PANIC_LINE("Please reboot your computer."), 84 PANIC_LINE(""), 85 PANIC_LINE(""), /* will be replaced by the panic description */ 86 }; 87 88 static const size_t panic_msg_lines = ARRAY_SIZE(panic_msg); 89 90 static const struct drm_panic_line logo_ascii[] = { 91 PANIC_LINE(" .--. _"), 92 PANIC_LINE(" |o_o | | |"), 93 PANIC_LINE(" |:_/ | | |"), 94 PANIC_LINE(" // \\ \\ |_|"), 95 PANIC_LINE(" (| | ) _"), 96 PANIC_LINE(" /'\\_ _/`\\ (_)"), 97 PANIC_LINE(" \\___)=(___/"), 98 }; 99 100 static const size_t logo_ascii_lines = ARRAY_SIZE(logo_ascii); 101 102 #if defined(CONFIG_LOGO) && !defined(MODULE) 103 static const struct linux_logo *logo_mono; 104 105 static int drm_panic_setup_logo(void) 106 { 107 const struct linux_logo *logo = fb_find_logo(1); 108 const unsigned char *logo_data; 109 struct linux_logo *logo_dup; 110 111 if (!logo || logo->type != LINUX_LOGO_MONO) 112 return 0; 113 114 /* The logo is __init, so we must make a copy for later use */ 115 logo_data = kmemdup(logo->data, 116 size_mul(DIV_ROUND_UP(logo->width, BITS_PER_BYTE), logo->height), 117 GFP_KERNEL); 118 if (!logo_data) 119 return -ENOMEM; 120 121 logo_dup = kmemdup(logo, sizeof(*logo), GFP_KERNEL); 122 if (!logo_dup) { 123 kfree(logo_data); 124 return -ENOMEM; 125 } 126 127 logo_dup->data = logo_data; 128 logo_mono = logo_dup; 129 130 return 0; 131 } 132 133 device_initcall(drm_panic_setup_logo); 134 #else 135 #define logo_mono ((const struct linux_logo *)NULL) 136 #endif 137 138 /* 139 * Blit & Fill functions 140 */ 141 static void drm_panic_blit_pixel(struct drm_scanout_buffer *sb, struct drm_rect *clip, 142 const u8 *sbuf8, unsigned int spitch, unsigned int scale, 143 u32 fg_color) 144 { 145 unsigned int y, x; 146 147 for (y = 0; y < drm_rect_height(clip); y++) 148 for (x = 0; x < drm_rect_width(clip); x++) 149 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) 150 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, fg_color); 151 } 152 153 static void drm_panic_write_pixel16(void *vaddr, unsigned int offset, u16 color) 154 { 155 u16 *p = vaddr + offset; 156 157 *p = color; 158 } 159 160 static void drm_panic_write_pixel24(void *vaddr, unsigned int offset, u32 color) 161 { 162 u8 *p = vaddr + offset; 163 164 *p++ = color & 0xff; 165 color >>= 8; 166 *p++ = color & 0xff; 167 color >>= 8; 168 *p = color & 0xff; 169 } 170 171 /* 172 * Special case if the pixel crosses page boundaries 173 */ 174 static void drm_panic_write_pixel24_xpage(void *vaddr, struct page *next_page, 175 unsigned int offset, u32 color) 176 { 177 u8 *vaddr2; 178 u8 *p = vaddr + offset; 179 180 vaddr2 = kmap_local_page_try_from_panic(next_page); 181 182 *p++ = color & 0xff; 183 color >>= 8; 184 185 if (offset == PAGE_SIZE - 1) 186 p = vaddr2; 187 188 *p++ = color & 0xff; 189 color >>= 8; 190 191 if (offset == PAGE_SIZE - 2) 192 p = vaddr2; 193 194 *p = color & 0xff; 195 kunmap_local(vaddr2); 196 } 197 198 static void drm_panic_write_pixel32(void *vaddr, unsigned int offset, u32 color) 199 { 200 u32 *p = vaddr + offset; 201 202 *p = color; 203 } 204 205 static void drm_panic_write_pixel(void *vaddr, unsigned int offset, u32 color, unsigned int cpp) 206 { 207 switch (cpp) { 208 case 2: 209 drm_panic_write_pixel16(vaddr, offset, color); 210 break; 211 case 3: 212 drm_panic_write_pixel24(vaddr, offset, color); 213 break; 214 case 4: 215 drm_panic_write_pixel32(vaddr, offset, color); 216 break; 217 default: 218 pr_debug_once("Can't blit with pixel width %d\n", cpp); 219 } 220 } 221 222 /* 223 * The scanout buffer pages are not mapped, so for each pixel, 224 * use kmap_local_page_try_from_panic() to map the page, and write the pixel. 225 * Try to keep the map from the previous pixel, to avoid too much map/unmap. 226 */ 227 static void drm_panic_blit_page(struct page **pages, unsigned int dpitch, 228 unsigned int cpp, const u8 *sbuf8, 229 unsigned int spitch, struct drm_rect *clip, 230 unsigned int scale, u32 fg32) 231 { 232 unsigned int y, x; 233 unsigned int page = ~0; 234 unsigned int height = drm_rect_height(clip); 235 unsigned int width = drm_rect_width(clip); 236 void *vaddr = NULL; 237 238 for (y = 0; y < height; y++) { 239 for (x = 0; x < width; x++) { 240 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) { 241 unsigned int new_page; 242 unsigned int offset; 243 244 offset = (y + clip->y1) * dpitch + (x + clip->x1) * cpp; 245 new_page = offset >> PAGE_SHIFT; 246 offset = offset % PAGE_SIZE; 247 if (new_page != page) { 248 if (!pages[new_page]) 249 continue; 250 if (vaddr) 251 kunmap_local(vaddr); 252 page = new_page; 253 vaddr = kmap_local_page_try_from_panic(pages[page]); 254 } 255 if (!vaddr) 256 continue; 257 258 // Special case for 24bit, as a pixel might cross page boundaries 259 if (cpp == 3 && offset + 3 > PAGE_SIZE) 260 drm_panic_write_pixel24_xpage(vaddr, pages[page + 1], 261 offset, fg32); 262 else 263 drm_panic_write_pixel(vaddr, offset, fg32, cpp); 264 } 265 } 266 } 267 if (vaddr) 268 kunmap_local(vaddr); 269 } 270 271 /* 272 * drm_panic_blit - convert a monochrome image to a linear framebuffer 273 * @sb: destination scanout buffer 274 * @clip: destination rectangle 275 * @sbuf8: source buffer, in monochrome format, 8 pixels per byte. 276 * @spitch: source pitch in bytes 277 * @scale: integer scale, source buffer is scale time smaller than destination 278 * rectangle 279 * @fg_color: foreground color, in destination format 280 * 281 * This can be used to draw a font character, which is a monochrome image, to a 282 * framebuffer in other supported format. 283 */ 284 static void drm_panic_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip, 285 const u8 *sbuf8, unsigned int spitch, 286 unsigned int scale, u32 fg_color) 287 288 { 289 struct iosys_map map; 290 291 if (sb->set_pixel) 292 return drm_panic_blit_pixel(sb, clip, sbuf8, spitch, scale, fg_color); 293 294 if (sb->pages) 295 return drm_panic_blit_page(sb->pages, sb->pitch[0], sb->format->cpp[0], 296 sbuf8, spitch, clip, scale, fg_color); 297 298 map = sb->map[0]; 299 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]); 300 301 switch (sb->format->cpp[0]) { 302 case 2: 303 drm_draw_blit16(&map, sb->pitch[0], sbuf8, spitch, 304 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 305 break; 306 case 3: 307 drm_draw_blit24(&map, sb->pitch[0], sbuf8, spitch, 308 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 309 break; 310 case 4: 311 drm_draw_blit32(&map, sb->pitch[0], sbuf8, spitch, 312 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 313 break; 314 default: 315 WARN_ONCE(1, "Can't blit with pixel width %d\n", sb->format->cpp[0]); 316 } 317 } 318 319 static void drm_panic_fill_pixel(struct drm_scanout_buffer *sb, 320 struct drm_rect *clip, 321 u32 color) 322 { 323 unsigned int y, x; 324 325 for (y = 0; y < drm_rect_height(clip); y++) 326 for (x = 0; x < drm_rect_width(clip); x++) 327 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, color); 328 } 329 330 static void drm_panic_fill_page(struct page **pages, unsigned int dpitch, 331 unsigned int cpp, struct drm_rect *clip, 332 u32 color) 333 { 334 unsigned int y, x; 335 unsigned int page = ~0; 336 void *vaddr = NULL; 337 338 for (y = clip->y1; y < clip->y2; y++) { 339 for (x = clip->x1; x < clip->x2; x++) { 340 unsigned int new_page; 341 unsigned int offset; 342 343 offset = y * dpitch + x * cpp; 344 new_page = offset >> PAGE_SHIFT; 345 offset = offset % PAGE_SIZE; 346 if (new_page != page) { 347 if (vaddr) 348 kunmap_local(vaddr); 349 page = new_page; 350 vaddr = kmap_local_page_try_from_panic(pages[page]); 351 } 352 if (!vaddr) 353 continue; 354 355 // Special case for 24bit, as a pixel might cross page boundaries 356 if (cpp == 3 && offset + 3 > PAGE_SIZE) 357 drm_panic_write_pixel24_xpage(vaddr, pages[page + 1], 358 offset, color); 359 else 360 drm_panic_write_pixel(vaddr, offset, color, cpp); 361 } 362 } 363 if (vaddr) 364 kunmap_local(vaddr); 365 } 366 367 /* 368 * drm_panic_fill - Fill a rectangle with a color 369 * @sb: destination scanout buffer 370 * @clip: destination rectangle 371 * @color: foreground color, in destination format 372 * 373 * Fill a rectangle with a color, in a linear framebuffer. 374 */ 375 static void drm_panic_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip, 376 u32 color) 377 { 378 struct iosys_map map; 379 380 if (sb->set_pixel) 381 return drm_panic_fill_pixel(sb, clip, color); 382 383 if (sb->pages) 384 return drm_panic_fill_page(sb->pages, sb->pitch[0], sb->format->cpp[0], 385 clip, color); 386 387 map = sb->map[0]; 388 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]); 389 390 switch (sb->format->cpp[0]) { 391 case 2: 392 drm_draw_fill16(&map, sb->pitch[0], drm_rect_height(clip), 393 drm_rect_width(clip), color); 394 break; 395 case 3: 396 drm_draw_fill24(&map, sb->pitch[0], drm_rect_height(clip), 397 drm_rect_width(clip), color); 398 break; 399 case 4: 400 drm_draw_fill32(&map, sb->pitch[0], drm_rect_height(clip), 401 drm_rect_width(clip), color); 402 break; 403 default: 404 WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]); 405 } 406 } 407 408 static unsigned int get_max_line_len(const struct drm_panic_line *lines, int len) 409 { 410 int i; 411 unsigned int max = 0; 412 413 for (i = 0; i < len; i++) 414 max = max(lines[i].len, max); 415 return max; 416 } 417 418 /* 419 * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle 420 */ 421 static void draw_txt_rectangle(struct drm_scanout_buffer *sb, 422 const struct font_desc *font, 423 const struct drm_panic_line *msg, 424 unsigned int msg_lines, 425 bool centered, 426 struct drm_rect *clip, 427 u32 color) 428 { 429 int i, j; 430 const u8 *src; 431 size_t font_pitch = DIV_ROUND_UP(font->width, 8); 432 struct drm_rect rec; 433 434 msg_lines = min(msg_lines, drm_rect_height(clip) / font->height); 435 for (i = 0; i < msg_lines; i++) { 436 size_t line_len = min(msg[i].len, drm_rect_width(clip) / font->width); 437 438 rec.y1 = clip->y1 + i * font->height; 439 rec.y2 = rec.y1 + font->height; 440 rec.x1 = clip->x1; 441 442 if (centered) 443 rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2; 444 445 for (j = 0; j < line_len; j++) { 446 src = drm_draw_get_char_bitmap(font, msg[i].txt[j], font_pitch); 447 rec.x2 = rec.x1 + font->width; 448 drm_panic_blit(sb, &rec, src, font_pitch, 1, color); 449 rec.x1 += font->width; 450 } 451 } 452 } 453 454 static void drm_panic_logo_rect(struct drm_rect *rect, const struct font_desc *font) 455 { 456 if (logo_mono) { 457 drm_rect_init(rect, 0, 0, logo_mono->width, logo_mono->height); 458 } else { 459 int logo_width = get_max_line_len(logo_ascii, logo_ascii_lines) * font->width; 460 461 drm_rect_init(rect, 0, 0, logo_width, logo_ascii_lines * font->height); 462 } 463 } 464 465 static void drm_panic_logo_draw(struct drm_scanout_buffer *sb, struct drm_rect *rect, 466 const struct font_desc *font, u32 fg_color) 467 { 468 if (rect->x2 > sb->width || rect->y2 > sb->height) 469 return; 470 471 if (logo_mono) 472 drm_panic_blit(sb, rect, logo_mono->data, 473 DIV_ROUND_UP(drm_rect_width(rect), 8), 1, fg_color); 474 else 475 draw_txt_rectangle(sb, font, logo_ascii, logo_ascii_lines, false, rect, 476 fg_color); 477 } 478 479 static void draw_panic_screen_user(struct drm_scanout_buffer *sb) 480 { 481 u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, 482 sb->format->format); 483 u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, 484 sb->format->format); 485 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL); 486 struct drm_rect r_screen, r_logo, r_msg; 487 unsigned int msg_width, msg_height; 488 489 if (!font) 490 return; 491 492 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 493 drm_panic_logo_rect(&r_logo, font); 494 495 msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width); 496 msg_height = min(panic_msg_lines * font->height, sb->height); 497 r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height); 498 499 /* Center the panic message */ 500 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2); 501 502 /* Fill with the background color, and draw text on top */ 503 drm_panic_fill(sb, &r_screen, bg_color); 504 505 if (!drm_rect_overlap(&r_logo, &r_msg)) 506 drm_panic_logo_draw(sb, &r_logo, font, fg_color); 507 508 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color); 509 } 510 511 /* 512 * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width. 513 * Return the y-offset of the next line. 514 */ 515 static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_desc *font, 516 struct drm_panic_line *line, int yoffset, u32 fg_color) 517 { 518 int chars_per_row = sb->width / font->width; 519 struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, font->height); 520 struct drm_panic_line line_wrap; 521 522 if (line->len > chars_per_row) { 523 line_wrap.len = line->len % chars_per_row; 524 line_wrap.txt = line->txt + line->len - line_wrap.len; 525 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color); 526 r_txt.y1 -= font->height; 527 if (r_txt.y1 < 0) 528 return r_txt.y1; 529 while (line_wrap.txt > line->txt) { 530 line_wrap.txt -= chars_per_row; 531 line_wrap.len = chars_per_row; 532 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color); 533 r_txt.y1 -= font->height; 534 if (r_txt.y1 < 0) 535 return r_txt.y1; 536 } 537 } else { 538 draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color); 539 r_txt.y1 -= font->height; 540 } 541 return r_txt.y1; 542 } 543 544 /* 545 * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom, 546 * and going up until reaching the top of the screen. 547 */ 548 static void draw_panic_screen_kmsg(struct drm_scanout_buffer *sb) 549 { 550 u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, 551 sb->format->format); 552 u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, 553 sb->format->format); 554 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL); 555 struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 556 struct kmsg_dump_iter iter; 557 char kmsg_buf[512]; 558 size_t kmsg_len; 559 struct drm_panic_line line; 560 int yoffset; 561 562 if (!font || font->width > sb->width) 563 return; 564 565 yoffset = sb->height - font->height - (sb->height % font->height) / 2; 566 567 /* Fill with the background color, and draw text on top */ 568 drm_panic_fill(sb, &r_screen, bg_color); 569 570 kmsg_dump_rewind(&iter); 571 while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) { 572 char *start; 573 char *end; 574 575 /* ignore terminating NUL and newline */ 576 start = kmsg_buf + kmsg_len - 2; 577 end = kmsg_buf + kmsg_len - 1; 578 while (start > kmsg_buf && yoffset >= 0) { 579 while (start > kmsg_buf && *start != '\n') 580 start--; 581 /* don't count the newline character */ 582 line.txt = start + (start == kmsg_buf ? 0 : 1); 583 line.len = end - line.txt; 584 585 yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color); 586 end = start; 587 start--; 588 } 589 } 590 } 591 592 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 593 /* 594 * It is unwise to allocate memory in the panic callback, so the buffers are 595 * pre-allocated. Only 2 buffers and the zlib workspace are needed. 596 * Two buffers are enough, using the following buffer usage: 597 * 1) kmsg messages are dumped in buffer1 598 * 2) kmsg is zlib-compressed into buffer2 599 * 3) compressed kmsg is encoded as QR-code Numeric stream in buffer1 600 * 4) QR-code image is generated in buffer2 601 * The Max QR code size is V40, 177x177, 4071 bytes for image, 2956 bytes for 602 * data segments. 603 * 604 * Typically, ~7500 bytes of kmsg, are compressed into 2800 bytes, which fits in 605 * a V40 QR-code (177x177). 606 * 607 * If CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL is not set, the kmsg data will be put 608 * directly in the QR code. 609 * 1) kmsg messages are dumped in buffer1 610 * 2) kmsg message is encoded as byte stream in buffer2 611 * 3) QR-code image is generated in buffer1 612 */ 613 614 static uint panic_qr_version = CONFIG_DRM_PANIC_SCREEN_QR_VERSION; 615 module_param(panic_qr_version, uint, 0644); 616 MODULE_PARM_DESC(panic_qr_version, "maximum version (size) of the QR code"); 617 618 #define MAX_QR_DATA 2956 619 #define MAX_ZLIB_RATIO 3 620 #define QR_BUFFER1_SIZE (MAX_ZLIB_RATIO * MAX_QR_DATA) /* Must also be > 4071 */ 621 #define QR_BUFFER2_SIZE 4096 622 #define QR_MARGIN 4 /* 4 modules of foreground color around the qr code */ 623 624 /* Compression parameters */ 625 #define COMPR_LEVEL 6 626 #define WINDOW_BITS 12 627 #define MEM_LEVEL 4 628 629 static char *qrbuf1; 630 static char *qrbuf2; 631 static struct z_stream_s stream; 632 633 static void __init drm_panic_qr_init(void) 634 { 635 qrbuf1 = kmalloc(QR_BUFFER1_SIZE, GFP_KERNEL); 636 qrbuf2 = kmalloc(QR_BUFFER2_SIZE, GFP_KERNEL); 637 stream.workspace = kmalloc(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL), 638 GFP_KERNEL); 639 } 640 641 static void drm_panic_qr_exit(void) 642 { 643 kfree(qrbuf1); 644 qrbuf1 = NULL; 645 kfree(qrbuf2); 646 qrbuf2 = NULL; 647 kfree(stream.workspace); 648 stream.workspace = NULL; 649 } 650 651 static int drm_panic_get_qr_code_url(u8 **qr_image) 652 { 653 struct kmsg_dump_iter iter; 654 char url[256]; 655 size_t kmsg_len, max_kmsg_size; 656 char *kmsg; 657 int max_qr_data_size, url_len; 658 659 url_len = snprintf(url, sizeof(url), CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL "?a=%s&v=%s&z=", 660 utsname()->machine, utsname()->release); 661 662 max_qr_data_size = drm_panic_qr_max_data_size(panic_qr_version, url_len); 663 max_kmsg_size = min(MAX_ZLIB_RATIO * max_qr_data_size, QR_BUFFER1_SIZE); 664 665 /* get kmsg to buffer 1 */ 666 kmsg_dump_rewind(&iter); 667 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len); 668 669 if (!kmsg_len) 670 return -ENODATA; 671 kmsg = qrbuf1; 672 673 try_again: 674 if (zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, 675 MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) 676 return -EINVAL; 677 678 stream.next_in = kmsg; 679 stream.avail_in = kmsg_len; 680 stream.total_in = 0; 681 stream.next_out = qrbuf2; 682 stream.avail_out = QR_BUFFER2_SIZE; 683 stream.total_out = 0; 684 685 if (zlib_deflate(&stream, Z_FINISH) != Z_STREAM_END) 686 return -EINVAL; 687 688 if (zlib_deflateEnd(&stream) != Z_OK) 689 return -EINVAL; 690 691 if (stream.total_out > max_qr_data_size) { 692 /* too much data for the QR code, so skip the first line and try again */ 693 kmsg = strchr(kmsg, '\n'); 694 if (!kmsg) 695 return -EINVAL; 696 /* skip the first \n */ 697 kmsg += 1; 698 kmsg_len = strlen(kmsg); 699 goto try_again; 700 } 701 *qr_image = qrbuf2; 702 703 /* generate qr code image in buffer2 */ 704 return drm_panic_qr_generate(url, qrbuf2, stream.total_out, QR_BUFFER2_SIZE, 705 qrbuf1, QR_BUFFER1_SIZE); 706 } 707 708 static int drm_panic_get_qr_code_raw(u8 **qr_image) 709 { 710 struct kmsg_dump_iter iter; 711 size_t kmsg_len; 712 size_t max_kmsg_size = min(drm_panic_qr_max_data_size(panic_qr_version, 0), 713 QR_BUFFER1_SIZE); 714 715 kmsg_dump_rewind(&iter); 716 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len); 717 if (!kmsg_len) 718 return -ENODATA; 719 720 *qr_image = qrbuf1; 721 return drm_panic_qr_generate(NULL, qrbuf1, kmsg_len, QR_BUFFER1_SIZE, 722 qrbuf2, QR_BUFFER2_SIZE); 723 } 724 725 static int drm_panic_get_qr_code(u8 **qr_image) 726 { 727 if (strlen(CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL) > 0) 728 return drm_panic_get_qr_code_url(qr_image); 729 else 730 return drm_panic_get_qr_code_raw(qr_image); 731 } 732 733 /* 734 * Draw the panic message at the center of the screen, with a QR Code 735 */ 736 static int _draw_panic_screen_qr_code(struct drm_scanout_buffer *sb) 737 { 738 u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, 739 sb->format->format); 740 u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, 741 sb->format->format); 742 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL); 743 struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas; 744 unsigned int max_qr_size, scale; 745 unsigned int msg_width, msg_height; 746 int qr_width, qr_canvas_width, qr_pitch, v_margin; 747 u8 *qr_image; 748 749 if (!font || !qrbuf1 || !qrbuf2 || !stream.workspace) 750 return -ENOMEM; 751 752 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 753 754 drm_panic_logo_rect(&r_logo, font); 755 756 msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width); 757 msg_height = min(panic_msg_lines * font->height, sb->height); 758 r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height); 759 760 max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4); 761 762 qr_width = drm_panic_get_qr_code(&qr_image); 763 if (qr_width <= 0) 764 return -ENOSPC; 765 766 qr_canvas_width = qr_width + QR_MARGIN * 2; 767 scale = max_qr_size / qr_canvas_width; 768 /* QR code is not readable if not scaled at least by 2 */ 769 if (scale < 2) 770 return -ENOSPC; 771 772 pr_debug("QR width %d and scale %d\n", qr_width, scale); 773 r_qr_canvas = DRM_RECT_INIT(0, 0, qr_canvas_width * scale, qr_canvas_width * scale); 774 775 v_margin = sb->height - drm_rect_height(&r_qr_canvas) - drm_rect_height(&r_msg); 776 if (v_margin < 0) 777 return -ENOSPC; 778 v_margin /= 5; 779 780 drm_rect_translate(&r_qr_canvas, (sb->width - r_qr_canvas.x2) / 2, 2 * v_margin); 781 r_qr = DRM_RECT_INIT(r_qr_canvas.x1 + QR_MARGIN * scale, r_qr_canvas.y1 + QR_MARGIN * scale, 782 qr_width * scale, qr_width * scale); 783 784 /* Center the panic message */ 785 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, 786 3 * v_margin + drm_rect_height(&r_qr_canvas)); 787 788 /* Fill with the background color, and draw text on top */ 789 drm_panic_fill(sb, &r_screen, bg_color); 790 791 if (!drm_rect_overlap(&r_logo, &r_msg) && !drm_rect_overlap(&r_logo, &r_qr_canvas)) 792 drm_panic_logo_draw(sb, &r_logo, font, fg_color); 793 794 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color); 795 796 /* Draw the qr code */ 797 qr_pitch = DIV_ROUND_UP(qr_width, 8); 798 drm_panic_fill(sb, &r_qr_canvas, fg_color); 799 drm_panic_fill(sb, &r_qr, bg_color); 800 drm_panic_blit(sb, &r_qr, qr_image, qr_pitch, scale, fg_color); 801 return 0; 802 } 803 804 static void draw_panic_screen_qr_code(struct drm_scanout_buffer *sb) 805 { 806 if (_draw_panic_screen_qr_code(sb)) 807 draw_panic_screen_user(sb); 808 } 809 #else 810 static void drm_panic_qr_init(void) {}; 811 static void drm_panic_qr_exit(void) {}; 812 #endif 813 814 enum drm_panic_type { 815 DRM_PANIC_TYPE_KMSG, 816 DRM_PANIC_TYPE_USER, 817 DRM_PANIC_TYPE_QR, 818 }; 819 820 static enum drm_panic_type drm_panic_type = -1; 821 822 static const char *drm_panic_type_map[] = { 823 [DRM_PANIC_TYPE_KMSG] = "kmsg", 824 [DRM_PANIC_TYPE_USER] = "user", 825 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 826 [DRM_PANIC_TYPE_QR] = "qr_code", 827 #endif 828 }; 829 830 static int drm_panic_type_set(const char *val, const struct kernel_param *kp) 831 { 832 unsigned int i; 833 834 for (i = 0; i < ARRAY_SIZE(drm_panic_type_map); i++) { 835 if (!strcmp(val, drm_panic_type_map[i])) { 836 drm_panic_type = i; 837 return 0; 838 } 839 } 840 841 return -EINVAL; 842 } 843 844 static int drm_panic_type_get(char *buffer, const struct kernel_param *kp) 845 { 846 return scnprintf(buffer, PAGE_SIZE, "%s\n", 847 drm_panic_type_map[drm_panic_type]); 848 } 849 850 static const struct kernel_param_ops drm_panic_ops = { 851 .set = drm_panic_type_set, 852 .get = drm_panic_type_get, 853 }; 854 855 module_param_cb(panic_screen, &drm_panic_ops, NULL, 0644); 856 MODULE_PARM_DESC(panic_screen, 857 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 858 "Choose what will be displayed by drm_panic, 'user', 'kmsg' or 'qr_code' [default=" 859 #else 860 "Choose what will be displayed by drm_panic, 'user' or 'kmsg' [default=" 861 #endif 862 CONFIG_DRM_PANIC_SCREEN "]"); 863 864 /* 865 * drm_panic_is_format_supported() 866 * @format: a fourcc color code 867 * Returns: true if supported, false otherwise. 868 * 869 * Check if drm_panic will be able to use this color format. 870 */ 871 static bool drm_panic_is_format_supported(const struct drm_format_info *format) 872 { 873 if (format->num_planes != 1) 874 return false; 875 return drm_draw_can_convert_from_xrgb8888(format->format); 876 } 877 878 static void draw_panic_dispatch(struct drm_scanout_buffer *sb) 879 { 880 switch (drm_panic_type) { 881 case DRM_PANIC_TYPE_KMSG: 882 draw_panic_screen_kmsg(sb); 883 break; 884 885 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 886 case DRM_PANIC_TYPE_QR: 887 draw_panic_screen_qr_code(sb); 888 break; 889 #endif 890 891 case DRM_PANIC_TYPE_USER: 892 default: 893 draw_panic_screen_user(sb); 894 } 895 } 896 897 static void drm_panic_set_description(const char *description) 898 { 899 u32 len; 900 901 if (description) { 902 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1]; 903 904 desc_line->txt = description; 905 len = strlen(description); 906 /* ignore the last newline character */ 907 if (len && description[len - 1] == '\n') 908 len -= 1; 909 desc_line->len = len; 910 } 911 } 912 913 static void drm_panic_clear_description(void) 914 { 915 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1]; 916 917 desc_line->len = 0; 918 desc_line->txt = NULL; 919 } 920 921 static void draw_panic_plane(struct drm_plane *plane, const char *description) 922 { 923 struct drm_scanout_buffer sb = { }; 924 int ret; 925 unsigned long flags; 926 927 if (!drm_panic_trylock(plane->dev, flags)) 928 return; 929 930 ret = plane->helper_private->get_scanout_buffer(plane, &sb); 931 932 if (ret || !drm_panic_is_format_supported(sb.format)) 933 goto unlock; 934 935 /* One of these should be set, or it can't draw pixels */ 936 if (!sb.set_pixel && !sb.pages && iosys_map_is_null(&sb.map[0])) 937 goto unlock; 938 939 drm_panic_set_description(description); 940 941 draw_panic_dispatch(&sb); 942 if (plane->helper_private->panic_flush) 943 plane->helper_private->panic_flush(plane); 944 945 drm_panic_clear_description(); 946 947 unlock: 948 drm_panic_unlock(plane->dev, flags); 949 } 950 951 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd) 952 { 953 return container_of(kd, struct drm_plane, kmsg_panic); 954 } 955 956 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail) 957 { 958 struct drm_plane *plane = to_drm_plane(dumper); 959 960 if (detail->reason == KMSG_DUMP_PANIC) 961 draw_panic_plane(plane, detail->description); 962 } 963 964 965 /* 966 * DEBUG FS, This is currently unsafe. 967 * Create one file per plane, so it's possible to debug one plane at a time. 968 * TODO: It would be better to emulate an NMI context. 969 */ 970 #ifdef CONFIG_DRM_PANIC_DEBUG 971 #include <linux/debugfs.h> 972 973 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf, 974 size_t count, loff_t *ppos) 975 { 976 bool run; 977 978 if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) { 979 struct drm_plane *plane = file->private_data; 980 981 draw_panic_plane(plane, "Test from debugfs"); 982 } 983 return count; 984 } 985 986 static const struct file_operations dbg_drm_panic_ops = { 987 .owner = THIS_MODULE, 988 .write = debugfs_trigger_write, 989 .open = simple_open, 990 }; 991 992 static void debugfs_register_plane(struct drm_plane *plane, int index) 993 { 994 char fname[32]; 995 996 snprintf(fname, 32, "drm_panic_plane_%d", index); 997 debugfs_create_file(fname, 0200, plane->dev->debugfs_root, 998 plane, &dbg_drm_panic_ops); 999 } 1000 #else 1001 static void debugfs_register_plane(struct drm_plane *plane, int index) {} 1002 #endif /* CONFIG_DRM_PANIC_DEBUG */ 1003 1004 /** 1005 * drm_panic_is_enabled 1006 * @dev: the drm device that may supports drm_panic 1007 * 1008 * returns true if the drm device supports drm_panic 1009 */ 1010 bool drm_panic_is_enabled(struct drm_device *dev) 1011 { 1012 struct drm_plane *plane; 1013 1014 if (!dev->mode_config.num_total_plane) 1015 return false; 1016 1017 drm_for_each_plane(plane, dev) 1018 if (plane->helper_private && plane->helper_private->get_scanout_buffer) 1019 return true; 1020 return false; 1021 } 1022 EXPORT_SYMBOL(drm_panic_is_enabled); 1023 1024 /** 1025 * drm_panic_register() - Initialize DRM panic for a device 1026 * @dev: the drm device on which the panic screen will be displayed. 1027 */ 1028 void drm_panic_register(struct drm_device *dev) 1029 { 1030 struct drm_plane *plane; 1031 int registered_plane = 0; 1032 1033 if (!dev->mode_config.num_total_plane) 1034 return; 1035 1036 drm_for_each_plane(plane, dev) { 1037 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer) 1038 continue; 1039 plane->kmsg_panic.dump = drm_panic; 1040 plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC; 1041 if (kmsg_dump_register(&plane->kmsg_panic)) 1042 drm_warn(dev, "Failed to register panic handler\n"); 1043 else { 1044 debugfs_register_plane(plane, registered_plane); 1045 registered_plane++; 1046 } 1047 } 1048 if (registered_plane) 1049 drm_info(dev, "Registered %d planes with drm panic\n", registered_plane); 1050 } 1051 1052 /** 1053 * drm_panic_unregister() 1054 * @dev: the drm device previously registered. 1055 */ 1056 void drm_panic_unregister(struct drm_device *dev) 1057 { 1058 struct drm_plane *plane; 1059 1060 if (!dev->mode_config.num_total_plane) 1061 return; 1062 1063 drm_for_each_plane(plane, dev) { 1064 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer) 1065 continue; 1066 kmsg_dump_unregister(&plane->kmsg_panic); 1067 } 1068 } 1069 1070 /** 1071 * drm_panic_init() - initialize DRM panic. 1072 */ 1073 void __init drm_panic_init(void) 1074 { 1075 if (drm_panic_type == -1 && drm_panic_type_set(CONFIG_DRM_PANIC_SCREEN, NULL)) { 1076 pr_warn("Unsupported value for CONFIG_DRM_PANIC_SCREEN ('%s'), falling back to 'user'...\n", 1077 CONFIG_DRM_PANIC_SCREEN); 1078 drm_panic_type = DRM_PANIC_TYPE_USER; 1079 } 1080 drm_panic_qr_init(); 1081 } 1082 1083 /** 1084 * drm_panic_exit() - Free the resources taken by drm_panic_exit() 1085 */ 1086 void drm_panic_exit(void) 1087 { 1088 drm_panic_qr_exit(); 1089 } 1090 1091 #ifdef CONFIG_DRM_KUNIT_TEST 1092 #include "tests/drm_panic_test.c" 1093 #endif 1094