2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Aleksandr Rybalko under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/linker.h>
41 #include "opt_platform.h"
43 #include <machine/metadata.h>
44 #include <machine/vm.h>
45 #include <machine/vmparam.h>
48 #include <machine/pmap.h>
50 #include <dev/vt/vt.h>
51 #include <dev/vt/hw/fb/vt_fb.h>
52 #include <dev/vt/colors/vt_termcolors.h>
54 static vd_init_t vt_efifb_init;
55 static vd_probe_t vt_efifb_probe;
56 static void vt_efifb_remap(void *efifb_data);
58 static struct vt_driver vt_efifb_driver = {
60 .vd_probe = vt_efifb_probe,
61 .vd_init = vt_efifb_init,
62 .vd_blank = vt_fb_blank,
63 .vd_bitblt_text = vt_fb_bitblt_text,
64 .vd_bitblt_bmp = vt_fb_bitblt_bitmap,
65 .vd_drawrect = vt_fb_drawrect,
66 .vd_setpixel = vt_fb_setpixel,
67 .vd_fb_ioctl = vt_fb_ioctl,
68 .vd_fb_mmap = vt_fb_mmap,
69 /* Better than VGA, but still generic driver. */
70 .vd_priority = VD_PRIORITY_GENERIC + 1,
73 static struct fb_info local_info;
74 VT_DRIVER_DECLARE(vt_efifb, vt_efifb_driver);
76 SYSINIT(efifb_remap, SI_SUB_KMEM, SI_ORDER_ANY, vt_efifb_remap, &local_info);
79 vt_efifb_probe(struct vt_device *vd)
86 TUNABLE_INT_FETCH("hw.syscons.disable", &disabled);
90 kmdp = preload_search_by_type("elf kernel");
92 kmdp = preload_search_by_type("elf64 kernel");
93 efifb = (struct efi_fb *)preload_search_info(kmdp,
94 MODINFO_METADATA | MODINFOMD_EFI_FB);
102 vt_efifb_init(struct vt_device *vd)
105 struct fb_info *info;
106 struct efi_fb *efifb;
111 info = vd->vd_softc = (void *)&local_info;
113 kmdp = preload_search_by_type("elf kernel");
115 kmdp = preload_search_by_type("elf64 kernel");
116 efifb = (struct efi_fb *)preload_search_info(kmdp,
117 MODINFO_METADATA | MODINFOMD_EFI_FB);
121 info->fb_height = efifb->fb_height;
122 info->fb_width = efifb->fb_width;
124 depth = fls(efifb->fb_mask_red);
125 d = fls(efifb->fb_mask_green);
126 depth = d > depth ? d : depth;
127 d = fls(efifb->fb_mask_blue);
128 depth = d > depth ? d : depth;
129 d = fls(efifb->fb_mask_reserved);
130 depth = d > depth ? d : depth;
131 info->fb_depth = depth;
133 info->fb_stride = efifb->fb_stride * (depth / 8);
135 vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB,
136 efifb->fb_mask_red, ffs(efifb->fb_mask_red) - 1,
137 efifb->fb_mask_green, ffs(efifb->fb_mask_green) - 1,
138 efifb->fb_mask_blue, ffs(efifb->fb_mask_blue) - 1);
140 info->fb_size = info->fb_height * info->fb_stride;
141 info->fb_pbase = efifb->fb_addr;
143 * Use the direct map as a crutch until pmap is available. Once pmap
144 * is online, the framebuffer will be remapped by vt_efifb_remap()
145 * using pmap_mapdev_attr().
147 info->fb_vbase = PHYS_TO_DMAP(efifb->fb_addr);
149 /* Get pixel storage size. */
150 info->fb_bpp = info->fb_stride / info->fb_width * 8;
153 * Early FB driver work with static window buffer, so reduce to minimal
154 * size, buffer or screen.
156 info->fb_width = MIN(info->fb_width, VT_FB_DEFAULT_WIDTH);
157 info->fb_height = MIN(info->fb_height, VT_FB_DEFAULT_HEIGHT);
161 return (CN_INTERNAL);
165 vt_efifb_remap(void *xinfo)
167 struct fb_info *info = xinfo;
169 if (info->fb_pbase == 0)
173 * Remap as write-combining. This massively improves performance and
174 * happens very early in kernel initialization, when everything is
175 * still single-threaded and interrupts are off, so replacing the
176 * mapping address is safe.
178 info->fb_vbase = (intptr_t)pmap_mapdev_attr(info->fb_pbase,
179 info->fb_size, VM_MEMATTR_WRITE_COMBINING);