blob: 0090544842f5d672225b127c9f69f53ec31ebe63 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * intelfb
3 *
Scott MacKenzie3a590262005-11-07 01:00:33 -08004 * Linux framebuffer driver for Intel(R) 830M/845G/852GM/855GM/865G/915G/915GM
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * integrated graphics chips.
6 *
7 * Copyright © 2002, 2003 David Dawes <dawes@xfree86.org>
8 * 2004 Sylvain Meyer
9 *
10 * This driver consists of two parts. The first part (intelfbdrv.c) provides
11 * the basic fbdev interfaces, is derived in part from the radeonfb and
12 * vesafb drivers, and is covered by the GPL. The second part (intelfbhw.c)
13 * provides the code to program the hardware. Most of it is derived from
14 * the i810/i830 XFree86 driver. The HW-specific code is covered here
15 * under a dual license (GPL and MIT/XFree86 license).
16 *
17 * Author: David Dawes
18 *
19 */
20
21/* $DHD: intelfb/intelfbdrv.c,v 1.20 2003/06/27 15:17:40 dawes Exp $ */
22
23/*
24 * Changes:
25 * 01/2003 - Initial driver (0.1.0), no mode switching, no acceleration.
26 * This initial version is a basic core that works a lot like
27 * the vesafb driver. It must be built-in to the kernel,
28 * and the initial video mode must be set with vga=XXX at
29 * boot time. (David Dawes)
30 *
31 * 01/2003 - Version 0.2.0: Mode switching added, colormap support
32 * implemented, Y panning, and soft screen blanking implemented.
33 * No acceleration yet. (David Dawes)
34 *
35 * 01/2003 - Version 0.3.0: fbcon acceleration support added. Module
36 * option handling added. (David Dawes)
37 *
38 * 01/2003 - Version 0.4.0: fbcon HW cursor support added. (David Dawes)
39 *
40 * 01/2003 - Version 0.4.1: Add auto-generation of built-in modes.
41 * (David Dawes)
42 *
43 * 02/2003 - Version 0.4.2: Add check for active non-CRT devices, and
44 * mode validation checks. (David Dawes)
45 *
46 * 02/2003 - Version 0.4.3: Check when the VC is in graphics mode so that
47 * acceleration is disabled while an XFree86 server is running.
48 * (David Dawes)
49 *
50 * 02/2003 - Version 0.4.4: Monitor DPMS support. (David Dawes)
51 *
52 * 02/2003 - Version 0.4.5: Basic XFree86 + fbdev working. (David Dawes)
53 *
54 * 02/2003 - Version 0.5.0: Modify to work with the 2.5.32 kernel as well
55 * as 2.4.x kernels. (David Dawes)
56 *
57 * 02/2003 - Version 0.6.0: Split out HW-specifics into a separate file.
58 * (David Dawes)
59 *
60 * 02/2003 - Version 0.7.0: Test on 852GM/855GM. Acceleration and HW
61 * cursor are disabled on this platform. (David Dawes)
62 *
63 * 02/2003 - Version 0.7.1: Test on 845G. Acceleration is disabled
64 * on this platform. (David Dawes)
65 *
66 * 02/2003 - Version 0.7.2: Test on 830M. Acceleration and HW
67 * cursor are disabled on this platform. (David Dawes)
68 *
69 * 02/2003 - Version 0.7.3: Fix 8-bit modes for mobile platforms
70 * (David Dawes)
71 *
72 * 02/2003 - Version 0.7.4: Add checks for FB and FBCON_HAS_CFB* configured
73 * in the kernel, and add mode bpp verification and default
74 * bpp selection based on which FBCON_HAS_CFB* are configured.
75 * (David Dawes)
76 *
77 * 02/2003 - Version 0.7.5: Add basic package/install scripts based on the
78 * DRI packaging scripts. (David Dawes)
79 *
80 * 04/2003 - Version 0.7.6: Fix typo that affects builds with SMP-enabled
81 * kernels. (David Dawes, reported by Anupam).
82 *
83 * 06/2003 - Version 0.7.7:
84 * Fix Makefile.kernel build problem (Tsutomu Yasuda).
85 * Fix mis-placed #endif (2.4.21 kernel).
86 *
87 * 09/2004 - Version 0.9.0 - by Sylvain Meyer
88 * Port to linux 2.6 kernel fbdev
89 * Fix HW accel and HW cursor on i845G
90 * Use of agpgart for fb memory reservation
91 * Add mtrr support
92 *
93 * 10/2004 - Version 0.9.1
94 * Use module_param instead of old MODULE_PARM
95 * Some cleanup
96 *
97 * 11/2004 - Version 0.9.2
98 * Add vram option to reserve more memory than stolen by BIOS
99 * Fix intelfbhw_pan_display typo
100 * Add __initdata annotations
101 *
102 * TODO:
103 *
104 *
105 * Wish List:
106 *
107 *
108 */
109
110#include <linux/config.h>
111#include <linux/module.h>
112#include <linux/kernel.h>
113#include <linux/errno.h>
114#include <linux/string.h>
115#include <linux/mm.h>
116#include <linux/tty.h>
117#include <linux/slab.h>
118#include <linux/delay.h>
119#include <linux/fb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#include <linux/ioport.h>
121#include <linux/init.h>
122#include <linux/pci.h>
123#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126#include <asm/io.h>
127
128#ifdef CONFIG_MTRR
129#include <asm/mtrr.h>
130#endif
131
132#include "intelfb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#include "intelfbhw.h"
134
Adrian Bunk14c6f522005-05-01 08:59:23 -0700135static void __devinit get_initial_mode(struct intelfb_info *dinfo);
136static void update_dinfo(struct intelfb_info *dinfo,
137 struct fb_var_screeninfo *var);
Adrian Bunk14c6f522005-05-01 08:59:23 -0700138static int intelfb_check_var(struct fb_var_screeninfo *var,
139 struct fb_info *info);
140static int intelfb_set_par(struct fb_info *info);
141static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
142 unsigned blue, unsigned transp,
143 struct fb_info *info);
144
145static int intelfb_blank(int blank, struct fb_info *info);
146static int intelfb_pan_display(struct fb_var_screeninfo *var,
147 struct fb_info *info);
148
149static void intelfb_fillrect(struct fb_info *info,
150 const struct fb_fillrect *rect);
151static void intelfb_copyarea(struct fb_info *info,
152 const struct fb_copyarea *region);
153static void intelfb_imageblit(struct fb_info *info,
154 const struct fb_image *image);
155static int intelfb_cursor(struct fb_info *info,
156 struct fb_cursor *cursor);
157
158static int intelfb_sync(struct fb_info *info);
159
160static int intelfb_ioctl(struct inode *inode, struct file *file,
161 unsigned int cmd, unsigned long arg,
162 struct fb_info *info);
163
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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 { 0, }
187};
188
189/* Global data */
190static int num_registered = 0;
191
192/* fb ops */
193static struct fb_ops intel_fb_ops = {
194 .owner = THIS_MODULE,
195 .fb_check_var = intelfb_check_var,
196 .fb_set_par = intelfb_set_par,
197 .fb_setcolreg = intelfb_setcolreg,
198 .fb_blank = intelfb_blank,
199 .fb_pan_display = intelfb_pan_display,
200 .fb_fillrect = intelfb_fillrect,
201 .fb_copyarea = intelfb_copyarea,
202 .fb_imageblit = intelfb_imageblit,
203 .fb_cursor = intelfb_cursor,
204 .fb_sync = intelfb_sync,
205 .fb_ioctl = intelfb_ioctl
206};
207
208/* PCI driver module table */
209static struct pci_driver intelfb_driver = {
Andrew Morton5a3b5892005-06-21 17:16:57 -0700210 .name = "intelfb",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .id_table = intelfb_pci_table,
212 .probe = intelfb_pci_register,
213 .remove = __devexit_p(intelfb_pci_unregister)
214};
215
216/* Module description/parameters */
217MODULE_AUTHOR("David Dawes <dawes@tungstengraphics.com>, "
218 "Sylvain Meyer <sylvain.meyer@worldonline.fr>");
219MODULE_DESCRIPTION(
220 "Framebuffer driver for Intel(R) " SUPPORTED_CHIPSETS " chipsets");
221MODULE_LICENSE("Dual BSD/GPL");
222MODULE_DEVICE_TABLE(pci, intelfb_pci_table);
223
Patrick McManus346e3992005-05-28 15:51:46 -0700224static int accel = 1;
225static int vram = 4;
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800226static int hwcursor = 0;
Patrick McManus346e3992005-05-28 15:51:46 -0700227static int mtrr = 1;
228static int fixed = 0;
229static int noinit = 0;
230static int noregister = 0;
231static int probeonly = 0;
232static int idonly = 0;
233static int bailearly = 0;
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700234static int voffset = 48;
Patrick McManus346e3992005-05-28 15:51:46 -0700235static char *mode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237module_param(accel, bool, S_IRUGO);
James Simmons4c7ffe02005-09-09 13:04:31 -0700238MODULE_PARM_DESC(accel, "Enable hardware acceleration");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239module_param(vram, int, S_IRUGO);
240MODULE_PARM_DESC(vram, "System RAM to allocate to framebuffer in MiB");
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700241module_param(voffset, int, S_IRUGO);
242MODULE_PARM_DESC(voffset, "Offset of framebuffer in MiB");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243module_param(hwcursor, bool, S_IRUGO);
244MODULE_PARM_DESC(hwcursor, "Enable HW cursor");
245module_param(mtrr, bool, S_IRUGO);
246MODULE_PARM_DESC(mtrr, "Enable MTRR support");
247module_param(fixed, bool, S_IRUGO);
248MODULE_PARM_DESC(fixed, "Disable mode switching");
249module_param(noinit, bool, 0);
250MODULE_PARM_DESC(noinit, "Don't initialise graphics mode when loading");
251module_param(noregister, bool, 0);
252MODULE_PARM_DESC(noregister, "Don't register, just probe and exit (debug)");
253module_param(probeonly, bool, 0);
254MODULE_PARM_DESC(probeonly, "Do a minimal probe (debug)");
255module_param(idonly, bool, 0);
256MODULE_PARM_DESC(idonly, "Just identify without doing anything else (debug)");
257module_param(bailearly, bool, 0);
258MODULE_PARM_DESC(bailearly, "Bail out early, depending on value (debug)");
259module_param(mode, charp, S_IRUGO);
260MODULE_PARM_DESC(mode,
261 "Initial video mode \"<xres>x<yres>[-<depth>][@<refresh>]\"");
262
263#ifndef MODULE
264#define OPT_EQUAL(opt, name) (!strncmp(opt, name, strlen(name)))
265#define OPT_INTVAL(opt, name) simple_strtoul(opt + strlen(name), NULL, 0)
266#define OPT_STRVAL(opt, name) (opt + strlen(name))
267
268static __inline__ char *
269get_opt_string(const char *this_opt, const char *name)
270{
271 const char *p;
272 int i;
273 char *ret;
274
275 p = OPT_STRVAL(this_opt, name);
276 i = 0;
277 while (p[i] && p[i] != ' ' && p[i] != ',')
278 i++;
279 ret = kmalloc(i + 1, GFP_KERNEL);
280 if (ret) {
281 strncpy(ret, p, i);
282 ret[i] = '\0';
283 }
284 return ret;
285}
286
287static __inline__ int
288get_opt_int(const char *this_opt, const char *name, int *ret)
289{
290 if (!ret)
291 return 0;
292
293 if (!OPT_EQUAL(this_opt, name))
294 return 0;
295
296 *ret = OPT_INTVAL(this_opt, name);
297 return 1;
298}
299
300static __inline__ int
301get_opt_bool(const char *this_opt, const char *name, int *ret)
302{
303 if (!ret)
304 return 0;
305
306 if (OPT_EQUAL(this_opt, name)) {
307 if (this_opt[strlen(name)] == '=')
308 *ret = simple_strtoul(this_opt + strlen(name) + 1,
309 NULL, 0);
310 else
311 *ret = 1;
312 } else {
313 if (OPT_EQUAL(this_opt, "no") && OPT_EQUAL(this_opt + 2, name))
314 *ret = 0;
315 else
316 return 0;
317 }
318 return 1;
319}
320
321static int __init
322intelfb_setup(char *options)
323{
324 char *this_opt;
325
326 DBG_MSG("intelfb_setup\n");
327
328 if (!options || !*options) {
329 DBG_MSG("no options\n");
330 return 0;
331 } else
332 DBG_MSG("options: %s\n", options);
333
334 /*
335 * These are the built-in options analogous to the module parameters
336 * defined above.
337 *
338 * The syntax is:
339 *
340 * video=intelfb:[mode][,<param>=<val>] ...
341 *
342 * e.g.,
343 *
344 * video=intelfb:1024x768-16@75,accel=0
345 */
346
347 while ((this_opt = strsep(&options, ","))) {
348 if (!*this_opt)
349 continue;
350 if (get_opt_bool(this_opt, "accel", &accel))
351 ;
352 else if (get_opt_int(this_opt, "vram", &vram))
353 ;
354 else if (get_opt_bool(this_opt, "hwcursor", &hwcursor))
355 ;
356 else if (get_opt_bool(this_opt, "mtrr", &mtrr))
357 ;
358 else if (get_opt_bool(this_opt, "fixed", &fixed))
359 ;
360 else if (get_opt_bool(this_opt, "init", &noinit))
361 noinit = !noinit;
362 else if (OPT_EQUAL(this_opt, "mode="))
363 mode = get_opt_string(this_opt, "mode=");
364 else
365 mode = this_opt;
366 }
367
368 return 0;
369}
370
371#endif
372
373static int __init
374intelfb_init(void)
375{
376#ifndef MODULE
377 char *option = NULL;
378#endif
379
380 DBG_MSG("intelfb_init\n");
381
382 INF_MSG("Framebuffer driver for "
383 "Intel(R) " SUPPORTED_CHIPSETS " chipsets\n");
384 INF_MSG("Version " INTELFB_VERSION "\n");
385
386 if (idonly)
387 return -ENODEV;
388
389#ifndef MODULE
390 if (fb_get_options("intelfb", &option))
391 return -ENODEV;
392 intelfb_setup(option);
393#endif
394
395 return pci_register_driver(&intelfb_driver);
396}
397
398static void __exit
399intelfb_exit(void)
400{
401 DBG_MSG("intelfb_exit\n");
402 pci_unregister_driver(&intelfb_driver);
403}
404
405module_init(intelfb_init);
406module_exit(intelfb_exit);
407
408/***************************************************************
409 * mtrr support functions *
410 ***************************************************************/
411
412#ifdef CONFIG_MTRR
413static inline void __devinit set_mtrr(struct intelfb_info *dinfo)
414{
415 dinfo->mtrr_reg = mtrr_add(dinfo->aperture.physical,
416 dinfo->aperture.size, MTRR_TYPE_WRCOMB, 1);
417 if (dinfo->mtrr_reg < 0) {
418 ERR_MSG("unable to set MTRR\n");
419 return;
420 }
421 dinfo->has_mtrr = 1;
422}
423static inline void unset_mtrr(struct intelfb_info *dinfo)
424{
425 if (dinfo->has_mtrr)
426 mtrr_del(dinfo->mtrr_reg, dinfo->aperture.physical,
427 dinfo->aperture.size);
428}
429#else
430#define set_mtrr(x) WRN_MSG("MTRR is disabled in the kernel\n")
431
432#define unset_mtrr(x) do { } while (0)
433#endif /* CONFIG_MTRR */
434
435/***************************************************************
436 * driver init / cleanup *
437 ***************************************************************/
438
439static void
440cleanup(struct intelfb_info *dinfo)
441{
442 DBG_MSG("cleanup\n");
443
444 if (!dinfo)
445 return;
446
447 fb_dealloc_cmap(&dinfo->info->cmap);
448 kfree(dinfo->info->pixmap.addr);
449
450 if (dinfo->registered)
451 unregister_framebuffer(dinfo->info);
452
453 unset_mtrr(dinfo);
454
455 if (dinfo->fbmem_gart && dinfo->gtt_fb_mem) {
456 agp_unbind_memory(dinfo->gtt_fb_mem);
457 agp_free_memory(dinfo->gtt_fb_mem);
458 }
459 if (dinfo->gtt_cursor_mem) {
460 agp_unbind_memory(dinfo->gtt_cursor_mem);
461 agp_free_memory(dinfo->gtt_cursor_mem);
462 }
463 if (dinfo->gtt_ring_mem) {
464 agp_unbind_memory(dinfo->gtt_ring_mem);
465 agp_free_memory(dinfo->gtt_ring_mem);
466 }
467
468 if (dinfo->mmio_base)
469 iounmap((void __iomem *)dinfo->mmio_base);
470 if (dinfo->aperture.virtual)
471 iounmap((void __iomem *)dinfo->aperture.virtual);
472
Antonino A. Daplas4b3760c2005-12-19 15:11:40 +0800473 if (dinfo->flag & INTELFB_MMIO_ACQUIRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 release_mem_region(dinfo->mmio_base_phys, INTEL_REG_SIZE);
Antonino A. Daplas4b3760c2005-12-19 15:11:40 +0800475 if (dinfo->flag & INTELFB_FB_ACQUIRED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 release_mem_region(dinfo->aperture.physical,
477 dinfo->aperture.size);
478 framebuffer_release(dinfo->info);
479}
480
481#define bailout(dinfo) do { \
482 DBG_MSG("bailout\n"); \
483 cleanup(dinfo); \
484 INF_MSG("Not going to register framebuffer, exiting...\n"); \
485 return -ENODEV; \
486} while (0)
487
488
489static int __devinit
490intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
491{
492 struct fb_info *info;
493 struct intelfb_info *dinfo;
James Simmons4c7ffe02005-09-09 13:04:31 -0700494 int i, err, dvo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int aperture_size, stolen_size;
496 struct agp_kern_info gtt_info;
497 int agp_memtype;
498 const char *s;
499 struct agp_bridge_data *bridge;
500 int aperture_bar = 0;
501 int mmio_bar = 1;
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700502 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 DBG_MSG("intelfb_pci_register\n");
505
506 num_registered++;
507 if (num_registered != 1) {
508 ERR_MSG("Attempted to register %d devices "
509 "(should be only 1).\n", num_registered);
510 return -ENODEV;
511 }
512
513 info = framebuffer_alloc(sizeof(struct intelfb_info), &pdev->dev);
514 if (!info) {
515 ERR_MSG("Could not allocate memory for intelfb_info.\n");
516 return -ENODEV;
517 }
518 if (fb_alloc_cmap(&info->cmap, 256, 1) < 0) {
519 ERR_MSG("Could not allocate cmap for intelfb_info.\n");
520 goto err_out_cmap;
521 return -ENODEV;
522 }
523
524 dinfo = info->par;
525 dinfo->info = info;
526 dinfo->fbops = &intel_fb_ops;
527 dinfo->pdev = pdev;
528
529 /* Reserve pixmap space. */
530 info->pixmap.addr = kmalloc(64 * 1024, GFP_KERNEL);
531 if (info->pixmap.addr == NULL) {
532 ERR_MSG("Cannot reserve pixmap memory.\n");
533 goto err_out_pixmap;
534 }
535 memset(info->pixmap.addr, 0, 64 * 1024);
536
537 /* set early this option because it could be changed by tv encoder
538 driver */
539 dinfo->fixed_mode = fixed;
540
541 /* Enable device. */
542 if ((err = pci_enable_device(pdev))) {
543 ERR_MSG("Cannot enable device.\n");
544 cleanup(dinfo);
545 return -ENODEV;
546 }
547
548 /* Set base addresses. */
Scott MacKenzie3a590262005-11-07 01:00:33 -0800549 if ((ent->device == PCI_DEVICE_ID_INTEL_915G) ||
550 (ent->device == PCI_DEVICE_ID_INTEL_915GM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 aperture_bar = 2;
552 mmio_bar = 0;
Scott MacKenzie3a590262005-11-07 01:00:33 -0800553 /* Disable HW cursor on 915G/M (not implemented yet) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 hwcursor = 0;
555 }
556 dinfo->aperture.physical = pci_resource_start(pdev, aperture_bar);
557 dinfo->aperture.size = pci_resource_len(pdev, aperture_bar);
558 dinfo->mmio_base_phys = pci_resource_start(pdev, mmio_bar);
559 DBG_MSG("fb aperture: 0x%llx/0x%llx, MMIO region: 0x%llx/0x%llx\n",
560 (unsigned long long)pci_resource_start(pdev, aperture_bar),
561 (unsigned long long)pci_resource_len(pdev, aperture_bar),
562 (unsigned long long)pci_resource_start(pdev, mmio_bar),
563 (unsigned long long)pci_resource_len(pdev, mmio_bar));
564
565 /* Reserve the fb and MMIO regions */
566 if (!request_mem_region(dinfo->aperture.physical, dinfo->aperture.size,
567 INTELFB_MODULE_NAME)) {
568 ERR_MSG("Cannot reserve FB region.\n");
569 cleanup(dinfo);
570 return -ENODEV;
571 }
Antonino A. Daplas4b3760c2005-12-19 15:11:40 +0800572
573 dinfo->flag |= INTELFB_FB_ACQUIRED;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!request_mem_region(dinfo->mmio_base_phys,
576 INTEL_REG_SIZE,
577 INTELFB_MODULE_NAME)) {
578 ERR_MSG("Cannot reserve MMIO region.\n");
579 cleanup(dinfo);
580 return -ENODEV;
581 }
582
Antonino A. Daplas4b3760c2005-12-19 15:11:40 +0800583 dinfo->flag |= INTELFB_MMIO_ACQUIRED;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Get the chipset info. */
586 dinfo->pci_chipset = pdev->device;
587
588 if (intelfbhw_get_chipset(pdev, &dinfo->name, &dinfo->chipset,
589 &dinfo->mobile)) {
590 cleanup(dinfo);
591 return -ENODEV;
592 }
593
594 if (intelfbhw_get_memory(pdev, &aperture_size,&stolen_size)) {
595 cleanup(dinfo);
596 return -ENODEV;
597 }
598
599 INF_MSG("%02x:%02x.%d: %s, aperture size %dMB, "
600 "stolen memory %dkB\n",
601 pdev->bus->number, PCI_SLOT(pdev->devfn),
602 PCI_FUNC(pdev->devfn), dinfo->name,
603 BtoMB(aperture_size), BtoKB(stolen_size));
604
605 /* Set these from the options. */
606 dinfo->accel = accel;
607 dinfo->hwcursor = hwcursor;
608
609 if (NOACCEL_CHIPSET(dinfo) && dinfo->accel == 1) {
610 INF_MSG("Acceleration is not supported for the %s chipset.\n",
611 dinfo->name);
612 dinfo->accel = 0;
613 }
614
615 /* Framebuffer parameters - Use all the stolen memory if >= vram */
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800616 if (ROUND_UP_TO_PAGE(stolen_size) >= MB(vram)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 dinfo->fb.size = ROUND_UP_TO_PAGE(stolen_size);
618 dinfo->fbmem_gart = 0;
619 } else {
620 dinfo->fb.size = MB(vram);
621 dinfo->fbmem_gart = 1;
622 }
623
624 /* Allocate space for the ring buffer and HW cursor if enabled. */
625 if (dinfo->accel) {
626 dinfo->ring.size = RINGBUFFER_SIZE;
627 dinfo->ring_tail_mask = dinfo->ring.size - 1;
628 }
629 if (dinfo->hwcursor) {
630 dinfo->cursor.size = HW_CURSOR_SIZE;
631 }
632
633 /* Use agpgart to manage the GATT */
634 if (!(bridge = agp_backend_acquire(pdev))) {
635 ERR_MSG("cannot acquire agp\n");
636 cleanup(dinfo);
637 return -ENODEV;
638 }
639
640 /* get the current gatt info */
641 if (agp_copy_info(bridge, &gtt_info)) {
642 ERR_MSG("cannot get agp info\n");
643 agp_backend_release(bridge);
644 cleanup(dinfo);
645 return -ENODEV;
646 }
647
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800648 if (MB(voffset) < stolen_size)
649 offset = (stolen_size >> 12);
650 else
651 offset = ROUND_UP_TO_PAGE(MB(voffset))/GTT_PAGE_SIZE;
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* set the mem offsets - set them after the already used pages */
654 if (dinfo->accel) {
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700655 dinfo->ring.offset = offset + gtt_info.current_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 if (dinfo->hwcursor) {
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700658 dinfo->cursor.offset = offset +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 + gtt_info.current_memory + (dinfo->ring.size >> 12);
660 }
661 if (dinfo->fbmem_gart) {
Sylvain Meyer27aef2d2005-06-21 17:17:01 -0700662 dinfo->fb.offset = offset +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 + gtt_info.current_memory + (dinfo->ring.size >> 12)
664 + (dinfo->cursor.size >> 12);
665 }
666
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800667 /* Allocate memories (which aren't stolen) */
Sylvain Meyer6bd49342005-08-15 21:27:13 +0800668 /* Map the fb and MMIO regions */
669 /* ioremap only up to the end of used aperture */
670 dinfo->aperture.virtual = (u8 __iomem *)ioremap_nocache
Antonino A. Daplas375726d2005-09-29 05:19:51 +0800671 (dinfo->aperture.physical, ((offset + dinfo->fb.offset) << 12)
Sylvain Meyer6bd49342005-08-15 21:27:13 +0800672 + dinfo->fb.size);
673 if (!dinfo->aperture.virtual) {
674 ERR_MSG("Cannot remap FB region.\n");
675 cleanup(dinfo);
676 return -ENODEV;
677 }
678
679 dinfo->mmio_base =
680 (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
681 INTEL_REG_SIZE);
682 if (!dinfo->mmio_base) {
683 ERR_MSG("Cannot remap MMIO region.\n");
684 cleanup(dinfo);
685 return -ENODEV;
686 }
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (dinfo->accel) {
689 if (!(dinfo->gtt_ring_mem =
690 agp_allocate_memory(bridge, dinfo->ring.size >> 12,
691 AGP_NORMAL_MEMORY))) {
692 ERR_MSG("cannot allocate ring buffer memory\n");
693 agp_backend_release(bridge);
694 cleanup(dinfo);
695 return -ENOMEM;
696 }
697 if (agp_bind_memory(dinfo->gtt_ring_mem,
698 dinfo->ring.offset)) {
699 ERR_MSG("cannot bind ring buffer memory\n");
700 agp_backend_release(bridge);
701 cleanup(dinfo);
702 return -EBUSY;
703 }
704 dinfo->ring.physical = dinfo->aperture.physical
705 + (dinfo->ring.offset << 12);
706 dinfo->ring.virtual = dinfo->aperture.virtual
707 + (dinfo->ring.offset << 12);
708 dinfo->ring_head = dinfo->ring.virtual;
709 }
710 if (dinfo->hwcursor) {
711 agp_memtype = dinfo->mobile ? AGP_PHYSICAL_MEMORY
712 : AGP_NORMAL_MEMORY;
713 if (!(dinfo->gtt_cursor_mem =
714 agp_allocate_memory(bridge, dinfo->cursor.size >> 12,
715 agp_memtype))) {
716 ERR_MSG("cannot allocate cursor memory\n");
717 agp_backend_release(bridge);
718 cleanup(dinfo);
719 return -ENOMEM;
720 }
721 if (agp_bind_memory(dinfo->gtt_cursor_mem,
722 dinfo->cursor.offset)) {
723 ERR_MSG("cannot bind cursor memory\n");
724 agp_backend_release(bridge);
725 cleanup(dinfo);
726 return -EBUSY;
727 }
728 if (dinfo->mobile)
729 dinfo->cursor.physical
730 = dinfo->gtt_cursor_mem->physical;
731 else
732 dinfo->cursor.physical = dinfo->aperture.physical
733 + (dinfo->cursor.offset << 12);
734 dinfo->cursor.virtual = dinfo->aperture.virtual
735 + (dinfo->cursor.offset << 12);
736 }
737 if (dinfo->fbmem_gart) {
738 if (!(dinfo->gtt_fb_mem =
739 agp_allocate_memory(bridge, dinfo->fb.size >> 12,
740 AGP_NORMAL_MEMORY))) {
741 WRN_MSG("cannot allocate framebuffer memory - use "
742 "the stolen one\n");
743 dinfo->fbmem_gart = 0;
744 }
745 if (agp_bind_memory(dinfo->gtt_fb_mem,
746 dinfo->fb.offset)) {
747 WRN_MSG("cannot bind framebuffer memory - use "
748 "the stolen one\n");
749 dinfo->fbmem_gart = 0;
750 }
751 }
752
753 /* update framebuffer memory parameters */
754 if (!dinfo->fbmem_gart)
755 dinfo->fb.offset = 0; /* starts at offset 0 */
756 dinfo->fb.physical = dinfo->aperture.physical
757 + (dinfo->fb.offset << 12);
758 dinfo->fb.virtual = dinfo->aperture.virtual + (dinfo->fb.offset << 12);
759 dinfo->fb_start = dinfo->fb.offset << 12;
760
761 /* release agpgart */
762 agp_backend_release(bridge);
763
764 if (mtrr)
765 set_mtrr(dinfo);
766
767 DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%x)\n",
768 dinfo->fb.physical, dinfo->fb.offset, dinfo->fb.size,
769 (u32 __iomem ) dinfo->fb.virtual);
770 DBG_MSG("MMIO: 0x%x/0x%x (0x%x)\n",
771 dinfo->mmio_base_phys, INTEL_REG_SIZE,
772 (u32 __iomem) dinfo->mmio_base);
773 DBG_MSG("ring buffer: 0x%x/0x%x (0x%x)\n",
774 dinfo->ring.physical, dinfo->ring.size,
775 (u32 __iomem ) dinfo->ring.virtual);
776 DBG_MSG("HW cursor: 0x%x/0x%x (0x%x) (offset 0x%x) (phys 0x%x)\n",
777 dinfo->cursor.physical, dinfo->cursor.size,
778 (u32 __iomem ) dinfo->cursor.virtual, dinfo->cursor.offset,
779 dinfo->cursor.physical);
780
781 DBG_MSG("options: vram = %d, accel = %d, hwcursor = %d, fixed = %d, "
782 "noinit = %d\n", vram, accel, hwcursor, fixed, noinit);
783 DBG_MSG("options: mode = \"%s\"\n", mode ? mode : "");
784
785 if (probeonly)
786 bailout(dinfo);
787
788 /*
789 * Check if the LVDS port or any DVO ports are enabled. If so,
790 * don't allow mode switching
791 */
792 dvo = intelfbhw_check_non_crt(dinfo);
793 if (dvo) {
794 dinfo->fixed_mode = 1;
795 WRN_MSG("Non-CRT device is enabled ( ");
796 i = 0;
797 while (dvo) {
798 if (dvo & 1) {
799 s = intelfbhw_dvo_to_string(1 << i);
800 if (s)
801 printk("%s ", s);
802 }
803 dvo >>= 1;
804 ++i;
805 }
806 printk("). Disabling mode switching.\n");
807 }
808
809 if (bailearly == 1)
810 bailout(dinfo);
811
812 if (FIXED_MODE(dinfo) && ORIG_VIDEO_ISVGA != VIDEO_TYPE_VLFB) {
813 ERR_MSG("Video mode must be programmed at boot time.\n");
814 cleanup(dinfo);
815 return -ENODEV;
816 }
817
818 if (bailearly == 2)
819 bailout(dinfo);
820
821 /* Initialise dinfo and related data. */
822 /* If an initial mode was programmed at boot time, get its details. */
823 if (ORIG_VIDEO_ISVGA == VIDEO_TYPE_VLFB)
824 get_initial_mode(dinfo);
825
826 if (bailearly == 3)
827 bailout(dinfo);
828
829 if (FIXED_MODE(dinfo)) {
830 /* remap fb address */
831 update_dinfo(dinfo, &dinfo->initial_var);
832 }
833
834 if (bailearly == 4)
835 bailout(dinfo);
836
837
838 if (intelfb_set_fbinfo(dinfo)) {
839 cleanup(dinfo);
840 return -ENODEV;
841 }
842
843 if (bailearly == 5)
844 bailout(dinfo);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (bailearly == 6)
847 bailout(dinfo);
848
849 pci_set_drvdata(pdev, dinfo);
850
851 /* Save the initial register state. */
852 i = intelfbhw_read_hw_state(dinfo, &dinfo->save_state,
853 bailearly > 6 ? bailearly - 6 : 0);
854 if (i != 0) {
855 DBG_MSG("intelfbhw_read_hw_state returned %d\n", i);
856 bailout(dinfo);
857 }
858
859 intelfbhw_print_hw_state(dinfo, &dinfo->save_state);
860
861 if (bailearly == 18)
862 bailout(dinfo);
863
864 /* Cursor initialisation */
865 if (dinfo->hwcursor) {
866 intelfbhw_cursor_init(dinfo);
867 intelfbhw_cursor_reset(dinfo);
868 }
869
870 if (bailearly == 19)
871 bailout(dinfo);
872
873 /* 2d acceleration init */
874 if (dinfo->accel)
875 intelfbhw_2d_start(dinfo);
876
877 if (bailearly == 20)
878 bailout(dinfo);
879
880 if (noregister)
881 bailout(dinfo);
882
883 if (register_framebuffer(dinfo->info) < 0) {
884 ERR_MSG("Cannot register framebuffer.\n");
885 cleanup(dinfo);
886 return -ENODEV;
887 }
888
889 dinfo->registered = 1;
890
891 return 0;
892
893err_out_pixmap:
894 fb_dealloc_cmap(&info->cmap);
895err_out_cmap:
896 framebuffer_release(info);
897 return -ENODEV;
898}
899
900static void __devexit
901intelfb_pci_unregister(struct pci_dev *pdev)
902{
903 struct intelfb_info *dinfo = pci_get_drvdata(pdev);
904
905 DBG_MSG("intelfb_pci_unregister\n");
906
907 if (!dinfo)
908 return;
909
910 cleanup(dinfo);
911
912 pci_set_drvdata(pdev, NULL);
913}
914
915/***************************************************************
916 * helper functions *
917 ***************************************************************/
918
919int __inline__
920intelfb_var_to_depth(const struct fb_var_screeninfo *var)
921{
922 DBG_MSG("intelfb_var_to_depth: bpp: %d, green.length is %d\n",
923 var->bits_per_pixel, var->green.length);
924
925 switch (var->bits_per_pixel) {
926 case 16:
927 return (var->green.length == 6) ? 16 : 15;
928 case 32:
929 return 24;
930 default:
931 return var->bits_per_pixel;
932 }
933}
934
935
936static __inline__ int
937var_to_refresh(const struct fb_var_screeninfo *var)
938{
939 int xtot = var->xres + var->left_margin + var->right_margin +
940 var->hsync_len;
941 int ytot = var->yres + var->upper_margin + var->lower_margin +
942 var->vsync_len;
943
944 return (1000000000 / var->pixclock * 1000 + 500) / xtot / ytot;
945}
946
947/***************************************************************
948 * Various intialisation functions *
949 ***************************************************************/
950
951static void __devinit
952get_initial_mode(struct intelfb_info *dinfo)
953{
954 struct fb_var_screeninfo *var;
955 int xtot, ytot;
956
957 DBG_MSG("get_initial_mode\n");
958
959 dinfo->initial_vga = 1;
960 dinfo->initial_fb_base = screen_info.lfb_base;
961 dinfo->initial_video_ram = screen_info.lfb_size * KB(64);
962 dinfo->initial_pitch = screen_info.lfb_linelength;
963
964 var = &dinfo->initial_var;
965 memset(var, 0, sizeof(*var));
966 var->xres = screen_info.lfb_width;
967 var->yres = screen_info.lfb_height;
968 var->bits_per_pixel = screen_info.lfb_depth;
969 switch (screen_info.lfb_depth) {
970 case 15:
971 var->bits_per_pixel = 16;
972 break;
973 case 24:
974 var->bits_per_pixel = 32;
975 break;
976 }
977
978 DBG_MSG("Initial info: FB is 0x%x/0x%x (%d kByte)\n",
979 dinfo->initial_fb_base, dinfo->initial_video_ram,
980 BtoKB(dinfo->initial_video_ram));
981
982 DBG_MSG("Initial info: mode is %dx%d-%d (%d)\n",
983 var->xres, var->yres, var->bits_per_pixel,
984 dinfo->initial_pitch);
985
986 /* Dummy timing values (assume 60Hz) */
987 var->left_margin = (var->xres / 8) & 0xf8;
988 var->right_margin = 32;
989 var->upper_margin = 16;
990 var->lower_margin = 4;
991 var->hsync_len = (var->xres / 8) & 0xf8;
992 var->vsync_len = 4;
993
994 xtot = var->xres + var->left_margin +
995 var->right_margin + var->hsync_len;
996 ytot = var->yres + var->upper_margin +
997 var->lower_margin + var->vsync_len;
998 var->pixclock = 10000000 / xtot * 1000 / ytot * 100 / 60;
999
1000 var->height = -1;
1001 var->width = -1;
1002
1003 if (var->bits_per_pixel > 8) {
1004 var->red.offset = screen_info.red_pos;
1005 var->red.length = screen_info.red_size;
1006 var->green.offset = screen_info.green_pos;
1007 var->green.length = screen_info.green_size;
1008 var->blue.offset = screen_info.blue_pos;
1009 var->blue.length = screen_info.blue_size;
1010 var->transp.offset = screen_info.rsvd_pos;
1011 var->transp.length = screen_info.rsvd_size;
1012 } else {
1013 var->red.length = 8;
1014 var->green.length = 8;
1015 var->blue.length = 8;
1016 }
1017}
1018
1019static int __devinit
1020intelfb_init_var(struct intelfb_info *dinfo)
1021{
1022 struct fb_var_screeninfo *var;
1023 int msrc = 0;
1024
1025 DBG_MSG("intelfb_init_var\n");
1026
1027 var = &dinfo->info->var;
1028 if (FIXED_MODE(dinfo)) {
1029 memcpy(var, &dinfo->initial_var,
1030 sizeof(struct fb_var_screeninfo));
1031 msrc = 5;
1032 } else {
1033 if (mode) {
1034 msrc = fb_find_mode(var, dinfo->info, mode,
1035 vesa_modes, VESA_MODEDB_SIZE,
1036 NULL, 0);
1037 if (msrc)
1038 msrc |= 8;
1039 }
1040 if (!msrc) {
1041 msrc = fb_find_mode(var, dinfo->info, PREFERRED_MODE,
1042 vesa_modes, VESA_MODEDB_SIZE,
1043 NULL, 0);
1044 }
1045 }
1046
1047 if (!msrc) {
1048 ERR_MSG("Cannot find a suitable video mode.\n");
1049 return 1;
1050 }
1051
1052 INF_MSG("Initial video mode is %dx%d-%d@%d.\n", var->xres, var->yres,
1053 var->bits_per_pixel, var_to_refresh(var));
1054
1055 DBG_MSG("Initial video mode is from %d.\n", msrc);
1056
1057#if ALLOCATE_FOR_PANNING
1058 /* Allow use of half of the video ram for panning */
1059 var->xres_virtual = var->xres;
1060 var->yres_virtual =
1061 dinfo->fb.size / 2 / (var->bits_per_pixel * var->xres);
1062 if (var->yres_virtual < var->yres)
1063 var->yres_virtual = var->yres;
1064#else
1065 var->yres_virtual = var->yres;
1066#endif
1067
1068 if (dinfo->accel)
1069 var->accel_flags |= FB_ACCELF_TEXT;
1070 else
1071 var->accel_flags &= ~FB_ACCELF_TEXT;
1072
1073 return 0;
1074}
1075
1076static int __devinit
1077intelfb_set_fbinfo(struct intelfb_info *dinfo)
1078{
1079 struct fb_info *info = dinfo->info;
1080
1081 DBG_MSG("intelfb_set_fbinfo\n");
1082
1083 info->flags = FBINFO_FLAG_DEFAULT;
1084 info->fbops = &intel_fb_ops;
1085 info->pseudo_palette = dinfo->pseudo_palette;
1086
1087 info->pixmap.size = 64*1024;
1088 info->pixmap.buf_align = 8;
James Simmons58a60642005-06-21 17:17:08 -07001089 info->pixmap.access_align = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 info->pixmap.flags = FB_PIXMAP_SYSTEM;
1091
1092 if (intelfb_init_var(dinfo))
1093 return 1;
1094
1095 info->pixmap.scan_align = 1;
Antonino A. Daplas33c37c02005-12-19 15:11:49 +08001096 strcpy(info->fix.id, dinfo->name);
1097 info->fix.smem_start = dinfo->fb.physical;
1098 info->fix.smem_len = dinfo->fb.size;
1099 info->fix.type = FB_TYPE_PACKED_PIXELS;
1100 info->fix.type_aux = 0;
1101 info->fix.xpanstep = 8;
1102 info->fix.ypanstep = 1;
1103 info->fix.ywrapstep = 0;
1104 info->fix.mmio_start = dinfo->mmio_base_phys;
1105 info->fix.mmio_len = INTEL_REG_SIZE;
1106 info->fix.accel = FB_ACCEL_I830;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 update_dinfo(dinfo, &info->var);
1108
1109 return 0;
1110}
1111
1112/* Update dinfo to match the active video mode. */
1113static void
1114update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
1115{
1116 DBG_MSG("update_dinfo\n");
1117
1118 dinfo->bpp = var->bits_per_pixel;
1119 dinfo->depth = intelfb_var_to_depth(var);
1120 dinfo->xres = var->xres;
1121 dinfo->yres = var->xres;
1122 dinfo->pixclock = var->pixclock;
1123
Antonino A. Daplas33c37c02005-12-19 15:11:49 +08001124 dinfo->info->fix.visual = dinfo->visual;
1125 dinfo->info->fix.line_length = dinfo->pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 switch (dinfo->bpp) {
1128 case 8:
1129 dinfo->visual = FB_VISUAL_PSEUDOCOLOR;
1130 dinfo->pitch = var->xres_virtual;
1131 break;
1132 case 16:
1133 dinfo->visual = FB_VISUAL_TRUECOLOR;
1134 dinfo->pitch = var->xres_virtual * 2;
1135 break;
1136 case 32:
1137 dinfo->visual = FB_VISUAL_TRUECOLOR;
1138 dinfo->pitch = var->xres_virtual * 4;
1139 break;
1140 }
1141
1142 /* Make sure the line length is a aligned correctly. */
1143 dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
1144
1145 if (FIXED_MODE(dinfo))
1146 dinfo->pitch = dinfo->initial_pitch;
1147
1148 dinfo->info->screen_base = (char __iomem *)dinfo->fb.virtual;
1149 dinfo->info->fix.line_length = dinfo->pitch;
1150 dinfo->info->fix.visual = dinfo->visual;
1151}
1152
1153/* fbops functions */
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155/***************************************************************
1156 * fbdev interface *
1157 ***************************************************************/
1158
1159static int
1160intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1161{
1162 int change_var = 0;
1163 struct fb_var_screeninfo v;
1164 struct intelfb_info *dinfo;
1165 static int first = 1;
1166
1167 DBG_MSG("intelfb_check_var: accel_flags is %d\n", var->accel_flags);
1168
1169 dinfo = GET_DINFO(info);
1170
1171 if (intelfbhw_validate_mode(dinfo, var) != 0)
1172 return -EINVAL;
1173
1174 v = *var;
1175
1176 /* Check for a supported bpp. */
1177 if (v.bits_per_pixel <= 8) {
1178 v.bits_per_pixel = 8;
1179 } else if (v.bits_per_pixel <= 16) {
1180 if (v.bits_per_pixel == 16)
1181 v.green.length = 6;
1182 v.bits_per_pixel = 16;
1183 } else if (v.bits_per_pixel <= 32) {
1184 v.bits_per_pixel = 32;
1185 } else
1186 return -EINVAL;
1187
1188 change_var = ((info->var.xres != var->xres) ||
1189 (info->var.yres != var->yres) ||
1190 (info->var.xres_virtual != var->xres_virtual) ||
1191 (info->var.yres_virtual != var->yres_virtual) ||
1192 (info->var.bits_per_pixel != var->bits_per_pixel) ||
1193 memcmp(&info->var.red, &var->red, sizeof(var->red)) ||
1194 memcmp(&info->var.green, &var->green,
1195 sizeof(var->green)) ||
1196 memcmp(&info->var.blue, &var->blue, sizeof(var->blue)));
1197
1198 if (FIXED_MODE(dinfo) &&
1199 (change_var ||
1200 var->yres_virtual > dinfo->initial_var.yres_virtual ||
1201 var->yres_virtual < dinfo->initial_var.yres ||
1202 var->xoffset || var->nonstd)) {
1203 if (first) {
1204 ERR_MSG("Changing the video mode is not supported.\n");
1205 first = 0;
1206 }
1207 return -EINVAL;
1208 }
1209
1210 switch (intelfb_var_to_depth(&v)) {
1211 case 8:
1212 v.red.offset = v.green.offset = v.blue.offset = 0;
1213 v.red.length = v.green.length = v.blue.length = 8;
1214 v.transp.offset = v.transp.length = 0;
1215 break;
1216 case 15:
1217 v.red.offset = 10;
1218 v.green.offset = 5;
1219 v.blue.offset = 0;
1220 v.red.length = v.green.length = v.blue.length = 5;
1221 v.transp.offset = v.transp.length = 0;
1222 break;
1223 case 16:
1224 v.red.offset = 11;
1225 v.green.offset = 5;
1226 v.blue.offset = 0;
1227 v.red.length = 5;
1228 v.green.length = 6;
1229 v.blue.length = 5;
1230 v.transp.offset = v.transp.length = 0;
1231 break;
1232 case 24:
1233 v.red.offset = 16;
1234 v.green.offset = 8;
1235 v.blue.offset = 0;
1236 v.red.length = v.green.length = v.blue.length = 8;
1237 v.transp.offset = v.transp.length = 0;
1238 break;
1239 case 32:
1240 v.red.offset = 16;
1241 v.green.offset = 8;
1242 v.blue.offset = 0;
1243 v.red.length = v.green.length = v.blue.length = 8;
1244 v.transp.offset = 24;
1245 v.transp.length = 8;
1246 break;
1247 }
1248
1249 if (v.xoffset < 0)
1250 v.xoffset = 0;
1251 if (v.yoffset < 0)
1252 v.yoffset = 0;
1253
1254 if (v.xoffset > v.xres_virtual - v.xres)
1255 v.xoffset = v.xres_virtual - v.xres;
1256 if (v.yoffset > v.yres_virtual - v.yres)
1257 v.yoffset = v.yres_virtual - v.yres;
1258
1259 v.red.msb_right = v.green.msb_right = v.blue.msb_right =
1260 v.transp.msb_right = 0;
1261
1262 *var = v;
1263
1264 return 0;
1265}
1266
1267static int
1268intelfb_set_par(struct fb_info *info)
1269{
1270 struct intelfb_hwstate *hw;
1271 struct intelfb_info *dinfo = GET_DINFO(info);
1272
1273 if (FIXED_MODE(dinfo)) {
1274 ERR_MSG("Changing the video mode is not supported.\n");
1275 return -EINVAL;
1276 }
1277
1278 hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
1279 if (!hw)
1280 return -ENOMEM;
1281
1282 DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
1283 info->var.yres, info->var.bits_per_pixel);
1284
1285 intelfb_blank(FB_BLANK_POWERDOWN, info);
1286
Sylvain Meyerdf529332005-06-21 17:17:02 -07001287 if (ACCEL(dinfo, info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 intelfbhw_2d_stop(dinfo);
1289
1290 memcpy(hw, &dinfo->save_state, sizeof(*hw));
1291 if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
1292 goto invalid_mode;
1293 if (intelfbhw_program_mode(dinfo, hw, 0))
1294 goto invalid_mode;
1295
1296#if REGDUMP > 0
1297 intelfbhw_read_hw_state(dinfo, hw, 0);
1298 intelfbhw_print_hw_state(dinfo, hw);
1299#endif
1300
1301 update_dinfo(dinfo, &info->var);
1302
Sylvain Meyerdf529332005-06-21 17:17:02 -07001303 if (ACCEL(dinfo, info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 intelfbhw_2d_start(dinfo);
1305
1306 intelfb_pan_display(&info->var, info);
1307
1308 intelfb_blank(FB_BLANK_UNBLANK, info);
1309
1310 if (ACCEL(dinfo, info)) {
1311 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
1312 FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
1313 FBINFO_HWACCEL_IMAGEBLIT;
1314 } else {
1315 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
1316 }
1317 kfree(hw);
1318 return 0;
1319invalid_mode:
1320 kfree(hw);
1321 return -EINVAL;
1322}
1323
1324static int
1325intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
1326 unsigned blue, unsigned transp, struct fb_info *info)
1327{
1328 struct intelfb_info *dinfo = GET_DINFO(info);
1329
1330#if VERBOSE > 0
1331 DBG_MSG("intelfb_setcolreg: regno %d, depth %d\n", regno, dinfo->depth);
1332#endif
1333
1334 if (regno > 255)
1335 return 1;
1336
1337 switch (dinfo->depth) {
1338 case 8:
1339 {
1340 red >>= 8;
1341 green >>= 8;
1342 blue >>= 8;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 intelfbhw_setcolreg(dinfo, regno, red, green, blue,
1345 transp);
1346 }
1347 break;
1348 case 15:
1349 dinfo->pseudo_palette[regno] = ((red & 0xf800) >> 1) |
1350 ((green & 0xf800) >> 6) |
1351 ((blue & 0xf800) >> 11);
1352 break;
1353 case 16:
1354 dinfo->pseudo_palette[regno] = (red & 0xf800) |
1355 ((green & 0xfc00) >> 5) |
1356 ((blue & 0xf800) >> 11);
1357 break;
1358 case 24:
1359 dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
1360 (green & 0xff00) |
1361 ((blue & 0xff00) >> 8);
1362 break;
1363 }
1364 return 0;
1365}
1366
1367static int
1368intelfb_blank(int blank, struct fb_info *info)
1369{
1370 intelfbhw_do_blank(blank, info);
1371 return 0;
1372}
1373
1374static int
1375intelfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1376{
1377 intelfbhw_pan_display(var, info);
1378 return 0;
1379}
1380
1381/* When/if we have our own ioctls. */
1382static int
1383intelfb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1384 unsigned long arg, struct fb_info *info)
1385{
1386 int retval = 0;
1387
1388 return retval;
1389}
1390
1391static void
1392intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
1393{
1394 struct intelfb_info *dinfo = GET_DINFO(info);
1395 u32 rop, color;
1396
1397#if VERBOSE > 0
1398 DBG_MSG("intelfb_fillrect\n");
1399#endif
1400
1401 if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1402 return cfb_fillrect(info, rect);
1403
1404 if (rect->rop == ROP_COPY)
1405 rop = PAT_ROP_GXCOPY;
1406 else // ROP_XOR
1407 rop = PAT_ROP_GXXOR;
1408
1409 if (dinfo->depth != 8)
1410 color = dinfo->pseudo_palette[rect->color];
1411 else
1412 color = rect->color;
1413
1414 intelfbhw_do_fillrect(dinfo, rect->dx, rect->dy,
1415 rect->width, rect->height, color,
1416 dinfo->pitch, info->var.bits_per_pixel,
1417 rop);
1418}
1419
1420static void
1421intelfb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
1422{
1423 struct intelfb_info *dinfo = GET_DINFO(info);
1424
1425#if VERBOSE > 0
1426 DBG_MSG("intelfb_copyarea\n");
1427#endif
1428
1429 if (!ACCEL(dinfo, info) || dinfo->depth == 4)
1430 return cfb_copyarea(info, region);
1431
1432 intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
1433 region->dy, region->width, region->height,
1434 dinfo->pitch, info->var.bits_per_pixel);
1435}
1436
1437static void
1438intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
1439{
1440 struct intelfb_info *dinfo = GET_DINFO(info);
1441 u32 fgcolor, bgcolor;
1442
1443#if VERBOSE > 0
1444 DBG_MSG("intelfb_imageblit\n");
1445#endif
1446
1447 if (!ACCEL(dinfo, info) || dinfo->depth == 4
1448 || image->depth != 1)
1449 return cfb_imageblit(info, image);
1450
1451 if (dinfo->depth != 8) {
1452 fgcolor = dinfo->pseudo_palette[image->fg_color];
1453 bgcolor = dinfo->pseudo_palette[image->bg_color];
1454 } else {
1455 fgcolor = image->fg_color;
1456 bgcolor = image->bg_color;
1457 }
1458
1459 if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
1460 image->height, image->data,
1461 image->dx, image->dy,
1462 dinfo->pitch, info->var.bits_per_pixel))
1463 return cfb_imageblit(info, image);
1464}
1465
1466static int
1467intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
1468{
1469 struct intelfb_info *dinfo = GET_DINFO(info);
1470
1471#if VERBOSE > 0
1472 DBG_MSG("intelfb_cursor\n");
1473#endif
1474
1475 if (!dinfo->hwcursor)
Antonino A. Daplasc465e052005-11-07 01:00:35 -08001476 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 intelfbhw_cursor_hide(dinfo);
1479
1480 /* If XFree killed the cursor - restore it */
1481 if (INREG(CURSOR_A_BASEADDR) != dinfo->cursor.offset << 12) {
1482 u32 fg, bg;
1483
1484 DBG_MSG("the cursor was killed - restore it !!\n");
1485 DBG_MSG("size %d, %d pos %d, %d\n",
1486 cursor->image.width, cursor->image.height,
1487 cursor->image.dx, cursor->image.dy);
1488
1489 intelfbhw_cursor_init(dinfo);
1490 intelfbhw_cursor_reset(dinfo);
1491 intelfbhw_cursor_setpos(dinfo, cursor->image.dx,
1492 cursor->image.dy);
1493
1494 if (dinfo->depth != 8) {
1495 fg =dinfo->pseudo_palette[cursor->image.fg_color];
1496 bg =dinfo->pseudo_palette[cursor->image.bg_color];
1497 } else {
1498 fg = cursor->image.fg_color;
1499 bg = cursor->image.bg_color;
1500 }
1501 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1502 intelfbhw_cursor_load(dinfo, cursor->image.width,
1503 cursor->image.height,
1504 dinfo->cursor_src);
1505
1506 if (cursor->enable)
1507 intelfbhw_cursor_show(dinfo);
1508 return 0;
1509 }
1510
1511 if (cursor->set & FB_CUR_SETPOS) {
1512 u32 dx, dy;
1513
1514 dx = cursor->image.dx - info->var.xoffset;
1515 dy = cursor->image.dy - info->var.yoffset;
1516
1517 intelfbhw_cursor_setpos(dinfo, dx, dy);
1518 }
1519
1520 if (cursor->set & FB_CUR_SETSIZE) {
1521 if (cursor->image.width > 64 || cursor->image.height > 64)
1522 return -ENXIO;
1523
1524 intelfbhw_cursor_reset(dinfo);
1525 }
1526
1527 if (cursor->set & FB_CUR_SETCMAP) {
1528 u32 fg, bg;
1529
1530 if (dinfo->depth != 8) {
1531 fg = dinfo->pseudo_palette[cursor->image.fg_color];
1532 bg = dinfo->pseudo_palette[cursor->image.bg_color];
1533 } else {
1534 fg = cursor->image.fg_color;
1535 bg = cursor->image.bg_color;
1536 }
1537
1538 intelfbhw_cursor_setcolor(dinfo, bg, fg);
1539 }
1540
1541 if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
1542 u32 s_pitch = (ROUND_UP_TO(cursor->image.width, 8) / 8);
1543 u32 size = s_pitch * cursor->image.height;
1544 u8 *dat = (u8 *) cursor->image.data;
1545 u8 *msk = (u8 *) cursor->mask;
1546 u8 src[64];
1547 u32 i;
1548
1549 if (cursor->image.depth != 1)
1550 return -ENXIO;
1551
1552 switch (cursor->rop) {
1553 case ROP_XOR:
1554 for (i = 0; i < size; i++)
1555 src[i] = dat[i] ^ msk[i];
1556 break;
1557 case ROP_COPY:
1558 default:
1559 for (i = 0; i < size; i++)
1560 src[i] = dat[i] & msk[i];
1561 break;
1562 }
1563
1564 /* save the bitmap to restore it when XFree will
1565 make the cursor dirty */
1566 memcpy(dinfo->cursor_src, src, size);
1567
1568 intelfbhw_cursor_load(dinfo, cursor->image.width,
1569 cursor->image.height, src);
1570 }
1571
1572 if (cursor->enable)
1573 intelfbhw_cursor_show(dinfo);
1574
1575 return 0;
1576}
1577
1578static int
1579intelfb_sync(struct fb_info *info)
1580{
1581 struct intelfb_info *dinfo = GET_DINFO(info);
1582
1583#if VERBOSE > 0
1584 DBG_MSG("intelfb_sync\n");
1585#endif
1586
1587 if (dinfo->ring_lockup)
1588 return 0;
1589
1590 intelfbhw_do_sync(dinfo);
1591 return 0;
1592}
1593