blob: 9e83664e345cf11e05abe86705719999028bc230 [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"
135
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);
Adrian Bunk14c6f522005-05-01 08:59:23 -0700139static int intelfb_check_var(struct fb_var_screeninfo *var,
140 struct fb_info *info);
141static int intelfb_set_par(struct fb_info *info);
142static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
143 unsigned blue, unsigned transp,
144 struct fb_info *info);
145
146static int intelfb_blank(int blank, struct fb_info *info);
147static int intelfb_pan_display(struct fb_var_screeninfo *var,
148 struct fb_info *info);
149
150static void intelfb_fillrect(struct fb_info *info,
151 const struct fb_fillrect *rect);
152static void intelfb_copyarea(struct fb_info *info,
153 const struct fb_copyarea *region);
154static void intelfb_imageblit(struct fb_info *info,
155 const struct fb_image *image);
156static int intelfb_cursor(struct fb_info *info,
157 struct fb_cursor *cursor);
158
159static int intelfb_sync(struct fb_info *info);
160
Christoph Hellwig67a66802006-01-14 13:21:25 -0800161static int intelfb_ioctl(struct fb_info *info,
162 unsigned int cmd, unsigned long arg);
Adrian Bunk14c6f522005-05-01 08:59:23 -0700163
164static int __devinit intelfb_pci_register(struct pci_dev *pdev,
165 const struct pci_device_id *ent);
166static void __devexit intelfb_pci_unregister(struct pci_dev *pdev);
167static int __devinit intelfb_set_fbinfo(struct intelfb_info *dinfo);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
170 * Limiting the class to PCI_CLASS_DISPLAY_VGA prevents function 1 of the
171 * mobile chipsets from being registered.
172 */
173#if DETECT_VGA_CLASS_ONLY
174#define INTELFB_CLASS_MASK ~0 << 8
175#else
176#define INTELFB_CLASS_MASK 0
177#endif
178
179static struct pci_device_id intelfb_pci_table[] __devinitdata = {
180 { 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 },
181 { 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 },
182 { 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 },
183 { 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 },
184 { 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 -0800185 { 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 +1100186 { 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 +1100187 { 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 -0700188 { 0, }
189};
190
191/* Global data */
192static int num_registered = 0;
193
194/* fb ops */
195static struct fb_ops intel_fb_ops = {
196 .owner = THIS_MODULE,
197 .fb_check_var = intelfb_check_var,
198 .fb_set_par = intelfb_set_par,
199 .fb_setcolreg = intelfb_setcolreg,
200 .fb_blank = intelfb_blank,
201 .fb_pan_display = intelfb_pan_display,
202 .fb_fillrect = intelfb_fillrect,
203 .fb_copyarea = intelfb_copyarea,
204 .fb_imageblit = intelfb_imageblit,
205 .fb_cursor = intelfb_cursor,
206 .fb_sync = intelfb_sync,
207 .fb_ioctl = intelfb_ioctl
208};
209
210/* PCI driver module table */
211static struct pci_driver intelfb_driver = {
Andrew Morton5a3b5892005-06-21 17:16:57 -0700212 .name = "intelfb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .id_table = intelfb_pci_table,
214 .probe = intelfb_pci_register,
215 .remove = __devexit_p(intelfb_pci_unregister)
216};
217
218/* Module description/parameters */
219MODULE_AUTHOR("David Dawes <dawes@tungstengraphics.com>, "
220 "Sylvain Meyer <sylvain.meyer@worldonline.fr>");
221MODULE_DESCRIPTION(
222 "Framebuffer driver for Intel(R) " SUPPORTED_CHIPSETS " chipsets");
223MODULE_LICENSE("Dual BSD/GPL");
224MODULE_DEVICE_TABLE(pci, intelfb_pci_table);
225
Patrick McManus346e3992005-05-28 15:51:46 -0700226static int accel = 1;
227static int vram = 4;
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800228static int hwcursor = 0;
Patrick McManus346e3992005-05-28 15:51:46 -0700229static int mtrr = 1;
230static int fixed = 0;
231static int noinit = 0;
232static int noregister = 0;
233static int probeonly = 0;
234static int idonly = 0;
235static int bailearly = 0;
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700236static int voffset = 48;
Patrick McManus346e3992005-05-28 15:51:46 -0700237static char *mode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239module_param(accel, bool, S_IRUGO);
James Simmons4c7ffe02005-09-09 13:04:31 -0700240MODULE_PARM_DESC(accel, "Enable hardware acceleration");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241module_param(vram, int, S_IRUGO);
242MODULE_PARM_DESC(vram, "System RAM to allocate to framebuffer in MiB");
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700243module_param(voffset, int, S_IRUGO);
244MODULE_PARM_DESC(voffset, "Offset of framebuffer in MiB");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245module_param(hwcursor, bool, S_IRUGO);
246MODULE_PARM_DESC(hwcursor, "Enable HW cursor");
247module_param(mtrr, bool, S_IRUGO);
248MODULE_PARM_DESC(mtrr, "Enable MTRR support");
249module_param(fixed, bool, S_IRUGO);
250MODULE_PARM_DESC(fixed, "Disable mode switching");
251module_param(noinit, bool, 0);
252MODULE_PARM_DESC(noinit, "Don't initialise graphics mode when loading");
253module_param(noregister, bool, 0);
254MODULE_PARM_DESC(noregister, "Don't register, just probe and exit (debug)");
255module_param(probeonly, bool, 0);
256MODULE_PARM_DESC(probeonly, "Do a minimal probe (debug)");
257module_param(idonly, bool, 0);
258MODULE_PARM_DESC(idonly, "Just identify without doing anything else (debug)");
259module_param(bailearly, bool, 0);
260MODULE_PARM_DESC(bailearly, "Bail out early, depending on value (debug)");
261module_param(mode, charp, S_IRUGO);
262MODULE_PARM_DESC(mode,
263 "Initial video mode \"<xres>x<yres>[-<depth>][@<refresh>]\"");
264
265#ifndef MODULE
266#define OPT_EQUAL(opt, name) (!strncmp(opt, name, strlen(name)))
267#define OPT_INTVAL(opt, name) simple_strtoul(opt + strlen(name), NULL, 0)
268#define OPT_STRVAL(opt, name) (opt + strlen(name))
269
270static __inline__ char *
271get_opt_string(const char *this_opt, const char *name)
272{
273 const char *p;
274 int i;
275 char *ret;
276
277 p = OPT_STRVAL(this_opt, name);
278 i = 0;
279 while (p[i] && p[i] != ' ' && p[i] != ',')
280 i++;
281 ret = kmalloc(i + 1, GFP_KERNEL);
282 if (ret) {
283 strncpy(ret, p, i);
284 ret[i] = '\0';
285 }
286 return ret;
287}
288
289static __inline__ int
290get_opt_int(const char *this_opt, const char *name, int *ret)
291{
292 if (!ret)
293 return 0;
294
295 if (!OPT_EQUAL(this_opt, name))
296 return 0;
297
298 *ret = OPT_INTVAL(this_opt, name);
299 return 1;
300}
301
302static __inline__ int
303get_opt_bool(const char *this_opt, const char *name, int *ret)
304{
305 if (!ret)
306 return 0;
307
308 if (OPT_EQUAL(this_opt, name)) {
309 if (this_opt[strlen(name)] == '=')
310 *ret = simple_strtoul(this_opt + strlen(name) + 1,
311 NULL, 0);
312 else
313 *ret = 1;
314 } else {
315 if (OPT_EQUAL(this_opt, "no") && OPT_EQUAL(this_opt + 2, name))
316 *ret = 0;
317 else
318 return 0;
319 }
320 return 1;
321}
322
323static int __init
324intelfb_setup(char *options)
325{
326 char *this_opt;
327
328 DBG_MSG("intelfb_setup\n");
329
330 if (!options || !*options) {
331 DBG_MSG("no options\n");
332 return 0;
333 } else
334 DBG_MSG("options: %s\n", options);
335
336 /*
337 * These are the built-in options analogous to the module parameters
338 * defined above.
339 *
340 * The syntax is:
341 *
342 * video=intelfb:[mode][,<param>=<val>] ...
343 *
344 * e.g.,
345 *
346 * video=intelfb:1024x768-16@75,accel=0
347 */
348
349 while ((this_opt = strsep(&options, ","))) {
350 if (!*this_opt)
351 continue;
352 if (get_opt_bool(this_opt, "accel", &accel))
353 ;
354 else if (get_opt_int(this_opt, "vram", &vram))
355 ;
356 else if (get_opt_bool(this_opt, "hwcursor", &hwcursor))
357 ;
358 else if (get_opt_bool(this_opt, "mtrr", &mtrr))
359 ;
360 else if (get_opt_bool(this_opt, "fixed", &fixed))
361 ;
362 else if (get_opt_bool(this_opt, "init", &noinit))
363 noinit = !noinit;
364 else if (OPT_EQUAL(this_opt, "mode="))
365 mode = get_opt_string(this_opt, "mode=");
366 else
367 mode = this_opt;
368 }
369
370 return 0;
371}
372
373#endif
374
375static int __init
376intelfb_init(void)
377{
378#ifndef MODULE
379 char *option = NULL;
380#endif
381
382 DBG_MSG("intelfb_init\n");
383
384 INF_MSG("Framebuffer driver for "
385 "Intel(R) " SUPPORTED_CHIPSETS " chipsets\n");
386 INF_MSG("Version " INTELFB_VERSION "\n");
387
388 if (idonly)
389 return -ENODEV;
390
391#ifndef MODULE
392 if (fb_get_options("intelfb", &option))
393 return -ENODEV;
394 intelfb_setup(option);
395#endif
396
397 return pci_register_driver(&intelfb_driver);
398}
399
400static void __exit
401intelfb_exit(void)
402{
403 DBG_MSG("intelfb_exit\n");
404 pci_unregister_driver(&intelfb_driver);
405}
406
407module_init(intelfb_init);
408module_exit(intelfb_exit);
409
410/***************************************************************
411 * mtrr support functions *
412 ***************************************************************/
413
414#ifdef CONFIG_MTRR
415static inline void __devinit set_mtrr(struct intelfb_info *dinfo)
416{
417 dinfo->mtrr_reg = mtrr_add(dinfo->aperture.physical,
418 dinfo->aperture.size, MTRR_TYPE_WRCOMB, 1);
419 if (dinfo->mtrr_reg < 0) {
420 ERR_MSG("unable to set MTRR\n");
421 return;
422 }
423 dinfo->has_mtrr = 1;
424}
425static inline void unset_mtrr(struct intelfb_info *dinfo)
426{
427 if (dinfo->has_mtrr)
428 mtrr_del(dinfo->mtrr_reg, dinfo->aperture.physical,
429 dinfo->aperture.size);
430}
431#else
432#define set_mtrr(x) WRN_MSG("MTRR is disabled in the kernel\n")
433
434#define unset_mtrr(x) do { } while (0)
435#endif /* CONFIG_MTRR */
436
437/***************************************************************
438 * driver init / cleanup *
439 ***************************************************************/
440
441static void
442cleanup(struct intelfb_info *dinfo)
443{
444 DBG_MSG("cleanup\n");
445
446 if (!dinfo)
447 return;
448
449 fb_dealloc_cmap(&dinfo->info->cmap);
450 kfree(dinfo->info->pixmap.addr);
451
452 if (dinfo->registered)
453 unregister_framebuffer(dinfo->info);
454
455 unset_mtrr(dinfo);
456
457 if (dinfo->fbmem_gart && dinfo->gtt_fb_mem) {
458 agp_unbind_memory(dinfo->gtt_fb_mem);
459 agp_free_memory(dinfo->gtt_fb_mem);
460 }
461 if (dinfo->gtt_cursor_mem) {
462 agp_unbind_memory(dinfo->gtt_cursor_mem);
463 agp_free_memory(dinfo->gtt_cursor_mem);
464 }
465 if (dinfo->gtt_ring_mem) {
466 agp_unbind_memory(dinfo->gtt_ring_mem);
467 agp_free_memory(dinfo->gtt_ring_mem);
468 }
469
470 if (dinfo->mmio_base)
471 iounmap((void __iomem *)dinfo->mmio_base);
472 if (dinfo->aperture.virtual)
473 iounmap((void __iomem *)dinfo->aperture.virtual);
474
Antonino A. Daplas4b3760c2005-12-19 15:11:40 +0800475 if (dinfo->flag & INTELFB_MMIO_ACQUIRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 release_mem_region(dinfo->mmio_base_phys, INTEL_REG_SIZE);
Antonino A. Daplas4b3760c2005-12-19 15:11:40 +0800477 if (dinfo->flag & INTELFB_FB_ACQUIRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 release_mem_region(dinfo->aperture.physical,
479 dinfo->aperture.size);
480 framebuffer_release(dinfo->info);
481}
482
483#define bailout(dinfo) do { \
484 DBG_MSG("bailout\n"); \
485 cleanup(dinfo); \
486 INF_MSG("Not going to register framebuffer, exiting...\n"); \
487 return -ENODEV; \
488} while (0)
489
490
491static int __devinit
492intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
493{
494 struct fb_info *info;
495 struct intelfb_info *dinfo;
James Simmons4c7ffe02005-09-09 13:04:31 -0700496 int i, err, dvo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int aperture_size, stolen_size;
498 struct agp_kern_info gtt_info;
499 int agp_memtype;
500 const char *s;
501 struct agp_bridge_data *bridge;
502 int aperture_bar = 0;
503 int mmio_bar = 1;
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700504 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 DBG_MSG("intelfb_pci_register\n");
507
508 num_registered++;
509 if (num_registered != 1) {
510 ERR_MSG("Attempted to register %d devices "
511 "(should be only 1).\n", num_registered);
512 return -ENODEV;
513 }
514
515 info = framebuffer_alloc(sizeof(struct intelfb_info), &pdev->dev);
516 if (!info) {
517 ERR_MSG("Could not allocate memory for intelfb_info.\n");
518 return -ENODEV;
519 }
520 if (fb_alloc_cmap(&info->cmap, 256, 1) < 0) {
521 ERR_MSG("Could not allocate cmap for intelfb_info.\n");
522 goto err_out_cmap;
523 return -ENODEV;
524 }
525
526 dinfo = info->par;
527 dinfo->info = info;
528 dinfo->fbops = &intel_fb_ops;
529 dinfo->pdev = pdev;
530
531 /* Reserve pixmap space. */
532 info->pixmap.addr = kmalloc(64 * 1024, GFP_KERNEL);
533 if (info->pixmap.addr == NULL) {
534 ERR_MSG("Cannot reserve pixmap memory.\n");
535 goto err_out_pixmap;
536 }
537 memset(info->pixmap.addr, 0, 64 * 1024);
538
539 /* set early this option because it could be changed by tv encoder
540 driver */
541 dinfo->fixed_mode = fixed;
542
543 /* Enable device. */
544 if ((err = pci_enable_device(pdev))) {
545 ERR_MSG("Cannot enable device.\n");
546 cleanup(dinfo);
547 return -ENODEV;
548 }
549
550 /* Set base addresses. */
Scott MacKenzie3a590262005-11-07 01:00:33 -0800551 if ((ent->device == PCI_DEVICE_ID_INTEL_915G) ||
Dave Airlie9639d5e2006-03-23 11:23:55 +1100552 (ent->device == PCI_DEVICE_ID_INTEL_915GM) ||
Dave Airlie9a906032006-03-23 21:53:05 +1100553 (ent->device == PCI_DEVICE_ID_INTEL_945G) ||
554 (ent->device == PCI_DEVICE_ID_INTEL_945GM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 aperture_bar = 2;
556 mmio_bar = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 dinfo->aperture.physical = pci_resource_start(pdev, aperture_bar);
559 dinfo->aperture.size = pci_resource_len(pdev, aperture_bar);
560 dinfo->mmio_base_phys = pci_resource_start(pdev, mmio_bar);
561 DBG_MSG("fb aperture: 0x%llx/0x%llx, MMIO region: 0x%llx/0x%llx\n",
562 (unsigned long long)pci_resource_start(pdev, aperture_bar),
563 (unsigned long long)pci_resource_len(pdev, aperture_bar),
564 (unsigned long long)pci_resource_start(pdev, mmio_bar),
565 (unsigned long long)pci_resource_len(pdev, mmio_bar));
566
567 /* Reserve the fb and MMIO regions */
568 if (!request_mem_region(dinfo->aperture.physical, dinfo->aperture.size,
569 INTELFB_MODULE_NAME)) {
570 ERR_MSG("Cannot reserve FB region.\n");
571 cleanup(dinfo);
572 return -ENODEV;
573 }
Antonino A. Daplas4b3760c2005-12-19 15:11:40 +0800574
575 dinfo->flag |= INTELFB_FB_ACQUIRED;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (!request_mem_region(dinfo->mmio_base_phys,
578 INTEL_REG_SIZE,
579 INTELFB_MODULE_NAME)) {
580 ERR_MSG("Cannot reserve MMIO region.\n");
581 cleanup(dinfo);
582 return -ENODEV;
583 }
584
Antonino A. Daplas4b3760c2005-12-19 15:11:40 +0800585 dinfo->flag |= INTELFB_MMIO_ACQUIRED;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Get the chipset info. */
588 dinfo->pci_chipset = pdev->device;
589
Dave Airlied0249602006-03-20 20:26:45 +1100590 if (intelfbhw_get_chipset(pdev, dinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 cleanup(dinfo);
592 return -ENODEV;
593 }
594
595 if (intelfbhw_get_memory(pdev, &aperture_size,&stolen_size)) {
596 cleanup(dinfo);
597 return -ENODEV;
598 }
599
600 INF_MSG("%02x:%02x.%d: %s, aperture size %dMB, "
601 "stolen memory %dkB\n",
602 pdev->bus->number, PCI_SLOT(pdev->devfn),
603 PCI_FUNC(pdev->devfn), dinfo->name,
604 BtoMB(aperture_size), BtoKB(stolen_size));
605
606 /* Set these from the options. */
607 dinfo->accel = accel;
608 dinfo->hwcursor = hwcursor;
609
610 if (NOACCEL_CHIPSET(dinfo) && dinfo->accel == 1) {
611 INF_MSG("Acceleration is not supported for the %s chipset.\n",
612 dinfo->name);
613 dinfo->accel = 0;
614 }
615
616 /* Framebuffer parameters - Use all the stolen memory if >= vram */
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800617 if (ROUND_UP_TO_PAGE(stolen_size) >= MB(vram)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 dinfo->fb.size = ROUND_UP_TO_PAGE(stolen_size);
619 dinfo->fbmem_gart = 0;
620 } else {
621 dinfo->fb.size = MB(vram);
622 dinfo->fbmem_gart = 1;
623 }
624
625 /* Allocate space for the ring buffer and HW cursor if enabled. */
626 if (dinfo->accel) {
627 dinfo->ring.size = RINGBUFFER_SIZE;
628 dinfo->ring_tail_mask = dinfo->ring.size - 1;
629 }
630 if (dinfo->hwcursor) {
631 dinfo->cursor.size = HW_CURSOR_SIZE;
632 }
633
634 /* Use agpgart to manage the GATT */
635 if (!(bridge = agp_backend_acquire(pdev))) {
636 ERR_MSG("cannot acquire agp\n");
637 cleanup(dinfo);
638 return -ENODEV;
639 }
640
641 /* get the current gatt info */
642 if (agp_copy_info(bridge, &gtt_info)) {
643 ERR_MSG("cannot get agp info\n");
644 agp_backend_release(bridge);
645 cleanup(dinfo);
646 return -ENODEV;
647 }
648
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800649 if (MB(voffset) < stolen_size)
650 offset = (stolen_size >> 12);
651 else
652 offset = ROUND_UP_TO_PAGE(MB(voffset))/GTT_PAGE_SIZE;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* set the mem offsets - set them after the already used pages */
655 if (dinfo->accel) {
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700656 dinfo->ring.offset = offset + gtt_info.current_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 if (dinfo->hwcursor) {
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700659 dinfo->cursor.offset = offset +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 + gtt_info.current_memory + (dinfo->ring.size >> 12);
661 }
662 if (dinfo->fbmem_gart) {
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700663 dinfo->fb.offset = offset +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 + gtt_info.current_memory + (dinfo->ring.size >> 12)
665 + (dinfo->cursor.size >> 12);
666 }
667
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800668 /* Allocate memories (which aren't stolen) */
Sylvain Meyer6bd49342005-08-15 21:27:13 +0800669 /* Map the fb and MMIO regions */
670 /* ioremap only up to the end of used aperture */
671 dinfo->aperture.virtual = (u8 __iomem *)ioremap_nocache
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800672 (dinfo->aperture.physical, ((offset + dinfo->fb.offset) << 12)
Sylvain Meyer6bd49342005-08-15 21:27:13 +0800673 + dinfo->fb.size);
674 if (!dinfo->aperture.virtual) {
675 ERR_MSG("Cannot remap FB region.\n");
676 cleanup(dinfo);
677 return -ENODEV;
678 }
679
680 dinfo->mmio_base =
681 (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
682 INTEL_REG_SIZE);
683 if (!dinfo->mmio_base) {
684 ERR_MSG("Cannot remap MMIO region.\n");
685 cleanup(dinfo);
686 return -ENODEV;
687 }
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (dinfo->accel) {
690 if (!(dinfo->gtt_ring_mem =
691 agp_allocate_memory(bridge, dinfo->ring.size >> 12,
692 AGP_NORMAL_MEMORY))) {
693 ERR_MSG("cannot allocate ring buffer memory\n");
694 agp_backend_release(bridge);
695 cleanup(dinfo);
696 return -ENOMEM;
697 }
698 if (agp_bind_memory(dinfo->gtt_ring_mem,
699 dinfo->ring.offset)) {
700 ERR_MSG("cannot bind ring buffer memory\n");
701 agp_backend_release(bridge);
702 cleanup(dinfo);
703 return -EBUSY;
704 }
705 dinfo->ring.physical = dinfo->aperture.physical
706 + (dinfo->ring.offset << 12);
707 dinfo->ring.virtual = dinfo->aperture.virtual
708 + (dinfo->ring.offset << 12);
709 dinfo->ring_head = dinfo->ring.virtual;
710 }
711 if (dinfo->hwcursor) {
712 agp_memtype = dinfo->mobile ? AGP_PHYSICAL_MEMORY
713 : AGP_NORMAL_MEMORY;
714 if (!(dinfo->gtt_cursor_mem =
715 agp_allocate_memory(bridge, dinfo->cursor.size >> 12,
716 agp_memtype))) {
717 ERR_MSG("cannot allocate cursor memory\n");
718 agp_backend_release(bridge);
719 cleanup(dinfo);
720 return -ENOMEM;
721 }
722 if (agp_bind_memory(dinfo->gtt_cursor_mem,
723 dinfo->cursor.offset)) {
724 ERR_MSG("cannot bind cursor memory\n");
725 agp_backend_release(bridge);
726 cleanup(dinfo);
727 return -EBUSY;
728 }
729 if (dinfo->mobile)
730 dinfo->cursor.physical
731 = dinfo->gtt_cursor_mem->physical;
732 else
733 dinfo->cursor.physical = dinfo->aperture.physical
734 + (dinfo->cursor.offset << 12);
735 dinfo->cursor.virtual = dinfo->aperture.virtual
736 + (dinfo->cursor.offset << 12);
737 }
738 if (dinfo->fbmem_gart) {
739 if (!(dinfo->gtt_fb_mem =
740 agp_allocate_memory(bridge, dinfo->fb.size >> 12,
741 AGP_NORMAL_MEMORY))) {
742 WRN_MSG("cannot allocate framebuffer memory - use "
743 "the stolen one\n");
744 dinfo->fbmem_gart = 0;
745 }
746 if (agp_bind_memory(dinfo->gtt_fb_mem,
747 dinfo->fb.offset)) {
748 WRN_MSG("cannot bind framebuffer memory - use "
749 "the stolen one\n");
750 dinfo->fbmem_gart = 0;
751 }
752 }
753
754 /* update framebuffer memory parameters */
755 if (!dinfo->fbmem_gart)
756 dinfo->fb.offset = 0; /* starts at offset 0 */
757 dinfo->fb.physical = dinfo->aperture.physical
758 + (dinfo->fb.offset << 12);
759 dinfo->fb.virtual = dinfo->aperture.virtual + (dinfo->fb.offset << 12);
760 dinfo->fb_start = dinfo->fb.offset << 12;
761
762 /* release agpgart */
763 agp_backend_release(bridge);
764
765 if (mtrr)
766 set_mtrr(dinfo);
767
768 DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%x)\n",
769 dinfo->fb.physical, dinfo->fb.offset, dinfo->fb.size,
770 (u32 __iomem ) dinfo->fb.virtual);
771 DBG_MSG("MMIO: 0x%x/0x%x (0x%x)\n",
772 dinfo->mmio_base_phys, INTEL_REG_SIZE,
773 (u32 __iomem) dinfo->mmio_base);
774 DBG_MSG("ring buffer: 0x%x/0x%x (0x%x)\n",
775 dinfo->ring.physical, dinfo->ring.size,
776 (u32 __iomem ) dinfo->ring.virtual);
777 DBG_MSG("HW cursor: 0x%x/0x%x (0x%x) (offset 0x%x) (phys 0x%x)\n",
778 dinfo->cursor.physical, dinfo->cursor.size,
779 (u32 __iomem ) dinfo->cursor.virtual, dinfo->cursor.offset,
780 dinfo->cursor.physical);
781
782 DBG_MSG("options: vram = %d, accel = %d, hwcursor = %d, fixed = %d, "
783 "noinit = %d\n", vram, accel, hwcursor, fixed, noinit);
784 DBG_MSG("options: mode = \"%s\"\n", mode ? mode : "");
785
786 if (probeonly)
787 bailout(dinfo);
788
789 /*
790 * Check if the LVDS port or any DVO ports are enabled. If so,
791 * don't allow mode switching
792 */
793 dvo = intelfbhw_check_non_crt(dinfo);
794 if (dvo) {
795 dinfo->fixed_mode = 1;
796 WRN_MSG("Non-CRT device is enabled ( ");
797 i = 0;
798 while (dvo) {
799 if (dvo & 1) {
800 s = intelfbhw_dvo_to_string(1 << i);
801 if (s)
802 printk("%s ", s);
803 }
804 dvo >>= 1;
805 ++i;
806 }
807 printk("). Disabling mode switching.\n");
808 }
809
810 if (bailearly == 1)
811 bailout(dinfo);
812
813 if (FIXED_MODE(dinfo) && ORIG_VIDEO_ISVGA != VIDEO_TYPE_VLFB) {
814 ERR_MSG("Video mode must be programmed at boot time.\n");
815 cleanup(dinfo);
816 return -ENODEV;
817 }
818
819 if (bailearly == 2)
820 bailout(dinfo);
821
822 /* Initialise dinfo and related data. */
823 /* If an initial mode was programmed at boot time, get its details. */
824 if (ORIG_VIDEO_ISVGA == VIDEO_TYPE_VLFB)
825 get_initial_mode(dinfo);
826
827 if (bailearly == 3)
828 bailout(dinfo);
829
830 if (FIXED_MODE(dinfo)) {
831 /* remap fb address */
832 update_dinfo(dinfo, &dinfo->initial_var);
833 }
834
835 if (bailearly == 4)
836 bailout(dinfo);
837
838
839 if (intelfb_set_fbinfo(dinfo)) {
840 cleanup(dinfo);
841 return -ENODEV;
842 }
843
844 if (bailearly == 5)
845 bailout(dinfo);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (bailearly == 6)
848 bailout(dinfo);
849
850 pci_set_drvdata(pdev, dinfo);
851
852 /* Save the initial register state. */
853 i = intelfbhw_read_hw_state(dinfo, &dinfo->save_state,
854 bailearly > 6 ? bailearly - 6 : 0);
855 if (i != 0) {
856 DBG_MSG("intelfbhw_read_hw_state returned %d\n", i);
857 bailout(dinfo);
858 }
859
860 intelfbhw_print_hw_state(dinfo, &dinfo->save_state);
861
862 if (bailearly == 18)
863 bailout(dinfo);
864
865 /* Cursor initialisation */
866 if (dinfo->hwcursor) {
867 intelfbhw_cursor_init(dinfo);
868 intelfbhw_cursor_reset(dinfo);
869 }
870
871 if (bailearly == 19)
872 bailout(dinfo);
873
874 /* 2d acceleration init */
875 if (dinfo->accel)
876 intelfbhw_2d_start(dinfo);
877
878 if (bailearly == 20)
879 bailout(dinfo);
880
881 if (noregister)
882 bailout(dinfo);
883
884 if (register_framebuffer(dinfo->info) < 0) {
885 ERR_MSG("Cannot register framebuffer.\n");
886 cleanup(dinfo);
887 return -ENODEV;
888 }
889
890 dinfo->registered = 1;
891
892 return 0;
893
894err_out_pixmap:
895 fb_dealloc_cmap(&info->cmap);
896err_out_cmap:
897 framebuffer_release(info);
898 return -ENODEV;
899}
900
901static void __devexit
902intelfb_pci_unregister(struct pci_dev *pdev)
903{
904 struct intelfb_info *dinfo = pci_get_drvdata(pdev);
905
906 DBG_MSG("intelfb_pci_unregister\n");
907
908 if (!dinfo)
909 return;
910
911 cleanup(dinfo);
912
913 pci_set_drvdata(pdev, NULL);
914}
915
916/***************************************************************
917 * helper functions *
918 ***************************************************************/
919
920int __inline__
921intelfb_var_to_depth(const struct fb_var_screeninfo *var)
922{
923 DBG_MSG("intelfb_var_to_depth: bpp: %d, green.length is %d\n",
924 var->bits_per_pixel, var->green.length);
925
926 switch (var->bits_per_pixel) {
927 case 16:
928 return (var->green.length == 6) ? 16 : 15;
929 case 32:
930 return 24;
931 default:
932 return var->bits_per_pixel;
933 }
934}
935
936
937static __inline__ int
938var_to_refresh(const struct fb_var_screeninfo *var)
939{
940 int xtot = var->xres + var->left_margin + var->right_margin +
941 var->hsync_len;
942 int ytot = var->yres + var->upper_margin + var->lower_margin +
943 var->vsync_len;
944
945 return (1000000000 / var->pixclock * 1000 + 500) / xtot / ytot;
946}
947
948/***************************************************************
949 * Various intialisation functions *
950 ***************************************************************/
951
952static void __devinit
953get_initial_mode(struct intelfb_info *dinfo)
954{
955 struct fb_var_screeninfo *var;
956 int xtot, ytot;
957
958 DBG_MSG("get_initial_mode\n");
959
960 dinfo->initial_vga = 1;
961 dinfo->initial_fb_base = screen_info.lfb_base;
962 dinfo->initial_video_ram = screen_info.lfb_size * KB(64);
963 dinfo->initial_pitch = screen_info.lfb_linelength;
964
965 var = &dinfo->initial_var;
966 memset(var, 0, sizeof(*var));
967 var->xres = screen_info.lfb_width;
968 var->yres = screen_info.lfb_height;
969 var->bits_per_pixel = screen_info.lfb_depth;
970 switch (screen_info.lfb_depth) {
971 case 15:
972 var->bits_per_pixel = 16;
973 break;
974 case 24:
975 var->bits_per_pixel = 32;
976 break;
977 }
978
979 DBG_MSG("Initial info: FB is 0x%x/0x%x (%d kByte)\n",
980 dinfo->initial_fb_base, dinfo->initial_video_ram,
981 BtoKB(dinfo->initial_video_ram));
982
983 DBG_MSG("Initial info: mode is %dx%d-%d (%d)\n",
984 var->xres, var->yres, var->bits_per_pixel,
985 dinfo->initial_pitch);
986
987 /* Dummy timing values (assume 60Hz) */
988 var->left_margin = (var->xres / 8) & 0xf8;
989 var->right_margin = 32;
990 var->upper_margin = 16;
991 var->lower_margin = 4;
992 var->hsync_len = (var->xres / 8) & 0xf8;
993 var->vsync_len = 4;
994
995 xtot = var->xres + var->left_margin +
996 var->right_margin + var->hsync_len;
997 ytot = var->yres + var->upper_margin +
998 var->lower_margin + var->vsync_len;
999 var->pixclock = 10000000 / xtot * 1000 / ytot * 100 / 60;
1000
1001 var->height = -1;
1002 var->width = -1;
1003
1004 if (var->bits_per_pixel > 8) {
1005 var->red.offset = screen_info.red_pos;
1006 var->red.length = screen_info.red_size;
1007 var->green.offset = screen_info.green_pos;
1008 var->green.length = screen_info.green_size;
1009 var->blue.offset = screen_info.blue_pos;
1010 var->blue.length = screen_info.blue_size;
1011 var->transp.offset = screen_info.rsvd_pos;
1012 var->transp.length = screen_info.rsvd_size;
1013 } else {
1014 var->red.length = 8;
1015 var->green.length = 8;
1016 var->blue.length = 8;
1017 }
1018}
1019
1020static int __devinit
1021intelfb_init_var(struct intelfb_info *dinfo)
1022{
1023 struct fb_var_screeninfo *var;
1024 int msrc = 0;
1025
1026 DBG_MSG("intelfb_init_var\n");
1027
1028 var = &dinfo->info->var;
1029 if (FIXED_MODE(dinfo)) {
1030 memcpy(var, &dinfo->initial_var,
1031 sizeof(struct fb_var_screeninfo));
1032 msrc = 5;
1033 } else {
1034 if (mode) {
1035 msrc = fb_find_mode(var, dinfo->info, mode,
1036 vesa_modes, VESA_MODEDB_SIZE,
1037 NULL, 0);
1038 if (msrc)
1039 msrc |= 8;
1040 }
1041 if (!msrc) {
1042 msrc = fb_find_mode(var, dinfo->info, PREFERRED_MODE,
1043 vesa_modes, VESA_MODEDB_SIZE,
1044 NULL, 0);
1045 }
1046 }
1047
1048 if (!msrc) {
1049 ERR_MSG("Cannot find a suitable video mode.\n");
1050 return 1;
1051 }
1052
1053 INF_MSG("Initial video mode is %dx%d-%d@%d.\n", var->xres, var->yres,
1054 var->bits_per_pixel, var_to_refresh(var));
1055
1056 DBG_MSG("Initial video mode is from %d.\n", msrc);
1057
1058#if ALLOCATE_FOR_PANNING
1059 /* Allow use of half of the video ram for panning */
1060 var->xres_virtual = var->xres;
1061 var->yres_virtual =
1062 dinfo->fb.size / 2 / (var->bits_per_pixel * var->xres);
1063 if (var->yres_virtual < var->yres)
1064 var->yres_virtual = var->yres;
1065#else
1066 var->yres_virtual = var->yres;
1067#endif
1068
1069 if (dinfo->accel)
1070 var->accel_flags |= FB_ACCELF_TEXT;
1071 else
1072 var->accel_flags &= ~FB_ACCELF_TEXT;
1073
1074 return 0;
1075}
1076
1077static int __devinit
1078intelfb_set_fbinfo(struct intelfb_info *dinfo)
1079{
1080 struct fb_info *info = dinfo->info;
1081
1082 DBG_MSG("intelfb_set_fbinfo\n");
1083
1084 info->flags = FBINFO_FLAG_DEFAULT;
1085 info->fbops = &intel_fb_ops;
1086 info->pseudo_palette = dinfo->pseudo_palette;
1087
1088 info->pixmap.size = 64*1024;
1089 info->pixmap.buf_align = 8;
James Simmons58a60642005-06-21 17:17:08 -07001090 info->pixmap.access_align = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 info->pixmap.flags = FB_PIXMAP_SYSTEM;
1092
1093 if (intelfb_init_var(dinfo))
1094 return 1;
1095
1096 info->pixmap.scan_align = 1;
Antonino A. Daplas33c37c02005-12-19 15:11:49 +08001097 strcpy(info->fix.id, dinfo->name);
1098 info->fix.smem_start = dinfo->fb.physical;
1099 info->fix.smem_len = dinfo->fb.size;
1100 info->fix.type = FB_TYPE_PACKED_PIXELS;
1101 info->fix.type_aux = 0;
1102 info->fix.xpanstep = 8;
1103 info->fix.ypanstep = 1;
1104 info->fix.ywrapstep = 0;
1105 info->fix.mmio_start = dinfo->mmio_base_phys;
1106 info->fix.mmio_len = INTEL_REG_SIZE;
1107 info->fix.accel = FB_ACCEL_I830;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 update_dinfo(dinfo, &info->var);
1109
1110 return 0;
1111}
1112
1113/* Update dinfo to match the active video mode. */
1114static void
1115update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
1116{
1117 DBG_MSG("update_dinfo\n");
1118
1119 dinfo->bpp = var->bits_per_pixel;
1120 dinfo->depth = intelfb_var_to_depth(var);
1121 dinfo->xres = var->xres;
1122 dinfo->yres = var->xres;
1123 dinfo->pixclock = var->pixclock;
1124
Antonino A. Daplas33c37c02005-12-19 15:11:49 +08001125 dinfo->info->fix.visual = dinfo->visual;
1126 dinfo->info->fix.line_length = dinfo->pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 switch (dinfo->bpp) {
1129 case 8:
1130 dinfo->visual = FB_VISUAL_PSEUDOCOLOR;
1131 dinfo->pitch = var->xres_virtual;
1132 break;
1133 case 16:
1134 dinfo->visual = FB_VISUAL_TRUECOLOR;
1135 dinfo->pitch = var->xres_virtual * 2;
1136 break;
1137 case 32:
1138 dinfo->visual = FB_VISUAL_TRUECOLOR;
1139 dinfo->pitch = var->xres_virtual * 4;
1140 break;
1141 }
1142
1143 /* Make sure the line length is a aligned correctly. */
1144 dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
1145
1146 if (FIXED_MODE(dinfo))
1147 dinfo->pitch = dinfo->initial_pitch;
1148
1149 dinfo->info->screen_base = (char __iomem *)dinfo->fb.virtual;
1150 dinfo->info->fix.line_length = dinfo->pitch;
1151 dinfo->info->fix.visual = dinfo->visual;
1152}
1153
1154/* fbops functions */
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/***************************************************************
1157 * fbdev interface *
1158 ***************************************************************/
1159
1160static int
1161intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1162{
1163 int change_var = 0;
1164 struct fb_var_screeninfo v;
1165 struct intelfb_info *dinfo;
1166 static int first = 1;
1167
1168 DBG_MSG("intelfb_check_var: accel_flags is %d\n", var->accel_flags);
1169
1170 dinfo = GET_DINFO(info);
1171
1172 if (intelfbhw_validate_mode(dinfo, var) != 0)
1173 return -EINVAL;
1174
1175 v = *var;
1176
1177 /* Check for a supported bpp. */
1178 if (v.bits_per_pixel <= 8) {
1179 v.bits_per_pixel = 8;
1180 } else if (v.bits_per_pixel <= 16) {
1181 if (v.bits_per_pixel == 16)
1182 v.green.length = 6;
1183 v.bits_per_pixel = 16;
1184 } else if (v.bits_per_pixel <= 32) {
1185 v.bits_per_pixel = 32;
1186 } else
1187 return -EINVAL;
1188
1189 change_var = ((info->var.xres != var->xres) ||
1190 (info->var.yres != var->yres) ||
1191 (info->var.xres_virtual != var->xres_virtual) ||
1192 (info->var.yres_virtual != var->yres_virtual) ||
1193 (info->var.bits_per_pixel != var->bits_per_pixel) ||
1194 memcmp(&info->var.red, &var->red, sizeof(var->red)) ||
1195 memcmp(&info->var.green, &var->green,
1196 sizeof(var->green)) ||
1197 memcmp(&info->var.blue, &var->blue, sizeof(var->blue)));
1198
1199 if (FIXED_MODE(dinfo) &&
1200 (change_var ||
1201 var->yres_virtual > dinfo->initial_var.yres_virtual ||
1202 var->yres_virtual < dinfo->initial_var.yres ||
1203 var->xoffset || var->nonstd)) {
1204 if (first) {
1205 ERR_MSG("Changing the video mode is not supported.\n");
1206 first = 0;
1207 }
1208 return -EINVAL;
1209 }
1210
1211 switch (intelfb_var_to_depth(&v)) {
1212 case 8:
1213 v.red.offset = v.green.offset = v.blue.offset = 0;
1214 v.red.length = v.green.length = v.blue.length = 8;
1215 v.transp.offset = v.transp.length = 0;
1216 break;
1217 case 15:
1218 v.red.offset = 10;
1219 v.green.offset = 5;
1220 v.blue.offset = 0;
1221 v.red.length = v.green.length = v.blue.length = 5;
1222 v.transp.offset = v.transp.length = 0;
1223 break;
1224 case 16:
1225 v.red.offset = 11;
1226 v.green.offset = 5;
1227 v.blue.offset = 0;
1228 v.red.length = 5;
1229 v.green.length = 6;
1230 v.blue.length = 5;
1231 v.transp.offset = v.transp.length = 0;
1232 break;
1233 case 24:
1234 v.red.offset = 16;
1235 v.green.offset = 8;
1236 v.blue.offset = 0;
1237 v.red.length = v.green.length = v.blue.length = 8;
1238 v.transp.offset = v.transp.length = 0;
1239 break;
1240 case 32:
1241 v.red.offset = 16;
1242 v.green.offset = 8;
1243 v.blue.offset = 0;
1244 v.red.length = v.green.length = v.blue.length = 8;
1245 v.transp.offset = 24;
1246 v.transp.length = 8;
1247 break;
1248 }
1249
1250 if (v.xoffset < 0)
1251 v.xoffset = 0;
1252 if (v.yoffset < 0)
1253 v.yoffset = 0;
1254
1255 if (v.xoffset > v.xres_virtual - v.xres)
1256 v.xoffset = v.xres_virtual - v.xres;
1257 if (v.yoffset > v.yres_virtual - v.yres)
1258 v.yoffset = v.yres_virtual - v.yres;
1259
1260 v.red.msb_right = v.green.msb_right = v.blue.msb_right =
1261 v.transp.msb_right = 0;
1262
1263 *var = v;
1264
1265 return 0;
1266}
1267
1268static int
1269intelfb_set_par(struct fb_info *info)
1270{
1271 struct intelfb_hwstate *hw;
1272 struct intelfb_info *dinfo = GET_DINFO(info);
1273
1274 if (FIXED_MODE(dinfo)) {
1275 ERR_MSG("Changing the video mode is not supported.\n");
1276 return -EINVAL;
1277 }
1278
1279 hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
1280 if (!hw)
1281 return -ENOMEM;
1282
1283 DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
1284 info->var.yres, info->var.bits_per_pixel);
1285
1286 intelfb_blank(FB_BLANK_POWERDOWN, info);
1287
Sylvain Meyerdf529332005-06-21 17:17:02 -07001288 if (ACCEL(dinfo, info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 intelfbhw_2d_stop(dinfo);
1290
1291 memcpy(hw, &dinfo->save_state, sizeof(*hw));
1292 if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
1293 goto invalid_mode;
1294 if (intelfbhw_program_mode(dinfo, hw, 0))
1295 goto invalid_mode;
1296
1297#if REGDUMP > 0
1298 intelfbhw_read_hw_state(dinfo, hw, 0);
1299 intelfbhw_print_hw_state(dinfo, hw);
1300#endif
1301
1302 update_dinfo(dinfo, &info->var);
1303
Sylvain Meyerdf529332005-06-21 17:17:02 -07001304 if (ACCEL(dinfo, info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 intelfbhw_2d_start(dinfo);
1306
1307 intelfb_pan_display(&info->var, info);
1308
1309 intelfb_blank(FB_BLANK_UNBLANK, info);
1310
1311 if (ACCEL(dinfo, info)) {
1312 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
1313 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
1314 FBINFO_HWACCEL_IMAGEBLIT;
1315 } else {
1316 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1317 }
1318 kfree(hw);
1319 return 0;
1320invalid_mode:
1321 kfree(hw);
1322 return -EINVAL;
1323}
1324
1325static int
1326intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1327 unsigned blue, unsigned transp, struct fb_info *info)
1328{
1329 struct intelfb_info *dinfo = GET_DINFO(info);
1330
1331#if VERBOSE > 0
1332 DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
1333#endif
1334
1335 if (regno > 255)
1336 return 1;
1337
Antonino A. Daplasd3015242006-03-11 03:27:26 -08001338 if (dinfo->depth == 8) {
1339 red >>= 8;
1340 green >>= 8;
1341 blue >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Antonino A. Daplasd3015242006-03-11 03:27:26 -08001343 intelfbhw_setcolreg(dinfo, regno, red, green, blue,
1344 transp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Antonino A. Daplasd3015242006-03-11 03:27:26 -08001346
1347 if (regno < 16) {
1348 switch (dinfo->depth) {
1349 case 15:
1350 dinfo->pseudo_palette[regno] = ((red & 0xf800) >> 1) |
1351 ((green & 0xf800) >> 6) |
1352 ((blue & 0xf800) >> 11);
1353 break;
1354 case 16:
1355 dinfo->pseudo_palette[regno] = (red & 0xf800) |
1356 ((green & 0xfc00) >> 5) |
1357 ((blue & 0xf800) >> 11);
1358 break;
1359 case 24:
1360 dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
1361 (green & 0xff00) |
1362 ((blue & 0xff00) >> 8);
1363 break;
1364 }
1365 }
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return 0;
1368}
1369
1370static int
1371intelfb_blank(int blank, struct fb_info *info)
1372{
1373 intelfbhw_do_blank(blank, info);
1374 return 0;
1375}
1376
1377static int
1378intelfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1379{
1380 intelfbhw_pan_display(var, info);
1381 return 0;
1382}
1383
1384/* When/if we have our own ioctls. */
1385static int
Christoph Hellwig67a66802006-01-14 13:21:25 -08001386intelfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 int retval = 0;
1389
1390 return retval;
1391}
1392
1393static void
1394intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
1395{
1396 struct intelfb_info *dinfo = GET_DINFO(info);
1397 u32 rop, color;
1398
1399#if VERBOSE > 0
1400 DBG_MSG("intelfb_fillrect\n");
1401#endif
1402
1403 if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1404 return cfb_fillrect(info, rect);
1405
1406 if (rect->rop == ROP_COPY)
1407 rop = PAT_ROP_GXCOPY;
1408 else // ROP_XOR
1409 rop = PAT_ROP_GXXOR;
1410
1411 if (dinfo->depth != 8)
1412 color = dinfo->pseudo_palette[rect->color];
1413 else
1414 color = rect->color;
1415
1416 intelfbhw_do_fillrect(dinfo, rect->dx, rect->dy,
1417 rect->width, rect->height, color,
1418 dinfo->pitch, info->var.bits_per_pixel,
1419 rop);
1420}
1421
1422static void
1423intelfb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1424{
1425 struct intelfb_info *dinfo = GET_DINFO(info);
1426
1427#if VERBOSE > 0
1428 DBG_MSG("intelfb_copyarea\n");
1429#endif
1430
1431 if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1432 return cfb_copyarea(info, region);
1433
1434 intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
1435 region->dy, region->width, region->height,
1436 dinfo->pitch, info->var.bits_per_pixel);
1437}
1438
1439static void
1440intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
1441{
1442 struct intelfb_info *dinfo = GET_DINFO(info);
1443 u32 fgcolor, bgcolor;
1444
1445#if VERBOSE > 0
1446 DBG_MSG("intelfb_imageblit\n");
1447#endif
1448
1449 if (!ACCEL(dinfo, info) || dinfo->depth == 4
1450 || image->depth != 1)
1451 return cfb_imageblit(info, image);
1452
1453 if (dinfo->depth != 8) {
1454 fgcolor = dinfo->pseudo_palette[image->fg_color];
1455 bgcolor = dinfo->pseudo_palette[image->bg_color];
1456 } else {
1457 fgcolor = image->fg_color;
1458 bgcolor = image->bg_color;
1459 }
1460
1461 if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
1462 image->height, image->data,
1463 image->dx, image->dy,
1464 dinfo->pitch, info->var.bits_per_pixel))
1465 return cfb_imageblit(info, image);
1466}
1467
1468static int
1469intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1470{
1471 struct intelfb_info *dinfo = GET_DINFO(info);
Dave Airlie8b91b0b2006-03-23 19:23:48 +11001472 u32 physical;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473#if VERBOSE > 0
1474 DBG_MSG("intelfb_cursor\n");
1475#endif
1476
1477 if (!dinfo->hwcursor)
Antonino A. Daplasc465e052005-11-07 01:00:35 -08001478 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 intelfbhw_cursor_hide(dinfo);
1481
1482 /* If XFree killed the cursor - restore it */
Dave Airlie3aff13c2006-03-31 17:08:52 +10001483 physical = (dinfo->mobile || IS_I9XX(dinfo)) ? dinfo->cursor.physical :
Dave Airlie8b91b0b2006-03-23 19:23:48 +11001484 (dinfo->cursor.offset << 12);
Dave Airlie8bb91f62006-03-23 13:06:32 +11001485
Dave Airlie8b91b0b2006-03-23 19:23:48 +11001486 if (INREG(CURSOR_A_BASEADDR) != physical) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 u32 fg, bg;
1488
1489 DBG_MSG("the cursor was killed - restore it !!\n");
1490 DBG_MSG("size %d, %d pos %d, %d\n",
1491 cursor->image.width, cursor->image.height,
1492 cursor->image.dx, cursor->image.dy);
1493
1494 intelfbhw_cursor_init(dinfo);
1495 intelfbhw_cursor_reset(dinfo);
1496 intelfbhw_cursor_setpos(dinfo, cursor->image.dx,
1497 cursor->image.dy);
1498
1499 if (dinfo->depth != 8) {
1500 fg =dinfo->pseudo_palette[cursor->image.fg_color];
1501 bg =dinfo->pseudo_palette[cursor->image.bg_color];
1502 } else {
1503 fg = cursor->image.fg_color;
1504 bg = cursor->image.bg_color;
1505 }
1506 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1507 intelfbhw_cursor_load(dinfo, cursor->image.width,
1508 cursor->image.height,
1509 dinfo->cursor_src);
1510
1511 if (cursor->enable)
1512 intelfbhw_cursor_show(dinfo);
1513 return 0;
1514 }
1515
1516 if (cursor->set & FB_CUR_SETPOS) {
1517 u32 dx, dy;
1518
1519 dx = cursor->image.dx - info->var.xoffset;
1520 dy = cursor->image.dy - info->var.yoffset;
1521
1522 intelfbhw_cursor_setpos(dinfo, dx, dy);
1523 }
1524
1525 if (cursor->set & FB_CUR_SETSIZE) {
1526 if (cursor->image.width > 64 || cursor->image.height > 64)
1527 return -ENXIO;
1528
1529 intelfbhw_cursor_reset(dinfo);
1530 }
1531
1532 if (cursor->set & FB_CUR_SETCMAP) {
1533 u32 fg, bg;
1534
1535 if (dinfo->depth != 8) {
1536 fg = dinfo->pseudo_palette[cursor->image.fg_color];
1537 bg = dinfo->pseudo_palette[cursor->image.bg_color];
1538 } else {
1539 fg = cursor->image.fg_color;
1540 bg = cursor->image.bg_color;
1541 }
1542
1543 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1544 }
1545
1546 if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1547 u32 s_pitch = (ROUND_UP_TO(cursor->image.width, 8) / 8);
1548 u32 size = s_pitch * cursor->image.height;
1549 u8 *dat = (u8 *) cursor->image.data;
1550 u8 *msk = (u8 *) cursor->mask;
1551 u8 src[64];
1552 u32 i;
1553
1554 if (cursor->image.depth != 1)
1555 return -ENXIO;
1556
1557 switch (cursor->rop) {
1558 case ROP_XOR:
1559 for (i = 0; i < size; i++)
1560 src[i] = dat[i] ^ msk[i];
1561 break;
1562 case ROP_COPY:
1563 default:
1564 for (i = 0; i < size; i++)
1565 src[i] = dat[i] & msk[i];
1566 break;
1567 }
1568
1569 /* save the bitmap to restore it when XFree will
1570 make the cursor dirty */
1571 memcpy(dinfo->cursor_src, src, size);
1572
1573 intelfbhw_cursor_load(dinfo, cursor->image.width,
1574 cursor->image.height, src);
1575 }
1576
1577 if (cursor->enable)
1578 intelfbhw_cursor_show(dinfo);
1579
1580 return 0;
1581}
1582
1583static int
1584intelfb_sync(struct fb_info *info)
1585{
1586 struct intelfb_info *dinfo = GET_DINFO(info);
1587
1588#if VERBOSE > 0
1589 DBG_MSG("intelfb_sync\n");
1590#endif
1591
1592 if (dinfo->ring_lockup)
1593 return 0;
1594
1595 intelfbhw_do_sync(dinfo);
1596 return 0;
1597}
1598