blob: 932abaa58a89af3bc63f03fc3a2f80c078b3eeaa [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>
18
Andy Lutomirskida0241f2011-05-26 10:13:32 -040019static bool request_mem_succeeded = false;
20
Matthew Garrettb4aa0162012-04-16 16:26:05 -040021static struct pci_dev *default_vga;
22
Henrik Kretzschmarec81c9c2010-08-10 18:02:28 -070023static struct fb_var_screeninfo efifb_defined __devinitdata = {
Huang, Ying7c831722007-11-28 16:21:55 -080024 .activate = FB_ACTIVATE_NOW,
25 .height = -1,
26 .width = -1,
27 .right_margin = 32,
28 .upper_margin = 16,
29 .lower_margin = 4,
30 .vsync_len = 4,
31 .vmode = FB_VMODE_NONINTERLACED,
32};
33
Henrik Kretzschmarec81c9c2010-08-10 18:02:28 -070034static struct fb_fix_screeninfo efifb_fix __devinitdata = {
Huang, Ying7c831722007-11-28 16:21:55 -080035 .id = "EFI VGA",
36 .type = FB_TYPE_PACKED_PIXELS,
37 .accel = FB_ACCEL_NONE,
38 .visual = FB_VISUAL_TRUECOLOR,
39};
40
Peter Jones7c08c9a2008-10-15 22:03:43 -070041enum {
42 M_I17, /* 17-Inch iMac */
43 M_I20, /* 20-Inch iMac */
44 M_I20_SR, /* 20-Inch iMac (Santa Rosa) */
45 M_I24, /* 24-Inch iMac */
Luke Mackena5757c22010-09-22 13:05:04 -070046 M_I24_8_1, /* 24-Inch iMac, 8,1th gen */
47 M_I24_10_1, /* 24-Inch iMac, 10,1th gen */
48 M_I27_11_1, /* 27-Inch iMac, 11,1th gen */
Peter Jones7c08c9a2008-10-15 22:03:43 -070049 M_MINI, /* Mac Mini */
Luke Mackena5757c22010-09-22 13:05:04 -070050 M_MINI_3_1, /* Mac Mini, 3,1th gen */
51 M_MINI_4_1, /* Mac Mini, 4,1th gen */
Peter Jones7c08c9a2008-10-15 22:03:43 -070052 M_MB, /* MacBook */
53 M_MB_2, /* MacBook, 2nd rev. */
54 M_MB_3, /* MacBook, 3rd rev. */
Luke Mackena5757c22010-09-22 13:05:04 -070055 M_MB_5_1, /* MacBook, 5th rev. */
56 M_MB_6_1, /* MacBook, 6th rev. */
57 M_MB_7_1, /* MacBook, 7th rev. */
Peter Jones7c08c9a2008-10-15 22:03:43 -070058 M_MB_SR, /* MacBook, 2nd gen, (Santa Rosa) */
59 M_MBA, /* MacBook Air */
Matthew Garrett97fb8502011-04-06 17:34:59 +000060 M_MBA_3, /* Macbook Air, 3rd rev */
Peter Jones7c08c9a2008-10-15 22:03:43 -070061 M_MBP, /* MacBook Pro */
62 M_MBP_2, /* MacBook Pro 2nd gen */
Luke Mackena5757c22010-09-22 13:05:04 -070063 M_MBP_2_2, /* MacBook Pro 2,2nd gen */
Peter Jones7c08c9a2008-10-15 22:03:43 -070064 M_MBP_SR, /* MacBook Pro (Santa Rosa) */
65 M_MBP_4, /* MacBook Pro, 4th gen */
Thomas Gerlach8a3bdfe2010-04-23 13:17:50 -040066 M_MBP_5_1, /* MacBook Pro, 5,1th gen */
Luke Mackena5757c22010-09-22 13:05:04 -070067 M_MBP_5_2, /* MacBook Pro, 5,2th gen */
68 M_MBP_5_3, /* MacBook Pro, 5,3rd gen */
69 M_MBP_6_1, /* MacBook Pro, 6,1th gen */
70 M_MBP_6_2, /* MacBook Pro, 6,2th gen */
71 M_MBP_7_1, /* MacBook Pro, 7,1th gen */
Davidlohr Buesoe9c5db02011-03-31 00:12:22 +000072 M_MBP_8_2, /* MacBook Pro, 8,2nd gen */
Peter Jones7c08c9a2008-10-15 22:03:43 -070073 M_UNKNOWN /* placeholder */
74};
75
Peter Jones47dfe512011-04-06 17:34:58 +000076#define OVERRIDE_NONE 0x0
77#define OVERRIDE_BASE 0x1
78#define OVERRIDE_STRIDE 0x2
79#define OVERRIDE_HEIGHT 0x4
80#define OVERRIDE_WIDTH 0x8
81
Peter Jones7c08c9a2008-10-15 22:03:43 -070082static struct efifb_dmi_info {
83 char *optname;
84 unsigned long base;
85 int stride;
86 int width;
87 int height;
Peter Jones47dfe512011-04-06 17:34:58 +000088 int flags;
Henrik Kretzschmarec81c9c2010-08-10 18:02:28 -070089} dmi_list[] __initdata = {
Peter Jones47dfe512011-04-06 17:34:58 +000090 [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
91 [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050, OVERRIDE_NONE }, /* guess */
92 [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050, OVERRIDE_NONE },
93 [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, /* guess */
94 [M_I24_8_1] = { "imac8", 0xc0060000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
95 [M_I24_10_1] = { "imac10", 0xc0010000, 2048 * 4, 1920, 1080, OVERRIDE_NONE },
96 [M_I27_11_1] = { "imac11", 0xc0010000, 2560 * 4, 2560, 1440, OVERRIDE_NONE },
97 [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768, OVERRIDE_NONE },
98 [M_MINI_3_1] = { "mini31", 0x40010000, 1024 * 4, 1024, 768, OVERRIDE_NONE },
99 [M_MINI_4_1] = { "mini41", 0xc0010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
100 [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
101 [M_MB_5_1] = { "macbook51", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
102 [M_MB_6_1] = { "macbook61", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
103 [M_MB_7_1] = { "macbook71", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
104 [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
Matthew Garrett97fb8502011-04-06 17:34:59 +0000105 /* 11" Macbook Air 3,1 passes the wrong stride */
106 [M_MBA_3] = { "mba3", 0, 2048 * 4, 0, 0, OVERRIDE_STRIDE },
Peter Jones47dfe512011-04-06 17:34:58 +0000107 [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
108 [M_MBP_2] = { "mbp2", 0, 0, 0, 0, OVERRIDE_NONE }, /* placeholder */
109 [M_MBP_2_2] = { "mbp22", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
110 [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
111 [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
112 [M_MBP_5_1] = { "mbp51", 0xc0010000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
113 [M_MBP_5_2] = { "mbp52", 0xc0010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
114 [M_MBP_5_3] = { "mbp53", 0xd0010000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
115 [M_MBP_6_1] = { "mbp61", 0x90030000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
116 [M_MBP_6_2] = { "mbp62", 0x90030000, 2048 * 4, 1680, 1050, OVERRIDE_NONE },
117 [M_MBP_7_1] = { "mbp71", 0xc0010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
118 [M_MBP_8_2] = { "mbp82", 0x90010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
119 [M_UNKNOWN] = { NULL, 0, 0, 0, 0, OVERRIDE_NONE }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700120};
121
122static int set_system(const struct dmi_system_id *id);
123
124#define EFIFB_DMI_SYSTEM_ID(vendor, name, enumid) \
125 { set_system, name, { \
126 DMI_MATCH(DMI_BIOS_VENDOR, vendor), \
127 DMI_MATCH(DMI_PRODUCT_NAME, name) }, \
128 &dmi_list[enumid] }
129
Henrik Kretzschmarec81c9c2010-08-10 18:02:28 -0700130static const struct dmi_system_id dmi_system_table[] __initconst = {
Peter Jones7c08c9a2008-10-15 22:03:43 -0700131 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac4,1", M_I17),
132 /* At least one of these two will be right; maybe both? */
133 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac5,1", M_I20),
134 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac5,1", M_I20),
135 /* At least one of these two will be right; maybe both? */
136 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac6,1", M_I24),
137 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac6,1", M_I24),
138 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac7,1", M_I20_SR),
Luke Mackena5757c22010-09-22 13:05:04 -0700139 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac8,1", M_I24_8_1),
140 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac10,1", M_I24_10_1),
141 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac11,1", M_I27_11_1),
Peter Jones7c08c9a2008-10-15 22:03:43 -0700142 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "Macmini1,1", M_MINI),
Luke Mackena5757c22010-09-22 13:05:04 -0700143 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini3,1", M_MINI_3_1),
144 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini4,1", M_MINI_4_1),
Peter Jones7c08c9a2008-10-15 22:03:43 -0700145 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook1,1", M_MB),
146 /* At least one of these two will be right; maybe both? */
147 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook2,1", M_MB),
148 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook2,1", M_MB),
149 /* At least one of these two will be right; maybe both? */
150 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook3,1", M_MB),
151 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook3,1", M_MB),
152 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook4,1", M_MB),
Luke Mackena5757c22010-09-22 13:05:04 -0700153 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook5,1", M_MB_5_1),
154 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook6,1", M_MB_6_1),
155 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook7,1", M_MB_7_1),
Peter Jones7c08c9a2008-10-15 22:03:43 -0700156 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA),
Matthew Garrett97fb8502011-04-06 17:34:59 +0000157 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir3,1", M_MBA_3),
Peter Jones7c08c9a2008-10-15 22:03:43 -0700158 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP),
159 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2),
Luke Mackena5757c22010-09-22 13:05:04 -0700160 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,2", M_MBP_2_2),
Peter Jones7c08c9a2008-10-15 22:03:43 -0700161 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro2,1", M_MBP_2),
162 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro3,1", M_MBP_SR),
163 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro3,1", M_MBP_SR),
164 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro4,1", M_MBP_4),
Thomas Gerlach8a3bdfe2010-04-23 13:17:50 -0400165 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,1", M_MBP_5_1),
Luke Mackena5757c22010-09-22 13:05:04 -0700166 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,2", M_MBP_5_2),
167 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,3", M_MBP_5_3),
168 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,1", M_MBP_6_1),
169 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,2", M_MBP_6_2),
170 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro7,1", M_MBP_7_1),
Davidlohr Buesoe9c5db02011-03-31 00:12:22 +0000171 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro8,2", M_MBP_8_2),
Peter Jones7c08c9a2008-10-15 22:03:43 -0700172 {},
173};
174
Peter Jones47dfe512011-04-06 17:34:58 +0000175#define choose_value(dmivalue, fwvalue, field, flags) ({ \
176 typeof(fwvalue) _ret_ = fwvalue; \
177 if ((flags) & (field)) \
178 _ret_ = dmivalue; \
179 else if ((fwvalue) == 0) \
180 _ret_ = dmivalue; \
181 _ret_; \
182 })
183
Peter Jones7c08c9a2008-10-15 22:03:43 -0700184static int set_system(const struct dmi_system_id *id)
185{
186 struct efifb_dmi_info *info = id->driver_data;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700187
Peter Jones47dfe512011-04-06 17:34:58 +0000188 if (info->base == 0 && info->height == 0 && info->width == 0
189 && info->stride == 0)
190 return 0;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700191
192 /* Trust the bootloader over the DMI tables */
Peter Jones85a00d92010-09-22 13:05:04 -0700193 if (screen_info.lfb_base == 0) {
194#if defined(CONFIG_PCI)
195 struct pci_dev *dev = NULL;
196 int found_bar = 0;
197#endif
Peter Jones47dfe512011-04-06 17:34:58 +0000198 if (info->base) {
199 screen_info.lfb_base = choose_value(info->base,
200 screen_info.lfb_base, OVERRIDE_BASE,
201 info->flags);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700202
Peter Jones85a00d92010-09-22 13:05:04 -0700203#if defined(CONFIG_PCI)
Peter Jones47dfe512011-04-06 17:34:58 +0000204 /* make sure that the address in the table is actually
205 * on a VGA device's PCI BAR */
Peter Jones85a00d92010-09-22 13:05:04 -0700206
Peter Jones47dfe512011-04-06 17:34:58 +0000207 for_each_pci_dev(dev) {
208 int i;
209 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
210 continue;
211 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
212 resource_size_t start, end;
Peter Jones85a00d92010-09-22 13:05:04 -0700213
Peter Jones47dfe512011-04-06 17:34:58 +0000214 start = pci_resource_start(dev, i);
215 if (start == 0)
216 break;
217 end = pci_resource_end(dev, i);
218 if (screen_info.lfb_base >= start &&
219 screen_info.lfb_base < end) {
220 found_bar = 1;
221 }
Peter Jones85a00d92010-09-22 13:05:04 -0700222 }
223 }
Peter Jones47dfe512011-04-06 17:34:58 +0000224 if (!found_bar)
225 screen_info.lfb_base = 0;
Peter Jones85a00d92010-09-22 13:05:04 -0700226#endif
Peter Jones47dfe512011-04-06 17:34:58 +0000227 }
Peter Jones85a00d92010-09-22 13:05:04 -0700228 }
229 if (screen_info.lfb_base) {
Peter Jones47dfe512011-04-06 17:34:58 +0000230 screen_info.lfb_linelength = choose_value(info->stride,
231 screen_info.lfb_linelength, OVERRIDE_STRIDE,
232 info->flags);
233 screen_info.lfb_width = choose_value(info->width,
234 screen_info.lfb_width, OVERRIDE_WIDTH,
235 info->flags);
236 screen_info.lfb_height = choose_value(info->height,
237 screen_info.lfb_height, OVERRIDE_HEIGHT,
238 info->flags);
Peter Jones85a00d92010-09-22 13:05:04 -0700239 if (screen_info.orig_video_isVGA == 0)
240 screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
241 } else {
242 screen_info.lfb_linelength = 0;
243 screen_info.lfb_width = 0;
244 screen_info.lfb_height = 0;
245 screen_info.orig_video_isVGA = 0;
246 return 0;
247 }
Peter Jones47dfe512011-04-06 17:34:58 +0000248
Konstantin Khlebnikovdc3e5b62011-04-19 09:47:15 +0000249 printk(KERN_INFO "efifb: dmi detected %s - framebuffer at 0x%08x "
Peter Jones47dfe512011-04-06 17:34:58 +0000250 "(%dx%d, stride %d)\n", id->ident,
Konstantin Khlebnikovdc3e5b62011-04-19 09:47:15 +0000251 screen_info.lfb_base, screen_info.lfb_width,
Peter Jones47dfe512011-04-06 17:34:58 +0000252 screen_info.lfb_height, screen_info.lfb_linelength);
253
254
Peter Jones85a00d92010-09-22 13:05:04 -0700255 return 1;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700256}
257
Huang, Ying7c831722007-11-28 16:21:55 -0800258static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
259 unsigned blue, unsigned transp,
260 struct fb_info *info)
261{
262 /*
263 * Set a single color register. The values supplied are
264 * already rounded down to the hardware's capabilities
265 * (according to the entries in the `var' structure). Return
266 * != 0 for invalid regno.
267 */
268
269 if (regno >= info->cmap.len)
270 return 1;
271
272 if (regno < 16) {
273 red >>= 8;
274 green >>= 8;
275 blue >>= 8;
276 ((u32 *)(info->pseudo_palette))[regno] =
277 (red << info->var.red.offset) |
278 (green << info->var.green.offset) |
279 (blue << info->var.blue.offset);
280 }
281 return 0;
282}
283
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800284static void efifb_destroy(struct fb_info *info)
285{
286 if (info->screen_base)
287 iounmap(info->screen_base);
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400288 if (request_mem_succeeded)
289 release_mem_region(info->apertures->ranges[0].base,
290 info->apertures->ranges[0].size);
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800291 framebuffer_release(info);
292}
293
Huang, Ying7c831722007-11-28 16:21:55 -0800294static struct fb_ops efifb_ops = {
295 .owner = THIS_MODULE,
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800296 .fb_destroy = efifb_destroy,
Huang, Ying7c831722007-11-28 16:21:55 -0800297 .fb_setcolreg = efifb_setcolreg,
298 .fb_fillrect = cfb_fillrect,
299 .fb_copyarea = cfb_copyarea,
300 .fb_imageblit = cfb_imageblit,
301};
302
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400303struct pci_dev *vga_default_device(void)
304{
305 return default_vga;
306}
307
Matthew Garrett1b231702012-04-24 09:31:28 +0100308EXPORT_SYMBOL_GPL(vga_default_device);
309
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400310void vga_set_default_device(struct pci_dev *pdev)
311{
312 default_vga = pdev;
313}
314
Peter Jones7c08c9a2008-10-15 22:03:43 -0700315static int __init efifb_setup(char *options)
316{
317 char *this_opt;
318 int i;
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400319 struct pci_dev *dev = NULL;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700320
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400321 if (options && *options) {
322 while ((this_opt = strsep(&options, ",")) != NULL) {
323 if (!*this_opt) continue;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700324
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400325 for (i = 0; i < M_UNKNOWN; i++) {
326 if (!strcmp(this_opt, dmi_list[i].optname) &&
327 dmi_list[i].base != 0) {
328 screen_info.lfb_base = dmi_list[i].base;
329 screen_info.lfb_linelength = dmi_list[i].stride;
330 screen_info.lfb_width = dmi_list[i].width;
331 screen_info.lfb_height = dmi_list[i].height;
332 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700333 }
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400334 if (!strncmp(this_opt, "base:", 5))
335 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
336 else if (!strncmp(this_opt, "stride:", 7))
337 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
338 else if (!strncmp(this_opt, "height:", 7))
339 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
340 else if (!strncmp(this_opt, "width:", 6))
341 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700342 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700343 }
Matthew Garrettb4aa0162012-04-16 16:26:05 -0400344
345 for_each_pci_dev(dev) {
346 int i;
347
348 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
349 continue;
350
351 for (i=0; i < DEVICE_COUNT_RESOURCE; i++) {
352 resource_size_t start, end;
353
354 if (!(pci_resource_flags(dev, i) & IORESOURCE_MEM))
355 continue;
356
357 start = pci_resource_start(dev, i);
358 end = pci_resource_end(dev, i);
359
360 if (!start || !end)
361 continue;
362
363 if (screen_info.lfb_base >= start &&
364 (screen_info.lfb_base + screen_info.lfb_size) < end)
365 default_vga = dev;
366 }
367 }
368
Peter Jones7c08c9a2008-10-15 22:03:43 -0700369 return 0;
370}
371
Andy Lutomirskibb8b2662011-05-26 10:13:34 -0400372static int __init efifb_probe(struct platform_device *dev)
Huang, Ying7c831722007-11-28 16:21:55 -0800373{
374 struct fb_info *info;
375 int err;
376 unsigned int size_vmode;
377 unsigned int size_remap;
378 unsigned int size_total;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700379
Peter Jones7c08c9a2008-10-15 22:03:43 -0700380 if (!screen_info.lfb_depth)
381 screen_info.lfb_depth = 32;
382 if (!screen_info.pages)
383 screen_info.pages = 1;
Matthew Garrett133bb072009-04-13 14:39:44 -0700384 if (!screen_info.lfb_base) {
385 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
386 return -ENODEV;
387 }
388 printk(KERN_INFO "efifb: probing for efifb\n");
Peter Jones7c08c9a2008-10-15 22:03:43 -0700389
390 /* just assume they're all unset if any are */
391 if (!screen_info.blue_size) {
392 screen_info.blue_size = 8;
393 screen_info.blue_pos = 0;
394 screen_info.green_size = 8;
395 screen_info.green_pos = 8;
396 screen_info.red_size = 8;
397 screen_info.red_pos = 16;
398 screen_info.rsvd_size = 8;
399 screen_info.rsvd_pos = 24;
400 }
Huang, Ying7c831722007-11-28 16:21:55 -0800401
402 efifb_fix.smem_start = screen_info.lfb_base;
403 efifb_defined.bits_per_pixel = screen_info.lfb_depth;
404 efifb_defined.xres = screen_info.lfb_width;
405 efifb_defined.yres = screen_info.lfb_height;
406 efifb_fix.line_length = screen_info.lfb_linelength;
407
408 /* size_vmode -- that is the amount of memory needed for the
409 * used video mode, i.e. the minimum amount of
410 * memory we need. */
411 size_vmode = efifb_defined.yres * efifb_fix.line_length;
412
413 /* size_total -- all video memory we have. Used for
414 * entries, ressource allocation and bounds
415 * checking. */
416 size_total = screen_info.lfb_size;
417 if (size_total < size_vmode)
418 size_total = size_vmode;
419
420 /* size_remap -- the amount of video memory we are going to
421 * use for efifb. With modern cards it is no
422 * option to simply use size_total as that
423 * wastes plenty of kernel address space. */
424 size_remap = size_vmode * 2;
Huang, Ying7c831722007-11-28 16:21:55 -0800425 if (size_remap > size_total)
426 size_remap = size_total;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700427 if (size_remap % PAGE_SIZE)
428 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
Huang, Ying7c831722007-11-28 16:21:55 -0800429 efifb_fix.smem_len = size_remap;
430
Peter Jones7c08c9a2008-10-15 22:03:43 -0700431 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400432 request_mem_succeeded = true;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700433 } else {
Huang, Ying7c831722007-11-28 16:21:55 -0800434 /* We cannot make this fatal. Sometimes this comes from magic
435 spaces our resource handlers simply don't know about */
436 printk(KERN_WARNING
437 "efifb: cannot reserve video memory at 0x%lx\n",
438 efifb_fix.smem_start);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700439 }
Huang, Ying7c831722007-11-28 16:21:55 -0800440
441 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
442 if (!info) {
Peter Jones7c08c9a2008-10-15 22:03:43 -0700443 printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800444 err = -ENOMEM;
445 goto err_release_mem;
446 }
447 info->pseudo_palette = info->par;
448 info->par = NULL;
449
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200450 info->apertures = alloc_apertures(1);
451 if (!info->apertures) {
452 err = -ENOMEM;
453 goto err_release_fb;
454 }
455 info->apertures->ranges[0].base = efifb_fix.smem_start;
456 info->apertures->ranges[0].size = size_remap;
Dave Airlie4410f392009-06-16 15:34:38 -0700457
Andy Lutomirski3c004b4f2011-05-26 10:13:31 -0400458 info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
Huang, Ying7c831722007-11-28 16:21:55 -0800459 if (!info->screen_base) {
460 printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
461 "0x%x @ 0x%lx\n",
462 efifb_fix.smem_len, efifb_fix.smem_start);
463 err = -EIO;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700464 goto err_release_fb;
Huang, Ying7c831722007-11-28 16:21:55 -0800465 }
466
467 printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
468 "using %dk, total %dk\n",
469 efifb_fix.smem_start, info->screen_base,
470 size_remap/1024, size_total/1024);
471 printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
472 efifb_defined.xres, efifb_defined.yres,
473 efifb_defined.bits_per_pixel, efifb_fix.line_length,
474 screen_info.pages);
475
476 efifb_defined.xres_virtual = efifb_defined.xres;
477 efifb_defined.yres_virtual = efifb_fix.smem_len /
478 efifb_fix.line_length;
479 printk(KERN_INFO "efifb: scrolling: redraw\n");
480 efifb_defined.yres_virtual = efifb_defined.yres;
481
482 /* some dummy values for timing to make fbset happy */
483 efifb_defined.pixclock = 10000000 / efifb_defined.xres *
484 1000 / efifb_defined.yres;
485 efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
486 efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
487
488 efifb_defined.red.offset = screen_info.red_pos;
489 efifb_defined.red.length = screen_info.red_size;
490 efifb_defined.green.offset = screen_info.green_pos;
491 efifb_defined.green.length = screen_info.green_size;
492 efifb_defined.blue.offset = screen_info.blue_pos;
493 efifb_defined.blue.length = screen_info.blue_size;
494 efifb_defined.transp.offset = screen_info.rsvd_pos;
495 efifb_defined.transp.length = screen_info.rsvd_size;
496
497 printk(KERN_INFO "efifb: %s: "
498 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
499 "Truecolor",
500 screen_info.rsvd_size,
501 screen_info.red_size,
502 screen_info.green_size,
503 screen_info.blue_size,
504 screen_info.rsvd_pos,
505 screen_info.red_pos,
506 screen_info.green_pos,
507 screen_info.blue_pos);
508
509 efifb_fix.ypanstep = 0;
510 efifb_fix.ywrapstep = 0;
511
512 info->fbops = &efifb_ops;
513 info->var = efifb_defined;
514 info->fix = efifb_fix;
Dave Airlie4410f392009-06-16 15:34:38 -0700515 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
Huang, Ying7c831722007-11-28 16:21:55 -0800516
Peter Jones7c08c9a2008-10-15 22:03:43 -0700517 if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
518 printk(KERN_ERR "efifb: cannot allocate colormap\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800519 goto err_unmap;
520 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700521 if ((err = register_framebuffer(info)) < 0) {
522 printk(KERN_ERR "efifb: cannot register framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800523 goto err_fb_dealoc;
524 }
525 printk(KERN_INFO "fb%d: %s frame buffer device\n",
Peter Jones7c08c9a2008-10-15 22:03:43 -0700526 info->node, info->fix.id);
Huang, Ying7c831722007-11-28 16:21:55 -0800527 return 0;
528
529err_fb_dealoc:
530 fb_dealloc_cmap(&info->cmap);
531err_unmap:
532 iounmap(info->screen_base);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700533err_release_fb:
Huang, Ying7c831722007-11-28 16:21:55 -0800534 framebuffer_release(info);
535err_release_mem:
Andy Lutomirskida0241f2011-05-26 10:13:32 -0400536 if (request_mem_succeeded)
Peter Jones7c08c9a2008-10-15 22:03:43 -0700537 release_mem_region(efifb_fix.smem_start, size_total);
Huang, Ying7c831722007-11-28 16:21:55 -0800538 return err;
539}
540
541static struct platform_driver efifb_driver = {
Huang, Ying7c831722007-11-28 16:21:55 -0800542 .driver = {
543 .name = "efifb",
544 },
545};
546
547static struct platform_device efifb_device = {
548 .name = "efifb",
549};
550
551static int __init efifb_init(void)
552{
553 int ret;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700554 char *option = NULL;
Huang, Ying7c831722007-11-28 16:21:55 -0800555
Matthew Garrettf462ed92012-07-27 12:58:53 -0400556 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI ||
557 !(screen_info.capabilities & VIDEO_CAPABILITY_SKIP_QUIRKS))
558 dmi_check_system(dmi_system_table);
Brian Malye14a6852009-03-31 15:25:50 -0700559
Huang, Ying7c831722007-11-28 16:21:55 -0800560 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
561 return -ENODEV;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700562
563 if (fb_get_options("efifb", &option))
564 return -ENODEV;
565 efifb_setup(option);
566
567 /* We don't get linelength from UGA Draw Protocol, only from
568 * EFI Graphics Protocol. So if it's not in DMI, and it's not
569 * passed in from the user, we really can't use the framebuffer.
570 */
571 if (!screen_info.lfb_linelength)
572 return -ENODEV;
Huang, Ying7c831722007-11-28 16:21:55 -0800573
Andy Lutomirskibb8b2662011-05-26 10:13:34 -0400574 ret = platform_device_register(&efifb_device);
575 if (ret)
576 return ret;
Huang, Ying7c831722007-11-28 16:21:55 -0800577
Andy Lutomirskibb8b2662011-05-26 10:13:34 -0400578 /*
579 * This is not just an optimization. We will interfere
580 * with a real driver if we get reprobed, so don't allow
581 * it.
582 */
583 ret = platform_driver_probe(&efifb_driver, efifb_probe);
584 if (ret) {
Wanlong Gaoe6b84802011-06-12 10:52:33 +0000585 platform_device_unregister(&efifb_device);
Andy Lutomirskibb8b2662011-05-26 10:13:34 -0400586 return ret;
Huang, Ying7c831722007-11-28 16:21:55 -0800587 }
Andy Lutomirskibb8b2662011-05-26 10:13:34 -0400588
Huang, Ying7c831722007-11-28 16:21:55 -0800589 return ret;
590}
591module_init(efifb_init);
592
593MODULE_LICENSE("GPL");