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