From 4177e98dfb04d04ce4c59e2b93d6e850cfd92efa Mon Sep 17 00:00:00 2001 From: Baptiste MOINE Date: Sat, 31 Oct 2026 17:30:05 +0100 Subject: [PATCH] Add fault injection for chips and rails --- DESIGN.md | 53 ++++++++++++++++++- Makefile | 2 +- README.md | 5 +- display/config_window.cpp | 54 ++++++++++++++++++++ emu_state.h | 85 ++++++++++++++++++++++++++++++ main.cpp | 37 +++++++++++++- t210/ccplex.cpp | 15 +++++- t210/mmio.cpp | 105 +++++++++++++++++++++++++++++++++++--- t210/mmio.h | 10 +++- tests/ccplex/payload.c | 18 ++++++- tests/ccplex/run.sh | 63 +++++++++++++++++++++++ 11 files changed, 431 insertions(+), 16 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 8fe3297..14a7f5f 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -804,7 +804,8 @@ emulated second. `make test` runs `tests/ccplex/`, a self-contained payload that checks the refusal, the EL3 entry, a core released with its clock stopped, the -mailbox, WFE parking and the reset; +mailbox, WFE parking and the reset, then the same payload under the +`vdd_cpu`, `max77621_cpu` and `max77812` faults (see Fault injection); `tests/se/`, which runs the SE's RSA, SHA-256, AES and RNG against vectors computed in Python (`gen_vectors.py` writes `vectors.h`); `tests/usb/`, a mass storage gadget over a RAM disk on each device controller (writable, and @@ -822,6 +823,56 @@ blended over it). `check.py` compares every pixel of the frame the emulator shows with the picture the payload drew. `tests/hwtest/` builds hwtest-rcm with distro toolchains and runs its whole sweep; CI runs all four. +## Fault injection + +A diagnostic payload is only tested once it has seen broken hardware, so +chips and rails can be killed: `--fault NAME` (repeatable, comma lists), +`NAME=dead` under `[faults]` in `rcm_emu.ini`, or the config window's Faults +section, which applies a change at once. The table is `kFaults` in +`emu_state.h`; `--fault list` prints it. A fault is hardware, so a payload +reboot keeps it. + +**Coherence is the rule.** A fault has to show up identically in every model +that depends on it, because a payload judges a part by what its neighbours +do. So a part is listed only once all of its modelled consequences are wired, +and a name with none is left out rather than accepted - otherwise a payload +could "pass" against a fault that cannot fail it. The two kinds: + +| Kind | What it models | Where it acts | +| --- | --- | --- | +| chip | The part is dead. | `i2c_slave_present()` NAKs its address (normal and packet mode alike, both read `I2C_STATUS`); the regulator write paths commit nothing to a NAKing slave; `cpu_rail_fault()` takes down every rail the chip supplies. | +| rail | The regulator answers and takes its enable, but delivers nothing - an open inductor, a shorted output cap. | Only what the rail feeds. Nothing changes over I2C: that is the case a probe reading regulator registers back cannot see, and the reason it is worth modelling. | + +| Fault | Consequences | +| --- | --- | +| `vdd_cpu` | `cpu_rail_on()` is false whatever the registers say. `FLOW_CTLR_RAM_REPAIR` leaves a request pending with STS clear (no rail, no acks from the CPU repair chain - see below), CPU0 refuses release with `CPU rail has no output (fault: vdd_cpu ...)`, and a running CPU0 stops. bdk's `ccplex_boot_cpu0()` spins on that STS with no timeout, so an unguarded payload hangs there - the "frozen while booting the CPU" console. | +| `max77621_cpu` (Erista), `max77812` (Mariko) | The chip fault, plus `vdd_cpu`. This matters on Erista, where the MAX77621's register file is seeded with VOUT_EN set: without the cascade the rail would stay up behind a chip that no longer answers. A fault on the other generation's chip changes nothing. | +| `max77621_gpu`, `bq24193`, `max17050`, `tmp451`, `bm92t36` | NAK only. Nothing else in the emulator consumes them. | + +The RAM repair coupling also holds with no fault at all, because it is how +the part works: software powers the CPU rail, then sets `RAM_REPAIR[REQ]` +and polls `[STS]`, which reports the acknowledgments of every segment of the +CPU repair chain, and a request "must remain high for as long as it does not +receive the corresponding ack signal" (TRM 17.1.3.2). So a repair requested +with the rail off stays pending and completes once the rail is up. Every +payload seen so far powers the rail first, as bdk does. + +The PMC partitions (CRAIL, C0NC, CE0) still ungate under a dead rail. The PMC +only takes a CPU power-good indication from the PMIC when +`PMC_CNTRL.CPUPWRGOOD_EN` is set; it resets to 0, bdk never sets it, and the +CPUPWRGOOD/CPUPWROFF timers "are used only when the platform supports a CPU +power good indication" (TRM 12.4.5 CPU Rail Partitions, 12.6.1 +APBDEV_PMC_CNTRL_0). So the +partition reads powered either way and the step that really needs the rail +is the RAM repair. A payload that did enable CPUPWRGOOD would make the ungate +wait on the PMIC's pin; none does, and that is not modelled. + +Deliberately not offered: the MAX77620 itself (nothing would run), the +ALC5639 codec (audio leaves through I2S whether or not it answers, so "dead" +would be half-modelled), and the GPU and DRAM rails (no consumer modelled). +Adding one means wiring every consequence first; `tests/ccplex/run.sh` shows +how a fault scenario is checked. + ## Bug history These are baked into the current source. Listed here so future maintainers diff --git a/Makefile b/Makefile index 2e9f5a2..bd4c2f0 100644 --- a/Makefile +++ b/Makefile @@ -124,7 +124,7 @@ test: $(OUTPUT) tests/ccplex/payload.bin tests/se/payload.bin $(USB_PAYLOADS) $( clean: rm -f $(OUTPUT) $(OBJS) $(OBJS:.o=.d) \ - tests/ccplex/payload.elf tests/ccplex/payload.bin tests/ccplex/out.log \ + tests/ccplex/payload.elf tests/ccplex/payload.bin tests/ccplex/out.log tests/ccplex/out_fault_*.log \ tests/se/payload.elf tests/se/payload.bin tests/se/out.log \ tests/usb/payload_*.elf tests/usb/payload_*.bin tests/usb/out_*.log \ tests/display/payload_*.elf tests/display/payload_*.bin \ diff --git a/README.md b/README.md index 61168a0..bc5dc3c 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,9 @@ tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin `make test` runs small self-contained payloads: one boots CPU0 and checks the refusal of an unpowered release, the EL3 entry, the mailbox, a stopped clock, -WFE parking and the reset; one checks the Security Engine's RSA, SHA-256, AES +WFE parking and the reset, then runs again with a dead CPU rail and dead +bucks (`--fault`) and checks the RAM repair is held and the core stays dark; +one checks the Security Engine's RSA, SHA-256, AES and RNG against vectors computed in Python; one brings up a USB mass storage gadget on each device controller (and a HID gadget) for the `--usb-host` PC to enumerate, read back and write, and serves it over NBD to a Python client; @@ -318,6 +320,7 @@ needed. | `--oem` | `erista` \| `mariko` | Switch SoC generation. Drives `APB_MISC_GP_HIDREV` so Hekate's `h_cfg.t210b01` matches and pkg1 identification skips the right OEM header. Default `erista`. | | `--bt-radio` | `healthy` \| `faulty` \| `absent` | Broadcom CYW4356 behaviour on UART-D. `healthy` powers up on the `BT_REG_ON` edge, holds `BT_HOST_WAKE` high, asserts `RTS_N` and answers HCI; `faulty` reproduces the 2110-1118 console (module fitted, never leaves POR); `absent` additionally leaves `BT_UART_RXD` in a break condition. Overrides `[bluetooth] radio` in the ini. Default `healthy`. | | `--wifi-radio` | `healthy` \| `faulty` \| `absent` | The WLAN half of the same CYW4356: a PCI Express endpoint on root port 1, reachable only from CPU0 (the BPMP reads all ones, as on silicon). `healthy` trains the link, enumerates as `14E4:43EC` and answers a ChipCommon ChipID read with `0x4356`; `faulty` still trains and enumerates but reads all ones on the backplane (live PCIe front-end, dead radio die); `absent` never leaves detect. The model also enforces the datasheet's power-up ordering, so a payload that releases PERST# too early gets a link that stays down and a `[pcie]` line naming the reason. Overrides `[wifi] radio` in the ini. Default `healthy`. | +| `--fault` | `NAME[,NAME…]` \| `list` | Break a chip or a rail; repeatable. A **chip** fault (`max77812`, `max77621_cpu`, `max77621_gpu`, `bq24193`, `max17050`, `tmp451`, `bm92t36`) makes the part NAK its I2C address, refuse writes and take every rail it supplies down with it. A **rail** fault (`vdd_cpu`) leaves the regulator answering and reading back as enabled while its output never comes up, so only what the rail feeds notices: the CPU RAM repair never completes and CPU0 cannot run. `list` prints the names and exits. Faults survive a payload reboot, like the hardware they model, and add to `[faults]` in the ini (`NAME=dead`). See [DESIGN.md](DESIGN.md#fault-injection). | | `--usb-host` | `nbd[:port]` (optional) | A PC on the USB-C port. It enumerates whatever USB gadget the payload brings up; a mass storage disk is read back (checked against the SD image), its last 8 sectors are written back unchanged and read again unless it is write-protected, and it is ejected; HID reports are polled. With `nbd` the disk is served over NBD on 127.0.0.1 (port 10809 by default) until the client disconnects, then ejected - see [Mounting a gadget's disk](#mounting-a-gadgets-disk). Logged as `[usb-host]`; `RCM_USB_TRACE=1` traces every transfer. Overrides `[usb] host` and `[usb] nbd_port` in the ini. | | `--input-script` | file or spec | Scripted input keyed to emulated time: ` P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, ` TAP [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, `+N` times an event N ms after the previous one. Events are separated by `,`, `;` or newlines. | | `--auto-pin-recovery` | (none) | Drive the Lockpick PIN-recovery menu without user input. | diff --git a/display/config_window.cpp b/display/config_window.cpp index 60a220d..b2e4106 100644 --- a/display/config_window.cpp +++ b/display/config_window.cpp @@ -3,6 +3,7 @@ #include "config_window.h" #include "emu_state.h" +#include "t210/mmio.h" #include "imgui.h" #include "backends/imgui_impl_sdl2.h" @@ -58,6 +59,7 @@ void reset_to_defaults(EmuState *s) { s->pmic_cnfg1_32k = 0xFC; // 32K_OK | 32K_OUT0_EN s->bt_radio = BT_RADIO_HEALTHY; s->wifi_radio = WIFI_RADIO_HEALTHY; + s->faults = 0; s->sd_inserted = true; s->sd_cid_manfid = 0x03; s->sd_cid_oemid = 0x5344; // "SD" @@ -218,6 +220,12 @@ bool config_window_save_ini_impl(const EmuState *s, const char *path) { write_header("wifi"); fprintf(f, "radio=%s\n", wifi_radio_name(s->wifi_radio.load())); + // Every fault, set or not, so the file itself lists what can be broken. + write_header("faults"); + for (const FaultDef &d : kFaults) + fprintf(f, "%s=%s\n", d.name, (s->faults.load() & d.id) ? "dead" : "ok"); + + // Fuses are dumped wholesale — small enough (256 × 4 bytes) and keeps the // load path branch-free. Zero words too: loading layers the file over // init_fuse_defaults(), so a fuse cleared in the UI and left out here @@ -310,6 +318,25 @@ bool config_window_load_ini_impl(EmuState *s, const char *path) { } continue; } + if (strcmp(section, "faults") == 0) { + // One key per fault, value dead or ok. Same string-vs-schema + // reasoning as [bluetooth]: an unknown name or value is reported, + // never quietly read as healthy. + const FaultDef *f = fault_find(key); + bool dead = false; + if (!f) { + fprintf(stderr, "[config] Unknown fault '%s' in [faults];" + " run with --fault list for the names\n", key); + } else if (!fault_value_parse(val, &dead)) { + fprintf(stderr, "[config] [faults] %s='%s': expected dead or ok\n", + key, val); + } else if (dead) { + s->faults |= f->id; + } else { + s->faults &= ~f->id; + } + continue; + } if (strcmp(section, "fuses") == 0) { uint32_t off = (uint32_t)strtoul(key, nullptr, 0); if (off < EmuState::FUSE_WORDS * 4 && (off & 3) == 0) { @@ -464,6 +491,30 @@ void build_ui(EmuState *state) { atomic_slider_int("MAX77620 CNFG1_32K", state->pmic_cnfg1_32k, 0, 255, "0x%02X"); } + if (ImGui::CollapsingHeader("Faults (dead chips and rails)")) { + // Applied at once. A chip fault NAKs from the next transaction; a + // rail fault stops a running CPU0 on the spot, as silicon would. + // Reboot for a clean POR with the fault already in place. + for (const FaultDef &d : kFaults) { + bool dead = (state->faults.load() & d.id) != 0; + char label[48]; + snprintf(label, sizeof(label), "%s (%s)", d.name, d.kind); + if (ImGui::Checkbox(label, &dead)) { + if (dead) + state->faults |= d.id; + else + state->faults &= ~d.id; + printf("[fault] %s %s (config window)\n", d.name, + dead ? "set" : "cleared"); + fflush(stdout); + faults_changed(state); + } + ImGui::SameLine(); + ImGui::TextDisabled("%s", d.what); + } + } + + if (ImGui::CollapsingHeader("Bluetooth (CYW4356)")) { // Selected before the probe runs; press 'a' on the payload's pager to // re-run the sweep after changing it, or Reboot for a clean POR. @@ -934,6 +985,9 @@ void build_ui(EmuState *state) { ImGui::Separator(); if (ImGui::Button("Reset to defaults")) { reset_to_defaults(state); + // Defaults clear every fault (and pick a SoC generation), so a CPU0 + // release a fault was holding back must be looked at again. + faults_changed(state); } ImGui::SameLine(); static double g_save_msg_until = 0.0; diff --git a/emu_state.h b/emu_state.h index f1a4aa0..db8de9d 100644 --- a/emu_state.h +++ b/emu_state.h @@ -91,6 +91,88 @@ inline bool wifi_radio_parse(const char *s, uint8_t *out) { return false; } +// ---- Fault injection ------------------------------------------------------ +// +// Named hardware faults, set by `[faults]` in rcm_emu.ini, `--fault NAME` on +// the command line or the config window's Faults section. They are hardware, +// so a payload reboot keeps them. Two kinds: +// +// chip The part is dead. It NAKs its I2C address, takes no writes, and +// every rail it supplies goes down with it. +// rail The regulator answers, takes its enable and reads back as on, but +// its output never comes up: an open inductor, a shorted output cap. +// Nothing visible over I2C; only what the rail feeds notices. This is +// the hard case, and the one a probe that only reads regulator +// registers back cannot catch. +// +// Coherence is the rule: a part is listed only once everything it feeds that +// the emulator models reacts to it. A name with no modelled consequence would +// let a payload "pass" against a fault that cannot fail it, so it is left out +// rather than accepted. DESIGN.md "Fault injection" has what each one does. +// +// Deliberately absent: +// MAX77620 the main PMIC: with it dead nothing would run to test. +// ALC5639 the codec: audio leaves the emulator through I2S whether or not +// the codec answers, so "dead" would be half-modelled. +// GPU / DRAM rails (MAX77621 0x1C output, MAX77812 M1/M3): no consumer is +// modelled. The chips' own death is offered, since all it does +// is NAK. +enum FaultId : uint32_t { + FAULT_MAX77621_CPU = 1u << 0, // Erista CPU buck, I2C5 0x1B; feeds vdd_cpu + FAULT_MAX77621_GPU = 1u << 1, // Erista GPU buck, I2C5 0x1C + FAULT_MAX77812 = 1u << 2, // Mariko CPU/GPU/DRAM buck, I2C5 0x33; feeds vdd_cpu + FAULT_BQ24193 = 1u << 3, // charger, I2C1 0x6B + FAULT_MAX17050 = 1u << 4, // fuel gauge, I2C1 0x36 + FAULT_TMP451 = 1u << 5, // thermal sensor, I2C1 0x4C + FAULT_BM92T36 = 1u << 6, // USB-PD controller, I2C1 0x18 + FAULT_VDD_CPU = 1u << 7, // the CPU rail's output, whichever buck drives it +}; + +struct FaultDef { + uint32_t id; + const char *name; // ini key and --fault value + const char *kind; // "chip" or "rail" + const char *what; // one line, for --fault list and the config window +}; + +inline const FaultDef kFaults[] = { + {FAULT_MAX77621_CPU, "max77621_cpu", "chip", + "Erista CPU buck (I2C5 0x1B): NAKs; vdd_cpu dead with it"}, + {FAULT_MAX77621_GPU, "max77621_gpu", "chip", + "Erista GPU buck (I2C5 0x1C): NAKs"}, + {FAULT_MAX77812, "max77812", "chip", + "Mariko CPU/GPU/DRAM buck (I2C5 0x33): NAKs; vdd_cpu dead with it"}, + {FAULT_BQ24193, "bq24193", "chip", "charger (I2C1 0x6B): NAKs"}, + {FAULT_MAX17050, "max17050", "chip", "fuel gauge (I2C1 0x36): NAKs"}, + {FAULT_TMP451, "tmp451", "chip", "thermal sensor (I2C1 0x4C): NAKs"}, + {FAULT_BM92T36, "bm92t36", "chip", "USB-PD (I2C1 0x18): NAKs"}, + {FAULT_VDD_CPU, "vdd_cpu", "rail", + "CPU rail output dead, buck still answers: CPU RAM repair never " + "completes, CPU0 cannot run"}, +}; + +inline const FaultDef *fault_find(const char *name) { + if (!name) return nullptr; + for (const FaultDef &f : kFaults) + if (!strcmp(name, f.name)) return &f; + return nullptr; +} + +inline const char *fault_name(uint32_t id) { + for (const FaultDef &f : kFaults) + if (f.id == id) return f.name; + return "?"; +} + +// Fault value in the ini: dead/1 sets it, ok/0 clears it. Anything else is +// refused so a typo cannot silently mean "healthy". +inline bool fault_value_parse(const char *s, bool *dead) { + if (!s) return false; + if (!strcmp(s, "dead") || !strcmp(s, "1")) { *dead = true; return true; } + if (!strcmp(s, "ok") || !strcmp(s, "0")) { *dead = false; return true; } + return false; +} + // Tegra DC window registers (TRM 24.10-24.11) as slots in a window's // register file: the indirect window page 0x700-0x7FF is slots 0x000-0x0FF, // the WINBUF page 0x800-0x83F slots 0x100-0x13F. @@ -368,6 +450,9 @@ struct EmuState { // root port 1). See WifiRadioMode above for what each mode models. std::atomic wifi_radio{WIFI_RADIO_HEALTHY}; + // Dead chips and rails, a FaultId bitmask. See FaultId above. + std::atomic faults{0}; + // SoC thermal sensor (TMP451, I2C5 @ 0x4C). std::atomic soc_temp_c10{420}; // remote channel (SoC die): °C * 10 std::atomic pcb_temp_c10{350}; // local channel (PCB): °C * 10 diff --git a/main.cpp b/main.cpp index 072c8d1..9c62eb8 100644 --- a/main.cpp +++ b/main.cpp @@ -268,6 +268,16 @@ int main(int argc, char *argv[]) { if (strcmp(argv[i], "--trace") == 0) emu_trace_enabled = true; + // "--fault list" names the faults and exits. Scanned here, like --trace, + // so it works without a payload: it is how you find the names to use. + for (int i = 1; i + 1 < argc; i++) + if (strcmp(argv[i], "--fault") == 0 && strcmp(argv[i + 1], "list") == 0) { + printf("Faults (--fault NAME, or NAME=dead under [faults] in rcm_emu.ini):\n"); + for (const FaultDef &f : kFaults) + printf(" %-13s %-4s %s\n", f.name, f.kind, f.what); + return 0; + } + // Started with no payload - usually a double-click, where a usage line // on stderr goes to a console nobody is looking at. Offer the picker // instead: drop a file on its window, or press O for the system dialog. @@ -277,7 +287,7 @@ int main(int argc, char *argv[]) { if (argc < 2) { picked = payload_picker_run(); if (picked.empty()) { - fprintf(stderr, "Usage: %s [--sd ] [--boot0 ] [--rawnand ] [--prod-keys ] [--oem erista|mariko] [--bt-radio healthy|faulty|absent] [--wifi-radio healthy|faulty|absent]\n", argv[0]); + fprintf(stderr, "Usage: %s [--sd ] [--boot0 ] [--rawnand ] [--prod-keys ] [--oem erista|mariko] [--bt-radio healthy|faulty|absent] [--wifi-radio healthy|faulty|absent] [--fault NAME[,NAME]|list]\n", argv[0]); fprintf(stderr, "\nControls:\n"); fprintf(stderr, " Arrow Up/Down = VOL+/VOL- buttons\n"); fprintf(stderr, " Enter = POWER button\n"); @@ -380,6 +390,25 @@ int main(int argc, char *argv[]) { state.usb_nbd_port = 0; printf("[emu] USB host connected [overrides ini]\n"); } + } else if (strcmp(argv[i], "--fault") == 0 && i + 1 < argc) { + // Kill a chip or a rail. Repeatable, and comma lists work too. + // Adds to whatever the ini set: a fault is a one-shot override + // the same way --oem is, so the same payload+ini can be re-run + // against broken hardware. ("--fault list" is handled at the top + // of main, before a payload is needed.) + const char *arg = argv[++i]; + char buf[256]; + snprintf(buf, sizeof(buf), "%s", arg); + for (char *tok = strtok(buf, ","); tok; tok = strtok(nullptr, ",")) { + if (const FaultDef *f = fault_find(tok)) { + state.faults |= f->id; + } else { + fprintf(stderr, "[emu] Unknown --fault '%s'; expected one of:", tok); + for (const FaultDef &d : kFaults) + fprintf(stderr, " %s", d.name); + fprintf(stderr, " (or 'list')\n"); + } + } } else if (strcmp(argv[i], "--wifi-radio") == 0 && i + 1 < argc) { // WLAN half of the same package, on PCIe. 'faulty' means // something different here from the Bluetooth side: the link @@ -398,6 +427,12 @@ int main(int argc, char *argv[]) { } } + // Say which hardware is broken before anything runs, so a capture can + // never be mistaken for one from a healthy console. + for (const FaultDef &f : kFaults) + if (state.faults.load() & f.id) + printf("[fault] %s (%s): %s\n", f.name, f.kind, f.what); + if (prod_keys_path) { extern int se_engine_load_prod_keys(const char *); se_engine_load_prod_keys(prod_keys_path); diff --git a/t210/ccplex.cpp b/t210/ccplex.cpp index a4fea19..98c440a 100644 --- a/t210/ccplex.cpp +++ b/t210/ccplex.cpp @@ -177,10 +177,20 @@ const uint8_t *host_ptr(uint64_t a, uint32_t len) { // Everything the core needs to actually execute once released. Returns the // first thing missing, or nullptr. const char *boot_blocked(EmuState *state) { - if (!cpu_rail_on(state)) + if (!cpu_rail_on(state)) { + // A fault first: the registers may well say "on", and the reason has to + // name what is actually wrong. + if (const char *f = cpu_rail_fault(state)) { + if (!strcmp(f, "vdd_cpu")) + return "CPU rail has no output (fault: vdd_cpu, buck answers but does not deliver)"; + if (!strcmp(f, "max77812")) + return "CPU rail has no output (fault: max77812 dead)"; + return "CPU rail has no output (fault: max77621_cpu dead)"; + } return state->pmic_otp.load() == 0x53 ? "CPU rail off (MAX77812 EN_CTRL.EN_M4 clear)" : "CPU rail off (MAX77621 VOUT_EN / MAX77620 GPIO5 EN pin)"; + } if (!pmc_partition_on(POWER_RAIL_CRAIL)) return "CRAIL partition powergated"; if (!pmc_partition_on(POWER_RAIL_C0NC)) return "C0NC partition powergated"; if (!pmc_partition_on(POWER_RAIL_CE0)) return "CE0 partition powergated"; @@ -546,7 +556,8 @@ void ccplex_partition_changed(EmuState *state, int part, bool on) { void ccplex_rail_changed(EmuState *state) { if (m.cpu0 != Cpu0::Off && !cpu_rail_on(state)) - stop_cpu0("CPU rail switched off"); + stop_cpu0(cpu_rail_fault(state) ? "CPU rail lost its output (fault injected)" + : "CPU rail switched off"); else maybe_release(state); } diff --git a/t210/mmio.cpp b/t210/mmio.cpp index 72c6d02..9347414 100644 --- a/t210/mmio.cpp +++ b/t210/mmio.cpp @@ -705,7 +705,27 @@ static void max77812_regs_init() { static void max77620_regs_init(EmuState *state); static uint8_t max77620_regs[256]; +// The fault, if any, that has taken the CPU rail's output away: the rail +// itself, or the buck this SoC generation drives it from. A dead chip takes +// its rails with it - that is what makes a chip fault coherent rather than +// just a NAK: on Erista the MAX77621 register file still reads as enabled +// (its seed has VOUT_EN set), so without this the rail would stay "up" behind +// a chip that no longer exists. +const char *cpu_rail_fault(EmuState *state) { + uint32_t f = state->faults.load(); + if (f & FAULT_VDD_CPU) + return "vdd_cpu"; + bool mariko = state->pmic_otp.load() == 0x53; + if (mariko && (f & FAULT_MAX77812)) + return "max77812"; + if (!mariko && (f & FAULT_MAX77621_CPU)) + return "max77621_cpu"; + return nullptr; +} + bool cpu_rail_on(EmuState *state) { + if (cpu_rail_fault(state)) + return false; if (state->pmic_otp.load() == 0x53) { max77812_regs_init(); return (max77812_regs[0x06] >> 6) & 1; // EN_CTRL.EN_M4 (CPU) @@ -721,6 +741,34 @@ bool cpu_rail_on(EmuState *state) { return en_pin && (max77621_regs[0][0] & 0x80); } +// A fault was set or cleared while running (config window). Everything that +// depends on one re-reads it on its next access; only CPU0 has to be told, +// because silicon stops a core the moment its rail goes and starts a +// refused release the moment the last precondition arrives. +void faults_changed(EmuState *state) { + ccplex_rail_changed(state); +} + +// Which fault, if any, kills the chip at this bus and address. Kept beside +// i2c_slave_present() so the two lists cannot drift apart. +static uint32_t fault_for_slave(bool on_i2c5, uint8_t addr) { + if (on_i2c5) { + switch (addr) { + case 0x1B: return FAULT_MAX77621_CPU; + case 0x1C: return FAULT_MAX77621_GPU; + case 0x33: return FAULT_MAX77812; + default: return 0; + } + } + switch (addr) { + case 0x18: return FAULT_BM92T36; + case 0x36: return FAULT_MAX17050; + case 0x4C: return FAULT_TMP451; + case 0x6B: return FAULT_BQ24193; + default: return 0; + } +} + // Which slaves actually answer, per bus. This is the emulator's answer to // "is that chip fitted?", so it must list exactly what is modelled below and // nothing else - a bus scan is a real diagnostic and an emulator that ACKs @@ -733,7 +781,7 @@ bool cpu_rail_on(EmuState *state) { // the address byte (TRM 35.x). bdk tests the whole nibble. #define I2C_STATUS_NOACK (1u << 0) -static bool i2c_slave_present(EmuState *state, bool on_i2c5, uint8_t addr) { +static bool i2c_slave_fitted(EmuState *state, bool on_i2c5, uint8_t addr) { if (on_i2c5) { bool mariko = state && state->pmic_otp.load() == 0x53; switch (addr) { @@ -762,6 +810,26 @@ static bool i2c_slave_present(EmuState *state, bool on_i2c5, uint8_t addr) { return false; } } + +// Fitted, and not killed by a fault. A fault only means something for a chip +// this board has: a dead MAX77621 on a Mariko is a chip that was never there, +// and must not be reported as one that died. +static bool i2c_slave_present(EmuState *state, bool on_i2c5, uint8_t addr) { + if (!i2c_slave_fitted(state, on_i2c5, addr)) + return false; + uint32_t dead = state ? state->faults.load() & fault_for_slave(on_i2c5, addr) : 0; + if (dead) { + static uint32_t logged = 0; + if (!(logged & dead)) { + logged |= dead; + printf("[fault] I2C%d 0x%02X NAK: %s is dead\n", on_i2c5 ? 5 : 1, + addr, fault_name(dead)); + fflush(stdout); + } + return false; + } + return true; +} // I2C_CNFG has to read back what was written: bdk sets NORMAL_MODE_GO with a // read-modify-write (`cnfg = (cnfg & ~GO) | GO`), so returning 0 would drop the // transfer size and direction bits before the transaction runs. [0]=I2C1, [1]=I2C5. @@ -1388,7 +1456,8 @@ static uint32_t i2c_device_reg_read(EmuState *state, bool on_i2c5, uint8_t slave // effect on the modelled chips as a normal-mode [reg, value] write. static void i2c_packet_reg_write(EmuState *state, bool on_i2c5, uint8_t dev, uint8_t reg, uint8_t v) { - if (!on_i2c5) + // Nothing to write to on I2C1, and a NAKed write lands nowhere. + if (!on_i2c5 || !i2c_slave_present(state, on_i2c5, dev)) return; bool mariko = state->pmic_otp.load() == 0x53; if (dev == MAX77620_I2C_ADDR) { @@ -1440,7 +1509,9 @@ void i2c_write(EmuState *state, uint64_t addr, uint32_t val) { } // CPU/GPU rail bucks. Same wire format; only the chip this SoC // generation is fitted with takes the write (the other one NAKs). - if ((val & (1u << 9)) && is_write && size >= 2 && on_i2c5) { + // A NAKed write lands nowhere: a dead buck must not switch its rail. + if ((val & (1u << 9)) && is_write && size >= 2 && on_i2c5 && + i2c_slave_present(state, on_i2c5, i2c_slave_addr)) { uint8_t reg = (uint8_t)(i2c_cmd_data1 & 0xFF); uint8_t v = (uint8_t)((i2c_cmd_data1 >> 8) & 0xFF); bool mariko = state->pmic_otp.load() == 0x53; @@ -3160,10 +3231,14 @@ static bool tmr_fire_next(EmuState *state, uint64_t *when) { } static uint32_t flow_read(EmuState *state, uint64_t addr) { - (void)state; uint32_t offset = (uint32_t)(addr - 0x60007000); - if (offset == 0x40) + if (offset == 0x40) { + // A request left pending for want of a rail completes once the rail is + // up (modelled as immediate: the repair itself takes microseconds). + if ((flow_ram_repair & RAM_REPAIR_REQ) && cpu_rail_on(state)) + flow_ram_repair = (flow_ram_repair & ~RAM_REPAIR_REQ) | RAM_REPAIR_STS; return flow_ram_repair; + } return mmio_regs.get(addr); } @@ -3172,8 +3247,24 @@ static void flow_write(EmuState *state, uint64_t addr, uint32_t val) { if (offset == 0x40) { flow_ram_repair = (val & ~(RAM_REPAIR_REQ | RAM_REPAIR_STS)) | (flow_ram_repair & RAM_REPAIR_STS); - if (val & RAM_REPAIR_REQ) - flow_ram_repair |= RAM_REPAIR_STS; + if (val & RAM_REPAIR_REQ) { + // STS reports the acks of every segment of the CPU repair chain, which + // software is to request only after powering the CPU rail, and REQ + // stays high until they arrive (TRM 17.1.3.2). No rail, no acks: the + // request is left pending with STS clear - and bdk's ccplex_boot_cpu0() + // spins on STS with no timeout, which is exactly where a console with + // a dead CPU rail hangs. + if (cpu_rail_on(state)) { + flow_ram_repair |= RAM_REPAIR_STS; + } else { + flow_ram_repair = (flow_ram_repair & ~RAM_REPAIR_STS) | RAM_REPAIR_REQ; + const char *f = cpu_rail_fault(state); + printf("[ccplex] RAM repair requested with no CPU rail (%s%s): " + "STS stays clear until it comes up\n", + f ? "fault: " : "rail off", f ? f : ""); + fflush(stdout); + } + } return; } if (offset == 0x04) { diff --git a/t210/mmio.h b/t210/mmio.h index 44e2cbc..062c330 100644 --- a/t210/mmio.h +++ b/t210/mmio.h @@ -57,9 +57,17 @@ bool pmc_partition_on(int part); void pmc_secure_scratch_write(unsigned n, uint32_t value); // True when the CCPLEX CPU rail is up: the MAX77621 on Erista (enabled, and -// its EN pin driven by MAX77620 GPIO5), the MAX77812 M4 phase on Mariko. +// its EN pin driven by MAX77620 GPIO5), the MAX77812 M4 phase on Mariko - +// and no fault has taken its output away. bool cpu_rail_on(EmuState *state); +// The fault name ("vdd_cpu", or the dead buck's) that keeps the CPU rail +// down, or nullptr. A dead chip takes its rails with it. +const char *cpu_rail_fault(EmuState *state); + +// A fault was set or cleared while running: tell the models that cache it. +void faults_changed(EmuState *state); + // Individual peripheral handlers. uint32_t timer_read(EmuState *state, uint64_t addr); void timer_write(EmuState *state, uint64_t addr, uint32_t val); diff --git a/tests/ccplex/payload.c b/tests/ccplex/payload.c index 42ec1a9..f011d2d 100644 --- a/tests/ccplex/payload.c +++ b/tests/ccplex/payload.c @@ -124,6 +124,11 @@ void _start(void) { : "unpowered: CPU0 stayed dark\n"); hold_cpu0(); + /* A write to the MAX77812, which the Erista this runs as does not have: + * it must land nowhere, and under --fault max77812 must not be reported + * as a chip that died. */ + i2c5_write(0x33, 0x06, 0x00); /* MAX77812 EN_CTRL */ + /* 2. The real bring-up, in ccplex_boot_cpu0() order. */ i2c5_write(0x3C, 0x40, 0x1C); /* MAX77620 AME_GPIO: GPIO5 plain */ i2c5_write(0x3C, 0x3B, 0x09); /* GPIO5: push-pull, out high */ @@ -138,8 +143,17 @@ void _start(void) { pwrgate_on(15); /* C0NC */ pwrgate_on(14); /* CE0 */ REG(FLOW + 0x40) = 1; /* RAM_REPAIR_REQ */ - while (!(REG(FLOW + 0x40) & 2)) - ; + /* bdk spins on STS forever. Bounded here so a run with a dead CPU rail + * (the --fault scenarios in run.sh) says where silicon would have hung + * and still reaches its power-off. 10 ms is thousands of times what a + * powered repair takes. */ + { + u32 t = REG(TIMERUS); + while (!(REG(FLOW + 0x40) & 2) && REG(TIMERUS) - t < 10000) + ; + puts_((REG(FLOW + 0x40) & 2) ? "ram repair: done\n" + : "ram repair: never completed\n"); + } REG(CAR + 0x434) = 1u << 3; /* RST_DEV_V_CLR: MSELECT */ /* Released with its clock stopped, CPU0 must not run until the stop is diff --git a/tests/ccplex/run.sh b/tests/ccplex/run.sh index 0d21d27..50fa54e 100755 --- a/tests/ccplex/run.sh +++ b/tests/ccplex/run.sh @@ -21,6 +21,7 @@ expect() { echo "ccplex test (emulator exit $status):" expect 'cannot run: CPU rail off' 'an unpowered release is refused, with the reason' +expect 'ram repair: done' 'a powered RAM repair completes' expect 'unpowered: CPU0 stayed dark' 'and the core really did not execute' expect 'CPU0 released: AArch64 EL3, MMU off, entry 0xA0000000' 'a powered release boots CPU0 at the SB vector' expect 'clock stopped: CPU0 held still' 'a core released with its clock stopped does not run' @@ -56,4 +57,66 @@ else fail=1 fi + +# ---- fault injection ------------------------------------------------------ +# +# The same payload against broken hardware. Pinned to Erista, which is what +# the payload's bring-up drives (MAX77621 + MAX77620 GPIO5), so the result +# does not depend on whatever rcm_emu.ini happens to say. +expect_not() { + if grep -q -- "$1" "$FLOG"; then + echo " FAIL $2 (present: $1)" + fail=1 + else + echo " ok $2" + fi +} +fexpect() { + if grep -q -- "$1" "$FLOG"; then + echo " ok $2" + else + echo " FAIL $2 (missing: $1)" + fail=1 + fi +} +run_fault() { + FLOG=tests/ccplex/out_fault_$1.log + SDL_VIDEODRIVER=dummy RCM_EMU_NO_AUDIO=1 timeout 120 "$EMU" "$PAYLOAD" \ + --oem erista --fault "$1" > "$FLOG" 2>&1 + fstatus=$? + echo "ccplex test, --fault $1 (emulator exit $fstatus):" + if [ "$fstatus" -ne 0 ]; then + echo " FAIL the emulator exited with status $fstatus (124 = timed out)" + fail=1 + fi +} + +# A rail whose buck answers and takes its enable, but delivers nothing. Over +# I2C the console looks healthy; only what the rail feeds notices. +run_fault vdd_cpu +fexpect '\[fault\] vdd_cpu (rail)' 'the fault is announced before the run' +fexpect 'RAM repair requested with no CPU rail (fault: vdd_cpu)' 'the RAM repair is held, and says why' +fexpect 'ram repair: never completed' 'so STS never sets (bdk would spin here forever)' +fexpect 'cannot run: CPU rail has no output (fault: vdd_cpu' 'the release is refused, naming the fault' +expect_not 'CPU0 released: AArch64' 'and the core never executes' +fexpect 'powered: magic=00000000' 'the payload sees no mailbox write' +expect_not 'I2C5 0x1B NAK' 'the buck itself still answers on I2C' +fexpect 'MAX77620 PWR_OFF received' 'the payload still runs to its power-off' + +# A dead buck: NAKs, takes no writes, and the rail goes with it - even though +# its register file was seeded with VOUT_EN set. +run_fault max77621_cpu +fexpect 'I2C5 0x1B NAK: max77621_cpu is dead' 'the dead chip NAKs its address' +fexpect 'ram repair: never completed' 'its rail is dead with it' +fexpect 'cannot run: CPU rail has no output (fault: max77621_cpu dead)' 'the release is refused, naming the chip' +expect_not 'CPU0 released: AArch64' 'and the core never executes' +fexpect 'MAX77620 PWR_OFF received' 'the main PMIC, not faulted, still powers off' + +# A fault on the other generation's chip must change nothing here: Erista +# has no MAX77812 to break. +run_fault max77812 +fexpect 'ram repair: done' 'an Erista ignores a dead MAX77812' +fexpect 'CPU0 released: AArch64 EL3, MMU off, entry 0xA0000000' 'and CPU0 boots as normal' +expect_not 'NAK: max77812 is dead' 'a chip this board does not have is not reported dead' + [ $fail -eq 0 ] && echo "ccplex test: PASS" || { echo "ccplex test: FAIL - see $LOG"; exit 1; }