[PATCH v2 2/3] riscv: sbi: Register SRST handlers with firmware priority
From: Anand Moon
Date: Wed Nov 18 2026 - 05:50:19 EST
The SBI System Reset (SRST) extension currently registers its poweroff
handler using register_platform_power_off(), which assigns it a lower
priority than the default PMIC poweroff handlers.
On platforms where OpenSBI handles system poweroff, a PMIC handler may
run first and perform I2C-backed regmap operations in atomic context,
triggering invalid wait context and scheduling-while-atomic warnings.
Register both SBI SRST restart and poweroff handlers using
register_sys_off_handler() with SYS_OFF_PRIO_FIRMWARE. This gives firmware
handlers precedence over default-priority PMIC handlers and avoids
board-specific workarounds in individual PMIC drivers.
Defer handler registration to a device initcall because sbi_init() runs
during early architecture initialization, before dynamic memory
allocation required by register_sys_off_handler() is available.
Preserve warm and cold reboot selection, retain the existing legacy
poweroff fallback, and report handler registration failures.
Cc: Troy Mitchell <troy.mitchell@xxxxxxxxxxxxxxxxxx>
Cc: Aurelien Jarno <aurelien@xxxxxxxxxxx>
Suggested-by: Aurelien Jarno <aurelien@xxxxxxxxxxx>
Signed-off-by: Anand Moon <linux.amoon@xxxxxxxxx>
---
New patch:
Link: https://lore.kernel.org/all/20261110061429.2036-2-linux.amoon@xxxxxxxxx/
---
arch/riscv/kernel/sbi.c | 43 +++++++++++++++++++++++++++++++----------
1 file changed, 33 insertions(+), 10 deletions(-)
diff --git a/arch/riscv/kernel/sbi.c b/arch/riscv/kernel/sbi.c
index af1ccea222f1..ab6e5bf25411 100644
--- a/arch/riscv/kernel/sbi.c
+++ b/arch/riscv/kernel/sbi.c
@@ -512,23 +512,50 @@ static void sbi_srst_reset(unsigned long type, unsigned long reason)
__func__, type, reason);
}
-static int sbi_srst_reboot(struct notifier_block *this,
- unsigned long mode, void *cmd)
+static int sbi_srst_reboot(struct sys_off_data *data)
{
- sbi_srst_reset((mode == REBOOT_WARM || mode == REBOOT_SOFT) ?
+ sbi_srst_reset((reboot_mode == REBOOT_WARM ||
+ reboot_mode == REBOOT_SOFT) ?
SBI_SRST_RESET_TYPE_WARM_REBOOT :
SBI_SRST_RESET_TYPE_COLD_REBOOT,
SBI_SRST_RESET_REASON_NONE);
+
return NOTIFY_DONE;
}
-static struct notifier_block sbi_srst_reboot_nb;
-
-static void sbi_srst_power_off(void)
+static int sbi_srst_power_off(struct sys_off_data *data)
{
sbi_srst_reset(SBI_SRST_RESET_TYPE_SHUTDOWN,
SBI_SRST_RESET_REASON_NONE);
+
+ return NOTIFY_DONE;
+}
+
+static int __init sbi_srst_sys_off_init(void)
+{
+ struct sys_off_handler *handler;
+
+ if (sbi_spec_version < sbi_mk_version(0, 3) ||
+ !sbi_probe_extension(SBI_EXT_SRST))
+ return 0;
+
+ handler = register_sys_off_handler(SYS_OFF_MODE_RESTART,
+ SYS_OFF_PRIO_FIRMWARE,
+ sbi_srst_reboot, NULL);
+ if (IS_ERR(handler))
+ pr_warn("Failed to register SBI SRST restart handler: %ld\n",
+ PTR_ERR(handler));
+
+ handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
+ SYS_OFF_PRIO_FIRMWARE,
+ sbi_srst_power_off, NULL);
+ if (IS_ERR(handler))
+ pr_warn("Failed to register SBI SRST poweroff handler: %ld\n",
+ PTR_ERR(handler));
+
+ return 0;
}
+device_initcall(sbi_srst_sys_off_init);
/**
* sbi_probe_extension() - Check if an SBI extension ID is supported or not.
@@ -677,11 +704,7 @@ void __init sbi_init(void)
if (sbi_spec_version >= sbi_mk_version(0, 3) &&
sbi_probe_extension(SBI_EXT_SRST)) {
pr_info("SBI SRST extension detected\n");
- register_platform_power_off(sbi_srst_power_off);
srst_power_off = true;
- sbi_srst_reboot_nb.notifier_call = sbi_srst_reboot;
- sbi_srst_reboot_nb.priority = 192;
- register_restart_handler(&sbi_srst_reboot_nb);
}
if (sbi_spec_version >= sbi_mk_version(2, 0) &&
sbi_probe_extension(SBI_EXT_DBCN) > 0) {
--
2.56.0