From 262c70fb85c97f98d71ad3d9d41f428b8b6b4187 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 14 May 2025 01:23:50 +0300 Subject: [PATCH] cpu: support AMD properly --- src/cpu.rs | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 85c10cf..09290cf 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -102,23 +102,56 @@ pub fn set_turbo(setting: TurboSetting) -> Result<()> { let value_boost = match setting { TurboSetting::Always => "1", // boost = 1 means turbo is enabled TurboSetting::Never => "0", // boost = 0 means turbo is disabled - TurboSetting::Auto => return Err(ControlError::InvalidValueError("Turbo Auto cannot be directly set via intel_pstate/no_turbo or cpufreq/boost. System default.".to_string())), + TurboSetting::Auto => return Err(ControlError::InvalidValueError("Turbo Auto cannot be directly set via intel_pstate/no_turbo or cpufreq/boost. System default.".to_string())), }; - + + // AMD specific paths + let amd_pstate_path = "/sys/devices/system/cpu/amd_pstate/cpufreq/boost"; + let msr_boost_path = "/sys/devices/system/cpu/cpufreq/amd_pstate_enable_boost"; + + // Path priority (from most to least specific) let pstate_path = "/sys/devices/system/cpu/intel_pstate/no_turbo"; let boost_path = "/sys/devices/system/cpu/cpufreq/boost"; + // Try each boost control path in order of specificity if Path::new(pstate_path).exists() { write_sysfs_value(pstate_path, value_pstate) + } else if Path::new(amd_pstate_path).exists() { + write_sysfs_value(amd_pstate_path, value_boost) + } else if Path::new(msr_boost_path).exists() { + write_sysfs_value(msr_boost_path, value_boost) } else if Path::new(boost_path).exists() { write_sysfs_value(boost_path, value_boost) } else { - Err(ControlError::NotSupported( - "Neither intel_pstate/no_turbo nor cpufreq/boost found.".to_string(), - )) + // Also try per-core cpufreq boost for some AMD systems + let result = try_set_per_core_boost(value_boost)?; + if result { + Ok(()) + } else { + Err(ControlError::NotSupported( + "No supported CPU boost control mechanism found.".to_string(), + )) + } } } +/// Try to set boost on a per-core basis for systems that support it +fn try_set_per_core_boost(value: &str) -> Result { + let mut success = false; + let num_cores = get_logical_core_count()?; + + for core_id in 0..num_cores { + let boost_path = format!("/sys/devices/system/cpu/cpu{}/cpufreq/boost", core_id); + + if Path::new(&boost_path).exists() { + write_sysfs_value(&boost_path, value)?; + success = true; + } + } + + Ok(success) +} + pub fn set_epp(epp: &str, core_id: Option) -> Result<()> { let action = |id: u32| { let path = format!("/sys/devices/system/cpu/cpu{id}/cpufreq/energy_performance_preference");