Skip to content

Commit 88d7131

Browse files
committed
[ot] hw/opentian: ot_otp_engine: Replace entropy BH with a timer
From testing, it seems that the use of Bottom Halves on hosts under higher processing loads (where QEMU is more often pre-empted) can produce inconsistent / slow results. Timers with a short timeout provide the same decoupling functionality as the BH but with more consistency and expedience. Signed-off-by: Alex Jones <alex.jones@lowrisc.org>
1 parent 6ab17e7 commit 88d7131

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

hw/opentitan/ot_otp_engine.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
#define LCI_PROG_SCHED_NS 1000u /* 1us*/
6262

6363
/* Use a timer with a small delay instead of a BH for reliability */
64-
#define DIGEST_DECOUPLE_DELAY_NS 100u /* 100 ns */
64+
#define DIGEST_DECOUPLE_DELAY_NS 100u /* 100 ns */
65+
#define ENTROPY_RESCHEDULE_DELAY_NS 100u /* 100 ns */
6566

6667
/* The size of keys used for OTP scrambling */
6768
#define OTP_SCRAMBLING_KEY_WIDTH 128u
@@ -206,7 +207,7 @@ struct OtOTPScrmblKeyInit_ {
206207
};
207208

208209
struct OtOTPKeyGen_ {
209-
QEMUBH *entropy_bh;
210+
QEMUTimer *request_entropy;
210211
OtPresentState *present;
211212
OtPrngState *prng;
212213
OtFifo32 entropy_buf;
@@ -1648,7 +1649,7 @@ static const OtOTPHWCfg *ot_otp_engine_get_hw_cfg(const OtOTPIf *dev)
16481649
return (const OtOTPHWCfg *)s->hw_cfg;
16491650
}
16501651

1651-
static void ot_otp_engine_request_entropy_bh(void *opaque)
1652+
static void ot_otp_engine_request_entropy(void *opaque)
16521653
{
16531654
OtOTPEngineState *s = opaque;
16541655

@@ -1683,7 +1684,9 @@ ot_otp_engine_keygen_push_entropy(void *opaque, uint32_t bits, bool fips)
16831684
resched);
16841685

16851686
if (resched && !s->keygen->edn_sched) {
1686-
qemu_bh_schedule(s->keygen->entropy_bh);
1687+
int64_t now = qemu_clock_get_ns(OT_OTP_HW_CLOCK);
1688+
timer_mod(s->keygen->request_entropy,
1689+
now + ENTROPY_RESCHEDULE_DELAY_NS);
16871690
}
16881691
}
16891692

@@ -1817,7 +1820,9 @@ static void ot_otp_engine_generate_scrambling_key(
18171820

18181821
if (needed_entropy) {
18191822
/* some entropy bits have been used, refill the buffer */
1820-
qemu_bh_schedule(s->keygen->entropy_bh);
1823+
int64_t now = qemu_clock_get_ns(OT_OTP_HW_CLOCK);
1824+
timer_mod(s->keygen->request_entropy,
1825+
now + ENTROPY_RESCHEDULE_DELAY_NS);
18211826
}
18221827
}
18231828

@@ -2756,7 +2761,7 @@ static void ot_otp_engine_reset_enter(Object *obj, ResetType type)
27562761
timer_del(s->dai->delay);
27572762
timer_del(s->dai->digest_write);
27582763
timer_del(s->lci->prog_delay);
2759-
qemu_bh_cancel(s->keygen->entropy_bh);
2764+
timer_del(s->keygen->request_entropy);
27602765
s->keygen->edn_sched = false;
27612766

27622767
memset(s->hw_cfg, 0, sizeof(*s->hw_cfg));
@@ -2815,7 +2820,8 @@ static void ot_otp_engine_reset_exit(Object *obj, ResetType type)
28152820
ot_edn_connect_endpoint(s->edn, s->edn_ep,
28162821
&ot_otp_engine_keygen_push_entropy, s);
28172822

2818-
qemu_bh_schedule(s->keygen->entropy_bh);
2823+
int64_t now = qemu_clock_get_ns(OT_OTP_HW_CLOCK);
2824+
timer_mod(s->keygen->request_entropy, now + ENTROPY_RESCHEDULE_DELAY_NS);
28192825
}
28202826

28212827
static void ot_otp_engine_realize(DeviceState *dev, Error **errp)
@@ -2908,9 +2914,10 @@ static void ot_otp_engine_init(Object *obj)
29082914
timer_new_ns(OT_VIRTUAL_CLOCK, &ot_otp_engine_dai_write_digest, s);
29092915
s->lci->prog_delay =
29102916
timer_new_ns(OT_OTP_HW_CLOCK, &ot_otp_engine_lci_write_word, s);
2917+
s->keygen->request_entropy =
2918+
timer_new_ns(OT_OTP_HW_CLOCK, &ot_otp_engine_request_entropy, s);
29112919
s->pwr_otp_bh = qemu_bh_new(&ot_otp_engine_pwr_otp_bh, s);
29122920
s->lc_broadcast.bh = qemu_bh_new(&ot_otp_engine_lc_broadcast_bh, s);
2913-
s->keygen->entropy_bh = qemu_bh_new(&ot_otp_engine_request_entropy_bh, s);
29142921

29152922
for (unsigned part_ix = 0; part_ix < s->part_count; part_ix++) {
29162923
if (!s->part_descs[part_ix].buffered) {

0 commit comments

Comments
 (0)