blob: e1ac4651adc74ba128f8e22a1e5cc4c884387081 [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,
Dennis Munsie2c474302006-05-27 18:33:35 +10001036 NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (msrc)
1038 msrc |= 8;
1039 }
1040 if (!msrc) {
1041 msrc = fb_find_mode(var, dinfo->info, PREFERRED_MODE,
Dennis Munsie2c474302006-05-27 18:33:35 +10001042 NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044 }
1045
1046 if (!msrc) {
1047 ERR_MSG("Cannot find a suitable video mode.\n");
1048 return 1;
1049 }
1050
1051 INF_MSG("Initial video mode is %dx%d-%d@%d.\n", var->xres, var->yres,
1052 var->bits_per_pixel, var_to_refresh(var));
1053
1054 DBG_MSG("Initial video mode is from %d.\n", msrc);
1055
1056#if ALLOCATE_FOR_PANNING
1057 /* Allow use of half of the video ram for panning */
1058 var->xres_virtual = var->xres;
1059 var->yres_virtual =
1060 dinfo->fb.size / 2 / (var->bits_per_pixel * var->xres);
1061 if (var->yres_virtual < var->yres)
1062 var->yres_virtual = var->yres;
1063#else
1064 var->yres_virtual = var->yres;
1065#endif
1066
1067 if (dinfo->accel)
1068 var->accel_flags |= FB_ACCELF_TEXT;
1069 else
1070 var->accel_flags &= ~FB_ACCELF_TEXT;
1071
1072 return 0;
1073}
1074
1075static int __devinit
1076intelfb_set_fbinfo(struct intelfb_info *dinfo)
1077{
1078 struct fb_info *info = dinfo->info;
1079
1080 DBG_MSG("intelfb_set_fbinfo\n");
1081
1082 info->flags = FBINFO_FLAG_DEFAULT;
1083 info->fbops = &intel_fb_ops;
1084 info->pseudo_palette = dinfo->pseudo_palette;
1085
1086 info->pixmap.size = 64*1024;
1087 info->pixmap.buf_align = 8;
James Simmons58a60642005-06-21 17:17:08 -07001088 info->pixmap.access_align = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 info->pixmap.flags = FB_PIXMAP_SYSTEM;
1090
1091 if (intelfb_init_var(dinfo))
1092 return 1;
1093
1094 info->pixmap.scan_align = 1;
Antonino A. Daplas33c37c02005-12-19 15:11:49 +08001095 strcpy(info->fix.id, dinfo->name);
1096 info->fix.smem_start = dinfo->fb.physical;
1097 info->fix.smem_len = dinfo->fb.size;
1098 info->fix.type = FB_TYPE_PACKED_PIXELS;
1099 info->fix.type_aux = 0;
1100 info->fix.xpanstep = 8;
1101 info->fix.ypanstep = 1;
1102 info->fix.ywrapstep = 0;
1103 info->fix.mmio_start = dinfo->mmio_base_phys;
1104 info->fix.mmio_len = INTEL_REG_SIZE;
1105 info->fix.accel = FB_ACCEL_I830;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 update_dinfo(dinfo, &info->var);
1107
1108 return 0;
1109}
1110
1111/* Update dinfo to match the active video mode. */
1112static void
1113update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
1114{
1115 DBG_MSG("update_dinfo\n");
1116
1117 dinfo->bpp = var->bits_per_pixel;
1118 dinfo->depth = intelfb_var_to_depth(var);
1119 dinfo->xres = var->xres;
1120 dinfo->yres = var->xres;
1121 dinfo->pixclock = var->pixclock;
1122
Antonino A. Daplas33c37c02005-12-19 15:11:49 +08001123 dinfo->info->fix.visual = dinfo->visual;
1124 dinfo->info->fix.line_length = dinfo->pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 switch (dinfo->bpp) {
1127 case 8:
1128 dinfo->visual = FB_VISUAL_PSEUDOCOLOR;
1129 dinfo->pitch = var->xres_virtual;
1130 break;
1131 case 16:
1132 dinfo->visual = FB_VISUAL_TRUECOLOR;
1133 dinfo->pitch = var->xres_virtual * 2;
1134 break;
1135 case 32:
1136 dinfo->visual = FB_VISUAL_TRUECOLOR;
1137 dinfo->pitch = var->xres_virtual * 4;
1138 break;
1139 }
1140
1141 /* Make sure the line length is a aligned correctly. */
Dennis Munsiedf7df8a2006-05-27 18:17:52 +10001142 if (IS_I9XX(dinfo))
1143 dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT_I9XX);
1144 else
1145 dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (FIXED_MODE(dinfo))
1148 dinfo->pitch = dinfo->initial_pitch;
1149
1150 dinfo->info->screen_base = (char __iomem *)dinfo->fb.virtual;
1151 dinfo->info->fix.line_length = dinfo->pitch;
1152 dinfo->info->fix.visual = dinfo->visual;
1153}
1154
1155/* fbops functions */
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/***************************************************************
1158 * fbdev interface *
1159 ***************************************************************/
1160
1161static int
1162intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1163{
1164 int change_var = 0;
1165 struct fb_var_screeninfo v;
1166 struct intelfb_info *dinfo;
1167 static int first = 1;
Dave Airlie3587c502006-04-03 14:46:55 +10001168 int i;
1169 /* Good pitches to allow tiling. Don't care about pitches < 1024. */
1170 static const int pitches[] = {
1171 128 * 8,
1172 128 * 16,
1173 128 * 32,
1174 128 * 64,
1175 0
1176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 DBG_MSG("intelfb_check_var: accel_flags is %d\n", var->accel_flags);
1179
1180 dinfo = GET_DINFO(info);
1181
Dave Airlie3587c502006-04-03 14:46:55 +10001182 /* update the pitch */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (intelfbhw_validate_mode(dinfo, var) != 0)
1184 return -EINVAL;
1185
1186 v = *var;
1187
Dave Airlie3587c502006-04-03 14:46:55 +10001188 for (i = 0; pitches[i] != 0; i++) {
1189 if (pitches[i] >= v.xres_virtual) {
1190 v.xres_virtual = pitches[i];
1191 break;
1192 }
1193 }
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* Check for a supported bpp. */
1196 if (v.bits_per_pixel <= 8) {
1197 v.bits_per_pixel = 8;
1198 } else if (v.bits_per_pixel <= 16) {
1199 if (v.bits_per_pixel == 16)
1200 v.green.length = 6;
1201 v.bits_per_pixel = 16;
1202 } else if (v.bits_per_pixel <= 32) {
1203 v.bits_per_pixel = 32;
1204 } else
1205 return -EINVAL;
1206
1207 change_var = ((info->var.xres != var->xres) ||
1208 (info->var.yres != var->yres) ||
1209 (info->var.xres_virtual != var->xres_virtual) ||
1210 (info->var.yres_virtual != var->yres_virtual) ||
1211 (info->var.bits_per_pixel != var->bits_per_pixel) ||
1212 memcmp(&info->var.red, &var->red, sizeof(var->red)) ||
1213 memcmp(&info->var.green, &var->green,
1214 sizeof(var->green)) ||
1215 memcmp(&info->var.blue, &var->blue, sizeof(var->blue)));
1216
1217 if (FIXED_MODE(dinfo) &&
1218 (change_var ||
1219 var->yres_virtual > dinfo->initial_var.yres_virtual ||
1220 var->yres_virtual < dinfo->initial_var.yres ||
1221 var->xoffset || var->nonstd)) {
1222 if (first) {
1223 ERR_MSG("Changing the video mode is not supported.\n");
1224 first = 0;
1225 }
1226 return -EINVAL;
1227 }
1228
1229 switch (intelfb_var_to_depth(&v)) {
1230 case 8:
1231 v.red.offset = v.green.offset = v.blue.offset = 0;
1232 v.red.length = v.green.length = v.blue.length = 8;
1233 v.transp.offset = v.transp.length = 0;
1234 break;
1235 case 15:
1236 v.red.offset = 10;
1237 v.green.offset = 5;
1238 v.blue.offset = 0;
1239 v.red.length = v.green.length = v.blue.length = 5;
1240 v.transp.offset = v.transp.length = 0;
1241 break;
1242 case 16:
1243 v.red.offset = 11;
1244 v.green.offset = 5;
1245 v.blue.offset = 0;
1246 v.red.length = 5;
1247 v.green.length = 6;
1248 v.blue.length = 5;
1249 v.transp.offset = v.transp.length = 0;
1250 break;
1251 case 24:
1252 v.red.offset = 16;
1253 v.green.offset = 8;
1254 v.blue.offset = 0;
1255 v.red.length = v.green.length = v.blue.length = 8;
1256 v.transp.offset = v.transp.length = 0;
1257 break;
1258 case 32:
1259 v.red.offset = 16;
1260 v.green.offset = 8;
1261 v.blue.offset = 0;
1262 v.red.length = v.green.length = v.blue.length = 8;
1263 v.transp.offset = 24;
1264 v.transp.length = 8;
1265 break;
1266 }
1267
1268 if (v.xoffset < 0)
1269 v.xoffset = 0;
1270 if (v.yoffset < 0)
1271 v.yoffset = 0;
1272
1273 if (v.xoffset > v.xres_virtual - v.xres)
1274 v.xoffset = v.xres_virtual - v.xres;
1275 if (v.yoffset > v.yres_virtual - v.yres)
1276 v.yoffset = v.yres_virtual - v.yres;
1277
1278 v.red.msb_right = v.green.msb_right = v.blue.msb_right =
1279 v.transp.msb_right = 0;
1280
1281 *var = v;
1282
1283 return 0;
1284}
1285
1286static int
1287intelfb_set_par(struct fb_info *info)
1288{
1289 struct intelfb_hwstate *hw;
1290 struct intelfb_info *dinfo = GET_DINFO(info);
1291
1292 if (FIXED_MODE(dinfo)) {
1293 ERR_MSG("Changing the video mode is not supported.\n");
1294 return -EINVAL;
1295 }
1296
1297 hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
1298 if (!hw)
1299 return -ENOMEM;
1300
1301 DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
1302 info->var.yres, info->var.bits_per_pixel);
1303
1304 intelfb_blank(FB_BLANK_POWERDOWN, info);
1305
Sylvain Meyerdf529332005-06-21 17:17:02 -07001306 if (ACCEL(dinfo, info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 intelfbhw_2d_stop(dinfo);
1308
1309 memcpy(hw, &dinfo->save_state, sizeof(*hw));
1310 if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
1311 goto invalid_mode;
1312 if (intelfbhw_program_mode(dinfo, hw, 0))
1313 goto invalid_mode;
1314
1315#if REGDUMP > 0
1316 intelfbhw_read_hw_state(dinfo, hw, 0);
1317 intelfbhw_print_hw_state(dinfo, hw);
1318#endif
1319
1320 update_dinfo(dinfo, &info->var);
1321
Sylvain Meyerdf529332005-06-21 17:17:02 -07001322 if (ACCEL(dinfo, info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 intelfbhw_2d_start(dinfo);
1324
1325 intelfb_pan_display(&info->var, info);
1326
1327 intelfb_blank(FB_BLANK_UNBLANK, info);
1328
1329 if (ACCEL(dinfo, info)) {
1330 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
1331 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
1332 FBINFO_HWACCEL_IMAGEBLIT;
1333 } else {
1334 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1335 }
1336 kfree(hw);
1337 return 0;
1338invalid_mode:
1339 kfree(hw);
1340 return -EINVAL;
1341}
1342
1343static int
1344intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1345 unsigned blue, unsigned transp, struct fb_info *info)
1346{
1347 struct intelfb_info *dinfo = GET_DINFO(info);
1348
1349#if VERBOSE > 0
1350 DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
1351#endif
1352
1353 if (regno > 255)
1354 return 1;
1355
Antonino A. Daplasd3015242006-03-11 03:27:26 -08001356 if (dinfo->depth == 8) {
1357 red >>= 8;
1358 green >>= 8;
1359 blue >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Antonino A. Daplasd3015242006-03-11 03:27:26 -08001361 intelfbhw_setcolreg(dinfo, regno, red, green, blue,
1362 transp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Antonino A. Daplasd3015242006-03-11 03:27:26 -08001364
1365 if (regno < 16) {
1366 switch (dinfo->depth) {
1367 case 15:
1368 dinfo->pseudo_palette[regno] = ((red & 0xf800) >> 1) |
1369 ((green & 0xf800) >> 6) |
1370 ((blue & 0xf800) >> 11);
1371 break;
1372 case 16:
1373 dinfo->pseudo_palette[regno] = (red & 0xf800) |
1374 ((green & 0xfc00) >> 5) |
1375 ((blue & 0xf800) >> 11);
1376 break;
1377 case 24:
1378 dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
1379 (green & 0xff00) |
1380 ((blue & 0xff00) >> 8);
1381 break;
1382 }
1383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
1386}
1387
1388static int
1389intelfb_blank(int blank, struct fb_info *info)
1390{
1391 intelfbhw_do_blank(blank, info);
1392 return 0;
1393}
1394
1395static int
1396intelfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1397{
1398 intelfbhw_pan_display(var, info);
1399 return 0;
1400}
1401
1402/* When/if we have our own ioctls. */
1403static int
Christoph Hellwig67a66802006-01-14 13:21:25 -08001404intelfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 int retval = 0;
1407
1408 return retval;
1409}
1410
1411static void
1412intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
1413{
1414 struct intelfb_info *dinfo = GET_DINFO(info);
1415 u32 rop, color;
1416
1417#if VERBOSE > 0
1418 DBG_MSG("intelfb_fillrect\n");
1419#endif
1420
1421 if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1422 return cfb_fillrect(info, rect);
1423
1424 if (rect->rop == ROP_COPY)
1425 rop = PAT_ROP_GXCOPY;
1426 else // ROP_XOR
1427 rop = PAT_ROP_GXXOR;
1428
1429 if (dinfo->depth != 8)
1430 color = dinfo->pseudo_palette[rect->color];
1431 else
1432 color = rect->color;
1433
1434 intelfbhw_do_fillrect(dinfo, rect->dx, rect->dy,
1435 rect->width, rect->height, color,
1436 dinfo->pitch, info->var.bits_per_pixel,
1437 rop);
1438}
1439
1440static void
1441intelfb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1442{
1443 struct intelfb_info *dinfo = GET_DINFO(info);
1444
1445#if VERBOSE > 0
1446 DBG_MSG("intelfb_copyarea\n");
1447#endif
1448
1449 if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1450 return cfb_copyarea(info, region);
1451
1452 intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
1453 region->dy, region->width, region->height,
1454 dinfo->pitch, info->var.bits_per_pixel);
1455}
1456
1457static void
1458intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
1459{
1460 struct intelfb_info *dinfo = GET_DINFO(info);
1461 u32 fgcolor, bgcolor;
1462
1463#if VERBOSE > 0
1464 DBG_MSG("intelfb_imageblit\n");
1465#endif
1466
1467 if (!ACCEL(dinfo, info) || dinfo->depth == 4
1468 || image->depth != 1)
1469 return cfb_imageblit(info, image);
1470
1471 if (dinfo->depth != 8) {
1472 fgcolor = dinfo->pseudo_palette[image->fg_color];
1473 bgcolor = dinfo->pseudo_palette[image->bg_color];
1474 } else {
1475 fgcolor = image->fg_color;
1476 bgcolor = image->bg_color;
1477 }
1478
1479 if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
1480 image->height, image->data,
1481 image->dx, image->dy,
1482 dinfo->pitch, info->var.bits_per_pixel))
1483 return cfb_imageblit(info, image);
1484}
1485
1486static int
1487intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1488{
1489 struct intelfb_info *dinfo = GET_DINFO(info);
Dave Airlie8b91b0b2006-03-23 19:23:48 +11001490 u32 physical;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491#if VERBOSE > 0
1492 DBG_MSG("intelfb_cursor\n");
1493#endif
1494
1495 if (!dinfo->hwcursor)
Antonino A. Daplasc465e052005-11-07 01:00:35 -08001496 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 intelfbhw_cursor_hide(dinfo);
1499
1500 /* If XFree killed the cursor - restore it */
Dave Airlie3aff13c2006-03-31 17:08:52 +10001501 physical = (dinfo->mobile || IS_I9XX(dinfo)) ? dinfo->cursor.physical :
Dave Airlie8b91b0b2006-03-23 19:23:48 +11001502 (dinfo->cursor.offset << 12);
Dave Airlie8bb91f62006-03-23 13:06:32 +11001503
Dave Airlie8b91b0b2006-03-23 19:23:48 +11001504 if (INREG(CURSOR_A_BASEADDR) != physical) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 u32 fg, bg;
1506
1507 DBG_MSG("the cursor was killed - restore it !!\n");
1508 DBG_MSG("size %d, %d pos %d, %d\n",
1509 cursor->image.width, cursor->image.height,
1510 cursor->image.dx, cursor->image.dy);
1511
1512 intelfbhw_cursor_init(dinfo);
1513 intelfbhw_cursor_reset(dinfo);
1514 intelfbhw_cursor_setpos(dinfo, cursor->image.dx,
1515 cursor->image.dy);
1516
1517 if (dinfo->depth != 8) {
1518 fg =dinfo->pseudo_palette[cursor->image.fg_color];
1519 bg =dinfo->pseudo_palette[cursor->image.bg_color];
1520 } else {
1521 fg = cursor->image.fg_color;
1522 bg = cursor->image.bg_color;
1523 }
1524 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1525 intelfbhw_cursor_load(dinfo, cursor->image.width,
1526 cursor->image.height,
1527 dinfo->cursor_src);
1528
1529 if (cursor->enable)
1530 intelfbhw_cursor_show(dinfo);
1531 return 0;
1532 }
1533
1534 if (cursor->set & FB_CUR_SETPOS) {
1535 u32 dx, dy;
1536
1537 dx = cursor->image.dx - info->var.xoffset;
1538 dy = cursor->image.dy - info->var.yoffset;
1539
1540 intelfbhw_cursor_setpos(dinfo, dx, dy);
1541 }
1542
1543 if (cursor->set & FB_CUR_SETSIZE) {
1544 if (cursor->image.width > 64 || cursor->image.height > 64)
1545 return -ENXIO;
1546
1547 intelfbhw_cursor_reset(dinfo);
1548 }
1549
1550 if (cursor->set & FB_CUR_SETCMAP) {
1551 u32 fg, bg;
1552
1553 if (dinfo->depth != 8) {
1554 fg = dinfo->pseudo_palette[cursor->image.fg_color];
1555 bg = dinfo->pseudo_palette[cursor->image.bg_color];
1556 } else {
1557 fg = cursor->image.fg_color;
1558 bg = cursor->image.bg_color;
1559 }
1560
1561 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1562 }
1563
1564 if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1565 u32 s_pitch = (ROUND_UP_TO(cursor->image.width, 8) / 8);
1566 u32 size = s_pitch * cursor->image.height;
1567 u8 *dat = (u8 *) cursor->image.data;
1568 u8 *msk = (u8 *) cursor->mask;
1569 u8 src[64];
1570 u32 i;
1571
1572 if (cursor->image.depth != 1)
1573 return -ENXIO;
1574
1575 switch (cursor->rop) {
1576 case ROP_XOR:
1577 for (i = 0; i < size; i++)
1578 src[i] = dat[i] ^ msk[i];
1579 break;
1580 case ROP_COPY:
1581 default:
1582 for (i = 0; i < size; i++)
1583 src[i] = dat[i] & msk[i];
1584 break;
1585 }
1586
1587 /* save the bitmap to restore it when XFree will
1588 make the cursor dirty */
1589 memcpy(dinfo->cursor_src, src, size);
1590
1591 intelfbhw_cursor_load(dinfo, cursor->image.width,
1592 cursor->image.height, src);
1593 }
1594
1595 if (cursor->enable)
1596 intelfbhw_cursor_show(dinfo);
1597
1598 return 0;
1599}
1600
1601static int
1602intelfb_sync(struct fb_info *info)
1603{
1604 struct intelfb_info *dinfo = GET_DINFO(info);
1605
1606#if VERBOSE > 0
1607 DBG_MSG("intelfb_sync\n");
1608#endif
1609
1610 if (dinfo->ring_lockup)
1611 return 0;
1612
1613 intelfbhw_do_sync(dinfo);
1614 return 0;
1615}
1616