blob: 4bfff349b1fbb56f0b99e206f21fc2986ad4db0d [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
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/fb.h>
13#include <linux/platform_device.h>
14#include <linux/screen_info.h>
Peter Jones7c08c9a2008-10-15 22:03:43 -070015#include <linux/dmi.h>
Peter Jones85a00d92010-09-22 13:05:04 -070016#include <linux/pci.h>
Huang, Ying7c831722007-11-28 16:21:55 -080017#include <video/vga.h>
David Herrmann2995e502013-08-02 14:05:23 +020018#include <asm/sysfb.h>
Huang, Ying7c831722007-11-28 16:21:55 -080019
Andy Lutomirskida0241f2011-05-26 10:13:32 -040020static bool request_mem_succeeded = false;
21
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) {
55 red >>= 8;
56 green >>= 8;
57 blue >>= 8;
58 ((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;
88 int i;
89
Matthew Garrettb4aa0162012-04-16 16:26:05 -040090 if (options && *options) {
91 while ((this_opt = strsep(&options, ",")) != NULL) {
92 if (!*this_opt) continue;
Peter Jones7c08c9a2008-10-15 22:03:43 -070093
Matthew Garrettb4aa0162012-04-16 16:26:05 -040094 for (i = 0; i < M_UNKNOWN; i++) {
James Bates55aa42f2013-10-02 18:43:39 +020095 if (efifb_dmi_list[i].base != 0 &&
96 !strcmp(this_opt, efifb_dmi_list[i].optname)) {
David Herrmann2995e502013-08-02 14:05:23 +020097 screen_info.lfb_base = efifb_dmi_list[i].base;
98 screen_info.lfb_linelength = efifb_dmi_list[i].stride;
99 screen_info.lfb_width = efifb_dmi_list[i].width;
100 screen_info.lfb_height = efifb_dmi_list[i].height;
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400101 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700102 }
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400103 if (!strncmp(this_opt, "base:", 5))
104 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
105 else if (!strncmp(this_opt, "stride:", 7))
106 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
107 else if (!strncmp(this_opt, "height:", 7))
108 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
109 else if (!strncmp(this_opt, "width:", 6))
110 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700111 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700112 }
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400113
Peter Jones7c08c9a2008-10-15 22:03:43 -0700114 return 0;
115}
116
David Herrmanne6816a82013-08-02 14:05:26 +0200117static int efifb_probe(struct platform_device *dev)
Huang, Ying7c831722007-11-28 16:21:55 -0800118{
119 struct fb_info *info;
120 int err;
121 unsigned int size_vmode;
122 unsigned int size_remap;
123 unsigned int size_total;
David Herrmanne6816a82013-08-02 14:05:26 +0200124 char *option = NULL;
125
126 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
127 return -ENODEV;
128
129 if (fb_get_options("efifb", &option))
130 return -ENODEV;
131 efifb_setup(option);
132
133 /* We don't get linelength from UGA Draw Protocol, only from
134 * EFI Graphics Protocol. So if it's not in DMI, and it's not
135 * passed in from the user, we really can't use the framebuffer.
136 */
137 if (!screen_info.lfb_linelength)
138 return -ENODEV;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700139
Peter Jones7c08c9a2008-10-15 22:03:43 -0700140 if (!screen_info.lfb_depth)
141 screen_info.lfb_depth = 32;
142 if (!screen_info.pages)
143 screen_info.pages = 1;
Matthew Garrett133bb072009-04-13 14:39:44 -0700144 if (!screen_info.lfb_base) {
145 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
146 return -ENODEV;
147 }
148 printk(KERN_INFO "efifb: probing for efifb\n");
Peter Jones7c08c9a2008-10-15 22:03:43 -0700149
150 /* just assume they're all unset if any are */
151 if (!screen_info.blue_size) {
152 screen_info.blue_size = 8;
153 screen_info.blue_pos = 0;
154 screen_info.green_size = 8;
155 screen_info.green_pos = 8;
156 screen_info.red_size = 8;
157 screen_info.red_pos = 16;
158 screen_info.rsvd_size = 8;
159 screen_info.rsvd_pos = 24;
160 }
Huang, Ying7c831722007-11-28 16:21:55 -0800161
162 efifb_fix.smem_start = screen_info.lfb_base;
163 efifb_defined.bits_per_pixel = screen_info.lfb_depth;
164 efifb_defined.xres = screen_info.lfb_width;
165 efifb_defined.yres = screen_info.lfb_height;
166 efifb_fix.line_length = screen_info.lfb_linelength;
167
168 /* size_vmode -- that is the amount of memory needed for the
169 * used video mode, i.e. the minimum amount of
170 * memory we need. */
171 size_vmode = efifb_defined.yres * efifb_fix.line_length;
172
173 /* size_total -- all video memory we have. Used for
174 * entries, ressource allocation and bounds
175 * checking. */
176 size_total = screen_info.lfb_size;
177 if (size_total < size_vmode)
178 size_total = size_vmode;
179
180 /* size_remap -- the amount of video memory we are going to
181 * use for efifb. With modern cards it is no
182 * option to simply use size_total as that
183 * wastes plenty of kernel address space. */
184 size_remap = size_vmode * 2;
Huang, Ying7c831722007-11-28 16:21:55 -0800185 if (size_remap > size_total)
186 size_remap = size_total;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700187 if (size_remap % PAGE_SIZE)
188 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
Huang, Ying7c831722007-11-28 16:21:55 -0800189 efifb_fix.smem_len = size_remap;
190
Peter Jones7c08c9a2008-10-15 22:03:43 -0700191 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400192 request_mem_succeeded = true;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700193 } else {
Huang, Ying7c831722007-11-28 16:21:55 -0800194 /* We cannot make this fatal. Sometimes this comes from magic
195 spaces our resource handlers simply don't know about */
196 printk(KERN_WARNING
197 "efifb: cannot reserve video memory at 0x%lx\n",
198 efifb_fix.smem_start);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700199 }
Huang, Ying7c831722007-11-28 16:21:55 -0800200
201 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
202 if (!info) {
Peter Jones7c08c9a2008-10-15 22:03:43 -0700203 printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800204 err = -ENOMEM;
205 goto err_release_mem;
206 }
David Herrmann65b40212014-01-23 15:14:55 +0100207 platform_set_drvdata(dev, info);
Huang, Ying7c831722007-11-28 16:21:55 -0800208 info->pseudo_palette = info->par;
209 info->par = NULL;
210
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200211 info->apertures = alloc_apertures(1);
212 if (!info->apertures) {
213 err = -ENOMEM;
214 goto err_release_fb;
215 }
216 info->apertures->ranges[0].base = efifb_fix.smem_start;
217 info->apertures->ranges[0].size = size_remap;
Dave Airlie4410f392009-06-16 15:34:38 -0700218
Andy Lutomirski3c004b4f2011-05-26 10:13:31 -0400219 info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
Huang, Ying7c831722007-11-28 16:21:55 -0800220 if (!info->screen_base) {
221 printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
222 "0x%x @ 0x%lx\n",
223 efifb_fix.smem_len, efifb_fix.smem_start);
224 err = -EIO;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700225 goto err_release_fb;
Huang, Ying7c831722007-11-28 16:21:55 -0800226 }
227
228 printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
229 "using %dk, total %dk\n",
230 efifb_fix.smem_start, info->screen_base,
231 size_remap/1024, size_total/1024);
232 printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
233 efifb_defined.xres, efifb_defined.yres,
234 efifb_defined.bits_per_pixel, efifb_fix.line_length,
235 screen_info.pages);
236
237 efifb_defined.xres_virtual = efifb_defined.xres;
238 efifb_defined.yres_virtual = efifb_fix.smem_len /
239 efifb_fix.line_length;
240 printk(KERN_INFO "efifb: scrolling: redraw\n");
241 efifb_defined.yres_virtual = efifb_defined.yres;
242
243 /* some dummy values for timing to make fbset happy */
244 efifb_defined.pixclock = 10000000 / efifb_defined.xres *
245 1000 / efifb_defined.yres;
246 efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
247 efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
248
249 efifb_defined.red.offset = screen_info.red_pos;
250 efifb_defined.red.length = screen_info.red_size;
251 efifb_defined.green.offset = screen_info.green_pos;
252 efifb_defined.green.length = screen_info.green_size;
253 efifb_defined.blue.offset = screen_info.blue_pos;
254 efifb_defined.blue.length = screen_info.blue_size;
255 efifb_defined.transp.offset = screen_info.rsvd_pos;
256 efifb_defined.transp.length = screen_info.rsvd_size;
257
258 printk(KERN_INFO "efifb: %s: "
259 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
260 "Truecolor",
261 screen_info.rsvd_size,
262 screen_info.red_size,
263 screen_info.green_size,
264 screen_info.blue_size,
265 screen_info.rsvd_pos,
266 screen_info.red_pos,
267 screen_info.green_pos,
268 screen_info.blue_pos);
269
270 efifb_fix.ypanstep = 0;
271 efifb_fix.ywrapstep = 0;
272
273 info->fbops = &efifb_ops;
274 info->var = efifb_defined;
275 info->fix = efifb_fix;
Dave Airlie4410f392009-06-16 15:34:38 -0700276 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
Huang, Ying7c831722007-11-28 16:21:55 -0800277
Peter Jones7c08c9a2008-10-15 22:03:43 -0700278 if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
279 printk(KERN_ERR "efifb: cannot allocate colormap\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800280 goto err_unmap;
281 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700282 if ((err = register_framebuffer(info)) < 0) {
283 printk(KERN_ERR "efifb: cannot register framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800284 goto err_fb_dealoc;
285 }
Joe Perches31b67802013-09-19 18:35:55 -0700286 fb_info(info, "%s frame buffer device\n", info->fix.id);
Huang, Ying7c831722007-11-28 16:21:55 -0800287 return 0;
288
289err_fb_dealoc:
290 fb_dealloc_cmap(&info->cmap);
291err_unmap:
292 iounmap(info->screen_base);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700293err_release_fb:
Huang, Ying7c831722007-11-28 16:21:55 -0800294 framebuffer_release(info);
295err_release_mem:
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400296 if (request_mem_succeeded)
Peter Jones7c08c9a2008-10-15 22:03:43 -0700297 release_mem_region(efifb_fix.smem_start, size_total);
Huang, Ying7c831722007-11-28 16:21:55 -0800298 return err;
299}
300
David Herrmann65b40212014-01-23 15:14:55 +0100301static int efifb_remove(struct platform_device *pdev)
302{
303 struct fb_info *info = platform_get_drvdata(pdev);
304
305 unregister_framebuffer(info);
306 framebuffer_release(info);
307
308 return 0;
309}
310
Huang, Ying7c831722007-11-28 16:21:55 -0800311static struct platform_driver efifb_driver = {
David Herrmanne6816a82013-08-02 14:05:26 +0200312 .driver = {
313 .name = "efi-framebuffer",
Huang, Ying7c831722007-11-28 16:21:55 -0800314 },
David Herrmanne6816a82013-08-02 14:05:26 +0200315 .probe = efifb_probe,
David Herrmann65b40212014-01-23 15:14:55 +0100316 .remove = efifb_remove,
Huang, Ying7c831722007-11-28 16:21:55 -0800317};
318
David Herrmanne6816a82013-08-02 14:05:26 +0200319module_platform_driver(efifb_driver);
Huang, Ying7c831722007-11-28 16:21:55 -0800320MODULE_LICENSE("GPL");