[dv/full_chip] Create pwrmgr testutils
There are many tests that need to trigger wakeups from different IPs,
so this abstracts one of the common pwrmgr operation.
Signed-off-by: Guillermo Maturana <maturana@google.com>
diff --git a/sw/device/lib/testing/meson.build b/sw/device/lib/testing/meson.build
index cd44d5a..30818ce 100644
--- a/sw/device/lib/testing/meson.build
+++ b/sw/device/lib/testing/meson.build
@@ -42,6 +42,34 @@
),
)
+# pwrmgr test utilities.
+sw_lib_testing_pwrmgr_testutils = declare_dependency(
+ link_with: static_library(
+ 'sw_lib_testing_pwrmgr_testutils',
+ sources: [
+ 'pwrmgr_testutils.c'
+ ],
+ dependencies: [
+ sw_lib_mmio,
+ sw_lib_dif_pwrmgr,
+ ],
+ ),
+)
+
+# rstmgr test utilities.
+sw_lib_testing_rstmgr_testutils = declare_dependency(
+ link_with: static_library(
+ 'sw_lib_testing_rstmgr_testutils',
+ sources: [
+ 'rstmgr_testutils.c'
+ ],
+ dependencies: [
+ sw_lib_mmio,
+ sw_lib_dif_rstmgr,
+ ],
+ ),
+)
+
sw_lib_testing_rv_plic_testutils = declare_dependency(
link_with: static_library(
'sw_lib_testing_rv_plic_testutils',
diff --git a/sw/device/lib/testing/pwrmgr_testutils.c b/sw/device/lib/testing/pwrmgr_testutils.c
new file mode 100644
index 0000000..01469c5
--- /dev/null
+++ b/sw/device/lib/testing/pwrmgr_testutils.c
@@ -0,0 +1,37 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#include "sw/device/lib/testing/pwrmgr_testutils.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "sw/device/lib/base/mmio.h"
+#include "sw/device/lib/dif/dif_pwrmgr.h"
+#include "sw/device/lib/testing/check.h"
+
+void pwrmgr_testutils_enable_low_power(
+ dif_pwrmgr_t *pwrmgr, dif_pwrmgr_request_sources_t wakeups,
+ dif_pwrmgr_domain_config_t domain_config) {
+ // Enable low power on the next WFI with clocks and power domains configured
+ // per domain_config.
+
+ // Issue #6504: USB clock in active power must be left enabled.
+ domain_config |= kDifPwrmgrDomainOptionUsbClockInActivePower;
+
+ CHECK_DIF_OK(
+ dif_pwrmgr_set_request_sources(pwrmgr, kDifPwrmgrReqTypeWakeup, wakeups));
+ CHECK_DIF_OK(dif_pwrmgr_set_domain_config(pwrmgr, domain_config));
+ CHECK_DIF_OK(dif_pwrmgr_low_power_set_enabled(pwrmgr, kDifToggleEnabled));
+}
+
+bool pwrmgr_testutils_is_wakeup_reason(dif_pwrmgr_t *pwrmgr,
+ dif_pwrmgr_request_sources_t reasons) {
+ dif_pwrmgr_wakeup_reason_t wakeup_reason;
+ CHECK_DIF_OK(dif_pwrmgr_wakeup_reason_get(pwrmgr, &wakeup_reason));
+
+ return (wakeup_reason.request_sources == 0 ||
+ wakeup_reason.types == kDifPwrmgrWakeupTypeRequest) &&
+ wakeup_reason.request_sources == reasons;
+}
diff --git a/sw/device/lib/testing/pwrmgr_testutils.h b/sw/device/lib/testing/pwrmgr_testutils.h
new file mode 100644
index 0000000..df65e4f
--- /dev/null
+++ b/sw/device/lib/testing/pwrmgr_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_PWRMGR_TESTUTILS_H_
+#define OPENTITAN_SW_DEVICE_LIB_TESTING_PWRMGR_TESTUTILS_H_
+
+#include <stdint.h>
+
+#include "sw/device/lib/dif/dif_pwrmgr.h"
+
+/**
+ * Set the device in low power mode.
+ *
+ * A WFI instruction needs to be separately run by the processor to actually
+ * enter low power.
+ *
+ * @param pwrmgr A power manager handle.
+ * @param wakeups The bit mask of wakeup requestors.
+ * @param domain_config The bit mask for configuring the clock and power
+ * domains.
+ */
+void pwrmgr_testutils_enable_low_power(
+ dif_pwrmgr_t *pwrmgr, dif_pwrmgr_request_sources_t wakeups,
+ dif_pwrmgr_domain_config_t domain_config);
+
+/**
+ * Determines if the wakeup reasons is as given.
+ *
+ * @param pwrmgr A power manager handle.
+ * @param reasons A bit mask of reasons.
+ */
+bool pwrmgr_testutils_is_wakeup_reason(dif_pwrmgr_t *pwrmgr,
+ dif_pwrmgr_request_sources_t reasons);
+
+#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_PWRMGR_TESTUTILS_H_
diff --git a/sw/device/lib/testing/rstmgr_testutils.c b/sw/device/lib/testing/rstmgr_testutils.c
new file mode 100644
index 0000000..14e5805
--- /dev/null
+++ b/sw/device/lib/testing/rstmgr_testutils.c
@@ -0,0 +1,19 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#include "sw/device/lib/testing/rstmgr_testutils.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "sw/device/lib/base/mmio.h"
+#include "sw/device/lib/dif/dif_rstmgr.h"
+#include "sw/device/lib/testing/check.h"
+
+bool rstmgr_testutils_is_reset_info(dif_rstmgr_t *rstmgr,
+ dif_rstmgr_reset_info_bitfield_t info) {
+ dif_rstmgr_reset_info_bitfield_t actual_info;
+ CHECK_DIF_OK(dif_rstmgr_reset_info_get(rstmgr, &actual_info));
+ return actual_info == info;
+}
diff --git a/sw/device/lib/testing/rstmgr_testutils.h b/sw/device/lib/testing/rstmgr_testutils.h
new file mode 100644
index 0000000..f685b7b
--- /dev/null
+++ b/sw/device/lib/testing/rstmgr_testutils.h
@@ -0,0 +1,21 @@
+// 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_RSTMGR_TESTUTILS_H_
+#define OPENTITAN_SW_DEVICE_LIB_TESTING_RSTMGR_TESTUTILS_H_
+
+#include <stdint.h>
+
+#include "sw/device/lib/dif/dif_rstmgr.h"
+
+/**
+ * Determines if the reset_info matches info.
+ *
+ * @param rstmgr A reset manager handle.
+ * @param info A bit mask of reset reasons.
+ */
+bool rstmgr_testutils_is_reset_info(dif_rstmgr_t *rstmgr,
+ dif_rstmgr_reset_info_bitfield_t info);
+
+#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_RSTMGR_TESTUTILS_H_