xref: /linux/drivers/video/fbdev/efifb.c (revision 90393c9b5408cafededdee2f34082c4c3c671901)
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/pci.h>
17 #include <linux/platform_device.h>
18 #include <linux/printk.h>
19 #include <linux/screen_info.h>
20 #include <linux/pm_runtime.h>
21 #include <video/vga.h>
22 #include <asm/efi.h>
23 #include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */
24 #include <drm/drm_connector.h>  /* For DRM_MODE_PANEL_ORIENTATION_* */
25 
26 struct bmp_file_header {
27 	u16 id;
28 	u32 file_size;
29 	u32 reserved;
30 	u32 bitmap_offset;
31 } __packed;
32 
33 struct bmp_dib_header {
34 	u32 dib_header_size;
35 	s32 width;
36 	s32 height;
37 	u16 planes;
38 	u16 bpp;
39 	u32 compression;
40 	u32 bitmap_size;
41 	u32 horz_resolution;
42 	u32 vert_resolution;
43 	u32 colors_used;
44 	u32 colors_important;
45 } __packed;
46 
47 static bool use_bgrt = true;
48 static bool request_mem_succeeded = false;
49 static u64 mem_flags = EFI_MEMORY_WC | EFI_MEMORY_UC;
50 
51 static struct pci_dev *efifb_pci_dev;	/* dev with BAR covering the efifb */
52 
53 struct efifb_par {
54 	u32 pseudo_palette[16];
55 	resource_size_t base;
56 	resource_size_t size;
57 };
58 
59 static struct fb_var_screeninfo efifb_defined = {
60 	.activate		= FB_ACTIVATE_NOW,
61 	.height			= -1,
62 	.width			= -1,
63 	.right_margin		= 32,
64 	.upper_margin		= 16,
65 	.lower_margin		= 4,
66 	.vsync_len		= 4,
67 	.vmode			= FB_VMODE_NONINTERLACED,
68 };
69 
70 static struct fb_fix_screeninfo efifb_fix = {
71 	.id			= "EFI VGA",
72 	.type			= FB_TYPE_PACKED_PIXELS,
73 	.accel			= FB_ACCEL_NONE,
74 	.visual			= FB_VISUAL_TRUECOLOR,
75 };
76 
77 static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
78 			   unsigned blue, unsigned transp,
79 			   struct fb_info *info)
80 {
81 	/*
82 	 *  Set a single color register. The values supplied are
83 	 *  already rounded down to the hardware's capabilities
84 	 *  (according to the entries in the `var' structure). Return
85 	 *  != 0 for invalid regno.
86 	 */
87 
88 	if (regno >= info->cmap.len)
89 		return 1;
90 
91 	if (regno < 16) {
92 		red   >>= 16 - info->var.red.length;
93 		green >>= 16 - info->var.green.length;
94 		blue  >>= 16 - info->var.blue.length;
95 		((u32 *)(info->pseudo_palette))[regno] =
96 			(red   << info->var.red.offset)   |
97 			(green << info->var.green.offset) |
98 			(blue  << info->var.blue.offset);
99 	}
100 	return 0;
101 }
102 
103 /*
104  * If fbcon deffered console takeover is configured, the intent is for the
105  * framebuffer to show the boot graphics (e.g. vendor logo) until there is some
106  * (error) message to display. But the boot graphics may have been destroyed by
107  * e.g. option ROM output, detect this and restore the boot graphics.
108  */
109 #if defined CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER && \
110     defined CONFIG_ACPI_BGRT
111 static void efifb_copy_bmp(u8 *src, u32 *dst, int width, const struct screen_info *si)
112 {
113 	u8 r, g, b;
114 
115 	while (width--) {
116 		b = *src++;
117 		g = *src++;
118 		r = *src++;
119 		*dst++ = (r << si->red_pos)   |
120 			 (g << si->green_pos) |
121 			 (b << si->blue_pos);
122 	}
123 }
124 
125 #ifdef CONFIG_X86
126 /*
127  * On x86 some firmwares use a low non native resolution for the display when
128  * they have shown some text messages. While keeping the bgrt filled with info
129  * for the native resolution. If the bgrt image intended for the native
130  * resolution still fits, it will be displayed very close to the right edge of
131  * the display looking quite bad. This function checks for this.
132  */
133 static bool efifb_bgrt_sanity_check(const struct screen_info *si, u32 bmp_width)
134 {
135 	/*
136 	 * All x86 firmwares horizontally center the image (the yoffset
137 	 * calculations differ between boards, but xoffset is predictable).
138 	 */
139 	u32 expected_xoffset = (si->lfb_width - bmp_width) / 2;
140 
141 	return bgrt_tab.image_offset_x == expected_xoffset;
142 }
143 #else
144 static bool efifb_bgrt_sanity_check(const struct screen_info *si, u32 bmp_width)
145 {
146 	return true;
147 }
148 #endif
149 
150 static void efifb_show_boot_graphics(struct fb_info *info, const struct screen_info *si)
151 {
152 	u32 bmp_width, bmp_height, bmp_pitch, dst_x, y, src_y;
153 	struct bmp_file_header *file_header;
154 	struct bmp_dib_header *dib_header;
155 	void *bgrt_image = NULL;
156 	u8 *dst = info->screen_base;
157 
158 	if (!use_bgrt)
159 		return;
160 
161 	if (!bgrt_tab.image_address) {
162 		pr_info("efifb: No BGRT, not showing boot graphics\n");
163 		return;
164 	}
165 
166 	if (bgrt_tab.status & 0x06) {
167 		pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n");
168 		return;
169 	}
170 
171 	/* Avoid flashing the logo if we're going to print std probe messages */
172 	if (console_loglevel > CONSOLE_LOGLEVEL_QUIET)
173 		return;
174 
175 	/* bgrt_tab.status is unreliable, so we don't check it */
176 
177 	if (si->lfb_depth != 32) {
178 		pr_info("efifb: not 32 bits, not showing boot graphics\n");
179 		return;
180 	}
181 
182 	bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
183 			      MEMREMAP_WB);
184 	if (!bgrt_image) {
185 		pr_warn("efifb: Ignoring BGRT: failed to map image memory\n");
186 		return;
187 	}
188 
189 	if (bgrt_image_size < (sizeof(*file_header) + sizeof(*dib_header)))
190 		goto error;
191 
192 	file_header = bgrt_image;
193 	if (file_header->id != 0x4d42 || file_header->reserved != 0)
194 		goto error;
195 
196 	dib_header = bgrt_image + sizeof(*file_header);
197 	if (dib_header->dib_header_size != 40 || dib_header->width < 0 ||
198 	    dib_header->planes != 1 || dib_header->bpp != 24 ||
199 	    dib_header->compression != 0)
200 		goto error;
201 
202 	bmp_width = dib_header->width;
203 	bmp_height = abs(dib_header->height);
204 	bmp_pitch = round_up(3 * bmp_width, 4);
205 
206 	if ((file_header->bitmap_offset + bmp_pitch * bmp_height) >
207 				bgrt_image_size)
208 		goto error;
209 
210 	if ((bgrt_tab.image_offset_x + bmp_width) > si->lfb_width ||
211 	    (bgrt_tab.image_offset_y + bmp_height) > si->lfb_height)
212 		goto error;
213 
214 	if (!efifb_bgrt_sanity_check(si, bmp_width))
215 		goto error;
216 
217 	pr_info("efifb: showing boot graphics\n");
218 
219 	for (y = 0; y < si->lfb_height; y++, dst += si->lfb_linelength) {
220 		/* Only background? */
221 		if (y < bgrt_tab.image_offset_y ||
222 		    y >= (bgrt_tab.image_offset_y + bmp_height)) {
223 			memset(dst, 0, 4 * si->lfb_width);
224 			continue;
225 		}
226 
227 		src_y = y - bgrt_tab.image_offset_y;
228 		/* Positive header height means upside down row order */
229 		if (dib_header->height > 0)
230 			src_y = (bmp_height - 1) - src_y;
231 
232 		memset(dst, 0, bgrt_tab.image_offset_x * 4);
233 		dst_x = bgrt_tab.image_offset_x;
234 		efifb_copy_bmp(bgrt_image + file_header->bitmap_offset +
235 					    src_y * bmp_pitch,
236 			       (u32 *)dst + dst_x, bmp_width, si);
237 		dst_x += bmp_width;
238 		memset((u32 *)dst + dst_x, 0, (si->lfb_width - dst_x) * 4);
239 	}
240 
241 	memunmap(bgrt_image);
242 	return;
243 
244 error:
245 	memunmap(bgrt_image);
246 	pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n");
247 }
248 #else
249 static inline void efifb_show_boot_graphics(struct fb_info *info, const struct screen_info *si)
250 { }
251 #endif
252 
253 /*
254  * fb_ops.fb_destroy is called by the last put_fb_info() call at the end
255  * of unregister_framebuffer() or fb_release(). Do any cleanup here.
256  */
257 static void efifb_destroy(struct fb_info *info)
258 {
259 	struct efifb_par *par = info->par;
260 
261 	if (efifb_pci_dev)
262 		pm_runtime_put(&efifb_pci_dev->dev);
263 
264 	if (info->screen_base) {
265 		if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
266 			iounmap(info->screen_base);
267 		else
268 			memunmap(info->screen_base);
269 	}
270 
271 	if (request_mem_succeeded)
272 		release_mem_region(par->base, par->size);
273 	fb_dealloc_cmap(&info->cmap);
274 
275 	framebuffer_release(info);
276 }
277 
278 static const struct fb_ops efifb_ops = {
279 	.owner		= THIS_MODULE,
280 	FB_DEFAULT_IOMEM_OPS,
281 	.fb_destroy	= efifb_destroy,
282 	.fb_setcolreg	= efifb_setcolreg,
283 };
284 
285 static int efifb_setup(struct screen_info *si, char *options)
286 {
287 	char *this_opt;
288 
289 	if (options && *options) {
290 		while ((this_opt = strsep(&options, ",")) != NULL) {
291 			if (!*this_opt) continue;
292 
293 			efifb_setup_from_dmi(si, this_opt);
294 
295 			if (!strncmp(this_opt, "base:", 5))
296 				si->lfb_base = simple_strtoul(this_opt+5, NULL, 0);
297 			else if (!strncmp(this_opt, "stride:", 7))
298 				si->lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
299 			else if (!strncmp(this_opt, "height:", 7))
300 				si->lfb_height = simple_strtoul(this_opt+7, NULL, 0);
301 			else if (!strncmp(this_opt, "width:", 6))
302 				si->lfb_width = simple_strtoul(this_opt+6, NULL, 0);
303 			else if (!strcmp(this_opt, "nowc"))
304 				mem_flags &= ~EFI_MEMORY_WC;
305 			else if (!strcmp(this_opt, "nobgrt"))
306 				use_bgrt = false;
307 		}
308 	}
309 
310 	return 0;
311 }
312 
313 static inline bool fb_base_is_valid(struct screen_info *si)
314 {
315 	if (si->lfb_base)
316 		return true;
317 
318 	if (!(si->capabilities & VIDEO_CAPABILITY_64BIT_BASE))
319 		return false;
320 
321 	if (si->ext_lfb_base)
322 		return true;
323 
324 	return false;
325 }
326 
327 #define efifb_attr_decl(name, fmt)					\
328 static ssize_t name##_show(struct device *dev,				\
329 			   struct device_attribute *attr,		\
330 			   char *buf)					\
331 {									\
332 	struct screen_info *si = dev_get_platdata(dev);			\
333 	if (!si)							\
334 		return -ENODEV;						\
335 	return sprintf(buf, fmt "\n", (si->lfb_##name));		\
336 }									\
337 static DEVICE_ATTR_RO(name)
338 
339 efifb_attr_decl(base, "0x%x");
340 efifb_attr_decl(linelength, "%u");
341 efifb_attr_decl(height, "%u");
342 efifb_attr_decl(width, "%u");
343 efifb_attr_decl(depth, "%u");
344 
345 static struct attribute *efifb_attrs[] = {
346 	&dev_attr_base.attr,
347 	&dev_attr_linelength.attr,
348 	&dev_attr_width.attr,
349 	&dev_attr_height.attr,
350 	&dev_attr_depth.attr,
351 	NULL
352 };
353 ATTRIBUTE_GROUPS(efifb);
354 
355 static bool pci_dev_disabled;	/* FB base matches BAR of a disabled device */
356 
357 static struct resource *bar_resource;
358 static u64 bar_offset;
359 
360 static int efifb_probe(struct platform_device *dev)
361 {
362 	struct screen_info *si;
363 	struct fb_info *info;
364 	struct efifb_par *par;
365 	int err, orientation;
366 	unsigned int size_vmode;
367 	unsigned int size_remap;
368 	unsigned int size_total;
369 	char *option = NULL;
370 	efi_memory_desc_t md;
371 
372 	/*
373 	 * If we fail probing the device, the kernel might try a different
374 	 * driver. We get a copy of the attached screen_info, so that we can
375 	 * modify its values without affecting later drivers.
376 	 */
377 	si = dev_get_platdata(&dev->dev);
378 	if (!si)
379 		return -ENODEV;
380 	si = devm_kmemdup(&dev->dev, si, sizeof(*si), GFP_KERNEL);
381 	if (!si)
382 		return -ENOMEM;
383 
384 	if (si->orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
385 		return -ENODEV;
386 
387 	if (fb_get_options("efifb", &option))
388 		return -ENODEV;
389 	efifb_setup(si, option);
390 
391 	/* We don't get linelength from UGA Draw Protocol, only from
392 	 * EFI Graphics Protocol.  So if it's not in DMI, and it's not
393 	 * passed in from the user, we really can't use the framebuffer.
394 	 */
395 	if (!si->lfb_linelength)
396 		return -ENODEV;
397 
398 	if (!si->lfb_depth)
399 		si->lfb_depth = 32;
400 	if (!si->pages)
401 		si->pages = 1;
402 	if (!fb_base_is_valid(si)) {
403 		printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
404 		return -ENODEV;
405 	}
406 	printk(KERN_INFO "efifb: probing for efifb\n");
407 
408 	/* just assume they're all unset if any are */
409 	if (!si->blue_size) {
410 		si->blue_size = 8;
411 		si->blue_pos = 0;
412 		si->green_size = 8;
413 		si->green_pos = 8;
414 		si->red_size = 8;
415 		si->red_pos = 16;
416 		si->rsvd_size = 8;
417 		si->rsvd_pos = 24;
418 	}
419 
420 	efifb_fix.smem_start = si->lfb_base;
421 
422 	if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
423 		u64 ext_lfb_base;
424 
425 		ext_lfb_base = (u64)(unsigned long)si->ext_lfb_base << 32;
426 		efifb_fix.smem_start |= ext_lfb_base;
427 	}
428 
429 	if (bar_resource &&
430 	    bar_resource->start + bar_offset != efifb_fix.smem_start) {
431 		dev_info(&efifb_pci_dev->dev,
432 			 "BAR has moved, updating efifb address\n");
433 		efifb_fix.smem_start = bar_resource->start + bar_offset;
434 	}
435 
436 	efifb_defined.bits_per_pixel = si->lfb_depth;
437 	efifb_defined.xres = si->lfb_width;
438 	efifb_defined.yres = si->lfb_height;
439 	efifb_fix.line_length = si->lfb_linelength;
440 
441 	/*   size_vmode -- that is the amount of memory needed for the
442 	 *                 used video mode, i.e. the minimum amount of
443 	 *                 memory we need. */
444 	size_vmode = efifb_defined.yres * efifb_fix.line_length;
445 
446 	/*   size_total -- all video memory we have. Used for
447 	 *                 entries, ressource allocation and bounds
448 	 *                 checking. */
449 	size_total = si->lfb_size;
450 	if (size_total < size_vmode)
451 		size_total = size_vmode;
452 
453 	/*   size_remap -- the amount of video memory we are going to
454 	 *                 use for efifb.  With modern cards it is no
455 	 *                 option to simply use size_total as that
456 	 *                 wastes plenty of kernel address space. */
457 	size_remap  = size_vmode * 2;
458 	if (size_remap > size_total)
459 		size_remap = size_total;
460 	if (size_remap % PAGE_SIZE)
461 		size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
462 	efifb_fix.smem_len = size_remap;
463 
464 	if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
465 		request_mem_succeeded = true;
466 	} else {
467 		/* We cannot make this fatal. Sometimes this comes from magic
468 		   spaces our resource handlers simply don't know about */
469 		pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
470 			efifb_fix.smem_start);
471 	}
472 
473 	info = framebuffer_alloc(sizeof(*par), &dev->dev);
474 	if (!info) {
475 		err = -ENOMEM;
476 		goto err_release_mem;
477 	}
478 	platform_set_drvdata(dev, info);
479 	par = info->par;
480 	info->pseudo_palette = par->pseudo_palette;
481 
482 	par->base = efifb_fix.smem_start;
483 	par->size = size_remap;
484 
485 	if (efi_enabled(EFI_MEMMAP) &&
486 	    !efi_mem_desc_lookup(efifb_fix.smem_start, &md)) {
487 		if ((efifb_fix.smem_start + efifb_fix.smem_len) >
488 		    (md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT))) {
489 			pr_err("efifb: video memory @ 0x%lx spans multiple EFI memory regions\n",
490 			       efifb_fix.smem_start);
491 			err = -EIO;
492 			goto err_release_fb;
493 		}
494 		/*
495 		 * If the UEFI memory map covers the efifb region, we may only
496 		 * remap it using the attributes the memory map prescribes.
497 		 */
498 		md.attribute &= EFI_MEMORY_UC | EFI_MEMORY_WC |
499 				EFI_MEMORY_WT | EFI_MEMORY_WB;
500 		if (md.attribute) {
501 			mem_flags |= EFI_MEMORY_WT | EFI_MEMORY_WB;
502 			mem_flags &= md.attribute;
503 		}
504 	}
505 	if (mem_flags & EFI_MEMORY_WC)
506 		info->screen_base = ioremap_wc(efifb_fix.smem_start,
507 					       efifb_fix.smem_len);
508 	else if (mem_flags & EFI_MEMORY_UC)
509 		info->screen_base = ioremap(efifb_fix.smem_start,
510 					    efifb_fix.smem_len);
511 	else if (mem_flags & EFI_MEMORY_WT)
512 		info->screen_base = memremap(efifb_fix.smem_start,
513 					     efifb_fix.smem_len, MEMREMAP_WT);
514 	else if (mem_flags & EFI_MEMORY_WB)
515 		info->screen_base = memremap(efifb_fix.smem_start,
516 					     efifb_fix.smem_len, MEMREMAP_WB);
517 	if (!info->screen_base) {
518 		pr_err("efifb: abort, cannot remap video memory 0x%x @ 0x%lx\n",
519 			efifb_fix.smem_len, efifb_fix.smem_start);
520 		err = -EIO;
521 		goto err_release_fb;
522 	}
523 
524 	efifb_show_boot_graphics(info, si);
525 
526 	pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
527 	       efifb_fix.smem_start, size_remap/1024, size_total/1024);
528 	pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
529 	       efifb_defined.xres, efifb_defined.yres,
530 	       efifb_defined.bits_per_pixel, efifb_fix.line_length,
531 	       si->pages);
532 
533 	efifb_defined.xres_virtual = efifb_defined.xres;
534 	efifb_defined.yres_virtual = efifb_fix.smem_len /
535 					efifb_fix.line_length;
536 	pr_info("efifb: scrolling: redraw\n");
537 	efifb_defined.yres_virtual = efifb_defined.yres;
538 
539 	/* some dummy values for timing to make fbset happy */
540 	efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
541 					1000 / efifb_defined.yres;
542 	efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
543 	efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
544 
545 	efifb_defined.red.offset    = si->red_pos;
546 	efifb_defined.red.length    = si->red_size;
547 	efifb_defined.green.offset  = si->green_pos;
548 	efifb_defined.green.length  = si->green_size;
549 	efifb_defined.blue.offset   = si->blue_pos;
550 	efifb_defined.blue.length   = si->blue_size;
551 	efifb_defined.transp.offset = si->rsvd_pos;
552 	efifb_defined.transp.length = si->rsvd_size;
553 
554 	pr_info("efifb: %s: "
555 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
556 	       "Truecolor",
557 	       si->rsvd_size,
558 	       si->red_size,
559 	       si->green_size,
560 	       si->blue_size,
561 	       si->rsvd_pos,
562 	       si->red_pos,
563 	       si->green_pos,
564 	       si->blue_pos);
565 
566 	efifb_fix.ypanstep  = 0;
567 	efifb_fix.ywrapstep = 0;
568 
569 	info->fbops = &efifb_ops;
570 	info->var = efifb_defined;
571 	info->fix = efifb_fix;
572 
573 	orientation = drm_get_panel_orientation_quirk(efifb_defined.xres,
574 						      efifb_defined.yres);
575 	switch (orientation) {
576 	default:
577 		info->fbcon_rotate_hint = FB_ROTATE_UR;
578 		break;
579 	case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
580 		info->fbcon_rotate_hint = FB_ROTATE_UD;
581 		break;
582 	case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
583 		info->fbcon_rotate_hint = FB_ROTATE_CCW;
584 		break;
585 	case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
586 		info->fbcon_rotate_hint = FB_ROTATE_CW;
587 		break;
588 	}
589 
590 	err = sysfs_create_groups(&dev->dev.kobj, efifb_groups);
591 	if (err) {
592 		pr_err("efifb: cannot add sysfs attrs\n");
593 		goto err_unmap;
594 	}
595 	err = fb_alloc_cmap(&info->cmap, 256, 0);
596 	if (err < 0) {
597 		pr_err("efifb: cannot allocate colormap\n");
598 		goto err_groups;
599 	}
600 
601 	if (efifb_pci_dev)
602 		WARN_ON(pm_runtime_get_sync(&efifb_pci_dev->dev) < 0);
603 
604 	err = devm_aperture_acquire_for_platform_device(dev, par->base, par->size);
605 	if (err) {
606 		pr_err("efifb: cannot acquire aperture\n");
607 		goto err_put_rpm_ref;
608 	}
609 	err = register_framebuffer(info);
610 	if (err < 0) {
611 		pr_err("efifb: cannot register framebuffer\n");
612 		goto err_put_rpm_ref;
613 	}
614 	fb_info(info, "%s frame buffer device\n", info->fix.id);
615 	return 0;
616 
617 err_put_rpm_ref:
618 	if (efifb_pci_dev)
619 		pm_runtime_put(&efifb_pci_dev->dev);
620 
621 	fb_dealloc_cmap(&info->cmap);
622 err_groups:
623 	sysfs_remove_groups(&dev->dev.kobj, efifb_groups);
624 err_unmap:
625 	if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
626 		iounmap(info->screen_base);
627 	else
628 		memunmap(info->screen_base);
629 err_release_fb:
630 	framebuffer_release(info);
631 err_release_mem:
632 	if (request_mem_succeeded)
633 		release_mem_region(efifb_fix.smem_start, size_total);
634 	return err;
635 }
636 
637 static void efifb_remove(struct platform_device *pdev)
638 {
639 	struct fb_info *info = platform_get_drvdata(pdev);
640 
641 	/* efifb_destroy takes care of info cleanup */
642 	unregister_framebuffer(info);
643 	sysfs_remove_groups(&pdev->dev.kobj, efifb_groups);
644 }
645 
646 static struct platform_driver efifb_driver = {
647 	.driver = {
648 		.name = "efi-framebuffer",
649 	},
650 	.probe = efifb_probe,
651 	.remove_new = efifb_remove,
652 };
653 
654 builtin_platform_driver(efifb_driver);
655 
656 #if defined(CONFIG_PCI)
657 
658 static void record_efifb_bar_resource(struct pci_dev *dev, int idx, u64 offset)
659 {
660 	u16 word;
661 
662 	efifb_pci_dev = dev;
663 
664 	pci_read_config_word(dev, PCI_COMMAND, &word);
665 	if (!(word & PCI_COMMAND_MEMORY)) {
666 		pci_dev_disabled = true;
667 		dev_err(&dev->dev,
668 			"BAR %d: assigned to efifb but device is disabled!\n",
669 			idx);
670 		return;
671 	}
672 
673 	bar_resource = &dev->resource[idx];
674 	bar_offset = offset;
675 
676 	dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
677 }
678 
679 static void efifb_fixup_resources(struct pci_dev *dev)
680 {
681 	u64 base = screen_info.lfb_base;
682 	u64 size = screen_info.lfb_size;
683 	int i;
684 
685 	if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
686 		return;
687 
688 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
689 		base |= (u64)screen_info.ext_lfb_base << 32;
690 
691 	if (!base)
692 		return;
693 
694 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
695 		struct resource *res = &dev->resource[i];
696 
697 		if (!(res->flags & IORESOURCE_MEM))
698 			continue;
699 
700 		if (res->start <= base && res->end >= base + size - 1) {
701 			record_efifb_bar_resource(dev, i, base - res->start);
702 			break;
703 		}
704 	}
705 }
706 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
707 			       16, efifb_fixup_resources);
708 
709 #endif
710