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