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 22 #include <drm/drm_drv.h> 23 #include <drm/drm_format_helper.h> 24 #include <drm/drm_fourcc.h> 25 #include <drm/drm_framebuffer.h> 26 #include <drm/drm_modeset_helper_vtables.h> 27 #include <drm/drm_panic.h> 28 #include <drm/drm_plane.h> 29 #include <drm/drm_print.h> 30 31 MODULE_AUTHOR("Jocelyn Falempe"); 32 MODULE_DESCRIPTION("DRM panic handler"); 33 MODULE_LICENSE("GPL"); 34 35 static char drm_panic_screen[16] = CONFIG_DRM_PANIC_SCREEN; 36 module_param_string(panic_screen, drm_panic_screen, sizeof(drm_panic_screen), 0644); 37 MODULE_PARM_DESC(panic_screen, 38 "Choose what will be displayed by drm_panic, 'user' or 'kmsg' [default=" 39 CONFIG_DRM_PANIC_SCREEN "]"); 40 41 /** 42 * DOC: overview 43 * 44 * To enable DRM panic for a driver, the primary plane must implement a 45 * &drm_plane_helper_funcs.get_scanout_buffer helper function. It is then 46 * automatically registered to the drm panic handler. 47 * When a panic occurs, the &drm_plane_helper_funcs.get_scanout_buffer will be 48 * called, and the driver can provide a framebuffer so the panic handler can 49 * draw the panic screen on it. Currently only linear buffer and a few color 50 * formats are supported. 51 * Optionally the driver can also provide a &drm_plane_helper_funcs.panic_flush 52 * callback, that will be called after that, to send additional commands to the 53 * hardware to make the scanout buffer visible. 54 */ 55 56 /* 57 * This module displays a user friendly message on screen when a kernel panic 58 * occurs. This is conflicting with fbcon, so you can only enable it when fbcon 59 * is disabled. 60 * It's intended for end-user, so have minimal technical/debug information. 61 * 62 * Implementation details: 63 * 64 * It is a panic handler, so it can't take lock, allocate memory, run tasks/irq, 65 * or attempt to sleep. It's a best effort, and it may not be able to display 66 * the message in all situations (like if the panic occurs in the middle of a 67 * modesetting). 68 * It will display only one static frame, so performance optimizations are low 69 * priority as the machine is already in an unusable state. 70 */ 71 72 struct drm_panic_line { 73 u32 len; 74 const char *txt; 75 }; 76 77 #define PANIC_LINE(s) {.len = sizeof(s) - 1, .txt = s} 78 79 static struct drm_panic_line panic_msg[] = { 80 PANIC_LINE("KERNEL PANIC !"), 81 PANIC_LINE(""), 82 PANIC_LINE("Please reboot your computer."), 83 }; 84 85 static const struct drm_panic_line logo_ascii[] = { 86 PANIC_LINE(" .--. _"), 87 PANIC_LINE(" |o_o | | |"), 88 PANIC_LINE(" |:_/ | | |"), 89 PANIC_LINE(" // \\ \\ |_|"), 90 PANIC_LINE(" (| | ) _"), 91 PANIC_LINE(" /'\\_ _/`\\ (_)"), 92 PANIC_LINE(" \\___)=(___/"), 93 }; 94 95 #ifdef CONFIG_LOGO 96 static const struct linux_logo *logo_mono; 97 98 static int drm_panic_setup_logo(void) 99 { 100 const struct linux_logo *logo = fb_find_logo(1); 101 const unsigned char *logo_data; 102 struct linux_logo *logo_dup; 103 104 if (!logo || logo->type != LINUX_LOGO_MONO) 105 return 0; 106 107 /* The logo is __init, so we must make a copy for later use */ 108 logo_data = kmemdup(logo->data, 109 size_mul(DIV_ROUND_UP(logo->width, BITS_PER_BYTE), logo->height), 110 GFP_KERNEL); 111 if (!logo_data) 112 return -ENOMEM; 113 114 logo_dup = kmemdup(logo, sizeof(*logo), GFP_KERNEL); 115 if (!logo_dup) { 116 kfree(logo_data); 117 return -ENOMEM; 118 } 119 120 logo_dup->data = logo_data; 121 logo_mono = logo_dup; 122 123 return 0; 124 } 125 126 device_initcall(drm_panic_setup_logo); 127 #else 128 #define logo_mono ((const struct linux_logo *)NULL) 129 #endif 130 131 /* 132 * Color conversion 133 */ 134 135 static u16 convert_xrgb8888_to_rgb565(u32 pix) 136 { 137 return ((pix & 0x00F80000) >> 8) | 138 ((pix & 0x0000FC00) >> 5) | 139 ((pix & 0x000000F8) >> 3); 140 } 141 142 static u16 convert_xrgb8888_to_rgba5551(u32 pix) 143 { 144 return ((pix & 0x00f80000) >> 8) | 145 ((pix & 0x0000f800) >> 5) | 146 ((pix & 0x000000f8) >> 2) | 147 BIT(0); /* set alpha bit */ 148 } 149 150 static u16 convert_xrgb8888_to_xrgb1555(u32 pix) 151 { 152 return ((pix & 0x00f80000) >> 9) | 153 ((pix & 0x0000f800) >> 6) | 154 ((pix & 0x000000f8) >> 3); 155 } 156 157 static u16 convert_xrgb8888_to_argb1555(u32 pix) 158 { 159 return BIT(15) | /* set alpha bit */ 160 ((pix & 0x00f80000) >> 9) | 161 ((pix & 0x0000f800) >> 6) | 162 ((pix & 0x000000f8) >> 3); 163 } 164 165 static u32 convert_xrgb8888_to_argb8888(u32 pix) 166 { 167 return pix | GENMASK(31, 24); /* fill alpha bits */ 168 } 169 170 static u32 convert_xrgb8888_to_xbgr8888(u32 pix) 171 { 172 return ((pix & 0x00ff0000) >> 16) << 0 | 173 ((pix & 0x0000ff00) >> 8) << 8 | 174 ((pix & 0x000000ff) >> 0) << 16 | 175 ((pix & 0xff000000) >> 24) << 24; 176 } 177 178 static u32 convert_xrgb8888_to_abgr8888(u32 pix) 179 { 180 return ((pix & 0x00ff0000) >> 16) << 0 | 181 ((pix & 0x0000ff00) >> 8) << 8 | 182 ((pix & 0x000000ff) >> 0) << 16 | 183 GENMASK(31, 24); /* fill alpha bits */ 184 } 185 186 static u32 convert_xrgb8888_to_xrgb2101010(u32 pix) 187 { 188 pix = ((pix & 0x000000FF) << 2) | 189 ((pix & 0x0000FF00) << 4) | 190 ((pix & 0x00FF0000) << 6); 191 return pix | ((pix >> 8) & 0x00300C03); 192 } 193 194 static u32 convert_xrgb8888_to_argb2101010(u32 pix) 195 { 196 pix = ((pix & 0x000000FF) << 2) | 197 ((pix & 0x0000FF00) << 4) | 198 ((pix & 0x00FF0000) << 6); 199 return GENMASK(31, 30) /* set alpha bits */ | pix | ((pix >> 8) & 0x00300C03); 200 } 201 202 /* 203 * convert_from_xrgb8888 - convert one pixel from xrgb8888 to the desired format 204 * @color: input color, in xrgb8888 format 205 * @format: output format 206 * 207 * Returns: 208 * Color in the format specified, casted to u32. 209 * Or 0 if the format is not supported. 210 */ 211 static u32 convert_from_xrgb8888(u32 color, u32 format) 212 { 213 switch (format) { 214 case DRM_FORMAT_RGB565: 215 return convert_xrgb8888_to_rgb565(color); 216 case DRM_FORMAT_RGBA5551: 217 return convert_xrgb8888_to_rgba5551(color); 218 case DRM_FORMAT_XRGB1555: 219 return convert_xrgb8888_to_xrgb1555(color); 220 case DRM_FORMAT_ARGB1555: 221 return convert_xrgb8888_to_argb1555(color); 222 case DRM_FORMAT_RGB888: 223 case DRM_FORMAT_XRGB8888: 224 return color; 225 case DRM_FORMAT_ARGB8888: 226 return convert_xrgb8888_to_argb8888(color); 227 case DRM_FORMAT_XBGR8888: 228 return convert_xrgb8888_to_xbgr8888(color); 229 case DRM_FORMAT_ABGR8888: 230 return convert_xrgb8888_to_abgr8888(color); 231 case DRM_FORMAT_XRGB2101010: 232 return convert_xrgb8888_to_xrgb2101010(color); 233 case DRM_FORMAT_ARGB2101010: 234 return convert_xrgb8888_to_argb2101010(color); 235 default: 236 WARN_ONCE(1, "Can't convert to %p4cc\n", &format); 237 return 0; 238 } 239 } 240 241 /* 242 * Blit & Fill 243 */ 244 /* check if the pixel at coord x,y is 1 (foreground) or 0 (background) */ 245 static bool drm_panic_is_pixel_fg(const u8 *sbuf8, unsigned int spitch, int x, int y) 246 { 247 return (sbuf8[(y * spitch) + x / 8] & (0x80 >> (x % 8))) != 0; 248 } 249 250 static void drm_panic_blit16(struct iosys_map *dmap, unsigned int dpitch, 251 const u8 *sbuf8, unsigned int spitch, 252 unsigned int height, unsigned int width, 253 u16 fg16) 254 { 255 unsigned int y, x; 256 257 for (y = 0; y < height; y++) 258 for (x = 0; x < width; x++) 259 if (drm_panic_is_pixel_fg(sbuf8, spitch, x, y)) 260 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, fg16); 261 } 262 263 static void drm_panic_blit24(struct iosys_map *dmap, unsigned int dpitch, 264 const u8 *sbuf8, unsigned int spitch, 265 unsigned int height, unsigned int width, 266 u32 fg32) 267 { 268 unsigned int y, x; 269 270 for (y = 0; y < height; y++) { 271 for (x = 0; x < width; x++) { 272 u32 off = y * dpitch + x * 3; 273 274 if (drm_panic_is_pixel_fg(sbuf8, spitch, x, y)) { 275 /* write blue-green-red to output in little endianness */ 276 iosys_map_wr(dmap, off, u8, (fg32 & 0x000000FF) >> 0); 277 iosys_map_wr(dmap, off + 1, u8, (fg32 & 0x0000FF00) >> 8); 278 iosys_map_wr(dmap, off + 2, u8, (fg32 & 0x00FF0000) >> 16); 279 } 280 } 281 } 282 } 283 284 static void drm_panic_blit32(struct iosys_map *dmap, unsigned int dpitch, 285 const u8 *sbuf8, unsigned int spitch, 286 unsigned int height, unsigned int width, 287 u32 fg32) 288 { 289 unsigned int y, x; 290 291 for (y = 0; y < height; y++) 292 for (x = 0; x < width; x++) 293 if (drm_panic_is_pixel_fg(sbuf8, spitch, x, y)) 294 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, fg32); 295 } 296 297 static void drm_panic_blit_pixel(struct drm_scanout_buffer *sb, struct drm_rect *clip, 298 const u8 *sbuf8, unsigned int spitch, u32 fg_color) 299 { 300 unsigned int y, x; 301 302 for (y = 0; y < drm_rect_height(clip); y++) 303 for (x = 0; x < drm_rect_width(clip); x++) 304 if (drm_panic_is_pixel_fg(sbuf8, spitch, x, y)) 305 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, fg_color); 306 } 307 308 /* 309 * drm_panic_blit - convert a monochrome image to a linear framebuffer 310 * @sb: destination scanout buffer 311 * @clip: destination rectangle 312 * @sbuf8: source buffer, in monochrome format, 8 pixels per byte. 313 * @spitch: source pitch in bytes 314 * @fg_color: foreground color, in destination format 315 * 316 * This can be used to draw a font character, which is a monochrome image, to a 317 * framebuffer in other supported format. 318 */ 319 static void drm_panic_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip, 320 const u8 *sbuf8, unsigned int spitch, u32 fg_color) 321 { 322 struct iosys_map map; 323 324 if (sb->set_pixel) 325 return drm_panic_blit_pixel(sb, clip, sbuf8, spitch, fg_color); 326 327 map = sb->map[0]; 328 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]); 329 330 switch (sb->format->cpp[0]) { 331 case 2: 332 drm_panic_blit16(&map, sb->pitch[0], sbuf8, spitch, 333 drm_rect_height(clip), drm_rect_width(clip), fg_color); 334 break; 335 case 3: 336 drm_panic_blit24(&map, sb->pitch[0], sbuf8, spitch, 337 drm_rect_height(clip), drm_rect_width(clip), fg_color); 338 break; 339 case 4: 340 drm_panic_blit32(&map, sb->pitch[0], sbuf8, spitch, 341 drm_rect_height(clip), drm_rect_width(clip), fg_color); 342 break; 343 default: 344 WARN_ONCE(1, "Can't blit with pixel width %d\n", sb->format->cpp[0]); 345 } 346 } 347 348 static void drm_panic_fill16(struct iosys_map *dmap, unsigned int dpitch, 349 unsigned int height, unsigned int width, 350 u16 color) 351 { 352 unsigned int y, x; 353 354 for (y = 0; y < height; y++) 355 for (x = 0; x < width; x++) 356 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, color); 357 } 358 359 static void drm_panic_fill24(struct iosys_map *dmap, unsigned int dpitch, 360 unsigned int height, unsigned int width, 361 u32 color) 362 { 363 unsigned int y, x; 364 365 for (y = 0; y < height; y++) { 366 for (x = 0; x < width; x++) { 367 unsigned int off = y * dpitch + x * 3; 368 369 /* write blue-green-red to output in little endianness */ 370 iosys_map_wr(dmap, off, u8, (color & 0x000000FF) >> 0); 371 iosys_map_wr(dmap, off + 1, u8, (color & 0x0000FF00) >> 8); 372 iosys_map_wr(dmap, off + 2, u8, (color & 0x00FF0000) >> 16); 373 } 374 } 375 } 376 377 static void drm_panic_fill32(struct iosys_map *dmap, unsigned int dpitch, 378 unsigned int height, unsigned int width, 379 u32 color) 380 { 381 unsigned int y, x; 382 383 for (y = 0; y < height; y++) 384 for (x = 0; x < width; x++) 385 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, color); 386 } 387 388 static void drm_panic_fill_pixel(struct drm_scanout_buffer *sb, 389 struct drm_rect *clip, 390 u32 color) 391 { 392 unsigned int y, x; 393 394 for (y = 0; y < drm_rect_height(clip); y++) 395 for (x = 0; x < drm_rect_width(clip); x++) 396 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, color); 397 } 398 399 /* 400 * drm_panic_fill - Fill a rectangle with a color 401 * @sb: destination scanout buffer 402 * @clip: destination rectangle 403 * @color: foreground color, in destination format 404 * 405 * Fill a rectangle with a color, in a linear framebuffer. 406 */ 407 static void drm_panic_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip, 408 u32 color) 409 { 410 struct iosys_map map; 411 412 if (sb->set_pixel) 413 return drm_panic_fill_pixel(sb, clip, color); 414 415 map = sb->map[0]; 416 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]); 417 418 switch (sb->format->cpp[0]) { 419 case 2: 420 drm_panic_fill16(&map, sb->pitch[0], drm_rect_height(clip), 421 drm_rect_width(clip), color); 422 break; 423 case 3: 424 drm_panic_fill24(&map, sb->pitch[0], drm_rect_height(clip), 425 drm_rect_width(clip), color); 426 break; 427 case 4: 428 drm_panic_fill32(&map, sb->pitch[0], drm_rect_height(clip), 429 drm_rect_width(clip), color); 430 break; 431 default: 432 WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]); 433 } 434 } 435 436 static const u8 *get_char_bitmap(const struct font_desc *font, char c, size_t font_pitch) 437 { 438 return font->data + (c * font->height) * font_pitch; 439 } 440 441 static unsigned int get_max_line_len(const struct drm_panic_line *lines, int len) 442 { 443 int i; 444 unsigned int max = 0; 445 446 for (i = 0; i < len; i++) 447 max = max(lines[i].len, max); 448 return max; 449 } 450 451 /* 452 * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle 453 */ 454 static void draw_txt_rectangle(struct drm_scanout_buffer *sb, 455 const struct font_desc *font, 456 const struct drm_panic_line *msg, 457 unsigned int msg_lines, 458 bool centered, 459 struct drm_rect *clip, 460 u32 color) 461 { 462 int i, j; 463 const u8 *src; 464 size_t font_pitch = DIV_ROUND_UP(font->width, 8); 465 struct drm_rect rec; 466 467 msg_lines = min(msg_lines, drm_rect_height(clip) / font->height); 468 for (i = 0; i < msg_lines; i++) { 469 size_t line_len = min(msg[i].len, drm_rect_width(clip) / font->width); 470 471 rec.y1 = clip->y1 + i * font->height; 472 rec.y2 = rec.y1 + font->height; 473 rec.x1 = clip->x1; 474 475 if (centered) 476 rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2; 477 478 for (j = 0; j < line_len; j++) { 479 src = get_char_bitmap(font, msg[i].txt[j], font_pitch); 480 rec.x2 = rec.x1 + font->width; 481 drm_panic_blit(sb, &rec, src, font_pitch, color); 482 rec.x1 += font->width; 483 } 484 } 485 } 486 487 static void draw_panic_static_user(struct drm_scanout_buffer *sb) 488 { 489 size_t msg_lines = ARRAY_SIZE(panic_msg); 490 size_t logo_ascii_lines = ARRAY_SIZE(logo_ascii); 491 u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format); 492 u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format); 493 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL); 494 struct drm_rect r_screen, r_logo, r_msg; 495 unsigned int logo_width, logo_height; 496 497 if (!font) 498 return; 499 500 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 501 502 if (logo_mono) { 503 logo_width = logo_mono->width; 504 logo_height = logo_mono->height; 505 } else { 506 logo_width = get_max_line_len(logo_ascii, logo_ascii_lines) * font->width; 507 logo_height = logo_ascii_lines * font->height; 508 } 509 510 r_logo = DRM_RECT_INIT(0, 0, logo_width, logo_height); 511 r_msg = DRM_RECT_INIT(0, 0, 512 min(get_max_line_len(panic_msg, msg_lines) * font->width, sb->width), 513 min(msg_lines * font->height, sb->height)); 514 515 /* Center the panic message */ 516 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2); 517 518 /* Fill with the background color, and draw text on top */ 519 drm_panic_fill(sb, &r_screen, bg_color); 520 521 if ((r_msg.x1 >= logo_width || r_msg.y1 >= logo_height) && 522 logo_width <= sb->width && logo_height <= sb->height) { 523 if (logo_mono) 524 drm_panic_blit(sb, &r_logo, logo_mono->data, DIV_ROUND_UP(logo_width, 8), 525 fg_color); 526 else 527 draw_txt_rectangle(sb, font, logo_ascii, logo_ascii_lines, false, &r_logo, 528 fg_color); 529 } 530 draw_txt_rectangle(sb, font, panic_msg, msg_lines, true, &r_msg, fg_color); 531 } 532 533 /* 534 * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width. 535 * Return the y-offset of the next line. 536 */ 537 static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_desc *font, 538 struct drm_panic_line *line, int yoffset, u32 fg_color) 539 { 540 int chars_per_row = sb->width / font->width; 541 struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, sb->height); 542 struct drm_panic_line line_wrap; 543 544 if (line->len > chars_per_row) { 545 line_wrap.len = line->len % chars_per_row; 546 line_wrap.txt = line->txt + line->len - line_wrap.len; 547 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color); 548 r_txt.y1 -= font->height; 549 if (r_txt.y1 < 0) 550 return r_txt.y1; 551 while (line_wrap.txt > line->txt) { 552 line_wrap.txt -= chars_per_row; 553 line_wrap.len = chars_per_row; 554 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color); 555 r_txt.y1 -= font->height; 556 if (r_txt.y1 < 0) 557 return r_txt.y1; 558 } 559 } else { 560 draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color); 561 r_txt.y1 -= font->height; 562 } 563 return r_txt.y1; 564 } 565 566 /* 567 * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom, 568 * and going up until reaching the top of the screen. 569 */ 570 static void draw_panic_static_kmsg(struct drm_scanout_buffer *sb) 571 { 572 u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format); 573 u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format); 574 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL); 575 struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 576 struct kmsg_dump_iter iter; 577 char kmsg_buf[512]; 578 size_t kmsg_len; 579 struct drm_panic_line line; 580 int yoffset; 581 582 if (!font) 583 return; 584 585 yoffset = sb->height - font->height - (sb->height % font->height) / 2; 586 587 /* Fill with the background color, and draw text on top */ 588 drm_panic_fill(sb, &r_screen, bg_color); 589 590 kmsg_dump_rewind(&iter); 591 while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) { 592 char *start; 593 char *end; 594 595 /* ignore terminating NUL and newline */ 596 start = kmsg_buf + kmsg_len - 2; 597 end = kmsg_buf + kmsg_len - 1; 598 while (start > kmsg_buf && yoffset >= 0) { 599 while (start > kmsg_buf && *start != '\n') 600 start--; 601 /* don't count the newline character */ 602 line.txt = start + (start == kmsg_buf ? 0 : 1); 603 line.len = end - line.txt; 604 605 yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color); 606 end = start; 607 start--; 608 } 609 } 610 } 611 612 /* 613 * drm_panic_is_format_supported() 614 * @format: a fourcc color code 615 * Returns: true if supported, false otherwise. 616 * 617 * Check if drm_panic will be able to use this color format. 618 */ 619 static bool drm_panic_is_format_supported(const struct drm_format_info *format) 620 { 621 if (format->num_planes != 1) 622 return false; 623 return convert_from_xrgb8888(0xffffff, format->format) != 0; 624 } 625 626 static void draw_panic_dispatch(struct drm_scanout_buffer *sb) 627 { 628 if (!strcmp(drm_panic_screen, "kmsg")) { 629 draw_panic_static_kmsg(sb); 630 } else { 631 draw_panic_static_user(sb); 632 } 633 } 634 635 static void draw_panic_plane(struct drm_plane *plane) 636 { 637 struct drm_scanout_buffer sb = { }; 638 int ret; 639 unsigned long flags; 640 641 if (!drm_panic_trylock(plane->dev, flags)) 642 return; 643 644 ret = plane->helper_private->get_scanout_buffer(plane, &sb); 645 646 if (!ret && drm_panic_is_format_supported(sb.format)) { 647 draw_panic_dispatch(&sb); 648 if (plane->helper_private->panic_flush) 649 plane->helper_private->panic_flush(plane); 650 } 651 drm_panic_unlock(plane->dev, flags); 652 } 653 654 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd) 655 { 656 return container_of(kd, struct drm_plane, kmsg_panic); 657 } 658 659 static void drm_panic(struct kmsg_dumper *dumper, enum kmsg_dump_reason reason) 660 { 661 struct drm_plane *plane = to_drm_plane(dumper); 662 663 if (reason == KMSG_DUMP_PANIC) 664 draw_panic_plane(plane); 665 } 666 667 668 /* 669 * DEBUG FS, This is currently unsafe. 670 * Create one file per plane, so it's possible to debug one plane at a time. 671 * TODO: It would be better to emulate an NMI context. 672 */ 673 #ifdef CONFIG_DRM_PANIC_DEBUG 674 #include <linux/debugfs.h> 675 676 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf, 677 size_t count, loff_t *ppos) 678 { 679 bool run; 680 681 if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) { 682 struct drm_plane *plane = file->private_data; 683 684 draw_panic_plane(plane); 685 } 686 return count; 687 } 688 689 static const struct file_operations dbg_drm_panic_ops = { 690 .owner = THIS_MODULE, 691 .write = debugfs_trigger_write, 692 .open = simple_open, 693 }; 694 695 static void debugfs_register_plane(struct drm_plane *plane, int index) 696 { 697 char fname[32]; 698 699 snprintf(fname, 32, "drm_panic_plane_%d", index); 700 debugfs_create_file(fname, 0200, plane->dev->debugfs_root, 701 plane, &dbg_drm_panic_ops); 702 } 703 #else 704 static void debugfs_register_plane(struct drm_plane *plane, int index) {} 705 #endif /* CONFIG_DRM_PANIC_DEBUG */ 706 707 /** 708 * drm_panic_register() - Initialize DRM panic for a device 709 * @dev: the drm device on which the panic screen will be displayed. 710 */ 711 void drm_panic_register(struct drm_device *dev) 712 { 713 struct drm_plane *plane; 714 int registered_plane = 0; 715 716 if (!dev->mode_config.num_total_plane) 717 return; 718 719 drm_for_each_plane(plane, dev) { 720 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer) 721 continue; 722 plane->kmsg_panic.dump = drm_panic; 723 plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC; 724 if (kmsg_dump_register(&plane->kmsg_panic)) 725 drm_warn(dev, "Failed to register panic handler\n"); 726 else { 727 debugfs_register_plane(plane, registered_plane); 728 registered_plane++; 729 } 730 } 731 if (registered_plane) 732 drm_info(dev, "Registered %d planes with drm panic\n", registered_plane); 733 } 734 EXPORT_SYMBOL(drm_panic_register); 735 736 /** 737 * drm_panic_unregister() 738 * @dev: the drm device previously registered. 739 */ 740 void drm_panic_unregister(struct drm_device *dev) 741 { 742 struct drm_plane *plane; 743 744 if (!dev->mode_config.num_total_plane) 745 return; 746 747 drm_for_each_plane(plane, dev) { 748 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer) 749 continue; 750 kmsg_dump_unregister(&plane->kmsg_panic); 751 } 752 } 753 EXPORT_SYMBOL(drm_panic_unregister); 754