git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/smp.rs
//! The cores: found from the hardware, started through PSCI; each sets up
//! its own part of the GIC and its own timer, then idles in its idle task
//! until the scheduler gives it work.
//!
//! No board lists its cores. The GICv3 has a redistributor for each, its
//! frames one after another, each saying in GICR_TYPER which core it
//! serves (its MPIDR affinity) and whether it is the last: the kernel
//! walks them at boot ([`discover`]). Core 0 is the one the kernel booted
//! on; the others are numbered in the redistributors' order.
use core::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
/// The most cores the kernel runs on: what the scheduler's table and the
/// online mask hold. More takes the kernel's stacks and task table made at
/// boot for the cores found, not fixed here.
pub const MAX_CORES: usize = 64;
/// Each core's MPIDR affinity, and its redistributor frame, by core number.
static MPIDRS: [AtomicU64; MAX_CORES] = [const { AtomicU64::new(0) }; MAX_CORES];
static FRAMES: [AtomicUsize; MAX_CORES] = [const { AtomicUsize::new(0) }; MAX_CORES];
/// How many cores were found.
static FOUND: AtomicUsize = AtomicUsize::new(1);
/// Finds the cores from the GIC's redistributors, the booting one first.
/// Called once, on core 0, before the GIC is set up.
pub fn discover() {
let me = crate::arch::mpidr();
let mut cores = crate::gic::redistributors();
if let Some(at) = cores.iter().position(|&(_, mpidr)| mpidr == me) {
let booting = cores.remove(at);
cores.insert(0, booting);
}
let found = cores.len().min(MAX_CORES);
for (core, &(frame, mpidr)) in cores.iter().take(found).enumerate() {
MPIDRS[core].store(mpidr, Ordering::Relaxed);
FRAMES[core].store(frame, Ordering::Relaxed);
}
FOUND.store(found.max(1), Ordering::Release);
}
/// Core `core`'s MPIDR affinity.
pub fn mpidr(core: usize) -> u64 {
MPIDRS[core].load(Ordering::Relaxed)
}
/// Core `core`'s redistributor, as the index of its frame.
pub fn frame(core: usize) -> usize {
FRAMES[core].load(Ordering::Relaxed)
}
/// How many cores the hardware has (up to [`MAX_CORES`]).
pub fn found() -> usize {
FOUND.load(Ordering::Acquire)
}
/// Bit n set once core n runs.
static ONLINE: AtomicU64 = AtomicU64::new(1);
/// The EL the kernel was entered at, which picks the PSCI conduit.
static ENTERED_AT: AtomicUsize = AtomicUsize::new(1);
const PSCI_CPU_ON: u64 = 0xc400_0003;
/// Calls PSCI through the conduit firmware offers: SMC when the kernel was
/// entered at EL2 (something above it, TF-A or QEMU, answers SMCs) or on a
/// board that always has TF-A; HVC otherwise (QEMU's own PSCI).
fn psci(function: u64, a: u64, b: u64, c: u64) -> i64 {
let result: i64;
// SAFETY: a PSCI call; firmware preserves all but x0–x3.
unsafe {
if crate::board::PSCI_SMC || ENTERED_AT.load(Ordering::Relaxed) == 2 {
core::arch::asm!("smc #0", inout("x0") function as i64 => result, inout("x1") a => _, inout("x2") b => _, inout("x3") c => _, options(nostack));
} else {
core::arch::asm!("hvc #0", inout("x0") function as i64 => result, inout("x1") a => _, inout("x2") b => _, inout("x3") c => _, options(nostack));
}
}
result
}
/// Restarts the machine through PSCI SYSTEM_RESET.
pub fn system_reset() -> ! {
const PSCI_SYSTEM_RESET: u64 = 0x8400_0009;
psci(PSCI_SYSTEM_RESET, 0, 0, 0);
loop {
core::hint::spin_loop();
}
}
/// Starts every other core found, and waits until each one that PSCI
/// accepted is up. Returns how many cores run.
pub fn start_cores(entered_at: usize) -> usize {
for psci in ["arm,psci", "arm,psci-0.2", "arm,psci-1.0"] {
crate::device::mark_compatible(psci, "psci", crate::device::Class::Power);
}
ENTERED_AT.store(entered_at, Ordering::Relaxed);
let entry = crate::mmu::physical(crate::boot::_start_secondary as *const () as usize) as u64;
let mut expected = 1u64;
for core in 1..found() {
if psci(PSCI_CPU_ON, mpidr(core), entry, core as u64) == 0 {
expected |= 1 << core;
}
}
while ONLINE.load(Ordering::Acquire) != expected {
core::hint::spin_loop();
}
expected.count_ones() as usize
}
/// Cores that run, as a bit mask.
pub fn online() -> u64 {
ONLINE.load(Ordering::Acquire)
}
/// Each other core's Rust entry, from `boot::_start_secondary`: on its idle
/// task's stack, MMU and caches on.
pub extern "C" fn secondary_main(core: usize) -> ! {
crate::mmu::close_lower_half();
crate::exceptions::init();
crate::gic::init_core(core);
crate::timer::start();
ONLINE.fetch_or(1 << core, Ordering::Release);
crate::irq::enable();
crate::sched::idle()
}