1 /* $NetBSD: genfb_machdep.c,v 1.23 2023/10/19 14:59:46 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Early attach support for raster consoles
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: genfb_machdep.c,v 1.23 2023/10/19 14:59:46 bouyer Exp $");
35
36 #include <sys/param.h>
37 #include <sys/conf.h>
38 #include <sys/device.h>
39 #include <sys/ioctl.h>
40 #include <sys/kernel.h>
41 #include <sys/kmem.h>
42 #include <sys/lwp.h>
43
44 #include <sys/bus.h>
45 #include <machine/bootinfo.h>
46
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wsdisplayvar.h>
49 #include <dev/rasops/rasops.h>
50 #include <dev/wsfont/wsfont.h>
51 #include <dev/wscons/wsdisplay_vconsvar.h>
52
53 #include <dev/wsfb/genfbvar.h>
54 #include <arch/x86/include/genfb_machdep.h>
55 #include <arch/xen/include/hypervisor.h>
56 #include <arch/xen/include/xen.h>
57
58 #include "wsdisplay.h"
59 #include "genfb.h"
60 #include "acpica.h"
61 #include "opt_xen.h"
62
63 #if NWSDISPLAY > 0 && NGENFB > 0
64 struct vcons_screen x86_genfb_console_screen;
65 bool x86_genfb_use_shadowfb = true;
66
67 static device_t x86_genfb_console_dev = NULL;
68
69 static struct wsscreen_descr x86_genfb_stdscreen = {
70 "std",
71 0, 0,
72 0,
73 0, 0,
74 0,
75 NULL
76 };
77
78 void
x86_genfb_set_console_dev(device_t dev)79 x86_genfb_set_console_dev(device_t dev)
80 {
81 KASSERT(x86_genfb_console_dev == NULL);
82 x86_genfb_console_dev = dev;
83 }
84
85 void
x86_genfb_ddb_trap_callback(int where)86 x86_genfb_ddb_trap_callback(int where)
87 {
88 if (x86_genfb_console_dev == NULL)
89 return;
90
91 if (where) {
92 genfb_enable_polling(x86_genfb_console_dev);
93 } else {
94 genfb_disable_polling(x86_genfb_console_dev);
95 }
96 }
97
98 int
x86_genfb_init(void)99 x86_genfb_init(void)
100 {
101 static int inited, attached;
102 struct rasops_info *ri = &x86_genfb_console_screen.scr_ri;
103 const struct btinfo_framebuffer *fbinfo = NULL;
104 bus_space_tag_t t = x86_bus_space_mem;
105 bus_space_handle_t h;
106 void *bits;
107 int err;
108
109 if (inited)
110 return attached;
111 inited = 1;
112
113 memset(&x86_genfb_console_screen, 0, sizeof(x86_genfb_console_screen));
114
115 #if defined(XEN) && defined(DOM0OPS)
116 if ((vm_guest == VM_GUEST_XENPVH || vm_guest == VM_GUEST_XENPV) &&
117 xendomain_is_dom0())
118 fbinfo = xen_genfb_getbtinfo();
119 #endif
120 if (fbinfo == NULL)
121 fbinfo = lookup_bootinfo(BTINFO_FRAMEBUFFER);
122
123 if (fbinfo == NULL || fbinfo->physaddr == 0)
124 return 0;
125
126 err = _x86_memio_map(t, (bus_addr_t)fbinfo->physaddr,
127 fbinfo->height * fbinfo->stride,
128 BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, &h);
129 if (err) {
130 aprint_error("x86_genfb_cnattach: couldn't map framebuffer\n");
131 return 0;
132 }
133
134 bits = bus_space_vaddr(t, h);
135 if (bits == NULL) {
136 aprint_error("x86_genfb_cnattach: couldn't get fb vaddr\n");
137 return 0;
138 }
139
140 #if NACPICA > 0 && !defined(XENPV)
141 acpi_md_vesa_modenum = fbinfo->vbemode;
142 #endif
143
144 wsfont_init();
145
146 ri->ri_width = fbinfo->width;
147 ri->ri_height = fbinfo->height;
148 ri->ri_depth = fbinfo->depth;
149 ri->ri_stride = fbinfo->stride;
150 ri->ri_rnum = fbinfo->rnum;
151 ri->ri_gnum = fbinfo->gnum;
152 ri->ri_bnum = fbinfo->bnum;
153 ri->ri_rpos = fbinfo->rpos;
154 ri->ri_gpos = fbinfo->gpos;
155 ri->ri_bpos = fbinfo->bpos;
156 if (x86_genfb_use_shadowfb && lookup_bootinfo(BTINFO_EFI) != NULL) {
157 /* XXX The allocated memory is never released... */
158 ri->ri_bits = kmem_alloc(ri->ri_stride * ri->ri_height,
159 KM_SLEEP);
160 ri->ri_hwbits = bits;
161 } else
162 ri->ri_bits = bits;
163 ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_CLEAR;
164 rasops_init(ri, ri->ri_height / 8, ri->ri_width / 8);
165 ri->ri_caps = WSSCREEN_WSCOLORS;
166 rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
167 ri->ri_width / ri->ri_font->fontwidth);
168
169 x86_genfb_stdscreen.nrows = ri->ri_rows;
170 x86_genfb_stdscreen.ncols = ri->ri_cols;
171 x86_genfb_stdscreen.textops = &ri->ri_ops;
172 x86_genfb_stdscreen.capabilities = ri->ri_caps;
173
174 attached = 1;
175 return 1;
176 }
177
178 int
x86_genfb_cnattach(void)179 x86_genfb_cnattach(void)
180 {
181 static int ncalls = 0;
182 struct rasops_info *ri = &x86_genfb_console_screen.scr_ri;
183 long defattr;
184
185 /* XXX jmcneill
186 * Defer console initialization until UVM is initialized
187 */
188 ++ncalls;
189 if (ncalls < 3)
190 return -1;
191
192 if (!x86_genfb_init())
193 return 0;
194
195 ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
196 wsdisplay_preattach(&x86_genfb_stdscreen, ri, 0, 0, defattr);
197
198 return 1;
199 }
200 #else /* NWSDISPLAY > 0 && NGENFB > 0 */
201 int
x86_genfb_init(void)202 x86_genfb_init(void)
203 {
204 return 0;
205 }
206
207 int
x86_genfb_cnattach(void)208 x86_genfb_cnattach(void)
209 {
210 return 0;
211 }
212 #endif
213