efifb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Framebuffer driver for EFI/UEFI based system
  4. *
  5. * (c) 2006 Edgar Hucek <gimli@dark-green.com>
  6. * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
  7. *
  8. */
  9. #include <linux/aperture.h>
  10. #include <linux/kernel.h>
  11. #include <linux/efi.h>
  12. #include <linux/efi-bgrt.h>
  13. #include <linux/errno.h>
  14. #include <linux/fb.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/printk.h>
  17. #include <linux/sysfb.h>
  18. #include <video/vga.h>
  19. #include <asm/efi.h>
  20. #include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */
  21. #include <drm/drm_connector.h> /* For DRM_MODE_PANEL_ORIENTATION_* */
  22. struct bmp_file_header {
  23. u16 id;
  24. u32 file_size;
  25. u32 reserved;
  26. u32 bitmap_offset;
  27. } __packed;
  28. struct bmp_dib_header {
  29. u32 dib_header_size;
  30. s32 width;
  31. s32 height;
  32. u16 planes;
  33. u16 bpp;
  34. u32 compression;
  35. u32 bitmap_size;
  36. u32 horz_resolution;
  37. u32 vert_resolution;
  38. u32 colors_used;
  39. u32 colors_important;
  40. } __packed;
  41. static bool use_bgrt = true;
  42. static bool request_mem_succeeded = false;
  43. static u64 mem_flags = EFI_MEMORY_WC | EFI_MEMORY_UC;
  44. struct efifb_par {
  45. u32 pseudo_palette[16];
  46. resource_size_t base;
  47. resource_size_t size;
  48. };
  49. static struct fb_var_screeninfo efifb_defined = {
  50. .activate = FB_ACTIVATE_NOW,
  51. .height = -1,
  52. .width = -1,
  53. .right_margin = 32,
  54. .upper_margin = 16,
  55. .lower_margin = 4,
  56. .vsync_len = 4,
  57. .vmode = FB_VMODE_NONINTERLACED,
  58. };
  59. static struct fb_fix_screeninfo efifb_fix = {
  60. .id = "EFI VGA",
  61. .type = FB_TYPE_PACKED_PIXELS,
  62. .accel = FB_ACCEL_NONE,
  63. .visual = FB_VISUAL_TRUECOLOR,
  64. };
  65. static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
  66. unsigned blue, unsigned transp,
  67. struct fb_info *info)
  68. {
  69. /*
  70. * Set a single color register. The values supplied are
  71. * already rounded down to the hardware's capabilities
  72. * (according to the entries in the `var' structure). Return
  73. * != 0 for invalid regno.
  74. */
  75. if (regno >= info->cmap.len)
  76. return 1;
  77. if (regno < 16) {
  78. red >>= 16 - info->var.red.length;
  79. green >>= 16 - info->var.green.length;
  80. blue >>= 16 - info->var.blue.length;
  81. ((u32 *)(info->pseudo_palette))[regno] =
  82. (red << info->var.red.offset) |
  83. (green << info->var.green.offset) |
  84. (blue << info->var.blue.offset);
  85. }
  86. return 0;
  87. }
  88. /*
  89. * If fbcon deffered console takeover is configured, the intent is for the
  90. * framebuffer to show the boot graphics (e.g. vendor logo) until there is some
  91. * (error) message to display. But the boot graphics may have been destroyed by
  92. * e.g. option ROM output, detect this and restore the boot graphics.
  93. */
  94. #if defined CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER && \
  95. defined CONFIG_ACPI_BGRT
  96. static void efifb_copy_bmp(u8 *src, u32 *dst, int width, const struct screen_info *si)
  97. {
  98. u8 r, g, b;
  99. while (width--) {
  100. b = *src++;
  101. g = *src++;
  102. r = *src++;
  103. *dst++ = (r << si->red_pos) |
  104. (g << si->green_pos) |
  105. (b << si->blue_pos);
  106. }
  107. }
  108. #ifdef CONFIG_X86
  109. /*
  110. * On x86 some firmwares use a low non native resolution for the display when
  111. * they have shown some text messages. While keeping the bgrt filled with info
  112. * for the native resolution. If the bgrt image intended for the native
  113. * resolution still fits, it will be displayed very close to the right edge of
  114. * the display looking quite bad. This function checks for this.
  115. */
  116. static bool efifb_bgrt_sanity_check(const struct screen_info *si, u32 bmp_width)
  117. {
  118. /*
  119. * All x86 firmwares horizontally center the image (the yoffset
  120. * calculations differ between boards, but xoffset is predictable).
  121. */
  122. u32 expected_xoffset = (si->lfb_width - bmp_width) / 2;
  123. return bgrt_tab.image_offset_x == expected_xoffset;
  124. }
  125. #else
  126. static bool efifb_bgrt_sanity_check(const struct screen_info *si, u32 bmp_width)
  127. {
  128. return true;
  129. }
  130. #endif
  131. static void efifb_show_boot_graphics(struct fb_info *info, const struct screen_info *si)
  132. {
  133. u32 bmp_width, bmp_height, bmp_pitch, dst_x, y, src_y;
  134. struct bmp_file_header *file_header;
  135. struct bmp_dib_header *dib_header;
  136. void *bgrt_image = NULL;
  137. u8 *dst = info->screen_base;
  138. if (!use_bgrt)
  139. return;
  140. if (!bgrt_tab.image_address) {
  141. pr_info("efifb: No BGRT, not showing boot graphics\n");
  142. return;
  143. }
  144. if (bgrt_tab.status & 0x06) {
  145. pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n");
  146. return;
  147. }
  148. /* Avoid flashing the logo if we're going to print std probe messages */
  149. if (console_loglevel > CONSOLE_LOGLEVEL_QUIET)
  150. return;
  151. /* bgrt_tab.status is unreliable, so we don't check it */
  152. if (si->lfb_depth != 32) {
  153. pr_info("efifb: not 32 bits, not showing boot graphics\n");
  154. return;
  155. }
  156. bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
  157. MEMREMAP_WB);
  158. if (!bgrt_image) {
  159. pr_warn("efifb: Ignoring BGRT: failed to map image memory\n");
  160. return;
  161. }
  162. if (bgrt_image_size < (sizeof(*file_header) + sizeof(*dib_header)))
  163. goto error;
  164. file_header = bgrt_image;
  165. if (file_header->id != 0x4d42 || file_header->reserved != 0)
  166. goto error;
  167. dib_header = bgrt_image + sizeof(*file_header);
  168. if (dib_header->dib_header_size != 40 || dib_header->width < 0 ||
  169. dib_header->planes != 1 || dib_header->bpp != 24 ||
  170. dib_header->compression != 0)
  171. goto error;
  172. bmp_width = dib_header->width;
  173. bmp_height = abs(dib_header->height);
  174. bmp_pitch = round_up(3 * bmp_width, 4);
  175. if ((file_header->bitmap_offset + bmp_pitch * bmp_height) >
  176. bgrt_image_size)
  177. goto error;
  178. if ((bgrt_tab.image_offset_x + bmp_width) > si->lfb_width ||
  179. (bgrt_tab.image_offset_y + bmp_height) > si->lfb_height)
  180. goto error;
  181. if (!efifb_bgrt_sanity_check(si, bmp_width))
  182. goto error;
  183. pr_info("efifb: showing boot graphics\n");
  184. for (y = 0; y < si->lfb_height; y++, dst += si->lfb_linelength) {
  185. /* Only background? */
  186. if (y < bgrt_tab.image_offset_y ||
  187. y >= (bgrt_tab.image_offset_y + bmp_height)) {
  188. memset(dst, 0, 4 * si->lfb_width);
  189. continue;
  190. }
  191. src_y = y - bgrt_tab.image_offset_y;
  192. /* Positive header height means upside down row order */
  193. if (dib_header->height > 0)
  194. src_y = (bmp_height - 1) - src_y;
  195. memset(dst, 0, bgrt_tab.image_offset_x * 4);
  196. dst_x = bgrt_tab.image_offset_x;
  197. efifb_copy_bmp(bgrt_image + file_header->bitmap_offset +
  198. src_y * bmp_pitch,
  199. (u32 *)dst + dst_x, bmp_width, si);
  200. dst_x += bmp_width;
  201. memset((u32 *)dst + dst_x, 0, (si->lfb_width - dst_x) * 4);
  202. }
  203. memunmap(bgrt_image);
  204. return;
  205. error:
  206. memunmap(bgrt_image);
  207. pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n");
  208. }
  209. #else
  210. static inline void efifb_show_boot_graphics(struct fb_info *info, const struct screen_info *si)
  211. { }
  212. #endif
  213. /*
  214. * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
  215. * of unregister_framebuffer() or fb_release(). Do any cleanup here.
  216. */
  217. static void efifb_destroy(struct fb_info *info)
  218. {
  219. struct efifb_par *par = info->par;
  220. if (info->screen_base) {
  221. if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
  222. iounmap(info->screen_base);
  223. else
  224. memunmap(info->screen_base);
  225. }
  226. if (request_mem_succeeded)
  227. release_mem_region(par->base, par->size);
  228. fb_dealloc_cmap(&info->cmap);
  229. framebuffer_release(info);
  230. }
  231. static const struct fb_ops efifb_ops = {
  232. .owner = THIS_MODULE,
  233. FB_DEFAULT_IOMEM_OPS,
  234. .fb_destroy = efifb_destroy,
  235. .fb_setcolreg = efifb_setcolreg,
  236. };
  237. static void efifb_setup(struct screen_info *si, char *options)
  238. {
  239. char *this_opt;
  240. if (options && *options) {
  241. while ((this_opt = strsep(&options, ",")) != NULL) {
  242. if (!*this_opt) continue;
  243. efifb_setup_from_dmi(si, this_opt);
  244. if (!strncmp(this_opt, "base:", 5))
  245. si->lfb_base = simple_strtoul(this_opt+5, NULL, 0);
  246. else if (!strncmp(this_opt, "stride:", 7))
  247. si->lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
  248. else if (!strncmp(this_opt, "height:", 7))
  249. si->lfb_height = simple_strtoul(this_opt+7, NULL, 0);
  250. else if (!strncmp(this_opt, "width:", 6))
  251. si->lfb_width = simple_strtoul(this_opt+6, NULL, 0);
  252. else if (!strcmp(this_opt, "nowc"))
  253. mem_flags &= ~EFI_MEMORY_WC;
  254. else if (!strcmp(this_opt, "nobgrt"))
  255. use_bgrt = false;
  256. }
  257. }
  258. }
  259. static inline bool fb_base_is_valid(struct screen_info *si)
  260. {
  261. if (si->lfb_base)
  262. return true;
  263. if (!(si->capabilities & VIDEO_CAPABILITY_64BIT_BASE))
  264. return false;
  265. if (si->ext_lfb_base)
  266. return true;
  267. return false;
  268. }
  269. #define efifb_attr_decl(name, fmt) \
  270. static ssize_t name##_show(struct device *dev, \
  271. struct device_attribute *attr, \
  272. char *buf) \
  273. { \
  274. struct screen_info *si = dev_get_drvdata(dev); \
  275. if (!si) \
  276. return -ENODEV; \
  277. return sprintf(buf, fmt "\n", (si->lfb_##name)); \
  278. } \
  279. static DEVICE_ATTR_RO(name)
  280. efifb_attr_decl(base, "0x%x");
  281. efifb_attr_decl(linelength, "%u");
  282. efifb_attr_decl(height, "%u");
  283. efifb_attr_decl(width, "%u");
  284. efifb_attr_decl(depth, "%u");
  285. static struct attribute *efifb_attrs[] = {
  286. &dev_attr_base.attr,
  287. &dev_attr_linelength.attr,
  288. &dev_attr_width.attr,
  289. &dev_attr_height.attr,
  290. &dev_attr_depth.attr,
  291. NULL
  292. };
  293. ATTRIBUTE_GROUPS(efifb);
  294. static int efifb_probe(struct platform_device *dev)
  295. {
  296. struct sysfb_display_info *dpy;
  297. struct screen_info *si;
  298. struct fb_info *info;
  299. struct efifb_par *par;
  300. int err, orientation;
  301. unsigned int size_vmode;
  302. unsigned int size_remap;
  303. unsigned int size_total;
  304. char *option = NULL;
  305. efi_memory_desc_t md;
  306. /*
  307. * If we fail probing the device, the kernel might try a different
  308. * driver. We get a copy of the attached screen_info, so that we can
  309. * modify its values without affecting later drivers.
  310. */
  311. dpy = dev_get_platdata(&dev->dev);
  312. if (!dpy)
  313. return -ENODEV;
  314. si = devm_kmemdup(&dev->dev, &dpy->screen, sizeof(*si), GFP_KERNEL);
  315. if (!si)
  316. return -ENOMEM;
  317. dev_set_drvdata(&dev->dev, si);
  318. if (si->orig_video_isVGA != VIDEO_TYPE_EFI)
  319. return -ENODEV;
  320. if (fb_get_options("efifb", &option))
  321. return -ENODEV;
  322. efifb_setup(si, option);
  323. /* We don't get linelength from UGA Draw Protocol, only from
  324. * EFI Graphics Protocol. So if it's not in DMI, and it's not
  325. * passed in from the user, we really can't use the framebuffer.
  326. */
  327. if (!si->lfb_linelength)
  328. return -ENODEV;
  329. if (!si->lfb_depth)
  330. si->lfb_depth = 32;
  331. if (!si->pages)
  332. si->pages = 1;
  333. if (!fb_base_is_valid(si)) {
  334. printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
  335. return -ENODEV;
  336. }
  337. printk(KERN_INFO "efifb: probing for efifb\n");
  338. /* just assume they're all unset if any are */
  339. if (!si->blue_size) {
  340. si->blue_size = 8;
  341. si->blue_pos = 0;
  342. si->green_size = 8;
  343. si->green_pos = 8;
  344. si->red_size = 8;
  345. si->red_pos = 16;
  346. si->rsvd_size = 8;
  347. si->rsvd_pos = 24;
  348. }
  349. efifb_fix.smem_start = __screen_info_lfb_base(si);
  350. efifb_defined.bits_per_pixel = si->lfb_depth;
  351. efifb_defined.xres = si->lfb_width;
  352. efifb_defined.yres = si->lfb_height;
  353. efifb_fix.line_length = si->lfb_linelength;
  354. /* size_vmode -- that is the amount of memory needed for the
  355. * used video mode, i.e. the minimum amount of
  356. * memory we need. */
  357. size_vmode = efifb_defined.yres * efifb_fix.line_length;
  358. /* size_total -- all video memory we have. Used for
  359. * entries, ressource allocation and bounds
  360. * checking. */
  361. size_total = si->lfb_size;
  362. if (size_total < size_vmode)
  363. size_total = size_vmode;
  364. /* size_remap -- the amount of video memory we are going to
  365. * use for efifb. With modern cards it is no
  366. * option to simply use size_total as that
  367. * wastes plenty of kernel address space. */
  368. size_remap = size_vmode * 2;
  369. if (size_remap > size_total)
  370. size_remap = size_total;
  371. if (size_remap % PAGE_SIZE)
  372. size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
  373. efifb_fix.smem_len = size_remap;
  374. if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
  375. request_mem_succeeded = true;
  376. } else {
  377. /* We cannot make this fatal. Sometimes this comes from magic
  378. spaces our resource handlers simply don't know about */
  379. pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
  380. efifb_fix.smem_start);
  381. }
  382. info = framebuffer_alloc(sizeof(*par), &dev->dev);
  383. if (!info) {
  384. err = -ENOMEM;
  385. goto err_release_mem;
  386. }
  387. par = info->par;
  388. info->pseudo_palette = par->pseudo_palette;
  389. par->base = efifb_fix.smem_start;
  390. par->size = size_remap;
  391. if (efi_enabled(EFI_MEMMAP) &&
  392. !efi_mem_desc_lookup(efifb_fix.smem_start, &md)) {
  393. if ((efifb_fix.smem_start + efifb_fix.smem_len) >
  394. (md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT))) {
  395. pr_err("efifb: video memory @ 0x%lx spans multiple EFI memory regions\n",
  396. efifb_fix.smem_start);
  397. err = -EIO;
  398. goto err_release_fb;
  399. }
  400. /*
  401. * If the UEFI memory map covers the efifb region, we may only
  402. * remap it using the attributes the memory map prescribes.
  403. */
  404. md.attribute &= EFI_MEMORY_UC | EFI_MEMORY_WC |
  405. EFI_MEMORY_WT | EFI_MEMORY_WB;
  406. if (md.attribute) {
  407. mem_flags |= EFI_MEMORY_WT | EFI_MEMORY_WB;
  408. mem_flags &= md.attribute;
  409. }
  410. }
  411. if (mem_flags & EFI_MEMORY_WC)
  412. info->screen_base = ioremap_wc(efifb_fix.smem_start,
  413. efifb_fix.smem_len);
  414. else if (mem_flags & EFI_MEMORY_UC)
  415. info->screen_base = ioremap(efifb_fix.smem_start,
  416. efifb_fix.smem_len);
  417. else if (mem_flags & EFI_MEMORY_WT)
  418. info->screen_base = memremap(efifb_fix.smem_start,
  419. efifb_fix.smem_len, MEMREMAP_WT);
  420. else if (mem_flags & EFI_MEMORY_WB)
  421. info->screen_base = memremap(efifb_fix.smem_start,
  422. efifb_fix.smem_len, MEMREMAP_WB);
  423. if (!info->screen_base) {
  424. pr_err("efifb: abort, cannot remap video memory 0x%x @ 0x%lx\n",
  425. efifb_fix.smem_len, efifb_fix.smem_start);
  426. err = -EIO;
  427. goto err_release_fb;
  428. }
  429. efifb_show_boot_graphics(info, si);
  430. pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
  431. efifb_fix.smem_start, size_remap/1024, size_total/1024);
  432. pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
  433. efifb_defined.xres, efifb_defined.yres,
  434. efifb_defined.bits_per_pixel, efifb_fix.line_length,
  435. si->pages);
  436. efifb_defined.xres_virtual = efifb_defined.xres;
  437. efifb_defined.yres_virtual = efifb_fix.smem_len /
  438. efifb_fix.line_length;
  439. pr_info("efifb: scrolling: redraw\n");
  440. efifb_defined.yres_virtual = efifb_defined.yres;
  441. /* some dummy values for timing to make fbset happy */
  442. efifb_defined.pixclock = 10000000 / efifb_defined.xres *
  443. 1000 / efifb_defined.yres;
  444. efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
  445. efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
  446. efifb_defined.red.offset = si->red_pos;
  447. efifb_defined.red.length = si->red_size;
  448. efifb_defined.green.offset = si->green_pos;
  449. efifb_defined.green.length = si->green_size;
  450. efifb_defined.blue.offset = si->blue_pos;
  451. efifb_defined.blue.length = si->blue_size;
  452. efifb_defined.transp.offset = si->rsvd_pos;
  453. efifb_defined.transp.length = si->rsvd_size;
  454. pr_info("efifb: %s: "
  455. "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
  456. "Truecolor",
  457. si->rsvd_size,
  458. si->red_size,
  459. si->green_size,
  460. si->blue_size,
  461. si->rsvd_pos,
  462. si->red_pos,
  463. si->green_pos,
  464. si->blue_pos);
  465. efifb_fix.ypanstep = 0;
  466. efifb_fix.ywrapstep = 0;
  467. info->fbops = &efifb_ops;
  468. info->var = efifb_defined;
  469. info->fix = efifb_fix;
  470. orientation = drm_get_panel_orientation_quirk(efifb_defined.xres,
  471. efifb_defined.yres);
  472. switch (orientation) {
  473. default:
  474. info->fbcon_rotate_hint = FB_ROTATE_UR;
  475. break;
  476. case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
  477. info->fbcon_rotate_hint = FB_ROTATE_UD;
  478. break;
  479. case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
  480. info->fbcon_rotate_hint = FB_ROTATE_CCW;
  481. break;
  482. case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
  483. info->fbcon_rotate_hint = FB_ROTATE_CW;
  484. break;
  485. }
  486. err = fb_alloc_cmap(&info->cmap, 256, 0);
  487. if (err < 0) {
  488. pr_err("efifb: cannot allocate colormap\n");
  489. goto err_unmap;
  490. }
  491. err = devm_aperture_acquire_for_platform_device(dev, par->base, par->size);
  492. if (err) {
  493. pr_err("efifb: cannot acquire aperture\n");
  494. goto err_fb_dealloc_cmap;
  495. }
  496. err = devm_register_framebuffer(&dev->dev, info);
  497. if (err < 0) {
  498. pr_err("efifb: cannot register framebuffer\n");
  499. goto err_fb_dealloc_cmap;
  500. }
  501. fb_info(info, "%s frame buffer device\n", info->fix.id);
  502. return 0;
  503. err_fb_dealloc_cmap:
  504. fb_dealloc_cmap(&info->cmap);
  505. err_unmap:
  506. if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
  507. iounmap(info->screen_base);
  508. else
  509. memunmap(info->screen_base);
  510. err_release_fb:
  511. framebuffer_release(info);
  512. err_release_mem:
  513. if (request_mem_succeeded)
  514. release_mem_region(efifb_fix.smem_start, size_total);
  515. return err;
  516. }
  517. static struct platform_driver efifb_driver = {
  518. .driver = {
  519. .name = "efi-framebuffer",
  520. .dev_groups = efifb_groups,
  521. },
  522. .probe = efifb_probe,
  523. };
  524. builtin_platform_driver(efifb_driver);