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
131static void get_initial_mode(struct intelfb_info *dinfo);
132static void update_dinfo(struct intelfb_info *dinfo,
133			 struct fb_var_screeninfo *var);
134static int intelfb_open(struct fb_info *info, int user);
135static int intelfb_release(struct fb_info *info, int user);
136static int intelfb_check_var(struct fb_var_screeninfo *var,
137			     struct fb_info *info);
138static int intelfb_set_par(struct fb_info *info);
139static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
140			     unsigned blue, unsigned transp,
141			     struct fb_info *info);
142
143static int intelfb_blank(int blank, struct fb_info *info);
144static int intelfb_pan_display(struct fb_var_screeninfo *var,
145			       struct fb_info *info);
146
147static void intelfb_fillrect(struct fb_info *info,
148			     const struct fb_fillrect *rect);
149static void intelfb_copyarea(struct fb_info *info,
150			     const struct fb_copyarea *region);
151static void intelfb_imageblit(struct fb_info *info,
152			      const struct fb_image *image);
153static int intelfb_cursor(struct fb_info *info,
154			   struct fb_cursor *cursor);
155
156static int intelfb_sync(struct fb_info *info);
157
158static int intelfb_ioctl(struct fb_info *info,
159			 unsigned int cmd, unsigned long arg);
160
161static int intelfb_pci_register(struct pci_dev *pdev,
162				const struct pci_device_id *ent);
163static void intelfb_pci_unregister(struct pci_dev *pdev);
164static 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
176static 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 */
193static int num_registered = 0;
194
195/* fb ops */
196static 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 */
214static 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 */
222MODULE_AUTHOR("David Dawes <dawes@tungstengraphics.com>, "
223	      "Sylvain Meyer <sylvain.meyer@worldonline.fr>");
224MODULE_DESCRIPTION("Framebuffer driver for Intel(R) " SUPPORTED_CHIPSETS
225		   " chipsets");
226MODULE_LICENSE("Dual BSD/GPL");
227MODULE_DEVICE_TABLE(pci, intelfb_pci_table);
228
229static bool accel       = 1;
230static int vram         = 4;
231static bool hwcursor    = 0;
232static bool mtrr        = 1;
233static bool fixed       = 0;
234static bool noinit      = 0;
235static bool noregister  = 0;
236static bool probeonly   = 0;
237static bool idonly      = 0;
238static int bailearly    = 0;
239static int voffset	= 48;
240static char *mode       = NULL;
241
242module_param(accel, bool, S_IRUGO);
243MODULE_PARM_DESC(accel, "Enable hardware acceleration");
244module_param(vram, int, S_IRUGO);
245MODULE_PARM_DESC(vram, "System RAM to allocate to framebuffer in MiB");
246module_param(voffset, int, S_IRUGO);
247MODULE_PARM_DESC(voffset, "Offset of framebuffer in MiB");
248module_param(hwcursor, bool, S_IRUGO);
249MODULE_PARM_DESC(hwcursor, "Enable HW cursor");
250module_param(mtrr, bool, S_IRUGO);
251MODULE_PARM_DESC(mtrr, "Enable MTRR support");
252module_param(fixed, bool, S_IRUGO);
253MODULE_PARM_DESC(fixed, "Disable mode switching");
254module_param(noinit, bool, 0);
255MODULE_PARM_DESC(noinit, "Don't initialise graphics mode when loading");
256module_param(noregister, bool, 0);
257MODULE_PARM_DESC(noregister, "Don't register, just probe and exit (debug)");
258module_param(probeonly, bool, 0);
259MODULE_PARM_DESC(probeonly, "Do a minimal probe (debug)");
260module_param(idonly, bool, 0);
261MODULE_PARM_DESC(idonly, "Just identify without doing anything else (debug)");
262module_param(bailearly, int, 0);
263MODULE_PARM_DESC(bailearly, "Bail out early, depending on value (debug)");
264module_param(mode, charp, S_IRUGO);
265MODULE_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
273static __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
291static __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
304static __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
325static 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
376static 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
400static void __exit intelfb_exit(void)
401{
402	DBG_MSG("intelfb_exit\n");
403	pci_unregister_driver(&intelfb_driver);
404}
405
406module_init(intelfb_init);
407module_exit(intelfb_exit);
408
409/***************************************************************
410 *                        driver init / cleanup                *
411 ***************************************************************/
412
413static 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
469static 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;
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
886err_out_pixmap:
887	fb_dealloc_cmap(&info->cmap);
888err_out_cmap:
889	framebuffer_release(info);
890	return -ENODEV;
891}
892
893static 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
925static __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
939static 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
1006static 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
1087static 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. */
1123static 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
1172static 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
1182static 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
1196static 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	if (!var->pixclock)
1218		return -EINVAL;
1219
1220	/* update the pitch */
1221	if (intelfbhw_validate_mode(dinfo, var) != 0)
1222		return -EINVAL;
1223
1224	v = *var;
1225
1226	for (i = 0; pitches[i] != 0; i++) {
1227		if (pitches[i] >= v.xres_virtual) {
1228			v.xres_virtual = pitches[i];
1229			break;
1230		}
1231	}
1232
1233	/* Check for a supported bpp. */
1234	if (v.bits_per_pixel <= 8)
1235		v.bits_per_pixel = 8;
1236	else if (v.bits_per_pixel <= 16) {
1237		if (v.bits_per_pixel == 16)
1238			v.green.length = 6;
1239		v.bits_per_pixel = 16;
1240	} else if (v.bits_per_pixel <= 32)
1241		v.bits_per_pixel = 32;
1242	else
1243		return -EINVAL;
1244
1245	change_var = ((info->var.xres != var->xres) ||
1246		      (info->var.yres != var->yres) ||
1247		      (info->var.xres_virtual != var->xres_virtual) ||
1248		      (info->var.yres_virtual != var->yres_virtual) ||
1249		      (info->var.bits_per_pixel != var->bits_per_pixel) ||
1250		      memcmp(&info->var.red, &var->red, sizeof(var->red)) ||
1251		      memcmp(&info->var.green, &var->green,
1252			     sizeof(var->green)) ||
1253		      memcmp(&info->var.blue, &var->blue, sizeof(var->blue)));
1254
1255	if (FIXED_MODE(dinfo) &&
1256	    (change_var ||
1257	     var->yres_virtual > dinfo->initial_var.yres_virtual ||
1258	     var->yres_virtual < dinfo->initial_var.yres ||
1259	     var->xoffset || var->nonstd)) {
1260		if (first) {
1261			ERR_MSG("Changing the video mode is not supported.\n");
1262			first = 0;
1263		}
1264		return -EINVAL;
1265	}
1266
1267	switch (intelfb_var_to_depth(&v)) {
1268	case 8:
1269		v.red.offset = v.green.offset = v.blue.offset = 0;
1270		v.red.length = v.green.length = v.blue.length = 8;
1271		v.transp.offset = v.transp.length = 0;
1272		break;
1273	case 15:
1274		v.red.offset = 10;
1275		v.green.offset = 5;
1276		v.blue.offset = 0;
1277		v.red.length = v.green.length = v.blue.length = 5;
1278		v.transp.offset = v.transp.length = 0;
1279		break;
1280	case 16:
1281		v.red.offset = 11;
1282		v.green.offset = 5;
1283		v.blue.offset = 0;
1284		v.red.length = 5;
1285		v.green.length = 6;
1286		v.blue.length = 5;
1287		v.transp.offset = v.transp.length = 0;
1288		break;
1289	case 24:
1290		v.red.offset = 16;
1291		v.green.offset = 8;
1292		v.blue.offset = 0;
1293		v.red.length = v.green.length = v.blue.length = 8;
1294		v.transp.offset = v.transp.length = 0;
1295		break;
1296	case 32:
1297		v.red.offset = 16;
1298		v.green.offset = 8;
1299		v.blue.offset = 0;
1300		v.red.length = v.green.length = v.blue.length = 8;
1301		v.transp.offset = 24;
1302		v.transp.length = 8;
1303		break;
1304	}
1305
1306	if (v.xoffset > v.xres_virtual - v.xres)
1307		v.xoffset = v.xres_virtual - v.xres;
1308	if (v.yoffset > v.yres_virtual - v.yres)
1309		v.yoffset = v.yres_virtual - v.yres;
1310
1311	v.red.msb_right = v.green.msb_right = v.blue.msb_right =
1312			  v.transp.msb_right = 0;
1313
1314        *var = v;
1315
1316	return 0;
1317}
1318
1319static int intelfb_set_par(struct fb_info *info)
1320{
1321	struct intelfb_hwstate *hw;
1322        struct intelfb_info *dinfo = GET_DINFO(info);
1323
1324	if (FIXED_MODE(dinfo)) {
1325		ERR_MSG("Changing the video mode is not supported.\n");
1326		return -EINVAL;
1327	}
1328
1329	hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
1330	if (!hw)
1331		return -ENOMEM;
1332
1333	DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
1334		info->var.yres, info->var.bits_per_pixel);
1335
1336	/*
1337	 * Disable VCO prior to timing register change.
1338	 */
1339	OUTREG(DPLL_A, INREG(DPLL_A) & ~DPLL_VCO_ENABLE);
1340
1341	intelfb_blank(FB_BLANK_POWERDOWN, info);
1342
1343	if (ACCEL(dinfo, info))
1344		intelfbhw_2d_stop(dinfo);
1345
1346	memcpy(hw, &dinfo->save_state, sizeof(*hw));
1347	if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
1348		goto invalid_mode;
1349	if (intelfbhw_program_mode(dinfo, hw, 0))
1350		goto invalid_mode;
1351
1352#if REGDUMP > 0
1353	intelfbhw_read_hw_state(dinfo, hw, 0);
1354	intelfbhw_print_hw_state(dinfo, hw);
1355#endif
1356
1357	update_dinfo(dinfo, &info->var);
1358
1359	if (ACCEL(dinfo, info))
1360		intelfbhw_2d_start(dinfo);
1361
1362	intelfb_pan_display(&info->var, info);
1363
1364	intelfb_blank(FB_BLANK_UNBLANK, info);
1365
1366	if (ACCEL(dinfo, info)) {
1367		info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
1368		FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
1369		FBINFO_HWACCEL_IMAGEBLIT;
1370	} else
1371		info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1372
1373	kfree(hw);
1374	return 0;
1375invalid_mode:
1376	kfree(hw);
1377	return -EINVAL;
1378}
1379
1380static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1381			     unsigned blue, unsigned transp,
1382			     struct fb_info *info)
1383{
1384	struct intelfb_info *dinfo = GET_DINFO(info);
1385
1386#if VERBOSE > 0
1387	DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
1388#endif
1389
1390	if (regno > 255)
1391		return 1;
1392
1393	if (dinfo->depth == 8) {
1394		red >>= 8;
1395		green >>= 8;
1396		blue >>= 8;
1397
1398		intelfbhw_setcolreg(dinfo, regno, red, green, blue,
1399				    transp);
1400	}
1401
1402	if (regno < 16) {
1403		switch (dinfo->depth) {
1404		case 15:
1405			dinfo->pseudo_palette[regno] = ((red & 0xf800) >>  1) |
1406				((green & 0xf800) >>  6) |
1407				((blue & 0xf800) >> 11);
1408			break;
1409		case 16:
1410			dinfo->pseudo_palette[regno] = (red & 0xf800) |
1411				((green & 0xfc00) >>  5) |
1412				((blue  & 0xf800) >> 11);
1413			break;
1414		case 24:
1415			dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
1416				(green & 0xff00) |
1417				((blue  & 0xff00) >> 8);
1418			break;
1419		}
1420	}
1421
1422	return 0;
1423}
1424
1425static int intelfb_blank(int blank, struct fb_info *info)
1426{
1427	intelfbhw_do_blank(blank, info);
1428	return 0;
1429}
1430
1431static int intelfb_pan_display(struct fb_var_screeninfo *var,
1432			       struct fb_info *info)
1433{
1434	intelfbhw_pan_display(var, info);
1435	return 0;
1436}
1437
1438/* When/if we have our own ioctls. */
1439static int intelfb_ioctl(struct fb_info *info, unsigned int cmd,
1440			 unsigned long arg)
1441{
1442	int retval = 0;
1443	struct intelfb_info *dinfo = GET_DINFO(info);
1444	u32 pipe = 0;
1445
1446	switch (cmd) {
1447		case FBIO_WAITFORVSYNC:
1448			if (get_user(pipe, (__u32 __user *)arg))
1449				return -EFAULT;
1450
1451			retval = intelfbhw_wait_for_vsync(dinfo, pipe);
1452			break;
1453		default:
1454			break;
1455	}
1456
1457	return retval;
1458}
1459
1460static void intelfb_fillrect (struct fb_info *info,
1461			      const struct fb_fillrect *rect)
1462{
1463        struct intelfb_info *dinfo = GET_DINFO(info);
1464	u32 rop, color;
1465
1466#if VERBOSE > 0
1467	DBG_MSG("intelfb_fillrect\n");
1468#endif
1469
1470	if (!ACCEL(dinfo, info) || dinfo->depth == 4) {
1471		cfb_fillrect(info, rect);
1472		return;
1473	}
1474
1475	if (rect->rop == ROP_COPY)
1476		rop = PAT_ROP_GXCOPY;
1477	else /* ROP_XOR */
1478		rop = PAT_ROP_GXXOR;
1479
1480	if (dinfo->depth != 8)
1481		color = dinfo->pseudo_palette[rect->color];
1482	else
1483		color = rect->color;
1484
1485	intelfbhw_do_fillrect(dinfo, rect->dx, rect->dy,
1486			      rect->width, rect->height, color,
1487			      dinfo->pitch, info->var.bits_per_pixel,
1488			      rop);
1489}
1490
1491static void intelfb_copyarea(struct fb_info *info,
1492			     const struct fb_copyarea *region)
1493{
1494        struct intelfb_info *dinfo = GET_DINFO(info);
1495
1496#if VERBOSE > 0
1497	DBG_MSG("intelfb_copyarea\n");
1498#endif
1499
1500	if (!ACCEL(dinfo, info) || dinfo->depth == 4) {
1501		cfb_copyarea(info, region);
1502		return;
1503	}
1504
1505	intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
1506			    region->dy, region->width, region->height,
1507			    dinfo->pitch, info->var.bits_per_pixel);
1508}
1509
1510static void intelfb_imageblit(struct fb_info *info,
1511			      const struct fb_image *image)
1512{
1513        struct intelfb_info *dinfo = GET_DINFO(info);
1514	u32 fgcolor, bgcolor;
1515
1516#if VERBOSE > 0
1517	DBG_MSG("intelfb_imageblit\n");
1518#endif
1519
1520	if (!ACCEL(dinfo, info) || dinfo->depth == 4
1521	    || image->depth != 1) {
1522		cfb_imageblit(info, image);
1523		return;
1524	}
1525
1526	if (dinfo->depth != 8) {
1527		fgcolor = dinfo->pseudo_palette[image->fg_color];
1528		bgcolor = dinfo->pseudo_palette[image->bg_color];
1529	} else {
1530		fgcolor = image->fg_color;
1531		bgcolor = image->bg_color;
1532	}
1533
1534	if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
1535				    image->height, image->data,
1536				    image->dx, image->dy,
1537				    dinfo->pitch, info->var.bits_per_pixel)) {
1538		cfb_imageblit(info, image);
1539		return;
1540	}
1541}
1542
1543static int intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1544{
1545        struct intelfb_info *dinfo = GET_DINFO(info);
1546	u32 physical;
1547#if VERBOSE > 0
1548	DBG_MSG("intelfb_cursor\n");
1549#endif
1550
1551	if (!dinfo->hwcursor)
1552		return -ENODEV;
1553
1554	intelfbhw_cursor_hide(dinfo);
1555
1556	/* If XFree killed the cursor - restore it */
1557	physical = (dinfo->mobile || IS_I9XX(dinfo)) ? dinfo->cursor.physical :
1558		   (dinfo->cursor.offset << 12);
1559
1560	if (INREG(CURSOR_A_BASEADDR) != physical) {
1561		u32 fg, bg;
1562
1563		DBG_MSG("the cursor was killed - restore it !!\n");
1564		DBG_MSG("size %d, %d   pos %d, %d\n",
1565			cursor->image.width, cursor->image.height,
1566			cursor->image.dx, cursor->image.dy);
1567
1568		intelfbhw_cursor_init(dinfo);
1569		intelfbhw_cursor_reset(dinfo);
1570		intelfbhw_cursor_setpos(dinfo, cursor->image.dx,
1571					cursor->image.dy);
1572
1573		if (dinfo->depth != 8) {
1574			fg =dinfo->pseudo_palette[cursor->image.fg_color];
1575			bg =dinfo->pseudo_palette[cursor->image.bg_color];
1576		} else {
1577			fg = cursor->image.fg_color;
1578			bg = cursor->image.bg_color;
1579		}
1580		intelfbhw_cursor_setcolor(dinfo, bg, fg);
1581		intelfbhw_cursor_load(dinfo, cursor->image.width,
1582				      cursor->image.height,
1583				      dinfo->cursor_src);
1584
1585		if (cursor->enable)
1586			intelfbhw_cursor_show(dinfo);
1587		return 0;
1588	}
1589
1590	if (cursor->set & FB_CUR_SETPOS) {
1591		u32 dx, dy;
1592
1593		dx = cursor->image.dx - info->var.xoffset;
1594		dy = cursor->image.dy - info->var.yoffset;
1595
1596		intelfbhw_cursor_setpos(dinfo, dx, dy);
1597	}
1598
1599	if (cursor->set & FB_CUR_SETSIZE) {
1600		if (cursor->image.width > 64 || cursor->image.height > 64)
1601			return -ENXIO;
1602
1603		intelfbhw_cursor_reset(dinfo);
1604	}
1605
1606	if (cursor->set & FB_CUR_SETCMAP) {
1607		u32 fg, bg;
1608
1609		if (dinfo->depth != 8) {
1610			fg = dinfo->pseudo_palette[cursor->image.fg_color];
1611			bg = dinfo->pseudo_palette[cursor->image.bg_color];
1612		} else {
1613			fg = cursor->image.fg_color;
1614			bg = cursor->image.bg_color;
1615		}
1616
1617		intelfbhw_cursor_setcolor(dinfo, bg, fg);
1618	}
1619
1620	if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1621		u32 s_pitch = (ROUND_UP_TO(cursor->image.width, 8) / 8);
1622		u32 size = s_pitch * cursor->image.height;
1623		u8 *dat = (u8 *) cursor->image.data;
1624		u8 *msk = (u8 *) cursor->mask;
1625		u8 src[64];
1626		u32 i;
1627
1628		if (cursor->image.depth != 1)
1629			return -ENXIO;
1630
1631		switch (cursor->rop) {
1632		case ROP_XOR:
1633			for (i = 0; i < size; i++)
1634				src[i] = dat[i] ^ msk[i];
1635			break;
1636		case ROP_COPY:
1637		default:
1638			for (i = 0; i < size; i++)
1639				src[i] = dat[i] & msk[i];
1640			break;
1641		}
1642
1643		/* save the bitmap to restore it when XFree will
1644		   make the cursor dirty */
1645		memcpy(dinfo->cursor_src, src, size);
1646
1647		intelfbhw_cursor_load(dinfo, cursor->image.width,
1648				      cursor->image.height, src);
1649	}
1650
1651	if (cursor->enable)
1652		intelfbhw_cursor_show(dinfo);
1653
1654	return 0;
1655}
1656
1657static int intelfb_sync(struct fb_info *info)
1658{
1659        struct intelfb_info *dinfo = GET_DINFO(info);
1660
1661#if VERBOSE > 0
1662	DBG_MSG("intelfb_sync\n");
1663#endif
1664
1665	if (dinfo->ring_lockup)
1666		return 0;
1667
1668	intelfbhw_do_sync(dinfo);
1669	return 0;
1670}
1671
1672