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