include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6.git] / drivers / video / vesafb.c
1 /*
2  * framebuffer driver for VBE 2.0 compliant graphic boards
3  *
4  * switching to graphics mode happens at boot time (while
5  * running in real mode, see arch/i386/boot/video.S).
6  *
7  * (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
8  *
9  */
10
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/mm.h>
16 #include <linux/delay.h>
17 #include <linux/fb.h>
18 #include <linux/ioport.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/screen_info.h>
22
23 #include <video/vga.h>
24 #include <asm/io.h>
25 #include <asm/mtrr.h>
26
27 #define dac_reg (0x3c8)
28 #define dac_val (0x3c9)
29
30 /* --------------------------------------------------------------------- */
31
32 static struct fb_var_screeninfo vesafb_defined __initdata = {
33         .activate       = FB_ACTIVATE_NOW,
34         .height         = -1,
35         .width          = -1,
36         .right_margin   = 32,
37         .upper_margin   = 16,
38         .lower_margin   = 4,
39         .vsync_len      = 4,
40         .vmode          = FB_VMODE_NONINTERLACED,
41 };
42
43 static struct fb_fix_screeninfo vesafb_fix __initdata = {
44         .id     = "VESA VGA",
45         .type   = FB_TYPE_PACKED_PIXELS,
46         .accel  = FB_ACCEL_NONE,
47 };
48
49 static int   inverse    __read_mostly;
50 static int   mtrr       __read_mostly;          /* disable mtrr */
51 static int   vram_remap __initdata;             /* Set amount of memory to be used */
52 static int   vram_total __initdata;             /* Set total amount of memory */
53 static int   pmi_setpal __read_mostly = 1;      /* pmi for palette changes ??? */
54 static int   ypan       __read_mostly;          /* 0..nothing, 1..ypan, 2..ywrap */
55 static void  (*pmi_start)(void) __read_mostly;
56 static void  (*pmi_pal)  (void) __read_mostly;
57 static int   depth      __read_mostly;
58 static int   vga_compat __read_mostly;
59 /* --------------------------------------------------------------------- */
60
61 static int vesafb_pan_display(struct fb_var_screeninfo *var,
62                               struct fb_info *info)
63 {
64 #ifdef __i386__
65         int offset;
66
67         offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
68
69         __asm__ __volatile__(
70                 "call *(%%edi)"
71                 : /* no return value */
72                 : "a" (0x4f07),         /* EAX */
73                   "b" (0),              /* EBX */
74                   "c" (offset),         /* ECX */
75                   "d" (offset >> 16),   /* EDX */
76                   "D" (&pmi_start));    /* EDI */
77 #endif
78         return 0;
79 }
80
81 static int vesa_setpalette(int regno, unsigned red, unsigned green,
82                             unsigned blue)
83 {
84         int shift = 16 - depth;
85         int err = -EINVAL;
86
87 /*
88  * Try VGA registers first...
89  */
90         if (vga_compat) {
91                 outb_p(regno,       dac_reg);
92                 outb_p(red   >> shift, dac_val);
93                 outb_p(green >> shift, dac_val);
94                 outb_p(blue  >> shift, dac_val);
95                 err = 0;
96         }
97
98 #ifdef __i386__
99 /*
100  * Fallback to the PMI....
101  */
102         if (err && pmi_setpal) {
103                 struct { u_char blue, green, red, pad; } entry;
104
105                 entry.red   = red   >> shift;
106                 entry.green = green >> shift;
107                 entry.blue  = blue  >> shift;
108                 entry.pad   = 0;
109                 __asm__ __volatile__(
110                 "call *(%%esi)"
111                 : /* no return value */
112                 : "a" (0x4f09),         /* EAX */
113                   "b" (0),              /* EBX */
114                   "c" (1),              /* ECX */
115                   "d" (regno),          /* EDX */
116                   "D" (&entry),         /* EDI */
117                   "S" (&pmi_pal));      /* ESI */
118                 err = 0;
119         }
120 #endif
121
122         return err;
123 }
124
125 static int vesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
126                             unsigned blue, unsigned transp,
127                             struct fb_info *info)
128 {
129         int err = 0;
130
131         /*
132          *  Set a single color register. The values supplied are
133          *  already rounded down to the hardware's capabilities
134          *  (according to the entries in the `var' structure). Return
135          *  != 0 for invalid regno.
136          */
137         
138         if (regno >= info->cmap.len)
139                 return 1;
140
141         if (info->var.bits_per_pixel == 8)
142                 err = vesa_setpalette(regno,red,green,blue);
143         else if (regno < 16) {
144                 switch (info->var.bits_per_pixel) {
145                 case 16:
146                         if (info->var.red.offset == 10) {
147                                 /* 1:5:5:5 */
148                                 ((u32*) (info->pseudo_palette))[regno] =
149                                         ((red   & 0xf800) >>  1) |
150                                         ((green & 0xf800) >>  6) |
151                                         ((blue  & 0xf800) >> 11);
152                         } else {
153                                 /* 0:5:6:5 */
154                                 ((u32*) (info->pseudo_palette))[regno] =
155                                         ((red   & 0xf800)      ) |
156                                         ((green & 0xfc00) >>  5) |
157                                         ((blue  & 0xf800) >> 11);
158                         }
159                         break;
160                 case 24:
161                 case 32:
162                         red   >>= 8;
163                         green >>= 8;
164                         blue  >>= 8;
165                         ((u32 *)(info->pseudo_palette))[regno] =
166                                 (red   << info->var.red.offset)   |
167                                 (green << info->var.green.offset) |
168                                 (blue  << info->var.blue.offset);
169                         break;
170                 }
171         }
172
173         return err;
174 }
175
176 static void vesafb_destroy(struct fb_info *info)
177 {
178         if (info->screen_base)
179                 iounmap(info->screen_base);
180         release_mem_region(info->aperture_base, info->aperture_size);
181         framebuffer_release(info);
182 }
183
184 static struct fb_ops vesafb_ops = {
185         .owner          = THIS_MODULE,
186         .fb_destroy     = vesafb_destroy,
187         .fb_setcolreg   = vesafb_setcolreg,
188         .fb_pan_display = vesafb_pan_display,
189         .fb_fillrect    = cfb_fillrect,
190         .fb_copyarea    = cfb_copyarea,
191         .fb_imageblit   = cfb_imageblit,
192 };
193
194 static int __init vesafb_setup(char *options)
195 {
196         char *this_opt;
197         
198         if (!options || !*options)
199                 return 0;
200         
201         while ((this_opt = strsep(&options, ",")) != NULL) {
202                 if (!*this_opt) continue;
203                 
204                 if (! strcmp(this_opt, "inverse"))
205                         inverse=1;
206                 else if (! strcmp(this_opt, "redraw"))
207                         ypan=0;
208                 else if (! strcmp(this_opt, "ypan"))
209                         ypan=1;
210                 else if (! strcmp(this_opt, "ywrap"))
211                         ypan=2;
212                 else if (! strcmp(this_opt, "vgapal"))
213                         pmi_setpal=0;
214                 else if (! strcmp(this_opt, "pmipal"))
215                         pmi_setpal=1;
216                 else if (! strncmp(this_opt, "mtrr:", 5))
217                         mtrr = simple_strtoul(this_opt+5, NULL, 0);
218                 else if (! strcmp(this_opt, "nomtrr"))
219                         mtrr=0;
220                 else if (! strncmp(this_opt, "vtotal:", 7))
221                         vram_total = simple_strtoul(this_opt+7, NULL, 0);
222                 else if (! strncmp(this_opt, "vremap:", 7))
223                         vram_remap = simple_strtoul(this_opt+7, NULL, 0);
224         }
225         return 0;
226 }
227
228 static int __devinit vesafb_probe(struct platform_device *dev)
229 {
230         struct fb_info *info;
231         int i, err;
232         unsigned int size_vmode;
233         unsigned int size_remap;
234         unsigned int size_total;
235
236         if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
237                 return -ENODEV;
238
239         vga_compat = (screen_info.capabilities & 2) ? 0 : 1;
240         vesafb_fix.smem_start = screen_info.lfb_base;
241         vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
242         if (15 == vesafb_defined.bits_per_pixel)
243                 vesafb_defined.bits_per_pixel = 16;
244         vesafb_defined.xres = screen_info.lfb_width;
245         vesafb_defined.yres = screen_info.lfb_height;
246         vesafb_fix.line_length = screen_info.lfb_linelength;
247         vesafb_fix.visual   = (vesafb_defined.bits_per_pixel == 8) ?
248                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
249
250         /*   size_vmode -- that is the amount of memory needed for the
251          *                 used video mode, i.e. the minimum amount of
252          *                 memory we need. */
253         size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
254
255         /*   size_total -- all video memory we have. Used for mtrr
256          *                 entries, ressource allocation and bounds
257          *                 checking. */
258         size_total = screen_info.lfb_size * 65536;
259         if (vram_total)
260                 size_total = vram_total * 1024 * 1024;
261         if (size_total < size_vmode)
262                 size_total = size_vmode;
263
264         /*   size_remap -- the amount of video memory we are going to
265          *                 use for vesafb.  With modern cards it is no
266          *                 option to simply use size_total as that
267          *                 wastes plenty of kernel address space. */
268         size_remap  = size_vmode * 2;
269         if (vram_remap)
270                 size_remap = vram_remap * 1024 * 1024;
271         if (size_remap < size_vmode)
272                 size_remap = size_vmode;
273         if (size_remap > size_total)
274                 size_remap = size_total;
275         vesafb_fix.smem_len = size_remap;
276
277 #ifndef __i386__
278         screen_info.vesapm_seg = 0;
279 #endif
280
281         if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
282                 printk(KERN_WARNING
283                        "vesafb: cannot reserve video memory at 0x%lx\n",
284                         vesafb_fix.smem_start);
285                 /* We cannot make this fatal. Sometimes this comes from magic
286                    spaces our resource handlers simply don't know about */
287         }
288
289         info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
290         if (!info) {
291                 release_mem_region(vesafb_fix.smem_start, size_total);
292                 return -ENOMEM;
293         }
294         info->pseudo_palette = info->par;
295         info->par = NULL;
296
297         /* set vesafb aperture size for generic probing */
298         info->aperture_base = screen_info.lfb_base;
299         info->aperture_size = size_total;
300
301         info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
302         if (!info->screen_base) {
303                 printk(KERN_ERR
304                        "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
305                         vesafb_fix.smem_len, vesafb_fix.smem_start);
306                 err = -EIO;
307                 goto err;
308         }
309
310         printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
311                "using %dk, total %dk\n",
312                vesafb_fix.smem_start, info->screen_base,
313                size_remap/1024, size_total/1024);
314         printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
315                vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
316
317         if (screen_info.vesapm_seg) {
318                 printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
319                        screen_info.vesapm_seg,screen_info.vesapm_off);
320         }
321
322         if (screen_info.vesapm_seg < 0xc000)
323                 ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
324
325         if (ypan || pmi_setpal) {
326                 unsigned short *pmi_base;
327                 pmi_base  = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
328                 pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
329                 pmi_pal   = (void*)((char*)pmi_base + pmi_base[2]);
330                 printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
331                 if (pmi_base[3]) {
332                         printk(KERN_INFO "vesafb: pmi: ports = ");
333                                 for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
334                                         printk("%x ",pmi_base[i]);
335                         printk("\n");
336                         if (pmi_base[i] != 0xffff) {
337                                 /*
338                                  * memory areas not supported (yet?)
339                                  *
340                                  * Rules are: we have to set up a descriptor for the requested
341                                  * memory area and pass it in the ES register to the BIOS function.
342                                  */
343                                 printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
344                                 ypan = pmi_setpal = 0;
345                         }
346                 }
347         }
348
349         if (vesafb_defined.bits_per_pixel == 8 && !pmi_setpal && !vga_compat) {
350                 printk(KERN_WARNING "vesafb: hardware palette is unchangeable,\n"
351                                     "        colors may be incorrect\n");
352                 vesafb_fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
353         }
354
355         vesafb_defined.xres_virtual = vesafb_defined.xres;
356         vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
357         if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
358                 printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
359                        (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
360         } else {
361                 printk(KERN_INFO "vesafb: scrolling: redraw\n");
362                 vesafb_defined.yres_virtual = vesafb_defined.yres;
363                 ypan = 0;
364         }
365
366         /* some dummy values for timing to make fbset happy */
367         vesafb_defined.pixclock     = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
368         vesafb_defined.left_margin  = (vesafb_defined.xres / 8) & 0xf8;
369         vesafb_defined.hsync_len    = (vesafb_defined.xres / 8) & 0xf8;
370         
371         vesafb_defined.red.offset    = screen_info.red_pos;
372         vesafb_defined.red.length    = screen_info.red_size;
373         vesafb_defined.green.offset  = screen_info.green_pos;
374         vesafb_defined.green.length  = screen_info.green_size;
375         vesafb_defined.blue.offset   = screen_info.blue_pos;
376         vesafb_defined.blue.length   = screen_info.blue_size;
377         vesafb_defined.transp.offset = screen_info.rsvd_pos;
378         vesafb_defined.transp.length = screen_info.rsvd_size;
379
380         if (vesafb_defined.bits_per_pixel <= 8) {
381                 depth = vesafb_defined.green.length;
382                 vesafb_defined.red.length =
383                 vesafb_defined.green.length =
384                 vesafb_defined.blue.length =
385                 vesafb_defined.bits_per_pixel;
386         }
387
388         printk(KERN_INFO "vesafb: %s: "
389                "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
390                (vesafb_defined.bits_per_pixel > 8) ?
391                "Truecolor" : (vga_compat || pmi_setpal) ?
392                "Pseudocolor" : "Static Pseudocolor",
393                screen_info.rsvd_size,
394                screen_info.red_size,
395                screen_info.green_size,
396                screen_info.blue_size,
397                screen_info.rsvd_pos,
398                screen_info.red_pos,
399                screen_info.green_pos,
400                screen_info.blue_pos);
401
402         vesafb_fix.ypanstep  = ypan     ? 1 : 0;
403         vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
404
405         /* request failure does not faze us, as vgacon probably has this
406          * region already (FIXME) */
407         request_region(0x3c0, 32, "vesafb");
408
409 #ifdef CONFIG_MTRR
410         if (mtrr) {
411                 unsigned int temp_size = size_total;
412                 unsigned int type = 0;
413
414                 switch (mtrr) {
415                 case 1:
416                         type = MTRR_TYPE_UNCACHABLE;
417                         break;
418                 case 2:
419                         type = MTRR_TYPE_WRBACK;
420                         break;
421                 case 3:
422                         type = MTRR_TYPE_WRCOMB;
423                         break;
424                 case 4:
425                         type = MTRR_TYPE_WRTHROUGH;
426                         break;
427                 default:
428                         type = 0;
429                         break;
430                 }
431
432                 if (type) {
433                         int rc;
434
435                         /* Find the largest power-of-two */
436                         while (temp_size & (temp_size - 1))
437                                 temp_size &= (temp_size - 1);
438
439                         /* Try and find a power of two to add */
440                         do {
441                                 rc = mtrr_add(vesafb_fix.smem_start, temp_size,
442                                               type, 1);
443                                 temp_size >>= 1;
444                         } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
445                 }
446         }
447 #endif
448         
449         info->fbops = &vesafb_ops;
450         info->var = vesafb_defined;
451         info->fix = vesafb_fix;
452         info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE |
453                 (ypan ? FBINFO_HWACCEL_YPAN : 0);
454
455         if (!ypan)
456                 info->fbops->fb_pan_display = NULL;
457
458         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
459                 err = -ENOMEM;
460                 goto err;
461         }
462         if (register_framebuffer(info)<0) {
463                 err = -EINVAL;
464                 fb_dealloc_cmap(&info->cmap);
465                 goto err;
466         }
467         printk(KERN_INFO "fb%d: %s frame buffer device\n",
468                info->node, info->fix.id);
469         return 0;
470 err:
471         if (info->screen_base)
472                 iounmap(info->screen_base);
473         framebuffer_release(info);
474         release_mem_region(vesafb_fix.smem_start, size_total);
475         return err;
476 }
477
478 static struct platform_driver vesafb_driver = {
479         .probe  = vesafb_probe,
480         .driver = {
481                 .name   = "vesafb",
482         },
483 };
484
485 static struct platform_device *vesafb_device;
486
487 static int __init vesafb_init(void)
488 {
489         int ret;
490         char *option = NULL;
491
492         /* ignore error return of fb_get_options */
493         fb_get_options("vesafb", &option);
494         vesafb_setup(option);
495         ret = platform_driver_register(&vesafb_driver);
496
497         if (!ret) {
498                 vesafb_device = platform_device_alloc("vesafb", 0);
499
500                 if (vesafb_device)
501                         ret = platform_device_add(vesafb_device);
502                 else
503                         ret = -ENOMEM;
504
505                 if (ret) {
506                         platform_device_put(vesafb_device);
507                         platform_driver_unregister(&vesafb_driver);
508                 }
509         }
510
511         return ret;
512 }
513 module_init(vesafb_init);
514
515 MODULE_LICENSE("GPL");