Skip to contentSkip to conversation

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

PATCH Superseded Anand Moon 5 participants 17 messages Nov 10 – Nov 18

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Anand Moon

author
#0 lore raw

PATCH 1/2 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.

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Alex Elder <[email protected]>
---
 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: b3dfbde62cf1981c4638939c159dfd087b7a72e9

Signature · 1 line
2.56.0
author
#1 lore raw

PATCH 2/2 power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Fixes 28124cc0fb8c ("driver: reset: spacemit-p1: add driver for poweroff/reboot")
---
 drivers/power/reset/spacemit-p1-reboot.c | 86 ++++++++++++++++++++----
 1 file changed, 74 insertions(+), 12 deletions(-)
drivers/power/reset/spacemit-p1-reboot.c +74 -12
index c0454b547718..8fcb3eadddf3 100644
@@ -4,8 +4,9 @@
  */
 
 #include <linux/bits.h>
+#include <linux/errno.h>
+#include <linux/i2c.h>
 #include <linux/platform_device.h>
-#include <linux/regmap.h>
 #include <linux/reboot.h>
 
 /* Power Control Register 2 */
@@ -13,13 +14,68 @@
 #define PWR_CTRL2_SHUTDOWN	BIT(2)	/* Shutdown request */
 #define PWR_CTRL2_RST		BIT(1)	/* Reset request */
 
+static int spacemit_p1_read_atomic(struct i2c_client *client,
+				   u8 reg, u8 *val)
+{
+	struct i2c_msg msgs[] = {
+		{
+			.addr = client->addr,
+			.flags = client->flags & I2C_M_TEN,
+			.len = 1,
+			.buf = &reg,
+		},
+		{
+			.addr = client->addr,
+			.flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
+			.len = 1,
+			.buf = val,
+		},
+	};
+	int ret;
+
+	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
+
+	return ret == ARRAY_SIZE(msgs) ? 0 : ret < 0 ? ret : -EIO;
+}
+
+static int spacemit_p1_write_atomic(struct i2c_client *client,
+				    u8 reg, u8 val)
+{
+	u8 buf[] = { reg, val };
+	struct i2c_msg msg = {
+		.addr = client->addr,
+		.flags = client->flags & I2C_M_TEN,
+		.len = sizeof(buf),
+		.buf = buf,
+	};
+	int ret;
+
+	ret = i2c_transfer(client->adapter, &msg, 1);
+
+	return ret == 1 ? 0 : ret < 0 ? ret : -EIO;
+}
+
+static int spacemit_p1_update_bits_atomic(struct i2c_client *client,
+					  u8 reg, u8 mask)
+{
+	u8 val;
+	int ret;
+
+	ret = spacemit_p1_read_atomic(client, reg, &val);
+	if (ret)
+		return ret;
+
+	return spacemit_p1_write_atomic(client, reg, val | mask);
+}
+
 static int spacemit_p1_pwroff_handler(struct sys_off_data *data)
 {
-	struct regmap *regmap = data->cb_data;
+	struct i2c_client *client = data->cb_data;
 	int ret;
 
 	/* Put the PMIC into shutdown state */
-	ret = regmap_set_bits(regmap, PWR_CTRL2, PWR_CTRL2_SHUTDOWN);
+	ret = spacemit_p1_update_bits_atomic(client, PWR_CTRL2,
+					     PWR_CTRL2_SHUTDOWN);
 	if (ret) {
 		dev_err(data->dev, "shutdown failed: %d\n", ret);
 		return notifier_from_errno(ret);
@@ -30,11 +86,12 @@ static int spacemit_p1_pwroff_handler(struct sys_off_data *data)
 
 static int spacemit_p1_restart_handler(struct sys_off_data *data)
 {
-	struct regmap *regmap = data->cb_data;
+	struct i2c_client *client = data->cb_data;
 	int ret;
 
 	/* Put the PMIC into reset state */
-	ret = regmap_set_bits(regmap, PWR_CTRL2, PWR_CTRL2_RST);
+	ret = spacemit_p1_update_bits_atomic(client, PWR_CTRL2,
+					     PWR_CTRL2_RST);
 	if (ret) {
 		dev_err(data->dev, "restart failed: %d\n", ret);
 		return notifier_from_errno(ret);
@@ -46,21 +103,26 @@ static int spacemit_p1_restart_handler(struct sys_off_data *data)
 static int spacemit_p1_reboot_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
-	struct regmap *regmap;
+	struct i2c_client *client;
 	int ret;
 
-	regmap = dev_get_regmap(dev->parent, NULL);
-	if (!regmap)
-		return -ENODEV;
+	client = i2c_verify_client(dev->parent);
+	if (!client)
+		return dev_err_probe(dev, -ENODEV,
+				     "Failed to find parent I2C client\n");
+
+	if (!client->adapter->algo->xfer_atomic)
+		return dev_err_probe(dev, -EOPNOTSUPP,
+				     "Atomic I2C transfers not supported\n");
 
-	ret = devm_register_power_off_handler(dev, &spacemit_p1_pwroff_handler,
-					      regmap);
+	ret = devm_register_power_off_handler(dev, spacemit_p1_pwroff_handler,
+					      client);
 	if (ret)
 		return dev_err_probe(dev, ret,
 				     "Failed to register power off handler\n");
 
 	ret = devm_register_restart_handler(dev, spacemit_p1_restart_handler,
-					    regmap);
+					    client);
 	if (ret)
 		return dev_err_probe(dev, ret,
 				     "Failed to register restart handler\n");
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi Anand,

Quoted 22 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Quoted 4 lines

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

While the patch looks correct, I am not sure this is the correct approach, as opposed to the one proposed by Troy [1].

Indeed bypassing regmap and using i2c atomic transfers needs to be done by all affected drivers. For instance the VisionFive 2 board, which use an axp15060 PMIC is also affected by this issue. And the corresponding driver is shared between architectures, which make this kind of change more difficult.

For the BPI-F3, note that with OpenSBI 1.9, the reboot is handled through the SBI SRST extension, preventing this issue to happen. On the long term, I am not sure we should keep this driver.

Regards
Aurelien

[1] https://lore.kernel.org/linux-riscv/20261104-v7-0-rc1-rv-dis-int-before-restart-v5-1-956ec09c900d@linux.dev/

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

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi Aurelien,

Quoted 39 lines · Aurelien Jarno

Hi Anand,

Quoted 22 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Quoted 4 lines

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

While the patch looks correct, I am not sure this is the correct approach, as opposed to the one proposed by Troy [1].

Indeed bypassing regmap and using i2c atomic transfers needs to be done by all affected drivers. For instance the VisionFive 2 board, which use an axp15060 PMIC is also affected by this issue. And the corresponding driver is shared between architectures, which make this kind of change more difficult.

Quoted 4 lines

For the BPI-F3, note that with OpenSBI 1.9, the reboot is handled through the SBI SRST extension, preventing this issue to happen. On the long term, I am not sure we should keep this driver.

That only covers restart. The SBI restart handler is registered at priority 192 and runs before the P1 one at SYS_OFF_PRIO_DEFAULT. Power off is the other way around: sbi_srst_power_off() goes through register_platform_power_off(), i.e. SYS_OFF_PRIO_PLATFORM, so the P1 power-off handler still runs first and still hits this on shutdown, even with OpenSBI 1.9.

So until this driver goes away, BPI-F3 still needs the arch fix [1] for power-off.

Signature · 1 line
Troy Mitchell
#4 lore raw

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi Troy,

Quoted 53 lines · Troy Mitchell

Hi Aurelien,

Quoted 39 lines · Aurelien Jarno

Hi Anand,

Quoted 22 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Quoted 4 lines

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

While the patch looks correct, I am not sure this is the correct approach, as opposed to the one proposed by Troy [1].

Indeed bypassing regmap and using i2c atomic transfers needs to be done by all affected drivers. For instance the VisionFive 2 board, which use an axp15060 PMIC is also affected by this issue. And the corresponding driver is shared between architectures, which make this kind of change more difficult.

Quoted 4 lines

For the BPI-F3, note that with OpenSBI 1.9, the reboot is handled through the SBI SRST extension, preventing this issue to happen. On the long term, I am not sure we should keep this driver.

That only covers restart. The SBI restart handler is registered at priority 192 and runs before the P1 one at SYS_OFF_PRIO_DEFAULT. Power off is the other way around: sbi_srst_power_off() goes through register_platform_power_off(), i.e. SYS_OFF_PRIO_PLATFORM, so the P1 power-off handler still runs first and still hits this on shutdown, even with OpenSBI 1.9.

Quoted 4 lines

So until this driver goes away, BPI-F3 still needs the arch fix [1] for power-off.

We also need a more global fix for the other boards like the VisionFive 2, so definitely we need the above fix instead of patching the drivers.

Aurelien

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

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi Troy. Aurelien,

Thanks for your review comments.

Quoted 57 lines · Aurelien Jarno

Hi Troy,

Quoted 53 lines · Troy Mitchell

Hi Aurelien,

Quoted 39 lines · Aurelien Jarno

Hi Anand,

Quoted 22 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Quoted 4 lines

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

While the patch looks correct, I am not sure this is the correct approach, as opposed to the one proposed by Troy [1].

Indeed bypassing regmap and using i2c atomic transfers needs to be done by all affected drivers. For instance the VisionFive 2 board, which use an axp15060 PMIC is also affected by this issue. And the corresponding driver is shared between architectures, which make this kind of change more difficult.

Quoted 4 lines

For the BPI-F3, note that with OpenSBI 1.9, the reboot is handled through the SBI SRST extension, preventing this issue to happen. On the long term, I am not sure we should keep this driver.

That only covers restart. The SBI restart handler is registered at priority 192 and runs before the P1 one at SYS_OFF_PRIO_DEFAULT. Power off is the other way around: sbi_srst_power_off() goes through register_platform_power_off(), i.e. SYS_OFF_PRIO_PLATFORM, so the P1 power-off handler still runs first and still hits this on shutdown, even with OpenSBI 1.9.

I had previously tested the OpenSBI P1 PMIC driver in a Yocto-based environment:

[1] https://lore.kernel.org/all/[email protected]/

With this driver registered in OpenSBI, Linux can delegate reboot and power-off operations to firmware through the SBI SRST extension.

The power-off sequence is:

    Linux kernel_power_off()
      -> RISC-V machine power-off
      -> SBI SRST shutdown
      -> OpenSBI system_reset()
      -> SpacemiT P1 PMIC
      -> Power off

I have now rebuilt the Linux kernel with:

# CONFIG_POWER_RESET_SPACEMIT_P1 is not set

Both reboot and power-off work correctly on the Banana Pi BPI-F3, and I have not observed any issues.

alarm@k1:~$ sudo poweroff

Broadcast message from root@k1 on pts/1 (Wed 2026-11-11 10:34:03 UTC):

The system will power off now!

[ 1744.735287] systemd-shutdown[1]: Sending SIGTERM to remaining processes...
[ 1745.071304] systemd-journald[245]: Received SIGTERM from PID 1
(systemd-shutdow).
[ 1745.504595] systemd-shutdown[1]: Sending SIGKILL to remaining processes...
[ 1745.790159] systemd-shutdown[1]: Unmounting file systems.
[ 1745.822902] (sd-umount)[658]: Unmounting
'/run/credentials/systemd-journald.service'.
[ 1745.861214] (sd-remount)[659]: Remounting '/' read-only with options ''.
[ 1746.079291] EXT4-fs (mmcblk1p7): re-mounted
ccb95ca0-2804-4ade-938f-23116f90c193 ro.
[ 1746.272104] systemd-shutdown[1]: All filesystems unmounted.
[ 1746.272471] systemd-shutdown[1]: Deactivating swaps.
[ 1746.273712] systemd-shutdown[1]: All swaps deactivated.
[ 1746.273956] systemd-shutdown[1]: Detaching loop devices.
[ 1746.464867] systemd-shutdown[1]: All loop devices detached.
[ 1746.465346] systemd-shutdown[1]: Stopping MD devices.
[ 1746.471149] systemd-shutdown[1]: All MD devices stopped.
[ 1746.471417] systemd-shutdown[1]: Detaching DM devices.
[ 1746.476047] systemd-shutdown[1]: All DM devices detached.
[ 1746.476377] systemd-shutdown[1]: All filesystems, swaps, loop
devices, MD devices and DM devices detached.
[ 1746.533569] systemd-shutdown[1]: Successfully changed into root pivot.
[ 1746.533862] systemd-shutdown[1]: Entering exitrd...
[ 1746.686127] shutdown[1]: Syncing filesystems and block devices.
[ 1746.701832] shutdown[1]: Sending SIGTERM to remaining processes...
[ 1746.934370] shutdown[1]: Sending SIGKILL to remaining processes...
[ 1747.164219] shutdown[1]: Unmounting file systems.
[ 1747.203935] (sd-umoun[662]: Unmounting '/run/credentials'.
[ 1747.232643] (sd-umoun[663]: Unmounting '/oldroot/sys/kernel/config'.
[ 1747.269882] (sd-umoun[664]: Unmounting '/oldroot/sys/kernel/tracing'.
[ 1747.298218] (sd-umoun[665]: Unmounting '/oldroot/sys/kernel/debug'.
[ 1747.327488] (sd-umoun[666]: Unmounting '/oldroot/dev/hugepages'.
[ 1747.359053] (sd-umoun[667]: Unmounting '/oldroot/dev/mqueue'.
[ 1747.387798] (sd-umoun[668]: Unmounting '/oldroot/run'.
[ 1747.425939] (sd-umoun[669]: Unmounting '/oldroot/proc'.
[ 1747.473927] (sd-umoun[670]: Unmounting '/oldroot/sys/fs/bpf'.
[ 1747.527910] (sd-umoun[671]: Unmounting '/oldroot/sys/fs/pstore'.
[ 1747.588367] (sd-umoun[672]: Unmounting '/oldroot/sys/fs/cgroup'.
[ 1747.630489] (sd-umoun[673]: Unmounting '/oldroot/sys/kernel/security'.
[ 1747.664244] (sd-umoun[674]: Unmounting '/oldroot/sys'.
[ 1747.704716] (sd-umoun[675]: Unmounting '/oldroot/dev/pts'.
[ 1747.755218] (sd-umoun[676]: Unmounting '/oldroot/dev/shm'.
[ 1747.791652] (sd-umoun[677]: Unmounting '/oldroot/dev'.
[ 1747.826501] (sd-umoun[678]: Unmounting '/oldroot'.
[ 1748.451336] EXT4-fs (mmcblk1p7): unmounting filesystem
ccb95ca0-2804-4ade-938f-23116f90c193.
[ 1748.643359] shutdown[1]: All filesystems unmounted.
[ 1748.643640] shutdown[1]: Deactivating swaps.
[ 1748.644512] shutdown[1]: All swaps deactivated.
[ 1748.644722] shutdown[1]: Detaching loop devices.
[ 1748.732490] shutdown[1]: All loop devices detached.
[ 1748.732764] shutdown[1]: Stopping MD devices.
[ 1748.754002] shutdown[1]: All MD devices stopped.
[ 1748.754286] shutdown[1]: Detaching DM devices.
[ 1748.759617] shutdown[1]: All DM devices detached.
[ 1748.759885] shutdown[1]: All filesystems, swaps, loop devices, MD
devices and DM devices detached.
[ 1748.770510] shutdown[1]: Syncing filesystems and block devices.
[ 1748.791699] shutdown[1]: Powering off.
[ 1749.009246] reboot: Power down

Given that OpenSBI already handles both operations,
I agree that the Linux P1 power-off and reset driver may no longer be necessary, at least on systems using firmware with the appropriate SBI SRST support.

Quoted 8 lines
Quoted 4 lines

So until this driver goes away, BPI-F3 still needs the arch fix [1] for power-off.

We also need a more global fix for the other boards like the VisionFive 2, so definitely we need the above fix instead of patching the drivers.

Maybe we need an opensbi driver for VisionFive 2 dev boards; any thoughts

Quoted 5 lines

Aurelien

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

Thanks
-Anand

#6 lore raw

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

In reply to Anand Moon

Hi,

Quoted 25 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Quoted 8 lines

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Fixes 28124cc0fb8c ("driver: reset: spacemit-p1: add driver for poweroff/reboot")

---

regmap runs i2c_transfer itself and thus also uses the atomic I2C transfers. The problem here is regmap itself. But instead of moving away from regmap, the atomic use case should be supported properly in regmap.

Greetings,

-- Sebastian

Quoted 141 lines
 drivers/power/reset/spacemit-p1-reboot.c | 86 ++++++++++++++++++++----
 1 file changed, 74 insertions(+), 12 deletions(-)
drivers/power/reset/spacemit-p1-reboot.c +74 -12
index c0454b547718..8fcb3eadddf3 100644
@@ -4,8 +4,9 @@
  */
 
 #include <linux/bits.h>
+#include <linux/errno.h>
+#include <linux/i2c.h>
 #include <linux/platform_device.h>
-#include <linux/regmap.h>
 #include <linux/reboot.h>
 
 /* Power Control Register 2 */
@@ -13,13 +14,68 @@
 #define PWR_CTRL2_SHUTDOWN	BIT(2)	/* Shutdown request */
 #define PWR_CTRL2_RST		BIT(1)	/* Reset request */
 
+static int spacemit_p1_read_atomic(struct i2c_client *client,
+				   u8 reg, u8 *val)
+{
+	struct i2c_msg msgs[] = {
+		{
+			.addr = client->addr,
+			.flags = client->flags & I2C_M_TEN,
+			.len = 1,
+			.buf = &reg,
+		},
+		{
+			.addr = client->addr,
+			.flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
+			.len = 1,
+			.buf = val,
+		},
+	};
+	int ret;
+
+	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
+
+	return ret == ARRAY_SIZE(msgs) ? 0 : ret < 0 ? ret : -EIO;
+}
+
+static int spacemit_p1_write_atomic(struct i2c_client *client,
+				    u8 reg, u8 val)
+{
+	u8 buf[] = { reg, val };
+	struct i2c_msg msg = {
+		.addr = client->addr,
+		.flags = client->flags & I2C_M_TEN,
+		.len = sizeof(buf),
+		.buf = buf,
+	};
+	int ret;
+
+	ret = i2c_transfer(client->adapter, &msg, 1);
+
+	return ret == 1 ? 0 : ret < 0 ? ret : -EIO;
+}
+
+static int spacemit_p1_update_bits_atomic(struct i2c_client *client,
+					  u8 reg, u8 mask)
+{
+	u8 val;
+	int ret;
+
+	ret = spacemit_p1_read_atomic(client, reg, &val);
+	if (ret)
+		return ret;
+
+	return spacemit_p1_write_atomic(client, reg, val | mask);
+}
+
 static int spacemit_p1_pwroff_handler(struct sys_off_data *data)
 {
-	struct regmap *regmap = data->cb_data;
+	struct i2c_client *client = data->cb_data;
 	int ret;
 
 	/* Put the PMIC into shutdown state */
-	ret = regmap_set_bits(regmap, PWR_CTRL2, PWR_CTRL2_SHUTDOWN);
+	ret = spacemit_p1_update_bits_atomic(client, PWR_CTRL2,
+					     PWR_CTRL2_SHUTDOWN);
 	if (ret) {
 		dev_err(data->dev, "shutdown failed: %d\n", ret);
 		return notifier_from_errno(ret);
@@ -30,11 +86,12 @@ static int spacemit_p1_pwroff_handler(struct sys_off_data *data)
 
 static int spacemit_p1_restart_handler(struct sys_off_data *data)
 {
-	struct regmap *regmap = data->cb_data;
+	struct i2c_client *client = data->cb_data;
 	int ret;
 
 	/* Put the PMIC into reset state */
-	ret = regmap_set_bits(regmap, PWR_CTRL2, PWR_CTRL2_RST);
+	ret = spacemit_p1_update_bits_atomic(client, PWR_CTRL2,
+					     PWR_CTRL2_RST);
 	if (ret) {
 		dev_err(data->dev, "restart failed: %d\n", ret);
 		return notifier_from_errno(ret);
@@ -46,21 +103,26 @@ static int spacemit_p1_restart_handler(struct sys_off_data *data)
 static int spacemit_p1_reboot_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
-	struct regmap *regmap;
+	struct i2c_client *client;
 	int ret;
 
-	regmap = dev_get_regmap(dev->parent, NULL);
-	if (!regmap)
-		return -ENODEV;
+	client = i2c_verify_client(dev->parent);
+	if (!client)
+		return dev_err_probe(dev, -ENODEV,
+				     "Failed to find parent I2C client\n");
+
+	if (!client->adapter->algo->xfer_atomic)
+		return dev_err_probe(dev, -EOPNOTSUPP,
+				     "Atomic I2C transfers not supported\n");
 
-	ret = devm_register_power_off_handler(dev, &spacemit_p1_pwroff_handler,
-					      regmap);
+	ret = devm_register_power_off_handler(dev, spacemit_p1_pwroff_handler,
+					      client);
 	if (ret)
 		return dev_err_probe(dev, ret,
 				     "Failed to register power off handler\n");
 
 	ret = devm_register_restart_handler(dev, spacemit_p1_restart_handler,
-					    regmap);
+					    client);
 	if (ret)
 		return dev_err_probe(dev, ret,
 				     "Failed to register restart handler\n");
Signature · 1 line
2.56.0
author
#7 lore raw

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi All,

Quoted 38 lines · Sebastian Reichel

Hi,

Quoted 25 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Quoted 8 lines

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Fixes 28124cc0fb8c ("driver: reset: spacemit-p1: add driver for poweroff/reboot")

---

Quoted 5 lines

regmap runs i2c_transfer itself and thus also uses the atomic I2C transfers. The problem here is regmap itself. But instead of moving away from regmap, the atomic use case should be supported properly in regmap.

On RISC-V platform, poweroff and reboot are already handled by OpenSBI through the SBI System Reset (SRST) extension. Linux delegates these operations to firmware using SBI_SRST_RESET_TYPE_SHUTDOWN for poweroff and SBI_SRST_RESET_TYPE_COLD_REBOOT or SBI_SRST_RESET_TYPE_WARM_REBOOT for reboot, depending on the supported reset type.

Therefore, there is no need for an additional Linux PMIC poweroff/reboot handler on this platform, as it duplicates functionality already provided by firmware.

While supporting atomic operations in regmap may be useful more generally, it is not necessary to address this platform's shutdown and reboot requirements.

I tested this on the SpacemiT K1 Banana Pi BPI-F3 after removing this PMIC poweroff/reboot driver.

OpenSBI commit which handles this extension

d9637d00bf47 ("lib: utils/reset: add SpacemiT P1 PMIC support") b10e18ec854a ("lib: utils/i2c: add minimal SpacemiT I2C driver")

Similarly, on the StarFive VisionFive 2, poweroff is handled by OpenSBI through the
AXP15060 PMIC, so there is no need to register an additional poweroff handler using
devm_register_power_off_handler().

OpenSBI commit which handles this extension

568ea49490d9 ("platform: starfive: add PMIC power ops in JH7110 visionfive2 board")

-----8<----------8<----------8<------

drivers/mfd/axp20x.c +0 -2
index 50df66de24e06..069259f7d7c15 100644
@@ -1458,8 +1458,14 @@ int axp20x_device_probe(struct axp20x_dev *axp20x)
                return ret;
        }
 
-       if (axp20x->variant != AXP288_ID)
-               devm_register_power_off_handler(axp20x->dev,

axp20x_power_off, axp20x);
+ /*

+        * The VisionFive 2 handles AXP15060 poweroff through OpenSBI.
+        * Avoid accessing the I2C PMIC from the atomic poweroff path.
+        */

+ if (axp20x->variant != AXP288_ID && !(axp20x->variant == AXP15060_ID &&
+ of_machine_is_compatible("starfive,jh7110")))
+ devm_register_power_off_handler(axp20x->dev,
+ axp20x_power_off, axp20x);

        dev_info(axp20x->dev, "AXP20X driver loaded\n");

Please let me know if this works on your board?

Quoted 2 lines

-- Sebastian

Thanks
-Anand

#8 lore raw

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi Anand,

Quoted 102 lines · Anand Moon

Hi All,

Quoted 38 lines · Sebastian Reichel

Hi,

Quoted 25 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Quoted 8 lines

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Fixes 28124cc0fb8c ("driver: reset: spacemit-p1: add driver for poweroff/reboot")

---

Quoted 5 lines

regmap runs i2c_transfer itself and thus also uses the atomic I2C transfers. The problem here is regmap itself. But instead of moving away from regmap, the atomic use case should be supported properly in regmap.

On RISC-V platform, poweroff and reboot are already handled by OpenSBI through the SBI System Reset (SRST) extension. Linux delegates these operations to firmware using SBI_SRST_RESET_TYPE_SHUTDOWN for poweroff and SBI_SRST_RESET_TYPE_COLD_REBOOT or SBI_SRST_RESET_TYPE_WARM_REBOOT for reboot, depending on the supported reset type.

Therefore, there is no need for an additional Linux PMIC poweroff/reboot handler on this platform, as it duplicates functionality already provided by firmware.

While supporting atomic operations in regmap may be useful more generally, it is not necessary to address this platform's shutdown and reboot requirements.

I tested this on the SpacemiT K1 Banana Pi BPI-F3 after removing this PMIC poweroff/reboot driver.

OpenSBI commit which handles this extension

d9637d00bf47 ("lib: utils/reset: add SpacemiT P1 PMIC support") b10e18ec854a ("lib: utils/i2c: add minimal SpacemiT I2C driver")

Similarly, on the StarFive VisionFive 2, poweroff is handled by OpenSBI through the
AXP15060 PMIC, so there is no need to register an additional poweroff handler using
devm_register_power_off_handler().

OpenSBI commit which handles this extension

568ea49490d9 ("platform: starfive: add PMIC power ops in JH7110 visionfive2 board")

-----8<----------8<----------8<------

drivers/mfd/axp20x.c +8 -2
index 50df66de24e06..069259f7d7c15 100644
@@ -1458,8 +1458,14 @@ int axp20x_device_probe(struct axp20x_dev *axp20x)
                return ret;
        }
 
-       if (axp20x->variant != AXP288_ID)
-               devm_register_power_off_handler(axp20x->dev,
 axp20x_power_off, axp20x);
+       /*
+        * The VisionFive 2 handles AXP15060 poweroff through OpenSBI.
+        * Avoid accessing the I2C PMIC from the atomic poweroff path.
+        */
+       if (axp20x->variant != AXP288_ID && !(axp20x->variant == AXP15060_ID &&
+                               of_machine_is_compatible("starfive,jh7110")))
+               devm_register_power_off_handler(axp20x->dev,
+                               axp20x_power_off, axp20x);
 
        dev_info(axp20x->dev, "AXP20X driver loaded\n");
Quoted 1 line

Please let me know if this works on your board?

Yes, that works, but I don't think this is the correct solution to disable here. Also this won't scale for future boards which might use a different existing PMIC driver with poweroff support and that will also need to be patched.

As pointed out by Troy in an earlier mail, the real problem is the priority at which the handler is registers, 192 for the reset and -256 for the poweroff one. I wonder if the SBI SRST extension should also register the poweroff one at 192. It seems it's about replacing the call to register_platform_power_off() by register_sys_off_handler(), which supports using a priority value. And maybe SYS_OFF_PRIO_FIRMWARE = 224 should be used there instead of 192.

Regards
Aurelien

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

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi Aurelien,

Quoted 104 lines · Aurelien Jarno

Hi Anand,

Quoted 100 lines · Anand Moon

Hi All,

Quoted 38 lines · Sebastian Reichel

Hi,

Quoted 25 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Quoted 8 lines

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Fixes 28124cc0fb8c ("driver: reset: spacemit-p1: add driver for poweroff/reboot")

---

Quoted 5 lines

regmap runs i2c_transfer itself and thus also uses the atomic I2C transfers. The problem here is regmap itself. But instead of moving away from regmap, the atomic use case should be supported properly in regmap.

On RISC-V platform, poweroff and reboot are already handled by OpenSBI through the SBI System Reset (SRST) extension. Linux delegates these operations to firmware using SBI_SRST_RESET_TYPE_SHUTDOWN for poweroff and SBI_SRST_RESET_TYPE_COLD_REBOOT or SBI_SRST_RESET_TYPE_WARM_REBOOT for reboot, depending on the supported reset type.

Therefore, there is no need for an additional Linux PMIC poweroff/reboot handler on this platform, as it duplicates functionality already provided by firmware.

While supporting atomic operations in regmap may be useful more generally, it is not necessary to address this platform's shutdown and reboot requirements.

I tested this on the SpacemiT K1 Banana Pi BPI-F3 after removing this PMIC poweroff/reboot driver.

OpenSBI commit which handles this extension

d9637d00bf47 ("lib: utils/reset: add SpacemiT P1 PMIC support") b10e18ec854a ("lib: utils/i2c: add minimal SpacemiT I2C driver")

Similarly, on the StarFive VisionFive 2, poweroff is handled by OpenSBI through the
AXP15060 PMIC, so there is no need to register an additional poweroff handler using
devm_register_power_off_handler().

OpenSBI commit which handles this extension

568ea49490d9 ("platform: starfive: add PMIC power ops in JH7110 visionfive2 board")

-----8<----------8<----------8<------

drivers/mfd/axp20x.c +8 -2
index 50df66de24e06..069259f7d7c15 100644
@@ -1458,8 +1458,14 @@ int axp20x_device_probe(struct axp20x_dev *axp20x)
                return ret;
        }
 
-       if (axp20x->variant != AXP288_ID)
-               devm_register_power_off_handler(axp20x->dev,
 axp20x_power_off, axp20x);
+       /*
+        * The VisionFive 2 handles AXP15060 poweroff through OpenSBI.
+        * Avoid accessing the I2C PMIC from the atomic poweroff path.
+        */
+       if (axp20x->variant != AXP288_ID && !(axp20x->variant == AXP15060_ID &&
+                               of_machine_is_compatible("starfive,jh7110")))
+               devm_register_power_off_handler(axp20x->dev,
+                               axp20x_power_off, axp20x);
 
Quoted 9 lines
Quoted 3 lines
drivers/mfd/axp20x.c +0 -0
        dev_info(axp20x->dev, "AXP20X driver loaded\n");

Please let me know if this works on your board?

Yes, that works, but I don't think this is the correct solution to disable here. Also this won't scale for future boards which might use a different existing PMIC driver with poweroff support and that will also need to be patched.

No changes needed over here, reboot and power-off priority will work fine.

Quoted 8 lines

As pointed out by Troy in an earlier mail, the real problem is the priority at which the handler is registers, 192 for the reset and -256 for the poweroff one. I wonder if the SBI SRST extension should also register the poweroff one at 192. It seems it's about replacing the call to register_platform_power_off() by register_sys_off_handler(), which supports using a priority value. And maybe SYS_OFF_PRIO_FIRMWARE = 224 should be used there instead of 192.

Thanks for your input; with the changes below.
It works fine on Spacemit k1 and Starfive Vision 2 for reboot and power-off.

 arch/riscv/kernel/sbi.c | 51 +++++++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 12 deletions(-)
arch/riscv/kernel/sbi.c +0 -0
index af1ccea222f1..32a3d2df9032 100644
@@ -512,23 +512,55 @@ 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 bool srst_power_off;
+static bool sbi_srst_available;
+
+static int __init sbi_srst_sys_off_init(void)
+{
+ struct sys_off_handler *handler;
+
+ if (!sbi_srst_available)
+ return 0;
+
+ handler = register_sys_off_handler(SYS_OFF_MODE_RESTART,
+ SYS_OFF_PRIO_HIGH,
+ 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));
+ } else {
+ srst_power_off = true;
+ }
+
+ return 0;
}
+device_initcall(sbi_srst_sys_off_init);

/**
* sbi_probe_extension() - Check if an SBI extension ID is supported or not.
@@ -643,7 +675,6 @@ int sbi_debug_console_read(char *bytes, unsigned int num_bytes)

void __init sbi_init(void)
{

-       bool srst_power_off = false;
        int ret;

        ret = sbi_get_spec_version();
@@ -677,11 +708,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);
+                       sbi_srst_available = true;
                }
                if (sbi_spec_version >= sbi_mk_version(2, 0) &&
                    sbi_probe_extension(SBI_EXT_DBCN) > 0) {

--
2.56.0

Quoted 6 lines

Regards
Aurelien

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

Thanks
-Anand

#10 lore raw

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi Anand,

Quoted 126 lines · Anand Moon

Hi Aurelien,

Quoted 104 lines · Aurelien Jarno

Hi Anand,

Quoted 100 lines · Anand Moon

Hi All,

Quoted 38 lines · Sebastian Reichel

Hi,

Quoted 25 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Quoted 8 lines

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Fixes 28124cc0fb8c ("driver: reset: spacemit-p1: add driver for poweroff/reboot")

---

Quoted 5 lines

regmap runs i2c_transfer itself and thus also uses the atomic I2C transfers. The problem here is regmap itself. But instead of moving away from regmap, the atomic use case should be supported properly in regmap.

On RISC-V platform, poweroff and reboot are already handled by OpenSBI through the SBI System Reset (SRST) extension. Linux delegates these operations to firmware using SBI_SRST_RESET_TYPE_SHUTDOWN for poweroff and SBI_SRST_RESET_TYPE_COLD_REBOOT or SBI_SRST_RESET_TYPE_WARM_REBOOT for reboot, depending on the supported reset type.

Therefore, there is no need for an additional Linux PMIC poweroff/reboot handler on this platform, as it duplicates functionality already provided by firmware.

While supporting atomic operations in regmap may be useful more generally, it is not necessary to address this platform's shutdown and reboot requirements.

I tested this on the SpacemiT K1 Banana Pi BPI-F3 after removing this PMIC poweroff/reboot driver.

OpenSBI commit which handles this extension

d9637d00bf47 ("lib: utils/reset: add SpacemiT P1 PMIC support") b10e18ec854a ("lib: utils/i2c: add minimal SpacemiT I2C driver")

Similarly, on the StarFive VisionFive 2, poweroff is handled by OpenSBI through the
AXP15060 PMIC, so there is no need to register an additional poweroff handler using
devm_register_power_off_handler().

OpenSBI commit which handles this extension

568ea49490d9 ("platform: starfive: add PMIC power ops in JH7110 visionfive2 board")

-----8<----------8<----------8<------

drivers/mfd/axp20x.c +8 -2
index 50df66de24e06..069259f7d7c15 100644
@@ -1458,8 +1458,14 @@ int axp20x_device_probe(struct axp20x_dev *axp20x)
                return ret;
        }
 
-       if (axp20x->variant != AXP288_ID)
-               devm_register_power_off_handler(axp20x->dev,
 axp20x_power_off, axp20x);
+       /*
+        * The VisionFive 2 handles AXP15060 poweroff through OpenSBI.
+        * Avoid accessing the I2C PMIC from the atomic poweroff path.
+        */
+       if (axp20x->variant != AXP288_ID && !(axp20x->variant == AXP15060_ID &&
+                               of_machine_is_compatible("starfive,jh7110")))
+               devm_register_power_off_handler(axp20x->dev,
+                               axp20x_power_off, axp20x);
 
Quoted 9 lines
Quoted 3 lines
drivers/mfd/axp20x.c +0 -0
        dev_info(axp20x->dev, "AXP20X driver loaded\n");

Please let me know if this works on your board?

Yes, that works, but I don't think this is the correct solution to disable here. Also this won't scale for future boards which might use a different existing PMIC driver with poweroff support and that will also need to be patched.

No changes needed over here, reboot and power-off priority will work fine.

Quoted 8 lines

As pointed out by Troy in an earlier mail, the real problem is the priority at which the handler is registers, 192 for the reset and -256 for the poweroff one. I wonder if the SBI SRST extension should also register the poweroff one at 192. It seems it's about replacing the call to register_platform_power_off() by register_sys_off_handler(), which supports using a priority value. And maybe SYS_OFF_PRIO_FIRMWARE = 224 should be used there instead of 192.

Quoted 5 lines

Thanks for your input; with the changes below.
It works fine on Spacemit k1 and Starfive Vision 2 for reboot and power-off.

 arch/riscv/kernel/sbi.c | 51 +++++++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 12 deletions(-)

Thanks for this patch. It sounds good overall, and it's always good to switch to the newest API.

I have been able to test it successfully on VisionFive 2 and Banana-Pi F3. Please find a few comments below.

Quoted 26 lines
arch/riscv/kernel/sbi.c +4 -6
index af1ccea222f1..32a3d2df9032 100644
@@ -512,23 +512,55 @@ 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)

arch/riscv/kernel/sbi.c static void sbi_srst_reset(unsigned long type, · line 536

Quoted 10 lines
arch/riscv/kernel/sbi.c +7 -0
+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 bool srst_power_off;
+static bool sbi_srst_available;

Not sure we want global variables for this, as they are not used anymore once the initialisation is done.

arch/riscv/kernel/sbi.c static void sbi_srst_reset(unsigned long type, · line 546

Quoted 10 lines
arch/riscv/kernel/sbi.c +10 -0
+
+static int __init sbi_srst_sys_off_init(void)
+{
+       struct sys_off_handler *handler;
+
+       if (!sbi_srst_available)
+               return 0;
+
+       handler = register_sys_off_handler(SYS_OFF_MODE_RESTART,
+                                          SYS_OFF_PRIO_HIGH,

I think you should use SYS_OFF_PRIO_FIRMWARE for both entry. It corresponds to the reboot provider.

Quoted 8 lines
arch/riscv/kernel/sbi.c +8 -0
+                                          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);

arch/riscv/kernel/sbi.c static void sbi_srst_reset(unsigned long type, · line 564

Quoted 10 lines
arch/riscv/kernel/sbi.c +9 -0
+       if (IS_ERR(handler)) {
+               pr_warn("Failed to register SBI SRST poweroff handler: %ld\n",
+                       PTR_ERR(handler));
+       } else {
+               srst_power_off = true;
+       }
+
+       return 0;
 }
+device_initcall(sbi_srst_sys_off_init);

Any reason to use a device_initcall as opposed to just calling the fuunction from the sbi_init function below? If so, please provide the reason in the commit message when submitting the final patch.

Quoted 14 lines
arch/riscv/kernel/sbi.c +0 -1
 /**
  * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
@@ -643,7 +675,6 @@ int sbi_debug_console_read(char *bytes, unsigned
 int num_bytes)
 
 void __init sbi_init(void)
 {
-       bool srst_power_off = false;
        int ret;
 
        ret = sbi_get_spec_version();
arch/riscv/kernel/sbi.c +0 -0
@@ -677,11 +708,7 @@ void __init sbi_init(void)
                if (sbi_spec_version >= sbi_mk_version(0, 3) &&
                    sbi_probe_extension(SBI_EXT_SRST)) {

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

Quoted 10 lines
arch/riscv/kernel/sbi.c +1 -5
                        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);
+                       sbi_srst_available = true;
                }
                if (sbi_spec_version >= sbi_mk_version(2, 0) &&
                    sbi_probe_extension(SBI_EXT_DBCN) > 0) {

Regards
Aurelien

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

Re: [PATCH v1 2/2] power: reset: spacemit-p1: Use atomic I2C transfers for power-off and restart

Hi Aurelien,

Thanks for testing. Your feedback helped me a lot.

Quoted 165 lines · Aurelien Jarno

Hi Anand,

Quoted 126 lines · Anand Moon

Hi Aurelien,

Quoted 104 lines · Aurelien Jarno

Hi Anand,

Quoted 100 lines · Anand Moon

Hi All,

Quoted 38 lines · Sebastian Reichel

Hi,

Quoted 25 lines · Anand Moon

The SpacemiT P1 power-off and restart handlers use regmap_set_bits() to access the PMIC. However, these handlers run in an atomic notifier context (specifically within an rcu_read_lock() critical section), while the I2C-backed regmap uses a sleepable mutex.

This triggers a "WARNING: suspicious RCU usage" warning due to an illegal context switch in the RCU read-side critical section during shutdown on the Banana Pi BPI-F3:

[  563.803475] WARNING: suspicious RCU usage
[  563.807813] 7.3.1-ARCH-RISC-V #1 Not tainted
[  563.816614] ./include/linux/rcupdate.h:387 Illegal context switch in RCU read-side critical section!
...
[  563.841445] locks held by shutdown/1: 2, last CPU#0:
[  563.846694]  #0: ffffffff8404fb20 (system_transition_mutex){+.+.}-{4:4}, at: __do_sys_reboot+0x230/0x29e
[  563.856806]  #1: ffffffff84167860 (rcu_read_lock){....}-{1:3}, at: atomic_notifier_call_chain+0x4a/0x1c2

Replace the regmap operations with atomic I2C transfers to avoid sleeping in an atomic context. Preserve unrelated register bits through read-modify-write operations and check for atomic I2C support before registering the handlers.

Obtain the I2C client from the parent device and verify that the adapter supports atomic transfers before registering the handlers.

Quoted 8 lines

Tested on the Banana Pi BPI-F3 (SpacemiT K1).

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Aurelien Jarno <[email protected]>
Fixes 28124cc0fb8c ("driver: reset: spacemit-p1: add driver for poweroff/reboot")

---

Quoted 5 lines

regmap runs i2c_transfer itself and thus also uses the atomic I2C transfers. The problem here is regmap itself. But instead of moving away from regmap, the atomic use case should be supported properly in regmap.

On RISC-V platform, poweroff and reboot are already handled by OpenSBI through the SBI System Reset (SRST) extension. Linux delegates these operations to firmware using SBI_SRST_RESET_TYPE_SHUTDOWN for poweroff and SBI_SRST_RESET_TYPE_COLD_REBOOT or SBI_SRST_RESET_TYPE_WARM_REBOOT for reboot, depending on the supported reset type.

Therefore, there is no need for an additional Linux PMIC poweroff/reboot handler on this platform, as it duplicates functionality already provided by firmware.

While supporting atomic operations in regmap may be useful more generally, it is not necessary to address this platform's shutdown and reboot requirements.

I tested this on the SpacemiT K1 Banana Pi BPI-F3 after removing this PMIC poweroff/reboot driver.

OpenSBI commit which handles this extension

d9637d00bf47 ("lib: utils/reset: add SpacemiT P1 PMIC support") b10e18ec854a ("lib: utils/i2c: add minimal SpacemiT I2C driver")

Similarly, on the StarFive VisionFive 2, poweroff is handled by OpenSBI through the
AXP15060 PMIC, so there is no need to register an additional poweroff handler using
devm_register_power_off_handler().

OpenSBI commit which handles this extension

568ea49490d9 ("platform: starfive: add PMIC power ops in JH7110 visionfive2 board")

-----8<----------8<----------8<------

drivers/mfd/axp20x.c +8 -2
index 50df66de24e06..069259f7d7c15 100644
@@ -1458,8 +1458,14 @@ int axp20x_device_probe(struct axp20x_dev *axp20x)
                return ret;
        }
 
-       if (axp20x->variant != AXP288_ID)
-               devm_register_power_off_handler(axp20x->dev,
 axp20x_power_off, axp20x);
+       /*
+        * The VisionFive 2 handles AXP15060 poweroff through OpenSBI.
+        * Avoid accessing the I2C PMIC from the atomic poweroff path.
+        */
+       if (axp20x->variant != AXP288_ID && !(axp20x->variant == AXP15060_ID &&
+                               of_machine_is_compatible("starfive,jh7110")))
+               devm_register_power_off_handler(axp20x->dev,
+                               axp20x_power_off, axp20x);
 
Quoted 9 lines
Quoted 3 lines
drivers/mfd/axp20x.c +0 -0
        dev_info(axp20x->dev, "AXP20X driver loaded\n");

Please let me know if this works on your board?

Yes, that works, but I don't think this is the correct solution to disable here. Also this won't scale for future boards which might use a different existing PMIC driver with poweroff support and that will also need to be patched.

No changes needed over here, reboot and power-off priority will work fine.

Quoted 8 lines

As pointed out by Troy in an earlier mail, the real problem is the priority at which the handler is registers, 192 for the reset and -256 for the poweroff one. I wonder if the SBI SRST extension should also register the poweroff one at 192. It seems it's about replacing the call to register_platform_power_off() by register_sys_off_handler(), which supports using a priority value. And maybe SYS_OFF_PRIO_FIRMWARE = 224 should be used there instead of 192.

Quoted 5 lines

Thanks for your input; with the changes below.
It works fine on Spacemit k1 and Starfive Vision 2 for reboot and power-off.

 arch/riscv/kernel/sbi.c | 51 +++++++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 12 deletions(-)

Thanks for this patch. It sounds good overall, and it's always good to switch to the newest API.

I have been able to test it successfully on VisionFive 2 and Banana-Pi F3. Please find a few comments below.

Quoted 23 lines
arch/riscv/kernel/sbi.c +4 -3
index af1ccea222f1..32a3d2df9032 100644
@@ -512,23 +512,55 @@ 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;
 }
 
Quoted 16 lines

arch/riscv/kernel/sbi.c static void sbi_srst_reset(unsigned long type, · line 536

Quoted 13 lines
arch/riscv/kernel/sbi.c +7 -3
-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 bool srst_power_off;
+static bool sbi_srst_available;

Not sure we want global variables for this, as they are not used anymore once the initialisation is done.

Correct, srst_power_off indicates whether SBI SRST poweroff is available,

The sbi_srst_available boolean flag indicates whether sbi_init () detected the SBI System Reset (SRST) extension during early initialization. It allows the deferred initcall to register the sys-off handlers only when SRST is supported, avoiding a second extension probe.

Ok I will remove them.

Quoted 5 lines
Quoted 5 lines
arch/riscv/kernel/sbi.c +5 -0
+
+static int __init sbi_srst_sys_off_init(void)
+{
+       struct sys_off_handler *handler;
+
Quoted 8 lines

arch/riscv/kernel/sbi.c static void sbi_srst_reset(unsigned long type, · line 546

Quoted 5 lines
arch/riscv/kernel/sbi.c +5 -0
+       if (!sbi_srst_available)
+               return 0;
+
+       handler = register_sys_off_handler(SYS_OFF_MODE_RESTART,
+                                          SYS_OFF_PRIO_HIGH,

I think you should use SYS_OFF_PRIO_FIRMWARE for both entry. It corresponds to the reboot provider.

Ok I will modify this.

Quoted 15 lines
Quoted 15 lines
arch/riscv/kernel/sbi.c +14 -0
+                                          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));
+       } else {
+               srst_power_off = true;
+       }
Quoted 9 lines

arch/riscv/kernel/sbi.c static void sbi_srst_reset(unsigned long type, · line 564

Quoted 4 lines
arch/riscv/kernel/sbi.c +3 -0
+
+       return 0;
 }
+device_initcall(sbi_srst_sys_off_init);

Any reason to use a device_initcall as opposed to just calling the fuunction from the sbi_init function below? If so, please provide the reason in the commit message when submitting the final patch.

Initially, I called register_sys_off_handler() directly from sbi_init(), replacing
the existing register_restart_handler() and register_platform_power_off() calls. However, the kernel failed to boot with this change.

The sbi_init() function runs during early RISC-V initialization, before the kernel
memory allocator is fully initialized. Since register_sys_off_handler() requires dynamic memory allocation, calling it at this stage is unsafe.

Moving the registration to a device_initcall() defers it until the required initialization
has completed. I will document this in the next revision's commit message.

Quoted 28 lines
Quoted 14 lines
arch/riscv/kernel/sbi.c +0 -1
 /**
  * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
@@ -643,7 +675,6 @@ int sbi_debug_console_read(char *bytes, unsigned
 int num_bytes)
 
 void __init sbi_init(void)
 {
-       bool srst_power_off = false;
        int ret;
 
        ret = sbi_get_spec_version();
arch/riscv/kernel/sbi.c +0 -0
@@ -677,11 +708,7 @@ void __init sbi_init(void)
                if (sbi_spec_version >= sbi_mk_version(0, 3) &&
                    sbi_probe_extension(SBI_EXT_SRST)) {

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

Quoted 10 lines
arch/riscv/kernel/sbi.c +1 -5
                        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);
+                       sbi_srst_available = true;
                }
                if (sbi_spec_version >= sbi_mk_version(2, 0) &&
                    sbi_probe_extension(SBI_EXT_DBCN) > 0) {

Regards
Aurelien

Quoted 3 lines

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

Thanks
-Anand

maintainer Reviewed-by on v1 1/2
#12 lore raw
In reply to Anand Moon
Quoted 8 lines · Anand Moon

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.

This usleep_range() was left behind when the PIO path and spacemit_i2c_delay() were added, so please add:

Fixes 5dd75dac1b35 ("i2c: spacemit: introduce pio for k1")

Otherwise, LGTM.

Signature · 1 line
Troy Mitchell
maintainer
#13 lore raw
In reply to Anand Moon
Quoted 10 lines · Anand Moon

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.

Assisted-by LLM

Also, please drop this tag. The same applies to patch 2. See commit 1e035297ffda.

Signature · 1 line
Troy Mitchell
author
#14 lore raw

Hi Troy,

Quoted 7 lines · Troy Mitchell
Quoted 5 lines · Anand Moon

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.

Quoted 8 lines
Quoted 5 lines

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.

Assisted-by LLM

Also, please drop this tag. The same applies to patch 2. See commit 1e035297ffda.

Ok once I get positive feedback on both patches, I will drop this tag and resubmit.

Quoted 3 lines

--
Troy Mitchell

Thanks
-Anand

Reviewed-by on v1 1/2
#15 lore raw
In reply to Anand Moon
Quoted 31 lines · Anand Moon

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.

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Alex Elder <[email protected]>
---
 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 */
Quoted 1 line

base-commit: b3dfbde62cf1981c4638939c159dfd087b7a72e9

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
Reviewed-by on v1 1/2
#16 lore raw
In reply to Anand Moon
Quoted 31 lines · Anand Moon

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.

Assisted-by LLM
Cc Troy Mitchell <[email protected]>
Cc Alex Elder <[email protected]>
---
  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 */
Quoted 1 line

base-commit: b3dfbde62cf1981c4638939c159dfd087b7a72e9

The use_pio flag is set for atomic transfers, so spacemit_i2c_delay() will use udelay() regardless of the delay period.

Looks good.