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