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