2 * Copyright (c) 2011,2016 The FreeBSD Foundation
5 * This software was developed by Andrew Turner under
6 * sponsorship from the FreeBSD Foundation.
8 * Developed by Damjan Marion <damjan.marion@gmail.com>
10 * Based on OMAP4 GIC code by Ben Gray
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the company nor the name of the author may be used to
21 * endorse or promote products derived from this software without specific
22 * prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "opt_platform.h"
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/module.h>
50 #include <machine/intr.h>
52 #include <contrib/dev/acpica/include/acpi.h>
53 #include <dev/acpica/acpivar.h>
55 #include <arm/arm/gic.h>
56 #include <arm/arm/gic_common.h>
58 struct gic_acpi_devinfo {
59 struct resource_list rl;
62 static device_identify_t gic_acpi_identify;
63 static device_probe_t gic_acpi_probe;
64 static device_attach_t gic_acpi_attach;
65 static bus_get_resource_list_t gic_acpi_get_resource_list;
66 static bool arm_gic_add_children(device_t);
68 static device_method_t gic_acpi_methods[] = {
69 /* Device interface */
70 DEVMETHOD(device_identify, gic_acpi_identify),
71 DEVMETHOD(device_probe, gic_acpi_probe),
72 DEVMETHOD(device_attach, gic_acpi_attach),
75 DEVMETHOD(bus_get_resource_list, gic_acpi_get_resource_list),
80 DEFINE_CLASS_1(gic, gic_acpi_driver, gic_acpi_methods,
81 sizeof(struct arm_gic_softc), arm_gic_driver);
83 static devclass_t gic_acpi_devclass;
85 EARLY_DRIVER_MODULE(gic, acpi, gic_acpi_driver, gic_acpi_devclass, 0, 0,
86 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
88 struct madt_table_data {
90 ACPI_MADT_GENERIC_DISTRIBUTOR *dist;
91 ACPI_MADT_GENERIC_INTERRUPT *intr[MAXCPU];
95 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
97 struct madt_table_data *madt_data;
98 ACPI_MADT_GENERIC_INTERRUPT *intr;
100 madt_data = (struct madt_table_data *)arg;
102 switch(entry->Type) {
103 case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
104 if (madt_data->dist != NULL) {
106 device_printf(madt_data->parent,
107 "gic: Already have a distributor table");
110 (ACPI_MADT_GENERIC_DISTRIBUTOR *)entry;
112 case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
113 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
114 if (intr->CpuInterfaceNumber < MAXCPU)
115 madt_data->intr[intr->CpuInterfaceNumber] = intr;
121 gic_acpi_identify(driver_t *driver, device_t parent)
123 struct madt_table_data madt_data;
124 ACPI_MADT_GENERIC_INTERRUPT *intr;
125 ACPI_TABLE_MADT *madt;
130 physaddr = acpi_find_table(ACPI_SIG_MADT);
134 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
136 device_printf(parent, "gic: Unable to map the MADT\n");
140 bzero(&madt_data, sizeof(madt_data));
141 madt_data.parent = parent;
142 madt_data.dist = NULL;
144 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
145 madt_handler, &madt_data);
147 /* Check the version of the GIC we have */
148 switch (madt_data.dist->Version) {
149 case ACPI_MADT_GIC_VERSION_NONE:
150 case ACPI_MADT_GIC_VERSION_V1:
151 case ACPI_MADT_GIC_VERSION_V2:
158 for (i = 0; i < MAXCPU; i++) {
159 if (madt_data.intr[i] != NULL) {
161 intr = madt_data.intr[i];
162 } else if (intr->BaseAddress !=
163 madt_data.intr[i]->BaseAddress) {
164 device_printf(parent,
165 "gic: Not all CPU interfaces at the same address, this may fail\n");
170 device_printf(parent, "gic: No CPU interfaces found\n");
174 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE,
177 device_printf(parent, "add gic child failed\n");
181 BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 0,
182 madt_data.dist->BaseAddress, 4 * 1024);
183 BUS_SET_RESOURCE(parent, dev, SYS_RES_MEMORY, 1,
184 intr->BaseAddress, 4 * 1024);
186 acpi_set_private(dev, (void *)(uintptr_t)madt_data.dist->Version);
188 acpi_unmap_table(madt);
192 gic_acpi_probe(device_t dev)
195 switch((uintptr_t)acpi_get_private(dev)) {
196 case ACPI_MADT_GIC_VERSION_NONE:
197 case ACPI_MADT_GIC_VERSION_V1:
198 case ACPI_MADT_GIC_VERSION_V2:
204 device_set_desc(dev, "ARM Generic Interrupt Controller");
205 return (BUS_PROBE_NOWILDCARD);
209 gic_acpi_attach(device_t dev)
211 struct arm_gic_softc *sc = device_get_softc(dev);
215 sc->gic_bus = GIC_BUS_ACPI;
217 err = arm_gic_attach(dev);
221 xref = ACPI_INTR_XREF;
224 * Now, when everything is initialized, it's right time to
225 * register interrupt controller to interrupt framefork.
227 if (intr_pic_register(dev, xref) == NULL) {
228 device_printf(dev, "could not register PIC\n");
233 * Controller is root:
235 if (intr_pic_claim_root(dev, xref, arm_gic_intr, sc,
236 GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
237 device_printf(dev, "could not set PIC as a root\n");
238 intr_pic_deregister(dev, xref);
241 /* If we have children probe and attach them */
242 if (arm_gic_add_children(dev)) {
243 bus_generic_probe(dev);
244 return (bus_generic_attach(dev));
254 static struct resource_list *
255 gic_acpi_get_resource_list(device_t bus, device_t child)
257 struct gic_acpi_devinfo *di;
259 di = device_get_ivars(child);
260 KASSERT(di != NULL, ("gic_acpi_get_resource_list: No devinfo"));
266 madt_gicv2m_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
268 struct arm_gic_softc *sc;
269 ACPI_MADT_GENERIC_MSI_FRAME *msi;
270 struct gic_acpi_devinfo *dinfo;
273 if (entry->Type == ACPI_MADT_TYPE_GENERIC_MSI_FRAME) {
276 msi = (ACPI_MADT_GENERIC_MSI_FRAME *)entry;
278 device_printf(dev, "frame: %x %lx %x %u %u\n", msi->MsiFrameId,
279 msi->BaseAddress, msi->Flags, msi->SpiCount, msi->SpiBase);
281 cdev = device_add_child(dev, NULL, -1);
285 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
286 resource_list_init(&dinfo->rl);
287 resource_list_add(&dinfo->rl, SYS_RES_MEMORY, 0,
288 msi->BaseAddress, msi->BaseAddress + PAGE_SIZE - 1,
290 device_set_ivars(cdev, dinfo);
295 arm_gic_add_children(device_t dev)
297 struct arm_gic_softc *sc = device_get_softc(dev);
298 ACPI_TABLE_MADT *madt;
301 /* This should return a valid address as it did in gic_acpi_identify */
302 physaddr = acpi_find_table(ACPI_SIG_MADT);
306 madt = acpi_map_table(physaddr, ACPI_SIG_MADT);
308 device_printf(dev, "gic: Unable to map the MADT\n");
312 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
313 madt_gicv2m_handler, sc);
315 acpi_unmap_table(madt);
321 arm_gicv2m_acpi_probe(device_t dev)
324 if (gic_get_bus(dev) != GIC_BUS_ACPI)
327 if (gic_get_hw_rev(dev) > 2)
330 device_set_desc(dev, "ARM Generic Interrupt Controller MSI/MSIX");
331 return (BUS_PROBE_DEFAULT);
335 arm_gicv2m_acpi_attach(device_t dev)
337 struct arm_gicv2m_softc *sc;
339 sc = device_get_softc(dev);
340 sc->sc_xref = ACPI_MSI_XREF;
342 return (arm_gicv2m_attach(dev));
345 static device_method_t arm_gicv2m_acpi_methods[] = {
346 /* Device interface */
347 DEVMETHOD(device_probe, arm_gicv2m_acpi_probe),
348 DEVMETHOD(device_attach, arm_gicv2m_acpi_attach),
354 DEFINE_CLASS_1(gicv2m, arm_gicv2m_acpi_driver, arm_gicv2m_acpi_methods,
355 sizeof(struct arm_gicv2m_softc), arm_gicv2m_driver);
357 static devclass_t arm_gicv2m_acpi_devclass;
359 EARLY_DRIVER_MODULE(gicv2m_acpi, gic, arm_gicv2m_acpi_driver,
360 arm_gicv2m_acpi_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);