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