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