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