Skip to content

refactor(aarch64): decode PSCI calls into an enum - #446

Open
mango-duck wants to merge 1 commit into
syswonder:devfrom
mango-duck:feat/psci-enum-refactor
Open

mango-duck wants to merge 1 commit into
syswonder:devfrom
mango-duck:feat/psci-enum-refactor

Conversation

@mango-duck

@mango-duck mango-duck commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Split the PSCI path into a decode step and an execution step. The incoming function ID is decoded into an enum value, one variant per supported call, with the call's arguments carried along; execution then dispatches on the variant.

The argument layout is now visible in the type, not inferred from register reads. Execution has to handle every variant, so a missing arm breaks the build.

Motivation

Follow-up to the review in #441, which suggested an enum-based decoder so that missed PSCI calls become compile errors. The old PSCI code had two problems:

  • The meaning and count of each argument were implicit in the register reads, and not visible at the dispatch point.
  • A function ID without an arm fell through to a runtime NOT_SUPPORTED.

Verification

Passed:

  • cargo fmt --check and make ARCH=aarch64 BOARD=rk3588 .
  • Orange Pi 5 Ultra (RK3588) with root zone + 1 guest:
    • all CPUs boot, guest starts and returns rc=0
    • CPU_ON wakes the target CPU
    • function ID 0x84000050, which has no variant, returns NOT_SUPPORTED
    • echo b > /proc/sysrq-trigger in the root zone resets the board to U-Boot through SYSTEM_RESET

Risks and Limitations

  • Nothing here should change behaviour: every function ID keeps its return value. Error codes now come from the standard PSCI error type instead of hand-written constants, with the same values.
  • aarch64 only.
  • CPU_SUSPEND and CPU_OFF are unchanged from upstream. Suspend and hotplug are not touched, and neither was exercised on hardware.
  • The function ID constants stay local as u64 values; the smccc equivalents are u32 , so switching would add a width conversion.

@mango-duck
mango-duck force-pushed the feat/psci-enum-refactor branch from 5f23c0c to fb29178 Compare November 24, 2026 08:36
@li041

li041 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR. The refactor itself looks well thoughout.

One thing I'd like to raise: the PR description looks like it was generated with AI assistance, and the line breaks are off. I'm not against using AI to help draft descriptions, but the PR description is one of our main channels of communication — for reviewers now, and for anyone reading the history later.

So it deserves the same care as the code: please re-read it and fix the wrapping, and make sure the wording actually reflects what the diff does.

@li041 li041 self-assigned this Nov 25, 2026
Decode the function ID into a PsciCall enum variant that carries its arguments, then dispatch on the variant exhaustively,
so a missing arm breaks the build. Argument layout is now visible in the type. SYSTEM_RESET/SYSTEM_OFF move into standalone functions.
@mango-duck
mango-duck force-pushed the feat/psci-enum-refactor branch from fb29178 to a98d4be Compare November 25, 2026 09:58
@mango-duck

Copy link
Copy Markdown
Contributor Author

Thanks for catching that. I’ve rewritten the description and re-wrapped the lines — the wording now matches what the diff actually does.

@agicy
agicy self-requested a review November 26, 2026 13:53
@agicy

agicy commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Thanks for following up on #441!

Looking at the two-step PsciCall enum in full, duplicating the call list across PsciFnId, PsciCall, decode, and execute (plus the #[allow(dead_code)] fields) feels heavier than necessary, while the handlers still take raw u64s.

We can make this much leaner and safer at the same time:

  1. Drop the local PsciFnId / SMCccFnId constants and match smccc::psci::* (u32) in a single match, returning Result<u64, psci::Error> so error conversion happens once at the caller (and psci_system_off can return -> !).
  2. Normalize arguments once at entry, and give handlers validated parameter types so invalid/out-of-bounds inputs are rejected by construction.

Rough sketch:

use smccc::{error::SUCCESS, psci::{self, *}};

struct ZoneCpuId(usize);
impl TryFrom<u64> for ZoneCpuId {
    type Error = psci::Error;
    fn try_from(mpidr: u64) -> Result<Self, Self::Error> {
        if mpidr & !MPIDR_MASK != 0 {
            return Err(Error::InvalidParameters);
        }
        let cpu = try_mpidr_to_cpuid(mpidr).ok_or(Error::InvalidParameters)?;
        this_zone().read().cpu_set().contains_cpu(cpu)
            .then_some(Self(cpu))
            .ok_or(Error::InvalidParameters)
    }
}

struct AffinityLevel(LowestAffinityLevel);
impl TryFrom<u64> for AffinityLevel {
    type Error = psci::Error;
    fn try_from(raw: u64) -> Result<Self, Self::Error> {
        (raw == 0).then_some(Self(LowestAffinityLevel::All)).ok_or(Error::InvalidParameters)
    }
}

fn psci_emulate_cpu_on(cpu: ZoneCpuId, entry: GuestPhysAddr, _ctx: u64) -> Result<u64, psci::Error> { ... }
fn psci_affinity_info(cpu: ZoneCpuId, _level: AffinityLevel) -> Result<u64, psci::Error> { ... }

fn handle_psci_smc(func_id: u32, arg0: u64, arg1: u64, arg2: u64) -> Result<u64, psci::Error> {
    // check or conversion?

    match func_id {
        PSCI_VERSION => Ok(u32::from(Version { major: 1, minor: 1 }) as u64),
        PSCI_CPU_SUSPEND_32 | PSCI_CPU_SUSPEND_64 => psci_cpu_suspend(arg0 as u32, arg1 as _, arg2),
        PSCI_CPU_OFF => todo!(),
        PSCI_CPU_ON_32 | PSCI_CPU_ON_64 => psci_emulate_cpu_on(arg0.try_into()?, arg1 as _, arg2),
        PSCI_AFFINITY_INFO_32 | PSCI_AFFINITY_INFO_64 => {
            psci_affinity_info(arg0.try_into()?, arg1.try_into()?)
        }
        PSCI_MIGRATE_INFO_TYPE => Ok(MigrateType::MigrationNotRequired as u64),
        PSCI_FEATURES => psci_emulate_features_info(arg0 as u32),
        PSCI_SYSTEM_RESET => psci_system_reset(),
        PSCI_SYSTEM_OFF => psci_system_off(),
        _ => Err(Error::NotSupported),
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants