gicv3_acpi.c revision 1.3.8.1 1 1.3.8.1 martin /* $NetBSD: gicv3_acpi.c,v 1.3.8.1 2019/09/22 12:42:12 martin 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.3.8.1 martin __KERNEL_RCSID(0, "$NetBSD: gicv3_acpi.c,v 1.3.8.1 2019/09/22 12:42:12 martin 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.1 jmcneill #define GICD_SIZE 0x10000
56 1.1 jmcneill #define GICR_SIZE 0x20000
57 1.2 jmcneill #define GITS_SIZE 0x20000
58 1.1 jmcneill
59 1.1 jmcneill extern struct bus_space arm_generic_bs_tag;
60 1.2 jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
61 1.1 jmcneill
62 1.1 jmcneill struct gicv3_acpi_softc {
63 1.1 jmcneill struct gicv3_softc sc_gic;
64 1.1 jmcneill
65 1.1 jmcneill ACPI_MADT_GENERIC_DISTRIBUTOR *sc_madt_gicd;
66 1.1 jmcneill };
67 1.1 jmcneill
68 1.1 jmcneill static int gicv3_acpi_match(device_t, cfdata_t, void *);
69 1.1 jmcneill static void gicv3_acpi_attach(device_t, device_t, void *);
70 1.1 jmcneill
71 1.1 jmcneill static int gicv3_acpi_map_dist(struct gicv3_acpi_softc *);
72 1.1 jmcneill static int gicv3_acpi_map_redist(struct gicv3_acpi_softc *);
73 1.3 jmcneill #if NPCI > 0
74 1.2 jmcneill static int gicv3_acpi_map_its(struct gicv3_acpi_softc *);
75 1.3 jmcneill #endif
76 1.1 jmcneill
77 1.1 jmcneill CFATTACH_DECL_NEW(gicv3_acpi, sizeof(struct gicv3_acpi_softc), gicv3_acpi_match, gicv3_acpi_attach, NULL, NULL);
78 1.1 jmcneill
79 1.1 jmcneill static int
80 1.1 jmcneill gicv3_acpi_match(device_t parent, cfdata_t cf, void *aux)
81 1.1 jmcneill {
82 1.1 jmcneill ACPI_SUBTABLE_HEADER *hdrp = aux;
83 1.1 jmcneill ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
84 1.1 jmcneill
85 1.1 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR)
86 1.1 jmcneill return 0;
87 1.1 jmcneill
88 1.1 jmcneill gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)hdrp;
89 1.1 jmcneill
90 1.1 jmcneill switch (gicd->Version) {
91 1.1 jmcneill case ACPI_MADT_GIC_VERSION_NONE:
92 1.1 jmcneill return __SHIFTOUT(reg_id_aa64pfr0_el1_read(), ID_AA64PFR0_EL1_GIC) == 1;
93 1.1 jmcneill case ACPI_MADT_GIC_VERSION_V3:
94 1.1 jmcneill case ACPI_MADT_GIC_VERSION_V4:
95 1.1 jmcneill return 1;
96 1.1 jmcneill default:
97 1.1 jmcneill return 0;
98 1.1 jmcneill }
99 1.1 jmcneill }
100 1.1 jmcneill
101 1.1 jmcneill static void
102 1.1 jmcneill gicv3_acpi_attach(device_t parent, device_t self, void *aux)
103 1.1 jmcneill {
104 1.1 jmcneill struct gicv3_acpi_softc * const sc = device_private(self);
105 1.1 jmcneill ACPI_MADT_GENERIC_DISTRIBUTOR *gicd = aux;
106 1.1 jmcneill int error;
107 1.1 jmcneill
108 1.1 jmcneill sc->sc_gic.sc_dev = self;
109 1.1 jmcneill sc->sc_gic.sc_bst = &arm_generic_bs_tag;
110 1.2 jmcneill sc->sc_gic.sc_dmat = &arm_generic_dma_tag;
111 1.1 jmcneill sc->sc_madt_gicd = gicd;
112 1.1 jmcneill
113 1.1 jmcneill aprint_naive("\n");
114 1.1 jmcneill aprint_normal(": GICv3\n");
115 1.1 jmcneill
116 1.1 jmcneill error = gicv3_acpi_map_dist(sc);
117 1.1 jmcneill if (error) {
118 1.1 jmcneill aprint_error_dev(self, "failed to map distributor: %d\n", error);
119 1.1 jmcneill return;
120 1.1 jmcneill }
121 1.1 jmcneill
122 1.1 jmcneill error = gicv3_acpi_map_redist(sc);
123 1.1 jmcneill if (error) {
124 1.1 jmcneill aprint_error_dev(self, "failed to map redistributor: %d\n", error);
125 1.1 jmcneill return;
126 1.1 jmcneill }
127 1.1 jmcneill
128 1.1 jmcneill error = gicv3_init(&sc->sc_gic);
129 1.1 jmcneill if (error) {
130 1.1 jmcneill aprint_error_dev(self, "failed to initialize GIC: %d\n", error);
131 1.1 jmcneill return;
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.3 jmcneill #if NPCI > 0
135 1.2 jmcneill gicv3_acpi_map_its(sc);
136 1.3 jmcneill #endif
137 1.2 jmcneill
138 1.1 jmcneill arm_fdt_irq_set_handler(gicv3_irq_handler);
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill static int
142 1.1 jmcneill gicv3_acpi_map_dist(struct gicv3_acpi_softc *sc)
143 1.1 jmcneill {
144 1.1 jmcneill const bus_addr_t addr = sc->sc_madt_gicd->BaseAddress;
145 1.1 jmcneill const bus_size_t size = GICD_SIZE;
146 1.1 jmcneill int error;
147 1.1 jmcneill
148 1.1 jmcneill error = bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &sc->sc_gic.sc_bsh_d);
149 1.1 jmcneill if (error)
150 1.1 jmcneill return error;
151 1.1 jmcneill
152 1.1 jmcneill return 0;
153 1.1 jmcneill }
154 1.1 jmcneill
155 1.1 jmcneill static ACPI_STATUS
156 1.1 jmcneill gicv3_acpi_count_gicr(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
157 1.1 jmcneill {
158 1.1 jmcneill ACPI_MADT_GENERIC_REDISTRIBUTOR *gicr;
159 1.1 jmcneill int *count = aux;
160 1.1 jmcneill
161 1.1 jmcneill if (hdrp->Type == ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR) {
162 1.1 jmcneill gicr = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)hdrp;
163 1.1 jmcneill *count += howmany(gicr->Length, GICR_SIZE);
164 1.1 jmcneill }
165 1.1 jmcneill
166 1.1 jmcneill return AE_OK;
167 1.1 jmcneill }
168 1.1 jmcneill
169 1.1 jmcneill static ACPI_STATUS
170 1.1 jmcneill gicv3_acpi_map_gicr(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
171 1.1 jmcneill {
172 1.1 jmcneill struct gicv3_acpi_softc * const sc = aux;
173 1.1 jmcneill ACPI_MADT_GENERIC_REDISTRIBUTOR *gicr;
174 1.1 jmcneill bus_space_handle_t bsh;
175 1.1 jmcneill bus_size_t off;
176 1.1 jmcneill
177 1.1 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR)
178 1.1 jmcneill return AE_OK;
179 1.1 jmcneill
180 1.1 jmcneill gicr = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)hdrp;
181 1.1 jmcneill
182 1.1 jmcneill if (bus_space_map(sc->sc_gic.sc_bst, gicr->BaseAddress, gicr->Length, 0, &bsh) != 0) {
183 1.1 jmcneill aprint_error_dev(sc->sc_gic.sc_dev, "failed to map redistributor at 0x%" PRIx64 " len %#x\n",
184 1.1 jmcneill gicr->BaseAddress, gicr->Length);
185 1.1 jmcneill return AE_OK;
186 1.1 jmcneill }
187 1.1 jmcneill
188 1.1 jmcneill for (off = 0; off < gicr->Length; off += GICR_SIZE) {
189 1.1 jmcneill const int redist = sc->sc_gic.sc_bsh_r_count;
190 1.1 jmcneill if (bus_space_subregion(sc->sc_gic.sc_bst, bsh, off, GICR_SIZE, &sc->sc_gic.sc_bsh_r[redist]) != 0) {
191 1.1 jmcneill aprint_error_dev(sc->sc_gic.sc_dev, "couldn't subregion redistributor registers\n");
192 1.1 jmcneill return AE_OK;
193 1.1 jmcneill }
194 1.1 jmcneill
195 1.1 jmcneill aprint_debug_dev(sc->sc_gic.sc_dev, "redist at 0x%" PRIx64 " [GICR]\n", gicr->BaseAddress + off);
196 1.1 jmcneill
197 1.1 jmcneill sc->sc_gic.sc_bsh_r_count++;
198 1.1 jmcneill
199 1.1 jmcneill /* If this is the last redist in this region, skip to the next one */
200 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);
201 1.1 jmcneill if (typer & GICR_TYPER_Last)
202 1.1 jmcneill break;
203 1.3.8.1 martin
204 1.3.8.1 martin /* If the redistributor supports virtual LPIs, skip the VLPI register region */
205 1.3.8.1 martin if (typer & GICR_TYPER_VLPIS)
206 1.3.8.1 martin off += GICR_SIZE;
207 1.1 jmcneill }
208 1.1 jmcneill
209 1.1 jmcneill return AE_OK;
210 1.1 jmcneill }
211 1.1 jmcneill
212 1.1 jmcneill static ACPI_STATUS
213 1.1 jmcneill gicv3_acpi_count_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
214 1.1 jmcneill {
215 1.1 jmcneill ACPI_MADT_GENERIC_INTERRUPT *gicc;
216 1.1 jmcneill int *count = aux;
217 1.1 jmcneill
218 1.1 jmcneill if (hdrp->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
219 1.1 jmcneill gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
220 1.1 jmcneill if ((gicc->Flags & ACPI_MADT_ENABLED) != 0)
221 1.1 jmcneill (*count)++;
222 1.1 jmcneill }
223 1.1 jmcneill
224 1.1 jmcneill return AE_OK;
225 1.1 jmcneill }
226 1.1 jmcneill
227 1.1 jmcneill static ACPI_STATUS
228 1.1 jmcneill gicv3_acpi_map_gicc(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
229 1.1 jmcneill {
230 1.1 jmcneill struct gicv3_acpi_softc * const sc = aux;
231 1.1 jmcneill ACPI_MADT_GENERIC_INTERRUPT *gicc;
232 1.1 jmcneill
233 1.1 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
234 1.1 jmcneill return AE_OK;
235 1.1 jmcneill
236 1.1 jmcneill gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
237 1.1 jmcneill if ((gicc->Flags & ACPI_MADT_ENABLED) == 0)
238 1.1 jmcneill return AE_OK;
239 1.1 jmcneill
240 1.1 jmcneill const int redist = sc->sc_gic.sc_bsh_r_count;
241 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) {
242 1.1 jmcneill aprint_error_dev(sc->sc_gic.sc_dev, "failed to map redistributor at 0x%" PRIx64 " len %#x\n",
243 1.1 jmcneill gicc->GicrBaseAddress, GICR_SIZE);
244 1.1 jmcneill return AE_OK;
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.1 jmcneill aprint_debug_dev(sc->sc_gic.sc_dev, "redist at 0x%" PRIx64 " [GICC]\n", gicc->GicrBaseAddress);
248 1.1 jmcneill
249 1.1 jmcneill sc->sc_gic.sc_bsh_r_count++;
250 1.1 jmcneill
251 1.1 jmcneill return AE_OK;
252 1.1 jmcneill }
253 1.1 jmcneill
254 1.1 jmcneill static int
255 1.1 jmcneill gicv3_acpi_map_redist(struct gicv3_acpi_softc *sc)
256 1.1 jmcneill {
257 1.1 jmcneill bool use_gicr = false;
258 1.1 jmcneill int max_redist = 0;
259 1.1 jmcneill
260 1.1 jmcneill /*
261 1.1 jmcneill * Try to use GICR structures to describe redistributors. If no GICR
262 1.1 jmcneill * subtables are found, use the GICR address from the GICC subtables.
263 1.1 jmcneill */
264 1.1 jmcneill acpi_madt_walk(gicv3_acpi_count_gicr, &max_redist);
265 1.1 jmcneill if (max_redist != 0)
266 1.1 jmcneill use_gicr = true;
267 1.1 jmcneill else
268 1.1 jmcneill acpi_madt_walk(gicv3_acpi_count_gicc, &max_redist);
269 1.1 jmcneill
270 1.1 jmcneill if (max_redist == 0)
271 1.1 jmcneill return ENODEV;
272 1.1 jmcneill
273 1.1 jmcneill sc->sc_gic.sc_bsh_r = kmem_alloc(sizeof(bus_space_handle_t) * max_redist, KM_SLEEP);
274 1.1 jmcneill if (use_gicr)
275 1.1 jmcneill acpi_madt_walk(gicv3_acpi_map_gicr, sc);
276 1.1 jmcneill else
277 1.1 jmcneill acpi_madt_walk(gicv3_acpi_map_gicc, sc);
278 1.1 jmcneill
279 1.1 jmcneill if (sc->sc_gic.sc_bsh_r_count == 0)
280 1.1 jmcneill return ENXIO;
281 1.1 jmcneill
282 1.1 jmcneill return 0;
283 1.1 jmcneill }
284 1.2 jmcneill
285 1.3 jmcneill #if NPCI > 0
286 1.2 jmcneill static ACPI_STATUS
287 1.2 jmcneill gicv3_acpi_map_gits(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
288 1.2 jmcneill {
289 1.2 jmcneill struct gicv3_acpi_softc * const sc = aux;
290 1.2 jmcneill ACPI_MADT_GENERIC_TRANSLATOR *gits;
291 1.2 jmcneill bus_space_handle_t bsh;
292 1.2 jmcneill
293 1.2 jmcneill if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_TRANSLATOR)
294 1.2 jmcneill return AE_OK;
295 1.2 jmcneill
296 1.2 jmcneill gits = (ACPI_MADT_GENERIC_TRANSLATOR *)hdrp;
297 1.2 jmcneill
298 1.2 jmcneill if (bus_space_map(sc->sc_gic.sc_bst, gits->BaseAddress, GITS_SIZE, 0, &bsh) != 0) {
299 1.2 jmcneill aprint_error_dev(sc->sc_gic.sc_dev, "failed to map ITS at 0x%" PRIx64 " len %#x\n",
300 1.2 jmcneill gits->BaseAddress, GITS_SIZE);
301 1.2 jmcneill return AE_OK;
302 1.2 jmcneill }
303 1.2 jmcneill
304 1.2 jmcneill aprint_normal_dev(sc->sc_gic.sc_dev, "ITS #%#x at 0x%" PRIx64 "\n", gits->TranslationId, gits->BaseAddress);
305 1.2 jmcneill
306 1.2 jmcneill gicv3_its_init(&sc->sc_gic, bsh, gits->BaseAddress, gits->TranslationId);
307 1.2 jmcneill
308 1.2 jmcneill return AE_OK;
309 1.2 jmcneill }
310 1.2 jmcneill
311 1.2 jmcneill static int
312 1.2 jmcneill gicv3_acpi_map_its(struct gicv3_acpi_softc *sc)
313 1.2 jmcneill {
314 1.2 jmcneill acpi_madt_walk(gicv3_acpi_map_gits, sc);
315 1.2 jmcneill
316 1.2 jmcneill return 0;
317 1.2 jmcneill }
318 1.3 jmcneill #endif
319