1 /*-
2 * Copyright (c) 2016 The FreeBSD Foundation
3 * Copyright (c) 2022 Arm Ltd
4 *
5 * This software was developed by Andrew Turner under
6 * the sponsorship of the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include "opt_acpi.h"
31
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/rman.h>
40
41 #include <machine/intr.h>
42 #include <machine/resource.h>
43
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <dev/acpica/acpivar.h>
46
47 #include "gic_v3_reg.h"
48 #include "gic_v3_var.h"
49
50 #define GICV3_PRIV_VGIC 0x80000000
51 #define GICV3_PRIV_FLAGS 0x80000000
52 #define HV_MSI_SPI_START 64
53 #define HV_MSI_SPI_LAST 0
54
55 struct gic_v3_acpi_devinfo {
56 struct gic_v3_devinfo di_gic_dinfo;
57 struct resource_list di_rl;
58 };
59
60 static device_identify_t gic_v3_acpi_identify;
61 static device_probe_t gic_v3_acpi_probe;
62 static device_attach_t gic_v3_acpi_attach;
63 static bus_get_resource_list_t gic_v3_acpi_get_resource_list;
64
65 static void gic_v3_acpi_bus_attach(device_t);
66
67 static device_method_t gic_v3_acpi_methods[] = {
68 /* Device interface */
69 DEVMETHOD(device_identify, gic_v3_acpi_identify),
70 DEVMETHOD(device_probe, gic_v3_acpi_probe),
71 DEVMETHOD(device_attach, gic_v3_acpi_attach),
72
73 /* Bus interface */
74 DEVMETHOD(bus_get_resource_list, gic_v3_acpi_get_resource_list),
75
76 /* End */
77 DEVMETHOD_END
78 };
79
80 DEFINE_CLASS_1(gic, gic_v3_acpi_driver, gic_v3_acpi_methods,
81 sizeof(struct gic_v3_softc), gic_v3_driver);
82
83 EARLY_DRIVER_MODULE(gic_v3, acpi, gic_v3_acpi_driver, 0, 0,
84 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
85
86 struct madt_table_data {
87 device_t parent;
88 device_t dev;
89 ACPI_MADT_GENERIC_DISTRIBUTOR *dist;
90 int count;
91 bool rdist_use_gicc;
92 bool have_vgic;
93 };
94
95 static void
madt_handler(ACPI_SUBTABLE_HEADER * entry,void * arg)96 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
97 {
98 struct madt_table_data *madt_data;
99
100 madt_data = (struct madt_table_data *)arg;
101
102 switch(entry->Type) {
103 case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
104 if (madt_data->dist != NULL) {
105 if (bootverbose)
106 device_printf(madt_data->parent,
107 "gic: Already have a distributor table");
108 break;
109 }
110 madt_data->dist = (ACPI_MADT_GENERIC_DISTRIBUTOR *)entry;
111 break;
112
113 case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
114 break;
115
116 default:
117 break;
118 }
119 }
120
121 static void
rdist_map(ACPI_SUBTABLE_HEADER * entry,void * arg)122 rdist_map(ACPI_SUBTABLE_HEADER *entry, void *arg)
123 {
124 ACPI_MADT_GENERIC_REDISTRIBUTOR *redist;
125 ACPI_MADT_GENERIC_INTERRUPT *intr;
126 struct madt_table_data *madt_data;
127 rman_res_t count;
128
129 madt_data = (struct madt_table_data *)arg;
130
131 switch(entry->Type) {
132 case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
133 if (madt_data->rdist_use_gicc)
134 break;
135 redist = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)entry;
136
137 madt_data->count++;
138 BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
139 SYS_RES_MEMORY, madt_data->count, redist->BaseAddress,
140 redist->Length);
141 break;
142
143 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
144 if (!madt_data->rdist_use_gicc)
145 break;
146
147 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
148
149 madt_data->count++;
150 /*
151 * Map the two 64k redistributor frames.
152 */
153 count = GICR_RD_BASE_SIZE + GICR_SGI_BASE_SIZE;
154 if (madt_data->dist->Version == ACPI_MADT_GIC_VERSION_V4)
155 count += GICR_VLPI_BASE_SIZE + GICR_RESERVED_SIZE;
156 BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
157 SYS_RES_MEMORY, madt_data->count, intr->GicrBaseAddress,
158 count);
159 if (intr->VgicInterrupt == 0)
160 madt_data->have_vgic = false;
161
162 default:
163 break;
164 }
165 }
166
167 static void
gic_v3_acpi_identify(driver_t * driver,device_t parent)168 gic_v3_acpi_identify(driver_t *driver, device_t parent)
169 {
170 struct madt_table_data madt_data;
171 ACPI_TABLE_MADT *madt;
172 vm_paddr_t physaddr;
173 uintptr_t private;
174 device_t dev;
175
176 physaddr = acpi_find_table(ACPI_SIG_MADT);
177 if (physaddr == 0)
178 return;
179
180 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
181 if (madt == NULL) {
182 device_printf(parent, "gic: Unable to map the MADT\n");
183 return;
184 }
185
186 madt_data.parent = parent;
187 madt_data.dist = NULL;
188 madt_data.count = 0;
189
190 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
191 madt_handler, &madt_data);
192 if (madt_data.dist == NULL) {
193 device_printf(parent,
194 "No gic interrupt or distributor table\n");
195 goto out;
196 }
197
198 /* Check the GIC version is supported by thiss driver */
199 switch(madt_data.dist->Version) {
200 case ACPI_MADT_GIC_VERSION_V3:
201 case ACPI_MADT_GIC_VERSION_V4:
202 break;
203 default:
204 goto out;
205 }
206
207 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE,
208 "gic", -1);
209 if (dev == NULL) {
210 device_printf(parent, "add gic child failed\n");
211 goto out;
212 }
213
214 /* Add the MADT data */
215 BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
216 madt_data.dist->BaseAddress, GICD_SIZE);
217
218 madt_data.dev = dev;
219 madt_data.rdist_use_gicc = false;
220 madt_data.have_vgic = true;
221 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
222 rdist_map, &madt_data);
223 if (madt_data.count == 0) {
224 /*
225 * No redistributors found, fall back to use the GICR
226 * address from the GICC sub-table.
227 */
228 madt_data.rdist_use_gicc = true;
229 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
230 rdist_map, &madt_data);
231 }
232
233 private = madt_data.dist->Version;
234 /* Flag that the VGIC is in use */
235 if (madt_data.have_vgic)
236 private |= GICV3_PRIV_VGIC;
237
238 acpi_set_private(dev, (void *)private);
239
240 out:
241 acpi_unmap_table(madt);
242 }
243
244 static int
gic_v3_acpi_probe(device_t dev)245 gic_v3_acpi_probe(device_t dev)
246 {
247
248 switch((uintptr_t)acpi_get_private(dev) & ~GICV3_PRIV_FLAGS) {
249 case ACPI_MADT_GIC_VERSION_V3:
250 case ACPI_MADT_GIC_VERSION_V4:
251 break;
252 default:
253 return (ENXIO);
254 }
255
256 device_set_desc(dev, GIC_V3_DEVSTR);
257 return (BUS_PROBE_NOWILDCARD);
258 }
259
260 static void
madt_count_redistrib(ACPI_SUBTABLE_HEADER * entry,void * arg)261 madt_count_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg)
262 {
263 struct gic_v3_softc *sc = arg;
264
265 if (entry->Type == ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR)
266 sc->gic_redists.nregions++;
267 }
268
269 static void
madt_count_gicc_redistrib(ACPI_SUBTABLE_HEADER * entry,void * arg)270 madt_count_gicc_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg)
271 {
272 struct gic_v3_softc *sc = arg;
273
274 if (entry->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
275 sc->gic_redists.nregions++;
276 }
277
278 static int
gic_v3_acpi_count_regions(device_t dev)279 gic_v3_acpi_count_regions(device_t dev)
280 {
281 struct gic_v3_softc *sc;
282 ACPI_TABLE_MADT *madt;
283 vm_paddr_t physaddr;
284
285 sc = device_get_softc(dev);
286
287 physaddr = acpi_find_table(ACPI_SIG_MADT);
288 if (physaddr == 0)
289 return (ENXIO);
290
291 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
292 if (madt == NULL) {
293 device_printf(dev, "Unable to map the MADT\n");
294 return (ENXIO);
295 }
296
297 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
298 madt_count_redistrib, sc);
299 /* Fall back to use the distributor GICR base address */
300 if (sc->gic_redists.nregions == 0) {
301 acpi_walk_subtables(madt + 1,
302 (char *)madt + madt->Header.Length,
303 madt_count_gicc_redistrib, sc);
304 }
305 acpi_unmap_table(madt);
306
307 return (sc->gic_redists.nregions > 0 ? 0 : ENXIO);
308 }
309
310 static int
gic_v3_acpi_attach(device_t dev)311 gic_v3_acpi_attach(device_t dev)
312 {
313 struct gic_v3_softc *sc;
314 int err;
315
316 sc = device_get_softc(dev);
317 sc->dev = dev;
318 sc->gic_bus = GIC_BUS_ACPI;
319
320 err = gic_v3_acpi_count_regions(dev);
321 if (err != 0)
322 goto count_error;
323 if (vm_guest == VM_GUEST_HV) {
324 sc->gic_mbi_start = HV_MSI_SPI_START;
325 sc->gic_mbi_end = HV_MSI_SPI_LAST;
326 }
327 err = gic_v3_attach(dev);
328 if (err != 0)
329 goto error;
330
331 sc->gic_pic = intr_pic_register(dev, ACPI_INTR_XREF);
332 if (sc->gic_pic == NULL) {
333 device_printf(dev, "could not register PIC\n");
334 err = ENXIO;
335 goto error;
336 }
337 /*
338 * Registering for MSI with SPI range, as this is
339 * required for Hyper-V GIC to work in ARM64.
340 */
341 if (vm_guest == VM_GUEST_HV) {
342 err = intr_msi_register(dev, ACPI_MSI_XREF);
343 if (err) {
344 device_printf(dev, "could not register MSI\n");
345 goto error;
346 }
347 }
348
349 if (intr_pic_claim_root(dev, ACPI_INTR_XREF, arm_gic_v3_intr, sc)
350 != 0) {
351 err = ENXIO;
352 goto error;
353 }
354
355 #ifdef SMP
356 err = intr_ipi_pic_register(dev, 0);
357 if (err != 0) {
358 device_printf(dev, "could not register for IPIs\n");
359 goto error;
360 }
361 #endif
362
363 /*
364 * Try to register the ITS driver to this GIC. The GIC will act as
365 * a bus in that case. Failure here will not affect the main GIC
366 * functionality.
367 */
368 gic_v3_acpi_bus_attach(dev);
369
370 if (device_get_children(dev, &sc->gic_children, &sc->gic_nchildren) !=0)
371 sc->gic_nchildren = 0;
372
373 return (0);
374
375 error:
376 /* Failure so free resources */
377 gic_v3_detach(dev);
378 count_error:
379 if (bootverbose) {
380 device_printf(dev,
381 "Failed to attach. Error %d\n", err);
382 }
383
384 return (err);
385 }
386
387 static void
gic_v3_add_children(ACPI_SUBTABLE_HEADER * entry,void * arg)388 gic_v3_add_children(ACPI_SUBTABLE_HEADER *entry, void *arg)
389 {
390 ACPI_MADT_GENERIC_TRANSLATOR *gict;
391 struct gic_v3_acpi_devinfo *di;
392 struct gic_v3_softc *sc;
393 device_t child, dev;
394 u_int xref;
395 int err, pxm;
396
397 if (entry->Type == ACPI_MADT_TYPE_GENERIC_TRANSLATOR) {
398 /* We have an ITS, add it as a child */
399 gict = (ACPI_MADT_GENERIC_TRANSLATOR *)entry;
400 dev = arg;
401 sc = device_get_softc(dev);
402
403 di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
404 err = acpi_iort_its_lookup(gict->TranslationId, &xref, &pxm);
405 if (err != 0) {
406 free(di, M_GIC_V3);
407 return;
408 }
409
410 child = device_add_child(dev, "its", -1);
411 if (child == NULL) {
412 free(di, M_GIC_V3);
413 return;
414 }
415
416 di->di_gic_dinfo.gic_domain = pxm;
417 di->di_gic_dinfo.msi_xref = xref;
418 resource_list_init(&di->di_rl);
419 resource_list_add(&di->di_rl, SYS_RES_MEMORY, 0,
420 gict->BaseAddress, gict->BaseAddress + 128 * 1024 - 1,
421 128 * 1024);
422 sc->gic_nchildren++;
423 device_set_ivars(child, di);
424 }
425 }
426
427 static void
gic_v3_acpi_bus_attach(device_t dev)428 gic_v3_acpi_bus_attach(device_t dev)
429 {
430 struct gic_v3_acpi_devinfo *di;
431 struct gic_v3_softc *sc;
432 ACPI_TABLE_MADT *madt;
433 device_t child;
434 vm_paddr_t physaddr;
435
436 sc = device_get_softc(dev);
437
438 physaddr = acpi_find_table(ACPI_SIG_MADT);
439 if (physaddr == 0)
440 return;
441
442 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
443 if (madt == NULL) {
444 device_printf(dev, "Unable to map the MADT to add children\n");
445 return;
446 }
447
448 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
449 gic_v3_add_children, dev);
450 /* Add the vgic child if needed */
451 if (((uintptr_t)acpi_get_private(dev) & GICV3_PRIV_FLAGS) != 0) {
452 child = device_add_child(dev, "vgic", -1);
453 if (child == NULL) {
454 device_printf(dev, "Could not add vgic child\n");
455 } else {
456 di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
457 resource_list_init(&di->di_rl);
458 di->di_gic_dinfo.gic_domain = -1;
459 di->di_gic_dinfo.is_vgic = 1;
460 device_set_ivars(child, di);
461 sc->gic_nchildren++;
462 }
463 }
464
465 acpi_unmap_table(madt);
466
467 bus_generic_attach(dev);
468 }
469
470 static struct resource_list *
gic_v3_acpi_get_resource_list(device_t bus,device_t child)471 gic_v3_acpi_get_resource_list(device_t bus, device_t child)
472 {
473 struct gic_v3_acpi_devinfo *di;
474
475 di = device_get_ivars(child);
476 KASSERT(di != NULL, ("%s: No devinfo", __func__));
477
478 return (&di->di_rl);
479 }
480