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