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 <linux/pm_runtime.h>
20 #include <video/vga.h>
22 #include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */
23 #include <drm/drm_connector.h> /* For DRM_MODE_PANEL_ORIENTATION_* */
25 struct bmp_file_header
{
32 struct bmp_dib_header
{
46 static bool use_bgrt
= true;
47 static bool request_mem_succeeded
= false;
48 static u64 mem_flags
= EFI_MEMORY_WC
| EFI_MEMORY_UC
;
50 static struct pci_dev
*efifb_pci_dev
; /* dev with BAR covering the efifb */
52 static struct fb_var_screeninfo efifb_defined
= {
53 .activate
= FB_ACTIVATE_NOW
,
60 .vmode
= FB_VMODE_NONINTERLACED
,
63 static struct fb_fix_screeninfo efifb_fix
= {
65 .type
= FB_TYPE_PACKED_PIXELS
,
66 .accel
= FB_ACCEL_NONE
,
67 .visual
= FB_VISUAL_TRUECOLOR
,
70 static int efifb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
71 unsigned blue
, unsigned transp
,
75 * Set a single color register. The values supplied are
76 * already rounded down to the hardware's capabilities
77 * (according to the entries in the `var' structure). Return
78 * != 0 for invalid regno.
81 if (regno
>= info
->cmap
.len
)
85 red
>>= 16 - info
->var
.red
.length
;
86 green
>>= 16 - info
->var
.green
.length
;
87 blue
>>= 16 - info
->var
.blue
.length
;
88 ((u32
*)(info
->pseudo_palette
))[regno
] =
89 (red
<< info
->var
.red
.offset
) |
90 (green
<< info
->var
.green
.offset
) |
91 (blue
<< info
->var
.blue
.offset
);
97 * If fbcon deffered console takeover is configured, the intent is for the
98 * framebuffer to show the boot graphics (e.g. vendor logo) until there is some
99 * (error) message to display. But the boot graphics may have been destroyed by
100 * e.g. option ROM output, detect this and restore the boot graphics.
102 #if defined CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER && \
103 defined CONFIG_ACPI_BGRT
104 static void efifb_copy_bmp(u8
*src
, u32
*dst
, int width
, struct screen_info
*si
)
112 *dst
++ = (r
<< si
->red_pos
) |
113 (g
<< si
->green_pos
) |
120 * On x86 some firmwares use a low non native resolution for the display when
121 * they have shown some text messages. While keeping the bgrt filled with info
122 * for the native resolution. If the bgrt image intended for the native
123 * resolution still fits, it will be displayed very close to the right edge of
124 * the display looking quite bad. This function checks for this.
126 static bool efifb_bgrt_sanity_check(struct screen_info
*si
, u32 bmp_width
)
129 * All x86 firmwares horizontally center the image (the yoffset
130 * calculations differ between boards, but xoffset is predictable).
132 u32 expected_xoffset
= (si
->lfb_width
- bmp_width
) / 2;
134 return bgrt_tab
.image_offset_x
== expected_xoffset
;
137 static bool efifb_bgrt_sanity_check(struct screen_info
*si
, u32 bmp_width
)
143 static void efifb_show_boot_graphics(struct fb_info
*info
)
145 u32 bmp_width
, bmp_height
, bmp_pitch
, dst_x
, y
, src_y
;
146 struct screen_info
*si
= &screen_info
;
147 struct bmp_file_header
*file_header
;
148 struct bmp_dib_header
*dib_header
;
149 void *bgrt_image
= NULL
;
150 u8
*dst
= info
->screen_base
;
155 if (!bgrt_tab
.image_address
) {
156 pr_info("efifb: No BGRT, not showing boot graphics\n");
160 if (bgrt_tab
.status
& 0x06) {
161 pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n");
165 /* Avoid flashing the logo if we're going to print std probe messages */
166 if (console_loglevel
> CONSOLE_LOGLEVEL_QUIET
)
169 /* bgrt_tab.status is unreliable, so we don't check it */
171 if (si
->lfb_depth
!= 32) {
172 pr_info("efifb: not 32 bits, not showing boot graphics\n");
176 bgrt_image
= memremap(bgrt_tab
.image_address
, bgrt_image_size
,
179 pr_warn("efifb: Ignoring BGRT: failed to map image memory\n");
183 if (bgrt_image_size
< (sizeof(*file_header
) + sizeof(*dib_header
)))
186 file_header
= bgrt_image
;
187 if (file_header
->id
!= 0x4d42 || file_header
->reserved
!= 0)
190 dib_header
= bgrt_image
+ sizeof(*file_header
);
191 if (dib_header
->dib_header_size
!= 40 || dib_header
->width
< 0 ||
192 dib_header
->planes
!= 1 || dib_header
->bpp
!= 24 ||
193 dib_header
->compression
!= 0)
196 bmp_width
= dib_header
->width
;
197 bmp_height
= abs(dib_header
->height
);
198 bmp_pitch
= round_up(3 * bmp_width
, 4);
200 if ((file_header
->bitmap_offset
+ bmp_pitch
* bmp_height
) >
204 if ((bgrt_tab
.image_offset_x
+ bmp_width
) > si
->lfb_width
||
205 (bgrt_tab
.image_offset_y
+ bmp_height
) > si
->lfb_height
)
208 if (!efifb_bgrt_sanity_check(si
, bmp_width
))
211 pr_info("efifb: showing boot graphics\n");
213 for (y
= 0; y
< si
->lfb_height
; y
++, dst
+= si
->lfb_linelength
) {
214 /* Only background? */
215 if (y
< bgrt_tab
.image_offset_y
||
216 y
>= (bgrt_tab
.image_offset_y
+ bmp_height
)) {
217 memset(dst
, 0, 4 * si
->lfb_width
);
221 src_y
= y
- bgrt_tab
.image_offset_y
;
222 /* Positive header height means upside down row order */
223 if (dib_header
->height
> 0)
224 src_y
= (bmp_height
- 1) - src_y
;
226 memset(dst
, 0, bgrt_tab
.image_offset_x
* 4);
227 dst_x
= bgrt_tab
.image_offset_x
;
228 efifb_copy_bmp(bgrt_image
+ file_header
->bitmap_offset
+
230 (u32
*)dst
+ dst_x
, bmp_width
, si
);
232 memset((u32
*)dst
+ dst_x
, 0, (si
->lfb_width
- dst_x
) * 4);
235 memunmap(bgrt_image
);
239 memunmap(bgrt_image
);
240 pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n");
243 static inline void efifb_show_boot_graphics(struct fb_info
*info
) {}
246 static void efifb_destroy(struct fb_info
*info
)
249 pm_runtime_put(&efifb_pci_dev
->dev
);
251 if (info
->screen_base
) {
252 if (mem_flags
& (EFI_MEMORY_UC
| EFI_MEMORY_WC
))
253 iounmap(info
->screen_base
);
255 memunmap(info
->screen_base
);
257 if (request_mem_succeeded
)
258 release_mem_region(info
->apertures
->ranges
[0].base
,
259 info
->apertures
->ranges
[0].size
);
260 fb_dealloc_cmap(&info
->cmap
);
263 static const struct fb_ops efifb_ops
= {
264 .owner
= THIS_MODULE
,
265 .fb_destroy
= efifb_destroy
,
266 .fb_setcolreg
= efifb_setcolreg
,
267 .fb_fillrect
= cfb_fillrect
,
268 .fb_copyarea
= cfb_copyarea
,
269 .fb_imageblit
= cfb_imageblit
,
272 static int efifb_setup(char *options
)
276 if (options
&& *options
) {
277 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
278 if (!*this_opt
) continue;
280 efifb_setup_from_dmi(&screen_info
, this_opt
);
282 if (!strncmp(this_opt
, "base:", 5))
283 screen_info
.lfb_base
= simple_strtoul(this_opt
+5, NULL
, 0);
284 else if (!strncmp(this_opt
, "stride:", 7))
285 screen_info
.lfb_linelength
= simple_strtoul(this_opt
+7, NULL
, 0) * 4;
286 else if (!strncmp(this_opt
, "height:", 7))
287 screen_info
.lfb_height
= simple_strtoul(this_opt
+7, NULL
, 0);
288 else if (!strncmp(this_opt
, "width:", 6))
289 screen_info
.lfb_width
= simple_strtoul(this_opt
+6, NULL
, 0);
290 else if (!strcmp(this_opt
, "nowc"))
291 mem_flags
&= ~EFI_MEMORY_WC
;
292 else if (!strcmp(this_opt
, "nobgrt"))
300 static inline bool fb_base_is_valid(void)
302 if (screen_info
.lfb_base
)
305 if (!(screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
))
308 if (screen_info
.ext_lfb_base
)
314 #define efifb_attr_decl(name, fmt) \
315 static ssize_t name##_show(struct device *dev, \
316 struct device_attribute *attr, \
319 return sprintf(buf, fmt "\n", (screen_info.lfb_##name)); \
321 static DEVICE_ATTR_RO(name)
323 efifb_attr_decl(base
, "0x%x");
324 efifb_attr_decl(linelength
, "%u");
325 efifb_attr_decl(height
, "%u");
326 efifb_attr_decl(width
, "%u");
327 efifb_attr_decl(depth
, "%u");
329 static struct attribute
*efifb_attrs
[] = {
331 &dev_attr_linelength
.attr
,
332 &dev_attr_width
.attr
,
333 &dev_attr_height
.attr
,
334 &dev_attr_depth
.attr
,
337 ATTRIBUTE_GROUPS(efifb
);
339 static bool pci_dev_disabled
; /* FB base matches BAR of a disabled device */
341 static struct resource
*bar_resource
;
342 static u64 bar_offset
;
344 static int efifb_probe(struct platform_device
*dev
)
346 struct fb_info
*info
;
347 int err
, orientation
;
348 unsigned int size_vmode
;
349 unsigned int size_remap
;
350 unsigned int size_total
;
352 efi_memory_desc_t md
;
355 * Generic drivers must not be registered if a framebuffer exists.
356 * If a native driver was probed, the display hardware was already
357 * taken and attempting to use the system framebuffer is dangerous.
359 if (num_registered_fb
> 0) {
361 "efifb: a framebuffer is already registered\n");
365 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_EFI
|| pci_dev_disabled
)
368 if (fb_get_options("efifb", &option
))
372 /* We don't get linelength from UGA Draw Protocol, only from
373 * EFI Graphics Protocol. So if it's not in DMI, and it's not
374 * passed in from the user, we really can't use the framebuffer.
376 if (!screen_info
.lfb_linelength
)
379 if (!screen_info
.lfb_depth
)
380 screen_info
.lfb_depth
= 32;
381 if (!screen_info
.pages
)
382 screen_info
.pages
= 1;
383 if (!fb_base_is_valid()) {
384 printk(KERN_DEBUG
"efifb: invalid framebuffer address\n");
387 printk(KERN_INFO
"efifb: probing for efifb\n");
389 /* just assume they're all unset if any are */
390 if (!screen_info
.blue_size
) {
391 screen_info
.blue_size
= 8;
392 screen_info
.blue_pos
= 0;
393 screen_info
.green_size
= 8;
394 screen_info
.green_pos
= 8;
395 screen_info
.red_size
= 8;
396 screen_info
.red_pos
= 16;
397 screen_info
.rsvd_size
= 8;
398 screen_info
.rsvd_pos
= 24;
401 efifb_fix
.smem_start
= screen_info
.lfb_base
;
403 if (screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
) {
406 ext_lfb_base
= (u64
)(unsigned long)screen_info
.ext_lfb_base
<< 32;
407 efifb_fix
.smem_start
|= ext_lfb_base
;
411 bar_resource
->start
+ bar_offset
!= efifb_fix
.smem_start
) {
412 dev_info(&efifb_pci_dev
->dev
,
413 "BAR has moved, updating efifb address\n");
414 efifb_fix
.smem_start
= bar_resource
->start
+ bar_offset
;
417 efifb_defined
.bits_per_pixel
= screen_info
.lfb_depth
;
418 efifb_defined
.xres
= screen_info
.lfb_width
;
419 efifb_defined
.yres
= screen_info
.lfb_height
;
420 efifb_fix
.line_length
= screen_info
.lfb_linelength
;
422 /* size_vmode -- that is the amount of memory needed for the
423 * used video mode, i.e. the minimum amount of
425 size_vmode
= efifb_defined
.yres
* efifb_fix
.line_length
;
427 /* size_total -- all video memory we have. Used for
428 * entries, ressource allocation and bounds
430 size_total
= screen_info
.lfb_size
;
431 if (size_total
< size_vmode
)
432 size_total
= size_vmode
;
434 /* size_remap -- the amount of video memory we are going to
435 * use for efifb. With modern cards it is no
436 * option to simply use size_total as that
437 * wastes plenty of kernel address space. */
438 size_remap
= size_vmode
* 2;
439 if (size_remap
> size_total
)
440 size_remap
= size_total
;
441 if (size_remap
% PAGE_SIZE
)
442 size_remap
+= PAGE_SIZE
- (size_remap
% PAGE_SIZE
);
443 efifb_fix
.smem_len
= size_remap
;
445 if (request_mem_region(efifb_fix
.smem_start
, size_remap
, "efifb")) {
446 request_mem_succeeded
= true;
448 /* We cannot make this fatal. Sometimes this comes from magic
449 spaces our resource handlers simply don't know about */
450 pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
451 efifb_fix
.smem_start
);
454 info
= framebuffer_alloc(sizeof(u32
) * 16, &dev
->dev
);
457 goto err_release_mem
;
459 platform_set_drvdata(dev
, info
);
460 info
->pseudo_palette
= info
->par
;
463 info
->apertures
= alloc_apertures(1);
464 if (!info
->apertures
) {
468 info
->apertures
->ranges
[0].base
= efifb_fix
.smem_start
;
469 info
->apertures
->ranges
[0].size
= size_remap
;
471 if (efi_enabled(EFI_MEMMAP
) &&
472 !efi_mem_desc_lookup(efifb_fix
.smem_start
, &md
)) {
473 if ((efifb_fix
.smem_start
+ efifb_fix
.smem_len
) >
474 (md
.phys_addr
+ (md
.num_pages
<< EFI_PAGE_SHIFT
))) {
475 pr_err("efifb: video memory @ 0x%lx spans multiple EFI memory regions\n",
476 efifb_fix
.smem_start
);
481 * If the UEFI memory map covers the efifb region, we may only
482 * remap it using the attributes the memory map prescribes.
484 md
.attribute
&= EFI_MEMORY_UC
| EFI_MEMORY_WC
|
485 EFI_MEMORY_WT
| EFI_MEMORY_WB
;
487 mem_flags
|= EFI_MEMORY_WT
| EFI_MEMORY_WB
;
488 mem_flags
&= md
.attribute
;
491 if (mem_flags
& EFI_MEMORY_WC
)
492 info
->screen_base
= ioremap_wc(efifb_fix
.smem_start
,
494 else if (mem_flags
& EFI_MEMORY_UC
)
495 info
->screen_base
= ioremap(efifb_fix
.smem_start
,
497 else if (mem_flags
& EFI_MEMORY_WT
)
498 info
->screen_base
= memremap(efifb_fix
.smem_start
,
499 efifb_fix
.smem_len
, MEMREMAP_WT
);
500 else if (mem_flags
& EFI_MEMORY_WB
)
501 info
->screen_base
= memremap(efifb_fix
.smem_start
,
502 efifb_fix
.smem_len
, MEMREMAP_WB
);
503 if (!info
->screen_base
) {
504 pr_err("efifb: abort, cannot remap video memory 0x%x @ 0x%lx\n",
505 efifb_fix
.smem_len
, efifb_fix
.smem_start
);
510 efifb_show_boot_graphics(info
);
512 pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
513 efifb_fix
.smem_start
, size_remap
/1024, size_total
/1024);
514 pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
515 efifb_defined
.xres
, efifb_defined
.yres
,
516 efifb_defined
.bits_per_pixel
, efifb_fix
.line_length
,
519 efifb_defined
.xres_virtual
= efifb_defined
.xres
;
520 efifb_defined
.yres_virtual
= efifb_fix
.smem_len
/
521 efifb_fix
.line_length
;
522 pr_info("efifb: scrolling: redraw\n");
523 efifb_defined
.yres_virtual
= efifb_defined
.yres
;
525 /* some dummy values for timing to make fbset happy */
526 efifb_defined
.pixclock
= 10000000 / efifb_defined
.xres
*
527 1000 / efifb_defined
.yres
;
528 efifb_defined
.left_margin
= (efifb_defined
.xres
/ 8) & 0xf8;
529 efifb_defined
.hsync_len
= (efifb_defined
.xres
/ 8) & 0xf8;
531 efifb_defined
.red
.offset
= screen_info
.red_pos
;
532 efifb_defined
.red
.length
= screen_info
.red_size
;
533 efifb_defined
.green
.offset
= screen_info
.green_pos
;
534 efifb_defined
.green
.length
= screen_info
.green_size
;
535 efifb_defined
.blue
.offset
= screen_info
.blue_pos
;
536 efifb_defined
.blue
.length
= screen_info
.blue_size
;
537 efifb_defined
.transp
.offset
= screen_info
.rsvd_pos
;
538 efifb_defined
.transp
.length
= screen_info
.rsvd_size
;
540 pr_info("efifb: %s: "
541 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
543 screen_info
.rsvd_size
,
544 screen_info
.red_size
,
545 screen_info
.green_size
,
546 screen_info
.blue_size
,
547 screen_info
.rsvd_pos
,
549 screen_info
.green_pos
,
550 screen_info
.blue_pos
);
552 efifb_fix
.ypanstep
= 0;
553 efifb_fix
.ywrapstep
= 0;
555 info
->fbops
= &efifb_ops
;
556 info
->var
= efifb_defined
;
557 info
->fix
= efifb_fix
;
558 info
->flags
= FBINFO_FLAG_DEFAULT
| FBINFO_MISC_FIRMWARE
;
560 orientation
= drm_get_panel_orientation_quirk(efifb_defined
.xres
,
562 switch (orientation
) {
564 info
->fbcon_rotate_hint
= FB_ROTATE_UR
;
566 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP
:
567 info
->fbcon_rotate_hint
= FB_ROTATE_UD
;
569 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP
:
570 info
->fbcon_rotate_hint
= FB_ROTATE_CCW
;
572 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP
:
573 info
->fbcon_rotate_hint
= FB_ROTATE_CW
;
577 err
= sysfs_create_groups(&dev
->dev
.kobj
, efifb_groups
);
579 pr_err("efifb: cannot add sysfs attrs\n");
582 err
= fb_alloc_cmap(&info
->cmap
, 256, 0);
584 pr_err("efifb: cannot allocate colormap\n");
589 WARN_ON(pm_runtime_get_sync(&efifb_pci_dev
->dev
) < 0);
591 err
= register_framebuffer(info
);
593 pr_err("efifb: cannot register framebuffer\n");
594 goto err_put_rpm_ref
;
596 fb_info(info
, "%s frame buffer device\n", info
->fix
.id
);
601 pm_runtime_put(&efifb_pci_dev
->dev
);
603 fb_dealloc_cmap(&info
->cmap
);
605 sysfs_remove_groups(&dev
->dev
.kobj
, efifb_groups
);
607 if (mem_flags
& (EFI_MEMORY_UC
| EFI_MEMORY_WC
))
608 iounmap(info
->screen_base
);
610 memunmap(info
->screen_base
);
612 framebuffer_release(info
);
614 if (request_mem_succeeded
)
615 release_mem_region(efifb_fix
.smem_start
, size_total
);
619 static int efifb_remove(struct platform_device
*pdev
)
621 struct fb_info
*info
= platform_get_drvdata(pdev
);
623 unregister_framebuffer(info
);
624 sysfs_remove_groups(&pdev
->dev
.kobj
, efifb_groups
);
625 framebuffer_release(info
);
630 static struct platform_driver efifb_driver
= {
632 .name
= "efi-framebuffer",
634 .probe
= efifb_probe
,
635 .remove
= efifb_remove
,
638 builtin_platform_driver(efifb_driver
);
640 #if defined(CONFIG_PCI)
642 static void record_efifb_bar_resource(struct pci_dev
*dev
, int idx
, u64 offset
)
648 pci_read_config_word(dev
, PCI_COMMAND
, &word
);
649 if (!(word
& PCI_COMMAND_MEMORY
)) {
650 pci_dev_disabled
= true;
652 "BAR %d: assigned to efifb but device is disabled!\n",
657 bar_resource
= &dev
->resource
[idx
];
660 dev_info(&dev
->dev
, "BAR %d: assigned to efifb\n", idx
);
663 static void efifb_fixup_resources(struct pci_dev
*dev
)
665 u64 base
= screen_info
.lfb_base
;
666 u64 size
= screen_info
.lfb_size
;
669 if (efifb_pci_dev
|| screen_info
.orig_video_isVGA
!= VIDEO_TYPE_EFI
)
672 if (screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
)
673 base
|= (u64
)screen_info
.ext_lfb_base
<< 32;
678 for (i
= 0; i
< PCI_STD_NUM_BARS
; i
++) {
679 struct resource
*res
= &dev
->resource
[i
];
681 if (!(res
->flags
& IORESOURCE_MEM
))
684 if (res
->start
<= base
&& res
->end
>= base
+ size
- 1) {
685 record_efifb_bar_resource(dev
, i
, base
- res
->start
);
690 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID
, PCI_ANY_ID
, PCI_BASE_CLASS_DISPLAY
,
691 16, efifb_fixup_resources
);