1 // SPDX-License-Identifier: GPL-2.0
3 * Framebuffer driver for EFI/UEFI based system
5 * (c) 2006 Edgar Hucek <gimli@dark-green.com>
6 * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
10 #include <linux/kernel.h>
11 #include <linux/efi.h>
12 #include <linux/efi-bgrt.h>
13 #include <linux/errno.h>
15 #include <linux/pci.h>
16 #include <linux/platform_device.h>
17 #include <linux/printk.h>
18 #include <linux/screen_info.h>
19 #include <video/vga.h>
21 #include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */
22 #include <drm/drm_connector.h> /* For DRM_MODE_PANEL_ORIENTATION_* */
24 struct bmp_file_header {
31 struct bmp_dib_header {
45 static bool use_bgrt = true;
46 static bool request_mem_succeeded = false;
47 static u64 mem_flags = EFI_MEMORY_WC | EFI_MEMORY_UC;
49 static struct fb_var_screeninfo efifb_defined = {
50 .activate = FB_ACTIVATE_NOW,
57 .vmode = FB_VMODE_NONINTERLACED,
60 static struct fb_fix_screeninfo efifb_fix = {
62 .type = FB_TYPE_PACKED_PIXELS,
63 .accel = FB_ACCEL_NONE,
64 .visual = FB_VISUAL_TRUECOLOR,
67 static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
68 unsigned blue, unsigned transp,
72 * Set a single color register. The values supplied are
73 * already rounded down to the hardware's capabilities
74 * (according to the entries in the `var' structure). Return
75 * != 0 for invalid regno.
78 if (regno >= info->cmap.len)
82 red >>= 16 - info->var.red.length;
83 green >>= 16 - info->var.green.length;
84 blue >>= 16 - info->var.blue.length;
85 ((u32 *)(info->pseudo_palette))[regno] =
86 (red << info->var.red.offset) |
87 (green << info->var.green.offset) |
88 (blue << info->var.blue.offset);
94 * If fbcon deffered console takeover is configured, the intent is for the
95 * framebuffer to show the boot graphics (e.g. vendor logo) until there is some
96 * (error) message to display. But the boot graphics may have been destroyed by
97 * e.g. option ROM output, detect this and restore the boot graphics.
99 #if defined CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER && \
100 defined CONFIG_ACPI_BGRT
101 static void efifb_copy_bmp(u8 *src, u32 *dst, int width, struct screen_info *si)
109 *dst++ = (r << si->red_pos) |
110 (g << si->green_pos) |
117 * On x86 some firmwares use a low non native resolution for the display when
118 * they have shown some text messages. While keeping the bgrt filled with info
119 * for the native resolution. If the bgrt image intended for the native
120 * resolution still fits, it will be displayed very close to the right edge of
121 * the display looking quite bad. This function checks for this.
123 static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width)
126 * All x86 firmwares horizontally center the image (the yoffset
127 * calculations differ between boards, but xoffset is predictable).
129 u32 expected_xoffset = (si->lfb_width - bmp_width) / 2;
131 return bgrt_tab.image_offset_x == expected_xoffset;
134 static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width)
140 static void efifb_show_boot_graphics(struct fb_info *info)
142 u32 bmp_width, bmp_height, bmp_pitch, dst_x, y, src_y;
143 struct screen_info *si = &screen_info;
144 struct bmp_file_header *file_header;
145 struct bmp_dib_header *dib_header;
146 void *bgrt_image = NULL;
147 u8 *dst = info->screen_base;
152 if (!bgrt_tab.image_address) {
153 pr_info("efifb: No BGRT, not showing boot graphics\n");
157 if (bgrt_tab.status & 0x06) {
158 pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n");
162 /* Avoid flashing the logo if we're going to print std probe messages */
163 if (console_loglevel > CONSOLE_LOGLEVEL_QUIET)
166 /* bgrt_tab.status is unreliable, so we don't check it */
168 if (si->lfb_depth != 32) {
169 pr_info("efifb: not 32 bits, not showing boot graphics\n");
173 bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
176 pr_warn("efifb: Ignoring BGRT: failed to map image memory\n");
180 if (bgrt_image_size < (sizeof(*file_header) + sizeof(*dib_header)))
183 file_header = bgrt_image;
184 if (file_header->id != 0x4d42 || file_header->reserved != 0)
187 dib_header = bgrt_image + sizeof(*file_header);
188 if (dib_header->dib_header_size != 40 || dib_header->width < 0 ||
189 dib_header->planes != 1 || dib_header->bpp != 24 ||
190 dib_header->compression != 0)
193 bmp_width = dib_header->width;
194 bmp_height = abs(dib_header->height);
195 bmp_pitch = round_up(3 * bmp_width, 4);
197 if ((file_header->bitmap_offset + bmp_pitch * bmp_height) >
201 if ((bgrt_tab.image_offset_x + bmp_width) > si->lfb_width ||
202 (bgrt_tab.image_offset_y + bmp_height) > si->lfb_height)
205 if (!efifb_bgrt_sanity_check(si, bmp_width))
208 pr_info("efifb: showing boot graphics\n");
210 for (y = 0; y < si->lfb_height; y++, dst += si->lfb_linelength) {
211 /* Only background? */
212 if (y < bgrt_tab.image_offset_y ||
213 y >= (bgrt_tab.image_offset_y + bmp_height)) {
214 memset(dst, 0, 4 * si->lfb_width);
218 src_y = y - bgrt_tab.image_offset_y;
219 /* Positive header height means upside down row order */
220 if (dib_header->height > 0)
221 src_y = (bmp_height - 1) - src_y;
223 memset(dst, 0, bgrt_tab.image_offset_x * 4);
224 dst_x = bgrt_tab.image_offset_x;
225 efifb_copy_bmp(bgrt_image + file_header->bitmap_offset +
227 (u32 *)dst + dst_x, bmp_width, si);
229 memset((u32 *)dst + dst_x, 0, (si->lfb_width - dst_x) * 4);
232 memunmap(bgrt_image);
236 memunmap(bgrt_image);
237 pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n");
240 static inline void efifb_show_boot_graphics(struct fb_info *info) {}
243 static void efifb_destroy(struct fb_info *info)
245 if (info->screen_base) {
246 if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
247 iounmap(info->screen_base);
249 memunmap(info->screen_base);
251 if (request_mem_succeeded)
252 release_mem_region(info->apertures->ranges[0].base,
253 info->apertures->ranges[0].size);
254 fb_dealloc_cmap(&info->cmap);
257 static const struct fb_ops efifb_ops = {
258 .owner = THIS_MODULE,
259 .fb_destroy = efifb_destroy,
260 .fb_setcolreg = efifb_setcolreg,
261 .fb_fillrect = cfb_fillrect,
262 .fb_copyarea = cfb_copyarea,
263 .fb_imageblit = cfb_imageblit,
266 static int efifb_setup(char *options)
270 if (options && *options) {
271 while ((this_opt = strsep(&options, ",")) != NULL) {
272 if (!*this_opt) continue;
274 efifb_setup_from_dmi(&screen_info, this_opt);
276 if (!strncmp(this_opt, "base:", 5))
277 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
278 else if (!strncmp(this_opt, "stride:", 7))
279 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
280 else if (!strncmp(this_opt, "height:", 7))
281 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
282 else if (!strncmp(this_opt, "width:", 6))
283 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
284 else if (!strcmp(this_opt, "nowc"))
285 mem_flags &= ~EFI_MEMORY_WC;
286 else if (!strcmp(this_opt, "nobgrt"))
294 static inline bool fb_base_is_valid(void)
296 if (screen_info.lfb_base)
299 if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE))
302 if (screen_info.ext_lfb_base)
308 #define efifb_attr_decl(name, fmt) \
309 static ssize_t name##_show(struct device *dev, \
310 struct device_attribute *attr, \
313 return sprintf(buf, fmt "\n", (screen_info.lfb_##name)); \
315 static DEVICE_ATTR_RO(name)
317 efifb_attr_decl(base, "0x%x");
318 efifb_attr_decl(linelength, "%u");
319 efifb_attr_decl(height, "%u");
320 efifb_attr_decl(width, "%u");
321 efifb_attr_decl(depth, "%u");
323 static struct attribute *efifb_attrs[] = {
325 &dev_attr_linelength.attr,
326 &dev_attr_width.attr,
327 &dev_attr_height.attr,
328 &dev_attr_depth.attr,
331 ATTRIBUTE_GROUPS(efifb);
333 static bool pci_dev_disabled; /* FB base matches BAR of a disabled device */
335 static struct pci_dev *efifb_pci_dev; /* dev with BAR covering the efifb */
336 static struct resource *bar_resource;
337 static u64 bar_offset;
339 static int efifb_probe(struct platform_device *dev)
341 struct fb_info *info;
342 int err, orientation;
343 unsigned int size_vmode;
344 unsigned int size_remap;
345 unsigned int size_total;
347 efi_memory_desc_t md;
349 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
352 if (fb_get_options("efifb", &option))
356 /* We don't get linelength from UGA Draw Protocol, only from
357 * EFI Graphics Protocol. So if it's not in DMI, and it's not
358 * passed in from the user, we really can't use the framebuffer.
360 if (!screen_info.lfb_linelength)
363 if (!screen_info.lfb_depth)
364 screen_info.lfb_depth = 32;
365 if (!screen_info.pages)
366 screen_info.pages = 1;
367 if (!fb_base_is_valid()) {
368 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
371 printk(KERN_INFO "efifb: probing for efifb\n");
373 /* just assume they're all unset if any are */
374 if (!screen_info.blue_size) {
375 screen_info.blue_size = 8;
376 screen_info.blue_pos = 0;
377 screen_info.green_size = 8;
378 screen_info.green_pos = 8;
379 screen_info.red_size = 8;
380 screen_info.red_pos = 16;
381 screen_info.rsvd_size = 8;
382 screen_info.rsvd_pos = 24;
385 efifb_fix.smem_start = screen_info.lfb_base;
387 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
390 ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
391 efifb_fix.smem_start |= ext_lfb_base;
395 bar_resource->start + bar_offset != efifb_fix.smem_start) {
396 dev_info(&efifb_pci_dev->dev,
397 "BAR has moved, updating efifb address\n");
398 efifb_fix.smem_start = bar_resource->start + bar_offset;
401 efifb_defined.bits_per_pixel = screen_info.lfb_depth;
402 efifb_defined.xres = screen_info.lfb_width;
403 efifb_defined.yres = screen_info.lfb_height;
404 efifb_fix.line_length = screen_info.lfb_linelength;
406 /* size_vmode -- that is the amount of memory needed for the
407 * used video mode, i.e. the minimum amount of
409 size_vmode = efifb_defined.yres * efifb_fix.line_length;
411 /* size_total -- all video memory we have. Used for
412 * entries, ressource allocation and bounds
414 size_total = screen_info.lfb_size;
415 if (size_total < size_vmode)
416 size_total = size_vmode;
418 /* size_remap -- the amount of video memory we are going to
419 * use for efifb. With modern cards it is no
420 * option to simply use size_total as that
421 * wastes plenty of kernel address space. */
422 size_remap = size_vmode * 2;
423 if (size_remap > size_total)
424 size_remap = size_total;
425 if (size_remap % PAGE_SIZE)
426 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
427 efifb_fix.smem_len = size_remap;
429 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
430 request_mem_succeeded = true;
432 /* We cannot make this fatal. Sometimes this comes from magic
433 spaces our resource handlers simply don't know about */
434 pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
435 efifb_fix.smem_start);
438 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
441 goto err_release_mem;
443 platform_set_drvdata(dev, info);
444 info->pseudo_palette = info->par;
447 info->apertures = alloc_apertures(1);
448 if (!info->apertures) {
452 info->apertures->ranges[0].base = efifb_fix.smem_start;
453 info->apertures->ranges[0].size = size_remap;
455 if (efi_enabled(EFI_MEMMAP) &&
456 !efi_mem_desc_lookup(efifb_fix.smem_start, &md)) {
457 if ((efifb_fix.smem_start + efifb_fix.smem_len) >
458 (md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT))) {
459 pr_err("efifb: video memory @ 0x%lx spans multiple EFI memory regions\n",
460 efifb_fix.smem_start);
465 * If the UEFI memory map covers the efifb region, we may only
466 * remap it using the attributes the memory map prescribes.
468 md.attribute &= EFI_MEMORY_UC | EFI_MEMORY_WC |
469 EFI_MEMORY_WT | EFI_MEMORY_WB;
471 mem_flags |= EFI_MEMORY_WT | EFI_MEMORY_WB;
472 mem_flags &= md.attribute;
475 if (mem_flags & EFI_MEMORY_WC)
476 info->screen_base = ioremap_wc(efifb_fix.smem_start,
478 else if (mem_flags & EFI_MEMORY_UC)
479 info->screen_base = ioremap(efifb_fix.smem_start,
481 else if (mem_flags & EFI_MEMORY_WT)
482 info->screen_base = memremap(efifb_fix.smem_start,
483 efifb_fix.smem_len, MEMREMAP_WT);
484 else if (mem_flags & EFI_MEMORY_WB)
485 info->screen_base = memremap(efifb_fix.smem_start,
486 efifb_fix.smem_len, MEMREMAP_WB);
487 if (!info->screen_base) {
488 pr_err("efifb: abort, cannot remap video memory 0x%x @ 0x%lx\n",
489 efifb_fix.smem_len, efifb_fix.smem_start);
494 efifb_show_boot_graphics(info);
496 pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
497 efifb_fix.smem_start, size_remap/1024, size_total/1024);
498 pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
499 efifb_defined.xres, efifb_defined.yres,
500 efifb_defined.bits_per_pixel, efifb_fix.line_length,
503 efifb_defined.xres_virtual = efifb_defined.xres;
504 efifb_defined.yres_virtual = efifb_fix.smem_len /
505 efifb_fix.line_length;
506 pr_info("efifb: scrolling: redraw\n");
507 efifb_defined.yres_virtual = efifb_defined.yres;
509 /* some dummy values for timing to make fbset happy */
510 efifb_defined.pixclock = 10000000 / efifb_defined.xres *
511 1000 / efifb_defined.yres;
512 efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
513 efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
515 efifb_defined.red.offset = screen_info.red_pos;
516 efifb_defined.red.length = screen_info.red_size;
517 efifb_defined.green.offset = screen_info.green_pos;
518 efifb_defined.green.length = screen_info.green_size;
519 efifb_defined.blue.offset = screen_info.blue_pos;
520 efifb_defined.blue.length = screen_info.blue_size;
521 efifb_defined.transp.offset = screen_info.rsvd_pos;
522 efifb_defined.transp.length = screen_info.rsvd_size;
524 pr_info("efifb: %s: "
525 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
527 screen_info.rsvd_size,
528 screen_info.red_size,
529 screen_info.green_size,
530 screen_info.blue_size,
531 screen_info.rsvd_pos,
533 screen_info.green_pos,
534 screen_info.blue_pos);
536 efifb_fix.ypanstep = 0;
537 efifb_fix.ywrapstep = 0;
539 info->fbops = &efifb_ops;
540 info->var = efifb_defined;
541 info->fix = efifb_fix;
542 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
544 orientation = drm_get_panel_orientation_quirk(efifb_defined.xres,
546 switch (orientation) {
548 info->fbcon_rotate_hint = FB_ROTATE_UR;
550 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
551 info->fbcon_rotate_hint = FB_ROTATE_UD;
553 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
554 info->fbcon_rotate_hint = FB_ROTATE_CCW;
556 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
557 info->fbcon_rotate_hint = FB_ROTATE_CW;
561 err = sysfs_create_groups(&dev->dev.kobj, efifb_groups);
563 pr_err("efifb: cannot add sysfs attrs\n");
566 err = fb_alloc_cmap(&info->cmap, 256, 0);
568 pr_err("efifb: cannot allocate colormap\n");
571 err = register_framebuffer(info);
573 pr_err("efifb: cannot register framebuffer\n");
576 fb_info(info, "%s frame buffer device\n", info->fix.id);
580 fb_dealloc_cmap(&info->cmap);
582 sysfs_remove_groups(&dev->dev.kobj, efifb_groups);
584 if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
585 iounmap(info->screen_base);
587 memunmap(info->screen_base);
589 framebuffer_release(info);
591 if (request_mem_succeeded)
592 release_mem_region(efifb_fix.smem_start, size_total);
596 static int efifb_remove(struct platform_device *pdev)
598 struct fb_info *info = platform_get_drvdata(pdev);
600 unregister_framebuffer(info);
601 sysfs_remove_groups(&pdev->dev.kobj, efifb_groups);
602 framebuffer_release(info);
607 static struct platform_driver efifb_driver = {
609 .name = "efi-framebuffer",
611 .probe = efifb_probe,
612 .remove = efifb_remove,
615 builtin_platform_driver(efifb_driver);
617 #if defined(CONFIG_PCI)
619 static void record_efifb_bar_resource(struct pci_dev *dev, int idx, u64 offset)
625 pci_read_config_word(dev, PCI_COMMAND, &word);
626 if (!(word & PCI_COMMAND_MEMORY)) {
627 pci_dev_disabled = true;
629 "BAR %d: assigned to efifb but device is disabled!\n",
634 bar_resource = &dev->resource[idx];
637 dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
640 static void efifb_fixup_resources(struct pci_dev *dev)
642 u64 base = screen_info.lfb_base;
643 u64 size = screen_info.lfb_size;
646 if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
649 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
650 base |= (u64)screen_info.ext_lfb_base << 32;
655 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
656 struct resource *res = &dev->resource[i];
658 if (!(res->flags & IORESOURCE_MEM))
661 if (res->start <= base && res->end >= base + size - 1) {
662 record_efifb_bar_resource(dev, i, base - res->start);
667 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
668 16, efifb_fixup_resources);