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