blob: 3a010641f6304e7460d5d7207052699e1b9e216f [file] [log] [blame]
Huang, Ying7c831722007-11-28 16:21:55 -08001/*
2 * Framebuffer driver for EFI/UEFI based system
3 *
4 * (c) 2006 Edgar Hucek <gimli@dark-green.com>
5 * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
6 *
7 */
8
Huang, Ying7c831722007-11-28 16:21:55 -08009#include <linux/kernel.h>
Ard Biesheuvel21289ec2016-04-25 21:06:50 +010010#include <linux/efi.h>
Huang, Ying7c831722007-11-28 16:21:55 -080011#include <linux/errno.h>
12#include <linux/fb.h>
Ard Biesheuvel55d728a2017-04-04 16:27:44 +010013#include <linux/pci.h>
Huang, Ying7c831722007-11-28 16:21:55 -080014#include <linux/platform_device.h>
15#include <linux/screen_info.h>
Huang, Ying7c831722007-11-28 16:21:55 -080016#include <video/vga.h>
Ard Biesheuvel21289ec2016-04-25 21:06:50 +010017#include <asm/efi.h>
Huang, Ying7c831722007-11-28 16:21:55 -080018
Andy Lutomirskida0241f2011-05-26 10:13:32 -040019static bool request_mem_succeeded = false;
Dave Airliedd0c41f2017-07-31 18:45:41 +020020static bool nowc = false;
Andy Lutomirskida0241f2011-05-26 10:13:32 -040021
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080022static struct fb_var_screeninfo efifb_defined = {
Huang, Ying7c831722007-11-28 16:21:55 -080023 .activate = FB_ACTIVATE_NOW,
24 .height = -1,
25 .width = -1,
26 .right_margin = 32,
27 .upper_margin = 16,
28 .lower_margin = 4,
29 .vsync_len = 4,
30 .vmode = FB_VMODE_NONINTERLACED,
31};
32
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080033static struct fb_fix_screeninfo efifb_fix = {
Huang, Ying7c831722007-11-28 16:21:55 -080034 .id = "EFI VGA",
35 .type = FB_TYPE_PACKED_PIXELS,
36 .accel = FB_ACCEL_NONE,
37 .visual = FB_VISUAL_TRUECOLOR,
38};
39
40static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
41 unsigned blue, unsigned transp,
42 struct fb_info *info)
43{
44 /*
45 * Set a single color register. The values supplied are
46 * already rounded down to the hardware's capabilities
47 * (according to the entries in the `var' structure). Return
48 * != 0 for invalid regno.
49 */
50
51 if (regno >= info->cmap.len)
52 return 1;
53
54 if (regno < 16) {
Max Staudtd50b3f42016-06-13 19:15:59 +020055 red >>= 16 - info->var.red.length;
56 green >>= 16 - info->var.green.length;
57 blue >>= 16 - info->var.blue.length;
Huang, Ying7c831722007-11-28 16:21:55 -080058 ((u32 *)(info->pseudo_palette))[regno] =
59 (red << info->var.red.offset) |
60 (green << info->var.green.offset) |
61 (blue << info->var.blue.offset);
62 }
63 return 0;
64}
65
Marcin Slusarz89f3f212010-02-22 12:44:22 -080066static void efifb_destroy(struct fb_info *info)
67{
68 if (info->screen_base)
69 iounmap(info->screen_base);
Andy Lutomirskida0241f2011-05-26 10:13:32 -040070 if (request_mem_succeeded)
71 release_mem_region(info->apertures->ranges[0].base,
72 info->apertures->ranges[0].size);
Peter Jones907355a2013-07-25 11:48:11 -040073 fb_dealloc_cmap(&info->cmap);
Marcin Slusarz89f3f212010-02-22 12:44:22 -080074}
75
Huang, Ying7c831722007-11-28 16:21:55 -080076static struct fb_ops efifb_ops = {
77 .owner = THIS_MODULE,
Marcin Slusarz89f3f212010-02-22 12:44:22 -080078 .fb_destroy = efifb_destroy,
Huang, Ying7c831722007-11-28 16:21:55 -080079 .fb_setcolreg = efifb_setcolreg,
80 .fb_fillrect = cfb_fillrect,
81 .fb_copyarea = cfb_copyarea,
82 .fb_imageblit = cfb_imageblit,
83};
84
David Herrmanne6816a82013-08-02 14:05:26 +020085static int efifb_setup(char *options)
Peter Jones7c08c9a2008-10-15 22:03:43 -070086{
87 char *this_opt;
Peter Jones7c08c9a2008-10-15 22:03:43 -070088
Matthew Garrettb4aa0162012-04-16 16:26:05 -040089 if (options && *options) {
90 while ((this_opt = strsep(&options, ",")) != NULL) {
91 if (!*this_opt) continue;
Peter Jones7c08c9a2008-10-15 22:03:43 -070092
Ard Biesheuvel21289ec2016-04-25 21:06:50 +010093 efifb_setup_from_dmi(&screen_info, this_opt);
94
Matthew Garrettb4aa0162012-04-16 16:26:05 -040095 if (!strncmp(this_opt, "base:", 5))
96 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
97 else if (!strncmp(this_opt, "stride:", 7))
98 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
99 else if (!strncmp(this_opt, "height:", 7))
100 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
101 else if (!strncmp(this_opt, "width:", 6))
102 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
Dave Airliedd0c41f2017-07-31 18:45:41 +0200103 else if (!strcmp(this_opt, "nowc"))
104 nowc = true;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700105 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700106 }
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400107
Peter Jones7c08c9a2008-10-15 22:03:43 -0700108 return 0;
109}
110
Matt Flemingae2ee622015-08-25 16:32:55 +0100111static inline bool fb_base_is_valid(void)
112{
113 if (screen_info.lfb_base)
114 return true;
115
116 if (!(screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE))
117 return false;
118
119 if (screen_info.ext_lfb_base)
120 return true;
121
122 return false;
123}
124
Peter Jones753375a2016-10-18 15:33:17 +0100125#define efifb_attr_decl(name, fmt) \
126static ssize_t name##_show(struct device *dev, \
127 struct device_attribute *attr, \
128 char *buf) \
129{ \
130 return sprintf(buf, fmt "\n", (screen_info.lfb_##name)); \
131} \
132static DEVICE_ATTR_RO(name)
133
134efifb_attr_decl(base, "0x%x");
135efifb_attr_decl(linelength, "%u");
136efifb_attr_decl(height, "%u");
137efifb_attr_decl(width, "%u");
138efifb_attr_decl(depth, "%u");
139
140static struct attribute *efifb_attrs[] = {
141 &dev_attr_base.attr,
142 &dev_attr_linelength.attr,
143 &dev_attr_width.attr,
144 &dev_attr_height.attr,
145 &dev_attr_depth.attr,
146 NULL
147};
148ATTRIBUTE_GROUPS(efifb);
149
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100150static bool pci_dev_disabled; /* FB base matches BAR of a disabled device */
151
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100152static struct pci_dev *efifb_pci_dev; /* dev with BAR covering the efifb */
153static struct resource *bar_resource;
154static u64 bar_offset;
155
David Herrmanne6816a82013-08-02 14:05:26 +0200156static int efifb_probe(struct platform_device *dev)
Huang, Ying7c831722007-11-28 16:21:55 -0800157{
158 struct fb_info *info;
159 int err;
160 unsigned int size_vmode;
161 unsigned int size_remap;
162 unsigned int size_total;
David Herrmanne6816a82013-08-02 14:05:26 +0200163 char *option = NULL;
164
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100165 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI || pci_dev_disabled)
David Herrmanne6816a82013-08-02 14:05:26 +0200166 return -ENODEV;
167
168 if (fb_get_options("efifb", &option))
169 return -ENODEV;
170 efifb_setup(option);
171
172 /* We don't get linelength from UGA Draw Protocol, only from
173 * EFI Graphics Protocol. So if it's not in DMI, and it's not
174 * passed in from the user, we really can't use the framebuffer.
175 */
176 if (!screen_info.lfb_linelength)
177 return -ENODEV;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700178
Peter Jones7c08c9a2008-10-15 22:03:43 -0700179 if (!screen_info.lfb_depth)
180 screen_info.lfb_depth = 32;
181 if (!screen_info.pages)
182 screen_info.pages = 1;
Matt Flemingae2ee622015-08-25 16:32:55 +0100183 if (!fb_base_is_valid()) {
Matthew Garrett133bb072009-04-13 14:39:44 -0700184 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
185 return -ENODEV;
186 }
187 printk(KERN_INFO "efifb: probing for efifb\n");
Peter Jones7c08c9a2008-10-15 22:03:43 -0700188
189 /* just assume they're all unset if any are */
190 if (!screen_info.blue_size) {
191 screen_info.blue_size = 8;
192 screen_info.blue_pos = 0;
193 screen_info.green_size = 8;
194 screen_info.green_pos = 8;
195 screen_info.red_size = 8;
196 screen_info.red_pos = 16;
197 screen_info.rsvd_size = 8;
198 screen_info.rsvd_pos = 24;
199 }
Huang, Ying7c831722007-11-28 16:21:55 -0800200
201 efifb_fix.smem_start = screen_info.lfb_base;
Matt Flemingae2ee622015-08-25 16:32:55 +0100202
203 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE) {
204 u64 ext_lfb_base;
205
206 ext_lfb_base = (u64)(unsigned long)screen_info.ext_lfb_base << 32;
207 efifb_fix.smem_start |= ext_lfb_base;
208 }
209
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100210 if (bar_resource &&
211 bar_resource->start + bar_offset != efifb_fix.smem_start) {
212 dev_info(&efifb_pci_dev->dev,
213 "BAR has moved, updating efifb address\n");
214 efifb_fix.smem_start = bar_resource->start + bar_offset;
215 }
216
Huang, Ying7c831722007-11-28 16:21:55 -0800217 efifb_defined.bits_per_pixel = screen_info.lfb_depth;
218 efifb_defined.xres = screen_info.lfb_width;
219 efifb_defined.yres = screen_info.lfb_height;
220 efifb_fix.line_length = screen_info.lfb_linelength;
221
222 /* size_vmode -- that is the amount of memory needed for the
223 * used video mode, i.e. the minimum amount of
224 * memory we need. */
225 size_vmode = efifb_defined.yres * efifb_fix.line_length;
226
227 /* size_total -- all video memory we have. Used for
228 * entries, ressource allocation and bounds
229 * checking. */
230 size_total = screen_info.lfb_size;
231 if (size_total < size_vmode)
232 size_total = size_vmode;
233
234 /* size_remap -- the amount of video memory we are going to
235 * use for efifb. With modern cards it is no
236 * option to simply use size_total as that
237 * wastes plenty of kernel address space. */
238 size_remap = size_vmode * 2;
Huang, Ying7c831722007-11-28 16:21:55 -0800239 if (size_remap > size_total)
240 size_remap = size_total;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700241 if (size_remap % PAGE_SIZE)
242 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
Huang, Ying7c831722007-11-28 16:21:55 -0800243 efifb_fix.smem_len = size_remap;
244
Peter Jones7c08c9a2008-10-15 22:03:43 -0700245 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400246 request_mem_succeeded = true;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700247 } else {
Huang, Ying7c831722007-11-28 16:21:55 -0800248 /* We cannot make this fatal. Sometimes this comes from magic
249 spaces our resource handlers simply don't know about */
Peter Jones753375a2016-10-18 15:33:17 +0100250 pr_warn("efifb: cannot reserve video memory at 0x%lx\n",
Huang, Ying7c831722007-11-28 16:21:55 -0800251 efifb_fix.smem_start);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700252 }
Huang, Ying7c831722007-11-28 16:21:55 -0800253
254 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
255 if (!info) {
Peter Jones753375a2016-10-18 15:33:17 +0100256 pr_err("efifb: cannot allocate framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800257 err = -ENOMEM;
258 goto err_release_mem;
259 }
David Herrmann65b40212014-01-23 15:14:55 +0100260 platform_set_drvdata(dev, info);
Huang, Ying7c831722007-11-28 16:21:55 -0800261 info->pseudo_palette = info->par;
262 info->par = NULL;
263
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200264 info->apertures = alloc_apertures(1);
265 if (!info->apertures) {
266 err = -ENOMEM;
267 goto err_release_fb;
268 }
269 info->apertures->ranges[0].base = efifb_fix.smem_start;
270 info->apertures->ranges[0].size = size_remap;
Dave Airlie4410f392009-06-16 15:34:38 -0700271
Dave Airliedd0c41f2017-07-31 18:45:41 +0200272 if (nowc)
273 info->screen_base = ioremap(efifb_fix.smem_start, efifb_fix.smem_len);
274 else
275 info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
Huang, Ying7c831722007-11-28 16:21:55 -0800276 if (!info->screen_base) {
Peter Jones753375a2016-10-18 15:33:17 +0100277 pr_err("efifb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
Huang, Ying7c831722007-11-28 16:21:55 -0800278 efifb_fix.smem_len, efifb_fix.smem_start);
279 err = -EIO;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700280 goto err_release_fb;
Huang, Ying7c831722007-11-28 16:21:55 -0800281 }
282
Peter Jones753375a2016-10-18 15:33:17 +0100283 pr_info("efifb: framebuffer at 0x%lx, using %dk, total %dk\n",
Andy Lutomirskia53dd252016-05-11 16:57:47 -0700284 efifb_fix.smem_start, size_remap/1024, size_total/1024);
Peter Jones753375a2016-10-18 15:33:17 +0100285 pr_info("efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
Huang, Ying7c831722007-11-28 16:21:55 -0800286 efifb_defined.xres, efifb_defined.yres,
287 efifb_defined.bits_per_pixel, efifb_fix.line_length,
288 screen_info.pages);
289
290 efifb_defined.xres_virtual = efifb_defined.xres;
291 efifb_defined.yres_virtual = efifb_fix.smem_len /
292 efifb_fix.line_length;
Peter Jones753375a2016-10-18 15:33:17 +0100293 pr_info("efifb: scrolling: redraw\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800294 efifb_defined.yres_virtual = efifb_defined.yres;
295
296 /* some dummy values for timing to make fbset happy */
297 efifb_defined.pixclock = 10000000 / efifb_defined.xres *
298 1000 / efifb_defined.yres;
299 efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
300 efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
301
302 efifb_defined.red.offset = screen_info.red_pos;
303 efifb_defined.red.length = screen_info.red_size;
304 efifb_defined.green.offset = screen_info.green_pos;
305 efifb_defined.green.length = screen_info.green_size;
306 efifb_defined.blue.offset = screen_info.blue_pos;
307 efifb_defined.blue.length = screen_info.blue_size;
308 efifb_defined.transp.offset = screen_info.rsvd_pos;
309 efifb_defined.transp.length = screen_info.rsvd_size;
310
Peter Jones753375a2016-10-18 15:33:17 +0100311 pr_info("efifb: %s: "
Huang, Ying7c831722007-11-28 16:21:55 -0800312 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
313 "Truecolor",
314 screen_info.rsvd_size,
315 screen_info.red_size,
316 screen_info.green_size,
317 screen_info.blue_size,
318 screen_info.rsvd_pos,
319 screen_info.red_pos,
320 screen_info.green_pos,
321 screen_info.blue_pos);
322
323 efifb_fix.ypanstep = 0;
324 efifb_fix.ywrapstep = 0;
325
326 info->fbops = &efifb_ops;
327 info->var = efifb_defined;
328 info->fix = efifb_fix;
Dave Airlie4410f392009-06-16 15:34:38 -0700329 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
Huang, Ying7c831722007-11-28 16:21:55 -0800330
Peter Jones753375a2016-10-18 15:33:17 +0100331 err = sysfs_create_groups(&dev->dev.kobj, efifb_groups);
332 if (err) {
333 pr_err("efifb: cannot add sysfs attrs\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800334 goto err_unmap;
335 }
Peter Jones753375a2016-10-18 15:33:17 +0100336 err = fb_alloc_cmap(&info->cmap, 256, 0);
337 if (err < 0) {
338 pr_err("efifb: cannot allocate colormap\n");
339 goto err_groups;
340 }
341 err = register_framebuffer(info);
342 if (err < 0) {
343 pr_err("efifb: cannot register framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800344 goto err_fb_dealoc;
345 }
Joe Perches31b67802013-09-19 18:35:55 -0700346 fb_info(info, "%s frame buffer device\n", info->fix.id);
Huang, Ying7c831722007-11-28 16:21:55 -0800347 return 0;
348
349err_fb_dealoc:
350 fb_dealloc_cmap(&info->cmap);
Peter Jones753375a2016-10-18 15:33:17 +0100351err_groups:
352 sysfs_remove_groups(&dev->dev.kobj, efifb_groups);
Huang, Ying7c831722007-11-28 16:21:55 -0800353err_unmap:
354 iounmap(info->screen_base);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700355err_release_fb:
Huang, Ying7c831722007-11-28 16:21:55 -0800356 framebuffer_release(info);
357err_release_mem:
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400358 if (request_mem_succeeded)
Peter Jones7c08c9a2008-10-15 22:03:43 -0700359 release_mem_region(efifb_fix.smem_start, size_total);
Huang, Ying7c831722007-11-28 16:21:55 -0800360 return err;
361}
362
David Herrmann65b40212014-01-23 15:14:55 +0100363static int efifb_remove(struct platform_device *pdev)
364{
365 struct fb_info *info = platform_get_drvdata(pdev);
366
367 unregister_framebuffer(info);
Peter Jones753375a2016-10-18 15:33:17 +0100368 sysfs_remove_groups(&pdev->dev.kobj, efifb_groups);
David Herrmann65b40212014-01-23 15:14:55 +0100369 framebuffer_release(info);
370
371 return 0;
372}
373
Huang, Ying7c831722007-11-28 16:21:55 -0800374static struct platform_driver efifb_driver = {
David Herrmanne6816a82013-08-02 14:05:26 +0200375 .driver = {
376 .name = "efi-framebuffer",
Huang, Ying7c831722007-11-28 16:21:55 -0800377 },
David Herrmanne6816a82013-08-02 14:05:26 +0200378 .probe = efifb_probe,
David Herrmann65b40212014-01-23 15:14:55 +0100379 .remove = efifb_remove,
Huang, Ying7c831722007-11-28 16:21:55 -0800380};
381
Ard Biesheuvel07ea7ec2016-04-25 21:06:51 +0100382builtin_platform_driver(efifb_driver);
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100383
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100384#if defined(CONFIG_PCI)
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100385
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100386static void record_efifb_bar_resource(struct pci_dev *dev, int idx, u64 offset)
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100387{
388 u16 word;
389
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100390 efifb_pci_dev = dev;
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100391
392 pci_read_config_word(dev, PCI_COMMAND, &word);
393 if (!(word & PCI_COMMAND_MEMORY)) {
394 pci_dev_disabled = true;
395 dev_err(&dev->dev,
396 "BAR %d: assigned to efifb but device is disabled!\n",
397 idx);
398 return;
399 }
400
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100401 bar_resource = &dev->resource[idx];
402 bar_offset = offset;
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100403
404 dev_info(&dev->dev, "BAR %d: assigned to efifb\n", idx);
405}
406
407static void efifb_fixup_resources(struct pci_dev *dev)
408{
409 u64 base = screen_info.lfb_base;
410 u64 size = screen_info.lfb_size;
411 int i;
412
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100413 if (efifb_pci_dev || screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100414 return;
415
416 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
417 base |= (u64)screen_info.ext_lfb_base << 32;
418
419 if (!base)
420 return;
421
Bjorn Helgaas92a16c82017-05-19 14:37:53 -0500422 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100423 struct resource *res = &dev->resource[i];
424
425 if (!(res->flags & IORESOURCE_MEM))
426 continue;
427
428 if (res->start <= base && res->end >= base + size - 1) {
Ard Biesheuveldcf8f5c2017-08-18 20:49:40 +0100429 record_efifb_bar_resource(dev, i, base - res->start);
Ard Biesheuvel55d728a2017-04-04 16:27:44 +0100430 break;
431 }
432 }
433}
434DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY,
435 16, efifb_fixup_resources);
436
437#endif