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