intelfb: use firmware EDID for mode database
[safe/jmp/linux-2.6] / drivers / video / intelfb / intelfbdrv.c
1 /*
2  * intelfb
3  *
4  * Linux framebuffer driver for Intel(R) 830M/845G/852GM/855GM/865G/915G/915GM/
5  * 945G/945GM integrated graphics chips.
6  *
7  * Copyright © 2002, 2003 David Dawes <dawes@xfree86.org>
8  *                   2004 Sylvain Meyer
9  *                   2006 David Airlie
10  *
11  * This driver consists of two parts.  The first part (intelfbdrv.c) provides
12  * the basic fbdev interfaces, is derived in part from the radeonfb and
13  * vesafb drivers, and is covered by the GPL.  The second part (intelfbhw.c)
14  * provides the code to program the hardware.  Most of it is derived from
15  * the i810/i830 XFree86 driver.  The HW-specific code is covered here
16  * under a dual license (GPL and MIT/XFree86 license).
17  *
18  * Author: David Dawes
19  *
20  */
21
22 /* $DHD: intelfb/intelfbdrv.c,v 1.20 2003/06/27 15:17:40 dawes Exp $ */
23
24 /*
25  * Changes:
26  *    01/2003 - Initial driver (0.1.0), no mode switching, no acceleration.
27  *              This initial version is a basic core that works a lot like
28  *              the vesafb driver.  It must be built-in to the kernel,
29  *              and the initial video mode must be set with vga=XXX at
30  *              boot time.  (David Dawes)
31  *
32  *    01/2003 - Version 0.2.0: Mode switching added, colormap support
33  *              implemented, Y panning, and soft screen blanking implemented.
34  *              No acceleration yet.  (David Dawes)
35  *
36  *    01/2003 - Version 0.3.0: fbcon acceleration support added.  Module
37  *              option handling added.  (David Dawes)
38  *
39  *    01/2003 - Version 0.4.0: fbcon HW cursor support added.  (David Dawes)
40  *
41  *    01/2003 - Version 0.4.1: Add auto-generation of built-in modes.
42  *              (David Dawes)
43  *
44  *    02/2003 - Version 0.4.2: Add check for active non-CRT devices, and
45  *              mode validation checks.  (David Dawes)
46  *
47  *    02/2003 - Version 0.4.3: Check when the VC is in graphics mode so that
48  *              acceleration is disabled while an XFree86 server is running.
49  *              (David Dawes)
50  *
51  *    02/2003 - Version 0.4.4: Monitor DPMS support.  (David Dawes)
52  *
53  *    02/2003 - Version 0.4.5: Basic XFree86 + fbdev working.  (David Dawes)
54  *
55  *    02/2003 - Version 0.5.0: Modify to work with the 2.5.32 kernel as well
56  *              as 2.4.x kernels.  (David Dawes)
57  *
58  *    02/2003 - Version 0.6.0: Split out HW-specifics into a separate file.
59  *              (David Dawes)
60  *
61  *    02/2003 - Version 0.7.0: Test on 852GM/855GM.  Acceleration and HW
62  *              cursor are disabled on this platform.  (David Dawes)
63  *
64  *    02/2003 - Version 0.7.1: Test on 845G.  Acceleration is disabled
65  *              on this platform.  (David Dawes)
66  *
67  *    02/2003 - Version 0.7.2: Test on 830M.  Acceleration and HW
68  *              cursor are disabled on this platform.  (David Dawes)
69  *
70  *    02/2003 - Version 0.7.3: Fix 8-bit modes for mobile platforms
71  *              (David Dawes)
72  *
73  *    02/2003 - Version 0.7.4: Add checks for FB and FBCON_HAS_CFB* configured
74  *              in the kernel, and add mode bpp verification and default
75  *              bpp selection based on which FBCON_HAS_CFB* are configured.
76  *              (David Dawes)
77  *
78  *    02/2003 - Version 0.7.5: Add basic package/install scripts based on the
79  *              DRI packaging scripts.  (David Dawes)
80  *
81  *    04/2003 - Version 0.7.6: Fix typo that affects builds with SMP-enabled
82  *              kernels.  (David Dawes, reported by Anupam).
83  *
84  *    06/2003 - Version 0.7.7:
85  *              Fix Makefile.kernel build problem (Tsutomu Yasuda).
86  *              Fix mis-placed #endif (2.4.21 kernel).
87  *
88  *    09/2004 - Version 0.9.0 - by Sylvain Meyer
89  *              Port to linux 2.6 kernel fbdev
90  *              Fix HW accel and HW cursor on i845G
91  *              Use of agpgart for fb memory reservation
92  *              Add mtrr support
93  *
94  *    10/2004 - Version 0.9.1
95  *              Use module_param instead of old MODULE_PARM
96  *              Some cleanup
97  *
98  *    11/2004 - Version 0.9.2
99  *              Add vram option to reserve more memory than stolen by BIOS
100  *              Fix intelfbhw_pan_display typo
101  *              Add __initdata annotations
102  *
103  * TODO:
104  *
105  *
106  * Wish List:
107  *
108  *
109  */
110
111 #include <linux/config.h>
112 #include <linux/module.h>
113 #include <linux/kernel.h>
114 #include <linux/errno.h>
115 #include <linux/string.h>
116 #include <linux/mm.h>
117 #include <linux/tty.h>
118 #include <linux/slab.h>
119 #include <linux/delay.h>
120 #include <linux/fb.h>
121 #include <linux/ioport.h>
122 #include <linux/init.h>
123 #include <linux/pci.h>
124 #include <linux/vmalloc.h>
125 #include <linux/pagemap.h>
126
127 #include <asm/io.h>
128
129 #ifdef CONFIG_MTRR
130 #include <asm/mtrr.h>
131 #endif
132
133 #include "intelfb.h"
134 #include "intelfbhw.h"
135 #include "../edid.h"
136
137 static void __devinit get_initial_mode(struct intelfb_info *dinfo);
138 static void update_dinfo(struct intelfb_info *dinfo,
139                          struct fb_var_screeninfo *var);
140 static int intelfb_check_var(struct fb_var_screeninfo *var,
141                              struct fb_info *info);
142 static int intelfb_set_par(struct fb_info *info);
143 static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
144                              unsigned blue, unsigned transp,
145                              struct fb_info *info);
146
147 static int intelfb_blank(int blank, struct fb_info *info);
148 static int intelfb_pan_display(struct fb_var_screeninfo *var,
149                                struct fb_info *info);
150
151 static void intelfb_fillrect(struct fb_info *info,
152                              const struct fb_fillrect *rect);
153 static void intelfb_copyarea(struct fb_info *info,
154                              const struct fb_copyarea *region);
155 static void intelfb_imageblit(struct fb_info *info,
156                               const struct fb_image *image);
157 static int intelfb_cursor(struct fb_info *info,
158                            struct fb_cursor *cursor);
159
160 static int intelfb_sync(struct fb_info *info);
161
162 static int intelfb_ioctl(struct fb_info *info,
163                          unsigned int cmd, unsigned long arg);
164
165 static int __devinit intelfb_pci_register(struct pci_dev *pdev,
166                                           const struct pci_device_id *ent);
167 static void __devexit intelfb_pci_unregister(struct pci_dev *pdev);
168 static int __devinit intelfb_set_fbinfo(struct intelfb_info *dinfo);
169
170 /*
171  * Limiting the class to PCI_CLASS_DISPLAY_VGA prevents function 1 of the
172  * mobile chipsets from being registered.
173  */
174 #if DETECT_VGA_CLASS_ONLY
175 #define INTELFB_CLASS_MASK ~0 << 8
176 #else
177 #define INTELFB_CLASS_MASK 0
178 #endif
179
180 static struct pci_device_id intelfb_pci_table[] __devinitdata = {
181         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_830M, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_830M },
182         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_845G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_845G },
183         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_85XGM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_85XGM },
184         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_865G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_865G },
185         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_915G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_915G },
186         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_915GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_915GM },
187         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945G },
188         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945GM },
189         { 0, }
190 };
191
192 /* Global data */
193 static int num_registered = 0;
194
195 /* fb ops */
196 static struct fb_ops intel_fb_ops = {
197         .owner =                THIS_MODULE,
198         .fb_check_var =         intelfb_check_var,
199         .fb_set_par =           intelfb_set_par,
200         .fb_setcolreg =         intelfb_setcolreg,
201         .fb_blank =             intelfb_blank,
202         .fb_pan_display =       intelfb_pan_display,
203         .fb_fillrect  =         intelfb_fillrect,
204         .fb_copyarea  =         intelfb_copyarea,
205         .fb_imageblit =         intelfb_imageblit,
206         .fb_cursor =            intelfb_cursor,
207         .fb_sync =              intelfb_sync,
208         .fb_ioctl =             intelfb_ioctl
209 };
210
211 /* PCI driver module table */
212 static struct pci_driver intelfb_driver = {
213         .name =         "intelfb",
214         .id_table =     intelfb_pci_table,
215         .probe =        intelfb_pci_register,
216         .remove =       __devexit_p(intelfb_pci_unregister)
217 };
218
219 /* Module description/parameters */
220 MODULE_AUTHOR("David Dawes <dawes@tungstengraphics.com>, "
221               "Sylvain Meyer <sylvain.meyer@worldonline.fr>");
222 MODULE_DESCRIPTION(
223         "Framebuffer driver for Intel(R) " SUPPORTED_CHIPSETS " chipsets");
224 MODULE_LICENSE("Dual BSD/GPL");
225 MODULE_DEVICE_TABLE(pci, intelfb_pci_table);
226
227 static int accel        = 1;
228 static int vram         = 4;
229 static int hwcursor     = 0;
230 static int mtrr         = 1;
231 static int fixed        = 0;
232 static int noinit       = 0;
233 static int noregister   = 0;
234 static int probeonly    = 0;
235 static int idonly       = 0;
236 static int bailearly    = 0;
237 static int voffset      = 48;
238 static char *mode       = NULL;
239
240 module_param(accel, bool, S_IRUGO);
241 MODULE_PARM_DESC(accel, "Enable hardware acceleration");
242 module_param(vram, int, S_IRUGO);
243 MODULE_PARM_DESC(vram, "System RAM to allocate to framebuffer in MiB");
244 module_param(voffset, int, S_IRUGO);
245 MODULE_PARM_DESC(voffset, "Offset of framebuffer in MiB");
246 module_param(hwcursor, bool, S_IRUGO);
247 MODULE_PARM_DESC(hwcursor, "Enable HW cursor");
248 module_param(mtrr, bool, S_IRUGO);
249 MODULE_PARM_DESC(mtrr, "Enable MTRR support");
250 module_param(fixed, bool, S_IRUGO);
251 MODULE_PARM_DESC(fixed, "Disable mode switching");
252 module_param(noinit, bool, 0);
253 MODULE_PARM_DESC(noinit, "Don't initialise graphics mode when loading");
254 module_param(noregister, bool, 0);
255 MODULE_PARM_DESC(noregister, "Don't register, just probe and exit (debug)");
256 module_param(probeonly, bool, 0);
257 MODULE_PARM_DESC(probeonly, "Do a minimal probe (debug)");
258 module_param(idonly, bool, 0);
259 MODULE_PARM_DESC(idonly, "Just identify without doing anything else (debug)");
260 module_param(bailearly, bool, 0);
261 MODULE_PARM_DESC(bailearly, "Bail out early, depending on value (debug)");
262 module_param(mode, charp, S_IRUGO);
263 MODULE_PARM_DESC(mode,
264                  "Initial video mode \"<xres>x<yres>[-<depth>][@<refresh>]\"");
265
266 #ifndef MODULE
267 #define OPT_EQUAL(opt, name) (!strncmp(opt, name, strlen(name)))
268 #define OPT_INTVAL(opt, name) simple_strtoul(opt + strlen(name) + 1, NULL, 0)
269 #define OPT_STRVAL(opt, name) (opt + strlen(name))
270
271 static __inline__ char *
272 get_opt_string(const char *this_opt, const char *name)
273 {
274         const char *p;
275         int i;
276         char *ret;
277
278         p = OPT_STRVAL(this_opt, name);
279         i = 0;
280         while (p[i] && p[i] != ' ' && p[i] != ',')
281                 i++;
282         ret = kmalloc(i + 1, GFP_KERNEL);
283         if (ret) {
284                 strncpy(ret, p, i);
285                 ret[i] = '\0';
286         }
287         return ret;
288 }
289
290 static __inline__ int
291 get_opt_int(const char *this_opt, const char *name, int *ret)
292 {
293         if (!ret)
294                 return 0;
295
296         if (!OPT_EQUAL(this_opt, name))
297                 return 0;
298
299         *ret = OPT_INTVAL(this_opt, name);
300         return 1;
301 }
302
303 static __inline__ int
304 get_opt_bool(const char *this_opt, const char *name, int *ret)
305 {
306         if (!ret)
307                 return 0;
308
309         if (OPT_EQUAL(this_opt, name)) {
310                 if (this_opt[strlen(name)] == '=')
311                         *ret = simple_strtoul(this_opt + strlen(name) + 1,
312                                               NULL, 0);
313                 else
314                         *ret = 1;
315         } else {
316                 if (OPT_EQUAL(this_opt, "no") && OPT_EQUAL(this_opt + 2, name))
317                         *ret = 0;
318                 else
319                         return 0;
320         }
321         return 1;
322 }
323
324 static int __init
325 intelfb_setup(char *options)
326 {
327         char *this_opt;
328
329         DBG_MSG("intelfb_setup\n");
330
331         if (!options || !*options) {
332                 DBG_MSG("no options\n");
333                 return 0;
334         } else
335                 DBG_MSG("options: %s\n", options);
336
337         /*
338          * These are the built-in options analogous to the module parameters
339          * defined above.
340          *
341          * The syntax is:
342          *
343          *    video=intelfb:[mode][,<param>=<val>] ...
344          *
345          * e.g.,
346          *
347          *    video=intelfb:1024x768-16@75,accel=0
348          */
349
350         while ((this_opt = strsep(&options, ","))) {
351                 if (!*this_opt)
352                         continue;
353                 if (get_opt_bool(this_opt, "accel", &accel))
354                         ;
355                 else if (get_opt_int(this_opt, "vram", &vram))
356                         ;
357                 else if (get_opt_bool(this_opt, "hwcursor", &hwcursor))
358                         ;
359                 else if (get_opt_bool(this_opt, "mtrr", &mtrr))
360                         ;
361                 else if (get_opt_bool(this_opt, "fixed", &fixed))
362                         ;
363                 else if (get_opt_bool(this_opt, "init", &noinit))
364                         noinit = !noinit;
365                 else if (OPT_EQUAL(this_opt, "mode="))
366                         mode = get_opt_string(this_opt, "mode=");
367                 else
368                         mode = this_opt;
369         }
370
371         return 0;
372 }
373
374 #endif
375
376 static int __init
377 intelfb_init(void)
378 {
379 #ifndef MODULE
380         char *option = NULL;
381 #endif
382
383         DBG_MSG("intelfb_init\n");
384
385         INF_MSG("Framebuffer driver for "
386                 "Intel(R) " SUPPORTED_CHIPSETS " chipsets\n");
387         INF_MSG("Version " INTELFB_VERSION "\n");
388
389         if (idonly)
390                 return -ENODEV;
391
392 #ifndef MODULE
393         if (fb_get_options("intelfb", &option))
394                 return -ENODEV;
395         intelfb_setup(option);
396 #endif
397
398         return pci_register_driver(&intelfb_driver);
399 }
400
401 static void __exit
402 intelfb_exit(void)
403 {
404         DBG_MSG("intelfb_exit\n");
405         pci_unregister_driver(&intelfb_driver);
406 }
407
408 module_init(intelfb_init);
409 module_exit(intelfb_exit);
410
411 /***************************************************************
412  *                     mtrr support functions                  *
413  ***************************************************************/
414
415 #ifdef CONFIG_MTRR
416 static inline void __devinit set_mtrr(struct intelfb_info *dinfo)
417 {
418         dinfo->mtrr_reg = mtrr_add(dinfo->aperture.physical,
419                                    dinfo->aperture.size, MTRR_TYPE_WRCOMB, 1);
420         if (dinfo->mtrr_reg < 0) {
421                 ERR_MSG("unable to set MTRR\n");
422                 return;
423         }
424         dinfo->has_mtrr = 1;
425 }
426 static inline void unset_mtrr(struct intelfb_info *dinfo)
427 {
428         if (dinfo->has_mtrr)
429                 mtrr_del(dinfo->mtrr_reg, dinfo->aperture.physical,
430                          dinfo->aperture.size);
431 }
432 #else
433 #define set_mtrr(x) WRN_MSG("MTRR is disabled in the kernel\n")
434
435 #define unset_mtrr(x) do { } while (0)
436 #endif /* CONFIG_MTRR */
437
438 /***************************************************************
439  *                        driver init / cleanup                *
440  ***************************************************************/
441
442 static void
443 cleanup(struct intelfb_info *dinfo)
444 {
445         DBG_MSG("cleanup\n");
446
447         if (!dinfo)
448                 return;
449
450         fb_dealloc_cmap(&dinfo->info->cmap);
451         kfree(dinfo->info->pixmap.addr);
452
453         if (dinfo->registered)
454                 unregister_framebuffer(dinfo->info);
455
456         unset_mtrr(dinfo);
457
458         if (dinfo->fbmem_gart && dinfo->gtt_fb_mem) {
459                 agp_unbind_memory(dinfo->gtt_fb_mem);
460                 agp_free_memory(dinfo->gtt_fb_mem);
461         }
462         if (dinfo->gtt_cursor_mem) {
463                 agp_unbind_memory(dinfo->gtt_cursor_mem);
464                 agp_free_memory(dinfo->gtt_cursor_mem);
465         }
466         if (dinfo->gtt_ring_mem) {
467                 agp_unbind_memory(dinfo->gtt_ring_mem);
468                 agp_free_memory(dinfo->gtt_ring_mem);
469         }
470
471         if (dinfo->mmio_base)
472                 iounmap((void __iomem *)dinfo->mmio_base);
473         if (dinfo->aperture.virtual)
474                 iounmap((void __iomem *)dinfo->aperture.virtual);
475
476         if (dinfo->flag & INTELFB_MMIO_ACQUIRED)
477                 release_mem_region(dinfo->mmio_base_phys, INTEL_REG_SIZE);
478         if (dinfo->flag & INTELFB_FB_ACQUIRED)
479                 release_mem_region(dinfo->aperture.physical,
480                                    dinfo->aperture.size);
481         framebuffer_release(dinfo->info);
482 }
483
484 #define bailout(dinfo) do {                                             \
485         DBG_MSG("bailout\n");                                           \
486         cleanup(dinfo);                                                 \
487         INF_MSG("Not going to register framebuffer, exiting...\n");     \
488         return -ENODEV;                                                 \
489 } while (0)
490
491
492 static int __devinit
493 intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
494 {
495         struct fb_info *info;
496         struct intelfb_info *dinfo;
497         int i, err, dvo;
498         int aperture_size, stolen_size;
499         struct agp_kern_info gtt_info;
500         int agp_memtype;
501         const char *s;
502         struct agp_bridge_data *bridge;
503         int aperture_bar = 0;
504         int mmio_bar = 1;
505         int offset;
506
507         DBG_MSG("intelfb_pci_register\n");
508
509         num_registered++;
510         if (num_registered != 1) {
511                 ERR_MSG("Attempted to register %d devices "
512                         "(should be only 1).\n", num_registered);
513                 return -ENODEV;
514         }
515
516         info = framebuffer_alloc(sizeof(struct intelfb_info), &pdev->dev);
517         if (!info) {
518                 ERR_MSG("Could not allocate memory for intelfb_info.\n");
519                 return -ENODEV;
520         }
521         if (fb_alloc_cmap(&info->cmap, 256, 1) < 0) {
522                 ERR_MSG("Could not allocate cmap for intelfb_info.\n");
523                 goto err_out_cmap;
524                 return -ENODEV;
525         }
526
527         dinfo = info->par;
528         dinfo->info  = info;
529         dinfo->fbops = &intel_fb_ops;
530         dinfo->pdev  = pdev;
531
532         /* Reserve pixmap space. */
533         info->pixmap.addr = kmalloc(64 * 1024, GFP_KERNEL);
534         if (info->pixmap.addr == NULL) {
535                 ERR_MSG("Cannot reserve pixmap memory.\n");
536                 goto err_out_pixmap;
537         }
538         memset(info->pixmap.addr, 0, 64 * 1024);
539
540         /* set early this option because it could be changed by tv encoder
541            driver */
542         dinfo->fixed_mode = fixed;
543
544         /* Enable device. */
545         if ((err = pci_enable_device(pdev))) {
546                 ERR_MSG("Cannot enable device.\n");
547                 cleanup(dinfo);
548                 return -ENODEV;
549         }
550
551         /* Set base addresses. */
552         if ((ent->device == PCI_DEVICE_ID_INTEL_915G) ||
553             (ent->device == PCI_DEVICE_ID_INTEL_915GM) ||
554             (ent->device == PCI_DEVICE_ID_INTEL_945G)  ||
555             (ent->device == PCI_DEVICE_ID_INTEL_945GM)) {
556                 aperture_bar = 2;
557                 mmio_bar = 0;
558         }
559         dinfo->aperture.physical = pci_resource_start(pdev, aperture_bar);
560         dinfo->aperture.size     = pci_resource_len(pdev, aperture_bar);
561         dinfo->mmio_base_phys    = pci_resource_start(pdev, mmio_bar);
562         DBG_MSG("fb aperture: 0x%llx/0x%llx, MMIO region: 0x%llx/0x%llx\n",
563                 (unsigned long long)pci_resource_start(pdev, aperture_bar),
564                 (unsigned long long)pci_resource_len(pdev, aperture_bar),
565                 (unsigned long long)pci_resource_start(pdev, mmio_bar),
566                 (unsigned long long)pci_resource_len(pdev, mmio_bar));
567
568         /* Reserve the fb and MMIO regions */
569         if (!request_mem_region(dinfo->aperture.physical, dinfo->aperture.size,
570                                 INTELFB_MODULE_NAME)) {
571                 ERR_MSG("Cannot reserve FB region.\n");
572                 cleanup(dinfo);
573                 return -ENODEV;
574         }
575
576         dinfo->flag |= INTELFB_FB_ACQUIRED;
577
578         if (!request_mem_region(dinfo->mmio_base_phys,
579                                 INTEL_REG_SIZE,
580                                 INTELFB_MODULE_NAME)) {
581                 ERR_MSG("Cannot reserve MMIO region.\n");
582                 cleanup(dinfo);
583                 return -ENODEV;
584         }
585
586         dinfo->flag |= INTELFB_MMIO_ACQUIRED;
587
588         /* Get the chipset info. */
589         dinfo->pci_chipset = pdev->device;
590
591         if (intelfbhw_get_chipset(pdev, dinfo)) {
592                 cleanup(dinfo);
593                 return -ENODEV;
594         }
595
596         if (intelfbhw_get_memory(pdev, &aperture_size,&stolen_size)) {
597                 cleanup(dinfo);
598                 return -ENODEV;
599         }
600
601         INF_MSG("%02x:%02x.%d: %s, aperture size %dMB, "
602                 "stolen memory %dkB\n",
603                 pdev->bus->number, PCI_SLOT(pdev->devfn),
604                 PCI_FUNC(pdev->devfn), dinfo->name,
605                 BtoMB(aperture_size), BtoKB(stolen_size));
606
607         /* Set these from the options. */
608         dinfo->accel    = accel;
609         dinfo->hwcursor = hwcursor;
610
611         if (NOACCEL_CHIPSET(dinfo) && dinfo->accel == 1) {
612                 INF_MSG("Acceleration is not supported for the %s chipset.\n",
613                         dinfo->name);
614                 dinfo->accel = 0;
615         }
616
617         /* Framebuffer parameters - Use all the stolen memory if >= vram */
618         if (ROUND_UP_TO_PAGE(stolen_size) >= MB(vram)) {
619                 dinfo->fb.size = ROUND_UP_TO_PAGE(stolen_size);
620                 dinfo->fbmem_gart = 0;
621         } else {
622                 dinfo->fb.size =  MB(vram);
623                 dinfo->fbmem_gart = 1;
624         }
625
626         /* Allocate space for the ring buffer and HW cursor if enabled. */
627         if (dinfo->accel) {
628                 dinfo->ring.size = RINGBUFFER_SIZE;
629                 dinfo->ring_tail_mask = dinfo->ring.size - 1;
630         }
631         if (dinfo->hwcursor) {
632                 dinfo->cursor.size = HW_CURSOR_SIZE;
633         }
634
635         /* Use agpgart to manage the GATT */
636         if (!(bridge = agp_backend_acquire(pdev))) {
637                 ERR_MSG("cannot acquire agp\n");
638                 cleanup(dinfo);
639                 return -ENODEV;
640         }
641
642         /* get the current gatt info */
643         if (agp_copy_info(bridge, &gtt_info)) {
644                 ERR_MSG("cannot get agp info\n");
645                 agp_backend_release(bridge);
646                 cleanup(dinfo);
647                 return -ENODEV;
648         }
649
650         if (MB(voffset) < stolen_size)
651                 offset = (stolen_size >> 12);
652         else
653                 offset = ROUND_UP_TO_PAGE(MB(voffset))/GTT_PAGE_SIZE;
654
655         /* set the mem offsets - set them after the already used pages */
656         if (dinfo->accel) {
657                 dinfo->ring.offset = offset + gtt_info.current_memory;
658         }
659         if (dinfo->hwcursor) {
660                 dinfo->cursor.offset = offset +
661                         + gtt_info.current_memory + (dinfo->ring.size >> 12);
662         }
663         if (dinfo->fbmem_gart) {
664                 dinfo->fb.offset = offset +
665                         + gtt_info.current_memory + (dinfo->ring.size >> 12)
666                         + (dinfo->cursor.size >> 12);
667         }
668
669         /* Allocate memories (which aren't stolen) */
670         /* Map the fb and MMIO regions */
671         /* ioremap only up to the end of used aperture */
672         dinfo->aperture.virtual = (u8 __iomem *)ioremap_nocache
673                 (dinfo->aperture.physical, ((offset + dinfo->fb.offset) << 12)
674                  + dinfo->fb.size);
675         if (!dinfo->aperture.virtual) {
676                 ERR_MSG("Cannot remap FB region.\n");
677                 cleanup(dinfo);
678                 return -ENODEV;
679         }
680
681         dinfo->mmio_base =
682                 (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
683                                                INTEL_REG_SIZE);
684         if (!dinfo->mmio_base) {
685                 ERR_MSG("Cannot remap MMIO region.\n");
686                 cleanup(dinfo);
687                 return -ENODEV;
688         }
689
690         if (dinfo->accel) {
691                 if (!(dinfo->gtt_ring_mem =
692                       agp_allocate_memory(bridge, dinfo->ring.size >> 12,
693                                           AGP_NORMAL_MEMORY))) {
694                         ERR_MSG("cannot allocate ring buffer memory\n");
695                         agp_backend_release(bridge);
696                         cleanup(dinfo);
697                         return -ENOMEM;
698                 }
699                 if (agp_bind_memory(dinfo->gtt_ring_mem,
700                                     dinfo->ring.offset)) {
701                         ERR_MSG("cannot bind ring buffer memory\n");
702                         agp_backend_release(bridge);
703                         cleanup(dinfo);
704                         return -EBUSY;
705                 }
706                 dinfo->ring.physical = dinfo->aperture.physical
707                         + (dinfo->ring.offset << 12);
708                 dinfo->ring.virtual  = dinfo->aperture.virtual
709                         + (dinfo->ring.offset << 12);
710                 dinfo->ring_head = dinfo->ring.virtual;
711         }
712         if (dinfo->hwcursor) {
713                 agp_memtype = dinfo->mobile ? AGP_PHYSICAL_MEMORY
714                         : AGP_NORMAL_MEMORY;
715                 if (!(dinfo->gtt_cursor_mem =
716                       agp_allocate_memory(bridge, dinfo->cursor.size >> 12,
717                                           agp_memtype))) {
718                         ERR_MSG("cannot allocate cursor memory\n");
719                         agp_backend_release(bridge);
720                         cleanup(dinfo);
721                         return -ENOMEM;
722                 }
723                 if (agp_bind_memory(dinfo->gtt_cursor_mem,
724                                     dinfo->cursor.offset)) {
725                         ERR_MSG("cannot bind cursor memory\n");
726                         agp_backend_release(bridge);
727                         cleanup(dinfo);
728                         return -EBUSY;
729                 }
730                 if (dinfo->mobile)
731                         dinfo->cursor.physical
732                                 = dinfo->gtt_cursor_mem->physical;
733                 else
734                         dinfo->cursor.physical = dinfo->aperture.physical
735                                 + (dinfo->cursor.offset << 12);
736                 dinfo->cursor.virtual = dinfo->aperture.virtual
737                         + (dinfo->cursor.offset << 12);
738         }
739         if (dinfo->fbmem_gart) {
740                 if (!(dinfo->gtt_fb_mem =
741                       agp_allocate_memory(bridge, dinfo->fb.size >> 12,
742                                           AGP_NORMAL_MEMORY))) {
743                         WRN_MSG("cannot allocate framebuffer memory - use "
744                                 "the stolen one\n");
745                         dinfo->fbmem_gart = 0;
746                 }
747                 if (agp_bind_memory(dinfo->gtt_fb_mem,
748                                     dinfo->fb.offset)) {
749                         WRN_MSG("cannot bind framebuffer memory - use "
750                                 "the stolen one\n");
751                         dinfo->fbmem_gart = 0;
752                 }
753         }
754
755         /* update framebuffer memory parameters */
756         if (!dinfo->fbmem_gart)
757                 dinfo->fb.offset = 0;   /* starts at offset 0 */
758         dinfo->fb.physical = dinfo->aperture.physical
759                 + (dinfo->fb.offset << 12);
760         dinfo->fb.virtual = dinfo->aperture.virtual + (dinfo->fb.offset << 12);
761         dinfo->fb_start = dinfo->fb.offset << 12;
762
763         /* release agpgart */
764         agp_backend_release(bridge);
765
766         if (mtrr)
767                 set_mtrr(dinfo);
768
769         DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%x)\n",
770                 dinfo->fb.physical, dinfo->fb.offset, dinfo->fb.size,
771                 (u32 __iomem ) dinfo->fb.virtual);
772         DBG_MSG("MMIO: 0x%x/0x%x (0x%x)\n",
773                 dinfo->mmio_base_phys, INTEL_REG_SIZE,
774                 (u32 __iomem) dinfo->mmio_base);
775         DBG_MSG("ring buffer: 0x%x/0x%x (0x%x)\n",
776                 dinfo->ring.physical, dinfo->ring.size,
777                 (u32 __iomem ) dinfo->ring.virtual);
778         DBG_MSG("HW cursor: 0x%x/0x%x (0x%x) (offset 0x%x) (phys 0x%x)\n",
779                 dinfo->cursor.physical, dinfo->cursor.size,
780                 (u32 __iomem ) dinfo->cursor.virtual, dinfo->cursor.offset,
781                 dinfo->cursor.physical);
782
783         DBG_MSG("options: vram = %d, accel = %d, hwcursor = %d, fixed = %d, "
784                 "noinit = %d\n", vram, accel, hwcursor, fixed, noinit);
785         DBG_MSG("options: mode = \"%s\"\n", mode ? mode : "");
786
787         if (probeonly)
788                 bailout(dinfo);
789
790         /*
791          * Check if the LVDS port or any DVO ports are enabled.  If so,
792          * don't allow mode switching
793          */
794         dvo = intelfbhw_check_non_crt(dinfo);
795         if (dvo) {
796                 dinfo->fixed_mode = 1;
797                 WRN_MSG("Non-CRT device is enabled ( ");
798                 i = 0;
799                 while (dvo) {
800                         if (dvo & 1) {
801                                 s = intelfbhw_dvo_to_string(1 << i);
802                                 if (s)
803                                         printk("%s ", s);
804                         }
805                         dvo >>= 1;
806                         ++i;
807                 }
808                 printk(").  Disabling mode switching.\n");
809         }
810
811         if (bailearly == 1)
812                 bailout(dinfo);
813
814         if (FIXED_MODE(dinfo) && ORIG_VIDEO_ISVGA != VIDEO_TYPE_VLFB) {
815                 ERR_MSG("Video mode must be programmed at boot time.\n");
816                 cleanup(dinfo);
817                 return -ENODEV;
818         }
819
820         if (bailearly == 2)
821                 bailout(dinfo);
822
823         /* Initialise dinfo and related data. */
824         /* If an initial mode was programmed at boot time, get its details. */
825         if (ORIG_VIDEO_ISVGA == VIDEO_TYPE_VLFB)
826                 get_initial_mode(dinfo);
827
828         if (bailearly == 3)
829                 bailout(dinfo);
830
831         if (FIXED_MODE(dinfo)) {
832                 /* remap fb address */
833                 update_dinfo(dinfo, &dinfo->initial_var);
834         }
835
836         if (bailearly == 4)
837                 bailout(dinfo);
838
839
840         if (intelfb_set_fbinfo(dinfo)) {
841                 cleanup(dinfo);
842                 return -ENODEV;
843         }
844
845         if (bailearly == 5)
846                 bailout(dinfo);
847
848         if (bailearly == 6)
849                 bailout(dinfo);
850
851         pci_set_drvdata(pdev, dinfo);
852
853         /* Save the initial register state. */
854         i = intelfbhw_read_hw_state(dinfo, &dinfo->save_state,
855                                     bailearly > 6 ? bailearly - 6 : 0);
856         if (i != 0) {
857                 DBG_MSG("intelfbhw_read_hw_state returned %d\n", i);
858                 bailout(dinfo);
859         }
860
861         intelfbhw_print_hw_state(dinfo, &dinfo->save_state);
862
863         if (bailearly == 18)
864                 bailout(dinfo);
865
866         /* Cursor initialisation */
867         if (dinfo->hwcursor) {
868                 intelfbhw_cursor_init(dinfo);
869                 intelfbhw_cursor_reset(dinfo);
870         }
871
872         if (bailearly == 19)
873                 bailout(dinfo);
874
875         /* 2d acceleration init */
876         if (dinfo->accel)
877                 intelfbhw_2d_start(dinfo);
878
879         if (bailearly == 20)
880                 bailout(dinfo);
881
882         if (noregister)
883                 bailout(dinfo);
884
885         if (register_framebuffer(dinfo->info) < 0) {
886                 ERR_MSG("Cannot register framebuffer.\n");
887                 cleanup(dinfo);
888                 return -ENODEV;
889         }
890
891         dinfo->registered = 1;
892
893         return 0;
894
895 err_out_pixmap:
896         fb_dealloc_cmap(&info->cmap);
897 err_out_cmap:
898         framebuffer_release(info);
899         return -ENODEV;
900 }
901
902 static void __devexit
903 intelfb_pci_unregister(struct pci_dev *pdev)
904 {
905         struct intelfb_info *dinfo = pci_get_drvdata(pdev);
906
907         DBG_MSG("intelfb_pci_unregister\n");
908
909         if (!dinfo)
910                 return;
911
912         cleanup(dinfo);
913
914         pci_set_drvdata(pdev, NULL);
915 }
916
917 /***************************************************************
918  *                       helper functions                      *
919  ***************************************************************/
920
921 int __inline__
922 intelfb_var_to_depth(const struct fb_var_screeninfo *var)
923 {
924         DBG_MSG("intelfb_var_to_depth: bpp: %d, green.length is %d\n",
925                 var->bits_per_pixel, var->green.length);
926
927         switch (var->bits_per_pixel) {
928         case 16:
929                 return (var->green.length == 6) ? 16 : 15;
930         case 32:
931                 return 24;
932         default:
933                 return var->bits_per_pixel;
934         }
935 }
936
937
938 static __inline__ int
939 var_to_refresh(const struct fb_var_screeninfo *var)
940 {
941         int xtot = var->xres + var->left_margin + var->right_margin +
942                    var->hsync_len;
943         int ytot = var->yres + var->upper_margin + var->lower_margin +
944                    var->vsync_len;
945
946         return (1000000000 / var->pixclock * 1000 + 500) / xtot / ytot;
947 }
948
949 /***************************************************************
950  *                Various intialisation functions              *
951  ***************************************************************/
952
953 static void __devinit
954 get_initial_mode(struct intelfb_info *dinfo)
955 {
956         struct fb_var_screeninfo *var;
957         int xtot, ytot;
958
959         DBG_MSG("get_initial_mode\n");
960
961         dinfo->initial_vga = 1;
962         dinfo->initial_fb_base = screen_info.lfb_base;
963         dinfo->initial_video_ram = screen_info.lfb_size * KB(64);
964         dinfo->initial_pitch = screen_info.lfb_linelength;
965
966         var = &dinfo->initial_var;
967         memset(var, 0, sizeof(*var));
968         var->xres = screen_info.lfb_width;
969         var->yres = screen_info.lfb_height;
970         var->bits_per_pixel = screen_info.lfb_depth;
971         switch (screen_info.lfb_depth) {
972         case 15:
973                 var->bits_per_pixel = 16;
974                 break;
975         case 24:
976                 var->bits_per_pixel = 32;
977                 break;
978         }
979
980         DBG_MSG("Initial info: FB is 0x%x/0x%x (%d kByte)\n",
981                 dinfo->initial_fb_base, dinfo->initial_video_ram,
982                 BtoKB(dinfo->initial_video_ram));
983
984         DBG_MSG("Initial info: mode is %dx%d-%d (%d)\n",
985                 var->xres, var->yres, var->bits_per_pixel,
986                 dinfo->initial_pitch);
987
988         /* Dummy timing values (assume 60Hz) */
989         var->left_margin = (var->xres / 8) & 0xf8;
990         var->right_margin = 32;
991         var->upper_margin = 16;
992         var->lower_margin = 4;
993         var->hsync_len = (var->xres / 8) & 0xf8;
994         var->vsync_len = 4;
995
996         xtot = var->xres + var->left_margin +
997                 var->right_margin + var->hsync_len;
998         ytot = var->yres + var->upper_margin +
999                 var->lower_margin + var->vsync_len;
1000         var->pixclock = 10000000 / xtot * 1000 / ytot * 100 / 60;
1001
1002         var->height = -1;
1003         var->width = -1;
1004
1005         if (var->bits_per_pixel > 8) {
1006                 var->red.offset = screen_info.red_pos;
1007                 var->red.length = screen_info.red_size;
1008                 var->green.offset = screen_info.green_pos;
1009                 var->green.length = screen_info.green_size;
1010                 var->blue.offset = screen_info.blue_pos;
1011                 var->blue.length = screen_info.blue_size;
1012                 var->transp.offset = screen_info.rsvd_pos;
1013                 var->transp.length = screen_info.rsvd_size;
1014         } else {
1015                 var->red.length = 8;
1016                 var->green.length = 8;
1017                 var->blue.length = 8;
1018         }
1019 }
1020
1021 static int __devinit
1022 intelfb_init_var(struct intelfb_info *dinfo)
1023 {
1024         struct fb_var_screeninfo *var;
1025         int msrc = 0;
1026
1027         DBG_MSG("intelfb_init_var\n");
1028
1029         var = &dinfo->info->var;
1030         if (FIXED_MODE(dinfo)) {
1031                 memcpy(var, &dinfo->initial_var,
1032                        sizeof(struct fb_var_screeninfo));
1033                 msrc = 5;
1034         } else {
1035                 const u8 *edid_s = fb_firmware_edid(&dinfo->pdev->dev);
1036                 u8 *edid_d = NULL;
1037
1038                 if (edid_s) {
1039                         edid_d = kmalloc(128, GFP_KERNEL);
1040
1041                         if (edid_d) {
1042                                 memcpy(edid_d, edid_s, 128);
1043                                 fb_edid_to_monspecs(edid_d,
1044                                                     &dinfo->info->monspecs);
1045                                 kfree(edid_d);
1046                         }
1047                 }
1048
1049                 if (mode) {
1050                         printk("intelfb: Looking for mode in private "
1051                                "database\n");
1052                         msrc = fb_find_mode(var, dinfo->info, mode,
1053                                             dinfo->info->monspecs.modedb,
1054                                             dinfo->info->monspecs.modedb_len,
1055                                             NULL, 0);
1056
1057                         if (msrc && msrc > 1) {
1058                                 printk("intelfb: No mode in private database, "
1059                                        "intelfb: looking for mode in global "
1060                                        "database ");
1061                                 msrc = fb_find_mode(var, dinfo->info, mode,
1062                                                     vesa_modes,
1063                                                     VESA_MODEDB_SIZE, NULL, 0);
1064
1065                                 if (msrc)
1066                                         msrc |= 8;
1067                         }
1068
1069                 }
1070
1071                 if (!msrc) {
1072                         msrc = fb_find_mode(var, dinfo->info, PREFERRED_MODE,
1073                                             vesa_modes, VESA_MODEDB_SIZE,
1074                                             NULL, 0);
1075                 }
1076         }
1077
1078         if (!msrc) {
1079                 ERR_MSG("Cannot find a suitable video mode.\n");
1080                 return 1;
1081         }
1082
1083         INF_MSG("Initial video mode is %dx%d-%d@%d.\n", var->xres, var->yres,
1084                 var->bits_per_pixel, var_to_refresh(var));
1085
1086         DBG_MSG("Initial video mode is from %d.\n", msrc);
1087
1088 #if ALLOCATE_FOR_PANNING
1089         /* Allow use of half of the video ram for panning */
1090         var->xres_virtual = var->xres;
1091         var->yres_virtual =
1092                 dinfo->fb.size / 2 / (var->bits_per_pixel * var->xres);
1093         if (var->yres_virtual < var->yres)
1094                 var->yres_virtual = var->yres;
1095 #else
1096         var->yres_virtual = var->yres;
1097 #endif
1098
1099         if (dinfo->accel)
1100                 var->accel_flags |= FB_ACCELF_TEXT;
1101         else
1102                 var->accel_flags &= ~FB_ACCELF_TEXT;
1103
1104         return 0;
1105 }
1106
1107 static int __devinit
1108 intelfb_set_fbinfo(struct intelfb_info *dinfo)
1109 {
1110         struct fb_info *info = dinfo->info;
1111
1112         DBG_MSG("intelfb_set_fbinfo\n");
1113
1114         info->flags = FBINFO_FLAG_DEFAULT;
1115         info->fbops = &intel_fb_ops;
1116         info->pseudo_palette = dinfo->pseudo_palette;
1117
1118         info->pixmap.size = 64*1024;
1119         info->pixmap.buf_align = 8;
1120         info->pixmap.access_align = 32;
1121         info->pixmap.flags = FB_PIXMAP_SYSTEM;
1122
1123         if (intelfb_init_var(dinfo))
1124                 return 1;
1125
1126         info->pixmap.scan_align = 1;
1127         strcpy(info->fix.id, dinfo->name);
1128         info->fix.smem_start = dinfo->fb.physical;
1129         info->fix.smem_len = dinfo->fb.size;
1130         info->fix.type = FB_TYPE_PACKED_PIXELS;
1131         info->fix.type_aux = 0;
1132         info->fix.xpanstep = 8;
1133         info->fix.ypanstep = 1;
1134         info->fix.ywrapstep = 0;
1135         info->fix.mmio_start = dinfo->mmio_base_phys;
1136         info->fix.mmio_len = INTEL_REG_SIZE;
1137         info->fix.accel = FB_ACCEL_I830;
1138         update_dinfo(dinfo, &info->var);
1139
1140         return 0;
1141 }
1142
1143 /* Update dinfo to match the active video mode. */
1144 static void
1145 update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
1146 {
1147         DBG_MSG("update_dinfo\n");
1148
1149         dinfo->bpp = var->bits_per_pixel;
1150         dinfo->depth = intelfb_var_to_depth(var);
1151         dinfo->xres = var->xres;
1152         dinfo->yres = var->xres;
1153         dinfo->pixclock = var->pixclock;
1154
1155         dinfo->info->fix.visual = dinfo->visual;
1156         dinfo->info->fix.line_length = dinfo->pitch;
1157
1158         switch (dinfo->bpp) {
1159         case 8:
1160                 dinfo->visual = FB_VISUAL_PSEUDOCOLOR;
1161                 dinfo->pitch = var->xres_virtual;
1162                 break;
1163         case 16:
1164                 dinfo->visual = FB_VISUAL_TRUECOLOR;
1165                 dinfo->pitch = var->xres_virtual * 2;
1166                 break;
1167         case 32:
1168                 dinfo->visual = FB_VISUAL_TRUECOLOR;
1169                 dinfo->pitch = var->xres_virtual * 4;
1170                 break;
1171         }
1172
1173         /* Make sure the line length is a aligned correctly. */
1174         if (IS_I9XX(dinfo))
1175                 dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT_I9XX);
1176         else
1177                 dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
1178
1179         if (FIXED_MODE(dinfo))
1180                 dinfo->pitch = dinfo->initial_pitch;
1181
1182         dinfo->info->screen_base = (char __iomem *)dinfo->fb.virtual;
1183         dinfo->info->fix.line_length = dinfo->pitch;
1184         dinfo->info->fix.visual = dinfo->visual;
1185 }
1186
1187 /* fbops functions */
1188
1189 /***************************************************************
1190  *                       fbdev interface                       *
1191  ***************************************************************/
1192
1193 static int
1194 intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1195 {
1196         int change_var = 0;
1197         struct fb_var_screeninfo v;
1198         struct intelfb_info *dinfo;
1199         static int first = 1;
1200         int i;
1201         /* Good pitches to allow tiling.  Don't care about pitches < 1024. */
1202         static const int pitches[] = {
1203                 128 * 8,
1204                 128 * 16,
1205                 128 * 32,
1206                 128 * 64,
1207                 0
1208         };
1209
1210         DBG_MSG("intelfb_check_var: accel_flags is %d\n", var->accel_flags);
1211
1212         dinfo = GET_DINFO(info);
1213
1214         /* update the pitch */
1215         if (intelfbhw_validate_mode(dinfo, var) != 0)
1216                 return -EINVAL;
1217
1218         v = *var;
1219
1220         for (i = 0; pitches[i] != 0; i++) {
1221                 if (pitches[i] >= v.xres_virtual) {
1222                         v.xres_virtual = pitches[i];
1223                         break;
1224                 }
1225         }
1226
1227         /* Check for a supported bpp. */
1228         if (v.bits_per_pixel <= 8) {
1229                 v.bits_per_pixel = 8;
1230         } else if (v.bits_per_pixel <= 16) {
1231                 if (v.bits_per_pixel == 16)
1232                         v.green.length = 6;
1233                 v.bits_per_pixel = 16;
1234         } else if (v.bits_per_pixel <= 32) {
1235                 v.bits_per_pixel = 32;
1236         } else
1237                 return -EINVAL;
1238
1239         change_var = ((info->var.xres != var->xres) ||
1240                       (info->var.yres != var->yres) ||
1241                       (info->var.xres_virtual != var->xres_virtual) ||
1242                       (info->var.yres_virtual != var->yres_virtual) ||
1243                       (info->var.bits_per_pixel != var->bits_per_pixel) ||
1244                       memcmp(&info->var.red, &var->red, sizeof(var->red)) ||
1245                       memcmp(&info->var.green, &var->green,
1246                              sizeof(var->green)) ||
1247                       memcmp(&info->var.blue, &var->blue, sizeof(var->blue)));
1248
1249         if (FIXED_MODE(dinfo) &&
1250             (change_var ||
1251              var->yres_virtual > dinfo->initial_var.yres_virtual ||
1252              var->yres_virtual < dinfo->initial_var.yres ||
1253              var->xoffset || var->nonstd)) {
1254                 if (first) {
1255                         ERR_MSG("Changing the video mode is not supported.\n");
1256                         first = 0;
1257                 }
1258                 return -EINVAL;
1259         }
1260
1261         switch (intelfb_var_to_depth(&v)) {
1262         case 8:
1263                 v.red.offset = v.green.offset = v.blue.offset = 0;
1264                 v.red.length = v.green.length = v.blue.length = 8;
1265                 v.transp.offset = v.transp.length = 0;
1266                 break;
1267         case 15:
1268                 v.red.offset = 10;
1269                 v.green.offset = 5;
1270                 v.blue.offset = 0;
1271                 v.red.length = v.green.length = v.blue.length = 5;
1272                 v.transp.offset = v.transp.length = 0;
1273                 break;
1274         case 16:
1275                 v.red.offset = 11;
1276                 v.green.offset = 5;
1277                 v.blue.offset = 0;
1278                 v.red.length = 5;
1279                 v.green.length = 6;
1280                 v.blue.length = 5;
1281                 v.transp.offset = v.transp.length = 0;
1282                 break;
1283         case 24:
1284                 v.red.offset = 16;
1285                 v.green.offset = 8;
1286                 v.blue.offset = 0;
1287                 v.red.length = v.green.length = v.blue.length = 8;
1288                 v.transp.offset = v.transp.length = 0;
1289                 break;
1290         case 32:
1291                 v.red.offset = 16;
1292                 v.green.offset = 8;
1293                 v.blue.offset = 0;
1294                 v.red.length = v.green.length = v.blue.length = 8;
1295                 v.transp.offset = 24;
1296                 v.transp.length = 8;
1297                 break;
1298         }
1299
1300         if (v.xoffset < 0)
1301                 v.xoffset = 0;
1302         if (v.yoffset < 0)
1303                 v.yoffset = 0;
1304
1305         if (v.xoffset > v.xres_virtual - v.xres)
1306                 v.xoffset = v.xres_virtual - v.xres;
1307         if (v.yoffset > v.yres_virtual - v.yres)
1308                 v.yoffset = v.yres_virtual - v.yres;
1309
1310         v.red.msb_right = v.green.msb_right = v.blue.msb_right =
1311                           v.transp.msb_right = 0;
1312
1313         *var = v;
1314
1315         return 0;
1316 }
1317
1318 static int
1319 intelfb_set_par(struct fb_info *info)
1320 {
1321         struct intelfb_hwstate *hw;
1322         struct intelfb_info *dinfo = GET_DINFO(info);
1323
1324         if (FIXED_MODE(dinfo)) {
1325                 ERR_MSG("Changing the video mode is not supported.\n");
1326                 return -EINVAL;
1327         }
1328
1329         hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
1330         if (!hw)
1331                 return -ENOMEM;
1332
1333         DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
1334                 info->var.yres, info->var.bits_per_pixel);
1335
1336         intelfb_blank(FB_BLANK_POWERDOWN, info);
1337
1338         if (ACCEL(dinfo, info))
1339                 intelfbhw_2d_stop(dinfo);
1340
1341         memcpy(hw, &dinfo->save_state, sizeof(*hw));
1342         if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
1343                 goto invalid_mode;
1344         if (intelfbhw_program_mode(dinfo, hw, 0))
1345                 goto invalid_mode;
1346
1347 #if REGDUMP > 0
1348         intelfbhw_read_hw_state(dinfo, hw, 0);
1349         intelfbhw_print_hw_state(dinfo, hw);
1350 #endif
1351
1352         update_dinfo(dinfo, &info->var);
1353
1354         if (ACCEL(dinfo, info))
1355                 intelfbhw_2d_start(dinfo);
1356
1357         intelfb_pan_display(&info->var, info);
1358
1359         intelfb_blank(FB_BLANK_UNBLANK, info);
1360
1361         if (ACCEL(dinfo, info)) {
1362                 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
1363                 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
1364                 FBINFO_HWACCEL_IMAGEBLIT;
1365         } else {
1366                 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1367         }
1368         kfree(hw);
1369         return 0;
1370 invalid_mode:
1371         kfree(hw);
1372         return -EINVAL;
1373 }
1374
1375 static int
1376 intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1377                   unsigned blue, unsigned transp, struct fb_info *info)
1378 {
1379         struct intelfb_info *dinfo = GET_DINFO(info);
1380
1381 #if VERBOSE > 0
1382         DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
1383 #endif
1384
1385         if (regno > 255)
1386                 return 1;
1387
1388         if (dinfo->depth == 8) {
1389                 red >>= 8;
1390                 green >>= 8;
1391                 blue >>= 8;
1392
1393                 intelfbhw_setcolreg(dinfo, regno, red, green, blue,
1394                                     transp);
1395         }
1396
1397         if (regno < 16) {
1398                 switch (dinfo->depth) {
1399                 case 15:
1400                         dinfo->pseudo_palette[regno] = ((red & 0xf800) >>  1) |
1401                                 ((green & 0xf800) >>  6) |
1402                                 ((blue & 0xf800) >> 11);
1403                         break;
1404                 case 16:
1405                         dinfo->pseudo_palette[regno] = (red & 0xf800) |
1406                                 ((green & 0xfc00) >>  5) |
1407                                 ((blue  & 0xf800) >> 11);
1408                         break;
1409                 case 24:
1410                         dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
1411                                 (green & 0xff00) |
1412                                 ((blue  & 0xff00) >> 8);
1413                         break;
1414                 }
1415         }
1416
1417         return 0;
1418 }
1419
1420 static int
1421 intelfb_blank(int blank, struct fb_info *info)
1422 {
1423         intelfbhw_do_blank(blank, info);
1424         return 0;
1425 }
1426
1427 static int
1428 intelfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1429 {
1430         intelfbhw_pan_display(var, info);
1431         return 0;
1432 }
1433
1434 /* When/if we have our own ioctls. */
1435 static int
1436 intelfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
1437 {
1438         int retval = 0;
1439
1440         return retval;
1441 }
1442
1443 static void
1444 intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
1445 {
1446         struct intelfb_info *dinfo = GET_DINFO(info);
1447         u32 rop, color;
1448
1449 #if VERBOSE > 0
1450         DBG_MSG("intelfb_fillrect\n");
1451 #endif
1452
1453         if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1454                 return cfb_fillrect(info, rect);
1455
1456         if (rect->rop == ROP_COPY)
1457                 rop = PAT_ROP_GXCOPY;
1458         else // ROP_XOR
1459                 rop = PAT_ROP_GXXOR;
1460
1461         if (dinfo->depth != 8)
1462                 color = dinfo->pseudo_palette[rect->color];
1463         else
1464                 color = rect->color;
1465
1466         intelfbhw_do_fillrect(dinfo, rect->dx, rect->dy,
1467                               rect->width, rect->height, color,
1468                               dinfo->pitch, info->var.bits_per_pixel,
1469                               rop);
1470 }
1471
1472 static void
1473 intelfb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1474 {
1475         struct intelfb_info *dinfo = GET_DINFO(info);
1476
1477 #if VERBOSE > 0
1478         DBG_MSG("intelfb_copyarea\n");
1479 #endif
1480
1481         if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1482                 return cfb_copyarea(info, region);
1483
1484         intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
1485                             region->dy, region->width, region->height,
1486                             dinfo->pitch, info->var.bits_per_pixel);
1487 }
1488
1489 static void
1490 intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
1491 {
1492         struct intelfb_info *dinfo = GET_DINFO(info);
1493         u32 fgcolor, bgcolor;
1494
1495 #if VERBOSE > 0
1496         DBG_MSG("intelfb_imageblit\n");
1497 #endif
1498
1499         if (!ACCEL(dinfo, info) || dinfo->depth == 4
1500             || image->depth != 1)
1501                 return cfb_imageblit(info, image);
1502
1503         if (dinfo->depth != 8) {
1504                 fgcolor = dinfo->pseudo_palette[image->fg_color];
1505                 bgcolor = dinfo->pseudo_palette[image->bg_color];
1506         } else {
1507                 fgcolor = image->fg_color;
1508                 bgcolor = image->bg_color;
1509         }
1510
1511         if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
1512                                     image->height, image->data,
1513                                     image->dx, image->dy,
1514                                     dinfo->pitch, info->var.bits_per_pixel))
1515                 return cfb_imageblit(info, image);
1516 }
1517
1518 static int
1519 intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1520 {
1521         struct intelfb_info *dinfo = GET_DINFO(info);
1522         u32 physical;
1523 #if VERBOSE > 0
1524         DBG_MSG("intelfb_cursor\n");
1525 #endif
1526
1527         if (!dinfo->hwcursor)
1528                 return -ENODEV;
1529
1530         intelfbhw_cursor_hide(dinfo);
1531
1532         /* If XFree killed the cursor - restore it */
1533         physical = (dinfo->mobile || IS_I9XX(dinfo)) ? dinfo->cursor.physical :
1534                    (dinfo->cursor.offset << 12);
1535
1536         if (INREG(CURSOR_A_BASEADDR) != physical) {
1537                 u32 fg, bg;
1538
1539                 DBG_MSG("the cursor was killed - restore it !!\n");
1540                 DBG_MSG("size %d, %d   pos %d, %d\n",
1541                         cursor->image.width, cursor->image.height,
1542                         cursor->image.dx, cursor->image.dy);
1543
1544                 intelfbhw_cursor_init(dinfo);
1545                 intelfbhw_cursor_reset(dinfo);
1546                 intelfbhw_cursor_setpos(dinfo, cursor->image.dx,
1547                                         cursor->image.dy);
1548
1549                 if (dinfo->depth != 8) {
1550                         fg =dinfo->pseudo_palette[cursor->image.fg_color];
1551                         bg =dinfo->pseudo_palette[cursor->image.bg_color];
1552                 } else {
1553                         fg = cursor->image.fg_color;
1554                         bg = cursor->image.bg_color;
1555                 }
1556                 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1557                 intelfbhw_cursor_load(dinfo, cursor->image.width,
1558                                       cursor->image.height,
1559                                       dinfo->cursor_src);
1560
1561                 if (cursor->enable)
1562                         intelfbhw_cursor_show(dinfo);
1563                 return 0;
1564         }
1565
1566         if (cursor->set & FB_CUR_SETPOS) {
1567                 u32 dx, dy;
1568
1569                 dx = cursor->image.dx - info->var.xoffset;
1570                 dy = cursor->image.dy - info->var.yoffset;
1571
1572                 intelfbhw_cursor_setpos(dinfo, dx, dy);
1573         }
1574
1575         if (cursor->set & FB_CUR_SETSIZE) {
1576                 if (cursor->image.width > 64 || cursor->image.height > 64)
1577                         return -ENXIO;
1578
1579                 intelfbhw_cursor_reset(dinfo);
1580         }
1581
1582         if (cursor->set & FB_CUR_SETCMAP) {
1583                 u32 fg, bg;
1584
1585                 if (dinfo->depth != 8) {
1586                         fg = dinfo->pseudo_palette[cursor->image.fg_color];
1587                         bg = dinfo->pseudo_palette[cursor->image.bg_color];
1588                 } else {
1589                         fg = cursor->image.fg_color;
1590                         bg = cursor->image.bg_color;
1591                 }
1592
1593                 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1594         }
1595
1596         if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1597                 u32 s_pitch = (ROUND_UP_TO(cursor->image.width, 8) / 8);
1598                 u32 size = s_pitch * cursor->image.height;
1599                 u8 *dat = (u8 *) cursor->image.data;
1600                 u8 *msk = (u8 *) cursor->mask;
1601                 u8 src[64];
1602                 u32 i;
1603
1604                 if (cursor->image.depth != 1)
1605                         return -ENXIO;
1606
1607                 switch (cursor->rop) {
1608                 case ROP_XOR:
1609                         for (i = 0; i < size; i++)
1610                                 src[i] = dat[i] ^ msk[i];
1611                         break;
1612                 case ROP_COPY:
1613                 default:
1614                         for (i = 0; i < size; i++)
1615                                 src[i] = dat[i] & msk[i];
1616                         break;
1617                 }
1618
1619                 /* save the bitmap to restore it when XFree will
1620                    make the cursor dirty */
1621                 memcpy(dinfo->cursor_src, src, size);
1622
1623                 intelfbhw_cursor_load(dinfo, cursor->image.width,
1624                                       cursor->image.height, src);
1625         }
1626
1627         if (cursor->enable)
1628                 intelfbhw_cursor_show(dinfo);
1629
1630         return 0;
1631 }
1632
1633 static int
1634 intelfb_sync(struct fb_info *info)
1635 {
1636         struct intelfb_info *dinfo = GET_DINFO(info);
1637
1638 #if VERBOSE > 0
1639         DBG_MSG("intelfb_sync\n");
1640 #endif
1641
1642         if (dinfo->ring_lockup)
1643                 return 0;
1644
1645         intelfbhw_do_sync(dinfo);
1646         return 0;
1647 }
1648