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