genfb_vmbus.c revision 1.1.2.2 1 1.1.2.2 christos /* $NetBSD: genfb_vmbus.c,v 1.1.2.2 2019/06/10 22:07:09 christos Exp $ */
2 1.1.2.2 christos
3 1.1.2.2 christos /*-
4 1.1.2.2 christos * Copyright (c) 2007 Michael Lorenz
5 1.1.2.2 christos * All rights reserved.
6 1.1.2.2 christos *
7 1.1.2.2 christos * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 christos * modification, are permitted provided that the following conditions
9 1.1.2.2 christos * are met:
10 1.1.2.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 christos * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 christos * documentation and/or other materials provided with the distribution.
15 1.1.2.2 christos *
16 1.1.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 christos */
28 1.1.2.2 christos
29 1.1.2.2 christos #include <sys/cdefs.h>
30 1.1.2.2 christos __KERNEL_RCSID(0, "$NetBSD: genfb_vmbus.c,v 1.1.2.2 2019/06/10 22:07:09 christos Exp $");
31 1.1.2.2 christos
32 1.1.2.2 christos #include "opt_wsfb.h"
33 1.1.2.2 christos #include "opt_genfb.h"
34 1.1.2.2 christos
35 1.1.2.2 christos #include <sys/param.h>
36 1.1.2.2 christos #include <sys/systm.h>
37 1.1.2.2 christos #include <sys/kernel.h>
38 1.1.2.2 christos #include <sys/device.h>
39 1.1.2.2 christos #include <sys/proc.h>
40 1.1.2.2 christos #include <sys/mutex.h>
41 1.1.2.2 christos #include <sys/ioctl.h>
42 1.1.2.2 christos #include <sys/systm.h>
43 1.1.2.2 christos #include <sys/kauth.h>
44 1.1.2.2 christos
45 1.1.2.2 christos #include <dev/wsfb/genfbvar.h>
46 1.1.2.2 christos
47 1.1.2.2 christos #include <dev/hyperv/vmbusvar.h>
48 1.1.2.2 christos #include <dev/hyperv/genfb_vmbusvar.h>
49 1.1.2.2 christos
50 1.1.2.2 christos static int genfb_vmbus_match(device_t, cfdata_t, void *);
51 1.1.2.2 christos static void genfb_vmbus_attach(device_t, device_t, void *);
52 1.1.2.2 christos static int genfb_vmbus_ioctl(void *, void *, u_long, void *, int,
53 1.1.2.2 christos struct lwp *);
54 1.1.2.2 christos static paddr_t genfb_vmbus_mmap(void *, void *, off_t, int);
55 1.1.2.2 christos static int genfb_vmbus_drm_print(void *, const char *);
56 1.1.2.2 christos static bool genfb_vmbus_shutdown(device_t, int);
57 1.1.2.2 christos
58 1.1.2.2 christos CFATTACH_DECL_NEW(genfb_vmbus, sizeof(struct genfb_vmbus_softc),
59 1.1.2.2 christos genfb_vmbus_match, genfb_vmbus_attach, NULL, NULL);
60 1.1.2.2 christos
61 1.1.2.2 christos static int
62 1.1.2.2 christos genfb_vmbus_match(device_t parent, cfdata_t match, void *aux)
63 1.1.2.2 christos {
64 1.1.2.2 christos struct vmbus_attach_args *aa = aux;
65 1.1.2.2 christos
66 1.1.2.2 christos if (memcmp(aa->aa_type, &hyperv_guid_video, sizeof(*aa->aa_type)) != 0)
67 1.1.2.2 christos return 0;
68 1.1.2.2 christos
69 1.1.2.2 christos if (!genfb_is_enabled())
70 1.1.2.2 christos return 0; /* explicitly disabled by MD code */
71 1.1.2.2 christos
72 1.1.2.2 christos /* Use genfb(4) at pci in Gen.1 VM. */
73 1.1.2.2 christos if (hyperv_is_gen1())
74 1.1.2.2 christos return 0;
75 1.1.2.2 christos
76 1.1.2.2 christos return 1;
77 1.1.2.2 christos }
78 1.1.2.2 christos
79 1.1.2.2 christos static void
80 1.1.2.2 christos genfb_vmbus_attach(device_t parent, device_t self, void *aux)
81 1.1.2.2 christos {
82 1.1.2.2 christos static const struct genfb_ops zero_ops;
83 1.1.2.2 christos struct genfb_vmbus_softc *sc = device_private(self);
84 1.1.2.2 christos struct vmbus_attach_args *aa = aux;
85 1.1.2.2 christos struct genfb_ops ops = zero_ops;
86 1.1.2.2 christos
87 1.1.2.2 christos aprint_naive("\n");
88 1.1.2.2 christos aprint_normal(": Hyper-V Synthetic Video\n");
89 1.1.2.2 christos
90 1.1.2.2 christos sc->sc_gen.sc_dev = self;
91 1.1.2.2 christos sc->sc_memt = aa->aa_memt;
92 1.1.2.2 christos
93 1.1.2.2 christos genfb_init(&sc->sc_gen);
94 1.1.2.2 christos
95 1.1.2.2 christos /* firmware / MD code responsible for restoring the display */
96 1.1.2.2 christos if (sc->sc_gen.sc_pmfcb == NULL)
97 1.1.2.2 christos pmf_device_register1(self, NULL, NULL,
98 1.1.2.2 christos genfb_vmbus_shutdown);
99 1.1.2.2 christos else
100 1.1.2.2 christos pmf_device_register1(self,
101 1.1.2.2 christos sc->sc_gen.sc_pmfcb->gpc_suspend,
102 1.1.2.2 christos sc->sc_gen.sc_pmfcb->gpc_resume,
103 1.1.2.2 christos genfb_vmbus_shutdown);
104 1.1.2.2 christos
105 1.1.2.2 christos if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
106 1.1.2.2 christos aprint_debug_dev(self, "not configured by firmware\n");
107 1.1.2.2 christos return;
108 1.1.2.2 christos }
109 1.1.2.2 christos
110 1.1.2.2 christos ops.genfb_ioctl = genfb_vmbus_ioctl;
111 1.1.2.2 christos ops.genfb_mmap = genfb_vmbus_mmap;
112 1.1.2.2 christos
113 1.1.2.2 christos if (genfb_attach(&sc->sc_gen, &ops) != 0)
114 1.1.2.2 christos return;
115 1.1.2.2 christos
116 1.1.2.2 christos /* now try to attach a DRM */
117 1.1.2.2 christos config_found_ia(self, "drm", aux, genfb_vmbus_drm_print);
118 1.1.2.2 christos }
119 1.1.2.2 christos
120 1.1.2.2 christos static int
121 1.1.2.2 christos genfb_vmbus_drm_print(void *aux, const char *pnp)
122 1.1.2.2 christos {
123 1.1.2.2 christos
124 1.1.2.2 christos if (pnp)
125 1.1.2.2 christos aprint_normal("drm at %s", pnp);
126 1.1.2.2 christos return UNCONF;
127 1.1.2.2 christos }
128 1.1.2.2 christos
129 1.1.2.2 christos static int
130 1.1.2.2 christos genfb_vmbus_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
131 1.1.2.2 christos struct lwp *l)
132 1.1.2.2 christos {
133 1.1.2.2 christos
134 1.1.2.2 christos switch (cmd) {
135 1.1.2.2 christos case WSDISPLAYIO_GTYPE:
136 1.1.2.2 christos *(u_int *)data = WSDISPLAY_TYPE_GENFB;
137 1.1.2.2 christos return 0;
138 1.1.2.2 christos }
139 1.1.2.2 christos
140 1.1.2.2 christos return EPASSTHROUGH;
141 1.1.2.2 christos }
142 1.1.2.2 christos
143 1.1.2.2 christos static paddr_t
144 1.1.2.2 christos genfb_vmbus_mmap(void *v, void *vs, off_t offset, int prot)
145 1.1.2.2 christos {
146 1.1.2.2 christos struct genfb_vmbus_softc *sc = v;
147 1.1.2.2 christos
148 1.1.2.2 christos return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset, offset, prot,
149 1.1.2.2 christos BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
150 1.1.2.2 christos }
151 1.1.2.2 christos
152 1.1.2.2 christos static bool
153 1.1.2.2 christos genfb_vmbus_shutdown(device_t self, int flags)
154 1.1.2.2 christos {
155 1.1.2.2 christos
156 1.1.2.2 christos genfb_enable_polling(self);
157 1.1.2.2 christos return true;
158 1.1.2.2 christos }
159