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