1 /*
2  * intelfb
3  *
4  * Linux framebuffer driver for Intel(R) 830M/845G/852GM/855GM/865G
5  * integrated graphics chips.
6  *
7  * Copyright (C) 2002, 2003 David Dawes <dawes@tungstengraphics.com>
8  *
9  * This driver consists of two parts.  The first part (intelfbdrv.c) provides
10  * the basic fbdev interfaces, is derived in part from the radeonfb and
11  * vesafb drivers, and is covered by the GPL.  The second part (intelfbhw.c)
12  * provides the code to program the hardware.  Most of it is derived from
13  * the i810/i830 XFree86 driver.  The HW-specific code is covered here
14  * under a dual license (GPL and MIT/XFree86 license).
15  *
16  * Author: David Dawes
17  *
18  */
19 
20 /* $DHD: intelfb/intelfbdrv.c,v 1.15 2003/02/06 17:50:08 dawes Exp $ */
21 /* $TG$ */
22 
23 /*
24  * Changes:
25  *    01/2003 - Initial driver (0.1.0), no mode switching, no acceleration.
26  *		This initial version is a basic core that works a lot like
27  *		the vesafb driver.  It must be built-in to the kernel,
28  *		and the initial video mode must be set with vga=XXX at
29  *		boot time.  (David Dawes)
30  *
31  *    01/2003 - Version 0.2.0: Mode switching added, colormap support
32  *		implemented, Y panning, and soft screen blanking implemented.
33  *		No acceleration yet.  (David Dawes)
34  *
35  *    01/2003 - Version 0.3.0: fbcon acceleration support added.  Module
36  *		option handling added.  (David Dawes)
37  *
38  *    01/2003 - Version 0.4.0: fbcon HW cursor support added.  (David Dawes)
39  *
40  *    01/2003 - Version 0.4.1: Add auto-generation of built-in modes.
41  *		(David Dawes)
42  *
43  *    02/2003 - Version 0.4.2: Add check for active non-CRT devices, and
44  *		mode validation checks.  (David Dawes)
45  *
46  *    02/2003 - Version 0.4.3: Check when the VC is in graphics mode so that
47  *		acceleration is disabled while an XFree86 server is running.
48  *		(David Dawes)
49  *
50  *    02/2003 - Version 0.4.4: Monitor DPMS support.  (David Dawes)
51  *
52  *    02/2003 - Version 0.4.5: Basic XFree86 + fbdev working.  (David Dawes)
53  *
54  *    02/2003 - Version 0.5.0: Modify to work with the 2.5.32 kernel as well
55  *		as 2.4.x kernels.  (David Dawes)
56  *
57  *    02/2003 - Version 0.6.0: Split out HW-specifics into a separate file.
58  *		(David Dawes)
59  *
60  *    02/2003 - Version 0.7.0: Test on 852GM/855GM.  Acceleration and HW
61  *		cursor are disabled on this platform.  (David Dawes)
62  *
63  *    02/2003 - Version 0.7.1: Test on 845G.  Acceleration is disabled
64  *		on this platform.  (David Dawes)
65  *
66  *    02/2003 - Version 0.7.2: Test on 830M.  Acceleration and HW
67  *		cursor are disabled on this platform.  (David Dawes)
68  *
69  *    02/2003 - Version 0.7.3: Fix 8-bit modes for mobile platforms
70  *		(David Dawes)
71  *
72  *    02/2003 - Version 0.7.4: Add checks for FB and FBCON_HAS_CFB* configured
73  *		in the kernel, and add mode bpp verification and default
74  *		bpp selection based on which FBCON_HAS_CFB* are configured.
75  *		(David Dawes)
76  *
77  *    02/2003 - Version 0.7.5: Add basic package/install scripts based on the
78  *		DRI packaging scripts.  (David Dawes)
79  *
80  * TODO:
81  *  -
82  *
83  * Wish List:
84  *  - Check clock limits for 845G and 830M.
85  *  - Test on SMP config.
86  *  - Check if any functions/data should be __devinit, etc.
87  *  - See if it's feasible to get/use DDC/EDID info.
88  *  - MTRR support.
89  *  - See if module unloading can work.
90  *  - See if driver works built-in to 2.5.32 kernel.
91  *  - Check acceleration problems on 830M-855GM.
92  *  - Add gtf support so that arbitrary modes can be calculated.
93  *  - Port driver to latest 2.5.x fbdev interface.
94  */
95 
96 #include <linux/config.h>
97 #include <linux/module.h>
98 #include <linux/kernel.h>
99 #include <linux/errno.h>
100 #include <linux/string.h>
101 #include <linux/mm.h>
102 #include <linux/tty.h>
103 #include <linux/slab.h>
104 #include <linux/delay.h>
105 #include <linux/fb.h>
106 #include <linux/console.h>
107 #include <linux/selection.h>
108 #include <linux/ioport.h>
109 #include <linux/init.h>
110 #include <linux/pci.h>
111 #include <linux/vmalloc.h>
112 #include <linux/kd.h>
113 #include <linux/vt_kern.h>
114 #include <linux/pagemap.h>
115 #include <linux/version.h>
116 
117 #include <asm/io.h>
118 
119 #include <video/fbcon.h>
120 #include <video/fbcon-cfb8.h>
121 #include <video/fbcon-cfb16.h>
122 #include <video/fbcon-cfb32.h>
123 
124 #include "intelfb.h"
125 
126 #include "builtinmodes.c"
127 
128 /*
129  * Limiting the class to PCI_CLASS_DISPLAY_VGA prevents function 1 of the
130  * mobile chipsets from being registered.
131  */
132 #if DETECT_VGA_CLASS_ONLY
133 #define INTELFB_CLASS_MASK ~0 << 8
134 #else
135 #define INTELFB_CLASS_MASK 0
136 #endif
137 
138 static struct pci_device_id intelfb_pci_table[] __devinitdata = {
139 	{ 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 },
140 	{ 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 },
141 	{ 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 },
142 	{ 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 },
143 	{ 0, }
144 };
145 
146 /* Global data */
147 static int num_registered = 0;
148 
149 
150 /* Forward declarations */
151 static int intelfb_get_fix(struct fb_fix_screeninfo *fix, int con,
152 			   struct fb_info *info);
153 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
154 static int intelfb_get_var(struct fb_var_screeninfo *var, int con,
155 			   struct fb_info *info);
156 #endif
157 static int intelfb_set_var(struct fb_var_screeninfo *var, int con,
158 			   struct fb_info *info);
159 static int intelfb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
160 			    struct fb_info *info);
161 static int intelfb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
162 			    struct fb_info *info);
163 static int intelfb_ioctl(struct inode *inode, struct file *file,
164 			 unsigned int cmd, unsigned long arg, int con,
165 			 struct fb_info *info);
166 static int intelfb_switch(int con, struct fb_info *info);
167 static int intelfb_updatevar(int con, struct fb_info *info);
168 static int intelfb_blank(int blank, struct fb_info *info);
169 static int intelfb_getcolreg(unsigned regno, unsigned *red, unsigned *green,
170 			     unsigned *blue, unsigned *transp,
171 			     struct fb_info *info);
172 static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
173 			     unsigned blue, unsigned transp,
174 			     struct fb_info *info);
175 static int intelfb_pci_register(struct pci_dev *pdev,
176 				const struct pci_device_id *ent);
177 static void __devexit intelfb_pci_unregister(struct pci_dev *pdev);
178 static int __devinit intelfb_set_fbinfo(struct intelfb_info *dinfo);
179 static void intelfb_set_dispsw(struct intelfb_info *dinfo,
180 			       struct display *disp);
181 static void mode_to_var(const struct fb_videomode *mode,
182 			struct fb_var_screeninfo *var, u32 bpp);
183 static int intelfb_set_mode(struct intelfb_info *dinfo,
184 			    struct fb_var_screeninfo *var,
185 			    struct display *disp, int blank);
186 static void intelfb_do_install_cmap(int con, struct fb_info *info);
187 static void update_dinfo(struct intelfb_info *dinfo,
188 			 struct fb_var_screeninfo *var, struct display *disp);
189 static void intelfb_flashcursor(unsigned long ptr);
190 static void fbcon_intelfb_setup(struct display *p);
191 static void fbcon_intelfb_bmove(struct display *p, int sy, int sx, int dy,
192 				int dx, int height, int width);
193 static void fbcon_intelfb_clear(struct vc_data *conp, struct display *p,
194 				int sy, int sx, int height, int width);
195 static void fbcon_intelfb_putc(struct vc_data *conp, struct display *p,
196 			       int c, int yy, int xx);
197 static void fbcon_intelfb_putcs(struct vc_data *conp, struct display *p,
198 				const unsigned short *s, int count,
199 				int yy, int xx);
200 static void fbcon_intelfb_revc(struct display *p, int xx, int yy);
201 static void fbcon_intelfb_clear_margins(struct vc_data *conp,
202 					struct display *p, int bottom_only);
203 static void fbcon_intelfb_cursor(struct display *disp, int mode, int x, int y);
204 
205 /* fb ops */
206 static struct fb_ops intel_fb_ops = {
207 	.owner =		THIS_MODULE,
208 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
209 	.fb_get_fix =		intelfb_get_fix,
210 	.fb_get_var =		intelfb_get_var,
211 #else
212 	.fb_blank =		intelfb_blank,
213 	.fb_setcolreg =		intelfb_setcolreg,
214 #endif
215 	.fb_set_var =		intelfb_set_var,
216 	.fb_get_cmap =		intelfb_get_cmap,
217 	.fb_set_cmap =		intelfb_set_cmap,
218 	.fb_pan_display	=	intelfbhw_pan_display,
219 	.fb_ioctl =		intelfb_ioctl
220 };
221 
222 /* PCI driver module table */
223 static struct pci_driver intelfb_driver = {
224 	.name =			INTELFB_MODULE_NAME,
225 	.id_table =		intelfb_pci_table,
226 	.probe =		intelfb_pci_register,
227 	.remove =		__devexit_p(intelfb_pci_unregister)
228 };
229 
230 /* fbcon acceleration */
231 static struct display_switch fbcon_intelfb = {
232 	.setup =		fbcon_intelfb_setup,
233 	.bmove =		fbcon_intelfb_bmove,
234 	.clear =		fbcon_intelfb_clear,
235 	.putc =			fbcon_intelfb_putc,
236 	.putcs =		fbcon_intelfb_putcs,
237 	.revc =			fbcon_intelfb_revc,
238 	.cursor =		fbcon_intelfb_cursor,
239 	.clear_margins =	fbcon_intelfb_clear_margins,
240 	.fontwidthmask =	FONTWIDTHRANGE(4, 16)
241 };
242 
243 /* Module description/parameters */
244 MODULE_AUTHOR("David Dawes <dawes@tungstengraphics.com>");
245 MODULE_DESCRIPTION(
246 	"Framebuffer driver for Intel(R) " SUPPORTED_CHIPSETS " chipsets");
247 MODULE_LICENSE("Dual BSD/GPL");
248 MODULE_DEVICE_TABLE(pci, intelfb_pci_table);
249 
250 INTELFB_INT_PARAM(accel, 1, "Enable console acceleration");
251 INTELFB_INT_PARAM(hwcursor, 1, "Enable HW cursor");
252 INTELFB_INT_PARAM(fixed, 0, "Disable mode switching");
253 INTELFB_INT_PARAM(noinit, 0, "Don't initialise graphics mode when loading");
254 INTELFB_INT_PARAM(noregister, 0, "Don't register, just probe and exit (debug)");
255 INTELFB_INT_PARAM(probeonly, 0, "Do a minimal probe (debug)");
256 INTELFB_INT_PARAM(idonly, 0,
257 		  "Just identify without doing anything else (debug)");
258 INTELFB_INT_PARAM(bailearly, 0, "Bail out early, depending on value (debug)");
259 INTELFB_STR_PARAM(mode, NULL,
260 		  "Initial video mode \"<xres>x<yres>[-<depth>][@<refresh>]\"");
261 INTELFB_STR_PARAM(font, NULL, "Specify which built-in font to use");
262 
263 
264 /* module load/unload entry points */
265 int __init
intelfb_init(void)266 intelfb_init(void)
267 {
268 	DBG_MSG("intelfb_init\n");
269 
270 	INF_MSG("Framebuffer driver for "
271 		"Intel(R) " SUPPORTED_CHIPSETS " chipsets\n");
272 	INF_MSG("Version " INTELFB_VERSION
273 		", written by David Dawes <dawes@tungstengraphics.com>\n");
274 
275 	if (idonly)
276 		return -ENODEV;
277 
278 	return pci_module_init(&intelfb_driver);
279 }
280 
281 void __exit
intelfb_exit(void)282 intelfb_exit(void)
283 {
284 	DBG_MSG("intelfb_exit\n");
285 	pci_unregister_driver(&intelfb_driver);
286 }
287 
288 #ifndef MODULE
289 #define OPT_EQUAL(opt, name) (!strncmp(opt, name, strlen(name)))
290 #define OPT_INTVAL(opt, name) simple_strtoul(opt + strlen(name), NULL, 0)
291 #define OPT_STRVAL(opt, name) (opt + strlen(name))
292 
293 static __inline__ char *
get_opt_string(const char * this_opt,const char * name)294 get_opt_string(const char *this_opt, const char *name)
295 {
296 	const char *p;
297 	int i;
298 	char *ret;
299 
300 	p = OPT_STRVAL(this_opt, name);
301 	i = 0;
302 	while (p[i] && p[i] != ' ' && p[i] != ',')
303 		i++;
304 	ret = kmalloc(i + 1, GFP_KERNEL);
305 	if (ret) {
306 		strncpy(ret, p, i);
307 		ret[i] = '\0';
308 	}
309 	return ret;
310 }
311 
312 static __inline__ int
get_opt_bool(const char * this_opt,const char * name,int * ret)313 get_opt_bool(const char *this_opt, const char *name, int *ret)
314 {
315 	if (!ret)
316 		return 0;
317 
318 	if (OPT_EQUAL(this_opt, name)) {
319 		if (this_opt[strlen(name)] == '=')
320 			*ret = simple_strtoul(this_opt + strlen(name) + 1,
321 					      NULL, 0);
322 		else
323 			*ret = 1;
324 	} else {
325 		if (OPT_EQUAL(this_opt, "no") && OPT_EQUAL(this_opt + 2, name))
326 			*ret = 0;
327 		else
328 			return 0;
329 	}
330 	return 1;
331 }
332 
333 int __init
intelfb_setup(char * options)334 intelfb_setup(char *options)
335 {
336 	char *this_opt;
337 
338 	DBG_MSG("intelfb_setup\n");
339 
340 	if (!options || !*options) {
341 		DBG_MSG("no options\n");
342 		return 0;
343 	} else
344 		DBG_MSG("options: %s\n", options);
345 
346 	/*
347 	 * These are the built-in options analogous to the module parameters
348 	 * defined above.
349 	 *
350 	 * The syntax is:
351 	 *
352 	 *    video=intelfb:[mode][,<param>=<val>] ...
353 	 *
354 	 * e.g.,
355 	 *
356 	 *    video=intelfb:1024x768-16@75,accel=0
357 	 */
358 
359 	while ((this_opt = strsep(&options, ","))) {
360 		if (!*this_opt)
361 			continue;
362 		if (get_opt_bool(this_opt, "accel", &accel))
363 			;
364 		else if (get_opt_bool(this_opt, "hwcursor", &hwcursor))
365 			;
366 		else if (get_opt_bool(this_opt, "fixed", &fixed))
367 			;
368 		else if (get_opt_bool(this_opt, "init", &noinit))
369 			noinit = !noinit;
370 		else if (OPT_EQUAL(this_opt, "font="))
371 			font = get_opt_string(this_opt, "font=");
372 		else if (OPT_EQUAL(this_opt, "mode="))
373 			mode = get_opt_string(this_opt, "mode=");
374 		else
375 			mode = this_opt;
376 	}
377 
378 	return 0;
379 }
380 #endif
381 
382 #ifdef MODULE
383 module_init(intelfb_init);
384 module_exit(intelfb_exit);
385 #endif
386 
387 static void
cleanup(struct intelfb_info * dinfo)388 cleanup(struct intelfb_info *dinfo)
389 {
390 	DBG_MSG("cleanup\n");
391 
392 	if (!dinfo)
393 		return;
394 
395 	if (dinfo->registered)
396 		unregister_framebuffer(&(dinfo->info));
397 
398 	if (&dinfo->cursor.timer)
399 		del_timer_sync(&dinfo->cursor.timer);
400 
401 #if USE_SYNC_PAGE
402 	if (dinfo->syncpage_virt) {
403 		struct page *pg = virt_to_page((void *)dinfo->syncpage_virt);
404 		if (pg) {
405 			put_page(pg);
406 			UnlockPage(pg);
407 			free_page(dinfo->syncpage_virt);
408 		}
409 	}
410 #endif
411 
412 	if (dinfo->cursor_base)
413 		iounmap((void *)dinfo->cursor_base);
414 	if (dinfo->ring_base)
415 		iounmap((void *)dinfo->ring_base);
416 	if (dinfo->fb_base)
417 		iounmap((void *)dinfo->fb_base);
418 	if (dinfo->mmio_base)
419 		iounmap((void *)dinfo->mmio_base);
420 	if (dinfo->mmio_base_phys && dinfo->pdev)
421 		release_mem_region(dinfo->mmio_base_phys,
422 				   pci_resource_len(dinfo->pdev, 1));
423 	if (dinfo->fb_base_phys && dinfo->pdev)
424 		release_mem_region(dinfo->fb_base_phys,
425 				   pci_resource_len(dinfo->pdev, 0));
426 	kfree(dinfo);
427 }
428 
429 #define bailout(dinfo) do {						\
430 	DBG_MSG("bailout\n");						\
431 	cleanup(dinfo);							\
432 	INF_MSG("Not going to register framebuffer, exiting...\n");	\
433 	return -ENODEV;							\
434 } while (0)
435 
436 
437 int
intelfb_var_to_depth(const struct fb_var_screeninfo * var)438 intelfb_var_to_depth(const struct fb_var_screeninfo *var)
439 {
440 #if 0
441 	DBG_MSG("intelfb_var_to_depth: bpp: %d, green.length is %d\n",
442 		var->bits_per_pixel, var->green.length);
443 #endif
444 
445 	switch (var->bits_per_pixel) {
446 	case 16:
447 		return (var->green.length == 6) ? 16 : 15;
448 	case 32:
449 		return 24;
450 	default:
451 		return var->bits_per_pixel;
452 	}
453 }
454 
455 static void
get_initial_mode(struct intelfb_info * dinfo)456 get_initial_mode(struct intelfb_info *dinfo)
457 {
458 	struct fb_var_screeninfo *var;
459 	int xtot, ytot;
460 
461 	DBG_MSG("get_initial_mode\n");
462 
463 	dinfo->initial_vga = 1;
464 	dinfo->initial_fb_base = screen_info.lfb_base;
465 	dinfo->initial_video_ram = screen_info.lfb_size * KB(64);
466 	dinfo->initial_pitch = screen_info.lfb_linelength;
467 
468 	var = &dinfo->initial_var;
469 	memset(var, 0, sizeof(*var));
470 	var->xres = screen_info.lfb_width;
471 	var->yres = screen_info.lfb_height;
472 	var->xres_virtual = var->xres;
473 #if ALLOCATE_FOR_PANNING
474 	/* Allow use of half of the video ram for panning */
475 	var->yres_virtual =
476 		dinfo->initial_video_ram / 2 / dinfo->initial_pitch;
477 	if (var->yres_virtual < var->yres)
478 		var->yres_virtual = var->yres;
479 #else
480 	var->yres_virtual = var->yres;
481 #endif
482 	var->bits_per_pixel = screen_info.lfb_depth;
483 	switch (screen_info.lfb_depth) {
484 	case 15:
485 		var->bits_per_pixel = 16;
486 		break;
487 	case 24:
488 		var->bits_per_pixel = 32;
489 		break;
490 	}
491 
492 	DBG_MSG("Initial info: FB is 0x%x/0x%x (%d kByte)\n",
493 		dinfo->initial_fb_base, dinfo->initial_video_ram,
494 		BtoKB(dinfo->initial_video_ram));
495 
496 	DBG_MSG("Initial info: mode is %dx%d-%d (%d)\n",
497 		var->xres, var->yres, var->bits_per_pixel,
498 		dinfo->initial_pitch);
499 
500 	/* Dummy timing values (assume 60Hz) */
501 	var->left_margin = (var->xres / 8) & 0xf8;
502 	var->right_margin = 32;
503 	var->upper_margin = 16;
504 	var->lower_margin = 4;
505 	var->hsync_len = (var->xres / 8) & 0xf8;
506 	var->vsync_len = 4;
507 
508 	xtot = var->xres + var->left_margin +
509 		var->right_margin + var->hsync_len;
510 	ytot = var->yres + var->upper_margin +
511 		var->lower_margin + var->vsync_len;
512 	var->pixclock = 10000000 / xtot * 1000 / ytot * 100 / 60;
513 
514 	var->height = -1;
515 	var->width = -1;
516 
517 	if (var->bits_per_pixel > 8) {
518 		var->red.offset = screen_info.red_pos;
519 		var->red.length = screen_info.red_size;
520 		var->green.offset = screen_info.green_pos;
521 		var->green.length = screen_info.green_size;
522 		var->blue.offset = screen_info.blue_pos;
523 		var->blue.length = screen_info.blue_size;
524 		var->transp.offset = screen_info.rsvd_pos;
525 		var->transp.length = screen_info.rsvd_size;
526 	} else {
527 		var->red.length = 8;
528 		var->green.length = 8;
529 		var->blue.length = 8;
530 	}
531 }
532 
533 static int
intelfb_pci_register(struct pci_dev * pdev,const struct pci_device_id * ent)534 intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
535 {
536 	struct intelfb_info *dinfo;
537 	int i, j, err;
538 	const char *s;
539 
540 	DBG_MSG("intelfb_pci_register\n");
541 
542 	num_registered++;
543 	if (num_registered != 1) {
544 		ERR_MSG("Attempted to register %d devices "
545 			"(should be only 1).\n", num_registered);
546 		return -ENODEV;
547 	}
548 
549 	if (!(dinfo = kmalloc(sizeof(struct intelfb_info), GFP_KERNEL))) {
550 		ERR_MSG("Could not allocate memory for intelfb_info.\n");
551 		return -ENODEV;
552 	}
553 
554 	memset(dinfo, 0, sizeof(*dinfo));
555 	dinfo->pdev = pdev;
556 
557 	/* Enable device. */
558 	if ((err = pci_enable_device(pdev))) {
559 		ERR_MSG("Cannot enable device.\n");
560 		cleanup(dinfo);
561 		return -ENODEV;
562 	}
563 
564 	/* Set base addresses. */
565 	dinfo->fb_base_phys = pci_resource_start(pdev, 0);
566 	dinfo->mmio_base_phys = pci_resource_start(pdev, 1);
567 
568 	DBG_MSG("fb region: 0x%lx/0x%lx, MMIO region: 0x%lx/0x%lx\n",
569 		pci_resource_start(pdev, 0), pci_resource_len(pdev, 0),
570 		pci_resource_start(pdev, 1), pci_resource_len(pdev, 1));
571 
572 	/* Reserve the fb and MMIO regions */
573 	if (!request_mem_region(dinfo->fb_base_phys, pci_resource_len(pdev, 0),
574 				INTELFB_MODULE_NAME)) {
575 		ERR_MSG("Cannot reserve FB region.\n");
576 		cleanup(dinfo);
577 		return -ENODEV;
578 	}
579 	if (!request_mem_region(dinfo->mmio_base_phys,
580 				pci_resource_len(pdev, 1),
581 				INTELFB_MODULE_NAME)) {
582 		ERR_MSG("Cannot reserve MMIO region.\n");
583 		cleanup(dinfo);
584 		return -ENODEV;
585 	}
586 
587 	/* Map the MMIO region. */
588 	dinfo->mmio_base = (u32)ioremap(dinfo->mmio_base_phys, INTEL_REG_SIZE);
589 	if (!dinfo->mmio_base) {
590 		ERR_MSG("Cannot map MMIO.\n");
591 		cleanup(dinfo);
592 		return -ENODEV;
593 	}
594 
595 	/* Get the chipset info. */
596 	dinfo->pci_chipset = pdev->device;
597 
598 	if (intelfbhw_get_chipset(pdev, &dinfo->name, &dinfo->chipset,
599 				  &dinfo->mobile)) {
600 		cleanup(dinfo);
601 		return -ENODEV;
602 	}
603 
604 	if (intelfbhw_get_memory(pdev, &dinfo->aperture_size,
605 				 &dinfo->stolen_size)) {
606 		cleanup(dinfo);
607 		return -ENODEV;
608 	}
609 
610 	INF_MSG("%02x:%02x.%d: %s, aperture size %dMB, "
611 		"stolen memory %dkB\n",
612 		pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
613 		dinfo->name, BtoMB(dinfo->aperture_size),
614 		BtoKB(dinfo->stolen_size));
615 
616 	/* Set these from the options. */
617 	dinfo->accel = accel;
618 	dinfo->hwcursor = hwcursor;
619 
620 	if (NOACCEL_CHIPSET(dinfo) && dinfo->accel == 1) {
621 		INF_MSG("Acceleration is not supported for the %s chipset.\n",
622 			dinfo->name);
623 		dinfo->accel = 0;
624 	}
625 
626 	/*
627 	 * For now, set the video ram size to the stolen size rounded
628 	 * down to a page multiple.  This means the agpgart driver doesn't
629 	 * need to get involved.
630 	 */
631 	dinfo->video_ram = ROUND_DOWN_TO_PAGE(dinfo->stolen_size);
632 
633 	/* Allocate space for the ring buffer and HW cursor. */
634 	if (dinfo->accel) {
635 		dinfo->ring_size = RINGBUFFER_SIZE;
636 		dinfo->video_ram -= dinfo->ring_size;
637 		dinfo->ring_base_phys = dinfo->fb_base_phys + dinfo->video_ram;
638 		dinfo->ring_tail_mask = dinfo->ring_size - 1;
639 	}
640 	if (dinfo->hwcursor && !dinfo->mobile) {
641 		dinfo->cursor_size = HW_CURSOR_SIZE;
642 		dinfo->video_ram -= dinfo->cursor_size;
643 		dinfo->cursor_offset = dinfo->video_ram;
644 		dinfo->cursor_base_phys = dinfo->fb_base_phys +
645 					  dinfo->cursor_offset;
646 	}
647 
648 	/* Framebuffer starts at offset 0. */
649 	dinfo->fb_offset = 0;
650 	dinfo->video_ram -= dinfo->fb_offset;
651 
652 	/* Map the FB region.  Only map the memory used as video ram. */
653 	dinfo->fb_base = (u32)ioremap(dinfo->fb_base_phys + dinfo->fb_offset,
654 				      dinfo->video_ram);
655 	if (!dinfo->fb_base) {
656 		ERR_MSG("Cannot map framebuffer.\n");
657 		cleanup(dinfo);
658 		return -ENODEV;
659 	}
660 
661 	if (dinfo->accel) {
662 		/* Map the ring buffer. */
663 		dinfo->ring_base = (u32)ioremap(dinfo->ring_base_phys,
664 						dinfo->ring_size);
665 		if (!dinfo->ring_base) {
666 			dinfo->accel = 0;
667 			WRN_MSG("Cannot map ring buffer: "
668 				"acceleration disabled.\n");
669 		}
670 	}
671 
672 	if (dinfo->hwcursor && !dinfo->mobile) {
673 		/* Map the HW cursor buffer. */
674 		dinfo->cursor_base = (u32)ioremap(dinfo->cursor_base_phys,
675 						  dinfo->cursor_size);
676 		if (!dinfo->cursor_base) {
677 			WRN_MSG("Cannot map the HW cursor space: "
678 				"HW cursor disabled.\n");
679 			dinfo->hwcursor = 0;
680 		}
681 	}
682 
683 	/*
684 	 * For mobile platforms, a physical page address is needed for the
685 	 * HW cursor.  Allocate it here and insert it into the GTT.
686 	 * If/when this driver is modified to be aware of the agpgart
687 	 * driver, it can be used to obtain/insert the page.
688 	 */
689 	if (dinfo->hwcursor && dinfo->mobile) {
690 #if !MOBILE_HW_CURSOR
691 		dinfo->hwcursor = 0;
692 		INF_MSG("HW cursor is not supported for mobile platforms.\n");
693 #else
694 		struct page *pg;
695 
696 		/*
697 		 * Allocate a page of physical memory for the graphics
698 		 * engine to write to for synchronisation purposes.
699 		 */
700 		pg = alloc_page(GFP_KERNEL);
701 		if (!pg) {
702 			WRN_MSG("Cannot allocate a page for the HW cursor.  "
703 				"Disabling the HW cursor.");
704 			dinfo->hwcursor = 0;
705 		} else {
706 			get_page(pg);
707 			LockPage(pg);
708 			dinfo->cursor_page_virt = (u32)page_address(pg);
709 			dinfo->cursor_base_real =
710 						virt_to_phys(page_address(pg));
711 
712 			/*
713 			 * XXX Add code to insert GTT entry and map the
714 			 * appropriate part of the aperture as
715 			 * dinfo->cursor_base.
716 			 */
717 		}
718 #endif
719 	}
720 	DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%x)\n",
721 		dinfo->fb_base_phys, dinfo->fb_offset, dinfo->video_ram,
722 		dinfo->fb_base);
723 	DBG_MSG("MMIO: 0x%x/0x%x (0x%x)\n",
724 		dinfo->mmio_base_phys, INTEL_REG_SIZE, dinfo->mmio_base);
725 	DBG_MSG("ring buffer: 0x%x/0x%x (0x%x)\n",
726 		dinfo->ring_base_phys, dinfo->ring_size, dinfo->ring_base);
727 	DBG_MSG("HW cursor: 0x%x/0x%x (0x%x) (offset 0x%x) (phys 0x%x)\n",
728 		dinfo->cursor_base_phys, dinfo->cursor_size,
729 		dinfo->cursor_base, dinfo->cursor_offset,
730 		dinfo->cursor_base_real);
731 
732 	DBG_MSG("options: accel = %d, fixed = %d, noinit = %d\n",
733 		accel, fixed, noinit);
734 	DBG_MSG("options: mode = \"%s\", font = \"%s\"\n",
735 		mode ? mode : "", font ? font : "");
736 
737 	if (probeonly)
738 		bailout(dinfo);
739 
740 	dinfo->fixed_mode = fixed;
741 
742 	/*
743 	 * Check if the LVDS port or any DVO ports are enabled.  If so,
744 	 * don't allow mode switching.
745 	 */
746 	if ((s = intelfbhw_check_non_crt(dinfo))) {
747 		WRN_MSG("Non-CRT device is enabled (%s).  "
748 			"Disabling mode switching.\n", s);
749 		dinfo->fixed_mode = 1;
750 	}
751 
752 	if (bailearly == 1)
753 		bailout(dinfo);
754 
755 	if (dinfo->fixed_mode && ORIG_VIDEO_ISVGA != VIDEO_TYPE_VLFB) {
756 		ERR_MSG("Video mode must be programmed at boot time.\n");
757 		cleanup(dinfo);
758 		return -ENODEV;
759 	}
760 
761 	if (bailearly == 2)
762 		bailout(dinfo);
763 
764 	/* Initialise dinfo and related data. */
765 	/* If an initial mode was programmed at boot time, get its details. */
766 	if (ORIG_VIDEO_ISVGA == VIDEO_TYPE_VLFB)
767 		get_initial_mode(dinfo);
768 
769 	if (bailearly == 3)
770 		bailout(dinfo);
771 
772 #if 0
773 	if (dinfo->fixed_mode) {
774 		/*
775 		 * XXX Check video ram amounts and remap if the region already
776 		 * mapped is too small because of ring/cursor allocations.
777 		 */
778 		update_dinfo(dinfo, &dinfo->initial_var, NULL);
779 	}
780 #endif
781 
782 	/* currcon will be set by the first switch. */
783 	dinfo->currcon = -1;
784 	dinfo->vc_mode = KD_TEXT;
785 
786 #if USE_SYNC_PAGE
787 	if (dinfo->accel) {
788 		struct page *pg;
789 
790 		/*
791 		 * Allocate a page of physical memory for the graphics
792 		 * engine to write to for synchronisation purposes.
793 		 */
794 		pg = alloc_page(GFP_KERNEL);
795 		if (!pg) {
796 			WRN_MSG("Cannot allocate a page for 2D accel.  "
797 				"Disabling acceleration.");
798 			dinfo->accel = 0;
799 		} else {
800 			get_page(pg);
801 			LockPage(pg);
802 			dinfo->syncpage_virt = (u32)page_address(pg);
803 			dinfo->syncpage_phys = virt_to_phys(page_address(pg));
804 			/* Write zero to the first dword */
805 			writel(0, dinfo->syncpage_virt);
806 			DBG_MSG("2D sync page: 0x%x (0x%x)\n",
807 				dinfo->syncpage_phys, dinfo->syncpage_virt);
808 		}
809 	}
810 #endif
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 	for (i = 0; i < 16; i++) {
825 		j = color_table[i];
826 		dinfo->palette[i].red = default_red[j];
827 		dinfo->palette[i].green = default_grn[j];
828 		dinfo->palette[i].blue = default_blu[j];
829 	}
830 
831 	if (bailearly == 6)
832 		bailout(dinfo);
833 
834 	pci_set_drvdata(pdev, dinfo);
835 
836 	/* Save the initial register state. */
837 	i = intelfbhw_read_hw_state(dinfo, &dinfo->save_state,
838 				    bailearly > 6 ? bailearly - 6 : 0);
839 	if (i != 0) {
840 		DBG_MSG("intelfbhw_read_hw_state returned %d\n", i);
841 		bailout(dinfo);
842 	}
843 
844 	intelfbhw_print_hw_state(dinfo, &dinfo->save_state);
845 
846 	if (bailearly == 18)
847 		bailout(dinfo);
848 
849 #if TEST_MODE_TO_HW
850 	{
851 		struct intelfb_hwstate hw;
852 		struct fb_var_screeninfo var;
853 		int i;
854 
855 		for (i = 0; i < num_modes; i++) {
856 			mode_to_var(&modedb[i], &var, 8);
857 			intelfbhw_read_hw_state(dinfo, &hw, 0);
858 			if (intelfbhw_mode_to_hw(dinfo, &hw, &var)) {
859 				DGB_MSG("Failed to set hw for mode %dx%d\n",
860 					var.xres, var.yres);
861 			} else {
862 				DGB_MSG("HW state for mode %dx%d\n",
863 					var.xres, var.yres);
864 				intelfbhw_print_hw_state(dinfo, &hw);
865 			}
866 		}
867 	}
868 #endif
869 
870 	/* Cursor initialisation */
871 	if (dinfo->hwcursor) {
872 		init_timer(&dinfo->cursor.timer);
873 		dinfo->cursor.timer.function = intelfb_flashcursor;
874 		dinfo->cursor.timer.data = (unsigned long)dinfo;
875 		dinfo->cursor.state = CM_ERASE;
876 		spin_lock_init(&dinfo->DAClock);
877 	}
878 
879 	if (bailearly == 19)
880 		bailout(dinfo);
881 
882 
883 	if (noregister)
884 		bailout(dinfo);
885 
886 	if (register_framebuffer(&(dinfo->info)) < 0) {
887 		ERR_MSG("Cannot register framebuffer.\n");
888 		cleanup(dinfo);
889 		return -ENODEV;
890 	}
891 
892 	dinfo->registered = 1;
893 
894 	return 0;
895 }
896 
897 static void __devexit
intelfb_pci_unregister(struct pci_dev * pdev)898 intelfb_pci_unregister(struct pci_dev *pdev)
899 {
900 	struct intelfb_info *dinfo = pci_get_drvdata(pdev);
901 
902 	DBG_MSG("intelfb_pci_unregister\n");
903 
904 	if (!dinfo)
905 		return;
906 
907 	cleanup(dinfo);
908 
909 	pci_set_drvdata(pdev, NULL);
910 }
911 
912 /*
913  * A simplified version of fb_find_mode.  The latter doesn't seem to work
914  * too well -- haven't figured out why yet.
915  */
916 static int
intelfb_find_mode(struct fb_var_screeninfo * var,struct fb_info * info,const char * mode_option,const struct fb_videomode * db,unsigned int dbsize,const struct fb_videomode * default_mode,unsigned int default_bpp)917 intelfb_find_mode(struct fb_var_screeninfo *var,
918 		  struct fb_info *info, const char *mode_option,
919 		  const struct fb_videomode *db, unsigned int dbsize,
920 		  const struct fb_videomode *default_mode,
921 		  unsigned int default_bpp)
922 {
923 	int i;
924 	char mname[20] = "", tmp[20] = "", *p, *q;
925 	unsigned int bpp = 0;
926 
927 	DBG_MSG("intelfb_find_mode\n");
928 
929 	/* Set up defaults */
930 	if (!db) {
931 		db = modedb;
932 		dbsize = sizeof(modedb) / sizeof(*modedb);
933 	}
934 
935 	if (!default_bpp)
936 #if defined(FBCON_HAS_CFB8)
937 		default_bpp = 8;
938 #elif defined(FBCON_HAS_CFB16)
939 		default_bpp = 16;
940 #elif defined(FBCON_HAS_CFB32)
941 		default_bpp = 32;
942 #endif
943 
944 	var->activate = FB_ACTIVATE_TEST;
945 	if (mode_option && *mode_option) {
946 		if (strlen(mode_option) < sizeof(tmp) - 1) {
947 			strcat(tmp, mode_option);
948 			q = tmp;
949 			p = strsep(&q, "-");
950 			strcat(mname, p);
951 			if (q) {
952 				p = strsep(&q, "@");
953 				bpp = simple_strtoul(p, NULL, 10);
954 				if (q) {
955 					strcat(mname, "@");
956 					strcat(mname, q);
957 				}
958 			}
959 		}
960 		if (!bpp)
961 			bpp = default_bpp;
962 		DBG_MSG("Mode is %s, bpp %d\n", mname, bpp);
963 	}
964 	if (*mname) {
965 		for (i = 0; i < dbsize; i++) {
966 			if (!strncmp(db[i].name, mname, strlen(mname))) {
967 				mode_to_var(&db[i], var, bpp);
968 				if (!intelfb_set_var(var, -1, info))
969 					return 1;
970 			}
971 		}
972 	}
973 
974 	if (!default_mode)
975 		return 0;
976 
977 	mode_to_var(default_mode, var, default_bpp);
978 	if (!intelfb_set_var(var, -1, info))
979 		return 3;
980 
981 	for (i = 0; i < dbsize; i++) {
982 		mode_to_var(&db[i], var, default_bpp);
983 		if (!intelfb_set_var(var, -1, info))
984 			return 4;
985 	}
986 
987 	return 0;
988 }
989 
990 static __inline__ int
var_to_refresh(const struct fb_var_screeninfo * var)991 var_to_refresh(const struct fb_var_screeninfo *var)
992 {
993 	int xtot = var->xres + var->left_margin + var->right_margin +
994 		   var->hsync_len;
995 	int ytot = var->yres + var->upper_margin + var->lower_margin +
996 		   var->vsync_len;
997 
998 	return (1000000000 / var->pixclock * 1000 + 500) / xtot / ytot;
999 }
1000 
1001 /* Various intialisation functions */
1002 
1003 static int __devinit
intelfb_init_disp_var(struct intelfb_info * dinfo)1004 intelfb_init_disp_var(struct intelfb_info *dinfo)
1005 {
1006 	int msrc = 0;
1007 
1008 	DBG_MSG("intelfb_init_disp_var\n");
1009 
1010 	if (dinfo->fixed_mode) {
1011 		dinfo->disp.var = dinfo->initial_var;
1012 		msrc = 5;
1013 	} else {
1014 		if (mode) {
1015 			msrc = intelfb_find_mode(&dinfo->disp.var,
1016 						 &dinfo->info, mode,
1017 						 modedb, num_modes, NULL, 0);
1018 			if (msrc)
1019 				msrc |= 8;
1020 		}
1021 		if (!msrc) {
1022 			msrc = intelfb_find_mode(&dinfo->disp.var,
1023 						 &dinfo->info, PREFERRED_MODE,
1024 						 modedb, num_modes,
1025 						 &modedb[DFLT_MODE], 0);
1026 		}
1027 	}
1028 
1029 	if (!msrc) {
1030 		ERR_MSG("Cannot find a suitable video mode.\n");
1031 		return 1;
1032 	}
1033 
1034 	INF_MSG("Initial video mode is %dx%d-%d@%d.\n", dinfo->disp.var.xres,
1035 		dinfo->disp.var.yres, intelfb_var_to_depth(&dinfo->disp.var),
1036 		var_to_refresh(&dinfo->disp.var));
1037 
1038 	DBG_MSG("Initial video mode is from %d.\n", msrc);
1039 
1040 	if (dinfo->accel)
1041 		dinfo->disp.var.accel_flags |= FB_ACCELF_TEXT;
1042 	else
1043 		dinfo->disp.var.accel_flags &= ~FB_ACCELF_TEXT;
1044 
1045 	return 0;
1046 }
1047 
1048 static void
intelfb_set_dispsw(struct intelfb_info * dinfo,struct display * disp)1049 intelfb_set_dispsw(struct intelfb_info *dinfo, struct display *disp)
1050 {
1051 	DBG_MSG("intelfb_set_dispsw: (bpp is %d)\n", disp->var.bits_per_pixel);
1052 
1053 	disp->dispsw_data = NULL;
1054 	disp->dispsw = NULL;
1055 
1056 	switch (disp->var.bits_per_pixel) {
1057 #ifdef FBCON_HAS_CFB8
1058 	case 8:
1059 		break;
1060 #endif
1061 #ifdef FBCON_HAS_CFB16
1062 	case 16:
1063 		disp->dispsw_data = &dinfo->con_cmap.cfb16;
1064 		break;
1065 #endif
1066 #ifdef FBCON_HAS_CFB32
1067 	case 32:
1068 		disp->dispsw_data = &dinfo->con_cmap.cfb32;
1069 		break;
1070 #endif
1071 	default:
1072 		WRN_MSG("Setting fbcon_dummy renderer (bpp is %d).\n",
1073 			disp->var.bits_per_pixel);
1074 		disp->dispsw = &fbcon_dummy;
1075 	}
1076 	/* For all supported bpp. */
1077 	if (!disp->dispsw)
1078 		disp->dispsw = &fbcon_intelfb;
1079 }
1080 
1081 static int __devinit
intelfb_init_disp(struct intelfb_info * dinfo)1082 intelfb_init_disp(struct intelfb_info *dinfo)
1083 {
1084 	struct fb_info *info;
1085 	struct display *disp;
1086 
1087 	DBG_MSG("intelfb_init_disp\n");
1088 
1089 	info = &dinfo->info;
1090 	disp = &dinfo->disp;
1091 
1092 	if (intelfb_init_disp_var(dinfo))
1093 		return 1;
1094 
1095 	info->disp = disp;
1096 
1097 	update_dinfo(dinfo, &disp->var, disp);
1098 
1099 	intelfb_set_dispsw(dinfo, disp);
1100 
1101 	dinfo->currcon_display = disp;
1102 
1103 	return 0;
1104 }
1105 
1106 static int __devinit
intelfb_set_fbinfo(struct intelfb_info * dinfo)1107 intelfb_set_fbinfo(struct intelfb_info *dinfo)
1108 {
1109 	struct fb_info *info;
1110 
1111 	DBG_MSG("intelfb_set_fbinfo\n");
1112 
1113 	info = &dinfo->info;
1114 
1115 	strcpy(info->modename, dinfo->name);
1116 	info->node = NODEV;
1117 	info->flags = FBINFO_FLAG_DEFAULT;
1118 	info->fbops = &intel_fb_ops;
1119 	info->display_fg = NULL;
1120 	info->changevar = NULL;
1121 	info->switch_con = intelfb_switch;
1122 	info->updatevar = intelfb_updatevar;
1123 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1124 	info->blank = intelfbhw_do_blank;
1125 #endif
1126 
1127 	if (intelfb_init_disp(dinfo))
1128 		return 1;
1129 	return 0;
1130 }
1131 
1132 /* Update dinfo to match the active video mode. */
1133 static void
update_dinfo(struct intelfb_info * dinfo,struct fb_var_screeninfo * var,struct display * disp)1134 update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var,
1135 	     struct display *disp)
1136 {
1137 	DBG_MSG("update_dinfo\n");
1138 
1139 	dinfo->bpp = var->bits_per_pixel;
1140 	dinfo->depth = intelfb_var_to_depth(var);
1141 	dinfo->xres = var->xres;
1142 	dinfo->yres = var->xres;
1143 	dinfo->pixclock = var->pixclock;
1144 
1145 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1146 	intelfb_get_fix(&dinfo->info.fix, dinfo->currcon, &dinfo->info);
1147 #endif
1148 
1149 	switch (dinfo->bpp) {
1150 #ifdef FBCON_HAS_CFB8
1151 	case 8:
1152 		dinfo->visual = FB_VISUAL_PSEUDOCOLOR;
1153 		dinfo->pitch = disp->var.xres_virtual;
1154 		break;
1155 #endif
1156 #ifdef FBCON_HAS_CFB16
1157 	case 16:
1158 		dinfo->visual = FB_VISUAL_TRUECOLOR;
1159 		dinfo->pitch = disp->var.xres_virtual * 2;
1160 		break;
1161 #endif
1162 #ifdef FBCON_HAS_CFB32
1163 	case 32:
1164 		dinfo->visual = FB_VISUAL_TRUECOLOR;
1165 		dinfo->pitch = disp->var.xres_virtual * 4;
1166 		break;
1167 #endif
1168 	}
1169 
1170 	/* Make sure the line length is a aligned correctly. */
1171 	dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
1172 
1173 	if (dinfo->fixed_mode)
1174 		dinfo->pitch = dinfo->initial_pitch;
1175 
1176 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1177 	disp->screen_base = (char *)dinfo->fb_base;
1178 	disp->visual = dinfo->visual;
1179 	disp->line_length = dinfo->pitch;
1180 	disp->type = FB_TYPE_PACKED_PIXELS;
1181 	disp->type_aux = 0;
1182 	disp->ypanstep = 1;
1183 	disp->ywrapstep = 0;
1184 #else
1185 	dinfo->info.screen_base = (char *)dinfo->fb_base;
1186 	dinfo->info.fix.line_length = dinfo->pitch;
1187 	dinfo->info.fix.visual = dinfo->visual;
1188 #endif
1189 	disp->can_soft_blank = 1;
1190 	disp->inverse = 0;
1191 	DBG_MSG("disp->scrollmode is 0x%x\n", disp->scrollmode);
1192 	if (disp->var.yres_virtual == disp->var.yres &&
1193 	    !(disp->var.accel_flags & FB_ACCELF_TEXT)) {
1194 		/* No space for panning. */
1195 		disp->scrollmode = SCROLL_YREDRAW;
1196 	} else {
1197 		disp->scrollmode = __SCROLL_YMOVE;
1198 	}
1199 }
1200 
1201 /* fbops functions */
1202 
1203 static int
intelfb_get_fix(struct fb_fix_screeninfo * fix,int con,struct fb_info * info)1204 intelfb_get_fix(struct fb_fix_screeninfo *fix, int con, struct fb_info *info)
1205 {
1206 	struct intelfb_info *dinfo = GET_DINFO(info);
1207 	struct display *disp;
1208 
1209 	DBG_MSG("intelfb_get_fix\n");
1210 
1211 	disp = GET_DISP(info, con);
1212 
1213 	memset(fix, 0, sizeof(*fix));
1214 	strcpy(fix->id, dinfo->name);
1215 	fix->smem_start = dinfo->fb_base_phys;
1216 	fix->smem_len = dinfo->video_ram;
1217 	fix->type = FB_TYPE_PACKED_PIXELS;
1218 	fix->type_aux = 0;
1219 	fix->visual = dinfo->visual;
1220 	fix->xpanstep = 8;
1221 	fix->ypanstep = 1;
1222 	fix->ywrapstep = 0;
1223 	fix->line_length = dinfo->pitch;
1224 	fix->mmio_start = dinfo->mmio_base_phys;
1225 	fix->mmio_len = INTEL_REG_SIZE;
1226 	fix->accel = FB_ACCEL_NONE;
1227 	return 0;
1228 }
1229 
1230 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
1231 static int
intelfb_get_var(struct fb_var_screeninfo * var,int con,struct fb_info * info)1232 intelfb_get_var(struct fb_var_screeninfo *var, int con, struct fb_info *info)
1233 {
1234 	struct intelfb_info *dinfo = GET_DINFO(info);
1235 
1236 	DBG_MSG("intelfb_get_var\n");
1237 
1238 	if (con == -1)
1239 		*var = dinfo->disp.var;
1240 	else
1241 		*var = fb_display[con].var;
1242 
1243 	return 0;
1244 }
1245 #endif
1246 
1247 static int
intelfb_set_var(struct fb_var_screeninfo * var,int con,struct fb_info * info)1248 intelfb_set_var(struct fb_var_screeninfo *var, int con, struct fb_info *info)
1249 {
1250 	struct display *disp;
1251 	int change_var = 0;
1252 	struct fb_var_screeninfo v;
1253 	struct intelfb_info *dinfo;
1254 	int ret;
1255 	static int first = 1;
1256 
1257 	DBG_MSG("intelfb_set_var: con is %d, accel_flags is %d\n",
1258 		con, var->accel_flags);
1259 
1260 	dinfo = GET_DINFO(info);
1261 	disp = GET_DISP(info, con);
1262 
1263 	if (intelfbhw_validate_mode(dinfo, con, var) != 0)
1264 		return -EINVAL;
1265 
1266 	memcpy(&v, var, sizeof(v));
1267 
1268 	/* Check for a supported bpp. */
1269 	if (v.bits_per_pixel <= 8) {
1270 #ifdef FBCON_HAS_CFB8
1271 		v.bits_per_pixel = 8;
1272 #else
1273 		return -EINVAL;
1274 #endif
1275 	} else if (v.bits_per_pixel <= 16) {
1276 #ifdef FBCON_HAS_CFB16
1277 		if (v.bits_per_pixel == 16)
1278 			v.green.length = 6;
1279 		v.bits_per_pixel = 16;
1280 #else
1281 		return -EINVAL;
1282 #endif
1283 	} else if (v.bits_per_pixel <= 32) {
1284 #ifdef FBCON_HAS_CFB32
1285 		v.bits_per_pixel = 32;
1286 #else
1287 		return -EINVAL;
1288 #endif
1289 	} else
1290 		return -EINVAL;
1291 
1292 	if (con < 0) {
1293 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1294 		info->var = *var;
1295 #endif
1296 		return 0;
1297 	}
1298 
1299 
1300 	change_var = ((disp->var.xres != var->xres) ||
1301 		      (disp->var.yres != var->yres) ||
1302 		      (disp->var.xres_virtual != var->xres_virtual) ||
1303 		      (disp->var.yres_virtual != var->yres_virtual) ||
1304 		      (disp->var.bits_per_pixel != var->bits_per_pixel) ||
1305 		      memcmp(&disp->var.red, &var->red, sizeof(var->red)) ||
1306 		      memcmp(&disp->var.green, &var->green,
1307 			     sizeof(var->green)) ||
1308 		      memcmp(&disp->var.blue, &var->blue, sizeof(var->blue)));
1309 
1310 	if (dinfo->fixed_mode &&
1311 	    (change_var ||
1312 	     var->yres_virtual > dinfo->initial_var.yres_virtual ||
1313 	     var->yres_virtual < dinfo->initial_var.yres ||
1314 	     var->xoffset || var->nonstd)) {
1315 		if (first) {
1316 			ERR_MSG("Changing the video mode is not supported.\n");
1317 			first = 0;
1318 		}
1319 		return -EINVAL;
1320 	}
1321 
1322 	if ((var->activate & FB_ACTIVATE_MASK) != FB_ACTIVATE_NOW)
1323 		return 0;
1324 
1325 	switch (intelfb_var_to_depth(&v)) {
1326 #ifdef FBCON_HAS_CFB8
1327 	case 8:
1328 		v.red.offset = v.green.offset = v.blue.offset = 0;
1329 		v.red.length = v.green.length = v.blue.length = 8;
1330 		v.transp.offset = v.transp.length = 0;
1331 		break;
1332 #endif
1333 #ifdef FBCON_HAS_CFB16
1334 	case 15:
1335 		v.red.offset = 10;
1336 		v.green.offset = 5;
1337 		v.blue.offset = 0;
1338 		v.red.length = v.green.length = v.blue.length = 5;
1339 		v.transp.offset = v.transp.length = 0;
1340 		break;
1341 	case 16:
1342 		v.red.offset = 11;
1343 		v.green.offset = 5;
1344 		v.blue.offset = 0;
1345 		v.red.length = 5;
1346 		v.green.length = 6;
1347 		v.blue.length = 5;
1348 		v.transp.offset = v.transp.length = 0;
1349 		break;
1350 #endif
1351 #ifdef FBCON_HAS_CFB32
1352 	case 24:
1353 		v.red.offset = 16;
1354 		v.green.offset = 8;
1355 		v.blue.offset = 0;
1356 		v.red.length = v.green.length = v.blue.length = 8;
1357 		v.transp.offset = v.transp.length = 0;
1358 		break;
1359 	case 32:
1360 		v.red.offset = 16;
1361 		v.green.offset = 8;
1362 		v.blue.offset = 0;
1363 		v.red.length = v.green.length = v.blue.length = 8;
1364 		v.transp.offset = 24;
1365 		v.transp.length = 8;
1366 		break;
1367 #endif
1368 	}
1369 
1370 	if (v.xoffset < 0)
1371 		v.xoffset = 0;
1372 	if (v.yoffset < 0)
1373 		v.yoffset = 0;
1374 
1375 	if (v.xoffset > v.xres_virtual - v.xres)
1376 		v.xoffset = v.xres_virtual - v.xres;
1377 	if (v.yoffset > v.yres_virtual - v.yres)
1378 		v.yoffset = v.yres_virtual - v.yres;
1379 
1380 	v.red.msb_right = v.green.msb_right = v.blue.msb_right =
1381 			  v.transp.msb_right = 0;
1382 
1383 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
1384 	info->var = v;
1385 #endif
1386 
1387 	memcpy(&disp->var, &v, sizeof(v));
1388 
1389 	update_dinfo(dinfo, &v, disp);
1390 
1391 	intelfb_set_dispsw(dinfo, disp);
1392 
1393 	if (change_var) {
1394 		if (info && info->changevar)
1395 			info->changevar(con);
1396 	}
1397 
1398 	intelfb_blank(1, info);
1399 
1400 #if 0
1401 	if (dinfo->hwcursor)
1402 		intelfbhw_cursor_hide(dinfo);
1403 #endif
1404 
1405 	intelfbhw_2d_stop(dinfo);
1406 
1407 	if (!dinfo->fixed_mode) {
1408 		mdelay(100);
1409 		if ((ret = intelfb_set_mode(dinfo, &v, disp, 1)))
1410 			return ret;
1411 
1412 		mdelay(100);
1413 	}
1414 
1415 	if (dinfo->vc_mode == KD_TEXT) {
1416 		intelfb_do_install_cmap(con, info);
1417 
1418 		if (dinfo->hwcursor) {
1419 			del_timer(&dinfo->cursor.timer);
1420 			dinfo->cursor.state = CM_ERASE;
1421 			if (disp->conp) {
1422 				intelfbhw_cursor_init(dinfo);
1423 				intelfbhw_cursor_load(dinfo, disp);
1424 				dinfo->cursor.redraw = 1;
1425 			}
1426 		}
1427 
1428 		mdelay(100);
1429 
1430 		intelfbhw_2d_start(dinfo);
1431 #if 0
1432 		if (dinfo->hwcursor)
1433 			intelfbhw_cursor_show(dinfo);
1434 #endif
1435 
1436 		if (v.yoffset != 0)
1437 			intelfbhw_pan_display(&v, con, info);
1438 	} else {
1439 		dinfo->cursor.enabled = 0;
1440 	}
1441 
1442 	intelfb_blank(0, info);
1443 
1444 	return 0;
1445 }
1446 
1447 static int
intelfb_get_cmap(struct fb_cmap * cmap,int kspc,int con,struct fb_info * info)1448 intelfb_get_cmap(struct fb_cmap *cmap, int kspc, int con, struct fb_info *info)
1449 {
1450 	struct intelfb_info *dinfo = GET_DINFO(info);
1451 	struct display *disp = GET_DISP(info, con);
1452 
1453 	DBG_MSG("intelfb_get_cmap: con = %d, bpp = %d\n", con,
1454 		disp->var.bits_per_pixel);
1455 
1456 	if (con == dinfo->currcon)
1457 		return fb_get_cmap(cmap, kspc, intelfb_getcolreg, info);
1458 	else if (disp->cmap.len)
1459 		fb_copy_cmap(&disp->cmap, cmap, kspc ? 0 : 2);
1460 	else {
1461 		int cmap_len = (disp->var.bits_per_pixel > 8) ? 16 : 256;
1462 		fb_copy_cmap(fb_default_cmap(cmap_len), cmap, kspc ? 0 : 2);
1463 	}
1464 	return 0;
1465 }
1466 
1467 static int
intelfb_set_cmap(struct fb_cmap * cmap,int kspc,int con,struct fb_info * info)1468 intelfb_set_cmap(struct fb_cmap *cmap, int kspc, int con, struct fb_info *info)
1469 {
1470 	struct intelfb_info *dinfo = GET_DINFO(info);
1471 	struct display *disp = GET_DISP(info, con);
1472 	unsigned int cmap_len;
1473 	int err;
1474 
1475 	DBG_MSG("intelfb_set_cmap: con = %d, bpp = %d\n", con,
1476 		disp->var.bits_per_pixel);
1477 
1478 	cmap_len = (disp->var.bits_per_pixel > 8) ? 16 : 256;
1479 	if (!disp->cmap.len) {
1480 		err = fb_alloc_cmap(&disp->cmap, cmap_len, 0);
1481 		if (err)
1482 			return err;
1483 	}
1484 	if (con == dinfo->currcon)
1485 		return FB_SET_CMAP(cmap, kspc, intelfb_setcolreg, info);
1486 	else
1487 		fb_copy_cmap(cmap, &disp->cmap, kspc ? 0 : 1);
1488 
1489 	return 0;
1490 }
1491 
1492 /* When/if we have our own ioctls. */
1493 static int
intelfb_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg,int con,struct fb_info * info)1494 intelfb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1495 	      unsigned long arg, int con, struct fb_info *info)
1496 {
1497 	DBG_MSG("intelfb_ioctl\n");
1498 	return -EINVAL;
1499 }
1500 
1501 static int
intelfb_switch(int con,struct fb_info * info)1502 intelfb_switch(int con, struct fb_info *info)
1503 {
1504 	struct intelfb_info *dinfo = GET_DINFO(info);
1505 	struct display *disp;
1506 	struct fb_cmap *cmap;
1507 	int switchmode = 0;
1508 
1509 	DBG_MSG("intelfb_switch: con = %d, currcon = %d\n",
1510 		con, dinfo->currcon);
1511 
1512 	disp = GET_DISP(info, con);
1513 	if (dinfo->currcon >= 0) {
1514 		cmap = &dinfo->currcon_display->cmap;
1515 		if (cmap->len)
1516 			fb_get_cmap(cmap, 1, intelfb_getcolreg, info);
1517 	}
1518 
1519 	switchmode = (con != dinfo->currcon);
1520 
1521 	dinfo->currcon = con;
1522 	dinfo->vc_mode = con >= 0 ? vt_cons[con]->vc_mode : KD_TEXT;
1523 	dinfo->currcon_display = disp;
1524 	disp->var.activate = FB_ACTIVATE_NOW;
1525 
1526 	if (switchmode) {
1527 		intelfb_set_var(&disp->var, con, info);
1528 		intelfb_do_install_cmap(con, info);
1529 	}
1530 
1531 	return 0;
1532 }
1533 
1534 /* Called whenever there's panning. */
1535 static int
intelfb_updatevar(int con,struct fb_info * info)1536 intelfb_updatevar(int con, struct fb_info *info)
1537 {
1538 	struct display *disp = GET_DISP(info, con);
1539 
1540 	DBG_MSG("intelfb_updatevar\n");
1541 
1542 	if (con < 0)
1543 		return -EINVAL;
1544 	else
1545 		return intelfbhw_pan_display(&disp->var, con, info);
1546 }
1547 
1548 static int
intelfb_blank(int blank,struct fb_info * info)1549 intelfb_blank(int blank, struct fb_info *info)
1550 {
1551 	intelfbhw_do_blank(blank, info);
1552 	return 0;
1553 }
1554 
1555 static int
intelfb_getcolreg(unsigned regno,unsigned * red,unsigned * green,unsigned * blue,unsigned * transp,struct fb_info * info)1556 intelfb_getcolreg(unsigned regno, unsigned *red, unsigned *green,
1557 		  unsigned *blue, unsigned *transp, struct fb_info *info)
1558 {
1559 	struct intelfb_info *dinfo = GET_DINFO(info);
1560 
1561 #if 0
1562 	DBG_MSG("intelfb_getcolreg\n");
1563 #endif
1564 
1565 	if (regno > 255)
1566 		return 1;
1567 
1568 	*red = (dinfo->palette[regno].red<<8) | dinfo->palette[regno].red;
1569 	*green = (dinfo->palette[regno].green<<8) | dinfo->palette[regno].green;
1570 	*blue = (dinfo->palette[regno].blue<<8) | dinfo->palette[regno].blue;
1571 	*transp = 0;
1572 
1573 	return 0;
1574 }
1575 
1576 static int
intelfb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)1577 intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1578 		  unsigned blue, unsigned transp, struct fb_info *info)
1579 {
1580 	struct intelfb_info *dinfo = GET_DINFO(info);
1581 
1582 #if 1
1583 	DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
1584 #endif
1585 
1586 	if ((dinfo->depth > 8 && regno > 16) || regno > 255)
1587 		return 1;
1588 
1589 	switch (dinfo->depth) {
1590 #ifdef FBCON_HAS_CFB8
1591 	case 8:
1592 		{
1593 			red >>= 8;
1594 			green >>= 8;
1595 			blue >>= 8;
1596 
1597 			dinfo->palette[regno].red = red;
1598 			dinfo->palette[regno].green = green;
1599 			dinfo->palette[regno].blue = blue;
1600 
1601 			intelfbhw_setcolreg(dinfo, regno, red, green, blue,
1602 					    transp);
1603 		}
1604 		break;
1605 #endif
1606 #ifdef FBCON_HAS_CFB16
1607 	case 15:
1608 		dinfo->con_cmap.cfb16[regno] = ((red & 0xf800) >>  1) |
1609 					       ((green & 0xf800) >>  6) |
1610 					       ((blue & 0xf800) >> 11);
1611 		break;
1612 	case 16:
1613 		dinfo->con_cmap.cfb16[regno] = (red & 0xf800) |
1614 					       ((green & 0xfc00) >>  5) |
1615 					       ((blue  & 0xf800) >> 11);
1616 		break;
1617 #endif
1618 #ifdef FBCON_HAS_CFB32
1619 	case 24:
1620 		dinfo->con_cmap.cfb32[regno] = ((red & 0xff00) << 8) |
1621 					       (green & 0xff00) |
1622 					       ((blue  & 0xff00) >> 8);
1623 		break;
1624 #endif
1625 	}
1626 	return 0;
1627 }
1628 
1629 /* Convert a mode to a var, also making the bpp a supported value. */
1630 static void
mode_to_var(const struct fb_videomode * mode,struct fb_var_screeninfo * var,u32 bpp)1631 mode_to_var(const struct fb_videomode *mode, struct fb_var_screeninfo *var,
1632 	    u32 bpp)
1633 {
1634 	if (!mode || !var)
1635 		return;
1636 
1637 	var->xres = mode->xres;
1638 	var->yres = mode->yres;
1639 	var->xres_virtual = mode->xres;
1640 	var->yres_virtual = mode->yres;
1641 	var->xoffset = 0;
1642 	var->yoffset = 0;
1643 	if (bpp <= 8)
1644 		var->bits_per_pixel = 8;
1645 	else if (bpp <= 16) {
1646 		if (bpp == 16)
1647 			var->green.length = 6;
1648 		var->bits_per_pixel = 16;
1649 	} else if (bpp <= 32)
1650 		var->bits_per_pixel = 32;
1651 	else {
1652 		WRN_MSG("var_to_mode: bad bpp: %d\n", bpp);
1653 		var->bits_per_pixel = bpp;
1654 	}
1655 	var->pixclock = mode->pixclock;
1656 	var->left_margin = mode->left_margin;
1657 	var->right_margin = mode->right_margin;
1658 	var->upper_margin = mode->upper_margin;
1659 	var->lower_margin = mode->lower_margin;
1660 	var->hsync_len = mode->hsync_len;
1661 	var->vsync_len = mode->vsync_len;
1662 	var->sync = mode->sync;
1663 	var->vmode = mode->vmode;
1664 	var->width = -1;
1665 	var->height = -1;
1666 }
1667 
1668 static int
intelfb_set_mode(struct intelfb_info * dinfo,struct fb_var_screeninfo * var,struct display * disp,int blank)1669 intelfb_set_mode(struct intelfb_info *dinfo, struct fb_var_screeninfo *var,
1670 		 struct display *disp, int blank)
1671 {
1672 	struct intelfb_hwstate hw;
1673 
1674 	DBG_MSG("intelfb_set_mode (%dx%d-%d)\n", var->xres, var->yres,
1675 		intelfb_var_to_depth(var));
1676 
1677 	memcpy(&hw, &dinfo->save_state, sizeof(hw));
1678 	if (intelfbhw_mode_to_hw(dinfo, &hw, var))
1679 		return -EINVAL;
1680 	intelfbhw_print_hw_state(dinfo, &hw);
1681 	if (intelfbhw_program_mode(dinfo, &hw, blank))
1682 		return -EINVAL;
1683 
1684 	update_dinfo(dinfo, var, disp);
1685 
1686 	return 0;
1687 }
1688 
1689 static void
intelfb_do_install_cmap(int con,struct fb_info * info)1690 intelfb_do_install_cmap(int con, struct fb_info *info)
1691 {
1692 	struct intelfb_info *dinfo = GET_DINFO(info);
1693 	struct display *disp;
1694 
1695 	if (con != dinfo->currcon)
1696 		return;
1697 
1698 	disp = GET_DISP(info, con);
1699 	if (disp->cmap.len)
1700 		FB_SET_CMAP(&disp->cmap, 1, intelfb_setcolreg, info);
1701 	else {
1702 		int size = (disp->var.bits_per_pixel > 8) ? 16 : 256;
1703 		FB_SET_CMAP(fb_default_cmap(size), 1, intelfb_setcolreg, info);
1704 	}
1705 }
1706 
1707 static void
check_vc_mode(struct intelfb_info * dinfo)1708 check_vc_mode(struct intelfb_info *dinfo)
1709 {
1710 #if VERBOSE > 1
1711 	DBG_MSG("check_vc_mode\n");
1712 #endif
1713 
1714 	if (dinfo->currcon >= 0) {
1715 		if (vt_cons[dinfo->currcon]->vc_mode != dinfo->vc_mode) {
1716 			dinfo->vc_mode = vt_cons[dinfo->currcon]->vc_mode;
1717 			if (dinfo->vc_mode == KD_GRAPHICS) {
1718 				DBG_MSG("vc_mode changed to KD_GRAPHICS\n");
1719 				intelfbhw_2d_stop(dinfo);
1720 				if (dinfo->cursor.enabled) {
1721 					intelfbhw_cursor_hide(dinfo);
1722 					dinfo->cursor.enabled = 1;
1723 				}
1724 			} else {
1725 				DBG_MSG("vc_mode changed to KD_TEXT\n");
1726 				intelfbhw_2d_start(dinfo);
1727 #if 0
1728 				if (dinfo->cursor.enabled)
1729 					intelfbhw_cursor_show(dinfo);
1730 #endif
1731 			}
1732 		}
1733 	}
1734 }
1735 
1736 static void
fbcon_intelfb_setup(struct display * p)1737 fbcon_intelfb_setup(struct display *p)
1738 {
1739 	struct intelfb_info *dinfo = GET_DINFO(p->fb_info);
1740 
1741 	DBG_MSG("fbcon_intelfb_setup: accel: %d bpp: %d\n",
1742 		TEXT_ACCEL(dinfo, &p->var), p->var.bits_per_pixel);
1743 
1744 	switch (p->var.bits_per_pixel) {
1745 #ifdef FBCON_HAS_CFB8
1746 	case 8:
1747 		fbcon_cfb8.setup(p);
1748 		break;
1749 #endif
1750 #ifdef FBCON_HAS_CFB16
1751 	case 16:
1752 		fbcon_cfb16.setup(p);
1753 		break;
1754 #endif
1755 #ifdef FBCON_HAS_CFB32
1756 	case 32:
1757 		fbcon_cfb32.setup(p);
1758 		break;
1759 #endif
1760 	}
1761 }
1762 
1763 static void
fbcon_intelfb_bmove(struct display * p,int sy,int sx,int dy,int dx,int height,int width)1764 fbcon_intelfb_bmove(struct display *p, int sy, int sx, int dy, int dx,
1765 		    int height, int width)
1766 {
1767 	struct intelfb_info *dinfo = GET_DINFO(p->fb_info);
1768 
1769 #if VERBOSE > 0
1770 	DBG_MSG("fbcon_intelfb_bmove: accel: %d, bpp: %d\n",
1771 		TEXT_ACCEL(dinfo, &p->var), p->var.bits_per_pixel);
1772 #endif
1773 
1774 	if (TEXT_ACCEL(dinfo, &p->var)) {
1775 		intelfbhw_do_bitblt(dinfo, fontwidth(p) * sx,
1776 				    fontheight(p) * sy,
1777 				    fontwidth(p) * dx, fontheight(p) * dy,
1778 				    fontwidth(p) * width,
1779 				    fontheight(p) * height,
1780 				    dinfo->pitch, p->var.bits_per_pixel);
1781 		intelfbhw_do_sync(dinfo);
1782 		return;
1783 	}
1784 
1785 	/* Non-accel fallback */
1786 	switch (p->var.bits_per_pixel) {
1787 #ifdef FBCON_HAS_CFB8
1788 	case 8:
1789 		fbcon_cfb8.bmove(p, sy, sx, dy, dx, height, width);
1790 		break;
1791 #endif
1792 #ifdef FBCON_HAS_CFB16
1793 	case 16:
1794 		fbcon_cfb16.bmove(p, sy, sx, dy, dx, height, width);
1795 		break;
1796 #endif
1797 #ifdef FBCON_HAS_CFB32
1798 	case 32:
1799 		fbcon_cfb32.bmove(p, sy, sx, dy, dx, height, width);
1800 		break;
1801 #endif
1802 	}
1803 }
1804 
1805 static void
fbcon_intelfb_clear(struct vc_data * conp,struct display * p,int sy,int sx,int height,int width)1806 fbcon_intelfb_clear(struct vc_data *conp, struct display *p, int sy, int sx,
1807 		    int height, int width)
1808 {
1809 	struct intelfb_info *dinfo = GET_DINFO(p->fb_info);
1810 
1811 #if VERBOSE > 0
1812 	DBG_MSG("fbcon_intelfb_clear: accel: %d, bpp: %d\n",
1813 		TEXT_ACCEL(dinfo, &p->var), p->var.bits_per_pixel);
1814 #endif
1815 
1816 	if (TEXT_ACCEL(dinfo, &p->var)) {
1817 		u32 bg = 0;
1818 
1819 		switch (p->var.bits_per_pixel) {
1820 		case 8:
1821 			bg = attr_bgcol_ec(p, conp);
1822 			break;
1823 		case 16:
1824 			bg = ((u16 *)p->dispsw_data)[attr_bgcol_ec(p, conp)];
1825 			break;
1826 		case 32:
1827 			bg = ((u32 *)p->dispsw_data)[attr_bgcol_ec(p, conp)];
1828 			break;
1829 		}
1830 		intelfbhw_do_fillrect(dinfo, fontwidth(p) * sx,
1831 				      fontheight(p) * sy, fontwidth(p) * width,
1832 				      fontheight(p) * height, bg, dinfo->pitch,
1833 				      p->var.bits_per_pixel, PAT_ROP_GXCOPY);
1834 		intelfbhw_do_sync(dinfo);
1835 		return;
1836 	}
1837 
1838 	/* Non-accel fallback */
1839 	switch (p->var.bits_per_pixel) {
1840 #ifdef FBCON_HAS_CFB8
1841 	case 8:
1842 		fbcon_cfb8.clear(conp, p, sy, sx, height, width);
1843 		break;
1844 #endif
1845 #ifdef FBCON_HAS_CFB16
1846 	case 16:
1847 		fbcon_cfb16.clear(conp, p, sy, sx, height, width);
1848 		break;
1849 #endif
1850 #ifdef FBCON_HAS_CFB32
1851 	case 32:
1852 		fbcon_cfb32.clear(conp, p, sy, sx, height, width);
1853 		break;
1854 #endif
1855 	}
1856 }
1857 
1858 static void
fbcon_intelfb_putc(struct vc_data * conp,struct display * p,int c,int yy,int xx)1859 fbcon_intelfb_putc(struct vc_data *conp, struct display *p, int c,
1860 		   int yy, int xx)
1861 {
1862 	struct intelfb_info *dinfo = GET_DINFO(p->fb_info);
1863 
1864 #if VERBOSE > 0
1865 	DBG_MSG("fbcon_intelfb_putc: accel: %d, bpp: %d\n",
1866 		TEXT_ACCEL(dinfo, &p->var), p->var.bits_per_pixel);
1867 #endif
1868 
1869 	/* intelfbhw_do_drawglyph() has problems with the 852GM/855GM */
1870 	if (TEXT_ACCEL(dinfo, &p->var) && USE_DRAWGLYPH(dinfo)) {
1871 		u32 bg = 0, fg = 0;
1872 		u8 *cdat;
1873 		int fw;
1874 
1875 		switch (p->var.bits_per_pixel) {
1876 		case 8:
1877 			bg = attr_bgcol(p, c);
1878 			fg = attr_fgcol(p, c);
1879 			break;
1880 		case 16:
1881 			bg = ((u16 *)p->dispsw_data)[attr_bgcol(p, c)];
1882 			fg = ((u16 *)p->dispsw_data)[attr_fgcol(p, c)];
1883 			break;
1884 		case 32:
1885 			bg = ((u32 *)p->dispsw_data)[attr_bgcol(p, c)];
1886 			fg = ((u32 *)p->dispsw_data)[attr_fgcol(p, c)];
1887 			break;
1888 		}
1889 
1890 		fw = ROUND_UP_TO(fontwidth(p), 8) / 8;
1891 		cdat = p->fontdata + (c & p->charmask) * fontheight(p) * fw;
1892 
1893 		if (intelfbhw_do_drawglyph(dinfo, fg, bg, fontwidth(p),
1894 					   fontheight(p), cdat,
1895 					   xx * fontwidth(p),
1896 					   yy * fontheight(p), dinfo->pitch,
1897 					   p->var.bits_per_pixel)) {
1898 			intelfbhw_do_sync(dinfo);
1899 			return;
1900 		}
1901 	}
1902 
1903 	if (TEXT_ACCEL(dinfo, &p->var))
1904 		intelfbhw_do_sync(dinfo);
1905 
1906 	/* Non-accel fallback */
1907 	switch (p->var.bits_per_pixel) {
1908 #ifdef FBCON_HAS_CFB8
1909 	case 8:
1910 		fbcon_cfb8.putc(conp, p, c, yy, xx);
1911 		break;
1912 #endif
1913 #ifdef FBCON_HAS_CFB16
1914 	case 16:
1915 		fbcon_cfb16.putc(conp, p, c, yy, xx);
1916 		break;
1917 #endif
1918 #ifdef FBCON_HAS_CFB32
1919 	case 32:
1920 		fbcon_cfb32.putc(conp, p, c, yy, xx);
1921 		break;
1922 #endif
1923 	}
1924 }
1925 
1926 static void
fbcon_intelfb_putcs(struct vc_data * conp,struct display * p,const unsigned short * s,int count,int yy,int xx)1927 fbcon_intelfb_putcs(struct vc_data *conp, struct display *p,
1928 		    const unsigned short *s, int count, int yy, int xx)
1929 {
1930 	struct intelfb_info *dinfo = GET_DINFO(p->fb_info);
1931 
1932 #if VERBOSE > 0
1933 	DBG_MSG("fbcon_intelfb_putcs: accel: %d, bpp: %d, (%d,%d) %d\n",
1934 		TEXT_ACCEL(dinfo, &p->var), p->var.bits_per_pixel,
1935 		xx, yy, count);
1936 #endif
1937 
1938 	if (TEXT_ACCEL(dinfo, &p->var) && USE_DRAWGLYPH(dinfo)) {
1939 		u32 bg = 0, fg = 0;
1940 		u8 *cdat;
1941 		int fw;
1942 		u16 c = scr_readw(s);
1943 
1944 		switch (p->var.bits_per_pixel) {
1945 		case 8:
1946 			bg = attr_bgcol(p, c);
1947 			fg = attr_fgcol(p, c);
1948 			break;
1949 		case 16:
1950 			bg = ((u16 *)p->dispsw_data)[attr_bgcol(p, c)];
1951 			fg = ((u16 *)p->dispsw_data)[attr_fgcol(p, c)];
1952 			break;
1953 		case 32:
1954 			bg = ((u32 *)p->dispsw_data)[attr_bgcol(p, c)];
1955 			fg = ((u32 *)p->dispsw_data)[attr_fgcol(p, c)];
1956 			break;
1957 		}
1958 		fw = ROUND_UP_TO(fontwidth(p), 8) / 8;
1959 		while (count--) {
1960 			cdat = p->fontdata + (scr_readw(s++) & p->charmask) *
1961 			       fontheight(p) * fw;
1962 
1963 			intelfbhw_do_drawglyph(dinfo, fg, bg, fontwidth(p),
1964 					       fontheight(p), cdat,
1965 					       xx * fontwidth(p),
1966 					       yy * fontheight(p),
1967 					       dinfo->pitch,
1968 					       p->var.bits_per_pixel);
1969 			xx++;
1970 		}
1971 		intelfbhw_do_sync(dinfo);
1972 		return;
1973 	}
1974 
1975 	if (TEXT_ACCEL(dinfo, &p->var))
1976 		intelfbhw_do_sync(dinfo);
1977 
1978 	/* Non-accel fallback */
1979 	switch (p->var.bits_per_pixel) {
1980 #ifdef FBCON_HAS_CFB8
1981 	case 8:
1982 		fbcon_cfb8.putcs(conp, p, s, count, yy, xx);
1983 		break;
1984 #endif
1985 #ifdef FBCON_HAS_CFB16
1986 	case 16:
1987 		fbcon_cfb16.putcs(conp, p, s, count, yy, xx);
1988 		break;
1989 #endif
1990 #ifdef FBCON_HAS_CFB32
1991 	case 32:
1992 		fbcon_cfb32.putcs(conp, p, s, count, yy, xx);
1993 		break;
1994 #endif
1995 	}
1996 }
1997 
1998 static void
fbcon_intelfb_revc(struct display * p,int xx,int yy)1999 fbcon_intelfb_revc(struct display *p, int xx, int yy)
2000 {
2001 	struct intelfb_info *dinfo = GET_DINFO(p->fb_info);
2002 
2003 #if VERBOSE > 0
2004 	DBG_MSG("fbcon_intelfb_revc: accel: %d, bpp: %d\n",
2005 		TEXT_ACCEL(dinfo, &p->var), p->var.bits_per_pixel);
2006 #endif
2007 
2008 	if (TEXT_ACCEL(dinfo, &p->var)) {
2009 		int bpp = p->var.bits_per_pixel;
2010 		u32 xor_mask = bpp == 8 ? 0x0f : 0xffffffff;
2011 
2012 		intelfbhw_do_fillrect(dinfo, xx * fontwidth(p),
2013 				      yy * fontheight(p), fontwidth(p),
2014 				      fontheight(p), xor_mask, dinfo->pitch,
2015 				      bpp, PAT_ROP_GXXOR);
2016 		intelfbhw_do_sync(dinfo);
2017 		return;
2018 	}
2019 
2020 	/* Non-accel fallback */
2021 	switch (p->var.bits_per_pixel) {
2022 #ifdef FBCON_HAS_CFB8
2023 	case 8:
2024 		fbcon_cfb8.revc(p, xx, yy);
2025 		break;
2026 #endif
2027 #ifdef FBCON_HAS_CFB16
2028 	case 16:
2029 		fbcon_cfb16.revc(p, xx, yy);
2030 		break;
2031 #endif
2032 #ifdef FBCON_HAS_CFB32
2033 	case 32:
2034 		fbcon_cfb32.revc(p, xx, yy);
2035 		break;
2036 #endif
2037 	}
2038 }
2039 
2040 static void
fbcon_intelfb_clear_margins(struct vc_data * conp,struct display * p,int bottom_only)2041 fbcon_intelfb_clear_margins(struct vc_data *conp, struct display *p,
2042 			    int bottom_only)
2043 {
2044 	struct intelfb_info *dinfo = GET_DINFO(p->fb_info);
2045 
2046 #if VERBOSE > 0
2047 	DBG_MSG("fbcon_intelfb_clear_margins: accel: %d, bpp: %d\n",
2048 		TEXT_ACCEL(dinfo, &p->var), p->var.bits_per_pixel);
2049 #endif
2050 
2051 	if (TEXT_ACCEL(dinfo, &p->var)) {
2052 		u32 cw, ch, rw, bh, rs, bs;
2053 
2054 		cw = fontwidth(p);
2055 		ch = fontheight(p);
2056 		rw = p->var.xres % cw;
2057 		bh = p->var.yres % ch;
2058 		rs = p->var.xres - rw;
2059 		bs = p->var.yres - bh;
2060 
2061 		if (!bottom_only && rw) {
2062 			intelfbhw_do_fillrect(dinfo, p->var.xoffset + rs, 0,
2063 					      rw, p->var.yres_virtual, 0,
2064 					      dinfo->pitch,
2065 					      p->var.bits_per_pixel,
2066 					      PAT_ROP_GXCOPY);
2067 		}
2068 		if (bh) {
2069 			intelfbhw_do_fillrect(dinfo, p->var.xoffset,
2070 					      p->var.yoffset + bs, rs, bh, 0,
2071 					      dinfo->pitch,
2072 					      p->var.bits_per_pixel,
2073 					      PAT_ROP_GXCOPY);
2074 		}
2075 		if ((!bottom_only && rw) || bh) {
2076 			intelfbhw_do_sync(dinfo);
2077 		}
2078 		return;
2079 	}
2080 
2081 	/* Non-accel fallback */
2082 	switch (p->var.bits_per_pixel) {
2083 #ifdef FBCON_HAS_CFB8
2084 	case 8:
2085 		fbcon_cfb8.clear_margins(conp, p, bottom_only);
2086 		break;
2087 #endif
2088 #ifdef FBCON_HAS_CFB16
2089 	case 16:
2090 		fbcon_cfb16.clear_margins(conp, p, bottom_only);
2091 		break;
2092 #endif
2093 #ifdef FBCON_HAS_CFB32
2094 	case 32:
2095 		fbcon_cfb32.clear_margins(conp, p, bottom_only);
2096 		break;
2097 #endif
2098 	}
2099 }
2100 
2101 void
intelfb_create_cursor_shape(struct intelfb_info * dinfo,struct display * disp)2102 intelfb_create_cursor_shape(struct intelfb_info *dinfo, struct display *disp)
2103 {
2104 	u32 h, cu, cd;
2105 
2106 	DBG_MSG("intelfb_create_cursor_shape\n");
2107 
2108 	h = fontheight(disp);
2109 	cd = h;
2110 	if (cd >= 10)
2111 		cd --;
2112 	dinfo->cursor.type = disp->conp->vc_cursor_type & CUR_HWMASK;
2113 	switch (dinfo->cursor.type) {
2114 	case CUR_NONE:
2115 		cu = cd;
2116 		break;
2117 	case CUR_UNDERLINE:
2118 		cu = cd - 2;
2119 		break;
2120 	case CUR_LOWER_THIRD:
2121 		cu = (h * 2) / 3;
2122 		break;
2123 	case CUR_LOWER_HALF:
2124 		cu = h / 2;
2125 		break;
2126 	case CUR_TWO_THIRDS:
2127 		cu = h / 3;
2128 		break;
2129 	case CUR_BLOCK:
2130 	default:
2131 		cu = 0;
2132 		cd = h;
2133 		break;
2134 	}
2135 	dinfo->cursor.w = fontwidth(disp);
2136 	dinfo->cursor.u = cu;
2137 	dinfo->cursor.d = cd;
2138 }
2139 
2140 static void
intelfb_flashcursor(unsigned long ptr)2141 intelfb_flashcursor(unsigned long ptr)
2142 {
2143 	struct intelfb_info *dinfo = (struct intelfb_info *)ptr;
2144 	unsigned long flags;
2145 
2146 #if VERBOSE > 2
2147 	DBG_MSG("intelfb_flashcursor\n");
2148 #endif
2149 
2150 	spin_lock_irqsave(&dinfo->DAClock, flags);
2151 	if (dinfo->cursor.enabled) {
2152 		if (dinfo->cursor.on)
2153 			intelfbhw_cursor_hide(dinfo);
2154 		else
2155 			intelfbhw_cursor_show(dinfo);
2156 		dinfo->cursor.enabled = 1;
2157 	}
2158 	dinfo->cursor.timer.expires = jiffies + HZ / 2;
2159 	add_timer(&dinfo->cursor.timer);
2160 	spin_unlock_irqrestore(&dinfo->DAClock, flags);
2161 }
2162 
2163 static void
fbcon_intelfb_cursor(struct display * disp,int mode,int x,int y)2164 fbcon_intelfb_cursor(struct display *disp, int mode, int x, int y)
2165 {
2166 	unsigned long flags = 0;
2167 	struct intelfb_info *dinfo = GET_DINFO(disp->fb_info);
2168 
2169 #if VERBOSE > 0
2170 	DBG_MSG("fbcon_intelfb_cursor, mode is %d (%d)\n",
2171 		mode, dinfo->cursor.state);
2172 #endif
2173 
2174 	/*
2175 	 * This appears to be the first place that we can detect changes
2176 	 * in vc_mode.  We need to know this so that acceleration can be
2177 	 * enabled or disabled so that it won't interfere with an XFree86
2178 	 * server.
2179 	 */
2180 	check_vc_mode(dinfo);
2181 
2182 	if (!dinfo->hwcursor) {
2183 		disp->dispsw->cursor = NULL;
2184 		fb_con.con_cursor(disp->conp, mode);
2185 		disp->dispsw->cursor = fbcon_intelfb_cursor;
2186 		return;
2187 	}
2188 
2189 	if (mode == CM_ERASE) {
2190 		if (dinfo->cursor.state != CM_ERASE) {
2191 			spin_lock_irqsave(&dinfo->DAClock, flags);
2192 			dinfo->cursor.state = CM_ERASE;
2193 			intelfbhw_cursor_hide(dinfo);
2194 			del_timer(&dinfo->cursor.timer);
2195 			spin_unlock_irqrestore(&dinfo->DAClock, flags);
2196 		}
2197 		return;
2198 	}
2199 	if ((disp->conp->vc_cursor_type & CUR_HWMASK) != dinfo->cursor.type)
2200 		intelfbhw_cursor_load(dinfo, disp);
2201 	x *= fontwidth(disp);
2202 	y *= fontheight(disp);
2203 	y -= disp->var.yoffset;
2204 	x -= disp->var.xoffset;
2205 	spin_lock_irqsave(&dinfo->DAClock, flags);
2206 	if (x != dinfo->cursor.x || y != dinfo->cursor.y ||
2207 	    dinfo->cursor.redraw) {
2208 #if 1
2209 		intelfbhw_cursor_hide(dinfo);
2210 #endif
2211 		intelfbhw_cursor_setcolor(dinfo, 0, 0xffffff);
2212 		intelfbhw_cursor_setpos(dinfo, x, y);
2213 	}
2214 	dinfo->cursor.state = CM_DRAW;
2215 	mod_timer(&dinfo->cursor.timer, jiffies + HZ / 2);
2216 	intelfbhw_cursor_show(dinfo);
2217 	spin_unlock_irqrestore(&dinfo->DAClock, flags);
2218 }
2219 
2220