tprof_armv8.c revision 1.4.8.2 1 1.4.8.2 christos /* $NetBSD: tprof_armv8.c,v 1.4.8.2 2019/06/10 22:07:33 christos 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.2 christos __KERNEL_RCSID(0, "$NetBSD: tprof_armv8.c,v 1.4.8.2 2019/06/10 22:07:33 christos 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.2 christos uint64_t pmcr, pmevtyper;
90 1.4.8.2 christos
91 1.4.8.2 christos /* Enable performance monitor */
92 1.4.8.2 christos pmcr = reg_pmcr_el0_read();
93 1.4.8.2 christos pmcr |= PMCR_E;
94 1.4.8.2 christos reg_pmcr_el0_write(pmcr);
95 1.4.8.2 christos
96 1.4.8.2 christos /* Disable event counter */
97 1.4.8.2 christos reg_pmcntenclr_el0_write(counter_mask);
98 1.4.8.2 christos
99 1.4.8.2 christos /* Configure event counter */
100 1.4.8.2 christos pmevtyper = __SHIFTIN(armv8_pmu_param.p_event, PMEVTYPER_EVTCOUNT);
101 1.4.8.2 christos if (!ISSET(armv8_pmu_param.p_flags, TPROF_PARAM_USER))
102 1.4.8.2 christos pmevtyper |= PMEVTYPER_U;
103 1.4.8.2 christos if (!ISSET(armv8_pmu_param.p_flags, TPROF_PARAM_KERN))
104 1.4.8.2 christos pmevtyper |= PMEVTYPER_P;
105 1.4.8.2 christos
106 1.4.8.2 christos armv8_pmu_set_pmevtyper(armv8_pmu_counter, pmevtyper);
107 1.4.8.2 christos
108 1.4.8.2 christos /* Enable overflow interrupts */
109 1.4.8.2 christos reg_pmintenset_el1_write(counter_mask);
110 1.4.8.2 christos
111 1.4.8.2 christos /* Clear overflow flag */
112 1.4.8.2 christos reg_pmovsclr_el0_write(counter_mask);
113 1.4.8.2 christos
114 1.4.8.2 christos /* Initialize event counter value */
115 1.4.8.2 christos armv8_pmu_set_pmevcntr(armv8_pmu_counter, counter_reset_val);
116 1.4.8.2 christos
117 1.4.8.2 christos /* Enable event counter */
118 1.4.8.2 christos reg_pmcntenset_el0_write(counter_mask);
119 1.4.8.2 christos }
120 1.4.8.2 christos
121 1.4.8.2 christos static void
122 1.4.8.2 christos armv8_pmu_stop_cpu(void *arg1, void *arg2)
123 1.4.8.2 christos {
124 1.4.8.2 christos const uint32_t counter_mask = __BIT(armv8_pmu_counter);
125 1.4.8.2 christos uint32_t pmcr;
126 1.4.8.2 christos
127 1.4.8.2 christos /* Disable overflow interrupts */
128 1.4.8.2 christos reg_pmintenclr_el1_write(counter_mask);
129 1.4.8.2 christos
130 1.4.8.2 christos /* Disable event counter */
131 1.4.8.2 christos reg_pmcntenclr_el0_write(counter_mask);
132 1.4.8.2 christos
133 1.4.8.2 christos /* Disable performance monitor */
134 1.4.8.2 christos pmcr = reg_pmcr_el0_read();
135 1.4.8.2 christos pmcr &= ~PMCR_E;
136 1.4.8.2 christos reg_pmcr_el0_write(pmcr);
137 1.4.8.2 christos }
138 1.4.8.2 christos
139 1.4.8.2 christos static uint64_t
140 1.4.8.2 christos armv8_pmu_estimate_freq(void)
141 1.4.8.2 christos {
142 1.4.8.2 christos uint64_t cpufreq = curcpu()->ci_data.cpu_cc_freq;
143 1.4.8.2 christos uint64_t freq = 10000;
144 1.4.8.2 christos uint32_t pmcr;
145 1.4.8.2 christos
146 1.4.8.2 christos counter_val = cpufreq / freq;
147 1.4.8.2 christos if (counter_val == 0)
148 1.4.8.2 christos counter_val = 4000000000ULL / freq;
149 1.4.8.2 christos
150 1.4.8.2 christos pmcr = reg_pmcr_el0_read();
151 1.4.8.2 christos if (pmcr & PMCR_D)
152 1.4.8.2 christos counter_val /= 64;
153 1.4.8.2 christos
154 1.4.8.2 christos return freq;
155 1.4.8.2 christos }
156 1.4.8.2 christos
157 1.4.8.2 christos static uint32_t
158 1.4.8.2 christos armv8_pmu_ident(void)
159 1.4.8.2 christos {
160 1.4.8.2 christos return TPROF_IDENT_ARMV8_GENERIC;
161 1.4.8.2 christos }
162 1.4.8.2 christos
163 1.4.8.2 christos static int
164 1.4.8.2 christos armv8_pmu_start(const tprof_param_t *param)
165 1.4.8.2 christos {
166 1.4.8.2 christos uint64_t xc;
167 1.4.8.2 christos
168 1.4.8.2 christos if (!armv8_pmu_event_implemented(param->p_event)) {
169 1.4.8.2 christos printf("%s: event %#" PRIx64 " not implemented on this CPU\n",
170 1.4.8.2 christos __func__, param->p_event);
171 1.4.8.2 christos return EINVAL;
172 1.4.8.2 christos }
173 1.4.8.2 christos
174 1.4.8.2 christos counter_reset_val = -counter_val + 1;
175 1.4.8.2 christos
176 1.4.8.2 christos armv8_pmu_param = *param;
177 1.4.8.2 christos xc = xc_broadcast(0, armv8_pmu_start_cpu, NULL, NULL);
178 1.4.8.2 christos xc_wait(xc);
179 1.4.8.2 christos
180 1.4.8.2 christos return 0;
181 1.4.8.2 christos }
182 1.4.8.2 christos
183 1.4.8.2 christos static void
184 1.4.8.2 christos armv8_pmu_stop(const tprof_param_t *param)
185 1.4.8.2 christos {
186 1.4.8.2 christos uint64_t xc;
187 1.4.8.2 christos
188 1.4.8.2 christos xc = xc_broadcast(0, armv8_pmu_stop_cpu, NULL, NULL);
189 1.4.8.2 christos xc_wait(xc);
190 1.4.8.2 christos }
191 1.4.8.2 christos
192 1.4.8.2 christos static const tprof_backend_ops_t tprof_armv8_pmu_ops = {
193 1.4.8.2 christos .tbo_estimate_freq = armv8_pmu_estimate_freq,
194 1.4.8.2 christos .tbo_ident = armv8_pmu_ident,
195 1.4.8.2 christos .tbo_start = armv8_pmu_start,
196 1.4.8.2 christos .tbo_stop = armv8_pmu_stop,
197 1.4.8.2 christos };
198 1.4.8.2 christos
199 1.4.8.2 christos int
200 1.4.8.2 christos armv8_pmu_intr(void *priv)
201 1.4.8.2 christos {
202 1.4.8.2 christos const struct trapframe * const tf = priv;
203 1.4.8.2 christos const uint32_t counter_mask = __BIT(armv8_pmu_counter);
204 1.4.8.2 christos tprof_frame_info_t tfi;
205 1.4.8.2 christos
206 1.4.8.2 christos const uint32_t pmovs = reg_pmovsset_el0_read();
207 1.4.8.2 christos if ((pmovs & counter_mask) != 0) {
208 1.4.8.2 christos tfi.tfi_pc = tf->tf_pc;
209 1.4.8.2 christos tfi.tfi_inkernel = tfi.tfi_pc >= VM_MIN_KERNEL_ADDRESS &&
210 1.4.8.2 christos tfi.tfi_pc < VM_MAX_KERNEL_ADDRESS;
211 1.4.8.2 christos tprof_sample(NULL, &tfi);
212 1.4.8.2 christos
213 1.4.8.2 christos armv8_pmu_set_pmevcntr(armv8_pmu_counter, counter_reset_val);
214 1.4.8.2 christos }
215 1.4.8.2 christos reg_pmovsclr_el0_write(pmovs);
216 1.4.8.2 christos
217 1.4.8.2 christos return 1;
218 1.4.8.2 christos }
219 1.4.8.2 christos
220 1.4.8.2 christos int
221 1.4.8.2 christos armv8_pmu_init(void)
222 1.4.8.2 christos {
223 1.4.8.2 christos /* Disable EL0 access to performance monitors */
224 1.4.8.2 christos reg_pmuserenr_el0_write(0);
225 1.4.8.2 christos
226 1.4.8.2 christos /* Disable interrupts */
227 1.4.8.2 christos reg_pmintenclr_el1_write(~0U);
228 1.4.8.2 christos
229 1.4.8.2 christos /* Disable counters */
230 1.4.8.2 christos reg_pmcntenclr_el0_write(~0U);
231 1.4.8.2 christos
232 1.4.8.2 christos /* Disable performance monitor */
233 1.4.8.2 christos reg_pmcr_el0_write(0);
234 1.4.8.2 christos
235 1.4.8.2 christos return tprof_backend_register("tprof_armv8", &tprof_armv8_pmu_ops,
236 1.4.8.2 christos TPROF_BACKEND_VERSION);
237 1.4.8.2 christos }
238