gicv3_acpi.c revision 1.7 1 1.7 jmcneill /* $NetBSD: gicv3_acpi.c,v 1.7 2020/02/13 00:02:21 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jmcneill * by Jared McNeill <jmcneill (at) invisible.ca>.
9 1.1 jmcneill *
10 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
11 1.1 jmcneill * modification, are permitted provided that the following conditions
12 1.1 jmcneill * are met:
13 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
15 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
17 1.1 jmcneill * documentation and/or other materials provided with the distribution.
18 1.1 jmcneill *
19 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jmcneill */
31 1.1 jmcneill
32 1.3 jmcneill #include "pci.h"
33 1.3 jmcneill
34 1.1 jmcneill #define _INTR_PRIVATE
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/cdefs.h>
37 1.7 jmcneill __KERNEL_RCSID(0, "$NetBSD: gicv3_acpi.c,v 1.7 2020/02/13 00:02:21 jmcneill Exp $");
38 1.1 jmcneill
39 1.1 jmcneill #include <sys/param.h>
40 1.1 jmcneill #include <sys/bus.h>
41 1.1 jmcneill #include <sys/cpu.h>
42 1.1 jmcneill #include <sys/kernel.h>
43 1.1 jmcneill #include <sys/device.h>
44 1.1 jmcneill #include <sys/kmem.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <dev/acpi/acpireg.h>
47 1.1 jmcneill #include <dev/acpi/acpivar.h>
48 1.1 jmcneill
49 1.1 jmcneill #include <dev/fdt/fdtvar.h>
50 1.1 jmcneill
51 1.1 jmcneill #include <arm/cortex/gicv3.h>
52 1.2 jmcneill #include <arm/cortex/gicv3_its.h>
53 1.1 jmcneill #include <arm/cortex/gic_reg.h>
54 1.1 jmcneill
55 1.5 jmcneill #include <arm/acpi/gic_v2m_acpi.h>
56 1.5 jmcneill
57 1.1 jmcneill #define GICD_SIZE 0x10000
58 1.1 jmcneill #define GICR_SIZE 0x20000
59 1.2 jmcneill #define GITS_SIZE 0x20000
60 1.1 jmcneill
61 1.1 jmcneill extern struct bus_space arm_generic_bs_tag;
62 1.2 jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
63 1.1 jmcneill
64 1.1 jmcneill struct gicv3_acpi_softc {
65 1.1 jmcneill struct gicv3_softc sc_gic;
66 1.1 jmcneill
67 1.1 jmcneill ACPI_MADT_GENERIC_DISTRIBUTOR *sc_madt_gicd;
68 1.1 jmcneill };
69 1.1 jmcneill
70 1.1 jmcneill static int gicv3_acpi_match(device_t, cfdata_t, void *);
71 1.1 jmcneill static void gicv3_acpi_attach(device_t, device_t, void *);
72 1.1 jmcneill
73 1.1 jmcneill static int gicv3_acpi_map_dist(struct gicv3_acpi_softc *);
74 1.1 jmcneill static int gicv3_acpi_map_redist(struct gicv3_acpi_softc *);
75 1.3 jmcneill #if NPCI > 0
76 1.5 jmcneill static int gicv3_acpi_map_msi(struct gicv3_acpi_softc *);
77 1.3 jmcneill #endif
78 1.1 jmcneill
79 1.1 jmcneill CFATTACH_DECL_NEW(gicv3_acpi, sizeof(struct gicv3_acpi_softc), gicv3_acpi_match, gicv3_acpi_attach, NULL, NULL);
80 1.1 jmcneill
81 1.1 jmcneill static int
82 1.1 jmcneill gicv3_acpi_match(device_t parent, cfdata_t cf, void *aux)
83 1.1 jmcneill {
84 1.1 jmcneill ACPI_SUBTABLE_HEADER *hdrp = aux;
85 1.1 jmcneill ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
86 1.1 jmcneill
87 1.1 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
88 1.1 jmcneill return 0;
89 1.1 jmcneill
90 1.1 jmcneill gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)hdrp;
91 1.1 jmcneill
92 1.1 jmcneill switch (gicd->Version) {
93 1.1 jmcneill case ACPI_MADT_GIC_VERSION_NONE:
94 1.1 jmcneill return __SHIFTOUT(reg_id_aa64pfr0_el1_read(), ID_AA64PFR0_EL1_GIC) == 1;
95 1.1 jmcneill case ACPI_MADT_GIC_VERSION_V3:
96 1.1 jmcneill case ACPI_MADT_GIC_VERSION_V4:
97 1.1 jmcneill return 1;
98 1.1 jmcneill default:
99 1.1 jmcneill return 0;
100 1.1 jmcneill }
101 1.1 jmcneill }
102 1.1 jmcneill
103 1.1 jmcneill static void
104 1.1 jmcneill gicv3_acpi_attach(device_t parent, device_t self, void *aux)
105 1.1 jmcneill {
106 1.1 jmcneill struct gicv3_acpi_softc * const sc = device_private(self);
107 1.1 jmcneill ACPI_MADT_GENERIC_DISTRIBUTOR *gicd = aux;
108 1.1 jmcneill int error;
109 1.1 jmcneill
110 1.1 jmcneill sc->sc_gic.sc_dev = self;
111 1.1 jmcneill sc->sc_gic.sc_bst = &arm_generic_bs_tag;
112 1.2 jmcneill sc->sc_gic.sc_dmat = &arm_generic_dma_tag;
113 1.1 jmcneill sc->sc_madt_gicd = gicd;
114 1.1 jmcneill
115 1.1 jmcneill aprint_naive("\n");
116 1.1 jmcneill aprint_normal(": GICv3\n");
117 1.1 jmcneill
118 1.1 jmcneill error = gicv3_acpi_map_dist(sc);
119 1.1 jmcneill if (error) {
120 1.1 jmcneill aprint_error_dev(self, "failed to map distributor: %d\n", error);
121 1.1 jmcneill return;
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill error = gicv3_acpi_map_redist(sc);
125 1.1 jmcneill if (error) {
126 1.1 jmcneill aprint_error_dev(self, "failed to map redistributor: %d\n", error);
127 1.1 jmcneill return;
128 1.1 jmcneill }
129 1.1 jmcneill
130 1.1 jmcneill error = gicv3_init(&sc->sc_gic);
131 1.1 jmcneill if (error) {
132 1.1 jmcneill aprint_error_dev(self, "failed to initialize GIC: %d\n", error);
133 1.1 jmcneill return;
134 1.1 jmcneill }
135 1.1 jmcneill
136 1.3 jmcneill #if NPCI > 0
137 1.5 jmcneill gicv3_acpi_map_msi(sc);
138 1.3 jmcneill #endif
139 1.2 jmcneill
140 1.1 jmcneill arm_fdt_irq_set_handler(gicv3_irq_handler);
141 1.1 jmcneill }
142 1.1 jmcneill
143 1.1 jmcneill static int
144 1.1 jmcneill gicv3_acpi_map_dist(struct gicv3_acpi_softc *sc)
145 1.1 jmcneill {
146 1.1 jmcneill const bus_addr_t addr = sc->sc_madt_gicd->BaseAddress;
147 1.1 jmcneill const bus_size_t size = GICD_SIZE;
148 1.1 jmcneill int error;
149 1.1 jmcneill
150 1.1 jmcneill error = bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &sc->sc_gic.sc_bsh_d);
151 1.1 jmcneill if (error)
152 1.1 jmcneill return error;
153 1.1 jmcneill
154 1.1 jmcneill return 0;
155 1.1 jmcneill }
156 1.1 jmcneill
157 1.1 jmcneill static ACPI_STATUS
158 1.1 jmcneill gicv3_acpi_count_gicr(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
159 1.1 jmcneill {
160 1.1 jmcneill ACPI_MADT_GENERIC_REDISTRIBUTOR *gicr;
161 1.1 jmcneill int *count = aux;
162 1.1 jmcneill
163 1.1 jmcneill if (hdrp->Type == ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR) {
164 1.1 jmcneill gicr = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)hdrp;
165 1.1 jmcneill *count += howmany(gicr->Length, GICR_SIZE);
166 1.1 jmcneill }
167 1.1 jmcneill
168 1.1 jmcneill return AE_OK;
169 1.1 jmcneill }
170 1.1 jmcneill
171 1.1 jmcneill static ACPI_STATUS
172 1.1 jmcneill gicv3_acpi_map_gicr(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
173 1.1 jmcneill {
174 1.1 jmcneill struct gicv3_acpi_softc * const sc = aux;
175 1.1 jmcneill ACPI_MADT_GENERIC_REDISTRIBUTOR *gicr;
176 1.1 jmcneill bus_space_handle_t bsh;
177 1.1 jmcneill bus_size_t off;
178 1.1 jmcneill
179 1.1 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR)
180 1.1 jmcneill return AE_OK;
181 1.1 jmcneill
182 1.1 jmcneill gicr = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)hdrp;
183 1.1 jmcneill
184 1.1 jmcneill if (bus_space_map(sc->sc_gic.sc_bst, gicr->BaseAddress, gicr->Length, 0, &bsh) != 0) {
185 1.1 jmcneill aprint_error_dev(sc->sc_gic.sc_dev, "failed to map redistributor at 0x%" PRIx64 " len %#x\n",
186 1.1 jmcneill gicr->BaseAddress, gicr->Length);
187 1.1 jmcneill return AE_OK;
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill for (off = 0; off < gicr->Length; off += GICR_SIZE) {
191 1.1 jmcneill const int redist = sc->sc_gic.sc_bsh_r_count;
192 1.1 jmcneill if (bus_space_subregion(sc->sc_gic.sc_bst, bsh, off, GICR_SIZE, &sc->sc_gic.sc_bsh_r[redist]) != 0) {
193 1.1 jmcneill aprint_error_dev(sc->sc_gic.sc_dev, "couldn't subregion redistributor registers\n");
194 1.1 jmcneill return AE_OK;
195 1.1 jmcneill }
196 1.1 jmcneill
197 1.1 jmcneill aprint_debug_dev(sc->sc_gic.sc_dev, "redist at 0x%" PRIx64 " [GICR]\n", gicr->BaseAddress + off);
198 1.1 jmcneill
199 1.1 jmcneill sc->sc_gic.sc_bsh_r_count++;
200 1.1 jmcneill
201 1.1 jmcneill /* If this is the last redist in this region, skip to the next one */
202 1.1 jmcneill const uint32_t typer = bus_space_read_4(sc->sc_gic.sc_bst, sc->sc_gic.sc_bsh_r[redist], GICR_TYPER);
203 1.1 jmcneill if (typer & GICR_TYPER_Last)
204 1.1 jmcneill break;
205 1.4 jmcneill
206 1.4 jmcneill /* If the redistributor supports virtual LPIs, skip the VLPI register region */
207 1.4 jmcneill if (typer & GICR_TYPER_VLPIS)
208 1.4 jmcneill off += GICR_SIZE;
209 1.1 jmcneill }
210 1.1 jmcneill
211 1.1 jmcneill return AE_OK;
212 1.1 jmcneill }
213 1.1 jmcneill
214 1.1 jmcneill static ACPI_STATUS
215 1.1 jmcneill gicv3_acpi_count_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
216 1.1 jmcneill {
217 1.1 jmcneill ACPI_MADT_GENERIC_INTERRUPT *gicc;
218 1.1 jmcneill int *count = aux;
219 1.1 jmcneill
220 1.1 jmcneill if (hdrp->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
221 1.1 jmcneill gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
222 1.1 jmcneill if ((gicc->Flags & ACPI_MADT_ENABLED) != 0)
223 1.1 jmcneill (*count)++;
224 1.1 jmcneill }
225 1.1 jmcneill
226 1.1 jmcneill return AE_OK;
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill static ACPI_STATUS
230 1.1 jmcneill gicv3_acpi_map_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
231 1.1 jmcneill {
232 1.1 jmcneill struct gicv3_acpi_softc * const sc = aux;
233 1.1 jmcneill ACPI_MADT_GENERIC_INTERRUPT *gicc;
234 1.1 jmcneill
235 1.1 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
236 1.1 jmcneill return AE_OK;
237 1.1 jmcneill
238 1.1 jmcneill gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
239 1.1 jmcneill if ((gicc->Flags & ACPI_MADT_ENABLED) == 0)
240 1.1 jmcneill return AE_OK;
241 1.1 jmcneill
242 1.1 jmcneill const int redist = sc->sc_gic.sc_bsh_r_count;
243 1.1 jmcneill if (bus_space_map(sc->sc_gic.sc_bst, gicc->GicrBaseAddress, GICR_SIZE, 0, &sc->sc_gic.sc_bsh_r[redist]) != 0) {
244 1.1 jmcneill aprint_error_dev(sc->sc_gic.sc_dev, "failed to map redistributor at 0x%" PRIx64 " len %#x\n",
245 1.1 jmcneill gicc->GicrBaseAddress, GICR_SIZE);
246 1.1 jmcneill return AE_OK;
247 1.1 jmcneill }
248 1.1 jmcneill
249 1.1 jmcneill aprint_debug_dev(sc->sc_gic.sc_dev, "redist at 0x%" PRIx64 " [GICC]\n", gicc->GicrBaseAddress);
250 1.1 jmcneill
251 1.1 jmcneill sc->sc_gic.sc_bsh_r_count++;
252 1.1 jmcneill
253 1.1 jmcneill return AE_OK;
254 1.1 jmcneill }
255 1.1 jmcneill
256 1.1 jmcneill static int
257 1.1 jmcneill gicv3_acpi_map_redist(struct gicv3_acpi_softc *sc)
258 1.1 jmcneill {
259 1.1 jmcneill bool use_gicr = false;
260 1.1 jmcneill int max_redist = 0;
261 1.1 jmcneill
262 1.1 jmcneill /*
263 1.1 jmcneill * Try to use GICR structures to describe redistributors. If no GICR
264 1.1 jmcneill * subtables are found, use the GICR address from the GICC subtables.
265 1.1 jmcneill */
266 1.1 jmcneill acpi_madt_walk(gicv3_acpi_count_gicr, &max_redist);
267 1.1 jmcneill if (max_redist != 0)
268 1.1 jmcneill use_gicr = true;
269 1.1 jmcneill else
270 1.1 jmcneill acpi_madt_walk(gicv3_acpi_count_gicc, &max_redist);
271 1.1 jmcneill
272 1.1 jmcneill if (max_redist == 0)
273 1.1 jmcneill return ENODEV;
274 1.1 jmcneill
275 1.1 jmcneill sc->sc_gic.sc_bsh_r = kmem_alloc(sizeof(bus_space_handle_t) * max_redist, KM_SLEEP);
276 1.1 jmcneill if (use_gicr)
277 1.1 jmcneill acpi_madt_walk(gicv3_acpi_map_gicr, sc);
278 1.1 jmcneill else
279 1.1 jmcneill acpi_madt_walk(gicv3_acpi_map_gicc, sc);
280 1.1 jmcneill
281 1.1 jmcneill if (sc->sc_gic.sc_bsh_r_count == 0)
282 1.1 jmcneill return ENXIO;
283 1.1 jmcneill
284 1.1 jmcneill return 0;
285 1.1 jmcneill }
286 1.2 jmcneill
287 1.3 jmcneill #if NPCI > 0
288 1.2 jmcneill static ACPI_STATUS
289 1.2 jmcneill gicv3_acpi_map_gits(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
290 1.2 jmcneill {
291 1.2 jmcneill struct gicv3_acpi_softc * const sc = aux;
292 1.2 jmcneill ACPI_MADT_GENERIC_TRANSLATOR *gits;
293 1.2 jmcneill bus_space_handle_t bsh;
294 1.2 jmcneill
295 1.2 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_TRANSLATOR)
296 1.2 jmcneill return AE_OK;
297 1.2 jmcneill
298 1.2 jmcneill gits = (ACPI_MADT_GENERIC_TRANSLATOR *)hdrp;
299 1.2 jmcneill
300 1.2 jmcneill if (bus_space_map(sc->sc_gic.sc_bst, gits->BaseAddress, GITS_SIZE, 0, &bsh) != 0) {
301 1.2 jmcneill aprint_error_dev(sc->sc_gic.sc_dev, "failed to map ITS at 0x%" PRIx64 " len %#x\n",
302 1.2 jmcneill gits->BaseAddress, GITS_SIZE);
303 1.2 jmcneill return AE_OK;
304 1.2 jmcneill }
305 1.2 jmcneill
306 1.7 jmcneill aprint_normal_dev(sc->sc_gic.sc_dev, "ITS #%d at 0x%" PRIx64 "\n",
307 1.7 jmcneill gits->TranslationId, gits->BaseAddress);
308 1.2 jmcneill
309 1.7 jmcneill gicv3_its_init(&sc->sc_gic, bsh, gits->BaseAddress, gits->TranslationId);
310 1.2 jmcneill
311 1.2 jmcneill return AE_OK;
312 1.2 jmcneill }
313 1.2 jmcneill
314 1.2 jmcneill static int
315 1.5 jmcneill gicv3_acpi_map_msi(struct gicv3_acpi_softc *sc)
316 1.2 jmcneill {
317 1.2 jmcneill acpi_madt_walk(gicv3_acpi_map_gits, sc);
318 1.5 jmcneill acpi_madt_walk(gic_v2m_acpi_find_msi_frame, sc->sc_gic.sc_dev);
319 1.2 jmcneill
320 1.2 jmcneill return 0;
321 1.2 jmcneill }
322 1.5 jmcneill
323 1.3 jmcneill #endif
324