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 static const size_t 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 static const size_t logo_ascii_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 unsigned int scale, 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 / scale, y / scale)) 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 unsigned int scale, 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 / scale, y / scale)) { 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 unsigned int scale, 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 / scale, y / scale)) 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, unsigned int scale, 306 u32 fg_color) 307 { 308 unsigned int y, x; 309 310 for (y = 0; y < drm_rect_height(clip); y++) 311 for (x = 0; x < drm_rect_width(clip); x++) 312 if (drm_panic_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) 313 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, fg_color); 314 } 315 316 /* 317 * drm_panic_blit - convert a monochrome image to a linear framebuffer 318 * @sb: destination scanout buffer 319 * @clip: destination rectangle 320 * @sbuf8: source buffer, in monochrome format, 8 pixels per byte. 321 * @spitch: source pitch in bytes 322 * @scale: integer scale, source buffer is scale time smaller than destination 323 * rectangle 324 * @fg_color: foreground color, in destination format 325 * 326 * This can be used to draw a font character, which is a monochrome image, to a 327 * framebuffer in other supported format. 328 */ 329 static void drm_panic_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip, 330 const u8 *sbuf8, unsigned int spitch, 331 unsigned int scale, u32 fg_color) 332 333 { 334 struct iosys_map map; 335 336 if (sb->set_pixel) 337 return drm_panic_blit_pixel(sb, clip, sbuf8, spitch, scale, fg_color); 338 339 map = sb->map[0]; 340 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]); 341 342 switch (sb->format->cpp[0]) { 343 case 2: 344 drm_panic_blit16(&map, sb->pitch[0], sbuf8, spitch, 345 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 346 break; 347 case 3: 348 drm_panic_blit24(&map, sb->pitch[0], sbuf8, spitch, 349 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 350 break; 351 case 4: 352 drm_panic_blit32(&map, sb->pitch[0], sbuf8, spitch, 353 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color); 354 break; 355 default: 356 WARN_ONCE(1, "Can't blit with pixel width %d\n", sb->format->cpp[0]); 357 } 358 } 359 360 static void drm_panic_fill16(struct iosys_map *dmap, unsigned int dpitch, 361 unsigned int height, unsigned int width, 362 u16 color) 363 { 364 unsigned int y, x; 365 366 for (y = 0; y < height; y++) 367 for (x = 0; x < width; x++) 368 iosys_map_wr(dmap, y * dpitch + x * sizeof(u16), u16, color); 369 } 370 371 static void drm_panic_fill24(struct iosys_map *dmap, unsigned int dpitch, 372 unsigned int height, unsigned int width, 373 u32 color) 374 { 375 unsigned int y, x; 376 377 for (y = 0; y < height; y++) { 378 for (x = 0; x < width; x++) { 379 unsigned int off = y * dpitch + x * 3; 380 381 /* write blue-green-red to output in little endianness */ 382 iosys_map_wr(dmap, off, u8, (color & 0x000000FF) >> 0); 383 iosys_map_wr(dmap, off + 1, u8, (color & 0x0000FF00) >> 8); 384 iosys_map_wr(dmap, off + 2, u8, (color & 0x00FF0000) >> 16); 385 } 386 } 387 } 388 389 static void drm_panic_fill32(struct iosys_map *dmap, unsigned int dpitch, 390 unsigned int height, unsigned int width, 391 u32 color) 392 { 393 unsigned int y, x; 394 395 for (y = 0; y < height; y++) 396 for (x = 0; x < width; x++) 397 iosys_map_wr(dmap, y * dpitch + x * sizeof(u32), u32, color); 398 } 399 400 static void drm_panic_fill_pixel(struct drm_scanout_buffer *sb, 401 struct drm_rect *clip, 402 u32 color) 403 { 404 unsigned int y, x; 405 406 for (y = 0; y < drm_rect_height(clip); y++) 407 for (x = 0; x < drm_rect_width(clip); x++) 408 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, color); 409 } 410 411 /* 412 * drm_panic_fill - Fill a rectangle with a color 413 * @sb: destination scanout buffer 414 * @clip: destination rectangle 415 * @color: foreground color, in destination format 416 * 417 * Fill a rectangle with a color, in a linear framebuffer. 418 */ 419 static void drm_panic_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip, 420 u32 color) 421 { 422 struct iosys_map map; 423 424 if (sb->set_pixel) 425 return drm_panic_fill_pixel(sb, clip, color); 426 427 map = sb->map[0]; 428 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]); 429 430 switch (sb->format->cpp[0]) { 431 case 2: 432 drm_panic_fill16(&map, sb->pitch[0], drm_rect_height(clip), 433 drm_rect_width(clip), color); 434 break; 435 case 3: 436 drm_panic_fill24(&map, sb->pitch[0], drm_rect_height(clip), 437 drm_rect_width(clip), color); 438 break; 439 case 4: 440 drm_panic_fill32(&map, sb->pitch[0], drm_rect_height(clip), 441 drm_rect_width(clip), color); 442 break; 443 default: 444 WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]); 445 } 446 } 447 448 static const u8 *get_char_bitmap(const struct font_desc *font, char c, size_t font_pitch) 449 { 450 return font->data + (c * font->height) * font_pitch; 451 } 452 453 static unsigned int get_max_line_len(const struct drm_panic_line *lines, int len) 454 { 455 int i; 456 unsigned int max = 0; 457 458 for (i = 0; i < len; i++) 459 max = max(lines[i].len, max); 460 return max; 461 } 462 463 /* 464 * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle 465 */ 466 static void draw_txt_rectangle(struct drm_scanout_buffer *sb, 467 const struct font_desc *font, 468 const struct drm_panic_line *msg, 469 unsigned int msg_lines, 470 bool centered, 471 struct drm_rect *clip, 472 u32 color) 473 { 474 int i, j; 475 const u8 *src; 476 size_t font_pitch = DIV_ROUND_UP(font->width, 8); 477 struct drm_rect rec; 478 479 msg_lines = min(msg_lines, drm_rect_height(clip) / font->height); 480 for (i = 0; i < msg_lines; i++) { 481 size_t line_len = min(msg[i].len, drm_rect_width(clip) / font->width); 482 483 rec.y1 = clip->y1 + i * font->height; 484 rec.y2 = rec.y1 + font->height; 485 rec.x1 = clip->x1; 486 487 if (centered) 488 rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2; 489 490 for (j = 0; j < line_len; j++) { 491 src = get_char_bitmap(font, msg[i].txt[j], font_pitch); 492 rec.x2 = rec.x1 + font->width; 493 drm_panic_blit(sb, &rec, src, font_pitch, 1, color); 494 rec.x1 += font->width; 495 } 496 } 497 } 498 499 static void drm_panic_logo_rect(struct drm_rect *rect, const struct font_desc *font) 500 { 501 if (logo_mono) { 502 drm_rect_init(rect, 0, 0, logo_mono->width, logo_mono->height); 503 } else { 504 int logo_width = get_max_line_len(logo_ascii, logo_ascii_lines) * font->width; 505 506 drm_rect_init(rect, 0, 0, logo_width, logo_ascii_lines * font->height); 507 } 508 } 509 510 static void drm_panic_logo_draw(struct drm_scanout_buffer *sb, struct drm_rect *rect, 511 const struct font_desc *font, u32 fg_color) 512 { 513 if (logo_mono) 514 drm_panic_blit(sb, rect, logo_mono->data, 515 DIV_ROUND_UP(drm_rect_width(rect), 8), 1, fg_color); 516 else 517 draw_txt_rectangle(sb, font, logo_ascii, logo_ascii_lines, false, rect, 518 fg_color); 519 } 520 521 static void draw_panic_static_user(struct drm_scanout_buffer *sb) 522 { 523 u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format); 524 u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format); 525 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL); 526 struct drm_rect r_screen, r_logo, r_msg; 527 unsigned int msg_width, msg_height; 528 529 if (!font) 530 return; 531 532 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 533 drm_panic_logo_rect(&r_logo, font); 534 535 msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width); 536 msg_height = min(panic_msg_lines * font->height, sb->height); 537 r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height); 538 539 /* Center the panic message */ 540 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2); 541 542 /* Fill with the background color, and draw text on top */ 543 drm_panic_fill(sb, &r_screen, bg_color); 544 545 if (!drm_rect_overlap(&r_logo, &r_msg)) 546 drm_panic_logo_draw(sb, &r_logo, font, fg_color); 547 548 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color); 549 } 550 551 /* 552 * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width. 553 * Return the y-offset of the next line. 554 */ 555 static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_desc *font, 556 struct drm_panic_line *line, int yoffset, u32 fg_color) 557 { 558 int chars_per_row = sb->width / font->width; 559 struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, sb->height); 560 struct drm_panic_line line_wrap; 561 562 if (line->len > chars_per_row) { 563 line_wrap.len = line->len % chars_per_row; 564 line_wrap.txt = line->txt + line->len - line_wrap.len; 565 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color); 566 r_txt.y1 -= font->height; 567 if (r_txt.y1 < 0) 568 return r_txt.y1; 569 while (line_wrap.txt > line->txt) { 570 line_wrap.txt -= chars_per_row; 571 line_wrap.len = chars_per_row; 572 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color); 573 r_txt.y1 -= font->height; 574 if (r_txt.y1 < 0) 575 return r_txt.y1; 576 } 577 } else { 578 draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color); 579 r_txt.y1 -= font->height; 580 } 581 return r_txt.y1; 582 } 583 584 /* 585 * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom, 586 * and going up until reaching the top of the screen. 587 */ 588 static void draw_panic_static_kmsg(struct drm_scanout_buffer *sb) 589 { 590 u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format); 591 u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format); 592 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL); 593 struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height); 594 struct kmsg_dump_iter iter; 595 char kmsg_buf[512]; 596 size_t kmsg_len; 597 struct drm_panic_line line; 598 int yoffset; 599 600 if (!font) 601 return; 602 603 yoffset = sb->height - font->height - (sb->height % font->height) / 2; 604 605 /* Fill with the background color, and draw text on top */ 606 drm_panic_fill(sb, &r_screen, bg_color); 607 608 kmsg_dump_rewind(&iter); 609 while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) { 610 char *start; 611 char *end; 612 613 /* ignore terminating NUL and newline */ 614 start = kmsg_buf + kmsg_len - 2; 615 end = kmsg_buf + kmsg_len - 1; 616 while (start > kmsg_buf && yoffset >= 0) { 617 while (start > kmsg_buf && *start != '\n') 618 start--; 619 /* don't count the newline character */ 620 line.txt = start + (start == kmsg_buf ? 0 : 1); 621 line.len = end - line.txt; 622 623 yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color); 624 end = start; 625 start--; 626 } 627 } 628 } 629 630 /* 631 * drm_panic_is_format_supported() 632 * @format: a fourcc color code 633 * Returns: true if supported, false otherwise. 634 * 635 * Check if drm_panic will be able to use this color format. 636 */ 637 static bool drm_panic_is_format_supported(const struct drm_format_info *format) 638 { 639 if (format->num_planes != 1) 640 return false; 641 return convert_from_xrgb8888(0xffffff, format->format) != 0; 642 } 643 644 static void draw_panic_dispatch(struct drm_scanout_buffer *sb) 645 { 646 if (!strcmp(drm_panic_screen, "kmsg")) { 647 draw_panic_static_kmsg(sb); 648 } else { 649 draw_panic_static_user(sb); 650 } 651 } 652 653 static void drm_panic_set_description(const char *description) 654 { 655 u32 len; 656 657 if (description) { 658 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1]; 659 660 desc_line->txt = description; 661 len = strlen(description); 662 /* ignore the last newline character */ 663 if (len && description[len - 1] == '\n') 664 len -= 1; 665 desc_line->len = len; 666 } 667 } 668 669 static void drm_panic_clear_description(void) 670 { 671 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1]; 672 673 desc_line->len = 0; 674 desc_line->txt = NULL; 675 } 676 677 static void draw_panic_plane(struct drm_plane *plane, const char *description) 678 { 679 struct drm_scanout_buffer sb = { }; 680 int ret; 681 unsigned long flags; 682 683 if (!drm_panic_trylock(plane->dev, flags)) 684 return; 685 686 drm_panic_set_description(description); 687 688 ret = plane->helper_private->get_scanout_buffer(plane, &sb); 689 690 if (!ret && drm_panic_is_format_supported(sb.format)) { 691 draw_panic_dispatch(&sb); 692 if (plane->helper_private->panic_flush) 693 plane->helper_private->panic_flush(plane); 694 } 695 drm_panic_clear_description(); 696 drm_panic_unlock(plane->dev, flags); 697 } 698 699 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd) 700 { 701 return container_of(kd, struct drm_plane, kmsg_panic); 702 } 703 704 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail) 705 { 706 struct drm_plane *plane = to_drm_plane(dumper); 707 708 if (detail->reason == KMSG_DUMP_PANIC) 709 draw_panic_plane(plane, detail->description); 710 } 711 712 713 /* 714 * DEBUG FS, This is currently unsafe. 715 * Create one file per plane, so it's possible to debug one plane at a time. 716 * TODO: It would be better to emulate an NMI context. 717 */ 718 #ifdef CONFIG_DRM_PANIC_DEBUG 719 #include <linux/debugfs.h> 720 721 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf, 722 size_t count, loff_t *ppos) 723 { 724 bool run; 725 726 if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) { 727 struct drm_plane *plane = file->private_data; 728 729 draw_panic_plane(plane, "Test from debugfs"); 730 } 731 return count; 732 } 733 734 static const struct file_operations dbg_drm_panic_ops = { 735 .owner = THIS_MODULE, 736 .write = debugfs_trigger_write, 737 .open = simple_open, 738 }; 739 740 static void debugfs_register_plane(struct drm_plane *plane, int index) 741 { 742 char fname[32]; 743 744 snprintf(fname, 32, "drm_panic_plane_%d", index); 745 debugfs_create_file(fname, 0200, plane->dev->debugfs_root, 746 plane, &dbg_drm_panic_ops); 747 } 748 #else 749 static void debugfs_register_plane(struct drm_plane *plane, int index) {} 750 #endif /* CONFIG_DRM_PANIC_DEBUG */ 751 752 /** 753 * drm_panic_is_enabled 754 * @dev: the drm device that may supports drm_panic 755 * 756 * returns true if the drm device supports drm_panic 757 */ 758 bool drm_panic_is_enabled(struct drm_device *dev) 759 { 760 struct drm_plane *plane; 761 762 if (!dev->mode_config.num_total_plane) 763 return false; 764 765 drm_for_each_plane(plane, dev) 766 if (plane->helper_private && plane->helper_private->get_scanout_buffer) 767 return true; 768 return false; 769 } 770 EXPORT_SYMBOL(drm_panic_is_enabled); 771 772 /** 773 * drm_panic_register() - Initialize DRM panic for a device 774 * @dev: the drm device on which the panic screen will be displayed. 775 */ 776 void drm_panic_register(struct drm_device *dev) 777 { 778 struct drm_plane *plane; 779 int registered_plane = 0; 780 781 if (!dev->mode_config.num_total_plane) 782 return; 783 784 drm_for_each_plane(plane, dev) { 785 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer) 786 continue; 787 plane->kmsg_panic.dump = drm_panic; 788 plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC; 789 if (kmsg_dump_register(&plane->kmsg_panic)) 790 drm_warn(dev, "Failed to register panic handler\n"); 791 else { 792 debugfs_register_plane(plane, registered_plane); 793 registered_plane++; 794 } 795 } 796 if (registered_plane) 797 drm_info(dev, "Registered %d planes with drm panic\n", registered_plane); 798 } 799 800 /** 801 * drm_panic_unregister() 802 * @dev: the drm device previously registered. 803 */ 804 void drm_panic_unregister(struct drm_device *dev) 805 { 806 struct drm_plane *plane; 807 808 if (!dev->mode_config.num_total_plane) 809 return; 810 811 drm_for_each_plane(plane, dev) { 812 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer) 813 continue; 814 kmsg_dump_unregister(&plane->kmsg_panic); 815 } 816 } 817