OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ben Zong-You Xie <ben717@andestech.com>
To: <opensbi@lists.infradead.org>
Subject: [PATCH 6/7] lib: utils/suspend: wait for the secondary Andes harts to sleep
Date: Tue, 28 Jul 2026 16:10:40 +0800	[thread overview]
Message-ID: <20260728081041.2724668-7-ben717@andestech.com> (raw)
In-Reply-To: <20260728081041.2724668-1-ben717@andestech.com>

A secondary hart may not have reached the target sleep state by the time
the primary checks, so the one-shot check could fail spuriously. Poll
each PCS status instead and give up after HART_SLEEP_TIMEOUT_MS.

Rework atcsmu_pcs_is_sleep() into the atcsmu_hart_is_sleep() predicate so
sbi_timer_waitms_until() can drive it.

Signed-off-by: Ben Zong-You Xie <ben717@andestech.com>
---
 include/sbi_utils/hsm/fdt_hsm_andes_atcsmu.h |  7 ++++-
 lib/utils/hsm/fdt_hsm_andes_atcsmu.c         | 22 ++++++----------
 lib/utils/suspend/fdt_suspend_andes_atcsmu.c | 27 +++++++++++++++-----
 3 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/include/sbi_utils/hsm/fdt_hsm_andes_atcsmu.h b/include/sbi_utils/hsm/fdt_hsm_andes_atcsmu.h
index 20e6cea09379..bb9a2bc2e259 100644
--- a/include/sbi_utils/hsm/fdt_hsm_andes_atcsmu.h
+++ b/include/sbi_utils/hsm/fdt_hsm_andes_atcsmu.h
@@ -9,6 +9,11 @@
 
 #include <sbi/sbi_types.h>
 
+struct atcsmu_sleep_arg {
+	u32 hartid;
+	bool deep_sleep;
+};
+
 /* clang-format off */
 
 #define SCRATCH_PAD_OFFSET		0x40
@@ -61,6 +66,6 @@ int atcsmu_set_reset_vector(u64 wakeup_addr, u32 hartid);
 u32 atcsmu_get_sleep_type(u32 hartid);
 void atcsmu_write_scratch(u32 value);
 u32 atcsmu_read_scratch(void);
-bool atcsmu_pcs_is_sleep(u32 hartid, bool deep_sleep);
+bool atcsmu_hart_is_sleep(void *opaque);
 
 #endif
diff --git a/lib/utils/hsm/fdt_hsm_andes_atcsmu.c b/lib/utils/hsm/fdt_hsm_andes_atcsmu.c
index dfbb425cd2ad..d57db8f881a6 100644
--- a/lib/utils/hsm/fdt_hsm_andes_atcsmu.c
+++ b/lib/utils/hsm/fdt_hsm_andes_atcsmu.c
@@ -94,23 +94,17 @@ u32 atcsmu_read_scratch(void)
 	return readl_relaxed((char *)atcsmu_base + SCRATCH_PAD_OFFSET);
 }
 
-bool atcsmu_pcs_is_sleep(u32 hartid, bool deep_sleep)
+bool atcsmu_hart_is_sleep(void *opaque)
 {
-	u32 pcs_status = readl_relaxed((char *)atcsmu_base + PCSm_STATUS_OFFSET(hartid));
-	u32 pd_status = deep_sleep ? PD_STATUS_DEEP_SLEEP : PD_STATUS_LIGHT_SLEEP;
+	struct atcsmu_sleep_arg *arg = opaque;
 
-	if (EXTRACT_FIELD(pcs_status, PD_TYPE_MASK) != PD_TYPE_SLEEP) {
-		sbi_printf("ATCSMU: hart%d (PCS%d): failed to sleep\n", hartid, hartid + 3);
-		return false;
-	}
-
-	if (EXTRACT_FIELD(pcs_status, PD_STATUS_MASK) != pd_status) {
-		sbi_printf("ATCSMU: hart%d (PCS%d): failed to enter %s sleep\n",
-			   hartid, hartid + 3, deep_sleep ? "deep" : "light");
-		return false;
-	}
+	u32 pcs_status = readl_relaxed((char *)atcsmu_base +
+				       PCSm_STATUS_OFFSET(arg->hartid));
+	u32 pd_status = arg->deep_sleep ? PD_STATUS_DEEP_SLEEP :
+					  PD_STATUS_LIGHT_SLEEP;
 
-	return true;
+	return EXTRACT_FIELD(pcs_status, PD_TYPE_MASK) == PD_TYPE_SLEEP &&
+	       EXTRACT_FIELD(pcs_status, PD_STATUS_MASK) == pd_status;
 }
 
 static int ae350_hart_start(u32 hartid, ulong saddr)
diff --git a/lib/utils/suspend/fdt_suspend_andes_atcsmu.c b/lib/utils/suspend/fdt_suspend_andes_atcsmu.c
index ced0eb41d5b7..8d15346afcfe 100644
--- a/lib/utils/suspend/fdt_suspend_andes_atcsmu.c
+++ b/lib/utils/suspend/fdt_suspend_andes_atcsmu.c
@@ -12,22 +12,37 @@
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_hart.h>
 #include <sbi/sbi_system.h>
+#include <sbi/sbi_timer.h>
 #include <sbi_utils/cache/fdt_cmo_helper.h>
 #include <sbi_utils/fdt/fdt_driver.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/hsm/fdt_hsm_andes_atcsmu.h>
 
-static int check_secondary_harts_sleep(u32 hartid, bool deep_sleep)
+#define HART_SLEEP_TIMEOUT_MS 1000
+
+static int wait_secondary_harts_sleep(u32 hartid, bool deep_sleep)
 {
 	const struct sbi_domain *dom = &root;
 	unsigned long i;
 	u32 target;
+	struct atcsmu_sleep_arg arg;
+
+	arg.deep_sleep = deep_sleep;
 
-	/* Ensure the secondary harts entering the corresponding sleep state */
+	/* Wait for the secondary harts entering the corresponding sleep state */
 	sbi_hartmask_for_each_hartindex(i, dom->possible_harts) {
 		target = sbi_hartindex_to_hartid(i);
-		if (target != hartid && !atcsmu_pcs_is_sleep(target, deep_sleep))
-			return SBI_EFAIL;
+		if (target == hartid)
+			continue;
+
+		arg.hartid = target;
+		if (!sbi_timer_waitms_until(atcsmu_hart_is_sleep, &arg,
+					    HART_SLEEP_TIMEOUT_MS)) {
+			sbi_printf("ATCSMU: hart%u (PCS%u): timed out waiting for %s sleep\n",
+				   target, target + 3,
+				   deep_sleep ? "deep" : "light");
+			return SBI_ETIMEOUT;
+		}
 	}
 
 	return SBI_OK;
@@ -51,7 +66,7 @@ static int ae350_system_suspend(u32 sleep_type, unsigned long addr)
 	atcsmu_set_wakeup_events(PCS_WAKEUP_RTC_ALARM_MASK | PCS_WAKEUP_UART2_MASK, hartid);
 
 	if (sleep_type == SBI_SUSP_AE350_LIGHT_SLEEP) {
-		rc = check_secondary_harts_sleep(hartid, false);
+		rc = wait_secondary_harts_sleep(hartid, false);
 		if (rc)
 			return rc;
 
@@ -59,7 +74,7 @@ static int ae350_system_suspend(u32 sleep_type, unsigned long addr)
 		csr_set(CSR_MIE, MIP_SEIP);
 		atcsmu_set_command(LIGHT_SLEEP_CMD, hartid);
 	} else if (sleep_type == SBI_SUSP_SLEEP_TYPE_SUSPEND) {
-		rc = check_secondary_harts_sleep(hartid, true);
+		rc = wait_secondary_harts_sleep(hartid, true);
 		if (rc)
 			return rc;
 
-- 
2.34.1


-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

  parent reply	other threads:[~2026-07-28  8:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28  8:10 [PATCH 0/7] Fix and improve the Andes ATCSMU and LLC drivers Ben Zong-You Xie
2026-07-28  8:10 ` [PATCH 1/7] lib: utils/cache: fix andes_llcache_enable() return value Ben Zong-You Xie
2026-07-28  8:10 ` [PATCH 2/7] lib: utils/andes: add the SMU and LLC registers to the root domain Ben Zong-You Xie
2026-07-28  8:10 ` [PATCH 3/7] platform: generic/andes: pair non-retentive CSR save/restore with a flag Ben Zong-You Xie
2026-07-28  8:10 ` [PATCH 4/7] lib: utils/suspend: restore Andes CSRs on system-suspend resume Ben Zong-You Xie
2026-07-28  8:10 ` [PATCH 5/7] lib: utils/hsm: wake sleeping Andes harts with an IPI Ben Zong-You Xie
2026-07-28  8:10 ` Ben Zong-You Xie [this message]
2026-07-28  8:10 ` [PATCH 7/7] lib: utils/andes: arm the SMU sleep command last Ben Zong-You Xie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260728081041.2724668-7-ben717@andestech.com \
    --to=ben717@andestech.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox