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 
efifb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)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
efifb_copy_bmp(u8 * src,u32 * dst,int width,struct screen_info * si)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  */
efifb_bgrt_sanity_check(struct screen_info * si,u32 bmp_width)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
efifb_bgrt_sanity_check(struct screen_info * si,u32 bmp_width)144 static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width)
145 {
146 	return true;
147 }
148 #endif
149 
efifb_show_boot_graphics(struct fb_info * info)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
efifb_show_boot_graphics(struct fb_info * info)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  */
efifb_destroy(struct fb_info * info)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 
efifb_setup(char * options)285 static int efifb_setup(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(&screen_info, this_opt);
294 
295 			if (!strncmp(this_opt, "base:", 5))
296 				screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
297 			else if (!strncmp(this_opt, "stride:", 7))
298 				screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
299 			else if (!strncmp(this_opt, "height:", 7))
300 				screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
301 			else if (!strncmp(this_opt, "width:", 6))
302 				screen_info.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 
fb_base_is_valid(void)313 static inline bool fb_base_is_valid(void)
314 {
315 	if (screen_info.lfb_base)
316 		return true;
317 
318 	if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE))
319 		return false;
320 
321 	if (screen_info.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 	return sprintf(buf, fmt "\n", (screen_info.lfb_##name));	\
333 }									\
334 static DEVICE_ATTR_RO(name)
335 
336 efifb_attr_decl(base, "0x%x");
337 efifb_attr_decl(linelength, "%u");
338 efifb_attr_decl(height, "%u");
339 efifb_attr_decl(width, "%u");
340 efifb_attr_decl(depth, "%u");
341 
342 static struct attribute *efifb_attrs[] = {
343 	&dev_attr_base.attr,
344 	&dev_attr_linelength.attr,
345 	&dev_attr_width.attr,
346 	&dev_attr_height.attr,
347 	&dev_attr_depth.attr,
348 	NULL
349 };
350 ATTRIBUTE_GROUPS(efifb);
351 
352 static bool pci_dev_disabled;	/* FB base matches BAR of a disabled device */
353 
354 static struct resource *bar_resource;
355 static u64 bar_offset;
356 
efifb_probe(struct platform_device * dev)357 static int efifb_probe(struct platform_device *dev)
358 {
359 	struct fb_info *info;
360 	struct efifb_par *par;
361 	int err, orientation;
362 	unsigned int size_vmode;
363 	unsigned int size_remap;
364 	unsigned int size_total;
365 	char *option = NULL;
366 	efi_memory_desc_t md;
367 
368 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
369 		return -ENODEV;
370 
371 	if (fb_get_options("efifb", &option))
372 		return -ENODEV;
373 	efifb_setup(option);
374 
375 	/* We don't get linelength from UGA Draw Protocol, only from
376 	 * EFI Graphics Protocol.  So if it's not in DMI, and it's not
377 	 * passed in from the user, we really can't use the framebuffer.
378 	 */
379 	if (!screen_info.lfb_linelength)
380 		return -ENODEV;
381 
382 	if (!screen_info.lfb_depth)
383 		screen_info.lfb_depth = 32;
384 	if (!screen_info.pages)
385 		screen_info.pages = 1;
386 	if (!fb_base_is_valid()) {
387 		printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
388 		return -ENODEV;
389 	}
390 	printk(KERN_INFO "efifb: probing for efifb\n");
391 
392 	/* just assume they're all unset if any are */
393 	if (!screen_info.blue_size) {
394 		screen_info.blue_size = 8;
395 		screen_info.blue_pos = 0;
396 		screen_info.green_size = 8;
397 		screen_info.green_pos = 8;
398 		screen_info.red_size = 8;
399 		screen_info.red_pos = 16;
400 		screen_info.rsvd_size = 8;
401 		screen_info.rsvd_pos = 24;
402 	}
403 
404 	efifb_fix.smem_start = screen_info.lfb_base;
405 
406 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
407 		u64 ext_lfb_base;
408 
409 		ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
410 		efifb_fix.smem_start |= ext_lfb_base;
411 	}
412 
413 	if (bar_resource &&
414 	    bar_resource->start + bar_offset != efifb_fix.smem_start) {
415 		dev_info(&efifb_pci_dev->dev,
416 			 "BAR has moved, updating efifb address\n");
417 		efifb_fix.smem_start = bar_resource->start + bar_offset;
418 	}
419 
420 	efifb_defined.bits_per_pixel = screen_info.lfb_depth;
421 	efifb_defined.xres = screen_info.lfb_width;
422 	efifb_defined.yres = screen_info.lfb_height;
423 	efifb_fix.line_length = screen_info.lfb_linelength;
424 
425 	/*   size_vmode -- that is the amount of memory needed for the
426 	 *                 used video mode, i.e. the minimum amount of
427 	 *                 memory we need. */
428 	size_vmode = efifb_defined.yres * efifb_fix.line_length;
429 
430 	/*   size_total -- all video memory we have. Used for
431 	 *                 entries, ressource allocation and bounds
432 	 *                 checking. */
433 	size_total = screen_info.lfb_size;
434 	if (size_total < size_vmode)
435 		size_total = size_vmode;
436 
437 	/*   size_remap -- the amount of video memory we are going to
438 	 *                 use for efifb.  With modern cards it is no
439 	 *                 option to simply use size_total as that
440 	 *                 wastes plenty of kernel address space. */
441 	size_remap  = size_vmode * 2;
442 	if (size_remap > size_total)
443 		size_remap = size_total;
444 	if (size_remap % PAGE_SIZE)
445 		size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
446 	efifb_fix.smem_len = size_remap;
447 
448 	if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
449 		request_mem_succeeded = true;
450 	} else {
451 		/* We cannot make this fatal. Sometimes this comes from magic
452 		   spaces our resource handlers simply don't know about */
453 		pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
454 			efifb_fix.smem_start);
455 	}
456 
457 	info = framebuffer_alloc(sizeof(*par), &dev->dev);
458 	if (!info) {
459 		err = -ENOMEM;
460 		goto err_release_mem;
461 	}
462 	platform_set_drvdata(dev, info);
463 	par = info->par;
464 	info->pseudo_palette = par->pseudo_palette;
465 
466 	par->base = efifb_fix.smem_start;
467 	par->size = size_remap;
468 
469 	if (efi_enabled(EFI_MEMMAP) &&
470 	    !efi_mem_desc_lookup(efifb_fix.smem_start, &md)) {
471 		if ((efifb_fix.smem_start + efifb_fix.smem_len) >
472 		    (md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT))) {
473 			pr_err("efifb: video memory @ 0x%lx spans multiple EFI memory regions\n",
474 			       efifb_fix.smem_start);
475 			err = -EIO;
476 			goto err_release_fb;
477 		}
478 		/*
479 		 * If the UEFI memory map covers the efifb region, we may only
480 		 * remap it using the attributes the memory map prescribes.
481 		 */
482 		md.attribute &= EFI_MEMORY_UC | EFI_MEMORY_WC |
483 				EFI_MEMORY_WT | EFI_MEMORY_WB;
484 		if (md.attribute) {
485 			mem_flags |= EFI_MEMORY_WT | EFI_MEMORY_WB;
486 			mem_flags &= md.attribute;
487 		}
488 	}
489 	if (mem_flags & EFI_MEMORY_WC)
490 		info->screen_base = ioremap_wc(efifb_fix.smem_start,
491 					       efifb_fix.smem_len);
492 	else if (mem_flags & EFI_MEMORY_UC)
493 		info->screen_base = ioremap(efifb_fix.smem_start,
494 					    efifb_fix.smem_len);
495 	else if (mem_flags & EFI_MEMORY_WT)
496 		info->screen_base = memremap(efifb_fix.smem_start,
497 					     efifb_fix.smem_len, MEMREMAP_WT);
498 	else if (mem_flags & EFI_MEMORY_WB)
499 		info->screen_base = memremap(efifb_fix.smem_start,
500 					     efifb_fix.smem_len, MEMREMAP_WB);
501 	if (!info->screen_base) {
502 		pr_err("efifb: abort, cannot remap video memory 0x%x @ 0x%lx\n",
503 			efifb_fix.smem_len, efifb_fix.smem_start);
504 		err = -EIO;
505 		goto err_release_fb;
506 	}
507 
508 	efifb_show_boot_graphics(info);
509 
510 	pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
511 	       efifb_fix.smem_start, size_remap/1024, size_total/1024);
512 	pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
513 	       efifb_defined.xres, efifb_defined.yres,
514 	       efifb_defined.bits_per_pixel, efifb_fix.line_length,
515 	       screen_info.pages);
516 
517 	efifb_defined.xres_virtual = efifb_defined.xres;
518 	efifb_defined.yres_virtual = efifb_fix.smem_len /
519 					efifb_fix.line_length;
520 	pr_info("efifb: scrolling: redraw\n");
521 	efifb_defined.yres_virtual = efifb_defined.yres;
522 
523 	/* some dummy values for timing to make fbset happy */
524 	efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
525 					1000 / efifb_defined.yres;
526 	efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
527 	efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
528 
529 	efifb_defined.red.offset    = screen_info.red_pos;
530 	efifb_defined.red.length    = screen_info.red_size;
531 	efifb_defined.green.offset  = screen_info.green_pos;
532 	efifb_defined.green.length  = screen_info.green_size;
533 	efifb_defined.blue.offset   = screen_info.blue_pos;
534 	efifb_defined.blue.length   = screen_info.blue_size;
535 	efifb_defined.transp.offset = screen_info.rsvd_pos;
536 	efifb_defined.transp.length = screen_info.rsvd_size;
537 
538 	pr_info("efifb: %s: "
539 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
540 	       "Truecolor",
541 	       screen_info.rsvd_size,
542 	       screen_info.red_size,
543 	       screen_info.green_size,
544 	       screen_info.blue_size,
545 	       screen_info.rsvd_pos,
546 	       screen_info.red_pos,
547 	       screen_info.green_pos,
548 	       screen_info.blue_pos);
549 
550 	efifb_fix.ypanstep  = 0;
551 	efifb_fix.ywrapstep = 0;
552 
553 	info->fbops = &efifb_ops;
554 	info->var = efifb_defined;
555 	info->fix = efifb_fix;
556 
557 	orientation = drm_get_panel_orientation_quirk(efifb_defined.xres,
558 						      efifb_defined.yres);
559 	switch (orientation) {
560 	default:
561 		info->fbcon_rotate_hint = FB_ROTATE_UR;
562 		break;
563 	case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
564 		info->fbcon_rotate_hint = FB_ROTATE_UD;
565 		break;
566 	case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
567 		info->fbcon_rotate_hint = FB_ROTATE_CCW;
568 		break;
569 	case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
570 		info->fbcon_rotate_hint = FB_ROTATE_CW;
571 		break;
572 	}
573 
574 	err = sysfs_create_groups(&dev->dev.kobj, efifb_groups);
575 	if (err) {
576 		pr_err("efifb: cannot add sysfs attrs\n");
577 		goto err_unmap;
578 	}
579 	err = fb_alloc_cmap(&info->cmap, 256, 0);
580 	if (err < 0) {
581 		pr_err("efifb: cannot allocate colormap\n");
582 		goto err_groups;
583 	}
584 
585 	if (efifb_pci_dev)
586 		WARN_ON(pm_runtime_get_sync(&efifb_pci_dev->dev) < 0);
587 
588 	err = devm_aperture_acquire_for_platform_device(dev, par->base, par->size);
589 	if (err) {
590 		pr_err("efifb: cannot acquire aperture\n");
591 		goto err_put_rpm_ref;
592 	}
593 	err = register_framebuffer(info);
594 	if (err < 0) {
595 		pr_err("efifb: cannot register framebuffer\n");
596 		goto err_put_rpm_ref;
597 	}
598 	fb_info(info, "%s frame buffer device\n", info->fix.id);
599 	return 0;
600 
601 err_put_rpm_ref:
602 	if (efifb_pci_dev)
603 		pm_runtime_put(&efifb_pci_dev->dev);
604 
605 	fb_dealloc_cmap(&info->cmap);
606 err_groups:
607 	sysfs_remove_groups(&dev->dev.kobj, efifb_groups);
608 err_unmap:
609 	if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
610 		iounmap(info->screen_base);
611 	else
612 		memunmap(info->screen_base);
613 err_release_fb:
614 	framebuffer_release(info);
615 err_release_mem:
616 	if (request_mem_succeeded)
617 		release_mem_region(efifb_fix.smem_start, size_total);
618 	return err;
619 }
620 
efifb_remove(struct platform_device * pdev)621 static void efifb_remove(struct platform_device *pdev)
622 {
623 	struct fb_info *info = platform_get_drvdata(pdev);
624 
625 	/* efifb_destroy takes care of info cleanup */
626 	unregister_framebuffer(info);
627 	sysfs_remove_groups(&pdev->dev.kobj, efifb_groups);
628 }
629 
630 static struct platform_driver efifb_driver = {
631 	.driver = {
632 		.name = "efi-framebuffer",
633 	},
634 	.probe = efifb_probe,
635 	.remove_new = efifb_remove,
636 };
637 
638 builtin_platform_driver(efifb_driver);
639 
640 #if defined(CONFIG_PCI)
641 
record_efifb_bar_resource(struct pci_dev * dev,int idx,u64 offset)642 static void record_efifb_bar_resource(struct pci_dev *dev, int idx, u64 offset)
643 {
644 	u16 word;
645 
646 	efifb_pci_dev = dev;
647 
648 	pci_read_config_word(dev, PCI_COMMAND, &word);
649 	if (!(word & PCI_COMMAND_MEMORY)) {
650 		pci_dev_disabled = true;
651 		dev_err(&dev->dev,
652 			"BAR %d: assigned to efifb but device is disabled!\n",
653 			idx);
654 		return;
655 	}
656 
657 	bar_resource = &dev->resource[idx];
658 	bar_offset = offset;
659 
660 	dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
661 }
662 
efifb_fixup_resources(struct pci_dev * dev)663 static void efifb_fixup_resources(struct pci_dev *dev)
664 {
665 	u64 base = screen_info.lfb_base;
666 	u64 size = screen_info.lfb_size;
667 	int i;
668 
669 	if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
670 		return;
671 
672 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
673 		base |= (u64)screen_info.ext_lfb_base << 32;
674 
675 	if (!base)
676 		return;
677 
678 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
679 		struct resource *res = &dev->resource[i];
680 
681 		if (!(res->flags & IORESOURCE_MEM))
682 			continue;
683 
684 		if (res->start <= base && res->end >= base + size - 1) {
685 			record_efifb_bar_resource(dev, i, base - res->start);
686 			break;
687 		}
688 	}
689 }
690 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
691 			       16, efifb_fixup_resources);
692 
693 #endif
694