Skip to content

[ot] hw/opentitan: rstmgr: defer reset bus and align combo timing - #342

Open
sasdf wants to merge 1 commit into
lowRISC:ot-10.2.0-devfrom
sasdf:mjTrntlwrpt
Open

sasdf wants to merge 1 commit into
lowRISC:ot-10.2.0-devfrom
sasdf:mjTrntlwrpt

Conversation

@sasdf

@sasdf sasdf commented Nov 3, 2026

Copy link
Copy Markdown

Fix race conditions and timing discrepancies during reset handling in ot_rstmgr and ot_sysrst_ctrl:

  • In ot_rstmgr_reset_bus: when called while the vCPU is running (s->cpu->running), queue the bus reset via async_run_on_cpu and issue cpu_exit(s->cpu). This prevents synchronous reset handling during an in-flight TCG translation block, avoiding PC corruption (0x8080 -> 0x8084 via CF_PCREL +4 epilogue) and subsequent illegal instruction exceptions in mask ROM.
  • In ot_rstmgr_reset_req: when on the vCPU thread (qemu_cpu_is_self), only call cpu_exit(s->cpu) instead of cpu_pause(s->cpu) to avoid prematurely setting s->cpu->stopped = true before the execution loop has unwound.
  • Clean up the vCPU stop-wait logic in ot_rstmgr_reset_bus to guard the wait loop on !qemu_cpu_is_self && !s->cpu->stopped, avoiding self-deadlock and ensuring s->cpu->stop = false across all paths.
  • In ot_sysrst_ctrl: switch combo_rst_timer from QEMU_CLOCK_REALTIME + hardcoded 3 ms to OT_VIRTUAL_CLOCK using (rst_det_cyc + 1) * 5000 ns to accurately match the 200 kHz AON clock cycle timing in COM_DET_CTL.
  • In ot_sysrst_ctrl: delete combo_rst_timer and lower rst_req on combo deassertion (prev_trig && !curr_trig), matching sysrst_ctrl_combo.sv, and remove ungrounded timer re-arming on input pin changes.

Fix race conditions and timing discrepancies during reset handling in
`ot_rstmgr` and `ot_sysrst_ctrl`:

- In `ot_rstmgr_reset_bus`: when called while the vCPU is running
  (`s->cpu->running`), queue the bus reset via `async_run_on_cpu` and issue
  `cpu_exit(s->cpu)`. This prevents synchronous reset handling during an
  in-flight TCG translation block, avoiding PC corruption (`0x8080` ->
  `0x8084` via `CF_PCREL` `+4` epilogue) and subsequent illegal instruction
  exceptions in mask ROM.
- In `ot_rstmgr_reset_req`: when on the vCPU thread (`qemu_cpu_is_self`),
  only call `cpu_exit(s->cpu)` instead of `cpu_pause(s->cpu)` to avoid
  prematurely setting `s->cpu->stopped = true` before the execution loop has
  unwound.
- Clean up the vCPU stop-wait logic in `ot_rstmgr_reset_bus` to guard the
  wait loop on `!qemu_cpu_is_self && !s->cpu->stopped`, avoiding self-deadlock
  and ensuring `s->cpu->stop = false` across all paths.
- In `ot_sysrst_ctrl`: switch `combo_rst_timer` from `QEMU_CLOCK_REALTIME` +
  hardcoded 3 ms to `OT_VIRTUAL_CLOCK` using `(rst_det_cyc + 1) * 5000 ns`
  to accurately match the 200 kHz AON clock cycle timing in `COM_DET_CTL`.
- In `ot_sysrst_ctrl`: delete `combo_rst_timer` and lower `rst_req` on combo
  deassertion (`prev_trig && !curr_trig`), matching `sysrst_ctrl_combo.sv`,
  and remove ungrounded timer re-arming on input pin changes.

Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com>
Signed-off-by: Yi-Hsuan Deng <yhdeng@google.com>
Change-Id: I8c6e38a670d60bedf5133ebd6ea32d046a6a6964
Comment thread hw/opentitan/ot_rstmgr.c

static void ot_rstmgr_reset_bus(void *opaque);

static void ot_rstmgr_reset_bus_on_cpu(CPUState *cpu, run_on_cpu_data data)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: probably you can move this function down so we don't define the dummy ot_rstmgr_reset_bus

@siemen11 siemen11 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you @sasdf, the code is not known to me, so I just let AI review it against the RTL here. Feel free to reach out and explain more, then I can review better

} else if (prev_trig && !curr_trig) {
if (out_ctl & R_COM_OUT_CTL_0_RST_REQ_MASK) {
timer_del(s->combo_rst_timer);
ibex_irq_set(&s->rst_req, 0);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In sysrst_ctrl_comboact.sv, rst_req_q is actually latched (assign rst_req_d = rst_req_q | combo_ot_pulse) and only cleared on reset (!rst_ni, which ot_sysrst_ctrl_reset_enter already handles), hence releasing the combo keys shouldn't lower rst_req here (only timer_del(s->combo_rst_timer) should happen if the detection timer hasn't expired yet)

qemu_clock_get_ns(QEMU_CLOCK_REALTIME) +
(rst_det_cyc > 0u ? 3000000LL : 0LL));
qemu_clock_get_ns(OT_VIRTUAL_CLOCK) +
(int64_t)(rst_det_cyc + 1u) * 5000LL);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: ((int64_t)rst_det_cyc + 1) * 5000LL to avoid 32-bit unsigned overflow if rst_det_cyc == UINT32_MAX (also IIUC u_sysrst_ctrl_detect in sysrst_ctrl_combo.sv counts key_intr_debounce_ctl + 1 cycles in DebounceSt before DetectSt, in case we want to include that too)

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