Skip to contentSkip to conversation

firmware: exynos-acpm: allow use during system shutdown

PATCH Applied André Draszik 2 participants 5 messages Mar 25, 2025 – Apr 7, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover firmware: exynos-acpm: allow use during system shutdown

One user of this ACPM driver is a PMIC driver that needs to communicate with the PMIC during late system shutdown [1] and at that time we are not allowed to sleep anymore.

This series address this by switching the code to using udelay() in the specific case of system shutdown. This approach was inspired by I2C's i2c_in_atomic_xfer_mode(), which has to deal with a similar corner case.

---
Changes in v3:
- switch to unconditional udelay() (Krzysztof)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- add missing ktime.h include
- switch to ktime_before() instead of !ktime_after()
- add link to user requiring this change to cover letter
- collect Tudor's Rb
- Link to v1: https://lore.kernel.org/r/[email protected]

---
André Draszik (2):
firmware: exynos-acpm: use ktime APIs for timeout detection
firmware: exynos-acpm: allow use during system shutdown

 drivers/firmware/samsung/exynos-acpm.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

---
base-commit: 9388ec571cb1adba59d1cded2300eeb11827679c
change-id: 20250321-acpm-atomic-033775b051ef

Best regards,

Signature · 1 line
André Draszik <[email protected]>
#1 lore raw

PATCH v3 1/2 firmware: exynos-acpm: use ktime APIs for timeout detection

acpm_dequeue_by_polling() uses a loop counter and assumes that each iteration of the loop takes 20us. It may take longer, though, because usleep_range() may sleep a different amount.

Switch to using ktime_get() / ktime_before() to detect the timeout condition more reliably.

This change also makes the code easier to follow and it allows us to adjust the sleep if necessary, without having to adjust the loop counter exit condition.

---
v3:
* slightly reword commit message due to updated patch 2/2

v2:
* add missing ktime.h
* ktime_before() instead of !ktime_after() (Tudor)

---
 drivers/firmware/samsung/exynos-acpm.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/firmware/samsung/exynos-acpm.c +7 -7
index a85b2dbdd9f0d7b1f327f54a0a283e4f32587a98..542eaff03f9e39422a8c5345ca75e05c1710a9ee 100644
@@ -15,6 +15,7 @@
 #include <linux/firmware/samsung/exynos-acpm-protocol.h>
 #include <linux/io.h>
 #include <linux/iopoll.h>
+#include <linux/ktime.h>
 #include <linux/mailbox/exynos-message.h>
 #include <linux/mailbox_client.h>
 #include <linux/module.h>
@@ -32,8 +33,7 @@
 
 #define ACPM_PROTOCOL_SEQNUM		GENMASK(21, 16)
 
-/* The unit of counter is 20 us. 5000 * 20 = 100 ms */
-#define ACPM_POLL_TIMEOUT		5000
+#define ACPM_POLL_TIMEOUT_US		(100 * USEC_PER_MSEC)
 #define ACPM_TX_TIMEOUT_US		500000
 
 #define ACPM_GS101_INITDATA_BASE	0xa000
@@ -284,12 +284,13 @@ static int acpm_dequeue_by_polling(struct acpm_chan *achan,
 				   const struct acpm_xfer *xfer)
 {
 	struct device *dev = achan->acpm->dev;
-	unsigned int cnt_20us = 0;
+	ktime_t timeout;
 	u32 seqnum;
 	int ret;
 
 	seqnum = FIELD_GET(ACPM_PROTOCOL_SEQNUM, xfer->txd[0]);
 
+	timeout = ktime_add_us(ktime_get(), ACPM_POLL_TIMEOUT_US);
 	do {
 		ret = acpm_get_rx(achan, xfer);
 		if (ret)
@@ -300,11 +301,10 @@ static int acpm_dequeue_by_polling(struct acpm_chan *achan,
 
 		/* Determined experimentally. */
 		usleep_range(20, 30);
-		cnt_20us++;
-	} while (cnt_20us < ACPM_POLL_TIMEOUT);
+	} while (ktime_before(ktime_get(), timeout));
 
-	dev_err(dev, "Timeout! ch:%u s:%u bitmap:%lx, cnt_20us = %d.\n",
-		achan->id, seqnum, achan->bitmap_seqnum[0], cnt_20us);
+	dev_err(dev, "Timeout! ch:%u s:%u bitmap:%lx.\n",
+		achan->id, seqnum, achan->bitmap_seqnum[0]);
 
 	return -ETIME;
 }
Signature · 1 line
2.49.0.395.g12beb8f557-goog
#2 lore raw

PATCH v3 2/2 firmware: exynos-acpm: allow use during system shutdown

In reply to André Draszik

We need to access the PMIC during late system shutdown and at that time we are not allowed to sleep anymore.

To make this case work, and since we can't detect this case in a non-racy way, switch to using udelay() unconditionally, instead of usleep_range().

---
v3:
* use udelay() unconditionally (Krzysztof)
* drop previous Rb tag

udelay(20) causes a checkpatch --strict warning (it suggests to use usleep_range() instead for usec >= 10), but that's exactly what we can not do.
Reducing the udelay to be smaller than 10 will generally cause the loop to be iterated more than once, which I wanted to avoid.
I could reflow the code to hide the actual value from checkpatch, e.g. with the help of a local variable if that is preferred to ignoring the checkpatch warning.

---
 drivers/firmware/samsung/exynos-acpm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/samsung/exynos-acpm.c +1 -1
index 542eaff03f9e39422a8c5345ca75e05c1710a9ee..379da420b9eb3fcbca5461bec7e2de6bf0774659 100644
@@ -300,7 +300,7 @@ static int acpm_dequeue_by_polling(struct acpm_chan *achan,
 			return 0;
 
 		/* Determined experimentally. */
-		usleep_range(20, 30);
+		udelay(20);
 	} while (ktime_before(ktime_get(), timeout));
 
 	dev_err(dev, "Timeout! ch:%u s:%u bitmap:%lx.\n",
Signature · 1 line
2.49.0.395.g12beb8f557-goog
#3 lore raw
In reply to André Draszik
Quoted 8 lines · André Draszik

One user of this ACPM driver is a PMIC driver that needs to communicate with the PMIC during late system shutdown [1] and at that time we are not allowed to sleep anymore.

This series address this by switching the code to using udelay() in the specific case of system shutdown. This approach was inspired by I2C's i2c_in_atomic_xfer_mode(), which has to deal with a similar corner case.

Looks like I forgot to update the paragraph here. As of v3, we now use udelay() unconditionally, instead of the fragile approach inspired by I2C, as suggested by Krzysztof.

A.

Quoted 28 lines

---
Changes in v3:
- switch to unconditional udelay() (Krzysztof)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- add missing ktime.h include
- switch to ktime_before() instead of !ktime_after()
- add link to user requiring this change to cover letter
- collect Tudor's Rb
- Link to v1: https://lore.kernel.org/r/[email protected]

---
André Draszik (2):
      firmware: exynos-acpm: use ktime APIs for timeout detection
      firmware: exynos-acpm: allow use during system shutdown

 drivers/firmware/samsung/exynos-acpm.c | 16 ++++++++--------

 1 file changed, 8 insertions(+), 8 deletions(-)

---
base-commit: 9388ec571cb1adba59d1cded2300eeb11827679c
change-id: 20250321-acpm-atomic-033775b051ef

Best regards,

#4 lore raw
In reply to André Draszik
Quoted 10 lines · André Draszik

One user of this ACPM driver is a PMIC driver that needs to communicate with the PMIC during late system shutdown [1] and at that time we are not allowed to sleep anymore.

This series address this by switching the code to using udelay() in the specific case of system shutdown. This approach was inspired by I2C's i2c_in_atomic_xfer_mode(), which has to deal with a similar corner case.

[...]

Applied, thanks!

[1/2] firmware: exynos-acpm: use ktime APIs for timeout detection

      https://git.kernel.org/krzk/linux/c/d2098981eb7b7d20edd294a8431908f8a0d2f9c0
[2/2] firmware: exynos-acpm: allow use during system shutdown
      https://git.kernel.org/krzk/linux/c/2d14c680e92f09d18b984cd1a8fae437f9ebc2ab

Best regards,

Signature · 1 line
Krzysztof Kozlowski <[email protected]>