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