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