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 1/3] lib: sbi: Add RISC-V vector context save/restore support
Date: Sat, 16 May 2026 11:07:44 +0100 [thread overview]
Message-ID: <20260516100746.13502-2-dave.patel@riscstar.com> (raw)
In-Reply-To: <20260516100746.13502-1-dave.patel@riscstar.com>
From: Dave Patel <dave.patel@riscstar.com>
Eager context switch: Add support for saving and restoring RISC-V vector
extension state in OpenSBI. This introduces a per-hart vector context
structure and helper routines to perform full context save and restore.
The vector context includes vcsr CSRs along with storage for all 32 vector
registers. The register state is saved and restored using byte-wise vector
load/store instructions (vs8r/vl8r).
The implementation follows an eager context switching model where the entire
vector state is saved and restored on every context switch. This provides a
simple and deterministic mechanism without requiring lazy trap-based
management.
Signed-off-by: Dave Patel <dave.patel@riscstar.com>
---
include/sbi/sbi_vector.h | 28 ++++++++++
lib/sbi/objects.mk | 1 +
lib/sbi/sbi_vector.c | 109 +++++++++++++++++++++++++++++++++++++++
3 files changed, 138 insertions(+)
create mode 100644 include/sbi/sbi_vector.h
create mode 100644 lib/sbi/sbi_vector.c
diff --git a/include/sbi/sbi_vector.h b/include/sbi/sbi_vector.h
new file mode 100644
index 00000000..bbd857c3
--- /dev/null
+++ b/include/sbi/sbi_vector.h
@@ -0,0 +1,28 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 RISCstar Solutions.
+ *
+ * Authors:
+ * Dave Patel <dave.patel@riscstar.com>
+ */
+
+#ifndef __SBI_VECTOR_H__
+#define __SBI_VECTOR_H__
+
+#include <sbi/sbi_types.h>
+
+struct sbi_vector_context {
+ unsigned long vcsr;
+ unsigned long vstart;
+
+ /* size depends on VLEN */
+ uint8_t vregs[];
+};
+
+void sbi_vector_save(struct sbi_vector_context *dst);
+void sbi_vector_restore(const struct sbi_vector_context *src);
+unsigned long vector_vlenb(void);
+
+#endif //__SBI_VECTOR_H__
+
diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk
index 97cc4521..ddb2e7ac 100644
--- a/lib/sbi/objects.mk
+++ b/lib/sbi/objects.mk
@@ -109,3 +109,4 @@ 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
diff --git a/lib/sbi/sbi_vector.c b/lib/sbi/sbi_vector.c
new file mode 100644
index 00000000..1d2ac944
--- /dev/null
+++ b/lib/sbi/sbi_vector.c
@@ -0,0 +1,109 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 RISCstar Solutions.
+ *
+ * Authors:
+ * Dave Patel <dave.patel@riscstar.com>
+ */
+
+#include <sbi/sbi_domain.h>
+#include <sbi/riscv_encoding.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/sbi_vector.h>
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>
+
+#ifdef OPENSBI_CC_SUPPORT_VECTOR
+
+unsigned long vector_vlenb(void)
+{
+ unsigned long vlenb = 0;
+
+ asm volatile (
+ ".option push\n\t"
+ ".option arch, +v\n\t"
+ "csrr %0, vlenb\n\t"
+ ".option pop\n\t"
+ : "=r"(vlenb)
+ :
+ : "memory");
+
+ return vlenb;
+}
+
+void sbi_vector_save(struct sbi_vector_context *dst)
+{
+ if (!dst)
+ return;
+
+ /* Step 1: Save CSRs */
+ dst->vcsr = csr_read(vcsr);
+ dst->vstart = csr_read(vstart);
+
+ ulong vlenb = vector_vlenb();
+ uint8_t *base = dst->vregs;
+
+ /* Step 3: Save vector registers */
+#define SAVE_VREG(i) \
+ ({ \
+ asm volatile( \
+ " .option push\n\t" \
+ " .option arch, +v\n\t" \
+ " vs8r.v v" #i ", (%0)\n\t" \
+ " .option pop\n\t" \
+ :: "r"(base + (i) * vlenb) : "memory"); \
+ }) \
+
+ SAVE_VREG(0);
+ SAVE_VREG(8);
+ SAVE_VREG(16);
+ SAVE_VREG(24);
+
+#undef SAVE_VREG
+}
+
+void sbi_vector_restore(const struct sbi_vector_context *src)
+{
+ if (!src)
+ return;
+
+ const uint8_t *base = src->vregs;
+ ulong vlenb = vector_vlenb();
+
+ /* Step 2: Restore vector registers */
+#define RESTORE_VREG(i) \
+ ({ \
+ asm volatile( \
+ " .option push\n\t" \
+ " .option arch, +v\n\t" \
+ " vl8r.v v" #i ", (%0)\n\t" \
+ " .option pop\n\t" \
+ :: "r"(base + (i) * vlenb) : "memory"); \
+ }) \
+
+ RESTORE_VREG(0);
+ RESTORE_VREG(8);
+ RESTORE_VREG(16);
+ RESTORE_VREG(24);
+#undef RESTORE_VREG
+
+ /* Step 3: Restore CSR's last */
+ /* Restore CSRs first */
+ csr_write(vcsr, src->vcsr);
+ csr_write(vstart, src->vstart);
+}
+
+#else
+
+void sbi_vector_save(struct sbi_vector_context *dst)
+{
+}
+
+void sbi_vector_restore(const struct sbi_vector_context *src)
+{
+}
+
+#endif /* OPENSBI_CC_SUPPORT_VECTOR */
--
2.43.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
next 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 ` dave.patel [this message]
2026-05-16 10:07 ` [PATCH v5 2/3] lib: sbi: Add floating-point context save/restore support dave.patel
2026-05-16 10:07 ` [PATCH v5 3/3] lib: sbi: domain FP/Vector context support for context switch dave.patel
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-2-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.