All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <ajones@ventanamicro.com>
To: opensbi@lists.infradead.org
Subject: [RFC PATCH 09/11] lib: sbi: Implement system suspend
Date: Fri,  6 Jan 2023 12:22:07 +0100	[thread overview]
Message-ID: <20230106112209.441825-10-ajones@ventanamicro.com> (raw)
In-Reply-To: <20230106112209.441825-1-ajones@ventanamicro.com>

Fill the implementation of the system suspend ecall. A platform
implementation of the suspend callbacks is still required for this
to do anything.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
---
 lib/sbi/sbi_system.c | 57 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)

diff --git a/lib/sbi/sbi_system.c b/lib/sbi/sbi_system.c
index 5c123a6c9d8d..c18562cdcfab 100644
--- a/lib/sbi/sbi_system.c
+++ b/lib/sbi/sbi_system.c
@@ -116,5 +116,60 @@ bool sbi_system_suspend_supported(u32 sleep_type)
 
 int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque)
 {
-	return 0;
+	int ret = SBI_ENOTSUPP;
+	const struct sbi_domain *dom = sbi_domain_thishart_ptr();
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	void (*jump_warmboot)(void) = (void (*)(void))scratch->warmboot_addr;
+	unsigned int hartid = current_hartid();
+	unsigned long prev_mode;
+	unsigned long i;
+
+	if (!dom || !dom->system_suspend_allowed)
+		return SBI_EFAIL;
+
+	if (!suspend_dev || !suspend_dev->system_suspend)
+		return SBI_EFAIL;
+
+	if (!sbi_system_suspend_supported(sleep_type))
+		return SBI_ENOTSUPP;
+
+	prev_mode = (csr_read(CSR_MSTATUS) & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
+	if (prev_mode != PRV_S && prev_mode != PRV_U)
+		return SBI_EFAIL;
+
+	sbi_hartmask_for_each_hart(i, &dom->assigned_harts) {
+		if (i == hartid)
+			continue;
+		if (__sbi_hsm_hart_get_state(i) != SBI_HSM_STATE_STOPPED)
+			return SBI_EFAIL;
+	}
+
+	if (!sbi_domain_check_addr(dom, resume_addr, prev_mode,
+				   SBI_DOMAIN_EXECUTE))
+		return SBI_EINVALID_ADDR;
+
+	if (!sbi_hsm_hart_change_state(scratch, SBI_HSM_STATE_STARTED,
+				       SBI_HSM_STATE_SUSPENDED))
+		return SBI_EFAIL;
+
+	/* Prepare for resume */
+	scratch->next_mode = prev_mode;
+	scratch->next_addr = resume_addr;
+	scratch->next_arg1 = opaque;
+
+	__sbi_hsm_suspend_non_ret_save(scratch);
+
+	/* Suspend */
+	ret = suspend_dev->system_suspend(sleep_type);
+	if (ret != SBI_OK) {
+		if (!sbi_hsm_hart_change_state(scratch, SBI_HSM_STATE_SUSPENDED,
+					       SBI_HSM_STATE_STARTED))
+			sbi_hart_hang();
+		return ret;
+	}
+
+	/* Resume */
+	jump_warmboot();
+
+	__builtin_unreachable();
 }
-- 
2.39.0



  parent reply	other threads:[~2023-01-06 11:22 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-06 11:21 [RFC PATCH 00/11] SBI system suspend (SUSP) extension Andrew Jones
2023-01-06 11:21 ` [RFC PATCH 01/11] lib: sbi_hsm: Factor out invalid state detection Andrew Jones
2023-01-17  3:36   ` Anup Patel
2023-01-06 11:22 ` [RFC PATCH 02/11] lib: sbi_hsm: Don't try to restore state on failed change Andrew Jones
2023-01-17  3:36   ` Anup Patel
2023-01-06 11:22 ` [RFC PATCH 03/11] lib: sbi_hsm: Ensure errors are consistent with spec Andrew Jones
2023-01-17  3:37   ` Anup Patel
2023-01-06 11:22 ` [RFC PATCH 04/11] lib: sbi_hsm: Move misplaced comment Andrew Jones
2023-01-17  3:39   ` Anup Patel
2023-01-06 11:22 ` [RFC PATCH 05/11] lib: sbi_hsm: Remove unnecessary include Andrew Jones
2023-01-17  3:39   ` Anup Patel
2023-01-06 11:22 ` [RFC PATCH 06/11] lib: sbi_hsm: Export some functions Andrew Jones
2023-01-17  3:40   ` Anup Patel
2023-01-06 11:22 ` [RFC PATCH 07/11] lib: sbi: Add system suspend skeleton Andrew Jones
2023-01-17  3:46   ` Anup Patel
2023-01-06 11:22 ` [RFC PATCH 08/11] lib: sbi: Add system_suspend_allowed domain property Andrew Jones
2023-01-17  3:47   ` Anup Patel
2023-01-06 11:22 ` Andrew Jones [this message]
2023-01-17  3:49   ` [RFC PATCH 09/11] lib: sbi: Implement system suspend Anup Patel
2023-01-06 11:22 ` [RFC PATCH 10/11] docs: Correct opensbi-domain property name Andrew Jones
2023-01-17  3:51   ` Anup Patel
2023-01-06 11:22 ` [RFC PATCH 11/11] platform: generic: Add system suspend test Andrew Jones
2023-01-17  3:45   ` Anup Patel
2023-01-06 11:34 ` [RFC PATCH 00/11] SBI system suspend (SUSP) extension Andrew Jones
2023-01-17  3:54 ` Anup Patel
2023-01-17  9:33   ` Andrew Jones

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=20230106112209.441825-10-ajones@ventanamicro.com \
    --to=ajones@ventanamicro.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.