2 * Copyright (c) 2016 The FreeBSD Foundation
4 * This software was developed by Andrew Turner under
5 * the sponsorship of the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
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 struct gic_v3_acpi_devinfo {
50 struct gic_v3_devinfo di_gic_dinfo;
51 struct resource_list di_rl;
54 static device_identify_t gic_v3_acpi_identify;
55 static device_probe_t gic_v3_acpi_probe;
56 static device_attach_t gic_v3_acpi_attach;
57 static bus_alloc_resource_t gic_v3_acpi_bus_alloc_res;
58 static bus_get_resource_list_t gic_v3_acpi_get_resource_list;
60 static void gic_v3_acpi_bus_attach(device_t);
62 static device_method_t gic_v3_acpi_methods[] = {
63 /* Device interface */
64 DEVMETHOD(device_identify, gic_v3_acpi_identify),
65 DEVMETHOD(device_probe, gic_v3_acpi_probe),
66 DEVMETHOD(device_attach, gic_v3_acpi_attach),
69 DEVMETHOD(bus_alloc_resource, gic_v3_acpi_bus_alloc_res),
70 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
71 DEVMETHOD(bus_get_resource_list, gic_v3_acpi_get_resource_list),
77 DEFINE_CLASS_1(gic, gic_v3_acpi_driver, gic_v3_acpi_methods,
78 sizeof(struct gic_v3_softc), gic_v3_driver);
80 static devclass_t gic_v3_acpi_devclass;
82 EARLY_DRIVER_MODULE(gic_v3, acpi, gic_v3_acpi_driver, gic_v3_acpi_devclass,
83 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
85 struct madt_table_data {
88 ACPI_MADT_GENERIC_DISTRIBUTOR *dist;
94 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
96 struct madt_table_data *madt_data;
98 madt_data = (struct madt_table_data *)arg;
100 switch(entry->Type) {
101 case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
102 if (madt_data->dist != NULL) {
104 device_printf(madt_data->parent,
105 "gic: Already have a distributor table");
108 madt_data->dist = (ACPI_MADT_GENERIC_DISTRIBUTOR *)entry;
111 case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
120 rdist_map(ACPI_SUBTABLE_HEADER *entry, void *arg)
122 ACPI_MADT_GENERIC_REDISTRIBUTOR *redist;
123 ACPI_MADT_GENERIC_INTERRUPT *intr;
124 struct madt_table_data *madt_data;
127 madt_data = (struct madt_table_data *)arg;
129 switch(entry->Type) {
130 case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
131 if (madt_data->rdist_use_gicc)
133 redist = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)entry;
136 BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
137 SYS_RES_MEMORY, madt_data->count, redist->BaseAddress,
141 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
142 if (!madt_data->rdist_use_gicc)
145 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
149 * Map the two 64k redistributor frames.
151 count = GICR_RD_BASE_SIZE + GICR_SGI_BASE_SIZE;
152 if (madt_data->dist->Version == ACPI_MADT_GIC_VERSION_V4)
153 count += GICR_VLPI_BASE_SIZE + GICR_RESERVED_SIZE;
154 BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
155 SYS_RES_MEMORY, madt_data->count, intr->GicrBaseAddress,
164 gic_v3_acpi_identify(driver_t *driver, device_t parent)
166 struct madt_table_data madt_data;
167 ACPI_TABLE_MADT *madt;
171 physaddr = acpi_find_table(ACPI_SIG_MADT);
175 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
177 device_printf(parent, "gic: Unable to map the MADT\n");
181 madt_data.parent = parent;
182 madt_data.dist = NULL;
185 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
186 madt_handler, &madt_data);
187 if (madt_data.dist == NULL) {
188 device_printf(parent,
189 "No gic interrupt or distributor table\n");
193 /* Check the GIC version is supported by thiss driver */
194 switch(madt_data.dist->Version) {
195 case ACPI_MADT_GIC_VERSION_V3:
196 case ACPI_MADT_GIC_VERSION_V4:
202 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE,
205 device_printf(parent, "add gic child failed\n");
209 /* Add the MADT data */
210 BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
211 madt_data.dist->BaseAddress, GICD_SIZE);
214 madt_data.rdist_use_gicc = false;
215 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
216 rdist_map, &madt_data);
217 if (madt_data.count == 0) {
219 * No redistributors found, fall back to use the GICR
220 * address from the GICC sub-table.
222 madt_data.rdist_use_gicc = true;
223 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
224 rdist_map, &madt_data);
227 acpi_set_private(dev, (void *)(uintptr_t)madt_data.dist->Version);
230 acpi_unmap_table(madt);
234 gic_v3_acpi_probe(device_t dev)
237 switch((uintptr_t)acpi_get_private(dev)) {
238 case ACPI_MADT_GIC_VERSION_V3:
239 case ACPI_MADT_GIC_VERSION_V4:
245 device_set_desc(dev, GIC_V3_DEVSTR);
246 return (BUS_PROBE_NOWILDCARD);
250 madt_count_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg)
252 struct gic_v3_softc *sc = arg;
254 if (entry->Type == ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR)
255 sc->gic_redists.nregions++;
259 madt_count_gicc_redistrib(ACPI_SUBTABLE_HEADER *entry, void *arg)
261 struct gic_v3_softc *sc = arg;
263 if (entry->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
264 sc->gic_redists.nregions++;
268 gic_v3_acpi_count_regions(device_t dev)
270 struct gic_v3_softc *sc;
271 ACPI_TABLE_MADT *madt;
274 sc = device_get_softc(dev);
276 physaddr = acpi_find_table(ACPI_SIG_MADT);
280 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
282 device_printf(dev, "Unable to map the MADT\n");
286 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
287 madt_count_redistrib, sc);
288 /* Fall back to use the distributor GICR base address */
289 if (sc->gic_redists.nregions == 0) {
290 acpi_walk_subtables(madt + 1,
291 (char *)madt + madt->Header.Length,
292 madt_count_gicc_redistrib, sc);
294 acpi_unmap_table(madt);
296 return (sc->gic_redists.nregions > 0 ? 0 : ENXIO);
300 gic_v3_acpi_attach(device_t dev)
302 struct gic_v3_softc *sc;
305 sc = device_get_softc(dev);
307 sc->gic_bus = GIC_BUS_ACPI;
309 err = gic_v3_acpi_count_regions(dev);
313 err = gic_v3_attach(dev);
317 sc->gic_pic = intr_pic_register(dev, ACPI_INTR_XREF);
318 if (sc->gic_pic == NULL) {
319 device_printf(dev, "could not register PIC\n");
324 if (intr_pic_claim_root(dev, ACPI_INTR_XREF, arm_gic_v3_intr, sc,
325 GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
331 * Try to register the ITS driver to this GIC. The GIC will act as
332 * a bus in that case. Failure here will not affect the main GIC
335 gic_v3_acpi_bus_attach(dev);
337 if (device_get_children(dev, &sc->gic_children, &sc->gic_nchildren) !=0)
338 sc->gic_nchildren = 0;
343 /* Failure so free resources */
348 "Failed to attach. Error %d\n", err);
355 gic_v3_add_children(ACPI_SUBTABLE_HEADER *entry, void *arg)
357 ACPI_MADT_GENERIC_TRANSLATOR *gict;
358 struct gic_v3_acpi_devinfo *di;
359 struct gic_v3_softc *sc;
364 if (entry->Type == ACPI_MADT_TYPE_GENERIC_TRANSLATOR) {
365 /* We have an ITS, add it as a child */
366 gict = (ACPI_MADT_GENERIC_TRANSLATOR *)entry;
368 sc = device_get_softc(dev);
370 child = device_add_child(dev, "its", -1);
374 di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
375 resource_list_init(&di->di_rl);
376 resource_list_add(&di->di_rl, SYS_RES_MEMORY, 0,
377 gict->BaseAddress, gict->BaseAddress + 128 * 1024 - 1,
379 err = acpi_iort_its_lookup(gict->TranslationId, &xref, &pxm);
381 di->di_gic_dinfo.gic_domain = pxm;
382 di->di_gic_dinfo.msi_xref = xref;
384 di->di_gic_dinfo.gic_domain = -1;
385 di->di_gic_dinfo.msi_xref = ACPI_MSI_XREF;
388 device_set_ivars(child, di);
393 gic_v3_acpi_bus_attach(device_t dev)
395 ACPI_TABLE_MADT *madt;
398 physaddr = acpi_find_table(ACPI_SIG_MADT);
402 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
404 device_printf(dev, "Unable to map the MADT to add children\n");
408 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
409 gic_v3_add_children, dev);
411 acpi_unmap_table(madt);
413 bus_generic_attach(dev);
416 static struct resource *
417 gic_v3_acpi_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
418 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
420 struct resource_list_entry *rle;
421 struct resource_list *rl;
423 /* We only allocate memory */
424 if (type != SYS_RES_MEMORY)
427 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
428 rl = BUS_GET_RESOURCE_LIST(bus, child);
432 /* Find defaults for this rid */
433 rle = resource_list_find(rl, type, *rid);
442 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
446 static struct resource_list *
447 gic_v3_acpi_get_resource_list(device_t bus, device_t child)
449 struct gic_v3_acpi_devinfo *di;
451 di = device_get_ivars(child);
452 KASSERT(di != NULL, ("%s: No devinfo", __func__));