blob: 7f9ff75d0db22814dcef20c68a923c5fc5125fe1 [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
Matthew Garrettb4aa0162012-04-16 16:26:05 -040022static struct pci_dev *default_vga;
23
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080024static struct fb_var_screeninfo efifb_defined = {
Huang, Ying7c831722007-11-28 16:21:55 -080025 .activate = FB_ACTIVATE_NOW,
26 .height = -1,
27 .width = -1,
28 .right_margin = 32,
29 .upper_margin = 16,
30 .lower_margin = 4,
31 .vsync_len = 4,
32 .vmode = FB_VMODE_NONINTERLACED,
33};
34
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080035static struct fb_fix_screeninfo efifb_fix = {
Huang, Ying7c831722007-11-28 16:21:55 -080036 .id = "EFI VGA",
37 .type = FB_TYPE_PACKED_PIXELS,
38 .accel = FB_ACCEL_NONE,
39 .visual = FB_VISUAL_TRUECOLOR,
40};
41
42static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
43 unsigned blue, unsigned transp,
44 struct fb_info *info)
45{
46 /*
47 * Set a single color register. The values supplied are
48 * already rounded down to the hardware's capabilities
49 * (according to the entries in the `var' structure). Return
50 * != 0 for invalid regno.
51 */
52
53 if (regno >= info->cmap.len)
54 return 1;
55
56 if (regno < 16) {
57 red >>= 8;
58 green >>= 8;
59 blue >>= 8;
60 ((u32 *)(info->pseudo_palette))[regno] =
61 (red << info->var.red.offset) |
62 (green << info->var.green.offset) |
63 (blue << info->var.blue.offset);
64 }
65 return 0;
66}
67
Marcin Slusarz89f3f212010-02-22 12:44:22 -080068static void efifb_destroy(struct fb_info *info)
69{
70 if (info->screen_base)
71 iounmap(info->screen_base);
Andy Lutomirskida0241f2011-05-26 10:13:32 -040072 if (request_mem_succeeded)
73 release_mem_region(info->apertures->ranges[0].base,
74 info->apertures->ranges[0].size);
Peter Jones907355a2013-07-25 11:48:11 -040075 fb_dealloc_cmap(&info->cmap);
Marcin Slusarz89f3f212010-02-22 12:44:22 -080076 framebuffer_release(info);
77}
78
Huang, Ying7c831722007-11-28 16:21:55 -080079static struct fb_ops efifb_ops = {
80 .owner = THIS_MODULE,
Marcin Slusarz89f3f212010-02-22 12:44:22 -080081 .fb_destroy = efifb_destroy,
Huang, Ying7c831722007-11-28 16:21:55 -080082 .fb_setcolreg = efifb_setcolreg,
83 .fb_fillrect = cfb_fillrect,
84 .fb_copyarea = cfb_copyarea,
85 .fb_imageblit = cfb_imageblit,
86};
87
Matthew Garrettb4aa0162012-04-16 16:26:05 -040088struct pci_dev *vga_default_device(void)
89{
90 return default_vga;
91}
92
Matthew Garrett1b231702012-04-24 09:31:28 +010093EXPORT_SYMBOL_GPL(vga_default_device);
94
Matthew Garrettb4aa0162012-04-16 16:26:05 -040095void vga_set_default_device(struct pci_dev *pdev)
96{
97 default_vga = pdev;
98}
99
David Herrmanne6816a82013-08-02 14:05:26 +0200100static int efifb_setup(char *options)
Peter Jones7c08c9a2008-10-15 22:03:43 -0700101{
102 char *this_opt;
103 int i;
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400104 struct pci_dev *dev = NULL;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700105
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400106 if (options && *options) {
107 while ((this_opt = strsep(&options, ",")) != NULL) {
108 if (!*this_opt) continue;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700109
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400110 for (i = 0; i < M_UNKNOWN; i++) {
David Herrmann2995e502013-08-02 14:05:23 +0200111 if (!strcmp(this_opt, efifb_dmi_list[i].optname) &&
112 efifb_dmi_list[i].base != 0) {
113 screen_info.lfb_base = efifb_dmi_list[i].base;
114 screen_info.lfb_linelength = efifb_dmi_list[i].stride;
115 screen_info.lfb_width = efifb_dmi_list[i].width;
116 screen_info.lfb_height = efifb_dmi_list[i].height;
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400117 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700118 }
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400119 if (!strncmp(this_opt, "base:", 5))
120 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
121 else if (!strncmp(this_opt, "stride:", 7))
122 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
123 else if (!strncmp(this_opt, "height:", 7))
124 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
125 else if (!strncmp(this_opt, "width:", 6))
126 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700127 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700128 }
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400129
130 for_each_pci_dev(dev) {
131 int i;
132
133 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
134 continue;
135
136 for (i=0; i < DEVICE_COUNT_RESOURCE; i++) {
137 resource_size_t start, end;
138
139 if (!(pci_resource_flags(dev, i) & IORESOURCE_MEM))
140 continue;
141
142 start = pci_resource_start(dev, i);
143 end = pci_resource_end(dev, i);
144
145 if (!start || !end)
146 continue;
147
148 if (screen_info.lfb_base >= start &&
149 (screen_info.lfb_base + screen_info.lfb_size) < end)
150 default_vga = dev;
151 }
152 }
153
Peter Jones7c08c9a2008-10-15 22:03:43 -0700154 return 0;
155}
156
David Herrmanne6816a82013-08-02 14:05:26 +0200157static int efifb_probe(struct platform_device *dev)
Huang, Ying7c831722007-11-28 16:21:55 -0800158{
159 struct fb_info *info;
160 int err;
161 unsigned int size_vmode;
162 unsigned int size_remap;
163 unsigned int size_total;
David Herrmanne6816a82013-08-02 14:05:26 +0200164 char *option = NULL;
165
166 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
167 return -ENODEV;
168
169 if (fb_get_options("efifb", &option))
170 return -ENODEV;
171 efifb_setup(option);
172
173 /* We don't get linelength from UGA Draw Protocol, only from
174 * EFI Graphics Protocol. So if it's not in DMI, and it's not
175 * passed in from the user, we really can't use the framebuffer.
176 */
177 if (!screen_info.lfb_linelength)
178 return -ENODEV;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700179
Peter Jones7c08c9a2008-10-15 22:03:43 -0700180 if (!screen_info.lfb_depth)
181 screen_info.lfb_depth = 32;
182 if (!screen_info.pages)
183 screen_info.pages = 1;
Matthew Garrett133bb072009-04-13 14:39:44 -0700184 if (!screen_info.lfb_base) {
185 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
186 return -ENODEV;
187 }
188 printk(KERN_INFO "efifb: probing for efifb\n");
Peter Jones7c08c9a2008-10-15 22:03:43 -0700189
190 /* just assume they're all unset if any are */
191 if (!screen_info.blue_size) {
192 screen_info.blue_size = 8;
193 screen_info.blue_pos = 0;
194 screen_info.green_size = 8;
195 screen_info.green_pos = 8;
196 screen_info.red_size = 8;
197 screen_info.red_pos = 16;
198 screen_info.rsvd_size = 8;
199 screen_info.rsvd_pos = 24;
200 }
Huang, Ying7c831722007-11-28 16:21:55 -0800201
202 efifb_fix.smem_start = screen_info.lfb_base;
203 efifb_defined.bits_per_pixel = screen_info.lfb_depth;
204 efifb_defined.xres = screen_info.lfb_width;
205 efifb_defined.yres = screen_info.lfb_height;
206 efifb_fix.line_length = screen_info.lfb_linelength;
207
208 /* size_vmode -- that is the amount of memory needed for the
209 * used video mode, i.e. the minimum amount of
210 * memory we need. */
211 size_vmode = efifb_defined.yres * efifb_fix.line_length;
212
213 /* size_total -- all video memory we have. Used for
214 * entries, ressource allocation and bounds
215 * checking. */
216 size_total = screen_info.lfb_size;
217 if (size_total < size_vmode)
218 size_total = size_vmode;
219
220 /* size_remap -- the amount of video memory we are going to
221 * use for efifb. With modern cards it is no
222 * option to simply use size_total as that
223 * wastes plenty of kernel address space. */
224 size_remap = size_vmode * 2;
Huang, Ying7c831722007-11-28 16:21:55 -0800225 if (size_remap > size_total)
226 size_remap = size_total;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700227 if (size_remap % PAGE_SIZE)
228 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
Huang, Ying7c831722007-11-28 16:21:55 -0800229 efifb_fix.smem_len = size_remap;
230
Peter Jones7c08c9a2008-10-15 22:03:43 -0700231 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400232 request_mem_succeeded = true;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700233 } else {
Huang, Ying7c831722007-11-28 16:21:55 -0800234 /* We cannot make this fatal. Sometimes this comes from magic
235 spaces our resource handlers simply don't know about */
236 printk(KERN_WARNING
237 "efifb: cannot reserve video memory at 0x%lx\n",
238 efifb_fix.smem_start);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700239 }
Huang, Ying7c831722007-11-28 16:21:55 -0800240
241 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
242 if (!info) {
Peter Jones7c08c9a2008-10-15 22:03:43 -0700243 printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800244 err = -ENOMEM;
245 goto err_release_mem;
246 }
247 info->pseudo_palette = info->par;
248 info->par = NULL;
249
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200250 info->apertures = alloc_apertures(1);
251 if (!info->apertures) {
252 err = -ENOMEM;
253 goto err_release_fb;
254 }
255 info->apertures->ranges[0].base = efifb_fix.smem_start;
256 info->apertures->ranges[0].size = size_remap;
Dave Airlie4410f392009-06-16 15:34:38 -0700257
Andy Lutomirski3c004b4f2011-05-26 10:13:31 -0400258 info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
Huang, Ying7c831722007-11-28 16:21:55 -0800259 if (!info->screen_base) {
260 printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
261 "0x%x @ 0x%lx\n",
262 efifb_fix.smem_len, efifb_fix.smem_start);
263 err = -EIO;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700264 goto err_release_fb;
Huang, Ying7c831722007-11-28 16:21:55 -0800265 }
266
267 printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
268 "using %dk, total %dk\n",
269 efifb_fix.smem_start, info->screen_base,
270 size_remap/1024, size_total/1024);
271 printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
272 efifb_defined.xres, efifb_defined.yres,
273 efifb_defined.bits_per_pixel, efifb_fix.line_length,
274 screen_info.pages);
275
276 efifb_defined.xres_virtual = efifb_defined.xres;
277 efifb_defined.yres_virtual = efifb_fix.smem_len /
278 efifb_fix.line_length;
279 printk(KERN_INFO "efifb: scrolling: redraw\n");
280 efifb_defined.yres_virtual = efifb_defined.yres;
281
282 /* some dummy values for timing to make fbset happy */
283 efifb_defined.pixclock = 10000000 / efifb_defined.xres *
284 1000 / efifb_defined.yres;
285 efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
286 efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
287
288 efifb_defined.red.offset = screen_info.red_pos;
289 efifb_defined.red.length = screen_info.red_size;
290 efifb_defined.green.offset = screen_info.green_pos;
291 efifb_defined.green.length = screen_info.green_size;
292 efifb_defined.blue.offset = screen_info.blue_pos;
293 efifb_defined.blue.length = screen_info.blue_size;
294 efifb_defined.transp.offset = screen_info.rsvd_pos;
295 efifb_defined.transp.length = screen_info.rsvd_size;
296
297 printk(KERN_INFO "efifb: %s: "
298 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
299 "Truecolor",
300 screen_info.rsvd_size,
301 screen_info.red_size,
302 screen_info.green_size,
303 screen_info.blue_size,
304 screen_info.rsvd_pos,
305 screen_info.red_pos,
306 screen_info.green_pos,
307 screen_info.blue_pos);
308
309 efifb_fix.ypanstep = 0;
310 efifb_fix.ywrapstep = 0;
311
312 info->fbops = &efifb_ops;
313 info->var = efifb_defined;
314 info->fix = efifb_fix;
Dave Airlie4410f392009-06-16 15:34:38 -0700315 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
Huang, Ying7c831722007-11-28 16:21:55 -0800316
Peter Jones7c08c9a2008-10-15 22:03:43 -0700317 if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
318 printk(KERN_ERR "efifb: cannot allocate colormap\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800319 goto err_unmap;
320 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700321 if ((err = register_framebuffer(info)) < 0) {
322 printk(KERN_ERR "efifb: cannot register framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800323 goto err_fb_dealoc;
324 }
325 printk(KERN_INFO "fb%d: %s frame buffer device\n",
Peter Jones7c08c9a2008-10-15 22:03:43 -0700326 info->node, info->fix.id);
Huang, Ying7c831722007-11-28 16:21:55 -0800327 return 0;
328
329err_fb_dealoc:
330 fb_dealloc_cmap(&info->cmap);
331err_unmap:
332 iounmap(info->screen_base);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700333err_release_fb:
Huang, Ying7c831722007-11-28 16:21:55 -0800334 framebuffer_release(info);
335err_release_mem:
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400336 if (request_mem_succeeded)
Peter Jones7c08c9a2008-10-15 22:03:43 -0700337 release_mem_region(efifb_fix.smem_start, size_total);
Huang, Ying7c831722007-11-28 16:21:55 -0800338 return err;
339}
340
341static struct platform_driver efifb_driver = {
David Herrmanne6816a82013-08-02 14:05:26 +0200342 .driver = {
343 .name = "efi-framebuffer",
344 .owner = THIS_MODULE,
Huang, Ying7c831722007-11-28 16:21:55 -0800345 },
David Herrmanne6816a82013-08-02 14:05:26 +0200346 .probe = efifb_probe,
Huang, Ying7c831722007-11-28 16:21:55 -0800347};
348
David Herrmanne6816a82013-08-02 14:05:26 +0200349module_platform_driver(efifb_driver);
Huang, Ying7c831722007-11-28 16:21:55 -0800350MODULE_LICENSE("GPL");