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