1/*	$NetBSD: genfb_vmbus.c,v 1.3 2021/08/07 16:19:11 thorpej 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.3 2021/08/07 16:19:11 thorpej 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
50static int	genfb_vmbus_match(device_t, cfdata_t, void *);
51static void	genfb_vmbus_attach(device_t, device_t, void *);
52static int	genfb_vmbus_ioctl(void *, void *, u_long, void *, int,
53		    struct lwp *);
54static paddr_t	genfb_vmbus_mmap(void *, void *, off_t, int);
55static int	genfb_vmbus_drm_print(void *, const char *);
56static bool	genfb_vmbus_shutdown(device_t, int);
57
58CFATTACH_DECL_NEW(genfb_vmbus, sizeof(struct genfb_vmbus_softc),
59    genfb_vmbus_match, genfb_vmbus_attach, NULL, NULL);
60
61static int
62genfb_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
79static void
80genfb_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(self, aux, genfb_vmbus_drm_print,
118	    CFARGS(.iattr = "drm"));
119}
120
121static int
122genfb_vmbus_drm_print(void *aux, const char *pnp)
123{
124
125	if (pnp)
126		aprint_normal("drm at %s", pnp);
127	return UNCONF;
128}
129
130static int
131genfb_vmbus_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
132    struct lwp *l)
133{
134
135	switch (cmd) {
136	case WSDISPLAYIO_GTYPE:
137		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
138		return 0;
139	}
140
141	return EPASSTHROUGH;
142}
143
144static paddr_t
145genfb_vmbus_mmap(void *v, void *vs, off_t offset, int prot)
146{
147	struct genfb_vmbus_softc *sc = v;
148
149	return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset, offset, prot,
150	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
151}
152
153static bool
154genfb_vmbus_shutdown(device_t self, int flags)
155{
156
157	genfb_enable_polling(self);
158	return true;
159}
160