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