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 #include <linux/utsname.h>
22 #include <linux/zlib.h>
23 
24 #include <drm/drm_drv.h>
25 #include <drm/drm_fourcc.h>
26 #include <drm/drm_framebuffer.h>
27 #include <drm/drm_modeset_helper_vtables.h>
28 #include <drm/drm_panic.h>
29 #include <drm/drm_plane.h>
30 #include <drm/drm_print.h>
31 #include <drm/drm_rect.h>
32 
33 #include "drm_crtc_internal.h"
34 #include "drm_draw_internal.h"
35 
36 MODULE_AUTHOR("Jocelyn Falempe");
37 MODULE_DESCRIPTION("DRM panic handler");
38 MODULE_LICENSE("GPL");
39 
40 static char drm_panic_screen[16] = CONFIG_DRM_PANIC_SCREEN;
41 module_param_string(panic_screen, drm_panic_screen, sizeof(drm_panic_screen), 0644);
42 MODULE_PARM_DESC(panic_screen,
43 		 "Choose what will be displayed by drm_panic, 'user' or 'kmsg' [default="
44 		 CONFIG_DRM_PANIC_SCREEN "]");
45 
46 /**
47  * DOC: overview
48  *
49  * To enable DRM panic for a driver, the primary plane must implement a
50  * &drm_plane_helper_funcs.get_scanout_buffer helper function. It is then
51  * automatically registered to the drm panic handler.
52  * When a panic occurs, the &drm_plane_helper_funcs.get_scanout_buffer will be
53  * called, and the driver can provide a framebuffer so the panic handler can
54  * draw the panic screen on it. Currently only linear buffer and a few color
55  * formats are supported.
56  * Optionally the driver can also provide a &drm_plane_helper_funcs.panic_flush
57  * callback, that will be called after that, to send additional commands to the
58  * hardware to make the scanout buffer visible.
59  */
60 
61 /*
62  * This module displays a user friendly message on screen when a kernel panic
63  * occurs. This is conflicting with fbcon, so you can only enable it when fbcon
64  * is disabled.
65  * It's intended for end-user, so have minimal technical/debug information.
66  *
67  * Implementation details:
68  *
69  * It is a panic handler, so it can't take lock, allocate memory, run tasks/irq,
70  * or attempt to sleep. It's a best effort, and it may not be able to display
71  * the message in all situations (like if the panic occurs in the middle of a
72  * modesetting).
73  * It will display only one static frame, so performance optimizations are low
74  * priority as the machine is already in an unusable state.
75  */
76 
77 struct drm_panic_line {
78 	u32 len;
79 	const char *txt;
80 };
81 
82 #define PANIC_LINE(s) {.len = sizeof(s) - 1, .txt = s}
83 
84 static struct drm_panic_line panic_msg[] = {
85 	PANIC_LINE("KERNEL PANIC!"),
86 	PANIC_LINE(""),
87 	PANIC_LINE("Please reboot your computer."),
88 	PANIC_LINE(""),
89 	PANIC_LINE(""), /* will be replaced by the panic description */
90 };
91 
92 static const size_t panic_msg_lines = ARRAY_SIZE(panic_msg);
93 
94 static const struct drm_panic_line logo_ascii[] = {
95 	PANIC_LINE("     .--.        _"),
96 	PANIC_LINE("    |o_o |      | |"),
97 	PANIC_LINE("    |:_/ |      | |"),
98 	PANIC_LINE("   //   \\ \\     |_|"),
99 	PANIC_LINE("  (|     | )     _"),
100 	PANIC_LINE(" /'\\_   _/`\\    (_)"),
101 	PANIC_LINE(" \\___)=(___/"),
102 };
103 
104 static const size_t logo_ascii_lines = ARRAY_SIZE(logo_ascii);
105 
106 #if defined(CONFIG_LOGO) && !defined(MODULE)
107 static const struct linux_logo *logo_mono;
108 
drm_panic_setup_logo(void)109 static int drm_panic_setup_logo(void)
110 {
111 	const struct linux_logo *logo = fb_find_logo(1);
112 	const unsigned char *logo_data;
113 	struct linux_logo *logo_dup;
114 
115 	if (!logo || logo->type != LINUX_LOGO_MONO)
116 		return 0;
117 
118 	/* The logo is __init, so we must make a copy for later use */
119 	logo_data = kmemdup(logo->data,
120 			    size_mul(DIV_ROUND_UP(logo->width, BITS_PER_BYTE), logo->height),
121 			    GFP_KERNEL);
122 	if (!logo_data)
123 		return -ENOMEM;
124 
125 	logo_dup = kmemdup(logo, sizeof(*logo), GFP_KERNEL);
126 	if (!logo_dup) {
127 		kfree(logo_data);
128 		return -ENOMEM;
129 	}
130 
131 	logo_dup->data = logo_data;
132 	logo_mono = logo_dup;
133 
134 	return 0;
135 }
136 
137 device_initcall(drm_panic_setup_logo);
138 #else
139 #define logo_mono	((const struct linux_logo *)NULL)
140 #endif
141 
142 /*
143  *  Blit & Fill functions
144  */
drm_panic_blit_pixel(struct drm_scanout_buffer * sb,struct drm_rect * clip,const u8 * sbuf8,unsigned int spitch,unsigned int scale,u32 fg_color)145 static void drm_panic_blit_pixel(struct drm_scanout_buffer *sb, struct drm_rect *clip,
146 				 const u8 *sbuf8, unsigned int spitch, unsigned int scale,
147 				 u32 fg_color)
148 {
149 	unsigned int y, x;
150 
151 	for (y = 0; y < drm_rect_height(clip); y++)
152 		for (x = 0; x < drm_rect_width(clip); x++)
153 			if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale))
154 				sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, fg_color);
155 }
156 
157 /*
158  * drm_panic_blit - convert a monochrome image to a linear framebuffer
159  * @sb: destination scanout buffer
160  * @clip: destination rectangle
161  * @sbuf8: source buffer, in monochrome format, 8 pixels per byte.
162  * @spitch: source pitch in bytes
163  * @scale: integer scale, source buffer is scale time smaller than destination
164  *         rectangle
165  * @fg_color: foreground color, in destination format
166  *
167  * This can be used to draw a font character, which is a monochrome image, to a
168  * framebuffer in other supported format.
169  */
drm_panic_blit(struct drm_scanout_buffer * sb,struct drm_rect * clip,const u8 * sbuf8,unsigned int spitch,unsigned int scale,u32 fg_color)170 static void drm_panic_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip,
171 			   const u8 *sbuf8, unsigned int spitch,
172 			   unsigned int scale, u32 fg_color)
173 
174 {
175 	struct iosys_map map;
176 
177 	if (sb->set_pixel)
178 		return drm_panic_blit_pixel(sb, clip, sbuf8, spitch, scale, fg_color);
179 
180 	map = sb->map[0];
181 	iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]);
182 
183 	switch (sb->format->cpp[0]) {
184 	case 2:
185 		drm_draw_blit16(&map, sb->pitch[0], sbuf8, spitch,
186 				drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
187 	break;
188 	case 3:
189 		drm_draw_blit24(&map, sb->pitch[0], sbuf8, spitch,
190 				drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
191 	break;
192 	case 4:
193 		drm_draw_blit32(&map, sb->pitch[0], sbuf8, spitch,
194 				drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
195 	break;
196 	default:
197 		WARN_ONCE(1, "Can't blit with pixel width %d\n", sb->format->cpp[0]);
198 	}
199 }
200 
drm_panic_fill_pixel(struct drm_scanout_buffer * sb,struct drm_rect * clip,u32 color)201 static void drm_panic_fill_pixel(struct drm_scanout_buffer *sb,
202 				 struct drm_rect *clip,
203 				 u32 color)
204 {
205 	unsigned int y, x;
206 
207 	for (y = 0; y < drm_rect_height(clip); y++)
208 		for (x = 0; x < drm_rect_width(clip); x++)
209 			sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, color);
210 }
211 
212 /*
213  * drm_panic_fill - Fill a rectangle with a color
214  * @sb: destination scanout buffer
215  * @clip: destination rectangle
216  * @color: foreground color, in destination format
217  *
218  * Fill a rectangle with a color, in a linear framebuffer.
219  */
drm_panic_fill(struct drm_scanout_buffer * sb,struct drm_rect * clip,u32 color)220 static void drm_panic_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip,
221 			   u32 color)
222 {
223 	struct iosys_map map;
224 
225 	if (sb->set_pixel)
226 		return drm_panic_fill_pixel(sb, clip, color);
227 
228 	map = sb->map[0];
229 	iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]);
230 
231 	switch (sb->format->cpp[0]) {
232 	case 2:
233 		drm_draw_fill16(&map, sb->pitch[0], drm_rect_height(clip),
234 				drm_rect_width(clip), color);
235 	break;
236 	case 3:
237 		drm_draw_fill24(&map, sb->pitch[0], drm_rect_height(clip),
238 				drm_rect_width(clip), color);
239 	break;
240 	case 4:
241 		drm_draw_fill32(&map, sb->pitch[0], drm_rect_height(clip),
242 				drm_rect_width(clip), color);
243 	break;
244 	default:
245 		WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]);
246 	}
247 }
248 
get_max_line_len(const struct drm_panic_line * lines,int len)249 static unsigned int get_max_line_len(const struct drm_panic_line *lines, int len)
250 {
251 	int i;
252 	unsigned int max = 0;
253 
254 	for (i = 0; i < len; i++)
255 		max = max(lines[i].len, max);
256 	return max;
257 }
258 
259 /*
260  * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle
261  */
draw_txt_rectangle(struct drm_scanout_buffer * sb,const struct font_desc * font,const struct drm_panic_line * msg,unsigned int msg_lines,bool centered,struct drm_rect * clip,u32 color)262 static void draw_txt_rectangle(struct drm_scanout_buffer *sb,
263 			       const struct font_desc *font,
264 			       const struct drm_panic_line *msg,
265 			       unsigned int msg_lines,
266 			       bool centered,
267 			       struct drm_rect *clip,
268 			       u32 color)
269 {
270 	int i, j;
271 	const u8 *src;
272 	size_t font_pitch = DIV_ROUND_UP(font->width, 8);
273 	struct drm_rect rec;
274 
275 	msg_lines = min(msg_lines,  drm_rect_height(clip) / font->height);
276 	for (i = 0; i < msg_lines; i++) {
277 		size_t line_len = min(msg[i].len, drm_rect_width(clip) / font->width);
278 
279 		rec.y1 = clip->y1 +  i * font->height;
280 		rec.y2 = rec.y1 + font->height;
281 		rec.x1 = clip->x1;
282 
283 		if (centered)
284 			rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2;
285 
286 		for (j = 0; j < line_len; j++) {
287 			src = drm_draw_get_char_bitmap(font, msg[i].txt[j], font_pitch);
288 			rec.x2 = rec.x1 + font->width;
289 			drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
290 			rec.x1 += font->width;
291 		}
292 	}
293 }
294 
drm_panic_logo_rect(struct drm_rect * rect,const struct font_desc * font)295 static void drm_panic_logo_rect(struct drm_rect *rect, const struct font_desc *font)
296 {
297 	if (logo_mono) {
298 		drm_rect_init(rect, 0, 0, logo_mono->width, logo_mono->height);
299 	} else {
300 		int logo_width = get_max_line_len(logo_ascii, logo_ascii_lines) * font->width;
301 
302 		drm_rect_init(rect, 0, 0, logo_width, logo_ascii_lines * font->height);
303 	}
304 }
305 
drm_panic_logo_draw(struct drm_scanout_buffer * sb,struct drm_rect * rect,const struct font_desc * font,u32 fg_color)306 static void drm_panic_logo_draw(struct drm_scanout_buffer *sb, struct drm_rect *rect,
307 				const struct font_desc *font, u32 fg_color)
308 {
309 	if (logo_mono)
310 		drm_panic_blit(sb, rect, logo_mono->data,
311 			       DIV_ROUND_UP(drm_rect_width(rect), 8), 1, fg_color);
312 	else
313 		draw_txt_rectangle(sb, font, logo_ascii, logo_ascii_lines, false, rect,
314 				   fg_color);
315 }
316 
draw_panic_static_user(struct drm_scanout_buffer * sb)317 static void draw_panic_static_user(struct drm_scanout_buffer *sb)
318 {
319 	u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
320 						    sb->format->format);
321 	u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
322 						    sb->format->format);
323 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
324 	struct drm_rect r_screen, r_logo, r_msg;
325 	unsigned int msg_width, msg_height;
326 
327 	if (!font)
328 		return;
329 
330 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
331 	drm_panic_logo_rect(&r_logo, font);
332 
333 	msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
334 	msg_height = min(panic_msg_lines * font->height, sb->height);
335 	r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
336 
337 	/* Center the panic message */
338 	drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2);
339 
340 	/* Fill with the background color, and draw text on top */
341 	drm_panic_fill(sb, &r_screen, bg_color);
342 
343 	if (!drm_rect_overlap(&r_logo, &r_msg))
344 		drm_panic_logo_draw(sb, &r_logo, font, fg_color);
345 
346 	draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
347 }
348 
349 /*
350  * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width.
351  * Return the y-offset of the next line.
352  */
draw_line_with_wrap(struct drm_scanout_buffer * sb,const struct font_desc * font,struct drm_panic_line * line,int yoffset,u32 fg_color)353 static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_desc *font,
354 			       struct drm_panic_line *line, int yoffset, u32 fg_color)
355 {
356 	int chars_per_row = sb->width / font->width;
357 	struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, sb->height);
358 	struct drm_panic_line line_wrap;
359 
360 	if (line->len > chars_per_row) {
361 		line_wrap.len = line->len % chars_per_row;
362 		line_wrap.txt = line->txt + line->len - line_wrap.len;
363 		draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
364 		r_txt.y1 -= font->height;
365 		if (r_txt.y1 < 0)
366 			return r_txt.y1;
367 		while (line_wrap.txt > line->txt) {
368 			line_wrap.txt -= chars_per_row;
369 			line_wrap.len = chars_per_row;
370 			draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
371 			r_txt.y1 -= font->height;
372 			if (r_txt.y1 < 0)
373 				return r_txt.y1;
374 		}
375 	} else {
376 		draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color);
377 		r_txt.y1 -= font->height;
378 	}
379 	return r_txt.y1;
380 }
381 
382 /*
383  * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom,
384  * and going up until reaching the top of the screen.
385  */
draw_panic_static_kmsg(struct drm_scanout_buffer * sb)386 static void draw_panic_static_kmsg(struct drm_scanout_buffer *sb)
387 {
388 	u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
389 						    sb->format->format);
390 	u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
391 						    sb->format->format);
392 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
393 	struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
394 	struct kmsg_dump_iter iter;
395 	char kmsg_buf[512];
396 	size_t kmsg_len;
397 	struct drm_panic_line line;
398 	int yoffset;
399 
400 	if (!font)
401 		return;
402 
403 	yoffset = sb->height - font->height - (sb->height % font->height) / 2;
404 
405 	/* Fill with the background color, and draw text on top */
406 	drm_panic_fill(sb, &r_screen, bg_color);
407 
408 	kmsg_dump_rewind(&iter);
409 	while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) {
410 		char *start;
411 		char *end;
412 
413 		/* ignore terminating NUL and newline */
414 		start = kmsg_buf + kmsg_len - 2;
415 		end = kmsg_buf + kmsg_len - 1;
416 		while (start > kmsg_buf && yoffset >= 0) {
417 			while (start > kmsg_buf && *start != '\n')
418 				start--;
419 			/* don't count the newline character */
420 			line.txt = start + (start == kmsg_buf ? 0 : 1);
421 			line.len = end - line.txt;
422 
423 			yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color);
424 			end = start;
425 			start--;
426 		}
427 	}
428 }
429 
430 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
431 /*
432  * It is unwise to allocate memory in the panic callback, so the buffers are
433  * pre-allocated. Only 2 buffers and the zlib workspace are needed.
434  * Two buffers are enough, using the following buffer usage:
435  * 1) kmsg messages are dumped in buffer1
436  * 2) kmsg is zlib-compressed into buffer2
437  * 3) compressed kmsg is encoded as QR-code Numeric stream in buffer1
438  * 4) QR-code image is generated in buffer2
439  * The Max QR code size is V40, 177x177, 4071 bytes for image, 2956 bytes for
440  * data segments.
441  *
442  * Typically, ~7500 bytes of kmsg, are compressed into 2800 bytes, which fits in
443  * a V40 QR-code (177x177).
444  *
445  * If CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL is not set, the kmsg data will be put
446  * directly in the QR code.
447  * 1) kmsg messages are dumped in buffer1
448  * 2) kmsg message is encoded as byte stream in buffer2
449  * 3) QR-code image is generated in buffer1
450  */
451 
452 static uint panic_qr_version = CONFIG_DRM_PANIC_SCREEN_QR_VERSION;
453 module_param(panic_qr_version, uint, 0644);
454 MODULE_PARM_DESC(panic_qr_version, "maximum version (size) of the QR code");
455 
456 #define MAX_QR_DATA 2956
457 #define MAX_ZLIB_RATIO 3
458 #define QR_BUFFER1_SIZE (MAX_ZLIB_RATIO * MAX_QR_DATA) /* Must also be > 4071  */
459 #define QR_BUFFER2_SIZE 4096
460 #define QR_MARGIN	4	/* 4 modules of foreground color around the qr code */
461 
462 /* Compression parameters */
463 #define COMPR_LEVEL 6
464 #define WINDOW_BITS 12
465 #define MEM_LEVEL 4
466 
467 static char *qrbuf1;
468 static char *qrbuf2;
469 static struct z_stream_s stream;
470 
drm_panic_qr_init(void)471 static void __init drm_panic_qr_init(void)
472 {
473 	qrbuf1 = kmalloc(QR_BUFFER1_SIZE, GFP_KERNEL);
474 	qrbuf2 = kmalloc(QR_BUFFER2_SIZE, GFP_KERNEL);
475 	stream.workspace = kmalloc(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
476 				   GFP_KERNEL);
477 }
478 
drm_panic_qr_exit(void)479 static void drm_panic_qr_exit(void)
480 {
481 	kfree(qrbuf1);
482 	qrbuf1 = NULL;
483 	kfree(qrbuf2);
484 	qrbuf2 = NULL;
485 	kfree(stream.workspace);
486 	stream.workspace = NULL;
487 }
488 
drm_panic_get_qr_code_url(u8 ** qr_image)489 static int drm_panic_get_qr_code_url(u8 **qr_image)
490 {
491 	struct kmsg_dump_iter iter;
492 	char url[256];
493 	size_t kmsg_len, max_kmsg_size;
494 	char *kmsg;
495 	int max_qr_data_size, url_len;
496 
497 	url_len = snprintf(url, sizeof(url), CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL "?a=%s&v=%s&z=",
498 			   utsname()->machine, utsname()->release);
499 
500 	max_qr_data_size = drm_panic_qr_max_data_size(panic_qr_version, url_len);
501 	max_kmsg_size = min(MAX_ZLIB_RATIO * max_qr_data_size, QR_BUFFER1_SIZE);
502 
503 	/* get kmsg to buffer 1 */
504 	kmsg_dump_rewind(&iter);
505 	kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
506 
507 	if (!kmsg_len)
508 		return -ENODATA;
509 	kmsg = qrbuf1;
510 
511 try_again:
512 	if (zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
513 			      MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
514 		return -EINVAL;
515 
516 	stream.next_in = kmsg;
517 	stream.avail_in = kmsg_len;
518 	stream.total_in = 0;
519 	stream.next_out = qrbuf2;
520 	stream.avail_out = QR_BUFFER2_SIZE;
521 	stream.total_out = 0;
522 
523 	if (zlib_deflate(&stream, Z_FINISH) != Z_STREAM_END)
524 		return -EINVAL;
525 
526 	if (zlib_deflateEnd(&stream) != Z_OK)
527 		return -EINVAL;
528 
529 	if (stream.total_out > max_qr_data_size) {
530 		/* too much data for the QR code, so skip the first line and try again */
531 		kmsg = strchr(kmsg, '\n');
532 		if (!kmsg)
533 			return -EINVAL;
534 		/* skip the first \n */
535 		kmsg += 1;
536 		kmsg_len = strlen(kmsg);
537 		goto try_again;
538 	}
539 	*qr_image = qrbuf2;
540 
541 	/* generate qr code image in buffer2 */
542 	return drm_panic_qr_generate(url, qrbuf2, stream.total_out, QR_BUFFER2_SIZE,
543 				     qrbuf1, QR_BUFFER1_SIZE);
544 }
545 
drm_panic_get_qr_code_raw(u8 ** qr_image)546 static int drm_panic_get_qr_code_raw(u8 **qr_image)
547 {
548 	struct kmsg_dump_iter iter;
549 	size_t kmsg_len;
550 	size_t max_kmsg_size = min(drm_panic_qr_max_data_size(panic_qr_version, 0),
551 				   QR_BUFFER1_SIZE);
552 
553 	kmsg_dump_rewind(&iter);
554 	kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
555 	if (!kmsg_len)
556 		return -ENODATA;
557 
558 	*qr_image = qrbuf1;
559 	return drm_panic_qr_generate(NULL, qrbuf1, kmsg_len, QR_BUFFER1_SIZE,
560 				     qrbuf2, QR_BUFFER2_SIZE);
561 }
562 
drm_panic_get_qr_code(u8 ** qr_image)563 static int drm_panic_get_qr_code(u8 **qr_image)
564 {
565 	if (strlen(CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL) > 0)
566 		return drm_panic_get_qr_code_url(qr_image);
567 	else
568 		return drm_panic_get_qr_code_raw(qr_image);
569 }
570 
571 /*
572  * Draw the panic message at the center of the screen, with a QR Code
573  */
_draw_panic_static_qr_code(struct drm_scanout_buffer * sb)574 static int _draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
575 {
576 	u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
577 						    sb->format->format);
578 	u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
579 						    sb->format->format);
580 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
581 	struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
582 	unsigned int max_qr_size, scale;
583 	unsigned int msg_width, msg_height;
584 	int qr_width, qr_canvas_width, qr_pitch, v_margin;
585 	u8 *qr_image;
586 
587 	if (!font || !qrbuf1 || !qrbuf2 || !stream.workspace)
588 		return -ENOMEM;
589 
590 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
591 
592 	drm_panic_logo_rect(&r_logo, font);
593 
594 	msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
595 	msg_height = min(panic_msg_lines * font->height, sb->height);
596 	r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
597 
598 	max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4);
599 
600 	qr_width = drm_panic_get_qr_code(&qr_image);
601 	if (qr_width <= 0)
602 		return -ENOSPC;
603 
604 	qr_canvas_width = qr_width + QR_MARGIN * 2;
605 	scale = max_qr_size / qr_canvas_width;
606 	/* QR code is not readable if not scaled at least by 2 */
607 	if (scale < 2)
608 		return -ENOSPC;
609 
610 	pr_debug("QR width %d and scale %d\n", qr_width, scale);
611 	r_qr_canvas = DRM_RECT_INIT(0, 0, qr_canvas_width * scale, qr_canvas_width * scale);
612 
613 	v_margin = (sb->height - drm_rect_height(&r_qr_canvas) - drm_rect_height(&r_msg)) / 5;
614 
615 	drm_rect_translate(&r_qr_canvas, (sb->width - r_qr_canvas.x2) / 2, 2 * v_margin);
616 	r_qr = DRM_RECT_INIT(r_qr_canvas.x1 + QR_MARGIN * scale, r_qr_canvas.y1 + QR_MARGIN * scale,
617 			     qr_width * scale, qr_width * scale);
618 
619 	/* Center the panic message */
620 	drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2,
621 			   3 * v_margin + drm_rect_height(&r_qr_canvas));
622 
623 	/* Fill with the background color, and draw text on top */
624 	drm_panic_fill(sb, &r_screen, bg_color);
625 
626 	if (!drm_rect_overlap(&r_logo, &r_msg) && !drm_rect_overlap(&r_logo, &r_qr))
627 		drm_panic_logo_draw(sb, &r_logo, font, fg_color);
628 
629 	draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
630 
631 	/* Draw the qr code */
632 	qr_pitch = DIV_ROUND_UP(qr_width, 8);
633 	drm_panic_fill(sb, &r_qr_canvas, fg_color);
634 	drm_panic_fill(sb, &r_qr, bg_color);
635 	drm_panic_blit(sb, &r_qr, qr_image, qr_pitch, scale, fg_color);
636 	return 0;
637 }
638 
draw_panic_static_qr_code(struct drm_scanout_buffer * sb)639 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
640 {
641 	if (_draw_panic_static_qr_code(sb))
642 		draw_panic_static_user(sb);
643 }
644 #else
draw_panic_static_qr_code(struct drm_scanout_buffer * sb)645 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
646 {
647 	draw_panic_static_user(sb);
648 }
649 
drm_panic_qr_init(void)650 static void drm_panic_qr_init(void) {};
drm_panic_qr_exit(void)651 static void drm_panic_qr_exit(void) {};
652 #endif
653 
654 /*
655  * drm_panic_is_format_supported()
656  * @format: a fourcc color code
657  * Returns: true if supported, false otherwise.
658  *
659  * Check if drm_panic will be able to use this color format.
660  */
drm_panic_is_format_supported(const struct drm_format_info * format)661 static bool drm_panic_is_format_supported(const struct drm_format_info *format)
662 {
663 	if (format->num_planes != 1)
664 		return false;
665 	return drm_draw_color_from_xrgb8888(0xffffff, format->format) != 0;
666 }
667 
draw_panic_dispatch(struct drm_scanout_buffer * sb)668 static void draw_panic_dispatch(struct drm_scanout_buffer *sb)
669 {
670 	if (!strcmp(drm_panic_screen, "kmsg")) {
671 		draw_panic_static_kmsg(sb);
672 	} else if (!strcmp(drm_panic_screen, "qr_code")) {
673 		draw_panic_static_qr_code(sb);
674 	} else {
675 		draw_panic_static_user(sb);
676 	}
677 }
678 
drm_panic_set_description(const char * description)679 static void drm_panic_set_description(const char *description)
680 {
681 	u32 len;
682 
683 	if (description) {
684 		struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
685 
686 		desc_line->txt = description;
687 		len = strlen(description);
688 		/* ignore the last newline character */
689 		if (len && description[len - 1] == '\n')
690 			len -= 1;
691 		desc_line->len = len;
692 	}
693 }
694 
drm_panic_clear_description(void)695 static void drm_panic_clear_description(void)
696 {
697 	struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
698 
699 	desc_line->len = 0;
700 	desc_line->txt = NULL;
701 }
702 
draw_panic_plane(struct drm_plane * plane,const char * description)703 static void draw_panic_plane(struct drm_plane *plane, const char *description)
704 {
705 	struct drm_scanout_buffer sb = { };
706 	int ret;
707 	unsigned long flags;
708 
709 	if (!drm_panic_trylock(plane->dev, flags))
710 		return;
711 
712 	drm_panic_set_description(description);
713 
714 	ret = plane->helper_private->get_scanout_buffer(plane, &sb);
715 
716 	if (!ret && drm_panic_is_format_supported(sb.format)) {
717 		draw_panic_dispatch(&sb);
718 		if (plane->helper_private->panic_flush)
719 			plane->helper_private->panic_flush(plane);
720 	}
721 	drm_panic_clear_description();
722 	drm_panic_unlock(plane->dev, flags);
723 }
724 
to_drm_plane(struct kmsg_dumper * kd)725 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd)
726 {
727 	return container_of(kd, struct drm_plane, kmsg_panic);
728 }
729 
drm_panic(struct kmsg_dumper * dumper,struct kmsg_dump_detail * detail)730 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail)
731 {
732 	struct drm_plane *plane = to_drm_plane(dumper);
733 
734 	if (detail->reason == KMSG_DUMP_PANIC)
735 		draw_panic_plane(plane, detail->description);
736 }
737 
738 
739 /*
740  * DEBUG FS, This is currently unsafe.
741  * Create one file per plane, so it's possible to debug one plane at a time.
742  * TODO: It would be better to emulate an NMI context.
743  */
744 #ifdef CONFIG_DRM_PANIC_DEBUG
745 #include <linux/debugfs.h>
746 
debugfs_trigger_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)747 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf,
748 				     size_t count, loff_t *ppos)
749 {
750 	bool run;
751 
752 	if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) {
753 		struct drm_plane *plane = file->private_data;
754 
755 		draw_panic_plane(plane, "Test from debugfs");
756 	}
757 	return count;
758 }
759 
760 static const struct file_operations dbg_drm_panic_ops = {
761 	.owner = THIS_MODULE,
762 	.write = debugfs_trigger_write,
763 	.open = simple_open,
764 };
765 
debugfs_register_plane(struct drm_plane * plane,int index)766 static void debugfs_register_plane(struct drm_plane *plane, int index)
767 {
768 	char fname[32];
769 
770 	snprintf(fname, 32, "drm_panic_plane_%d", index);
771 	debugfs_create_file(fname, 0200, plane->dev->debugfs_root,
772 			    plane, &dbg_drm_panic_ops);
773 }
774 #else
debugfs_register_plane(struct drm_plane * plane,int index)775 static void debugfs_register_plane(struct drm_plane *plane, int index) {}
776 #endif /* CONFIG_DRM_PANIC_DEBUG */
777 
778 /**
779  * drm_panic_is_enabled
780  * @dev: the drm device that may supports drm_panic
781  *
782  * returns true if the drm device supports drm_panic
783  */
drm_panic_is_enabled(struct drm_device * dev)784 bool drm_panic_is_enabled(struct drm_device *dev)
785 {
786 	struct drm_plane *plane;
787 
788 	if (!dev->mode_config.num_total_plane)
789 		return false;
790 
791 	drm_for_each_plane(plane, dev)
792 		if (plane->helper_private && plane->helper_private->get_scanout_buffer)
793 			return true;
794 	return false;
795 }
796 EXPORT_SYMBOL(drm_panic_is_enabled);
797 
798 /**
799  * drm_panic_register() - Initialize DRM panic for a device
800  * @dev: the drm device on which the panic screen will be displayed.
801  */
drm_panic_register(struct drm_device * dev)802 void drm_panic_register(struct drm_device *dev)
803 {
804 	struct drm_plane *plane;
805 	int registered_plane = 0;
806 
807 	if (!dev->mode_config.num_total_plane)
808 		return;
809 
810 	drm_for_each_plane(plane, dev) {
811 		if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
812 			continue;
813 		plane->kmsg_panic.dump = drm_panic;
814 		plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC;
815 		if (kmsg_dump_register(&plane->kmsg_panic))
816 			drm_warn(dev, "Failed to register panic handler\n");
817 		else {
818 			debugfs_register_plane(plane, registered_plane);
819 			registered_plane++;
820 		}
821 	}
822 	if (registered_plane)
823 		drm_info(dev, "Registered %d planes with drm panic\n", registered_plane);
824 }
825 
826 /**
827  * drm_panic_unregister()
828  * @dev: the drm device previously registered.
829  */
drm_panic_unregister(struct drm_device * dev)830 void drm_panic_unregister(struct drm_device *dev)
831 {
832 	struct drm_plane *plane;
833 
834 	if (!dev->mode_config.num_total_plane)
835 		return;
836 
837 	drm_for_each_plane(plane, dev) {
838 		if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
839 			continue;
840 		kmsg_dump_unregister(&plane->kmsg_panic);
841 	}
842 }
843 
844 /**
845  * drm_panic_init() - initialize DRM panic.
846  */
drm_panic_init(void)847 void __init drm_panic_init(void)
848 {
849 	drm_panic_qr_init();
850 }
851 
852 /**
853  * drm_panic_exit() - Free the resources taken by drm_panic_exit()
854  */
drm_panic_exit(void)855 void drm_panic_exit(void)
856 {
857 	drm_panic_qr_exit();
858 }
859