Skip to content

Commit

Permalink
[dv,mem_bkdr_util] Memory back door specializations
Browse files Browse the repository at this point in the history
Modify the mem_bkdr_util class to remove the specialized functionality
for flash, ROM and SRAM and migrate these operations into extended
classes.

This is somewhat cleaner and more elegant, and also helps with
Darjeeling since there is no flash controller in that design.

Update block- and top-level DV environments to use the new classes
and to remove explicit specializations for Rom access from
`chip_sw_base_vseq.`

Signed-off-by: Adrian Lees <[email protected]>
  • Loading branch information
alees24 committed Jan 28, 2025
1 parent 28cc548 commit 6aaf64c
Show file tree
Hide file tree
Showing 27 changed files with 635 additions and 560 deletions.
92 changes: 92 additions & 0 deletions hw/dv/sv/mem_bkdr_util/flash_bkdr_util.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

// Specialization of the `mem_bkdr_util` class for flash memories.

class flash_bkdr_util extends mem_bkdr_util;

localparam int unsigned FlashDataWidth = flash_phy_pkg::DataWidth;
localparam int unsigned FlashStagesPerCycle = FlashDataWidth / flash_phy_pkg::GfMultCycles;
localparam int unsigned FlashKeySize = flash_phy_pkg::KeySize;
localparam int unsigned FlashNumRoundsHalf = crypto_dpi_prince_pkg::NumRoundsHalf;
localparam int unsigned FlashAddrWidth = 16;

// Initialize the class instance.
// `extra_bits_per_subword` is the width any additional metadata that is not captured in secded
// package.
function new(string name = "", string path, int unsigned depth,
longint unsigned n_bits, err_detection_e err_detection_scheme,
int num_prince_rounds_half = 3,
int extra_bits_per_subword = 0, int unsigned system_base_addr = 0);
super.new(name, path, depth, n_bits, err_detection_scheme, num_prince_rounds_half,
extra_bits_per_subword, system_base_addr);
endfunction

function bit [FlashDataWidth-1:0] flash_gf_mult2(bit [FlashDataWidth-1:0] operand);
bit [FlashDataWidth-1:0] mult_out;

mult_out = operand[FlashDataWidth-1] ? (operand << 1) ^
flash_phy_pkg::ScrambleIPoly : (operand << 1);
return mult_out;
endfunction

function bit [FlashStagesPerCycle-1:0][FlashDataWidth-1:0] flash_gen_matrix(
bit [FlashDataWidth-1:0] seed, bit init);
bit [FlashStagesPerCycle-1:0][FlashDataWidth-1:0] matrix_out;

matrix_out[0] = init ? seed : flash_gf_mult2(seed);
matrix_out[FlashStagesPerCycle-1:1] = '0;
for (int i = 1; i < FlashStagesPerCycle; i++) begin
matrix_out[i] = flash_gf_mult2(matrix_out[i-1]);
end
return matrix_out;
endfunction

function bit [FlashDataWidth-1:0] flash_galois_multiply(bit [FlashKeySize-1:0] addr_key,
bit [FlashAddrWidth-1:0] addr);
bit [FlashStagesPerCycle-1:0][FlashDataWidth-1:0] matrix[2];
bit [FlashDataWidth-1:0] product[2];
bit [FlashDataWidth-1:0] add_vector;
bit [FlashDataWidth-1:0] mult_out;

// generate matrix.
matrix[0] =
flash_gen_matrix({addr_key[FlashKeySize-FlashAddrWidth-1:FlashKeySize-64], addr}, 1'b1);
matrix[1] = flash_gen_matrix(matrix[0][FlashStagesPerCycle-1], 1'b0);
// galois multiply.
for (int j = 0; j < 2; j++) begin
mult_out = '0;
for (int i = 0; i < FlashStagesPerCycle; i++) begin
add_vector = addr_key[(j*FlashStagesPerCycle)+i] ? matrix[j][i] : '0;
mult_out = mult_out ^ add_vector;
end
product[j] = mult_out;
end
product[1] = product[1] ^ product[0];
return product[1];
endfunction

virtual function void flash_write_scrambled(
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] byte_addr,
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key);
bit [FlashAddrWidth-1:0] word_addr;
bit [FlashDataWidth-1:0] mask;
bit [FlashDataWidth-1:0] masked_data;
bit [FlashNumRoundsHalf-1:0][FlashDataWidth-1:0] scrambled_data;
bit [71:0] ecc_72;
bit [75:0] ecc_76;

word_addr = byte_addr >> addr_lsb;
mask = flash_galois_multiply(flash_addr_key, word_addr);
masked_data = data ^ mask;

crypto_dpi_prince_pkg::sv_dpi_prince_encrypt(.plaintext(masked_data), .key(flash_data_key),
.old_key_schedule(0), .ciphertext(scrambled_data));
masked_data = scrambled_data[FlashNumRoundsHalf-1] ^ mask;
// ecc functions used are hardcoded to a fixed sized.
ecc_72 = prim_secded_pkg::prim_secded_hamming_72_64_enc(data[63:0]);
ecc_76 = prim_secded_pkg::prim_secded_hamming_76_68_enc({ecc_72[67:64], masked_data[63:0]});
write(byte_addr, ecc_76);
endfunction
endclass
6 changes: 3 additions & 3 deletions hw/dv/sv/mem_bkdr_util/mem_bkdr_util.core
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ filesets:
files:
- sram_scrambler_pkg.sv
- mem_bkdr_util_pkg.sv
- flash_bkdr_util.sv: {is_include_file: true}
- mem_bkdr_util.sv: {is_include_file: true}
- mem_bkdr_util__rom.sv: {is_include_file: true}
- mem_bkdr_util__sram.sv: {is_include_file: true}
- mem_bkdr_util__flash.sv: {is_include_file: true}
- rom_bkdr_util.sv: {is_include_file: true}
- sram_bkdr_util.sv: {is_include_file: true}
file_type: systemVerilogSource

targets:
Expand Down
23 changes: 0 additions & 23 deletions hw/dv/sv/mem_bkdr_util/mem_bkdr_util.sv
Original file line number Diff line number Diff line change
Expand Up @@ -575,20 +575,6 @@ class mem_bkdr_util extends uvm_object;
#0;
endtask

// Backdoor load memory from file to a given address offset
// Allows to place multiple images at different offsets. Possible SRAM tiling is
// considered automatically when using this function
virtual task bkdr_load_from_file(string file,
logic [bus_params_pkg::BUS_AW-1:0] addr_offset,
logic [SRAM_KEY_WIDTH-1:0] key,
logic [SRAM_BLOCK_WIDTH-1:0] nonce);
bit [38:0] preload_data[] = new [num_entries];
$readmemh(file, preload_data);
foreach(preload_data[i]) begin
sram_encrypt_write32_integ(addr_offset + i * 4, preload_data[i], key, nonce, 0);
end
endtask

// save mem contents to file
virtual function void write_mem_to_file(string file);
check_file(file, "w");
Expand Down Expand Up @@ -659,15 +645,6 @@ class mem_bkdr_util extends uvm_object;
addr, rw_data, err_mask, rw_data ^ err_mask), UVM_HIGH)
endfunction

// Wrapper functions for encrypted SRAM reads and writes.
`include "mem_bkdr_util__sram.sv"

// Wrapper function for encrypted ROM reads.
`include "mem_bkdr_util__rom.sv"

// Wrapper function for encrypted FLASH writes.
`include "mem_bkdr_util__flash.sv"

`undef HAS_ECC
`undef HAS_PARITY

Expand Down
79 changes: 0 additions & 79 deletions hw/dv/sv/mem_bkdr_util/mem_bkdr_util__flash.sv

This file was deleted.

Loading

0 comments on commit 6aaf64c

Please sign in to comment.