xref: /freebsd/sys/dev/vt/hw/efifb/efifb.c (revision a1e1814d763ae0dd1c50a85bbd6884969ed26126)
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 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/fbio.h>
37 #include <sys/linker.h>
38 
39 #include "opt_platform.h"
40 
41 #include <machine/metadata.h>
42 #include <machine/vm.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_probe_t vt_efifb_probe;
53 
54 static struct vt_driver vt_efifb_driver = {
55 	.vd_name = "efifb",
56 	.vd_probe = vt_efifb_probe,
57 	.vd_init = vt_efifb_init,
58 	.vd_blank = vt_fb_blank,
59 	.vd_bitblt_text = vt_fb_bitblt_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 	/* Better than VGA, but still generic driver. */
66 	.vd_priority = VD_PRIORITY_GENERIC + 1,
67 };
68 
69 static struct fb_info local_info;
70 VT_DRIVER_DECLARE(vt_efifb, vt_efifb_driver);
71 
72 static int
73 vt_efifb_probe(struct vt_device *vd)
74 {
75 	int		disabled;
76 	struct efi_fb	*efifb;
77 	caddr_t		kmdp;
78 
79 	disabled = 0;
80 	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
81 	if (disabled != 0)
82 		return (CN_DEAD);
83 
84 	kmdp = preload_search_by_type("elf kernel");
85 	if (kmdp == NULL)
86 		kmdp = preload_search_by_type("elf64 kernel");
87 	efifb = (struct efi_fb *)preload_search_info(kmdp,
88 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
89 	if (efifb == NULL)
90 		return (CN_DEAD);
91 
92 	return (CN_INTERNAL);
93 }
94 
95 static int
96 vt_efifb_init(struct vt_device *vd)
97 {
98 	struct fb_info	*info;
99 	struct efi_fb	*efifb;
100 	caddr_t		kmdp;
101 
102 	info = vd->vd_softc;
103 	if (info == NULL)
104 		info = vd->vd_softc = (void *)&local_info;
105 
106 	kmdp = preload_search_by_type("elf kernel");
107 	if (kmdp == NULL)
108 		kmdp = preload_search_by_type("elf64 kernel");
109 	efifb = (struct efi_fb *)preload_search_info(kmdp,
110 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
111 	if (efifb == NULL)
112 		return (CN_DEAD);
113 
114 	info->fb_height = efifb->fb_height;
115 	info->fb_width = efifb->fb_width;
116 
117 	info->fb_depth = fls(efifb->fb_mask_red | efifb->fb_mask_green |
118 	    efifb->fb_mask_blue | efifb->fb_mask_reserved);
119 	/* Round to a multiple of the bits in a byte. */
120 	info->fb_bpp = (info->fb_depth + NBBY - 1) & ~(NBBY - 1);
121 
122 	/* Stride in bytes, not pixels */
123 	info->fb_stride = efifb->fb_stride * (info->fb_bpp / NBBY);
124 
125 	vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB,
126 	    efifb->fb_mask_red, ffs(efifb->fb_mask_red) - 1,
127 	    efifb->fb_mask_green, ffs(efifb->fb_mask_green) - 1,
128 	    efifb->fb_mask_blue, ffs(efifb->fb_mask_blue) - 1);
129 
130 	info->fb_size = info->fb_height * info->fb_stride;
131 	info->fb_pbase = efifb->fb_addr;
132 	info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
133 	    info->fb_size, VM_MEMATTR_WRITE_COMBINING);
134 
135 	vt_fb_init(vd);
136 
137 	return (CN_INTERNAL);
138 }
139