Skip to content

hpcp: gate per-counter enables on combinational cnt_mode_dis_pre#53

Open
flaviens wants to merge 1 commit into
XUANTIE-RV:mainfrom
flaviens:hpcp-priv-gate-fix
Open

hpcp: gate per-counter enables on combinational cnt_mode_dis_pre#53
flaviens wants to merge 1 commit into
XUANTIE-RV:mainfrom
flaviens:hpcp-priv-gate-fix

Conversation

@flaviens
Copy link
Copy Markdown

@flaviens flaviens commented May 4, 2026

Hi!

The pmdm/pmds/pmdu bits silence the hardware perf counters in M, S, and U mode respectively. Today every per-counter enable in ct_hpcp_top.v gates on the registered cnt_mode_dis, which lags the combinational cnt_mode_dis_pre by one cycle on every privilege-mode change. Each transition into a disabled mode counts one extra event that should have been silenced, and each transition out misses one event that should have been counted.

Drive the per-counter enables and the exported hpcp_xx_cnt_en from cnt_mode_dis_pre directly. The clock-gating logic at line 1086 already uses (cnt_mode_dis_pre ^ cnt_mode_dis) to keep the block clocked across the transition cycle, so removing the one-cycle lag from the counter enables does not change clock activity. The registered cnt_mode_dis is left in place because that XOR depends on it.

Affected sites in C910_RTL_FACTORY/gen_rtl/pmu/rtl/ct_hpcp_top.v:

  • 18 per-counter enables (mcycle_en, minstret_en, mhpmcnt3..mhpmcnt18)
  • 4 L2 counter enables (l2cnt_{ra,rm,wa,wm}_cnt_en)
  • the exported global enable hpcp_xx_cnt_en

Thanks!
Flavien

Copilot AI review requested due to automatic review settings May 4, 2026 03:42
The `pmdm`/`pmds`/`pmdu` bits silence the hardware perf counters in M, S,
and U mode respectively. Today every per-counter enable in
`ct_hpcp_top.v` gates on the registered `cnt_mode_dis`, which lags the
combinational `cnt_mode_dis_pre` by one cycle on every privilege-mode
change. Each transition into a disabled mode counts one extra event that
should have been silenced, and each transition out misses one event that
should have been counted.

Drive the per-counter enables and the exported `hpcp_xx_cnt_en` from
`cnt_mode_dis_pre` directly. The clock-gating logic at line 1086 already
uses `(cnt_mode_dis_pre ^ cnt_mode_dis)` to keep the block clocked
across the transition cycle, so removing the one-cycle lag from the
counter enables does not change clock activity. The registered
`cnt_mode_dis` is left in place because that XOR depends on it.

Affected sites in `C910_RTL_FACTORY/gen_rtl/pmu/rtl/ct_hpcp_top.v`:
- 18 per-counter enables (mcycle_en, minstret_en, mhpmcnt3..mhpmcnt18)
- 4 L2 counter enables (l2cnt_{ra,rm,wa,wm}_cnt_en)
- the exported global enable hpcp_xx_cnt_en
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a one-cycle lag in how the PMU’s per-counter enables react to privilege-mode changes by gating enables directly on the combinational cnt_mode_dis_pre (instead of the registered cnt_mode_dis). This ensures counters are silenced/enabled on the correct transition cycle when pmdm/pmds/pmdu disable counting in M/S/U mode.

Changes:

  • Drive all per-counter enables (mcycle, minstret, mhpmcnt3..18) from !cnt_mode_dis_pre instead of !cnt_mode_dis.
  • Drive the L2 counter enables (l2cnt_{ra,rm,wa,wm}_cnt_en) from !cnt_mode_dis_pre instead of !cnt_mode_dis.
  • Drive the exported global enable hpcp_xx_cnt_en from !cnt_mode_dis_pre instead of !cnt_mode_dis.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants