| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <cstdint> |
| 7 | #include <libdrm/amdgpu_drm.h> |
| 8 | #include <libdrm/radeon_drm.h> |
| 9 | #include <optional> |
| 10 | #include <string> |
| 11 | #include <string_view> |
| 12 | #include <sys/ioctl.h> |
| 13 | #include <tuple> |
| 14 | #include <units.h> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace Utils::AMD { |
| 19 | |
| 20 | template<typename Data> |
| 21 | bool readAMDGPUInfoSensor(int deviceFD, Data *value, std::uint32_t sensor) |
| 22 | { |
| 23 | #if defined(AMDGPU_INFO_SENSOR) && defined(DRM_IOCTL_AMDGPU_INFO) |
| 24 | struct drm_amdgpu_info buffer = {}; |
| 25 | |
| 26 | buffer.query = AMDGPU_INFO_SENSOR; |
| 27 | buffer.return_pointer = reinterpret_cast<std::uint64_t>(value); |
| 28 | buffer.return_size = sizeof(*value); |
| 29 | buffer.sensor_info.type = sensor; |
| 30 | |
| 31 | return ioctl(deviceFD, DRM_IOCTL_AMDGPU_INFO, &buffer) >= 0; |
| 32 | #else |
| 33 | return false; |
| 34 | #endif |
| 35 | } |
| 36 | |
| 37 | template<typename Data> |
| 38 | bool readAMDGPUInfo(int deviceFD, Data *value, std::uint32_t query) |
| 39 | { |
| 40 | #if defined(DRM_IOCTL_AMDGPU_INFO) |
| 41 | struct drm_amdgpu_info buffer = {}; |
| 42 | |
| 43 | buffer.query = query; |
| 44 | buffer.return_pointer = reinterpret_cast<std::uint64_t>(value); |
| 45 | buffer.return_size = sizeof(*value); |
| 46 | |
| 47 | return ioctl(deviceFD, DRM_IOCTL_AMDGPU_INFO, &buffer) >= 0; |
| 48 | #else |
| 49 | return false; |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | bool readAMDGPUVRamSize(int deviceFD, units::data::megabyte_t *size); |
| 54 | |
| 55 | template<typename Data> |
| 56 | bool readRadeonInfoSensor(int deviceFD, Data *value, std::uint32_t sensor) |
| 57 | { |
| 58 | #if defined(DRM_IOCTL_RADEON_INFO) |
| 59 | struct drm_radeon_info buffer = {}; |
| 60 | |
| 61 | buffer.request = sensor; |
| 62 | buffer.value = reinterpret_cast<std::uint64_t>(value); |
| 63 | |
| 64 | return ioctl(deviceFD, DRM_IOCTL_RADEON_INFO, &buffer) >= 0; |
| 65 | #else |
| 66 | return false; |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | bool readRadeonVRamSize(int deviceFD, units::data::megabyte_t *size); |
| 71 | |
| 72 | /// Returns DPM states clocks. (4.6+) |
| 73 | /// @param ppDpmLines pp_dpm_sclk / pp_dpm_mclk data source contents |
| 74 | std::optional<std::vector<std::pair<unsigned int, units::frequency::megahertz_t>>> |
| 75 | parseDPMStates(std::vector<std::string> const &ppDpmLines); |
| 76 | |
| 77 | /// Returns the current state index from pp_dpm_sclk / pp_dpm_mclk. (4.6+) |
| 78 | /// @param ppDpmLines pp_dpm_sclk / pp_dpm_mclk data source contents |
| 79 | std::optional<unsigned int> |
| 80 | parseDPMCurrentStateIndex(std::vector<std::string> const &ppDpmLines); |
| 81 | |
| 82 | /// Returns power profile modes. (4.17+) |
| 83 | /// NOTE For use with non-columnar data only. |
| 84 | /// @param ppPowerProfileModeLines pp_power_profile_mode data source contents |
| 85 | std::optional<std::vector<std::pair<std::string, int>>> |
| 86 | parsePowerProfileModeModes(std::vector<std::string> const &ppPowerProfileModeLines); |
| 87 | |
| 88 | /// Returns current power profile mode index. (4.17+) |
| 89 | /// NOTE For use with non-columnar data only. |
| 90 | /// @param ppPowerProfileModeLines pp_power_profile_mode data source contents |
| 91 | std::optional<int> parsePowerProfileModeCurrentModeIndex( |
| 92 | std::vector<std::string> const &ppPowerProfileModeLines); |
| 93 | |
| 94 | /// Returns power profile modes on ASICs with columnar data. (6.11+) |
| 95 | /// @param ppPowerProfileModeLines pp_power_profile_mode data source contents |
| 96 | std::optional<std::vector<std::pair<std::string, int>>> |
| 97 | parsePowerProfileModeModesColumnar( |
| 98 | std::vector<std::string> const &ppPowerProfileModeLines); |
| 99 | |
| 100 | /// Returns current power profile mode index on ASICs with columnar data. (6.11+) |
| 101 | /// @param ppPowerProfileModeLines pp_power_profile_mode data source contents |
| 102 | std::optional<int> parsePowerProfileModeCurrentModeIndexColumnar( |
| 103 | std::vector<std::string> const &ppPowerProfileModeLines); |
| 104 | |
| 105 | /// Returns the available clock + voltage states for a control. (4.17+) |
| 106 | /// @param controlName name of the control |
| 107 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 108 | std::optional<std::vector<std::tuple<unsigned int, units::frequency::megahertz_t, |
| 109 | units::voltage::millivolt_t>>> |
| 110 | parseOverdriveClksVolts(std::string_view controlName, |
| 111 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 112 | |
| 113 | /// Returns the state's clock range for a control. (4.18+) |
| 114 | /// @param controlName name of the control |
| 115 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 116 | std::optional<std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>> |
| 117 | parseOverdriveClkRange(std::string_view controlName, |
| 118 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 119 | |
| 120 | /// Returns the state's voltage range. (4.18+) |
| 121 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 122 | std::optional<std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>> |
| 123 | parseOverdriveVoltRange(std::vector<std::string> const &ppOdClkVoltageLines); |
| 124 | |
| 125 | /// Returns the available clock states for a control. (4.20+, vega20+) |
| 126 | /// @param controlName name of the control |
| 127 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 128 | std::optional<std::vector<std::pair<unsigned int, units::frequency::megahertz_t>>> |
| 129 | parseOverdriveClks(std::string_view controlName, |
| 130 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 131 | |
| 132 | /// Returns the voltage curve points for curve voltage ASICs. (4.20+, vega20+) |
| 133 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 134 | std::optional<std::vector< |
| 135 | std::pair<units::frequency::megahertz_t, units::voltage::millivolt_t>>> |
| 136 | parseOverdriveVoltCurve(std::vector<std::string> const &ppOdClkVoltageLines); |
| 137 | |
| 138 | /// Returns voltage range of curve points for curve voltage ASICs. (4.20+, vega20+) |
| 139 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 140 | std::optional<std::vector<std::pair< |
| 141 | std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>, |
| 142 | std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>>>> |
| 143 | parseOverdriveVoltCurveRange(std::vector<std::string> const &ppOdClkVoltageLines); |
| 144 | |
| 145 | /// Returns the voltage offset. |
| 146 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 147 | std::optional<units::voltage::millivolt_t> |
| 148 | parseOverdriveVoltOffset(std::vector<std::string> const &ppOdClkVoltageLines); |
| 149 | |
| 150 | /// Returns voltage offset range. (6.14+) |
| 151 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 152 | std::optional<std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>> |
| 153 | parseOverdriveVoltOffsetRange(std::vector<std::string> const &ppOdClkVoltageLines); |
| 154 | |
| 155 | /// Returns a list containing the name of the available CLK controls. |
| 156 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 157 | std::optional<std::vector<std::string>> |
| 158 | parseOverdriveClkControls(std::vector<std::string> const &ppOdClkVoltageLines); |
| 159 | |
| 160 | /// Translates a CLK control name to the control commit command id. |
| 161 | /// @param controlName name of the control |
| 162 | /// @returns commit command id |
| 163 | std::optional<std::string> |
| 164 | getOverdriveClkControlCmdId(std::string_view controlName); |
| 165 | |
| 166 | /// Translates a CLK offset control name to the control commit command id. |
| 167 | /// @param controlName name of the control |
| 168 | /// @returns commit command id |
| 169 | std::optional<std::string> |
| 170 | getOverdriveClkOffsetControlCmdId(std::string_view controlName); |
| 171 | |
| 172 | /// Returns a list containing the name of the available CLK offset controls (6.14+). |
| 173 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 174 | std::optional<std::vector<std::string>> parseOverdriveClkOffsetControls( |
| 175 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 176 | |
| 177 | /// Returns the frequency offset value of a control. (6.14+) |
| 178 | /// @param controlName name of the control |
| 179 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 180 | std::optional<units::frequency::megahertz_t> |
| 181 | parseOverdriveClkOffset(std::string_view controlName, |
| 182 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 183 | |
| 184 | /// Returns the frequency offset range of a control. (6.14+) |
| 185 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 186 | std::optional<std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>> |
| 187 | parseOverdriveClkOffsetRange(std::string_view controlName, |
| 188 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 189 | |
| 190 | /// Returns a list containing the indices of all out of range states present on |
| 191 | /// pp_od_clk_voltage for the given control name. |
| 192 | /// @param controlName name of the control |
| 193 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 194 | std::optional<std::vector<unsigned int>> ppOdClkVoltageFreqRangeOutOfRangeStates( |
| 195 | std::string const &controlName, |
| 196 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 197 | |
| 198 | /// Returns whether pp_od_clk_voltage has known frequency + voltage control quirks. |
| 199 | /// @param controlName name of the control |
| 200 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 201 | bool ppOdClkVoltageHasKnownFreqVoltQuirks( |
| 202 | std::string const &controlName, |
| 203 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 204 | |
| 205 | /// Returns whether pp_od_clk_voltage has known voltage curve control quirks. |
| 206 | /// @param ppOdClkVoltageLines pp_od_clk_voltage data source contents |
| 207 | bool ppOdClkVoltageHasKnownVoltCurveQuirks( |
| 208 | std::vector<std::string> const &ppOdClkVoltageLines); |
| 209 | |
| 210 | /// Returns the fan curve points for the overdrive based fan control. (6.7+, RDNA3+) |
| 211 | /// @param fanCurveLines fan_curve data source contents |
| 212 | std::optional<std::vector<std::tuple<unsigned int, units::temperature::celsius_t, |
| 213 | units::concentration::percent_t>>> |
| 214 | parseOverdriveFanCurve(std::vector<std::string> const &fanCurveLines); |
| 215 | |
| 216 | /// Returns the fan curve temperature range for the overdrive based fan control. (6.7+, RDNA3+) |
| 217 | /// @param fanCurveLines fan_curve data source contents |
| 218 | std::optional<std::pair<units::temperature::celsius_t, units::temperature::celsius_t>> |
| 219 | parseOverdriveFanCurveTempRange(std::vector<std::string> const &fanCurveLines); |
| 220 | |
| 221 | /// Returns the fan curve speed range for the overdrive based fan control. (6.7+, RDNA3+) |
| 222 | /// @param fanCurveLines fan_curve data source contents |
| 223 | std::optional< |
| 224 | std::pair<units::concentration::percent_t, units::concentration::percent_t>> |
| 225 | parseOverdriveFanCurveSpeedRange(std::vector<std::string> const &fanCurveLines); |
| 226 | |
| 227 | /// Returns the fan stop value for the overdrive based fan control. (6.13+, RDNA3+) |
| 228 | /// @param fanStopLines fan_zero_rpm_enable data source contents |
| 229 | std::optional<bool> |
| 230 | parseOverdriveFanStop(std::vector<std::string> const &fanStopLines); |
| 231 | |
| 232 | /// Returns the fan stop temperature value for the overdrive based fan control. (6.13+, RDNA3+) |
| 233 | /// @param fanStopTempLines fan_zero_rpm_stop_temperature data source contents |
| 234 | std::optional<units::temperature::celsius_t> |
| 235 | parseOverdriveFanStopTemp(std::vector<std::string> const &fanStopTempLines); |
| 236 | |
| 237 | /// Returns the fan stop temperature range for the overdrive based fan control. (6.13+, RDNA3+) |
| 238 | /// @param fanStopTempLines fan_zero_rpm_stop_temperature data source contents |
| 239 | std::optional<std::pair<units::temperature::celsius_t, units::temperature::celsius_t>> |
| 240 | parseOverdriveFanStopTempRange(std::vector<std::string> const &fanStopTempLines); |
| 241 | |
| 242 | /// Returns true when pp_power_profile_mode data has columnar format. |
| 243 | bool isPowerProfileModeDataColumnar(std::vector<std::string> const &data); |
| 244 | |
| 245 | /// Returns true when overdrive clock + voltage state control is available. |
| 246 | bool hasOverdriveClkVoltControl(std::vector<std::string> const &data); |
| 247 | |
| 248 | /// Returns true when overdrive clock control is available. |
| 249 | bool hasOverdriveClkControl(std::vector<std::string> const &data); |
| 250 | |
| 251 | /// Returns true when overdrive clock offset control is available. |
| 252 | bool hasOverdriveClkOffsetControl(std::vector<std::string> const &data); |
| 253 | |
| 254 | /// Returns true when overdrive voltage curve control is available. |
| 255 | bool hasOverdriveVoltCurveControl(std::vector<std::string> const &data); |
| 256 | |
| 257 | /// Returns true when overdrive voltage offset control is available. |
| 258 | bool hasOverdriveVoltOffsetControl(std::vector<std::string> const &data); |
| 259 | |
| 260 | /// Returns true when overdrive fan target temperature control is available. |
| 261 | bool hasOverdriveFanTargetTempControl(std::vector<std::string> const &data); |
| 262 | |
| 263 | /// Returns true when overdrive fan minimum pwm control is available. |
| 264 | bool hasOverdriveFanMinimumPWMControl(std::vector<std::string> const &data); |
| 265 | |
| 266 | /// Returns true when overdrive fan acoustic target rpm threshold control is |
| 267 | /// available. |
| 268 | bool hasOverdriveFanAcousticTargetControl(std::vector<std::string> const &data); |
| 269 | |
| 270 | /// Returns true when overdrive fan acoustic limit rpm threshold control is |
| 271 | /// available. |
| 272 | bool hasOverdriveFanAcousticLimitControl(std::vector<std::string> const &data); |
| 273 | |
| 274 | } // namespace Utils::AMD |