1262785Sray/*-
2262785Sray * Copyright (c) 2014 The FreeBSD Foundation
3262785Sray * All rights reserved.
4262785Sray *
5262785Sray * This software was developed by Aleksandr Rybalko under sponsorship from the
6262785Sray * FreeBSD Foundation.
7262785Sray *
8262785Sray * Redistribution and use in source and binary forms, with or without
9262785Sray * modification, are permitted provided that the following conditions
10262785Sray * are met:
11262785Sray * 1. Redistributions of source code must retain the above copyright
12262785Sray *    notice, this list of conditions and the following disclaimer.
13262785Sray * 2. Redistributions in binary form must reproduce the above copyright
14262785Sray *    notice, this list of conditions and the following disclaimer in the
15262785Sray *    documentation and/or other materials provided with the distribution.
16262785Sray *
17262785Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18262785Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19262785Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20262785Sray * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21262785Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22262785Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23262785Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24262785Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25262785Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26262785Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27262785Sray * SUCH DAMAGE.
28262785Sray *
29262785Sray * $FreeBSD$
30262785Sray */
31262785Sray
32262785Sray#include <sys/cdefs.h>
33262785Sray__FBSDID("$FreeBSD$");
34262785Sray
35262785Sray#include <sys/param.h>
36262785Sray#include <sys/systm.h>
37262785Sray#include <sys/kernel.h>
38262785Sray#include <sys/fbio.h>
39262785Sray#include <sys/linker.h>
40262785Sray
41262785Sray#include "opt_platform.h"
42262785Sray
43262785Sray#include <machine/metadata.h>
44262785Sray#include <machine/vm.h>
45262785Sray#include <machine/vmparam.h>
46262785Sray#include <vm/vm.h>
47262785Sray#include <vm/pmap.h>
48262785Sray#include <machine/pmap.h>
49262785Sray
50262785Sray#include <dev/vt/vt.h>
51262785Sray#include <dev/vt/hw/fb/vt_fb.h>
52262785Sray#include <dev/vt/colors/vt_termcolors.h>
53262785Sray
54267538Sraystatic vd_init_t vt_efifb_init;
55267538Sraystatic vd_probe_t vt_efifb_probe;
56262785Sray
57267538Sraystatic struct vt_driver vt_efifb_driver = {
58267538Sray	.vd_name = "efifb",
59267538Sray	.vd_probe = vt_efifb_probe,
60267538Sray	.vd_init = vt_efifb_init,
61262785Sray	.vd_blank = vt_fb_blank,
62271128Semaste	.vd_bitblt_text = vt_fb_bitblt_text,
63271128Semaste	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
64271769Sdumbbell	.vd_drawrect = vt_fb_drawrect,
65271769Sdumbbell	.vd_setpixel = vt_fb_setpixel,
66270981Semaste	.vd_fb_ioctl = vt_fb_ioctl,
67270981Semaste	.vd_fb_mmap = vt_fb_mmap,
68262785Sray	/* Better than VGA, but still generic driver. */
69262785Sray	.vd_priority = VD_PRIORITY_GENERIC + 1,
70262785Sray};
71262785Sray
72267538Sraystatic struct fb_info local_info;
73267538SrayVT_DRIVER_DECLARE(vt_efifb, vt_efifb_driver);
74262785Sray
75262785Sraystatic int
76267538Srayvt_efifb_probe(struct vt_device *vd)
77262785Sray{
78267538Sray	int		disabled;
79267538Sray	struct efi_fb	*efifb;
80267538Sray	caddr_t		kmdp;
81267538Sray
82267538Sray	disabled = 0;
83267538Sray	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
84267538Sray	if (disabled != 0)
85267538Sray		return (CN_DEAD);
86267538Sray
87267538Sray	kmdp = preload_search_by_type("elf kernel");
88267538Sray	if (kmdp == NULL)
89267538Sray		kmdp = preload_search_by_type("elf64 kernel");
90267538Sray	efifb = (struct efi_fb *)preload_search_info(kmdp,
91267538Sray	    MODINFO_METADATA | MODINFOMD_EFI_FB);
92267538Sray	if (efifb == NULL)
93267538Sray		return (CN_DEAD);
94267538Sray
95267538Sray	return (CN_INTERNAL);
96267538Sray}
97267538Sray
98267538Sraystatic int
99267538Srayvt_efifb_init(struct vt_device *vd)
100267538Sray{
101262785Sray	struct fb_info	*info;
102262785Sray	struct efi_fb	*efifb;
103262785Sray	caddr_t		kmdp;
104262785Sray
105262785Sray	info = vd->vd_softc;
106267538Sray	if (info == NULL)
107267538Sray		info = vd->vd_softc = (void *)&local_info;
108262785Sray
109262785Sray	kmdp = preload_search_by_type("elf kernel");
110262785Sray	if (kmdp == NULL)
111262785Sray		kmdp = preload_search_by_type("elf64 kernel");
112270974Semaste	efifb = (struct efi_fb *)preload_search_info(kmdp,
113270974Semaste	    MODINFO_METADATA | MODINFOMD_EFI_FB);
114267538Sray	if (efifb == NULL)
115262785Sray		return (CN_DEAD);
116262785Sray
117262785Sray	info->fb_height = efifb->fb_height;
118262785Sray	info->fb_width = efifb->fb_width;
119262785Sray
120287128Smarcel	info->fb_depth = fls(efifb->fb_mask_red | efifb->fb_mask_green |
121287128Smarcel	    efifb->fb_mask_blue | efifb->fb_mask_reserved);
122287128Smarcel	/* Round to a multiple of the bits in a byte. */
123287128Smarcel	info->fb_bpp = (info->fb_depth + NBBY - 1) & ~(NBBY - 1);
124262785Sray
125287128Smarcel	/* Stride in bytes, not pixels */
126287128Smarcel	info->fb_stride = efifb->fb_stride * (info->fb_bpp / NBBY);
127262785Sray
128270262Sdumbbell	vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB,
129262785Sray	    efifb->fb_mask_red, ffs(efifb->fb_mask_red) - 1,
130262785Sray	    efifb->fb_mask_green, ffs(efifb->fb_mask_green) - 1,
131262785Sray	    efifb->fb_mask_blue, ffs(efifb->fb_mask_blue) - 1);
132262785Sray
133262785Sray	info->fb_size = info->fb_height * info->fb_stride;
134262785Sray	info->fb_pbase = efifb->fb_addr;
135287126Smarcel	info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
136287126Smarcel	    info->fb_size, VM_MEMATTR_WRITE_COMBINING);
137262785Sray
138262785Sray	vt_fb_init(vd);
139262785Sray
140262785Sray	return (CN_INTERNAL);
141262785Sray}
142