Software APIs
ast_unittest.cc
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/silicon_creator/lib/drivers/ast.h"
6 
7 #include <array>
8 
9 #include "gtest/gtest.h"
11 #include "sw/device/lib/base/mock_abs_mmio.h"
12 #include "sw/device/lib/base/multibits.h"
13 #include "sw/device/silicon_creator/lib/base/mock_csr.h"
14 #include "sw/device/silicon_creator/lib/drivers/mock_otp.h"
15 #include "sw/device/silicon_creator/testing/rom_test.h"
16 
18 #include "otp_ctrl_regs.h"
19 #include "sensor_ctrl_regs.h"
20 
21 namespace ast_unittest {
22 namespace {
23 using ::testing::Return;
24 using ::testing::ValuesIn;
25 
26 class AstTest : public rom_test::RomTest {
27  protected:
28  /**
29  * Sets up expectations to read the STATUS register of sensor_ctrl twice.
30  *
31  * @param done1 Value of the AST_INIT_DONE bit for the first read.
32  * @param done2 Value of the AST_INIT_DONE bit for the second read.
33  *
34  */
35  void ExpectStatusRead(bool done1, bool done2) {
36  EXPECT_ABS_READ32(base_ + SENSOR_CTRL_STATUS_REG_OFFSET,
37  {{SENSOR_CTRL_STATUS_AST_INIT_DONE_BIT, done1}});
38  EXPECT_ABS_READ32(base_ + SENSOR_CTRL_STATUS_REG_OFFSET,
39  {{SENSOR_CTRL_STATUS_AST_INIT_DONE_BIT, done2}});
40  }
41 
42  /**
43  * Sets up an expectation to read the AST_INIT_EN OTP item.
44  *
45  * @param val Value to return;
46  */
47  void ExpectOtpRead(multi_bit_bool_t val) {
48  EXPECT_CALL(otp_, read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_AST_INIT_EN_OFFSET))
49  .WillOnce(Return(val));
50  }
51 
53  rom_test::MockAbsMmio mmio_;
54  rom_test::MockOtp otp_;
55  mock_csr::MockCsr csr_;
56 };
57 
58 TEST_F(AstTest, InitDoneTrue) {
59  ExpectStatusRead(true, true);
60 
61  EXPECT_EQ(ast_init_done(), kHardenedBoolTrue);
62 }
63 
64 TEST_F(AstTest, InitDoneFalse) {
65  ExpectStatusRead(false, false);
66 
67  EXPECT_EQ(ast_init_done(), kHardenedBoolFalse);
68 
69  ExpectStatusRead(true, false);
70 
71  EXPECT_EQ(ast_init_done(), kHardenedBoolFalse);
72 
73  ExpectStatusRead(false, true);
74 
75  EXPECT_EQ(ast_init_done(), kHardenedBoolFalse);
76 }
77 
78 class AstLcStateTest : public AstTest,
79  public testing::WithParamInterface<lifecycle_state_t> {};
80 
81 constexpr std::array<lifecycle_state_t, 2> kLcStatesWithoutCheck{
82  kLcStateTest,
83  kLcStateRma,
84 };
85 
87 
88 TEST_P(AstLcStatesWithoutCheckTest, CheckLcSkip) {
89  EXPECT_EQ(ast_check(GetParam()), kErrorOk);
90 }
91 
92 INSTANTIATE_TEST_SUITE_P(LcStatesWithoutCheck, AstLcStatesWithoutCheckTest,
93  ValuesIn(kLcStatesWithoutCheck));
94 
95 constexpr std::array<lifecycle_state_t, 3> kLcStatesWithCheck{
96  kLcStateDev,
97  kLcStateProd,
98  kLcStateProdEnd,
99 };
100 
102 
103 TEST_P(AstLcStatesWithCheckTest, CheckOtpSkip) {
104  ExpectOtpRead(kMultiBitBool4False);
105 
106  EXPECT_EQ(ast_check(GetParam()), kErrorOk);
107 }
108 
109 TEST_P(AstLcStatesWithCheckTest, CheckTimeout) {
110  ExpectOtpRead(kMultiBitBool4True);
111  EXPECT_CSR_WRITE(CSR_REG_MCYCLE, 0);
112  EXPECT_CSR_READ(CSR_REG_MCYCLE, 100);
113  ExpectStatusRead(false, false);
114  EXPECT_CSR_READ(CSR_REG_MCYCLE, kAstCheckPollCpuCycles);
115  ExpectStatusRead(false, false);
116 
117  EXPECT_EQ(ast_check(GetParam()), kErrorAstInitNotDone);
118 }
119 
120 TEST_P(AstLcStatesWithCheckTest, CheckSuccess) {
121  ExpectOtpRead(kMultiBitBool4True);
122  EXPECT_CSR_WRITE(CSR_REG_MCYCLE, 0);
123  EXPECT_CSR_READ(CSR_REG_MCYCLE, 100);
124  ExpectStatusRead(false, false);
125  EXPECT_CSR_READ(CSR_REG_MCYCLE, kAstCheckPollCpuCycles);
126  ExpectStatusRead(true, true);
127 
128  EXPECT_EQ(ast_check(GetParam()), kErrorOk);
129 }
130 
131 INSTANTIATE_TEST_SUITE_P(LcStatesWithCheck, AstLcStatesWithCheckTest,
132  ValuesIn(kLcStatesWithCheck));
133 
134 } // namespace
135 } // namespace ast_unittest