19 #include "sw/device/lib/runtime/irq.h"
21 #include "sw/device/lib/testing/alert_handler_testutils.h"
22 #include "sw/device/lib/testing/aon_timer_testutils.h"
23 #include "sw/device/lib/testing/keymgr_testutils.h"
24 #include "sw/device/lib/testing/pwrmgr_testutils.h"
25 #include "sw/device/lib/testing/rstmgr_testutils.h"
26 #include "sw/device/lib/testing/rv_plic_testutils.h"
27 #include "sw/device/lib/testing/test_framework/FreeRTOSConfig.h"
28 #include "sw/device/lib/testing/test_framework/check.h"
31 #include "alert_handler_regs.h"
33 OTTF_DEFINE_TEST_CONFIG();
37 kTestParamWakeupThresholdUsec = 200000,
41 kTestParamCycleThroughAllPingsUsec = kTestParamWakeupThresholdUsec >> 2,
42 kTestParamAlertHandlerIrqDeadlineUsec = 100,
43 kTestParamAlertHandlerPhase0EscalationDurationUsec = 100,
44 kTestParamAlertHandlerPingTimeoutUsec = 20,
48 kTestParamWakeupThresholdUsec > 175000,
49 "Invalid kTestParamWakeupThresholdUsec. See test plan for more details.");
51 static const dt_pwrmgr_t kPwrmgrDt = 0;
52 static_assert(kDtPwrmgrCount == 1,
"this test expects a pwrmgr");
53 static const dt_aon_timer_t kAonTimerDt = 0;
54 static_assert(kDtAonTimerCount >= 1,
55 "this test expects at least one aon_timer");
56 static const dt_rstmgr_t kRstmgrDt = 0;
57 static_assert(kDtPwrmgrCount == 1,
"this test expects a rstmgr");
58 static const dt_alert_handler_t kAlertHandlerDt = 0;
59 static_assert(kDtAlertHandlerCount == 1,
"this test expects an alert_handler");
60 static const dt_flash_ctrl_t kFlashCtrlDt = 0;
61 static_assert(kDtFlashCtrlCount == 1,
"this test expects a flash_ctrl");
62 static const dt_rv_plic_t kRvPlicDt = 0;
63 static_assert(kDtRvPlicCount == 1,
"this test expects exactly one rv_plic");
66 static dif_rv_plic_t plic;
67 static dif_pwrmgr_t pwrmgr;
68 static dif_rstmgr_t rstmgr;
69 static dif_aon_timer_t aon_timer;
70 static dif_alert_handler_t alert_handler;
71 static const uint32_t kPlicTarget = 0;
73 static volatile bool interrupt_serviced =
false;
78 static void init_peripherals(
void) {
79 CHECK_DIF_OK(dif_rv_plic_init_from_dt(kRvPlicDt, &plic));
80 CHECK_DIF_OK(dif_alert_handler_init_from_dt(kAlertHandlerDt, &alert_handler));
81 CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr));
82 CHECK_DIF_OK(dif_rstmgr_init_from_dt(kRstmgrDt, &rstmgr));
83 CHECK_DIF_OK(dif_aon_timer_init_from_dt(kAonTimerDt, &aon_timer));
88 rv_plic_testutils_irq_range_enable(
90 dt_alert_handler_irq_to_plic_id(kAlertHandlerDt,
91 kDtAlertHandlerIrqClassa),
92 dt_alert_handler_irq_to_plic_id(kAlertHandlerDt,
93 kDtAlertHandlerIrqClassd));
101 static void alert_handler_config(
void) {
109 alert_classes[i] = kDifAlertHandlerClassA;
116 i < ALERT_HANDLER_PARAM_N_LOC_ALERT; ++i) {
118 loc_alert_classes[i] = kDifAlertHandlerClassB;
121 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
122 kTestParamAlertHandlerPhase0EscalationDurationUsec, &cycles));
127 .duration_cycles = cycles,
131 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
132 kTestParamAlertHandlerIrqDeadlineUsec, &cycles));
135 .accumulator_threshold = 0,
136 .irq_deadline_cycles = cycles,
137 .escalation_phases = esc_phases,
138 .escalation_phases_len =
ARRAYSIZE(esc_phases),
146 kDifAlertHandlerClassB};
147 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
148 kTestParamAlertHandlerPingTimeoutUsec, &cycles));
151 .alert_classes = alert_classes,
153 .local_alerts = loc_alerts,
154 .local_alert_classes = loc_alert_classes,
155 .local_alerts_len =
ARRAYSIZE(loc_alerts),
157 .class_configs = class_configs,
159 .ping_timeout = cycles,
162 CHECK_STATUS_OK(alert_handler_testutils_configure_all(&alert_handler, config,
165 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
168 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
175 static void check_local_alerts(
void) {
177 i < ALERT_HANDLER_PARAM_N_LOC_ALERT; ++i) {
181 CHECK(!is_cause,
"Unexpected local alert cause: %d", i);
188 static void chip_sw_reset(
void) {
191 CHECK(
false,
"Should have reset before this line");
201 void ottf_external_isr(uint32_t *exc_info) { interrupt_serviced =
true; }
208 kDtAonTimerWakeupWkupReq, &wakeup_sources));
216 CHECK_STATUS_OK(keymgr_testutils_flash_init(&flash_ctrl, &kCreatorSecret,
222 if (UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0)) ==
true) {
233 CHECK(UNWRAP(rstmgr_testutils_reset_info_any(&rstmgr, reset_info)));
234 CHECK_STATUS_OK(rstmgr_testutils_pre_reset(&rstmgr));
236 alert_handler_config();
238 irq_global_ctrl(
true);
239 irq_external_ctrl(
true);
241 uint64_t wakeup_threshold = 0;
242 CHECK_STATUS_OK(aon_timer_testutils_get_aon_cycles_64_from_us(
243 kTestParamWakeupThresholdUsec, &wakeup_threshold));
247 uint64_t wakeup_threshold_new = wakeup_threshold * 50;
248 CHECK(wakeup_threshold_new > wakeup_threshold,
249 "Detected wakeup_threshold overflow.");
250 wakeup_threshold = wakeup_threshold_new;
256 check_local_alerts();
257 CHECK(interrupt_serviced ==
false,
"Unexpected interrupt triggered.");
261 aon_timer_testutils_wakeup_config(&aon_timer, wakeup_threshold));
263 pwrmgr_testutils_enable_low_power(&pwrmgr, wakeup_sources, 0));
265 CHECK(
false,
"Fail to enter in low power mode!");
267 }
else if (UNWRAP(pwrmgr_testutils_is_wakeup_reason(
268 &pwrmgr, wakeup_sources)) ==
true) {
270 CHECK(UNWRAP(rstmgr_testutils_is_reset_info(
272 CHECK_STATUS_OK(aon_timer_testutils_shutdown(&aon_timer));
278 check_local_alerts();
279 CHECK(interrupt_serviced ==
false,
"Unexpected interrupt triggered.");
284 LOG_ERROR(
"Unexpected wakeup detected: type = %d, request_source = %d",