10 #include "dt/dt_alert_handler.h"
11 #include "dt/dt_aon_timer.h"
12 #include "dt/dt_pwrmgr.h"
13 #include "dt/dt_rv_core_ibex.h"
14 #include "dt/dt_rv_timer.h"
18 #include "sw/device/lib/dif/dif_rv_core_ibex.h"
21 #include "sw/device/lib/runtime/irq.h"
23 #include "sw/device/lib/testing/alert_handler_testutils.h"
24 #include "sw/device/lib/testing/aon_timer_testutils.h"
25 #include "sw/device/lib/testing/pwrmgr_testutils.h"
26 #include "sw/device/lib/testing/test_framework/check.h"
27 #include "sw/device/lib/testing/test_framework/ottf_isrs.h"
30 OTTF_DEFINE_TEST_CONFIG();
32 typedef void (*isr_handler)(void);
34 static volatile isr_handler expected_isr_handler;
37 static volatile bool nmi_fired =
false;
38 static volatile bool ext_irq_fired =
false;
39 static volatile bool irq_is_pending =
false;
41 static dif_pwrmgr_t pwrmgr;
42 static dif_rv_core_ibex_t rv_core_ibex;
43 static dif_aon_timer_t aon_timer;
44 static dif_alert_handler_t alert_handler;
45 static dt_pwrmgr_t kPwrmgrDt = kDtPwrmgrAon;
53 kEscalationPhase0Micros = 100 * 1000,
54 kEscalationPhase2Micros = 100,
55 kIrqDeadlineMicros = 10,
56 kWdogBarkMicros = 500,
62 const int kNumTestIterations = 3;
64 static void alert_handler_config(
void) {
65 uint32_t cycles[3] = {0};
66 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
67 kEscalationPhase0Micros, &cycles[0]));
68 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
69 kEscalationPhase2Micros, &cycles[1]));
70 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(kIrqDeadlineMicros,
75 .duration_cycles = cycles[0]},
78 .duration_cycles = cycles[1]}};
81 .accumulator_threshold = 0,
82 .irq_deadline_cycles = cycles[2],
83 .escalation_phases = esc_phases,
84 .escalation_phases_len =
ARRAYSIZE(esc_phases),
88 dt_alert_id_t alerts[] = {
89 dt_pwrmgr_alert_to_alert_id(kPwrmgrDt, kDtPwrmgrAlertFatalFault)};
94 .alert_classes = alert_classes,
97 .class_configs = class_config,
99 .ping_timeout = kAlertHandlerTestutilsDefaultPingTimeout,
103 alert_handler_testutils_configure_all(&alert_handler, config,
110 static void wdog_handler(
void) {
115 CHECK_DIF_OK(dif_aon_timer_irq_is_pending(
116 &aon_timer, kDifAonTimerIrqWdogTimerBark, &is_pending));
117 irq_is_pending = is_pending;
125 dif_aon_timer_irq_acknowledge(&aon_timer, kDifAonTimerIrqWdogTimerBark));
131 static void alert_handler_handler(
void) {
139 &alert_handler, kDifAlertHandlerClassA, &state));
144 &alert_handler, kDifAlertHandlerClassA, &can_clear));
145 CHECK(can_clear,
"Alert handler is locked.");
150 kDifAlertHandlerClassA));
156 static void wdog_nmi_test(
void) {
158 expected_isr_handler = wdog_handler;
163 uint32_t count_cycles = 0;
164 CHECK_STATUS_OK(aon_timer_testutils_get_aon_cycles_32_from_us(kWdogBarkMicros,
167 aon_timer_testutils_watchdog_config(&aon_timer,
175 CHECK(irq_is_pending,
"Expected watchdog bark interrupt to be pending");
179 "Wdog NMI state not expected:\n\t"
180 "wdog_enable:%x\n\twdog_raised:%"
185 CHECK_DIF_OK(dif_rv_core_ibex_get_nmi_state(
188 "Wdog NMI state not expected:\n\t"
189 "wdog_enable:%x\n\twdog_raised:%"
197 static void alert_handler_nmi_test(
void) {
198 alert_handler_config();
201 expected_isr_handler = alert_handler_handler;
206 CHECK_DIF_OK(dif_pwrmgr_alert_force(&pwrmgr, kDifPwrmgrAlertFatalFault));
213 "Alert handler class A is expected to be in phase 0.");
218 "Alert handler NMI state not expected:\n\t"
219 "alert_enable:%x\n\talert_raised:%x\n",
223 CHECK_DIF_OK(dif_rv_core_ibex_get_nmi_state(
226 "Alert handler NMI state not expected:\n\t"
227 "alert_enable:%x\n\talert_raised:%x\n",
237 void ottf_external_nmi_handler(uint32_t *exc_info) {
240 expected_isr_handler();
242 CHECK_DIF_OK(dif_rv_core_ibex_get_nmi_state(
244 CHECK_DIF_OK(dif_rv_core_ibex_clear_nmi_state(&rv_core_ibex,
245 kDifRvCoreIbexNmiSourceAll));
252 void ottf_external_isr(uint32_t *exc_info) { ext_irq_fired =
true; }
257 void init_peripherals(
void) {
258 static_assert(kDtRvCoreIbexCount >= 1,
"This test requires an Ibex core");
260 dif_rv_core_ibex_init_from_dt((dt_rv_core_ibex_t)0, &rv_core_ibex));
262 CHECK_DIF_OK(dif_aon_timer_init_from_dt(kDtAonTimerAon, &aon_timer));
264 static_assert(kDtAlertHandlerCount >= 1,
"This test needs an Alert Handler");
266 dif_alert_handler_init_from_dt((dt_alert_handler_t)0, &alert_handler));
268 CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr));
279 irq_global_ctrl(
false);
280 irq_external_ctrl(
false);
285 dif_rv_core_ibex_enable_nmi(&rv_core_ibex, kDifRvCoreIbexNmiSourceWdog));
287 dif_rv_core_ibex_enable_nmi(&rv_core_ibex, kDifRvCoreIbexNmiSourceAlert));
291 for (
int k = 0; k < kNumTestIterations; ++k) {
292 LOG_INFO(
"Test iteration %d begin", k);
293 alert_handler_nmi_test();
295 LOG_INFO(
"Test iteration %d end", k);
299 CHECK(ext_irq_fired ==
false,
"Unexpected external interrupt triggered.");
302 return UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0));