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