2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Val Packett <val@packett.cool>
5 * Copyright (c) 2021 Ruslan Bukin <br@bsdpad.com>
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
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
36 #include <contrib/dev/acpica/include/acpi.h>
37 #include <dev/acpica/acpivar.h>
39 #include "acpi_bus_if.h"
49 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg)
51 ACPI_MADT_GENERIC_INTERRUPT *intr;
52 struct intr_map_data_acpi *ad;
53 struct intr_map_data *data;
69 if (entry->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
71 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry;
73 for (i = 0; i < MAXCPU; i++) {
75 if (pcpu != NULL && PCPU_GET_MPIDR(pcpu) == intr->ArmMpidr) {
83 device_printf(sc->dev, "MADT: could not find pcpu, "
84 "ArmMpidr %lx\n", intr->ArmMpidr);
89 device_printf(sc->dev, "MADT: cpu %d (mpidr %lu) irq %d "
90 "%s-triggered\n", cpuid, intr->ArmMpidr,
91 intr->PerformanceInterrupt,
92 (intr->Flags & ACPI_MADT_PERFORMANCE_IRQ_MODE) ?
95 bus_set_resource(sc->dev, SYS_RES_IRQ, ctx->i,
96 intr->PerformanceInterrupt, 1);
98 sc->irq[ctx->i].res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
99 &rid, RF_ACTIVE | RF_SHAREABLE);
100 if (sc->irq[ctx->i].res == NULL) {
101 device_printf(sc->dev, "Failed to allocate IRQ %d\n", ctx->i);
107 * BUS_CONFIG_INTR does nothing on arm64, so we manually set trigger
110 data = rman_get_virtual(sc->irq[ctx->i].res);
111 KASSERT(data->type == INTR_MAP_DATA_ACPI, ("Wrong data type"));
112 ad = (struct intr_map_data_acpi *)data;
113 ad->trig = (intr->Flags & ACPI_MADT_PERFORMANCE_IRQ_MODE) ?
114 INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL;
115 ad->pol = INTR_POLARITY_HIGH;
117 if (!intr_is_per_cpu(sc->irq[ctx->i].res))
118 sc->irq[ctx->i].cpuid = cpuid;
124 pmu_acpi_identify(driver_t *driver, device_t parent)
128 if (acpi_find_table(ACPI_SIG_MADT) == 0)
131 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST,
135 device_printf(parent, "pmu: Unable to add pmu child\n");
139 pmu_acpi_probe(device_t dev)
142 device_set_desc(dev, "Performance Monitoring Unit");
144 return (BUS_PROBE_NOWILDCARD);
148 pmu_acpi_attach(device_t dev)
150 struct pmu_softc *sc;
152 ACPI_TABLE_MADT *madt;
155 sc = device_get_softc(dev);
158 madt = acpi_map_table(acpi_find_table(ACPI_SIG_MADT), ACPI_SIG_MADT);
160 device_printf(dev, "Unable to map the MADT table\n");
164 /* We have to initialize cpuid to -1. */
165 for (i = 0; i < MAX_RLEN; i++)
166 sc->irq[i].cpuid = -1;
171 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length,
174 acpi_unmap_table(madt);
179 return (pmu_attach(dev));
182 static device_method_t pmu_acpi_methods[] = {
183 DEVMETHOD(device_identify, pmu_acpi_identify),
184 DEVMETHOD(device_probe, pmu_acpi_probe),
185 DEVMETHOD(device_attach, pmu_acpi_attach),
189 DEFINE_CLASS_0(pmu, pmu_acpi_driver, pmu_acpi_methods,
190 sizeof(struct pmu_softc));
192 DRIVER_MODULE(pmu, acpi, pmu_acpi_driver, 0, 0);