Skip to contentSkip to conversation

i2c: spacemit: Avoid sleeping during bus reset in atomic context

PATCH In review Anand Moon 2 participants 6 messages Nov 18 – Nov 24

Subsystems (5)
lore
Share & tools
lore

Anand Moon

author
#0 lore raw

PATCH v2 1/3 i2c: spacemit: Avoid sleeping during bus reset in atomic context

The spacemit_i2c_conditionally_reset_bus() function uses usleep_range() to wait after triggering a bus reset request. However, this function can be invoked during atomic I2C transfers (such as power-off or restart handlers running in atomic notifier chains), where sleeping is illegal.

Replace the sleep operation with the driver's internal helper, spacemit_i2c_delay(), which conditionally uses udelay() when running in an atomic context, avoiding "scheduling while atomic" bugs.

Fixes 5dd75dac1b35 ("i2c: spacemit: introduce pio for k1")
Cc Troy Mitchell <[email protected]>
Cc Alex Elder <[email protected]>

---
v1: https://lore.kernel.org/all/[email protected]/
collected all te reviewed-by

---
 drivers/i2c/busses/i2c-k1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-k1.c +1 -1
index ff32876e24d3..f4b21d7efaaf 100644
@@ -332,7 +332,7 @@ static void spacemit_i2c_conditionally_reset_bus(struct spacemit_i2c_dev *i2c)
 		writel(FIELD_PREP(SPACEMIT_RCR_FIELD_RST_CYC, 1),
 		       i2c->base + SPACEMIT_IRCR);
 		writel(SPACEMIT_CR_RSTREQ, i2c->base + SPACEMIT_ICR);
-		usleep_range(20, 30);
+		spacemit_i2c_delay(i2c, 20);
 	}
 
 	/* check sda again here */

base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca

Signature · 1 line
2.56.0
author
#1 lore raw

PATCH v2 2/3 riscv: sbi: Register SRST handlers with firmware priority

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.

---
New patch:

---
 arch/riscv/kernel/sbi.c | 43 +++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)
arch/riscv/kernel/sbi.c +33 -10
index af1ccea222f1..ab6e5bf25411 100644
@@ -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) {
Signature · 1 line
2.56.0
Reviewed-by Tested-by on v2 2/3
#2 lore raw

Re: [PATCH v2 2/3] riscv: sbi: Register SRST handlers with firmware priority

Quoted 94 lines · Anand Moon

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.

---
New patch:

   Link: https://lore.kernel.org/all/[email protected]/
---
 arch/riscv/kernel/sbi.c | 43 +++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)
arch/riscv/kernel/sbi.c +33 -6
index af1ccea222f1..ab6e5bf25411 100644
@@ -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) &&

arch/riscv/kernel/sbi.c void __init sbi_init(void) · line 710

Quoted 10 lines
arch/riscv/kernel/sbi.c +0 -4
 		    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) {
Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
author
#3 lore raw

PATCH v2 3/3 power: reset: Remove SpacemiT P1 reboot driver

In reply to Anand Moon

The SpacemiT P1 reboot driver uses I2C-backed regmap operations to request system shutdown and restart through the PMIC.

The system-off handlers run in atomic context, where regmap operations may sleep. This can trigger invalid wait context and scheduling-while-atomic warnings during shutdown or restart.

On SpacemiT K1 platforms, system poweroff and restart are handled by OpenSBI through the SBI System Reset (SRST) extension. The Linux PMIC reboot driver duplicates this firmware functionality and is no longer needed.

Remove the driver along with its Kconfig and Makefile entries. The P1 MFD driver and its other functionality remain unaffected.

Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>

---
New patch

---
 drivers/power/reset/Kconfig              |  9 ---
 drivers/power/reset/Makefile             |  1 -
 drivers/power/reset/spacemit-p1-reboot.c | 87 ------------------------
 3 files changed, 97 deletions(-)
 delete mode 100644 drivers/power/reset/spacemit-p1-reboot.c
drivers/power/reset/Kconfig +0 -9
index bce996bbef28..9e997d58fcc6 100644
@@ -295,15 +295,6 @@ config POWER_RESET_KEYSTONE
 	help
 	  Reboot support for the KEYSTONE SoCs.
 
-config POWER_RESET_SPACEMIT_P1
-	tristate "SpacemiT P1 poweroff and reset driver"
-	depends on ARCH_SPACEMIT || COMPILE_TEST
-	depends on MFD_SPACEMIT_P1
-	default MFD_SPACEMIT_P1
-	help
-	  This driver supports power-off and reset operations for the SpacemiT
-	  P1 PMIC.
-
 config POWER_RESET_SYSCON
 	bool "Generic SYSCON regmap reset driver"
 	depends on OF
drivers/power/reset/Makefile +0 -1
index e31cab4ba78e..2d5bdd1584cc 100644
@@ -25,7 +25,6 @@ obj-$(CONFIG_POWER_RESET_LTC2952) += ltc2952-poweroff.o
 obj-$(CONFIG_POWER_RESET_QNAP) += qnap-poweroff.o
 obj-$(CONFIG_POWER_RESET_REGULATOR) += regulator-poweroff.o
 obj-$(CONFIG_POWER_RESET_RESTART) += restart-poweroff.o
-obj-$(CONFIG_POWER_RESET_SPACEMIT_P1) += spacemit-p1-reboot.o
 obj-$(CONFIG_POWER_RESET_ST) += st-poweroff.o
 obj-$(CONFIG_POWER_RESET_TH1520_AON) += th1520-aon-reboot.o
 obj-$(CONFIG_POWER_RESET_TORADEX_EC) += tdx-ec-poweroff.o
drivers/power/reset/spacemit-p1-reboot.c deleted +0 -87

87 lines not on this page · raw message

Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH v2 3/3] power: reset: Remove SpacemiT P1 reboot driver

Hi Anand,

Quoted 18 lines · Anand Moon

The SpacemiT P1 reboot driver uses I2C-backed regmap operations to request system shutdown and restart through the PMIC.

The system-off handlers run in atomic context, where regmap operations may sleep. This can trigger invalid wait context and scheduling-while-atomic warnings during shutdown or restart.

On SpacemiT K1 platforms, system poweroff and restart are handled by OpenSBI through the SBI System Reset (SRST) extension. The Linux PMIC reboot driver duplicates this firmware functionality and is no longer needed.

Remove the driver along with its Kconfig and Makefile entries. The P1 MFD driver and its other functionality remain unaffected.

Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Quoted 8 lines

---
New patch

---
 drivers/power/reset/Kconfig              |  9 ---
 drivers/power/reset/Makefile             |  1 -
 drivers/power/reset/spacemit-p1-reboot.c | 87 ------------------------
 3 files changed, 97 deletions(-)
 delete mode 100644 drivers/power/reset/spacemit-p1-reboot.c

The patch is correct, but you might also want to remove the spacemit-p1-reboot entry from drivers/mfd/simple-mfd-i2c.c.

Anyway, while I support removing this driver on the long term, it might be a bit early to remove it. Indeed, while the driver is available in OpenSBI 1.9, using this version requires either patch the vendor U-Boot or using the upstream U-Boot which still has some limitations (thanks for your efforts improving it).

At the end with your patch 2, keeping this driver a bit longer doesn't hurt, as it is only used as a fallback if the SBI SRST extension is not available.

Regards
Aurelien

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
author
#5 lore raw

Re: [PATCH v2 3/3] power: reset: Remove SpacemiT P1 reboot driver

Hi Aurelien,

Thanks for your review comments.

Quoted 40 lines · Aurelien Jarno

Hi Anand,

Quoted 18 lines · Anand Moon

The SpacemiT P1 reboot driver uses I2C-backed regmap operations to request system shutdown and restart through the PMIC.

The system-off handlers run in atomic context, where regmap operations may sleep. This can trigger invalid wait context and scheduling-while-atomic warnings during shutdown or restart.

On SpacemiT K1 platforms, system poweroff and restart are handled by OpenSBI through the SBI System Reset (SRST) extension. The Linux PMIC reboot driver duplicates this firmware functionality and is no longer needed.

Remove the driver along with its Kconfig and Makefile entries. The P1 MFD driver and its other functionality remain unaffected.

Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Quoted 8 lines

---
New patch

---
 drivers/power/reset/Kconfig              |  9 ---
 drivers/power/reset/Makefile             |  1 -
 drivers/power/reset/spacemit-p1-reboot.c | 87 ------------------------
 3 files changed, 97 deletions(-)
 delete mode 100644 drivers/power/reset/spacemit-p1-reboot.c

The patch is correct, but you might also want to remove the spacemit-p1-reboot entry from drivers/mfd/simple-mfd-i2c.c.

Anyway, while I support removing this driver on the long term, it might be a bit early to remove it. Indeed, while the driver is available in OpenSBI 1.9, using this version requires either patch the vendor U-Boot or using the upstream U-Boot which still has some limitations (thanks for your efforts improving it).

Quoted 4 lines

At the end with your patch 2, keeping this driver a bit longer doesn't hurt, as it is only used as a fallback if the SBI SRST extension is not available.

Ok I have no issue keeping this as a fallback driver.

Quoted 6 lines

Regards
Aurelien

--
Aurelien Jarno GPG: 4096R/1DDD8C9B
[email protected] http://aurel32.net

Thanks
-Anand