15 #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/aon_timer_testutils.h"
21 #include "sw/device/lib/testing/keymgr_testutils.h"
22 #include "sw/device/lib/testing/pwrmgr_testutils.h"
23 #include "sw/device/lib/testing/rand_testutils.h"
24 #include "sw/device/lib/testing/ret_sram_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"
32 #include "sw/device/lib/testing/autogen/isr_testutils.h"
34 OTTF_DEFINE_TEST_CONFIG();
36 static const dt_pwrmgr_t kPwrmgrDt = 0;
37 static_assert(kDtPwrmgrCount == 1,
"this test expects a pwrmgr");
38 static const dt_aon_timer_t kAonTimerDt = 0;
39 static_assert(kDtAonTimerCount >= 1,
40 "this test expects at least one aon_timer");
41 static const dt_rstmgr_t kRstmgrDt = 0;
42 static_assert(kDtPwrmgrCount == 1,
"this test expects a rstmgr");
43 static const dt_alert_handler_t kAlertHandlerDt = 0;
44 static_assert(kDtAlertHandlerCount == 1,
"this test expects an alert_handler");
45 static const dt_rv_core_ibex_t kRvCoreIbexDt = 0;
46 static_assert(kDtRvCoreIbexCount == 1,
"this test expects exactly one Ibex");
47 static const dt_flash_ctrl_t kFlashCtrlDt = 0;
48 static_assert(kDtFlashCtrlCount == 1,
"this test expects a flash_ctrl");
49 static const dt_rv_plic_t kRvPlicDt = 0;
50 static_assert(kDtRvPlicCount == 1,
"this test expects exactly one rv_plic");
56 static dif_rv_plic_t plic;
57 static dif_alert_handler_t alert_handler;
58 static dif_aon_timer_t aon_timer;
59 static dif_pwrmgr_t pwrmgr;
60 static dif_rstmgr_t rstmgr;
61 static dif_rv_core_ibex_t ibex;
69 static void init_peripherals(
void) {
70 CHECK_DIF_OK(dif_rv_plic_init_from_dt(kRvPlicDt, &plic));
72 CHECK_DIF_OK(dif_alert_handler_init_from_dt(kAlertHandlerDt, &alert_handler));
75 CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr));
78 kDtAonTimerWakeupWkupReq, &wakeup_sources));
81 CHECK_DIF_OK(dif_aon_timer_init_from_dt(kAonTimerDt, &aon_timer));
83 CHECK_DIF_OK(dif_rstmgr_init_from_dt(kRstmgrDt, &rstmgr));
85 CHECK_DIF_OK(dif_rv_core_ibex_init_from_dt(kRvCoreIbexDt, &ibex));
94 void wait_enough_for_alert_ping(
void) {
110 LOG_FATAL(
"SUPPORTED PLATFORMS: DV and FPGA");
111 LOG_FATAL(
"TO SUPPORT THE PLATFORM %d, COMPUTE THE RIGHT WAIT-TIME",
119 kCounterTestSteps = 0,
122 kCounterTestPhase = 1,
130 static void alert_handler_config(uint32_t ping_timeout) {
138 alert_classes[i] = kDifAlertHandlerClassA;
144 kDifAlertHandlerLocalAlertAlertPingFail,
145 kDifAlertHandlerLocalAlertAlertIntegrityFail,
146 kDifAlertHandlerLocalAlertBusIntegrityFail,
147 kDifAlertHandlerLocalAlertEscalationIntegrityFail,
148 kDifAlertHandlerLocalAlertEscalationPingFail,
149 kDifAlertHandlerLocalAlertShadowedStorageError,
150 kDifAlertHandlerLocalAlertShadowedUpdateError};
152 kDifAlertHandlerClassB, kDifAlertHandlerClassA, kDifAlertHandlerClassA,
153 kDifAlertHandlerClassA, kDifAlertHandlerClassA, kDifAlertHandlerClassA,
154 kDifAlertHandlerClassA};
159 .duration_cycles = 2000}};
163 .accumulator_threshold = 0,
164 .irq_deadline_cycles = 10000,
165 .escalation_phases = esc_phases,
166 .escalation_phases_len =
ARRAYSIZE(esc_phases),
174 kDifAlertHandlerClassB};
177 .alert_classes = alert_classes,
179 .local_alerts = loc_alerts,
180 .local_alert_classes = loc_alert_classes,
181 .local_alerts_len =
ARRAYSIZE(loc_alerts),
183 .class_configs = class_configs,
185 .ping_timeout = ping_timeout,
188 CHECK_STATUS_OK(alert_handler_testutils_configure_all(&alert_handler, config,
191 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
194 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
201 static void alert_handler_clear_cause_regs(
void) {
216 static uint16_t alert_handler_num_fired_alerts(
void) {
219 uint16_t accumulator = 0;
221 for (
size_t alert = 0; alert < ALERT_HANDLER_PARAM_N_ALERTS; alert++) {
224 accumulator += is_cause;
232 static uint16_t alert_handler_num_fired_loc_alerts(
void) {
235 uint16_t accumulator = 0;
242 accumulator += is_cause;
251 static void enter_low_power(
bool deep_sleep) {
255 kDifPwrmgrDomainOptionUsbClockInLowPower |
256 kDifPwrmgrDomainOptionUsbClockInActivePower)) |
257 (!deep_sleep ? kDifPwrmgrDomainOptionMainPowerInLowPower : 0);
260 pwrmgr_testutils_enable_low_power(&pwrmgr, wakeup_sources, cfg));
267 static void check_wakeup_reason(
void) {
270 CHECK(UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, wakeup_sources)) ==
272 "wakeup reason wrong exp:%d obs:%d", wakeup_sources, wakeup_reason);
278 void cleanup_wakeup_src(
void) {
288 static uint32_t rnd_num_iterations;
290 static size_t test_step_cnt;
297 void init_test_components(
void) {
299 irq_global_ctrl(
true);
300 irq_external_ctrl(
true);
306 dt_pwrmgr_irq_to_plic_id(kPwrmgrDt, kDtPwrmgrIrqWakeup);
307 rv_plic_testutils_irq_range_enable(&plic, kPlicTarget, plic_id, plic_id);
318 static void execute_test_phases(uint8_t test_phase, uint32_t ping_timeout_cyc) {
323 uint16_t num_fired_alerts;
324 uint16_t num_fired_loc_alerts;
331 alert_handler_config(ping_timeout_cyc);
335 if (UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0)) ==
true) {
338 CHECK_STATUS_OK(ret_sram_testutils_counter_clear(kCounterTestSteps));
339 CHECK_STATUS_OK(ret_sram_testutils_counter_clear(kCounterTestPhase));
341 rand_testutils_reseed();
342 rnd_num_iterations = rand_testutils_gen32_range(4, 8);
344 ret_sram_testutils_counter_set(kCounterTestPhase, rnd_num_iterations));
346 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
349 CHECK_STATUS_OK(aon_timer_testutils_wakeup_config(
350 &aon_timer, (uint32_t)rand_testutils_gen32_range(2, 4)));
352 enter_low_power(
false);
354 LOG_INFO(
"Test phase = %d, test step = %d", test_phase, test_step_cnt);
356 check_wakeup_reason();
357 cleanup_wakeup_src();
364 "The alert handler should be locked after waking up from normal sleep");
374 num_fired_alerts = alert_handler_num_fired_alerts();
375 num_fired_loc_alerts = alert_handler_num_fired_loc_alerts();
376 if (test_phase == 1) {
378 CHECK(num_fired_alerts == 0,
"Phase #1: Expected_num_fired_alerts is 0");
379 CHECK(num_fired_loc_alerts == 0,
380 "Phase #1: Expected_num_fired_loc_alerts is 0");
391 if (test_phase == 1) {
393 wait_enough_for_alert_ping();
395 CHECK(alert_handler_num_fired_alerts() == 0,
396 "Phase #1: Expected_num_fired_alerts is 0");
397 CHECK(alert_handler_num_fired_loc_alerts() == 0,
398 "Phase #1: Expected_num_fired_loc_alerts is 0");
399 }
else if (test_phase == 2) {
402 alert_handler_clear_cause_regs();
405 wait_enough_for_alert_ping();
407 CHECK(alert_handler_num_fired_alerts() > 0,
408 "Phase #2: No new alerts has been fired after wakeup!");
409 CHECK(alert_handler_num_fired_loc_alerts() == 2,
410 "Phase #2: Only 2 loc_alerts (esc-ping-fail, alert-ping-fail) "
413 &alert_handler, kDifAlertHandlerLocalAlertAlertPingFail, &is_cause));
415 "Phase #2: kDifAlertHandlerLocalAlertAlertPingFail must have been "
418 &alert_handler, kDifAlertHandlerLocalAlertEscalationPingFail,
421 "Phase #2: kDifAlertHandlerLocalAlertEscalationPingFail must have "
428 CHECK_STATUS_OK(ret_sram_testutils_counter_increment(kCounterTestSteps));
430 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
433 CHECK_STATUS_OK(aon_timer_testutils_wakeup_config(
434 &aon_timer, (uint32_t)rand_testutils_gen32_range(20, 30)));
440 sleep_mode = (test_step_cnt == rnd_num_iterations);
441 enter_low_power(sleep_mode);
448 bool ottf_handle_irq(uint32_t *exc_info, dt_instance_id_t devid,
450 if (devid == dt_pwrmgr_instance_id(kPwrmgrDt) &&
451 irq_id == dt_pwrmgr_irq_to_plic_id(kPwrmgrDt, kDtPwrmgrIrqWakeup)) {
452 CHECK_DIF_OK(dif_pwrmgr_irq_acknowledge(&pwrmgr, kDtPwrmgrIrqWakeup));
462 static void chip_sw_reset(
void) {
465 CHECK(
false,
"Should have reset before this line");
469 init_test_components();
471 ret_sram_testutils_init();
474 rstmgr_testutils_reason_clear();
480 CHECK_STATUS_OK(ret_sram_testutils_counter_clear(kCounterTestSteps));
481 CHECK_STATUS_OK(ret_sram_testutils_counter_clear(kCounterTestPhase));
483 rand_testutils_reseed();
484 rnd_num_iterations = rand_testutils_gen32_range(4, 8);
486 ret_sram_testutils_counter_set(kCounterTestPhase, rnd_num_iterations));
487 LOG_INFO(
"Will run %d iterations per phase", rnd_num_iterations);
489 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
495 CHECK_STATUS_OK(keymgr_testutils_flash_init(&flash_ctrl, &kCreatorSecret,
501 ret_sram_testutils_counter_get(kCounterTestPhase, &rnd_num_iterations));
503 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
508 while (test_step_cnt < rnd_num_iterations) {
509 execute_test_phases(1, 256);
516 while (test_step_cnt <= 2 * rnd_num_iterations) {
517 execute_test_phases(2, 1);
521 cleanup_wakeup_src();