virtio_acpi.c revision 1.1 1 /* $NetBSD: virtio_acpi.c,v 1.1 2018/10/21 12:26:38 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 (at) 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.1 2018/10/21 12:26:38 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
43 #define VIRTIO_PRIVATE
44 #include <dev/virtio/virtio_mmiovar.h>
45
46 struct virtio_acpi_softc {
47 struct virtio_mmio_softc sc_msc;
48 int sc_irq;
49 int sc_irqtype;
50 };
51
52 static int virtio_acpi_match(device_t, cfdata_t, void *);
53 static void virtio_acpi_attach(device_t, device_t, void *);
54 static int virtio_acpi_rescan(device_t, const char *, const int *);
55 static int virtio_acpi_detach(device_t, int);
56
57 static int virtio_acpi_setup_interrupts(struct virtio_mmio_softc *);
58 static void virtio_acpi_free_interrupts(struct virtio_mmio_softc *);
59
60 CFATTACH_DECL3_NEW(virtio_acpi, sizeof(struct virtio_acpi_softc),
61 virtio_acpi_match, virtio_acpi_attach, virtio_acpi_detach, NULL,
62 virtio_acpi_rescan, (void *)voidop, DVF_DETACH_SHUTDOWN);
63
64 static const char * const compatible[] = {
65 "LNRO0005",
66 NULL
67 };
68
69 static int
70 virtio_acpi_match(device_t parent, cfdata_t cf, void *aux)
71 {
72 struct acpi_attach_args *aa = aux;
73
74 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
75 return 0;
76
77 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
78 }
79
80 static void
81 virtio_acpi_attach(device_t parent, device_t self, void *aux)
82 {
83 struct virtio_acpi_softc * const sc = device_private(self);
84 struct virtio_mmio_softc * const msc = &sc->sc_msc;
85 struct virtio_softc * const vsc = &msc->sc_sc;
86 struct acpi_attach_args *aa = aux;
87 struct acpi_resources res;
88 struct acpi_mem *mem;
89 struct acpi_irq *irq;
90 ACPI_STATUS rv;
91 int error;
92
93 msc->sc_iot = aa->aa_memt;
94 vsc->sc_dev = self;
95 vsc->sc_dmat = aa->aa_dmat;
96
97 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
98 &res, &acpi_resource_parse_ops_default);
99 if (ACPI_FAILURE(rv))
100 return;
101
102 mem = acpi_res_mem(&res, 0);
103 if (mem == NULL) {
104 aprint_error_dev(self, "couldn't find mem resource\n");
105 goto done;
106 }
107
108 irq = acpi_res_irq(&res, 0);
109 if (irq == NULL) {
110 aprint_error_dev(self, "couldn't find irq resource\n");
111 goto done;
112 }
113 sc->sc_irq = irq->ar_irq;
114 sc->sc_irqtype = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
115
116 error = bus_space_map(msc->sc_iot, mem->ar_base, mem->ar_length, 0, &msc->sc_ioh);
117 if (error) {
118 aprint_error_dev(self, "couldn't map registers\n");
119 return;
120 }
121 msc->sc_iosize = mem->ar_length;
122
123 msc->sc_setup_interrupts = virtio_acpi_setup_interrupts;
124 msc->sc_free_interrupts = virtio_acpi_free_interrupts;
125
126 virtio_mmio_common_attach(msc);
127 virtio_acpi_rescan(self, "virtio", NULL);
128
129 done:
130 acpi_resource_cleanup(&res);
131 }
132
133 static int
134 virtio_acpi_detach(device_t self, int flags)
135 {
136 struct virtio_acpi_softc * const sc = device_private(self);
137 struct virtio_mmio_softc * const msc = &sc->sc_msc;
138
139 return virtio_mmio_common_detach(msc, flags);
140 }
141
142 static int
143 virtio_acpi_rescan(device_t self, const char *ifattr, const int *locs)
144 {
145 struct virtio_acpi_softc * const sc = device_private(self);
146 struct virtio_mmio_softc * const msc = &sc->sc_msc;
147 struct virtio_softc * const vsc = &msc->sc_sc;
148 struct virtio_attach_args va;
149
150 if (vsc->sc_child)
151 return 0;
152
153 memset(&va, 0, sizeof(va));
154 va.sc_childdevid = vsc->sc_childdevid;
155
156 config_found_ia(self, ifattr, &va, virtiobusprint);
157
158 return 0;
159 }
160
161 static int
162 virtio_acpi_setup_interrupts(struct virtio_mmio_softc *msc)
163 {
164 struct virtio_acpi_softc * const sc = (struct virtio_acpi_softc *)msc;
165 struct virtio_softc * const vsc = &msc->sc_sc;
166
167 msc->sc_ih = intr_establish(sc->sc_irq, IPL_VM, sc->sc_irqtype, virtio_mmio_intr, msc);
168 if (msc->sc_ih == NULL) {
169 aprint_error_dev(vsc->sc_dev, "couldn't install interrupt handler\n");
170 return -1;
171 }
172
173 aprint_normal_dev(vsc->sc_dev, "interrupting on irq %d\n", sc->sc_irq);
174
175 return 0;
176 }
177
178 static void
179 virtio_acpi_free_interrupts(struct virtio_mmio_softc *msc)
180 {
181 if (msc->sc_ih != NULL) {
182 intr_disestablish(msc->sc_ih);
183 msc->sc_ih = NULL;
184 }
185 }
186