Pull 1024-cpu into release branch
[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
5  * integrated graphics chips.
6  *
7  * Copyright © 2002, 2003 David Dawes <dawes@xfree86.org>
8  *                   2004 Sylvain Meyer
9  *
10  * This driver consists of two parts.  The first part (intelfbdrv.c) provides
11  * the basic fbdev interfaces, is derived in part from the radeonfb and
12  * vesafb drivers, and is covered by the GPL.  The second part (intelfbhw.c)
13  * provides the code to program the hardware.  Most of it is derived from
14  * the i810/i830 XFree86 driver.  The HW-specific code is covered here
15  * under a dual license (GPL and MIT/XFree86 license).
16  *
17  * Author: David Dawes
18  *
19  */
20
21 /* $DHD: intelfb/intelfbdrv.c,v 1.20 2003/06/27 15:17:40 dawes Exp $ */
22
23 /*
24  * Changes:
25  *    01/2003 - Initial driver (0.1.0), no mode switching, no acceleration.
26  *              This initial version is a basic core that works a lot like
27  *              the vesafb driver.  It must be built-in to the kernel,
28  *              and the initial video mode must be set with vga=XXX at
29  *              boot time.  (David Dawes)
30  *
31  *    01/2003 - Version 0.2.0: Mode switching added, colormap support
32  *              implemented, Y panning, and soft screen blanking implemented.
33  *              No acceleration yet.  (David Dawes)
34  *
35  *    01/2003 - Version 0.3.0: fbcon acceleration support added.  Module
36  *              option handling added.  (David Dawes)
37  *
38  *    01/2003 - Version 0.4.0: fbcon HW cursor support added.  (David Dawes)
39  *
40  *    01/2003 - Version 0.4.1: Add auto-generation of built-in modes.
41  *              (David Dawes)
42  *
43  *    02/2003 - Version 0.4.2: Add check for active non-CRT devices, and
44  *              mode validation checks.  (David Dawes)
45  *
46  *    02/2003 - Version 0.4.3: Check when the VC is in graphics mode so that
47  *              acceleration is disabled while an XFree86 server is running.
48  *              (David Dawes)
49  *
50  *    02/2003 - Version 0.4.4: Monitor DPMS support.  (David Dawes)
51  *
52  *    02/2003 - Version 0.4.5: Basic XFree86 + fbdev working.  (David Dawes)
53  *
54  *    02/2003 - Version 0.5.0: Modify to work with the 2.5.32 kernel as well
55  *              as 2.4.x kernels.  (David Dawes)
56  *
57  *    02/2003 - Version 0.6.0: Split out HW-specifics into a separate file.
58  *              (David Dawes)
59  *
60  *    02/2003 - Version 0.7.0: Test on 852GM/855GM.  Acceleration and HW
61  *              cursor are disabled on this platform.  (David Dawes)
62  *
63  *    02/2003 - Version 0.7.1: Test on 845G.  Acceleration is disabled
64  *              on this platform.  (David Dawes)
65  *
66  *    02/2003 - Version 0.7.2: Test on 830M.  Acceleration and HW
67  *              cursor are disabled on this platform.  (David Dawes)
68  *
69  *    02/2003 - Version 0.7.3: Fix 8-bit modes for mobile platforms
70  *              (David Dawes)
71  *
72  *    02/2003 - Version 0.7.4: Add checks for FB and FBCON_HAS_CFB* configured
73  *              in the kernel, and add mode bpp verification and default
74  *              bpp selection based on which FBCON_HAS_CFB* are configured.
75  *              (David Dawes)
76  *
77  *    02/2003 - Version 0.7.5: Add basic package/install scripts based on the
78  *              DRI packaging scripts.  (David Dawes)
79  *
80  *    04/2003 - Version 0.7.6: Fix typo that affects builds with SMP-enabled
81  *              kernels.  (David Dawes, reported by Anupam).
82  *
83  *    06/2003 - Version 0.7.7:
84  *              Fix Makefile.kernel build problem (Tsutomu Yasuda).
85  *              Fix mis-placed #endif (2.4.21 kernel).
86  *
87  *    09/2004 - Version 0.9.0 - by Sylvain Meyer
88  *              Port to linux 2.6 kernel fbdev
89  *              Fix HW accel and HW cursor on i845G
90  *              Use of agpgart for fb memory reservation
91  *              Add mtrr support
92  *
93  *    10/2004 - Version 0.9.1
94  *              Use module_param instead of old MODULE_PARM
95  *              Some cleanup
96  *
97  *    11/2004 - Version 0.9.2
98  *              Add vram option to reserve more memory than stolen by BIOS
99  *              Fix intelfbhw_pan_display typo
100  *              Add __initdata annotations
101  *
102  * TODO:
103  *
104  *
105  * Wish List:
106  *
107  *
108  */
109
110 #include <linux/config.h>
111 #include <linux/module.h>
112 #include <linux/kernel.h>
113 #include <linux/errno.h>
114 #include <linux/string.h>
115 #include <linux/mm.h>
116 #include <linux/tty.h>
117 #include <linux/slab.h>
118 #include <linux/delay.h>
119 #include <linux/fb.h>
120 #include <linux/ioport.h>
121 #include <linux/init.h>
122 #include <linux/pci.h>
123 #include <linux/vmalloc.h>
124 #include <linux/pagemap.h>
125 #include <linux/version.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_get_fix(struct fb_fix_screeninfo *fix,
140                            struct fb_info *info);
141
142 static int intelfb_check_var(struct fb_var_screeninfo *var,
143                              struct fb_info *info);
144 static int intelfb_set_par(struct fb_info *info);
145 static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
146                              unsigned blue, unsigned transp,
147                              struct fb_info *info);
148
149 static int intelfb_blank(int blank, struct fb_info *info);
150 static int intelfb_pan_display(struct fb_var_screeninfo *var,
151                                struct fb_info *info);
152
153 static void intelfb_fillrect(struct fb_info *info,
154                              const struct fb_fillrect *rect);
155 static void intelfb_copyarea(struct fb_info *info,
156                              const struct fb_copyarea *region);
157 static void intelfb_imageblit(struct fb_info *info,
158                               const struct fb_image *image);
159 static int intelfb_cursor(struct fb_info *info,
160                            struct fb_cursor *cursor);
161
162 static int intelfb_sync(struct fb_info *info);
163
164 static int intelfb_ioctl(struct inode *inode, struct file *file,
165                          unsigned int cmd, unsigned long arg,
166                          struct fb_info *info);
167
168 static int __devinit intelfb_pci_register(struct pci_dev *pdev,
169                                           const struct pci_device_id *ent);
170 static void __devexit intelfb_pci_unregister(struct pci_dev *pdev);
171 static int __devinit intelfb_set_fbinfo(struct intelfb_info *dinfo);
172
173 /*
174  * Limiting the class to PCI_CLASS_DISPLAY_VGA prevents function 1 of the
175  * mobile chipsets from being registered.
176  */
177 #if DETECT_VGA_CLASS_ONLY
178 #define INTELFB_CLASS_MASK ~0 << 8
179 #else
180 #define INTELFB_CLASS_MASK 0
181 #endif
182
183 static struct pci_device_id intelfb_pci_table[] __devinitdata = {
184         { 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 },
185         { 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 },
186         { 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 },
187         { 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 },
188         { 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 },
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), 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->mmio_base_phys)
477                 release_mem_region(dinfo->mmio_base_phys, INTEL_REG_SIZE);
478         if (dinfo->aperture.physical)
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                 aperture_bar = 2;
554                 mmio_bar = 0;
555                 /* Disable HW cursor on 915G (not implemented yet) */
556                 hwcursor = 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         if (!request_mem_region(dinfo->mmio_base_phys,
575                                 INTEL_REG_SIZE,
576                                 INTELFB_MODULE_NAME)) {
577                 ERR_MSG("Cannot reserve MMIO region.\n");
578                 cleanup(dinfo);
579                 return -ENODEV;
580         }
581
582         /* Get the chipset info. */
583         dinfo->pci_chipset = pdev->device;
584
585         if (intelfbhw_get_chipset(pdev, &dinfo->name, &dinfo->chipset,
586                                   &dinfo->mobile)) {
587                 cleanup(dinfo);
588                 return -ENODEV;
589         }
590
591         if (intelfbhw_get_memory(pdev, &aperture_size,&stolen_size)) {
592                 cleanup(dinfo);
593                 return -ENODEV;
594         }
595
596         INF_MSG("%02x:%02x.%d: %s, aperture size %dMB, "
597                 "stolen memory %dkB\n",
598                 pdev->bus->number, PCI_SLOT(pdev->devfn),
599                 PCI_FUNC(pdev->devfn), dinfo->name,
600                 BtoMB(aperture_size), BtoKB(stolen_size));
601
602         /* Set these from the options. */
603         dinfo->accel    = accel;
604         dinfo->hwcursor = hwcursor;
605
606         if (NOACCEL_CHIPSET(dinfo) && dinfo->accel == 1) {
607                 INF_MSG("Acceleration is not supported for the %s chipset.\n",
608                         dinfo->name);
609                 dinfo->accel = 0;
610         }
611
612         /* Framebuffer parameters - Use all the stolen memory if >= vram */
613         if (ROUND_UP_TO_PAGE(stolen_size) >= MB(vram)) {
614                 dinfo->fb.size = ROUND_UP_TO_PAGE(stolen_size);
615                 dinfo->fbmem_gart = 0;
616         } else {
617                 dinfo->fb.size =  MB(vram);
618                 dinfo->fbmem_gart = 1;
619         }
620
621         /* Allocate space for the ring buffer and HW cursor if enabled. */
622         if (dinfo->accel) {
623                 dinfo->ring.size = RINGBUFFER_SIZE;
624                 dinfo->ring_tail_mask = dinfo->ring.size - 1;
625         }
626         if (dinfo->hwcursor) {
627                 dinfo->cursor.size = HW_CURSOR_SIZE;
628         }
629
630         /* Use agpgart to manage the GATT */
631         if (!(bridge = agp_backend_acquire(pdev))) {
632                 ERR_MSG("cannot acquire agp\n");
633                 cleanup(dinfo);
634                 return -ENODEV;
635         }
636
637         /* get the current gatt info */
638         if (agp_copy_info(bridge, &gtt_info)) {
639                 ERR_MSG("cannot get agp info\n");
640                 agp_backend_release(bridge);
641                 cleanup(dinfo);
642                 return -ENODEV;
643         }
644
645         if (MB(voffset) < stolen_size)
646                 offset = (stolen_size >> 12);
647         else
648                 offset = ROUND_UP_TO_PAGE(MB(voffset))/GTT_PAGE_SIZE;
649
650         /* set the mem offsets - set them after the already used pages */
651         if (dinfo->accel) {
652                 dinfo->ring.offset = offset + gtt_info.current_memory;
653         }
654         if (dinfo->hwcursor) {
655                 dinfo->cursor.offset = offset +
656                         + gtt_info.current_memory + (dinfo->ring.size >> 12);
657         }
658         if (dinfo->fbmem_gart) {
659                 dinfo->fb.offset = offset +
660                         + gtt_info.current_memory + (dinfo->ring.size >> 12)
661                         + (dinfo->cursor.size >> 12);
662         }
663
664         /* Allocate memories (which aren't stolen) */
665         /* Map the fb and MMIO regions */
666         /* ioremap only up to the end of used aperture */
667         dinfo->aperture.virtual = (u8 __iomem *)ioremap_nocache
668                 (dinfo->aperture.physical, ((offset + dinfo->fb.offset) << 12)
669                  + dinfo->fb.size);
670         if (!dinfo->aperture.virtual) {
671                 ERR_MSG("Cannot remap FB region.\n");
672                 cleanup(dinfo);
673                 return -ENODEV;
674         }
675
676         dinfo->mmio_base =
677                 (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
678                                                INTEL_REG_SIZE);
679         if (!dinfo->mmio_base) {
680                 ERR_MSG("Cannot remap MMIO region.\n");
681                 cleanup(dinfo);
682                 return -ENODEV;
683         }
684
685         if (dinfo->accel) {
686                 if (!(dinfo->gtt_ring_mem =
687                       agp_allocate_memory(bridge, dinfo->ring.size >> 12,
688                                           AGP_NORMAL_MEMORY))) {
689                         ERR_MSG("cannot allocate ring buffer memory\n");
690                         agp_backend_release(bridge);
691                         cleanup(dinfo);
692                         return -ENOMEM;
693                 }
694                 if (agp_bind_memory(dinfo->gtt_ring_mem,
695                                     dinfo->ring.offset)) {
696                         ERR_MSG("cannot bind ring buffer memory\n");
697                         agp_backend_release(bridge);
698                         cleanup(dinfo);
699                         return -EBUSY;
700                 }
701                 dinfo->ring.physical = dinfo->aperture.physical
702                         + (dinfo->ring.offset << 12);
703                 dinfo->ring.virtual  = dinfo->aperture.virtual
704                         + (dinfo->ring.offset << 12);
705                 dinfo->ring_head = dinfo->ring.virtual;
706         }
707         if (dinfo->hwcursor) {
708                 agp_memtype = dinfo->mobile ? AGP_PHYSICAL_MEMORY
709                         : AGP_NORMAL_MEMORY;
710                 if (!(dinfo->gtt_cursor_mem =
711                       agp_allocate_memory(bridge, dinfo->cursor.size >> 12,
712                                           agp_memtype))) {
713                         ERR_MSG("cannot allocate cursor memory\n");
714                         agp_backend_release(bridge);
715                         cleanup(dinfo);
716                         return -ENOMEM;
717                 }
718                 if (agp_bind_memory(dinfo->gtt_cursor_mem,
719                                     dinfo->cursor.offset)) {
720                         ERR_MSG("cannot bind cursor memory\n");
721                         agp_backend_release(bridge);
722                         cleanup(dinfo);
723                         return -EBUSY;
724                 }
725                 if (dinfo->mobile)
726                         dinfo->cursor.physical
727                                 = dinfo->gtt_cursor_mem->physical;
728                 else
729                         dinfo->cursor.physical = dinfo->aperture.physical
730                                 + (dinfo->cursor.offset << 12);
731                 dinfo->cursor.virtual = dinfo->aperture.virtual
732                         + (dinfo->cursor.offset << 12);
733         }
734         if (dinfo->fbmem_gart) {
735                 if (!(dinfo->gtt_fb_mem =
736                       agp_allocate_memory(bridge, dinfo->fb.size >> 12,
737                                           AGP_NORMAL_MEMORY))) {
738                         WRN_MSG("cannot allocate framebuffer memory - use "
739                                 "the stolen one\n");
740                         dinfo->fbmem_gart = 0;
741                 }
742                 if (agp_bind_memory(dinfo->gtt_fb_mem,
743                                     dinfo->fb.offset)) {
744                         WRN_MSG("cannot bind framebuffer memory - use "
745                                 "the stolen one\n");
746                         dinfo->fbmem_gart = 0;
747                 }
748         }
749
750         /* update framebuffer memory parameters */
751         if (!dinfo->fbmem_gart)
752                 dinfo->fb.offset = 0;   /* starts at offset 0 */
753         dinfo->fb.physical = dinfo->aperture.physical
754                 + (dinfo->fb.offset << 12);
755         dinfo->fb.virtual = dinfo->aperture.virtual + (dinfo->fb.offset << 12);
756         dinfo->fb_start = dinfo->fb.offset << 12;
757
758         /* release agpgart */
759         agp_backend_release(bridge);
760
761         if (mtrr)
762                 set_mtrr(dinfo);
763
764         DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%x)\n",
765                 dinfo->fb.physical, dinfo->fb.offset, dinfo->fb.size,
766                 (u32 __iomem ) dinfo->fb.virtual);
767         DBG_MSG("MMIO: 0x%x/0x%x (0x%x)\n",
768                 dinfo->mmio_base_phys, INTEL_REG_SIZE,
769                 (u32 __iomem) dinfo->mmio_base);
770         DBG_MSG("ring buffer: 0x%x/0x%x (0x%x)\n",
771                 dinfo->ring.physical, dinfo->ring.size,
772                 (u32 __iomem ) dinfo->ring.virtual);
773         DBG_MSG("HW cursor: 0x%x/0x%x (0x%x) (offset 0x%x) (phys 0x%x)\n",
774                 dinfo->cursor.physical, dinfo->cursor.size,
775                 (u32 __iomem ) dinfo->cursor.virtual, dinfo->cursor.offset,
776                 dinfo->cursor.physical);
777
778         DBG_MSG("options: vram = %d, accel = %d, hwcursor = %d, fixed = %d, "
779                 "noinit = %d\n", vram, accel, hwcursor, fixed, noinit);
780         DBG_MSG("options: mode = \"%s\"\n", mode ? mode : "");
781
782         if (probeonly)
783                 bailout(dinfo);
784
785         /*
786          * Check if the LVDS port or any DVO ports are enabled.  If so,
787          * don't allow mode switching
788          */
789         dvo = intelfbhw_check_non_crt(dinfo);
790         if (dvo) {
791                 dinfo->fixed_mode = 1;
792                 WRN_MSG("Non-CRT device is enabled ( ");
793                 i = 0;
794                 while (dvo) {
795                         if (dvo & 1) {
796                                 s = intelfbhw_dvo_to_string(1 << i);
797                                 if (s)
798                                         printk("%s ", s);
799                         }
800                         dvo >>= 1;
801                         ++i;
802                 }
803                 printk(").  Disabling mode switching.\n");
804         }
805
806         if (bailearly == 1)
807                 bailout(dinfo);
808
809         if (FIXED_MODE(dinfo) && ORIG_VIDEO_ISVGA != VIDEO_TYPE_VLFB) {
810                 ERR_MSG("Video mode must be programmed at boot time.\n");
811                 cleanup(dinfo);
812                 return -ENODEV;
813         }
814
815         if (bailearly == 2)
816                 bailout(dinfo);
817
818         /* Initialise dinfo and related data. */
819         /* If an initial mode was programmed at boot time, get its details. */
820         if (ORIG_VIDEO_ISVGA == VIDEO_TYPE_VLFB)
821                 get_initial_mode(dinfo);
822
823         if (bailearly == 3)
824                 bailout(dinfo);
825
826         if (FIXED_MODE(dinfo)) {
827                 /* remap fb address */
828                 update_dinfo(dinfo, &dinfo->initial_var);
829         }
830
831         if (bailearly == 4)
832                 bailout(dinfo);
833
834
835         if (intelfb_set_fbinfo(dinfo)) {
836                 cleanup(dinfo);
837                 return -ENODEV;
838         }
839
840         if (bailearly == 5)
841                 bailout(dinfo);
842
843         if (bailearly == 6)
844                 bailout(dinfo);
845
846         pci_set_drvdata(pdev, dinfo);
847
848         /* Save the initial register state. */
849         i = intelfbhw_read_hw_state(dinfo, &dinfo->save_state,
850                                     bailearly > 6 ? bailearly - 6 : 0);
851         if (i != 0) {
852                 DBG_MSG("intelfbhw_read_hw_state returned %d\n", i);
853                 bailout(dinfo);
854         }
855
856         intelfbhw_print_hw_state(dinfo, &dinfo->save_state);
857
858         if (bailearly == 18)
859                 bailout(dinfo);
860
861         /* Cursor initialisation */
862         if (dinfo->hwcursor) {
863                 intelfbhw_cursor_init(dinfo);
864                 intelfbhw_cursor_reset(dinfo);
865         }
866
867         if (bailearly == 19)
868                 bailout(dinfo);
869
870         /* 2d acceleration init */
871         if (dinfo->accel)
872                 intelfbhw_2d_start(dinfo);
873
874         if (bailearly == 20)
875                 bailout(dinfo);
876
877         if (noregister)
878                 bailout(dinfo);
879
880         if (register_framebuffer(dinfo->info) < 0) {
881                 ERR_MSG("Cannot register framebuffer.\n");
882                 cleanup(dinfo);
883                 return -ENODEV;
884         }
885
886         dinfo->registered = 1;
887
888         return 0;
889
890 err_out_pixmap:
891         fb_dealloc_cmap(&info->cmap);
892 err_out_cmap:
893         framebuffer_release(info);
894         return -ENODEV;
895 }
896
897 static void __devexit
898 intelfb_pci_unregister(struct pci_dev *pdev)
899 {
900         struct intelfb_info *dinfo = pci_get_drvdata(pdev);
901
902         DBG_MSG("intelfb_pci_unregister\n");
903
904         if (!dinfo)
905                 return;
906
907         cleanup(dinfo);
908
909         pci_set_drvdata(pdev, NULL);
910 }
911
912 /***************************************************************
913  *                       helper functions                      *
914  ***************************************************************/
915
916 int __inline__
917 intelfb_var_to_depth(const struct fb_var_screeninfo *var)
918 {
919         DBG_MSG("intelfb_var_to_depth: bpp: %d, green.length is %d\n",
920                 var->bits_per_pixel, var->green.length);
921
922         switch (var->bits_per_pixel) {
923         case 16:
924                 return (var->green.length == 6) ? 16 : 15;
925         case 32:
926                 return 24;
927         default:
928                 return var->bits_per_pixel;
929         }
930 }
931
932
933 static __inline__ int
934 var_to_refresh(const struct fb_var_screeninfo *var)
935 {
936         int xtot = var->xres + var->left_margin + var->right_margin +
937                    var->hsync_len;
938         int ytot = var->yres + var->upper_margin + var->lower_margin +
939                    var->vsync_len;
940
941         return (1000000000 / var->pixclock * 1000 + 500) / xtot / ytot;
942 }
943
944 /***************************************************************
945  *                Various intialisation functions              *
946  ***************************************************************/
947
948 static void __devinit
949 get_initial_mode(struct intelfb_info *dinfo)
950 {
951         struct fb_var_screeninfo *var;
952         int xtot, ytot;
953
954         DBG_MSG("get_initial_mode\n");
955
956         dinfo->initial_vga = 1;
957         dinfo->initial_fb_base = screen_info.lfb_base;
958         dinfo->initial_video_ram = screen_info.lfb_size * KB(64);
959         dinfo->initial_pitch = screen_info.lfb_linelength;
960
961         var = &dinfo->initial_var;
962         memset(var, 0, sizeof(*var));
963         var->xres = screen_info.lfb_width;
964         var->yres = screen_info.lfb_height;
965         var->bits_per_pixel = screen_info.lfb_depth;
966         switch (screen_info.lfb_depth) {
967         case 15:
968                 var->bits_per_pixel = 16;
969                 break;
970         case 24:
971                 var->bits_per_pixel = 32;
972                 break;
973         }
974
975         DBG_MSG("Initial info: FB is 0x%x/0x%x (%d kByte)\n",
976                 dinfo->initial_fb_base, dinfo->initial_video_ram,
977                 BtoKB(dinfo->initial_video_ram));
978
979         DBG_MSG("Initial info: mode is %dx%d-%d (%d)\n",
980                 var->xres, var->yres, var->bits_per_pixel,
981                 dinfo->initial_pitch);
982
983         /* Dummy timing values (assume 60Hz) */
984         var->left_margin = (var->xres / 8) & 0xf8;
985         var->right_margin = 32;
986         var->upper_margin = 16;
987         var->lower_margin = 4;
988         var->hsync_len = (var->xres / 8) & 0xf8;
989         var->vsync_len = 4;
990
991         xtot = var->xres + var->left_margin +
992                 var->right_margin + var->hsync_len;
993         ytot = var->yres + var->upper_margin +
994                 var->lower_margin + var->vsync_len;
995         var->pixclock = 10000000 / xtot * 1000 / ytot * 100 / 60;
996
997         var->height = -1;
998         var->width = -1;
999
1000         if (var->bits_per_pixel > 8) {
1001                 var->red.offset = screen_info.red_pos;
1002                 var->red.length = screen_info.red_size;
1003                 var->green.offset = screen_info.green_pos;
1004                 var->green.length = screen_info.green_size;
1005                 var->blue.offset = screen_info.blue_pos;
1006                 var->blue.length = screen_info.blue_size;
1007                 var->transp.offset = screen_info.rsvd_pos;
1008                 var->transp.length = screen_info.rsvd_size;
1009         } else {
1010                 var->red.length = 8;
1011                 var->green.length = 8;
1012                 var->blue.length = 8;
1013         }
1014 }
1015
1016 static int __devinit
1017 intelfb_init_var(struct intelfb_info *dinfo)
1018 {
1019         struct fb_var_screeninfo *var;
1020         int msrc = 0;
1021
1022         DBG_MSG("intelfb_init_var\n");
1023
1024         var = &dinfo->info->var;
1025         if (FIXED_MODE(dinfo)) {
1026                 memcpy(var, &dinfo->initial_var,
1027                        sizeof(struct fb_var_screeninfo));
1028                 msrc = 5;
1029         } else {
1030                 if (mode) {
1031                         msrc = fb_find_mode(var, dinfo->info, mode,
1032                                             vesa_modes, VESA_MODEDB_SIZE,
1033                                             NULL, 0);
1034                         if (msrc)
1035                                 msrc |= 8;
1036                 }
1037                 if (!msrc) {
1038                         msrc = fb_find_mode(var, dinfo->info, PREFERRED_MODE,
1039                                             vesa_modes, VESA_MODEDB_SIZE,
1040                                             NULL, 0);
1041                 }
1042         }
1043
1044         if (!msrc) {
1045                 ERR_MSG("Cannot find a suitable video mode.\n");
1046                 return 1;
1047         }
1048
1049         INF_MSG("Initial video mode is %dx%d-%d@%d.\n", var->xres, var->yres,
1050                 var->bits_per_pixel, var_to_refresh(var));
1051
1052         DBG_MSG("Initial video mode is from %d.\n", msrc);
1053
1054 #if ALLOCATE_FOR_PANNING
1055         /* Allow use of half of the video ram for panning */
1056         var->xres_virtual = var->xres;
1057         var->yres_virtual =
1058                 dinfo->fb.size / 2 / (var->bits_per_pixel * var->xres);
1059         if (var->yres_virtual < var->yres)
1060                 var->yres_virtual = var->yres;
1061 #else
1062         var->yres_virtual = var->yres;
1063 #endif
1064
1065         if (dinfo->accel)
1066                 var->accel_flags |= FB_ACCELF_TEXT;
1067         else
1068                 var->accel_flags &= ~FB_ACCELF_TEXT;
1069
1070         return 0;
1071 }
1072
1073 static int __devinit
1074 intelfb_set_fbinfo(struct intelfb_info *dinfo)
1075 {
1076         struct fb_info *info = dinfo->info;
1077
1078         DBG_MSG("intelfb_set_fbinfo\n");
1079
1080         info->flags = FBINFO_FLAG_DEFAULT;
1081         info->fbops = &intel_fb_ops;
1082         info->pseudo_palette = dinfo->pseudo_palette;
1083
1084         info->pixmap.size = 64*1024;
1085         info->pixmap.buf_align = 8;
1086         info->pixmap.access_align = 32;
1087         info->pixmap.flags = FB_PIXMAP_SYSTEM;
1088
1089         if (intelfb_init_var(dinfo))
1090                 return 1;
1091
1092         info->pixmap.scan_align = 1;
1093
1094         update_dinfo(dinfo, &info->var);
1095
1096         return 0;
1097 }
1098
1099 /* Update dinfo to match the active video mode. */
1100 static void
1101 update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
1102 {
1103         DBG_MSG("update_dinfo\n");
1104
1105         dinfo->bpp = var->bits_per_pixel;
1106         dinfo->depth = intelfb_var_to_depth(var);
1107         dinfo->xres = var->xres;
1108         dinfo->yres = var->xres;
1109         dinfo->pixclock = var->pixclock;
1110
1111         intelfb_get_fix(&dinfo->info->fix, dinfo->info);
1112
1113         switch (dinfo->bpp) {
1114         case 8:
1115                 dinfo->visual = FB_VISUAL_PSEUDOCOLOR;
1116                 dinfo->pitch = var->xres_virtual;
1117                 break;
1118         case 16:
1119                 dinfo->visual = FB_VISUAL_TRUECOLOR;
1120                 dinfo->pitch = var->xres_virtual * 2;
1121                 break;
1122         case 32:
1123                 dinfo->visual = FB_VISUAL_TRUECOLOR;
1124                 dinfo->pitch = var->xres_virtual * 4;
1125                 break;
1126         }
1127
1128         /* Make sure the line length is a aligned correctly. */
1129         dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
1130
1131         if (FIXED_MODE(dinfo))
1132                 dinfo->pitch = dinfo->initial_pitch;
1133
1134         dinfo->info->screen_base = (char __iomem *)dinfo->fb.virtual;
1135         dinfo->info->fix.line_length = dinfo->pitch;
1136         dinfo->info->fix.visual = dinfo->visual;
1137 }
1138
1139 /* fbops functions */
1140
1141 static int
1142 intelfb_get_fix(struct fb_fix_screeninfo *fix, struct fb_info *info)
1143 {
1144         struct intelfb_info *dinfo = GET_DINFO(info);
1145
1146         DBG_MSG("intelfb_get_fix\n");
1147
1148         memset(fix, 0, sizeof(*fix));
1149         strcpy(fix->id, dinfo->name);
1150         fix->smem_start = dinfo->fb.physical;
1151         fix->smem_len = dinfo->fb.size;
1152         fix->type = FB_TYPE_PACKED_PIXELS;
1153         fix->type_aux = 0;
1154         fix->visual = dinfo->visual;
1155         fix->xpanstep = 8;
1156         fix->ypanstep = 1;
1157         fix->ywrapstep = 0;
1158         fix->line_length = dinfo->pitch;
1159         fix->mmio_start = dinfo->mmio_base_phys;
1160         fix->mmio_len = INTEL_REG_SIZE;
1161         fix->accel = FB_ACCEL_I830;
1162         return 0;
1163 }
1164
1165 /***************************************************************
1166  *                       fbdev interface                       *
1167  ***************************************************************/
1168
1169 static int
1170 intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1171 {
1172         int change_var = 0;
1173         struct fb_var_screeninfo v;
1174         struct intelfb_info *dinfo;
1175         static int first = 1;
1176
1177         DBG_MSG("intelfb_check_var: accel_flags is %d\n", var->accel_flags);
1178
1179         dinfo = GET_DINFO(info);
1180
1181         if (intelfbhw_validate_mode(dinfo, var) != 0)
1182                 return -EINVAL;
1183
1184         v = *var;
1185
1186         /* Check for a supported bpp. */
1187         if (v.bits_per_pixel <= 8) {
1188                 v.bits_per_pixel = 8;
1189         } else if (v.bits_per_pixel <= 16) {
1190                 if (v.bits_per_pixel == 16)
1191                         v.green.length = 6;
1192                 v.bits_per_pixel = 16;
1193         } else if (v.bits_per_pixel <= 32) {
1194                 v.bits_per_pixel = 32;
1195         } else
1196                 return -EINVAL;
1197
1198         change_var = ((info->var.xres != var->xres) ||
1199                       (info->var.yres != var->yres) ||
1200                       (info->var.xres_virtual != var->xres_virtual) ||
1201                       (info->var.yres_virtual != var->yres_virtual) ||
1202                       (info->var.bits_per_pixel != var->bits_per_pixel) ||
1203                       memcmp(&info->var.red, &var->red, sizeof(var->red)) ||
1204                       memcmp(&info->var.green, &var->green,
1205                              sizeof(var->green)) ||
1206                       memcmp(&info->var.blue, &var->blue, sizeof(var->blue)));
1207
1208         if (FIXED_MODE(dinfo) &&
1209             (change_var ||
1210              var->yres_virtual > dinfo->initial_var.yres_virtual ||
1211              var->yres_virtual < dinfo->initial_var.yres ||
1212              var->xoffset || var->nonstd)) {
1213                 if (first) {
1214                         ERR_MSG("Changing the video mode is not supported.\n");
1215                         first = 0;
1216                 }
1217                 return -EINVAL;
1218         }
1219
1220         switch (intelfb_var_to_depth(&v)) {
1221         case 8:
1222                 v.red.offset = v.green.offset = v.blue.offset = 0;
1223                 v.red.length = v.green.length = v.blue.length = 8;
1224                 v.transp.offset = v.transp.length = 0;
1225                 break;
1226         case 15:
1227                 v.red.offset = 10;
1228                 v.green.offset = 5;
1229                 v.blue.offset = 0;
1230                 v.red.length = v.green.length = v.blue.length = 5;
1231                 v.transp.offset = v.transp.length = 0;
1232                 break;
1233         case 16:
1234                 v.red.offset = 11;
1235                 v.green.offset = 5;
1236                 v.blue.offset = 0;
1237                 v.red.length = 5;
1238                 v.green.length = 6;
1239                 v.blue.length = 5;
1240                 v.transp.offset = v.transp.length = 0;
1241                 break;
1242         case 24:
1243                 v.red.offset = 16;
1244                 v.green.offset = 8;
1245                 v.blue.offset = 0;
1246                 v.red.length = v.green.length = v.blue.length = 8;
1247                 v.transp.offset = v.transp.length = 0;
1248                 break;
1249         case 32:
1250                 v.red.offset = 16;
1251                 v.green.offset = 8;
1252                 v.blue.offset = 0;
1253                 v.red.length = v.green.length = v.blue.length = 8;
1254                 v.transp.offset = 24;
1255                 v.transp.length = 8;
1256                 break;
1257         }
1258
1259         if (v.xoffset < 0)
1260                 v.xoffset = 0;
1261         if (v.yoffset < 0)
1262                 v.yoffset = 0;
1263
1264         if (v.xoffset > v.xres_virtual - v.xres)
1265                 v.xoffset = v.xres_virtual - v.xres;
1266         if (v.yoffset > v.yres_virtual - v.yres)
1267                 v.yoffset = v.yres_virtual - v.yres;
1268
1269         v.red.msb_right = v.green.msb_right = v.blue.msb_right =
1270                           v.transp.msb_right = 0;
1271
1272         *var = v;
1273
1274         return 0;
1275 }
1276
1277 static int
1278 intelfb_set_par(struct fb_info *info)
1279 {
1280         struct intelfb_hwstate *hw;
1281         struct intelfb_info *dinfo = GET_DINFO(info);
1282
1283         if (FIXED_MODE(dinfo)) {
1284                 ERR_MSG("Changing the video mode is not supported.\n");
1285                 return -EINVAL;
1286         }
1287
1288         hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
1289         if (!hw)
1290                 return -ENOMEM;
1291
1292         DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
1293                 info->var.yres, info->var.bits_per_pixel);
1294
1295         intelfb_blank(FB_BLANK_POWERDOWN, info);
1296
1297         if (ACCEL(dinfo, info))
1298                 intelfbhw_2d_stop(dinfo);
1299
1300         memcpy(hw, &dinfo->save_state, sizeof(*hw));
1301         if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
1302                 goto invalid_mode;
1303         if (intelfbhw_program_mode(dinfo, hw, 0))
1304                 goto invalid_mode;
1305
1306 #if REGDUMP > 0
1307         intelfbhw_read_hw_state(dinfo, hw, 0);
1308         intelfbhw_print_hw_state(dinfo, hw);
1309 #endif
1310
1311         update_dinfo(dinfo, &info->var);
1312
1313         if (ACCEL(dinfo, info))
1314                 intelfbhw_2d_start(dinfo);
1315
1316         intelfb_pan_display(&info->var, info);
1317
1318         intelfb_blank(FB_BLANK_UNBLANK, info);
1319
1320         if (ACCEL(dinfo, info)) {
1321                 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
1322                 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
1323                 FBINFO_HWACCEL_IMAGEBLIT;
1324         } else {
1325                 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1326         }
1327         kfree(hw);
1328         return 0;
1329 invalid_mode:
1330         kfree(hw);
1331         return -EINVAL;
1332 }
1333
1334 static int
1335 intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1336                   unsigned blue, unsigned transp, struct fb_info *info)
1337 {
1338         struct intelfb_info *dinfo = GET_DINFO(info);
1339
1340 #if VERBOSE > 0
1341         DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
1342 #endif
1343
1344         if (regno > 255)
1345                 return 1;
1346
1347         switch (dinfo->depth) {
1348         case 8:
1349                 {
1350                         red >>= 8;
1351                         green >>= 8;
1352                         blue >>= 8;
1353
1354                         intelfbhw_setcolreg(dinfo, regno, red, green, blue,
1355                                             transp);
1356                 }
1357                 break;
1358         case 15:
1359                 dinfo->pseudo_palette[regno] = ((red & 0xf800) >>  1) |
1360                                                ((green & 0xf800) >>  6) |
1361                                                ((blue & 0xf800) >> 11);
1362                 break;
1363         case 16:
1364                 dinfo->pseudo_palette[regno] = (red & 0xf800) |
1365                                                ((green & 0xfc00) >>  5) |
1366                                                ((blue  & 0xf800) >> 11);
1367                 break;
1368         case 24:
1369                 dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
1370                                                (green & 0xff00) |
1371                                                ((blue  & 0xff00) >> 8);
1372                 break;
1373         }
1374         return 0;
1375 }
1376
1377 static int
1378 intelfb_blank(int blank, struct fb_info *info)
1379 {
1380         intelfbhw_do_blank(blank, info);
1381         return 0;
1382 }
1383
1384 static int
1385 intelfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1386 {
1387         intelfbhw_pan_display(var, info);
1388         return 0;
1389 }
1390
1391 /* When/if we have our own ioctls. */
1392 static int
1393 intelfb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1394               unsigned long arg, struct fb_info *info)
1395 {
1396         int retval = 0;
1397
1398         return retval;
1399 }
1400
1401 static void
1402 intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
1403 {
1404         struct intelfb_info *dinfo = GET_DINFO(info);
1405         u32 rop, color;
1406
1407 #if VERBOSE > 0
1408         DBG_MSG("intelfb_fillrect\n");
1409 #endif
1410
1411         if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1412                 return cfb_fillrect(info, rect);
1413
1414         if (rect->rop == ROP_COPY)
1415                 rop = PAT_ROP_GXCOPY;
1416         else // ROP_XOR
1417                 rop = PAT_ROP_GXXOR;
1418
1419         if (dinfo->depth != 8)
1420                 color = dinfo->pseudo_palette[rect->color];
1421         else
1422                 color = rect->color;
1423
1424         intelfbhw_do_fillrect(dinfo, rect->dx, rect->dy,
1425                               rect->width, rect->height, color,
1426                               dinfo->pitch, info->var.bits_per_pixel,
1427                               rop);
1428 }
1429
1430 static void
1431 intelfb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1432 {
1433         struct intelfb_info *dinfo = GET_DINFO(info);
1434
1435 #if VERBOSE > 0
1436         DBG_MSG("intelfb_copyarea\n");
1437 #endif
1438
1439         if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1440                 return cfb_copyarea(info, region);
1441
1442         intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
1443                             region->dy, region->width, region->height,
1444                             dinfo->pitch, info->var.bits_per_pixel);
1445 }
1446
1447 static void
1448 intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
1449 {
1450         struct intelfb_info *dinfo = GET_DINFO(info);
1451         u32 fgcolor, bgcolor;
1452
1453 #if VERBOSE > 0
1454         DBG_MSG("intelfb_imageblit\n");
1455 #endif
1456
1457         if (!ACCEL(dinfo, info) || dinfo->depth == 4
1458             || image->depth != 1)
1459                 return cfb_imageblit(info, image);
1460
1461         if (dinfo->depth != 8) {
1462                 fgcolor = dinfo->pseudo_palette[image->fg_color];
1463                 bgcolor = dinfo->pseudo_palette[image->bg_color];
1464         } else {
1465                 fgcolor = image->fg_color;
1466                 bgcolor = image->bg_color;
1467         }
1468
1469         if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
1470                                     image->height, image->data,
1471                                     image->dx, image->dy,
1472                                     dinfo->pitch, info->var.bits_per_pixel))
1473                 return cfb_imageblit(info, image);
1474 }
1475
1476 static int
1477 intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1478 {
1479         struct intelfb_info *dinfo = GET_DINFO(info);
1480
1481 #if VERBOSE > 0
1482         DBG_MSG("intelfb_cursor\n");
1483 #endif
1484
1485         if (!dinfo->hwcursor)
1486                 return soft_cursor(info, cursor);
1487
1488         intelfbhw_cursor_hide(dinfo);
1489
1490         /* If XFree killed the cursor - restore it */
1491         if (INREG(CURSOR_A_BASEADDR) != dinfo->cursor.offset << 12) {
1492                 u32 fg, bg;
1493
1494                 DBG_MSG("the cursor was killed - restore it !!\n");
1495                 DBG_MSG("size %d, %d   pos %d, %d\n",
1496                         cursor->image.width, cursor->image.height,
1497                         cursor->image.dx, cursor->image.dy);
1498
1499                 intelfbhw_cursor_init(dinfo);
1500                 intelfbhw_cursor_reset(dinfo);
1501                 intelfbhw_cursor_setpos(dinfo, cursor->image.dx,
1502                                         cursor->image.dy);
1503
1504                 if (dinfo->depth != 8) {
1505                         fg =dinfo->pseudo_palette[cursor->image.fg_color];
1506                         bg =dinfo->pseudo_palette[cursor->image.bg_color];
1507                 } else {
1508                         fg = cursor->image.fg_color;
1509                         bg = cursor->image.bg_color;
1510                 }
1511                 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1512                 intelfbhw_cursor_load(dinfo, cursor->image.width,
1513                                       cursor->image.height,
1514                                       dinfo->cursor_src);
1515
1516                 if (cursor->enable)
1517                         intelfbhw_cursor_show(dinfo);
1518                 return 0;
1519         }
1520
1521         if (cursor->set & FB_CUR_SETPOS) {
1522                 u32 dx, dy;
1523
1524                 dx = cursor->image.dx - info->var.xoffset;
1525                 dy = cursor->image.dy - info->var.yoffset;
1526
1527                 intelfbhw_cursor_setpos(dinfo, dx, dy);
1528         }
1529
1530         if (cursor->set & FB_CUR_SETSIZE) {
1531                 if (cursor->image.width > 64 || cursor->image.height > 64)
1532                         return -ENXIO;
1533
1534                 intelfbhw_cursor_reset(dinfo);
1535         }
1536
1537         if (cursor->set & FB_CUR_SETCMAP) {
1538                 u32 fg, bg;
1539
1540                 if (dinfo->depth != 8) {
1541                         fg = dinfo->pseudo_palette[cursor->image.fg_color];
1542                         bg = dinfo->pseudo_palette[cursor->image.bg_color];
1543                 } else {
1544                         fg = cursor->image.fg_color;
1545                         bg = cursor->image.bg_color;
1546                 }
1547
1548                 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1549         }
1550
1551         if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1552                 u32 s_pitch = (ROUND_UP_TO(cursor->image.width, 8) / 8);
1553                 u32 size = s_pitch * cursor->image.height;
1554                 u8 *dat = (u8 *) cursor->image.data;
1555                 u8 *msk = (u8 *) cursor->mask;
1556                 u8 src[64];
1557                 u32 i;
1558
1559                 if (cursor->image.depth != 1)
1560                         return -ENXIO;
1561
1562                 switch (cursor->rop) {
1563                 case ROP_XOR:
1564                         for (i = 0; i < size; i++)
1565                                 src[i] = dat[i] ^ msk[i];
1566                         break;
1567                 case ROP_COPY:
1568                 default:
1569                         for (i = 0; i < size; i++)
1570                                 src[i] = dat[i] & msk[i];
1571                         break;
1572                 }
1573
1574                 /* save the bitmap to restore it when XFree will
1575                    make the cursor dirty */
1576                 memcpy(dinfo->cursor_src, src, size);
1577
1578                 intelfbhw_cursor_load(dinfo, cursor->image.width,
1579                                       cursor->image.height, src);
1580         }
1581
1582         if (cursor->enable)
1583                 intelfbhw_cursor_show(dinfo);
1584
1585         return 0;
1586 }
1587
1588 static int
1589 intelfb_sync(struct fb_info *info)
1590 {
1591         struct intelfb_info *dinfo = GET_DINFO(info);
1592
1593 #if VERBOSE > 0
1594         DBG_MSG("intelfb_sync\n");
1595 #endif
1596
1597         if (dinfo->ring_lockup)
1598                 return 0;
1599
1600         intelfbhw_do_sync(dinfo);
1601         return 0;
1602 }
1603