mirror of
https://github.com/riscv-software-src/opensbi.git
synced 2026-05-23 14:21:32 +01:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user