|
| 1 | +// Copyright lowRISC contributors (OpenTitan project). |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include "sw/device/lib/dif/dif_otbn.h" |
| 6 | +#include "sw/device/lib/testing/entropy_testutils.h" |
| 7 | +#include "sw/device/lib/testing/otbn_testutils.h" |
| 8 | +#include "sw/device/lib/testing/test_framework/check.h" |
| 9 | +#include "sw/device/lib/testing/test_framework/ottf_main.h" |
| 10 | + |
| 11 | +/** |
| 12 | + * This test exercises OTBN's WFI (Wait For Interrupt) instruction. |
| 13 | + * |
| 14 | + * This test does: |
| 15 | + * - Enable WFI for OTBN. |
| 16 | + * - Write the 1st magic value into `input` before starting. |
| 17 | + * - Start an OTBN program which copies `input` to `output` and then calls WFI. |
| 18 | + * - Poll until OTBN reports STATUS == PAUSED. |
| 19 | + * - Check if the done interrupt is pending and acknowledge it. |
| 20 | + * - Check OTBN does not advance while paused. |
| 21 | + * - Check `output` holds the 1st magic value in DMEM. |
| 22 | + * - Write a 2nd magic value into `input`. |
| 23 | + * - Issue the RESUME command |
| 24 | + * - Wait on the done interrupt until OTBN pauses again |
| 25 | + * - Check that OTBN copied the 2nd magic value to `output`. |
| 26 | + * - Resume execution and wait for the program to finish without errors. |
| 27 | + */ |
| 28 | +OTTF_DEFINE_TEST_CONFIG(); |
| 29 | + |
| 30 | +OTBN_DECLARE_APP_SYMBOLS(wfi_test); |
| 31 | +OTBN_DECLARE_SYMBOL_ADDR(wfi_test, input); |
| 32 | +OTBN_DECLARE_SYMBOL_ADDR(wfi_test, output); |
| 33 | + |
| 34 | +static const otbn_app_t kAppWfiTest = OTBN_APP_T_INIT(wfi_test); |
| 35 | +// DMEM word the host writes for OTBN to copy (before start and while paused). |
| 36 | +static const otbn_addr_t kInput = OTBN_ADDR_T_INIT(wfi_test, input); |
| 37 | +// DMEM word OTBN copies `input` into before each WFI. |
| 38 | +static const otbn_addr_t kOutput = OTBN_ADDR_T_INIT(wfi_test, output); |
| 39 | + |
| 40 | +// The 1st magic value the host writes to DMEM before starting the program. |
| 41 | +static const uint32_t kMagic1Value = 0xf00dcafe; |
| 42 | +// The 2nd magic value the host writes to DMEM while OTBN is paused. |
| 43 | +static const uint32_t kMagic2Value = 0xdeadbeef; |
| 44 | + |
| 45 | +/** |
| 46 | + * Waits until OTBN stops executing the current program. |
| 47 | + * |
| 48 | + * First waits for the EXECUTE command to take effect (OTBN leaves IDLE), then |
| 49 | + * waits until OTBN is no longer busy executing. It stops executing because it |
| 50 | + * either paused on a WFI, finished, or locked up. |
| 51 | + */ |
| 52 | +static status_t otbn_wait_while_executing(const dif_otbn_t *otbn, |
| 53 | + dif_otbn_status_t *status) { |
| 54 | + do { |
| 55 | + TRY(dif_otbn_get_status(otbn, status)); |
| 56 | + } while (*status == kDifOtbnStatusIdle); |
| 57 | + while (*status == kDifOtbnStatusBusyExecute) { |
| 58 | + TRY(dif_otbn_get_status(otbn, status)); |
| 59 | + } |
| 60 | + return OK_STATUS(); |
| 61 | +} |
| 62 | + |
| 63 | +bool test_main(void) { |
| 64 | + dif_otbn_t otbn; |
| 65 | + CHECK_STATUS_OK(entropy_testutils_auto_mode_init()); |
| 66 | + CHECK_DIF_OK(dif_otbn_init_from_dt(kDtOtbn, &otbn)); |
| 67 | + |
| 68 | + // Clear any stale interrupt state so we can observe the WFI-driven pause. |
| 69 | + CHECK_DIF_OK(dif_otbn_irq_acknowledge_all(&otbn)); |
| 70 | + |
| 71 | + // Load the app and enable the WFI instruction. |
| 72 | + CHECK_STATUS_OK(otbn_testutils_load_app(&otbn, kAppWfiTest)); |
| 73 | + CHECK_DIF_OK(dif_otbn_set_ctrl_wfi_enable(&otbn, true)); |
| 74 | + |
| 75 | + // Seed `input` with the 1st magic value before starting. The program copies |
| 76 | + // it to `output` before the WFI, so the host can read it back while paused. |
| 77 | + CHECK_STATUS_OK(otbn_testutils_write_data(&otbn, sizeof(kMagic1Value), |
| 78 | + &kMagic1Value, kInput)); |
| 79 | + |
| 80 | + // Start execution. The program copies `input` to `output` and then pauses |
| 81 | + // itself on the WFI instruction. |
| 82 | + CHECK_STATUS_OK(otbn_testutils_execute(&otbn)); |
| 83 | + |
| 84 | + // Wait for OTBN to stop executing and confirm it entered the PAUSED state. |
| 85 | + dif_otbn_status_t status; |
| 86 | + CHECK_STATUS_OK(otbn_wait_while_executing(&otbn, &status)); |
| 87 | + CHECK(status == kDifOtbnStatusPaused, |
| 88 | + "OTBN did not pause on WFI, status = 0x%x", status); |
| 89 | + |
| 90 | + // The WFI instruction raises the `done` interrupt when it pauses. |
| 91 | + bool irq_pending; |
| 92 | + CHECK_DIF_OK(dif_otbn_irq_is_pending(&otbn, kDifOtbnIrqDone, &irq_pending)); |
| 93 | + CHECK(irq_pending, "OTBN did not raise the done interrupt when pausing."); |
| 94 | + CHECK_DIF_OK(dif_otbn_irq_acknowledge(&otbn, kDifOtbnIrqDone)); |
| 95 | + |
| 96 | + // OTBN stays paused until we resume it, so INSN_CNT must be constant. |
| 97 | + uint32_t insn_cnt_paused; |
| 98 | + CHECK_DIF_OK(dif_otbn_get_insn_cnt(&otbn, &insn_cnt_paused)); |
| 99 | + CHECK(insn_cnt_paused > 0, "Expected OTBN to retire instructions before WFI."); |
| 100 | + uint32_t insn_cnt_still_paused; |
| 101 | + CHECK_DIF_OK(dif_otbn_get_insn_cnt(&otbn, &insn_cnt_still_paused)); |
| 102 | + CHECK(insn_cnt_still_paused == insn_cnt_paused, |
| 103 | + "OTBN executed instructions while paused."); |
| 104 | + |
| 105 | + // Read back `output`: the program copied the 1st magic value from `input` |
| 106 | + // before the WFI. |
| 107 | + uint32_t output; |
| 108 | + CHECK_STATUS_OK( |
| 109 | + otbn_testutils_read_data(&otbn, sizeof(output), kOutput, &output)); |
| 110 | + CHECK(output == kMagic1Value, |
| 111 | + "DMEM output = 0x%08x, expected 0x%08x", output, kMagic1Value); |
| 112 | + |
| 113 | + // Write the 2nd magic value into `input`. After the RESUME the program copies |
| 114 | + // it to `output`, proving execution continued and saw the host's write. |
| 115 | + CHECK_STATUS_OK(otbn_testutils_write_data(&otbn, sizeof(kMagic2Value), |
| 116 | + &kMagic2Value, kInput)); |
| 117 | + |
| 118 | + CHECK_DIF_OK(dif_otbn_write_cmd(&otbn, kDifOtbnCmdResume)); |
| 119 | + |
| 120 | + // Wait on the done interrupt until OTBN pauses again on the 2nd WFI. We |
| 121 | + // acknowledged the interrupt after the 1st pause, so a fresh pending done |
| 122 | + // signals the 2nd pause. |
| 123 | + do { |
| 124 | + CHECK_DIF_OK(dif_otbn_irq_is_pending(&otbn, kDifOtbnIrqDone, &irq_pending)); |
| 125 | + } while (!irq_pending); |
| 126 | + CHECK_DIF_OK(dif_otbn_get_status(&otbn, &status)); |
| 127 | + CHECK(status == kDifOtbnStatusPaused, |
| 128 | + "OTBN did not pause on the 2nd WFI, status = 0x%x", status); |
| 129 | + CHECK_DIF_OK(dif_otbn_irq_acknowledge(&otbn, kDifOtbnIrqDone)); |
| 130 | + |
| 131 | + // The program copied the 2nd magic value from `input` to `output`. |
| 132 | + CHECK_STATUS_OK( |
| 133 | + otbn_testutils_read_data(&otbn, sizeof(output), kOutput, &output)); |
| 134 | + CHECK(output == kMagic2Value, |
| 135 | + "DMEM output = 0x%08x, expected 0x%08x", output, kMagic2Value); |
| 136 | + |
| 137 | + // Resume execution and wait for the program to finish without errors. |
| 138 | + CHECK_DIF_OK(dif_otbn_write_cmd(&otbn, kDifOtbnCmdResume)); |
| 139 | + CHECK_STATUS_OK(otbn_testutils_wait_for_done(&otbn, kDifOtbnErrBitsNoError)); |
| 140 | + |
| 141 | + // The `done` interrupt fires again on completion. |
| 142 | + CHECK_DIF_OK(dif_otbn_irq_is_pending(&otbn, kDifOtbnIrqDone, &irq_pending)); |
| 143 | + CHECK(irq_pending, "OTBN did not raise the done interrupt on completion."); |
| 144 | + CHECK_DIF_OK(dif_otbn_irq_acknowledge(&otbn, kDifOtbnIrqDone)); |
| 145 | + |
| 146 | + LOG_INFO("OTBN WFI test passed: paused and resumed correctly."); |
| 147 | + return true; |
| 148 | +} |
0 commit comments