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