xref: /linux/drivers/video/fbdev/vesafb.c (revision 55ec81f7517fad09135f65552cea0a3ee84fff30)
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 fb_info *info;
247 	struct vesafb_par *par;
248 	int i, err;
249 	unsigned int size_vmode;
250 	unsigned int size_remap;
251 	unsigned int size_total;
252 	char *option = NULL;
253 
254 	/* ignore error return of fb_get_options */
255 	fb_get_options("vesafb", &option);
256 	vesafb_setup(option);
257 
258 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
259 		return -ENODEV;
260 
261 	vga_compat = (screen_info.capabilities & 2) ? 0 : 1;
262 	vesafb_fix.smem_start = screen_info.lfb_base;
263 	vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
264 	if (15 == vesafb_defined.bits_per_pixel)
265 		vesafb_defined.bits_per_pixel = 16;
266 	vesafb_defined.xres = screen_info.lfb_width;
267 	vesafb_defined.yres = screen_info.lfb_height;
268 	vesafb_fix.line_length = screen_info.lfb_linelength;
269 	vesafb_fix.visual   = (vesafb_defined.bits_per_pixel == 8) ?
270 		FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
271 
272 	/*   size_vmode -- that is the amount of memory needed for the
273 	 *                 used video mode, i.e. the minimum amount of
274 	 *                 memory we need. */
275 	size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
276 
277 	/*   size_total -- all video memory we have. Used for mtrr
278 	 *                 entries, resource allocation and bounds
279 	 *                 checking. */
280 	size_total = screen_info.lfb_size * 65536;
281 	if (vram_total)
282 		size_total = vram_total * 1024 * 1024;
283 	if (size_total < size_vmode)
284 		size_total = size_vmode;
285 
286 	/*   size_remap -- the amount of video memory we are going to
287 	 *                 use for vesafb.  With modern cards it is no
288 	 *                 option to simply use size_total as that
289 	 *                 wastes plenty of kernel address space. */
290 	size_remap  = size_vmode * 2;
291 	if (vram_remap)
292 		size_remap = vram_remap * 1024 * 1024;
293 	if (size_remap < size_vmode)
294 		size_remap = size_vmode;
295 	if (size_remap > size_total)
296 		size_remap = size_total;
297 	vesafb_fix.smem_len = size_remap;
298 
299 #ifndef __i386__
300 	screen_info.vesapm_seg = 0;
301 #endif
302 
303 	if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
304 		printk(KERN_WARNING
305 		       "vesafb: cannot reserve video memory at 0x%lx\n",
306 			vesafb_fix.smem_start);
307 		/* We cannot make this fatal. Sometimes this comes from magic
308 		   spaces our resource handlers simply don't know about */
309 	}
310 
311 	info = framebuffer_alloc(sizeof(struct vesafb_par), &dev->dev);
312 	if (!info) {
313 		release_mem_region(vesafb_fix.smem_start, size_total);
314 		return -ENOMEM;
315 	}
316 	platform_set_drvdata(dev, info);
317 	par = info->par;
318 	info->pseudo_palette = par->pseudo_palette;
319 
320 	par->base = screen_info.lfb_base;
321 	par->size = size_total;
322 
323 	printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
324 	       vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
325 
326 	if (screen_info.vesapm_seg) {
327 		printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
328 		       screen_info.vesapm_seg,screen_info.vesapm_off);
329 	}
330 
331 	if (screen_info.vesapm_seg < 0xc000)
332 		ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
333 
334 	if (ypan || pmi_setpal) {
335 		unsigned short *pmi_base;
336 		pmi_base  = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
337 		pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
338 		pmi_pal   = (void*)((char*)pmi_base + pmi_base[2]);
339 		printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
340 		if (pmi_base[3]) {
341 			printk(KERN_INFO "vesafb: pmi: ports = ");
342 			for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
343 				printk("%x ", pmi_base[i]);
344 			printk("\n");
345 			if (pmi_base[i] != 0xffff) {
346 				/*
347 				 * memory areas not supported (yet?)
348 				 *
349 				 * Rules are: we have to set up a descriptor for the requested
350 				 * memory area and pass it in the ES register to the BIOS function.
351 				 */
352 				printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
353 				ypan = pmi_setpal = 0;
354 			}
355 		}
356 	}
357 
358 	if (vesafb_defined.bits_per_pixel == 8 && !pmi_setpal && !vga_compat) {
359 		printk(KERN_WARNING "vesafb: hardware palette is unchangeable,\n"
360 		                    "        colors may be incorrect\n");
361 		vesafb_fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
362 	}
363 
364 	vesafb_defined.xres_virtual = vesafb_defined.xres;
365 	vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
366 	if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
367 		printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
368 		       (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
369 	} else {
370 		printk(KERN_INFO "vesafb: scrolling: redraw\n");
371 		vesafb_defined.yres_virtual = vesafb_defined.yres;
372 		ypan = 0;
373 	}
374 
375 	/* some dummy values for timing to make fbset happy */
376 	vesafb_defined.pixclock     = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
377 	vesafb_defined.left_margin  = (vesafb_defined.xres / 8) & 0xf8;
378 	vesafb_defined.hsync_len    = (vesafb_defined.xres / 8) & 0xf8;
379 
380 	vesafb_defined.red.offset    = screen_info.red_pos;
381 	vesafb_defined.red.length    = screen_info.red_size;
382 	vesafb_defined.green.offset  = screen_info.green_pos;
383 	vesafb_defined.green.length  = screen_info.green_size;
384 	vesafb_defined.blue.offset   = screen_info.blue_pos;
385 	vesafb_defined.blue.length   = screen_info.blue_size;
386 	vesafb_defined.transp.offset = screen_info.rsvd_pos;
387 	vesafb_defined.transp.length = screen_info.rsvd_size;
388 
389 	if (vesafb_defined.bits_per_pixel <= 8) {
390 		depth = vesafb_defined.green.length;
391 		vesafb_defined.red.length =
392 		vesafb_defined.green.length =
393 		vesafb_defined.blue.length =
394 		vesafb_defined.bits_per_pixel;
395 	}
396 
397 	printk(KERN_INFO "vesafb: %s: "
398 	       "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
399 	       (vesafb_defined.bits_per_pixel > 8) ?
400 	       "Truecolor" : (vga_compat || pmi_setpal) ?
401 	       "Pseudocolor" : "Static Pseudocolor",
402 	       screen_info.rsvd_size,
403 	       screen_info.red_size,
404 	       screen_info.green_size,
405 	       screen_info.blue_size,
406 	       screen_info.rsvd_pos,
407 	       screen_info.red_pos,
408 	       screen_info.green_pos,
409 	       screen_info.blue_pos);
410 
411 	vesafb_fix.ypanstep  = ypan     ? 1 : 0;
412 	vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
413 
414 	/* request failure does not faze us, as vgacon probably has this
415 	 * region already (FIXME) */
416 	par->region = request_region(0x3c0, 32, "vesafb");
417 
418 	if (mtrr == 3) {
419 		unsigned int temp_size = size_total;
420 
421 		/* Find the largest power-of-two */
422 		temp_size = roundup_pow_of_two(temp_size);
423 
424 		/* Try and find a power of two to add */
425 		do {
426 			par->wc_cookie =
427 				arch_phys_wc_add(vesafb_fix.smem_start,
428 						 temp_size);
429 			temp_size >>= 1;
430 		} while (temp_size >= PAGE_SIZE && par->wc_cookie < 0);
431 
432 		info->screen_base = ioremap_wc(vesafb_fix.smem_start, vesafb_fix.smem_len);
433 	} else {
434 		if (mtrr && mtrr != 3)
435 			WARN_ONCE(1, "Only MTRR_TYPE_WRCOMB (3) make sense\n");
436 		info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
437 	}
438 
439 	if (!info->screen_base) {
440 		printk(KERN_ERR
441 		       "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
442 			vesafb_fix.smem_len, vesafb_fix.smem_start);
443 		err = -EIO;
444 		goto err_release_region;
445 	}
446 
447 	printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
448 	       "using %dk, total %dk\n",
449 	       vesafb_fix.smem_start, info->screen_base,
450 	       size_remap/1024, size_total/1024);
451 
452 	if (!ypan)
453 		vesafb_ops.fb_pan_display = NULL;
454 
455 	info->fbops = &vesafb_ops;
456 	info->var = vesafb_defined;
457 	info->fix = vesafb_fix;
458 	info->flags = (ypan ? FBINFO_HWACCEL_YPAN : 0);
459 
460 	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
461 		err = -ENOMEM;
462 		goto err_release_region;
463 	}
464 	err = devm_aperture_acquire_for_platform_device(dev, par->base, par->size);
465 	if (err)
466 		goto err_fb_dealloc_cmap;
467 	if (register_framebuffer(info)<0) {
468 		err = -EINVAL;
469 		goto err_fb_dealloc_cmap;
470 	}
471 	fb_info(info, "%s frame buffer device\n", info->fix.id);
472 	return 0;
473 err_fb_dealloc_cmap:
474 	fb_dealloc_cmap(&info->cmap);
475 err_release_region:
476 	arch_phys_wc_del(par->wc_cookie);
477 	if (info->screen_base)
478 		iounmap(info->screen_base);
479 	if (par->region)
480 		release_region(0x3c0, 32);
481 	framebuffer_release(info);
482 	release_mem_region(vesafb_fix.smem_start, size_total);
483 	return err;
484 }
485 
486 static void vesafb_remove(struct platform_device *pdev)
487 {
488 	struct fb_info *info = platform_get_drvdata(pdev);
489 
490 	if (((struct vesafb_par *)(info->par))->region)
491 		release_region(0x3c0, 32);
492 
493 	/* vesafb_destroy takes care of info cleanup */
494 	unregister_framebuffer(info);
495 }
496 
497 static struct platform_driver vesafb_driver = {
498 	.driver = {
499 		.name = "vesa-framebuffer",
500 	},
501 	.probe = vesafb_probe,
502 	.remove_new = vesafb_remove,
503 };
504 
505 module_platform_driver(vesafb_driver);
506 MODULE_LICENSE("GPL");
507