vmbus_acpi.c revision 1.3
1/*	$NetBSD: vmbus_acpi.c,v 1.3 2020/10/24 08:57:06 skrll 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.3 2020/10/24 08:57:06 skrll 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
42ACPI_MODULE_NAME	("vmbus_acpi")
43
44static int	vmbus_acpi_match(device_t, cfdata_t, void *);
45static void	vmbus_acpi_attach(device_t, device_t, void *);
46static int	vmbus_acpi_detach(device_t, int);
47
48struct vmbus_acpi_softc {
49	struct vmbus_softc sc;
50};
51
52CFATTACH_DECL_NEW(vmbus_acpi, sizeof(struct vmbus_acpi_softc),
53    vmbus_acpi_match, vmbus_acpi_attach, vmbus_acpi_detach, NULL);
54
55static const char * const vmbus_acpi_ids[] = {
56	"VMBUS",
57	"VMBus",
58	NULL
59};
60
61static int
62vmbus_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
78static void
79vmbus_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 = BUS_DMA_TAG_VALID(aa->aa_dmat64) ?
88	    aa->aa_dmat64 : aa->aa_dmat;
89
90	if (vmbus_attach(&sc->sc))
91		return;
92
93	(void)pmf_device_register(self, NULL, NULL);
94}
95
96static int
97vmbus_acpi_detach(device_t self, int flags)
98{
99	struct vmbus_acpi_softc *sc = device_private(self);
100	int rv;
101
102	rv = vmbus_detach(&sc->sc, flags);
103	if (rv)
104		return rv;
105
106	pmf_device_deregister(self);
107
108	return 0;
109}
110