Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[util,test] fix bug in flash scrambling script and add flash scrambling smoketest #17575

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hw/top_earlgrey/data/chip_testplan.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -3349,6 +3349,7 @@
"chip_sw_rstmgr_smoketest",
"chip_sw_sram_ctrl_smoketest",
"chip_sw_uart_smoketest",
"chip_sw_flash_scrambling_smoketest",
]
}
{
Expand Down Expand Up @@ -3518,7 +3519,7 @@
in #14095 for more details on how to approach the implementation.
'''
stage: V3
tests: []
tests: ["chip_sw_power_virus"]
}
{
name: chip_sw_power_idle_load
Expand Down
12 changes: 12 additions & 0 deletions hw/top_earlgrey/dv/chip_sim_cfg.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,17 @@
]
run_timeout_mins: 300
}
{
name: chip_sw_flash_scrambling_smoketest
uvm_test_seq: chip_sw_base_vseq
sw_images: [
"//sw/device/tests:flash_scrambling_smoketest:1",
"//sw/device/tests:flash_scrambling_smoketest_otp_img_rma:4",
]
en_run_modes: ["sw_test_mode_test_rom"]
run_opts: ["+use_otp_image=OtpTypeCustom"]
run_timeout_mins: 10
}
]

// List of regressions.
Expand All @@ -1822,6 +1833,7 @@
"chip_sw_spi_device_pass_through",
"chip_sw_i2c_host_tx_rx",
"chip_sw_i2c_device_tx_rx",
"chip_sw_flash_scrambling_smoketest",
"chip_plic_all_irqs",
"chip_sw_example_flash",
"chip_sw_example_rom",
Expand Down
40 changes: 19 additions & 21 deletions sw/device/lib/testing/test_rom/test_rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ bool rom_test_main(void) {
mmio_region_from_addr(TOP_EARLGREY_FLASH_CTRL_CORE_BASE_ADDR)));
CHECK_DIF_OK(dif_flash_ctrl_start_controller_init(&flash_ctrl));
flash_ctrl_testutils_wait_for_init(&flash_ctrl);
#if !OT_IS_ENGLISH_BREAKFAST
timothytrippel marked this conversation as resolved.
Show resolved Hide resolved
// Check the otp to see if flash scramble should be enabled.
otp_val = abs_mmio_read32(
TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR + OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET +
OTP_CTRL_PARAM_CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG_OFFSET);
if (otp_val != 0) {
dif_flash_ctrl_region_properties_t default_properties;
CHECK_DIF_OK(dif_flash_ctrl_get_default_region_properties(
&flash_ctrl, &default_properties));
default_properties.scramble_en =
bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_SCRAMBLING);
default_properties.ecc_en =
bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_ECC);
default_properties.high_endurance_en =
bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_HE);
CHECK_DIF_OK(dif_flash_ctrl_set_default_region_properties(
&flash_ctrl, default_properties));
}
#endif
CHECK_DIF_OK(
dif_flash_ctrl_set_flash_enablement(&flash_ctrl, kDifToggleEnabled));

Expand Down Expand Up @@ -171,27 +190,6 @@ bool rom_test_main(void) {
LOG_INFO("Jitter is enabled");
}

#if !OT_IS_ENGLISH_BREAKFAST
// Check the otp to see if flash scramble should be enabled.
otp_val = abs_mmio_read32(
TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR + OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET +
OTP_CTRL_PARAM_CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG_OFFSET);

if (otp_val != 0) {
dif_flash_ctrl_region_properties_t default_properties;
CHECK_DIF_OK(dif_flash_ctrl_get_default_region_properties(
&flash_ctrl, &default_properties));
default_properties.scramble_en =
bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_SCRAMBLING);
default_properties.ecc_en =
bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_ECC);
default_properties.high_endurance_en =
bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_HE);
CHECK_DIF_OK(dif_flash_ctrl_set_default_region_properties(
&flash_ctrl, default_properties));
}
#endif

if (bootstrap_requested() == kHardenedBoolTrue) {
// This log statement is used to synchronize the rom and DV testbench
// for specific test cases.
Expand Down
40 changes: 36 additions & 4 deletions sw/device/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2100,10 +2100,8 @@ otp_json(
otp_partition(
name = "CREATOR_SW_CFG",
items = {
"CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG": "0000090909",
# TODO(#14814): enable flash scrambling / ECC by replacing above
# with below.
#"CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG": "0000090606",
# Enable flash scrambling and ECC.
"CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG": "0000090606",
},
),
],
Expand Down Expand Up @@ -2231,3 +2229,37 @@ opentitan_functest(
"//sw/device/lib/testing/test_framework:ujson_ottf",
],
)

otp_json(
name = "flash_scrambling_smoketest_otp_overlay",
partitions = [
otp_partition(
name = "CREATOR_SW_CFG",
items = {
# Enable flash scrambling and ECC.
"CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG": "0000090606",
},
),
],
)

otp_image(
name = "flash_scrambling_smoketest_otp_img_rma",
src = "//hw/ip/otp_ctrl/data:otp_json_rma",
overlays = STD_OTP_OVERLAYS + [":flash_scrambling_smoketest_otp_overlay"],
visibility = ["//visibility:private"],
)

opentitan_functest(
name = "flash_scrambling_smoketest",
srcs = ["example_test_from_flash.c"],
dv = dv_params(
otp = ":flash_scrambling_smoketest_otp_img_rma",
),
targets = [
"dv",
],
deps = [
"//sw/device/lib/testing/test_framework:ottf_main",
],
)
4 changes: 2 additions & 2 deletions sw/device/tests/power_virus_systemtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,8 @@ static void check_otp_csr_configs(void) {
dif_flash_ctrl_region_properties_t default_properties;
CHECK_DIF_OK(dif_flash_ctrl_get_default_region_properties(
&flash_ctrl, &default_properties));
CHECK(default_properties.scramble_en == kMultiBitBool4False);
CHECK(default_properties.ecc_en == kMultiBitBool4False);
CHECK(default_properties.scramble_en == kMultiBitBool4True);
CHECK(default_properties.ecc_en == kMultiBitBool4True);
CHECK(default_properties.high_endurance_en == kMultiBitBool4False);
}

Expand Down
7 changes: 4 additions & 3 deletions util/design/gen-flash-img.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"""

import argparse
import functools
import re
import sys
from dataclasses import dataclass
Expand Down Expand Up @@ -107,7 +106,6 @@ class FlashScramblingConfigs:
data_key: int = None


@functools.lru_cache(maxsize=None)
def _xex_scramble(data: int, word_addr: int, flash_addr_key: int,
flash_data_key: int) -> int:
operand_a = ((flash_addr_key & FLASH_GF_OPERAND_A_MASK) >>
Expand Down Expand Up @@ -246,12 +244,14 @@ def _reformat_flash_vmem(
line_items = line.split()
reformatted_line = ""
address = None
address_offset = 0
data = None
for item in line_items:
# Process the address first.
if re.match(r"^@", item):
reformatted_line += item
address = int(item.lstrip("@"), 16)
address_offset = 0
# Process the data words.
else:
data = int(item, 16)
Expand All @@ -262,7 +262,7 @@ def _reformat_flash_vmem(
data_w_intg_ecc &= 0xF_FFFF_FFFF_FFFF_FFFF
if scrambling_configs.scrambling_enabled:
intg_ecc = data_w_intg_ecc & (0xF << FLASH_WORD_SIZE)
data = _xex_scramble(data, address,
data = _xex_scramble(data, address + address_offset,
scrambling_configs.addr_key,
scrambling_configs.data_key)
data_w_intg_ecc = intg_ecc | data
Expand All @@ -274,6 +274,7 @@ def _reformat_flash_vmem(
data_w_intg_ecc)
reformatted_line += str.format(VMEM_FORMAT_STR,
data_w_full_ecc)
address_offset += 1

# Append reformatted line to what will be the new output VMEM file.
reformatted_vmem_lines.append(reformatted_line)
Expand Down