From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 063261098782 for ; Fri, 20 Mar 2026 14:30:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=hSdZ2Nk9AV52+HLpvEtQ0HtrZ8m88OnRwqQdv21V3qQ=; b=Q5w7nSiBgs/Urq 28ClFIwX2gdI5KAr3YvkaTCbng/lAMVw/R5KYIJA0QoyfGN6pO3tufpRjj6y1SgupARBqI2k1SDQv OgebX7uOZbFCRrFKWxgdyml3Y8MFdvJMLtW8fWPk6i6wdf1hGs017Sku8knzFaQt+IHjKvM2BNLil isNn5uowdsNazK+Ii9cXZnZpyLJOpt3zD2kwEk/0iTUcrWLMTfcHIG4i2Cd1DXzgjNE39npVKgjWg W9cC3IiistKrX1AvPPczq0joBqWt4rJPu99MEiZxN1LFykqw6PMTElppgB0eqrH13K9+Ycp1AOqD0 +qduhhefppM7G/06adEw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1w3ars-0000000CwXg-01IZ; Fri, 20 Mar 2026 14:30:28 +0000 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1w3aro-0000000CwVu-3lHO for opensbi@lists.infradead.org; Fri, 20 Mar 2026 14:30:26 +0000 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-486fd27754bso12124585e9.3 for ; Fri, 20 Mar 2026 07:30:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=riscstar-com.20230601.gappssmtp.com; s=20230601; t=1774017023; x=1774621823; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/Oacnl03g+N4S9BJS7fsauQ0bdr58pc0cuAZ1gVCFiY=; b=LFDxopNHXz41b1ddgqTZwugAehTggOHNYtWE7KnXBghYNT3D/UyuJ101Uz7DS2a0C5 u319/oSgSCQVau0rUiqVpT4ZviPeWAX+d15OZNOwiMG7nSBnnYKvHkvjNhx3bVNg9LlY 24w+XOx2fOweHUfZIYPFA52noBHDjp6lrBeoiMlTevMhEZqNwAFejY11OWSmO819kWhU oRLVdYdAGmmAdIXNXUL3LteLkT2JsssQB966tlAuk1f4TaL9EkRqVo07zEWo/w2WYCJf vFdBLZq3rZMwv1NJXy0BmUP7JvH1NxkkI7Llwsh5UCE1EtYD2hIlOxtb/ZONHRHFjQa7 4PIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774017023; x=1774621823; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=/Oacnl03g+N4S9BJS7fsauQ0bdr58pc0cuAZ1gVCFiY=; b=fzk+thUK6M9kPCbM7vAEDU3ORrcXrA+MdvzV/rkCE3UZkHm5/W+yYJTiFYcQRrxJ4q KE5evEAgIdc3p3RJNVgdabrjT20THA2k4Lp4Aq0442tQ7jWYL93RkR1Wnjfvdn6jXTMG ogj9I5Qrno45EFwEbzK6/QCi90pzGUWFC0FauLIGnSiSYz9jQO4CNcW6nYgS8AQ1ALBR BewRYvNlowgnNCCNohzS1P0IMPYgVFtRo+yyCyNogO+aJLwJB6RhhyQxpyKqK7XsuEWB MA6bvO94yHtgvEuqDurjhp6g0QwHlaRn2fdTs9+4BlYB7CcD2/6i2xcRqizgc0ageMsQ dGdw== X-Gm-Message-State: AOJu0Yz5mcrReMDkyE0YYaBI+vkIhgS2YgvbcTIu4SHvvKYjxLbE6o4W mxlpJ2MqSs+GkxaesXEyOJRj7BxN/Bip2U4zBWVMMNjvX+JwwCjW+adDIbDSBJNd7HFiLCX64Io xSSgwZx8= X-Gm-Gg: ATEYQzyYdqwSajjdHvfaC9mOKHYqQxRzeo+sfwbuGTqzfPlbJDAS6R58iOxy0nZRho+ lc6HA7OJ8a9Li9yZrCebfx4q3PI4E92xuZGUqAvmzYn3tGfnn0p0LkIHM/98pC2I8gMarQivpYz S4Ny3LjSN/OLwvnqVtQz6kQKCFMMCRYXSsftUOe+UDJJhVr+qJM/bXb36E98rUCqLYpl5AX9Da4 LL3F+Rwt5xJ9tE9XTz8qvXT0ABE8Hkw+HEC5wNsozWRzr4JXIPfuB73eMX5+rWVGK1+RHJC+xDb GcLwEi5uAjbgOKy38XImgUEZQFPP4qTDsrYPHkdxPDu29RO9jii+0tDPkNXMf3Jc1Ncz20CmhoS Y5UoJYUXkAMdMiNdsaRBZQMzlI6/CxXbCM9f8NBfGGwgr8sr1sFyBssE7jJHRh3a4Td4ruK3q08 etHkba/gaWOlU2g1d64K4kfRFu9lHkUWcXJ66GUbWaFD13/vKVKLErps8QpGwTx9RGdaR94LS5u P03yg== X-Received: by 2002:a05:600c:3b07:b0:477:6d96:b3e5 with SMTP id 5b1f17b1804b1-486fedab303mr49161335e9.7.1774017022747; Fri, 20 Mar 2026 07:30:22 -0700 (PDT) Received: from Dev-EliteBook.Home (97e782b0.skybroadband.com. [151.231.130.176]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-486ff1aeafdsm16608435e9.21.2026.03.20.07.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Mar 2026 07:30:22 -0700 (PDT) From: dave.patel@riscstar.com To: opensbi@lists.infradead.org Cc: Scott Bambrough , Robin Randhawa , Anup Patel , Samuel Holland , Dave Patel , Ray Mao , Anup Patel , Dhaval , Peter Lin Subject: [PATCH 1/4] lib: sbi: Add RISC-V vector context save/restore support (eager switching) Date: Fri, 20 Mar 2026 14:30:15 +0000 Message-ID: <20260320143018.74191-2-dave.patel@riscstar.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260320143018.74191-1-dave.patel@riscstar.com> References: <20260320143018.74191-1-dave.patel@riscstar.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260320_073024_990440_AE4C5CD5 X-CRM114-Status: GOOD ( 15.31 ) X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "opensbi" Errors-To: opensbi-bounces+opensbi=archiver.kernel.org@lists.infradead.org From: Dave Patel 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 vl, vtype, vcsr CSRs along with storage for all 32 vector registers. The register state is saved and restored using byte-wise vector load/store instructions (vse8.v/vle8.v), making the implementation independent of current SEW/LMUL configuration. 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. A per-hart pointer is used to track the current vector context owner. Notes: - The maximum supported VLEN is capped via SBI_MAX_VLENB. - The implementation assumes the vector unit is enabled when invoked. - vstart CSR is not currently saved/restored and is expected to be zero across context switch boundaries. Signed-off-by: Dave Patel --- include/sbi/sbi_vector.h | 30 +++++++++ lib/sbi/objects.mk | 1 + lib/sbi/sbi_vector.c | 136 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 167 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..6be477c0 --- /dev/null +++ b/include/sbi/sbi_vector.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (c) 2026 RISCstar Solutions. + * + * Authors: + * Dave Patel + */ + +#ifndef __SBI_VECTOR_H__ +#define __SBI_VECTOR_H__ + +#include + +#define SBI_MAX_VLENB 256 + +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_save(struct sbi_vector_context *dst); +void sbi_vector_restore(const struct sbi_vector_context *src); + +#endif //__SBI_VECTOR_H__ + diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk index ea816e92..5c0caf39 100644 --- a/lib/sbi/objects.mk +++ b/lib/sbi/objects.mk @@ -106,3 +106,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..37239ce2 --- /dev/null +++ b/lib/sbi/sbi_vector.c @@ -0,0 +1,136 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (c) 2026 RISCstar Solutions. + * + * Authors: + * Dave Patel + */ + +#include +#include +#include +#include + +/* Per-hart vector owner */ +static inline struct sbi_vector_context **vec_owner_ptr(void) +{ + struct sbi_scratch *scratch = sbi_scratch_thishart_ptr(); + return &scratch->vec_ctx; +} + +/* Get current vector context */ +struct sbi_vector_context *sbi_current_vector_context(void) +{ + return *vec_owner_ptr(); +} + +static inline unsigned long vector_vlenb(void) +{ + unsigned long vlenb; + asm volatile ("csrr %0, vlenb" : "=r"(vlenb)); + return vlenb; +} + + +void sbi_vector_save(struct sbi_vector_context *dst) +{ + if (!dst) + return; + + uint8_t *base = dst->vregs; + unsigned long vlenb = vector_vlenb(); + + asm volatile("csrr %0, vtype" : "=r"(dst->vtype)); + asm volatile("csrr %0, vl" : "=r"(dst->vl)); + asm volatile("csrr %0, vcsr" : "=r"(dst->vcsr)); + +#define SAVE_VREG(num) \ + asm volatile("vse8.v v" #num ", (%0)" :: "r"(base + num * vlenb) : "memory") + + SAVE_VREG(0); + SAVE_VREG(1); + SAVE_VREG(2); + SAVE_VREG(3); + SAVE_VREG(4); + SAVE_VREG(5); + SAVE_VREG(6); + SAVE_VREG(7); + SAVE_VREG(8); + SAVE_VREG(9); + SAVE_VREG(10); + SAVE_VREG(11); + SAVE_VREG(12); + SAVE_VREG(13); + SAVE_VREG(14); + SAVE_VREG(15); + SAVE_VREG(16); + SAVE_VREG(17); + SAVE_VREG(18); + SAVE_VREG(19); + SAVE_VREG(20); + SAVE_VREG(21); + SAVE_VREG(22); + SAVE_VREG(23); + SAVE_VREG(24); + SAVE_VREG(25); + SAVE_VREG(26); + SAVE_VREG(27); + SAVE_VREG(28); + SAVE_VREG(29); + SAVE_VREG(30); + SAVE_VREG(31); + +#undef SAVE_VREG +} + +void sbi_vector_restore(const struct sbi_vector_context *src) +{ + if (!src) + return; + + const uint8_t *base = src->vregs; + unsigned long vlenb = vector_vlenb(); + + asm volatile("csrw vtype, %0" :: "r"(src->vtype)); + asm volatile("csrw vl, %0" :: "r"(src->vl)); + asm volatile("csrw vcsr, %0" :: "r"(src->vcsr)); + +#define RESTORE_VREG(num) \ + asm volatile("vle8.v v" #num ", (%0)" :: "r"(base + num * vlenb) : "memory") + + RESTORE_VREG(0); + RESTORE_VREG(1); + RESTORE_VREG(2); + RESTORE_VREG(3); + RESTORE_VREG(4); + RESTORE_VREG(5); + RESTORE_VREG(6); + RESTORE_VREG(7); + RESTORE_VREG(8); + RESTORE_VREG(9); + RESTORE_VREG(10); + RESTORE_VREG(11); + RESTORE_VREG(12); + RESTORE_VREG(13); + RESTORE_VREG(14); + RESTORE_VREG(15); + RESTORE_VREG(16); + RESTORE_VREG(17); + RESTORE_VREG(18); + RESTORE_VREG(19); + RESTORE_VREG(20); + RESTORE_VREG(21); + RESTORE_VREG(22); + RESTORE_VREG(23); + RESTORE_VREG(24); + RESTORE_VREG(25); + RESTORE_VREG(26); + RESTORE_VREG(27); + RESTORE_VREG(28); + RESTORE_VREG(29); + RESTORE_VREG(30); + RESTORE_VREG(31); + +#undef RESTORE_VREG +} + -- 2.43.0 -- opensbi mailing list opensbi@lists.infradead.org http://lists.infradead.org/mailman/listinfo/opensbi