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