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