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