]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/video/efifb.c
efifb: check that the base address is plausible on pci systems
[net-next-2.6.git] / drivers / video / efifb.c
CommitLineData
7c83172b
HY
1/*
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>
7c08c9ae 15#include <linux/dmi.h>
85a00d9b 16#include <linux/pci.h>
7c83172b
HY
17#include <video/vga.h>
18
ec81c9cc 19static struct fb_var_screeninfo efifb_defined __devinitdata = {
7c83172b
HY
20 .activate = FB_ACTIVATE_NOW,
21 .height = -1,
22 .width = -1,
23 .right_margin = 32,
24 .upper_margin = 16,
25 .lower_margin = 4,
26 .vsync_len = 4,
27 .vmode = FB_VMODE_NONINTERLACED,
28};
29
ec81c9cc 30static struct fb_fix_screeninfo efifb_fix __devinitdata = {
7c83172b
HY
31 .id = "EFI VGA",
32 .type = FB_TYPE_PACKED_PIXELS,
33 .accel = FB_ACCEL_NONE,
34 .visual = FB_VISUAL_TRUECOLOR,
35};
36
7c08c9ae
PJ
37enum {
38 M_I17, /* 17-Inch iMac */
39 M_I20, /* 20-Inch iMac */
40 M_I20_SR, /* 20-Inch iMac (Santa Rosa) */
41 M_I24, /* 24-Inch iMac */
42 M_MINI, /* Mac Mini */
43 M_MB, /* MacBook */
44 M_MB_2, /* MacBook, 2nd rev. */
45 M_MB_3, /* MacBook, 3rd rev. */
46 M_MB_SR, /* MacBook, 2nd gen, (Santa Rosa) */
47 M_MBA, /* MacBook Air */
48 M_MBP, /* MacBook Pro */
49 M_MBP_2, /* MacBook Pro 2nd gen */
50 M_MBP_SR, /* MacBook Pro (Santa Rosa) */
51 M_MBP_4, /* MacBook Pro, 4th gen */
8a3bdfe6 52 M_MBP_5_1, /* MacBook Pro, 5,1th gen */
7c08c9ae
PJ
53 M_UNKNOWN /* placeholder */
54};
55
56static struct efifb_dmi_info {
57 char *optname;
58 unsigned long base;
59 int stride;
60 int width;
61 int height;
ec81c9cc 62} dmi_list[] __initdata = {
7c08c9ae
PJ
63 [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900 },
64 [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050 }, /* guess */
65 [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050 },
66 [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200 }, /* guess */
67 [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768 },
68 [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800 },
69 [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800 },
70 [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900 },
71 [M_MBP_2] = { "mbp2", 0, 0, 0, 0 }, /* placeholder */
72 [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900 },
73 [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200 },
8a3bdfe6 74 [M_MBP_5_1] = { "mbp51", 0xc0010000, 2048 * 4, 1440, 900 },
7c08c9ae
PJ
75 [M_UNKNOWN] = { NULL, 0, 0, 0, 0 }
76};
77
78static int set_system(const struct dmi_system_id *id);
79
80#define EFIFB_DMI_SYSTEM_ID(vendor, name, enumid) \
81 { set_system, name, { \
82 DMI_MATCH(DMI_BIOS_VENDOR, vendor), \
83 DMI_MATCH(DMI_PRODUCT_NAME, name) }, \
84 &dmi_list[enumid] }
85
ec81c9cc 86static const struct dmi_system_id dmi_system_table[] __initconst = {
7c08c9ae
PJ
87 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac4,1", M_I17),
88 /* At least one of these two will be right; maybe both? */
89 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac5,1", M_I20),
90 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac5,1", M_I20),
91 /* At least one of these two will be right; maybe both? */
92 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac6,1", M_I24),
93 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac6,1", M_I24),
94 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac7,1", M_I20_SR),
95 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "Macmini1,1", M_MINI),
96 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook1,1", M_MB),
97 /* At least one of these two will be right; maybe both? */
98 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook2,1", M_MB),
99 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook2,1", M_MB),
100 /* At least one of these two will be right; maybe both? */
101 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook3,1", M_MB),
102 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook3,1", M_MB),
103 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook4,1", M_MB),
104 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA),
105 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP),
106 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2),
107 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro2,1", M_MBP_2),
108 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro3,1", M_MBP_SR),
109 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro3,1", M_MBP_SR),
110 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro4,1", M_MBP_4),
8a3bdfe6 111 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,1", M_MBP_5_1),
7c08c9ae
PJ
112 {},
113};
114
115static int set_system(const struct dmi_system_id *id)
116{
117 struct efifb_dmi_info *info = id->driver_data;
118 if (info->base == 0)
85a00d9b 119 return 0;
7c08c9ae
PJ
120
121 printk(KERN_INFO "efifb: dmi detected %s - framebuffer at %p "
122 "(%dx%d, stride %d)\n", id->ident,
123 (void *)info->base, info->width, info->height,
124 info->stride);
125
126 /* Trust the bootloader over the DMI tables */
85a00d9b
PJ
127 if (screen_info.lfb_base == 0) {
128#if defined(CONFIG_PCI)
129 struct pci_dev *dev = NULL;
130 int found_bar = 0;
131#endif
7c08c9ae 132 screen_info.lfb_base = info->base;
7c08c9ae 133
85a00d9b
PJ
134#if defined(CONFIG_PCI)
135 /* make sure that the address in the table is actually on a
136 * VGA device's PCI BAR */
137
138 for_each_pci_dev(dev) {
139 int i;
140 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
141 continue;
142 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
143 resource_size_t start, end;
144
145 start = pci_resource_start(dev, i);
146 if (start == 0)
147 break;
148 end = pci_resource_end(dev, i);
149 if (screen_info.lfb_base >= start &&
150 screen_info.lfb_base < end) {
151 found_bar = 1;
152 }
153 }
154 }
155 if (!found_bar)
156 screen_info.lfb_base = 0;
157#endif
158 }
159 if (screen_info.lfb_base) {
160 if (screen_info.lfb_linelength == 0)
161 screen_info.lfb_linelength = info->stride;
162 if (screen_info.lfb_width == 0)
163 screen_info.lfb_width = info->width;
164 if (screen_info.lfb_height == 0)
165 screen_info.lfb_height = info->height;
166 if (screen_info.orig_video_isVGA == 0)
167 screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
168 } else {
169 screen_info.lfb_linelength = 0;
170 screen_info.lfb_width = 0;
171 screen_info.lfb_height = 0;
172 screen_info.orig_video_isVGA = 0;
173 return 0;
174 }
175 return 1;
7c08c9ae
PJ
176}
177
7c83172b
HY
178static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
179 unsigned blue, unsigned transp,
180 struct fb_info *info)
181{
182 /*
183 * Set a single color register. The values supplied are
184 * already rounded down to the hardware's capabilities
185 * (according to the entries in the `var' structure). Return
186 * != 0 for invalid regno.
187 */
188
189 if (regno >= info->cmap.len)
190 return 1;
191
192 if (regno < 16) {
193 red >>= 8;
194 green >>= 8;
195 blue >>= 8;
196 ((u32 *)(info->pseudo_palette))[regno] =
197 (red << info->var.red.offset) |
198 (green << info->var.green.offset) |
199 (blue << info->var.blue.offset);
200 }
201 return 0;
202}
203
89f3f219
MS
204static void efifb_destroy(struct fb_info *info)
205{
206 if (info->screen_base)
207 iounmap(info->screen_base);
1471ca9a 208 release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size);
89f3f219
MS
209 framebuffer_release(info);
210}
211
7c83172b
HY
212static struct fb_ops efifb_ops = {
213 .owner = THIS_MODULE,
89f3f219 214 .fb_destroy = efifb_destroy,
7c83172b
HY
215 .fb_setcolreg = efifb_setcolreg,
216 .fb_fillrect = cfb_fillrect,
217 .fb_copyarea = cfb_copyarea,
218 .fb_imageblit = cfb_imageblit,
219};
220
7c08c9ae
PJ
221static int __init efifb_setup(char *options)
222{
223 char *this_opt;
224 int i;
225
226 if (!options || !*options)
227 return 0;
228
229 while ((this_opt = strsep(&options, ",")) != NULL) {
230 if (!*this_opt) continue;
231
232 for (i = 0; i < M_UNKNOWN; i++) {
233 if (!strcmp(this_opt, dmi_list[i].optname) &&
234 dmi_list[i].base != 0) {
235 screen_info.lfb_base = dmi_list[i].base;
236 screen_info.lfb_linelength = dmi_list[i].stride;
237 screen_info.lfb_width = dmi_list[i].width;
238 screen_info.lfb_height = dmi_list[i].height;
239 }
240 }
241 if (!strncmp(this_opt, "base:", 5))
242 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
243 else if (!strncmp(this_opt, "stride:", 7))
244 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
245 else if (!strncmp(this_opt, "height:", 7))
246 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
247 else if (!strncmp(this_opt, "width:", 6))
248 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
249 }
250 return 0;
251}
252
c2e13037 253static int __devinit efifb_probe(struct platform_device *dev)
7c83172b
HY
254{
255 struct fb_info *info;
256 int err;
257 unsigned int size_vmode;
258 unsigned int size_remap;
259 unsigned int size_total;
7c08c9ae
PJ
260 int request_succeeded = 0;
261
7c08c9ae
PJ
262 if (!screen_info.lfb_depth)
263 screen_info.lfb_depth = 32;
264 if (!screen_info.pages)
265 screen_info.pages = 1;
133bb070
MG
266 if (!screen_info.lfb_base) {
267 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
268 return -ENODEV;
269 }
270 printk(KERN_INFO "efifb: probing for efifb\n");
7c08c9ae
PJ
271
272 /* just assume they're all unset if any are */
273 if (!screen_info.blue_size) {
274 screen_info.blue_size = 8;
275 screen_info.blue_pos = 0;
276 screen_info.green_size = 8;
277 screen_info.green_pos = 8;
278 screen_info.red_size = 8;
279 screen_info.red_pos = 16;
280 screen_info.rsvd_size = 8;
281 screen_info.rsvd_pos = 24;
282 }
7c83172b
HY
283
284 efifb_fix.smem_start = screen_info.lfb_base;
285 efifb_defined.bits_per_pixel = screen_info.lfb_depth;
286 efifb_defined.xres = screen_info.lfb_width;
287 efifb_defined.yres = screen_info.lfb_height;
288 efifb_fix.line_length = screen_info.lfb_linelength;
289
290 /* size_vmode -- that is the amount of memory needed for the
291 * used video mode, i.e. the minimum amount of
292 * memory we need. */
293 size_vmode = efifb_defined.yres * efifb_fix.line_length;
294
295 /* size_total -- all video memory we have. Used for
296 * entries, ressource allocation and bounds
297 * checking. */
298 size_total = screen_info.lfb_size;
299 if (size_total < size_vmode)
300 size_total = size_vmode;
301
302 /* size_remap -- the amount of video memory we are going to
303 * use for efifb. With modern cards it is no
304 * option to simply use size_total as that
305 * wastes plenty of kernel address space. */
306 size_remap = size_vmode * 2;
7c83172b
HY
307 if (size_remap > size_total)
308 size_remap = size_total;
7c08c9ae
PJ
309 if (size_remap % PAGE_SIZE)
310 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
7c83172b
HY
311 efifb_fix.smem_len = size_remap;
312
7c08c9ae
PJ
313 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
314 request_succeeded = 1;
315 } else {
7c83172b
HY
316 /* We cannot make this fatal. Sometimes this comes from magic
317 spaces our resource handlers simply don't know about */
318 printk(KERN_WARNING
319 "efifb: cannot reserve video memory at 0x%lx\n",
320 efifb_fix.smem_start);
7c08c9ae 321 }
7c83172b
HY
322
323 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
324 if (!info) {
7c08c9ae 325 printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
7c83172b
HY
326 err = -ENOMEM;
327 goto err_release_mem;
328 }
329 info->pseudo_palette = info->par;
330 info->par = NULL;
331
1471ca9a
MS
332 info->apertures = alloc_apertures(1);
333 if (!info->apertures) {
334 err = -ENOMEM;
335 goto err_release_fb;
336 }
337 info->apertures->ranges[0].base = efifb_fix.smem_start;
338 info->apertures->ranges[0].size = size_remap;
4410f391 339
7c83172b
HY
340 info->screen_base = ioremap(efifb_fix.smem_start, efifb_fix.smem_len);
341 if (!info->screen_base) {
342 printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
343 "0x%x @ 0x%lx\n",
344 efifb_fix.smem_len, efifb_fix.smem_start);
345 err = -EIO;
7c08c9ae 346 goto err_release_fb;
7c83172b
HY
347 }
348
349 printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
350 "using %dk, total %dk\n",
351 efifb_fix.smem_start, info->screen_base,
352 size_remap/1024, size_total/1024);
353 printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
354 efifb_defined.xres, efifb_defined.yres,
355 efifb_defined.bits_per_pixel, efifb_fix.line_length,
356 screen_info.pages);
357
358 efifb_defined.xres_virtual = efifb_defined.xres;
359 efifb_defined.yres_virtual = efifb_fix.smem_len /
360 efifb_fix.line_length;
361 printk(KERN_INFO "efifb: scrolling: redraw\n");
362 efifb_defined.yres_virtual = efifb_defined.yres;
363
364 /* some dummy values for timing to make fbset happy */
365 efifb_defined.pixclock = 10000000 / efifb_defined.xres *
366 1000 / efifb_defined.yres;
367 efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
368 efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
369
370 efifb_defined.red.offset = screen_info.red_pos;
371 efifb_defined.red.length = screen_info.red_size;
372 efifb_defined.green.offset = screen_info.green_pos;
373 efifb_defined.green.length = screen_info.green_size;
374 efifb_defined.blue.offset = screen_info.blue_pos;
375 efifb_defined.blue.length = screen_info.blue_size;
376 efifb_defined.transp.offset = screen_info.rsvd_pos;
377 efifb_defined.transp.length = screen_info.rsvd_size;
378
379 printk(KERN_INFO "efifb: %s: "
380 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
381 "Truecolor",
382 screen_info.rsvd_size,
383 screen_info.red_size,
384 screen_info.green_size,
385 screen_info.blue_size,
386 screen_info.rsvd_pos,
387 screen_info.red_pos,
388 screen_info.green_pos,
389 screen_info.blue_pos);
390
391 efifb_fix.ypanstep = 0;
392 efifb_fix.ywrapstep = 0;
393
394 info->fbops = &efifb_ops;
395 info->var = efifb_defined;
396 info->fix = efifb_fix;
4410f391 397 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
7c83172b 398
7c08c9ae
PJ
399 if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
400 printk(KERN_ERR "efifb: cannot allocate colormap\n");
7c83172b
HY
401 goto err_unmap;
402 }
7c08c9ae
PJ
403 if ((err = register_framebuffer(info)) < 0) {
404 printk(KERN_ERR "efifb: cannot register framebuffer\n");
7c83172b
HY
405 goto err_fb_dealoc;
406 }
407 printk(KERN_INFO "fb%d: %s frame buffer device\n",
7c08c9ae 408 info->node, info->fix.id);
7c83172b
HY
409 return 0;
410
411err_fb_dealoc:
412 fb_dealloc_cmap(&info->cmap);
413err_unmap:
414 iounmap(info->screen_base);
7c08c9ae 415err_release_fb:
7c83172b
HY
416 framebuffer_release(info);
417err_release_mem:
7c08c9ae
PJ
418 if (request_succeeded)
419 release_mem_region(efifb_fix.smem_start, size_total);
7c83172b
HY
420 return err;
421}
422
423static struct platform_driver efifb_driver = {
424 .probe = efifb_probe,
425 .driver = {
426 .name = "efifb",
427 },
428};
429
430static struct platform_device efifb_device = {
431 .name = "efifb",
432};
433
434static int __init efifb_init(void)
435{
436 int ret;
7c08c9ae 437 char *option = NULL;
7c83172b 438
e14a685d
BM
439 dmi_check_system(dmi_system_table);
440
7c83172b
HY
441 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
442 return -ENODEV;
7c08c9ae
PJ
443
444 if (fb_get_options("efifb", &option))
445 return -ENODEV;
446 efifb_setup(option);
447
448 /* We don't get linelength from UGA Draw Protocol, only from
449 * EFI Graphics Protocol. So if it's not in DMI, and it's not
450 * passed in from the user, we really can't use the framebuffer.
451 */
452 if (!screen_info.lfb_linelength)
453 return -ENODEV;
7c83172b
HY
454
455 ret = platform_driver_register(&efifb_driver);
456
457 if (!ret) {
458 ret = platform_device_register(&efifb_device);
459 if (ret)
460 platform_driver_unregister(&efifb_driver);
461 }
462 return ret;
463}
464module_init(efifb_init);
465
466MODULE_LICENSE("GPL");