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