Run CCPLEX CPU0 so hwtest's PCIe probe completes, and speed up the core

hwtest-rcm's Wi-Fi probe hands PCIe to an AArch64 stub on CPU0, because
PCIe and MSELECT answer a CPU-complex master only. The emulator had no
CCPLEX, so the stub never reported; and the sweep never got that far
anyway, because it hung in the fan test.

CPU0 (t210/ccplex.cpp)
- A second Unicorn engine, AArch64, Cortex-A57, sharing IRAM, DRAM, TZRAM
  and every peripheral model with the BPMP. It starts when
  RST_CPUG_CMPLX_CLR releases CPU0 with every precondition of bdk's
  ccplex_boot_cpu0() met: CPU rail (MAX77621 + MAX77620 GPIO5 on Erista,
  MAX77812 EN_M4 on Mariko), CRAIL/C0NC/CE0, CPUG clock, PLLX, MSELECT,
  RAM repair, an AArch64 vector in SB_AA64_RESET_LOW/HIGH. A release that
  misses one is refused and the reason printed.
- Entered at EL3, MMU off, like silicon. Unicorn builds its ARM64 core at
  EL1 and neither a PSTATE write (stale hflags) nor an exception (never
  delivered) moves it, so a one-instruction ERET trampoline does.
- Its own clock (1 ns/instruction, 250 ns/bus access), run in 250 us
  lockstep slices behind the BPMP; WFE/WFI park it to the slice end.
- PCIe is master-aware: the BPMP still reads all ones, CPU0 is served.
  MSELECT, AFI_PCIE_CONFIG, LNKSTA, the endpoint class/BAR, the PCIE
  partition, PEX_L1 pinmux and PMC_GLB_AMAP_CFG take their measured values.

hwtest now runs its whole sweep and reports "WLAN core enumerated on the
PCIe bus" / "Wireless : pass", matching the healthy-console capture in
its README.

Performance
- No per-instruction hooks. Three UC_HOOK_CODE callbacks (one registered
  twice, one doing a uc_mem_read per instruction) are replaced by a block
  hook that counts each block's instructions once (cached), credited when
  it has run - the same totals, so the clock is unchanged.
- Peripherals are uc_mmio_map() windows instead of memory hooks, which
  had forced every guest load/store onto Unicorn's slow path; the register
  cache is a paged array instead of a std::map.
- The display reads the framebuffer straight from host DRAM, reuses its
  buffers, skips unchanged frames, and no renderer uses PRESENTVSYNC (a
  vsync'd present could stall the emulated CPU for a whole frame).
Reaching hwtest's fan test: 2.7 s -> 0.8 s; whole sweep ~3 s.

Reliability
- TIMERUS poll pacing: bdk's fan_get_speed() waits for TIMERUS to EQUAL a
  deadline (int - u32 is unsigned, so `> 0` is `!= 0`), and the dilated
  clock stepped past it, so the loop spun until TIMERUS wrapped. Polls at
  most 200 instructions apart now advance the clock by at most 1 us.
- MAX77621/MAX77812 are register files: bdk's hw_init() disabling the
  Erista CPU/GPU bucks used to be ignored and read back as enabled.
- Mariko reads FUSE_RESERVED_ODM28_B01 bit 0 set, so bdk addresses the
  retail MAX77812 at 0x33 instead of the NAK'd 0x31.
- Windows: disk images are opened O_BINARY (MinGW defaults to text mode,
  corrupting sectors) and rawnand chunks are sized with _fstati64.
- Soft reboot resets CPU0 and the PCIe root complex; the payload is
  loaded once, size-checked; an empty/unreadable payload is rejected; the
  NOP-slide check no longer trips on a counter that never reset; the M/C
  windows fall back to a software renderer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
This commit is contained in:
Claude committed 2026-09-25 15:55:48 +00:00
1 parent 9f6e96001c
commit f19a52b779
15 files changed
+1816 -672

No files matched your search

+32 -8
View File
@@ -5,6 +5,7 @@
#include "../emu_state.h"
#include "memory_map.h"
#include "mmio.h"
// ==========================================================================
// Register map (tegra210.dtsi pcie@1003000, U-Boot drivers/pci/pci_tegra.c)
@@ -108,6 +109,14 @@
static constexpr uint64_t T_VDD_TO_POR_US = 57000; // WL_REG_ON -> POR done
static constexpr uint64_t T_REFCLK_STABLE_US = 10000; // refclk -> PERST# high
// Reset values, as a CPU-complex master reads them on real silicon.
// MSELECT_CONFIG_0 is the TRM's documented reset value (16.3.1): the PCIe
// aperture (bit 5) is already enabled. AFI_PCIE_CONFIG is what hwtest's CPU0
// stub reads straight after the BPMP has cycled the AFI reset - xbar X4_X1 in
// bits 23:20 and root port 1 disabled (bit 2) until software clears it.
static constexpr uint32_t MSELECT_CONFIG_RESET = 0x07FF4020;
static constexpr uint32_t AFI_PCIE_CONFIG_RESET = 0x00103025;
// Broadcom identity (brcm_hw_ids.h, brcmfmac/pcie.c).
static constexpr uint32_t BRCM_4356_CFG0 = 0x43EC14E4;
static constexpr uint32_t BRCM_BAR0_WINDOW = 0x80;
@@ -158,7 +167,8 @@ struct PcieModel {
bool mgmt_warned = false;
bool plle = false;
bool pllrefe = false;
bool powergated = true;
// The PCIE partition is already up at RCM entry (see pmc_pwrgate_status).
bool powergated = false;
// XUSB pad controller.
uint32_t padctl[0x1000 / 4] = {};
@@ -177,9 +187,11 @@ PcieModel pcie;
// BAR sizing masks: a write of all-ones reads back ~(size-1) with the type
// bits preserved, which is how any enumerator discovers how big a BAR is.
// BAR0 is 32 KiB of registers, BAR2 the 2 MiB TCM; both are 64-bit
// prefetchable, which is why brcmfmac calls them resource 0 and resource 2.
constexpr uint32_t EP_BAR_TYPE = 0x0000000C; // 64-bit, prefetchable
// BAR0 is 32 KiB of registers, BAR2 the 2 MiB TCM; both are 64-bit memory
// BARs, which is why brcmfmac calls them resource 0 and resource 2. They are
// NOT prefetchable: a real CYW4356 in ROM phase reads BAR0 = 0x00000004
// (hwtest's healthy-console capture), i.e. type 64-bit, prefetch clear.
constexpr uint32_t EP_BAR_TYPE = 0x00000004; // 64-bit, non-prefetchable
constexpr uint32_t EP_BAR0_MASK = ~(0x8000u - 1);
constexpr uint32_t EP_BAR2_MASK = ~(0x200000u - 1);
@@ -187,7 +199,7 @@ void ep_init() {
memset(pcie.ep, 0, sizeof(pcie.ep));
pcie.ep[0x00 / 4] = BRCM_4356_CFG0; // 14E4:43EC
pcie.ep[0x04 / 4] = 0x00100000; // status: capability list
pcie.ep[0x08 / 4] = 0x02800000; // network controller, other
pcie.ep[0x08 / 4] = 0x02800003; // network controller, rev 03
pcie.ep[0x0C / 4] = 0x00000000; // header type 0
pcie.ep[0x10 / 4] = EP_BAR_TYPE; // BAR0 lo: 32 KiB registers
pcie.ep[0x18 / 4] = EP_BAR_TYPE; // BAR2 lo: 2 MiB TCM
@@ -227,6 +239,9 @@ bool radio_core_alive(EmuState *state) {
// which is how a probe that cannot work on hardware passed here for days.
// Refusing them is the single most valuable thing this file does.
static bool bpmp_pcie_denied(uint64_t addr) {
// CPU0 (t210/ccplex.cpp) is a CPU-complex master and reaches all of it.
if (g_bus_master == BUS_CPU0)
return false;
static bool warned = false;
if (!warned) {
warned = true;
@@ -249,7 +264,8 @@ void mselect_complain(uint64_t addr) {
pcie.mselect_warned = true;
printf("[pcie] *** access to %08X with MSELECT still in reset ***\n",
(unsigned)addr);
printf("[pcie] *** on real silicon this STALLS THE BPMP for good: no ***\n");
printf("[pcie] *** on real silicon this STALLS THE %s for good: no ***\n",
g_bus_master == BUS_CPU0 ? "CPU0" : "BPMP");
printf("[pcie] *** abort, no timeout, no reboot path. Clear ***\n");
printf("[pcie] *** RST_DEV_V bit %d first (bdk ccplex.c:121). ***\n",
CLK_V_MSELECT);
@@ -345,9 +361,13 @@ bool ep_reachable(EmuState *state) {
void pcie_reset(EmuState *state) {
pcie = PcieModel();
ep_init();
pcie.mselect[0] = MSELECT_CONFIG_RESET;
pcie.afi[AFI_PCIE_CONFIG / 4] = AFI_PCIE_CONFIG_RESET;
(void)state;
}
bool pcie_mselect_up() { return mselect_up(); }
void pcie_set_powergate(bool ungated) {
if (pcie.powergated == !ungated)
return;
@@ -469,7 +489,8 @@ uint32_t padctl_read(EmuState *state, uint64_t addr) {
pcie.padctl_warned = true;
printf("[pcie] *** read of XUSB_PADCTL %08X with the block still ***\n",
(unsigned)addr);
printf("[pcie] *** in reset. On real silicon this STALLS THE ***\n");
printf("[pcie] *** in reset. On real silicon this STALLS THE %s ***\n",
g_bus_master == BUS_CPU0 ? "CPU0" : "BPMP");
printf("[pcie] *** BPMP for good. Clear RST_DEV_W bit %d first. ***\n",
CLK_W_XUSB_PADCTL);
fflush(stdout);
@@ -637,8 +658,11 @@ uint32_t pcie_read(EmuState *state, uint64_t addr) {
// speeds"), so speed = 1 and width = 1.
if (!rp.link)
return rp.reg[off / 4] & 0xFFFF;
// LNKSTA 0x3011 on hardware: gen1, x1, Slot Clock
// Configuration (bit 28 - the endpoint runs off the refclk the
// root port drives) and Data Link Layer Link Active.
return (rp.reg[off / 4] & 0xFFFF) | RP_LINK_DL_ACTIVE |
(1u << 16) | (1u << 20);
(1u << 28) | (1u << 16) | (1u << 20);
default:
return rp.reg[off / 4];
}