OTBN, the OpenTitan Big Number accelerator, is a cryptographic accelerator. For detailed information on OTBN design features, see the [OTBN HWIP technical specification]({{< relref “..” >}}).
The OTBN testbench is based on the [CIP testbench architecture]({{< relref “hw/dv/sv/cip_lib/doc” >}}). It builds on the [dv_utils]({{< relref “hw/dv/sv/dv_utils/README.md” >}}) and [csr_utils]({{< relref “hw/dv/sv/csr_utils/README.md” >}}) packages.
OTBN testing makes use of a DPI-based model called otbn_core_model. This is shown in the block diagram. The dotted interfaces in the otbn block are bound in by the model to access internal signals (register file and memory contents).
The top-level testbench is located at hw/ip/otbn/dv/uvm/tb.sv. This instantiates the OTBN DUT module hw/ip/otbn/rtl/otbn.sv.
OTBN has the following interfaces:
idle_oThe idle and interrupt signals are modelled with the basic [pins_if]({{< relref “hw/dv/sv/common_ifs#pins_if” >}}) interface.
As well as instantiating OTBN, the testbench also instantiates an otbn_core_model. This module wraps an ISS (instruction set simulator) subprocess and performs checks to make sure that OTBN behaves the same as the ISS. The model communicates with the testbench through an otbn_model_if interface, which is monitored by the otbn_model_agent, described below.
The model agent is instantiated by the testbench to monitor the OTBN model. It is a passive agent (essentially just a monitor): the inputs to the model are set in tb.sv. The monitor for the agent generates transactions when it sees a start signal or a done signal.
The start signal is important because we “cheat” and pull it out of the DUT. To make sure that the processor is starting when we expect, we check start transactions against TL writes in the scoreboard.
The main reference model for OTBN is the instruction set simulator (ISS), which is run as a subprocess by DPI code inside otbn_core_model. This Python-based simulator can be found at hw/ip/otbn/dv/otbnsim.
When testing OTBN, we are careful to distinguish between
Testing lots of different instruction streams doesn‘t really use the UVM machinery, so we have a “pre-DV” phase of testing that generates constrained-random instruction streams (as ELF binaries) and runs a simple block-level simulation on each to check that the RTL matches the model. The idea is that this is much quicker for designers to use to smoke-test proposed changes, and can be run with Verilator, so it doesn’t require an EDA tool licence. This pre-DV phase cannot drive sign-off, but it does use much of the same tooling.
Once we are running full DV tests, we re-use this work, by using the same collection of randomised instruction streams and randomly picking from them for most of the sequences. At the moment, the full DV tests create binaries on the fly by running hw/ip/otbn/dv/uvm/gen-binaries.py. This results in one or more ELF files in a directory, which the simulation then picks from at random.
The pre-DV testing doesn't address external stimuli like resets or TileLink-based register accesses. These are driven by specialised test sequences, described below.
The test sequences can be found in hw/ip/otbn/dv/uvm/env/seq_lib. The basic test sequence (otbn_base_vseq) loads the instruction stream from a randomly chosen binary (see above), configures OTBN and then lets it run to completion.
More specialized sequences include things like multiple runs, register accesses during operation (which should fail) and memory corruption. We also check things like the correct operation of the interrupt registers.
We distinguish between architectural and micro-architectural functional coverage. The idea is that the points that go into architectural coverage are those that a DV engineer could derive by reading the block specification. The points that go into micro-architectural coverage are those that require knowledge of the block‘s micro-architecture. Some of these will come from DV engineers; others from the block’s designers. These two views are complementary and will probably duplicate coverage points. For example, an architectural coverage point might be “the processor executed ADDI and the result overflowed”. This might overlap with something like “the overflow signal in the ALU was true when adding”.
The [call stack]({{< relref “.#call-stack” >}}) is exposed as a special register behind x1. It has a bounded depth of 8 elements. We expect to see the following events:
x1All four of these events should be crossed with the three states of the call stack: empty, partially full, and full.
The [loop stack]({{< relref “.#loop-stack” >}}) is accessed by executing LOOP and LOOPI instructions. Important events for it are tracked at those instructions, rather than separately.
Each flag in each flag group should be set to one from zero by some instruction. Similarly, each flag in each flag group should be cleared to zero from one by some instruction.
As a processor, much of OTBN‘s coverage points are described in terms of instructions being executed. Because OTBN doesn’t have a complicated multi-stage pipeline or any real exception handling, we don't track much temporal information (such as sequences of instructions).
As well as instruction-specific coverage points detailed below, we include a requirement that each instruction is executed at least once.
For any instruction with one or more immediate fields, we require “toggle coverage” for those fields. That is, we expect to see execution with each bit of each immediate field being zero and one. We also expect to see each field with values '0 and '1 (all zeros and all ones). If the field is treated as a signed number, we also expect to see it with the extremal values for its range (just the MSB set, for the most negative value; all but the MSB set, for the most positive value).
For any instruction that reads from or writes to a GPR, we expect to see that operand equal to x0, x1 and an arbitrary register in the range x2 .. x31. We don't have any particular coverage requirements for WDRs (since all of them work essentially the same).
For any source GPR, we require “toggle coverage” for its value. For example, ADD reads from its <grs1> operand. We want to see each of the 32 bits of that operand set and unset (giving 64 coverage points).
If an instruction can generate flag changes, we expect to see each flag that the instruction can change being both set and cleared by the instruction. This needn‘t be crossed with the two flag groups (that’s tracked separately in the “Flags” block above). For example, BN.ADD can write to each of the flags C, M, L and Z. This paragraph implies eight coverage points (four flags times two values) for that instruction.
As for ADD.
Nothing beyond immediate toggle coverage.
0x1f which leaves the top bit set.As for SLL.
0x1f which leaves the bottom bit set. (Note that this point also checks that we're performing a logical, rather than arithmetic, right shift)As for SRL.
0x1f which leaves the bottom bit set. (Note that this point also checks that we're performing an arithmetic, rather than logical, right shift)As for SRA.
x0 (to ensure we‘re not just AND’ing things with zero)As for AND.
x0 (to ensure we‘re not just OR’ing things with '1)As for OR.
x0 (to ensure we‘re not just XOR’ing things with zero)As for XOR.
<grs1> is above the top of memory and a negative <offset> brings the load address in range.<grs1> is negative and a positive <offset> brings the load address in range.<grs1> is above the top of memory and a negative <offset> brings the load address in range.<grs1> is negative and a positive <offset> brings the load address in range.All points should be crossed with branch taken / branch not taken.
The “branch to current address” item is problematic if we want to take the branch. Probably we need some tests with short timeouts to handle this properly.
As for BEQ.
Note that the “jump to current address” item won't be a problem to test since it will quickly overflow the call stack.
<offset> aligns (each of the 3 possible misalignments).<offset>.<offset> to give a valid target.Note that the “jump to current address” item won‘t be a problem to test since it will quickly over- or underflow the call stack, provided <grd> and <grs1> aren’t both x1.
bits_to_set to each valid CSR.<grd> other than x0.<grd> equal to x0.No special coverage points for this instruction.
'1 (the maximal value)As for LOOP, but without the count of '1 (not achievable with an immediate).
wrs2 whose top bit is setwrs2 whose top bit is setAs for BN.ADD.
MOD (zero and all ones)MOD) when MOD is nonzero.MOD) when MOD is nonzero.MOD.MOD2^256-1, crossed with whether the subtraction of MOD results in a value that will wrap.wrs1_qwsel with wrs2_qwsel to make sure they are applied to the right inputsAs for BN.MULQACC, plus the generic flag group cover points.
As for BN.MULQACC plus the following:
wrd_hwsel, since the flag changes are different in the two modes.As for BN.ADD.
As for BN.ADDC.
As for BN.SUB.
MOD (zero and all ones)MOD (so MOD is not added).MOD (so MOD is added).MOD (so MOD is added, but the top bit is still set)-MOD.No special coverage.
As for BN.SUB.
As for BN.SUBB.
grs1 is above the top of memory and a negative offset brings the load address in range.grs1 is negative and a positive offset brings the load address in range.grd greater than 31, giving an illegal instruction errorgrd with grd_incgrs1 with grd_incgrs1 is above the top of memory and a negative offset brings the load address in range.grs1 is negative and a positive offset brings the load address in range.grd greater than 31, giving an illegal instruction errorgrs2 with grs2_incgrs1 with grd_incNo special coverage.
grd is greater than 31 with whether the register value at grs is greater than 31Much of the checking for these tests is actually performed in otbn_core_model, which ensures that the RTL and ISS have the same behaviour. However, the scoreboard does have some checks, to ensure that interrupt and idle signals are high at the expected times.
Core TLUL protocol assertions are checked by binding the [TL-UL protocol checker]({{< relref “hw/ip/tlul/doc/TlulProtocolChecker.md” >}}) into the design.
Outputs are also checked for 'X values by assertions in the design RTL. The design RTL contains other assertions defined by the designers, which will be checked in simulation (and won't have been checked by the pre-DV Verilator simulations).
Finally, the otbn_idle_checker checks that the idle_o output correctly matches the running state that you'd expect, based on writes to the CMD register and responses that will appear in the DONE interrupt.
Tests can be run with [dvsim.py]({{< relref “hw/dv/tools/README.md” >}}). The link gives details of the tool's features and command line arguments. To run a basic smoke test, go to the top of the repository and run:
$ util/dvsim/dvsim.py hw/ip/otbn/dv/uvm/otbn_sim_cfg.hjson -i otbn_smoke
{{< incGenFromIpDesc “../../data/otbn_testplan.hjson” “testplan” >}}