xref: /openbmc/linux/drivers/video/fbdev/efifb.c (revision e3b39825)
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/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/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>
21 #include <asm/efi.h>
22 #include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */
23 #include <drm/drm_connector.h>  /* For DRM_MODE_PANEL_ORIENTATION_* */
24 
25 struct bmp_file_header {
26 	u16 id;
27 	u32 file_size;
28 	u32 reserved;
29 	u32 bitmap_offset;
30 } __packed;
31 
32 struct bmp_dib_header {
33 	u32 dib_header_size;
34 	s32 width;
35 	s32 height;
36 	u16 planes;
37 	u16 bpp;
38 	u32 compression;
39 	u32 bitmap_size;
40 	u32 horz_resolution;
41 	u32 vert_resolution;
42 	u32 colors_used;
43 	u32 colors_important;
44 } __packed;
45 
46 static bool use_bgrt = true;
47 static bool request_mem_succeeded = false;
48 static u64 mem_flags = EFI_MEMORY_WC | EFI_MEMORY_UC;
49 
50 static struct pci_dev *efifb_pci_dev;	/* dev with BAR covering the efifb */
51 
52 static struct fb_var_screeninfo efifb_defined = {
53 	.activate		= FB_ACTIVATE_NOW,
54 	.height			= -1,
55 	.width			= -1,
56 	.right_margin		= 32,
57 	.upper_margin		= 16,
58 	.lower_margin		= 4,
59 	.vsync_len		= 4,
60 	.vmode			= FB_VMODE_NONINTERLACED,
61 };
62 
63 static struct fb_fix_screeninfo efifb_fix = {
64 	.id			= "EFI VGA",
65 	.type			= FB_TYPE_PACKED_PIXELS,
66 	.accel			= FB_ACCEL_NONE,
67 	.visual			= FB_VISUAL_TRUECOLOR,
68 };
69 
70 static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
71 			   unsigned blue, unsigned transp,
72 			   struct fb_info *info)
73 {
74 	/*
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.
79 	 */
80 
81 	if (regno >= info->cmap.len)
82 		return 1;
83 
84 	if (regno < 16) {
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);
92 	}
93 	return 0;
94 }
95 
96 /*
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.
101  */
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)
105 {
106 	u8 r, g, b;
107 
108 	while (width--) {
109 		b = *src++;
110 		g = *src++;
111 		r = *src++;
112 		*dst++ = (r << si->red_pos)   |
113 			 (g << si->green_pos) |
114 			 (b << si->blue_pos);
115 	}
116 }
117 
118 #ifdef CONFIG_X86
119 /*
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.
125  */
126 static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width)
127 {
128 	/*
129 	 * All x86 firmwares horizontally center the image (the yoffset
130 	 * calculations differ between boards, but xoffset is predictable).
131 	 */
132 	u32 expected_xoffset = (si->lfb_width - bmp_width) / 2;
133 
134 	return bgrt_tab.image_offset_x == expected_xoffset;
135 }
136 #else
137 static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width)
138 {
139 	return true;
140 }
141 #endif
142 
143 static void efifb_show_boot_graphics(struct fb_info *info)
144 {
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;
151 
152 	if (!use_bgrt)
153 		return;
154 
155 	if (!bgrt_tab.image_address) {
156 		pr_info("efifb: No BGRT, not showing boot graphics\n");
157 		return;
158 	}
159 
160 	if (bgrt_tab.status & 0x06) {
161 		pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n");
162 		return;
163 	}
164 
165 	/* Avoid flashing the logo if we're going to print std probe messages */
166 	if (console_loglevel > CONSOLE_LOGLEVEL_QUIET)
167 		return;
168 
169 	/* bgrt_tab.status is unreliable, so we don't check it */
170 
171 	if (si->lfb_depth != 32) {
172 		pr_info("efifb: not 32 bits, not showing boot graphics\n");
173 		return;
174 	}
175 
176 	bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
177 			      MEMREMAP_WB);
178 	if (!bgrt_image) {
179 		pr_warn("efifb: Ignoring BGRT: failed to map image memory\n");
180 		return;
181 	}
182 
183 	if (bgrt_image_size < (sizeof(*file_header) + sizeof(*dib_header)))
184 		goto error;
185 
186 	file_header = bgrt_image;
187 	if (file_header->id != 0x4d42 || file_header->reserved != 0)
188 		goto error;
189 
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)
194 		goto error;
195 
196 	bmp_width = dib_header->width;
197 	bmp_height = abs(dib_header->height);
198 	bmp_pitch = round_up(3 * bmp_width, 4);
199 
200 	if ((file_header->bitmap_offset + bmp_pitch * bmp_height) >
201 				bgrt_image_size)
202 		goto error;
203 
204 	if ((bgrt_tab.image_offset_x + bmp_width) > si->lfb_width ||
205 	    (bgrt_tab.image_offset_y + bmp_height) > si->lfb_height)
206 		goto error;
207 
208 	if (!efifb_bgrt_sanity_check(si, bmp_width))
209 		goto error;
210 
211 	pr_info("efifb: showing boot graphics\n");
212 
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);
218 			continue;
219 		}
220 
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;
225 
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 +
229 					    src_y * bmp_pitch,
230 			       (u32 *)dst + dst_x, bmp_width, si);
231 		dst_x += bmp_width;
232 		memset((u32 *)dst + dst_x, 0, (si->lfb_width - dst_x) * 4);
233 	}
234 
235 	memunmap(bgrt_image);
236 	return;
237 
238 error:
239 	memunmap(bgrt_image);
240 	pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n");
241 }
242 #else
243 static inline void efifb_show_boot_graphics(struct fb_info *info) {}
244 #endif
245 
246 static void efifb_destroy(struct fb_info *info)
247 {
248 	if (efifb_pci_dev)
249 		pm_runtime_put(&efifb_pci_dev->dev);
250 
251 	if (info->screen_base) {
252 		if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
253 			iounmap(info->screen_base);
254 		else
255 			memunmap(info->screen_base);
256 	}
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);
261 }
262 
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,
270 };
271 
272 static int efifb_setup(char *options)
273 {
274 	char *this_opt;
275 
276 	if (options && *options) {
277 		while ((this_opt = strsep(&options, ",")) != NULL) {
278 			if (!*this_opt) continue;
279 
280 			efifb_setup_from_dmi(&screen_info, this_opt);
281 
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"))
293 				use_bgrt = false;
294 		}
295 	}
296 
297 	return 0;
298 }
299 
300 static inline bool fb_base_is_valid(void)
301 {
302 	if (screen_info.lfb_base)
303 		return true;
304 
305 	if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE))
306 		return false;
307 
308 	if (screen_info.ext_lfb_base)
309 		return true;
310 
311 	return false;
312 }
313 
314 #define efifb_attr_decl(name, fmt)					\
315 static ssize_t name##_show(struct device *dev,				\
316 			   struct device_attribute *attr,		\
317 			   char *buf)					\
318 {									\
319 	return sprintf(buf, fmt "\n", (screen_info.lfb_##name));	\
320 }									\
321 static DEVICE_ATTR_RO(name)
322 
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");
328 
329 static struct attribute *efifb_attrs[] = {
330 	&dev_attr_base.attr,
331 	&dev_attr_linelength.attr,
332 	&dev_attr_width.attr,
333 	&dev_attr_height.attr,
334 	&dev_attr_depth.attr,
335 	NULL
336 };
337 ATTRIBUTE_GROUPS(efifb);
338 
339 static bool pci_dev_disabled;	/* FB base matches BAR of a disabled device */
340 
341 static struct resource *bar_resource;
342 static u64 bar_offset;
343 
344 static int efifb_probe(struct platform_device *dev)
345 {
346 	struct fb_info *info;
347 	int err, orientation;
348 	unsigned int size_vmode;
349 	unsigned int size_remap;
350 	unsigned int size_total;
351 	char *option = NULL;
352 	efi_memory_desc_t md;
353 
354 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
355 		return -ENODEV;
356 
357 	if (fb_get_options("efifb", &option))
358 		return -ENODEV;
359 	efifb_setup(option);
360 
361 	/* We don't get linelength from UGA Draw Protocol, only from
362 	 * EFI Graphics Protocol.  So if it's not in DMI, and it's not
363 	 * passed in from the user, we really can't use the framebuffer.
364 	 */
365 	if (!screen_info.lfb_linelength)
366 		return -ENODEV;
367 
368 	if (!screen_info.lfb_depth)
369 		screen_info.lfb_depth = 32;
370 	if (!screen_info.pages)
371 		screen_info.pages = 1;
372 	if (!fb_base_is_valid()) {
373 		printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
374 		return -ENODEV;
375 	}
376 	printk(KERN_INFO "efifb: probing for efifb\n");
377 
378 	/* just assume they're all unset if any are */
379 	if (!screen_info.blue_size) {
380 		screen_info.blue_size = 8;
381 		screen_info.blue_pos = 0;
382 		screen_info.green_size = 8;
383 		screen_info.green_pos = 8;
384 		screen_info.red_size = 8;
385 		screen_info.red_pos = 16;
386 		screen_info.rsvd_size = 8;
387 		screen_info.rsvd_pos = 24;
388 	}
389 
390 	efifb_fix.smem_start = screen_info.lfb_base;
391 
392 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
393 		u64 ext_lfb_base;
394 
395 		ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
396 		efifb_fix.smem_start |= ext_lfb_base;
397 	}
398 
399 	if (bar_resource &&
400 	    bar_resource->start + bar_offset != efifb_fix.smem_start) {
401 		dev_info(&efifb_pci_dev->dev,
402 			 "BAR has moved, updating efifb address\n");
403 		efifb_fix.smem_start = bar_resource->start + bar_offset;
404 	}
405 
406 	efifb_defined.bits_per_pixel = screen_info.lfb_depth;
407 	efifb_defined.xres = screen_info.lfb_width;
408 	efifb_defined.yres = screen_info.lfb_height;
409 	efifb_fix.line_length = screen_info.lfb_linelength;
410 
411 	/*   size_vmode -- that is the amount of memory needed for the
412 	 *                 used video mode, i.e. the minimum amount of
413 	 *                 memory we need. */
414 	size_vmode = efifb_defined.yres * efifb_fix.line_length;
415 
416 	/*   size_total -- all video memory we have. Used for
417 	 *                 entries, ressource allocation and bounds
418 	 *                 checking. */
419 	size_total = screen_info.lfb_size;
420 	if (size_total < size_vmode)
421 		size_total = size_vmode;
422 
423 	/*   size_remap -- the amount of video memory we are going to
424 	 *                 use for efifb.  With modern cards it is no
425 	 *                 option to simply use size_total as that
426 	 *                 wastes plenty of kernel address space. */
427 	size_remap  = size_vmode * 2;
428 	if (size_remap > size_total)
429 		size_remap = size_total;
430 	if (size_remap % PAGE_SIZE)
431 		size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
432 	efifb_fix.smem_len = size_remap;
433 
434 	if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
435 		request_mem_succeeded = true;
436 	} else {
437 		/* We cannot make this fatal. Sometimes this comes from magic
438 		   spaces our resource handlers simply don't know about */
439 		pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
440 			efifb_fix.smem_start);
441 	}
442 
443 	info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
444 	if (!info) {
445 		err = -ENOMEM;
446 		goto err_release_mem;
447 	}
448 	platform_set_drvdata(dev, info);
449 	info->pseudo_palette = info->par;
450 	info->par = NULL;
451 
452 	info->apertures = alloc_apertures(1);
453 	if (!info->apertures) {
454 		err = -ENOMEM;
455 		goto err_release_fb;
456 	}
457 	info->apertures->ranges[0].base = efifb_fix.smem_start;
458 	info->apertures->ranges[0].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);
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 	       screen_info.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    = screen_info.red_pos;
521 	efifb_defined.red.length    = screen_info.red_size;
522 	efifb_defined.green.offset  = screen_info.green_pos;
523 	efifb_defined.green.length  = screen_info.green_size;
524 	efifb_defined.blue.offset   = screen_info.blue_pos;
525 	efifb_defined.blue.length   = screen_info.blue_size;
526 	efifb_defined.transp.offset = screen_info.rsvd_pos;
527 	efifb_defined.transp.length = screen_info.rsvd_size;
528 
529 	pr_info("efifb: %s: "
530 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
531 	       "Truecolor",
532 	       screen_info.rsvd_size,
533 	       screen_info.red_size,
534 	       screen_info.green_size,
535 	       screen_info.blue_size,
536 	       screen_info.rsvd_pos,
537 	       screen_info.red_pos,
538 	       screen_info.green_pos,
539 	       screen_info.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 	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
548 
549 	orientation = drm_get_panel_orientation_quirk(efifb_defined.xres,
550 						      efifb_defined.yres);
551 	switch (orientation) {
552 	default:
553 		info->fbcon_rotate_hint = FB_ROTATE_UR;
554 		break;
555 	case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
556 		info->fbcon_rotate_hint = FB_ROTATE_UD;
557 		break;
558 	case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
559 		info->fbcon_rotate_hint = FB_ROTATE_CCW;
560 		break;
561 	case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
562 		info->fbcon_rotate_hint = FB_ROTATE_CW;
563 		break;
564 	}
565 
566 	err = sysfs_create_groups(&dev->dev.kobj, efifb_groups);
567 	if (err) {
568 		pr_err("efifb: cannot add sysfs attrs\n");
569 		goto err_unmap;
570 	}
571 	err = fb_alloc_cmap(&info->cmap, 256, 0);
572 	if (err < 0) {
573 		pr_err("efifb: cannot allocate colormap\n");
574 		goto err_groups;
575 	}
576 
577 	if (efifb_pci_dev)
578 		WARN_ON(pm_runtime_get_sync(&efifb_pci_dev->dev) < 0);
579 
580 	err = register_framebuffer(info);
581 	if (err < 0) {
582 		pr_err("efifb: cannot register framebuffer\n");
583 		goto err_put_rpm_ref;
584 	}
585 	fb_info(info, "%s frame buffer device\n", info->fix.id);
586 	return 0;
587 
588 err_put_rpm_ref:
589 	if (efifb_pci_dev)
590 		pm_runtime_put(&efifb_pci_dev->dev);
591 
592 	fb_dealloc_cmap(&info->cmap);
593 err_groups:
594 	sysfs_remove_groups(&dev->dev.kobj, efifb_groups);
595 err_unmap:
596 	if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
597 		iounmap(info->screen_base);
598 	else
599 		memunmap(info->screen_base);
600 err_release_fb:
601 	framebuffer_release(info);
602 err_release_mem:
603 	if (request_mem_succeeded)
604 		release_mem_region(efifb_fix.smem_start, size_total);
605 	return err;
606 }
607 
608 static int efifb_remove(struct platform_device *pdev)
609 {
610 	struct fb_info *info = platform_get_drvdata(pdev);
611 
612 	unregister_framebuffer(info);
613 	sysfs_remove_groups(&pdev->dev.kobj, efifb_groups);
614 	framebuffer_release(info);
615 
616 	return 0;
617 }
618 
619 static struct platform_driver efifb_driver = {
620 	.driver = {
621 		.name = "efi-framebuffer",
622 	},
623 	.probe = efifb_probe,
624 	.remove = efifb_remove,
625 };
626 
627 builtin_platform_driver(efifb_driver);
628 
629 #if defined(CONFIG_PCI)
630 
631 static void record_efifb_bar_resource(struct pci_dev *dev, int idx, u64 offset)
632 {
633 	u16 word;
634 
635 	efifb_pci_dev = dev;
636 
637 	pci_read_config_word(dev, PCI_COMMAND, &word);
638 	if (!(word & PCI_COMMAND_MEMORY)) {
639 		pci_dev_disabled = true;
640 		dev_err(&dev->dev,
641 			"BAR %d: assigned to efifb but device is disabled!\n",
642 			idx);
643 		return;
644 	}
645 
646 	bar_resource = &dev->resource[idx];
647 	bar_offset = offset;
648 
649 	dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
650 }
651 
652 static void efifb_fixup_resources(struct pci_dev *dev)
653 {
654 	u64 base = screen_info.lfb_base;
655 	u64 size = screen_info.lfb_size;
656 	int i;
657 
658 	if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
659 		return;
660 
661 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
662 		base |= (u64)screen_info.ext_lfb_base << 32;
663 
664 	if (!base)
665 		return;
666 
667 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
668 		struct resource *res = &dev->resource[i];
669 
670 		if (!(res->flags & IORESOURCE_MEM))
671 			continue;
672 
673 		if (res->start <= base && res->end >= base + size - 1) {
674 			record_efifb_bar_resource(dev, i, base - res->start);
675 			break;
676 		}
677 	}
678 }
679 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
680 			       16, efifb_fixup_resources);
681 
682 #endif
683