xref: /linux/drivers/video/fbdev/efifb.c (revision 8386f58f8deda81110283798a387fb53ec21957c)
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, 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(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(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)
151 {
152 	u32 bmp_width, bmp_height, bmp_pitch, dst_x, y, src_y;
153 	struct screen_info *si = &screen_info;
154 	struct bmp_file_header *file_header;
155 	struct bmp_dib_header *dib_header;
156 	void *bgrt_image = NULL;
157 	u8 *dst = info->screen_base;
158 
159 	if (!use_bgrt)
160 		return;
161 
162 	if (!bgrt_tab.image_address) {
163 		pr_info("efifb: No BGRT, not showing boot graphics\n");
164 		return;
165 	}
166 
167 	if (bgrt_tab.status & 0x06) {
168 		pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n");
169 		return;
170 	}
171 
172 	/* Avoid flashing the logo if we're going to print std probe messages */
173 	if (console_loglevel > CONSOLE_LOGLEVEL_QUIET)
174 		return;
175 
176 	/* bgrt_tab.status is unreliable, so we don't check it */
177 
178 	if (si->lfb_depth != 32) {
179 		pr_info("efifb: not 32 bits, not showing boot graphics\n");
180 		return;
181 	}
182 
183 	bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
184 			      MEMREMAP_WB);
185 	if (!bgrt_image) {
186 		pr_warn("efifb: Ignoring BGRT: failed to map image memory\n");
187 		return;
188 	}
189 
190 	if (bgrt_image_size < (sizeof(*file_header) + sizeof(*dib_header)))
191 		goto error;
192 
193 	file_header = bgrt_image;
194 	if (file_header->id != 0x4d42 || file_header->reserved != 0)
195 		goto error;
196 
197 	dib_header = bgrt_image + sizeof(*file_header);
198 	if (dib_header->dib_header_size != 40 || dib_header->width < 0 ||
199 	    dib_header->planes != 1 || dib_header->bpp != 24 ||
200 	    dib_header->compression != 0)
201 		goto error;
202 
203 	bmp_width = dib_header->width;
204 	bmp_height = abs(dib_header->height);
205 	bmp_pitch = round_up(3 * bmp_width, 4);
206 
207 	if ((file_header->bitmap_offset + bmp_pitch * bmp_height) >
208 				bgrt_image_size)
209 		goto error;
210 
211 	if ((bgrt_tab.image_offset_x + bmp_width) > si->lfb_width ||
212 	    (bgrt_tab.image_offset_y + bmp_height) > si->lfb_height)
213 		goto error;
214 
215 	if (!efifb_bgrt_sanity_check(si, bmp_width))
216 		goto error;
217 
218 	pr_info("efifb: showing boot graphics\n");
219 
220 	for (y = 0; y < si->lfb_height; y++, dst += si->lfb_linelength) {
221 		/* Only background? */
222 		if (y < bgrt_tab.image_offset_y ||
223 		    y >= (bgrt_tab.image_offset_y + bmp_height)) {
224 			memset(dst, 0, 4 * si->lfb_width);
225 			continue;
226 		}
227 
228 		src_y = y - bgrt_tab.image_offset_y;
229 		/* Positive header height means upside down row order */
230 		if (dib_header->height > 0)
231 			src_y = (bmp_height - 1) - src_y;
232 
233 		memset(dst, 0, bgrt_tab.image_offset_x * 4);
234 		dst_x = bgrt_tab.image_offset_x;
235 		efifb_copy_bmp(bgrt_image + file_header->bitmap_offset +
236 					    src_y * bmp_pitch,
237 			       (u32 *)dst + dst_x, bmp_width, si);
238 		dst_x += bmp_width;
239 		memset((u32 *)dst + dst_x, 0, (si->lfb_width - dst_x) * 4);
240 	}
241 
242 	memunmap(bgrt_image);
243 	return;
244 
245 error:
246 	memunmap(bgrt_image);
247 	pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n");
248 }
249 #else
250 static inline void efifb_show_boot_graphics(struct fb_info *info) {}
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_destroy	= efifb_destroy,
281 	.fb_setcolreg	= efifb_setcolreg,
282 	.fb_fillrect	= cfb_fillrect,
283 	.fb_copyarea	= cfb_copyarea,
284 	.fb_imageblit	= cfb_imageblit,
285 };
286 
287 static int efifb_setup(char *options)
288 {
289 	char *this_opt;
290 
291 	if (options && *options) {
292 		while ((this_opt = strsep(&options, ",")) != NULL) {
293 			if (!*this_opt) continue;
294 
295 			efifb_setup_from_dmi(&screen_info, this_opt);
296 
297 			if (!strncmp(this_opt, "base:", 5))
298 				screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
299 			else if (!strncmp(this_opt, "stride:", 7))
300 				screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
301 			else if (!strncmp(this_opt, "height:", 7))
302 				screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
303 			else if (!strncmp(this_opt, "width:", 6))
304 				screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
305 			else if (!strcmp(this_opt, "nowc"))
306 				mem_flags &= ~EFI_MEMORY_WC;
307 			else if (!strcmp(this_opt, "nobgrt"))
308 				use_bgrt = false;
309 		}
310 	}
311 
312 	return 0;
313 }
314 
315 static inline bool fb_base_is_valid(void)
316 {
317 	if (screen_info.lfb_base)
318 		return true;
319 
320 	if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE))
321 		return false;
322 
323 	if (screen_info.ext_lfb_base)
324 		return true;
325 
326 	return false;
327 }
328 
329 #define efifb_attr_decl(name, fmt)					\
330 static ssize_t name##_show(struct device *dev,				\
331 			   struct device_attribute *attr,		\
332 			   char *buf)					\
333 {									\
334 	return sprintf(buf, fmt "\n", (screen_info.lfb_##name));	\
335 }									\
336 static DEVICE_ATTR_RO(name)
337 
338 efifb_attr_decl(base, "0x%x");
339 efifb_attr_decl(linelength, "%u");
340 efifb_attr_decl(height, "%u");
341 efifb_attr_decl(width, "%u");
342 efifb_attr_decl(depth, "%u");
343 
344 static struct attribute *efifb_attrs[] = {
345 	&dev_attr_base.attr,
346 	&dev_attr_linelength.attr,
347 	&dev_attr_width.attr,
348 	&dev_attr_height.attr,
349 	&dev_attr_depth.attr,
350 	NULL
351 };
352 ATTRIBUTE_GROUPS(efifb);
353 
354 static bool pci_dev_disabled;	/* FB base matches BAR of a disabled device */
355 
356 static struct resource *bar_resource;
357 static u64 bar_offset;
358 
359 static int efifb_probe(struct platform_device *dev)
360 {
361 	struct fb_info *info;
362 	struct efifb_par *par;
363 	int err, orientation;
364 	unsigned int size_vmode;
365 	unsigned int size_remap;
366 	unsigned int size_total;
367 	char *option = NULL;
368 	efi_memory_desc_t md;
369 
370 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
371 		return -ENODEV;
372 
373 	if (fb_get_options("efifb", &option))
374 		return -ENODEV;
375 	efifb_setup(option);
376 
377 	/* We don't get linelength from UGA Draw Protocol, only from
378 	 * EFI Graphics Protocol.  So if it's not in DMI, and it's not
379 	 * passed in from the user, we really can't use the framebuffer.
380 	 */
381 	if (!screen_info.lfb_linelength)
382 		return -ENODEV;
383 
384 	if (!screen_info.lfb_depth)
385 		screen_info.lfb_depth = 32;
386 	if (!screen_info.pages)
387 		screen_info.pages = 1;
388 	if (!fb_base_is_valid()) {
389 		printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
390 		return -ENODEV;
391 	}
392 	printk(KERN_INFO "efifb: probing for efifb\n");
393 
394 	/* just assume they're all unset if any are */
395 	if (!screen_info.blue_size) {
396 		screen_info.blue_size = 8;
397 		screen_info.blue_pos = 0;
398 		screen_info.green_size = 8;
399 		screen_info.green_pos = 8;
400 		screen_info.red_size = 8;
401 		screen_info.red_pos = 16;
402 		screen_info.rsvd_size = 8;
403 		screen_info.rsvd_pos = 24;
404 	}
405 
406 	efifb_fix.smem_start = screen_info.lfb_base;
407 
408 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
409 		u64 ext_lfb_base;
410 
411 		ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
412 		efifb_fix.smem_start |= ext_lfb_base;
413 	}
414 
415 	if (bar_resource &&
416 	    bar_resource->start + bar_offset != efifb_fix.smem_start) {
417 		dev_info(&efifb_pci_dev->dev,
418 			 "BAR has moved, updating efifb address\n");
419 		efifb_fix.smem_start = bar_resource->start + bar_offset;
420 	}
421 
422 	efifb_defined.bits_per_pixel = screen_info.lfb_depth;
423 	efifb_defined.xres = screen_info.lfb_width;
424 	efifb_defined.yres = screen_info.lfb_height;
425 	efifb_fix.line_length = screen_info.lfb_linelength;
426 
427 	/*   size_vmode -- that is the amount of memory needed for the
428 	 *                 used video mode, i.e. the minimum amount of
429 	 *                 memory we need. */
430 	size_vmode = efifb_defined.yres * efifb_fix.line_length;
431 
432 	/*   size_total -- all video memory we have. Used for
433 	 *                 entries, ressource allocation and bounds
434 	 *                 checking. */
435 	size_total = screen_info.lfb_size;
436 	if (size_total < size_vmode)
437 		size_total = size_vmode;
438 
439 	/*   size_remap -- the amount of video memory we are going to
440 	 *                 use for efifb.  With modern cards it is no
441 	 *                 option to simply use size_total as that
442 	 *                 wastes plenty of kernel address space. */
443 	size_remap  = size_vmode * 2;
444 	if (size_remap > size_total)
445 		size_remap = size_total;
446 	if (size_remap % PAGE_SIZE)
447 		size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
448 	efifb_fix.smem_len = size_remap;
449 
450 	if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
451 		request_mem_succeeded = true;
452 	} else {
453 		/* We cannot make this fatal. Sometimes this comes from magic
454 		   spaces our resource handlers simply don't know about */
455 		pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
456 			efifb_fix.smem_start);
457 	}
458 
459 	info = framebuffer_alloc(sizeof(*par), &dev->dev);
460 	if (!info) {
461 		err = -ENOMEM;
462 		goto err_release_mem;
463 	}
464 	platform_set_drvdata(dev, info);
465 	par = info->par;
466 	info->pseudo_palette = par->pseudo_palette;
467 
468 	par->base = efifb_fix.smem_start;
469 	par->size = size_remap;
470 
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);
477 			err = -EIO;
478 			goto err_release_fb;
479 		}
480 		/*
481 		 * If the UEFI memory map covers the efifb region, we may only
482 		 * remap it using the attributes the memory map prescribes.
483 		 */
484 		md.attribute &= EFI_MEMORY_UC | EFI_MEMORY_WC |
485 				EFI_MEMORY_WT | EFI_MEMORY_WB;
486 		if (md.attribute) {
487 			mem_flags |= EFI_MEMORY_WT | EFI_MEMORY_WB;
488 			mem_flags &= md.attribute;
489 		}
490 	}
491 	if (mem_flags & EFI_MEMORY_WC)
492 		info->screen_base = ioremap_wc(efifb_fix.smem_start,
493 					       efifb_fix.smem_len);
494 	else if (mem_flags & EFI_MEMORY_UC)
495 		info->screen_base = ioremap(efifb_fix.smem_start,
496 					    efifb_fix.smem_len);
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);
506 		err = -EIO;
507 		goto err_release_fb;
508 	}
509 
510 	efifb_show_boot_graphics(info);
511 
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,
517 	       screen_info.pages);
518 
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;
524 
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;
530 
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;
539 
540 	pr_info("efifb: %s: "
541 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
542 	       "Truecolor",
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,
548 	       screen_info.red_pos,
549 	       screen_info.green_pos,
550 	       screen_info.blue_pos);
551 
552 	efifb_fix.ypanstep  = 0;
553 	efifb_fix.ywrapstep = 0;
554 
555 	info->fbops = &efifb_ops;
556 	info->var = efifb_defined;
557 	info->fix = efifb_fix;
558 	info->flags = FBINFO_FLAG_DEFAULT;
559 
560 	orientation = drm_get_panel_orientation_quirk(efifb_defined.xres,
561 						      efifb_defined.yres);
562 	switch (orientation) {
563 	default:
564 		info->fbcon_rotate_hint = FB_ROTATE_UR;
565 		break;
566 	case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
567 		info->fbcon_rotate_hint = FB_ROTATE_UD;
568 		break;
569 	case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
570 		info->fbcon_rotate_hint = FB_ROTATE_CCW;
571 		break;
572 	case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
573 		info->fbcon_rotate_hint = FB_ROTATE_CW;
574 		break;
575 	}
576 
577 	err = sysfs_create_groups(&dev->dev.kobj, efifb_groups);
578 	if (err) {
579 		pr_err("efifb: cannot add sysfs attrs\n");
580 		goto err_unmap;
581 	}
582 	err = fb_alloc_cmap(&info->cmap, 256, 0);
583 	if (err < 0) {
584 		pr_err("efifb: cannot allocate colormap\n");
585 		goto err_groups;
586 	}
587 
588 	if (efifb_pci_dev)
589 		WARN_ON(pm_runtime_get_sync(&efifb_pci_dev->dev) < 0);
590 
591 	err = devm_aperture_acquire_for_platform_device(dev, par->base, par->size);
592 	if (err) {
593 		pr_err("efifb: cannot acquire aperture\n");
594 		goto err_put_rpm_ref;
595 	}
596 	err = register_framebuffer(info);
597 	if (err < 0) {
598 		pr_err("efifb: cannot register framebuffer\n");
599 		goto err_put_rpm_ref;
600 	}
601 	fb_info(info, "%s frame buffer device\n", info->fix.id);
602 	return 0;
603 
604 err_put_rpm_ref:
605 	if (efifb_pci_dev)
606 		pm_runtime_put(&efifb_pci_dev->dev);
607 
608 	fb_dealloc_cmap(&info->cmap);
609 err_groups:
610 	sysfs_remove_groups(&dev->dev.kobj, efifb_groups);
611 err_unmap:
612 	if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
613 		iounmap(info->screen_base);
614 	else
615 		memunmap(info->screen_base);
616 err_release_fb:
617 	framebuffer_release(info);
618 err_release_mem:
619 	if (request_mem_succeeded)
620 		release_mem_region(efifb_fix.smem_start, size_total);
621 	return err;
622 }
623 
624 static void efifb_remove(struct platform_device *pdev)
625 {
626 	struct fb_info *info = platform_get_drvdata(pdev);
627 
628 	/* efifb_destroy takes care of info cleanup */
629 	unregister_framebuffer(info);
630 	sysfs_remove_groups(&pdev->dev.kobj, efifb_groups);
631 }
632 
633 static struct platform_driver efifb_driver = {
634 	.driver = {
635 		.name = "efi-framebuffer",
636 	},
637 	.probe = efifb_probe,
638 	.remove_new = efifb_remove,
639 };
640 
641 builtin_platform_driver(efifb_driver);
642 
643 #if defined(CONFIG_PCI)
644 
645 static void record_efifb_bar_resource(struct pci_dev *dev, int idx, u64 offset)
646 {
647 	u16 word;
648 
649 	efifb_pci_dev = dev;
650 
651 	pci_read_config_word(dev, PCI_COMMAND, &word);
652 	if (!(word & PCI_COMMAND_MEMORY)) {
653 		pci_dev_disabled = true;
654 		dev_err(&dev->dev,
655 			"BAR %d: assigned to efifb but device is disabled!\n",
656 			idx);
657 		return;
658 	}
659 
660 	bar_resource = &dev->resource[idx];
661 	bar_offset = offset;
662 
663 	dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
664 }
665 
666 static void efifb_fixup_resources(struct pci_dev *dev)
667 {
668 	u64 base = screen_info.lfb_base;
669 	u64 size = screen_info.lfb_size;
670 	int i;
671 
672 	if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
673 		return;
674 
675 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
676 		base |= (u64)screen_info.ext_lfb_base << 32;
677 
678 	if (!base)
679 		return;
680 
681 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
682 		struct resource *res = &dev->resource[i];
683 
684 		if (!(res->flags & IORESOURCE_MEM))
685 			continue;
686 
687 		if (res->start <= base && res->end >= base + size - 1) {
688 			record_efifb_bar_resource(dev, i, base - res->start);
689 			break;
690 		}
691 	}
692 }
693 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
694 			       16, efifb_fixup_resources);
695 
696 #endif
697