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