Skip to content

Conversation

engdoreis
Copy link

@engdoreis engdoreis commented Oct 22, 2025

ThIs PR does not cover all the TPM implementation, the HW registers will be covered by a follow up PR.

To test use the Opentitan PR lowRISC/opentitan#28542 and run:

bazel test --test_output=streamed --build_tests_only \
    $(bazel query //sw/device/tests:all | grep qemu | grep spi_device) \
    --test_tag_filters="qemu" --override_repository="+qemu+qemu_opentitan=$qemu_repo"

@rivos-eblot
Copy link

I'll review it soon; I've noticed there are a couple of type mismatches (e.g. bool x; ... x = 1;) and missing unsigned constant suffixes (u)

@rivos-eblot
Copy link

see also the PR title, as they now follow the

`component`: ...

formatting, i.e. ot_spi_device: Implement the TPM mode

Copy link

@AlexJones0 AlexJones0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got part way through reviewing but then @engdoreis told me this is still being actively worked on as a draft. I'll leave the comments I had for now though to aid development, but I haven't looked at any of the TPM logic itself yet.

@engdoreis engdoreis changed the title [DRAFT][OT spi_device] Implement the TPM mode [ot] hw/opentitan: ot_spi_device: Implement the TPM mode Oct 22, 2025
@engdoreis engdoreis marked this pull request as ready for review October 22, 2025 16:28
@engdoreis engdoreis force-pushed the ot_spi_tpm branch 3 times, most recently from c004a06 to 8675744 Compare October 23, 2025 11:05
Based on the table in the spi_device/doc/programmers_guide

Signed-off-by: Douglas Reis <[email protected]>
Copy link

@rivos-eblot rivos-eblot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I would recommend to add new trace_ot_spi_device_...
Traces are very useful for debugging the emulation itself, but they are also very useful when developping guest SW (RISC-V code) and remote host peer SW. Thanks.

#define SPI_SRAM_MBX_SIZE 0x400u
#define SPI_SRAM_SFDP_OFFSET (SPI_SRAM_MBX_OFFSET + SPI_SRAM_MBX_SIZE)
#define SPI_SRAM_SFDP_SIZE 0x100u

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to remove extra empty lines.

#define SPI_SRAM_PAYLOAD_OFFSET (SPI_SRAM_SFDP_OFFSET + SPI_SRAM_SFDP_SIZE)
#define SPI_SRAM_INGRESS_OFFSET 0xE00u

static_assert(SPI_SRAM_INGRESS_OFFSET >=

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to move the assertions after the constant definitions

} OtSpiFlashState;

typedef enum {
SPI_TPM_IDLE, // Wait for the fist byte of the header.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QEMU comments only use C style (/* */)

OtSpiTpmState state;
uint32_t can_receive;
uint32_t transfer_size;
bool read;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to move it near the other bool

*/
static bool ot_spi_device_is_tpm_mode_crb(const OtSPIDeviceState *s)
{
return (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very weird formatting :-)
there is an explci cast to an enumerated balue but you're returning a bool?
Looks like the cast is copy/pasted.

SpiDeviceTpm *tpm = &s->tpm;

if (size != tpm->can_receive) {
error_setg(&error_fatal, "TPM expected %u bytes, got %u\n",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suspicious fatal error (just checking): Is this an internal error or something that can be triggered from the peer?

if (size != tpm->can_receive) {
error_setg(&error_fatal, "TPM expected %u bytes, got %u\n",
tpm->can_receive, size);
tpm->state = SPI_TPM_END;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't be reached :-)

return;
}

tpm->reg = buf[1] << 8 | buf[2];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing us

tpm->locality = tpm->reg >> 12u;
s->tpm_regs[R_TPM_CMD_ADDR] =
(tpm->opcode << R_TPM_CMD_ADDR_CMD_SHIFT) | tpm->reg;
if (tpm->read) { // When read, we immidiatelly release the software to fill

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++ style comment + typo

if (tpm->read) {
tpm->state = tpm->should_sw_handle ? SPI_TPM_READ : SPI_TPM_READ_HW_REG;
}
tx_buf[size - 1] = 0x01u;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants