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