axplat_aarch64_peripherals/
psci.rs

1//! ARM Power State Coordination Interface.
2
3#![allow(dead_code)]
4
5use core::sync::atomic::{AtomicBool, Ordering};
6
7const PSCI_0_2_FN_BASE: u32 = 0x84000000;
8const PSCI_0_2_64BIT: u32 = 0x40000000;
9const PSCI_0_2_FN_CPU_SUSPEND: u32 = PSCI_0_2_FN_BASE + 1;
10const PSCI_0_2_FN_CPU_OFF: u32 = PSCI_0_2_FN_BASE + 2;
11const PSCI_0_2_FN_CPU_ON: u32 = PSCI_0_2_FN_BASE + 3;
12const PSCI_0_2_FN_MIGRATE: u32 = PSCI_0_2_FN_BASE + 5;
13const PSCI_0_2_FN_SYSTEM_OFF: u32 = PSCI_0_2_FN_BASE + 8;
14const PSCI_0_2_FN_SYSTEM_RESET: u32 = PSCI_0_2_FN_BASE + 9;
15const PSCI_0_2_FN64_CPU_SUSPEND: u32 = PSCI_0_2_FN_BASE + PSCI_0_2_64BIT + 1;
16const PSCI_0_2_FN64_CPU_ON: u32 = PSCI_0_2_FN_BASE + PSCI_0_2_64BIT + 3;
17const PSCI_0_2_FN64_MIGRATE: u32 = PSCI_0_2_FN_BASE + PSCI_0_2_64BIT + 5;
18
19static PSCI_METHOD_HVC: AtomicBool = AtomicBool::new(false);
20
21/// PSCI return values, inclusive of all PSCI versions.
22#[derive(PartialEq, Debug)]
23#[repr(i32)]
24enum PsciError {
25    NotSupported = -1,
26    InvalidParams = -2,
27    Denied = -3,
28    AlreadyOn = -4,
29    OnPending = -5,
30    InternalFailure = -6,
31    NotPresent = -7,
32    Disabled = -8,
33    InvalidAddress = -9,
34}
35
36impl From<i32> for PsciError {
37    fn from(code: i32) -> PsciError {
38        use PsciError::*;
39        match code {
40            -1 => NotSupported,
41            -2 => InvalidParams,
42            -3 => Denied,
43            -4 => AlreadyOn,
44            -5 => OnPending,
45            -6 => InternalFailure,
46            -7 => NotPresent,
47            -8 => Disabled,
48            -9 => InvalidAddress,
49            _ => panic!("Unknown PSCI error code: {}", code),
50        }
51    }
52}
53
54/// arm,psci method: smc
55/// when SMCCC_CONDUIT_SMC = 1
56fn arm_smccc_smc(func: u32, arg0: usize, arg1: usize, arg2: usize) -> usize {
57    let mut ret;
58    unsafe {
59        core::arch::asm!(
60            "smc #0",
61            inlateout("x0") func as usize => ret,
62            in("x1") arg0,
63            in("x2") arg1,
64            in("x3") arg2,
65        )
66    }
67    ret
68}
69
70/// psci "hvc" method call
71fn psci_hvc_call(func: u32, arg0: usize, arg1: usize, arg2: usize) -> usize {
72    let ret;
73    unsafe {
74        core::arch::asm!(
75            "hvc #0",
76            inlateout("x0") func as usize => ret,
77            in("x1") arg0,
78            in("x2") arg1,
79            in("x3") arg2,
80        )
81    }
82    ret
83}
84
85fn psci_call(func: u32, arg0: usize, arg1: usize, arg2: usize) -> Result<(), PsciError> {
86    let ret = if PSCI_METHOD_HVC.load(Ordering::Acquire) {
87        psci_hvc_call(func, arg0, arg1, arg2)
88    } else {
89        arm_smccc_smc(func, arg0, arg1, arg2)
90    };
91    if ret == 0 {
92        Ok(())
93    } else {
94        Err(PsciError::from(ret as i32))
95    }
96}
97
98/// Initialize with the given PSCI method.
99///
100/// Method should be either "smc" or "hvc".
101pub fn init(method: &str) {
102    match method {
103        "smc" => PSCI_METHOD_HVC.store(false, Ordering::Release),
104        "hvc" => PSCI_METHOD_HVC.store(true, Ordering::Release),
105        _ => panic!("Unknown PSCI method: {}", method),
106    }
107}
108
109/// Shutdown the whole system, including all CPUs.
110pub fn system_off() -> ! {
111    info!("Shutting down...");
112    psci_call(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0).ok();
113    warn!("It should shutdown!");
114    loop {
115        axcpu::asm::halt();
116    }
117}
118
119/// Power up a core. This call is used to power up cores that either:
120///
121/// * Have not yet been booted into the calling supervisory software.
122/// * Have been previously powered down with a `cpu_off` call.
123///
124/// `target_cpu` contains a copy of the affinity fields of the MPIDR register.
125/// `entry_point` is the physical address of the secondary CPU's entry point.
126/// `arg` will be passed to the `X0` register of the secondary CPU.
127pub fn cpu_on(target_cpu: usize, entry_point: usize, arg: usize) {
128    info!("Starting CPU {:x} ON ...", target_cpu);
129    let res = psci_call(PSCI_0_2_FN64_CPU_ON, target_cpu, entry_point, arg);
130    if let Err(e) = res {
131        error!("failed to boot CPU {:x} ({:?})", target_cpu, e);
132    }
133}
134
135/// Power down the calling core. This call is intended for use in hotplug. A
136/// core that is powered down by `cpu_off` can only be powered up again in
137/// response to a `cpu_on`.
138pub fn cpu_off() {
139    const PSCI_POWER_STATE_TYPE_STANDBY: u32 = 0;
140    const PSCI_POWER_STATE_TYPE_POWER_DOWN: u32 = 1;
141    const PSCI_0_2_POWER_STATE_TYPE_SHIFT: u32 = 16;
142    let state: u32 = PSCI_POWER_STATE_TYPE_POWER_DOWN << PSCI_0_2_POWER_STATE_TYPE_SHIFT;
143    psci_call(PSCI_0_2_FN_CPU_OFF, state as usize, 0, 0).ok();
144}