drm_panic.c 25 KB

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