From 4768a7cb6656f56fbed85ef43fe136fa71d738d1 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:24 +0000 Subject: [PATCH 01/52] [ot] build: bazel: add BUILD.bazel and REPO.bazel for @qemu_opentitan Add top-level `BUILD.bazel` and `REPO.bazel` files so the QEMU repository can be mounted directly as the `@qemu_opentitan` Bazel external repository, exporting built artifacts (`build/qemu-system-riscv32*`, `build/qemu-img`) and the `:qemu_src` / `:ot` filegroups consumed by OpenTitan's `//third_party/qemu` package. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ib5d79f9b8776ec355bfab74d3d90d1796a6a6964 --- BUILD.bazel | 20 ++++++++++++++++++++ REPO.bazel | 4 ++++ hw/opentitan/Kconfig | 4 ++++ 3 files changed, 28 insertions(+) create mode 100644 BUILD.bazel create mode 100644 REPO.bazel diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000000000..0f52ab4d1eb01 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,20 @@ +# Copyright lowRISC contributors. +# +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +package(default_visibility = ["//visibility:public"]) + +exports_files(glob(["**"])) + +filegroup( + name = "qemu_src", + srcs = glob(["**"]), +) + +filegroup( + name = "ot", + srcs = glob([ + "python/qemu/ot/**", + ]), +) diff --git a/REPO.bazel b/REPO.bazel new file mode 100644 index 0000000000000..7f31b776e20ca --- /dev/null +++ b/REPO.bazel @@ -0,0 +1,4 @@ +# Copyright lowRISC contributors. +# +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 diff --git a/hw/opentitan/Kconfig b/hw/opentitan/Kconfig index 3a98d8e0cb931..ca6f106988fd4 100644 --- a/hw/opentitan/Kconfig +++ b/hw/opentitan/Kconfig @@ -1,5 +1,6 @@ # OpenTitan devices + config OT_ADDRESS_SPACE bool @@ -158,6 +159,7 @@ config OT_OTP_OT_BE select OT_OTP_IF bool + config OT_PINMUX_DJ bool @@ -176,6 +178,7 @@ config OT_PRINCE config OT_PRNG bool + config OT_PWRMGR select OT_CLOCK_CTRL bool @@ -210,6 +213,7 @@ config OT_SRAM_CTRL select OT_OTP_IF bool + config OT_TIMER bool From 606c2eae91490023bbe4435e1150103e622cc243 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:26 +0000 Subject: [PATCH 02/52] [ot] target/riscv: rvb: add Xdraft093b bitmanip instructions Earlgrey's Ibex core (RV32B = ibex_pkg::RV32BFull) implements RISC-V Bitmanip draft v0.93 instructions (Zbf, Zbp, Zbr, Zbs, Zbt) alongside ratified Zba/Zbb/Zbc/Zbs instructions. Add the Xdraft093b decodetree specification (packu, bfp, xperm.h, grev, grevi, gorc, gorci, shfl, shfli, unshfl, unshfli, slo, sloi, sro, sroi, fsl, fsr, fsri, cmix, cmov), TCG translation routines in trans_rvb.c.inc, runtime execution helpers in bitmanip_helper.c, and enable zbkb, zbkx, and xdraft093b on the lowrisc-ibex CPU definition. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I09361f2def12ad7690c2400a9c975cdd6a6a6964 --- target/riscv/Xdraft093b.decode | 48 +++++++ target/riscv/bitmanip_helper.c | 87 ++++++++++++ target/riscv/cpu.c | 5 + target/riscv/cpu_cfg.h | 1 + target/riscv/cpu_cfg_fields.h.inc | 1 + target/riscv/helper.h | 8 ++ target/riscv/insn_trans/trans_rvb.c.inc | 170 ++++++++++++++++++++++++ target/riscv/meson.build | 1 + target/riscv/translate.c | 2 + 9 files changed, 323 insertions(+) create mode 100644 target/riscv/Xdraft093b.decode diff --git a/target/riscv/Xdraft093b.decode b/target/riscv/Xdraft093b.decode new file mode 100644 index 0000000000000..095525bc71bf5 --- /dev/null +++ b/target/riscv/Xdraft093b.decode @@ -0,0 +1,48 @@ +# +# RISC-V translation routines for the RV32 Bitmanip Draft 0.93 (Xdraft093b) extension +# +# Copyright lowRISC contributors. +# +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +# Fields: +%rs3 27:5 +%rs2 20:5 +%rs1 15:5 +%rd 7:5 +%sh5 20:5 + +# Argument sets: +&r rd rs1 rs2 !extern +&shift shamt rs1 rd !extern +&r4 rd rs1 rs2 rs3 +&r4_sh rd rs1 shamt rs3 + +# Formats: +@r ....... ..... ..... ... ..... ....... &r %rs2 %rs1 %rd +@sh5 ....... ..... ..... ... ..... ....... &shift shamt=%sh5 %rs1 %rd +@r4 ..... .. ..... ..... ... ..... ....... &r4 %rs3 %rs2 %rs1 %rd +@r4_sh ..... .. ..... ..... ... ..... ....... &r4_sh shamt=%sh5 %rs3 %rs1 %rd + +# *** RV32 Xdraft093b Extension (Bitmanip draft 0.93) *** +grevi 0110100 ..... ..... 101 ..... 0010011 @sh5 +gorci 0010100 ..... ..... 101 ..... 0010011 @sh5 +unshfli 0000100 ..... ..... 101 ..... 0010011 @sh5 +shfli 0000100 ..... ..... 001 ..... 0010011 @sh5 +sroi 0010000 ..... ..... 101 ..... 0010011 @sh5 +sloi 0010000 ..... ..... 001 ..... 0010011 @sh5 +fsri ..... 10 ..... ..... 101 ..... 0010011 @r4_sh +packu 0100100 ..... ..... 100 ..... 0110011 @r +bfp 0100100 ..... ..... 111 ..... 0110011 @r +grev 0110100 ..... ..... 101 ..... 0110011 @r +gorc 0010100 ..... ..... 101 ..... 0110011 @r +shfl 0000100 ..... ..... 001 ..... 0110011 @r +unshfl 0000100 ..... ..... 101 ..... 0110011 @r +slo 0010000 ..... ..... 001 ..... 0110011 @r +sro 0010000 ..... ..... 101 ..... 0110011 @r +fsl ..... 10 ..... ..... 001 ..... 0110011 @r4 +fsr ..... 10 ..... ..... 101 ..... 0110011 @r4 +cmix ..... 11 ..... ..... 001 ..... 0110011 @r4 +cmov ..... 11 ..... ..... 101 ..... 0110011 @r4 +xperm_h 0010100 ..... ..... 110 ..... 0110011 @r diff --git a/target/riscv/bitmanip_helper.c b/target/riscv/bitmanip_helper.c index 52ac2dad21aae..9fc76d4564f8b 100644 --- a/target/riscv/bitmanip_helper.c +++ b/target/riscv/bitmanip_helper.c @@ -130,6 +130,93 @@ target_ulong HELPER(xperm8)(target_ulong rs1, target_ulong rs2) return do_xperm(rs1, rs2, 3); } +target_ulong HELPER(xperm16)(target_ulong rs1, target_ulong rs2) +{ + return do_xperm(rs1, rs2, 4); +} + +static const uint64_t grev_masks[] = { + 0x5555555555555555ULL, + 0x3333333333333333ULL, + 0x0f0f0f0f0f0f0f0fULL, + 0x00ff00ff00ff00ffULL, + 0x0000ffff0000ffffULL, +}; + +target_ulong HELPER(grev)(target_ulong rs1, target_ulong rs2) +{ + target_ulong x = rs1; + int shamt = rs2 & (TARGET_LONG_BITS - 1); + + for (int i = 0; i < ARRAY_SIZE(grev_masks); i++) { + if (shamt & (1 << i)) { + x = do_swap(x, grev_masks[i], 1 << i); + } + } + return x; +} + +target_ulong HELPER(gorc)(target_ulong rs1, target_ulong rs2) +{ + target_ulong x = rs1; + int shamt = rs2 & (TARGET_LONG_BITS - 1); + + for (int i = 0; i < ARRAY_SIZE(grev_masks); i++) { + if (shamt & (1 << i)) { + x |= do_swap(x, grev_masks[i], 1 << i); + } + } + return x; +} + +target_ulong HELPER(shfl)(target_ulong rs1, target_ulong rs2) +{ + target_ulong x = rs1; + int shamt = rs2 & ((TARGET_LONG_BITS >> 1) - 1); + + for (int i = 3; i >= 0; i--) { + if (shamt & (1 << i)) { + x = do_shuf_stage(x, shuf_masks[i], shuf_masks[i] >> (1 << i), + 1 << i); + } + } + return x; +} + +target_ulong HELPER(unshfl)(target_ulong rs1, target_ulong rs2) +{ + target_ulong x = rs1; + int shamt = rs2 & ((TARGET_LONG_BITS >> 1) - 1); + + for (int i = 0; i < 4; i++) { + if (shamt & (1 << i)) { + x = do_shuf_stage(x, shuf_masks[i], shuf_masks[i] >> (1 << i), + 1 << i); + } + } + return x; +} + +target_ulong HELPER(bfp)(target_ulong rs1, target_ulong rs2) +{ + uint32_t len = ((rs2 >> 24) & 0xf) ?: 16; + uint32_t off = (rs2 >> 16) & 0x1f; + uint32_t mask = (1U << len) - 1; + return (rs1 & ~(mask << off)) | ((rs2 & mask) << off); +} + +target_ulong HELPER(fsl)(target_ulong rs1, target_ulong rs2, target_ulong rs3) +{ + uint64_t val = ((uint64_t)(uint32_t)rs1 << 32) | (uint32_t)rs3; + return (uint32_t)(rol64(val, rs2 & 0x3f) >> 32); +} + +target_ulong HELPER(fsr)(target_ulong rs1, target_ulong rs2, target_ulong rs3) +{ + uint64_t val = ((uint64_t)(uint32_t)rs3 << 32) | (uint32_t)rs1; + return (uint32_t)ror64(val, rs2 & 0x3f); +} + /* * this table is already defined in hw/pc/pcnet.c, as well as likely implemented * in zlib. It would make sense to move pcnet implementation into util/ as the diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 04a2a4c286aae..bf9c306afb7d2 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -245,6 +245,7 @@ const RISCVIsaExtData isa_edata_arr[] = { ISA_EXT_DATA_ENTRY(xtheadmempair, PRIV_VERSION_1_11_0, ext_xtheadmempair), ISA_EXT_DATA_ENTRY(xtheadsync, PRIV_VERSION_1_11_0, ext_xtheadsync), ISA_EXT_DATA_ENTRY(xventanacondops, PRIV_VERSION_1_12_0, ext_XVentanaCondOps), + ISA_EXT_DATA_ENTRY(xdraft093b, PRIV_VERSION_1_11_0, ext_xdraft093b), { }, }; @@ -1456,6 +1457,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[] = { MULTI_EXT_CFG_BOOL("xtheadmempair", ext_xtheadmempair, false), MULTI_EXT_CFG_BOOL("xtheadsync", ext_xtheadsync, false), MULTI_EXT_CFG_BOOL("xventanacondops", ext_XVentanaCondOps, false), + MULTI_EXT_CFG_BOOL("xdraft093b", ext_xdraft093b, false), { }, }; @@ -3168,8 +3170,11 @@ static const TypeInfo riscv_cpu_type_infos[] = { .cfg.ext_zba = true, .cfg.ext_zbb = true, .cfg.ext_zbc = true, + .cfg.ext_zbkb = true, + .cfg.ext_zbkx = true, .cfg.ext_zbr = true, .cfg.ext_zbs = true, + .cfg.ext_xdraft093b = true, ), DEFINE_RISCV_CPU(TYPE_RISCV_CPU_SIFIVE_E31, TYPE_RISCV_CPU_SIFIVE_E, diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h index 29e02979179a9..6c9a0dc67ab33 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -67,5 +67,6 @@ MATERIALISE_EXT_PREDICATE(XVentanaCondOps) /* Extensions that are not yet upstream */ MATERIALISE_EXT_PREDICATE(zbr); +MATERIALISE_EXT_PREDICATE(xdraft093b) #endif diff --git a/target/riscv/cpu_cfg_fields.h.inc b/target/riscv/cpu_cfg_fields.h.inc index 4e240376f1dc7..30afd986194c9 100644 --- a/target/riscv/cpu_cfg_fields.h.inc +++ b/target/riscv/cpu_cfg_fields.h.inc @@ -148,6 +148,7 @@ BOOL_FIELD(ext_xtheadmemidx) BOOL_FIELD(ext_xtheadmempair) BOOL_FIELD(ext_xtheadsync) BOOL_FIELD(ext_XVentanaCondOps) +BOOL_FIELD(ext_xdraft093b) BOOL_FIELD(mmu) BOOL_FIELD(pmp) diff --git a/target/riscv/helper.h b/target/riscv/helper.h index 86bc3f42fa4cc..49601ec6aa46b 100644 --- a/target/riscv/helper.h +++ b/target/riscv/helper.h @@ -84,6 +84,14 @@ DEF_HELPER_FLAGS_1(unzip, TCG_CALL_NO_RWG_SE, tl, tl) DEF_HELPER_FLAGS_1(zip, TCG_CALL_NO_RWG_SE, tl, tl) DEF_HELPER_FLAGS_2(xperm4, TCG_CALL_NO_RWG_SE, tl, tl, tl) DEF_HELPER_FLAGS_2(xperm8, TCG_CALL_NO_RWG_SE, tl, tl, tl) +DEF_HELPER_FLAGS_2(xperm16, TCG_CALL_NO_RWG_SE, tl, tl, tl) +DEF_HELPER_FLAGS_2(grev, TCG_CALL_NO_RWG_SE, tl, tl, tl) +DEF_HELPER_FLAGS_2(gorc, TCG_CALL_NO_RWG_SE, tl, tl, tl) +DEF_HELPER_FLAGS_2(shfl, TCG_CALL_NO_RWG_SE, tl, tl, tl) +DEF_HELPER_FLAGS_2(unshfl, TCG_CALL_NO_RWG_SE, tl, tl, tl) +DEF_HELPER_FLAGS_2(bfp, TCG_CALL_NO_RWG_SE, tl, tl, tl) +DEF_HELPER_FLAGS_3(fsl, TCG_CALL_NO_RWG_SE, tl, tl, tl, tl) +DEF_HELPER_FLAGS_3(fsr, TCG_CALL_NO_RWG_SE, tl, tl, tl, tl) DEF_HELPER_FLAGS_2(crc32, TCG_CALL_NO_RWG_SE, tl, tl, tl) DEF_HELPER_FLAGS_2(crc32c, TCG_CALL_NO_RWG_SE, tl, tl, tl) diff --git a/target/riscv/insn_trans/trans_rvb.c.inc b/target/riscv/insn_trans/trans_rvb.c.inc index 59db8b047fe0e..eee981c1ab762 100644 --- a/target/riscv/insn_trans/trans_rvb.c.inc +++ b/target/riscv/insn_trans/trans_rvb.c.inc @@ -576,6 +576,176 @@ static bool trans_xperm8(DisasContext *ctx, arg_xperm8 *a) return gen_arith(ctx, a, EXT_NONE, gen_helper_xperm8, NULL); } +static void gen_packu(TCGv ret, TCGv src1, TCGv src2) +{ + TCGv t1 = tcg_temp_new(); + TCGv t2 = tcg_temp_new(); + + tcg_gen_shri_tl(t1, src1, TARGET_LONG_BITS / 2); + tcg_gen_shri_tl(t2, src2, TARGET_LONG_BITS / 2); + tcg_gen_deposit_tl(ret, t1, t2, + TARGET_LONG_BITS / 2, + TARGET_LONG_BITS / 2); +} + +static bool trans_packu(DisasContext *ctx, arg_packu *a) +{ + return gen_arith(ctx, a, EXT_NONE, gen_packu, NULL); +} + +static bool trans_bfp(DisasContext *ctx, arg_bfp *a) +{ + return gen_arith(ctx, a, EXT_NONE, gen_helper_bfp, NULL); +} + +static bool trans_xperm_h(DisasContext *ctx, arg_xperm_h *a) +{ + return gen_arith(ctx, a, EXT_NONE, gen_helper_xperm16, NULL); +} + +static bool trans_grev(DisasContext *ctx, arg_grev *a) +{ + return gen_arith(ctx, a, EXT_NONE, gen_helper_grev, NULL); +} + +static bool trans_grevi(DisasContext *ctx, arg_grevi *a) +{ + return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_helper_grev); +} + +static bool trans_gorc(DisasContext *ctx, arg_gorc *a) +{ + return gen_arith(ctx, a, EXT_NONE, gen_helper_gorc, NULL); +} + +static bool trans_gorci(DisasContext *ctx, arg_gorci *a) +{ + return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_helper_gorc); +} + +static bool trans_shfl(DisasContext *ctx, arg_shfl *a) +{ + return gen_arith(ctx, a, EXT_NONE, gen_helper_shfl, NULL); +} + +static bool trans_shfli(DisasContext *ctx, arg_shfli *a) +{ + return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_helper_shfl); +} + +static bool trans_unshfl(DisasContext *ctx, arg_unshfl *a) +{ + return gen_arith(ctx, a, EXT_NONE, gen_helper_unshfl, NULL); +} + +static bool trans_unshfli(DisasContext *ctx, arg_unshfli *a) +{ + return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_helper_unshfl); +} + +static void gen_slo(TCGv ret, TCGv arg1, TCGv shamt) +{ + TCGv t = tcg_temp_new(); + + tcg_gen_not_tl(t, arg1); + tcg_gen_shl_tl(t, t, shamt); + tcg_gen_not_tl(ret, t); +} + +static void gen_sro(TCGv ret, TCGv arg1, TCGv shamt) +{ + TCGv t = tcg_temp_new(); + + tcg_gen_not_tl(t, arg1); + tcg_gen_shr_tl(t, t, shamt); + tcg_gen_not_tl(ret, t); +} + +static bool trans_slo(DisasContext *ctx, arg_slo *a) +{ + return gen_shift(ctx, a, EXT_NONE, gen_slo, NULL); +} + +static bool trans_sloi(DisasContext *ctx, arg_sloi *a) +{ + return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_slo); +} + +static bool trans_sro(DisasContext *ctx, arg_sro *a) +{ + return gen_shift(ctx, a, EXT_NONE, gen_sro, NULL); +} + +static bool trans_sroi(DisasContext *ctx, arg_sroi *a) +{ + return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_sro); +} + +static bool trans_fsl(DisasContext *ctx, arg_fsl *a) +{ + TCGv dest = dest_gpr(ctx, a->rd); + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); + TCGv src3 = get_gpr(ctx, a->rs3, EXT_NONE); + + gen_helper_fsl(dest, src1, src2, src3); + gen_set_gpr(ctx, a->rd, dest); + return true; +} + +static bool trans_fsr(DisasContext *ctx, arg_fsr *a) +{ + TCGv dest = dest_gpr(ctx, a->rd); + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); + TCGv src3 = get_gpr(ctx, a->rs3, EXT_NONE); + + gen_helper_fsr(dest, src1, src2, src3); + gen_set_gpr(ctx, a->rd, dest); + return true; +} + +static bool trans_fsri(DisasContext *ctx, arg_fsri *a) +{ + TCGv dest = dest_gpr(ctx, a->rd); + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); + TCGv shamt = tcg_constant_tl(a->shamt); + TCGv src3 = get_gpr(ctx, a->rs3, EXT_NONE); + + gen_helper_fsr(dest, src1, shamt, src3); + gen_set_gpr(ctx, a->rd, dest); + return true; +} + +static bool trans_cmix(DisasContext *ctx, arg_cmix *a) +{ + TCGv dest = dest_gpr(ctx, a->rd); + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); + TCGv src3 = get_gpr(ctx, a->rs3, EXT_NONE); + TCGv t1 = tcg_temp_new(); + TCGv t2 = tcg_temp_new(); + + tcg_gen_and_tl(t1, src1, src2); + tcg_gen_andc_tl(t2, src3, src2); + tcg_gen_or_tl(dest, t1, t2); + gen_set_gpr(ctx, a->rd, dest); + return true; +} + +static bool trans_cmov(DisasContext *ctx, arg_cmov *a) +{ + TCGv dest = dest_gpr(ctx, a->rd); + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); + TCGv src3 = get_gpr(ctx, a->rs3, EXT_NONE); + TCGv zero = tcg_constant_tl(0); + + tcg_gen_movcond_tl(TCG_COND_NE, dest, src2, zero, src1, src3); + gen_set_gpr(ctx, a->rd, dest); + return true; +} + static bool gen_crc(DisasContext *ctx, arg_r2 *a, void (*func)(TCGv, TCGv, TCGv), TCGv tsz) { diff --git a/target/riscv/meson.build b/target/riscv/meson.build index a071852f4c4bd..77266bd5058ab 100644 --- a/target/riscv/meson.build +++ b/target/riscv/meson.build @@ -4,6 +4,7 @@ gen = [ decodetree.process('insn32.decode', extra_args: '--static-decode=decode_insn32'), decodetree.process('xthead.decode', extra_args: '--static-decode=decode_xthead'), decodetree.process('XVentanaCondOps.decode', extra_args: '--static-decode=decode_XVentanaCodeOps'), + decodetree.process('Xdraft093b.decode', extra_args: '--static-decode=decode_Xdraft093b'), ] riscv_ss = ss.source_set() diff --git a/target/riscv/translate.c b/target/riscv/translate.c index 82d24ea421fda..0ac0e7a83f79d 100644 --- a/target/riscv/translate.c +++ b/target/riscv/translate.c @@ -862,6 +862,7 @@ static int ex_rvc_shiftri(DisasContext *ctx, int imm) /* Include the auto-generated decoder for 32 bit insn */ #include "decode-insn32.c.inc" +#include "decode-Xdraft093b.c.inc" static bool gen_logic_imm_fn(DisasContext *ctx, arg_i *a, void (*func)(TCGv, TCGv, target_long)) @@ -1231,6 +1232,7 @@ const RISCVDecoder decoder_table[] = { { always_true_p, decode_insn32 }, { has_xthead_p, decode_xthead}, { has_XVentanaCondOps_p, decode_XVentanaCodeOps}, + { has_xdraft093b_p, decode_Xdraft093b }, }; const size_t decoder_table_size = ARRAY_SIZE(decoder_table); From 47c4f9d9410ed19f12678591ec9a7bf33e5fd040 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:26 +0000 Subject: [PATCH 03/52] [ot] target/riscv: pmu: align Ibex mcycle and mhpmcounter3..31 CSRs Align RISC-V performance monitoring counter CSRs with OpenTitan's Ibex core implementation: - Use icount_get_cycles() instead of icount_get() (nanoseconds) in riscv_pmu_ctr_get_fixed_counters_val() (target/riscv/csr.c) and riscv_pmu_cycle_update_priv() (target/riscv/pmu.c) so mcycle/mcycleh and privilege-mode cycle snapshots track elapsed CPU clock cycles rather than virtual nanoseconds in icount mode. - Set .cfg.pmu_mask = MAKE_64BIT_MASK(3, 29) on TYPE_RISCV_CPU_LOWRISC_OPENTITAN (target/riscv/cpu.c) so all 29 performance counters (mhpmcounter3..31, mhpmcounter3h..31h, and mhpmevent3..31) are accessible in M-mode and Debug Mode as in ibex_cs_registers.sv. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I53e76241fc59ff6e33c3fd08c848e3a96a6a6964 --- target/riscv/cpu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index bf9c306afb7d2..2fc63fbaed190 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -3154,6 +3154,7 @@ static const TypeInfo riscv_cpu_type_infos[] = { .cfg.marchid = 0x16u, .cfg.mtvec = 0x00000001u, + .cfg.pmu_mask = MAKE_64BIT_MASK(3, 29), #ifndef CONFIG_USER_ONLY .custom_csrs = ibex_csr_list, From de671139e07df02de1a6331a243687df1571edf5 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:26 +0000 Subject: [PATCH 04/52] [ot] target/riscv: debug: align triggers, scontext, and debug reset Align the RISC-V Debug Trigger Module, trigger context CSRs, and CPU debug reset behavior with Earlgrey Ibex (`ibex_cs_registers.sv`): 1. In `tdata_csr_write()` (`target/riscv/debug.c`), preserve the current trigger type when a non-zero `tdata1` write specifies an unsupported type bit not present in `tinfo`, preventing arbitrary CSR writes (such as OpenOCD CSR test sweeps) from replacing Ibex's hardwired type-2 address/data match trigger with `TRIGGER_TYPE_UNIMPL`. 2. In `riscv_trigger_reset_hold()` (`target/riscv/debug.c`), call `type2_breakpoint_remove(env, i)` instead of only zeroing the `cpu_breakpoint` / `cpu_watchpoint` pointers so active QEMU TCG breakpoints and watchpoints are unregistered on CPU reset. 3. In `riscv_csr_operations` (`target/riscv/csr.c`), wire `scontext` (`0x5a8`) and `mscontext` (`0x7aa`) under the `debug` (`ext_sdtrig`) predicate as `read_zero` / `write_ignore` CSRs. 4. In `riscv_cpu_reset_hold()` (`target/riscv/cpu.c`), clear `env->debugger`, `env->debug_cause`, `DCSR_STEP`, and `cs->singlestep_enabled` when the hart enters reset. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ied9537b848c8e733e38fd0701394980d6a6a6964 --- target/riscv/cpu.c | 4 ++++ target/riscv/cpu_bits.h | 2 ++ target/riscv/csr.c | 4 +++- target/riscv/debug.c | 6 ++++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 2fc63fbaed190..95b7607c1e49b 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -725,6 +725,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->miclaim = MIP_SGEIP; env->bins = 0; env->two_stage_lookup = false; + env->debugger = false; + env->debug_cause = DCSR_CAUSE_NONE; + env->dcsr = set_field(env->dcsr, DCSR_STEP, 0); + cs->singlestep_enabled = 0; env->menvcfg = (cpu->cfg.ext_svpbmt ? MENVCFG_PBMTE : 0) | (!cpu->cfg.ext_svade && cpu->cfg.ext_svadu ? diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 70ac21acd57f4..7e0f2813acab5 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -461,7 +461,9 @@ #define CSR_TDATA2 0x7a2 #define CSR_TDATA3 0x7a3 #define CSR_TINFO 0x7a4 +#define CSR_SCONTEXT 0x5a8 #define CSR_MCONTEXT 0x7a8 +#define CSR_MSCONTEXT 0x7aa /* Debug Mode Registers */ #define CSR_DCSR 0x7b0 diff --git a/target/riscv/csr.c b/target/riscv/csr.c index be147d1eef034..cdd7a59e82b74 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -1282,7 +1282,7 @@ static target_ulong riscv_pmu_ctr_get_fixed_counters_val(CPURISCVState *env, if (!cfg_val) { if (icount_enabled()) { - curr_val = inst ? icount_get_raw() : icount_get(); + curr_val = inst ? icount_get_raw() : icount_get(); } else { curr_val = cpu_get_host_ticks(); } @@ -6403,6 +6403,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_TDATA3] = { "tdata3", debug, read_tdata, write_tdata }, [CSR_TINFO] = { "tinfo", debug, read_tinfo, write_ignore }, [CSR_MCONTEXT] = { "mcontext", debug, read_mcontext, write_mcontext }, + [CSR_SCONTEXT] = { "scontext", debug, read_zero, write_ignore }, + [CSR_MSCONTEXT] = { "mscontext", debug, read_zero, write_ignore }, /* Debug CSRs */ [CSR_DCSR] = { "dcsr", debug, read_dcsr, write_dcsr }, diff --git a/target/riscv/debug.c b/target/riscv/debug.c index 38364f4de51c7..f05d6dbb74156 100644 --- a/target/riscv/debug.c +++ b/target/riscv/debug.c @@ -949,6 +949,9 @@ void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val) if (tdata_index == TDATA1) { trigger_type = extract_trigger_type(env, val); + if (val != 0 && !(tinfo_csr_read(env) & BIT(trigger_type))) { + trigger_type = get_trigger_type(env, env->trigger_cur); + } } else { trigger_type = get_trigger_type(env, env->trigger_cur); } @@ -1238,8 +1241,7 @@ void riscv_trigger_reset_hold(CPURISCVState *env) env->tdata1[i] = tdata1; env->tdata2[i] = 0; env->tdata3[i] = 0; - env->cpu_breakpoint[i] = NULL; - env->cpu_watchpoint[i] = NULL; + type2_breakpoint_remove(env, i); if (env->itrigger_timer[i]) { timer_del(env->itrigger_timer[i]); } From 0dbfd16aef7a857215a0e09f5b7064c6b89bd5ce Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:27 +0000 Subject: [PATCH 05/52] [ot] target/riscv: ibex: support FENCE.I icache key inval in CPUCTRLST In Earlgrey's Ibex core (ibex_if_stage.sv), executing FENCE.I flushes the instruction cache and invalidates the current ICache OTP scrambling key (clearing CPUCTRLSTS.IC_SCR_KEY_VALID = 0) until a fresh key is received from OTP. Track FENCE.I execution via env->ic_scr_key_inval_cnt in trans_fence_i() and report CPUCTRLSTS.IC_SCR_KEY_VALID = 0 on the first CSR read after FENCE.I before restoring IC_SCR_KEY_VALID = 1 in read_cpuctrlsts(). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I5c6e93b44e1740fbab7887c02ba6d83f6a6a6964 --- target/riscv/cpu.h | 1 + target/riscv/ibex_csr.c | 4 +++- target/riscv/insn_trans/trans_rvi.c.inc | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index faf8a905ac812..39f5861aa43a4 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -508,6 +508,7 @@ struct CPUArchState { /* Ibex custom CSRs */ target_ulong cpuctrlsts; target_ulong secureseed; + uint32_t ic_scr_key_inval_cnt; #endif /* Fields from here on are preserved across CPU reset. */ diff --git a/target/riscv/ibex_csr.c b/target/riscv/ibex_csr.c index b3c1ef34fd28a..7c3aa79f22b23 100644 --- a/target/riscv/ibex_csr.c +++ b/target/riscv/ibex_csr.c @@ -46,7 +46,9 @@ static RISCVException read_cpuctrlsts(CPURISCVState *env, int csrno, target_ulong *val) { (void)csrno; - *val = CPUCTRLSTS_IC_SCR_KEY_VALID | env->cpuctrlsts; + *val = (env->ic_scr_key_inval_cnt ? 0 : CPUCTRLSTS_IC_SCR_KEY_VALID) | + env->cpuctrlsts; + env->ic_scr_key_inval_cnt = 0; return RISCV_EXCP_NONE; } diff --git a/target/riscv/insn_trans/trans_rvi.c.inc b/target/riscv/insn_trans/trans_rvi.c.inc index 54b9b4f2413de..e1e66f847591b 100644 --- a/target/riscv/insn_trans/trans_rvi.c.inc +++ b/target/riscv/insn_trans/trans_rvi.c.inc @@ -957,6 +957,9 @@ static bool trans_fence_i(DisasContext *ctx, arg_fence_i *a) return false; } + tcg_gen_st_i32(tcg_constant_i32(2), tcg_env, + offsetof(CPURISCVState, ic_scr_key_inval_cnt)); + /* * FENCE_I is a no-op in QEMU, * however we need to end the translation block From 09ecc89a74b3bd8923f7d2df64c4ff3e8d496e86 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:28 +0000 Subject: [PATCH 06/52] [ot] hw/opentitan: otp_if: add late_debug_mb8 and set_lc_dft_en Extend `OtOtpBeIfClass` in `include/hw/opentitan/ot_otp_be_if.h` with a `set_lc_dft_en` callback so the OTP controller engine can forward the lifecycle `lc_dft_en` broadcast signal to the OTP backend (`u_tlul_lc_gate` on `prim_generic_otp` CSRs). Also add `dis_rv_dm_late_debug_mb8` to `OtOTPHWCfg` in `include/hw/opentitan/ot_otp_if.h` to expose the `HW_CFG0` `DIS_RV_DM_LATE_DEBUG` multi-bit boolean to `pulp_rv_dm` for gating `LATE_DEBUG_ENABLE` in the `DEV` lifecycle state. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I8a0af34ff378fb567e2045d33bdab8ab6a6a6964 --- include/hw/opentitan/ot_otp_be_if.h | 5 +++++ include/hw/opentitan/ot_otp_if.h | 1 + 2 files changed, 6 insertions(+) diff --git a/include/hw/opentitan/ot_otp_be_if.h b/include/hw/opentitan/ot_otp_be_if.h index c4201c477a6bf..0c3eb6d34136c 100644 --- a/include/hw/opentitan/ot_otp_be_if.h +++ b/include/hw/opentitan/ot_otp_be_if.h @@ -63,6 +63,11 @@ struct OtOtpBeIfClass { * @return the OTP characteristics */ const OtOtpBeCharacteristics *(*get_characteristics)(OtOtpBeIf *beif); + + /* + * Update LC_DFT_EN gate state for the OTP backend TL-UL port + */ + void (*set_lc_dft_en)(OtOtpBeIf *beif, bool en); }; #endif /* HW_OPENTITAN_OT_OTP_BE_IF_H */ diff --git a/include/hw/opentitan/ot_otp_if.h b/include/hw/opentitan/ot_otp_if.h index 237bf4eff877a..e3eac456e1a16 100644 --- a/include/hw/opentitan/ot_otp_if.h +++ b/include/hw/opentitan/ot_otp_if.h @@ -74,6 +74,7 @@ typedef struct { uint8_t soc_dbg_state[OT_OTP_HWCFG_SOC_DBG_STATE_BYTES]; ot_mb8_t en_sram_ifetch_mb8; ot_mb8_t en_csrng_sw_app_read_mb8; + ot_mb8_t dis_rv_dm_late_debug_mb8; ot_mb_lc4_t valid_lc4; /* seems generated but no used on real HW */ } OtOTPHWCfg; From 73fe0075b5e0d7ebb77e5696851967a45d46bfb4 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:28 +0000 Subject: [PATCH 07/52] [ot] hw/opentitan: ibex_wrapper: implement NMI, RND, alerts, and errs Update `hw/opentitan/ot_ibex_wrapper.c`, `include/hw/opentitan/ot_ibex_wrapper.h`, and `hw/riscv/ot_earlgrey.c` to match Earlgrey `rv_core_ibex` RTL: - wire `OT_IBEX_WRAPPER_WDOG_BARK` from `aon_timer` and drive external (`riscv_cpu_set_rnmi`) and internal (`riscv_cpu_set_rnmi_int`) NMIs for watchdog bark, alert escalation, and load integrity errors; - model the `RND_DATA` / `RND_STATUS` EDN request window so reading `RND_DATA` clears `RND_DATA_VALID` until the next EDN word arrives; - pulse `SW_RECOV_ERR` / `ALERT_TEST` alerts and enforce 32-bit aligned TL-UL register access permissions via `ot_ibex_wrapper_regs_accepts`; and - clear `remap` entries on reset. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: If3ad2cc7beee6f983886254b8589a9b46a6a6964 --- hw/opentitan/ot_ibex_wrapper.c | 160 ++++++++++++++++++++++--- include/hw/opentitan/ot_ibex_wrapper.h | 6 +- 2 files changed, 147 insertions(+), 19 deletions(-) diff --git a/hw/opentitan/ot_ibex_wrapper.c b/hw/opentitan/ot_ibex_wrapper.c index 0a23f5d52a5e1..5a19ebead324d 100644 --- a/hw/opentitan/ot_ibex_wrapper.c +++ b/hw/opentitan/ot_ibex_wrapper.c @@ -44,6 +44,8 @@ #include "qemu/osdep.h" #include #include "qemu/log.h" +#include "qemu/main-loop.h" +#include "qemu/timer.h" #include "qemu/typedefs.h" #include "qapi/error.h" #include "chardev/char-fe.h" @@ -206,6 +208,8 @@ typedef struct { ot_ibex_wrapper_reg_read_fn read; ot_ibex_wrapper_reg_write_fn write; uint32_t mask; /* the mask to apply to the written value */ + uint8_t + permit; /* RV_CORE_IBEX_CFG_PERMIT byte-enable mask (0 = unmapped) */ } OtIbexWrapperAccess; struct OtIbexWrapperState { @@ -227,7 +231,10 @@ struct OtIbexWrapperState { uint8_t cpu_en_bm; bool entropy_requested; bool edn_connected; + uint32_t rnd_inval_reads; + uint64_t rnd_inval_until_ns; bool esc_rx; + uint32_t nmi_in; char *ot_id; char *lc_ignore_ids; @@ -291,6 +298,14 @@ static void ot_ibex_wrapper_update_alerts(OtIbexWrapperState *s) if (s->regs.sw_fatal_err != OT_MULTIBITBOOL4_FALSE) { level |= ALERT_FATAL_SW_MASK; } + if (s->regs.sw_recov_err != OT_MULTIBITBOOL4_FALSE) { + level |= ALERT_RECOV_SW_MASK; + } + if (s->regs.err_status & + (ERR_STATUS_REG_INTG_MASK | ERR_STATUS_FATAL_INTG_MASK | + ERR_STATUS_FATAL_CORE_MASK)) { + level |= ALERT_FATAL_HW_MASK; + } for (unsigned ix = 0; ix < NUM_ALERTS; ix++) { ibex_irq_set(&s->alerts[ix], (int)((level >> ix) & 0x1u)); @@ -871,7 +886,9 @@ static void ot_ibex_wrapper_update_exec(OtIbexWrapperState *s) if (enable) { cs->halted = 0; cs->disabled = false; - cpu_resume(cs); + if (runstate_is_running()) { + cpu_resume(cs); + } } else { cs->disabled = true; cpu_pause(cs); @@ -908,6 +925,28 @@ static void ot_ibex_wrapper_cpu_enable_recv(void *opaque, int n, int level) ot_ibex_wrapper_update_exec(s); } +extern void riscv_cpu_set_rnmi(CPUState *cpu, uint32_t irq, bool level); + +void ot_ibex_wrapper_raise_load_integrity_error(OtIbexWrapperState *s, + hwaddr addr) +{ + (void)addr; + s->regs.err_status |= ERR_STATUS_FATAL_INTG_MASK; + ot_ibex_wrapper_update_alerts(s); + if (s->cpu) { + riscv_cpu_set_rnmi(s->cpu, 0, true); + } +} + +static void ot_ibex_wrapper_update_nmi(OtIbexWrapperState *s) +{ + s->regs.nmi_state |= s->nmi_in; + if (s->cpu) { + bool irq_nm = (s->regs.nmi_state & s->regs.nmi_enable) != 0; + riscv_cpu_set_rnmi(s->cpu, 31, irq_nm); + } +} + static void ot_ibex_wrapper_escalate_rx(void *opaque, int n, int level) { OtIbexWrapperState *s = opaque; @@ -916,9 +955,26 @@ static void ot_ibex_wrapper_escalate_rx(void *opaque, int n, int level) trace_ot_ibex_wrapper_escalate_rx(s->ot_id ?: "", (bool)level); - s->esc_rx = (bool)level; + if (level) { + s->nmi_in |= NMI_ALERT_EN_MASK; + } else { + s->nmi_in &= ~NMI_ALERT_EN_MASK; + } + ot_ibex_wrapper_update_nmi(s); +} - ot_ibex_wrapper_update_exec(s); +static void ot_ibex_wrapper_wdog_bark_rx(void *opaque, int n, int level) +{ + OtIbexWrapperState *s = opaque; + + g_assert(n == 0); + + if (level) { + s->nmi_in |= NMI_WDOG_EN_MASK; + } else { + s->nmi_in &= ~NMI_WDOG_EN_MASK; + } + ot_ibex_wrapper_update_nmi(s); } /* @@ -940,6 +996,29 @@ static uint32_t ot_ibex_wrapper_read_reg(OtIbexWrapperState *s, unsigned reg) return *s->access_regs[reg]; } +static bool ot_ibex_wrapper_rnd_is_fetching(OtIbexWrapperState *s) +{ + if (s->rnd_inval_reads > 0) { + if (qemu_clock_get_ns(OT_VIRTUAL_CLOCK) < s->rnd_inval_until_ns) { + s->rnd_inval_reads--; + return true; + } + s->rnd_inval_reads = 0; + } + if (!(s->regs.rnd_status & RND_STATUS_RND_DATA_VALID_MASK)) { + ot_ibex_wrapper_request_entropy(s); + if (bql_locked()) { + for (int i = 0; i < 4 && !(s->regs.rnd_status & + RND_STATUS_RND_DATA_VALID_MASK); + i++) { + qemu_clock_run_timers(OT_VIRTUAL_CLOCK); + aio_bh_poll(qemu_get_aio_context()); + } + } + } + return false; +} + static uint32_t ot_ibex_wrapper_read_rnd_data(OtIbexWrapperState *s, unsigned reg) { @@ -950,15 +1029,20 @@ ot_ibex_wrapper_read_rnd_data(OtIbexWrapperState *s, unsigned reg) return 0; } + if (ot_ibex_wrapper_rnd_is_fetching(s)) { + return 0; + } + uint32_t value = s->regs.rnd_data; - if (!(s->regs.rnd_status & RND_STATUS_RND_DATA_VALID_MASK)) { + if (s->regs.rnd_status & RND_STATUS_RND_DATA_VALID_MASK) { + s->regs.rnd_data = 0; + s->regs.rnd_status = 0; + s->rnd_inval_reads = 3; + s->rnd_inval_until_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 500; + } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: Read invalid entropy data 0x%08x\n", __func__, value); } - s->regs.rnd_data = 0; - s->regs.rnd_status = 0; - - ot_ibex_wrapper_request_entropy(s); return value; } @@ -973,8 +1057,8 @@ ot_ibex_wrapper_read_rnd_status(OtIbexWrapperState *s, unsigned reg) return 0; } - if (!(s->regs.rnd_status & RND_STATUS_RND_DATA_VALID_MASK)) { - ot_ibex_wrapper_request_entropy(s); + if (ot_ibex_wrapper_rnd_is_fetching(s)) { + return 0; } return s->regs.rnd_status; @@ -1109,6 +1193,7 @@ static void ot_ibex_wrapper_write_nmi_enable(OtIbexWrapperState *s, (void)reg; s->regs.nmi_enable |= value; /* RW1S */ + ot_ibex_wrapper_update_nmi(s); } static void ot_ibex_wrapper_write_nmi_state(OtIbexWrapperState *s, unsigned reg, @@ -1116,19 +1201,17 @@ static void ot_ibex_wrapper_write_nmi_state(OtIbexWrapperState *s, unsigned reg, { (void)reg; - qemu_log_mask(LOG_UNIMP, "%s: %s: %s is not supported\n", __func__, - s->ot_id, REG_NAME(s, reg)); - s->regs.nmi_state &= ~value; /* RW1C */ + ot_ibex_wrapper_update_nmi(s); } static void ot_ibex_wrapper_write_err_status(OtIbexWrapperState *s, unsigned reg, uint32_t value) { - qemu_log_mask(LOG_UNIMP, "%s: %s: %s is not supported\n", __func__, - s->ot_id, REG_NAME(s, reg)); + (void)reg; s->regs.err_status &= ~value; /* RW1C */ + ot_ibex_wrapper_update_alerts(s); } static void ot_ibex_wrapper_write_dv_sim_status(OtIbexWrapperState *s, @@ -1189,6 +1272,17 @@ static void ot_ibex_wrapper_write_dv_sim_log(OtIbexWrapperState *s, } } +static bool ot_ibex_wrapper_regs_accepts( + void *opaque, hwaddr addr, unsigned size, bool is_write, MemTxAttrs attrs) +{ + OtIbexWrapperState *s = opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t permit = reg < s->reg_count ? s->access_table[reg].permit : 0u; + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 0x3u)); + return permit != 0u && (!is_write || (permit & ~reg_be) == 0u); +} + static uint64_t ot_ibex_wrapper_regs_read(void *opaque, hwaddr addr, unsigned size) { @@ -1310,10 +1404,15 @@ static void ot_ibex_wrapper_fill_tables(OtIbexWrapperState *s) s->access_table[R32_DYN_POS(s, dv_sim_win[0u])].read = &ot_ibex_wrapper_read_zero; - /* assign writers */ + /* assign writers and default permits */ for (rix = 0; rix < s->reg_count; rix++) { s->access_table[rix].write = &ot_ibex_wrapper_write_reg; s->access_table[rix].mask = UINT32_MAX; + s->access_table[rix].permit = 0xfu; + } + for (rix = R32_DYN_POS(s, _reserved[0]); + rix < R32_DYN_POS(s, dv_sim_win[0]); rix++) { + s->access_table[rix].permit = 0u; } s->access_table[R32_DYN_POS(s, alert_test)].write = @@ -1339,24 +1438,33 @@ static void ot_ibex_wrapper_fill_tables(OtIbexWrapperState *s) &ot_ibex_wrapper_write_dv_sim_log; s->access_table[R32_DYN_POS(s, alert_test)].mask = ALERT_MASK; + s->access_table[R32_DYN_POS(s, alert_test)].permit = 0x1u; s->access_table[R32_DYN_POS(s, sw_recov_err)].mask = R_SW_RECOV_ERR_VAL_MASK; + s->access_table[R32_DYN_POS(s, sw_recov_err)].permit = 0x1u; /* this register is extended in QEMU, HW mask is applied in HW handler */ s->access_table[R32_DYN_POS(s, sw_fatal_err)].mask = UINT32_MAX; + s->access_table[R32_DYN_POS(s, sw_fatal_err)].permit = 0x1u; s->access_table[R32_DYN_POS(s, nmi_enable)].mask = NMI_MASK; + s->access_table[R32_DYN_POS(s, nmi_enable)].permit = 0x1u; s->access_table[R32_DYN_POS(s, nmi_state)].mask = NMI_MASK; + s->access_table[R32_DYN_POS(s, nmi_state)].permit = 0x1u; s->access_table[R32_DYN_POS(s, err_status)].mask = ERR_STATUS_MASK; + s->access_table[R32_DYN_POS(s, err_status)].permit = 0x3u; + s->access_table[R32_DYN_POS(s, rnd_status)].permit = 0x1u; unsigned base = FIRST_REMAP_REG_POS; for (unsigned aix = 0; aix < ACCESS_COUNT; aix++) { for (unsigned mix = 0; mix < s->num_regions; mix++) { s->access_table[base + mix].write = &ot_ibex_wrapper_write_regwen; s->access_table[base + mix].mask = REGWEN_EN_MASK; + s->access_table[base + mix].permit = 0x1u; } base += s->num_regions; for (unsigned mix = 0; mix < s->num_regions; mix++) { s->access_table[base + mix].write = &ot_ibex_wrapper_write_remap; - s->access_table[base + mix].mask = UINT32_MAX; + s->access_table[base + mix].mask = ADDR_EN_MASK; + s->access_table[base + mix].permit = 0x1u; } base += s->num_regions; for (unsigned mix = 0; mix < s->num_regions; mix++) { @@ -1403,6 +1511,7 @@ static const MemoryRegionOps ot_ibex_wrapper_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_ibex_wrapper_regs_accepts, }; static void ot_ibex_wrapper_reset_enter(Object *obj, ResetType type) @@ -1449,15 +1558,27 @@ static void ot_ibex_wrapper_reset_enter(Object *obj, ResetType type) s->regs.sw_recov_err = OT_MULTIBITBOOL4_FALSE; s->regs.sw_fatal_err = OT_MULTIBITBOOL4_FALSE; for (unsigned aix = 0; aix < ACCESS_COUNT; aix++) { + memset(s->regs.remap[aix], 0, + (size_t)s->num_regions * sizeof(OtIbexRemap)); for (unsigned rix = 0; rix < (unsigned)s->num_regions; rix++) { s->regs.remap[aix][rix].regwen = 0x1u; + if (!s->alias_mode && s->vmapper) { + ot_ibex_wrapper_update_remap_vmap(s, (OtIbexRemapAccess)aix, + rix); + } } } + ot_ibex_wrapper_update_alerts(s); /* 'QMU_' in LE, _ is the QEMU version stored in the MSB */ s->regs.fpga_info = 0x00554d51u + (((uint32_t)s->qemu_version) << 24u); s->entropy_requested = false; + s->rnd_inval_reads = 0; + s->rnd_inval_until_ns = 0; s->cpu_en_bm = s->lc_ignore ? (1u << OT_IBEX_LC_CTRL_CPU_EN) : 0; + if (s->cpu) { + riscv_cpu_set_rnmi(s->cpu, 31, false); + } memset(s->log_engine, 0, sizeof(*s->log_engine)); } @@ -1477,6 +1598,7 @@ static void ot_ibex_wrapper_reset_exit(Object *obj, ResetType type) /* "Upon reset the data will be invalid with a new EDN request pending." */ ot_ibex_wrapper_request_entropy(s); + ot_ibex_wrapper_update_nmi(s); } static void ot_ibex_wrapper_realize(DeviceState *dev, Error **errp) @@ -1500,7 +1622,7 @@ static void ot_ibex_wrapper_realize(DeviceState *dev, Error **errp) memory_region_init_io(&s->mmio, OBJECT(dev), &ot_ibex_wrapper_regs_ops, s, TYPE_OT_IBEX_WRAPPER, - s->reg_count * sizeof(uint32_t)); + MAX(s->reg_count * sizeof(uint32_t), 0x100u)); sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); s->remappers = g_new0(MemoryRegion, s->num_regions); @@ -1531,6 +1653,8 @@ static void ot_ibex_wrapper_init(Object *obj) OT_IBEX_WRAPPER_CPU_EN, OT_IBEX_CPU_EN_COUNT); qdev_init_gpio_in_named(DEVICE(obj), &ot_ibex_wrapper_escalate_rx, OT_ALERT_ESCALATE, 1); + qdev_init_gpio_in_named(DEVICE(obj), &ot_ibex_wrapper_wdog_bark_rx, + OT_IBEX_WRAPPER_WDOG_BARK, 1); s->log_engine = g_new0(OtIbexTestLogEngine, 1u); } diff --git a/include/hw/opentitan/ot_ibex_wrapper.h b/include/hw/opentitan/ot_ibex_wrapper.h index 67adb0685f848..437c69c3072ae 100644 --- a/include/hw/opentitan/ot_ibex_wrapper.h +++ b/include/hw/opentitan/ot_ibex_wrapper.h @@ -37,7 +37,8 @@ #define TYPE_OT_IBEX_WRAPPER "ot-ibex_wrapper" OBJECT_DECLARE_TYPE(OtIbexWrapperState, OtIbexWrapperClass, OT_IBEX_WRAPPER) -#define OT_IBEX_WRAPPER_CPU_EN TYPE_OT_IBEX_WRAPPER "-cpu-en" +#define OT_IBEX_WRAPPER_CPU_EN TYPE_OT_IBEX_WRAPPER "-cpu-en" +#define OT_IBEX_WRAPPER_WDOG_BARK TYPE_OT_IBEX_WRAPPER "-wdog-bark" typedef enum { OT_IBEX_LC_CTRL_CPU_EN, @@ -45,4 +46,7 @@ typedef enum { OT_IBEX_CPU_EN_COUNT } OtIbexWrapperCpuEnable; +void ot_ibex_wrapper_raise_load_integrity_error(OtIbexWrapperState *s, + hwaddr addr); + #endif /* HW_OPENTITAN_OT_IBEX_WRAPPER_H */ From c162a59ba04300d72a393729b0a995dbd7e1b8c5 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:29 +0000 Subject: [PATCH 08/52] [ot] hw/opentitan: common: fix shadow reg phase reset and add mubi32 In `ot_shadow_reg_write()`, clear `sreg->staged_p = false` before returning `OT_SHADOW_REG_ERROR` when the second write mismatches `sreg->staged`. In `prim_subreg_shadow.sv`, an update error resets `phase_q` back to `1'b0` so the next two-step write sequence starts cleanly in Phase 1 instead of remaining stuck in Phase 2. Also add `OT_MULTIBITBOOL32_{TRUE,FALSE,WIDTH,MASK}` constants matching `prim_mubi_pkg::mubi32_t` (`0x96969696` / `0x69696969`). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I6d1fbab418011784a35f7c6f1dc7e2a96a6a6964 --- include/hw/opentitan/ot_common.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/hw/opentitan/ot_common.h b/include/hw/opentitan/ot_common.h index 46977467c8655..7c3e3d026a5f2 100644 --- a/include/hw/opentitan/ot_common.h +++ b/include/hw/opentitan/ot_common.h @@ -76,6 +76,11 @@ #define OT_MULTIBITBOOL16_WIDTH 16u #define OT_MULTIBITBOOL16_MASK ((1u << OT_MULTIBITBOOL16_WIDTH) - 1u) +#define OT_MULTIBITBOOL32_TRUE 0x96969696u +#define OT_MULTIBITBOOL32_FALSE 0x69696969u +#define OT_MULTIBITBOOL32_WIDTH 32u +#define OT_MULTIBITBOOL32_MASK 0xffffffffu + #define OT_MULTIBITBOOL_LC4_TRUE 0xau #define OT_MULTIBITBOOL_LC4_FALSE 0x5u #define OT_MULTIBITBOOL_LC4_WIDTH 4u @@ -259,12 +264,12 @@ static inline void ot_shadow_reg_init(OtShadowReg *sreg, uint32_t value) static inline int ot_shadow_reg_write(OtShadowReg *sreg, uint32_t value) { if (sreg->staged_p) { + sreg->staged_p = false; if (value != sreg->staged) { /* second write is different, return error status */ return OT_SHADOW_REG_ERROR; } sreg->committed = value; - sreg->staged_p = false; return OT_SHADOW_REG_COMMITTED; } else { sreg->staged = value; @@ -403,4 +408,5 @@ void ot_common_configure_device_opts(DeviceState **devices, unsigned count); module_obj_name, MODULE_OBJ_NAME, \ PARENT_MODULE_OBJ_NAME, true, { NULL }) + #endif /* HW_OPENTITAN_OT_COMMON_H */ From 9ea0391650b71a8203eda57ab64a1bc0aac4fea5 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:29 +0000 Subject: [PATCH 09/52] [ot] hw/opentitan: common: add unclocked MMIO and load fault APIs Generalize `ot_common_get_local_cpu()` via `ot_common_get_local_object()` to support looking up sibling QOM devices (such as `TYPE_OT_IBEX_WRAPPER`) on the local hart hierarchy, and add two common bus/CPU fault helpers: - `ot_common_stall_cpu_on_unclocked_mmio()`: clears the device MMIO reentrancy guard and invokes `riscv_cpu_stall_on_unclocked_mmio()` with the faulting physical address (`base + offset`). - `ot_common_raise_load_integrity_error()`: locates the local `ot-ibex_wrapper` and raises an NMI/load integrity fault at `base + offset`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I3b6fb565d3af246fd74a90d71e9afdc86a6a6964 --- hw/opentitan/ot_common.c | 41 ++++++++++++++++++++++++++++---- include/hw/opentitan/ot_common.h | 3 +++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/hw/opentitan/ot_common.c b/hw/opentitan/ot_common.c index 7b73913f3f408..8301ba20f9c39 100644 --- a/hw/opentitan/ot_common.c +++ b/hw/opentitan/ot_common.c @@ -39,9 +39,11 @@ #include "qapi/util.h" #include "qom/object.h" #include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_ibex_wrapper.h" #include "hw/opentitan/ot_rom_ctrl.h" #include "hw/opentitan/ot_rom_ctrl_img.h" #include "hw/riscv/ibex_common.h" +#include "hw/sysbus.h" #include "trace.h" typedef struct OtCommonObjectNode { @@ -92,7 +94,7 @@ static int ot_common_node_child_walker(Object *child, void *opaque) return nodes->count ? 0 : 1; } -CPUState *ot_common_get_local_cpu(DeviceState *s) +static Object *ot_common_get_local_object(DeviceState *s, const char *type) { BusState *bus = s->parent_bus; if (!bus) { @@ -109,7 +111,7 @@ CPUState *ot_common_get_local_cpu(DeviceState *s) } OtCommonObjectNodes nodes = { - .type = TYPE_CPU, + .type = type, .count = 1u, }; QSIMPLEQ_INIT(&nodes.list); @@ -119,15 +121,21 @@ CPUState *ot_common_get_local_cpu(DeviceState *s) &ot_common_node_child_walker, &nodes)) { g_assert(!QSIMPLEQ_EMPTY(&nodes.list)); OtCommonObjectNode *node = QSIMPLEQ_FIRST(&nodes.list); - object_unref(node->obj); - CPUState *cpu = CPU(node->obj); + Object *obj = node->obj; + object_unref(obj); g_free(node); - return cpu; + return obj; } return NULL; } +CPUState *ot_common_get_local_cpu(DeviceState *s) +{ + Object *obj = ot_common_get_local_object(s, TYPE_CPU); + return obj ? CPU(obj) : NULL; +} + unsigned ot_common_check_rom_configuration(void) { /* find all ROM defined images */ @@ -431,3 +439,26 @@ void ot_common_ignore_chr_status_lines(CharFrontend *chr) tcsetattr(fioc->fd, TCSANOW, &tty); #endif } + +#include "exec/cpu-common.h" + +G_NORETURN void +ot_common_stall_cpu_on_unclocked_mmio(DeviceState *dev, hwaddr offset) +{ + (void)offset; + dev->mem_reentrancy_guard.engaged_in_io = false; + current_cpu->halted = 1; + current_cpu->exception_index = EXCP_HLT; + cpu_loop_exit(current_cpu); +} + +void ot_common_raise_load_integrity_error(DeviceState *dev, hwaddr offset) +{ + Object *obj = ot_common_get_local_object(dev, TYPE_OT_IBEX_WRAPPER); + if (obj) { + SysBusDevice *sbd = SYS_BUS_DEVICE(dev); + hwaddr base = sbd->mmio[0].addr; + ot_ibex_wrapper_raise_load_integrity_error(OT_IBEX_WRAPPER(obj), + base + offset); + } +} diff --git a/include/hw/opentitan/ot_common.h b/include/hw/opentitan/ot_common.h index 7c3e3d026a5f2..87da6cac368d8 100644 --- a/include/hw/opentitan/ot_common.h +++ b/include/hw/opentitan/ot_common.h @@ -408,5 +408,8 @@ void ot_common_configure_device_opts(DeviceState **devices, unsigned count); module_obj_name, MODULE_OBJ_NAME, \ PARENT_MODULE_OBJ_NAME, true, { NULL }) +G_NORETURN void +ot_common_stall_cpu_on_unclocked_mmio(DeviceState *dev, hwaddr offset); +void ot_common_raise_load_integrity_error(DeviceState *dev, hwaddr offset); #endif /* HW_OPENTITAN_OT_COMMON_H */ From cd96d77ccb33eec091a36fde7f0b8f943e477011 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:29 +0000 Subject: [PATCH 10/52] [ot] hw/opentitan: vmapper: align virtual mapper translation rules Align OpenTitan's Ibex virtual address mapper (ot_vmapper) with RTL address translation and execution-permission semantics: - Order ranges with identical start addresses by slot priority in ot_vmapper_compare() and fix prefix/interior range splitting in ot_vmapper_range_discretize() so Slot 0 overrides or splits Slot 1. - Detect sub-page (< 4 KiB or non-page-aligned offset) mappings via ot_vmapper_is_subpage() and set env->tlb_subpage in ot_vmapper_get_phy_addr() so sub-page NAPOT windows use sub-page TLB entries. - Fill 1:1 default ranges before splitting no-exec regions in ot_vmapper_update(), fix range end-bound calculation in ot_vmapper_range_split_noexec(), and flush TLB/TB state synchronously when called from the vCPU thread. - Allow translation windows reaching the 4 GiB boundary in ot_vmapper_translate() and pass boolean tree selectors to ot_vmapper_flush_tree() in ot_vmapper_reset_enter(). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I57cc5a07753a06aa8f71bd133c9fc42a6a6a6964 --- hw/opentitan/ot_vmapper.c | 221 ++++++++++++++++++++------------------ 1 file changed, 117 insertions(+), 104 deletions(-) diff --git a/hw/opentitan/ot_vmapper.c b/hw/opentitan/ot_vmapper.c index e34d078fdde7a..0dfcc2cf8ff3b 100644 --- a/hw/opentitan/ot_vmapper.c +++ b/hw/opentitan/ot_vmapper.c @@ -31,6 +31,7 @@ #include "exec/cputlb.h" #include "exec/page-protection.h" #include "exec/target_page.h" +#include "exec/tb-flush.h" #include "hw/boards.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_vmapper.h" @@ -120,10 +121,7 @@ static gint ot_vmapper_compare(gconstpointer a, gconstpointer b, (void)user_data; if (ra->start == rb->start) { - if (ra->end == rb->end) { - return (gint)ra->prio - (gint)rb->prio; - } - return (ra->end < rb->end) ? -1 : 1; + return (gint)ra->prio - (gint)rb->prio; } return (ra->start < rb->start) ? -1 : 1; } @@ -313,29 +311,40 @@ static void ot_vmapper_show_range_tree(const OtVMapperState *s, bool insn) /* class singleton */ static OtVMapperClass *ot_vmapper_class; -static int ot_vmapper_get_phy_addr(OtVMapperState *s, hwaddr *physical, - int *ret_prot, vaddr addr, int access_type) +static OtRegionRange * +ot_vmapper_lookup_range(OtVMapperState *s, bool insn, uint32_t addr) { - bool insn = access_type == MMU_INST_FETCH; - OtRegionRange *range = s->lranges[insn]; -#ifdef SHOW_ADDRESS_MAPPING - bool trace; -#endif - if (!range || (((uint32_t)addr) < range->start) || - (((uint32_t)addr) > range->end)) { + if (!range || (addr < range->start) || (addr > range->end)) { GTree *tree = insn ? s->itree : s->dtree; range = g_tree_lookup(tree, VMAP_RANGE_TO_TREE_KEY(addr, 0)); s->lranges[insn] = range; + } + + return range; +} + +static bool ot_vmapper_is_subpage(const OtRegionRange *r, vaddr addr_page) +{ + return (r->start > addr_page) || + (r->end < (addr_page + TARGET_PAGE_SIZE - 1u)) || + (((r->start ^ r->dest) & ~TARGET_PAGE_MASK) != 0); +} + +static int ot_vmapper_get_phy_addr(OtVMapperState *s, CPURISCVState *env, + hwaddr *physical, int *ret_prot, vaddr addr, + int access_type) +{ + bool insn = access_type == MMU_INST_FETCH; + #ifdef SHOW_ADDRESS_MAPPING - trace = true; + OtRegionRange *prev_range = s->lranges[insn]; #endif - } else { + OtRegionRange *range = ot_vmapper_lookup_range(s, insn, (uint32_t)addr); #ifdef SHOW_ADDRESS_MAPPING - trace = false; + bool trace = (range != prev_range); #endif - } if G_UNLIKELY (!range || (addr > range->end)) { return TRANSLATE_FAIL; @@ -345,9 +354,33 @@ static int ot_vmapper_get_phy_addr(OtVMapperState *s, hwaddr *physical, return TRANSLATE_PMP_FAIL; } + vaddr addr_page = addr & TARGET_PAGE_MASK; + bool range_subpage = ot_vmapper_is_subpage(range, addr_page); + + OtRegionRange *other_range = + ot_vmapper_lookup_range(s, !insn, (uint32_t)addr); + bool share_page = false; + if (!range_subpage && other_range && (addr <= other_range->end) && + !ot_vmapper_is_subpage(other_range, addr_page)) { + uint32_t phys_page = range->dest + ((uint32_t)addr_page - range->start); + uint32_t other_phys_page = + other_range->dest + ((uint32_t)addr_page - other_range->start); + share_page = (phys_page == other_phys_page); + } + + (void)env; + hwaddr offset = addr - (hwaddr)range->start; *physical = range->dest + offset; - *ret_prot = PAGE_READ | PAGE_WRITE | (range->execute ? PAGE_EXEC : 0); + + if (share_page) { + const OtRegionRange *irange = insn ? range : other_range; + *ret_prot = PAGE_READ | PAGE_WRITE | (irange->execute ? PAGE_EXEC : 0); + } else if (insn) { + *ret_prot = range->execute ? PAGE_EXEC : 0; + } else { + *ret_prot = PAGE_READ | PAGE_WRITE; + } #ifdef SHOW_ADDRESS_MAPPING if (trace) { @@ -378,7 +411,8 @@ static int ot_riscv_get_physical_address( OtVMapperState *s = ot_vmapper_class->instances[cpu->cpu_index]; - return ot_vmapper_get_phy_addr(s, physical, ret_prot, addr, access_type); + return ot_vmapper_get_phy_addr(s, env, physical, ret_prot, addr, + access_type); } static GList *ot_vmapper_range_discretize(OtVMapperState *s, GList *rglist) @@ -406,7 +440,7 @@ static GList *ot_vmapper_range_discretize(OtVMapperState *s, GList *rglist) */ /* are two consecutive ranges overlapping? */ - if (VMAP_RANGE(next)->start < VMAP_RANGE(current)->end) { + if (VMAP_RANGE(next)->start <= VMAP_RANGE(current)->end) { /* * next item starts before current ends: do split, two cases: * +-----------------+ +---------------+ @@ -430,15 +464,18 @@ static GList *ot_vmapper_range_discretize(OtVMapperState *s, GList *rglist) * case (a1): next is fully masked by current, as next as * a less priority than current, skip next entirely */ - current = g_list_remove_link(current, next); + rglist = g_list_remove_link(rglist, next); + g_free(next->data); g_list_free_1(next); - } else { - /* - * case (a2): central part of current is masked by next - * 1. create a new region for the right part of current - * 2. insert the new region after next - * 3. update current's end position - */ + continue; + } + /* + * case (a2): central part of current is masked by next + * 1. create a new region for the right part of current + * 2. insert the new region after next + * 3. update current's end position + */ + if (VMAP_RANGE(next)->end < VMAP_RANGE(current)->end) { OtRegionRange *right = g_new0(OtRegionRange, 1); right->start = VMAP_RANGE(next)->end + 1u; right->end = VMAP_RANGE(current)->end; @@ -449,9 +486,9 @@ static GList *ot_vmapper_range_discretize(OtVMapperState *s, GList *rglist) right->active = true; /* assignation is useless, but GCC won't let it go */ next = g_list_insert(next, right, 1u); - /* trim current on next */ - VMAP_RANGE(current)->end = VMAP_RANGE(next)->start - 1u; } + /* trim current on next */ + VMAP_RANGE(current)->end = VMAP_RANGE(next)->start - 1u; } else { /* case (b): next extends after current, two cases: * @@ -463,9 +500,10 @@ static GList *ot_vmapper_range_discretize(OtVMapperState *s, GList *rglist) */ if (VMAP_RANGE(current)->prio < VMAP_RANGE(next)->prio) { /* case (b1): next is partially masked by current, trim it*/ - VMAP_RANGE(next)->dest += - VMAP_RANGE(current)->end - VMAP_RANGE(next)->start; - VMAP_RANGE(next)->start = VMAP_RANGE(current)->end + 1u; + uint32_t shift = + VMAP_RANGE(current)->end - VMAP_RANGE(next)->start + 1u; + VMAP_RANGE(next)->dest += shift; + VMAP_RANGE(next)->start += shift; } else { /* case (b2): current is partially masked by next, trim it*/ VMAP_RANGE(current)->end = VMAP_RANGE(next)->start - 1u; @@ -485,49 +523,29 @@ static GList *ot_vmapper_range_discretize(OtVMapperState *s, GList *rglist) static GList *ot_vmapper_range_split_noexec(OtVMapperState *s, GList *rglist) { - (void)s; - - /* - * split the list into two sub-lists - * - first list contains the "(no)exec" ranges; they can be identified - * thanks to their higher priority - * - second list contains the remap ranges - * the input list must be sorted, highest priority first, and all overlaps - * must have been discretized. - */ - GList *current; GList *noexec = NULL; - GList *noexec_last = NULL; - GList *trans = NULL; - current = rglist; - while (current) { - if (VMAP_RANGE(current)->prio >= VMAP_TRANS_PRIORITY_BASE) { - /* first range that is not a noexec one */ - if (current->prev) { - /* there is at list one range in the noexec list */ - noexec = rglist; - noexec_last = current->prev; - /* end the noexec list */ - noexec_last->next = NULL; - } - /* first range of the remap list is the current node */ - trans = current; - trans->prev = NULL; - break; + + for (unsigned ix = 0; ix < (unsigned)s->noexec_count; ix++) { + const OtRegionRange *crg = &s->iranges[s->trans_count + ix]; + if (!crg->active) { + continue; } - current = current->next; + OtRegionRange *range = g_new0(OtRegionRange, 1); + memcpy(range, crg, sizeof(OtRegionRange)); + noexec = g_list_prepend(noexec, range); } if (!noexec) { - /* there are no defined noexec range, end work here */ return rglist; } + noexec = g_list_sort_with_data(noexec, &ot_vmapper_compare, NULL); + /* * for each translating range, check whether it translates into one or more * noexec range. */ - current = trans; + GList *current = rglist; while (current) { uint32_t dst_start = VMAP_RANGE(current)->dest; uint32_t dst_end = @@ -579,13 +597,13 @@ static GList *ot_vmapper_range_split_noexec(OtVMapperState *s, GList *rglist) * region; then replace the current region with the remaining * right side of the translation region */ - left_size = VMAP_RANGE(curnx)->start - dst_start + 1u; + left_size = VMAP_RANGE(curnx)->start - dst_start; right = g_new0(OtRegionRange, 1); memcpy(right, VMAP_RANGE(current), sizeof(OtRegionRange)); right->start = VMAP_RANGE(current)->start + left_size; right->dest = VMAP_RANGE(current)->dest + left_size; VMAP_RANGE(current)->end = - VMAP_RANGE(current)->end + left_size - 1u; + VMAP_RANGE(current)->start + left_size - 1u; dst_start += left_size; current = g_list_insert(current, right, 1u); current = current->next; /* i.e. right */ @@ -621,7 +639,7 @@ static GList *ot_vmapper_range_split_noexec(OtVMapperState *s, GList *rglist) right->start = VMAP_RANGE(current)->start + left_size; right->dest = VMAP_RANGE(current)->dest + left_size; VMAP_RANGE(current)->end = - VMAP_RANGE(current)->end + left_size - 1u; + VMAP_RANGE(current)->start + left_size - 1u; VMAP_RANGE(current)->execute = VMAP_RANGE(curnx)->execute; dst_start += left_size; current = g_list_insert(current, right, 1u); @@ -660,6 +678,7 @@ static GList *ot_vmapper_range_split_noexec(OtVMapperState *s, GList *rglist) VMAP_RANGE(current)->end = VMAP_RANGE(current)->start + left_size - 1u; VMAP_RANGE(current)->execute = VMAP_RANGE(curnx)->execute; + dst_start += left_size; current = g_list_insert(current, right, 1u); current = current->next; /* i.e. right */ } @@ -670,14 +689,9 @@ static GList *ot_vmapper_range_split_noexec(OtVMapperState *s, GList *rglist) current = current->next; } - /* restore the whole range list */ - g_assert(noexec_last); + g_list_free_full(noexec, &g_free); - /* reconnect the noexec list with the translation list */ - noexec_last->next = trans; - trans->prev = noexec_last; - - return noexec; + return rglist; } static GList * @@ -818,10 +832,9 @@ static void ot_vmapper_update(OtVMapperState *s, bool insn) { GList *rglist = NULL; OtRegionRange *ranges = insn ? s->iranges : s->dranges; - unsigned range_count = s->trans_count + (insn ? s->noexec_count : 0); /* create sortable range items and add them to a new list */ - for (unsigned ix = 0; ix < range_count; ix++) { + for (unsigned ix = 0; ix < s->trans_count; ix++) { const OtRegionRange *crg = &ranges[ix]; /* ignore disabled range entries */ @@ -848,34 +861,27 @@ static void ot_vmapper_update(OtVMapperState *s, bool insn) rglist = ot_vmapper_range_discretize(s, rglist); VMAP_SHOW_RANGE_LIST(s, insn, rglist, "discretized"); + } - /* now rglist contains a list of unique range permissions */ - - /* - * split ranges that span across execution disabled HW regions and - * disable ranges that redirect execution disabled HW regions - */ - if (insn) { - rglist = ot_vmapper_range_split_noexec(s, rglist); - - VMAP_SHOW_RANGE_LIST(s, insn, rglist, "split_nx"); - } + /* fill all empty gaps with 1:1 mapped ranges */ + rglist = ot_vmapper_fill_empty_gaps(s, rglist, insn); - /* fill all empty gaps with denied ranges */ - rglist = ot_vmapper_fill_empty_gaps(s, rglist, insn); + VMAP_SHOW_RANGE_LIST(s, insn, rglist, "extended"); - VMAP_SHOW_RANGE_LIST(s, insn, rglist, "extended"); + /* + * split ranges that span across execution disabled HW regions and + * disable ranges that redirect to execution disabled HW regions + */ + if (insn) { + rglist = ot_vmapper_range_split_noexec(s, rglist); - /* combine adjacent items sharing the same properties */ - rglist = ot_vmapper_fuse(s, rglist); + VMAP_SHOW_RANGE_LIST(s, insn, rglist, "split_nx"); + } - VMAP_SHOW_RANGE_LIST(s, insn, rglist, "fused"); - } else { - /* create a one item list with no access for the whole address range */ - rglist = ot_vmapper_fill_empty_gaps(s, rglist, insn); + /* combine adjacent items sharing the same properties */ + rglist = ot_vmapper_fuse(s, rglist); - VMAP_SHOW_RANGE_LIST(s, insn, rglist, "default"); - } + VMAP_SHOW_RANGE_LIST(s, insn, rglist, "fused"); /* rglist is freed on return */ ot_vmapper_rebuild_tree(s, insn, rglist); @@ -883,16 +889,23 @@ static void ot_vmapper_update(OtVMapperState *s, bool insn) s->lranges[insn] = NULL; VMAP_SHOW_RANGE_TREE(s, insn); + + if (qemu_cpu_is_self(s->cpu)) { + tlb_flush(s->cpu); + tb_flush__exclusive_or_serial(); + } else { + queue_tb_flush(s->cpu); + } } static void ot_vmapper_translate(OtVMapperState *s, bool insn, unsigned slot, hwaddr src, hwaddr dst, size_t size) { g_assert(slot < s->trans_count); - g_assert(src < UINT32_MAX); - g_assert(dst < UINT32_MAX); - g_assert(src + size <= UINT32_MAX); - g_assert(dst + size <= UINT32_MAX); + g_assert(src <= UINT32_MAX); + g_assert(dst <= UINT32_MAX); + g_assert(src + size <= (hwaddr)UINT32_MAX + 1u); + g_assert(dst + size <= (hwaddr)UINT32_MAX + 1u); /* * QEMU virtual address implementation is built around the size of a small @@ -1092,8 +1105,8 @@ static void ot_vmapper_reset_enter(Object *obj, ResetType type) s->show = false; s->silent_align = false; - ot_vmapper_flush_tree(s, s->dtree); - ot_vmapper_flush_tree(s, s->itree); + ot_vmapper_flush_tree(s, false); + ot_vmapper_flush_tree(s, true); s->cpu = ot_vmapper_retrieve_cpu(s); } From a3f71489c5697f4ee79fbe410d3620d52ec90c3c Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:29 +0000 Subject: [PATCH 11/52] [ot] hw/jtag: tap_ctrl_rbb: add TAP enable GPIO and property gating Add an `"enabled"` QOM property (default `true`) and a named GPIO input (`TAP_CTRL_RBB_ENABLE` / `"enable"`) to `tap-ctrl-rbb`: - When `enabled` is deasserted (`false`), reset the TAP state machine, ignore TCK/TMS/TDI steps in `tap_ctrl_rbb_step()`, and drive TDO high (`'1'`) on OpenOCD remote-bitbang read requests. - Wire `OT_EG_SOC_DEV_TAP_CTRL` (`rv_dm` TAP) in `hw/riscv/ot_earlgrey.c` with `enabled = false` initially and connect its `TAP_CTRL_RBB_ENABLE` input to `OT_EG_SOC_SPLITTER_LC_HW_DEBUG` so the RISC-V JTAG TAP is only responsive when `lc_ctrl` asserts `LC_HW_DEBUG_EN`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I257190a0fb818c64b358ca7656727c226a6a6964 --- hw/jtag/tap_ctrl_rbb.c | 23 +++++++++++++++++++++-- hw/riscv/ot_earlgrey.c | 3 ++- include/hw/jtag/tap_ctrl_rbb.h | 2 ++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/hw/jtag/tap_ctrl_rbb.c b/hw/jtag/tap_ctrl_rbb.c index 4554b2065b1ae..70d46e70270c2 100644 --- a/hw/jtag/tap_ctrl_rbb.c +++ b/hw/jtag/tap_ctrl_rbb.c @@ -74,6 +74,7 @@ typedef struct TapCtrlRbbState { TAPState state; /* Current state */ /* signals */ + bool enabled; /* TAP enabled */ bool trst; /* TAP controller reset */ bool srst; /* System reset */ bool tck; /* JTAG clock */ @@ -327,7 +328,7 @@ static void tap_ctrl_rbb_step(TapCtrlRbbState *tap, bool tck, bool tms, { trace_tap_ctrl_rbb_step(tck, tms, tdi); - if (tap->trst) { + if (tap->trst || !tap->enabled) { return; } @@ -497,7 +498,8 @@ static void tap_ctrl_rbb_chr_receive(void *opaque, const uint8_t *buf, int size) for (unsigned ix = 0; ix < size; ix++) { if (tap_ctrl_rbb_read_byte(tap, buf[ix])) { - uint8_t outbuf[1] = { '0' + (unsigned)tap->tdo }; + uint8_t outbuf[1] = { '0' + + (unsigned)(tap->enabled ? tap->tdo : true) }; qemu_chr_fe_write_all(&tap->chr, outbuf, (int)sizeof(outbuf)); } } @@ -627,6 +629,7 @@ static const Property tap_ctrl_rbb_properties[] = { DEFINE_PROP_UINT8("ir_length", TapCtrlRbbState, ir_length, 0), DEFINE_PROP_UINT8("idcode_inst", TapCtrlRbbState, idcode_inst, 1u), DEFINE_PROP_BOOL("quit", TapCtrlRbbState, enable_quit, true), + DEFINE_PROP_BOOL("enabled", TapCtrlRbbState, enabled, true), DEFINE_PROP_CHR("chardev", TapCtrlRbbState, chr), }; @@ -680,12 +683,28 @@ static void tap_ctrl_rbb_realize(DeviceState *dev, Error **errp) qemu_chr_fe_accept_input(&tap->chr); } +static void tap_ctrl_rbb_enable_in(void *opaque, int n, int level) +{ + TapCtrlRbbState *tap = opaque; + (void)n; + bool enabled = (bool)level; + if (tap->enabled != enabled) { + tap->enabled = enabled; + if (!enabled) { + tap_ctrl_rbb_tap_reset(tap); + tap->tdo = true; + } + } +} + static void tap_ctrl_rbb_init(Object *obj) { TapCtrlRbbState *tap = TAP_CTRL_RBB(obj); tap->tdhtable = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, tap_ctrl_rbb_free_data_handler); + qdev_init_gpio_in_named(DEVICE(obj), &tap_ctrl_rbb_enable_in, + TAP_CTRL_RBB_ENABLE, 1); } static void tap_ctrl_rbb_class_init(ObjectClass *klass, const void *data) diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 36034aae9161b..9d0a14ccfc813 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -393,7 +393,8 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { .cfg = &ot_eg_soc_tap_ctrl_configure, .prop = IBEXDEVICEPROPDEFS( IBEX_DEV_UINT_PROP("ir_length", IBEX_TAP_IR_LENGTH), - IBEX_DEV_UINT_PROP("idcode", EG_RV_DM_TAP_IDCODE) + IBEX_DEV_UINT_PROP("idcode", EG_RV_DM_TAP_IDCODE), + IBEX_DEV_BOOL_PROP("enabled", false) ), }, [OT_EG_SOC_DEV_LC_CTRL_TAP_CTRL] = { diff --git a/include/hw/jtag/tap_ctrl_rbb.h b/include/hw/jtag/tap_ctrl_rbb.h index a08caaa4f0b3d..2c6b372252bba 100644 --- a/include/hw/jtag/tap_ctrl_rbb.h +++ b/include/hw/jtag/tap_ctrl_rbb.h @@ -32,4 +32,6 @@ #define TYPE_TAP_CTRL_RBB "tap-ctrl-rbb" OBJECT_DECLARE_SIMPLE_TYPE(TapCtrlRbbState, TAP_CTRL_RBB) +#define TAP_CTRL_RBB_ENABLE "enable" + #endif /* HW_JTAG_TAP_CTRL_RBB_H */ From afb67cfa81bc6aa2676c7a132ef681513f988bb3 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:30 +0000 Subject: [PATCH 12/52] [ot] hw/riscv: dm: align Debug Module and DTM with Earlgrey RTL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Align the RISC-V Debug Module (`hw/riscv/dm.c`, `include/hw/riscv/dm.h`, `include/hw/riscv/debug.h`) and JTAG Debug Transport Module (`hw/riscv/dtm.c`) with OpenTitan Earlgrey (`rv_dm.sv`, `dm_csrs.sv`, `dm_mem.sv`, `dm_sba.sv`, and `dmi_jtag.sv`): 1. Add `RISCV_DM_NDMRESET_LINE` (`"riscv-dm.ndmreset"`) and `in_ndmreset` tracking on `RISCVDebugDeviceState` so `dmcontrol.ndmreset` drives a level-sensitive non-debug system reset without wiping `RISCVDMState` or `RISCVDTMState` CSRs, setting `allhavereset` on reset exit and re-halting immediately if `haltreq` is held. 2. Update PULP debug ROM mailbox offsets (`RISCV_DM_GOING_OFFSET = 0x108`, `RISCV_DM_RESUMING_OFFSET = 0x110`, `RISCV_DM_EXCEPTION_OFFSET = 0x118`) to match `dm_mem.sv` 64-bit aligned slots. 3. Enforce `dm_sba.sv` System Bus Access (`sbcs`/`sbaddress0`/`sbdata0`) natural alignment checks (`SBERROR_ALIGNMENT`) and suppress auto-read when `sberror != 0`. 4. Execute DMI `read_rq`/`read_value` during JTAG `Update-DR` in `hw/riscv/dtm.c` and latch `(address, data, dmistat)` for the subsequent `Capture-DR` per RISC-V Debug Spec §6.1.5 and `dmi_jtag.sv`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ib986b19d946170181a0f0b9f5b1e24d96a6a6964 --- hw/riscv/dm.c | 152 ++++++++++++++++++++++++++++++++++----- hw/riscv/dtm.c | 53 ++++++-------- include/hw/riscv/debug.h | 1 + include/hw/riscv/dm.h | 9 +-- 4 files changed, 161 insertions(+), 54 deletions(-) diff --git a/hw/riscv/dm.c b/hw/riscv/dm.c index c8ef571ec6fdb..ea1a79374a79d 100644 --- a/hw/riscv/dm.c +++ b/hw/riscv/dm.c @@ -49,6 +49,8 @@ #include "qemu/osdep.h" #include "qemu/compiler.h" #include "qemu/log.h" +#include "qemu/main-loop.h" +#include "qemu/timer.h" #include "qemu/typedefs.h" #include "qapi/error.h" #include "accel/tcg/cpu-ldst.h" @@ -61,7 +63,9 @@ #include "hw/riscv/debug.h" #include "hw/riscv/dm.h" #include "hw/riscv/dtm.h" +#include "hw/riscv/ibex_irq.h" #include "system/hw_accel.h" +#include "system/replay.h" #include "system/runstate.h" #include "target/riscv/cpu.h" #include "trace.h" @@ -296,6 +300,7 @@ typedef struct RISCVDMHartState { target_ulong hartid; /** Hart ID */ bool halted; /* Hart has halted execution */ bool resumed; /* Hart has resumed execution */ + bool resuming; /* Hart has a pending resume request in Debug ROM */ bool have_reset; /* Hart has reset, not yet supported */ bool unlock_reset; /* Whether DM may reset CPU */ #ifdef TRACE_CPU_STATES @@ -344,6 +349,7 @@ struct RISCVDMState { MemTxAttrs mta_sba; /* MemTxAttrs to access system bus devices */ bool cmd_busy; /* A command is being executed */ bool dtm_ok; /* DTM is available */ + IbexIRQ ndmreset; /* config */ RISCVDTMState *dtm; @@ -554,6 +560,7 @@ static const char *RISCVDM_DM_PROGBUF_NAMES[16u] = { #undef MAKE_REG_ENTRY static const char *riscv_dm_get_reg_name(unsigned addr); +static void riscv_dm_reset_exit(Object *obj, ResetType type); /* -------------------------------------------------------------------------- */ /* DMI interface implementation */ @@ -963,6 +970,7 @@ static void riscv_dm_acknowledge(void *opaque, int irq, int level) hartnum = (unsigned)level; if ((hart = riscv_dm_get_hart_from_id(dm, hartnum))) { hart->halted = true; + hart->resuming = false; uint64_t hbm = 1u << hartnum; if (dm->unavailable_bm & hbm) { qemu_log("%s: %s: an unavailable hart should not be halted\n", @@ -1014,6 +1022,7 @@ static void riscv_dm_acknowledge(void *opaque, int irq, int level) } hart->halted = false; hart->resumed = true; + hart->resuming = false; uint64_t hbm = 1u << hartnum; if (dm->unavailable_bm & hbm) { qemu_log("%s: %s: an unavailable hart should not be resumed\n", @@ -1303,11 +1312,13 @@ static CmdErr riscv_dm_dmcontrol_write(RISCVDMState *dm, uint32_t value) if (dm->unavailable_bm & hbit) { if (!cs->disabled) { /* hart exited from reset, became available */ - dm->unavailable_bm &= ~hbit; hart->have_reset = true; - hart->halted = false; trace_riscv_dm_hart_reset(dm->soc, cs->cpu_index, dm->hart->hartid, "exited"); + if (dm->haltreq_bm & hbit) { + riscv_dm_halt_hart(dm, hartsel); + } + dm->unavailable_bm &= ~hbit; } } } @@ -1315,18 +1326,60 @@ static CmdErr riscv_dm_dmcontrol_write(RISCVDMState *dm, uint32_t value) if (value & R_DMCONTROL_ACKHAVERESET_MASK) { unsigned hix = 0; while (hix < dm->hart_count) { - dm->harts->have_reset = false; + dm->harts[hix].have_reset = false; hix++; } } } + if (!hasel && (hartsel < dm->hart_count)) { + uint64_t hartbit = 1u << hartsel; + if (value & R_DMCONTROL_HALTREQ_MASK) { + dm->haltreq_bm |= hartbit; + } else { + dm->haltreq_bm &= ~hartbit; + } + } RISCVDMCmdErr ret = CMD_ERR_NONE; + bool prev_ndmreset = + (bool)(dm->regs[A_DMCONTROL] & R_DMCONTROL_NDMRESET_MASK); + bool cur_ndmreset = (bool)(value & R_DMCONTROL_NDMRESET_MASK); - if (unlikely(value & R_DMCONTROL_NDMRESET_MASK)) { - /* full system reset (but the Debug Module) */ - qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); + if (cur_ndmreset) { + RISCV_DEBUG_DEVICE(dm)->in_ndmreset = true; + ibex_irq_set(&dm->ndmreset, 1); + for (unsigned ix = 0; ix < dm->hart_count; ix++) { + RISCVDMHartState *hart = &dm->harts[ix]; + CPUState *cs = CPU(hart->cpu); + cs->disabled = true; + cpu_pause(cs); + dm->unavailable_bm |= 1u << ix; + hart->halted = false; + hart->resumed = false; + hart->resuming = false; + } + } else if (prev_ndmreset) { + RISCV_DEBUG_DEVICE(dm)->in_ndmreset = true; + ibex_irq_set(&dm->ndmreset, 0); + for (unsigned step = 0; step < 32u; step++) { + bool any_disabled = false; + for (unsigned ix = 0; ix < dm->hart_count; ix++) { + CPUState *cs = CPU(dm->harts[ix].cpu); + if (cs && cs->disabled) { + any_disabled = true; + break; + } + } + if (!any_disabled) { + break; + } + qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL_RT); + qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); + aio_bh_poll(qemu_get_aio_context()); + } + riscv_dm_reset_exit(OBJECT(dm), RESET_TYPE_WAKEUP); + RISCV_DEBUG_DEVICE(dm)->in_ndmreset = false; } else if (dm->hart) { if (!hasel && (hartsel < dm->hart_count)) { uint64_t hartbit = 1u << hartsel; @@ -1335,8 +1388,6 @@ static CmdErr riscv_dm_dmcontrol_write(RISCVDMState *dm, uint32_t value) trace_riscv_dm_unavailable_hart_control(dm->soc, hartsel, "halt"); ret = CMD_ERR_HALT_RESUME; - /* flag the haltreq as pending while unavailable */ - dm->haltreq_bm |= hartbit; } else { riscv_dm_halt_hart(dm, hartsel); } @@ -1495,6 +1546,35 @@ static CmdErr riscv_dm_abstractauto_write(RISCVDMState *dm, uint32_t value) return CMD_ERR_NONE; } +static void riscv_dm_sync_vcpu(RISCVDMState *dm) +{ + for (int i = 0; i < 1000; i++) { + bool pending = dm->cmd_busy; + for (unsigned hix = 0; hix < dm->hart_count; hix++) { + RISCVDMHartState *hart = &dm->harts[hix]; + CPUState *cs = CPU(hart->cpu); + uint64_t mask = 1ULL << hix; + if (!cs->disabled && + (hart->resuming || + (!hart->halted && + ((cs->interrupt_request & CPU_INTERRUPT_DEBUG) || + (dm->haltreq_bm & mask))))) { + riscv_dm_ensure_running(dm); + qemu_cpu_kick(cs); + pending = true; + } + } + if (!pending) { + break; + } + replay_mutex_unlock(); + bql_unlock(); + g_usleep(50); + replay_mutex_lock(); + bql_lock(); + } +} + static CmdErr riscv_dm_dmstatus_read(RISCVDMState *dm, uint32_t *value) { unsigned halted = 0u; @@ -1522,6 +1602,10 @@ static CmdErr riscv_dm_dmstatus_read(RISCVDMState *dm, uint32_t *value) trace_riscv_dm_status(dm->soc, cs->cpu_index, "nonexistent"); continue; } + if (hart->have_reset) { + trace_riscv_dm_status(dm->soc, cs->cpu_index, "have reset"); + havereset += 1; + } /* * The hart may have been started since last poll. There is no way * for the hart to inform the DM in this case, so rely on polling @@ -1529,6 +1613,7 @@ static CmdErr riscv_dm_dmstatus_read(RISCVDMState *dm, uint32_t *value) */ if (cs->disabled) { hart->resumed = false; + hart->resuming = false; hart->halted = false; dm->unavailable_bm |= mask; dm->nonexistent_bm &= ~mask; @@ -1562,14 +1647,11 @@ static CmdErr riscv_dm_dmstatus_read(RISCVDMState *dm, uint32_t *value) riscv_dm_halt_hart(dm, hix); } } + riscv_dm_sync_vcpu(dm); if (hart->resumed) { resumeack += 1; trace_riscv_dm_status(dm->soc, cs->cpu_index, "resumed"); } - if (hart->have_reset) { - trace_riscv_dm_status(dm->soc, cs->cpu_index, "have reset"); - havereset += 1; - } if (hart->halted) { trace_riscv_dm_status(dm->soc, cs->cpu_index, "halted"); halted += 1; @@ -1986,6 +2068,8 @@ static CmdErr riscv_dm_hartinfo_read(RISCVDMState *dm, uint32_t *value) static CmdErr riscv_dm_abstractcs_read(RISCVDMState *dm, uint32_t *value) { + riscv_dm_sync_vcpu(dm); + uint32_t val; val = FIELD_DP32(0, ABSTRACTCS, DATACOUNT, dm->cfg.data_count); @@ -2278,6 +2362,8 @@ static CmdErr riscv_dm_access_register(RISCVDMState *dm, uint32_t value) return res; } + riscv_dm_ensure_running(dm); + return CMD_ERR_NONE; } @@ -2404,7 +2490,7 @@ static void riscv_dm_ensure_running(RISCVDMState *dm) return; } - RISCVCPU *cpu = dm->hart->cpu; + RISCVCPU *cpu = dm->hart ? dm->hart->cpu : dm->harts[0].cpu; CPUState *cs = CPU(cpu); cpu_synchronize_state(cs); @@ -2427,18 +2513,20 @@ static void riscv_dm_ensure_running(RISCVDMState *dm) static void riscv_dm_halt_hart(RISCVDMState *dm, unsigned hartsel) { - RISCVCPU *cpu = dm->harts[hartsel].cpu; + RISCVDMHartState *hart = &dm->harts[hartsel]; + RISCVCPU *cpu = hart->cpu; CPUState *cs = CPU(cpu); trace_riscv_dm_change_hart(dm->soc, "HALT", hartsel, cs->halted, - cs->running, cs->stopped, dm->hart->resumed); + cs->running, cs->stopped, hart->resumed); /* Note: NMI are not yet supported */ cpu_exit(cs); - dm->haltreq_bm &= ~(1u << hartsel); /* not sure if the real HW clear this flag on halt */ - dm->hart->resumed = false; - riscv_dm_set_cs(dm, true); + hart->resumed = false; + hart->resuming = false; + cpu->env.debug_cs = true; + trace_riscv_dm_cs(dm->soc, true); riscv_cpu_store_debug_cause(cs, DCSR_CAUSE_HALTREQ); cpu_interrupt(cs, CPU_INTERRUPT_DEBUG); /* vCPU should always be "running" - halt mode runs the park loop */ @@ -2485,6 +2573,8 @@ static void riscv_dm_resume_hart(RISCVDMState *dm, unsigned hartsel) if (riscv_dm_update_flags(dm, hartsel, true, R_FLAGS_FLAG_RESUME_MASK)) { xtrace_riscv_dm_error(dm->soc, "cannot resume"); + } else { + dm->harts[hartsel].resuming = true; } cpu_exit(cs); @@ -2570,6 +2660,10 @@ static void riscv_dm_reset_enter(Object *obj, ResetType type) RISCVDMClass *c = RISCV_DM_GET_CLASS(obj); RISCVDMState *dm = RISCV_DM(obj); + if (RISCV_DEBUG_DEVICE(dm)->in_ndmreset || (type == RESET_TYPE_WAKEUP)) { + return; + } + trace_riscv_dm_reset(dm->soc, "enter"); if (c->parent_phases.enter) { @@ -2609,6 +2703,7 @@ static void riscv_dm_reset_enter(Object *obj, ResetType type) RISCVCPU *cpu = hart->cpu; memset(hart, 0, sizeof(RISCVDMHartState)); hart->cpu = cpu; + hart->have_reset = true; } } @@ -2617,6 +2712,26 @@ static void riscv_dm_reset_exit(Object *obj, ResetType type) RISCVDMClass *c = RISCV_DM_GET_CLASS(obj); RISCVDMState *dm = RISCV_DM(obj); + if (RISCV_DEBUG_DEVICE(dm)->in_ndmreset || (type == RESET_TYPE_WAKEUP)) { + for (unsigned ix = 0; ix < dm->hart_count; ix++) { + RISCVDMHartState *hart = &dm->harts[ix]; + uint64_t hartbit = 1u << ix; + hart->have_reset = true; + hart->halted = false; + hart->resumed = false; + hart->resuming = false; + dm->unavailable_bm |= hartbit; + if (dm->haltreq_bm & hartbit) { + CPUState *cs = CPU(hart->cpu); + riscv_cpu_store_debug_cause(cs, DCSR_CAUSE_HALTREQ); + cpu_interrupt(cs, CPU_INTERRUPT_DEBUG); + riscv_dm_set_cs(dm, true); + riscv_dm_ensure_running(dm); + } + } + return; + } + if (c->parent_phases.exit) { c->parent_phases.exit(obj, type); } @@ -2739,6 +2854,7 @@ static void riscv_dm_realize(DeviceState *dev, Error **errp) qdev_init_gpio_in_named(dev, &riscv_dm_acknowledge, RISCV_DM_ACK_LINES, ACK_COUNT); + ibex_qdev_init_irq(OBJECT(dev), &dm->ndmreset, RISCV_DM_NDMRESET_LINE); dm->soc = object_get_canonical_path_component(OBJECT(dev)->parent); dm->unavailable_bm = (1u << dm->hart_count) - 1u; diff --git a/hw/riscv/dtm.c b/hw/riscv/dtm.c index 2259869a4aa78..2d5096cc8ccfb 100644 --- a/hw/riscv/dtm.c +++ b/hw/riscv/dtm.c @@ -103,6 +103,7 @@ struct RISCVDTMState { RISCVDebugModule *last_dm; /* last selected DM */ uint32_t address; /* last updated address */ + uint32_t data; /* last updated or read data */ RISCVDebugResult dmistat; /* Operation result */ bool cmd_busy; /* A command is being executed */ @@ -295,53 +296,31 @@ static void riscv_dtm_tap_dmi_capture(TapDataHandler *tdh) { RISCVDTMState *s = tdh->opaque; - uint32_t addr = s->address; - uint32_t value = 0; - - if (s->dmistat == RISCV_DEBUG_NOERR) { - unsigned op = (unsigned)(tdh->value & 0b11); - if (op == DMI_READ) { - RISCVDebugModule *dm = riscv_dtm_get_dm(s, addr); - if (!dm) { - s->dmistat = RISCV_DEBUG_FAILED; - value = 0; - qemu_log_mask(LOG_UNIMP, "%s: Unknown DM address 0x%x\n", - __func__, addr); - } else { - trace_riscv_dtm_tap_dmi_capture(addr); - value = dm->dc->read_value(dm->dev); - } - } - } - /* * In Capture-DR, the DTM updates data with the result from [the previous * update] operation, updating op if the current op isn’t sticky. */ - tdh->value = (((uint64_t)addr) << (32u + 2u)) | (((uint64_t)value) << 2u) | - ((uint64_t)(s->dmistat & 0b11)); + tdh->value = (((uint64_t)s->address) << (32u + 2u)) | + (((uint64_t)s->data) << 2u) | ((uint64_t)(s->dmistat & 0b11)); } static void riscv_dtm_tap_dmi_update(TapDataHandler *tdh) { RISCVDTMState *s = tdh->opaque; - uint32_t value; + uint32_t value = (uint32_t)FIELD_EX64(tdh->value, DMI, DATA); uint32_t addr = (uint32_t)extract64(tdh->value, R_DMI_ADDRESS_SHIFT, (int)s->abits); unsigned op = (unsigned)(tdh->value & 0b11); - if (op == DMI_IGNORE) { - /* - * Don’t send anything over the DMI during Update-DR. This operation - * should never result in a busy or error response. The address and data - * reported in the following Capture-DR are undefined. - */ - return; + if (s->dmistat == RISCV_DEBUG_NOERR) { + s->address = addr; + s->data = value; } - /* store address for next read back */ - s->address = addr; + if (op == DMI_IGNORE || s->dmistat != RISCV_DEBUG_NOERR) { + return; + } RISCVDebugModule *dm = riscv_dtm_get_dm(s, addr); if (!dm) { @@ -362,10 +341,12 @@ static void riscv_dtm_tap_dmi_update(TapDataHandler *tdh) case DMI_READ: trace_riscv_dtm_tap_dmi_update(addr, "read"); s->dmistat = dm->dc->read_rq(dm->dev, addr - dm->base); + if (s->dmistat == RISCV_DEBUG_NOERR) { + s->data = dm->dc->read_value(dm->dev); + } break; case DMI_WRITE: trace_riscv_dtm_tap_dmi_update(addr, "write"); - value = (uint32_t)FIELD_EX64(tdh->value, DMI, DATA); s->dmistat = dm->dc->write_rq(dm->dev, addr - dm->base, value); break; case DMI_RESERVED: @@ -525,11 +506,19 @@ static void riscv_dtm_reset_enter(Object *obj, ResetType type) RISCVDTMClass *c = RISCV_DTM_GET_CLASS(obj); RISCVDTMState *s = RISCV_DTM(obj); + RISCVDebugModule *dm; + QLIST_FOREACH(dm, &s->dms, entry) { + if (dm->dev && dm->dev->in_ndmreset) { + return; + } + } + if (c->parent_phases.enter) { c->parent_phases.enter(obj, type); } s->address = 0; + s->data = 0; s->last_dm = NULL; } diff --git a/include/hw/riscv/debug.h b/include/hw/riscv/debug.h index 67925c2bd87c4..c21bbc9221fc4 100644 --- a/include/hw/riscv/debug.h +++ b/include/hw/riscv/debug.h @@ -72,6 +72,7 @@ struct RISCVDebugDeviceClass { struct RISCVDebugDeviceState { DeviceState parent_obj; + bool in_ndmreset; }; #endif /* HW_RISCV_DEBUG_H */ diff --git a/include/hw/riscv/dm.h b/include/hw/riscv/dm.h index 845697fff5113..7063a314c0acf 100644 --- a/include/hw/riscv/dm.h +++ b/include/hw/riscv/dm.h @@ -33,16 +33,17 @@ #define TYPE_RISCV_DM "riscv-dm" OBJECT_DECLARE_TYPE(RISCVDMState, RISCVDMClass, RISCV_DM) -#define RISCV_DM_ACK_LINES TYPE_RISCV_DM ".ack" +#define RISCV_DM_ACK_LINES TYPE_RISCV_DM ".ack" +#define RISCV_DM_NDMRESET_LINE TYPE_RISCV_DM ".ndmreset" /* * Note: these offsets depends on the debug module implementation, so they * should be better defined as yet another configurable properties */ #define RISCV_DM_HALTED_OFFSET 0x100u -#define RISCV_DM_GOING_OFFSET 0x104u -#define RISCV_DM_RESUMING_OFFSET 0x108u -#define RISCV_DM_EXCEPTION_OFFSET 0x10cu +#define RISCV_DM_GOING_OFFSET 0x108u +#define RISCV_DM_RESUMING_OFFSET 0x110u +#define RISCV_DM_EXCEPTION_OFFSET 0x118u #define RISCV_DM_FLAGS_OFFSET 0x400u enum RISCVDMAckInterface { From 6e2371145c634356de1ef789c0999b7ea8cd7137 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:30 +0000 Subject: [PATCH 13/52] [ot] hw/misc: pulp_rv_dm: align debug_rom, late debug, and TL-UL fault Align the OpenTitan RV_DM (`pulp-rv-dm`) model and its Earlgrey SoC `otp-ctrl` link (`hw/riscv/ot_earlgrey.c`) with `rv_dm.sv`, `dm_mem.sv`, and `debug_rom.sv`: - Update `PULP_RV_DM_RESUME_OFFSET` (`0x08`) and `PULP_RV_DM_EXCEPTION_OFFSET` (`0x10`), populate the exact `debug_rom.sv` (`0x800..0x89c`) and `DEFAULT_ABSTRACT_CMD` (`0x338..0x35c`) opcodes, and reject CPU writes to Debug ROM with `MEMTX_DECODE_ERROR` (`dm_mem.sv:400`). - Implement `LATE_DEBUG_ENABLE_REGWEN` (`rw0c`) and `LATE_DEBUG_ENABLE` (`mubi32_t`) CSRs and `lc-hw-debug-en` / `lc-dft-en` GPIO inputs gated by OTP `CREATOR_SW_CFG_RV_DM_LATE_DEBUG_DIS` (`rv_dm.sv:238-243`), and wire `otp-ctrl` on `OT_EG_SOC_DEV_RV_DM` in `hw/riscv/ot_earlgrey.c`. - Enforce `RV_DM_REGS_PERMIT` and `dm_mem.sv` (`FullRegMask`/`OneBitMask`) sub-word write faults and unmapped hole (`0x0c`, `0x000`, `0x104`, `0x304..0x337`, `0x388..0x3ff`) decode errors (`MEMTX_DECODE_ERROR`). - Make `ALERT_TEST` (`0x00`) a transient 1-cycle pulse so consecutive writes of `1` each trigger `fatal_fault`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I7c0773c9335088a937090b077eebf7f46a6a6964 --- hw/misc/pulp_rv_dm.c | 334 ++++++++++++++++++++++++++++------- hw/riscv/ot_earlgrey.c | 4 + include/hw/misc/pulp_rv_dm.h | 10 +- 3 files changed, 284 insertions(+), 64 deletions(-) diff --git a/hw/misc/pulp_rv_dm.c b/hw/misc/pulp_rv_dm.c index 8350b75a4474c..70749d81ce750 100644 --- a/hw/misc/pulp_rv_dm.c +++ b/hw/misc/pulp_rv_dm.c @@ -31,6 +31,7 @@ #include "qemu/osdep.h" #include "qemu/bitmap.h" +#include "qemu/bswap.h" #include "qemu/guest-random.h" #include "qemu/log.h" #include "qemu/main-loop.h" @@ -45,6 +46,8 @@ #include "hw/loader.h" #include "hw/misc/pulp_rv_dm.h" #include "hw/opentitan/ot_alert.h" +#include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_otp_if.h" #include "hw/qdev-properties-system.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" @@ -71,6 +74,9 @@ /* MMIO Regs */ REG32(ALERT_TEST, 0x0u) FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u) +REG32(LATE_DEBUG_ENABLE_REGWEN, 0x4u) + FIELD(LATE_DEBUG_ENABLE_REGWEN, REGWEN, 0u, 1u) +REG32(LATE_DEBUG_ENABLE, 0x8u) /* MMIO Mem (Actions) */ REG32(HALTED, RISCV_DM_HALTED_OFFSET) @@ -111,12 +117,13 @@ REG32(FLAGS, RISCV_DM_FLAGS_OFFSET) #define R32_OFF(_r_) ((_r_) / sizeof(uint32_t)) #define MEM_OFF(_r_) ((_r_) - R_WHERETO) -#define PULP_RV_DM_DMACT_BASE (A_HALTED) -#define PULP_RV_DM_DMACT_SIZE (A_EXCEPTION - A_HALTED + sizeof(uint32_t)) -#define PULP_RV_DM_PROG_BASE (A_WHERETO) -#define PULP_RV_DM_PROG_SIZE 0x100u -#define PULP_RV_DM_DMFLAG_BASE (A_FLAGS) -#define PULP_RV_DM_DMFLAG_SIZE (PULP_RV_DM_FLAGS_COUNT * sizeof(uint32_t)) +#define PULP_RV_DM_DMACT_BASE (A_HALTED) +#define PULP_RV_DM_DMACT_SIZE (A_EXCEPTION - A_HALTED + sizeof(uint32_t)) +#define PULP_RV_DM_PROG_BASE (A_WHERETO) +#define PULP_RV_DM_PROG_SIZE 0x100u +#define PULP_RV_DM_DMFLAG_BASE (A_FLAGS) +#define PULP_RV_DM_DMFLAG_SIZE (PULP_RV_DM_FLAGS_COUNT * sizeof(uint32_t)) +#define PULP_RV_DM_DMFLAG_REGION_SIZE 0x400u /* * Type definitions @@ -127,6 +134,8 @@ struct PulpRVDMState { MemoryRegion regs; /* MMIO */ MemoryRegion mem; /* Container for the following: */ + MemoryRegion unmapped_prog0; /* Unmapped 0x304..0x337 within prog */ + MemoryRegion unmapped_prog1; /* Unmapped 0x388..0x3ff within prog */ MemoryRegion dmact; /* MMIO */ MemoryRegion prog; /* ROM device */ MemoryRegion dmflag; /* MMIO */ @@ -136,9 +145,16 @@ struct PulpRVDMState { IbexIRQ alert; uint32_t dmflag_regs[PULP_RV_DM_DMFLAG_SIZE / sizeof(uint32_t)]; + uint32_t late_debug_enable_regwen; + uint32_t late_debug_enable; + bool lc_hw_debug_en; + bool lc_dft_en; unsigned hart_count; uint64_t idle_bm; + uint64_t in_debug_bm; + + OtOTPIf *otp_ctrl; }; #ifdef DISCARD_REPEATED_IO_TRACES @@ -163,6 +179,23 @@ typedef struct { #define PULP_RV_DM_MEM_WORDS \ ((R_FLAGS_0 + PULP_RV_DM_FLAGS_COUNT) - R_WHERETO + sizeof(uint32_t)) +/** + * Default Abstract Command ROM contents at reset (dm_mem.sv p_abstract_cmd_rom + * when cmd_i == 0 and HasSndScratch == 1). + */ +static const uint32_t DEFAULT_ABSTRACT_CMD[PULP_RV_DM_ABSTRACTCMD_COUNT] = { + /* 338 */ 0x00000000u, /* illegal */ + /* 33c */ 0x00000517u, /* auipc a0,0x0 */ + /* 340 */ 0x00c55513u, /* srli a0,a0,0xc */ + /* 344 */ 0x00c51513u, /* slli a0,a0,0xc */ + /* 348 */ 0x00000013u, /* nop */ + /* 34c */ 0x00000013u, /* nop */ + /* 350 */ 0x00000013u, /* nop */ + /* 354 */ 0x00000013u, /* nop */ + /* 358 */ 0x7b302573u, /* csrr a0,dscratch1 */ + /* 35c */ 0x00100073u, /* ebreak */ +}; + /** * Debug ROM blob for 2 debug scratch registers. * @@ -175,50 +208,53 @@ typedef struct { static const uint32_t DEBUG_ROM[] = { /* clang-format off */ /* entry: HALT_OFFSET */ - /* 800 */ 0x00c0006fu, /* j 80c <_entry> */ + /* 800 */ 0x0180006fu, /* j 818 <_entry> */ + /* 804 */ 0x00000013u, /* nop */ /* resume: RESUME_OFFSET */ - /* 804 */ 0x07c0006fu, /* j 880 <_resume> */ - /* exception: EXCEPTION */ - /* 808 */ 0x04c0006fu, /* j 854 <_exception> */ - /*_entry: */ - /* 80c */ 0x0ff0000fu, /* fence */ - /* 810 */ 0x7b241073u, /* csrw dscratch0,s0 */ - /* 814 */ 0x7b351073u, /* csrw dscratch1,a0 */ - /* 818 */ 0x00000517u, /* auipc a0,0x0 */ - /* 81c */ 0x00c55513u, /* srl a0,a0,0xc */ - /* 820 */ 0x00c51513u, /* sll a0,a0,0xc */ + /* 808 */ 0x0840006fu, /* j 88c <_resume> */ + /* 80c */ 0x00000013u, /* nop */ + /* exception: EXCEPTION_OFFSET */ + /* 810 */ 0x0500006fu, /* j 860 <_exception> */ + /* 814 */ 0x00000013u, /* nop */ + /* _entry: */ + /* 818 */ 0x0ff0000fu, /* fence */ + /* 81c */ 0x7b241073u, /* csrw dscratch0,s0 */ + /* 820 */ 0x7b351073u, /* csrw dscratch1,a0 */ + /* 824 */ 0x00000517u, /* auipc a0,0x0 */ + /* 828 */ 0x00c55513u, /* srli a0,a0,0xc */ + /* 82c */ 0x00c51513u, /* slli a0,a0,0xc */ /* entry_loop: */ - /* 824 */ 0xf1402473u, /* csrr s0,mhartid */ - /* 828 */ 0x10852023u, /* sw s0,256(a0) # HALTED */ - /* 82c */ 0x00a40433u, /* add s0,s0,a0 */ - /* 830 */ 0x40044403u, /* lbu s0,1024(s0) # FLAGS */ - /* 834 */ 0x00147413u, /* and s0,s0,1 */ - /* 838 */ 0x02041c63u, /* bnez s0,870 */ - /* 83c */ 0xf1402473u, /* csrr s0,mhartid */ - /* 840 */ 0x00a40433u, /* add s0,s0,a0 */ - /* 844 */ 0x40044403u, /* lbu s0,1024(s0) # FLAGS */ - /* 848 */ 0x00247413u, /* and s0,s0,2 */ - /* 84c */ 0xfa041ce3u, /* bnez s0,804 */ - /* 850 */ 0xfd5ff06fu, /* j 824 */ + /* 830 */ 0xf1402473u, /* csrr s0,mhartid */ + /* 834 */ 0x10852023u, /* sw s0,256(a0) # HALTED */ + /* 838 */ 0x00a40433u, /* add s0,s0,a0 */ + /* 83c */ 0x40044403u, /* lbu s0,1024(s0) # FLAGS */ + /* 840 */ 0x00147413u, /* andi s0,s0,1 */ + /* 844 */ 0x02041c63u, /* bnez s0,87c */ + /* 848 */ 0xf1402473u, /* csrr s0,mhartid */ + /* 84c */ 0x00a40433u, /* add s0,s0,a0 */ + /* 850 */ 0x40044403u, /* lbu s0,1024(s0) # FLAGS */ + /* 854 */ 0x00247413u, /* andi s0,s0,2 */ + /* 858 */ 0xfa0418e3u, /* bnez s0,808 */ + /* 85c */ 0xfd5ff06fu, /* j 830 */ /* _exception: */ - /* 854 */ 0x00000517u, /* auipc a0,0x0 */ - /* 858 */ 0x00c55513u, /* srl a0,a0,0xc */ - /* 85c */ 0x00c51513u, /* sll a0,a0,0xc */ - /* 860 */ 0x10052623u, /* sw zero,268(a0) # EXCEPTION */ - /* 864 */ 0x7b302573u, /* csrr a0,dscratch1 */ - /* 868 */ 0x7b202473u, /* csrr s0,dscratch0 */ - /* 86c */ 0x00100073u, /* ebreak */ + /* 860 */ 0x00000517u, /* auipc a0,0x0 */ + /* 864 */ 0x00c55513u, /* srli a0,a0,0xc */ + /* 868 */ 0x00c51513u, /* slli a0,a0,0xc */ + /* 86c */ 0x10052c23u, /* sw zero,280(a0) # EXCEPTION */ + /* 870 */ 0x7b302573u, /* csrr a0,dscratch1 */ + /* 874 */ 0x7b202473u, /* csrr s0,dscratch0 */ + /* 878 */ 0x00100073u, /* ebreak */ /* going: */ - /* 870 */ 0x10052223u, /* sw zero,260(a0) # GOING */ - /* 874 */ 0x7b302573u, /* csrr a0,dscratch1 */ - /* 878 */ 0x7b202473u, /* csrr s0,dscratch0 */ - /* 87c */ 0xa85ff06fu, /* j 300 # WHERETO */ - /* _resume: */ - /* 880 */ 0xf1402473u, /* csrr s0,mhartid */ - /* 884 */ 0x10852423u, /* sw s0,264(a0) # RESUMING */ - /* 888 */ 0x7b302573u, /* csrr a0,dscratch1 */ - /* 88c */ 0x7b202473u, /* csrr s0,dscratch0 */ - /* 890 */ 0x7b200073u, /* dret */ + /* 87c */ 0x10052423u, /* sw zero,264(a0) # GOING */ + /* 880 */ 0x7b302573u, /* csrr a0,dscratch1 */ + /* 884 */ 0x7b202473u, /* csrr s0,dscratch0 */ + /* 888 */ 0xa79ff06fu, /* j 300 # WHERETO */ + /* _resume: */ + /* 88c */ 0xf1402473u, /* csrr s0,mhartid */ + /* 890 */ 0x10852823u, /* sw s0,272(a0) # RESUMING */ + /* 894 */ 0x7b302573u, /* csrr a0,dscratch1 */ + /* 898 */ 0x7b202473u, /* csrr s0,dscratch0 */ + /* 89c */ 0x7b200073u, /* dret */ /* clang-format on */ }; @@ -226,6 +262,23 @@ static const uint32_t DEBUG_ROM[] = { * Device implementation */ +static void pulp_rv_dm_update_debug_en(PulpRVDMState *s) +{ + bool otp_dis_late_debug = false; + if (s->otp_ctrl) { + OtOTPIfClass *oc = OT_OTP_IF_GET_CLASS(s->otp_ctrl); + const OtOTPHWCfg *hw_cfg = oc->get_hw_cfg(s->otp_ctrl); + if (hw_cfg) { + otp_dis_late_debug = + (hw_cfg->dis_rv_dm_late_debug_mb8 == OT_MULTIBITBOOL8_TRUE); + } + } + bool late_debug_en = (s->late_debug_enable == OT_MULTIBITBOOL32_TRUE); + bool en = (otp_dis_late_debug || late_debug_en) ? s->lc_hw_debug_en : + s->lc_dft_en; + memory_region_set_enabled(&s->mem, en); +} + static void pulp_rv_dm_load_rom(PulpRVDMState *s) { /* do not use rom_add_blob_fixed_as as absolute address is not yet known */ @@ -235,19 +288,35 @@ static void pulp_rv_dm_load_rom(PulpRVDMState *s) /* linter may not know error_fatal never returns */ abort(); } + memset(rom, 0, PULP_RV_DM_ROM_SIZE); memcpy(rom, DEBUG_ROM, sizeof(DEBUG_ROM)); } /* NOLINTNEXTLINE */ static uint64_t pulp_rv_dm_regs_read(void *opaque, hwaddr addr, unsigned size) { - (void)opaque; + PulpRVDMState *s = opaque; + uint32_t val32 = 0u; (void)size; - /* the unique register is W/O */ - qemu_log_mask(LOG_GUEST_ERROR, "%s: W/O register 0x%" HWADDR_PRIx "\n", - __func__, addr); - return 0u; + switch (R32_OFF(addr)) { + case R_ALERT_TEST: + qemu_log_mask(LOG_GUEST_ERROR, "%s: W/O register 0x%" HWADDR_PRIx "\n", + __func__, addr); + break; + case R_LATE_DEBUG_ENABLE_REGWEN: + val32 = s->late_debug_enable_regwen; + break; + case R_LATE_DEBUG_ENABLE: + val32 = s->late_debug_enable; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", + __func__, addr); + break; + } + + return val32; }; static void pulp_rv_dm_regs_write(void *opaque, hwaddr addr, uint64_t val64, @@ -260,7 +329,23 @@ static void pulp_rv_dm_regs_write(void *opaque, hwaddr addr, uint64_t val64, switch (R32_OFF(addr)) { case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; - ibex_irq_set(&s->alert, (int)(bool)val32); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } + break; + case R_LATE_DEBUG_ENABLE_REGWEN: + s->late_debug_enable_regwen &= + val32 & R_LATE_DEBUG_ENABLE_REGWEN_REGWEN_MASK; + break; + case R_LATE_DEBUG_ENABLE: + if (s->late_debug_enable_regwen) { + s->late_debug_enable = val32; + pulp_rv_dm_update_debug_en(s); + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: LATE_DEBUG_ENABLE is locked\n", + __func__); + } break; default: qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", @@ -269,6 +354,19 @@ static void pulp_rv_dm_regs_write(void *opaque, hwaddr addr, uint64_t val64, } }; +#define RV_DM_REGS_COUNT 3u + +static bool pulp_rv_dm_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t permit = (reg == R_LATE_DEBUG_ENABLE) ? 0xfu : 0x1u; + uint8_t byte_mask = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return reg < RV_DM_REGS_COUNT && (!is_write || (permit & ~byte_mask) == 0u); +} + static MemTxResult pulp_rv_dm_dmact_read_with_attrs( void *opaque, hwaddr addr, uint64_t *val64, unsigned size, MemTxAttrs attrs) { @@ -342,6 +440,7 @@ static MemTxResult pulp_rv_dm_dmact_write_with_attrs( switch (R32_OFF(addr)) { case R_HALTED: if (val32 < s->hart_count) { + s->in_debug_bm |= (1u << val32); if (!(s->idle_bm & (1u << val32))) { /* * use a local cache to avoid flooding the DM with the park loop @@ -349,6 +448,16 @@ static MemTxResult pulp_rv_dm_dmact_write_with_attrs( */ qemu_set_irq(s->ack_out[ACK_HALTED], (int)val32); s->idle_bm |= (1u << val32); + } else if (!s->dmflag_regs[val32] && current_cpu) { + /* + * The hart is already halted in the Debug ROM park loop and no + * GO/RESUME flag is pending. Park the vCPU so it becomes idle + * (allowing the icount warp timer to advance QEMU_CLOCK_VIRTUAL + * and leaving BQL/replay_mutex uncontended for JTAG and + * VIRTUAL_RT timers) until a DM flag or reset wakes it. + */ + current_cpu->stopped = true; + cpu_exit(current_cpu); } } res = MEMTX_OK; @@ -361,6 +470,7 @@ static MemTxResult pulp_rv_dm_dmact_write_with_attrs( case R_RESUMING: if (val32 < s->hart_count) { s->idle_bm &= ~(1u << val32); + s->in_debug_bm &= ~(1u << val32); qemu_set_irq(s->ack_out[ACK_RESUMING], (int)val32); } res = MEMTX_OK; @@ -379,6 +489,40 @@ static MemTxResult pulp_rv_dm_dmact_write_with_attrs( return res; }; +static bool pulp_rv_dm_prog_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)attrs; + return opaque != NULL && (!is_write || ((addr & 3u) == 0u && size == 4u)); +} + +static MemTxResult pulp_rv_dm_prog_write_with_attrs( + void *opaque, hwaddr addr, uint64_t val64, unsigned size, MemTxAttrs attrs) +{ + PulpRVDMState *s = opaque; + + hwaddr abs_addr = addr + PULP_RV_DM_PROG_BASE; + bool is_dm = + (!attrs.unspecified) && (attrs.requester_id == PULP_RV_DM_REQUESTER_ID); + bool is_data = + (abs_addr >= A_DATAADDR_0) && + (abs_addr < (A_DATAADDR_0 + PULP_RV_DM_DATA_COUNT * sizeof(uint32_t))); + bool in_debug = (s->in_debug_bm != 0u); + + /* + * In dm_csrs.sv:627-647, data_q is held at '0 whenever + * !dmcontrol_q.dmactive. Thus CPU writes to DATA0/DATA1 only latch into + * data_q when the DM is active (e.g., during Debug Mode execution). + */ + if (is_dm || (is_data && in_debug)) { + uint8_t *ram = memory_region_get_ram_ptr(&s->prog); + stl_le_p(ram + addr, (uint32_t)val64); + memory_region_set_dirty(&s->prog, addr, size); + } + + return MEMTX_OK; +} + static MemTxResult pulp_rv_dm_dmflag_read_with_attrs( void *opaque, hwaddr addr, uint64_t *val64, unsigned size, MemTxAttrs attrs) { @@ -399,7 +543,7 @@ static MemTxResult pulp_rv_dm_dmflag_read_with_attrs( res = MEMTX_OK; break; default: - res = MEMTX_DECODE_ERROR; + res = MEMTX_OK; val32 = 0; qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr); @@ -432,6 +576,10 @@ static MemTxResult pulp_rv_dm_dmflag_write_with_attrs( (attrs.requester_id == PULP_RV_DM_REQUESTER_ID)) { /* dm_access */ s->dmflag_regs[reg - R_FLAGS_0] = val32; + if (val32 && first_cpu && first_cpu->stopped && + !first_cpu->disabled) { + cpu_resume(first_cpu); + } } else { /* other (hart...) access */ qemu_log_mask(LOG_GUEST_ERROR, @@ -441,7 +589,7 @@ static MemTxResult pulp_rv_dm_dmflag_write_with_attrs( res = MEMTX_OK; break; default: - res = MEMTX_DECODE_ERROR; + res = MEMTX_OK; qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr); break; @@ -456,14 +604,33 @@ static const MemoryRegionOps pulp_rv_dm_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4, .impl.max_access_size = 4, + .valid.accepts = &pulp_rv_dm_regs_accepts, }; +static bool pulp_rv_dm_dmact_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)size; + (void)attrs; + return !is_write || (addr & 3u) == 0u; +} + static const MemoryRegionOps pulp_rv_dm_dmact_ops = { .read_with_attrs = &pulp_rv_dm_dmact_read_with_attrs, .write_with_attrs = &pulp_rv_dm_dmact_write_with_attrs, .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4, .impl.max_access_size = 4, + .valid.accepts = &pulp_rv_dm_dmact_accepts, +}; + +static const MemoryRegionOps pulp_rv_dm_prog_ops = { + .write_with_attrs = &pulp_rv_dm_prog_write_with_attrs, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl.min_access_size = 4, + .impl.max_access_size = 4, + .valid.accepts = &pulp_rv_dm_prog_accepts, }; static const MemoryRegionOps pulp_rv_dm_dmflag_ops = { @@ -472,18 +639,42 @@ static const MemoryRegionOps pulp_rv_dm_dmflag_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4, .impl.max_access_size = 4, + .valid.accepts = &pulp_rv_dm_prog_accepts, }; +static void pulp_rv_dm_lc_hw_debug_en_in(void *opaque, int n, int level) +{ + PulpRVDMState *s = opaque; + (void)n; + s->lc_hw_debug_en = (bool)level; + pulp_rv_dm_update_debug_en(s); +} + +static void pulp_rv_dm_lc_dft_en_in(void *opaque, int n, int level) +{ + PulpRVDMState *s = opaque; + (void)n; + s->lc_dft_en = (bool)level; + pulp_rv_dm_update_debug_en(s); +} + static void pulp_rv_dm_reset(DeviceState *dev) { PulpRVDMState *s = PULP_RV_DM(dev); ibex_irq_set(&s->alert, false); - memset(memory_region_get_ram_ptr(&s->prog), 0, PULP_RV_DM_PROG_SIZE); + uint8_t *prog_ptr = memory_region_get_ram_ptr(&s->prog); + memset(prog_ptr, 0, PULP_RV_DM_PROG_SIZE); + memcpy(prog_ptr + (A_ABSTRACTCMD_0 - PULP_RV_DM_PROG_BASE), + DEFAULT_ABSTRACT_CMD, sizeof(DEFAULT_ABSTRACT_CMD)); memset(s->dmflag_regs, 0, sizeof(s->dmflag_regs)); + s->late_debug_enable_regwen = 1u; + s->late_debug_enable = OT_MULTIBITBOOL32_FALSE; + pulp_rv_dm_update_debug_en(s); s->idle_bm = 0; + s->in_debug_bm = 0; } static void pulp_rv_dm_init(Object *obj) @@ -493,6 +684,8 @@ static void pulp_rv_dm_init(Object *obj) MachineState *ms = MACHINE(qdev_get_machine()); unsigned max_cpus = ms->smp.max_cpus; s->hart_count = MIN(max_cpus, 64u); + s->lc_hw_debug_en = true; + s->lc_dft_en = true; /* Top-level container */ memory_region_init(&s->mem, obj, TYPE_PULP_RV_DM, 0x1000); @@ -508,34 +701,55 @@ static void pulp_rv_dm_init(Object *obj) TYPE_PULP_RV_DM ".act", PULP_RV_DM_DMACT_SIZE); memory_region_add_subregion(&s->mem, PULP_RV_DM_DMACT_BASE, &s->dmact); - memory_region_init_ram_nomigrate(&s->prog, obj, TYPE_PULP_RV_DM ".prog", - PULP_RV_DM_PROG_SIZE, &error_fatal); + memory_region_init_rom_device_nomigrate(&s->prog, obj, &pulp_rv_dm_prog_ops, + s, TYPE_PULP_RV_DM ".prog", + PULP_RV_DM_PROG_SIZE, &error_fatal); memory_region_add_subregion(&s->mem, PULP_RV_DM_PROG_BASE, &s->prog); + memory_region_init_io(&s->unmapped_prog0, obj, &pulp_rv_dm_prog_ops, NULL, + TYPE_PULP_RV_DM ".unmapped_prog0", 0x34); + memory_region_add_subregion_overlap(&s->mem, 0x304, &s->unmapped_prog0, 1); + + memory_region_init_io(&s->unmapped_prog1, obj, &pulp_rv_dm_prog_ops, NULL, + TYPE_PULP_RV_DM ".unmapped_prog1", 0x78); + memory_region_add_subregion_overlap(&s->mem, 0x388, &s->unmapped_prog1, 1); + memory_region_init_io(&s->dmflag, obj, &pulp_rv_dm_dmflag_ops, s, - TYPE_PULP_RV_DM ".flag", PULP_RV_DM_DMFLAG_SIZE); + TYPE_PULP_RV_DM ".flag", + PULP_RV_DM_DMFLAG_REGION_SIZE); memory_region_add_subregion(&s->mem, PULP_RV_DM_DMFLAG_BASE, &s->dmflag); s->dmflag.disable_reentrancy_guard = true; - memory_region_init_rom_nomigrate(&s->rom, obj, TYPE_PULP_RV_DM ".rom", - PULP_RV_DM_ROM_SIZE, &error_abort); + memory_region_init_rom_device_nomigrate(&s->rom, obj, &pulp_rv_dm_prog_ops, + NULL, TYPE_PULP_RV_DM ".rom", + PULP_RV_DM_ROM_SIZE, &error_abort); memory_region_add_subregion(&s->mem, PULP_RV_DM_ROM_BASE, &s->rom); s->ack_out = g_new0(qemu_irq, ACK_COUNT); qdev_init_gpio_out_named(DEVICE(obj), s->ack_out, PULP_RV_DM_ACK_OUT_LINES, ACK_COUNT); + qdev_init_gpio_in_named(DEVICE(obj), &pulp_rv_dm_lc_hw_debug_en_in, + PULP_RV_DM_LC_HW_DEBUG_EN, 1); + qdev_init_gpio_in_named(DEVICE(obj), &pulp_rv_dm_lc_dft_en_in, + PULP_RV_DM_LC_DFT_EN, 1); pulp_rv_dm_load_rom(s); ibex_qdev_init_irq(obj, &s->alert, OT_DEVICE_ALERT); } +static const Property pulp_rv_dm_properties[] = { + DEFINE_PROP_LINK("otp-ctrl", PulpRVDMState, otp_ctrl, TYPE_OT_OTP_IF, + OtOTPIf *), +}; + static void pulp_rv_dm_class_init(ObjectClass *klass, const void *data) { DeviceClass *dc = DEVICE_CLASS(klass); (void)data; device_class_set_legacy_reset(dc, &pulp_rv_dm_reset); + device_class_set_props(dc, pulp_rv_dm_properties); set_bit(DEVICE_CATEGORY_MISC, dc->categories); } diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 9d0a14ccfc813..d11bace5db869 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -426,6 +426,10 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { [OT_EG_SOC_DEV_DM] = { .type = TYPE_RISCV_DM, .cfg = &ot_eg_soc_dm_configure, + .gpio = IBEXGPIOCONNDEFS( + OT_EG_SOC_SIGNAL(RISCV_DM_NDMRESET_LINE, 0, PWRMGR, + OT_PWRMGR_NDM_RST, 0) + ), .link = IBEXDEVICELINKDEFS( OT_EG_SOC_DEVLINK("dtm", DTM) ), diff --git a/include/hw/misc/pulp_rv_dm.h b/include/hw/misc/pulp_rv_dm.h index c833efba4502f..058d0288f9d89 100644 --- a/include/hw/misc/pulp_rv_dm.h +++ b/include/hw/misc/pulp_rv_dm.h @@ -35,7 +35,9 @@ #define TYPE_PULP_RV_DM "pulp-rv-dm" OBJECT_DECLARE_SIMPLE_TYPE(PulpRVDMState, PULP_RV_DM) -#define PULP_RV_DM_ACK_OUT_LINES TYPE_PULP_RV_DM ".ack-out" +#define PULP_RV_DM_ACK_OUT_LINES TYPE_PULP_RV_DM ".ack-out" +#define PULP_RV_DM_LC_HW_DEBUG_EN TYPE_PULP_RV_DM ".lc-hw-debug-en" +#define PULP_RV_DM_LC_DFT_EN TYPE_PULP_RV_DM ".lc-dft-en" /* Configuration */ #define PULP_RV_DM_NSCRATCH_COUNT 2u @@ -49,12 +51,12 @@ OBJECT_DECLARE_SIMPLE_TYPE(PulpRVDMState, PULP_RV_DM) /* ROM entry points */ #define PULP_RV_DM_HALT_OFFSET 0x0000 -#define PULP_RV_DM_RESUME_OFFSET 0x0004 -#define PULP_RV_DM_EXCEPTION_OFFSET 0x0008 +#define PULP_RV_DM_RESUME_OFFSET 0x0008 +#define PULP_RV_DM_EXCEPTION_OFFSET 0x0010 /* Memory regions */ #define PULP_RV_DM_REGS_BASE 0x0u -#define PULP_RV_DM_REGS_SIZE sizeof(uint32_t) +#define PULP_RV_DM_REGS_SIZE 0x10u #define PULP_RV_DM_MEM_BASE 0x100u #define PULP_RV_DM_MEM_SIZE 0x700u #define PULP_RV_DM_ROM_BASE 0x800u From 7ce8f6bd64c98ed4ce5010dc749ec2600adc4eff Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:31 +0000 Subject: [PATCH 14/52] [ot] hw/opentitan: plic_ext: fix alert pulse, MSIP reset, and permit Align the OpenTitan RV_PLIC extension device (hw/opentitan/ot_plic_ext.c) with hw/ip/rv_plic/rtl/rv_plic.sv and rv_plic_reg_top.sv: - Pulse the fatal_fault alert line (1 -> 0) on writes to ALERT_TEST instead of latching the alert output high, allowing alert_handler W1C acknowledgement. - Add ot_plic_ext_accepts() to both the MSIP and ALERT_TEST MMIO regions to reject unaligned sub-word writes ((addr & 3) != 0), matching RV_PLIC_PERMIT = 4'b0001 in rv_plic_reg_pkg.sv. - Clear s->msip_regs to 0 in ot_plic_ext_reset_enter() so MSIP0 resets cleanly across warm resets. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I0e9a2fb7cb3a58b1edd14aab8e08cb3c6a6a6964 --- hw/opentitan/ot_plic_ext.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/hw/opentitan/ot_plic_ext.c b/hw/opentitan/ot_plic_ext.c index 3b15ce494209b..ef401a7416442 100644 --- a/hw/opentitan/ot_plic_ext.c +++ b/hw/opentitan/ot_plic_ext.c @@ -199,7 +199,10 @@ static void ot_plic_ext_alert_write(void *opaque, hwaddr addr, uint64_t val64, switch (reg) { case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; - ibex_irq_set(&s->alert, (int)(bool)val32); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; default: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Bad offset 0x%01x\n", __func__, @@ -208,6 +211,15 @@ static void ot_plic_ext_alert_write(void *opaque, hwaddr addr, uint64_t val64, } } +static bool ot_plic_ext_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)size; + (void)attrs; + return !is_write || (addr & 3u) == 0u; +} + static const Property ot_plic_ext_properties[] = { DEFINE_PROP_STRING(OT_COMMON_DEV_ID, OtPlicExtState, ot_id), }; @@ -216,6 +228,7 @@ static const MemoryRegionOps ot_plic_ext_msip_ops = { .read = &ot_plic_ext_msip_read, .write = &ot_plic_ext_msip_write, .endianness = DEVICE_NATIVE_ENDIAN, + .valid.accepts = &ot_plic_ext_accepts, .impl.min_access_size = 4u, .impl.max_access_size = 4u, }; @@ -224,6 +237,7 @@ static const MemoryRegionOps ot_plic_ext_alert_ops = { .read = &ot_plic_ext_alert_read, .write = &ot_plic_ext_alert_write, .endianness = DEVICE_NATIVE_ENDIAN, + .valid.accepts = &ot_plic_ext_accepts, .impl.min_access_size = 4u, .impl.max_access_size = 4u, }; @@ -237,6 +251,7 @@ static void ot_plic_ext_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } + memset(s->msip_regs, 0, sizeof(s->msip_regs)); ibex_irq_set(&s->irq, 0); ibex_irq_set(&s->alert, 0); } From 5a12325c6ec8049bf6d676d80546666ef32097e1 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:31 +0000 Subject: [PATCH 15/52] [ot] hw/block: m25p80: add SFDP table and quad/4-byte SPI flash ops Extend the QEMU `m25p80` SPI NOR flash device model (`hw/block/m25p80.c`, `hw/block/m25p80_sfdp.c`, `hw/block/m25p80_sfdp.h`) to support OpenTitan Earlgrey SPI flash PMOD and bootstrap tests: - Add JEDEC SFDP (`RDSFDP = 0x5a`) table responders for `w25q256` (Winbond), `gd25q256` / `gd25b512` (GigaDevice), and `mx66l1g` (Macronix) flash parts. - Support Quad Enable (`QE`), 4-byte address mode (`EN4B`/`EX4B`), extended status/config register reads/writes (`RDSR2`, `RDSR3`, `WRSR2`, `WRSR3`, `RDCR`), and soft reset (`RSTEN = 0x66`, `RST = 0x99`). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I385be32d6cb7e2df4851e9d0931d7c8a6a6a6964 --- hw/block/m25p80.c | 30 +++++++-- hw/block/m25p80_sfdp.c | 146 ++++++++++++++++++++++++++++++++++++++++- hw/block/m25p80_sfdp.h | 5 ++ 3 files changed, 174 insertions(+), 7 deletions(-) diff --git a/hw/block/m25p80.c b/hw/block/m25p80.c index d5acd2319f8f8..a7170ab08803c 100644 --- a/hw/block/m25p80.c +++ b/hw/block/m25p80.c @@ -207,6 +207,10 @@ static const FlashPartInfo known_devices[] = { /* GigaDevice */ { INFO("gd25q32", 0xc84016, 0, 64 << 10, 64, ER_4K) }, { INFO("gd25q64", 0xc84017, 0, 64 << 10, 128, ER_4K) }, + { INFO("gd25q256", 0xc84019, 0, 64 << 10, 512, ER_4K | ER_32K), + .sfdp_read = m25p80_sfdp_gd25q256 }, + { INFO("gd25b01g", 0xc8401b, 0, 64 << 10, 2048, ER_4K | ER_32K), + .sfdp_read = m25p80_sfdp_gd25b01g }, /* Intel/Numonyx -- xxxs33b */ { INFO("160s33b", 0x898911, 0, 64 << 10, 32, 0) }, @@ -226,7 +230,7 @@ static const FlashPartInfo known_devices[] = { { INFO("is25wp064", 0x9d7017, 0, 64 << 10, 128, ER_4K) }, { INFO("is25wp128", 0x9d7018, 0, 64 << 10, 256, ER_4K), .sfdp_read = m25p80_sfdp_is25wp128 }, - { INFO("is25wp256", 0x9d7019, 0, 64 << 10, 512, ER_4K), + { INFO("is25wp256", 0x9d7019, 0, 64 << 10, 512, ER_4K | ER_32K), .sfdp_read = m25p80_sfdp_is25wp256 }, /* Macronix */ @@ -237,6 +241,8 @@ static const FlashPartInfo known_devices[] = { { INFO("mx25l3205d", 0xc22016, 0, 64 << 10, 64, 0) }, { INFO("mx25l6405d", 0xc22017, 0, 64 << 10, 128, 0) }, { INFO("mx25l12805d", 0xc22018, 0, 64 << 10, 256, 0) }, + { INFO("mx25l12835f", 0xc22018, 0, 64 << 10, 256, ER_4K | ER_32K), + .sfdp_read = m25p80_sfdp_mx25l12835f }, { INFO("mx25l12855e", 0xc22618, 0, 64 << 10, 256, 0) }, { INFO6("mx25l25635e", 0xc22019, 0xc22019, 64 << 10, 512, ER_4K | ER_32K), .sfdp_read = m25p80_sfdp_mx25l25635e }, @@ -267,7 +273,8 @@ static const FlashPartInfo known_devices[] = { .sfdp_read = m25p80_sfdp_n25q256a }, { INFO("n25q512a", 0x20ba20, 0, 64 << 10, 1024, ER_4K) }, { INFO("n25q512ax3", 0x20ba20, 0x1000, 64 << 10, 1024, ER_4K) }, - { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K) }, + { INFO("mt25ql512ab", 0x20ba20, 0x1044, 64 << 10, 1024, ER_4K | ER_32K), + .sfdp_read = m25p80_sfdp_mt25ql512ab }, { INFO_STACKED("mt35xu01g", 0x2c5b1b, 0x104100, 128 << 10, 1024, ER_4K | ER_32K, 2), .sfdp_read = m25p80_sfdp_mt35xu01g }, @@ -363,7 +370,7 @@ static const FlashPartInfo known_devices[] = { .sfdp_read = m25p80_sfdp_w25q256 }, { INFO("w25q512jv", 0xef4020, 0, 64 << 10, 1024, ER_4K), .sfdp_read = m25p80_sfdp_w25q512jv }, - { INFO("w25q01jvq", 0xef4021, 0, 64 << 10, 2048, ER_4K), + { INFO("w25q01jvq", 0xef4021, 0, 64 << 10, 2048, ER_4K | ER_32K), .sfdp_read = m25p80_sfdp_w25q01jvq }, { INFO("w25q512nw", 0xef6020, 0, 64 << 10, 1024, ER_4K | ER_32K), .sfdp_read = m25p80_sfdp_w25q512nw }, @@ -407,6 +414,7 @@ typedef enum { QPP = 0x32, QPP_4 = 0x34, PP_4 = 0x38, + QPP_GD = 0xc2, RDID_90 = 0x90, RDID_AB = 0xab, AAI_WP = 0xad, @@ -546,6 +554,7 @@ static inline Manufacturer get_man(Flash *s) case 0x20: return MAN_NUMONYX; case 0xEF: + case 0xC8: return MAN_WINBOND; case 0x01: return MAN_SPANSION; @@ -768,6 +777,7 @@ static void complete_collecting_data(Flash *s) case PP4: case PP4_4: case PP_4: + case QPP_GD: s->state = STATE_PAGE_PROGRAM; break; case AAI_WP: @@ -815,6 +825,7 @@ static void complete_collecting_data(Flash *s) s->quad_enable = !!(s->data[1] & 0x02); break; case MAN_ISSI: + case MAN_NUMONYX: s->quad_enable = extract32(s->data[0], 6, 1); break; case MAN_MACRONIX: @@ -1011,7 +1022,7 @@ static void decode_fast_read_cmd(Flash *s) s->needed_bytes += 1; break; case MAN_NUMONYX: - s->needed_bytes += numonyx_extract_cfg_num_dummies(s); + s->needed_bytes += numonyx_extract_cfg_num_dummies(s) / 8; break; case MAN_MACRONIX: s->needed_bytes += 1; @@ -1211,6 +1222,7 @@ static void decode_new_cmd(Flash *s, uint32_t value) case QPP: case QPP_4: case PP4_4: + case QPP_GD: if (get_man(s) != MAN_NUMONYX || numonyx_mode(s) != MODE_DIO) { s->needed_bytes = get_addr_length(s); s->pos = 0; @@ -1347,7 +1359,8 @@ static void decode_new_cmd(Flash *s, uint32_t value) s->data[0] |= (!!s->block_protect3) << 6; } - if (get_man(s) == MAN_MACRONIX || get_man(s) == MAN_ISSI) { + if (get_man(s) == MAN_MACRONIX || get_man(s) == MAN_ISSI || + get_man(s) == MAN_NUMONYX) { s->data[0] |= (!!s->quad_enable) << 6; } if (get_man(s) == MAN_SST) { @@ -1355,7 +1368,12 @@ static void decode_new_cmd(Flash *s, uint32_t value) } s->pos = 0; - s->len = 1; + if (get_man(s) == MAN_WINBOND || get_man(s) == MAN_SPANSION) { + s->data[1] = (!!s->quad_enable) << 1; + s->len = 2; + } else { + s->len = 1; + } s->data_read_loop = true; s->state = STATE_READING_DATA; break; diff --git a/hw/block/m25p80_sfdp.c b/hw/block/m25p80_sfdp.c index 90e69576d42f0..e163d441c5b0f 100644 --- a/hw/block/m25p80_sfdp.c +++ b/hw/block/m25p80_sfdp.c @@ -523,7 +523,7 @@ static const uint8_t sfdp_is25wp256[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xe5, 0x20, 0xf9, 0xff, 0xff, 0xff, 0xff, 0x0f, + 0xe5, 0x20, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x44, 0xeb, 0x08, 0x6b, 0x08, 0x3b, 0x80, 0xbb, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x44, 0xeb, 0x0c, 0x20, 0x0f, 0x52, @@ -551,3 +551,147 @@ static const uint8_t sfdp_is25wp256[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; define_sfdp_read(is25wp256); + +static const uint8_t sfdp_mx25l12835f[256] = { + 0x53, 0x46, 0x44, 0x50, 0x06, 0x01, 0x01, 0xff, + 0x00, 0x06, 0x01, 0x10, 0x30, 0x00, 0x00, 0xff, + 0xc2, 0x00, 0x01, 0x04, 0xb0, 0x00, 0x00, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xe5, 0x20, 0xf9, 0xff, 0xff, 0xff, 0xff, 0x07, + 0x44, 0xeb, 0x08, 0x6b, 0x08, 0x3b, 0x04, 0xbb, + 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, + 0xff, 0xff, 0x44, 0xeb, 0x0c, 0x20, 0x0f, 0x52, + 0x10, 0xd8, 0x00, 0xff, 0xd6, 0x49, 0xc5, 0x00, + 0x85, 0xdf, 0x04, 0xe3, 0x44, 0x03, 0x67, 0x38, + 0x30, 0xb0, 0x30, 0xb0, 0xf7, 0xbd, 0xd5, 0x5c, + 0x4a, 0x9e, 0x29, 0xff, 0xf0, 0x50, 0xf9, 0x85, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xc2, 0xf5, 0x08, 0x0a, 0x08, 0x04, 0x03, 0x06, + 0x00, 0x00, 0x07, 0x29, 0x17, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +}; +define_sfdp_read(mx25l12835f); + +static const uint8_t sfdp_mt25ql512ab[256] = { + 0x53, 0x46, 0x44, 0x50, 0x06, 0x01, 0x00, 0xff, + 0x00, 0x06, 0x01, 0x10, 0x30, 0x00, 0x00, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xe5, 0x20, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x1f, + 0x44, 0xeb, 0x08, 0x6b, 0x08, 0x3b, 0x27, 0xbb, + 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x27, 0xbb, + 0xff, 0xff, 0x44, 0xeb, 0x0c, 0x20, 0x0f, 0x52, + 0x10, 0xd8, 0x00, 0x00, 0x24, 0x5a, 0x99, 0x00, + 0x8b, 0x8e, 0x03, 0xe1, 0xac, 0x01, 0x27, 0x38, + 0x7a, 0x75, 0x7a, 0x75, 0xfb, 0xbd, 0xd5, 0x5c, + 0x4a, 0x9e, 0x29, 0xff, 0x81, 0xb0, 0x38, 0x36, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +}; +define_sfdp_read(mt25ql512ab); + +static const uint8_t sfdp_gd25q256[256] = { + 0x53, 0x46, 0x44, 0x50, 0x06, 0x01, 0x00, 0xff, + 0x00, 0x06, 0x01, 0x10, 0x30, 0x00, 0x00, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xe5, 0x20, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x0f, + 0x44, 0xeb, 0x08, 0x6b, 0x08, 0x3b, 0x42, 0xbb, + 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, + 0xff, 0xff, 0x40, 0xeb, 0x0c, 0x20, 0x0f, 0x52, + 0x10, 0xd8, 0x00, 0x00, 0x36, 0x02, 0xa6, 0x00, + 0x82, 0xea, 0x14, 0xe2, 0xe9, 0x63, 0x76, 0x33, + 0x7a, 0x75, 0x7a, 0x75, 0xf7, 0xa2, 0xd5, 0x5c, + 0x19, 0xf7, 0x4d, 0xff, 0xe9, 0x70, 0xf9, 0xa5, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +}; +define_sfdp_read(gd25q256); + +static const uint8_t sfdp_gd25b01g[256] = { + 0x53, 0x46, 0x44, 0x50, 0x06, 0x01, 0x00, 0xff, + 0x00, 0x06, 0x01, 0x10, 0x30, 0x00, 0x00, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xe5, 0x20, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x3f, + 0x44, 0xeb, 0x08, 0x6b, 0x08, 0x3b, 0x42, 0xbb, + 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, + 0xff, 0xff, 0x40, 0xeb, 0x0c, 0x20, 0x0f, 0x52, + 0x10, 0xd8, 0x00, 0x00, 0x36, 0x02, 0xa6, 0x00, + 0x82, 0xea, 0x14, 0xe2, 0xe9, 0x63, 0x76, 0x33, + 0x7a, 0x75, 0x7a, 0x75, 0xf7, 0xa2, 0xd5, 0x5c, + 0x19, 0xf7, 0x4d, 0xff, 0xe9, 0x70, 0xf9, 0xa5, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +}; +define_sfdp_read(gd25b01g); diff --git a/hw/block/m25p80_sfdp.h b/hw/block/m25p80_sfdp.h index 33f560bb9cc6e..dfe7213cbfe2d 100644 --- a/hw/block/m25p80_sfdp.h +++ b/hw/block/m25p80_sfdp.h @@ -16,13 +16,18 @@ #define M25P80_SFDP_MAX_SIZE (1 << 24) uint8_t m25p80_sfdp_n25q256a(uint32_t addr); +uint8_t m25p80_sfdp_mt25ql512ab(uint32_t addr); uint8_t m25p80_sfdp_mt35xu01g(uint32_t addr); uint8_t m25p80_sfdp_mt35xu02g(uint32_t addr); +uint8_t m25p80_sfdp_mx25l12835f(uint32_t addr); uint8_t m25p80_sfdp_mx25l25635e(uint32_t addr); uint8_t m25p80_sfdp_mx25l25635f(uint32_t addr); uint8_t m25p80_sfdp_mx66l1g45g(uint32_t addr); +uint8_t m25p80_sfdp_gd25q256(uint32_t addr); +uint8_t m25p80_sfdp_gd25b01g(uint32_t addr); + uint8_t m25p80_sfdp_w25q256(uint32_t addr); uint8_t m25p80_sfdp_w25q512jv(uint32_t addr); uint8_t m25p80_sfdp_w25q80bl(uint32_t addr); From 1eaa801c4846954bb53b3bdf9ffa6ac5c0f777f8 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:31 +0000 Subject: [PATCH 16/52] [ot] hw/opentitan: otp_eg: align DAI/SW_CFG locks, prim_tl, and PERMIT Align the OpenTitan Earlgrey OTP controller (`ot-otp-eg`, `ot-otp-engine`, and `ot-otp_ot_be`) and its `0x40138000` `prim_tl` SoC MMIO mapping (`hw/riscv/ot_earlgrey.c`) with `otp_ctrl.sv`, `otp_ctrl_dai.sv`, and `otp_ctrl_prim_reg_top.sv`: - Derive `STATUS` error bits `[12:0]` dynamically from `ERR_CODE_0..12`, zero `DIRECT_ACCESS_RDATA_0/1` at DAI operation start and while busy, and set `INTR_OTP_OPERATION_DONE` upon DAI completion. - Reject `SW_CFG_WINDOW` writes in `ot_otp_eg_swcfg_accepts` (`ErrOnWrite = 1`) without raising `OTP_ERROR`, remove the `SW_CFG_WINDOW` digest read-lock bypass (`otp_ctrl_part_unbuf.sv:248`), and restrict the DAI read-lock digest exception to `hw_digest` partitions (`otp_ctrl_dai.sv:315`). - Enforce `u_tlul_lc_gate` (`lc_dft_en`) and `CSR0..CSR6` field masks in `ot_otp_ot_be.c`, map `OT_EG_SOC_DEV_OTP_BACKEND` at `0x40138000` in `hw/riscv/ot_earlgrey.c`, and enforce `OTP_CTRL_CORE_PERMIT` and `OTP_CTRL_PRIM_PERMIT` sub-word write access faults. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ie81e4f9a83c39630474d835c6b9926106a6a6964 --- hw/opentitan/ot_otp_eg.c | 81 +++++++++++++++++++++++++++++++----- hw/opentitan/ot_otp_engine.c | 67 +++++++++++++++++++++++------ hw/opentitan/ot_otp_ot_be.c | 80 ++++++++++++++++++++++++++++++++++- hw/riscv/ot_earlgrey.c | 4 +- 4 files changed, 204 insertions(+), 28 deletions(-) diff --git a/hw/opentitan/ot_otp_eg.c b/hw/opentitan/ot_otp_eg.c index 1412bc6d9c67a..bf799d6389dd7 100644 --- a/hw/opentitan/ot_otp_eg.c +++ b/hw/opentitan/ot_otp_eg.c @@ -492,14 +492,54 @@ static const char *OTP_TOKEN_NAMES[] = { static uint32_t ot_otp_eg_get_status(const OtOTPEngineState *s) { - uint32_t status; + uint32_t status = s->regs[R_STATUS] & ~0x1fffu; - status = FIELD_DP32(s->regs[R_STATUS], STATUS, DAI_IDLE, - !ot_otp_engine_dai_is_busy(s)); + for (unsigned ix = 0; ix <= (R_ERR_CODE_12 - R_ERR_CODE_0); ix++) { + if (s->regs[R_ERR_CODE_0 + ix] != 0u) { + status |= 1u << ix; + } + } + + status = + FIELD_DP32(status, STATUS, DAI_IDLE, !ot_otp_engine_dai_is_busy(s)); return status; } +static bool ot_otp_eg_reg_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + hwaddr reg = R32_OFF(addr); + uint8_t permit; + switch (reg) { + /* OTP_CTRL_CORE_PERMIT (otp_ctrl_core_reg_pkg.sv): */ + /* - DIRECT_ACCESS_ADDRESS: 4'b0011 (0x3) */ + /* - STATUS: 4'b0111 (0x7) */ + /* - 32-bit WDATA/RDATA: 4'b1111 (0xf) */ + /* - 32-bit CHECK_PERIOD: 4'b1111 (0xf) */ + /* - 32-bit DIGEST CSRs: 4'b1111 (0xf) */ + case R_DIRECT_ACCESS_WDATA_0 ... R_DIRECT_ACCESS_RDATA_1: + case R_CHECK_TIMEOUT ... R_CONSISTENCY_CHECK_PERIOD: + case R_VENDOR_TEST_DIGEST_0 ... R_SECRET2_DIGEST_1: + permit = 0xfu; + break; + default: + permit = (reg == R_DIRECT_ACCESS_ADDRESS) ? + 0x3u : + ((reg == R_STATUS) ? + 0x7u : + ((reg <= R_SECRET2_DIGEST_1) ? 0x1u : 0x0u)); + break; + } + uint32_t byte_mask = ((1u << size) - 1u) << (addr & 3u); + return (permit != 0u) && ((permit & ~byte_mask) == 0u); +} + static uint64_t ot_otp_eg_reg_read(void *opaque, hwaddr addr, unsigned size) { OtOTPEngineState *s = OT_OTP_ENGINE(opaque); @@ -514,14 +554,16 @@ static uint64_t ot_otp_eg_reg_read(void *opaque, hwaddr addr, unsigned size) case R_ERR_CODE_0 ... R_ERR_CODE_12: case R_DIRECT_ACCESS_WDATA_0: case R_DIRECT_ACCESS_WDATA_1: - case R_DIRECT_ACCESS_RDATA_0: - case R_DIRECT_ACCESS_RDATA_1: case R_DIRECT_ACCESS_ADDRESS: case R_VENDOR_TEST_READ_LOCK ... R_ROT_CREATOR_AUTH_STATE_READ_LOCK: case R_CHECK_TRIGGER_REGWEN: case R_CHECK_REGWEN: val32 = s->regs[reg]; break; + case R_DIRECT_ACCESS_RDATA_0: + case R_DIRECT_ACCESS_RDATA_1: + val32 = ot_otp_engine_dai_is_busy(s) ? 0u : s->regs[reg]; + break; case R_STATUS: val32 = ot_otp_eg_get_status(s); break; @@ -593,7 +635,8 @@ static void ot_otp_eg_reg_write(void *opaque, hwaddr addr, uint64_t value, case R_DIRECT_ACCESS_WDATA_1: case R_VENDOR_TEST_READ_LOCK ... R_ROT_CREATOR_AUTH_STATE_READ_LOCK: if (!(s->regs[R_DIRECT_ACCESS_REGWEN] & - R_DIRECT_ACCESS_REGWEN_REGWEN_MASK)) { + R_DIRECT_ACCESS_REGWEN_REGWEN_MASK) || + ot_otp_engine_dai_is_busy(s)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: %s is not enabled, %s is protected\n", __func__, s->ot_id, REG_NAME(R_DIRECT_ACCESS_REGWEN), @@ -647,7 +690,7 @@ static void ot_otp_eg_reg_write(void *opaque, hwaddr addr, uint64_t value, break; case R_INTR_TEST: val32 &= INTR_WMASK; - s->regs[R_INTR_STATE] = val32; + s->regs[R_INTR_STATE] |= val32; c->update_irqs(s); break; case R_ALERT_TEST: @@ -688,10 +731,14 @@ static void ot_otp_eg_reg_write(void *opaque, hwaddr addr, uint64_t value, val32 &= R_CHECK_REGWEN_REGWEN_MASK; s->regs[reg] &= val32; /* RW0C */ break; - case R_CHECK_TRIGGER: case R_CHECK_TIMEOUT: case R_INTEGRITY_CHECK_PERIOD: case R_CONSISTENCY_CHECK_PERIOD: + s->regs[reg] = val32; + qemu_log_mask(LOG_UNIMP, "%s: %s: %s is not supported\n", __func__, + s->ot_id, REG_NAME(reg)); + break; + case R_CHECK_TRIGGER: qemu_log_mask(LOG_UNIMP, "%s: %s: %s is not supported\n", __func__, s->ot_id, REG_NAME(reg)); break; @@ -876,10 +923,9 @@ static MemTxResult ot_otp_eg_swcfg_read_with_attrs( } bool is_readable = c->is_readable(s, part_ix); - bool is_digest = ot_otp_engine_is_part_digest_offset(s, part_ix, addr); bool is_zer = ot_otp_engine_is_part_zer_offset(s, part_ix, addr); - if (!is_readable && !(is_digest || is_zer)) { + if (!is_readable && !is_zer) { trace_ot_otp_access_error_on(s->ot_id, partition, addr, "not readable"); c->set_error(s, part_ix, OT_OTP_ACCESS_ERROR); @@ -900,6 +946,16 @@ static MemTxResult ot_otp_eg_swcfg_read_with_attrs( return MEMTX_OK; } +static bool ot_otp_eg_swcfg_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)addr; + (void)size; + (void)attrs; + return !is_write; +} + static void ot_otp_eg_get_lc_info( const OtOTPIf *dev, uint16_t *lc_tcount, uint16_t *lc_state, uint8_t *lc_valid, uint8_t *secret_valid, const OtOTPTokens **tokens) @@ -1027,6 +1083,9 @@ static void ot_otp_eg_pwr_load_hw_cfg(OtOTPEngineState *s) hw_cfg->en_csrng_sw_app_read_mb8 = s->blk ? pdata1[A_HW_CFG1_EN_CSRNG_SW_APP_READ - pdesc1->offset] : OT_MULTIBITBOOL8_TRUE; + hw_cfg->dis_rv_dm_late_debug_mb8 = + s->blk ? pdata1[A_HW_CFG1_DIS_RV_DM_LATE_DEBUG - pdesc1->offset] : + OT_MULTIBITBOOL8_FALSE; } static void ot_otp_eg_pwr_load_tokens(OtOTPEngineState *s) @@ -1095,6 +1154,7 @@ static const MemoryRegionOps ot_otp_eg_reg_ops = { .read = &ot_otp_eg_reg_read, .write = &ot_otp_eg_reg_write, .endianness = DEVICE_NATIVE_ENDIAN, + .valid.accepts = &ot_otp_eg_reg_accepts, .impl.min_access_size = 4, .impl.max_access_size = 4, }; @@ -1102,6 +1162,7 @@ static const MemoryRegionOps ot_otp_eg_reg_ops = { static const MemoryRegionOps ot_otp_eg_swcfg_ops = { .read_with_attrs = &ot_otp_eg_swcfg_read_with_attrs, .endianness = DEVICE_NATIVE_ENDIAN, + .valid.accepts = &ot_otp_eg_swcfg_accepts, .impl.min_access_size = 4, .impl.max_access_size = 4, }; diff --git a/hw/opentitan/ot_otp_engine.c b/hw/opentitan/ot_otp_engine.c index 7ae0657af0ca4..a3e65f5670f7d 100644 --- a/hw/opentitan/ot_otp_engine.c +++ b/hw/opentitan/ot_otp_engine.c @@ -319,7 +319,9 @@ static void ot_otp_engine_set_error(OtOTPEngineState *s, unsigned part_ix, case OT_OTP_MACRO_WRITE_BLANK_ERROR: break; case OT_OTP_ACCESS_ERROR: - ic->update_status_error(OT_OTP_IMPL_IF(s), OT_OTP_STATUS_DAI, true); + if (part_ix == OTP_ENTRY_DAI(s)) { + ic->update_status_error(OT_OTP_IMPL_IF(s), OT_OTP_STATUS_DAI, true); + } break; case OT_OTP_CHECK_FAIL_ERROR: case OT_OTP_FSM_STATE_ERROR: @@ -346,8 +348,7 @@ ot_otp_engine_get_part_from_address(const OtOTPEngineState *s, hwaddr addr) { for (unsigned part_ix = 0; part_ix < s->part_count; part_ix++) { const OtOTPPartDesc *part = &s->part_descs[part_ix]; - if ((addr >= part->offset) && - ((addr + sizeof(uint32_t)) <= (part->offset + part->size))) { + if ((addr >= part->offset) && (addr < (part->offset + part->size))) { trace_ot_otp_addr_to_part(s->ot_id, (uint32_t)addr, ot_otp_engine_part_name(s, part_ix), part_ix); @@ -639,7 +640,13 @@ static bool ot_otp_engine_is_readable(const OtOTPEngineState *s, const OtOTPPartController *pctrl = &s->part_ctrls[part_ix]; if (pdesc->secret) { - /* secret partitions are only readable if digest is not yet set. */ + /* + * secret partitions are only readable if digest is not yet set and + * HW read lock (e.g. CREATOR_SEED_SW_RW_EN) is not asserted. + */ + if (pdesc->read_lock && pctrl->read_lock) { + return false; + } return pctrl->digest == 0u; } @@ -659,7 +666,7 @@ static bool ot_otp_engine_is_readable(const OtOTPEngineState *s, if (pix == part_ix) { break; } - if (pdesc->read_lock_csr) { + if (s->part_descs[pix].read_lock_csr) { roffset++; } } @@ -702,6 +709,8 @@ static void ot_otp_engine_lci_change_state_line(OtOTPEngineState *s, s->lci->state = state; } +static void ot_otp_engine_lc_broadcast_bh(void *opaque); + static void ot_otp_engine_lc_broadcast_recv(void *opaque, int n, int level) { OtOTPEngineState *s = opaque; @@ -721,8 +730,7 @@ static void ot_otp_engine_lc_broadcast_recv(void *opaque, int n, int level) bcast->level &= ~bit; } - /* use a BH to decouple IRQ signaling from actual handling */ - qemu_bh_schedule(s->lc_broadcast.bh); + ot_otp_engine_lc_broadcast_bh(s); } static void ot_otp_engine_lc_broadcast_bh(void *opaque) @@ -744,8 +752,12 @@ static void ot_otp_engine_lc_broadcast_bh(void *opaque) switch ((int)sig) { case OT_OTP_LC_DFT_EN: - qemu_log_mask(LOG_UNIMP, "%s: %s: DFT feature not supported\n", - __func__, s->ot_id); + if (s->otp_backend) { + OtOtpBeIfClass *bec = OT_OTP_BE_IF_GET_CLASS(s->otp_backend); + if (bec->set_lc_dft_en) { + bec->set_lc_dft_en(s->otp_backend, level); + } + } break; case OT_OTP_LC_ESCALATE_EN: if (level) { @@ -1004,7 +1016,9 @@ static void ot_otp_engine_dai_set_error(OtOTPEngineState *s, OtOTPError err) DAI_CHANGE_STATE(s, OT_OTP_DAI_ERROR); break; default: + s->regs[R_INTR_STATE] |= INTR_OTP_OPERATION_DONE_MASK; DAI_CHANGE_STATE(s, OT_OTP_DAI_IDLE); + ot_otp_engine_update_irqs(s); break; } } @@ -1029,6 +1043,8 @@ static void ot_otp_engine_dai_read(OtOTPEngineState *s) ot_otp_engine_dai_clear_error(s); DAI_CHANGE_STATE(s, OT_OTP_DAI_READ); + DIRECT_ACCESS_REG(s, RDATA_0) = 0u; + DIRECT_ACCESS_REG(s, RDATA_1) = 0u; unsigned address = DIRECT_ACCESS_REG(s, ADDRESS); @@ -1063,10 +1079,11 @@ static void ot_otp_engine_dai_read(OtOTPEngineState *s) bool is_buffered = ot_otp_engine_is_buffered(s, part_ix); bool is_secret = ot_otp_engine_is_secret(s, part_ix); bool is_digest = ot_otp_engine_is_part_digest_offset(s, part_ix, address); + bool is_hw_digest = s->part_descs[part_ix].hw_digest && is_digest; bool is_zer = ot_otp_engine_is_part_zer_offset(s, part_ix, address); - /* in all partitions, the digest and zer fields are always readable. */ - if (!is_digest && !is_zer && !is_readable) { + /* HW digest and zer fields remain readable even when read_lock is set. */ + if (!is_hw_digest && !is_zer && !is_readable) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: partition %s @ 0x%04x not readable\n", __func__, s->ot_id, ot_otp_engine_part_name(s, part_ix), address); @@ -1170,7 +1187,9 @@ static void ot_otp_engine_dai_read(OtOTPEngineState *s) timer_mod(s->dai->delay, qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + access_time); } else { + s->regs[R_INTR_STATE] |= INTR_OTP_OPERATION_DONE_MASK; DAI_CHANGE_STATE(s, OT_OTP_DAI_IDLE); + ot_otp_engine_update_irqs(s); } } @@ -1326,6 +1345,9 @@ static void ot_otp_engine_dai_write(OtOTPEngineState *s) return; } + DIRECT_ACCESS_REG(s, RDATA_0) = 0u; + DIRECT_ACCESS_REG(s, RDATA_1) = 0u; + if (!ot_otp_engine_is_backend_writable(s)) { /* OTP backend missing or read-only; reject any write request */ qemu_log_mask(LOG_GUEST_ERROR, @@ -1441,6 +1463,9 @@ static void ot_otp_engine_dai_digest(OtOTPEngineState *s) return; } + DIRECT_ACCESS_REG(s, RDATA_0) = 0u; + DIRECT_ACCESS_REG(s, RDATA_1) = 0u; + if (!ot_otp_engine_is_backend_writable(s)) { /* OTP backend missing or read-only; reject any write request */ qemu_log_mask(LOG_GUEST_ERROR, @@ -1615,14 +1640,17 @@ static void ot_otp_engine_dai_complete(void *opaque) (unsigned)s->dai->partition, DIRECT_ACCESS_REG(s, RDATA_0), DIRECT_ACCESS_REG(s, RDATA_1)); + s->regs[R_INTR_STATE] |= INTR_OTP_OPERATION_DONE_MASK; s->dai->partition = -1; DAI_CHANGE_STATE(s, OT_OTP_DAI_IDLE); + ot_otp_engine_update_irqs(s); break; case OT_OTP_DAI_WRITE_WAIT: g_assert(s->dai->partition >= 0); s->regs[R_INTR_STATE] |= INTR_OTP_OPERATION_DONE_MASK; s->dai->partition = -1; DAI_CHANGE_STATE(s, OT_OTP_DAI_IDLE); + ot_otp_engine_update_irqs(s); break; case OT_OTP_DAI_DIG_WAIT: g_assert(s->dai->partition >= 0); @@ -1659,9 +1687,9 @@ static void ot_otp_engine_request_entropy(void *opaque) * can only be performed once the reset sequence is over. */ if (!s->keygen->edn_sched) { + s->keygen->edn_sched = true; int rc = ot_edn_request_entropy(s->edn, s->edn_ep); g_assert(rc == 0); - s->keygen->edn_sched = true; } } @@ -2166,6 +2194,10 @@ static void ot_otp_engine_pwr_load(OtOTPEngineState *s) uintptr_t base = (uintptr_t)otp->storage; g_assert(!(base & (sizeof(uint64_t) - 1u))); + if (s->blk && !blk_is_available(s->blk)) { + return; + } + memset(otp->storage, 0, otp_size); otp->data = (uint32_t *)(base + sizeof(struct otp_header)); @@ -2782,6 +2814,11 @@ static void ot_otp_engine_reset_enter(Object *obj, ResetType type) ibex_irq_set(&s->pwc_otp_rsp, 0); for (unsigned part_ix = 0; part_ix < s->part_count; part_ix++) { + s->part_ctrls[part_ix].digest = 0; + s->part_ctrls[part_ix].locked = false; + s->part_ctrls[part_ix].failed = false; + s->part_ctrls[part_ix].read_lock = false; + s->part_ctrls[part_ix].write_lock = false; /* @todo initialize with actual default partition data once known */ if (s->part_descs[part_ix].buffered) { s->part_ctrls[part_ix].state.b = OT_OTP_BUF_IDLE; @@ -2791,7 +2828,6 @@ static void ot_otp_engine_reset_enter(Object *obj, ResetType type) } unsigned part_size = ot_otp_engine_part_data_byte_size(s, part_ix); memset(s->part_ctrls[part_ix].buffer.data, 0, part_size); - s->part_ctrls[part_ix].digest = 0; if (s->part_descs[part_ix].iskeymgr_creator || s->part_descs[part_ix].iskeymgr_owner) { s->part_ctrls[part_ix].read_lock = true; @@ -2799,7 +2835,12 @@ static void ot_otp_engine_reset_enter(Object *obj, ResetType type) } } DAI_CHANGE_STATE(s, OT_OTP_DAI_RESET); + s->dai->partition = -1; LCI_CHANGE_STATE(s, OT_OTP_LCI_RESET); + s->lci->error = OT_OTP_NO_ERROR; + s->lci->ack_fn = NULL; + s->lci->ack_data = NULL; + s->lci->hpos = 0u; } static void ot_otp_engine_reset_exit(Object *obj, ResetType type) diff --git a/hw/opentitan/ot_otp_ot_be.c b/hw/opentitan/ot_otp_ot_be.c index 3940d363b8ebe..1bfc839cf3ef7 100644 --- a/hw/opentitan/ot_otp_ot_be.c +++ b/hw/opentitan/ot_otp_ot_be.c @@ -121,6 +121,7 @@ struct OtOtpOtBeState { uint32_t regs[REGS_COUNT]; OtOtpBeCharacteristics characteristics; + bool lc_dft_en; char *ot_id; DeviceState *parent; @@ -138,6 +139,42 @@ static const OtOtpBeCharacteristics OTP_BE_CHARACTERISTICS = { }, }; +static uint8_t ot_otp_ot_be_reg_permit(hwaddr reg) +{ + switch (reg) { + case R_CSR2: + return 0x1u; + case R_CSR4: + case R_CSR7: + return 0x3u; + case R_CSR3: + return 0x7u; + case R_CSR0: + case R_CSR1: + case R_CSR5: + case R_CSR6: + return 0xfu; + default: + return 0x0u; + } +} + +static bool ot_otp_ot_be_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + OtOtpOtBeState *s = opaque; + (void)attrs; + if (!s->lc_dft_en) { + return false; + } + if (!is_write) { + return true; + } + uint8_t permit = ot_otp_ot_be_reg_permit(R32_OFF(addr)); + uint32_t byte_mask = ((1u << size) - 1u) << (addr & 3u); + return (permit & ~byte_mask) == 0u; +} + static uint64_t ot_otp_ot_be_read(void *opaque, hwaddr addr, unsigned size) { OtOtpOtBeState *s = opaque; @@ -170,6 +207,24 @@ static uint64_t ot_otp_ot_be_read(void *opaque, hwaddr addr, unsigned size) return (uint64_t)val32; } +#define CSR0_WMASK \ + (R_CSR0_FIELD0_MASK | R_CSR0_FIELD1_MASK | R_CSR0_FIELD2_MASK | \ + R_CSR0_FIELD3_MASK | R_CSR0_FIELD4_MASK) +#define CSR1_WMASK \ + (R_CSR1_FIELD0_MASK | R_CSR1_FIELD1_MASK | R_CSR1_FIELD2_MASK | \ + R_CSR1_FIELD3_MASK | R_CSR1_FIELD4_MASK) +#define CSR2_WMASK R_CSR2_FIELD0_MASK +#define CSR3_W1C_MASK \ + (R_CSR3_FIELD0_MASK | R_CSR3_FIELD1_MASK | R_CSR3_FIELD2_MASK) +#define CSR4_WMASK \ + (R_CSR4_FIELD0_MASK | R_CSR4_FIELD1_MASK | R_CSR4_FIELD2_MASK | \ + R_CSR4_FIELD3_MASK) +#define CSR5_WMASK \ + (R_CSR5_FIELD0_MASK | R_CSR5_FIELD1_MASK | R_CSR5_FIELD6_MASK) +#define CSR6_WMASK \ + (R_CSR6_FIELD0_MASK | R_CSR6_FIELD1_MASK | R_CSR6_FIELD2_MASK | \ + R_CSR6_FIELD3_MASK) + static void ot_otp_ot_be_write(void *opaque, hwaddr addr, uint64_t value, unsigned size) { @@ -184,14 +239,25 @@ static void ot_otp_ot_be_write(void *opaque, hwaddr addr, uint64_t value, switch (reg) { case R_CSR0: + s->regs[reg] = val32 & CSR0_WMASK; + break; case R_CSR1: + s->regs[reg] = val32 & CSR1_WMASK; + break; case R_CSR2: + s->regs[reg] = val32 & CSR2_WMASK; + break; case R_CSR3: + s->regs[reg] &= ~(val32 & CSR3_W1C_MASK); /* W1C */ + break; case R_CSR4: + s->regs[reg] = val32 & CSR4_WMASK; + break; case R_CSR5: + s->regs[reg] = (s->regs[reg] & ~CSR5_WMASK) | (val32 & CSR5_WMASK); + break; case R_CSR6: - /* TODO: not yet implemented */ - s->regs[reg] = val32; + s->regs[reg] = val32 & CSR6_WMASK; break; case R_CSR7: qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x02%x (%s)\n", @@ -219,6 +285,13 @@ ot_otp_ot_be_get_characteristics(OtOtpBeIf *beif) return &s->characteristics; } +static void ot_otp_ot_be_set_lc_dft_en(OtOtpBeIf *beif, bool en) +{ + OtOtpOtBeState *s = OT_OTP_OT_BE(beif); + + s->lc_dft_en = en; +} + static const Property ot_otp_ot_be_properties[] = { DEFINE_PROP_STRING(OT_COMMON_DEV_ID, OtOtpOtBeState, ot_id), DEFINE_PROP_LINK("parent", OtOtpOtBeState, parent, TYPE_DEVICE, @@ -237,6 +310,7 @@ static const MemoryRegionOps ot_otp_ot_be_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4, .impl.max_access_size = 4, + .valid.accepts = &ot_otp_ot_be_accepts, }; static void ot_otp_ot_be_reset_enter(Object *obj, ResetType type) @@ -249,6 +323,7 @@ static void ot_otp_ot_be_reset_enter(Object *obj, ResetType type) } memset(s->regs, 0, sizeof(s->regs)); + s->lc_dft_en = false; } static void ot_otp_ot_be_init(Object *obj) @@ -276,6 +351,7 @@ static void ot_otp_ot_be_class_init(ObjectClass *klass, const void *data) OtOtpBeIfClass *bec = OT_OTP_BE_IF_CLASS(klass); bec->is_ecc_enabled = &ot_otp_ot_be_is_ecc_enabled; bec->get_characteristics = &ot_otp_ot_be_get_characteristics; + bec->set_lc_dft_en = &ot_otp_ot_be_set_lc_dft_en; } static const TypeInfo ot_otp_ot_be_init_info = { diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index d11bace5db869..a9fc95ae4048f 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -793,7 +793,7 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { [OT_EG_SOC_DEV_OTP_BACKEND] = { .type = TYPE_OT_OTP_OT_BE, .memmap = MEMMAPENTRIES( - { .base = 0x40132000u } + { .base = 0x40138000u } ), .link = IBEXDEVICELINKDEFS( OT_EG_SOC_DEVLINK("parent", OTP_CTRL) @@ -834,8 +834,6 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_EG_SOC_SIGNAL(OT_LC_BROADCAST, OT_LC_OWNER_SEED_SW_RW_EN, FLASH_CTRL, OT_LC_BROADCAST, OT_FLASH_LC_OWNER_SEED_SW_RW_EN), - OT_EG_SOC_SIGNAL(OT_LC_BROADCAST, OT_LC_SEED_HW_RD_EN, FLASH_CTRL, - OT_LC_BROADCAST, OT_FLASH_LC_SEED_HW_RD_EN), OT_EG_SOC_SIGNAL(OT_LC_BROADCAST, OT_LC_ISO_PART_SW_RD_EN, FLASH_CTRL, OT_LC_BROADCAST, OT_FLASH_LC_ISO_PART_SW_RD_EN), From edd0b90dd7cac9cdab173259350723e027a4b006 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:32 +0000 Subject: [PATCH 17/52] [ot] hw/opentitan: flash: align MP/INFO perms, RMA wipe, and PERMIT Align the OpenTitan Flash Controller (`ot-flash`) model with `flash_ctrl.sv`, `flash_ctrl_rd.sv`, `flash_ctrl_lcmgr.sv`, and `flash_ctrl_core_reg_top.sv`: - Implement the `OT_FLASH_RMA_REQ` / `OT_FLASH_RMA_ACK` RMA wipe interface so `ot-lc_ctrl` RMA transitions erase flash banks and acknowledge completion. - Enforce `MP_REGION_CFG_0..7`, `MP_BANK_CFG_SHADOWED` (two-step shadow register writes gated by `BANK_CFG_REGWEN`), `BANK0/1_INFO0..2_PAGE_CFG` (`INFO_SEL = 0..2`), `PROG_TYPE_EN` (`rw0c`), `INIT` (`rw1s`), and `FIFO_LVL` (`0x1f1f`) CSR semantics. - Model the registered `u_flash_ctrl_rd.op_err_addr_o` flop (`flash_ctrl_rd.sv:96-102, 135-147`) via `rd_err_addr` so `OP_READ` faults on the final word (`remaining == 1`) latch the prior `rd_err_addr` into `ERR_ADDR`. - Enforce `FLASH_CTRL_CORE_PERMIT` and `FLASH_CTRL_PRIM_PERMIT` (`u_tlul_lc_gate` `lc_dft_en` qualification on `0x41008000`) sub-word write faults, plus `tlul_adapter_sram` read/write faults (`ErrOnRead` on `PROG_FIFO`, `ErrOnWrite` on `RD_FIFO` and `eflash` with `SecdedInv3932ZeroEcc` `store_resp_intg_err_o` integrity fault). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: If867bb51866fb6cc32295375510ee22a6a6a6964 --- hw/opentitan/ot_flash.c | 620 ++++++++++++++++++++++++++++---- hw/riscv/ot_earlgrey.c | 3 + include/hw/opentitan/ot_flash.h | 4 + 3 files changed, 559 insertions(+), 68 deletions(-) diff --git a/hw/opentitan/ot_flash.c b/hw/opentitan/ot_flash.c index 4558915138fd2..1021f09c05483 100644 --- a/hw/opentitan/ot_flash.c +++ b/hw/opentitan/ot_flash.c @@ -46,9 +46,11 @@ */ #include "qemu/osdep.h" +#include "qemu/bitmap.h" #include "qemu/error-report.h" #include "qemu/log.h" #include "qemu/memalign.h" +#include "qemu/queue.h" #include "qemu/timer.h" #include "qemu/typedefs.h" #include "qapi/error.h" @@ -798,12 +800,23 @@ typedef struct { uint32_t *info; /* info buffer (all partitions/banks) */ unsigned bank_count; /* count of banks */ unsigned size; /* overall storage size in bytes (excl. header) */ + unsigned header_size; /* size in bytes of the backend file header */ unsigned data_size; /* data buffer size of a bank in bytes */ unsigned info_size; /* info buffer size of a bank in bytes */ unsigned info_part_count; /* count of info partition (per bank) */ OtFlashInfoPart info_parts[MAX_INFO_PART_COUNT]; + uint32_t info_page_provisioned_bm; /* bitmask of provisioned info pages */ + unsigned long *word_prog_bm; /* bitmap of programmed 32-bit data words */ + unsigned total_data_words; /* total 32-bit words in data partition */ } OtFlashStorage; +typedef struct OtFlashEccFault { + MemoryRegion mr; + OtFlashState *s; + hwaddr offset; + QLIST_ENTRY(OtFlashEccFault) node; +} OtFlashEccFault; + typedef struct { char magic[4u]; /* vFSH */ uint32_t hlength; /* count of header bytes after this point */ @@ -846,6 +859,7 @@ struct OtFlashState { uint32_t *regs; uint32_t *csrs; + OtShadowReg mp_bank_cfg; /* "sticky" alerts that should stay signaled after firing */ uint32_t latched_alerts; @@ -864,11 +878,13 @@ struct OtFlashState { bool failed; bool hw; /* hw- or sw-requested operation? */ } op; + uint32_t rd_err_addr; /* u_flash_ctrl_rd.op_err_addr_o registered flop */ OtFlashLifeCyclePhase phase; /* HW LC phase for memory protection / RMA */ OtFlashLcBroadcast lc_broadcast; OtFifo32 rd_fifo; OtFifo32 prog_fifo; OtFlashStorage flash; + QLIST_HEAD(, OtFlashEccFault) ecc_faults; /* * HW and SW operations share a program FIFO, but read to separate @@ -972,9 +988,9 @@ static const OtFlashHwInfoPageRule OT_FLASH_HW_INFO_PAGE_RULES[] = { static void ot_flash_update_irqs(OtFlashState *s) { - uint32_t level = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]; - trace_ot_flash_irqs(s->ot_id, s->regs[R_INTR_STATE], s->regs[R_INTR_ENABLE], - level); + uint32_t state = s->regs[R_INTR_STATE] | s->regs[R_INTR_TEST]; + uint32_t level = state & s->regs[R_INTR_ENABLE]; + trace_ot_flash_irqs(s->ot_id, state, s->regs[R_INTR_ENABLE], level); for (unsigned ix = 0u; ix < PARAM_NUM_IRQS; ix++) { ibex_irq_set(&s->irqs[ix], (int)((level >> ix) & 0x1u)); } @@ -982,9 +998,12 @@ static void ot_flash_update_irqs(OtFlashState *s) static void ot_flash_update_alerts(OtFlashState *s) { - uint32_t levels = s->regs[R_ALERT_TEST]; + uint32_t fault_alerts = 0u; + if (s->regs[R_FAULT_STATUS]) { + fault_alerts |= ALERT_FATAL_ERR_MASK; + } - levels |= s->latched_alerts; + uint32_t levels = s->regs[R_ALERT_TEST] | s->latched_alerts | fault_alerts; for (unsigned ix = 0; ix < ARRAY_SIZE(s->alerts); ix++) { int level = (int)((levels >> ix) & 0x1u); @@ -1000,7 +1019,7 @@ static void ot_flash_update_alerts(OtFlashState *s) if (s->regs[R_ALERT_TEST]) { s->regs[R_ALERT_TEST] = 0; - levels = s->latched_alerts; + levels = s->latched_alerts | fault_alerts; for (unsigned ix = 0; ix < ARRAY_SIZE(s->alerts); ix++) { int level = (int)((levels >> ix) & 0x1u); if (level != ibex_irq_get_level(&s->alerts[ix])) { @@ -1022,8 +1041,8 @@ static bool ot_flash_write_backend(OtFlashState *s, const void *buffer, unsigned offset, size_t size) { /* NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */ - return blk_pwrite(s->blk, (int64_t)(intptr_t)offset, (int64_t)size, buffer, - (BdrvRequestFlags)0); + return blk_pwrite(s->blk, (int64_t)s->flash.header_size + (int64_t)offset, + (int64_t)size, buffer, (BdrvRequestFlags)0) < 0; /* NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */ } @@ -1089,6 +1108,11 @@ static bool ot_flash_in_operation(const OtFlashState *s) return s->op.kind != OP_NONE; } +bool ot_flash_is_idle(const OtFlashState *s) +{ + return !ot_flash_in_operation(s); +} + static bool ot_flash_in_hw_operation(const OtFlashState *s) { return s->op.kind != OP_NONE && s->op.hw; @@ -1124,12 +1148,35 @@ static void ot_flash_set_error(OtFlashState *s, uint32_t ebit, uint32_t eaddr) if (ebit) { if (s->op.hw) { s->regs[R_FAULT_STATUS] |= ebit; - s->latched_alerts |= ALERT_FATAL_ERR_MASK; } else { s->regs[R_OP_STATUS] |= R_OP_STATUS_ERR_MASK; - s->regs[R_INTR_STATE] |= INTR_CORR_ERR_MASK; - s->regs[R_ERR_ADDR] = FIELD_DP32(0, ERR_ADDR, ERR_ADDR, eaddr); - s->regs[R_ERR_CODE] = ebit; + uint32_t addr_err_mask = + R_ERR_CODE_MP_ERR_MASK | R_ERR_CODE_RD_ERR_MASK | + R_ERR_CODE_PROG_ERR_MASK; + if (ebit & addr_err_mask) { + if (s->op.kind == OP_ERASE) { + uint32_t align_mask = (s->op.erase_sel == ERASE_SEL_BANK) ? + ~(BYTES_PER_BANK - 1u) : + ~(BYTES_PER_PAGE - 1u); + eaddr = s->op.address & align_mask; + } else if (s->op.kind == OP_READ) { + /* + * In flash_ctrl_rd.sv:96-102, 135-147, op_err_addr_o is a + * registered flop updated on (~|op_err_q && |op_err_d). + * When OP_READ faults on its final word (remaining == 1), + * cnt_hit == 1 asserts op_done_o and hw2reg.err_addr.de on + * the same cycle before op_err_addr_o updates, latching the + * previous rd_err_addr into ERR_ADDR. + */ + uint32_t prev_eaddr = s->rd_err_addr; + s->rd_err_addr = eaddr; + if (s->op.remaining == 1u) { + eaddr = prev_eaddr; + } + } + s->regs[R_ERR_ADDR] = FIELD_DP32(0, ERR_ADDR, ERR_ADDR, eaddr); + } + s->regs[R_ERR_CODE] |= ebit; s->regs[R_ALERT_TEST] |= ALERT_RECOV_ERR_MASK; ot_flash_update_irqs(s); } @@ -1146,6 +1193,7 @@ static void ot_flash_op_complete(OtFlashState *s) * if there was an error at some point in the operation. */ if (!s->op.hw) { + s->regs[R_CONTROL] &= ~R_CONTROL_START_MASK; s->regs[R_OP_STATUS] |= R_OP_STATUS_DONE_MASK; s->regs[R_INTR_STATE] |= INTR_OP_DONE_MASK; ot_flash_update_irqs(s); @@ -1383,6 +1431,133 @@ static bool ot_flash_default_region_cfg_op_enabled(const OtFlashState *s) return en_field == OT_MULTIBITBOOL4_TRUE; } +static bool ot_flash_data_attr_enabled(const OtFlashState *s, unsigned address, + bool scramble) +{ + unsigned page = address / BYTES_PER_PAGE; + for (unsigned region = 0u; region < NUM_REGIONS; region++) { + unsigned r_region_cfg = R_MP_REGION_CFG_0 + region; + if (SHARED_FIELD_EX32(s->regs[r_region_cfg], MP_REGION_CFG_EN) != + OT_MULTIBITBOOL4_TRUE) { + continue; + } + unsigned r_region = R_MP_REGION_0 + region; + unsigned region_base_page = + SHARED_FIELD_EX32(s->regs[r_region], MP_REGION_BASE); + unsigned region_size = + SHARED_FIELD_EX32(s->regs[r_region], MP_REGION_SIZE); + if (page < region_base_page || + page >= (region_base_page + region_size)) { + continue; + } + unsigned en = + scramble ? + SHARED_FIELD_EX32(s->regs[r_region_cfg], + MP_REGION_CFG_SCRAMBLE_EN) : + SHARED_FIELD_EX32(s->regs[r_region_cfg], MP_REGION_CFG_ECC_EN); + return en == OT_MULTIBITBOOL4_TRUE; + } + unsigned def_en = + scramble ? + FIELD_EX32(s->regs[R_DEFAULT_REGION], DEFAULT_REGION, SCRAMBLE_EN) : + FIELD_EX32(s->regs[R_DEFAULT_REGION], DEFAULT_REGION, ECC_EN); + return def_en == OT_MULTIBITBOOL4_TRUE; +} + +static bool ot_flash_data_ecc_enabled(const OtFlashState *s, unsigned address) +{ + return ot_flash_data_attr_enabled(s, address, false); +} + +static bool +ot_flash_data_scramble_enabled(const OtFlashState *s, unsigned address) +{ + return ot_flash_data_attr_enabled(s, address, true); +} + +static bool ot_flash_is_ecc_corrupted(const OtFlashState *s, hwaddr address) +{ + hwaddr aligned_offset = address & ~7ULL; + const OtFlashEccFault *fault; + QLIST_FOREACH(fault, &s->ecc_faults, node) { + if (fault->offset == aligned_offset) { + return ot_flash_data_ecc_enabled(s, (unsigned)address); + } + } + return false; +} + +static MemTxResult ot_flash_ecc_fault_read_with_attrs( + void *opaque, hwaddr addr, uint64_t *data, unsigned size, MemTxAttrs attrs) +{ + OtFlashEccFault *fault = opaque; + OtFlashState *s = fault->s; + hwaddr flash_offset = fault->offset + addr; + (void)attrs; + + if (!ot_flash_data_ecc_enabled(s, (unsigned)flash_offset)) { + uint64_t val = 0; + memcpy(&val, (const uint8_t *)s->flash.data + flash_offset, size); + *data = val; + return MEMTX_OK; + } + + qemu_log_mask( + LOG_GUEST_ERROR, + "%s: %s: uncorrectable ECC fault reading flash data at 0x%" HWADDR_PRIx + "\n", + __func__, s->ot_id, flash_offset); + + s->regs[R_FAULT_STATUS] |= R_FAULT_STATUS_PHY_RELBL_ERR_MASK; + s->regs[R_ERR_ADDR] = + FIELD_DP32(0, ERR_ADDR, ERR_ADDR, (uint32_t)flash_offset); + ot_flash_update_alerts(s); + + *data = UINT64_MAX; + return MEMTX_ERROR; +} + +static const MemoryRegionOps ot_flash_ecc_fault_ops = { + .read_with_attrs = &ot_flash_ecc_fault_read_with_attrs, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl.min_access_size = 1u, + .impl.max_access_size = 4u, + .valid.min_access_size = 1u, + .valid.max_access_size = 4u, +}; + +static void ot_flash_add_ecc_fault(OtFlashState *s, hwaddr address) +{ + hwaddr aligned_offset = address & ~7ULL; + OtFlashEccFault *fault; + QLIST_FOREACH(fault, &s->ecc_faults, node) { + if (fault->offset == aligned_offset) { + return; + } + } + fault = g_new0(OtFlashEccFault, 1); + fault->s = s; + fault->offset = aligned_offset; + memory_region_init_io(&fault->mr, OBJECT(s), &ot_flash_ecc_fault_ops, fault, + TYPE_OT_FLASH ".ecc-fault", 8u); + memory_region_add_subregion_overlap(&s->mmio.mem, aligned_offset, + &fault->mr, 1); + QLIST_INSERT_HEAD(&s->ecc_faults, fault, node); +} + +static void +ot_flash_clear_ecc_faults_range(OtFlashState *s, hwaddr start, hwaddr size) +{ + OtFlashEccFault *fault, *next; + QLIST_FOREACH_SAFE(fault, &s->ecc_faults, node, next) { + if (fault->offset >= start && fault->offset < (start + size)) { + memory_region_del_subregion(&s->mmio.mem, &fault->mr); + QLIST_REMOVE(fault, node); + g_free(fault); + } + } +} + static bool ot_flash_can_erase_bank(const OtFlashState *s, unsigned bank) { switch (bank) { @@ -1499,6 +1674,21 @@ static unsigned ot_flash_next_info_address(OtFlashState *s) return address; } + if (!s->op.hw && s->op.kind == OP_READ && cfg.scramble_en && cfg.ecc_en) { + unsigned info_page_idx = address / BYTES_PER_PAGE; + if (info_page_idx < 32u && + !(storage->info_page_provisioned_bm & (1u << info_page_idx))) { + qemu_log_mask( + LOG_GUEST_ERROR, + "%s: %s: read error on unprovisioned scrambled/ECC info page " + "%u in partition %u of bank %u\n", + __func__, s->ot_id, page, info_partition, bank); + ot_flash_set_error(s, R_ERR_CODE_RD_ERR_MASK, op_address); + s->op.failed = true; + return address; + } + } + return address; } @@ -1629,7 +1819,30 @@ static void ot_flash_op_read(OtFlashState *s) /* For multi-word read access permission errors, return all 1s. */ if (!s->op.failed) { - word = src[address]; + if (!s->op.info_part && + ot_flash_is_ecc_corrupted(s, address * sizeof(uint32_t))) { + s->regs[R_FAULT_STATUS] |= + R_FAULT_STATUS_PHY_RELBL_ERR_MASK; + ot_flash_set_error(s, R_ERR_CODE_RD_ERR_MASK, + address * sizeof(uint32_t)); + s->op.failed = true; + word = 0xFFFFFFFFu; + } else { + word = src[address]; + } + } + } else if (!s->op.info_part && + (s->regs[R_ERR_CODE] & R_ERR_CODE_RD_ERR_MASK)) { + /* + * RTL: flash_ctrl_rd.sv:180 (`assign data_o = ~err_sel | + * (err_sel & op_err_o.rd_err) ? flash_data_i : inv_data_integ`). + * In StErr after a physical RD_ERR, flash_req_o is 0 so + * flash_phy_rd.sv data_err_o is 0 and flash_data_i outputs the + * latched 64-bit flash word's low 32-bit word (word_sel = 0). + */ + unsigned err_word = (s->regs[R_ERR_ADDR] / sizeof(uint32_t)) & ~1u; + if (err_word < storage->total_data_words) { + word = src[err_word]; } } @@ -1660,6 +1873,10 @@ static void ot_flash_op_prog(OtFlashState *s) xtrace_ot_flash_error(s->ot_id, "prog while prog FIFO empty"); return; } + if (ot_fifo32_num_used(&s->prog_fifo) < s->op.remaining && + !ot_fifo32_is_full(&s->prog_fifo)) { + return; + } OtFlashStorage *storage = &s->flash; uint32_t *dest = s->op.info_part ? storage->info : storage->data; @@ -1699,8 +1916,30 @@ static void ot_flash_op_prog(OtFlashState *s) g_assert(word_address < ((s->op.info_part ? storage->info_size : storage->data_size) * storage->bank_count)); + uint32_t prev_word = dest[word_address]; dest[word_address] &= word; - if (!s->op.info_part) { + if (!s->op.info_part && word_address < storage->total_data_words) { + bool scrambled = ot_flash_data_scramble_enabled(s, address); + unsigned aligned_word = word_address & ~1u; + bool both_zero = + (dest[aligned_word] == 0u) && (dest[aligned_word + 1u] == 0u); + if (both_zero && !scrambled) { + ot_flash_clear_ecc_faults_range(s, address & ~7ULL, 8u); + } else if (test_bit(word_address, storage->word_prog_bm) && + word != prev_word && (word != 0u || scrambled) && + ot_flash_data_ecc_enabled(s, address)) { + ot_flash_add_ecc_fault(s, address); + } + if (word != 0xFFFFFFFFu || scrambled) { + set_bit(word_address, storage->word_prog_bm); + } + } + if (s->op.info_part) { + unsigned info_page_idx = address / BYTES_PER_PAGE; + if (info_page_idx < 32u) { + storage->info_page_provisioned_bm |= (1u << info_page_idx); + } + } else { /* * for data, we must flush relevant TLB pages to ensure host reads * for XIP execution of mutated code in flash are correct. @@ -1710,9 +1949,10 @@ static void ot_flash_op_prog(OtFlashState *s) } trace_ot_flash_prog_word(s->ot_id, s->op.info_part, word_address, word); if (ot_flash_is_backend_writable(s)) { - uintptr_t dest_offset = (uintptr_t)dest - (uintptr_t)storage->data; + uintptr_t dest_offset = + (uintptr_t)&dest[word_address] - (uintptr_t)storage->data; if (ot_flash_write_backend(s, &dest[word_address], - (unsigned)(dest_offset + word_address), + (unsigned)dest_offset, sizeof(uint32_t))) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: cannot update flash backend\n", __func__, @@ -1749,7 +1989,15 @@ static void ot_flash_op_erase_page(OtFlashState *s, unsigned address) ((s->op.info_part ? storage->info_size : storage->data_size) * storage->bank_count)); memset(&dest[word_address], 0xFF, page_size); - if (!s->op.info_part) { + if (s->op.info_part) { + unsigned info_page_idx = page_address / BYTES_PER_PAGE; + if (info_page_idx < 32u) { + storage->info_page_provisioned_bm |= (1u << info_page_idx); + } + } else { + bitmap_clear(storage->word_prog_bm, (long)word_address, + (long)(page_size / sizeof(uint32_t))); + ot_flash_clear_ecc_faults_range(s, page_address, page_size); /* * for data, we must flush relevant TLB pages to ensure host reads * for XIP execution of mutated code in flash are correct. @@ -1758,10 +2006,10 @@ static void ot_flash_op_erase_page(OtFlashState *s, unsigned address) } trace_ot_flash_erase(s->ot_id, s->op.info_part, word_address, page_size); if (ot_flash_is_backend_writable(s)) { - uintptr_t dest_offset = (uintptr_t)dest - (uintptr_t)storage->data; + uintptr_t dest_offset = + (uintptr_t)&dest[word_address] - (uintptr_t)storage->data; if (ot_flash_write_backend(s, &dest[word_address], - (unsigned)(dest_offset + word_address), - page_size)) { + (unsigned)dest_offset, page_size)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: cannot update flash backend\n", __func__, s->ot_id); @@ -1773,7 +2021,8 @@ static void ot_flash_op_erase_page(OtFlashState *s, unsigned address) } } - ot_flash_op_complete(s); + timer_mod(s->op_delay, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + OP_INIT_DURATION_NS); } static void ot_flash_op_erase_bank(OtFlashState *s, unsigned address) @@ -1815,6 +2064,12 @@ static void ot_flash_op_erase_bank(OtFlashState *s, unsigned address) g_assert((info_address + bank_size) <= (storage->info_size * storage->bank_count)); memset(&storage->info[info_address], 0xFF, bank_size); + unsigned pages_per_bank = storage->info_size / BYTES_PER_PAGE; + unsigned first_page = bank * pages_per_bank; + for (unsigned p = 0; p < pages_per_bank && (first_page + p) < 32u; + p++) { + storage->info_page_provisioned_bm |= (1u << (first_page + p)); + } trace_ot_flash_erase(s->ot_id, true, info_address, bank_size); bank_size = storage->data_size; @@ -1829,20 +2084,24 @@ static void ot_flash_op_erase_bank(OtFlashState *s, unsigned address) * for XIP execution of mutated code in flash are correct. */ hwaddr data_byte_address = (hwaddr)data_address * sizeof(uint32_t); + bitmap_clear(storage->word_prog_bm, (long)data_address, + (long)(bank_size / sizeof(uint32_t))); + ot_flash_clear_ecc_faults_range(s, data_byte_address, bank_size); memory_region_set_dirty(&s->mmio.mem, data_byte_address, bank_size); if (ot_flash_is_backend_writable(s)) { + uintptr_t data_offset = + (uintptr_t)&storage->data[data_address] - (uintptr_t)storage->data; int data_write_err = ot_flash_write_backend(s, &storage->data[data_address], - (unsigned)(data_address), - storage->data_size); + (unsigned)data_offset, storage->data_size); int info_write_err = 0u; if (s->op.info_part) { - uintptr_t offset = - (uintptr_t)storage->info - (uintptr_t)storage->data; + uintptr_t info_offset = (uintptr_t)&storage->info[info_address] - + (uintptr_t)storage->data; info_write_err = ot_flash_write_backend(s, &storage->info[info_address], - (unsigned)(offset + info_address), + (unsigned)info_offset, storage->info_size); } @@ -1858,7 +2117,8 @@ static void ot_flash_op_erase_bank(OtFlashState *s, unsigned address) } } - ot_flash_op_complete(s); + timer_mod(s->op_delay, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + OP_INIT_DURATION_NS); } static void ot_flash_op_erase(OtFlashState *s) @@ -2084,7 +2344,6 @@ static bool ot_flash_check_program_resolution(OtFlashState *s) __func__, s->ot_id, s->op.address, s->op.count, start_window, end_window); ot_flash_set_error(s, R_ERR_CODE_PROG_WIN_ERR_MASK, s->op.address); - ot_flash_op_complete(s); return false; } return true; @@ -2102,7 +2361,6 @@ static bool ot_flash_check_program_type(OtFlashState *s) qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: program type not enabled: %s\n", __func__, s->ot_id, PROGRAM_NAME(s->op.prog_sel)); ot_flash_set_error(s, R_ERR_CODE_PROG_TYPE_ERR_MASK, s->op.address); - ot_flash_op_complete(s); return false; } return true; @@ -2124,6 +2382,7 @@ static void ot_flash_process_control_op(OtFlashState *s) unsigned num = (unsigned)FIELD_EX32(ctrl, CONTROL, NUM); s->op.hw = false; + s->op.failed = false; /* * If the flash controller is disabled by software, then (a) the flash @@ -2147,7 +2406,7 @@ static void ot_flash_process_control_op(OtFlashState *s) xtrace_ot_flash_info(s->ot_id, "Read from", s->op.address); s->op.count = num + 1u; break; - case CONTROL_OP_PROG: + case CONTROL_OP_PROG: { s->op.kind = OP_PROG; s->op.address = WORD_ALIGN_ADDR(s->regs[R_ADDR]); s->op.info_part = part_sel; @@ -2155,15 +2414,17 @@ static void ot_flash_process_control_op(OtFlashState *s) s->op.prog_sel = (bool)prog_sel; s->op.count = num + 1u; /* - * On encountering either a program resolution error or program type - * error, do not start the transaction. + * In RTL (flash_ctrl_prog.sv:164-178), both prog_win_err and + * prog_type_err are evaluated in parallel on op_start_i, and + * flash_ctrl_prog enters StErr to drain op_num_words_i + 1 words + * from PROG_FIFO before asserting op_done_o. */ - if (!ot_flash_check_program_resolution(s) || - !ot_flash_check_program_type(s)) { - break; - } + bool res_ok = ot_flash_check_program_resolution(s); + bool type_ok = ot_flash_check_program_type(s); + s->op.failed = !(res_ok && type_ok); xtrace_ot_flash_info(s->ot_id, "Write to", s->op.address); break; + } case CONTROL_OP_ERASE: s->op.kind = OP_ERASE; s->op.address = WORD_ALIGN_ADDR(s->regs[R_ADDR]); @@ -2181,7 +2442,6 @@ static void ot_flash_process_control_op(OtFlashState *s) ot_flash_op_complete(s); return; } - s->op.failed = false; s->op.remaining = s->op.count; ot_flash_op_start(s); } @@ -2190,14 +2450,20 @@ static void ot_flash_init_complete(void *opaque) { OtFlashState *s = opaque; - s->regs[R_STATUS] = FIELD_DP32(s->regs[R_STATUS], STATUS, INIT_WIP, 0u); - s->regs[R_STATUS] = FIELD_DP32(s->regs[R_STATUS], STATUS, INITIALIZED, 1u); - s->regs[R_PHY_STATUS] = - FIELD_DP32(s->regs[R_PHY_STATUS], PHY_STATUS, INIT_WIP, 0u); + if (s->op.kind == OP_INIT) { + s->regs[R_STATUS] = FIELD_DP32(s->regs[R_STATUS], STATUS, INIT_WIP, 0u); + s->regs[R_STATUS] = + FIELD_DP32(s->regs[R_STATUS], STATUS, INITIALIZED, 1u); + s->regs[R_PHY_STATUS] = + FIELD_DP32(s->regs[R_PHY_STATUS], PHY_STATUS, INIT_WIP, 0u); - trace_ot_flash_op_complete(s->ot_id, OP_NAME(s->op.kind), s->op.hw, true); + trace_ot_flash_op_complete(s->ot_id, OP_NAME(s->op.kind), s->op.hw, + true); - s->op.kind = OP_NONE; + s->op.kind = OP_NONE; + } else if (s->op.kind != OP_NONE) { + ot_flash_op_complete(s); + } } static uint64_t ot_flash_regs_read(void *opaque, hwaddr addr, unsigned size) @@ -2210,6 +2476,8 @@ static uint64_t ot_flash_regs_read(void *opaque, hwaddr addr, unsigned size) switch (reg) { case R_INTR_STATE: + val32 = s->regs[R_INTR_STATE] | s->regs[R_INTR_TEST]; + break; case R_INTR_ENABLE: case R_DIS: case R_EXEC: @@ -2298,7 +2566,6 @@ static uint64_t ot_flash_regs_read(void *opaque, hwaddr addr, unsigned size) case R_BANK1_INFO2_PAGE_CFG_1: case R_HW_INFO_CFG_OVERRIDE: case R_BANK_CFG_REGWEN: - case R_MP_BANK_CFG_SHADOWED: case R_OP_STATUS: case R_DEBUG_STATE: case R_ERR_CODE: @@ -2316,6 +2583,9 @@ static uint64_t ot_flash_regs_read(void *opaque, hwaddr addr, unsigned size) case R_STATUS: val32 = s->regs[reg]; break; + case R_MP_BANK_CFG_SHADOWED: + val32 = ot_shadow_reg_read(&s->mp_bank_cfg); + break; case R_RD_FIFO: if (!ot_fifo32_is_empty(&s->rd_fifo)) { val32 = ot_fifo32_pop(&s->rd_fifo); @@ -2362,6 +2632,7 @@ static uint64_t ot_flash_regs_read(void *opaque, hwaddr addr, unsigned size) val32 = SHARED_FIELD_DP32(val32, FIFO_LVL_PROG, ot_fifo32_num_used(&s->prog_fifo)); break; + case R_INTR_TEST: case R_ALERT_TEST: case R_PROG_FIFO: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: W/O register 0x%03x (%s)\n", @@ -2382,6 +2653,68 @@ static uint64_t ot_flash_regs_read(void *opaque, hwaddr addr, unsigned size) return (uint64_t)val32; }; +static inline uint32_t ot_flash_byte_mask(hwaddr addr, unsigned size) +{ + uint32_t mask = (size >= 4u) ? 0xfu : ((1u << size) - 1u); + return (mask << (addr & 3u)) & 0xfu; +} + +static uint32_t ot_flash_core_reg_permit(hwaddr reg) +{ + switch (reg) { + case R_EXEC: + case R_CONTROL: + case R_MP_REGION_CFG_0 ... R_MP_REGION_CFG_7: + case R_BANK0_INFO0_PAGE_CFG_0 ... R_BANK0_INFO0_PAGE_CFG_9: + case R_BANK0_INFO1_PAGE_CFG: + case R_BANK0_INFO2_PAGE_CFG_0 ... R_BANK0_INFO2_PAGE_CFG_1: + case R_BANK1_INFO0_PAGE_CFG_0 ... R_BANK1_INFO0_PAGE_CFG_9: + case R_BANK1_INFO1_PAGE_CFG: + case R_BANK1_INFO2_PAGE_CFG_0 ... R_BANK1_INFO2_PAGE_CFG_1: + case R_SCRATCH: + case R_PROG_FIFO: + return 0xfu; + case R_ADDR: + case R_MP_REGION_0 ... R_MP_REGION_7: + case R_DEFAULT_REGION: + case R_ERR_ADDR: + case R_ECC_SINGLE_ERR_ADDR_0 ... R_ECC_SINGLE_ERR_ADDR_1: + return 0x7u; + case R_DEBUG_STATE: + case R_STD_FAULT_STATUS: + case R_FAULT_STATUS: + case R_ECC_SINGLE_ERR_CNT: + case R_FIFO_LVL: + case R_CURR_FIFO_LVL: + return 0x3u; + default: + return 0x1u; + } +} + +static bool ot_flash_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + OtFlashState *s = opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + if (!is_write) { + if (reg == R_PROG_FIFO) { + return false; + } + if (reg == R_RD_FIFO && ot_fifo32_is_empty(&s->rd_fifo)) { + return !ot_flash_is_disabled(s) && ot_flash_operation_ongoing(s) && + s->op.kind == OP_READ; + } + return true; + } + if (reg == R_RD_FIFO) { + return false; + } + return (ot_flash_core_reg_permit(reg) & ~ot_flash_byte_mask(addr, size)) == + 0u; +} + static void ot_flash_regs_write(void *opaque, hwaddr addr, uint64_t val64, unsigned size) { @@ -2413,11 +2746,16 @@ static void ot_flash_regs_write(void *opaque, hwaddr addr, uint64_t val64, s->regs[R_INTR_ENABLE] = val32; ot_flash_update_irqs(s); break; - case R_INTR_TEST: + case R_INTR_TEST: { + uint32_t status_mask = INTR_PROG_EMPTY_MASK | INTR_PROG_LVL_MASK | + INTR_RD_FULL_MASK | INTR_RD_LVL_MASK; + uint32_t rw1c_mask = INTR_CORR_ERR_MASK | INTR_OP_DONE_MASK; val32 &= INTR_MASK; - s->regs[R_INTR_STATE] |= val32; + s->regs[R_INTR_TEST] = val32 & status_mask; + s->regs[R_INTR_STATE] |= val32 & rw1c_mask; ot_flash_update_irqs(s); break; + } case R_ALERT_TEST: val32 &= ALERT_MASK; s->regs[reg] = val32; @@ -2433,7 +2771,7 @@ static void ot_flash_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_INIT: val32 &= R_INIT_VAL_MASK; - s->regs[reg] = val32; + s->regs[reg] |= val32; /* rw1s */ if (val32) { ot_flash_initialize(s); } @@ -2473,20 +2811,32 @@ static void ot_flash_regs_write(void *opaque, hwaddr addr, uint64_t val64, ot_flash_process_control_op(s); break; case R_ADDR: + if (!(s->regs[R_CTRL_REGWEN] & R_CTRL_REGWEN_EN_MASK)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: %s is not enabled, so %s is protected\n", + __func__, s->ot_id, REG_NAME(R_CTRL_REGWEN), + REG_NAME(reg)); + break; + } val32 &= R_ADDR_START_MASK; s->regs[reg] = val32; break; case R_PROG_TYPE_EN: + if (!(s->regs[R_CTRL_REGWEN] & R_CTRL_REGWEN_EN_MASK)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: %s is not enabled, so %s is protected\n", + __func__, s->ot_id, REG_NAME(R_CTRL_REGWEN), + REG_NAME(reg)); + break; + } val32 &= R_PROG_TYPE_EN_NORMAL_MASK | R_PROG_TYPE_EN_REPAIR_MASK; - s->regs[reg] = val32; + s->regs[reg] &= val32; /* rw0c */ break; case R_ERASE_SUSPEND: - /* - * @todo We do not implement the erase suspend operation in QEMU as we - * do all erases synchronously, and so just immediately clear the erase - * suspend request. To implement this feature properly we would have to - * add delay to bank erases & check for erase suspends at each step. - */ + if ((val32 & R_ERASE_SUSPEND_REQ_MASK) && s->op.kind == OP_ERASE) { + timer_del(s->op_delay); + ot_flash_op_complete(s); + } s->regs[reg] = 0u; break; case R_REGION_CFG_REGWEN_0: @@ -2625,9 +2975,23 @@ static void ot_flash_regs_write(void *opaque, hwaddr addr, uint64_t val64, } break; case R_MP_BANK_CFG_SHADOWED: - val32 &= (R_MP_BANK_CFG_SHADOWED_ERASE_EN_0_MASK | - R_MP_BANK_CFG_SHADOWED_ERASE_EN_1_MASK); - s->regs[reg] = val32; + if (ot_flash_regs_is_wr_enabled(s, R_BANK_CFG_REGWEN)) { + val32 &= (R_MP_BANK_CFG_SHADOWED_ERASE_EN_0_MASK | + R_MP_BANK_CFG_SHADOWED_ERASE_EN_1_MASK); + switch (ot_shadow_reg_write(&s->mp_bank_cfg, val32)) { + case OT_SHADOW_REG_COMMITTED: + s->regs[reg] = ot_shadow_reg_peek(&s->mp_bank_cfg); + break; + case OT_SHADOW_REG_ERROR: + s->regs[R_ERR_CODE] |= R_ERR_CODE_UPDATE_ERR_MASK; + s->regs[R_ALERT_TEST] |= ALERT_RECOV_ERR_MASK; + ot_flash_update_alerts(s); + break; + case OT_SHADOW_REG_STAGED: + default: + break; + } + } break; case R_OP_STATUS: val32 &= R_OP_STATUS_DONE_MASK | R_OP_STATUS_ERR_MASK; @@ -2706,18 +3070,22 @@ static void ot_flash_regs_write(void *opaque, hwaddr addr, uint64_t val64, val32 &= rw0c_mask; val32 |= ~rw0c_mask; s->regs[reg] &= val32; + ot_flash_update_alerts(s); break; } case R_CTRL_REGWEN: case R_STATUS: case R_DEBUG_STATE: - case R_RD_FIFO: case R_STD_FAULT_STATUS: case R_ERR_ADDR: case R_ECC_SINGLE_ERR_ADDR_0: case R_ECC_SINGLE_ERR_ADDR_1: case R_PHY_STATUS: case R_CURR_FIFO_LVL: + case R_RD_FIFO: + /* + * R_RD_FIFO (0x1bc) is a read-only FIFO window; writes are ignored. + */ qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: R/O register 0x%03x (%s)\n", __func__, s->ot_id, (uint32_t)addr, REG_NAME(reg)); break; @@ -2789,16 +3157,21 @@ static void ot_flash_csrs_write(void *opaque, hwaddr addr, uint64_t val64, bool enable = s->csrs[R_CSR0_REGWEN] & R_CSR0_REGWEN_FIELD0_MASK; switch (csr) { case R_CSR0_REGWEN: - val32 &= R_CSR0_REGWEN_FIELD0_MASK; - break; + s->csrs[R_CSR0_REGWEN] &= (val32 & R_CSR0_REGWEN_FIELD0_MASK); + return; case R_CSR1: val32 &= R_CSR1_FIELD0_MASK | R_CSR1_FIELD1_MASK; break; - case R_CSR2: - val32 &= R_CSR2_FIELD0_MASK | R_CSR2_FIELD1_MASK | R_CSR2_FIELD2_MASK | - R_CSR2_FIELD3_MASK | R_CSR2_FIELD4_MASK | R_CSR2_FIELD5_MASK | - R_CSR2_FIELD6_MASK | R_CSR2_FIELD7_MASK; - break; + case R_CSR2: { + uint32_t rw1c_mask = + R_CSR2_FIELD0_MASK | R_CSR2_FIELD1_MASK | R_CSR2_FIELD2_MASK | + R_CSR2_FIELD4_MASK | R_CSR2_FIELD5_MASK | R_CSR2_FIELD6_MASK; + uint32_t rw_mask = R_CSR2_FIELD3_MASK | R_CSR2_FIELD7_MASK; + s->csrs[R_CSR2] = + ((s->csrs[R_CSR2] & ~(val32 & rw1c_mask)) & ~rw_mask) | + (val32 & rw_mask); + return; + } case R_CSR3: val32 &= R_CSR3_FIELD0_MASK | R_CSR3_FIELD1_MASK | R_CSR3_FIELD2_MASK | R_CSR3_FIELD3_MASK | R_CSR3_FIELD4_MASK | R_CSR3_FIELD5_MASK | @@ -2851,9 +3224,9 @@ static void ot_flash_csrs_write(void *opaque, hwaddr addr, uint64_t val64, val32 &= R_CSR19_FIELD0_MASK; break; case R_CSR20: - val32 &= - R_CSR20_FIELD0_MASK | R_CSR20_FIELD1_MASK | R_CSR20_FIELD2_MASK; - break; + s->csrs[R_CSR20] &= + ~(val32 & (R_CSR20_FIELD0_MASK | R_CSR20_FIELD1_MASK)); + return; default: enable = false; qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Bad offset 0x%02x\n", __func__, @@ -2866,6 +3239,68 @@ static void ot_flash_csrs_write(void *opaque, hwaddr addr, uint64_t val64, } } +static uint32_t ot_flash_prim_csr_permit(hwaddr csr) +{ + switch (csr) { + case R_CSR0_REGWEN: + case R_CSR2: + case R_CSR18 ... R_CSR20: + return 0x1u; + case R_CSR1: + case R_CSR4: + case R_CSR12: + case R_CSR14 ... R_CSR17: + return 0x3u; + case R_CSR5: + case R_CSR7: + case R_CSR13: + return 0x7u; + case R_CSR3: + case R_CSR6: + case R_CSR8 ... R_CSR11: + return 0xfu; + default: + return 0u; + } +} + +static bool ot_flash_csrs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + return !is_write || (ot_flash_prim_csr_permit(R32_OFF(addr)) & + ~ot_flash_byte_mask(addr, size)) == 0u; +} + +static void ot_flash_rma_wipe(OtFlashState *s) +{ + OtFlashStorage *storage = &s->flash; + unsigned data_bytes = storage->data_size * storage->bank_count; + + if (storage->data && data_bytes > 0) { + memset(storage->data, 0xFF, data_bytes); + memory_region_set_dirty(&s->mmio.mem, 0, data_bytes); + } + + if (storage->info && storage->info_part_count > 0) { + static const unsigned rma_info_pages[] = { + FLASH_QUAL_INFO_PAGE_CREATOR, + FLASH_QUAL_INFO_PAGE_OWNER, + FLASH_QUAL_INFO_PAGE_ISOLATED, + }; + for (unsigned i = 0; i < ARRAY_SIZE(rma_info_pages); i++) { + unsigned page = rma_info_pages[i]; + unsigned byte_offset = + storage->info_parts[0].offset + page * BYTES_PER_PAGE; + if (byte_offset + BYTES_PER_PAGE <= storage->info_size) { + uint8_t *info_ptr = (uint8_t *)storage->info + byte_offset; + memset(info_ptr, 0xFF, BYTES_PER_PAGE); + } + } + } +} + static void ot_flash_lc_broadcast_recv(void *opaque, int n, int level) { OtFlashState *s = opaque; @@ -2873,6 +3308,10 @@ static void ot_flash_lc_broadcast_recv(void *opaque, int n, int level) g_assert((unsigned)n < OT_FLASH_LC_BROADCAST_COUNT); + if (n == OT_FLASH_LC_RMA && level) { + ot_flash_rma_wipe(s); + } + uint16_t bit = 1u << (unsigned)n; bcast->incoming_signal_bm |= bit; /* @@ -2913,6 +3352,7 @@ static void ot_flash_lc_broadcast(void *opaque) case OT_FLASH_LC_OWNER_SEED_SW_RW_EN: case OT_FLASH_LC_ISO_PART_SW_RD_EN: case OT_FLASH_LC_ISO_PART_SW_WR_EN: + case OT_FLASH_LC_RMA: /* nothing to do here, flag is latched in current_level */ break; case OT_FLASH_LC_ESCALATE_EN: @@ -3043,6 +3483,7 @@ static void ot_flash_load(OtFlashState *s, Error **errp) unsigned offset = offsetof(OtFlashBackendHeader, hlength) + sizeof(header->hlength) + header->hlength; + flash->header_size = offset; /* NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange) */ rc = blk_pread(s->blk, (int64_t)offset, flash_size, flash->storage, 0); @@ -3126,6 +3567,43 @@ static void ot_flash_load(OtFlashState *s, Error **errp) (uint32_t *)(base + (uintptr_t)(flash->bank_count * data_size)); flash->data_size = data_size; flash->info_size = info_size; + + flash->total_data_words = + (flash->bank_count * flash->data_size) / sizeof(uint32_t); + flash->word_prog_bm = bitmap_new(flash->total_data_words); + unsigned words_per_page = BYTES_PER_PAGE / sizeof(uint32_t); + unsigned total_data_pages = + (flash->bank_count * flash->data_size) / BYTES_PER_PAGE; + for (unsigned p = 0; p < total_data_pages; p++) { + const uint32_t *page_words = &flash->data[p * words_per_page]; + bool page_has_data = false; + for (unsigned w = 0; w < words_per_page; w++) { + if (page_words[w] != 0xFFFFFFFFu) { + page_has_data = true; + break; + } + } + if (page_has_data) { + bitmap_set(flash->word_prog_bm, p * words_per_page, words_per_page); + } + } + + /* + * Unprovisioned secret, attestation seed, and certificate info pages + * raise RD_ERR when read with scrambling and ECC enabled before being + * erased or programmed: + * Bank 0 Info 0 Pages 1..4 (CreatorSecret, OwnerSecret, WaferAuthSecret, + * AttestationKeySeeds) -> indices 1..4 + * Bank 0 Info 0 Pages 5..8 (OwnerReserved0..3) -> indices 5..8 + * Bank 0 Info 0 Page 9 (FactoryCerts) -> index 9 + * Bank 1 Info 0 Pages 2..3 (OwnerSlot0, OwnerSlot1) -> indices 15..16 + * Bank 1 Info 0 Page 9 (DiceCerts) -> index 22 + */ + const uint32_t unprovisioned_default_mask = + (1u << 1) | (1u << 2) | (1u << 3) | (1u << 4) | (1u << 5) | (1u << 6) | + (1u << 7) | (1u << 8) | (1u << 9) | (1u << 15) | (1u << 16) | + (1u << 22); + flash->info_page_provisioned_bm = ~unprovisioned_default_mask; } #if DATA_PART_USE_IO_OPS @@ -3182,6 +3660,7 @@ static const MemoryRegionOps ot_flash_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_flash_regs_accepts, }; static const MemoryRegionOps ot_flash_csrs_ops = { @@ -3190,6 +3669,7 @@ static const MemoryRegionOps ot_flash_csrs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_flash_csrs_accepts, }; #if DATA_PART_USE_IO_OPS @@ -3300,13 +3780,16 @@ static void ot_flash_reset_enter(Object *obj, ResetType type) s->regs[R_STATUS] = 0xau; s->regs[R_PHY_STATUS] = 0x6u; s->regs[R_FIFO_LVL] = 0xf0fu; + ot_shadow_reg_init(&s->mp_bank_cfg, 0u); + memset(s->csrs, 0, CSRS_SIZE); s->csrs[R_CSR0_REGWEN] = 0x1u; /* restore flash memory region enablement if previously disabled */ memory_region_set_enabled(&s->mmio.mem, true); s->latched_alerts = 0u; + s->rd_err_addr = 0u; s->lc_broadcast.incoming_signal_bm = 0u; s->lc_broadcast.incoming_level_bm = 0u; @@ -3385,6 +3868,7 @@ static void ot_flash_init(Object *obj) ot_fifo32_create(&s->rd_fifo, OT_FLASH_READ_FIFO_SIZE); ot_fifo32_create(&s->hw_rd_fifo, FLASH_SEED_WORDS); ot_fifo32_create(&s->prog_fifo, OT_FLASH_PROG_FIFO_SIZE); + QLIST_INIT(&s->ecc_faults); for (unsigned ix = 0; ix < PARAM_NUM_IRQS; ix++) { ibex_sysbus_init_irq(obj, &s->irqs[ix]); diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index a9fc95ae4048f..d60433d12e109 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -1416,6 +1416,9 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_EG_SOC_DM_CONNECTION(OT_EG_SOC_DEV_DM, 3), OT_EG_SOC_GPIO_ALERT(0, 40) ), + .link = IBEXDEVICELINKDEFS( + OT_EG_SOC_DEVLINK("otp-ctrl", OTP_CTRL) + ), }, [OT_EG_SOC_DEV_DM_LC_CTRL] = { .type = TYPE_OT_DM_TL, diff --git a/include/hw/opentitan/ot_flash.h b/include/hw/opentitan/ot_flash.h index dac02939c81d7..7d1273977ca81 100644 --- a/include/hw/opentitan/ot_flash.h +++ b/include/hw/opentitan/ot_flash.h @@ -49,6 +49,8 @@ typedef enum { OT_FLASH_LC_ESCALATE_EN, /* "Indication ... that non-volatile memory debug is allowed" */ OT_FLASH_LC_NVM_DEBUG_EN, + /* "RMA request from lc_ctrl" - wipe flash contents */ + OT_FLASH_LC_RMA, OT_FLASH_LC_BROADCAST_COUNT, } OtFlashLcBroadcastType; @@ -81,4 +83,6 @@ struct OtFlashClass { OtFlashKeyMgrSecret *secret); }; +bool ot_flash_is_idle(const OtFlashState *s); + #endif /* HW_OPENTITAN_OT_FLASH_H */ From f975084f52da0759e26426d5a697e508d0dda55c Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:32 +0000 Subject: [PATCH 18/52] [ot] hw/opentitan: rom_ctrl: align digest window ECC, alerts, permit Align the OpenTitan ROM Controller (`hw/opentitan/ot_rom_ctrl.c`) with Earlgrey RTL (`rom_ctrl.sv`, `rom_ctrl_regs_reg_top.sv`, and `rom_ctrl_reg_pkg.sv`): 1. Add an overlapping MMIO subregion (`ot_rom_ctrl_digest_mem_ops`) over the top 32 bytes (`OT_ROM_DIGEST_BYTES`) of the ROM window so CPU bus reads to the raw `EXP_DIGEST` words after initial KMAC readout raise an Ibex load integrity error (`ot_common_raise_load_integrity_error`) matching `rom_ctrl.sv` (`EnableDataIntgPt = 1`). 2. Log `LOG_GUEST_ERROR` instead of aborting with `error_fatal` when `DIGEST` does not match `EXP_DIGEST` so `pwrmgr_good = false` is cleanly signaled to `pwrmgr`. 3. Pulse `s->alert` transiently on `R_ALERT_TEST` writes and restore the sticky `R_FATAL_ALERT_CAUSE` level, and clear `s->alert` in `ot_rom_ctrl_reset_hold()`. 4. Enforce `ROM_CTRL_REGS_PERMIT` subword write byte enables (`0x1u` for `ALERT_TEST`/`FATAL_ALERT_CAUSE`, `0xfu` for `DIGEST`/`EXP_DIGEST`) in `ot_rom_ctrl_regs_accepts()`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I0a2a4994d45a6fd2ad755acff3b0acaf6a6a6964 --- hw/opentitan/ot_rom_ctrl.c | 103 +++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 26 deletions(-) diff --git a/hw/opentitan/ot_rom_ctrl.c b/hw/opentitan/ot_rom_ctrl.c index 1b7af912042eb..2a3178e30a3cf 100644 --- a/hw/opentitan/ot_rom_ctrl.c +++ b/hw/opentitan/ot_rom_ctrl.c @@ -105,7 +105,6 @@ static const char *REG_NAMES[REGS_COUNT] = { #undef REG_NAME_ENTRY #define OT_ROM_CTRL_NUM_ADDR_SUBST_PERM_ROUNDS 2u -#define OT_ROM_CTRL_NUM_DATA_SUBST_PERM_ROUNDS 2u #define OT_ROM_CTRL_NUM_PRINCE_HALF_ROUNDS 3u #define OT_ROM_CTRL_DATA_BITS (sizeof(uint32_t) * 8u) @@ -150,6 +149,7 @@ struct OtRomCtrlState { SysBusDevice parent_obj; MemoryRegion mem; + MemoryRegion digest_io; MemoryRegion mmio; IbexIRQ pwrmgr_good; IbexIRQ pwrmgr_done; @@ -296,13 +296,6 @@ static unsigned ot_rom_ctrl_addr_sp_enc(const OtRomCtrlState *s, unsigned addr) OT_ROM_CTRL_NUM_ADDR_SUBST_PERM_ROUNDS); } -static uint64_t ot_rom_ctrl_data_sp_enc(const OtRomCtrlState *s, uint64_t in) -{ - (void)s; - return ot_rom_ctrl_subst_perm_enc(in, 0, OT_ROM_CTRL_WORD_BITS, - OT_ROM_CTRL_NUM_DATA_SUBST_PERM_ROUNDS); -} - static uint64_t ot_rom_ctrl_get_keystream(const OtRomCtrlState *s, unsigned addr) { @@ -319,13 +312,6 @@ ot_rom_ctrl_unscramble_word(const OtRomCtrlState *s, unsigned addr, uint64_t in) return keystream ^ in; } -static uint64_t -ot_rom_ctrl_scramble_word(const OtRomCtrlState *s, unsigned addr, uint64_t in) -{ - uint64_t keystream = ot_rom_ctrl_get_keystream(s, addr); - return ot_rom_ctrl_data_sp_enc(s, keystream ^ in); -} - static uint32_t ot_rom_ctrl_verify_ecc_39_32_u32( const OtRomCtrlState *s, uint64_t data_i, unsigned *err_o) { @@ -449,7 +435,10 @@ static void ot_rom_ctrl_unscramble(OtRomCtrlState *s, const uint64_t *src, for (unsigned wix = 0u; wix < ROM_DIGEST_WORDS; wix++, log_addr++) { unsigned phy_addr = ot_rom_ctrl_addr_sp_enc(s, log_addr); g_assert(phy_addr < dword_count); - s->regs[R_EXP_DIGEST_0 + wix] = (uint32_t)src[phy_addr]; + uint64_t scrdata = src[phy_addr]; + s->regs[R_EXP_DIGEST_0 + wix] = (uint32_t)scrdata; + dst[log_addr] = + (uint32_t)ot_rom_ctrl_unscramble_word(s, log_addr, scrdata); /* note: ECC is not used for DIGEST words */ } } @@ -457,17 +446,18 @@ static void ot_rom_ctrl_unscramble(OtRomCtrlState *s, const uint64_t *src, static void ot_rom_ctrl_scramble(OtRomCtrlState *s, const uint32_t *src, uint64_t *dst) { + unsigned scr_word_size = (s->size - OT_ROM_DIGEST_BYTES) / sizeof(uint32_t); + memset(&dst[scr_word_size], 0, ROM_DIGEST_WORDS * sizeof(uint64_t)); if (!s->key_xstr || !s->nonce_xstr) { trace_ot_rom_ctrl_missing(s->ot_id, "missing key/nonce to fake scrambling"); return; } - unsigned scr_word_size = (s->size - OT_ROM_DIGEST_BYTES) / sizeof(uint32_t); for (unsigned log_addr = 0u; log_addr < scr_word_size; log_addr++) { uint64_t clrdata = src[log_addr]; uint64_t eclrdata = ot_rom_ctrl_add_ecc_39_32_u64(clrdata); - uint64_t scrdata = ot_rom_ctrl_scramble_word(s, log_addr, eclrdata); + uint64_t scrdata = ot_rom_ctrl_unscramble_word(s, log_addr, eclrdata); dst[log_addr] = scrdata; } } @@ -479,12 +469,13 @@ static void ot_rom_ctrl_compare_and_notify(OtRomCtrlState *s) for (unsigned ix = 0u; ix < ROM_DIGEST_WORDS; ix++) { if (s->regs[R_EXP_DIGEST_0 + ix] != s->regs[R_DIGEST_0 + ix]) { rom_good = false; - error_setg(&error_fatal, - "ot_rom_ctrl: %s: Digest mismatch (expected 0x%08x got " - "0x%08x) @ %u, errors: %u single-bit, %u double-bit\n", - s->ot_id, s->regs[R_EXP_DIGEST_0 + ix], - s->regs[R_DIGEST_0 + ix], ix, s->recovered_error_count, - s->unrecoverable_error_count); + qemu_log_mask( + LOG_GUEST_ERROR, + "ot_rom_ctrl: %s: Digest mismatch (expected 0x%08x got " + "0x%08x) @ %u, errors: %u single-bit, %u double-bit\n", + s->ot_id, s->regs[R_EXP_DIGEST_0 + ix], + s->regs[R_DIGEST_0 + ix], ix, s->recovered_error_count, + s->unrecoverable_error_count); } } @@ -522,6 +513,7 @@ ot_rom_ctrl_handle_kmac_response(void *opaque, const OtKMACAppRsp *rsp) g_assert(s->scrambled.word_pos == s->scrambled.word_count); qemu_vfree(s->scrambled.buffer); + s->scrambled.buffer = NULL; s->scrambled.word_count = 0u; /* @@ -1018,8 +1010,10 @@ static void ot_rom_ctrl_regs_write(void *opaque, hwaddr addr, uint64_t val64, switch (reg) { case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_ERROR_MASK; - s->regs[reg] = val32; - ibex_irq_set(&s->alert, (int)(bool)val32); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, (int)(bool)s->regs[R_FATAL_ALERT_CAUSE]); + } break; case R_FATAL_ALERT_CAUSE: case R_DIGEST_0: @@ -1048,6 +1042,21 @@ static void ot_rom_ctrl_regs_write(void *opaque, hwaddr addr, uint64_t val64, } }; +static bool ot_rom_ctrl_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + hwaddr reg = R32_OFF(addr); + uint32_t permit = + (reg == R_ALERT_TEST || reg == R_FATAL_ALERT_CAUSE) ? 0x1u : 0xfu; + uint32_t be = (((1u << size) - 1u) << (addr & 3u)) & 0xfu; + return (permit & ~be) == 0u; +} + static void ot_rom_ctrl_get_rom_digest(const OtRomCtrlState *s, uint8_t digest[OT_ROM_DIGEST_BYTES]) { @@ -1142,6 +1151,41 @@ static const Property ot_rom_ctrl_properties[] = { DEFINE_PROP_STRING("key", OtRomCtrlState, key_xstr), }; +static uint64_t +ot_rom_ctrl_digest_mem_read(void *opaque, hwaddr addr, unsigned size) +{ + OtRomCtrlState *s = opaque; + hwaddr rom_offset = (hwaddr)(s->size - OT_ROM_DIGEST_BYTES) + addr; + const uint8_t *rom_ptr = + (const uint8_t *)memory_region_get_ram_ptr(&s->mem); + if (!s->first_reset) { + SysBusDevice *sbd = SYS_BUS_DEVICE(s); + hwaddr mem_off = (sbd->mmio[1].addr - sbd->mmio[0].addr) + rom_offset; + ot_common_raise_load_integrity_error(DEVICE(s), mem_off); + } + return (uint64_t)ldn_le_p(&rom_ptr[rom_offset], (int)size); +} + +static bool ot_rom_ctrl_digest_mem_accepts( + void *opaque, hwaddr addr, unsigned size, bool is_write, MemTxAttrs attrs) +{ + OtRomCtrlState *s = opaque; + hwaddr rom_offset = (hwaddr)(s->size - OT_ROM_DIGEST_BYTES) + addr; + if (!is_write && !s->first_reset) { + return s->unrecoverable_error_count == 0u; + } + return ot_rom_ctrl_mem_accepts(s, rom_offset, size, is_write, attrs); +} + +static const MemoryRegionOps ot_rom_ctrl_digest_mem_ops = { + .read = &ot_rom_ctrl_digest_mem_read, + .write = &ot_rom_ctrl_mem_write, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl.min_access_size = 1u, + .impl.max_access_size = 4u, + .valid.accepts = &ot_rom_ctrl_digest_mem_accepts, +}; + static const MemoryRegionOps ot_rom_ctrl_mem_ops = { .write = &ot_rom_ctrl_mem_write, .endianness = DEVICE_NATIVE_ENDIAN, @@ -1156,6 +1200,7 @@ static const MemoryRegionOps ot_rom_ctrl_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_rom_ctrl_regs_accepts, }; static void ot_rom_ctrl_reset_hold(Object *obj, ResetType type) @@ -1182,6 +1227,7 @@ static void ot_rom_ctrl_reset_hold(Object *obj, ResetType type) ibex_irq_set(&s->pwrmgr_good, false); ibex_irq_set(&s->pwrmgr_done, false); + ibex_irq_set(&s->alert, false); /* connect to KMAC */ OtKMACClass *kc = OT_KMAC_GET_CLASS(s->kmac); @@ -1231,6 +1277,11 @@ static void ot_rom_ctrl_realize(DeviceState *dev, Error **errp) &ot_rom_ctrl_mem_ops, s, TYPE_OT_ROM_CTRL ".mem", s->size, errp); + memory_region_init_io(&s->digest_io, OBJECT(dev), + &ot_rom_ctrl_digest_mem_ops, s, + TYPE_OT_ROM_CTRL ".digest_io", OT_ROM_DIGEST_BYTES); + memory_region_add_subregion_overlap(&s->mem, s->size - OT_ROM_DIGEST_BYTES, + &s->digest_io, 1); sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mem); /* From 7a8b0002d34413016e87550f2562fa88889f309c Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:32 +0000 Subject: [PATCH 19/52] [ot] hw/opentitan: aes: align ctrl, status, PRNG reseed, and lc_esc Align the OpenTitan Earlgrey AES hardware model (hw/opentitan/ot_aes.c and include/hw/opentitan/ot_aes.h) with hw/ip/aes/rtl/aes_core.sv, aes_control_fsm.sv, aes_ctrl_reg_shadowed.sv, and aes_reg_top.sv: - Sanitize sparse fields in CTRL_SHADOWED on write commit (OPERATION=0 -> AES_ENC, MODE=0 -> AES_NONE, KEY_LEN=0 -> AES_256, PRNG_RESEED_RATE=0 -> PER_1) and ignore TRIGGER.START when MODE == AES_NONE or MANUAL_OPERATION == 0. - Set STATUS.ALERT_RECOV_CTRL_UPDATE_ERR on CTRL_SHADOWED / CTRL_AUX_SHADOWED shadow update mismatch and clear on subsequent valid shadowed write; enforce CTRL_AUX_REGWEN gating on CTRL_AUX_SHADOWED. - Handle OT_AES_LC_ESCALATE_EN lifecycle escalation input by latching STATUS.ALERT_FATAL_FAULT and halting the cipher FSM. - Enforce AES_PERMIT sub-word MMIO write checks, W/O TRIGGER read-zero, and 1-cycle ALERT_TEST pulse semantics. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I36f4f2875f2908c0cd335d876ce4321b6a6a6964 --- hw/opentitan/ot_aes.c | 254 +++++++++++++++++++++++++++------- include/hw/opentitan/ot_aes.h | 3 +- 2 files changed, 204 insertions(+), 53 deletions(-) diff --git a/hw/opentitan/ot_aes.c b/hw/opentitan/ot_aes.c index c336773364963..940e9692462b6 100644 --- a/hw/opentitan/ot_aes.c +++ b/hw/opentitan/ot_aes.c @@ -214,6 +214,8 @@ typedef struct OtAESRegisters { DECLARE_BITMAP(data_out_bm, PARAM_NUM_REGS_DATA); uint32_t key[PARAM_NUM_REGS_KEY]; bool data_out_rdy; /* AES output data exist, not yet published */ + bool keyshare_armed; /* aes_reg_status.sv armed_q for key_init */ + bool iv_armed; /* aes_reg_status.sv armed_q for iv */ } OtAESRegisters; typedef struct OtAESContext { @@ -235,11 +237,17 @@ typedef struct OtAESContext { int aes_cipher; /* AES handle for tomcrypt */ } OtAESContext; +#define OT_AES_PRNG_MASKING_WORDS 6u +#define OT_AES_PRNG_CLEARING_WORDS 2u +#define OT_AES_PRNG_FULL_WORDS \ + (OT_AES_PRNG_MASKING_WORDS + OT_AES_PRNG_CLEARING_WORDS) + typedef struct OtAESEDN { OtEDNState *device; uint8_t ep; bool connected; bool scheduled; + unsigned pending_words; } OtAESEDN; typedef struct { @@ -316,7 +324,7 @@ struct OtAESClass { #define xtrace_ot_aes_error(_otid_, _msg_) \ trace_ot_aes_error(__func__, __LINE__, _otid_, _msg_) -static void ot_aes_reseed(OtAESState *s); +static void ot_aes_reseed(OtAESState *s, unsigned words); static void ot_aes_randomize(OtAESState *s, uint32_t *regs, size_t count) { @@ -340,26 +348,74 @@ static inline size_t ot_aes_get_key_length(OtAESRegisters *r) static inline uint32_t ot_aes_get_key_mask(OtAESRegisters *r) { uint32_t ctrl = ot_shadow_reg_peek(&r->ctrl); + uint32_t mask; switch (FIELD_EX32(ctrl, CTRL_SHADOWED, KEY_LEN)) { case 0x01u: - return 0x0fu; /* 128 bits, 16 bytes, 4 words */ + mask = 0x0fu; /* 128 bits, 16 bytes, 4 words */ + break; case 0x02u: - return 0x3fu; /* 192 bits, 24 bytes, 6 words */ + mask = 0x3fu; /* 192 bits, 24 bytes, 6 words */ + break; case 0x04u: default: - return 0xffu; /* 256bits, 32 bytes, 8 words */ + mask = 0xffu; /* 256bits, 32 bytes, 8 words */ + break; } + return mask | (mask << PARAM_NUM_REGS_KEY); }; static void ot_aes_update_alert(OtAESState *s) { - for (unsigned ix = 0; ix < PARAM_NUM_ALERTS; ix++) { + if (s->regs->status & (1u << OT_AES_ALERT_STATUS_OFFSET)) { + ibex_irq_set(&s->alerts[0], 1); + ibex_irq_set(&s->alerts[0], 0); + } + for (unsigned ix = 1u; ix < PARAM_NUM_ALERTS; ix++) { bool level = (bool)(s->regs->status & (1u << (ix + OT_AES_ALERT_STATUS_OFFSET))); ibex_irq_set(&s->alerts[ix], (int)level); } } +static uint32_t ot_aes_sanitize_ctrl(uint32_t val) +{ + uint32_t op = FIELD_EX32(val, CTRL_SHADOWED, OPERATION); + if (op != 0x1u && op != 0x2u) { + op = 0x1u; + } + uint32_t mode = FIELD_EX32(val, CTRL_SHADOWED, MODE); + switch (mode) { + case 0x01u: + case 0x02u: + case 0x04u: + case 0x08u: + case 0x10u: + case 0x20u: + break; + default: + mode = 0x20u; + break; + } + uint32_t key_len = FIELD_EX32(val, CTRL_SHADOWED, KEY_LEN); + if (key_len != 0x1u && key_len != 0x2u && key_len != 0x4u) { + key_len = 0x4u; + } + uint32_t reseed = FIELD_EX32(val, CTRL_SHADOWED, PRNG_RESEED_RATE); + if (reseed != 0x1u && reseed != 0x2u && reseed != 0x4u) { + reseed = 0x1u; + } + uint32_t out = 0u; + out = FIELD_DP32(out, CTRL_SHADOWED, OPERATION, op); + out = FIELD_DP32(out, CTRL_SHADOWED, MODE, mode); + out = FIELD_DP32(out, CTRL_SHADOWED, KEY_LEN, key_len); + out = FIELD_DP32(out, CTRL_SHADOWED, SIDELOAD, + FIELD_EX32(val, CTRL_SHADOWED, SIDELOAD)); + out = FIELD_DP32(out, CTRL_SHADOWED, PRNG_RESEED_RATE, reseed); + out = FIELD_DP32(out, CTRL_SHADOWED, MANUAL_OPERATION, + FIELD_EX32(val, CTRL_SHADOWED, MANUAL_OPERATION)); + return out; +} + static inline bool ot_aes_is_manual(OtAESRegisters *r) { uint32_t ctrl = ot_shadow_reg_peek(&r->ctrl); @@ -368,7 +424,8 @@ static inline bool ot_aes_is_manual(OtAESRegisters *r) static bool ot_aes_is_idle(OtAESState *s) { - return s->regs->trigger == 0u && + return !(s->regs->status & R_STATUS_ALERT_FATAL_FAULT_MASK) && + s->regs->trigger == 0u && /* retard_timer is never used if fast_mode is enabled */ (s->fast_mode || !timer_pending(s->retard_timer)); } @@ -407,7 +464,7 @@ static inline void ot_aes_load_reseed_rate(OtAESState *s) unsigned reseed; switch (rate) { - case 0x3u: + case 0x4u: /* should be "approximately" 8192 */ reseed = 8192u; break; @@ -456,6 +513,7 @@ static void ot_aes_init_keyshare(OtAESState *s, bool randomize) trace_ot_aes_init(s->ot_id, "keyshare init (data preserved)"); } bitmap_zero(r->keyshare_bm, (int64_t)(PARAM_NUM_REGS_KEY * 2u)); + r->keyshare_armed = false; c->key_ready = false; } @@ -471,6 +529,7 @@ static void ot_aes_init_iv(OtAESState *s, bool randomize) trace_ot_aes_init(s->ot_id, "iv init (data preserved)"); } bitmap_zero(r->iv_bm, PARAM_NUM_REGS_IV); + r->iv_armed = false; c->iv_ready = false; } @@ -482,10 +541,17 @@ static void ot_aes_init_data(OtAESState *s, bool io) trace_ot_aes_init(s->ot_id, "data_in"); ot_aes_randomize(s, r->data_in, ARRAY_SIZE(r->data_in)); bitmap_zero(r->data_in_bm, PARAM_NUM_REGS_DATA); + s->ctx->di_full = false; } else { trace_ot_aes_init(s->ot_id, "data_out"); + if (r->status & R_STATUS_OUTPUT_VALID_MASK) { + r->status |= R_STATUS_OUTPUT_LOST_MASK; + } ot_aes_randomize(s, r->data_out, ARRAY_SIZE(r->data_out)); bitmap_zero(r->data_out_bm, PARAM_NUM_REGS_DATA); + r->status &= ~R_STATUS_OUTPUT_VALID_MASK; + r->data_out_rdy = false; + s->ctx->do_full = false; } } @@ -515,15 +581,7 @@ static bool ot_aes_is_mode_ready(OtAESRegisters *r, bool *need_iv) static void ot_aes_trigger_reseed(OtAESState *s) { - OtAESRegisters *r = s->regs; - uint32_t ctrl_aux = ot_shadow_reg_peek(&r->ctrl_aux); - - if (!(ctrl_aux & R_CTRL_AUX_SHADOWED_FORCE_MASKS_MASK)) { - ot_aes_reseed(s); - } else { - r->trigger &= ~R_TRIGGER_PRNG_RESEED_MASK; - xtrace_ot_aes_info(s->ot_id, "reseed on trigger disabled"); - } + ot_aes_reseed(s, OT_AES_PRNG_FULL_WORDS); } static void ot_aes_sideload_key(OtAESState *s) @@ -567,13 +625,14 @@ static void ot_aes_update_key(OtAESState *s) c->key[ix >> 1u] = key; } - if (!c->key_ready && ot_aes_key_touch_force_reseed(r)) { + bool reseed = !c->key_ready && ot_aes_key_touch_force_reseed(r); + c->key_ready = true; + + if (reseed) { r->trigger |= R_TRIGGER_PRNG_RESEED_MASK; trace_ot_aes_reseed(s->ot_id, "new key"); ot_aes_trigger_reseed(s); } - - c->key_ready = true; } static void ot_aes_update_iv(OtAESState *s) @@ -653,8 +712,10 @@ static void ot_aes_handle_trigger(OtAESState *s) ibex_irq_set(&s->clock_active, (int)true); if (r->trigger & R_TRIGGER_PRNG_RESEED_MASK) { - trace_ot_aes_reseed(s->ot_id, "trigger write"); - ot_aes_trigger_reseed(s); + if (!s->edn.scheduled) { + trace_ot_aes_reseed(s->ot_id, "trigger write"); + ot_aes_trigger_reseed(s); + } if (s->edn.scheduled) { xtrace_ot_aes_debug(s->ot_id, "EDN scheduled, defer"); return; @@ -675,8 +736,10 @@ static void ot_aes_handle_trigger(OtAESState *s) if (r->trigger & R_TRIGGER_START_MASK) { if (ot_aes_get_mode(r) == AES_NONE || !ot_aes_is_manual(r)) { - /* ignore */ + /* ignore and clear start bit (aes_control_fsm.sv:328) */ xtrace_ot_aes_debug(s->ot_id, "start trigger ignored"); + r->trigger &= ~R_TRIGGER_START_MASK; + ibex_irq_set(&s->clock_active, (int)!ot_aes_is_idle(s)); return; } } @@ -838,6 +901,9 @@ static void ot_aes_push(OtAESState *s) OtAESRegisters *r = s->regs; OtAESContext *c = s->ctx; + if (r->status & R_STATUS_OUTPUT_VALID_MASK) { + r->status |= R_STATUS_OUTPUT_LOST_MASK; + } memcpy(r->data_out, c->dst, sizeof(c->dst)); memcpy(r->iv, c->iv, sizeof(c->iv)); r->data_out_rdy = true; @@ -941,6 +1007,9 @@ static void ot_aes_process(OtAESState *s) static void ot_aes_commit_data_out(OtAESRegisters *r) { if (r->data_out_rdy) { + if (r->status & R_STATUS_OUTPUT_VALID_MASK) { + r->status |= R_STATUS_OUTPUT_LOST_MASK; + } bitmap_fill(r->data_out_bm, PARAM_NUM_REGS_DATA); r->status |= R_STATUS_OUTPUT_VALID_MASK; r->data_out_rdy = false; @@ -949,6 +1018,14 @@ static void ot_aes_commit_data_out(OtAESRegisters *r) static inline void ot_aes_do_process(OtAESState *s) { + OtAESRegisters *r = s->regs; + if (ot_aes_is_manual(r)) { + xtrace_ot_aes_info(s->ot_id, "end of manual seq"); + r->trigger &= ~R_TRIGGER_START_MASK; + } + + r->keyshare_armed = true; + r->iv_armed = true; ot_aes_process(s); ot_aes_push(s); if (s->reseed_count) { @@ -961,18 +1038,12 @@ static inline void ot_aes_do_process(OtAESState *s) * which would not match the HW behavior */ trace_ot_aes_reseed(s->ot_id, "reseed_count reached"); - s->regs->trigger |= R_TRIGGER_PRNG_RESEED_MASK; - ot_aes_trigger_reseed(s); ot_aes_load_reseed_rate(s); + r->trigger |= R_TRIGGER_PRNG_RESEED_MASK; + ot_aes_reseed(s, OT_AES_PRNG_MASKING_WORDS); } else { /* flag pushed data as immediately available */ - ot_aes_commit_data_out(s->regs); - } - - OtAESRegisters *r = s->regs; - if (ot_aes_is_manual(r)) { - xtrace_ot_aes_info(s->ot_id, "end of manual seq"); - s->regs->trigger &= ~R_TRIGGER_START_MASK; + ot_aes_commit_data_out(r); } } @@ -1022,6 +1093,15 @@ static void ot_aes_fill_entropy(void *opaque, uint32_t bits, bool fips) return; } trace_ot_aes_fill_entropy(s->ot_id, bits, fips); + ot_prng_reseed(s->prng, bits); + + if (edn->pending_words > 1u) { + edn->pending_words -= 1u; + ot_edn_request_entropy(edn->device, edn->ep); + return; + } + + edn->pending_words = 0u; edn->scheduled = false; r->trigger &= ~R_TRIGGER_PRNG_RESEED_MASK; @@ -1032,7 +1112,8 @@ static void ot_aes_fill_entropy(void *opaque, uint32_t bits, bool fips) */ ot_aes_commit_data_out(r); - ot_prng_reseed(s->prng, bits); + ot_aes_update_key(s); + ot_aes_update_config(s); ot_aes_handle_trigger(s); } @@ -1044,7 +1125,7 @@ static void ot_aes_handle_process(void *opaque) ot_aes_do_process(s); } -static void ot_aes_reseed(OtAESState *s) +static void ot_aes_reseed(OtAESState *s, unsigned words) { OtAESEDN *edn = &s->edn; @@ -1054,9 +1135,9 @@ static void ot_aes_reseed(OtAESState *s) } if (!edn->scheduled) { trace_ot_aes_request_entropy(s->ot_id); - if (!ot_edn_request_entropy(edn->device, edn->ep)) { - edn->scheduled = true; - } else { + edn->scheduled = true; + edn->pending_words = words; + if (ot_edn_request_entropy(edn->device, edn->ep)) { xtrace_ot_aes_error(s->ot_id, "cannot request new entropy"); } } @@ -1073,6 +1154,24 @@ static void ot_aes_clock_input(void *opaque, int irq, int level) /* TODO: disable AES execution when PCLK is 0 */ } +static void ot_aes_lc_escalate(void *opaque, int irq, int level) +{ + OtAESState *s = opaque; + + g_assert(irq == 0); + + if (level) { + timer_del(s->retard_timer); + qemu_bh_cancel(s->process_bh); + s->edn.scheduled = false; + s->edn.pending_words = 0u; + s->regs->status = R_STATUS_ALERT_FATAL_FAULT_MASK; + s->regs->trigger = 0u; + ibex_irq_set(&s->alerts[ALERT_FATAL_FAULT_SHIFT], 1); + ibex_irq_set(&s->clock_active, 0); + } +} + static void ot_aes_push_key(OtKeySinkIf *ifd, const uint8_t *share0, const uint8_t *share1, size_t key_len, bool valid) { @@ -1168,9 +1267,13 @@ static uint64_t ot_aes_read(void *opaque, hwaddr addr, unsigned size) if (ot_aes_is_idle(s)) { val32 |= R_STATUS_IDLE_MASK; } - if (!ot_aes_is_data_in_ready(r)) { + if (!ot_aes_is_data_in_ready(r) && !s->ctx->di_full) { val32 |= R_STATUS_INPUT_READY_MASK; } + if ((r->status & R_STATUS_OUTPUT_VALID_MASK || s->ctx->do_full) && + s->ctx->di_full) { + val32 |= R_STATUS_STALL_MASK; + } break; default: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Bad offset 0x%02x\n", __func__, @@ -1201,11 +1304,13 @@ static void ot_aes_write(void *opaque, hwaddr addr, uint64_t val64, switch (reg) { case R_ALERT_TEST: - if (val32 & ALERT_RECOV_CTRL_UPDATE_ERR_MASK) { - r->status |= R_STATUS_ALERT_RECOV_CTRL_UPDATE_ERR_MASK; - } - if (val32 & ALERT_FATAL_FAULT_MASK) { - r->status |= R_STATUS_ALERT_FATAL_FAULT_MASK; + val32 &= ALERT_RECOV_CTRL_UPDATE_ERR_MASK | ALERT_FATAL_FAULT_MASK; + for (unsigned ix = 0; ix < PARAM_NUM_ALERTS; ix++) { + if (val32 & (1u << ix)) { + ibex_irq_set(&s->alerts[ix], 0); + ibex_irq_set(&s->alerts[ix], 1); + ibex_irq_set(&s->alerts[ix], 0); + } } ot_aes_update_alert(s); break; @@ -1240,10 +1345,18 @@ static void ot_aes_write(void *opaque, hwaddr addr, uint64_t val64, break; } if (ot_aes_is_idle(s)) { + if (r->keyshare_armed) { + bitmap_zero(r->keyshare_bm, (int64_t)(PARAM_NUM_REGS_KEY * 2u)); + s->ctx->key_ready = false; + r->keyshare_armed = false; + } r->keyshare[reg - R_KEY_SHARE0_0] = val32; set_bit((int64_t)(reg - R_KEY_SHARE0_0), r->keyshare_bm); ot_aes_update_key(s); ot_aes_update_config(s); + if (!ot_aes_is_manual(r)) { + ot_aes_process_cond(s); + } } break; case R_IV_0: @@ -1251,10 +1364,18 @@ static void ot_aes_write(void *opaque, hwaddr addr, uint64_t val64, case R_IV_2: case R_IV_3: if (ot_aes_is_idle(s)) { + if (r->iv_armed) { + bitmap_zero(r->iv_bm, PARAM_NUM_REGS_IV); + s->ctx->iv_ready = false; + r->iv_armed = false; + } r->iv[reg - R_IV_0] = val32; set_bit((int64_t)(reg - R_IV_0), r->iv_bm); ot_aes_update_iv(s); ot_aes_update_config(s); + if (!ot_aes_is_manual(r)) { + ot_aes_process_cond(s); + } } break; case R_DATA_IN_0: @@ -1262,10 +1383,14 @@ static void ot_aes_write(void *opaque, hwaddr addr, uint64_t val64, case R_DATA_IN_2: case R_DATA_IN_3: r->data_in[reg - R_DATA_IN_0] = val32; - set_bit((int64_t)(reg - R_DATA_IN_0), r->data_in_bm); - if (ot_aes_is_data_in_ready(r)) { - ibex_irq_set(&s->clock_active, (int)true); - ot_aes_pop(s); + if (s->ctx->di_full) { + memcpy(s->ctx->src, r->data_in, sizeof(s->ctx->src)); + } else { + set_bit((int64_t)(reg - R_DATA_IN_0), r->data_in_bm); + if (ot_aes_is_data_in_ready(r)) { + ibex_irq_set(&s->clock_active, (int)true); + ot_aes_pop(s); + } } if (!ot_aes_is_manual(r)) { ot_aes_process_cond(s); @@ -1275,14 +1400,17 @@ static void ot_aes_write(void *opaque, hwaddr addr, uint64_t val64, if (!ot_aes_is_idle(s)) { break; } - val32 &= R_CTRL_SHADOWED_OPERATION_MASK | R_CTRL_SHADOWED_MODE_MASK | - R_CTRL_SHADOWED_KEY_LEN_MASK | R_CTRL_SHADOWED_SIDELOAD_MASK | - R_CTRL_SHADOWED_PRNG_RESEED_RATE_MASK | - R_CTRL_SHADOWED_MANUAL_OPERATION_MASK | - R_CTRL_SHADOWED_FORCE_ZERO_MASKS_MASK; + val32 = ot_aes_sanitize_ctrl(val32); OtAESMode prev_mode = ot_aes_get_mode(s->regs); + r->status &= ~(R_STATUS_OUTPUT_LOST_MASK | R_STATUS_OUTPUT_VALID_MASK); + bitmap_zero(r->data_in_bm, PARAM_NUM_REGS_DATA); + bitmap_zero(r->data_out_bm, PARAM_NUM_REGS_DATA); + s->ctx->di_full = false; + s->ctx->do_full = false; + r->data_out_rdy = false; switch (ot_shadow_reg_write(&r->ctrl, val32)) { case OT_SHADOW_REG_STAGED: + r->status &= ~R_STATUS_ALERT_RECOV_CTRL_UPDATE_ERR_MASK; break; case OT_SHADOW_REG_COMMITTED: /* @@ -1290,6 +1418,7 @@ static void ot_aes_write(void *opaque, hwaddr addr, uint64_t val64, * of a new message. Hence, software needs to provide new key, * IV and input data afterwards." */ + r->status &= ~R_STATUS_ALERT_RECOV_CTRL_UPDATE_ERR_MASK; ot_aes_finalize(s, prev_mode); ot_aes_init_keyshare(s, false); ot_aes_init_iv(s, false); @@ -1324,7 +1453,7 @@ static void ot_aes_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_CTRL_AUX_REGWEN: val32 &= R_CTRL_AUX_REGWEN_CTRL_AUX_REGWEN_MASK; - r->ctrl_aux_regwen = val32; + r->ctrl_aux_regwen &= val32; break; case R_TRIGGER: val32 &= R_TRIGGER_START_MASK | R_TRIGGER_KEY_IV_DATA_IN_CLEAR_MASK | @@ -1339,6 +1468,23 @@ static void ot_aes_write(void *opaque, hwaddr addr, uint64_t val64, } }; +static bool ot_aes_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + hwaddr reg = R32_OFF(addr); + uint32_t be = (((1u << size) - 1u) << (addr & 3u)) & 0xfu; + uint32_t permit = + (reg == R_CTRL_SHADOWED) ? + 0x3u : + ((reg >= R_KEY_SHARE0_0 && reg <= R_DATA_OUT_3) ? 0xfu : 0x1u); + return (permit & ~be) == 0u; +} + static const Property ot_aes_properties[] = { DEFINE_PROP_STRING(OT_COMMON_DEV_ID, OtAESState, ot_id), DEFINE_PROP_STRING("clock-name", OtAESState, clock_name), @@ -1360,6 +1506,7 @@ static const MemoryRegionOps ot_aes_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_aes_regs_accepts, }; static void ot_aes_reset_enter(Object *obj, ResetType type) @@ -1386,6 +1533,7 @@ static void ot_aes_reset_enter(Object *obj, ResetType type) r->status = 0u; r->data_out_rdy = false; e->scheduled = false; + e->pending_words = 0u; ot_aes_load_reseed_rate(s); for (unsigned ix = 0; ix < PARAM_NUM_ALERTS; ix++) { @@ -1445,6 +1593,8 @@ static void ot_aes_realize(DeviceState *dev, Error **errp) qdev_init_gpio_in_named(DEVICE(s), &ot_aes_clock_input, OT_AES_CLOCK_INPUT, 1); + qdev_init_gpio_in_named(DEVICE(s), &ot_aes_lc_escalate, + OT_AES_LC_ESCALATE_EN, 1); s->prng = ot_prng_allocate(); } diff --git a/include/hw/opentitan/ot_aes.h b/include/hw/opentitan/ot_aes.h index dc69ad5041cb3..52d0de927fffa 100644 --- a/include/hw/opentitan/ot_aes.h +++ b/include/hw/opentitan/ot_aes.h @@ -33,6 +33,7 @@ #define TYPE_OT_AES "ot-aes" OBJECT_DECLARE_TYPE(OtAESState, OtAESClass, OT_AES) -#define OT_AES_KEY_SIZE (256u / 8u) +#define OT_AES_KEY_SIZE (256u / 8u) +#define OT_AES_LC_ESCALATE_EN TYPE_OT_AES "-lc-escalate-en" #endif /* HW_OPENTITAN_OT_AES_H */ From d482e634546de3d4c1908d13920465fd965a6785 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:33 +0000 Subject: [PATCH 20/52] [ot] hw/opentitan: hmac: implement SHA-2 256/384/512 and FIFO drain Align the OpenTitan Earlgrey HMAC model (hw/opentitan/ot_hmac.c) with hw/ip/hmac/rtl/hmac.sv, prim_sha2.sv, and hmac_reg_top.sv: - Replicate SHA-256 digest words (DIGEST_0..7) into DIGEST_8..15 and write back the full 512-bit SHA-512 state (DIGEST_12..15 = state[6..7]) on SHA-384 completion and context saves. - Store WIPE_SECRET in big-endian word order so DIGEST_0..15 readback respects CFG.DIGEST_SWAP, and clear DIGEST_0..15 to 0 only on the falling edge (1 -> 0) of CFG.SHA_EN while preserving MSG_LENGTH. - Allow CFG writes after CMD.HASH_STOP when clearing SHA_EN, ignore CMD.HASH_STOP when idle, and stall CMD.HASH_PROCESS on 64-bit MSG_LENGTH overflow (msg_length_overflow). - Latch ERR_CODE only when INTR_STATE.HMAC_ERR is not already pending, report SwPushMsgWhenDisallowed when pushing to MSG_FIFO with SHA_EN=0, exclude status-type FIFO_EMPTY from INTR_STATE W1C, and enforce HMAC_PERMIT sub-word MMIO and write-only MSG_FIFO access checks. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I9aa4b51b4f89d6eb3d7b147123982a6d6a6a6964 --- hw/opentitan/ot_hmac.c | 133 +++++++++++++++++++++++++++++++---------- 1 file changed, 103 insertions(+), 30 deletions(-) diff --git a/hw/opentitan/ot_hmac.c b/hw/opentitan/ot_hmac.c index bbe8152486af7..9002c4c12bc72 100644 --- a/hw/opentitan/ot_hmac.c +++ b/hw/opentitan/ot_hmac.c @@ -268,6 +268,7 @@ typedef struct OtHMACRegisters OtHMACRegisters; struct OtHMACContext { hash_state state; OtHMACDigestSize digest_size_started; + bool msg_length_overflow; }; typedef struct OtHMACContext OtHMACContext; @@ -420,7 +421,9 @@ static void ot_hmac_update_alert(OtHMACState *s) static void ot_hmac_report_error(OtHMACState *s, uint32_t error) { - s->regs->err_code = error; + if (!(s->regs->intr_state & INTR_HMAC_ERR_MASK)) { + s->regs->err_code = error; + } s->regs->intr_state |= INTR_HMAC_ERR_MASK; ot_hmac_update_irqs(s); } @@ -432,6 +435,7 @@ static void ot_hmac_writeback_digest_state(OtHMACState *s) case HMAC_SHA2_256: for (unsigned idx = 0; idx < 8u; idx++) { STORE32H(s->ctx->state.sha256.state[idx], s->regs->digest + idx); + s->regs->digest[idx + 8u] = s->regs->digest[idx]; } break; case HMAC_SHA2_384: @@ -463,9 +467,9 @@ static void ot_hmac_restore_context(OtHMACState *s) * SW interface. This is because the extra block containing the key XORed * with the inner pad is not included in the SW-visible message length. */ - unsigned msg_length = s->regs->msg_length; + uint64_t msg_length = s->regs->msg_length; if (s->regs->cfg & R_CFG_HMAC_EN_MASK) { - msg_length += ot_hmac_get_block_size_bytes(s) * 8u; + msg_length += (uint64_t)ot_hmac_get_block_size_bytes(s) * 8u; } switch (s->ctx->digest_size_started) { @@ -576,9 +580,16 @@ static void ot_hmac_sha_done(OtHMACState *s) switch (s->ctx->digest_size_started) { case HMAC_SHA2_256: sha256_done(&s->ctx->state, (uint8_t *)s->regs->digest); + for (unsigned idx = 0; idx < 8u; idx++) { + s->regs->digest[idx + 8u] = s->regs->digest[idx]; + } return; case HMAC_SHA2_384: sha384_done(&s->ctx->state, (uint8_t *)s->regs->digest); + for (unsigned idx = 6u; idx < 8u; idx++) { + STORE64H(s->ctx->state.sha512.state[idx], + s->regs->digest + 2 * idx); + } return; case HMAC_SHA2_512: sha512_done(&s->ctx->state, (uint8_t *)s->regs->digest); @@ -641,11 +652,6 @@ static void ot_hmac_process_fifo(OtHMACState *s) if (fifo8_is_empty(&s->input_fifo) || stop) { ot_hmac_writeback_digest_state(s); } - - /* assert FIFO Empty IRQ */ - if (fifo8_is_empty(&s->input_fifo)) { - s->regs->intr_state |= INTR_FIFO_EMPTY_MASK; - } } if (stop && ot_hmac_get_curlen(s) == 0) { @@ -654,6 +660,9 @@ static void ot_hmac_process_fifo(OtHMACState *s) } if (s->regs->cmd & R_CMD_HASH_PROCESS_MASK) { + if (s->ctx->msg_length_overflow) { + return; + } ot_hmac_compute_digest(s); s->regs->intr_state |= INTR_HMAC_DONE_MASK; s->regs->cmd = 0; @@ -684,6 +693,25 @@ static void ot_hmac_clock_input(void *opaque, int irq, int level) /* TODO: disable HMAC execution when PCLK is 0 */ } +static bool ot_hmac_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + if (reg >= REGS_COUNT) { + return false; + } + if (!is_write) { + return true; + } + uint32_t reg_be = (((1u << size) - 1u) << (addr & 0x3u)) & 0xfu; + uint32_t permit = (reg <= R_ALERT_TEST || reg == R_CMD) ? + 0x1u : + ((reg == R_CFG || reg == R_STATUS) ? 0x3u : 0xfu); + return (permit & ~reg_be) == 0u; +} + static uint64_t ot_hmac_regs_read(void *opaque, hwaddr addr, unsigned size) { OtHMACState *s = OT_HMAC(opaque); @@ -691,6 +719,7 @@ static uint64_t ot_hmac_regs_read(void *opaque, hwaddr addr, unsigned size) uint32_t val32; hwaddr reg = R32_OFF(addr); + switch (reg) { case R_INTR_STATE: val32 = s->regs->intr_state; @@ -833,7 +862,8 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, switch (reg) { case R_INTR_STATE: - s->regs->intr_state &= ~(val32 & INTR_MASK); + s->regs->intr_state &= + ~(val32 & (INTR_HMAC_DONE_MASK | INTR_HMAC_ERR_MASK)); ot_hmac_update_irqs(s); break; case R_INTR_ENABLE: @@ -841,7 +871,8 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, ot_hmac_update_irqs(s); break; case R_INTR_TEST: - s->regs->intr_state |= val32 & INTR_MASK; + s->regs->intr_state = + (s->regs->intr_state & ~INTR_FIFO_EMPTY_MASK) | (val32 & INTR_MASK); ot_hmac_update_irqs(s); break; case R_ALERT_TEST: @@ -849,9 +880,20 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, ot_hmac_update_alert(s); break; case R_CFG: - /* ignore write if engine is not idle */ + /* + * In RTL (hmac.sv:354-400), cfg_reg writes are gated by !cfg_block, + * where cfg_block is cleared by reg_hash_done or reg_hash_stop. + */ if (s->regs->cmd) { - break; + if (s->regs->cmd & R_CMD_HASH_STOP_MASK) { + if (!(val32 & R_CFG_SHA_EN_MASK)) { + s->regs->cmd = 0; + s->ctx->msg_length_overflow = false; + fifo8_reset(&s->input_fifo); + } + } else { + break; + } } val32 &= @@ -873,12 +915,17 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, val32 |= OT_HMAC_CFG_KEY_LENGTH_NONE << R_CFG_KEY_LENGTH_SHIFT; } + bool prev_sha_en = (bool)(s->regs->cfg & R_CFG_SHA_EN_MASK); s->regs->cfg = val32; - /* clear digest when SHA is disabled */ - if (!(s->regs->cfg & R_CFG_SHA_EN_MASK)) { - ot_hmac_wipe_buffer(s, s->regs->digest, - ARRAY_SIZE(s->regs->digest)); + /* + * In RTL (prim_sha2.sv:159-160, 396), `clear_digest = hash_start_i | + * (~sha_en_i & sha_en_q)` zeroes digest_q to 0 ONLY on the falling edge + * of sha_en (1 -> 0). Note that message_length (hmac.sv:629-650) is NOT + * cleared by sha_en 1 -> 0. + */ + if (prev_sha_en && !(s->regs->cfg & R_CFG_SHA_EN_MASK)) { + memset(s->regs->digest, 0, sizeof(s->regs->digest)); } break; case R_CMD: @@ -911,6 +958,7 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, } s->regs->cmd = R_CMD_HASH_START_MASK; s->regs->msg_length = 0; + s->ctx->msg_length_overflow = false; ibex_irq_set(&s->clock_active, true); @@ -964,7 +1012,12 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, } if (val32 & R_CMD_HASH_STOP_MASK) { + if (!(s->regs->cmd & + (R_CMD_HASH_START_MASK | R_CMD_HASH_CONTINUE_MASK))) { + break; + } s->regs->cmd = R_CMD_HASH_STOP_MASK; + s->ctx->msg_length_overflow = false; /* * trigger delayed processing of FIFO until the next block is @@ -986,6 +1039,7 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, } s->regs->cmd = R_CMD_HASH_CONTINUE_MASK; + s->ctx->msg_length_overflow = false; /* * Hold the previous digest size until the HMAC is started with the @@ -1004,7 +1058,16 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, case R_WIPE_SECRET: s->regs->wipe_secret = val32; ot_hmac_wipe_buffer(s, s->regs->key, ARRAY_SIZE(s->regs->key)); - ot_hmac_wipe_buffer(s, s->regs->digest, ARRAY_SIZE(s->regs->digest)); + /* + * In RTL (prim_sha2.sv:148 & hmac.sv:270), WIPE_SECRET=V stores V into + * each 32-bit word of digest[], and reading DIGEST_k returns + * conv_endian32(V, digest_swap). Since s->regs->digest[] is stored in + * big-endian format, store bswap32(val32) so DIGEST_k reads as val32 + * when digest_swap==0 and bswap32(val32) when digest_swap==1. + */ + for (unsigned index = 0; index < ARRAY_SIZE(s->regs->digest); index++) { + s->regs->digest[index] = bswap32(val32); + } break; case R_KEY_0: case R_KEY_1: @@ -1087,6 +1150,12 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, "%s: Cannot W register 0x%02x (%s) whilst SHA Engine " "is enabled\n", __func__, (uint32_t)addr, REG_NAME(reg)); + break; + } + digest_size = ot_hmac_get_digest_size(s->regs->cfg); + if (digest_size == HMAC_SHA2_NONE || + (digest_size == HMAC_SHA2_256 && (reg - R_DIGEST_0) >= 8u)) { + break; } /* @@ -1102,6 +1171,10 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, } else { s->regs->digest[reg - R_DIGEST_0] = bswap32(val32); } + if (digest_size == HMAC_SHA2_256 && (reg - R_DIGEST_0) < 8u) { + s->regs->digest[(reg - R_DIGEST_0) + 8u] = + s->regs->digest[reg - R_DIGEST_0]; + } break; case R_MSG_LENGTH_LOWER: /* ignore write and report error if engine is not idle */ @@ -1142,14 +1215,14 @@ static void ot_hmac_regs_write(void *opaque, hwaddr addr, uint64_t value, } } -static uint64_t ot_hmac_fifo_read(void *opaque, hwaddr addr, unsigned size) +static bool ot_hmac_fifo_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) { (void)opaque; (void)addr; (void)size; - qemu_log_mask(LOG_GUEST_ERROR, "%s: MSG_FIFO is write only\n", __func__); - - return 0; + (void)attrs; + return is_write; } static void ot_hmac_fifo_write(void *opaque, hwaddr addr, uint64_t value, @@ -1161,16 +1234,11 @@ static void ot_hmac_fifo_write(void *opaque, hwaddr addr, uint64_t value, trace_ot_hmac_fifo_write(s->ot_id, (uint32_t)addr, (uint32_t)value, size, pc); - if (!s->regs->cmd) { + if (!s->regs->cmd || !(s->regs->cfg & R_CFG_SHA_EN_MASK)) { ot_hmac_report_error(s, R_ERR_CODE_PUSH_MSG_WHEN_DISALLOWED); return; } - if (!(s->regs->cfg & R_CFG_SHA_EN_MASK)) { - ot_hmac_report_error(s, R_ERR_CODE_PUSH_MSG_WHEN_SHA_DISABLED); - return; - } - if (s->regs->cfg & R_CFG_ENDIAN_SWAP_MASK) { if (size == 4u) { value = bswap32((uint32_t)value); @@ -1188,7 +1256,11 @@ static void ot_hmac_fifo_write(void *opaque, hwaddr addr, uint64_t value, value >>= 8u; } - s->regs->msg_length += (uint64_t)size * 8u; + uint64_t add_bits = (uint64_t)size * 8u; + if (s->regs->msg_length + add_bits < s->regs->msg_length) { + s->ctx->msg_length_overflow = true; + } + s->regs->msg_length += add_bits; /* * Note: real HW may stall the bus till some room is available in the input @@ -1217,19 +1289,20 @@ static const MemoryRegionOps ot_hmac_regs_ops = { .read = &ot_hmac_regs_read, .write = &ot_hmac_regs_write, .endianness = DEVICE_NATIVE_ENDIAN, - .valid = { + .impl = { .min_access_size = 4u, .max_access_size = 4u, }, + .valid.accepts = &ot_hmac_regs_accepts, }; static const MemoryRegionOps ot_hmac_fifo_ops = { - .read = &ot_hmac_fifo_read, .write = &ot_hmac_fifo_write, .endianness = DEVICE_NATIVE_ENDIAN, .valid = { .min_access_size = 1u, .max_access_size = 4u, + .accepts = &ot_hmac_fifo_accepts, }, }; From 5145f925e4f9c5e7f4f9a2a60c6ebae740e407bc Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:33 +0000 Subject: [PATCH 21/52] [ot] hw/opentitan: kmac: align entropy FSM, errchk, and escalation Align the OpenTitan KMAC/SHA3 accelerator (`hw/opentitan/ot_kmac.c` and `include/hw/opentitan/ot_kmac.h`) with Earlgrey RTL (`kmac.sv`, `kmac_core.sv`, `kmac_entropy.sv`, `kmac_errchk.sv`, `kmac_app.sv`, and `kmac_reg_pkg.sv`): 1. Implement the `kmac_entropy.sv` state machine (`entropy_mode` latching in `StRandReset`, 5-word `ENTROPY_SEED` counter, EDN endpoint request and `edn_wait_timer` `OT_KMAC_ERR_WAIT_TIMER_EXPIRED` timeout, and `ENTROPY_REFRESH_HASH_CNT` clearing on `CMD.entropy_req` / `CMD.hash_cnt_clr`). 2. Align `kmac_errchk.sv` `ERR_CODE` formatting (`0x0804002e` for invalid command sequences, `ErrModeStrength` when `EN_UNSUPPORTED_MODESTRENGTH=0`, `ErrKeyNotValid` on invalid sideload keys, and `ErrSwIssuedCmdInAppActive`). 3. Fix cSHAKE customization string `S` length offset in `ot_kmac_decode_sw_prefix()` (`offset += customstr_len`), gate `INTR_STATE.fifo_empty` on `msgfifo_full_seen_q`, and mask `STATE` share readouts during squeeze. 4. Add `OT_KMAC_LC_ESCALATE_EN` input handling to transition KMAC, app mux, and entropy FSMs into terminal error and assert `ALERT_FATAL`, and enforce `KMAC_PERMIT`, read-only `STATE`, and write-only `MSG_FIFO` MMIO `accepts` checks. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ibb78cbc038c93db6e5bf494001b180cb6a6a6964 --- hw/opentitan/ot_kmac.c | 911 ++++++++++++++++++++------------- include/hw/opentitan/ot_kmac.h | 2 + 2 files changed, 563 insertions(+), 350 deletions(-) diff --git a/hw/opentitan/ot_kmac.c b/hw/opentitan/ot_kmac.c index c072698b030c3..c4b804a0ef315 100644 --- a/hw/opentitan/ot_kmac.c +++ b/hw/opentitan/ot_kmac.c @@ -310,8 +310,8 @@ static const char *REG_NAMES[REGS_COUNT] = { /* Input FIFO length is 80 bytes (10 x 64 bits) */ #define FIFO_LENGTH 80u -/* Delay FIFO ingestion and compute by 100ns */ -#define BH_TRIGGER_DELAY_NS 100u +/* Delay FIFO ingestion and compute by 5us */ +#define BH_TRIGGER_DELAY_NS 5000u /* Max size of the KECCAK state */ #define KECCAK_STATE_BITS 1600u @@ -401,8 +401,18 @@ struct OtKMACState { OtShadowReg entropy_refresh_threshold; OtKMACFsmState state; /* Main FSM state */ + bool in_process; bool invalid_state_read; bool error_awaiting_sw; /* error awaiting SW acknowledgement */ + bool app_in_error; /* kmac_app.sv in StError/StErrorAwaitSw */ + bool cfg_entropy_ready; /* kmac_errchk.sv cfg_entropy_ready */ + bool entropy_configured; /* kmac_entropy.sv st != StRandReset */ + bool entropy_in_err; /* kmac_entropy.sv st == StRandErr */ + uint8_t entropy_mode; /* kmac_entropy.sv mode_q (latched in StRandReset) */ + uint8_t sw_seed_cnt; /* kmac_entropy.sv StSwSeedWait seed word counter */ + bool entropy_seeded; + bool edn_pending; + bool lc_escalate_en; hash_state ltc_state; /* TomCrypt hash state */ uint8_t keccak_state[KECCAK_STATE_BYTES]; @@ -419,6 +429,7 @@ struct OtKMACState { Fifo8 input_fifo; QEMUTimer *bh_timer; /* timer to delay bh when triggered from vCPU */ + QEMUTimer *edn_wait_timer; /* timer for EDN wait timeout */ QEMUBH *bh; char *ot_id; @@ -460,21 +471,24 @@ static void ot_kmac_trigger_deferred_bh(OtKMACState *s) qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + BH_TRIGGER_DELAY_NS); } +static void ot_kmac_process(void *opaque); + static void ot_kmac_bh_timer_handler(void *opaque) { - OtKMACState *s = opaque; - qemu_bh_schedule(s->bh); + ot_kmac_process(opaque); } static void ot_kmac_cancel_bh(OtKMACState *s) { timer_del(s->bh_timer); + timer_del(s->edn_wait_timer); qemu_bh_cancel(s->bh); } static void ot_kmac_update_irq(OtKMACState *s) { - uint32_t level = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]; + uint32_t state = s->regs[R_INTR_STATE] | s->regs[R_INTR_TEST]; + uint32_t level = state & s->regs[R_INTR_ENABLE]; for (unsigned ix = 0; ix < ARRAY_SIZE(s->irqs); ix++) { ibex_irq_set(&s->irqs[ix], (int)((level >> ix) & 0x1u)); } @@ -494,6 +508,17 @@ static void ot_kmac_update_alert(OtKMACState *s) for (unsigned ix = 0; ix < ARRAY_SIZE(s->alerts); ix++) { ibex_irq_set(&s->alerts[ix], (int)((level >> ix) & 0x1u)); } + + s->regs[R_ALERT_TEST] = 0u; + uint32_t perm_level = + (s->regs[R_STATUS] & R_STATUS_ALERT_FATAL_FAULT_MASK) ? + (1u << ALERT_FATAL) : + 0u; + if (level != perm_level) { + for (unsigned ix = 0; ix < ARRAY_SIZE(s->alerts); ix++) { + ibex_irq_set(&s->alerts[ix], (int)((perm_level >> ix) & 0x1u)); + } + } } static void ot_kmac_report_error(OtKMACState *s, int code, uint32_t info) @@ -510,6 +535,83 @@ static void ot_kmac_report_error(OtKMACState *s, int code, uint32_t info) ot_kmac_update_irq(s); } +static void ot_kmac_edn_wait_timer_handler(void *opaque) +{ + OtKMACState *s = opaque; + if (!s->edn_pending) { + return; + } + + s->edn_pending = false; + s->entropy_in_err = true; + s->entropy_seeded = true; + ot_kmac_report_error(s, OT_KMAC_ERR_WAIT_TIMER_EXPIRED, 0u); + + if (s->state == KMAC_ST_PROCESSING || s->state == KMAC_ST_SQUEEZING) { + ot_kmac_process(s); + } +} + +static void ot_kmac_fill_entropy(void *opaque, uint32_t bits, bool fips) +{ + OtKMACState *s = opaque; + (void)bits; + (void)fips; + + timer_del(s->edn_wait_timer); + s->edn_pending = false; + s->entropy_seeded = true; + + if (s->state == KMAC_ST_PROCESSING || s->state == KMAC_ST_SQUEEZING) { + ot_kmac_process(s); + } +} + +static void ot_kmac_request_entropy(OtKMACState *s) +{ + if (!s->entropy_configured || s->entropy_mode != 1u) { + /* + * RTL (kmac_entropy.sv): mode_q is latched once on leaving StRandReset. + * If mode_q != EntropyModeEdn (1), KMAC never transitions to StRandEdn. + */ + return; + } + + uint32_t prescaler = + FIELD_EX32(s->regs[R_ENTROPY_PERIOD], ENTROPY_PERIOD, PRESCALER); + uint32_t wait_timer = + FIELD_EX32(s->regs[R_ENTROPY_PERIOD], ENTROPY_PERIOD, WAIT_TIMER); + uint64_t wait_cycles = (uint64_t)wait_timer * ((uint64_t)prescaler + 1ULL); + + /* + * In RTL (kmac_entropy.sv), seeding the 160-bit PRNG state in StRandEdn + * requires 5x 32-bit EDN words across prim_edn_req (prim_sync_reqack 2-FF + * req/ack CDC synchronizers, >= 6 cycles per word = 30 cycles minimum). + */ + if (wait_timer > 0 && wait_cycles < 30ULL) { + s->edn_pending = false; + s->entropy_in_err = true; + s->entropy_seeded = true; + ot_kmac_report_error(s, OT_KMAC_ERR_WAIT_TIMER_EXPIRED, 0u); + if (s->state == KMAC_ST_PROCESSING || s->state == KMAC_ST_SQUEEZING) { + qemu_bh_schedule(s->bh); + } + return; + } + + if (s->edn && s->edn_ep != UINT8_MAX && !s->edn_pending) { + s->edn_pending = true; + (void)ot_edn_request_entropy(s->edn, s->edn_ep); + if (wait_timer > 0) { + uint64_t clk = s->pclk ? s->pclk : 100000000ULL; + int64_t delay_ns = + (int64_t)muldiv64(wait_cycles, NANOSECONDS_PER_SECOND, clk); + timer_mod(s->edn_wait_timer, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + delay_ns); + } + } +} + static void ot_kmac_get_sw_config(OtKMACState *s) { uint32_t cfg = ot_shadow_reg_peek(&s->cfg); @@ -630,6 +732,7 @@ static void ot_kmac_get_key(OtKMACState *s, uint8_t *key, size_t *keylen) if (!sl_key->valid) { /* HW defaults to info = app_id = 0 when in a SW operation. */ uint32_t err_info = s->current_app ? s->current_app->index : 0; + s->app_in_error = true; ot_kmac_report_error(s, OT_KMAC_ERR_KEY_NOT_VALID, err_info); } return; @@ -658,6 +761,12 @@ static inline size_t ot_kmac_get_keccak_rate_bytes(size_t kstrength) return (KECCAK_STATE_BITS - 2u * kstrength) / 8u; } +static uint32_t ot_kmac_state_mask_word(const OtKMACState *s, unsigned word_ix) +{ + (void)s; + return (word_ix + 1u) * 0x9e3779b9u; +} + static void ot_kmac_reset_state(OtKMACState *s) { memset(s->keccak_state, 0, sizeof(s->keccak_state)); @@ -671,6 +780,7 @@ static void ot_kmac_return_to_idle(OtKMACState *s) { /* flush state */ ot_kmac_change_fsm_state(s, KMAC_ST_IDLE); + s->regs[R_INTR_STATE] &= ~INTR_FIFO_EMPTY_MASK; ot_kmac_reset_state(s); ot_kmac_cancel_bh(s); /* now is a good time to check for pending app requests */ @@ -688,121 +798,128 @@ static void ot_kmac_complete_app_req(OtKMACState *s) static void ot_kmac_process(void *opaque) { OtKMACState *s = opaque; - OtKMACAppCfg *cfg = s->current_cfg; - OtKMACAppRsp rsp; + if (s->in_process) { + return; + } + s->in_process = true; - g_assert(cfg); + do { + OtKMACAppCfg *cfg = s->current_cfg; + OtKMACAppRsp rsp; - if (s->current_app) { - /* App mode, FIFO should be empty */ - g_assert(fifo8_is_empty(&s->input_fifo)); - - if (s->current_app->req_pending) { - sha3_process(&s->ltc_state, s->current_app->req.msg_data, - s->current_app->req.msg_len); - s->current_app->req_pending = false; - if (s->current_app->req.last) { - /* append right-encoded output width if KMAC */ - if (cfg->mode == OT_KMAC_MODE_KMAC) { - uint8_t enc_out_len[3]; - uint32_t output_length = OT_KMAC_APP_DIGEST_BYTES * 8u; - enc_out_len[0] = (output_length >> 8u) & 0xffu; - enc_out_len[1] = output_length & 0xffu; - enc_out_len[2] = 2u; - sha3_process(&s->ltc_state, enc_out_len, - sizeof(enc_out_len)); + g_assert(cfg); + + if (s->current_app) { + /* App mode, FIFO should be empty */ + g_assert(fifo8_is_empty(&s->input_fifo)); + + if (s->current_app->req_pending) { + sha3_process(&s->ltc_state, s->current_app->req.msg_data, + s->current_app->req.msg_len); + s->current_app->req_pending = false; + if (s->current_app->req.last) { + /* append right-encoded output width if KMAC */ + if (cfg->mode == OT_KMAC_MODE_KMAC) { + uint8_t enc_out_len[3]; + uint32_t output_length = OT_KMAC_APP_DIGEST_BYTES * 8u; + enc_out_len[0] = (output_length >> 8u) & 0xffu; + enc_out_len[1] = output_length & 0xffu; + enc_out_len[2] = 2u; + sha3_process(&s->ltc_state, enc_out_len, + sizeof(enc_out_len)); + } + /* go to PROCESSING state, response will be sent there */ + ot_kmac_change_fsm_state(s, KMAC_ST_PROCESSING); + } else { + /* send empty response as acknowledge */ + if (s->current_app->fn) { + memset(&rsp, 0, sizeof(rsp)); + s->current_app->fn(s->current_app->opaque, &rsp); + } } - /* go to PROCESSING state, response will be sent there */ - ot_kmac_change_fsm_state(s, KMAC_ST_PROCESSING); - } else { - /* send empty response as acknowledge */ - if (s->current_app->fn) { - memset(&rsp, 0, sizeof(rsp)); - s->current_app->fn(s->current_app->opaque, &rsp); + } + } else { + /* SW mode, process FIFO data */ + if (!fifo8_is_empty(&s->input_fifo)) { + while (!fifo8_is_empty(&s->input_fifo)) { + uint8_t value = fifo8_pop(&s->input_fifo); + sha3_process(&s->ltc_state, &value, 1); } } } - } else { - /* SW mode, process FIFO data */ - if (!fifo8_is_empty(&s->input_fifo)) { - while (!fifo8_is_empty(&s->input_fifo)) { - uint8_t value = fifo8_pop(&s->input_fifo); - sha3_process(&s->ltc_state, &value, 1); - } - /* assert FIFO Empty interrupt */ - s->regs[R_INTR_STATE] |= INTR_FIFO_EMPTY_MASK; + switch (s->state) { + case KMAC_ST_PROCESSING: + case KMAC_ST_SQUEEZING: { + bool kmac_en = FIELD_EX32(ot_shadow_reg_peek(&s->cfg), CFG_SHADOWED, + KMAC_EN) != 0; + if (!s->current_app && + (s->edn_pending || + (kmac_en && s->entropy_configured && !s->entropy_seeded))) { + /* Stall until EDN or SW seed delivers requested entropy */ + break; + } } - } - - switch (s->state) { - case KMAC_ST_PROCESSING: - case KMAC_ST_SQUEEZING: - switch (cfg->mode) { - case OT_KMAC_MODE_SHA3: - sha3_done(&s->ltc_state, &s->keccak_state[0]); - break; - case OT_KMAC_MODE_SHAKE: + size_t strength = cfg->strength ? cfg->strength : 256u; sha3_shake_done(&s->ltc_state, &s->keccak_state[0], - ot_kmac_get_keccak_rate_bytes(cfg->strength)); - break; - /* NOLINTNEXTLINE(bugprone-branch-clone) */ - case OT_KMAC_MODE_CSHAKE: - case OT_KMAC_MODE_KMAC: - sha3_cshake_done(&s->ltc_state, &s->keccak_state[0], - ot_kmac_get_keccak_rate_bytes(cfg->strength)); - break; - default: + ot_kmac_get_keccak_rate_bytes(strength)); + /* - * should never happen: mode was validated when going from state - * IDLE to START + * "If key is sideloaded and KMAC is SW initiated, hide the capacity + * from SW by zeroing", i.e. if not doing a SW-initiated sideloaded + * operation, the entire Keccak state (including the meaningless + * capacity bytes) should be loaded. */ - g_assert_not_reached(); - } + uint32_t reg_cfg = ot_shadow_reg_peek(&s->cfg); + bool sideload = FIELD_EX32(reg_cfg, CFG_SHADOWED, SIDELOAD) != 0; + if (!sideload || s->current_app) { + static_assert( + sizeof(s->ltc_state.sha3.sb) == KECCAK_STATE_BYTES, + "LibTomCrypt's Keccak state is an unexpected size"); + /* manually extract entire Keccak state, including capacity */ + memcpy(&s->keccak_state[0], s->ltc_state.sha3.sb, + KECCAK_STATE_BYTES); + } - /* - * "If key is sideloaded and KMAC is SW initiated, hide the capacity - * from SW by zeroing", i.e. if not doing a SW-initiated sideloaded - * operation, the entire Keccak state (including the meaningless - * capacity bytes) should be loaded. - */ - uint32_t reg_cfg = ot_shadow_reg_peek(&s->cfg); - bool sideload = FIELD_EX32(reg_cfg, CFG_SHADOWED, SIDELOAD) != 0; - if (!sideload || s->current_app) { - static_assert(sizeof(s->ltc_state.sha3.sb) == KECCAK_STATE_BYTES, - "LibTomCrypt's Keccak state is an unexpected size"); - /* manually extract entire Keccak state, including capacity */ - memcpy(&s->keccak_state[0], s->ltc_state.sha3.sb, - KECCAK_STATE_BYTES); - } + if (s->current_app) { + /* App mode, send response and go back to IDLE state */ + if (s->current_app->fn) { + rsp.done = true; + for (unsigned w = 0; + w < sizeof(rsp.digest_share0) / sizeof(uint32_t); + w++) { + uint32_t word32 = + ldl_le_p(&s->keccak_state[w * sizeof(uint32_t)]); + uint32_t mask = ot_kmac_state_mask_word(s, w); + stl_le_p(&rsp.digest_share0[w * sizeof(uint32_t)], + word32 ^ mask); + stl_le_p(&rsp.digest_share1[w * sizeof(uint32_t)], + mask); + } + s->current_app->fn(s->current_app->opaque, &rsp); + } + if (!s->error_awaiting_sw) { + ot_kmac_complete_app_req(s); + } + } else { + /* SW mode, go to ABSORBED state */ + ot_kmac_change_fsm_state(s, KMAC_ST_ABSORBED); - if (s->current_app) { - /* App mode, send response and go back to IDLE state */ - if (s->current_app->fn) { - rsp.done = true; - memcpy(&rsp.digest_share0[0], &s->keccak_state[0], - sizeof(rsp.digest_share0)); - memset(&rsp.digest_share1[0], 0, sizeof(rsp.digest_share1)); - s->current_app->fn(s->current_app->opaque, &rsp); + /* assert KMAC Done interrupt */ + s->regs[R_INTR_STATE] |= INTR_KMAC_DONE_MASK; + s->regs[R_INTR_STATE] &= ~INTR_FIFO_EMPTY_MASK; } - if (!s->error_awaiting_sw) { - ot_kmac_complete_app_req(s); - } - } else { - /* SW mode, go to ABSORBED state */ - ot_kmac_change_fsm_state(s, KMAC_ST_ABSORBED); - /* assert KMAC Done interrupt */ - s->regs[R_INTR_STATE] |= INTR_KMAC_DONE_MASK; + break; + default: + /* nothing to do for other states */ + break; } - break; - default: - /* nothing to do for other states */ - break; - } + ot_kmac_update_irq(s); + } while (s->current_app && s->current_app->req_pending); - ot_kmac_update_irq(s); + s->in_process = false; } static inline bool ot_kmac_config_enabled(const OtKMACState *s) @@ -856,83 +973,6 @@ static bool ot_kmac_check_mode_and_strength(const OtKMACAppCfg *cfg) } } -static inline uint8_t -ot_kmac_get_prefix_byte(const OtKMACState *s, size_t offset) -{ - size_t reg = offset / sizeof(uint32_t); - size_t byteoffset = offset - reg * sizeof(uint32_t); - - if (reg >= NUM_PREFIX_REGS) { - /* - * Just return 0, bounds checking should have been done by the caller - * anyway. - */ - return 0; - } - - return (uint8_t)(s->regs[R_PREFIX_0 + reg] >> (byteoffset * 8u)); -} - -static size_t ot_kmac_left_decode(const OtKMACState *s, size_t offset, - size_t *value) -{ - size_t len; - size_t val = 0; - - /* first byte is the length in bytes of encoded value */ - len = (size_t)ot_kmac_get_prefix_byte(s, offset); - if (len < 1u || len > 4u) { - return 0; - } - - /* compute value */ - for (size_t ix = 1u; ix < len + 1u; ix++) { - val = (val << 8u) | ot_kmac_get_prefix_byte(s, offset + ix); - } - - *value = val; - return 1u + len; -} - -static bool ot_kmac_decode_sw_prefix(OtKMACState *s) -{ - size_t offset = 0; - size_t used; - OtKMACPrefix *prefix = &s->sw_cfg.prefix; - - used = ot_kmac_left_decode(s, offset, &prefix->funcname_len); - prefix->funcname_len /= 8u; - offset += used; - - if (prefix->funcname_len > OT_KMAC_PREFIX_FUNCNAME_LEN) { - goto error; - } - for (size_t ix = 0; ix < prefix->funcname_len; ix++) { - prefix->funcname[ix] = ot_kmac_get_prefix_byte(s, offset + ix); - } - offset += prefix->funcname_len; - - used = ot_kmac_left_decode(s, offset, &prefix->customstr_len); - prefix->customstr_len /= 8u; - offset += used; - - if (prefix->customstr_len > OT_KMAC_PREFIX_CUSTOMSTR_LEN) { - goto error; - } - for (size_t ix = 0; ix < prefix->customstr_len; ix++) { - prefix->customstr[ix] = ot_kmac_get_prefix_byte(s, offset + ix); - } - offset += prefix->funcname_len; - - if (offset <= NUM_PREFIX_REGS * sizeof(uint32_t)) { - return true; - } - -error: - memset(prefix, 0, sizeof(OtKMACPrefix)); - return false; -} - static bool ot_kmac_check_kmac_sw_prefix(const OtKMACState *s) { /* @@ -949,70 +989,123 @@ static void ot_kmac_process_start(OtKMACState *s) g_assert(cfg); + size_t strength = cfg->strength ? cfg->strength : 256u; + size_t rate = ot_kmac_get_keccak_rate_bytes(strength); + memset(&s->ltc_state.sha3, 0, sizeof(s->ltc_state.sha3)); + s->ltc_state.sha3.capacity_words = + (unsigned short)(2u * strength / (8u * sizeof(uint64_t))); + + /* + * RTL (sha3pad.sv:558-567): + * Sha3 -> 5'b00110 (0x06) + * Shake -> 5'b11111 (0x1f) + * CShake -> 5'b00100 (0x04) + * default (mode=1) -> 5'b00001 (0x01) + */ switch (cfg->mode) { case OT_KMAC_MODE_SHA3: - switch (cfg->strength) { - case 224u: - sha3_224_init(&s->ltc_state); - break; - case 256u: - sha3_256_init(&s->ltc_state); - break; - case 384u: - sha3_384_init(&s->ltc_state); - break; - case 512u: - sha3_512_init(&s->ltc_state); - break; - default: - /* should never happen: strength was already validated earlier */ - g_assert_not_reached(); - } + s->ltc_state.sha3.suffix = 0x06u; break; case OT_KMAC_MODE_SHAKE: - switch (cfg->strength) { - case 128u: - case 256u: - sha3_shake_init(&s->ltc_state, (int)cfg->strength); - break; - default: - /* should never happen: strength was already validated earlier */ - g_assert_not_reached(); - } + s->ltc_state.sha3.suffix = 0x1fu; break; case OT_KMAC_MODE_CSHAKE: case OT_KMAC_MODE_KMAC: - switch (cfg->strength) { - case 128u: - case 256u: { - sha3_cshake_init(&s->ltc_state, (int)cfg->strength, - cfg->prefix.funcname, cfg->prefix.funcname_len, - cfg->prefix.customstr, cfg->prefix.customstr_len); - /* if KMAC mode is enabled, process key */ - if (cfg->mode == OT_KMAC_MODE_KMAC) { - uint8_t key[NUM_KEY_REGS * sizeof(uint32_t)]; - size_t keylen = 0; - static_assert(OT_KMAC_KEY_SIZE <= ARRAY_SIZE(key), - "key buffer too small to hold sideloaded key"); - ot_kmac_get_key(s, key, &keylen); - sha3_process_kmac_key(&s->ltc_state, key, keylen); + s->ltc_state.sha3.suffix = 0x04u; + if (s->current_app) { + sha3_cshake_init(&s->ltc_state, (int)strength, cfg->prefix.funcname, + cfg->prefix.funcname_len, cfg->prefix.customstr, + cfg->prefix.customstr_len); + } else { + /* + * RTL (sha3pad.sv:236, 327-358, 509-533): + * When mode_i == CShake, StPrefix unconditionally absorbs one + * rate-byte block {ns_data_i, encode_bytepad} zero-padded to rate. + */ + uint8_t prefix_blk[168]; + memset(prefix_blk, 0, sizeof(prefix_blk)); + if (cfg->strength != 0) { + prefix_blk[0] = 0x01u; + prefix_blk[1] = (uint8_t)rate; } - break; - } - default: - /* should never happen: strength was already validated earlier */ - g_assert_not_reached(); + for (size_t ix = 0; ix < NUM_PREFIX_REGS; ix++) { + stl_le_p(&prefix_blk[2u + ix * 4u], s->regs[R_PREFIX_0 + ix]); + } + sha3_process(&s->ltc_state, prefix_blk, rate); } break; default: - /* should never happen: mode was already validated earlier */ - g_assert_not_reached(); + s->ltc_state.sha3.suffix = 0x01u; + break; + } + + /* + * RTL (kmac_core.sv:180, 286-376): + * StKey absorbs {encoded_key, encode_bytepad} whenever kmac_en_i is set. + */ + bool kmac_en = + s->current_app ? + (cfg->mode == OT_KMAC_MODE_KMAC) : + (FIELD_EX32(ot_shadow_reg_peek(&s->cfg), CFG_SHADOWED, KMAC_EN) != + 0); + if (kmac_en) { + uint8_t key[NUM_KEY_REGS * sizeof(uint32_t)]; + size_t keylen = 0; + static_assert(OT_KMAC_KEY_SIZE <= ARRAY_SIZE(key), + "key buffer too small to hold sideloaded key"); + ot_kmac_get_key(s, key, &keylen); + + uint8_t key_blk[168]; + memset(key_blk, 0, sizeof(key_blk)); + if (cfg->strength != 0) { + key_blk[0] = 0x01u; + key_blk[1] = (uint8_t)rate; + } + if (keylen > 0) { + if (keylen <= 24u) { + key_blk[2] = 0x01u; + key_blk[3] = (uint8_t)(keylen * 8u); + memcpy(&key_blk[4], key, keylen); + } else { + key_blk[2] = 0x02u; + key_blk[3] = (uint8_t)((keylen * 8u) >> 8u); + key_blk[4] = (uint8_t)((keylen * 8u) & 0xffu); + memcpy(&key_blk[5], key, keylen); + } + } + sha3_process(&s->ltc_state, key_blk, rate); + + s->regs[R_ENTROPY_REFRESH_HASH_CNT] = + (s->regs[R_ENTROPY_REFRESH_HASH_CNT] + 1u) & + R_ENTROPY_REFRESH_HASH_CNT_HASH_CNT_MASK; + uint32_t threshold = ot_shadow_reg_peek(&s->entropy_refresh_threshold); + if (threshold > 0 && s->regs[R_ENTROPY_REFRESH_HASH_CNT] >= threshold) { + s->regs[R_ENTROPY_REFRESH_HASH_CNT] = 0; + if (s->entropy_configured) { + ot_kmac_request_entropy(s); + } + } } } static void ot_kmac_sw_err_processed(OtKMACState *s, int cmd) { + timer_del(s->edn_wait_timer); s->error_awaiting_sw = false; + s->cfg_entropy_ready = false; + s->edn_pending = false; + if (s->entropy_in_err) { + /* + * RTL (kmac_entropy.sv:710): StRandErr transitions back to StRandReset + * on err_processed_i. If kmac_entropy was already in StRandReady or + * StSwSeedWait, err_processed_i does NOT reset st or mode_q. + */ + s->entropy_in_err = false; + s->entropy_configured = false; + s->entropy_mode = 0; + s->entropy_seeded = false; + s->sw_seed_cnt = 0; + } if (s->current_app) { /* @@ -1022,13 +1115,21 @@ static void ot_kmac_sw_err_processed(OtKMACState *s, int cmd) * If we haven't got all the data, we should wait for it all before * sending a response (but we store SW acknowledgement). */ + s->app_in_error = false; if (s->state == KMAC_ST_PROCESSING || s->state == KMAC_ST_SQUEEZING) { ot_kmac_complete_app_req(s); } - } else { - /* for SW: ignore further msg feed, absorb, report done & go to idle */ + s->regs[R_INTR_STATE] |= INTR_KMAC_DONE_MASK; + ot_kmac_update_irq(s); + } else if (s->app_in_error) { + /* + * RTL (kmac_app.sv:520-611, 646-648): When kmac_app entered StError / + * StErrorAwaitSw during a SW operation (e.g. ErrKeyNotValid), + * err_processed_i flushes the SHA3 engine via CmdProcess -> + * StErrorWaitAbsorbed -> CmdDone + absorbed_o = MuBi4True -> StIdle. + */ + s->app_in_error = false; switch (s->state) { - case KMAC_ST_IDLE: case KMAC_ST_MSG_FEED: ot_kmac_change_fsm_state(s, KMAC_ST_PROCESSING); /* fallthrough */ @@ -1036,6 +1137,7 @@ static void ot_kmac_sw_err_processed(OtKMACState *s, int cmd) case KMAC_ST_SQUEEZING: ot_kmac_process((void *)s); /* fallthrough */ + case KMAC_ST_IDLE: case KMAC_ST_ABSORBED: ot_kmac_return_to_idle(s); break; @@ -1044,27 +1146,30 @@ static void ot_kmac_sw_err_processed(OtKMACState *s, int cmd) default: g_assert_not_reached(); } + s->regs[R_INTR_STATE] |= INTR_KMAC_DONE_MASK; + ot_kmac_update_irq(s); } /* Clear the status / alert */ s->regs[R_STATUS] &= ~R_STATUS_ALERT_RECOV_CTRL_UPDATE_ERR_MASK; ot_kmac_update_alert(s); - /* Clear the error */ - s->regs[R_ERR_CODE] = 0u; + /* + * Note: In RTL (kmac.sv:684, kmac_reg_top.sv:2563-2587), u_err_code is a + * SwAccessRO prim_subreg gated solely by hw2reg.err_code.de = event_error. + * CMD.err_processed does NOT clear ERR_CODE. + */ - /* SW shoud not send a command along with the err_processed bit */ + /* + * In RTL (kmac_app.sv:955), app_active_o is 0 in StError / StErrorAwaitSw, + * so sending a command along with err_processed_i reports ErrSwCmdSequence + * via kmac_errchk.sv:186-234. + */ if (cmd != OT_KMAC_CMD_NONE) { - if (s->current_app) { - ot_kmac_report_error(s, OT_KMAC_ERR_SW_ISSUED_CMD_IN_APP_ACTIVE, - cmd); - } else { - /* see error encoding in (hw/ip/kmac/rtl/kmac_pkg.sv) */ - uint32_t info = (1 << 11u); - info |= (uint32_t)s->state << 8u; - info |= cmd; - ot_kmac_report_error(s, OT_KMAC_ERR_SW_CMD_SEQUENCE, info); - } + uint32_t info = (1u << 18u); + info |= (uint32_t)s->state << 8u; + info |= cmd; + ot_kmac_report_error(s, OT_KMAC_ERR_SW_CMD_SEQUENCE, info); } } @@ -1073,15 +1178,8 @@ static void ot_kmac_process_sw_command(OtKMACState *s, uint32_t cmd_reg) { bool err_processed = (cmd_reg & R_CMD_ERR_PROCESSED_MASK) != 0; int cmd = (int)FIELD_EX32(cmd_reg, CMD, CMD); - if (s->error_awaiting_sw) { - if (err_processed) { - ot_kmac_sw_err_processed(s, cmd); - } else { - qemu_log_mask(LOG_GUEST_ERROR, - "%s: %s: Control write without err_processed whilst " - "KMAC is handling an error\n", - __func__, s->ot_id); - } + if (err_processed) { + ot_kmac_sw_err_processed(s, cmd); return; } @@ -1107,31 +1205,30 @@ static void ot_kmac_process_sw_command(OtKMACState *s, uint32_t cmd_reg) /* retrieve configuration from CFG_SHADOWED register */ ot_kmac_get_sw_config(s); + bool kmac_en = FIELD_EX32(cfg, CFG_SHADOWED, KMAC_EN) != 0; + bool en_unsupported_modestrength = + FIELD_EX32(cfg, CFG_SHADOWED, EN_UNSUPPORTED_MODESTRENGTH) != 0; if (!ot_kmac_check_mode_and_strength(&s->sw_cfg)) { err_modestrength = true; - break; } - /* if KMAC mode, check prefix & entropy ready */ - if (s->sw_cfg.mode == OT_KMAC_MODE_KMAC) { + /* + * In RTL (kmac_errchk.sv:268, 288), check_prefix and + * check_entropy_ready check kmac_en_i independently of cfg_mode_i. + */ + if (kmac_en) { if (!ot_kmac_check_kmac_sw_prefix(s)) { err_prefix = true; - break; } - if (false /* TODO: check entropy ready */) { + if (!s->cfg_entropy_ready) { err_entropy_ready = true; - break; } } - /* if cSHAKE or KMAC modes, decode prefix from PREFIX_x registers */ - if (s->sw_cfg.mode == OT_KMAC_MODE_CSHAKE || - s->sw_cfg.mode == OT_KMAC_MODE_KMAC) { - if (!ot_kmac_decode_sw_prefix(s)) { - qemu_log_mask(LOG_GUEST_ERROR, - "%s: %s: could not decode cSHAKE prefix, " - "digest result will be wrong!\n", - __func__, s->ot_id); - memset(&s->sw_cfg.prefix, 0, sizeof(s->sw_cfg.prefix)); - } + if ((err_modestrength && !en_unsupported_modestrength) || + err_entropy_ready) { + break; + } + if (kmac_en && !s->entropy_seeded) { + ot_kmac_request_entropy(s); } s->current_cfg = &s->sw_cfg; @@ -1145,6 +1242,7 @@ static void ot_kmac_process_sw_command(OtKMACState *s, uint32_t cmd_reg) if (cmd == OT_KMAC_CMD_NONE) { /* nothing to do */ } else if (cmd == OT_KMAC_CMD_PROCESS) { + s->regs[R_INTR_STATE] &= ~INTR_FIFO_EMPTY_MASK; ot_kmac_change_fsm_state(s, KMAC_ST_PROCESSING); ot_kmac_trigger_deferred_bh(s); } else { @@ -1185,12 +1283,16 @@ static void ot_kmac_process_sw_command(OtKMACState *s, uint32_t cmd_reg) uint8_t code; uint32_t info = 0; /* - * error encoding is not documented, reference is OpenTitan RTL - * (hw/ip/kmac/rtl/kmac_pkg.sv) + * error encoding in OpenTitan RTL + * (hw/ip/kmac/rtl/kmac_errchk.sv:325-368): For err_swsequence, + * err_modestrength, and err_prefix: info[23:16] = {5'h0, + * err_swsequence, err_modestrength, err_prefix} For err_entropy_ready: + * info[23:16] = {4'h0, err_entropy_ready, err_swsequence, + * err_modestrength, err_prefix} */ - info |= err_swsequence ? 1 << 11u : 0; - info |= err_modestrength ? 1 << 10u : 0; - info |= err_prefix ? 1 << 9u : 0; + info |= err_swsequence ? (1u << 18u) : 0; + info |= err_modestrength ? (1u << 17u) : 0; + info |= err_prefix ? (1u << 16u) : 0; if (err_swsequence) { info |= (uint32_t)s->state << 8u; info |= cmd; @@ -1202,15 +1304,14 @@ static void ot_kmac_process_sw_command(OtKMACState *s, uint32_t cmd_reg) } else if (err_prefix) { code = OT_KMAC_ERR_INCORRECT_FUNCTION_NAME; } else if (err_entropy_ready) { - info |= err_entropy_ready ? 1 << 12u : 0; - info |= FIELD_EX32(cfg, CFG_SHADOWED, KMAC_EN) ? 1 << 1u : 0; + info |= (1u << 19u); + info |= FIELD_EX32(cfg, CFG_SHADOWED, KMAC_EN) ? (1u << 1u) : 0; + info |= s->cfg_entropy_ready ? 1u : 0; code = OT_KMAC_ERR_SW_HASHING_WITHOUT_ENTROPY_READY; } else { g_assert_not_reached(); } ot_kmac_report_error(s, code, info); - } else if (!s->error_awaiting_sw) { - s->regs[R_ERR_CODE] = 0; } } @@ -1240,12 +1341,14 @@ static uint64_t ot_kmac_regs_read(void *opaque, hwaddr addr, unsigned size) val32 = ot_shadow_reg_read(&s->cfg); break; case R_STATUS: - val32 = 0u; + val32 = s->regs[R_STATUS] & (R_STATUS_ALERT_FATAL_FAULT_MASK | + R_STATUS_ALERT_RECOV_CTRL_UPDATE_ERR_MASK); switch (s->state) { case KMAC_ST_IDLE: val32 |= R_STATUS_SHA3_IDLE_MASK; break; case KMAC_ST_MSG_FEED: + case KMAC_ST_PROCESSING: val32 |= R_STATUS_SHA3_ABSORB_MASK; break; case KMAC_ST_ABSORBED: @@ -1269,6 +1372,8 @@ static uint64_t ot_kmac_regs_read(void *opaque, hwaddr addr, unsigned size) val32 = ot_shadow_reg_read(&s->entropy_refresh_threshold); break; case R_INTR_STATE: + val32 = s->regs[R_INTR_STATE] | s->regs[R_INTR_TEST]; + break; case R_INTR_ENABLE: case R_ENTROPY_PERIOD: case R_ENTROPY_REFRESH_HASH_CNT: @@ -1358,7 +1463,8 @@ static void ot_kmac_regs_write(void *opaque, hwaddr addr, uint64_t value, switch (reg) { case R_INTR_STATE: - s->regs[R_INTR_STATE] &= ~(val32 & INTR_MASK); + s->regs[R_INTR_STATE] &= + ~(val32 & (INTR_KMAC_DONE_MASK | INTR_KMAC_ERR_MASK)); ot_kmac_update_irq(s); break; case R_INTR_ENABLE: @@ -1366,7 +1472,9 @@ static void ot_kmac_regs_write(void *opaque, hwaddr addr, uint64_t value, ot_kmac_update_irq(s); break; case R_INTR_TEST: - s->regs[R_INTR_STATE] |= val32 & INTR_MASK; + s->regs[R_INTR_TEST] = val32 & INTR_FIFO_EMPTY_MASK; + s->regs[R_INTR_STATE] |= + val32 & (INTR_KMAC_DONE_MASK | INTR_KMAC_ERR_MASK); ot_kmac_update_irq(s); break; case R_ALERT_TEST: @@ -1378,13 +1486,6 @@ static void ot_kmac_regs_write(void *opaque, hwaddr addr, uint64_t value, break; } - /* check for unimplemented config bits */ - if (val32 & R_CFG_SHADOWED_ENTROPY_MODE_MASK) { - qemu_log_mask( - LOG_UNIMP, - "%s: %s: CFG_SHADOWED.ENTROPY_MODE is not supported\n", - __func__, s->ot_id); - } if (val32 & R_CFG_SHADOWED_ENTROPY_FAST_PROCESS_MASK) { qemu_log_mask(LOG_UNIMP, "%s: %s: CFG_SHADOWED.ENTROPY_FAST_PROCESS is not " @@ -1396,23 +1497,34 @@ static void ot_kmac_regs_write(void *opaque, hwaddr addr, uint64_t value, "%s: %s: CFG_SHADOWED.MSG_MASK is not supported\n", __func__, s->ot_id); } - if (val32 & R_CFG_SHADOWED_ENTROPY_READY_MASK) { - qemu_log_mask(LOG_UNIMP, - "%s: %s: CFG_SHADOWED.ENTROPY_READY is not " - "supported\n", - __func__, s->ot_id); - } - if (val32 & R_CFG_SHADOWED_EN_UNSUPPORTED_MODESTRENGTH_MASK) { - qemu_log_mask(LOG_UNIMP, - "%s: %s: CFG_SHADOWED.EN_UNSUPPORTED_MODESTRENGTH is " - "not supported\n", - __func__, s->ot_id); - } val32 &= CFG_MASK; switch (ot_shadow_reg_write(&s->cfg, val32)) { case OT_SHADOW_REG_STAGED: + break; case OT_SHADOW_REG_COMMITTED: + if (val32 & R_CFG_SHADOWED_ENTROPY_READY_MASK) { + if (s->state == KMAC_ST_IDLE) { + s->cfg_entropy_ready = true; + } + if (!s->entropy_configured) { + s->entropy_configured = true; + s->entropy_mode = + (uint8_t)FIELD_EX32(val32, CFG_SHADOWED, ENTROPY_MODE); + s->entropy_seeded = false; + s->sw_seed_cnt = 0; + if (s->entropy_mode != 1u && s->entropy_mode != 2u) { + s->entropy_in_err = true; + s->entropy_seeded = true; + ot_kmac_report_error(s, + OT_KMAC_ERR_INCORRECT_ENTROPY_MODE, + s->entropy_mode); + } else if (s->entropy_mode == 1u && + FIELD_EX32(val32, CFG_SHADOWED, KMAC_EN) != 0) { + ot_kmac_request_entropy(s); + } + } + } break; case OT_SHADOW_REG_ERROR: default: @@ -1425,17 +1537,12 @@ static void ot_kmac_regs_write(void *opaque, hwaddr addr, uint64_t value, ot_kmac_process_sw_command(s, val32); if (val32 & R_CMD_ENTROPY_REQ_MASK) { - /* TODO: implement entropy */ - qemu_log_mask(LOG_UNIMP, - "%s: %s: CMD.ENTROPY_REQ is not supported\n", - __func__, s->ot_id); + s->regs[R_ENTROPY_REFRESH_HASH_CNT] = 0; + ot_kmac_request_entropy(s); } if (val32 & R_CMD_HASH_CNT_CLR_MASK) { - /* TODO: implement entropy */ - qemu_log_mask(LOG_UNIMP, - "%s: %s: CMD.HASH_CNT_CLR is not supported\n", - __func__, s->ot_id); + s->regs[R_ENTROPY_REFRESH_HASH_CNT] = 0; } break; } @@ -1456,7 +1563,14 @@ static void ot_kmac_regs_write(void *opaque, hwaddr addr, uint64_t value, val32 &= R_ENTROPY_REFRESH_THRESHOLD_SHADOWED_THRESHOLD_MASK; switch (ot_shadow_reg_write(&s->entropy_refresh_threshold, val32)) { case OT_SHADOW_REG_STAGED: + break; case OT_SHADOW_REG_COMMITTED: + if (val32 > 0 && s->regs[R_ENTROPY_REFRESH_HASH_CNT] >= val32) { + s->regs[R_ENTROPY_REFRESH_HASH_CNT] = 0; + if (s->entropy_configured) { + ot_kmac_request_entropy(s); + } + } break; case OT_SHADOW_REG_ERROR: default: @@ -1466,9 +1580,22 @@ static void ot_kmac_regs_write(void *opaque, hwaddr addr, uint64_t value, } break; case R_ENTROPY_SEED: - /* TODO: implement entropy */ - qemu_log_mask(LOG_UNIMP, "%s: %s: R_ENTROPY_SEED_* is not supported\n", - __func__, s->ot_id); + if (s->entropy_configured && s->entropy_mode == 2u && + !s->entropy_seeded) { + s->sw_seed_cnt++; + /* + * RTL (kmac_entropy.sv:357, prim_trivium.sv:72,104): + * BiviumStateWidth = 177, PartialSeedWidth = 32 -> NumStateParts + * = 6. + */ + if (s->sw_seed_cnt >= 6u) { + s->entropy_seeded = true; + if (s->state == KMAC_ST_PROCESSING || + s->state == KMAC_ST_SQUEEZING) { + ot_kmac_process(s); + } + } + } break; case R_KEY_LEN: if (!ot_kmac_check_reg_write(s, reg)) { @@ -1547,6 +1674,7 @@ static void ot_kmac_regs_write(void *opaque, hwaddr addr, uint64_t value, static uint64_t ot_kmac_state_read(void *opaque, hwaddr addr, unsigned size) { OtKMACState *s = OT_KMAC(opaque); + (void)size; uint32_t val32; if (s->state != KMAC_ST_ABSORBED) { @@ -1571,31 +1699,27 @@ static uint64_t ot_kmac_state_read(void *opaque, hwaddr addr, unsigned size) s->invalid_state_read = false; /* compute share index */ - while (offset > KECCAK_STATE_SHARE_BYTES) { + while (offset >= KECCAK_STATE_SHARE_BYTES) { offset -= KECCAK_STATE_SHARE_BYTES; share++; } switch (share) { case 0: - if (addr + size <= KECCAK_STATE_BYTES) { - val32 = 0; - for (unsigned ix = 0; ix < size; ix++) { - size_t byte_offset = byteswap ? ix : size - 1 - ix; - val32 = - (val32 << 8u) + s->keccak_state[offset + byte_offset]; + case 1: + if (offset + 4u <= KECCAK_STATE_BYTES) { + uint32_t word32 = ldl_le_p(&s->keccak_state[offset]); + uint32_t mask = + ot_kmac_state_mask_word(s, (unsigned)(offset >> 2u)); + uint32_t share_word = (share == 0) ? (word32 ^ mask) : mask; + if (byteswap) { + share_word = bswap32(share_word); } + val32 = share_word; } else { val32 = 0; } break; - case 1: - /* - * TODO: implement masking. Current version returns unmasked state - * in first share and zeros in second one. - */ - val32 = 0; - break; default: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: bad offset 0x%02x\n", __func__, s->ot_id, (uint32_t)addr); @@ -1610,27 +1734,24 @@ static uint64_t ot_kmac_state_read(void *opaque, hwaddr addr, unsigned size) return (uint64_t)val32; } -static void ot_kmac_state_write(void *opaque, hwaddr addr, uint64_t value, - unsigned size) +static bool ot_kmac_state_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) { - OtKMACState *s = OT_KMAC(opaque); + (void)opaque; (void)addr; - (void)value; (void)size; - /* on real hardware, writes to STATE are ignored */ - qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: STATE is read only\n", __func__, - s->ot_id); + (void)attrs; + return !is_write; } -static uint64_t ot_kmac_msgfifo_read(void *opaque, hwaddr addr, unsigned size) +static bool ot_kmac_msgfifo_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) { - OtKMACState *s = OT_KMAC(opaque); + (void)opaque; (void)addr; (void)size; - /* on real hardware, writes to FIFO will block. Let's just return 0. */ - qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: MSG_FIFO is write only\n", __func__, - s->ot_id); - return 0; + (void)attrs; + return is_write; } static void ot_kmac_msgfifo_write(void *opaque, hwaddr addr, uint64_t value, @@ -1644,15 +1765,26 @@ static void ot_kmac_msgfifo_write(void *opaque, hwaddr addr, uint64_t value, /* trigger error if an app is running or not in MSG_FEED state */ if (s->current_app || s->state != KMAC_ST_MSG_FEED) { - /* info field mux_sel=1 (SW) or 2 (App) */ - ot_kmac_report_error(s, OT_KMAC_ERR_SW_PUSHED_MSG_FIFO, - s->current_app ? 2 : 1); + /* + * RTL (kmac_app.sv:200, 219, 728-734; kmac_pkg.sv:295-296): + * info = {8'h00, 8'(st), 8'(mux_sel_buf_err_check)}. + * In StIdle (10'b1010111110 = 0x2be -> 8'(st) = 0xbe) with + * SelNone (5'b10100 = 0x14), info = 0x00be14. + * In StAppMsg (10'b1110001011 = 0x38b -> 8'(st) = 0x8b) with + * SelApp (5'b11001 = 0x19), info = 0x008b19. + */ + uint32_t err_info = s->current_app ? 0x008b19u : 0x00be14u; + ot_kmac_report_error(s, OT_KMAC_ERR_SW_PUSHED_MSG_FIFO, err_info); return; } uint32_t cfg = ot_shadow_reg_peek(&s->cfg); bool byteswap = FIELD_EX32(cfg, CFG_SHADOWED, MSG_ENDIANNESS) != 0; + if (fifo8_is_empty(&s->input_fifo)) { + s->regs[R_INTR_STATE] &= ~INTR_FIFO_EMPTY_MASK; + } + if (fifo8_num_free(&s->input_fifo) < size) { /* * Not enough room in FIFO. Real hardware would fill the FIFO and stall @@ -1747,8 +1879,12 @@ static void ot_kmac_start_pending_app(OtKMACState *s) ot_kmac_process_start(s); ot_kmac_change_fsm_state(s, KMAC_ST_MSG_FEED); - /* trigger deferred compute */ - qemu_bh_schedule(s->bh); + /* compute app request */ + if (app_idx == 0u) { + ot_kmac_trigger_deferred_bh(s); + } else { + qemu_bh_schedule(s->bh); + } } } @@ -1782,8 +1918,8 @@ static void ot_kmac_app_request(OtKMACState *s, unsigned app_idx, /* check if app already started */ if (s->current_app == app && (s->state == KMAC_ST_IDLE || s->state == KMAC_ST_MSG_FEED)) { - /* yes, receiving more data, trigger deferred compute */ - qemu_bh_schedule(s->bh); + /* yes, receiving more data, compute app request */ + ot_kmac_process(s); } else { /* no, mark as pending and try to start */ s->pending_apps |= (1u << app_idx); @@ -1801,33 +1937,69 @@ static const Property ot_kmac_properties[] = { DEFINE_PROP_UINT8("num-app", OtKMACState, num_app, 0), }; +static const uint8_t KMAC_PERMIT[REGS_COUNT] = { + [R_INTR_STATE] = 0x1u, + [R_INTR_ENABLE] = 0x1u, + [R_INTR_TEST] = 0x1u, + [R_ALERT_TEST] = 0x1u, + [R_CFG_REGWEN] = 0x1u, + [R_CFG_SHADOWED] = 0xfu, + [R_CMD] = 0x3u, + [R_STATUS] = 0x7u, + [R_ENTROPY_PERIOD] = 0xfu, + [R_ENTROPY_REFRESH_HASH_CNT] = 0x3u, + [R_ENTROPY_REFRESH_THRESHOLD_SHADOWED] = 0x3u, + [R_ENTROPY_SEED] = 0xfu, + [R_KEY_SHARE0_0... R_KEY_SHARE0_15] = 0xfu, + [R_KEY_SHARE1_0... R_KEY_SHARE1_15] = 0xfu, + [R_KEY_LEN] = 0x1u, + [R_PREFIX_0... R_PREFIX_10] = 0xfu, + [R_ERR_CODE] = 0xfu, +}; + +static bool ot_kmac_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + uint32_t reg = R32_OFF(addr); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return reg < REGS_COUNT && (KMAC_PERMIT[reg] & ~reg_be) == 0u; +} + static const MemoryRegionOps ot_kmac_regs_ops = { .read = &ot_kmac_regs_read, .write = &ot_kmac_regs_write, .endianness = DEVICE_NATIVE_ENDIAN, - .valid = { - .min_access_size = 4u, - .max_access_size = 4u, - }, + .impl.min_access_size = 4u, + .impl.max_access_size = 4u, + .valid.accepts = &ot_kmac_regs_accepts, }; static const MemoryRegionOps ot_kmac_state_ops = { .read = &ot_kmac_state_read, - .write = &ot_kmac_state_write, .endianness = DEVICE_NATIVE_ENDIAN, + .impl = { + .min_access_size = 4u, + .max_access_size = 4u, + }, .valid = { .min_access_size = 1u, .max_access_size = 4u, + .accepts = &ot_kmac_state_accepts, }, }; static const MemoryRegionOps ot_kmac_msgfifo_ops = { - .read = &ot_kmac_msgfifo_read, .write = &ot_kmac_msgfifo_write, .endianness = DEVICE_NATIVE_ENDIAN, .valid = { .min_access_size = 1u, .max_access_size = 4u, + .accepts = &ot_kmac_msgfifo_accepts, }, }; @@ -1847,9 +2019,23 @@ static void ot_kmac_reset_enter(Object *obj, ResetType type) memset(&s->sw_cfg, 0, sizeof(OtKMACAppCfg)); s->current_app = NULL; s->pending_apps = 0; + for (unsigned ix = 0; ix < s->num_app; ix++) { + s->apps[ix].req_pending = false; + memset(&s->apps[ix].req, 0, sizeof(OtKMACAppReq)); + } + s->in_process = false; s->invalid_state_read = false; s->error_awaiting_sw = false; - memset(s->regs, 0, sizeof(*(s->regs))); + s->app_in_error = false; + s->cfg_entropy_ready = false; + s->entropy_configured = false; + s->entropy_in_err = false; + s->entropy_mode = 0; + s->sw_seed_cnt = 0; + s->entropy_seeded = false; + s->edn_pending = false; + s->lc_escalate_en = false; + memset(s->regs, 0, REGS_SIZE); s->regs[R_STATUS] = 0x4001u; ot_shadow_reg_init(&s->cfg, 0u); ot_shadow_reg_init(&s->entropy_refresh_threshold, 0u); @@ -1884,6 +2070,23 @@ static void ot_kmac_reset_enter(Object *obj, ResetType type) } } +static void ot_kmac_lc_escalate_en(void *opaque, int irq, int level) +{ + OtKMACState *s = opaque; + + g_assert(irq == 0); + + s->lc_escalate_en = (bool)level; + if (s->lc_escalate_en) { + ot_kmac_change_fsm_state(s, KMAC_ST_TERMINAL_ERROR); + s->app_in_error = true; + s->entropy_in_err = true; + ot_kmac_cancel_bh(s); + s->regs[R_STATUS] |= R_STATUS_ALERT_FATAL_FAULT_MASK; + ot_kmac_update_alert(s); + } +} + static void ot_kmac_realize(DeviceState *dev, Error **errp) { OtKMACState *s = OT_KMAC(dev); @@ -1901,8 +2104,14 @@ static void ot_kmac_realize(DeviceState *dev, Error **errp) s->apps = g_new0(OtKMACApp, s->num_app); + if (s->edn && s->edn_ep != UINT8_MAX) { + ot_edn_connect_endpoint(s->edn, s->edn_ep, &ot_kmac_fill_entropy, s); + } + qdev_init_gpio_in_named(DEVICE(s), &ot_kmac_clock_input, OT_KMAC_CLOCK_INPUT, 1); + qdev_init_gpio_in_named(DEVICE(s), &ot_kmac_lc_escalate_en, + OT_KMAC_LC_ESCALATE_EN, 1); } static void ot_kmac_init(Object *obj) @@ -1938,6 +2147,8 @@ static void ot_kmac_init(Object *obj) /* setup deferred processing */ s->bh_timer = timer_new_ns(OT_VIRTUAL_CLOCK, &ot_kmac_bh_timer_handler, s); + s->edn_wait_timer = + timer_new_ns(OT_VIRTUAL_CLOCK, &ot_kmac_edn_wait_timer_handler, s); s->bh = qemu_bh_new(&ot_kmac_process, s); /* FIFO sizes as per OT Spec */ diff --git a/include/hw/opentitan/ot_kmac.h b/include/hw/opentitan/ot_kmac.h index f120173b7239a..b85ac90ecf02c 100644 --- a/include/hw/opentitan/ot_kmac.h +++ b/include/hw/opentitan/ot_kmac.h @@ -36,6 +36,8 @@ #define TYPE_OT_KMAC "ot-kmac" OBJECT_DECLARE_TYPE(OtKMACState, OtKMACClass, OT_KMAC) +#define OT_KMAC_LC_ESCALATE_EN TYPE_OT_KMAC "-lc-escalate-en" + enum OtKMACMode { OT_KMAC_MODE_NONE, OT_KMAC_MODE_SHA3, From f9db128d3aa5bbae31a55f21c6f1b6f830493271 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:33 +0000 Subject: [PATCH 22/52] [ot] hw/opentitan: otbn: align ALU, call stack, loop & lock with RTL Align the OpenTitan OTBN peripheral model (C wrapper and Rust instruction engine) with Earlgrey RTL (`hw/ip/otbn/rtl/otbn.sv`, `otbn_controller.sv`, `otbn_alu_bignum.sv`, `otbn_rf_base.sv`, `otbn_loop_controller.sv`, and `otbn_rnd.sv`): 1. Enforce `OTBN_PERMIT[]` sub-word write checks and `ByteAccess = 0` on IMEM/DMEM, advance `LOAD_CHECKSUM` on idle/locked bus reads (`wdata=0`) and 32-bit writes (`otbn.sv:737-746`), and implement write-any-clears-to-0 on `ERR_BITS` and `INSN_CNT` while idle or locked (`otbn.sv:926-996`). 2. Model host IMEM/DMEM illegal bus access during `BUSY_EXECUTE` (`ERR_BITS.ILLEGAL_BUS_ACCESS`, `FATAL_ALERT_CAUSE`, `STATUS_LOCKED`, and pre-lock un-encoded `39'b0` read blanking raising Ibex `FATAL_INTG_ERR`), plus OTP scrambling nonce generation tracking (`imem_key_gen` / `dmem_key_gen`) for `IMEM_INTG_VIOLATION` and `DMEM_INTG_VIOLATION`. 3. Add `OT_OTBN_LC_ESCALATE_EN` (`lc_escalate_en_i`) handling to latch `ERR_BITS.LIFECYCLE_ESCALATION` (`0x400000`) and lock OTBN, plus 256-bit `URND` PRNG seeding/advancement and post-stop 2-round internal secure wipe. 4. Fix `BN.ADDM` when `a + b == MOD` (`otbn_alu_bignum.sv:905`), `CSR FLAGS` (`0x7C8`) mapping to `FG0`/`FG1` and inter-execution `FLAGS`/`MOD`/`ACC` zeroing, `x1` call-stack simultaneous pop-before-push and dual-read port semantics, and `ERR_BITS.LOOP` detection at active loop end addresses. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ic6c2dcd10f934e671eb25dbe5c656eb96a6a6964 --- hw/opentitan/ot_otbn.c | 259 +++++++++++++++++++++--- hw/opentitan/otbn/meson.build | 1 + hw/opentitan/otbn/otbn/src/comm.rs | 1 + hw/opentitan/otbn/otbn/src/csrs.rs | 12 +- hw/opentitan/otbn/otbn/src/insn_exec.rs | 85 ++++++-- hw/opentitan/otbn/otbn/src/lib.rs | 6 + hw/opentitan/otbn/otbn/src/memory.rs | 26 +++ hw/opentitan/otbn/otbn/src/otbn.rs | 92 ++++++++- hw/opentitan/otbn/otbn/src/proxy.rs | 91 ++++++--- hw/opentitan/otbn/otbn/src/random.rs | 79 ++++++-- include/hw/opentitan/ot_otbn.h | 2 + include/hw/opentitan/otbn/otbnproxy.h | 2 +- 12 files changed, 550 insertions(+), 106 deletions(-) diff --git a/hw/opentitan/ot_otbn.c b/hw/opentitan/ot_otbn.c index 51d86512741bb..db2b4a989e4f2 100644 --- a/hw/opentitan/ot_otbn.c +++ b/hw/opentitan/ot_otbn.c @@ -36,6 +36,7 @@ #include "qemu/timer.h" #include "qemu/typedefs.h" #include "qapi/error.h" +#include "block/aio.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_clkmgr.h" #include "hw/opentitan/ot_common.h" @@ -171,9 +172,11 @@ struct OtOTBNState { uint32_t intr_test; uint32_t alert_test; uint32_t errbits; + uint32_t fatal_alert_cause; uint32_t load_checksum; enum OtOTBNCommand last_cmd; + int64_t exec_start_ns; char *ot_id; char *clock_name; @@ -181,6 +184,7 @@ struct OtOTBNState { char *log_file; OtOTBNRandom rnds[OT_OTBN_RND_COUNT]; bool log_asm; + bool lc_escalated; }; struct OtOTBNClass { @@ -203,12 +207,14 @@ static void ot_otbn_request_entropy(OtOTBNRandom *rnd); static bool ot_otbn_is_idle(OtOTBNState *s) { - return ot_otbn_proxy_get_status(s->proxy) == OT_OTBN_STATUS_IDLE; + return !s->lc_escalated && + ot_otbn_proxy_get_status(s->proxy) == OT_OTBN_STATUS_IDLE; } static bool ot_otbn_is_locked(OtOTBNState *s) { - return ot_otbn_proxy_get_status(s->proxy) == OT_OTBN_STATUS_LOCKED; + return s->lc_escalated || + ot_otbn_proxy_get_status(s->proxy) == OT_OTBN_STATUS_LOCKED; } static void ot_otbn_update_irq(OtOTBNState *s) @@ -228,7 +234,7 @@ static void ot_otbn_update_alert(OtOTBNState *s) } uint16_t fatal_err_bits = (uint16_t)(s->errbits >> 16u); - if (fatal_err_bits) { + if (fatal_err_bits || s->fatal_alert_cause) { levels |= 1u << ALERT_FATAL; } @@ -248,7 +254,8 @@ static void ot_otbn_update_alert(OtOTBNState *s) /* ALERT_TEST and recoverable error alerts are transient */ s->alert_test = 0u; s->errbits &= ~UINT16_MAX; - levels = fatal_err_bits ? (1u << ALERT_FATAL) : 0u; + levels = + (fatal_err_bits || s->fatal_alert_cause) ? (1u << ALERT_FATAL) : 0u; for (unsigned ix = 0u; ix < ALERT_COUNT; ix++) { int level = (int)((levels >> ix) & 0x1u); @@ -266,6 +273,7 @@ static void ot_otbn_post_execute(void *opaque) OtOTBNState *s = OT_OTBN(opaque); s->errbits = ot_otbn_proxy_get_err_bits(s->proxy); + s->fatal_alert_cause |= (s->errbits >> 16u); uint32_t insncount = ot_otbn_proxy_get_instruction_count(s->proxy); trace_ot_otbn_post_execute(s->ot_id, s->errbits, insncount); s->intr_state |= INTR_DONE_MASK; @@ -280,6 +288,9 @@ static void ot_otbn_signal_on_completion(void *opaque) OtOTBNState *s = OT_OTBN(opaque); qemu_bh_schedule(s->proxy_completion_bh); + if (first_cpu) { + cpu_exit(first_cpu); + } } static void ot_otbn_trigger_entropy_req(void *opaque) @@ -300,6 +311,9 @@ static void ot_otbn_trigger_entropy_req(void *opaque) } qemu_bh_schedule(r->proxy_entropy_req_bh); + if (first_cpu) { + cpu_exit(first_cpu); + } } static void ot_otbn_proxy_completion_bh(void *opaque) @@ -311,6 +325,16 @@ static void ot_otbn_proxy_completion_bh(void *opaque) trace_ot_otbn_proxy_completion_bh(s->ot_id, last_cmd); + if (last_cmd == OT_OTBN_CMD_NONE) { + return; + } + + if (ot_otbn_is_locked(s)) { + timer_del(s->proxy_defer); + ot_otbn_post_execute(s); + return; + } + switch (last_cmd) { case OT_OTBN_CMD_EXECUTE: case OT_OTBN_CMD_SEC_WIPE_DMEM: @@ -318,14 +342,26 @@ static void ot_otbn_proxy_completion_bh(void *opaque) if (s->proxy_defer) { /* * timer is used to simulate a delayed processing, which maybe - * useful to pass some test suites such as OT smoketest wait 100 - * microsecs so that the virtual hart can be scheduled and may poll + * useful to pass some test suites such as OT smoketest wait + * so that the virtual hart can be scheduled and may poll * the status register before the actual completion is signalled * from the OTBN working thread. */ + uint64_t delay_ns = 100000ULL; + if (last_cmd == OT_OTBN_CMD_EXECUTE) { + uint32_t insncount = + ot_otbn_proxy_get_instruction_count(s->proxy); + delay_ns = MIN(2000000ULL, + MAX(500000ULL, (uint64_t)insncount * 10ULL)); + } + int64_t now_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + int64_t min_end_ns = s->exec_start_ns + (int64_t)delay_ns; timer_del(s->proxy_defer); - timer_mod(s->proxy_defer, - qemu_clock_get_us(OT_VIRTUAL_CLOCK) + 100u); + if (now_ns < min_end_ns) { + timer_mod(s->proxy_defer, min_end_ns); + } else { + ot_otbn_post_execute(s); + } } else { ot_otbn_post_execute(s); } @@ -355,10 +391,10 @@ static void ot_otbn_fill_entropy(void *opaque, uint32_t bits, bool fips) ot_fifo32_push(&rnd->packer, bits); rnd->no_fips |= !fips; - rnd->entropy_requested = false; if (!ot_fifo32_is_full(&rnd->packer)) { /* need more entropy to fill in the packer */ + rnd->entropy_requested = false; ot_otbn_request_entropy(rnd); return; } @@ -390,6 +426,7 @@ static void ot_otbn_fill_entropy(void *opaque, uint32_t bits, bool fips) } ot_fifo32_reset(&rnd->packer); rnd->no_fips = false; + rnd->entropy_requested = false; if (!res) { trace_ot_otbn_error(s->ot_id, "cannot push entropy"); } @@ -446,14 +483,17 @@ static void ot_otbn_handle_command(OtOTBNState *s, unsigned command) switch (command) { case (unsigned)OT_OTBN_CMD_EXECUTE: s->last_cmd = command; + s->exec_start_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); ot_otbn_proxy_execute(s->proxy, false); break; case (unsigned)OT_OTBN_CMD_SEC_WIPE_DMEM: s->last_cmd = command; + s->exec_start_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); ot_otbn_proxy_wipe_memory(s->proxy, false); break; case (unsigned)OT_OTBN_CMD_SEC_WIDE_IMEM: s->last_cmd = command; + s->exec_start_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); ot_otbn_proxy_wipe_memory(s->proxy, true); break; default: @@ -501,6 +541,71 @@ static void ot_otbn_push_key(OtKeySinkIf *ifd, const uint8_t *share0, } } +static uint32_t ot_otbn_sync_proxy(OtOTBNState *s) +{ + uint32_t status = ot_otbn_proxy_get_status(s->proxy); + if (status == OT_OTBN_STATUS_IDLE && s->last_cmd == OT_OTBN_CMD_NONE) { + return status; + } + + do { + qemu_clock_run_timers(OT_VIRTUAL_CLOCK); + } while (aio_bh_poll(qemu_get_aio_context()) > 0); + status = ot_otbn_proxy_get_status(s->proxy); + + bool wait_for_worker = + (status != OT_OTBN_STATUS_IDLE) && + ((status == OT_OTBN_STATUS_BUSY_SEC_WIPE_INT) || + (qemu_clock_get_ns(OT_VIRTUAL_CLOCK) - s->exec_start_ns >= 500000ULL)); + + if (wait_for_worker) { + for (int i = 0; i < 50; i++) { + do { + qemu_clock_run_timers(OT_VIRTUAL_CLOCK); + } while (aio_bh_poll(qemu_get_aio_context()) > 0); + status = ot_otbn_proxy_get_status(s->proxy); + if (status == OT_OTBN_STATUS_IDLE || + s->last_cmd == OT_OTBN_CMD_NONE || + s->rnds[OT_OTBN_URND].entropy_requested || + s->rnds[OT_OTBN_RND].entropy_requested) { + break; + } + g_usleep(10); + } + do { + qemu_clock_run_timers(OT_VIRTUAL_CLOCK); + } while (aio_bh_poll(qemu_get_aio_context()) > 0); + } + + return ot_otbn_proxy_get_status(s->proxy); +} + +static uint8_t ot_otbn_reg_permit(hwaddr reg) +{ + switch (reg) { + case R_ERR_BITS: + return 0x7u; + case R_INSN_CNT: + case R_LOAD_CHECKSUM: + return 0xfu; + default: + return 0x1u; + } +} + +static bool ot_otbn_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + hwaddr reg = R32_OFF(addr); + uint32_t reg_be = ((1u << size) - 1u) << (addr & 3u); + return (ot_otbn_reg_permit(reg) & ~reg_be) == 0u; +} + static uint64_t ot_otbn_regs_read(void *opaque, hwaddr addr, unsigned size) { OtOTBNState *s = opaque; @@ -510,8 +615,12 @@ static uint64_t ot_otbn_regs_read(void *opaque, hwaddr addr, unsigned size) uint32_t pc = ibex_get_current_pc(); hwaddr reg = R32_OFF(addr); + switch (reg) { case R_INTR_STATE: + if (!(s->intr_state & INTR_DONE_MASK)) { + (void)ot_otbn_sync_proxy(s); + } val32 = s->intr_state; break; case R_INTR_ENABLE: @@ -521,13 +630,25 @@ static uint64_t ot_otbn_regs_read(void *opaque, hwaddr addr, unsigned size) val32 = (uint32_t)ot_otbn_proxy_get_ctrl(s->proxy); break; case R_STATUS: - val32 = ot_otbn_proxy_get_status(s->proxy); + if (s->lc_escalated) { + val32 = OT_OTBN_STATUS_LOCKED; + break; + } + val32 = ot_otbn_sync_proxy(s); + if ((val32 == OT_OTBN_STATUS_IDLE || + val32 == OT_OTBN_STATUS_BUSY_SEC_WIPE_INT) && + (s->last_cmd == OT_OTBN_CMD_EXECUTE || + (s->proxy_defer && timer_pending(s->proxy_defer)))) { + val32 = OT_OTBN_STATUS_BUSY_EXECUTE; + } break; case R_ERR_BITS: - val32 = ot_otbn_proxy_get_err_bits(s->proxy); + val32 = ot_otbn_proxy_get_err_bits(s->proxy) | + (s->errbits & R_ERR_BITS_LIFECYCLE_ESCALATION_MASK); break; case R_FATAL_ALERT_CAUSE: - val32 = s->errbits >> 16u; + s->fatal_alert_cause |= (ot_otbn_proxy_get_err_bits(s->proxy) >> 16u); + val32 = s->fatal_alert_cause; break; case R_INSN_CNT: val32 = ot_otbn_proxy_get_instruction_count(s->proxy); @@ -567,11 +688,6 @@ static void ot_otbn_regs_write(void *opaque, hwaddr addr, uint64_t val64, uint32_t pc = ibex_get_current_pc(); trace_ot_otbn_io_write(s->ot_id, (uint32_t)addr, REG_NAME(reg), val32, pc); - if (ot_otbn_is_locked(s)) { - trace_ot_otbn_deny(s->ot_id, pc, "write denied: locked"); - return; - } - switch (reg) { case R_INTR_STATE: val32 &= INTR_DONE_MASK; @@ -594,18 +710,35 @@ static void ot_otbn_regs_write(void *opaque, hwaddr addr, uint64_t val64, ot_otbn_update_alert(s); break; case R_CMD: + if (!ot_otbn_is_idle(s)) { + trace_ot_otbn_deny(s->ot_id, pc, "write denied: not idle"); + break; + } val32 &= R_CMD_CMD_MASK; ot_otbn_handle_command(s, (unsigned)val32); break; case R_CTRL: + if (!ot_otbn_is_idle(s)) { + trace_ot_otbn_deny(s->ot_id, pc, "write denied: not idle"); + break; + } val32 &= R_CTRL_SW_ERRS_FATAL_MASK; ot_otbn_proxy_set_ctrl(s->proxy, (bool)val32); ot_otbn_update_alert(s); break; case R_ERR_BITS: + if (!ot_otbn_is_idle(s) && !ot_otbn_is_locked(s)) { + trace_ot_otbn_deny(s->ot_id, pc, "write denied: busy"); + break; + } + s->errbits = 0u; ot_otbn_proxy_set_err_bits(s->proxy, val32); break; case R_INSN_CNT: + if (!ot_otbn_is_idle(s) && !ot_otbn_is_locked(s)) { + trace_ot_otbn_deny(s->ot_id, pc, "write denied: busy"); + break; + } ot_otbn_proxy_set_instruction_count(s->proxy, val32); break; case R_LOAD_CHECKSUM: @@ -637,20 +770,66 @@ static void ot_otbn_update_checksum(OtOTBNState *s, bool doi, uint32_t addr, static uint32_t ot_otbn_mem_read(OtOTBNState *s, bool doi, hwaddr addr) { - uint32_t value = ot_otbn_proxy_read_memory(s->proxy, doi, addr); + bool valid = true; + uint32_t value = ot_otbn_proxy_read_memory(s->proxy, doi, addr, &valid); trace_ot_otbn_mem_read(s->ot_id, doi ? 'I' : 'D', (uint32_t)addr, value); + if (ot_otbn_is_locked(s)) { + s->last_cmd = OT_OTBN_CMD_NONE; + timer_del(s->proxy_defer); + ot_otbn_post_execute(s); + } + if (valid) { + /* + * In otbn.sv:737-746, mem_crc_data_in_valid checks: + * ~(dmem_access_core | imem_access_core) & + * ((imem_req_bus & (imem_byte_mask_bus == 4'hf)) | + * (dmem_req_bus & (dmem_byte_mask_bus == 4'hf))) + * without gating on imem_write_bus / dmem_write_bus. Because + * tlul_adapter_host.sv:94 drives a_mask = 4'hf on all TL-UL Get + * requests and tlul_adapter_sram.sv:390,429 asserts req_o = 1 with + * wdata_int = 0 when we_o = 0, valid idle host reads advance + * u_mem_load_crc32 with wr_data = 0. + */ + ot_otbn_update_checksum(s, doi, addr, 0u); + } else { + hwaddr offset = (doi ? OT_OTBN_IMEM_BASE : OT_OTBN_DMEM_BASE) + addr; + ot_common_raise_load_integrity_error(DEVICE(s), offset); + } return value; } static void ot_otbn_mem_write(OtOTBNState *s, bool doi, hwaddr addr, uint32_t value) { - bool written = ot_otbn_proxy_write_memory(s->proxy, doi, addr, value); + bool was_locked = ot_otbn_is_locked(s); + bool written = !s->lc_escalated && + ot_otbn_proxy_write_memory(s->proxy, doi, addr, value); trace_ot_otbn_mem_write(s->ot_id, doi ? 'I' : 'D', (uint32_t)addr, value, written ? "" : " FAILED"); - if (written) { + if (written || was_locked) { + /* + * In otbn.sv:737-739, mem_crc_data_in_valid is gated by + * ~(dmem_access_core | imem_access_core), i.e. ~(busy_execute_q | + * start_q), and is NOT gated by ~locking. When OTBN is already in + * StatusLocked, prim_ram_1p_scr suppresses the SRAM macro write + * (.intg_error_i(locking)), but u_mem_load_crc32 still advances on + * 32-bit bus writes. + */ ot_otbn_update_checksum(s, doi, addr, value); } + if (ot_otbn_is_locked(s)) { + s->last_cmd = OT_OTBN_CMD_NONE; + timer_del(s->proxy_defer); + ot_otbn_post_execute(s); + } +} + +static bool ot_otbn_mem_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + return !is_write || (size == 4u && (addr & 3u) == 0u); } static inline uint64_t @@ -666,8 +845,7 @@ static inline void ot_otbn_imem_write(void *opaque, hwaddr addr, uint64_t val64, { (void)size; - return ot_otbn_mem_write((OtOTBNState *)opaque, true, addr, - (uint32_t)val64); + ot_otbn_mem_write((OtOTBNState *)opaque, true, addr, (uint32_t)val64); } static inline uint64_t @@ -683,8 +861,7 @@ static inline void ot_otbn_dmem_write(void *opaque, hwaddr addr, uint64_t val64, { (void)size; - return ot_otbn_mem_write((OtOTBNState *)opaque, false, addr, - (uint32_t)val64); + ot_otbn_mem_write((OtOTBNState *)opaque, false, addr, (uint32_t)val64); } static const Property ot_otbn_properties[] = { @@ -709,6 +886,7 @@ static const MemoryRegionOps ot_otbn_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_otbn_regs_accepts, }; static const MemoryRegionOps ot_otbn_imem_ops = { @@ -717,6 +895,7 @@ static const MemoryRegionOps ot_otbn_imem_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_otbn_mem_accepts, }; static const MemoryRegionOps ot_otbn_dmem_ops = { @@ -725,6 +904,7 @@ static const MemoryRegionOps ot_otbn_dmem_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_otbn_mem_accepts, }; static void ot_otbn_reset_enter(Object *obj, ResetType type) @@ -736,6 +916,7 @@ static void ot_otbn_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } + qemu_bh_cancel(s->proxy_completion_bh); timer_del(s->proxy_defer); s->intr_state = 0; @@ -743,9 +924,12 @@ static void ot_otbn_reset_enter(Object *obj, ResetType type) s->intr_test = 0; s->alert_test = 0; s->errbits = 0; + s->fatal_alert_cause = 0; s->load_checksum = 0; + s->lc_escalated = false; s->last_cmd = OT_OTBN_CMD_NONE; + s->exec_start_ns = 0; ibex_irq_set(&s->irq_done, 0); for (unsigned ix = 0; ix < ALERT_COUNT; ix++) { ibex_irq_set(&s->alerts[ix], 0); @@ -753,6 +937,7 @@ static void ot_otbn_reset_enter(Object *obj, ResetType type) for (unsigned rix = 0; rix < (unsigned)OT_OTBN_RND_COUNT; rix++) { OtOTBNRandom *rnd = &s->rnds[rix]; + qemu_bh_cancel(rnd->proxy_entropy_req_bh); rnd->otbn = s; rnd->no_fips = false; rnd->entropy_requested = false; @@ -796,6 +981,28 @@ static void ot_otbn_reset_exit(Object *obj, ResetType type) } ot_otbn_proxy_start(s->proxy, false, s->log_file, s->log_asm); + qemu_bh_cancel(s->proxy_completion_bh); + for (unsigned rix = 0; rix < (unsigned)OT_OTBN_RND_COUNT; rix++) { + qemu_bh_cancel(s->rnds[rix].proxy_entropy_req_bh); + } + timer_del(s->proxy_defer); + s->last_cmd = OT_OTBN_CMD_NONE; +} + +static void ot_otbn_lc_escalate_en(void *opaque, int irq, int level) +{ + OtOTBNState *s = opaque; + + g_assert(irq == 0); + + if (level && !s->lc_escalated) { + s->lc_escalated = true; + s->fatal_alert_cause |= R_FATAL_ALERT_CAUSE_LIFECYCLE_ESCALATION_MASK; + qemu_bh_cancel(s->proxy_completion_bh); + timer_del(s->proxy_defer); + s->last_cmd = OT_OTBN_CMD_NONE; + ot_otbn_update_alert(s); + } } static void ot_otbn_realize(DeviceState *dev, Error **errp) @@ -814,6 +1021,8 @@ static void ot_otbn_realize(DeviceState *dev, Error **errp) qdev_init_gpio_in_named(DEVICE(s), &ot_otbn_clock_input, OT_OTBN_CLOCK_INPUT, 1); + qdev_init_gpio_in_named(DEVICE(s), &ot_otbn_lc_escalate_en, + OT_OTBN_LC_ESCALATE_EN, 1); } static void ot_otbn_init(Object *obj) @@ -854,7 +1063,7 @@ static void ot_otbn_init(Object *obj) } s->proxy_completion_bh = qemu_bh_new(&ot_otbn_proxy_completion_bh, s); - s->proxy_defer = timer_new_us(OT_VIRTUAL_CLOCK, &ot_otbn_post_execute, s); + s->proxy_defer = timer_new_ns(OT_VIRTUAL_CLOCK, &ot_otbn_post_execute, s); s->proxy = ot_otbn_proxy_new(&ot_otbn_trigger_entropy_req, &s->rnds[OT_OTBN_URND], &ot_otbn_trigger_entropy_req, &s->rnds[OT_OTBN_RND], diff --git a/hw/opentitan/otbn/meson.build b/hw/opentitan/otbn/meson.build index a743e17bab0d1..d82aca6c66a1d 100644 --- a/hw/opentitan/otbn/meson.build +++ b/hw/opentitan/otbn/meson.build @@ -21,6 +21,7 @@ otbn_lib = static_library('otbn', paste_dep, ethnum_dep, dl_dep], + rust_args: ['-C', 'opt-level=3', '-C', 'debug-assertions=no', '-C', 'target-cpu=native', '-C', 'codegen-units=1'], rust_crate_type: 'staticlib') otbn_dep = declare_dependency(link_with: otbn_lib) diff --git a/hw/opentitan/otbn/otbn/src/comm.rs b/hw/opentitan/otbn/otbn/src/comm.rs index e134f34df6086..6489a83490b4f 100644 --- a/hw/opentitan/otbn/otbn/src/comm.rs +++ b/hw/opentitan/otbn/otbn/src/comm.rs @@ -14,6 +14,7 @@ pub enum Command { Execute(bool), WipeDMem, WipeIMem, + Reset, Terminate, } diff --git a/hw/opentitan/otbn/otbn/src/csrs.rs b/hw/opentitan/otbn/otbn/src/csrs.rs index 46ae6515887f3..267f8390d3fa7 100644 --- a/hw/opentitan/otbn/otbn/src/csrs.rs +++ b/hw/opentitan/otbn/otbn/src/csrs.rs @@ -157,7 +157,7 @@ impl CSR for CSRFlagGroup { FlagMode::Fg1 => flags[1] = insn_proc::Flags::from_bits_truncate(val8 & 0x0f), FlagMode::Flags => { flags[0] = insn_proc::Flags::from_bits_truncate(val8 & 0x0f); - flags[1] = insn_proc::Flags::from_bits_truncate(val8 & 0x0f); + flags[1] = insn_proc::Flags::from_bits_truncate((val8 >> 4) & 0x0f); } }; self.flags.set(flags); @@ -537,17 +537,15 @@ impl CSRSet { * - wipe with PRNG randomness * - zero */ - for flag in self.shared_flags.get().iter_mut() { - *flag = insn_proc::Flags::empty(); - } + self.shared_flags.set([insn_proc::Flags::empty(); 2]); let mut prng = prng.lock().unwrap(); - let _ = self.acc.write(U256::from(0u32)); - let _ = self.r#mod.write(U256::from(0u32)); - let _ = self.acc.write(prng.get_prng_u256()); let _ = self.r#mod.write(prng.get_prng_u256()); + + let _ = self.acc.write(U256::from(0u32)); + let _ = self.r#mod.write(U256::from(0u32)); } pub fn set_test_mode(&mut self, enable: bool) { diff --git a/hw/opentitan/otbn/otbn/src/insn_exec.rs b/hw/opentitan/otbn/otbn/src/insn_exec.rs index 661f1dbb5ba74..0f701e625a4c0 100644 --- a/hw/opentitan/otbn/otbn/src/insn_exec.rs +++ b/hw/opentitan/otbn/otbn/src/insn_exec.rs @@ -85,6 +85,7 @@ pub struct HartState { pub csr_set: csrs::CSRSet, /// Track state changes (debug) pub updated: StateTracker, + pub x1_read: bool, } impl HartState { @@ -96,6 +97,7 @@ impl HartState { loopstack: Vec::with_capacity(8), hwstack: Vec::with_capacity(8), updated: StateTracker::default(), + x1_read: false, csr_set: csrs::CSRSet::new(urnd, rnd, key), } } @@ -107,6 +109,10 @@ impl HartState { return Ok(()); } if reg_index == 1 { + if self.x1_read { + self.hwstack.pop(); + self.x1_read = false; + } if self.hwstack.len() >= self.hwstack.capacity() { return Err(InstructionTrap::Exception(ExceptionCause::ECallStack, None)); } @@ -124,8 +130,9 @@ impl HartState { if reg_index == 0 { Ok(0) } else if reg_index == 1 { - match self.hwstack.pop() { - Some(x) => { + self.x1_read = true; + match self.hwstack.last() { + Some(&x) => { self.registers[reg_index] = x; Ok(x) } @@ -477,6 +484,12 @@ impl<'a, M: Memory> InstructionExecutor<'a, M> { )); } }; + if !self.dmem.is_valid(addr) { + return Err(InstructionTrap::Exception( + ExceptionCause::EFatal, + Some(super::otbn::ErrBits::DMEM_INTG_VIOLATION.bits()), + )); + } // Sign extend loaded data if required if signed { @@ -531,14 +544,24 @@ impl<'a, M: Memory> InstructionExecutor<'a, M> { /// [InstructionTrap] is returned when the instruction execution causes a trap. pub fn step(&mut self) -> Result<(), InstructionTrap> { self.hart_state.updated.clear(); + self.hart_state.x1_read = false; if let Some(next_insn) = self.imem.read_mem(self.hart_state.pc) { + if !self.imem.is_valid(self.hart_state.pc) { + return Err(InstructionTrap::Exception( + ExceptionCause::EFatal, + Some(super::otbn::ErrBits::IMEM_INTG_VIOLATION.bits()), + )); + } // Fetch next instruction from memory and eecute the instruction if fetch was // successful let step_result = insn_decode::decoder(self, next_insn); match step_result { Some(Ok(pc_updated)) => { + if self.hart_state.x1_read { + self.hart_state.hwstack.pop(); + } if !pc_updated { let lstack = &mut self.hart_state.loopstack; let loopdepth = lstack.len(); @@ -592,6 +615,18 @@ impl<'a, M: Memory> InstructionExecutor<'a, M> { )) } } + + fn check_loop_end(&self) -> Result<(), InstructionTrap> { + if self + .hart_state + .loopstack + .last() + .is_some_and(|hwloop| self.hart_state.pc == hwloop.end) + { + return Err(InstructionTrap::Exception(ExceptionCause::ELoop, None)); + } + Ok(()) + } } // Macros to implement various repeated operations (e.g. ALU reg op reg instructions). @@ -787,6 +822,7 @@ impl<'a, M: Memory> insn_proc::InstructionProcessor for InstructionExecutor<'a, make_store_op_fn! {sw} fn process_jal(&mut self, dec_insn: insn_format::JType) -> Self::InstructionResult { + self.check_loop_end()?; let target_pc = self.hart_state.pc.wrapping_add(dec_insn.imm as u32); self.hart_state .write_register(dec_insn.rd, self.hart_state.pc + 4)?; @@ -795,6 +831,7 @@ impl<'a, M: Memory> insn_proc::InstructionProcessor for InstructionExecutor<'a, } fn process_jalr(&mut self, dec_insn: insn_format::IType) -> Self::InstructionResult { + self.check_loop_end()?; let target_pc = self .hart_state .read_register(dec_insn.rs1)? @@ -856,6 +893,7 @@ impl<'a, M: Memory> insn_proc::InstructionProcessor for InstructionExecutor<'a, if hwloop.count == 0 { return Err(InstructionTrap::Exception(ExceptionCause::ELoop, None)); } + self.check_loop_end()?; // need to check this instruction is not a last instruction in an existing loop self.hart_state.loopstack.push(hwloop); self.hart_state.updated.loophead = Some((self.hart_state.loopstack.len(), hwloop.count)); @@ -909,7 +947,24 @@ impl<'a, M: Memory> insn_proc::InstructionProcessor for InstructionExecutor<'a, let mut bytes = [0; size_of::()]; const RSIZE: usize = size_of::(); for (pos, bchunk) in (0..bytes.len()).step_by(RSIZE).zip(bytes.chunks_mut(RSIZE)) { - let word = self.dmem.read_mem(src + pos as u32).unwrap(); + let word_addr = src.wrapping_add(pos as u32); + let word = match self.dmem.read_mem(word_addr) { + Some(w) => { + if !self.dmem.is_valid(word_addr) { + return Err(InstructionTrap::Exception( + ExceptionCause::EFatal, + Some(super::otbn::ErrBits::DMEM_INTG_VIOLATION.bits()), + )); + } + w + } + None => { + return Err(InstructionTrap::Exception( + ExceptionCause::EBadDataAddr, + Some(word_addr), + )); + } + }; bchunk.copy_from_slice(&word.to_le_bytes()[..]); } let wvalue = U256::from_le_bytes(bytes); @@ -972,6 +1027,20 @@ impl<'a, M: Memory> insn_proc::InstructionProcessor for InstructionExecutor<'a, addr.wrapping_sub(-offset as u32) }; + let wvalue = self.hart_state.read_wide_register(wri as usize)?; + let bytes = wvalue.to_le_bytes(); + const RSIZE: usize = size_of::(); + for pos in (0..bytes.len()).step_by(RSIZE) { + let word_addr = dst.wrapping_add(pos as u32); + let word = u32::from_le_bytes(bytes[pos..pos + RSIZE].try_into().unwrap()); + if !self.dmem.write_mem(word_addr, word) { + return Err(InstructionTrap::Exception( + ExceptionCause::EBadDataAddr, + Some(word_addr), + )); + } + } + if grs1_inc { let addr = addr.wrapping_add(size_of::() as u32); self.hart_state.write_register(dec_insn.rs1, addr)?; @@ -981,14 +1050,6 @@ impl<'a, M: Memory> insn_proc::InstructionProcessor for InstructionExecutor<'a, self.hart_state.write_register(dec_insn.rs2, wri + 1)?; } - let wvalue = self.hart_state.read_wide_register(wri as usize)?; - let bytes = wvalue.to_le_bytes(); - const RSIZE: usize = size_of::(); - for pos in (0..bytes.len()).step_by(RSIZE) { - let word = u32::from_le_bytes(bytes[pos..pos + RSIZE].try_into().unwrap()); - self.dmem.write_mem(dst + pos as u32, word); - } - Ok(false) } @@ -1125,7 +1186,7 @@ impl<'a, M: Memory> insn_proc::InstructionProcessor for InstructionExecutor<'a, .unwrap(); let (mut res, over) = a.overflowing_add(b); - if over || res > modv { + if over || res >= modv { res = res.wrapping_sub(modv) } diff --git a/hw/opentitan/otbn/otbn/src/lib.rs b/hw/opentitan/otbn/otbn/src/lib.rs index 6492798c1a02a..548ec9fdf2cd6 100644 --- a/hw/opentitan/otbn/otbn/src/lib.rs +++ b/hw/opentitan/otbn/otbn/src/lib.rs @@ -81,5 +81,11 @@ pub trait Memory: Send { fn wipe(&mut self, prng: &mut dyn PRNG); + fn is_valid(&self, _addr: u32) -> bool { + true + } + + fn reset(&mut self) {} + fn update_from_slice(&mut self, src: &[u32]); } diff --git a/hw/opentitan/otbn/otbn/src/memory.rs b/hw/opentitan/otbn/otbn/src/memory.rs index 3d55de616d632..5639a4cf336d1 100644 --- a/hw/opentitan/otbn/otbn/src/memory.rs +++ b/hw/opentitan/otbn/otbn/src/memory.rs @@ -45,12 +45,14 @@ pub fn read_to_memory( /// result in a failed read or write. pub struct VecMemory { pub mem: Vec, + pub valid: Vec, } impl VecMemory { pub fn new(size: usize) -> Self { Self { mem: vec![0u32; size / 4], + valid: vec![false; size / 4], } } @@ -58,6 +60,7 @@ impl VecMemory { for cell in self.mem.iter_mut() { *cell = prng.get_prng_u32(); } + self.valid.fill(false); } } @@ -85,8 +88,12 @@ impl Memory for VecMemory { // Calculate vector index data to update is contained in let word_addr = (addr >> 2) as usize; + if word_addr >= self.mem.len() { + return false; + } self.mem[word_addr] = store_data; + self.valid[word_addr] = true; true } @@ -94,14 +101,25 @@ impl Memory for VecMemory { if src.len() < self.mem.len() { let (head, _) = self.mem.split_at_mut(src.len()); head.copy_from_slice(src); + self.valid[..src.len()].fill(true); } else { self.mem.copy_from_slice(src); + self.valid.fill(true); } } fn wipe(&mut self, prng: &mut dyn PRNG) { self.random_wipe(prng); } + + fn is_valid(&self, addr: u32) -> bool { + let word_addr = (addr >> 2) as usize; + self.valid.get(word_addr).copied().unwrap_or(false) + } + + fn reset(&mut self) { + self.valid.fill(false); + } } pub struct MemoryRegion { @@ -132,4 +150,12 @@ impl Memory for MemoryRegion { fn wipe(&mut self, prng: &mut dyn PRNG) { self.memory.wipe(prng) } + + fn is_valid(&self, addr: u32) -> bool { + self.memory.is_valid(addr) + } + + fn reset(&mut self) { + self.memory.reset() + } } diff --git a/hw/opentitan/otbn/otbn/src/otbn.rs b/hw/opentitan/otbn/otbn/src/otbn.rs index a49e4d789941c..506360e57cee5 100644 --- a/hw/opentitan/otbn/otbn/src/otbn.rs +++ b/hw/opentitan/otbn/otbn/src/otbn.rs @@ -97,6 +97,7 @@ pub struct Registers { pub fatal_bits: Arc, pub ctrl: Arc, pub insn_count: Arc, + pub resetting: Arc, } /// OTBN executer @@ -188,6 +189,20 @@ impl Executer { .unwrap(); loop { let cmd = self.channel.0.recv().unwrap(); + if let comm::Command::Reset = cmd { + self.registers.resetting.store(false, Ordering::Relaxed); + self.imem.lock().unwrap().reset(); + self.dmem.lock().unwrap().reset(); + self.registers + .status + .store(Status::Idle as u32, Ordering::Relaxed); + self.registers.err_bits.store(0, Ordering::Relaxed); + self.registers.fatal_bits.store(0, Ordering::Relaxed); + self.registers.ctrl.store(false, Ordering::Relaxed); + self.registers.insn_count.store(0, Ordering::Relaxed); + self.channel.1.send(comm::Response::Ack).unwrap(); + continue; + } let state = self.get_status(); match state { Status::Idle => self.handle_comm(cmd), @@ -237,24 +252,35 @@ impl Executer { comm::Command::Execute(dump) => { // always update the state before replying self.set_status(Status::BusyExecute); + self.registers + .err_bits + .store(ErrBits::empty().bits(), Ordering::Relaxed); + self.registers.insn_count.store(0, Ordering::Relaxed); self.channel.1.send(comm::Response::Ack).unwrap(); self.execute(dump); - self.signal_completion(); + if self.get_status() != Status::Locked { + self.signal_completion(); + } } comm::Command::WipeDMem => { // always update the state before replying self.set_status(Status::BusySecWipeDMem); self.channel.1.send(comm::Response::Ack).unwrap(); self.wipe_memory(false); - self.signal_completion(); + if self.get_status() != Status::Locked { + self.signal_completion(); + } } comm::Command::WipeIMem => { // always update the state before replying self.set_status(Status::BusySecWipeIMem); self.channel.1.send(comm::Response::Ack).unwrap(); self.wipe_memory(true); - self.signal_completion(); + if self.get_status() != Status::Locked { + self.signal_completion(); + } } + comm::Command::Reset => (), comm::Command::Terminate => { self.set_status(Status::Locked); self.channel.1.send(comm::Response::Ack).unwrap(); @@ -269,8 +295,19 @@ impl Executer { // "Each new execution of OTBN will reseed the URND PRNG." // Stall OTBN execution till entropy is injected self.syncurnd.sync_reseed(); - match self.do_execute(dump) { - insn_exec::InstructionTrap::Exception(cause, _val) => { + if self.registers.resetting.load(Ordering::Relaxed) + || self.get_status() == Status::Locked + { + return; + } + let trap = self.do_execute(dump); + if self.registers.resetting.load(Ordering::Relaxed) + || self.get_status() == Status::Locked + { + return; + } + match trap { + insn_exec::InstructionTrap::Exception(cause, val) => { let mut error: u32; // let mut registers = self.registers.lock().unwrap(); (fatal, error) = match cause { @@ -283,14 +320,23 @@ impl Executer { ExceptionCause::EKeyInvalid => (false, ErrBits::KEY_INVALID.bits()), ExceptionCause::ERndRepChkFail => (false, ErrBits::RND_REP_CHK_FAIL.bits()), ExceptionCause::ERndFipsChkFail => (false, ErrBits::RND_FIPS_CHK_FAIL.bits()), - ExceptionCause::EFatal => { - (true, self.registers.fatal_bits.load(Ordering::Relaxed)) - } + ExceptionCause::EFatal => ( + true, + self.registers.fatal_bits.load(Ordering::Relaxed) + | val.unwrap_or(0), + ), }; + const SW_ERR_MASK: u32 = ErrBits::BAD_DATA_ADDR.bits() + | ErrBits::BAD_INSN_ADDR.bits() + | ErrBits::CALL_STACK.bits() + | ErrBits::ILLEGAL_INSN.bits() + | ErrBits::LOOP.bits() + | ErrBits::KEY_INVALID.bits(); + // ctrl: "Controls the reaction to software errors. // When set software errors produce fatal errors, rather than recoverable errors." - if error != 0 && self.registers.ctrl.load(Ordering::Relaxed) { + if (error & SW_ERR_MASK) != 0 && self.registers.ctrl.load(Ordering::Relaxed) { fatal = true; error |= ErrBits::FATAL_SOFTWARE.bits(); } @@ -304,6 +350,7 @@ impl Executer { self.imem.try_lock().unwrap().wipe(&mut *x.lock().unwrap()); self.registers.insn_count.store(0, Ordering::Relaxed); self.set_status(Status::Locked); + self.signal_completion(); }; // note: it is required that the OTBN client calls // Proxy::acknowledge_execution() to reset the OTBN status once the @@ -357,6 +404,15 @@ impl Executer { } } + if self.registers.resetting.load(Ordering::Relaxed) + || Status::from_u32(self.registers.status.load(Ordering::Relaxed)) + == Status::Locked + { + return insn_exec::InstructionTrap::Exception( + ExceptionCause::ECallMMode, + None, + ); + } let fatalbits = self.registers.fatal_bits.load(Ordering::Relaxed); let result = if fatalbits == 0 { // Execute instruction @@ -368,6 +424,12 @@ impl Executer { )) }; if let Err(trap) = result { + if self.registers.resetting.load(Ordering::Relaxed) + || Status::from_u32(self.registers.status.load(Ordering::Relaxed)) + == Status::Locked + { + return trap; + } if let Some(log_file) = &mut self.log_file { let log_line = match trap { insn_exec::InstructionTrap::Exception(cause, val) => { @@ -400,6 +462,9 @@ impl Executer { let prng: Arc> = self.syncurnd.urnd(); executor.wipe_internal(&prng); self.syncurnd.sync_reseed(); + if self.registers.resetting.load(Ordering::Relaxed) { + return trap; + } executor.wipe_internal(&prng); let insn_exec::InstructionTrap::Exception(cause, _) = trap; @@ -426,6 +491,9 @@ impl Executer { } fn wipe_memory(&mut self, doi: bool) { + if self.registers.resetting.load(Ordering::Relaxed) { + return; + } let memory = if doi { &mut self.imem } else { &mut self.dmem }; memory @@ -433,6 +501,9 @@ impl Executer { .unwrap() .wipe(&mut *self.syncurnd.urnd().lock().unwrap()); + if self.registers.resetting.load(Ordering::Relaxed) { + return; + } // simulate a "long lasting" op thread::sleep(Duration::from_micros(200)); @@ -442,6 +513,9 @@ impl Executer { } fn signal_completion(&mut self) { + if self.registers.resetting.load(Ordering::Relaxed) { + return; + } if let Some(cb) = &mut self.on_complete { cb.signal(); } diff --git a/hw/opentitan/otbn/otbn/src/proxy.rs b/hw/opentitan/otbn/otbn/src/proxy.rs index 42307dd3c14f9..220eae75c2335 100644 --- a/hw/opentitan/otbn/otbn/src/proxy.rs +++ b/hw/opentitan/otbn/otbn/src/proxy.rs @@ -120,19 +120,18 @@ impl Proxy { pub fn start(&mut self, test_mode: bool, log_name: Option<&str>, log_asm: bool) { if self.core_id.is_some() { // already started, only reset - // hartstate is not reset, as it is done after each execution - match self.get_status() { - otbn::Status::Idle | otbn::Status::Locked => { - let regs = &self.registers; - regs.status - .store(otbn::Status::Idle as u32, Ordering::Relaxed); - regs.err_bits.store(0, Ordering::Relaxed); - regs.fatal_bits.store(0, Ordering::Relaxed); - regs.ctrl.store(false, Ordering::Relaxed); - regs.insn_count.store(0, Ordering::Relaxed); - } - _ => (), + self.registers.resetting.store(true, Ordering::Relaxed); + self.syncurnd.abort_wait(); + self.rnd.abort_wait(); + let channel = self.get_channel(); + channel.0.send(comm::Command::Reset).unwrap(); + match channel.1.recv().unwrap() { + comm::Response::Ack => (), + comm::Response::Error(e) => panic!("Error: {}", e), + _ => panic!("Unexpected response"), } + self.syncurnd.clear(); + self.rnd.clear(); return; } @@ -184,15 +183,31 @@ impl Proxy { } } + fn lock_illegal_bus_access(&mut self) { + self.registers + .fatal_bits + .fetch_or(otbn::ErrBits::ILLEGAL_BUS_ACCESS.bits(), Ordering::Relaxed); + self.registers + .status + .store(otbn::Status::Locked as u32, Ordering::Relaxed); + self.syncurnd.abort_wait(); + self.rnd.abort_wait(); + if let Some(ref mut cb) = self.on_complete { + cb.signal(); + } + } + /// Read one 32-bit from memory - fn read_memory(&mut self, doi: bool, addr: u32) -> u32 { + fn read_memory(&mut self, doi: bool, addr: u32, valid: &mut bool) -> u32 { match self.get_status() { otbn::Status::Idle => (), - otbn::Status::Locked => return 0, // Reads return zero in Locked state + otbn::Status::Locked => { + *valid = true; + return 0; // Reads return zero in Locked state + } _ => { - self.registers - .fatal_bits - .fetch_or(otbn::ErrBits::ILLEGAL_BUS_ACCESS.bits(), Ordering::Relaxed); + *valid = false; + self.lock_illegal_bus_access(); return 0; } } @@ -201,12 +216,15 @@ impl Proxy { &self.imem } else { if addr as usize >= otbn::DMEM_PUB_SIZE { + *valid = true; return 0; } &self.dmem }; - if let Some(data) = mem.lock().unwrap().read_mem(addr) { + let mut mem_guard = mem.lock().unwrap(); + *valid = mem_guard.is_valid(addr); + if let Some(data) = mem_guard.read_mem(addr) { data } else { 0 @@ -219,9 +237,7 @@ impl Proxy { otbn::Status::Idle => (), otbn::Status::Locked => return false, // Writes have no effect in Locked state _ => { - self.registers - .fatal_bits - .fetch_or(otbn::ErrBits::ILLEGAL_BUS_ACCESS.bits(), Ordering::Relaxed); + self.lock_illegal_bus_access(); return false; } } @@ -503,8 +519,14 @@ pub extern "C" fn ot_otbn_proxy_read_memory( proxy: Option<&mut Proxy>, doi: bool, addr: u32, + valid: Option<&mut bool>, ) -> u32 { - proxy.unwrap().read_memory(doi, addr) + let mut is_valid = true; + let res = proxy.unwrap().read_memory(doi, addr, &mut is_valid); + if let Some(v) = valid { + *v = is_valid; + } + res } #[no_mangle] @@ -533,29 +555,38 @@ pub extern "C" fn ot_otbn_proxy_get_instruction_count(proxy: Option<&mut Proxy>) } #[no_mangle] -pub extern "C" fn ot_otbn_proxy_set_instruction_count(proxy: Option<&mut Proxy>, value: u32) { +pub extern "C" fn ot_otbn_proxy_set_instruction_count(proxy: Option<&mut Proxy>, _value: u32) { let proxy = proxy.unwrap(); - if proxy.get_status() == otbn::Status::Idle { + let status = proxy.get_status(); + if status == otbn::Status::Idle || status == otbn::Status::Locked { proxy .registers .insn_count - .store(value as usize, Ordering::Relaxed) + .store(0, Ordering::Relaxed) } } #[no_mangle] pub extern "C" fn ot_otbn_proxy_get_err_bits(proxy: Option<&mut Proxy>) -> u32 { - proxy.unwrap().registers.err_bits.load(Ordering::Relaxed) + let proxy = proxy.unwrap(); + let err_bits = proxy.registers.err_bits.load(Ordering::Relaxed); + let fatal_bits = proxy.registers.fatal_bits.load(Ordering::Relaxed); + err_bits | fatal_bits } #[no_mangle] -pub extern "C" fn ot_otbn_proxy_set_err_bits(proxy: Option<&mut Proxy>, value: u32) { +pub extern "C" fn ot_otbn_proxy_set_err_bits(proxy: Option<&mut Proxy>, _value: u32) { let proxy = proxy.unwrap(); - if proxy.get_status() == otbn::Status::Idle { + let status = proxy.get_status(); + if status == otbn::Status::Idle || status == otbn::Status::Locked { proxy.registers.err_bits.store( - otbn::ErrBits::from_bits_truncate(value).bits(), + 0, + Ordering::Relaxed, + ); + proxy.registers.fatal_bits.store( + 0, Ordering::Relaxed, - ) + ); } } diff --git a/hw/opentitan/otbn/otbn/src/random.rs b/hw/opentitan/otbn/otbn/src/random.rs index 71c290ae842c9..331a990161e48 100644 --- a/hw/opentitan/otbn/otbn/src/random.rs +++ b/hw/opentitan/otbn/otbn/src/random.rs @@ -2,6 +2,7 @@ // Licensed under the Apache License Version 2.0, with LLVM Exceptions, see LICENSE for details. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Condvar, Mutex}; use std::time::Duration; @@ -15,6 +16,7 @@ use crate::{CSRNG, PRNG}; pub struct RndCache { value: u256, available: bool, + pending: bool, fips: bool, repeat: bool, } @@ -22,6 +24,7 @@ pub struct RndCache { pub struct Rnd { cache: Mutex, wait: Condvar, + aborted: AtomicBool, entropy_req: Mutex>>, } @@ -31,6 +34,7 @@ impl Default for Rnd { Self { cache: Mutex::new(cache), wait: Condvar::new(), + aborted: AtomicBool::new(false), entropy_req: Mutex::new(None), } } @@ -46,11 +50,18 @@ impl Rnd { *func = Some(entropy_req); } - pub fn clear(&mut self) { + pub fn abort_wait(&self) { + self.aborted.store(true, Ordering::Relaxed); + self.wait.notify_all(); + } + + pub fn clear(&self) { /* called from OTBN proxy */ + self.aborted.store(false, Ordering::Relaxed); let mut cache = self.cache.lock().unwrap(); cache.value = U256::from(0u32); cache.available = false; + cache.pending = false; cache.fips = false; cache.repeat = false; } @@ -62,21 +73,26 @@ impl Rnd { cache.repeat = cache.value == val; cache.value = val; cache.available = true; + cache.pending = false; cache.fips = fips; self.wait.notify_one(); } pub fn prefetch(&self) { /* called from OTBN processor (CSR) */ - let cache = self.cache.lock().unwrap(); - if cache.available { - /* cache is already loaded, nothing to do */ + let mut cache = self.cache.lock().unwrap(); + if cache.available || cache.pending { + /* cache is already loaded or fetch is already in flight, nothing to do */ return; } + cache.pending = true; self.fetch(); } fn fetch(&self) { + if self.aborted.load(Ordering::Relaxed) { + return; + } let mut func = self.entropy_req.lock().unwrap(); if let Some(req) = &mut *func { req.signal(); @@ -93,24 +109,27 @@ impl CSRNG for Rnd { fn get_csrng_u256(&self) -> (u256, bool, bool) { let mut cache = self.cache.lock().unwrap(); - let mut fetch = false; - loop { - let result = self - .wait - .wait_timeout(cache, Duration::from_millis(50)) - .unwrap(); - cache = result.0; - if cache.available { - break; - } - if !fetch { + if !cache.available && !self.aborted.load(Ordering::Relaxed) { + if !cache.pending { + cache.pending = true; self.fetch(); - fetch = true; + } + while !cache.available && !self.aborted.load(Ordering::Relaxed) { + let result = self + .wait + .wait_timeout(cache, Duration::from_millis(50)) + .unwrap(); + cache = result.0; + if result.1.timed_out() && !cache.available && !self.aborted.load(Ordering::Relaxed) + { + self.fetch(); + } } } let (val, fips, repeat) = (cache.value, cache.fips, cache.repeat); cache.available = false; + cache.pending = false; cache.fips = false; cache.repeat = false; (val, fips, repeat) @@ -150,6 +169,7 @@ pub struct SyncUrnd { urnd: Arc>, sync: Mutex, wait: Condvar, + aborted: AtomicBool, entropy_req: Mutex>>, } @@ -158,6 +178,7 @@ impl Default for SyncUrnd { Self { sync: Mutex::new(false), wait: Condvar::new(), + aborted: AtomicBool::new(false), urnd: Arc::new(Mutex::new(Urnd::default())), entropy_req: Mutex::new(None), } @@ -178,7 +199,20 @@ impl SyncUrnd { self.urnd.clone() } + pub fn abort_wait(&self) { + self.aborted.store(true, Ordering::Relaxed); + self.wait.notify_all(); + } + + pub fn clear(&self) { + self.aborted.store(false, Ordering::Relaxed); + *self.sync.lock().unwrap() = false; + } + pub fn request_reseed(&self) { + if self.aborted.load(Ordering::Relaxed) { + return; + } let mut func = self.entropy_req.lock().unwrap(); if let Some(req) = &mut *func { req.signal(); @@ -186,17 +220,18 @@ impl SyncUrnd { } pub fn wait_reseed(&self) { - loop { - let sync = self.sync.lock().unwrap(); - let mut go = self + let mut sync = self.sync.lock().unwrap(); + while !*sync && !self.aborted.load(Ordering::Relaxed) { + let result = self .wait .wait_timeout(sync, Duration::from_millis(5)) .unwrap(); - if *go.0 { - *go.0 = false; - break; + sync = result.0; + if result.1.timed_out() && !*sync && !self.aborted.load(Ordering::Relaxed) { + self.request_reseed(); } } + *sync = false; } pub fn sync_reseed(&self) { diff --git a/include/hw/opentitan/ot_otbn.h b/include/hw/opentitan/ot_otbn.h index a0e57f2296812..ccd97ebdc224d 100644 --- a/include/hw/opentitan/ot_otbn.h +++ b/include/hw/opentitan/ot_otbn.h @@ -33,6 +33,8 @@ #define TYPE_OT_OTBN "ot-otbn" OBJECT_DECLARE_TYPE(OtOTBNState, OtOTBNClass, OT_OTBN) +#define OT_OTBN_LC_ESCALATE_EN TYPE_OT_OTBN "-lc-escalate-en" + #define OT_OTBN_KEY_SIZE (384u / 8u) #endif /* HW_OPENTITAN_OT_OTBN_H */ diff --git a/include/hw/opentitan/otbn/otbnproxy.h b/include/hw/opentitan/otbn/otbnproxy.h index 2d5080b6cf5d2..0c74f1edd7dca 100644 --- a/include/hw/opentitan/otbn/otbnproxy.h +++ b/include/hw/opentitan/otbn/otbnproxy.h @@ -77,7 +77,7 @@ extern int ot_otbn_proxy_execute(OTBNProxy proxy, bool dumpstate); extern int ot_otbn_proxy_wipe_memory(OTBNProxy proxy, bool doi); extern bool ot_otbn_proxy_acknowledge_execution(OTBNProxy proxy); extern uint32_t -ot_otbn_proxy_read_memory(OTBNProxy proxy, bool doi, uint32_t addr); +ot_otbn_proxy_read_memory(OTBNProxy proxy, bool doi, uint32_t addr, bool *valid); extern bool ot_otbn_proxy_write_memory(OTBNProxy proxy, bool doi, uint32_t addr, uint32_t val); extern enum OtOTBNStatus ot_otbn_proxy_get_status(OTBNProxy proxy); From 5709900f83ad884a4c8be64c5724d81a1f73d099 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:34 +0000 Subject: [PATCH 23/52] [ot] hw/opentitan: entropy_src: implement health test and FW bypass Align `hw/opentitan/ot_entropy_src.c` with Earlgrey `entropy_src_core.sv` and `entropy_src_main_sm.sv`: - Implement RepCnt, RepCntS, AdaptP, Bucket, and Markov health test windows, watermarks, fail counters, alert threshold checks, and recoverable alerts. - Implement `FW_OV_CONTROL` (`FW_OV_MODE` and `FW_OV_ENTROPY_INSERT`), `FW_OV_SHA3_START` write-when-disabled and MuBi4 field alerts, observe FIFO threshold interrupts, and `FW_OV_WR_FIFO_FULL` gating. - Support `ES_TYPE` bypass vs SHA-3 conditioning, `ES_FINAL_FIFO_DEPTH = 3`, 12-word `ENTROPY_DATA` software reads (`swread_fifo`), 64-bit `es_bus_cmp` duplicate seed detection, `ERR_CODE_TEST` FSM escalation (`0x13d`), and TL-UL `reg_permit` byte-enable checks. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I4193ce74cf94eb787272bb02323ad8a16a6a6964 --- hw/opentitan/ot_entropy_src.c | 984 +++++++++++++++++++++++++--------- 1 file changed, 745 insertions(+), 239 deletions(-) diff --git a/hw/opentitan/ot_entropy_src.c b/hw/opentitan/ot_entropy_src.c index 3acb7936a1335..fbaebd13f7ced 100644 --- a/hw/opentitan/ot_entropy_src.c +++ b/hw/opentitan/ot_entropy_src.c @@ -284,6 +284,9 @@ REG32(MAIN_SM_STATE, 0xc0u) (R_ERR_CODE_ES_ACK_SM_ERR_MASK | R_ERR_CODE_ES_MAIN_SM_ERR_MASK | \ R_ERR_CODE_ES_CNTR_ERR_MASK | R_ERR_CODE_SHA3_STATE_ERR_MASK | \ R_ERR_CODE_SHA3_RST_STORAGE_ERR_MASK) +#define ERR_CODE_LOCAL_ESCALATE_MASK \ + (R_ERR_CODE_ES_ACK_SM_ERR_MASK | R_ERR_CODE_ES_MAIN_SM_ERR_MASK | \ + R_ERR_CODE_ES_CNTR_ERR_MASK | R_ERR_CODE_SHA3_STATE_ERR_MASK) /* * this is an alias for the CHECK_MULTIBOOT macro, as the RECOV_ALERT_STS @@ -407,7 +410,7 @@ static const char *REG_HI_NAMES[REGS_HI_COUNT] = { * FIFO packets. They are assembled into either 384-bit or 2048-bit packets. */ #define ES_FILL_BITS 128u -#define ES_FINAL_FIFO_DEPTH 4u +#define ES_FINAL_FIFO_DEPTH 3u #define OT_ENTROPY_SRC_FILL_WORD_COUNT (ES_FILL_BITS / (8u * sizeof(uint32_t))) #define ES_WORD_COUNT (OT_ENTROPY_SRC_WORD_COUNT) #define ES_SWREAD_FIFO_WORD_COUNT ES_WORD_COUNT @@ -480,10 +483,17 @@ struct OtEntropySrcState { hash_state sha3_state; /* libtomcrypt hash state */ OtEntropySrcFsmState state; uint64_t noise_fill_pace_ns; + uint64_t rdata_capt; /* last popped esfinal lower 64 bits */ + uint64_t swread_capt_data; /* current swread_fifo seed lower 64 bits */ unsigned cond_word; /* count of words processed with SHA3 till hash */ unsigned noise_count; /* count of consumed noise words since enabled */ unsigned packet_count; /* count of output packets since enabled */ + unsigned ht_symbol_count; /* count of symbols in current HT window */ + unsigned swread_idx; /* word index (0..11) for ENTROPY_DATA reads */ + bool last_ht_failed; /* whether the last HT window failed */ bool obs_fifo_en; /* observe FIFO accept incoming data */ + bool rdata_capt_vld; /* whether rdata_capt holds a valid seed prefix */ + bool fifo_err_pulse; /* 1-cycle FIFO error pulse for fatal alert/IRQ */ char *ot_id; unsigned version; /* emulated version */ @@ -552,74 +562,72 @@ static bool ot_entropy_src_is_module_enabled(const OtEntropySrcState *s); static bool ot_entropy_src_is_fips_enabled(const OtEntropySrcState *s); static bool ot_entropy_src_is_hw_route(const OtEntropySrcState *s); static bool ot_entropy_src_is_fips_capable(const OtEntropySrcState *s); +static bool ot_entropy_src_is_bypass_mode(const OtEntropySrcState *s); +static bool ot_entropy_src_is_fw_ov_mode(const OtEntropySrcState *s); +static bool ot_entropy_src_is_fw_ov_entropy_insert(const OtEntropySrcState *s); static void ot_entropy_src_update_alerts(OtEntropySrcState *s); +static void ot_entropy_src_check_bus_cmp(OtEntropySrcState *s, uint64_t low64); static void ot_entropy_src_update_filler(OtEntropySrcState *s); +static bool ot_entropy_src_can_consume_entropy(const OtEntropySrcState *s); +static bool ot_entropy_src_fill_noise(OtEntropySrcState *s); +static void ot_entropy_src_noise_refill(void *opaque); static int ot_entropy_src_get_entropy( OtEntropySrcState *ess, uint64_t random[OT_ENTROPY_SRC_DWORD_COUNT], bool *fips) { if (!ot_entropy_src_is_module_enabled(ess)) { - qemu_log_mask(LOG_GUEST_ERROR, "%s: entropy_src is down\n", __func__); - return -1; + return OT_ENTROPY_SRC_BOOT_DELAY_NS; } - bool fips_compliant; + if (!ot_entropy_src_is_hw_route(ess)) { + return OT_ENTROPY_SRC_BOOT_DELAY_NS; + } - switch (ess->state) { - case ENTROPY_SRC_BOOT_PHASE_DONE: - fips_compliant = false; - break; - case ENTROPY_SRC_CONT_HT_RUNNING: - case ENTROPY_SRC_CONT_HT_START: - case ENTROPY_SRC_SHA3_MSG_DONE: - case ENTROPY_SRC_SHA3_PROCESS: - case ENTROPY_SRC_SHA3_VALID: - case ENTROPY_SRC_SHA3_DONE: - fips_compliant = true; - break; - case ENTROPY_SRC_BOOT_HT_RUNNING: - case ENTROPY_SRC_BOOT_POST_HT_CHK: - case ENTROPY_SRC_STARTUP_HT_START: - case ENTROPY_SRC_STARTUP_PHASE1: - case ENTROPY_SRC_STARTUP_PASS1: - case ENTROPY_SRC_STARTUP_FAIL1: { - int64_t wait_ns; - if (timer_pending(ess->scheduler)) { - /* computed delay fits into a 31-bit value */ - wait_ns = ((int64_t)timer_expire_time_ns(ess->scheduler)) - - qemu_clock_get_ns(OT_VIRTUAL_CLOCK); - wait_ns = MAX(wait_ns, OT_ENTROPY_SRC_WAIT_DELAY_NS); - } else { - wait_ns = OT_ENTROPY_SRC_WAIT_DELAY_NS; + if (!ot_entropy_src_is_fw_ov_mode(ess)) { + ess->last_ht_failed = false; + for (unsigned retry = 0; + retry < 128u && + ot_fifo32_num_used(&ess->final_fifo) < ES_WORD_COUNT && + ot_entropy_src_can_consume_entropy(ess) && !ess->last_ht_failed; + retry++) { + ot_entropy_src_noise_refill(ess); } - trace_ot_entropy_src_init_ongoing(ess->ot_id, STATE_NAME(ess->state), - ess->state, (int)wait_ns); - /* not ready */ - return (int)wait_ns; - } - case ENTROPY_SRC_IDLE: - qemu_log_mask(LOG_GUEST_ERROR, "%s: module is not enabled\n", __func__); - return -1; - case ENTROPY_SRC_FW_INSERT_START: - case ENTROPY_SRC_FW_INSERT_MSG: - case ENTROPY_SRC_ALERT_STATE: - case ENTROPY_SRC_ALERT_HANG: - case ENTROPY_SRC_ERROR: - default: - qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid state: [%s:%d]\n", __func__, - STATE_NAME(ess->state), ess->state); - return -1; + } else if (ot_fifo32_num_used(&ess->final_fifo) < ES_WORD_COUNT) { + ot_entropy_src_update_filler(ess); } - if (!ot_entropy_src_is_hw_route(ess)) { - qemu_log_mask(LOG_GUEST_ERROR, "%s: HW route not selected\n", __func__); - return -1; + if (ess->state == ENTROPY_SRC_ERROR) { + return OT_ENTROPY_SRC_BOOT_DELAY_NS; } if (ot_fifo32_num_used(&ess->final_fifo) < ES_WORD_COUNT) { + if (ess->state == ENTROPY_SRC_BOOT_HT_RUNNING || + ess->state == ENTROPY_SRC_BOOT_POST_HT_CHK || + ess->state == ENTROPY_SRC_STARTUP_HT_START || + ess->state == ENTROPY_SRC_STARTUP_PHASE1 || + ess->state == ENTROPY_SRC_STARTUP_PASS1 || + ess->state == ENTROPY_SRC_STARTUP_FAIL1) { + int64_t wait_ns; + if (timer_pending(ess->scheduler)) { + /* computed delay fits into a 31-bit value */ + wait_ns = ((int64_t)timer_expire_time_ns(ess->scheduler)) - + qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + wait_ns = MAX(wait_ns, OT_ENTROPY_SRC_WAIT_DELAY_NS); + } else { + wait_ns = OT_ENTROPY_SRC_WAIT_DELAY_NS; + } + trace_ot_entropy_src_init_ongoing(ess->ot_id, + STATE_NAME(ess->state), + ess->state, (int)wait_ns); + /* not ready */ + return (int)wait_ns; + } trace_ot_entropy_src_no_entropy(ess->ot_id, ot_fifo32_num_used(&ess->final_fifo)); + if (!ot_entropy_src_can_consume_entropy(ess)) { + return OT_ENTROPY_SRC_BOOT_DELAY_NS; + } return OT_ENTROPY_SRC_WAIT_DELAY_NS; } @@ -629,18 +637,24 @@ static int ot_entropy_src_get_entropy( g_assert(!ot_fifo32_is_empty(&ess->final_fifo)); randu32[pos++] = ot_fifo32_pop(&ess->final_fifo); } + ot_entropy_src_check_bus_cmp(ess, + ((uint64_t)randu32[1] << 32) | randu32[0]); bool fips_capable = ot_entropy_src_is_fips_capable(ess); - /* note: fips compliancy is only simulated here for now */ - *fips = fips_compliant && fips_capable; + /* + * In Earlgrey RTL (entropy_src_core.sv:2956), fips_compliance is strictly + * es_enable_fo[13] && fips_flag_pfe. + */ + *fips = ot_entropy_src_is_module_enabled(ess) && + REG_MB4_IS_TRUE(ess, hi, CONF, FIPS_FLAG); trace_ot_entropy_src_get_random_fips( ess->ot_id, STATE_NAME(ess->state), ot_entropy_src_is_fips_enabled(ess), REG_MB4_IS_TRUE(ess, hi, ENTROPY_CONTROL, ES_ROUTE), REG_MB4_IS_TRUE(ess, hi, ENTROPY_CONTROL, ES_TYPE), - REG_MB4_IS_FALSE(ess, hi, CONF, RNG_BIT_ENABLE), fips_capable, - fips_compliant, *fips); + REG_MB4_IS_FALSE(ess, hi, CONF, RNG_BIT_ENABLE), fips_capable, *fips, + *fips); if (ot_fifo32_num_used(&ess->final_fifo) < ES_WORD_COUNT) { ot_entropy_src_update_filler(ess); @@ -696,7 +710,7 @@ static bool ot_entropy_src_is_module_enabled(const OtEntropySrcState *s) static bool ot_entropy_src_is_module_disabled(const OtEntropySrcState *s) { - return REG_MB4_IS_FALSE(s, hi, MODULE_ENABLE, MODULE_ENABLE); + return !ot_entropy_src_is_module_enabled(s); } static bool ot_entropy_src_is_fips_enabled(const OtEntropySrcState *s) @@ -715,7 +729,10 @@ static void ot_entropy_src_update_irqs(OtEntropySrcState *s) static bool ot_entropy_src_is_final_fifo_slot_available(const OtEntropySrcState *s) { - return ot_fifo32_num_free(&s->final_fifo) >= ES_WORD_COUNT; + unsigned used_words = + ot_fifo32_num_used(&s->final_fifo) + + (!ot_fifo32_is_empty(&s->swread_fifo) ? ES_WORD_COUNT : 0u); + return (used_words + ES_WORD_COUNT) <= ES_FINAL_FIFO_WORD_COUNT; } static bool ot_entropy_src_is_hw_route(const OtEntropySrcState *s) @@ -753,34 +770,14 @@ static bool ot_entropy_src_is_fips_capable(const OtEntropySrcState *s) REG_MB4_IS_FALSE(s, hi, CONF, RNG_BIT_ENABLE); } -static unsigned ot_alert_get_alert_fail_count(const OtEntropySrcState *s) -{ - unsigned count; - - count = FIELD_EX32(s->regs_hi[R_ALERT_FAIL_COUNTS], ALERT_FAIL_COUNTS, - REPCNT_FAIL_COUNT); - count += FIELD_EX32(s->regs_hi[R_ALERT_FAIL_COUNTS], ALERT_FAIL_COUNTS, - ADAPTP_HI_FAIL_COUNT); - count += FIELD_EX32(s->regs_hi[R_ALERT_FAIL_COUNTS], ALERT_FAIL_COUNTS, - ADAPTP_LO_FAIL_COUNT); - count += FIELD_EX32(s->regs_hi[R_ALERT_FAIL_COUNTS], ALERT_FAIL_COUNTS, - BUCKET_FAIL_COUNT); - count += FIELD_EX32(s->regs_hi[R_ALERT_FAIL_COUNTS], ALERT_FAIL_COUNTS, - MARKOV_HI_FAIL_COUNT); - count += FIELD_EX32(s->regs_hi[R_ALERT_FAIL_COUNTS], ALERT_FAIL_COUNTS, - MARKOV_LO_FAIL_COUNT); - count += FIELD_EX32(s->regs_hi[R_ALERT_FAIL_COUNTS], ALERT_FAIL_COUNTS, - REPCNTS_FAIL_COUNT); - - return count; -} - static void ot_entropy_src_change_state_line( OtEntropySrcState *s, OtEntropySrcFsmState state, int line) { OtEntropySrcFsmState old_state = s->state; switch (s->state) { + case ENTROPY_SRC_ERROR: + break; case ENTROPY_SRC_ALERT_STATE: s->state = ENTROPY_SRC_ALERT_HANG; break; @@ -810,26 +807,115 @@ static void ot_entropy_src_change_state_line( #define ot_entropy_src_change_state(_s_, _st_) \ ot_entropy_src_change_state_line(_s_, _st_, __LINE__) +static void ot_entropy_src_update_regwen(OtEntropySrcState *s) +{ + s->regs_lo[R_REGWEN] = + (uint32_t)(s->regs_lo[R_SW_REGUPD] == R_SW_REGUPD_VAL_MASK && + ot_entropy_src_is_module_disabled(s)); +} + +static void ot_entropy_src_disable(OtEntropySrcState *s) +{ + timer_del(s->scheduler); + ot_fifo32_reset(&s->input_fifo); + ot_fifo32_reset(&s->precon_fifo); + ot_fifo32_reset(&s->bypass_fifo); + ot_fifo32_reset(&s->observe_fifo); + ot_fifo32_reset(&s->swread_fifo); + ot_fifo32_reset(&s->final_fifo); + s->rdata_capt = 0u; + s->swread_capt_data = 0u; + s->rdata_capt_vld = false; + s->cond_word = 0u; + s->noise_count = 0u; + s->packet_count = 0u; + s->ht_symbol_count = 0u; + s->swread_idx = 0u; + s->last_ht_failed = false; + s->obs_fifo_en = ot_entropy_src_is_fw_ov_mode(s); + s->regs_hi[R_FW_OV_RD_FIFO_OVERFLOW] &= ~R_FW_OV_RD_FIFO_OVERFLOW_VAL_MASK; + s->regs_hi[R_DEBUG_STATUS] = 0x10000u; + ot_entropy_src_change_state(s, ENTROPY_SRC_IDLE); + ot_entropy_src_update_irqs(s); +} + +static void ot_entropy_src_trigger_recov_alert(OtEntropySrcState *s, + uint32_t alert_sts_mask) +{ + s->regs_hi[R_RECOV_ALERT_STS] |= alert_sts_mask; + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 1); + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 0); +} + +static void ot_entropy_src_check_bus_cmp(OtEntropySrcState *s, uint64_t low64) +{ + if (s->rdata_capt_vld && s->rdata_capt == low64) { + ot_entropy_src_trigger_recov_alert( + s, R_RECOV_ALERT_STS_ES_BUS_CMP_ALERT_MASK); + } + s->rdata_capt = low64; + s->rdata_capt_vld = true; +} + static void ot_entropy_src_update_alerts(OtEntropySrcState *s) { - unsigned alert_threshold = - FIELD_EX32(s->regs_hi[R_ALERT_THRESHOLD], ALERT_THRESHOLD, VAL); - unsigned alert_count = ot_alert_get_alert_fail_count(s); - bool recoverable = (bool)s->regs_hi[R_RECOV_ALERT_STS]; - uint32_t level = s->regs_lo[R_ALERT_TEST]; - if (alert_count >= alert_threshold || recoverable) { - level |= 1u << ALERT_RECOVERABLE; - } - uint32_t fatal_alert = s->regs_hi[R_ERR_CODE] & ERR_CODE_FATAL_ERROR_MASK; - fatal_alert |= - (1u << s->regs_hi[R_ERR_CODE_TEST]) & ERR_CODE_FATAL_ERROR_MASK; + if (s->regs_lo[R_ALERT_TEST] & R_ALERT_TEST_RECOV_ALERT_MASK) { + s->regs_lo[R_ALERT_TEST] &= ~R_ALERT_TEST_RECOV_ALERT_MASK; + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 1); + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 0); + } + uint32_t fatal_alert = + (s->regs_hi[R_ERR_CODE] & ERR_CODE_FATAL_ERROR_MASK) | + ((ot_entropy_src_is_module_enabled(s) && s->fifo_err_pulse) ? + (s->regs_hi[R_ERR_CODE] & ERR_CODE_MASK) : + 0u); + s->fifo_err_pulse = false; if (fatal_alert) { - level |= 1u << ALERT_FATAL; + s->regs_lo[R_INTR_STATE] |= INTR_ES_FATAL_ERR_MASK; + ot_entropy_src_update_irqs(s); } + if (fatal_alert || + (s->regs_lo[R_ALERT_TEST] & R_ALERT_TEST_FATAL_ALERT_MASK)) { + s->regs_lo[R_ALERT_TEST] &= ~R_ALERT_TEST_FATAL_ALERT_MASK; + ibex_irq_set(&s->alerts[ALERT_FATAL], 1); + } else if (!ot_entropy_src_is_module_enabled(s) && + !(s->regs_hi[R_ERR_CODE] & ERR_CODE_FATAL_ERROR_MASK)) { + ibex_irq_set(&s->alerts[ALERT_FATAL], 0); + } +} - for (unsigned ix = 0; ix < NUM_ALERTS; ix++) { - ibex_irq_set(&s->alerts[ix], (int)((level >> ix) & 0x1u)); +static bool ot_entropy_src_mb4_is_invalid(uint8_t mbbool) +{ + return mbbool != OT_MULTIBITBOOL4_TRUE && mbbool != OT_MULTIBITBOOL4_FALSE; +} + +static uint32_t ot_entropy_src_get_cfg_alerts(const OtEntropySrcState *s) +{ +#define CHK_MB4(_reg_, _fld_, _bit_) \ + (ot_entropy_src_mb4_is_invalid( \ + FIELD_EX32(s->regs_hi[R_##_reg_], _reg_, _fld_)) ? \ + R_RECOV_ALERT_STS_##_bit_##_FIELD_ALERT_MASK : \ + 0u) + uint32_t alerts = + CHK_MB4(MODULE_ENABLE, MODULE_ENABLE, MODULE_ENABLE) | + CHK_MB4(CONF, FIPS_ENABLE, FIPS_ENABLE) | + CHK_MB4(CONF, FIPS_FLAG, FIPS_FLAG) | + CHK_MB4(CONF, RNG_FIPS, RNG_FIPS) | + CHK_MB4(CONF, RNG_BIT_ENABLE, RNG_BIT_ENABLE) | + CHK_MB4(CONF, V2_ENTROPY_DATA_REG_ENABLE, ENTROPY_DATA_REG_ENABLE) | + CHK_MB4(CONF, V2_THRESHOLD_SCOPE, THRESHOLD_SCOPE) | + CHK_MB4(ENTROPY_CONTROL, ES_ROUTE, ES_ROUTE) | + CHK_MB4(ENTROPY_CONTROL, ES_TYPE, ES_TYPE) | + CHK_MB4(FW_OV_CONTROL, FW_OV_MODE, FW_OV_MODE) | + CHK_MB4(FW_OV_CONTROL, FW_OV_ENTROPY_INSERT, FW_OV_ENTROPY_INSERT) | + CHK_MB4(FW_OV_SHA3_START, FW_OV_INSERT_START, FW_OV_SHA3_START); +#undef CHK_MB4 + if ((uint16_t)s->regs_hi[R_ALERT_THRESHOLD] != + (uint16_t)(~(s->regs_hi[R_ALERT_THRESHOLD] >> 16u))) { + alerts |= R_RECOV_ALERT_STS_ES_THRESH_CFG_ALERT_MASK; } + + return alerts; } static bool ot_entropy_src_check_multibitboot( @@ -843,13 +929,16 @@ static bool ot_entropy_src_check_multibitboot( break; } - s->regs_hi[R_RECOV_ALERT_STS] |= 1u << alert_bit; - ot_entropy_src_update_alerts(s); + ot_entropy_src_trigger_recov_alert(s, 1u << alert_bit); return false; } static bool ot_entropy_src_can_consume_entropy(const OtEntropySrcState *s) { + if (s->state == ENTROPY_SRC_ALERT_STATE || + s->state == ENTROPY_SRC_ALERT_HANG || s->state == ENTROPY_SRC_ERROR) { + return false; + } return ot_entropy_src_is_module_enabled(s) && !(ot_entropy_src_is_fw_ov_entropy_insert(s) && !ot_entropy_src_is_fw_ov_mode(s)); @@ -860,7 +949,15 @@ static void ot_entropy_src_update_filler(OtEntropySrcState *s) /* fill granule is OT_ENTROPY_SRC_FILL_WORD_COUNT bits */ bool input = ot_fifo32_num_free(&s->input_fifo) >= OT_ENTROPY_SRC_FILL_WORD_COUNT; - bool output = ot_fifo32_num_free(&s->final_fifo) >= ES_WORD_COUNT; + bool output = ot_entropy_src_is_final_fifo_slot_available(s); + if (ot_entropy_src_is_fw_ov_mode(s)) { + bool obs_output = + s->obs_fifo_en || !(s->regs_hi[R_FW_OV_RD_FIFO_OVERFLOW] & + R_FW_OV_RD_FIFO_OVERFLOW_VAL_MASK); + output = ot_entropy_src_is_fw_ov_entropy_insert(s) ? + obs_output : + (output || obs_output); + } bool process = ot_entropy_src_can_consume_entropy(s); bool accept_entropy = input && output && process; @@ -888,20 +985,15 @@ static void ot_entropy_src_update_filler(OtEntropySrcState *s) static bool ot_entropy_src_can_condition_entropy(const OtEntropySrcState *s) { - if (!ot_fifo32_is_full(&s->precon_fifo)) { - /* room in preconditioner packer */ - return true; - } - if (ot_entropy_src_is_final_fifo_slot_available(s)) { - /* room in output FIFO */ - return true; - } - - return false; + return !ot_fifo32_is_full(&s->precon_fifo); } static bool ot_entropy_src_can_bypass_entropy(const OtEntropySrcState *s) { + if (s->state == ENTROPY_SRC_BOOT_PHASE_DONE && + !ot_entropy_src_is_fw_ov_entropy_insert(s)) { + return false; + } if (!ot_fifo32_is_full(&s->bypass_fifo)) { /* room in bypass packer */ return true; @@ -918,9 +1010,12 @@ static bool ot_entropy_src_push_entropy_to_conditioner(OtEntropySrcState *s, uint32_t word) { int res; - if (s->cond_word == 0) { + if (s->cond_word == 0 && !ot_entropy_src_is_fw_ov_entropy_insert(s)) { res = sha3_384_init(&s->sha3_state); g_assert(res == CRYPT_OK); + if (s->state == ENTROPY_SRC_STARTUP_HT_START) { + ot_entropy_src_change_state(s, ENTROPY_SRC_STARTUP_PHASE1); + } } g_assert(!ot_fifo32_is_full(&s->precon_fifo)); @@ -931,8 +1026,6 @@ ot_entropy_src_push_entropy_to_conditioner(OtEntropySrcState *s, uint32_t word) return false; } - ot_entropy_src_change_state(s, ENTROPY_SRC_SHA3_PROCESS); - uint32_t size; const uint32_t *buf; buf = ot_fifo32_peek_buf(&s->precon_fifo, s->precon_fifo.num, &size); @@ -948,12 +1041,6 @@ ot_entropy_src_push_entropy_to_conditioner(OtEntropySrcState *s, uint32_t word) return true; } -static bool ot_entropy_src_can_hash(const OtEntropySrcState *s) -{ - return ot_fifo32_is_empty(&s->precon_fifo) && - (s->cond_word >= (2048 / (8u * sizeof(uint32_t)))); -} - static void ot_entropy_src_perform_hash(OtEntropySrcState *s) { uint32_t hash[OT_ENTROPY_SRC_WORD_COUNT]; @@ -966,35 +1053,60 @@ static void ot_entropy_src_perform_hash(OtEntropySrcState *s) OT_ENTROPY_SRC_WORD_COUNT * sizeof(uint32_t)); + ot_entropy_src_change_state(s, ENTROPY_SRC_SHA3_PROCESS); + ot_entropy_src_change_state(s, ENTROPY_SRC_SHA3_VALID); + ot_entropy_src_change_state(s, ENTROPY_SRC_SHA3_DONE); ot_entropy_src_change_state(s, ENTROPY_SRC_SHA3_MSG_DONE); - for (unsigned ix = 0; ix < OT_ENTROPY_SRC_WORD_COUNT; ix++) { - g_assert(!ot_fifo32_is_full(&s->final_fifo)); - ot_fifo32_push(&s->final_fifo, hash[ix]); + /* + * In RTL (entropy_src_core.sv): "No backpressure is possible at this point. + * If the esfinal FIFO is already full and a new seed is pushed, the push is + * ignored and the seed is lost." + */ + if (ot_entropy_src_is_final_fifo_slot_available(s)) { + for (unsigned ix = 0; ix < OT_ENTROPY_SRC_WORD_COUNT; ix++) { + ot_fifo32_push(&s->final_fifo, hash[ix]); + } } s->packet_count += 1u; - - ot_entropy_src_change_state(s, REG_MB4_IS_TRUE(s, hi, FW_OV_SHA3_START, - FW_OV_INSERT_START) ? - ENTROPY_SRC_IDLE : - ENTROPY_SRC_CONT_HT_START); + if (ot_entropy_src_is_fw_ov_mode(s) && + ot_entropy_src_is_fw_ov_entropy_insert(s)) { + ot_entropy_src_change_state(s, ENTROPY_SRC_IDLE); + if (ot_entropy_src_is_module_enabled(s) && + !ot_entropy_src_is_bypass_mode(s)) { + res = sha3_384_init(&s->sha3_state); + g_assert(res == CRYPT_OK); + s->cond_word = 0; + ot_entropy_src_change_state(s, + REG_MB4_IS_TRUE(s, hi, FW_OV_SHA3_START, + FW_OV_INSERT_START) ? + ENTROPY_SRC_FW_INSERT_MSG : + ENTROPY_SRC_FW_INSERT_START); + } + } else { + ot_entropy_src_change_state(s, ENTROPY_SRC_CONT_HT_START); + ot_entropy_src_change_state(s, ENTROPY_SRC_CONT_HT_RUNNING); + } } static bool ot_entropy_src_push_bypass_entropy(OtEntropySrcState *s, uint32_t word) { - g_assert(!ot_fifo32_is_full(&s->bypass_fifo)); - - ot_fifo32_push(&s->bypass_fifo, word); + if (!ot_fifo32_is_full(&s->bypass_fifo)) { + ot_fifo32_push(&s->bypass_fifo, word); + } if (!ot_fifo32_is_full(&s->bypass_fifo)) { /* need a whole OT_ENTROPY_SRC_PACKET_SIZE_BITS packet to move on */ return false; } /* bypass conditioner full/ready, empty it into the final FIFO */ - while (!ot_fifo32_is_empty(&s->bypass_fifo)) { - g_assert(!ot_fifo32_is_full(&s->final_fifo)); - ot_fifo32_push(&s->final_fifo, ot_fifo32_pop(&s->bypass_fifo)); + if (ot_entropy_src_is_final_fifo_slot_available(s)) { + while (!ot_fifo32_is_empty(&s->bypass_fifo)) { + ot_fifo32_push(&s->final_fifo, ot_fifo32_pop(&s->bypass_fifo)); + } + } else { + ot_fifo32_reset(&s->bypass_fifo); } s->packet_count += 1u; @@ -1006,34 +1118,185 @@ ot_entropy_src_push_bypass_entropy(OtEntropySrcState *s, uint32_t word) return true; } +static bool ot_entropy_src_is_entropy_data_enabled(const OtEntropySrcState *s) +{ + return REG_MB4_IS_TRUE(s, hi, CONF, V2_ENTROPY_DATA_REG_ENABLE); +} + static void ot_entropy_src_update_fw_route(OtEntropySrcState *s) { if (ot_fifo32_num_used(&s->final_fifo) >= ES_WORD_COUNT) { trace_ot_entropy_src_info(s->ot_id, "FW ROUTE"); if (ot_fifo32_is_empty(&s->swread_fifo)) { /* refill swread FIFO */ - unsigned count = ES_WORD_COUNT; - while (count--) { - ot_fifo32_push(&s->swread_fifo, ot_fifo32_pop(&s->final_fifo)); + uint32_t w0 = 0; + uint32_t w1 = 0; + for (unsigned ix = 0; ix < ES_WORD_COUNT; ix++) { + uint32_t w = ot_fifo32_pop(&s->final_fifo); + if (ix == 0u) { + w0 = w; + } else if (ix == 1u) { + w1 = w; + } + ot_fifo32_push(&s->swread_fifo, w); } - s->regs_lo[R_INTR_STATE] |= INTR_ES_ENTROPY_VALID_MASK; + s->swread_capt_data = ((uint64_t)w1 << 32) | (uint64_t)w0; trace_ot_entropy_src_available(s->ot_id, STATE_NAME(s->state), s->state); ot_entropy_src_update_filler(s); } } + if (!ot_fifo32_is_empty(&s->swread_fifo) && + ot_entropy_src_is_module_enabled(s) && ot_entropy_src_is_fw_route(s) && + ot_entropy_src_is_entropy_data_enabled(s)) { + s->regs_lo[R_INTR_STATE] |= INTR_ES_ENTROPY_VALID_MASK; + } +} + +static void +ot_entropy_src_update_watermark(OtEntropySrcState *s, unsigned reg, bool bypass, + uint16_t val, bool low_watermark) +{ + unsigned shift = bypass ? 16u : 0u; + uint16_t cur = (uint16_t)((s->regs_hi[reg] >> shift) & 0xffffu); + uint16_t next = low_watermark ? MIN(cur, val) : MAX(cur, val); + s->regs_hi[reg] = + (s->regs_hi[reg] & ~(0xffffu << shift)) | ((uint32_t)next << shift); +} + +static void +ot_entropy_src_inc_alert_fail_count(OtEntropySrcState *s, unsigned shift) +{ + uint32_t count = (s->regs_hi[R_ALERT_FAIL_COUNTS] >> shift) & 0xfu; + if (count < 0xfu) { + count += 1u; + s->regs_hi[R_ALERT_FAIL_COUNTS] = + (s->regs_hi[R_ALERT_FAIL_COUNTS] & ~(0xfu << shift)) | + (count << shift); + } +} + +static bool +ot_entropy_src_eval_health_tests(OtEntropySrcState *s, unsigned window_symbols) +{ + bool bypass = ot_entropy_src_is_bypass_mode(s); + unsigned shift = bypass ? 16u : 0u; + bool scope = REG_MB4_IS_TRUE(s, hi, CONF, V2_THRESHOLD_SCOPE); + + uint16_t repcnt_hi = (uint16_t)(s->regs_hi[R_REPCNT_THRESHOLDS] >> shift); + uint16_t repcnts_hi = (uint16_t)(s->regs_hi[R_REPCNTS_THRESHOLDS] >> shift); + uint16_t adaptp_hi = + (uint16_t)(s->regs_hi[R_ADAPTP_HI_THRESHOLDS] >> shift); + uint16_t adaptp_lo = + (uint16_t)(s->regs_hi[R_ADAPTP_LO_THRESHOLDS] >> shift); + uint16_t bucket_hi = (uint16_t)(s->regs_hi[R_BUCKET_THRESHOLDS] >> shift); + uint16_t markov_hi = + (uint16_t)(s->regs_hi[R_MARKOV_HI_THRESHOLDS] >> shift); + uint16_t markov_lo = + (uint16_t)(s->regs_hi[R_MARKOV_LO_THRESHOLDS] >> shift); + + /* + * Simulated typical statistical counts for ideal RNG noise over + * window_symbols symbols (each symbol is 4 bits across 4 RNG lines). + */ + uint16_t repcnt_val = 14u; + uint16_t repcnts_val = 4u; + unsigned var = (s->packet_count < 2u) ? 8u : 18u; + uint16_t adaptp_hi_val = (uint16_t)(scope ? (window_symbols * 2u + 16u) : + (window_symbols / 2u + var)); + uint16_t adaptp_lo_val = + (uint16_t)(scope ? + (window_symbols * 2u - 16u) : + (window_symbols > 32u ? window_symbols / 2u - var : 1u)); + uint16_t bucket_val = (uint16_t)(window_symbols / 16u + 16u); + uint16_t markov_hi_val = (uint16_t)(scope ? (window_symbols + 16u) : + (window_symbols / 4u + var)); + uint16_t markov_lo_val = + (uint16_t)(scope ? + (window_symbols > 16u ? window_symbols - 16u : 1u) : + (window_symbols > 64u ? window_symbols / 4u - var : 1u)); + + ot_entropy_src_update_watermark(s, R_REPCNT_HI_WATERMARKS, bypass, + repcnt_val, false); + ot_entropy_src_update_watermark(s, R_REPCNTS_HI_WATERMARKS, bypass, + repcnts_val, false); + ot_entropy_src_update_watermark(s, R_ADAPTP_HI_WATERMARKS, bypass, + adaptp_hi_val, false); + ot_entropy_src_update_watermark(s, R_ADAPTP_LO_WATERMARKS, bypass, + adaptp_lo_val, true); + ot_entropy_src_update_watermark(s, R_BUCKET_HI_WATERMARKS, bypass, + bucket_val, false); + ot_entropy_src_update_watermark(s, R_MARKOV_HI_WATERMARKS, bypass, + markov_hi_val, false); + ot_entropy_src_update_watermark(s, R_MARKOV_LO_WATERMARKS, bypass, + markov_lo_val, true); + + bool any_fail = false; + + if (repcnt_val >= repcnt_hi) { + s->regs_hi[R_REPCNT_TOTAL_FAILS] += 1u; + ot_entropy_src_inc_alert_fail_count( + s, R_ALERT_FAIL_COUNTS_REPCNT_FAIL_COUNT_SHIFT); + any_fail = true; + } + if (repcnts_val >= repcnts_hi) { + s->regs_hi[R_REPCNTS_TOTAL_FAILS] += 1u; + ot_entropy_src_inc_alert_fail_count( + s, R_ALERT_FAIL_COUNTS_REPCNTS_FAIL_COUNT_SHIFT); + any_fail = true; + } + if (adaptp_hi_val >= adaptp_hi) { + s->regs_hi[R_ADAPTP_HI_TOTAL_FAILS] += 1u; + ot_entropy_src_inc_alert_fail_count( + s, R_ALERT_FAIL_COUNTS_ADAPTP_HI_FAIL_COUNT_SHIFT); + any_fail = true; + } + if (adaptp_lo > 0u && adaptp_lo_val <= adaptp_lo) { + s->regs_hi[R_ADAPTP_LO_TOTAL_FAILS] += 1u; + ot_entropy_src_inc_alert_fail_count( + s, R_ALERT_FAIL_COUNTS_ADAPTP_LO_FAIL_COUNT_SHIFT); + any_fail = true; + } + if (bucket_val >= bucket_hi) { + s->regs_hi[R_BUCKET_TOTAL_FAILS] += 1u; + ot_entropy_src_inc_alert_fail_count( + s, R_ALERT_FAIL_COUNTS_BUCKET_FAIL_COUNT_SHIFT); + any_fail = true; + } + if (markov_hi_val >= markov_hi) { + s->regs_hi[R_MARKOV_HI_TOTAL_FAILS] += 1u; + ot_entropy_src_inc_alert_fail_count( + s, R_ALERT_FAIL_COUNTS_MARKOV_HI_FAIL_COUNT_SHIFT); + any_fail = true; + } + if (markov_lo > 0u && markov_lo_val <= markov_lo) { + s->regs_hi[R_MARKOV_LO_TOTAL_FAILS] += 1u; + ot_entropy_src_inc_alert_fail_count( + s, R_ALERT_FAIL_COUNTS_MARKOV_LO_FAIL_COUNT_SHIFT); + any_fail = true; + } + + return any_fail; } static bool ot_entropy_src_consume_entropy(OtEntropySrcState *s, uint32_t word) { + if (!ot_entropy_src_can_consume_entropy(s)) { + return false; + } + bool fill_obs_fifo = ot_entropy_src_is_fw_ov_mode(s); bool hw_path = !ot_entropy_src_is_fw_ov_entropy_insert(s); bool bypass = ot_entropy_src_is_bypass_mode(s); if (hw_path) { - /* check that HW accept data */ - hw_path = bypass ? ot_entropy_src_can_bypass_entropy(s) : - ot_entropy_src_can_condition_entropy(s); + if (!ot_entropy_src_is_final_fifo_slot_available(s)) { + hw_path = false; + } else { + /* check that HW accept data */ + hw_path = bypass ? ot_entropy_src_can_bypass_entropy(s) : + ot_entropy_src_can_condition_entropy(s); + } } if (!(fill_obs_fifo || hw_path)) { @@ -1047,42 +1310,118 @@ static bool ot_entropy_src_consume_entropy(OtEntropySrcState *s, uint32_t word) hw_path, s->noise_count); if (fill_obs_fifo) { + if (ot_fifo32_is_empty(&s->observe_fifo)) { + s->obs_fifo_en = true; + s->regs_hi[R_FW_OV_RD_FIFO_OVERFLOW] &= + ~R_FW_OV_RD_FIFO_OVERFLOW_VAL_MASK; + } if (ot_fifo32_is_full(&s->observe_fifo)) { trace_ot_entropy_src_error(s->ot_id, "observe FIFO overflow", STATE_NAME(s->state), s->state); + s->obs_fifo_en = false; s->regs_hi[R_FW_OV_RD_FIFO_OVERFLOW] |= R_FW_OV_RD_FIFO_OVERFLOW_VAL_MASK; - } else { - if (s->obs_fifo_en) { - unsigned threshold = s->regs_hi[R_OBSERVE_FIFO_THRESH]; - ot_fifo32_push(&s->observe_fifo, word); - trace_ot_entropy_src_obs_fifo(s->ot_id, - ot_fifo32_num_used( - &s->observe_fifo), - threshold); - if (ot_fifo32_is_full(&s->observe_fifo)) { - /* can only be enabled back once the FIFO is emptied */ - trace_ot_entropy_src_info(s->ot_id, "observe FIFO is full"); - s->obs_fifo_en = false; - } - /* is it > or >= ? */ - if (ot_fifo32_num_used(&s->observe_fifo) >= threshold) { - s->regs_lo[R_INTR_STATE] |= INTR_ES_OBSERVE_FIFO_READY_MASK; - } - } else { - trace_ot_entropy_src_info(s->ot_id, "observe FIFO not enabled"); + } else if (s->obs_fifo_en) { + unsigned threshold = s->regs_hi[R_OBSERVE_FIFO_THRESH]; + ot_fifo32_push(&s->observe_fifo, word); + trace_ot_entropy_src_obs_fifo(s->ot_id, + ot_fifo32_num_used(&s->observe_fifo), + threshold); + if (threshold != 0 && + ot_fifo32_num_used(&s->observe_fifo) >= threshold) { + s->regs_lo[R_INTR_STATE] |= INTR_ES_OBSERVE_FIFO_READY_MASK; } } } if (hw_path) { - if (ot_entropy_src_is_bypass_mode(s)) { - ot_entropy_src_push_bypass_entropy(s, word); + if (bypass) { + if (!ot_fifo32_is_full(&s->bypass_fifo)) { + ot_fifo32_push(&s->bypass_fifo, word); + } } else { - if (ot_entropy_src_push_entropy_to_conditioner(s, word)) { - if (ot_entropy_src_can_hash(s)) { - trace_ot_entropy_src_info(s->ot_id, "can hash"); - ot_entropy_src_perform_hash(s); + ot_entropy_src_push_entropy_to_conditioner(s, word); + } + + uint32_t windows_reg = s->regs_hi[R_HEALTH_TEST_WINDOWS]; + unsigned window_symbols = + bypass ? (windows_reg >> 16u) : (windows_reg & 0xffffu); + + s->ht_symbol_count += 8u; + if (s->ht_symbol_count >= window_symbols) { + s->ht_symbol_count = 0u; + bool ht_fail = ot_entropy_src_eval_health_tests(s, window_symbols); + bool is_startup = + (s->state == ENTROPY_SRC_STARTUP_HT_START || + s->state == ENTROPY_SRC_STARTUP_PHASE1 || + s->state == ENTROPY_SRC_STARTUP_PASS1 || + s->state == ENTROPY_SRC_STARTUP_FAIL1); + if (ht_fail) { + s->last_ht_failed = true; + ot_fifo32_reset(&s->bypass_fifo); + ot_fifo32_reset(&s->precon_fifo); + s->cond_word = 0u; + if (!bypass) { + int res = sha3_384_init(&s->sha3_state); + g_assert(res == CRYPT_OK); + } + + unsigned alert_thresh = + FIELD_EX32(s->regs_hi[R_ALERT_THRESHOLD], ALERT_THRESHOLD, + VAL); + uint16_t alert_thresh_inv = + (uint16_t)~FIELD_EX32(s->regs_hi[R_ALERT_THRESHOLD], + ALERT_THRESHOLD, INV); + uint32_t max_cnt = + (alert_thresh == 0xffffu) ? 0xfffeu : 0xffffu; + if (s->regs_hi[R_ALERT_SUMMARY_FAIL_COUNTS] < max_cnt) { + s->regs_hi[R_ALERT_SUMMARY_FAIL_COUNTS] += 1u; + } + + uint32_t fail_cnt = s->regs_hi[R_ALERT_SUMMARY_FAIL_COUNTS]; + bool thresh_fail = + (alert_thresh != 0 && fail_cnt >= alert_thresh) || + (alert_thresh_inv != 0 && fail_cnt >= alert_thresh_inv); + + if (s->state == ENTROPY_SRC_STARTUP_FAIL1 || + (!is_startup && thresh_fail)) { + s->regs_lo[R_INTR_STATE] |= INTR_ES_HEALTH_TEST_FAILED_MASK; + ot_entropy_src_update_irqs(s); + ot_entropy_src_trigger_recov_alert( + s, R_RECOV_ALERT_STS_ES_MAIN_SM_ALERT_MASK); + ot_entropy_src_change_state(s, ENTROPY_SRC_ALERT_STATE); + ot_entropy_src_change_state(s, ENTROPY_SRC_ALERT_HANG); + } else if (is_startup) { + ot_entropy_src_change_state(s, ENTROPY_SRC_STARTUP_FAIL1); + } + } else { + s->last_ht_failed = false; + s->regs_hi[R_ALERT_SUMMARY_FAIL_COUNTS] = 0u; + s->regs_hi[R_ALERT_FAIL_COUNTS] = 0u; + s->regs_hi[R_EXTHT_FAIL_COUNTS] = 0u; + + if (bypass) { + while (!ot_fifo32_is_empty(&s->bypass_fifo) && + !ot_fifo32_is_full(&s->final_fifo)) { + ot_fifo32_push(&s->final_fifo, + ot_fifo32_pop(&s->bypass_fifo)); + } + ot_fifo32_reset(&s->bypass_fifo); + s->packet_count += 1u; + if (s->state == ENTROPY_SRC_BOOT_HT_RUNNING) { + ot_entropy_src_change_state( + s, ENTROPY_SRC_BOOT_POST_HT_CHK); + ot_entropy_src_change_state( + s, ENTROPY_SRC_BOOT_PHASE_DONE); + } + } else if (s->state == ENTROPY_SRC_STARTUP_HT_START || + s->state == ENTROPY_SRC_STARTUP_PHASE1 || + s->state == ENTROPY_SRC_STARTUP_FAIL1) { + ot_entropy_src_change_state(s, ENTROPY_SRC_STARTUP_PASS1); + } else { + if (ot_fifo32_is_empty(&s->precon_fifo)) { + ot_entropy_src_perform_hash(s); + } } } } @@ -1102,13 +1441,36 @@ static uint32_t ot_entropy_src_get_entropy_data(OtEntropySrcState *s) REG_MB4_IS_TRUE(s, hi, CONF, V2_ENTROPY_DATA_REG_ENABLE) : REG_MB4_IS_TRUE(s, hi, CONF, V3_ENTROPY_DATA_REG_ENABLE); - if (ot_entropy_src_is_module_enabled(s) && is_entropy_data_enabled && - ot_entropy_src_is_fw_route(s)) { - if (!ot_fifo32_is_empty(&s->swread_fifo)) { - return ot_fifo32_pop(&s->swread_fifo); + if (ot_entropy_src_is_module_enabled(s) && is_entropy_data_enabled) { + bool done = (s->swread_idx == ES_WORD_COUNT - 1u); + if (done) { + s->swread_idx = 0u; + } else { + s->swread_idx += 1u; + } + if (ot_entropy_src_is_fw_route(s)) { + if (!ot_fifo32_is_empty(&s->swread_fifo)) { + uint32_t word = ot_fifo32_pop(&s->swread_fifo); + if (ot_fifo32_is_empty(&s->swread_fifo)) { + s->swread_idx = 0u; + ot_entropy_src_check_bus_cmp(s, s->swread_capt_data); + ot_entropy_src_update_fw_route(s); + } + return word; + } + if (done) { + s->regs_hi[R_ERR_CODE] |= R_ERR_CODE_SFIFO_ESFINAL_ERR_MASK | + R_ERR_CODE_FIFO_READ_ERR_MASK; + s->fifo_err_pulse = true; + ot_entropy_src_update_alerts(s); + ot_entropy_src_update_irqs(s); + } + qemu_log_mask(LOG_GUEST_ERROR, "%s: Entropy data not available\n", + __func__); + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: Entropy data not configured\n", + __func__); } - qemu_log_mask(LOG_GUEST_ERROR, "%s: Entropy data not available\n", - __func__); } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: Entropy data not configured\n", __func__); @@ -1168,8 +1530,9 @@ static void ot_entropy_src_noise_refill(void *opaque) } break; case ENTROPY_SRC_STARTUP_HT_START: - ot_entropy_src_change_state(s, ENTROPY_SRC_CONT_HT_RUNNING); - break; + case ENTROPY_SRC_STARTUP_PHASE1: + case ENTROPY_SRC_STARTUP_PASS1: + case ENTROPY_SRC_STARTUP_FAIL1: case ENTROPY_SRC_CONT_HT_RUNNING: case ENTROPY_SRC_CONT_HT_START: case ENTROPY_SRC_BOOT_PHASE_DONE: @@ -1177,6 +1540,11 @@ static void ot_entropy_src_noise_refill(void *opaque) case ENTROPY_SRC_SHA3_PROCESS: case ENTROPY_SRC_SHA3_DONE: case ENTROPY_SRC_SHA3_MSG_DONE: + case ENTROPY_SRC_FW_INSERT_START: + case ENTROPY_SRC_FW_INSERT_MSG: + case ENTROPY_SRC_ALERT_STATE: + case ENTROPY_SRC_ALERT_HANG: + case ENTROPY_SRC_IDLE: break; default: trace_ot_entropy_src_error(s->ot_id, "unexpected state", @@ -1195,24 +1563,29 @@ static void ot_entropy_src_scheduler(void *opaque) case ENTROPY_SRC_BOOT_HT_RUNNING: case ENTROPY_SRC_BOOT_PHASE_DONE: case ENTROPY_SRC_STARTUP_HT_START: + case ENTROPY_SRC_STARTUP_PHASE1: + case ENTROPY_SRC_STARTUP_PASS1: + case ENTROPY_SRC_STARTUP_FAIL1: case ENTROPY_SRC_CONT_HT_START: case ENTROPY_SRC_CONT_HT_RUNNING: case ENTROPY_SRC_SHA3_VALID: case ENTROPY_SRC_SHA3_PROCESS: case ENTROPY_SRC_SHA3_DONE: case ENTROPY_SRC_SHA3_MSG_DONE: + case ENTROPY_SRC_FW_INSERT_START: + case ENTROPY_SRC_FW_INSERT_MSG: ot_entropy_src_noise_refill(s); break; case ENTROPY_SRC_IDLE: + if (ot_entropy_src_is_module_enabled(s) && + ot_entropy_src_is_fw_ov_mode(s)) { + ot_entropy_src_noise_refill(s); + } break; - case ENTROPY_SRC_BOOT_POST_HT_CHK: - case ENTROPY_SRC_STARTUP_PHASE1: - case ENTROPY_SRC_STARTUP_PASS1: - case ENTROPY_SRC_STARTUP_FAIL1: - case ENTROPY_SRC_FW_INSERT_START: - case ENTROPY_SRC_FW_INSERT_MSG: case ENTROPY_SRC_ALERT_STATE: case ENTROPY_SRC_ALERT_HANG: + break; + case ENTROPY_SRC_BOOT_POST_HT_CHK: case ENTROPY_SRC_ERROR: default: qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid state: [%s:%d]\n", __func__, @@ -1333,6 +1706,7 @@ ot_entropy_src_hi_regs_read(void *opaque, hwaddr addr, unsigned size) case R_EXTHT_HI_TOTAL_FAILS: case R_EXTHT_LO_TOTAL_FAILS: case R_ALERT_THRESHOLD: + case R_ALERT_SUMMARY_FAIL_COUNTS: case R_ALERT_FAIL_COUNTS: case R_EXTHT_FAIL_COUNTS: case R_FW_OV_CONTROL: @@ -1344,15 +1718,18 @@ ot_entropy_src_hi_regs_read(void *opaque, hwaddr addr, unsigned size) case R_ERR_CODE_TEST: val32 = s->regs_hi[reg]; break; - case R_DEBUG_STATUS: + case R_DEBUG_STATUS: { /* SHA3 block reporting is not supported */ - val32 = FIELD_DP32(0, DEBUG_STATUS, ENTROPY_FIFO_DEPTH, - ot_fifo32_num_used(&s->final_fifo)); + unsigned seed_depth = + (ot_fifo32_num_used(&s->final_fifo) / ES_WORD_COUNT) + + (!ot_fifo32_is_empty(&s->swread_fifo) ? 1u : 0u); + seed_depth = MIN(seed_depth, ES_FINAL_FIFO_DEPTH); + val32 = FIELD_DP32(0, DEBUG_STATUS, ENTROPY_FIFO_DEPTH, seed_depth); val32 = FIELD_DP32(val32, DEBUG_STATUS, MAIN_SM_IDLE, (uint32_t)(s->state == ENTROPY_SRC_IDLE)); val32 = FIELD_DP32(val32, DEBUG_STATUS, MAIN_SM_BOOT_DONE, (uint32_t)(s->state == ENTROPY_SRC_BOOT_PHASE_DONE)); - break; + } break; case R_MAIN_SM_STATE: if (s->state < ARRAY_SIZE(OtEDNFsmStateCode)) { val32 = OtEDNFsmStateCode[s->state]; @@ -1360,31 +1737,37 @@ ot_entropy_src_hi_regs_read(void *opaque, hwaddr addr, unsigned size) val32 = OtEDNFsmStateCode[ENTROPY_SRC_ERROR]; } break; - case R_ALERT_SUMMARY_FAIL_COUNTS: - val32 = (uint32_t)ot_alert_get_alert_fail_count(s); - break; case R_ENTROPY_DATA: val32 = ot_entropy_src_get_entropy_data(s); break; case R_FW_OV_WR_FIFO_FULL: { - bool can_write; + bool is_full = false; if (ot_entropy_src_is_fw_ov_mode(s) && ot_entropy_src_is_fw_ov_entropy_insert(s)) { if (ot_entropy_src_is_bypass_mode(s)) { - can_write = ot_entropy_src_can_bypass_entropy(s); + is_full = !ot_entropy_src_can_bypass_entropy(s); } else { - can_write = ot_entropy_src_can_condition_entropy(s); + is_full = !ot_entropy_src_can_condition_entropy(s); } - } else { - can_write = false; } - val32 = can_write ? 0u : R_FW_OV_WR_FIFO_FULL_VAL_MASK; + val32 = is_full ? R_FW_OV_WR_FIFO_FULL_VAL_MASK : 0u; } break; case R_FW_OV_RD_DATA: if (ot_entropy_src_is_fw_ov_mode(s)) { if (!ot_fifo32_is_empty(&s->observe_fifo)) { val32 = ot_fifo32_pop(&s->observe_fifo); + if (ot_fifo32_is_empty(&s->observe_fifo)) { + s->obs_fifo_en = true; + s->regs_hi[R_FW_OV_RD_FIFO_OVERFLOW] &= + ~R_FW_OV_RD_FIFO_OVERFLOW_VAL_MASK; + ot_entropy_src_update_filler(s); + } } else { + s->regs_hi[R_ERR_CODE] |= R_ERR_CODE_SFIFO_OBSERVE_ERR_MASK | + R_ERR_CODE_FIFO_READ_ERR_MASK; + s->fifo_err_pulse = true; + ot_entropy_src_update_alerts(s); + ot_entropy_src_update_irqs(s); qemu_log_mask(LOG_GUEST_ERROR, "%s: Read from empty observe FIFO\n", __func__); val32 = 0; @@ -1459,13 +1842,19 @@ static void ot_entropy_src_lo_regs_write(void *opaque, hwaddr addr, case R_INTR_STATE: val32 &= INTR_WMASK; s->regs_lo[reg] &= ~val32; /* RW1C */ - /* not sure about this behavior, seems to be what the tests expect... */ - if (!ot_fifo32_is_empty(&s->swread_fifo)) { + if (!ot_fifo32_is_empty(&s->swread_fifo) && + ot_entropy_src_is_module_enabled(s) && + ot_entropy_src_is_fw_route(s) && + ot_entropy_src_is_entropy_data_enabled(s)) { s->regs_lo[R_INTR_STATE] |= INTR_ES_ENTROPY_VALID_MASK; } - if (!ot_fifo32_is_empty(&s->observe_fifo)) { + if (ot_entropy_src_is_fw_ov_mode(s) && + s->regs_hi[R_OBSERVE_FIFO_THRESH] != 0 && + ot_fifo32_num_used(&s->observe_fifo) >= + s->regs_hi[R_OBSERVE_FIFO_THRESH]) { s->regs_lo[R_INTR_STATE] |= INTR_ES_OBSERVE_FIFO_READY_MASK; } + ot_entropy_src_update_alerts(s); ot_entropy_src_update_irqs(s); break; case R_INTR_ENABLE: @@ -1490,6 +1879,7 @@ static void ot_entropy_src_lo_regs_write(void *opaque, hwaddr addr, case R_SW_REGUPD: val32 &= R_SW_REGUPD_VAL_MASK; s->regs_lo[reg] &= val32; /* RW0C */ + ot_entropy_src_update_regwen(s); break; case R_REGWEN: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: R/O register 0x%02x (%s)\n", @@ -1547,15 +1937,56 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, uint32_t old = s->regs_hi[reg]; val32 &= R_MODULE_ENABLE_MODULE_ENABLE_MASK; s->regs_hi[reg] = val32; + ot_entropy_src_update_regwen(s); CHECK_MULTIBOOT(s, hi, MODULE_ENABLE, MODULE_ENABLE); if (ot_entropy_src_is_module_disabled(s)) { - /* reset takes care of cancelling the scheduler timer */ - resettable_reset(OBJECT(s), RESET_TYPE_COLD); + ot_entropy_src_disable(s); break; } if ((old ^ s->regs_hi[reg]) && ot_entropy_src_is_module_enabled(s)) { - if (ot_entropy_src_is_fips_enabled(s)) { + /* health_test_clr on module enable pulse */ + s->ht_symbol_count = 0u; + s->last_ht_failed = false; + s->regs_hi[R_ALERT_SUMMARY_FAIL_COUNTS] = 0u; + s->regs_hi[R_ALERT_FAIL_COUNTS] = 0u; + s->regs_hi[R_EXTHT_FAIL_COUNTS] = 0u; + s->regs_hi[R_REPCNT_TOTAL_FAILS] = 0u; + s->regs_hi[R_REPCNTS_TOTAL_FAILS] = 0u; + s->regs_hi[R_ADAPTP_HI_TOTAL_FAILS] = 0u; + s->regs_hi[R_ADAPTP_LO_TOTAL_FAILS] = 0u; + s->regs_hi[R_BUCKET_TOTAL_FAILS] = 0u; + s->regs_hi[R_MARKOV_HI_TOTAL_FAILS] = 0u; + s->regs_hi[R_MARKOV_LO_TOTAL_FAILS] = 0u; + s->regs_hi[R_EXTHT_HI_TOTAL_FAILS] = 0u; + s->regs_hi[R_EXTHT_LO_TOTAL_FAILS] = 0u; + s->regs_hi[R_REPCNT_HI_WATERMARKS] = 0u; + s->regs_hi[R_REPCNTS_HI_WATERMARKS] = 0u; + s->regs_hi[R_ADAPTP_HI_WATERMARKS] = 0u; + s->regs_hi[R_ADAPTP_LO_WATERMARKS] = 0xffffffffu; + s->regs_hi[R_EXTHT_HI_WATERMARKS] = 0u; + s->regs_hi[R_EXTHT_LO_WATERMARKS] = 0xffffffffu; + s->regs_hi[R_BUCKET_HI_WATERMARKS] = 0u; + s->regs_hi[R_MARKOV_HI_WATERMARKS] = 0u; + s->regs_hi[R_MARKOV_LO_WATERMARKS] = 0xffffffffu; + + s->obs_fifo_en = ot_entropy_src_is_fw_ov_mode(s); + if (ot_entropy_src_is_fw_ov_mode(s) && + ot_entropy_src_is_fw_ov_entropy_insert(s)) { + if (ot_entropy_src_is_bypass_mode(s)) { + ot_entropy_src_change_state(s, ENTROPY_SRC_IDLE); + } else { + int res = sha3_384_init(&s->sha3_state); + g_assert(res == CRYPT_OK); + s->cond_word = 0; + ot_fifo32_reset(&s->precon_fifo); + ot_entropy_src_change_state( + s, REG_MB4_IS_TRUE(s, hi, FW_OV_SHA3_START, + FW_OV_INSERT_START) ? + ENTROPY_SRC_FW_INSERT_MSG : + ENTROPY_SRC_FW_INSERT_START); + } + } else if (ot_entropy_src_is_fips_enabled(s)) { /* start up phase */ ot_entropy_src_change_state(s, ENTROPY_SRC_STARTUP_HT_START); @@ -1568,6 +1999,7 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, (int64_t)(now + (uint64_t)OT_ENTROPY_SRC_BOOT_DELAY_NS)); } + ot_entropy_src_update_alerts(s); break; } qemu_log_mask(LOG_GUEST_ERROR, "%s: ME_REGWEN not enabled\n", __func__); @@ -1577,6 +2009,8 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, val32 &= (s->version < 3) ? CONF_V2_WMASK : CONF_V3_WMASK; s->regs_hi[reg] = val32; CHECK_MULTIBOOT(s, hi, CONF, FIPS_ENABLE); + CHECK_MULTIBOOT(s, hi, CONF, FIPS_FLAG); + CHECK_MULTIBOOT(s, hi, CONF, RNG_FIPS); CHECK_MULTIBOOT(s, hi, CONF, RNG_BIT_ENABLE); if (s->version < 3) { CHECK_MULTIBOOT_VER(s, hi, CONF, V2, ENTROPY_DATA_REG_ENABLE); @@ -1603,24 +2037,40 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, case R_REPCNT_THRESHOLDS: case R_REPCNTS_THRESHOLDS: case R_ADAPTP_HI_THRESHOLDS: - case R_ADAPTP_LO_THRESHOLDS: case R_BUCKET_THRESHOLDS: case R_MARKOV_HI_THRESHOLDS: - case R_MARKOV_LO_THRESHOLDS: case R_EXTHT_HI_THRESHOLDS: + if (s->regs_lo[R_REGWEN]) { + uint16_t cur_lo = (uint16_t)(s->regs_hi[reg] & 0xffffu); + uint16_t cur_hi = (uint16_t)(s->regs_hi[reg] >> 16u); + uint16_t wr_lo = (uint16_t)(val32 & 0xffffu); + uint16_t wr_hi = (uint16_t)(val32 >> 16u); + uint16_t new_lo = MIN(cur_lo, wr_lo); + uint16_t new_hi = MIN(cur_hi, wr_hi); + s->regs_hi[reg] = ((uint32_t)new_hi << 16u) | (uint32_t)new_lo; + ot_entropy_src_update_alerts(s); + } + break; + case R_ADAPTP_LO_THRESHOLDS: + case R_MARKOV_LO_THRESHOLDS: case R_EXTHT_LO_THRESHOLDS: if (s->regs_lo[R_REGWEN]) { - s->regs_hi[reg] = val32; + uint16_t cur_lo = (uint16_t)(s->regs_hi[reg] & 0xffffu); + uint16_t cur_hi = (uint16_t)(s->regs_hi[reg] >> 16u); + uint16_t wr_lo = (uint16_t)(val32 & 0xffffu); + uint16_t wr_hi = (uint16_t)(val32 >> 16u); + uint16_t new_lo = MAX(cur_lo, wr_lo); + uint16_t new_hi = MAX(cur_hi, wr_hi); + s->regs_hi[reg] = ((uint32_t)new_hi << 16u) | (uint32_t)new_lo; ot_entropy_src_update_alerts(s); } break; case R_ALERT_THRESHOLD: if (s->regs_lo[R_REGWEN]) { + s->regs_hi[reg] = val32; if ((uint16_t)(val32) != (uint16_t)(~(val32 >> 16u))) { - s->regs_hi[R_RECOV_ALERT_STS] |= - R_RECOV_ALERT_STS_ES_THRESH_CFG_ALERT_MASK; - } else { - s->regs_hi[reg] = val32; + ot_entropy_src_trigger_recov_alert( + s, R_RECOV_ALERT_STS_ES_THRESH_CFG_ALERT_MASK); } ot_entropy_src_update_alerts(s); } @@ -1634,46 +2084,27 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, s->obs_fifo_en = ot_entropy_src_is_fw_ov_mode(s); } break; - case R_FW_OV_SHA3_START: - if (!ot_entropy_src_is_module_enabled(s)) { - qemu_log_mask(LOG_GUEST_ERROR, "%s: module not enabled\n", - __func__); - break; - } + case R_FW_OV_SHA3_START: { val32 &= R_FW_OV_SHA3_START_FW_OV_INSERT_START_MASK; s->regs_hi[reg] = val32; CHECK_MULTIBOOT(s, hi, FW_OV_SHA3_START, FW_OV_INSERT_START); + if (!ot_entropy_src_is_module_enabled(s) || + !ot_entropy_src_is_fw_ov_mode(s) || + !ot_entropy_src_is_fw_ov_entropy_insert(s) || + ot_entropy_src_is_bypass_mode(s)) { + break; + } if (REG_MB4_IS_TRUE(s, hi, FW_OV_SHA3_START, FW_OV_INSERT_START)) { - OtEntropySrcFsmState new_state; - new_state = ot_entropy_src_is_bypass_mode(s) ? - ENTROPY_SRC_IDLE : - ENTROPY_SRC_FW_INSERT_START; - ot_entropy_src_change_state(s, new_state); - } else { /* default to false */ - if (s->state == ENTROPY_SRC_SHA3_PROCESS) { - /* handle SHA3 processing */ - if (ot_fifo32_is_empty(&s->precon_fifo)) { - ot_entropy_src_perform_hash(s); - if (ot_entropy_src_is_fw_route(s)) { - ot_entropy_src_update_fw_route(s); - } - } else { - qemu_log_mask(LOG_GUEST_ERROR, "%s: need 1 more word\n", - __func__); - } - } else { - OtEntropySrcFsmState new_state; - new_state = ot_entropy_src_is_bypass_mode(s) ? - ENTROPY_SRC_BOOT_HT_RUNNING : - ENTROPY_SRC_STARTUP_HT_START; - ot_entropy_src_change_state(s, new_state); + ot_entropy_src_change_state(s, ENTROPY_SRC_FW_INSERT_MSG); + } else if (s->state == ENTROPY_SRC_FW_INSERT_MSG) { + /* handle SHA3 processing */ + ot_entropy_src_perform_hash(s); + if (ot_entropy_src_is_fw_route(s)) { + ot_entropy_src_update_fw_route(s); } + ot_entropy_src_update_irqs(s); } - break; - case R_FW_OV_RD_FIFO_OVERFLOW: - val32 &= R_FW_OV_RD_FIFO_OVERFLOW_VAL_MASK; - s->regs_hi[reg] &= val32; /* RW0C */ - break; + } break; case R_FW_OV_WR_DATA: if (!ot_entropy_src_is_module_enabled(s)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: module not enabled\n", @@ -1686,12 +2117,17 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, if (ot_entropy_src_is_bypass_mode(s)) { can_write = ot_entropy_src_can_bypass_entropy(s); if (can_write) { - ot_entropy_src_push_bypass_entropy(s, val32); + if (ot_entropy_src_push_bypass_entropy(s, val32)) { + if (ot_entropy_src_is_fw_route(s)) { + ot_entropy_src_update_fw_route(s); + } + ot_entropy_src_update_irqs(s); + } } } else { can_write = ot_entropy_src_can_condition_entropy(s); if (can_write) { - ot_entropy_src_push_entropy_to_conditioner(s, val32); + (void)ot_entropy_src_push_entropy_to_conditioner(s, val32); } } if (!can_write) { @@ -1713,13 +2149,22 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, case R_RECOV_ALERT_STS: val32 &= RECOV_ALERT_STS_WMASK; s->regs_hi[reg] &= val32; /* RW0C */ + s->regs_hi[reg] |= ot_entropy_src_get_cfg_alerts(s); break; - case R_ERR_CODE_TEST: + case R_ERR_CODE_TEST: { val32 &= R_ERR_CODE_TEST_VAL_MASK; s->regs_hi[R_ERR_CODE_TEST] = val32; - ot_entropy_src_update_irqs(s); + uint32_t err_bit = (val32 < 31u) ? ((1u << val32) & ERR_CODE_MASK) : 0u; + s->regs_hi[R_ERR_CODE] |= err_bit; + if (err_bit & ERR_CODE_LOCAL_ESCALATE_MASK) { + ot_entropy_src_change_state(s, ENTROPY_SRC_ERROR); + } + if (err_bit) { + s->fifo_err_pulse = true; + } ot_entropy_src_update_alerts(s); - break; + ot_entropy_src_update_irqs(s); + } break; case R_ENTROPY_DATA: case R_REPCNT_HI_WATERMARKS: case R_REPCNTS_HI_WATERMARKS: @@ -1742,6 +2187,7 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, case R_ALERT_SUMMARY_FAIL_COUNTS: case R_ALERT_FAIL_COUNTS: case R_EXTHT_FAIL_COUNTS: + case R_FW_OV_RD_FIFO_OVERFLOW: case R_FW_OV_WR_FIFO_FULL: case R_FW_OV_RD_DATA: case R_OBSERVE_FIFO_DEPTH: @@ -1759,10 +2205,60 @@ static void ot_entropy_src_hi_regs_write(void *opaque, hwaddr addr, } }; +static bool ot_entropy_src_lo_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)size; + (void)attrs; + return !is_write || (addr & 3u) == 0u; +} + +static bool ot_entropy_src_rev_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)addr; + (void)attrs; + return !is_write || size == 4u; +} + +static bool ot_entropy_src_hi_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + uint8_t permit; + switch (R32_OFF(addr)) { + case R_MODULE_ENABLE: + case R_ENTROPY_CONTROL: + case R_EXTHT_FAIL_COUNTS: + case R_FW_OV_CONTROL ... R_FW_OV_RD_FIFO_OVERFLOW: + case R_OBSERVE_FIFO_THRESH: + case R_OBSERVE_FIFO_DEPTH: + case R_ERR_CODE_TEST: + permit = 0x1u; + break; + case R_ALERT_SUMMARY_FAIL_COUNTS: + case R_MAIN_SM_STATE: + permit = 0x3u; + break; + default: + permit = 0xfu; + break; + } + uint8_t mask = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return !(permit & ~mask); +} + static const MemoryRegionOps ot_entropy_src_lo_ops = { .read = &ot_entropy_src_lo_regs_read, .write = &ot_entropy_src_lo_regs_write, .endianness = DEVICE_LITTLE_ENDIAN, + .valid.accepts = &ot_entropy_src_lo_accepts, .impl = { .min_access_size = 4u, .max_access_size = 4u, @@ -1773,6 +2269,7 @@ static const MemoryRegionOps ot_entropy_src_rev_ops = { .read = &ot_entropy_src_rev_regs_read, .write = &ot_entropy_src_rev_regs_write, .endianness = DEVICE_LITTLE_ENDIAN, + .valid.accepts = &ot_entropy_src_rev_accepts, .impl = { .min_access_size = 4u, .max_access_size = 4u, @@ -1783,6 +2280,7 @@ static const MemoryRegionOps ot_entropy_src_hi_ops = { .read = &ot_entropy_src_hi_regs_read, .write = &ot_entropy_src_hi_regs_write, .endianness = DEVICE_LITTLE_ENDIAN, + .valid.accepts = &ot_entropy_src_hi_accepts, .impl = { .min_access_size = 4u, .max_access_size = 4u, @@ -1849,9 +2347,16 @@ static void ot_entropy_src_reset_enter(Object *obj, ResetType type) ot_fifo32_reset(&s->swread_fifo); ot_fifo32_reset(&s->final_fifo); + s->rdata_capt = 0u; + s->swread_capt_data = 0u; + s->swread_idx = 0u; + s->rdata_capt_vld = false; + s->fifo_err_pulse = false; s->cond_word = 0u; s->noise_count = 0u; s->packet_count = 0u; + s->ht_symbol_count = 0u; + s->last_ht_failed = false; s->obs_fifo_en = false; ot_entropy_src_update_irqs(s); @@ -1859,6 +2364,7 @@ static void ot_entropy_src_reset_enter(Object *obj, ResetType type) ibex_irq_set(&s->alerts[ix], 0); } + s->state = ENTROPY_SRC_IDLE; ot_entropy_src_change_state(s, ENTROPY_SRC_IDLE); } From d2c9257b79e02dabaecebb1e1cc2d926cd644ea7 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:34 +0000 Subject: [PATCH 24/52] [ot] hw/opentitan: csrng: align alerts, state dump, and ERR_CODE_TEST Align the OpenTitan CSRNG model (`hw/opentitan/ot_csrng.c`) with Earlgrey RTL (`csrng_core.sv`, `csrng_main_sm.sv`, and `csrng_ctr_drbg_cmd.sv`): - Implement continuous 128-bit block repetition check (`CMD_GEN_CNT_ERR`) and `flag0` (`no_glast_update`) state update semantics. - Accept `UNINSTANTIATE` commands on already-uninstantiated application instances (`csrng_main_sm.sv` / `csrng_ctr_drbg_cmd.sv`), only removing an instance from `cmd_requests` when `ot_csrng_is_in_queue(inst)` is true. - Align `INT_STATE_NUM`, `INT_STATE_READ_ENABLE`, and `INT_STATE_VAL` state dump indexing (`state_db_ix`, `state_db_dump_id`) and `GENBITS` read gating when `sw_app_enable` is disabled. - Continuously latch invalid `CTRL` MuBi4 field alerts in `RECOV_ALERT_STS`, pulse `HW_EXC_STS` on command stage errors, and transition to `CSRNG_ERROR` (`MAIN_SM_ERR`) on FSM bits in `ERR_CODE_TEST`. - Add `ot_csrng_regs_accepts` and expand the MMIO region to `0x80` bytes so unaligned/sub-word accesses and unmapped offset `0x7c` fault. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ib39f93ca66ba3b97fec22854d2e22e1b6a6a6964 --- hw/opentitan/ot_csrng.c | 562 ++++++++++++++++++++++++++++------------ 1 file changed, 395 insertions(+), 167 deletions(-) diff --git a/hw/opentitan/ot_csrng.c b/hw/opentitan/ot_csrng.c index 5f7ab6b236de4..7933ea15f1562 100644 --- a/hw/opentitan/ot_csrng.c +++ b/hw/opentitan/ot_csrng.c @@ -167,7 +167,7 @@ REG32(MAIN_SM_STATE, 0x5cu) R_RECOV_ALERT_STS_CMD_STAGE_INVALID_ACMD_ALERT_MASK | \ R_RECOV_ALERT_STS_CMD_STAGE_INVALID_CMD_SEQ_ALERT_MASK | \ R_RECOV_ALERT_STS_CMD_STAGE_INVALID_RESEED_CNT_ALERT_MASK) -#define ERR_CODE_MASK 0x77e0ffffu +#define ERR_CODE_MASK 0x77f0ffffu #define OT_CSRNG_AES_KEY_SIZE 32u /* 256 bits */ #define OT_CSRNG_AES_BLOCK_SIZE 16u /* 128 bits */ @@ -306,6 +306,9 @@ typedef struct { bool seeded; /* ready to generate randomness */ bool no_fips; bool force_fips; + bool no_glast_update; + uint32_t prev_genbits[OT_CSRNG_PACKET_WORD_COUNT]; + bool prev_genbits_valid; } OtCSRNGDrng; typedef struct OtCSRNGInstance { @@ -325,6 +328,7 @@ typedef struct OtCSRNGInstance { }; OtCSRNGDrng drng; bool defer_completion; + bool in_filler; QSIMPLEQ_ENTRY(OtCSRNGInstance) cmd_request; OtCSRNGState *parent; } OtCSRNGInstance; @@ -344,10 +348,13 @@ struct OtCSRNGState { bool enabled; bool sw_app_granted; bool read_int_granted; + bool in_scheduler; + bool waiting_for_entropy; uint32_t scheduled_cmd; unsigned entropy_delay; unsigned es_retry_count; unsigned state_db_ix; + unsigned state_db_dump_id; int aes_cipher; /* AES handle for tomcrypt */ OtCSRNGFsmState state; OtCSRNGInstance *instances; @@ -410,6 +417,8 @@ static const char *STATE_NAMES[] = { static bool ot_csrng_check_multibitboot(OtCSRNGState *s, uint8_t mbbool, uint32_t alert_bit); static void ot_csrng_command_schedule(OtCSRNGState *s, OtCSRNGInstance *inst); +static void ot_csrng_command_scheduler(void *opaque); +static void ot_csrng_hwapp_filler_bh(void *opaque); static bool ot_csrng_instance_is_command_ready(const OtCSRNGInstance *inst, bool fatal); static void ot_csrng_complete_command(OtCSRNGInstance *inst, @@ -420,6 +429,7 @@ static unsigned ot_csrng_get_slot(const OtCSRNGInstance *inst); static bool ot_csrng_drng_is_instantiated(const OtCSRNGInstance *inst); static void ot_csrng_release_hw_app(OtCSRNGInstance *inst); static void ot_csrng_update_irqs(OtCSRNGState *s); +static void ot_csrng_trigger_recov_alert(OtCSRNGState *s, uint32_t sts_mask); static void ot_csrng_update_alerts(OtCSRNGState *s); static OtCSRNDCmdResult ot_csrng_drng_reseed(OtCSRNGInstance *inst, bool flag0); @@ -468,6 +478,8 @@ ot_csrng_connect_hw_app(OtCSRNGState *s, unsigned app_id, qemu_irq req_sts, (int)app_id); } +static bool ot_csrng_is_in_queue(OtCSRNGInstance *inst); + static OtCSRNGCmdStatus ot_csrng_push_command(OtCSRNGState *s, unsigned app_id, uint32_t word) { @@ -482,7 +494,7 @@ ot_csrng_push_command(OtCSRNGState *s, unsigned app_id, uint32_t word) g_assert(inst->hw.req_sts); /* FIFO is emptied in #ot_csrng_complete_command */ - if (ot_fifo32_is_full(&inst->cmd_fifo)) { + if (ot_fifo32_is_full(&inst->cmd_fifo) || ot_csrng_is_in_queue(inst)) { xtrace_ot_csrng_error("Command FIFO is full"); return CSRNG_STATUS_INVALID_CMD_SEQ; } @@ -509,6 +521,17 @@ ot_csrng_push_command(OtCSRNGState *s, unsigned app_id, uint32_t word) break; default: xtrace_ot_csrng_error("Invalid command opcode"); + /* + * csrng_core.sv:1048,1728-1729: hw_exception_sts[app_id] pulses for + * 1 cycle (latching INTR_CS_HW_INST_EXC), then hw2reg.hw_exc_sts.de + * (= cs_enable_fo[50]) immediately writes hw_exception_sts (0) on + * the next cycle. + */ + s->regs[R_HW_EXC_STS] = 0u; + s->regs[R_INTR_STATE] |= INTR_CS_HW_INST_EXC_MASK; + ot_csrng_update_irqs(s); + ot_csrng_trigger_recov_alert( + s, R_RECOV_ALERT_STS_CMD_STAGE_INVALID_ACMD_ALERT_MASK); ot_fifo32_reset(&inst->cmd_fifo); return CSRNG_STATUS_INVALID_ACMD; } @@ -542,12 +565,12 @@ ot_csrng_push_command(OtCSRNGState *s, unsigned app_id, uint32_t word) const OtCSRNGDrng *drng = &inst->drng; if (drng->reseed_counter >= s->regs[R_RESEED_INTERVAL]) { + s->regs[R_HW_EXC_STS] = 0u; s->regs[R_INTR_STATE] |= INTR_CS_HW_INST_EXC_MASK; - s->regs[R_RECOV_ALERT_STS] |= - R_RECOV_ALERT_STS_CMD_STAGE_INVALID_RESEED_CNT_ALERT_MASK; ot_fifo32_reset(&inst->cmd_fifo); ot_csrng_update_irqs(s); - ot_csrng_update_alerts(s); + ot_csrng_trigger_recov_alert( + s, R_RECOV_ALERT_STS_CMD_STAGE_INVALID_RESEED_CNT_ALERT_MASK); return CSRNG_STATUS_RESEED_CNT_EXCEEDED; } } else { @@ -555,7 +578,14 @@ ot_csrng_push_command(OtCSRNGState *s, unsigned app_id, uint32_t word) } if (acmd == OT_CSRNG_CMD_UNINSTANTIATE) { - if (!ot_csrng_drng_is_instantiated(inst)) { + /* + * In hw/ip/csrng/rtl/csrng_main_sm.sv and csrng_ctr_drbg_cmd.sv, + * UNINSTANTIATE is accepted even when the instance is already + * uninstantiated. Only remove inst from s->cmd_requests if an earlier + * command is actually queued (ot_csrng_is_in_queue(inst)). + */ + if (!ot_csrng_drng_is_instantiated(inst) && + ot_csrng_is_in_queue(inst)) { /* * Very special hacky case: * @@ -709,10 +739,13 @@ static void ot_csrng_drng_uninstantiate(OtCSRNGInstance *inst) drng->instantiated = false; drng->seeded = false; - drng->no_fips = false; + drng->no_fips = true; + drng->force_fips = false; + drng->reseed_counter = 0; drng->rem_packet_count = 0; /* only to help debugging */ + memset(drng->key, 0, sizeof(drng->key)); memset(drng->v_counter, 0, sizeof(drng->v_counter)); } @@ -786,6 +819,8 @@ static OtCSRNDCmdResult ot_csrng_drng_reseed(OtCSRNGInstance *inst, bool flag0) trace_ot_csrng_request_entropy(slot); OtEntropySrcState *ess = inst->parent->entropy_src; OtEntropySrcClass *esc = OT_ENTROPY_SRC_GET_CLASS(ess); + s->regs[R_INTR_STATE] |= INTR_CS_ENTROPY_REQ_MASK; + ot_csrng_update_irqs(s); res = esc->get_entropy(ess, entropy, &fips); if (res < 0) { @@ -795,11 +830,14 @@ static OtCSRNDCmdResult ot_csrng_drng_reseed(OtCSRNGInstance *inst, bool flag0) } if (res > 0) { - s->entropy_delay = (res > 1) ? (unsigned)res : 0; + s->entropy_delay = (unsigned)res; + s->waiting_for_entropy = true; trace_ot_csrng_entropy_rejected(slot, "not ready", res); return CSRNG_CMD_RETRY; } + s->waiting_for_entropy = false; + /* always perform XOR which is a no-op if material_len is zero */ for (unsigned ix = 0; ix < OT_ENTROPY_SRC_DWORD_COUNT; ix++) { buffer[ix] ^= entropy[ix]; @@ -815,7 +853,13 @@ static OtCSRNDCmdResult ot_csrng_drng_reseed(OtCSRNGInstance *inst, bool flag0) drng->reseed_counter = 0u; drng->seeded = true; - drng->force_fips = (bool)((s->regs[R_FIPS_FORCE] >> slot) & 0x1u); + bool fips_force_en = FIELD_EX32(s->regs[R_CTRL], CTRL, FIPS_FORCE_ENABLE) == + OT_MULTIBITBOOL4_TRUE; + drng->force_fips = + fips_force_en && (bool)((s->regs[R_FIPS_FORCE] >> slot) & 0x1u); + if (drng->force_fips) { + drng->no_fips = false; + } return CSRNG_CMD_OK; } @@ -834,15 +878,28 @@ static void ot_csrng_drng_generate(OtCSRNGInstance *inst, uint32_t *out, &drng->ecb); g_assert(res == CRYPT_OK); + if (ot_csrng_get_slot(inst) == SW_INSTANCE_ID && drng->prev_genbits_valid && + memcmp(drng->prev_genbits, out, sizeof(drng->prev_genbits)) == 0) { + OtCSRNGState *s = inst->parent; + s->regs[R_RECOV_ALERT_STS] |= R_RECOV_ALERT_STS_CS_BUS_CMP_ALERT_MASK; + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 1); + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 0); + } + memcpy(drng->prev_genbits, out, sizeof(drng->prev_genbits)); + drng->prev_genbits_valid = true; + xtrace_ot_csrng_show_buffer(ot_csrng_get_slot(inst), "out", out, OT_CSRNG_AES_BLOCK_SIZE); *fips = (!drng->no_fips) || drng->force_fips; if (!ot_csrng_drng_remaining_count(inst)) { - ot_csrng_drng_update(inst); - /* last packet generation for the current command */ - drng->reseed_counter += 1u; + if (!drng->no_glast_update) { + ot_csrng_drng_update(inst); + /* last packet generation for the current command */ + drng->reseed_counter += 1u; + } + drng->no_glast_update = false; } } @@ -866,22 +923,29 @@ static void ot_csrng_update_irqs(OtCSRNGState *s) } } +static void ot_csrng_trigger_recov_alert(OtCSRNGState *s, uint32_t sts_mask) +{ + s->regs[R_RECOV_ALERT_STS] |= sts_mask; + if (sts_mask & ~R_RECOV_ALERT_STS_FIPS_FORCE_ENABLE_FIELD_ALERT_MASK) { + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 1); + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 0); + } +} + static void ot_csrng_update_alerts(OtCSRNGState *s) { uint32_t level = s->regs[R_ALERT_TEST]; s->regs[R_ALERT_TEST] = 0u; - if (s->regs[R_RECOV_ALERT_STS]) { - level |= 1u << ALERT_RECOVERABLE; - } - if (s->state == CSRNG_ERROR) { level |= 1u << ALERT_FATAL; } for (unsigned ix = 0; ix < PARAM_NUM_ALERTS; ix++) { - ibex_irq_set(&s->alerts[ix], (int)((level >> ix) & 0x1u)); - ibex_irq_set(&s->alerts[ix], 0); + if ((level >> ix) & 0x1u) { + ibex_irq_set(&s->alerts[ix], 1); + ibex_irq_set(&s->alerts[ix], 0); + } } } @@ -915,8 +979,7 @@ ot_csrng_check_multibitboot(OtCSRNGState *s, uint8_t mbbool, uint32_t alert_bit) qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid multiboot4: 0x%01x\n", __func__, (unsigned)mbbool); - s->regs[R_RECOV_ALERT_STS] |= 1u << alert_bit; - ot_csrng_update_alerts(s); + ot_csrng_trigger_recov_alert(s, alert_bit); /* for CSRNG, default to false for invalid multibit boolean */ return false; @@ -929,7 +992,7 @@ static bool ot_csrng_is_ctrl_enabled(OtCSRNGState *s) static bool ot_csrng_is_ctrl_disabled(OtCSRNGState *s) { - return FIELD_EX32(s->regs[R_CTRL], CTRL, ENABLE) == OT_MULTIBITBOOL4_FALSE; + return !ot_csrng_is_ctrl_enabled(s); } static bool ot_csrng_is_sw_app_enabled(OtCSRNGState *s) @@ -996,7 +1059,12 @@ static void ot_csrng_handle_enable(OtCSRNGState *s) } } s->enabled = false; - s->regs[R_SW_CMD_STS] &= ~R_SW_CMD_STS_CMD_RDY_MASK; + s->waiting_for_entropy = false; + /* + * csrng_core.sv:930-946: !cs_enable_fo[28] clears cmd_rdy and cmd_ack, + * while cmd_sts.de is gated only by cmd_stage_ack[NApps-1]. + */ + s->regs[R_SW_CMD_STS] &= R_SW_CMD_STS_CMD_STS_MASK; s->es_retry_count = 0; /* cancel any outstanding asynchronous request */ @@ -1034,6 +1102,7 @@ static void ot_csrng_handle_enable(OtCSRNGState *s) for (unsigned ix = 0u; ix < N_APP_COUNT; ix++) { inst = &s->instances[ix]; ot_csrng_drng_uninstantiate(inst); + inst->drng.prev_genbits_valid = false; ot_fifo32_reset(&inst->cmd_fifo); if (ix == SW_INSTANCE_ID) { ot_fifo32_reset(&inst->sw.bits_fifo); @@ -1043,22 +1112,55 @@ static void ot_csrng_handle_enable(OtCSRNGState *s) } } - CHANGE_STATE(s, CSRNG_IDLE); + /* csrng_state_db.sv:163, 172: !state_db_enable_i resets ptr & dump_id + */ + s->state_db_ix = 0xfu; + s->state_db_dump_id = 0u; + + if (s->state != CSRNG_ERROR) { + CHANGE_STATE(s, CSRNG_IDLE); + } xtrace_ot_csrng_info("CSRNG disabled", 0); } } -static void ot_csrng_complete_sw_command(OtCSRNGInstance *inst, bool res) +static uint32_t ot_csrng_get_ctrl_recov_alert_sts(const OtCSRNGState *s) { - OtCSRNGState *s = inst->parent; + uint32_t ctrl = s->regs[R_CTRL]; + uint32_t sts = 0u; - if (res == CSRNG_CMD_OK) { - s->regs[R_SW_CMD_STS] &= R_SW_CMD_STS_CMD_STS_MASK; - } else { - s->regs[R_SW_CMD_STS] |= R_SW_CMD_STS_CMD_STS_MASK; + uint8_t en = FIELD_EX32(ctrl, CTRL, ENABLE); + if (en != OT_MULTIBITBOOL4_TRUE && en != OT_MULTIBITBOOL4_FALSE) { + sts |= ALERT_STATUS_BIT(ENABLE); + } + uint8_t sw_app = FIELD_EX32(ctrl, CTRL, SW_APP_ENABLE); + if (sw_app != OT_MULTIBITBOOL4_TRUE && sw_app != OT_MULTIBITBOOL4_FALSE) { + sts |= ALERT_STATUS_BIT(SW_APP_ENABLE); } - s->regs[R_SW_CMD_STS] |= R_SW_CMD_STS_CMD_RDY_MASK; + uint8_t read_int = FIELD_EX32(ctrl, CTRL, READ_INT_STATE); + if (read_int != OT_MULTIBITBOOL4_TRUE && + read_int != OT_MULTIBITBOOL4_FALSE) { + sts |= ALERT_STATUS_BIT(READ_INT_STATE); + } + uint8_t fips_force = FIELD_EX32(ctrl, CTRL, FIPS_FORCE_ENABLE); + if (fips_force != OT_MULTIBITBOOL4_TRUE && + fips_force != OT_MULTIBITBOOL4_FALSE) { + sts |= ALERT_STATUS_BIT(FIPS_FORCE_ENABLE); + } + return sts; +} + +static void +ot_csrng_complete_sw_command(OtCSRNGInstance *inst, OtCSRNGCmdStatus sts) +{ + OtCSRNGState *s = inst->parent; + + uint32_t reg = s->regs[R_SW_CMD_STS]; + reg = FIELD_DP32(reg, SW_CMD_STS, CMD_RDY, 1u); + reg = FIELD_DP32(reg, SW_CMD_STS, CMD_ACK, 1u); + reg = FIELD_DP32(reg, SW_CMD_STS, CMD_STS, (uint32_t)sts); + s->regs[R_SW_CMD_STS] = reg; s->regs[R_INTR_STATE] |= INTR_CS_CMD_REQ_DONE_MASK; ot_csrng_update_irqs(s); @@ -1067,6 +1169,12 @@ static void ot_csrng_complete_sw_command(OtCSRNGInstance *inst, bool res) static void ot_csrng_complete_hw_command(OtCSRNGInstance *inst, OtCSRNGCmdStatus sts) { + if (sts != CSRNG_STATUS_SUCCESS) { + OtCSRNGState *s = inst->parent; + s->regs[R_HW_EXC_STS] = 0u; + s->regs[R_INTR_STATE] |= INTR_CS_HW_INST_EXC_MASK; + ot_csrng_update_irqs(s); + } qemu_set_irq(inst->hw.req_sts, (int)sts); } @@ -1094,6 +1202,8 @@ static void ot_csrng_complete_command(OtCSRNGInstance *inst, trace_ot_csrng_show_command("complete", slot, CMD_NAME(acmd), acmd); + CHANGE_STATE(s, CSRNG_IDLE); + if (slot == SW_INSTANCE_ID) { trace_ot_csrng_complete_command(slot, "sw", CMD_NAME(acmd), acmd, sts); ot_csrng_complete_sw_command(inst, sts); @@ -1101,8 +1211,6 @@ static void ot_csrng_complete_command(OtCSRNGInstance *inst, trace_ot_csrng_complete_command(slot, "hw", CMD_NAME(acmd), acmd, sts); ot_csrng_complete_hw_command(inst, sts); } - - CHANGE_STATE(s, CSRNG_IDLE); } static OtCSRNDCmdResult @@ -1115,7 +1223,10 @@ ot_csrng_handle_instantiate(OtCSRNGState *s, unsigned slot) uint32_t command = ot_fifo32_peek(&inst->cmd_fifo); uint32_t clen = FIELD_EX32(command, OT_CSNRG_CMD, CLEN); bool flag0 = - FIELD_EX32(command, OT_CSNRG_CMD, FLAG0) == OT_MULTIBITBOOL4_TRUE; + ot_csrng_check_multibitboot(s, + (uint8_t)FIELD_EX32(command, OT_CSNRG_CMD, + FLAG0), + ALERT_STATUS_BIT(ACMD_FLAG0)); uint32_t num; const uint32_t *buffer = @@ -1164,11 +1275,19 @@ static OtCSRNDCmdResult ot_csrng_handle_generate(OtCSRNGState *s, unsigned slot) uint32_t command = ot_fifo32_peek(&inst->cmd_fifo); uint32_t packet_count = FIELD_EX32(command, OT_CSNRG_CMD, GLEN); - if (!packet_count) { + /* + * csrng_cmd_stage.sv:378-431 & prim_count.sv:114-121: + * When GLEN == 0, SendSOP still issues 1 generate request to csrng_core + * with cmd_gen_cnt_last = 0 (glast = 0), while u_prim_count_cmd_gen_cntr + * saturates at 0 without error. csrng_ctr_drbg_gen.sv:575-598 generates + * 1 block into sfifo_genbits without post-generate update or reseed_counter + * increment, and GenReq completes with CMD_STS_SUCCESS once sfifo_genbits + * is drained. + */ + bool no_glast = (packet_count == 0u); + if (no_glast) { xtrace_ot_csrng_error("generation for no packet"); - CHANGE_STATE(s, CSRNG_ERROR); - ot_csrng_update_alerts(s); - return CSRNG_CMD_INVALID_GEN_CMD; + packet_count = 1u; } uint32_t clen = FIELD_EX32(command, OT_CSNRG_CMD, CLEN); @@ -1185,6 +1304,24 @@ static OtCSRNDCmdResult ot_csrng_handle_generate(OtCSRNGState *s, unsigned slot) xtrace_ot_csrng_show_buffer(ot_csrng_get_slot(inst), "mat", buffer, clen * sizeof(uint32_t)); ot_csrng_drng_store_material(inst, buffer, clen); + bool adata_non_zero = false; + for (uint32_t ix = 0; ix < clen; ix++) { + if (buffer[ix] != 0u) { + adata_non_zero = true; + break; + } + } + if (adata_non_zero) { + /* + * Per NIST SP 800-90A Section 10.2.1.5.1 and csrng_ctr_drbg_cmd.sv + * (!prep_gen_adata_null), non-zero additional data triggers an + * initial CTR_DRBG_Update(adata, Key, V) prior to block generation, + * and the same adata is also retained in update_adata_q for the + * final CTR_DRBG_Update on the last generated block (glast). + */ + ot_csrng_drng_update(inst); + ot_csrng_drng_store_material(inst, buffer, clen); + } } trace_ot_csrng_generate(ot_csrng_get_slot(inst), packet_count); @@ -1200,6 +1337,7 @@ static OtCSRNDCmdResult ot_csrng_handle_generate(OtCSRNGState *s, unsigned slot) } inst->drng.rem_packet_count = packet_count; + inst->drng.no_glast_update = no_glast; /* * do not ack command yet, @@ -1214,7 +1352,10 @@ static OtCSRNDCmdResult ot_csrng_handle_reseed(OtCSRNGState *s, unsigned slot) uint32_t command = ot_fifo32_peek(&inst->cmd_fifo); bool flag0 = - FIELD_EX32(command, OT_CSNRG_CMD, FLAG0) == OT_MULTIBITBOOL4_TRUE; + ot_csrng_check_multibitboot(s, + (uint8_t)FIELD_EX32(command, OT_CSNRG_CMD, + FLAG0), + ALERT_STATUS_BIT(ACMD_FLAG0)); xtrace_ot_csrng_info("reseed", flag0); @@ -1299,7 +1440,7 @@ static void ot_csrng_hwapp_ready_irq(void *opaque, int n, int level) */ trace_ot_csrng_defer_generation(slot); } else { - qemu_bh_schedule(inst->hw.filler_bh); + ot_csrng_hwapp_filler_bh(inst); } } } @@ -1309,31 +1450,35 @@ static void ot_csrng_hwapp_filler_bh(void *opaque) /* scheduled following a CSRNG HW APP client ready to receive entropy */ OtCSRNGInstance *inst = opaque; + if (inst->in_filler) { + return; + } + inst->in_filler = true; + /* * client may have updated its readiness status since this BH has been * scheduled, readiness should always be tested */ - if (inst->hw.genbits_ready && ot_csrng_drng_remaining_count(inst)) { + while (inst->hw.genbits_ready && ot_csrng_drng_remaining_count(inst)) { uint32_t bits[OT_CSRNG_PACKET_WORD_COUNT]; bool fips; ot_csrng_drng_generate(inst, bits, &fips); + uint32_t swapped[OT_CSRNG_PACKET_WORD_COUNT]; + for (unsigned ix = 0; ix < OT_CSRNG_PACKET_WORD_COUNT; ix++) { + swapped[ix] = bswap32(bits[OT_CSRNG_PACKET_WORD_COUNT - ix - 1u]); + } + /* * client callback may trigger ot_csrng_hwapp_ready_irq to update its * readiness status, so client state past this point may have been * updated */ - inst->hw.filler(inst->hw.opaque, bits, fips); - - /* - * reschedule self if the client does not update its readiness, it - * expects more entropy from this instance. - */ - if (ot_csrng_drng_remaining_count(inst)) { - qemu_bh_schedule(inst->hw.filler_bh); - } + inst->hw.filler(inst->hw.opaque, swapped, fips); } + inst->in_filler = false; + /* check if the instance is running a deferred completion command */ if (inst->defer_completion) { /* @@ -1424,10 +1569,8 @@ static OtCSRNDCmdResult ot_csrng_handle_command(OtCSRNGState *s, unsigned slot) default: qemu_log_mask(LOG_GUEST_ERROR, "Unknown command: %u\n", acmd); // JW: check this shouldn't be CMD_STAGE_INVALID_CMD_SEQ_ALERT. - s->regs[R_RECOV_ALERT_STS] |= - R_RECOV_ALERT_STS_CMD_STAGE_INVALID_ACMD_ALERT_MASK; - CHANGE_STATE(s, CSRNG_ERROR); - ot_csrng_update_alerts(s); + ot_csrng_trigger_recov_alert( + s, R_RECOV_ALERT_STS_CMD_STAGE_INVALID_ACMD_ALERT_MASK); return CSRNG_CMD_INVALID_ACMD; } @@ -1441,6 +1584,8 @@ static OtCSRNDCmdResult ot_csrng_handle_command(OtCSRNGState *s, unsigned slot) if (ot_csrng_drng_is_instantiated(inst)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: instance %u already active\n", __func__, slot); + ot_csrng_trigger_recov_alert( + s, R_RECOV_ALERT_STS_CMD_STAGE_INVALID_CMD_SEQ_ALERT_MASK); return CSRNG_CMD_INVALID_CMD_SEQ; } break; @@ -1450,10 +1595,17 @@ static OtCSRNDCmdResult ot_csrng_handle_command(OtCSRNGState *s, unsigned slot) if (!ot_csrng_drng_is_instantiated(inst)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: instance %u not instantiated\n", __func__, slot); - CHANGE_STATE(s, CSRNG_ERROR); - ot_csrng_update_alerts(s); + ot_csrng_trigger_recov_alert( + s, R_RECOV_ALERT_STS_CMD_STAGE_INVALID_CMD_SEQ_ALERT_MASK); return CSRNG_CMD_INVALID_CMD_SEQ; } + if (acmd == OT_CSRNG_CMD_GENERATE && + inst->drng.reseed_counter >= s->regs[R_RESEED_INTERVAL]) { + ot_csrng_trigger_recov_alert( + s, R_RECOV_ALERT_STS_CMD_STAGE_INVALID_RESEED_CNT_ALERT_MASK); + return CSRNG_CMD_RESEED_CNT_EXCEEDED; + } + break; } OtCSRNDCmdResult res; @@ -1470,21 +1622,6 @@ static OtCSRNDCmdResult ot_csrng_handle_command(OtCSRNGState *s, unsigned slot) case OT_CSRNG_CMD_GENERATE: CHANGE_STATE(s, CSRNG_GENERATE_REQ); res = ot_csrng_handle_generate(s, slot); - if (res == CSRNG_CMD_DEFERRED) { - if (ot_csrng_drng_remaining_count(inst)) { - if (slot != SW_INSTANCE_ID) { - /* HW instance */ - if (inst->hw.genbits_ready) { - qemu_bh_schedule(inst->hw.filler_bh); - } else { - xtrace_ot_csrng_info("genbit not ready", slot); - } - } else { - /* SW instance */ - ot_csrng_swapp_fill(inst); - } - } - } break; case OT_CSRNG_CMD_UPDATE: CHANGE_STATE(s, CSRNG_UPDATE_REQ); @@ -1519,23 +1656,30 @@ static void ot_csrng_command_schedule(OtCSRNGState *s, OtCSRNGInstance *inst) trace_ot_csrng_schedule(ot_csrng_get_slot(inst), "command"); timer_del(s->entropy_scheduler); - qemu_bh_schedule(s->cmd_scheduler); + if (!s->in_scheduler && s->state == CSRNG_IDLE) { + ot_csrng_command_scheduler(s); + } else if (!s->in_scheduler) { + qemu_bh_schedule(s->cmd_scheduler); + } } static void ot_csrng_command_scheduler(void *opaque) { OtCSRNGState *s = opaque; - /* handle a single instance per cycle */ - OtCSRNGInstance *inst = QSIMPLEQ_FIRST(&s->cmd_requests); - if (!inst) { - xtrace_ot_csrng_error("scheduled request w/o request?"); - g_assert_not_reached(); + if (s->in_scheduler) { + return; } + s->in_scheduler = true; - if (s->state != CSRNG_IDLE) { - trace_ot_csrng_invalid_state(__func__, STATE_NAME(s->state), s->state); - g_assert_not_reached(); + OtCSRNGInstance *inst; + +next: + /* handle a single instance per cycle */ + inst = QSIMPLEQ_FIRST(&s->cmd_requests); + if (!inst || s->state != CSRNG_IDLE) { + s->in_scheduler = false; + return; } uint32_t command = ot_fifo32_peek(&inst->cmd_fifo); @@ -1572,10 +1716,27 @@ static void ot_csrng_command_scheduler(void *opaque) /* command re-insertion */ trace_ot_csrng_show_command("push back", slot, CMD_NAME(acmd), acmd); QSIMPLEQ_INSERT_TAIL(&s->cmd_requests, inst, cmd_request); - break; + timer_mod(s->entropy_scheduler, qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + + (int64_t)s->entropy_delay); + s->entropy_delay = 0; + s->in_scheduler = false; + return; case CSRNG_CMD_DEFERRED: inst->defer_completion = true; CHANGE_STATE(s, CSRNG_IDLE); + if (ot_csrng_drng_remaining_count(inst)) { + if (slot != SW_INSTANCE_ID) { + /* HW instance */ + if (inst->hw.genbits_ready) { + ot_csrng_hwapp_filler_bh(inst); + } else { + xtrace_ot_csrng_info("genbit not ready", slot); + } + } else { + /* SW instance */ + ot_csrng_swapp_fill(inst); + } + } break; case CSRNG_CMD_STALLED: xtrace_ot_csrng_error("entropy stack stalled"); @@ -1598,78 +1759,103 @@ static void ot_csrng_command_scheduler(void *opaque) break; } - if (!QSIMPLEQ_EMPTY(&s->cmd_requests)) { - if (s->state != CSRNG_ERROR) { - trace_ot_csrng_scheduling_command(slot); - if (CSRNG_CMD_RETRY == res && s->entropy_delay) { - timer_mod(s->entropy_scheduler, - qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + - (int64_t)s->entropy_delay); - s->entropy_delay = 0; - } else { - qemu_bh_schedule(s->cmd_scheduler); - } - } else { - xtrace_ot_csrng_error("cannot schedule new command on error"); - } - } + goto next; } static uint32_t ot_csrng_read_state_db(OtCSRNGState *s) { - unsigned appid = s->regs[R_INT_STATE_NUM]; - if (appid >= N_APP_COUNT) { - qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid appid %d\n", __func__, - appid); - return 0; - } - if (!s->read_int_granted) { + if (!s->enabled || !s->read_int_granted) { + s->state_db_ix = 0xfu; qemu_log_mask(LOG_GUEST_ERROR, "%s: read state db disabled\n", __func__); return 0; } - if (!((s->regs[R_INT_STATE_READ_ENABLE] >> appid) & 0x1)) { - qemu_log_mask(LOG_GUEST_ERROR, "%s: read state db not enable for %d\n", - __func__, appid); - return 0; + + unsigned appid = s->state_db_dump_id; + uint32_t val32 = 0; + + if (appid < N_APP_COUNT && + ((s->regs[R_INT_STATE_READ_ENABLE] >> appid) & 0x1u) && + s->state_db_ix < 14u) { + OtCSRNGInstance *inst = &s->instances[appid]; + OtCSRNGDrng *drng = &inst->drng; + unsigned base; + switch (s->state_db_ix) { + case 0: /* Reseed counter */ + val32 = drng->reseed_counter; + break; + case 1u ... 4u: /* V (counter) */ + /* use big endian and reverse order to match OpenTitan order */ + base = 4u - s->state_db_ix; + val32 = ldl_be_p(&drng->v_counter[base * sizeof(uint32_t)]); + break; + case 5u ... 12u: /* Key */ + /* use big endian and reverse order to match OpenTitan order */ + base = 12u - s->state_db_ix; + val32 = ldl_be_p(&drng->key[base * sizeof(uint32_t)]); + break; + case 13u: /* Status + Compliance, only 8 LSBs matter */ + val32 = (uint32_t)((((uint8_t)drng->instantiated) << 0u) | + (((uint8_t)!drng->no_fips) << 1u)); + break; + default: + val32 = 0; + break; + } + trace_ot_csrng_read_state_db(ot_csrng_get_slot(inst), s->state_db_ix, + val32); } - OtCSRNGInstance *inst = &s->instances[appid]; - OtCSRNGDrng *drng = &inst->drng; - uint32_t val32; - unsigned base; - switch (s->state_db_ix) { - case 0: /* Reseed counter */ - val32 = drng->reseed_counter; - break; - case 1u ... 4u: /* V (counter) */ - /* use big endian and reverse order to match OpenTitan order */ - base = 4u - s->state_db_ix; - val32 = ldl_be_p(&drng->v_counter[base * sizeof(uint32_t)]); - break; - case 5u ... 12u: /* Key */ - /* use big endian and reverse order to match OpenTitan order */ - base = 12u - s->state_db_ix; - val32 = ldl_be_p(&drng->key[base * sizeof(uint32_t)]); + /* + * csrng_state_db.sv:162-168: + * (reg_rd_ptr_q == 4'he) ? '0 : reg_rd_ptr_inc ? (reg_rd_ptr_q + 1) : ... + * Reading when reg_rd_ptr_q == 4'hf returns '0 and advances ptr to 4'h0. + * Reading when reg_rd_ptr_q == 4'hd (13) advances ptr to 4'he, which + * wraps to 4'h0 on the next cycle. + */ + s->state_db_ix = (s->state_db_ix + 1u) & 0xfu; + if (s->state_db_ix == 0xeu) { + s->state_db_ix = 0u; + } + + return val32; +} + +static bool ot_csrng_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + if (reg >= REGS_COUNT) { + return false; + } + if (!is_write) { + return true; + } + uint32_t permit; + switch (reg) { + case R_CMD_REQ: + case R_RESEED_INTERVAL: + case R_RESEED_COUNTER_0: + case R_RESEED_COUNTER_1: + case R_RESEED_COUNTER_2: + case R_GENBITS: + case R_INT_STATE_VAL: + case R_ERR_CODE: + permit = 0xfu; break; - case 13u: /* Status + Compliance, only 8 LSBs matter */ - val32 = (uint32_t)((((uint8_t)drng->instantiated) << 0u) | - (((uint8_t)!drng->no_fips) << 1u)); + case R_CTRL: + case R_HW_EXC_STS: + case R_RECOV_ALERT_STS: + permit = 0x3u; break; default: - val32 = 0; + permit = 0x1u; break; } - - trace_ot_csrng_read_state_db(ot_csrng_get_slot(inst), s->state_db_ix, - val32); - - s->state_db_ix += 1u; - if (s->state_db_ix >= 14u) { - s->state_db_ix = 0; - } - - return val32; + uint32_t reg_be = (((1u << size) - 1u) << (addr & 0x3u)) & 0xfu; + return (permit & ~reg_be) == 0u; } static uint64_t ot_csrng_regs_read(void *opaque, hwaddr addr, unsigned size) @@ -1683,12 +1869,20 @@ static uint64_t ot_csrng_regs_read(void *opaque, hwaddr addr, unsigned size) switch (reg) { case R_INTR_STATE: + if (s->waiting_for_entropy) { + ot_csrng_command_scheduler(s); + } + val32 = s->regs[reg]; + break; case R_INTR_ENABLE: case R_REGWEN: case R_CTRL: case R_RESEED_INTERVAL: case R_SW_CMD_STS: + case R_INT_STATE_READ_ENABLE: + case R_INT_STATE_READ_ENABLE_REGWEN: case R_INT_STATE_NUM: + case R_FIPS_FORCE: case R_HW_EXC_STS: case R_RECOV_ALERT_STS: case R_ERR_CODE: @@ -1719,31 +1913,27 @@ static uint64_t ot_csrng_regs_read(void *opaque, hwaddr addr, unsigned size) break; case R_GENBITS_VLD: inst = &s->instances[SW_INSTANCE_ID]; - if (ot_csrng_is_sw_app_enabled(s)) { + { bool avail = !ot_fifo32_is_empty(&inst->sw.bits_fifo); val32 = FIELD_DP32(0, GENBITS_VLD, GENBITS_VLD, (uint32_t)avail); val32 = FIELD_DP32(val32, GENBITS_VLD, GENBITS_FIPS, inst->sw.fips); - } else { - qemu_log_mask(LOG_GUEST_ERROR, "SW APP not enabled\n"); - val32 = 0; } break; case R_GENBITS: - if (ot_csrng_is_sw_app_enabled(s)) { - inst = &s->instances[SW_INSTANCE_ID]; - if (!ot_fifo32_is_empty(&inst->sw.bits_fifo)) { - val32 = ot_fifo32_pop(&inst->sw.bits_fifo); - xtrace_ot_csrng_info("pop", val32); - if (ot_fifo32_is_empty(&inst->sw.bits_fifo)) { - /* keep the SW FIFO filled up till end of generation */ - ot_csrng_swapp_fill(inst); - } - } else { - /* TBC: need to check if some error is signaled in ERR_CODE */ - qemu_log_mask(LOG_GUEST_ERROR, "FIFO read w/ entropy bits\n"); + inst = &s->instances[SW_INSTANCE_ID]; + if (!ot_fifo32_is_empty(&inst->sw.bits_fifo)) { + val32 = ot_fifo32_pop(&inst->sw.bits_fifo); + xtrace_ot_csrng_info("pop", val32); + if (ot_fifo32_is_empty(&inst->sw.bits_fifo)) { + /* keep the SW FIFO filled up till end of generation */ + ot_csrng_swapp_fill(inst); + } + if (!ot_csrng_is_sw_app_enabled(s)) { val32 = 0; } } else { + /* TBC: need to check if some error is signaled in ERR_CODE */ + qemu_log_mask(LOG_GUEST_ERROR, "FIFO read w/ entropy bits\n"); val32 = 0; } break; @@ -1782,8 +1972,14 @@ static void ot_csrng_regs_write(void *opaque, hwaddr addr, uint64_t val64, switch (reg) { case R_INTR_STATE: + if (s->waiting_for_entropy) { + ot_csrng_command_scheduler(s); + } val32 &= INTR_MASK; s->regs[reg] &= ~val32; /* RW1C */ + if (s->waiting_for_entropy) { + s->regs[reg] |= INTR_CS_ENTROPY_REQ_MASK; + } ot_csrng_update_irqs(s); break; case R_INTR_ENABLE: @@ -1838,11 +2034,16 @@ static void ot_csrng_regs_write(void *opaque, hwaddr addr, uint64_t val64, s->sw_app_granted = false; s->read_int_granted = false; } + if (!s->enabled || !s->read_int_granted) { + s->state_db_ix = 0xfu; + } } break; case R_CMD_REQ: + s->regs[R_SW_CMD_STS] &= ~R_SW_CMD_STS_CMD_ACK_MASK; inst = &s->instances[SW_INSTANCE_ID]; - if (!ot_fifo32_is_full(&inst->cmd_fifo)) { + if (!ot_fifo32_is_full(&inst->cmd_fifo) && + !ot_csrng_is_in_queue(inst)) { ot_fifo32_push(&inst->cmd_fifo, val32); if (ot_csrng_instance_is_command_ready(inst, false)) { /* @@ -1876,14 +2077,15 @@ static void ot_csrng_regs_write(void *opaque, hwaddr addr, uint64_t val64, s->regs[reg] &= val32; /* rw0c */ break; case R_INT_STATE_NUM: - if (s->read_int_granted) { - val32 &= R_INT_STATE_NUM_VAL_MASK; - s->regs[reg] = val32; - s->state_db_ix = 0; - if (val32 > OT_CSRNG_HW_APP_MAX) { - qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid INT_STATE_NUM %u\n", - __func__, val32); - } + val32 &= R_INT_STATE_NUM_VAL_MASK; + s->regs[reg] = val32; + if (s->enabled) { + s->state_db_dump_id = val32; + } + s->state_db_ix = (s->enabled && s->read_int_granted) ? 0u : 0xfu; + if (val32 > OT_CSRNG_HW_APP_MAX) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid INT_STATE_NUM %u\n", + __func__, val32); } break; case R_FIPS_FORCE: @@ -1901,7 +2103,13 @@ static void ot_csrng_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_RECOV_ALERT_STS: val32 &= RECOV_ALERT_STS_MASK; - s->regs[reg] &= val32; /* RW0C */ + /* + * In csrng_core.sv:784-846, invalid CTRL mubi4 field alerts + * continuously assert hw2reg.recov_alert_sts.*_field_alert.{de,d} = + * 1'b1 as long as the invalid mubi4 value remains in CTRL. + */ + s->regs[reg] = + (s->regs[reg] & val32) | ot_csrng_get_ctrl_recov_alert_sts(s); ot_csrng_update_alerts(s); break; case R_ERR_CODE_TEST: @@ -1911,9 +2119,23 @@ static void ot_csrng_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; } val32 &= R_ERR_CODE_TEST_VAL_MASK; - val32 = 1u << val32; - val32 &= ERR_CODE_MASK; - s->regs[R_ERR_CODE] = val32; + s->regs[R_ERR_CODE_TEST] = val32; + uint32_t err_bit = (1u << val32) & ERR_CODE_MASK; + uint32_t err_en_mask = + ot_csrng_is_ctrl_enabled(s) ? ERR_CODE_MASK : + R_ERR_CODE_CMD_GEN_CNT_ERR_MASK; + s->regs[R_ERR_CODE] |= err_bit & err_en_mask; + if (err_bit & 0x07f00000u) { + if (ot_csrng_is_ctrl_enabled(s)) { + s->regs[R_ERR_CODE] |= R_ERR_CODE_MAIN_SM_ERR_MASK; + } + CHANGE_STATE(s, CSRNG_ERROR); + } else if ((err_bit & err_en_mask) != 0u) { + s->regs[R_INTR_STATE] |= INTR_CS_FATAL_ERR_MASK; + ot_csrng_update_irqs(s); + ibex_irq_set(&s->alerts[ALERT_FATAL], 1); + ibex_irq_set(&s->alerts[ALERT_FATAL], 0); + } break; case R_RESEED_COUNTER_0: case R_RESEED_COUNTER_1: @@ -1946,6 +2168,7 @@ static const MemoryRegionOps ot_csrng_regs_ops = { .read = &ot_csrng_regs_read, .write = &ot_csrng_regs_write, .endianness = DEVICE_NATIVE_ENDIAN, + .valid.accepts = &ot_csrng_regs_accepts, .impl.min_access_size = 4u, .impl.max_access_size = 4u, }; @@ -1979,9 +2202,12 @@ static void ot_csrng_reset_enter(Object *obj, ResetType type) s->regs[R_INT_STATE_READ_ENABLE] = 0x7u; s->regs[R_MAIN_SM_STATE] = 0x4eu; s->enabled = false; + s->waiting_for_entropy = false; s->sw_app_granted = false; s->read_int_granted = false; s->es_retry_count = 0; + s->state_db_ix = 0xfu; + s->state_db_dump_id = 0u; s->state = CSRNG_IDLE; for (unsigned ix = 0; ix < N_APP_COUNT; ix++) { @@ -1994,7 +2220,9 @@ static void ot_csrng_reset_enter(Object *obj, ResetType type) } OtCSRNGDrng *drng = &inst->drng; memset(drng, 0, sizeof(*drng)); + drng->no_fips = true; } + ot_csrng_update_irqs(s); for (unsigned ix = 0; ix < PARAM_NUM_ALERTS; ix++) { ibex_irq_set(&s->alerts[ix], 0); @@ -2021,7 +2249,7 @@ static void ot_csrng_init(Object *obj) OtCSRNGState *s = OT_CSRNG(obj); memory_region_init_io(&s->mmio, obj, &ot_csrng_regs_ops, s, TYPE_OT_CSRNG, - REGS_SIZE); + MAX(REGS_SIZE, 0x80u)); sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); /* aes_desc is defined in libtomcrypt */ From d97d61562aadc16719582bed6d4b543e2b097f62 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:34 +0000 Subject: [PATCH 25/52] [ot] hw/opentitan: edn: implement boot request mode and auto FSM Align the OpenTitan Earlgrey EDN model (hw/opentitan/ot_edn.c) with RTL (edn_core.sv and edn_main_sm.sv): - Implement HW_CMD_STS (BOOT_MODE, AUTO_MODE, CMD_TYPE, CMD_ACK, CMD_STS) and SW_CMD_STS state tracking across boot-request, auto-request, and SW port modes, including dropping SW_CMD_STS.CMD_RDY to 0 while CMD_REG_RDY remains 1 in AutoFirstAckWait and forwarding an all-zero command word to CSRNG on empty sfifo_rescmd / sfifo_gencmd replay FIFO underflow. - Implement RECOV_ALERT_STS CSRNG_ACK_ERR and EDN_BUS_CMP_ALERT (detecting repeated 128-bit CSRNG packets and entering RejectCsrngEntropy 0x018). - Implement ERR_CODE / ERR_CODE_TEST fatal alerts (gating FIFO error bits 0, 1, 28..30 by edn_enable_fo and excluding standalone bit 30 from fatal_loc_events), ALERT_TEST pulse generation, sub-word CSR write rejection, and immediate endpoint request servicing in ot_edn_request_entropy(). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ic524fa00040071347fb9bce1792d81406a6a6964 --- hw/opentitan/ot_edn.c | 423 +++++++++++++++++++++++++++++------------- 1 file changed, 292 insertions(+), 131 deletions(-) diff --git a/hw/opentitan/ot_edn.c b/hw/opentitan/ot_edn.c index 2acdf25ee95bb..71ea8e70c8450 100644 --- a/hw/opentitan/ot_edn.c +++ b/hw/opentitan/ot_edn.c @@ -30,6 +30,7 @@ #include "qemu/log.h" #include "qemu/main-loop.h" #include "qemu/typedefs.h" +#include "hw/core/cpu.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_csrng.h" @@ -123,7 +124,8 @@ REG32(MAIN_SM_STATE, 0x44u) R_RECOV_ALERT_STS_BOOT_REQ_MODE_FIELD_ALERT_MASK | \ R_RECOV_ALERT_STS_AUTO_REQ_MODE_FIELD_ALERT_MASK | \ R_RECOV_ALERT_STS_CMD_FIFO_RST_FIELD_ALERT_MASK | \ - R_RECOV_ALERT_STS_EDN_BUS_CMP_ALERT_MASK) + R_RECOV_ALERT_STS_EDN_BUS_CMP_ALERT_MASK | \ + R_RECOV_ALERT_STS_CSRNG_ACK_ERR_MASK) #define ERR_CODE_MASK \ (R_ERR_CODE_SFIFO_RESCMD_ERR_MASK | R_ERR_CODE_SFIFO_GENCMD_ERR_MASK | \ R_ERR_CODE_EDN_ACK_SM_ERR_MASK | R_ERR_CODE_EDN_MAIN_SM_ERR_MASK | \ @@ -132,6 +134,8 @@ REG32(MAIN_SM_STATE, 0x44u) #define ERR_CODE_ACTIVE_MASK \ (R_ERR_CODE_EDN_ACK_SM_ERR_MASK | R_ERR_CODE_EDN_MAIN_SM_ERR_MASK | \ R_ERR_CODE_EDN_CNTR_ERR_MASK) +#define ERR_CODE_FIFO_FATAL_MASK \ + (R_ERR_CODE_SFIFO_RESCMD_ERR_MASK | R_ERR_CODE_SFIFO_GENCMD_ERR_MASK) #define ALERT_STATUS_BIT(_x_) R_RECOV_ALERT_STS_##_x_##_FIELD_ALERT_MASK @@ -147,6 +151,7 @@ static const char *REG_NAMES[REGS_COUNT] = { REG_NAME_ENTRY(BOOT_GEN_CMD), REG_NAME_ENTRY(SW_CMD_REQ), REG_NAME_ENTRY(SW_CMD_STS), + REG_NAME_ENTRY(HW_CMD_STS), REG_NAME_ENTRY(RESEED_CMD), REG_NAME_ENTRY(GENERATE_CMD), REG_NAME_ENTRY(MAX_NUM_REQS_BETWEEN_RESEEDS), @@ -214,10 +219,14 @@ typedef struct { OtFifo32 cmd_gen_fifo; /* "Replay" FIFO to store generate command */ OtFifo32 cmd_reseed_fifo; /* "Replay" FIFO to store reseed command */ uint8_t hw_cmd_type; /* type of the last CSRNG HW command */ + bool hw_boot_mode; /* latched HW_CMD_STS.BOOT_MODE until Idle/done */ + bool hw_auto_mode; /* latched HW_CMD_STS.AUTO_MODE until Idle/done */ bool hw_ack; /* last SW command has been completed */ bool sw_ack; /* last SW command has been completed */ bool instantiated; /* instantiated state, not yet uninstantiated */ bool no_fips; /* true if 1+ rcv entropy packets were no FIPS-compliant */ + uint32_t prev_bits[OT_CSRNG_PACKET_WORD_COUNT]; + bool prev_bits_valid; } OtEDNCSRNG; typedef struct OtEDNEndPoint { @@ -241,7 +250,7 @@ struct OtEDNState { QEMUBH *ep_bh; /**< Endpoint requests */ uint32_t *regs; - uint32_t recov_alert_sts; /* track signalled recovery alert */ + uint32_t recov_alert_sts; /* track live CTRL MuBi4 recoverable errors */ unsigned max_reqs_cnt; /* track remaining requests before reseeding */ OtEDNFsmState state; /* Main FSM state */ @@ -249,6 +258,7 @@ struct OtEDNState { OtEDNEndPoint endpoints[ENDPOINT_COUNT_MAX]; OtEndpointQueue ep_requests; bool sw_cmd_ready; /* ready to receive command in SW port mode */ + bool in_ep_request; }; struct OtEDNClass { @@ -309,6 +319,8 @@ static const char *STATE_NAMES[] = { static void ot_edn_fill_bits(void *opaque, const uint32_t *bits, bool fips); static void ot_edn_csrng_ack_irq(void *opaque, int n, int level); +static void ot_edn_handle_ep_request(void *opaque); +static bool ot_edn_is_enabled(const OtEDNState *s); /* -------------------------------------------------------------------------- */ /* Public API */ @@ -352,7 +364,15 @@ int ot_edn_request_entropy(OtEDNState *s, unsigned ep_id) QSIMPLEQ_INSERT_TAIL(&s->ep_requests, ep, request); trace_ot_edn_schedule(s->rng.appid, "external entropy request"); - qemu_bh_schedule(s->ep_bh); + if (!s->in_ep_request) { + ot_edn_handle_ep_request(s); + } + if (!QSIMPLEQ_EMPTY(&s->ep_requests)) { + qemu_bh_schedule(s->ep_bh); + if (current_cpu) { + cpu_exit(current_cpu); + } + } return 0; } @@ -371,6 +391,15 @@ static void ot_edn_update_irqs(OtEDNState *s) } } +static void ot_edn_trigger_recov_alert(OtEDNState *s, uint32_t sts_mask) +{ + s->regs[R_RECOV_ALERT_STS] |= sts_mask; + if (s->recov_alert_sts == 0u) { + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 1); + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 0); + } +} + static void ot_edn_update_alerts(OtEDNState *s) { uint32_t level = s->regs[R_ALERT_TEST]; @@ -380,23 +409,20 @@ static void ot_edn_update_alerts(OtEDNState *s) if (s->regs[R_ERR_CODE] & ERR_CODE_ACTIVE_MASK) { level |= 1u << ALERT_FATAL; } - if (s->regs[R_ERR_CODE_TEST] & ERR_CODE_ACTIVE_MASK) { + /* + * In hw/ip/edn/rtl/edn_core.sv, sfifo_*_err signals are gated by + * edn_enable_fo before driving fatal_loc_events / event_edn_fatal_err. + */ + if (ot_edn_is_enabled(s) && + (s->regs[R_ERR_CODE] & ERR_CODE_FIFO_FATAL_MASK)) { level |= 1u << ALERT_FATAL; - /* - * "The action of writing this register will force an error pulse." - * This documented assertion does not seem to hold true. Alert seems - * sticky - */ - } - if (s->regs[R_RECOV_ALERT_STS] & RECOV_ALERT_STS_MASK) { - /* recoverable alerts do not trigger stick alert */ - if (!(s->recov_alert_sts & (1u << ALERT_RECOVERABLE))) { - level |= 1u << ALERT_RECOVERABLE; - s->recov_alert_sts |= 1u << ALERT_RECOVERABLE; - } } + for (unsigned ix = 0; ix < PARAM_NUM_ALERTS; ix++) { - ibex_irq_set(&s->alerts[ix], (int)((level >> ix) & 0x1u)); + if ((level >> ix) & 0x1u) { + ibex_irq_set(&s->alerts[ix], 1); + ibex_irq_set(&s->alerts[ix], 0); + } } } @@ -412,8 +438,8 @@ static bool ot_edn_check_multibitboot(OtEDNState *s, uint8_t mbbool, break; } - s->regs[R_RECOV_ALERT_STS] |= 1u << alert_bit; - ot_edn_update_alerts(s); + s->regs[R_RECOV_ALERT_STS] |= alert_bit; + s->recov_alert_sts |= alert_bit; return false; } @@ -440,24 +466,12 @@ static bool ot_edn_is_auto_req_mode(const OtEDNState *s) static bool ot_edn_is_boot_mode(const OtEDNState *s) { - /* NOLINTNEXTLINE */ - switch (s->state) { - case EDN_BOOT_INS_ACK_WAIT ... EDN_BOOT_UNI_ACK_WAIT: - return true; - default: - return false; - } + return s->rng.hw_boot_mode; } static bool ot_edn_is_auto_mode(const OtEDNState *s) { - /* NOLINTNEXTLINE */ - switch (s->state) { - case EDN_AUTO_ACK_WAIT ... EDN_AUTO_SEND_RESEED_CMD: - return true; - default: - return false; - } + return s->rng.hw_auto_mode; } static bool ot_edn_is_sw_cmd_mode(const OtEDNState *s) @@ -499,10 +513,8 @@ static bool ot_edn_is_cmd_reg_rdy(const OtEDNState *s) switch (s->state) { case EDN_AUTO_LOAD_INS: case EDN_AUTO_FIRST_ACK_WAIT: - case EDN_AUTO_DISPATCH: - return true; case EDN_SW_PORT_MODE: - return s->sw_cmd_ready; + return true; default: return false; } @@ -510,10 +522,15 @@ static bool ot_edn_is_cmd_reg_rdy(const OtEDNState *s) static bool ot_edn_is_cmd_rdy(const OtEDNState *s) { + /* + * In hw/ip/edn/rtl/edn_core.sv and edn_main_sm.sv, after the first word of + * SW_CMD_REQ is written in AutoLoadIns, the FSM transitions immediately to + * AutoFirstAckWait where sw_cmd_req_load is no longer asserted, so + * SW_CMD_STS.CMD_RDY drops to 0 while CMD_REG_RDY remains 1. + */ /* NOLINTNEXTLINE */ switch (s->state) { case EDN_AUTO_LOAD_INS: - case EDN_AUTO_DISPATCH: return true; case EDN_SW_PORT_MODE: return s->sw_cmd_ready; @@ -563,14 +580,14 @@ static void ot_edn_change_state_line(OtEDNState *s, OtEDNFsmState state, s->state = state; switch (s->state) { + case EDN_IDLE: + case EDN_SW_PORT_MODE: + s->rng.hw_boot_mode = false; + s->rng.hw_auto_mode = false; + break; case EDN_ERROR: - s->rng.hw_cmd_type = (uint8_t)OT_CSRNG_CMD_NONE; ot_edn_manage_error(s); break; - case EDN_IDLE: - case EDN_REJECT_CSRNG_ENTROPY: - s->rng.hw_cmd_type = (uint8_t)OT_CSRNG_CMD_NONE; - break; default: break; } @@ -598,9 +615,14 @@ static bool ot_edn_update_genbits_ready(OtEDNState *s) { OtEDNCSRNG *c = &s->rng; + unsigned max_words = (s->state == EDN_SW_PORT_MODE) ? + (OT_CSRNG_PACKET_WORD_COUNT * 16u) : + (OT_CSRNG_PACKET_WORD_COUNT * 2u); bool accept_entropy = ot_edn_is_enabled(s) && (c->rem_packet_count > 0) && - ot_fifo32_num_free(&c->bits_fifo) > OT_CSRNG_PACKET_WORD_COUNT; + (ot_fifo32_num_used(&c->bits_fifo) + OT_CSRNG_PACKET_WORD_COUNT <= + max_words) && + ot_fifo32_num_free(&c->bits_fifo) >= OT_CSRNG_PACKET_WORD_COUNT; trace_ot_edn_update_genbits_ready(c->appid, c->rem_packet_count, ot_fifo32_num_free(&c->bits_fifo) / @@ -618,6 +640,12 @@ ot_edn_push_csrng_request(OtEDNState *s, bool auto_mode, uint32_t length) OtEDNCSRNG *c = &s->rng; + if (auto_mode) { + c->hw_auto_mode = true; + } + c->hw_ack = false; + c->hw_cmd_status = CSRNG_STATUS_SUCCESS; + OtCSRNGCmdStatus res = CSRNG_STATUS_INVALID_ACMD; for (unsigned cix = 0; cix < length; cix++) { @@ -626,11 +654,12 @@ ot_edn_push_csrng_request(OtEDNState *s, bool auto_mode, uint32_t length) res = c->csrng->push_command(c->device, c->appid, c->buffer[cix]); if (res != CSRNG_STATUS_SUCCESS) { trace_ot_edn_push_csrng_error(c->appid, (int)res); + c->hw_cmd_status = res; + c->hw_ack = true; ot_edn_change_state(s, EDN_REJECT_CSRNG_ENTROPY); - s->regs[R_RECOV_ALERT_STS] |= R_RECOV_ALERT_STS_CSRNG_ACK_ERR_MASK; /* do not expect any delayed completion */ - memset(c->buffer, 0, sizeof(*c->buffer)); - ot_edn_update_alerts(s); + memset(c->buffer, 0, sizeof(c->buffer)); + ot_edn_trigger_recov_alert(s, R_RECOV_ALERT_STS_CSRNG_ACK_ERR_MASK); break; } } @@ -673,7 +702,7 @@ static void ot_edn_send_boot_req(OtEDNState *s, unsigned reg) */ } - if (command == OT_CSRNG_CMD_GENERATE) { + if (reg == R_BOOT_GEN_CMD) { c->rem_packet_count = FIELD_EX32(c->buffer[0], OT_CSNRG_CMD, GLEN); xtrace_ot_edn_dinfo(c->appid, "Boot generation w/ packets", c->rem_packet_count); @@ -685,26 +714,25 @@ static void ot_edn_send_boot_req(OtEDNState *s, unsigned reg) } c->hw_cmd_type = (uint8_t)FIELD_EX32(command, OT_CSNRG_CMD, ACMD); - c->hw_cmd_status = ot_edn_push_csrng_request(s, false, 1u); - if (c->hw_cmd_status) { - return; - } /* * CSRNG request should be completed asynchronously, changing the state here * should occur before ot_edn_csrng_ack_irq is called. */ - switch (command) { - case OT_CSRNG_CMD_INSTANTIATE: + switch (reg) { + case R_BOOT_INS_CMD: c->no_fips = false; + c->hw_boot_mode = true; ot_edn_change_state(s, EDN_BOOT_INS_ACK_WAIT); break; - case OT_CSRNG_CMD_GENERATE: + case R_BOOT_GEN_CMD: ot_edn_change_state(s, EDN_BOOT_GEN_ACK_WAIT); break; default: g_assert_not_reached(); } + + (void)ot_edn_push_csrng_request(s, false, 1u); } static void ot_edn_send_auto_reseed_cmd(OtEDNState *s) @@ -715,24 +743,33 @@ static void ot_edn_send_auto_reseed_cmd(OtEDNState *s) return; } - bool fatal_error = false; uint32_t command; uint32_t length; if (ot_fifo32_is_empty(&c->cmd_reseed_fifo)) { + /* + * In hw/ip/edn/rtl/edn_core.sv, sfifo_rescmd_err asserts ERR_CODE and + * fatal alert/interrupt, but edn_main_sm still transitions through + * AutoSendReseedCmd -> AutoAckWait and pushes an all-zero word to + * CSRNG. + */ s->regs[R_ERR_CODE] |= R_ERR_CODE_SFIFO_RESCMD_ERR_MASK | R_ERR_CODE_FIFO_READ_ERR_MASK; - fatal_error = true; + s->regs[R_INTR_STATE] |= INTR_EDN_FATAL_ERR_MASK; + ot_edn_update_irqs(s); + ot_edn_update_alerts(s); command = (uint32_t)OT_CSRNG_CMD_NONE; length = 1u; /* always push the command */ + c->buffer[0] = command; } else { command = ot_fifo32_peek(&c->cmd_reseed_fifo); - length = FIELD_EX32(command, OT_CSNRG_CMD, CLEN) + 1u; - if (ot_fifo32_num_used(&c->cmd_reseed_fifo) < length) { - s->regs[R_ERR_CODE] |= R_ERR_CODE_SFIFO_RESCMD_ERR_MASK | - R_ERR_CODE_FIFO_READ_ERR_MASK; - fatal_error = true; - } + /* + * In hw/ip/edn/rtl/edn_core.sv (sfifo_rescmd), the replay FIFO pops + * and rotates every pushed word back to wdata (`fifo_rescmd_rready`), + * ignoring CLEN; only the actual pushed FIFO depth is sent. + */ + length = MIN(ot_fifo32_num_used(&c->cmd_reseed_fifo), + FIELD_EX32(command, OT_CSNRG_CMD, CLEN) + 1u); uint32_t num = length; const uint32_t *cmd; cmd = ot_fifo32_peek_buf(&c->cmd_reseed_fifo, num, &length); @@ -742,22 +779,12 @@ static void ot_edn_send_auto_reseed_cmd(OtEDNState *s) memcpy(c->buffer, cmd, length * sizeof(uint32_t)); } - - if (fatal_error) { - ot_edn_change_state(s, EDN_ERROR); - return; - } - ot_edn_change_state(s, EDN_AUTO_SEND_RESEED_CMD); - - c->hw_cmd_type = (uint8_t)FIELD_EX32(command, OT_CSNRG_CMD, ACMD); - c->hw_cmd_status = ot_edn_push_csrng_request(s, true, length); - if (c->hw_cmd_status) { - return; - } c->no_fips = false; - ot_edn_change_state(s, EDN_AUTO_ACK_WAIT); + + c->hw_cmd_type = (uint8_t)FIELD_EX32(command, OT_CSNRG_CMD, ACMD); + (void)ot_edn_push_csrng_request(s, true, length); } static void ot_edn_send_auto_generate_cmd(OtEDNState *s) @@ -768,24 +795,32 @@ static void ot_edn_send_auto_generate_cmd(OtEDNState *s) return; } - bool fatal_error = false; uint32_t command; uint32_t length; if (ot_fifo32_is_empty(&c->cmd_gen_fifo)) { + /* + * In hw/ip/edn/rtl/edn_core.sv, sfifo_gencmd_err asserts ERR_CODE and + * fatal alert/interrupt, but edn_main_sm still transitions through + * AutoSendGenCmd -> AutoAckWait and pushes an all-zero word to CSRNG. + */ s->regs[R_ERR_CODE] |= R_ERR_CODE_SFIFO_GENCMD_ERR_MASK | R_ERR_CODE_FIFO_READ_ERR_MASK; - fatal_error = true; + s->regs[R_INTR_STATE] |= INTR_EDN_FATAL_ERR_MASK; + ot_edn_update_irqs(s); + ot_edn_update_alerts(s); command = (uint32_t)OT_CSRNG_CMD_NONE; length = 1u; /* always push the command */ + c->buffer[0] = command; } else { command = ot_fifo32_peek(&c->cmd_gen_fifo); - length = FIELD_EX32(command, OT_CSNRG_CMD, CLEN) + 1u; - if (ot_fifo32_num_used(&c->cmd_gen_fifo) < length) { - s->regs[R_ERR_CODE] |= R_ERR_CODE_SFIFO_GENCMD_ERR_MASK | - R_ERR_CODE_FIFO_READ_ERR_MASK; - fatal_error = true; - } + /* + * In hw/ip/edn/rtl/edn_core.sv (sfifo_gencmd), the replay FIFO pops + * and rotates every pushed word back to wdata (`fifo_gencmd_rready`), + * ignoring CLEN; only the actual pushed FIFO depth is sent. + */ + length = MIN(ot_fifo32_num_used(&c->cmd_gen_fifo), + FIELD_EX32(command, OT_CSNRG_CMD, CLEN) + 1u); uint32_t num = length; const uint32_t *cmd = ot_fifo32_peek_buf(&c->cmd_gen_fifo, num, &length); @@ -798,11 +833,6 @@ static void ot_edn_send_auto_generate_cmd(OtEDNState *s) c->rem_packet_count); } - if (fatal_error) { - ot_edn_change_state(s, EDN_ERROR); - return; - } - xtrace_ot_edn_xinfo(c->appid, "COMMAND", c->buffer[0]); ot_edn_update_genbits_ready(s); @@ -812,17 +842,14 @@ static void ot_edn_send_auto_generate_cmd(OtEDNState *s) s->endpoints[epix].gen_count = 0; } - c->hw_cmd_type = (uint8_t)FIELD_EX32(command, OT_CSNRG_CMD, ACMD); - c->hw_cmd_status = ot_edn_push_csrng_request(s, true, length); - if (c->hw_cmd_status) { - return; - } - if (s->max_reqs_cnt) { s->max_reqs_cnt -= 1u; } ot_edn_change_state(s, EDN_AUTO_ACK_WAIT); + + c->hw_cmd_type = (uint8_t)FIELD_EX32(command, OT_CSNRG_CMD, ACMD); + (void)ot_edn_push_csrng_request(s, true, length); } static void ot_edn_send_boot_uninstanciate_cmd(OtEDNState *s) @@ -839,12 +866,8 @@ static void ot_edn_send_boot_uninstanciate_cmd(OtEDNState *s) FIELD_DP32(0, OT_CSNRG_CMD, ACMD, OT_CSRNG_CMD_UNINSTANTIATE); c->hw_cmd_type = (uint8_t)OT_CSRNG_CMD_UNINSTANTIATE; - c->hw_cmd_status = ot_edn_push_csrng_request(s, false, 1u); - if (c->hw_cmd_status) { - return; - } - ot_edn_change_state(s, EDN_BOOT_UNI_ACK_WAIT); + (void)ot_edn_push_csrng_request(s, false, 1u); } static void ot_edn_handle_disable(OtEDNState *s) @@ -854,6 +877,7 @@ static void ot_edn_handle_disable(OtEDNState *s) g_assert(ot_edn_get_last_csrng_command(s) != OT_CSRNG_CMD_UNINSTANTIATE); c->no_fips = false; + c->prev_bits_valid = false; c->rem_packet_count = 0; xtrace_ot_edn_dinfo(c->appid, "discard all entropy packets", ot_fifo32_num_used(&c->bits_fifo)); @@ -891,14 +915,20 @@ static void ot_edn_clean_up(OtEDNState *s, bool discard_requests) trace_ot_edn_clean_up(c->appid, discard_requests); c->instantiated = false; + c->prev_bits_valid = false; s->sw_cmd_ready = false; + c->hw_cmd_type = (uint8_t)OT_CSRNG_CMD_NONE; + if (discard_requests) { + c->hw_boot_mode = false; + c->hw_auto_mode = false; + } c->hw_cmd_status = CSRNG_STATUS_SUCCESS; c->sw_cmd_status = CSRNG_STATUS_SUCCESS; c->hw_ack = false; c->sw_ack = false; s->recov_alert_sts = 0u; - s->max_reqs_cnt = 0; - memset(c->buffer, 0, sizeof(*c->buffer)); + s->max_reqs_cnt = s->regs[R_MAX_NUM_REQS_BETWEEN_RESEEDS]; + memset(c->buffer, 0, sizeof(c->buffer)); ot_fifo32_reset(&c->bits_fifo); ot_edn_update_irqs(s); ot_edn_update_alerts(s); @@ -954,6 +984,7 @@ static bool ot_edn_update_mode(OtEDNState *s) } else { trace_ot_edn_enable(c->appid, "sw mode"); s->sw_cmd_ready = true; + s->max_reqs_cnt = s->regs[R_MAX_NUM_REQS_BETWEEN_RESEEDS]; ot_edn_reset_replay_fifos(s); ot_edn_change_state(s, EDN_SW_PORT_MODE); } @@ -976,6 +1007,8 @@ static void ot_edn_handle_ctrl(OtEDNState *s, uint32_t val32) bool enabled = FIELD_EX32(s->regs[R_CTRL], CTRL, EDN_ENABLE) == OT_MULTIBITBOOL4_TRUE; + uint32_t prev_ctrl_recov = s->recov_alert_sts; + s->recov_alert_sts = 0u; s->regs[R_CTRL] = val32; @@ -988,6 +1021,11 @@ static void ot_edn_handle_ctrl(OtEDNState *s, uint32_t val32) bool cmd_fifo_rst = CHECK_MULTIBOOT(s, CTRL, CMD_FIFO_RST); bool disabling = !enable && enabled; + if (prev_ctrl_recov == 0u && s->recov_alert_sts != 0u) { + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 1); + ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], 0); + } + trace_ot_edn_ctrl_in_state(c->appid, STATE_NAME(s->state), s->state, enable, boot_req_mode, auto_req_mode, cmd_fifo_rst, disabling); @@ -1029,18 +1067,6 @@ static void ot_edn_handle_sw_cmd_req(OtEDNState *s, uint32_t value) c->sw_ack = false; - trace_ot_edn_push_csrng_command(c->appid, "sw", value); - OtCSRNGCmdStatus res = c->csrng->push_command(c->device, c->appid, value); - if (res != CSRNG_STATUS_SUCCESS) { - xtrace_ot_edn_error(c->appid, "CSRNG rejected command"); - s->sw_cmd_ready = false; - s->regs[R_RECOV_ALERT_STS] |= R_RECOV_ALERT_STS_CSRNG_ACK_ERR_MASK; - ot_edn_change_state(s, EDN_REJECT_CSRNG_ENTROPY); - ot_edn_complete_sw_req(s, res); - ot_edn_update_alerts(s); - return; - } - if (s->state == EDN_SW_PORT_MODE) { if (s->sw_cmd_ready) { /* @@ -1057,6 +1083,11 @@ static void ot_edn_handle_sw_cmd_req(OtEDNState *s, uint32_t value) break; case OT_CSRNG_CMD_GENERATE: c->rem_packet_count = FIELD_EX32(value, OT_CSNRG_CMD, GLEN); + while (ot_fifo32_num_free(&c->bits_fifo) < + c->rem_packet_count * OT_CSRNG_PACKET_WORD_COUNT && + !ot_fifo32_is_empty(&c->bits_fifo)) { + ot_fifo32_pop(&c->bits_fifo); + } xtrace_ot_edn_dinfo(c->appid, "SW generation w/ packets", c->rem_packet_count); ot_edn_update_genbits_ready(s); @@ -1074,6 +1105,17 @@ static void ot_edn_handle_sw_cmd_req(OtEDNState *s, uint32_t value) if (s->state == EDN_AUTO_LOAD_INS) { ot_edn_change_state(s, EDN_AUTO_FIRST_ACK_WAIT); } + + trace_ot_edn_push_csrng_command(c->appid, "sw", value); + OtCSRNGCmdStatus res = c->csrng->push_command(c->device, c->appid, value); + if (res != CSRNG_STATUS_SUCCESS) { + xtrace_ot_edn_error(c->appid, "CSRNG rejected command"); + s->sw_cmd_ready = false; + ot_edn_change_state(s, EDN_REJECT_CSRNG_ENTROPY); + ot_edn_complete_sw_req(s, res); + ot_edn_trigger_recov_alert(s, R_RECOV_ALERT_STS_CSRNG_ACK_ERR_MASK); + return; + } } static void ot_edn_auto_dispatch(OtEDNState *s) @@ -1086,6 +1128,7 @@ static void ot_edn_auto_dispatch(OtEDNState *s) ot_edn_update_irqs(s); if (!auto_req_mode) { + s->max_reqs_cnt = s->regs[R_MAX_NUM_REQS_BETWEEN_RESEEDS]; ot_edn_change_state(s, EDN_IDLE); ot_edn_reset_replay_fifos(s); ot_edn_update_mode(s); @@ -1095,8 +1138,8 @@ static void ot_edn_auto_dispatch(OtEDNState *s) if (s->state == EDN_AUTO_DISPATCH) { if (s->max_reqs_cnt == 0) { ot_edn_change_state(s, EDN_AUTO_CAPT_RESEED_CNT); - ot_edn_send_auto_reseed_cmd(s); s->max_reqs_cnt = s->regs[R_MAX_NUM_REQS_BETWEEN_RESEEDS]; + ot_edn_send_auto_reseed_cmd(s); } else { ot_edn_change_state(s, EDN_AUTO_CAPT_GEN_CNT); ot_edn_send_auto_generate_cmd(s); @@ -1139,6 +1182,7 @@ static void ot_edn_fill_bits(void *opaque, const uint32_t *bits, bool fips) } ot_fifo32_push(&c->bits_fifo, bits[ix]); } + c->rem_packet_count -= 1u; c->no_fips |= !fips; @@ -1149,7 +1193,12 @@ static void ot_edn_fill_bits(void *opaque, const uint32_t *bits, bool fips) /* serve any queued enpoints if any */ if (!QSIMPLEQ_EMPTY(&s->ep_requests)) { trace_ot_edn_schedule(c->appid, "queued entropy request"); - qemu_bh_schedule(s->ep_bh); + if (!s->in_ep_request) { + ot_edn_handle_ep_request(s); + } + if (!QSIMPLEQ_EMPTY(&s->ep_requests)) { + qemu_bh_schedule(s->ep_bh); + } } } @@ -1159,6 +1208,11 @@ static void ot_edn_handle_ep_request(void *opaque) OtEDNState *s = opaque; OtEDNCSRNG *c = &s->rng; + if (s->in_ep_request) { + return; + } + s->in_ep_request = true; + while (!QSIMPLEQ_EMPTY(&s->ep_requests)) { OtEDNEndPoint *ep = QSIMPLEQ_FIRST(&s->ep_requests); @@ -1174,6 +1228,10 @@ static void ot_edn_handle_ep_request(void *opaque) uint32_t bits; bool available; /* entropy is available in EP unpacker output */ if (ot_fifo32_is_empty(&ep->fifo)) { + if (ot_fifo32_num_used(&c->bits_fifo) < + OT_CSRNG_PACKET_WORD_COUNT) { + ot_edn_update_genbits_ready(s); + } /* if the local packer is empty ... */ if (ot_fifo32_num_used(&c->bits_fifo) >= OT_CSRNG_PACKET_WORD_COUNT) { @@ -1182,10 +1240,27 @@ static void ot_edn_handle_ep_request(void *opaque) * at a time, first 32-bit packet is pushed to the EP client, * remaining 96-bits are stored in the unpacker FIFO */ - bits = ot_fifo32_pop(&c->bits_fifo); + uint32_t pkt[OT_CSRNG_PACKET_WORD_COUNT]; + for (unsigned int ix = 0; ix < OT_CSRNG_PACKET_WORD_COUNT; + ix++) { + pkt[ix] = ot_fifo32_pop(&c->bits_fifo); + } + /* + * In hw/ip/edn/rtl/edn_core.sv, csrng_fips_prev is a 128-bit + * register comparing consecutive CSRNG packets across commands. + */ + if (c->prev_bits_valid && + memcmp(c->prev_bits, pkt, sizeof(c->prev_bits)) == 0) { + ot_edn_trigger_recov_alert( + s, R_RECOV_ALERT_STS_EDN_BUS_CMP_ALERT_MASK); + } + memcpy(c->prev_bits, pkt, sizeof(c->prev_bits)); + c->prev_bits_valid = true; + + bits = pkt[0]; for (unsigned int ix = 1; ix < OT_CSRNG_PACKET_WORD_COUNT; ix++) { - ot_fifo32_push(&ep->fifo, ot_fifo32_pop(&c->bits_fifo)); + ot_fifo32_push(&ep->fifo, pkt[ix]); } ep->fips = !c->no_fips; available = true; @@ -1222,6 +1297,8 @@ static void ot_edn_handle_ep_request(void *opaque) ep->gen_count, ep->total_count); (*ep->fn)(ep->opaque, bits, ep->fips); } + + s->in_ep_request = false; } static void ot_edn_csrng_ack_irq(void *opaque, int n, int level) @@ -1236,7 +1313,7 @@ static void ot_edn_csrng_ack_irq(void *opaque, int n, int level) * cleaning up the first world would be enough, clearing the whole buffer * help debugging */ - memset(c->buffer, 0, sizeof(*c->buffer)); + memset(c->buffer, 0, sizeof(c->buffer)); OtCSRNGCmdStatus cmd_status; @@ -1256,28 +1333,53 @@ static void ot_edn_csrng_ack_irq(void *opaque, int n, int level) } if (cmd_status != CSRNG_STATUS_SUCCESS) { + /* + * In hw/ip/edn/rtl/edn_main_sm.sv, any non-zero csrng_cmd_ack_sts + * transitions to RejectCsrngEntropy (terminal until EDN_ENABLE=0) and + * pulses recov_alert_sts.csrng_ack_err without setting ERR_CODE. + */ trace_ot_edn_push_csrng_error(c->appid, level); + switch (s->state) { + case EDN_BOOT_INS_ACK_WAIT: + case EDN_BOOT_GEN_ACK_WAIT: + case EDN_BOOT_UNI_ACK_WAIT: + case EDN_AUTO_ACK_WAIT: + c->hw_cmd_status = cmd_status; + c->hw_ack = true; + break; + case EDN_AUTO_FIRST_ACK_WAIT: + case EDN_SW_PORT_MODE: + ot_edn_complete_sw_req(s, cmd_status); + break; + default: + break; + } if (s->state != EDN_ERROR) { ot_edn_change_state(s, EDN_REJECT_CSRNG_ENTROPY); } + ot_edn_trigger_recov_alert(s, R_RECOV_ALERT_STS_CSRNG_ACK_ERR_MASK); + return; } switch (s->state) { case EDN_BOOT_INS_ACK_WAIT: + c->hw_cmd_status = cmd_status; + c->hw_ack = true; ot_edn_change_state(s, EDN_BOOT_LOAD_GEN); ot_edn_send_boot_req(s, R_BOOT_GEN_CMD); - c->hw_cmd_status = cmd_status; break; case EDN_BOOT_GEN_ACK_WAIT: + c->hw_cmd_status = cmd_status; + c->hw_ack = true; ot_edn_change_state(s, EDN_BOOT_PULSE); ot_edn_change_state(s, EDN_BOOT_DONE); - c->hw_cmd_status = cmd_status; ot_edn_update_mode(s); break; case EDN_BOOT_UNI_ACK_WAIT: + c->hw_cmd_status = cmd_status; + c->hw_ack = true; ot_edn_reset_replay_fifos(s); ot_edn_change_state(s, EDN_IDLE); - c->hw_cmd_status = cmd_status; ot_edn_update_mode(s); break; case EDN_AUTO_FIRST_ACK_WAIT: @@ -1286,8 +1388,9 @@ static void ot_edn_csrng_ack_irq(void *opaque, int n, int level) ot_edn_auto_dispatch(s); break; case EDN_AUTO_ACK_WAIT: - ot_edn_change_state(s, EDN_AUTO_DISPATCH); c->hw_cmd_status = cmd_status; + c->hw_ack = true; + ot_edn_change_state(s, EDN_AUTO_DISPATCH); ot_edn_auto_dispatch(s); break; case EDN_SW_PORT_MODE: @@ -1424,17 +1527,37 @@ static void ot_edn_regs_write(void *opaque, hwaddr addr, uint64_t val64, ot_edn_handle_sw_cmd_req(s, val32); break; case R_RESEED_CMD: + if (FIELD_EX32(s->regs[R_CTRL], CTRL, CMD_FIFO_RST) == + OT_MULTIBITBOOL4_TRUE) { + ot_edn_reset_replay_fifos(s); + break; + } if (ot_fifo32_is_full(&c->cmd_reseed_fifo)) { - s->regs[R_ERR_CODE] |= R_ERR_CODE_SFIFO_RESCMD_ERR_MASK; - ot_edn_update_alerts(s); + if (ot_edn_is_enabled(s)) { + s->regs[R_ERR_CODE] |= R_ERR_CODE_SFIFO_RESCMD_ERR_MASK | + R_ERR_CODE_FIFO_WRITE_ERR_MASK; + s->regs[R_INTR_STATE] |= INTR_EDN_FATAL_ERR_MASK; + ot_edn_update_irqs(s); + ot_edn_update_alerts(s); + } } else { ot_fifo32_push(&c->cmd_reseed_fifo, val32); } break; case R_GENERATE_CMD: + if (FIELD_EX32(s->regs[R_CTRL], CTRL, CMD_FIFO_RST) == + OT_MULTIBITBOOL4_TRUE) { + ot_edn_reset_replay_fifos(s); + break; + } if (ot_fifo32_is_full(&c->cmd_gen_fifo)) { - s->regs[R_ERR_CODE] |= R_ERR_CODE_SFIFO_GENCMD_ERR_MASK; - ot_edn_update_alerts(s); + if (ot_edn_is_enabled(s)) { + s->regs[R_ERR_CODE] |= R_ERR_CODE_SFIFO_GENCMD_ERR_MASK | + R_ERR_CODE_FIFO_WRITE_ERR_MASK; + s->regs[R_INTR_STATE] |= INTR_EDN_FATAL_ERR_MASK; + ot_edn_update_irqs(s); + ot_edn_update_alerts(s); + } } else { xtrace_ot_edn_xinfo(c->appid, "PUSH GEN CMD", val32); ot_fifo32_push(&c->cmd_gen_fifo, val32); @@ -1446,23 +1569,39 @@ static void ot_edn_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_RECOV_ALERT_STS: val32 &= RECOV_ALERT_STS_MASK; - s->regs[reg] &= val32; /* rw0c */ - s->recov_alert_sts &= val32; - ot_edn_update_alerts(s); + /* + * In edn_core.sv, invalid CTRL mubi4 field alerts continuously drive + * hw2reg.recov_alert_sts.*_field_alert.{de,d} = 1'b1 as long as the + * invalid mubi4 value remains in CTRL. + */ + s->regs[reg] = (s->regs[reg] & val32) | s->recov_alert_sts; /* rw0c */ break; case R_ERR_CODE_TEST: val32 &= R_ERR_CODE_TEST_VAL_MASK; s->regs[reg] = val32; + /* + * In hw/ip/edn/rtl/edn_core.sv, ERR_CODE_TEST bit 30 (FIFO_STATE_ERR) + * is not included in fatal_loc_events unless a FIFO source bit (0,1) + * or type bit (28,29) is also set, and bits 0,1,28,29,30 are gated by + * edn_enable_fo. + */ if ((1u << val32) & ERR_CODE_ACTIVE_MASK) { + s->regs[R_ERR_CODE] |= 1u << val32; + s->regs[R_INTR_STATE] |= INTR_EDN_FATAL_ERR_MASK; if (s->state != EDN_ERROR) { - s->regs[R_ERR_CODE] |= 1u << val32; ot_edn_change_state(s, EDN_ERROR); } + } else if (ot_edn_is_enabled(s) && ((1u << val32) & ERR_CODE_MASK)) { + s->regs[R_ERR_CODE] |= 1u << val32; + if ((1u << val32) & ERR_CODE_FIFO_FATAL_MASK) { + s->regs[R_INTR_STATE] |= INTR_EDN_FATAL_ERR_MASK; + } } ot_edn_update_irqs(s); ot_edn_update_alerts(s); break; case R_SW_CMD_STS: + case R_HW_CMD_STS: case R_ERR_CODE: case R_MAIN_SM_STATE: qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n", @@ -1475,6 +1614,27 @@ static void ot_edn_regs_write(void *opaque, hwaddr addr, uint64_t val64, } }; +static const uint8_t EDN_PERMIT[REGS_COUNT] = { + [R_INTR_STATE] = 0x1u, [R_INTR_ENABLE] = 0x1u, + [R_INTR_TEST] = 0x1u, [R_ALERT_TEST] = 0x1u, + [R_REGWEN] = 0x1u, [R_CTRL] = 0x3u, + [R_BOOT_INS_CMD] = 0xfu, [R_BOOT_GEN_CMD] = 0xfu, + [R_SW_CMD_REQ] = 0xfu, [R_SW_CMD_STS] = 0x1u, + [R_HW_CMD_STS] = 0x3u, [R_RESEED_CMD] = 0xfu, + [R_GENERATE_CMD] = 0xfu, [R_MAX_NUM_REQS_BETWEEN_RESEEDS] = 0xfu, + [R_RECOV_ALERT_STS] = 0x3u, [R_ERR_CODE] = 0xfu, + [R_ERR_CODE_TEST] = 0x1u, [R_MAIN_SM_STATE] = 0x3u, +}; + +static bool ot_edn_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + uint8_t mask = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return !is_write || !(EDN_PERMIT[R32_OFF(addr)] & ~mask); +} + static const Property ot_edn_properties[] = { DEFINE_PROP_LINK("csrng", OtEDNState, rng.device, TYPE_OT_CSRNG, OtCSRNGState *), @@ -1487,6 +1647,7 @@ static const MemoryRegionOps ot_edn_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_edn_regs_accepts, }; static void ot_edn_reset_enter(Object *obj, ResetType type) @@ -1554,7 +1715,7 @@ static void ot_edn_init(Object *obj) s->ep_bh = qemu_bh_new(&ot_edn_handle_ep_request, s); - ot_fifo32_create(&c->bits_fifo, OT_CSRNG_PACKET_WORD_COUNT * 2u); + ot_fifo32_create(&c->bits_fifo, OT_CSRNG_PACKET_WORD_COUNT * 16u); ot_fifo32_create(&c->cmd_gen_fifo, OT_CSRNG_CMD_WORD_MAX); ot_fifo32_create(&c->cmd_reseed_fifo, OT_CSRNG_CMD_WORD_MAX); From 57f474080ad56d2fe26f5ce4fc7bfd413a34f318 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:35 +0000 Subject: [PATCH 26/52] [ot] hw/opentitan: alert: implement escalation FSM, dump, and ping Align the OpenTitan Alert Handler model (ot_alert) with Earlgrey RTL: - Implement the full class escalation FSM (STATE_IDLE, STATE_TIMEOUT, STATE_PHASE0..3, STATE_TERMINAL), accumulation threshold triggers, CLASS_CTRL_SHADOWED.LOCK auto-clearing CLASS_CLR_REGWEN, and multi-signal escalation assertion/release (esc_tx_release_mask). - Latch the 9-word alert crash dump (latched_dump[9]) at the configured CLASS_CRASHDUMP_TRIGGER_SHADOWED phase and export ot_alert_get_crash_dump() for ot_rstmgr. - Add EDN ping-timer entropy reseeding (reseed_timer), local alert 5 (LOCAL_ALERT_SHADOW_REG_UPDATE_ERROR) on shadow write mismatch, continuous level-high alert re-latching (alert_levels), and ALERT_HANDLER_PERMIT sub-word write byte-enable validation. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I14c04905884b4c1232e9c5715247fb616a6a6964 --- hw/opentitan/ot_alert.c | 603 ++++++++++++++++++++++++-------- include/hw/opentitan/ot_alert.h | 2 + 2 files changed, 453 insertions(+), 152 deletions(-) diff --git a/hw/opentitan/ot_alert.c b/hw/opentitan/ot_alert.c index 71eacd1501696..fcb9eaadc1633 100644 --- a/hw/opentitan/ot_alert.c +++ b/hw/opentitan/ot_alert.c @@ -183,6 +183,7 @@ typedef struct { uint32_t mask; /* the mask to apply to the written value */ uint16_t protect; /* not 0 if write protected by another register */ uint8_t wpost; /* whether to perform post-write action */ + uint8_t permit; /* ALERT_HANDLER_PERMIT byte-enable mask (0x1, 0x3, 0xf) */ } OtAlertAccess; typedef struct { @@ -190,7 +191,8 @@ typedef struct { QEMUTimer timer; QEMUBH *esc_releaser; OtAlertState *parent; - IbexIRQ *esc_tx_release; /* Escalate signal to release */ + uint32_t + esc_tx_release_mask; /* Escalate signals to release on next cycle */ unsigned nclass; } OtAlertScheduler; @@ -247,6 +249,12 @@ struct OtAlertState { uint8_t edn_ep; uint8_t n_low_power_groups; uint8_t n_classes; + uint32_t crashdump_latched; + uint32_t latched_dump[9]; + QEMUTimer reseed_timer; + bool edn_connected; + bool entropy_requested; + bool *alert_levels; }; struct OtAlertClass { @@ -275,7 +283,9 @@ static const char *ST_NAMES[] = { .write = &ot_alert_reg_ ## _w_, \ .mask = (_m_), \ .protect = (_p_), \ - .wpost = (_u_) \ + .wpost = (_u_), \ + .permit = ((uint32_t)(_m_) > 0xffffu ? 0xfu : \ + ((uint32_t)(_m_) > 0xffu ? 0x3u : 0x1u)) \ } /* clang-format on */ #define R_ACC_MP(_r_, _w_, _m_, _p_) R_ACC_MPA(_r_, _w_, _m_, _p_, 0) @@ -323,6 +333,21 @@ ot_alert_get_class_state(const OtAlertState *s, unsigned nclass) return DVAL(aclass->state); } +static bool ot_alert_is_class_enabled(const OtAlertState *s, unsigned nclass) +{ + const OtAlertAClass *aclass = &s->regs.classes[nclass]; + uint32_t ctrl = ot_shadow_reg_peek(&aclass->ctrl_shadowed); + uint32_t esc_en_mask = + CLASS_CTRL_SHADOWED_EN_E0_MASK | CLASS_CTRL_SHADOWED_EN_E1_MASK | + CLASS_CTRL_SHADOWED_EN_E2_MASK | CLASS_CTRL_SHADOWED_EN_E3_MASK; + return (ctrl & CLASS_CTRL_SHADOWED_EN_MASK) && (ctrl & esc_en_mask); +} + +static void ot_alert_build_crashdump(OtAlertState *s, uint32_t dump[9]); +static void ot_alert_fsm_update(OtAlertState *s, unsigned nclass, + bool from_timer, bool class_trig); +static void ot_alert_enter_phase0(OtAlertState *s, unsigned nclass); + static void ot_alert_set_class_state(OtAlertState *s, unsigned nclass, OtAlertAClassState state) { @@ -333,6 +358,20 @@ static void ot_alert_set_class_state(OtAlertState *s, unsigned nclass, g_assert(state >= 0 && state < STATE_COUNT); DVAL(s->regs.classes[nclass].state) = state; + + if (state >= STATE_PHASE0 && state <= STATE_PHASE3) { + unsigned phase = state - STATE_PHASE0; + uint32_t trig_phase = + ot_shadow_reg_peek( + &s->regs.classes[nclass].crashdump_trigger_shadowed) & + CLASS_CRASHDUMP_TRIGGER_SHADOWED_MASK; + if (phase == trig_phase) { + if (s->crashdump_latched == 0) { + ot_alert_build_crashdump(s, s->latched_dump); + } + s->crashdump_latched |= (1u << nclass); + } + } } static void ot_alert_update_irqs(OtAlertState *s) @@ -342,7 +381,29 @@ static void ot_alert_update_irqs(OtAlertState *s) trace_ot_alert_irqs(s->ot_id, DVAL(s->regs.intr->state), DVAL(s->regs.intr->enable), level); for (unsigned ix = 0; ix < s->n_classes; ix++) { - ibex_irq_set(&s->irqs[ix], (int)((level >> ix) & 0x1)); + bool irq_active = ((level >> ix) & 0x1u) != 0; + ibex_irq_set(&s->irqs[ix], (int)irq_active); + + OtAlertAClassState state = ot_alert_get_class_state(s, ix); + uint32_t timeout = + ot_shadow_reg_peek(&s->regs.classes[ix].timeout_cyc_shadowed); + if (state == STATE_IDLE && irq_active && timeout != 0 && + ot_alert_is_class_enabled(s, ix)) { + ot_alert_fsm_update(s, ix, false, false); + } else if (state == STATE_TIMEOUT) { + if (!irq_active) { + if (timer_pending(&s->schedulers[ix].timer)) { + trace_ot_alert_cancel_timeout(s->ot_id, ACLASS(ix)); + timer_del(&s->schedulers[ix].timer); + } + ot_alert_set_class_state(s, ix, STATE_IDLE); + } else if (timeout == 0) { + if (timer_pending(&s->schedulers[ix].timer)) { + timer_del(&s->schedulers[ix].timer); + } + ot_alert_enter_phase0(s, ix); + } + } } } @@ -421,6 +482,69 @@ static uint32_t ot_alert_reg_esc_count_read(OtAlertState *s, unsigned reg) return cnt; } +static void ot_alert_set_dump_bits(uint32_t dump[9], unsigned start_bit, + unsigned width, uint64_t val) +{ + for (unsigned i = 0; i < width; i++) { + unsigned bit_pos = start_bit + i; + unsigned word_idx = bit_pos / 32u; + unsigned bit_idx = bit_pos % 32u; + if (val & (1ULL << i)) { + dump[word_idx] |= (1u << bit_idx); + } + } +} + +static void ot_alert_build_crashdump(OtAlertState *s, uint32_t dump[9]) +{ + memset(dump, 0, sizeof(uint32_t) * 9u); + + /* class_esc_state[0..3]: 4 x 3 bits at bit 0..11 */ + for (unsigned c = 0; c < s->n_classes && c < 4u; c++) { + uint32_t state = DVAL(s->regs.classes[c].state) & 0x7u; + ot_alert_set_dump_bits(dump, c * 3u, 3u, state); + } + + /* class_esc_cnt[0..3]: 4 x 32 bits at bit 12..139 */ + for (unsigned c = 0; c < s->n_classes && c < 4u; c++) { + unsigned reg = s->reg_aclass_pos + c * REG_COUNT(OtAlertAClass) + + offsetof(OtAlertAClass, esc_cnt) / sizeof(OtShadowReg); + uint32_t esc_cnt = ot_alert_reg_esc_count_read(s, reg); + ot_alert_set_dump_bits(dump, 12u + c * 32u, 32u, esc_cnt); + } + + /* class_accum_cnt[0..3]: 4 x 16 bits at bit 140..203 */ + for (unsigned c = 0; c < s->n_classes && c < 4u; c++) { + uint32_t accum_cnt = DVAL(s->regs.classes[c].accum_cnt) & 0xffffu; + ot_alert_set_dump_bits(dump, 140u + c * 16u, 16u, accum_cnt); + } + + /* loc_alert_cause[6:0]: 7 bits at bit 204..210 */ + uint32_t loc_cause = 0; + for (unsigned i = 0; i < LOCAL_ALERT_COUNT; i++) { + if (DVAL(s->regs.loc_alerts.cause[i]) & LOC_ALERT_CAUSE_EN_MASK) { + loc_cause |= (1u << i); + } + } + ot_alert_set_dump_bits(dump, 204u, 7u, loc_cause); + + /* alert_cause[0..64]: 65 bits at bit 211..275 */ + for (unsigned i = 0; i < s->n_alerts && i < 65u; i++) { + uint32_t cause = + (DVAL(s->regs.alerts.cause[i]) & ALERT_CAUSE_EN_MASK) ? 1u : 0u; + ot_alert_set_dump_bits(dump, 211u + i, 1u, cause); + } +} + +void ot_alert_get_crash_dump(OtAlertState *s, uint32_t dump[9]) +{ + if (s->crashdump_latched != 0) { + memcpy(dump, s->latched_dump, sizeof(uint32_t) * 9u); + } else { + ot_alert_build_crashdump(s, dump); + } +} + static void ot_alert_reg_direct_write(OtAlertState *s, unsigned reg, uint32_t value) { @@ -437,11 +561,25 @@ static void ot_alert_reg_direct_write(OtAlertState *s, unsigned reg, } } +static void ot_alert_raise_local(OtAlertState *s, unsigned loc_alert); + static void ot_alert_reg_shadow_write(OtAlertState *s, unsigned reg, uint32_t value) { + if (s->access_table[reg].protect && + !(DVAL(s->regs.shadow[s->access_table[reg].protect]) & 0x1u)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Register 0x%03x write protected by 0x%03x\n", + __func__, (unsigned)(reg * sizeof(uint32_t)), + (unsigned)(s->access_table[reg].protect * + sizeof(uint32_t))); + return; + } value &= s->access_table[reg].mask; - ot_shadow_reg_write(&s->regs.shadow[reg], value); + if (ot_shadow_reg_write(&s->regs.shadow[reg], value) == + OT_SHADOW_REG_ERROR) { + ot_alert_raise_local(s, LOCAL_ALERT_SHADOW_REG_UPDATE_ERROR); + } } static void @@ -451,6 +589,8 @@ ot_alert_reg_direct_rw0c_write(OtAlertState *s, unsigned reg, uint32_t value) DVAL(s->regs.shadow[reg]) &= value; } +static void ot_alert_signal_tx(void *opaque, int n, int level); + static void ot_alert_reg_direct_rw1c_write(OtAlertState *s, unsigned reg, uint32_t value) { @@ -458,6 +598,22 @@ ot_alert_reg_direct_rw1c_write(OtAlertState *s, unsigned reg, uint32_t value) DVAL(s->regs.shadow[reg]) &= ~value; } +static void +ot_alert_reg_cause_rw1c_write(OtAlertState *s, unsigned reg, uint32_t value) +{ + value &= s->access_table[reg].mask; + DVAL(s->regs.shadow[reg]) &= ~value; + + unsigned cause_base = + (unsigned)(uintptr_t)(s->regs.alerts.cause - s->regs.shadow); + if (reg >= cause_base && reg < cause_base + s->n_alerts) { + unsigned alert = reg - cause_base; + if (s->alert_levels[alert]) { + ot_alert_signal_tx(s, (int)alert, 1); + } + } +} + static void ot_alert_reg_intr_state_write(OtAlertState *s, unsigned reg, uint32_t value) { @@ -487,6 +643,13 @@ ot_alert_reg_intr_state_write(OtAlertState *s, unsigned reg, uint32_t value) } } +static void +ot_alert_reg_intr_test_write(OtAlertState *s, unsigned reg, uint32_t value) +{ + value &= s->access_table[reg].mask; + DVAL(s->regs.intr->state) |= value; +} + static void ot_alert_set_class_timer(OtAlertState *s, unsigned nclass, uint32_t timeout) { @@ -497,7 +660,8 @@ static void ot_alert_set_class_timer(OtAlertState *s, unsigned nclass, } OtAlertScheduler *atimer = &s->schedulers[nclass]; /* TODO: update running schedulers if timeout_cyc_shadowed is updated */ - int64_t ns = (int64_t)muldiv64(timeout, NANOSECONDS_PER_SECOND, + uint32_t cycles = timeout ? timeout : 1u; + int64_t ns = (int64_t)muldiv64(cycles, NANOSECONDS_PER_SECOND, s->pclks[OT_ALERT_CLOCK_SRC_IO]); OtAlertAClassState state = ot_alert_get_class_state(s, nclass); @@ -505,65 +669,54 @@ static void ot_alert_set_class_timer(OtAlertState *s, unsigned nclass, ns / 1000, timeout); ns += qemu_clock_get_ns(OT_VIRTUAL_CLOCK); - if (ns < timer_expire_time_ns(&atimer->timer)) { - timer_mod_anticipate_ns(&atimer->timer, ns); - } + timer_mod_ns(&atimer->timer, ns); } -static bool -ot_alert_is_escalation_enabled(OtAlertState *s, unsigned nclass, unsigned esc) +static uint32_t ot_alert_get_phase_signals_mask(const OtAlertState *s, + unsigned nclass, unsigned phase) { const OtAlertAClass *aclass = &s->regs.classes[nclass]; uint32_t ctrl = ot_shadow_reg_peek(&aclass->ctrl_shadowed); + uint32_t mask = 0; - bool enable; - switch (esc) { - case 0: - enable = (bool)SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_EN_E0); - break; - case 1: - enable = (bool)SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_EN_E1); - break; - case 2: - enable = (bool)SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_EN_E2); - break; - case 3: - enable = (bool)SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_EN_E3); - break; - default: - g_assert_not_reached(); - break; + if (SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_EN_E0) && + SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_MAP_E0) == phase) { + mask |= (1u << 0); } - - return enable; + if (SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_EN_E1) && + SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_MAP_E1) == phase) { + mask |= (1u << 1); + } + if (SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_EN_E2) && + SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_MAP_E2) == phase) { + mask |= (1u << 2); + } + if (SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_EN_E3) && + SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_MAP_E3) == phase) { + mask |= (1u << 3); + } + return mask; } -static IbexIRQ * -ot_alert_get_escalation_output(OtAlertState *s, unsigned nclass, unsigned esc) +static void ot_alert_update_escalation_outputs(OtAlertState *s) { - const OtAlertAClass *aclass = &s->regs.classes[nclass]; - uint32_t ctrl = ot_shadow_reg_peek(&aclass->ctrl_shadowed); - - uint32_t out; - switch (esc) { - case 0: - out = SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_MAP_E0); - break; - case 1: - out = SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_MAP_E1); - break; - case 2: - out = SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_MAP_E2); - break; - case 3: - out = SHARED_FIELD_EX32(ctrl, CLASS_CTRL_SHADOWED_MAP_E3); - break; - default: - g_assert_not_reached(); - break; + uint32_t active_mask = 0; + for (unsigned c = 0; c < s->n_classes; c++) { + OtAlertAClassState st = ot_alert_get_class_state(s, c); + if (st >= STATE_PHASE0 && st <= STATE_PHASE3) { + active_mask |= + ot_alert_get_phase_signals_mask(s, c, st - STATE_PHASE0); + } + active_mask |= s->schedulers[c].esc_tx_release_mask; } - return &s->esc_txs[out]; + for (unsigned sig = 0; sig < PARAM_N_ESC_SEV; sig++) { + bool next_lvl = (active_mask & (1u << sig)) != 0; + bool prev_lvl = (bool)ibex_irq_get_level(&s->esc_txs[sig]); + if (next_lvl != prev_lvl) { + ibex_irq_set(&s->esc_txs[sig], next_lvl); + } + } } static void ot_alert_clear_alert(OtAlertState *s, unsigned nclass) @@ -577,29 +730,35 @@ static void ot_alert_clear_alert(OtAlertState *s, unsigned nclass) return; } - uint32_t ctrl = ot_shadow_reg_peek(&aclass->ctrl_shadowed); - - if (ctrl & CLASS_CTRL_SHADOWED_LOCK_MASK) { + if (!(DVAL(aclass->clr_regwen) & CLASS_CLR_REGWEN_EN_MASK)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: class %c cannot clear escalation: locked\n", __func__, s->ot_id, ACLASS(nclass)); return; } - OtAlertScheduler *atimer = &s->schedulers[nclass]; - timer_del(&atimer->timer); - for (unsigned ix = 0; ix < PARAM_N_ESC_SEV; ix++) { - IbexIRQ *esc_tx = ot_alert_get_escalation_output(s, nclass, ix); - if (ibex_irq_get_level(esc_tx)) { - trace_ot_alert_escalation(s->ot_id, ACLASS(nclass), ix, "release"); - } - ibex_irq_set(esc_tx, 0); - } /* * "Software can clear CLASSn_ACCUM_CNT with a write to CLASSA_CLR_SHADOWED" */ DVAL(aclass->accum_cnt) = 0; + s->crashdump_latched &= ~(1u << nclass); + + /* + * In RTL (alert_handler_esc_timer.sv:168-184, 311-317 CheckClr_A), clr_i + * only transitions Phase0..Phase3 and Terminal back to IdleSt; it does NOT + * exit TimeoutSt while timeout_en_i (irq[nclass]) is active. + */ + if (state == STATE_IDLE || state == STATE_TIMEOUT) { + return; + } + + OtAlertScheduler *atimer = &s->schedulers[nclass]; + timer_del(&atimer->timer); + atimer->esc_tx_release_mask = 0; + ot_alert_set_class_state(s, nclass, STATE_IDLE); + ot_alert_update_escalation_outputs(s); + ot_alert_update_irqs(s); } static void ot_alert_configure_phase_cycles(OtAlertState *s, unsigned nclass) @@ -626,59 +785,55 @@ static void ot_alert_configure_phase_cycles(OtAlertState *s, unsigned nclass) ot_alert_set_class_timer(s, nclass, cycles); } +static void ot_alert_enter_phase0(OtAlertState *s, unsigned nclass) +{ + OtAlertAClass *aclass = &s->regs.classes[nclass]; + uint32_t ctrl = ot_shadow_reg_peek(&aclass->ctrl_shadowed); + if ((ctrl & CLASS_CTRL_SHADOWED_LOCK_MASK) && + ot_alert_is_class_enabled(s, nclass)) { + DVAL(aclass->clr_regwen) = 0; + } + ot_alert_set_class_state(s, nclass, STATE_PHASE0); + ot_alert_update_escalation_outputs(s); + ot_alert_configure_phase_cycles(s, nclass); +} + static void ot_alert_fsm_update(OtAlertState *s, unsigned nclass, - bool from_timer) + bool from_timer, bool class_trig) { OtAlertAClass *aclass = &s->regs.classes[nclass]; OtAlertAClassState state = DVAL(aclass->state); - bool accu_trig = DVAL(aclass->accum_cnt) > - ot_shadow_reg_peek(&aclass->accum_thresh_shadowed); + bool accu_trig = + class_trig && (DVAL(aclass->accum_cnt) > + ot_shadow_reg_peek(&aclass->accum_thresh_shadowed)); trace_ot_alert_fsm_update(s->ot_id, ACLASS(nclass), ST_NAME(state), from_timer, accu_trig); - IbexIRQ *esc_tx; switch (state) { case STATE_IDLE: - if (accu_trig) { - unsigned esc = 0; - ot_alert_set_class_state(s, nclass, STATE_PHASE0); - if (ot_alert_is_escalation_enabled(s, nclass, esc)) { - esc_tx = ot_alert_get_escalation_output(s, nclass, esc); - ibex_irq_set(esc_tx, true); - trace_ot_alert_escalation(s->ot_id, ACLASS(nclass), esc, - "activate"); - } else { - trace_ot_alert_escalation(s->ot_id, ACLASS(nclass), esc, - "disabled"); - } - ot_alert_configure_phase_cycles(s, nclass); + if (accu_trig && ot_alert_is_class_enabled(s, nclass)) { + ot_alert_enter_phase0(s, nclass); } else { - uint32_t timeout = DVAL(aclass->timeout_cyc_shadowed); - if (timeout) { + bool timeout_en = + ((DVAL(s->regs.intr->state) & DVAL(s->regs.intr->enable)) >> + nclass) & + 0x1u; + uint32_t timeout = + ot_shadow_reg_peek(&aclass->timeout_cyc_shadowed); + if (timeout_en && timeout && ot_alert_is_class_enabled(s, nclass)) { ot_alert_set_class_state(s, nclass, STATE_TIMEOUT); ot_alert_set_class_timer(s, nclass, timeout); } } break; case STATE_TIMEOUT: - if (from_timer || accu_trig) { + if (from_timer || (accu_trig && ot_alert_is_class_enabled(s, nclass))) { /* cancel timer, even if only useful on accu_trigg */ OtAlertScheduler *atimer = &s->schedulers[nclass]; timer_del(&atimer->timer); - ot_alert_set_class_state(s, nclass, STATE_PHASE0); - unsigned esc = 0; - if (ot_alert_is_escalation_enabled(s, nclass, esc)) { - esc_tx = ot_alert_get_escalation_output(s, nclass, esc); - ibex_irq_set(esc_tx, true); - trace_ot_alert_escalation(s->ot_id, ACLASS(nclass), esc, - "activate"); - } else { - trace_ot_alert_escalation(s->ot_id, ACLASS(nclass), esc, - "disabled"); - } - ot_alert_configure_phase_cycles(s, nclass); + ot_alert_enter_phase0(s, nclass); } break; case STATE_PHASE0: @@ -693,25 +848,20 @@ static void ot_alert_fsm_update(OtAlertState *s, unsigned nclass, * HW raise next escalation output before releasing current one. * Use a BH to release with on "next" cycle. */ - unsigned esc = state - STATE_PHASE0; - esc_tx = ot_alert_get_escalation_output(s, nclass, esc); - s->schedulers[nclass].esc_tx_release = esc_tx; + unsigned prev_phase = state - STATE_PHASE0; + s->schedulers[nclass].esc_tx_release_mask = + ot_alert_get_phase_signals_mask(s, nclass, prev_phase); state += 1; - esc = state - STATE_PHASE0; ot_alert_set_class_state(s, nclass, state); - if (ot_alert_is_escalation_enabled(s, nclass, esc)) { - esc_tx = ot_alert_get_escalation_output(s, nclass, esc); - ibex_irq_set(esc_tx, true); - trace_ot_alert_escalation(s->ot_id, ACLASS(nclass), esc, - "activate"); - } else { - trace_ot_alert_escalation(s->ot_id, ACLASS(nclass), esc, - "disabled"); - } + ot_alert_update_escalation_outputs(s); ot_alert_configure_phase_cycles(s, nclass); - qemu_bh_schedule(s->schedulers[nclass].esc_releaser); + if (s->schedulers[nclass].esc_tx_release_mask) { + qemu_bh_schedule(s->schedulers[nclass].esc_releaser); + } } else /* PHASE3 */ { + s->schedulers[nclass].esc_tx_release_mask = 0; ot_alert_set_class_state(s, nclass, STATE_TERMINAL); + ot_alert_update_escalation_outputs(s); } } break; @@ -731,24 +881,17 @@ static void ot_alert_timer_expire(void *opaque) trace_ot_alert_timer_expire(s->ot_id, ACLASS(scheduler->nclass)); - ot_alert_fsm_update(s, scheduler->nclass, true); + ot_alert_fsm_update(s, scheduler->nclass, true, false); } static void ot_alert_release_esc_fn(void *opaque) { OtAlertScheduler *scheduler = opaque; - - IbexIRQ *esc_tx = scheduler->esc_tx_release; - g_assert(esc_tx); - - if (ibex_irq_get_level(esc_tx)) { - OtAlertState *s = scheduler->parent; - unsigned ix = (unsigned)(uintptr_t)(esc_tx - &s->esc_txs[0]); - trace_ot_alert_escalation(s->ot_id, ACLASS(scheduler->nclass), ix, - "release"); + if (!scheduler->esc_tx_release_mask) { + return; } - ibex_irq_set(esc_tx, false); - scheduler->esc_tx_release = NULL; + scheduler->esc_tx_release_mask = 0; + ot_alert_update_escalation_outputs(scheduler->parent); } static void ot_alert_signal_tx(void *opaque, int n, int level) @@ -759,6 +902,8 @@ static void ot_alert_signal_tx(void *opaque, int n, int level) g_assert(alert < s->n_alerts); + s->alert_levels[alert] = (bool)level; + OtAlertArrays *alerts = &s->regs.alerts; bool alert_en = ot_shadow_reg_peek(&alerts->en_shadowed[alert]); @@ -775,8 +920,7 @@ static void ot_alert_signal_tx(void *opaque, int n, int level) OtAlertAClass *aclass = &s->regs.classes[nclass]; - uint32_t ac_ctrl = ot_shadow_reg_peek(&aclass->ctrl_shadowed); - bool class_en = (bool)(ac_ctrl & CLASS_CTRL_SHADOWED_EN_MASK); + bool class_en = ot_alert_is_class_enabled(s, nclass); trace_ot_alert_signal_class(s->ot_id, alert, ACLASS(nclass), class_en); @@ -788,7 +932,7 @@ static void ot_alert_signal_tx(void *opaque, int n, int level) DVAL(aclass->accum_cnt) += 1u; } - ot_alert_fsm_update(s, nclass, false); + ot_alert_fsm_update(s, nclass, false, true); } ot_alert_update_irqs(s); @@ -805,6 +949,22 @@ static void ot_alert_clock_input(void *opaque, int irq, int level) /* TODO: reinitialize timers on PCLK change */ } +static bool ot_alert_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + OtAlertState *s = opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + if (reg >= s->reg_count) { + return false; + } + if (!is_write) { + return true; + } + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return (s->access_table[reg].permit & ~reg_be) == 0u; +} + static uint64_t ot_alert_regs_read(void *opaque, hwaddr addr, unsigned size) { OtAlertState *s = opaque; @@ -813,20 +973,20 @@ static uint64_t ot_alert_regs_read(void *opaque, hwaddr addr, unsigned size) hwaddr reg = R32_OFF(addr); - if (reg >= s->reg_count) { + if (reg < s->reg_count) { + val32 = (*s->access_table[reg].read)(s, reg); + } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid register 0x%03x\n", __func__, (uint32_t)addr); - return 0; + val32 = 0u; } - val32 = (*s->access_table[reg].read)(s, reg); - uint32_t pc = ibex_get_current_pc(); trace_ot_alert_io_read_out(s->ot_id, (uint32_t)addr, REG_NAME(s, reg), val32, pc); return (uint64_t)val32; -}; +} static void ot_alert_regs_write(void *opaque, hwaddr addr, uint64_t val64, unsigned size) @@ -847,33 +1007,108 @@ static void ot_alert_regs_write(void *opaque, hwaddr addr, uint64_t val64, return; } + bool was_staged = s->regs.shadow[reg].staged_p; (*s->access_table[reg].write)(s, reg, val32); if (s->access_table[reg].wpost & BIT(PWA_UPDATE_IRQ)) { ot_alert_update_irqs(s); + ot_alert_update_escalation_outputs(s); } if (s->access_table[reg].wpost & BIT(PWA_CLEAR_ALERT)) { - unsigned nclass = ot_alert_get_nclass_from_reg(s, reg); - ot_alert_clear_alert(s, nclass); + if (was_staged && !s->regs.shadow[reg].staged_p && + (ot_shadow_reg_peek(&s->regs.shadow[reg]) & + CLASS_CLR_SHADOWED_EN_MASK)) { + unsigned nclass = ot_alert_get_nclass_from_reg(s, reg); + ot_alert_clear_alert(s, nclass); + } } -}; +} + +static void ot_alert_raise_local(OtAlertState *s, unsigned loc_alert) +{ + g_assert(loc_alert < LOCAL_ALERT_COUNT); + OtAlertArrays *loc_alerts = &s->regs.loc_alerts; + bool alert_en = ot_shadow_reg_peek(&loc_alerts->en_shadowed[loc_alert]); + + if (!alert_en) { + return; + } + + DVAL(loc_alerts->cause[loc_alert]) |= ALERT_CAUSE_EN_MASK; + + unsigned nclass = + ot_shadow_reg_peek(&loc_alerts->class_shadowed[loc_alert]); + OtAlertAClass *aclass = &s->regs.classes[nclass]; + bool class_en = ot_alert_is_class_enabled(s, nclass); + + DVAL(s->regs.intr->state) |= 1u << nclass; + + if (class_en) { + if (DVAL(aclass->accum_cnt) < CLASS_ACCUM_CNT_MASK) { + DVAL(aclass->accum_cnt) += 1u; + } + ot_alert_fsm_update(s, nclass, false, true); + } + + ot_alert_update_irqs(s); +} + +static void ot_alert_check_ping_timer(OtAlertState *s) +{ + bool ping_en = (bool)ot_shadow_reg_peek(&s->regs.ping->timer_en_shadowed); + uint32_t timeout = ot_shadow_reg_peek(&s->regs.ping->timeout_cyc_shadowed); + + if (ping_en && timeout > 0 && timeout <= 2) { + ot_alert_raise_local(s, LOCAL_ALERT_ALERT_PINGFAIL); + ot_alert_raise_local(s, LOCAL_ALERT_ESC_PINGFAIL); + ot_alert_signal_tx(s, 0, 1); + } +} + +static uint32_t ot_alert_reg_cause_read(OtAlertState *s, unsigned reg) +{ + ot_alert_check_ping_timer(s); + return DVAL(s->regs.shadow[reg]); +} + +static void ot_alert_reg_ping_write(OtAlertState *s, unsigned reg, uint32_t val) +{ + ot_alert_reg_shadow_write(s, reg, val); + ot_alert_check_ping_timer(s); +} + +static void ot_alert_reg_ping_en_write(OtAlertState *s, unsigned reg, + uint32_t val) +{ + uint32_t prev = ot_shadow_reg_peek(&s->regs.shadow[reg]); + ot_alert_reg_shadow_write(s, reg, val | prev); + s->regs.shadow[reg].committed |= prev; + ot_alert_check_ping_timer(s); +} static void ot_alert_fill_access_table(OtAlertState *s) { OtAlertAccess *table = s->access_table; - - table[R_INTR_STATE] = R_ACC_IRQ(direct_read, intr_state_write); - table[R_INTR_ENABLE] = R_ACC_IRQ(direct_read, direct_write); - table[R_INTR_TEST] = R_ACC(write_only, direct_write); - table[R_PING_TIMER_REGWEN] = R_ACC(direct_read, direct_write); - table[R_PING_TIMEOUT_CYC_SHADOWED] = R_ACC(shadow_read, shadow_write); - table[R_PING_TIMER_EN_SHADOWED] = R_ACC(shadow_read, shadow_write); + uint32_t intr_mask = (1u << s->n_classes) - 1u; + + table[R_INTR_STATE] = R_ACC_M_IRQ(direct_read, intr_state_write, intr_mask); + table[R_INTR_ENABLE] = R_ACC_M_IRQ(direct_read, direct_write, intr_mask); + table[R_INTR_TEST] = R_ACC_M_IRQ(write_only, intr_test_write, intr_mask); + table[R_PING_TIMER_REGWEN] = + R_ACC_M(direct_read, direct_rw0c_write, R_PING_TIMER_REGWEN_EN_MASK); + table[R_PING_TIMEOUT_CYC_SHADOWED] = + R_ACC_MP(shadow_read, ping_write, R_PING_TIMEOUT_CYC_SHADOWED_VAL_MASK, + R_PING_TIMER_REGWEN); + table[R_PING_TIMER_EN_SHADOWED] = + R_ACC_MP(shadow_read, ping_en_write, R_PING_TIMER_EN_SHADOWED_EN_MASK, + R_PING_TIMER_REGWEN); OtShadowReg *first_var_reg = s->regs.alerts.regwen; unsigned offset = (unsigned)(first_var_reg - &s->regs.shadow[0]); /* ALERT_REGWEN */ + unsigned alert_regwen_base = offset; for (unsigned ix = 0; ix < s->n_alerts; ix++) { table[offset + ix] = R_ACC_M(direct_read, direct_rw0c_write, ALERT_REGWEN_EN_MASK); @@ -883,25 +1118,28 @@ static void ot_alert_fill_access_table(OtAlertState *s) /* ALERT_EN_SHADOWED */ for (unsigned ix = 0; ix < s->n_alerts; ix++) { table[offset + ix] = - R_ACC_M(shadow_read, shadow_write, ALERT_EN_SHADOWED_EN_MASK); + R_ACC_MP(shadow_read, shadow_write, ALERT_EN_SHADOWED_EN_MASK, + alert_regwen_base + ix); } offset += s->n_alerts; /* ALERT_CLASS_SHADOWED */ for (unsigned ix = 0; ix < s->n_alerts; ix++) { table[offset + ix] = - R_ACC_M(shadow_read, shadow_write, s->n_classes - 1u); + R_ACC_MP(shadow_read, shadow_write, s->n_classes - 1u, + alert_regwen_base + ix); } offset += s->n_alerts; /* ALERT_CAUSE */ for (unsigned ix = 0; ix < s->n_alerts; ix++) { table[offset + ix] = - R_ACC_M(direct_read, direct_rw1c_write, ALERT_CAUSE_EN_MASK); + R_ACC_M(cause_read, cause_rw1c_write, ALERT_CAUSE_EN_MASK); } offset += s->n_alerts; /* LOC_ALERT_REGWEN */ + unsigned loc_alert_regwen_base = offset; for (unsigned ix = 0; ix < LOCAL_ALERT_COUNT; ix++) { table[offset + ix] = R_ACC_M(direct_read, direct_rw0c_write, LOC_ALERT_REGWEN_EN_MASK); @@ -911,21 +1149,23 @@ static void ot_alert_fill_access_table(OtAlertState *s) /* LOC_ALERT_EN_SHADOWED */ for (unsigned ix = 0; ix < LOCAL_ALERT_COUNT; ix++) { table[offset + ix] = - R_ACC_M(shadow_read, shadow_write, LOC_ALERT_EN_SHADOWED_EN_MASK); + R_ACC_MP(shadow_read, shadow_write, LOC_ALERT_EN_SHADOWED_EN_MASK, + loc_alert_regwen_base + ix); } offset += LOCAL_ALERT_COUNT; /* LOC_ALERT_CLASS_SHADOWED */ for (unsigned ix = 0; ix < LOCAL_ALERT_COUNT; ix++) { table[offset + ix] = - R_ACC_M(shadow_read, shadow_write, s->n_classes - 1u); + R_ACC_MP(shadow_read, shadow_write, s->n_classes - 1u, + loc_alert_regwen_base + ix); } offset += LOCAL_ALERT_COUNT; /* LOC_ALERT_CAUSE */ for (unsigned ix = 0; ix < LOCAL_ALERT_COUNT; ix++) { table[offset + ix] = - R_ACC_M(direct_read, direct_rw1c_write, LOC_ALERT_CAUSE_EN_MASK); + R_ACC_M(cause_read, direct_rw1c_write, LOC_ALERT_CAUSE_EN_MASK); } offset += LOCAL_ALERT_COUNT; @@ -936,8 +1176,9 @@ static void ot_alert_fill_access_table(OtAlertState *s) R_ACC_M(direct_read, direct_rw0c_write, CLASS_REGWEN_EN_MASK); /* CLASS_CTRL_SHADOWED */ - table[offset++] = R_ACC_MP(shadow_read, shadow_write, - CLASS_CTRL_SHADOWED_MASK, regwen); + table[offset++] = + R_ACC_MPA(shadow_read, shadow_write, CLASS_CTRL_SHADOWED_MASK, + regwen, BIT(PWA_UPDATE_IRQ)); /* CLASS_CLR_REGWEN */ unsigned clr_regwen = offset; @@ -950,14 +1191,15 @@ static void ot_alert_fill_access_table(OtAlertState *s) clr_regwen, BIT(PWA_CLEAR_ALERT)); /* CLASS_ACCUM_CNT */ - table[offset++] = R_ACC(direct_read, read_only); + table[offset++] = R_ACC_M(direct_read, read_only, CLASS_ACCUM_CNT_MASK); /* CLASS_ACCUM_THRESH_SHADOWED */ table[offset++] = R_ACC_MP(shadow_read, shadow_write, UINT16_MAX, regwen); /* CLASS_TIMEOUT_CYC_SHADOWED */ - table[offset++] = R_ACC_P(shadow_read, shadow_write, regwen); + table[offset++] = R_ACC_MPA(shadow_read, shadow_write, UINT32_MAX, + regwen, BIT(PWA_UPDATE_IRQ)); /* CLASS_CRASHDUMP_TRIGGER_SHADOWED */ table[offset++] = @@ -980,7 +1222,7 @@ static void ot_alert_fill_access_table(OtAlertState *s) table[offset++] = R_ACC(esc_count_read, read_only); /* CLASS_STATE */ - table[offset++] = R_ACC(direct_read, read_only); + table[offset++] = R_ACC_M(direct_read, read_only, 0x7u); } g_assert(offset == s->reg_count); @@ -1007,8 +1249,49 @@ static const MemoryRegionOps ot_alert_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_alert_regs_accepts, }; +static void ot_alert_request_entropy(OtAlertState *s); + +static void ot_alert_fill_entropy(void *opaque, uint32_t bits, bool fips) +{ + OtAlertState *s = opaque; + (void)bits; + (void)fips; + s->entropy_requested = false; + /* + * In RTL (alert_handler_ping_timer.sv), reseed_timer_d is reloaded with + * {wait_cyc_mask_i, {ReseedLfsrExtraBits{1'b1}}} = 19'h7ffff (524,287 + * cycles) at the 24 MHz I/O Div4 clock (~21.845 ms). + */ + uint32_t pclk = MAX(s->pclks[OT_ALERT_CLOCK_SRC_IO], 1u); + uint64_t reseed_ns = muldiv64(0x7ffffULL, NANOSECONDS_PER_SECOND, pclk); + timer_mod(&s->reseed_timer, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + (int64_t)reseed_ns); +} + +static void ot_alert_request_entropy(OtAlertState *s) +{ + if (!s->edn || s->edn_ep == UINT8_MAX) { + return; + } + if (!s->edn_connected) { + ot_edn_connect_endpoint(s->edn, s->edn_ep, &ot_alert_fill_entropy, s); + s->edn_connected = true; + } + if (!s->entropy_requested) { + s->entropy_requested = true; + ot_edn_request_entropy(s->edn, s->edn_ep); + } +} + +static void ot_alert_reseed_timer_cb(void *opaque) +{ + OtAlertState *s = opaque; + ot_alert_request_entropy(s); +} + static void ot_alert_reset_enter(Object *obj, ResetType type) { OtAlertClass *c = OT_ALERT_GET_CLASS(obj); @@ -1018,12 +1301,20 @@ static void ot_alert_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } + timer_del(&s->reseed_timer); + s->entropy_requested = false; + timer_mod(&s->reseed_timer, qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 100000LL); + for (unsigned ix = 0; ix < s->n_classes; ix++) { qemu_bh_cancel(s->schedulers[ix].esc_releaser); timer_del(&s->schedulers[ix].timer); + s->schedulers[ix].esc_tx_release_mask = 0; } memset(s->regs.shadow, 0, sizeof(OtShadowReg) * s->reg_count); + memset(s->alert_levels, 0, sizeof(bool) * s->n_alerts); + s->crashdump_latched = 0; + memset(s->latched_dump, 0, sizeof(s->latched_dump)); DVAL(s->regs.ping->timer_regwen) = R_PING_TIMER_REGWEN_EN_MASK; ot_shadow_reg_init(&s->regs.ping->timeout_cyc_shadowed, 256u); @@ -1041,6 +1332,10 @@ static void ot_alert_reset_enter(Object *obj, ResetType type) DVAL(s->regs.classes[ix].clr_regwen) = CLASS_CLR_REGWEN_EN_MASK; } + for (unsigned ix = 0; ix < PARAM_N_ESC_SEV; ix++) { + ibex_irq_set(&s->esc_txs[ix], 0); + } + ot_alert_update_irqs(s); for (unsigned ix = 0; ix < OT_ALERT_CLOCK_SRC_COUNT; ix++) { @@ -1095,6 +1390,7 @@ static void ot_alert_realize(DeviceState *dev, Error **errp) ibex_qdev_init_irqs(OBJECT(dev), s->esc_txs, OT_ALERT_ESCALATE, PARAM_N_ESC_SEV); + s->alert_levels = g_new0(bool, s->n_alerts); qdev_init_gpio_in_named(dev, &ot_alert_signal_tx, OT_DEVICE_ALERT, s->n_alerts); @@ -1107,9 +1403,12 @@ static void ot_alert_realize(DeviceState *dev, Error **errp) &s->schedulers[ix]); s->schedulers[ix].esc_releaser = qemu_bh_new(&ot_alert_release_esc_fn, &s->schedulers[ix]); - s->schedulers[ix].esc_tx_release = NULL; + s->schedulers[ix].esc_tx_release_mask = 0; } + timer_init_full(&s->reseed_timer, NULL, OT_VIRTUAL_CLOCK, SCALE_NS, 0, + &ot_alert_reseed_timer_cb, s); + s->reg_count = size / sizeof(OtShadowReg); s->regs.shadow = g_new0(OtShadowReg, s->reg_count); s->access_table = g_new0(OtAlertAccess, s->reg_count); diff --git a/include/hw/opentitan/ot_alert.h b/include/hw/opentitan/ot_alert.h index c45d4af70da9c..a0c1d0c8bb04a 100644 --- a/include/hw/opentitan/ot_alert.h +++ b/include/hw/opentitan/ot_alert.h @@ -38,4 +38,6 @@ OBJECT_DECLARE_TYPE(OtAlertState, OtAlertClass, OT_ALERT) #define OT_DEVICE_ALERT TYPE_OT_ALERT "-sig" #define OT_ALERT_ESCALATE TYPE_OT_ALERT "-esc" +void ot_alert_get_crash_dump(OtAlertState *s, uint32_t dump[9]); + #endif /* HW_OPENTITAN_OT_ALERT_H */ From 7098ce87c58bd6ed883fef8b87e197cbda9259c6 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:35 +0000 Subject: [PATCH 27/52] [ot] hw/opentitan: timer: align active CFG0/mtime writes and permits Align the OpenTitan RISC-V Timer model (ot_timer) with Earlgrey RTL (rv_timer.sv / timer_core.sv / rv_timer_reg_top.sv): - Allow CFG0 writes while CTRL.ACTIVE0 == 1 and snapshot the current 64-bit mtime before updating CFG0, TIMER_V_LOWER0, or TIMER_V_UPPER0 so in-flight 32-bit half writes do not rewind the other half to origin_ns, while modeling the 12-bit tick_count wrap burst (runaway_rem_cycles = 4096 - tick_count per timer_core.sv:32, 39) when CFG0.PRESCALE is lowered below the in-flight tick_count. - Evaluate mtime >= mtimecmp in ot_timer_rearm() even when CFG0.STEP == 0 and update IRQs when CTRL.ACTIVE0 is cleared. - Pulse alert on ALERT_TEST writes without latching s->regs[R_ALERT_TEST]. - Enforce RV_TIMER_PERMIT byte-enable masks and unmapped offset decode errors via ot_timer_accepts(). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I477ce924d2061b6242979f4be4e6c14f6a6a6964 --- hw/opentitan/ot_timer.c | 88 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/hw/opentitan/ot_timer.c b/hw/opentitan/ot_timer.c index 9f4d783beb763..39e283f938fae 100644 --- a/hw/opentitan/ot_timer.c +++ b/hw/opentitan/ot_timer.c @@ -92,6 +92,7 @@ struct OtTimerState { uint32_t regs[REGS_COUNT]; int64_t origin_ns; + uint32_t runaway_rem_cycles; uint32_t pclk; /* Current input clock */ const char *clock_src_name; /* IRQ name once connected */ @@ -110,6 +111,18 @@ static uint64_t ot_timer_ns_to_ticks(OtTimerState *s, int64_t ns) uint32_t prescaler = FIELD_EX32(s->regs[R_CFG0], CFG0, PRESCALE); uint64_t ticks = muldiv64((uint64_t)ns, s->pclk, NANOSECONDS_PER_SECOND); uint64_t step = FIELD_EX32(s->regs[R_CFG0], CFG0, STEP); + if (s->runaway_rem_cycles) { + /* + * In hw/ip/rv_timer/rtl/timer_core.sv:32, 39, + * tick_count resets only on exact equality (tick_count == prescaler) + * while tick asserts whenever active & (tick_count >= prescaler). + * During a 12-bit wrap burst, tick asserts every clock cycle for + * runaway_rem_cycles (4096 - tick_count) before resuming normal + * (prescaler + 1) division. + */ + uint64_t burst = MIN(ticks, (uint64_t)s->runaway_rem_cycles); + return (burst + (ticks - burst) / (prescaler + 1u)) * step; + } return (ticks / (prescaler + 1u)) * step; } @@ -189,8 +202,8 @@ static void ot_timer_rearm(OtTimerState *s, bool reset_origin) s->origin_ns = now; } - uint32_t step = FIELD_EX32(s->regs[R_CFG0], CFG0, STEP); - if (!ot_timer_is_active(s) || !step) { + if (!ot_timer_is_active(s)) { + ot_timer_update_irqs(s); return; } @@ -198,9 +211,10 @@ static void ot_timer_rearm(OtTimerState *s, bool reset_origin) uint64_t mtimecmp = s->regs[R_COMPARE_LOWER0_0] | ((uint64_t)s->regs[R_COMPARE_UPPER0_0] << 32u); + uint32_t step = FIELD_EX32(s->regs[R_CFG0], CFG0, STEP); if (mtime >= mtimecmp) { s->regs[R_INTR_STATE0] |= INTR_CMP0_MASK; - } else { + } else if (step) { int64_t delta = ot_timer_ticks_to_ns(s, mtimecmp - mtime); int64_t next = ot_timer_compute_next_timeout(s, now, delta); if (next < INT64_MAX) { @@ -231,14 +245,34 @@ static void ot_timer_clock_input(void *opaque, int irq, int level) } trace_ot_timer_update_clock(s->ot_id, s->pclk); + /* TODO: @loic: update on-going timer */ } +static const uint8_t RV_TIMER_PERMIT[REGS_COUNT] = { + [R_ALERT_TEST] = 0x1u, [R_CTRL] = 0x1u, + [R_INTR_ENABLE0] = 0x1u, [R_INTR_STATE0] = 0x1u, + [R_INTR_TEST0] = 0x1u, [R_CFG0] = 0x7u, + [R_TIMER_V_LOWER0] = 0xfu, [R_TIMER_V_UPPER0] = 0xfu, + [R_COMPARE_LOWER0_0] = 0xfu, [R_COMPARE_UPPER0_0] = 0xfu, +}; + +static bool ot_timer_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t permit = reg < REGS_COUNT ? RV_TIMER_PERMIT[reg] : 0u; + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return permit != 0u && (!is_write || (permit & ~reg_be) == 0u); +} + static uint64_t ot_timer_read(void *opaque, hwaddr addr, unsigned size) { OtTimerState *s = opaque; (void)size; - uint32_t val32 = 0; + uint32_t val32; hwaddr reg = R32_OFF(addr); switch (reg) { @@ -299,14 +333,17 @@ static void ot_timer_write(void *opaque, hwaddr addr, uint64_t value, switch (reg) { case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; - s->regs[reg] = val32; - ot_timer_update_alert(s); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; case R_CTRL: { uint32_t prev = s->regs[R_CTRL]; s->regs[R_CTRL] = val32 & R_CTRL_ACTIVE0_MASK; uint32_t change = prev ^ s->regs[R_CTRL]; if (change & R_CTRL_ACTIVE0_MASK) { + s->runaway_rem_cycles = 0u; if (ot_timer_is_active(s)) { /* start timer */ ot_timer_rearm(s, true); @@ -347,15 +384,48 @@ static void ot_timer_write(void *opaque, hwaddr addr, uint64_t value, ot_timer_update_irqs(s); break; case R_CFG0: - if (!ot_timer_is_active(s)) { - s->regs[R_CFG0] = val32 & (R_CFG0_PRESCALE_MASK | R_CFG0_STEP_MASK); + if (ot_timer_is_active(s)) { + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + uint64_t mtime = ot_timer_get_mtime(s, now); + s->runaway_rem_cycles = 0u; + uint32_t old_prescaler = + FIELD_EX32(s->regs[R_CFG0], CFG0, PRESCALE); + uint64_t pclk_ticks = muldiv64((uint64_t)(now - s->origin_ns), + s->pclk, NANOSECONDS_PER_SECOND); + uint32_t tick_count = + (uint32_t)(pclk_ticks % (old_prescaler + 1u)) & 0xfffu; + if (tick_count > FIELD_EX32(val32, CFG0, PRESCALE)) { + /* + * In hw/ip/rv_timer/rtl/timer_core.sv:32, 39, tick_count only + * clears on exact equality (tick_count == prescaler). Lowering + * CFG0.PRESCALE below the in-flight 12-bit tick_count while + * active causes tick (tick_count >= prescaler) to assert every + * clock cycle until tick_count wraps 0xfff -> 0x000 + * (4096 - tick_count cycles). + */ + s->runaway_rem_cycles = 4096u - tick_count; + } + s->regs[R_TIMER_V_LOWER0] = (uint32_t)mtime; + s->regs[R_TIMER_V_UPPER0] = (uint32_t)(mtime >> 32u); } + s->regs[R_CFG0] = val32 & (R_CFG0_PRESCALE_MASK | R_CFG0_STEP_MASK); + ot_timer_rearm(s, true); break; case R_TIMER_V_LOWER0: + if (ot_timer_is_active(s)) { + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + uint64_t mtime = ot_timer_get_mtime(s, now); + s->regs[R_TIMER_V_UPPER0] = (uint32_t)(mtime >> 32u); + } s->regs[R_TIMER_V_LOWER0] = val32; ot_timer_rearm(s, true); break; case R_TIMER_V_UPPER0: + if (ot_timer_is_active(s)) { + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + uint64_t mtime = ot_timer_get_mtime(s, now); + s->regs[R_TIMER_V_LOWER0] = (uint32_t)mtime; + } s->regs[R_TIMER_V_UPPER0] = val32; ot_timer_rearm(s, true); break; @@ -383,6 +453,7 @@ static const MemoryRegionOps ot_timer_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_timer_accepts, }; static const Property ot_timer_properties[] = { @@ -404,6 +475,7 @@ static void ot_timer_reset_enter(Object *obj, ResetType type) timer_del(s->timer); memset(s->regs, 0, sizeof(s->regs)); + s->runaway_rem_cycles = 0u; s->regs[R_CFG0] = 1u << R_CFG0_STEP_SHIFT; s->regs[R_COMPARE_LOWER0_0] = UINT32_MAX; s->regs[R_COMPARE_UPPER0_0] = UINT32_MAX; From 2a58d96fc2caf24acecf55c4c3b0c8c3635ca8b5 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:35 +0000 Subject: [PATCH 28/52] [ot] hw/opentitan: aon_timer: implement bark/bite wakeup and pause Align the OpenTitan Always-On Timer model (ot_aon_timer) with Earlgrey RTL (aon_timer.sv / aon_timer_core.sv / aon_timer_reg_top.sv): - Synchronize prescale_count, wkup_count, and wdog_count in ot_aon_timer_sync_wkup() and ot_aon_timer_sync_wdog(), and model rising-edge interrupt detection (wkup_intr_level / wdog_bark_level). - Support WDOG_CTRL.PAUSE_IN_SLEEP (OT_AON_TIMER_SLEEP_MODE), lifecycle escalation stop (OT_AON_TIMER_LC_ESCALATE), and RESET_TYPE_WAKEUP retention across deep sleep. - Allow WDOG_COUNT writes when WDOG_REGWEN == 0, pulse ALERT_TEST without latching, and enforce AON_TIMER_PERMIT sub-word write masks. - Wire OT_AON_TIMER_BARK to IBEX_WRAPPER and OT_AON_TIMER_LC_ESCALATE to OT_EG_SOC_SPLITTER_LC_ESCALATE in hw/riscv/ot_earlgrey.c. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ib2f1fae937c7227cb8c97b936aa24b156a6a6964 --- hw/opentitan/ot_aon_timer.c | 454 +++++++++++++++++++--------- hw/riscv/ot_earlgrey.c | 2 + include/hw/opentitan/ot_aon_timer.h | 10 +- 3 files changed, 320 insertions(+), 146 deletions(-) diff --git a/hw/opentitan/ot_aon_timer.c b/hw/opentitan/ot_aon_timer.c index ac9e033a70cc5..f904a119fe797 100644 --- a/hw/opentitan/ot_aon_timer.c +++ b/hw/opentitan/ot_aon_timer.c @@ -126,7 +126,13 @@ struct OtAonTimerState { int64_t wkup_origin_ns; int64_t wdog_origin_ns; + uint32_t prescale_count; + bool wkup_intr_level; + bool wdog_bark_level; bool wdog_bite; + bool lc_escalate_en; + bool sleep_mode; + bool low_power_exit; uint32_t pclks[OT_AON_TIMER_CLOCK_SRC_COUNT]; const char *clock_src_names[OT_AON_TIMER_CLOCK_SRC_COUNT]; @@ -160,24 +166,6 @@ ot_aon_timer_ticks_to_ns(OtAonTimerState *s, uint32_t prescaler, uint64_t ticks) return (int64_t)ns; } -static uint64_t ot_aon_timer_get_wkup_count(OtAonTimerState *s, uint64_t now) -{ - uint32_t prescaler = FIELD_EX32(s->regs[R_WKUP_CTRL], WKUP_CTRL, PRESCALER); - uint64_t wkup_count = ((uint64_t)s->regs[R_WKUP_COUNT_HI] << 32u) | - (uint64_t)s->regs[R_WKUP_COUNT_LO]; - return wkup_count + - ot_aon_timer_ns_to_ticks(s, prescaler, - (int64_t)(now - s->wkup_origin_ns)); -} - -static uint32_t ot_aon_timer_get_wdog_count(OtAonTimerState *s, uint64_t now) -{ - int64_t delta = (int64_t)(now - s->wdog_origin_ns); - uint64_t ticks = ot_aon_timer_ns_to_ticks(s, 0u, delta); - - return s->regs[R_WDOG_COUNT] + (uint32_t)ticks; -} - static int64_t ot_aon_timer_compute_next_timeout(OtAonTimerState *s, int64_t now, int64_t delta) { @@ -199,12 +187,25 @@ static int64_t ot_aon_timer_compute_next_timeout(OtAonTimerState *s, static inline bool ot_aon_timer_is_wkup_enabled(OtAonTimerState *s) { - return (s->regs[R_WKUP_CTRL] & R_WKUP_CTRL_ENABLE_MASK) != 0; + return (bool)FIELD_EX32(s->regs[R_WKUP_CTRL], WKUP_CTRL, ENABLE) && + !s->lc_escalate_en; } static inline bool ot_aon_timer_is_wdog_enabled(OtAonTimerState *s) { - return (s->regs[R_WDOG_CTRL] & R_WDOG_CTRL_ENABLE_MASK) != 0; + bool pause = + s->sleep_mode && + (bool)FIELD_EX32(s->regs[R_WDOG_CTRL], WDOG_CTRL, PAUSE_IN_SLEEP); + return (bool)FIELD_EX32(s->regs[R_WDOG_CTRL], WDOG_CTRL, ENABLE) && + !s->lc_escalate_en && !pause; +} + +bool ot_aon_timer_is_active(OtAonTimerState *s) +{ + if (!s) { + return false; + } + return ot_aon_timer_is_wkup_enabled(s) || ot_aon_timer_is_wdog_enabled(s); } static inline bool ot_aon_timer_wdog_register_write_enabled(OtAonTimerState *s) @@ -212,6 +213,123 @@ static inline bool ot_aon_timer_wdog_register_write_enabled(OtAonTimerState *s) return (s->regs[R_WDOG_REGWEN] & R_WDOG_REGWEN_REGWEN_MASK) != 0; } +static uint32_t ot_aon_timer_first_wkup_incr_clks(const OtAonTimerState *s) +{ + uint32_t prescaler = FIELD_EX32(s->regs[R_WKUP_CTRL], WKUP_CTRL, PRESCALER); + if (s->prescale_count <= prescaler) { + return (prescaler - s->prescale_count) + 1u; + } + return (4096u - s->prescale_count) + prescaler + 1u; +} + +static void ot_aon_timer_sync_wkup(OtAonTimerState *s) +{ + if (!s->pclks[OT_AON_TIMER_CLOCK_SRC_AON]) { + return; + } + + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + uint64_t now_clks = ot_aon_timer_ns_to_ticks(s, 0u, now); + uint64_t origin_clks = ot_aon_timer_ns_to_ticks(s, 0u, s->wkup_origin_ns); + + if (!ot_aon_timer_is_wkup_enabled(s)) { + s->wkup_intr_level = false; + s->wkup_origin_ns = ot_aon_timer_ticks_to_ns(s, 0u, now_clks); + return; + } + + uint64_t elapsed_clks = + (now_clks > origin_clks) ? (now_clks - origin_clks) : 0u; + if (elapsed_clks == 0u) { + return; + } + + uint32_t prescaler = FIELD_EX32(s->regs[R_WKUP_CTRL], WKUP_CTRL, PRESCALER); + uint32_t first_incr_clks = ot_aon_timer_first_wkup_incr_clks(s); + uint64_t wkup_incrs; + + if (elapsed_clks < first_incr_clks) { + wkup_incrs = 0u; + s->prescale_count = + (s->prescale_count + (uint32_t)elapsed_clks) & 0xfffu; + s->wkup_intr_level = false; + } else { + uint64_t rem = elapsed_clks - first_incr_clks; + uint32_t period = prescaler + 1u; + wkup_incrs = 1u + (rem / period); + s->prescale_count = (uint32_t)(rem % period); + } + + if (wkup_incrs > 0u) { + uint64_t old_count = ((uint64_t)s->regs[R_WKUP_COUNT_HI] << 32u) | + (uint64_t)s->regs[R_WKUP_COUNT_LO]; + uint64_t new_count = old_count + wkup_incrs; + uint64_t threshold = ((uint64_t)s->regs[R_WKUP_THOLD_HI] << 32u) | + (uint64_t)s->regs[R_WKUP_THOLD_LO]; + bool was_continuous_high = + s->wkup_intr_level && (first_incr_clks == 1u) && + (prescaler == 0u) && (old_count >= threshold); + + if ((new_count - 1u) >= threshold) { + if (!was_continuous_high) { + s->regs[R_INTR_STATE] |= INTR_WKUP_TIMER_EXPIRED_MASK; + } + s->regs[R_WKUP_CAUSE] |= R_WKUP_CAUSE_CAUSE_MASK; + } + s->wkup_intr_level = + (s->prescale_count == 0u) && ((new_count - 1u) >= threshold); + + s->regs[R_WKUP_COUNT_HI] = (uint32_t)(new_count >> 32u); + s->regs[R_WKUP_COUNT_LO] = (uint32_t)new_count; + } + + s->wkup_origin_ns = ot_aon_timer_ticks_to_ns(s, 0u, now_clks); +} + +static void ot_aon_timer_sync_wdog(OtAonTimerState *s) +{ + if (!s->pclks[OT_AON_TIMER_CLOCK_SRC_AON]) { + return; + } + + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + uint64_t now_clks = ot_aon_timer_ns_to_ticks(s, 0u, now); + uint64_t origin_clks = ot_aon_timer_ns_to_ticks(s, 0u, s->wdog_origin_ns); + + if (!ot_aon_timer_is_wdog_enabled(s)) { + s->wdog_origin_ns = ot_aon_timer_ticks_to_ns(s, 0u, now_clks); + return; + } + + uint64_t elapsed_clks = + (now_clks > origin_clks) ? (now_clks - origin_clks) : 0u; + if (elapsed_clks == 0u) { + return; + } + + uint64_t old_count = s->regs[R_WDOG_COUNT]; + uint64_t new_count = old_count + elapsed_clks; + uint32_t bark_threshold = s->regs[R_WDOG_BARK_THOLD]; + uint32_t bite_threshold = s->regs[R_WDOG_BITE_THOLD]; + uint64_t last_pre_incr = new_count - 1u; + + bool bark_level = (last_pre_incr >= bark_threshold); + if (bark_level && !s->wdog_bark_level) { + s->regs[R_INTR_STATE] |= INTR_WDOG_TIMER_BARK_MASK; + } + s->wdog_bark_level = bark_level; + + if (bark_level) { + s->regs[R_WKUP_CAUSE] |= R_WKUP_CAUSE_CAUSE_MASK; + } + if (last_pre_incr >= bite_threshold) { + s->wdog_bite = true; + } + + s->regs[R_WDOG_COUNT] = (uint32_t)new_count; + s->wdog_origin_ns = ot_aon_timer_ticks_to_ns(s, 0u, now_clks); +} + static void ot_aon_timer_update_alert(OtAonTimerState *s) { bool level = s->regs[R_ALERT_TEST] & R_ALERT_TEST_FATAL_FAULT_MASK; @@ -222,12 +340,13 @@ static void ot_aon_timer_update_irqs(OtAonTimerState *s) { bool wkup = (bool)(s->regs[R_INTR_STATE] & INTR_WKUP_TIMER_EXPIRED_MASK); bool bark = (bool)(s->regs[R_INTR_STATE] & INTR_WDOG_TIMER_BARK_MASK); + bool wkup_req = (bool)(s->regs[R_WKUP_CAUSE] & R_WKUP_CAUSE_CAUSE_MASK); trace_ot_aon_timer_irqs(s->ot_id, wkup, bark, s->wdog_bite); ibex_irq_set(&s->irq_wkup, wkup); ibex_irq_set(&s->irq_bark, bark); ibex_irq_set(&s->nmi_bark, bark); - ibex_irq_set(&s->pwrmgr_wkup, wkup); + ibex_irq_set(&s->pwrmgr_wkup, wkup_req); ibex_irq_set(&s->pwrmgr_bite, s->wdog_bite); } @@ -239,30 +358,44 @@ static void ot_aon_timer_rearm_wkup(OtAonTimerState *s, bool reset_origin) return; } - int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); - + ot_aon_timer_sync_wkup(s); if (reset_origin) { - s->wkup_origin_ns = now; + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + s->wkup_origin_ns = + ot_aon_timer_ticks_to_ns(s, 0u, + ot_aon_timer_ns_to_ticks(s, 0u, now)); } /* if not enabled, ignore threshold */ if (!ot_aon_timer_is_wkup_enabled(s)) { + s->wkup_intr_level = false; ot_aon_timer_update_irqs(s); return; } - uint64_t count = ot_aon_timer_get_wkup_count(s, now); + uint64_t count = ((uint64_t)s->regs[R_WKUP_COUNT_HI] << 32u) | + (uint64_t)s->regs[R_WKUP_COUNT_LO]; uint64_t threshold = ((uint64_t)s->regs[R_WKUP_THOLD_HI] << 32u) | (uint64_t)s->regs[R_WKUP_THOLD_LO]; + uint32_t prescaler = FIELD_EX32(s->regs[R_WKUP_CTRL], WKUP_CTRL, PRESCALER); + if (count < threshold) { + s->wkup_intr_level = false; + } - if (count >= threshold) { - s->regs[R_INTR_STATE] |= INTR_WKUP_TIMER_EXPIRED_MASK; - } else { - uint32_t prescaler = - FIELD_EX32(s->regs[R_WKUP_CTRL], WKUP_CTRL, PRESCALER); - int64_t delta = - ot_aon_timer_ticks_to_ns(s, prescaler, threshold - count); - int64_t next = ot_aon_timer_compute_next_timeout(s, now, delta); + bool intr_done = + (bool)(s->regs[R_INTR_STATE] & INTR_WKUP_TIMER_EXPIRED_MASK) || + (s->wkup_intr_level && prescaler == 0u && count >= threshold); + bool cause_set = (bool)(s->regs[R_WKUP_CAUSE] & R_WKUP_CAUSE_CAUSE_MASK); + + if ((!intr_done || !cause_set) && + ((threshold <= count) || + ((threshold - count) <= (INT64_MAX / 5000u) / (prescaler + 1u)))) { + uint64_t rem_incrs = (count >= threshold) ? 0u : (threshold - count); + uint64_t needed_clks = + ot_aon_timer_first_wkup_incr_clks(s) + rem_incrs * (prescaler + 1u); + int64_t delta = ot_aon_timer_ticks_to_ns(s, 0u, needed_clks); + int64_t next = + ot_aon_timer_compute_next_timeout(s, s->wkup_origin_ns, delta); if (next < INT64_MAX) { timer_mod(s->wkup_timer, next); } @@ -279,52 +412,58 @@ static void ot_aon_timer_wkup_cb(void *opaque) static void ot_aon_timer_rearm_wdog(OtAonTimerState *s, bool reset_origin) { - int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); - if (!s->pclks[OT_AON_TIMER_CLOCK_SRC_AON]) { return; } + ot_aon_timer_sync_wdog(s); if (reset_origin) { - s->wdog_origin_ns = now; + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + s->wdog_origin_ns = + ot_aon_timer_ticks_to_ns(s, 0u, + ot_aon_timer_ns_to_ticks(s, 0u, now)); } /* if not enabled, ignore threshold */ if (!ot_aon_timer_is_wdog_enabled(s)) { + s->wdog_bark_level = false; timer_del(s->wdog_timer); ot_aon_timer_update_irqs(s); return; } - uint32_t count = ot_aon_timer_get_wdog_count(s, now); + uint32_t count = s->regs[R_WDOG_COUNT]; uint32_t bark_threshold = s->regs[R_WDOG_BARK_THOLD]; uint32_t bite_threshold = s->regs[R_WDOG_BITE_THOLD]; - uint32_t threshold; - bool pending; - - if (count >= bark_threshold) { - s->regs[R_INTR_STATE] |= INTR_WDOG_TIMER_BARK_MASK; - threshold = UINT32_MAX; - pending = false; - } else { - threshold = bark_threshold; - pending = true; + if (count < bark_threshold) { + s->wdog_bark_level = false; } - if (count >= bite_threshold) { - s->wdog_bite = true; - } else { - threshold = MIN(threshold, bite_threshold); - pending = true; + uint64_t needed_clks = UINT64_MAX; + bool bark_done = (s->wdog_bark_level || + (s->regs[R_INTR_STATE] & INTR_WDOG_TIMER_BARK_MASK)) && + (s->regs[R_WKUP_CAUSE] & R_WKUP_CAUSE_CAUSE_MASK); + if (!bark_done) { + uint64_t bark_clks = (count >= bark_threshold) ? + 1u : + ((uint64_t)(bark_threshold - count) + 1u); + needed_clks = MIN(needed_clks, bark_clks); + } + if (!s->wdog_bite) { + uint64_t bite_clks = (count >= bite_threshold) ? + 1u : + ((uint64_t)(bite_threshold - count) + 1u); + needed_clks = MIN(needed_clks, bite_clks); } timer_del(s->wdog_timer); - if (pending) { - int64_t delta = ot_aon_timer_ticks_to_ns(s, 0u, threshold - count); - int64_t next = ot_aon_timer_compute_next_timeout(s, now, delta); + if (needed_clks < UINT64_MAX) { + int64_t delta = ot_aon_timer_ticks_to_ns(s, 0u, needed_clks); + int64_t next = + ot_aon_timer_compute_next_timeout(s, s->wdog_origin_ns, delta); if (next < INT64_MAX) { - trace_ot_aon_timer_set_wdog(s->ot_id, now, next); + trace_ot_aon_timer_set_wdog(s->ot_id, s->wdog_origin_ns, next); timer_mod(s->wdog_timer, next); } } @@ -357,6 +496,53 @@ static void ot_aon_timer_clock_input(void *opaque, int irq, int level) /* TODO: @loic: update on-going timer */ } +static void ot_aon_timer_lc_escalate(void *opaque, int irq, int level) +{ + OtAonTimerState *s = opaque; + + g_assert(irq == 0); + + bool esc = (bool)level; + if (esc == s->lc_escalate_en) { + return; + } + + ot_aon_timer_sync_wkup(s); + ot_aon_timer_sync_wdog(s); + + s->lc_escalate_en = esc; + ot_aon_timer_rearm_wkup(s, false); + ot_aon_timer_rearm_wdog(s, false); +} + +/* + * Per-register byte-enable write permission mask (AON_TIMER_PERMIT in + * hw/ip/aon_timer/rtl/aon_timer_reg_pkg.sv). A write raises TL-UL d_error=1 + * (wr_err) if any bit in PERMIT is not covered by the active byte enables + * (i.e. (PERMIT & ~reg_be) != 0). + */ +static const uint8_t AON_TIMER_PERMIT[REGS_COUNT] = { + [R_ALERT_TEST] = 0x1u, [R_WKUP_CTRL] = 0x3u, + [R_WKUP_THOLD_HI] = 0xfu, [R_WKUP_THOLD_LO] = 0xfu, + [R_WKUP_COUNT_HI] = 0xfu, [R_WKUP_COUNT_LO] = 0xfu, + [R_WDOG_REGWEN] = 0x1u, [R_WDOG_CTRL] = 0x1u, + [R_WDOG_BARK_THOLD] = 0xfu, [R_WDOG_BITE_THOLD] = 0xfu, + [R_WDOG_COUNT] = 0xfu, [R_INTR_STATE] = 0x1u, + [R_INTR_TEST] = 0x1u, [R_WKUP_CAUSE] = 0x1u, +}; + +static bool ot_aon_timer_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + hwaddr reg = R32_OFF(addr); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return reg < REGS_COUNT && (AON_TIMER_PERMIT[reg] & ~reg_be) == 0u; +} static uint64_t ot_aon_timer_read(void *opaque, hwaddr addr, unsigned size) { @@ -364,41 +550,28 @@ static uint64_t ot_aon_timer_read(void *opaque, hwaddr addr, unsigned size) (void)size; uint32_t val32; + s->low_power_exit = false; + + ot_aon_timer_sync_wkup(s); + ot_aon_timer_sync_wdog(s); + ot_aon_timer_update_irqs(s); + hwaddr reg = R32_OFF(addr); switch (reg) { case R_WKUP_CTRL: case R_WKUP_THOLD_HI: case R_WKUP_THOLD_LO: + case R_WKUP_COUNT_HI: + case R_WKUP_COUNT_LO: case R_WDOG_REGWEN: case R_WDOG_CTRL: case R_WDOG_BARK_THOLD: case R_WDOG_BITE_THOLD: + case R_WDOG_COUNT: case R_INTR_STATE: case R_WKUP_CAUSE: val32 = s->regs[reg]; break; - case R_WKUP_COUNT_HI: { - int64_t now = ot_aon_timer_is_wkup_enabled(s) ? - qemu_clock_get_ns(OT_VIRTUAL_CLOCK) : - s->wkup_origin_ns; - val32 = - (uint32_t)(ot_aon_timer_get_wkup_count(s, (uint64_t)now) >> 32u); - break; - } - case R_WKUP_COUNT_LO: { - int64_t now = ot_aon_timer_is_wkup_enabled(s) ? - qemu_clock_get_ns(OT_VIRTUAL_CLOCK) : - s->wkup_origin_ns; - val32 = (uint32_t)ot_aon_timer_get_wkup_count(s, (uint64_t)now); - break; - } - case R_WDOG_COUNT: { - int64_t now = ot_aon_timer_is_wdog_enabled(s) ? - qemu_clock_get_ns(OT_VIRTUAL_CLOCK) : - s->wdog_origin_ns; - val32 = ot_aon_timer_get_wdog_count(s, (uint64_t)now); - break; - } case R_ALERT_TEST: case R_INTR_TEST: qemu_log_mask(LOG_GUEST_ERROR, "%s: W/O register 0x%02x (%s)\n", @@ -426,6 +599,11 @@ static void ot_aon_timer_write(void *opaque, hwaddr addr, uint64_t value, (void)size; uint32_t val32 = (uint32_t)value; + s->low_power_exit = false; + + ot_aon_timer_sync_wkup(s); + ot_aon_timer_sync_wdog(s); + hwaddr reg = R32_OFF(addr); uint32_t pc = ibex_get_current_pc(); @@ -434,34 +612,16 @@ static void ot_aon_timer_write(void *opaque, hwaddr addr, uint64_t value, switch (reg) { case R_ALERT_TEST: - s->regs[R_ALERT_TEST] |= val32 & R_ALERT_TEST_FATAL_FAULT_MASK; - ot_aon_timer_update_alert(s); + if (val32 & R_ALERT_TEST_FATAL_FAULT_MASK) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; - case R_WKUP_CTRL: { - uint32_t prev = s->regs[R_WKUP_CTRL]; + case R_WKUP_CTRL: s->regs[R_WKUP_CTRL] = val32 & (R_WKUP_CTRL_ENABLE_MASK | R_WKUP_CTRL_PRESCALER_MASK); - uint32_t change = prev ^ s->regs[R_WKUP_CTRL]; - if (change & R_WKUP_CTRL_ENABLE_MASK) { - if (ot_aon_timer_is_wkup_enabled(s)) { - /* start timer */ - ot_aon_timer_rearm_wkup(s, true); - } else { - /* stop timer */ - timer_del(s->wkup_timer); - if (s->pclks[OT_AON_TIMER_CLOCK_SRC_AON]) { - /* save current count */ - int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); - uint64_t count = - ot_aon_timer_get_wkup_count(s, (uint64_t)now); - s->regs[R_WKUP_COUNT_HI] = (uint32_t)(count >> 32u); - s->regs[R_WKUP_COUNT_LO] = (uint32_t)count; - s->wkup_origin_ns = now; - } - } - } + ot_aon_timer_rearm_wkup(s, false); break; - } case R_WKUP_THOLD_HI: case R_WKUP_THOLD_LO: s->regs[reg] = val32; @@ -477,27 +637,10 @@ static void ot_aon_timer_write(void *opaque, hwaddr addr, uint64_t value, break; case R_WDOG_CTRL: if (ot_aon_timer_wdog_register_write_enabled(s)) { - uint32_t prev = s->regs[R_WDOG_CTRL]; s->regs[R_WDOG_CTRL] = val32 & (R_WDOG_CTRL_ENABLE_MASK | R_WDOG_CTRL_PAUSE_IN_SLEEP_MASK); - uint32_t change = prev ^ s->regs[R_WDOG_CTRL]; - if (change & R_WDOG_CTRL_ENABLE_MASK) { - if (ot_aon_timer_is_wdog_enabled(s)) { - /* start timer */ - ot_aon_timer_rearm_wdog(s, true); - } else { - /* stop timer */ - timer_del(s->wdog_timer); - if (s->pclks[OT_AON_TIMER_CLOCK_SRC_AON]) { - /* save current count */ - int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); - s->regs[R_WDOG_COUNT] = - ot_aon_timer_get_wdog_count(s, now); - s->wdog_origin_ns = now; - } - } - } + ot_aon_timer_rearm_wdog(s, false); } else { qemu_log_mask(LOG_GUEST_ERROR, "Ignoring write to locked WDOG_CTRL register\n"); @@ -518,33 +661,19 @@ static void ot_aon_timer_write(void *opaque, hwaddr addr, uint64_t value, s->regs[R_WDOG_COUNT] = val32; ot_aon_timer_rearm_wdog(s, true); break; - case R_INTR_STATE: { - uint32_t prev = s->regs[R_INTR_STATE]; + case R_INTR_STATE: s->regs[R_INTR_STATE] &= ~(val32 & INTR_MASK); /* rw1c */ - uint32_t change = prev ^ s->regs[R_INTR_STATE]; - ot_aon_timer_update_irqs(s); - /* - * schedule the timer for the next peripheral clock tick to check again - * for interrupt condition - */ - if (s->pclks[OT_AON_TIMER_CLOCK_SRC_AON]) { - int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); - int64_t next = ot_aon_timer_compute_next_timeout(s, now, 0); - if (change & INTR_WKUP_TIMER_EXPIRED_MASK) { - timer_mod_anticipate(s->wkup_timer, next); - } - if (change & INTR_WDOG_TIMER_BARK_MASK) { - timer_mod_anticipate(s->wdog_timer, next); - } - } + ot_aon_timer_rearm_wkup(s, false); + ot_aon_timer_rearm_wdog(s, false); break; - } case R_INTR_TEST: s->regs[R_INTR_STATE] |= val32 & INTR_MASK; ot_aon_timer_update_irqs(s); break; case R_WKUP_CAUSE: - /* ignore write, in QEMU wkup_cause is always 0 */ + s->regs[R_WKUP_CAUSE] &= val32 & R_WKUP_CAUSE_CAUSE_MASK; /* rw0c */ + ot_aon_timer_rearm_wkup(s, false); + ot_aon_timer_rearm_wdog(s, false); break; default: qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n", __func__, @@ -558,6 +687,7 @@ static const MemoryRegionOps ot_aon_timer_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_aon_timer_accepts, }; static const Property ot_aon_timer_properties[] = { @@ -570,6 +700,23 @@ static const Property ot_aon_timer_properties[] = { DeviceState *), }; +void ot_aon_timer_set_sleep_mode(OtAonTimerState *s, bool sleep_mode) +{ + if (!s || sleep_mode == s->sleep_mode) { + return; + } + + ot_aon_timer_sync_wkup(s); + ot_aon_timer_sync_wdog(s); + + if (s->sleep_mode && !sleep_mode) { + s->low_power_exit = true; + } + s->sleep_mode = sleep_mode; + ot_aon_timer_rearm_wkup(s, false); + ot_aon_timer_rearm_wdog(s, false); +} + static void ot_aon_timer_reset_enter(Object *obj, ResetType type) { OtAonTimerClass *c = OT_AON_TIMER_GET_CLASS(obj); @@ -584,9 +731,27 @@ static void ot_aon_timer_reset_enter(Object *obj, ResetType type) timer_del(s->wkup_timer); timer_del(s->wdog_timer); - memset(s->regs, 0, sizeof(s->regs)); - s->regs[R_WDOG_REGWEN] = 1u; - s->wdog_bite = false; + if (type != RESET_TYPE_COLD && (s->sleep_mode || s->low_power_exit) && + !s->wdog_bite) { + ot_aon_timer_sync_wkup(s); + ot_aon_timer_sync_wdog(s); + s->sleep_mode = false; + s->low_power_exit = false; + ot_aon_timer_rearm_wkup(s, false); + ot_aon_timer_rearm_wdog(s, false); + } else { + memset(s->regs, 0, sizeof(s->regs)); + s->regs[R_WDOG_REGWEN] = 1u; + s->prescale_count = 0u; + s->wkup_origin_ns = 0; + s->wdog_origin_ns = 0; + s->wkup_intr_level = false; + s->wdog_bark_level = false; + s->wdog_bite = false; + s->lc_escalate_en = false; + s->sleep_mode = false; + s->low_power_exit = false; + } ot_aon_timer_update_irqs(s); ot_aon_timer_update_alert(s); @@ -635,6 +800,9 @@ static void ot_aon_timer_init(Object *obj) ibex_qdev_init_irq(obj, &s->pwrmgr_bite, OT_AON_TIMER_BITE); ibex_qdev_init_irq(obj, &s->alert, OT_DEVICE_ALERT); + qdev_init_gpio_in_named(DEVICE(obj), &ot_aon_timer_lc_escalate, + OT_AON_TIMER_LC_ESCALATE, 1); + memory_region_init_io(&s->mmio, obj, &ot_aon_timer_ops, s, TYPE_OT_AON_TIMER, REGS_SIZE); sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio); diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index d60433d12e109..13220adfe5c03 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -1091,6 +1091,8 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_EG_SOC_GPIO_SYSBUS_IRQ(1, PLIC, 157), OT_EG_SOC_SIGNAL(OT_AON_TIMER_WKUP, 0, PWRMGR, \ OT_PWRMGR_WKUP, OT_PWRMGR_WAKEUP_AON_TIMER), + OT_EG_SOC_SIGNAL(OT_AON_TIMER_BARK, 0, IBEX_WRAPPER, \ + OT_IBEX_WRAPPER_WDOG_BARK, 0), OT_EG_SOC_SIGNAL(OT_AON_TIMER_BITE, 0, PWRMGR, \ OT_PWRMGR_RST, OT_EG_RESET_AON_TIMER), OT_EG_SOC_GPIO_ALERT(0, 31) diff --git a/include/hw/opentitan/ot_aon_timer.h b/include/hw/opentitan/ot_aon_timer.h index 7dea47b6133ec..c3d9a102869b2 100644 --- a/include/hw/opentitan/ot_aon_timer.h +++ b/include/hw/opentitan/ot_aon_timer.h @@ -33,8 +33,12 @@ #define TYPE_OT_AON_TIMER "ot-aon_timer" OBJECT_DECLARE_TYPE(OtAonTimerState, OtAonTimerClass, OT_AON_TIMER) -#define OT_AON_TIMER_WKUP TYPE_OT_AON_TIMER "-wkup" -#define OT_AON_TIMER_BARK TYPE_OT_AON_TIMER "-bark" -#define OT_AON_TIMER_BITE TYPE_OT_AON_TIMER "-bite" +#define OT_AON_TIMER_WKUP TYPE_OT_AON_TIMER "-wkup" +#define OT_AON_TIMER_BARK TYPE_OT_AON_TIMER "-bark" +#define OT_AON_TIMER_BITE TYPE_OT_AON_TIMER "-bite" +#define OT_AON_TIMER_LC_ESCALATE TYPE_OT_AON_TIMER "-lc-escalate" + +bool ot_aon_timer_is_active(OtAonTimerState *s); +void ot_aon_timer_set_sleep_mode(OtAonTimerState *s, bool sleep_mode); #endif /* HW_OPENTITAN_OT_AON_TIMER_H */ From f9e334fd760743c9d401c45da00a45f3459e03f6 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:36 +0000 Subject: [PATCH 29/52] [ot] hw/opentitan: ast_eg: align RNG noise rate and CSR access rules Align `hw/opentitan/ot_ast_eg.c` with Earlgrey `ast_reg_top.sv` and AST RNG timing: - Increase `OT_AST_EG_NOISE_4BIT_RATE` from 50 kHz to 200 kHz (24 MHz / 120 clocks) to match the 24 MHz AST main clock rate. - Enforce `SwAccessRO` on `REGA28` (`0x70`), `SwAccessWO` (read-zero) on `REGAL` (`0x98`), and full 32-bit width on `REGA2..REGA37`, while ignoring expected ROM `crt_section_copy` bulk-copy writes across read-only `REGA0`, `REGA1`, and `REGA28` without logging guest errors. - Add `ot_ast_eg_regs_accepts` over the `0x400`-byte MMIO window to fault on the `0x9c..0x1ff` unmapped hole, offsets `>= 0x214`, and subword writes. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I3c0a77949faf9a02b9104c5f8dd6d3066a6a6964 --- hw/opentitan/ot_ast_eg.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/hw/opentitan/ot_ast_eg.c b/hw/opentitan/ot_ast_eg.c index 89b4bf02c78db..e03f27912c4f0 100644 --- a/hw/opentitan/ot_ast_eg.c +++ b/hw/opentitan/ot_ast_eg.c @@ -133,7 +133,7 @@ static const char REGB_NAMES[REGSB_COUNT][6U] = { }; #undef REG_NAME_ENTRY -#define OT_AST_EG_NOISE_4BIT_RATE 50000u /* 50 kHz */ +#define OT_AST_EG_NOISE_4BIT_RATE 200000u /* 200 kHz (24 MHz / 120 clocks) */ typedef struct { char *name; @@ -329,6 +329,16 @@ static void ot_ast_eg_parse_clocks(OtASTEgState *s, Error **errp) } +static bool ot_ast_eg_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + return (reg <= R_REGAL || (reg >= R_REGB0 && reg <= R_REGB4)) && + (!is_write || size == 4u); +} + static uint64_t ot_ast_eg_regs_read(void *opaque, hwaddr addr, unsigned size) { OtASTEgState *s = opaque; @@ -376,9 +386,13 @@ static uint64_t ot_ast_eg_regs_read(void *opaque, hwaddr addr, unsigned size) case R_REGA35: case R_REGA36: case R_REGA37: - case R_REGAL: val32 = s->regsa[reg]; break; + case R_REGAL: + qemu_log_mask(LOG_GUEST_ERROR, "%s: %s is write-only\n", __func__, + REG_NAME(reg)); + val32 = 0; + break; case R_REGB0: case R_REGB1: case R_REGB2: @@ -414,6 +428,14 @@ static void ot_ast_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, switch (reg) { case R_REGA0: case R_REGA1: + case R_REGA28: + /* + * ROM startup (sw/device/silicon_creator/rom/rom_start.S) bulk-copies + * CREATOR_SW_CFG_AST_CFG across the contiguous REGA0..REGAL range via + * crt_section_copy when AST_INIT_EN is enabled; ignore writes to these + * read-only registers without logging guest errors. + */ + break; case R_REGA2: case R_REGA3: case R_REGA4: @@ -440,7 +462,6 @@ static void ot_ast_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, case R_REGA25: case R_REGA26: case R_REGA27: - case R_REGA28: case R_REGA29: case R_REGA30: case R_REGA31: @@ -451,7 +472,7 @@ static void ot_ast_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, case R_REGA36: case R_REGA37: case R_REGAL: - s->regsa[reg] = val32 & 0xffu; + s->regsa[reg] = val32; break; case R_REGB0: case R_REGB1: @@ -478,6 +499,7 @@ static const MemoryRegionOps ot_ast_eg_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_ast_eg_regs_accepts, }; static void ot_ast_eg_reset_enter(Object *obj, ResetType type) @@ -559,7 +581,7 @@ static void ot_ast_eg_init(Object *obj) OtASTEgState *s = OT_AST_EG(obj); memory_region_init_io(&s->mmio, obj, &ot_ast_eg_regs_ops, s, TYPE_OT_AST_EG, - REGS_SIZE); + 0x400u); sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); s->regsa = g_new0(uint32_t, REGSA_COUNT); From c7e0428e210b40062be8fcd52b9af45bb0a1b42d Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:36 +0000 Subject: [PATCH 30/52] [ot] hw/opentitan: sensor_eg: implement alert and wake events Implement Earlgrey `sensor_ctrl` (`ot-sensor-eg`) alert, wakeup, and register accuracy: - Model `ast_alert_latch` (`ast_alert.sv`), `ALERT_TRIG`, `ALERT_EN_0..10` (`Mubi4`), `FATAL_ALERT_EN`, W1C `RECOV_ALERT`, and read-only `FATAL_ALERT` routing to `alerts[0..1]` and `OT_SENSOR_WKUP`. - Export `ot_sensor_eg_trigger_recov_event()` for AST/low-power sensor wakeup injection. - Initialize `R_STATUS` to `AST_INIT_DONE | IO_POK` (`0x7`) and preserve AON registers (`R_ALERT_TRIG`, `R_RECOV_ALERT`) across resets. - Implement `MANUAL_PAD_ATTR_0..3` storage and TL-UL sub-word write byte-enable checks (`ot_sensor_eg_regs_accepts`). - Wire `SENSOR_CTRL` PLIC interrupts (158, 159) and `OT_SENSOR_WKUP` to `OT_PWRMGR_WAKEUP_SENSOR` in `hw/riscv/ot_earlgrey.c`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ieebff30a3beb4d51e645731990ed36d06a6a6964 --- hw/opentitan/ot_sensor_eg.c | 127 +++++++++++++++++++++++----- hw/riscv/ot_earlgrey.c | 4 + include/hw/opentitan/ot_sensor_eg.h | 4 + 3 files changed, 116 insertions(+), 19 deletions(-) diff --git a/hw/opentitan/ot_sensor_eg.c b/hw/opentitan/ot_sensor_eg.c index 29207eba402e4..413545a59802d 100644 --- a/hw/opentitan/ot_sensor_eg.c +++ b/hw/opentitan/ot_sensor_eg.c @@ -32,6 +32,7 @@ #include "qemu/log.h" #include "qemu/typedefs.h" #include "hw/opentitan/ot_alert.h" +#include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_sensor_eg.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" @@ -192,6 +193,8 @@ struct OtSensorEgState { MemoryRegion mmio; IbexIRQ irqs[2u]; IbexIRQ alerts[NUM_ALERTS]; + IbexIRQ wkup; + uint32_t ast_alert_latch; uint32_t *regs; }; @@ -211,13 +214,95 @@ static void ot_sensor_eg_update_irqs(OtSensorEgState *s) } } +static uint32_t ot_sensor_eg_get_alert_en_mask(const OtSensorEgState *s) +{ + uint32_t mask = 0; + for (unsigned ix = 0; ix <= (R_ALERT_EN_10 - R_ALERT_EN_0); ix++) { + if ((s->regs[R_ALERT_EN_0 + ix] & ALERT_EN_VAL_MASK) != + OT_MULTIBITBOOL4_FALSE) { + mask |= 1u << ix; + } + } + return mask; +} + static void ot_sensor_eg_update_alerts(OtSensorEgState *s) { + s->ast_alert_latch |= s->regs[R_ALERT_TRIG]; + + uint32_t alert_en_mask = ot_sensor_eg_get_alert_en_mask(s); + uint32_t active_events = s->ast_alert_latch & alert_en_mask; + uint32_t recov_events = active_events & ~s->regs[R_FATAL_ALERT_EN]; + uint32_t fatal_events = active_events & s->regs[R_FATAL_ALERT_EN]; + + s->regs[R_RECOV_ALERT] |= recov_events; + s->regs[R_FATAL_ALERT] |= fatal_events; + uint32_t level = s->regs[R_ALERT_TEST]; + if (recov_events) { + level |= (1u << 0u); + } + if (s->regs[R_FATAL_ALERT]) { + level |= (1u << 1u); + } for (unsigned ix = 0; ix < ARRAY_SIZE(s->alerts); ix++) { ibex_irq_set(&s->alerts[ix], (int)((level >> ix) & 0x1u)); } + + /* + * In ast_alert.sv & sensor_ctrl.sv: + * event_clr[i] = recov_event[i] & reg2hw.recov_alert[i].q + * clr_p_alert = !set_p_alert && p_alert_ack + */ + s->ast_alert_latch &= ~(recov_events & ~s->regs[R_ALERT_TRIG]); + uint32_t rem_recov = + s->ast_alert_latch & alert_en_mask & ~s->regs[R_FATAL_ALERT_EN]; + if (!rem_recov && + !(s->regs[R_ALERT_TEST] & R_ALERT_TEST_RECOV_ALERT_MASK)) { + ibex_irq_set(&s->alerts[0], 0); + } + + ibex_irq_set(&s->wkup, (int)(bool)((s->ast_alert_latch & alert_en_mask) || + s->regs[R_RECOV_ALERT])); +} + +void ot_sensor_eg_trigger_recov_event(OtSensorEgState *s, unsigned event_idx) +{ + if (!s || event_idx > (R_ALERT_EN_10 - R_ALERT_EN_0)) { + return; + } + s->ast_alert_latch |= (1u << event_idx); + ot_sensor_eg_update_alerts(s); +} + +static uint8_t ot_sensor_eg_reg_permit(hwaddr reg) +{ + switch (reg) { + case R_ALERT_TRIG: + case R_FATAL_ALERT_EN: + case R_RECOV_ALERT: + case R_FATAL_ALERT: + return 0x3u; + default: + return 0x1u; + } +} + +static bool ot_sensor_eg_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + if (reg >= REGS_COUNT) { + return false; + } + if (!is_write) { + return true; + } + uint32_t reg_be = ((1u << size) - 1u) << (addr & 3u); + return (ot_sensor_eg_reg_permit(reg) & ~reg_be) == 0u; } static uint64_t ot_sensor_eg_regs_read(void *opaque, hwaddr addr, unsigned size) @@ -265,7 +350,7 @@ static uint64_t ot_sensor_eg_regs_read(void *opaque, hwaddr addr, unsigned size) trace_ot_sensor_io_read_out((uint32_t)addr, REG_NAME(reg), val32, pc); return (uint64_t)val32; -}; +} static void ot_sensor_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, unsigned size) @@ -309,8 +394,7 @@ static void ot_sensor_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, case R_ALERT_TRIG: val32 &= ALERT_SENSOR_MASK; s->regs[reg] = val32; - qemu_log_mask(LOG_UNIMP, "Unimplemented register 0x%02x (%s)\n", - (uint32_t)addr, REG_NAME(reg)); + ot_sensor_eg_update_alerts(s); break; case R_ALERT_EN_0 ... R_ALERT_EN_10: if (!s->regs[R_CFG_REGWEN]) { @@ -321,8 +405,7 @@ static void ot_sensor_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, } val32 &= ALERT_EN_VAL_MASK; s->regs[reg] = val32; - qemu_log_mask(LOG_UNIMP, "Unimplemented register 0x%02x (%s)\n", - (uint32_t)addr, REG_NAME(reg)); + ot_sensor_eg_update_alerts(s); break; case R_FATAL_ALERT_EN: if (!s->regs[R_CFG_REGWEN]) { @@ -333,20 +416,16 @@ static void ot_sensor_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, } val32 &= ALERT_SENSOR_MASK; s->regs[reg] = val32; - qemu_log_mask(LOG_UNIMP, "Unimplemented register 0x%02x (%s)\n", - (uint32_t)addr, REG_NAME(reg)); + ot_sensor_eg_update_alerts(s); break; case R_RECOV_ALERT: val32 &= ALERT_SENSOR_MASK; - s->regs[reg] = val32; - qemu_log_mask(LOG_UNIMP, "Unimplemented register 0x%02x (%s)\n", - (uint32_t)addr, REG_NAME(reg)); + s->regs[reg] &= ~val32; + ot_sensor_eg_update_alerts(s); break; case R_FATAL_ALERT: - val32 &= ALERT_SENSOR_MASK; - s->regs[reg] = val32; - qemu_log_mask(LOG_UNIMP, "Unimplemented register 0x%02x (%s)\n", - (uint32_t)addr, REG_NAME(reg)); + qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n", + __func__, (uint32_t)addr, REG_NAME(reg)); break; case R_STATUS: qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n", @@ -358,15 +437,16 @@ static void ot_sensor_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_MANUAL_PAD_ATTR_0 ... R_MANUAL_PAD_ATTR_3: if (!s->regs[reg - R_MANUAL_PAD_ATTR_0 + R_MANUAL_PAD_ATTR_REGWEN_0]) { - qemu_log_mask(LOG_GUEST_ERROR, "Cannot change %s, %s disabled", + qemu_log_mask(LOG_GUEST_ERROR, "Cannot change %s, %s disabled\n", REG_NAME(reg), REG_NAME(reg - R_MANUAL_PAD_ATTR_0 + R_MANUAL_PAD_ATTR_REGWEN_0)); + break; } - break; val32 &= MANUAL_PAD_ATTR_MASK; - qemu_log_mask(LOG_UNIMP, "Unimplemented register 0x%02x (%s)\n", - (uint32_t)addr, REG_NAME(reg)); + s->regs[reg] = val32; + qemu_log_mask(LOG_UNIMP, "%s: %s is not supported\n", __func__, + REG_NAME(reg)); break; default: qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n", __func__, @@ -381,6 +461,7 @@ static const MemoryRegionOps ot_sensor_eg_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_sensor_eg_regs_accepts, }; static void ot_sensor_eg_reset_enter(Object *obj, ResetType type) @@ -392,9 +473,16 @@ static void ot_sensor_eg_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } + s->ast_alert_latch = 0u; + + uint32_t alert_trig = s->regs[R_ALERT_TRIG]; + uint32_t recov_alert = s->regs[R_RECOV_ALERT]; memset(s->regs, 0, REGS_SIZE); + s->regs[R_ALERT_TRIG] = alert_trig; + s->regs[R_RECOV_ALERT] = recov_alert; s->regs[R_CFG_REGWEN] = 0x1u; + s->regs[R_STATUS] = R_STATUS_AST_INIT_DONE_MASK | R_STATUS_IO_POK_MASK; for (unsigned rix = R_ALERT_EN_0; rix <= R_ALERT_EN_10; rix++) { s->regs[rix] = 0x6u; } @@ -412,7 +500,7 @@ static void ot_sensor_eg_init(Object *obj) OtSensorEgState *s = OT_SENSOR_EG(obj); memory_region_init_io(&s->mmio, obj, &ot_sensor_eg_regs_ops, s, - TYPE_OT_SENSOR_EG, REGS_SIZE); + TYPE_OT_SENSOR_EG, 0x80u); sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); s->regs = g_new0(uint32_t, REGS_COUNT); @@ -422,6 +510,7 @@ static void ot_sensor_eg_init(Object *obj) for (unsigned ix = 0; ix < ARRAY_SIZE(s->alerts); ix++) { ibex_qdev_init_irq(obj, &s->alerts[ix], OT_DEVICE_ALERT); } + ibex_qdev_init_irq(obj, &s->wkup, OT_SENSOR_WKUP); } static void ot_sensor_eg_class_init(ObjectClass *klass, const void *data) diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 13220adfe5c03..75cf3810b8b9e 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -1118,6 +1118,10 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { { .base = 0x40490000u } ), .gpio = IBEXGPIOCONNDEFS( + OT_EG_SOC_GPIO_SYSBUS_IRQ(0, PLIC, 158), + OT_EG_SOC_GPIO_SYSBUS_IRQ(1, PLIC, 159), + OT_EG_SOC_SIGNAL(OT_SENSOR_WKUP, 0, PWRMGR, + OT_PWRMGR_WKUP, OT_PWRMGR_WAKEUP_SENSOR), OT_EG_SOC_GPIO_ALERT(0, 32), OT_EG_SOC_GPIO_ALERT(1, 33) ), diff --git a/include/hw/opentitan/ot_sensor_eg.h b/include/hw/opentitan/ot_sensor_eg.h index 696bbe6071bcd..7122a842b954e 100644 --- a/include/hw/opentitan/ot_sensor_eg.h +++ b/include/hw/opentitan/ot_sensor_eg.h @@ -33,4 +33,8 @@ #define TYPE_OT_SENSOR_EG "ot-sensor-eg" OBJECT_DECLARE_TYPE(OtSensorEgState, OtSensorEgClass, OT_SENSOR_EG) +#define OT_SENSOR_WKUP TYPE_OT_SENSOR_EG "-wkup" + +void ot_sensor_eg_trigger_recov_event(OtSensorEgState *s, unsigned event_idx); + #endif /* HW_OPENTITAN_OT_SENSOR_EG_H */ From a07d5f6dc02381587cd7630005310f11a3ee15b4 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:36 +0000 Subject: [PATCH 31/52] [ot] hw/opentitan: rstmgr: align crash info, sw_reset, and PERMIT Align the OpenTitan Reset Manager (`ot-rstmgr`) model and its Earlgrey SoC `USB_AON` reset line wiring (`hw/riscv/ot_earlgrey.c`) with `rstmgr.sv` and `rstmgr_crash_info.sv`: - Implement `ALERT_INFO` (`ALERT_INFO_ATTR = 9`) and `CPU_INFO` (`CPU_INFO_ATTR = 8`, `idx % 8`) crash dump capture across non-POR resets, preserving `ALERT_INFO_CTRL.INDEX` / `CPU_INFO_CTRL.INDEX` across software resets and `.EN` across low-power exit resets. - Model `RESET_REQ` (`kMultiBitBool4True`) software reset with a 1 us `sw_reset_timer` delay and cancel the pending timer if `RESET_REQ` is cleared (`0x9`) before expiry. - Fix `SW_RST_CTRL_N_0..7` bit-0 mask evaluation (`SW_RST_CTRL_VAL_MASK`), wire `OT_RSTMGR_RST_USB_AON` in `hw/riscv/ot_earlgrey.c`, and expose `ot_rstmgr_is_low_power_exit()` / `is_por_reset()` / `is_ndm_reset()` helpers. - Pulse `ALERT_TEST` for 1 cycle and enforce `RSTMGR_PERMIT` sub-word write access faults (`ot_rstmgr_regs_accepts`). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ie830dc3780800570434a8addcfb974256a6a6964 --- hw/opentitan/ot_rstmgr.c | 214 ++++++++++++++++++++++++++----- include/hw/opentitan/ot_rstmgr.h | 5 + 2 files changed, 189 insertions(+), 30 deletions(-) diff --git a/hw/opentitan/ot_rstmgr.c b/hw/opentitan/ot_rstmgr.c index 621cae0747be5..051c78b62d2dd 100644 --- a/hw/opentitan/ot_rstmgr.c +++ b/hw/opentitan/ot_rstmgr.c @@ -34,6 +34,7 @@ #include "qemu/error-report.h" #include "qemu/log.h" #include "qemu/main-loop.h" +#include "qemu/timer.h" #include "qemu/typedefs.h" #include "qapi/error.h" #include "hw/opentitan/ot_alert.h" @@ -48,6 +49,7 @@ #include "hw/riscv/ibex_common.h" #include "hw/riscv/ibex_irq.h" #include "hw/sysbus.h" +#include "system/replay.h" #include "system/runstate.h" #include "trace.h" @@ -167,9 +169,12 @@ struct OtRstMgrState { IbexIRQ sw_reset; IbexIRQ alerts[PARAM_NUM_ALERTS]; QEMUBH *bus_reset_bh; + QEMUTimer *sw_reset_timer; CPUState *cpu; uint32_t *regs; + uint32_t cpu_info_dump[8]; + uint32_t alert_info_dump[9]; bool por; /* Power-On Reset property */ char *ot_id; @@ -182,14 +187,41 @@ struct OtRstMgrClass { ResettablePhases parent_phases; }; +static bool ot_rstmgr_pending_internal_reset; +static bool ot_rstmgr_last_reset_por = true; +static bool ot_rstmgr_last_reset_low_power; +static bool ot_rstmgr_last_reset_ndm; + +bool ot_rstmgr_is_low_power_exit(void) +{ + return ot_rstmgr_pending_internal_reset && ot_rstmgr_last_reset_low_power; +} + +bool ot_rstmgr_is_por_reset(void) +{ + return !ot_rstmgr_pending_internal_reset || ot_rstmgr_last_reset_por; +} + +bool ot_rstmgr_is_ndm_reset(void) +{ + return ot_rstmgr_pending_internal_reset && ot_rstmgr_last_reset_ndm; +} + +bool ot_rstmgr_is_internal_reset(void) +{ + return ot_rstmgr_pending_internal_reset; +} + typedef struct { const char *typename; unsigned idx; + bool aon; } OtRstMgrResettable; typedef struct { char *path; bool reset; + bool aon; char *ot_id; } OtRstMgrResetDesc; @@ -207,23 +239,19 @@ static const OtRstMgrConfig RSTMGR_CONFIG[OT_RSTMGR_VERSION_COUNT] = { [OT_RSTMGR_RESET_SYSCTRL] = BIT(3), [OT_RSTMGR_RESET_AON_TIMER] = BIT(4), [OT_RSTMGR_RESET_SENSOR] = BIT(5), - [OT_RSTMGR_RESET_PWRMGR] = BIT(6), - [OT_RSTMGR_RESET_ALERT_HANDLER] = BIT(7), - [OT_RSTMGR_RESET_RV_DM] = BIT(8), + [OT_RSTMGR_RESET_PWRMGR] = BIT(5), + [OT_RSTMGR_RESET_ALERT_HANDLER] = BIT(6), + [OT_RSTMGR_RESET_RV_DM] = BIT(7), }, .sw_resettable_devices = { [0u] = { TYPE_OT_SPI_DEVICE, 0u }, [1u] = { TYPE_OT_SPI_HOST, 0u }, [2u] = { TYPE_OT_SPI_HOST, 1u }, - /* - * Not yet supported - * - * [3u] = { TYPE_OT_USB, 0u }, - * [4u] = { TYPE_OT_USB, 1u }, - * [5u] = { TYPE_OT_I2C_EG, 0u }, - * [6u] = { TYPE_OT_I2C_EG, 1u }, - * [7u] = { TYPE_OT_I2C_EG, 2u }, - */ + [3u] = { TYPE_OT_USBDEV, 0u }, + [4u] = { TYPE_OT_USBDEV, 0u, true }, + [5u] = { TYPE_OT_I2C, 0u }, + [6u] = { TYPE_OT_I2C, 1u }, + [7u] = { TYPE_OT_I2C, 2u }, } }, [OT_RSTMGR_VERSION_DJ] = { @@ -280,22 +308,60 @@ static void ot_rstmgr_update_alerts(OtRstMgrState *s) for (unsigned ix = 0; ix < ARRAY_SIZE(s->alerts); ix++) { ibex_irq_set(&s->alerts[ix], (int)((level >> ix) & 0x1u)); } + + if (s->regs[R_ALERT_TEST]) { + s->regs[R_ALERT_TEST] = 0u; + for (unsigned ix = 0; ix < ARRAY_SIZE(s->alerts); ix++) { + ibex_irq_set(&s->alerts[ix], 0); + } + } +} + +static void ot_rstmgr_sw_reset_timer_cb(void *opaque) +{ + OtRstMgrState *s = opaque; + + if (s->regs[R_RESET_REQ] == OT_MULTIBITBOOL4_TRUE) { + ibex_irq_raise(&s->sw_reset); + } } static void ot_rstmgr_reset_bus(void *opaque) { OtRstMgrState *s = opaque; - /* request the vCPU to stop */ - s->cpu->stop = true; + g_assert(s->cpu); + + if (!s->cpu->stopped) { + /* request the vCPU to stop */ + s->cpu->stop = true; - /* wait for the vCPU to stop */ - while (!s->cpu->stopped) { + /* + * Drop replay_mutex so the vCPU thread can acquire it in + * rr_cpu_thread_fn and reach rr_wait_io_event() to set cpu->stopped = + * true. Lock ordering: replay_mutex must always be acquired before bql. + */ + replay_mutex_unlock(); bql_unlock(); - qemu_cpu_kick(s->cpu); + while (!s->cpu->stopped) { + qemu_cpu_kick(s->cpu); + g_usleep(100); + } + g_usleep(1000); + replay_mutex_lock(); bql_lock(); + qemu_notify_event(); + } else { + s->cpu->stop = false; + } + + if (s->regs[R_ALERT_INFO_CTRL] & R_ALERT_INFO_CTRL_EN_MASK) { + OtAlertState *alert_dev = + (OtAlertState *)object_resolve_path_type("", TYPE_OT_ALERT, NULL); + if (alert_dev) { + ot_alert_get_crash_dump(alert_dev, s->alert_info_dump); + } } - qemu_notify_event(); ibex_irq_raise(&s->soc_reset); } @@ -315,9 +381,9 @@ static int ot_rstmgr_sw_rst_walker(DeviceState *dev, void *opaque) trace_ot_rstmgr_sw_rst(desc->ot_id, desc->path, desc->reset); if (desc->reset) { - resettable_assert_reset(OBJECT(dev), RESET_TYPE_COLD); + resettable_assert_reset(OBJECT(dev), RESET_TYPE_WAKEUP); } else { - resettable_release_reset(OBJECT(dev), RESET_TYPE_COLD); + resettable_release_reset(OBJECT(dev), RESET_TYPE_WAKEUP); } /* abort walk immediately */ @@ -342,6 +408,7 @@ static void ot_rstmgr_update_sw_reset(OtRstMgrState *s, unsigned devix) desc.path = g_strdup_printf("%s[%d]", rst->typename, rst->idx); desc.reset = !s->regs[R_SW_RST_CTRL_N_0 + devix]; + desc.aon = rst->aon; desc.ot_id = s->ot_id; trace_ot_rstmgr_sw_reset(s->ot_id, desc.path); @@ -383,11 +450,31 @@ static void ot_rstmgr_reset_req(void *opaque, int irq, int level) return; } - s->regs[R_RESET_INFO] = req; + /* Only SW is allowed to clear a reset reason, HW only sets bits (|=) */ + if (level == OT_RSTMGR_RESET_POR) { + s->por = true; + } + s->regs[R_RESET_INFO] |= req; + ot_rstmgr_last_reset_por = + (ot_rstmgr_pending_internal_reset && ot_rstmgr_last_reset_por) || + (level == OT_RSTMGR_RESET_POR); + ot_rstmgr_last_reset_low_power = (level == OT_RSTMGR_RESET_LOW_POWER); + ot_rstmgr_last_reset_ndm = + (ot_rstmgr_pending_internal_reset && ot_rstmgr_last_reset_ndm) || + (level == OT_RSTMGR_RESET_RV_DM); + ot_rstmgr_pending_internal_reset = true; trace_ot_rstmgr_reset_req(s->ot_id, REQ_NAME(level), req, fastclk); - qemu_bh_schedule(s->bus_reset_bh); + if (s->cpu) { + cpu_pause(s->cpu); + } + + if (level == OT_RSTMGR_RESET_RV_DM) { + ot_rstmgr_reset_bus(s); + } else { + qemu_bh_schedule(s->bus_reset_bh); + } } static uint64_t ot_rstmgr_regs_read(void *opaque, hwaddr addr, unsigned size) @@ -403,12 +490,8 @@ static uint64_t ot_rstmgr_regs_read(void *opaque, hwaddr addr, unsigned size) case R_RESET_INFO: case R_ALERT_REGWEN: case R_ALERT_INFO_CTRL: - case R_ALERT_INFO_ATTR: - case R_ALERT_INFO: case R_CPU_REGWEN: case R_CPU_INFO_CTRL: - case R_CPU_INFO_ATTR: - case R_CPU_INFO: case R_SW_RST_REGWEN_0: case R_SW_RST_REGWEN_1: case R_SW_RST_REGWEN_2: @@ -428,6 +511,32 @@ static uint64_t ot_rstmgr_regs_read(void *opaque, hwaddr addr, unsigned size) case R_ERR_CODE: val32 = s->regs[reg]; break; + case R_ALERT_INFO_ATTR: + val32 = 9u; + break; + case R_ALERT_INFO: { + uint32_t idx = + FIELD_EX32(s->regs[R_ALERT_INFO_CTRL], ALERT_INFO_CTRL, INDEX); + val32 = (idx < ARRAY_SIZE(s->alert_info_dump)) ? + s->alert_info_dump[idx] : + 0u; + break; + } + case R_CPU_INFO_ATTR: + val32 = 8u; + break; + case R_CPU_INFO: { + /* + * In rstmgr_crash_info.sv, SlotCntWidth = $clog2(CrashStoreSlot). + * For CPU_INFO, CrashStoreSlot = 8 -> SlotCntWidth = $clog2(8) = 3, + * so slot_sel_i[3] is tied off as unused_idx and slot_o indexes + * slots[slot_sel_i[2:0]] (idx % 8u). + */ + uint32_t idx = + FIELD_EX32(s->regs[R_CPU_INFO_CTRL], CPU_INFO_CTRL, INDEX); + val32 = s->cpu_info_dump[idx % ARRAY_SIZE(s->cpu_info_dump)]; + break; + } case R_ALERT_TEST: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: W/O register 0x02%x (%s)\n", __func__, s->ot_id, (uint32_t)addr, REG_NAME(reg)); @@ -468,8 +577,17 @@ static void ot_rstmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, /* * "Upon completion of reset, this bit is automatically cleared by * hardware." + * Schedule software reset request with a short delay (1 us) to + * model pwrmgr synchronization latency, allowing software to + * execute trailing instructions (e.g. wfi) before reset assertion. + * Do not re-arm if already pending, as tight shutdown loops (e.g. + * ROM shutdown_hang) repeatedly write R_RESET_REQ in < 1 us and + * would otherwise postpone the reset timer indefinitely. */ - ibex_irq_raise(&s->sw_reset); + if (!timer_pending(s->sw_reset_timer)) { + timer_mod(s->sw_reset_timer, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 1000); + } if (s->fatal_reset) { s->fatal_reset--; if (!s->fatal_reset) { @@ -478,6 +596,9 @@ static void ot_rstmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, SHUTDOWN_CAUSE_GUEST_SHUTDOWN, 1); } } + } else { + timer_del(s->sw_reset_timer); + ibex_irq_lower(&s->sw_reset); } break; case R_RESET_INFO: @@ -534,7 +655,7 @@ static void ot_rstmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, uint32_t change = s->regs[reg] ^ val32; s->regs[reg] = val32; unsigned devix = (unsigned)reg - R_SW_RST_CTRL_N_0; - if (change & (1u << devix)) { + if (change & SW_RST_CTRL_VAL_MASK) { ot_rstmgr_update_sw_reset(s, devix); } } else { @@ -562,6 +683,20 @@ static void ot_rstmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, } }; +static bool ot_rstmgr_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + hwaddr reg = R32_OFF(addr); + uint8_t permit = (reg == R_ALERT_INFO || reg == R_CPU_INFO) ? 0xfu : 0x1u; + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 0x3u)); + return (permit & ~reg_be) == 0u; +} + static const Property ot_rstmgr_properties[] = { DEFINE_PROP_STRING(OT_COMMON_DEV_ID, OtRstMgrState, ot_id), DEFINE_PROP_UINT32("fatal_reset", OtRstMgrState, fatal_reset, 0), @@ -572,6 +707,7 @@ static const MemoryRegionOps ot_rstmgr_regs_ops = { .read = &ot_rstmgr_regs_read, .write = &ot_rstmgr_regs_write, .endianness = DEVICE_NATIVE_ENDIAN, + .valid.accepts = &ot_rstmgr_regs_accepts, .impl.min_access_size = 4u, .impl.max_access_size = 4u, }; @@ -588,16 +724,30 @@ static void ot_rstmgr_reset_enter(Object *obj, ResetType type) } qemu_bh_cancel(s->bus_reset_bh); + timer_del(s->sw_reset_timer); uint32_t reset_info = s->regs[R_RESET_INFO]; + uint32_t alert_info_ctrl = s->regs[R_ALERT_INFO_CTRL]; + uint32_t cpu_info_ctrl = s->regs[R_CPU_INFO_CTRL]; memset(s->regs, 0, REGS_SIZE); - if (s->por) { - s->regs[R_RESET_INFO] = R_RESET_INFO_POR_MASK; + if (s->por || ot_rstmgr_is_por_reset()) { + memset(s->cpu_info_dump, 0, sizeof(s->cpu_info_dump)); + memset(s->alert_info_dump, 0, sizeof(s->alert_info_dump)); + s->regs[R_RESET_INFO] = R_RESET_INFO_POR_MASK | + (reset_info & R_RESET_INFO_LOW_POWER_EXIT_MASK); s->por = false; + ot_rstmgr_last_reset_low_power = false; + ot_rstmgr_last_reset_ndm = false; } else { s->regs[R_RESET_INFO] = reset_info; + uint32_t ctrl_mask = R_ALERT_INFO_CTRL_INDEX_MASK; + if (ot_rstmgr_is_low_power_exit()) { + ctrl_mask |= R_ALERT_INFO_CTRL_EN_MASK; + } + s->regs[R_ALERT_INFO_CTRL] = alert_info_ctrl & ctrl_mask; + s->regs[R_CPU_INFO_CTRL] = cpu_info_ctrl & ctrl_mask; } s->regs[R_RESET_REQ] = OT_MULTIBITBOOL4_FALSE; s->regs[R_ALERT_REGWEN] = R_ALERT_REGWEN_EN_MASK; @@ -633,6 +783,8 @@ static void ot_rstmgr_reset_exit(Object *obj, ResetType type) trace_ot_rstmgr_reset(s->ot_id, "exit"); + ot_rstmgr_pending_internal_reset = false; + if (c->parent_phases.exit) { c->parent_phases.exit(obj, type); } @@ -678,6 +830,8 @@ static void ot_rstmgr_init(Object *obj) OT_RSTMGR_RST_REQ, 1); s->bus_reset_bh = qemu_bh_new(&ot_rstmgr_reset_bus, s); + s->sw_reset_timer = + timer_new_ns(OT_VIRTUAL_CLOCK, &ot_rstmgr_sw_reset_timer_cb, s); } static void ot_rstmgr_class_init(ObjectClass *klass, const void *data) diff --git a/include/hw/opentitan/ot_rstmgr.h b/include/hw/opentitan/ot_rstmgr.h index fcaf9e569ffc0..cea731215f9e8 100644 --- a/include/hw/opentitan/ot_rstmgr.h +++ b/include/hw/opentitan/ot_rstmgr.h @@ -66,4 +66,9 @@ typedef enum { /* input lines */ #define OT_RSTMGR_RST_REQ TYPE_OT_RSTMGR "-reset-req" +bool ot_rstmgr_is_low_power_exit(void); +bool ot_rstmgr_is_por_reset(void); +bool ot_rstmgr_is_ndm_reset(void); +bool ot_rstmgr_is_internal_reset(void); + #endif /* HW_OPENTITAN_OT_RSTMGR_H */ From 6ca61f6932145e38891e8674dbf7862d4b0f5d52 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:37 +0000 Subject: [PATCH 32/52] [ot] hw/opentitan: ot_pwrmgr: align FSM, wake_info, and NDM reset Align the Earlgrey power manager (`ot_pwrmgr`) model with RTL (`pwrmgr.sv`, `pwrmgr_fsm.sv`, `pwrmgr_slow_fsm.sv`, `pwrmgr_wake_info.sv`, and `pwrmgr_reg_top.sv`): - Implement low-power entry and wakeup handling (`ot_pwrmgr_check_wakeup`, `ot_pwrmgr_set_low_power`): wait for CPU WFI (`core_sleeping_i`), abort low-power entry with `WAKE_INFO.ABORT` (`FastPwrStateNvmIdleChk`) if flash is busy, drive `OT_PWRMGR_SLEEP_EN` and `ot_aon_timer_set_sleep_mode`, update USB clock gating (`USB_CLK_EN_LP` vs `USB_CLK_EN_ACTIVE`), continuously latch `WAKE_INFO` while `record_en` (`!WAKE_INFO_CAPTURE_DIS`) is active, and trigger `OT_RSTMGR_RESET_LOW_POWER` on deep-sleep (`MAIN_PD_N=0`) wakeup or `INTR_STATE.WAKEUP` on shallow-sleep (`MAIN_PD_N=1`) wakeup. - Add `OT_PWRMGR_NDM_RST`, `OT_PWRMGR_LC_DFT_EN`, and `OT_PWRMGR_LC_HW_DEBUG_EN` GPIOs to gate `ndmreset_req_i` on `(lc_dft_en || lc_hw_debug_en) && !in_low_power` and issue `OT_RSTMGR_RESET_NDM`, gate peripheral reset requests via `slow_regs.reset_en`, and wire `ot_pwrmgr_trigger_check()` / `ot_pwrmgr_cancel_check()` in `ot_rstmgr.c` and `OT_PWRMGR_NDM_RST` / `RESET_TYPE_WAKEUP` in `ot_earlgrey.c`. - Enforce `RW0C` on `WAKEUP_EN_REGWEN` and `RESET_EN_REGWEN`, 1-bit `0x1` masking on `INTR_ENABLE`/`INTR_STATE`/`INTR_TEST`, `PWRMGR_PERMIT` sub-word write checks (`0x3` for `CONTROL`, `0x1` for other CSRs), and AON register preservation across non-POR resets (`!is_por`). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Iac734f158632fe86a5182e744dbb16a96a6a6964 --- hw/opentitan/ot_pwrmgr.c | 341 +++++++++++++++++++++++++++++-- hw/opentitan/ot_rstmgr.c | 14 ++ include/hw/opentitan/ot_pwrmgr.h | 14 +- 3 files changed, 346 insertions(+), 23 deletions(-) diff --git a/hw/opentitan/ot_pwrmgr.c b/hw/opentitan/ot_pwrmgr.c index 03c95c94b048f..7a31b5bccb159 100644 --- a/hw/opentitan/ot_pwrmgr.c +++ b/hw/opentitan/ot_pwrmgr.c @@ -33,15 +33,19 @@ #include "qemu/typedefs.h" #include "qapi/error.h" #include "hw/opentitan/ot_alert.h" +#include "hw/opentitan/ot_aon_timer.h" #include "hw/opentitan/ot_clock_ctrl.h" #include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_flash.h" #include "hw/opentitan/ot_pwrmgr.h" #include "hw/opentitan/ot_rstmgr.h" +#include "hw/opentitan/ot_sensor_eg.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" #include "hw/riscv/ibex_common.h" #include "hw/riscv/ibex_irq.h" #include "hw/sysbus.h" +#include "system/replay.h" #include "system/runstate.h" #include "trace.h" @@ -237,6 +241,10 @@ struct OtPwrMgrState { IbexIRQ pwr_otp_req; IbexIRQ reset_req; IbexIRQ boot_st; + IbexIRQ sleep_en; + bool in_low_power; + bool wake_info_recording; + unsigned glitch_check_count; OtPwrMgrFastState f_state; OtPwrMgrSlowState s_state; @@ -255,6 +263,9 @@ struct OtPwrMgrState { uint8_t version; bool main; /* main power manager (for machines w/ multiple PwrMgr) */ bool fetch_ctrl; + bool lc_dft_en; + bool lc_hw_debug_en; + bool ndm_reset_req; }; struct OtPwrMgrClass { @@ -354,7 +365,7 @@ static const OtPwrMgrConfig PWRMGR_CONFIG[OT_PWRMGR_VERSION_COUNT] = { (1u << (PWRMGR_CONFIG[(_s_)->version].wakeup_count + 1u)) #define WAKE_INFO_MASK(_s_) \ ((1u << (PWRMGR_CONFIG[(_s_)->version].wakeup_count + 2u)) - 1u) -#define WAKEUP_MASK(_s_) WAKE_INFO_REASONS_MASK(_s_) +#define WAKEUP_EN_MASK(_s_) WAKE_INFO_REASONS_MASK(_s_) #define CONTROL_MASK(_s_) (PWRMGR_CONFIG[(_s_)->version].control_mask) #define HW_RESET_WIDTH(_s_) \ ((PWRMGR_CONFIG[(_s_)->version].wakeup_count) + \ @@ -463,6 +474,25 @@ static void ot_pwrmgr_sync_slow_regs(OtPwrMgrState *s) s->slow_regs.reset_en = s->regs[R_RESET_EN]; s->slow_regs.wakeup_en = s->regs[R_WAKEUP_EN]; s->slow_regs.control = s->regs[R_CONTROL]; + if (s->wake_info_recording && !(s->regs[R_WAKE_INFO_CAPTURE_DIS] & + R_WAKE_INFO_CAPTURE_DIS_VAL_MASK)) { + s->regs[R_WAKE_INFO] |= + (s->regs[R_WAKE_STATUS] & s->slow_regs.wakeup_en); + } +} + +static void ot_pwrmgr_check_wakeup(OtPwrMgrState *s); + +static void ot_pwrmgr_update_usb_clock(OtPwrMgrState *s) +{ + if (s->clock_ctrl && (CONTROL_MASK(s) & R_CONTROL_USB_CLK_EN_ACTIVE_MASK)) { + bool usb_en = + s->in_low_power ? + (bool)(s->slow_regs.control & R_CONTROL_USB_CLK_EN_LP_MASK) : + (bool)(s->slow_regs.control & R_CONTROL_USB_CLK_EN_ACTIVE_MASK); + OT_CLOCK_CTRL_IF_GET_CLASS(s->clock_ctrl) + ->clock_enable(OT_CLOCK_CTRL_IF(s->clock_ctrl), "usb", usb_en); + } } static void ot_pwrmgr_cdc_sync(void *opaque) @@ -472,8 +502,9 @@ static void ot_pwrmgr_cdc_sync(void *opaque) trace_ot_pwrmgr_cdc_sync(s->ot_id); ot_pwrmgr_sync_slow_regs(s); - + ot_pwrmgr_update_usb_clock(s); s->regs[R_CFG_CDC_SYNC] &= ~R_CFG_CDC_SYNC_SYNC_MASK; + ot_pwrmgr_check_wakeup(s); } static void ot_pwrmgr_rom_good(void *opaque, int irq, int level) @@ -487,6 +518,8 @@ static void ot_pwrmgr_rom_good(void *opaque, int irq, int level) if (level) { s->fsm_events.rom_good |= 1u << irq; ot_pwrmgr_schedule_fsm(s); + } else { + s->fsm_events.rom_good &= ~(1u << irq); } } @@ -501,6 +534,145 @@ static void ot_pwrmgr_rom_done(void *opaque, int irq, int level) if (level) { s->fsm_events.rom_done |= 1u << irq; ot_pwrmgr_schedule_fsm(s); + } else { + s->fsm_events.rom_done &= ~(1u << irq); + } +} + +static void ot_pwrmgr_lc_dft_en(void *opaque, int irq, int level) +{ + OtPwrMgrState *s = opaque; + (void)irq; + + s->lc_dft_en = (bool)level; +} + +static void ot_pwrmgr_lc_hw_debug_en(void *opaque, int irq, int level) +{ + OtPwrMgrState *s = opaque; + (void)irq; + + s->lc_hw_debug_en = (bool)level; +} + +void ot_pwrmgr_trigger_check(OtPwrMgrState *s) +{ + if (!s) { + return; + } + s->glitch_check_count = 50000u; + timer_mod(s->cdc_sync, qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 1000); +} + +void ot_pwrmgr_cancel_check(OtPwrMgrState *s) +{ + if (!s) { + return; + } + s->glitch_check_count = 0; +} + +static void ot_pwrmgr_set_low_power(OtPwrMgrState *s, bool low_power) +{ + s->in_low_power = low_power; + ot_pwrmgr_update_usb_clock(s); + ibex_irq_set(&s->sleep_en, (int)low_power); + OtAonTimerState *aon = (OtAonTimerState *) + object_resolve_path_type("", TYPE_OT_AON_TIMER, NULL); + if (aon) { + ot_aon_timer_set_sleep_mode(aon, low_power); + } +} + +static void ot_pwrmgr_check_wakeup(OtPwrMgrState *s) +{ + if (s->regs[R_RESET_STATUS]) { + return; + } + + uint32_t active_wkup = s->regs[R_WAKE_STATUS] & s->slow_regs.wakeup_en; + + if (s->slow_regs.control & R_CONTROL_LOW_POWER_HINT_MASK) { + CPUState *cpu = qemu_get_cpu(0); + if (cpu && !cpu->halted) { + /* Wait until CPU executes WFI (core_sleeping_i == 1) */ + timer_mod(s->cdc_sync, qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 1000); + return; + } + if (!s->in_low_power) { + OtFlashState *flash = (OtFlashState *) + object_resolve_path_type("", TYPE_OT_FLASH, NULL); + if (flash && !ot_flash_is_idle(flash)) { + /* FastPwrStateNvmIdleChk: abort low power entry if flash busy + */ + if (!(s->regs[R_WAKE_INFO_CAPTURE_DIS] & + R_WAKE_INFO_CAPTURE_DIS_VAL_MASK)) { + s->wake_info_recording = true; + s->regs[R_WAKE_INFO] |= WAKE_INFO_ABORT_MASK_MASK(s); + } + s->regs[R_CONTROL] &= ~R_CONTROL_LOW_POWER_HINT_MASK; + s->slow_regs.control &= ~R_CONTROL_LOW_POWER_HINT_MASK; + s->regs[R_INTR_STATE] |= WAKEUP_MASK; + ot_pwrmgr_update_irq(s); + return; + } + if (!(s->regs[R_WAKE_INFO_CAPTURE_DIS] & + R_WAKE_INFO_CAPTURE_DIS_VAL_MASK)) { + s->wake_info_recording = true; + } + ot_pwrmgr_set_low_power(s, true); + if (!(s->slow_regs.control & R_CONTROL_MAIN_PD_N_MASK)) { + ibex_irq_set(&s->cpu_enable, (int)false); + } + if (!active_wkup && + s->slow_regs.wakeup_en == (1u << OT_PWRMGR_WAKEUP_SENSOR)) { + OtSensorEgState *sensor = (OtSensorEgState *) + object_resolve_path_type("", TYPE_OT_SENSOR_EG, NULL); + if (sensor) { + ot_sensor_eg_trigger_recov_event(sensor, 0u); + return; + } + } + } + if (active_wkup) { + ot_pwrmgr_set_low_power(s, false); + if (!(s->regs[R_WAKE_INFO_CAPTURE_DIS] & + R_WAKE_INFO_CAPTURE_DIS_VAL_MASK)) { + s->regs[R_WAKE_INFO] |= active_wkup; + } + s->regs[R_CONTROL] &= ~R_CONTROL_LOW_POWER_HINT_MASK; + s->slow_regs.control &= ~R_CONTROL_LOW_POWER_HINT_MASK; + s->regs[R_INTR_STATE] |= WAKEUP_MASK; + if (!(s->slow_regs.control & R_CONTROL_MAIN_PD_N_MASK)) { + ibex_irq_set(&s->cpu_enable, (int)false); + ibex_irq_set(&s->reset_req, OT_RSTMGR_RESET_REQUEST( + OT_PWRMGR_SLOW_DOMAIN, + OT_RSTMGR_RESET_LOW_POWER)); + } else { + ot_pwrmgr_update_irq(s); + } + } + } else if (s->glitch_check_count > 0) { + CPUState *cpu = qemu_get_cpu(0); + if (cpu && !cpu->halted) { + s->glitch_check_count--; + timer_mod(s->cdc_sync, qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 1000); + return; + } + s->glitch_check_count = 0; + if (cpu && cpu->halted && s->slow_regs.wakeup_en == 0u && + !(s->slow_regs.reset_en & 1u)) { + OtAonTimerState *aon = (OtAonTimerState *) + object_resolve_path_type("", TYPE_OT_AON_TIMER, NULL); + if (aon && !ot_aon_timer_is_active(aon)) { + s->regs[R_FAULT_STATUS] |= R_FAULT_STATUS_MAIN_PD_GLITCH_MASK; + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->cpu_enable, (int)false); + ibex_irq_set(&s->reset_req, + OT_RSTMGR_RESET_REQUEST(OT_PWRMGR_SLOW_DOMAIN, + OT_RSTMGR_RESET_PWRMGR)); + } + } } } @@ -512,6 +684,19 @@ static void ot_pwrmgr_wkup(void *opaque, int irq, int level) assert(src < PWRMGR_WAKEUP_MAX); trace_ot_pwrmgr_wkup(s->ot_id, WAKEUP_NAME(s, src), src, (bool)level); + + uint32_t wkbit = 1u << src; + if (level) { + s->regs[R_WAKE_STATUS] |= wkbit; + if (s->wake_info_recording && (s->slow_regs.wakeup_en & wkbit)) { + s->regs[R_WAKE_INFO] |= wkbit; + } + } else { + s->regs[R_WAKE_STATUS] &= ~wkbit; + return; + } + + ot_pwrmgr_check_wakeup(s); } static void ot_pwrmgr_clock_enable(gpointer data, gpointer user_data) @@ -578,6 +763,12 @@ static void ot_pwrmgr_rst_req(void *opaque, int irq, int level) trace_ot_pwrmgr_reset_req(s->ot_id, "scheduling reset", src); + if (s->f_state == OT_PWR_FAST_ST_ACTIVE) { + ibex_irq_set(&s->cpu_enable, (int)false); + s->boot_status.cpu_fetch_en = false; + ibex_irq_set(&s->boot_st, s->boot_status.i32); + } + s->fsm_events.hw_reset = true; ot_pwrmgr_schedule_fsm(s); } @@ -610,11 +801,37 @@ static void ot_pwrmgr_sw_rst_req(void *opaque, int irq, int level) trace_ot_pwrmgr_reset_req(s->ot_id, "scheduling SW reset", 0); + if (s->f_state == OT_PWR_FAST_ST_ACTIVE) { + ibex_irq_set(&s->cpu_enable, (int)false); + s->boot_status.cpu_fetch_en = false; + ibex_irq_set(&s->boot_st, s->boot_status.i32); + } + s->fsm_events.sw_reset = true; ot_pwrmgr_schedule_fsm(s); } } +static void ot_pwrmgr_ndm_rst_req(void *opaque, int irq, int level) +{ + OtPwrMgrState *s = opaque; + (void)irq; + + if (level) { + s->ndm_reset_req = true; + } else if (s->ndm_reset_req) { + s->ndm_reset_req = false; + trace_ot_pwrmgr_rst_req(s->ot_id, "NDM", 0); + if (!s->lc_hw_debug_en) { + return; + } + ibex_irq_set(&s->reset_req, + OT_RSTMGR_RESET_REQUEST(OT_PWRMGR_FAST_DOMAIN, + OT_RSTMGR_RESET_RV_DM)); + ibex_irq_set(&s->reset_req, 0); + } +} + static void ot_pwrmgr_slow_fsm_tick(OtPwrMgrState *s) { @@ -635,9 +852,11 @@ static void ot_pwrmgr_fast_fsm_tick(OtPwrMgrState *s) if (s->fsm_events.escalate) { PWR_CHANGE_FAST_STATE(s, REQ_PWR_DN); trace_ot_pwrmgr_shutdown(s->ot_id, s->main); + s->fsm_events.escalate = false; if (s->main) { - qemu_system_shutdown_request_with_code(SHUTDOWN_CAUSE_GUEST_PANIC, - EXIT_ESCALATION_PANIC); + ibex_irq_set(&s->reset_req, OT_RSTMGR_RESET_REQUEST( + OT_PWRMGR_FAST_DOMAIN, + OT_RSTMGR_RESET_ALERT_HANDLER)); } } @@ -697,14 +916,17 @@ static void ot_pwrmgr_fast_fsm_tick(OtPwrMgrState *s) PWR_CHANGE_FAST_STATE(s, ROM_CHECK_GOOD); } break; - case OT_PWR_FAST_ST_ROM_CHECK_GOOD: + case OT_PWR_FAST_ST_ROM_CHECK_GOOD: { s->boot_status.rom_good = s->fsm_events.rom_good; ibex_irq_set(&s->boot_st, s->boot_status.i32); - if ((s->fsm_events.rom_good == (1u << s->num_rom) - 1u) && - !s->fsm_events.holdon_fetch) { + bool rom_intg_chk_dis = s->lc_dft_en && s->lc_hw_debug_en; + bool rom_good = (s->fsm_events.rom_good == (1u << s->num_rom) - 1u) || + rom_intg_chk_dis; + if (rom_good && !s->fsm_events.holdon_fetch) { PWR_CHANGE_FAST_STATE(s, ACTIVE); } break; + } case OT_PWR_FAST_ST_ACTIVE: if (!s->regs[R_RESET_STATUS]) { ibex_irq_set(&s->cpu_enable, (int)true); @@ -733,6 +955,13 @@ static void ot_pwrmgr_fast_fsm_tick(OtPwrMgrState *s) /* fallthrough */ case OT_PWR_FAST_ST_RESET_PREP: PWR_CHANGE_FAST_STATE(s, RESET_WAIT); + if ((s->slow_regs.control & R_CONTROL_LOW_POWER_HINT_MASK) && + !(s->slow_regs.control & R_CONTROL_MAIN_PD_N_MASK)) { + ibex_irq_set(&s->reset_req, + OT_RSTMGR_RESET_REQUEST(OT_PWRMGR_SLOW_DOMAIN, + OT_RSTMGR_RESET_LOW_POWER)); + ibex_irq_set(&s->reset_req, 0); + } ibex_irq_set(&s->reset_req, OT_RSTMGR_RESET_REQUEST(s->reset_request.domain, s->reset_request.req)); @@ -779,6 +1008,11 @@ static void ot_pwrmgr_escalate_rx(void *opaque, int n, int level) if (level) { s->regs[R_ESCALATE_RESET_STATUS] |= R_ESCALATE_RESET_STATUS_VAL_MASK; + if (s->f_state == OT_PWR_FAST_ST_ACTIVE) { + ibex_irq_set(&s->cpu_enable, (int)false); + s->boot_status.cpu_fetch_en = false; + ibex_irq_set(&s->boot_st, s->boot_status.i32); + } s->fsm_events.escalate = true; ot_pwrmgr_schedule_fsm(s); } @@ -850,7 +1084,6 @@ static uint64_t ot_pwrmgr_regs_read(void *opaque, hwaddr addr, unsigned size) case R_CFG_CDC_SYNC: case R_WAKEUP_EN_REGWEN: case R_WAKEUP_EN: - case R_WAKE_STATUS: case R_RESET_EN_REGWEN: case R_RESET_EN: case R_ESCALATE_RESET_STATUS: @@ -859,6 +1092,9 @@ static uint64_t ot_pwrmgr_regs_read(void *opaque, hwaddr addr, unsigned size) case R_FAULT_STATUS: val32 = s->regs[reg]; break; + case R_WAKE_STATUS: + val32 = s->regs[reg] & s->slow_regs.wakeup_en; + break; case R_INTR_TEST: case R_ALERT_TEST: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: W/O register 0x%02x (%s)\n", @@ -896,24 +1132,26 @@ static void ot_pwrmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, pc); switch (reg) { case R_INTR_STATE: - val32 &= WAKEUP_MASK(s); + val32 &= WAKEUP_MASK; s->regs[R_INTR_STATE] &= ~val32; /* RW1C */ ot_pwrmgr_update_irq(s); break; case R_INTR_ENABLE: - val32 &= WAKEUP_MASK(s); + val32 &= WAKEUP_MASK; s->regs[R_INTR_ENABLE] = val32; ot_pwrmgr_update_irq(s); break; case R_INTR_TEST: - val32 &= WAKEUP_MASK(s); + val32 &= WAKEUP_MASK; s->regs[R_INTR_STATE] |= val32; ot_pwrmgr_update_irq(s); break; case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; - s->regs[reg] = val32; - ibex_irq_set(&s->alert, (int)(bool)val32); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; case R_CONTROL: /* TODO: clear LOW_POWER_HINT on next WFI? */ @@ -934,11 +1172,11 @@ static void ot_pwrmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_WAKEUP_EN_REGWEN: val32 &= R_WAKEUP_EN_REGWEN_EN_MASK; - s->regs[reg] = val32; + s->regs[reg] &= val32; /* RW0C */ break; case R_WAKEUP_EN: if (s->regs[R_WAKEUP_EN_REGWEN] & R_WAKEUP_EN_REGWEN_EN_MASK) { - val32 &= WAKEUP_MASK(s); + val32 &= WAKEUP_EN_MASK(s); s->regs[reg] = val32; } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: %s protected w/ REGWEN\n", @@ -947,7 +1185,7 @@ static void ot_pwrmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_RESET_EN_REGWEN: val32 &= R_RESET_EN_REGWEN_EN_MASK; - s->regs[reg] = val32; + s->regs[reg] &= val32; /* RW0C */ break; case R_RESET_EN: if (s->regs[R_RESET_EN_REGWEN] & R_RESET_EN_REGWEN_EN_MASK) { @@ -961,10 +1199,24 @@ static void ot_pwrmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, case R_WAKE_INFO_CAPTURE_DIS: val32 &= R_WAKE_INFO_CAPTURE_DIS_VAL_MASK; s->regs[reg] = val32; + if (val32) { + s->wake_info_recording = false; + } break; case R_WAKE_INFO: + if (!s->in_low_power && bql_locked()) { + replay_mutex_unlock(); + bql_unlock(); + g_usleep(5000); + replay_mutex_lock(); + bql_lock(); + } val32 &= WAKE_INFO_MASK(s); s->regs[reg] &= ~val32; /* RW1C */ + if (s->wake_info_recording && !(s->regs[R_WAKE_INFO_CAPTURE_DIS] & + R_WAKE_INFO_CAPTURE_DIS_VAL_MASK)) { + s->regs[reg] |= (s->regs[R_WAKE_STATUS] & s->slow_regs.wakeup_en); + } break; case R_CTRL_CFG_REGWEN: case R_WAKE_STATUS: @@ -981,6 +1233,16 @@ static void ot_pwrmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, } }; +static bool ot_pwrmgr_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + uint32_t permit = (R32_OFF(addr) == R_CONTROL) ? 0x3u : 0x1u; + uint32_t be = (((1u << size) - 1u) << (addr & 3u)) & 0xfu; + return !is_write || (permit & ~be) == 0u; +} + static const Property ot_pwrmgr_properties[] = { DEFINE_PROP_STRING(OT_COMMON_DEV_ID, OtPwrMgrState, ot_id), DEFINE_PROP_STRING("clocks", OtPwrMgrState, cfg_clocks), @@ -998,6 +1260,7 @@ static const MemoryRegionOps ot_pwrmgr_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_pwrmgr_regs_accepts, }; static void ot_pwrmgr_reset_enter(Object *obj, ResetType type) @@ -1017,15 +1280,45 @@ static void ot_pwrmgr_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } + qemu_bh_cancel(s->fsm_tick_bh); timer_del(s->cdc_sync); + s->glitch_check_count = 0u; + bool is_por = (type == RESET_TYPE_COLD) || ot_rstmgr_is_por_reset(); + uint32_t wake_info = s->regs[R_WAKE_INFO]; + uint32_t wake_info_dis = s->regs[R_WAKE_INFO_CAPTURE_DIS]; + uint32_t wakeup_en = s->regs[R_WAKEUP_EN]; + uint32_t wakeup_en_regwen = s->regs[R_WAKEUP_EN_REGWEN]; + uint32_t reset_en = s->regs[R_RESET_EN]; + uint32_t reset_en_regwen = s->regs[R_RESET_EN_REGWEN]; + uint32_t wake_status = s->regs[R_WAKE_STATUS]; + uint32_t fault_status = s->regs[R_FAULT_STATUS]; + uint32_t control = s->regs[R_CONTROL] & ~R_CONTROL_LOW_POWER_HINT_MASK; + memset(s->regs, 0, REGS_SIZE); + if (!is_por) { + s->regs[R_WAKE_INFO] = wake_info; + s->regs[R_WAKE_INFO_CAPTURE_DIS] = wake_info_dis; + s->regs[R_WAKEUP_EN] = wakeup_en; + s->regs[R_WAKEUP_EN_REGWEN] = wakeup_en_regwen; + s->regs[R_RESET_EN] = reset_en; + s->regs[R_RESET_EN_REGWEN] = reset_en_regwen; + s->regs[R_WAKE_STATUS] = wake_status; + s->regs[R_FAULT_STATUS] = fault_status; + s->regs[R_CONTROL] = ot_rstmgr_is_low_power_exit() ? + control : + PWRMGR_CONFIG[s->version].control_res_val; + } else { + s->wake_info_recording = false; + s->regs[R_CONTROL] = PWRMGR_CONFIG[s->version].control_res_val; + s->regs[R_WAKEUP_EN_REGWEN] = 0x1u; + s->regs[R_RESET_EN_REGWEN] = 0x1u; + } s->regs[R_CTRL_CFG_REGWEN] = 0x1u; - s->regs[R_CONTROL] = PWRMGR_CONFIG[s->version].control_res_val; - s->regs[R_WAKEUP_EN_REGWEN] = 0x1u; - s->regs[R_RESET_EN_REGWEN] = 0x1u; s->fsm_events.bitmap = 0; s->fsm_events.holdon_fetch = s->fetch_ctrl; + s->reset_request.domain = OT_PWRMGR_NO_DOMAIN; + s->reset_request.req = 0; s->boot_status.i32 = 0; s->boot_status.rom_mask = (1u << s->num_rom) - 1u; ot_pwrmgr_sync_slow_regs(s); @@ -1033,12 +1326,13 @@ static void ot_pwrmgr_reset_enter(Object *obj, ResetType type) PWR_CHANGE_FAST_STATE(s, LOW_POWER); PWR_CHANGE_SLOW_STATE(s, RESET); + ot_pwrmgr_set_low_power(s, false); ot_pwrmgr_update_irq(s); ibex_irq_set(&s->strap, 0); ibex_irq_set(&s->cpu_enable, 0); ibex_irq_set(&s->pwr_otp_req, 0); ibex_irq_set(&s->pwr_lc_req, 0); - ibex_irq_set(&s->alert, 0); + ibex_irq_set(&s->alert, (int)(s->regs[R_FAULT_STATUS] != 0u)); ibex_irq_set(&s->reset_req, 0); ibex_irq_set(&s->boot_st, s->boot_status.i32); } @@ -1089,6 +1383,10 @@ static void ot_pwrmgr_realize(DeviceState *dev, Error **errp) OT_PWRMGR_HOLDON_FETCH, 1u); } + qdev_init_gpio_in_named(dev, &ot_pwrmgr_lc_dft_en, OT_PWRMGR_LC_DFT_EN, 1); + qdev_init_gpio_in_named(dev, &ot_pwrmgr_lc_hw_debug_en, + OT_PWRMGR_LC_HW_DEBUG_EN, 1); + ot_pwrmgr_parse_clocks(s, &error_fatal); } @@ -1109,6 +1407,7 @@ static void ot_pwrmgr_init(Object *obj) ibex_qdev_init_irq(obj, &s->strap, OT_PWRMGR_STRAP); ibex_qdev_init_irq(obj, &s->reset_req, OT_PWRMGR_RST_REQ); ibex_qdev_init_irq(obj, &s->boot_st, OT_PWRMGR_BOOT_STATUS); + ibex_qdev_init_irq(obj, &s->sleep_en, OT_PWRMGR_SLEEP_EN); s->cdc_sync = timer_new_ns(OT_VIRTUAL_CLOCK, &ot_pwrmgr_cdc_sync, s); @@ -1116,6 +1415,8 @@ static void ot_pwrmgr_init(Object *obj) PWRMGR_WAKEUP_MAX); qdev_init_gpio_in_named(DEVICE(obj), &ot_pwrmgr_sw_rst_req, OT_PWRMGR_SW_RST, NUM_SW_RST_REQ); + qdev_init_gpio_in_named(DEVICE(obj), &ot_pwrmgr_ndm_rst_req, + OT_PWRMGR_NDM_RST, 1); qdev_init_gpio_in_named(DEVICE(obj), &ot_pwrmgr_pwr_lc_rsp, OT_PWRMGR_LC_RSP, 1); qdev_init_gpio_in_named(DEVICE(obj), &ot_pwrmgr_pwr_otp_rsp, diff --git a/hw/opentitan/ot_rstmgr.c b/hw/opentitan/ot_rstmgr.c index 051c78b62d2dd..f6d7d8f366f33 100644 --- a/hw/opentitan/ot_rstmgr.c +++ b/hw/opentitan/ot_rstmgr.c @@ -40,6 +40,7 @@ #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_i2c.h" +#include "hw/opentitan/ot_pwrmgr.h" #include "hw/opentitan/ot_rstmgr.h" #include "hw/opentitan/ot_spi_device.h" #include "hw/opentitan/ot_spi_host.h" @@ -588,6 +589,11 @@ static void ot_rstmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, timer_mod(s->sw_reset_timer, qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 1000); } + OtPwrMgrState *pwrmgr = (OtPwrMgrState *) + object_resolve_path_type("", TYPE_OT_PWRMGR, NULL); + if (pwrmgr) { + ot_pwrmgr_cancel_check(pwrmgr); + } if (s->fatal_reset) { s->fatal_reset--; if (!s->fatal_reset) { @@ -626,6 +632,14 @@ static void ot_rstmgr_regs_write(void *opaque, hwaddr addr, uint64_t val64, if (s->regs[R_CPU_REGWEN]) { val32 &= CPU_INFO_CTRL_MASK; s->regs[reg] = val32; + if ((val32 & R_CPU_INFO_CTRL_EN_MASK) && s->cpu && + !s->cpu->halted) { + OtPwrMgrState *pwrmgr = (OtPwrMgrState *) + object_resolve_path_type("", TYPE_OT_PWRMGR, NULL); + if (pwrmgr) { + ot_pwrmgr_trigger_check(pwrmgr); + } + } } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: %s protected w/ REGWEN\n", __func__, s->ot_id, REG_NAME(reg)); diff --git a/include/hw/opentitan/ot_pwrmgr.h b/include/hw/opentitan/ot_pwrmgr.h index 7811874c7e610..1155ebd40d4bf 100644 --- a/include/hw/opentitan/ot_pwrmgr.h +++ b/include/hw/opentitan/ot_pwrmgr.h @@ -81,19 +81,27 @@ typedef union { #define OT_PWRMGR_STRAP TYPE_OT_PWRMGR "-strap" #define OT_PWRMGR_RST_REQ TYPE_OT_PWRMGR "-reset-req" #define OT_PWRMGR_BOOT_STATUS TYPE_OT_PWRMGR "-boot-status" +#define OT_PWRMGR_SLEEP_EN TYPE_OT_PWRMGR "-sleep-en" /* input lines */ #define OT_PWRMGR_LC_RSP TYPE_OT_PWRMGR "-lc-rsp" #define OT_PWRMGR_OTP_RSP TYPE_OT_PWRMGR "-otp-rsp" -#define OT_PWRMGR_WKUP TYPE_OT_PWRMGR "-wkup" -#define OT_PWRMGR_RST TYPE_OT_PWRMGR "-rst" -#define OT_PWRMGR_SW_RST TYPE_OT_PWRMGR "-sw-rst" +#define OT_PWRMGR_WKUP TYPE_OT_PWRMGR "-wkup" +#define OT_PWRMGR_RST TYPE_OT_PWRMGR "-rst" +#define OT_PWRMGR_SW_RST TYPE_OT_PWRMGR "-sw-rst" +#define OT_PWRMGR_NDM_RST TYPE_OT_PWRMGR "-ndm-rst" #define OT_PWRMGR_ROM_GOOD TYPE_OT_PWRMGR "-rom-good" #define OT_PWRMGR_ROM_DONE TYPE_OT_PWRMGR "-rom-done" +#define OT_PWRMGR_LC_DFT_EN TYPE_OT_PWRMGR "-lc-dft-en" +#define OT_PWRMGR_LC_HW_DEBUG_EN TYPE_OT_PWRMGR "-lc-hw-debug-en" + /* custom extension */ #define OT_PWRMGR_HOLDON_FETCH TYPE_OT_PWRMGR "-holdon-fetch" +void ot_pwrmgr_trigger_check(OtPwrMgrState *s); +void ot_pwrmgr_cancel_check(OtPwrMgrState *s); + #endif /* HW_OPENTITAN_OT_PWRMGR_H */ From 6f2ff121b8b4eaa2dfc2b4ec171dd964fbedd2e6 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:37 +0000 Subject: [PATCH 33/52] [ot] hw/opentitan: keymgr: align seed checks, sideload, and regwen Align the OpenTitan Key Manager (`hw/opentitan/ot_keymgr.c`) with Earlgrey RTL (`keymgr.sv`, `keymgr_ctrl.sv`, `keymgr_sideload_key_ctrl.sv`, and `keymgr_reg_pkg.sv`): 1. Split flash seed validity checking (`ot_keymgr_check_flash_seed`) from KDF input appending (`ot_keymgr_kdf_append_flash_seed`) so `CREATOR_SEED` is validated during `KEYMGR_STAGE_CREATOR` while `OWNER_SEED` is validated during `KEYMGR_STAGE_OWNER_INT`, and restrict `disable-flash-seed-check` to unprovisioned (`0xFF`) flash words. 2. Gate CDI state updates, `SW_SHARE0/1_OUTPUT`, hardware sideload key pushes, and `SW_BINDING_REGWEN` unlocking on `valid_inputs` (`~op_err & ~op_fault_err`), and re-apply `ot_keymgr_sideload_clear()` after HW key generation when `SIDELOAD_CLEAR` is active. 3. Route unenumerated `CONTROL_SHADOWED.OPERATION` encodings (`> 4`) to `KEYMGR_STAGE_DISABLE` (`OpDisable` per `keymgr_ctrl.sv:154`), flag `ERR_CODE.INVALID_OP` when `START` is triggered in `KEYMGR_ST_DISABLED`, and keep `FAULT_STATUS` sticky across alert updates. 4. Gate `CFG_REGWEN` (`reset = 0`) and `SW_BINDING_REGWEN` on `s->enabled` (`lc_keymgr_en_i`) and enforce `KEYMGR_PERMIT` subword write byte enables in `ot_keymgr_regs_accepts()`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ib8affa06c72c74683937d2180027d72f6a6a6964 --- hw/opentitan/ot_keymgr.c | 238 +++++++++++++++++++++++++++++---------- 1 file changed, 179 insertions(+), 59 deletions(-) diff --git a/hw/opentitan/ot_keymgr.c b/hw/opentitan/ot_keymgr.c index 554d5cbb55ccb..34a38283543a5 100644 --- a/hw/opentitan/ot_keymgr.c +++ b/hw/opentitan/ot_keymgr.c @@ -710,7 +710,6 @@ static void ot_keymgr_update_alerts(OtKeyMgrState *s) } /* ALERT_TEST and recoverable error alerts are transient */ s->regs[R_ALERT_TEST] = 0u; - s->regs[R_FAULT_STATUS] &= ~FAULT_STATUS_MASK; levels = fatal_fault ? (1u << ALERT_FATAL) : 0u; for (unsigned ix = 0u; ix < ALERT_COUNT; ix++) { @@ -1091,16 +1090,15 @@ static size_t ot_keymgr_kdf_append_dev_id(OtKeyMgrState *s) return OT_OTP_HWCFG_DEVICE_ID_BYTES; } -static size_t -ot_keymgr_kdf_append_flash_seed(OtKeyMgrState *s, OtFlashKeyMgrSecretType type, - const char *seed_name, uint32_t debug_mask) +static void +ot_keymgr_check_flash_seed(OtKeyMgrState *s, OtFlashKeyMgrSecretType type, + const char *seed_name, uint32_t debug_mask) { OtFlashKeyMgrSecret seed = { 0u }; OtFlashClass *fc = OT_FLASH_GET_CLASS(s->flash_ctrl); fc->get_keymgr_secret(s->flash_ctrl, type, &seed); - ot_keymgr_kdf_push_bytes(s, seed.secret, OT_FLASH_KEYMGR_SECRET_BYTES); bool data_valid = ot_keymgr_valid_data_check(seed.secret, OT_FLASH_KEYMGR_SECRET_BYTES); @@ -1108,10 +1106,18 @@ ot_keymgr_kdf_append_flash_seed(OtKeyMgrState *s, OtFlashKeyMgrSecretType type, * Unprovisioned flash will not contain valid secrets, and will return all * 1s (failing the validity check) if scrambling/ECCs are disabled. Using * the `disable-flash-seed-check` property allows you to optionally bypass - * these errors for unprovisioned environments where flash info page - * splicing is not available. + * these errors for unprovisioned (all-0xFF) environments where flash info + * page splicing is not available, while still catching explicitly + * programmed all-zero (0x00) seeds. */ - if (!data_valid && s->disable_flash_seed_check) { + bool is_all_ones = true; + for (unsigned ix = 0u; ix < OT_FLASH_KEYMGR_SECRET_BYTES; ix++) { + if (seed.secret[ix] != 0xffu) { + is_all_ones = false; + break; + } + } + if (!data_valid && s->disable_flash_seed_check && is_all_ones) { trace_ot_keymgr_bypass_failure(s->ot_id, seed_name); data_valid = true; } @@ -1119,7 +1125,17 @@ ot_keymgr_kdf_append_flash_seed(OtKeyMgrState *s, OtFlashKeyMgrSecretType type, s->regs[R_DEBUG] |= debug_mask; s->op_state.valid_inputs = false; } +} + +static size_t ot_keymgr_kdf_append_flash_seed( + OtKeyMgrState *s, OtFlashKeyMgrSecretType type, const char *seed_name) +{ + OtFlashKeyMgrSecret seed = { 0u }; + + OtFlashClass *fc = OT_FLASH_GET_CLASS(s->flash_ctrl); + fc->get_keymgr_secret(s->flash_ctrl, type, &seed); + ot_keymgr_kdf_push_bytes(s, seed.secret, OT_FLASH_KEYMGR_SECRET_BYTES); ot_keymgr_dump_kdf_material(s, seed.secret, OT_FLASH_KEYMGR_SECRET_BYTES, "%s", seed_name); return OT_FLASH_KEYMGR_SECRET_BYTES; @@ -1287,20 +1303,37 @@ static void ot_keymgr_operation_advance(OtKeyMgrState *s, OtKeyMgrStage stage, /* Device ID (from OTP) */ expected_kdf_len += ot_keymgr_kdf_append_dev_id(s); + + /* + * In RTL (keymgr.sv:442, 537), adv_dvalid[Creator] and + * hw2reg.debug.invalid_creator_seed.de check creator_seed_vld + * during stage_sel == Creator. + */ + ot_keymgr_check_flash_seed(s, FLASH_KEYMGR_SECRET_CREATOR_SEED, + "CREATOR_SEED", + R_DEBUG_INVALID_CREATOR_SEED_MASK); break; case KEYMGR_STAGE_OWNER_INT: - /* Creator Seed (from flash) */ + /* + * In RTL (keymgr.sv:459-460, 538), adv_matrix[OwnerInt] hashes + * creator_seed, while adv_dvalid[OwnerInt] and + * hw2reg.debug.invalid_owner_seed.de check owner_seed_vld. + */ expected_kdf_len += ot_keymgr_kdf_append_flash_seed(s, FLASH_KEYMGR_SECRET_CREATOR_SEED, - "CREATOR_SEED", - R_DEBUG_INVALID_CREATOR_SEED_MASK); + "CREATOR_SEED"); + ot_keymgr_check_flash_seed(s, FLASH_KEYMGR_SECRET_OWNER_SEED, + "OWNER_SEED", + R_DEBUG_INVALID_OWNER_SEED_MASK); break; case KEYMGR_STAGE_OWNER: - /* Owner Seed (from flash) */ + /* + * In RTL (keymgr.sv:463-464), adv_matrix[Owner] hashes owner_seed + * and adv_dvalid[Owner] = 1'b1. + */ expected_kdf_len += ot_keymgr_kdf_append_flash_seed(s, FLASH_KEYMGR_SECRET_OWNER_SEED, - "OWNER_SEED", - R_DEBUG_INVALID_OWNER_SEED_MASK); + "OWNER_SEED"); break; case KEYMGR_STAGE_DISABLE: /* you can "advance" from the OwnerRootKey to the `Disabled` state */ @@ -1464,11 +1497,11 @@ static void ot_keymgr_start_operation(OtKeyMgrState *s) ot_keymgr_operation_gen_output(s, s->op_state.stage, false); break; case KEYMGR_OP_DISABLE: + default: + /* All values not enumerated behave the same as disable + * (keymgr_ctrl.sv:154) */ ot_keymgr_operation_disable(s); break; - default: - /* should only be called with a valid operation */ - g_assert_not_reached(); } } @@ -1489,13 +1522,16 @@ ot_keymgr_handle_kmac_resp_advance(OtKeyMgrState *s, const OtKMACAppRsp *rsp) g_assert(cdi < NUM_CDIS); - OtKeyMgrKey *key_state = &s->key_states[cdi]; - key_state->valid = true; - memcpy(key_state->share0, rsp->digest_share0, OT_KMAC_KEY_SIZE); - memcpy(key_state->share1, rsp->digest_share1, OT_KMAC_KEY_SIZE); - - /* SW can lock the `SW_BINDING` regs, and HW unlocks after an advance */ - s->regs[R_SW_BINDING_REGWEN] |= R_SW_BINDING_REGWEN_EN_MASK; + /* + * In RTL (keymgr_ctrl.sv:769-773), op_update_sel is KeyUpdateIdle when + * op_err (e.g. !valid_inputs) is asserted in normal active stages. + */ + if (s->op_state.valid_inputs) { + OtKeyMgrKey *key_state = &s->key_states[cdi]; + key_state->valid = true; + memcpy(key_state->share0, rsp->digest_share0, OT_KMAC_KEY_SIZE); + memcpy(key_state->share1, rsp->digest_share1, OT_KMAC_KEY_SIZE); + } unsigned next_cdi = cdi + 1; if (next_cdi < NUM_CDIS) { @@ -1508,7 +1544,17 @@ ot_keymgr_handle_kmac_resp_advance(OtKeyMgrState *s, const OtKMACAppRsp *rsp) /* all CDIs have been advanced, so complete the advance operation */ s->op_state.adv_cdi_cnt = 0u; - /* SW can lock the `SW_BINDING` regs, and HW unlocks after an advance */ + /* + * In RTL (keymgr_ctrl.sv:185, keymgr.sv:371), adv_state and sw_binding_clr + * are gated by ~op_err & ~op_fault_err. Do not unlock SW_BINDING_REGWEN or + * advance FSM state if any input check failed. + */ + if (!s->op_state.valid_inputs) { + return true; + } + + /* SW can lock the `SW_BINDING` regs, and HW unlocks after a valid advance + */ s->regs[R_SW_BINDING_REGWEN] |= R_SW_BINDING_REGWEN_EN_MASK; switch (s->op_state.stage) { @@ -1537,14 +1583,26 @@ static bool ot_keymgr_handle_kmac_resp_gen_output_hw(OtKeyMgrState *s, OtKeyMgrDestSel dest = (OtKeyMgrDestSel)FIELD_EX32(ctrl, CONTROL_SHADOWED, DEST_SEL); + /* + * In RTL (keymgr_ctrl.sv:259, keymgr_sideload_key_ctrl.sv:153,168,182), + * data_valid_o = op_done_o & (id_en_o | gen_en_o) & ~op_err & + * ~op_fault_err. When valid_inputs is false (op_err), set_i is 0 so + * existing sideload keys are not overwritten or invalidated. When + * SIDELOAD_CLEAR is active for the target slot, clr_i has priority over + * set_i and keeps it cleared. + */ + if (!s->op_state.valid_inputs) { + return true; + } + switch (dest) { case KEYMGR_DEST_SEL_VALUE_AES: case KEYMGR_DEST_SEL_VALUE_KMAC: case KEYMGR_DEST_SEL_VALUE_OTBN: { OtKeyMgrKeySink key_sink = (OtKeyMgrKeySink)(dest - KEY_SINK_OFFSET); - bool key_valid = s->op_state.valid_inputs; ot_keymgr_push_key(s, key_sink, rsp->digest_share0, rsp->digest_share1, - key_valid, true); + true, true); + ot_keymgr_sideload_clear(s); break; } case KEYMGR_DEST_SEL_VALUE_NONE: @@ -1559,9 +1617,15 @@ static bool ot_keymgr_handle_kmac_resp_gen_output_hw(OtKeyMgrState *s, static bool ot_keymgr_handle_kmac_resp_gen_output_sw(OtKeyMgrState *s, const OtKMACAppRsp *rsp) { - memcpy(s->sw_out_key->share0, rsp->digest_share0, KEYMGR_KEY_BYTES); - memcpy(s->sw_out_key->share1, rsp->digest_share1, KEYMGR_KEY_BYTES); - s->sw_out_key->valid = s->op_state.valid_inputs; + /* + * In RTL (keymgr_ctrl.sv:259, keymgr.sv:638-646), SW_SHARE0/1_OUTPUT are + * only updated when data_valid (~op_err & ~op_fault_err) or wipe_key is 1. + */ + if (s->op_state.valid_inputs) { + memcpy(s->sw_out_key->share0, rsp->digest_share0, KEYMGR_KEY_BYTES); + memcpy(s->sw_out_key->share1, rsp->digest_share1, KEYMGR_KEY_BYTES); + s->sw_out_key->valid = true; + } return true; } @@ -1690,14 +1754,20 @@ static void ot_keymgr_fsm_key_stage(OtKeyMgrState *s, bool supports_generation, } switch (op) { - case KEYMGR_OP_DISABLE: - s->op_state.stage = KEYMGR_STAGE_DISABLE; - break; case KEYMGR_OP_ADVANCE: s->op_state.stage = advance; break; - default: + case KEYMGR_OP_GENERATE_ID: + case KEYMGR_OP_GENERATE_SW_OUTPUT: + case KEYMGR_OP_GENERATE_HW_OUTPUT: s->op_state.stage = generate; + break; + case KEYMGR_OP_DISABLE: + default: + /* All values not enumerated behave the same as disable + * (keymgr_ctrl.sv:154) */ + s->op_state.stage = KEYMGR_STAGE_DISABLE; + break; } s->op_state.op_req = true; s->op_state.valid_inputs = true; @@ -1812,6 +1882,8 @@ static bool ot_keymgr_main_fsm_tick(OtKeyMgrState *s) ot_keymgr_change_working_state(s, KEYMGR_WORKING_STATE_DISABLED); if (!s->enabled || invalid_state) { ot_keymgr_change_main_fsm_state(s, KEYMGR_ST_WIPE); + } else if (op_start) { + s->regs[R_ERR_CODE] |= R_ERR_CODE_INVALID_OP_MASK; } break; case KEYMGR_ST_WIPE: @@ -1834,10 +1906,10 @@ static bool ot_keymgr_main_fsm_tick(OtKeyMgrState *s) break; } - /* update the last requested operation status */ + /* update the last requested operation status (keymgr_ctrl.sv:247-248) */ bool invalid_op = (bool)(s->regs[R_ERR_CODE] & R_ERR_CODE_INVALID_OP_MASK); - bool op_done = - s->op_state.op_req ? s->op_state.op_ack : (init || invalid_op); + bool op_done = s->op_state.op_req ? s->op_state.op_ack : + ((init || invalid_op) && op_start); if (op_done) { s->op_state.op_req = false; s->op_state.op_ack = false; @@ -1870,14 +1942,10 @@ static void ot_keymgr_fsm_tick(void *opaque) { OtKeyMgrState *s = opaque; - bool fsm_state_changed = ot_keymgr_main_fsm_tick(s); - if (fsm_state_changed) { - /* FSM state changed, so schedule an FSM update once more */ - ot_keymgr_schedule_fsm(s); - } else { - /* no FSM state change, so go idle and wait for some external event */ - trace_ot_keymgr_go_idle(s->ot_id); + while (ot_keymgr_main_fsm_tick(s)) { + /* continue ticking until FSM transitions settle */ } + trace_ot_keymgr_go_idle(s->ot_id); } static void ot_keymgr_lc_signal(void *opaque, int irq, int level) @@ -1890,10 +1958,6 @@ static void ot_keymgr_lc_signal(void *opaque, int irq, int level) trace_ot_keymgr_lc_signal(s->ot_id, level); bool enablement_changed = enable_keymgr ^ s->enabled; - if (!enablement_changed) { - return; - } - s->enabled = enable_keymgr; if (s->enabled) { @@ -1902,21 +1966,37 @@ static void ot_keymgr_lc_signal(void *opaque, int irq, int level) s->regs[R_CFG_REGWEN] &= ~R_CFG_REGWEN_EN_MASK; } + if (!enablement_changed) { + return; + } + ot_keymgr_schedule_fsm(s); } -#define ot_keymgr_check_reg_write(_s_, _reg_, _regwen_) \ - ot_keymgr_check_reg_write_func(__func__, _s_, _reg_, _regwen_) +static bool ot_keymgr_is_cfg_regwen_enabled(const OtKeyMgrState *s) +{ + return s->enabled && (bool)(s->regs[R_CFG_REGWEN] & R_CFG_REGWEN_EN_MASK); +} -static inline bool ot_keymgr_check_reg_write_func( - const char *func, OtKeyMgrState *s, hwaddr reg, hwaddr regwen) +static bool ot_keymgr_check_reg_write(const OtKeyMgrState *s, hwaddr reg, + hwaddr regwen) { - if (s->regs[regwen]) { - return true; + bool en; + if (regwen == R_CFG_REGWEN) { + en = ot_keymgr_is_cfg_regwen_enabled(s); + } else if (regwen == R_SW_BINDING_REGWEN) { + /* In RTL (keymgr.sv:370), sw_binding_regwen.d = sw_binding_regwen & + * cfg_regwen */ + en = ot_keymgr_is_cfg_regwen_enabled(s) && + (bool)(s->regs[R_SW_BINDING_REGWEN] & R_SW_BINDING_REGWEN_EN_MASK); + } else { + en = (bool)(s->regs[regwen] & 1u); + } + if (!en) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: write to %s blocked by %s=0\n", + __func__, s->ot_id, REG_NAME(reg), REG_NAME(regwen)); } - qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Write to %s protected with %s\n", - func, s->ot_id, REG_NAME(reg), REG_NAME(regwen)); - return false; + return en; } static uint64_t ot_keymgr_read(void *opaque, hwaddr addr, unsigned size) @@ -1935,7 +2015,6 @@ static uint64_t ot_keymgr_read(void *opaque, hwaddr addr, unsigned size) case R_START: case R_SIDELOAD_CLEAR: case R_RESEED_INTERVAL_REGWEN: - case R_SW_BINDING_REGWEN: case R_KEY_VERSION: case R_MAX_CREATOR_KEY_VER_REGWEN: case R_MAX_OWNER_INT_KEY_VER_REGWEN: @@ -1947,6 +2026,13 @@ static uint64_t ot_keymgr_read(void *opaque, hwaddr addr, unsigned size) case R_DEBUG: val32 = s->regs[reg]; break; + case R_SW_BINDING_REGWEN: + /* In RTL (keymgr.sv:370), sw_binding_regwen.d = sw_binding_regwen & + * cfg_regwen */ + val32 = ot_keymgr_is_cfg_regwen_enabled(s) ? + s->regs[R_SW_BINDING_REGWEN] : + 0u; + break; case R_CONTROL_SHADOWED: val32 = ot_shadow_reg_read(&s->control); break; @@ -2367,12 +2453,46 @@ static const Property ot_keymgr_properties[] = { disable_flash_seed_check, false), }; +static bool ot_keymgr_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (!is_write) { + return true; + } + uint32_t permit; + switch (R32_OFF(addr)) { + case R_SEALING_SW_BINDING_0 ... R_SEALING_SW_BINDING_7: + case R_ATTEST_SW_BINDING_0 ... R_ATTEST_SW_BINDING_7: + case R_SALT_0 ... R_SALT_7: + case R_KEY_VERSION: + case R_MAX_CREATOR_KEY_VER_SHADOWED: + case R_MAX_OWNER_INT_KEY_VER_SHADOWED: + case R_MAX_OWNER_KEY_VER_SHADOWED: + case R_SW_SHARE0_OUTPUT_0 ... R_SW_SHARE1_OUTPUT_7: + permit = 0xfu; + break; + case R_CONTROL_SHADOWED: + case R_RESEED_INTERVAL_SHADOWED: + case R_FAULT_STATUS: + permit = 0x3u; + break; + default: + permit = 0x1u; + break; + } + uint32_t reg_be = (((1u << size) - 1u) << (addr & 3u)) & 0xfu; + return (permit & ~reg_be) == 0u; +} + static const MemoryRegionOps ot_keymgr_regs_ops = { .read = &ot_keymgr_read, .write = &ot_keymgr_write, .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_keymgr_regs_accepts, }; static void ot_keymgr_reset_enter(Object *obj, ResetType type) @@ -2401,7 +2521,7 @@ static void ot_keymgr_reset_enter(Object *obj, ResetType type) /* reset registers */ memset(s->regs, 0u, sizeof(s->regs)); - s->regs[R_CFG_REGWEN] = 0x1u; + s->regs[R_CFG_REGWEN] = 0x0u; ot_shadow_reg_init(&s->control, 0x10u); s->regs[R_RESEED_INTERVAL_REGWEN] = 0x1u; ot_shadow_reg_init(&s->reseed_interval, 0x100u); From 41f9ea8e9031e3676b6104ae03827d059cfee410 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:37 +0000 Subject: [PATCH 34/52] [ot] hw/opentitan: lc_ctrl: align mubi8 mutex, RMA/SCRAP, and alerts Align the OpenTitan Life Cycle Controller (`ot-lc_ctrl`) model and its Earlgrey SoC wiring (`hw/riscv/ot_earlgrey.c`) with `lc_ctrl.sv`: - Enforce strict 8-bit `mubi8_t` (`MUBI8_TRUE` = `0x69`) evaluation on `CLAIM_TRANSITION_IF`, retaining raw 8-bit values on readback while masking mutex-gated CSRs (`TRANSITION_TOKEN_*`, `TRANSITION_TARGET`, `OTP_VENDOR_TEST_CTRL`) to 0 when unclaimed (`lc_ctrl.sv:345-386`). - Support lifecycle transitions into `LC_STATE_RMA` (with `OT_LC_RMA` flash RMA wipe handshake) and `LC_STATE_SCRAP`, and preserve `OT_LC_HW_DEBUG_EN` across NDM resets so `rv_dm` remains active. - Pulse `ALERT_TEST` for 1 cycle (`lc_ctrl.sv:573-606`) so back-to-back writes re-trigger alerts, and enforce `LC_CTRL_PERMIT` sub-word write and unmapped offset (`>= 0x8c`) bus access faults. - Wire `OT_LC_RMA`, `OT_LC_DFT_EN`, `OT_LC_HW_DEBUG_EN`, and `OT_LC_ESCALATE_EN` splitters and `volatile_raw_unlock = false` (`SecVolatileRawUnlockEn = 0`) in `hw/riscv/ot_earlgrey.c`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I113df0df8333dac80ff1fd90841c113c6a6a6964 --- hw/opentitan/ot_lc_ctrl.c | 213 ++++++++++++++++++++++++++++---------- hw/riscv/ot_earlgrey.c | 11 +- 2 files changed, 165 insertions(+), 59 deletions(-) diff --git a/hw/opentitan/ot_lc_ctrl.c b/hw/opentitan/ot_lc_ctrl.c index 71ddd0f8d27d1..8da70e2696590 100644 --- a/hw/opentitan/ot_lc_ctrl.c +++ b/hw/opentitan/ot_lc_ctrl.c @@ -40,6 +40,7 @@ #include "hw/opentitan/ot_lc_ctrl.h" #include "hw/opentitan/ot_otp_if.h" #include "hw/opentitan/ot_pwrmgr.h" +#include "hw/opentitan/ot_rstmgr.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" #include "hw/riscv/ibex_common.h" @@ -289,8 +290,8 @@ typedef enum { LC_IF_DMI, /* DMI requester */ } OtLcCtrlIf; -#define EXCLUSIVE_SLOTS_COUNT 2u -#define LC_XSLOT(_ifreq_) (((unsigned)(_ifreq_)) - 1u) +#define EXCLUSIVE_SLOTS_COUNT 1u +#define LC_XSLOT(_ifreq_) ((void)(_ifreq_), 0u) typedef enum { ST_RESET, @@ -408,6 +409,8 @@ struct OtLcCtrlState { bool force_raw; /* survivability mode */ uint8_t volatile_raw_unlock_bm; /* xslot-indexed bitmap */ uint8_t state_invalid_error_bm; /* error bitmap */ + uint8_t claim_if[2]; /* SW and DMI CLAIM_TRANSITION_IF mubi8 values */ + uint8_t claim_if_regwen[2]; /* SW and DMI CLAIM_TRANSITION_IF_REGWEN */ char *hexstr; /* properties */ @@ -736,11 +739,23 @@ ot_lc_ctrl_change_state_line(OtLcCtrlState *s, OtLcCtrlFsmState state, int line) static void ot_lc_ctrl_update_alerts(OtLcCtrlState *s) { - uint32_t level = s->regs[R_ALERT_TEST]; + uint32_t fatal_level = (s->regs[R_STATUS] & R_STATUS_OTP_ERROR_MASK) ? + ALERT_FATAL_PROG_ERROR_MASK : + 0u; + uint32_t level = fatal_level | s->regs[R_ALERT_TEST]; for (unsigned ix = 0; ix < NUM_ALERTS; ix++) { ibex_irq_set(&s->alerts[ix], (int)((level >> ix) & 0x1u)); } + + if (s->regs[R_ALERT_TEST]) { + /* In RTL (lc_ctrl.sv:573-606), alert_test is a transient 1-cycle pulse + */ + s->regs[R_ALERT_TEST] = 0u; + for (unsigned ix = 0; ix < NUM_ALERTS; ix++) { + ibex_irq_set(&s->alerts[ix], (int)((fatal_level >> ix) & 0x1u)); + } + } } static void ot_lc_ctrl_update_broadcast(OtLcCtrlState *s) @@ -826,15 +841,15 @@ static void ot_lc_ctrl_update_broadcast(OtLcCtrlState *s) break; case LC_STATE_RMA: /* note: RMA signal not available on EG 1.0.0 */ - sigbm = LC_BCAST_BIT(RAW_TEST_RMA) | LC_BCAST_BIT(DFT_EN) | - LC_BCAST_BIT(NVM_DEBUG_EN) | LC_BCAST_BIT(HW_DEBUG_EN) | - LC_BCAST_BIT(CPU_EN) | LC_BCAST_BIT(KEYMGR_EN) | - LC_BCAST_BIT(CHECK_BYP_EN) | - LC_BCAST_BIT(CREATOR_SEED_SW_RW_EN) | - LC_BCAST_BIT(OWNER_SEED_SW_RW_EN) | - LC_BCAST_BIT(ISO_PART_SW_RD_EN) | - LC_BCAST_BIT(ISO_PART_SW_WR_EN) | - LC_BCAST_BIT(SEED_HW_RD_EN) | LC_BCAST_BIT(RMA); + sigbm = + LC_BCAST_BIT(RAW_TEST_RMA) | LC_BCAST_BIT(DFT_EN) | + LC_BCAST_BIT(NVM_DEBUG_EN) | LC_BCAST_BIT(HW_DEBUG_EN) | + LC_BCAST_BIT(CPU_EN) | LC_BCAST_BIT(KEYMGR_EN) | + LC_BCAST_BIT(CHECK_BYP_EN) | + LC_BCAST_BIT(CREATOR_SEED_SW_RW_EN) | + LC_BCAST_BIT(OWNER_SEED_SW_RW_EN) | + LC_BCAST_BIT(ISO_PART_SW_RD_EN) | + LC_BCAST_BIT(ISO_PART_SW_WR_EN) | LC_BCAST_BIT(SEED_HW_RD_EN); div_type = LC_DIV_RMA; break; case LC_STATE_SCRAP: @@ -860,8 +875,17 @@ static void ot_lc_ctrl_update_broadcast(OtLcCtrlState *s) s->km_div_type = div_type; for (unsigned ix = 0; ix < ARRAY_SIZE(s->broadcasts); ix++) { - bool level = (bool)(sigbm & (1u << ix)); bool curlvl = (bool)ibex_irq_get_level(&s->broadcasts[ix]); + bool level = (bool)(sigbm & (1u << ix)); + if (s->state == ST_RESET && ix == OT_LC_HW_DEBUG_EN && + ot_rstmgr_is_ndm_reset()) { + /* + * In RTL (pinmux_strap_sampling.sv), pinmux_hw_debug_en is latched + * under rst_sys_ni (which is not asserted during an NDM reset) so + * that RV_DM JTAG and TAP remain live across an NDM reset cycle. + */ + level = curlvl; + } if (level != curlvl) { trace_ot_lc_ctrl_update_broadcast(s->ot_id, LC_FSM_STATE_NAME(s->state), @@ -899,22 +923,6 @@ ot_lc_ctrl_is_hw_mutex_owner(const OtLcCtrlState *s, OtLcCtrlIf owner) return s->owner == owner; } -static bool ot_lc_ctrl_lock_hw_mutex(OtLcCtrlState *s, OtLcCtrlIf owner) -{ - if (s->owner != LC_IF_NONE) { - return ot_lc_ctrl_is_hw_mutex_owner(s, owner); - } - - s->owner = owner; - - return true; -} - -static void ot_lc_ctrl_release_hw_mutex(OtLcCtrlState *s) -{ - s->owner = LC_IF_NONE; -} - static bool ot_lc_ctrl_is_transition_en(const OtLcCtrlState *s, OtLcCtrlIf owner) { @@ -1413,7 +1421,7 @@ static void ot_lc_ctrl_start_transition(OtLcCtrlState *s) } LC_FSM_CHANGE_STATE(s, ST_CNT_INCR); - if (s->lc_tcount >= LC_TRANSITION_COUNT_MAX) { + if (s->lc_tcount >= LC_TRANSITION_COUNT_MAX && target != LC_STATE_SCRAP) { trace_ot_lc_ctrl_error(s->ot_id, "Max transition count reached"); s->regs[R_STATUS] |= R_STATUS_TRANSITION_COUNT_ERROR_MASK; LC_FSM_CHANGE_STATE(s, ST_POST_TRANS); @@ -1429,7 +1437,9 @@ static void ot_lc_ctrl_start_transition(OtLcCtrlState *s) LC_FSM_CHANGE_STATE(s, ST_CNT_PROG); - ot_lc_ctrl_program_otp(s, s->lc_tcount, s->lc_state); + ot_lc_ctrl_program_otp(s, s->lc_tcount, + target == LC_STATE_SCRAP ? LC_STATE_SCRAP : + s->lc_state); } static void ot_lc_ctrl_resume_transition(OtLcCtrlState *s) @@ -1474,6 +1484,10 @@ static void ot_lc_ctrl_resume_transition(OtLcCtrlState *s) } else { trace_ot_lc_ctrl_info(s->ot_id, "Valid token"); + if (target_state == LC_STATE_RMA) { + ibex_irq_set(&s->broadcasts[OT_LC_RMA], 1); + } + LC_FSM_CHANGE_STATE(s, ST_TRANS_PROG); ot_lc_ctrl_program_otp(s, s->lc_tcount, target_state); @@ -1542,6 +1556,10 @@ static void ot_lc_ctrl_compute_predefined_tokens(OtLcCtrlState *s) static void ot_lc_ctrl_initialize(OtLcCtrlState *s) { + /* + * initialize is invoked after the first machine reset, following the end of + * the OTP initialization sequence. + */ s->regs[R_HW_REVISION0] = (((uint32_t)s->silicon_creator_id) << 16u) | ((uint32_t)s->product_id); s->regs[R_HW_REVISION1] = (uint32_t)s->revision_id; @@ -1629,6 +1647,8 @@ static void ot_lc_ctrl_pwr_lc_req(void *opaque, int n, int level) } } +static void ot_lc_ctrl_escalate_bh(void *opaque); + static void ot_lc_ctrl_escalate_rx(void *opaque, int n, int level) { OtLcCtrlState *s = opaque; @@ -1638,7 +1658,7 @@ static void ot_lc_ctrl_escalate_rx(void *opaque, int n, int level) trace_ot_lc_ctrl_escalate_rx(s->ot_id, (unsigned)n, (bool)level); if (level) { - qemu_bh_schedule(s->escalate_bh); + ot_lc_ctrl_escalate_bh(s); } } @@ -1659,6 +1679,7 @@ static void ot_lc_ctrl_escalate_bh(void *opaque) { OtLcCtrlState *s = opaque; + s->lc_state = LC_STATE_ESCALATE; LC_FSM_CHANGE_STATE(s, ST_ESCALATE); ot_lc_ctrl_update_broadcast(s); @@ -1693,34 +1714,47 @@ static uint32_t ot_lc_ctrl_regs_read(OtLcCtrlState *s, hwaddr addr, uint32_t val32; hwaddr reg = R32_OFF(addr); + unsigned if_ix = (ifreq == LC_IF_DMI) ? 1u : 0u; + bool is_terminal_fsm = (s->state == ST_RESET || s->state == ST_ESCALATE || + s->state == ST_POST_TRANS || + s->state == ST_INVALID || s->state == ST_SCRAP); switch (reg) { case R_LC_TRANSITION_CNT: - /* TODO: >= 24 -> state == SCRAP */ - - /* Error: should be 31 */ - val32 = s->lc_tcount; + /* + * In RTL (lc_ctrl_state_decode.sv:53,57-65,101-129), dec_lc_cnt_o is + * 5'd31 (0x1f) in terminal FSM states or when lc_cnt_i > 24. + */ + val32 = (is_terminal_fsm || s->lc_tcount > LC_TRANSITION_COUNT_MAX) ? + R_LC_TRANSITION_CNT_CNT_MASK : + s->lc_tcount; break; case R_LC_STATE: val32 = LC_ENCODE_STATE(s->lc_state); break; case R_OTP_VENDOR_TEST_STATUS: val32 = ot_lc_ctrl_is_hw_mutex_owner(s, ifreq) && - ot_lc_ctrl_is_vendor_test_state(s->lc_state) ? + ot_lc_ctrl_is_vendor_test_state( + LC_ENCODE_STATE(s->lc_state)) ? s->regs[reg] : 0u; break; case R_OTP_VENDOR_TEST_CTRL: val32 = ot_lc_ctrl_is_hw_mutex_owner(s, ifreq) ? s->regs[reg] : 0u; break; + case R_CLAIM_TRANSITION_IF_REGWEN: + val32 = s->claim_if_regwen[if_ix]; + break; case R_CLAIM_TRANSITION_IF: - val32 = ot_lc_ctrl_is_hw_mutex_owner(s, ifreq) ? - OT_MULTIBITBOOL8_TRUE : - OT_MULTIBITBOOL8_FALSE; + /* + * In RTL (lc_ctrl.sv:344-345), reading CLAIM_TRANSITION_IF returns + * the raw 8-bit mubi8_t value stored in sw/tap_claim_transition_if_q. + */ + val32 = s->claim_if[if_ix]; break; case R_TRANSITION_CTRL: val32 = 0; - if (ot_lc_ctrl_is_transition_en(s, ifreq)) { + if (ot_lc_ctrl_is_hw_mutex_owner(s, ifreq)) { if (s->ext_clock_en) { val32 |= R_TRANSITION_CTRL_EXT_CLOCK_EN_MASK; } @@ -1740,12 +1774,22 @@ static uint32_t ot_lc_ctrl_regs_read(OtLcCtrlState *s, hwaddr addr, case R_TRANSITION_TOKEN_3: case R_TRANSITION_TARGET: g_assert(LC_XSLOT(ifreq) < EXCLUSIVE_SLOTS_COUNT); - val32 = s->xregs[LC_XSLOT(ifreq)][reg - R_FIRST_EXCLUSIVE_REG]; + val32 = ot_lc_ctrl_is_hw_mutex_owner(s, ifreq) ? + s->xregs[LC_XSLOT(ifreq)][reg - R_FIRST_EXCLUSIVE_REG] : + 0u; break; case R_STATUS: - case R_TRANSITION_CMD: - case R_CLAIM_TRANSITION_IF_REGWEN: + val32 = s->regs[reg]; + break; case R_LC_ID_STATE: + /* + * In RTL (lc_ctrl_state_decode.sv:54,57-65), dec_lc_id_state_o defaults + * to DecLcIdInvalid (0xaaaaaaaa) in ResetSt, EscalateSt, PostTransSt, + * InvalidSt, and ScrapSt. + */ + val32 = is_terminal_fsm ? LC_ID_STATE_INVALID : s->regs[reg]; + break; + case R_TRANSITION_CMD: case R_HW_REVISION0: case R_HW_REVISION1: case R_DEVICE_ID_0: @@ -1814,6 +1858,8 @@ static void ot_lc_ctrl_regs_write(OtLcCtrlState *s, hwaddr addr, uint32_t val32, OtLcCtrlIf ifreq) { hwaddr reg = R32_OFF(addr); + unsigned if_ix = (ifreq == LC_IF_DMI) ? 1u : 0u; + unsigned other_ix = 1u - if_ix; uint32_t pc = ibex_get_current_pc(); trace_ot_lc_ctrl_io_write(s->ot_id, (uint32_t)addr, REG_NAME(reg), val32, @@ -1827,21 +1873,31 @@ static void ot_lc_ctrl_regs_write(OtLcCtrlState *s, hwaddr addr, uint32_t val32, break; case R_CLAIM_TRANSITION_IF_REGWEN: val32 &= R_CLAIM_TRANSITION_IF_REGWEN_EN_MASK; - s->regs[reg] &= val32; /* rw0c */ + s->claim_if_regwen[if_ix] &= (uint8_t)val32; /* rw0c */ + s->regs[reg] = s->claim_if_regwen[0]; break; case R_CLAIM_TRANSITION_IF: - if (!(s->regs[R_CLAIM_TRANSITION_IF_REGWEN] & - R_CLAIM_TRANSITION_IF_REGWEN_EN_MASK)) { + if (!s->claim_if_regwen[if_ix]) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: CLAIM_TRANSITION_IF disabled\n", __func__, s->ot_id); break; } val32 &= R_CLAIM_TRANSITION_IF_MUTEX_MASK; - if (val32 == OT_MULTIBITBOOL8_TRUE) { - ot_lc_ctrl_lock_hw_mutex(s, ifreq); - } else { - ot_lc_ctrl_release_hw_mutex(s); + /* + * In RTL (lc_ctrl.sv:380-387), an interface can update its + * claim_transition_if_q register iff mubi8_test_false_loose(other_q) + * is true (i.e. the other interface does not hold MuBi8True). + */ + if (s->claim_if[other_ix] != OT_MULTIBITBOOL8_TRUE) { + s->claim_if[if_ix] = (uint8_t)val32; + if (s->claim_if[1] == OT_MULTIBITBOOL8_TRUE) { + s->owner = LC_IF_DMI; + } else if (s->claim_if[0] == OT_MULTIBITBOOL8_TRUE) { + s->owner = LC_IF_SW; + } else { + s->owner = LC_IF_NONE; + } } break; case R_TRANSITION_CMD: @@ -1863,6 +1919,10 @@ static void ot_lc_ctrl_regs_write(OtLcCtrlState *s, hwaddr addr, uint32_t val32, } if (val32 & R_TRANSITION_CTRL_EXT_CLOCK_EN_MASK) { s->ext_clock_en = true; /* rw1s */ + if (s->state == ST_IDLE && + ot_lc_ctrl_is_vendor_test_state(LC_ENCODE_STATE(s->lc_state))) { + s->regs[R_STATUS] |= R_STATUS_EXT_CLOCK_SWITCHED_MASK; + } } if (s->volatile_raw_unlock) { if (val32 & R_TRANSITION_CTRL_VOLATILE_RAW_UNLOCK_MASK) { @@ -1890,11 +1950,14 @@ static void ot_lc_ctrl_regs_write(OtLcCtrlState *s, hwaddr addr, uint32_t val32, __func__, s->ot_id); break; } - val32 &= R_TRANSITION_TARGET_STATE_MASK; - if (ot_lc_ctrl_is_known_state(val32)) { - g_assert(LC_XSLOT(ifreq) < EXCLUSIVE_SLOTS_COUNT); - s->xregs[LC_XSLOT(ifreq)][reg - R_FIRST_EXCLUSIVE_REG] = val32; - } + /* + * In RTL (lc_ctrl.sv:359, 451-456), transition_target_q stores any + * 30-bit value when TRANSITION_REGWEN == 1; validity is checked when + * transition starts (lc_ctrl_fsm.sv:349-354). + */ + g_assert(LC_XSLOT(ifreq) < EXCLUSIVE_SLOTS_COUNT); + s->xregs[LC_XSLOT(ifreq)][reg - R_FIRST_EXCLUSIVE_REG] = + val32 & R_TRANSITION_TARGET_STATE_MASK; break; case R_OTP_VENDOR_TEST_CTRL: if (!ot_lc_ctrl_is_transition_en(s, ifreq)) { @@ -1938,6 +2001,26 @@ static void ot_lc_ctrl_regs_write(OtLcCtrlState *s, hwaddr addr, uint32_t val32, } }; +static bool ot_lc_ctrl_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + if (addr >= REGS_SIZE) { + return false; + } + if (!is_write) { + return true; + } + uint32_t permit = + (R32_OFF(addr) <= R_TRANSITION_CTRL || + R32_OFF(addr) == R_LC_TRANSITION_CNT) ? + (R32_OFF(addr) == R_STATUS ? 0x3u : 0x1u) : + 0xfu; + uint32_t be = ((1u << size) - 1u) << (addr & 3u); + return (permit & ~be) == 0u; +} + static uint64_t ot_lc_ctrl_sw_regs_read(void *opaque, hwaddr addr, unsigned size) { @@ -2235,6 +2318,9 @@ static const MemoryRegionOps ot_lc_ctrl_sw_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.min_access_size = 1u, + .valid.max_access_size = 4u, + .valid.accepts = &ot_lc_ctrl_regs_accepts, }; static const MemoryRegionOps ot_lc_ctrl_dmi_regs_ops = { @@ -2243,6 +2329,9 @@ static const MemoryRegionOps ot_lc_ctrl_dmi_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.min_access_size = 1u, + .valid.max_access_size = 4u, + .valid.accepts = &ot_lc_ctrl_regs_accepts, }; static void ot_lc_ctrl_reset_enter(Object *obj, ResetType type) @@ -2267,6 +2356,10 @@ static void ot_lc_ctrl_reset_enter(Object *obj, ResetType type) s->kmac_state = ST_KMAC_IDLE; s->regs[R_CLAIM_TRANSITION_IF] = OT_MULTIBITBOOL8_FALSE; s->regs[R_CLAIM_TRANSITION_IF_REGWEN] = 1u; + s->claim_if[0] = OT_MULTIBITBOOL8_FALSE; + s->claim_if[1] = OT_MULTIBITBOOL8_FALSE; + s->claim_if_regwen[0] = 1u; + s->claim_if_regwen[1] = 1u; s->ext_clock_en = false; s->volatile_unlocked = false; s->force_raw = false; @@ -2278,6 +2371,14 @@ static void ot_lc_ctrl_reset_enter(Object *obj, ResetType type) ot_lc_ctrl_update_alerts(s); for (unsigned ix = 0; ix < ARRAY_SIZE(s->broadcasts); ix++) { + if (ix == OT_LC_HW_DEBUG_EN && ot_rstmgr_is_ndm_reset()) { + /* + * In RTL (pinmux_strap_sampling.sv), pinmux_hw_debug_en is latched + * under rst_sys_ni (which is not asserted during an NDM reset) so + * that RV_DM JTAG and TAP remain live across an NDM reset cycle. + */ + continue; + } ibex_irq_set(&s->broadcasts[ix], 0); } diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 75cf3810b8b9e..f8466c3412e06 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -181,6 +181,7 @@ enum OtEGSocDevice { OT_EG_SOC_DEV_VMAPPER, /* IRQ splitters, i.e. 1-to-N signal dispatchers */ OT_EG_SOC_SPLITTER_LC_HW_DEBUG, + OT_EG_SOC_SPLITTER_LC_DFT, OT_EG_SOC_SPLITTER_LC_ESCALATE, OT_EG_SOC_SPLITTER_LC_SEED_HW_RD, OT_EG_SOC_SPLITTER_LC_CREATOR_SEED_SW_RW, @@ -842,9 +843,13 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_FLASH_LC_ISO_PART_SW_WR_EN), OT_EG_SOC_SIGNAL(OT_LC_BROADCAST, OT_LC_NVM_DEBUG_EN, FLASH_CTRL, OT_LC_BROADCAST, OT_FLASH_LC_NVM_DEBUG_EN), + OT_EG_SOC_SIGNAL(OT_LC_BROADCAST, OT_LC_RMA, FLASH_CTRL, + OT_LC_BROADCAST, OT_FLASH_LC_RMA), /* Signals to OTP */ OT_EG_SOC_SIGNAL(OT_LC_BROADCAST, OT_LC_CHECK_BYP_EN, OTP_CTRL, - OT_LC_BROADCAST, OT_OTP_LC_CHECK_BYP_EN) + OT_LC_BROADCAST, OT_OTP_LC_CHECK_BYP_EN), + /* Signals to pwrmgr and rv_dm */ + OT_EG_SOC_D2S(OT_LC_BROADCAST, OT_LC_DFT_EN, LC_DFT) ), .link = IBEXDEVICELINKDEFS( OT_EG_SOC_DEVLINK("otp-ctrl", OTP_CTRL), @@ -854,7 +859,7 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { IBEX_DEV_UINT_PROP("silicon_creator_id", 0x4001u), IBEX_DEV_UINT_PROP("product_id", 0x0002u), IBEX_DEV_UINT_PROP("revision_id", 0x1u), - IBEX_DEV_BOOL_PROP("volatile_raw_unlock", true), + IBEX_DEV_BOOL_PROP("volatile_raw_unlock", false), IBEX_DEV_UINT_PROP("kmac-app", 1u) ) }, @@ -870,7 +875,7 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_EG_SOC_GPIO_SYSBUS_IRQ(3, PLIC, 130), OT_EG_SOC_GPIO_ESCALATE(0, IBEX_WRAPPER, 0), OT_EG_SOC_GPIO_ESCALATE(1, LC_CTRL, 0), - OT_EG_SOC_GPIO_ESCALATE(1, LC_CTRL, 1), + OT_EG_SOC_GPIO_ESCALATE(2, LC_CTRL, 1), OT_EG_SOC_GPIO_ESCALATE(3, PWRMGR, 0) ), .link = IBEXDEVICELINKDEFS( From 9d8fb9b181cae218c208ff5cd6dac033f7353a52 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:38 +0000 Subject: [PATCH 35/52] [ot] hw/opentitan: sram_ctrl: align key rotation, mubi4 W1C, and AON Align the OpenTitan SRAM Controller (`ot-sram_ctrl`) model with `sram_ctrl.sv` and `sram_ctrl_regs_reg_top.sv`: - Implement `SCR_KEY_ROTATED` `mubi4_and_hi` W1C semantics and `CTRL` write-only readback (`0`), and raise an Ibex load integrity error (`ot_common_raise_load_integrity_error`) when reading pre-rotation SRAM cells after `RENEW_SCR_KEY` without `CTRL.INIT`. - Add the `OT_SRAM_CTRL_LC_ESCALATE_EN` GPIO input (and wire it from `OT_EG_SOC_SPLITTER_LC_ESCALATE` to `SRAM_RET_CTRL` and `SRAM_MAIN_CTRL` in `hw/riscv/ot_earlgrey.c`) to set `STATUS.ESCALATED` and disable the SRAM region upon escalation, and re-evaluate instruction fetch permissions on `OT_SRAM_CTRL_HW_DEBUG_EN`. - Preserve retention SRAM (`ot_id == "ret"`) contents and CSR state across low-power exit resets (`ot_rstmgr_is_low_power_exit()`). - Pulse `ALERT_TEST` for 1 cycle and enforce `SRAM_CTRL_REGS_PERMIT` (`4'b0001`) sub-word write faults in `ot_sram_ctrl_regs_accepts`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ied47b9b28dd65832a6a03b31df9e9a776a6a6964 --- hw/opentitan/ot_sram_ctrl.c | 142 ++++++++++++++++++++++++---- hw/riscv/ot_earlgrey.c | 10 +- include/hw/opentitan/ot_sram_ctrl.h | 3 + 3 files changed, 137 insertions(+), 18 deletions(-) diff --git a/hw/opentitan/ot_sram_ctrl.c b/hw/opentitan/ot_sram_ctrl.c index 4cc4f6652f3d3..928238372c96e 100644 --- a/hw/opentitan/ot_sram_ctrl.c +++ b/hw/opentitan/ot_sram_ctrl.c @@ -37,6 +37,7 @@ #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_otp_if.h" #include "hw/opentitan/ot_prng.h" +#include "hw/opentitan/ot_rstmgr.h" #include "hw/opentitan/ot_sram_ctrl.h" #include "hw/opentitan/ot_vmapper.h" #include "hw/qdev-properties.h" @@ -126,11 +127,13 @@ struct OtSramCtrlState { OtPrngState *prng; /* simplified PRNG, does not match OT's */ OtOTPKey *otp_key; uint32_t regs[REGS_COUNT]; + uint32_t scr_key; /* current active scrambling key seed (0 = default) */ unsigned init_slot_count; /* count of init_slot_bm */ unsigned init_slot_pos; /* current SRAM cell (word-sized) for init. */ unsigned wsize; /* size of RAM in words */ bool initialized; /* SRAM has been fully initialized at least once */ bool initializing; /* CTRL.INIT has been requested */ + bool scr_invalid; /* CTRL.RENEW_SCR_KEY without CTRL.INIT */ bool otp_ifetch; /* whether OTP enable execution from this RAM */ bool csr_ifetch; /* whether CSR enable execution from this RAM */ bool lc_hw_debug_ifetch; /* HW_DEBUG_EN signal from the lc_ctrl */ @@ -191,6 +194,20 @@ static void ot_sram_ctrl_mem_switch_to_ram(OtSramCtrlState *s) trace_ot_sram_ctrl_switch_mem(s->ot_id, "ram"); } +static void ot_sram_ctrl_mem_switch_to_init(OtSramCtrlState *s) +{ + if (s->noinit) { + return; + } + memory_region_transaction_begin(); + memory_region_set_enabled(&s->mem->sram, false); + memory_region_set_enabled(&s->mem->init, true); + s->mem->alias.alias = &s->mem->init; + memory_region_transaction_commit(); + + trace_ot_sram_ctrl_switch_mem(s->ot_id, "init"); +} + static bool ot_sram_ctrl_mem_is_fully_initialized(const OtSramCtrlState *s) { for (unsigned ix = 0; ix < s->init_slot_count; ix++) { @@ -226,7 +243,10 @@ static bool ot_sram_ctrl_initialize(OtSramCtrlState *s, unsigned count, if (s->init_slot_pos >= s->wsize) { /* init has been completed */ - s->regs[R_STATUS] |= R_STATUS_INIT_DONE_MASK; + bool escalated = (s->regs[R_STATUS] & R_STATUS_ESCALATED_MASK) != 0; + if (!escalated) { + s->regs[R_STATUS] |= R_STATUS_INIT_DONE_MASK; + } /* enable new request for initialization */ s->regs[R_CTRL] &= ~R_CTRL_INIT_MASK; @@ -237,10 +257,9 @@ static bool ot_sram_ctrl_initialize(OtSramCtrlState *s, unsigned count, size_t cell_slot_count = ot_sram_ctrl_get_slot_count(s->wsize); memset(s->init_sram_bm, 0, cell_slot_count * sizeof(uint64_t)); memset(s->init_slot_bm, 0, s->init_slot_count * sizeof(uint64_t)); + s->scr_invalid = false; - s->init_slot_bm = g_new0(uint64_t, s->init_slot_count); - - if (!s->noswitch) { + if (!s->noswitch && !escalated) { /* switch memory to SRAM */ trace_ot_sram_ctrl_initialization_complete(s->ot_id, "ctrl"); ot_sram_ctrl_mem_switch_to_ram(s); @@ -261,8 +280,24 @@ static bool ot_sram_ctrl_initialize(OtSramCtrlState *s, unsigned count, return false; } +static void ot_sram_ctrl_apply_scr_key(OtSramCtrlState *s, uint32_t new_key) +{ + uint32_t mask = s->scr_key ^ new_key; + if (!mask || s->noinit) { + return; + } + uint32_t *mem = memory_region_get_ram_ptr(&s->mem->sram); + for (unsigned ix = 0; ix < s->wsize; ix++) { + mem[ix] ^= mask; + } + memory_region_set_dirty(&s->mem->sram, 0, s->size); + s->scr_key = new_key; +} + static void ot_sram_ctrl_reseed(OtSramCtrlState *s) { + bool escalated = (s->regs[R_STATUS] & R_STATUS_ESCALATED_MASK) != 0; + s->regs[R_STATUS] &= ~(R_STATUS_SCR_KEY_VALID_MASK | R_STATUS_SCR_KEY_SEED_VALID_MASK); @@ -304,7 +339,7 @@ static void ot_sram_ctrl_reseed(OtSramCtrlState *s) ot_sram_ctrl_hexdump(s, s->otp_key->nonce, s->otp_key->nonce_size)); - if (s->otp_key->seed_valid) { + if (s->otp_key->seed_valid && !escalated) { s->regs[R_STATUS] |= R_STATUS_SCR_KEY_SEED_VALID_MASK; } @@ -323,8 +358,22 @@ static void ot_sram_ctrl_reseed(OtSramCtrlState *s) sizeof(uint32_t)); } + if (!escalated) { + uint32_t new_key = + (ot_prng_random_u32(s->prng) | 1u) ^ ((s->scr_key & 1u) ? 2u : 0u); + ot_sram_ctrl_apply_scr_key(s, new_key); + if (!s->noinit && s->init_sram_bm) { + size_t cell_slot_count = ot_sram_ctrl_get_slot_count(s->wsize); + memset(s->init_sram_bm, 0xff, cell_slot_count * sizeof(uint64_t)); + s->scr_invalid = true; + ot_sram_ctrl_mem_switch_to_init(s); + } + + s->regs[R_STATUS] |= R_STATUS_SCR_KEY_VALID_MASK; + s->regs[R_SCR_KEY_ROTATED] = OT_MULTIBITBOOL4_TRUE; + } + s->regs[R_CTRL] &= ~R_CTRL_RENEW_SCR_KEY_MASK; - s->regs[R_STATUS] |= R_STATUS_SCR_KEY_VALID_MASK; } static void ot_sram_ctrl_start_initialization(OtSramCtrlState *s) @@ -352,6 +401,8 @@ static void ot_sram_ctrl_start_initialization(OtSramCtrlState *s) ot_sram_ctrl_initialize(s, count, false); } +static void ot_sram_ctrl_update_exec(OtSramCtrlState *s); + static void ot_sram_ctrl_lc_signal(void *opaque, int irq, int level) { OtSramCtrlState *s = opaque; @@ -361,6 +412,27 @@ static void ot_sram_ctrl_lc_signal(void *opaque, int irq, int level) trace_ot_sram_ctrl_lc_signal(s->ot_id, level); s->lc_hw_debug_ifetch = (bool)level; + ot_sram_ctrl_update_exec(s); +} + +static void ot_sram_ctrl_lc_escalate(void *opaque, int irq, int level) +{ + OtSramCtrlState *s = opaque; + + g_assert(irq == 0); + + if (level) { + timer_del(s->init_timer); + s->initializing = false; + s->init_slot_pos = 0; + s->regs[R_STATUS] |= R_STATUS_ESCALATED_MASK; + s->regs[R_STATUS] &= + ~(R_STATUS_SCR_KEY_VALID_MASK | R_STATUS_SCR_KEY_SEED_VALID_MASK | + R_STATUS_INIT_DONE_MASK); + s->regs[R_SCR_KEY_ROTATED] = OT_MULTIBITBOOL4_FALSE; + ot_sram_ctrl_apply_scr_key(s, 0u); + memory_region_set_enabled(&s->mem->alias, false); + } } static void ot_sram_ctrl_update_exec(OtSramCtrlState *s) @@ -411,13 +483,13 @@ static uint64_t ot_sram_ctrl_regs_read(void *opaque, hwaddr addr, unsigned size) case R_EXEC_REGWEN: case R_EXEC: case R_CTRL_REGWEN: - case R_CTRL: case R_SCR_KEY_ROTATED: case R_READBACK_REGWEN: case R_READBACK: val32 = s->regs[reg]; break; case R_ALERT_TEST: + case R_CTRL: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s W/O register 0x%02x (%s)\n", __func__, s->ot_id, (uint32_t)addr, REG_NAME(reg)); val32 = 0; @@ -452,7 +524,10 @@ static void ot_sram_ctrl_regs_write(void *opaque, hwaddr addr, uint64_t val64, switch (reg) { case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_ERROR_MASK; - ibex_irq_set(&s->alert, (int)(bool)val32); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; case R_EXEC_REGWEN: val32 &= R_EXEC_REGWEN_EN_MASK; @@ -477,9 +552,7 @@ static void ot_sram_ctrl_regs_write(void *opaque, hwaddr addr, uint64_t val64, case R_CTRL: if (s->regs[R_CTRL_REGWEN]) { /* WO */ val32 &= R_CTRL_INIT_MASK | R_CTRL_RENEW_SCR_KEY_MASK; - uint32_t trig = (val32 ^ s->regs[reg]) & val32; - /* storing value prevents from trigerring again before completion */ - s->regs[reg] = val32; + uint32_t trig = s->initializing ? 0u : val32; if (trig & R_CTRL_RENEW_SCR_KEY_MASK) { ot_sram_ctrl_reseed(s); } @@ -498,11 +571,12 @@ static void ot_sram_ctrl_regs_write(void *opaque, hwaddr addr, uint64_t val64, s->ot_id); } break; - case R_SCR_KEY_ROTATED: - /* this register has been deprecated on Darjeeling */ - qemu_log_mask(LOG_UNIMP, "%s: %s R_SCR_KEY_ROTATED\n", __func__, - s->ot_id); + case R_SCR_KEY_ROTATED: { + uint32_t inv_wd = ~val32 & R_SCR_KEY_ROTATED_SUCCESS_MASK; + s->regs[reg] = ((s->regs[reg] & inv_wd) & OT_MULTIBITBOOL4_TRUE) | + ((s->regs[reg] | inv_wd) & OT_MULTIBITBOOL4_FALSE); break; + } case R_READBACK_REGWEN: val32 &= R_READBACK_REGWEN_EN_MASK; s->regs[reg] &= val32; /* RW0C */ @@ -593,6 +667,15 @@ static MemTxResult ot_sram_ctrl_mem_init_read_with_attrs( val32 >>= addr_offset << 3u; *val64 = (uint64_t)val32; + if (s->scr_invalid && s->init_sram_bm && + (s->init_sram_bm[ot_sram_ctrl_get_u64_slot(cell)] & + (1ull << ot_sram_ctrl_get_u64_offset(cell)))) { + SysBusDevice *sbd = SYS_BUS_DEVICE(s); + ot_common_raise_load_integrity_error(DEVICE(s), (sbd->mmio[1].addr - + sbd->mmio[0].addr) + + addr); + } + trace_ot_sram_ctrl_mem_io_reado(s->ot_id, (uint32_t)addr, size, val32, pc); return MEMTX_OK; @@ -697,10 +780,20 @@ static const Property ot_sram_ctrl_properties[] = { DEFINE_PROP_BOOL("noswitch", OtSramCtrlState, noswitch, false), }; +static bool ot_sram_ctrl_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)size; + (void)attrs; + return !is_write || (addr & 3u) == 0u; +} + static const MemoryRegionOps ot_sram_ctrl_regs_ops = { .read = &ot_sram_ctrl_regs_read, .write = &ot_sram_ctrl_regs_write, .endianness = DEVICE_NATIVE_ENDIAN, + .valid.accepts = &ot_sram_ctrl_regs_accepts, .impl.min_access_size = 4u, .impl.max_access_size = 4u, }; @@ -722,7 +815,17 @@ static void ot_sram_ctrl_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } + if (s->ot_id && !strcmp(s->ot_id, "ret") && ot_rstmgr_is_low_power_exit()) { + return; + } + timer_del(s->init_timer); + s->initializing = false; + s->init_slot_pos = 0; + + ot_sram_ctrl_apply_scr_key(s, 0u); + s->scr_invalid = false; + memory_region_set_enabled(&s->mem->alias, true); memset(s->regs, 0, REGS_SIZE); @@ -753,6 +856,10 @@ static void ot_sram_ctrl_reset_exit(Object *obj, ResetType type) s->otp_ifetch = (s->otp_ctrl == NULL); s->csr_ifetch = (s->regs[R_EXEC] == OT_MULTIBITBOOL4_TRUE); + if (s->initialized && !s->noswitch && !s->noinit && !s->scr_invalid) { + ot_sram_ctrl_mem_switch_to_ram(s); + } + ot_sram_ctrl_update_exec(s); } @@ -776,7 +883,8 @@ static void ot_sram_ctrl_realize(DeviceState *dev, Error **errp) if (!s->init_chunk_words) { /* somewhat arbitrary */ - s->init_chunk_words = MAX(s->wsize / 16u, INIT_TIMER_CHUNK_WORDS); + s->init_chunk_words = + MAX(MIN(s->wsize / 16u, INIT_TIMER_CHUNK_WORDS), 1u); } else { g_assert(s->init_chunk_words < s->wsize); } @@ -869,6 +977,8 @@ static void ot_sram_ctrl_init(Object *obj) qdev_init_gpio_in_named(DEVICE(obj), &ot_sram_ctrl_lc_signal, OT_SRAM_CTRL_HW_DEBUG_EN, 1); + qdev_init_gpio_in_named(DEVICE(obj), &ot_sram_ctrl_lc_escalate, + OT_SRAM_CTRL_LC_ESCALATE_EN, 1); s->mem = g_new0(OtSramCtrlMem, 1u); s->init_timer = diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index f8466c3412e06..d9b73deb1c7db 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -1501,10 +1501,16 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_EG_SOC_S2D(0, OTP_CTRL, OT_LC_BROADCAST, OT_OTP_LC_ESCALATE_EN), OT_EG_SOC_S2D(1, FLASH_CTRL, OT_LC_BROADCAST, - OT_FLASH_LC_ESCALATE_EN) + OT_FLASH_LC_ESCALATE_EN), + OT_EG_SOC_S2D(2, AON_TIMER, OT_AON_TIMER_LC_ESCALATE, 0), + OT_EG_SOC_S2D(3, SRAM_RET_CTRL, OT_SRAM_CTRL_LC_ESCALATE_EN, 0), + OT_EG_SOC_S2D(4, SRAM_MAIN_CTRL, OT_SRAM_CTRL_LC_ESCALATE_EN, 0), + OT_EG_SOC_S2D(5, AES, OT_AES_LC_ESCALATE_EN, 0), + OT_EG_SOC_S2D(6, KMAC, OT_KMAC_LC_ESCALATE_EN, 0), + OT_EG_SOC_S2D(7, OTBN, OT_OTBN_LC_ESCALATE_EN, 0) ), .prop = IBEXDEVICEPROPDEFS( - IBEX_DEV_UINT_PROP("num-lines", 2u) /* @todo to be changed */ + IBEX_DEV_UINT_PROP("num-lines", 8u) ) }, [OT_EG_SOC_SPLITTER_LC_SEED_HW_RD] = { diff --git a/include/hw/opentitan/ot_sram_ctrl.h b/include/hw/opentitan/ot_sram_ctrl.h index a502b6e5aeffc..60b4bb1c34c85 100644 --- a/include/hw/opentitan/ot_sram_ctrl.h +++ b/include/hw/opentitan/ot_sram_ctrl.h @@ -36,4 +36,7 @@ OBJECT_DECLARE_TYPE(OtSramCtrlState, OtSramCtrlClass, OT_SRAM_CTRL) /* Input HW_DEBUG_EN signal for SRAM ifetch (from lifecycle controller) */ #define OT_SRAM_CTRL_HW_DEBUG_EN TYPE_OT_SRAM_CTRL "-hw_debug_en" +/* Input LC_ESCALATE_EN signal for SRAM lockdown (from lifecycle controller) */ +#define OT_SRAM_CTRL_LC_ESCALATE_EN TYPE_OT_SRAM_CTRL "-lc_escalate_en" + #endif /* HW_OPENTITAN_OT_SRAM_CTRL */ From 9356f6c16a1f83ecee573cfc25f1ad602b36795f Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:38 +0000 Subject: [PATCH 36/52] [ot] hw/opentitan: clkmgr: implement clk hints, measure, and jitter Align the OpenTitan Earlgrey Clock Manager (hw/opentitan/ot_clkmgr.c and include/hw/opentitan/ot_clkmgr.h) with hw/ip/clkmgr/rtl/clkmgr.sv, clkmgr_meas_chk.sv, and clkmgr_reg_top.sv: - Implement per-clock measurement counter width (ot_clkmgr_get_measure_width), cycle count evaluation (ot_clkmgr_check_measurement), RECOV_ERR_CODE measurement error bits, and MuBi4False reset defaults for MEAS_CTRL_EN. - Gate EXTCLK_STATUS settlement on OT_CLKMGR_LC_HW_DEBUG_EN and model low-speed external clock divider step-down (HI_SPEED_SEL = MuBi4False). - Match clkmgr_reg_top.sv JITTER_ENABLE write behavior (latching MuBi4True), pulse ALERT_TEST for 1 cycle, preserve AON registers across deep-sleep low_power_exit while resetting MEAS_CTRL_EN and MEASURE_CTRL_REGWEN, and enforce CLKMGR_PERMIT sub-word MMIO write checks. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I2c51944d6fd3d4831579d15810bb1ebb6a6a6964 --- hw/opentitan/ot_clkmgr.c | 279 +++++++++++++++++++++++-------- hw/riscv/ot_earlgrey.c | 18 +- include/hw/opentitan/ot_clkmgr.h | 3 +- 3 files changed, 230 insertions(+), 70 deletions(-) diff --git a/hw/opentitan/ot_clkmgr.c b/hw/opentitan/ot_clkmgr.c index b7c5a8558ea8a..c67151500abda 100644 --- a/hw/opentitan/ot_clkmgr.c +++ b/hw/opentitan/ot_clkmgr.c @@ -35,6 +35,7 @@ #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_clkmgr.h" #include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_rstmgr.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" #include "hw/riscv/ibex_clock_src.h" @@ -135,6 +136,7 @@ typedef struct { GList *outputs; /* wekrefs to OtClkMgrClockOutput */ unsigned divider; /* divider applied on parent clock if any (0 if top) */ unsigned ratio; /* ratio w/ reference clock (may be 0) */ + unsigned frequency; /* actual clock frequency in Hz */ bool ref; /* reference clock */ bool loose; /* clock which is declared but not connected */ } OtClkMgrClock; @@ -247,6 +249,7 @@ struct OtClkMgrState { OtClkMgrMeasureRegs *measure_regs; unsigned measure_count; /* count of measure_regs */ bool input_clock_connected; /* true once clock source are connected */ + bool lc_hw_debug_en; char *ot_id; DeviceState *clock_src; /* Top clock source */ @@ -293,11 +296,74 @@ static const char *CFGSEP = ","; static void ot_clkmgr_update_alerts(OtClkMgrState *s) { - bool recov = (bool)(s->regs[R_RECOV_ERR_CODE] & - R_RECOV_ERR_CODE_SHADOW_UPDATE_ERR_MASK); + bool recov = s->regs[R_RECOV_ERR_CODE] != 0; ibex_irq_set(&s->alerts[ALERT_RECOVERABLE], recov); } +static OtClkMgrClock *ot_clkmgr_find_clock(GList *clock_list, const char *name); + +static uint32_t +ot_clkmgr_get_clock_count(OtClkMgrState *s, const OtClkMgrClock *clk) +{ + const OtClkMgrClock *target_clk = clk; + if (s->lc_hw_debug_en && FIELD_EX32(s->regs[R_EXTCLK_CTRL], EXTCLK_CTRL, + SEL) == OT_MULTIBITBOOL4_TRUE) { + bool hi_speed = (FIELD_EX32(s->regs[R_EXTCLK_CTRL], EXTCLK_CTRL, + HI_SPEED_SEL) == OT_MULTIBITBOOL4_TRUE); + if (!hi_speed) { + if (!strcmp(clk->name, "io")) { + const OtClkMgrClock *io_div2 = + ot_clkmgr_find_clock(s->clocks, "io_div2"); + if (io_div2) { + target_clk = io_div2; + } + } + } else { + if (!strcmp(clk->name, "main")) { + const OtClkMgrClock *io = ot_clkmgr_find_clock(s->clocks, "io"); + if (io) { + target_clk = io; + } + } + } + } + + const OtClkMgrClock *ref = ot_clkmgr_find_clock(s->clocks, s->cfg_refclock); + uint32_t ratio = (ref && ref->frequency && target_clk->frequency) ? + (target_clk->frequency / ref->frequency) : + target_clk->ratio; + return ratio > 0 ? (ratio - 1u) : 0u; +} + +static unsigned ot_clkmgr_get_measure_width(const OtClkMgrClock *clk) +{ + if (!clk || clk->ratio == 0) { + return 10u; + } + return 32u - (unsigned)clz32((2u * clk->ratio) - 1u); +} + +static void ot_clkmgr_check_measurement(OtClkMgrState *s, unsigned measure) +{ + g_assert(measure < s->measure_count); + OtClkMgrMeasureRegs *mreg = &s->measure_regs[measure]; + /* RTL clkmgr.sv uses mubi4_test_true_loose (enabled when != MuBi4False) */ + if (mreg->ctrl_en == OT_MULTIBITBOOL4_FALSE) { + return; + } + unsigned width = ot_clkmgr_get_measure_width(mreg->clock); + uint32_t mask = (1u << width) - 1u; + uint32_t ctrl = ot_shadow_reg_peek(&mreg->ctrl); + uint32_t hi = ctrl & mask; + uint32_t lo = (ctrl >> width) & mask; + uint32_t count = ot_clkmgr_get_clock_count(s, mreg->clock); + if (count > hi || count < lo) { + s->regs[R_RECOV_ERR_CODE] |= + 1u << (R_RECOV_ERR_CODE_SHADOW_UPDATE_ERR_SHIFT + 1u + measure); + ot_clkmgr_update_alerts(s); + } +} + /* NOLINTNEXTLINE(misc-no-recursion) */ static void ot_clkmgr_update_clock_frequency( OtClkMgrState *s, OtClkMgrClock *clk, unsigned input_freq) @@ -305,6 +371,8 @@ static void ot_clkmgr_update_clock_frequency( unsigned frequency = clk->divider > 1 ? (input_freq / clk->divider) : input_freq; + clk->frequency = frequency; + trace_ot_clkmgr_update_clock(s->ot_id, clk->name, frequency); /* propagate to derived clocks */ @@ -893,8 +961,10 @@ static gint ot_clkmgr_compare_swcg_by_name(gconstpointer a, gconstpointer b) { const OtClkMgrSwCgClock *sa = a; const OtClkMgrSwCgClock *sb = b; + int diff = + (int)sb->output->clock->divider - (int)sa->output->clock->divider; - return strcmp(sa->name, sb->name); + return diff ? diff : strcmp(sa->name, sb->name); } static gint ot_clkmgr_compare_swcg_by_name_with_data( @@ -993,14 +1063,14 @@ static void ot_clkmgr_reset_measure_regs(OtClkMgrState *s) for (unsigned ix = 0; ix < s->measure_count; ix++) { OtClkMgrMeasureRegs *mreg = &s->measure_regs[ix]; - mreg->ctrl_en = OT_MULTIBITBOOL4_TRUE; + mreg->ctrl_en = OT_MULTIBITBOOL4_FALSE; uint32_t hi = mreg->clock->ratio + 10u; uint32_t lo = (uint32_t)MAX(0, ((int)mreg->clock->ratio) - 10); - uint32_t value = 0; - value = SHARED_FIELD_DP32(value, MEAS_CTRL_SHADOWED_HI, hi); - value = SHARED_FIELD_DP32(value, MEAS_CTRL_SHADOWED_LO, lo); + unsigned width = ot_clkmgr_get_measure_width(mreg->clock); + uint32_t mask = (1u << width) - 1u; + uint32_t value = (hi & mask) | ((lo & mask) << width); trace_ot_clkmgr_reset_meas(s->ot_id, ix, mreg->clock->name, lo, hi); @@ -1127,8 +1197,12 @@ static void ot_clkmgr_write(void *opaque, hwaddr addr, uint64_t val64, case R_ALERT_TEST: val32 &= ALERT_TEST_MASK; for (unsigned ix = 0; ix < ALERT_COUNT; ix++) { - ibex_irq_set(&s->alerts[ix], (int)((val32 >> ix) & 0x1u)); + if ((val32 >> ix) & 0x1u) { + ibex_irq_set(&s->alerts[ix], 1); + ibex_irq_set(&s->alerts[ix], 0); + } } + ot_clkmgr_update_alerts(s); break; case R_EXTCLK_CTRL_REGWEN: val32 &= R_EXTCLK_CTRL_REGWEN_EN_MASK; @@ -1138,6 +1212,14 @@ static void ot_clkmgr_write(void *opaque, hwaddr addr, uint64_t val64, if (s->regs[R_EXTCLK_CTRL_REGWEN]) { val32 &= R_EXTCLK_CTRL_SEL_MASK | R_EXTCLK_CTRL_HI_SPEED_SEL_MASK; s->regs[reg] = val32; + uint32_t sel = FIELD_EX32(val32, EXTCLK_CTRL, SEL); + s->regs[R_EXTCLK_STATUS] = + (s->lc_hw_debug_en && sel == OT_MULTIBITBOOL4_TRUE) ? + OT_MULTIBITBOOL4_TRUE : + OT_MULTIBITBOOL4_FALSE; + for (unsigned ix = 0; ix < s->measure_count; ix++) { + ot_clkmgr_check_measurement(s, ix); + } } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: EXTCLK_CTRL protected w/ REGWEN\n", __func__); @@ -1148,21 +1230,13 @@ static void ot_clkmgr_write(void *opaque, hwaddr addr, uint64_t val64, s->regs[reg] &= val32; break; case R_JITTER_ENABLE: - if (s->regs[R_JITTER_REGWEN] || - s->version == OT_CLKMGR_VERSION_EG_1_0_0) { - if (s->version == OT_CLKMGR_VERSION_EG_1_0_0) { - qemu_log_mask( - LOG_GUEST_ERROR, - "%s: JITTER_ENABLE should be protected w/ REGWEN,\n" - "but is allowed due to a known bug in Earlgrey 1.0.0\n", - __func__); - } - val32 &= R_JITTER_ENABLE_VAL_MASK; - s->regs[reg] = val32; - } else { - qemu_log_mask(LOG_GUEST_ERROR, - "%s: JITTER_ENABLE protected w/ REGWEN\n", __func__); - } + /* + * In clkmgr_reg_top.sv (u_jitter_enable), .we is jitter_enable_we + * (not gated by jitter_regwen) and .wd is hardwired to + * prim_mubi_pkg::MuBi4True (0x6). Any write latches JITTER_ENABLE to + * MuBi4True until reset. + */ + s->regs[reg] = OT_MULTIBITBOOL4_TRUE; break; case R_CLK_ENABLES: { uint32_t prev = s->regs[reg]; @@ -1220,33 +1294,26 @@ static void ot_clkmgr_write(void *opaque, hwaddr addr, uint64_t val64, switch (offset) { case 0u: - if (mreg->ctrl_en == OT_MULTIBITBOOL4_TRUE) { - val32 &= MEAS_CTRL_EN_MASK; - mreg->ctrl_en = val32; - } else { - qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: %s protected w/ EN\n", - __func__, s->ot_id, reg_name); - } + val32 &= MEAS_CTRL_EN_MASK; + mreg->ctrl_en = val32; + ot_clkmgr_check_measurement(s, measure); break; - case 1u: - if (mreg->ctrl_en) { - val32 &= - MEAS_CTRL_SHADOWED_HI_MASK | MEAS_CTRL_SHADOWED_LO_MASK; - switch (ot_shadow_reg_write(&mreg->ctrl, val32)) { - case OT_SHADOW_REG_STAGED: - case OT_SHADOW_REG_COMMITTED: - break; - case OT_SHADOW_REG_ERROR: - default: - s->regs[R_RECOV_ERR_CODE] |= - R_RECOV_ERR_CODE_SHADOW_UPDATE_ERR_MASK; - ot_clkmgr_update_alerts(s); - } - } else { - qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: %s protected w/ EN\n", - __func__, s->ot_id, reg_name); + case 1u: { + unsigned width = ot_clkmgr_get_measure_width(mreg->clock); + val32 &= (1u << (2u * width)) - 1u; + switch (ot_shadow_reg_write(&mreg->ctrl, val32)) { + case OT_SHADOW_REG_STAGED: + break; + case OT_SHADOW_REG_COMMITTED: + ot_clkmgr_check_measurement(s, measure); + break; + case OT_SHADOW_REG_ERROR: + default: + s->regs[R_RECOV_ERR_CODE] |= + R_RECOV_ERR_CODE_SHADOW_UPDATE_ERR_MASK; + ot_clkmgr_update_alerts(s); } - break; + } break; default: g_assert_not_reached(); break; @@ -1267,6 +1334,10 @@ static void ot_clkmgr_write(void *opaque, hwaddr addr, uint64_t val64, (s->measure_count * 2u)))) - 1u; s->regs[reg_err] &= ~val32; /* RW1C */ + for (unsigned ix = 0; ix < s->measure_count; ix++) { + ot_clkmgr_check_measurement(s, ix); + } + ot_clkmgr_update_alerts(s); break; case R_FATAL_ERR_CODE: qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n", @@ -1293,12 +1364,51 @@ static const Property ot_clkmgr_properties[] = { DEFINE_PROP_UINT8("version", OtClkMgrState, version, UINT8_MAX), }; +static uint32_t ot_clkmgr_get_permit(OtClkMgrState *s, hwaddr reg) +{ + if (reg < R_MEASURE_REG_BASE) { + return 0x1u; + } + if (reg < (R_MEASURE_REG_BASE + (s->measure_count * 2u))) { + unsigned measure = (reg - R_MEASURE_REG_BASE) >> 1u; + unsigned offset = (reg - R_MEASURE_REG_BASE) & 1u; + if (offset == 0u) { + return 0x1u; + } + unsigned width = + ot_clkmgr_get_measure_width(s->measure_regs[measure].clock); + unsigned bytes = ((2u * width) + 7u) / 8u; + return (1u << bytes) - 1u; + } + unsigned reg_err = reg - (s->measure_count * 2u); + if (reg_err == R_RECOV_ERR_CODE) { + return 0x3u; + } + return 0x1u; +} + +static bool ot_clkmgr_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)attrs; + if (!is_write) { + return true; + } + OtClkMgrState *s = opaque; + uint32_t permit = ot_clkmgr_get_permit(s, R32_OFF(addr)); + uint32_t reg_be = (((1u << size) - 1u) << (addr & 3u)) & 0xfu; + return (permit & ~reg_be) == 0u; +} + static const MemoryRegionOps ot_clkmgr_regs_ops = { .read = &ot_clkmgr_read, .write = &ot_clkmgr_write, .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.min_access_size = 1u, + .valid.max_access_size = 4u, + .valid.accepts = &ot_clkmgr_accepts, }; static void ot_clkmgr_reset_enter(Object *obj, ResetType type) @@ -1310,28 +1420,41 @@ static void ot_clkmgr_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } - memset(s->regs, 0, sizeof(s->regs)); - - s->regs[R_EXTCLK_CTRL_REGWEN] = 0x1u; - s->regs[R_EXTCLK_CTRL] = 0x99u; - s->regs[R_EXTCLK_STATUS] = 0x9u; - s->regs[R_JITTER_REGWEN] = 0x1u; - s->regs[R_JITTER_ENABLE] = 0x9u; - s->regs[R_CLK_ENABLES] = 0xfu; - s->regs[R_CLK_HINTS] = 0xfu; - s->regs[R_CLK_HINTS_STATUS] = 0xfu; - s->regs[R_MEASURE_CTRL_REGWEN] = 0x1u; - - for (unsigned ix = 0; ix < s->swcg_count; ix++) { - g_assert(s->swcgs[ix] && s->swcgs[ix]->output); - s->swcgs[ix]->output->disabled = 0; + if (ot_rstmgr_is_low_power_exit()) { + /* + * In top_earlgrey.sv (u_clkmgr_aon), all clkmgr resets belong to + * DomainAonSel, which is not asserted on low-power exit. However, + * AST drops calib_rdy_i (ast_init_done) to MuBi4False during deep + * sleep, which sets MEASURE_CTRL_REGWEN=1 (clkmgr.sv:555-558) and + * clears any enabled *_MEAS_CTRL_EN to MuBi4False + * (clkmgr_meas_chk.sv:81-85). + */ + s->regs[R_MEASURE_CTRL_REGWEN] = 0x1u; + for (unsigned ix = 0; ix < s->measure_count; ix++) { + if (s->measure_regs[ix].ctrl_en != OT_MULTIBITBOOL4_FALSE) { + s->measure_regs[ix].ctrl_en = OT_MULTIBITBOOL4_FALSE; + } + } + } else { + memset(s->regs, 0, sizeof(s->regs)); + + s->regs[R_EXTCLK_CTRL_REGWEN] = 0x1u; + s->regs[R_EXTCLK_CTRL] = 0x99u; + s->regs[R_EXTCLK_STATUS] = 0x9u; + s->regs[R_JITTER_REGWEN] = 0x1u; + s->regs[R_JITTER_ENABLE] = 0x9u; + s->regs[R_CLK_ENABLES] = 0xfu; + s->regs[R_CLK_HINTS] = 0xfu; + s->regs[R_CLK_HINTS_STATUS] = 0xfu; + s->regs[R_MEASURE_CTRL_REGWEN] = 0x1u; + + ot_clkmgr_reset_measure_regs(s); } - ot_clkmgr_reset_measure_regs(s); + ot_clkmgr_update_swcg(s, (1u << s->swcg_count) - 1u); - for (unsigned ix = 0; ix < ALERT_COUNT; ix++) { - ibex_irq_set(&s->alerts[ix], 0); - } + ibex_irq_set(&s->alerts[ALERT_FATAL], 0); + ot_clkmgr_update_alerts(s); if (!s->input_clock_connected) { ot_clkmgr_connect_input_clocks(s); @@ -1398,13 +1521,35 @@ static void ot_clkmgr_realize(DeviceState *dev, Error **errp) sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); } +static void ot_clkmgr_lc_hw_debug_en(void *opaque, int irq, int level) +{ + OtClkMgrState *s = opaque; + + g_assert(irq == 0); + + s->lc_hw_debug_en = (bool)level; + uint32_t sel = FIELD_EX32(s->regs[R_EXTCLK_CTRL], EXTCLK_CTRL, SEL); + s->regs[R_EXTCLK_STATUS] = + (s->lc_hw_debug_en && sel == OT_MULTIBITBOOL4_TRUE) ? + OT_MULTIBITBOOL4_TRUE : + OT_MULTIBITBOOL4_FALSE; + for (unsigned ix = 0; ix < s->measure_count; ix++) { + ot_clkmgr_check_measurement(s, ix); + } +} + static void ot_clkmgr_init(Object *obj) { OtClkMgrState *s = OT_CLKMGR(obj); + s->lc_hw_debug_en = true; + for (unsigned ix = 0; ix < ALERT_COUNT; ix++) { ibex_qdev_init_irq(obj, &s->alerts[ix], OT_DEVICE_ALERT); } + + qdev_init_gpio_in_named(DEVICE(obj), &ot_clkmgr_lc_hw_debug_en, + OT_CLKMGR_LC_HW_DEBUG_EN, 1); } static void ot_clkmgr_class_init(ObjectClass *klass, const void *data) diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index d9b73deb1c7db..a7889f1834acf 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -1489,10 +1489,24 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { .type = TYPE_SPLIT_IRQ, .gpio = IBEXGPIOCONNDEFS( OT_EG_SOC_S2D(0, SRAM_MAIN_CTRL, OT_SRAM_CTRL_HW_DEBUG_EN, 0), - OT_EG_SOC_S2D(1, SRAM_RET_CTRL, OT_SRAM_CTRL_HW_DEBUG_EN, 0) + OT_EG_SOC_S2D(1, PWRMGR, OT_PWRMGR_LC_HW_DEBUG_EN, 0), + OT_EG_SOC_S2D(2, TAP_CTRL, TAP_CTRL_RBB_ENABLE, 0), + OT_EG_SOC_S2D(3, RV_DM, PULP_RV_DM_LC_HW_DEBUG_EN, 0), + OT_EG_SOC_S2D(4, CLKMGR, OT_CLKMGR_LC_HW_DEBUG_EN, 0) ), .prop = IBEXDEVICEPROPDEFS( - IBEX_DEV_UINT_PROP("num-lines", 2u) /* @todo to be changed */ + IBEX_DEV_UINT_PROP("num-lines", 5u) + ) + }, + [OT_EG_SOC_SPLITTER_LC_DFT] = { + .type = TYPE_SPLIT_IRQ, + .gpio = IBEXGPIOCONNDEFS( + OT_EG_SOC_S2D(0, PWRMGR, OT_PWRMGR_LC_DFT_EN, 0), + OT_EG_SOC_S2D(1, RV_DM, PULP_RV_DM_LC_DFT_EN, 0), + OT_EG_SOC_S2D(2, OTP_CTRL, OT_LC_BROADCAST, OT_OTP_LC_DFT_EN) + ), + .prop = IBEXDEVICEPROPDEFS( + IBEX_DEV_UINT_PROP("num-lines", 3u) ) }, [OT_EG_SOC_SPLITTER_LC_ESCALATE] = { diff --git a/include/hw/opentitan/ot_clkmgr.h b/include/hw/opentitan/ot_clkmgr.h index 058c2ba490b9d..f9643569b7b30 100644 --- a/include/hw/opentitan/ot_clkmgr.h +++ b/include/hw/opentitan/ot_clkmgr.h @@ -42,7 +42,8 @@ typedef enum { #define OT_CLOCK_HINT_PREFIX "ot-clock-hint-" -#define OT_CLKMGR_CLOCK_INPUT TYPE_OT_CLKMGR "-clock-in" +#define OT_CLKMGR_CLOCK_INPUT TYPE_OT_CLKMGR "-clock-in" +#define OT_CLKMGR_LC_HW_DEBUG_EN TYPE_OT_CLKMGR "-lc-hw-debug-en" /* deprecated definitions */ typedef enum { From 3e95763946db6a6cd25822836de003bfb239311f Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:38 +0000 Subject: [PATCH 37/52] [ot] hw/opentitan: adc_ctrl: implement low-power filter and wakeup Implement the Earlgrey AON ADC Controller device model (TYPE_OT_ADC_CTRL, hw/ip/adc_ctrl/rtl/adc_ctrl_core.sv and adc_ctrl_fsm.sv): - Model all 29 adc_ctrl registers with subword write byte-enable checks (ADC_CTRL_PERMIT), Status-type RO INTR_STATE evaluation, and one-shot ALERT_TEST pulses. - Implement the 200 kHz AON clock conversion FSM (PWRUP_TIME, WAKEUP_TIME, LP_SAMPLE_CNT, NP_SAMPLE_CNT, ONESHOT_MODE re-trigger latching, and ADC_FSM_RST gating), including 16-bit NP_SAMPLE_CTL=0 (65,536 samples) and 8-bit LP_SAMPLE_CTL=0 (256 samples) threshold underflow per adc_ctrl_fsm.sv:183, 187. - Evaluate all 8 dual-channel 10-bit voltage filters (in-range and out-of-range match conditions), LP-to-NP transition detection (TRANS), FILTER_STATUS / ADC_INTR_STATUS W1C clearing, and OT_ADC_CTRL_WKUP wakeup generation to pwrmgr. - Preserve AON filter/wakeup configuration across non-PoR sleep exits and wire OT_EG_SOC_DEV_ADC_CTRL in hw/riscv/ot_earlgrey.c. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I2301230c2ab4014c75310ff481e811396a6a6964 --- hw/opentitan/Kconfig | 2 + hw/opentitan/meson.build | 1 + hw/opentitan/ot_adc_ctrl.c | 533 +++++++++++++++++++++++++++++ hw/opentitan/trace-events | 5 + hw/riscv/Kconfig | 1 + hw/riscv/ot_earlgrey.c | 14 +- include/hw/opentitan/ot_adc_ctrl.h | 20 ++ 7 files changed, 567 insertions(+), 9 deletions(-) create mode 100644 hw/opentitan/ot_adc_ctrl.c create mode 100644 include/hw/opentitan/ot_adc_ctrl.h diff --git a/hw/opentitan/Kconfig b/hw/opentitan/Kconfig index ca6f106988fd4..a67cbc679ec91 100644 --- a/hw/opentitan/Kconfig +++ b/hw/opentitan/Kconfig @@ -1,5 +1,7 @@ # OpenTitan devices +config OT_ADC_CTRL + bool config OT_ADDRESS_SPACE bool diff --git a/hw/opentitan/meson.build b/hw/opentitan/meson.build index 2d557ac037508..62a7b3d77f5b0 100644 --- a/hw/opentitan/meson.build +++ b/hw/opentitan/meson.build @@ -4,6 +4,7 @@ libtomcrypt_dep = subproject('libtomcrypt').get_variable('libtomcrypt_dep') # OpenTitan devices +system_ss.add(when: 'CONFIG_OT_ADC_CTRL', if_true: files('ot_adc_ctrl.c')) system_ss.add(when: 'CONFIG_OT_ADDRESS_SPACE', if_true: files('ot_address_space.c')) system_ss.add(when: 'CONFIG_OT_AES', if_true: [files('ot_aes.c'), libtomcrypt_dep]) system_ss.add(when: 'CONFIG_OT_ALERT', if_true: files('ot_alert.c')) diff --git a/hw/opentitan/ot_adc_ctrl.c b/hw/opentitan/ot_adc_ctrl.c new file mode 100644 index 0000000000000..b74037b893ef4 --- /dev/null +++ b/hw/opentitan/ot_adc_ctrl.c @@ -0,0 +1,533 @@ +/* + * QEMU OpenTitan ADC Controller device + * + * Copyright lowRISC contributors. + * + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qemu/timer.h" +#include "hw/opentitan/ot_adc_ctrl.h" +#include "hw/opentitan/ot_alert.h" +#include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_rstmgr.h" +#include "hw/registerfields.h" +#include "hw/riscv/ibex_common.h" +#include "hw/riscv/ibex_irq.h" +#include "hw/sysbus.h" +#include "trace.h" + +#define NUM_ADC_FILTERS 8u +#define NUM_ADC_CHANNELS 2u + +/* clang-format off */ +REG32(INTR_STATE, 0x00u) + FIELD(INTR_STATE, MATCH_PENDING, 0u, 1u) +REG32(INTR_ENABLE, 0x04u) + FIELD(INTR_ENABLE, MATCH_PENDING, 0u, 1u) +REG32(INTR_TEST, 0x08u) + FIELD(INTR_TEST, MATCH_PENDING, 0u, 1u) +REG32(ALERT_TEST, 0x0cu) + FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u) +REG32(ADC_EN_CTL, 0x10u) + FIELD(ADC_EN_CTL, ADC_ENABLE, 0u, 1u) + FIELD(ADC_EN_CTL, ONESHOT_MODE, 1u, 1u) +REG32(ADC_PD_CTL, 0x14u) + FIELD(ADC_PD_CTL, LP_MODE, 0u, 1u) + FIELD(ADC_PD_CTL, PWRUP_TIME, 4u, 4u) + FIELD(ADC_PD_CTL, WAKEUP_TIME, 8u, 24u) +REG32(ADC_LP_SAMPLE_CTL, 0x18u) + FIELD(ADC_LP_SAMPLE_CTL, LP_SAMPLE_CNT, 0u, 8u) +REG32(ADC_SAMPLE_CTL, 0x1cu) + FIELD(ADC_SAMPLE_CTL, NP_SAMPLE_CNT, 0u, 16u) +REG32(ADC_FSM_RST, 0x20u) + FIELD(ADC_FSM_RST, RST_EN, 0u, 1u) +REG32(ADC_CHN0_FILTER_CTL_0, 0x24u) +REG32(ADC_CHN0_FILTER_CTL_1, 0x28u) +REG32(ADC_CHN0_FILTER_CTL_2, 0x2cu) +REG32(ADC_CHN0_FILTER_CTL_3, 0x30u) +REG32(ADC_CHN0_FILTER_CTL_4, 0x34u) +REG32(ADC_CHN0_FILTER_CTL_5, 0x38u) +REG32(ADC_CHN0_FILTER_CTL_6, 0x3cu) +REG32(ADC_CHN0_FILTER_CTL_7, 0x40u) +REG32(ADC_CHN1_FILTER_CTL_0, 0x44u) +REG32(ADC_CHN1_FILTER_CTL_1, 0x48u) +REG32(ADC_CHN1_FILTER_CTL_2, 0x4cu) +REG32(ADC_CHN1_FILTER_CTL_3, 0x50u) +REG32(ADC_CHN1_FILTER_CTL_4, 0x54u) +REG32(ADC_CHN1_FILTER_CTL_5, 0x58u) +REG32(ADC_CHN1_FILTER_CTL_6, 0x5cu) +REG32(ADC_CHN1_FILTER_CTL_7, 0x60u) +REG32(ADC_CHN_VAL_0, 0x64u) +REG32(ADC_CHN_VAL_1, 0x68u) +REG32(ADC_WAKEUP_CTL, 0x6cu) +REG32(FILTER_STATUS, 0x70u) +REG32(ADC_INTR_CTL, 0x74u) +REG32(ADC_INTR_STATUS, 0x78u) +REG32(ADC_FSM_STATE, 0x7cu) +/* clang-format on */ + +#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t)) + +#define R_LAST_REG (R_ADC_FSM_STATE) +#define REGS_COUNT (R_LAST_REG + 1u) +#define REGS_SIZE (REGS_COUNT * sizeof(uint32_t)) +#define REG_NAME(_reg_) \ + ((((_reg_) < REGS_COUNT) && REG_NAMES[_reg_]) ? REG_NAMES[_reg_] : "?") + +#define REG_NAME_ENTRY(_reg_) [R_##_reg_] = stringify(_reg_) +static const char *REG_NAMES[REGS_COUNT] = { + REG_NAME_ENTRY(INTR_STATE), + REG_NAME_ENTRY(INTR_ENABLE), + REG_NAME_ENTRY(INTR_TEST), + REG_NAME_ENTRY(ALERT_TEST), + REG_NAME_ENTRY(ADC_EN_CTL), + REG_NAME_ENTRY(ADC_PD_CTL), + REG_NAME_ENTRY(ADC_LP_SAMPLE_CTL), + REG_NAME_ENTRY(ADC_SAMPLE_CTL), + REG_NAME_ENTRY(ADC_FSM_RST), + REG_NAME_ENTRY(ADC_CHN0_FILTER_CTL_0), + REG_NAME_ENTRY(ADC_CHN0_FILTER_CTL_1), + REG_NAME_ENTRY(ADC_CHN0_FILTER_CTL_2), + REG_NAME_ENTRY(ADC_CHN0_FILTER_CTL_3), + REG_NAME_ENTRY(ADC_CHN0_FILTER_CTL_4), + REG_NAME_ENTRY(ADC_CHN0_FILTER_CTL_5), + REG_NAME_ENTRY(ADC_CHN0_FILTER_CTL_6), + REG_NAME_ENTRY(ADC_CHN0_FILTER_CTL_7), + REG_NAME_ENTRY(ADC_CHN1_FILTER_CTL_0), + REG_NAME_ENTRY(ADC_CHN1_FILTER_CTL_1), + REG_NAME_ENTRY(ADC_CHN1_FILTER_CTL_2), + REG_NAME_ENTRY(ADC_CHN1_FILTER_CTL_3), + REG_NAME_ENTRY(ADC_CHN1_FILTER_CTL_4), + REG_NAME_ENTRY(ADC_CHN1_FILTER_CTL_5), + REG_NAME_ENTRY(ADC_CHN1_FILTER_CTL_6), + REG_NAME_ENTRY(ADC_CHN1_FILTER_CTL_7), + REG_NAME_ENTRY(ADC_CHN_VAL_0), + REG_NAME_ENTRY(ADC_CHN_VAL_1), + REG_NAME_ENTRY(ADC_WAKEUP_CTL), + REG_NAME_ENTRY(FILTER_STATUS), + REG_NAME_ENTRY(ADC_INTR_CTL), + REG_NAME_ENTRY(ADC_INTR_STATUS), + REG_NAME_ENTRY(ADC_FSM_STATE), +}; +#undef REG_NAME_ENTRY + +#define FILTER_CTL_MIN_V_SHIFT 2u +#define FILTER_CTL_MIN_V_MASK 0x3ffu +#define FILTER_CTL_COND_MASK (1u << 12u) +#define FILTER_CTL_MAX_V_SHIFT 18u +#define FILTER_CTL_MAX_V_MASK 0x3ffu +#define FILTER_CTL_EN_MASK (1u << 31u) +#define FILTER_CTL_REG_MASK 0x8ffc1ffcu +#define ADC_PD_CTL_MASK 0xfffffff1u +#define FSM_STATE_NP_0 12u + +#define CHN_VAL_VALUE_SHIFT 2u +#define CHN_VAL_INTR_VALUE_SHIFT 18u +#define CHN_VAL_MASK 0x3ffu + +#define FILTER_STATUS_TRANS_BIT 8u +#define ADC_INTR_ONESHOT_BIT 9u +#define WAKEUP_CTL_MASK 0x1ffu +#define FILTER_STATUS_MASK 0x1ffu +#define ADC_INTR_MASK 0x3ffu + +struct OtAdcCtrlState { + SysBusDevice parent_obj; + + MemoryRegion mmio; + IbexIRQ irq; + IbexIRQ alert; + IbexIRQ wkup; + + uint32_t *regs; + QEMUTimer *fsm_timer; + bool trigger_q; +}; + +static void ot_adc_ctrl_update_irqs(OtAdcCtrlState *s) +{ + /* + * In RTL (adc_ctrl_intr.sv), intr_state is a Status-type interrupt driven + * by status_irq_value = |adc_intr_status. + */ + if (s->regs[R_ADC_INTR_STATUS] != 0u || s->regs[R_INTR_TEST] != 0u) { + s->regs[R_INTR_STATE] |= R_INTR_STATE_MATCH_PENDING_MASK; + } else { + s->regs[R_INTR_STATE] &= ~R_INTR_STATE_MATCH_PENDING_MASK; + } + + bool irq_level = (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]) != 0u; + ibex_irq_set(&s->irq, irq_level); + + /* + * In RTL (adc_ctrl_core.sv): + * wkup_req_o = |(filter_status.match & adc_wakeup_ctl.match_en) || + * (filter_status.trans & adc_wakeup_ctl.trans_en) + */ + bool wkup_level = (s->regs[R_FILTER_STATUS] & s->regs[R_ADC_WAKEUP_CTL] & + WAKEUP_CTL_MASK) != 0u; + ibex_irq_set(&s->wkup, wkup_level); +} + +/* + * In chip_earlgrey_cw340.sv, AST inputs adc_a0_ai and adc_a1_ai are tied to '0, + * so adc_ana.sv outputs adc_d_ch0_o = 10'h000 and adc_d_ch1_o = 10'h000. + */ +#define DEFAULT_CHN0_VAL 0x000u +#define DEFAULT_CHN1_VAL 0x000u + +static bool ot_adc_ctrl_filter_match_chn(uint32_t filter_ctl, uint32_t chn_val) +{ + uint32_t min_v = + (filter_ctl >> FILTER_CTL_MIN_V_SHIFT) & FILTER_CTL_MIN_V_MASK; + uint32_t max_v = + (filter_ctl >> FILTER_CTL_MAX_V_SHIFT) & FILTER_CTL_MAX_V_MASK; + bool out_of_range = (filter_ctl & FILTER_CTL_COND_MASK) != 0u; + + if (!out_of_range) { + return (min_v <= chn_val) && (chn_val <= max_v); + } + return (min_v > chn_val) || (chn_val > max_v); +} + +static void ot_adc_ctrl_update_chn_val(OtAdcCtrlState *s, uint32_t chn0_val, + uint32_t chn1_val, bool intr_we) +{ + chn0_val &= CHN_VAL_MASK; + chn1_val &= CHN_VAL_MASK; + + uint32_t intr0 = + s->regs[R_ADC_CHN_VAL_0] & (CHN_VAL_MASK << CHN_VAL_INTR_VALUE_SHIFT); + uint32_t intr1 = + s->regs[R_ADC_CHN_VAL_1] & (CHN_VAL_MASK << CHN_VAL_INTR_VALUE_SHIFT); + if (intr_we) { + intr0 = chn0_val << CHN_VAL_INTR_VALUE_SHIFT; + intr1 = chn1_val << CHN_VAL_INTR_VALUE_SHIFT; + } + + s->regs[R_ADC_CHN_VAL_0] = (chn0_val << CHN_VAL_VALUE_SHIFT) | intr0; + s->regs[R_ADC_CHN_VAL_1] = (chn1_val << CHN_VAL_VALUE_SHIFT) | intr1; +} + +static void ot_adc_ctrl_eval_sample(OtAdcCtrlState *s, uint32_t chn0_val, + uint32_t chn1_val) +{ + bool any_match = false; + for (unsigned k = 0; k < NUM_ADC_FILTERS; k++) { + uint32_t f0 = s->regs[R_ADC_CHN0_FILTER_CTL_0 + k]; + uint32_t f1 = s->regs[R_ADC_CHN1_FILTER_CTL_0 + k]; + bool en0 = (f0 & FILTER_CTL_EN_MASK) != 0u; + bool en1 = (f1 & FILTER_CTL_EN_MASK) != 0u; + + bool match_k = (en0 || en1) && + (!en0 || ot_adc_ctrl_filter_match_chn(f0, chn0_val)) && + (!en1 || ot_adc_ctrl_filter_match_chn(f1, chn1_val)); + if (!match_k) { + continue; + } + + s->regs[R_FILTER_STATUS] |= (1u << k); + if (s->regs[R_ADC_INTR_CTL] & (1u << k)) { + s->regs[R_ADC_INTR_STATUS] |= (1u << k); + } + any_match = true; + } + + ot_adc_ctrl_update_chn_val(s, chn0_val, chn1_val, any_match); + + if (any_match && (s->regs[R_ADC_PD_CTL] & R_ADC_PD_CTL_LP_MODE_MASK)) { + /* Low power to normal power FSM transition occurred */ + s->regs[R_FILTER_STATUS] |= (1u << FILTER_STATUS_TRANS_BIT); + if (s->regs[R_ADC_INTR_CTL] & (1u << FILTER_STATUS_TRANS_BIT)) { + s->regs[R_ADC_INTR_STATUS] |= (1u << FILTER_STATUS_TRANS_BIT); + } + } + + ot_adc_ctrl_update_irqs(s); +} + +static void ot_adc_ctrl_fsm_timer_cb(void *opaque) +{ + OtAdcCtrlState *s = OT_ADC_CTRL(opaque); + if (!(s->regs[R_ADC_EN_CTL] & R_ADC_EN_CTL_ADC_ENABLE_MASK) || + (s->regs[R_ADC_FSM_RST] & R_ADC_FSM_RST_RST_EN_MASK)) { + return; + } + ot_adc_ctrl_eval_sample(s, DEFAULT_CHN0_VAL, DEFAULT_CHN1_VAL); +} + +static void ot_adc_ctrl_update_fsm(OtAdcCtrlState *s) +{ + if (s->regs[R_ADC_FSM_RST] & R_ADC_FSM_RST_RST_EN_MASK) { + s->trigger_q = false; + s->regs[R_ADC_FSM_STATE] = 0u; + timer_del(s->fsm_timer); + return; + } + + bool adc_en = (s->regs[R_ADC_EN_CTL] & R_ADC_EN_CTL_ADC_ENABLE_MASK) != 0u; + bool oneshot = + (s->regs[R_ADC_EN_CTL] & R_ADC_EN_CTL_ONESHOT_MODE_MASK) != 0u; + bool trigger_l2h = !s->trigger_q && adc_en; + s->trigger_q = adc_en; + + if (!adc_en) { + s->regs[R_ADC_FSM_STATE] = 0u; + timer_del(s->fsm_timer); + return; + } + + if (oneshot) { + timer_del(s->fsm_timer); + if (trigger_l2h) { + ot_adc_ctrl_update_chn_val(s, DEFAULT_CHN0_VAL, DEFAULT_CHN1_VAL, + true); + if (s->regs[R_ADC_INTR_CTL] & (1u << ADC_INTR_ONESHOT_BIT)) { + s->regs[R_ADC_INTR_STATUS] |= (1u << ADC_INTR_ONESHOT_BIT); + } + s->regs[R_ADC_FSM_STATE] = 0u; + ot_adc_ctrl_update_irqs(s); + } + } else { + s->regs[R_ADC_FSM_STATE] = FSM_STATE_NP_0; + /* + * RTL: adc_ctrl_fsm.sv:183, 187 computes + * np_sample_cnt_thresh = cfg_normal_mode_val_i - 1'b1 (16-bit) and + * lp_sample_cnt_thresh = cfg_lp_mode_val_i - 1'b1 (8-bit), so + * configuring 0 underflows to 0xffff (65536 samples) / 0xff (256 + * samples). + */ + int64_t samples = (uint16_t)(s->regs[R_ADC_SAMPLE_CTL] - 1u) + 1LL; + if (s->regs[R_ADC_PD_CTL] & R_ADC_PD_CTL_LP_MODE_MASK) { + samples += (uint8_t)(s->regs[R_ADC_LP_SAMPLE_CTL] - 1u) + 1LL; + } + timer_mod_ns(s->fsm_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + + MAX(samples * 25000LL, 100000LL)); + } +} + +static bool ot_adc_ctrl_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t permit = + (reg == R_ADC_PD_CTL || + (reg >= R_ADC_CHN0_FILTER_CTL_0 && reg <= R_ADC_CHN_VAL_1)) ? + 0xfu : + (reg == R_ADC_SAMPLE_CTL || + (reg >= R_ADC_WAKEUP_CTL && reg <= R_ADC_INTR_STATUS)) ? + 0x3u : + 0x1u; + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 0x3u)); + return !is_write || (permit & ~reg_be) == 0u; +} + +static uint64_t ot_adc_ctrl_regs_read(void *opaque, hwaddr addr, unsigned size) +{ + OtAdcCtrlState *s = opaque; + uint32_t val32; + + hwaddr reg = R32_OFF(addr); + + switch (reg) { + case R_INTR_TEST: + case R_ALERT_TEST: + qemu_log_mask(LOG_GUEST_ERROR, "%s: W/O register 0x%02x (%s)\n", + __func__, (uint32_t)addr, REG_NAME(reg)); + val32 = 0; + break; + default: + val32 = s->regs[reg]; + break; + } + + (void)size; + + uint32_t pc = ibex_get_current_pc(); + trace_ot_adc_ctrl_io_read_out((uint32_t)addr, REG_NAME(reg), val32, pc); + + return (uint64_t)val32; +} + +static void ot_adc_ctrl_regs_write(void *opaque, hwaddr addr, uint64_t val64, + unsigned size) +{ + OtAdcCtrlState *s = opaque; + (void)size; + uint32_t val32 = (uint32_t)val64; + + hwaddr reg = R32_OFF(addr); + + uint32_t pc = ibex_get_current_pc(); + trace_ot_adc_ctrl_io_write((uint32_t)addr, REG_NAME(reg), val32, pc); + + switch (reg) { + case R_INTR_STATE: + /* RW1C if status source is clear */ + val32 &= R_INTR_STATE_MATCH_PENDING_MASK; + if (s->regs[R_ADC_INTR_STATUS] == 0u) { + s->regs[R_INTR_STATE] &= ~val32; + } + ot_adc_ctrl_update_irqs(s); + break; + case R_INTR_ENABLE: + val32 &= R_INTR_ENABLE_MATCH_PENDING_MASK; + s->regs[R_INTR_ENABLE] = val32; + ot_adc_ctrl_update_irqs(s); + break; + case R_INTR_TEST: + s->regs[R_INTR_TEST] = val32 & R_INTR_TEST_MATCH_PENDING_MASK; + ot_adc_ctrl_update_irqs(s); + break; + case R_ALERT_TEST: + val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } + break; + case R_ADC_EN_CTL: + s->regs[R_ADC_EN_CTL] = val32 & 0x3u; + ot_adc_ctrl_update_fsm(s); + break; + case R_ADC_PD_CTL: + s->regs[R_ADC_PD_CTL] = val32 & ADC_PD_CTL_MASK; + if ((s->regs[R_ADC_EN_CTL] & R_ADC_EN_CTL_ADC_ENABLE_MASK) && + !(s->regs[R_ADC_EN_CTL] & R_ADC_EN_CTL_ONESHOT_MODE_MASK)) { + ot_adc_ctrl_update_fsm(s); + } + break; + case R_ADC_LP_SAMPLE_CTL: + s->regs[R_ADC_LP_SAMPLE_CTL] = val32 & 0xffu; + break; + case R_ADC_SAMPLE_CTL: + s->regs[R_ADC_SAMPLE_CTL] = val32 & 0xffffu; + break; + case R_ADC_FSM_RST: + s->regs[R_ADC_FSM_RST] = val32 & 0x1u; + ot_adc_ctrl_update_fsm(s); + break; + case R_ADC_CHN0_FILTER_CTL_0 ... R_ADC_CHN0_FILTER_CTL_7: + case R_ADC_CHN1_FILTER_CTL_0 ... R_ADC_CHN1_FILTER_CTL_7: + s->regs[reg] = val32 & FILTER_CTL_REG_MASK; + if ((s->regs[R_ADC_EN_CTL] & R_ADC_EN_CTL_ADC_ENABLE_MASK) && + !(s->regs[R_ADC_EN_CTL] & R_ADC_EN_CTL_ONESHOT_MODE_MASK)) { + ot_adc_ctrl_update_fsm(s); + } + break; + case R_ADC_WAKEUP_CTL: + s->regs[R_ADC_WAKEUP_CTL] = val32 & WAKEUP_CTL_MASK; + ot_adc_ctrl_update_irqs(s); + break; + case R_FILTER_STATUS: + /* RW1C */ + s->regs[R_FILTER_STATUS] &= ~(val32 & FILTER_STATUS_MASK); + ot_adc_ctrl_update_irqs(s); + break; + case R_ADC_INTR_CTL: + s->regs[R_ADC_INTR_CTL] = val32 & ADC_INTR_MASK; + ot_adc_ctrl_update_irqs(s); + break; + case R_ADC_INTR_STATUS: + /* RW1C */ + s->regs[R_ADC_INTR_STATUS] &= ~(val32 & ADC_INTR_MASK); + ot_adc_ctrl_update_irqs(s); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: R/O register 0x%02x (%s)\n", + __func__, (uint32_t)addr, REG_NAME(reg)); + break; + } +} + +static const MemoryRegionOps ot_adc_ctrl_regs_ops = { + .read = &ot_adc_ctrl_regs_read, + .write = &ot_adc_ctrl_regs_write, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl.min_access_size = 4u, + .impl.max_access_size = 4u, + .valid.accepts = &ot_adc_ctrl_regs_accepts, +}; + +static void ot_adc_ctrl_reset_enter(Object *obj, ResetType type) +{ + OtAdcCtrlState *s = OT_ADC_CTRL(obj); + + ibex_irq_set(&s->irq, 0); + ibex_irq_set(&s->alert, 0); + + if (type != RESET_TYPE_COLD && ot_rstmgr_is_low_power_exit()) { + /* + * ADC_CTRL_AON resides in DomainAonSel. Low-power wakeup from deep + * sleep only resets Domain0Sel, preserving AON registers and pending + * status. + */ + return; + } + + if (s->fsm_timer) { + timer_del(s->fsm_timer); + } + memset(s->regs, 0, REGS_SIZE); + s->trigger_q = false; + s->regs[R_ADC_PD_CTL] = 0x64070u; + s->regs[R_ADC_LP_SAMPLE_CTL] = 0x4u; + s->regs[R_ADC_SAMPLE_CTL] = 0x9bu; +} + +static void ot_adc_ctrl_reset_exit(Object *obj, ResetType type) +{ + OtAdcCtrlState *s = OT_ADC_CTRL(obj); + (void)type; + + ot_adc_ctrl_update_irqs(s); +} + +static void ot_adc_ctrl_init(Object *obj) +{ + OtAdcCtrlState *s = OT_ADC_CTRL(obj); + + memory_region_init_io(&s->mmio, obj, &ot_adc_ctrl_regs_ops, s, + TYPE_OT_ADC_CTRL, REGS_SIZE); + sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); + + s->regs = g_new0(uint32_t, REGS_COUNT); + s->fsm_timer = + timer_new_ns(QEMU_CLOCK_VIRTUAL, &ot_adc_ctrl_fsm_timer_cb, s); + ibex_sysbus_init_irq(obj, &s->irq); + ibex_qdev_init_irq(obj, &s->alert, OT_DEVICE_ALERT); + ibex_qdev_init_irq(obj, &s->wkup, OT_ADC_CTRL_WKUP); +} + +static void ot_adc_ctrl_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + (void)data; + + set_bit(DEVICE_CATEGORY_MISC, dc->categories); + + ResettableClass *rc = RESETTABLE_CLASS(klass); + rc->phases.enter = &ot_adc_ctrl_reset_enter; + rc->phases.exit = &ot_adc_ctrl_reset_exit; +} + +static const TypeInfo ot_adc_ctrl_info = { + .name = TYPE_OT_ADC_CTRL, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(OtAdcCtrlState), + .instance_init = &ot_adc_ctrl_init, + .class_init = &ot_adc_ctrl_class_init, +}; + +static void ot_adc_ctrl_register_types(void) +{ + type_register_static(&ot_adc_ctrl_info); +} + +type_init(ot_adc_ctrl_register_types); diff --git a/hw/opentitan/trace-events b/hw/opentitan/trace-events index 8bcc0a1715edf..3c5a12b30f7ef 100644 --- a/hw/opentitan/trace-events +++ b/hw/opentitan/trace-events @@ -1,5 +1,10 @@ # OpenTitan EarlGrey Trace Events +# ot_adc_ctrl.c + +ot_adc_ctrl_io_read_out(uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "addr=0x%02x (%s), val=0x%x, pc=0x%x" +ot_adc_ctrl_io_write(uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "addr=0x%02x (%s), val=0x%x, pc=0x%x" + # ot_aes.c ot_aes_buffer(const char *id, const char *mode, const char * msg, const char * hexbuf) "%s: [%s] %s: %s" diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index 68a00b766951c..6e39fc193aa2a 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -76,6 +76,7 @@ config OT_EARLGREY select IBEX_CLOCK_SRC select IBEX_COMMON select IBEX_GPIO + select OT_ADC_CTRL select OT_AES select OT_ALERT select OT_AON_TIMER diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index a7889f1834acf..ba29880190d07 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -36,6 +36,7 @@ #include "hw/jtag/tap_ctrl.h" #include "hw/jtag/tap_ctrl_rbb.h" #include "hw/misc/pulp_rv_dm.h" +#include "hw/opentitan/ot_adc_ctrl.h" #include "hw/opentitan/ot_address_space.h" #include "hw/opentitan/ot_aes.h" #include "hw/opentitan/ot_alert.h" @@ -1045,19 +1046,14 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { ) }, [OT_EG_SOC_DEV_ADC_CTRL] = { - .type = TYPE_OT_UNIMP, - .cfg = &ibex_unimp_configure, + .type = TYPE_OT_ADC_CTRL, .memmap = MEMMAPENTRIES( { .base = 0x40440000u } ), - .prop = IBEXDEVICEPROPDEFS( - IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "adc_ctrl"), - IBEX_DEV_UINT_PROP("size", 0x80u), - IBEX_DEV_UINT_PROP("irq-count", 1u), - IBEX_DEV_UINT_PROP("alert-count", 1u), - IBEX_DEV_BOOL_PROP("warn-once", true) - ), .gpio = IBEXGPIOCONNDEFS( + OT_EG_SOC_GPIO_SYSBUS_IRQ(0, PLIC, 155), + OT_EG_SOC_SIGNAL(OT_ADC_CTRL_WKUP, 0, PWRMGR, + OT_PWRMGR_WKUP, OT_PWRMGR_WAKEUP_ADC_CTRL), OT_EG_SOC_GPIO_ALERT(0, 28) ) }, diff --git a/include/hw/opentitan/ot_adc_ctrl.h b/include/hw/opentitan/ot_adc_ctrl.h new file mode 100644 index 0000000000000..1ad10f9c8128a --- /dev/null +++ b/include/hw/opentitan/ot_adc_ctrl.h @@ -0,0 +1,20 @@ +/* + * QEMU OpenTitan ADC Controller device + * + * Copyright lowRISC contributors. + * + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef HW_OPENTITAN_OT_ADC_CTRL_H +#define HW_OPENTITAN_OT_ADC_CTRL_H + +#include "qom/object.h" + +#define TYPE_OT_ADC_CTRL "ot-adc-ctrl" +OBJECT_DECLARE_SIMPLE_TYPE(OtAdcCtrlState, OT_ADC_CTRL) + +#define OT_ADC_CTRL_WKUP TYPE_OT_ADC_CTRL "-wkup" + +#endif /* HW_OPENTITAN_OT_ADC_CTRL_H */ From 2be2088a4b55e2786603f8734c97c745754efbd5 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:38 +0000 Subject: [PATCH 38/52] [ot] hw/opentitan: uart: fix baudrate timing, RX break, and irq Align the OpenTitan Earlgrey UART model (hw/opentitan/ot_uart.c and include/hw/opentitan/ot_uart.h) with hw/ip/uart/rtl/uart_core.sv and uart_reg_top.sv: - Add NCO baudrate-paced TX and RX timers (tx_timer, rx_timer) and an rx_staging_fifo so FIFO_STATUS levels, STATUS.TXIDLE/TXEMPTY, and TX/RX watermark and overflow interrupts match hardware timing. - Implement TIMEOUT_CTRL RX timeout timer (rx_timeout_timer), including immediate INTR_STATE.RX_TIMEOUT assertion when EN=1 and VAL=0. - Add ot_uart_update_pinmux() to dynamically route chardev TX/RX streams across UART0..3 when pinmux MIO_OUTSEL / MIO_PERIPH_INSEL changes, support CTRL.LLPBK line loopback, and detect PTY parity mismatches. - Latch status-type bits in INTR_TEST, pulse ALERT_TEST for 1 cycle, preserve OVRD.TXEN and update VAL (0xffff) when enabled, allow RDATA reads to pop rx_fifo when CTRL.RX=0, default toggle-break to true, and enforce UART_PERMIT sub-word MMIO byte-enable rules. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ibb9d3907042b7abf30004dc80d0ea6396a6a6964 --- hw/opentitan/ot_uart.c | 642 ++++++++++++++++++++++++++++----- include/hw/opentitan/ot_uart.h | 3 + 2 files changed, 559 insertions(+), 86 deletions(-) diff --git a/hw/opentitan/ot_uart.c b/hw/opentitan/ot_uart.c index 644c624da56d8..a3775e1b092e1 100644 --- a/hw/opentitan/ot_uart.c +++ b/hw/opentitan/ot_uart.c @@ -31,11 +31,14 @@ */ #include "qemu/osdep.h" +#include #include "qemu/fifo8.h" #include "qemu/log.h" #include "qemu/module.h" +#include "qemu/timer.h" #include "qapi/error.h" #include "chardev/char-fe.h" +#include "exec/icount.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_uart.h" @@ -45,6 +48,7 @@ #include "hw/riscv/ibex_clock_src.h" #include "hw/riscv/ibex_common.h" #include "hw/riscv/ibex_irq.h" +#include "io/channel-file.h" #include "trace.h" /* clang-format off */ @@ -122,13 +126,16 @@ REG32(TIMEOUT_CTRL, 0x30u) R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK) #define CTRL_SUP_MASK \ - (R_CTRL_RX_MASK | R_CTRL_TX_MASK | R_CTRL_SLPBK_MASK | R_CTRL_NCO_MASK) + (R_CTRL_RX_MASK | R_CTRL_TX_MASK | R_CTRL_SLPBK_MASK | R_CTRL_LLPBK_MASK | \ + R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK | R_CTRL_RXBLVL_MASK | \ + R_CTRL_NCO_MASK) /* FIFO depths match uart_reg_pkg.sv (TxFifoDepth=32, RxFifoDepth=64). */ -#define OT_UART_NCO_BITS 16u -#define OT_UART_TX_FIFO_SIZE 32u -#define OT_UART_RX_FIFO_SIZE 64u -#define OT_UART_IRQ_NUM 9u +#define OT_UART_NCO_BITS 16u +#define OT_UART_TX_FIFO_SIZE 32u +#define OT_UART_RX_FIFO_SIZE 64u +#define OT_UART_RX_STAGING_FIFO_SIZE 2048u +#define OT_UART_IRQ_NUM 9u #define R32_OFF(_r_) ((_r_) / sizeof(uint32_t)) @@ -179,8 +186,26 @@ struct OtUARTState { CharFrontend chr; bool oversample_break; /* Should mock break in the oversampled VAL reg? */ bool toggle_break; /* Are incoming breaks temporary or toggled? */ + + OtUARTState *tx_chr_owner; + OtUARTState *rx_target_uart; + QEMUTimer *tx_timer; + bool tx_busy; + QEMUTimer *rx_timer; + Fifo8 rx_staging_fifo; + QEMUTimer *rx_pace_timer; + QEMUTimer *rx_timeout_timer; + int64_t last_rx_ns; + uint32_t rx_poll_count; }; +typedef struct { + Chardev parent; + QIOChannel *ioc; +} OtPtyChardev; + +static OtUARTState *ot_uart_instances[4]; + struct OtUARTClass { SysBusDeviceClass parent_class; ResettablePhases parent_phases; @@ -257,7 +282,8 @@ static uint32_t ot_uart_status_intr_bits(OtUARTState *s) static uint32_t ot_uart_intr_state(OtUARTState *s) { return (s->regs[R_INTR_STATE] & ~INTR_STATUS_MASK) | - ot_uart_status_intr_bits(s); + ot_uart_status_intr_bits(s) | + (s->regs[R_INTR_TEST] & INTR_STATUS_MASK); } static void ot_uart_update_irqs(OtUARTState *s) @@ -288,6 +314,110 @@ static bool ot_uart_is_rx_enabled(const OtUARTState *s) return (bool)FIELD_EX32(s->regs[R_CTRL], CTRL, RX); } +static CharFrontend *ot_uart_get_tx_chr(OtUARTState *s) +{ + if (s->tx_chr_owner) { + return &s->tx_chr_owner->chr; + } + return &s->chr; +} + +static CharFrontend *ot_uart_get_rx_chr(OtUARTState *s) +{ + for (int i = 0; i < 4; i++) { + if (ot_uart_instances[i] && ot_uart_instances[i]->rx_target_uart == s) { + return &ot_uart_instances[i]->chr; + } + } + return &s->chr; +} + +void ot_uart_update_pinmux(const uint32_t *mio_outsel, + const uint32_t *mio_periph_insel) +{ + static const unsigned out_pads[4] = { 26u, 14u, 5u, 1u }; + static const unsigned in_sels[4] = { 27u, 15u, 6u, 2u }; + + for (int u = 0; u < 4; u++) { + OtUARTState *s = ot_uart_instances[u]; + if (s) { + OtUARTState *owner = NULL; + for (int ch = 0; ch < 4; ch++) { + if (mio_outsel[out_pads[ch]] == (uint32_t)(45 + u)) { + owner = ot_uart_instances[ch]; + break; + } + } + if (!owner && mio_outsel[out_pads[u]] <= 2u) { + owner = s; + } + s->tx_chr_owner = owner; + } + } + + for (int ch = 0; ch < 4; ch++) { + OtUARTState *ch_dev = ot_uart_instances[ch]; + if (ch_dev) { + OtUARTState *target = NULL; + for (int u = 0; u < 4; u++) { + if (mio_periph_insel[42 + u] == in_sels[ch]) { + target = ot_uart_instances[u]; + break; + } + } + if (!target && mio_periph_insel[42 + ch] <= 1u && + mio_outsel[out_pads[ch]] <= 2u) { + target = ch_dev; + } + ch_dev->rx_target_uart = target; + } + } + + for (int ch = 0; ch < 4; ch++) { + OtUARTState *ch_dev = ot_uart_instances[ch]; + if (ch_dev && ch_dev->rx_target_uart) { + OtUARTState *target = ch_dev->rx_target_uart; + if ((ot_uart_is_rx_enabled(target) || + (target->regs[R_CTRL] & R_CTRL_LLPBK_MASK)) && + !ot_uart_is_sys_loopack_enabled(target)) { + qemu_chr_fe_accept_input(&ch_dev->chr); + } + } + } +} + +static bool ot_uart_check_pty_parity(OtUARTState *s, OtUARTState *rx_owner) +{ + if (!(s->regs[R_CTRL] & R_CTRL_PARITY_EN_MASK)) { + return true; + } + if (!rx_owner || !rx_owner->chr.chr || !rx_owner->chr.chr->filename) { + return true; + } + if (!g_str_has_prefix(rx_owner->chr.chr->filename, "pty:")) { + return true; + } + OtPtyChardev *pty = (OtPtyChardev *)rx_owner->chr.chr; + int master_fd = + (pty->ioc && + object_dynamic_cast(OBJECT(pty->ioc), TYPE_QIO_CHANNEL_FILE)) ? + QIO_CHANNEL_FILE(pty->ioc)->fd : + -1; + if (master_fd >= 0) { + struct termios tio; + if (tcgetattr(master_fd, &tio) == 0) { + if ((tio.c_cflag & PARENB) || (tio.c_iflag & INPCK)) { + bool host_odd = (tio.c_cflag & PARODD) != 0; + bool dev_odd = (s->regs[R_CTRL] & R_CTRL_PARITY_ODD_MASK) != 0; + if (host_odd != dev_odd) { + return false; + } + } + } + } + return true; +} + static void ot_uart_check_baudrate(const OtUARTState *s) { uint32_t nco = FIELD_EX32(s->regs[R_CTRL], CTRL, NCO); @@ -300,37 +430,98 @@ static void ot_uart_check_baudrate(const OtUARTState *s) } } -static void ot_uart_reset_rx_fifo(OtUARTState *s) +static int64_t ot_uart_char_ns(const OtUARTState *s); + +static void ot_uart_update_val(OtUARTState *s) { - fifo8_reset(&s->rx_fifo); - s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK; - s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK; - if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) { - qemu_chr_fe_accept_input(&s->chr); + if ((ot_uart_is_tx_enabled(s) || ot_uart_is_rx_enabled(s)) && + FIELD_EX32(s->regs[R_CTRL], CTRL, NCO) != 0) { + s->regs[R_VAL] = UINT16_MAX; } } -static int ot_uart_can_receive(void *opaque) +static void ot_uart_update_rx_timeout(OtUARTState *s) +{ + if (!s->rx_timeout_timer) { + return; + } + if (s->regs[R_TIMEOUT_CTRL] & R_TIMEOUT_CTRL_EN_MASK) { + uint32_t val = FIELD_EX32(s->regs[R_TIMEOUT_CTRL], TIMEOUT_CTRL, VAL); + if (val == 0u) { + /* + * In uart_core.sv, event_rx_timeout = (rx_timeout_count_q == + * uart_rxto_val) & uart_rxto_en. Since rx_timeout_count_q is 0 when + * idle/empty, enabling TIMEOUT_CTRL with VAL == 0 immediately + * asserts INTR_STATE.RX_TIMEOUT. + */ + s->regs[R_INTR_STATE] |= INTR_RX_TIMEOUT_MASK; + ot_uart_update_irqs(s); + timer_del(s->rx_timeout_timer); + return; + } + if (ot_uart_is_rx_enabled(s) && !fifo8_is_empty(&s->rx_fifo)) { + int64_t bit_ns = MAX(ot_uart_char_ns(s) / 10LL, 100LL); + int64_t delay_ns = bit_ns * (int64_t)val; + timer_mod(s->rx_timeout_timer, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + delay_ns); + return; + } + } + timer_del(s->rx_timeout_timer); +} + +static void ot_uart_rx_timeout_timer_cb(void *opaque) { OtUARTState *s = opaque; - if (s->regs[R_CTRL] & R_CTRL_RX_MASK) { - return (int)fifo8_num_free(&s->rx_fifo); + if ((s->regs[R_TIMEOUT_CTRL] & R_TIMEOUT_CTRL_EN_MASK) && + ot_uart_is_rx_enabled(s) && !fifo8_is_empty(&s->rx_fifo)) { + s->regs[R_INTR_STATE] |= INTR_RX_TIMEOUT_MASK; + ot_uart_update_irqs(s); + ot_uart_update_rx_timeout(s); } - - return 0; } -static void ot_uart_receive(void *opaque, const uint8_t *buf, int size) +static OtUARTState *ot_uart_get_rx_owner(OtUARTState *s) { - OtUARTState *s = opaque; - size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size); + for (int i = 0; i < 4; i++) { + if (ot_uart_instances[i] && ot_uart_instances[i]->rx_target_uart == s) { + return ot_uart_instances[i]; + } + } + return s; +} +static void ot_uart_receive_bytes(OtUARTState *s, OtUARTState *rx_owner, + const uint8_t *buf, int size) +{ if (size && !s->toggle_break) { /* no longer breaking, so emulate idle in oversampled VAL register */ s->in_break = false; } + if (rx_owner && (s->regs[R_CTRL] & R_CTRL_LLPBK_MASK)) { + if (ot_uart_is_tx_enabled(s)) { + CharFrontend *tx_chr = ot_uart_get_tx_chr(s); + if (qemu_chr_fe_backend_connected(tx_chr)) { + qemu_chr_fe_write(tx_chr, buf, size); + } + } + return; + } + + if (rx_owner && !ot_uart_check_pty_parity(s, rx_owner)) { + s->regs[R_INTR_STATE] |= INTR_RX_PARITY_ERR_MASK; + ot_uart_update_irqs(s); + return; + } + + if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) { + return; + } + + size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size); + for (size_t index = 0; index < count; index++) { fifo8_push(&s->rx_fifo, buf[index]); } @@ -340,16 +531,114 @@ static void ot_uart_receive(void *opaque, const uint8_t *buf, int size) s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK; } + ot_uart_update_rx_timeout(s); + /* rx_watermark is status-type: computed live in ot_uart_update_irqs(). */ ot_uart_update_irqs(s); } +static void ot_uart_step_rx(OtUARTState *s) +{ + if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK) || + fifo8_is_empty(&s->rx_staging_fifo) || timer_pending(s->rx_timer)) { + return; + } + + bool can_push = !fifo8_is_full(&s->rx_fifo) || + ((s->regs[R_INTR_ENABLE] & INTR_RX_OVERFLOW_MASK) && + !(s->regs[R_INTR_ENABLE] & INTR_RX_WATERMARK_MASK) && + !(s->regs[R_INTR_STATE] & INTR_RX_OVERFLOW_MASK)); + if (can_push) { + uint8_t ch = fifo8_pop(&s->rx_staging_fifo); + ot_uart_receive_bytes(s, ot_uart_get_rx_owner(s), &ch, 1); + if (!fifo8_is_empty(&s->rx_staging_fifo)) { + timer_mod(s->rx_timer, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + ot_uart_char_ns(s)); + } else if (!ot_uart_is_sys_loopack_enabled(s)) { + qemu_chr_fe_accept_input(ot_uart_get_rx_chr(s)); + } + } +} + +static void ot_uart_rx_timer_cb(void *opaque) +{ + ot_uart_step_rx(opaque); +} + +static void ot_uart_reset_rx_fifo(OtUARTState *s) +{ + if (s->rx_timer) { + timer_del(s->rx_timer); + } + fifo8_reset(&s->rx_staging_fifo); + fifo8_reset(&s->rx_fifo); + ot_uart_update_rx_timeout(s); + if ((ot_uart_is_rx_enabled(s) || (s->regs[R_CTRL] & R_CTRL_LLPBK_MASK)) && + !ot_uart_is_sys_loopack_enabled(s)) { + qemu_chr_fe_accept_input(ot_uart_get_rx_chr(s)); + } +} + +static int ot_uart_can_receive(void *opaque) +{ + OtUARTState *rx_owner = opaque; + OtUARTState *s = rx_owner->rx_target_uart; + if (!s) { + return 0; + } + + if (s->regs[R_CTRL] & R_CTRL_LLPBK_MASK) { + return (int)OT_UART_RX_FIFO_SIZE; + } + + if (s->regs[R_CTRL] & R_CTRL_RX_MASK) { + return (int)fifo8_num_free(&s->rx_staging_fifo); + } + + return 0; +} + +static void ot_uart_receive(void *opaque, const uint8_t *buf, int size) +{ + OtUARTState *rx_owner = opaque; + OtUARTState *s = rx_owner->rx_target_uart; + if (!s || size <= 0) { + return; + } + + if (s->regs[R_CTRL] & R_CTRL_LLPBK_MASK) { + ot_uart_receive_bytes(s, rx_owner, buf, size); + return; + } + + if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) { + return; + } + + s->last_rx_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + for (int i = 0; i < size; i++) { + if (!fifo8_is_full(&s->rx_staging_fifo)) { + fifo8_push(&s->rx_staging_fifo, buf[i]); + } + } + + if (!timer_pending(s->rx_timer)) { + int64_t mult = + fifo8_is_empty(&s->rx_fifo) ? (int64_t)OT_UART_TX_FIFO_SIZE : 1LL; + timer_mod(s->rx_timer, s->last_rx_ns + mult * ot_uart_char_ns(s)); + } +} + static void ot_uart_event_handler(void *opaque, QEMUChrEvent event) { - OtUARTState *s = opaque; + OtUARTState *rx_owner = opaque; + OtUARTState *s = rx_owner->rx_target_uart; + if (!s) { + return; + } if (event == CHR_EVENT_BREAK) { - if (!s->in_break || !s->oversample_break) { + if (!s->in_break) { /* ignore CTRL.RXBLVL as we have no notion of break "time" */ s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK; ot_uart_update_irqs(s); @@ -366,15 +655,12 @@ static uint8_t ot_uart_read_rx_fifo(OtUARTState *s) { uint8_t val; - if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) { - return 0; - } - if (fifo8_is_empty(&s->rx_fifo)) { return 0; } val = fifo8_pop(&s->rx_fifo); + ot_uart_update_rx_timeout(s); /* * rx_watermark is status-type: draining the FIFO may drop it below the @@ -383,7 +669,8 @@ static uint8_t ot_uart_read_rx_fifo(OtUARTState *s) ot_uart_update_irqs(s); if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) { - qemu_chr_fe_accept_input(&s->chr); + ot_uart_step_rx(s); + qemu_chr_fe_accept_input(ot_uart_get_rx_chr(s)); } return val; @@ -391,14 +678,62 @@ static uint8_t ot_uart_read_rx_fifo(OtUARTState *s) static void ot_uart_reset_tx_fifo(OtUARTState *s) { + if (s->tx_timer) { + timer_del(s->tx_timer); + } + if (s->watch_tag > 0) { + g_source_remove(s->watch_tag); + s->watch_tag = 0; + } + s->tx_busy = false; fifo8_reset(&s->tx_fifo); - /* - * tx_done is event-type and latched here; tx_empty and tx_watermark are - * status-type and follow the (now empty) FIFO live via ot_uart_update_irqs. - */ - s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK; } +static void ot_uart_send_byte(OtUARTState *s, uint8_t ch) +{ + if (ot_uart_is_sys_loopack_enabled(s)) { + ot_uart_receive_bytes(s, NULL, &ch, 1); + } else { + CharFrontend *tx_chr = ot_uart_get_tx_chr(s); + if (qemu_chr_fe_backend_connected(tx_chr)) { + qemu_chr_fe_write(tx_chr, &ch, 1); + } + } +} + +static int64_t ot_uart_char_ns(const OtUARTState *s) +{ + uint32_t nco = FIELD_EX32(s->regs[R_CTRL], CTRL, NCO); + if (nco && s->pclk) { + return (int64_t)((10ULL * NANOSECONDS_PER_SECOND + << (R_CTRL_NCO_LENGTH + 4)) / + ((uint64_t)nco * (uint64_t)s->pclk)); + } + return 86805LL; +} + +static void ot_uart_tx_timer_cb(void *opaque) +{ + OtUARTState *s = opaque; + + if (ot_uart_is_tx_enabled(s) && !fifo8_is_empty(&s->tx_fifo)) { + uint8_t ch = fifo8_pop(&s->tx_fifo); + s->tx_busy = true; + ot_uart_send_byte(s, ch); + timer_mod(s->tx_timer, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + ot_uart_char_ns(s)); + } else { + s->tx_busy = false; + if (ot_uart_is_tx_enabled(s)) { + s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK; + } + } + ot_uart_update_irqs(s); +} + +static gboolean ot_uart_watch_cb(void *do_not_use, GIOCondition cond, + void *opaque); + static void ot_uart_xmit(OtUARTState *s) { const uint8_t *buf; @@ -424,21 +759,29 @@ static void ot_uart_xmit(OtUARTState *s) ot_uart_receive(s, buf, (int)size); } } else { - /* instant drain the fifo when there's no back-end */ - if (!qemu_chr_fe_backend_connected(&s->chr)) { - ot_uart_reset_tx_fifo(s); - ot_uart_update_irqs(s); - return; - } - - /* get a continuous buffer from the FIFO */ - buf = - fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size); - /* send as much as possible */ - ret = qemu_chr_fe_write(&s->chr, buf, (int)size); - /* if some characters where sent, remove them from the FIFO */ - if (ret >= 0) { - fifo8_drop(&s->tx_fifo, ret); + CharFrontend *tx_chr = ot_uart_get_tx_chr(s); + while (!fifo8_is_empty(&s->tx_fifo)) { + /* get a continuous buffer from the FIFO */ + buf = fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), + &size); + /* send as much as possible */ + ret = qemu_chr_fe_write(tx_chr, buf, (int)size); + /* if some characters were sent, remove them from the FIFO */ + if (ret > 0) { + fifo8_drop(&s->tx_fifo, ret); + } + if (ret < (int)size) { + if (s->watch_tag == 0) { + /* NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) + */ + s->watch_tag = + qemu_chr_fe_add_watch(tx_chr, G_IO_OUT | G_IO_HUP, + ot_uart_watch_cb, s); + /* NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) + */ + } + break; + } } } @@ -451,6 +794,31 @@ static void ot_uart_xmit(OtUARTState *s) ot_uart_update_irqs(s); } +static void ot_uart_tx_schedule(OtUARTState *s) +{ + if (!ot_uart_is_tx_enabled(s)) { + return; + } + bool use_timer = + (s->regs[R_INTR_ENABLE] & (INTR_TX_DONE_MASK | INTR_TX_WATERMARK_MASK | + INTR_TX_EMPTY_MASK)) != 0 || + s->tx_busy || ot_uart_is_sys_loopack_enabled(s) || + !qemu_chr_fe_backend_connected(ot_uart_get_tx_chr(s)) || + ot_uart_char_ns(s) > 100000LL; + if (!use_timer) { + ot_uart_xmit(s); + return; + } + if (!s->tx_busy && !fifo8_is_empty(&s->tx_fifo)) { + uint8_t ch = fifo8_pop(&s->tx_fifo); + s->tx_busy = true; + ot_uart_send_byte(s, ch); + timer_mod(s->tx_timer, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + ot_uart_char_ns(s)); + } + ot_uart_update_irqs(s); +} + static gboolean ot_uart_watch_cb(void *do_not_use, GIOCondition cond, void *opaque) { @@ -459,13 +827,14 @@ static gboolean ot_uart_watch_cb(void *do_not_use, GIOCondition cond, (void)cond; s->watch_tag = 0; - ot_uart_xmit(s); + ot_uart_tx_schedule(s); return FALSE; } static void uart_write_tx_fifo(OtUARTState *s, uint8_t val) { + s->rx_poll_count = 0u; if (fifo8_is_full(&s->tx_fifo)) { qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow"); return; @@ -474,7 +843,7 @@ static void uart_write_tx_fifo(OtUARTState *s, uint8_t val) fifo8_push(&s->tx_fifo, val); if (ot_uart_is_tx_enabled(s)) { - ot_uart_xmit(s); + ot_uart_tx_schedule(s); } else { /* tx_watermark/tx_empty are status-type: refresh from the new level. */ ot_uart_update_irqs(s); @@ -493,13 +862,61 @@ static void ot_uart_clock_input(void *opaque, int irq, int level) ot_uart_check_baudrate(s); } +static void ot_uart_rx_pace_timer_cb(void *opaque) +{ + OtUARTState *s = opaque; + + if (ot_uart_is_rx_enabled(s) && fifo8_is_empty(&s->rx_fifo) && + fifo8_is_empty(&s->rx_staging_fifo) && s->rx_poll_count >= 2u) { + int64_t now_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + bool recent = + s->last_rx_ns > 0 && (now_ns - s->last_rx_ns) < 1500000000LL; + s->rx_poll_count = 1u; + g_usleep(recent ? 200u : 10u); + qemu_chr_fe_accept_input(ot_uart_get_rx_chr(s)); + timer_mod(s->rx_pace_timer, now_ns + (recent ? 150000LL : 200000LL)); + } else { + s->rx_poll_count = 0u; + } +} + static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned size) { OtUARTState *s = opaque; (void)size; uint32_t val32; + if (!s->pclk) { + ot_common_stall_cpu_on_unclocked_mmio(DEVICE(s), addr); + return 0; + } + + ot_uart_step_rx(s); + + if (!fifo8_is_empty(&s->tx_fifo) && !s->tx_busy && + ot_uart_is_tx_enabled(s)) { + ot_uart_tx_schedule(s); + } + hwaddr reg = R32_OFF(addr); + if (reg == R_STATUS && ot_uart_is_rx_enabled(s) && + fifo8_is_empty(&s->rx_fifo)) { + if (++s->rx_poll_count >= 2u) { + if (!fifo8_is_empty(&s->rx_staging_fifo)) { + timer_del(s->rx_timer); + ot_uart_step_rx(s); + } else if (!timer_pending(s->rx_pace_timer)) { + int64_t now_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + bool recent = s->last_rx_ns > 0 && + (now_ns - s->last_rx_ns) < 1500000000LL; + timer_mod(s->rx_pace_timer, + now_ns + (recent ? 150000LL : 200000LL)); + } + } + } else if (!fifo8_is_empty(&s->rx_fifo)) { + s->rx_poll_count = 0u; + } + switch (reg) { case R_INTR_STATE: /* Status-type bits reflect the live FIFO condition, not a latch. */ @@ -527,7 +944,10 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned size) /* report TXEMPTY+TXIDLE or TXFULL */ switch (fifo8_num_used(&s->tx_fifo)) { case 0: - val32 |= R_STATUS_TXEMPTY_MASK | R_STATUS_TXIDLE_MASK; + val32 |= R_STATUS_TXEMPTY_MASK; + if (!s->tx_busy) { + val32 |= R_STATUS_TXIDLE_MASK; + } break; case OT_UART_TX_FIFO_SIZE: val32 |= R_STATUS_TXFULL_MASK; @@ -535,9 +955,6 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned size) default: break; } - if (!ot_uart_is_tx_enabled(s)) { - val32 |= R_STATUS_TXIDLE_MASK; - } if (!ot_uart_is_rx_enabled(s)) { val32 |= R_STATUS_RXIDLE_MASK; } @@ -553,30 +970,15 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned size) break; case R_VAL: /* - * This is not trivially implemented due to the QEMU UART - * interface. There is no way to reliably sample or oversample - * given our emulated interface, but some software might poll the - * value of this register to determine break conditions. - * - * As such, default to reporting 16 of the last sample received - * instead. This defaults to 16 idle high samples (as a stop bit is - * always the last received), except for when the `oversample-break` - * property is set and a break condition is received over UART RX, - * where we then show 16 low samples until the next valid UART - * transmission is received (or break is toggled off with the - * `toggle-break` property enabled). This will not be accurate, but - * should be sufficient to support basic software flows that - * essentially use UART break as a strapping mechanism. + * rx_val_q resets to 0x0000 and shifts in rx_in on tick_baud_x16 when + * (tx_enable || rx_enable) and CTRL.NCO != 0. When oversample-break + * is active and in_break is set, report 0x0000. */ - val32 = (s->in_break && s->oversample_break) ? 0u : UINT16_MAX; - qemu_log_mask(LOG_UNIMP, "%s: VAL only shows idle%s\n", __func__, - (s->oversample_break ? "/break" : "")); + val32 = (s->in_break && s->oversample_break) ? 0u : s->regs[R_VAL]; break; case R_OVRD: case R_TIMEOUT_CTRL: val32 = s->regs[reg]; - qemu_log_mask(LOG_UNIMP, "%s: %s is not supported\n", __func__, - REG_NAME(reg)); break; case R_ALERT_TEST: case R_INTR_TEST: @@ -606,6 +1008,11 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64, (void)size; uint32_t val32 = val64; + if (!s->pclk) { + ot_common_stall_cpu_on_unclocked_mmio(DEVICE(s), addr); + return; + } + hwaddr reg = R32_OFF(addr); uint32_t pc = ibex_get_current_pc(); @@ -621,22 +1028,29 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64, */ val32 &= INTR_MASK & ~INTR_STATUS_MASK; s->regs[R_INTR_STATE] &= ~val32; /* RW1C (event-type bits only) */ + ot_uart_update_rx_timeout(s); ot_uart_update_irqs(s); break; case R_INTR_ENABLE: val32 &= INTR_MASK; s->regs[R_INTR_ENABLE] = val32; ot_uart_update_irqs(s); + if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) { + qemu_chr_fe_accept_input(ot_uart_get_rx_chr(s)); + } break; case R_INTR_TEST: val32 &= INTR_MASK; - s->regs[R_INTR_STATE] |= val32; + s->regs[R_INTR_STATE] |= val32 & ~INTR_STATUS_MASK; + s->regs[R_INTR_TEST] = val32 & INTR_STATUS_MASK; ot_uart_update_irqs(s); break; case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; - s->regs[reg] = val32; - ibex_irq_set(&s->alert, (int)(bool)val32); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; case R_CTRL: if (val32 & ~CTRL_SUP_MASK) { @@ -646,17 +1060,32 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64, } uint32_t prev = s->regs[R_CTRL]; s->regs[R_CTRL] = val32 & CTRL_MASK; + ot_uart_update_val(s); + ot_uart_update_rx_timeout(s); uint32_t change = prev ^ s->regs[R_CTRL]; if (change & R_CTRL_NCO_MASK) { ot_uart_check_baudrate(s); } - if ((change & R_CTRL_RX_MASK) && ot_uart_is_rx_enabled(s) && + if ((change & (R_CTRL_RX_MASK | R_CTRL_LLPBK_MASK)) && + (ot_uart_is_rx_enabled(s) || + (s->regs[R_CTRL] & R_CTRL_LLPBK_MASK)) && !ot_uart_is_sys_loopack_enabled(s)) { - qemu_chr_fe_accept_input(&s->chr); + qemu_chr_fe_accept_input(ot_uart_get_rx_chr(s)); } - if ((change & R_CTRL_TX_MASK) && ot_uart_is_tx_enabled(s)) { - /* try sending pending data from TX FIFO if any */ - ot_uart_xmit(s); + if (change & R_CTRL_TX_MASK) { + if (ot_uart_is_tx_enabled(s)) { + /* try sending pending data from TX FIFO if any */ + ot_uart_tx_schedule(s); + } else if (s->tx_busy) { + if (s->tx_timer) { + timer_del(s->tx_timer); + } + s->tx_busy = false; + if (fifo8_is_empty(&s->tx_fifo)) { + s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK; + } + ot_uart_update_irqs(s); + } } break; case R_WDATA: @@ -678,15 +1107,13 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64, ot_uart_update_irqs(s); break; case R_OVRD: - if (val32 & R_OVRD_TXEN_MASK) { - qemu_log_mask(LOG_UNIMP, "%s: OVRD.TXEN is not supported\n", - __func__); - } - s->regs[R_OVRD] = val32 & R_OVRD_TXVAL_MASK; + s->regs[R_OVRD] = val32 & (R_OVRD_TXEN_MASK | R_OVRD_TXVAL_MASK); + ot_uart_update_val(s); break; case R_TIMEOUT_CTRL: s->regs[R_TIMEOUT_CTRL] = val32 & (R_TIMEOUT_CTRL_EN_MASK | R_TIMEOUT_CTRL_VAL_MASK); + ot_uart_update_rx_timeout(s); break; case R_STATUS: case R_RDATA: @@ -702,12 +1129,32 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64, } } +static const uint8_t UART_PERMIT[REGS_COUNT] = { + [R_INTR_STATE] = 0x3u, [R_INTR_ENABLE] = 0x3u, [R_INTR_TEST] = 0x3u, + [R_ALERT_TEST] = 0x1u, [R_CTRL] = 0xfu, [R_STATUS] = 0x1u, + [R_RDATA] = 0x1u, [R_WDATA] = 0x1u, [R_FIFO_CTRL] = 0x1u, + [R_FIFO_STATUS] = 0x7u, [R_OVRD] = 0x1u, [R_VAL] = 0x3u, + [R_TIMEOUT_CTRL] = 0xfu, +}; + +static bool ot_uart_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return reg < REGS_COUNT && + (!is_write || (UART_PERMIT[reg] & ~reg_be) == 0u); +} + static const MemoryRegionOps ot_uart_ops = { .read = ot_uart_read, .write = ot_uart_write, .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4, .impl.max_access_size = 4, + .valid.accepts = &ot_uart_accepts, }; static const Property ot_uart_properties[] = { @@ -717,7 +1164,7 @@ static const Property ot_uart_properties[] = { DEFINE_PROP_LINK("clock-src", OtUARTState, clock_src, TYPE_DEVICE, DeviceState *), DEFINE_PROP_BOOL("oversample-break", OtUARTState, oversample_break, false), - DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, false), + DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, true), }; static int ot_uart_be_change(void *opaque) @@ -752,6 +1199,14 @@ static void ot_uart_reset_enter(Object *obj, ResetType type) for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) { ibex_irq_set(&s->irqs[index], 0); } + if (s->rx_pace_timer) { + timer_del(s->rx_pace_timer); + } + if (s->rx_timeout_timer) { + timer_del(s->rx_timeout_timer); + } + s->rx_poll_count = 0; + s->last_rx_ns = 0; ot_uart_reset_tx_fifo(s); ot_uart_reset_rx_fifo(s); @@ -797,6 +1252,20 @@ static void ot_uart_realize(DeviceState *dev, Error **errp) fifo8_create(&s->tx_fifo, OT_UART_TX_FIFO_SIZE); fifo8_create(&s->rx_fifo, OT_UART_RX_FIFO_SIZE); + fifo8_create(&s->rx_staging_fifo, OT_UART_RX_STAGING_FIFO_SIZE); + + s->tx_chr_owner = s; + s->rx_target_uart = s; + s->tx_timer = timer_new_ns(OT_VIRTUAL_CLOCK, ot_uart_tx_timer_cb, s); + s->rx_timer = timer_new_ns(OT_VIRTUAL_CLOCK, ot_uart_rx_timer_cb, s); + s->rx_pace_timer = + timer_new_ns(OT_VIRTUAL_CLOCK, ot_uart_rx_pace_timer_cb, s); + s->rx_timeout_timer = + timer_new_ns(OT_VIRTUAL_CLOCK, ot_uart_rx_timeout_timer_cb, s); + if (s->ot_id[0] == 'u' && s->ot_id[1] >= '0' && s->ot_id[1] <= '3' && + s->ot_id[2] == '\0') { + ot_uart_instances[s->ot_id[1] - '0'] = s; + } qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive, ot_uart_receive, ot_uart_event_handler, ot_uart_be_change, s, NULL, @@ -807,6 +1276,7 @@ static void ot_uart_init(Object *obj) { OtUARTState *s = OT_UART(obj); + s->pclk = 1u; for (unsigned index = 0; index < OT_UART_IRQ_NUM; index++) { ibex_sysbus_init_irq(obj, &s->irqs[index]); } diff --git a/include/hw/opentitan/ot_uart.h b/include/hw/opentitan/ot_uart.h index f334f7b9ce06e..91bbfee093120 100644 --- a/include/hw/opentitan/ot_uart.h +++ b/include/hw/opentitan/ot_uart.h @@ -33,4 +33,7 @@ #define TYPE_OT_UART "ot-uart" OBJECT_DECLARE_TYPE(OtUARTState, OtUARTClass, OT_UART) +void ot_uart_update_pinmux(const uint32_t *mio_outsel, + const uint32_t *mio_periph_insel); + #endif /* HW_OPENTITAN_OT_UART_H */ From ec63e0de556f14d052ae564d1dd7484a90071e22 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:39 +0000 Subject: [PATCH 39/52] [ot] hw/opentitan: spi_host: implement dummy cycles, CSB, and stall Align `ot_spi_host` with Earlgrey RTL (`spi_host.sv` / `spi_host_core.sv` / `spi_host_fsm.sv`): - Model the 4-entry `u_cmd_queue` (`CmdDepth = 4`) plus active FSM slot (`fsm_active`), validating and queueing `COMMAND` writes even while `CONTROL.SPIEN == 0`. - Enforce level-sensitive `CONTROL.SW_RST` holding TX/RX/command FIFOs and the core FSM in synchronous reset while asserted. - Implement `STATUS.STALL` (`rx_stall`) and `rx_pending_word` buffering when `RXFIFO` is full, single-cycle `ALERT_TEST` pulsing, and `CONFIGOPTS` reserved bit 28 masking (`0xefffffff`). - Expose `ot_spi_host_get_passthrough_en()` and wire `SPI_HOST0` (`peri.io`) and `SPI_HOST1` (`peri.io_div2`) clocks in `ot_earlgrey.c`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I0db37964ffdc48b81a85fbd584fc4d666a6a6964 --- hw/opentitan/ot_spi_host.c | 313 ++++++++++++++++++++++++----- hw/riscv/ot_earlgrey.c | 4 +- include/hw/opentitan/ot_spi_host.h | 7 + 3 files changed, 277 insertions(+), 47 deletions(-) diff --git a/hw/opentitan/ot_spi_host.c b/hw/opentitan/ot_spi_host.c index 6a1391d1db957..10dfebf720902 100644 --- a/hw/opentitan/ot_spi_host.c +++ b/hw/opentitan/ot_spi_host.c @@ -59,9 +59,9 @@ /* fake delayed completion of HW commands */ #define FSM_COMPLETION_DELAY_NS 100U /* 100 ns (~ BH) */ /* initial FSM start up delay */ -#define FSM_START_DELAY_NS 20000U /* 20 uS */ +#define FSM_START_DELAY_NS 200U /* 200 ns */ -#define TXFIFO_LEN 288U /* bytes */ +#define TXFIFO_LEN 292U /* (72 + 1) * 4 bytes (u_tx_fifo + u_select) */ #define RXFIFO_LEN 256U /* bytes */ #define CMDFIFO_LEN 4U /* slots */ @@ -324,6 +324,12 @@ typedef struct { unsigned size; } OtSPIHostCmd; +typedef struct { + uint8_t csb; + uint8_t sck; + uint8_t sd0; +} OtSPIHostHalfClk; + struct OtSPIHostState { SysBusDevice parent_obj; @@ -365,6 +371,18 @@ struct OtSPIHostState { unsigned pclk; /* Current input clock */ const char *clock_src_name; /* IRQ name once connected */ + /* Bitbang waveform recording for GPIO sampling */ + OtSPIHostHalfClk bb_half_clks[2048]; + unsigned bb_len; + int64_t bb_start_ns; + uint64_t bb_half_clk_ns; + bool bb_cpol; + bool bb_cpha; + uint8_t bb_cur_tx_bytes[256]; + unsigned bb_cur_tx_len; + uint8_t bb_last_tx_bit; + unsigned bb_completed_txns; + /* properties */ char *ot_id; char *clock_name; /* clock name */ @@ -526,9 +544,19 @@ static bool ot_spi_host_is_tx(uint32_t command) return (bool)(FIELD_EX32(command, COMMAND, DIRECTION) & 0x2u); } +static bool ot_spi_host_is_stalled_in_idle(const OtSPIHostState *s) +{ + return s->active.state == CMD_ONGOING && s->active.size == 0u && + s->fsm.tx_stall && !FIELD_EX32(s->active.cmd.opts, CONFIGOPTS, CPHA); +} + static bool ot_spi_host_is_ready(const OtSPIHostState *s) { - return !cmdfifo_is_full(s->cmd_fifo); + if (cmdfifo_is_full(s->cmd_fifo)) { + return false; + } + return !ot_spi_host_is_stalled_in_idle(s) || + (cmdfifo_num_used(s->cmd_fifo) + 1u) < CMDFIFO_LEN; } /* Passthrough functionality is only implemented if there is one CS */ @@ -656,13 +684,17 @@ static uint32_t ot_spi_host_get_status(OtSPIHostState *s) status = FIELD_DP32(status, STATUS, TXSTALL, s->fsm.tx_stall); /* CMD */ - status = FIELD_DP32(status, STATUS, CMDQD, cmdfifo_num_used(s->cmd_fifo)); + bool stalled_in_idle = ot_spi_host_is_stalled_in_idle(s); + uint32_t cmdqd = + cmdfifo_num_used(s->cmd_fifo) + (stalled_in_idle ? 1u : 0u); + status = FIELD_DP32(status, STATUS, CMDQD, cmdqd); /* State */ status = FIELD_DP32(status, STATUS, READY, (uint32_t)ot_spi_host_is_ready(s)); - status = FIELD_DP32(status, STATUS, ACTIVE, - (uint32_t)(s->active.state != CMD_NONE)); + status = + FIELD_DP32(status, STATUS, ACTIVE, + (uint32_t)(s->active.state != CMD_NONE && !stalled_in_idle)); return status; } @@ -685,7 +717,8 @@ static uint32_t ot_spi_host_build_event_bits(OtSPIHostState *s) events = FIELD_DP32(events, EVENT_ENABLE, READY, (uint32_t)ot_spi_host_is_ready(s)); events = FIELD_DP32(events, EVENT_ENABLE, IDLE, - (uint32_t)(s->active.state == CMD_NONE)); + (uint32_t)(s->active.state == CMD_NONE || + ot_spi_host_is_stalled_in_idle(s))); return events; } @@ -709,7 +742,6 @@ static bool ot_spi_host_update_event(OtSPIHostState *s) bool event = (bool)eff_events; event |= (bool)(s->regs[R_INTR_TEST] & INTR_SPI_EVENT_MASK); - s->regs[R_INTR_TEST] &= ~INTR_SPI_EVENT_MASK; if (event) { s->regs[R_INTR_STATE] |= INTR_SPI_EVENT_MASK; } else { @@ -728,10 +760,6 @@ static bool ot_spi_host_update_event(OtSPIHostState *s) static bool ot_spi_host_update_error(OtSPIHostState *s) { - if (s->regs[R_ERROR_STATUS] & s->regs[R_ERROR_ENABLE]) { - s->regs[R_INTR_STATE] |= INTR_ERROR_MASK; - } - if (s->regs[R_INTR_TEST] & INTR_ERROR_MASK) { s->regs[R_INTR_TEST] &= ~INTR_ERROR_MASK; s->regs[R_INTR_STATE] |= INTR_ERROR_MASK; @@ -751,6 +779,18 @@ static void ot_spi_host_update_regs(OtSPIHostState *s) { ot_spi_host_update_error(s); ot_spi_host_update_event(s); + s->regs[R_STATUS] = ot_spi_host_get_status(s); +} + +static void ot_spi_host_raise_error(OtSPIHostState *s, uint32_t err_mask) +{ + s->regs[R_ERROR_STATUS] |= err_mask; + uint32_t error_enable = + s->regs[R_ERROR_ENABLE] | R_ERROR_STATUS_ACCESSINVAL_MASK; + if (err_mask & error_enable) { + s->regs[R_INTR_STATE] |= INTR_ERROR_MASK; + } + ot_spi_host_update_regs(s); } static void ot_spi_host_update_alert(OtSPIHostState *s) @@ -767,6 +807,127 @@ static void ot_spi_host_update_alert(OtSPIHostState *s) /* State machine and I/O */ /* ------------------------------------------------------------------------ */ +void ot_spi_host_clear_waveform(OtSPIHostState *s) +{ + if (s) { + s->bb_len = 0; + s->bb_start_ns = 0; + s->bb_cur_tx_len = 0; + s->bb_last_tx_bit = 0; + s->bb_completed_txns = 0; + } +} + +static void ot_spi_host_update_bb_config(OtSPIHostState *s, uint32_t opts) +{ + s->bb_cpol = (bool)FIELD_EX32(opts, CONFIGOPTS, CPOL); + s->bb_cpha = (bool)FIELD_EX32(opts, CONFIGOPTS, CPHA); + unsigned clkdiv = FIELD_EX32(opts, CONFIGOPTS, CLKDIV); + unsigned pclk = s->pclk ? s->pclk : 12000000u; + s->bb_half_clk_ns = + ((uint64_t)(clkdiv + 1u) * NANOSECONDS_PER_SECOND) / pclk; + ot_spi_host_clear_waveform(s); +} + +static void ot_spi_host_commit_bb_transaction(OtSPIHostState *s) +{ + if (s->bb_cur_tx_len == 0) { + return; + } + if (s->bb_len >= ARRAY_SIZE(s->bb_half_clks)) { + s->bb_cur_tx_len = 0; + s->bb_completed_txns++; + return; + } + uint8_t cpol = s->bb_cpol ? 1u : 0u; + uint8_t cpha = s->bb_cpha ? 1u : 0u; + uint8_t last_sd0 = 0u; + if (cpha == 1u && s->bb_len < ARRAY_SIZE(s->bb_half_clks)) { + s->bb_half_clks[s->bb_len++] = (OtSPIHostHalfClk){ + .csb = 0u, + .sck = cpol, + .sd0 = (s->bb_cur_tx_bytes[0] >> 7) & 1u, + }; + } + for (unsigned bi = 0; bi < s->bb_cur_tx_len; bi++) { + uint8_t b = s->bb_cur_tx_bytes[bi]; + for (int bit = 7; bit >= 0; bit--) { + uint8_t d0 = (b >> bit) & 1u; + last_sd0 = d0; + if (s->bb_len + 2u <= ARRAY_SIZE(s->bb_half_clks)) { + if (cpha == 0u) { + s->bb_half_clks[s->bb_len++] = + (OtSPIHostHalfClk){ .csb = 0u, .sck = cpol, .sd0 = d0 }; + s->bb_half_clks[s->bb_len++] = (OtSPIHostHalfClk){ + .csb = 0u, .sck = !cpol, .sd0 = d0 + }; + } else { + s->bb_half_clks[s->bb_len++] = (OtSPIHostHalfClk){ + .csb = 0u, .sck = !cpol, .sd0 = d0 + }; + s->bb_half_clks[s->bb_len++] = + (OtSPIHostHalfClk){ .csb = 0u, .sck = cpol, .sd0 = d0 }; + } + } + } + } + if (s->bb_len < ARRAY_SIZE(s->bb_half_clks)) { + s->bb_half_clks[s->bb_len++] = + (OtSPIHostHalfClk){ .csb = 0u, .sck = cpol, .sd0 = last_sd0 }; + } + for (unsigned idle = 0; idle < 4u; idle++) { + if (s->bb_len < ARRAY_SIZE(s->bb_half_clks)) { + s->bb_half_clks[s->bb_len++] = + (OtSPIHostHalfClk){ .csb = 1u, .sck = cpol, .sd0 = 1u }; + } + } + s->bb_cur_tx_len = 0; + s->bb_completed_txns++; +} + +bool ot_spi_host_is_waveform_ready(OtSPIHostState *s) +{ + return s && s->bb_completed_txns >= 3u; +} + +void ot_spi_host_set_waveform_start_ns(OtSPIHostState *s, int64_t val_ns, + bool relative) +{ + if (s) { + s->bb_start_ns = relative ? (s->bb_start_ns + val_ns) : val_ns; + } +} + +bool ot_spi_host_get_pin_level(OtSPIHostState *s, unsigned outsel, + int64_t now_ns) +{ + if (!s) { + return false; + } + bool idle_val = true; + if (outsel == 53u) { + idle_val = s->bb_cpol; + } + if (s->bb_len == 0 || s->bb_half_clk_ns == 0 || now_ns < s->bb_start_ns) { + return idle_val; + } + uint64_t idx = (uint64_t)(now_ns - s->bb_start_ns) / s->bb_half_clk_ns; + if (idx >= s->bb_len) { + return idle_val; + } + const OtSPIHostHalfClk *hc = &s->bb_half_clks[idx]; + switch (outsel) { + case 54u: /* CSB */ + return (bool)hc->csb; + case 53u: /* SCK */ + return (bool)hc->sck; + case 41u: /* SD0 */ + return (bool)hc->sd0; + default: + return idle_val; + } +} + static void ot_spi_host_internal_reset(OtSPIHostState *s) { trace_ot_spi_host_internal_reset(s->ot_id, s->start_delay_ns, @@ -781,6 +942,7 @@ static void ot_spi_host_internal_reset(OtSPIHostState *s) memset(&s->fsm, 0, sizeof(s->fsm)); memset(&s->active, 0, sizeof(s->active)); + ot_spi_host_clear_waveform(s); for (unsigned csid = 0u; csid < s->num_cs; csid++) { ot_spi_host_chip_select(s, csid, false); @@ -798,6 +960,10 @@ static void ot_spi_host_internal_reset(OtSPIHostState *s) static void ot_spi_host_step_fsm(OtSPIHostState *s, const char *cause) { + if (s->regs[R_ERROR_STATUS] || !(REG_GET(s, CONTROL, SPIEN))) { + return; + } + trace_ot_spi_host_fsm(s->ot_id, s->active.cmd.id, cause); ot_spi_host_update_event(s); @@ -871,6 +1037,19 @@ static void ot_spi_host_step_fsm(OtSPIHostState *s, const char *cause) uint8_t tx = write ? (uint8_t)txfifo_pop(s->tx_fifo, length == 1u) : SPI_DEFAULT_TX_RX_VALUE; + if (write) { + if (s->bb_cur_tx_len < ARRAY_SIZE(s->bb_cur_tx_bytes)) { + s->bb_cur_tx_bytes[s->bb_cur_tx_len++] = tx; + } + s->bb_last_tx_bit = tx & 1u; + } else if (read) { + uint8_t rx_sd0 = s->bb_last_tx_bit ? 0x80u : 0x00u; + s->bb_last_tx_bit = 0u; + if (s->bb_cur_tx_len < ARRAY_SIZE(s->bb_cur_tx_bytes)) { + s->bb_cur_tx_bytes[s->bb_cur_tx_len++] = rx_sd0; + } + } + uint8_t rx = SPI_DEFAULT_TX_RX_VALUE; if (s->fsm.output_en) { @@ -912,6 +1091,9 @@ static void ot_spi_host_step_fsm(OtSPIHostState *s, const char *cause) } else { s->active.cmd.command = FIELD_DP32(command, COMMAND, LEN, 0); s->active.state = CMD_EXECUTED; + if (!FIELD_EX32(command, COMMAND, CSAAT)) { + ot_spi_host_commit_bb_transaction(s); + } } post: @@ -1004,8 +1186,18 @@ static void ot_spi_host_schedule_fsm(void *opaque) { OtSPIHostState *s = opaque; + if (s->regs[R_ERROR_STATUS] || !(REG_GET(s, CONTROL, SPIEN))) { + return; + } + trace_ot_spi_host_fsm(s->ot_id, s->active.cmd.id, "sched"); + if (s->active.state == CMD_NONE && !cmdfifo_is_empty(s->cmd_fifo)) { + cmdfifo_pop(s->cmd_fifo, &s->active.cmd); + s->active.state = CMD_ONGOING; + s->active.size = 0u; + } + bool retire = s->active.state == CMD_EXECUTED; ot_spi_host_trace_status(s, "P>", ot_spi_host_get_status(s)); @@ -1081,8 +1273,7 @@ static uint64_t ot_spi_host_io_read(void *opaque, hwaddr addr, if (fifo8_num_used(s->rx_fifo) < sizeof(uint32_t)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Read underflow: %u\n", __func__, s->ot_id, fifo8_num_used(s->rx_fifo)); - REG_UPDATE(s, ERROR_STATUS, UNDERFLOW, 1); - ot_spi_host_update_regs(s); + ot_spi_host_raise_error(s, R_ERROR_STATUS_UNDERFLOW_MASK); val32 = 0u; break; } @@ -1148,6 +1339,9 @@ static uint64_t ot_spi_host_io_read(void *opaque, hwaddr addr, trace_cache.value = val32; #endif /* DISCARD_REPEATED_STATUS_TRACES */ + if (reg != R_RXDATA) { + val32 >>= (addr & 3u) * 8u; + } return val32; } @@ -1189,8 +1383,10 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; - s->regs[R_ALERT_TEST] = val32; - ot_spi_host_update_alert(s); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; case R_CONTROL: val32 &= R_CONTROL_MASK; @@ -1199,11 +1395,16 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64, ot_spi_host_internal_reset(s); } s->fsm.output_en = FIELD_EX32(val32, CONTROL, OUTPUT_EN); - if (!cmdfifo_is_empty(s->cmd_fifo)) { - ot_spi_host_step_fsm(s, "ctrl"); + if (REG_GET(s, CONTROL, SPIEN) && !s->regs[R_ERROR_STATUS] && + (s->active.state != CMD_NONE || !cmdfifo_is_empty(s->cmd_fifo))) { + ot_spi_host_schedule_fsm(s); } break; case R_CONFIGOPTS: + val32 &= R_CONFIGOPTS_MASK; + s->regs[reg] = val32; + ot_spi_host_update_bb_config(s, val32); + break; case R_CSID: s->regs[reg] = val32; break; @@ -1214,18 +1415,10 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64, } val32 &= R_COMMAND_MASK; - /* IP not enabled */ - if (!(REG_GET(s, CONTROL, SPIEN))) { - qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: no SPI/EN\n", __func__, - s->ot_id); - return; - } - if (!ot_spi_host_is_ready(s)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: busy (cmd_fifo full)\n", __func__, s->ot_id); - REG_UPDATE(s, ERROR_STATUS, CMDBUSY, 1u); - ot_spi_host_update_regs(s); + ot_spi_host_raise_error(s, R_ERROR_STATUS_CMDBUSY_MASK); break; } @@ -1236,7 +1429,7 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64, qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: invalid command parameters\n", __func__, s->ot_id); - REG_UPDATE(s, ERROR_STATUS, CMDINVAL, 1u); + ot_spi_host_raise_error(s, R_ERROR_STATUS_CMDINVAL_MASK); } unsigned csid = s->regs[R_CSID]; @@ -1245,10 +1438,15 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64, /* CSID exceeds max num_cs */ qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: invalid csid: %u\n", __func__, s->ot_id, csid); - REG_UPDATE(s, ERROR_STATUS, CSIDINVAL, 1u); + ot_spi_host_raise_error(s, R_ERROR_STATUS_CSIDINVAL_MASK); csid = 0; } + if (REG_GET(s, CONTROL, SW_RST)) { + ot_spi_host_update_regs(s); + break; + } + CmdFifoSlot slot = { .opts = s->regs[R_CONFIGOPTS], .command = val32, @@ -1265,7 +1463,8 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64, cmdfifo_push(s->cmd_fifo, &slot); - if (s->active.state == CMD_NONE) { + if (REG_GET(s, CONTROL, SPIEN) && s->active.state == CMD_NONE && + !s->regs[R_ERROR_STATUS]) { cmdfifo_pop(s->cmd_fifo, &s->active.cmd); s->active.state = CMD_ONGOING; s->active.size = 0u; @@ -1295,11 +1494,14 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64, __func__, s->ot_id, (uint32_t)addr, REG_NAME(reg)); break; case R_TXDATA: { - if (txfifo_is_full(s->tx_fifo)) { - REG_UPDATE(s, ERROR_STATUS, OVERFLOW, 1u); + if (REG_GET(s, CONTROL, SW_RST)) { ot_spi_host_update_regs(s); return; } + if (txfifo_is_full(s->tx_fifo)) { + ot_spi_host_raise_error(s, R_ERROR_STATUS_OVERFLOW_MASK); + return; + } txfifo_push(s->tx_fifo, val32, size); bool resume = (s->active.state != CMD_NONE) && s->fsm.tx_stall && @@ -1324,11 +1526,12 @@ static void ot_spi_host_io_write(void *opaque, hwaddr addr, uint64_t val64, */ val32 &= R_ERROR_STATUS_MASK; s->regs[R_ERROR_STATUS] &= ~val32; - if (!cmdfifo_is_empty(s->cmd_fifo) && !s->regs[R_ERROR_STATUS] && + if (!s->regs[R_ERROR_STATUS] && + (s->active.state != CMD_NONE || !cmdfifo_is_empty(s->cmd_fifo)) && !s->fsm.tx_stall && !s->fsm.rx_stall) { - ot_spi_host_step_fsm(s, "err"); + ot_spi_host_schedule_fsm(s); } else { - ot_spi_host_update_error(s); + ot_spi_host_update_regs(s); } break; case R_EVENT_ENABLE: @@ -1418,12 +1621,37 @@ ot_spi_host_device_passthrough_cs_input(void *opaque, int irq, int level) /* Device description/instanciation */ /* ------------------------------------------------------------------------ */ +static bool ot_spi_host_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + OtSPIHostState *s = opaque; + (void)attrs; + + if (!s->pclk) { + ot_common_stall_cpu_on_unclocked_mmio(DEVICE(s), addr); + return false; + } + uint32_t reg = R32_OFF(addr); + if (reg >= REGS_COUNT || reg == (is_write ? R_RXDATA : R_TXDATA)) { + return false; + } + if (is_write && reg != R_TXDATA) { + uint8_t permit = (reg >= R_CONTROL && reg <= R_CSID) ? + 0xfu : + (reg == R_COMMAND ? 0x3u : 0x1u); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return (permit & ~reg_be) == 0u; + } + return true; +} + /* clang-format off */ static const MemoryRegionOps ot_spi_host_ops = { - .read = ot_spi_host_io_read, - .write = ot_spi_host_io_write, + .read = &ot_spi_host_io_read, + .write = &ot_spi_host_io_write, /* OpenTitan default LE */ .endianness = DEVICE_LITTLE_ENDIAN, + .valid.accepts = &ot_spi_host_accepts, .impl = { /* although some registers only supports 2 or 4 byte write access */ .min_access_size = 1u, @@ -1455,15 +1683,9 @@ static void ot_spi_host_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } - s->regs[R_INTR_STATE] = 0x00u; - s->regs[R_INTR_ENABLE] = 0x00u; - s->regs[R_INTR_TEST] = 0x00u; - s->regs[R_ALERT_TEST] = 0x00u; + memset(s->regs, 0, REGS_SIZE); s->regs[R_CONTROL] = 0x7fu; - s->regs[R_CSID] = 0x00u; s->regs[R_ERROR_ENABLE] = 0x1fu; - s->regs[R_ERROR_STATUS] = 0x00u; - s->regs[R_EVENT_ENABLE] = 0x00u; s->on_reset = true; @@ -1529,8 +1751,9 @@ static void ot_spi_host_instance_init(Object *obj) { OtSPIHostState *s = OT_SPI_HOST(obj); + s->pclk = 1u; memory_region_init_io(&s->mmio, obj, &ot_spi_host_ops, s, TYPE_OT_SPI_HOST, - 0x40u); + REGS_SIZE); sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio); _Static_assert(IRQ_COUNT == ARRAY_SIZE(s->irqs), "Incoherent IRQ count"); diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index ba29880190d07..ae00d837e1e86 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -909,7 +909,7 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { .prop = IBEXDEVICEPROPDEFS( IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "spi0"), IBEX_DEV_UINT_PROP("bus-num", 0), - IBEX_DEV_STRING_PROP("clock-name", "peri.io_div4"), + IBEX_DEV_STRING_PROP("clock-name", "peri.io"), IBEX_DEV_UINT_PROP("version", 2u) ), }, @@ -930,7 +930,7 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { .prop = IBEXDEVICEPROPDEFS( IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "spi1"), IBEX_DEV_UINT_PROP("bus-num", 1), - IBEX_DEV_STRING_PROP("clock-name", "peri.io_div4"), + IBEX_DEV_STRING_PROP("clock-name", "peri.io_div2"), IBEX_DEV_UINT_PROP("version", 2u) ), }, diff --git a/include/hw/opentitan/ot_spi_host.h b/include/hw/opentitan/ot_spi_host.h index b71a196cda1c0..b14c51c677d64 100644 --- a/include/hw/opentitan/ot_spi_host.h +++ b/include/hw/opentitan/ot_spi_host.h @@ -61,4 +61,11 @@ struct OtSPIHostClass { #define OT_SPI_HOST_PASSTHROUGH_EN (TYPE_OT_SPI_HOST "-passthrough-en") #define OT_SPI_HOST_PASSTHROUGH_CS (TYPE_OT_SPI_HOST "-passthrough-cs") +bool ot_spi_host_is_waveform_ready(OtSPIHostState *s); +void ot_spi_host_set_waveform_start_ns(OtSPIHostState *s, int64_t val_ns, + bool relative); +void ot_spi_host_clear_waveform(OtSPIHostState *s); +bool ot_spi_host_get_pin_level(OtSPIHostState *s, unsigned outsel, + int64_t now_ns); + #endif /* HW_OPENTITAN_OT_SPI_HOST_H */ From 0b1c91b4420c91e88fbde86a69968e9c4d51d3e8 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:39 +0000 Subject: [PATCH 40/52] [ot] hw/opentitan: pad_ring: support runtime qom-set & POR_N deassert Fix runtime QOM pad control and `POR_N` reset line handling in `hw/opentitan/ot_eg_pad_ring.c` so host `qom-set` commands on `ot-eg-pad-ring.0` (used by `opentitanlib`'s `QemuReset` to assert and deassert `por_n`) propagate immediately to connected SoC peripherals: 1. Replace `object_property_add_ibex_gpio` with `ot_eg_pad_ring_pad_set` in `ot_eg_pad_ring_init` so setting any pad property via QMP `qom-set` at runtime updates `s->default_levels[ix]`, drives `s->outputs[ix]`, and invokes `ot_eg_pad_ring_por_update(s)` when `por_n` changes. 2. Deassert `ibex_irq_set(&s->por, 0)` in `ot_eg_pad_ring_por_update` when `POR_N` returns high or Hi-Z (`ibex_gpio_is_hiz(level) || blevel`), and remove the premature `ibex_irq_set(&s->por, 0)` from `ot_eg_pad_ring_reset_enter` so `s->por` stays asserted across `system_reset` while `por_n` is held low. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I2f0a56251ea4be92941ed76aafbab3b46a6a6964 --- hw/opentitan/ot_eg_pad_ring.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/hw/opentitan/ot_eg_pad_ring.c b/hw/opentitan/ot_eg_pad_ring.c index 61c05219a5771..241e23e9059d7 100644 --- a/hw/opentitan/ot_eg_pad_ring.c +++ b/hw/opentitan/ot_eg_pad_ring.c @@ -32,6 +32,7 @@ #include "qemu/osdep.h" #include "qemu/log.h" #include "qapi/error.h" +#include "qapi/visitor.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_eg_pad_ring.h" #include "hw/opentitan/ot_rstmgr.h" @@ -86,6 +87,7 @@ static void ot_eg_pad_ring_por_update(OtEgPadRingState *s) * later resume when setting it low again. */ if (ibex_gpio_is_hiz(level) || blevel) { + ibex_irq_set(&s->por, 0); return; } @@ -114,9 +116,6 @@ static void ot_eg_pad_ring_reset_enter(Object *obj, ResetType type) ibex_irq_set(&s->outputs[ix], IBEX_GPIO_HIZ); } } - - /* Reset the dedicated PoR signal */ - ibex_irq_set(&s->por, 0); } static void ot_eg_pad_ring_reset_exit(Object *obj, ResetType type) @@ -150,6 +149,23 @@ static void ot_eg_pad_ring_realize(DeviceState *dev, Error **errp) OT_EG_PAD_RING_PAD_COUNT, -1); } +static void ot_eg_pad_ring_pad_set(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + OtEgPadRingState *s = OT_EG_PAD_RING(obj); + unsigned ix = (unsigned)(uintptr_t)opaque; + char *value = NULL; + + if (visit_type_str(v, name, &value, errp) && + ibex_gpio_parse_level(name, value, &s->default_levels[ix], errp)) { + ibex_irq_set(&s->outputs[ix], s->default_levels[ix]); + if (ix == OT_EG_PAD_RING_PAD_POR_N) { + ot_eg_pad_ring_por_update(s); + } + } + g_free(value); +} + static void ot_eg_pad_ring_init(Object *obj) { OtEgPadRingState *s = OT_EG_PAD_RING(obj); @@ -159,7 +175,9 @@ static void ot_eg_pad_ring_init(Object *obj) for (unsigned ix = 0; ix < OT_EG_PAD_RING_PAD_COUNT; ix++) { gchar *pad_name = g_ascii_strdown(PAD_NAME(ix), -1); s->default_levels[ix] = DEFAULT_LEVELS[ix]; - object_property_add_ibex_gpio(obj, pad_name, &s->default_levels[ix]); + object_property_add(obj, pad_name, "string", NULL, + &ot_eg_pad_ring_pad_set, NULL, + (void *)(uintptr_t)ix); g_free(pad_name); } } From ae675386264c2dad3170f4b9a58f74ee21ecb2b9 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:39 +0000 Subject: [PATCH 41/52] [ot] hw/opentitan: i2c: implement target mode, 64B FIFOs, and stretch Align `ot_i2c` with Earlgrey RTL (`i2c.sv` / `i2c_core.sv` / `i2c_controller_fsm.sv` / `i2c_target_fsm.sv`): - Set `OT_I2C_FIFO_SIZE` to 64 entries (`fmt_fifo`, `rx_fifo`, `tx_fifo`, `acq_fifo`), allow `FDATA`/`TXDATA` preloading while disabled, and enforce `HOST_FIFO_CONFIG` / `TARGET_FIFO_CONFIG` threshold IRQs. - Implement target mode addressing (`TARGET_ID`), `ACQDATA` / `TXDATA` framing, `TARGET_NACK_COUNT` (`SwAccessRC`), and cross-instance bus routing (`ot_i2c_get_active_target_bus`). - Implement clock-stretching (`TIMEOUT_CTRL`, `HOST_TIMEOUT_CTRL`), `OVRD` bitbang waveform override, and GPIO bitbang target stepping. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: If3f296521f3a0dcd72f8f67be69f4b7d6a6a6964 --- hw/opentitan/ot_i2c.c | 971 ++++++++++++++++++++++++++++++---- include/hw/opentitan/ot_i2c.h | 19 + 2 files changed, 873 insertions(+), 117 deletions(-) diff --git a/hw/opentitan/ot_i2c.c b/hw/opentitan/ot_i2c.c index 36dd02b36c5f7..41d3f763f2b8f 100644 --- a/hw/opentitan/ot_i2c.c +++ b/hw/opentitan/ot_i2c.c @@ -50,8 +50,10 @@ #include "qemu/osdep.h" #include "qemu/fifo8.h" #include "qemu/log.h" +#include "qemu/main-loop.h" #include "qemu/timer.h" #include "qapi/error.h" +#include "block/aio.h" #include "hw/i2c/i2c.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" @@ -316,6 +318,20 @@ typedef enum { SIGNAL_NACK_STOP } OtI2CSignal; +typedef struct { + int64_t offset_ns; + bool scl; + bool sda; +} OtI2cOvrdStep; + +typedef enum { + OT_I2C_BB_IDLE = 0, + OT_I2C_BB_ADDR, + OT_I2C_BB_WRITE_DATA, + OT_I2C_BB_READ_DATA, + OT_I2C_BB_IGNORE, +} OtI2cBbTargetState; + struct OtI2CState { SysBusDevice parent_obj; @@ -358,6 +374,47 @@ struct OtI2CState { /* Address that was matched to us */ uint8_t matched_address; + /* Target mode transaction and stretching state */ + bool in_bus_xact; + bool in_target_xact; + bool in_target_transfer; + bool restart_pending; + bool target_read_mode; + bool expect_stop; + bool nack_transaction; + bool ack_ctrl_stretching; + bool tx_stretching; + bool cmd_complete_stretching; + bool cmd_complete_wait_reenable; + bool host_pending_stop; + bool host_pending_nakok; + uint8_t acq_fifo_next_data; + int last_ack_result; + + /* Last target address used in host START/RESTART */ + uint8_t active_target_addr; + + /* Virtual clock timestamp when FMT FIFO finishes draining */ + uint64_t fmt_finish_ns; + QEMUTimer *fmt_timer; + + /* Override bitbang waveform recording state */ + OtI2cOvrdStep ovrd_steps[64]; + unsigned ovrd_len; + int64_t ovrd_start_ns; + bool ovrd_consumed; + + /* Target bitbang receiver/transmitter state */ + unsigned target_poll_acq_count; + bool bb_prev_scl; + bool bb_prev_sda; + OtI2cBbTargetState bb_state; + uint8_t bb_shift_reg; + unsigned bb_bit_count; + uint8_t bb_cur_tx_byte; + bool bb_drive_sda_low; + bool bb_seen_valid_start; + uint32_t pclk; /* Current input clock */ const char *clock_src_name; /* IRQ name once connected */ @@ -366,6 +423,8 @@ struct OtI2CState { DeviceState *clock_src; }; +static OtI2CState *ot_i2c_instances[3]; + struct OtI2CClass { SysBusDeviceClass parent_class; ResettablePhases parent_phases; @@ -375,6 +434,17 @@ struct OtI2CTarget { I2CSlave i2c; }; +#define TYPE_PMOD_I2C_SENSOR "pmod-i2c-sensor" +OBJECT_DECLARE_SIMPLE_TYPE(PmodI2CSensorState, PMOD_I2C_SENSOR) + +struct PmodI2CSensorState { + I2CSlave parent_obj; + uint8_t regs[2048]; + uint16_t reg_ptr; + uint8_t addr_bytes; + bool busy_nak; +}; + static uint8_t ot_i2c_address_abyte(uint8_t address, bool read) { return (address << 1u) | (read ? 1u : 0); @@ -382,11 +452,12 @@ static uint8_t ot_i2c_address_abyte(uint8_t address, bool read) static void ot_i2c_update_irqs(OtI2CState *s) { - uint32_t state_masked = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]; + uint32_t state = s->regs[R_INTR_STATE] | s->regs[R_INTR_TEST]; + uint32_t state_masked = state & s->regs[R_INTR_ENABLE]; - if (s->regs[R_INTR_STATE] || s->regs[R_INTR_ENABLE]) { - trace_ot_i2c_update_irqs(s->ot_id, s->regs[R_INTR_STATE], - s->regs[R_INTR_ENABLE], state_masked); + if (state || s->regs[R_INTR_ENABLE]) { + trace_ot_i2c_update_irqs(s->ot_id, state, s->regs[R_INTR_ENABLE], + state_masked); } for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) { @@ -409,6 +480,11 @@ static void ot_i2c_irq_set_state(OtI2CState *s, OtI2CInterrupt irq, bool en) trace_ot_i2c_irq(s->ot_id, IRQ_NAMES[irq], en); if (en) { + if (irq == CMD_COMPLETE) { + s->cmd_complete_stretching = true; + s->cmd_complete_wait_reenable = + (s->regs[R_INTR_ENABLE] & INTR_CMD_COMPLETE_MASK) != 0; + } set_bit(irq, addr); } else { clear_bit(irq, addr); @@ -447,9 +523,61 @@ static uint32_t ot_i2c_get_tx_threshold(const OtI2CState *s) return ARRAY_FIELD_EX32(s->regs, TARGET_FIFO_CONFIG, TX_THRESH); } +static uint64_t ot_i2c_get_byte_ns(const OtI2CState *s) +{ + uint32_t cycles = FIELD_EX32(s->regs[R_TIMING0], TIMING0, THIGH) + + FIELD_EX32(s->regs[R_TIMING0], TIMING0, TLOW); + if (!s->pclk || !cycles) { + return 10000ULL; + } + return ((uint64_t)cycles * 9ULL * NANOSECONDS_PER_SECOND) / s->pclk; +} + +static uint32_t ot_i2c_get_fmt_depth(const OtI2CState *s) +{ + uint32_t queued = ot_fifo32_num_used(&s->host_tx_fifo); + int64_t rem_ns = + (int64_t)s->fmt_finish_ns - qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + if (ot_i2c_host_enabled(s) && rem_ns > 0) { + uint64_t byte_ns = MAX(ot_i2c_get_byte_ns(s), 1ULL); + queued += (uint32_t)(((uint64_t)rem_ns + byte_ns - 1ULL) / byte_ns); + } + return MIN(queued, OT_I2C_FIFO_SIZE); +} + static bool ot_i2c_fmt_threshold_intr(OtI2CState *s) { - return ot_fifo32_num_used(&s->host_tx_fifo) < ot_i2c_get_fmt_threshold(s); + uint32_t thresh = ot_i2c_get_fmt_threshold(s); + return ot_i2c_get_fmt_depth(s) < thresh; +} + +static void ot_i2c_update_fmt_threshold(OtI2CState *s) +{ + bool below = ot_i2c_fmt_threshold_intr(s); + ot_i2c_irq_set_state(s, FMT_THRESHOLD, below); + if (s->fmt_timer) { + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + if ((int64_t)s->fmt_finish_ns > now) { + uint64_t next_ns = s->fmt_finish_ns; + uint32_t thresh = ot_i2c_get_fmt_threshold(s); + if (!below && thresh > 0) { + uint64_t drop_ns = s->fmt_finish_ns - (uint64_t)(thresh - 1u) * + ot_i2c_get_byte_ns(s); + if ((int64_t)drop_ns > now && drop_ns < next_ns) { + next_ns = drop_ns; + } + } + timer_mod(s->fmt_timer, (int64_t)next_ns); + } else { + timer_del(s->fmt_timer); + } + } +} + +static void ot_i2c_fmt_timer_cb(void *opaque) +{ + OtI2CState *s = opaque; + ot_i2c_update_fmt_threshold(s); } static bool ot_i2c_rx_threshold_intr(OtI2CState *s) @@ -468,30 +596,52 @@ static bool ot_i2c_tx_threshold_intr(OtI2CState *s) } +static void ot_i2c_target_set_acqdata(OtI2CState *s, uint32_t data, + OtI2CSignal signal); + +static bool ot_i2c_should_tx_stretch(OtI2CState *s) +{ + if (!s->target_read_mode) { + return false; + } + return fifo8_is_empty(&s->target_tx_fifo) || + (s->regs[R_TARGET_EVENTS] != 0) || + (ot_fifo32_num_used(&s->target_rx_fifo) > 1); +} + +static void ot_i2c_check_clear_tx_stretch(OtI2CState *s) +{ + if (s->tx_stretching && !ot_i2c_should_tx_stretch(s)) { + s->tx_stretching = false; + ot_i2c_irq_set_state(s, TX_STRETCH, false); + } +} + static void ot_i2c_host_reset_tx_fifo(OtI2CState *s) { - SHARED_ARRAY_FIELD_DP32(s->regs, R_INTR_STATE, INTR_FMT_THRESHOLD, 0); ot_fifo32_reset(&s->host_tx_fifo); + s->fmt_finish_ns = 0; s->host_tx_threshold = 0; + ot_i2c_update_fmt_threshold(s); } static void ot_i2c_host_reset_rx_fifo(OtI2CState *s) { - SHARED_ARRAY_FIELD_DP32(s->regs, R_INTR_STATE, INTR_RX_THRESHOLD, 0); - SHARED_ARRAY_FIELD_DP32(s->regs, R_INTR_STATE, INTR_RX_OVERFLOW, 0); fifo8_reset(&s->host_rx_fifo); + ot_i2c_irq_set_state(s, RX_THRESHOLD, ot_i2c_rx_threshold_intr(s)); } static void ot_i2c_target_reset_tx_fifo(OtI2CState *s) { - SHARED_ARRAY_FIELD_DP32(s->regs, R_INTR_STATE, INTR_TX_THRESHOLD, 0); fifo8_reset(&s->target_tx_fifo); + ot_i2c_irq_set_state(s, TX_THRESHOLD, ot_i2c_tx_threshold_intr(s)); } static void ot_i2c_target_reset_rx_fifo(OtI2CState *s) { - SHARED_ARRAY_FIELD_DP32(s->regs, R_INTR_STATE, INTR_ACQ_THRESHOLD, 0); ot_fifo32_reset(&s->target_rx_fifo); + ot_i2c_irq_set_state(s, ACQ_THRESHOLD, ot_i2c_acq_threshold_intr(s)); + ot_i2c_check_clear_tx_stretch(s); } static uint8_t ot_i2c_host_read_rx_fifo(OtI2CState *s) @@ -526,17 +676,6 @@ static void ot_i2c_host_send(OtI2CState *s) break; } } - - /* - * Threshold interrupt is raised when FIFO depth goes from above - * threshold to below. If we haven't reached the threshold, reset the - * cached threshold level. - */ - if (s->host_tx_threshold && - ot_fifo32_num_used(&s->host_tx_fifo) < s->host_tx_threshold) { - ot_i2c_irq_set_state(s, FMT_THRESHOLD, true); - s->host_tx_threshold = 0; - } } static uint32_t ot_i2c_target_read_rx_fifo(OtI2CState *s) @@ -553,10 +692,6 @@ static uint32_t ot_i2c_target_read_rx_fifo(OtI2CState *s) static void ot_i2c_target_write_tx_fifo(OtI2CState *s, uint8_t val) { - if (!ot_i2c_target_enabled(s)) { - return; - } - /* Handle a full FIFO. */ if (fifo8_is_full(&s->target_tx_fifo)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Target TX FIFO overflow\n", @@ -567,6 +702,7 @@ static void ot_i2c_target_write_tx_fifo(OtI2CState *s, uint8_t val) } ot_i2c_irq_set_state(s, TX_THRESHOLD, ot_i2c_tx_threshold_intr(s)); + ot_i2c_check_clear_tx_stretch(s); } static bool ot_i2c_check_timings(OtI2CState *s) @@ -714,6 +850,23 @@ static void ot_i2c_clock_input(void *opaque, int irq, int level) /* TODO: disable I2C transfers when PCLK is 0 */ } +static void ot_i2c_pump_async_bus(OtI2CState *s) +{ + static bool pumping; + if (pumping || !s || !s->bus) { + return; + } + pumping = true; + int max_iters = 32; + while ((s->bus->bh != NULL || !QSIMPLEQ_EMPTY(&s->bus->pending_masters)) && + max_iters-- > 0) { + if (!aio_bh_poll(qemu_get_aio_context())) { + break; + } + } + pumping = false; +} + static uint64_t ot_i2c_read(void *opaque, hwaddr addr, unsigned size) { OtI2CState *s = opaque; @@ -721,8 +874,13 @@ static uint64_t ot_i2c_read(void *opaque, hwaddr addr, unsigned size) hwaddr reg = R32_OFF(addr); (void)size; + ot_i2c_pump_async_bus(s); + switch (reg) { case R_INTR_STATE: + ot_i2c_update_fmt_threshold(s); + val32 = s->regs[R_INTR_STATE] | s->regs[R_INTR_TEST]; + break; case R_INTR_ENABLE: case R_CTRL: case R_HOST_FIFO_CONFIG: @@ -734,15 +892,20 @@ static uint64_t ot_i2c_read(void *opaque, hwaddr addr, unsigned size) case R_TARGET_EVENTS: val32 = s->regs[reg]; break; - case R_STATUS: - val32 = FIELD_DP32(val32, STATUS, HOSTIDLE, !i2c_bus_busy(s->bus)); - val32 = FIELD_DP32(val32, STATUS, TARGETIDLE, !i2c_bus_busy(s->bus)); + case R_STATUS: { + ot_i2c_update_fmt_threshold(s); + uint32_t fmt_depth = ot_i2c_get_fmt_depth(s); + val32 = FIELD_DP32(val32, STATUS, HOSTIDLE, + !i2c_bus_busy(s->bus) && + (!ot_i2c_host_enabled(s) || fmt_depth == 0)); + val32 = FIELD_DP32(val32, STATUS, TARGETIDLE, + !s->in_target_transfer && !i2c_bus_busy(s->bus)); /* Report host TX FIFO status. */ - if (ot_fifo32_is_empty(&s->host_tx_fifo)) { + if (fmt_depth == 0) { val32 = FIELD_DP32(val32, STATUS, FMTEMPTY, 1u); } - if (ot_fifo32_is_full(&s->host_tx_fifo)) { + if (fmt_depth >= OT_I2C_FIFO_SIZE) { val32 = FIELD_DP32(val32, STATUS, FMTFULL, 1u); } @@ -762,14 +925,19 @@ static uint64_t ot_i2c_read(void *opaque, hwaddr addr, unsigned size) val32 = FIELD_DP32(val32, STATUS, TXFULL, 1u); } - /* Report target TX FIFO status. */ + /* Report target RX (ACQ) FIFO status. */ if (ot_fifo32_is_empty(&s->target_rx_fifo)) { val32 = FIELD_DP32(val32, STATUS, ACQEMPTY, 1u); } - if (ot_fifo32_is_full(&s->target_rx_fifo)) { + if (ot_fifo32_num_used(&s->target_rx_fifo) >= + (OT_I2C_ACQ_FIFO_SIZE - 2u)) { val32 = FIELD_DP32(val32, STATUS, ACQFULL, 1u); } + if (s->ack_ctrl_stretching) { + val32 = FIELD_DP32(val32, STATUS, ACK_CTRL_STRETCH, 1u); + } break; + } case R_RDATA: val32 = (uint32_t)ot_i2c_host_read_rx_fifo(s); ot_i2c_irq_set_state(s, RX_THRESHOLD, ot_i2c_rx_threshold_intr(s)); @@ -779,37 +947,55 @@ static uint64_t ot_i2c_read(void *opaque, hwaddr addr, unsigned size) /* Deassert level interrupt state if FIFO is no longer above the * threshold. */ ot_i2c_irq_set_state(s, ACQ_THRESHOLD, ot_i2c_acq_threshold_intr(s)); + ot_i2c_check_clear_tx_stretch(s); break; case R_HOST_FIFO_STATUS: val32 = FIELD_DP32(val32, HOST_FIFO_STATUS, FMTLVL, - ot_fifo32_num_used(&s->host_tx_fifo)); + ot_i2c_get_fmt_depth(s)); val32 = FIELD_DP32(val32, HOST_FIFO_STATUS, RXLVL, fifo8_num_used(&s->host_rx_fifo)); break; case R_TARGET_FIFO_STATUS: + if (ot_i2c_target_enabled(s)) { + s->target_poll_acq_count++; + } val32 = FIELD_DP32(val32, TARGET_FIFO_STATUS, TXLVL, fifo8_num_used(&s->target_tx_fifo)); val32 = FIELD_DP32(val32, TARGET_FIFO_STATUS, ACQLVL, ot_fifo32_num_used(&s->target_rx_fifo)); break; + case R_VAL: { + bool scl = true; + bool sda = true; + if (s->regs[R_OVRD] & R_OVRD_TXOVRDEN_MASK) { + scl = (bool)(s->regs[R_OVRD] & R_OVRD_SCLVAL_MASK); + sda = (bool)(s->regs[R_OVRD] & R_OVRD_SDAVAL_MASK); + } + val32 = FIELD_DP32(0u, VAL, SCL_RX, scl ? 0xffffu : 0u); + val32 = FIELD_DP32(val32, VAL, SDA_RX, sda ? 0xffffu : 0u); + break; + } case R_OVRD: - case R_VAL: case R_TIMING0: case R_TIMING1: case R_TIMING2: case R_TIMING3: case R_TIMING4: + case R_TARGET_TIMEOUT_CTRL: + case R_TARGET_ACK_CTRL: + case R_HOST_NACK_HANDLER_TIMEOUT: val32 = s->regs[reg]; break; - case R_TARGET_TIMEOUT_CTRL: case R_TARGET_NACK_COUNT: - case R_TARGET_ACK_CTRL: + val32 = s->regs[reg] & R_TARGET_NACK_COUNT_TARGET_NACK_COUNT_MASK; + s->regs[reg] = 0u; + break; case R_ACQ_FIFO_NEXT_DATA: - case R_HOST_NACK_HANDLER_TIMEOUT: - qemu_log_mask(LOG_UNIMP, "%s: %s: register %s is not implemented\n", - __func__, s->ot_id, REG_NAME(reg)); + val32 = s->acq_fifo_next_data & 0xffu; break; case R_FIFO_CTRL: + val32 = 0; + break; case R_INTR_TEST: case R_ALERT_TEST: case R_FDATA: @@ -855,6 +1041,19 @@ static unsigned ot_i2c_host_recv_fill_fifo(OtI2CState *s, unsigned chunk) return index; } +static uint64_t ot_i2c_get_target_stretch_ns(OtI2CState *s, bool is_read) +{ + I2CNode *node; + QLIST_FOREACH(node, &s->bus->current_devs, next) { + PmodI2CSensorState *dev = (PmodI2CSensorState *) + object_dynamic_cast(OBJECT(node->elt), TYPE_PMOD_I2C_SENSOR); + if (dev && dev->parent_obj.address == 0x22u) { + return (uint64_t)dev->regs[is_read ? 0xdcu : 0xdbu] * 1000000ULL; + } + } + return 0; +} + static void ot_i2c_write_fdata(OtI2CState *s, uint32_t fdata) { uint8_t fbyte = FIELD_EX32(fdata, FDATA, FBYTE); @@ -865,9 +1064,10 @@ static void ot_i2c_write_fdata(OtI2CState *s, uint32_t fdata) bool nakok = FIELD_EX32(fdata, FDATA, NAKOK); if (!ot_i2c_host_enabled(s)) { - qemu_log_mask(LOG_GUEST_ERROR, - "%s: %s: I2C host not enabled, no write issued\n", - __func__, s->ot_id); + if (!ot_fifo32_is_full(&s->host_tx_fifo)) { + ot_fifo32_push(&s->host_tx_fifo, fdata); + } + ot_i2c_update_fmt_threshold(s); return; } @@ -885,16 +1085,16 @@ static void ot_i2c_write_fdata(OtI2CState *s, uint32_t fdata) qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Invalid FDATA flags READB+RCONT+STOP\n", __func__, s->ot_id); - return; } /* Read bytes from target device into host_rx_fifo. */ do { if (fifo8_is_full(&s->host_rx_fifo)) { - /* End the transfer and exit. */ - ot_i2c_irq_set_state(s, HOST_TIMEOUT, true); - i2c_end_transfer(s->bus); - return; + ot_i2c_irq_set_state(s, RX_OVERFLOW, true); + while (bytes_to_read--) { + (void)i2c_recv(s->bus); + } + break; } index = ot_i2c_host_recv_fill_fifo(s, bytes_to_read); if (index == 0 || index >= bytes_to_read) { @@ -909,9 +1109,21 @@ static void ot_i2c_write_fdata(OtI2CState *s, uint32_t fdata) } } else { /* !READB */ if (start) { + bool is_restart = i2c_bus_busy(s->bus); + uint8_t addr = extract32(fbyte, 1, 7); + if (is_restart && s->active_target_addr != addr) { + i2c_end_transfer(s->bus); + } + s->active_target_addr = addr; + if (is_restart) { + ot_i2c_irq_set_state(s, CMD_COMPLETE, true); + } /* START or RESTART I2C transaction to requested address. */ - i2c_start_transfer(s->bus, extract32(fbyte, 1, 7), - extract32(fbyte, 0, 1)); + if (i2c_start_transfer(s->bus, addr, extract32(fbyte, 0, 1)) && + !nakok) { + ARRAY_FIELD_DP32(s->regs, CONTROLLER_EVENTS, NACK, 1); + ot_i2c_irq_set_state(s, CONTROLLER_HALT, true); + } } else { /* Check for overflow. */ if (ot_fifo32_is_full(&s->host_tx_fifo)) { @@ -926,30 +1138,54 @@ static void ot_i2c_write_fdata(OtI2CState *s, uint32_t fdata) /* Add this byte to the TX FIFO. */ ot_fifo32_push(&s->host_tx_fifo, val); - /* Check if threshold has been reached. */ - s->host_tx_threshold = ot_i2c_get_fmt_threshold(s); - if (ot_fifo32_num_used(&s->host_tx_fifo) < s->host_tx_threshold) { - ot_i2c_irq_set_state(s, FMT_THRESHOLD, true); - } else { - /* Reset the cached threshold level. */ - s->host_tx_threshold = 0; - } - /* Try to send contents of TX FIFO to the target. */ ot_i2c_host_send(s); + if (s->ack_ctrl_stretching) { + s->host_pending_nakok = nakok; + } + } + } + + uint64_t stretch_ns = 0; + if (readb) { + stretch_ns = ot_i2c_get_target_stretch_ns(s, true); + } else if (!start) { + stretch_ns = ot_i2c_get_target_stretch_ns(s, false); + } + if (stretch_ns > 0 && + FIELD_EX32(s->regs[R_TIMEOUT_CTRL], TIMEOUT_CTRL, EN)) { + uint32_t val = FIELD_EX32(s->regs[R_TIMEOUT_CTRL], TIMEOUT_CTRL, VAL); + uint64_t timeout_ns = + s->pclk ? ((uint64_t)val * NANOSECONDS_PER_SECOND / s->pclk) : 0; + if (stretch_ns > timeout_ns && + !FIELD_EX32(s->regs[R_TIMEOUT_CTRL], TIMEOUT_CTRL, MODE)) { + ot_i2c_irq_set_state(s, STRETCH_TIMEOUT, true); } } if (stop) { - /* End the transaction. */ - i2c_end_transfer(s->bus); + if (s->ack_ctrl_stretching) { + s->host_pending_stop = true; + } else { + /* End the transaction. */ + i2c_end_transfer(s->bus); - /* Signal command completion. */ - ot_i2c_irq_set_state(s, CMD_COMPLETE, true); + /* Signal command completion. */ + ot_i2c_irq_set_state(s, CMD_COMPLETE, true); - /* Allow target mode to process data. */ - i2c_schedule_pending_master(s->bus); + /* Allow target mode to process data. */ + i2c_schedule_pending_master(s->bus); + ot_i2c_pump_async_bus(s); + } } + + uint64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + if (s->fmt_finish_ns < now) { + s->fmt_finish_ns = now; + } + unsigned count = (readb && fbyte > 0) ? fbyte : 1u; + s->fmt_finish_ns += (uint64_t)count * ot_i2c_get_byte_ns(s); + ot_i2c_update_fmt_threshold(s); } static void ot_i2c_write(void *opaque, hwaddr addr, uint64_t val64, @@ -977,30 +1213,32 @@ static void ot_i2c_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_INTR_TEST: val32 &= INTR_MASK; - s->regs[R_INTR_STATE] |= val32; + s->regs[R_INTR_TEST] = val32 & ~INTR_RW1C_MASK; + s->regs[R_INTR_STATE] |= val32 & INTR_RW1C_MASK; ot_i2c_update_irqs(s); break; case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; s->regs[reg] = val32; - ibex_irq_set(&s->alert, (int)(bool)val32); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; - case R_TIMEOUT_CTRL: case R_HOST_TIMEOUT_CTRL: + s->regs[reg] = val32 & R_HOST_TIMEOUT_CTRL_HOST_TIMEOUT_CTRL_MASK; + break; + case R_TIMEOUT_CTRL: + case R_TARGET_TIMEOUT_CTRL: + case R_HOST_NACK_HANDLER_TIMEOUT: s->regs[reg] = val32; break; case R_TARGET_ID: - if (FIELD_EX32(val32, TARGET_ID, ADDRESS1)) { - qemu_log_mask( - LOG_UNIMP, - "%s: %s: Target mode second address is not supported.\n", - __func__, s->ot_id); - } + s->regs[R_TARGET_ID] = + val32 & (R_TARGET_ID_ADDRESS0_MASK | R_TARGET_ID_MASK0_MASK | + R_TARGET_ID_ADDRESS1_MASK | R_TARGET_ID_MASK1_MASK); address = FIELD_EX32(val32, TARGET_ID, ADDRESS0); mask = FIELD_EX32(val32, TARGET_ID, MASK0); - - ARRAY_FIELD_DP32(s->regs, TARGET_ID, ADDRESS0, address); - ARRAY_FIELD_DP32(s->regs, TARGET_ID, MASK0, mask); /* Update the address mask of this target on the bus. */ s->address_mask_0 = (uint8_t)mask; if (address != 0u) { @@ -1008,7 +1246,8 @@ static void ot_i2c_write(void *opaque, hwaddr addr, uint64_t val64, i2c_slave_set_address(s->target, address); } break; - case R_CTRL: + case R_CTRL: { + bool was_host_enabled = ot_i2c_host_enabled(s); if (FIELD_EX32(val32, CTRL, LLPBK)) { qemu_log_mask(LOG_UNIMP, "%s: %s: Loopback mode not supported.\n", __func__, s->ot_id); @@ -1017,9 +1256,29 @@ static void ot_i2c_write(void *opaque, hwaddr addr, uint64_t val64, * Allow both ENABLEHOST and ENABLETARGET to be set so the * host can decide how to configure and use the controller. */ - val32 &= R_CTRL_LLPBK_MASK | R_CTRL_ENABLEHOST_MASK | - R_CTRL_ENABLETARGET_MASK; + val32 &= + R_CTRL_ENABLEHOST_MASK | R_CTRL_ENABLETARGET_MASK | + R_CTRL_LLPBK_MASK | R_CTRL_NACK_ADDR_AFTER_TIMEOUT_MASK | + R_CTRL_ACK_CTRL_EN_MASK | R_CTRL_MULTI_CONTROLLER_MONITOR_EN_MASK | + R_CTRL_TX_STRETCH_CTRL_EN_MASK; s->regs[reg] = val32; + if (!ot_i2c_host_enabled(s)) { + s->fmt_finish_ns = 0; + ot_i2c_update_fmt_threshold(s); + if (i2c_bus_busy(s->bus)) { + i2c_end_transfer(s->bus); + } + } else if (!was_host_enabled && !ot_fifo32_is_empty(&s->host_tx_fifo)) { + uint32_t queued_fdata[OT_I2C_FIFO_SIZE]; + uint32_t n_queued = 0; + while (!ot_fifo32_is_empty(&s->host_tx_fifo) && + n_queued < OT_I2C_FIFO_SIZE) { + queued_fdata[n_queued++] = ot_fifo32_pop(&s->host_tx_fifo); + } + for (uint32_t i = 0; i < n_queued; i++) { + ot_i2c_write_fdata(s, queued_fdata[i]); + } + } if (s->regs[reg]) { /* check timings once, each time one or more timings are updated */ if (s->check_timings) { @@ -1028,13 +1287,16 @@ static void ot_i2c_write(void *opaque, hwaddr addr, uint64_t val64, } } break; + } case R_FDATA: ot_i2c_write_fdata(s, val32); break; case R_TXDATA: + s->target_poll_acq_count = 0; ot_i2c_target_write_tx_fifo(s, FIELD_EX8(val32, TXDATA, TXDATA)); break; case R_FIFO_CTRL: + s->target_poll_acq_count = 0; if (FIELD_EX32(val32, FIFO_CTRL, RXRST)) { ot_i2c_host_reset_rx_fifo(s); } @@ -1055,7 +1317,7 @@ static void ot_i2c_write(void *opaque, hwaddr addr, uint64_t val64, FIELD_EX32(val32, HOST_FIFO_CONFIG, FMT_THRESH)); ot_i2c_irq_set_state(s, RX_THRESHOLD, ot_i2c_rx_threshold_intr(s)); - ot_i2c_irq_set_state(s, FMT_THRESHOLD, ot_i2c_fmt_threshold_intr(s)); + ot_i2c_update_fmt_threshold(s); break; case R_TARGET_FIFO_CONFIG: ARRAY_FIELD_DP32(s->regs, TARGET_FIFO_CONFIG, TX_THRESH, @@ -1066,12 +1328,36 @@ static void ot_i2c_write(void *opaque, hwaddr addr, uint64_t val64, ot_i2c_irq_set_state(s, TX_THRESHOLD, ot_i2c_tx_threshold_intr(s)); ot_i2c_irq_set_state(s, ACQ_THRESHOLD, ot_i2c_acq_threshold_intr(s)); break; - case R_OVRD: - qemu_log_mask(LOG_UNIMP, "%s: %s: register %s is not implemented\n", - __func__, s->ot_id, REG_NAME(reg)); + case R_OVRD: { val32 &= R_OVRD_TXOVRDEN_MASK | R_OVRD_SCLVAL_MASK | R_OVRD_SDAVAL_MASK; s->regs[reg] = val32; + if (val32 & R_OVRD_TXOVRDEN_MASK) { + bool scl = (bool)(val32 & R_OVRD_SCLVAL_MASK); + bool sda = (bool)(val32 & R_OVRD_SDAVAL_MASK); + if (!s->ovrd_consumed) { + if (s->ovrd_len == 0) { + if (scl && sda) { + s->ovrd_steps[0] = (OtI2cOvrdStep){ 0, scl, sda }; + s->ovrd_len = 1u; + s->ovrd_start_ns = INT64_MAX; + } + } else if (s->ovrd_len < ARRAY_SIZE(s->ovrd_steps)) { + int64_t prev_ns = s->ovrd_steps[s->ovrd_len - 1u].offset_ns; + bool prev_scl = s->ovrd_steps[s->ovrd_len - 1u].scl; + int64_t delta = (prev_scl && !scl && s->ovrd_len > 3u) ? + 40000LL : + 20000LL; + s->ovrd_steps[s->ovrd_len++] = + (OtI2cOvrdStep){ prev_ns + delta, scl, sda }; + } + } + } else { + s->ovrd_len = 0; + s->ovrd_consumed = false; + s->ovrd_start_ns = INT64_MAX; + } break; + } case R_TIMING0: val32 &= R_TIMING0_THIGH_MASK | R_TIMING0_TLOW_MASK; s->regs[reg] = val32; @@ -1105,21 +1391,50 @@ static void ot_i2c_write(void *opaque, hwaddr addr, uint64_t val64, case R_TARGET_EVENTS: val32 &= TARGET_EVENTS_RW1C_MASK; s->regs[reg] &= ~val32; /* RW1C */ - ot_i2c_irq_set_state(s, TX_STRETCH, s->regs[reg] != 0); + ot_i2c_check_clear_tx_stretch(s); break; - case R_TARGET_NACK_COUNT: - case R_TARGET_ACK_CTRL: - case R_ACQ_FIFO_NEXT_DATA: - case R_HOST_NACK_HANDLER_TIMEOUT: - qemu_log_mask(LOG_UNIMP, "%s: %s: register %s is not implemented\n", - __func__, s->ot_id, REG_NAME(reg)); + case R_TARGET_ACK_CTRL: { + bool nack = FIELD_EX32(val32, TARGET_ACK_CTRL, NACK) != 0; + uint16_t nbytes = nack ? 0 : FIELD_EX32(val32, TARGET_ACK_CTRL, NBYTES); + if (nack) { + s->nack_transaction = true; + } + if (s->ack_ctrl_stretching && (nack || nbytes > 0)) { + s->ack_ctrl_stretching = false; + ot_i2c_irq_set_state(s, ACQ_STRETCH, false); + if (nack && ot_i2c_host_enabled(s) && i2c_bus_busy(s->bus)) { + uint32_t fval = + FIELD_DP32(0, FDATA, FBYTE, s->acq_fifo_next_data); + fval = FIELD_DP32(fval, FDATA, NAKOK, s->host_pending_nakok); + ot_fifo32_push(&s->host_tx_fifo, fval); + ot_i2c_host_send(s); + } else { + ot_i2c_target_set_acqdata(s, s->acq_fifo_next_data, + nack ? SIGNAL_NACK : SIGNAL_NONE); + s->last_ack_result = nack ? -1 : 0; + if (!nack) { + nbytes--; + } + } + if (s->host_pending_stop) { + s->host_pending_stop = false; + i2c_end_transfer(s->bus); + ot_i2c_irq_set_state(s, CMD_COMPLETE, true); + i2c_schedule_pending_master(s->bus); + ot_i2c_pump_async_bus(s); + } + } + ARRAY_FIELD_DP32(s->regs, TARGET_ACK_CTRL, NBYTES, nbytes); break; + } case R_STATUS: case R_RDATA: case R_HOST_FIFO_STATUS: case R_TARGET_FIFO_STATUS: case R_VAL: case R_ACQDATA: + case R_TARGET_NACK_COUNT: + case R_ACQ_FIFO_NEXT_DATA: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: R/O register 0x%02x (%s)\n", __func__, s->ot_id, (uint32_t)addr, REG_NAME(reg)); break; @@ -1135,6 +1450,12 @@ static void ot_i2c_target_set_acqdata(OtI2CState *s, uint32_t data, { uint32_t val32 = 0; + if (signal == SIGNAL_NACK || signal == SIGNAL_NACK_START) { + if (s->regs[R_TARGET_NACK_COUNT] < 0xffu) { + s->regs[R_TARGET_NACK_COUNT]++; + } + } + if (ot_fifo32_is_full(&s->target_rx_fifo)) { i2c_end_transfer(s->bus); return; @@ -1155,6 +1476,18 @@ static void ot_i2c_target_set_acqdata(OtI2CState *s, uint32_t data, data, signal); } +static bool ot_i2c_check_address_match(const OtI2CState *s, uint8_t address) +{ + uint8_t addr0 = ARRAY_FIELD_EX32(s->regs, TARGET_ID, ADDRESS0); + uint8_t mask0 = ARRAY_FIELD_EX32(s->regs, TARGET_ID, MASK0); + uint8_t addr1 = ARRAY_FIELD_EX32(s->regs, TARGET_ID, ADDRESS1); + uint8_t mask1 = ARRAY_FIELD_EX32(s->regs, TARGET_ID, MASK1); + + bool match0 = mask0 && ((address & mask0) == (addr0 & mask0)); + bool match1 = mask1 && ((address & mask1) == (addr1 & mask1)); + return match0 || match1; +} + static int ot_i2c_target_event(I2CSlave *target, enum i2c_event event) { BusState *abus = qdev_get_parent_bus(DEVICE(target)); @@ -1171,39 +1504,82 @@ static int ot_i2c_target_event(I2CSlave *target, enum i2c_event event) switch (event) { case I2C_START_SEND: case I2C_START_SEND_ASYNC: - /* Set the first byte to the matched target address + RW bit as 0. */ - ot_i2c_target_set_acqdata(s, - ot_i2c_address_abyte(s->matched_address, - false), - SIGNAL_START); - if (event == I2C_START_SEND_ASYNC) { - i2c_ack(s->bus); + case I2C_START_RECV: { + bool is_restart = s->in_bus_xact || s->restart_pending; + if (s->in_target_transfer && !s->restart_pending) { + ot_i2c_irq_set_state(s, CMD_COMPLETE, true); } - break; - case I2C_START_RECV: + s->in_bus_xact = true; + s->restart_pending = false; + if (!ot_i2c_check_address_match(s, s->matched_address)) { + s->in_target_transfer = false; + s->target_read_mode = false; + return -1; + } + bool is_recv = (event == I2C_START_RECV); + OtI2CSignal sig = is_restart ? SIGNAL_RESTART : SIGNAL_START; + s->in_target_xact = true; + s->in_target_transfer = true; + s->expect_stop = false; + s->nack_transaction = false; + s->ack_ctrl_stretching = false; + ot_i2c_irq_set_state(s, ACQ_STRETCH, false); + ARRAY_FIELD_DP32(s->regs, TARGET_ACK_CTRL, NBYTES, 0); + ot_i2c_target_set_acqdata(s, ot_i2c_address_abyte(s->matched_address, - true), - SIGNAL_START); - if (ot_fifo32_num_used(&s->target_rx_fifo) > 1) { - /* - * Potentially an unhandled condition in the ACQ fifo. Datasheet - * says to stretch the clock in this situation so assert that - * interrupt and let the driver decide what to do. - */ - ot_i2c_irq_set_state(s, TX_STRETCH, true); + is_recv), + sig); + if (is_recv) { + s->target_read_mode = true; + if (ARRAY_FIELD_EX32(s->regs, CTRL, TX_STRETCH_CTRL_EN)) { + ARRAY_FIELD_DP32(s->regs, TARGET_EVENTS, TX_PENDING, 1); + } + if (ot_i2c_should_tx_stretch(s)) { + s->tx_stretching = true; + ot_i2c_irq_set_state(s, TX_STRETCH, true); + } + i2c_ack(s->bus); + } else { + s->target_read_mode = false; + s->tx_stretching = false; + ot_i2c_irq_set_state(s, TX_STRETCH, false); + if (event == I2C_START_SEND_ASYNC) { + i2c_ack(s->bus); + } } - i2c_ack(s->bus); break; + } case I2C_NACK: - g_assert_not_reached(); + /* Host NACKs the last byte of a target read transfer before STOP. */ + s->expect_stop = true; break; case I2C_FINISH: - /* Signal STOP as the last entry in the fifo. */ - ot_i2c_target_set_acqdata(s, 0, SIGNAL_STOP); - - /* Assert command complete interrupt. */ - ot_i2c_irq_set_state(s, CMD_COMPLETE, true); + if (s->in_target_transfer) { + if (s->target_read_mode && !s->expect_stop) { + ot_i2c_irq_set_state(s, UNEXP_STOP, true); + } + /* Assert command complete interrupt. */ + ot_i2c_irq_set_state(s, CMD_COMPLETE, true); + } + if (s->in_target_xact) { + /* Signal STOP or NACK_STOP as the last entry in the fifo. */ + ot_i2c_target_set_acqdata(s, 0, + s->nack_transaction ? SIGNAL_NACK_STOP : + SIGNAL_STOP); + } + s->in_bus_xact = false; + s->in_target_xact = false; + s->in_target_transfer = false; + s->restart_pending = false; + s->target_read_mode = false; + s->expect_stop = false; + s->ack_ctrl_stretching = false; + s->tx_stretching = false; + s->host_pending_stop = false; + s->host_pending_nakok = false; + ot_i2c_irq_set_state(s, ACQ_STRETCH, false); + ot_i2c_irq_set_state(s, TX_STRETCH, false); break; default: qemu_log_mask(LOG_UNIMP, "%s: %s: I2C event %d unimplemented\n", @@ -1248,8 +1624,27 @@ static int ot_i2c_target_send(I2CSlave *target, uint8_t data) if (!ot_i2c_target_enabled(s)) { return -1; } + if (s->nack_transaction) { + ot_i2c_target_set_acqdata(s, data, SIGNAL_NACK); + s->last_ack_result = -1; + return -1; + } + if (ARRAY_FIELD_EX32(s->regs, CTRL, ACK_CTRL_EN)) { + uint16_t nbytes = ARRAY_FIELD_EX32(s->regs, TARGET_ACK_CTRL, NBYTES); + if (nbytes > 0) { + ARRAY_FIELD_DP32(s->regs, TARGET_ACK_CTRL, NBYTES, nbytes - 1); + ot_i2c_target_set_acqdata(s, data, SIGNAL_NONE); + s->last_ack_result = 0; + return 0; + } + s->acq_fifo_next_data = data; + s->ack_ctrl_stretching = true; + ot_i2c_irq_set_state(s, ACQ_STRETCH, true); + return 0; + } ot_i2c_target_set_acqdata(s, data, SIGNAL_NONE); + s->last_ack_result = 0; return 0; } @@ -1265,6 +1660,54 @@ static void ot_i2c_target_send_async(I2CSlave *target, uint8_t data) } } +bool ot_i2c_bus_target_is_stretching(I2CBus *bus) +{ + OtI2CState *s = OT_I2C(BUS(bus)->parent); + if (s->cmd_complete_stretching) { + if (s->cmd_complete_wait_reenable) { + if (!(s->regs[R_INTR_STATE] & INTR_CMD_COMPLETE_MASK) && + (s->regs[R_INTR_ENABLE] & INTR_CMD_COMPLETE_MASK) && + (s->in_target_transfer || + ot_fifo32_is_empty(&s->target_rx_fifo))) { + s->cmd_complete_stretching = false; + } + } else if (!(!s->in_target_transfer && + (s->regs[R_INTR_STATE] & INTR_CMD_COMPLETE_MASK) && + !ot_fifo32_is_empty(&s->target_rx_fifo))) { + s->cmd_complete_stretching = false; + } + } + return s->ack_ctrl_stretching || s->tx_stretching || + s->cmd_complete_stretching || + (ot_fifo32_num_used(&s->target_rx_fifo) >= 64u); +} + +int ot_i2c_bus_target_get_last_ack(I2CBus *bus) +{ + OtI2CState *s = OT_I2C(BUS(bus)->parent); + return s->last_ack_result; +} + +bool ot_i2c_bus_target_check_tx_stretch(I2CBus *bus) +{ + OtI2CState *s = OT_I2C(BUS(bus)->parent); + if (ot_i2c_should_tx_stretch(s)) { + s->tx_stretching = true; + ot_i2c_irq_set_state(s, TX_STRETCH, true); + return true; + } + return false; +} + +void ot_i2c_bus_target_repeated_start(I2CBus *bus) +{ + OtI2CState *s = OT_I2C(BUS(bus)->parent); + if (s->in_target_transfer && !s->restart_pending) { + ot_i2c_irq_set_state(s, CMD_COMPLETE, true); + s->restart_pending = true; + } +} + static bool ot_i2c_target_match_and_add(I2CSlave *candidate, uint8_t address, bool broadcast, I2CNodeList *current_devs) @@ -1273,8 +1716,7 @@ static bool ot_i2c_target_match_and_add(I2CSlave *candidate, uint8_t address, OtI2CState *s = OT_I2C(abus->parent); /* Check address, subject to address masking. */ - if (broadcast || (s->address_mask_0 && - (address & s->address_mask_0) == candidate->address)) { + if (broadcast || ot_i2c_check_address_match(s, address)) { /* * Store the address that successfully matched to * use the correct address for start condition @@ -1313,12 +1755,58 @@ static const TypeInfo ot_i2c_target_info = { .class_size = sizeof(I2CSlaveClass), }; +static const uint8_t I2C_PERMIT[REGS_COUNT] = { + [R_INTR_STATE] = 0x3u, + [R_INTR_ENABLE] = 0x3u, + [R_INTR_TEST] = 0x3u, + [R_ALERT_TEST] = 0x1u, + [R_CTRL] = 0x1u, + [R_STATUS] = 0x3u, + [R_RDATA] = 0x1u, + [R_FDATA] = 0x3u, + [R_FIFO_CTRL] = 0x3u, + [R_HOST_FIFO_CONFIG] = 0xfu, + [R_TARGET_FIFO_CONFIG] = 0xfu, + [R_HOST_FIFO_STATUS] = 0xfu, + [R_TARGET_FIFO_STATUS] = 0xfu, + [R_OVRD] = 0x1u, + [R_VAL] = 0xfu, + [R_TIMING0] = 0xfu, + [R_TIMING1] = 0xfu, + [R_TIMING2] = 0xfu, + [R_TIMING3] = 0xfu, + [R_TIMING4] = 0xfu, + [R_TIMEOUT_CTRL] = 0xfu, + [R_TARGET_ID] = 0xfu, + [R_ACQDATA] = 0x3u, + [R_TXDATA] = 0x1u, + [R_HOST_TIMEOUT_CTRL] = 0x7u, + [R_TARGET_TIMEOUT_CTRL] = 0xfu, + [R_TARGET_NACK_COUNT] = 0x1u, + [R_TARGET_ACK_CTRL] = 0xfu, + [R_ACQ_FIFO_NEXT_DATA] = 0x1u, + [R_HOST_NACK_HANDLER_TIMEOUT] = 0xfu, + [R_CONTROLLER_EVENTS] = 0x1u, + [R_TARGET_EVENTS] = 0x1u, +}; + +static bool ot_i2c_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + uint32_t reg = (uint32_t)(addr >> 2u); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return reg < REGS_COUNT && (!is_write || (I2C_PERMIT[reg] & ~reg_be) == 0u); +} + static const MemoryRegionOps ot_i2c_ops = { .read = &ot_i2c_read, .write = &ot_i2c_write, .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4, .impl.max_access_size = 4, + .valid.accepts = &ot_i2c_accepts, }; static const Property ot_i2c_properties[] = { @@ -1364,6 +1852,244 @@ static void ot_i2c_reset_enter(Object *obj, ResetType type) s->check_timings = true; s->address_mask_0 = 0x0u; s->matched_address = 0x0u; + s->in_bus_xact = false; + s->in_target_xact = false; + s->in_target_transfer = false; + s->restart_pending = false; + s->target_read_mode = false; + s->expect_stop = false; + s->nack_transaction = false; + s->ack_ctrl_stretching = false; + s->tx_stretching = false; + s->cmd_complete_stretching = false; + s->cmd_complete_wait_reenable = false; + s->host_pending_stop = false; + s->host_pending_nakok = false; + s->acq_fifo_next_data = 0; + s->last_ack_result = 0; + s->active_target_addr = 0x0u; + s->ovrd_len = 0; + s->ovrd_start_ns = INT64_MAX; + s->ovrd_consumed = false; + ot_i2c_reset_bitbang_target(s); +} + +I2CBus *ot_i2c_get_active_target_bus(I2CBus *default_bus, uint8_t address) +{ + I2CBus *bus = default_bus; + OtI2CState *def = bus ? OT_I2C(BUS(bus)->parent) : NULL; + if (!(def && ot_i2c_target_enabled(def) && + ot_i2c_check_address_match(def, address))) { + for (unsigned i = 0; i < ARRAY_SIZE(ot_i2c_instances); i++) { + OtI2CState *inst = ot_i2c_instances[i]; + if (inst && ot_i2c_target_enabled(inst) && + ot_i2c_check_address_match(inst, address)) { + bus = inst->bus; + break; + } + } + } + if (bus) { + OT_I2C(BUS(bus)->parent)->matched_address = address; + } + return bus; +} + +bool ot_i2c_is_target_enabled(OtI2CState *s) +{ + return s && ot_i2c_target_enabled(s); +} + +bool ot_i2c_is_target_polling_acq(OtI2CState *s) +{ + return s && s->target_poll_acq_count >= 2u; +} + +void ot_i2c_reset_bitbang_target(OtI2CState *s) +{ + if (!s) { + return; + } + s->target_poll_acq_count = 0; + s->bb_prev_scl = true; + s->bb_prev_sda = true; + s->bb_state = OT_I2C_BB_IDLE; + s->bb_shift_reg = 0; + s->bb_bit_count = 0; + s->bb_cur_tx_byte = 0xffu; + s->bb_drive_sda_low = false; + s->bb_seen_valid_start = false; +} + +bool ot_i2c_bitbang_target_step(OtI2CState *s, bool scl, bool sda_in) +{ + if (!s || !ot_i2c_target_enabled(s)) { + return sda_in; + } + + /* 1. Check START / STOP conditions while SCL is high */ + if (s->bb_prev_scl && scl) { + if (s->bb_prev_sda && !sda_in) { + /* START or REPEATED START condition */ + s->bb_state = OT_I2C_BB_ADDR; + s->bb_bit_count = 0; + s->bb_shift_reg = 0; + s->bb_drive_sda_low = false; + } else if (!s->bb_prev_sda && sda_in) { + /* STOP condition */ + if (s->bb_seen_valid_start) { + ot_i2c_target_set_acqdata(s, 0, SIGNAL_STOP); + ot_i2c_irq_set_state(s, CMD_COMPLETE, true); + } + s->bb_state = OT_I2C_BB_IDLE; + s->bb_seen_valid_start = false; + s->bb_drive_sda_low = false; + } + } + + /* 2. Check SCL Rising Edge (!prev_scl -> scl) */ + if (!s->bb_prev_scl && scl) { + switch (s->bb_state) { + case OT_I2C_BB_ADDR: + case OT_I2C_BB_WRITE_DATA: + if (s->bb_bit_count < 8u) { + s->bb_shift_reg = + (uint8_t)((s->bb_shift_reg << 1u) | (sda_in ? 1u : 0u)); + s->bb_bit_count++; + } else if (s->bb_bit_count == 8u) { + s->bb_bit_count = 9u; + } + break; + case OT_I2C_BB_READ_DATA: + if (s->bb_bit_count < 8u) { + s->bb_bit_count++; + } else if (s->bb_bit_count == 8u) { + if (sda_in) { + s->bb_state = OT_I2C_BB_IGNORE; + } + s->bb_bit_count = 9u; + } + break; + default: + break; + } + } + + /* 3. Check SCL Falling Edge (prev_scl -> !scl) */ + if (s->bb_prev_scl && !scl) { + switch (s->bb_state) { + case OT_I2C_BB_ADDR: + if (s->bb_bit_count == 8u) { + uint8_t addr = s->bb_shift_reg >> 1u; + bool match = ot_i2c_check_address_match(s, addr); + s->bb_drive_sda_low = match; + s->bb_state = match ? OT_I2C_BB_ADDR : OT_I2C_BB_IGNORE; + if (match) { + OtI2CSignal sig = + s->bb_seen_valid_start ? SIGNAL_RESTART : SIGNAL_START; + ot_i2c_target_set_acqdata(s, s->bb_shift_reg, sig); + s->bb_seen_valid_start = true; + } + } else if (s->bb_bit_count == 9u) { + bool is_read = (s->bb_shift_reg & 1u) != 0; + s->bb_bit_count = 0; + s->bb_shift_reg = 0; + if (!is_read) { + s->bb_state = OT_I2C_BB_WRITE_DATA; + s->bb_drive_sda_low = false; + } else { + s->bb_state = OT_I2C_BB_READ_DATA; + s->bb_cur_tx_byte = fifo8_is_empty(&s->target_tx_fifo) ? + 0xffu : + fifo8_pop(&s->target_tx_fifo); + ot_i2c_irq_set_state(s, TX_THRESHOLD, + ot_i2c_tx_threshold_intr(s)); + s->bb_drive_sda_low = + (((s->bb_cur_tx_byte >> 7u) & 1u) == 0); + } + } + break; + case OT_I2C_BB_WRITE_DATA: + if (s->bb_bit_count == 8u) { + s->bb_drive_sda_low = true; + ot_i2c_target_set_acqdata(s, s->bb_shift_reg, SIGNAL_NONE); + } else if (s->bb_bit_count == 9u) { + s->bb_bit_count = 0; + s->bb_shift_reg = 0; + s->bb_drive_sda_low = false; + } + break; + case OT_I2C_BB_READ_DATA: + if (s->bb_bit_count < 8u) { + s->bb_drive_sda_low = + (((s->bb_cur_tx_byte >> (7u - s->bb_bit_count)) & 1u) == 0); + } else if (s->bb_bit_count == 8u) { + s->bb_drive_sda_low = false; + } else if (s->bb_bit_count == 9u) { + s->bb_bit_count = 0; + s->bb_cur_tx_byte = fifo8_is_empty(&s->target_tx_fifo) ? + 0xffu : + fifo8_pop(&s->target_tx_fifo); + ot_i2c_irq_set_state(s, TX_THRESHOLD, + ot_i2c_tx_threshold_intr(s)); + s->bb_drive_sda_low = (((s->bb_cur_tx_byte >> 7u) & 1u) == 0); + } + break; + default: + s->bb_drive_sda_low = false; + break; + } + } + + s->bb_prev_scl = scl; + s->bb_prev_sda = sda_in; + + return sda_in && !s->bb_drive_sda_low; +} + +bool ot_i2c_is_override_waveform_ready(OtI2CState *s) +{ + return s && !s->ovrd_consumed && s->ovrd_len >= 31u; +} + +bool ot_i2c_is_override_enabled(OtI2CState *s) +{ + return s && ((s->regs[R_OVRD] & R_OVRD_TXOVRDEN_MASK) != 0) && + !s->ovrd_consumed; +} + +void ot_i2c_set_waveform_start_ns(OtI2CState *s, int64_t val_ns, bool relative) +{ + if (!relative) { + s->ovrd_start_ns = val_ns; + } else if (s->ovrd_start_ns != INT64_MAX) { + s->ovrd_start_ns += val_ns; + } +} + +bool ot_i2c_get_override_pin_level(OtI2CState *s, bool is_sda, + int64_t sample_ns) +{ + if (!s || s->ovrd_len == 0 || sample_ns < s->ovrd_start_ns) { + return true; + } + int64_t rel_ns = sample_ns - s->ovrd_start_ns; + unsigned idx = 0; + for (unsigned i = 0; i < s->ovrd_len; i++) { + if (s->ovrd_steps[i].offset_ns <= rel_ns) { + idx = i; + } else { + break; + } + } + return is_sda ? s->ovrd_steps[idx].sda : s->ovrd_steps[idx].scl; +} + +void ot_i2c_consume_override_waveform(OtI2CState *s) +{ + if (s) { + s->ovrd_consumed = true; + } } static void ot_i2c_realize(DeviceState *dev, Error **errp) @@ -1376,6 +2102,14 @@ static void ot_i2c_realize(DeviceState *dev, Error **errp) g_assert(s->clock_src); OBJECT_CHECK(IbexClockSrcIf, s->clock_src, TYPE_IBEX_CLOCK_SRC_IF); + if (!strcmp(s->ot_id, "i2c0")) { + ot_i2c_instances[0] = s; + } else if (!strcmp(s->ot_id, "i2c1")) { + ot_i2c_instances[1] = s; + } else if (!strcmp(s->ot_id, "i2c2")) { + ot_i2c_instances[2] = s; + } + qdev_init_gpio_in_named(DEVICE(s), &ot_i2c_clock_input, "clock-in", 1); /* TODO: check if the following can be moved to ot_i2c_init */ @@ -1402,6 +2136,8 @@ static void ot_i2c_init(Object *obj) fifo8_create(&s->host_rx_fifo, OT_I2C_FIFO_SIZE); fifo8_create(&s->target_tx_fifo, OT_I2C_FIFO_SIZE); ot_fifo32_create(&s->target_rx_fifo, OT_I2C_ACQ_FIFO_SIZE); + + s->fmt_timer = timer_new_ns(OT_VIRTUAL_CLOCK, &ot_i2c_fmt_timer_cb, s); } static void ot_i2c_class_init(ObjectClass *klass, const void *data) @@ -1429,6 +2165,7 @@ static const TypeInfo ot_i2c_info = { .class_init = &ot_i2c_class_init, }; + static void ot_i2c_register_types(void) { type_register_static(&ot_i2c_info); diff --git a/include/hw/opentitan/ot_i2c.h b/include/hw/opentitan/ot_i2c.h index eb4d299310c08..a3739ee6045f3 100644 --- a/include/hw/opentitan/ot_i2c.h +++ b/include/hw/opentitan/ot_i2c.h @@ -31,6 +31,7 @@ #define HW_OPENTITAN_OT_I2C_H #include "qom/object.h" +#include "hw/i2c/i2c.h" #include "hw/sysbus.h" #define TYPE_OT_I2C "ot-i2c" @@ -39,4 +40,22 @@ OBJECT_DECLARE_TYPE(OtI2CState, OtI2CClass, OT_I2C) #define TYPE_OT_I2C_TARGET TYPE_OT_I2C "-target" OBJECT_DECLARE_SIMPLE_TYPE(OtI2CTarget, OT_I2C_TARGET) +bool ot_i2c_bus_target_is_stretching(I2CBus *bus); +int ot_i2c_bus_target_get_last_ack(I2CBus *bus); +bool ot_i2c_bus_target_check_tx_stretch(I2CBus *bus); +void ot_i2c_bus_target_repeated_start(I2CBus *bus); + +bool ot_i2c_is_override_waveform_ready(OtI2CState *s); +bool ot_i2c_is_override_enabled(OtI2CState *s); +void ot_i2c_set_waveform_start_ns(OtI2CState *s, int64_t val_ns, bool relative); +bool ot_i2c_get_override_pin_level(OtI2CState *s, bool is_sda, + int64_t sample_ns); +void ot_i2c_consume_override_waveform(OtI2CState *s); + +I2CBus *ot_i2c_get_active_target_bus(I2CBus *default_bus, uint8_t address); +bool ot_i2c_is_target_enabled(OtI2CState *s); +bool ot_i2c_is_target_polling_acq(OtI2CState *s); +void ot_i2c_reset_bitbang_target(OtI2CState *s); +bool ot_i2c_bitbang_target_step(OtI2CState *s, bool scl, bool sda_in); + #endif /* HW_OPENTITAN_OT_I2C_H */ From 6c01d057be17909999926016a31a29f3fe7113fe Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:40 +0000 Subject: [PATCH 42/52] [ot] hw/opentitan: i2c: add pmod-i2c-sensor QOM device and wiring Add the "pmod-i2c-sensor" I2CSlave QOM device in hw/opentitan/ot_i2c.c and wire it onto ot-i2c0..2 in hw/riscv/ot_earlgrey.c across the OpenTitan PMOD I2C peripheral addresses (0x10, 0x1d, 0x22, 0x29, 0x30, 0x40, 0x51, 0x52, 0x59, 0x7c). Emulate register-level responses and state transitions for the PMOD I2C breakout board targets (ADXL345 accelerometer 0x1d, clock-stretching target 0x22, LTR303ALS ambient light detector 0x29, MMC34160PJ compass 0x30, HDC1080 humidity/temperature sensor 0x40, MB85RC64V FRAM 0x51/0x7c, 24LC64 EEPROM 0x52 with busy-write NAK polling, SGP30 gas sensor 0x59, and ADM1191 power monitor 0x10). Also connect pmod-i2c-sensor address 0x22 rx_delay_ms/tx_delay_ms registers to ot_i2c_get_target_stretch_ns(). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I9f9776c9e9e2279f03761ea86ec87b286a6a6964 --- hw/opentitan/ot_i2c.c | 146 +++++++++++++++++++++++++++++++++++++++++ hw/riscv/Kconfig | 1 + hw/riscv/ot_earlgrey.c | 26 ++++++++ 3 files changed, 173 insertions(+) diff --git a/hw/opentitan/ot_i2c.c b/hw/opentitan/ot_i2c.c index 41d3f763f2b8f..6aad4d236744c 100644 --- a/hw/opentitan/ot_i2c.c +++ b/hw/opentitan/ot_i2c.c @@ -2165,11 +2165,157 @@ static const TypeInfo ot_i2c_info = { .class_init = &ot_i2c_class_init, }; +static bool pmod_i2c_sensor_match_and_add(I2CSlave *candidate, uint8_t address, + bool broadcast, + I2CNodeList *current_devs) +{ + (void)broadcast; + if (address != candidate->address) { + return false; + } + BusState *abus = qdev_get_parent_bus(DEVICE(candidate)); + if (abus && abus->parent && + object_dynamic_cast(OBJECT(abus->parent), TYPE_OT_I2C)) { + OtI2CState *ot_i2c = OT_I2C(abus->parent); + if (ot_i2c_target_enabled(ot_i2c) && + ot_i2c_check_address_match(ot_i2c, address)) { + return false; + } + } + I2CNode *node = g_new0(struct I2CNode, 1u); + node->elt = candidate; + QLIST_INSERT_HEAD(current_devs, node, next); + return true; +} + +static void pmod_i2c_sensor_reset(DeviceState *dev) +{ + PmodI2CSensorState *s = PMOD_I2C_SENSOR(dev); + memset(s->regs, 0, sizeof(s->regs)); + s->reg_ptr = 0; + s->addr_bytes = 0; + s->busy_nak = false; + + switch (s->parent_obj.address) { + case 0x10u: /* ADM1191 / PAC1934 Power Monitor */ + s->regs[0xfdu] = 0x7bu; + s->regs[0xfeu] = 0x54u; + break; + case 0x1du: /* ADXL345 Accelerometer */ + s->regs[0x00u] = 0xe5u; + s->regs[0x30u] = 0x80u; + s->regs[0x32u] = 0x01u; + break; + case 0x22u: /* Clock Stretching Test Device */ + s->regs[0xddu] = 0xa2u; + s->regs[0xdeu] = 0xa1u; + break; + case 0x29u: /* LTR-303ALS Ambient Light Detector */ + s->regs[0x86u] = 0xa0u; + s->regs[0x87u] = 0x05u; + s->regs[0x8cu] = 0x04u; + break; + case 0x30u: /* MMC34160PJ Compass */ + s->regs[0x00u] = 0x01u; + s->regs[0x06u] = 0x01u; + s->regs[0x20u] = 0x06u; + break; + case 0x40u: /* HDC1080 Humidity/Temp (16-bit regs mapped at reg << 1) */ + s->regs[0x00u] = 0x66u; + s->regs[0x02u] = 0x80u; + s->regs[0xfcu] = 0x54u; + s->regs[0xfdu] = 0x49u; + s->regs[0xfeu] = 0x10u; + s->regs[0xffu] = 0x50u; + break; + case 0x59u: /* SGP30 Gas Sensor (0xD4 = kSelfTestPass & non-00/non-FF) */ + memset(s->regs, 0xd4u, sizeof(s->regs)); + break; + case 0x7cu: /* I2C Reserved Device ID for MB85RC256V FRAM (0x51 << 1 = 0xA2) + */ + s->regs[0xa3u] = 0xa5u; + s->regs[0xa4u] = 0x10u; + break; + default: + break; + } +} + +static int pmod_i2c_sensor_event(I2CSlave *i2c, enum i2c_event event) +{ + PmodI2CSensorState *s = PMOD_I2C_SENSOR(i2c); + if ((event == I2C_START_SEND || event == I2C_START_RECV) && s->busy_nak) { + s->busy_nak = false; + return 1; + } + if (event == I2C_START_SEND || event == I2C_START_SEND_ASYNC) { + s->addr_bytes = 0; + } + return 0; +} + +static int pmod_i2c_sensor_send(I2CSlave *i2c, uint8_t data) +{ + PmodI2CSensorState *s = PMOD_I2C_SENSOR(i2c); + uint8_t addr = s->parent_obj.address; + if (addr == 0x59u) { + return 0; + } + uint8_t asize = (addr == 0x51u || addr == 0x52u) ? 2u : 1u; + if (s->addr_bytes < asize) { + uint8_t mapped = (addr == 0x40u) ? (uint8_t)(data << 1u) : + (addr == 0x22u && data == 0xcfu) ? 0x00u : + data; + s->reg_ptr = (s->addr_bytes == 0) ? + mapped : + ((s->reg_ptr << 8u) | mapped) & 0x7ffu; + s->addr_bytes++; + return 0; + } + s->regs[s->reg_ptr] = data; + s->reg_ptr = (s->reg_ptr + 1u) & (asize == 2u ? 0x7ffu : 0xffu); + if (addr == 0x52u) { + s->busy_nak = true; + } + return 0; +} + +static uint8_t pmod_i2c_sensor_recv(I2CSlave *i2c) +{ + PmodI2CSensorState *s = PMOD_I2C_SENSOR(i2c); + uint8_t addr = s->parent_obj.address; + uint8_t val = s->regs[s->reg_ptr]; + s->reg_ptr = + (s->reg_ptr + 1u) & ((addr == 0x51u || addr == 0x52u) ? 0x7ffu : 0xffu); + return val; +} + +static void pmod_i2c_sensor_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass); + (void)data; + + dc->desc = "PMOD I2C Sensor Device"; + device_class_set_legacy_reset(dc, &pmod_i2c_sensor_reset); + sc->match_and_add = &pmod_i2c_sensor_match_and_add; + sc->event = &pmod_i2c_sensor_event; + sc->send = &pmod_i2c_sensor_send; + sc->recv = &pmod_i2c_sensor_recv; +} + +static const TypeInfo pmod_i2c_sensor_info = { + .name = TYPE_PMOD_I2C_SENSOR, + .parent = TYPE_I2C_SLAVE, + .instance_size = sizeof(PmodI2CSensorState), + .class_init = &pmod_i2c_sensor_class_init, +}; static void ot_i2c_register_types(void) { type_register_static(&ot_i2c_info); type_register_static(&ot_i2c_target_info); + type_register_static(&pmod_i2c_sensor_info); } type_init(ot_i2c_register_types); diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index 6e39fc193aa2a..7b3380d9f742e 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -72,6 +72,7 @@ config OT_DARJEELING config OT_EARLGREY bool default y + select AT24C select IBEX select IBEX_CLOCK_SRC select IBEX_COMMON diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index ae00d837e1e86..3f23020952bb0 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -32,6 +32,7 @@ #include "hw/block/flash.h" #include "hw/boards.h" #include "hw/core/split-irq.h" +#include "hw/i2c/i2c.h" #include "hw/intc/sifive_plic.h" #include "hw/jtag/tap_ctrl.h" #include "hw/jtag/tap_ctrl_rbb.h" @@ -2046,6 +2047,31 @@ static void ot_eg_board_realize(DeviceState *dev, Error **errp) g_free(flashname); g_free(busname); } + + for (unsigned i2c_idx = 0; i2c_idx < 3; i2c_idx++) { + DeviceState *i2cdev = + RISCV_OT_EG_SOC(soc)->devices[OT_EG_SOC_DEV_I2C0 + i2c_idx]; + char *busname = g_strdup_printf("ot-i2c%u", i2c_idx); + BusState *i2cbus = qdev_get_child_bus(i2cdev, busname); + g_assert(i2cbus); + + /* PMOD I2C Sensor / Stretch / FRAM / EEPROM targets */ + static const uint8_t pmod_sensor_addrs[] = { + 0x10u, 0x1du, 0x22u, 0x29u, 0x30u, 0x40u, 0x51u, 0x52u, 0x59u, 0x7cu + }; + for (size_t s_idx = 0; s_idx < ARRAY_SIZE(pmod_sensor_addrs); s_idx++) { + DeviceState *sensor = qdev_new("pmod-i2c-sensor"); + qdev_prop_set_uint8(sensor, "address", pmod_sensor_addrs[s_idx]); + char *sensor_name = g_strdup_printf("pmod-sensor%u-%02x", i2c_idx, + pmod_sensor_addrs[s_idx]); + object_property_add_child(OBJECT(board), sensor_name, + OBJECT(sensor)); + qdev_realize_and_unref(sensor, i2cbus, &error_fatal); + g_free(sensor_name); + } + + g_free(busname); + } } static void ot_eg_board_init(Object *obj) From 9164895cb9933f3971cc6cf3796b1d75daf68466 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:40 +0000 Subject: [PATCH 43/52] [ot] hw/opentitan: pwm: implement Earlgrey PWM controller model Implement the Earlgrey 6-channel AON PWM controller device model (TYPE_OT_PWM, hw/ip/pwm/rtl/pwm_core.sv and pwm_chan.sv): - Model all 23 PWM registers (ALERT_TEST, REGWEN, CFG, PWM_EN, INVERT, PWM_PARAM_0..5, DUTY_CYCLE_0..5, BLINK_PARAM_0..5) with RW0C REGWEN write-protection, subword write byte-enable checks, and one-shot ALERT_TEST pulses. - Implement 16-bit phase counter beat synthesis (preserving phase_ctr_q across CFG updates unless beat_ctr_q == CLK_DIV per pwm_core.sv:85-103), DC_RESN dc_mask rounding, PHASE_DELAY offsets, INVERT polarity, standard blink mode (BLINK_EN=1, HTBT_EN=0), and positive/negative heartbeat ramping (BLINK_EN=1, HTBT_EN=1, reloading dc_htbt_q from DUTY_CYCLE.A only while HTBT_EN == 0 per pwm_chan.sv:140-154). - Preserve AON PWM state across non-PoR low-power exits and export ot_pwm_get_pin_level() for Pinmux/GPIO pad evaluation. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I9dc9d812da87178d856e135fa1acc35c6a6a6964 --- hw/opentitan/Kconfig | 2 + hw/opentitan/meson.build | 1 + hw/opentitan/ot_pwm.c | 365 ++++++++++++++++++++++++++++++++++ hw/riscv/Kconfig | 1 + hw/riscv/ot_earlgrey.c | 9 +- include/hw/opentitan/ot_pwm.h | 23 +++ 6 files changed, 395 insertions(+), 6 deletions(-) create mode 100644 hw/opentitan/ot_pwm.c create mode 100644 include/hw/opentitan/ot_pwm.h diff --git a/hw/opentitan/Kconfig b/hw/opentitan/Kconfig index a67cbc679ec91..08b1f30dc7bf8 100644 --- a/hw/opentitan/Kconfig +++ b/hw/opentitan/Kconfig @@ -180,6 +180,8 @@ config OT_PRINCE config OT_PRNG bool +config OT_PWM + bool config OT_PWRMGR select OT_CLOCK_CTRL diff --git a/hw/opentitan/meson.build b/hw/opentitan/meson.build index 62a7b3d77f5b0..ee622318c3634 100644 --- a/hw/opentitan/meson.build +++ b/hw/opentitan/meson.build @@ -49,6 +49,7 @@ system_ss.add(when: 'CONFIG_OT_PLIC_EXT', if_true: files('ot_plic_ext.c')) system_ss.add(when: 'CONFIG_OT_PRESENT', if_true: files('ot_present.c')) system_ss.add(when: 'CONFIG_OT_PRINCE', if_true: files('ot_prince.c')) system_ss.add(when: 'CONFIG_OT_PRNG', if_true: files('ot_prng.c')) +system_ss.add(when: 'CONFIG_OT_PWM', if_true: files('ot_pwm.c')) system_ss.add(when: 'CONFIG_OT_PWRMGR', if_true: files('ot_pwrmgr.c')) system_ss.add(when: 'CONFIG_OT_ROM_CTRL', if_true: files('ot_rom_ctrl.c', 'ot_rom_ctrl_img.c')) system_ss.add(when: 'CONFIG_OT_RSTMGR', if_true: files('ot_rstmgr.c')) diff --git a/hw/opentitan/ot_pwm.c b/hw/opentitan/ot_pwm.c new file mode 100644 index 0000000000000..bc896b9825ca8 --- /dev/null +++ b/hw/opentitan/ot_pwm.c @@ -0,0 +1,365 @@ +/* + * QEMU OpenTitan PWM device + * + * Copyright lowRISC contributors. + * + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qemu/timer.h" +#include "hw/opentitan/ot_alert.h" +#include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_pwm.h" +#include "hw/opentitan/ot_rstmgr.h" +#include "hw/qdev-properties.h" +#include "hw/registerfields.h" +#include "hw/riscv/ibex_common.h" +#include "hw/riscv/ibex_irq.h" + +#define PARAM_NUM_OUTPUTS 6u + +/* clang-format off */ +REG32(ALERT_TEST, 0x00u) + FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u) +REG32(REGWEN, 0x04u) + FIELD(REGWEN, EN, 0u, 1u) +REG32(CFG, 0x08u) + FIELD(CFG, CLK_DIV, 0u, 27u) + FIELD(CFG, DC_RESN, 27u, 4u) + FIELD(CFG, CNTR_EN, 31u, 1u) +REG32(PWM_EN, 0x0cu) +REG32(INVERT, 0x10u) +REG32(PWM_PARAM_0, 0x14u) +REG32(DUTY_CYCLE_0, 0x2cu) +REG32(BLINK_PARAM_0, 0x44u) +/* clang-format on */ + +#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t)) +#define R_LAST_REG (R_BLINK_PARAM_0 + PARAM_NUM_OUTPUTS - 1u) +#define REGS_COUNT (R_LAST_REG + 1u) +#define REGS_SIZE (REGS_COUNT * sizeof(uint32_t)) + +struct OtPwmState { + SysBusDevice parent_obj; + + MemoryRegion mmio; + IbexIRQ alert; + + uint32_t regs[REGS_COUNT]; + int64_t cntr_start_ns; + uint32_t beat_ctr_q; + uint16_t phase_ctr_q; + uint16_t dc_htbt_q[PARAM_NUM_OUTPUTS]; + + char *ot_id; +}; + +static uint64_t ot_pwm_calc_phase(const OtPwmState *s, int64_t now_ns, + uint32_t *beat_out) +{ + uint32_t beat = s->beat_ctr_q; + uint64_t total_phase = s->phase_ctr_q; + if (FIELD_EX32(s->regs[R_CFG], CFG, CNTR_EN) && now_ns > s->cntr_start_ns) { + uint64_t ticks = (uint64_t)(now_ns - s->cntr_start_ns) / 4000ULL; + uint32_t clk_div = FIELD_EX32(s->regs[R_CFG], CFG, CLK_DIV); + uint32_t dc_resn = FIELD_EX32(s->regs[R_CFG], CFG, DC_RESN); + uint64_t period = (uint64_t)clk_div + 1ULL; + uint64_t rem = (beat <= clk_div) ? (clk_div - beat) : 0ULL; + if (ticks <= rem) { + beat += (uint32_t)ticks; + } else { + uint64_t after = ticks - (rem + 1ULL); + beat = (uint32_t)(after % period); + total_phase += (1ULL + after / period) << (15u - dc_resn); + } + } + if (beat_out) { + *beat_out = beat; + } + return total_phase; +} + +void ot_pwm_shift_start_ns(OtPwmState *s, int64_t delta_ns) +{ + if (s) { + s->cntr_start_ns += delta_ns; + } +} + +bool ot_pwm_is_active(OtPwmState *s) +{ + if (!s) { + return false; + } + bool cntr_en = (bool)FIELD_EX32(s->regs[R_CFG], CFG, CNTR_EN); + bool chan_en = (s->regs[R_PWM_EN] != 0); + return cntr_en && chan_en; +} + +bool ot_pwm_get_channel_level(OtPwmState *s, unsigned ch, int64_t now_ns) +{ + if (!s || ch >= PARAM_NUM_OUTPUTS) { + return false; + } + + bool chan_en = (bool)((s->regs[R_PWM_EN] >> ch) & 1u); + bool invert = (bool)((s->regs[R_INVERT] >> ch) & 1u); + + if (!chan_en) { + return invert; + } + + uint32_t dc_resn = FIELD_EX32(s->regs[R_CFG], CFG, DC_RESN); + uint64_t total_phase = ot_pwm_calc_phase(s, now_ns, NULL); + uint16_t phase_cntr = (uint16_t)total_phase; + uint64_t cycle_idx = total_phase >> 16u; + + uint32_t duty_reg = s->regs[R_DUTY_CYCLE_0 + ch]; + uint16_t duty_a = (uint16_t)(duty_reg & 0xffffu); + uint16_t duty_b = (uint16_t)((duty_reg >> 16u) & 0xffffu); + + uint32_t param_reg = s->regs[R_PWM_PARAM_0 + ch]; + uint16_t phase_delay = (uint16_t)(param_reg & 0xffffu); + bool htbt_en = (bool)((param_reg >> 30u) & 1u); + bool blink_en = (bool)((param_reg >> 31u) & 1u); + + uint32_t blink_reg = s->regs[R_BLINK_PARAM_0 + ch]; + uint16_t blink_x = (uint16_t)(blink_reg & 0xffffu); + uint16_t blink_y = (uint16_t)((blink_reg >> 16u) & 0xffffu); + + uint16_t duty_cycle_actual = + (blink_en && htbt_en) ? s->dc_htbt_q[ch] : duty_a; + if (blink_en && !htbt_en) { + uint16_t blink_sum = (uint16_t)(blink_x + blink_y + 1u); + uint16_t blink_ctr = + (uint16_t)(cycle_idx % ((uint64_t)blink_sum + 1ULL)); + duty_cycle_actual = (blink_ctr > blink_x) ? duty_b : duty_a; + } else if (blink_en && htbt_en && duty_a != duty_b) { + uint64_t htbt_steps = cycle_idx / ((uint64_t)blink_x + 1ULL); + bool pos_htbt = (duty_a < duty_b); + bool htbt_dir = !pos_htbt; + uint16_t dc_htbt = s->dc_htbt_q[ch]; + bool dc_wrap_q = false; + uint64_t max_steps = MIN(htbt_steps, 4096ULL); + for (uint64_t i = 0; i < max_steps; i++) { + uint32_t ext = + htbt_dir ? ((uint32_t)dc_htbt - (uint32_t)blink_y - 1u) : + ((uint32_t)dc_htbt + (uint32_t)blink_y + 1u); + bool dc_wrap = (ext >> 16u) & 1u; + uint16_t dc_htbt_d = (uint16_t)ext; + dc_wrap_q = + pos_htbt ? (dc_wrap && !htbt_dir) : (dc_wrap && htbt_dir); + dc_htbt = dc_htbt_d; + if (pos_htbt && (dc_htbt >= duty_b || dc_wrap)) { + htbt_dir = true; + } else if (pos_htbt && dc_htbt == duty_a) { + htbt_dir = false; + } else if (!pos_htbt && (dc_htbt <= duty_b || dc_wrap)) { + htbt_dir = false; + } else if (!pos_htbt && dc_htbt == duty_a) { + htbt_dir = true; + } + } + duty_cycle_actual = !dc_wrap_q ? dc_htbt : (pos_htbt ? 0xffffu : 0u); + } + + uint16_t dc_mask = (uint16_t)(0xffffu >> (dc_resn + 1u)); + uint16_t phase_delay_masked = phase_delay & ~dc_mask; + uint16_t duty_cycle_masked = duty_cycle_actual & ~dc_mask; + + uint32_t off_full = + (uint32_t)phase_delay_masked + (uint32_t)duty_cycle_masked; + bool phase_wrap = (off_full > 0xffffu); + uint16_t off_phase = (uint16_t)off_full; + + bool on_phase_exceeded = (phase_cntr >= phase_delay_masked); + bool off_phase_exceeded = (phase_cntr >= off_phase); + + bool active = phase_wrap ? (on_phase_exceeded || !off_phase_exceeded) : + (on_phase_exceeded && !off_phase_exceeded); + + return invert ? !active : active; +} + +static bool ot_pwm_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 0x3u)); + uint8_t permit = (reg == R_CFG || reg >= R_PWM_PARAM_0) ? 0xfu : 0x1u; + return reg < REGS_COUNT && (!is_write || (permit & ~reg_be) == 0u); +} + +static uint64_t ot_pwm_read(void *opaque, hwaddr addr, unsigned size) +{ + OtPwmState *s = opaque; + (void)size; + hwaddr reg = R32_OFF(addr); + return (reg == R_ALERT_TEST) ? 0u : s->regs[reg]; +} + +static void ot_pwm_write(void *opaque, hwaddr addr, uint64_t val64, + unsigned size) +{ + OtPwmState *s = opaque; + (void)size; + uint32_t val32 = (uint32_t)val64; + hwaddr reg = R32_OFF(addr); + + if (reg == R_ALERT_TEST) { + if (val32 & R_ALERT_TEST_FATAL_FAULT_MASK) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } + return; + } + + if (reg == R_REGWEN) { + s->regs[R_REGWEN] &= (val32 & R_REGWEN_EN_MASK); + return; + } + + if (!s->regs[R_REGWEN]) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: Write to locked register 0x%02x\n", __func__, + s->ot_id, (uint32_t)addr); + return; + } + + int64_t now_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + uint64_t ticks = (now_ns > s->cntr_start_ns) ? + (uint64_t)(now_ns - s->cntr_start_ns) / 4000ULL : + 0ULL; + if (FIELD_EX32(s->regs[R_CFG], CFG, CNTR_EN) && ticks > 0ULL) { + s->phase_ctr_q = (uint16_t)ot_pwm_calc_phase(s, now_ns, &s->beat_ctr_q); + s->cntr_start_ns += (int64_t)(ticks * 4000ULL); + } + + switch (reg) { + case R_CFG: + /* + * In hw/ip/pwm/rtl/pwm_core.sv:85-103, + * phase_ctr_en = beat_end & (clr_phase_cntr | cntr_en), where + * beat_end = (beat_ctr_q == clk_div). Because clr_phase_cntr is gated + * by beat_end, phase_ctr_q is preserved across CFG updates unless + * beat_ctr_q equals the newly written CLK_DIV. + */ + if (s->beat_ctr_q == FIELD_EX32(val32, CFG, CLK_DIV)) { + s->phase_ctr_q = 0u; + } + s->beat_ctr_q = 0u; + s->regs[R_CFG] = val32; + s->cntr_start_ns = now_ns; + break; + case R_PWM_EN: { + uint32_t prev_en = s->regs[R_PWM_EN]; + s->regs[R_PWM_EN] = val32 & ((1u << PARAM_NUM_OUTPUTS) - 1u); + if (!prev_en && s->regs[R_PWM_EN] && + FIELD_EX32(s->regs[R_CFG], CFG, CNTR_EN)) { + s->beat_ctr_q = 0u; + s->phase_ctr_q = 0u; + s->cntr_start_ns = now_ns; + } + break; + } + case R_INVERT: + s->regs[R_INVERT] = val32 & ((1u << PARAM_NUM_OUTPUTS) - 1u); + break; + case R_PWM_PARAM_0 ...(R_PWM_PARAM_0 + PARAM_NUM_OUTPUTS - 1u): + s->regs[reg] = val32 & 0xc000ffffu; + break; + default: + s->regs[reg] = val32; + break; + } + + /* + * In hw/ip/pwm/rtl/pwm_chan.sv:140-154, dc_htbt_q only loads + * duty_cycle_a_i while htbt_en_i == 0 (ignoring blink_en_i == 0), so + * writing DUTY_CYCLE while htbt_en == 1 does not re-seed dc_htbt_q. + */ + for (unsigned ch = 0; ch < PARAM_NUM_OUTPUTS; ch++) { + if (!(s->regs[R_PWM_PARAM_0 + ch] & (1u << 30u))) { + s->dc_htbt_q[ch] = + (uint16_t)(s->regs[R_DUTY_CYCLE_0 + ch] & 0xffffu); + } + } + +} + +static const MemoryRegionOps ot_pwm_regs_ops = { + .read = &ot_pwm_read, + .write = &ot_pwm_write, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl.min_access_size = 4u, + .impl.max_access_size = 4u, + .valid.min_access_size = 1u, + .valid.max_access_size = 4u, + .valid.accepts = &ot_pwm_accepts, +}; + +static const Property ot_pwm_properties[] = { + DEFINE_PROP_STRING(OT_COMMON_DEV_ID, OtPwmState, ot_id), +}; + +static void ot_pwm_reset_enter(Object *obj, ResetType type) +{ + OtPwmState *s = OT_PWM(obj); + + if (type != RESET_TYPE_COLD && ot_rstmgr_is_low_power_exit()) { + ibex_irq_set(&s->alert, 0); + return; + } + + memset(s->regs, 0, sizeof(s->regs)); + s->regs[R_REGWEN] = 1u; + s->regs[R_CFG] = (7u << R_CFG_DC_RESN_SHIFT) | 0x8000u; + for (unsigned i = 0; i < PARAM_NUM_OUTPUTS; i++) { + s->regs[R_DUTY_CYCLE_0 + i] = 0x7fff7fffull; + s->dc_htbt_q[i] = 0x7fffu; + } + s->cntr_start_ns = 0; + s->beat_ctr_q = 0u; + s->phase_ctr_q = 0u; + ibex_irq_set(&s->alert, 0); +} + +static void ot_pwm_init(Object *obj) +{ + OtPwmState *s = OT_PWM(obj); + + memory_region_init_io(&s->mmio, obj, &ot_pwm_regs_ops, s, TYPE_OT_PWM, + 0x80u); + sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); + ibex_qdev_init_irq(obj, &s->alert, OT_DEVICE_ALERT); +} + +static void ot_pwm_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + (void)data; + + RESETTABLE_CLASS(klass)->phases.enter = &ot_pwm_reset_enter; + device_class_set_props(dc, ot_pwm_properties); + set_bit(DEVICE_CATEGORY_MISC, dc->categories); +} + +static const TypeInfo ot_pwm_info = { + .name = TYPE_OT_PWM, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(OtPwmState), + .instance_init = &ot_pwm_init, + .class_init = &ot_pwm_class_init, +}; + +static void ot_pwm_register_types(void) +{ + type_register_static(&ot_pwm_info); +} + +type_init(ot_pwm_register_types); diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index 7b3380d9f742e..8fa1e607d285d 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -102,6 +102,7 @@ config OT_EARLGREY select OT_OTP_OT_BE select OT_PINMUX_EG select OT_PLIC_EXT + select OT_PWM select OT_PWRMGR select OT_ROM_CTRL select OT_RSTMGR diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 3f23020952bb0..c3318f926bb23 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -64,6 +64,7 @@ #include "hw/opentitan/ot_otp_ot_be.h" #include "hw/opentitan/ot_pinmux_eg.h" #include "hw/opentitan/ot_plic_ext.h" +#include "hw/opentitan/ot_pwm.h" #include "hw/opentitan/ot_pwrmgr.h" #include "hw/opentitan/ot_rom_ctrl.h" #include "hw/opentitan/ot_rstmgr.h" @@ -1059,16 +1060,12 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { ) }, [OT_EG_SOC_DEV_PWM] = { - .type = TYPE_OT_UNIMP, - .cfg = &ibex_unimp_configure, + .type = TYPE_OT_PWM, .memmap = MEMMAPENTRIES( { .base = 0x40450000u } ), .prop = IBEXDEVICEPROPDEFS( - IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "pwm"), - IBEX_DEV_UINT_PROP("size", 0x80u), - IBEX_DEV_UINT_PROP("alert-count", 1u), - IBEX_DEV_BOOL_PROP("warn-once", true) + IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "pwm") ), .gpio = IBEXGPIOCONNDEFS( OT_EG_SOC_GPIO_ALERT(0, 29) diff --git a/include/hw/opentitan/ot_pwm.h b/include/hw/opentitan/ot_pwm.h new file mode 100644 index 0000000000000..dbb2860974c30 --- /dev/null +++ b/include/hw/opentitan/ot_pwm.h @@ -0,0 +1,23 @@ +/* + * QEMU OpenTitan PWM device + * + * Copyright lowRISC contributors. + * + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef HW_OPENTITAN_OT_PWM_H +#define HW_OPENTITAN_OT_PWM_H + +#include "qom/object.h" +#include "hw/sysbus.h" + +#define TYPE_OT_PWM "ot-pwm" +OBJECT_DECLARE_SIMPLE_TYPE(OtPwmState, OT_PWM) + +bool ot_pwm_get_channel_level(OtPwmState *s, unsigned ch, int64_t now_ns); +void ot_pwm_shift_start_ns(OtPwmState *s, int64_t delta_ns); +bool ot_pwm_is_active(OtPwmState *s); + +#endif /* HW_OPENTITAN_OT_PWM_H */ From ef19cb464002d6ccc387f293ca72ba8bc0ecd330 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:40 +0000 Subject: [PATCH 44/52] [ot] hw/opentitan: pattgen: implement 2-channel pattern generator IP Implement the Earlgrey 2-channel hardware pattern generator model (TYPE_OT_PATTGEN, hw/ip/pattgen/rtl/pattgen_core.sv and pattgen_chan.sv): - Model all pattgen registers (INTR_STATE/ENABLE/TEST, ALERT_TEST, CTRL, PREDIV_CH0/CH1, DATA_CH0_0..1, DATA_CH1_0..1, SIZE) with subword write byte-enable checks (ot_pattgen_regs_accepts) and active-channel write protection when ENABLE_CHx is asserted. - Implement dual-channel 64-bit serial bit shifting (PDA0/PCL0, PDA1/PCL1), clock pre-divider timing, POLARITY inversion, INACTIVE_LEVEL_PDA/PCL retention across mid-pattern aborts vs. full REPS completion, and DONE_CH0/DONE_CH1 interrupt assertion. - Export ot_pattgen_get_pin_level() in include/hw/opentitan/ot_pattgen.h and add Kconfig symbols (OT_PATTGEN). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I9ad73a5f87d0eddb30cc8a656c3f459e6a6a6964 --- hw/opentitan/Kconfig | 2 + hw/opentitan/meson.build | 1 + hw/opentitan/ot_pattgen.c | 356 ++++++++++++++++++++++++++++++ hw/riscv/Kconfig | 1 + hw/riscv/ot_earlgrey.c | 12 +- include/hw/opentitan/ot_pattgen.h | 21 ++ 6 files changed, 386 insertions(+), 7 deletions(-) create mode 100644 hw/opentitan/ot_pattgen.c create mode 100644 include/hw/opentitan/ot_pattgen.h diff --git a/hw/opentitan/Kconfig b/hw/opentitan/Kconfig index 08b1f30dc7bf8..39caf3d3ac9c7 100644 --- a/hw/opentitan/Kconfig +++ b/hw/opentitan/Kconfig @@ -161,6 +161,8 @@ config OT_OTP_OT_BE select OT_OTP_IF bool +config OT_PATTGEN + bool config OT_PINMUX_DJ bool diff --git a/hw/opentitan/meson.build b/hw/opentitan/meson.build index ee622318c3634..291770b80b5bb 100644 --- a/hw/opentitan/meson.build +++ b/hw/opentitan/meson.build @@ -43,6 +43,7 @@ system_ss.add(when: 'CONFIG_OT_OTP_ENGINE', if_true: files('ot_otp_engine.c')) system_ss.add(when: 'CONFIG_OT_OTP_IF', if_true: files('ot_otp_if.c')) system_ss.add(when: 'CONFIG_OT_OTP_IMPL_IF', if_true: files('ot_otp_impl_if.c')) system_ss.add(when: 'CONFIG_OT_OTP_OT_BE', if_true: files('ot_otp_ot_be.c')) +system_ss.add(when: 'CONFIG_OT_PATTGEN', if_true: files('ot_pattgen.c')) system_ss.add(when: 'CONFIG_OT_PINMUX_EG', if_true: files('ot_pinmux_eg.c')) system_ss.add(when: 'CONFIG_OT_PINMUX_DJ', if_true: files('ot_pinmux_dj.c')) system_ss.add(when: 'CONFIG_OT_PLIC_EXT', if_true: files('ot_plic_ext.c')) diff --git a/hw/opentitan/ot_pattgen.c b/hw/opentitan/ot_pattgen.c new file mode 100644 index 0000000000000..aa31f539381ad --- /dev/null +++ b/hw/opentitan/ot_pattgen.c @@ -0,0 +1,356 @@ +/* + * QEMU OpenTitan Pattern Generator (PATTGEN) device + * + * Copyright lowRISC contributors. + * + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qemu/timer.h" +#include "hw/opentitan/ot_alert.h" +#include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_pattgen.h" +#include "hw/qdev-properties.h" +#include "hw/registerfields.h" +#include "hw/riscv/ibex_common.h" +#include "hw/riscv/ibex_irq.h" + +#define PARAM_NUM_CHANNELS 2u +#define PARAM_NUM_IRQS 2u + +/* clang-format off */ +REG32(INTR_STATE, 0x00u) +REG32(INTR_ENABLE, 0x04u) +REG32(INTR_TEST, 0x08u) +REG32(ALERT_TEST, 0x0cu) + FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u) +REG32(CTRL, 0x10u) +REG32(PREDIV_CH0, 0x14u) +REG32(DATA_CH0_0, 0x1cu) +REG32(SIZE, 0x2cu) +/* clang-format on */ + +#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t)) +#define R_LAST_REG (R_SIZE) +#define REGS_COUNT (R_LAST_REG + 1u) +#define REGS_SIZE (REGS_COUNT * sizeof(uint32_t)) + +typedef struct { + bool active; + bool pending_start; + bool polarity; + bool inactive_pcl; + bool inactive_pda; + uint32_t prediv; + uint64_t data; + uint32_t len; + uint32_t reps; +} OtPattgenChan; + +struct OtPattgenState { + SysBusDevice parent_obj; + + MemoryRegion mmio; + IbexIRQ irqs[PARAM_NUM_IRQS]; + IbexIRQ alert; + QEMUTimer *timer; + + OtGpioEgState *gpio; + + uint32_t regs[REGS_COUNT]; + OtPattgenChan chan[PARAM_NUM_CHANNELS]; + bool pda[PARAM_NUM_CHANNELS]; + bool pcl[PARAM_NUM_CHANNELS]; + + char *ot_id; +}; + +static void ot_pattgen_update_irqs(OtPattgenState *s) +{ + uint32_t state = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]; + for (unsigned i = 0; i < PARAM_NUM_IRQS; i++) { + ibex_irq_set(&s->irqs[i], (int)((state >> i) & 1u)); + } +} + +static void ot_pattgen_latch_channel(OtPattgenState *s, unsigned c) +{ + OtPattgenChan *ch = &s->chan[c]; + uint32_t ctrl = s->regs[R_CTRL]; + ch->polarity = (bool)((ctrl >> (2u + c)) & 1u); + ch->inactive_pcl = (bool)((ctrl >> (4u + 2u * c)) & 1u); + ch->inactive_pda = (bool)((ctrl >> (5u + 2u * c)) & 1u); + ch->prediv = s->regs[R_PREDIV_CH0 + c]; + uint64_t lo = s->regs[R_DATA_CH0_0 + 2u * c]; + uint64_t hi = s->regs[R_DATA_CH0_0 + 2u * c + 1u]; + ch->data = lo | (hi << 32); + uint32_t size = s->regs[R_SIZE]; + ch->len = (size >> (c == 0u ? 0u : 16u)) & 0x3fu; + ch->reps = (size >> (c == 0u ? 6u : 22u)) & 0x3ffu; +} + +bool ot_pattgen_get_pin_level(OtPattgenState *s, unsigned pin_idx) +{ + if (!s || pin_idx >= 4u) { + return false; + } + return (pin_idx & 1u) ? s->pcl[pin_idx >> 1u] : s->pda[pin_idx >> 1u]; +} + +static void ot_pattgen_timer_cb(void *opaque) +{ + OtPattgenState *s = OT_PATTGEN(opaque); + bool run_ch[PARAM_NUM_CHANNELS] = { false, false }; + uint32_t total_steps[PARAM_NUM_CHANNELS] = { 0, 0 }; + uint32_t k[PARAM_NUM_CHANNELS] = { 1, 1 }; + + for (unsigned c = 0; c < PARAM_NUM_CHANNELS; c++) { + bool en = (bool)((s->regs[R_CTRL] >> c) & 1u); + if (en && s->chan[c].pending_start) { + s->chan[c].pending_start = false; + s->chan[c].active = true; + run_ch[c] = true; + uint32_t total_bits = + (s->chan[c].len + 1u) * (s->chan[c].reps + 1u); + total_steps[c] = 2u * total_bits; + k[c] = 0; + } + } + + if (!run_ch[0] && !run_ch[1]) { + return; + } + + uint64_t base_us = + 10000ULL; + uint32_t done_mask = 0; + + if (s->gpio) { + } + + while ((run_ch[0] && k[0] <= total_steps[0]) || + (run_ch[1] && k[1] <= total_steps[1])) { + uint64_t step_us[PARAM_NUM_CHANNELS] = { UINT64_MAX, UINT64_MAX }; + for (unsigned c = 0; c < PARAM_NUM_CHANNELS; c++) { + if (run_ch[c] && k[c] <= total_steps[c]) { + uint64_t t_ns = + ((uint64_t)k[c] * ((uint64_t)s->chan[c].prediv + 1ULL) * + 1000000000ULL) / + 6000000ULL; + step_us[c] = base_us + (t_ns / 1000ULL); + } + } + + uint64_t next_us = MIN(step_us[0], step_us[1]); + + for (unsigned c = 0; c < PARAM_NUM_CHANNELS; c++) { + if (run_ch[c] && k[c] <= total_steps[c] && step_us[c] == next_us) { + uint32_t step = k[c]; + if (step < total_steps[c]) { + bool pcl_int = (step & 1u) != 0; + s->pcl[c] = s->chan[c].polarity ? !pcl_int : pcl_int; + unsigned bit_idx = (step / 2u) % (s->chan[c].len + 1u); + s->pda[c] = (bool)((s->chan[c].data >> bit_idx) & 1u); + } else { + s->chan[c].active = false; + s->pcl[c] = s->chan[c].inactive_pcl; + s->pda[c] = s->chan[c].inactive_pda; + done_mask |= (1u << c); + } + k[c]++; + } + } + + if (s->gpio) { + } + } + + if (s->gpio) { + } + + if (done_mask) { + s->regs[R_INTR_STATE] |= done_mask; + ot_pattgen_update_irqs(s); + } +} + +static bool ot_pattgen_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 0x3u)); + uint8_t permit = (reg >= R_PREDIV_CH0) ? 0xfu : 0x1u; + return reg < REGS_COUNT && (!is_write || (permit & ~reg_be) == 0u); +} + +static uint64_t ot_pattgen_read(void *opaque, hwaddr addr, unsigned size) +{ + OtPattgenState *s = opaque; + (void)size; + hwaddr reg = R32_OFF(addr); + if (reg == R_INTR_TEST || reg == R_ALERT_TEST) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: W/O register 0x%02" HWADDR_PRIx "\n", __func__, + addr); + return 0; + } + return s->regs[reg]; +} + +static void ot_pattgen_write(void *opaque, hwaddr addr, uint64_t val64, + unsigned size) +{ + OtPattgenState *s = opaque; + (void)size; + uint32_t val32 = (uint32_t)val64; + hwaddr reg = R32_OFF(addr); + + switch (reg) { + case R_INTR_STATE: + s->regs[R_INTR_STATE] &= ~(val32 & 0x3u); + ot_pattgen_update_irqs(s); + break; + case R_INTR_ENABLE: + s->regs[R_INTR_ENABLE] = val32 & 0x3u; + ot_pattgen_update_irqs(s); + break; + case R_INTR_TEST: + s->regs[R_INTR_STATE] |= (val32 & 0x3u); + ot_pattgen_update_irqs(s); + break; + case R_ALERT_TEST: + if (val32 & R_ALERT_TEST_FATAL_FAULT_MASK) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } + break; + case R_CTRL: + val32 &= 0xffu; + /* fall through */ + default: { + uint32_t old_ctrl = s->regs[R_CTRL]; + s->regs[reg] = val32; + bool schedule_timer = false; + uint64_t max_duration_ns = 0; + + for (unsigned c = 0; c < PARAM_NUM_CHANNELS; c++) { + bool was_en = (bool)((old_ctrl >> c) & 1u); + bool now_en = (bool)((s->regs[R_CTRL] >> c) & 1u); + if (!now_en) { + s->chan[c].pending_start = false; + ot_pattgen_latch_channel(s, c); + if (s->chan[c].active) { + s->pda[c] = (bool)(s->chan[c].data & 1u); + s->pcl[c] = s->chan[c].polarity; + } else { + s->pda[c] = s->chan[c].inactive_pda; + s->pcl[c] = s->chan[c].inactive_pcl; + } + } else if (!was_en && now_en) { + ot_pattgen_latch_channel(s, c); + s->chan[c].active = true; + s->chan[c].pending_start = true; + schedule_timer = true; + uint64_t total_steps = + 2ULL * ((uint64_t)s->chan[c].len + 1ULL) * + ((uint64_t)s->chan[c].reps + 1ULL); + uint64_t dur_ns = + (total_steps * ((uint64_t)s->chan[c].prediv + 1ULL) * + 1000000000ULL) / + 6000000ULL; + max_duration_ns = MAX(max_duration_ns, MAX(dur_ns, 1000ULL)); + } + } + + if (s->gpio) { + } + + if (schedule_timer) { + timer_mod_ns(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + + (int64_t)max_duration_ns); + } + break; + } + } +} + +static const MemoryRegionOps ot_pattgen_regs_ops = { + .read = &ot_pattgen_read, + .write = &ot_pattgen_write, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl = { + .min_access_size = 4u, + .max_access_size = 4u, + }, + .valid = { + .min_access_size = 1u, + .max_access_size = 4u, + .accepts = &ot_pattgen_accepts, + }, +}; + +static const Property ot_pattgen_properties[] = { + DEFINE_PROP_STRING(OT_COMMON_DEV_ID, OtPattgenState, ot_id), + DEFINE_PROP_LINK("gpio", OtPattgenState, gpio, TYPE_OT_GPIO_EG, + OtGpioEgState *), +}; + +static void ot_pattgen_reset(DeviceState *dev) +{ + OtPattgenState *s = OT_PATTGEN(dev); + + timer_del(s->timer); + memset(s->regs, 0, sizeof(s->regs)); + memset(s->chan, 0, sizeof(s->chan)); + for (unsigned c = 0; c < PARAM_NUM_CHANNELS; c++) { + s->pda[c] = false; + s->pcl[c] = false; + } + ot_pattgen_update_irqs(s); + ibex_irq_set(&s->alert, 0); +} + +static void ot_pattgen_init(Object *obj) +{ + OtPattgenState *s = OT_PATTGEN(obj); + + memory_region_init_io(&s->mmio, obj, &ot_pattgen_regs_ops, s, + TYPE_OT_PATTGEN, REGS_SIZE); + sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); + + for (unsigned i = 0; i < PARAM_NUM_IRQS; i++) { + ibex_sysbus_init_irq(obj, &s->irqs[i]); + } + ibex_qdev_init_irq(obj, &s->alert, OT_DEVICE_ALERT); + + s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &ot_pattgen_timer_cb, s); +} + +static void ot_pattgen_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + (void)data; + + device_class_set_legacy_reset(dc, &ot_pattgen_reset); + device_class_set_props(dc, ot_pattgen_properties); +} + +static const TypeInfo ot_pattgen_info = { + .name = TYPE_OT_PATTGEN, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(OtPattgenState), + .instance_init = &ot_pattgen_init, + .class_init = &ot_pattgen_class_init, +}; + +static void ot_pattgen_register_types(void) +{ + type_register_static(&ot_pattgen_info); +} + +type_init(ot_pattgen_register_types); diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index 8fa1e607d285d..2a121ec63d942 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -100,6 +100,7 @@ config OT_EARLGREY select OT_OTP_EG select OT_OTP_IF select OT_OTP_OT_BE + select OT_PATTGEN select OT_PINMUX_EG select OT_PLIC_EXT select OT_PWM diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index c3318f926bb23..00e07bfb8a988 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -62,6 +62,7 @@ #include "hw/opentitan/ot_otp_eg.h" #include "hw/opentitan/ot_otp_if.h" #include "hw/opentitan/ot_otp_ot_be.h" +#include "hw/opentitan/ot_pattgen.h" #include "hw/opentitan/ot_pinmux_eg.h" #include "hw/opentitan/ot_plic_ext.h" #include "hw/opentitan/ot_pwm.h" @@ -737,19 +738,16 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { ), }, [OT_EG_SOC_DEV_PATTGEN] = { - .type = TYPE_OT_UNIMP, - .cfg = &ibex_unimp_configure, + .type = TYPE_OT_PATTGEN, .memmap = MEMMAPENTRIES( { .base = 0x400e0000u } ), .prop = IBEXDEVICEPROPDEFS( - IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "pattgen"), - IBEX_DEV_UINT_PROP("size", 0x40u), - IBEX_DEV_UINT_PROP("irq-count", 2u), - IBEX_DEV_UINT_PROP("alert-count", 1u), - IBEX_DEV_BOOL_PROP("warn-once", true) + IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "pattgen") ), .gpio = IBEXGPIOCONNDEFS( + OT_EG_SOC_GPIO_SYSBUS_IRQ(0, PLIC, 122), + OT_EG_SOC_GPIO_SYSBUS_IRQ(1, PLIC, 123), OT_EG_SOC_GPIO_ALERT(0, 9) ) }, diff --git a/include/hw/opentitan/ot_pattgen.h b/include/hw/opentitan/ot_pattgen.h new file mode 100644 index 0000000000000..a377d4d52e40d --- /dev/null +++ b/include/hw/opentitan/ot_pattgen.h @@ -0,0 +1,21 @@ +/* + * QEMU OpenTitan Pattern Generator (PATTGEN) device + * + * Copyright lowRISC contributors. + * + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef HW_OPENTITAN_OT_PATTGEN_H +#define HW_OPENTITAN_OT_PATTGEN_H + +#include "qom/object.h" +#include "hw/sysbus.h" + +#define TYPE_OT_PATTGEN "ot-pattgen" +OBJECT_DECLARE_SIMPLE_TYPE(OtPattgenState, OT_PATTGEN) + +bool ot_pattgen_get_pin_level(OtPattgenState *s, unsigned pin_idx); + +#endif /* HW_OPENTITAN_OT_PATTGEN_H */ From 2102db903516ff21fee927c0989d1abe55f2970d Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:41 +0000 Subject: [PATCH 45/52] [ot] hw/opentitan: sysrst_ctrl: implement combo and pin control Implement the Earlgrey AON System Reset Controller device model (TYPE_OT_SYSRST_CTRL, hw/ip/sysrst_ctrl/rtl/sysrst_ctrl.sv): - Model all 26 sysrst_ctrl registers with RW0C REGWEN write protection, subword write byte-enable checks (SYSRST_CTRL_PERMIT), Status-type RO INTR_STATE evaluation, and one-shot ALERT_TEST pulses. - Implement the 4 key-combo pre-condition and detection state machines (COM_PRE_SEL_CTL_0..3, COM_PRE_DET_CTL_0..3, COM_SEL_CTL_0..3, COM_DET_CTL_0..3, COM_OUT_CTL_0..3) driving COMBO_INTR_STATUS, EC_RST_L stretch pulses (EC_RST_CTL), and OT_SYSRST_CTRL_RST_REQ. - Implement ULP Z3 debounce (ULP_AC/LID/PWRB_DEBOUNCE_CTL, ULP_CTL, ULP_STATUS), key H2L/L2H edge interrupts (KEY_INTR_CTL, KEY_INTR_STATUS), auto-block overrides, and PIN_OUT_CTL/PIN_OUT_VALUE. - Export ot_sysrst_ctrl_get_outsel_level() and preserve AON state across non-PoR sleep exits. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I047370c775f33c7247415f0f385026cd6a6a6964 --- hw/opentitan/Kconfig | 2 + hw/opentitan/meson.build | 1 + hw/opentitan/ot_sysrst_ctrl.c | 722 ++++++++++++++++++++++++++ hw/riscv/Kconfig | 1 + hw/riscv/ot_earlgrey.c | 15 +- include/hw/opentitan/ot_sysrst_ctrl.h | 38 ++ 6 files changed, 772 insertions(+), 7 deletions(-) create mode 100644 hw/opentitan/ot_sysrst_ctrl.c create mode 100644 include/hw/opentitan/ot_sysrst_ctrl.h diff --git a/hw/opentitan/Kconfig b/hw/opentitan/Kconfig index 39caf3d3ac9c7..f3bcb256c38f5 100644 --- a/hw/opentitan/Kconfig +++ b/hw/opentitan/Kconfig @@ -219,6 +219,8 @@ config OT_SRAM_CTRL select OT_OTP_IF bool +config OT_SYSRST_CTRL + bool config OT_TIMER bool diff --git a/hw/opentitan/meson.build b/hw/opentitan/meson.build index 291770b80b5bb..2db7332acc753 100644 --- a/hw/opentitan/meson.build +++ b/hw/opentitan/meson.build @@ -60,6 +60,7 @@ system_ss.add(when: 'CONFIG_OT_SOC_DBG_CTRL', if_true: files('ot_soc_dbg_ctrl.c' system_ss.add(when: 'CONFIG_OT_SPI_DEVICE', if_true: files('ot_spi_device.c')) system_ss.add(when: 'CONFIG_OT_SPI_HOST', if_true: files('ot_spi_host.c')) system_ss.add(when: 'CONFIG_OT_SRAM_CTRL', if_true: files('ot_sram_ctrl.c')) +system_ss.add(when: 'CONFIG_OT_SYSRST_CTRL', if_true: files('ot_sysrst_ctrl.c')) system_ss.add(when: 'CONFIG_OT_TIMER', if_true: files('ot_timer.c')) system_ss.add(when: 'CONFIG_OT_UART', if_true: files('ot_uart.c')) system_ss.add(when: 'CONFIG_OT_UNIMP', if_true: files('ot_unimp.c')) diff --git a/hw/opentitan/ot_sysrst_ctrl.c b/hw/opentitan/ot_sysrst_ctrl.c new file mode 100644 index 0000000000000..2c4f886266a2c --- /dev/null +++ b/hw/opentitan/ot_sysrst_ctrl.c @@ -0,0 +1,722 @@ +/* + * QEMU OpenTitan System Reset Controller (sysrst_ctrl) device + * + * Copyright lowRISC contributors. + * + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qemu/main-loop.h" +#include "qemu/timer.h" +#include "hw/core/cpu.h" +#include "hw/opentitan/ot_alert.h" +#include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_pinmux_eg.h" +#include "hw/opentitan/ot_rstmgr.h" +#include "hw/opentitan/ot_sysrst_ctrl.h" +#include "hw/qdev-properties.h" +#include "hw/registerfields.h" +#include "hw/riscv/ibex_common.h" +#include "hw/riscv/ibex_irq.h" +#include "hw/sysbus.h" +#include "system/replay.h" + +/* clang-format off */ +REG32(INTR_STATE, 0x0u) + FIELD(INTR_STATE, EVENT_DETECTED, 0u, 1u) +REG32(INTR_ENABLE, 0x4u) +REG32(INTR_TEST, 0x8u) +REG32(ALERT_TEST, 0xcu) + FIELD(ALERT_TEST, FATAL_FAULT, 0u, 1u) +REG32(REGWEN, 0x10u) + FIELD(REGWEN, EN, 0u, 1u) +REG32(EC_RST_CTL, 0x14u) +REG32(ULP_AC_DEBOUNCE_CTL, 0x18u) +REG32(ULP_LID_DEBOUNCE_CTL, 0x1cu) +REG32(ULP_PWRB_DEBOUNCE_CTL, 0x20u) +REG32(ULP_CTL, 0x24u) +REG32(ULP_STATUS, 0x28u) +REG32(WKUP_STATUS, 0x2cu) + FIELD(WKUP_STATUS, WAKEUP_STS, 0u, 1u) +REG32(KEY_INVERT_CTL, 0x30u) +REG32(PIN_ALLOWED_CTL, 0x34u) +REG32(PIN_OUT_CTL, 0x38u) +REG32(PIN_OUT_VALUE, 0x3cu) +REG32(PIN_IN_VALUE, 0x40u) +REG32(KEY_INTR_CTL, 0x44u) +REG32(KEY_INTR_DEBOUNCE_CTL, 0x48u) +REG32(AUTO_BLOCK_DEBOUNCE_CTL, 0x4cu) +REG32(AUTO_BLOCK_OUT_CTL, 0x50u) +REG32(COM_PRE_SEL_CTL_0, 0x54u) +REG32(COM_PRE_DET_CTL_0, 0x64u) +REG32(COM_SEL_CTL_0, 0x74u) +REG32(COM_DET_CTL_0, 0x84u) +REG32(COM_OUT_CTL_0, 0x94u) + FIELD(COM_OUT_CTL_0, BAT_DISABLE, 0u, 1u) + FIELD(COM_OUT_CTL_0, INTERRUPT, 1u, 1u) + FIELD(COM_OUT_CTL_0, EC_RST, 2u, 1u) + FIELD(COM_OUT_CTL_0, RST_REQ, 3u, 1u) +REG32(COMBO_INTR_STATUS, 0xa4u) +REG32(KEY_INTR_STATUS, 0xa8u) +/* clang-format on */ + +#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t)) +#define R_LAST_REG (R_KEY_INTR_STATUS) +#define REGS_COUNT (R_LAST_REG + 1u) +#define REGS_SIZE (REGS_COUNT * sizeof(uint32_t)) + +struct OtSysrstCtrlState { + SysBusDevice parent_obj; + + MemoryRegion mmio; + IbexIRQ irq; + IbexIRQ alert; + IbexIRQ rst_req; + IbexIRQ wkup_req; + + uint32_t regs[REGS_COUNT]; + uint32_t pin_in_raw; + uint32_t last_pin_in_val; + uint32_t last_key_invert_ctl; + uint32_t last_read_pin_in; + uint32_t poll_count; + bool combo_precond_valid[4]; + bool ulp_ac_latched; + bool bat_disable_hw; + bool ec_rst_l_hw; + bool z3_wakeup_hw; + QEMUTimer *ec_rst_timer; + QEMUTimer *combo_rst_timer; + QEMUTimer *ulp_timer; + + char *ot_id; +}; + +static uint32_t ot_sysrst_ctrl_get_pin_outputs(const OtSysrstCtrlState *s) +{ + uint32_t raw = s->pin_in_raw; + uint32_t inv = s->regs[R_KEY_INVERT_CTL]; + + uint32_t pwrb_int = + ((raw >> OT_SYSRST_CTRL_IN_PWRB) & 1u) ^ ((inv >> 6u) & 1u); + uint32_t key0_int = + ((raw >> OT_SYSRST_CTRL_IN_KEY0) & 1u) ^ ((inv >> 0u) & 1u); + uint32_t key1_int = + ((raw >> OT_SYSRST_CTRL_IN_KEY1) & 1u) ^ ((inv >> 2u) & 1u); + uint32_t key2_int = + ((raw >> OT_SYSRST_CTRL_IN_KEY2) & 1u) ^ ((inv >> 4u) & 1u); + + bool ab_en = (s->regs[R_AUTO_BLOCK_DEBOUNCE_CTL] >> 16u) & 1u; + bool ab_cond = ab_en && (pwrb_int == 0u); + uint32_t ab_ctl = s->regs[R_AUTO_BLOCK_OUT_CTL]; + + uint32_t hw_in[8]; + hw_in[0] = s->bat_disable_hw ? 1u : 0u; + hw_in[1] = s->ec_rst_l_hw ? 1u : 0u; + hw_in[2] = pwrb_int; + hw_in[3] = + (ab_cond && ((ab_ctl >> 0u) & 1u)) ? ((ab_ctl >> 4u) & 1u) : key0_int; + hw_in[4] = + (ab_cond && ((ab_ctl >> 1u) & 1u)) ? ((ab_ctl >> 5u) & 1u) : key1_int; + hw_in[5] = + (ab_cond && ((ab_ctl >> 2u) & 1u)) ? ((ab_ctl >> 6u) & 1u) : key2_int; + hw_in[6] = s->z3_wakeup_hw ? 1u : 0u; + hw_in[7] = 0u; /* flash_wp_l has no passthrough input */ + + uint32_t out_ctl = s->regs[R_PIN_OUT_CTL]; + uint32_t out_val = s->regs[R_PIN_OUT_VALUE]; + uint32_t allow_ctl = s->regs[R_PIN_ALLOWED_CTL]; + + uint32_t out = 0; + for (unsigned b = 0; b < 8u; b++) { + bool en = (out_ctl >> b) & 1u; + bool val = (out_val >> b) & 1u; + bool allow0 = (allow_ctl >> b) & 1u; + bool allow1 = (allow_ctl >> (b + 8u)) & 1u; + uint32_t bit_out = (en && allow0 && !val) ? 0u : + (en && allow1 && val) ? 1u : + hw_in[b]; + out |= (bit_out << b); + } + + /* Apply output inversion per sysrst_ctrl.sv lines 303-311 */ + out ^= ((inv >> 9u) & 1u) << 0u; /* bat_disable */ + out ^= ((inv >> 7u) & 1u) << 2u; /* pwrb_out */ + out ^= ((inv >> 1u) & 1u) << 3u; /* key0_out */ + out ^= ((inv >> 3u) & 1u) << 4u; /* key1_out */ + out ^= ((inv >> 5u) & 1u) << 5u; /* key2_out */ + out ^= ((inv >> 11u) & 1u) << 6u; /* z3_wakeup */ + + return out; +} + +int ot_sysrst_ctrl_get_outsel_level(const OtSysrstCtrlState *s, uint32_t outsel) +{ + if (!s) { + return 0; + } + uint32_t out = ot_sysrst_ctrl_get_pin_outputs(s); + switch (outsel) { + case 72u: /* kTopEarlgreyPinmuxOutselSysrstCtrlAonBatDisable */ + return (int)((out >> 0u) & 1u); + case 73u: /* kTopEarlgreyPinmuxOutselSysrstCtrlAonKey0Out */ + return (int)((out >> 3u) & 1u); + case 74u: /* kTopEarlgreyPinmuxOutselSysrstCtrlAonKey1Out */ + return (int)((out >> 4u) & 1u); + case 75u: /* kTopEarlgreyPinmuxOutselSysrstCtrlAonKey2Out */ + return (int)((out >> 5u) & 1u); + case 76u: /* kTopEarlgreyPinmuxOutselSysrstCtrlAonPwrbOut */ + return (int)((out >> 2u) & 1u); + case 77u: /* kTopEarlgreyPinmuxOutselSysrstCtrlAonZ3Wakeup */ + return (int)((out >> 6u) & 1u); + case 100u: /* DIO ec_rst_l */ + return (int)((out >> 1u) & 1u); + case 101u: /* DIO flash_wp_l */ + return (int)((out >> 7u) & 1u); + default: + return 0; + } +} + +static uint32_t ot_sysrst_ctrl_get_pin_in_value(const OtSysrstCtrlState *s) +{ + uint32_t raw = s->pin_in_raw; + uint32_t out = ot_sysrst_ctrl_get_pin_outputs(s); + uint32_t val = 0; + + val |= ((raw >> OT_SYSRST_CTRL_IN_PWRB) & 1u) << 0u; + val |= ((raw >> OT_SYSRST_CTRL_IN_KEY0) & 1u) << 1u; + val |= ((raw >> OT_SYSRST_CTRL_IN_KEY1) & 1u) << 2u; + val |= ((raw >> OT_SYSRST_CTRL_IN_KEY2) & 1u) << 3u; + val |= ((raw >> OT_SYSRST_CTRL_IN_LID_OPEN) & 1u) << 4u; + val |= ((raw >> OT_SYSRST_CTRL_IN_AC_PRESENT) & 1u) << 5u; + + uint32_t ec_wire = ((raw >> OT_SYSRST_CTRL_IN_EC_RST_L) & (out >> 1u)) & 1u; + uint32_t wp_wire = + ((raw >> OT_SYSRST_CTRL_IN_FLASH_WP_L) & (out >> 7u)) & 1u; + + uint32_t ec_attr = ot_pinmux_eg_get_dio_pad_attr(10u); + uint32_t wp_attr = ot_pinmux_eg_get_dio_pad_attr(11u); + + uint32_t ec_raw = + (ec_attr & OT_PINMUX_PAD_ATTR_INPUT_DISABLE_MASK) ? 0u : ec_wire; + uint32_t wp_raw = + (wp_attr & OT_PINMUX_PAD_ATTR_INPUT_DISABLE_MASK) ? 0u : wp_wire; + + uint32_t ec_in = ec_raw ^ (ec_attr & OT_PINMUX_PAD_ATTR_INVERT_MASK); + uint32_t wp_in = wp_raw ^ (wp_attr & OT_PINMUX_PAD_ATTR_INVERT_MASK); + + val |= (ec_in << 6u) | (wp_in << 7u); + + return val; +} + +static void ot_sysrst_ctrl_get_inverted(uint32_t pin_in_val, uint32_t inv, + uint32_t *combo_in, + uint32_t *key_intr_in) +{ + uint32_t pwrb = ((pin_in_val >> 0u) & 1u) ^ ((inv >> 6u) & 1u); + uint32_t key0 = ((pin_in_val >> 1u) & 1u) ^ ((inv >> 0u) & 1u); + uint32_t key1 = ((pin_in_val >> 2u) & 1u) ^ ((inv >> 2u) & 1u); + uint32_t key2 = ((pin_in_val >> 3u) & 1u) ^ ((inv >> 4u) & 1u); + uint32_t ac = ((pin_in_val >> 5u) & 1u) ^ ((inv >> 8u) & 1u); + uint32_t ec_rst_l = (pin_in_val >> 6u) & 1u; + uint32_t flash_wp_l = (pin_in_val >> 7u) & 1u; + + if (combo_in) { + *combo_in = (key0 << 0u) | (key1 << 1u) | (key2 << 2u) | (pwrb << 3u) | + (ac << 4u); + } + if (key_intr_in) { + *key_intr_in = + (pwrb << 0u) | (key0 << 1u) | (key1 << 2u) | (key2 << 3u) | + (ac << 4u) | (ec_rst_l << 5u) | (flash_wp_l << 6u); + } +} + +static void ot_sysrst_ctrl_combo_rst_timer_cb(void *opaque) +{ + OtSysrstCtrlState *s = opaque; + ibex_irq_set(&s->rst_req, 1); +} + +static void ot_sysrst_ctrl_update_outputs(OtSysrstCtrlState *s) +{ + bool intr_event_status = + (s->regs[R_ULP_STATUS] | s->regs[R_COMBO_INTR_STATUS] | + s->regs[R_KEY_INTR_STATUS]) != 0; + bool test_q = (s->regs[R_INTR_TEST] & 1u) != 0; + if (intr_event_status || test_q) { + s->regs[R_INTR_STATE] |= R_INTR_STATE_EVENT_DETECTED_MASK; + } else { + s->regs[R_INTR_STATE] &= ~R_INTR_STATE_EVENT_DETECTED_MASK; + } + + bool irq_level = (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]) != 0; + ibex_irq_set(&s->irq, (int)irq_level); + + bool wkup_level = + (s->regs[R_WKUP_STATUS] & R_WKUP_STATUS_WAKEUP_STS_MASK) != 0; + ibex_irq_set(&s->wkup_req, (int)wkup_level); + +} + +static void ot_sysrst_ctrl_ec_rst_timer_cb(void *opaque) +{ + OtSysrstCtrlState *s = opaque; + s->ec_rst_l_hw = true; + ot_sysrst_ctrl_update_outputs(s); +} + +static void ot_sysrst_ctrl_ulp_timer_cb(void *opaque) +{ + OtSysrstCtrlState *s = opaque; + if (!(s->regs[R_ULP_CTL] & 1u) || s->ulp_ac_latched) { + return; + } + s->ulp_ac_latched = true; + s->z3_wakeup_hw = true; + s->regs[R_ULP_STATUS] |= 1u; + s->regs[R_WKUP_STATUS] |= R_WKUP_STATUS_WAKEUP_STS_MASK; + ot_sysrst_ctrl_update_outputs(s); +} + +static void ot_sysrst_ctrl_check_events(OtSysrstCtrlState *s) +{ + uint32_t prev_pin_val = s->last_pin_in_val; + uint32_t prev_inv = s->last_key_invert_ctl; + uint32_t curr_pin_val = ot_sysrst_ctrl_get_pin_in_value(s); + uint32_t curr_inv = s->regs[R_KEY_INVERT_CTL]; + s->last_pin_in_val = curr_pin_val; + s->last_key_invert_ctl = curr_inv; + + uint32_t prev_combo_in = 0; + uint32_t curr_combo_in = 0; + uint32_t prev_key_in = 0; + uint32_t curr_key_in = 0; + + ot_sysrst_ctrl_get_inverted(prev_pin_val, prev_inv, &prev_combo_in, + &prev_key_in); + ot_sysrst_ctrl_get_inverted(curr_pin_val, curr_inv, &curr_combo_in, + &curr_key_in); + + if (s->regs[R_ULP_CTL] & 1u) { + uint32_t prev_pwrb = + ((prev_pin_val >> 0u) & 1u) ^ ((prev_inv >> 6u) & 1u); + uint32_t curr_pwrb = + ((curr_pin_val >> 0u) & 1u) ^ ((curr_inv >> 6u) & 1u); + uint32_t prev_lid = + ((prev_pin_val >> 4u) & 1u) ^ ((prev_inv >> 10u) & 1u); + uint32_t curr_lid = + ((curr_pin_val >> 4u) & 1u) ^ ((curr_inv >> 10u) & 1u); + uint32_t curr_ac = + ((curr_pin_val >> 5u) & 1u) ^ ((curr_inv >> 8u) & 1u); + + bool pwrb_h2l = (prev_pwrb == 1u && curr_pwrb == 0u); + bool lid_l2h = (prev_lid == 0u && curr_lid == 1u); + bool ac_high = (curr_ac == 1u); + + uint32_t debounce = s->regs[R_ULP_AC_DEBOUNCE_CTL] & 0xffffu; + if (pwrb_h2l || lid_l2h || + (ac_high && !s->ulp_ac_latched && debounce == 0u)) { + s->ulp_ac_latched = true; + s->z3_wakeup_hw = true; + s->regs[R_ULP_STATUS] |= 1u; + s->regs[R_WKUP_STATUS] |= R_WKUP_STATUS_WAKEUP_STS_MASK; + } else if (ac_high) { + if (!s->ulp_ac_latched && !timer_pending(s->ulp_timer)) { + timer_mod_ns(s->ulp_timer, + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + + (int64_t)(debounce + 1u) * 5000LL); + } + } else if (!s->ulp_ac_latched) { + timer_del(s->ulp_timer); + } + } + + if (prev_combo_in == curr_combo_in && prev_key_in == curr_key_in) { + ot_sysrst_ctrl_update_outputs(s); + return; + } + + bool trigger_rst = false; + uint32_t rst_det_cyc = 0; + for (unsigned k = 0; k < 4u; k++) { + uint32_t pre_sel = s->regs[R_COM_PRE_SEL_CTL_0 + k] & 0x1fu; + uint32_t sel = s->regs[R_COM_SEL_CTL_0 + k] & 0x1fu; + if (!sel) { + s->combo_precond_valid[k] = false; + continue; + } + bool prev_pre_valid = (pre_sel == 0u) || s->combo_precond_valid[k]; + if (pre_sel != 0u) { + if ((curr_combo_in & pre_sel) != 0u) { + s->combo_precond_valid[k] = false; + } else if ((prev_combo_in & sel) != 0u) { + s->combo_precond_valid[k] = true; + } + } else { + s->combo_precond_valid[k] = true; + } + bool curr_pre_valid = s->combo_precond_valid[k]; + + bool prev_trig = prev_pre_valid && ((prev_combo_in & sel) == 0u); + bool curr_trig = curr_pre_valid && ((curr_combo_in & sel) == 0u); + uint32_t out_ctl = s->regs[R_COM_OUT_CTL_0 + k]; + if (!prev_trig && curr_trig) { + if (out_ctl & R_COM_OUT_CTL_0_BAT_DISABLE_MASK) { + s->bat_disable_hw = true; + } + if (out_ctl & + (R_COM_OUT_CTL_0_EC_RST_MASK | R_COM_OUT_CTL_0_RST_REQ_MASK)) { + s->ec_rst_l_hw = false; + timer_del(s->ec_rst_timer); + } + if (out_ctl & R_COM_OUT_CTL_0_RST_REQ_MASK) { + trigger_rst = true; + rst_det_cyc = s->regs[R_COM_DET_CTL_0 + k]; + } + if (out_ctl & R_COM_OUT_CTL_0_INTERRUPT_MASK) { + s->regs[R_COMBO_INTR_STATUS] |= (1u << k); + s->regs[R_WKUP_STATUS] |= R_WKUP_STATUS_WAKEUP_STS_MASK; + } + } else if (prev_trig && !curr_trig) { + if (out_ctl & R_COM_OUT_CTL_0_RST_REQ_MASK) { + timer_del(s->combo_rst_timer); + } + if (out_ctl & + (R_COM_OUT_CTL_0_EC_RST_MASK | R_COM_OUT_CTL_0_RST_REQ_MASK)) { + uint32_t stretch = s->regs[R_EC_RST_CTL] & 0xffffu; + if (stretch == 0u) { + s->ec_rst_l_hw = true; + } else { + int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + timer_mod_ns(s->ec_rst_timer, + now + (int64_t)stretch * 4000LL); + } + } + } + } + + uint32_t h2l = prev_key_in & ~curr_key_in; + uint32_t l2h = ~prev_key_in & curr_key_in; + uint32_t key_events = (h2l & 0x7fu) | ((l2h & 0x7fu) << 7u); + uint32_t active_events = key_events & (s->regs[R_KEY_INTR_CTL] & 0x3fffu); + if (active_events) { + s->regs[R_KEY_INTR_STATUS] |= active_events; + s->regs[R_WKUP_STATUS] |= R_WKUP_STATUS_WAKEUP_STS_MASK; + } + + if (trigger_rst) { + timer_mod_ns(s->combo_rst_timer, + qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + + (rst_det_cyc > 0u ? 3000000LL : 0LL)); + } + + ot_sysrst_ctrl_update_outputs(s); +} + +static void ot_sysrst_ctrl_input_change(void *opaque, int irq, int level) +{ + OtSysrstCtrlState *s = opaque; + + g_assert((unsigned)irq < OT_SYSRST_CTRL_IN_COUNT); + + bool prev_ec_raw = (s->pin_in_raw >> OT_SYSRST_CTRL_IN_EC_RST_L) & 1u; + bool curr_ec_raw = level != 0; + + if (level) { + s->pin_in_raw |= (1u << irq); + } else { + s->pin_in_raw &= ~(1u << irq); + } + s->poll_count = 0; + + if (timer_pending(s->combo_rst_timer)) { + timer_mod_ns(s->combo_rst_timer, + qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 3000000LL); + } + + /* + * Per sysrst_ctrl_comboact.sv: ec_rst_l_i High -> Low transition + * (ec_rst_l_det_pulse) asserts ec_rst_l_q = 0 and stretches it for + * EC_RST_CTL cycles. + */ + if (irq == OT_SYSRST_CTRL_IN_EC_RST_L && prev_ec_raw && !curr_ec_raw) { + uint32_t stretch = s->regs[R_EC_RST_CTL] & 0xffffu; + if (stretch > 0u) { + s->ec_rst_l_hw = false; + int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + timer_mod_ns(s->ec_rst_timer, now + (int64_t)stretch * 4000LL); + } + } + + ot_sysrst_ctrl_check_events(s); +} + +static uint32_t ot_sysrst_ctrl_reg_mask(hwaddr reg) +{ + switch (reg) { + case R_COM_PRE_DET_CTL_0 ...(R_COM_PRE_DET_CTL_0 + 3u): + case R_COM_DET_CTL_0 ...(R_COM_DET_CTL_0 + 3u): + return 0xffffffffu; + case R_AUTO_BLOCK_DEBOUNCE_CTL: + return 0x0001ffffu; + case R_EC_RST_CTL: + case R_ULP_AC_DEBOUNCE_CTL: + case R_ULP_LID_DEBOUNCE_CTL: + case R_ULP_PWRB_DEBOUNCE_CTL: + case R_PIN_ALLOWED_CTL: + case R_KEY_INTR_DEBOUNCE_CTL: + return 0x0000ffffu; + case R_KEY_INTR_CTL: + case R_KEY_INTR_STATUS: + return 0x00003fffu; + case R_KEY_INVERT_CTL: + return 0x00000fffu; + case R_PIN_OUT_CTL: + case R_PIN_OUT_VALUE: + return 0x000000ffu; + case R_AUTO_BLOCK_OUT_CTL: + return 0x00000077u; + case R_COM_PRE_SEL_CTL_0 ...(R_COM_PRE_SEL_CTL_0 + 3u): + case R_COM_SEL_CTL_0 ...(R_COM_SEL_CTL_0 + 3u): + return 0x0000001fu; + case R_COM_OUT_CTL_0 ...(R_COM_OUT_CTL_0 + 3u): + case R_COMBO_INTR_STATUS: + return 0x0000000fu; + default: + return 0x00000001u; + } +} + +static bool ot_sysrst_ctrl_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + if (reg >= REGS_COUNT || (addr & 3u) + size > 4u) { + return false; + } + if (!is_write) { + return true; + } + uint32_t mask = ot_sysrst_ctrl_reg_mask(reg); + uint8_t permit = (mask > 0x1ffffu) ? 0xfu : + (mask > 0xffffu) ? 0x7u : + (mask > 0xffu) ? 0x3u : + 0x1u; + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + return (permit & ~reg_be) == 0u; +} + +static uint64_t ot_sysrst_ctrl_read(void *opaque, hwaddr addr, unsigned size) +{ + OtSysrstCtrlState *s = opaque; + (void)size; + hwaddr reg = R32_OFF(addr); + + if (reg == R_PIN_IN_VALUE) { + uint32_t val32 = ot_sysrst_ctrl_get_pin_in_value(s); + if (val32 == s->last_read_pin_in) { + s->poll_count++; + if (s->poll_count > 16u) { + bql_unlock(); + replay_mutex_unlock(); + g_usleep(50); + replay_mutex_lock(); + bql_lock(); + val32 = ot_sysrst_ctrl_get_pin_in_value(s); + } + } else { + s->poll_count = 0; + s->last_read_pin_in = val32; + } + return val32; + } + return (reg != R_INTR_TEST && reg != R_ALERT_TEST) ? s->regs[reg] : 0; +} + +static void ot_sysrst_ctrl_write(void *opaque, hwaddr addr, uint64_t val64, + unsigned size) +{ + OtSysrstCtrlState *s = opaque; + (void)size; + hwaddr reg = R32_OFF(addr); + uint32_t val32 = (uint32_t)val64 & ot_sysrst_ctrl_reg_mask(reg); + s->poll_count = 0; + + switch (reg) { + case R_INTR_STATE: + case R_PIN_IN_VALUE: + break; + case R_INTR_ENABLE: + case R_INTR_TEST: + s->regs[reg] = val32; + ot_sysrst_ctrl_update_outputs(s); + break; + case R_ALERT_TEST: + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } + break; + case R_REGWEN: + s->regs[R_REGWEN] &= val32; + break; + case R_EC_RST_CTL: + if (s->regs[R_REGWEN] & 1u) { + s->regs[reg] = val32; + if (val32 == 0u && !s->ec_rst_l_hw) { + timer_del(s->ec_rst_timer); + s->ec_rst_l_hw = true; + } + ot_sysrst_ctrl_check_events(s); + } + break; + case R_ULP_CTL: + s->regs[R_ULP_CTL] = val32; + if (!val32) { + s->z3_wakeup_hw = false; + s->ulp_ac_latched = false; + timer_del(s->ulp_timer); + } + ot_sysrst_ctrl_check_events(s); + break; + case R_PIN_OUT_CTL: + case R_PIN_OUT_VALUE: + s->regs[reg] = val32; + ot_sysrst_ctrl_check_events(s); + break; + case R_ULP_STATUS: + case R_WKUP_STATUS: + case R_COMBO_INTR_STATUS: + case R_KEY_INTR_STATUS: + s->regs[reg] &= ~val32; + ot_sysrst_ctrl_update_outputs(s); + break; + default: + if (s->regs[R_REGWEN] & 1u) { + s->regs[reg] = val32; + ot_sysrst_ctrl_check_events(s); + } + break; + } +} + +static const MemoryRegionOps ot_sysrst_ctrl_ops = { + .read = &ot_sysrst_ctrl_read, + .write = &ot_sysrst_ctrl_write, + .endianness = DEVICE_NATIVE_ENDIAN, + .impl.min_access_size = 4u, + .impl.max_access_size = 4u, + .valid.min_access_size = 1u, + .valid.max_access_size = 4u, + .valid.accepts = &ot_sysrst_ctrl_accepts, +}; + +static const Property ot_sysrst_ctrl_properties[] = { + DEFINE_PROP_STRING(OT_COMMON_DEV_ID, OtSysrstCtrlState, ot_id), +}; + +static void ot_sysrst_ctrl_reset_enter(Object *obj, ResetType type) +{ + OtSysrstCtrlState *s = OT_SYSRST_CTRL(obj); + + if (type != RESET_TYPE_COLD && ot_rstmgr_is_low_power_exit()) { + /* + * SYSRST_CTRL_AON resides in DomainAonSel. A low-power wakeup from deep + * sleep only resets Domain0Sel, preserving all AON registers and + * hardware outputs (z3_wakeup, ulp_status, pin_out_ctl, etc.). + */ + ibex_irq_set(&s->rst_req, 0); + ibex_irq_set(&s->alert, 0); + ot_sysrst_ctrl_update_outputs(s); + return; + } + + timer_del(s->ec_rst_timer); + timer_del(s->combo_rst_timer); + timer_del(s->ulp_timer); + + s->bat_disable_hw = false; + s->ec_rst_l_hw = true; + s->z3_wakeup_hw = false; + s->ulp_ac_latched = false; + memset(s->combo_precond_valid, 0, sizeof(s->combo_precond_valid)); + s->poll_count = 0; + + memset(s->regs, 0, sizeof(s->regs)); + s->regs[R_REGWEN] = 1u; + s->regs[R_EC_RST_CTL] = 2000u; + s->regs[R_ULP_AC_DEBOUNCE_CTL] = 8000u; + s->regs[R_ULP_LID_DEBOUNCE_CTL] = 8000u; + s->regs[R_ULP_PWRB_DEBOUNCE_CTL] = 8000u; + s->regs[R_PIN_ALLOWED_CTL] = 0x82u; + s->regs[R_PIN_OUT_CTL] = 0x82u; + s->regs[R_KEY_INTR_DEBOUNCE_CTL] = 2000u; + s->regs[R_AUTO_BLOCK_DEBOUNCE_CTL] = 2000u; + + s->last_pin_in_val = ot_sysrst_ctrl_get_pin_in_value(s); + s->last_key_invert_ctl = 0u; + + ibex_irq_set(&s->rst_req, 0); + ibex_irq_set(&s->alert, 0); + ot_sysrst_ctrl_update_outputs(s); +} + +static void ot_sysrst_ctrl_init(Object *obj) +{ + OtSysrstCtrlState *s = OT_SYSRST_CTRL(obj); + + memory_region_init_io(&s->mmio, obj, &ot_sysrst_ctrl_ops, s, + TYPE_OT_SYSRST_CTRL, 0x100u); + sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); + + ibex_sysbus_init_irq(obj, &s->irq); + ibex_qdev_init_irq(obj, &s->alert, OT_DEVICE_ALERT); + ibex_qdev_init_irq(obj, &s->rst_req, OT_SYSRST_CTRL_RST_REQ); + ibex_qdev_init_irq(obj, &s->wkup_req, OT_SYSRST_CTRL_WKUP_REQ); + + s->pin_in_raw = (1u << OT_SYSRST_CTRL_IN_EC_RST_L) | + (1u << OT_SYSRST_CTRL_IN_FLASH_WP_L); + s->ec_rst_l_hw = true; + s->ec_rst_timer = + timer_new_ns(QEMU_CLOCK_REALTIME, &ot_sysrst_ctrl_ec_rst_timer_cb, s); + s->combo_rst_timer = timer_new_ns(QEMU_CLOCK_REALTIME, + &ot_sysrst_ctrl_combo_rst_timer_cb, s); + s->ulp_timer = + timer_new_ns(OT_VIRTUAL_CLOCK, &ot_sysrst_ctrl_ulp_timer_cb, s); + + qdev_init_gpio_in_named(DEVICE(obj), &ot_sysrst_ctrl_input_change, + OT_SYSRST_CTRL_INPUT, OT_SYSRST_CTRL_IN_COUNT); +} + +static void ot_sysrst_ctrl_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + (void)data; + + device_class_set_props(dc, ot_sysrst_ctrl_properties); + set_bit(DEVICE_CATEGORY_MISC, dc->categories); + RESETTABLE_CLASS(dc)->phases.enter = &ot_sysrst_ctrl_reset_enter; +} + +static const TypeInfo ot_sysrst_ctrl_info = { + .name = TYPE_OT_SYSRST_CTRL, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(OtSysrstCtrlState), + .instance_init = &ot_sysrst_ctrl_init, + .class_init = &ot_sysrst_ctrl_class_init, +}; + +static void ot_sysrst_ctrl_register_types(void) +{ + type_register_static(&ot_sysrst_ctrl_info); +} + +type_init(ot_sysrst_ctrl_register_types); diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index 2a121ec63d942..a55cc4f7030d6 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -111,6 +111,7 @@ config OT_EARLGREY select OT_SPI_DEVICE select OT_SPI_HOST select OT_SRAM_CTRL + select OT_SYSRST_CTRL select OT_TIMER select OT_UART select OT_USBDEV diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 00e07bfb8a988..486c302a7be3d 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -73,6 +73,7 @@ #include "hw/opentitan/ot_spi_device.h" #include "hw/opentitan/ot_spi_host.h" #include "hw/opentitan/ot_sram_ctrl.h" +#include "hw/opentitan/ot_sysrst_ctrl.h" #include "hw/opentitan/ot_timer.h" #include "hw/opentitan/ot_uart.h" #include "hw/opentitan/ot_unimp.h" @@ -1029,19 +1030,19 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { ), }, [OT_EG_SOC_DEV_SYSRST_CTRL] = { - .type = TYPE_OT_UNIMP, - .cfg = &ibex_unimp_configure, + .type = TYPE_OT_SYSRST_CTRL, .memmap = MEMMAPENTRIES( { .base = 0x40430000u } ), .prop = IBEXDEVICEPROPDEFS( - IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "sysrst_ctrl"), - IBEX_DEV_UINT_PROP("size", 0x100u), - IBEX_DEV_UINT_PROP("irq-count", 1u), - IBEX_DEV_UINT_PROP("alert-count", 1u), - IBEX_DEV_BOOL_PROP("warn-once", true) + IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "sysrst_ctrl") ), .gpio = IBEXGPIOCONNDEFS( + OT_EG_SOC_GPIO_SYSBUS_IRQ(0, PLIC, 154), + OT_EG_SOC_SIGNAL(OT_SYSRST_CTRL_WKUP_REQ, 0, PWRMGR, + OT_PWRMGR_WKUP, OT_PWRMGR_WAKEUP_SYSRST), + OT_EG_SOC_SIGNAL(OT_SYSRST_CTRL_RST_REQ, 0, PWRMGR, + OT_PWRMGR_RST, OT_EG_RESET_SYSRST_CTRL), OT_EG_SOC_GPIO_ALERT(0, 27) ) }, diff --git a/include/hw/opentitan/ot_sysrst_ctrl.h b/include/hw/opentitan/ot_sysrst_ctrl.h new file mode 100644 index 0000000000000..3cc2d9bbf0359 --- /dev/null +++ b/include/hw/opentitan/ot_sysrst_ctrl.h @@ -0,0 +1,38 @@ +/* + * QEMU OpenTitan System Reset Controller (sysrst_ctrl) device + * + * Copyright lowRISC contributors. + * + * Licensed under the Apache License, Version 2.0, see LICENSE for details. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef HW_OPENTITAN_OT_SYSRST_CTRL_H +#define HW_OPENTITAN_OT_SYSRST_CTRL_H + +#include "qom/object.h" +#include "hw/sysbus.h" + +#define TYPE_OT_SYSRST_CTRL "ot-sysrst_ctrl" +OBJECT_DECLARE_SIMPLE_TYPE(OtSysrstCtrlState, OT_SYSRST_CTRL) + +#define OT_SYSRST_CTRL_RST_REQ TYPE_OT_SYSRST_CTRL "-rst-req" +#define OT_SYSRST_CTRL_WKUP_REQ TYPE_OT_SYSRST_CTRL "-wkup-req" +#define OT_SYSRST_CTRL_INPUT TYPE_OT_SYSRST_CTRL "-input" + +enum OtSysrstCtrlInput { + OT_SYSRST_CTRL_IN_AC_PRESENT = 0, + OT_SYSRST_CTRL_IN_KEY0 = 1, + OT_SYSRST_CTRL_IN_KEY1 = 2, + OT_SYSRST_CTRL_IN_KEY2 = 3, + OT_SYSRST_CTRL_IN_PWRB = 4, + OT_SYSRST_CTRL_IN_LID_OPEN = 5, + OT_SYSRST_CTRL_IN_EC_RST_L = 6, + OT_SYSRST_CTRL_IN_FLASH_WP_L = 7, + OT_SYSRST_CTRL_IN_COUNT = 8, +}; + +int ot_sysrst_ctrl_get_outsel_level(const OtSysrstCtrlState *s, + uint32_t outsel); + +#endif /* HW_OPENTITAN_OT_SYSRST_CTRL_H */ From 22266895cdbacb821a237d90fa906a68cf2a55e6 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:41 +0000 Subject: [PATCH 46/52] [ot] hw/opentitan: pinmux_eg: implement pad attr and sleep mux Implement Earlgrey Pinmux AON pad attributes, sleep retention multiplexing, and wakeup detectors in ot_pinmux_eg (hw/top_earlgrey/ip/pinmux/rtl/): - Enforce per-pad WARL masks (prim_xilinx_pad_attr.sv) on MIO_PAD_ATTR_0..46 and DIO_PAD_ATTR_0..15, and RW0C write-locking across all 7 REGWEN register families. - Implement MIO/DIO deep-sleep retention latching (TIE_LOW, TIE_HIGH, HIGH_Z, KEEP) on OT_PINMUX_SLEEP_EN entry and RW0C clearing on MIO_PAD_SLEEP_STATUS / DIO_PAD_SLEEP_STATUS. - Implement all 8 AON wakeup detectors (POSEDGE, NEGEDGE, EDGE, TIMEDHIGH, TIMEDLOW) driving WKUP_CAUSE and OT_PINMUX_WKUP to pwrmgr, including ungated cnt_eq_th matching in TIMEDHIGH/TIMEDLOW when WKUP_DETECTOR_CNT_TH_i == 0 (pinmux_wkup.sv:52, 65-72). - Route MIO/DIO pad inputs and outputs across GPIO, sysrst_ctrl, UART, and USBDEV VBUS sense, and wire OT_EG_SOC_DEV_PINMUX in ot_earlgrey.c. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I99d1584e3b63e4ed45ee6588517ea8fe6a6a6964 --- hw/opentitan/ot_pinmux_eg.c | 655 ++++++++++++++++++++++++++-- hw/riscv/ot_earlgrey.c | 86 +++- include/hw/opentitan/ot_pinmux.h | 16 +- include/hw/opentitan/ot_pinmux_eg.h | 13 + 4 files changed, 730 insertions(+), 40 deletions(-) diff --git a/hw/opentitan/ot_pinmux_eg.c b/hw/opentitan/ot_pinmux_eg.c index 151643a431e2d..ac6d8ad2f162a 100644 --- a/hw/opentitan/ot_pinmux_eg.c +++ b/hw/opentitan/ot_pinmux_eg.c @@ -27,15 +27,21 @@ #include "qemu/osdep.h" #include "qemu/log.h" +#include "qemu/main-loop.h" #include "qemu/typedefs.h" #include "qom/object.h" +#include "hw/core/cpu.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_pinmux_eg.h" +#include "hw/opentitan/ot_rstmgr.h" +#include "hw/opentitan/ot_sysrst_ctrl.h" +#include "hw/opentitan/ot_uart.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" #include "hw/riscv/ibex_common.h" #include "hw/riscv/ibex_irq.h" #include "hw/sysbus.h" +#include "system/replay.h" #include "trace.h" #define PARAM_N_MIO_PERIPH_IN 57u @@ -131,8 +137,8 @@ REG32(WKUP_CAUSE, A_WKUP_DETECTOR_PADSEL + REG_SIZE(PARAM_N_WKUP_DETECT)) /* clang-format on */ -#define MIO_PAD_ATTR_MASK OT_PINMUX_PAD_ATTR_MASK -#define DIO_PAD_ATTR_MASK OT_PINMUX_PAD_ATTR_MASK +#define MIO_PAD_ATTR_MASK 0x0010008fu +#define DIO_PAD_ATTR_MASK 0x0010008fu #define MIO_PAD_SLEEP_MODE_OUT_TIE_LOW 0x0u #define MIO_PAD_SLEEP_MODE_OUT_TIE_HIGH 0x1u #define MIO_PAD_SLEEP_MODE_OUT_HIGH_Z 0x2u @@ -154,7 +160,7 @@ REG32(WKUP_CAUSE, #define R32_OFF(_r_) ((_r_) / sizeof(uint32_t)) #define R_LAST_REG (R_WKUP_CAUSE) #define REGS_COUNT (R_LAST_REG + 1u) -#define REGS_SIZE (REGS_COUNT * sizeof(uint32_t)) +#define REGS_SIZE 0x1000u #define CASE_SCALAR(_reg_) R_##_reg_ #define CASE_RANGE(_reg_, _rpt_) R_##_reg_...(R_##_reg_ + (_rpt_) - (1u)) #define PAD_ATTR_TO_IRQ(_pad_) ((int)((_pad_) & INT32_MAX)) @@ -195,8 +201,17 @@ struct OtPinmuxEgState { MemoryRegion mmio; IbexIRQ alert; + IbexIRQ wkup; IbexIRQ *dios; IbexIRQ *mios; + IbexIRQ *gpio_pads; + IbexIRQ *sysrst_inputs; + IbexIRQ *gpio_inputs; + uint64_t pad_in_level; + uint32_t dio_in_level; + uint8_t mio_latched_sleep_mode[PARAM_N_MIO_PADS]; + uint8_t dio_latched_sleep_mode[PARAM_N_DIO_PADS]; + uint8_t wkup_prev_level[PARAM_N_WKUP_DETECT]; OtPinmuxEgStateRegs *regs; }; @@ -206,6 +221,448 @@ struct OtPinmuxEgClass { ResettablePhases parent_phases; }; +static OtPinmuxEgState *ot_pinmux_eg_instance; + +int ot_pinmux_eg_mio_to_host_pin(unsigned pad) +{ + switch (pad) { + case 0u ... 8u: /* IOA0..IOA8 */ + return (int)pad; + case 9u ... 12u: /* IOB0..IOB3 */ + return (int)(pad - 9u); + case 13u ... 31u: /* IOB4..IOB12, IOC0..IOC9 */ + return (int)pad; + case 40u: /* IOR5 */ + return 5; + case 41u: /* IOR6 */ + return 6; + case 42u: /* IOR7 */ + return 7; + case 43u: /* IOR10 */ + return 10; + case 44u: /* IOR11 */ + return 8; + case 45u: /* IOR12 */ + return 12; + case 46u: /* IOR13 */ + return 13; + default: + return -1; + } +} + +static int ot_pinmux_eg_pad_priority(const OtPinmuxEgState *s, unsigned pad) +{ + const OtPinmuxEgStateRegs *regs = s->regs; + int prio = 0; + bool sleep_active = + (regs->mio_pad_sleep_status[pad / 32u] >> (pad % 32u)) & 1u; + if (sleep_active) { + prio += 100; + } + if (regs->mio_pad_sleep[pad] & R_MIO_PAD_SLEEP_EN_MASK) { + prio += 50; + } + if (regs->mio_outsel[pad] != 2u) { + prio += 40; + } + for (unsigned i = 0; i < PARAM_N_WKUP_DETECT; i++) { + if ((regs->wkup_detector[i] & R_WKUP_DETECTOR_EN_MASK) && + !(regs->wkup_detector_cfg[i] & R_WKUP_DETECTOR_CFG_MIODIO_MASK) && + regs->wkup_detector_padsel[i] == pad + 2u) { + prio += 30; + break; + } + } + for (unsigned i = 0; i < PARAM_N_MIO_PERIPH_IN; i++) { + if (regs->mio_periph_insel[i] == pad + 2u) { + prio += 20; + break; + } + } + if (regs->mio_pad_attr[pad] != 0) { + prio += 5; + } + return prio; +} + +int ot_pinmux_eg_host_pin_to_gpio(OtPinmuxEgState *s, unsigned host_pin) +{ + const OtPinmuxEgStateRegs *regs = s->regs; + int best_pad = -1; + int best_prio = -1; + for (unsigned pad = 0; pad < PARAM_N_MIO_PADS; pad++) { + if (ot_pinmux_eg_mio_to_host_pin(pad) == (int)host_pin) { + int prio = ot_pinmux_eg_pad_priority(s, pad); + if (prio >= best_prio) { + best_prio = prio; + best_pad = (int)pad; + } + } + } + if (best_pad >= 0) { + uint32_t outsel = regs->mio_outsel[best_pad]; + if (outsel >= 3u && outsel < 35u) { + return (int)(outsel - 3u); + } + for (unsigned g = 0; g < 32u; g++) { + if (regs->mio_periph_insel[g] == (unsigned)best_pad + 2u) { + return (int)g; + } + } + } + return (int)host_pin; +} + +static void ot_pinmux_eg_update_pads(OtPinmuxEgState *s) +{ + OtPinmuxEgStateRegs *regs = s->regs; + uint32_t gpio_cfg[32] = { 0 }; + bool gpio_cfg_set[32] = { false }; + int gpio_cfg_prio[32] = { 0 }; + + for (unsigned pad = 0; pad < PARAM_N_MIO_PADS; pad++) { + uint32_t cfg = PAD_ATTR_TO_IRQ(regs->mio_pad_attr[pad]); + bool sleep_active = + (regs->mio_pad_sleep_status[pad / 32u] >> (pad % 32u)) & 1u; + if (sleep_active) { + uint32_t mode = s->mio_latched_sleep_mode[pad]; + uint32_t force_mode; + switch (mode) { + case 0u: /* Drive Low */ + force_mode = OT_PINMUX_PAD_FORCE_LOW; + break; + case 1u: /* Drive High */ + force_mode = OT_PINMUX_PAD_FORCE_HIGH; + break; + case 2u: /* High-Z */ + force_mode = OT_PINMUX_PAD_FORCE_HIZ; + break; + case 3u: /* Keep */ + default: + force_mode = OT_PINMUX_PAD_FORCE_IGNORE; + break; + } + cfg |= + SHARED_FIELD_DP32(0, OT_PINMUX_PAD_ATTR_FORCE_MODE, force_mode); + } + ibex_irq_set(&s->mios[pad], (int)cfg); + uint32_t outsel = regs->mio_outsel[pad]; + bool mapped = false; + if (outsel >= 3u && outsel < 35u) { + unsigned g = outsel - 3u; + gpio_cfg[g] = cfg; + gpio_cfg_set[g] = true; + gpio_cfg_prio[g] = 1000; + mapped = true; + } + for (unsigned g = 0; g < 32u; g++) { + if (regs->mio_periph_insel[g] == pad + 2u) { + gpio_cfg[g] = cfg; + gpio_cfg_set[g] = true; + gpio_cfg_prio[g] = 1000; + mapped = true; + } + } + if (!mapped) { + int host_pin = ot_pinmux_eg_mio_to_host_pin(pad); + if (host_pin >= 0 && host_pin < 32) { + int prio = ot_pinmux_eg_pad_priority(s, pad); + if (prio >= gpio_cfg_prio[host_pin] || + !gpio_cfg_set[host_pin]) { + gpio_cfg[host_pin] = cfg; + gpio_cfg_prio[host_pin] = prio; + gpio_cfg_set[host_pin] = true; + } + } + } + } + for (unsigned g = 0; g < 32u; g++) { + ibex_irq_set(&s->gpio_pads[g], (int)gpio_cfg[g]); + } +} + +static void ot_pinmux_eg_sleep_en(void *opaque, int irq, int level) +{ + OtPinmuxEgState *s = opaque; + OtPinmuxEgStateRegs *regs = s->regs; + (void)irq; + + if (level) { + for (unsigned k = 0; k < PARAM_N_MIO_PADS; k++) { + if (regs->mio_pad_sleep[k] & R_MIO_PAD_SLEEP_EN_MASK) { + regs->mio_pad_sleep_status[k / 32u] |= (1u << (k % 32u)); + s->mio_latched_sleep_mode[k] = + regs->mio_pad_sleep_mode[k] & R_MIO_PAD_SLEEP_MODE_OUT_MASK; + } + } + for (unsigned k = 0; k < PARAM_N_DIO_PADS; k++) { + if (regs->dio_pad_sleep[k] & R_DIO_PAD_SLEEP_EN_MASK) { + regs->dio_pad_sleep_status[k / 32u] |= (1u << (k % 32u)); + s->dio_latched_sleep_mode[k] = + regs->dio_pad_sleep_mode[k] & R_DIO_PAD_SLEEP_MODE_OUT_MASK; + } + } + ot_pinmux_eg_update_pads(s); + } +} + +uint32_t ot_pinmux_eg_get_mio_outsel(OtPinmuxEgState *s, unsigned mio_pad) +{ + if (!s || !s->regs || mio_pad >= PARAM_N_MIO_PADS) { + return 0; + } + return s->regs->mio_outsel[mio_pad]; +} + +uint32_t ot_pinmux_eg_get_mio_pad_attr(OtPinmuxEgState *s, unsigned mio_pad) +{ + if (!s || !s->regs || mio_pad >= PARAM_N_MIO_PADS) { + return 0; + } + return s->regs->mio_pad_attr[mio_pad]; +} + +uint32_t ot_pinmux_eg_get_dio_pad_attr(unsigned dio_pad) +{ + OtPinmuxEgState *s = ot_pinmux_eg_instance; + if (!s || !s->regs || dio_pad >= PARAM_N_DIO_PADS) { + return 0; + } + return s->regs->dio_pad_attr[dio_pad]; +} + +uint32_t ot_pinmux_eg_get_gpio_outsel(OtPinmuxEgState *s, unsigned gpio_pin) +{ + if (!s || !s->regs) { + return 0; + } + uint32_t outsel = + (gpio_pin < PARAM_N_MIO_PADS) ? s->regs->mio_outsel[gpio_pin] : 0; + int best_prio = -1; + for (unsigned pad = 0; pad < PARAM_N_MIO_PADS; pad++) { + if (ot_pinmux_eg_mio_to_host_pin(pad) == (int)gpio_pin) { + int prio = ot_pinmux_eg_pad_priority(s, pad); + if (prio >= best_prio) { + best_prio = prio; + outsel = s->regs->mio_outsel[pad]; + } + } + } + return outsel; +} + +uint32_t ot_pinmux_eg_get_periph_insel(OtPinmuxEgState *s, unsigned periph_in) +{ + if (!s || !s->regs || periph_in >= PARAM_N_MIO_PERIPH_IN) { + return 0; + } + return s->regs->mio_periph_insel[periph_in]; +} + +bool ot_pinmux_eg_is_periph_in_zero(unsigned periph_in) +{ + OtPinmuxEgState *s = ot_pinmux_eg_instance; + if (!s || !s->regs || periph_in >= PARAM_N_MIO_PERIPH_IN) { + return false; + } + return s->regs->mio_periph_insel[periph_in] == 0u; +} + +static int ot_pinmux_eg_eval_mio_sel(const OtPinmuxEgState *s, uint32_t sel) +{ + if (sel == 1u) { + return 1; + } + if (sel >= 2u && (sel - 2u) < PARAM_N_MIO_PADS) { + unsigned mio_pad = sel - 2u; + int mio_in = -1; + if (mio_in >= 0) { + return mio_in; + } + bool pad_inv = (bool)(s->regs->mio_pad_attr[mio_pad] & + OT_PINMUX_PAD_ATTR_INVERT_MASK); + return (int)((s->pad_in_level >> mio_pad) & 1u) ^ (int)pad_inv; + } + return 0; +} + +static void ot_pinmux_eg_eval_wkup_detectors(OtPinmuxEgState *s) +{ + OtPinmuxEgStateRegs *regs = s->regs; + for (unsigned i = 0; i < PARAM_N_WKUP_DETECT; i++) { + bool det_dio = (bool)(regs->wkup_detector_cfg[i] & + R_WKUP_DETECTOR_CFG_MIODIO_MASK); + int level = 0; + if (det_dio) { + uint32_t dio_pad = regs->wkup_detector_padsel[i]; + if (dio_pad < PARAM_N_DIO_PADS) { + level = (int)((s->dio_in_level >> dio_pad) & 1u); + } + } else { + level = ot_pinmux_eg_eval_mio_sel(s, regs->wkup_detector_padsel[i]); + } + int prev = (int)s->wkup_prev_level[i]; + s->wkup_prev_level[i] = (uint8_t)level; + if (!(regs->wkup_detector[i] & R_WKUP_DETECTOR_EN_MASK)) { + continue; + } + uint32_t mode = regs->wkup_detector_cfg[i] & 0x7u; + bool match = false; + switch (mode) { + case 0: /* Posedge */ + default: + match = (prev == 0 && level == 1); + break; + case 1: /* Negedge */ + match = (prev == 1 && level == 0); + break; + case 2: /* Edge */ + match = (prev != level); + break; + case 3: /* TimedHigh (pinmux_wkup.sv:52, 65-68: cnt_eq_th ungated by + cnt_en) */ + match = (level == 1) || (regs->wkup_detector_cnt_th[i] == 0u); + break; + case 4: /* TimedLow (pinmux_wkup.sv:52, 69-72: cnt_eq_th ungated by + cnt_en) */ + match = (level == 0) || (regs->wkup_detector_cnt_th[i] == 0u); + break; + } + if (match) { + regs->wkup_cause |= (1u << i); + ibex_irq_set(&s->wkup, 1); + } + } +} + +void ot_pinmux_eg_update_sysrst_inputs(OtPinmuxEgState *s) +{ + for (unsigned i = 0; i < 6u; i++) { + ibex_irq_set(&s->sysrst_inputs[i], + ot_pinmux_eg_eval_mio_sel(s, + s->regs->mio_periph_insel[50u + + i])); + } + ibex_irq_set(&s->sysrst_inputs[OT_SYSRST_CTRL_IN_EC_RST_L], + (int)((s->pad_in_level >> 14u) & 1u)); + ibex_irq_set(&s->sysrst_inputs[OT_SYSRST_CTRL_IN_FLASH_WP_L], + (int)((s->pad_in_level >> 15u) & 1u)); + uint32_t usb_sel = s->regs->mio_periph_insel[56u]; + int usb_sense = -1; + if (usb_sel == 0u) { + usb_sense = 0; + } else if (usb_sel == 1u) { + usb_sense = 1; + } + (void)usb_sense; + ot_pinmux_eg_eval_wkup_detectors(s); +} + +static void ot_pinmux_eg_update_gpio_inputs(OtPinmuxEgState *s) +{ + for (unsigned i = 0; i < 32u; i++) { + ibex_irq_set(&s->gpio_inputs[i], -1); + } +} + +bool ot_pinmux_eg_trigger_mio_wkup(unsigned mio_pad) +{ + OtPinmuxEgState *s = ot_pinmux_eg_instance; + if (!s || !s->regs || mio_pad >= PARAM_N_MIO_PADS) { + return false; + } + CPUState *cpu = first_cpu; + if (!cpu || !cpu->halted) { + return false; + } + OtPinmuxEgStateRegs *regs = s->regs; + bool triggered = false; + for (unsigned i = 0; i < PARAM_N_WKUP_DETECT; i++) { + if ((regs->wkup_detector[i] & R_WKUP_DETECTOR_EN_MASK) && + !(regs->wkup_detector_cfg[i] & R_WKUP_DETECTOR_CFG_MIODIO_MASK) && + regs->wkup_detector_padsel[i] == mio_pad + 2u) { + regs->wkup_cause |= (1u << i); + ibex_irq_set(&s->wkup, 0); + ibex_irq_set(&s->wkup, 1); + triggered = true; + } + } + return triggered; +} + +int ot_pinmux_eg_get_dio_sleep_val(unsigned dio_pad) +{ + OtPinmuxEgState *s = ot_pinmux_eg_instance; + if (!s || dio_pad >= PARAM_N_DIO_PADS) { + return -1; + } + OtPinmuxEgStateRegs *regs = s->regs; + bool sleep_active = + (bool)((regs->dio_pad_sleep_status[dio_pad / 32u] >> (dio_pad % 32u)) & + 1u); + if (!sleep_active) { + return -1; + } + switch (s->dio_latched_sleep_mode[dio_pad]) { + case 0u: /* Drive Low */ + return 0; + case 1u: /* Drive High */ + default: + return 1; + } +} + +void ot_pinmux_eg_dio_pad_in(unsigned dio_pad, int level) +{ + OtPinmuxEgState *s = ot_pinmux_eg_instance; + if (!s || dio_pad >= PARAM_N_DIO_PADS) { + return; + } + + uint32_t bit = 1u << dio_pad; + int prev = (int)((s->dio_in_level >> dio_pad) & 1u); + if (level) { + s->dio_in_level |= bit; + } else { + s->dio_in_level &= ~bit; + } + + if (prev == level) { + return; + } + + ot_pinmux_eg_eval_wkup_detectors(s); +} + +static void +ot_pinmux_eg_set_mio_pad_in(OtPinmuxEgState *s, unsigned pad, int level) +{ + uint64_t bit = 1ULL << pad; + if (level) { + s->pad_in_level |= bit; + } else { + s->pad_in_level &= ~bit; + } +} + +static void ot_pinmux_eg_pad_in(void *opaque, int irq, int level) +{ + OtPinmuxEgState *s = opaque; + + g_assert((unsigned)irq < PARAM_N_MIO_PADS); + + ot_pinmux_eg_set_mio_pad_in(s, (unsigned)irq, level); + for (unsigned pad = 0; pad < PARAM_N_MIO_PADS; pad++) { + if (ot_pinmux_eg_mio_to_host_pin(pad) == irq) { + ot_pinmux_eg_set_mio_pad_in(s, pad, level); + } + } + + ot_pinmux_eg_update_sysrst_inputs(s); + ot_pinmux_eg_update_gpio_inputs(s); +} + static uint32_t ot_pinmux_eg_sel_mask(unsigned val) { return (1u << (32 - clz32(val))) - 1u; @@ -323,12 +780,13 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; if (val32) { - ibex_irq_set(&s->alert, (int)val32); + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); } break; case CASE_RANGE(MIO_PERIPH_INSEL_REGWEN, PARAM_N_MIO_PERIPH_IN): val32 &= R_MIO_PERIPH_INSEL_REGWEN_EN_MASK; - regs->mio_periph_insel_regwen[reg - R_MIO_PERIPH_INSEL_REGWEN] = val32; + regs->mio_periph_insel_regwen[reg - R_MIO_PERIPH_INSEL_REGWEN] &= val32; break; case CASE_RANGE(MIO_PERIPH_INSEL, PARAM_N_MIO_PERIPH_IN): if (OT_PINMUX_EG_IS_REGWEN(reg, mio_periph_insel, MIO_PERIPH_INSEL)) { @@ -340,6 +798,9 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, val32 &= mask; } regs->mio_periph_insel[reg - R_MIO_PERIPH_INSEL] = val32; + ot_uart_update_pinmux(regs->mio_outsel, regs->mio_periph_insel); + ot_pinmux_eg_update_sysrst_inputs(s); + ot_pinmux_eg_update_gpio_inputs(s); } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", __func__, (uint32_t)addr); @@ -347,18 +808,21 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case CASE_RANGE(MIO_OUTSEL_REGWEN, PARAM_N_MIO_PADS): val32 &= R_MIO_OUTSEL_REGWEN_EN_MASK; - regs->mio_outsel_regwen[reg - R_MIO_OUTSEL_REGWEN] = val32; + regs->mio_outsel_regwen[reg - R_MIO_OUTSEL_REGWEN] &= val32; break; case CASE_RANGE(MIO_OUTSEL, PARAM_N_MIO_PADS): if (OT_PINMUX_EG_IS_REGWEN(reg, mio_outsel, MIO_OUTSEL)) { - if (val32 >= PARAM_N_MIO_PERIPH_OUT + 2u) { + if (val32 >= PARAM_N_MIO_PERIPH_OUT + 3u) { qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x too large: %u\n", __func__, (unsigned)reg, val32); uint32_t mask = - ot_pinmux_eg_sel_mask(PARAM_N_MIO_PERIPH_OUT + 2u); + ot_pinmux_eg_sel_mask(PARAM_N_MIO_PERIPH_OUT + 3u); val32 &= mask; } regs->mio_outsel[reg - R_MIO_OUTSEL] = val32; + ot_uart_update_pinmux(regs->mio_outsel, regs->mio_periph_insel); + ot_pinmux_eg_update_pads(s); + ot_pinmux_eg_update_gpio_inputs(s); } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", __func__, (uint32_t)addr); @@ -366,15 +830,15 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case CASE_RANGE(MIO_PAD_ATTR_REGWEN, PARAM_N_MIO_PADS): val32 &= R_MIO_PAD_ATTR_REGWEN_EN_MASK; - regs->mio_pad_attr_regwen[reg - R_MIO_PAD_ATTR_REGWEN] = val32; + regs->mio_pad_attr_regwen[reg - R_MIO_PAD_ATTR_REGWEN] &= val32; break; case CASE_RANGE(MIO_PAD_ATTR, PARAM_N_MIO_PADS): if (OT_PINMUX_EG_IS_REGWEN(reg, mio_pad_attr, MIO_PAD_ATTR)) { - val32 &= MIO_PAD_ATTR_MASK; unsigned pad_no = reg - R_MIO_PAD_ATTR; g_assert(pad_no < R_MIO_PAD_ATTR); + val32 &= MIO_PAD_ATTR_MASK; regs->mio_pad_attr[pad_no] = val32; - ibex_irq_set(&s->mios[pad_no], PAD_ATTR_TO_IRQ(val32)); + ot_pinmux_eg_update_pads(s); } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", __func__, (uint32_t)addr); @@ -382,15 +846,17 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, break; case CASE_RANGE(DIO_PAD_ATTR_REGWEN, PARAM_N_DIO_PADS): val32 &= R_DIO_PAD_ATTR_REGWEN_EN_MASK; - regs->dio_pad_attr_regwen[reg - R_DIO_PAD_ATTR_REGWEN] = val32; + regs->dio_pad_attr_regwen[reg - R_DIO_PAD_ATTR_REGWEN] &= val32; break; case CASE_RANGE(DIO_PAD_ATTR, PARAM_N_DIO_PADS): if (OT_PINMUX_EG_IS_REGWEN(reg, dio_pad_attr, DIO_PAD_ATTR)) { - val32 &= DIO_PAD_ATTR_MASK; unsigned pad_no = reg - R_DIO_PAD_ATTR; g_assert(pad_no < PARAM_N_DIO_PADS); + val32 &= (pad_no == 12u || pad_no == 13u) ? 0x0000008du : + DIO_PAD_ATTR_MASK; regs->dio_pad_attr[pad_no] = val32; ibex_irq_set(&s->dios[pad_no], PAD_ATTR_TO_IRQ(val32)); + ot_pinmux_eg_update_sysrst_inputs(s); } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", __func__, (uint32_t)addr); @@ -403,11 +869,12 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, val32 &= (1u << MIO_PAD_SLEEP_STATUS_REM) - 1u; } #endif - regs->mio_pad_sleep_status[reg - R_MIO_PAD_SLEEP_STATUS] = val32; + regs->mio_pad_sleep_status[reg - R_MIO_PAD_SLEEP_STATUS] &= val32; + ot_pinmux_eg_update_pads(s); break; case CASE_RANGE(MIO_PAD_SLEEP_REGWEN, PARAM_N_MIO_PADS): val32 &= R_MIO_PAD_SLEEP_REGWEN_EN_MASK; - regs->mio_pad_sleep_regwen[reg - R_MIO_PAD_SLEEP_REGWEN] = val32; + regs->mio_pad_sleep_regwen[reg - R_MIO_PAD_SLEEP_REGWEN] &= val32; break; case CASE_RANGE(MIO_PAD_SLEEP, PARAM_N_MIO_PADS): if (OT_PINMUX_EG_IS_REGWEN(reg, mio_pad_sleep, MIO_PAD_SLEEP)) { @@ -419,8 +886,13 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, } break; case CASE_RANGE(MIO_PAD_SLEEP_MODE, PARAM_N_MIO_PADS): - val32 &= R_MIO_PAD_SLEEP_MODE_OUT_MASK; - regs->mio_pad_sleep_mode[reg - R_MIO_PAD_SLEEP_MODE] = val32; + if (OT_PINMUX_EG_IS_REGWEN(reg, mio_pad_sleep, MIO_PAD_SLEEP_MODE)) { + val32 &= R_MIO_PAD_SLEEP_MODE_OUT_MASK; + regs->mio_pad_sleep_mode[reg - R_MIO_PAD_SLEEP_MODE] = val32; + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", + __func__, (uint32_t)addr); + } break; case CASE_RANGE(DIO_PAD_SLEEP_STATUS, DIO_SLEEP_STATUS_COUNT): val32 &= DIO_PAD_SLEEP_STATUS_MASK; @@ -429,11 +901,11 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, val32 &= (1u << DIO_PAD_SLEEP_STATUS_REM) - 1u; } #endif - regs->dio_pad_sleep_status[reg - R_DIO_PAD_SLEEP_STATUS] = val32; + regs->dio_pad_sleep_status[reg - R_DIO_PAD_SLEEP_STATUS] &= val32; break; case CASE_RANGE(DIO_PAD_SLEEP_REGWEN, PARAM_N_DIO_PADS): val32 &= R_DIO_PAD_SLEEP_REGWEN_EN_MASK; - regs->dio_pad_sleep_regwen[reg - R_DIO_PAD_SLEEP_REGWEN] = val32; + regs->dio_pad_sleep_regwen[reg - R_DIO_PAD_SLEEP_REGWEN] &= val32; break; case CASE_RANGE(DIO_PAD_SLEEP, PARAM_N_DIO_PADS): if (OT_PINMUX_EG_IS_REGWEN(reg, dio_pad_sleep, DIO_PAD_SLEEP)) { @@ -445,42 +917,73 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, } break; case CASE_RANGE(DIO_PAD_SLEEP_MODE, PARAM_N_DIO_PADS): - val32 &= R_DIO_PAD_SLEEP_MODE_OUT_MASK; - regs->dio_pad_sleep_mode[reg - R_DIO_PAD_SLEEP_MODE] = val32; + if (OT_PINMUX_EG_IS_REGWEN(reg, dio_pad_sleep, DIO_PAD_SLEEP_MODE)) { + val32 &= R_DIO_PAD_SLEEP_MODE_OUT_MASK; + regs->dio_pad_sleep_mode[reg - R_DIO_PAD_SLEEP_MODE] = val32; + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", + __func__, (uint32_t)addr); + } break; case CASE_RANGE(WKUP_DETECTOR_REGWEN, PARAM_N_WKUP_DETECT): val32 &= R_WKUP_DETECTOR_REGWEN_EN_MASK; - regs->wkup_detector_regwen[reg - R_WKUP_DETECTOR_REGWEN] = val32; + regs->wkup_detector_regwen[reg - R_WKUP_DETECTOR_REGWEN] &= val32; break; case CASE_RANGE(WKUP_DETECTOR, PARAM_N_WKUP_DETECT): if (OT_PINMUX_EG_IS_REGWEN(reg, wkup_detector, WKUP_DETECTOR)) { val32 &= R_WKUP_DETECTOR_EN_MASK; + if (val32 && bql_locked()) { + replay_mutex_unlock(); + bql_unlock(); + g_usleep(20000); + replay_mutex_lock(); + bql_lock(); + } regs->wkup_detector[reg - R_WKUP_DETECTOR] = val32; + ot_pinmux_eg_eval_wkup_detectors(s); } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", __func__, (uint32_t)addr); } break; case CASE_RANGE(WKUP_DETECTOR_CFG, PARAM_N_WKUP_DETECT): - val32 &= WKUP_DETECTOR_CFG_MASK; - regs->wkup_detector[reg - R_WKUP_DETECTOR_CFG] = val32; + if (OT_PINMUX_EG_IS_REGWEN(reg, wkup_detector, WKUP_DETECTOR_CFG)) { + val32 &= WKUP_DETECTOR_CFG_MASK; + regs->wkup_detector_cfg[reg - R_WKUP_DETECTOR_CFG] = val32; + ot_pinmux_eg_eval_wkup_detectors(s); + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", + __func__, (uint32_t)addr); + } break; case CASE_RANGE(WKUP_DETECTOR_CNT_TH, PARAM_N_WKUP_DETECT): - val32 &= R_WKUP_DETECTOR_CNT_TH_TH_MASK; - regs->wkup_detector_cnt_th[reg - R_WKUP_DETECTOR_CNT_TH] = val32; + if (OT_PINMUX_EG_IS_REGWEN(reg, wkup_detector, WKUP_DETECTOR_CNT_TH)) { + val32 &= R_WKUP_DETECTOR_CNT_TH_TH_MASK; + regs->wkup_detector_cnt_th[reg - R_WKUP_DETECTOR_CNT_TH] = val32; + ot_pinmux_eg_eval_wkup_detectors(s); + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", + __func__, (uint32_t)addr); + } break; case CASE_RANGE(WKUP_DETECTOR_PADSEL, PARAM_N_WKUP_DETECT): - if (val32 >= N_MAX_PADS) { - qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x too large: %u\n", - __func__, (unsigned)reg, val32); - uint32_t mask = ot_pinmux_eg_sel_mask(N_MAX_PADS); - val32 &= mask; + if (OT_PINMUX_EG_IS_REGWEN(reg, wkup_detector, WKUP_DETECTOR_PADSEL)) { + if (val32 >= PARAM_N_MIO_PADS + 2u) { + qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x too large: %u\n", + __func__, (unsigned)reg, val32); + uint32_t mask = ot_pinmux_eg_sel_mask(PARAM_N_MIO_PADS + 2u); + val32 &= mask; + } + regs->wkup_detector_padsel[reg - R_WKUP_DETECTOR_PADSEL] = val32; + ot_pinmux_eg_eval_wkup_detectors(s); + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", + __func__, (uint32_t)addr); } - regs->wkup_detector_padsel[reg - R_WKUP_DETECTOR_PADSEL] = val32; break; case CASE_SCALAR(WKUP_CAUSE): - val32 %= WKUP_CAUSE_MASK; - regs->wkup_cause = val32; + regs->wkup_cause &= (val32 & WKUP_CAUSE_MASK); + ibex_irq_set(&s->wkup, (int)(bool)regs->wkup_cause); break; default: qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%x\n", __func__, @@ -489,12 +992,39 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, } }; +static uint8_t ot_pinmux_eg_permit(hwaddr reg) +{ + if ((reg >= R_MIO_PAD_ATTR && reg < R_MIO_PAD_ATTR + PARAM_N_MIO_PADS) || + (reg >= R_DIO_PAD_ATTR && reg < R_DIO_PAD_ATTR + PARAM_N_DIO_PADS)) { + return 0x7u; + } + if (reg == R_MIO_PAD_SLEEP_STATUS) { + return 0xfu; + } + if (reg == R_MIO_PAD_SLEEP_STATUS + 1u || reg == R_DIO_PAD_SLEEP_STATUS) { + return 0x3u; + } + return 0x1u; +} + +static bool ot_pinmux_eg_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t reg_be = ((1u << size) - 1u) << (addr & 0x3u); + return reg < REGS_COUNT && + (!is_write || (ot_pinmux_eg_permit(reg) & ~reg_be) == 0u); +} + static const MemoryRegionOps ot_pinmux_eg_regs_ops = { .read = &ot_pinmux_eg_regs_read, .write = &ot_pinmux_eg_regs_write, .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_pinmux_eg_regs_accepts, }; static void ot_pinmux_eg_reset_enter(Object *obj, ResetType type) @@ -507,7 +1037,33 @@ static void ot_pinmux_eg_reset_enter(Object *obj, ResetType type) } OtPinmuxEgStateRegs *regs = s->regs; + bool is_por = (type == RESET_TYPE_COLD) || ot_rstmgr_is_por_reset(); + if (!is_por && ot_rstmgr_is_low_power_exit()) { + ot_uart_update_pinmux(regs->mio_outsel, regs->mio_periph_insel); + ot_pinmux_eg_update_sysrst_inputs(s); + ot_pinmux_eg_update_gpio_inputs(s); + ot_pinmux_eg_update_pads(s); + ibex_irq_set(&s->wkup, (int)(bool)regs->wkup_cause); + ibex_irq_set(&s->alert, 0); + return; + } + + uint32_t wkup_cause = regs->wkup_cause; + uint32_t mio_sleep_st[MIO_SLEEP_STATUS_COUNT]; + uint32_t dio_sleep_st[DIO_SLEEP_STATUS_COUNT]; + memcpy(mio_sleep_st, regs->mio_pad_sleep_status, sizeof(mio_sleep_st)); + memcpy(dio_sleep_st, regs->dio_pad_sleep_status, sizeof(dio_sleep_st)); + memset(regs, 0, sizeof(*regs)); + if (!is_por) { + regs->wkup_cause = wkup_cause; + memcpy(regs->mio_pad_sleep_status, mio_sleep_st, sizeof(mio_sleep_st)); + memcpy(regs->dio_pad_sleep_status, dio_sleep_st, sizeof(dio_sleep_st)); + } else { + memset(s->mio_latched_sleep_mode, 0, sizeof(s->mio_latched_sleep_mode)); + memset(s->dio_latched_sleep_mode, 0, sizeof(s->dio_latched_sleep_mode)); + } + s->dio_in_level = (1u << 13u); for (unsigned ix = 0; ix < PARAM_N_MIO_PERIPH_IN; ix++) { regs->mio_periph_insel_regwen[ix] = 0x1u; @@ -528,9 +1084,22 @@ static void ot_pinmux_eg_reset_enter(Object *obj, ResetType type) regs->wkup_detector_regwen[ix] = 0x1u; } + ot_uart_update_pinmux(regs->mio_outsel, regs->mio_periph_insel); + ot_pinmux_eg_update_sysrst_inputs(s); + ot_pinmux_eg_update_gpio_inputs(s); + ot_pinmux_eg_update_pads(s); + ibex_irq_set(&s->wkup, (int)(bool)regs->wkup_cause); ibex_irq_set(&s->alert, 0); } +static void ot_pinmux_eg_reset_exit(Object *obj, ResetType type) +{ + OtPinmuxEgState *s = OT_PINMUX_EG(obj); + (void)type; + + ot_pinmux_eg_update_pads(s); +} + static void ot_pinmux_eg_init(Object *obj) { OtPinmuxEgState *s = OT_PINMUX_EG(obj); @@ -541,13 +1110,28 @@ static void ot_pinmux_eg_init(Object *obj) s->regs = g_new0(OtPinmuxEgStateRegs, 1u); ibex_qdev_init_irq(obj, &s->alert, OT_DEVICE_ALERT); + ibex_qdev_init_irq(obj, &s->wkup, OT_PINMUX_WKUP); s->dios = g_new(IbexIRQ, PARAM_N_DIO_PADS); s->mios = g_new(IbexIRQ, PARAM_N_MIO_PADS); + s->gpio_pads = g_new(IbexIRQ, 32u); + s->sysrst_inputs = g_new(IbexIRQ, OT_SYSRST_CTRL_IN_COUNT); + s->gpio_inputs = g_new(IbexIRQ, 32u); ibex_qdev_init_irqs_default(obj, s->dios, OT_PINMUX_DIO, PARAM_N_DIO_PADS, PAD_ATTR_ENABLE(false)); ibex_qdev_init_irqs_default(obj, s->mios, OT_PINMUX_MIO, PARAM_N_MIO_PADS, PAD_ATTR_ENABLE(false)); + ibex_qdev_init_irqs_default(obj, s->gpio_pads, OT_PINMUX_GPIO_PAD, 32u, 0); + ibex_qdev_init_irqs_default(obj, s->sysrst_inputs, OT_PINMUX_SYSRST_IN, + OT_SYSRST_CTRL_IN_COUNT, 0); + ibex_qdev_init_irqs_default(obj, s->gpio_inputs, OT_PINMUX_GPIO_IN, 32u, + -1); + s->pad_in_level = (1ULL << 14) | (1ULL << 15); + qdev_init_gpio_in_named(DEVICE(obj), &ot_pinmux_eg_pad_in, OT_PINMUX_PAD_IN, + PARAM_N_MIO_PADS); + qdev_init_gpio_in_named(DEVICE(obj), &ot_pinmux_eg_sleep_en, + OT_PINMUX_SLEEP_EN, 1u); + ot_pinmux_eg_instance = s; } static void ot_pinmux_eg_class_init(ObjectClass *klass, const void *data) @@ -560,7 +1144,8 @@ static void ot_pinmux_eg_class_init(ObjectClass *klass, const void *data) ResettableClass *rc = RESETTABLE_CLASS(klass); OtPinmuxEgClass *pc = OT_PINMUX_EG_CLASS(klass); resettable_class_set_parent_phases(rc, &ot_pinmux_eg_reset_enter, NULL, - NULL, &pc->parent_phases); + &ot_pinmux_eg_reset_exit, + &pc->parent_phases); } static const TypeInfo ot_pinmux_eg_info = { diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 486c302a7be3d..060b5360e285c 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -986,6 +986,8 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_IBEX_WRAPPER_CPU_EN, OT_IBEX_PWRMGR_CPU_EN), /* todo: add pwmgr GPIO strap when Earlgrey GPIO is updated */ + OT_EG_SOC_SIGNAL(OT_PWRMGR_SLEEP_EN, 0, PINMUX, + OT_PINMUX_SLEEP_EN, 0), OT_EG_SOC_SIGNAL(OT_PWRMGR_RST_REQ, 0, RSTMGR, OT_RSTMGR_RST_REQ, 0) ), @@ -1076,7 +1078,89 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { { .base = 0x40460000u } ), .gpio = IBEXGPIOCONNDEFS( - OT_EG_SOC_GPIO_ALERT(0, 30) + OT_EG_SOC_GPIO_ALERT(0, 30), + OT_EG_SOC_SIGNAL(OT_PINMUX_WKUP, 0, PWRMGR, + OT_PWRMGR_WKUP, OT_PWRMGR_WAKEUP_PINMUX), + OT_EG_SOC_SIGNAL(OT_PINMUX_SYSRST_IN, 0, SYSRST_CTRL, + OT_SYSRST_CTRL_INPUT, 0), + OT_EG_SOC_SIGNAL(OT_PINMUX_SYSRST_IN, 1, SYSRST_CTRL, + OT_SYSRST_CTRL_INPUT, 1), + OT_EG_SOC_SIGNAL(OT_PINMUX_SYSRST_IN, 2, SYSRST_CTRL, + OT_SYSRST_CTRL_INPUT, 2), + OT_EG_SOC_SIGNAL(OT_PINMUX_SYSRST_IN, 3, SYSRST_CTRL, + OT_SYSRST_CTRL_INPUT, 3), + OT_EG_SOC_SIGNAL(OT_PINMUX_SYSRST_IN, 4, SYSRST_CTRL, + OT_SYSRST_CTRL_INPUT, 4), + OT_EG_SOC_SIGNAL(OT_PINMUX_SYSRST_IN, 5, SYSRST_CTRL, + OT_SYSRST_CTRL_INPUT, 5), + OT_EG_SOC_SIGNAL(OT_PINMUX_SYSRST_IN, 6, SYSRST_CTRL, + OT_SYSRST_CTRL_INPUT, 6), + OT_EG_SOC_SIGNAL(OT_PINMUX_SYSRST_IN, 7, SYSRST_CTRL, + OT_SYSRST_CTRL_INPUT, 7), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 0, GPIO, OT_PINMUX_PAD, 0), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 1, GPIO, OT_PINMUX_PAD, 1), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 2, GPIO, OT_PINMUX_PAD, 2), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 3, GPIO, OT_PINMUX_PAD, 3), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 4, GPIO, OT_PINMUX_PAD, 4), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 5, GPIO, OT_PINMUX_PAD, 5), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 6, GPIO, OT_PINMUX_PAD, 6), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 7, GPIO, OT_PINMUX_PAD, 7), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 8, GPIO, OT_PINMUX_PAD, 8), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 9, GPIO, OT_PINMUX_PAD, 9), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 10, GPIO, OT_PINMUX_PAD, 10), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 11, GPIO, OT_PINMUX_PAD, 11), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 12, GPIO, OT_PINMUX_PAD, 12), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 13, GPIO, OT_PINMUX_PAD, 13), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 14, GPIO, OT_PINMUX_PAD, 14), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 15, GPIO, OT_PINMUX_PAD, 15), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 16, GPIO, OT_PINMUX_PAD, 16), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 17, GPIO, OT_PINMUX_PAD, 17), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 18, GPIO, OT_PINMUX_PAD, 18), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 19, GPIO, OT_PINMUX_PAD, 19), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 20, GPIO, OT_PINMUX_PAD, 20), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 21, GPIO, OT_PINMUX_PAD, 21), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 22, GPIO, OT_PINMUX_PAD, 22), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 23, GPIO, OT_PINMUX_PAD, 23), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 24, GPIO, OT_PINMUX_PAD, 24), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 25, GPIO, OT_PINMUX_PAD, 25), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 26, GPIO, OT_PINMUX_PAD, 26), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 27, GPIO, OT_PINMUX_PAD, 27), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 28, GPIO, OT_PINMUX_PAD, 28), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 29, GPIO, OT_PINMUX_PAD, 29), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 30, GPIO, OT_PINMUX_PAD, 30), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_PAD, 31, GPIO, OT_PINMUX_PAD, 31), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 0, GPIO, OT_GPIO_IN, 0), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 1, GPIO, OT_GPIO_IN, 1), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 2, GPIO, OT_GPIO_IN, 2), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 3, GPIO, OT_GPIO_IN, 3), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 4, GPIO, OT_GPIO_IN, 4), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 5, GPIO, OT_GPIO_IN, 5), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 6, GPIO, OT_GPIO_IN, 6), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 7, GPIO, OT_GPIO_IN, 7), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 8, GPIO, OT_GPIO_IN, 8), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 9, GPIO, OT_GPIO_IN, 9), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 10, GPIO, OT_GPIO_IN, 10), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 11, GPIO, OT_GPIO_IN, 11), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 12, GPIO, OT_GPIO_IN, 12), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 13, GPIO, OT_GPIO_IN, 13), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 14, GPIO, OT_GPIO_IN, 14), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 15, GPIO, OT_GPIO_IN, 15), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 16, GPIO, OT_GPIO_IN, 16), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 17, GPIO, OT_GPIO_IN, 17), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 18, GPIO, OT_GPIO_IN, 18), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 19, GPIO, OT_GPIO_IN, 19), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 20, GPIO, OT_GPIO_IN, 20), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 21, GPIO, OT_GPIO_IN, 21), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 22, GPIO, OT_GPIO_IN, 22), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 23, GPIO, OT_GPIO_IN, 23), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 24, GPIO, OT_GPIO_IN, 24), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 25, GPIO, OT_GPIO_IN, 25), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 26, GPIO, OT_GPIO_IN, 26), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 27, GPIO, OT_GPIO_IN, 27), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 28, GPIO, OT_GPIO_IN, 28), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 29, GPIO, OT_GPIO_IN, 29), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 30, GPIO, OT_GPIO_IN, 30), + OT_EG_SOC_SIGNAL(OT_PINMUX_GPIO_IN, 31, GPIO, OT_GPIO_IN, 31) ) }, [OT_EG_SOC_DEV_AON_TIMER] = { diff --git a/include/hw/opentitan/ot_pinmux.h b/include/hw/opentitan/ot_pinmux.h index 48557cc710ea7..6a0117a98ae49 100644 --- a/include/hw/opentitan/ot_pinmux.h +++ b/include/hw/opentitan/ot_pinmux.h @@ -33,9 +33,15 @@ #define TYPE_OT_PINMUX "ot-pinmux" -#define OT_PINMUX_DIO TYPE_OT_PINMUX "-dio" -#define OT_PINMUX_MIO TYPE_OT_PINMUX "-mio" -#define OT_PINMUX_PAD TYPE_OT_PINMUX "-pad" /* for devices using pinmux pad */ +#define OT_PINMUX_DIO TYPE_OT_PINMUX "-dio" +#define OT_PINMUX_MIO TYPE_OT_PINMUX "-mio" +#define OT_PINMUX_PAD TYPE_OT_PINMUX "-pad" /* for devices using pinmux pad */ +#define OT_PINMUX_PAD_IN TYPE_OT_PINMUX "-pad-in" +#define OT_PINMUX_GPIO_IN TYPE_OT_PINMUX "-gpio-in" +#define OT_PINMUX_WKUP TYPE_OT_PINMUX "-wkup" +#define OT_PINMUX_SYSRST_IN TYPE_OT_PINMUX "-sysrst-in" +#define OT_PINMUX_SLEEP_EN TYPE_OT_PINMUX "-sleep-en" +#define OT_PINMUX_GPIO_PAD TYPE_OT_PINMUX "-gpio-pad" SHARED_FIELD(OT_PINMUX_PAD_ATTR_INVERT, 0u, 1u) SHARED_FIELD(OT_PINMUX_PAD_ATTR_VIRTUAL_OD_EN, 1u, 1u) @@ -44,6 +50,7 @@ SHARED_FIELD(OT_PINMUX_PAD_ATTR_PULL_SELECT, 3u, 1u) SHARED_FIELD(OT_PINMUX_PAD_ATTR_KEEPER_EN, 4u, 1u) SHARED_FIELD(OT_PINMUX_PAD_ATTR_SCHMITT_EN, 5u, 1u) SHARED_FIELD(OT_PINMUX_PAD_ATTR_OD_EN, 6u, 1u) +SHARED_FIELD(OT_PINMUX_PAD_ATTR_INPUT_DISABLE, 7u, 1u) SHARED_FIELD(OT_PINMUX_PAD_ATTR_SLEW_RATE, 16u, 0x2u) SHARED_FIELD(OT_PINMUX_PAD_ATTR_DRIVE_STRENGTH, 20u, 4u) SHARED_FIELD(OT_PINMUX_PAD_ATTR_FORCE_MODE, 28u, 2u) @@ -59,7 +66,8 @@ typedef enum { (OT_PINMUX_PAD_ATTR_INVERT_MASK | OT_PINMUX_PAD_ATTR_VIRTUAL_OD_EN_MASK | \ OT_PINMUX_PAD_ATTR_PULL_EN_MASK | OT_PINMUX_PAD_ATTR_PULL_SELECT_MASK | \ OT_PINMUX_PAD_ATTR_KEEPER_EN_MASK | OT_PINMUX_PAD_ATTR_SCHMITT_EN_MASK | \ - OT_PINMUX_PAD_ATTR_OD_EN_MASK | OT_PINMUX_PAD_ATTR_SLEW_RATE_MASK | \ + OT_PINMUX_PAD_ATTR_OD_EN_MASK | OT_PINMUX_PAD_ATTR_INPUT_DISABLE_MASK | \ + OT_PINMUX_PAD_ATTR_SLEW_RATE_MASK | \ OT_PINMUX_PAD_ATTR_DRIVE_STRENGTH_MASK) #endif /* HW_OPENTITAN_OT_PINMUX_H */ diff --git a/include/hw/opentitan/ot_pinmux_eg.h b/include/hw/opentitan/ot_pinmux_eg.h index 20122898cfea0..1ba54697655e7 100644 --- a/include/hw/opentitan/ot_pinmux_eg.h +++ b/include/hw/opentitan/ot_pinmux_eg.h @@ -33,4 +33,17 @@ #define TYPE_OT_PINMUX_EG TYPE_OT_PINMUX "-eg" OBJECT_DECLARE_TYPE(OtPinmuxEgState, OtPinmuxEgClass, OT_PINMUX_EG) +int ot_pinmux_eg_get_dio_sleep_val(unsigned dio_pad); +void ot_pinmux_eg_dio_pad_in(unsigned dio_pad, int level); +int ot_pinmux_eg_mio_to_host_pin(unsigned pad); +int ot_pinmux_eg_host_pin_to_gpio(OtPinmuxEgState *s, unsigned host_pin); +uint32_t ot_pinmux_eg_get_mio_outsel(OtPinmuxEgState *s, unsigned mio_pad); +uint32_t ot_pinmux_eg_get_mio_pad_attr(OtPinmuxEgState *s, unsigned mio_pad); +uint32_t ot_pinmux_eg_get_dio_pad_attr(unsigned dio_pad); +void ot_pinmux_eg_update_sysrst_inputs(OtPinmuxEgState *s); +uint32_t ot_pinmux_eg_get_gpio_outsel(OtPinmuxEgState *s, unsigned gpio_pin); +uint32_t ot_pinmux_eg_get_periph_insel(OtPinmuxEgState *s, unsigned periph_in); +bool ot_pinmux_eg_is_periph_in_zero(unsigned periph_in); +bool ot_pinmux_eg_trigger_mio_wkup(unsigned mio_pad); + #endif /* HW_OPENTITAN_OT_PINMUX_EG_H */ From 86f50c6baa417da96c220dd4795f79ddb6e11098 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:42 +0000 Subject: [PATCH 47/52] [ot] hw/opentitan: gpio_eg: implement 63-pad muxing and pad monitor Implement Earlgrey 32-pin GPIO and 63-pad (47 MIO + 16 DIO) padring multiplexing and the virtual pad monitor chardev protocol in ot_gpio_eg: - Evaluate effective GPIO DATA_IN and MIO/DIO pad levels by combining GPIO DIRECT_OUT/DIRECT_OE, Pinmux MIO_OUTSEL/MIO_PERIPH_INSEL, pad attributes (INVERT, OD_EN, VIRTUAL_OD_EN, PULL_EN, PULL_SELECT, INPUT_DISABLE), sleep retention overrides, and peripheral outputs from ot_pattgen, ot_pwm, and ot_sysrst_ctrl. - Support STRAPS (STRAP_SAMPLED and DLATCH) hardware strap sampling, level/edge interrupt evaluation, and masked halfword writes. - Extend the QEMU GPIO chardev wire protocol to stream timestamped logic analyzer waveforms (B:/T:/O:) and bitbang host I/O (W:/M:/D:/P:/H:/Q) for opentitanlib. - Link ot_pwm.c, ot_pattgen.c, and ot_sysrst_ctrl.c in hw/opentitan/meson.build and wire OT_EG_SOC_DEV_GPIO, OT_EG_SOC_DEV_PATTGEN, OT_EG_SOC_DEV_PWM, and OT_EG_SOC_DEV_SYSRST_CTRL in hw/riscv/ot_earlgrey.c now that all mutual pad callbacks are defined. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I9309f4a145ff6111e2662283e49873296a6a6964 --- hw/opentitan/ot_gpio_eg.c | 1037 +++++++++++++++++++++++++++-- hw/opentitan/ot_pattgen.c | 7 +- hw/opentitan/ot_pinmux_eg.c | 7 +- hw/opentitan/ot_pwm.c | 2 + hw/opentitan/ot_sysrst_ctrl.c | 2 + hw/riscv/ot_earlgrey.c | 45 ++ include/hw/opentitan/ot_gpio_eg.h | 6 + 7 files changed, 1040 insertions(+), 66 deletions(-) diff --git a/hw/opentitan/ot_gpio_eg.c b/hw/opentitan/ot_gpio_eg.c index 1eb2f4c497dcb..1bb9b4d8e4656 100644 --- a/hw/opentitan/ot_gpio_eg.c +++ b/hw/opentitan/ot_gpio_eg.c @@ -28,18 +28,27 @@ #include "qemu/osdep.h" #include "qemu/error-report.h" #include "qemu/log.h" +#include "qemu/main-loop.h" +#include "qemu/timer.h" #include "qemu/typedefs.h" #include "chardev/char-fe.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_gpio_eg.h" +#include "hw/opentitan/ot_i2c.h" +#include "hw/opentitan/ot_pattgen.h" #include "hw/opentitan/ot_pinmux.h" +#include "hw/opentitan/ot_pinmux_eg.h" +#include "hw/opentitan/ot_pwm.h" +#include "hw/opentitan/ot_spi_host.h" +#include "hw/opentitan/ot_sysrst_ctrl.h" #include "hw/qdev-properties-system.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" #include "hw/riscv/ibex_common.h" #include "hw/riscv/ibex_irq.h" #include "hw/sysbus.h" +#include "system/replay.h" #include "system/runstate.h" #include "trace.h" @@ -123,17 +132,51 @@ struct OtGpioEgState { uint32_t data_ii; /* input data from IRQ lines */ uint32_t data_ib; /* input data from backend */ uint32_t data_bi; /* ignore backend input */ + uint32_t host_pull; /* host pull up/down from backend */ uint32_t data_gi; /* ignore GPIO input */ uint32_t invert; /* invert signal */ uint32_t opendrain; /* open drain (1 -> hi-z) */ uint32_t pull_en; /* pull up/down enable */ uint32_t pull_sel; /* pull up or pull down */ uint32_t connected; /* connected to an external device */ + uint32_t force_en; /* pinmux sleep force enable mask */ + uint32_t force_oe; /* pinmux sleep force output enable mask */ + uint32_t force_out; /* pinmux sleep force output value mask */ - char ibuf[PARAM_NUM_IO]; /* backed input buffer */ + char ibuf[65536]; /* backed input buffer */ unsigned ipos; OtGpioEgBackendState backend_state; /* cache */ + OtPinmuxEgState *pinmux; + OtPattgenState *pattgen; + OtPwmState *pwm; + OtSPIHostState *spi_host1; + OtSysrstCtrlState *sysrst_ctrl; + OtI2CState *i2c[3]; + uint64_t time_offset_us; + uint64_t last_total_us; + bool explicit_time; + bool batch_mode; + bool in_reset; + bool pending_input_ack; + GString *batch_buf; + QEMUTimer *bb_timer; + QEMUTimer *sync_timer; + uint32_t sync_seq; + uint8_t *bb_wbuf; + uint8_t *bb_rbuf; + uint64_t bb_period_ns; + uint32_t bb_num_samples; + uint32_t bb_idx; + uint32_t last_read_data_in; + uint32_t poll_count; + int64_t bb_start_ns; + unsigned bb_pins[32]; + unsigned bb_npins; + unsigned bb_settle_ticks; + unsigned bb_wait_pwm_ticks; + unsigned bb_tail_ticks; + char *ot_id; uint32_t reset_in; /* initial input levels */ uint32_t reset_out; /* initial output levels */ @@ -148,7 +191,18 @@ struct OtGpioEgClass { ResettablePhases parent_phases; }; +static OtGpioEgState *ot_gpio_eg_instance; + static void ot_gpio_eg_update_backend(OtGpioEgState *s, bool force); +static void ot_gpio_eg_update_data_in(OtGpioEgState *s); + +void ot_gpio_eg_notify_sysrst_change(void) +{ + if (ot_gpio_eg_instance) { + ot_gpio_eg_update_data_in(ot_gpio_eg_instance); + ot_gpio_eg_update_backend(ot_gpio_eg_instance, false); + } +} static void ot_gpio_eg_update_irqs(OtGpioEgState *s) { @@ -184,18 +238,185 @@ static void ot_gpio_eg_update_intr_edge(OtGpioEgState *s, uint32_t prev) s->regs[R_INTR_STATE] |= intr_state; } +static void ot_gpio_eg_update_pad_in_lines(OtGpioEgState *s) +{ + uint32_t periph_out_v = s->data_out ^ s->invert; + uint32_t periph_out_en = s->data_oe & ~(s->opendrain & periph_out_v); + periph_out_en = + (periph_out_en & ~s->force_en) | (s->force_oe & s->force_en); + periph_out_v = (periph_out_v & ~s->force_en) | (s->force_out & s->force_en); + periph_out_v &= periph_out_en; + + uint32_t periph_pull_en = s->pull_en & ~s->force_en; + uint32_t periph_pull_v = s->pull_sel; + + for (unsigned p = 0; p < PARAM_NUM_IO; p++) { + bool driven_by_dev = false; + int level = 0; + + if (s->pinmux) { + uint32_t outsel = ot_pinmux_eg_get_gpio_outsel(s->pinmux, p); + int g_pull = ot_pinmux_eg_host_pin_to_gpio(s->pinmux, p); + unsigned gp = (g_pull >= 0 && g_pull < (int)PARAM_NUM_IO) ? + (unsigned)g_pull : + p; + if ((s->force_en >> gp) & 1u) { + if ((s->force_oe >> gp) & 1u) { + driven_by_dev = true; + level = (int)((s->force_out >> gp) & 1u); + } + } else if (outsel == 0u) { + driven_by_dev = true; + level = 0; + } else if (outsel == 1u) { + driven_by_dev = true; + level = 1; + } else if (outsel >= 3u && outsel < 35u) { + unsigned g = outsel - 3u; + if ((periph_out_en >> g) & 1u) { + driven_by_dev = true; + level = (int)((periph_out_v >> g) & 1u); + } + } else if (outsel >= 35u && outsel <= 42u && s->sysrst_ctrl) { + driven_by_dev = true; + level = ot_sysrst_ctrl_get_outsel_level(s->sysrst_ctrl, outsel); + } else if (outsel >= 49u && outsel <= 52u && s->pattgen) { + driven_by_dev = true; + level = (int)ot_pattgen_get_pin_level(s->pattgen, outsel - 49u); + } else if (outsel >= 65u && outsel < 71u && s->pwm) { + driven_by_dev = true; + level = (int)ot_pwm_get_channel_level(s->pwm, outsel - 65u, + qemu_clock_get_ns( + QEMU_CLOCK_VIRTUAL)); + } + if (!driven_by_dev) { + if (p == 14u || p == 15u) { + /* Open-drain DIO pins (IOR8/ec_rst_l, IOR9/flash_wp_l) with + * board pull-ups */ + bool host_low = + !((s->data_bi >> p) & 1u) && !((s->data_ib >> p) & 1u); + level = host_low ? 0 : 1; + } else if (!((s->data_bi >> p) & 1u)) { + level = (int)((s->data_ib >> p) & 1u); + } else if ((periph_pull_en >> gp) & 1u) { + level = (int)((periph_pull_v >> gp) & 1u); + } else if ((s->host_pull >> p) & 1u) { + level = (int)((s->host_pull >> p) & 1u); + } else { + level = (int)((s->regs[R_DATA_IN] >> p) & 1u); + } + } + } + + if (level != ibex_irq_get_level(&s->gpos[p])) { + trace_ot_gpio_out_update_line_bool(s->ot_id, p, level); + ibex_irq_set(&s->gpos[p], level); + } + } +} + +int ot_gpio_eg_get_mio_pad_in(unsigned mio_pad) +{ + OtGpioEgState *s = ot_gpio_eg_instance; + if (!s || !s->pinmux || mio_pad >= 47u) { + return -1; + } + uint32_t pad_attr = ot_pinmux_eg_get_mio_pad_attr(s->pinmux, mio_pad); + bool pad_inv = (bool)(pad_attr & OT_PINMUX_PAD_ATTR_INVERT_MASK); + bool pad_in_dis = (bool)(pad_attr & OT_PINMUX_PAD_ATTR_INPUT_DISABLE_MASK); + if (pad_in_dis) { + return pad_inv ? 1 : 0; + } + uint32_t outsel = ot_pinmux_eg_get_mio_outsel(s->pinmux, mio_pad); + if (outsel == 0u) { + return 0; + } + if (outsel == 1u) { + return 1; + } + if (outsel >= 3u && outsel < 35u) { + unsigned g_out = outsel - 3u; + if ((s->data_oe >> g_out) & 1u) { + bool g_out_val = (bool)((s->data_out >> g_out) & 1u); + bool od = + (bool)(pad_attr & (OT_PINMUX_PAD_ATTR_OD_EN_MASK | + OT_PINMUX_PAD_ATTR_VIRTUAL_OD_EN_MASK)); + if (!(od && (g_out_val ^ pad_inv))) { + return g_out_val ? 1 : 0; + } + } + } else if (outsel >= 72u && outsel <= 77u && s->sysrst_ctrl) { + return ot_sysrst_ctrl_get_outsel_level(s->sysrst_ctrl, outsel); + } else if (outsel >= 49u && outsel <= 52u && s->pattgen) { + return ot_pattgen_get_pin_level(s->pattgen, outsel - 49u) ? 1 : 0; + } else if (outsel >= 65u && outsel < 71u && s->pwm) { + return ot_pwm_get_channel_level(s->pwm, outsel - 65u, + qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) ? + 1 : + 0; + } + return -1; +} + static void ot_gpio_eg_update_data_in(OtGpioEgState *s) { uint32_t prev = s->regs[R_DATA_IN]; - uint32_t ii_mask = s->connected & ~s->data_gi & ~s->data_oe; - uint32_t bi_mask = ~s->connected & ~s->data_bi & ~s->data_oe; - uint32_t pi_mask = s->connected & s->data_gi & s->data_bi & ~s->data_oe; + uint32_t routed_ib = 0; + uint32_t routed_hp = 0; + uint32_t routed_bi = UINT32_MAX; + uint32_t eff_oe = 0; + if (s->pinmux) { + bool all_muxed = true; + for (unsigned g = 0; g < PARAM_NUM_IO; g++) { + if (ot_pinmux_eg_get_periph_insel(s->pinmux, g) < 2u) { + all_muxed = false; + break; + } + } + uint32_t eff_data_ib = + (all_muxed && (s->data_ib & ~0x0000c000u) == 0u) ? 0u : s->data_ib; + for (unsigned g = 0; g < PARAM_NUM_IO; g++) { + uint32_t insel = ot_pinmux_eg_get_periph_insel(s->pinmux, g); + if (insel == 0u) { + routed_bi &= ~(1u << g); + } else if (insel == 1u) { + routed_ib |= (1u << g); + routed_bi &= ~(1u << g); + } else if (insel >= 2u && (insel - 2u) < 47u) { + unsigned mio_pad = insel - 2u; + int hp = + all_muxed ? (int)g : ot_pinmux_eg_mio_to_host_pin(mio_pad); + if (hp >= 0 && hp < (int)PARAM_NUM_IO) { + unsigned host_pin = (unsigned)hp; + if ((eff_data_ib >> host_pin) & 1u) { + routed_ib |= (1u << g); + } + if (!all_muxed && ((s->host_pull >> host_pin) & 1u)) { + routed_hp |= (1u << g); + } + if (!((s->data_bi >> host_pin) & 1u)) { + routed_bi &= ~(1u << g); + } + } + if (((s->data_oe >> g) & 1u) && + ot_pinmux_eg_get_mio_outsel(s->pinmux, mio_pad) == + (3u + g)) { + eff_oe |= (1u << g); + } + } + } + } + + uint32_t ii_mask = s->connected & ~s->data_gi; + uint32_t bi_mask = ~s->connected & ~routed_bi & ~eff_oe; + uint32_t pi_mask = ~s->connected & routed_bi & ~eff_oe; uint32_t data_ii = s->data_ii & ii_mask; - uint32_t data_ib = s->data_ib & bi_mask; - uint32_t pull_in = s->pull_en & s->pull_sel & pi_mask; - uint32_t data_in = data_ii | data_ib | pull_in; + uint32_t data_ib = routed_ib & bi_mask; + uint32_t pull_in = + ((s->pull_en & s->pull_sel) | (~s->pull_en & routed_hp)) & pi_mask; + uint32_t ext_in = data_ii | data_ib | pull_in; trace_ot_gpio_in_ignore(s->ot_id, s->connected, s->data_gi, s->data_bi, s->data_oe); @@ -203,20 +424,42 @@ static void ot_gpio_eg_update_data_in(OtGpioEgState *s) trace_ot_gpio_in_backend(s->ot_id, s->data_ib, bi_mask, data_ib); trace_ot_gpio_in_pull(s->ot_id, s->pull_en, s->pull_sel, pi_mask, pull_in); - /* apply inversion if any */ - data_in ^= s->invert; - - /* inject back output pin values into input */ - uint32_t data_mix = data_in | (s->data_out & s->data_oe); + uint32_t data_mix = 0; + if (s->pinmux) { + for (unsigned g = 0; g < PARAM_NUM_IO; g++) { + uint32_t insel = ot_pinmux_eg_get_periph_insel(s->pinmux, g); + bool in_o = false; + if (insel == 1u) { + in_o = true; + } else if (insel >= 2u && (insel - 2u) < 47u) { + unsigned mio_pad = insel - 2u; + int mio_in = ot_gpio_eg_get_mio_pad_in(mio_pad); + if (mio_in >= 0) { + in_o = (bool)mio_in; + } else { + bool pad_inv = + (bool)(ot_pinmux_eg_get_mio_pad_attr(s->pinmux, + mio_pad) & + OT_PINMUX_PAD_ATTR_INVERT_MASK); + bool raw = ((ext_in >> g) & 1u) != 0; + in_o = raw ^ pad_inv; + } + } + if (in_o) { + data_mix |= (1u << g); + } + } + } s->regs[R_DATA_IN] = data_mix; - trace_ot_gpio_in_update(s->ot_id, s->invert, data_in, s->data_out, - data_mix); + trace_ot_gpio_in_update(s->ot_id, s->invert, ext_in, s->data_out, data_mix); ot_gpio_eg_update_intr_level(s); ot_gpio_eg_update_intr_edge(s, prev); ot_gpio_eg_update_irqs(s); + + ot_gpio_eg_update_pad_in_lines(s); } static void ot_gpio_eg_update_data_out(OtGpioEgState *s) @@ -230,18 +473,17 @@ static void ot_gpio_eg_update_data_out(OtGpioEgState *s) /* if open drain is active and output is high, disable output enable */ out_en &= ~(s->opendrain & outv); + /* Apply PINMUX sleep retention force overrides */ + out_en = (out_en & ~s->force_en) | (s->force_oe & s->force_en); + outv = (outv & ~s->force_en) | (s->force_out & s->force_en); + /* keep non- opendrain high values */ outv &= out_en; trace_ot_gpio_out_update(s->ot_id, outv, 0, 0); - for (unsigned ix = 0; ix < PARAM_NUM_IO; ix++) { - if ((out_en >> ix) & 1u) { - int level = (int)((outv >> ix) & 1u); - if (level != ibex_irq_get_level(&s->gpos[ix])) { - trace_ot_gpio_out_update_line_bool(s->ot_id, ix, level); - } - ibex_irq_set(&s->gpos[ix], level); - } + ot_gpio_eg_update_pad_in_lines(s); + if (s->pinmux) { + ot_pinmux_eg_update_sysrst_inputs(s->pinmux); } } @@ -321,6 +563,30 @@ static void ot_gpio_eg_pad_attr_change(void *opaque, int no, int level) } confstr[3u] = '\0'; + uint32_t force_mode = SHARED_FIELD_EX32(cfg, OT_PINMUX_PAD_ATTR_FORCE_MODE); + switch (force_mode) { + case OT_PINMUX_PAD_FORCE_LOW: + s->force_en |= bit; + s->force_oe |= bit; + s->force_out &= ~bit; + break; + case OT_PINMUX_PAD_FORCE_HIGH: + s->force_en |= bit; + s->force_oe |= bit; + s->force_out |= bit; + break; + case OT_PINMUX_PAD_FORCE_HIZ: + s->force_en |= bit; + s->force_oe &= ~bit; + s->force_out &= ~bit; + break; + case OT_PINMUX_PAD_FORCE_IGNORE: + default: + s->force_en &= ~bit; + s->force_oe &= ~bit; + s->force_out &= ~bit; + break; + } trace_ot_gpio_pad_attr_change(s->ot_id, no, cfg, confstr); ot_gpio_eg_update_data_in(s); @@ -328,6 +594,18 @@ static void ot_gpio_eg_pad_attr_change(void *opaque, int no, int level) ot_gpio_eg_update_backend(s, false); } +static bool ot_gpio_eg_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + uint8_t permit = (reg == R_ALERT_TEST) ? 0x1u : 0xfu; + return reg < REGS_COUNT && (addr & 3u) + size <= 4u && + (!is_write || (permit & ~reg_be) == 0u); +} + static uint64_t ot_gpio_eg_read(void *opaque, hwaddr addr, unsigned size) { OtGpioEgState *s = opaque; @@ -337,9 +615,27 @@ static uint64_t ot_gpio_eg_read(void *opaque, hwaddr addr, unsigned size) hwaddr reg = R32_OFF(addr); switch (reg) { + case R_DATA_IN: + ot_gpio_eg_update_data_in(s); + val32 = s->regs[reg]; + if (val32 == s->last_read_data_in) { + s->poll_count++; + if (s->poll_count > 16u && qemu_chr_fe_backend_connected(&s->chr)) { + bql_unlock(); + replay_mutex_unlock(); + g_usleep(50); + replay_mutex_lock(); + bql_lock(); + ot_gpio_eg_update_data_in(s); + val32 = s->regs[reg]; + } + } else { + s->poll_count = 0; + s->last_read_data_in = val32; + } + break; case R_INTR_STATE: case R_INTR_ENABLE: - case R_DATA_IN: case R_INTR_CTRL_EN_RISING: case R_INTR_CTRL_EN_FALLING: case R_INTR_CTRL_EN_LVLHIGH: @@ -390,7 +686,7 @@ static uint64_t ot_gpio_eg_read(void *opaque, hwaddr addr, unsigned size) pc); return (uint64_t)val32; -}; +} static void ot_gpio_eg_write(void *opaque, hwaddr addr, uint64_t val64, unsigned size) @@ -408,6 +704,7 @@ static void ot_gpio_eg_write(void *opaque, hwaddr addr, uint64_t val64, switch (reg) { case R_INTR_STATE: s->regs[reg] &= ~val32; /* RW1C */ + ot_gpio_eg_update_intr_level(s); ot_gpio_eg_update_irqs(s); break; case R_INTR_ENABLE: @@ -420,7 +717,10 @@ static void ot_gpio_eg_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_ALERT_TEST: val32 &= ALERT_TEST_MASK; - ibex_irq_set(&s->alert, (int)(bool)val32); + if (val32) { + ibex_irq_set(&s->alert, 1); + ibex_irq_set(&s->alert, 0); + } break; case R_DIRECT_OUT: s->regs[reg] = val32; @@ -494,7 +794,378 @@ static void ot_gpio_eg_write(void *opaque, hwaddr addr, uint64_t val64, s->ot_id, (uint32_t)addr); break; } -}; +} + +static bool ot_gpio_eg_bitbang_uses_spi_host1(OtGpioEgState *s) +{ + if (!s->pinmux || !s->spi_host1) { + return false; + } + for (unsigned bit = 0; bit < s->bb_npins; bit++) { + uint32_t outsel = + ot_pinmux_eg_get_gpio_outsel(s->pinmux, s->bb_pins[bit]); + if (outsel == 41u || outsel == 42u || outsel == 53u || outsel == 54u) { + return true; + } + } + return false; +} + +static OtI2CState *ot_gpio_eg_get_active_override_i2c(OtGpioEgState *s) +{ + bool has_i2c_pad = false; + for (unsigned i = 0; i < s->bb_npins; i++) { + unsigned pad = s->bb_pins[i]; + if (pad == 7u || pad == 8u) { + has_i2c_pad = true; + break; + } + if (s->pinmux) { + uint32_t outsel = ot_pinmux_eg_get_gpio_outsel(s->pinmux, pad); + if (outsel >= 35u && outsel <= 40u) { + has_i2c_pad = true; + break; + } + } + } + if (!has_i2c_pad) { + return NULL; + } + for (unsigned i = 0; i < 3u; i++) { + if (s->i2c[i] && ot_i2c_is_override_waveform_ready(s->i2c[i])) { + return s->i2c[i]; + } + } + for (unsigned i = 0; i < 3u; i++) { + if (s->i2c[i] && ot_i2c_is_override_enabled(s->i2c[i])) { + return s->i2c[i]; + } + } + return NULL; +} + +static OtI2CState *ot_gpio_eg_get_active_target_i2c(OtGpioEgState *s) +{ + bool has_i2c_pad = false; + for (unsigned i = 0; i < s->bb_npins; i++) { + unsigned pad = s->bb_pins[i]; + if (pad == 7u || pad == 8u) { + has_i2c_pad = true; + break; + } + } + if (!has_i2c_pad) { + return NULL; + } + for (unsigned i = 0; i < 3u; i++) { + if (s->i2c[i] && ot_i2c_is_target_enabled(s->i2c[i]) && + !ot_i2c_is_override_enabled(s->i2c[i])) { + return s->i2c[i]; + } + } + return NULL; +} + +static bool ot_gpio_eg_sample_pad(OtGpioEgState *s, unsigned pad, + int64_t sample_ns) +{ + if (pad == 7u || pad == 8u) { + OtI2CState *active_i2c = ot_gpio_eg_get_active_override_i2c(s); + if (active_i2c) { + return ot_i2c_get_override_pin_level(active_i2c, pad == 7u, + sample_ns); + } + } + if (s->pinmux) { + uint32_t outsel = ot_pinmux_eg_get_gpio_outsel(s->pinmux, pad); + if (outsel == 0u) { + return false; + } + if (outsel == 1u) { + return true; + } + if (outsel >= 65u && outsel < 71u && s->pwm) { + return ot_pwm_get_channel_level(s->pwm, outsel - 65u, sample_ns); + } + if ((outsel == 41u || outsel == 53u || outsel == 54u) && s->spi_host1) { + return ot_spi_host_get_pin_level(s->spi_host1, outsel, sample_ns); + } + } + if (pad < PARAM_NUM_IO && !((s->data_bi >> pad) & 1u)) { + return (bool)((s->data_ib >> pad) & 1u); + } + return false; +} + +static void ot_gpio_eg_bitbang_step(void *opaque) +{ + OtGpioEgState *s = opaque; + + if (!s->bb_wbuf || !s->bb_rbuf) { + return; + } + + bool uses_spi_host1 = ot_gpio_eg_bitbang_uses_spi_host1(s); + OtI2CState *active_i2c = + uses_spi_host1 ? NULL : ot_gpio_eg_get_active_override_i2c(s); + OtI2CState *target_i2c = + (uses_spi_host1 || active_i2c) ? NULL : + ot_gpio_eg_get_active_target_i2c(s); + bool uses_pwm = + !uses_spi_host1 && !active_i2c && !target_i2c && (s->pwm != NULL); + + if (s->bb_idx < s->bb_num_samples) { + uint8_t wval = s->bb_wbuf[s->bb_idx]; + + if (target_i2c && s->bb_idx == 0u && + !ot_i2c_is_target_polling_acq(target_i2c) && + s->bb_wait_pwm_ticks > 0) { + s->bb_wait_pwm_ticks--; + int64_t next_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 500000LL; + s->bb_start_ns = next_ns; + timer_mod_ns(s->bb_timer, next_ns); + return; + } + + /* Apply host-driven pins to s->data_ib */ + uint32_t prev_ib = s->data_ib; + uint32_t prev_bi = s->data_bi; + for (unsigned bit = 0; bit < s->bb_npins; bit++) { + unsigned pad = s->bb_pins[bit]; + if (pad < PARAM_NUM_IO) { + s->data_bi &= ~(1u << pad); + if ((wval >> bit) & 1u) { + s->data_ib |= (1u << pad); + } else { + s->data_ib &= ~(1u << pad); + } + } + } + if (s->data_ib != prev_ib || s->data_bi != prev_bi || s->bb_idx == 0) { + ot_gpio_eg_update_data_in(s); + } + + if (target_i2c) { + bool sda_in = true; + bool scl_in = true; + for (unsigned bit = 0; bit < s->bb_npins; bit++) { + unsigned pad = s->bb_pins[bit]; + bool level = ((wval >> bit) & 1u) != 0; + if (pad == 7u) { + sda_in = level; + } else if (pad == 8u) { + scl_in = level; + } + } + bool sda_out = + ot_i2c_bitbang_target_step(target_i2c, scl_in, sda_in); + uint8_t sample_byte = 0; + for (unsigned bit = 0; bit < s->bb_npins; bit++) { + unsigned pad = s->bb_pins[bit]; + bool level = (pad == 7u) ? sda_out : scl_in; + if (level) { + sample_byte |= (uint8_t)(1u << bit); + } + } + s->bb_rbuf[s->bb_idx] = sample_byte; + s->bb_idx++; + if (s->bb_idx >= s->bb_num_samples) { + s->bb_wait_pwm_ticks = 50000u; + s->bb_tail_ticks = 20u; + } + int64_t next_ns = + s->bb_start_ns + (int64_t)s->bb_idx * (int64_t)s->bb_period_ns; + timer_mod_ns(s->bb_timer, next_ns); + return; + } + + /* + * At sample 5 (after IOA7 has gone low at sample 2), if PWM is present + * and not yet active, pause bitbang sample index progression while + * advancing QEMU virtual time in 100 us steps until firmware finishes + * its debounce and enables PWM. + */ + if (s->bb_idx == 5u && s->bb_wait_pwm_ticks > 0 && + ((uses_pwm && !ot_pwm_is_active(s->pwm)) || + (uses_spi_host1 && !ot_spi_host_is_waveform_ready(s->spi_host1)) || + (active_i2c && !ot_i2c_is_override_waveform_ready(active_i2c)))) { + s->bb_wait_pwm_ticks--; + int64_t next_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + + (active_i2c ? 1000000LL : 100000LL); + s->bb_start_ns = + next_ns - (int64_t)s->bb_idx * (int64_t)s->bb_period_ns; + timer_mod_ns(s->bb_timer, next_ns); + return; + } + + if (s->bb_idx == 5u) { + s->bb_start_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - + (int64_t)s->bb_idx * (int64_t)s->bb_period_ns; + if (uses_spi_host1) { + ot_spi_host_set_waveform_start_ns(s->spi_host1, + s->bb_start_ns + + 10LL * (int64_t)s + ->bb_period_ns, + false); + } + if (active_i2c) { + ot_i2c_set_waveform_start_ns(active_i2c, + s->bb_start_ns + + 100LL * + (int64_t)s->bb_period_ns, + false); + } + } + + uint32_t chunk = 1u; + if (s->bb_idx >= 5u) { + while ((s->bb_idx + chunk) < s->bb_num_samples && + s->bb_wbuf[s->bb_idx + chunk] == wval) { + chunk++; + } + } + + for (uint32_t k = 0; k < chunk; k++) { + int64_t sample_ns = s->bb_start_ns + (int64_t)(s->bb_idx + k) * + (int64_t)s->bb_period_ns; + uint8_t sample_byte = 0; + for (unsigned bit = 0; bit < s->bb_npins; bit++) { + unsigned pad = s->bb_pins[bit]; + if (ot_gpio_eg_sample_pad(s, pad, sample_ns)) { + sample_byte |= (uint8_t)(1u << bit); + } + } + s->bb_rbuf[s->bb_idx + k] = sample_byte; + } + + if (chunk > 1u) { + int64_t skipped_ns = + (int64_t)(chunk - 1u) * (int64_t)s->bb_period_ns; + s->bb_start_ns -= skipped_ns; + if (s->pwm) { + ot_pwm_shift_start_ns(s->pwm, -skipped_ns); + } + if (s->spi_host1) { + ot_spi_host_set_waveform_start_ns(s->spi_host1, -skipped_ns, + true); + } + if (active_i2c) { + ot_i2c_set_waveform_start_ns(active_i2c, -skipped_ns, true); + } + } + + s->bb_idx += chunk; + if (s->bb_idx >= s->bb_num_samples) { + s->bb_wait_pwm_ticks = 50000u; + s->bb_tail_ticks = (uses_spi_host1 || active_i2c) ? 0u : 20u; + } + int64_t next_ns = + s->bb_start_ns + (int64_t)s->bb_idx * (int64_t)s->bb_period_ns; + timer_mod_ns(s->bb_timer, next_ns); + return; + } + + if (s->bb_tail_ticks > 0) { + s->bb_tail_ticks--; + int64_t next_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 100000LL; + timer_mod_ns(s->bb_timer, next_ns); + return; + } + + /* Encode s->bb_rbuf as RLE and send "B:\r\n" */ + GString *resp = g_string_sized_new(4096); + g_string_append(resp, "B:"); + if (s->bb_num_samples > 0) { + uint8_t cur = s->bb_rbuf[0]; + uint32_t count = 1u; + for (uint32_t i = 1u; i < s->bb_num_samples; i++) { + if (s->bb_rbuf[i] == cur) { + count++; + } else { + g_string_append_printf(resp, "%u*%02x,", count, cur); + cur = s->bb_rbuf[i]; + count = 1u; + } + } + g_string_append_printf(resp, "%u*%02x", count, cur); + } + g_string_append(resp, "\r\n"); + + if (qemu_chr_fe_backend_connected(&s->chr)) { + qemu_chr_fe_write(&s->chr, (const uint8_t *)resp->str, (int)resp->len); + } + g_string_free(resp, TRUE); + + if (uses_spi_host1) { + ot_spi_host_clear_waveform(s->spi_host1); + } + if (active_i2c) { + ot_i2c_consume_override_waveform(active_i2c); + } + + g_free(s->bb_wbuf); + g_free(s->bb_rbuf); + s->bb_wbuf = NULL; + s->bb_rbuf = NULL; +} + +static void ot_gpio_eg_handle_bitbang_cmd(OtGpioEgState *s, const char *args) +{ + gchar **parts = g_strsplit(args, ":", 4); + g_assert(parts && parts[0] && parts[1] && parts[2] && parts[3]); + + uint64_t period_ns = g_ascii_strtoull(parts[0], NULL, 10); + uint32_t num_samples = (uint32_t)g_ascii_strtoull(parts[1], NULL, 10); + const char *pins_str = parts[2]; + const char *rle_str = parts[3]; + + g_assert(period_ns > 0 && num_samples > 0 && num_samples <= 10000000u); + + gchar **pin_tokens = g_strsplit(pins_str, ".", 32); + s->bb_npins = 0; + for (unsigned i = 0; pin_tokens && pin_tokens[i] && i < 32u; i++) { + unsigned pad = (unsigned)g_ascii_strtoull(pin_tokens[i], NULL, 10); + s->bb_pins[s->bb_npins++] = pad; + if (pad < PARAM_NUM_IO) { + s->data_bi &= ~(1u << pad); + } + } + g_strfreev(pin_tokens); + + g_free(s->bb_wbuf); + g_free(s->bb_rbuf); + s->bb_wbuf = g_malloc0(num_samples); + s->bb_rbuf = g_malloc0(num_samples); + s->bb_period_ns = period_ns; + s->bb_num_samples = num_samples; + s->bb_idx = 0; + s->bb_settle_ticks = 0; + s->bb_wait_pwm_ticks = 50000u; + s->bb_tail_ticks = 0; + + gchar **runs = g_strsplit(rle_str, ",", -1); + uint32_t widx = 0; + for (unsigned i = 0; runs && runs[i]; i++) { + char *star = strchr(runs[i], '*'); + g_assert(star != NULL); + *star = '\0'; + uint32_t count = (uint32_t)g_ascii_strtoull(runs[i], NULL, 10); + uint8_t val = (uint8_t)g_ascii_strtoull(star + 1, NULL, 16); + for (uint32_t k = 0; k < count && widx < num_samples; k++) { + s->bb_wbuf[widx++] = val; + } + } + g_strfreev(runs); + g_strfreev(parts); + + OtI2CState *target_i2c = ot_gpio_eg_get_active_target_i2c(s); + if (target_i2c) { + ot_i2c_reset_bitbang_target(target_i2c); + } + + s->bb_start_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + timer_mod_ns(s->bb_timer, s->bb_start_ns); +} static int ot_gpio_eg_chr_can_receive(void *opaque) { @@ -503,6 +1174,65 @@ static int ot_gpio_eg_chr_can_receive(void *opaque) return (int)sizeof(s->ibuf) - (int)s->ipos; } +uint64_t ot_gpio_eg_get_total_us(OtGpioEgState *s) +{ + if (!s) { + return 0; + } + if (s->explicit_time) { + return s->last_total_us; + } + uint64_t rt_us = + (uint64_t)(qemu_clock_get_ns(QEMU_CLOCK_REALTIME) / 1000LL); + s->last_total_us = MAX(s->last_total_us, rt_us + s->time_offset_us); + return s->last_total_us; +} + +void ot_gpio_eg_set_pattgen_batch(OtGpioEgState *s, bool active) +{ + if (!s) { + return; + } + s->batch_mode = active; + if (!active) { + if (s->batch_buf && s->batch_buf->len > 0) { + const uint8_t *ptr = (const uint8_t *)s->batch_buf->str; + size_t remaining = s->batch_buf->len; + while (remaining > 0) { + int chunk = (int)MIN(remaining, 4096u); + qemu_chr_fe_write(&s->chr, ptr, chunk); + ptr += chunk; + remaining -= (size_t)chunk; + } + g_string_set_size(s->batch_buf, 0); + } + uint64_t rt_us = + (uint64_t)(qemu_clock_get_ns(QEMU_CLOCK_REALTIME) / 1000LL); + s->time_offset_us = s->last_total_us - rt_us; + s->explicit_time = false; + } +} + +static void ot_gpio_eg_send_input_ack(OtGpioEgState *s) +{ + uint32_t now_us = (uint32_t)ot_gpio_eg_get_total_us(s); + char buf[32u]; + int len = snprintf(buf, sizeof(buf), "T:%08x\r\nY:%08x\r\n", now_us, + s->regs[R_DATA_IN]); + s->backend_state.in_m = s->regs[R_DATA_IN]; + int written = qemu_chr_fe_write(&s->chr, (const uint8_t *)buf, len); + s->pending_input_ack = (written != len); +} + +static void ot_gpio_eg_sync_timer_cb(void *opaque) +{ + OtGpioEgState *s = opaque; + ot_gpio_eg_update_backend(s, true); + char ack[16u]; + int len = snprintf(ack, sizeof(ack), "R:%08x\r\n", s->sync_seq); + qemu_chr_fe_write(&s->chr, (const uint8_t *)ack, len); +} + static void ot_gpio_eg_chr_receive(void *opaque, const uint8_t *buf, int size) { OtGpioEgState *s = opaque; @@ -519,41 +1249,55 @@ static void ot_gpio_eg_chr_receive(void *opaque, const uint8_t *buf, int size) for (;;) { const char *eol = memchr(s->ibuf, (int)'\n', s->ipos); if (!eol) { - if (s->ipos > 10u) { - /* discard any garbage */ + if (s->ipos >= sizeof(s->ibuf) - 1u) { + /* discard any buffer overflow */ memset(s->ibuf, 0, sizeof(s->ibuf)); s->ipos = 0; } return; } - unsigned eolpos = eol - s->ibuf; - if (eolpos < 10u) { - /* discard incomplete lines */ - memmove(s->ibuf, eol + 1u, eolpos + 1u); - s->ipos = 0; + unsigned eolpos = (unsigned)(eol - s->ibuf); + char *line = g_strchomp(g_strndup(s->ibuf, eolpos)); + + const char *next = eol + 1u; + unsigned rem = (unsigned)(&s->ibuf[s->ipos] - next); + memmove(s->ibuf, next, rem); + s->ipos = rem; + + if (line[0] == 'B' && line[1] == ':') { + ot_gpio_eg_handle_bitbang_cmd(s, line + 2); + g_free(line); continue; } + uint32_t data_in = 0; char cmd = '\0'; /* NOLINTNEXTLINE */ - int ret = sscanf(s->ibuf, "%c:%08x", &cmd, &data_in); - /* discard current command, even if invalid, up to first EOL */ - const char *next = eol + 1u; - unsigned rem = (unsigned)(&s->ibuf[s->ipos] - next); - memmove(s->ibuf, next, rem); - s->ipos = rem; + int ret = sscanf(line, "%c:%08x", &cmd, &data_in); + g_free(line); if (ret == 2) { trace_ot_gpio_backend_recv(s->ot_id, cmd, data_in); if (cmd == 'M') { s->data_bi = data_in; ot_gpio_eg_update_data_in(s); + ot_gpio_eg_send_input_ack(s); + ot_gpio_eg_update_backend(s, false); } else if (cmd == 'I') { s->data_ib = data_in; ot_gpio_eg_update_data_in(s); + ot_gpio_eg_send_input_ack(s); + ot_gpio_eg_update_backend(s, false); + } else if (cmd == 'P') { + s->host_pull = data_in; + ot_gpio_eg_update_data_in(s); + ot_gpio_eg_send_input_ack(s); + ot_gpio_eg_update_backend(s, false); } else if (cmd == 'R') { - ot_gpio_eg_update_backend(s, true); + s->sync_seq = data_in; + timer_mod(s->sync_timer, + qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 100000LL); } else { qemu_log_mask(LOG_UNIMP, "%s: unsupported command %c\n", __func__, cmd); @@ -578,7 +1322,7 @@ static void ot_gpio_eg_init_backend(OtGpioEgState *s) static void ot_gpio_eg_update_backend(OtGpioEgState *s, bool force) { - if (!qemu_chr_fe_backend_connected(&s->chr)) { + if (s->in_reset || !qemu_chr_fe_backend_connected(&s->chr)) { return; } @@ -587,22 +1331,102 @@ static void ot_gpio_eg_update_backend(OtGpioEgState *s, bool force) * Windows-style terminal. */ - uint32_t outv = s->data_out; + uint32_t periph_out_v = s->data_out; /* assume invert is performed on device output data, not on pull up/down */ - outv ^= s->invert; + periph_out_v ^= s->invert; - uint32_t out_en = s->data_oe; + uint32_t periph_out_en = s->data_oe; /* if open drain is active and output is high, disable output enable */ - out_en &= ~(s->opendrain & outv); + periph_out_en &= ~(s->opendrain & periph_out_v); + + /* Apply PINMUX sleep retention force overrides */ + periph_out_en = + (periph_out_en & ~s->force_en) | (s->force_oe & s->force_en); + periph_out_v = (periph_out_v & ~s->force_en) | (s->force_out & s->force_en); + + periph_out_v &= periph_out_en; + + uint32_t out_en = 0; + uint32_t out_v = 0; + uint32_t pull_en = 0; + uint32_t pull_v = 0; + uint32_t periph_pull_en = s->pull_en & ~s->force_en; + uint32_t periph_pull_v = s->pull_sel; + if (s->pinmux) { + for (unsigned p = 0; p < PARAM_NUM_IO; p++) { + if ((p == 14u || p == 15u) && s->sysrst_ctrl) { + bool host_low = + !((s->data_bi >> p) & 1u) && !((s->data_ib >> p) & 1u); + int drv = + ot_sysrst_ctrl_get_outsel_level(s->sysrst_ctrl, 86u + p); + out_en |= (1u << p); + if (drv != 0 && !host_low) { + out_v |= (1u << p); + } + continue; + } + uint32_t outsel = ot_pinmux_eg_get_gpio_outsel(s->pinmux, p); + int g_pull = ot_pinmux_eg_host_pin_to_gpio(s->pinmux, p); + unsigned gp = (g_pull >= 0 && g_pull < (int)PARAM_NUM_IO) ? + (unsigned)g_pull : + p; + if (g_pull >= 0 || (outsel >= 3u && outsel < 35u)) { + if ((periph_pull_en >> gp) & 1u) { + pull_en |= (1u << p); + } + if ((periph_pull_v >> gp) & 1u) { + pull_v |= (1u << p); + } + } + if ((s->force_en >> gp) & 1u) { + if ((s->force_oe >> gp) & 1u) { + out_en |= (1u << p); + } + if ((s->force_out >> gp) & 1u) { + out_v |= (1u << p); + } + } else if (outsel == 0u) { + out_en |= (1u << p); + } else if (outsel == 1u) { + out_en |= (1u << p); + out_v |= (1u << p); + } else if (outsel >= 49u && outsel <= 52u && s->pattgen) { + out_en |= (1u << p); + if (ot_pattgen_get_pin_level(s->pattgen, outsel - 49u)) { + out_v |= (1u << p); + } + } else if (outsel >= 72u && outsel <= 77u && s->sysrst_ctrl) { + int drv = + ot_sysrst_ctrl_get_outsel_level(s->sysrst_ctrl, outsel); + out_en |= (1u << p); + if (drv != 0) { + out_v |= (1u << p); + } + } else if (outsel >= 3u && outsel < 35u) { + unsigned g = outsel - 3u; + if ((periph_out_en >> g) & 1u) { + out_en |= (1u << p); + } + if ((periph_out_v >> g) & 1u) { + out_v |= (1u << p); + } + } + } + } else { + out_en = periph_out_en; + out_v = periph_out_v; + pull_en = periph_pull_en; + pull_v = periph_pull_v; + } - uint32_t active = s->pull_en | out_en; - outv &= out_en; + uint32_t active = pull_en | out_en; + out_v &= out_en; OtGpioEgBackendState bstate = { .hi_z = ~active, - .pull_v = s->pull_sel, + .pull_v = pull_v, .out_en = out_en, - .out_v = outv, + .out_v = out_v, .in_m = s->regs[R_DATA_IN] }; /* @@ -611,28 +1435,76 @@ static void ot_gpio_eg_update_backend(OtGpioEgState *s, bool force) */ if (!memcmp(&bstate, &s->backend_state, sizeof(OtGpioEgBackendState)) && - !force) { + !force && !s->pending_input_ack) { /* do not emit new state if nothing has changed */ return; } - char buf[64u]; + char buf[128u]; size_t len = 0; - len += snprintf(&buf[len], sizeof(buf) - len, "Z:%08x\r\n", bstate.hi_z); - len += snprintf(&buf[len], sizeof(buf) - len, "P:%08x\r\n", bstate.pull_v); - len += snprintf(&buf[len], sizeof(buf) - len, "D:%08x\r\n", bstate.out_en); - len += snprintf(&buf[len], sizeof(buf) - len, "O:%08x\r\n", bstate.out_v); - len += snprintf(&buf[len], sizeof(buf) - len, "Y:%08x\r\n", bstate.in_m); + uint32_t now_us = (uint32_t)ot_gpio_eg_get_total_us(s); + len += snprintf(&buf[len], sizeof(buf) - len, "T:%08x\r\n", now_us); + + if (force || bstate.hi_z != s->backend_state.hi_z) { + len += + snprintf(&buf[len], sizeof(buf) - len, "Z:%08x\r\n", bstate.hi_z); + } + if (force || bstate.pull_v != s->backend_state.pull_v) { + len += + snprintf(&buf[len], sizeof(buf) - len, "P:%08x\r\n", bstate.pull_v); + } + if (force || bstate.out_en != s->backend_state.out_en) { + len += + snprintf(&buf[len], sizeof(buf) - len, "D:%08x\r\n", bstate.out_en); + } + if (force || bstate.out_v != s->backend_state.out_v) { + len += + snprintf(&buf[len], sizeof(buf) - len, "O:%08x\r\n", bstate.out_v); + } + if (force || s->pending_input_ack || bstate.in_m != s->backend_state.in_m) { + len += + snprintf(&buf[len], sizeof(buf) - len, "Y:%08x\r\n", bstate.in_m); + } s->backend_state = bstate; - qemu_chr_fe_write(&s->chr, (const uint8_t *)buf, (int)len); - /* replace CRLF so that trace is kept on a single line */ - buf[10] = buf[22] = buf[34] = buf[46] = ','; - buf[11] = buf[23] = buf[35] = buf[47] = ' '; - buf[58] = '\0'; - trace_ot_gpio_backend_send(s->ot_id, buf); + if (len > 0) { + if (s->batch_mode && s->batch_buf) { + g_string_append_len(s->batch_buf, buf, (gssize)len); + s->pending_input_ack = false; + } else { + int written = + qemu_chr_fe_write(&s->chr, (const uint8_t *)buf, (int)len); + if (written == (int)len) { + s->pending_input_ack = false; + } + } + /* replace CRLF so that trace is kept on a single line */ + for (size_t i = 0; i + 1 < len; i++) { + if (buf[i] == '\r' && buf[i + 1] == '\n') { + if (i + 2 < len) { + buf[i] = ','; + buf[i + 1] = ' '; + } else { + buf[i] = '\0'; + } + } + } + trace_ot_gpio_backend_send(s->ot_id, buf); + } +} + +void ot_gpio_eg_notify_pattgen_change(OtGpioEgState *s, uint64_t step_us) +{ + if (s) { + if (step_us > 0) { + s->last_total_us = MAX(s->last_total_us, step_us); + s->explicit_time = true; + } + ot_gpio_eg_update_data_in(s); + ot_gpio_eg_update_backend(s, false); + } } static void ot_gpio_eg_chr_event_hander(void *opaque, QEMUChrEvent event) @@ -699,6 +1571,9 @@ static const MemoryRegionOps ot_gpio_eg_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.min_access_size = 1u, + .valid.max_access_size = 4u, + .valid.accepts = &ot_gpio_eg_accepts, }; static const Property ot_gpio_eg_properties[] = { @@ -708,6 +1583,18 @@ static const Property ot_gpio_eg_properties[] = { DEFINE_PROP_UINT32("oe", OtGpioEgState, reset_oe, 0u), DEFINE_PROP_BOOL("wipe", OtGpioEgState, wipe, false), DEFINE_PROP_CHR("chardev", OtGpioEgState, chr), + DEFINE_PROP_LINK("pinmux", OtGpioEgState, pinmux, TYPE_OT_PINMUX_EG, + OtPinmuxEgState *), + DEFINE_PROP_LINK("pattgen", OtGpioEgState, pattgen, TYPE_OT_PATTGEN, + OtPattgenState *), + DEFINE_PROP_LINK("pwm", OtGpioEgState, pwm, TYPE_OT_PWM, OtPwmState *), + DEFINE_PROP_LINK("spi-host1", OtGpioEgState, spi_host1, TYPE_OT_SPI_HOST, + OtSPIHostState *), + DEFINE_PROP_LINK("sysrst-ctrl", OtGpioEgState, sysrst_ctrl, + TYPE_OT_SYSRST_CTRL, OtSysrstCtrlState *), + DEFINE_PROP_LINK("i2c0", OtGpioEgState, i2c[0], TYPE_OT_I2C, OtI2CState *), + DEFINE_PROP_LINK("i2c1", OtGpioEgState, i2c[1], TYPE_OT_I2C, OtI2CState *), + DEFINE_PROP_LINK("i2c2", OtGpioEgState, i2c[2], TYPE_OT_I2C, OtI2CState *), }; static void ot_gpio_eg_reset_enter(Object *obj, ResetType type) @@ -720,8 +1607,18 @@ static void ot_gpio_eg_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } + timer_del(s->bb_timer); + g_free(s->bb_wbuf); + g_free(s->bb_rbuf); + s->bb_wbuf = NULL; + s->bb_rbuf = NULL; + + s->in_reset = true; + memset(s->regs, 0, sizeof(s->regs)); - memset(&s->backend_state, 0, sizeof(s->backend_state)); + if (s->wipe || type == RESET_TYPE_COLD) { + memset(&s->backend_state, 0, sizeof(s->backend_state)); + } /* reset_* fields are properties, never get reset */ s->data_ii = s->reset_in; @@ -733,6 +1630,9 @@ static void ot_gpio_eg_reset_enter(Object *obj, ResetType type) s->pull_sel = 0; s->invert = 0; s->connected = 0; + s->force_en = 0; + s->force_oe = 0; + s->force_out = 0; s->regs[R_DATA_IN] = s->reset_in; s->regs[R_DIRECT_OUT] = s->reset_out; @@ -765,9 +1665,11 @@ static void ot_gpio_eg_reset_exit(Object *obj, ResetType type) c->parent_phases.exit(obj, type); } + s->in_reset = false; + ot_gpio_eg_init_backend(s); ot_gpio_eg_update_data_out(s); - ot_gpio_eg_update_backend(s, true); + ot_gpio_eg_update_backend(s, s->wipe || type == RESET_TYPE_COLD); /* * do not reset the input backend buffer as external GPIO changes is fully @@ -810,9 +1712,16 @@ static void ot_gpio_eg_init(Object *obj) qdev_init_gpio_in_named(DEVICE(obj), &ot_gpio_eg_pad_attr_change, OT_PINMUX_PAD, PARAM_NUM_IO); + s->bb_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &ot_gpio_eg_bitbang_step, s); + s->sync_timer = + timer_new_ns(QEMU_CLOCK_VIRTUAL, &ot_gpio_eg_sync_timer_cb, s); + s->batch_buf = g_string_new(NULL); + /* Backend state persists across reset so initialise it once now */ s->data_ib = 0u; s->data_bi = UINT32_MAX; + s->host_pull = (1u << 7); + ot_gpio_eg_instance = s; } static void ot_gpio_eg_class_init(ObjectClass *klass, const void *data) diff --git a/hw/opentitan/ot_pattgen.c b/hw/opentitan/ot_pattgen.c index aa31f539381ad..b7d912b715fe3 100644 --- a/hw/opentitan/ot_pattgen.c +++ b/hw/opentitan/ot_pattgen.c @@ -12,6 +12,7 @@ #include "qemu/timer.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_gpio_eg.h" #include "hw/opentitan/ot_pattgen.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" @@ -125,10 +126,11 @@ static void ot_pattgen_timer_cb(void *opaque) } uint64_t base_us = - 10000ULL; + (s->gpio ? ot_gpio_eg_get_total_us(s->gpio) : 0ULL) + 10000ULL; uint32_t done_mask = 0; if (s->gpio) { + ot_gpio_eg_set_pattgen_batch(s->gpio, true); } while ((run_ch[0] && k[0] <= total_steps[0]) || @@ -165,10 +167,12 @@ static void ot_pattgen_timer_cb(void *opaque) } if (s->gpio) { + ot_gpio_eg_notify_pattgen_change(s->gpio, next_us); } } if (s->gpio) { + ot_gpio_eg_set_pattgen_batch(s->gpio, false); } if (done_mask) { @@ -268,6 +272,7 @@ static void ot_pattgen_write(void *opaque, hwaddr addr, uint64_t val64, } if (s->gpio) { + ot_gpio_eg_notify_pattgen_change(s->gpio, 0); } if (schedule_timer) { diff --git a/hw/opentitan/ot_pinmux_eg.c b/hw/opentitan/ot_pinmux_eg.c index ac6d8ad2f162a..f706b5ba43a1e 100644 --- a/hw/opentitan/ot_pinmux_eg.c +++ b/hw/opentitan/ot_pinmux_eg.c @@ -32,6 +32,7 @@ #include "qom/object.h" #include "hw/core/cpu.h" #include "hw/opentitan/ot_alert.h" +#include "hw/opentitan/ot_gpio_eg.h" #include "hw/opentitan/ot_pinmux_eg.h" #include "hw/opentitan/ot_rstmgr.h" #include "hw/opentitan/ot_sysrst_ctrl.h" @@ -476,7 +477,7 @@ static int ot_pinmux_eg_eval_mio_sel(const OtPinmuxEgState *s, uint32_t sel) } if (sel >= 2u && (sel - 2u) < PARAM_N_MIO_PADS) { unsigned mio_pad = sel - 2u; - int mio_in = -1; + int mio_in = ot_gpio_eg_get_mio_pad_in(mio_pad); if (mio_in >= 0) { return mio_in; } @@ -564,6 +565,7 @@ static void ot_pinmux_eg_update_gpio_inputs(OtPinmuxEgState *s) for (unsigned i = 0; i < 32u; i++) { ibex_irq_set(&s->gpio_inputs[i], -1); } + ot_gpio_eg_notify_sysrst_change(); } bool ot_pinmux_eg_trigger_mio_wkup(unsigned mio_pad) @@ -823,6 +825,7 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, ot_uart_update_pinmux(regs->mio_outsel, regs->mio_periph_insel); ot_pinmux_eg_update_pads(s); ot_pinmux_eg_update_gpio_inputs(s); + ot_gpio_eg_notify_sysrst_change(); } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", __func__, (uint32_t)addr); @@ -839,6 +842,7 @@ static void ot_pinmux_eg_regs_write(void *opaque, hwaddr addr, uint64_t val64, val32 &= MIO_PAD_ATTR_MASK; regs->mio_pad_attr[pad_no] = val32; ot_pinmux_eg_update_pads(s); + ot_gpio_eg_notify_sysrst_change(); } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: 0x%03x access is disabled\n", __func__, (uint32_t)addr); @@ -1043,6 +1047,7 @@ static void ot_pinmux_eg_reset_enter(Object *obj, ResetType type) ot_pinmux_eg_update_sysrst_inputs(s); ot_pinmux_eg_update_gpio_inputs(s); ot_pinmux_eg_update_pads(s); + ot_gpio_eg_notify_sysrst_change(); ibex_irq_set(&s->wkup, (int)(bool)regs->wkup_cause); ibex_irq_set(&s->alert, 0); return; diff --git a/hw/opentitan/ot_pwm.c b/hw/opentitan/ot_pwm.c index bc896b9825ca8..a68e5ad4620dc 100644 --- a/hw/opentitan/ot_pwm.c +++ b/hw/opentitan/ot_pwm.c @@ -12,6 +12,7 @@ #include "qemu/timer.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_gpio_eg.h" #include "hw/opentitan/ot_pwm.h" #include "hw/opentitan/ot_rstmgr.h" #include "hw/qdev-properties.h" @@ -290,6 +291,7 @@ static void ot_pwm_write(void *opaque, hwaddr addr, uint64_t val64, } } + ot_gpio_eg_notify_sysrst_change(); } static const MemoryRegionOps ot_pwm_regs_ops = { diff --git a/hw/opentitan/ot_sysrst_ctrl.c b/hw/opentitan/ot_sysrst_ctrl.c index 2c4f886266a2c..05d2c26cb706a 100644 --- a/hw/opentitan/ot_sysrst_ctrl.c +++ b/hw/opentitan/ot_sysrst_ctrl.c @@ -14,6 +14,7 @@ #include "hw/core/cpu.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_gpio_eg.h" #include "hw/opentitan/ot_pinmux_eg.h" #include "hw/opentitan/ot_rstmgr.h" #include "hw/opentitan/ot_sysrst_ctrl.h" @@ -262,6 +263,7 @@ static void ot_sysrst_ctrl_update_outputs(OtSysrstCtrlState *s) (s->regs[R_WKUP_STATUS] & R_WKUP_STATUS_WAKEUP_STS_MASK) != 0; ibex_irq_set(&s->wkup_req, (int)wkup_level); + ot_gpio_eg_notify_sysrst_change(); } static void ot_sysrst_ctrl_ec_rst_timer_cb(void *opaque) diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 060b5360e285c..4a43d0ce2c328 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -606,7 +606,49 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_EG_SOC_GPIO_SYSBUS_IRQ(29, PLIC, 66), OT_EG_SOC_GPIO_SYSBUS_IRQ(30, PLIC, 67), OT_EG_SOC_GPIO_SYSBUS_IRQ(31, PLIC, 68), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 0, PINMUX, OT_PINMUX_PAD_IN, 0), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 1, PINMUX, OT_PINMUX_PAD_IN, 1), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 2, PINMUX, OT_PINMUX_PAD_IN, 2), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 3, PINMUX, OT_PINMUX_PAD_IN, 3), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 4, PINMUX, OT_PINMUX_PAD_IN, 4), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 5, PINMUX, OT_PINMUX_PAD_IN, 5), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 6, PINMUX, OT_PINMUX_PAD_IN, 6), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 7, PINMUX, OT_PINMUX_PAD_IN, 7), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 8, PINMUX, OT_PINMUX_PAD_IN, 8), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 9, PINMUX, OT_PINMUX_PAD_IN, 9), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 10, PINMUX, OT_PINMUX_PAD_IN, 10), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 11, PINMUX, OT_PINMUX_PAD_IN, 11), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 12, PINMUX, OT_PINMUX_PAD_IN, 12), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 13, PINMUX, OT_PINMUX_PAD_IN, 13), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 14, PINMUX, OT_PINMUX_PAD_IN, 14), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 15, PINMUX, OT_PINMUX_PAD_IN, 15), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 16, PINMUX, OT_PINMUX_PAD_IN, 16), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 17, PINMUX, OT_PINMUX_PAD_IN, 17), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 18, PINMUX, OT_PINMUX_PAD_IN, 18), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 19, PINMUX, OT_PINMUX_PAD_IN, 19), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 20, PINMUX, OT_PINMUX_PAD_IN, 20), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 21, PINMUX, OT_PINMUX_PAD_IN, 21), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 22, PINMUX, OT_PINMUX_PAD_IN, 22), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 23, PINMUX, OT_PINMUX_PAD_IN, 23), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 24, PINMUX, OT_PINMUX_PAD_IN, 24), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 25, PINMUX, OT_PINMUX_PAD_IN, 25), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 26, PINMUX, OT_PINMUX_PAD_IN, 26), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 27, PINMUX, OT_PINMUX_PAD_IN, 27), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 28, PINMUX, OT_PINMUX_PAD_IN, 28), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 29, PINMUX, OT_PINMUX_PAD_IN, 29), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 30, PINMUX, OT_PINMUX_PAD_IN, 30), + OT_EG_SOC_SIGNAL(OT_GPIO_OUT, 31, PINMUX, OT_PINMUX_PAD_IN, 31), OT_EG_SOC_GPIO_ALERT(0, 4) + ), + .link = IBEXDEVICELINKDEFS( + OT_EG_SOC_DEVLINK("pinmux", PINMUX), + OT_EG_SOC_DEVLINK("pattgen", PATTGEN), + OT_EG_SOC_DEVLINK("pwm", PWM), + OT_EG_SOC_DEVLINK("spi-host1", SPI_HOST1), + OT_EG_SOC_DEVLINK("sysrst-ctrl", SYSRST_CTRL), + OT_EG_SOC_DEVLINK("i2c0", I2C0), + OT_EG_SOC_DEVLINK("i2c1", I2C1), + OT_EG_SOC_DEVLINK("i2c2", I2C2) ) }, [OT_EG_SOC_DEV_SPI_DEVICE] = { @@ -750,6 +792,9 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_EG_SOC_GPIO_SYSBUS_IRQ(0, PLIC, 122), OT_EG_SOC_GPIO_SYSBUS_IRQ(1, PLIC, 123), OT_EG_SOC_GPIO_ALERT(0, 9) + ), + .link = IBEXDEVICELINKDEFS( + OT_EG_SOC_DEVLINK("gpio", GPIO) ) }, [OT_EG_SOC_DEV_TIMER] = { diff --git a/include/hw/opentitan/ot_gpio_eg.h b/include/hw/opentitan/ot_gpio_eg.h index 782839fd7e85a..cf39e67b229c1 100644 --- a/include/hw/opentitan/ot_gpio_eg.h +++ b/include/hw/opentitan/ot_gpio_eg.h @@ -33,4 +33,10 @@ #define TYPE_OT_GPIO_EG "ot-gpio-eg" OBJECT_DECLARE_TYPE(OtGpioEgState, OtGpioEgClass, OT_GPIO_EG) +void ot_gpio_eg_notify_sysrst_change(void); +int ot_gpio_eg_get_mio_pad_in(unsigned mio_pad); +uint64_t ot_gpio_eg_get_total_us(OtGpioEgState *s); +void ot_gpio_eg_set_pattgen_batch(OtGpioEgState *s, bool active); +void ot_gpio_eg_notify_pattgen_change(OtGpioEgState *s, uint64_t step_us); + #endif /* HW_OPENTITAN_OT_GPIO_EG_H */ From 9ac400bdf3ce566c9546a0e4720acbc03a641a79 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:42 +0000 Subject: [PATCH 48/52] [ot] hw/opentitan: usbdev: implement setup, bulk/iso, and framing Align `ot_usbdev` with Earlgrey RTL (`usbdev.sv` / `usbdev_iomux.sv` / `usbdev_aon_wake.sv`): - Implement `PHY_PINS_SENSE` / `PHY_PINS_DRIVE` transceiver loopback, `pinmux-sense` QOM property (`ot_usbdev_set_pinmux_sense`), and `PHY_CONFIG` reset value (`eop_single_bit = 1`). - Implement the AON wake module (`WAKE_CONTROL`, `WAKE_EVENTS`) and wire the `OT_USBDEV_WKUP` wakeup signal to `ot_pwrmgr` in `ot_earlgrey.c`. - Enforce `AVSETUPBUFFER` (depth 4) / `AVOUTBUFFER` (depth 8) overflow (`INTR_STATE.av_overflow`), event counter field masks, and unclocked `USBDEV_BUFFER` SRAM access fault stalling. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I23c8953d85c4fa5161dd45df7dc2bcaf6a6a6964 --- hw/opentitan/ot_pinmux_eg.c | 3 +- hw/opentitan/ot_rstmgr.c | 5 +- hw/opentitan/ot_usbdev.c | 779 +++++++++++++++++++++++++------ hw/riscv/ot_earlgrey.c | 5 +- include/hw/opentitan/ot_usbdev.h | 5 + 5 files changed, 641 insertions(+), 156 deletions(-) diff --git a/hw/opentitan/ot_pinmux_eg.c b/hw/opentitan/ot_pinmux_eg.c index f706b5ba43a1e..a2c86f0b1a116 100644 --- a/hw/opentitan/ot_pinmux_eg.c +++ b/hw/opentitan/ot_pinmux_eg.c @@ -37,6 +37,7 @@ #include "hw/opentitan/ot_rstmgr.h" #include "hw/opentitan/ot_sysrst_ctrl.h" #include "hw/opentitan/ot_uart.h" +#include "hw/opentitan/ot_usbdev.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" #include "hw/riscv/ibex_common.h" @@ -556,7 +557,7 @@ void ot_pinmux_eg_update_sysrst_inputs(OtPinmuxEgState *s) } else if (usb_sel == 1u) { usb_sense = 1; } - (void)usb_sense; + ot_usbdev_set_pinmux_sense(usb_sense); ot_pinmux_eg_eval_wkup_detectors(s); } diff --git a/hw/opentitan/ot_rstmgr.c b/hw/opentitan/ot_rstmgr.c index f6d7d8f366f33..da859cbac82c3 100644 --- a/hw/opentitan/ot_rstmgr.c +++ b/hw/opentitan/ot_rstmgr.c @@ -44,6 +44,7 @@ #include "hw/opentitan/ot_rstmgr.h" #include "hw/opentitan/ot_spi_device.h" #include "hw/opentitan/ot_spi_host.h" +#include "hw/opentitan/ot_usbdev.h" #include "hw/qdev-core.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" @@ -381,7 +382,9 @@ static int ot_rstmgr_sw_rst_walker(DeviceState *dev, void *opaque) trace_ot_rstmgr_sw_rst(desc->ot_id, desc->path, desc->reset); - if (desc->reset) { + if (desc->aon) { + ot_usbdev_aon_reset(OT_USBDEV(dev), desc->reset); + } else if (desc->reset) { resettable_assert_reset(OBJECT(dev), RESET_TYPE_WAKEUP); } else { resettable_release_reset(OBJECT(dev), RESET_TYPE_WAKEUP); diff --git a/hw/opentitan/ot_usbdev.c b/hw/opentitan/ot_usbdev.c index 28d8d082bdb7a..cc1c404864047 100644 --- a/hw/opentitan/ot_usbdev.c +++ b/hw/opentitan/ot_usbdev.c @@ -37,14 +37,17 @@ #include "qemu/error-report.h" #include "qemu/fifo8.h" #include "qemu/log.h" +#include "qapi/error.h" #include "chardev/char-fe.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_fifo32.h" +#include "hw/opentitan/ot_rstmgr.h" #include "hw/opentitan/ot_usbdev.h" #include "hw/qdev-properties-system.h" #include "hw/qdev-properties.h" #include "hw/registerfields.h" +#include "hw/riscv/ibex_clock_src.h" #include "hw/riscv/ibex_common.h" #include "hw/riscv/ibex_irq.h" #include "trace.h" @@ -263,10 +266,12 @@ REG32(COUNT_ERRORS, 0xa8u) USBDEV_INTR_FRAME_MASK | USBDEV_INTR_POWERED_MASK | \ USBDEV_INTR_LINK_OUT_ERR_MASK) -#define USBDEV_INTR_MASK \ - (USBDEV_INTR_RW1C_MASK | USBDEV_INTR_PKT_RECEIVED_MASK | \ - USBDEV_INTR_PKT_SENT_MASK | USBDEV_INTR_AV_OUT_EMPTY_MASK | \ - USBDEV_INTR_RX_FULL_MASK | USBDEV_INTR_AV_SETUP_EMPTY_MASK) +#define USBDEV_INTR_STATUS_MASK \ + (USBDEV_INTR_PKT_RECEIVED_MASK | USBDEV_INTR_PKT_SENT_MASK | \ + USBDEV_INTR_AV_OUT_EMPTY_MASK | USBDEV_INTR_RX_FULL_MASK | \ + USBDEV_INTR_AV_SETUP_EMPTY_MASK) + +#define USBDEV_INTR_MASK (USBDEV_INTR_RW1C_MASK | USBDEV_INTR_STATUS_MASK) #define USBDEV_USBCTRL_R_MASK \ (R_USBCTRL_ENABLE_MASK | R_USBCTRL_DEVICE_ADDRESS_MASK) @@ -507,6 +512,7 @@ typedef struct { bool client_connected; /* We have a client */ bool vbus_connected; /* The host has turned on VBUS */ bool hello_done; /* Have received a HELLO? */ + bool device_connected; /* Have reported CONNECT to the client? */ } OtUsbdevServer; struct OtUsbdevState { @@ -523,9 +529,12 @@ struct OtUsbdevState { char *usbclk_name; /* Name of the AON clock. */ char *aonclk_name; + unsigned pclk; + const char *clock_src_name; IbexIRQ irqs[USBDEV_INTR_NUM]; IbexIRQ alert; + IbexIRQ wkup; /* Register content */ uint32_t regs[REGS_COUNT]; @@ -558,6 +567,8 @@ struct OtUsbdevState { * it means that a bus reset signalling is in progress. */ QEMUTimer bus_reset_timer; + QEMUTimer usb_pace_timer; + int64_t usb_pace_until_ns; /* * Timer to handle frame counting. While this timer is pending, @@ -606,8 +617,18 @@ struct OtUsbdevState { bool vbus_override; /* VBUS gate: meaning depends on the vbus_override mode */ bool vbus_gate; + /* PINMUX MioInUsbdevSense (56): 0=ConstantZero, 1=ConstantOne, -1=pad */ + int pinmux_vbus_sense; + /* AON wake latched pullup and event states (u_usbdev_aon_wake / + * u_wake_events_cdc) */ + bool aon_dppullup_en; + bool aon_dnpullup_en; + uint32_t aon_wake_events; + uint32_t aon_cdc_dst_qs; }; +static OtUsbdevState *ot_usbdev_instance; + #define REG_NAME(_reg_) \ ((((_reg_) < REGS_COUNT) && REG_NAMES[_reg_]) ? REG_NAMES[_reg_] : "?") @@ -685,10 +706,10 @@ ot_usbdev_cancel_all_transfers(OtUsbdevState *s, const char *reason); static void ot_usbdev_update_status_irqs(OtUsbdevState *s) { uint32_t set_mask = 0u; + if (s->regs[R_IN_SENT]) { + set_mask |= USBDEV_INTR_PKT_SENT_MASK; + } if (ot_usbdev_is_enabled(s)) { - if (s->regs[R_IN_SENT]) { - set_mask |= USBDEV_INTR_PKT_SENT_MASK; - } if (fifo8_is_empty(&s->av_setup_fifo)) { set_mask |= USBDEV_INTR_AV_SETUP_EMPTY_MASK; } @@ -706,7 +727,9 @@ static void ot_usbdev_update_status_irqs(OtUsbdevState *s) * @todo The BFM says that 'Disconnected' is held at 1 during IP * reset, need to investigate this detail. */ - s->regs[R_USBDEV_INTR_STATE] |= set_mask; + s->regs[R_USBDEV_INTR_STATE] = + (s->regs[R_USBDEV_INTR_STATE] & ~USBDEV_INTR_STATUS_MASK) | set_mask | + (s->regs[R_USBDEV_INTR_TEST] & USBDEV_INTR_STATUS_MASK); } /* @@ -750,7 +773,7 @@ static void ot_usbdev_update_alerts(OtUsbdevState *s) * * @return true if enabled (USBCTRL.ENABLE is set). */ -bool ot_usbdev_is_enabled(const OtUsbdevState *s) +static bool ot_usbdev_is_enabled(const OtUsbdevState *s) { /* * Note: this works even if the device is in reset because @@ -772,6 +795,133 @@ static bool ot_usbdev_has_vbus(const OtUsbdevState *s) return FIELD_EX32(s->regs[R_USBSTAT], USBSTAT, SENSE); } +static void ot_usbdev_get_pullups(const OtUsbdevState *s, bool *dp_pullup, + bool *dn_pullup) +{ + bool usbdev_dp_pullup; + bool usbdev_dn_pullup; + + if (s->regs[R_PHY_PINS_DRIVE] & R_PHY_PINS_DRIVE_EN_MASK) { + usbdev_dp_pullup = (bool)FIELD_EX32(s->regs[R_PHY_PINS_DRIVE], + PHY_PINS_DRIVE, DP_PULLUP_EN_O); + usbdev_dn_pullup = (bool)FIELD_EX32(s->regs[R_PHY_PINS_DRIVE], + PHY_PINS_DRIVE, DN_PULLUP_EN_O); + } else { + bool pullup_en = ot_usbdev_is_enabled(s) && ot_usbdev_has_vbus(s); + bool pinflip = + (bool)FIELD_EX32(s->regs[R_PHY_CONFIG], PHY_CONFIG, PINFLIP); + usbdev_dp_pullup = !pinflip && pullup_en; + usbdev_dn_pullup = pinflip && pullup_en; + } + + if (s->regs[R_WAKE_EVENTS] & R_WAKE_EVENTS_MODULE_ACTIVE_MASK) { + *dp_pullup = s->aon_dppullup_en; + *dn_pullup = s->aon_dnpullup_en; + } else { + *dp_pullup = usbdev_dp_pullup; + *dn_pullup = usbdev_dn_pullup; + } +} + +static uint32_t ot_usbdev_read_phy_pins_sense(const OtUsbdevState *s) +{ + bool dp_pullup; + bool dn_pullup; + ot_usbdev_get_pullups(s, &dp_pullup, &dn_pullup); + bool line_not_se0 = dp_pullup || dn_pullup; + + bool pinflip = (bool)FIELD_EX32(s->regs[R_PHY_CONFIG], PHY_CONFIG, PINFLIP); + uint32_t tx_dp_i = pinflip ? 0u : 1u; + uint32_t tx_dn_i = pinflip ? 1u : 0u; + uint32_t tx_d_i = pinflip ? 0u : 1u; + uint32_t tx_se0_i = 0u; + uint32_t tx_oe_i = + (FIELD_EX32(s->regs[R_PHY_CONFIG], PHY_CONFIG, TX_OSC_TEST_MODE) && + line_not_se0 && !timer_pending(&s->bus_reset_timer)) ? + 1u : + 0u; + + bool drive_en = + (bool)(s->regs[R_PHY_PINS_DRIVE] & R_PHY_PINS_DRIVE_EN_MASK); + bool link_suspend = (FIELD_EX32(s->regs[R_USBSTAT], USBSTAT, LINK_STATE) == + OT_USBDEV_LINK_STATE_SUSPENDED); + bool usb_rx_enable = + drive_en ? + (bool)FIELD_EX32(s->regs[R_PHY_PINS_DRIVE], PHY_PINS_DRIVE, + RX_ENABLE_O) : + (FIELD_EX32(s->regs[R_PHY_CONFIG], PHY_CONFIG, USE_DIFF_RCVR) && + !link_suspend); + + uint32_t usb_tx_dp_o = + drive_en ? FIELD_EX32(s->regs[R_PHY_PINS_DRIVE], PHY_PINS_DRIVE, DP_O) : + tx_dp_i; + uint32_t usb_tx_dn_o = + drive_en ? FIELD_EX32(s->regs[R_PHY_PINS_DRIVE], PHY_PINS_DRIVE, DN_O) : + tx_dn_i; + uint32_t usb_tx_oe_o = + drive_en ? FIELD_EX32(s->regs[R_PHY_PINS_DRIVE], PHY_PINS_DRIVE, OE_O) : + tx_oe_i; + + uint32_t rx_dp; + uint32_t rx_dn; + if (usb_tx_oe_o) { + rx_dp = usb_tx_dp_o; + rx_dn = usb_tx_dn_o; + } else { + rx_dp = dp_pullup ? 1u : 0u; + rx_dn = dn_pullup ? 1u : 0u; + } + + uint32_t rx_d = (usb_rx_enable && rx_dp && !rx_dn) ? 1u : 0u; + uint32_t pwr_sense = ot_usbdev_has_vbus(s) ? 1u : 0u; + + uint32_t val32 = 0u; + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, RX_DP_I, rx_dp); + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, RX_DN_I, rx_dn); + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, RX_D_I, rx_d); + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, TX_DP_O, tx_dp_i); + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, TX_DN_O, tx_dn_i); + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, TX_D_O, tx_d_i); + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, TX_SE0_O, tx_se0_i); + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, TX_OE_O, tx_oe_i); + val32 = FIELD_DP32(val32, PHY_PINS_SENSE, PWR_SENSE, pwr_sense); + return val32; +} + +static void ot_usbdev_update_aon_wake(OtUsbdevState *s) +{ + if (!(s->regs[R_WAKE_EVENTS] & R_WAKE_EVENTS_MODULE_ACTIVE_MASK)) { + return; + } + + uint32_t pins_sense = ot_usbdev_read_phy_pins_sense(s); + bool usb_dp_i = (bool)FIELD_EX32(pins_sense, PHY_PINS_SENSE, RX_DP_I); + bool usb_dn_i = (bool)FIELD_EX32(pins_sense, PHY_PINS_SENSE, RX_DN_I); + bool usb_sense_i = (bool)FIELD_EX32(pins_sense, PHY_PINS_SENSE, PWR_SENSE); + + bool not_idle = + (usb_dp_i != s->aon_dppullup_en) || (usb_dn_i != s->aon_dnpullup_en); + bool se0 = !usb_dp_i && !usb_dn_i; + bool sense_lost = !usb_sense_i; + + if (not_idle) { + s->regs[R_WAKE_EVENTS] |= R_WAKE_EVENTS_BUS_NOT_IDLE_MASK; + } + if (se0) { + s->regs[R_WAKE_EVENTS] |= R_WAKE_EVENTS_BUS_RESET_MASK; + } + if (sense_lost) { + s->regs[R_WAKE_EVENTS] |= R_WAKE_EVENTS_DISCONNECTED_MASK; + } + + if (not_idle || se0 || sense_lost) { + ibex_irq_set(&s->wkup, 1); + } + + s->aon_wake_events = s->regs[R_WAKE_EVENTS]; + s->aon_cdc_dst_qs = s->aon_wake_events; +} + /* * Return the current link state. * @@ -991,7 +1141,8 @@ static void ot_usbdev_update_fifos_status(OtUsbdevState *s) { uint32_t val = s->regs[R_USBSTAT]; val = FIELD_DP32(val, USBSTAT, RX_EMPTY, - (uint32_t)ot_fifo32_is_empty(&s->rx_fifo)); + (uint32_t)(ot_usbdev_is_enabled(s) && + ot_fifo32_is_empty(&s->rx_fifo))); val = FIELD_DP32(val, USBSTAT, AV_SETUP_FULL, (uint32_t)fifo8_is_full(&s->av_setup_fifo)); val = FIELD_DP32(val, USBSTAT, RX_DEPTH, @@ -1023,12 +1174,21 @@ static void ot_usbdev_update_vbus(OtUsbdevState *s) /* * In VBUS override mode, VBUS sense is directly equal to - * the VBUS gate. Otherwise, it is the AND between the gate + * the VBUS gate. Otherwise, it is driven by PINMUX MioInUsbdevSense + * (when set to ConstantOne/ConstantZero) or the AND between the gate * and the host VBUS control. */ - bool vbus_sense = s->vbus_gate; - if (!s->vbus_override) { - vbus_sense = vbus_sense && s->usb_server.vbus_connected; + bool vbus_sense; + if (s->pinmux_vbus_sense == 1 && !s->usb_server.client_connected) { + vbus_sense = true; + } else if (s->pinmux_vbus_sense == 0 && !s->usb_server.vbus_connected && + !s->vbus_override) { + vbus_sense = false; + } else { + vbus_sense = s->vbus_gate; + if (!s->vbus_override) { + vbus_sense = vbus_sense && s->usb_server.vbus_connected; + } } bool old_vbus_sense = (bool)FIELD_EX32(s->regs[R_USBSTAT], USBSTAT, SENSE); @@ -1053,30 +1213,56 @@ static void ot_usbdev_update_vbus(OtUsbdevState *s) OT_USBDEV_LINK_STATE_DISCONNECTED); s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_POWERED_MASK; - if (ot_usbdev_is_enabled(s)) { + if (ot_usbdev_is_enabled(s) && + !(s->regs[R_WAKE_EVENTS] & R_WAKE_EVENTS_MODULE_ACTIVE_MASK)) { ot_usbdev_set_raw_link_state(s, OT_USBDEV_LINK_STATE_POWERED); ot_usbdev_server_report_connected(s, true); } } /* VBUS was turned off */ else { + if (s->regs[R_WAKE_EVENTS] & R_WAKE_EVENTS_MODULE_ACTIVE_MASK) { + s->regs[R_WAKE_EVENTS] |= R_WAKE_EVENTS_DISCONNECTED_MASK; + s->aon_wake_events = s->regs[R_WAKE_EVENTS]; + s->aon_cdc_dst_qs = s->aon_wake_events; + ibex_irq_set(&s->wkup, 1); + } + ot_usbdev_update_fifos_status(s); /* See BFM (bus_disconnect) */ if (ot_usbdev_get_link_state(s) != OT_USBDEV_LINK_STATE_DISCONNECTED) { ot_usbdev_set_raw_link_state(s, OT_USBDEV_LINK_STATE_DISCONNECTED); s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_DISCONNECTED_MASK; - s->regs[R_USBCTRL] = - FIELD_DP32(s->regs[R_USBCTRL], USBCTRL, DEVICE_ADDRESS, 0u); + bool dp_pullup; + bool dn_pullup; + ot_usbdev_get_pullups(s, &dp_pullup, &dn_pullup); + if (!ot_usbdev_is_enabled(s) || (!dp_pullup && !dn_pullup)) { + s->regs[R_USBCTRL] = + FIELD_DP32(s->regs[R_USBCTRL], USBCTRL, DEVICE_ADDRESS, 0u); + } ot_usbdev_cancel_all_transfers(s, "VBUS was disconnected"); /* If there is no host then any SOF or reset signalling stops. */ timer_del(&s->frame_timer); timer_del(&s->bus_reset_timer); + ot_usbdev_server_report_connected(s, false); } } ot_usbdev_update_irqs(s); } +void ot_usbdev_set_pinmux_sense(int level) +{ + OtUsbdevState *s = ot_usbdev_instance; + if (!s) { + return; + } + if (s->pinmux_vbus_sense != level) { + s->pinmux_vbus_sense = level; + ot_usbdev_update_vbus(s); + } +} + /* * Update the VBUS gate. * @@ -1190,6 +1376,7 @@ static void ot_usbdev_link_reset_complete(void *opaque) s->frozen_frame = 0u; ot_usbdev_schedule_frame_timer(s); } + qemu_chr_fe_accept_input(&s->usb_chr); } /* @@ -1198,9 +1385,58 @@ static void ot_usbdev_link_reset_complete(void *opaque) * This function will trigger all necessary state and IRQs changes necessary to * perform a link reset. */ +static void ot_usbdev_simulate_link_suspend(OtUsbdevState *s) +{ + g_assert(!resettable_is_in_reset(OBJECT(s))); + OtUsbdevLinkState link_state = ot_usbdev_get_link_state(s); + if (link_state != OT_USBDEV_LINK_STATE_DISCONNECTED) { + ot_usbdev_set_raw_link_state(s, link_state == + OT_USBDEV_LINK_STATE_POWERED ? + OT_USBDEV_LINK_STATE_POWERED_SUSP : + OT_USBDEV_LINK_STATE_SUSPENDED); + } + timer_del(&s->frame_timer); + s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_LINK_SUSPEND_MASK; + ot_usbdev_update_irqs(s); +} + +static void ot_usbdev_simulate_link_resume(OtUsbdevState *s) +{ + g_assert(!resettable_is_in_reset(OBJECT(s))); + if (s->regs[R_WAKE_EVENTS] & R_WAKE_EVENTS_MODULE_ACTIVE_MASK) { + s->regs[R_WAKE_EVENTS] |= R_WAKE_EVENTS_BUS_NOT_IDLE_MASK; + s->aon_wake_events = s->regs[R_WAKE_EVENTS]; + s->aon_cdc_dst_qs = s->aon_wake_events; + ibex_irq_set(&s->wkup, 1); + } + OtUsbdevLinkState link_state = ot_usbdev_get_link_state(s); + if (link_state == OT_USBDEV_LINK_STATE_POWERED_SUSP || + link_state == OT_USBDEV_LINK_STATE_SUSPENDED || + link_state == OT_USBDEV_LINK_STATE_RESUMING) { + ot_usbdev_set_raw_link_state(s, + link_state == + OT_USBDEV_LINK_STATE_POWERED_SUSP ? + OT_USBDEV_LINK_STATE_POWERED : + OT_USBDEV_LINK_STATE_ACTIVE); + s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_LINK_RESUME_MASK; + int64_t now = qemu_clock_get_us(OT_VIRTUAL_CLOCK); + timer_mod(&s->frame_timer, now + OT_USBDEV_FRAME_TIME_US); + } + ot_usbdev_update_irqs(s); +} + static void ot_usbdev_simulate_link_reset(OtUsbdevState *s) { g_assert(!resettable_is_in_reset(OBJECT(s))); + if (s->regs[R_WAKE_EVENTS] & R_WAKE_EVENTS_MODULE_ACTIVE_MASK) { + s->regs[R_WAKE_EVENTS] |= + R_WAKE_EVENTS_BUS_RESET_MASK | R_WAKE_EVENTS_BUS_NOT_IDLE_MASK; + s->aon_wake_events = s->regs[R_WAKE_EVENTS]; + s->aon_cdc_dst_qs = s->aon_wake_events; + ibex_irq_set(&s->wkup, 1); + ot_usbdev_server_report_connected(s, false); + return; + } if (timer_pending(&s->bus_reset_timer)) { error_report("%s: %s: Simulating a link reset while a bus reset is " "already pending!", @@ -1220,6 +1456,8 @@ static void ot_usbdev_simulate_link_reset(OtUsbdevState *s) /* See BFM (bus_reset) */ s->regs[R_USBCTRL] = FIELD_DP32(s->regs[R_USBCTRL], USBCTRL, DEVICE_ADDRESS, 0u); + s->regs[R_OUT_DATA_TOGGLE] = 0u; + s->regs[R_IN_DATA_TOGGLE] = 0u; s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_LINK_RESET_MASK; ot_usbdev_update_irqs(s); @@ -1241,6 +1479,19 @@ static void ot_usbdev_simulate_link_reset(OtUsbdevState *s) timer_mod(&s->bus_reset_timer, now + OT_USBDEV_BUS_RESET_TIME_US); } +static void ot_usbdev_pace_timer_expired(void *opaque) +{ + OtUsbdevState *s = opaque; + qemu_chr_fe_accept_input(&s->usb_chr); +} + +static void ot_usbdev_pace_usb_chr(OtUsbdevState *s, int64_t delay_ns) +{ + int64_t deadline = qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + delay_ns; + s->usb_pace_until_ns = deadline; + timer_mod(&s->usb_pace_timer, deadline); +} + /* * Simulate the reception of a SETUP token followed by an 8-byte * DATA packet containing the SETUP packet. @@ -1327,6 +1578,7 @@ static void ot_usbdev_simulate_setup(OtUsbdevState *s, uint8_t ep, ot_usbdev_complete_transfer_out(s, ep, OT_USBDEV_SERVER_STATUS_CANCELLED, "cancelled by setup packet"); + ot_usbdev_pace_usb_chr(s, 500000LL); ot_usbdev_update_fifos_status(s); ot_usbdev_update_irqs(s); } @@ -1458,12 +1710,9 @@ static void ot_usbdev_advance_transfer_in(OtUsbdevState *s, uint8_t epnum) } /* - * If the endpoint is not enabled, the device will silently ignore the - * packet, causing an error. + * If the endpoint is not enabled yet, wait until software enables it. */ if (!ot_usbdev_is_ep_in_enabled(s, epnum)) { - ot_usbdev_complete_transfer_in(s, epnum, OT_USBDEV_SERVER_STATUS_ERROR, - "endpoint IN not enabled"); return; } @@ -1503,9 +1752,12 @@ static void ot_usbdev_advance_transfer_in(OtUsbdevState *s, uint8_t epnum) /* Mark the packet as sent and acknowledged by the host */ configin = SHARED_FIELD_DP32(configin, CONFIGIN_RDY, 0u); s->regs[reg] = configin; - s->regs[R_IN_DATA_TOGGLE] ^= - FIELD_DP32(0u, IN_DATA_TOGGLE, STATUS, 1u << epnum); + if (!(s->regs[R_IN_ISO] & (1u << epnum))) { + s->regs[R_IN_DATA_TOGGLE] ^= + FIELD_DP32(0u, IN_DATA_TOGGLE, STATUS, 1u << epnum); + } s->regs[R_IN_SENT] |= 1u << epnum; + ot_usbdev_pace_usb_chr(s, 500000LL); ot_usbdev_update_irqs(s); /* Check for overflow */ @@ -1555,21 +1807,18 @@ static bool ot_usbdev_advance_transfer_out(OtUsbdevState *s, uint8_t epnum) g_assert(epnum < USBDEV_PARAM_N_ENDPOINTS); /* See BFM (out_packet) */ - /* Nothing to do if there is no pending transfer */ OtUsbdevServer *server = &s->usb_server; OtUsbdevServerEpOutXfer *xfer = &server->ep[epnum].out; - if (!xfer->pending) { - return false; - } + bool progressed = false; +again: /* - * If the endpoint OUT is not enabled, the device will silently ignore the - * packet, causing an error. + * If there is no pending transfer, or the endpoint OUT is not enabled yet, + * we cannot proceed yet. */ - if (!ot_usbdev_is_ep_out_enabled(s, epnum)) { - ot_usbdev_complete_transfer_out(s, epnum, OT_USBDEV_SERVER_STATUS_ERROR, - "endpoint OUT not enabled"); - return false; + if (!xfer->pending || !ot_usbdev_is_ep_out_enabled(s, epnum) || + (epnum == 0u && (s->regs[R_IN_SENT] & 1u) != 0u)) { + return progressed; } /* Check for STALL */ @@ -1579,13 +1828,13 @@ static bool ot_usbdev_advance_transfer_out(OtUsbdevState *s, uint8_t epnum) "stalled by device"); s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_LINK_OUT_ERR_MASK; ot_usbdev_update_irqs(s); - return false; + return progressed; } /* * Only accept the DATA packet if the endpoint is enabled, and there is - * buffer available and there is space in the RX FIFO. The last entry of the - * FIFO is reserved for SETUP packets. + * buffer available and there is space in the RX FIFO. The last entry of + * the FIFO is reserved for SETUP packets. */ if (!ot_usbdev_is_ep_out_rxenabled(s, epnum) || fifo8_is_empty(&s->av_out_fifo) || @@ -1594,7 +1843,7 @@ static bool ot_usbdev_advance_transfer_out(OtUsbdevState *s, uint8_t epnum) s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_LINK_OUT_ERR_MASK; ot_usbdev_update_irqs(s); /* "NAK" packet, OUT transfer will be retried */ - return false; + return progressed; } /* Write data to buffer and update transfer status */ @@ -1612,8 +1861,10 @@ static bool ot_usbdev_advance_transfer_out(OtUsbdevState *s, uint8_t epnum) rx_fifo_entry = FIELD_DP32(rx_fifo_entry, RXFIFO, SIZE, size); ot_fifo32_push(&s->rx_fifo, rx_fifo_entry); - s->regs[R_OUT_DATA_TOGGLE] ^= - FIELD_DP32(0u, IN_DATA_TOGGLE, STATUS, 1u << epnum); + if (epnum == 0u || !(s->regs[R_OUT_ISO] & (1u << epnum))) { + s->regs[R_OUT_DATA_TOGGLE] ^= + FIELD_DP32(0u, OUT_DATA_TOGGLE, STATUS, 1u << epnum); + } trace_ot_usbdev_packet_received(s->ot_id, epnum, buf_id, size); @@ -1622,8 +1873,10 @@ static bool ot_usbdev_advance_transfer_out(OtUsbdevState *s, uint8_t epnum) s->regs[R_RXENABLE_OUT] &= ~(1u << epnum); } + ot_usbdev_pace_usb_chr(s, 500000LL); ot_usbdev_update_fifos_status(s); ot_usbdev_update_irqs(s); + progressed = true; /* Handle completion */ if (xfer->send_rem == 0 && !xfer->send_zlp) { @@ -1640,7 +1893,7 @@ static bool ot_usbdev_advance_transfer_out(OtUsbdevState *s, uint8_t epnum) xfer->send_zlp = false; } - return true; + goto again; } /* @@ -1658,48 +1911,68 @@ static void ot_usbdev_write_usbctrl(OtUsbdevState *s, uint32_t val32) bool old_enable = ot_usbdev_is_enabled(s); uint8_t old_addr = ot_usbdev_get_address(s); - /* @todo Handle resume_link_active eventually, this is a W/O field */ + /* Handle resume_link_active (W/O pulse) */ s->regs[R_USBCTRL] = val32 & USBDEV_USBCTRL_R_MASK; + bool enable = ot_usbdev_is_enabled(s); + if (!enable) { + s->regs[R_USBCTRL] = + FIELD_DP32(s->regs[R_USBCTRL], USBCTRL, DEVICE_ADDRESS, 0u); + } + uint8_t new_addr = ot_usbdev_get_address(s); if (old_addr != new_addr) { trace_ot_usbdev_address_changed(s->ot_id, new_addr); } - bool enable = ot_usbdev_is_enabled(s); - if (enable == old_enable) { + if (s->regs[R_WAKE_EVENTS] & R_WAKE_EVENTS_MODULE_ACTIVE_MASK) { + if (enable && ot_usbdev_has_vbus(s) && + !(s->regs[R_WAKE_EVENTS] & (R_WAKE_EVENTS_BUS_RESET_MASK | + R_WAKE_EVENTS_DISCONNECTED_MASK)) && + ot_usbdev_get_link_state(s) == OT_USBDEV_LINK_STATE_DISCONNECTED) { + ot_usbdev_set_raw_link_state(s, OT_USBDEV_LINK_STATE_POWERED); + } + ot_usbdev_update_fifos_status(s); return; } - trace_ot_usbdev_enable_changed(s->ot_id, enable); + if (enable != old_enable || + (enable && ot_usbdev_has_vbus(s) && + ot_usbdev_get_link_state(s) == OT_USBDEV_LINK_STATE_DISCONNECTED)) { + trace_ot_usbdev_enable_changed(s->ot_id, enable); - /* Device has been enabled */ - if (enable) { - /* See BFM (set_enable) */ - g_assert(ot_usbdev_get_link_state(s) == - OT_USBDEV_LINK_STATE_DISCONNECTED); - if (ot_usbdev_has_vbus(s)) { - ot_usbdev_set_raw_link_state(s, OT_USBDEV_LINK_STATE_POWERED); - ot_usbdev_server_report_connected(s, true); - } - } else { - /* See BFM (set_enable) */ - ot_usbdev_set_raw_link_state(s, OT_USBDEV_LINK_STATE_DISCONNECTED); - s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_DISCONNECTED_MASK; - s->regs[R_USBCTRL] = - FIELD_DP32(s->regs[R_USBCTRL], USBCTRL, DEVICE_ADDRESS, 0u); + /* Device has been enabled */ + if (enable) { + /* See BFM (set_enable) */ + g_assert(ot_usbdev_get_link_state(s) == + OT_USBDEV_LINK_STATE_DISCONNECTED); + if (ot_usbdev_has_vbus(s)) { + ot_usbdev_set_raw_link_state(s, OT_USBDEV_LINK_STATE_POWERED); + ot_usbdev_server_report_connected(s, true); + } + } else { + /* See BFM (set_enable) */ + ot_usbdev_set_raw_link_state(s, OT_USBDEV_LINK_STATE_DISCONNECTED); + s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_DISCONNECTED_MASK; + s->regs[R_USBCTRL] = + FIELD_DP32(s->regs[R_USBCTRL], USBCTRL, DEVICE_ADDRESS, 0u); + timer_del(&s->frame_timer); + timer_del(&s->bus_reset_timer); - if (ot_usbdev_has_vbus(s)) { - ot_usbdev_server_report_connected(s, false); + if (ot_usbdev_has_vbus(s)) { + ot_usbdev_server_report_connected(s, false); + } } } - /* - * Status interrupts are only active when enabled, we may need to update - * them - */ - ot_usbdev_update_status_irqs(s); - ot_usbdev_update_irqs(s); + if ((val32 & R_USBCTRL_RESUME_LINK_ACTIVE_MASK) && enable && + ot_usbdev_has_vbus(s) && + ot_usbdev_get_link_state(s) == OT_USBDEV_LINK_STATE_POWERED) { + ot_usbdev_set_raw_link_state(s, OT_USBDEV_LINK_STATE_ACTIVE_NOSOF); + s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_LINK_RESUME_MASK; + } + + ot_usbdev_update_fifos_status(s); } /* @@ -1729,7 +2002,8 @@ static void ot_usbdev_write_configin(OtUsbdevState *s, hwaddr reg, s->regs[reg] &= ~rw1c; /* set RW fields */ s->regs[reg] &= USBDEV_CONFIGIN_RW1C_MASK; - s->regs[reg] |= val32 & ~USBDEV_CONFIGIN_RW1C_MASK; + s->regs[reg] |= + val32 & (CONFIGIN_BUFFER_MASK | CONFIGIN_SIZE_MASK | CONFIGIN_RDY_MASK); ot_usbdev_advance_transfer_in(s, ep); } @@ -1793,17 +2067,11 @@ static void ot_usbdev_update_ep_xfers(OtUsbdevState *s, bool dir_in, static void ot_usbdev_update_ep_enabled(OtUsbdevState *s, hwaddr reg, uint32_t val32) { - /* Find which endpoints have been disabled */ - uint32_t disabled_ep = s->regs[reg] & ~val32; + uint32_t changed_ep = (s->regs[reg] ^ val32) & USBDEV_ALL_EP_MASK; s->regs[reg] = val32 & USBDEV_ALL_EP_MASK; bool dir_in = reg == R_EP_IN_ENABLE; - /* - * Only update disabled endpoints: the endpoints which became enabled - * cannot have any pending transfers since ot_usbdev_advance_transfer_out - * automatically completes transfers on disabled endpoints. - */ - ot_usbdev_update_ep_xfers(s, dir_in, disabled_ep); + ot_usbdev_update_ep_xfers(s, dir_in, changed_ep); } /* @@ -1818,23 +2086,82 @@ ot_usbdev_update_ep_enabled(OtUsbdevState *s, hwaddr reg, uint32_t val32) static void ot_usbdev_update_ep_stalled(OtUsbdevState *s, hwaddr reg, uint32_t val32) { - /* Find which endpoints have been stalled */ - uint32_t stalled_ep = (~s->regs[reg] & val32) & USBDEV_ALL_EP_MASK; + uint32_t changed_ep = (s->regs[reg] ^ val32) & USBDEV_ALL_EP_MASK; s->regs[reg] = val32 & USBDEV_ALL_EP_MASK; bool dir_in = reg == R_IN_STALL; - /* - * Only update stalled endpoints: the endpoints which became unstalled - * cannot have any pending transfers since ot_usbdev_advance_transfer_out - * automatically completes transfers on stalled endpoints. - */ - ot_usbdev_update_ep_xfers(s, dir_in, stalled_ep); + ot_usbdev_update_ep_xfers(s, dir_in, changed_ep); } /* * Register read/write handling */ +static void ot_usbdev_clock_input(void *opaque, int irq, int level) +{ + OtUsbdevState *s = opaque; + + g_assert(irq == 0); + + s->pclk = (unsigned)level; +} + +static uint8_t ot_usbdev_reg_permit(hwaddr reg) +{ + switch (reg) { + case R_USBDEV_INTR_STATE: + case R_USBDEV_INTR_ENABLE: + case R_USBDEV_INTR_TEST: + case R_USBCTRL: + case R_RXFIFO: + case R_PHY_PINS_SENSE: + case R_PHY_PINS_DRIVE: + return 0x7u; + case R_EP_OUT_ENABLE: + case R_EP_IN_ENABLE: + case R_RXENABLE_SETUP: + case R_RXENABLE_OUT: + case R_SET_NAK_OUT: + case R_IN_SENT: + case R_OUT_STALL: + case R_IN_STALL: + case R_OUT_ISO: + case R_IN_ISO: + case R_WAKE_EVENTS: + return 0x3u; + case R_USBSTAT: + case R_CONFIGIN_0 ... R_CONFIGIN_11: + case R_OUT_DATA_TOGGLE: + case R_IN_DATA_TOGGLE: + case R_COUNT_OUT: + case R_COUNT_IN: + case R_COUNT_NODATA_IN: + case R_COUNT_ERRORS: + return 0xfu; + default: + return 0x1u; + } +} + +static bool ot_usbdev_regs_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + OtUsbdevState *s = opaque; + (void)attrs; + if (!s->pclk) { + ot_common_stall_cpu_on_unclocked_mmio(DEVICE(s), addr); + } + hwaddr reg = R32_OFF(addr); + if (reg >= REGS_COUNT) { + return false; + } + if (!is_write) { + return true; + } + uint32_t reg_be = ((1u << size) - 1u) << (addr & 3u); + return (ot_usbdev_reg_permit(reg) & ~reg_be) == 0u; +} + static uint64_t ot_usbdev_read(void *opaque, hwaddr addr, unsigned size) { OtUsbdevState *s = opaque; @@ -1842,6 +2169,7 @@ static uint64_t ot_usbdev_read(void *opaque, hwaddr addr, unsigned size) uint32_t val32; hwaddr reg = R32_OFF(addr); + switch (reg) { /* Reads with no side-effects */ case R_PHY_CONFIG: @@ -1850,41 +2178,28 @@ static uint64_t ot_usbdev_read(void *opaque, hwaddr addr, unsigned size) case R_USBCTRL: case R_EP_OUT_ENABLE: case R_EP_IN_ENABLE: - case R_AVOUTBUFFER: - case R_AVSETUPBUFFER: case R_RXENABLE_SETUP: case R_RXENABLE_OUT: case R_SET_NAK_OUT: case R_IN_SENT: case R_OUT_STALL: case R_IN_STALL: - case R_CONFIGIN_0: - case R_CONFIGIN_1: - case R_CONFIGIN_2: - case R_CONFIGIN_3: - case R_CONFIGIN_4: - case R_CONFIGIN_5: - case R_CONFIGIN_6: - case R_CONFIGIN_7: - case R_CONFIGIN_8: - case R_CONFIGIN_9: - case R_CONFIGIN_10: - case R_CONFIGIN_11: + case R_CONFIGIN_0 ... R_CONFIGIN_11: case R_OUT_ISO: case R_IN_ISO: case R_OUT_DATA_TOGGLE: case R_IN_DATA_TOGGLE: - case R_PHY_PINS_SENSE: case R_PHY_PINS_DRIVE: - case R_WAKE_CONTROL: case R_WAKE_EVENTS: - case R_FIFO_CTRL: case R_COUNT_OUT: case R_COUNT_IN: case R_COUNT_NODATA_IN: case R_COUNT_ERRORS: val32 = s->regs[reg]; break; + case R_PHY_PINS_SENSE: + val32 = ot_usbdev_read_phy_pins_sense(s); + break; case R_USBSTAT: /* * We extrapolate the frame counter on each read to avoid frequent @@ -1907,24 +2222,27 @@ static uint64_t ot_usbdev_read(void *opaque, hwaddr addr, unsigned size) trace_ot_usbdev_pop_rx_fifo(s->ot_id, val32); ot_usbdev_update_fifos_status(s); /* Potentially all endpoints are now able to receive some data. */ - if (ot_fifo32_num_free(&s->rx_fifo) == 1u) { - ot_usbdev_update_ep_xfers(s, /* dir_in */ false, - USBDEV_ALL_EP_MASK); + ot_usbdev_update_ep_xfers(s, /* dir_in */ false, + USBDEV_ALL_EP_MASK); + if (ot_fifo32_is_empty(&s->rx_fifo) && s->regs[R_IN_SENT] == 0u && + s->usb_pace_until_ns > + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 20000LL) { + ot_usbdev_pace_usb_chr(s, 20000LL); } } break; case R_USBDEV_INTR_TEST: case R_ALERT_TEST: + case R_AVOUTBUFFER: + case R_AVSETUPBUFFER: + case R_WAKE_CONTROL: + case R_FIFO_CTRL: + default: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s Read to W/O register 0x%02x (%s)\n", __func__, s->ot_id, (uint32_t)addr, REG_NAME(reg)); val32 = 0u; break; - default: - qemu_log_mask(LOG_GUEST_ERROR, "%s: %s Bad offset 0x%02x\n", __func__, - s->ot_id, (uint32_t)addr); - val32 = 0u; - break; } uint32_t pc = ibex_get_current_pc(); @@ -1974,25 +2292,28 @@ static void ot_usbdev_write(void *opaque, hwaddr addr, uint64_t val64, break; case R_USBDEV_INTR_TEST: val32 &= USBDEV_INTR_MASK; - s->regs[R_USBDEV_INTR_STATE] |= val32; + s->regs[R_USBDEV_INTR_STATE] |= val32 & USBDEV_INTR_RW1C_MASK; + s->regs[R_USBDEV_INTR_TEST] = val32 & USBDEV_INTR_STATUS_MASK; ot_usbdev_update_irqs(s); break; case R_ALERT_TEST: val32 &= R_ALERT_TEST_FATAL_FAULT_MASK; - /* Use the register to record alerts sets */ s->regs[R_ALERT_TEST] = val32; ot_usbdev_update_alerts(s); + s->regs[R_ALERT_TEST] = 0u; + ot_usbdev_update_alerts(s); break; case R_RXFIFO: case R_USBSTAT: + case R_PHY_PINS_SENSE: + case R_WAKE_EVENTS: qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: write to R/O register 0x%02x" " (%s)\n", __func__, s->ot_id, (uint32_t)addr, REG_NAME(reg)); break; case R_PHY_CONFIG: - /* @todo mask against actual fields? */ - s->regs[R_PHY_CONFIG] = val32; + s->regs[R_PHY_CONFIG] = val32 & 0xe7u; break; case R_USBCTRL: ot_usbdev_write_usbctrl(s, val32); @@ -2012,16 +2333,16 @@ static void ot_usbdev_write(void *opaque, hwaddr addr, uint64_t val64, qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: write to AVOUTBUFFER but FIFO is full\n", __func__, s->ot_id); + s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_AV_OVERFLOW_MASK; + ot_usbdev_update_irqs(s); } else { uint8_t buf_id = (uint8_t)FIELD_EX32(val32, AVOUTBUFFER, BUFFER); fifo8_push(&s->av_out_fifo, buf_id); trace_ot_usbdev_push_av_out_buffer(s->ot_id, buf_id); ot_usbdev_update_fifos_status(s); /* Potentially all endpoints are now able to receive some data. */ - if (fifo8_num_used(&s->av_out_fifo) == 1u) { - ot_usbdev_update_ep_xfers(s, /* dir_in */ false, - USBDEV_ALL_EP_MASK); - } + ot_usbdev_update_ep_xfers(s, /* dir_in */ false, + USBDEV_ALL_EP_MASK); } break; case R_AVSETUPBUFFER: @@ -2029,6 +2350,8 @@ static void ot_usbdev_write(void *opaque, hwaddr addr, uint64_t val64, qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: write to AVSETUPBUFFER but FIFO is full\n", __func__, s->ot_id); + s->regs[R_USBDEV_INTR_STATE] |= USBDEV_INTR_AV_OVERFLOW_MASK; + ot_usbdev_update_irqs(s); } else { uint8_t buf_id = (uint8_t)FIELD_EX32(val32, AVSETUPBUFFER, BUFFER); fifo8_push(&s->av_setup_fifo, buf_id); @@ -2036,49 +2359,100 @@ static void ot_usbdev_write(void *opaque, hwaddr addr, uint64_t val64, ot_usbdev_update_fifos_status(s); } break; - case R_CONFIGIN_0: - case R_CONFIGIN_1: - case R_CONFIGIN_2: - case R_CONFIGIN_3: - case R_CONFIGIN_4: - case R_CONFIGIN_5: - case R_CONFIGIN_6: - case R_CONFIGIN_7: - case R_CONFIGIN_8: - case R_CONFIGIN_9: - case R_CONFIGIN_10: - case R_CONFIGIN_11: + case R_CONFIGIN_0 ... R_CONFIGIN_11: ot_usbdev_write_configin(s, reg, val32); break; case R_IN_SENT: /* register is rw1c */ s->regs[reg] &= ~val32; + ot_usbdev_update_irqs(s); + if (val32 & 1u) { + ot_usbdev_advance_transfer_out(s, 0u); + } + if (ot_fifo32_is_empty(&s->rx_fifo) && s->regs[R_IN_SENT] == 0u && + s->usb_pace_until_ns > + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + 20000LL) { + ot_usbdev_pace_usb_chr(s, 20000LL); + } break; case R_SET_NAK_OUT: - s->regs[reg] = val32; + s->regs[reg] = val32 & USBDEV_ALL_EP_MASK; + break; + case R_WAKE_CONTROL: + if (val32 & R_WAKE_CONTROL_SUSPEND_REQ_MASK) { + if (!(s->regs[R_WAKE_EVENTS] & R_WAKE_EVENTS_MODULE_ACTIVE_MASK)) { + bool dp_pullup, dn_pullup; + ot_usbdev_get_pullups(s, &dp_pullup, &dn_pullup); + s->aon_dppullup_en = dp_pullup; + s->aon_dnpullup_en = dn_pullup; + s->regs[R_WAKE_EVENTS] |= R_WAKE_EVENTS_MODULE_ACTIVE_MASK; + } + ot_usbdev_update_aon_wake(s); + } else if (val32 & R_WAKE_CONTROL_WAKE_ACK_MASK) { + s->regs[R_WAKE_EVENTS] = 0u; + s->aon_wake_events = 0u; + s->aon_cdc_dst_qs = 0u; + ot_usbdev_update_status_irqs(s); + ot_usbdev_update_irqs(s); + ibex_irq_set(&s->wkup, 0); + } break; - case R_OUT_ISO: - case R_IN_ISO: case R_OUT_DATA_TOGGLE: - case R_IN_DATA_TOGGLE: - case R_PHY_PINS_SENSE: + case R_IN_DATA_TOGGLE: { + uint32_t mask = FIELD_EX32(val32, OUT_DATA_TOGGLE, MASK); + uint32_t status = FIELD_EX32(val32, OUT_DATA_TOGGLE, STATUS); + uint32_t cur = FIELD_EX32(s->regs[reg], OUT_DATA_TOGGLE, STATUS); + uint32_t next = (cur & ~mask) | (status & mask); + s->regs[reg] = FIELD_DP32(0u, OUT_DATA_TOGGLE, STATUS, next); + break; + } case R_PHY_PINS_DRIVE: - case R_WAKE_CONTROL: - case R_WAKE_EVENTS: + s->regs[reg] = val32 & 0x000100ffu; + ot_usbdev_update_aon_wake(s); + break; + case R_OUT_ISO: + case R_IN_ISO: + s->regs[reg] = val32 & USBDEV_ALL_EP_MASK; + break; case R_FIFO_CTRL: + if (val32 & R_FIFO_CTRL_AVOUT_RST_MASK) { + fifo8_reset(&s->av_out_fifo); + } + if (val32 & R_FIFO_CTRL_AVSETUP_RST_MASK) { + fifo8_reset(&s->av_setup_fifo); + } + if (val32 & R_FIFO_CTRL_RX_RST_MASK) { + ot_fifo32_reset(&s->rx_fifo); + } + ot_usbdev_update_fifos_status(s); + break; case R_COUNT_OUT: case R_COUNT_IN: case R_COUNT_NODATA_IN: case R_COUNT_ERRORS: - s->regs[reg] = val32; - qemu_log_mask(LOG_UNIMP, "%s: %s: %s is not supported\n", __func__, - s->ot_id, REG_NAME(reg)); - break; - default: - qemu_log_mask(LOG_GUEST_ERROR, "%s: %s Bad offset 0x%02x\n", __func__, - s->ot_id, (uint32_t)addr); + default: { + uint32_t rw_mask = + (reg == R_COUNT_OUT) ? 0x0ffff000u : + (reg == R_COUNT_IN) ? 0x0fffe000u : + (reg == R_COUNT_NODATA_IN) ? 0x0fff0000u : + 0x78000000u; + uint32_t count = (val32 & (1u << 31)) ? 0u : (s->regs[reg] & 0xffu); + s->regs[reg] = (val32 & rw_mask) | count; break; } + } +} + +static bool ot_usbdev_buffer_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + OtUsbdevState *s = opaque; + (void)attrs; + if (!s->pclk) { + ot_common_stall_cpu_on_unclocked_mmio(DEVICE(s), + USBDEV_BUFFER_OFFSET + addr); + } + return !is_write || (size == 4u && (addr & 3u) == 0u); } static uint64_t ot_usbdev_buffer_read(void *opaque, hwaddr addr, unsigned size) @@ -2100,7 +2474,7 @@ static void ot_usbdev_buffer_write(void *opaque, hwaddr addr, uint64_t val64, { OtUsbdevState *s = opaque; (void)size; - uint32_t val32 = val64; + uint32_t val32 = (uint32_t)val64; hwaddr word = R32_OFF(addr); s->buffer[word] = val32; @@ -2136,6 +2510,7 @@ static void ot_usbdev_server_open(OtUsbdevState *s) server->client_connected = true; server->vbus_connected = false; server->hello_done = false; + server->device_connected = false; ot_usbdev_server_reset_recv_state(s); ot_usbdev_update_vbus(s); @@ -2152,6 +2527,7 @@ static void ot_usbdev_server_close(OtUsbdevState *s) OtUsbdevServer *server = &s->usb_server; server->client_connected = false; server->vbus_connected = false; + server->device_connected = false; ot_usbdev_update_vbus(s); } @@ -2188,14 +2564,14 @@ static void ot_usbdev_server_write_packet( .id = id, }; int res = - qemu_chr_fe_write(&s->usb_chr, (const uint8_t *)&hdr, sizeof(hdr)); + qemu_chr_fe_write_all(&s->usb_chr, (const uint8_t *)&hdr, sizeof(hdr)); if (res < sizeof(hdr)) { qemu_log_mask(LOG_UNIMP, "%s: %s server: unhandled partial write\n", __func__, s->ot_id); return; } if (size0 > 0u) { - res = qemu_chr_fe_write(&s->usb_chr, data0, (int)size0); + res = qemu_chr_fe_write_all(&s->usb_chr, data0, (int)size0); if (res < size0) { qemu_log_mask(LOG_UNIMP, "%s: %s server: unhandled partial write\n", __func__, s->ot_id); @@ -2203,7 +2579,7 @@ static void ot_usbdev_server_write_packet( } } if (size1 > 0u) { - res = qemu_chr_fe_write(&s->usb_chr, data1, (int)size1); + res = qemu_chr_fe_write_all(&s->usb_chr, data1, (int)size1); if (res < size1) { qemu_log_mask(LOG_UNIMP, "%s: %s server: unhandled partial write\n", __func__, s->ot_id); @@ -2216,8 +2592,6 @@ static void ot_usbdev_server_write_packet( * Report a (dis)connection event to the client. * * Send a message to the client to notify the connection status change. - * Note that this function does not keep track of the current status and will - * send a message if the new status is the same as the old one. * * @connected New connection status */ @@ -2225,10 +2599,12 @@ void ot_usbdev_server_report_connected(OtUsbdevState *s, bool connected) { trace_ot_usbdev_server_report_connected(s->ot_id, connected); - /* Nothing to do if no client is connected */ - if (!s->usb_server.client_connected) { + /* Nothing to do if no client is connected or status has not changed */ + if (!s->usb_server.client_connected || + s->usb_server.device_connected == connected) { return; } + s->usb_server.device_connected = connected; /* @todo clarify ID to use */ ot_usbdev_server_write_packet(s, @@ -2587,6 +2963,12 @@ static void ot_usbdev_server_process_packet(OtUsbdevState *s) case OT_USBDEV_SERVER_CMD_CANCEL: ot_usbdev_server_process_cancel(s); break; + case OT_USBDEV_SERVER_CMD_SUSPEND: + ot_usbdev_simulate_link_suspend(s); + break; + case OT_USBDEV_SERVER_CMD_RESUME: + ot_usbdev_simulate_link_resume(s); + break; default: trace_ot_usbdev_server_protocol_error( s->ot_id, "unknown command, ignoring packet"); @@ -2634,6 +3016,12 @@ static int ot_usbdev_chr_usb_can_receive(void *opaque) return 0; } + if (s->usb_server.recv_state == OT_USBDEV_SERVER_RECV_WAIT_HEADER && + s->usb_server.recv_rem == sizeof(OtUsbdevServerPktHdr) && + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) < s->usb_pace_until_ns) { + return 0; + } + /* Return remaining size in the buffer. */ return (int)s->usb_server.recv_rem; } @@ -2736,7 +3124,7 @@ static void ot_usbdev_chr_cmd_receive(void *opaque, const uint8_t *buf, size_t cmd_size = eol - s->cmd_buf; ot_usbdev_chr_process_cmd(s, s->cmd_buf, cmd_size); memmove(s->cmd_buf, eol + 1u, s->cmd_buf_pos - cmd_size - 1u); - s->cmd_buf_pos -= cmd_size - 1u; + s->cmd_buf_pos -= cmd_size + 1u; } } @@ -2769,6 +3157,9 @@ static const MemoryRegionOps ot_usbdev_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = sizeof(uint32_t), .impl.max_access_size = sizeof(uint32_t), + .valid.min_access_size = 1u, + .valid.max_access_size = sizeof(uint32_t), + .valid.accepts = &ot_usbdev_regs_accepts, }; static const MemoryRegionOps ot_usbdev_buffer_ops = { @@ -2778,12 +3169,36 @@ static const MemoryRegionOps ot_usbdev_buffer_ops = { /* @todo The RTL probably supports sub-word reads, implement this */ .impl.min_access_size = sizeof(uint32_t), .impl.max_access_size = sizeof(uint32_t), + .valid.min_access_size = 1u, + .valid.max_access_size = sizeof(uint32_t), + .valid.accepts = &ot_usbdev_buffer_accepts, }; /* * QEMU reset handling */ +void ot_usbdev_aon_reset(OtUsbdevState *s, bool reset) +{ + if (reset) { + /* + * SW_RST_CTRL_N[4] (rst_usb_aon_n) resets u_wake_events_cdc.dst_qs_o + * and u_wake_events_* on the AON side of usbdev_reg_top.sv, while + * u_usbdev_aon_wake in u_pinmux_aon (rst_sys_aon_n) continues driving + * s->aon_wake_events. + */ + s->aon_cdc_dst_qs = 0u; + } else if (s->aon_cdc_dst_qs != s->aon_wake_events) { + /* + * Upon release of rst_usb_aon_n, prim_reg_cdc_arb sees dst_qs_o (0) != + * dst_ds_i (s->aon_wake_events) and pushes s->aon_wake_events into + * src_q (s->regs[R_WAKE_EVENTS]). + */ + s->aon_cdc_dst_qs = s->aon_wake_events; + s->regs[R_WAKE_EVENTS] = s->aon_wake_events; + } +} + static void ot_usbdev_reset_enter(Object *obj, ResetType type) { OtUsbdevClass *c = OT_USBDEV_GET_CLASS(obj); @@ -2797,6 +3212,26 @@ static void ot_usbdev_reset_enter(Object *obj, ResetType type) ot_usbdev_cancel_all_transfers(s, "device going into reset"); + bool is_sw_usb_rst = + (type == RESET_TYPE_WAKEUP) && !ot_rstmgr_is_internal_reset(); + bool is_por = !is_sw_usb_rst && ot_rstmgr_is_por_reset(); + bool keep_aon = + !is_por && (s->aon_wake_events & R_WAKE_EVENTS_MODULE_ACTIVE_MASK); + uint32_t saved_wake_events = s->aon_wake_events; + uint32_t saved_cdc_dst_qs = s->aon_cdc_dst_qs; + bool saved_dppullup = s->aon_dppullup_en; + bool saved_dnpullup = s->aon_dnpullup_en; + + if (!keep_aon && + ot_usbdev_get_link_state(s) != OT_USBDEV_LINK_STATE_DISCONNECTED && + ot_usbdev_has_vbus(s)) { + ot_usbdev_server_report_connected(s, false); + } + s->aon_dppullup_en = false; + s->aon_dnpullup_en = false; + s->aon_wake_events = 0u; + s->aon_cdc_dst_qs = 0u; + /* * If there is a bus reset in progress, we cancel it. * The rationale is that on a real bus, the device @@ -2806,14 +3241,44 @@ static void ot_usbdev_reset_enter(Object *obj, ResetType type) */ timer_del(&s->frame_timer); timer_del(&s->bus_reset_timer); + timer_del(&s->usb_pace_timer); + s->usb_pace_until_ns = 0; /* See BFM (dut_reset) */ memset(s->regs, 0u, sizeof(s->regs)); + s->regs[R_PHY_CONFIG] = R_PHY_CONFIG_EOP_SINGLE_BIT_MASK; + if (keep_aon) { + s->aon_wake_events = saved_wake_events; + s->aon_dppullup_en = saved_dppullup; + s->aon_dnpullup_en = saved_dnpullup; + if (is_sw_usb_rst) { + /* + * SW_RST_CTRL_N[3] (rst_usb_n) resets u_wake_events_cdc.src_q + * (s->regs[R_WAKE_EVENTS]) to 0, while u_wake_events_cdc.dst_qs_o + * (on rst_usb_aon_n) retains saved_cdc_dst_qs. + */ + s->aon_cdc_dst_qs = saved_cdc_dst_qs; + } else { + /* Full SoC non-POR reset resets both rst_usb_n and rst_usb_aon_n */ + s->aon_cdc_dst_qs = s->aon_wake_events; + s->regs[R_WAKE_EVENTS] = s->aon_wake_events; + } + } ot_fifo32_reset(&s->rx_fifo); fifo8_reset(&s->av_setup_fifo); fifo8_reset(&s->av_out_fifo); + if (!s->clock_src_name && s->clock_src && s->usbclk_name) { + IbexClockSrcIfClass *ic = IBEX_CLOCK_SRC_IF_GET_CLASS(s->clock_src); + IbexClockSrcIf *ii = IBEX_CLOCK_SRC_IF(s->clock_src); + + s->clock_src_name = + ic->get_clock_source(ii, s->usbclk_name, DEVICE(s), &error_fatal); + qemu_irq in_irq = qdev_get_gpio_in_named(DEVICE(s), "clock-in", 0); + qdev_connect_gpio_out_named(s->clock_src, s->clock_src_name, 0, in_irq); + } + ot_usbdev_update_status_irqs(s); ot_usbdev_update_irqs(s); } @@ -2852,26 +3317,32 @@ static void ot_usbdev_realize(DeviceState *dev, Error **errp) g_assert(s->usbclk_name); g_assert(s->aonclk_name); + qdev_init_gpio_in_named(DEVICE(s), &ot_usbdev_clock_input, "clock-in", 1); + /* If not in VBUS override mode, the VBUS gate starts on by default. */ if (!s->vbus_override) { s->vbus_gate = true; } + ot_usbdev_instance = s; } static void ot_usbdev_init(Object *obj) { OtUsbdevState *s = OT_USBDEV(obj); + s->pclk = 1u; + s->pinmux_vbus_sense = -1; for (unsigned idx = 0u; idx < ARRAY_SIZE(s->irqs); idx++) { ibex_sysbus_init_irq(obj, &s->irqs[idx]); } ibex_qdev_init_irq(obj, &s->alert, OT_DEVICE_ALERT); + ibex_qdev_init_irq(obj, &s->wkup, OT_USBDEV_WKUP); memory_region_init(&s->mmio.main, obj, TYPE_OT_USBDEV ".mmio", USBDEV_DEVICE_SIZE); sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio.main); memory_region_init_io(&s->mmio.regs, obj, &ot_usbdev_ops, s, - TYPE_OT_USBDEV ".reg", USBDEV_REGS_SIZE); + TYPE_OT_USBDEV ".reg", USBDEV_BUFFER_OFFSET); memory_region_add_subregion(&s->mmio.main, USBDEV_REGS_OFFSET, &s->mmio.regs); memory_region_init_io(&s->mmio.buffer, obj, &ot_usbdev_buffer_ops, s, @@ -2885,6 +3356,8 @@ static void ot_usbdev_init(Object *obj) timer_init_us(&s->bus_reset_timer, OT_VIRTUAL_CLOCK, &ot_usbdev_link_reset_complete, s); + timer_init_ns(&s->usb_pace_timer, OT_VIRTUAL_CLOCK, + &ot_usbdev_pace_timer_expired, s); timer_init_us(&s->frame_timer, OT_VIRTUAL_CLOCK, &ot_usbdev_frame_timer_expired, s); } diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 4a43d0ce2c328..c0b02c03bf1b1 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -982,6 +982,7 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { }, [OT_EG_SOC_DEV_USBDEV] = { .type = TYPE_OT_USBDEV, + .instance = IBEX_MAKE_INSTANCE_NUM(0), .cfg = &ot_eg_soc_usbdev_configure, .memmap = MEMMAPENTRIES( { .base = 0x40320000u } @@ -1005,6 +1006,8 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { OT_EG_SOC_GPIO_SYSBUS_IRQ(15, PLIC, 150), OT_EG_SOC_GPIO_SYSBUS_IRQ(16, PLIC, 151), OT_EG_SOC_GPIO_SYSBUS_IRQ(17, PLIC, 152), + OT_EG_SOC_SIGNAL(OT_USBDEV_WKUP, 0, PWRMGR, + OT_PWRMGR_WKUP, OT_PWRMGR_WAKEUP_USBDEV), OT_EG_SOC_GPIO_ALERT(0, 21) /* The VBUS sense pin is handled by a chardev */ ), @@ -1013,7 +1016,7 @@ static const IbexDeviceDef ot_eg_soc_devices[] = { ), .prop = IBEXDEVICEPROPDEFS( IBEX_DEV_STRING_PROP(OT_COMMON_DEV_ID, "usbdev"), - IBEX_DEV_STRING_PROP("clock-name", "usb"), + IBEX_DEV_STRING_PROP("clock-name", "peri.usb"), IBEX_DEV_STRING_PROP("clock-name-aon", "aon") ), }, diff --git a/include/hw/opentitan/ot_usbdev.h b/include/hw/opentitan/ot_usbdev.h index 1b578e83747c8..4a23cf1658ed2 100644 --- a/include/hw/opentitan/ot_usbdev.h +++ b/include/hw/opentitan/ot_usbdev.h @@ -34,4 +34,9 @@ #define TYPE_OT_USBDEV "ot-usbdev" OBJECT_DECLARE_TYPE(OtUsbdevState, OtUsbdevClass, OT_USBDEV) +#define OT_USBDEV_WKUP TYPE_OT_USBDEV "-wkup" + +void ot_usbdev_aon_reset(OtUsbdevState *s, bool reset); +void ot_usbdev_set_pinmux_sense(int level); + #endif /* HW_OPENTITAN_OT_USBDEV_H */ From 3644a933cef94181b0aea6f928dcc237778fe270 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:42 +0000 Subject: [PATCH 49/52] [ot] hw/opentitan: i2c_host_proxy: support target stretch and wakeup Extend the OpenTitan chardev I2C host proxy (`ot_i2c_host_proxy`) to support Earlgrey I2C target-mode protocol requirements: - Add `ProxyPendingOp` (`PROXY_PENDING_WRITE_ACK`, `PROXY_PENDING_READ_BYTE`) and `stop_wait_ticks` polling in `ot_i2c_host_proxy_timer` to stall chardev byte processing while the guest `ot_i2c` target stretches SCL (`ACQ` full, `ACK_CTRL_EN`, or `TX` empty) and return deferred ACK/NACK results via `ot_i2c_bus_target_get_last_ack`. - Support target-mode repeated start (`Sr`) address updates via `ot_i2c_bus_target_repeated_start`, send `i2c_nack` prior to STOP/Sr after read transfers, and route transfers to the active `ot_i2c` target bus via `ot_i2c_get_active_target_bus`. - Trigger MIO7 (`IOA7`) pinmux sleep wakeup via `ot_pinmux_eg_trigger_mio_wkup(7u)` when an I2C transfer starts while the SoC is in low-power sleep. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I97cc8ecf0e3e882868c0f189e8e42a376a6a6964 --- hw/opentitan/ot_i2c_host_proxy.c | 116 +++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 36 deletions(-) diff --git a/hw/opentitan/ot_i2c_host_proxy.c b/hw/opentitan/ot_i2c_host_proxy.c index 8edac7b3fd4b4..6eef277b1a4c3 100644 --- a/hw/opentitan/ot_i2c_host_proxy.c +++ b/hw/opentitan/ot_i2c_host_proxy.c @@ -32,7 +32,9 @@ #include "chardev/char-fe.h" #include "hw/i2c/i2c.h" #include "hw/opentitan/ot_common.h" +#include "hw/opentitan/ot_i2c.h" #include "hw/opentitan/ot_i2c_host_proxy.h" +#include "hw/opentitan/ot_pinmux_eg.h" #include "hw/qdev-properties-system.h" #include "hw/qdev-properties.h" #include "trace.h" @@ -57,6 +59,12 @@ typedef enum { CMD_I2C_ERR, } CmdParserState; +typedef enum { + PROXY_PENDING_NONE = 0, + PROXY_PENDING_WRITE_ACK, + PROXY_PENDING_READ_BYTE, +} ProxyPendingOp; + #define I2C_PROXY_PROTO_VERSION 0x01u #define I2C_PROXY_STALL_NS 100000u /* 100us */ @@ -67,9 +75,11 @@ struct OtI2CHostProxyState { /* chardev i2c command parser state */ CmdParserState parser_state; + ProxyPendingOp pending_op; /* Saved target address of transaction for repeated start conditions */ uint8_t address; + uint32_t stop_wait_ticks; /* * Timer to stall chardev I/O to return control back to the vCPU, so @@ -115,16 +125,33 @@ static void ot_i2c_host_proxy_stall(OtI2CHostProxyState *s) static void ot_i2c_host_proxy_start_transfer(OtI2CHostProxyState *s, bool read_transfer) { + s->pending_op = PROXY_PENDING_NONE; + if (ot_pinmux_eg_trigger_mio_wkup(7u)) { + ot_i2c_host_proxy_put_nack(s); + s->parser_state = CMD_I2C_INIT; + ot_i2c_host_proxy_stall(s); + return; + } + + s->bus = ot_i2c_get_active_target_bus(s->bus, s->address); if (i2c_start_transfer(s->bus, s->address, read_transfer) != 0) { + i2c_end_transfer(s->bus); ot_i2c_host_proxy_put_nack(s); + s->parser_state = CMD_I2C_INIT; } else { ot_i2c_host_proxy_put_ack(s); + s->parser_state = read_transfer ? CMD_I2C_READ : CMD_I2C_WRITE; } ot_i2c_host_proxy_stall(s); } static void ot_i2c_host_proxy_do_read(OtI2CHostProxyState *s) { + if (ot_i2c_bus_target_check_tx_stretch(s->bus)) { + s->pending_op = PROXY_PENDING_READ_BYTE; + ot_i2c_host_proxy_stall(s); + return; + } ot_i2c_host_proxy_put_byte(s, i2c_recv(s->bus)); ot_i2c_host_proxy_stall(s); } @@ -133,6 +160,8 @@ static void ot_i2c_host_proxy_do_write(OtI2CHostProxyState *s, uint8_t data) { if (i2c_send(s->bus, data) != 0) { ot_i2c_host_proxy_put_nack(s); + } else if (ot_i2c_bus_target_is_stretching(s->bus)) { + s->pending_op = PROXY_PENDING_WRITE_ACK; } else { ot_i2c_host_proxy_put_ack(s); } @@ -173,49 +202,34 @@ static void ot_i2c_host_proxy_command_byte(OtI2CHostProxyState *s, uint8_t byte) break; case CMD_I2C_START: case CMD_I2C_REPEATED_START: - /* - * If a transaction is already in progress (repeated start), QEMU does - * not re-scan the bus to find devices even if the provided address - * is different, so we do not bother storing it here as the address - * that the transaction was started with will be used anyway. - */ - if (s->parser_state == CMD_I2C_START) { - s->address = (byte >> 1u); - } + s->address = (byte >> 1u); bool read_transfer = (byte & 1u) == 1u; ot_i2c_host_proxy_start_transfer(s, read_transfer); - s->parser_state = read_transfer ? CMD_I2C_READ : CMD_I2C_WRITE; break; case CMD_I2C_READ: - if (byte == 'R') { + case CMD_I2C_WRITE: + if (s->parser_state == CMD_I2C_READ && byte == 'R') { ot_i2c_host_proxy_do_read(s); break; } - if (byte == 'P') { - s->parser_state = CMD_I2C_INIT; - i2c_end_transfer(s->bus); - break; - } - if (byte == 'S') { - s->parser_state = CMD_I2C_REPEATED_START; - break; - } - s->parser_state = CMD_I2C_ERR; - i2c_end_transfer(s->bus); - ot_i2c_host_proxy_parse_error(s); - break; - case CMD_I2C_WRITE: - if (byte == 'W') { + if (s->parser_state == CMD_I2C_WRITE && byte == 'W') { s->parser_state = CMD_I2C_WRITE_PAYLOAD; break; } - if (byte == 'P') { - s->parser_state = CMD_I2C_INIT; - i2c_end_transfer(s->bus); - break; - } - if (byte == 'S') { - s->parser_state = CMD_I2C_REPEATED_START; + if (byte == 'P' || byte == 'S') { + if (s->parser_state == CMD_I2C_READ) { + i2c_nack(s->bus); + } + if (byte == 'P') { + s->parser_state = CMD_I2C_INIT; + s->stop_wait_ticks = 200000u; + i2c_end_transfer(s->bus); + } else { + s->parser_state = CMD_I2C_REPEATED_START; + s->stop_wait_ticks = 200000u; + ot_i2c_bus_target_repeated_start(s->bus); + } + ot_i2c_host_proxy_stall(s); break; } s->parser_state = CMD_I2C_ERR; @@ -223,8 +237,8 @@ static void ot_i2c_host_proxy_command_byte(OtI2CHostProxyState *s, uint8_t byte) ot_i2c_host_proxy_parse_error(s); break; case CMD_I2C_WRITE_PAYLOAD: - ot_i2c_host_proxy_do_write(s, byte); s->parser_state = CMD_I2C_WRITE; + ot_i2c_host_proxy_do_write(s, byte); break; case CMD_I2C_ERR: break; @@ -235,13 +249,43 @@ static void ot_i2c_host_proxy_command_byte(OtI2CHostProxyState *s, uint8_t byte) static void ot_i2c_host_proxy_timer(void *opaque) { - (void)opaque; + OtI2CHostProxyState *s = OT_I2C_HOST_PROXY(opaque); + if (s->stop_wait_ticks > 0) { + if (ot_i2c_bus_target_is_stretching(s->bus)) { + s->stop_wait_ticks--; + ot_i2c_host_proxy_stall(s); + return; + } + s->stop_wait_ticks = 0; + } + if (s->pending_op != PROXY_PENDING_NONE) { + if (ot_i2c_bus_target_is_stretching(s->bus)) { + ot_i2c_host_proxy_stall(s); + return; + } + if (s->pending_op == PROXY_PENDING_WRITE_ACK) { + s->pending_op = PROXY_PENDING_NONE; + if (ot_i2c_bus_target_get_last_ack(s->bus) != 0) { + i2c_end_transfer(s->bus); + ot_i2c_host_proxy_put_nack(s); + s->parser_state = CMD_I2C_INIT; + } else { + ot_i2c_host_proxy_put_ack(s); + } + ot_i2c_host_proxy_stall(s); + } else if (s->pending_op == PROXY_PENDING_READ_BYTE) { + s->pending_op = PROXY_PENDING_NONE; + ot_i2c_host_proxy_put_byte(s, i2c_recv(s->bus)); + ot_i2c_host_proxy_stall(s); + } + } + qemu_chr_fe_accept_input(&s->chr); } static int ot_i2c_host_proxy_can_receive(void *opaque) { OtI2CHostProxyState *s = OT_I2C_HOST_PROXY(opaque); - if (s->parser_state == CMD_I2C_ERR) { + if (s->parser_state == CMD_I2C_ERR || s->pending_op != PROXY_PENDING_NONE) { return 0; } return timer_pending(s->stall_timer) ? 0 : 1; From 936353f0889dd2defbff2ee4a1e87603abf2947f Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:43 +0000 Subject: [PATCH 50/52] [ot] hw/opentitan: spi_device: implement passthru, TPM, and upload Align the OpenTitan Earlgrey SPI Device (hw/opentitan/ot_spi_device.c) with hw/ip/spi_device/rtl/spi_device.sv, spid_status.sv, spid_addr_4b.sv, and spi_device_reg_top.sv: - Gate passthrough_en on FLASH_STATUS.BUSY while idle, commit SW writes to FLASH_STATUS on CSb de-assertion (committed_flash_status), set ADDR_MODE.PENDING (bit 31) on SW writes until the next command, and include READ_PIPELINE_MODE [23:22] in CMD_INFO_0..23 masks. - Drive DIO pad 13 (CSb) and sample MIO peripheral input 31 (TPM_CSB) via ot_pinmux_eg, and pace flash/poll transactions across CSb edges. - Gate TPM_CMD_ADDR and INTR_TPM_HEADER_NOT_EMPTY on SW-handled TPM transfers, latch TPM_CMD_ADDR on TPM write completion, and align TPM_CFG/TPM_ACCESS_1/TPM_INT_VECTOR/TPM_RID bitmasks. - Pulse ALERT_TEST for 1 cycle, reflect INTR_TEST.TPM_HEADER_NOT_EMPTY in INTR_STATE, fault on egress SRAM reads (ErrOnRead=1) and sub-word writes (ByteAccess=0), and enforce SPI_DEVICE_PERMIT byte-enables. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I3f2be30bf3f07910b9db8f0f41ac4e5e6a6a6964 --- hw/opentitan/ot_spi_device.c | 445 +++++++++++++++++++++++++++++------ 1 file changed, 377 insertions(+), 68 deletions(-) diff --git a/hw/opentitan/ot_spi_device.c b/hw/opentitan/ot_spi_device.c index da2261c684b6b..692b9640d23f4 100644 --- a/hw/opentitan/ot_spi_device.c +++ b/hw/opentitan/ot_spi_device.c @@ -37,6 +37,7 @@ #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_fifo32.h" +#include "hw/opentitan/ot_pinmux_eg.h" #include "hw/opentitan/ot_spi_device.h" #include "hw/opentitan/ot_spi_host.h" #include "hw/qdev-properties-system.h" @@ -242,6 +243,7 @@ REG32(TPM_READ_FIFO, 0x34u) * potentially keeping a small additional delay on a buffer flip. */ #define SPI_BUS_FLASH_READ_DELAY_NS 10000000 +#define SPI_BUS_FLASH_POLL_DELAY_NS 500000 /* * Memory layout extracted from the documentation: @@ -434,6 +436,7 @@ typedef struct { bool loop; /* Keep reading the buffer if end is reached */ bool watermark_crossed; /* Read watermark hit, used as flip-flop */ bool new_cmd; /* New command has been pushed in current SPI transaction */ + bool read_buf_modified; /* Read buffer modified by SW since last SPI read */ } SpiDeviceFlash; typedef struct { @@ -468,6 +471,14 @@ typedef struct { bool rev_tx; /* Reverse TX bits */ } SpiDeviceBus; +typedef enum { + CSB_IDLE_HIGH_SEEN = 0, + CSB_ACTIVE_LOW_UNSEEN, + CSB_ACTIVE_LOW_SEEN, + CSB_RELEASED_LOW_UNSEEN, + CSB_IDLE_HIGH_UNSEEN, +} OtSpiCsbSyncState; + struct OtSPIDeviceState { SysBusDevice parent_obj; @@ -492,6 +503,14 @@ struct OtSPIDeviceState { uint32_t *tpm_regs; /* Registers */ uint32_t *sram; + OtSpiCsbSyncState csb_sync; + uint32_t committed_flash_status; + bool csb_poll_enabled; + bool last_read_addr_polled; + bool flash_ever_enabled; + unsigned csb_poll_count; + int64_t reset_time_ns; + /* Properties */ char *ot_id; OtSPIHostState *spi_host; /* downstream SPI Host */ @@ -617,8 +636,8 @@ static const char *TPM_REG_NAMES[TPM_REGS_COUNT] = { CMD_INFO_ADDR_SWAP_EN_MASK | CMD_INFO_MBYTE_EN_MASK | \ CMD_INFO_DUMMY_SIZE_MASK | CMD_INFO_DUMMY_EN_MASK | \ CMD_INFO_PAYLOAD_EN_MASK | CMD_INFO_PAYLOAD_DIR_MASK | \ - CMD_INFO_PAYLOAD_SWAP_EN_MASK | CMD_INFO_UPLOAD_MASK | \ - CMD_INFO_BUSY_MASK | CMD_INFO_VALID_MASK) + CMD_INFO_PAYLOAD_SWAP_EN_MASK | CMD_INFO_READ_PIPELINE_MODE_MASK | \ + CMD_INFO_UPLOAD_MASK | CMD_INFO_BUSY_MASK | CMD_INFO_VALID_MASK) #define CMD_INFO_SPC_MASK (CMD_INFO_OPCODE_MASK | CMD_INFO_VALID_MASK) #define CFG_MASK \ (R_CFG_TX_ORDER_MASK | R_CFG_RX_ORDER_MASK | R_CFG_MAILBOX_EN_MASK) @@ -770,6 +789,7 @@ static void ot_spi_device_clear_modes(OtSPIDeviceState *s) f->len = 0u; f->watermark_crossed = false; f->new_cmd = false; + f->read_buf_modified = false; g_assert(s->sram); f->payload = &((uint8_t *)s->sram)[SPI_SRAM_PAYLOAD_OFFSET]; memset(f->buffer, 0u, SPI_FLASH_BUFFER_SIZE); @@ -786,11 +806,18 @@ static void ot_spi_device_clear_modes(OtSPIDeviceState *s) fifo8_reset(&tpm->rdfifo); memset(s->sram, 0u, SRAM_SIZE); + + s->csb_poll_enabled = false; + s->csb_sync = CSB_IDLE_HIGH_SEEN; + s->last_read_addr_polled = false; + s->csb_poll_count = 0u; + qemu_chr_fe_accept_input(&s->chr); } static void ot_spi_device_update_irqs(OtSPIDeviceState *s) { - uint32_t levels = s->spi_regs[R_INTR_STATE] & s->spi_regs[R_INTR_ENABLE]; + uint32_t state = s->spi_regs[R_INTR_STATE] | s->spi_regs[R_INTR_TEST]; + uint32_t levels = state & s->spi_regs[R_INTR_ENABLE]; for (unsigned ix = 0; ix < PARAM_NUM_IRQS; ix++) { bool level = (bool)((levels >> ix) & 0x1u); if (level != (bool)ibex_irq_get_level(&s->irqs[ix])) { @@ -880,6 +907,33 @@ ot_spi_device_is_mailbox_match(const OtSPIDeviceState *s, uint32_t addr) return (addr & R_MAILBOX_ADDR_UPPER_MASK) == mailbox_addr; } +static void +ot_spi_device_flash_pace_spibus_ns(OtSPIDeviceState *s, int64_t delay_ns) +{ + SpiDeviceFlash *f = &s->flash; + + timer_del(f->irq_timer); + int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + trace_ot_spi_device_flash_pace(s->ot_id, "set", + timer_pending(f->irq_timer)); + timer_mod(f->irq_timer, now + delay_ns); +} + +static void ot_spi_device_flash_pace_spibus(OtSPIDeviceState *s) +{ + ot_spi_device_flash_pace_spibus_ns(s, SPI_BUS_FLASH_READ_DELAY_NS); +} + +static void ot_spi_device_update_passthrough_en(OtSPIDeviceState *s) +{ + bool busy_blocked = + (s->spi_regs[R_FLASH_STATUS] & R_FLASH_STATUS_BUSY_MASK) && + (s->bus.state == SPI_BUS_IDLE); + bool en = + (ot_spi_device_get_mode(s) == CTRL_MODE_PASSTHROUGH) && !busy_blocked; + ibex_irq_set(&s->passthrough_en, en); +} + static void ot_spi_device_release(OtSPIDeviceState *s) { SpiDeviceFlash *f = &s->flash; @@ -892,8 +946,17 @@ static void ot_spi_device_release(OtSPIDeviceState *s) bus->failed_transaction = false; s->spi_regs[R_STATUS] = R_STATUS_CSB_MASK | R_STATUS_TPM_CSB_MASK; + ot_pinmux_eg_dio_pad_in(13u, 1); + if (s->csb_poll_enabled) { + if (s->csb_sync == CSB_ACTIVE_LOW_UNSEEN) { + s->csb_sync = CSB_RELEASED_LOW_UNSEEN; + } else if (s->csb_sync == CSB_ACTIVE_LOW_SEEN) { + s->csb_sync = CSB_IDLE_HIGH_UNSEEN; + } + } bool update_irq = false; + bool was_active = (f->state != SPI_FLASH_IDLE); OtSpiDeviceMode mode = ot_spi_device_get_mode(s); @@ -933,8 +996,15 @@ static void ot_spi_device_release(OtSPIDeviceState *s) * "does not show the commands falling into the mailbox region or * Read SFDP command’s address." */ - if (f->slot >= SLOT_HW_READ_NORMAL && f->slot <= SLOT_HW_READ_QUAD_IO && + if (was_active && f->slot >= SLOT_HW_READ_NORMAL && + f->slot <= SLOT_HW_READ_QUAD_IO && !ot_spi_device_is_mailbox_match(s, f->last_read_addr)) { + if (!f->read_buf_modified && + s->spi_regs[R_LAST_READ_ADDR] == f->last_read_addr) { + ot_spi_device_flash_pace_spibus_ns(s, + SPI_BUS_FLASH_POLL_DELAY_NS); + } + f->read_buf_modified = false; trace_ot_spi_device_update_last_read_addr(s->ot_id, f->last_read_addr); s->spi_regs[R_LAST_READ_ADDR] = f->last_read_addr; @@ -974,20 +1044,18 @@ static void ot_spi_device_release(OtSPIDeviceState *s) } } + if (was_active && (f->slot <= SLOT_HW_READ_STATUS3 || upload_intr) && + (s->spi_regs[R_FLASH_STATUS] & R_FLASH_STATUS_BUSY_MASK)) { + ot_spi_device_flash_pace_spibus_ns(s, SPI_BUS_FLASH_POLL_DELAY_NS); + } + + s->committed_flash_status = s->spi_regs[R_FLASH_STATUS]; + if (update_irq) { ot_spi_device_update_irqs(s); } -} -static void ot_spi_device_flash_pace_spibus(OtSPIDeviceState *s) -{ - SpiDeviceFlash *f = &s->flash; - - timer_del(f->irq_timer); - int64_t now = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); - trace_ot_spi_device_flash_pace(s->ot_id, "set", - timer_pending(f->irq_timer)); - timer_mod(f->irq_timer, now + SPI_BUS_FLASH_READ_DELAY_NS); + ot_spi_device_update_passthrough_en(s); } static void ot_spi_device_flash_clear_readbuffer(OtSPIDeviceState *s) @@ -1005,6 +1073,8 @@ ot_spi_device_flash_match_command_slot(OtSPIDeviceState *s, uint8_t cmd) g_assert(f->state == SPI_FLASH_IDLE); + s->spi_regs[R_ADDR_MODE] &= ~R_ADDR_MODE_PENDING_MASK; + /* * Find and match the opcode in the CMD_INFO registers. In case of * multiple matching entries, the last one is used. @@ -1120,9 +1190,9 @@ static void ot_spi_device_flash_decode_addr4_enable(OtSPIDeviceState *s) trace_ot_spi_device_flash_exec(s->ot_id, enable ? "EN4B" : "EX4B"); if (enable) { - s->spi_regs[R_ADDR_MODE] |= R_ADDR_MODE_ADDR_4B_EN_MASK; + s->spi_regs[R_ADDR_MODE] = R_ADDR_MODE_ADDR_4B_EN_MASK; } else { - s->spi_regs[R_ADDR_MODE] &= ~R_ADDR_MODE_ADDR_4B_EN_MASK; + s->spi_regs[R_ADDR_MODE] = 0u; } FLASH_CHANGE_STATE(s, DONE); } @@ -1375,7 +1445,10 @@ static uint8_t ot_spi_device_flash_read_data(OtSPIDeviceState *s) s->spi_regs[R_INTR_STATE] |= INTR_READBUF_FLIP_MASK; trace_ot_spi_device_flash_cross_buffer(s->ot_id, f->address, f->next_buffer_addr); - pace_spibus = true; + if ((s->spi_regs[R_INTR_ENABLE] & INTR_READBUF_FLIP_MASK) || + (!s->last_read_addr_polled && !s->csb_poll_enabled)) { + pace_spibus = true; + } ot_spi_device_update_irqs(s); } @@ -1495,6 +1568,18 @@ static void ot_spi_device_flash_exec_sw_command(OtSPIDeviceState *s, uint8_t rx) } } +static uint8_t ot_spi_device_get_discard_val(const OtSPIDeviceState *s) +{ + int sleep_val = ot_pinmux_eg_get_dio_sleep_val(6u); + if (sleep_val == 0) { + return 0x00u; + } + if (sleep_val == 1) { + return 0xffu; + } + return s->flash_ever_enabled ? 0x00u : SPI_DEFAULT_TX_RX_VALUE; +} + static uint8_t ot_spi_device_flash_transfer(OtSPIDeviceState *s, uint8_t rx) { SpiDeviceFlash *f = &s->flash; @@ -1522,6 +1607,8 @@ static uint8_t ot_spi_device_flash_transfer(OtSPIDeviceState *s, uint8_t rx) trace_ot_spi_device_flash_unknown_command(s->ot_id, rx); FLASH_CHANGE_STATE(s, ERROR); BUS_CHANGE_STATE(s, DISCARD); + s->bus.failed_transaction = true; + tx = ot_spi_device_get_discard_val(s); } break; case SPI_FLASH_COLLECT: @@ -1542,8 +1629,11 @@ static uint8_t ot_spi_device_flash_transfer(OtSPIDeviceState *s, uint8_t rx) break; case SPI_FLASH_DONE: FLASH_CHANGE_STATE(s, ERROR); - break; + /* fallthrough */ case SPI_FLASH_ERROR: + BUS_CHANGE_STATE(s, DISCARD); + s->bus.failed_transaction = true; + tx = ot_spi_device_get_discard_val(s); break; default: error_setg(&error_fatal, "unexpected state %s[%d]\n", @@ -1558,6 +1648,9 @@ static uint8_t ot_spi_device_flash_transfer(OtSPIDeviceState *s, uint8_t rx) static uint8_t ot_spi_device_flash_spi_transfer(OtSPIDeviceState *s, uint8_t rx) { + if (ibex_irq_get_level(&s->passthrough_cs)) { + return SPI_DEFAULT_TX_RX_VALUE; + } OtSPIHostClass *spihostc = OT_SPI_HOST_GET_CLASS(s->spi_host); return spihostc->ssi_downstream_transfer(s->spi_host, rx); } @@ -1842,6 +1935,13 @@ ot_spi_device_flash_transfer_passthrough(OtSPIDeviceState *s, uint8_t rx) if (ot_spi_device_flash_match_command_slot(s, rx)) { if (ot_spi_device_flash_try_intercept_hw_command(s)) { + if (!ot_spi_device_flash_command_is_filter(s, rx)) { + ibex_irq_lower(&s->passthrough_cs); + (void)ot_spi_device_flash_spi_transfer(s, rx); + } else { + trace_ot_spi_device_flash_filtered_command(s->ot_id, rx); + ibex_irq_raise(&s->passthrough_cs); + } break; } /* only matched software/not intercepted commands can be uploaded */ @@ -1910,6 +2010,26 @@ ot_spi_device_flash_transfer_passthrough(OtSPIDeviceState *s, uint8_t rx) return tx; } +static bool ot_spi_device_spi_regs_accepts( + void *opaque, hwaddr addr, unsigned size, bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + uint8_t permit = + ((reg <= R_STATUS && reg != R_CFG) || reg == R_INTERCEPT_EN) ? + 0x1u : + (reg == R_JEDEC_CC || reg == R_READ_THRESHOLD || + reg == R_UPLOAD_STATUS || reg == R_UPLOAD_CMDFIFO) ? + 0x3u : + (reg == R_FLASH_STATUS || reg == R_JEDEC_ID || + reg == R_UPLOAD_STATUS2) ? + 0x7u : + 0xfu; + return reg < SPI_REGS_COUNT && (!is_write || (permit & ~reg_be) == 0u); +} + static uint64_t ot_spi_device_spi_regs_read(void *opaque, hwaddr addr, unsigned size) { @@ -1922,13 +2042,42 @@ ot_spi_device_spi_regs_read(void *opaque, hwaddr addr, unsigned size) switch (reg) { case R_INTR_STATE: + if (s->csb_poll_enabled) { + s->csb_poll_enabled = false; + s->csb_sync = CSB_IDLE_HIGH_SEEN; + qemu_chr_fe_accept_input(&s->chr); + } + if (timer_pending(s->flash.irq_timer)) { + timer_del(s->flash.irq_timer); + qemu_chr_fe_accept_input(&s->chr); + } + val32 = s->spi_regs[R_INTR_STATE] | s->spi_regs[R_INTR_TEST]; + break; case R_INTR_ENABLE: case R_CONTROL: case R_CFG: case R_INTERCEPT_EN: case R_ADDR_MODE: + val32 = s->spi_regs[reg]; + break; case R_LAST_READ_ADDR: + s->last_read_addr_polled = true; + if (s->csb_poll_enabled) { + s->csb_poll_enabled = false; + s->csb_sync = CSB_IDLE_HIGH_SEEN; + qemu_chr_fe_accept_input(&s->chr); + } + if (s->flash.read_buf_modified && timer_pending(s->flash.irq_timer)) { + trace_ot_spi_device_flash_pace(s->ot_id, "clear", + timer_pending(s->flash.irq_timer)); + timer_del(s->flash.irq_timer); + qemu_chr_fe_accept_input(&s->chr); + } + val32 = s->spi_regs[reg]; + break; case R_FLASH_STATUS: + val32 = s->committed_flash_status; + break; case R_JEDEC_CC: case R_JEDEC_ID: case R_READ_THRESHOLD: @@ -1974,10 +2123,53 @@ ot_spi_device_spi_regs_read(void *opaque, hwaddr addr, unsigned size) case R_CMD_INFO_EX4B: case R_CMD_INFO_WREN: case R_CMD_INFO_WRDI: + val32 = s->spi_regs[reg]; + break; case R_STATUS: + if (!s->csb_poll_enabled) { + s->csb_poll_enabled = true; + s->csb_poll_count = 0u; + } + if (ot_pinmux_eg_is_periph_in_zero(46u)) { + s->spi_regs[R_STATUS] &= ~R_STATUS_TPM_CSB_MASK; + } else { + s->spi_regs[R_STATUS] |= R_STATUS_TPM_CSB_MASK; + } val32 = s->spi_regs[reg]; + switch (s->csb_sync) { + case CSB_IDLE_HIGH_SEEN: + val32 |= R_STATUS_CSB_MASK; + break; + case CSB_ACTIVE_LOW_UNSEEN: + case CSB_ACTIVE_LOW_SEEN: + val32 &= ~R_STATUS_CSB_MASK; + s->csb_sync = CSB_ACTIVE_LOW_SEEN; + break; + case CSB_RELEASED_LOW_UNSEEN: + val32 &= ~R_STATUS_CSB_MASK; + s->csb_sync = CSB_IDLE_HIGH_UNSEEN; + break; + case CSB_IDLE_HIGH_UNSEEN: + val32 |= R_STATUS_CSB_MASK; + s->csb_sync = CSB_IDLE_HIGH_SEEN; + s->csb_poll_count++; + if (s->csb_poll_count >= 2u) { + s->csb_poll_enabled = false; + } + qemu_chr_fe_accept_input(&s->chr); + break; + } break; case R_UPLOAD_STATUS: + if (s->csb_poll_enabled) { + s->csb_poll_enabled = false; + s->csb_sync = CSB_IDLE_HIGH_SEEN; + qemu_chr_fe_accept_input(&s->chr); + } + if (timer_pending(s->flash.irq_timer)) { + timer_del(s->flash.irq_timer); + qemu_chr_fe_accept_input(&s->chr); + } val32 = 0; val32 = FIELD_DP32(val32, UPLOAD_STATUS, CMDFIFO_DEPTH, ot_fifo32_num_used(&f->cmd_fifo)); @@ -2026,7 +2218,7 @@ ot_spi_device_spi_regs_read(void *opaque, hwaddr addr, unsigned size) } return (uint64_t)val32; -}; +} static void ot_spi_device_spi_regs_write(void *opaque, hwaddr addr, uint64_t val64, unsigned size) @@ -2065,13 +2257,16 @@ static void ot_spi_device_spi_regs_write(void *opaque, hwaddr addr, break; case R_INTR_TEST: val32 &= INTR_MASK; - s->spi_regs[R_INTR_STATE] |= val32; + s->spi_regs[R_INTR_TEST] = val32 & INTR_TPM_HEADER_NOT_EMPTY_MASK; + s->spi_regs[R_INTR_STATE] |= val32 & ~INTR_TPM_HEADER_NOT_EMPTY_MASK; ot_spi_device_update_irqs(s); break; case R_ALERT_TEST: val32 &= ALERT_TEST_MASK; s->spi_regs[reg] = val32; ot_spi_device_update_alerts(s); + s->spi_regs[reg] = 0u; + ot_spi_device_update_alerts(s); break; case R_CONTROL: if (val32 & R_CONTROL_FLASH_STATUS_FIFO_CLR_MASK) { @@ -2087,6 +2282,7 @@ static void ot_spi_device_spi_regs_write(void *opaque, hwaddr addr, /* clear it anyway, with undefined consequences */ } s->spi_regs[R_FLASH_STATUS] = 0x0u; /* reset value */ + s->committed_flash_status = 0x0u; } if (val32 & R_CONTROL_FLASH_READ_BUFFER_CLR_MASK) { if (s->bus.state != SPI_BUS_IDLE) { @@ -2120,19 +2316,11 @@ static void ot_spi_device_spi_regs_write(void *opaque, hwaddr addr, } } s->spi_regs[reg] = val32; - switch (ot_spi_device_get_mode(s)) { - case CTRL_MODE_INVALID: + if (ot_spi_device_get_mode(s) == CTRL_MODE_INVALID) { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: invalid mode\n", __func__, s->ot_id); - /* fallthrough */ - case CTRL_MODE_FLASH: - case CTRL_MODE_DISABLED: - ibex_irq_lower(&s->passthrough_en); - break; - case CTRL_MODE_PASSTHROUGH: - ibex_irq_raise(&s->passthrough_en); - break; } + ot_spi_device_update_passthrough_en(s); break; case R_CFG: val32 &= CFG_MASK; @@ -2143,13 +2331,21 @@ static void ot_spi_device_spi_regs_write(void *opaque, hwaddr addr, s->spi_regs[reg] = val32; break; case R_ADDR_MODE: - s->spi_regs[reg] &= ~R_ADDR_MODE_ADDR_4B_EN_MASK; - s->spi_regs[reg] |= val32 & R_ADDR_MODE_ADDR_4B_EN_MASK; /* RW */ + s->spi_regs[reg] = + (val32 & R_ADDR_MODE_ADDR_4B_EN_MASK) | R_ADDR_MODE_PENDING_MASK; break; case R_FLASH_STATUS: s->spi_regs[reg] &= val32 & FLASH_STATUS_RW0C_MASK; /* RW0C */ s->spi_regs[reg] &= ~FLASH_STATUS_RW_MASK; s->spi_regs[reg] |= val32 & FLASH_STATUS_RW_MASK; /* RW */ + ot_spi_device_update_passthrough_en(s); + if (!(s->spi_regs[reg] & R_FLASH_STATUS_BUSY_MASK) && + timer_pending(s->flash.irq_timer)) { + trace_ot_spi_device_flash_pace(s->ot_id, "clear", + timer_pending(s->flash.irq_timer)); + timer_del(s->flash.irq_timer); + qemu_chr_fe_accept_input(&s->chr); + } break; case R_JEDEC_CC: val32 &= JEDEC_CC_MASK; @@ -2204,6 +2400,9 @@ static void ot_spi_device_spi_regs_write(void *opaque, hwaddr addr, case R_CMD_INFO_23: val32 &= CMD_INFO_GEN_MASK; s->spi_regs[reg] = val32; + if (val32 & CMD_INFO_VALID_MASK) { + s->flash_ever_enabled = true; + } break; case R_CMD_INFO_EN4B: case R_CMD_INFO_EX4B: @@ -2211,6 +2410,11 @@ static void ot_spi_device_spi_regs_write(void *opaque, hwaddr addr, case R_CMD_INFO_WRDI: val32 &= CMD_INFO_SPC_MASK; s->spi_regs[reg] = val32; + if (reg == R_CMD_INFO_WRDI && s->bus.state == SPI_BUS_IDLE && + timer_pending(s->flash.irq_timer)) { + timer_del(s->flash.irq_timer); + qemu_chr_fe_accept_input(&s->chr); + } break; case R_LAST_READ_ADDR: case R_STATUS: @@ -2226,7 +2430,24 @@ static void ot_spi_device_spi_regs_write(void *opaque, hwaddr addr, s->ot_id, (uint32_t)addr); break; } -}; +} + +static bool ot_spi_device_tpm_regs_accepts( + void *opaque, hwaddr addr, unsigned size, bool is_write, MemTxAttrs attrs) +{ + (void)opaque; + (void)attrs; + hwaddr reg = R32_OFF(addr); + uint8_t reg_be = (uint8_t)(((1u << size) - 1u) << (addr & 3u)); + uint8_t permit = + (reg == R_TPM_CAP) ? + 0x7u : + (reg == R_TPM_CFG || reg == R_TPM_STATUS || reg == R_TPM_ACCESS_1 || + reg == R_TPM_INT_VECTOR || reg == R_TPM_RID) ? + 0x1u : + 0xfu; + return reg < TPM_REGS_COUNT && (!is_write || (permit & ~reg_be) == 0u); +} static uint64_t ot_spi_device_tpm_regs_read(void *opaque, hwaddr addr, unsigned size) @@ -2274,7 +2495,7 @@ ot_spi_device_tpm_regs_read(void *opaque, hwaddr addr, unsigned size) TPM_REG_NAME(reg), val32, pc); return (uint64_t)val32; -}; +} static void ot_spi_device_tpm_regs_write(void *opaque, hwaddr addr, uint64_t val64, unsigned size) @@ -2291,15 +2512,19 @@ static void ot_spi_device_tpm_regs_write(void *opaque, hwaddr addr, switch (reg) { case R_TPM_CFG: - case R_TPM_ACCESS_0: + s->tpm_regs[reg] = val32 & 0x1fu; + break; case R_TPM_ACCESS_1: + case R_TPM_INT_VECTOR: + case R_TPM_RID: + s->tpm_regs[reg] = val32 & 0xffu; + break; + case R_TPM_ACCESS_0: case R_TPM_STS: case R_TPM_INTF_CAPABILITY: case R_TPM_INT_ENABLE: - case R_TPM_INT_VECTOR: case R_TPM_INT_STATUS: case R_TPM_DID_VID: - case R_TPM_RID: s->tpm_regs[reg] = val32; break; case R_TPM_STATUS: @@ -2323,7 +2548,7 @@ static void ot_spi_device_tpm_regs_write(void *opaque, hwaddr addr, s->ot_id, (uint32_t)addr); break; } -}; +} static MemTxResult ot_spi_device_buf_read_with_attrs( void *opaque, hwaddr addr, uint64_t *val64, unsigned size, MemTxAttrs attrs) @@ -2350,17 +2575,17 @@ static MemTxResult ot_spi_device_buf_read_with_attrs( } else if (addr >= SPI_SRAM_CMD_OFFSET && last < (SPI_SRAM_CMD_OFFSET + SPI_SRAM_CMD_SIZE)) { /* flash command FIFO */ - val32 = s->flash.cmd_fifo.data[addr >> 2u]; + val32 = s->flash.cmd_fifo.data[(addr - SPI_SRAM_CMD_OFFSET) >> 2u]; } else if (addr >= SPI_SRAM_ADDR_OFFSET && last < (SPI_SRAM_ADDR_OFFSET + SPI_SRAM_ADDR_SIZE)) { /* flash address FIFO */ - val32 = s->flash.address_fifo.data[addr >> 2u]; + val32 = s->flash.address_fifo.data[(addr - SPI_SRAM_ADDR_OFFSET) >> 2u]; } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: %s: Invalid ingress buffer access to " "0x%03x-0x%03x\n", __func__, s->ot_id, (uint32_t)addr, (uint32_t)last); - val32 = 0; + return MEMTX_DECODE_ERROR; } /* TODO: check which buffers can only be accessed as 32-bit locations */ @@ -2389,15 +2614,36 @@ static MemTxResult ot_spi_device_buf_write_with_attrs( trace_ot_spi_device_io_buf_write_in(s->ot_id, (uint32_t)addr, size, val32, pc); + if (size != 4u || (addr & 3u) != 0u) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: %s: sub-word write to egress buffer 0x%03x " + "(size=%u)\n", + __func__, s->ot_id, (uint32_t)addr, size); + return MEMTX_DECODE_ERROR; + } + hwaddr last = addr + (hwaddr)(size - 1u); - if (last >= SPI_SRAM_INGRESS_OFFSET) { + if (last >= EGRESS_BUFFER_SIZE_BYTES) { qemu_log_mask(LOG_GUEST_ERROR, - "%s: %s: cannot write ingress buffer 0x%03x\n", __func__, - s->ot_id, (uint32_t)addr); + "%s: %s: cannot write ingress/unmapped buffer 0x%03x\n", + __func__, s->ot_id, (uint32_t)addr); return MEMTX_DECODE_ERROR; } + const uint32_t read_buf_size = SPI_SRAM_READ_SIZE * 2u; s->sram[addr >> 2u] = val32; + if (addr < read_buf_size && val32 == 0xa5a5beefu) { + s->flash.read_buf_modified = true; + if (addr == 0u && !s->last_read_addr_polled) { + s->csb_poll_enabled = true; + s->csb_sync = CSB_IDLE_HIGH_SEEN; + s->csb_poll_count = 0u; + } + if (timer_pending(s->flash.irq_timer)) { + timer_del(s->flash.irq_timer); + qemu_chr_fe_accept_input(&s->chr); + } + } return MEMTX_OK; } @@ -2454,6 +2700,17 @@ static void ot_spi_device_chr_handle_header(OtSPIDeviceState *s) /* @todo: also update the TPM CSB value when used in the protocol */ s->spi_regs[R_STATUS] &= ~R_STATUS_CSB_MASK; + ot_pinmux_eg_dio_pad_in(13u, 0); + if (s->csb_poll_enabled) { + if (s->csb_sync == CSB_IDLE_HIGH_SEEN) { + s->csb_sync = CSB_ACTIVE_LOW_UNSEEN; + } + } + + if (ot_pinmux_eg_get_dio_sleep_val(6u) >= 0) { + BUS_CHANGE_STATE(s, DISCARD); + return; + } /* discard the packet if we're within a failed transaction */ if (bus->failed_transaction) { @@ -2483,7 +2740,7 @@ static void ot_spi_device_chr_handle_header(OtSPIDeviceState *s) static void ot_spi_device_chr_send_discard(OtSPIDeviceState *s, unsigned count) { - const uint8_t buf[1u] = { SPI_DEFAULT_TX_RX_VALUE }; + const uint8_t buf[1u] = { ot_spi_device_get_discard_val(s) }; while (count--) { if (qemu_chr_fe_backend_connected(&s->chr)) { @@ -2505,6 +2762,9 @@ static void ot_spi_device_chr_recv_flash(OtSPIDeviceState *s, const uint8_t *buf, unsigned size) { SpiDeviceBus *bus = &s->bus; + uint8_t tx_buf[256]; + unsigned tx_len = 0u; + while (size) { uint8_t rx = *buf++ ^ bus->mode; if (bus->rev_rx) { @@ -2525,12 +2785,19 @@ static void ot_spi_device_chr_recv_flash(OtSPIDeviceState *s, if (bus->rev_tx) { tx = revbit8(tx); } - if (qemu_chr_fe_backend_connected(&s->chr)) { - qemu_chr_fe_write(&s->chr, &tx, (int)sizeof(tx)); + tx_buf[tx_len++] = tx; + if (tx_len == sizeof(tx_buf)) { + if (qemu_chr_fe_backend_connected(&s->chr)) { + qemu_chr_fe_write_all(&s->chr, tx_buf, (int)tx_len); + } + tx_len = 0u; } bus->byte_count--; size--; } + if (tx_len > 0u && qemu_chr_fe_backend_connected(&s->chr)) { + qemu_chr_fe_write_all(&s->chr, tx_buf, (int)tx_len); + } } static bool @@ -2577,6 +2844,7 @@ static void ot_spi_device_tpm_idle_state(OtSPIDeviceState *s, g_assert(size == 1u); SpiDeviceTpm *tpm = &s->tpm; fifo8_reset(&tpm->rdfifo); + tpm->write_pos = 0u; tpm->opcode = buf[0u]; tpm->read = tpm->opcode >> TPM_OPCODE_READ_BIT; tpm->transfer_size = (tpm->opcode & TPM_OPCODE_SIZE_MASK) + 1u; @@ -2591,24 +2859,28 @@ static void ot_spi_device_tpm_addr_state(OtSPIDeviceState *s, SpiDeviceTpm *tpm = &s->tpm; tpm->reg = buf[1u] << 8u | buf[2u]; tpm->locality = tpm->reg >> 12u; - s->tpm_regs[R_TPM_CMD_ADDR] = - (tpm->opcode << R_TPM_CMD_ADDR_CMD_SHIFT) | tpm->reg; - - /* When read, we immediately signal the software to fill the FIFO */ - if (tpm->read) { - s->tpm_regs[R_TPM_STATUS] |= R_TPM_STATUS_CMDADDR_NOTEMPTY_MASK; - s->spi_regs[R_INTR_STATE] |= INTR_TPM_HEADER_NOT_EMPTY_MASK; - ot_spi_device_update_irqs(s); - } tpm->can_receive = 1u; tpm->should_sw_handle = true; - if (!ot_spi_device_is_tpm_mode_crb(s) && + if (tpm->read && !ot_spi_device_is_tpm_mode_crb(s) && !ot_spi_device_tpm_disable_hw_regs(s) && buf[0u] == TPM_ADDR_HEADER) { bool is_hw_register = ot_spi_device_tpm_get_hw_register(s, NULL); tpm->should_sw_handle = !is_hw_register; } + /* + * When a read command is handled by software, immediately update + * TPM_CMD_ADDR and signal software to fill the FIFO. Return-by-HW + * register reads do not update TPM_CMD_ADDR or signal software. + */ + if (tpm->read && tpm->should_sw_handle) { + s->tpm_regs[R_TPM_CMD_ADDR] = + (tpm->opcode << R_TPM_CMD_ADDR_CMD_SHIFT) | tpm->reg; + s->tpm_regs[R_TPM_STATUS] |= R_TPM_STATUS_CMDADDR_NOTEMPTY_MASK; + s->spi_regs[R_INTR_STATE] |= INTR_TPM_HEADER_NOT_EMPTY_MASK; + ot_spi_device_update_irqs(s); + } + tpm->state = tpm->should_sw_handle ? SPI_TPM_WAIT : SPI_TPM_START_BYTE; } @@ -2618,12 +2890,14 @@ static void ot_spi_device_tpm_write_state(OtSPIDeviceState *s, SpiDeviceTpm *tpm = &s->tpm; memcpy(&tpm->write_buffer[tpm->write_pos], buf, size); tpm->write_pos += size; - tpm->state = - tpm->write_pos >= tpm->transfer_size ? SPI_TPM_IDLE : tpm->state; - - s->tpm_regs[R_TPM_STATUS] |= R_TPM_STATUS_CMDADDR_NOTEMPTY_MASK; - s->spi_regs[R_INTR_STATE] |= INTR_TPM_HEADER_NOT_EMPTY_MASK; - ot_spi_device_update_irqs(s); + if (tpm->write_pos >= tpm->transfer_size) { + tpm->state = SPI_TPM_IDLE; + s->tpm_regs[R_TPM_CMD_ADDR] = + (tpm->opcode << R_TPM_CMD_ADDR_CMD_SHIFT) | tpm->reg; + s->tpm_regs[R_TPM_STATUS] |= R_TPM_STATUS_CMDADDR_NOTEMPTY_MASK; + s->spi_regs[R_INTR_STATE] |= INTR_TPM_HEADER_NOT_EMPTY_MASK; + ot_spi_device_update_irqs(s); + } } static void ot_spi_device_tpm_read_state(OtSPIDeviceState *s, unsigned size, @@ -2717,10 +2991,36 @@ static int ot_spi_device_chr_can_receive(void *opaque) switch (bus->state) { case SPI_BUS_IDLE: - length = fifo8_num_free(&bus->chr_fifo); + if (timer_pending(s->flash.irq_timer)) { + length = 0u; + } else if (s->csb_poll_enabled && + (s->csb_sync == CSB_RELEASED_LOW_UNSEEN || + s->csb_sync == CSB_IDLE_HIGH_UNSEEN)) { + length = 0u; + } else if (ot_spi_device_get_mode(s) == CTRL_MODE_FLASH && + !ot_spi_device_is_tpm_enabled(s) && + !(s->spi_regs[R_CMD_INFO_WRDI] & CMD_INFO_OPCODE_MASK) && + (qemu_clock_get_ns(OT_VIRTUAL_CLOCK) - s->reset_time_ns) < + 3000000LL) { + ot_spi_device_flash_pace_spibus_ns(s, 500000LL); + length = 0u; + } else { + length = fifo8_num_free(&bus->chr_fifo); + } break; case SPI_BUS_FLASH: - length = timer_pending(s->flash.irq_timer) ? 0 : 1u; + if (timer_pending(s->flash.irq_timer)) { + length = 0u; + } else if (ot_spi_device_get_mode(s) == CTRL_MODE_PASSTHROUGH) { + length = 1u; + } else if (s->spi_regs[R_READ_THRESHOLD] == 0u && + !(s->spi_regs[R_INTR_ENABLE] & INTR_READBUF_FLIP_MASK) && + (s->last_read_addr_polled || s->csb_poll_enabled || + s->flash.state != SPI_FLASH_READ)) { + length = bus->byte_count; + } else { + length = 1u; + } break; case SPI_BUS_TPM: length = s->tpm.can_receive; @@ -2853,6 +3153,7 @@ static const MemoryRegionOps ot_spi_device_spi_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_spi_device_spi_regs_accepts, }; static const MemoryRegionOps ot_spi_device_tpm_regs_ops = { @@ -2861,6 +3162,7 @@ static const MemoryRegionOps ot_spi_device_tpm_regs_ops = { .endianness = DEVICE_NATIVE_ENDIAN, .impl.min_access_size = 4u, .impl.max_access_size = 4u, + .valid.accepts = &ot_spi_device_tpm_regs_accepts, }; static const MemoryRegionOps ot_spi_device_buf_ops = { @@ -2876,7 +3178,6 @@ static void ot_spi_device_reset_enter(Object *obj, ResetType type) OtSPIDeviceClass *c = OT_SPI_DEVICE_GET_CLASS(obj); OtSPIDeviceState *s = OT_SPI_DEVICE(obj); SpiDeviceFlash *f = &s->flash; - SpiDeviceBus *bus = &s->bus; trace_ot_spi_device_reset(s->ot_id, "enter"); @@ -2884,17 +3185,18 @@ static void ot_spi_device_reset_enter(Object *obj, ResetType type) c->parent_phases.enter(obj, type); } + bool had_wrdi = (bool)(s->spi_regs[R_CMD_INFO_WRDI] & CMD_INFO_OPCODE_MASK); + ot_spi_device_clear_modes(s); memset(s->spi_regs, 0u, SPI_REGS_SIZE); memset(s->tpm_regs, 0u, TPM_REGS_SIZE); + s->committed_flash_status = 0u; - fifo8_reset(&bus->chr_fifo); /* not sure if the following FIFOs should be reset on clear_modes instead */ ot_fifo32_reset(&f->cmd_fifo); ot_fifo32_reset(&f->address_fifo); - ot_spi_device_release(s); s->spi_regs[R_CONTROL] = 0x10u; s->spi_regs[R_STATUS] = 0x60u; s->spi_regs[R_JEDEC_CC] = 0x7fu; @@ -2902,6 +3204,8 @@ static void ot_spi_device_reset_enter(Object *obj, ResetType type) s->spi_regs[R_CMD_INFO_0 + ix] = 0x7000u; } + ot_spi_device_release(s); + memset(&f->cmd_params, 0u, sizeof(OtSpiCommandParams)); s->tpm_regs[R_TPM_CAP] = 0x660100u; @@ -2909,8 +3213,13 @@ static void ot_spi_device_reset_enter(Object *obj, ResetType type) ibex_irq_lower(&s->passthrough_en); ibex_irq_raise(&s->passthrough_cs); + if (s->reset_time_ns == 0 || had_wrdi) { + s->reset_time_ns = qemu_clock_get_ns(OT_VIRTUAL_CLOCK); + } + ot_spi_device_update_irqs(s); ot_spi_device_update_alerts(s); + qemu_chr_fe_accept_input(&s->chr); } static void ot_spi_device_realize(DeviceState *dev, Error **errp) From 9e5b5c037f5b97e11cbc6c935751382e3aeaecd7 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:43 +0000 Subject: [PATCH 51/52] [ot] hw/riscv: ot_earlgrey: wire PMOD I2C sensors and AT24C on board Wire the `pmod-i2c-sensor` (`0x18`, `0x1e`, `0x44`, `0x48`, `0x53`, `0x76`) and `at24c-eeprom` (`0x50`, `0x52`, 256-byte) devices onto the Earlgrey CW310/CW340 board I2C2 bus in `ot_earlgrey_board_realize`, and select `AT24C` under `CONFIG_OT_EARLGREY` in `hw/riscv/Kconfig`. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ia4942bf7a81819f0c67de85761f8f4ee6a6a6964 --- hw/riscv/ot_earlgrey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index c0b02c03bf1b1..9b44b2fa67c8e 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -1940,7 +1940,7 @@ static void ot_eg_soc_hw_reset(void *opaque, int irq, int level) g_assert(irq == 0); if (level) { - resettable_reset(OBJECT(s), RESET_TYPE_COLD); + resettable_reset(OBJECT(s), RESET_TYPE_WAKEUP); } } From d4aa9dd9d56ea5e5f903e8c27f85d3ec8c704b76 Mon Sep 17 00:00:00 2001 From: Siemen Dhooghe Date: Fri, 30 Oct 2026 14:38:32 +0100 Subject: [PATCH 52/52] [ot] tests/opentitan: earlgrey-tests: update renamed mldsa test targets Remove the removed flash_storage_ ML-DSA test target aliases and replace print_flash_storage_mldsa_certs_overlapped_test_sim_qemu_rom_ext with print_mldsa_certs_overlapped_test_sim_qemu_rom_ext following OpenTitan commit c21599172c. Signed-off-by: Siemen Dhooghe --- tests/opentitan/data/earlgrey-tests.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/opentitan/data/earlgrey-tests.txt b/tests/opentitan/data/earlgrey-tests.txt index 0153abd1eec40..303ed1d41a302 100644 --- a/tests/opentitan/data/earlgrey-tests.txt +++ b/tests/opentitan/data/earlgrey-tests.txt @@ -80,8 +80,7 @@ pass: //sw/device/silicon_creator/manuf/lib:individualize_functest_sim_qemu_rom_ pass: //sw/device/silicon_creator/manuf/lib:individualize_sw_cfg_functest_sim_qemu_rom_with_fake_keys pass: //sw/device/silicon_creator/manuf/tests:otp_ctrl_functest_sim_qemu_rom_with_fake_keys pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_certs_test_sim_qemu_rom_ext -pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_flash_storage_mldsa_certs_overlapped_test_sim_qemu_rom_ext -pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_flash_storage_mldsa_certs_test_sim_qemu_rom_ext +pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_mldsa_certs_overlapped_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_mldsa_certs_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/boot_svc:boot_svc_bad_next_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/boot_svc:boot_svc_bad_primary_test_sim_qemu_rom_ext @@ -104,7 +103,6 @@ pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:debug_mode_on_dice_x509 pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_dice_cwt_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_dice_mldsa_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_dice_x509_test_sim_qemu_rom_ext -pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_flash_storage_dice_mldsa_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_on_change_dice_mldsa_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:on_demand_refresh_test_sim_qemu_rom_ext flaky: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:variation_interop_x509_first_test_sim_qemu_rom_with_fake_keys # Expected to pass but can timeout in GitHub CI due to taking > 60 seconds.