* [PATCH v3 1/2] firmware: exynos-acpm: use ktime APIs for timeout detection
2025-03-25 9:46 [PATCH v3 0/2] firmware: exynos-acpm: allow use during system shutdown André Draszik
@ 2025-03-25 9:46 ` André Draszik
2025-03-25 9:46 ` [PATCH v3 2/2] firmware: exynos-acpm: allow use during system shutdown André Draszik
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: André Draszik @ 2025-03-25 9:46 UTC (permalink / raw)
To: Tudor Ambarus, Krzysztof Kozlowski, Alim Akhtar
Cc: Peter Griffin, Will McVicker, kernel-team, linux-kernel,
linux-samsung-soc, linux-arm-kernel, André Draszik
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.
Reviewed-by: Tudor Ambarus <tudor.ambarus@linaro.org>
Signed-off-by: André Draszik <andre.draszik@linaro.org>
---
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(-)
diff --git a/drivers/firmware/samsung/exynos-acpm.c b/drivers/firmware/samsung/exynos-acpm.c
index a85b2dbdd9f0d7b1f327f54a0a283e4f32587a98..542eaff03f9e39422a8c5345ca75e05c1710a9ee 100644
--- a/drivers/firmware/samsung/exynos-acpm.c
+++ b/drivers/firmware/samsung/exynos-acpm.c
@@ -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;
}
--
2.49.0.395.g12beb8f557-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v3 2/2] firmware: exynos-acpm: allow use during system shutdown
2025-03-25 9:46 [PATCH v3 0/2] firmware: exynos-acpm: allow use during system shutdown André Draszik
2025-03-25 9:46 ` [PATCH v3 1/2] firmware: exynos-acpm: use ktime APIs for timeout detection André Draszik
@ 2025-03-25 9:46 ` André Draszik
2025-03-25 10:15 ` [PATCH v3 0/2] " André Draszik
2025-04-07 6:47 ` Krzysztof Kozlowski
3 siblings, 0 replies; 5+ messages in thread
From: André Draszik @ 2025-03-25 9:46 UTC (permalink / raw)
To: Tudor Ambarus, Krzysztof Kozlowski, Alim Akhtar
Cc: Peter Griffin, Will McVicker, kernel-team, linux-kernel,
linux-samsung-soc, linux-arm-kernel, 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().
Signed-off-by: André Draszik <andre.draszik@linaro.org>
---
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(-)
diff --git a/drivers/firmware/samsung/exynos-acpm.c b/drivers/firmware/samsung/exynos-acpm.c
index 542eaff03f9e39422a8c5345ca75e05c1710a9ee..379da420b9eb3fcbca5461bec7e2de6bf0774659 100644
--- a/drivers/firmware/samsung/exynos-acpm.c
+++ b/drivers/firmware/samsung/exynos-acpm.c
@@ -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",
--
2.49.0.395.g12beb8f557-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v3 0/2] firmware: exynos-acpm: allow use during system shutdown
2025-03-25 9:46 [PATCH v3 0/2] firmware: exynos-acpm: allow use during system shutdown André Draszik
2025-03-25 9:46 ` [PATCH v3 1/2] firmware: exynos-acpm: use ktime APIs for timeout detection André Draszik
2025-03-25 9:46 ` [PATCH v3 2/2] firmware: exynos-acpm: allow use during system shutdown André Draszik
@ 2025-03-25 10:15 ` André Draszik
2025-04-07 6:47 ` Krzysztof Kozlowski
3 siblings, 0 replies; 5+ messages in thread
From: André Draszik @ 2025-03-25 10:15 UTC (permalink / raw)
To: Tudor Ambarus, Krzysztof Kozlowski, Alim Akhtar
Cc: Peter Griffin, Will McVicker, kernel-team, linux-kernel,
linux-samsung-soc, linux-arm-kernel
On Tue, 2025-03-25 at 09:46 +0000, André Draszik wrote:
> 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.
>
> Link: https://lore.kernel.org/all/20250323-s2mpg10-v1-29-d08943702707@linaro.org/ [1]
>
> Signed-off-by: André Draszik <andre.draszik@linaro.org>
> ---
> Changes in v3:
> - switch to unconditional udelay() (Krzysztof)
> - Link to v2: https://lore.kernel.org/r/20250324-acpm-atomic-v2-0-7d87746e1765@linaro.org
>
> 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/20250321-acpm-atomic-v1-0-fb887bde7e61@linaro.org
>
> ---
> 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,
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v3 0/2] firmware: exynos-acpm: allow use during system shutdown
2025-03-25 9:46 [PATCH v3 0/2] firmware: exynos-acpm: allow use during system shutdown André Draszik
` (2 preceding siblings ...)
2025-03-25 10:15 ` [PATCH v3 0/2] " André Draszik
@ 2025-04-07 6:47 ` Krzysztof Kozlowski
3 siblings, 0 replies; 5+ messages in thread
From: Krzysztof Kozlowski @ 2025-04-07 6:47 UTC (permalink / raw)
To: Tudor Ambarus, Krzysztof Kozlowski, Alim Akhtar,
André Draszik
Cc: Peter Griffin, Will McVicker, kernel-team, linux-kernel,
linux-samsung-soc, linux-arm-kernel
On Tue, 25 Mar 2025 09:46:06 +0000, André Draszik wrote:
> 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,
--
Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
^ permalink raw reply [flat|nested] 5+ messages in thread