1 // SPDX-License-Identifier: GPL-2.0
3 * Framebuffer driver for EFI/UEFI based system
5 * (c) 2006 Edgar Hucek <gimli@dark-green.com>
6 * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
10 #include <linux/kernel.h>
11 #include <linux/efi.h>
12 #include <linux/errno.h>
14 #include <linux/pci.h>
15 #include <linux/platform_device.h>
16 #include <linux/screen_info.h>
17 #include <video/vga.h>
19 #include <drm/drm_utils.h> /* For drm_get_panel_orientation_quirk */
20 #include <drm/drm_connector.h> /* For DRM_MODE_PANEL_ORIENTATION_* */
22 static bool request_mem_succeeded
= false;
23 static bool nowc
= false;
25 static struct fb_var_screeninfo efifb_defined
= {
26 .activate
= FB_ACTIVATE_NOW
,
33 .vmode
= FB_VMODE_NONINTERLACED
,
36 static struct fb_fix_screeninfo efifb_fix
= {
38 .type
= FB_TYPE_PACKED_PIXELS
,
39 .accel
= FB_ACCEL_NONE
,
40 .visual
= FB_VISUAL_TRUECOLOR
,
43 static int efifb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
44 unsigned blue
, unsigned transp
,
48 * Set a single color register. The values supplied are
49 * already rounded down to the hardware's capabilities
50 * (according to the entries in the `var' structure). Return
51 * != 0 for invalid regno.
54 if (regno
>= info
->cmap
.len
)
58 red
>>= 16 - info
->var
.red
.length
;
59 green
>>= 16 - info
->var
.green
.length
;
60 blue
>>= 16 - info
->var
.blue
.length
;
61 ((u32
*)(info
->pseudo_palette
))[regno
] =
62 (red
<< info
->var
.red
.offset
) |
63 (green
<< info
->var
.green
.offset
) |
64 (blue
<< info
->var
.blue
.offset
);
69 static void efifb_destroy(struct fb_info
*info
)
71 if (info
->screen_base
)
72 iounmap(info
->screen_base
);
73 if (request_mem_succeeded
)
74 release_mem_region(info
->apertures
->ranges
[0].base
,
75 info
->apertures
->ranges
[0].size
);
76 fb_dealloc_cmap(&info
->cmap
);
79 static struct fb_ops efifb_ops
= {
81 .fb_destroy
= efifb_destroy
,
82 .fb_setcolreg
= efifb_setcolreg
,
83 .fb_fillrect
= cfb_fillrect
,
84 .fb_copyarea
= cfb_copyarea
,
85 .fb_imageblit
= cfb_imageblit
,
88 static int efifb_setup(char *options
)
92 if (options
&& *options
) {
93 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
94 if (!*this_opt
) continue;
96 efifb_setup_from_dmi(&screen_info
, this_opt
);
98 if (!strncmp(this_opt
, "base:", 5))
99 screen_info
.lfb_base
= simple_strtoul(this_opt
+5, NULL
, 0);
100 else if (!strncmp(this_opt
, "stride:", 7))
101 screen_info
.lfb_linelength
= simple_strtoul(this_opt
+7, NULL
, 0) * 4;
102 else if (!strncmp(this_opt
, "height:", 7))
103 screen_info
.lfb_height
= simple_strtoul(this_opt
+7, NULL
, 0);
104 else if (!strncmp(this_opt
, "width:", 6))
105 screen_info
.lfb_width
= simple_strtoul(this_opt
+6, NULL
, 0);
106 else if (!strcmp(this_opt
, "nowc"))
114 static inline bool fb_base_is_valid(void)
116 if (screen_info
.lfb_base
)
119 if (!(screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
))
122 if (screen_info
.ext_lfb_base
)
128 #define efifb_attr_decl(name, fmt) \
129 static ssize_t name##_show(struct device *dev, \
130 struct device_attribute *attr, \
133 return sprintf(buf, fmt "\n", (screen_info.lfb_##name)); \
135 static DEVICE_ATTR_RO(name)
137 efifb_attr_decl(base
, "0x%x");
138 efifb_attr_decl(linelength
, "%u");
139 efifb_attr_decl(height
, "%u");
140 efifb_attr_decl(width
, "%u");
141 efifb_attr_decl(depth
, "%u");
143 static struct attribute
*efifb_attrs
[] = {
145 &dev_attr_linelength
.attr
,
146 &dev_attr_width
.attr
,
147 &dev_attr_height
.attr
,
148 &dev_attr_depth
.attr
,
151 ATTRIBUTE_GROUPS(efifb
);
153 static bool pci_dev_disabled
; /* FB base matches BAR of a disabled device */
155 static struct pci_dev
*efifb_pci_dev
; /* dev with BAR covering the efifb */
156 static struct resource
*bar_resource
;
157 static u64 bar_offset
;
159 static int efifb_probe(struct platform_device
*dev
)
161 struct fb_info
*info
;
162 int err
, orientation
;
163 unsigned int size_vmode
;
164 unsigned int size_remap
;
165 unsigned int size_total
;
168 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_EFI
|| pci_dev_disabled
)
171 if (fb_get_options("efifb", &option
))
175 /* We don't get linelength from UGA Draw Protocol, only from
176 * EFI Graphics Protocol. So if it's not in DMI, and it's not
177 * passed in from the user, we really can't use the framebuffer.
179 if (!screen_info
.lfb_linelength
)
182 if (!screen_info
.lfb_depth
)
183 screen_info
.lfb_depth
= 32;
184 if (!screen_info
.pages
)
185 screen_info
.pages
= 1;
186 if (!fb_base_is_valid()) {
187 printk(KERN_DEBUG
"efifb: invalid framebuffer address\n");
190 printk(KERN_INFO
"efifb: probing for efifb\n");
192 /* just assume they're all unset if any are */
193 if (!screen_info
.blue_size
) {
194 screen_info
.blue_size
= 8;
195 screen_info
.blue_pos
= 0;
196 screen_info
.green_size
= 8;
197 screen_info
.green_pos
= 8;
198 screen_info
.red_size
= 8;
199 screen_info
.red_pos
= 16;
200 screen_info
.rsvd_size
= 8;
201 screen_info
.rsvd_pos
= 24;
204 efifb_fix
.smem_start
= screen_info
.lfb_base
;
206 if (screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
) {
209 ext_lfb_base
= (u64
)(unsigned long)screen_info
.ext_lfb_base
<< 32;
210 efifb_fix
.smem_start
|= ext_lfb_base
;
214 bar_resource
->start
+ bar_offset
!= efifb_fix
.smem_start
) {
215 dev_info(&efifb_pci_dev
->dev
,
216 "BAR has moved, updating efifb address\n");
217 efifb_fix
.smem_start
= bar_resource
->start
+ bar_offset
;
220 efifb_defined
.bits_per_pixel
= screen_info
.lfb_depth
;
221 efifb_defined
.xres
= screen_info
.lfb_width
;
222 efifb_defined
.yres
= screen_info
.lfb_height
;
223 efifb_fix
.line_length
= screen_info
.lfb_linelength
;
225 /* size_vmode -- that is the amount of memory needed for the
226 * used video mode, i.e. the minimum amount of
228 size_vmode
= efifb_defined
.yres
* efifb_fix
.line_length
;
230 /* size_total -- all video memory we have. Used for
231 * entries, ressource allocation and bounds
233 size_total
= screen_info
.lfb_size
;
234 if (size_total
< size_vmode
)
235 size_total
= size_vmode
;
237 /* size_remap -- the amount of video memory we are going to
238 * use for efifb. With modern cards it is no
239 * option to simply use size_total as that
240 * wastes plenty of kernel address space. */
241 size_remap
= size_vmode
* 2;
242 if (size_remap
> size_total
)
243 size_remap
= size_total
;
244 if (size_remap
% PAGE_SIZE
)
245 size_remap
+= PAGE_SIZE
- (size_remap
% PAGE_SIZE
);
246 efifb_fix
.smem_len
= size_remap
;
248 if (request_mem_region(efifb_fix
.smem_start
, size_remap
, "efifb")) {
249 request_mem_succeeded
= true;
251 /* We cannot make this fatal. Sometimes this comes from magic
252 spaces our resource handlers simply don't know about */
253 pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
254 efifb_fix
.smem_start
);
257 info
= framebuffer_alloc(sizeof(u32
) * 16, &dev
->dev
);
259 pr_err("efifb: cannot allocate framebuffer\n");
261 goto err_release_mem
;
263 platform_set_drvdata(dev
, info
);
264 info
->pseudo_palette
= info
->par
;
267 info
->apertures
= alloc_apertures(1);
268 if (!info
->apertures
) {
272 info
->apertures
->ranges
[0].base
= efifb_fix
.smem_start
;
273 info
->apertures
->ranges
[0].size
= size_remap
;
276 info
->screen_base
= ioremap(efifb_fix
.smem_start
, efifb_fix
.smem_len
);
278 info
->screen_base
= ioremap_wc(efifb_fix
.smem_start
, efifb_fix
.smem_len
);
279 if (!info
->screen_base
) {
280 pr_err("efifb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
281 efifb_fix
.smem_len
, efifb_fix
.smem_start
);
286 pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
287 efifb_fix
.smem_start
, size_remap
/1024, size_total
/1024);
288 pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
289 efifb_defined
.xres
, efifb_defined
.yres
,
290 efifb_defined
.bits_per_pixel
, efifb_fix
.line_length
,
293 efifb_defined
.xres_virtual
= efifb_defined
.xres
;
294 efifb_defined
.yres_virtual
= efifb_fix
.smem_len
/
295 efifb_fix
.line_length
;
296 pr_info("efifb: scrolling: redraw\n");
297 efifb_defined
.yres_virtual
= efifb_defined
.yres
;
299 /* some dummy values for timing to make fbset happy */
300 efifb_defined
.pixclock
= 10000000 / efifb_defined
.xres
*
301 1000 / efifb_defined
.yres
;
302 efifb_defined
.left_margin
= (efifb_defined
.xres
/ 8) & 0xf8;
303 efifb_defined
.hsync_len
= (efifb_defined
.xres
/ 8) & 0xf8;
305 efifb_defined
.red
.offset
= screen_info
.red_pos
;
306 efifb_defined
.red
.length
= screen_info
.red_size
;
307 efifb_defined
.green
.offset
= screen_info
.green_pos
;
308 efifb_defined
.green
.length
= screen_info
.green_size
;
309 efifb_defined
.blue
.offset
= screen_info
.blue_pos
;
310 efifb_defined
.blue
.length
= screen_info
.blue_size
;
311 efifb_defined
.transp
.offset
= screen_info
.rsvd_pos
;
312 efifb_defined
.transp
.length
= screen_info
.rsvd_size
;
314 pr_info("efifb: %s: "
315 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
317 screen_info
.rsvd_size
,
318 screen_info
.red_size
,
319 screen_info
.green_size
,
320 screen_info
.blue_size
,
321 screen_info
.rsvd_pos
,
323 screen_info
.green_pos
,
324 screen_info
.blue_pos
);
326 efifb_fix
.ypanstep
= 0;
327 efifb_fix
.ywrapstep
= 0;
329 info
->fbops
= &efifb_ops
;
330 info
->var
= efifb_defined
;
331 info
->fix
= efifb_fix
;
332 info
->flags
= FBINFO_FLAG_DEFAULT
| FBINFO_MISC_FIRMWARE
;
334 orientation
= drm_get_panel_orientation_quirk(efifb_defined
.xres
,
336 switch (orientation
) {
338 info
->fbcon_rotate_hint
= FB_ROTATE_UR
;
340 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP
:
341 info
->fbcon_rotate_hint
= FB_ROTATE_UD
;
343 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP
:
344 info
->fbcon_rotate_hint
= FB_ROTATE_CCW
;
346 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP
:
347 info
->fbcon_rotate_hint
= FB_ROTATE_CW
;
351 err
= sysfs_create_groups(&dev
->dev
.kobj
, efifb_groups
);
353 pr_err("efifb: cannot add sysfs attrs\n");
356 err
= fb_alloc_cmap(&info
->cmap
, 256, 0);
358 pr_err("efifb: cannot allocate colormap\n");
361 err
= register_framebuffer(info
);
363 pr_err("efifb: cannot register framebuffer\n");
366 fb_info(info
, "%s frame buffer device\n", info
->fix
.id
);
370 fb_dealloc_cmap(&info
->cmap
);
372 sysfs_remove_groups(&dev
->dev
.kobj
, efifb_groups
);
374 iounmap(info
->screen_base
);
376 framebuffer_release(info
);
378 if (request_mem_succeeded
)
379 release_mem_region(efifb_fix
.smem_start
, size_total
);
383 static int efifb_remove(struct platform_device
*pdev
)
385 struct fb_info
*info
= platform_get_drvdata(pdev
);
387 unregister_framebuffer(info
);
388 sysfs_remove_groups(&pdev
->dev
.kobj
, efifb_groups
);
389 framebuffer_release(info
);
394 static struct platform_driver efifb_driver
= {
396 .name
= "efi-framebuffer",
398 .probe
= efifb_probe
,
399 .remove
= efifb_remove
,
402 builtin_platform_driver(efifb_driver
);
404 #if defined(CONFIG_PCI)
406 static void record_efifb_bar_resource(struct pci_dev
*dev
, int idx
, u64 offset
)
412 pci_read_config_word(dev
, PCI_COMMAND
, &word
);
413 if (!(word
& PCI_COMMAND_MEMORY
)) {
414 pci_dev_disabled
= true;
416 "BAR %d: assigned to efifb but device is disabled!\n",
421 bar_resource
= &dev
->resource
[idx
];
424 dev_info(&dev
->dev
, "BAR %d: assigned to efifb\n", idx
);
427 static void efifb_fixup_resources(struct pci_dev
*dev
)
429 u64 base
= screen_info
.lfb_base
;
430 u64 size
= screen_info
.lfb_size
;
433 if (efifb_pci_dev
|| screen_info
.orig_video_isVGA
!= VIDEO_TYPE_EFI
)
436 if (screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
)
437 base
|= (u64
)screen_info
.ext_lfb_base
<< 32;
442 for (i
= 0; i
<= PCI_STD_RESOURCE_END
; i
++) {
443 struct resource
*res
= &dev
->resource
[i
];
445 if (!(res
->flags
& IORESOURCE_MEM
))
448 if (res
->start
<= base
&& res
->end
>= base
+ size
- 1) {
449 record_efifb_bar_resource(dev
, i
, base
- res
->start
);
454 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID
, PCI_ANY_ID
, PCI_BASE_CLASS_DISPLAY
,
455 16, efifb_fixup_resources
);