mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
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
510 lines
18 KiB
C++
510 lines
18 KiB
C++
#include "ccplex.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
#include <unicorn/unicorn.h>
|
|
|
|
#include "../emu_state.h"
|
|
#include "memory_map.h"
|
|
#include "mmio.h"
|
|
#include "pcie.h"
|
|
|
|
// ==========================================================================
|
|
// Registers (bdk soc/clock.h, soc/t210.h, soc/pmc.h; Tegra X1 TRM ch.5/16)
|
|
// ==========================================================================
|
|
|
|
#define CAR_CCLK_BURST_POLICY 0x020
|
|
#define CCLK_RUN_SRC(v) (((v) >> 4) & 0xF)
|
|
#define CCLK_SRC_PLLX_OUT0_LJ 8
|
|
#define CCLK_SRC_PLLX_OUT0 14
|
|
#define CAR_PLLX_BASE 0x0E0
|
|
#define PLL_BASE_ENABLE (1u << 30)
|
|
#define CAR_CLK_OUT_ENB_V 0x360
|
|
#define CAR_CLK_ENB_V_SET 0x440
|
|
#define CAR_CLK_ENB_V_CLR 0x444
|
|
#define CLK_V_CPUG 0
|
|
#define CLK_V_MSELECT 3
|
|
#define CAR_RST_CPUG_CMPLX_SET 0x450
|
|
#define CAR_RST_CPUG_CMPLX_CLR 0x454
|
|
// The bits ccplex_boot_cpu0() clears for CPU0 - CPURESET0, DBGRESET0,
|
|
// CORERESET0 and bit 30 - and NONCPURESET, which it clears first. The core
|
|
// runs once all five are clear.
|
|
#define RST_CPU0_MASK ((1u << 0) | (1u << 16) | (1u << 24) | (1u << 30))
|
|
#define RST_NONCPU (1u << 29)
|
|
|
|
// SB block, SYSREG + 0x200.
|
|
#define SB_CSR 0x00
|
|
#define SB_AA64_RESET_LOW 0x30
|
|
#define SB_AA64_RESET_HIGH 0x34
|
|
#define SB_AA64_RST_AARCH64_MODE_EN (1u << 0)
|
|
|
|
// PMC partitions carrying CPU0.
|
|
#define POWER_RAIL_CRAIL 0
|
|
#define POWER_RAIL_CE0 14
|
|
#define POWER_RAIL_C0NC 15
|
|
|
|
// ==========================================================================
|
|
// Timing
|
|
// ==========================================================================
|
|
//
|
|
// PLLX runs the A57s at ~1 GHz. Retiring one instruction per nanosecond is
|
|
// the right order of magnitude, and it is ten times the rate the BPMP clock
|
|
// model uses, which is the other thing that has to hold: CPU0 is the fast
|
|
// core.
|
|
//
|
|
// A bus access is not free. From the A57, a register read goes out through
|
|
// MSELECT and the APC bridge onto a 100-odd MHz APB, and costs a few hundred
|
|
// nanoseconds. Charging it keeps a udelay() loop - which is nothing but
|
|
// TIMERUS reads - at a realistic few million accesses per emulated second
|
|
// instead of a billion instructions of busy-wait.
|
|
static constexpr uint64_t NS_PER_INSN = 1;
|
|
static constexpr uint64_t NS_PER_BUS_ACCESS = 250;
|
|
|
|
// ==========================================================================
|
|
// Reset entry
|
|
// ==========================================================================
|
|
//
|
|
// A real A57 comes out of reset at EL3 in AArch64 and fetches from the
|
|
// SB_AA64_RESET vector. Unicorn creates its ARM64 core at EL1 instead (a
|
|
// backward-compatibility choice in its cpu init), and it cannot be moved:
|
|
// writing PSTATE updates the mode bits but not the translator's cached
|
|
// hflags, so the first `msr scr_el3` still decodes as EL1 and is UNDEFINED.
|
|
// Nor can an exception carry it up, because Unicorn never delivers guest
|
|
// exceptions - SMC and friends end the run.
|
|
//
|
|
// ERET can. Its helper takes the current EL from PSTATE - which the register
|
|
// write did update - reads SPSR_EL3/ELR, and rebuilds hflags for the target
|
|
// EL. So the core starts on a one-instruction trampoline in a page only CPU0
|
|
// can see, with PSTATE, SPSR_EL3 = EL3h/DAIF masked and ELR = the vector.
|
|
// The translator fetches the new PC's address from ELR at the EL it thinks
|
|
// it is at (EL1), so ELR_EL1 carries it too. One ERET later the core is at
|
|
// EL3h with interrupts masked and the MMU off: architectural reset state.
|
|
//
|
|
// The page sits at 1 TiB: inside the A57's 44-bit physical space, far
|
|
// outside the Tegra's, so it can never alias anything a payload can reach.
|
|
static constexpr uint64_t RESET_TRAMPOLINE = 0x10000000000ULL;
|
|
static constexpr uint32_t A64_ERET = 0xD69F03E0;
|
|
static constexpr uint32_t A64_WFE = 0xD503205F;
|
|
static constexpr uint32_t A64_WFI = 0xD503207F;
|
|
static constexpr uint64_t PSTATE_EL3H_DAIF = 0x3CD;
|
|
|
|
namespace {
|
|
|
|
enum class Cpu0 { Off, Running, Hung };
|
|
|
|
struct Model {
|
|
uint32_t rst_cpug = 0xFFFFFFFFu; // everything held at power-on
|
|
uint32_t clk_enb_v = 0;
|
|
uint32_t cclk_burst = 0;
|
|
uint32_t pllx_base = 0;
|
|
uint32_t sb_csr = 0;
|
|
uint32_t sb_rst_lo = 0;
|
|
uint32_t sb_rst_hi = 0;
|
|
|
|
Cpu0 cpu0 = Cpu0::Off;
|
|
uc_engine *uc = nullptr;
|
|
uint64_t pc = 0;
|
|
uint64_t ns = 0; // CPU0's clock, emulated nanoseconds
|
|
uint64_t target_ns = 0; // end of the slice being run
|
|
uint64_t pending = 0; // instructions in the block now executing
|
|
bool wfe_next = false; // the block now executing ends in WFE/WFI
|
|
bool in_run = false;
|
|
bool kill_pending = false;
|
|
bool reset_pending = false;
|
|
const char *kill_reason = nullptr;
|
|
|
|
uint64_t retired = 0;
|
|
uint64_t bus_accesses = 0;
|
|
uint32_t boots = 0;
|
|
const char *last_refusal = nullptr;
|
|
};
|
|
|
|
Model m;
|
|
EmuState *g_state = nullptr;
|
|
uc_hook h_block, h_unmapped, h_intr;
|
|
|
|
bool held() { return (m.rst_cpug & (RST_CPU0_MASK | RST_NONCPU)) != 0; }
|
|
|
|
uint64_t entry_vector() {
|
|
return ((uint64_t)m.sb_rst_hi << 32) | (m.sb_rst_lo & ~1u);
|
|
}
|
|
|
|
bool entry_mapped(uint64_t a) {
|
|
return (a >= DRAM_BASE && a + 4 <= DRAM_BASE + DRAM_WINDOW_SIZE) ||
|
|
(a >= IRAM_BASE && a + 4 <= IRAM_BASE + IRAM_SIZE);
|
|
}
|
|
|
|
const uint8_t *host_ptr(uint64_t a, uint32_t len) {
|
|
// Valid for the identity map CPU0 runs with before (MMU off) and after
|
|
// (the stub's 1 GiB identity blocks) enabling the MMU.
|
|
if (a >= DRAM_BASE && a + len <= DRAM_BASE + DRAM_WINDOW_SIZE)
|
|
return g_state->dram_low_ptr + (a - DRAM_BASE);
|
|
if (a >= IRAM_BASE && a + len <= IRAM_BASE + IRAM_SIZE)
|
|
return g_state->iram_ptr + (a - IRAM_BASE);
|
|
return nullptr;
|
|
}
|
|
|
|
// 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))
|
|
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";
|
|
if (!(m.clk_enb_v & (1u << CLK_V_CPUG))) return "CPUG clock disabled (CLK_ENB_V bit 0)";
|
|
uint32_t src = CCLK_RUN_SRC(m.cclk_burst);
|
|
if ((src == CCLK_SRC_PLLX_OUT0_LJ || src == CCLK_SRC_PLLX_OUT0) &&
|
|
!(m.pllx_base & PLL_BASE_ENABLE))
|
|
return "CCLK runs from PLLX but PLLX is disabled";
|
|
if (!(m.clk_enb_v & (1u << CLK_V_MSELECT))) return "MSELECT clock disabled";
|
|
if (!pcie_mselect_up()) return "MSELECT still in reset";
|
|
if (!flow_ram_repair_done())
|
|
return "fast-cluster RAM repair never requested (FLOW_CTLR_RAM_REPAIR)";
|
|
if (!(m.sb_rst_lo & SB_AA64_RST_AARCH64_MODE_EN))
|
|
return "SB_AA64_RESET_LOW has no AArch64 vector (AArch32 CPU boot is not modelled)";
|
|
if (!entry_mapped(entry_vector())) return "reset vector outside IRAM/DRAM";
|
|
return nullptr;
|
|
}
|
|
|
|
void destroy_engine() {
|
|
if (!m.uc)
|
|
return;
|
|
mmio_unmap_bus(m.uc);
|
|
uc_close(m.uc);
|
|
m.uc = nullptr;
|
|
}
|
|
|
|
void stop_cpu0(const char *why) {
|
|
if (m.cpu0 == Cpu0::Off)
|
|
return;
|
|
if (m.in_run) {
|
|
// The core is the one executing: it just wrote the register that stops
|
|
// it. Finish the current access, then tear down once the run returns.
|
|
m.kill_pending = true;
|
|
m.kill_reason = why;
|
|
uc_emu_stop(m.uc);
|
|
return;
|
|
}
|
|
printf("[ccplex] CPU0 stopped (%s) after %llu instructions, %llu bus "
|
|
"accesses\n", why, (unsigned long long)m.retired,
|
|
(unsigned long long)m.bus_accesses);
|
|
fflush(stdout);
|
|
destroy_engine();
|
|
m.cpu0 = Cpu0::Off;
|
|
}
|
|
|
|
// ---- CPU0 engine hooks ---------------------------------------------------
|
|
|
|
void commit_pending() {
|
|
m.ns += m.pending * NS_PER_INSN;
|
|
m.retired += m.pending;
|
|
m.pending = 0;
|
|
}
|
|
|
|
// Once per translated block, before it runs. Accounts the previous block
|
|
// (which has now fully executed), ends the slice when CPU0's clock reaches
|
|
// its target, and parks the core after a WFE/WFI. Stopping here means the
|
|
// block is NOT executed - Unicorn leaves PC on its first instruction - so
|
|
// the next slice picks it up again.
|
|
void hook_block(uc_engine *uc, uint64_t address, uint32_t size, void *) {
|
|
commit_pending();
|
|
if (m.wfe_next) {
|
|
// The block that just ran ended in WFE/WFI: the core sleeps until
|
|
// something wakes it. Nothing in this model sends it an event, so it
|
|
// sleeps to the end of the slice and resumes - a spurious wake-up, which
|
|
// the architecture allows, and which a `for(;;) wfe;` park loop turns
|
|
// straight back into sleep. Without this that loop would spin through
|
|
// hundreds of millions of instructions per emulated second.
|
|
m.wfe_next = false;
|
|
if (m.ns < m.target_ns)
|
|
m.ns = m.target_ns;
|
|
}
|
|
g_state->emu_usec = m.ns / 1000;
|
|
if (m.ns >= m.target_ns || m.kill_pending) {
|
|
uc_emu_stop(uc);
|
|
return;
|
|
}
|
|
// Every A64 instruction is four bytes.
|
|
m.pending = size >= 4 ? size / 4 : 1;
|
|
if (const uint8_t *p = host_ptr(address + size - 4, 4)) {
|
|
uint32_t last;
|
|
memcpy(&last, p, 4);
|
|
m.wfe_next = (last == A64_WFE || last == A64_WFI);
|
|
}
|
|
}
|
|
|
|
bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address, int size,
|
|
int64_t value, void *) {
|
|
(void)size;
|
|
(void)value;
|
|
uint64_t pc = 0;
|
|
uc_reg_read(uc, UC_ARM64_REG_PC, &pc);
|
|
const char *kind = type == UC_MEM_FETCH_UNMAPPED ? "fetch"
|
|
: type == UC_MEM_WRITE_UNMAPPED ? "write"
|
|
: "read";
|
|
// Nothing decodes this address, so on silicon the transaction never
|
|
// completes and the core waits on it for ever. Return false: the run ends
|
|
// and the core is marked wedged, which is what the BPMP's watchdog on the
|
|
// mailbox heartbeat is there to notice.
|
|
printf("[ccplex] CPU0 %s of unmapped 0x%llX at PC=0x%llX: nothing answers, "
|
|
"CPU0 wedged\n", kind, (unsigned long long)address,
|
|
(unsigned long long)pc);
|
|
fflush(stdout);
|
|
return false;
|
|
}
|
|
|
|
void hook_intr(uc_engine *uc, uint32_t intno, void *) {
|
|
// Unicorn does not deliver guest exceptions (see the reset-entry note),
|
|
// so a fault cannot reach the stub's own vectors. Say what happened and
|
|
// stop the core rather than resume past the faulting instruction.
|
|
uint64_t pc = 0;
|
|
uc_reg_read(uc, UC_ARM64_REG_PC, &pc);
|
|
static const char *names[] = {"?", "UDEF", "SWI", "PREFETCH_ABORT",
|
|
"DATA_ABORT", "IRQ", "FIQ", "BKPT", "EXIT",
|
|
"KERNEL_TRAP", "HVC", "HYP_TRAP", "SMC"};
|
|
printf("[ccplex] CPU0 took exception %u (%s) near PC=0x%llX; exception "
|
|
"entry is not modelled, CPU0 halted\n", intno,
|
|
intno < sizeof(names) / sizeof(names[0]) ? names[intno] : "?",
|
|
(unsigned long long)pc);
|
|
fflush(stdout);
|
|
m.kill_pending = false;
|
|
m.cpu0 = Cpu0::Hung;
|
|
uc_emu_stop(uc);
|
|
}
|
|
|
|
bool start_cpu0(EmuState *state) {
|
|
uint64_t entry = entry_vector();
|
|
uc_engine *uc = nullptr;
|
|
uc_err err = uc_open(UC_ARCH_ARM64, UC_MODE_ARM, &uc);
|
|
if (err != UC_ERR_OK) {
|
|
printf("[ccplex] cannot create the CPU0 engine: %s\n", uc_strerror(err));
|
|
return false;
|
|
}
|
|
uc_ctl_set_cpu_model(uc, UC_CPU_ARM64_A57);
|
|
|
|
// IRAM, DRAM and TZRAM are the same host memory the BPMP engine maps, so
|
|
// a stub the BPMP copies into DRAM and the mailbox both cores poll are one
|
|
// set of bytes. CPU0's engine is new on every release, so it cannot hold
|
|
// stale translations of code the BPMP has rewritten since the last boot.
|
|
err = uc_mem_map_ptr(uc, IRAM_BASE, IRAM_SIZE, UC_PROT_ALL, state->iram_ptr);
|
|
if (err == UC_ERR_OK)
|
|
err = uc_mem_map_ptr(uc, DRAM_BASE, DRAM_WINDOW_SIZE, UC_PROT_ALL,
|
|
state->dram_low_ptr);
|
|
if (err == UC_ERR_OK)
|
|
err = uc_mem_map(uc, RESET_TRAMPOLINE, 0x1000, UC_PROT_READ | UC_PROT_EXEC);
|
|
if (err != UC_ERR_OK) {
|
|
printf("[ccplex] cannot map CPU0 memory: %s\n", uc_strerror(err));
|
|
uc_close(uc);
|
|
return false;
|
|
}
|
|
mmio_map_bus(uc, state);
|
|
uc_mem_write(uc, RESET_TRAMPOLINE, &A64_ERET, sizeof(A64_ERET));
|
|
|
|
uint64_t v = PSTATE_EL3H_DAIF;
|
|
uc_reg_write(uc, UC_ARM64_REG_PSTATE, &v);
|
|
uc_arm64_cp_reg spsr_el3 = {4, 0, 3, 6, 0, PSTATE_EL3H_DAIF}; // crn crm op0 op1 op2
|
|
uc_arm64_cp_reg elr_el3 = {4, 0, 3, 6, 1, entry};
|
|
uc_arm64_cp_reg scr_el3 = {1, 1, 3, 6, 0, 0}; // reset: secure, nothing routed
|
|
uc_reg_write(uc, UC_ARM64_REG_CP_REG, &spsr_el3);
|
|
uc_reg_write(uc, UC_ARM64_REG_CP_REG, &elr_el3);
|
|
uc_reg_write(uc, UC_ARM64_REG_CP_REG, &scr_el3);
|
|
v = entry;
|
|
uc_reg_write(uc, UC_ARM64_REG_ELR_EL1, &v);
|
|
|
|
uc_hook_add(uc, &h_block, UC_HOOK_BLOCK, (void *)hook_block, nullptr, 1, 0);
|
|
uc_hook_add(uc, &h_unmapped, UC_HOOK_MEM_UNMAPPED, (void *)hook_unmapped,
|
|
nullptr, 1, 0);
|
|
uc_hook_add(uc, &h_intr, UC_HOOK_INTR, (void *)hook_intr, nullptr, 1, 0);
|
|
|
|
m.uc = uc;
|
|
m.pc = RESET_TRAMPOLINE;
|
|
m.ns = state->emu_usec * 1000; // released at the BPMP's current time
|
|
m.pending = 0;
|
|
m.wfe_next = false;
|
|
m.kill_pending = false;
|
|
m.retired = 0;
|
|
m.bus_accesses = 0;
|
|
m.cpu0 = Cpu0::Running;
|
|
m.boots++;
|
|
printf("[ccplex] CPU0 released: AArch64 EL3, MMU off, entry 0x%08llX "
|
|
"(boot #%u)\n", (unsigned long long)entry, m.boots);
|
|
fflush(stdout);
|
|
return true;
|
|
}
|
|
|
|
void try_release(EmuState *state) {
|
|
const char *why = boot_blocked(state);
|
|
if (why) {
|
|
// On silicon the core would never run and nothing would say why. Name
|
|
// it, once per distinct reason, so a payload that got the sequence wrong
|
|
// learns what it got wrong.
|
|
if (why != m.last_refusal)
|
|
printf("[ccplex] CPU0 released from reset but cannot run: %s\n", why);
|
|
m.last_refusal = why;
|
|
fflush(stdout);
|
|
return;
|
|
}
|
|
m.last_refusal = nullptr;
|
|
start_cpu0(state);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// ==========================================================================
|
|
|
|
void ccplex_reset(EmuState *state) {
|
|
g_state = state;
|
|
if (m.in_run) {
|
|
// A reset from inside CPU0's own slice: defer the teardown to the end
|
|
// of the run.
|
|
m.kill_pending = true;
|
|
m.reset_pending = true;
|
|
m.kill_reason = "SoC reset";
|
|
uc_emu_stop(m.uc);
|
|
return;
|
|
}
|
|
destroy_engine();
|
|
uint32_t boots = m.boots;
|
|
m = Model();
|
|
m.boots = boots;
|
|
}
|
|
|
|
void ccplex_car_write(EmuState *state, uint32_t offset, uint32_t val) {
|
|
switch (offset) {
|
|
case CAR_CCLK_BURST_POLICY: m.cclk_burst = val; break;
|
|
case CAR_PLLX_BASE: m.pllx_base = val; break;
|
|
case CAR_CLK_OUT_ENB_V: m.clk_enb_v = val; break;
|
|
case CAR_CLK_ENB_V_SET: m.clk_enb_v |= val; break;
|
|
case CAR_CLK_ENB_V_CLR:
|
|
m.clk_enb_v &= ~val;
|
|
if ((val & (1u << CLK_V_CPUG)) && m.cpu0 == Cpu0::Running) {
|
|
// Clock stopped under a running core: it freezes where it is. Treat
|
|
// it as wedged until the next reset, which is how bdk always follows
|
|
// this write anyway.
|
|
printf("[ccplex] CPUG clock gated under a running CPU0 - core frozen\n");
|
|
m.cpu0 = Cpu0::Hung;
|
|
}
|
|
break;
|
|
case CAR_RST_CPUG_CMPLX_SET:
|
|
m.rst_cpug |= val;
|
|
if (val & (RST_CPU0_MASK | RST_NONCPU))
|
|
stop_cpu0("held in reset");
|
|
break;
|
|
case CAR_RST_CPUG_CMPLX_CLR: {
|
|
bool was_held = held();
|
|
m.rst_cpug &= ~val;
|
|
if (was_held && !held() && m.cpu0 == Cpu0::Off)
|
|
try_release(state);
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool ccplex_car_read(uint32_t offset, uint32_t *out) {
|
|
switch (offset) {
|
|
case CAR_CLK_OUT_ENB_V:
|
|
*out = m.clk_enb_v;
|
|
return true;
|
|
case CAR_RST_CPUG_CMPLX_SET:
|
|
case CAR_RST_CPUG_CMPLX_CLR:
|
|
*out = m.rst_cpug;
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
uint32_t ccplex_sb_read(uint32_t offset) {
|
|
switch (offset) {
|
|
case SB_CSR: return m.sb_csr;
|
|
case SB_AA64_RESET_LOW: return m.sb_rst_lo;
|
|
case SB_AA64_RESET_HIGH: return m.sb_rst_hi;
|
|
default: return 0;
|
|
}
|
|
}
|
|
|
|
void ccplex_sb_write(EmuState *state, uint32_t offset, uint32_t val) {
|
|
(void)state;
|
|
switch (offset) {
|
|
case SB_CSR:
|
|
// NS_RST_VEC_WR_DIS (bit 1) is sticky until reset. It only locks out
|
|
// non-secure writers; the BPMP is secure, so the vector stays writable
|
|
// from here either way.
|
|
m.sb_csr |= val;
|
|
break;
|
|
case SB_AA64_RESET_LOW: m.sb_rst_lo = val; break;
|
|
case SB_AA64_RESET_HIGH: m.sb_rst_hi = val; break;
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
void ccplex_partition_changed(EmuState *state, int part, bool on) {
|
|
(void)state;
|
|
if (on)
|
|
return;
|
|
if (part == POWER_RAIL_CRAIL) stop_cpu0("CRAIL partition powergated");
|
|
if (part == POWER_RAIL_C0NC) stop_cpu0("C0NC partition powergated");
|
|
if (part == POWER_RAIL_CE0) stop_cpu0("CE0 partition powergated");
|
|
}
|
|
|
|
void ccplex_rail_changed(EmuState *state) {
|
|
if (m.cpu0 != Cpu0::Off && !cpu_rail_on(state))
|
|
stop_cpu0("CPU rail switched off");
|
|
}
|
|
|
|
void ccplex_bus_tick(EmuState *state) {
|
|
m.ns += NS_PER_BUS_ACCESS;
|
|
m.bus_accesses++;
|
|
state->emu_usec = m.ns / 1000;
|
|
}
|
|
|
|
bool ccplex_cpu0_running() { return m.cpu0 == Cpu0::Running && m.uc; }
|
|
|
|
void ccplex_run(EmuState *state, uint64_t until_us) {
|
|
if (!ccplex_cpu0_running())
|
|
return;
|
|
uint64_t target = until_us * 1000;
|
|
if (m.ns >= target)
|
|
return;
|
|
|
|
// While CPU0 runs, it is the bus master and "now" is its clock: every
|
|
// register model reads state->emu_usec, and must see CPU0's time, not the
|
|
// BPMP's. Both are put back afterwards.
|
|
uint64_t bpmp_usec = state->emu_usec;
|
|
BusMaster prev = g_bus_master;
|
|
g_bus_master = BUS_CPU0;
|
|
state->emu_usec = m.ns / 1000;
|
|
m.target_ns = target;
|
|
m.in_run = true;
|
|
|
|
uc_err err = uc_emu_start(m.uc, m.pc, 0, 0, 0);
|
|
|
|
commit_pending();
|
|
uc_reg_read(m.uc, UC_ARM64_REG_PC, &m.pc);
|
|
m.in_run = false;
|
|
g_bus_master = prev;
|
|
state->emu_usec = bpmp_usec;
|
|
|
|
if (m.kill_pending) {
|
|
m.kill_pending = false;
|
|
const char *why = m.kill_reason ? m.kill_reason : "stopped";
|
|
m.cpu0 = Cpu0::Running; // so stop_cpu0 does the teardown
|
|
stop_cpu0(why);
|
|
if (m.reset_pending)
|
|
ccplex_reset(state);
|
|
return;
|
|
}
|
|
if (err != UC_ERR_OK && m.cpu0 == Cpu0::Running) {
|
|
printf("[ccplex] CPU0 wedged at PC=0x%llX: %s\n",
|
|
(unsigned long long)m.pc, uc_strerror(err));
|
|
fflush(stdout);
|
|
m.cpu0 = Cpu0::Hung;
|
|
}
|
|
}
|