lib: sbi: domain FP/Vector context support for context switch

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.

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>
Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
Link: https://lore.kernel.org/r/20260518083023.997323-4-anup.patel@oss.qualcomm.com
Signed-off-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
Dave Patel
2026-05-18 14:00:23 +05:30
committed by Anup Patel
parent 718e1d194f
commit 22d7e99ed2
3 changed files with 42 additions and 0 deletions
+6
View File
@@ -89,6 +89,12 @@ enum sbi_hart_extensions {
SBI_HART_EXT_XSIFIVE_CEASE,
/** Hart has Smrnmi extension */
SBI_HART_EXT_SMRNMI,
/** 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,
+33
View File
@@ -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 {
@@ -49,6 +51,11 @@ struct hart_context {
/** Supervisor resource management configuration register */
unsigned long srmcfg;
/** Float context state */
struct sbi_fp_context fp_ctx;
/** Vector context state */
struct sbi_vector_context *vec_ctx;
/** Reference to the owning domain */
struct sbi_domain *dom;
/** Previous context (caller) to jump to during context exits */
@@ -143,6 +150,19 @@ 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);
/* Eager context switch for float */
if (sbi_hart_has_extension(scratch, SBI_HART_EXT_F) ||
sbi_hart_has_extension(scratch, SBI_HART_EXT_D)) {
sbi_fp_save(&ctx->fp_ctx);
sbi_fp_restore(&dom_ctx->fp_ctx);
}
/* Eager context switch for vector */
if (sbi_hart_has_extension(scratch, 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));
@@ -168,6 +188,7 @@ static int switch_to_next_domain_context(struct hart_context *ctx,
static int hart_context_init(u32 hartindex)
{
size_t vec_size;
struct hart_context *ctx;
struct sbi_domain *dom;
@@ -180,6 +201,18 @@ 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)) {
vec_size = sbi_vector_context_size();
/* 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);
+3
View File
@@ -399,6 +399,9 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
__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(smrnmi, SBI_HART_EXT_SMRNMI),
__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),