1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2014 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * This software was developed by Aleksandr Rybalko under sponsorship from the
8 * FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
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/vmparam.h>
45#include <vm/vm.h>
46#include <vm/pmap.h>
47
48#include <dev/vt/vt.h>
49#include <dev/vt/hw/fb/vt_fb.h>
50#include <dev/vt/colors/vt_termcolors.h>
51
52static vd_init_t vt_efifb_init;
53static vd_probe_t vt_efifb_probe;
54
55static struct vt_driver vt_efifb_driver = {
56	.vd_name = "efifb",
57	.vd_probe = vt_efifb_probe,
58	.vd_init = vt_efifb_init,
59	.vd_blank = vt_fb_blank,
60	.vd_bitblt_text = vt_fb_bitblt_text,
61	.vd_invalidate_text = vt_fb_invalidate_text,
62	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
63	.vd_drawrect = vt_fb_drawrect,
64	.vd_setpixel = vt_fb_setpixel,
65	.vd_fb_ioctl = vt_fb_ioctl,
66	.vd_fb_mmap = vt_fb_mmap,
67	.vd_suspend = vt_suspend,
68	.vd_resume = vt_resume,
69	/* Better than VGA, but still generic driver. */
70	.vd_priority = VD_PRIORITY_GENERIC + 1,
71};
72
73static struct fb_info local_info;
74VT_DRIVER_DECLARE(vt_efifb, vt_efifb_driver);
75
76static int
77vt_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
99static int
100vt_efifb_init(struct vt_device *vd)
101{
102	struct fb_info	*info;
103	struct efi_fb	*efifb;
104	caddr_t		kmdp;
105
106	info = vd->vd_softc;
107	if (info == NULL)
108		info = vd->vd_softc = (void *)&local_info;
109
110	kmdp = preload_search_by_type("elf kernel");
111	if (kmdp == NULL)
112		kmdp = preload_search_by_type("elf64 kernel");
113	efifb = (struct efi_fb *)preload_search_info(kmdp,
114	    MODINFO_METADATA | MODINFOMD_EFI_FB);
115	if (efifb == NULL)
116		return (CN_DEAD);
117
118	info->fb_height = efifb->fb_height;
119	info->fb_width = efifb->fb_width;
120
121	info->fb_depth = fls(efifb->fb_mask_red | efifb->fb_mask_green |
122	    efifb->fb_mask_blue | efifb->fb_mask_reserved);
123	/* Round to a multiple of the bits in a byte. */
124	info->fb_bpp = roundup2(info->fb_depth, NBBY);
125
126	/* Stride in bytes, not pixels */
127	info->fb_stride = efifb->fb_stride * (info->fb_bpp / NBBY);
128
129	vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB,
130	    efifb->fb_mask_red, ffs(efifb->fb_mask_red) - 1,
131	    efifb->fb_mask_green, ffs(efifb->fb_mask_green) - 1,
132	    efifb->fb_mask_blue, ffs(efifb->fb_mask_blue) - 1);
133
134	info->fb_size = info->fb_height * info->fb_stride;
135	info->fb_pbase = efifb->fb_addr;
136	info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
137	    info->fb_size, VM_MEMATTR_WRITE_COMBINING);
138
139	vt_fb_init(vd);
140
141	return (CN_INTERNAL);
142}
143