xref: /linux/drivers/gpu/drm/drm_panic_helper.c (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 // SPDX-License-Identifier: GPL-2.0 or MIT
2 
3 #include <linux/export.h>
4 #include <linux/font.h>
5 #include <linux/highmem.h>
6 #include <linux/iosys-map.h>
7 #include <linux/linux_logo.h>
8 #include <linux/utsname.h>
9 #include <linux/zlib.h>
10 
11 #include <kunit/visibility.h>
12 
13 #include <drm/drm_fourcc.h>
14 #include <drm/drm_framebuffer.h>
15 #include <drm/drm_modeset_helper_vtables.h>
16 #include <drm/drm_panic.h>
17 #include <drm/drm_panic_helper.h>
18 #include <drm/drm_plane.h>
19 #include <drm/drm_rect.h>
20 
21 #include "drm_draw_internal.h"
22 #include "drm_panic_internal.h"
23 
24 struct drm_panic_line {
25 	u32 len;
26 	const char *txt;
27 };
28 
29 #define PANIC_LINE(s) {.len = sizeof(s) - 1, .txt = s}
30 
31 static struct drm_panic_line panic_msg[] = {
32 	PANIC_LINE("KERNEL PANIC!"),
33 	PANIC_LINE(""),
34 	PANIC_LINE("Please reboot your computer."),
35 	PANIC_LINE(""),
36 	PANIC_LINE(""), /* will be replaced by the panic description */
37 };
38 
39 static const size_t panic_msg_lines = ARRAY_SIZE(panic_msg);
40 
41 static const struct drm_panic_line logo_ascii[] = {
42 	PANIC_LINE("     .--.        _"),
43 	PANIC_LINE("    |o_o |      | |"),
44 	PANIC_LINE("    |:_/ |      | |"),
45 	PANIC_LINE("   //   \\ \\     |_|"),
46 	PANIC_LINE("  (|     | )     _"),
47 	PANIC_LINE(" /'\\_   _/`\\    (_)"),
48 	PANIC_LINE(" \\___)=(___/"),
49 };
50 
51 static const size_t logo_ascii_lines = ARRAY_SIZE(logo_ascii);
52 
53 #if defined(CONFIG_LOGO) && !defined(MODULE)
54 static const struct linux_logo *logo_mono;
55 
56 static int __init drm_panic_helper_setup_logo(void)
57 {
58 	const struct linux_logo *logo = fb_find_logo(1);
59 	const unsigned char *logo_data;
60 	struct linux_logo *logo_dup;
61 
62 	if (!logo || logo->type != LINUX_LOGO_MONO)
63 		return 0;
64 
65 	/* The logo is __init, so we must make a copy for later use */
66 	logo_data = kmemdup(logo->data,
67 			    size_mul(DIV_ROUND_UP(logo->width, BITS_PER_BYTE), logo->height),
68 			    GFP_KERNEL);
69 	if (!logo_data)
70 		return -ENOMEM;
71 
72 	logo_dup = kmemdup(logo, sizeof(*logo), GFP_KERNEL);
73 	if (!logo_dup) {
74 		kfree(logo_data);
75 		return -ENOMEM;
76 	}
77 
78 	logo_dup->data = logo_data;
79 	logo_mono = logo_dup;
80 
81 	return 0;
82 }
83 #else
84 #define logo_mono	((const struct linux_logo *)NULL)
85 static int __init drm_panic_helper_setup_logo(void)
86 {
87 	return 0;
88 }
89 #endif
90 
91 /*
92  *  Blit & Fill functions
93  */
94 static void drm_panic_helper_blit_pixel(struct drm_scanout_buffer *sb, struct drm_rect *clip,
95 					const u8 *sbuf8, unsigned int spitch, unsigned int scale,
96 					u32 fg_color)
97 {
98 	unsigned int y, x;
99 
100 	for (y = 0; y < drm_rect_height(clip); y++)
101 		for (x = 0; x < drm_rect_width(clip); x++)
102 			if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale))
103 				sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, fg_color);
104 }
105 
106 static void drm_panic_helper_write_pixel16(void *vaddr, unsigned int offset, u16 color)
107 {
108 	u16 *p = vaddr + offset;
109 
110 	*p = color;
111 }
112 
113 static void drm_panic_helper_write_pixel24(void *vaddr, unsigned int offset, u32 color)
114 {
115 	u8 *p = vaddr + offset;
116 
117 	*p++ = color & 0xff;
118 	color >>= 8;
119 	*p++ = color & 0xff;
120 	color >>= 8;
121 	*p = color & 0xff;
122 }
123 
124 /*
125  * Special case if the pixel crosses page boundaries
126  */
127 static void drm_panic_helper_write_pixel24_xpage(void *vaddr, struct page *next_page,
128 						 unsigned int offset, u32 color)
129 {
130 	u8 *vaddr2;
131 	u8 *p = vaddr + offset;
132 
133 	vaddr2 = kmap_local_page_try_from_panic(next_page);
134 	if (!vaddr2)
135 		return;
136 
137 	*p++ = color & 0xff;
138 	color >>= 8;
139 
140 	if (offset == PAGE_SIZE - 1)
141 		p = vaddr2;
142 
143 	*p++ = color & 0xff;
144 	color >>= 8;
145 
146 	if (offset == PAGE_SIZE - 2)
147 		p = vaddr2;
148 
149 	*p = color & 0xff;
150 	kunmap_local(vaddr2);
151 }
152 
153 static void drm_panic_helper_write_pixel32(void *vaddr, unsigned int offset, u32 color)
154 {
155 	u32 *p = vaddr + offset;
156 
157 	*p = color;
158 }
159 
160 static void drm_panic_helper_write_pixel(void *vaddr, unsigned int offset, u32 color,
161 					 unsigned int cpp)
162 {
163 	switch (cpp) {
164 	case 2:
165 		drm_panic_helper_write_pixel16(vaddr, offset, color);
166 		break;
167 	case 3:
168 		drm_panic_helper_write_pixel24(vaddr, offset, color);
169 		break;
170 	case 4:
171 		drm_panic_helper_write_pixel32(vaddr, offset, color);
172 		break;
173 	default:
174 		pr_debug_once("Can't blit with pixel width %d\n", cpp);
175 	}
176 }
177 
178 /*
179  * The scanout buffer pages are not mapped, so for each pixel,
180  * use kmap_local_page_try_from_panic() to map the page, and write the pixel.
181  * Try to keep the map from the previous pixel, to avoid too much map/unmap.
182  */
183 static void drm_panic_helper_blit_page(struct page **pages, unsigned int dpitch,
184 				       unsigned int cpp, const u8 *sbuf8,
185 				       unsigned int spitch, struct drm_rect *clip,
186 				       unsigned int scale, u32 fg32)
187 {
188 	unsigned int y, x;
189 	unsigned int page = ~0;
190 	unsigned int height = drm_rect_height(clip);
191 	unsigned int width = drm_rect_width(clip);
192 	void *vaddr = NULL;
193 
194 	for (y = 0; y < height; y++) {
195 		for (x = 0; x < width; x++) {
196 			if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) {
197 				unsigned int new_page;
198 				unsigned int offset;
199 
200 				offset = (y + clip->y1) * dpitch + (x + clip->x1) * cpp;
201 				new_page = offset >> PAGE_SHIFT;
202 				offset = offset % PAGE_SIZE;
203 				if (new_page != page) {
204 					if (!pages[new_page])
205 						continue;
206 					if (vaddr)
207 						kunmap_local(vaddr);
208 					page = new_page;
209 					vaddr = kmap_local_page_try_from_panic(pages[page]);
210 				}
211 				if (!vaddr)
212 					continue;
213 
214 				// Special case for 24bit, as a pixel might cross page boundaries
215 				if (cpp == 3 && offset + 3 > PAGE_SIZE)
216 					drm_panic_helper_write_pixel24_xpage(vaddr,
217 									     pages[page + 1],
218 									     offset, fg32);
219 				else
220 					drm_panic_helper_write_pixel(vaddr, offset, fg32, cpp);
221 			}
222 		}
223 	}
224 	if (vaddr)
225 		kunmap_local(vaddr);
226 }
227 
228 /*
229  * drm_panic_helper_blit - convert a monochrome image to a linear framebuffer
230  * @sb: destination scanout buffer
231  * @clip: destination rectangle
232  * @sbuf8: source buffer, in monochrome format, 8 pixels per byte.
233  * @spitch: source pitch in bytes
234  * @scale: integer scale, source buffer is scale time smaller than destination
235  *         rectangle
236  * @fg_color: foreground color, in destination format
237  *
238  * This can be used to draw a font character, which is a monochrome image, to a
239  * framebuffer in other supported format.
240  */
241 static void drm_panic_helper_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip,
242 				  const u8 *sbuf8, unsigned int spitch,
243 				  unsigned int scale, u32 fg_color)
244 
245 {
246 	struct iosys_map map;
247 
248 	if (sb->set_pixel)
249 		return drm_panic_helper_blit_pixel(sb, clip, sbuf8, spitch, scale, fg_color);
250 
251 	if (sb->pages)
252 		return drm_panic_helper_blit_page(sb->pages, sb->pitch[0], sb->format->cpp[0],
253 						  sbuf8, spitch, clip, scale, fg_color);
254 
255 	map = sb->map[0];
256 	iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]);
257 
258 	switch (sb->format->cpp[0]) {
259 	case 2:
260 		drm_draw_blit16(&map, sb->pitch[0], sbuf8, spitch,
261 				drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
262 	break;
263 	case 3:
264 		drm_draw_blit24(&map, sb->pitch[0], sbuf8, spitch,
265 				drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
266 	break;
267 	case 4:
268 		drm_draw_blit32(&map, sb->pitch[0], sbuf8, spitch,
269 				drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
270 	break;
271 	default:
272 		WARN_ONCE(1, "Can't blit with pixel width %d\n", sb->format->cpp[0]);
273 	}
274 }
275 
276 static void drm_panic_helper_fill_pixel(struct drm_scanout_buffer *sb,
277 					struct drm_rect *clip,
278 					u32 color)
279 {
280 	unsigned int y, x;
281 
282 	for (y = 0; y < drm_rect_height(clip); y++)
283 		for (x = 0; x < drm_rect_width(clip); x++)
284 			sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, color);
285 }
286 
287 static void drm_panic_helper_fill_page(struct page **pages, unsigned int dpitch,
288 				       unsigned int cpp, struct drm_rect *clip,
289 				       u32 color)
290 {
291 	unsigned int y, x;
292 	unsigned int page = ~0;
293 	void *vaddr = NULL;
294 
295 	for (y = clip->y1; y < clip->y2; y++) {
296 		for (x = clip->x1; x < clip->x2; x++) {
297 			unsigned int new_page;
298 			unsigned int offset;
299 
300 			offset = y * dpitch + x * cpp;
301 			new_page = offset >> PAGE_SHIFT;
302 			offset = offset % PAGE_SIZE;
303 			if (new_page != page) {
304 				if (vaddr)
305 					kunmap_local(vaddr);
306 				page = new_page;
307 				vaddr = kmap_local_page_try_from_panic(pages[page]);
308 			}
309 			if (!vaddr)
310 				continue;
311 
312 			// Special case for 24bit, as a pixel might cross page boundaries
313 			if (cpp == 3 && offset + 3 > PAGE_SIZE)
314 				drm_panic_helper_write_pixel24_xpage(vaddr, pages[page + 1],
315 								     offset, color);
316 			else
317 				drm_panic_helper_write_pixel(vaddr, offset, color, cpp);
318 		}
319 	}
320 	if (vaddr)
321 		kunmap_local(vaddr);
322 }
323 
324 /*
325  * drm_panic_helper_fill - Fill a rectangle with a color
326  * @sb: destination scanout buffer
327  * @clip: destination rectangle
328  * @color: foreground color, in destination format
329  *
330  * Fill a rectangle with a color, in a linear framebuffer.
331  */
332 static void drm_panic_helper_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip,
333 				  u32 color)
334 {
335 	struct iosys_map map;
336 
337 	if (sb->set_pixel)
338 		return drm_panic_helper_fill_pixel(sb, clip, color);
339 
340 	if (sb->pages)
341 		return drm_panic_helper_fill_page(sb->pages, sb->pitch[0], sb->format->cpp[0],
342 						  clip, color);
343 
344 	map = sb->map[0];
345 	iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]);
346 
347 	switch (sb->format->cpp[0]) {
348 	case 2:
349 		drm_draw_fill16(&map, sb->pitch[0], drm_rect_height(clip),
350 				drm_rect_width(clip), color);
351 	break;
352 	case 3:
353 		drm_draw_fill24(&map, sb->pitch[0], drm_rect_height(clip),
354 				drm_rect_width(clip), color);
355 	break;
356 	case 4:
357 		drm_draw_fill32(&map, sb->pitch[0], drm_rect_height(clip),
358 				drm_rect_width(clip), color);
359 	break;
360 	default:
361 		WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]);
362 	}
363 }
364 
365 static unsigned int get_max_line_len(const struct drm_panic_line *lines, int len)
366 {
367 	int i;
368 	unsigned int max = 0;
369 
370 	for (i = 0; i < len; i++)
371 		max = max(lines[i].len, max);
372 	return max;
373 }
374 
375 /*
376  * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle
377  */
378 static void draw_txt_rectangle(struct drm_scanout_buffer *sb,
379 			       const struct font_desc *font,
380 			       const struct drm_panic_line *msg,
381 			       unsigned int msg_lines,
382 			       bool centered,
383 			       struct drm_rect *clip,
384 			       u32 color)
385 {
386 	int i, j;
387 	const u8 *src;
388 	size_t font_pitch = DIV_ROUND_UP(font->width, 8);
389 	struct drm_rect rec;
390 
391 	msg_lines = min(msg_lines,  drm_rect_height(clip) / font->height);
392 	for (i = 0; i < msg_lines; i++) {
393 		size_t line_len = min(msg[i].len, drm_rect_width(clip) / font->width);
394 
395 		rec.y1 = clip->y1 +  i * font->height;
396 		rec.y2 = rec.y1 + font->height;
397 		rec.x1 = clip->x1;
398 
399 		if (centered)
400 			rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2;
401 
402 		for (j = 0; j < line_len; j++) {
403 			src = font_data_glyph_buf(font->data, font->width, font->height,
404 						  (unsigned char)msg[i].txt[j]);
405 			rec.x2 = rec.x1 + font->width;
406 			if (src)
407 				drm_panic_helper_blit(sb, &rec, src, font_pitch, 1, color);
408 			rec.x1 += font->width;
409 		}
410 	}
411 }
412 
413 static void drm_panic_helper_logo_rect(struct drm_rect *rect, const struct font_desc *font)
414 {
415 	if (logo_mono) {
416 		drm_rect_init(rect, 0, 0, logo_mono->width, logo_mono->height);
417 	} else {
418 		int logo_width = get_max_line_len(logo_ascii, logo_ascii_lines) * font->width;
419 
420 		drm_rect_init(rect, 0, 0, logo_width, logo_ascii_lines * font->height);
421 	}
422 }
423 
424 static void drm_panic_helper_logo_draw(struct drm_scanout_buffer *sb, struct drm_rect *rect,
425 				       const struct font_desc *font, u32 fg_color)
426 {
427 	if (rect->x2 > sb->width || rect->y2 > sb->height)
428 		return;
429 
430 	if (logo_mono)
431 		drm_panic_helper_blit(sb, rect, logo_mono->data,
432 				      DIV_ROUND_UP(drm_rect_width(rect), 8), 1, fg_color);
433 	else
434 		draw_txt_rectangle(sb, font, logo_ascii, logo_ascii_lines, false, rect,
435 				   fg_color);
436 }
437 
438 VISIBLE_IF_KUNIT int drm_panic_helper_draw_screen_user(struct drm_scanout_buffer *sb,
439 						       u32 fg_color, u32 bg_color)
440 {
441 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
442 	struct drm_rect r_screen, r_logo, r_msg;
443 	unsigned int msg_width, msg_height;
444 
445 	if (!font || font->width > sb->width || font->height > sb->height)
446 		return -EINVAL;
447 
448 	fg_color = drm_draw_color_from_xrgb8888(fg_color, sb->format->format);
449 	bg_color = drm_draw_color_from_xrgb8888(bg_color, sb->format->format);
450 
451 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
452 	drm_panic_helper_logo_rect(&r_logo, font);
453 
454 	msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
455 	msg_height = min(panic_msg_lines * font->height, sb->height);
456 	r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
457 
458 	/* Center the panic message */
459 	drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2);
460 
461 	/* Fill with the background color, and draw text on top */
462 	drm_panic_helper_fill(sb, &r_screen, bg_color);
463 
464 	if (!drm_rect_overlap(&r_logo, &r_msg))
465 		drm_panic_helper_logo_draw(sb, &r_logo, font, fg_color);
466 
467 	draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
468 
469 	return 0;
470 }
471 EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_draw_screen_user);
472 
473 /*
474  * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width.
475  * Return the y-offset of the next line.
476  */
477 static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_desc *font,
478 			       struct drm_panic_line *line, int yoffset, u32 fg_color)
479 {
480 	int chars_per_row = sb->width / font->width;
481 	struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, font->height);
482 	struct drm_panic_line line_wrap;
483 
484 	if (line->len > chars_per_row) {
485 		line_wrap.len = line->len % chars_per_row;
486 		line_wrap.txt = line->txt + line->len - line_wrap.len;
487 		draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
488 		r_txt.y1 -= font->height;
489 		if (r_txt.y1 < 0)
490 			return r_txt.y1;
491 		while (line_wrap.txt > line->txt) {
492 			line_wrap.txt -= chars_per_row;
493 			line_wrap.len = chars_per_row;
494 			draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
495 			r_txt.y1 -= font->height;
496 			if (r_txt.y1 < 0)
497 				return r_txt.y1;
498 		}
499 	} else {
500 		draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color);
501 		r_txt.y1 -= font->height;
502 	}
503 	return r_txt.y1;
504 }
505 
506 /*
507  * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom,
508  * and going up until reaching the top of the screen.
509  */
510 VISIBLE_IF_KUNIT int drm_panic_helper_draw_screen_kmsg(struct drm_scanout_buffer *sb,
511 						       u32 fg_color, u32 bg_color)
512 {
513 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
514 	struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
515 	struct kmsg_dump_iter iter;
516 	char kmsg_buf[512];
517 	size_t kmsg_len;
518 	struct drm_panic_line line;
519 	int yoffset;
520 
521 	if (!font || font->width > sb->width || font->height > sb->height)
522 		return -EINVAL;
523 
524 	fg_color = drm_draw_color_from_xrgb8888(fg_color, sb->format->format);
525 	bg_color = drm_draw_color_from_xrgb8888(bg_color, sb->format->format);
526 
527 	yoffset = sb->height - font->height - (sb->height % font->height) / 2;
528 
529 	/* Fill with the background color, and draw text on top */
530 	drm_panic_helper_fill(sb, &r_screen, bg_color);
531 
532 	kmsg_dump_rewind(&iter);
533 	while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) {
534 		char *start;
535 		char *end;
536 
537 		/* ignore terminating NUL and newline */
538 		start = kmsg_buf + kmsg_len - 2;
539 		end = kmsg_buf + kmsg_len - 1;
540 		while (start > kmsg_buf && yoffset >= 0) {
541 			while (start > kmsg_buf && *start != '\n')
542 				start--;
543 			/* don't count the newline character */
544 			line.txt = start + (start == kmsg_buf ? 0 : 1);
545 			line.len = end - line.txt;
546 
547 			yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color);
548 			end = start;
549 			start--;
550 		}
551 	}
552 
553 	return 0;
554 }
555 EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_draw_screen_kmsg);
556 
557 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
558 /*
559  * It is unwise to allocate memory in the panic callback, so the buffers are
560  * pre-allocated. Only 2 buffers and the zlib workspace are needed.
561  * Two buffers are enough, using the following buffer usage:
562  * 1) kmsg messages are dumped in buffer1
563  * 2) kmsg is zlib-compressed into buffer2
564  * 3) compressed kmsg is encoded as QR-code Numeric stream in buffer1
565  * 4) QR-code image is generated in buffer2
566  * The Max QR code size is V40, 177x177, 4071 bytes for image, 2956 bytes for
567  * data segments.
568  *
569  * Typically, ~7500 bytes of kmsg, are compressed into 2800 bytes, which fits in
570  * a V40 QR-code (177x177).
571  *
572  * If CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL is not set, the kmsg data will be put
573  * directly in the QR code.
574  * 1) kmsg messages are dumped in buffer1
575  * 2) kmsg message is encoded as byte stream in buffer2
576  * 3) QR-code image is generated in buffer1
577  */
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 
594 static void __init drm_panic_helper_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 
602 static void drm_panic_helper_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 
612 static int drm_panic_helper_get_qr_code_url(u8 **qr_image, unsigned int qr_version)
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), "%s?a=%s&v=%s&z=",
621 			   CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL,
622 			   utsname()->machine, utsname()->release);
623 
624 	max_qr_data_size = drm_panic_helper_qr_max_data_size(qr_version, url_len);
625 	max_kmsg_size = min(MAX_ZLIB_RATIO * max_qr_data_size, QR_BUFFER1_SIZE);
626 
627 	/* get kmsg to buffer 1 */
628 	kmsg_dump_rewind(&iter);
629 	kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
630 
631 	if (!kmsg_len)
632 		return -ENODATA;
633 	kmsg = qrbuf1;
634 
635 try_again:
636 	if (zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
637 			      MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
638 		return -EINVAL;
639 
640 	stream.next_in = kmsg;
641 	stream.avail_in = kmsg_len;
642 	stream.total_in = 0;
643 	stream.next_out = qrbuf2;
644 	stream.avail_out = QR_BUFFER2_SIZE;
645 	stream.total_out = 0;
646 
647 	if (zlib_deflate(&stream, Z_FINISH) != Z_STREAM_END)
648 		return -EINVAL;
649 
650 	if (zlib_deflateEnd(&stream) != Z_OK)
651 		return -EINVAL;
652 
653 	if (stream.total_out > max_qr_data_size) {
654 		/* too much data for the QR code, so skip the first line and try again */
655 		kmsg = strchr(kmsg, '\n');
656 		if (!kmsg)
657 			return -EINVAL;
658 		/* skip the first \n */
659 		kmsg += 1;
660 		kmsg_len = strlen(kmsg);
661 		goto try_again;
662 	}
663 	*qr_image = qrbuf2;
664 
665 	/* generate qr code image in buffer2 */
666 	return drm_panic_helper_qr_generate(url, qrbuf2, stream.total_out, QR_BUFFER2_SIZE,
667 					    qrbuf1, QR_BUFFER1_SIZE);
668 }
669 
670 static int drm_panic_helper_get_qr_code_raw(u8 **qr_image, unsigned int qr_version)
671 {
672 	struct kmsg_dump_iter iter;
673 	size_t kmsg_len;
674 	size_t max_kmsg_size = min(drm_panic_helper_qr_max_data_size(qr_version, 0),
675 				   QR_BUFFER1_SIZE);
676 
677 	kmsg_dump_rewind(&iter);
678 	kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
679 	if (!kmsg_len)
680 		return -ENODATA;
681 
682 	*qr_image = qrbuf1;
683 	return drm_panic_helper_qr_generate(NULL, qrbuf1, kmsg_len, QR_BUFFER1_SIZE,
684 					    qrbuf2, QR_BUFFER2_SIZE);
685 }
686 
687 static int drm_panic_helper_get_qr_code(u8 **qr_image, unsigned int qr_version)
688 {
689 	if (strlen(CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL) > 0)
690 		return drm_panic_helper_get_qr_code_url(qr_image, qr_version);
691 	else
692 		return drm_panic_helper_get_qr_code_raw(qr_image, qr_version);
693 }
694 
695 /*
696  * Draw the panic message at the center of the screen, with a QR Code
697  */
698 VISIBLE_IF_KUNIT int drm_panic_helper_draw_screen_qr_code(struct drm_scanout_buffer *sb,
699 							  u32 fg_color, u32 bg_color,
700 							  unsigned int qr_version)
701 {
702 	const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
703 	struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
704 	unsigned int max_qr_size, scale;
705 	unsigned int msg_width, msg_height;
706 	int qr_width, qr_canvas_width, qr_pitch, v_margin;
707 	u8 *qr_image;
708 
709 	if (!qrbuf1 || !qrbuf2 || !stream.workspace)
710 		return -ENOMEM;
711 	if (!font)
712 		return -EINVAL;
713 
714 	fg_color = drm_draw_color_from_xrgb8888(fg_color, sb->format->format);
715 	bg_color = drm_draw_color_from_xrgb8888(bg_color, sb->format->format);
716 
717 	r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
718 
719 	drm_panic_helper_logo_rect(&r_logo, font);
720 
721 	msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
722 	msg_height = min(panic_msg_lines * font->height, sb->height);
723 	r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
724 
725 	max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4);
726 
727 	qr_width = drm_panic_helper_get_qr_code(&qr_image, qr_version);
728 	if (qr_width < 0)
729 		return qr_width;
730 	else if (!qr_width)
731 		return -ENOSPC;
732 
733 	qr_canvas_width = qr_width + QR_MARGIN * 2;
734 	scale = max_qr_size / qr_canvas_width;
735 	/* QR code is not readable if not scaled at least by 2 */
736 	if (scale < 2)
737 		return -ENOSPC;
738 
739 	pr_debug("QR width %d and scale %d\n", qr_width, scale);
740 	r_qr_canvas = DRM_RECT_INIT(0, 0, qr_canvas_width * scale, qr_canvas_width * scale);
741 
742 	v_margin = sb->height - drm_rect_height(&r_qr_canvas) - drm_rect_height(&r_msg);
743 	if (v_margin < 0)
744 		return -ENOSPC;
745 	v_margin /= 5;
746 
747 	drm_rect_translate(&r_qr_canvas, (sb->width - r_qr_canvas.x2) / 2, 2 * v_margin);
748 	r_qr = DRM_RECT_INIT(r_qr_canvas.x1 + QR_MARGIN * scale, r_qr_canvas.y1 + QR_MARGIN * scale,
749 			     qr_width * scale, qr_width * scale);
750 
751 	/* Center the panic message */
752 	drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2,
753 			   3 * v_margin + drm_rect_height(&r_qr_canvas));
754 
755 	/* Fill with the background color, and draw text on top */
756 	drm_panic_helper_fill(sb, &r_screen, bg_color);
757 
758 	if (!drm_rect_overlap(&r_logo, &r_msg) && !drm_rect_overlap(&r_logo, &r_qr_canvas))
759 		drm_panic_helper_logo_draw(sb, &r_logo, font, fg_color);
760 
761 	draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
762 
763 	/* Draw the qr code */
764 	qr_pitch = DIV_ROUND_UP(qr_width, 8);
765 	drm_panic_helper_fill(sb, &r_qr_canvas, fg_color);
766 	drm_panic_helper_fill(sb, &r_qr, bg_color);
767 	drm_panic_helper_blit(sb, &r_qr, qr_image, qr_pitch, scale, fg_color);
768 	return 0;
769 }
770 EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_draw_screen_qr_code);
771 #else
772 static void __init drm_panic_helper_qr_init(void) { };
773 static void __exit drm_panic_helper_qr_exit(void) { };
774 #endif
775 
776 /*
777  * drm_panic_helper_format_is_supported()
778  * @format: a fourcc color code
779  * Returns: true if supported, false otherwise.
780  *
781  * Check if drm_panic will be able to use this color format.
782  */
783 static bool drm_panic_helper_format_is_supported(const struct drm_format_info *format)
784 {
785 	if (format->num_planes != 1)
786 		return false;
787 	return drm_draw_can_convert_from_xrgb8888(format->format);
788 }
789 
790 static int draw_panic_helper_dispatch(struct drm_scanout_buffer *sb,
791 				      enum drm_panic_type panic_type,
792 				      u32 fg_color, u32 bg_color,
793 				      unsigned int qr_version)
794 {
795 	int ret;
796 
797 retry:
798 	switch (panic_type) {
799 	case DRM_PANIC_TYPE_KMSG:
800 		ret = drm_panic_helper_draw_screen_kmsg(sb, fg_color, bg_color);
801 		if (ret) {
802 			panic_type = DRM_PANIC_TYPE_USER;
803 			goto retry;
804 		}
805 		break;
806 #if IS_ENABLED(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
807 	case DRM_PANIC_TYPE_QR:
808 		ret = drm_panic_helper_draw_screen_qr_code(sb, fg_color, bg_color, qr_version);
809 		if (ret) {
810 			panic_type = DRM_PANIC_TYPE_USER;
811 			goto retry;
812 		}
813 		break;
814 #endif
815 	case DRM_PANIC_TYPE_USER:
816 	default:
817 		ret = drm_panic_helper_draw_screen_user(sb, fg_color, bg_color);
818 	}
819 
820 	return ret;
821 }
822 
823 VISIBLE_IF_KUNIT void drm_panic_helper_set_description(const char *description)
824 {
825 	u32 len;
826 
827 	if (description) {
828 		struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
829 
830 		desc_line->txt = description;
831 		len = strlen(description);
832 		/* ignore the last newline character */
833 		if (len && description[len - 1] == '\n')
834 			len -= 1;
835 		desc_line->len = len;
836 	}
837 }
838 EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_set_description);
839 
840 VISIBLE_IF_KUNIT void drm_panic_helper_clear_description(void)
841 {
842 	struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
843 
844 	desc_line->len = 0;
845 	desc_line->txt = NULL;
846 }
847 EXPORT_SYMBOL_IF_KUNIT(drm_panic_helper_clear_description);
848 
849 /**
850  * drm_plane_helper_display_panic_screen - Displays a panic screen according to the given settings
851  * @plane: the DRM plane to display to
852  * @description: error message to display
853  * @panic_type: type of panic screen
854  * @fg_color: text foreground color
855  * @bg_color: text background color
856  * @qr_version: version of the QR code, if any
857  *
858  * This helper display a panic screen on common primary planes. The panic
859  * screen can either display a kernel message, a user message or a QR code.
860  *
861  * The helper uses struct drm_plane_helper_funcs.get_scanout_buffer, where
862  * the plane can provide a scanout buffer that the panic handler can draw to.
863  * Currently only linear buffer and a few color formats are supported.
864  *
865  * Optionally the plane can also provide a &drm_plane_helper_funcs.panic_flush
866  * callback, which the DRM panic handler calls after drawing to send additional
867  * commands to the hardware to make the scanout buffer visible.
868  */
869 int drm_plane_helper_display_panic_screen(struct drm_plane *plane, const char *description,
870 					  enum drm_panic_type panic_type,
871 					  u32 fg_color, u32 bg_color, unsigned int qr_version)
872 {
873 	struct drm_scanout_buffer sb = { };
874 	int ret;
875 
876 	if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
877 		return -EINVAL;
878 
879 	ret = plane->helper_private->get_scanout_buffer(plane, &sb);
880 	if (ret)
881 		return ret;
882 
883 	if (!drm_panic_helper_format_is_supported(sb.format))
884 		return -EINVAL;
885 
886 	/* One of these should be set, or it can't draw pixels */
887 	if (!sb.set_pixel && !sb.pages && iosys_map_is_null(&sb.map[0]))
888 		return -EINVAL;
889 
890 	drm_panic_helper_set_description(description);
891 
892 	ret = draw_panic_helper_dispatch(&sb, panic_type, fg_color, bg_color, qr_version);
893 	if (!ret) {
894 		/*
895 		 * Only flush if we have a panic screen to display. Otherwise
896 		 * it's probably better to leave the display output as-is.
897 		 */
898 		if (plane->helper_private->panic_flush)
899 			plane->helper_private->panic_flush(plane);
900 	}
901 
902 	drm_panic_helper_clear_description();
903 
904 	return ret;
905 }
906 EXPORT_SYMBOL(drm_plane_helper_display_panic_screen);
907 
908 int __init drm_panic_helper_init(void)
909 {
910 	drm_panic_helper_setup_logo();
911 	drm_panic_helper_qr_init();
912 
913 	return 0;
914 }
915 
916 void __exit drm_panic_helper_exit(void)
917 {
918 	drm_panic_helper_qr_exit();
919 }
920