tprof_armv8.c revision 1.4.8.3 1 1.4.8.3 martin /* $NetBSD: tprof_armv8.c,v 1.4.8.3 2020/04/08 14:08:13 martin Exp $ */
2 1.4.8.2 christos
3 1.4.8.2 christos /*-
4 1.4.8.2 christos * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.4.8.2 christos * All rights reserved.
6 1.4.8.2 christos *
7 1.4.8.2 christos * Redistribution and use in source and binary forms, with or without
8 1.4.8.2 christos * modification, are permitted provided that the following conditions
9 1.4.8.2 christos * are met:
10 1.4.8.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.4.8.2 christos * notice, this list of conditions and the following disclaimer.
12 1.4.8.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.8.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.4.8.2 christos * documentation and/or other materials provided with the distribution.
15 1.4.8.2 christos *
16 1.4.8.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.4.8.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.4.8.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.4.8.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.4.8.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.4.8.2 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.4.8.2 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.4.8.2 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.4.8.2 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.4.8.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.4.8.2 christos * SUCH DAMAGE.
27 1.4.8.2 christos */
28 1.4.8.2 christos
29 1.4.8.2 christos #include <sys/cdefs.h>
30 1.4.8.3 martin __KERNEL_RCSID(0, "$NetBSD: tprof_armv8.c,v 1.4.8.3 2020/04/08 14:08:13 martin Exp $");
31 1.4.8.2 christos
32 1.4.8.2 christos #include <sys/param.h>
33 1.4.8.2 christos #include <sys/bus.h>
34 1.4.8.2 christos #include <sys/cpu.h>
35 1.4.8.2 christos #include <sys/xcall.h>
36 1.4.8.2 christos
37 1.4.8.2 christos #include <dev/tprof/tprof.h>
38 1.4.8.2 christos
39 1.4.8.2 christos #include <arm/armreg.h>
40 1.4.8.2 christos #include <arm/locore.h>
41 1.4.8.2 christos
42 1.4.8.2 christos #include <dev/tprof/tprof_armv8.h>
43 1.4.8.2 christos
44 1.4.8.2 christos static tprof_param_t armv8_pmu_param;
45 1.4.8.2 christos static const u_int armv8_pmu_counter = 1;
46 1.4.8.2 christos static uint32_t counter_val;
47 1.4.8.2 christos static uint32_t counter_reset_val;
48 1.4.8.2 christos
49 1.4.8.2 christos static bool
50 1.4.8.2 christos armv8_pmu_event_implemented(uint16_t event)
51 1.4.8.2 christos {
52 1.4.8.2 christos uint64_t eid[2];
53 1.4.8.2 christos
54 1.4.8.2 christos if (event >= 64)
55 1.4.8.2 christos return false;
56 1.4.8.2 christos
57 1.4.8.2 christos eid[0] = reg_pmceid0_el0_read();
58 1.4.8.2 christos eid[1] = reg_pmceid1_el0_read();
59 1.4.8.2 christos
60 1.4.8.2 christos const u_int idx = event / 32;
61 1.4.8.2 christos const u_int bit = event % 32;
62 1.4.8.2 christos
63 1.4.8.2 christos if (eid[idx] & __BIT(bit))
64 1.4.8.2 christos return true;
65 1.4.8.2 christos
66 1.4.8.2 christos return false;
67 1.4.8.2 christos }
68 1.4.8.2 christos
69 1.4.8.2 christos static void
70 1.4.8.2 christos armv8_pmu_set_pmevtyper(u_int counter, uint64_t val)
71 1.4.8.2 christos {
72 1.4.8.2 christos reg_pmselr_el0_write(counter);
73 1.4.8.2 christos arm_isb();
74 1.4.8.2 christos reg_pmxevtyper_el0_write(val);
75 1.4.8.2 christos }
76 1.4.8.2 christos
77 1.4.8.2 christos static void
78 1.4.8.2 christos armv8_pmu_set_pmevcntr(u_int counter, uint32_t val)
79 1.4.8.2 christos {
80 1.4.8.2 christos reg_pmselr_el0_write(counter);
81 1.4.8.2 christos arm_isb();
82 1.4.8.2 christos reg_pmxevcntr_el0_write(val);
83 1.4.8.2 christos }
84 1.4.8.2 christos
85 1.4.8.2 christos static void
86 1.4.8.2 christos armv8_pmu_start_cpu(void *arg1, void *arg2)
87 1.4.8.2 christos {
88 1.4.8.2 christos const uint32_t counter_mask = __BIT(armv8_pmu_counter);
89 1.4.8.3 martin uint64_t pmevtyper;
90 1.4.8.2 christos
91 1.4.8.2 christos /* Disable event counter */
92 1.4.8.2 christos reg_pmcntenclr_el0_write(counter_mask);
93 1.4.8.2 christos
94 1.4.8.2 christos /* Configure event counter */
95 1.4.8.2 christos pmevtyper = __SHIFTIN(armv8_pmu_param.p_event, PMEVTYPER_EVTCOUNT);
96 1.4.8.2 christos if (!ISSET(armv8_pmu_param.p_flags, TPROF_PARAM_USER))
97 1.4.8.2 christos pmevtyper |= PMEVTYPER_U;
98 1.4.8.2 christos if (!ISSET(armv8_pmu_param.p_flags, TPROF_PARAM_KERN))
99 1.4.8.2 christos pmevtyper |= PMEVTYPER_P;
100 1.4.8.2 christos
101 1.4.8.2 christos armv8_pmu_set_pmevtyper(armv8_pmu_counter, pmevtyper);
102 1.4.8.2 christos
103 1.4.8.2 christos /* Enable overflow interrupts */
104 1.4.8.2 christos reg_pmintenset_el1_write(counter_mask);
105 1.4.8.2 christos
106 1.4.8.2 christos /* Clear overflow flag */
107 1.4.8.2 christos reg_pmovsclr_el0_write(counter_mask);
108 1.4.8.2 christos
109 1.4.8.2 christos /* Initialize event counter value */
110 1.4.8.2 christos armv8_pmu_set_pmevcntr(armv8_pmu_counter, counter_reset_val);
111 1.4.8.2 christos
112 1.4.8.2 christos /* Enable event counter */
113 1.4.8.2 christos reg_pmcntenset_el0_write(counter_mask);
114 1.4.8.2 christos }
115 1.4.8.2 christos
116 1.4.8.2 christos static void
117 1.4.8.2 christos armv8_pmu_stop_cpu(void *arg1, void *arg2)
118 1.4.8.2 christos {
119 1.4.8.2 christos const uint32_t counter_mask = __BIT(armv8_pmu_counter);
120 1.4.8.2 christos
121 1.4.8.2 christos /* Disable overflow interrupts */
122 1.4.8.2 christos reg_pmintenclr_el1_write(counter_mask);
123 1.4.8.2 christos
124 1.4.8.2 christos /* Disable event counter */
125 1.4.8.2 christos reg_pmcntenclr_el0_write(counter_mask);
126 1.4.8.2 christos }
127 1.4.8.2 christos
128 1.4.8.2 christos static uint64_t
129 1.4.8.2 christos armv8_pmu_estimate_freq(void)
130 1.4.8.2 christos {
131 1.4.8.2 christos uint64_t cpufreq = curcpu()->ci_data.cpu_cc_freq;
132 1.4.8.2 christos uint64_t freq = 10000;
133 1.4.8.2 christos uint32_t pmcr;
134 1.4.8.2 christos
135 1.4.8.2 christos counter_val = cpufreq / freq;
136 1.4.8.2 christos if (counter_val == 0)
137 1.4.8.2 christos counter_val = 4000000000ULL / freq;
138 1.4.8.2 christos
139 1.4.8.2 christos pmcr = reg_pmcr_el0_read();
140 1.4.8.2 christos if (pmcr & PMCR_D)
141 1.4.8.2 christos counter_val /= 64;
142 1.4.8.2 christos
143 1.4.8.2 christos return freq;
144 1.4.8.2 christos }
145 1.4.8.2 christos
146 1.4.8.2 christos static uint32_t
147 1.4.8.2 christos armv8_pmu_ident(void)
148 1.4.8.2 christos {
149 1.4.8.2 christos return TPROF_IDENT_ARMV8_GENERIC;
150 1.4.8.2 christos }
151 1.4.8.2 christos
152 1.4.8.2 christos static int
153 1.4.8.2 christos armv8_pmu_start(const tprof_param_t *param)
154 1.4.8.2 christos {
155 1.4.8.2 christos uint64_t xc;
156 1.4.8.2 christos
157 1.4.8.2 christos if (!armv8_pmu_event_implemented(param->p_event)) {
158 1.4.8.2 christos printf("%s: event %#" PRIx64 " not implemented on this CPU\n",
159 1.4.8.2 christos __func__, param->p_event);
160 1.4.8.2 christos return EINVAL;
161 1.4.8.2 christos }
162 1.4.8.2 christos
163 1.4.8.2 christos counter_reset_val = -counter_val + 1;
164 1.4.8.2 christos
165 1.4.8.2 christos armv8_pmu_param = *param;
166 1.4.8.2 christos xc = xc_broadcast(0, armv8_pmu_start_cpu, NULL, NULL);
167 1.4.8.2 christos xc_wait(xc);
168 1.4.8.2 christos
169 1.4.8.2 christos return 0;
170 1.4.8.2 christos }
171 1.4.8.2 christos
172 1.4.8.2 christos static void
173 1.4.8.2 christos armv8_pmu_stop(const tprof_param_t *param)
174 1.4.8.2 christos {
175 1.4.8.2 christos uint64_t xc;
176 1.4.8.2 christos
177 1.4.8.2 christos xc = xc_broadcast(0, armv8_pmu_stop_cpu, NULL, NULL);
178 1.4.8.2 christos xc_wait(xc);
179 1.4.8.2 christos }
180 1.4.8.2 christos
181 1.4.8.2 christos static const tprof_backend_ops_t tprof_armv8_pmu_ops = {
182 1.4.8.2 christos .tbo_estimate_freq = armv8_pmu_estimate_freq,
183 1.4.8.2 christos .tbo_ident = armv8_pmu_ident,
184 1.4.8.2 christos .tbo_start = armv8_pmu_start,
185 1.4.8.2 christos .tbo_stop = armv8_pmu_stop,
186 1.4.8.2 christos };
187 1.4.8.2 christos
188 1.4.8.2 christos int
189 1.4.8.2 christos armv8_pmu_intr(void *priv)
190 1.4.8.2 christos {
191 1.4.8.2 christos const struct trapframe * const tf = priv;
192 1.4.8.2 christos const uint32_t counter_mask = __BIT(armv8_pmu_counter);
193 1.4.8.2 christos tprof_frame_info_t tfi;
194 1.4.8.2 christos
195 1.4.8.2 christos const uint32_t pmovs = reg_pmovsset_el0_read();
196 1.4.8.2 christos if ((pmovs & counter_mask) != 0) {
197 1.4.8.2 christos tfi.tfi_pc = tf->tf_pc;
198 1.4.8.2 christos tfi.tfi_inkernel = tfi.tfi_pc >= VM_MIN_KERNEL_ADDRESS &&
199 1.4.8.2 christos tfi.tfi_pc < VM_MAX_KERNEL_ADDRESS;
200 1.4.8.2 christos tprof_sample(NULL, &tfi);
201 1.4.8.2 christos
202 1.4.8.2 christos armv8_pmu_set_pmevcntr(armv8_pmu_counter, counter_reset_val);
203 1.4.8.2 christos }
204 1.4.8.2 christos reg_pmovsclr_el0_write(pmovs);
205 1.4.8.2 christos
206 1.4.8.2 christos return 1;
207 1.4.8.2 christos }
208 1.4.8.2 christos
209 1.4.8.2 christos int
210 1.4.8.2 christos armv8_pmu_init(void)
211 1.4.8.2 christos {
212 1.4.8.2 christos /* Disable EL0 access to performance monitors */
213 1.4.8.2 christos reg_pmuserenr_el0_write(0);
214 1.4.8.2 christos
215 1.4.8.2 christos /* Disable interrupts */
216 1.4.8.2 christos reg_pmintenclr_el1_write(~0U);
217 1.4.8.2 christos
218 1.4.8.3 martin /* Disable event counters */
219 1.4.8.3 martin reg_pmcntenclr_el0_write(PMCNTEN_P);
220 1.4.8.2 christos
221 1.4.8.2 christos return tprof_backend_register("tprof_armv8", &tprof_armv8_pmu_ops,
222 1.4.8.2 christos TPROF_BACKEND_VERSION);
223 1.4.8.2 christos }
224