1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2020 Greg V <greg@unrelenting.technology> 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 ctx->error = ENODEV; 86 return; 87 } 88 89 if (bootverbose) 90 device_printf(sc->dev, "MADT: cpu %d (mpidr %lu) irq %d " 91 "%s-triggered\n", cpuid, intr->ArmMpidr, 92 intr->PerformanceInterrupt, 93 (intr->Flags & ACPI_MADT_PERFORMANCE_IRQ_MODE) ? 94 "edge" : "level"); 95 96 bus_set_resource(sc->dev, SYS_RES_IRQ, ctx->i, 97 intr->PerformanceInterrupt, 1); 98 99 sc->irq[ctx->i].res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, 100 &rid, RF_ACTIVE | RF_SHAREABLE); 101 if (sc->irq[ctx->i].res == NULL) { 102 device_printf(sc->dev, "Failed to allocate IRQ %d\n", ctx->i); 103 ctx->error = ENXIO; 104 return; 105 } 106 107 /* 108 * BUS_CONFIG_INTR does nothing on arm64, so we manually set trigger 109 * mode. 110 */ 111 data = rman_get_virtual(sc->irq[ctx->i].res); 112 KASSERT(data->type == INTR_MAP_DATA_ACPI, ("Wrong data type")); 113 ad = (struct intr_map_data_acpi *)data; 114 ad->trig = (intr->Flags & ACPI_MADT_PERFORMANCE_IRQ_MODE) ? 115 INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL; 116 ad->pol = INTR_POLARITY_HIGH; 117 118 if (!intr_is_per_cpu(sc->irq[ctx->i].res)) 119 sc->irq[ctx->i].cpuid = cpuid; 120 121 ctx->i++; 122 } 123 124 static void 125 pmu_acpi_identify(driver_t *driver, device_t parent) 126 { 127 device_t dev; 128 129 if (acpi_find_table(ACPI_SIG_MADT) == 0) 130 return; 131 132 dev = BUS_ADD_CHILD(parent, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST, 133 "pmu", -1); 134 135 if (dev == NULL) 136 device_printf(parent, "pmu: Unable to add pmu child\n"); 137 } 138 139 static int 140 pmu_acpi_probe(device_t dev) 141 { 142 143 device_set_desc(dev, "Performance Monitoring Unit"); 144 145 return (BUS_PROBE_NOWILDCARD); 146 } 147 148 static int 149 pmu_acpi_attach(device_t dev) 150 { 151 struct pmu_softc *sc; 152 struct madt_ctx ctx; 153 ACPI_TABLE_MADT *madt; 154 int i; 155 156 sc = device_get_softc(dev); 157 sc->dev = dev; 158 159 madt = acpi_map_table(acpi_find_table(ACPI_SIG_MADT), ACPI_SIG_MADT); 160 if (madt == NULL) { 161 device_printf(dev, "Unable to map the MADT table\n"); 162 return (ENXIO); 163 } 164 165 /* We have to initialize cpuid to -1. */ 166 for (i = 0; i < MAX_RLEN; i++) 167 sc->irq[i].cpuid = -1; 168 169 ctx.sc = sc; 170 ctx.i = 0; 171 ctx.error = 0; 172 acpi_walk_subtables(madt + 1, (char *)madt + madt->Header.Length, 173 madt_handler, &ctx); 174 175 acpi_unmap_table(madt); 176 177 if (ctx.error) 178 return (ctx.error); 179 180 return (pmu_attach(dev)); 181 } 182 183 static device_method_t pmu_acpi_methods[] = { 184 DEVMETHOD(device_identify, pmu_acpi_identify), 185 DEVMETHOD(device_probe, pmu_acpi_probe), 186 DEVMETHOD(device_attach, pmu_acpi_attach), 187 DEVMETHOD_END, 188 }; 189 190 DEFINE_CLASS_0(pmu, pmu_acpi_driver, pmu_acpi_methods, 191 sizeof(struct pmu_softc)); 192 193 DRIVER_MODULE(pmu, acpi, pmu_acpi_driver, 0, 0); 194