2 * Copyright (c) 2016 The FreeBSD Foundation
3 * Copyright (c) 2022 Arm Ltd
5 * This software was developed by Andrew Turner under
6 * the sponsorship of the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
40 #include <machine/intr.h>
41 #include <machine/resource.h>
43 #include <contrib/dev/acpica/include/acpi.h>
44 #include <dev/acpica/acpivar.h>
46 #include "gic_v3_reg.h"
47 #include "gic_v3_var.h"
49 #define GICV3_PRIV_VGIC 0x80000000
50 #define GICV3_PRIV_FLAGS 0x80000000
51 #define HV_MSI_SPI_START 64
52 #define HV_MSI_SPI_LAST 0
54 struct gic_v3_acpi_devinfo {
55 struct gic_v3_devinfo di_gic_dinfo;
56 struct resource_list di_rl;
59 static device_identify_t gic_v3_acpi_identify;
60 static device_probe_t gic_v3_acpi_probe;
61 static device_attach_t gic_v3_acpi_attach;
62 static bus_get_resource_list_t gic_v3_acpi_get_resource_list;
64 static void gic_v3_acpi_bus_attach(device_t);
66 static device_method_t gic_v3_acpi_methods[] = {
67 /* Device interface */
68 DEVMETHOD(device_identify, gic_v3_acpi_identify),
69 DEVMETHOD(device_probe, gic_v3_acpi_probe),
70 DEVMETHOD(device_attach, gic_v3_acpi_attach),
73 DEVMETHOD(bus_get_resource_list, gic_v3_acpi_get_resource_list),
79 DEFINE_CLASS_1(gic, gic_v3_acpi_driver, gic_v3_acpi_methods,
80 sizeof(struct gic_v3_softc), gic_v3_driver);
82 EARLY_DRIVER_MODULE(gic_v3, acpi, gic_v3_acpi_driver, 0, 0,
83 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
85 struct madt_table_data {
88 ACPI_MADT_GENERIC_DISTRIBUTOR *dist;
95 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
97 struct madt_table_data *madt_data;
99 madt_data = (struct madt_table_data *)arg;
101 switch(entry->Type) {
102 case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
103 if (madt_data->dist != NULL) {
105 device_printf(madt_data->parent,
106 "gic: Already have a distributor table");
109 madt_data->dist = (ACPI_MADT_GENERIC_DISTRIBUTOR *)entry;
112 case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
121 rdist_map(ACPI_SUBTABLE_HEADER *entry, void *arg)
123 ACPI_MADT_GENERIC_REDISTRIBUTOR *redist;
124 ACPI_MADT_GENERIC_INTERRUPT *intr;
125 struct madt_table_data *madt_data;
128 madt_data = (struct madt_table_data *)arg;
130 switch(entry->Type) {
131 case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
132 if (madt_data->rdist_use_gicc)
134 redist = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)entry;
137 BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
138 SYS_RES_MEMORY, madt_data->count, redist->BaseAddress,
142 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
143 if (!madt_data->rdist_use_gicc)
146 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
150 * Map the two 64k redistributor frames.
152 count = GICR_RD_BASE_SIZE + GICR_SGI_BASE_SIZE;
153 if (madt_data->dist->Version == ACPI_MADT_GIC_VERSION_V4)
154 count += GICR_VLPI_BASE_SIZE + GICR_RESERVED_SIZE;
155 BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
156 SYS_RES_MEMORY, madt_data->count, intr->GicrBaseAddress,
158 if (intr->VgicInterrupt == 0)
159 madt_data->have_vgic = false;
167 gic_v3_acpi_identify(driver_t *driver, device_t parent)
169 struct madt_table_data madt_data;
170 ACPI_TABLE_MADT *madt;
175 physaddr = acpi_find_table(ACPI_SIG_MADT);
179 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
181 device_printf(parent, "gic: Unable to map the MADT\n");
185 madt_data.parent = parent;
186 madt_data.dist = NULL;
189 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
190 madt_handler, &madt_data);
191 if (madt_data.dist == NULL) {
192 device_printf(parent,
193 "No gic interrupt or distributor table\n");
197 /* Check the GIC version is supported by thiss driver */
198 switch(madt_data.dist->Version) {
199 case ACPI_MADT_GIC_VERSION_V3:
200 case ACPI_MADT_GIC_VERSION_V4:
206 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE,
209 device_printf(parent, "add gic child failed\n");
213 /* Add the MADT data */
214 BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
215 madt_data.dist->BaseAddress, GICD_SIZE);
218 madt_data.rdist_use_gicc = false;
219 madt_data.have_vgic = true;
220 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
221 rdist_map, &madt_data);
222 if (madt_data.count == 0) {
224 * No redistributors found, fall back to use the GICR
225 * address from the GICC sub-table.
227 madt_data.rdist_use_gicc = true;
228 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
229 rdist_map, &madt_data);
232 private = madt_data.dist->Version;
233 /* Flag that the VGIC is in use */
234 if (madt_data.have_vgic)
235 private |= GICV3_PRIV_VGIC;
237 acpi_set_private(dev, (void *)private);
240 acpi_unmap_table(madt);
244 gic_v3_acpi_probe(device_t dev)
247 switch((uintptr_t)acpi_get_private(dev) & ~GICV3_PRIV_FLAGS) {
248 case ACPI_MADT_GIC_VERSION_V3:
249 case ACPI_MADT_GIC_VERSION_V4:
255 device_set_desc(dev, GIC_V3_DEVSTR);
256 return (BUS_PROBE_NOWILDCARD);
260 madt_count_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg)
262 struct gic_v3_softc *sc = arg;
264 if (entry->Type == ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR)
265 sc->gic_redists.nregions++;
269 madt_count_gicc_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg)
271 struct gic_v3_softc *sc = arg;
273 if (entry->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
274 sc->gic_redists.nregions++;
278 gic_v3_acpi_count_regions(device_t dev)
280 struct gic_v3_softc *sc;
281 ACPI_TABLE_MADT *madt;
284 sc = device_get_softc(dev);
286 physaddr = acpi_find_table(ACPI_SIG_MADT);
290 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
292 device_printf(dev, "Unable to map the MADT\n");
296 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
297 madt_count_redistrib, sc);
298 /* Fall back to use the distributor GICR base address */
299 if (sc->gic_redists.nregions == 0) {
300 acpi_walk_subtables(madt + 1,
301 (char *)madt + madt->Header.Length,
302 madt_count_gicc_redistrib, sc);
304 acpi_unmap_table(madt);
306 return (sc->gic_redists.nregions > 0 ? 0 : ENXIO);
310 gic_v3_acpi_attach(device_t dev)
312 struct gic_v3_softc *sc;
315 sc = device_get_softc(dev);
317 sc->gic_bus = GIC_BUS_ACPI;
319 err = gic_v3_acpi_count_regions(dev);
322 if (vm_guest == VM_GUEST_HV) {
323 sc->gic_mbi_start = HV_MSI_SPI_START;
324 sc->gic_mbi_end = HV_MSI_SPI_LAST;
326 err = gic_v3_attach(dev);
330 sc->gic_pic = intr_pic_register(dev, ACPI_INTR_XREF);
331 if (sc->gic_pic == NULL) {
332 device_printf(dev, "could not register PIC\n");
337 * Registering for MSI with SPI range, as this is
338 * required for Hyper-V GIC to work in ARM64.
340 if (vm_guest == VM_GUEST_HV) {
341 err = intr_msi_register(dev, ACPI_MSI_XREF);
343 device_printf(dev, "could not register MSI\n");
348 if (intr_pic_claim_root(dev, ACPI_INTR_XREF, arm_gic_v3_intr, sc)
355 err = intr_ipi_pic_register(dev, 0);
357 device_printf(dev, "could not register for IPIs\n");
363 * Try to register the ITS driver to this GIC. The GIC will act as
364 * a bus in that case. Failure here will not affect the main GIC
367 gic_v3_acpi_bus_attach(dev);
369 if (device_get_children(dev, &sc->gic_children, &sc->gic_nchildren) !=0)
370 sc->gic_nchildren = 0;
375 /* Failure so free resources */
380 "Failed to attach. Error %d\n", err);
387 gic_v3_add_children(ACPI_SUBTABLE_HEADER *entry, void *arg)
389 ACPI_MADT_GENERIC_TRANSLATOR *gict;
390 struct gic_v3_acpi_devinfo *di;
391 struct gic_v3_softc *sc;
396 if (entry->Type == ACPI_MADT_TYPE_GENERIC_TRANSLATOR) {
397 /* We have an ITS, add it as a child */
398 gict = (ACPI_MADT_GENERIC_TRANSLATOR *)entry;
400 sc = device_get_softc(dev);
402 di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
403 err = acpi_iort_its_lookup(gict->TranslationId, &xref, &pxm);
409 child = device_add_child(dev, "its", -1);
415 di->di_gic_dinfo.gic_domain = pxm;
416 di->di_gic_dinfo.msi_xref = xref;
417 resource_list_init(&di->di_rl);
418 resource_list_add(&di->di_rl, SYS_RES_MEMORY, 0,
419 gict->BaseAddress, gict->BaseAddress + 128 * 1024 - 1,
422 device_set_ivars(child, di);
427 gic_v3_acpi_bus_attach(device_t dev)
429 struct gic_v3_acpi_devinfo *di;
430 struct gic_v3_softc *sc;
431 ACPI_TABLE_MADT *madt;
435 sc = device_get_softc(dev);
437 physaddr = acpi_find_table(ACPI_SIG_MADT);
441 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
443 device_printf(dev, "Unable to map the MADT to add children\n");
447 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
448 gic_v3_add_children, dev);
449 /* Add the vgic child if needed */
450 if (((uintptr_t)acpi_get_private(dev) & GICV3_PRIV_FLAGS) != 0) {
451 child = device_add_child(dev, "vgic", -1);
453 device_printf(dev, "Could not add vgic child\n");
455 di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
456 resource_list_init(&di->di_rl);
457 di->di_gic_dinfo.gic_domain = -1;
458 di->di_gic_dinfo.is_vgic = 1;
459 device_set_ivars(child, di);
464 acpi_unmap_table(madt);
466 bus_generic_attach(dev);
469 static struct resource_list *
470 gic_v3_acpi_get_resource_list(device_t bus, device_t child)
472 struct gic_v3_acpi_devinfo *di;
474 di = device_get_ivars(child);
475 KASSERT(di != NULL, ("%s: No devinfo", __func__));