]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
riscv: Add basic vector extension support
authorChristoph Müllner <christoph.muellner@vrull.eu>
Tue, 14 Feb 2023 05:22:03 +0000 (06:22 +0100)
committerHugo Landau <hlandau@openssl.org>
Thu, 26 Oct 2023 14:55:49 +0000 (15:55 +0100)
The RISC-V vector extension comes with an implementation-defined
number of bits per vector register (VLEN), which can be read out at
run-time using the CSR 'vlenb' (which returns VLEN/8) followed by a
multiplication by 8 (to convert bytes to bits).

This patch introduces a RISC-V capability 'V' to specify the
availability of the vector extension. If this extension is found at
run-time, then we read out VLEN as described above and cache it.
Caching ensures that we only read the CSR once at startup.
This is necessary because reading out CSR can be expensive
(e.g. if CSR readout is implemented using trap-and-emulate).

Follow-up patches can make use of VLEN and chose the best strategy
based on the available length of the vector registers.

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21923)

crypto/riscv64cpuid.pl
crypto/riscvcap.c
include/crypto/riscv_arch.def
include/crypto/riscv_arch.h

index 675e9b6111901c1fa45dddaf39338431d466f696..5dcdc5c584cda59d8c494010741a9e2d884187aa 100644 (file)
@@ -84,6 +84,22 @@ OPENSSL_cleanse:
 ___
 }
 
+{
+my ($ret) = ('a0');
+$code .= <<___;
+################################################################################
+# size_t riscv_vlen_asm(void)
+# Return VLEN (i.e. the length of a vector register in bits).
+.p2align 3
+.globl riscv_vlen_asm
+.type riscv_vlen_asm,\@function
+riscv_vlen_asm:
+    csrr $ret, vlenb
+    slli $ret, $ret, 3
+    ret
+.size riscv_vlen_asm,.-riscv_vlen_asm
+___
+}
 
 print $code;
 close STDOUT or die "error closing STDOUT: $!";
index 1cbfb4a574f35a6807b051037aaa4b52dd771b4a..db75c21b2895c34cd98166535ef484bd5d87b616 100644 (file)
 #define OPENSSL_RISCVCAP_IMPL
 #include "crypto/riscv_arch.h"
 
+extern size_t riscv_vlen_asm(void);
+
 static void parse_env(const char *envstr);
 static void strtoupper(char *str);
 
+static size_t vlen = 0;
+
 uint32_t OPENSSL_rdtsc(void)
 {
     return 0;
@@ -67,6 +71,11 @@ static void parse_env(const char *envstr)
     }
 }
 
+size_t riscv_vlen(void)
+{
+    return vlen;
+}
+
 # if defined(__GNUC__) && __GNUC__>=2
 __attribute__ ((constructor))
 # endif
@@ -81,6 +90,9 @@ void OPENSSL_cpuid_setup(void)
 
     if ((e = getenv("OPENSSL_riscvcap"))) {
         parse_env(e);
-        return;
+    }
+
+    if (RISCV_HAS_V()) {
+        vlen = riscv_vlen_asm();
     }
 }
index 6c26dbf401421d0f904ff7cfe4acdf40d64ef233..b355fa4ddc34c6490ac029a6972bd64f1514e690 100644 (file)
@@ -32,6 +32,7 @@ RISCV_DEFINE_CAP(ZKSED, 0, 10)
 RISCV_DEFINE_CAP(ZKSH, 0, 11)
 RISCV_DEFINE_CAP(ZKR, 0, 12)
 RISCV_DEFINE_CAP(ZKT, 0, 13)
+RISCV_DEFINE_CAP(V, 0, 14)
 
 /*
  * In the future ...
index 95185841115e820c11ba4248614810c90a6fb665..8bd281ad8078f66c15df8a0538f4b8dd5ae1b74e 100644 (file)
@@ -61,4 +61,10 @@ static const size_t kRISCVNumCaps =
 #define RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE() (RISCV_HAS_ZBKB() && RISCV_HAS_ZKND() && RISCV_HAS_ZKNE())
 #define RISCV_HAS_ZKND_AND_ZKNE() (RISCV_HAS_ZKND() && RISCV_HAS_ZKNE())
 
+/*
+ * Get the size of a vector register in bits (VLEN).
+ * If RISCV_HAS_V() is false, then this returns 0.
+ */
+size_t riscv_vlen(void);
+
 #endif