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 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> 15 #include <linux/fb.h> 16 #include <linux/platform_device.h> 17 #include <linux/printk.h> 18 #include <linux/screen_info.h> 19 #include <video/vga.h> 20 #include <asm/efi.h> 21 #include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */ 22 #include <drm/drm_connector.h> /* For DRM_MODE_PANEL_ORIENTATION_* */ 23 24 struct bmp_file_header { 25 u16 id; 26 u32 file_size; 27 u32 reserved; 28 u32 bitmap_offset; 29 } __packed; 30 31 struct bmp_dib_header { 32 u32 dib_header_size; 33 s32 width; 34 s32 height; 35 u16 planes; 36 u16 bpp; 37 u32 compression; 38 u32 bitmap_size; 39 u32 horz_resolution; 40 u32 vert_resolution; 41 u32 colors_used; 42 u32 colors_important; 43 } __packed; 44 45 static bool use_bgrt = true; 46 static bool request_mem_succeeded = false; 47 static u64 mem_flags = EFI_MEMORY_WC | EFI_MEMORY_UC; 48 49 struct efifb_par { 50 u32 pseudo_palette[16]; 51 resource_size_t base; 52 resource_size_t size; 53 }; 54 55 static struct fb_var_screeninfo efifb_defined = { 56 .activate = FB_ACTIVATE_NOW, 57 .height = -1, 58 .width = -1, 59 .right_margin = 32, 60 .upper_margin = 16, 61 .lower_margin = 4, 62 .vsync_len = 4, 63 .vmode = FB_VMODE_NONINTERLACED, 64 }; 65 66 static struct fb_fix_screeninfo efifb_fix = { 67 .id = "EFI VGA", 68 .type = FB_TYPE_PACKED_PIXELS, 69 .accel = FB_ACCEL_NONE, 70 .visual = FB_VISUAL_TRUECOLOR, 71 }; 72 73 static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green, 74 unsigned blue, unsigned transp, 75 struct fb_info *info) 76 { 77 /* 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. 82 */ 83 84 if (regno >= info->cmap.len) 85 return 1; 86 87 if (regno < 16) { 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); 95 } 96 return 0; 97 } 98 99 /* 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. 104 */ 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) 108 { 109 u8 r, g, b; 110 111 while (width--) { 112 b = *src++; 113 g = *src++; 114 r = *src++; 115 *dst++ = (r << si->red_pos) | 116 (g << si->green_pos) | 117 (b << si->blue_pos); 118 } 119 } 120 121 #ifdef CONFIG_X86 122 /* 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. 128 */ 129 static bool efifb_bgrt_sanity_check(const struct screen_info *si, u32 bmp_width) 130 { 131 /* 132 * All x86 firmwares horizontally center the image (the yoffset 133 * calculations differ between boards, but xoffset is predictable). 134 */ 135 u32 expected_xoffset = (si->lfb_width - bmp_width) / 2; 136 137 return bgrt_tab.image_offset_x == expected_xoffset; 138 } 139 #else 140 static bool efifb_bgrt_sanity_check(const struct screen_info *si, u32 bmp_width) 141 { 142 return true; 143 } 144 #endif 145 146 static void efifb_show_boot_graphics(struct fb_info *info, const struct screen_info *si) 147 { 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; 153 154 if (!use_bgrt) 155 return; 156 157 if (!bgrt_tab.image_address) { 158 pr_info("efifb: No BGRT, not showing boot graphics\n"); 159 return; 160 } 161 162 if (bgrt_tab.status & 0x06) { 163 pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n"); 164 return; 165 } 166 167 /* Avoid flashing the logo if we're going to print std probe messages */ 168 if (console_loglevel > CONSOLE_LOGLEVEL_QUIET) 169 return; 170 171 /* bgrt_tab.status is unreliable, so we don't check it */ 172 173 if (si->lfb_depth != 32) { 174 pr_info("efifb: not 32 bits, not showing boot graphics\n"); 175 return; 176 } 177 178 bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size, 179 MEMREMAP_WB); 180 if (!bgrt_image) { 181 pr_warn("efifb: Ignoring BGRT: failed to map image memory\n"); 182 return; 183 } 184 185 if (bgrt_image_size < (sizeof(*file_header) + sizeof(*dib_header))) 186 goto error; 187 188 file_header = bgrt_image; 189 if (file_header->id != 0x4d42 || file_header->reserved != 0) 190 goto error; 191 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) 196 goto error; 197 198 bmp_width = dib_header->width; 199 bmp_height = abs(dib_header->height); 200 bmp_pitch = round_up(3 * bmp_width, 4); 201 202 if ((file_header->bitmap_offset + bmp_pitch * bmp_height) > 203 bgrt_image_size) 204 goto error; 205 206 if ((bgrt_tab.image_offset_x + bmp_width) > si->lfb_width || 207 (bgrt_tab.image_offset_y + bmp_height) > si->lfb_height) 208 goto error; 209 210 if (!efifb_bgrt_sanity_check(si, bmp_width)) 211 goto error; 212 213 pr_info("efifb: showing boot graphics\n"); 214 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); 220 continue; 221 } 222 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; 227 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 + 231 src_y * bmp_pitch, 232 (u32 *)dst + dst_x, bmp_width, si); 233 dst_x += bmp_width; 234 memset((u32 *)dst + dst_x, 0, (si->lfb_width - dst_x) * 4); 235 } 236 237 memunmap(bgrt_image); 238 return; 239 240 error: 241 memunmap(bgrt_image); 242 pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n"); 243 } 244 #else 245 static inline void efifb_show_boot_graphics(struct fb_info *info, const struct screen_info *si) 246 { } 247 #endif 248 249 /* 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. 252 */ 253 static void efifb_destroy(struct fb_info *info) 254 { 255 struct efifb_par *par = info->par; 256 257 if (info->screen_base) { 258 if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC)) 259 iounmap(info->screen_base); 260 else 261 memunmap(info->screen_base); 262 } 263 264 if (request_mem_succeeded) 265 release_mem_region(par->base, par->size); 266 fb_dealloc_cmap(&info->cmap); 267 268 framebuffer_release(info); 269 } 270 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, 276 }; 277 278 static int efifb_setup(struct screen_info *si, char *options) 279 { 280 char *this_opt; 281 282 if (options && *options) { 283 while ((this_opt = strsep(&options, ",")) != NULL) { 284 if (!*this_opt) continue; 285 286 efifb_setup_from_dmi(si, this_opt); 287 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")) 299 use_bgrt = false; 300 } 301 } 302 303 return 0; 304 } 305 306 static inline bool fb_base_is_valid(struct screen_info *si) 307 { 308 if (si->lfb_base) 309 return true; 310 311 if (!(si->capabilities & VIDEO_CAPABILITY_64BIT_BASE)) 312 return false; 313 314 if (si->ext_lfb_base) 315 return true; 316 317 return false; 318 } 319 320 #define efifb_attr_decl(name, fmt) \ 321 static ssize_t name##_show(struct device *dev, \ 322 struct device_attribute *attr, \ 323 char *buf) \ 324 { \ 325 struct screen_info *si = dev_get_drvdata(dev); \ 326 if (!si) \ 327 return -ENODEV; \ 328 return sprintf(buf, fmt "\n", (si->lfb_##name)); \ 329 } \ 330 static DEVICE_ATTR_RO(name) 331 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"); 337 338 static struct attribute *efifb_attrs[] = { 339 &dev_attr_base.attr, 340 &dev_attr_linelength.attr, 341 &dev_attr_width.attr, 342 &dev_attr_height.attr, 343 &dev_attr_depth.attr, 344 NULL 345 }; 346 ATTRIBUTE_GROUPS(efifb); 347 348 static int efifb_probe(struct platform_device *dev) 349 { 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; 357 char *option = NULL; 358 efi_memory_desc_t md; 359 360 /* 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. 364 */ 365 si = dev_get_platdata(&dev->dev); 366 if (!si) 367 return -ENODEV; 368 si = devm_kmemdup(&dev->dev, si, sizeof(*si), GFP_KERNEL); 369 if (!si) 370 return -ENOMEM; 371 372 dev_set_drvdata(&dev->dev, si); 373 374 if (si->orig_video_isVGA != VIDEO_TYPE_EFI) 375 return -ENODEV; 376 377 if (fb_get_options("efifb", &option)) 378 return -ENODEV; 379 efifb_setup(si, option); 380 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. 384 */ 385 if (!si->lfb_linelength) 386 return -ENODEV; 387 388 if (!si->lfb_depth) 389 si->lfb_depth = 32; 390 if (!si->pages) 391 si->pages = 1; 392 if (!fb_base_is_valid(si)) { 393 printk(KERN_DEBUG "efifb: invalid framebuffer address\n"); 394 return -ENODEV; 395 } 396 printk(KERN_INFO "efifb: probing for efifb\n"); 397 398 /* just assume they're all unset if any are */ 399 if (!si->blue_size) { 400 si->blue_size = 8; 401 si->blue_pos = 0; 402 si->green_size = 8; 403 si->green_pos = 8; 404 si->red_size = 8; 405 si->red_pos = 16; 406 si->rsvd_size = 8; 407 si->rsvd_pos = 24; 408 } 409 410 efifb_fix.smem_start = __screen_info_lfb_base(si); 411 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; 416 417 /* size_vmode -- that is the amount of memory needed for the 418 * used video mode, i.e. the minimum amount of 419 * memory we need. */ 420 size_vmode = efifb_defined.yres * efifb_fix.line_length; 421 422 /* size_total -- all video memory we have. Used for 423 * entries, ressource allocation and bounds 424 * checking. */ 425 size_total = si->lfb_size; 426 if (size_total < size_vmode) 427 size_total = size_vmode; 428 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; 439 440 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) { 441 request_mem_succeeded = true; 442 } else { 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); 447 } 448 449 info = framebuffer_alloc(sizeof(*par), &dev->dev); 450 if (!info) { 451 err = -ENOMEM; 452 goto err_release_mem; 453 } 454 par = info->par; 455 info->pseudo_palette = par->pseudo_palette; 456 457 par->base = efifb_fix.smem_start; 458 par->size = size_remap; 459 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); 466 err = -EIO; 467 goto err_release_fb; 468 } 469 /* 470 * If the UEFI memory map covers the efifb region, we may only 471 * remap it using the attributes the memory map prescribes. 472 */ 473 md.attribute &= EFI_MEMORY_UC | EFI_MEMORY_WC | 474 EFI_MEMORY_WT | EFI_MEMORY_WB; 475 if (md.attribute) { 476 mem_flags |= EFI_MEMORY_WT | EFI_MEMORY_WB; 477 mem_flags &= md.attribute; 478 } 479 } 480 if (mem_flags & EFI_MEMORY_WC) 481 info->screen_base = ioremap_wc(efifb_fix.smem_start, 482 efifb_fix.smem_len); 483 else if (mem_flags & EFI_MEMORY_UC) 484 info->screen_base = ioremap(efifb_fix.smem_start, 485 efifb_fix.smem_len); 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); 495 err = -EIO; 496 goto err_release_fb; 497 } 498 499 efifb_show_boot_graphics(info, si); 500 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, 506 si->pages); 507 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; 513 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; 519 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; 528 529 pr_info("efifb: %s: " 530 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n", 531 "Truecolor", 532 si->rsvd_size, 533 si->red_size, 534 si->green_size, 535 si->blue_size, 536 si->rsvd_pos, 537 si->red_pos, 538 si->green_pos, 539 si->blue_pos); 540 541 efifb_fix.ypanstep = 0; 542 efifb_fix.ywrapstep = 0; 543 544 info->fbops = &efifb_ops; 545 info->var = efifb_defined; 546 info->fix = efifb_fix; 547 548 orientation = drm_get_panel_orientation_quirk(efifb_defined.xres, 549 efifb_defined.yres); 550 switch (orientation) { 551 default: 552 info->fbcon_rotate_hint = FB_ROTATE_UR; 553 break; 554 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP: 555 info->fbcon_rotate_hint = FB_ROTATE_UD; 556 break; 557 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP: 558 info->fbcon_rotate_hint = FB_ROTATE_CCW; 559 break; 560 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP: 561 info->fbcon_rotate_hint = FB_ROTATE_CW; 562 break; 563 } 564 565 err = fb_alloc_cmap(&info->cmap, 256, 0); 566 if (err < 0) { 567 pr_err("efifb: cannot allocate colormap\n"); 568 goto err_unmap; 569 } 570 571 err = devm_aperture_acquire_for_platform_device(dev, par->base, par->size); 572 if (err) { 573 pr_err("efifb: cannot acquire aperture\n"); 574 goto err_fb_dealloc_cmap; 575 } 576 err = devm_register_framebuffer(&dev->dev, info); 577 if (err < 0) { 578 pr_err("efifb: cannot register framebuffer\n"); 579 goto err_fb_dealloc_cmap; 580 } 581 fb_info(info, "%s frame buffer device\n", info->fix.id); 582 return 0; 583 584 err_fb_dealloc_cmap: 585 fb_dealloc_cmap(&info->cmap); 586 err_unmap: 587 if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC)) 588 iounmap(info->screen_base); 589 else 590 memunmap(info->screen_base); 591 err_release_fb: 592 framebuffer_release(info); 593 err_release_mem: 594 if (request_mem_succeeded) 595 release_mem_region(efifb_fix.smem_start, size_total); 596 return err; 597 } 598 599 static struct platform_driver efifb_driver = { 600 .driver = { 601 .name = "efi-framebuffer", 602 .dev_groups = efifb_groups, 603 }, 604 .probe = efifb_probe, 605 }; 606 607 builtin_platform_driver(efifb_driver); 608