2 * Copyright (c) 2016 The FreeBSD Foundation
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
41 #include <machine/intr.h>
42 #include <machine/resource.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <dev/acpica/acpivar.h>
47 #include "gic_v3_reg.h"
48 #include "gic_v3_var.h"
50 struct gic_v3_acpi_devinfo {
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;
59 static void gic_v3_acpi_bus_attach(device_t);
61 static device_method_t gic_v3_acpi_methods[] = {
62 /* Device interface */
63 DEVMETHOD(device_identify, gic_v3_acpi_identify),
64 DEVMETHOD(device_probe, gic_v3_acpi_probe),
65 DEVMETHOD(device_attach, gic_v3_acpi_attach),
68 DEVMETHOD(bus_alloc_resource, gic_v3_acpi_bus_alloc_res),
69 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
75 DEFINE_CLASS_1(gic, gic_v3_acpi_driver, gic_v3_acpi_methods,
76 sizeof(struct gic_v3_softc), gic_v3_driver);
78 static devclass_t gic_v3_acpi_devclass;
80 EARLY_DRIVER_MODULE(gic_v3, acpi, gic_v3_acpi_driver, gic_v3_acpi_devclass,
81 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
84 struct madt_table_data {
87 ACPI_MADT_GENERIC_DISTRIBUTOR *dist;
92 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
94 struct madt_table_data *madt_data;
96 madt_data = (struct madt_table_data *)arg;
99 case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
100 if (madt_data->dist != NULL) {
102 device_printf(madt_data->parent,
103 "gic: Already have a distributor table");
106 madt_data->dist = (ACPI_MADT_GENERIC_DISTRIBUTOR *)entry;
109 case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
118 rdist_map(ACPI_SUBTABLE_HEADER *entry, void *arg)
120 ACPI_MADT_GENERIC_REDISTRIBUTOR *redist;
121 struct madt_table_data *madt_data;
123 madt_data = (struct madt_table_data *)arg;
125 switch(entry->Type) {
126 case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
127 redist = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)entry;
130 BUS_SET_RESOURCE(madt_data->parent, madt_data->dev,
131 SYS_RES_MEMORY, madt_data->count, redist->BaseAddress,
141 gic_v3_acpi_identify(driver_t *driver, device_t parent)
143 struct madt_table_data madt_data;
144 ACPI_TABLE_MADT *madt;
148 physaddr = acpi_find_table(ACPI_SIG_MADT);
152 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
154 device_printf(parent, "gic: Unable to map the MADT\n");
158 madt_data.parent = parent;
159 madt_data.dist = NULL;
162 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
163 madt_handler, &madt_data);
164 if (madt_data.dist == NULL) {
165 device_printf(parent,
166 "No gic interrupt or distributor table\n");
169 /* This is for the wrong GIC version */
170 if (madt_data.dist->Version != ACPI_MADT_GIC_VERSION_V3)
173 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE,
176 device_printf(parent, "add gic child failed\n");
180 /* Add the MADT data */
181 BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
182 madt_data.dist->BaseAddress, 128 * 1024);
185 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
186 rdist_map, &madt_data);
188 acpi_set_private(dev, (void *)(uintptr_t)madt_data.dist->Version);
191 acpi_unmap_table(madt);
195 gic_v3_acpi_probe(device_t dev)
198 switch((uintptr_t)acpi_get_private(dev)) {
199 case ACPI_MADT_GIC_VERSION_V3:
205 device_set_desc(dev, GIC_V3_DEVSTR);
206 return (BUS_PROBE_NOWILDCARD);
210 gic_v3_acpi_attach(device_t dev)
212 struct gic_v3_softc *sc;
215 sc = device_get_softc(dev);
217 sc->gic_bus = GIC_BUS_ACPI;
219 /* TODO: Count these correctly */
220 sc->gic_redists.nregions = 1;
222 err = gic_v3_attach(dev);
226 sc->gic_pic = intr_pic_register(dev, 0);
227 if (sc->gic_pic == NULL) {
228 device_printf(dev, "could not register PIC\n");
233 if (intr_pic_claim_root(dev, 0, arm_gic_v3_intr, sc,
234 GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
240 * Try to register the ITS driver to this GIC. The GIC will act as
241 * a bus in that case. Failure here will not affect the main GIC
244 gic_v3_acpi_bus_attach(dev);
251 "Failed to attach. Error %d\n", err);
253 /* Failure so free resources */
260 gic_v3_add_children(ACPI_SUBTABLE_HEADER *entry, void *arg)
262 ACPI_MADT_GENERIC_TRANSLATOR *gict;
263 struct gic_v3_acpi_devinfo *di;
266 if (entry->Type == ACPI_MADT_TYPE_GENERIC_TRANSLATOR) {
267 /* We have an ITS, add it as a child */
268 gict = (ACPI_MADT_GENERIC_TRANSLATOR *)entry;
271 child = device_add_child(dev, "its", -1);
275 di = malloc(sizeof(*di), M_GIC_V3, M_WAITOK | M_ZERO);
276 resource_list_init(&di->di_rl);
277 resource_list_add(&di->di_rl, SYS_RES_MEMORY, 0,
278 gict->BaseAddress, gict->BaseAddress + 128 * 1024 - 1,
280 device_set_ivars(child, di);
285 gic_v3_acpi_bus_attach(device_t dev)
287 ACPI_TABLE_MADT *madt;
290 physaddr = acpi_find_table(ACPI_SIG_MADT);
294 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
296 device_printf(dev, "Unable to map the MADT to add children\n");
300 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
301 gic_v3_add_children, dev);
303 acpi_unmap_table(madt);
305 bus_generic_attach(dev);
308 static struct resource *
309 gic_v3_acpi_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
310 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
312 struct gic_v3_acpi_devinfo *di;
313 struct resource_list_entry *rle;
315 /* We only allocate memory */
316 if (type != SYS_RES_MEMORY)
319 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
320 if ((di = device_get_ivars(child)) == NULL)
323 /* Find defaults for this rid */
324 rle = resource_list_find(&di->di_rl, type, *rid);
333 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,