xref: /netbsd-src/sys/dev/acpi/vmbus_acpi.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: vmbus_acpi.c,v 1.2 2019/05/24 14:28:48 nonaka Exp $	*/
2 
3 /*
4  * Copyright (c) 2018 Kimihiro Nonaka <nonaka@NetBSD.org>
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. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: vmbus_acpi.c,v 1.2 2019/05/24 14:28:48 nonaka Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/systm.h>
34 #include <sys/kmem.h>
35 
36 #include <dev/acpi/acpireg.h>
37 #include <dev/acpi/acpivar.h>
38 
39 #include <dev/hyperv/vmbusvar.h>
40 
41 #define _COMPONENT	ACPI_RESOURCE_COMPONENT
42 ACPI_MODULE_NAME	("vmbus_acpi")
43 
44 static int	vmbus_acpi_match(device_t, cfdata_t, void *);
45 static void	vmbus_acpi_attach(device_t, device_t, void *);
46 static int	vmbus_acpi_detach(device_t, int);
47 
48 struct vmbus_acpi_softc {
49 	struct vmbus_softc sc;
50 };
51 
52 CFATTACH_DECL_NEW(vmbus_acpi, sizeof(struct vmbus_acpi_softc),
53     vmbus_acpi_match, vmbus_acpi_attach, vmbus_acpi_detach, NULL);
54 
55 static const char * const vmbus_acpi_ids[] = {
56 	"VMBUS",
57 	"VMBus",
58 	NULL
59 };
60 
61 static int
62 vmbus_acpi_match(device_t parent, cfdata_t match, void *opaque)
63 {
64 	struct acpi_attach_args *aa = opaque;
65 
66 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
67 		return 0;
68 
69 	if (!acpi_match_hid(aa->aa_node->ad_devinfo, vmbus_acpi_ids))
70 		return 0;
71 
72 	if (!vmbus_match(parent, match, opaque))
73 		return 0;
74 
75 	return 1;
76 }
77 
78 static void
79 vmbus_acpi_attach(device_t parent, device_t self, void *opaque)
80 {
81 	struct vmbus_acpi_softc *sc = device_private(self);
82 	struct acpi_attach_args *aa = opaque;
83 
84 	sc->sc.sc_dev = self;
85 	sc->sc.sc_iot = aa->aa_iot;
86 	sc->sc.sc_memt = aa->aa_memt;
87 	sc->sc.sc_dmat = aa->aa_dmat64 ? aa->aa_dmat64 : aa->aa_dmat;
88 
89 	if (vmbus_attach(&sc->sc))
90 		return;
91 
92 	(void)pmf_device_register(self, NULL, NULL);
93 }
94 
95 static int
96 vmbus_acpi_detach(device_t self, int flags)
97 {
98 	struct vmbus_acpi_softc *sc = device_private(self);
99 	int rv;
100 
101 	rv = vmbus_detach(&sc->sc, flags);
102 	if (rv)
103 		return rv;
104 
105 	pmf_device_deregister(self);
106 
107 	return 0;
108 }
109