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
177 /*
178 * Special case if the pixel crosses page boundaries
179 */
drm_panic_write_pixel24_xpage(void * vaddr,struct page * next_page,unsigned int offset,u32 color)180 static void drm_panic_write_pixel24_xpage(void *vaddr, struct page *next_page,
181 unsigned int offset, u32 color)
182 {
183 u8 *vaddr2;
184 u8 *p = vaddr + offset;
185
186 vaddr2 = kmap_local_page_try_from_panic(next_page);
187
188 *p++ = color & 0xff;
189 color >>= 8;
190
191 if (offset == PAGE_SIZE - 1)
192 p = vaddr2;
193
194 *p++ = color & 0xff;
195 color >>= 8;
196
197 if (offset == PAGE_SIZE - 2)
198 p = vaddr2;
199
200 *p = color & 0xff;
201 kunmap_local(vaddr2);
202 }
203
drm_panic_write_pixel32(void * vaddr,unsigned int offset,u32 color)204 static void drm_panic_write_pixel32(void *vaddr, unsigned int offset, u32 color)
205 {
206 u32 *p = vaddr + offset;
207
208 *p = color;
209 }
210
drm_panic_write_pixel(void * vaddr,unsigned int offset,u32 color,unsigned int cpp)211 static void drm_panic_write_pixel(void *vaddr, unsigned int offset, u32 color, unsigned int cpp)
212 {
213 switch (cpp) {
214 case 2:
215 drm_panic_write_pixel16(vaddr, offset, color);
216 break;
217 case 3:
218 drm_panic_write_pixel24(vaddr, offset, color);
219 break;
220 case 4:
221 drm_panic_write_pixel32(vaddr, offset, color);
222 break;
223 default:
224 pr_debug_once("Can't blit with pixel width %d\n", cpp);
225 }
226 }
227
228 /*
229 * The scanout buffer pages are not mapped, so for each pixel,
230 * use kmap_local_page_try_from_panic() to map the page, and write the pixel.
231 * Try to keep the map from the previous pixel, to avoid too much map/unmap.
232 */
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)233 static void drm_panic_blit_page(struct page **pages, unsigned int dpitch,
234 unsigned int cpp, const u8 *sbuf8,
235 unsigned int spitch, struct drm_rect *clip,
236 unsigned int scale, u32 fg32)
237 {
238 unsigned int y, x;
239 unsigned int page = ~0;
240 unsigned int height = drm_rect_height(clip);
241 unsigned int width = drm_rect_width(clip);
242 void *vaddr = NULL;
243
244 for (y = 0; y < height; y++) {
245 for (x = 0; x < width; x++) {
246 if (drm_draw_is_pixel_fg(sbuf8, spitch, x / scale, y / scale)) {
247 unsigned int new_page;
248 unsigned int offset;
249
250 offset = (y + clip->y1) * dpitch + (x + clip->x1) * cpp;
251 new_page = offset >> PAGE_SHIFT;
252 offset = offset % PAGE_SIZE;
253 if (new_page != page) {
254 if (!pages[new_page])
255 continue;
256 if (vaddr)
257 kunmap_local(vaddr);
258 page = new_page;
259 vaddr = kmap_local_page_try_from_panic(pages[page]);
260 }
261 if (!vaddr)
262 continue;
263
264 // Special case for 24bit, as a pixel might cross page boundaries
265 if (cpp == 3 && offset + 3 > PAGE_SIZE)
266 drm_panic_write_pixel24_xpage(vaddr, pages[page + 1],
267 offset, fg32);
268 else
269 drm_panic_write_pixel(vaddr, offset, fg32, cpp);
270 }
271 }
272 }
273 if (vaddr)
274 kunmap_local(vaddr);
275 }
276
277 /*
278 * drm_panic_blit - convert a monochrome image to a linear framebuffer
279 * @sb: destination scanout buffer
280 * @clip: destination rectangle
281 * @sbuf8: source buffer, in monochrome format, 8 pixels per byte.
282 * @spitch: source pitch in bytes
283 * @scale: integer scale, source buffer is scale time smaller than destination
284 * rectangle
285 * @fg_color: foreground color, in destination format
286 *
287 * This can be used to draw a font character, which is a monochrome image, to a
288 * framebuffer in other supported format.
289 */
drm_panic_blit(struct drm_scanout_buffer * sb,struct drm_rect * clip,const u8 * sbuf8,unsigned int spitch,unsigned int scale,u32 fg_color)290 static void drm_panic_blit(struct drm_scanout_buffer *sb, struct drm_rect *clip,
291 const u8 *sbuf8, unsigned int spitch,
292 unsigned int scale, u32 fg_color)
293
294 {
295 struct iosys_map map;
296
297 if (sb->set_pixel)
298 return drm_panic_blit_pixel(sb, clip, sbuf8, spitch, scale, fg_color);
299
300 if (sb->pages)
301 return drm_panic_blit_page(sb->pages, sb->pitch[0], sb->format->cpp[0],
302 sbuf8, spitch, clip, scale, fg_color);
303
304 map = sb->map[0];
305 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]);
306
307 switch (sb->format->cpp[0]) {
308 case 2:
309 drm_draw_blit16(&map, sb->pitch[0], sbuf8, spitch,
310 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
311 break;
312 case 3:
313 drm_draw_blit24(&map, sb->pitch[0], sbuf8, spitch,
314 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
315 break;
316 case 4:
317 drm_draw_blit32(&map, sb->pitch[0], sbuf8, spitch,
318 drm_rect_height(clip), drm_rect_width(clip), scale, fg_color);
319 break;
320 default:
321 WARN_ONCE(1, "Can't blit with pixel width %d\n", sb->format->cpp[0]);
322 }
323 }
324
drm_panic_fill_pixel(struct drm_scanout_buffer * sb,struct drm_rect * clip,u32 color)325 static void drm_panic_fill_pixel(struct drm_scanout_buffer *sb,
326 struct drm_rect *clip,
327 u32 color)
328 {
329 unsigned int y, x;
330
331 for (y = 0; y < drm_rect_height(clip); y++)
332 for (x = 0; x < drm_rect_width(clip); x++)
333 sb->set_pixel(sb, clip->x1 + x, clip->y1 + y, color);
334 }
335
drm_panic_fill_page(struct page ** pages,unsigned int dpitch,unsigned int cpp,struct drm_rect * clip,u32 color)336 static void drm_panic_fill_page(struct page **pages, unsigned int dpitch,
337 unsigned int cpp, struct drm_rect *clip,
338 u32 color)
339 {
340 unsigned int y, x;
341 unsigned int page = ~0;
342 void *vaddr = NULL;
343
344 for (y = clip->y1; y < clip->y2; y++) {
345 for (x = clip->x1; x < clip->x2; x++) {
346 unsigned int new_page;
347 unsigned int offset;
348
349 offset = y * dpitch + x * cpp;
350 new_page = offset >> PAGE_SHIFT;
351 offset = offset % PAGE_SIZE;
352 if (new_page != page) {
353 if (vaddr)
354 kunmap_local(vaddr);
355 page = new_page;
356 vaddr = kmap_local_page_try_from_panic(pages[page]);
357 }
358 if (!vaddr)
359 continue;
360
361 // Special case for 24bit, as a pixel might cross page boundaries
362 if (cpp == 3 && offset + 3 > PAGE_SIZE)
363 drm_panic_write_pixel24_xpage(vaddr, pages[page + 1],
364 offset, color);
365 else
366 drm_panic_write_pixel(vaddr, offset, color, cpp);
367 }
368 }
369 if (vaddr)
370 kunmap_local(vaddr);
371 }
372
373 /*
374 * drm_panic_fill - Fill a rectangle with a color
375 * @sb: destination scanout buffer
376 * @clip: destination rectangle
377 * @color: foreground color, in destination format
378 *
379 * Fill a rectangle with a color, in a linear framebuffer.
380 */
drm_panic_fill(struct drm_scanout_buffer * sb,struct drm_rect * clip,u32 color)381 static void drm_panic_fill(struct drm_scanout_buffer *sb, struct drm_rect *clip,
382 u32 color)
383 {
384 struct iosys_map map;
385
386 if (sb->set_pixel)
387 return drm_panic_fill_pixel(sb, clip, color);
388
389 if (sb->pages)
390 return drm_panic_fill_page(sb->pages, sb->pitch[0], sb->format->cpp[0],
391 clip, color);
392
393 map = sb->map[0];
394 iosys_map_incr(&map, clip->y1 * sb->pitch[0] + clip->x1 * sb->format->cpp[0]);
395
396 switch (sb->format->cpp[0]) {
397 case 2:
398 drm_draw_fill16(&map, sb->pitch[0], drm_rect_height(clip),
399 drm_rect_width(clip), color);
400 break;
401 case 3:
402 drm_draw_fill24(&map, sb->pitch[0], drm_rect_height(clip),
403 drm_rect_width(clip), color);
404 break;
405 case 4:
406 drm_draw_fill32(&map, sb->pitch[0], drm_rect_height(clip),
407 drm_rect_width(clip), color);
408 break;
409 default:
410 WARN_ONCE(1, "Can't fill with pixel width %d\n", sb->format->cpp[0]);
411 }
412 }
413
get_max_line_len(const struct drm_panic_line * lines,int len)414 static unsigned int get_max_line_len(const struct drm_panic_line *lines, int len)
415 {
416 int i;
417 unsigned int max = 0;
418
419 for (i = 0; i < len; i++)
420 max = max(lines[i].len, max);
421 return max;
422 }
423
424 /*
425 * Draw a text in a rectangle on a framebuffer. The text is truncated if it overflows the rectangle
426 */
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)427 static void draw_txt_rectangle(struct drm_scanout_buffer *sb,
428 const struct font_desc *font,
429 const struct drm_panic_line *msg,
430 unsigned int msg_lines,
431 bool centered,
432 struct drm_rect *clip,
433 u32 color)
434 {
435 int i, j;
436 const u8 *src;
437 size_t font_pitch = DIV_ROUND_UP(font->width, 8);
438 struct drm_rect rec;
439
440 msg_lines = min(msg_lines, drm_rect_height(clip) / font->height);
441 for (i = 0; i < msg_lines; i++) {
442 size_t line_len = min(msg[i].len, drm_rect_width(clip) / font->width);
443
444 rec.y1 = clip->y1 + i * font->height;
445 rec.y2 = rec.y1 + font->height;
446 rec.x1 = clip->x1;
447
448 if (centered)
449 rec.x1 += (drm_rect_width(clip) - (line_len * font->width)) / 2;
450
451 for (j = 0; j < line_len; j++) {
452 src = drm_draw_get_char_bitmap(font, msg[i].txt[j], font_pitch);
453 rec.x2 = rec.x1 + font->width;
454 drm_panic_blit(sb, &rec, src, font_pitch, 1, color);
455 rec.x1 += font->width;
456 }
457 }
458 }
459
drm_panic_logo_rect(struct drm_rect * rect,const struct font_desc * font)460 static void drm_panic_logo_rect(struct drm_rect *rect, const struct font_desc *font)
461 {
462 if (logo_mono) {
463 drm_rect_init(rect, 0, 0, logo_mono->width, logo_mono->height);
464 } else {
465 int logo_width = get_max_line_len(logo_ascii, logo_ascii_lines) * font->width;
466
467 drm_rect_init(rect, 0, 0, logo_width, logo_ascii_lines * font->height);
468 }
469 }
470
drm_panic_logo_draw(struct drm_scanout_buffer * sb,struct drm_rect * rect,const struct font_desc * font,u32 fg_color)471 static void drm_panic_logo_draw(struct drm_scanout_buffer *sb, struct drm_rect *rect,
472 const struct font_desc *font, u32 fg_color)
473 {
474 if (rect->x2 > sb->width || rect->y2 > sb->height)
475 return;
476
477 if (logo_mono)
478 drm_panic_blit(sb, rect, logo_mono->data,
479 DIV_ROUND_UP(drm_rect_width(rect), 8), 1, fg_color);
480 else
481 draw_txt_rectangle(sb, font, logo_ascii, logo_ascii_lines, false, rect,
482 fg_color);
483 }
484
draw_panic_static_user(struct drm_scanout_buffer * sb)485 static void draw_panic_static_user(struct drm_scanout_buffer *sb)
486 {
487 u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
488 sb->format->format);
489 u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
490 sb->format->format);
491 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
492 struct drm_rect r_screen, r_logo, r_msg;
493 unsigned int msg_width, msg_height;
494
495 if (!font)
496 return;
497
498 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
499 drm_panic_logo_rect(&r_logo, font);
500
501 msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
502 msg_height = min(panic_msg_lines * font->height, sb->height);
503 r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
504
505 /* Center the panic message */
506 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2, (sb->height - r_msg.y2) / 2);
507
508 /* Fill with the background color, and draw text on top */
509 drm_panic_fill(sb, &r_screen, bg_color);
510
511 if (!drm_rect_overlap(&r_logo, &r_msg))
512 drm_panic_logo_draw(sb, &r_logo, font, fg_color);
513
514 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
515 }
516
517 /*
518 * Draw one line of kmsg, and handle wrapping if it won't fit in the screen width.
519 * Return the y-offset of the next line.
520 */
draw_line_with_wrap(struct drm_scanout_buffer * sb,const struct font_desc * font,struct drm_panic_line * line,int yoffset,u32 fg_color)521 static int draw_line_with_wrap(struct drm_scanout_buffer *sb, const struct font_desc *font,
522 struct drm_panic_line *line, int yoffset, u32 fg_color)
523 {
524 int chars_per_row = sb->width / font->width;
525 struct drm_rect r_txt = DRM_RECT_INIT(0, yoffset, sb->width, font->height);
526 struct drm_panic_line line_wrap;
527
528 if (line->len > chars_per_row) {
529 line_wrap.len = line->len % chars_per_row;
530 line_wrap.txt = line->txt + line->len - line_wrap.len;
531 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
532 r_txt.y1 -= font->height;
533 if (r_txt.y1 < 0)
534 return r_txt.y1;
535 while (line_wrap.txt > line->txt) {
536 line_wrap.txt -= chars_per_row;
537 line_wrap.len = chars_per_row;
538 draw_txt_rectangle(sb, font, &line_wrap, 1, false, &r_txt, fg_color);
539 r_txt.y1 -= font->height;
540 if (r_txt.y1 < 0)
541 return r_txt.y1;
542 }
543 } else {
544 draw_txt_rectangle(sb, font, line, 1, false, &r_txt, fg_color);
545 r_txt.y1 -= font->height;
546 }
547 return r_txt.y1;
548 }
549
550 /*
551 * Draw the kmsg buffer to the screen, starting from the youngest message at the bottom,
552 * and going up until reaching the top of the screen.
553 */
draw_panic_static_kmsg(struct drm_scanout_buffer * sb)554 static void draw_panic_static_kmsg(struct drm_scanout_buffer *sb)
555 {
556 u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
557 sb->format->format);
558 u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
559 sb->format->format);
560 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
561 struct drm_rect r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
562 struct kmsg_dump_iter iter;
563 char kmsg_buf[512];
564 size_t kmsg_len;
565 struct drm_panic_line line;
566 int yoffset;
567
568 if (!font || font->width > sb->width)
569 return;
570
571 yoffset = sb->height - font->height - (sb->height % font->height) / 2;
572
573 /* Fill with the background color, and draw text on top */
574 drm_panic_fill(sb, &r_screen, bg_color);
575
576 kmsg_dump_rewind(&iter);
577 while (kmsg_dump_get_buffer(&iter, false, kmsg_buf, sizeof(kmsg_buf), &kmsg_len)) {
578 char *start;
579 char *end;
580
581 /* ignore terminating NUL and newline */
582 start = kmsg_buf + kmsg_len - 2;
583 end = kmsg_buf + kmsg_len - 1;
584 while (start > kmsg_buf && yoffset >= 0) {
585 while (start > kmsg_buf && *start != '\n')
586 start--;
587 /* don't count the newline character */
588 line.txt = start + (start == kmsg_buf ? 0 : 1);
589 line.len = end - line.txt;
590
591 yoffset = draw_line_with_wrap(sb, font, &line, yoffset, fg_color);
592 end = start;
593 start--;
594 }
595 }
596 }
597
598 #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
599 /*
600 * It is unwise to allocate memory in the panic callback, so the buffers are
601 * pre-allocated. Only 2 buffers and the zlib workspace are needed.
602 * Two buffers are enough, using the following buffer usage:
603 * 1) kmsg messages are dumped in buffer1
604 * 2) kmsg is zlib-compressed into buffer2
605 * 3) compressed kmsg is encoded as QR-code Numeric stream in buffer1
606 * 4) QR-code image is generated in buffer2
607 * The Max QR code size is V40, 177x177, 4071 bytes for image, 2956 bytes for
608 * data segments.
609 *
610 * Typically, ~7500 bytes of kmsg, are compressed into 2800 bytes, which fits in
611 * a V40 QR-code (177x177).
612 *
613 * If CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL is not set, the kmsg data will be put
614 * directly in the QR code.
615 * 1) kmsg messages are dumped in buffer1
616 * 2) kmsg message is encoded as byte stream in buffer2
617 * 3) QR-code image is generated in buffer1
618 */
619
620 static uint panic_qr_version = CONFIG_DRM_PANIC_SCREEN_QR_VERSION;
621 module_param(panic_qr_version, uint, 0644);
622 MODULE_PARM_DESC(panic_qr_version, "maximum version (size) of the QR code");
623
624 #define MAX_QR_DATA 2956
625 #define MAX_ZLIB_RATIO 3
626 #define QR_BUFFER1_SIZE (MAX_ZLIB_RATIO * MAX_QR_DATA) /* Must also be > 4071 */
627 #define QR_BUFFER2_SIZE 4096
628 #define QR_MARGIN 4 /* 4 modules of foreground color around the qr code */
629
630 /* Compression parameters */
631 #define COMPR_LEVEL 6
632 #define WINDOW_BITS 12
633 #define MEM_LEVEL 4
634
635 static char *qrbuf1;
636 static char *qrbuf2;
637 static struct z_stream_s stream;
638
drm_panic_qr_init(void)639 static void __init drm_panic_qr_init(void)
640 {
641 qrbuf1 = kmalloc(QR_BUFFER1_SIZE, GFP_KERNEL);
642 qrbuf2 = kmalloc(QR_BUFFER2_SIZE, GFP_KERNEL);
643 stream.workspace = kmalloc(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
644 GFP_KERNEL);
645 }
646
drm_panic_qr_exit(void)647 static void drm_panic_qr_exit(void)
648 {
649 kfree(qrbuf1);
650 qrbuf1 = NULL;
651 kfree(qrbuf2);
652 qrbuf2 = NULL;
653 kfree(stream.workspace);
654 stream.workspace = NULL;
655 }
656
drm_panic_get_qr_code_url(u8 ** qr_image)657 static int drm_panic_get_qr_code_url(u8 **qr_image)
658 {
659 struct kmsg_dump_iter iter;
660 char url[256];
661 size_t kmsg_len, max_kmsg_size;
662 char *kmsg;
663 int max_qr_data_size, url_len;
664
665 url_len = snprintf(url, sizeof(url), CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL "?a=%s&v=%s&z=",
666 utsname()->machine, utsname()->release);
667
668 max_qr_data_size = drm_panic_qr_max_data_size(panic_qr_version, url_len);
669 max_kmsg_size = min(MAX_ZLIB_RATIO * max_qr_data_size, QR_BUFFER1_SIZE);
670
671 /* get kmsg to buffer 1 */
672 kmsg_dump_rewind(&iter);
673 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
674
675 if (!kmsg_len)
676 return -ENODATA;
677 kmsg = qrbuf1;
678
679 try_again:
680 if (zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
681 MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK)
682 return -EINVAL;
683
684 stream.next_in = kmsg;
685 stream.avail_in = kmsg_len;
686 stream.total_in = 0;
687 stream.next_out = qrbuf2;
688 stream.avail_out = QR_BUFFER2_SIZE;
689 stream.total_out = 0;
690
691 if (zlib_deflate(&stream, Z_FINISH) != Z_STREAM_END)
692 return -EINVAL;
693
694 if (zlib_deflateEnd(&stream) != Z_OK)
695 return -EINVAL;
696
697 if (stream.total_out > max_qr_data_size) {
698 /* too much data for the QR code, so skip the first line and try again */
699 kmsg = strchr(kmsg, '\n');
700 if (!kmsg)
701 return -EINVAL;
702 /* skip the first \n */
703 kmsg += 1;
704 kmsg_len = strlen(kmsg);
705 goto try_again;
706 }
707 *qr_image = qrbuf2;
708
709 /* generate qr code image in buffer2 */
710 return drm_panic_qr_generate(url, qrbuf2, stream.total_out, QR_BUFFER2_SIZE,
711 qrbuf1, QR_BUFFER1_SIZE);
712 }
713
drm_panic_get_qr_code_raw(u8 ** qr_image)714 static int drm_panic_get_qr_code_raw(u8 **qr_image)
715 {
716 struct kmsg_dump_iter iter;
717 size_t kmsg_len;
718 size_t max_kmsg_size = min(drm_panic_qr_max_data_size(panic_qr_version, 0),
719 QR_BUFFER1_SIZE);
720
721 kmsg_dump_rewind(&iter);
722 kmsg_dump_get_buffer(&iter, false, qrbuf1, max_kmsg_size, &kmsg_len);
723 if (!kmsg_len)
724 return -ENODATA;
725
726 *qr_image = qrbuf1;
727 return drm_panic_qr_generate(NULL, qrbuf1, kmsg_len, QR_BUFFER1_SIZE,
728 qrbuf2, QR_BUFFER2_SIZE);
729 }
730
drm_panic_get_qr_code(u8 ** qr_image)731 static int drm_panic_get_qr_code(u8 **qr_image)
732 {
733 if (strlen(CONFIG_DRM_PANIC_SCREEN_QR_CODE_URL) > 0)
734 return drm_panic_get_qr_code_url(qr_image);
735 else
736 return drm_panic_get_qr_code_raw(qr_image);
737 }
738
739 /*
740 * Draw the panic message at the center of the screen, with a QR Code
741 */
_draw_panic_static_qr_code(struct drm_scanout_buffer * sb)742 static int _draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
743 {
744 u32 fg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_FOREGROUND_COLOR,
745 sb->format->format);
746 u32 bg_color = drm_draw_color_from_xrgb8888(CONFIG_DRM_PANIC_BACKGROUND_COLOR,
747 sb->format->format);
748 const struct font_desc *font = get_default_font(sb->width, sb->height, NULL, NULL);
749 struct drm_rect r_screen, r_logo, r_msg, r_qr, r_qr_canvas;
750 unsigned int max_qr_size, scale;
751 unsigned int msg_width, msg_height;
752 int qr_width, qr_canvas_width, qr_pitch, v_margin;
753 u8 *qr_image;
754
755 if (!font || !qrbuf1 || !qrbuf2 || !stream.workspace)
756 return -ENOMEM;
757
758 r_screen = DRM_RECT_INIT(0, 0, sb->width, sb->height);
759
760 drm_panic_logo_rect(&r_logo, font);
761
762 msg_width = min(get_max_line_len(panic_msg, panic_msg_lines) * font->width, sb->width);
763 msg_height = min(panic_msg_lines * font->height, sb->height);
764 r_msg = DRM_RECT_INIT(0, 0, msg_width, msg_height);
765
766 max_qr_size = min(3 * sb->width / 4, 3 * sb->height / 4);
767
768 qr_width = drm_panic_get_qr_code(&qr_image);
769 if (qr_width <= 0)
770 return -ENOSPC;
771
772 qr_canvas_width = qr_width + QR_MARGIN * 2;
773 scale = max_qr_size / qr_canvas_width;
774 /* QR code is not readable if not scaled at least by 2 */
775 if (scale < 2)
776 return -ENOSPC;
777
778 pr_debug("QR width %d and scale %d\n", qr_width, scale);
779 r_qr_canvas = DRM_RECT_INIT(0, 0, qr_canvas_width * scale, qr_canvas_width * scale);
780
781 v_margin = sb->height - drm_rect_height(&r_qr_canvas) - drm_rect_height(&r_msg);
782 if (v_margin < 0)
783 return -ENOSPC;
784 v_margin /= 5;
785
786 drm_rect_translate(&r_qr_canvas, (sb->width - r_qr_canvas.x2) / 2, 2 * v_margin);
787 r_qr = DRM_RECT_INIT(r_qr_canvas.x1 + QR_MARGIN * scale, r_qr_canvas.y1 + QR_MARGIN * scale,
788 qr_width * scale, qr_width * scale);
789
790 /* Center the panic message */
791 drm_rect_translate(&r_msg, (sb->width - r_msg.x2) / 2,
792 3 * v_margin + drm_rect_height(&r_qr_canvas));
793
794 /* Fill with the background color, and draw text on top */
795 drm_panic_fill(sb, &r_screen, bg_color);
796
797 if (!drm_rect_overlap(&r_logo, &r_msg) && !drm_rect_overlap(&r_logo, &r_qr_canvas))
798 drm_panic_logo_draw(sb, &r_logo, font, fg_color);
799
800 draw_txt_rectangle(sb, font, panic_msg, panic_msg_lines, true, &r_msg, fg_color);
801
802 /* Draw the qr code */
803 qr_pitch = DIV_ROUND_UP(qr_width, 8);
804 drm_panic_fill(sb, &r_qr_canvas, fg_color);
805 drm_panic_fill(sb, &r_qr, bg_color);
806 drm_panic_blit(sb, &r_qr, qr_image, qr_pitch, scale, fg_color);
807 return 0;
808 }
809
draw_panic_static_qr_code(struct drm_scanout_buffer * sb)810 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
811 {
812 if (_draw_panic_static_qr_code(sb))
813 draw_panic_static_user(sb);
814 }
815 #else
draw_panic_static_qr_code(struct drm_scanout_buffer * sb)816 static void draw_panic_static_qr_code(struct drm_scanout_buffer *sb)
817 {
818 draw_panic_static_user(sb);
819 }
820
drm_panic_qr_init(void)821 static void drm_panic_qr_init(void) {};
drm_panic_qr_exit(void)822 static void drm_panic_qr_exit(void) {};
823 #endif
824
825 /*
826 * drm_panic_is_format_supported()
827 * @format: a fourcc color code
828 * Returns: true if supported, false otherwise.
829 *
830 * Check if drm_panic will be able to use this color format.
831 */
drm_panic_is_format_supported(const struct drm_format_info * format)832 static bool drm_panic_is_format_supported(const struct drm_format_info *format)
833 {
834 if (format->num_planes != 1)
835 return false;
836 return drm_draw_color_from_xrgb8888(0xffffff, format->format) != 0;
837 }
838
draw_panic_dispatch(struct drm_scanout_buffer * sb)839 static void draw_panic_dispatch(struct drm_scanout_buffer *sb)
840 {
841 if (!strcmp(drm_panic_screen, "kmsg")) {
842 draw_panic_static_kmsg(sb);
843 } else if (!strcmp(drm_panic_screen, "qr_code")) {
844 draw_panic_static_qr_code(sb);
845 } else {
846 draw_panic_static_user(sb);
847 }
848 }
849
drm_panic_set_description(const char * description)850 static void drm_panic_set_description(const char *description)
851 {
852 u32 len;
853
854 if (description) {
855 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
856
857 desc_line->txt = description;
858 len = strlen(description);
859 /* ignore the last newline character */
860 if (len && description[len - 1] == '\n')
861 len -= 1;
862 desc_line->len = len;
863 }
864 }
865
drm_panic_clear_description(void)866 static void drm_panic_clear_description(void)
867 {
868 struct drm_panic_line *desc_line = &panic_msg[panic_msg_lines - 1];
869
870 desc_line->len = 0;
871 desc_line->txt = NULL;
872 }
873
draw_panic_plane(struct drm_plane * plane,const char * description)874 static void draw_panic_plane(struct drm_plane *plane, const char *description)
875 {
876 struct drm_scanout_buffer sb = { };
877 int ret;
878 unsigned long flags;
879
880 if (!drm_panic_trylock(plane->dev, flags))
881 return;
882
883 ret = plane->helper_private->get_scanout_buffer(plane, &sb);
884
885 if (ret || !drm_panic_is_format_supported(sb.format))
886 goto unlock;
887
888 /* One of these should be set, or it can't draw pixels */
889 if (!sb.set_pixel && !sb.pages && iosys_map_is_null(&sb.map[0]))
890 goto unlock;
891
892 drm_panic_set_description(description);
893
894 draw_panic_dispatch(&sb);
895 if (plane->helper_private->panic_flush)
896 plane->helper_private->panic_flush(plane);
897
898 drm_panic_clear_description();
899
900 unlock:
901 drm_panic_unlock(plane->dev, flags);
902 }
903
to_drm_plane(struct kmsg_dumper * kd)904 static struct drm_plane *to_drm_plane(struct kmsg_dumper *kd)
905 {
906 return container_of(kd, struct drm_plane, kmsg_panic);
907 }
908
drm_panic(struct kmsg_dumper * dumper,struct kmsg_dump_detail * detail)909 static void drm_panic(struct kmsg_dumper *dumper, struct kmsg_dump_detail *detail)
910 {
911 struct drm_plane *plane = to_drm_plane(dumper);
912
913 if (detail->reason == KMSG_DUMP_PANIC)
914 draw_panic_plane(plane, detail->description);
915 }
916
917
918 /*
919 * DEBUG FS, This is currently unsafe.
920 * Create one file per plane, so it's possible to debug one plane at a time.
921 * TODO: It would be better to emulate an NMI context.
922 */
923 #ifdef CONFIG_DRM_PANIC_DEBUG
924 #include <linux/debugfs.h>
925
debugfs_trigger_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)926 static ssize_t debugfs_trigger_write(struct file *file, const char __user *user_buf,
927 size_t count, loff_t *ppos)
928 {
929 bool run;
930
931 if (kstrtobool_from_user(user_buf, count, &run) == 0 && run) {
932 struct drm_plane *plane = file->private_data;
933
934 draw_panic_plane(plane, "Test from debugfs");
935 }
936 return count;
937 }
938
939 static const struct file_operations dbg_drm_panic_ops = {
940 .owner = THIS_MODULE,
941 .write = debugfs_trigger_write,
942 .open = simple_open,
943 };
944
debugfs_register_plane(struct drm_plane * plane,int index)945 static void debugfs_register_plane(struct drm_plane *plane, int index)
946 {
947 char fname[32];
948
949 snprintf(fname, 32, "drm_panic_plane_%d", index);
950 debugfs_create_file(fname, 0200, plane->dev->debugfs_root,
951 plane, &dbg_drm_panic_ops);
952 }
953 #else
debugfs_register_plane(struct drm_plane * plane,int index)954 static void debugfs_register_plane(struct drm_plane *plane, int index) {}
955 #endif /* CONFIG_DRM_PANIC_DEBUG */
956
957 /**
958 * drm_panic_is_enabled
959 * @dev: the drm device that may supports drm_panic
960 *
961 * returns true if the drm device supports drm_panic
962 */
drm_panic_is_enabled(struct drm_device * dev)963 bool drm_panic_is_enabled(struct drm_device *dev)
964 {
965 struct drm_plane *plane;
966
967 if (!dev->mode_config.num_total_plane)
968 return false;
969
970 drm_for_each_plane(plane, dev)
971 if (plane->helper_private && plane->helper_private->get_scanout_buffer)
972 return true;
973 return false;
974 }
975 EXPORT_SYMBOL(drm_panic_is_enabled);
976
977 /**
978 * drm_panic_register() - Initialize DRM panic for a device
979 * @dev: the drm device on which the panic screen will be displayed.
980 */
drm_panic_register(struct drm_device * dev)981 void drm_panic_register(struct drm_device *dev)
982 {
983 struct drm_plane *plane;
984 int registered_plane = 0;
985
986 if (!dev->mode_config.num_total_plane)
987 return;
988
989 drm_for_each_plane(plane, dev) {
990 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
991 continue;
992 plane->kmsg_panic.dump = drm_panic;
993 plane->kmsg_panic.max_reason = KMSG_DUMP_PANIC;
994 if (kmsg_dump_register(&plane->kmsg_panic))
995 drm_warn(dev, "Failed to register panic handler\n");
996 else {
997 debugfs_register_plane(plane, registered_plane);
998 registered_plane++;
999 }
1000 }
1001 if (registered_plane)
1002 drm_info(dev, "Registered %d planes with drm panic\n", registered_plane);
1003 }
1004
1005 /**
1006 * drm_panic_unregister()
1007 * @dev: the drm device previously registered.
1008 */
drm_panic_unregister(struct drm_device * dev)1009 void drm_panic_unregister(struct drm_device *dev)
1010 {
1011 struct drm_plane *plane;
1012
1013 if (!dev->mode_config.num_total_plane)
1014 return;
1015
1016 drm_for_each_plane(plane, dev) {
1017 if (!plane->helper_private || !plane->helper_private->get_scanout_buffer)
1018 continue;
1019 kmsg_dump_unregister(&plane->kmsg_panic);
1020 }
1021 }
1022
1023 /**
1024 * drm_panic_init() - initialize DRM panic.
1025 */
drm_panic_init(void)1026 void __init drm_panic_init(void)
1027 {
1028 drm_panic_qr_init();
1029 }
1030
1031 /**
1032 * drm_panic_exit() - Free the resources taken by drm_panic_exit()
1033 */
drm_panic_exit(void)1034 void drm_panic_exit(void)
1035 {
1036 drm_panic_qr_exit();
1037 }
1038