Boot Linux on CPU0

This commit is contained in:
Baptiste MOINE committed 2026-11-05 23:45:44 +01:00
1 parent d5bf5922ed
commit 20949b4242
21 files changed
+1865 -25

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
#!/bin/sh
# Builds tests/kernelcpu/payload.bin: cpu0.S with devkitA64's assembler,
# embedded as a word array into the BPMP payload built with arm-none-eabi.
set -e
D=$(dirname "$0")
A64=${A64:-/opt/devkitpro/devkitA64/bin/aarch64-none-elf-}
ARM=${ARM:-/opt/devkitpro/devkitARM/bin/arm-none-eabi-}
${A64}gcc -c -o "$D/cpu0.o" "$D/cpu0.S"
${A64}ld -Ttext=0xA0000000 -o "$D/cpu0.elf" "$D/cpu0.o"
${A64}objcopy -O binary "$D/cpu0.elf" "$D/cpu0.bin"
python3 - "$D/cpu0.bin" "$D/cpu0_blob.h" <<'PY'
import sys, struct
b = open(sys.argv[1], "rb").read()
b += b"\0" * (-len(b) % 4)
w = struct.unpack("<%dI" % (len(b) // 4), b)
with open(sys.argv[2], "w") as f:
f.write("/* Generated by build.sh from cpu0.S. */\nstatic const u32 cpu0_blob[] = {\n")
for i in range(0, len(w), 6):
f.write(" " + ", ".join("0x%08x" % x for x in w[i:i + 6]) + ",\n")
f.write("};\n")
PY
${ARM}gcc -march=armv4t -marm -Os -ffreestanding -nostdlib -fno-builtin -I"$D" \
-T "$D/link.ld" -o "$D/payload.elf" "$D/payload.c"
${ARM}objcopy -O binary "$D/payload.elf" "$D/payload.bin"
echo "built $D/payload.bin"
+124
View File
@@ -0,0 +1,124 @@
/*
* CPU0 side of the kernel-CPU test: what a kernel needs from the core
* model, exercised the way Linux uses it.
*
* Entered at EL3, MMU off, at 0xA0000000. Drops to EL1h the way a boot
* shim hands over to a kernel, then:
* [1] CurrentEL at EL1 (expect 0x4)
* [2] PSCI_VERSION over SMC (expect 0x00010000)
* [3] ESR_EL1 seen by the BRK handler (expect 0xF2000123)
* [4] CNTFRQ_EL0 (expect 19200000)
* [5] virtual timer interrupts taken (expect 10)
* [6] CNTVCT ticks across them (expect about 10 x 19200)
* [7] the last interrupt ID acknowledged (expect 27)
* [8] instructions spent waiting: the WFI loop count (expect small)
* [0] magic 'KCPU', written last
* then parks in WFE.
*/
.equ MBOX, 0xA0030000
.equ GICD, 0x50041000
.equ GICC, 0x50042000
.equ TICK, 19200 /* 1 ms at 19.2 MHz */
.text
.global _start
_start:
mov x0, #0x431 /* SCR_EL3: RW, RES1, NS */
msr scr_el3, x0
mov x0, #(1 << 31) /* HCR_EL2.RW: EL1 is AArch64 */
msr hcr_el2, x0
mov x0, #3
msr cnthctl_el2, x0
msr cntvoff_el2, xzr
ldr x0, =0x30C50830 /* SCTLR_EL1: RES1, MMU off */
msr sctlr_el1, x0
mov x0, #0x3C5 /* EL1h, DAIF masked */
msr spsr_el3, x0
adr x0, el1
msr elr_el3, x0
eret
el1:
adr x0, vectors
msr vbar_el1, x0
isb
ldr x1, =MBOX
mrs x2, currentel
str w2, [x1, #4]
ldr x0, =0x84000000 /* PSCI_VERSION */
smc #0
str w0, [x1, #8]
brk #0x123 /* the sync handler steps over it */
mrs x3, cntfrq_el0
str w3, [x1, #16]
ldr x4, =GICD
mov w5, #1
str w5, [x4] /* GICD_CTLR */
mov w5, #(1 << 27)
str w5, [x4, #0x100] /* ISENABLER0: PPI 27 */
ldr w5, [x4, #0x418]
and w5, w5, #0x00FFFFFF
mov w12, #0xA0000000
orr w5, w5, w12
str w5, [x4, #0x418] /* IPRIORITYR[27] = 0xA0 */
ldr x6, =GICC
mov w5, #0xF0
str w5, [x6, #4] /* GICC_PMR */
mov w5, #1
str w5, [x6] /* GICC_CTLR */
mov w7, #TICK
msr cntv_tval_el0, x7
mov w7, #1
msr cntv_ctl_el0, x7
mrs x8, cntvct_el0
mov w11, #0
msr daifclr, #2 /* IRQs on */
1: wfi
add w11, w11, #1
ldr w9, [x1, #20]
cmp w9, #10
b.lo 1b
msr daifset, #2
mrs x10, cntvct_el0
sub x10, x10, x8
str w10, [x1, #24]
str w11, [x1, #32]
mov w7, #0
msr cntv_ctl_el0, x7
ldr w2, =0x4B435055 /* 'KCPU' */
str w2, [x1]
2: wfe
b 2b
.ltorg
.balign 2048
vectors:
.skip 0x200 /* current EL with SP_EL0: unused */
/* 0x200: synchronous, current EL with SP_ELx */
mrs x20, esr_el1
str w20, [x1, #12]
mrs x21, elr_el1
add x21, x21, #4
msr elr_el1, x21
eret
.balign 0x80
/* 0x280: IRQ, current EL with SP_ELx */
ldr w22, [x6, #0x0C] /* GICC_IAR */
ldr w23, [x1, #20]
add w23, w23, #1
str w23, [x1, #20]
str w22, [x1, #28]
mov w24, #TICK
msr cntv_tval_el0, x24
str w22, [x6, #0x10] /* GICC_EOIR */
eret
.balign 0x80
.skip 0x80 * 2 /* FIQ, SError */
.skip 0x400 /* lower ELs: unused */
+10
View File
@@ -0,0 +1,10 @@
/* Loaded and entered at 0x40010000, like any RCM payload. */
ENTRY(_start)
SECTIONS {
. = 0x40010000;
.text : { KEEP(*(.text.start)) *(.text*) }
.rodata : { *(.rodata*) }
.data : { *(.data*) }
.bss : { *(.bss*) *(COMMON) }
/DISCARD/ : { *(.comment) *(.note*) *(.ARM.attributes) *(.ARM.exidx*) }
}
+133
View File
@@ -0,0 +1,133 @@
/*
* Kernel-CPU regression payload: boots CPU0 at cpu0.S and reports its mailbox.
*
* A freestanding BPMP payload (ARM mode, loaded at 0x40010000 like any RCM
* payload) that drives the emulator's CPU0 model through the same register
* sequence as bdk's ccplex_boot_cpu0(), and reports what happened on UART-B.
* tests/ccplex/run.sh checks the emulator's log for the expected lines.
*
* 1. Release CPU0 from reset with nothing powered. The core must NOT run,
* and the emulator must say which precondition is missing.
* 2. Do the real bring-up - CPU rail (MAX77620 GPIO5 + MAX77621 VOUT on
* I2C5), CRAIL/C0NC/CE0 partitions, CPUG + MSELECT clocks, MSELECT out
* of reset, PLLX, RAM repair, SB_AA64_RESET_LOW - then release it. An
* AArch64 blob copied to DRAM records CurrentEL and a TIMERUS delta in
* a mailbox, writes a magic word last, and parks in WFE.
* Released first with its clock stopped (CLK_CPUG_CMPLX), it must
* stay still until the stop is cleared.
* 3. Put CPU0 back into reset and power the console off.
*
* Built by `make test` with arm-none-eabi-gcc; no other toolchain needed, as
* the AArch64 side is a handful of pre-assembled words.
*/
typedef unsigned int u32;
#define REG(a) (*(volatile u32 *)(a))
#define TIMERUS 0x60005010u
#define CAR 0x60006000u
#define FLOW 0x60007000u
#define SB 0x6000C200u
#define UARTB_THR 0x70006040u
#define PMC 0x7000E400u
#define I2C5 0x7000D000u
#define CPU_ENTRY 0xA0000000u
#define MBOX 0xA0030000u
#define KCPU_MAGIC 0x4B435055u
static void putc_(char c) { REG(UARTB_THR) = (u32)(unsigned char)c; }
static void puts_(const char *s) {
while (*s)
putc_(*s++);
}
static void puthex(u32 v) {
for (int i = 28; i >= 0; i -= 4)
putc_("0123456789ABCDEF"[(v >> i) & 0xF]);
}
static void udelay(u32 us) {
u32 start = REG(TIMERUS);
while (REG(TIMERUS) - start < us)
;
}
/* bdk-style normal-mode I2C write: [reg, val] in CMD_DATA1, 2 bytes, GO. */
static void i2c5_write(u32 dev, u32 reg, u32 val) {
REG(I2C5 + 0x04) = dev << 1; /* CMD_ADDR0 */
REG(I2C5 + 0x0C) = reg | (val << 8); /* CMD_DATA1 */
REG(I2C5 + 0x00) = (1u << 1) | (1u << 9); /* CNFG: 2 bytes, write, GO */
}
static void pwrgate_on(u32 part) {
if (!(REG(PMC + 0x38) & (1u << part))) /* PWRGATE_STATUS */
REG(PMC + 0x30) = part | (1u << 8); /* PWRGATE_TOGGLE */
}
#define RST_CPU0 ((1u << 30) | (1u << 24) | (1u << 16) | (1u << 0))
#define RST_NONCPU (1u << 29)
static void release_cpu0(void) {
REG(SB + 0x30) = CPU_ENTRY | 1u; /* SB_AA64_RESET_LOW */
REG(SB + 0x34) = 0; /* SB_AA64_RESET_HIGH */
REG(CAR + 0x454) = RST_NONCPU; /* RST_CPUG_CMPLX_CLR */
REG(CAR + 0x454) = RST_CPU0;
}
static void hold_cpu0(void) {
REG(CAR + 0x450) = RST_CPU0; /* RST_CPUG_CMPLX_SET */
REG(CAR + 0x450) = RST_NONCPU;
}
#include "cpu0_blob.h"
__attribute__((section(".text.start"), noreturn))
void _start(void) {
volatile u32 *mb = (volatile u32 *)MBOX;
puts_("kernelcpu test: start\n");
for (u32 i = 0; i < sizeof(cpu0_blob) / sizeof(cpu0_blob[0]); i++)
((volatile u32 *)CPU_ENTRY)[i] = cpu0_blob[i];
for (u32 i = 0; i < 16; i++)
mb[i] = 0;
/* The bring-up, in ccplex_boot_cpu0() order (Erista). */
i2c5_write(0x3C, 0x40, 0x1C); /* MAX77620 AME_GPIO: GPIO5 plain */
i2c5_write(0x3C, 0x3B, 0x09); /* GPIO5: push-pull, out high */
i2c5_write(0x1B, 0x00, 0x80 | 55); /* MAX77621 VOUT: on, 0.950 V */
REG(CAR + 0x0E0) = (1u << 30) | (2u << 20) | (156u << 8) | 2u; /* PLLX */
REG(CAR + 0x3B4) = 4u << 1; /* CLK_SOURCE_MSELECT */
REG(CAR + 0x440) = 1u << 3; /* CLK_ENB_V_SET: MSELECT */
REG(CAR + 0x020) = 0x20008888; /* CCLK_BURST: PLLX_OUT0_LJ */
REG(CAR + 0x024) = 1u << 31; /* SUPER_CCLK_DIVIDER */
REG(CAR + 0x440) = 1u << 0; /* CLK_ENB_V_SET: CPUG */
pwrgate_on(0); /* CRAIL */
pwrgate_on(15); /* C0NC */
pwrgate_on(14); /* CE0 */
REG(FLOW + 0x40) = 1; /* RAM_REPAIR_REQ */
while (!(REG(FLOW + 0x40) & 2))
;
REG(CAR + 0x434) = 1u << 3; /* RST_DEV_V_CLR: MSELECT */
release_cpu0();
u32 start = REG(TIMERUS);
while (mb[0] != KCPU_MAGIC && REG(TIMERUS) - start < 200000)
udelay(100);
static const char *names[9] = { "magic", "el", "psci", "esr", "cntfrq",
"irqs", "ticks", "irqid", "wfis" };
for (u32 i = 0; i < 9; i++) {
puts_(names[i]);
putc_('=');
puthex(mb[i]);
putc_(i == 8 ? '\n' : ' ');
}
hold_cpu0();
puts_("kernelcpu test: done\n");
i2c5_write(0x3C, 0x41, 0x02); /* MAX77620 ONOFFCNFG1: PWR_OFF */
for (;;)
;
}
+53
View File
@@ -0,0 +1,53 @@
#!/bin/sh
# Kernel-CPU regression test: CPU0 dropped to EL1 takes timer interrupts
# through the GIC, a BRK into its own vector, and a PSCI call over SMC.
# Usage: tests/kernelcpu/run.sh [rcm_emu] [payload]
EMU=${1:-./rcm_emu}
PAYLOAD=${2:-tests/kernelcpu/payload.bin}
LOG=${LOG:-tests/kernelcpu/out.log}
SDL_VIDEODRIVER=dummy RCM_EMU_NO_AUDIO=1 timeout 120 "$EMU" "$PAYLOAD" --oem erista \
> "$LOG" 2>&1
status=$?
fail=0
expect() {
if grep -q -- "$1" "$LOG"; then
echo " ok $2"
else
echo " FAIL $2 (missing: $1)"
fail=1
fi
}
echo "kernelcpu test (emulator exit $status):"
expect 'magic=4B435055' 'CPU0 ran the whole test and wrote the mailbox'
expect 'el=00000004' 'the shim dropped it to EL1'
expect 'psci=00010000' 'PSCI_VERSION over SMC answers 1.0'
expect 'esr=F2000123' 'BRK is taken to the EL1 vector with its ESR'
expect 'cntfrq=0124F800' 'the counter runs at 19.2 MHz'
expect 'irqs=0000000A' 'ten virtual timer interrupts arrive through the GIC'
expect 'irqid=0000001B' 'the GIC acknowledges them as PPI 27'
ticks=$(sed -n 's/.* ticks=\([0-9A-F]*\).*/\1/p' "$LOG" | head -1)
if [ -n "$ticks" ] && [ $((0x$ticks)) -ge 190000 ] && [ $((0x$ticks)) -le 200000 ]; then
echo " ok ten 1 ms ticks take about 192000 counts (0x$ticks)"
else
echo " FAIL the tick count across ten interrupts is off (0x${ticks:-?})"
fail=1
fi
retired=$(sed -n 's/.*CPU0 stopped (held in reset) after \([0-9]*\) instructions.*/\1/p' "$LOG" | head -1)
if [ -n "$retired" ] && [ "$retired" -lt 5000 ]; then
echo " ok WFI sleeps until the timer ($retired instructions retired)"
else
echo " FAIL WFI spun instead of sleeping (retired: ${retired:-?})"
fail=1
fi
if [ "$status" -eq 0 ]; then
echo " ok the emulator exited cleanly"
else
echo " FAIL the emulator exited with status $status (124 = timed out)"
fail=1
fi
[ $fail -eq 0 ] && echo "kernelcpu test: PASS" || echo "kernelcpu test: FAIL"
exit $fail