Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: "Clément Léger" <cleger@rivosinc.com>
To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org
Cc: "Clément Léger" <cleger@rivosinc.com>,
	"Andrew Jones" <ajones@ventanamicro.com>,
	"Anup Patel" <apatel@ventanamicro.com>,
	"Atish Patra" <atishp@rivosinc.com>
Subject: [kvm-unit-tests PATCH v11 4/8] lib: riscv: Add functions for version checking
Date: Mon, 17 Mar 2025 17:46:49 +0100	[thread overview]
Message-ID: <20250317164655.1120015-5-cleger@rivosinc.com> (raw)
In-Reply-To: <20250317164655.1120015-1-cleger@rivosinc.com>

Version checking was done using some custom hardcoded values, backport a
few SBI function and defines from Linux to do that cleanly.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
---
 lib/riscv/asm/sbi.h | 15 +++++++++++++++
 lib/riscv/sbi.c     |  9 +++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/lib/riscv/asm/sbi.h b/lib/riscv/asm/sbi.h
index 2f4d91ef..ee9d6e50 100644
--- a/lib/riscv/asm/sbi.h
+++ b/lib/riscv/asm/sbi.h
@@ -18,6 +18,13 @@
 #define SBI_ERR_IO			-13
 #define SBI_ERR_DENIED_LOCKED		-14
 
+/* SBI spec version fields */
+#define SBI_SPEC_VERSION_MAJOR_SHIFT	24
+#define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
+#define SBI_SPEC_VERSION_MINOR_MASK	0xffffff
+#define SBI_SPEC_VERSION_MASK		((SBI_SPEC_VERSION_MAJOR_MASK << SBI_SPEC_VERSION_MAJOR_SHIFT) | \
+					SBI_SPEC_VERSION_MINOR_MASK)
+
 #ifndef __ASSEMBLER__
 #include <cpumask.h>
 
@@ -110,6 +117,13 @@ struct sbiret {
 	long value;
 };
 
+/* Make SBI version */
+static inline unsigned long sbi_mk_version(unsigned long major, unsigned long minor)
+{
+	return ((major & SBI_SPEC_VERSION_MAJOR_MASK) << SBI_SPEC_VERSION_MAJOR_SHIFT)
+		| (minor & SBI_SPEC_VERSION_MINOR_MASK);
+}
+
 struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 			unsigned long arg1, unsigned long arg2,
 			unsigned long arg3, unsigned long arg4,
@@ -124,6 +138,7 @@ struct sbiret sbi_send_ipi_cpu(int cpu);
 struct sbiret sbi_send_ipi_cpumask(const cpumask_t *mask);
 struct sbiret sbi_send_ipi_broadcast(void);
 struct sbiret sbi_set_timer(unsigned long stime_value);
+struct sbiret sbi_get_spec_version(void);
 long sbi_probe(int ext);
 
 #endif /* !__ASSEMBLER__ */
diff --git a/lib/riscv/sbi.c b/lib/riscv/sbi.c
index 02dd338c..9d4eb541 100644
--- a/lib/riscv/sbi.c
+++ b/lib/riscv/sbi.c
@@ -107,12 +107,17 @@ struct sbiret sbi_set_timer(unsigned long stime_value)
 	return sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value, 0, 0, 0, 0, 0);
 }
 
+struct sbiret sbi_get_spec_version(void)
+{
+	return sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_GET_SPEC_VERSION, 0, 0, 0, 0, 0, 0);
+}
+
 long sbi_probe(int ext)
 {
 	struct sbiret ret;
 
-	ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_GET_SPEC_VERSION, 0, 0, 0, 0, 0, 0);
-	assert(!ret.error && (ret.value & 0x7ffffffful) >= 2);
+	ret = sbi_get_spec_version();
+	assert(!ret.error && (ret.value & SBI_SPEC_VERSION_MASK) >= sbi_mk_version(0, 2));
 
 	ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, ext, 0, 0, 0, 0, 0);
 	assert(!ret.error);
-- 
2.47.2


  parent reply	other threads:[~2025-03-17 16:49 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-17 16:46 [kvm-unit-tests PATCH v11 0/8] riscv: add SBI SSE extension tests Clément Léger
2025-03-17 16:46 ` [kvm-unit-tests PATCH v11 1/8] kbuild: Allow multiple asm-offsets file to be generated Clément Léger
2025-03-17 16:46 ` [kvm-unit-tests PATCH v11 2/8] riscv: Set .aux.o files as .PRECIOUS Clément Léger
2025-03-17 16:46 ` [kvm-unit-tests PATCH v11 3/8] riscv: Use asm-offsets to generate SBI_EXT_HSM values Clément Léger
2025-03-20 13:36   ` Andrew Jones
2025-03-17 16:46 ` Clément Léger [this message]
2025-03-19 17:31   ` [kvm-unit-tests PATCH v11 4/8] lib: riscv: Add functions for version checking Andrew Jones
2025-03-20  8:08     ` Clément Léger
2025-03-17 16:46 ` [kvm-unit-tests PATCH v11 5/8] lib: riscv: Add functions to get implementer ID and version Clément Léger
2025-03-19 17:36   ` Andrew Jones
2025-03-17 16:46 ` [kvm-unit-tests PATCH v11 6/8] riscv: lib: Add SBI SSE extension definitions Clément Léger
2025-03-17 16:46 ` [kvm-unit-tests PATCH v11 7/8] lib: riscv: Add SBI SSE support Clément Léger
2025-03-17 16:46 ` [kvm-unit-tests PATCH v11 8/8] riscv: sbi: Add SSE extension tests Clément Léger
2025-03-20 13:40   ` Andrew Jones
2025-03-20 13:44     ` Clément Léger
2025-03-20 13:46   ` Andrew Jones
2025-03-20 13:50     ` Clément Léger
2025-03-19 18:01 ` [kvm-unit-tests PATCH v11 0/8] riscv: add SBI " Andrew Jones
2025-03-20  8:13   ` Clément Léger
2025-03-20 14:26 ` Andrew Jones
2025-03-22 10:46 ` 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=20250317164655.1120015-5-cleger@rivosinc.com \
    --to=cleger@rivosinc.com \
    --cc=ajones@ventanamicro.com \
    --cc=apatel@ventanamicro.com \
    --cc=atishp@rivosinc.com \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.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