blob: e57c00824965ca6656b456ab733b0a5ef7ee5c93 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Huang, Ying7c831722007-11-28 16:21:55 -08002/*
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
Huang, Ying7c831722007-11-28 16:21:55 -080010#include <linux/kernel.h>
Ard Biesheuvel21289ec2016-04-25 21:06:50 +010011#include <linux/efi.h>
Hans de Goede34db50e2018-07-03 17:43:10 +020012#include <linux/efi-bgrt.h>
Huang, Ying7c831722007-11-28 16:21:55 -080013#include <linux/errno.h>
14#include <linux/fb.h>
Ard Biesheuvel55d728a2017-04-04 16:27:44 +010015#include <linux/pci.h>
Huang, Ying7c831722007-11-28 16:21:55 -080016#include <linux/platform_device.h>
Hans de Goede34db50e2018-07-03 17:43:10 +020017#include <linux/printk.h>
Huang, Ying7c831722007-11-28 16:21:55 -080018#include <linux/screen_info.h>
Huang, Ying7c831722007-11-28 16:21:55 -080019#include <video/vga.h>
Ard Biesheuvel21289ec2016-04-25 21:06:50 +010020#include <asm/efi.h>
Hans de Goede028b1862017-11-25 20:35:52 +010021#include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */
22#include <drm/drm_connector.h> /* For DRM_MODE_PANEL_ORIENTATION_* */
Huang, Ying7c831722007-11-28 16:21:55 -080023
Hans de Goede34db50e2018-07-03 17:43:10 +020024struct bmp_file_header {
25 u16 id;
26 u32 file_size;
27 u32 reserved;
28 u32 bitmap_offset;
29} __packed;
30
31struct 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
Hans de Goedecf7389b2018-09-26 18:11:22 +020045static bool use_bgrt = true;
Andy Lutomirskida0241f2011-05-26 10:13:32 -040046static bool request_mem_succeeded = false;
Ard Biesheuvel38ac0282018-07-11 11:40:40 +020047static u64 mem_flags = EFI_MEMORY_WC | EFI_MEMORY_UC;
Andy Lutomirskida0241f2011-05-26 10:13:32 -040048
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080049static struct fb_var_screeninfo efifb_defined = {
Huang, Ying7c831722007-11-28 16:21:55 -080050 .activate = FB_ACTIVATE_NOW,
51 .height = -1,
52 .width = -1,
53 .right_margin = 32,
54 .upper_margin = 16,
55 .lower_margin = 4,
56 .vsync_len = 4,
57 .vmode = FB_VMODE_NONINTERLACED,
58};
59
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080060static struct fb_fix_screeninfo efifb_fix = {
Huang, Ying7c831722007-11-28 16:21:55 -080061 .id = "EFI VGA",
62 .type = FB_TYPE_PACKED_PIXELS,
63 .accel = FB_ACCEL_NONE,
64 .visual = FB_VISUAL_TRUECOLOR,
65};
66
67static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
68 unsigned blue, unsigned transp,
69 struct fb_info *info)
70{
71 /*
72 * Set a single color register. The values supplied are
73 * already rounded down to the hardware's capabilities
74 * (according to the entries in the `var' structure). Return
75 * != 0 for invalid regno.
76 */
77
78 if (regno >= info->cmap.len)
79 return 1;
80
81 if (regno < 16) {
Max Staudtd50b3f42016-06-13 19:15:59 +020082 red >>= 16 - info->var.red.length;
83 green >>= 16 - info->var.green.length;
84 blue >>= 16 - info->var.blue.length;
Huang, Ying7c831722007-11-28 16:21:55 -080085 ((u32 *)(info->pseudo_palette))[regno] =
86 (red << info->var.red.offset) |
87 (green << info->var.green.offset) |
88 (blue << info->var.blue.offset);
89 }
90 return 0;
91}
92
Hans de Goede34db50e2018-07-03 17:43:10 +020093/*
94 * If fbcon deffered console takeover is configured, the intent is for the
95 * framebuffer to show the boot graphics (e.g. vendor logo) until there is some
96 * (error) message to display. But the boot graphics may have been destroyed by
97 * e.g. option ROM output, detect this and restore the boot graphics.
98 */
99#if defined CONFIG_FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER && \
100 defined CONFIG_ACPI_BGRT
101static void efifb_copy_bmp(u8 *src, u32 *dst, int width, struct screen_info *si)
102{
103 u8 r, g, b;
104
105 while (width--) {
106 b = *src++;
107 g = *src++;
108 r = *src++;
109 *dst++ = (r << si->red_pos) |
110 (g << si->green_pos) |
111 (b << si->blue_pos);
112 }
113}
114
Hans de Goede88fe4ce2018-07-24 19:11:28 +0200115#ifdef CONFIG_X86
116/*
117 * On x86 some firmwares use a low non native resolution for the display when
118 * they have shown some text messages. While keeping the bgrt filled with info
119 * for the native resolution. If the bgrt image intended for the native
120 * resolution still fits, it will be displayed very close to the right edge of
121 * the display looking quite bad. This function checks for this.
122 */
123static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width)
124{
Hans de Goede51677df2019-07-21 15:19:18 +0200125 /*
126 * All x86 firmwares horizontally center the image (the yoffset
127 * calculations differ between boards, but xoffset is predictable).
128 */
129 u32 expected_xoffset = (si->lfb_width - bmp_width) / 2;
Hans de Goede88fe4ce2018-07-24 19:11:28 +0200130
Hans de Goede51677df2019-07-21 15:19:18 +0200131 return bgrt_tab.image_offset_x == expected_xoffset;
Hans de Goede88fe4ce2018-07-24 19:11:28 +0200132}
133#else
134static bool efifb_bgrt_sanity_check(struct screen_info *si, u32 bmp_width)
135{
136 return true;
137}
138#endif
139
Hans de Goede34db50e2018-07-03 17:43:10 +0200140static void efifb_show_boot_graphics(struct fb_info *info)
141{
142 u32 bmp_width, bmp_height, bmp_pitch, screen_pitch, dst_x, y, src_y;
143 struct screen_info *si = &screen_info;
144 struct bmp_file_header *file_header;
145 struct bmp_dib_header *dib_header;
146 void *bgrt_image = NULL;
147 u8 *dst = info->screen_base;
148
Hans de Goedecf7389b2018-09-26 18:11:22 +0200149 if (!use_bgrt)
150 return;
151
Hans de Goede34db50e2018-07-03 17:43:10 +0200152 if (!bgrt_tab.image_address) {
153 pr_info("efifb: No BGRT, not showing boot graphics\n");
154 return;
155 }
156
Hans de Goedeb6b1d5112019-06-21 15:13:14 +0200157 if (bgrt_tab.status & 0x06) {
158 pr_info("efifb: BGRT rotation bits set, not showing boot graphics\n");
159 return;
160 }
161
Hans de Goede34db50e2018-07-03 17:43:10 +0200162 /* Avoid flashing the logo if we're going to print std probe messages */
163 if (console_loglevel > CONSOLE_LOGLEVEL_QUIET)
164 return;
165
166 /* bgrt_tab.status is unreliable, so we don't check it */
167
168 if (si->lfb_depth != 32) {
169 pr_info("efifb: not 32 bits, not showing boot graphics\n");
170 return;
171 }
172
173 bgrt_image = memremap(bgrt_tab.image_address, bgrt_image_size,
174 MEMREMAP_WB);
175 if (!bgrt_image) {
176 pr_warn("efifb: Ignoring BGRT: failed to map image memory\n");
177 return;
178 }
179
180 if (bgrt_image_size < (sizeof(*file_header) + sizeof(*dib_header)))
181 goto error;
182
183 file_header = bgrt_image;
184 if (file_header->id != 0x4d42 || file_header->reserved != 0)
185 goto error;
186
187 dib_header = bgrt_image + sizeof(*file_header);
188 if (dib_header->dib_header_size != 40 || dib_header->width < 0 ||
189 dib_header->planes != 1 || dib_header->bpp != 24 ||
190 dib_header->compression != 0)
191 goto error;
192
193 bmp_width = dib_header->width;
194 bmp_height = abs(dib_header->height);
195 bmp_pitch = round_up(3 * bmp_width, 4);
196 screen_pitch = si->lfb_linelength;
197
198 if ((file_header->bitmap_offset + bmp_pitch * bmp_height) >
199 bgrt_image_size)
200 goto error;
201
202 if ((bgrt_tab.image_offset_x + bmp_width) > si->lfb_width ||
203 (bgrt_tab.image_offset_y + bmp_height) > si->lfb_height)
204 goto error;
205
Hans de Goede88fe4ce2018-07-24 19:11:28 +0200206 if (!efifb_bgrt_sanity_check(si, bmp_width))
207 goto error;
208
Hans de Goede34db50e2018-07-03 17:43:10 +0200209 pr_info("efifb: showing boot graphics\n");
210
211 for (y = 0; y < si->lfb_height; y++, dst += si->lfb_linelength) {
212 /* Only background? */
213 if (y < bgrt_tab.image_offset_y ||
214 y >= (bgrt_tab.image_offset_y + bmp_height)) {
215 memset(dst, 0, 4 * si->lfb_width);
216 continue;
217 }
218
219 src_y = y - bgrt_tab.image_offset_y;
220 /* Positive header height means upside down row order */
221 if (dib_header->height > 0)
222 src_y = (bmp_height - 1) - src_y;
223
224 memset(dst, 0, bgrt_tab.image_offset_x * 4);
225 dst_x = bgrt_tab.image_offset_x;
226 efifb_copy_bmp(bgrt_image + file_header->bitmap_offset +
227 src_y * bmp_pitch,
228 (u32 *)dst + dst_x, bmp_width, si);
229 dst_x += bmp_width;
230 memset((u32 *)dst + dst_x, 0, (si->lfb_width - dst_x) * 4);
231 }
232
233 memunmap(bgrt_image);
234 return;
235
236error:
237 memunmap(bgrt_image);
238 pr_warn("efifb: Ignoring BGRT: unexpected or invalid BMP data\n");
239}
240#else
241static inline void efifb_show_boot_graphics(struct fb_info *info) {}
242#endif
243
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800244static void efifb_destroy(struct fb_info *info)
245{
Ard Biesheuvel38ac0282018-07-11 11:40:40 +0200246 if (info->screen_base) {
247 if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
248 iounmap(info->screen_base);
249 else
250 memunmap(info->screen_base);
251 }
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400252 if (request_mem_succeeded)
253 release_mem_region(info->apertures->ranges[0].base,
254 info->apertures->ranges[0].size);
Peter Jones907355a2013-07-25 11:48:11 -0400255 fb_dealloc_cmap(&info->cmap);
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800256}
257
Jani Nikula8a48ac332019-12-03 18:38:50 +0200258static const struct fb_ops efifb_ops = {
Huang, Ying7c831722007-11-28 16:21:55 -0800259 .owner = THIS_MODULE,
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800260 .fb_destroy = efifb_destroy,
Huang, Ying7c831722007-11-28 16:21:55 -0800261 .fb_setcolreg = efifb_setcolreg,
262 .fb_fillrect = cfb_fillrect,
263 .fb_copyarea = cfb_copyarea,
264 .fb_imageblit = cfb_imageblit,
265};
266
David Herrmanne6816a82013-08-02 14:05:26 +0200267static int efifb_setup(char *options)
Peter Jones7c08c9a2008-10-15 22:03:43 -0700268{
269 char *this_opt;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700270
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400271 if (options && *options) {
272 while ((this_opt = strsep(&options, ",")) != NULL) {
273 if (!*this_opt) continue;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700274
Ard Biesheuvel21289ec2016-04-25 21:06:50 +0100275 efifb_setup_from_dmi(&screen_info, this_opt);
276
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400277 if (!strncmp(this_opt, "base:", 5))
278 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
279 else if (!strncmp(this_opt, "stride:", 7))
280 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
281 else if (!strncmp(this_opt, "height:", 7))
282 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
283 else if (!strncmp(this_opt, "width:", 6))
284 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
Dave Airliedd0c41f2017-07-31 18:45:41 +0200285 else if (!strcmp(this_opt, "nowc"))
Ard Biesheuvel38ac0282018-07-11 11:40:40 +0200286 mem_flags &= ~EFI_MEMORY_WC;
Hans de Goedecf7389b2018-09-26 18:11:22 +0200287 else if (!strcmp(this_opt, "nobgrt"))
288 use_bgrt = false;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700289 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700290 }
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400291
Peter Jones7c08c9a2008-10-15 22:03:43 -0700292 return 0;
293}
294
Matt Flemingae2ee622015-08-25 16:32:55 +0100295static inline bool fb_base_is_valid(void)
296{
297 if (screen_info.lfb_base)
298 return true;
299
300 if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE))
301 return false;
302
303 if (screen_info.ext_lfb_base)
304 return true;
305
306 return false;
307}
308
Peter Jones753375a2016-10-18 15:33:17 +0100309#define efifb_attr_decl(name, fmt) \
310static ssize_t name##_show(struct device *dev, \
311 struct device_attribute *attr, \
312 char *buf) \
313{ \
314 return sprintf(buf, fmt "\n", (screen_info.lfb_##name)); \
315} \
316static DEVICE_ATTR_RO(name)
317
318efifb_attr_decl(base, "0x%x");
319efifb_attr_decl(linelength, "%u");
320efifb_attr_decl(height, "%u");
321efifb_attr_decl(width, "%u");
322efifb_attr_decl(depth, "%u");
323
324static struct attribute *efifb_attrs[] = {
325 &dev_attr_base.attr,
326 &dev_attr_linelength.attr,
327 &dev_attr_width.attr,
328 &dev_attr_height.attr,
329 &dev_attr_depth.attr,
330 NULL
331};
332ATTRIBUTE_GROUPS(efifb);
333
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100334static bool pci_dev_disabled; /* FB base matches BAR of a disabled device */
335
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100336static struct pci_dev *efifb_pci_dev; /* dev with BAR covering the efifb */
337static struct resource *bar_resource;
338static u64 bar_offset;
339
David Herrmanne6816a82013-08-02 14:05:26 +0200340static int efifb_probe(struct platform_device *dev)
Huang, Ying7c831722007-11-28 16:21:55 -0800341{
342 struct fb_info *info;
Hans de Goede028b1862017-11-25 20:35:52 +0100343 int err, orientation;
Huang, Ying7c831722007-11-28 16:21:55 -0800344 unsigned int size_vmode;
345 unsigned int size_remap;
346 unsigned int size_total;
David Herrmanne6816a82013-08-02 14:05:26 +0200347 char *option = NULL;
Ard Biesheuvel38ac0282018-07-11 11:40:40 +0200348 efi_memory_desc_t md;
David Herrmanne6816a82013-08-02 14:05:26 +0200349
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100350 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
David Herrmanne6816a82013-08-02 14:05:26 +0200351 return -ENODEV;
352
353 if (fb_get_options("efifb", &option))
354 return -ENODEV;
355 efifb_setup(option);
356
357 /* We don't get linelength from UGA Draw Protocol, only from
358 * EFI Graphics Protocol. So if it's not in DMI, and it's not
359 * passed in from the user, we really can't use the framebuffer.
360 */
361 if (!screen_info.lfb_linelength)
362 return -ENODEV;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700363
Peter Jones7c08c9a2008-10-15 22:03:43 -0700364 if (!screen_info.lfb_depth)
365 screen_info.lfb_depth = 32;
366 if (!screen_info.pages)
367 screen_info.pages = 1;
Matt Flemingae2ee622015-08-25 16:32:55 +0100368 if (!fb_base_is_valid()) {
Matthew Garrett133bb072009-04-13 14:39:44 -0700369 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
370 return -ENODEV;
371 }
372 printk(KERN_INFO "efifb: probing for efifb\n");
Peter Jones7c08c9a2008-10-15 22:03:43 -0700373
374 /* just assume they're all unset if any are */
375 if (!screen_info.blue_size) {
376 screen_info.blue_size = 8;
377 screen_info.blue_pos = 0;
378 screen_info.green_size = 8;
379 screen_info.green_pos = 8;
380 screen_info.red_size = 8;
381 screen_info.red_pos = 16;
382 screen_info.rsvd_size = 8;
383 screen_info.rsvd_pos = 24;
384 }
Huang, Ying7c831722007-11-28 16:21:55 -0800385
386 efifb_fix.smem_start = screen_info.lfb_base;
Matt Flemingae2ee622015-08-25 16:32:55 +0100387
388 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
389 u64 ext_lfb_base;
390
391 ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
392 efifb_fix.smem_start |= ext_lfb_base;
393 }
394
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100395 if (bar_resource &&
396 bar_resource->start + bar_offset != efifb_fix.smem_start) {
397 dev_info(&efifb_pci_dev->dev,
398 "BAR has moved, updating efifb address\n");
399 efifb_fix.smem_start = bar_resource->start + bar_offset;
400 }
401
Huang, Ying7c831722007-11-28 16:21:55 -0800402 efifb_defined.bits_per_pixel = screen_info.lfb_depth;
403 efifb_defined.xres = screen_info.lfb_width;
404 efifb_defined.yres = screen_info.lfb_height;
405 efifb_fix.line_length = screen_info.lfb_linelength;
406
407 /* size_vmode -- that is the amount of memory needed for the
408 * used video mode, i.e. the minimum amount of
409 * memory we need. */
410 size_vmode = efifb_defined.yres * efifb_fix.line_length;
411
412 /* size_total -- all video memory we have. Used for
413 * entries, ressource allocation and bounds
414 * checking. */
415 size_total = screen_info.lfb_size;
416 if (size_total < size_vmode)
417 size_total = size_vmode;
418
419 /* size_remap -- the amount of video memory we are going to
420 * use for efifb. With modern cards it is no
421 * option to simply use size_total as that
422 * wastes plenty of kernel address space. */
423 size_remap = size_vmode * 2;
Huang, Ying7c831722007-11-28 16:21:55 -0800424 if (size_remap > size_total)
425 size_remap = size_total;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700426 if (size_remap % PAGE_SIZE)
427 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
Huang, Ying7c831722007-11-28 16:21:55 -0800428 efifb_fix.smem_len = size_remap;
429
Peter Jones7c08c9a2008-10-15 22:03:43 -0700430 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400431 request_mem_succeeded = true;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700432 } else {
Huang, Ying7c831722007-11-28 16:21:55 -0800433 /* We cannot make this fatal. Sometimes this comes from magic
434 spaces our resource handlers simply don't know about */
Peter Jones753375a2016-10-18 15:33:17 +0100435 pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
Huang, Ying7c831722007-11-28 16:21:55 -0800436 efifb_fix.smem_start);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700437 }
Huang, Ying7c831722007-11-28 16:21:55 -0800438
439 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
440 if (!info) {
441 err = -ENOMEM;
442 goto err_release_mem;
443 }
David Herrmann65b40212014-01-23 15:14:55 +0100444 platform_set_drvdata(dev, info);
Huang, Ying7c831722007-11-28 16:21:55 -0800445 info->pseudo_palette = info->par;
446 info->par = NULL;
447
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200448 info->apertures = alloc_apertures(1);
449 if (!info->apertures) {
450 err = -ENOMEM;
451 goto err_release_fb;
452 }
453 info->apertures->ranges[0].base = efifb_fix.smem_start;
454 info->apertures->ranges[0].size = size_remap;
Dave Airlie4410f392009-06-16 15:34:38 -0700455
Juergen Gross6163a982020-07-10 16:16:51 +0200456 if (efi_enabled(EFI_MEMMAP) &&
Ard Biesheuvelc2999c22019-03-28 20:34:26 +0100457 !efi_mem_desc_lookup(efifb_fix.smem_start, &md)) {
Ard Biesheuvel38ac0282018-07-11 11:40:40 +0200458 if ((efifb_fix.smem_start + efifb_fix.smem_len) >
459 (md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT))) {
460 pr_err("efifb: video memory @ 0x%lx spans multiple EFI memory regions\n",
461 efifb_fix.smem_start);
462 err = -EIO;
463 goto err_release_fb;
464 }
465 /*
466 * If the UEFI memory map covers the efifb region, we may only
467 * remap it using the attributes the memory map prescribes.
468 */
Ard Biesheuvelf8585532019-05-16 23:31:59 +0200469 md.attribute &= EFI_MEMORY_UC | EFI_MEMORY_WC |
470 EFI_MEMORY_WT | EFI_MEMORY_WB;
471 if (md.attribute) {
472 mem_flags |= EFI_MEMORY_WT | EFI_MEMORY_WB;
473 mem_flags &= md.attribute;
474 }
Ard Biesheuvel38ac0282018-07-11 11:40:40 +0200475 }
476 if (mem_flags & EFI_MEMORY_WC)
477 info->screen_base = ioremap_wc(efifb_fix.smem_start,
478 efifb_fix.smem_len);
479 else if (mem_flags & EFI_MEMORY_UC)
480 info->screen_base = ioremap(efifb_fix.smem_start,
481 efifb_fix.smem_len);
482 else if (mem_flags & EFI_MEMORY_WT)
483 info->screen_base = memremap(efifb_fix.smem_start,
484 efifb_fix.smem_len, MEMREMAP_WT);
485 else if (mem_flags & EFI_MEMORY_WB)
486 info->screen_base = memremap(efifb_fix.smem_start,
487 efifb_fix.smem_len, MEMREMAP_WB);
Huang, Ying7c831722007-11-28 16:21:55 -0800488 if (!info->screen_base) {
Ard Biesheuvel38ac0282018-07-11 11:40:40 +0200489 pr_err("efifb: abort, cannot remap video memory 0x%x @ 0x%lx\n",
Huang, Ying7c831722007-11-28 16:21:55 -0800490 efifb_fix.smem_len, efifb_fix.smem_start);
491 err = -EIO;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700492 goto err_release_fb;
Huang, Ying7c831722007-11-28 16:21:55 -0800493 }
494
Hans de Goede34db50e2018-07-03 17:43:10 +0200495 efifb_show_boot_graphics(info);
496
Peter Jones753375a2016-10-18 15:33:17 +0100497 pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
Andy Lutomirskia53dd252016-05-11 16:57:47 -0700498 efifb_fix.smem_start, size_remap/1024, size_total/1024);
Peter Jones753375a2016-10-18 15:33:17 +0100499 pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
Huang, Ying7c831722007-11-28 16:21:55 -0800500 efifb_defined.xres, efifb_defined.yres,
501 efifb_defined.bits_per_pixel, efifb_fix.line_length,
502 screen_info.pages);
503
504 efifb_defined.xres_virtual = efifb_defined.xres;
505 efifb_defined.yres_virtual = efifb_fix.smem_len /
506 efifb_fix.line_length;
Peter Jones753375a2016-10-18 15:33:17 +0100507 pr_info("efifb: scrolling: redraw\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800508 efifb_defined.yres_virtual = efifb_defined.yres;
509
510 /* some dummy values for timing to make fbset happy */
511 efifb_defined.pixclock = 10000000 / efifb_defined.xres *
512 1000 / efifb_defined.yres;
513 efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
514 efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
515
516 efifb_defined.red.offset = screen_info.red_pos;
517 efifb_defined.red.length = screen_info.red_size;
518 efifb_defined.green.offset = screen_info.green_pos;
519 efifb_defined.green.length = screen_info.green_size;
520 efifb_defined.blue.offset = screen_info.blue_pos;
521 efifb_defined.blue.length = screen_info.blue_size;
522 efifb_defined.transp.offset = screen_info.rsvd_pos;
523 efifb_defined.transp.length = screen_info.rsvd_size;
524
Peter Jones753375a2016-10-18 15:33:17 +0100525 pr_info("efifb: %s: "
Huang, Ying7c831722007-11-28 16:21:55 -0800526 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
527 "Truecolor",
528 screen_info.rsvd_size,
529 screen_info.red_size,
530 screen_info.green_size,
531 screen_info.blue_size,
532 screen_info.rsvd_pos,
533 screen_info.red_pos,
534 screen_info.green_pos,
535 screen_info.blue_pos);
536
537 efifb_fix.ypanstep = 0;
538 efifb_fix.ywrapstep = 0;
539
540 info->fbops = &efifb_ops;
541 info->var = efifb_defined;
542 info->fix = efifb_fix;
Dave Airlie4410f392009-06-16 15:34:38 -0700543 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
Huang, Ying7c831722007-11-28 16:21:55 -0800544
Hans de Goede028b1862017-11-25 20:35:52 +0100545 orientation = drm_get_panel_orientation_quirk(efifb_defined.xres,
546 efifb_defined.yres);
547 switch (orientation) {
548 default:
549 info->fbcon_rotate_hint = FB_ROTATE_UR;
550 break;
551 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
552 info->fbcon_rotate_hint = FB_ROTATE_UD;
553 break;
554 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
555 info->fbcon_rotate_hint = FB_ROTATE_CCW;
556 break;
557 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
558 info->fbcon_rotate_hint = FB_ROTATE_CW;
559 break;
560 }
561
Peter Jones753375a2016-10-18 15:33:17 +0100562 err = sysfs_create_groups(&dev->dev.kobj, efifb_groups);
563 if (err) {
564 pr_err("efifb: cannot add sysfs attrs\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800565 goto err_unmap;
566 }
Peter Jones753375a2016-10-18 15:33:17 +0100567 err = fb_alloc_cmap(&info->cmap, 256, 0);
568 if (err < 0) {
569 pr_err("efifb: cannot allocate colormap\n");
570 goto err_groups;
571 }
572 err = register_framebuffer(info);
573 if (err < 0) {
574 pr_err("efifb: cannot register framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800575 goto err_fb_dealoc;
576 }
Joe Perches31b67802013-09-19 18:35:55 -0700577 fb_info(info, "%s frame buffer device\n", info->fix.id);
Huang, Ying7c831722007-11-28 16:21:55 -0800578 return 0;
579
580err_fb_dealoc:
581 fb_dealloc_cmap(&info->cmap);
Peter Jones753375a2016-10-18 15:33:17 +0100582err_groups:
583 sysfs_remove_groups(&dev->dev.kobj, efifb_groups);
Huang, Ying7c831722007-11-28 16:21:55 -0800584err_unmap:
Ard Biesheuvel38ac0282018-07-11 11:40:40 +0200585 if (mem_flags & (EFI_MEMORY_UC | EFI_MEMORY_WC))
586 iounmap(info->screen_base);
587 else
588 memunmap(info->screen_base);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700589err_release_fb:
Huang, Ying7c831722007-11-28 16:21:55 -0800590 framebuffer_release(info);
591err_release_mem:
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400592 if (request_mem_succeeded)
Peter Jones7c08c9a2008-10-15 22:03:43 -0700593 release_mem_region(efifb_fix.smem_start, size_total);
Huang, Ying7c831722007-11-28 16:21:55 -0800594 return err;
595}
596
David Herrmann65b40212014-01-23 15:14:55 +0100597static int efifb_remove(struct platform_device *pdev)
598{
599 struct fb_info *info = platform_get_drvdata(pdev);
600
601 unregister_framebuffer(info);
Peter Jones753375a2016-10-18 15:33:17 +0100602 sysfs_remove_groups(&pdev->dev.kobj, efifb_groups);
David Herrmann65b40212014-01-23 15:14:55 +0100603 framebuffer_release(info);
604
605 return 0;
606}
607
Huang, Ying7c831722007-11-28 16:21:55 -0800608static struct platform_driver efifb_driver = {
David Herrmanne6816a82013-08-02 14:05:26 +0200609 .driver = {
610 .name = "efi-framebuffer",
Huang, Ying7c831722007-11-28 16:21:55 -0800611 },
David Herrmanne6816a82013-08-02 14:05:26 +0200612 .probe = efifb_probe,
David Herrmann65b40212014-01-23 15:14:55 +0100613 .remove = efifb_remove,
Huang, Ying7c831722007-11-28 16:21:55 -0800614};
615
Ard Biesheuvel07ea7ec2016-04-25 21:06:51 +0100616builtin_platform_driver(efifb_driver);
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100617
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100618#if defined(CONFIG_PCI)
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100619
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100620static void record_efifb_bar_resource(struct pci_dev *dev, int idx, u64 offset)
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100621{
622 u16 word;
623
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100624 efifb_pci_dev = dev;
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100625
626 pci_read_config_word(dev, PCI_COMMAND, &word);
627 if (!(word & PCI_COMMAND_MEMORY)) {
628 pci_dev_disabled = true;
629 dev_err(&dev->dev,
630 "BAR %d: assigned to efifb but device is disabled!\n",
631 idx);
632 return;
633 }
634
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100635 bar_resource = &dev->resource[idx];
636 bar_offset = offset;
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100637
638 dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
639}
640
641static void efifb_fixup_resources(struct pci_dev *dev)
642{
643 u64 base = screen_info.lfb_base;
644 u64 size = screen_info.lfb_size;
645 int i;
646
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100647 if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100648 return;
649
650 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
651 base |= (u64)screen_info.ext_lfb_base << 32;
652
653 if (!base)
654 return;
655
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300656 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100657 struct resource *res = &dev->resource[i];
658
659 if (!(res->flags & IORESOURCE_MEM))
660 continue;
661
662 if (res->start <= base && res->end >= base + size - 1) {
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100663 record_efifb_bar_resource(dev, i, base - res->start);
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100664 break;
665 }
666 }
667}
668DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
669 16, efifb_fixup_resources);
670
671#endif