1 /*- 2 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * Performance Monitoring Unit 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "opt_hwpmc_hooks.h" 39 #include "opt_platform.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/bus.h> 44 #include <sys/kernel.h> 45 #include <sys/module.h> 46 #include <sys/malloc.h> 47 #include <sys/rman.h> 48 #include <sys/timeet.h> 49 #include <sys/timetc.h> 50 #include <sys/pmc.h> 51 #include <sys/pmckern.h> 52 53 #ifdef FDT 54 #include <dev/ofw/openfirm.h> 55 #include <dev/ofw/ofw_bus.h> 56 #include <dev/ofw/ofw_bus_subr.h> 57 #endif 58 59 #include <machine/bus.h> 60 #include <machine/cpu.h> 61 #include <machine/intr.h> 62 63 #ifdef notyet 64 #define MAX_RLEN 8 65 #else 66 #define MAX_RLEN 1 67 #endif 68 69 struct pmu_softc { 70 struct resource *res[MAX_RLEN]; 71 device_t dev; 72 void *ih[MAX_RLEN]; 73 }; 74 75 static struct resource_spec pmu_spec[] = { 76 { SYS_RES_IRQ, 0, RF_ACTIVE }, 77 /* We don't currently handle pmu events, other than on cpu 0 */ 78 #ifdef notyet 79 { SYS_RES_IRQ, 1, RF_ACTIVE | RF_OPTIONAL }, 80 { SYS_RES_IRQ, 2, RF_ACTIVE | RF_OPTIONAL }, 81 { SYS_RES_IRQ, 3, RF_ACTIVE | RF_OPTIONAL }, 82 { SYS_RES_IRQ, 4, RF_ACTIVE | RF_OPTIONAL }, 83 { SYS_RES_IRQ, 5, RF_ACTIVE | RF_OPTIONAL }, 84 { SYS_RES_IRQ, 6, RF_ACTIVE | RF_OPTIONAL }, 85 { SYS_RES_IRQ, 7, RF_ACTIVE | RF_OPTIONAL }, 86 #endif 87 { -1, 0 } 88 }; 89 90 /* CCNT */ 91 #if __ARM_ARCH > 6 92 int pmu_attched = 0; 93 uint32_t ccnt_hi[MAXCPU]; 94 #endif 95 96 #define PMU_OVSR_C 0x80000000 /* Cycle Counter */ 97 #define PMU_IESR_C 0x80000000 /* Cycle Counter */ 98 99 static int 100 pmu_intr(void *arg) 101 { 102 #ifdef HWPMC_HOOKS 103 struct trapframe *tf; 104 #endif 105 uint32_t r; 106 #if defined(__arm__) && (__ARM_ARCH > 6) 107 u_int cpu; 108 109 cpu = PCPU_GET(cpuid); 110 111 r = cp15_pmovsr_get(); 112 if (r & PMU_OVSR_C) { 113 atomic_add_32(&ccnt_hi[cpu], 1); 114 /* Clear the event. */ 115 r &= ~PMU_OVSR_C; 116 cp15_pmovsr_set(PMU_OVSR_C); 117 } 118 #else 119 r = 1; 120 #endif 121 122 #ifdef HWPMC_HOOKS 123 /* Only call into the HWPMC framework if we know there is work. */ 124 if (r != 0 && pmc_intr) { 125 tf = arg; 126 (*pmc_intr)(PCPU_GET(cpuid), tf); 127 } 128 #endif 129 130 return (FILTER_HANDLED); 131 } 132 133 static int 134 pmu_attach(device_t dev) 135 { 136 struct pmu_softc *sc; 137 #if defined(__arm__) && (__ARM_ARCH > 6) 138 uint32_t iesr; 139 #endif 140 int err; 141 int i; 142 143 sc = device_get_softc(dev); 144 sc->dev = dev; 145 146 if (bus_alloc_resources(dev, pmu_spec, sc->res)) { 147 device_printf(dev, "could not allocate resources\n"); 148 return (ENXIO); 149 } 150 151 /* Setup interrupt handler */ 152 for (i = 0; i < MAX_RLEN; i++) { 153 if (sc->res[i] == NULL) 154 break; 155 156 err = bus_setup_intr(dev, sc->res[i], INTR_MPSAFE | INTR_TYPE_MISC, 157 pmu_intr, NULL, NULL, &sc->ih[i]); 158 if (err) { 159 device_printf(dev, "Unable to setup interrupt handler.\n"); 160 return (ENXIO); 161 } 162 } 163 164 #if defined(__arm__) && (__ARM_ARCH > 6) 165 /* Initialize to 0. */ 166 for (i = 0; i < MAXCPU; i++) 167 ccnt_hi[i] = 0; 168 169 /* Enable the interrupt to fire on overflow. */ 170 iesr = cp15_pminten_get(); 171 iesr |= PMU_IESR_C; 172 cp15_pminten_set(iesr); 173 174 /* Need this for getcyclecount() fast path. */ 175 pmu_attched |= 1; 176 #endif 177 178 return (0); 179 } 180 181 #ifdef FDT 182 static struct ofw_compat_data compat_data[] = { 183 {"arm,armv8-pmuv3", 1}, 184 {"arm,cortex-a17-pmu", 1}, 185 {"arm,cortex-a15-pmu", 1}, 186 {"arm,cortex-a12-pmu", 1}, 187 {"arm,cortex-a9-pmu", 1}, 188 {"arm,cortex-a8-pmu", 1}, 189 {"arm,cortex-a7-pmu", 1}, 190 {"arm,cortex-a5-pmu", 1}, 191 {"arm,arm11mpcore-pmu", 1}, 192 {"arm,arm1176-pmu", 1}, 193 {"arm,arm1136-pmu", 1}, 194 {"qcom,krait-pmu", 1}, 195 {NULL, 0} 196 }; 197 198 static int 199 pmu_fdt_probe(device_t dev) 200 { 201 202 if (!ofw_bus_status_okay(dev)) 203 return (ENXIO); 204 205 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) { 206 device_set_desc(dev, "Performance Monitoring Unit"); 207 return (BUS_PROBE_DEFAULT); 208 } 209 210 return (ENXIO); 211 } 212 213 static device_method_t pmu_fdt_methods[] = { 214 DEVMETHOD(device_probe, pmu_fdt_probe), 215 DEVMETHOD(device_attach, pmu_attach), 216 { 0, 0 } 217 }; 218 219 static driver_t pmu_fdt_driver = { 220 "pmu", 221 pmu_fdt_methods, 222 sizeof(struct pmu_softc), 223 }; 224 225 static devclass_t pmu_fdt_devclass; 226 227 DRIVER_MODULE(pmu, simplebus, pmu_fdt_driver, pmu_fdt_devclass, 0, 0); 228 #endif 229