Skip to content
Open
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
10 changes: 4 additions & 6 deletions sw/device/lib/crypto/drivers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ cc_library(
],
deps = [
":entropy",
":rv_core_ibex",
"//hw/top:dt_kmac",
"//hw/top:kmac_c_regs",
"//sw/device/lib/base:abs_mmio",
"//sw/device/lib/base:bitfield",
"//sw/device/lib/base:hardened",
"//sw/device/lib/base:macros",
"//sw/device/lib/crypto/impl:status",
"//sw/device/silicon_creator/lib/drivers:ibex",
],
)

Expand Down Expand Up @@ -213,8 +213,8 @@ cc_library(
"//sw/device/lib/base:macros",
"//sw/device/lib/base:memory",
"//sw/device/lib/crypto/drivers:entropy",
"//sw/device/lib/crypto/drivers:rv_core_ibex",
"//sw/device/lib/crypto/impl:status",
"//sw/device/silicon_creator/lib/drivers:ibex",
],
)

Expand All @@ -238,6 +238,7 @@ dual_cc_library(
shared = [
"//sw/device/lib/base:hardened",
"//sw/device/lib/crypto/impl:status",
"//sw/device/silicon_creator/lib/drivers:ibex",
],
),
)
Expand All @@ -250,14 +251,11 @@ opentitan_test(
timeout = "long",
),
deps = [
":rv_core_ibex",
"//hw/top:rv_core_ibex_c_regs",
"//sw/device/lib/base:bitfield",
"//sw/device/lib/base:csr",
"//sw/device/lib/base:status",
"//sw/device/lib/runtime:log",
"//sw/device/lib/testing/test_framework:check",
"//sw/device/lib/testing/test_framework:ottf_main",
"//sw/device/silicon_creator/lib/drivers:ibex",
],
)

Expand Down
2 changes: 1 addition & 1 deletion sw/device/lib/crypto/drivers/hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "sw/device/lib/base/hardened_memory.h"
#include "sw/device/lib/base/memory.h"
#include "sw/device/lib/crypto/drivers/entropy.h"
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
#include "sw/device/lib/crypto/impl/status.h"
#include "sw/device/silicon_creator/lib/drivers/ibex.h"

#include "hw/top/hmac_regs.h" // Generated.

Expand Down
2 changes: 1 addition & 1 deletion sw/device/lib/crypto/drivers/kmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "sw/device/lib/base/bitfield.h"
#include "sw/device/lib/base/memory.h"
#include "sw/device/lib/crypto/drivers/entropy.h"
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
#include "sw/device/lib/crypto/impl/status.h"
#include "sw/device/silicon_creator/lib/drivers/ibex.h"

#include "hw/top/kmac_regs.h" // Generated.

Expand Down
20 changes: 1 addition & 19 deletions sw/device/lib/crypto/drivers/rv_core_ibex.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "sw/device/lib/base/hardened.h"
#include "sw/device/lib/base/macros.h"
#include "sw/device/lib/crypto/impl/status.h"
#include "sw/device/silicon_creator/lib/drivers/ibex.h"

#include "hw/top/rv_core_ibex_regs.h"

Expand Down Expand Up @@ -56,19 +57,6 @@ hardened_bool_t ibex_check_security_config(void) {
return kHardenedBoolTrue;
}

/**
* Blocks until data is ready in the RND register.
*/
static void wait_rnd_valid(void) {
while (true) {
uint32_t reg = abs_mmio_read32(rv_core_ibex_base() +
RV_CORE_IBEX_RND_STATUS_REG_OFFSET);
if (bitfield_bit32_read(reg, RV_CORE_IBEX_RND_STATUS_RND_DATA_VALID_BIT)) {
return;
}
}
}

status_t ibex_disable_icache(hardened_bool_t *icache_enabled) {
// Check if the instruction cache is already disabled.
uint32_t csr;
Expand Down Expand Up @@ -98,12 +86,6 @@ void ibex_restore_icache(hardened_bool_t icache_enabled) {
}
}

uint32_t ibex_rnd32_read(void) {
wait_rnd_valid();
return abs_mmio_read32(rv_core_ibex_base() +
RV_CORE_IBEX_RND_DATA_REG_OFFSET);
}

OT_ALWAYS_INLINE void ibex_clear_rf(void) {
#ifdef OT_PLATFORM_RV32
// ibex_clear_rf() below cannot clear x8 (frame pointer). To avoid that
Expand Down
10 changes: 0 additions & 10 deletions sw/device/lib/crypto/drivers/rv_core_ibex.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ void ibex_restore_icache(hardened_bool_t icache_enabled);
OT_WARN_UNUSED_RESULT
hardened_bool_t ibex_check_security_config(void);

/**
* Get random data from the EDN0 interface.
*
* Important: this function will hang if the entropy complex is not
* initialized. Callers are responsible for checking first.
*
* @return 32 bits of randomness from EDN0.
*/
uint32_t ibex_rnd32_read(void);

/**
* Write a random value into x5...x7 and x9...x31.
*
Expand Down
3 changes: 1 addition & 2 deletions sw/device/lib/crypto/drivers/rv_core_ibex_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"

#include "sw/device/lib/base/status.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_main.h"
#include "sw/device/silicon_creator/lib/drivers/ibex.h"

#include "hw/top/rv_core_ibex_regs.h"

Expand Down
2 changes: 1 addition & 1 deletion sw/device/lib/crypto/impl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ cc_library(
"//sw/device/lib/base:hardened_memory",
"//sw/device/lib/crypto/drivers:entropy",
"//sw/device/lib/crypto/drivers:hmac",
"//sw/device/lib/crypto/drivers:rv_core_ibex",
"//sw/device/silicon_creator/lib/drivers:ibex",
],
)

Expand Down
2 changes: 1 addition & 1 deletion sw/device/lib/crypto/impl/hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#include "sw/device/lib/base/hardened_memory.h"
#include "sw/device/lib/crypto/drivers/entropy.h"
#include "sw/device/lib/crypto/drivers/hmac.h"
#include "sw/device/lib/crypto/drivers/rv_core_ibex.h"
#include "sw/device/lib/crypto/impl/integrity.h"
#include "sw/device/lib/crypto/impl/keyblob.h"
#include "sw/device/lib/crypto/impl/security_config.h"
#include "sw/device/lib/crypto/impl/status.h"
#include "sw/device/silicon_creator/lib/drivers/ibex.h"

// Module ID for status codes.
#define MODULE_ID MAKE_MODULE_ID('h', 'm', 'c')
Expand Down
2 changes: 1 addition & 1 deletion sw/device/silicon_creator/lib/cert/asn1_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ TEST(Asn1, TagLengthEncoding) {
asn1_tag_t tag; \
asn1_start_tag(&state, &tag, kAsn1TagNumberSequence); \
EXPECT_EQ(state.error, kErrorOk); \
asn1_push_bytes(&state, &tmp[0], tmp.size()); \
asn1_push_bytes(&state, tmp.data(), tmp.size()); \
EXPECT_EQ(state.error, kErrorOk); \
asn1_finish_tag(&tag); \
EXPECT_EQ(state.error, kErrorOk); \
Expand Down
51 changes: 36 additions & 15 deletions sw/device/silicon_creator/lib/drivers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,19 @@ opentitan_test(
dual_cc_library(
name = "ibex",
srcs = dual_inputs(
host = ["ibex_host.c"],
shared = ["ibex.c"],
device = ["ibex.c"],
host = ["mock_ibex.cc"],
),
hdrs = dual_inputs(
host = ["mock_ibex.h"],
shared = ["ibex.h"],
),
deps = dual_inputs(
host = [
"//sw/device/lib/base:global_mock",
"//sw/device/silicon_creator/testing:rom_test",
"@googletest//:gtest",
],
shared = [
"//hw/top:rv_core_ibex_c_regs",
"//hw/top:dt",
Expand All @@ -286,7 +292,7 @@ cc_test(
name = "ibex_unittest",
srcs = ["ibex_unittest.cc"],
deps = [
":ibex",
dual_cc_device_library_of(":ibex"),
"//sw/device/silicon_creator/testing:rom_test",
"@googletest//:gtest_main",
],
Expand Down Expand Up @@ -612,7 +618,6 @@ dual_cc_library(
"//sw/device/lib/base:hardened",
"//hw/top:entropy_src_c_regs",
"//hw/top:otp_ctrl_c_regs",
"//hw/top:rv_core_ibex_c_regs",
"//sw/device/lib/base:crc32",
],
host = [
Expand All @@ -621,6 +626,7 @@ dual_cc_library(
"@googletest//:gtest",
],
shared = [
":ibex",
":lifecycle",
"//sw/device/lib/base:macros",
"//sw/device/silicon_creator/lib:error",
Expand Down Expand Up @@ -873,18 +879,33 @@ cc_test(
],
)

cc_library(
dual_cc_library(
name = "pwrmgr",
srcs = ["pwrmgr.c"],
hdrs = ["pwrmgr.h"],
deps = [
":ibex",
"//hw/top:dt",
"//hw/top:pwrmgr_c_regs",
"//sw/device/lib/base:abs_mmio",
"//sw/device/silicon_creator/lib:error",
"//sw/device/silicon_creator/lib/base:sec_mmio",
],
srcs = dual_inputs(
device = ["pwrmgr.c"],
host = ["mock_pwrmgr.cc"],
),
hdrs = dual_inputs(
host = ["mock_pwrmgr.h"],
shared = ["pwrmgr.h"],
),
deps = dual_inputs(
device = [
":ibex",
"//hw/top:pwrmgr_c_regs",
"//sw/device/lib/base:abs_mmio",
"//sw/device/silicon_creator/lib/base:sec_mmio",
],
host = [
"//sw/device/lib/base:global_mock",
"//sw/device/silicon_creator/testing:rom_test",
"@googletest//:gtest",
],
shared = [
"//hw/top:dt",
"//sw/device/silicon_creator/lib:error",
],
),
)

cc_library(
Expand Down
51 changes: 44 additions & 7 deletions sw/device/silicon_creator/lib/drivers/ibex.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@

static const dt_rv_core_ibex_t kRvCoreIbexDt = kDtRvCoreIbex;

void ibex_mcycle_zero(void) {
CSR_WRITE(CSR_REG_MCYCLE, 0);
CSR_WRITE(CSR_REG_MCYCLEH, 0);
}

uint32_t ibex_mcycle32(void) {
uint32_t val;
CSR_READ(CSR_REG_MCYCLE, &val);
return val;
}

uint64_t ibex_mcycle(void) {
uint32_t lo, hi, hi2;
do {
CSR_READ(CSR_REG_MCYCLEH, &hi);
CSR_READ(CSR_REG_MCYCLE, &lo);
CSR_READ(CSR_REG_MCYCLEH, &hi2);
} while (hi != hi2);
return ((uint64_t)hi << 32) | lo;
}

uint64_t ibex_time_to_cycles(uint64_t time_us) {
return to_cpu_cycles(time_us);
}

/**
* Base address of the rv_core_ibex registers.
*
Expand All @@ -22,6 +47,25 @@ static inline uint32_t rv_core_ibex_base(void) {
return dt_rv_core_ibex_reg_block(kRvCoreIbexDt, kDtRvCoreIbexRegBlockCfg);
}

/**
* Blocks until data is ready in the RND register.
*/
static void wait_rnd_valid(void) {
while (true) {
uint32_t reg = abs_mmio_read32(rv_core_ibex_base() +
RV_CORE_IBEX_RND_STATUS_REG_OFFSET);
if (bitfield_bit32_read(reg, RV_CORE_IBEX_RND_STATUS_RND_DATA_VALID_BIT)) {
return;
}
}
}

uint32_t ibex_rnd32_read(void) {
wait_rnd_valid();
return abs_mmio_read32(rv_core_ibex_base() +
RV_CORE_IBEX_RND_DATA_REG_OFFSET);
}

uint32_t ibex_fpga_version(void) {
const uint32_t kBase = rv_core_ibex_base();
return abs_mmio_read32(kBase + RV_CORE_IBEX_FPGA_INFO_REG_OFFSET);
Expand Down Expand Up @@ -162,10 +206,3 @@ void ibex_clear_nmi(ibex_nmi_source_t nmi_src) {
abs_mmio_write32(rv_core_ibex_base() + RV_CORE_IBEX_NMI_STATE_REG_OFFSET,
nmi_src);
}

// `extern` declarations to give the inline functions in the corresponding
// header a link location.
extern void ibex_mcycle_zero(void);
extern uint32_t ibex_mcycle32(void);
extern uint64_t ibex_mcycle(void);
extern uint64_t ibex_time_to_cycles(uint64_t time_us);
42 changes: 14 additions & 28 deletions sw/device/silicon_creator/lib/drivers/ibex.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,37 @@ uint32_t ibex_fpga_version(void);
OT_WARN_UNUSED_RESULT
size_t ibex_addr_remap_slots(void);

#ifdef OT_PLATFORM_RV32
/**
* Set the MCYCLE counter register to zero.
*/
inline void ibex_mcycle_zero(void) {
CSR_WRITE(CSR_REG_MCYCLE, 0);
CSR_WRITE(CSR_REG_MCYCLEH, 0);
}
void ibex_mcycle_zero(void);

/**
* Read the low 32 bits of the MCYCLE counter.
*/
OT_WARN_UNUSED_RESULT
inline uint32_t ibex_mcycle32(void) {
uint32_t val;
CSR_READ(CSR_REG_MCYCLE, &val);
return val;
}
uint32_t ibex_mcycle32(void);

/**
* Read the 64-bit MCYCLE counter.
*/
OT_WARN_UNUSED_RESULT
inline uint64_t ibex_mcycle(void) {
uint32_t lo, hi, hi2;
do {
CSR_READ(CSR_REG_MCYCLEH, &hi);
CSR_READ(CSR_REG_MCYCLE, &lo);
CSR_READ(CSR_REG_MCYCLEH, &hi2);
} while (hi != hi2);
return ((uint64_t)hi << 32) | lo;
}
uint64_t ibex_mcycle(void);

/**
* Convert from microseconds to CPU cycles.
*/
inline uint64_t ibex_time_to_cycles(uint64_t time_us) {
return to_cpu_cycles(time_us);
}
#else
extern void ibex_mcycle_zero(void);
extern uint32_t ibex_mcycle32(void);
extern uint64_t ibex_mcycle(void);
extern uint64_t ibex_time_to_cycles(uint64_t time_us);
#endif
uint64_t ibex_time_to_cycles(uint64_t time_us);

/**
* Get random data from the EDN0 interface.
*
* Important: this function will hang if the entropy complex is not
* initialized. Callers are responsible for checking first.
*
* @return 32 bits of randomness from EDN0.
*/
uint32_t ibex_rnd32_read(void);

/**
* An Ibex exception type for silicon_creator code.
Expand Down
Loading
Loading