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