23 #include "sw/device/lib/runtime/irq.h"
25 #include "sw/device/lib/testing/alert_handler_testutils.h"
26 #include "sw/device/lib/testing/rand_testutils.h"
27 #include "sw/device/lib/testing/ret_sram_testutils.h"
28 #include "sw/device/lib/testing/rstmgr_testutils.h"
29 #include "sw/device/lib/testing/rv_plic_testutils.h"
30 #include "sw/device/lib/testing/test_framework/FreeRTOSConfig.h"
31 #include "sw/device/lib/testing/test_framework/check.h"
35 #include "alert_handler_regs.h"
36 #include "hmac_regs.h"
39 #include "kmac_regs.h"
40 #include "otbn_regs.h"
41 #include "spi_host_regs.h"
42 #include "sw/device/lib/testing/autogen/isr_testutils.h"
43 #include "usbdev_regs.h"
45 OTTF_DEFINE_TEST_CONFIG();
47 static dif_rv_plic_t plic;
48 static dif_alert_handler_t alert_handler;
49 static dif_clkmgr_t clkmgr;
50 static dif_spi_host_t spi_host0;
51 static dif_spi_host_t spi_host1;
52 static dif_usbdev_t usbdev;
54 static dif_hmac_t hmac;
55 static dif_kmac_t kmac;
56 static dif_otbn_t otbn;
57 static dif_rstmgr_t rstmgr;
61 static plic_isr_ctx_t plic_ctx = {
63 .hart_id = kPlicTarget,
72 static alert_handler_isr_ctx_t alert_handler_ctx = {
73 .alert_handler = &alert_handler,
75 .expected_irq = kDifAlertHandlerIrqClassb,
82 static void init_peripherals(
void) {
85 CHECK_DIF_OK(dif_rv_plic_init(base_addr, &plic));
88 CHECK_DIF_OK(dif_alert_handler_init(base_addr, &alert_handler));
90 CHECK_DIF_OK(dif_clkmgr_init(
93 CHECK_DIF_OK(dif_spi_host_init(
96 CHECK_DIF_OK(dif_spi_host_init(
99 CHECK_DIF_OK(dif_usbdev_init(
114 CHECK_DIF_OK(dif_rstmgr_init(
122 static const uint32_t kmac_alerts[] = {
127 static const uint32_t spihost0_alerts[] = {
129 static const uint32_t spihost1_alerts[] = {
133 static const uint32_t num_aes_alerts =
ARRAYSIZE(aes_alerts);
134 static const uint32_t num_hmac_alerts =
ARRAYSIZE(hmac_alerts);
135 static const uint32_t num_kmac_alerts =
ARRAYSIZE(kmac_alerts);
136 static const uint32_t num_otbn_alerts =
ARRAYSIZE(otbn_alerts);
137 static const uint32_t num_spihost0_alerts =
ARRAYSIZE(spihost0_alerts);
138 static const uint32_t num_spihost1_alerts =
ARRAYSIZE(spihost1_alerts);
139 static const uint32_t num_usbdev_alerts =
ARRAYSIZE(usbdev_alerts);
141 static const size_t num_alerts =
189 static const test_t kPeripherals[] = {
194 .offset = AES_ALERT_TEST_REG_OFFSET,
196 .fatal_alert_bit = kDifAesAlertFatalFault,
197 .alert_ids = aes_alerts,
198 .num_alert_peri = num_aes_alerts,
205 .offset = HMAC_ALERT_TEST_REG_OFFSET,
207 .fatal_alert_bit = kDifHmacAlertFatalFault,
208 .alert_ids = hmac_alerts,
209 .num_alert_peri = num_hmac_alerts,
216 .offset = KMAC_ALERT_TEST_REG_OFFSET,
219 .alert_ids = kmac_alerts,
220 .num_alert_peri = num_kmac_alerts,
227 .offset = OTBN_ALERT_TEST_REG_OFFSET,
229 .fatal_alert_bit = kDifOtbnAlertFatal,
230 .alert_ids = otbn_alerts,
231 .num_alert_peri = num_otbn_alerts,
239 .offset = SPI_HOST_ALERT_TEST_REG_OFFSET,
241 .fatal_alert_bit = 0,
242 .alert_ids = spihost0_alerts,
243 .num_alert_peri = num_spihost0_alerts,
245 .is_hintable =
false,
250 .offset = SPI_HOST_ALERT_TEST_REG_OFFSET,
252 .fatal_alert_bit = 0,
253 .alert_ids = spihost1_alerts,
254 .num_alert_peri = num_spihost1_alerts,
256 .is_hintable =
false,
261 .offset = USBDEV_ALERT_TEST_REG_OFFSET,
263 .fatal_alert_bit = 0,
264 .alert_ids = usbdev_alerts,
265 .num_alert_peri = num_usbdev_alerts,
267 .is_hintable =
false,
279 static void alert_handler_config_peripherals(uint32_t num_peripherals,
280 const test_t *first_peripheral,
281 uint32_t ping_timeout) {
287 size_t array_idx = 0;
288 for (
int ii = 0; ii < num_peripherals; ii++) {
289 const test_t *peri_ptr = &first_peripheral[ii];
291 alerts[array_idx] = peri_ptr->
alert_ids[jj];
292 alert_classes[array_idx] = kDifAlertHandlerClassA;
299 kDifAlertHandlerLocalAlertAlertPingFail};
305 .duration_cycles = 2000}};
309 .accumulator_threshold = 0,
310 .irq_deadline_cycles = 10000,
311 .escalation_phases = esc_phases,
312 .escalation_phases_len =
ARRAYSIZE(esc_phases),
320 kDifAlertHandlerClassB};
323 .alert_classes = alert_classes,
324 .alerts_len = array_idx,
325 .local_alerts = loc_alerts,
326 .local_alert_classes = loc_alert_classes,
327 .local_alerts_len =
ARRAYSIZE(loc_alerts),
329 .class_configs = class_configs,
331 .ping_timeout = ping_timeout,
334 CHECK_STATUS_OK(alert_handler_testutils_configure_all(&alert_handler, config,
337 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
340 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
347 void set_peripheral_clock(
const test_t *peripheral,
353 &clkmgr, peripheral->
clk_index, new_clk_state));
356 &clkmgr, peripheral->
clk_index, &clk_state));
357 CHECK(clk_state == new_clk_state,
358 "intended_clk_state = %d, received_clk_state = %d", new_clk_state,
363 &clkmgr, peripheral->
clk_index, new_clk_state));
366 &clkmgr, peripheral->
clk_index, &clk_state));
367 CHECK(clk_state == new_clk_state,
368 "intended_clk_state = %d, received_clk_state = %d", new_clk_state,
376 void wait_enough_for_alert_ping(
void) {
392 LOG_FATAL(
"SUPPORTED PLATFORMS: DV and FPGA");
393 LOG_FATAL(
"TO SUPPORT THE PLATFORM %d, COMPUTE THE RIGHT WAIT-TIME",
405 void ottf_external_isr(uint32_t *exc_info) {
407 dif_alert_handler_irq_t irq_serviced;
408 isr_testutils_alert_handler_isr(plic_ctx, alert_handler_ctx,
409 &peripheral_serviced, &irq_serviced);
411 "Interrupt from unexpected peripheral: %d", peripheral_serviced);
416 kCounterTestSteps = 0,
423 ret_sram_testutils_init();
429 size_t test_step_cnt;
433 rst_info = rstmgr_testutils_reason_get();
434 rstmgr_testutils_reason_clear();
438 CHECK_STATUS_OK(ret_sram_testutils_counter_clear(kCounterTestSteps));
440 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
450 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
468 while (test_step_cnt < 1 *
ARRAYSIZE(kPeripherals)) {
476 ret_sram_testutils_counter_get(kCounterTestSteps, &test_step_cnt));
477 test_phase = test_step_cnt /
ARRAYSIZE(kPeripherals);
478 peri_idx = test_step_cnt - (test_phase)*
ARRAYSIZE(kPeripherals);
479 LOG_INFO(
"Testing peripheral %d", peri_idx);
487 alert_handler_config_peripherals(
488 1, &kPeripherals[peri_idx],
492 wait_enough_for_alert_ping();
496 &alert_handler, kDifAlertHandlerLocalAlertAlertPingFail, &is_cause));
498 "Expected response is ping_timeout_fail == 0! But we got "
499 "ping_timeout_fail = 1 for peripheral[%d]",
506 CHECK_STATUS_OK(ret_sram_testutils_counter_increment(kCounterTestSteps));
533 if (test_step_cnt == 1 *
ARRAYSIZE(kPeripherals)) {
534 alert_handler_config_peripherals(
536 &kPeripherals[0], 256);
539 while (test_step_cnt < 2 *
ARRAYSIZE(kPeripherals)) {
542 test_phase = test_step_cnt /
ARRAYSIZE(kPeripherals);
543 peri_idx = test_step_cnt - (test_phase)*
ARRAYSIZE(kPeripherals);
552 wait_enough_for_alert_ping();
556 &alert_handler, kDifAlertHandlerLocalAlertAlertPingFail, &is_cause));
557 CHECK(!is_cause,
"Expected response: No alert_ping_fail! but we got %d",