[sw] Introduce rv_core_ibex_testutils
This adds some common methods to rv_core_ibex_testutils
that can be reused in other testutils and tests.
Two rnd data related methods are added in the commit.
Signed-off-by: Srikrishna Iyer <sriyer@google.com>
diff --git a/sw/device/lib/testing/BUILD b/sw/device/lib/testing/BUILD
index ad0daf7..8630dbd 100644
--- a/sw/device/lib/testing/BUILD
+++ b/sw/device/lib/testing/BUILD
@@ -235,6 +235,18 @@
)
cc_library(
+ name = "rv_core_ibex_testutils",
+ srcs = ["rv_core_ibex_testutils.c"],
+ hdrs = ["rv_core_ibex_testutils.h"],
+ target_compatible_with = [OPENTITAN_CPU],
+ deps = [
+ "//sw/device/lib/dif:rv_core_ibex",
+ "//sw/device/lib/runtime:ibex",
+ "//sw/device/lib/testing/test_framework:check",
+ ],
+)
+
+cc_library(
name = "rv_plic_testutils",
srcs = ["rv_plic_testutils.c"],
hdrs = ["rv_plic_testutils.h"],
diff --git a/sw/device/lib/testing/rv_core_ibex_testutils.c b/sw/device/lib/testing/rv_core_ibex_testutils.c
new file mode 100644
index 0000000..439eb3f
--- /dev/null
+++ b/sw/device/lib/testing/rv_core_ibex_testutils.c
@@ -0,0 +1,29 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "sw/device/lib/dif/dif_rv_core_ibex.h"
+#include "sw/device/lib/runtime/ibex.h"
+#include "sw/device/lib/testing/test_framework/check.h"
+
+bool rv_core_ibex_testutils_is_rnd_data_valid(
+ const dif_rv_core_ibex_t *rv_core_ibex) {
+ dif_rv_core_ibex_rnd_status_t rnd_status;
+ CHECK_DIF_OK(dif_rv_core_ibex_get_rnd_status(rv_core_ibex, &rnd_status));
+ return rnd_status & kDifRvCoreIbexRndStatusValid;
+}
+
+uint32_t rv_core_ibex_testutils_get_rnd_data(
+ const dif_rv_core_ibex_t *rv_core_ibex, uint32_t timeout_usec) {
+ IBEX_SPIN_FOR(rv_core_ibex_testutils_is_rnd_data_valid(rv_core_ibex),
+ timeout_usec);
+
+ uint32_t rnd_data;
+ CHECK_DIF_OK(dif_rv_core_ibex_read_rnd_data(rv_core_ibex, &rnd_data));
+ CHECK(rnd_data != 0);
+ CHECK(rnd_data != UINT32_MAX);
+ return rnd_data;
+}
diff --git a/sw/device/lib/testing/rv_core_ibex_testutils.h b/sw/device/lib/testing/rv_core_ibex_testutils.h
new file mode 100644
index 0000000..7ce4ef4
--- /dev/null
+++ b/sw/device/lib/testing/rv_core_ibex_testutils.h
@@ -0,0 +1,36 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_RV_CORE_IBEX_TESTUTILS_H_
+#define OPENTITAN_SW_DEVICE_LIB_TESTING_RV_CORE_IBEX_TESTUTILS_H_
+
+#include <stdbool.h>
+#include <stdint.h>
+
+/**
+ * Returns the validity of random data read from the entropy source as bool.
+ *
+ * Returns true if the random data read from the entropy source is valid, else
+ * false. Whether the random data is FIPS compliant or not is not indicated.
+ * @param rv_core_ibex An rv_core_ibex handle.
+ * @return The validity of random data read from the entropy source.
+ */
+bool rv_core_ibex_testutils_is_rnd_data_valid(
+ const dif_rv_core_ibex_t *rv_core_ibex);
+
+/**
+ * Returns a random data read from the entropy source.
+ *
+ * A spinwait loop is invoked to wait for the random data fetched from the
+ * entropy source to be valid. The spinwait times out after the timeout_usec
+ * microseconds. Once valid, the random data is read and checked before it is
+ * returned.
+ * @param rv_core_ibex An rv_core_ibex handle.
+ * @param timeout_usec Timeout in microseconds.
+ * @return The random data read from the entropy source.
+ */
+uint32_t rv_core_ibex_testutils_get_rnd_data(
+ const dif_rv_core_ibex_t *rv_core_ibex, uint32_t timeout_usec);
+
+#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_RV_CORE_IBEX_TESTUTILS_H_