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
489 extern size_t drm_panic_qr_max_data_size(u8 version, size_t url_len);
490
491 extern u8 drm_panic_qr_generate(const char *url, u8 *data, size_t data_len, size_t data_size,
492 u8 *tmp, size_t tmp_size);
493
drm_panic_get_qr_code_url(u8 ** qr_image)494 static int drm_panic_get_qr_code_url(u8 **qr_image)
495 {
496 struct kmsg_dump_iter iter;
497 char url[256];
498 size_t kmsg_len, max_kmsg_size;
499 char *kmsg;
500 int max_qr_data_size, url_len;
501
502 url_len = snprintf(url, sizeof(url), CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL "?a=%s&v=%s&zl=",
503 utsname()->machine, utsname()->release);
504
505 max_qr_data_size = drm_panic_qr_max_data_size(panic_qr_version, url_len);
506 max_kmsg_size = min(MAX_ZLIB_RATIO * max_qr_data_size, QR_BUFFER1_SIZE);
507
508 /* get kmsg to buffer 1 */
509 kmsg_dump_rewind(&iter);
510 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
511
512 if (!kmsg_len)
513 return -ENODATA;
514 kmsg = qrbuf1;
515
516 try_again:
517 if (zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
518 MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
519 return -EINVAL;
520
521 stream.next_in = kmsg;
522 stream.avail_in = kmsg_len;
523 stream.total_in = 0;
524 stream.next_out = qrbuf2;
525 stream.avail_out = QR_BUFFER2_SIZE;
526 stream.total_out = 0;
527
528 if (zlib_deflate(&stream, Z_FINISH) != Z_STREAM_END)
529 return -EINVAL;
530
531 if (zlib_deflateEnd(&stream) != Z_OK)
532 return -EINVAL;
533
534 if (stream.total_out > max_qr_data_size) {
535 /* too much data for the QR code, so skip the first line and try again */
536 kmsg = strchr(kmsg, '\n');
537 if (!kmsg)
538 return -EINVAL;
539 /* skip the first \n */
540 kmsg += 1;
541 kmsg_len = strlen(kmsg);
542 goto try_again;
543 }
544 *qr_image = qrbuf2;
545
546 /* generate qr code image in buffer2 */
547 return drm_panic_qr_generate(url, qrbuf2, stream.total_out, QR_BUFFER2_SIZE,
548 qrbuf1, QR_BUFFER1_SIZE);
549 }
550
drm_panic_get_qr_code_raw(u8 ** qr_image)551 static int drm_panic_get_qr_code_raw(u8 **qr_image)
552 {
553 struct kmsg_dump_iter iter;
554 size_t kmsg_len;
555 size_t max_kmsg_size = min(drm_panic_qr_max_data_size(panic_qr_version, 0),
556 QR_BUFFER1_SIZE);
557
558 kmsg_dump_rewind(&iter);
559 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
560 if (!kmsg_len)
561 return -ENODATA;
562
563 *qr_image = qrbuf1;
564 return drm_panic_qr_generate(NULL, qrbuf1, kmsg_len, QR_BUFFER1_SIZE,
565 qrbuf2, QR_BUFFER2_SIZE);
566 }
567
drm_panic_get_qr_code(u8 ** qr_image)568 static int drm_panic_get_qr_code(u8 **qr_image)
569 {
570 if (strlen(CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL) > 0)
571 return drm_panic_get_qr_code_url(qr_image);
572 else
573 return drm_panic_get_qr_code_raw(qr_image);
574 }
575
576 /*
577 * Draw the panic message at the center of the screen, with a QR Code
578 */
_draw_panic_static_qr_code(struct drm_scanout_buffer * sb)579 static int _draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
580 {
581 u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
582 sb->format->format);
583 u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
584 sb->format->format);
585 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
586 struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
587 unsigned int max_qr_size, scale;
588 unsigned int msg_width, msg_height;
589 int qr_width, qr_canvas_width, qr_pitch, v_margin;
590 u8 *qr_image;
591
592 if (!font || !qrbuf1 || !qrbuf2 || !stream.workspace)
593 return -ENOMEM;
594
595 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
596
597 drm_panic_logo_rect(&r_logo, font);
598
599 msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
600 msg_height = min(panic_msg_lines * font->height, sb->height);
601 r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
602
603 max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4);
604
605 qr_width = drm_panic_get_qr_code(&qr_image);
606 if (qr_width <= 0)
607 return -ENOSPC;
608
609 qr_canvas_width = qr_width + QR_MARGIN * 2;
610 scale = max_qr_size / qr_canvas_width;
611 /* QR code is not readable if not scaled at least by 2 */
612 if (scale < 2)
613 return -ENOSPC;
614
615 pr_debug("QR width %d and scale %d\n", qr_width, scale);
616 r_qr_canvas = DRM_RECT_INIT(0, 0, qr_canvas_width * scale, qr_canvas_width * scale);
617
618 v_margin = (sb->height - drm_rect_height(&r_qr_canvas) - drm_rect_height(&r_msg)) / 5;
619
620 drm_rect_translate(&r_qr_canvas, (sb->width - r_qr_canvas.x2) / 2, 2 * v_margin);
621 r_qr = DRM_RECT_INIT(r_qr_canvas.x1 + QR_MARGIN * scale, r_qr_canvas.y1 + QR_MARGIN * scale,
622 qr_width * scale, qr_width * scale);
623
624 /* Center the panic message */
625 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2,
626 3 * v_margin + drm_rect_height(&r_qr_canvas));
627
628 /* Fill with the background color, and draw text on top */
629 drm_panic_fill(sb, &r_screen, bg_color);
630
631 if (!drm_rect_overlap(&r_logo, &r_msg) && !drm_rect_overlap(&r_logo, &r_qr))
632 drm_panic_logo_draw(sb, &r_logo, font, fg_color);
633
634 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
635
636 /* Draw the qr code */
637 qr_pitch = DIV_ROUND_UP(qr_width, 8);
638 drm_panic_fill(sb, &r_qr_canvas, fg_color);
639 drm_panic_fill(sb, &r_qr, bg_color);
640 drm_panic_blit(sb, &r_qr, qr_image, qr_pitch, scale, fg_color);
641 return 0;
642 }
643
draw_panic_static_qr_code(struct drm_scanout_buffer * sb)644 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
645 {
646 if (_draw_panic_static_qr_code(sb))
647 draw_panic_static_user(sb);
648 }
649 #else
draw_panic_static_qr_code(struct drm_scanout_buffer * sb)650 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
651 {
652 draw_panic_static_user(sb);
653 }
654
drm_panic_qr_init(void)655 static void drm_panic_qr_init(void) {};
drm_panic_qr_exit(void)656 static void drm_panic_qr_exit(void) {};
657 #endif
658
659 /*
660 * drm_panic_is_format_supported()
661 * @format: a fourcc color code
662 * Returns: true if supported, false otherwise.
663 *
664 * Check if drm_panic will be able to use this color format.
665 */
drm_panic_is_format_supported(const struct drm_format_info * format)666 static bool drm_panic_is_format_supported(const struct drm_format_info *format)
667 {
668 if (format->num_planes != 1)
669 return false;
670 return drm_draw_color_from_xrgb8888(0xffffff, format->format) != 0;
671 }
672
draw_panic_dispatch(struct drm_scanout_buffer * sb)673 static void draw_panic_dispatch(struct drm_scanout_buffer *sb)
674 {
675 if (!strcmp(drm_panic_screen, "kmsg")) {
676 draw_panic_static_kmsg(sb);
677 } else if (!strcmp(drm_panic_screen, "qr_code")) {
678 draw_panic_static_qr_code(sb);
679 } else {
680 draw_panic_static_user(sb);
681 }
682 }
683
drm_panic_set_description(const char * description)684 static void drm_panic_set_description(const char *description)
685 {
686 u32 len;
687
688 if (description) {
689 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
690
691 desc_line->txt = description;
692 len = strlen(description);
693 /* ignore the last newline character */
694 if (len && description[len - 1] == '\n')
695 len -= 1;
696 desc_line->len = len;
697 }
698 }
699
drm_panic_clear_description(void)700 static void drm_panic_clear_description(void)
701 {
702 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
703
704 desc_line->len = 0;
705 desc_line->txt = NULL;
706 }
707
draw_panic_plane(struct drm_plane * plane,const char * description)708 static void draw_panic_plane(struct drm_plane *plane, const char *description)
709 {
710 struct drm_scanout_buffer sb = { };
711 int ret;
712 unsigned long flags;
713
714 if (!drm_panic_trylock(plane->dev, flags))
715 return;
716
717 drm_panic_set_description(description);
718
719 ret = plane->helper_private->get_scanout_buffer(plane, &sb);
720
721 if (!ret && drm_panic_is_format_supported(sb.format)) {
722 draw_panic_dispatch(&sb);
723 if (plane->helper_private->panic_flush)
724 plane->helper_private->panic_flush(plane);
725 }
726 drm_panic_clear_description();
727 drm_panic_unlock(plane->dev, flags);
728 }
729
to_drm_plane(struct kmsg_dumper * kd)730 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd)
731 {
732 return container_of(kd, struct drm_plane, kmsg_panic);
733 }
734
drm_panic(struct kmsg_dumper * dumper,struct kmsg_dump_detail * detail)735 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail)
736 {
737 struct drm_plane *plane = to_drm_plane(dumper);
738
739 if (detail->reason == KMSG_DUMP_PANIC)
740 draw_panic_plane(plane, detail->description);
741 }
742
743
744 /*
745 * DEBUG FS, This is currently unsafe.
746 * Create one file per plane, so it's possible to debug one plane at a time.
747 * TODO: It would be better to emulate an NMI context.
748 */
749 #ifdef CONFIG_DRM_PANIC_DEBUG
750 #include <linux/debugfs.h>
751
debugfs_trigger_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)752 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf,
753 size_t count, loff_t *ppos)
754 {
755 bool run;
756
757 if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) {
758 struct drm_plane *plane = file->private_data;
759
760 draw_panic_plane(plane, "Test from debugfs");
761 }
762 return count;
763 }
764
765 static const struct file_operations dbg_drm_panic_ops = {
766 .owner = THIS_MODULE,
767 .write = debugfs_trigger_write,
768 .open = simple_open,
769 };
770
debugfs_register_plane(struct drm_plane * plane,int index)771 static void debugfs_register_plane(struct drm_plane *plane, int index)
772 {
773 char fname[32];
774
775 snprintf(fname, 32, "drm_panic_plane_%d", index);
776 debugfs_create_file(fname, 0200, plane->dev->debugfs_root,
777 plane, &dbg_drm_panic_ops);
778 }
779 #else
debugfs_register_plane(struct drm_plane * plane,int index)780 static void debugfs_register_plane(struct drm_plane *plane, int index) {}
781 #endif /* CONFIG_DRM_PANIC_DEBUG */
782
783 /**
784 * drm_panic_is_enabled
785 * @dev: the drm device that may supports drm_panic
786 *
787 * returns true if the drm device supports drm_panic
788 */
drm_panic_is_enabled(struct drm_device * dev)789 bool drm_panic_is_enabled(struct drm_device *dev)
790 {
791 struct drm_plane *plane;
792
793 if (!dev->mode_config.num_total_plane)
794 return false;
795
796 drm_for_each_plane(plane, dev)
797 if (plane->helper_private && plane->helper_private->get_scanout_buffer)
798 return true;
799 return false;
800 }
801 EXPORT_SYMBOL(drm_panic_is_enabled);
802
803 /**
804 * drm_panic_register() - Initialize DRM panic for a device
805 * @dev: the drm device on which the panic screen will be displayed.
806 */
drm_panic_register(struct drm_device * dev)807 void drm_panic_register(struct drm_device *dev)
808 {
809 struct drm_plane *plane;
810 int registered_plane = 0;
811
812 if (!dev->mode_config.num_total_plane)
813 return;
814
815 drm_for_each_plane(plane, dev) {
816 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
817 continue;
818 plane->kmsg_panic.dump = drm_panic;
819 plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC;
820 if (kmsg_dump_register(&plane->kmsg_panic))
821 drm_warn(dev, "Failed to register panic handler\n");
822 else {
823 debugfs_register_plane(plane, registered_plane);
824 registered_plane++;
825 }
826 }
827 if (registered_plane)
828 drm_info(dev, "Registered %d planes with drm panic\n", registered_plane);
829 }
830
831 /**
832 * drm_panic_unregister()
833 * @dev: the drm device previously registered.
834 */
drm_panic_unregister(struct drm_device * dev)835 void drm_panic_unregister(struct drm_device *dev)
836 {
837 struct drm_plane *plane;
838
839 if (!dev->mode_config.num_total_plane)
840 return;
841
842 drm_for_each_plane(plane, dev) {
843 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
844 continue;
845 kmsg_dump_unregister(&plane->kmsg_panic);
846 }
847 }
848
849 /**
850 * drm_panic_init() - initialize DRM panic.
851 */
drm_panic_init(void)852 void __init drm_panic_init(void)
853 {
854 drm_panic_qr_init();
855 }
856
857 /**
858 * drm_panic_exit() - Free the resources taken by drm_panic_exit()
859 */
drm_panic_exit(void)860 void drm_panic_exit(void)
861 {
862 drm_panic_qr_exit();
863 }
864