blob: db9ee8e96b0fead3904ca3c33f14a6033b028b85 [file] [log] [blame]
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +01001/*
2 * PMU emulation helpers for TCG IBM POWER chips
3 *
4 * Copyright IBM Corp. 2021
5 *
6 * Authors:
7 * Daniel Henrique Barboza <danielhb413@gmail.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
13#include "qemu/osdep.h"
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +010014#include "cpu.h"
15#include "helper_regs.h"
16#include "exec/exec-all.h"
17#include "exec/helper-proto.h"
18#include "qemu/error-report.h"
Philippe Mathieu-Daudée78d2f92023-08-28 16:19:30 +020019#include "qemu/timer.h"
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +010020#include "hw/ppc/ppc.h"
Richard Henderson6e8b9902022-01-04 07:55:34 +010021#include "power8-pmu.h"
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +010022
23#if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
24
Daniel Henrique Barboza1474ba62021-12-17 17:57:18 +010025static bool pmc_has_overflow_enabled(CPUPPCState *env, int sprn)
26{
27 if (sprn == SPR_POWER_PMC1) {
28 return env->spr[SPR_POWER_MMCR0] & MMCR0_PMC1CE;
29 }
30
31 return env->spr[SPR_POWER_MMCR0] & MMCR0_PMCjCE;
32}
33
Nicholas Piggin6494d2c2023-05-30 23:04:47 +100034/*
35 * Called after MMCR0 or MMCR1 changes to update pmc_ins_cnt and pmc_cyc_cnt.
36 * hflags must subsequently be updated.
37 */
38static void pmu_update_summaries(CPUPPCState *env)
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +010039{
Richard Henderson6e8b9902022-01-04 07:55:34 +010040 target_ulong mmcr0 = env->spr[SPR_POWER_MMCR0];
41 target_ulong mmcr1 = env->spr[SPR_POWER_MMCR1];
42 int ins_cnt = 0;
43 int cyc_cnt = 0;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +010044
Richard Henderson6e8b9902022-01-04 07:55:34 +010045 if (mmcr0 & MMCR0_FC) {
Nicholas Piggin6494d2c2023-05-30 23:04:47 +100046 goto out;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +010047 }
48
Richard Henderson6e8b9902022-01-04 07:55:34 +010049 if (!(mmcr0 & MMCR0_FC14) && mmcr1 != 0) {
50 target_ulong sel;
51
52 sel = extract64(mmcr1, MMCR1_PMC1EVT_EXTR, MMCR1_EVT_SIZE);
53 switch (sel) {
54 case 0x02:
55 case 0xfe:
56 ins_cnt |= 1 << 1;
57 break;
58 case 0x1e:
59 case 0xf0:
60 cyc_cnt |= 1 << 1;
61 break;
62 }
63
64 sel = extract64(mmcr1, MMCR1_PMC2EVT_EXTR, MMCR1_EVT_SIZE);
65 ins_cnt |= (sel == 0x02) << 2;
66 cyc_cnt |= (sel == 0x1e) << 2;
67
68 sel = extract64(mmcr1, MMCR1_PMC3EVT_EXTR, MMCR1_EVT_SIZE);
69 ins_cnt |= (sel == 0x02) << 3;
70 cyc_cnt |= (sel == 0x1e) << 3;
71
72 sel = extract64(mmcr1, MMCR1_PMC4EVT_EXTR, MMCR1_EVT_SIZE);
73 ins_cnt |= ((sel == 0xfa) || (sel == 0x2)) << 4;
74 cyc_cnt |= (sel == 0x1e) << 4;
75 }
76
77 ins_cnt |= !(mmcr0 & MMCR0_FC56) << 5;
78 cyc_cnt |= !(mmcr0 & MMCR0_FC56) << 6;
79
Nicholas Piggin6494d2c2023-05-30 23:04:47 +100080 out:
Richard Henderson6e8b9902022-01-04 07:55:34 +010081 env->pmc_ins_cnt = ins_cnt;
82 env->pmc_cyc_cnt = cyc_cnt;
Nicholas Piggin6494d2c2023-05-30 23:04:47 +100083}
84
Glenn Miles4de4a472024-03-28 20:41:33 +100085static void hreg_bhrb_filter_update(CPUPPCState *env)
86{
87 target_long ifm;
88
89 if (!(env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE)) {
90 /* disable recording to BHRB */
91 env->bhrb_filter = BHRB_TYPE_NORECORD;
92 return;
93 }
94
95 ifm = (env->spr[SPR_POWER_MMCRA] & MMCRA_IFM_MASK) >> MMCRA_IFM_SHIFT;
96 switch (ifm) {
97 case 0:
98 /* record all branches */
99 env->bhrb_filter = -1;
100 break;
101 case 1:
102 /* only record calls (LK = 1) */
103 env->bhrb_filter = BHRB_TYPE_CALL;
104 break;
105 case 2:
106 /* only record indirect branches */
107 env->bhrb_filter = BHRB_TYPE_INDIRECT;
108 break;
109 case 3:
110 /* only record conditional branches */
111 env->bhrb_filter = BHRB_TYPE_COND;
112 break;
113 }
114}
115
Glenn Milesa7138e22024-03-28 20:41:29 +1000116void pmu_mmcr01a_updated(CPUPPCState *env)
Nicholas Piggin6494d2c2023-05-30 23:04:47 +1000117{
Nicholas Pigginc29b0702023-05-30 23:43:13 +1000118 PowerPCCPU *cpu = env_archcpu(env);
119
Nicholas Piggin6494d2c2023-05-30 23:04:47 +1000120 pmu_update_summaries(env);
121 hreg_update_pmu_hflags(env);
Nicholas Pigginc29b0702023-05-30 23:43:13 +1000122
123 if (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAO) {
124 ppc_set_irq(cpu, PPC_INTERRUPT_PERFM, 1);
125 } else {
126 ppc_set_irq(cpu, PPC_INTERRUPT_PERFM, 0);
127 }
128
Glenn Miles4de4a472024-03-28 20:41:33 +1000129 hreg_bhrb_filter_update(env);
130
Nicholas Piggin6494d2c2023-05-30 23:04:47 +1000131 /*
132 * Should this update overflow timers (if mmcr0 is updated) so they
133 * get set in cpu_post_load?
134 */
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100135}
136
137static bool pmu_increment_insns(CPUPPCState *env, uint32_t num_insns)
138{
Richard Hendersonffae5612022-01-04 07:55:35 +0100139 target_ulong mmcr0 = env->spr[SPR_POWER_MMCR0];
140 unsigned ins_cnt = env->pmc_ins_cnt;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100141 bool overflow_triggered = false;
Richard Hendersonffae5612022-01-04 07:55:35 +0100142 target_ulong tmp;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100143
Leandro Luporieeaaefe2022-10-25 17:24:24 -0300144 if (ins_cnt & (1 << 1)) {
145 tmp = env->spr[SPR_POWER_PMC1];
146 tmp += num_insns;
147 if (tmp >= PMC_COUNTER_NEGATIVE_VAL && (mmcr0 & MMCR0_PMC1CE)) {
148 tmp = PMC_COUNTER_NEGATIVE_VAL;
149 overflow_triggered = true;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100150 }
Leandro Luporieeaaefe2022-10-25 17:24:24 -0300151 env->spr[SPR_POWER_PMC1] = tmp;
152 }
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100153
Leandro Luporieeaaefe2022-10-25 17:24:24 -0300154 if (ins_cnt & (1 << 2)) {
155 tmp = env->spr[SPR_POWER_PMC2];
156 tmp += num_insns;
157 if (tmp >= PMC_COUNTER_NEGATIVE_VAL && (mmcr0 & MMCR0_PMCjCE)) {
158 tmp = PMC_COUNTER_NEGATIVE_VAL;
159 overflow_triggered = true;
160 }
161 env->spr[SPR_POWER_PMC2] = tmp;
162 }
163
164 if (ins_cnt & (1 << 3)) {
165 tmp = env->spr[SPR_POWER_PMC3];
166 tmp += num_insns;
167 if (tmp >= PMC_COUNTER_NEGATIVE_VAL && (mmcr0 & MMCR0_PMCjCE)) {
168 tmp = PMC_COUNTER_NEGATIVE_VAL;
169 overflow_triggered = true;
170 }
171 env->spr[SPR_POWER_PMC3] = tmp;
172 }
173
174 if (ins_cnt & (1 << 4)) {
175 target_ulong mmcr1 = env->spr[SPR_POWER_MMCR1];
176 int sel = extract64(mmcr1, MMCR1_PMC4EVT_EXTR, MMCR1_EVT_SIZE);
177 if (sel == 0x02 || (env->spr[SPR_CTRL] & CTRL_RUN)) {
178 tmp = env->spr[SPR_POWER_PMC4];
Richard Hendersonffae5612022-01-04 07:55:35 +0100179 tmp += num_insns;
180 if (tmp >= PMC_COUNTER_NEGATIVE_VAL && (mmcr0 & MMCR0_PMCjCE)) {
181 tmp = PMC_COUNTER_NEGATIVE_VAL;
182 overflow_triggered = true;
183 }
Leandro Luporieeaaefe2022-10-25 17:24:24 -0300184 env->spr[SPR_POWER_PMC4] = tmp;
Richard Hendersonffae5612022-01-04 07:55:35 +0100185 }
186 }
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100187
Richard Hendersonffae5612022-01-04 07:55:35 +0100188 if (ins_cnt & (1 << 5)) {
189 tmp = env->spr[SPR_POWER_PMC5];
190 tmp += num_insns;
191 if (tmp >= PMC_COUNTER_NEGATIVE_VAL && (mmcr0 & MMCR0_PMCjCE)) {
192 tmp = PMC_COUNTER_NEGATIVE_VAL;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100193 overflow_triggered = true;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100194 }
Richard Hendersonffae5612022-01-04 07:55:35 +0100195 env->spr[SPR_POWER_PMC5] = tmp;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100196 }
197
198 return overflow_triggered;
199}
200
Daniel Henrique Barbozac2eff582021-12-17 17:57:18 +0100201static void pmu_update_cycles(CPUPPCState *env)
202{
203 uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
204 uint64_t time_delta = now - env->pmu_base_time;
Richard Hendersoneec4dfd2022-01-04 07:55:35 +0100205 int sprn, cyc_cnt = env->pmc_cyc_cnt;
Daniel Henrique Barbozac2eff582021-12-17 17:57:18 +0100206
207 for (sprn = SPR_POWER_PMC1; sprn <= SPR_POWER_PMC6; sprn++) {
Richard Hendersoneec4dfd2022-01-04 07:55:35 +0100208 if (cyc_cnt & (1 << (sprn - SPR_POWER_PMC1 + 1))) {
209 /*
210 * The pseries and powernv clock runs at 1Ghz, meaning
211 * that 1 nanosec equals 1 cycle.
212 */
213 env->spr[sprn] += time_delta;
Daniel Henrique Barbozac2eff582021-12-17 17:57:18 +0100214 }
Daniel Henrique Barbozac2eff582021-12-17 17:57:18 +0100215 }
216
217 /* Update base_time for future calculations */
218 env->pmu_base_time = now;
219}
220
Daniel Henrique Barboza1474ba62021-12-17 17:57:18 +0100221/*
222 * Helper function to retrieve the cycle overflow timer of the
223 * 'sprn' counter.
224 */
225static QEMUTimer *get_cyc_overflow_timer(CPUPPCState *env, int sprn)
226{
227 return env->pmu_cyc_overflow_timers[sprn - SPR_POWER_PMC1];
228}
229
230static void pmc_update_overflow_timer(CPUPPCState *env, int sprn)
231{
232 QEMUTimer *pmc_overflow_timer = get_cyc_overflow_timer(env, sprn);
233 int64_t timeout;
234
235 /*
236 * PMC5 does not have an overflow timer and this pointer
237 * will be NULL.
238 */
239 if (!pmc_overflow_timer) {
240 return;
241 }
242
Richard Hendersoneec4dfd2022-01-04 07:55:35 +0100243 if (!(env->pmc_cyc_cnt & (1 << (sprn - SPR_POWER_PMC1 + 1))) ||
Daniel Henrique Barboza1474ba62021-12-17 17:57:18 +0100244 !pmc_has_overflow_enabled(env, sprn)) {
245 /* Overflow timer is not needed for this counter */
246 timer_del(pmc_overflow_timer);
247 return;
248 }
249
250 if (env->spr[sprn] >= PMC_COUNTER_NEGATIVE_VAL) {
Richard Hendersoneec4dfd2022-01-04 07:55:35 +0100251 timeout = 0;
Daniel Henrique Barboza1474ba62021-12-17 17:57:18 +0100252 } else {
253 timeout = PMC_COUNTER_NEGATIVE_VAL - env->spr[sprn];
254 }
255
256 /*
257 * Use timer_mod_anticipate() because an overflow timer might
258 * be already running for this PMC.
259 */
260 timer_mod_anticipate(pmc_overflow_timer, env->pmu_base_time + timeout);
261}
262
263static void pmu_update_overflow_timers(CPUPPCState *env)
264{
265 int sprn;
266
267 /*
268 * Scroll through all PMCs and start counter overflow timers for
269 * PM_CYC events, if needed.
270 */
271 for (sprn = SPR_POWER_PMC1; sprn <= SPR_POWER_PMC6; sprn++) {
272 pmc_update_overflow_timer(env, sprn);
273 }
274}
275
Daniel Henrique Barbozaadc4eda2022-03-02 06:51:36 +0100276static void pmu_delete_timers(CPUPPCState *env)
277{
278 QEMUTimer *pmc_overflow_timer;
279 int sprn;
280
281 for (sprn = SPR_POWER_PMC1; sprn <= SPR_POWER_PMC6; sprn++) {
282 pmc_overflow_timer = get_cyc_overflow_timer(env, sprn);
283
284 if (pmc_overflow_timer) {
285 timer_del(pmc_overflow_timer);
286 }
287 }
288}
289
Daniel Henrique Barbozac2eff582021-12-17 17:57:18 +0100290void helper_store_mmcr0(CPUPPCState *env, target_ulong value)
291{
292 pmu_update_cycles(env);
293
294 env->spr[SPR_POWER_MMCR0] = value;
295
Glenn Milesa7138e22024-03-28 20:41:29 +1000296 pmu_mmcr01a_updated(env);
Daniel Henrique Barboza1474ba62021-12-17 17:57:18 +0100297
298 /* Update cycle overflow timers with the current MMCR0 state */
299 pmu_update_overflow_timers(env);
Daniel Henrique Barbozac2eff582021-12-17 17:57:18 +0100300}
301
Daniel Henrique Barbozaa6f91242021-12-17 17:57:18 +0100302void helper_store_mmcr1(CPUPPCState *env, uint64_t value)
303{
304 pmu_update_cycles(env);
305
306 env->spr[SPR_POWER_MMCR1] = value;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100307
Glenn Milesa7138e22024-03-28 20:41:29 +1000308 pmu_mmcr01a_updated(env);
309}
310
311void helper_store_mmcrA(CPUPPCState *env, uint64_t value)
312{
313 env->spr[SPR_POWER_MMCRA] = value;
314
315 pmu_mmcr01a_updated(env);
Daniel Henrique Barbozaa6f91242021-12-17 17:57:18 +0100316}
317
Daniel Henrique Barboza308b9fa2021-12-17 17:57:18 +0100318target_ulong helper_read_pmc(CPUPPCState *env, uint32_t sprn)
319{
320 pmu_update_cycles(env);
321
322 return env->spr[sprn];
323}
324
325void helper_store_pmc(CPUPPCState *env, uint32_t sprn, uint64_t value)
326{
327 pmu_update_cycles(env);
328
Nicholas Pigginfbda88f2023-05-15 19:26:47 +1000329 env->spr[sprn] = (uint32_t)value;
Daniel Henrique Barboza1474ba62021-12-17 17:57:18 +0100330
331 pmc_update_overflow_timer(env, sprn);
Daniel Henrique Barboza308b9fa2021-12-17 17:57:18 +0100332}
333
Nicholas Pigginc29b0702023-05-30 23:43:13 +1000334static void perfm_alert(PowerPCCPU *cpu)
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +0100335{
336 CPUPPCState *env = &cpu->env;
337
Daniel Henrique Barbozaadc4eda2022-03-02 06:51:36 +0100338 pmu_update_cycles(env);
339
340 if (env->spr[SPR_POWER_MMCR0] & MMCR0_FCECE) {
Daniel Henrique Barbozaadc4eda2022-03-02 06:51:36 +0100341 env->spr[SPR_POWER_MMCR0] |= MMCR0_FC;
342
Nicholas Piggin6494d2c2023-05-30 23:04:47 +1000343 /* Changing MMCR0_FC requires summaries and hflags update */
Glenn Milesa7138e22024-03-28 20:41:29 +1000344 pmu_mmcr01a_updated(env);
Daniel Henrique Barbozaadc4eda2022-03-02 06:51:36 +0100345
346 /*
347 * Delete all pending timers if we need to freeze
348 * the PMC. We'll restart them when the PMC starts
349 * running again.
350 */
351 pmu_delete_timers(env);
352 }
353
354 if (env->spr[SPR_POWER_MMCR0] & MMCR0_PMAE) {
Nicholas Piggin6494d2c2023-05-30 23:04:47 +1000355 /* These MMCR0 bits do not require summaries or hflags update. */
Daniel Henrique Barbozaadc4eda2022-03-02 06:51:36 +0100356 env->spr[SPR_POWER_MMCR0] &= ~MMCR0_PMAE;
357 env->spr[SPR_POWER_MMCR0] |= MMCR0_PMAO;
Nicholas Pigginc29b0702023-05-30 23:43:13 +1000358 ppc_set_irq(cpu, PPC_INTERRUPT_PERFM, 1);
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +0100359 }
360
Daniel Henrique Barbozad3412df2022-03-02 06:51:36 +0100361 raise_ebb_perfm_exception(env);
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +0100362}
363
Leandro Luporieeaaefe2022-10-25 17:24:24 -0300364void helper_handle_pmc5_overflow(CPUPPCState *env)
365{
366 env->spr[SPR_POWER_PMC5] = PMC_COUNTER_NEGATIVE_VAL;
Nicholas Pigginc29b0702023-05-30 23:43:13 +1000367 perfm_alert(env_archcpu(env));
Leandro Luporieeaaefe2022-10-25 17:24:24 -0300368}
369
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100370/* This helper assumes that the PMC is running. */
371void helper_insns_inc(CPUPPCState *env, uint32_t num_insns)
372{
373 bool overflow_triggered;
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100374
375 overflow_triggered = pmu_increment_insns(env, num_insns);
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100376 if (overflow_triggered) {
Nicholas Pigginc29b0702023-05-30 23:43:13 +1000377 perfm_alert(env_archcpu(env));
Daniel Henrique Barboza46d396b2021-12-17 17:57:18 +0100378 }
379}
380
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +0100381static void cpu_ppc_pmu_timer_cb(void *opaque)
382{
383 PowerPCCPU *cpu = opaque;
384
Nicholas Pigginc29b0702023-05-30 23:43:13 +1000385 perfm_alert(cpu);
Daniel Henrique Barboza8f2e9d42021-12-17 17:57:18 +0100386}
387
388void cpu_ppc_pmu_init(CPUPPCState *env)
389{
390 PowerPCCPU *cpu = env_archcpu(env);
391 int i, sprn;
392
393 for (sprn = SPR_POWER_PMC1; sprn <= SPR_POWER_PMC6; sprn++) {
394 if (sprn == SPR_POWER_PMC5) {
395 continue;
396 }
397
398 i = sprn - SPR_POWER_PMC1;
399
400 env->pmu_cyc_overflow_timers[i] = timer_new_ns(QEMU_CLOCK_VIRTUAL,
401 &cpu_ppc_pmu_timer_cb,
402 cpu);
403 }
404}
405#endif /* defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY) */