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