Skip to content

Commit

Permalink
Revert "fix(AsusWMI): use platform to set TDP instead of FirmwareAttr…
Browse files Browse the repository at this point in the history
…ibutes"

This reverts commit be7c492.
  • Loading branch information
pastaq authored and ShadowApex committed Dec 24, 2024
1 parent 483cb66 commit 80b8a76
Showing 1 changed file with 97 additions and 29 deletions.
126 changes: 97 additions & 29 deletions src/performance/gpu/asus/asus_wmi.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,85 @@
use crate::performance::gpu::tdp::{TDPError, TDPResult};

use rog_platform::{error::PlatformError, platform::RogPlatform};

impl From<PlatformError> for TDPError {
fn from(value: PlatformError) -> Self {
Self::FailedOperation(value.to_string())
}
}
use rog_platform::{
firmware_attributes::{AttrValue, FirmwareAttributes},
platform::RogPlatform,
};

/// Implementation of asus-wmi sysfs
/// See https://www.kernel.org/doc/html/v6.8-rc4/admin-guide/abi-testing.html#abi-sys-devices-platform-platform-ppt-apu-sppt
pub struct AsusWmi {
platform: RogPlatform,
attributes: FirmwareAttributes,
}

impl AsusWmi {
/// test if we are in an asus system with asus-wmi loaded
pub async fn new() -> Option<Self> {
match RogPlatform::new() {
Ok(platform) => {
Ok(_) => {
log::info!("Module asus-wmi found");
Some(Self { platform })
Some(Self {
attributes: FirmwareAttributes::new(),
})
}
Err(err) => {
log::info!("Module asus-wmi not found: {err:?}");
log::info!("Module asus-wmi not found: {}", err);
None
}
}
}

/// Returns the currently set STAPM value
pub async fn tdp(&self) -> TDPResult<f64> {
Ok(self.platform.get_ppt_pl1_spl()? as f64)
match self
.attributes
.attributes()
.iter()
.find(|a| a.name() == "ppt_pl1_spl")
.unwrap()
.current_value()
{
Ok(attr_value) => match attr_value {
AttrValue::Integer(value) => {
log::info!("Found STAPM value: {value}");
Ok(value as f64)
}
_ => Err(TDPError::FailedOperation(
"Failed to read STAPM value".to_string(),
)),
},
Err(e) => Err(TDPError::FailedOperation(format!(
"Failed to read STAPM Value: {e:?}"
))),
}
}

/// Sets STAPM to the given value and adjusts the SPPT/FPPT to maintaing the current boost
/// ratio
pub async fn set_tdp(&mut self, value: f64) -> TDPResult<()> {
if value < 1.0 || value > u8::MAX as f64 {
return Err(TDPError::InvalidArgument(
"Value must be between 1 and 255".to_string(),
));
}

// Get the current Boost value
let boost = self.boost().await?;

// Set the STAPM value to the given TDP value
self.platform.set_ppt_pl1_spl(value as u8)?;
let val = AttrValue::Integer(value as i32);
match self
.attributes
.attributes_mut()
.iter()
.find(|a| a.name() == "ppt_pl1_spl")
.unwrap()
.set_current_value(val)
{
Ok(_) => {
log::info!("Set STAPM value to {value}");
}
Err(e) => {
return Err(TDPError::FailedOperation(format!(
"Failed to set STAPM value: {e:}"
)));
}
}

// Set the boost back to the expected value with the new TDP
// Set the boost back to the expeted value with the new TDP
self.set_boost(boost).await?;

Ok(())
Expand All @@ -63,10 +93,35 @@ impl AsusWmi {
return Err(e);
}
};
let slow_ppt = self.platform.get_ppt_platform_sppt()? as f64;

let slow_ppt = match self
.attributes
.attributes()
.iter()
.find(|a| a.name() == "ppt_platform_sppt")
.unwrap()
.current_value()
{
Ok(attr_value) => match attr_value {
AttrValue::Integer(value) => {
log::info!("Found Slow PPT value: {value}");
value as f64
}
_ => {
return Err(TDPError::FailedOperation(
"Failed to read Slow PPT value".to_string(),
))
}
},
Err(e) => {
return Err(TDPError::FailedOperation(format!(
"Failed to read Slow PPT value: {e:?}"
)))
}
};

let boost = slow_ppt - stapm;
log::info!("Found current boost: {boost}");

Ok(boost)
}

Expand All @@ -78,15 +133,28 @@ impl AsusWmi {
return Err(e);
}
};
if (stapm + value) < 1.0 || (stapm + value) > u8::MAX as f64 {
return Err(TDPError::InvalidArgument(
"Combined TDP + Boost value must be between 1 and 255".to_string(),
));
}
let boost = (stapm + value) as u8;

let sppt_val = (stapm + value) as i32;
let boost = AttrValue::Integer(sppt_val);

// ppt_platform_sppt will set sppt to value and fppt to value + 25%
self.platform.set_ppt_platform_sppt(boost)?;
match self
.attributes
.attributes_mut()
.iter()
.find(|a| a.name() == "ppt_platform_sppt")
.unwrap()
.set_current_value(boost)
{
Ok(_) => {
log::info!("Set Slow PPT to {sppt_val}");
}
Err(e) => {
return Err(TDPError::FailedOperation(format!(
"Failed to set Slow PPT: {e:?}"
)))
}
};

Ok(())
}
Expand Down

0 comments on commit 80b8a76

Please sign in to comment.