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