xref: /freebsd/sys/dev/vt/hw/efifb/efifb.c (revision 643ac419fafba89f5adda0e0ea75b538727453fb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/fbio.h>
38 #include <sys/linker.h>
39 
40 #include "opt_platform.h"
41 
42 #include <machine/metadata.h>
43 #include <machine/vmparam.h>
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 
47 #include <dev/vt/vt.h>
48 #include <dev/vt/hw/fb/vt_fb.h>
49 #include <dev/vt/colors/vt_termcolors.h>
50 
51 static vd_init_t vt_efifb_init;
52 static vd_fini_t vt_efifb_fini;
53 static vd_probe_t vt_efifb_probe;
54 
55 static struct vt_driver vt_efifb_driver = {
56 	.vd_name = "efifb",
57 	.vd_probe = vt_efifb_probe,
58 	.vd_init = vt_efifb_init,
59 	.vd_fini = vt_efifb_fini,
60 	.vd_blank = vt_fb_blank,
61 	.vd_bitblt_text = vt_fb_bitblt_text,
62 	.vd_invalidate_text = vt_fb_invalidate_text,
63 	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
64 	.vd_drawrect = vt_fb_drawrect,
65 	.vd_setpixel = vt_fb_setpixel,
66 	.vd_fb_ioctl = vt_fb_ioctl,
67 	.vd_fb_mmap = vt_fb_mmap,
68 	.vd_suspend = vt_suspend,
69 	.vd_resume = vt_resume,
70 	/* Better than VGA, but still generic driver. */
71 	.vd_priority = VD_PRIORITY_GENERIC + 1,
72 };
73 
74 static struct fb_info local_info;
75 VT_DRIVER_DECLARE(vt_efifb, vt_efifb_driver);
76 
77 static int
78 vt_efifb_probe(struct vt_device *vd)
79 {
80 	int		disabled;
81 	struct efi_fb	*efifb;
82 	caddr_t		kmdp;
83 
84 	disabled = 0;
85 	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
86 	if (disabled != 0)
87 		return (CN_DEAD);
88 
89 	kmdp = preload_search_by_type("elf kernel");
90 	if (kmdp == NULL)
91 		kmdp = preload_search_by_type("elf64 kernel");
92 	efifb = (struct efi_fb *)preload_search_info(kmdp,
93 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
94 	if (efifb == NULL)
95 		return (CN_DEAD);
96 
97 	return (CN_INTERNAL);
98 }
99 
100 static int
101 vt_efifb_init(struct vt_device *vd)
102 {
103 	struct fb_info	*info;
104 	struct efi_fb	*efifb;
105 	caddr_t		kmdp;
106 	int		roff, goff, boff;
107 
108 	info = vd->vd_softc;
109 	if (info == NULL)
110 		info = vd->vd_softc = (void *)&local_info;
111 
112 	kmdp = preload_search_by_type("elf kernel");
113 	if (kmdp == NULL)
114 		kmdp = preload_search_by_type("elf64 kernel");
115 	efifb = (struct efi_fb *)preload_search_info(kmdp,
116 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
117 	if (efifb == NULL)
118 		return (CN_DEAD);
119 
120 	info->fb_type = FBTYPE_EFIFB;
121 	info->fb_height = efifb->fb_height;
122 	info->fb_width = efifb->fb_width;
123 
124 	info->fb_depth = fls(efifb->fb_mask_red | efifb->fb_mask_green |
125 	    efifb->fb_mask_blue | efifb->fb_mask_reserved);
126 	/* Round to a multiple of the bits in a byte. */
127 	info->fb_bpp = roundup2(info->fb_depth, NBBY);
128 
129 	/* Stride in bytes, not pixels */
130 	info->fb_stride = efifb->fb_stride * (info->fb_bpp / NBBY);
131 
132 	roff = ffs(efifb->fb_mask_red) - 1;
133 	goff = ffs(efifb->fb_mask_green) - 1;
134 	boff = ffs(efifb->fb_mask_blue) - 1;
135 	vt_config_cons_colors(info, COLOR_FORMAT_RGB,
136 	    efifb->fb_mask_red >> roff, roff,
137 	    efifb->fb_mask_green >> goff, goff,
138 	    efifb->fb_mask_blue >> boff, boff);
139 	info->fb_cmsize = NCOLORS;
140 
141 	info->fb_size = info->fb_height * info->fb_stride;
142 	info->fb_pbase = efifb->fb_addr;
143 	info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
144 	    info->fb_size, VM_MEMATTR_WRITE_COMBINING);
145 
146 	vt_fb_init(vd);
147 
148 	return (CN_INTERNAL);
149 }
150 
151 static void
152 vt_efifb_fini(struct vt_device *vd, void *softc)
153 {
154 	struct fb_info	*info = softc;
155 
156 	vt_fb_fini(vd, softc);
157 	pmap_unmapdev(info->fb_vbase, info->fb_size);
158 }
159