tprof_armv8.c revision 1.17 1 /* $NetBSD: tprof_armv8.c,v 1.17 2022/12/01 00:29:10 ryo Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * 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 __KERNEL_RCSID(0, "$NetBSD: tprof_armv8.c,v 1.17 2022/12/01 00:29:10 ryo Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/cpu.h>
35 #include <sys/xcall.h>
36
37 #include <dev/tprof/tprof.h>
38
39 #include <arm/armreg.h>
40 #include <arm/cpufunc.h>
41
42 #include <dev/tprof/tprof_armv8.h>
43
44 static tprof_param_t armv8_pmu_param;
45 static const u_int armv8_pmu_counter = 0;
46 static uint32_t counter_val;
47 static uint32_t counter_reset_val;
48
49 static bool
50 armv8_pmu_event_implemented(uint16_t event)
51 {
52 uint64_t eid[2];
53
54 if (event >= 64)
55 return false;
56
57 eid[0] = reg_pmceid0_el0_read();
58 eid[1] = reg_pmceid1_el0_read();
59
60 /* The low 32bits of PMCEID[01]_EL0 contain the common events 0 to n */
61 const u_int idx = event / 32;
62 const u_int bit = event % 32;
63
64 if (eid[idx] & __BIT(bit))
65 return true;
66
67 return false;
68 }
69
70 static void
71 armv8_pmu_set_pmevtyper(u_int counter, uint64_t val)
72 {
73 reg_pmselr_el0_write(counter);
74 isb();
75 reg_pmxevtyper_el0_write(val);
76 }
77
78 static void
79 armv8_pmu_set_pmevcntr(u_int counter, uint32_t val)
80 {
81 reg_pmselr_el0_write(counter);
82 isb();
83 reg_pmxevcntr_el0_write(val);
84 }
85
86 static void
87 armv8_pmu_start_cpu(void *arg1, void *arg2)
88 {
89 const uint32_t counter_mask = __BIT(armv8_pmu_counter);
90 uint64_t pmevtyper;
91
92 /* Disable event counter */
93 reg_pmcntenclr_el0_write(counter_mask);
94
95 /* Configure event counter */
96 pmevtyper = __SHIFTIN(armv8_pmu_param.p_event, PMEVTYPER_EVTCOUNT);
97 if (!ISSET(armv8_pmu_param.p_flags, TPROF_PARAM_USER))
98 pmevtyper |= PMEVTYPER_U;
99 if (!ISSET(armv8_pmu_param.p_flags, TPROF_PARAM_KERN))
100 pmevtyper |= PMEVTYPER_P;
101
102 armv8_pmu_set_pmevtyper(armv8_pmu_counter, pmevtyper);
103
104 /* Enable overflow interrupts */
105 reg_pmintenset_el1_write(counter_mask);
106
107 /* Clear overflow flag */
108 reg_pmovsclr_el0_write(counter_mask);
109
110 /* Initialize event counter value */
111 armv8_pmu_set_pmevcntr(armv8_pmu_counter, counter_reset_val);
112
113 /* Enable event counter */
114 reg_pmcntenset_el0_write(counter_mask);
115 reg_pmcr_el0_write(reg_pmcr_el0_read() | PMCR_E);
116 }
117
118 static void
119 armv8_pmu_stop_cpu(void *arg1, void *arg2)
120 {
121 const uint32_t counter_mask = __BIT(armv8_pmu_counter);
122
123 /* Disable overflow interrupts */
124 reg_pmintenclr_el1_write(counter_mask);
125
126 /* Disable event counter */
127 reg_pmcntenclr_el0_write(counter_mask);
128 }
129
130 static uint64_t
131 armv8_pmu_estimate_freq(void)
132 {
133 uint64_t cpufreq = curcpu()->ci_data.cpu_cc_freq;
134 uint64_t freq = 10000;
135
136 counter_val = cpufreq / freq;
137 if (counter_val == 0)
138 counter_val = 4000000000ULL / freq;
139
140 return freq;
141 }
142
143 static uint32_t
144 armv8_pmu_ident(void)
145 {
146 return TPROF_IDENT_ARMV8_GENERIC;
147 }
148
149 static int
150 armv8_pmu_start(const tprof_param_t *param)
151 {
152 /* PMCR.N of 0 means that no event counters are available */
153 if (__SHIFTOUT(reg_pmcr_el0_read(), PMCR_N) == 0) {
154 return EINVAL;
155 }
156
157 if (!armv8_pmu_event_implemented(param->p_event)) {
158 printf("%s: event %#" PRIx64 " not implemented on this CPU\n",
159 __func__, param->p_event);
160 return EINVAL;
161 }
162
163 counter_reset_val = -counter_val + 1;
164
165 armv8_pmu_param = *param;
166 uint64_t xc = xc_broadcast(0, armv8_pmu_start_cpu, NULL, NULL);
167 xc_wait(xc);
168
169 return 0;
170 }
171
172 static void
173 armv8_pmu_stop(const tprof_param_t *param)
174 {
175 uint64_t xc;
176
177 xc = xc_broadcast(0, armv8_pmu_stop_cpu, NULL, NULL);
178 xc_wait(xc);
179 }
180
181 static const tprof_backend_ops_t tprof_armv8_pmu_ops = {
182 .tbo_estimate_freq = armv8_pmu_estimate_freq,
183 .tbo_ident = armv8_pmu_ident,
184 .tbo_start = armv8_pmu_start,
185 .tbo_stop = armv8_pmu_stop,
186 };
187
188 int
189 armv8_pmu_intr(void *priv)
190 {
191 const struct trapframe * const tf = priv;
192 const uint32_t counter_mask = __BIT(armv8_pmu_counter);
193 tprof_frame_info_t tfi;
194
195 const uint32_t pmovs = reg_pmovsset_el0_read();
196 if ((pmovs & counter_mask) != 0) {
197 tfi.tfi_pc = tf->tf_pc;
198 tfi.tfi_inkernel = tfi.tfi_pc >= VM_MIN_KERNEL_ADDRESS &&
199 tfi.tfi_pc < VM_MAX_KERNEL_ADDRESS;
200 tprof_sample(NULL, &tfi);
201
202 armv8_pmu_set_pmevcntr(armv8_pmu_counter, counter_reset_val);
203 }
204 reg_pmovsclr_el0_write(pmovs);
205
206 return 1;
207 }
208
209 static void
210 armv8_pmu_init_cpu(void *arg1, void *arg2)
211 {
212 /* Disable EL0 access to performance monitors */
213 reg_pmuserenr_el0_write(0);
214
215 /* Disable interrupts */
216 reg_pmintenclr_el1_write(PMINTEN_P);
217
218 /* Disable event counters */
219 reg_pmcntenclr_el0_write(PMCNTEN_P);
220 }
221
222 bool
223 armv8_pmu_detect(void)
224 {
225 const uint64_t dfr0 = reg_id_aa64dfr0_el1_read();
226 const u_int pmuver = __SHIFTOUT(dfr0, ID_AA64DFR0_EL1_PMUVER);
227
228 return pmuver != ID_AA64DFR0_EL1_PMUVER_NONE &&
229 pmuver != ID_AA64DFR0_EL1_PMUVER_IMPL;
230 }
231
232 int
233 armv8_pmu_init(void)
234 {
235 KASSERT(armv8_pmu_detect());
236
237 uint64_t xc = xc_broadcast(0, armv8_pmu_init_cpu, NULL, NULL);
238 xc_wait(xc);
239
240 return tprof_backend_register("tprof_armv8", &tprof_armv8_pmu_ops,
241 TPROF_BACKEND_VERSION);
242 }
243