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