genfb_vmbus.c revision 1.1.4.3 1 1.1.4.3 martin /* $NetBSD: genfb_vmbus.c,v 1.1.4.3 2019/06/12 11:35:40 martin Exp $ */
2 1.1.4.2 martin
3 1.1.4.2 martin /*-
4 1.1.4.2 martin * Copyright (c) 2007 Michael Lorenz
5 1.1.4.2 martin * All rights reserved.
6 1.1.4.2 martin *
7 1.1.4.2 martin * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 martin * modification, are permitted provided that the following conditions
9 1.1.4.2 martin * are met:
10 1.1.4.2 martin * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 martin * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 martin * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 martin * documentation and/or other materials provided with the distribution.
15 1.1.4.2 martin *
16 1.1.4.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.4.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.4.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.4.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.4.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.4.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.4.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.4.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.4.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.4.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.4.2 martin * POSSIBILITY OF SUCH DAMAGE.
27 1.1.4.2 martin */
28 1.1.4.2 martin
29 1.1.4.2 martin #include <sys/cdefs.h>
30 1.1.4.3 martin __KERNEL_RCSID(0, "$NetBSD: genfb_vmbus.c,v 1.1.4.3 2019/06/12 11:35:40 martin Exp $");
31 1.1.4.3 martin
32 1.1.4.3 martin #include "opt_wsfb.h"
33 1.1.4.3 martin #include "opt_genfb.h"
34 1.1.4.3 martin
35 1.1.4.3 martin #include <sys/param.h>
36 1.1.4.3 martin #include <sys/systm.h>
37 1.1.4.3 martin #include <sys/kernel.h>
38 1.1.4.3 martin #include <sys/device.h>
39 1.1.4.3 martin #include <sys/proc.h>
40 1.1.4.3 martin #include <sys/mutex.h>
41 1.1.4.3 martin #include <sys/ioctl.h>
42 1.1.4.3 martin #include <sys/systm.h>
43 1.1.4.3 martin #include <sys/kauth.h>
44 1.1.4.3 martin
45 1.1.4.3 martin #include <dev/wsfb/genfbvar.h>
46 1.1.4.3 martin
47 1.1.4.3 martin #include <dev/hyperv/vmbusvar.h>
48 1.1.4.3 martin #include <dev/hyperv/genfb_vmbusvar.h>
49 1.1.4.3 martin
50 1.1.4.3 martin static int genfb_vmbus_match(device_t, cfdata_t, void *);
51 1.1.4.3 martin static void genfb_vmbus_attach(device_t, device_t, void *);
52 1.1.4.3 martin static int genfb_vmbus_ioctl(void *, void *, u_long, void *, int,
53 1.1.4.3 martin struct lwp *);
54 1.1.4.3 martin static paddr_t genfb_vmbus_mmap(void *, void *, off_t, int);
55 1.1.4.3 martin static int genfb_vmbus_drm_print(void *, const char *);
56 1.1.4.3 martin static bool genfb_vmbus_shutdown(device_t, int);
57 1.1.4.3 martin
58 1.1.4.3 martin CFATTACH_DECL_NEW(genfb_vmbus, sizeof(struct genfb_vmbus_softc),
59 1.1.4.3 martin genfb_vmbus_match, genfb_vmbus_attach, NULL, NULL);
60 1.1.4.3 martin
61 1.1.4.3 martin static int
62 1.1.4.3 martin genfb_vmbus_match(device_t parent, cfdata_t match, void *aux)
63 1.1.4.3 martin {
64 1.1.4.3 martin struct vmbus_attach_args *aa = aux;
65 1.1.4.3 martin
66 1.1.4.3 martin if (memcmp(aa->aa_type, &hyperv_guid_video, sizeof(*aa->aa_type)) != 0)
67 1.1.4.3 martin return 0;
68 1.1.4.3 martin
69 1.1.4.3 martin if (!genfb_is_enabled())
70 1.1.4.3 martin return 0; /* explicitly disabled by MD code */
71 1.1.4.3 martin
72 1.1.4.3 martin /* Use genfb(4) at pci in Gen.1 VM. */
73 1.1.4.3 martin if (hyperv_is_gen1())
74 1.1.4.3 martin return 0;
75 1.1.4.3 martin
76 1.1.4.3 martin return 1;
77 1.1.4.3 martin }
78 1.1.4.3 martin
79 1.1.4.3 martin static void
80 1.1.4.3 martin genfb_vmbus_attach(device_t parent, device_t self, void *aux)
81 1.1.4.3 martin {
82 1.1.4.3 martin static const struct genfb_ops zero_ops;
83 1.1.4.3 martin struct genfb_vmbus_softc *sc = device_private(self);
84 1.1.4.3 martin struct vmbus_attach_args *aa = aux;
85 1.1.4.3 martin struct genfb_ops ops = zero_ops;
86 1.1.4.3 martin
87 1.1.4.3 martin aprint_naive("\n");
88 1.1.4.3 martin aprint_normal(": Hyper-V Synthetic Video\n");
89 1.1.4.3 martin
90 1.1.4.3 martin sc->sc_gen.sc_dev = self;
91 1.1.4.3 martin sc->sc_memt = aa->aa_memt;
92 1.1.4.3 martin
93 1.1.4.3 martin genfb_init(&sc->sc_gen);
94 1.1.4.3 martin
95 1.1.4.3 martin /* firmware / MD code responsible for restoring the display */
96 1.1.4.3 martin if (sc->sc_gen.sc_pmfcb == NULL)
97 1.1.4.3 martin pmf_device_register1(self, NULL, NULL,
98 1.1.4.3 martin genfb_vmbus_shutdown);
99 1.1.4.3 martin else
100 1.1.4.3 martin pmf_device_register1(self,
101 1.1.4.3 martin sc->sc_gen.sc_pmfcb->gpc_suspend,
102 1.1.4.3 martin sc->sc_gen.sc_pmfcb->gpc_resume,
103 1.1.4.3 martin genfb_vmbus_shutdown);
104 1.1.4.3 martin
105 1.1.4.3 martin if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
106 1.1.4.3 martin aprint_debug_dev(self, "not configured by firmware\n");
107 1.1.4.3 martin return;
108 1.1.4.3 martin }
109 1.1.4.3 martin
110 1.1.4.3 martin ops.genfb_ioctl = genfb_vmbus_ioctl;
111 1.1.4.3 martin ops.genfb_mmap = genfb_vmbus_mmap;
112 1.1.4.3 martin
113 1.1.4.3 martin if (genfb_attach(&sc->sc_gen, &ops) != 0)
114 1.1.4.3 martin return;
115 1.1.4.3 martin
116 1.1.4.3 martin /* now try to attach a DRM */
117 1.1.4.3 martin config_found_ia(self, "drm", aux, genfb_vmbus_drm_print);
118 1.1.4.3 martin }
119 1.1.4.3 martin
120 1.1.4.3 martin static int
121 1.1.4.3 martin genfb_vmbus_drm_print(void *aux, const char *pnp)
122 1.1.4.3 martin {
123 1.1.4.3 martin
124 1.1.4.3 martin if (pnp)
125 1.1.4.3 martin aprint_normal("drm at %s", pnp);
126 1.1.4.3 martin return UNCONF;
127 1.1.4.3 martin }
128 1.1.4.3 martin
129 1.1.4.3 martin static int
130 1.1.4.3 martin genfb_vmbus_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
131 1.1.4.3 martin struct lwp *l)
132 1.1.4.3 martin {
133 1.1.4.3 martin
134 1.1.4.3 martin switch (cmd) {
135 1.1.4.3 martin case WSDISPLAYIO_GTYPE:
136 1.1.4.3 martin *(u_int *)data = WSDISPLAY_TYPE_GENFB;
137 1.1.4.3 martin return 0;
138 1.1.4.3 martin }
139 1.1.4.3 martin
140 1.1.4.3 martin return EPASSTHROUGH;
141 1.1.4.3 martin }
142 1.1.4.3 martin
143 1.1.4.3 martin static paddr_t
144 1.1.4.3 martin genfb_vmbus_mmap(void *v, void *vs, off_t offset, int prot)
145 1.1.4.3 martin {
146 1.1.4.3 martin struct genfb_vmbus_softc *sc = v;
147 1.1.4.3 martin
148 1.1.4.3 martin return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset, offset, prot,
149 1.1.4.3 martin BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
150 1.1.4.3 martin }
151 1.1.4.3 martin
152 1.1.4.3 martin static bool
153 1.1.4.3 martin genfb_vmbus_shutdown(device_t self, int flags)
154 1.1.4.3 martin {
155 1.1.4.3 martin
156 1.1.4.3 martin genfb_enable_polling(self);
157 1.1.4.3 martin return true;
158 1.1.4.3 martin }
159 1.1.4.3 martin /* $NetBSD: genfb_vmbus.c,v 1.1.4.3 2019/06/12 11:35:40 martin Exp $ */
160 1.1.4.3 martin
161 1.1.4.3 martin /*-
162 1.1.4.3 martin * Copyright (c) 2007 Michael Lorenz
163 1.1.4.3 martin * All rights reserved.
164 1.1.4.3 martin *
165 1.1.4.3 martin * Redistribution and use in source and binary forms, with or without
166 1.1.4.3 martin * modification, are permitted provided that the following conditions
167 1.1.4.3 martin * are met:
168 1.1.4.3 martin * 1. Redistributions of source code must retain the above copyright
169 1.1.4.3 martin * notice, this list of conditions and the following disclaimer.
170 1.1.4.3 martin * 2. Redistributions in binary form must reproduce the above copyright
171 1.1.4.3 martin * notice, this list of conditions and the following disclaimer in the
172 1.1.4.3 martin * documentation and/or other materials provided with the distribution.
173 1.1.4.3 martin *
174 1.1.4.3 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
175 1.1.4.3 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
176 1.1.4.3 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
177 1.1.4.3 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
178 1.1.4.3 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
179 1.1.4.3 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
180 1.1.4.3 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
181 1.1.4.3 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
182 1.1.4.3 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
183 1.1.4.3 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
184 1.1.4.3 martin * POSSIBILITY OF SUCH DAMAGE.
185 1.1.4.3 martin */
186 1.1.4.3 martin
187 1.1.4.3 martin #include <sys/cdefs.h>
188 1.1.4.3 martin __KERNEL_RCSID(0, "$NetBSD: genfb_vmbus.c,v 1.1.4.3 2019/06/12 11:35:40 martin Exp $");
189 1.1.4.2 martin
190 1.1.4.2 martin #include "opt_wsfb.h"
191 1.1.4.2 martin #include "opt_genfb.h"
192 1.1.4.2 martin
193 1.1.4.2 martin #include <sys/param.h>
194 1.1.4.2 martin #include <sys/systm.h>
195 1.1.4.2 martin #include <sys/kernel.h>
196 1.1.4.2 martin #include <sys/device.h>
197 1.1.4.2 martin #include <sys/proc.h>
198 1.1.4.2 martin #include <sys/mutex.h>
199 1.1.4.2 martin #include <sys/ioctl.h>
200 1.1.4.2 martin #include <sys/systm.h>
201 1.1.4.2 martin #include <sys/kauth.h>
202 1.1.4.2 martin
203 1.1.4.2 martin #include <dev/wsfb/genfbvar.h>
204 1.1.4.2 martin
205 1.1.4.2 martin #include <dev/hyperv/vmbusvar.h>
206 1.1.4.2 martin #include <dev/hyperv/genfb_vmbusvar.h>
207 1.1.4.2 martin
208 1.1.4.2 martin static int genfb_vmbus_match(device_t, cfdata_t, void *);
209 1.1.4.2 martin static void genfb_vmbus_attach(device_t, device_t, void *);
210 1.1.4.2 martin static int genfb_vmbus_ioctl(void *, void *, u_long, void *, int,
211 1.1.4.2 martin struct lwp *);
212 1.1.4.2 martin static paddr_t genfb_vmbus_mmap(void *, void *, off_t, int);
213 1.1.4.2 martin static int genfb_vmbus_drm_print(void *, const char *);
214 1.1.4.2 martin static bool genfb_vmbus_shutdown(device_t, int);
215 1.1.4.2 martin
216 1.1.4.2 martin CFATTACH_DECL_NEW(genfb_vmbus, sizeof(struct genfb_vmbus_softc),
217 1.1.4.2 martin genfb_vmbus_match, genfb_vmbus_attach, NULL, NULL);
218 1.1.4.2 martin
219 1.1.4.2 martin static int
220 1.1.4.2 martin genfb_vmbus_match(device_t parent, cfdata_t match, void *aux)
221 1.1.4.2 martin {
222 1.1.4.2 martin struct vmbus_attach_args *aa = aux;
223 1.1.4.2 martin
224 1.1.4.2 martin if (memcmp(aa->aa_type, &hyperv_guid_video, sizeof(*aa->aa_type)) != 0)
225 1.1.4.2 martin return 0;
226 1.1.4.2 martin
227 1.1.4.2 martin if (!genfb_is_enabled())
228 1.1.4.2 martin return 0; /* explicitly disabled by MD code */
229 1.1.4.2 martin
230 1.1.4.2 martin /* Use genfb(4) at pci in Gen.1 VM. */
231 1.1.4.2 martin if (hyperv_is_gen1())
232 1.1.4.2 martin return 0;
233 1.1.4.2 martin
234 1.1.4.2 martin return 1;
235 1.1.4.2 martin }
236 1.1.4.2 martin
237 1.1.4.2 martin static void
238 1.1.4.2 martin genfb_vmbus_attach(device_t parent, device_t self, void *aux)
239 1.1.4.2 martin {
240 1.1.4.2 martin static const struct genfb_ops zero_ops;
241 1.1.4.2 martin struct genfb_vmbus_softc *sc = device_private(self);
242 1.1.4.2 martin struct vmbus_attach_args *aa = aux;
243 1.1.4.2 martin struct genfb_ops ops = zero_ops;
244 1.1.4.2 martin
245 1.1.4.2 martin aprint_naive("\n");
246 1.1.4.2 martin aprint_normal(": Hyper-V Synthetic Video\n");
247 1.1.4.2 martin
248 1.1.4.2 martin sc->sc_gen.sc_dev = self;
249 1.1.4.2 martin sc->sc_memt = aa->aa_memt;
250 1.1.4.2 martin
251 1.1.4.2 martin genfb_init(&sc->sc_gen);
252 1.1.4.2 martin
253 1.1.4.2 martin /* firmware / MD code responsible for restoring the display */
254 1.1.4.2 martin if (sc->sc_gen.sc_pmfcb == NULL)
255 1.1.4.2 martin pmf_device_register1(self, NULL, NULL,
256 1.1.4.2 martin genfb_vmbus_shutdown);
257 1.1.4.2 martin else
258 1.1.4.2 martin pmf_device_register1(self,
259 1.1.4.2 martin sc->sc_gen.sc_pmfcb->gpc_suspend,
260 1.1.4.2 martin sc->sc_gen.sc_pmfcb->gpc_resume,
261 1.1.4.2 martin genfb_vmbus_shutdown);
262 1.1.4.2 martin
263 1.1.4.2 martin if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
264 1.1.4.2 martin aprint_debug_dev(self, "not configured by firmware\n");
265 1.1.4.2 martin return;
266 1.1.4.2 martin }
267 1.1.4.2 martin
268 1.1.4.2 martin ops.genfb_ioctl = genfb_vmbus_ioctl;
269 1.1.4.2 martin ops.genfb_mmap = genfb_vmbus_mmap;
270 1.1.4.2 martin
271 1.1.4.2 martin if (genfb_attach(&sc->sc_gen, &ops) != 0)
272 1.1.4.2 martin return;
273 1.1.4.2 martin
274 1.1.4.2 martin /* now try to attach a DRM */
275 1.1.4.2 martin config_found_ia(self, "drm", aux, genfb_vmbus_drm_print);
276 1.1.4.2 martin }
277 1.1.4.2 martin
278 1.1.4.2 martin static int
279 1.1.4.2 martin genfb_vmbus_drm_print(void *aux, const char *pnp)
280 1.1.4.2 martin {
281 1.1.4.2 martin
282 1.1.4.2 martin if (pnp)
283 1.1.4.2 martin aprint_normal("drm at %s", pnp);
284 1.1.4.2 martin return UNCONF;
285 1.1.4.2 martin }
286 1.1.4.2 martin
287 1.1.4.2 martin static int
288 1.1.4.2 martin genfb_vmbus_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
289 1.1.4.2 martin struct lwp *l)
290 1.1.4.2 martin {
291 1.1.4.2 martin
292 1.1.4.2 martin switch (cmd) {
293 1.1.4.2 martin case WSDISPLAYIO_GTYPE:
294 1.1.4.2 martin *(u_int *)data = WSDISPLAY_TYPE_GENFB;
295 1.1.4.2 martin return 0;
296 1.1.4.2 martin }
297 1.1.4.2 martin
298 1.1.4.2 martin return EPASSTHROUGH;
299 1.1.4.2 martin }
300 1.1.4.2 martin
301 1.1.4.2 martin static paddr_t
302 1.1.4.2 martin genfb_vmbus_mmap(void *v, void *vs, off_t offset, int prot)
303 1.1.4.2 martin {
304 1.1.4.2 martin struct genfb_vmbus_softc *sc = v;
305 1.1.4.2 martin
306 1.1.4.2 martin return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset, offset, prot,
307 1.1.4.2 martin BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
308 1.1.4.2 martin }
309 1.1.4.2 martin
310 1.1.4.2 martin static bool
311 1.1.4.2 martin genfb_vmbus_shutdown(device_t self, int flags)
312 1.1.4.2 martin {
313 1.1.4.2 martin
314 1.1.4.2 martin genfb_enable_polling(self);
315 1.1.4.2 martin return true;
316 1.1.4.2 martin }
317