xref: /freebsd/sys/dev/vt/hw/efifb/efifb.c (revision 95d45410b5100e07f6f98450bcd841a8945d4726)
1 /*-
2  * Copyright (c) 2014 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Aleksandr Rybalko under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
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/vm.h>
45 #include <machine/vmparam.h>
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <machine/pmap.h>
49 
50 #include <dev/vt/vt.h>
51 #include <dev/vt/hw/fb/vt_fb.h>
52 #include <dev/vt/colors/vt_termcolors.h>
53 
54 static vd_init_t vt_efifb_init;
55 static vd_probe_t vt_efifb_probe;
56 static void vt_efifb_remap(void *efifb_data);
57 
58 static struct vt_driver vt_efifb_driver = {
59 	.vd_name = "efifb",
60 	.vd_probe = vt_efifb_probe,
61 	.vd_init = vt_efifb_init,
62 	.vd_blank = vt_fb_blank,
63 	.vd_bitbltchr = vt_fb_bitbltchr,
64 	.vd_maskbitbltchr = vt_fb_maskbitbltchr,
65 	.vd_fb_ioctl = vt_fb_ioctl,
66 	.vd_fb_mmap = vt_fb_mmap,
67 	/* Better than VGA, but still generic driver. */
68 	.vd_priority = VD_PRIORITY_GENERIC + 1,
69 };
70 
71 static struct fb_info local_info;
72 VT_DRIVER_DECLARE(vt_efifb, vt_efifb_driver);
73 
74 SYSINIT(efifb_remap, SI_SUB_KMEM, SI_ORDER_ANY, vt_efifb_remap, &local_info);
75 
76 static int
77 vt_efifb_probe(struct vt_device *vd)
78 {
79 	int		disabled;
80 	struct efi_fb	*efifb;
81 	caddr_t		kmdp;
82 
83 	disabled = 0;
84 	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
85 	if (disabled != 0)
86 		return (CN_DEAD);
87 
88 	kmdp = preload_search_by_type("elf kernel");
89 	if (kmdp == NULL)
90 		kmdp = preload_search_by_type("elf64 kernel");
91 	efifb = (struct efi_fb *)preload_search_info(kmdp,
92 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
93 	if (efifb == NULL)
94 		return (CN_DEAD);
95 
96 	return (CN_INTERNAL);
97 }
98 
99 static int
100 vt_efifb_init(struct vt_device *vd)
101 {
102 	int		depth, d;
103 	struct fb_info	*info;
104 	struct efi_fb	*efifb;
105 	caddr_t		kmdp;
106 
107 	info = vd->vd_softc;
108 	if (info == NULL)
109 		info = vd->vd_softc = (void *)&local_info;
110 
111 	kmdp = preload_search_by_type("elf kernel");
112 	if (kmdp == NULL)
113 		kmdp = preload_search_by_type("elf64 kernel");
114 	efifb = (struct efi_fb *)preload_search_info(kmdp,
115 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
116 	if (efifb == NULL)
117 		return (CN_DEAD);
118 
119 	info->fb_height = efifb->fb_height;
120 	info->fb_width = efifb->fb_width;
121 
122 	depth = fls(efifb->fb_mask_red);
123 	d = fls(efifb->fb_mask_green);
124 	depth = d > depth ? d : depth;
125 	d = fls(efifb->fb_mask_blue);
126 	depth = d > depth ? d : depth;
127 	d = fls(efifb->fb_mask_reserved);
128 	depth = d > depth ? d : depth;
129 	info->fb_depth = depth;
130 
131 	info->fb_stride = efifb->fb_stride * (depth / 8);
132 
133 	vt_generate_vga_palette(info->fb_cmap, COLOR_FORMAT_RGB,
134 	    efifb->fb_mask_red, ffs(efifb->fb_mask_red) - 1,
135 	    efifb->fb_mask_green, ffs(efifb->fb_mask_green) - 1,
136 	    efifb->fb_mask_blue, ffs(efifb->fb_mask_blue) - 1);
137 
138 	info->fb_size = info->fb_height * info->fb_stride;
139 	info->fb_pbase = efifb->fb_addr;
140 	/*
141 	 * Use the direct map as a crutch until pmap is available. Once pmap
142 	 * is online, the framebuffer will be remapped by vt_efifb_remap()
143 	 * using pmap_mapdev_attr().
144 	 */
145 	info->fb_vbase = PHYS_TO_DMAP(efifb->fb_addr);
146 
147 	/* Get pixel storage size. */
148 	info->fb_bpp = info->fb_stride / info->fb_width * 8;
149 
150 	/*
151 	 * Early FB driver work with static window buffer, so reduce to minimal
152 	 * size, buffer or screen.
153 	 */
154 	info->fb_width = MIN(info->fb_width, VT_FB_DEFAULT_WIDTH);
155 	info->fb_height = MIN(info->fb_height, VT_FB_DEFAULT_HEIGHT);
156 
157 	fb_probe(info);
158 	vt_fb_init(vd);
159 
160 	/* Clear the screen. */
161 	vt_fb_blank(vd, TC_BLACK);
162 
163 	return (CN_INTERNAL);
164 }
165 
166 static void
167 vt_efifb_remap(void *xinfo)
168 {
169 	struct fb_info *info = xinfo;
170 
171 	if (info->fb_pbase == 0)
172 		return;
173 
174 	/*
175 	 * Remap as write-combining. This massively improves performance and
176 	 * happens very early in kernel initialization, when everything is
177 	 * still single-threaded and interrupts are off, so replacing the
178 	 * mapping address is safe.
179 	 */
180 	info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
181 	    info->fb_size, VM_MEMATTR_WRITE_COMBINING);
182 }
183 
184