//! Core power management: C-states, CPU watchdog, CAC (DPM) weights, boost, //! and the P-state / C-state / CPPC getters for ACPI (CCX_SP3 §3.9, §3.13). pub use crate::ccx::power::{CstateInfo, Pstate, c2_latency_us, decode_pstate, pstates}; use fstart_arch::x86::cpu::amd::msr::{HWCR, Hwcr, PSTATE_0}; use fstart_arch::x86::cpu::msr::MsrAccess; use heapless::Vec; use tock_registers::interfaces::{ReadWriteable, Readable, Writeable}; use super::topology::Thread; use crate::ccx::msr::{ CPU_WDT_CFG, CSTATE_BASE_ADDR, CSTATE_CONFIG, CpuWdtCfg, CstateBaseAddr, CstateConfig, DPM_CFG, DPM_WAC_ACC_INDEX, DPM_WAC_DATA, DpmCfg, }; use crate::cpu::Silicon; use crate::smu::{ARGUMENT_COUNT, Error as SmuError, SmuMessages}; // --- C-states ------------------------------------------------------------- /// C-state policy (donor defaults: on, CC6 on, IO base 0x813). #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct CstatePolicy { pub enable: bool, /// Naples and Rome donors always enable CC6; disabling it is erratum /// 1474's workaround (ZP/Rome/Milan) and Milan's CBS option. pub cc6: bool, /// IO range trapped as C-state requests (8 ports, `_CST`). pub io_base: u16, } impl Default for CstatePolicy { fn default() -> Self { Self { enable: true, cc6: true, io_base: 0x813, } } } /// CSTATE_CONFIG: CC1 at DFS ID 8 on CCR0–2 and CC6 if enabled — the Milan /// rule `0x80808 | cc6·0x404040`, which gives the fixed Naples/Rome value /// 0x484848 with CC6 on. pub fn cstate_cfg(cc6: bool) -> u64 { let cc6 = u64::from(cc6); (CstateConfig::CCR0_CC1DFSID.val(8) + CstateConfig::CCR1_CC1DFSID.val(8) + CstateConfig::CCR2_CC1DFSID.val(8) + CstateConfig::CCR0_CC6EN.val(cc6) + CstateConfig::CCR1_CC6EN.val(cc6) + CstateConfig::CCR2_CC6EN.val(cc6)) .value } /// Program C-states on the executing thread (DXE MP callback, every /// thread; CSTATE_CONFIG on primary threads). pub fn init_cstates(cpu: &M, thread: &Thread, policy: &CstatePolicy) { if !policy.enable { return; } if thread.primary() { cpu.reg(CSTATE_CONFIG).set(cstate_cfg(policy.cc6)); } cpu.reg(CSTATE_BASE_ADDR) .write(CstateBaseAddr::CSTATE_ADDR.val(policy.io_base.into())); } pub fn cstate_info(policy: &CstatePolicy) -> Option { (policy.enable && policy.io_base != 0).then_some(CstateInfo { io_base: policy.io_base, cc6: policy.cc6, }) } // --- Boost and P0 ------------------------------------------------------------ /// Core performance boost on/off (donor default on), and the P0 rewrite every /// thread does after the SMU has set the P-states. pub fn init_pstate(cpu: &M, boost: bool) { let p0 = cpu.reg(PSTATE_0); p0.set(p0.get()); if !boost { cpu.reg(HWCR).modify(Hwcr::CPB_DIS::SET); } } // --- CPU watchdog ------------------------------------------------------------ /// CPU watchdog timeout: CpuWdtCountSel/TimeBase. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Wdt { pub count_sel: u8, pub time_base: u8, } /// Count multipliers by CpuWdtCountSel (PPR 54945/55803). const WDT_COUNTS: [u64; 10] = [4095, 2047, 1023, 511, 255, 127, 63, 31, 8191, 16383]; /// Time bases in ns by CpuWdtTimeBase. const WDT_BASES: [u64; 2] = [1_310_000, 1_280]; /// CountSel by increasing period (donor table, both directions walk it). const WDT_ORDER: [u8; 10] = [7, 6, 5, 4, 3, 2, 1, 0, 8, 9]; /// DF watchdog multipliers and time bases (DfGlobalCtrl \[18:16\], \[5:4\]; /// ZP DXE `0x98f0`, SSP DXE `0x9850`, identical). const DF_COUNTS: [u64; 8] = [31, 127, 255, 511, 1023, 2047, 4095, 16383]; const DF_BASES: [u64; 3] = [1_310_000, 1_280, 10]; impl Wdt { /// The donor default (PCD 0x100: 2047 × 1.31 ms). pub const DEFAULT: Self = Self { count_sel: 1, time_base: 0, }; /// Timeout in ns; `None` for reserved encodings. pub fn period_ns(self) -> Option { Some(WDT_COUNTS.get(self.count_sel as usize)? * WDT_BASES.get(self.time_base as usize)?) } /// Next longer (`step = 1`) or shorter (`-1`) setting. fn step(self, longer: bool) -> Option { let pos = WDT_ORDER.iter().position(|&c| c == self.count_sel)?; match (longer, pos, self.time_base) { // Longest count: move to the coarser base, shortest count. (true, 9, 0) => None, (true, 9, b) => Some(Self { count_sel: WDT_ORDER[0], time_base: b - 1, }), (true, p, b) => Some(Self { count_sel: WDT_ORDER[p + 1], time_base: b, }), // Shortest count: move to the finer base, longest count. (false, 0, 1) => None, (false, 0, _) => Some(Self { count_sel: WDT_ORDER[9], time_base: 1, }), (false, p, b) => Some(Self { count_sel: WDT_ORDER[p - 1], time_base: b, }), } } } /// The DF CCM watchdog period from DF::DfGlobalCtrl (D18F0x3F8 of node 0), /// `None` if disabled (time base 3). pub fn df_wdt_ns(df_global_ctrl: u32) -> Option { let base = DF_BASES.get(((df_global_ctrl >> 4) & 3) as usize)?; Some(DF_COUNTS[((df_global_ctrl >> 16) & 7) as usize] * base) } /// The CPU watchdog setting to use given the DF watchdog: Naples wants the /// CPU WDT *longer* than the DF's (raised step by step), Rome and Milan /// *shorter* (lowered). `None` = leave the CPU WDT off, as the donors do /// when no setting satisfies the rule (DXE ZP `0x1884`, SSP `0x1c0c`, GN /// `0x211c`). pub fn wdt_setting(generation: Silicon, requested: Wdt, df_global_ctrl: u32) -> Option { let Some(df) = df_wdt_ns(df_global_ctrl) else { return Some(requested); }; let longer = generation == Silicon::Zp; let ok = |w: Wdt| { w.period_ns() .is_some_and(|p| if longer { p >= df } else { p < df }) }; // At most 20 settings exist: bounded. core::iter::successors(Some(requested), |w| w.step(longer)) .take(WDT_ORDER.len() * WDT_BASES.len() + 1) .find(|&w| ok(w)) } /// Program CpuWdtCfg (primary threads). `severity` is Rome/Milan's \[9:7\] /// (PCD 0xff = keep the reset value). pub fn init_wdt(cpu: &M, setting: Option, severity: Option) { let reg = cpu.reg(CPU_WDT_CFG); match setting { Some(w) => { let sev = severity.map_or( tock_registers::fields::FieldValue::::none(), |s| CpuWdtCfg::SEVERITY.val(s.into()), ); reg.modify( CpuWdtCfg::EN::SET + CpuWdtCfg::TIME_BASE.val(w.time_base.into()) + CpuWdtCfg::COUNT_SEL.val(w.count_sel.into()) + sev, ); } None => reg.modify( CpuWdtCfg::EN::CLEAR + CpuWdtCfg::TIME_BASE.val(0) + CpuWdtCfg::COUNT_SEL.val(0), ), } } // --- CAC weights ------------------------------------------------------------- /// Weight chains: 21 on Zen/Zen 2, 23 on Zen 3. pub const fn cac_weight_count(generation: Silicon) -> usize { match generation { Silicon::Zp | Silicon::Ssp => 21, Silicon::Gn => 23, } } /// The weights, read once from the SMU (BSP). pub type CacWeights = Vec; /// Read the weights: V9 message 0x10 with `resp0 << 32 | resp1` /// (AmdNbioSmuV9Dxe `0x1066`), V11 0x09 with `resp1 << 32 | resp0` /// (SmuV11Dxe `0x37b8`). pub fn read_cac_weights( generation: Silicon, smu: &M, ) -> Result { let id = match generation { Silicon::Zp => 0x10, _ => 0x09, }; (0..cac_weight_count(generation) as u32) .map(|i| { let mut args = [0; ARGUMENT_COUNT]; args[0] = i; smu.send(id, &mut args)?; let (lo, hi) = (u64::from(args[0]), u64::from(args[1])); Ok(match generation { Silicon::Zp => lo << 32 | hi, _ => hi << 32 | lo, }) }) .collect() } /// Write the weights on a primary thread: DPM_CFG cleared while writing, /// then restored with CFG_LOCKED. pub fn write_cac_weights(cpu: &M, weights: &[u64]) { let saved = cpu.reg(DPM_CFG).get(); cpu.reg(DPM_CFG).set(0); weights.iter().enumerate().for_each(|(i, &w)| { cpu.reg(DPM_WAC_ACC_INDEX).set(i as u64); cpu.reg(DPM_WAC_DATA).set(w); }); cpu.reg(DPM_CFG).set(DpmCfg::CFG_LOCKED::SET.modify(saved)); } // --- ACPI getters -------------------------------------------------------------- /// How the OS gets CPPC. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Cppc { None, /// Rome: SMU shared-memory CPPC (PCCT), if the SMU feature is enabled. SharedMemory, /// Milan: CPPC MSRs (CPUID Fn8000_0008_EBX\[27\]). Msr, } /// CPPC availability. `smu_cppc` is the SMU's CPPC feature bit as enabled. pub fn cppc(generation: Silicon, cpuid_8000_0008_ebx: u32, smu_cppc: bool) -> Cppc { match generation { Silicon::Zp => Cppc::None, Silicon::Gn if cpuid_8000_0008_ebx & 1 << 27 != 0 => Cppc::Msr, _ if smu_cppc => Cppc::SharedMemory, _ => Cppc::None, } } #[cfg(test)] mod tests { use super::*; use fstart_arch::x86::cpu::msr::testing::MsrSpace; #[test] fn cc6_values() { assert_eq!(cstate_cfg(true), 0x48_4848); assert_eq!(cstate_cfg(false), 0x08_0808); let cpu = MsrSpace::new([]); init_cstates( &cpu, &Thread::new(Silicon::Gn, 1, true, false), &CstatePolicy::default(), ); let cpu = cpu.into_inner(); assert!(!cpu.written(0xc001_0296), "secondary thread"); assert_eq!(cpu.get(0xc001_0073), 0x813); } #[test] fn wdt_rule() { // DF: 4095 × 1.31 ms ([18:16] = 6, [5:4] = 0) = 5.36 s. let df = 6 << 16; // Default 2047 × 1.31 ms = 2.68 s: already shorter on Rome. assert_eq!( wdt_setting(Silicon::Ssp, Wdt::DEFAULT, df), Some(Wdt::DEFAULT) ); // Naples raises it to the first setting not shorter than the DF's. assert_eq!( wdt_setting(Silicon::Zp, Wdt::DEFAULT, df), Some(Wdt { count_sel: 0, time_base: 0 }) ); // DF 31 × 10 ns: nothing on the CPU is shorter, the WDT stays off. assert_eq!(wdt_setting(Silicon::Gn, Wdt::DEFAULT, 2 << 4), None); // DF 127 × 1.28 µs: Rome walks down through the 1.31 ms counts, // switches to 1.28 µs and stops at 63 × 1.28 µs. assert_eq!( wdt_setting(Silicon::Ssp, Wdt::DEFAULT, 1 << 16 | 1 << 4), Some(Wdt { count_sel: 6, time_base: 1 }) ); assert_eq!( wdt_setting(Silicon::Ssp, Wdt::DEFAULT, 3 << 4), Some(Wdt::DEFAULT) ); let cpu = MsrSpace::new([(0xc001_0074, 0x280)]); init_wdt(&cpu, Some(Wdt::DEFAULT), None); assert_eq!(cpu.into_inner().get(0xc001_0074), 0x289); } #[test] fn cac_weights() { struct Smu; impl SmuMessages for Smu { fn send(&self, id: u32, args: &mut [u32; ARGUMENT_COUNT]) -> Result<(), SmuError> { assert_eq!(id, 0x09); *args = [0x100 + args[0], 0xa, 0, 0, 0, 0]; Ok(()) } } let w = read_cac_weights(Silicon::Gn, &Smu).unwrap(); assert_eq!(w.len(), 23); assert_eq!(w[2], 0xa_0000_0102); let cpu = MsrSpace::new([(0xc001_1074, 0x1000)]); write_cac_weights(&cpu, &w[..2]); let cpu = cpu.into_inner(); assert_eq!(cpu.writes[0], (0xc001_1074, 0)); assert_eq!(cpu.writes[4], (0xc001_1077, 0xa_0000_0101)); assert_eq!(cpu.get(0xc001_1074), 0x8000_0000_0000_1000); } #[test] fn cppc_availability() { assert_eq!(cppc(Silicon::Gn, 1 << 27, false), Cppc::Msr); assert_eq!(cppc(Silicon::Ssp, 0, true), Cppc::SharedMemory); } }