14 #include "sw/device/lib/dif/dif_rv_core_ibex.h"
17 #include "sw/device/lib/runtime/irq.h"
19 #include "sw/device/lib/testing/alert_handler_testutils.h"
20 #include "sw/device/lib/testing/pwrmgr_testutils.h"
21 #include "sw/device/lib/testing/test_framework/check.h"
22 #include "sw/device/lib/testing/test_framework/ottf_isrs.h"
26 #include "sw/device/lib/testing/autogen/isr_testutils.h"
28 OTTF_DEFINE_TEST_CONFIG();
30 static const dt_pwrmgr_t kPwrmgrDt = 0;
31 static_assert(kDtPwrmgrCount == 1,
"this test expects a pwrmgr");
32 static const dt_rv_timer_t kRvTimerDt = 0;
33 static_assert(kDtRvTimerCount >= 1,
"this test expects at least one rv_timer");
34 static const dt_alert_handler_t kAlertHandlerDt = 0;
35 static_assert(kDtAlertHandlerCount == 1,
"this test expects an alert_handler");
36 static const dt_rv_core_ibex_t kRvCoreIbexDt = 0;
37 static_assert(kDtRvCoreIbexCount == 1,
"this test expects exactly one Ibex");
40 uint32_t alert_raised;
42 uint64_t elapsed_ticks;
48 static volatile bool ext_irq_fired =
false;
50 static dif_pwrmgr_t pwrmgr;
51 static dif_rv_core_ibex_t rv_core_ibex;
52 static dif_alert_handler_t alert_handler;
53 static dif_rv_timer_t timer;
61 kEscalationPhase0Micros = 100 * 1000,
62 kEscalationPhase2Micros = 100,
63 kIrqDeadlineMicros = 10,
64 kTick1us = 1 * 1000 * 1000,
66 kTimeThresholdUs = 5000,
69 static status_t set_tick(uint32_t tick_hz) {
77 static void alerts_configure_all(
const dif_alert_handler_t *alert_handler,
97 ALERT_HANDLER_PING_TIMEOUT_CYC_SHADOWED_PING_TIMEOUT_CYC_SHADOWED_MASK);
121 static void alert_handler_config(
void) {
122 uint32_t cycles[3] = {0};
123 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
124 kEscalationPhase0Micros, &cycles[0]));
125 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
126 kEscalationPhase2Micros, &cycles[1]));
127 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(kIrqDeadlineMicros,
132 .duration_cycles = cycles[0]},
135 .duration_cycles = cycles[1]}};
138 .accumulator_threshold = 0,
139 .irq_deadline_cycles = cycles[2],
140 .escalation_phases = esc_phases,
141 .escalation_phases_len =
ARRAYSIZE(esc_phases),
150 .alert_classes = alert_classes,
153 .class_configs = class_config,
155 .ping_timeout = kAlertHandlerTestutilsDefaultPingTimeout,
160 CHECK_STATUS_OK(set_tick(kTick1us));
165 (uint64_t *)&
counters.elapsed_ticks));
166 CHECK(
counters.elapsed_ticks == 0,
"Failed to write the counter");
178 static void alert_handler_ping_ok_test(
void) {
179 alert_handler_config();
181 uint64_t theshold_us = kTimeThresholdUs;
183 theshold_us = theshold_us * 10;
184 CHECK(theshold_us > kTimeThresholdUs,
"Threshold overflow");
189 &timer, kHart, (uint64_t *)&
counters.elapsed_ticks));
190 }
while (
counters.elapsed_ticks < theshold_us);
200 void ottf_external_nmi_handler(uint32_t *exc_info) {
201 CHECK_DIF_OK(dif_rv_core_ibex_get_nmi_state(
211 CHECK_DIF_OK(dif_rv_core_ibex_clear_nmi_state(&rv_core_ibex,
212 kDifRvCoreIbexNmiSourceAll));
219 void ottf_external_isr(uint32_t *exc_info) { ext_irq_fired =
true; }
221 void init_peripherals(
void) {
222 CHECK_DIF_OK(dif_rv_core_ibex_init_from_dt(kRvCoreIbexDt, &rv_core_ibex));
224 CHECK_DIF_OK(dif_alert_handler_init_from_dt(kAlertHandlerDt, &alert_handler));
226 CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr));
228 CHECK_DIF_OK(dif_rv_timer_init_from_dt(kRvTimerDt, &timer));
235 irq_global_ctrl(
false);
236 irq_external_ctrl(
false);
240 dif_rv_core_ibex_enable_nmi(&rv_core_ibex, kDifRvCoreIbexNmiSourceAll));
242 alert_handler_ping_ok_test();
245 CHECK(ext_irq_fired ==
false,
"Unexpected external interrupt triggered.");
246 CHECK(
counters.alert_raised == 0,
"Unexpected alert raised.");
247 CHECK(
counters.wdog_barked == 0,
"Unexpected watchdog bark.");
250 return UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0));