drm_panic.c 30 KB

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