xref: /freebsd/sys/dev/vt/hw/vbefb/vbefb.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2014 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Aleksandr Rybalko under sponsorship from the
8  * FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fbio.h>
39 #include <sys/linker.h>
40 
41 #include "opt_platform.h"
42 
43 #include <machine/metadata.h>
44 #include <machine/vmparam.h>
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 
48 #include <dev/vt/vt.h>
49 #include <dev/vt/hw/fb/vt_fb.h>
50 #include <dev/vt/colors/vt_termcolors.h>
51 
52 static vd_init_t vt_vbefb_init;
53 static vd_fini_t vt_vbefb_fini;
54 static vd_probe_t vt_vbefb_probe;
55 
56 static struct vt_driver vt_vbefb_driver = {
57 	.vd_name = "vbefb",
58 	.vd_probe = vt_vbefb_probe,
59 	.vd_init = vt_vbefb_init,
60 	.vd_fini = vt_vbefb_fini,
61 	.vd_blank = vt_fb_blank,
62 	.vd_bitblt_text = vt_fb_bitblt_text,
63 	.vd_invalidate_text = vt_fb_invalidate_text,
64 	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
65 	.vd_drawrect = vt_fb_drawrect,
66 	.vd_setpixel = vt_fb_setpixel,
67 	.vd_fb_ioctl = vt_fb_ioctl,
68 	.vd_fb_mmap = vt_fb_mmap,
69 	.vd_suspend = vt_suspend,
70 	.vd_resume = vt_resume,
71 	/* Better than VGA, but still generic driver. */
72 	.vd_priority = VD_PRIORITY_GENERIC + 1,
73 };
74 
75 static struct fb_info local_vbe_info;
76 VT_DRIVER_DECLARE(vt_vbefb, vt_vbefb_driver);
77 
78 static int
79 vt_vbefb_probe(struct vt_device *vd)
80 {
81 	int		disabled;
82 	struct vbe_fb	*vbefb;
83 	caddr_t		kmdp;
84 
85 	disabled = 0;
86 	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
87 	if (disabled != 0)
88 		return (CN_DEAD);
89 
90 	kmdp = preload_search_by_type("elf kernel");
91 	if (kmdp == NULL)
92 		kmdp = preload_search_by_type("elf64 kernel");
93 	vbefb = (struct vbe_fb *)preload_search_info(kmdp,
94 	    MODINFO_METADATA | MODINFOMD_VBE_FB);
95 	if (vbefb == NULL)
96 		return (CN_DEAD);
97 
98 	return (CN_INTERNAL);
99 }
100 
101 static int
102 vt_vbefb_init(struct vt_device *vd)
103 {
104 	struct fb_info	*info;
105 	struct vbe_fb	*vbefb;
106 	caddr_t		kmdp;
107 	int		format, roff, goff, boff;
108 
109 	info = vd->vd_softc;
110 	if (info == NULL)
111 		info = vd->vd_softc = (void *)&local_vbe_info;
112 
113 	kmdp = preload_search_by_type("elf kernel");
114 	if (kmdp == NULL)
115 		kmdp = preload_search_by_type("elf64 kernel");
116 	vbefb = (struct vbe_fb *)preload_search_info(kmdp,
117 	    MODINFO_METADATA | MODINFOMD_VBE_FB);
118 	if (vbefb == NULL)
119 		return (CN_DEAD);
120 
121 	info->fb_height = vbefb->fb_height;
122 	info->fb_width = vbefb->fb_width;
123 
124 	info->fb_depth = vbefb->fb_bpp;
125 	/* Round to a multiple of the bits in a byte. */
126 	info->fb_bpp = roundup2(vbefb->fb_bpp, NBBY);
127 
128 	/* Stride in bytes, not pixels */
129 	info->fb_stride = vbefb->fb_stride * (info->fb_bpp / NBBY);
130 
131 	if (info->fb_depth == 8)
132 		format = COLOR_FORMAT_VGA;
133 	else
134 		format = COLOR_FORMAT_RGB;
135 
136 	roff = ffs(vbefb->fb_mask_red) - 1;
137 	goff = ffs(vbefb->fb_mask_green) - 1;
138 	boff = ffs(vbefb->fb_mask_blue) - 1;
139 	vt_generate_cons_palette(info->fb_cmap, format,
140 	    vbefb->fb_mask_red >> roff, roff,
141 	    vbefb->fb_mask_green >> goff, goff,
142 	    vbefb->fb_mask_blue >> boff, boff);
143 
144 	/* Mark cmap initialized. */
145 	info->fb_cmsize = NCOLORS;
146 
147 	info->fb_size = info->fb_height * info->fb_stride;
148 	info->fb_pbase = vbefb->fb_addr;
149 	info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
150 	    info->fb_size, VM_MEMATTR_WRITE_COMBINING);
151 
152 	vt_fb_init(vd);
153 
154 	return (CN_INTERNAL);
155 }
156 
157 static void
158 vt_vbefb_fini(struct vt_device *vd, void *softc)
159 {
160 	struct fb_info	*info = softc;
161 
162 	vt_fb_fini(vd, softc);
163 	pmap_unmapdev(info->fb_vbase, info->fb_size);
164 }
165