xref: /netbsd-src/sys/dev/acpi/virtio_acpi.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $NetBSD: virtio_acpi.c,v 1.2 2018/11/16 23:18:17 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jared McNeill <jmcneill@invisible.ca>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: virtio_acpi.c,v 1.2 2018/11/16 23:18:17 jmcneill Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 
40 #include <dev/acpi/acpireg.h>
41 #include <dev/acpi/acpivar.h>
42 #include <dev/acpi/acpi_intr.h>
43 
44 #define	VIRTIO_PRIVATE
45 #include <dev/virtio/virtio_mmiovar.h>
46 
47 struct virtio_acpi_softc {
48 	struct virtio_mmio_softc	sc_msc;
49 	ACPI_HANDLE			sc_handle;
50 	int				sc_irq;
51 	int				sc_irqtype;
52 };
53 
54 static int	virtio_acpi_match(device_t, cfdata_t, void *);
55 static void	virtio_acpi_attach(device_t, device_t, void *);
56 static int	virtio_acpi_rescan(device_t, const char *, const int *);
57 static int	virtio_acpi_detach(device_t, int);
58 
59 static int	virtio_acpi_setup_interrupts(struct virtio_mmio_softc *);
60 static void	virtio_acpi_free_interrupts(struct virtio_mmio_softc *);
61 
62 CFATTACH_DECL3_NEW(virtio_acpi, sizeof(struct virtio_acpi_softc),
63     virtio_acpi_match, virtio_acpi_attach, virtio_acpi_detach, NULL,
64     virtio_acpi_rescan, (void *)voidop, DVF_DETACH_SHUTDOWN);
65 
66 static const char * const compatible[] = {
67 	"LNRO0005",
68 	NULL
69 };
70 
71 static int
72 virtio_acpi_match(device_t parent, cfdata_t cf, void *aux)
73 {
74 	struct acpi_attach_args *aa = aux;
75 
76 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
77 		return 0;
78 
79 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
80 }
81 
82 static void
83 virtio_acpi_attach(device_t parent, device_t self, void *aux)
84 {
85 	struct virtio_acpi_softc * const sc = device_private(self);
86 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
87 	struct virtio_softc * const vsc = &msc->sc_sc;
88 	struct acpi_attach_args *aa = aux;
89 	struct acpi_resources res;
90 	struct acpi_mem *mem;
91 	struct acpi_irq *irq;
92 	ACPI_STATUS rv;
93 	int error;
94 
95 	sc->sc_handle = aa->aa_node->ad_handle;
96 	msc->sc_iot = aa->aa_memt;
97 	vsc->sc_dev = self;
98 	vsc->sc_dmat = aa->aa_dmat;
99 
100 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
101 	    &res, &acpi_resource_parse_ops_default);
102 	if (ACPI_FAILURE(rv))
103 		return;
104 
105 	mem = acpi_res_mem(&res, 0);
106 	if (mem == NULL) {
107 		aprint_error_dev(self, "couldn't find mem resource\n");
108 		goto done;
109 	}
110 
111 	irq = acpi_res_irq(&res, 0);
112 	if (irq == NULL) {
113 		aprint_error_dev(self, "couldn't find irq resource\n");
114 		goto done;
115 	}
116 	sc->sc_irq = irq->ar_irq;
117 	sc->sc_irqtype = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
118 
119 	error = bus_space_map(msc->sc_iot, mem->ar_base, mem->ar_length, 0, &msc->sc_ioh);
120 	if (error) {
121 		aprint_error_dev(self, "couldn't map registers\n");
122 		return;
123 	}
124 	msc->sc_iosize = mem->ar_length;
125 
126 	msc->sc_setup_interrupts = virtio_acpi_setup_interrupts;
127 	msc->sc_free_interrupts = virtio_acpi_free_interrupts;
128 
129 	virtio_mmio_common_attach(msc);
130 	virtio_acpi_rescan(self, "virtio", NULL);
131 
132 done:
133 	acpi_resource_cleanup(&res);
134 }
135 
136 static int
137 virtio_acpi_detach(device_t self, int flags)
138 {
139 	struct virtio_acpi_softc * const sc = device_private(self);
140 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
141 
142 	return virtio_mmio_common_detach(msc, flags);
143 }
144 
145 static int
146 virtio_acpi_rescan(device_t self, const char *ifattr, const int *locs)
147 {
148 	struct virtio_acpi_softc * const sc = device_private(self);
149 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
150 	struct virtio_softc * const vsc = &msc->sc_sc;
151 	struct virtio_attach_args va;
152 
153 	if (vsc->sc_child)
154 		return 0;
155 
156 	memset(&va, 0, sizeof(va));
157 	va.sc_childdevid = vsc->sc_childdevid;
158 
159 	config_found_ia(self, ifattr, &va, virtiobusprint);
160 
161 	return 0;
162 }
163 
164 static int
165 virtio_acpi_setup_interrupts(struct virtio_mmio_softc *msc)
166 {
167 	struct virtio_acpi_softc * const sc = (struct virtio_acpi_softc *)msc;
168 	struct virtio_softc * const vsc = &msc->sc_sc;
169 
170 	msc->sc_ih = acpi_intr_establish(vsc->sc_dev, (uint64_t)sc->sc_handle,
171             IPL_VM, false, virtio_mmio_intr, msc, device_xname(vsc->sc_dev));
172 	if (msc->sc_ih == NULL) {
173 		aprint_error_dev(vsc->sc_dev, "couldn't install interrupt handler\n");
174 		return -1;
175 	}
176 
177 	aprint_normal_dev(vsc->sc_dev, "interrupting on irq %d\n", sc->sc_irq);
178 
179 	return 0;
180 }
181 
182 static void
183 virtio_acpi_free_interrupts(struct virtio_mmio_softc *msc)
184 {
185 	if (msc->sc_ih != NULL) {
186 		acpi_intr_disestablish(msc->sc_ih);
187 		msc->sc_ih = NULL;
188 	}
189 }
190