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