blob: cd173f2e464e6ea10aaac78edd08bc71dfb8800b [file] [log] [blame]
Timothy Trippele9c06242021-08-18 23:04:02 +00001// Copyright lowRISC contributors.
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_entropy_src.h"
6
7#include "gtest/gtest.h"
8#include "sw/device/lib/base/mmio.h"
9#include "sw/device/lib/base/testing/mock_mmio.h"
Timothy Trippelc5f72c62021-09-28 22:37:11 +000010#include "sw/device/lib/dif/dif_base.h"
Timothy Trippele9c06242021-08-18 23:04:02 +000011
12#include "entropy_src_regs.h" // Generated
13
14namespace dif_entropy_src_unittest {
15namespace {
16
17class DifEntropySrcTest : public testing::Test, public mock_mmio::MmioTest {
18 protected:
Timothy Trippelc5f72c62021-09-28 22:37:11 +000019 const dif_entropy_src_t entropy_src_ = {.base_addr = dev().region()};
Timothy Trippele9c06242021-08-18 23:04:02 +000020};
21
22class InitTest : public DifEntropySrcTest {};
23
24TEST_F(InitTest, BadArgs) {
Timothy Trippelc5f72c62021-09-28 22:37:11 +000025 EXPECT_EQ(dif_entropy_src_init(dev().region(), nullptr), kDifBadArg);
Timothy Trippele9c06242021-08-18 23:04:02 +000026}
27
28TEST_F(InitTest, Init) {
29 dif_entropy_src_t entropy;
Timothy Trippelc5f72c62021-09-28 22:37:11 +000030 EXPECT_EQ(dif_entropy_src_init(dev().region(), &entropy), kDifOk);
Timothy Trippele9c06242021-08-18 23:04:02 +000031}
32
33class ConfigTest : public DifEntropySrcTest {
34 protected:
35 dif_entropy_src_config_t config_ = {
Mark Branstad1f43bf52021-10-05 08:32:54 -070036 .mode = kDifEntropySrcModePtrng,
Timothy Trippele9c06242021-08-18 23:04:02 +000037 .tests = {0},
38 .reset_health_test_registers = false,
39 .single_bit_mode = kDifEntropySrcSingleBitModeDisabled,
40 .route_to_firmware = false,
Timothy Chend2b4c592021-09-15 20:36:37 -070041 .fips_mode = false,
Timothy Trippele9c06242021-08-18 23:04:02 +000042 .test_config = {0},
Miguel Osorio6cc78112021-09-28 16:45:05 -070043 .fw_override =
44 {
45 .enable = false,
46 .entropy_insert_enable = false,
47 .buffer_threshold = kDifEntropyFifoIntDefaultThreshold,
48 },
Timothy Trippele9c06242021-08-18 23:04:02 +000049 };
50};
51
52TEST_F(ConfigTest, NullArgs) {
Timothy Trippelc5f72c62021-09-28 22:37:11 +000053 EXPECT_EQ(dif_entropy_src_configure(nullptr, {}), kDifBadArg);
Timothy Trippele9c06242021-08-18 23:04:02 +000054}
55
Miguel Osorio6cc78112021-09-28 16:45:05 -070056TEST_F(ConfigTest, InvalidFifoThreshold) {
57 config_.fw_override.buffer_threshold = 65;
58 EXPECT_EQ(dif_entropy_src_configure(&entropy_src_, config_), kDifBadArg);
59}
60
61TEST_F(ConfigTest, InvalidFwOverrideSettings) {
62 config_.fw_override.enable = false;
63 config_.fw_override.entropy_insert_enable = true;
64 EXPECT_EQ(dif_entropy_src_configure(&entropy_src_, config_), kDifBadArg);
65}
66
Timothy Trippele9c06242021-08-18 23:04:02 +000067struct ConfigParams {
68 dif_entropy_src_mode_t mode;
69 dif_entropy_src_single_bit_mode_t single_bit_mode;
70 bool route_to_firmware;
71 bool reset_health_test_registers;
72
73 uint32_t expected_mode;
74 bool expected_rng_bit_en;
75 uint32_t expected_rng_sel;
76 uint32_t expected_seed;
77};
78
79class ConfigTestAllParams : public ConfigTest,
80 public testing::WithParamInterface<ConfigParams> {};
81
82TEST_P(ConfigTestAllParams, ValidConfigurationMode) {
83 const ConfigParams &test_param = GetParam();
84 config_.mode = test_param.mode;
85 config_.single_bit_mode = test_param.single_bit_mode;
86 config_.route_to_firmware = test_param.route_to_firmware;
87 config_.reset_health_test_registers = test_param.reset_health_test_registers;
88
Miguel Osorio6cc78112021-09-28 16:45:05 -070089 EXPECT_WRITE32(ENTROPY_SRC_OBSERVE_FIFO_THRESH_REG_OFFSET,
90 config_.fw_override.buffer_threshold);
91 EXPECT_WRITE32(
92 ENTROPY_SRC_FW_OV_CONTROL_REG_OFFSET,
93 {
94 {ENTROPY_SRC_FW_OV_CONTROL_FW_OV_MODE_OFFSET,
95 (uint32_t)(config_.fw_override.enable ? 0xa : 0x5)},
96 {ENTROPY_SRC_FW_OV_CONTROL_FW_OV_ENTROPY_INSERT_OFFSET,
97 (uint32_t)(config_.fw_override.entropy_insert_enable ? 0xa : 0x5)},
98 });
99
Timothy Trippele9c06242021-08-18 23:04:02 +0000100 EXPECT_WRITE32(ENTROPY_SRC_ENTROPY_CONTROL_REG_OFFSET,
101 {
Miguel Osoriocb6904b2021-07-27 13:08:48 -0700102 {ENTROPY_SRC_ENTROPY_CONTROL_ES_ROUTE_OFFSET,
103 (uint32_t)(test_param.route_to_firmware ? 0xa : 0x5)},
104 {ENTROPY_SRC_ENTROPY_CONTROL_ES_TYPE_OFFSET, 0x5},
Timothy Trippele9c06242021-08-18 23:04:02 +0000105 });
Miguel Osoriocb6904b2021-07-27 13:08:48 -0700106
Timothy Chenab23a912021-09-07 15:00:34 -0700107 // Current dif does not perform a read modified write
108 // EXPECT_READ32(ENTROPY_SRC_CONF_REG_OFFSET, 0);
109
Miguel Osoriocb6904b2021-07-27 13:08:48 -0700110 uint32_t rng_bit_enable = test_param.expected_rng_bit_en ? 0xa : 0x5;
Timothy Chend2b4c592021-09-15 20:36:37 -0700111 uint32_t route_to_fw = test_param.route_to_firmware ? 0xa : 0x5;
Timothy Chenab23a912021-09-07 15:00:34 -0700112 uint32_t enable =
113 test_param.expected_mode != kDifEntropySrcModeDisabled ? 0xa : 0x5;
Miguel Osorio6cc78112021-09-28 16:45:05 -0700114
Timothy Chenab23a912021-09-07 15:00:34 -0700115 uint32_t reset_ht = test_param.reset_health_test_registers ? 0xa : 0x5;
Timothy Trippele9c06242021-08-18 23:04:02 +0000116 EXPECT_WRITE32(
117 ENTROPY_SRC_CONF_REG_OFFSET,
118 {
Timothy Trippele9c06242021-08-18 23:04:02 +0000119 {ENTROPY_SRC_CONF_RNG_BIT_SEL_OFFSET, test_param.expected_rng_sel},
Miguel Osoriocb6904b2021-07-27 13:08:48 -0700120 {ENTROPY_SRC_CONF_RNG_BIT_ENABLE_OFFSET, rng_bit_enable},
Timothy Chenab23a912021-09-07 15:00:34 -0700121 {ENTROPY_SRC_CONF_HEALTH_TEST_CLR_OFFSET, reset_ht},
122 {ENTROPY_SRC_CONF_BOOT_BYPASS_DISABLE_OFFSET, 0x5},
Timothy Chend2b4c592021-09-15 20:36:37 -0700123 {ENTROPY_SRC_CONF_ENTROPY_DATA_REG_ENABLE_OFFSET, route_to_fw},
Timothy Chenab23a912021-09-07 15:00:34 -0700124 {ENTROPY_SRC_CONF_ENABLE_OFFSET, enable},
Timothy Trippele9c06242021-08-18 23:04:02 +0000125 });
126
Timothy Trippelc5f72c62021-09-28 22:37:11 +0000127 EXPECT_EQ(dif_entropy_src_configure(&entropy_src_, config_), kDifOk);
Timothy Trippele9c06242021-08-18 23:04:02 +0000128}
129
130INSTANTIATE_TEST_SUITE_P(
131 ConfigTestAllParams, ConfigTestAllParams,
132 testing::Values(
133 // Test entropy mode.
134 ConfigParams{kDifEntropySrcModeDisabled,
Timothy Chenab23a912021-09-07 15:00:34 -0700135 kDifEntropySrcSingleBitModeDisabled, false, false,
136 kDifEntropySrcModeDisabled, false, 0, 0},
Timothy Trippele9c06242021-08-18 23:04:02 +0000137 ConfigParams{kDifEntropySrcModePtrng,
138 kDifEntropySrcSingleBitModeDisabled, false, false, 1,
139 false, 0, 0},
Mark Branstad1f43bf52021-10-05 08:32:54 -0700140 ConfigParams{kDifEntropySrcModePtrng,
Timothy Trippele9c06242021-08-18 23:04:02 +0000141 kDifEntropySrcSingleBitModeDisabled, false, false, 2,
142 false, 0, 4},
143 // Test route_to_firmware
Mark Branstad1f43bf52021-10-05 08:32:54 -0700144 ConfigParams{kDifEntropySrcModePtrng,
Timothy Trippele9c06242021-08-18 23:04:02 +0000145 kDifEntropySrcSingleBitModeDisabled, true, false, 2, false,
146 0, 4},
147 // Test reset_health_test_registers
Mark Branstad1f43bf52021-10-05 08:32:54 -0700148 ConfigParams{kDifEntropySrcModePtrng,
Timothy Trippele9c06242021-08-18 23:04:02 +0000149 kDifEntropySrcSingleBitModeDisabled, true, true, 2, false,
150 0, 4},
151 // Test single_bit_mode
Timothy Chend2b4c592021-09-15 20:36:37 -0700152 ConfigParams{kDifEntropySrcModePtrng, kDifEntropySrcSingleBitMode0,
153 true, true, 2, true, 0, 4},
154 ConfigParams{kDifEntropySrcModePtrng, kDifEntropySrcSingleBitMode1,
155 true, true, 2, true, 1, 4},
156 ConfigParams{kDifEntropySrcModePtrng, kDifEntropySrcSingleBitMode2,
157 true, true, 2, true, 2, 4},
158 ConfigParams{kDifEntropySrcModePtrng, kDifEntropySrcSingleBitMode3,
159 true, true, 2, true, 3, 4}));
Timothy Trippele9c06242021-08-18 23:04:02 +0000160
161class ReadTest : public DifEntropySrcTest {};
162
163TEST_F(ReadTest, EntropyBadArg) {
164 uint32_t word;
Timothy Trippelc5f72c62021-09-28 22:37:11 +0000165 EXPECT_EQ(dif_entropy_src_read(nullptr, &word), kDifBadArg);
Timothy Trippele9c06242021-08-18 23:04:02 +0000166}
167
168TEST_F(ReadTest, WordBadArg) {
Timothy Trippelc5f72c62021-09-28 22:37:11 +0000169 EXPECT_EQ(dif_entropy_src_read(&entropy_src_, nullptr), kDifBadArg);
Timothy Trippele9c06242021-08-18 23:04:02 +0000170}
171
172TEST_F(ReadTest, ReadDataUnAvailable) {
173 EXPECT_READ32(ENTROPY_SRC_INTR_STATE_REG_OFFSET, 0);
174 uint32_t got_word;
Timothy Trippelc5f72c62021-09-28 22:37:11 +0000175 EXPECT_EQ(dif_entropy_src_read(&entropy_src_, &got_word), kDifUnavailable);
Timothy Trippele9c06242021-08-18 23:04:02 +0000176}
177
178TEST_F(ReadTest, ReadOk) {
179 const uint32_t expected_word = 0x65585497;
180 EXPECT_READ32(ENTROPY_SRC_INTR_STATE_REG_OFFSET,
181 1 << ENTROPY_SRC_INTR_STATE_ES_ENTROPY_VALID_BIT);
182 EXPECT_READ32(ENTROPY_SRC_ENTROPY_DATA_REG_OFFSET, expected_word);
183 EXPECT_READ32(ENTROPY_SRC_INTR_STATE_REG_OFFSET,
184 1 << ENTROPY_SRC_INTR_STATE_ES_ENTROPY_VALID_BIT);
185 EXPECT_WRITE32(ENTROPY_SRC_INTR_STATE_REG_OFFSET,
186 {{ENTROPY_SRC_INTR_STATE_ES_ENTROPY_VALID_BIT, true}});
187
188 uint32_t got_word;
Timothy Trippelc5f72c62021-09-28 22:37:11 +0000189 EXPECT_EQ(dif_entropy_src_read(&entropy_src_, &got_word), kDifOk);
Timothy Trippele9c06242021-08-18 23:04:02 +0000190 EXPECT_EQ(got_word, expected_word);
191}
192
193} // namespace
194} // namespace dif_entropy_src_unittest