mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
Boot Linux on CPU0
This commit is contained in:
1 parent
d5bf5922ed
commit
20949b4242
21 files changed
+1865
-25
No files matched your search
+122
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* BPMP side of booting a Linux kernel on the emulator's CPU0.
|
||||
*
|
||||
* run.sh has the emulator place the kernel Image at 0x80080000 and the
|
||||
* device tree at 0x83000000 (--load), as a bootloader would have. This
|
||||
* payload powers CPU0 the way bdk's ccplex_boot_cpu0() does (Erista rail),
|
||||
* releases it at shim.S - which drops to EL1 and enters the kernel - and
|
||||
* then halts the BPMP for good, so every emulated cycle goes to CPU0.
|
||||
*/
|
||||
|
||||
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 KERNEL 0x80080000u
|
||||
|
||||
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 "shim_blob.h"
|
||||
|
||||
__attribute__((section(".text.start"), noreturn))
|
||||
void _start(void) {
|
||||
puts_("linux boot: kernel header ");
|
||||
puthex(((volatile u32 *)KERNEL)[14]); /* 'ARM\x64' at offset 0x38 */
|
||||
putc_('\n');
|
||||
for (u32 i = 0; i < sizeof(shim_blob) / sizeof(shim_blob[0]); i++)
|
||||
((volatile u32 *)CPU_ENTRY)[i] = shim_blob[i];
|
||||
|
||||
/* The clocks a bootloader has set before any OS runs: bdk hw_init()'s
|
||||
* _config_oscillators(), as hekate leaves them for switchroot's chain.
|
||||
* The kernel's super-clock code BUG()s on a burst policy still in
|
||||
* STANDBY, which is what these registers hold until someone writes
|
||||
* them. */
|
||||
REG(CAR + 0x55C) = (REG(CAR + 0x55C) & 0xFFFFFFF3) | 4; /* CLK_M divisor 2 */
|
||||
REG(0x700F0000 + 0x20) = 19200000; /* SYSCTR0_CNTFID0 */
|
||||
REG(0x60005014) = 0x45F; /* TIMERUS_USEC_CFG, 19.2 MHz */
|
||||
REG(CAR + 0x050) = 0x50000071; /* OSC_CTRL: 38.4 MHz */
|
||||
REG(CAR + 0x030) = 0x10; /* CLK_SYSTEM_RATE */
|
||||
REG(CAR + 0x400) = 0; /* CLK_SOURCE_SYS */
|
||||
REG(CAR + 0x028) = 0x20004444; /* SCLK_BURST_POLICY: run, PLLP_OUT2 */
|
||||
REG(CAR + 0x02C) = 0x80000000; /* SUPER_SCLK_DIVIDER */
|
||||
REG(CAR + 0x030) = 2; /* HCLK /1, PCLK /3 */
|
||||
REG(CAR + 0x370) = 0x20000000; /* CCLKLP_BURST_POLICY: run, clk_m */
|
||||
|
||||
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();
|
||||
puts_("linux boot: CPU0 released, BPMP halting\n");
|
||||
|
||||
/* bdk's bpmp_halt(): WAITEVENT | JTAG, no wake source. */
|
||||
REG(FLOW + 0x04) = (2u << 29) | (1u << 28);
|
||||
for (;;)
|
||||
;
|
||||
}
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
# Builds linux/boot.bin: shim.S (devkitA64) embedded in the BPMP payload.
|
||||
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/shim.o" "$D/shim.S"
|
||||
${A64}ld -Ttext=0xA0000000 -o "$D/shim.elf" "$D/shim.o"
|
||||
${A64}objcopy -O binary "$D/shim.elf" "$D/shim.bin"
|
||||
python3 - "$D/shim.bin" "$D/shim_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 shim.S. */\nstatic const u32 shim_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/boot.elf" "$D/boot.c"
|
||||
${ARM}objcopy -O binary "$D/boot.elf" "$D/boot.bin"
|
||||
echo "built $D/boot.bin"
|
||||
@@ -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*) }
|
||||
}
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
# linux/mkdtb.sh IN.dtb OUT.dtb - the switchroot device tree with what the
|
||||
# emulator boot needs: a console on UART-B (the port the emulator prints),
|
||||
# kept past init, one CPU, verbose logging. The bootloader normally edits
|
||||
# bootargs the same way; nothing else changes.
|
||||
set -e
|
||||
DTC=${DTC:-$HOME/l4t/kernel/kernel-4.9/scripts/dtc/dtc}
|
||||
IN=$1; OUT=$2
|
||||
T=$(mktemp -d)
|
||||
"$DTC" -I dtb -O dts -q -o "$T/in.dts" "$IN"
|
||||
ARGS=${BOOTARGS:-"earlycon=uart8250,mmio32,0x70006040 keep_bootcon console=tty0 maxcpus=1 nosmp loglevel=8 ignore_loglevel clk_ignore_unused printk.time=1"}
|
||||
python3 - "$T/in.dts" "$T/out.dts" "$ARGS" <<'PY'
|
||||
import re, sys
|
||||
s = open(sys.argv[1]).read()
|
||||
args = sys.argv[3]
|
||||
s, n = re.subn(r'(\n\tchosen \{[^}]*?bootargs = )"[^"]*";', lambda m: m.group(1) + '"' + args + '";', s, count=1)
|
||||
if n != 1:
|
||||
raise SystemExit("no /chosen bootargs found")
|
||||
open(sys.argv[2], "w").write(s)
|
||||
PY
|
||||
"$DTC" -I dts -O dtb -q -o "$OUT" "$T/out.dts"
|
||||
rm -rf "$T"
|
||||
echo "built $OUT"
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh
|
||||
# linux/run.sh IMAGE DTB [LOG] [SECS] [extra rcm_emu args...]
|
||||
#
|
||||
# Boots a Linux kernel (an arm64 Image, e.g. switchroot's L4T 4.9) on the
|
||||
# emulator's CPU0: the emulator places the Image at 0x80080000 and the
|
||||
# device tree at 0x83000000, linux/boot.bin powers CPU0 and hands over at
|
||||
# EL1 through linux/shim.S. Run linux/mkdtb.sh on the device tree first so
|
||||
# it carries an earlycon on UART-B, which the emulator prints.
|
||||
#
|
||||
# Needs an emulator built against Unicorn 2.1 or later (make UNICORN_DIR=...).
|
||||
set -e
|
||||
D=$(cd "$(dirname "$0")" && pwd)
|
||||
E=$(cd "$D/.." && pwd)
|
||||
IMAGE=$1; DTB=$2; LOG=${3:-$D/boot.log}; SECS=${4:-600}
|
||||
shift 4 2>/dev/null || shift $#
|
||||
export PATH=/opt/devkitpro/devkitARM/bin:/opt/devkitpro/devkitA64/bin:$PATH
|
||||
[ -f "$D/boot.bin" ] || sh "$D/build.sh"
|
||||
cd "$E"
|
||||
SDL_VIDEODRIVER=${SDL_VIDEODRIVER:-dummy} RCM_EMU_NO_AUDIO=1 timeout "$SECS" \
|
||||
./rcm_emu "$D/boot.bin" --oem erista \
|
||||
--load "$IMAGE@0x80080000" --load "$DTB@0x83000000" "$@" > "$LOG" 2>&1 || true
|
||||
echo "log: $LOG ($(grep -a -c '\[uartB\]' "$LOG") kernel lines)"
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* The hand-over a kernel expects from its bootloader, as switchroot's chain
|
||||
* (hekate, ATF, U-Boot) leaves it: AArch64 EL1h, non-secure, MMU and caches
|
||||
* off, x0 = the device tree, x1..x3 = 0 (Documentation/arm64/booting.txt).
|
||||
*
|
||||
* CPU0 enters here at EL3. EL2 is configured so EL1 runs AArch64 with the
|
||||
* timers and FP/SIMD untrapped, then one ERET lands in the kernel at EL1.
|
||||
* PSCI calls the kernel makes over SMC are answered by the emulator, which
|
||||
* plays the secure monitor.
|
||||
*/
|
||||
.equ KERNEL, 0x80080000
|
||||
.equ DTB, 0x83000000
|
||||
|
||||
.text
|
||||
.global _start
|
||||
_start:
|
||||
mov x0, #0x431 /* SCR_EL3: RW, RES1, NS */
|
||||
msr scr_el3, x0
|
||||
msr cptr_el3, xzr /* FP/SIMD not trapped to EL3 */
|
||||
mov x0, #0x33FF
|
||||
msr cptr_el2, x0 /* RES1 bits, nothing trapped to EL2 */
|
||||
mov x0, #(1 << 31) /* HCR_EL2.RW: EL1 is AArch64 */
|
||||
msr hcr_el2, x0
|
||||
mov x0, #3 /* EL1 may use the physical timer */
|
||||
msr cnthctl_el2, x0
|
||||
msr cntvoff_el2, xzr
|
||||
ldr x0, =0x30C50830 /* SCTLR_EL1: RES1, MMU and caches off */
|
||||
msr sctlr_el1, x0
|
||||
mov x0, #0x3C5 /* EL1h, DAIF masked */
|
||||
msr spsr_el3, x0
|
||||
ldr x0, =KERNEL
|
||||
msr elr_el3, x0
|
||||
ldr x0, =DTB
|
||||
mov x1, xzr
|
||||
mov x2, xzr
|
||||
mov x3, xzr
|
||||
eret
|
||||
.ltorg
|
||||
Reference in new issue
Block a user