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/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> 23 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> 32 33 #include "drm_crtc_internal.h" 34 35 MODULE_AUTHOR("Jocelyn Falempe"); 36 MODULE_DESCRIPTION("DRM panic handler"); 37 MODULE_LICENSE("GPL"); 38 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 "]"); 44 45 /** 46 * DOC: overview 47 * 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. 58 */ 59 60 /* 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 63 * is disabled. 64 * It's intended for end-user, so have minimal technical/debug information. 65 * 66 * Implementation details: 67 * 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 71 * modesetting). 72 * It will display only one static frame, so performance optimizations are low 73 * priority as the machine is already in an unusable state. 74 */ 75 76 struct drm_panic_line { 77 u32 len; 78 const char *txt; 79 }; 80 81 #define PANIC_LINE(s) {.len = sizeof(s) - 1, .txt = s} 82 83 static struct drm_panic_line panic_msg[] = { 84 PANIC_LINE("KERNEL PANIC!"), 85 PANIC_LINE(""), 86 PANIC_LINE("Please reboot your computer."), 87 PANIC_LINE(""), 88 PANIC_LINE(""), /* will be replaced by the panic description */ 89 }; 90 91 static const size_t panic_msg_lines = ARRAY_SIZE(panic_msg); 92 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(" \\___)=(___/"), 101 }; 102 103 static const size_t logo_ascii_lines = ARRAY_SIZE(logo_ascii); 104 105 #if defined(CONFIG_LOGO) && !defined(MODULE) 106 static const struct linux_logo *logo_mono; 107 108 static int drm_panic_setup_logo(void) 109 { 110 const struct linux_logo *logo = fb_find_logo(1); 111 const unsigned char *logo_data; 112 struct linux_logo *logo_dup; 113 114 if (!logo || logo->type != LINUX_LOGO_MONO) 115 return 0; 116 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), 120 GFP_KERNEL); 121 if (!logo_data) 122 return -ENOMEM; 123 124 logo_dup = kmemdup(logo, sizeof(*logo), GFP_KERNEL); 125 if (!logo_dup) { 126 kfree(logo_data); 127 return -ENOMEM; 128 } 129 130 logo_dup->data = logo_data; 131 logo_mono = logo_dup; 132 133 return 0; 134 } 135 136 device_initcall(drm_panic_setup_logo); 137 #else 138 #define logo_mono ((const struct linux_logo *)NULL) 139 #endif 140 141 /* 142 * Color conversion 143 */ 144 145 static u16 convert_xrgb8888_to_rgb565(u32 pix) 146 { 147 return ((pix & 0x00F80000) >> 8) | 148 ((pix & 0x0000FC00) >> 5) | 149 ((pix & 0x000000F8) >> 3); 150 } 151 152 static u16 convert_xrgb8888_to_rgba5551(u32 pix) 153 { 154 return ((pix & 0x00f80000) >> 8) | 155 ((pix & 0x0000f800) >> 5) | 156 ((pix & 0x000000f8) >> 2) | 157 BIT(0); /* set alpha bit */ 158 } 159 160 static u16 convert_xrgb8888_to_xrgb1555(u32 pix) 161 { 162 return ((pix & 0x00f80000) >> 9) | 163 ((pix & 0x0000f800) >> 6) | 164 ((pix & 0x000000f8) >> 3); 165 } 166 167 static u16 convert_xrgb8888_to_argb1555(u32 pix) 168 { 169 return BIT(15) | /* set alpha bit */ 170 ((pix & 0x00f80000) >> 9) | 171 ((pix & 0x0000f800) >> 6) | 172 ((pix & 0x000000f8) >> 3); 173 } 174 175 static u32 convert_xrgb8888_to_argb8888(u32 pix) 176 { 177 return pix | GENMASK(31, 24); /* fill alpha bits */ 178 } 179 180 static u32 convert_xrgb8888_to_xbgr8888(u32 pix) 181 { 182 return ((pix & 0x00ff0000) >> 16) << 0 | 183 ((pix & 0x0000ff00) >> 8) << 8 | 184 ((pix & 0x000000ff) >> 0) << 16 | 185 ((pix & 0xff000000) >> 24) << 24; 186 } 187 188 static u32 convert_xrgb8888_to_abgr8888(u32 pix) 189 { 190 return ((pix & 0x00ff0000) >> 16) << 0 | 191 ((pix & 0x0000ff00) >> 8) << 8 | 192 ((pix & 0x000000ff) >> 0) << 16 | 193 GENMASK(31, 24); /* fill alpha bits */ 194 } 195 196 static u32 convert_xrgb8888_to_xrgb2101010(u32 pix) 197 { 198 pix = ((pix & 0x000000FF) << 2) | 199 ((pix & 0x0000FF00) << 4) | 200 ((pix & 0x00FF0000) << 6); 201 return pix | ((pix >> 8) & 0x00300C03); 202 } 203 204 static u32 convert_xrgb8888_to_argb2101010(u32 pix) 205 { 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); 210 } 211 212 /* 213 * convert_from_xrgb8888 - convert one pixel from xrgb8888 to the desired format 214 * @color: input color, in xrgb8888 format 215 * @format: output format 216 * 217 * Returns: 218 * Color in the format specified, casted to u32. 219 * Or 0 if the format is not supported. 220 */ 221 static u32 convert_from_xrgb8888(u32 color, u32 format) 222 { 223 switch (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: 234 return color; 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); 245 default: 246 WARN_ONCE(1, "Can't convert to %p4cc\n", &format); 247 return 0; 248 } 249 } 250 251 /* 252 * Blit & Fill 253 */ 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) 256 { 257 return (sbuf8[(y * spitch) + x / 8] & (0x80 >> (x % 8))) != 0; 258 } 259 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) 264 { 265 unsigned int y, x; 266 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); 271 } 272 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) 277 { 278 unsigned int y, x; 279 280 for (y = 0; y < height; y++) { 281 for (x = 0; x < width; x++) { 282 u32 off = y * dpitch + x * 3; 283 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); 289 } 290 } 291 } 292 } 293 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) 298 { 299 unsigned int y, x; 300 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); 305 } 306 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, 309 u32 fg_color) 310 { 311 unsigned int y, x; 312 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); 317 } 318 319 /* 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 326 * rectangle 327 * @fg_color: foreground color, in destination format 328 * 329 * This can be used to draw a font character, which is a monochrome image, to a 330 * framebuffer in other supported format. 331 */ 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) 335 336 { 337 struct iosys_map map; 338 339 if (sb->set_pixel) 340 return drm_panic_blit_pixel(sb, clip, sbuf8, spitch, scale, fg_color); 341 342 map = sb->map[0]; 343 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]); 344 345 switch (sb->format->cpp[0]) { 346 case 2: 347 drm_panic_blit16(&map, sb->pitch[0], sbuf8, spitch, 348 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 349 break; 350 case 3: 351 drm_panic_blit24(&map, sb->pitch[0], sbuf8, spitch, 352 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 353 break; 354 case 4: 355 drm_panic_blit32(&map, sb->pitch[0], sbuf8, spitch, 356 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 357 break; 358 default: 359 WARN_ONCE(1, "Can't blit with pixel width %d\n", sb->format->cpp[0]); 360 } 361 } 362 363 static void drm_panic_fill16(struct iosys_map *dmap, unsigned int dpitch, 364 unsigned int height, unsigned int width, 365 u16 color) 366 { 367 unsigned int y, x; 368 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); 372 } 373 374 static void drm_panic_fill24(struct iosys_map *dmap, unsigned int dpitch, 375 unsigned int height, unsigned int width, 376 u32 color) 377 { 378 unsigned int y, x; 379 380 for (y = 0; y < height; y++) { 381 for (x = 0; x < width; x++) { 382 unsigned int off = y * dpitch + x * 3; 383 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); 388 } 389 } 390 } 391 392 static void drm_panic_fill32(struct iosys_map *dmap, unsigned int dpitch, 393 unsigned int height, unsigned int width, 394 u32 color) 395 { 396 unsigned int y, x; 397 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); 401 } 402 403 static void drm_panic_fill_pixel(struct drm_scanout_buffer *sb, 404 struct drm_rect *clip, 405 u32 color) 406 { 407 unsigned int y, x; 408 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); 412 } 413 414 /* 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 419 * 420 * Fill a rectangle with a color, in a linear framebuffer. 421 */ 422 static void drm_panic_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip, 423 u32 color) 424 { 425 struct iosys_map map; 426 427 if (sb->set_pixel) 428 return drm_panic_fill_pixel(sb, clip, color); 429 430 map = sb->map[0]; 431 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]); 432 433 switch (sb->format->cpp[0]) { 434 case 2: 435 drm_panic_fill16(&map, sb->pitch[0], drm_rect_height(clip), 436 drm_rect_width(clip), color); 437 break; 438 case 3: 439 drm_panic_fill24(&map, sb->pitch[0], drm_rect_height(clip), 440 drm_rect_width(clip), color); 441 break; 442 case 4: 443 drm_panic_fill32(&map, sb->pitch[0], drm_rect_height(clip), 444 drm_rect_width(clip), color); 445 break; 446 default: 447 WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]); 448 } 449 } 450 451 static const u8 *get_char_bitmap(const struct font_desc *font, char c, size_t font_pitch) 452 { 453 return font->data + (c * font->height) * font_pitch; 454 } 455 456 static unsigned int get_max_line_len(const struct drm_panic_line *lines, int len) 457 { 458 int i; 459 unsigned int max = 0; 460 461 for (i = 0; i < len; i++) 462 max = max(lines[i].len, max); 463 return max; 464 } 465 466 /* 467 * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle 468 */ 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, 473 bool centered, 474 struct drm_rect *clip, 475 u32 color) 476 { 477 int i, j; 478 const u8 *src; 479 size_t font_pitch = DIV_ROUND_UP(font->width, 8); 480 struct drm_rect rec; 481 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); 485 486 rec.y1 = clip->y1 + i * font->height; 487 rec.y2 = rec.y1 + font->height; 488 rec.x1 = clip->x1; 489 490 if (centered) 491 rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2; 492 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; 498 } 499 } 500 } 501 502 static void drm_panic_logo_rect(struct drm_rect *rect, const struct font_desc *font) 503 { 504 if (logo_mono) { 505 drm_rect_init(rect, 0, 0, logo_mono->width, logo_mono->height); 506 } else { 507 int logo_width = get_max_line_len(logo_ascii, logo_ascii_lines) * font->width; 508 509 drm_rect_init(rect, 0, 0, logo_width, logo_ascii_lines * font->height); 510 } 511 } 512 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) 515 { 516 if (logo_mono) 517 drm_panic_blit(sb, rect, logo_mono->data, 518 DIV_ROUND_UP(drm_rect_width(rect), 8), 1, fg_color); 519 else 520 draw_txt_rectangle(sb, font, logo_ascii, logo_ascii_lines, false, rect, 521 fg_color); 522 } 523 524 static void draw_panic_static_user(struct drm_scanout_buffer *sb) 525 { 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; 531 532 if (!font) 533 return; 534 535 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 536 drm_panic_logo_rect(&r_logo, font); 537 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); 541 542 /* Center the panic message */ 543 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2); 544 545 /* Fill with the background color, and draw text on top */ 546 drm_panic_fill(sb, &r_screen, bg_color); 547 548 if (!drm_rect_overlap(&r_logo, &r_msg)) 549 drm_panic_logo_draw(sb, &r_logo, font, fg_color); 550 551 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color); 552 } 553 554 /* 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. 557 */ 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) 560 { 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; 564 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; 570 if (r_txt.y1 < 0) 571 return r_txt.y1; 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; 577 if (r_txt.y1 < 0) 578 return r_txt.y1; 579 } 580 } else { 581 draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color); 582 r_txt.y1 -= font->height; 583 } 584 return r_txt.y1; 585 } 586 587 /* 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. 590 */ 591 static void draw_panic_static_kmsg(struct drm_scanout_buffer *sb) 592 { 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; 598 char kmsg_buf[512]; 599 size_t kmsg_len; 600 struct drm_panic_line line; 601 int yoffset; 602 603 if (!font) 604 return; 605 606 yoffset = sb->height - font->height - (sb->height % font->height) / 2; 607 608 /* Fill with the background color, and draw text on top */ 609 drm_panic_fill(sb, &r_screen, bg_color); 610 611 kmsg_dump_rewind(&iter); 612 while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) { 613 char *start; 614 char *end; 615 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') 621 start--; 622 /* don't count the newline character */ 623 line.txt = start + (start == kmsg_buf ? 0 : 1); 624 line.len = end - line.txt; 625 626 yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color); 627 end = start; 628 start--; 629 } 630 } 631 } 632 633 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE) 634 /* 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 643 * data segments. 644 * 645 * Typically, ~7500 bytes of kmsg, are compressed into 2800 bytes, which fits in 646 * a V40 QR-code (177x177). 647 * 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 653 */ 654 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"); 658 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 */ 664 665 /* Compression parameters */ 666 #define COMPR_LEVEL 6 667 #define WINDOW_BITS 12 668 #define MEM_LEVEL 4 669 670 static char *qrbuf1; 671 static char *qrbuf2; 672 static struct z_stream_s stream; 673 674 static void __init drm_panic_qr_init(void) 675 { 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), 679 GFP_KERNEL); 680 } 681 682 static void drm_panic_qr_exit(void) 683 { 684 kfree(qrbuf1); 685 qrbuf1 = NULL; 686 kfree(qrbuf2); 687 qrbuf2 = NULL; 688 kfree(stream.workspace); 689 stream.workspace = NULL; 690 } 691 692 extern size_t drm_panic_qr_max_data_size(u8 version, size_t url_len); 693 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); 696 697 static int drm_panic_get_qr_code_url(u8 **qr_image) 698 { 699 struct kmsg_dump_iter iter; 700 char url[256]; 701 size_t kmsg_len, max_kmsg_size; 702 char *kmsg; 703 int max_qr_data_size, url_len; 704 705 url_len = snprintf(url, sizeof(url), CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL "?a=%s&v=%s&zl=", 706 utsname()->machine, utsname()->release); 707 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); 710 711 /* get kmsg to buffer 1 */ 712 kmsg_dump_rewind(&iter); 713 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len); 714 715 if (!kmsg_len) 716 return -ENODATA; 717 kmsg = qrbuf1; 718 719 try_again: 720 if (zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, 721 MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) 722 return -EINVAL; 723 724 stream.next_in = kmsg; 725 stream.avail_in = kmsg_len; 726 stream.total_in = 0; 727 stream.next_out = qrbuf2; 728 stream.avail_out = QR_BUFFER2_SIZE; 729 stream.total_out = 0; 730 731 if (zlib_deflate(&stream, Z_FINISH) != Z_STREAM_END) 732 return -EINVAL; 733 734 if (zlib_deflateEnd(&stream) != Z_OK) 735 return -EINVAL; 736 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'); 740 if (!kmsg) 741 return -EINVAL; 742 /* skip the first \n */ 743 kmsg += 1; 744 kmsg_len = strlen(kmsg); 745 goto try_again; 746 } 747 *qr_image = qrbuf2; 748 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); 752 } 753 754 static int drm_panic_get_qr_code_raw(u8 **qr_image) 755 { 756 struct kmsg_dump_iter iter; 757 size_t kmsg_len; 758 size_t max_kmsg_size = min(drm_panic_qr_max_data_size(panic_qr_version, 0), 759 QR_BUFFER1_SIZE); 760 761 kmsg_dump_rewind(&iter); 762 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len); 763 if (!kmsg_len) 764 return -ENODATA; 765 766 *qr_image = qrbuf1; 767 return drm_panic_qr_generate(NULL, qrbuf1, kmsg_len, QR_BUFFER1_SIZE, 768 qrbuf2, QR_BUFFER2_SIZE); 769 } 770 771 static int drm_panic_get_qr_code(u8 **qr_image) 772 { 773 if (strlen(CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL) > 0) 774 return drm_panic_get_qr_code_url(qr_image); 775 else 776 return drm_panic_get_qr_code_raw(qr_image); 777 } 778 779 /* 780 * Draw the panic message at the center of the screen, with a QR Code 781 */ 782 static int _draw_panic_static_qr_code(struct drm_scanout_buffer *sb) 783 { 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; 791 u8 *qr_image; 792 793 if (!font || !qrbuf1 || !qrbuf2 || !stream.workspace) 794 return -ENOMEM; 795 796 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 797 798 drm_panic_logo_rect(&r_logo, font); 799 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); 803 804 max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4); 805 806 qr_width = drm_panic_get_qr_code(&qr_image); 807 if (qr_width <= 0) 808 return -ENOSPC; 809 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 */ 813 if (scale < 2) 814 return -ENOSPC; 815 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); 818 819 v_margin = (sb->height - drm_rect_height(&r_qr_canvas) - drm_rect_height(&r_msg)) / 5; 820 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); 824 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)); 828 829 /* Fill with the background color, and draw text on top */ 830 drm_panic_fill(sb, &r_screen, bg_color); 831 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); 834 835 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color); 836 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); 842 return 0; 843 } 844 845 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb) 846 { 847 if (_draw_panic_static_qr_code(sb)) 848 draw_panic_static_user(sb); 849 } 850 #else 851 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb) 852 { 853 draw_panic_static_user(sb); 854 } 855 856 static void drm_panic_qr_init(void) {}; 857 static void drm_panic_qr_exit(void) {}; 858 #endif 859 860 /* 861 * drm_panic_is_format_supported() 862 * @format: a fourcc color code 863 * Returns: true if supported, false otherwise. 864 * 865 * Check if drm_panic will be able to use this color format. 866 */ 867 static bool drm_panic_is_format_supported(const struct drm_format_info *format) 868 { 869 if (format->num_planes != 1) 870 return false; 871 return convert_from_xrgb8888(0xffffff, format->format) != 0; 872 } 873 874 static void draw_panic_dispatch(struct drm_scanout_buffer *sb) 875 { 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); 880 } else { 881 draw_panic_static_user(sb); 882 } 883 } 884 885 static void drm_panic_set_description(const char *description) 886 { 887 u32 len; 888 889 if (description) { 890 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1]; 891 892 desc_line->txt = description; 893 len = strlen(description); 894 /* ignore the last newline character */ 895 if (len && description[len - 1] == '\n') 896 len -= 1; 897 desc_line->len = len; 898 } 899 } 900 901 static void drm_panic_clear_description(void) 902 { 903 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1]; 904 905 desc_line->len = 0; 906 desc_line->txt = NULL; 907 } 908 909 static void draw_panic_plane(struct drm_plane *plane, const char *description) 910 { 911 struct drm_scanout_buffer sb = { }; 912 int ret; 913 unsigned long flags; 914 915 if (!drm_panic_trylock(plane->dev, flags)) 916 return; 917 918 drm_panic_set_description(description); 919 920 ret = plane->helper_private->get_scanout_buffer(plane, &sb); 921 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); 926 } 927 drm_panic_clear_description(); 928 drm_panic_unlock(plane->dev, flags); 929 } 930 931 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd) 932 { 933 return container_of(kd, struct drm_plane, kmsg_panic); 934 } 935 936 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail) 937 { 938 struct drm_plane *plane = to_drm_plane(dumper); 939 940 if (detail->reason == KMSG_DUMP_PANIC) 941 draw_panic_plane(plane, detail->description); 942 } 943 944 945 /* 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. 949 */ 950 #ifdef CONFIG_DRM_PANIC_DEBUG 951 #include <linux/debugfs.h> 952 953 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf, 954 size_t count, loff_t *ppos) 955 { 956 bool run; 957 958 if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) { 959 struct drm_plane *plane = file->private_data; 960 961 draw_panic_plane(plane, "Test from debugfs"); 962 } 963 return count; 964 } 965 966 static const struct file_operations dbg_drm_panic_ops = { 967 .owner = THIS_MODULE, 968 .write = debugfs_trigger_write, 969 .open = simple_open, 970 }; 971 972 static void debugfs_register_plane(struct drm_plane *plane, int index) 973 { 974 char fname[32]; 975 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); 979 } 980 #else 981 static void debugfs_register_plane(struct drm_plane *plane, int index) {} 982 #endif /* CONFIG_DRM_PANIC_DEBUG */ 983 984 /** 985 * drm_panic_is_enabled 986 * @dev: the drm device that may supports drm_panic 987 * 988 * returns true if the drm device supports drm_panic 989 */ 990 bool drm_panic_is_enabled(struct drm_device *dev) 991 { 992 struct drm_plane *plane; 993 994 if (!dev->mode_config.num_total_plane) 995 return false; 996 997 drm_for_each_plane(plane, dev) 998 if (plane->helper_private && plane->helper_private->get_scanout_buffer) 999 return true; 1000 return false; 1001 } 1002 EXPORT_SYMBOL(drm_panic_is_enabled); 1003 1004 /** 1005 * drm_panic_register() - Initialize DRM panic for a device 1006 * @dev: the drm device on which the panic screen will be displayed. 1007 */ 1008 void drm_panic_register(struct drm_device *dev) 1009 { 1010 struct drm_plane *plane; 1011 int registered_plane = 0; 1012 1013 if (!dev->mode_config.num_total_plane) 1014 return; 1015 1016 drm_for_each_plane(plane, dev) { 1017 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer) 1018 continue; 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"); 1023 else { 1024 debugfs_register_plane(plane, registered_plane); 1025 registered_plane++; 1026 } 1027 } 1028 if (registered_plane) 1029 drm_info(dev, "Registered %d planes with drm panic\n", registered_plane); 1030 } 1031 1032 /** 1033 * drm_panic_unregister() 1034 * @dev: the drm device previously registered. 1035 */ 1036 void drm_panic_unregister(struct drm_device *dev) 1037 { 1038 struct drm_plane *plane; 1039 1040 if (!dev->mode_config.num_total_plane) 1041 return; 1042 1043 drm_for_each_plane(plane, dev) { 1044 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer) 1045 continue; 1046 kmsg_dump_unregister(&plane->kmsg_panic); 1047 } 1048 } 1049 1050 /** 1051 * drm_panic_init() - initialize DRM panic. 1052 */ 1053 void __init drm_panic_init(void) 1054 { 1055 drm_panic_qr_init(); 1056 } 1057 1058 /** 1059 * drm_panic_exit() - Free the resources taken by drm_panic_exit() 1060 */ 1061 void drm_panic_exit(void) 1062 { 1063 drm_panic_qr_exit(); 1064 } 1065