4 * Copyright (c) 2021 Western Digital Corporation or its affiliates.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "qemu/error-report.h"
24 #include "sysemu/cpu-timers.h"
25 #include "sysemu/device_tree.h"
27 #define RISCV_TIMEBASE_FREQ 1000000000 /* 1Ghz */
30 * To keep it simple, any event can be mapped to any programmable counters in
31 * QEMU. The generic cycle & instruction count events can also be monitored
32 * using programmable counters. In that case, mcycle & minstret must continue
33 * to provide the correct value as well. Heterogeneous PMU per hart is not
34 * supported yet. Thus, number of counters are same across all harts.
36 void riscv_pmu_generate_fdt_node(void *fdt
, uint32_t cmask
, char *pmu_name
)
38 uint32_t fdt_event_ctr_map
[15] = {};
41 * The event encoding is specified in the SBI specification
42 * Event idx is a 20bits wide number encoded as follows:
43 * event_idx[19:16] = type
44 * event_idx[15:0] = code
45 * The code field in cache events are encoded as follows:
46 * event_idx.code[15:3] = cache_id
47 * event_idx.code[2:1] = op_id
48 * event_idx.code[0:0] = result_id
51 /* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */
52 fdt_event_ctr_map
[0] = cpu_to_be32(0x00000001);
53 fdt_event_ctr_map
[1] = cpu_to_be32(0x00000001);
54 fdt_event_ctr_map
[2] = cpu_to_be32(cmask
| 1 << 0);
56 /* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */
57 fdt_event_ctr_map
[3] = cpu_to_be32(0x00000002);
58 fdt_event_ctr_map
[4] = cpu_to_be32(0x00000002);
59 fdt_event_ctr_map
[5] = cpu_to_be32(cmask
| 1 << 2);
61 /* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x00 type(0x01) */
62 fdt_event_ctr_map
[6] = cpu_to_be32(0x00010019);
63 fdt_event_ctr_map
[7] = cpu_to_be32(0x00010019);
64 fdt_event_ctr_map
[8] = cpu_to_be32(cmask
);
66 /* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x00 type(0x01) */
67 fdt_event_ctr_map
[9] = cpu_to_be32(0x0001001B);
68 fdt_event_ctr_map
[10] = cpu_to_be32(0x0001001B);
69 fdt_event_ctr_map
[11] = cpu_to_be32(cmask
);
71 /* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x00 type(0x01) */
72 fdt_event_ctr_map
[12] = cpu_to_be32(0x00010021);
73 fdt_event_ctr_map
[13] = cpu_to_be32(0x00010021);
74 fdt_event_ctr_map
[14] = cpu_to_be32(cmask
);
76 /* This a OpenSBI specific DT property documented in OpenSBI docs */
77 qemu_fdt_setprop(fdt
, pmu_name
, "riscv,event-to-mhpmcounters",
78 fdt_event_ctr_map
, sizeof(fdt_event_ctr_map
));
81 static bool riscv_pmu_counter_valid(RISCVCPU
*cpu
, uint32_t ctr_idx
)
83 if (ctr_idx
< 3 || ctr_idx
>= RV_MAX_MHPMCOUNTERS
||
84 !(cpu
->pmu_avail_ctrs
& BIT(ctr_idx
))) {
91 static bool riscv_pmu_counter_enabled(RISCVCPU
*cpu
, uint32_t ctr_idx
)
93 CPURISCVState
*env
= &cpu
->env
;
95 if (riscv_pmu_counter_valid(cpu
, ctr_idx
) &&
96 !get_field(env
->mcountinhibit
, BIT(ctr_idx
))) {
103 static int riscv_pmu_incr_ctr_rv32(RISCVCPU
*cpu
, uint32_t ctr_idx
)
105 CPURISCVState
*env
= &cpu
->env
;
106 target_ulong max_val
= UINT32_MAX
;
107 PMUCTRState
*counter
= &env
->pmu_ctrs
[ctr_idx
];
108 bool virt_on
= env
->virt_enabled
;
110 /* Privilege mode filtering */
111 if ((env
->priv
== PRV_M
&&
112 (env
->mhpmeventh_val
[ctr_idx
] & MHPMEVENTH_BIT_MINH
)) ||
113 (env
->priv
== PRV_S
&& virt_on
&&
114 (env
->mhpmeventh_val
[ctr_idx
] & MHPMEVENTH_BIT_VSINH
)) ||
115 (env
->priv
== PRV_U
&& virt_on
&&
116 (env
->mhpmeventh_val
[ctr_idx
] & MHPMEVENTH_BIT_VUINH
)) ||
117 (env
->priv
== PRV_S
&& !virt_on
&&
118 (env
->mhpmeventh_val
[ctr_idx
] & MHPMEVENTH_BIT_SINH
)) ||
119 (env
->priv
== PRV_U
&& !virt_on
&&
120 (env
->mhpmeventh_val
[ctr_idx
] & MHPMEVENTH_BIT_UINH
))) {
124 /* Handle the overflow scenario */
125 if (counter
->mhpmcounter_val
== max_val
) {
126 if (counter
->mhpmcounterh_val
== max_val
) {
127 counter
->mhpmcounter_val
= 0;
128 counter
->mhpmcounterh_val
= 0;
129 /* Generate interrupt only if OF bit is clear */
130 if (!(env
->mhpmeventh_val
[ctr_idx
] & MHPMEVENTH_BIT_OF
)) {
131 env
->mhpmeventh_val
[ctr_idx
] |= MHPMEVENTH_BIT_OF
;
132 riscv_cpu_update_mip(env
, MIP_LCOFIP
, BOOL_TO_MASK(1));
135 counter
->mhpmcounterh_val
++;
138 counter
->mhpmcounter_val
++;
144 static int riscv_pmu_incr_ctr_rv64(RISCVCPU
*cpu
, uint32_t ctr_idx
)
146 CPURISCVState
*env
= &cpu
->env
;
147 PMUCTRState
*counter
= &env
->pmu_ctrs
[ctr_idx
];
148 uint64_t max_val
= UINT64_MAX
;
149 bool virt_on
= env
->virt_enabled
;
151 /* Privilege mode filtering */
152 if ((env
->priv
== PRV_M
&&
153 (env
->mhpmevent_val
[ctr_idx
] & MHPMEVENT_BIT_MINH
)) ||
154 (env
->priv
== PRV_S
&& virt_on
&&
155 (env
->mhpmevent_val
[ctr_idx
] & MHPMEVENT_BIT_VSINH
)) ||
156 (env
->priv
== PRV_U
&& virt_on
&&
157 (env
->mhpmevent_val
[ctr_idx
] & MHPMEVENT_BIT_VUINH
)) ||
158 (env
->priv
== PRV_S
&& !virt_on
&&
159 (env
->mhpmevent_val
[ctr_idx
] & MHPMEVENT_BIT_SINH
)) ||
160 (env
->priv
== PRV_U
&& !virt_on
&&
161 (env
->mhpmevent_val
[ctr_idx
] & MHPMEVENT_BIT_UINH
))) {
165 /* Handle the overflow scenario */
166 if (counter
->mhpmcounter_val
== max_val
) {
167 counter
->mhpmcounter_val
= 0;
168 /* Generate interrupt only if OF bit is clear */
169 if (!(env
->mhpmevent_val
[ctr_idx
] & MHPMEVENT_BIT_OF
)) {
170 env
->mhpmevent_val
[ctr_idx
] |= MHPMEVENT_BIT_OF
;
171 riscv_cpu_update_mip(env
, MIP_LCOFIP
, BOOL_TO_MASK(1));
174 counter
->mhpmcounter_val
++;
179 int riscv_pmu_incr_ctr(RISCVCPU
*cpu
, enum riscv_pmu_event_idx event_idx
)
183 CPURISCVState
*env
= &cpu
->env
;
186 if (!cpu
->cfg
.pmu_mask
) {
189 value
= g_hash_table_lookup(cpu
->pmu_event_ctr_map
,
190 GUINT_TO_POINTER(event_idx
));
195 ctr_idx
= GPOINTER_TO_UINT(value
);
196 if (!riscv_pmu_counter_enabled(cpu
, ctr_idx
) ||
197 get_field(env
->mcountinhibit
, BIT(ctr_idx
))) {
201 if (riscv_cpu_mxl(env
) == MXL_RV32
) {
202 ret
= riscv_pmu_incr_ctr_rv32(cpu
, ctr_idx
);
204 ret
= riscv_pmu_incr_ctr_rv64(cpu
, ctr_idx
);
210 bool riscv_pmu_ctr_monitor_instructions(CPURISCVState
*env
,
217 /* Fixed instret counter */
218 if (target_ctr
== 2) {
222 cpu
= env_archcpu(env
);
223 if (!cpu
->pmu_event_ctr_map
) {
227 event_idx
= RISCV_PMU_EVENT_HW_INSTRUCTIONS
;
228 ctr_idx
= GPOINTER_TO_UINT(g_hash_table_lookup(cpu
->pmu_event_ctr_map
,
229 GUINT_TO_POINTER(event_idx
)));
234 return target_ctr
== ctr_idx
? true : false;
237 bool riscv_pmu_ctr_monitor_cycles(CPURISCVState
*env
, uint32_t target_ctr
)
243 /* Fixed mcycle counter */
244 if (target_ctr
== 0) {
248 cpu
= env_archcpu(env
);
249 if (!cpu
->pmu_event_ctr_map
) {
253 event_idx
= RISCV_PMU_EVENT_HW_CPU_CYCLES
;
254 ctr_idx
= GPOINTER_TO_UINT(g_hash_table_lookup(cpu
->pmu_event_ctr_map
,
255 GUINT_TO_POINTER(event_idx
)));
257 /* Counter zero is not used for event_ctr_map */
262 return (target_ctr
== ctr_idx
) ? true : false;
265 static gboolean
pmu_remove_event_map(gpointer key
, gpointer value
,
268 return (GPOINTER_TO_UINT(value
) == GPOINTER_TO_UINT(udata
)) ? true : false;
271 static int64_t pmu_icount_ticks_to_ns(int64_t value
)
275 if (icount_enabled()) {
276 ret
= icount_to_ns(value
);
278 ret
= (NANOSECONDS_PER_SECOND
/ RISCV_TIMEBASE_FREQ
) * value
;
284 int riscv_pmu_update_event_map(CPURISCVState
*env
, uint64_t value
,
288 RISCVCPU
*cpu
= env_archcpu(env
);
290 if (!riscv_pmu_counter_valid(cpu
, ctr_idx
) || !cpu
->pmu_event_ctr_map
) {
295 * Expected mhpmevent value is zero for reset case. Remove the current
299 g_hash_table_foreach_remove(cpu
->pmu_event_ctr_map
,
300 pmu_remove_event_map
,
301 GUINT_TO_POINTER(ctr_idx
));
305 event_idx
= value
& MHPMEVENT_IDX_MASK
;
306 if (g_hash_table_lookup(cpu
->pmu_event_ctr_map
,
307 GUINT_TO_POINTER(event_idx
))) {
312 case RISCV_PMU_EVENT_HW_CPU_CYCLES
:
313 case RISCV_PMU_EVENT_HW_INSTRUCTIONS
:
314 case RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS
:
315 case RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS
:
316 case RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS
:
319 /* We don't support any raw events right now */
322 g_hash_table_insert(cpu
->pmu_event_ctr_map
, GUINT_TO_POINTER(event_idx
),
323 GUINT_TO_POINTER(ctr_idx
));
328 static void pmu_timer_trigger_irq(RISCVCPU
*cpu
,
329 enum riscv_pmu_event_idx evt_idx
)
332 CPURISCVState
*env
= &cpu
->env
;
333 PMUCTRState
*counter
;
334 target_ulong
*mhpmevent_val
;
335 uint64_t of_bit_mask
;
336 int64_t irq_trigger_at
;
338 if (evt_idx
!= RISCV_PMU_EVENT_HW_CPU_CYCLES
&&
339 evt_idx
!= RISCV_PMU_EVENT_HW_INSTRUCTIONS
) {
343 ctr_idx
= GPOINTER_TO_UINT(g_hash_table_lookup(cpu
->pmu_event_ctr_map
,
344 GUINT_TO_POINTER(evt_idx
)));
345 if (!riscv_pmu_counter_enabled(cpu
, ctr_idx
)) {
349 if (riscv_cpu_mxl(env
) == MXL_RV32
) {
350 mhpmevent_val
= &env
->mhpmeventh_val
[ctr_idx
];
351 of_bit_mask
= MHPMEVENTH_BIT_OF
;
353 mhpmevent_val
= &env
->mhpmevent_val
[ctr_idx
];
354 of_bit_mask
= MHPMEVENT_BIT_OF
;
357 counter
= &env
->pmu_ctrs
[ctr_idx
];
358 if (counter
->irq_overflow_left
> 0) {
359 irq_trigger_at
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
360 counter
->irq_overflow_left
;
361 timer_mod_anticipate_ns(cpu
->pmu_timer
, irq_trigger_at
);
362 counter
->irq_overflow_left
= 0;
366 if (cpu
->pmu_avail_ctrs
& BIT(ctr_idx
)) {
367 /* Generate interrupt only if OF bit is clear */
368 if (!(*mhpmevent_val
& of_bit_mask
)) {
369 *mhpmevent_val
|= of_bit_mask
;
370 riscv_cpu_update_mip(env
, MIP_LCOFIP
, BOOL_TO_MASK(1));
375 /* Timer callback for instret and cycle counter overflow */
376 void riscv_pmu_timer_cb(void *priv
)
378 RISCVCPU
*cpu
= priv
;
380 /* Timer event was triggered only for these events */
381 pmu_timer_trigger_irq(cpu
, RISCV_PMU_EVENT_HW_CPU_CYCLES
);
382 pmu_timer_trigger_irq(cpu
, RISCV_PMU_EVENT_HW_INSTRUCTIONS
);
385 int riscv_pmu_setup_timer(CPURISCVState
*env
, uint64_t value
, uint32_t ctr_idx
)
387 uint64_t overflow_delta
, overflow_at
;
388 int64_t overflow_ns
, overflow_left
= 0;
389 RISCVCPU
*cpu
= env_archcpu(env
);
390 PMUCTRState
*counter
= &env
->pmu_ctrs
[ctr_idx
];
392 if (!riscv_pmu_counter_valid(cpu
, ctr_idx
) || !cpu
->cfg
.ext_sscofpmf
) {
397 overflow_delta
= UINT64_MAX
- value
+ 1;
399 overflow_delta
= UINT64_MAX
;
403 * QEMU supports only int64_t timers while RISC-V counters are uint64_t.
404 * Compute the leftover and save it so that it can be reprogrammed again
405 * when timer expires.
407 if (overflow_delta
> INT64_MAX
) {
408 overflow_left
= overflow_delta
- INT64_MAX
;
411 if (riscv_pmu_ctr_monitor_cycles(env
, ctr_idx
) ||
412 riscv_pmu_ctr_monitor_instructions(env
, ctr_idx
)) {
413 overflow_ns
= pmu_icount_ticks_to_ns((int64_t)overflow_delta
);
414 overflow_left
= pmu_icount_ticks_to_ns(overflow_left
) ;
418 overflow_at
= (uint64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
421 if (overflow_at
> INT64_MAX
) {
422 overflow_left
+= overflow_at
- INT64_MAX
;
423 counter
->irq_overflow_left
= overflow_left
;
424 overflow_at
= INT64_MAX
;
426 timer_mod_anticipate_ns(cpu
->pmu_timer
, overflow_at
);
432 void riscv_pmu_init(RISCVCPU
*cpu
, Error
**errp
)
434 if (cpu
->cfg
.pmu_mask
& (COUNTEREN_CY
| COUNTEREN_TM
| COUNTEREN_IR
)) {
435 error_setg(errp
, "\"pmu-mask\" contains invalid bits (0-2) set");
439 if (ctpop32(cpu
->cfg
.pmu_mask
) > (RV_MAX_MHPMCOUNTERS
- 3)) {
440 error_setg(errp
, "Number of counters exceeds maximum available");
444 cpu
->pmu_event_ctr_map
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
445 if (!cpu
->pmu_event_ctr_map
) {
446 error_setg(errp
, "Unable to allocate PMU event hash table");
450 cpu
->pmu_avail_ctrs
= cpu
->cfg
.pmu_mask
;