intelfb: add preliminary i2c support
[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  * 945G/945GM integrated graphics chips.
6  *
7  * Copyright © 2002, 2003 David Dawes <dawes@xfree86.org>
8  *                   2004 Sylvain Meyer
9  *                   2006 David Airlie
10  *
11  * This driver consists of two parts.  The first part (intelfbdrv.c) provides
12  * the basic fbdev interfaces, is derived in part from the radeonfb and
13  * vesafb drivers, and is covered by the GPL.  The second part (intelfbhw.c)
14  * provides the code to program the hardware.  Most of it is derived from
15  * the i810/i830 XFree86 driver.  The HW-specific code is covered here
16  * under a dual license (GPL and MIT/XFree86 license).
17  *
18  * Author: David Dawes
19  *
20  */
21
22 /* $DHD: intelfb/intelfbdrv.c,v 1.20 2003/06/27 15:17:40 dawes Exp $ */
23
24 /*
25  * Changes:
26  *    01/2003 - Initial driver (0.1.0), no mode switching, no acceleration.
27  *              This initial version is a basic core that works a lot like
28  *              the vesafb driver.  It must be built-in to the kernel,
29  *              and the initial video mode must be set with vga=XXX at
30  *              boot time.  (David Dawes)
31  *
32  *    01/2003 - Version 0.2.0: Mode switching added, colormap support
33  *              implemented, Y panning, and soft screen blanking implemented.
34  *              No acceleration yet.  (David Dawes)
35  *
36  *    01/2003 - Version 0.3.0: fbcon acceleration support added.  Module
37  *              option handling added.  (David Dawes)
38  *
39  *    01/2003 - Version 0.4.0: fbcon HW cursor support added.  (David Dawes)
40  *
41  *    01/2003 - Version 0.4.1: Add auto-generation of built-in modes.
42  *              (David Dawes)
43  *
44  *    02/2003 - Version 0.4.2: Add check for active non-CRT devices, and
45  *              mode validation checks.  (David Dawes)
46  *
47  *    02/2003 - Version 0.4.3: Check when the VC is in graphics mode so that
48  *              acceleration is disabled while an XFree86 server is running.
49  *              (David Dawes)
50  *
51  *    02/2003 - Version 0.4.4: Monitor DPMS support.  (David Dawes)
52  *
53  *    02/2003 - Version 0.4.5: Basic XFree86 + fbdev working.  (David Dawes)
54  *
55  *    02/2003 - Version 0.5.0: Modify to work with the 2.5.32 kernel as well
56  *              as 2.4.x kernels.  (David Dawes)
57  *
58  *    02/2003 - Version 0.6.0: Split out HW-specifics into a separate file.
59  *              (David Dawes)
60  *
61  *    02/2003 - Version 0.7.0: Test on 852GM/855GM.  Acceleration and HW
62  *              cursor are disabled on this platform.  (David Dawes)
63  *
64  *    02/2003 - Version 0.7.1: Test on 845G.  Acceleration is disabled
65  *              on this platform.  (David Dawes)
66  *
67  *    02/2003 - Version 0.7.2: Test on 830M.  Acceleration and HW
68  *              cursor are disabled on this platform.  (David Dawes)
69  *
70  *    02/2003 - Version 0.7.3: Fix 8-bit modes for mobile platforms
71  *              (David Dawes)
72  *
73  *    02/2003 - Version 0.7.4: Add checks for FB and FBCON_HAS_CFB* configured
74  *              in the kernel, and add mode bpp verification and default
75  *              bpp selection based on which FBCON_HAS_CFB* are configured.
76  *              (David Dawes)
77  *
78  *    02/2003 - Version 0.7.5: Add basic package/install scripts based on the
79  *              DRI packaging scripts.  (David Dawes)
80  *
81  *    04/2003 - Version 0.7.6: Fix typo that affects builds with SMP-enabled
82  *              kernels.  (David Dawes, reported by Anupam).
83  *
84  *    06/2003 - Version 0.7.7:
85  *              Fix Makefile.kernel build problem (Tsutomu Yasuda).
86  *              Fix mis-placed #endif (2.4.21 kernel).
87  *
88  *    09/2004 - Version 0.9.0 - by Sylvain Meyer
89  *              Port to linux 2.6 kernel fbdev
90  *              Fix HW accel and HW cursor on i845G
91  *              Use of agpgart for fb memory reservation
92  *              Add mtrr support
93  *
94  *    10/2004 - Version 0.9.1
95  *              Use module_param instead of old MODULE_PARM
96  *              Some cleanup
97  *
98  *    11/2004 - Version 0.9.2
99  *              Add vram option to reserve more memory than stolen by BIOS
100  *              Fix intelfbhw_pan_display typo
101  *              Add __initdata annotations
102  *
103  * TODO:
104  *
105  *
106  * Wish List:
107  *
108  *
109  */
110
111 #include <linux/config.h>
112 #include <linux/module.h>
113 #include <linux/kernel.h>
114 #include <linux/errno.h>
115 #include <linux/string.h>
116 #include <linux/mm.h>
117 #include <linux/tty.h>
118 #include <linux/slab.h>
119 #include <linux/delay.h>
120 #include <linux/fb.h>
121 #include <linux/ioport.h>
122 #include <linux/init.h>
123 #include <linux/pci.h>
124 #include <linux/vmalloc.h>
125 #include <linux/pagemap.h>
126
127 #include <asm/io.h>
128
129 #ifdef CONFIG_MTRR
130 #include <asm/mtrr.h>
131 #endif
132
133 #include "intelfb.h"
134 #include "intelfbhw.h"
135 #include "../edid.h"
136
137 static void __devinit get_initial_mode(struct intelfb_info *dinfo);
138 static void update_dinfo(struct intelfb_info *dinfo,
139                          struct fb_var_screeninfo *var);
140 static int intelfb_open(struct fb_info *info, int user);
141 static int intelfb_release(struct fb_info *info, int user);
142 static int intelfb_check_var(struct fb_var_screeninfo *var,
143                              struct fb_info *info);
144 static int intelfb_set_par(struct fb_info *info);
145 static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
146                              unsigned blue, unsigned transp,
147                              struct fb_info *info);
148
149 static int intelfb_blank(int blank, struct fb_info *info);
150 static int intelfb_pan_display(struct fb_var_screeninfo *var,
151                                struct fb_info *info);
152
153 static void intelfb_fillrect(struct fb_info *info,
154                              const struct fb_fillrect *rect);
155 static void intelfb_copyarea(struct fb_info *info,
156                              const struct fb_copyarea *region);
157 static void intelfb_imageblit(struct fb_info *info,
158                               const struct fb_image *image);
159 static int intelfb_cursor(struct fb_info *info,
160                            struct fb_cursor *cursor);
161
162 static int intelfb_sync(struct fb_info *info);
163
164 static int intelfb_ioctl(struct fb_info *info,
165                          unsigned int cmd, unsigned long arg);
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         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945G },
190         { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945GM },
191         { 0, }
192 };
193
194 /* Global data */
195 static int num_registered = 0;
196
197 /* fb ops */
198 static struct fb_ops intel_fb_ops = {
199         .owner =                THIS_MODULE,
200         .fb_open =              intelfb_open,
201         .fb_release =           intelfb_release,
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 =         "intelfb",
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        = 1;
232 static int vram         = 4;
233 static int hwcursor     = 0;
234 static int mtrr         = 1;
235 static int fixed        = 0;
236 static int noinit       = 0;
237 static int noregister   = 0;
238 static int probeonly    = 0;
239 static int idonly       = 0;
240 static int bailearly    = 0;
241 static int voffset      = 48;
242 static char *mode       = NULL;
243
244 module_param(accel, bool, S_IRUGO);
245 MODULE_PARM_DESC(accel, "Enable hardware acceleration");
246 module_param(vram, int, S_IRUGO);
247 MODULE_PARM_DESC(vram, "System RAM to allocate to framebuffer in MiB");
248 module_param(voffset, int, S_IRUGO);
249 MODULE_PARM_DESC(voffset, "Offset of framebuffer in MiB");
250 module_param(hwcursor, bool, S_IRUGO);
251 MODULE_PARM_DESC(hwcursor, "Enable HW cursor");
252 module_param(mtrr, bool, S_IRUGO);
253 MODULE_PARM_DESC(mtrr, "Enable MTRR support");
254 module_param(fixed, bool, S_IRUGO);
255 MODULE_PARM_DESC(fixed, "Disable mode switching");
256 module_param(noinit, bool, 0);
257 MODULE_PARM_DESC(noinit, "Don't initialise graphics mode when loading");
258 module_param(noregister, bool, 0);
259 MODULE_PARM_DESC(noregister, "Don't register, just probe and exit (debug)");
260 module_param(probeonly, bool, 0);
261 MODULE_PARM_DESC(probeonly, "Do a minimal probe (debug)");
262 module_param(idonly, bool, 0);
263 MODULE_PARM_DESC(idonly, "Just identify without doing anything else (debug)");
264 module_param(bailearly, bool, 0);
265 MODULE_PARM_DESC(bailearly, "Bail out early, depending on value (debug)");
266 module_param(mode, charp, S_IRUGO);
267 MODULE_PARM_DESC(mode,
268                  "Initial video mode \"<xres>x<yres>[-<depth>][@<refresh>]\"");
269
270 #ifndef MODULE
271 #define OPT_EQUAL(opt, name) (!strncmp(opt, name, strlen(name)))
272 #define OPT_INTVAL(opt, name) simple_strtoul(opt + strlen(name) + 1, NULL, 0)
273 #define OPT_STRVAL(opt, name) (opt + strlen(name))
274
275 static __inline__ char *
276 get_opt_string(const char *this_opt, const char *name)
277 {
278         const char *p;
279         int i;
280         char *ret;
281
282         p = OPT_STRVAL(this_opt, name);
283         i = 0;
284         while (p[i] && p[i] != ' ' && p[i] != ',')
285                 i++;
286         ret = kmalloc(i + 1, GFP_KERNEL);
287         if (ret) {
288                 strncpy(ret, p, i);
289                 ret[i] = '\0';
290         }
291         return ret;
292 }
293
294 static __inline__ int
295 get_opt_int(const char *this_opt, const char *name, int *ret)
296 {
297         if (!ret)
298                 return 0;
299
300         if (!OPT_EQUAL(this_opt, name))
301                 return 0;
302
303         *ret = OPT_INTVAL(this_opt, name);
304         return 1;
305 }
306
307 static __inline__ int
308 get_opt_bool(const char *this_opt, const char *name, int *ret)
309 {
310         if (!ret)
311                 return 0;
312
313         if (OPT_EQUAL(this_opt, name)) {
314                 if (this_opt[strlen(name)] == '=')
315                         *ret = simple_strtoul(this_opt + strlen(name) + 1,
316                                               NULL, 0);
317                 else
318                         *ret = 1;
319         } else {
320                 if (OPT_EQUAL(this_opt, "no") && OPT_EQUAL(this_opt + 2, name))
321                         *ret = 0;
322                 else
323                         return 0;
324         }
325         return 1;
326 }
327
328 static int __init
329 intelfb_setup(char *options)
330 {
331         char *this_opt;
332
333         DBG_MSG("intelfb_setup\n");
334
335         if (!options || !*options) {
336                 DBG_MSG("no options\n");
337                 return 0;
338         } else
339                 DBG_MSG("options: %s\n", options);
340
341         /*
342          * These are the built-in options analogous to the module parameters
343          * defined above.
344          *
345          * The syntax is:
346          *
347          *    video=intelfb:[mode][,<param>=<val>] ...
348          *
349          * e.g.,
350          *
351          *    video=intelfb:1024x768-16@75,accel=0
352          */
353
354         while ((this_opt = strsep(&options, ","))) {
355                 if (!*this_opt)
356                         continue;
357                 if (get_opt_bool(this_opt, "accel", &accel))
358                         ;
359                 else if (get_opt_int(this_opt, "vram", &vram))
360                         ;
361                 else if (get_opt_bool(this_opt, "hwcursor", &hwcursor))
362                         ;
363                 else if (get_opt_bool(this_opt, "mtrr", &mtrr))
364                         ;
365                 else if (get_opt_bool(this_opt, "fixed", &fixed))
366                         ;
367                 else if (get_opt_bool(this_opt, "init", &noinit))
368                         noinit = !noinit;
369                 else if (OPT_EQUAL(this_opt, "mode="))
370                         mode = get_opt_string(this_opt, "mode=");
371                 else
372                         mode = this_opt;
373         }
374
375         return 0;
376 }
377
378 #endif
379
380 static int __init
381 intelfb_init(void)
382 {
383 #ifndef MODULE
384         char *option = NULL;
385 #endif
386
387         DBG_MSG("intelfb_init\n");
388
389         INF_MSG("Framebuffer driver for "
390                 "Intel(R) " SUPPORTED_CHIPSETS " chipsets\n");
391         INF_MSG("Version " INTELFB_VERSION "\n");
392
393         if (idonly)
394                 return -ENODEV;
395
396 #ifndef MODULE
397         if (fb_get_options("intelfb", &option))
398                 return -ENODEV;
399         intelfb_setup(option);
400 #endif
401
402         return pci_register_driver(&intelfb_driver);
403 }
404
405 static void __exit
406 intelfb_exit(void)
407 {
408         DBG_MSG("intelfb_exit\n");
409         pci_unregister_driver(&intelfb_driver);
410 }
411
412 module_init(intelfb_init);
413 module_exit(intelfb_exit);
414
415 /***************************************************************
416  *                     mtrr support functions                  *
417  ***************************************************************/
418
419 #ifdef CONFIG_MTRR
420 static inline void __devinit set_mtrr(struct intelfb_info *dinfo)
421 {
422         dinfo->mtrr_reg = mtrr_add(dinfo->aperture.physical,
423                                    dinfo->aperture.size, MTRR_TYPE_WRCOMB, 1);
424         if (dinfo->mtrr_reg < 0) {
425                 ERR_MSG("unable to set MTRR\n");
426                 return;
427         }
428         dinfo->has_mtrr = 1;
429 }
430 static inline void unset_mtrr(struct intelfb_info *dinfo)
431 {
432         if (dinfo->has_mtrr)
433                 mtrr_del(dinfo->mtrr_reg, dinfo->aperture.physical,
434                          dinfo->aperture.size);
435 }
436 #else
437 #define set_mtrr(x) WRN_MSG("MTRR is disabled in the kernel\n")
438
439 #define unset_mtrr(x) do { } while (0)
440 #endif /* CONFIG_MTRR */
441
442 /***************************************************************
443  *                        driver init / cleanup                *
444  ***************************************************************/
445
446 static void
447 cleanup(struct intelfb_info *dinfo)
448 {
449         DBG_MSG("cleanup\n");
450
451         if (!dinfo)
452                 return;
453
454         intelfbhw_disable_irq(dinfo);
455
456         fb_dealloc_cmap(&dinfo->info->cmap);
457         kfree(dinfo->info->pixmap.addr);
458
459         if (dinfo->registered)
460                 unregister_framebuffer(dinfo->info);
461
462         unset_mtrr(dinfo);
463
464         if (dinfo->fbmem_gart && dinfo->gtt_fb_mem) {
465                 agp_unbind_memory(dinfo->gtt_fb_mem);
466                 agp_free_memory(dinfo->gtt_fb_mem);
467         }
468         if (dinfo->gtt_cursor_mem) {
469                 agp_unbind_memory(dinfo->gtt_cursor_mem);
470                 agp_free_memory(dinfo->gtt_cursor_mem);
471         }
472         if (dinfo->gtt_ring_mem) {
473                 agp_unbind_memory(dinfo->gtt_ring_mem);
474                 agp_free_memory(dinfo->gtt_ring_mem);
475         }
476
477 #ifdef CONFIG_FB_INTEL_I2C
478         /* un-register I2C bus */
479         intelfb_delete_i2c_busses(dinfo);
480 #endif
481
482         if (dinfo->mmio_base)
483                 iounmap((void __iomem *)dinfo->mmio_base);
484         if (dinfo->aperture.virtual)
485                 iounmap((void __iomem *)dinfo->aperture.virtual);
486
487         if (dinfo->flag & INTELFB_MMIO_ACQUIRED)
488                 release_mem_region(dinfo->mmio_base_phys, INTEL_REG_SIZE);
489         if (dinfo->flag & INTELFB_FB_ACQUIRED)
490                 release_mem_region(dinfo->aperture.physical,
491                                    dinfo->aperture.size);
492         framebuffer_release(dinfo->info);
493 }
494
495 #define bailout(dinfo) do {                                             \
496         DBG_MSG("bailout\n");                                           \
497         cleanup(dinfo);                                                 \
498         INF_MSG("Not going to register framebuffer, exiting...\n");     \
499         return -ENODEV;                                                 \
500 } while (0)
501
502
503 static int __devinit
504 intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
505 {
506         struct fb_info *info;
507         struct intelfb_info *dinfo;
508         int i, err, dvo;
509         int aperture_size, stolen_size;
510         struct agp_kern_info gtt_info;
511         int agp_memtype;
512         const char *s;
513         struct agp_bridge_data *bridge;
514         int aperture_bar = 0;
515         int mmio_bar = 1;
516         int offset;
517
518         DBG_MSG("intelfb_pci_register\n");
519
520         num_registered++;
521         if (num_registered != 1) {
522                 ERR_MSG("Attempted to register %d devices "
523                         "(should be only 1).\n", num_registered);
524                 return -ENODEV;
525         }
526
527         info = framebuffer_alloc(sizeof(struct intelfb_info), &pdev->dev);
528         if (!info) {
529                 ERR_MSG("Could not allocate memory for intelfb_info.\n");
530                 return -ENODEV;
531         }
532         if (fb_alloc_cmap(&info->cmap, 256, 1) < 0) {
533                 ERR_MSG("Could not allocate cmap for intelfb_info.\n");
534                 goto err_out_cmap;
535                 return -ENODEV;
536         }
537
538         dinfo = info->par;
539         dinfo->info  = info;
540         dinfo->fbops = &intel_fb_ops;
541         dinfo->pdev  = pdev;
542
543         /* Reserve pixmap space. */
544         info->pixmap.addr = kmalloc(64 * 1024, GFP_KERNEL);
545         if (info->pixmap.addr == NULL) {
546                 ERR_MSG("Cannot reserve pixmap memory.\n");
547                 goto err_out_pixmap;
548         }
549         memset(info->pixmap.addr, 0, 64 * 1024);
550
551         /* set early this option because it could be changed by tv encoder
552            driver */
553         dinfo->fixed_mode = fixed;
554
555         /* Enable device. */
556         if ((err = pci_enable_device(pdev))) {
557                 ERR_MSG("Cannot enable device.\n");
558                 cleanup(dinfo);
559                 return -ENODEV;
560         }
561
562         /* Set base addresses. */
563         if ((ent->device == PCI_DEVICE_ID_INTEL_915G) ||
564             (ent->device == PCI_DEVICE_ID_INTEL_915GM) ||
565             (ent->device == PCI_DEVICE_ID_INTEL_945G)  ||
566             (ent->device == PCI_DEVICE_ID_INTEL_945GM)) {
567                 aperture_bar = 2;
568                 mmio_bar = 0;
569         }
570         dinfo->aperture.physical = pci_resource_start(pdev, aperture_bar);
571         dinfo->aperture.size     = pci_resource_len(pdev, aperture_bar);
572         dinfo->mmio_base_phys    = pci_resource_start(pdev, mmio_bar);
573         DBG_MSG("fb aperture: 0x%llx/0x%llx, MMIO region: 0x%llx/0x%llx\n",
574                 (unsigned long long)pci_resource_start(pdev, aperture_bar),
575                 (unsigned long long)pci_resource_len(pdev, aperture_bar),
576                 (unsigned long long)pci_resource_start(pdev, mmio_bar),
577                 (unsigned long long)pci_resource_len(pdev, mmio_bar));
578
579         /* Reserve the fb and MMIO regions */
580         if (!request_mem_region(dinfo->aperture.physical, dinfo->aperture.size,
581                                 INTELFB_MODULE_NAME)) {
582                 ERR_MSG("Cannot reserve FB region.\n");
583                 cleanup(dinfo);
584                 return -ENODEV;
585         }
586
587         dinfo->flag |= INTELFB_FB_ACQUIRED;
588
589         if (!request_mem_region(dinfo->mmio_base_phys,
590                                 INTEL_REG_SIZE,
591                                 INTELFB_MODULE_NAME)) {
592                 ERR_MSG("Cannot reserve MMIO region.\n");
593                 cleanup(dinfo);
594                 return -ENODEV;
595         }
596
597         dinfo->flag |= INTELFB_MMIO_ACQUIRED;
598
599         /* Get the chipset info. */
600         dinfo->pci_chipset = pdev->device;
601
602         if (intelfbhw_get_chipset(pdev, dinfo)) {
603                 cleanup(dinfo);
604                 return -ENODEV;
605         }
606
607         if (intelfbhw_get_memory(pdev, &aperture_size,&stolen_size)) {
608                 cleanup(dinfo);
609                 return -ENODEV;
610         }
611
612         INF_MSG("%02x:%02x.%d: %s, aperture size %dMB, "
613                 "stolen memory %dkB\n",
614                 pdev->bus->number, PCI_SLOT(pdev->devfn),
615                 PCI_FUNC(pdev->devfn), dinfo->name,
616                 BtoMB(aperture_size), BtoKB(stolen_size));
617
618         /* Set these from the options. */
619         dinfo->accel    = accel;
620         dinfo->hwcursor = hwcursor;
621
622         if (NOACCEL_CHIPSET(dinfo) && dinfo->accel == 1) {
623                 INF_MSG("Acceleration is not supported for the %s chipset.\n",
624                         dinfo->name);
625                 dinfo->accel = 0;
626         }
627
628         /* Framebuffer parameters - Use all the stolen memory if >= vram */
629         if (ROUND_UP_TO_PAGE(stolen_size) >= MB(vram)) {
630                 dinfo->fb.size = ROUND_UP_TO_PAGE(stolen_size);
631                 dinfo->fbmem_gart = 0;
632         } else {
633                 dinfo->fb.size =  MB(vram);
634                 dinfo->fbmem_gart = 1;
635         }
636
637         /* Allocate space for the ring buffer and HW cursor if enabled. */
638         if (dinfo->accel) {
639                 dinfo->ring.size = RINGBUFFER_SIZE;
640                 dinfo->ring_tail_mask = dinfo->ring.size - 1;
641         }
642         if (dinfo->hwcursor) {
643                 dinfo->cursor.size = HW_CURSOR_SIZE;
644         }
645
646         /* Use agpgart to manage the GATT */
647         if (!(bridge = agp_backend_acquire(pdev))) {
648                 ERR_MSG("cannot acquire agp\n");
649                 cleanup(dinfo);
650                 return -ENODEV;
651         }
652
653         /* get the current gatt info */
654         if (agp_copy_info(bridge, &gtt_info)) {
655                 ERR_MSG("cannot get agp info\n");
656                 agp_backend_release(bridge);
657                 cleanup(dinfo);
658                 return -ENODEV;
659         }
660
661         if (MB(voffset) < stolen_size)
662                 offset = (stolen_size >> 12);
663         else
664                 offset = ROUND_UP_TO_PAGE(MB(voffset))/GTT_PAGE_SIZE;
665
666         /* set the mem offsets - set them after the already used pages */
667         if (dinfo->accel) {
668                 dinfo->ring.offset = offset + gtt_info.current_memory;
669         }
670         if (dinfo->hwcursor) {
671                 dinfo->cursor.offset = offset +
672                         + gtt_info.current_memory + (dinfo->ring.size >> 12);
673         }
674         if (dinfo->fbmem_gart) {
675                 dinfo->fb.offset = offset +
676                         + gtt_info.current_memory + (dinfo->ring.size >> 12)
677                         + (dinfo->cursor.size >> 12);
678         }
679
680         /* Allocate memories (which aren't stolen) */
681         /* Map the fb and MMIO regions */
682         /* ioremap only up to the end of used aperture */
683         dinfo->aperture.virtual = (u8 __iomem *)ioremap_nocache
684                 (dinfo->aperture.physical, ((offset + dinfo->fb.offset) << 12)
685                  + dinfo->fb.size);
686         if (!dinfo->aperture.virtual) {
687                 ERR_MSG("Cannot remap FB region.\n");
688                 cleanup(dinfo);
689                 return -ENODEV;
690         }
691
692         dinfo->mmio_base =
693                 (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
694                                                INTEL_REG_SIZE);
695         if (!dinfo->mmio_base) {
696                 ERR_MSG("Cannot remap MMIO region.\n");
697                 cleanup(dinfo);
698                 return -ENODEV;
699         }
700
701         if (dinfo->accel) {
702                 if (!(dinfo->gtt_ring_mem =
703                       agp_allocate_memory(bridge, dinfo->ring.size >> 12,
704                                           AGP_NORMAL_MEMORY))) {
705                         ERR_MSG("cannot allocate ring buffer memory\n");
706                         agp_backend_release(bridge);
707                         cleanup(dinfo);
708                         return -ENOMEM;
709                 }
710                 if (agp_bind_memory(dinfo->gtt_ring_mem,
711                                     dinfo->ring.offset)) {
712                         ERR_MSG("cannot bind ring buffer memory\n");
713                         agp_backend_release(bridge);
714                         cleanup(dinfo);
715                         return -EBUSY;
716                 }
717                 dinfo->ring.physical = dinfo->aperture.physical
718                         + (dinfo->ring.offset << 12);
719                 dinfo->ring.virtual  = dinfo->aperture.virtual
720                         + (dinfo->ring.offset << 12);
721                 dinfo->ring_head = 0;
722         }
723         if (dinfo->hwcursor) {
724                 agp_memtype = dinfo->mobile ? AGP_PHYSICAL_MEMORY
725                         : AGP_NORMAL_MEMORY;
726                 if (!(dinfo->gtt_cursor_mem =
727                       agp_allocate_memory(bridge, dinfo->cursor.size >> 12,
728                                           agp_memtype))) {
729                         ERR_MSG("cannot allocate cursor memory\n");
730                         agp_backend_release(bridge);
731                         cleanup(dinfo);
732                         return -ENOMEM;
733                 }
734                 if (agp_bind_memory(dinfo->gtt_cursor_mem,
735                                     dinfo->cursor.offset)) {
736                         ERR_MSG("cannot bind cursor memory\n");
737                         agp_backend_release(bridge);
738                         cleanup(dinfo);
739                         return -EBUSY;
740                 }
741                 if (dinfo->mobile)
742                         dinfo->cursor.physical
743                                 = dinfo->gtt_cursor_mem->physical;
744                 else
745                         dinfo->cursor.physical = dinfo->aperture.physical
746                                 + (dinfo->cursor.offset << 12);
747                 dinfo->cursor.virtual = dinfo->aperture.virtual
748                         + (dinfo->cursor.offset << 12);
749         }
750         if (dinfo->fbmem_gart) {
751                 if (!(dinfo->gtt_fb_mem =
752                       agp_allocate_memory(bridge, dinfo->fb.size >> 12,
753                                           AGP_NORMAL_MEMORY))) {
754                         WRN_MSG("cannot allocate framebuffer memory - use "
755                                 "the stolen one\n");
756                         dinfo->fbmem_gart = 0;
757                 }
758                 if (agp_bind_memory(dinfo->gtt_fb_mem,
759                                     dinfo->fb.offset)) {
760                         WRN_MSG("cannot bind framebuffer memory - use "
761                                 "the stolen one\n");
762                         dinfo->fbmem_gart = 0;
763                 }
764         }
765
766         /* update framebuffer memory parameters */
767         if (!dinfo->fbmem_gart)
768                 dinfo->fb.offset = 0;   /* starts at offset 0 */
769         dinfo->fb.physical = dinfo->aperture.physical
770                 + (dinfo->fb.offset << 12);
771         dinfo->fb.virtual = dinfo->aperture.virtual + (dinfo->fb.offset << 12);
772         dinfo->fb_start = dinfo->fb.offset << 12;
773
774         /* release agpgart */
775         agp_backend_release(bridge);
776
777         if (mtrr)
778                 set_mtrr(dinfo);
779
780         DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%p)\n",
781                 dinfo->fb.physical, dinfo->fb.offset, dinfo->fb.size,
782                 dinfo->fb.virtual);
783         DBG_MSG("MMIO: 0x%x/0x%x (0x%p)\n",
784                 dinfo->mmio_base_phys, INTEL_REG_SIZE,
785                 dinfo->mmio_base);
786         DBG_MSG("ring buffer: 0x%x/0x%x (0x%p)\n",
787                 dinfo->ring.physical, dinfo->ring.size,
788                 dinfo->ring.virtual);
789         DBG_MSG("HW cursor: 0x%x/0x%x (0x%p) (offset 0x%x) (phys 0x%x)\n",
790                 dinfo->cursor.physical, dinfo->cursor.size,
791                 dinfo->cursor.virtual, dinfo->cursor.offset,
792                 dinfo->cursor.physical);
793
794         DBG_MSG("options: vram = %d, accel = %d, hwcursor = %d, fixed = %d, "
795                 "noinit = %d\n", vram, accel, hwcursor, fixed, noinit);
796         DBG_MSG("options: mode = \"%s\"\n", mode ? mode : "");
797
798         if (probeonly)
799                 bailout(dinfo);
800
801         /*
802          * Check if the LVDS port or any DVO ports are enabled.  If so,
803          * don't allow mode switching
804          */
805         dvo = intelfbhw_check_non_crt(dinfo);
806         if (dvo) {
807                 dinfo->fixed_mode = 1;
808                 WRN_MSG("Non-CRT device is enabled ( ");
809                 i = 0;
810                 while (dvo) {
811                         if (dvo & 1) {
812                                 s = intelfbhw_dvo_to_string(1 << i);
813                                 if (s)
814                                         printk("%s ", s);
815                         }
816                         dvo >>= 1;
817                         ++i;
818                 }
819                 printk(").  Disabling mode switching.\n");
820         }
821
822         if (bailearly == 1)
823                 bailout(dinfo);
824
825         if (FIXED_MODE(dinfo) && ORIG_VIDEO_ISVGA != VIDEO_TYPE_VLFB) {
826                 ERR_MSG("Video mode must be programmed at boot time.\n");
827                 cleanup(dinfo);
828                 return -ENODEV;
829         }
830
831         if (bailearly == 2)
832                 bailout(dinfo);
833
834         /* Initialise dinfo and related data. */
835         /* If an initial mode was programmed at boot time, get its details. */
836         if (ORIG_VIDEO_ISVGA == VIDEO_TYPE_VLFB)
837                 get_initial_mode(dinfo);
838
839         if (bailearly == 3)
840                 bailout(dinfo);
841
842         if (FIXED_MODE(dinfo)) {
843                 /* remap fb address */
844                 update_dinfo(dinfo, &dinfo->initial_var);
845         }
846
847         if (bailearly == 4)
848                 bailout(dinfo);
849
850
851         if (intelfb_set_fbinfo(dinfo)) {
852                 cleanup(dinfo);
853                 return -ENODEV;
854         }
855
856         if (bailearly == 5)
857                 bailout(dinfo);
858
859 #ifdef CONFIG_FB_INTEL_I2C
860         /* register I2C bus */
861         intelfb_create_i2c_busses(dinfo);
862 #endif
863
864         if (bailearly == 6)
865                 bailout(dinfo);
866
867         pci_set_drvdata(pdev, dinfo);
868
869         /* Save the initial register state. */
870         i = intelfbhw_read_hw_state(dinfo, &dinfo->save_state,
871                                     bailearly > 6 ? bailearly - 6 : 0);
872         if (i != 0) {
873                 DBG_MSG("intelfbhw_read_hw_state returned %d\n", i);
874                 bailout(dinfo);
875         }
876
877         intelfbhw_print_hw_state(dinfo, &dinfo->save_state);
878
879         if (bailearly == 18)
880                 bailout(dinfo);
881
882         /* Cursor initialisation */
883         if (dinfo->hwcursor) {
884                 intelfbhw_cursor_init(dinfo);
885                 intelfbhw_cursor_reset(dinfo);
886         }
887
888         if (bailearly == 19)
889                 bailout(dinfo);
890
891         /* 2d acceleration init */
892         if (dinfo->accel)
893                 intelfbhw_2d_start(dinfo);
894
895         if (bailearly == 20)
896                 bailout(dinfo);
897
898         if (noregister)
899                 bailout(dinfo);
900
901         if (register_framebuffer(dinfo->info) < 0) {
902                 ERR_MSG("Cannot register framebuffer.\n");
903                 cleanup(dinfo);
904                 return -ENODEV;
905         }
906
907         dinfo->registered = 1;
908         dinfo->open = 0;
909
910         init_waitqueue_head(&dinfo->vsync.wait);
911         spin_lock_init(&dinfo->int_lock);
912         dinfo->irq_flags = 0;
913         dinfo->vsync.pan_display = 0;
914         dinfo->vsync.pan_offset = 0;
915
916         return 0;
917
918 err_out_pixmap:
919         fb_dealloc_cmap(&info->cmap);
920 err_out_cmap:
921         framebuffer_release(info);
922         return -ENODEV;
923 }
924
925 static void __devexit
926 intelfb_pci_unregister(struct pci_dev *pdev)
927 {
928         struct intelfb_info *dinfo = pci_get_drvdata(pdev);
929
930         DBG_MSG("intelfb_pci_unregister\n");
931
932         if (!dinfo)
933                 return;
934
935         cleanup(dinfo);
936
937         pci_set_drvdata(pdev, NULL);
938 }
939
940 /***************************************************************
941  *                       helper functions                      *
942  ***************************************************************/
943
944 int __inline__
945 intelfb_var_to_depth(const struct fb_var_screeninfo *var)
946 {
947         DBG_MSG("intelfb_var_to_depth: bpp: %d, green.length is %d\n",
948                 var->bits_per_pixel, var->green.length);
949
950         switch (var->bits_per_pixel) {
951         case 16:
952                 return (var->green.length == 6) ? 16 : 15;
953         case 32:
954                 return 24;
955         default:
956                 return var->bits_per_pixel;
957         }
958 }
959
960
961 static __inline__ int
962 var_to_refresh(const struct fb_var_screeninfo *var)
963 {
964         int xtot = var->xres + var->left_margin + var->right_margin +
965                    var->hsync_len;
966         int ytot = var->yres + var->upper_margin + var->lower_margin +
967                    var->vsync_len;
968
969         return (1000000000 / var->pixclock * 1000 + 500) / xtot / ytot;
970 }
971
972 /***************************************************************
973  *                Various intialisation functions              *
974  ***************************************************************/
975
976 static void __devinit
977 get_initial_mode(struct intelfb_info *dinfo)
978 {
979         struct fb_var_screeninfo *var;
980         int xtot, ytot;
981
982         DBG_MSG("get_initial_mode\n");
983
984         dinfo->initial_vga = 1;
985         dinfo->initial_fb_base = screen_info.lfb_base;
986         dinfo->initial_video_ram = screen_info.lfb_size * KB(64);
987         dinfo->initial_pitch = screen_info.lfb_linelength;
988
989         var = &dinfo->initial_var;
990         memset(var, 0, sizeof(*var));
991         var->xres = screen_info.lfb_width;
992         var->yres = screen_info.lfb_height;
993         var->bits_per_pixel = screen_info.lfb_depth;
994         switch (screen_info.lfb_depth) {
995         case 15:
996                 var->bits_per_pixel = 16;
997                 break;
998         case 24:
999                 var->bits_per_pixel = 32;
1000                 break;
1001         }
1002
1003         DBG_MSG("Initial info: FB is 0x%x/0x%x (%d kByte)\n",
1004                 dinfo->initial_fb_base, dinfo->initial_video_ram,
1005                 BtoKB(dinfo->initial_video_ram));
1006
1007         DBG_MSG("Initial info: mode is %dx%d-%d (%d)\n",
1008                 var->xres, var->yres, var->bits_per_pixel,
1009                 dinfo->initial_pitch);
1010
1011         /* Dummy timing values (assume 60Hz) */
1012         var->left_margin = (var->xres / 8) & 0xf8;
1013         var->right_margin = 32;
1014         var->upper_margin = 16;
1015         var->lower_margin = 4;
1016         var->hsync_len = (var->xres / 8) & 0xf8;
1017         var->vsync_len = 4;
1018
1019         xtot = var->xres + var->left_margin +
1020                 var->right_margin + var->hsync_len;
1021         ytot = var->yres + var->upper_margin +
1022                 var->lower_margin + var->vsync_len;
1023         var->pixclock = 10000000 / xtot * 1000 / ytot * 100 / 60;
1024
1025         var->height = -1;
1026         var->width = -1;
1027
1028         if (var->bits_per_pixel > 8) {
1029                 var->red.offset = screen_info.red_pos;
1030                 var->red.length = screen_info.red_size;
1031                 var->green.offset = screen_info.green_pos;
1032                 var->green.length = screen_info.green_size;
1033                 var->blue.offset = screen_info.blue_pos;
1034                 var->blue.length = screen_info.blue_size;
1035                 var->transp.offset = screen_info.rsvd_pos;
1036                 var->transp.length = screen_info.rsvd_size;
1037         } else {
1038                 var->red.length = 8;
1039                 var->green.length = 8;
1040                 var->blue.length = 8;
1041         }
1042 }
1043
1044 static int __devinit
1045 intelfb_init_var(struct intelfb_info *dinfo)
1046 {
1047         struct fb_var_screeninfo *var;
1048         int msrc = 0;
1049
1050         DBG_MSG("intelfb_init_var\n");
1051
1052         var = &dinfo->info->var;
1053         if (FIXED_MODE(dinfo)) {
1054                 memcpy(var, &dinfo->initial_var,
1055                        sizeof(struct fb_var_screeninfo));
1056                 msrc = 5;
1057         } else {
1058                 const u8 *edid_s = fb_firmware_edid(&dinfo->pdev->dev);
1059                 u8 *edid_d = NULL;
1060
1061                 if (edid_s) {
1062                         edid_d = kmalloc(EDID_LENGTH, GFP_KERNEL);
1063
1064                         if (edid_d) {
1065                                 memcpy(edid_d, edid_s, EDID_LENGTH);
1066                                 fb_edid_to_monspecs(edid_d,
1067                                                     &dinfo->info->monspecs);
1068                                 kfree(edid_d);
1069                         }
1070                 }
1071
1072                 if (mode) {
1073                         printk("intelfb: Looking for mode in private "
1074                                "database\n");
1075                         msrc = fb_find_mode(var, dinfo->info, mode,
1076                                             dinfo->info->monspecs.modedb,
1077                                             dinfo->info->monspecs.modedb_len,
1078                                             NULL, 0);
1079
1080                         if (msrc && msrc > 1) {
1081                                 printk("intelfb: No mode in private database, "
1082                                        "intelfb: looking for mode in global "
1083                                        "database ");
1084                                 msrc = fb_find_mode(var, dinfo->info, mode,
1085                                                     NULL, 0, NULL, 0);
1086
1087                                 if (msrc)
1088                                         msrc |= 8;
1089                         }
1090
1091                 }
1092
1093                 if (!msrc) {
1094                         msrc = fb_find_mode(var, dinfo->info, PREFERRED_MODE,
1095                                             NULL, 0, NULL, 0);
1096                 }
1097         }
1098
1099         if (!msrc) {
1100                 ERR_MSG("Cannot find a suitable video mode.\n");
1101                 return 1;
1102         }
1103
1104         INF_MSG("Initial video mode is %dx%d-%d@%d.\n", var->xres, var->yres,
1105                 var->bits_per_pixel, var_to_refresh(var));
1106
1107         DBG_MSG("Initial video mode is from %d.\n", msrc);
1108
1109 #if ALLOCATE_FOR_PANNING
1110         /* Allow use of half of the video ram for panning */
1111         var->xres_virtual = var->xres;
1112         var->yres_virtual =
1113                 dinfo->fb.size / 2 / (var->bits_per_pixel * var->xres);
1114         if (var->yres_virtual < var->yres)
1115                 var->yres_virtual = var->yres;
1116 #else
1117         var->yres_virtual = var->yres;
1118 #endif
1119
1120         if (dinfo->accel)
1121                 var->accel_flags |= FB_ACCELF_TEXT;
1122         else
1123                 var->accel_flags &= ~FB_ACCELF_TEXT;
1124
1125         return 0;
1126 }
1127
1128 static int __devinit
1129 intelfb_set_fbinfo(struct intelfb_info *dinfo)
1130 {
1131         struct fb_info *info = dinfo->info;
1132
1133         DBG_MSG("intelfb_set_fbinfo\n");
1134
1135         info->flags = FBINFO_FLAG_DEFAULT;
1136         info->fbops = &intel_fb_ops;
1137         info->pseudo_palette = dinfo->pseudo_palette;
1138
1139         info->pixmap.size = 64*1024;
1140         info->pixmap.buf_align = 8;
1141         info->pixmap.access_align = 32;
1142         info->pixmap.flags = FB_PIXMAP_SYSTEM;
1143
1144         if (intelfb_init_var(dinfo))
1145                 return 1;
1146
1147         info->pixmap.scan_align = 1;
1148         strcpy(info->fix.id, dinfo->name);
1149         info->fix.smem_start = dinfo->fb.physical;
1150         info->fix.smem_len = dinfo->fb.size;
1151         info->fix.type = FB_TYPE_PACKED_PIXELS;
1152         info->fix.type_aux = 0;
1153         info->fix.xpanstep = 8;
1154         info->fix.ypanstep = 1;
1155         info->fix.ywrapstep = 0;
1156         info->fix.mmio_start = dinfo->mmio_base_phys;
1157         info->fix.mmio_len = INTEL_REG_SIZE;
1158         info->fix.accel = FB_ACCEL_I830;
1159         update_dinfo(dinfo, &info->var);
1160
1161         return 0;
1162 }
1163
1164 /* Update dinfo to match the active video mode. */
1165 static void
1166 update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
1167 {
1168         DBG_MSG("update_dinfo\n");
1169
1170         dinfo->bpp = var->bits_per_pixel;
1171         dinfo->depth = intelfb_var_to_depth(var);
1172         dinfo->xres = var->xres;
1173         dinfo->yres = var->xres;
1174         dinfo->pixclock = var->pixclock;
1175
1176         dinfo->info->fix.visual = dinfo->visual;
1177         dinfo->info->fix.line_length = dinfo->pitch;
1178
1179         switch (dinfo->bpp) {
1180         case 8:
1181                 dinfo->visual = FB_VISUAL_PSEUDOCOLOR;
1182                 dinfo->pitch = var->xres_virtual;
1183                 break;
1184         case 16:
1185                 dinfo->visual = FB_VISUAL_TRUECOLOR;
1186                 dinfo->pitch = var->xres_virtual * 2;
1187                 break;
1188         case 32:
1189                 dinfo->visual = FB_VISUAL_TRUECOLOR;
1190                 dinfo->pitch = var->xres_virtual * 4;
1191                 break;
1192         }
1193
1194         /* Make sure the line length is a aligned correctly. */
1195         if (IS_I9XX(dinfo))
1196                 dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT_I9XX);
1197         else
1198                 dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
1199
1200         if (FIXED_MODE(dinfo))
1201                 dinfo->pitch = dinfo->initial_pitch;
1202
1203         dinfo->info->screen_base = (char __iomem *)dinfo->fb.virtual;
1204         dinfo->info->fix.line_length = dinfo->pitch;
1205         dinfo->info->fix.visual = dinfo->visual;
1206 }
1207
1208 /* fbops functions */
1209
1210 /***************************************************************
1211  *                       fbdev interface                       *
1212  ***************************************************************/
1213
1214 static int
1215 intelfb_open(struct fb_info *info, int user)
1216 {
1217         struct intelfb_info *dinfo = GET_DINFO(info);
1218
1219         if (user) {
1220                 dinfo->open++;
1221         }
1222
1223         return 0;
1224 }
1225
1226 static int
1227 intelfb_release(struct fb_info *info, int user)
1228 {
1229         struct intelfb_info *dinfo = GET_DINFO(info);
1230
1231         if (user) {
1232                 dinfo->open--;
1233                 msleep(1);
1234                 if (!dinfo->open) {
1235                         intelfbhw_disable_irq(dinfo);
1236                 }
1237         }
1238
1239         return 0;
1240 }
1241
1242 static int
1243 intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1244 {
1245         int change_var = 0;
1246         struct fb_var_screeninfo v;
1247         struct intelfb_info *dinfo;
1248         static int first = 1;
1249         int i;
1250         /* Good pitches to allow tiling.  Don't care about pitches < 1024. */
1251         static const int pitches[] = {
1252                 128 * 8,
1253                 128 * 16,
1254                 128 * 32,
1255                 128 * 64,
1256                 0
1257         };
1258
1259         DBG_MSG("intelfb_check_var: accel_flags is %d\n", var->accel_flags);
1260
1261         dinfo = GET_DINFO(info);
1262
1263         /* update the pitch */
1264         if (intelfbhw_validate_mode(dinfo, var) != 0)
1265                 return -EINVAL;
1266
1267         v = *var;
1268
1269         for (i = 0; pitches[i] != 0; i++) {
1270                 if (pitches[i] >= v.xres_virtual) {
1271                         v.xres_virtual = pitches[i];
1272                         break;
1273                 }
1274         }
1275
1276         /* Check for a supported bpp. */
1277         if (v.bits_per_pixel <= 8) {
1278                 v.bits_per_pixel = 8;
1279         } else if (v.bits_per_pixel <= 16) {
1280                 if (v.bits_per_pixel == 16)
1281                         v.green.length = 6;
1282                 v.bits_per_pixel = 16;
1283         } else if (v.bits_per_pixel <= 32) {
1284                 v.bits_per_pixel = 32;
1285         } else
1286                 return -EINVAL;
1287
1288         change_var = ((info->var.xres != var->xres) ||
1289                       (info->var.yres != var->yres) ||
1290                       (info->var.xres_virtual != var->xres_virtual) ||
1291                       (info->var.yres_virtual != var->yres_virtual) ||
1292                       (info->var.bits_per_pixel != var->bits_per_pixel) ||
1293                       memcmp(&info->var.red, &var->red, sizeof(var->red)) ||
1294                       memcmp(&info->var.green, &var->green,
1295                              sizeof(var->green)) ||
1296                       memcmp(&info->var.blue, &var->blue, sizeof(var->blue)));
1297
1298         if (FIXED_MODE(dinfo) &&
1299             (change_var ||
1300              var->yres_virtual > dinfo->initial_var.yres_virtual ||
1301              var->yres_virtual < dinfo->initial_var.yres ||
1302              var->xoffset || var->nonstd)) {
1303                 if (first) {
1304                         ERR_MSG("Changing the video mode is not supported.\n");
1305                         first = 0;
1306                 }
1307                 return -EINVAL;
1308         }
1309
1310         switch (intelfb_var_to_depth(&v)) {
1311         case 8:
1312                 v.red.offset = v.green.offset = v.blue.offset = 0;
1313                 v.red.length = v.green.length = v.blue.length = 8;
1314                 v.transp.offset = v.transp.length = 0;
1315                 break;
1316         case 15:
1317                 v.red.offset = 10;
1318                 v.green.offset = 5;
1319                 v.blue.offset = 0;
1320                 v.red.length = v.green.length = v.blue.length = 5;
1321                 v.transp.offset = v.transp.length = 0;
1322                 break;
1323         case 16:
1324                 v.red.offset = 11;
1325                 v.green.offset = 5;
1326                 v.blue.offset = 0;
1327                 v.red.length = 5;
1328                 v.green.length = 6;
1329                 v.blue.length = 5;
1330                 v.transp.offset = v.transp.length = 0;
1331                 break;
1332         case 24:
1333                 v.red.offset = 16;
1334                 v.green.offset = 8;
1335                 v.blue.offset = 0;
1336                 v.red.length = v.green.length = v.blue.length = 8;
1337                 v.transp.offset = v.transp.length = 0;
1338                 break;
1339         case 32:
1340                 v.red.offset = 16;
1341                 v.green.offset = 8;
1342                 v.blue.offset = 0;
1343                 v.red.length = v.green.length = v.blue.length = 8;
1344                 v.transp.offset = 24;
1345                 v.transp.length = 8;
1346                 break;
1347         }
1348
1349         if (v.xoffset < 0)
1350                 v.xoffset = 0;
1351         if (v.yoffset < 0)
1352                 v.yoffset = 0;
1353
1354         if (v.xoffset > v.xres_virtual - v.xres)
1355                 v.xoffset = v.xres_virtual - v.xres;
1356         if (v.yoffset > v.yres_virtual - v.yres)
1357                 v.yoffset = v.yres_virtual - v.yres;
1358
1359         v.red.msb_right = v.green.msb_right = v.blue.msb_right =
1360                           v.transp.msb_right = 0;
1361
1362         *var = v;
1363
1364         return 0;
1365 }
1366
1367 static int
1368 intelfb_set_par(struct fb_info *info)
1369 {
1370         struct intelfb_hwstate *hw;
1371         struct intelfb_info *dinfo = GET_DINFO(info);
1372
1373         if (FIXED_MODE(dinfo)) {
1374                 ERR_MSG("Changing the video mode is not supported.\n");
1375                 return -EINVAL;
1376         }
1377
1378         hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
1379         if (!hw)
1380                 return -ENOMEM;
1381
1382         DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
1383                 info->var.yres, info->var.bits_per_pixel);
1384
1385         intelfb_blank(FB_BLANK_POWERDOWN, info);
1386
1387         if (ACCEL(dinfo, info))
1388                 intelfbhw_2d_stop(dinfo);
1389
1390         memcpy(hw, &dinfo->save_state, sizeof(*hw));
1391         if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
1392                 goto invalid_mode;
1393         if (intelfbhw_program_mode(dinfo, hw, 0))
1394                 goto invalid_mode;
1395
1396 #if REGDUMP > 0
1397         intelfbhw_read_hw_state(dinfo, hw, 0);
1398         intelfbhw_print_hw_state(dinfo, hw);
1399 #endif
1400
1401         update_dinfo(dinfo, &info->var);
1402
1403         if (ACCEL(dinfo, info))
1404                 intelfbhw_2d_start(dinfo);
1405
1406         intelfb_pan_display(&info->var, info);
1407
1408         intelfb_blank(FB_BLANK_UNBLANK, info);
1409
1410         if (ACCEL(dinfo, info)) {
1411                 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
1412                 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
1413                 FBINFO_HWACCEL_IMAGEBLIT;
1414         } else {
1415                 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1416         }
1417         kfree(hw);
1418         return 0;
1419 invalid_mode:
1420         kfree(hw);
1421         return -EINVAL;
1422 }
1423
1424 static int
1425 intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1426                   unsigned blue, unsigned transp, struct fb_info *info)
1427 {
1428         struct intelfb_info *dinfo = GET_DINFO(info);
1429
1430 #if VERBOSE > 0
1431         DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
1432 #endif
1433
1434         if (regno > 255)
1435                 return 1;
1436
1437         if (dinfo->depth == 8) {
1438                 red >>= 8;
1439                 green >>= 8;
1440                 blue >>= 8;
1441
1442                 intelfbhw_setcolreg(dinfo, regno, red, green, blue,
1443                                     transp);
1444         }
1445
1446         if (regno < 16) {
1447                 switch (dinfo->depth) {
1448                 case 15:
1449                         dinfo->pseudo_palette[regno] = ((red & 0xf800) >>  1) |
1450                                 ((green & 0xf800) >>  6) |
1451                                 ((blue & 0xf800) >> 11);
1452                         break;
1453                 case 16:
1454                         dinfo->pseudo_palette[regno] = (red & 0xf800) |
1455                                 ((green & 0xfc00) >>  5) |
1456                                 ((blue  & 0xf800) >> 11);
1457                         break;
1458                 case 24:
1459                         dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
1460                                 (green & 0xff00) |
1461                                 ((blue  & 0xff00) >> 8);
1462                         break;
1463                 }
1464         }
1465
1466         return 0;
1467 }
1468
1469 static int
1470 intelfb_blank(int blank, struct fb_info *info)
1471 {
1472         intelfbhw_do_blank(blank, info);
1473         return 0;
1474 }
1475
1476 static int
1477 intelfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1478 {
1479         intelfbhw_pan_display(var, info);
1480         return 0;
1481 }
1482
1483 /* When/if we have our own ioctls. */
1484 static int
1485 intelfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
1486 {
1487         int retval = 0;
1488         struct intelfb_info *dinfo = GET_DINFO(info);
1489         u32 pipe = 0;
1490
1491         switch (cmd) {
1492                 case FBIO_WAITFORVSYNC:
1493                         if (get_user(pipe, (__u32 __user *)arg))
1494                                 return -EFAULT;
1495
1496                         retval = intelfbhw_wait_for_vsync(dinfo, pipe);
1497                         break;
1498                 default:
1499                         break;
1500         }
1501
1502         return retval;
1503 }
1504
1505 static void
1506 intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
1507 {
1508         struct intelfb_info *dinfo = GET_DINFO(info);
1509         u32 rop, color;
1510
1511 #if VERBOSE > 0
1512         DBG_MSG("intelfb_fillrect\n");
1513 #endif
1514
1515         if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1516                 return cfb_fillrect(info, rect);
1517
1518         if (rect->rop == ROP_COPY)
1519                 rop = PAT_ROP_GXCOPY;
1520         else // ROP_XOR
1521                 rop = PAT_ROP_GXXOR;
1522
1523         if (dinfo->depth != 8)
1524                 color = dinfo->pseudo_palette[rect->color];
1525         else
1526                 color = rect->color;
1527
1528         intelfbhw_do_fillrect(dinfo, rect->dx, rect->dy,
1529                               rect->width, rect->height, color,
1530                               dinfo->pitch, info->var.bits_per_pixel,
1531                               rop);
1532 }
1533
1534 static void
1535 intelfb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1536 {
1537         struct intelfb_info *dinfo = GET_DINFO(info);
1538
1539 #if VERBOSE > 0
1540         DBG_MSG("intelfb_copyarea\n");
1541 #endif
1542
1543         if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1544                 return cfb_copyarea(info, region);
1545
1546         intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
1547                             region->dy, region->width, region->height,
1548                             dinfo->pitch, info->var.bits_per_pixel);
1549 }
1550
1551 static void
1552 intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
1553 {
1554         struct intelfb_info *dinfo = GET_DINFO(info);
1555         u32 fgcolor, bgcolor;
1556
1557 #if VERBOSE > 0
1558         DBG_MSG("intelfb_imageblit\n");
1559 #endif
1560
1561         if (!ACCEL(dinfo, info) || dinfo->depth == 4
1562             || image->depth != 1)
1563                 return cfb_imageblit(info, image);
1564
1565         if (dinfo->depth != 8) {
1566                 fgcolor = dinfo->pseudo_palette[image->fg_color];
1567                 bgcolor = dinfo->pseudo_palette[image->bg_color];
1568         } else {
1569                 fgcolor = image->fg_color;
1570                 bgcolor = image->bg_color;
1571         }
1572
1573         if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
1574                                     image->height, image->data,
1575                                     image->dx, image->dy,
1576                                     dinfo->pitch, info->var.bits_per_pixel))
1577                 return cfb_imageblit(info, image);
1578 }
1579
1580 static int
1581 intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1582 {
1583         struct intelfb_info *dinfo = GET_DINFO(info);
1584         u32 physical;
1585 #if VERBOSE > 0
1586         DBG_MSG("intelfb_cursor\n");
1587 #endif
1588
1589         if (!dinfo->hwcursor)
1590                 return -ENODEV;
1591
1592         intelfbhw_cursor_hide(dinfo);
1593
1594         /* If XFree killed the cursor - restore it */
1595         physical = (dinfo->mobile || IS_I9XX(dinfo)) ? dinfo->cursor.physical :
1596                    (dinfo->cursor.offset << 12);
1597
1598         if (INREG(CURSOR_A_BASEADDR) != physical) {
1599                 u32 fg, bg;
1600
1601                 DBG_MSG("the cursor was killed - restore it !!\n");
1602                 DBG_MSG("size %d, %d   pos %d, %d\n",
1603                         cursor->image.width, cursor->image.height,
1604                         cursor->image.dx, cursor->image.dy);
1605
1606                 intelfbhw_cursor_init(dinfo);
1607                 intelfbhw_cursor_reset(dinfo);
1608                 intelfbhw_cursor_setpos(dinfo, cursor->image.dx,
1609                                         cursor->image.dy);
1610
1611                 if (dinfo->depth != 8) {
1612                         fg =dinfo->pseudo_palette[cursor->image.fg_color];
1613                         bg =dinfo->pseudo_palette[cursor->image.bg_color];
1614                 } else {
1615                         fg = cursor->image.fg_color;
1616                         bg = cursor->image.bg_color;
1617                 }
1618                 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1619                 intelfbhw_cursor_load(dinfo, cursor->image.width,
1620                                       cursor->image.height,
1621                                       dinfo->cursor_src);
1622
1623                 if (cursor->enable)
1624                         intelfbhw_cursor_show(dinfo);
1625                 return 0;
1626         }
1627
1628         if (cursor->set & FB_CUR_SETPOS) {
1629                 u32 dx, dy;
1630
1631                 dx = cursor->image.dx - info->var.xoffset;
1632                 dy = cursor->image.dy - info->var.yoffset;
1633
1634                 intelfbhw_cursor_setpos(dinfo, dx, dy);
1635         }
1636
1637         if (cursor->set & FB_CUR_SETSIZE) {
1638                 if (cursor->image.width > 64 || cursor->image.height > 64)
1639                         return -ENXIO;
1640
1641                 intelfbhw_cursor_reset(dinfo);
1642         }
1643
1644         if (cursor->set & FB_CUR_SETCMAP) {
1645                 u32 fg, bg;
1646
1647                 if (dinfo->depth != 8) {
1648                         fg = dinfo->pseudo_palette[cursor->image.fg_color];
1649                         bg = dinfo->pseudo_palette[cursor->image.bg_color];
1650                 } else {
1651                         fg = cursor->image.fg_color;
1652                         bg = cursor->image.bg_color;
1653                 }
1654
1655                 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1656         }
1657
1658         if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1659                 u32 s_pitch = (ROUND_UP_TO(cursor->image.width, 8) / 8);
1660                 u32 size = s_pitch * cursor->image.height;
1661                 u8 *dat = (u8 *) cursor->image.data;
1662                 u8 *msk = (u8 *) cursor->mask;
1663                 u8 src[64];
1664                 u32 i;
1665
1666                 if (cursor->image.depth != 1)
1667                         return -ENXIO;
1668
1669                 switch (cursor->rop) {
1670                 case ROP_XOR:
1671                         for (i = 0; i < size; i++)
1672                                 src[i] = dat[i] ^ msk[i];
1673                         break;
1674                 case ROP_COPY:
1675                 default:
1676                         for (i = 0; i < size; i++)
1677                                 src[i] = dat[i] & msk[i];
1678                         break;
1679                 }
1680
1681                 /* save the bitmap to restore it when XFree will
1682                    make the cursor dirty */
1683                 memcpy(dinfo->cursor_src, src, size);
1684
1685                 intelfbhw_cursor_load(dinfo, cursor->image.width,
1686                                       cursor->image.height, src);
1687         }
1688
1689         if (cursor->enable)
1690                 intelfbhw_cursor_show(dinfo);
1691
1692         return 0;
1693 }
1694
1695 static int
1696 intelfb_sync(struct fb_info *info)
1697 {
1698         struct intelfb_info *dinfo = GET_DINFO(info);
1699
1700 #if VERBOSE > 0
1701         DBG_MSG("intelfb_sync\n");
1702 #endif
1703
1704         if (dinfo->ring_lockup)
1705                 return 0;
1706
1707         intelfbhw_do_sync(dinfo);
1708         return 0;
1709 }
1710