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/aperture.h>
11 #include <linux/kernel.h>
12 #include <linux/efi.h>
13 #include <linux/efi-bgrt.h>
14 #include <linux/errno.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
;
50 u32 pseudo_palette
[16];
55 static struct fb_var_screeninfo efifb_defined
= {
56 .activate
= FB_ACTIVATE_NOW
,
63 .vmode
= FB_VMODE_NONINTERLACED
,
66 static struct fb_fix_screeninfo efifb_fix
= {
68 .type
= FB_TYPE_PACKED_PIXELS
,
69 .accel
= FB_ACCEL_NONE
,
70 .visual
= FB_VISUAL_TRUECOLOR
,
73 static int efifb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
74 unsigned blue
, unsigned transp
,
78 * Set a single color register. The values supplied are
79 * already rounded down to the hardware's capabilities
80 * (according to the entries in the `var' structure). Return
81 * != 0 for invalid regno.
84 if (regno
>= info
->cmap
.len
)
88 red
>>= 16 - info
->var
.red
.length
;
89 green
>>= 16 - info
->var
.green
.length
;
90 blue
>>= 16 - info
->var
.blue
.length
;
91 ((u32
*)(info
->pseudo_palette
))[regno
] =
92 (red
<< info
->var
.red
.offset
) |
93 (green
<< info
->var
.green
.offset
) |
94 (blue
<< info
->var
.blue
.offset
);
100 * If fbcon deffered console takeover is configured, the intent is for the
101 * framebuffer to show the boot graphics (e.g. vendor logo) until there is some
102 * (error) message to display. But the boot graphics may have been destroyed by
103 * e.g. option ROM output, detect this and restore the boot graphics.
105 #if defined CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER && \
106 defined CONFIG_ACPI_BGRT
107 static void efifb_copy_bmp(u8
*src
, u32
*dst
, int width
, const struct screen_info
*si
)
115 *dst
++ = (r
<< si
->red_pos
) |
116 (g
<< si
->green_pos
) |
123 * On x86 some firmwares use a low non native resolution for the display when
124 * they have shown some text messages. While keeping the bgrt filled with info
125 * for the native resolution. If the bgrt image intended for the native
126 * resolution still fits, it will be displayed very close to the right edge of
127 * the display looking quite bad. This function checks for this.
129 static bool efifb_bgrt_sanity_check(const struct screen_info
*si
, u32 bmp_width
)
132 * All x86 firmwares horizontally center the image (the yoffset
133 * calculations differ between boards, but xoffset is predictable).
135 u32 expected_xoffset
= (si
->lfb_width
- bmp_width
) / 2;
137 return bgrt_tab
.image_offset_x
== expected_xoffset
;
140 static bool efifb_bgrt_sanity_check(const struct screen_info
*si
, u32 bmp_width
)
146 static void efifb_show_boot_graphics(struct fb_info
*info
, const struct screen_info
*si
)
148 u32 bmp_width
, bmp_height
, bmp_pitch
, dst_x
, y
, src_y
;
149 struct bmp_file_header
*file_header
;
150 struct bmp_dib_header
*dib_header
;
151 void *bgrt_image
= NULL
;
152 u8
*dst
= info
->screen_base
;
157 if (!bgrt_tab
.image_address
) {
158 pr_info("efifb: No BGRT, not showing boot graphics\n");
162 if (bgrt_tab
.status
& 0x06) {
163 pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n");
167 /* Avoid flashing the logo if we're going to print std probe messages */
168 if (console_loglevel
> CONSOLE_LOGLEVEL_QUIET
)
171 /* bgrt_tab.status is unreliable, so we don't check it */
173 if (si
->lfb_depth
!= 32) {
174 pr_info("efifb: not 32 bits, not showing boot graphics\n");
178 bgrt_image
= memremap(bgrt_tab
.image_address
, bgrt_image_size
,
181 pr_warn("efifb: Ignoring BGRT: failed to map image memory\n");
185 if (bgrt_image_size
< (sizeof(*file_header
) + sizeof(*dib_header
)))
188 file_header
= bgrt_image
;
189 if (file_header
->id
!= 0x4d42 || file_header
->reserved
!= 0)
192 dib_header
= bgrt_image
+ sizeof(*file_header
);
193 if (dib_header
->dib_header_size
!= 40 || dib_header
->width
< 0 ||
194 dib_header
->planes
!= 1 || dib_header
->bpp
!= 24 ||
195 dib_header
->compression
!= 0)
198 bmp_width
= dib_header
->width
;
199 bmp_height
= abs(dib_header
->height
);
200 bmp_pitch
= round_up(3 * bmp_width
, 4);
202 if ((file_header
->bitmap_offset
+ bmp_pitch
* bmp_height
) >
206 if ((bgrt_tab
.image_offset_x
+ bmp_width
) > si
->lfb_width
||
207 (bgrt_tab
.image_offset_y
+ bmp_height
) > si
->lfb_height
)
210 if (!efifb_bgrt_sanity_check(si
, bmp_width
))
213 pr_info("efifb: showing boot graphics\n");
215 for (y
= 0; y
< si
->lfb_height
; y
++, dst
+= si
->lfb_linelength
) {
216 /* Only background? */
217 if (y
< bgrt_tab
.image_offset_y
||
218 y
>= (bgrt_tab
.image_offset_y
+ bmp_height
)) {
219 memset(dst
, 0, 4 * si
->lfb_width
);
223 src_y
= y
- bgrt_tab
.image_offset_y
;
224 /* Positive header height means upside down row order */
225 if (dib_header
->height
> 0)
226 src_y
= (bmp_height
- 1) - src_y
;
228 memset(dst
, 0, bgrt_tab
.image_offset_x
* 4);
229 dst_x
= bgrt_tab
.image_offset_x
;
230 efifb_copy_bmp(bgrt_image
+ file_header
->bitmap_offset
+
232 (u32
*)dst
+ dst_x
, bmp_width
, si
);
234 memset((u32
*)dst
+ dst_x
, 0, (si
->lfb_width
- dst_x
) * 4);
237 memunmap(bgrt_image
);
241 memunmap(bgrt_image
);
242 pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n");
245 static inline void efifb_show_boot_graphics(struct fb_info
*info
, const struct screen_info
*si
)
250 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
251 * of unregister_framebuffer() or fb_release(). Do any cleanup here.
253 static void efifb_destroy(struct fb_info
*info
)
255 struct efifb_par
*par
= info
->par
;
257 if (info
->screen_base
) {
258 if (mem_flags
& (EFI_MEMORY_UC
| EFI_MEMORY_WC
))
259 iounmap(info
->screen_base
);
261 memunmap(info
->screen_base
);
264 if (request_mem_succeeded
)
265 release_mem_region(par
->base
, par
->size
);
266 fb_dealloc_cmap(&info
->cmap
);
268 framebuffer_release(info
);
271 static const struct fb_ops efifb_ops
= {
272 .owner
= THIS_MODULE
,
273 FB_DEFAULT_IOMEM_OPS
,
274 .fb_destroy
= efifb_destroy
,
275 .fb_setcolreg
= efifb_setcolreg
,
278 static int efifb_setup(struct screen_info
*si
, char *options
)
282 if (options
&& *options
) {
283 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
284 if (!*this_opt
) continue;
286 efifb_setup_from_dmi(si
, this_opt
);
288 if (!strncmp(this_opt
, "base:", 5))
289 si
->lfb_base
= simple_strtoul(this_opt
+5, NULL
, 0);
290 else if (!strncmp(this_opt
, "stride:", 7))
291 si
->lfb_linelength
= simple_strtoul(this_opt
+7, NULL
, 0) * 4;
292 else if (!strncmp(this_opt
, "height:", 7))
293 si
->lfb_height
= simple_strtoul(this_opt
+7, NULL
, 0);
294 else if (!strncmp(this_opt
, "width:", 6))
295 si
->lfb_width
= simple_strtoul(this_opt
+6, NULL
, 0);
296 else if (!strcmp(this_opt
, "nowc"))
297 mem_flags
&= ~EFI_MEMORY_WC
;
298 else if (!strcmp(this_opt
, "nobgrt"))
306 static inline bool fb_base_is_valid(struct screen_info
*si
)
311 if (!(si
->capabilities
& VIDEO_CAPABILITY_64BIT_BASE
))
314 if (si
->ext_lfb_base
)
320 #define efifb_attr_decl(name, fmt) \
321 static ssize_t name##_show(struct device *dev, \
322 struct device_attribute *attr, \
325 struct screen_info *si = dev_get_drvdata(dev); \
328 return sprintf(buf, fmt "\n", (si->lfb_##name)); \
330 static DEVICE_ATTR_RO(name)
332 efifb_attr_decl(base
, "0x%x");
333 efifb_attr_decl(linelength
, "%u");
334 efifb_attr_decl(height
, "%u");
335 efifb_attr_decl(width
, "%u");
336 efifb_attr_decl(depth
, "%u");
338 static struct attribute
*efifb_attrs
[] = {
340 &dev_attr_linelength
.attr
,
341 &dev_attr_width
.attr
,
342 &dev_attr_height
.attr
,
343 &dev_attr_depth
.attr
,
346 ATTRIBUTE_GROUPS(efifb
);
348 static int efifb_probe(struct platform_device
*dev
)
350 struct screen_info
*si
;
351 struct fb_info
*info
;
352 struct efifb_par
*par
;
353 int err
, orientation
;
354 unsigned int size_vmode
;
355 unsigned int size_remap
;
356 unsigned int size_total
;
358 efi_memory_desc_t md
;
361 * If we fail probing the device, the kernel might try a different
362 * driver. We get a copy of the attached screen_info, so that we can
363 * modify its values without affecting later drivers.
365 si
= dev_get_platdata(&dev
->dev
);
368 si
= devm_kmemdup(&dev
->dev
, si
, sizeof(*si
), GFP_KERNEL
);
372 dev_set_drvdata(&dev
->dev
, si
);
374 if (si
->orig_video_isVGA
!= VIDEO_TYPE_EFI
)
377 if (fb_get_options("efifb", &option
))
379 efifb_setup(si
, option
);
381 /* We don't get linelength from UGA Draw Protocol, only from
382 * EFI Graphics Protocol. So if it's not in DMI, and it's not
383 * passed in from the user, we really can't use the framebuffer.
385 if (!si
->lfb_linelength
)
392 if (!fb_base_is_valid(si
)) {
393 printk(KERN_DEBUG
"efifb: invalid framebuffer address\n");
396 printk(KERN_INFO
"efifb: probing for efifb\n");
398 /* just assume they're all unset if any are */
399 if (!si
->blue_size
) {
410 efifb_fix
.smem_start
= __screen_info_lfb_base(si
);
412 efifb_defined
.bits_per_pixel
= si
->lfb_depth
;
413 efifb_defined
.xres
= si
->lfb_width
;
414 efifb_defined
.yres
= si
->lfb_height
;
415 efifb_fix
.line_length
= si
->lfb_linelength
;
417 /* size_vmode -- that is the amount of memory needed for the
418 * used video mode, i.e. the minimum amount of
420 size_vmode
= efifb_defined
.yres
* efifb_fix
.line_length
;
422 /* size_total -- all video memory we have. Used for
423 * entries, ressource allocation and bounds
425 size_total
= si
->lfb_size
;
426 if (size_total
< size_vmode
)
427 size_total
= size_vmode
;
429 /* size_remap -- the amount of video memory we are going to
430 * use for efifb. With modern cards it is no
431 * option to simply use size_total as that
432 * wastes plenty of kernel address space. */
433 size_remap
= size_vmode
* 2;
434 if (size_remap
> size_total
)
435 size_remap
= size_total
;
436 if (size_remap
% PAGE_SIZE
)
437 size_remap
+= PAGE_SIZE
- (size_remap
% PAGE_SIZE
);
438 efifb_fix
.smem_len
= size_remap
;
440 if (request_mem_region(efifb_fix
.smem_start
, size_remap
, "efifb")) {
441 request_mem_succeeded
= true;
443 /* We cannot make this fatal. Sometimes this comes from magic
444 spaces our resource handlers simply don't know about */
445 pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
446 efifb_fix
.smem_start
);
449 info
= framebuffer_alloc(sizeof(*par
), &dev
->dev
);
452 goto err_release_mem
;
455 info
->pseudo_palette
= par
->pseudo_palette
;
457 par
->base
= efifb_fix
.smem_start
;
458 par
->size
= size_remap
;
460 if (efi_enabled(EFI_MEMMAP
) &&
461 !efi_mem_desc_lookup(efifb_fix
.smem_start
, &md
)) {
462 if ((efifb_fix
.smem_start
+ efifb_fix
.smem_len
) >
463 (md
.phys_addr
+ (md
.num_pages
<< EFI_PAGE_SHIFT
))) {
464 pr_err("efifb: video memory @ 0x%lx spans multiple EFI memory regions\n",
465 efifb_fix
.smem_start
);
470 * If the UEFI memory map covers the efifb region, we may only
471 * remap it using the attributes the memory map prescribes.
473 md
.attribute
&= EFI_MEMORY_UC
| EFI_MEMORY_WC
|
474 EFI_MEMORY_WT
| EFI_MEMORY_WB
;
476 mem_flags
|= EFI_MEMORY_WT
| EFI_MEMORY_WB
;
477 mem_flags
&= md
.attribute
;
480 if (mem_flags
& EFI_MEMORY_WC
)
481 info
->screen_base
= ioremap_wc(efifb_fix
.smem_start
,
483 else if (mem_flags
& EFI_MEMORY_UC
)
484 info
->screen_base
= ioremap(efifb_fix
.smem_start
,
486 else if (mem_flags
& EFI_MEMORY_WT
)
487 info
->screen_base
= memremap(efifb_fix
.smem_start
,
488 efifb_fix
.smem_len
, MEMREMAP_WT
);
489 else if (mem_flags
& EFI_MEMORY_WB
)
490 info
->screen_base
= memremap(efifb_fix
.smem_start
,
491 efifb_fix
.smem_len
, MEMREMAP_WB
);
492 if (!info
->screen_base
) {
493 pr_err("efifb: abort, cannot remap video memory 0x%x @ 0x%lx\n",
494 efifb_fix
.smem_len
, efifb_fix
.smem_start
);
499 efifb_show_boot_graphics(info
, si
);
501 pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
502 efifb_fix
.smem_start
, size_remap
/1024, size_total
/1024);
503 pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
504 efifb_defined
.xres
, efifb_defined
.yres
,
505 efifb_defined
.bits_per_pixel
, efifb_fix
.line_length
,
508 efifb_defined
.xres_virtual
= efifb_defined
.xres
;
509 efifb_defined
.yres_virtual
= efifb_fix
.smem_len
/
510 efifb_fix
.line_length
;
511 pr_info("efifb: scrolling: redraw\n");
512 efifb_defined
.yres_virtual
= efifb_defined
.yres
;
514 /* some dummy values for timing to make fbset happy */
515 efifb_defined
.pixclock
= 10000000 / efifb_defined
.xres
*
516 1000 / efifb_defined
.yres
;
517 efifb_defined
.left_margin
= (efifb_defined
.xres
/ 8) & 0xf8;
518 efifb_defined
.hsync_len
= (efifb_defined
.xres
/ 8) & 0xf8;
520 efifb_defined
.red
.offset
= si
->red_pos
;
521 efifb_defined
.red
.length
= si
->red_size
;
522 efifb_defined
.green
.offset
= si
->green_pos
;
523 efifb_defined
.green
.length
= si
->green_size
;
524 efifb_defined
.blue
.offset
= si
->blue_pos
;
525 efifb_defined
.blue
.length
= si
->blue_size
;
526 efifb_defined
.transp
.offset
= si
->rsvd_pos
;
527 efifb_defined
.transp
.length
= si
->rsvd_size
;
529 pr_info("efifb: %s: "
530 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
541 efifb_fix
.ypanstep
= 0;
542 efifb_fix
.ywrapstep
= 0;
544 info
->fbops
= &efifb_ops
;
545 info
->var
= efifb_defined
;
546 info
->fix
= efifb_fix
;
548 orientation
= drm_get_panel_orientation_quirk(efifb_defined
.xres
,
550 switch (orientation
) {
552 info
->fbcon_rotate_hint
= FB_ROTATE_UR
;
554 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP
:
555 info
->fbcon_rotate_hint
= FB_ROTATE_UD
;
557 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP
:
558 info
->fbcon_rotate_hint
= FB_ROTATE_CCW
;
560 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP
:
561 info
->fbcon_rotate_hint
= FB_ROTATE_CW
;
565 err
= fb_alloc_cmap(&info
->cmap
, 256, 0);
567 pr_err("efifb: cannot allocate colormap\n");
571 err
= devm_aperture_acquire_for_platform_device(dev
, par
->base
, par
->size
);
573 pr_err("efifb: cannot acquire aperture\n");
574 goto err_fb_dealloc_cmap
;
576 err
= devm_register_framebuffer(&dev
->dev
, info
);
578 pr_err("efifb: cannot register framebuffer\n");
579 goto err_fb_dealloc_cmap
;
581 fb_info(info
, "%s frame buffer device\n", info
->fix
.id
);
585 fb_dealloc_cmap(&info
->cmap
);
587 if (mem_flags
& (EFI_MEMORY_UC
| EFI_MEMORY_WC
))
588 iounmap(info
->screen_base
);
590 memunmap(info
->screen_base
);
592 framebuffer_release(info
);
594 if (request_mem_succeeded
)
595 release_mem_region(efifb_fix
.smem_start
, size_total
);
599 static struct platform_driver efifb_driver
= {
601 .name
= "efi-framebuffer",
602 .dev_groups
= efifb_groups
,
604 .probe
= efifb_probe
,
607 builtin_platform_driver(efifb_driver
);