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