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 #include "opt_hwpmc_hooks.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/malloc.h>
44 #include <sys/rman.h>
45 #include <sys/timeet.h>
46 #include <sys/timetc.h>
47 #include <sys/pmc.h>
48 #include <sys/pmckern.h>
49
50 #include <machine/bus.h>
51 #include <machine/cpu.h>
52 #include <machine/intr.h>
53
54 #include "pmu.h"
55
56 /* CCNT */
57 #if defined(__arm__) && (__ARM_ARCH > 6)
58 int pmu_attched = 0;
59 uint32_t ccnt_hi[MAXCPU];
60 #endif
61
62 #define PMU_OVSR_C 0x80000000 /* Cycle Counter */
63 #define PMU_IESR_C 0x80000000 /* Cycle Counter */
64
65 static int
pmu_intr(void * arg)66 pmu_intr(void *arg)
67 {
68 #ifdef HWPMC_HOOKS
69 struct trapframe *tf;
70 #endif
71 uint32_t r;
72 #if defined(__arm__) && (__ARM_ARCH > 6)
73 u_int cpu;
74
75 cpu = PCPU_GET(cpuid);
76
77 r = cp15_pmovsr_get();
78 if (r & PMU_OVSR_C) {
79 atomic_add_32(&ccnt_hi[cpu], 1);
80 /* Clear the event. */
81 r &= ~PMU_OVSR_C;
82 cp15_pmovsr_set(PMU_OVSR_C);
83 }
84 #else
85 r = 1;
86 #endif
87
88 #ifdef HWPMC_HOOKS
89 /* Only call into the HWPMC framework if we know there is work. */
90 if (r != 0 && pmc_intr) {
91 tf = arg;
92 (*pmc_intr)(tf);
93 }
94 #endif
95
96 return (FILTER_HANDLED);
97 }
98
99 int
pmu_attach(device_t dev)100 pmu_attach(device_t dev)
101 {
102 struct pmu_softc *sc;
103 #if defined(__arm__) && (__ARM_ARCH > 6)
104 uint32_t iesr;
105 #endif
106 int err, i;
107
108 sc = device_get_softc(dev);
109 sc->dev = dev;
110
111 for (i = 0; i < MAX_RLEN; i++) {
112 if (sc->irq[i].res == NULL)
113 break;
114 err = bus_setup_intr(dev, sc->irq[i].res,
115 INTR_MPSAFE | INTR_TYPE_MISC, pmu_intr, NULL, NULL,
116 &sc->irq[i].ih);
117 if (err != 0) {
118 device_printf(dev,
119 "Unable to setup interrupt handler.\n");
120 goto fail;
121 }
122 if (sc->irq[i].cpuid != -1) {
123 err = bus_bind_intr(dev, sc->irq[i].res,
124 sc->irq[i].cpuid);
125 if (err != 0) {
126 device_printf(sc->dev,
127 "Unable to bind interrupt.\n");
128 goto fail;
129 }
130 }
131 }
132
133 #if defined(__arm__) && (__ARM_ARCH > 6)
134 /* Initialize to 0. */
135 for (i = 0; i < MAXCPU; i++)
136 ccnt_hi[i] = 0;
137
138 /* Enable the interrupt to fire on overflow. */
139 iesr = cp15_pminten_get();
140 iesr |= PMU_IESR_C;
141 cp15_pminten_set(iesr);
142
143 /* Need this for getcyclecount() fast path. */
144 pmu_attched |= 1;
145 #endif
146
147 return (0);
148
149 fail:
150 for (i = 1; i < MAX_RLEN; i++) {
151 if (sc->irq[i].ih != NULL)
152 bus_teardown_intr(dev, sc->irq[i].res, sc->irq[i].ih);
153 if (sc->irq[i].res != NULL)
154 bus_release_resource(dev, SYS_RES_IRQ, i,
155 sc->irq[i].res);
156 }
157 return(err);
158 }
159
160