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