mbox series

[v3,0/3] lib: sbi: Fix Smrnmi resume init and optional platform callback

Message ID 20260915-smrnmi-context-restore-v3-0-e9dd31152515@sifive.com
Headers show
Series lib: sbi: Fix Smrnmi resume init and optional platform callback | expand

Message

Nia Su Sept. 16, 2026, 5:56 a.m. UTC
On non-retentive suspend resume, the hart never initializes the
Smrnmi CSRs the way hart_detect_features() does at boot, so a hart
waking from non-retentive suspend resumes with NMIs disabled and no
RNMI handler installed.

Patch 1 extracts the setup into sbi_smrnmi_hart_init() and calls it
from both hart_detect_features() and sbi_hart_reinit().

Patch 2 changes smrnmi_handlers_init to return int, so a failed
callback no longer gets ignored before NMIE is enabled anyway. It
also documents the SBI_OK/error return convention and aligns the
neighboring rnmi_handler comment to use SBI_OK as well.

Patch 3 allows a NULL smrnmi_handlers_init only when the platform
sets the new SBI_PLATFORM_HAS_FIXED_SMRNMI_VECTOR feature flag, so a
platform with a fixed or mtvec-based NMI vector can say so
explicitly instead of a bare NULL being indistinguishable from a
callback the platform simply forgot to set. It also reports the init
mode ("Platform" or "Default") in the boot HART banner.

Signed-off-by: Nia Su <nia.su@sifive.com>
---
Changes in v3:
- Patch 1: call sbi_smrnmi_hart_init() early in sbi_hart_reinit();
  document that smrnmi_handlers_init may run twice on cold boot.
- Patch 2: document SBI_OK/error return convention on
  smrnmi_handlers_init; align rnmi_handler comment to use SBI_OK too.
- Patch 3: gate NULL smrnmi_handlers_init behind new
  SBI_PLATFORM_HAS_FIXED_SMRNMI_VECTOR flag instead of accepting it
  unconditionally.
- Link to v2: https://lore.kernel.org/r/20260903-smrnmi-context-restore-v2-0-10b17506d257@sifive.com

Changes in v2:
- Break v1's patch 1 into three smaller, self-contained patches for
  a clearer structure.
- Dropped v1's patch 2, which is unrelated to this fix and will be
  sent separately.
- Link to v1: https://lore.kernel.org/opensbi/20260827-rnmi-trap-fixes-v1-0-df371cf92301@sifive.com/

---
Nia Su (3):
      lib: sbi: extract sbi_smrnmi_hart_init() and call on non-retentive resume
      lib: sbi: report smrnmi_handlers_init failures instead of ignoring them
      lib: sbi: allow NULL smrnmi_handlers_init and report init mode at boot

 include/sbi/sbi_platform.h | 19 ++++++++++---
 lib/sbi/sbi_hart.c         | 68 ++++++++++++++++++++++++++++++----------------
 lib/sbi/sbi_init.c         |  9 ++++++
 3 files changed, 69 insertions(+), 27 deletions(-)
---
base-commit: 35511bc6ee1c9c17b6a89b44c52e2044bb51b979
change-id: 20260901-smrnmi-context-restore-b3b1f23bec28

--

Comments

Anup Patel Nov. 3, 2026, 7:53 a.m. UTC | #1
On Wed, Sep 16, 2026 at 11:26 AM Nia Su <nia.su@sifive.com> wrote:
>
> On non-retentive suspend resume, the hart never initializes the
> Smrnmi CSRs the way hart_detect_features() does at boot, so a hart
> waking from non-retentive suspend resumes with NMIs disabled and no
> RNMI handler installed.
>
> Patch 1 extracts the setup into sbi_smrnmi_hart_init() and calls it
> from both hart_detect_features() and sbi_hart_reinit().
>
> Patch 2 changes smrnmi_handlers_init to return int, so a failed
> callback no longer gets ignored before NMIE is enabled anyway. It
> also documents the SBI_OK/error return convention and aligns the
> neighboring rnmi_handler comment to use SBI_OK as well.
>
> Patch 3 allows a NULL smrnmi_handlers_init only when the platform
> sets the new SBI_PLATFORM_HAS_FIXED_SMRNMI_VECTOR feature flag, so a
> platform with a fixed or mtvec-based NMI vector can say so
> explicitly instead of a bare NULL being indistinguishable from a
> callback the platform simply forgot to set. It also reports the init
> mode ("Platform" or "Default") in the boot HART banner.
>
> Signed-off-by: Nia Su <nia.su@sifive.com>
> ---
> Changes in v3:
> - Patch 1: call sbi_smrnmi_hart_init() early in sbi_hart_reinit();
>   document that smrnmi_handlers_init may run twice on cold boot.
> - Patch 2: document SBI_OK/error return convention on
>   smrnmi_handlers_init; align rnmi_handler comment to use SBI_OK too.
> - Patch 3: gate NULL smrnmi_handlers_init behind new
>   SBI_PLATFORM_HAS_FIXED_SMRNMI_VECTOR flag instead of accepting it
>   unconditionally.
> - Link to v2: https://lore.kernel.org/r/20260903-smrnmi-context-restore-v2-0-10b17506d257@sifive.com
>
> Changes in v2:
> - Break v1's patch 1 into three smaller, self-contained patches for
>   a clearer structure.
> - Dropped v1's patch 2, which is unrelated to this fix and will be
>   sent separately.
> - Link to v1: https://lore.kernel.org/opensbi/20260827-rnmi-trap-fixes-v1-0-df371cf92301@sifive.com/
>
> ---
> Nia Su (3):
>       lib: sbi: extract sbi_smrnmi_hart_init() and call on non-retentive resume
>       lib: sbi: report smrnmi_handlers_init failures instead of ignoring them
>       lib: sbi: allow NULL smrnmi_handlers_init and report init mode at boot

I have renamed sbi_smrnmi_hart_init() to smrnmi_hart_init() at the time
of merging. Otherwise, LGTM.

Reviewed-by: Anup Patel <anup@brainfault.org>

Applied this series to the riscv/opensbi repo.

Thanks,
Anup