xref: /linux/drivers/gpu/drm/drm_panic.c (revision 4b570ac2eb54f66ff64f2864be6303b8d67cc7f9)
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 			     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 draw_panic_static_user(struct drm_scanout_buffer *sb)
500 {
501 	u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format);
502 	u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format);
503 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
504 	struct drm_rect r_screen, r_logo, r_msg;
505 	unsigned int logo_width, logo_height;
506 	unsigned int msg_width, msg_height;
507 
508 	if (!font)
509 		return;
510 
511 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
512 
513 	if (logo_mono) {
514 		logo_width = logo_mono->width;
515 		logo_height = logo_mono->height;
516 	} else {
517 		logo_width = get_max_line_len(logo_ascii, PANIC_LOGO_LINES) * font->width;
518 		logo_height = PANIC_LOGO_LINES * font->height;
519 	}
520 	r_logo = DRM_RECT_INIT(0, 0, logo_width, logo_height);
521 
522 	msg_width = min(get_max_line_len(panic_msg, PANIC_MSG_LINES) * font->width, sb->width);
523 	msg_height = min(PANIC_MSG_LINES * font->height, sb->height);
524 	r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
525 
526 	/* Center the panic message */
527 	drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2);
528 
529 	/* Fill with the background color, and draw text on top */
530 	drm_panic_fill(sb, &r_screen, bg_color);
531 
532 	if (!drm_rect_overlap(&r_logo, &r_msg)) {
533 		if (logo_mono)
534 			drm_panic_blit(sb, &r_logo, logo_mono->data, DIV_ROUND_UP(logo_width, 8),
535 				       fg_color);
536 		else
537 			draw_txt_rectangle(sb, font, logo_ascii, PANIC_LOGO_LINES, false, &r_logo,
538 					   fg_color);
539 	}
540 	draw_txt_rectangle(sb, font, panic_msg, PANIC_MSG_LINES, true, &r_msg, fg_color);
541 }
542 
543 /*
544  * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width.
545  * Return the y-offset of the next line.
546  */
547 static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_desc *font,
548 			       struct drm_panic_line *line, int yoffset, u32 fg_color)
549 {
550 	int chars_per_row = sb->width / font->width;
551 	struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, sb->height);
552 	struct drm_panic_line line_wrap;
553 
554 	if (line->len > chars_per_row) {
555 		line_wrap.len = line->len % chars_per_row;
556 		line_wrap.txt = line->txt + line->len - line_wrap.len;
557 		draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
558 		r_txt.y1 -= font->height;
559 		if (r_txt.y1 < 0)
560 			return r_txt.y1;
561 		while (line_wrap.txt > line->txt) {
562 			line_wrap.txt -= chars_per_row;
563 			line_wrap.len = chars_per_row;
564 			draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
565 			r_txt.y1 -= font->height;
566 			if (r_txt.y1 < 0)
567 				return r_txt.y1;
568 		}
569 	} else {
570 		draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color);
571 		r_txt.y1 -= font->height;
572 	}
573 	return r_txt.y1;
574 }
575 
576 /*
577  * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom,
578  * and going up until reaching the top of the screen.
579  */
580 static void draw_panic_static_kmsg(struct drm_scanout_buffer *sb)
581 {
582 	u32 fg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR, sb->format->format);
583 	u32 bg_color = convert_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR, sb->format->format);
584 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
585 	struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
586 	struct kmsg_dump_iter iter;
587 	char kmsg_buf[512];
588 	size_t kmsg_len;
589 	struct drm_panic_line line;
590 	int yoffset;
591 
592 	if (!font)
593 		return;
594 
595 	yoffset = sb->height - font->height - (sb->height % font->height) / 2;
596 
597 	/* Fill with the background color, and draw text on top */
598 	drm_panic_fill(sb, &r_screen, bg_color);
599 
600 	kmsg_dump_rewind(&iter);
601 	while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) {
602 		char *start;
603 		char *end;
604 
605 		/* ignore terminating NUL and newline */
606 		start = kmsg_buf + kmsg_len - 2;
607 		end = kmsg_buf + kmsg_len - 1;
608 		while (start > kmsg_buf && yoffset >= 0) {
609 			while (start > kmsg_buf && *start != '\n')
610 				start--;
611 			/* don't count the newline character */
612 			line.txt = start + (start == kmsg_buf ? 0 : 1);
613 			line.len = end - line.txt;
614 
615 			yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color);
616 			end = start;
617 			start--;
618 		}
619 	}
620 }
621 
622 /*
623  * drm_panic_is_format_supported()
624  * @format: a fourcc color code
625  * Returns: true if supported, false otherwise.
626  *
627  * Check if drm_panic will be able to use this color format.
628  */
629 static bool drm_panic_is_format_supported(const struct drm_format_info *format)
630 {
631 	if (format->num_planes != 1)
632 		return false;
633 	return convert_from_xrgb8888(0xffffff, format->format) != 0;
634 }
635 
636 static void draw_panic_dispatch(struct drm_scanout_buffer *sb)
637 {
638 	if (!strcmp(drm_panic_screen, "kmsg")) {
639 		draw_panic_static_kmsg(sb);
640 	} else {
641 		draw_panic_static_user(sb);
642 	}
643 }
644 
645 static void drm_panic_set_description(const char *description)
646 {
647 	u32 len;
648 
649 	if (description) {
650 		struct drm_panic_line *desc_line = &panic_msg[PANIC_MSG_LINES - 1];
651 
652 		desc_line->txt = description;
653 		len = strlen(description);
654 		/* ignore the last newline character */
655 		if (len && description[len - 1] == '\n')
656 			len -= 1;
657 		desc_line->len = len;
658 	}
659 }
660 
661 static void drm_panic_clear_description(void)
662 {
663 	struct drm_panic_line *desc_line = &panic_msg[PANIC_MSG_LINES - 1];
664 
665 	desc_line->len = 0;
666 	desc_line->txt = NULL;
667 }
668 
669 static void draw_panic_plane(struct drm_plane *plane, const char *description)
670 {
671 	struct drm_scanout_buffer sb = { };
672 	int ret;
673 	unsigned long flags;
674 
675 	if (!drm_panic_trylock(plane->dev, flags))
676 		return;
677 
678 	drm_panic_set_description(description);
679 
680 	ret = plane->helper_private->get_scanout_buffer(plane, &sb);
681 
682 	if (!ret && drm_panic_is_format_supported(sb.format)) {
683 		draw_panic_dispatch(&sb);
684 		if (plane->helper_private->panic_flush)
685 			plane->helper_private->panic_flush(plane);
686 	}
687 	drm_panic_clear_description();
688 	drm_panic_unlock(plane->dev, flags);
689 }
690 
691 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd)
692 {
693 	return container_of(kd, struct drm_plane, kmsg_panic);
694 }
695 
696 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail)
697 {
698 	struct drm_plane *plane = to_drm_plane(dumper);
699 
700 	if (detail->reason == KMSG_DUMP_PANIC)
701 		draw_panic_plane(plane, detail->description);
702 }
703 
704 
705 /*
706  * DEBUG FS, This is currently unsafe.
707  * Create one file per plane, so it's possible to debug one plane at a time.
708  * TODO: It would be better to emulate an NMI context.
709  */
710 #ifdef CONFIG_DRM_PANIC_DEBUG
711 #include <linux/debugfs.h>
712 
713 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf,
714 				     size_t count, loff_t *ppos)
715 {
716 	bool run;
717 
718 	if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) {
719 		struct drm_plane *plane = file->private_data;
720 
721 		draw_panic_plane(plane, "Test from debugfs");
722 	}
723 	return count;
724 }
725 
726 static const struct file_operations dbg_drm_panic_ops = {
727 	.owner = THIS_MODULE,
728 	.write = debugfs_trigger_write,
729 	.open = simple_open,
730 };
731 
732 static void debugfs_register_plane(struct drm_plane *plane, int index)
733 {
734 	char fname[32];
735 
736 	snprintf(fname, 32, "drm_panic_plane_%d", index);
737 	debugfs_create_file(fname, 0200, plane->dev->debugfs_root,
738 			    plane, &dbg_drm_panic_ops);
739 }
740 #else
741 static void debugfs_register_plane(struct drm_plane *plane, int index) {}
742 #endif /* CONFIG_DRM_PANIC_DEBUG */
743 
744 /**
745  * drm_panic_is_enabled
746  * @dev: the drm device that may supports drm_panic
747  *
748  * returns true if the drm device supports drm_panic
749  */
750 bool drm_panic_is_enabled(struct drm_device *dev)
751 {
752 	struct drm_plane *plane;
753 
754 	if (!dev->mode_config.num_total_plane)
755 		return false;
756 
757 	drm_for_each_plane(plane, dev)
758 		if (plane->helper_private && plane->helper_private->get_scanout_buffer)
759 			return true;
760 	return false;
761 }
762 EXPORT_SYMBOL(drm_panic_is_enabled);
763 
764 /**
765  * drm_panic_register() - Initialize DRM panic for a device
766  * @dev: the drm device on which the panic screen will be displayed.
767  */
768 void drm_panic_register(struct drm_device *dev)
769 {
770 	struct drm_plane *plane;
771 	int registered_plane = 0;
772 
773 	if (!dev->mode_config.num_total_plane)
774 		return;
775 
776 	drm_for_each_plane(plane, dev) {
777 		if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
778 			continue;
779 		plane->kmsg_panic.dump = drm_panic;
780 		plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC;
781 		if (kmsg_dump_register(&plane->kmsg_panic))
782 			drm_warn(dev, "Failed to register panic handler\n");
783 		else {
784 			debugfs_register_plane(plane, registered_plane);
785 			registered_plane++;
786 		}
787 	}
788 	if (registered_plane)
789 		drm_info(dev, "Registered %d planes with drm panic\n", registered_plane);
790 }
791 
792 /**
793  * drm_panic_unregister()
794  * @dev: the drm device previously registered.
795  */
796 void drm_panic_unregister(struct drm_device *dev)
797 {
798 	struct drm_plane *plane;
799 
800 	if (!dev->mode_config.num_total_plane)
801 		return;
802 
803 	drm_for_each_plane(plane, dev) {
804 		if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
805 			continue;
806 		kmsg_dump_unregister(&plane->kmsg_panic);
807 	}
808 }
809