| 1 |
// Copyright (C) 2024 Advanced Micro Devices. All rights reserved. |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
// this software and associated documentation files (the "Software"), to deal in |
| 5 |
// the Software without restriction, including without limitation the rights to |
| 6 |
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 7 |
// the Software, and to permit persons to whom the Software is furnished to do so, |
| 8 |
// subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in all |
| 11 |
// copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 |
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 |
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 |
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 |
// |
| 20 |
use std::fmt; |
| 21 |
|
| 22 |
// Re-export all the alias type |
| 23 |
pub use crate::amdsmi_wrapper::{AmdsmiEventHandleT, AmdsmiProcessorHandle, AmdsmiSocketHandle}; |
| 24 |
|
| 25 |
// Re-export all the enums type |
| 26 |
pub use crate::amdsmi_wrapper::{ |
| 27 |
AmdsmiCachePropertyTypeT, AmdsmiCardFormFactorT, AmdsmiClkLimitTypeT, AmdsmiClkTypeT, |
| 28 |
AmdsmiComputePartitionTypeT, AmdsmiCounterCommandT, AmdsmiDevPerfLevelT, AmdsmiEventGroupT, |
| 29 |
AmdsmiEventTypeT, AmdsmiEvtNotificationTypeT, AmdsmiFreqIndT, AmdsmiFwBlockT, AmdsmiGpuBlockT, |
| 30 |
AmdsmiInitFlagsT, AmdsmiLinkTypeT, AmdsmiMemoryPartitionTypeT, AmdsmiMemoryTypeT, |
| 31 |
AmdsmiPowerProfilePresetMasksT, AmdsmiRasErrStateT, AmdsmiStatusT, |
| 32 |
AmdsmiTemperatureMetricT, AmdsmiTemperatureTypeT, AmdsmiUtilizationCounterTypeT, |
| 33 |
AmdsmiVoltageMetricT, AmdsmiVoltageTypeT, AmdsmiXgmiStatusT, ProcessorTypeT, AmdsmiAcceleratorPartitionTypeT |
| 34 |
}; |
| 35 |
|
| 36 |
// Re-export all the struct type |
| 37 |
pub use crate::amdsmi_wrapper::{ |
| 38 |
AmdMetricsTableHeaderT, AmdsmiAcceleratorPartitionProfileT, AmdsmiAsicInfoT, AmdsmiBoardInfoT, |
| 39 |
AmdsmiClkInfoT, AmdsmiCounterValueT, AmdsmiDpmPolicyEntryT, AmdsmiDpmPolicyT, |
| 40 |
AmdsmiDriverInfoT, AmdsmiEngineUsageT, AmdsmiErrorCountT, AmdsmiEvtNotificationDataT, |
| 41 |
AmdsmiFreqVoltRegionT, AmdsmiFrequenciesT, AmdsmiFrequencyRangeT, AmdsmiFwInfoT, |
| 42 |
AmdsmiGpuCacheInfoT, AmdsmiGpuCacheInfoTCache, AmdsmiGpuMetricsT, AmdsmiKfdInfoT, |
| 43 |
AmdsmiLinkMetricsT, AmdsmiLinkMetricsTLinks, AmdsmiNameValueT, |
| 44 |
AmdsmiOdVoltFreqDataT, AmdsmiP2pCapabilityT, AmdsmiPcieBandwidthT, AmdsmiPcieInfoT, |
| 45 |
AmdsmiPcieInfoTPcieMetric, AmdsmiPcieInfoTPcieStatic, AmdsmiPowerCapInfoT, AmdsmiPowerInfoT, |
| 46 |
AmdsmiPowerProfileStatusT, AmdsmiProcInfoT, AmdsmiProcInfoTEngineUsage, |
| 47 |
AmdsmiProcInfoTMemoryUsage, AmdsmiProcessInfoT, AmdsmiRangeT, AmdsmiRasFeatureT, |
| 48 |
AmdsmiRegTypeT, AmdsmiRetiredPageRecordT, AmdsmiTopologyNearestT, AmdsmiUtilizationCounterT, |
| 49 |
AmdsmiVbiosInfoT, AmdsmiVersionT, AmdsmiViolationStatusT, AmdsmiVramInfoT, AmdsmiVramUsageT, |
| 50 |
AmdsmiXgmiInfoT, AmdsmiNpsCapsT, AmdsmiNpsCapsTNpsFlags |
| 51 |
}; |
| 52 |
|
| 53 |
//Re-export all the union type |
| 54 |
pub use crate::amdsmi_wrapper::AmdsmiBdfT; |
| 55 |
|
| 56 |
//Re-export the constant type |
| 57 |
pub use crate::amdsmi_wrapper::{ |
| 58 |
AMDSMI_MAX_AID, AMDSMI_MAX_CACHE_TYPES, AMDSMI_MAX_CONTAINER_TYPE, AMDSMI_MAX_DEVICES, |
| 59 |
AMDSMI_MAX_ENGINES, AMDSMI_MAX_FAN_SPEED, AMDSMI_MAX_MM_IP_COUNT, AMDSMI_MAX_NUM_CLKS, |
| 60 |
AMDSMI_MAX_NUM_FREQUENCIES, AMDSMI_MAX_NUM_GFX_CLKS, AMDSMI_MAX_NUM_JPEG, |
| 61 |
AMDSMI_MAX_NUM_PM_POLICIES, AMDSMI_MAX_NUM_VCN, AMDSMI_MAX_NUM_XGMI_LINKS, |
| 62 |
AMDSMI_MAX_NUM_XGMI_PHYSICAL_LINK, AMDSMI_MAX_UTILIZATION_VALUES, AMDSMI_NUM_HBM_INSTANCES, |
| 63 |
AMDSMI_NUM_VOLTAGE_CURVE_POINTS, |
| 64 |
}; |
| 65 |
|
| 66 |
pub type AmdsmiResult<T> = Result<T, AmdsmiStatusT>; |
| 67 |
|
| 68 |
//#[macro_export] |
| 69 |
macro_rules! call_unsafe { |
| 70 |
($call:expr) => {{ |
| 71 |
let status = unsafe { $call }; |
| 72 |
if status != AmdsmiStatusT::AmdsmiStatusSuccess { |
| 73 |
return Err(status); |
| 74 |
} |
| 75 |
}}; |
| 76 |
} |
| 77 |
|
| 78 |
macro_rules! define_cstr { |
| 79 |
($len:expr) => {{ |
| 80 |
let buf = vec![0 as std::os::raw::c_char; $len as usize]; |
| 81 |
let len = $len as usize; |
| 82 |
(buf, len) |
| 83 |
}}; |
| 84 |
} |
| 85 |
|
| 86 |
macro_rules! cstr_to_string { |
| 87 |
($vec:expr) => {{ |
| 88 |
let c_str = unsafe { std::ffi::CStr::from_ptr($vec.as_ptr()) }; |
| 89 |
c_str.to_string_lossy().into_owned() |
| 90 |
}}; |
| 91 |
} |
| 92 |
|
| 93 |
// Implements getter methods for C string fields in the specified struct. |
| 94 |
// |
| 95 |
// This macro generates methods that convert C string fields to Rust `String`. |
| 96 |
// |
| 97 |
// # Arguments |
| 98 |
// |
| 99 |
// * `$struct_name` - The name of the struct for which the getter methods are being generated. |
| 100 |
// * `$($field_name),+` - The names of the fields for which the getter methods are being generated. |
| 101 |
// |
| 102 |
// # Example |
| 103 |
/// |
| 104 |
// ```rust |
| 105 |
// |
| 106 |
// use std::ffi::c_char; |
| 107 |
// |
| 108 |
// pub struct AmdsmiVbiosInfoT { |
| 109 |
// pub name: [c_char; 64usize], |
| 110 |
// pub version: [c_char; 32usize], |
| 111 |
// } |
| 112 |
// |
| 113 |
// impl_cstr_getters!(AmdsmiVbiosInfoT, name, version); |
| 114 |
// ``` |
| 115 |
macro_rules! impl_cstr_getters { |
| 116 |
($struct_name:ident, $($field_name:ident),+) => { |
| 117 |
impl $struct_name { |
| 118 |
$( |
| 119 |
pub fn $field_name(&self) -> String { |
| 120 |
unsafe { |
| 121 |
std::ffi::CStr::from_ptr(self.$field_name.as_ptr()) |
| 122 |
.to_string_lossy() |
| 123 |
.into_owned() |
| 124 |
} |
| 125 |
} |
| 126 |
)+ |
| 127 |
} |
| 128 |
}; |
| 129 |
} |
| 130 |
|
| 131 |
impl fmt::Display for AmdsmiStatusT { |
| 132 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 133 |
let status_str = crate::amdsmi_status_code_to_string(*self) |
| 134 |
.unwrap_or("An unknown error occurred".to_string()); |
| 135 |
write!(f, "{}", status_str) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
impl fmt::Display for AmdsmiBdfT { |
| 140 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 141 |
write!(f, "{}", self.to_human_readable_string()) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
impl AmdsmiBdfT { |
| 146 |
/// Converts the AmdsmiBdfT structure to a human-readable string. |
| 147 |
pub fn to_human_readable_string(&self) -> String { |
| 148 |
format!( |
| 149 |
"{:04x}:{:02x}:{:02x}.{:x}", |
| 150 |
unsafe { self.__bindgen_anon_1.domain_number() }, |
| 151 |
unsafe { self.__bindgen_anon_1.bus_number() }, |
| 152 |
unsafe { self.__bindgen_anon_1.device_number() }, |
| 153 |
unsafe { self.__bindgen_anon_1.function_number() } |
| 154 |
) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// Implement the getters for the C string fields in AmdsmiVbiosInfoT |
| 159 |
impl_cstr_getters!(AmdsmiVbiosInfoT, name, build_date, part_number, version, boot_firmware); |
| 160 |
|
| 161 |
// Implement the getters for the C string fields in AmdsmiAsicInfoT |
| 162 |
impl_cstr_getters!(AmdsmiAsicInfoT, market_name, vendor_name, asic_serial); |
| 163 |
|
| 164 |
// Implement the getters for the C string fields in AmdsmiBoardInfoT |
| 165 |
impl_cstr_getters!( |
| 166 |
AmdsmiBoardInfoT, |
| 167 |
model_number, |
| 168 |
product_serial, |
| 169 |
fru_id, |
| 170 |
product_name, |
| 171 |
manufacturer_name |
| 172 |
); |
| 173 |
|
| 174 |
// Implement the getters for the C string fields in AmdsmiDriverInfoT |
| 175 |
impl_cstr_getters!(AmdsmiDriverInfoT, driver_version, driver_date, driver_name); |
| 176 |
|
| 177 |
// Implement the getters for the C string fields in AmdsmiNameValueT |
| 178 |
impl_cstr_getters!(AmdsmiNameValueT, name); |
| 179 |
|
| 180 |
// Implement the getters for the C string fields in AmdsmiProcInfoT |
| 181 |
impl_cstr_getters!(AmdsmiProcInfoT, name, container_name); |
| 182 |
|
| 183 |
// Implement the getters for the C string fields in AmdsmiDpmPolicyEntryT |
| 184 |
impl_cstr_getters!(AmdsmiDpmPolicyEntryT, policy_description); |