From: dave.patel@riscstar.com
To: Anup Patel <anup.patel@oss.qualcomm.com>,
Anup Patel <anuppate@qti.qualcomm.com>
Cc: Scott Bambrough <scott@riscstar.com>,
Robin Randhawa <robin.randhawa@sifive.com>,
Samuel Holland <samuel.holland@sifive.com>,
Dave Patel <dave.patel@riscstar.com>,
Ray Mao <raymond.mao@riscstar.com>, Dhaval <dhaval@rivosinc.com>,
Peter Lin <peter.lin@sifive.com>,
opensbi@lists.infradead.org
Subject: [PATCH v5 3/3] lib: sbi: domain FP/Vector context support for context switch
Date: Sat, 16 May 2026 11:07:46 +0100 [thread overview]
Message-ID: <20260516100746.13502-4-dave.patel@riscstar.com> (raw)
In-Reply-To: <20260516100746.13502-1-dave.patel@riscstar.com>
From: Dave Patel <dave.patel@riscstar.com>
This patch adds proper support for per-domain floating-point (FP) and
vector (V) contexts in the domain context switch logic. Each domain
now maintains its own FP and vector state, which is saved and restored
during domain switches.
Conditionalize FP and Vector save/restore based on extensions, unconditional
save and restore of floating-point (FP) and Vector registers fails on
generic platform firmware. This firmware must run on multiple platforms
that may lack these extensions.
Address this by conditionally executing FP save/restore only if the underlying
hart supports the F or D extensions. Similarly, perform Vector save/restore
only if the hart supports the Vector extension.
Changes include:
- Added `fp_ctx` and `vec_ctx` members to `struct hart_context`.
- Introduced dynamic vector struct allocation for vlenb in 'struct hart_context'
to allocate and free per-domain FP and vector context.
- Modified `sbi_domain_register()` to initialize FP/Vector context per domain.
- Updated `switch_to_next_domain_context()` to save/restore FP and vector
contexts safely:
- Ensures FS/VS fields in `mstatus` are enabled (set to Initial) only if Off.
- Added runtime checks for FP and vector extensions where needed.
- Added SBI_HART_EXT_F, SBI_HART_EXT_D, SBI_HART_EXT_V to enum
sbi_hart_extensions and the sbi_hart_ext[] array. Use sbi_hart_has_extension()
to check for these capabilities before performing the context switches
This improves support for multi-domain systems with FP and Vector
extensions, and prevents corruption of FP/Vector state during domain
switches.
Signed-off-by: Dave Patel <dave.patel@riscstar.com>
---
include/sbi/sbi_hart.h | 6 ++++++
lib/sbi/sbi_domain_context.c | 40 ++++++++++++++++++++++++++++++++++++
lib/sbi/sbi_hart.c | 3 +++
3 files changed, 49 insertions(+)
diff --git a/include/sbi/sbi_hart.h b/include/sbi/sbi_hart.h
index a788b34c..68a01b97 100644
--- a/include/sbi/sbi_hart.h
+++ b/include/sbi/sbi_hart.h
@@ -87,6 +87,12 @@ enum sbi_hart_extensions {
SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1,
/** Hart has Xsfcease extension */
SBI_HART_EXT_XSIFIVE_CEASE,
+ /** Hart has V extension */
+ SBI_HART_EXT_V,
+ /** Hart has F extension */
+ SBI_HART_EXT_F,
+ /** Hart has D extension */
+ SBI_HART_EXT_D,
/** Maximum index of Hart extension */
SBI_HART_EXT_MAX,
diff --git a/lib/sbi/sbi_domain_context.c b/lib/sbi/sbi_domain_context.c
index 158f4990..46485728 100644
--- a/lib/sbi/sbi_domain_context.c
+++ b/lib/sbi/sbi_domain_context.c
@@ -18,6 +18,8 @@
#include <sbi/sbi_domain_context.h>
#include <sbi/sbi_platform.h>
#include <sbi/sbi_trap.h>
+#include <sbi/sbi_vector.h>
+#include <sbi/sbi_fp.h>
/** Context representation for a hart within a domain */
struct hart_context {
@@ -55,6 +57,11 @@ struct hart_context {
struct hart_context *prev_ctx;
/** Is context initialized and runnable */
bool initialized;
+
+ /** float context state */
+ struct sbi_fp_context fp_ctx;
+ /** vector context state */
+ struct sbi_vector_context *vec_ctx;
};
static struct sbi_domain_data dcpriv;
@@ -143,6 +150,25 @@ static int switch_to_next_domain_context(struct hart_context *ctx,
if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSQOSID))
ctx->srmcfg = csr_swap(CSR_SRMCFG, dom_ctx->srmcfg);
+ /* Make sure FS and VS is on before context switch */
+ csr_set(CSR_MSTATUS, MSTATUS_FS | MSTATUS_VS);
+
+ /* Eager context switch F and V */
+
+ if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
+ SBI_HART_EXT_F) ||
+ sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
+ SBI_HART_EXT_D)) {
+ sbi_fp_save(&ctx->fp_ctx);
+ sbi_fp_restore(&dom_ctx->fp_ctx);
+ }
+
+ if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
+ SBI_HART_EXT_V)) {
+ sbi_vector_save(ctx->vec_ctx);
+ sbi_vector_restore(dom_ctx->vec_ctx);
+ }
+
/* Save current trap state and restore target domain's trap state */
trap_ctx = sbi_trap_get_context(scratch);
sbi_memcpy(&ctx->trap_ctx, trap_ctx, sizeof(*trap_ctx));
@@ -180,6 +206,20 @@ static int hart_context_init(u32 hartindex)
if (!ctx)
return SBI_ENOMEM;
+ if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
+ SBI_HART_EXT_V)) {
+ unsigned long vlenb = vector_vlenb();
+ /* Calculate size: base struct + 32 registers of vlenb size */
+ size_t vec_size = sizeof(struct sbi_vector_context) + (32 * vlenb);
+
+ /* Allocate the vector context pointer */
+ ctx->vec_ctx = sbi_zalloc(vec_size);
+ if (!ctx->vec_ctx) {
+ sbi_free(ctx);
+ return SBI_ENOMEM;
+ }
+ }
+
/* Bind context and domain */
ctx->dom = dom;
hart_context_set(dom, hartindex, ctx);
diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c
index 60e95bca..b5e0ee10 100644
--- a/lib/sbi/sbi_hart.c
+++ b/lib/sbi/sbi_hart.c
@@ -396,6 +396,9 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
__SBI_HART_EXT_DATA(ssstateen, SBI_HART_EXT_SSSTATEEN),
__SBI_HART_EXT_DATA(xsfcflushdlone, SBI_HART_EXT_XSIFIVE_CFLUSH_D_L1),
__SBI_HART_EXT_DATA(xsfcease, SBI_HART_EXT_XSIFIVE_CEASE),
+ __SBI_HART_EXT_DATA(v, SBI_HART_EXT_V),
+ __SBI_HART_EXT_DATA(f, SBI_HART_EXT_F),
+ __SBI_HART_EXT_DATA(d, SBI_HART_EXT_D),
};
_Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
--
2.43.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
prev parent reply other threads:[~2026-05-16 10:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-16 10:07 [PATCH v5 0/3] Add eager FP and RISC-V vector context switching support dave.patel
2026-05-16 10:07 ` [PATCH v5 1/3] lib: sbi: Add RISC-V vector context save/restore support dave.patel
2026-05-16 10:07 ` [PATCH v5 2/3] lib: sbi: Add floating-point " dave.patel
2026-05-16 10:07 ` dave.patel [this message]
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=20260516100746.13502-4-dave.patel@riscstar.com \
--to=dave.patel@riscstar.com \
--cc=anup.patel@oss.qualcomm.com \
--cc=anuppate@qti.qualcomm.com \
--cc=dhaval@rivosinc.com \
--cc=opensbi@lists.infradead.org \
--cc=peter.lin@sifive.com \
--cc=raymond.mao@riscstar.com \
--cc=robin.randhawa@sifive.com \
--cc=samuel.holland@sifive.com \
--cc=scott@riscstar.com \
/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.