1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2020 Val Packett <val@packett.cool> 5 * Copyright (c) 2021 Ruslan Bukin <br@bsdpad.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/bus.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/rman.h> 35 36 #include <contrib/dev/acpica/include/acpi.h> 37 #include <dev/acpica/acpivar.h> 38 39 #include "acpi_bus_if.h" 40 #include "pmu.h" 41 42 struct madt_ctx { 43 struct pmu_softc *sc; 44 int error; 45 int i; 46 }; 47 48 static void 49 madt_handler(ACPI_SUBTABLE_HEADER *entry, void *arg) 50 { 51 ACPI_MADT_GENERIC_INTERRUPT *intr; 52 struct intr_map_data_acpi *ad; 53 struct intr_map_data *data; 54 struct madt_ctx *ctx; 55 struct pmu_softc *sc; 56 struct pcpu *pcpu; 57 int rid; 58 int cpuid; 59 int i; 60 61 ctx = arg; 62 sc = ctx->sc; 63 rid = ctx->i; 64 cpuid = -1; 65 66 if (ctx->error) 67 return; 68 69 if (entry->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT) 70 return; 71 intr = (ACPI_MADT_GENERIC_INTERRUPT *)entry; 72 73 for (i = 0; i < MAXCPU; i++) { 74 pcpu = pcpu_find(i); 75 if (pcpu != NULL && PCPU_GET_MPIDR(pcpu) == intr->ArmMpidr) { 76 cpuid = i; 77 break; 78 } 79 } 80 81 if (cpuid == -1) { 82 /* pcpu not found. */ 83 device_printf(sc->dev, "MADT: could not find pcpu, " 84 "ArmMpidr %lx\n", intr->ArmMpidr); 85 return; 86 } 87 88 if (bootverbose) 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) ? 93 "edge" : "level"); 94 95 bus_set_resource(sc->dev, SYS_RES_IRQ, ctx->i, 96 intr->PerformanceInterrupt, 1); 97 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); 102 ctx->error = ENXIO; 103 return; 104 } 105 106 /* 107 * BUS_CONFIG_INTR does nothing on arm64, so we manually set trigger 108 * mode. 109 */ 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; 116 117 if (!intr_is_per_cpu(sc->irq[ctx->i].res)) 118 sc->irq[ctx->i].cpuid = cpuid; 119 120 ctx->i++; 121 } 122 123 static void 124 pmu_acpi_identify(driver_t *driver, device_t parent) 125 { 126 device_t dev; 127 128 if (acpi_find_table(ACPI_SIG_MADT) == 0) 129 return; 130 131 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST, 132 "pmu", -1); 133 134 if (dev == NULL) 135 device_printf(parent, "pmu: Unable to add pmu child\n"); 136 } 137 138 static int 139 pmu_acpi_probe(device_t dev) 140 { 141 142 device_set_desc(dev, "Performance Monitoring Unit"); 143 144 return (BUS_PROBE_NOWILDCARD); 145 } 146 147 static int 148 pmu_acpi_attach(device_t dev) 149 { 150 struct pmu_softc *sc; 151 struct madt_ctx ctx; 152 ACPI_TABLE_MADT *madt; 153 int i; 154 155 sc = device_get_softc(dev); 156 sc->dev = dev; 157 158 madt = acpi_map_table(acpi_find_table(ACPI_SIG_MADT), ACPI_SIG_MADT); 159 if (madt == NULL) { 160 device_printf(dev, "Unable to map the MADT table\n"); 161 return (ENXIO); 162 } 163 164 /* We have to initialize cpuid to -1. */ 165 for (i = 0; i < MAX_RLEN; i++) 166 sc->irq[i].cpuid = -1; 167 168 ctx.sc = sc; 169 ctx.i = 0; 170 ctx.error = 0; 171 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length, 172 madt_handler, &ctx); 173 174 acpi_unmap_table(madt); 175 176 if (ctx.error) 177 return (ctx.error); 178 179 return (pmu_attach(dev)); 180 } 181 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), 186 DEVMETHOD_END, 187 }; 188 189 DEFINE_CLASS_0(pmu, pmu_acpi_driver, pmu_acpi_methods, 190 sizeof(struct pmu_softc)); 191 192 DRIVER_MODULE(pmu, acpi, pmu_acpi_driver, 0, 0); 193