Skip to contentSkip to conversation

FPU and Vector Lazy context switch implementation Test changes

PATCH New [email protected] 1 participant 1 message Mar 11

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 FPU and Vector Lazy context switch implementation Test changes

From: Dave Patel <[email protected]>

---
 .gitignore                    |   1 +
 firmware/payloads/test_main.c |  42 ++++++++-
 include/sbi/sbi_domain.h      |  21 +++++
 include/sbi/sbi_fp.h          |  33 +++++++
 include/sbi/sbi_scratch.h     |   5 ++
 include/sbi/sbi_trap.h        |   3 +
 include/sbi/sbi_vector.h      |  40 +++++++++
 lib/sbi/objects.mk            |   2 +
 lib/sbi/sbi_fp.c              | 138 +++++++++++++++++++++++++++++
 lib/sbi/sbi_hart.c            |   5 ++
 lib/sbi/sbi_illegal_insn.c    |  68 ++++++++++++++-
 lib/sbi/sbi_trap.c            |  37 ++++++++
 lib/sbi/sbi_vector.c          | 159 ++++++++++++++++++++++++++++++++++
 lib/utils/irqchip/aplic.c     |   2 +
 14 files changed, 550 insertions(+), 6 deletions(-)
 create mode 100644 include/sbi/sbi_fp.h
 create mode 100644 include/sbi/sbi_vector.h
 create mode 100644 lib/sbi/sbi_fp.c
 create mode 100644 lib/sbi/sbi_vector.c
.gitignore +1 -0
index 94caeb90..7208d3f6 100644
@@ -20,3 +20,4 @@ install/
 tags
 cscope*
 *~
+out/
firmware/payloads/test_main.c +39 -3

48 lines not on this page · raw message

include/sbi/sbi_domain.h +21 -0
index 882b62c2..95e588d9 100644
@@ -183,6 +183,27 @@ static inline bool sbi_domain_memregion_is_subset(
 	return false;
 }
 
+/* Floating point context */
+struct sbi_domain_fp_state {
+    bool     valid;
+    uint64_t f[32];   /* f0–f31 */
+    uint32_t fcsr;
+};
+
+/* Vector context (VLEN-agnostic raw blob) */
+struct sbi_domain_vec_state {
+    bool     valid;
+    uint64_t v[32 * 2]; /* placeholder blob for v0–v31 */
+    uint64_t vtype;
+    uint64_t vl;
+};
+
+/* Extended domain context */
+struct sbi_domain_extctx {
+	struct sbi_domain_fp_state  fp;
+	struct sbi_domain_vec_state vec;
+};
+
 /** Representation of OpenSBI domain */
 struct sbi_domain {
 	/** Node in linked list of domains */
include/sbi/sbi_fp.h new file +33 -0
new file mode 100644
index 00000000..b2cb9267
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (c) 2026 RISCstar Solutions.
+ *
+ * Authors:
+ *     Dave Patel <[email protected]>
+ */
+
+#ifndef __SBI_FP_H__
+#define __SBI_FP_H__
+
+/* Floating-point status field (FS) in sstatus */
+#define SSTATUS_FS_SHIFT   13
+#define SSTATUS_FS_MASK    (3UL << SSTATUS_FS_SHIFT)
+
+#define SSTATUS_FS_OFF     (0UL << SSTATUS_FS_SHIFT)
+#define SSTATUS_FS_INITIAL (1UL << SSTATUS_FS_SHIFT)
+#define SSTATUS_FS_CLEAN   (2UL << SSTATUS_FS_SHIFT)
+#define SSTATUS_FS_DIRTY   (3UL << SSTATUS_FS_SHIFT)
+
+struct sbi_fp_context {
+    unsigned long f[32];
+    unsigned long fcsr;
+};
+
+struct sbi_fp_context *sbi_current_fp_context(void);
+void sbi_fp_lazy_restore(struct sbi_fp_context *ctx);
+void sbi_lazy_fp_switch(struct sbi_fp_context *ctx);
+void sbi_fp_save(struct sbi_fp_context *dst);
+void sbi_fp_restore(const struct sbi_fp_context *src);
+
+#endif //__SBI_VECTOR_H__
+
include/sbi/sbi_scratch.h +5 -0
index 58d54628..13e4a294 100644
@@ -54,6 +54,8 @@
 #ifndef __ASSEMBLER__
 
 #include <sbi/sbi_types.h>
+#include <sbi/sbi_fp.h>
+#include <sbi/sbi_vector.h>
 
 /** Representation of per-HART scratch space */
 struct sbi_scratch {
@@ -87,6 +89,9 @@ struct sbi_scratch {
 	unsigned long options;
 	/** Index of the hart */
 	unsigned long hartindex;
+    /* Current FP/Vector context owner per hart */
+    struct sbi_fp_context     *fp_ctx;
+    struct sbi_vector_context *vec_ctx;
 };
 
 /**
include/sbi/sbi_trap.h +3 -0
index 731a0c98..70d8bd2e 100644
@@ -239,6 +239,9 @@ struct sbi_trap_context {
 	struct sbi_trap_regs regs;
 	/** Trap details */
 	struct sbi_trap_info trap;
+    /* FP/Vector ownership pointers for lazy context switching */
+    struct sbi_fp_context     *fp_ctx;
+    struct sbi_vector_context *vec_ctx;
 	/** Pointer to previous trap context */
 	struct sbi_trap_context *prev_context;
 };
include/sbi/sbi_vector.h new file +40 -0
new file mode 100644
index 00000000..06302d70
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (c) 2026 RISCstar Solutions.
+ *
+ * Authors:
+ *     Dave Patel <[email protected]>
+ */
+
+#ifndef __SBI_VECTOR_H__
+#define __SBI_VECTOR_H__
+
+#include <sbi/sbi_types.h>
+
+#define SBI_MAX_VLENB 256
+
+/* Vector status field (VS) in sstatus */
+#define SSTATUS_VS_SHIFT   9
+#define SSTATUS_VS_MASK    (3UL << SSTATUS_VS_SHIFT)
+
+#define SSTATUS_VS_OFF     (0UL << SSTATUS_VS_SHIFT)
+#define SSTATUS_VS_INITIAL (1UL << SSTATUS_VS_SHIFT)
+#define SSTATUS_VS_CLEAN   (2UL << SSTATUS_VS_SHIFT)
+#define SSTATUS_VS_DIRTY   (3UL << SSTATUS_VS_SHIFT)
+
+struct sbi_vector_context {
+    unsigned long vl;
+    unsigned long vtype;
+    unsigned long vcsr;
+
+    /* size depends on VLEN */
+    uint8_t vregs[32 * SBI_MAX_VLENB];
+};
+
+struct sbi_vector_context *sbi_current_vector_context(void);
+void sbi_vector_lazy_restore(struct sbi_vector_context *ctx);
+void sbi_lazy_vec_switch(struct sbi_vector_context *ctx);
+void sbi_vector_save(struct sbi_vector_context *dst);
+void sbi_vector_restore(const struct sbi_vector_context *src);
+#endif //__SBI_VECTOR_H__
+
lib/sbi/objects.mk +2 -0
index ea816e92..ca560c2e 100644
@@ -106,3 +106,5 @@ libsbi-objs-y += sbi_trap_v_ldst.o
 libsbi-objs-y += sbi_unpriv.o
 libsbi-objs-y += sbi_expected_trap.o
 libsbi-objs-y += sbi_cppc.o
+libsbi-objs-y += sbi_vector.o
+libsbi-objs-y += sbi_fp.o
lib/sbi/sbi_fp.c new file +138 -0

138 lines not on this page · raw message

lib/sbi/sbi_hart.c +5 -0
index 60e95bca..ba7ea74d 100644
@@ -814,6 +814,11 @@ sbi_hart_switch_mode(unsigned long arg0, unsigned long arg1,
 			csr_write(CSR_VSIE, 0);
 			csr_write(CSR_VSATP, 0);
 		} else {
+            /* Disable FP and vector units for lazy restore */
+            unsigned long sstatus = csr_read(CSR_SSTATUS);
+            sstatus &= ~SSTATUS_FS;
+            sstatus &= ~SSTATUS_VS;
+            csr_write(CSR_SSTATUS, sstatus);
 			csr_write(CSR_STVEC, next_addr);
 			csr_write(CSR_SSCRATCH, 0);
 			csr_write(CSR_SIE, 0);
lib/sbi/sbi_illegal_insn.c +65 -3

102 lines not on this page · raw message

lib/sbi/sbi_trap.c +37 -0
index f41db4d1..b4541db8 100644
@@ -24,6 +24,8 @@
 #include <sbi/sbi_sse.h>
 #include <sbi/sbi_timer.h>
 #include <sbi/sbi_trap.h>
+#include <sbi/sbi_fp.h>
+#include <sbi/sbi_vector.h>
 
 static void sbi_trap_error_one(const struct sbi_trap_context *tcntx,
 			       const char *prefix, u32 hartid, u32 depth)
@@ -92,6 +94,14 @@ static void __noreturn sbi_trap_error(const char *msg, int rc,
 	sbi_hart_hang();
 }
 
+void sbi_lazy_context_switch(struct sbi_trap_context *old_ctx,
+                              struct sbi_trap_context *new_ctx)
+{
+    /* Lazy FP/Vector switch for new context */
+    sbi_lazy_fp_switch(new_ctx->fp_ctx);
+    sbi_lazy_vec_switch(new_ctx->vec_ctx);
+}
+
 /**
  * Redirect trap to lower privledge mode (S-mode or U-mode)
  *
@@ -309,6 +319,22 @@ struct sbi_trap_context *sbi_trap_handler(struct sbi_trap_context *tcntx)
 	const struct sbi_trap_info *trap = &tcntx->trap;
 	struct sbi_trap_regs *regs = &tcntx->regs;
 	ulong mcause = tcntx->trap.cause;
+	static bool fpflag = false;
+	static bool vecflag = false;
+
+	unsigned long sstatus = csr_read(CSR_SSTATUS);
+
+ 	if ((sstatus & SSTATUS_FS) == SSTATUS_FS_DIRTY) {
+		fpflag = true;
+		struct sbi_fp_context *current = scratch->fp_ctx;
+		sbi_fp_save(current);
+	}
+
+	if ((sstatus & SSTATUS_VS) == SSTATUS_VS_DIRTY) {
+		vecflag = true;
+		struct sbi_vector_context *current = scratch->vec_ctx;
+		sbi_vector_save(current);
+	}
 
 	/* Update trap context pointer */
 	tcntx->prev_context = sbi_trap_get_context(scratch);
@@ -373,5 +399,16 @@ trap_done:
 		sbi_sse_process_pending_events(regs);
 
 	sbi_trap_set_context(scratch, tcntx->prev_context);
+
+	if (fpflag == true) {
+		struct sbi_fp_context *current = scratch->fp_ctx;
+		sbi_fp_restore(current);
+	}
+
+	if (vecflag == true) {
+		struct sbi_vector_context *current = scratch->vec_ctx;
+		sbi_vector_restore(current);
+	}
+
 	return tcntx;
 }
lib/sbi/sbi_vector.c new file +159 -0

159 lines not on this page · raw message

lib/utils/irqchip/aplic.c +2 -0
index 36e40245..4d0329fb 100644
@@ -395,6 +395,7 @@ static void aplic_hwirq_eoi(struct sbi_irqchip_device *chip, u32 hwirq)
 	writel(hwirq, idc + APLIC_CLRIPNUM);
 }
 
+#ifdef APLIC_QEMU_VIRQ_TEST
 static void aplic_set_target(struct sbi_irqchip_device *chip, u32 hwirq,
 			     u32 hart_idx)
 {
@@ -411,6 +412,7 @@ static void aplic_set_target(struct sbi_irqchip_device *chip, u32 hwirq,
 	writel(APLIC_ENABLE_IDELIVERY, (void *)(idc + APLIC_IDC_IDELIVERY));
 	writel(APLIC_ENABLE_ITHRESHOLD, (void *)(idc + APLIC_IDC_ITHRESHOLD));
 }
+#endif
 
 static int aplic_hwirq_setup(struct sbi_irqchip_device *chip, u32 hwirq)
 {
Signature · 7 lines
2.43.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi