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: releng/10.3/sys/dev/vt/hw/efifb/efifb.c 287128 2015-08-25 15:14:50Z marcel $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: releng/10.3/sys/dev/vt/hw/efifb/efifb.c 287128 2015-08-25 15:14:50Z marcel $");
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
54static vd_init_t vt_efifb_init;
55static vd_probe_t vt_efifb_probe;
56
57static struct vt_driver vt_efifb_driver = {
58	.vd_name = "efifb",
59	.vd_probe = vt_efifb_probe,
60	.vd_init = vt_efifb_init,
61	.vd_blank = vt_fb_blank,
62	.vd_bitblt_text = vt_fb_bitblt_text,
63	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
64	.vd_drawrect = vt_fb_drawrect,
65	.vd_setpixel = vt_fb_setpixel,
66	.vd_fb_ioctl = vt_fb_ioctl,
67	.vd_fb_mmap = vt_fb_mmap,
68	/* Better than VGA, but still generic driver. */
69	.vd_priority = VD_PRIORITY_GENERIC + 1,
70};
71
72static struct fb_info local_info;
73VT_DRIVER_DECLARE(vt_efifb, vt_efifb_driver);
74
75static int
76vt_efifb_probe(struct vt_device *vd)
77{
78	int		disabled;
79	struct efi_fb	*efifb;
80	caddr_t		kmdp;
81
82	disabled = 0;
83	TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
84	if (disabled != 0)
85		return (CN_DEAD);
86
87	kmdp = preload_search_by_type("elf kernel");
88	if (kmdp == NULL)
89		kmdp = preload_search_by_type("elf64 kernel");
90	efifb = (struct efi_fb *)preload_search_info(kmdp,
91	    MODINFO_METADATA | MODINFOMD_EFI_FB);
92	if (efifb == NULL)
93		return (CN_DEAD);
94
95	return (CN_INTERNAL);
96}
97
98static int
99vt_efifb_init(struct vt_device *vd)
100{
101	struct fb_info	*info;
102	struct efi_fb	*efifb;
103	caddr_t		kmdp;
104
105	info = vd->vd_softc;
106	if (info == NULL)
107		info = vd->vd_softc = (void *)&local_info;
108
109	kmdp = preload_search_by_type("elf kernel");
110	if (kmdp == NULL)
111		kmdp = preload_search_by_type("elf64 kernel");
112	efifb = (struct efi_fb *)preload_search_info(kmdp,
113	    MODINFO_METADATA | MODINFOMD_EFI_FB);
114	if (efifb == NULL)
115		return (CN_DEAD);
116
117	info->fb_height = efifb->fb_height;
118	info->fb_width = efifb->fb_width;
119
120	info->fb_depth = fls(efifb->fb_mask_red | efifb->fb_mask_green |
121	    efifb->fb_mask_blue | efifb->fb_mask_reserved);
122	/* Round to a multiple of the bits in a byte. */
123	info->fb_bpp = (info->fb_depth + NBBY - 1) & ~(NBBY - 1);
124
125	/* Stride in bytes, not pixels */
126	info->fb_stride = efifb->fb_stride * (info->fb_bpp / NBBY);
127
128	vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB,
129	    efifb->fb_mask_red, ffs(efifb->fb_mask_red) - 1,
130	    efifb->fb_mask_green, ffs(efifb->fb_mask_green) - 1,
131	    efifb->fb_mask_blue, ffs(efifb->fb_mask_blue) - 1);
132
133	info->fb_size = info->fb_height * info->fb_stride;
134	info->fb_pbase = efifb->fb_addr;
135	info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
136	    info->fb_size, VM_MEMATTR_WRITE_COMBINING);
137
138	vt_fb_init(vd);
139
140	return (CN_INTERNAL);
141}
142