xref: /freebsd/sys/dev/vt/hw/efifb/efifb.c (revision e2a08ac9ce424f543a2f03c67fb882fdabbdd32a)
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_efifb_init;
49 static vd_fini_t vt_efifb_fini;
50 static vd_probe_t vt_efifb_probe;
51 
52 static struct vt_driver vt_efifb_driver = {
53 	.vd_name = "efifb",
54 	.vd_probe = vt_efifb_probe,
55 	.vd_init = vt_efifb_init,
56 	.vd_fini = vt_efifb_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_bitblt_argb = vt_fb_bitblt_argb,
62 	.vd_drawrect = vt_fb_drawrect,
63 	.vd_setpixel = vt_fb_setpixel,
64 	.vd_fb_ioctl = vt_fb_ioctl,
65 	.vd_fb_mmap = vt_fb_mmap,
66 	.vd_suspend = vt_suspend,
67 	.vd_resume = vt_resume,
68 	/* Better than VGA, but still generic driver. */
69 	.vd_priority = VD_PRIORITY_GENERIC + 1,
70 };
71 
72 static struct fb_info local_info;
73 VT_DRIVER_DECLARE(vt_efifb, vt_efifb_driver);
74 
75 static int
vt_efifb_probe(struct vt_device * vd)76 vt_efifb_probe(struct vt_device *vd)
77 {
78 	int		disabled;
79 	struct efi_fb	*efifb;
80 
81 	disabled = 0;
82 	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
83 	if (disabled != 0)
84 		return (CN_DEAD);
85 
86 	efifb = (struct efi_fb *)preload_search_info(preload_kmdp,
87 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
88 	if (efifb == NULL)
89 		return (CN_DEAD);
90 
91 	return (CN_INTERNAL);
92 }
93 
94 static int
vt_efifb_init(struct vt_device * vd)95 vt_efifb_init(struct vt_device *vd)
96 {
97 	struct fb_info	*info;
98 	struct efi_fb	*efifb;
99 	vm_memattr_t	memattr;
100 	int		roff, goff, boff;
101 
102 #ifdef VM_MEMATTR_WRITE_COMBINING
103 	char attr[16];
104 
105 	/*
106 	 * XXX TODO: I think there's more nuance here than we're acknowledging,
107 	 * and we should look into it.  It may be that the framebuffer lives in
108 	 * a segment of memory that doesn't support one or both of these.  We
109 	 * should likely be consulting the memory map for any applicable
110 	 * cacheability attributes before making a final decision.
111 	 */
112 	memattr = VM_MEMATTR_WRITE_COMBINING;
113 	if (TUNABLE_STR_FETCH("hw.efifb.cache_attr", attr, sizeof(attr))) {
114 		/*
115 		 * We'll allow WC but it's currently the default, UC is the only
116 		 * other tested one at this time.
117 		 */
118 		if (strcasecmp(attr, "wc") != 0 &&
119 		    strcasecmp(attr, "uc") != 0) {
120 			printf("efifb: unsupported cache attr specified: %s\n",
121 			    attr);
122 			printf("efifb: expected \"wc\" or \"uc\"\n");
123 		} else if (strcasecmp(attr, "uc") == 0) {
124 			memattr = VM_MEMATTR_UNCACHEABLE;
125 		}
126 	}
127 #else
128 	memattr = VM_MEMATTR_UNCACHEABLE;
129 #endif
130 
131 	info = vd->vd_softc;
132 	if (info == NULL)
133 		info = vd->vd_softc = (void *)&local_info;
134 
135 	efifb = (struct efi_fb *)preload_search_info(preload_kmdp,
136 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
137 	if (efifb == NULL)
138 		return (CN_DEAD);
139 
140 	info->fb_type = FBTYPE_EFIFB;
141 	info->fb_height = efifb->fb_height;
142 	info->fb_width = efifb->fb_width;
143 
144 	info->fb_depth = fls(efifb->fb_mask_red | efifb->fb_mask_green |
145 	    efifb->fb_mask_blue | efifb->fb_mask_reserved);
146 	/* Round to a multiple of the bits in a byte. */
147 	info->fb_bpp = roundup2(info->fb_depth, NBBY);
148 
149 	/* Stride in bytes, not pixels */
150 	info->fb_stride = efifb->fb_stride * (info->fb_bpp / NBBY);
151 
152 	roff = ffs(efifb->fb_mask_red) - 1;
153 	goff = ffs(efifb->fb_mask_green) - 1;
154 	boff = ffs(efifb->fb_mask_blue) - 1;
155 	vt_config_cons_colors(info, COLOR_FORMAT_RGB,
156 	    efifb->fb_mask_red >> roff, roff,
157 	    efifb->fb_mask_green >> goff, goff,
158 	    efifb->fb_mask_blue >> boff, boff);
159 	info->fb_cmsize = NCOLORS;
160 
161 	info->fb_size = info->fb_height * info->fb_stride;
162 	info->fb_pbase = efifb->fb_addr;
163 	info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
164 	    info->fb_size, memattr);
165 
166 	vt_fb_init(vd);
167 
168 	return (CN_INTERNAL);
169 }
170 
171 static void
vt_efifb_fini(struct vt_device * vd,void * softc)172 vt_efifb_fini(struct vt_device *vd, void *softc)
173 {
174 	struct fb_info	*info = softc;
175 
176 	vt_fb_fini(vd, softc);
177 	pmap_unmapdev((void *)info->fb_vbase, info->fb_size);
178 }
179