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