| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "amdutils.h" |
| 5 | |
| 6 | #include "common/stringutils.h" |
| 7 | #include <algorithm> |
| 8 | #include <cstddef> |
| 9 | #include <iterator> |
| 10 | #include <regex> |
| 11 | #include <units.h> |
| 12 | #include <unordered_map> |
| 13 | #include <utility> |
| 14 | |
| 15 | namespace Utils::AMD { |
| 16 | |
| 17 | bool readAMDGPUVRamSize(int deviceFD, units::data::megabyte_t *size) |
| 18 | { |
| 19 | #if defined(DRM_IOCTL_AMDGPU_INFO) && defined(AMDGPU_INFO_MEMORY) |
| 20 | struct drm_amdgpu_memory_info drm_info = {}; |
| 21 | struct drm_amdgpu_info buffer = {}; |
| 22 | |
| 23 | buffer.query = AMDGPU_INFO_MEMORY; |
| 24 | buffer.return_pointer = reinterpret_cast<std::uint64_t>(&drm_info); |
| 25 | buffer.return_size = sizeof(drm_info); |
| 26 | |
| 27 | if (ioctl(deviceFD, DRM_IOCTL_AMDGPU_INFO, &buffer) >= 0) { |
| 28 | *size = units::make_unit<units::data::megabyte_t>( |
| 29 | drm_info.vram.total_heap_size / (1024 * 1024)); |
| 30 | return true; |
| 31 | } |
| 32 | else |
| 33 | return false; |
| 34 | #else |
| 35 | return false; |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | bool readRadeonVRamSize(int deviceFD, units::data::megabyte_t *size) |
| 40 | { |
| 41 | #if defined(DRM_IOCTL_RADEON_GEM_INFO) |
| 42 | struct drm_radeon_gem_info buffer = {}; |
| 43 | if (ioctl(deviceFD, DRM_IOCTL_RADEON_GEM_INFO, &buffer) >= 0) { |
| 44 | *size = units::make_unit<units::data::megabyte_t>(buffer.vram_size / |
| 45 | (1024 * 1024)); |
| 46 | return true; |
| 47 | } |
| 48 | else |
| 49 | return false; |
| 50 | #else |
| 51 | return false; |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | std::optional<std::vector<std::pair<unsigned int, units::frequency::megahertz_t>>> |
| 56 | parseDPMStates(std::vector<std::string> const &ppDpmLines) |
| 57 | { |
| 58 | // Relevant lines format (kernel 4.6+) |
| 59 | // 0: 300Mhz * |
| 60 | // ... |
| 61 | // N: 1303Mhz |
| 62 | std::regex const regex(R"(^(\d+)\s*:\s*(\d+)\s*Mhz\s*\*?\s*$)", |
| 63 | std::regex::icase); |
| 64 | std::vector<std::pair<unsigned int, units::frequency::megahertz_t>> states; |
| 65 | |
| 66 | for (auto const &line : ppDpmLines) { |
| 67 | std::smatch result; |
| 68 | if (!std::regex_search(line, result, regex)) |
| 69 | return {}; |
| 70 | |
| 71 | unsigned int index{0}, freq{0}; |
| 72 | if (!(Utils::String::toNumber<unsigned int>(index, result[1]) && |
| 73 | Utils::String::toNumber<unsigned int>(freq, result[2]))) |
| 74 | return {}; |
| 75 | |
| 76 | states.emplace_back(index, units::frequency::megahertz_t(freq)); |
| 77 | } |
| 78 | |
| 79 | if (states.empty()) |
| 80 | return {}; |
| 81 | |
| 82 | return std::move(states); |
| 83 | } |
| 84 | |
| 85 | std::optional<unsigned int> |
| 86 | parseDPMCurrentStateIndex(std::vector<std::string> const &ppDpmLines) |
| 87 | { |
| 88 | // Relevant lines format (kernel 4.6+) |
| 89 | // 0: 300Mhz * |
| 90 | // ... |
| 91 | // N: 1303Mhz |
| 92 | // |
| 93 | // '*' marks the current state |
| 94 | std::regex const regex(R"(^(\d+)\s*:\s*\d+\s*Mhz\s*\*\s*$)", std::regex::icase); |
| 95 | |
| 96 | for (auto const &line : ppDpmLines) { |
| 97 | std::smatch result; |
| 98 | if (std::regex_search(line, result, regex)) { |
| 99 | unsigned int index{0}; |
| 100 | if (!Utils::String::toNumber<unsigned int>(index, result[1])) |
| 101 | return {}; |
| 102 | |
| 103 | return index; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return {}; |
| 108 | } |
| 109 | |
| 110 | std::optional<std::vector<std::pair<std::string, int>>> |
| 111 | parsePowerProfileModeModes(std::vector<std::string> const &ppPowerProfileModeLines) |
| 112 | { |
| 113 | // Relevant lines format: |
| 114 | // 1 3D_FULL_SCREEN *: ... |
| 115 | // 1 3D_FULL_SCREEN: ... |
| 116 | // 1 3D_FULL_SCREEN*: ... |
| 117 | // 1 3D_FULL_SCREEN : ... |
| 118 | // 1 3D_FULL_SCREEN* |
| 119 | // 1 3D_FULL_SCREEN |
| 120 | std::regex const regex(R"(^\s*(\d+)\s+([^\*\(\s:]+))"); |
| 121 | std::vector<std::pair<std::string, int>> modes; |
| 122 | |
| 123 | for (auto const &line : ppPowerProfileModeLines) { |
| 124 | |
| 125 | std::smatch result; |
| 126 | if (!std::regex_search(line, result, regex)) |
| 127 | continue; |
| 128 | |
| 129 | // skip BOOT and CUSTOM modes |
| 130 | std::string const mode(result[2]); |
| 131 | if (mode.find("BOOT") != std::string::npos || |
| 132 | mode.find("CUSTOM") != std::string::npos) |
| 133 | continue; |
| 134 | |
| 135 | int index{0}; |
| 136 | if (!Utils::String::toNumber<int>(index, result[1])) |
| 137 | continue; |
| 138 | |
| 139 | modes.emplace_back(std::move(mode), index); |
| 140 | } |
| 141 | |
| 142 | if (!modes.empty()) |
| 143 | return std::move(modes); |
| 144 | |
| 145 | return {}; |
| 146 | } |
| 147 | |
| 148 | std::optional<int> parsePowerProfileModeCurrentModeIndex( |
| 149 | std::vector<std::string> const &ppPowerProfileModeLines) |
| 150 | { |
| 151 | // Relevant lines format: |
| 152 | // 1 3D_FULL_SCREEN *: ... |
| 153 | // 1 3D_FULL_SCREEN*: ... |
| 154 | // 1 3D_FULL_SCREEN* |
| 155 | std::regex const regex(R"(^\s*(\d+)\s+(?:[^\*\(\s]+)\s*\*)"); |
| 156 | |
| 157 | for (auto const &line : ppPowerProfileModeLines) { |
| 158 | |
| 159 | std::smatch result; |
| 160 | if (!std::regex_search(line, result, regex)) |
| 161 | continue; |
| 162 | |
| 163 | int index{0}; |
| 164 | if (!Utils::String::toNumber<int>(index, result[1])) |
| 165 | break; |
| 166 | |
| 167 | return index; |
| 168 | } |
| 169 | |
| 170 | return {}; |
| 171 | } |
| 172 | |
| 173 | std::optional<std::vector<std::pair<std::string, int>>> |
| 174 | parsePowerProfileModeModesColumnar( |
| 175 | std::vector<std::string> const &ppPowerProfileModeLines) |
| 176 | { |
| 177 | // Format of the first line: |
| 178 | // 0 BOOTUP_DEFAULT* 1 3D_FULL_SCREEN 2 POWER_SAVING 3 VIDEO ... |
| 179 | if (ppPowerProfileModeLines.empty()) |
| 180 | return {}; |
| 181 | |
| 182 | auto const &data = ppPowerProfileModeLines.front(); |
| 183 | |
| 184 | std::regex const regex(R"(\s*(\d+)\s+(\w+)\s*\*{0,1})", std::regex::icase); |
| 185 | std::vector<std::pair<std::string, int>> modes; |
| 186 | |
| 187 | auto targetIt = std::sregex_iterator(data.cbegin(), data.cend(), regex); |
| 188 | while (targetIt != std::sregex_iterator()) { |
| 189 | std::smatch match = *targetIt; |
| 190 | |
| 191 | // skip BOOT and CUSTOM modes |
| 192 | std::string const mode(match[2]); |
| 193 | if (mode.find("BOOT") != std::string::npos || |
| 194 | mode.find("CUSTOM") != std::string::npos) { |
| 195 | targetIt = std::next(targetIt); |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | int index{0}; |
| 200 | if (!Utils::String::toNumber<int>(index, match[1])) { |
| 201 | targetIt = std::next(targetIt); |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | modes.emplace_back(std::move(mode), index); |
| 206 | targetIt = std::next(targetIt); |
| 207 | } |
| 208 | |
| 209 | if (!modes.empty()) |
| 210 | return std::move(modes); |
| 211 | |
| 212 | return {}; |
| 213 | } |
| 214 | |
| 215 | std::optional<int> parsePowerProfileModeCurrentModeIndexColumnar( |
| 216 | std::vector<std::string> const &ppPowerProfileModeLines) |
| 217 | { |
| 218 | // Format of the first line: |
| 219 | // 0 BOOTUP_DEFAULT* 1 3D_FULL_SCREEN 2 POWER_SAVING 3 VIDEO ... |
| 220 | if (ppPowerProfileModeLines.empty()) |
| 221 | return {}; |
| 222 | |
| 223 | auto const &data = ppPowerProfileModeLines.front(); |
| 224 | |
| 225 | std::regex const regex(R"(\s*(\d+)\s+\w+\s*\*)", std::regex::icase); |
| 226 | std::smatch result; |
| 227 | if (!std::regex_search(data, result, regex)) |
| 228 | return {}; |
| 229 | |
| 230 | int index{0}; |
| 231 | if (!Utils::String::toNumber<int>(index, result[1])) |
| 232 | return {}; |
| 233 | |
| 234 | return index; |
| 235 | } |
| 236 | |
| 237 | std::optional<std::tuple<unsigned int, units::frequency::megahertz_t, |
| 238 | units::voltage::millivolt_t>> |
| 239 | parseOverdriveClkVoltLine(std::string const &line) |
| 240 | { |
| 241 | // Relevant lines format (kernel 4.17+): |
| 242 | // ... |
| 243 | // 0: 300MHz 800mV |
| 244 | // |
| 245 | // On Navi ASICs: |
| 246 | // ... |
| 247 | // 0: 300MHz @ 800mV |
| 248 | // ... |
| 249 | std::regex const regex(R"((\d+)\s*:\s*(\d+)\s*MHz[\s@]*(\d+)\s*mV\s*$)", |
| 250 | std::regex::icase); |
| 251 | std::smatch result; |
| 252 | |
| 253 | if (std::regex_search(line, result, regex)) { |
| 254 | unsigned int index{0}, freq{0}, volt{0}; |
| 255 | if (Utils::String::toNumber<unsigned int>(index, result[1]) && |
| 256 | Utils::String::toNumber<unsigned int>(freq, result[2]) && |
| 257 | Utils::String::toNumber<unsigned int>(volt, result[3])) |
| 258 | return std::make_tuple(index, units::frequency::megahertz_t(freq), |
| 259 | units::voltage::millivolt_t(volt)); |
| 260 | } |
| 261 | |
| 262 | return {}; |
| 263 | } |
| 264 | |
| 265 | std::optional<std::vector<std::tuple<unsigned int, units::frequency::megahertz_t, |
| 266 | units::voltage::millivolt_t>>> |
| 267 | parseOverdriveClksVolts(std::string_view controlName, |
| 268 | std::vector<std::string> const &ppOdClkVoltageLines) |
| 269 | { |
| 270 | // Relevant lines format (kernel 4.17+): |
| 271 | // ... |
| 272 | // OD_controlName: |
| 273 | // ... |
| 274 | // OD_otherLbl: |
| 275 | // ... |
| 276 | auto targetIt = std::find_if( |
| 277 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 278 | [&](std::string const &line) { |
| 279 | return line.find("OD_" + std::string(controlName) + ":") != |
| 280 | std::string::npos; |
| 281 | }); |
| 282 | if (targetIt != ppOdClkVoltageLines.cend() && |
| 283 | std::next(targetIt) != ppOdClkVoltageLines.cend()) { |
| 284 | targetIt = std::next(targetIt); |
| 285 | |
| 286 | auto endIt = std::find_if(targetIt, ppOdClkVoltageLines.cend(), |
| 287 | [&](std::string const &line) { |
| 288 | return line.find("OD_") != std::string::npos; |
| 289 | }); |
| 290 | |
| 291 | std::vector<std::tuple<unsigned int, units::frequency::megahertz_t, |
| 292 | units::voltage::millivolt_t>> |
| 293 | states; |
| 294 | |
| 295 | while (targetIt != endIt) { |
| 296 | auto state = parseOverdriveClkVoltLine(*targetIt); |
| 297 | if (state.has_value()) |
| 298 | states.emplace_back(std::move(*state)); |
| 299 | |
| 300 | targetIt = std::next(targetIt); |
| 301 | } |
| 302 | |
| 303 | return std::move(states); |
| 304 | } |
| 305 | |
| 306 | return {}; |
| 307 | } |
| 308 | |
| 309 | std::optional<std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>> |
| 310 | parseOverdriveClkRange(std::string const &line) |
| 311 | { |
| 312 | // Relevant lines format (kernel 4.18+): |
| 313 | // ... |
| 314 | // Lbl...: 400MHz 500MHz |
| 315 | // ... |
| 316 | std::regex const regex(R"(^(?:[^\:\s]+)\s*:\s*(\d+)\s*MHz\s*(\d+)\s*MHz\s*$)", |
| 317 | std::regex::icase); |
| 318 | std::smatch result; |
| 319 | |
| 320 | if (std::regex_search(line, result, regex)) { |
| 321 | int min{0}, max{0}; |
| 322 | if (Utils::String::toNumber<int>(min, result[1]) && |
| 323 | Utils::String::toNumber<int>(max, result[2])) |
| 324 | return std::make_pair(units::make_unit<units::frequency::megahertz_t>(min), |
| 325 | units::make_unit<units::frequency::megahertz_t>(max)); |
| 326 | } |
| 327 | |
| 328 | return {}; |
| 329 | } |
| 330 | |
| 331 | std::optional<std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>> |
| 332 | parseOverdriveClkRange(std::string_view controlName, |
| 333 | std::vector<std::string> const &ppOdClkVoltageLines) |
| 334 | { |
| 335 | // Relevant lines format (kernel 4.18+): |
| 336 | // ... |
| 337 | // OD_RANGE: |
| 338 | // ... |
| 339 | // controlName: min max |
| 340 | // ... |
| 341 | auto rangeIt = std::find_if( |
| 342 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 343 | [&](std::string const &line) { |
| 344 | return line.find("OD_RANGE:") != std::string::npos; |
| 345 | }); |
| 346 | if (rangeIt != ppOdClkVoltageLines.cend()) { |
| 347 | auto targetIt = std::find_if( |
| 348 | rangeIt, ppOdClkVoltageLines.cend(), [&](std::string const &line) { |
| 349 | return line.find(std::string(controlName) + ":") != std::string::npos; |
| 350 | }); |
| 351 | |
| 352 | if (targetIt != ppOdClkVoltageLines.cend()) |
| 353 | return parseOverdriveClkRange(*targetIt); |
| 354 | } |
| 355 | |
| 356 | return {}; |
| 357 | } |
| 358 | |
| 359 | std::optional<std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>> |
| 360 | parseOverdriveVoltRangeLine(std::string const &line) |
| 361 | { |
| 362 | // Relevant lines format (kernel 4.18+): |
| 363 | // ... |
| 364 | // Lbl...: 400mV 500mV |
| 365 | // ... |
| 366 | std::regex const regex(R"(^(?:[^\:\s]+)\s*:\s*(\d+)\s*mV\s*(\d+)\s*mV\s*$)", |
| 367 | std::regex::icase); |
| 368 | std::smatch result; |
| 369 | |
| 370 | if (std::regex_search(line, result, regex)) { |
| 371 | int min{0}, max{0}; |
| 372 | if (Utils::String::toNumber<int>(min, result[1]) && |
| 373 | Utils::String::toNumber<int>(max, result[2])) |
| 374 | return std::make_pair(units::make_unit<units::voltage::millivolt_t>(min), |
| 375 | units::make_unit<units::voltage::millivolt_t>(max)); |
| 376 | } |
| 377 | |
| 378 | return {}; |
| 379 | } |
| 380 | |
| 381 | std::optional<std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>> |
| 382 | parseOverdriveVoltRange(std::vector<std::string> const &ppOdClkVoltageLines) |
| 383 | { |
| 384 | // Relevant lines format (kernel 4.18+): |
| 385 | // ... |
| 386 | // OD_RANGE: |
| 387 | // ... |
| 388 | // VDDC: min max |
| 389 | // ... |
| 390 | auto rangeIt = std::find_if( |
| 391 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 392 | [&](std::string const &line) { |
| 393 | return line.find("OD_RANGE:") != std::string::npos; |
| 394 | }); |
| 395 | if (rangeIt != ppOdClkVoltageLines.cend()) { |
| 396 | auto targetIt = std::find_if( |
| 397 | rangeIt, ppOdClkVoltageLines.cend(), [&](std::string const &line) { |
| 398 | return line.find("VDDC:") != std::string::npos; |
| 399 | }); |
| 400 | |
| 401 | if (targetIt != ppOdClkVoltageLines.cend()) |
| 402 | return parseOverdriveVoltRangeLine(*targetIt); |
| 403 | } |
| 404 | |
| 405 | return {}; |
| 406 | } |
| 407 | |
| 408 | std::optional<std::pair<unsigned int, units::frequency::megahertz_t>> |
| 409 | parseOverdriveClksLine(std::string const &line) |
| 410 | { |
| 411 | // Relevant lines format (kernel 4.20+): |
| 412 | // ... |
| 413 | // 0: 300MHz |
| 414 | // ... |
| 415 | std::regex const regex(R"(^(\d+)\s*:\s*(\d+)\s*MHz\s*$)", std::regex::icase); |
| 416 | std::smatch result; |
| 417 | |
| 418 | if (std::regex_search(line, result, regex)) { |
| 419 | unsigned int index{0}, freq{0}; |
| 420 | if (Utils::String::toNumber<unsigned int>(index, result[1]) && |
| 421 | Utils::String::toNumber<unsigned int>(freq, result[2])) |
| 422 | return std::make_pair(index, units::frequency::megahertz_t(freq)); |
| 423 | } |
| 424 | |
| 425 | return {}; |
| 426 | } |
| 427 | |
| 428 | std::optional<std::vector<std::pair<unsigned int, units::frequency::megahertz_t>>> |
| 429 | parseOverdriveClks(std::string_view controlName, |
| 430 | std::vector<std::string> const &ppOdClkVoltageLines) |
| 431 | { |
| 432 | // Relevant lines format (kernel 4.17+): |
| 433 | // ... |
| 434 | // OD_controlName: |
| 435 | // ... |
| 436 | // OD_otherLbl: |
| 437 | // ... |
| 438 | auto targetIt = std::find_if( |
| 439 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 440 | [&](std::string const &line) { |
| 441 | return line.find("OD_" + std::string(controlName) + ":") != |
| 442 | std::string::npos; |
| 443 | }); |
| 444 | if (targetIt != ppOdClkVoltageLines.cend() && |
| 445 | std::next(targetIt) != ppOdClkVoltageLines.cend()) { |
| 446 | targetIt = std::next(targetIt); |
| 447 | |
| 448 | auto endIt = std::find_if(targetIt, ppOdClkVoltageLines.cend(), |
| 449 | [&](std::string const &line) { |
| 450 | return line.find("OD_") != std::string::npos; |
| 451 | }); |
| 452 | |
| 453 | std::vector<std::pair<unsigned int, units::frequency::megahertz_t>> states; |
| 454 | |
| 455 | while (targetIt != endIt) { |
| 456 | auto state = parseOverdriveClksLine(*targetIt); |
| 457 | if (state.has_value()) |
| 458 | states.emplace_back(std::move(*state)); |
| 459 | |
| 460 | targetIt = std::next(targetIt); |
| 461 | } |
| 462 | |
| 463 | return std::move(states); |
| 464 | } |
| 465 | |
| 466 | return {}; |
| 467 | } |
| 468 | |
| 469 | std::optional<std::vector< |
| 470 | std::pair<units::frequency::megahertz_t, units::voltage::millivolt_t>>> |
| 471 | parseOverdriveVoltCurve(std::vector<std::string> const &ppOdClkVoltageLines) |
| 472 | { |
| 473 | // Relevant lines format (kernel 4.20+): |
| 474 | // ... |
| 475 | // OD_VDDC_CURVE: |
| 476 | // 0: 700Mhz 800mV |
| 477 | // ... |
| 478 | // OD_RANGE: |
| 479 | auto targetIt = std::find_if( |
| 480 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 481 | [&](std::string const &line) { |
| 482 | return line.find("OD_VDDC_CURVE:") != std::string::npos; |
| 483 | }); |
| 484 | if (targetIt != ppOdClkVoltageLines.cend() && |
| 485 | std::next(targetIt) != ppOdClkVoltageLines.cend()) { |
| 486 | targetIt = std::next(targetIt); |
| 487 | |
| 488 | auto endIt = std::find_if(targetIt, ppOdClkVoltageLines.cend(), |
| 489 | [&](std::string const &line) { |
| 490 | return line.find("OD_") != std::string::npos; |
| 491 | }); |
| 492 | |
| 493 | std::vector<std::pair<units::frequency::megahertz_t, units::voltage::millivolt_t>> |
| 494 | points; |
| 495 | |
| 496 | while (targetIt != endIt) { |
| 497 | auto state = parseOverdriveClkVoltLine(*targetIt); |
| 498 | if (state.has_value()) { |
| 499 | auto &[_, freq, volt] = *state; |
| 500 | points.emplace_back(std::make_pair(freq, volt)); |
| 501 | } |
| 502 | |
| 503 | targetIt = std::next(targetIt); |
| 504 | } |
| 505 | |
| 506 | if (!points.empty()) |
| 507 | return std::move(points); |
| 508 | } |
| 509 | |
| 510 | return {}; |
| 511 | } |
| 512 | |
| 513 | std::optional<std::vector<std::pair< |
| 514 | std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>, |
| 515 | std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>>>> |
| 516 | parseOverdriveVoltCurveRange(std::vector<std::string> const &ppOdClkVoltageLines) |
| 517 | { |
| 518 | // Relevant lines format (kernel 4.20+): |
| 519 | // ... |
| 520 | // OD_RANGE: |
| 521 | // ... |
| 522 | // VDDC_CURVE_SCLK[0]: 808Mhz 2200Mhz |
| 523 | // VDDC_CURVE_VOLT[0]: 738mV 1218mV |
| 524 | // ... |
| 525 | // VDDC_CURVE_SCLK[N]: 808Mhz 2200Mhz |
| 526 | // VDDC_CURVE_VOLT[N]: 738mV 1218mV |
| 527 | // ... |
| 528 | auto rangeIt = std::find_if( |
| 529 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 530 | [&](std::string const &line) { |
| 531 | return line.find("OD_RANGE:") != std::string::npos; |
| 532 | }); |
| 533 | if (rangeIt != ppOdClkVoltageLines.cend()) { |
| 534 | |
| 535 | std::vector<std::pair< |
| 536 | std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>, |
| 537 | std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>>> |
| 538 | ranges; |
| 539 | |
| 540 | auto freqIt = std::next(rangeIt); |
| 541 | |
| 542 | // skip lines not starting with VDDC_CURVE_ |
| 543 | freqIt = std::find_if( |
| 544 | freqIt, ppOdClkVoltageLines.cend(), [&](std::string const &line) { |
| 545 | return line.find("VDDC_CURVE_") != std::string::npos; |
| 546 | }); |
| 547 | |
| 548 | while (freqIt != ppOdClkVoltageLines.cend() && |
| 549 | (*freqIt).find("VDDC_CURVE_SCLK[") != std::string::npos) { |
| 550 | |
| 551 | auto voltIt = std::next(freqIt); |
| 552 | if (voltIt != ppOdClkVoltageLines.cend() && |
| 553 | (*voltIt).find("VDDC_CURVE_VOLT[") != std::string::npos) { |
| 554 | |
| 555 | auto freqRange = parseOverdriveClkRange(*freqIt); |
| 556 | auto voltRange = parseOverdriveVoltRangeLine(*voltIt); |
| 557 | |
| 558 | if (freqRange.has_value() && voltRange.has_value()) |
| 559 | ranges.emplace_back( |
| 560 | std::make_pair(std::move(*freqRange), std::move(*voltRange))); |
| 561 | else |
| 562 | return {}; // invalid data format |
| 563 | } |
| 564 | else |
| 565 | return {}; // invalid data format |
| 566 | |
| 567 | freqIt = std::next(voltIt); |
| 568 | } |
| 569 | |
| 570 | if (!ranges.empty()) |
| 571 | return std::move(ranges); |
| 572 | } |
| 573 | |
| 574 | return {}; |
| 575 | } |
| 576 | |
| 577 | std::optional<units::voltage::millivolt_t> |
| 578 | parseOverdriveVoltOffset(std::vector<std::string> const &ppOdClkVoltageLines) |
| 579 | { |
| 580 | auto targetIt = std::find_if( |
| 581 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 582 | [&](std::string const &line) { |
| 583 | return line.find("OD_VDDGFX_OFFSET:") != std::string::npos; |
| 584 | }); |
| 585 | if (targetIt != ppOdClkVoltageLines.cend() && |
| 586 | std::next(targetIt) != ppOdClkVoltageLines.cend()) { |
| 587 | std::regex const regex(R"(^(-?\d+)\s*mV\s*$)", std::regex::icase); |
| 588 | |
| 589 | std::smatch result; |
| 590 | if (std::regex_search(*std::next(targetIt), result, regex)) { |
| 591 | int value; |
| 592 | if (Utils::String::toNumber(value, result[1])) |
| 593 | return units::voltage::millivolt_t(value); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | return {}; |
| 598 | } |
| 599 | |
| 600 | std::optional<std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>> |
| 601 | parseOverdriveVoltOffsetRangeLine(std::string const &line) |
| 602 | { |
| 603 | // Relevant lines format (kernel 6.14+): |
| 604 | // ... |
| 605 | // label: -200mv 0mv |
| 606 | // ... |
| 607 | std::regex const regex(R"(^(?:[^\:\s]+)\s*:\s*(-?\d+)\s*mV\s*(\d+)\s*mV\s*$)", |
| 608 | std::regex::icase); |
| 609 | std::smatch result; |
| 610 | |
| 611 | if (std::regex_search(line, result, regex)) { |
| 612 | int min{0}, max{0}; |
| 613 | if (Utils::String::toNumber<int>(min, result[1]) && |
| 614 | Utils::String::toNumber<int>(max, result[2])) |
| 615 | return std::make_pair(units::make_unit<units::voltage::millivolt_t>(min), |
| 616 | units::make_unit<units::voltage::millivolt_t>(max)); |
| 617 | } |
| 618 | |
| 619 | return {}; |
| 620 | } |
| 621 | |
| 622 | std::optional<std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>> |
| 623 | parseOverdriveVoltOffsetRange(std::vector<std::string> const &ppOdClkVoltageLines) |
| 624 | { |
| 625 | // Relevant lines format (kernel 6.14+): |
| 626 | // ... |
| 627 | // OD_RANGE: |
| 628 | // ... |
| 629 | // VDDGFX_OFFSET: -200mv 0mv |
| 630 | // ... |
| 631 | auto rangeIt = std::find_if( |
| 632 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 633 | [&](std::string const &line) { |
| 634 | return line.find("OD_RANGE:") != std::string::npos; |
| 635 | }); |
| 636 | if (rangeIt != ppOdClkVoltageLines.cend()) { |
| 637 | auto targetIt = std::find_if( |
| 638 | rangeIt, ppOdClkVoltageLines.cend(), [&](std::string const &line) { |
| 639 | return line.find("VDDGFX_OFFSET:") != std::string::npos; |
| 640 | }); |
| 641 | |
| 642 | if (targetIt != ppOdClkVoltageLines.cend()) |
| 643 | return parseOverdriveVoltOffsetRangeLine(*targetIt); |
| 644 | } |
| 645 | |
| 646 | return {}; |
| 647 | } |
| 648 | |
| 649 | std::optional<std::vector<std::string>> |
| 650 | parseOverdriveClkControls(std::vector<std::string> const &ppOdClkVoltageLines) |
| 651 | { |
| 652 | std::regex const regex(R"(^OD_(\wCLK):\s*$)", std::regex::icase); |
| 653 | std::vector<std::string> controlNames; |
| 654 | |
| 655 | for (auto const &line : ppOdClkVoltageLines) { |
| 656 | std::smatch result; |
| 657 | if (!std::regex_search(line, result, regex)) |
| 658 | continue; |
| 659 | |
| 660 | controlNames.emplace_back(result[1]); |
| 661 | } |
| 662 | |
| 663 | if (!controlNames.empty()) |
| 664 | return controlNames; |
| 665 | |
| 666 | return {}; |
| 667 | } |
| 668 | |
| 669 | std::optional<std::string> |
| 670 | getOverdriveClkControlCmdId(std::string_view controlName) |
| 671 | { |
| 672 | static std::unordered_map<std::string_view, std::string> const nameCmdIdMap{ |
| 673 | {"SCLK", "s"}, {"MCLK", "m"}}; |
| 674 | |
| 675 | if (nameCmdIdMap.count(controlName) > 0) |
| 676 | return nameCmdIdMap.at(controlName); |
| 677 | |
| 678 | return {}; |
| 679 | } |
| 680 | |
| 681 | std::optional<std::string> |
| 682 | getOverdriveClkOffsetControlCmdId(std::string_view controlName) |
| 683 | { |
| 684 | // NOTE: Clock offsets implementation uses the same command ids of clock |
| 685 | // control commands. This deviates from the voltage offset command, which is |
| 686 | // terminated by the 'o' character. |
| 687 | return getOverdriveClkControlCmdId(controlName); |
| 688 | } |
| 689 | |
| 690 | std::optional<std::vector<std::string>> parseOverdriveClkOffsetControls( |
| 691 | std::vector<std::string> const &ppOdClkVoltageLines) |
| 692 | { |
| 693 | std::regex const regex(R"(^OD_(\wCLK)_OFFSET:\s*$)", std::regex::icase); |
| 694 | std::vector<std::string> controlNames; |
| 695 | |
| 696 | for (auto const &line : ppOdClkVoltageLines) { |
| 697 | std::smatch result; |
| 698 | if (!std::regex_search(line, result, regex)) |
| 699 | continue; |
| 700 | |
| 701 | controlNames.emplace_back(result[1]); |
| 702 | } |
| 703 | |
| 704 | if (!controlNames.empty()) |
| 705 | return controlNames; |
| 706 | |
| 707 | return {}; |
| 708 | } |
| 709 | |
| 710 | std::optional<units::frequency::megahertz_t> |
| 711 | parseOverdriveClkOffsetLine(std::string const &line) |
| 712 | { |
| 713 | // Relevant lines format (kernel 6.14+): |
| 714 | // OD_SCLK_OFFSET: |
| 715 | // -200MHz |
| 716 | // ... |
| 717 | std::regex const regex(R"(^(-?\d+)\s*Mhz\s*$)", std::regex::icase); |
| 718 | |
| 719 | std::smatch result; |
| 720 | if (std::regex_search(line, result, regex)) { |
| 721 | int value; |
| 722 | if (Utils::String::toNumber(value, result[1])) |
| 723 | return units::frequency::megahertz_t(value); |
| 724 | } |
| 725 | |
| 726 | return {}; |
| 727 | } |
| 728 | |
| 729 | std::optional<units::frequency::megahertz_t> |
| 730 | parseOverdriveClkOffset(std::string_view controlName, |
| 731 | std::vector<std::string> const &ppOdClkVoltageLines) |
| 732 | { |
| 733 | // Relevant lines format (kernel 6.14+): |
| 734 | // ... |
| 735 | // OD_controlName_OFFSET: |
| 736 | // -100Mhz |
| 737 | // OD_otherLbl: |
| 738 | // ... |
| 739 | auto targetIt = std::find_if( |
| 740 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 741 | [&](std::string const &line) { |
| 742 | return line.find("OD_" + std::string(controlName) + "_OFFSET:") != |
| 743 | std::string::npos; |
| 744 | }); |
| 745 | if (targetIt != ppOdClkVoltageLines.cend() && |
| 746 | std::next(targetIt) != ppOdClkVoltageLines.cend()) |
| 747 | return parseOverdriveClkOffsetLine(*std::next(targetIt)); |
| 748 | |
| 749 | return {}; |
| 750 | } |
| 751 | |
| 752 | std::optional<std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>> |
| 753 | parseOverdriveClkRangeLine(std::string const &line) |
| 754 | { |
| 755 | // Relevant lines format (kernel 6.14+): |
| 756 | // ... |
| 757 | // label: -500Mhz 1000Mhz |
| 758 | // ... |
| 759 | std::regex const regex(R"(^(?:[^\:\s]+)\s*:\s*(-?\d+)\s*Mhz\s*(\d+)\s*Mhz\s*$)", |
| 760 | std::regex::icase); |
| 761 | std::smatch result; |
| 762 | |
| 763 | if (std::regex_search(line, result, regex)) { |
| 764 | int min{0}, max{0}; |
| 765 | if (Utils::String::toNumber<int>(min, result[1]) && |
| 766 | Utils::String::toNumber<int>(max, result[2])) |
| 767 | return std::make_pair(units::make_unit<units::frequency::megahertz_t>(min), |
| 768 | units::make_unit<units::frequency::megahertz_t>(max)); |
| 769 | } |
| 770 | |
| 771 | return {}; |
| 772 | } |
| 773 | |
| 774 | std::optional<std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>> |
| 775 | parseOverdriveClkOffsetRange(std::string_view controlName, |
| 776 | std::vector<std::string> const &ppOdClkVoltageLines) |
| 777 | { |
| 778 | // Relevant lines format (kernel 6.14+): |
| 779 | // ... |
| 780 | // OD_RANGE: |
| 781 | // ... |
| 782 | // controlName_OFFSET: -500Mhz 1000Mhz |
| 783 | // ... |
| 784 | auto rangeIt = std::find_if( |
| 785 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 786 | [&](std::string const &line) { |
| 787 | return line.find("OD_RANGE:") != std::string::npos; |
| 788 | }); |
| 789 | if (rangeIt != ppOdClkVoltageLines.cend()) { |
| 790 | auto targetIt = std::find_if( |
| 791 | rangeIt, ppOdClkVoltageLines.cend(), [&](std::string const &line) { |
| 792 | return line.find(std::string(controlName) + "_OFFSET:") != |
| 793 | std::string::npos; |
| 794 | }); |
| 795 | |
| 796 | if (targetIt != ppOdClkVoltageLines.cend()) |
| 797 | return parseOverdriveClkRangeLine(*targetIt); |
| 798 | } |
| 799 | |
| 800 | return {}; |
| 801 | } |
| 802 | |
| 803 | std::optional<std::vector<unsigned int>> ppOdClkVoltageFreqRangeOutOfRangeStates( |
| 804 | std::string const &controlName, |
| 805 | std::vector<std::string> const &ppOdClkVoltageLines) |
| 806 | { |
| 807 | // Search for out of range frequency clocks (RX6X00 XT) |
| 808 | // ... |
| 809 | // "OD_MCLK:", |
| 810 | // "0: 97Mhz", |
| 811 | // ... |
| 812 | // "OD_RANGE:", |
| 813 | // ... |
| 814 | // "MCLK: 674Mhz 1200Mhz", |
| 815 | auto clks = parseOverdriveClks(controlName, ppOdClkVoltageLines); |
| 816 | auto range = parseOverdriveClkRange(controlName, ppOdClkVoltageLines); |
| 817 | if (!(clks.has_value() && range.has_value())) |
| 818 | return std::nullopt; |
| 819 | |
| 820 | std::vector<unsigned int> states; |
| 821 | auto [min, max] = *range; |
| 822 | for (auto const &[index, clk] : *clks) { |
| 823 | if (!(clk >= min && clk <= max)) |
| 824 | states.push_back(index); |
| 825 | } |
| 826 | |
| 827 | if (!states.empty()) |
| 828 | return states; |
| 829 | |
| 830 | return std::nullopt; |
| 831 | } |
| 832 | |
| 833 | bool ppOdClkVoltageHasKnownFreqVoltQuirks( |
| 834 | std::string const &, std::vector<std::string> const &ppOdClkVoltageLines) |
| 835 | { |
| 836 | // Check for missing range section (kernel < 4.18) |
| 837 | auto odRangeIter = std::find_if( |
| 838 | ppOdClkVoltageLines.cbegin(), ppOdClkVoltageLines.cend(), |
| 839 | [&](std::string const &line) { return line == "OD_RANGE:"; }); |
| 840 | if (odRangeIter == ppOdClkVoltageLines.cend()) |
| 841 | return true; |
| 842 | |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | bool ppOdClkVoltageHasKnownVoltCurveQuirks( |
| 847 | std::vector<std::string> const &ppOdClkVoltageLines) |
| 848 | { |
| 849 | // Check for voltage incomplete curve points (navi on kernel < 5.6) |
| 850 | // "OD_VDDC_CURVE:", |
| 851 | // "0: 700Mhz @ 0mV", |
| 852 | auto atIter = std::find_if(ppOdClkVoltageLines.cbegin(), |
| 853 | ppOdClkVoltageLines.cend(), |
| 854 | [&](std::string const &line) { |
| 855 | return line.find("@") != std::string::npos; |
| 856 | }); |
| 857 | if (atIter != ppOdClkVoltageLines.cend()) { |
| 858 | auto points = parseOverdriveVoltCurve(ppOdClkVoltageLines); |
| 859 | if (!points.has_value()) |
| 860 | return true; |
| 861 | |
| 862 | return points->at(0).second == units::voltage::millivolt_t(0); |
| 863 | } |
| 864 | |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | std::optional<std::tuple<unsigned int, units::temperature::celsius_t, |
| 869 | units::concentration::percent_t>> |
| 870 | parseOverdriveFanCurveLine(std::string const &line) |
| 871 | { |
| 872 | // Relevant lines format (kernel 6.7+): |
| 873 | // ... |
| 874 | // 0: 45C 15% |
| 875 | // ... |
| 876 | std::regex const regex(R"((\d+)\s*:\s*(\d+)\s*C\s*(\d+)\s*%\s*$)", |
| 877 | std::regex::icase); |
| 878 | std::smatch result; |
| 879 | |
| 880 | if (std::regex_search(line, result, regex)) { |
| 881 | unsigned int index{0}, temp{0}, speed{0}; |
| 882 | if (Utils::String::toNumber<unsigned int>(index, result[1]) && |
| 883 | Utils::String::toNumber<unsigned int>(temp, result[2]) && |
| 884 | Utils::String::toNumber<unsigned int>(speed, result[3])) |
| 885 | return std::make_tuple(index, units::temperature::celsius_t(temp), |
| 886 | units::concentration::percent_t(speed)); |
| 887 | } |
| 888 | |
| 889 | return {}; |
| 890 | } |
| 891 | |
| 892 | std::optional<std::vector<std::tuple<unsigned int, units::temperature::celsius_t, |
| 893 | units::concentration::percent_t>>> |
| 894 | parseOverdriveFanCurve(std::vector<std::string> const &fanCurveLines) |
| 895 | { |
| 896 | // Relevant lines format (kernel 6.7+): |
| 897 | // OD_FAN_CURVE: |
| 898 | // 0: 0C 0% |
| 899 | // 1: 45C 15% |
| 900 | // 2: 50C 30% |
| 901 | // 3: 55C 70% |
| 902 | // 4: 65C 100% |
| 903 | // OD_RANGE: |
| 904 | |
| 905 | auto targetIt = std::find_if( |
| 906 | fanCurveLines.cbegin(), fanCurveLines.cend(), [&](std::string const &line) { |
| 907 | return line.find("OD_FAN_CURVE:") != std::string::npos; |
| 908 | }); |
| 909 | if (targetIt != fanCurveLines.cend() && |
| 910 | std::next(targetIt) != fanCurveLines.cend()) { |
| 911 | targetIt = std::next(targetIt); |
| 912 | |
| 913 | auto endIt = std::find_if(targetIt, fanCurveLines.cend(), |
| 914 | [&](std::string const &line) { |
| 915 | return line.find("OD_") != std::string::npos; |
| 916 | }); |
| 917 | |
| 918 | bool invalidPointData = false; |
| 919 | std::vector<std::tuple<unsigned int, units::temperature::celsius_t, |
| 920 | units::concentration::percent_t>> |
| 921 | points; |
| 922 | |
| 923 | while (targetIt != endIt) { |
| 924 | auto point = parseOverdriveFanCurveLine(*targetIt); |
| 925 | if (point.has_value()) { |
| 926 | points.emplace_back(std::move(*point)); |
| 927 | } |
| 928 | else { |
| 929 | invalidPointData = true; |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | targetIt = std::next(targetIt); |
| 934 | } |
| 935 | |
| 936 | if (!points.empty() && !invalidPointData) |
| 937 | return std::move(points); |
| 938 | } |
| 939 | |
| 940 | return {}; |
| 941 | } |
| 942 | |
| 943 | std::optional<std::pair<units::temperature::celsius_t, units::temperature::celsius_t>> |
| 944 | parseOverdriveFanCurveTempRangeLine(std::string const &line) |
| 945 | { |
| 946 | // Relevant lines format (kernel 6.7+): |
| 947 | // ... |
| 948 | // FAN_CURVE(hotspot temp): 25C 100C |
| 949 | // ... |
| 950 | std::regex const regex(R"(^.+\s*:\s*(\d+)\s*C\s*(\d+)\s*C\s*$)", |
| 951 | std::regex::icase); |
| 952 | std::smatch result; |
| 953 | |
| 954 | if (std::regex_search(line, result, regex)) { |
| 955 | int min{0}, max{0}; |
| 956 | if (Utils::String::toNumber<int>(min, result[1]) && |
| 957 | Utils::String::toNumber<int>(max, result[2])) |
| 958 | return std::make_pair(units::make_unit<units::temperature::celsius_t>(min), |
| 959 | units::make_unit<units::temperature::celsius_t>(max)); |
| 960 | } |
| 961 | |
| 962 | return {}; |
| 963 | } |
| 964 | |
| 965 | std::optional<std::pair<units::temperature::celsius_t, units::temperature::celsius_t>> |
| 966 | parseOverdriveFanCurveTempRange(std::vector<std::string> const &fanCurveLines) |
| 967 | { |
| 968 | // Relevant lines format (kernel 6.7+): |
| 969 | // ... |
| 970 | // OD_RANGE: |
| 971 | // ... |
| 972 | // FAN_CURVE(hotspot temp): 25C 100C |
| 973 | // ... |
| 974 | auto rangeIt = std::find_if( |
| 975 | fanCurveLines.cbegin(), fanCurveLines.cend(), [&](std::string const &line) { |
| 976 | return line.find("OD_RANGE:") != std::string::npos; |
| 977 | }); |
| 978 | if (rangeIt != fanCurveLines.cend()) { |
| 979 | std::regex const regex(R"(^FAN_CURVE\s*\(\w+\s+temp\)\s*:)", |
| 980 | std::regex::icase); |
| 981 | auto targetIt = std::find_if( |
| 982 | rangeIt, fanCurveLines.cend(), [&](std::string const &line) { |
| 983 | std::smatch result; |
| 984 | return std::regex_search(line, result, regex); |
| 985 | }); |
| 986 | |
| 987 | if (targetIt != fanCurveLines.cend()) |
| 988 | return parseOverdriveFanCurveTempRangeLine(*targetIt); |
| 989 | } |
| 990 | |
| 991 | return {}; |
| 992 | } |
| 993 | |
| 994 | std::optional< |
| 995 | std::pair<units::concentration::percent_t, units::concentration::percent_t>> |
| 996 | parseOverdriveFanCurveSpeedRangeLine(std::string const &line) |
| 997 | { |
| 998 | // Relevant lines format (kernel 6.7+): |
| 999 | // ... |
| 1000 | // FAN_CURVE(fan speed): 0% 100% |
| 1001 | // ... |
| 1002 | std::regex const regex(R"(^.+\s*:\s*(\d+)\s*%\s*(\d+)\s*%\s*$)", |
| 1003 | std::regex::icase); |
| 1004 | std::smatch result; |
| 1005 | |
| 1006 | if (std::regex_search(line, result, regex)) { |
| 1007 | int min{0}, max{0}; |
| 1008 | if (Utils::String::toNumber<int>(min, result[1]) && |
| 1009 | Utils::String::toNumber<int>(max, result[2])) |
| 1010 | return std::make_pair( |
| 1011 | units::make_unit<units::concentration::percent_t>(min), |
| 1012 | units::make_unit<units::concentration::percent_t>(max)); |
| 1013 | } |
| 1014 | |
| 1015 | return {}; |
| 1016 | } |
| 1017 | |
| 1018 | std::optional< |
| 1019 | std::pair<units::concentration::percent_t, units::concentration::percent_t>> |
| 1020 | parseOverdriveFanCurveSpeedRange(std::vector<std::string> const &fanCurveLines) |
| 1021 | { |
| 1022 | // Relevant lines format (kernel 6.7+): |
| 1023 | // ... |
| 1024 | // OD_RANGE: |
| 1025 | // ... |
| 1026 | // FAN_CURVE(fan speed): 0% 100% |
| 1027 | // ... |
| 1028 | auto rangeIt = std::find_if( |
| 1029 | fanCurveLines.cbegin(), fanCurveLines.cend(), [&](std::string const &line) { |
| 1030 | return line.find("OD_RANGE:") != std::string::npos; |
| 1031 | }); |
| 1032 | if (rangeIt != fanCurveLines.cend()) { |
| 1033 | std::regex const regex(R"(^FAN_CURVE\s*\(\w+\s+speed\)\s*:)", |
| 1034 | std::regex::icase); |
| 1035 | auto targetIt = std::find_if( |
| 1036 | rangeIt, fanCurveLines.cend(), [&](std::string const &line) { |
| 1037 | std::smatch result; |
| 1038 | return std::regex_search(line, result, regex); |
| 1039 | }); |
| 1040 | |
| 1041 | if (targetIt != fanCurveLines.cend()) |
| 1042 | return parseOverdriveFanCurveSpeedRangeLine(*targetIt); |
| 1043 | } |
| 1044 | |
| 1045 | return {}; |
| 1046 | } |
| 1047 | |
| 1048 | std::optional<bool> |
| 1049 | parseOverdriveFanStop(std::vector<std::string> const &fanStopLines) |
| 1050 | { |
| 1051 | // Relevant lines format (kernel 6.13+): |
| 1052 | // FAN_ZERO_RPM_ENABLE: |
| 1053 | // 1 |
| 1054 | // OD_RANGE: |
| 1055 | // ZERO_RPM_ENABLE: 0 1 |
| 1056 | |
| 1057 | auto targetIt = std::find_if( |
| 1058 | fanStopLines.cbegin(), fanStopLines.cend(), [&](std::string const &line) { |
| 1059 | return line.find("FAN_ZERO_RPM_ENABLE:") != std::string::npos; |
| 1060 | }); |
| 1061 | if (targetIt != fanStopLines.cend() && |
| 1062 | std::next(targetIt) != fanStopLines.cend()) { |
| 1063 | std::regex const regex(R"(^\s*(\d+)\s*$)", std::regex::icase); |
| 1064 | |
| 1065 | std::smatch result; |
| 1066 | if (std::regex_search(*std::next(targetIt), result, regex)) { |
| 1067 | int value; |
| 1068 | if (Utils::String::toNumber(value, result[1])) |
| 1069 | return value > 0; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | return {}; |
| 1074 | } |
| 1075 | |
| 1076 | std::optional<units::temperature::celsius_t> |
| 1077 | parseOverdriveFanStopTemp(std::vector<std::string> const &fanStopTempLines) |
| 1078 | { |
| 1079 | // Relevant lines format (kernel 6.13+): |
| 1080 | // FAN_ZERO_RPM_STOP_TEMPERATURE: |
| 1081 | // 20 |
| 1082 | // OD_RANGE: |
| 1083 | // ZERO_RPM_STOP_TEMPERATURE: 10 100 |
| 1084 | |
| 1085 | auto targetIt = std::find_if( |
| 1086 | fanStopTempLines.cbegin(), fanStopTempLines.cend(), |
| 1087 | [&](std::string const &line) { |
| 1088 | return line.find("FAN_ZERO_RPM_STOP_TEMPERATURE:") != std::string::npos; |
| 1089 | }); |
| 1090 | if (targetIt != fanStopTempLines.cend() && |
| 1091 | std::next(targetIt) != fanStopTempLines.cend()) { |
| 1092 | std::regex const regex(R"(^\s*(-?\d+)\s*$)", std::regex::icase); |
| 1093 | |
| 1094 | std::smatch result; |
| 1095 | if (std::regex_search(*std::next(targetIt), result, regex)) { |
| 1096 | int value; |
| 1097 | if (Utils::String::toNumber(value, result[1])) |
| 1098 | return units::temperature::celsius_t(value); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | return {}; |
| 1103 | } |
| 1104 | |
| 1105 | std::optional<std::pair<units::temperature::celsius_t, units::temperature::celsius_t>> |
| 1106 | parseOverdriveFanStopTempRange(std::vector<std::string> const &fanStopTempLines) |
| 1107 | { |
| 1108 | // Relevant lines format (kernel 6.13+): |
| 1109 | // FAN_ZERO_RPM_STOP_TEMPERATURE: |
| 1110 | // 20 |
| 1111 | // OD_RANGE: |
| 1112 | // ZERO_RPM_STOP_TEMPERATURE: 10 100 |
| 1113 | |
| 1114 | auto targetIt = std::find_if( |
| 1115 | fanStopTempLines.cbegin(), fanStopTempLines.cend(), |
| 1116 | [&](std::string const &line) { |
| 1117 | return line.find("OD_RANGE:") != std::string::npos; |
| 1118 | }); |
| 1119 | if (targetIt != fanStopTempLines.cend() && |
| 1120 | std::next(targetIt) != fanStopTempLines.cend()) { |
| 1121 | std::regex const regex(R"(^ZERO_RPM_STOP_TEMPERATURE\s*:\s*(\d+)\s*(\d+)$)", |
| 1122 | std::regex::icase); |
| 1123 | std::smatch result; |
| 1124 | if (std::regex_search(*std::next(targetIt), result, regex)) { |
| 1125 | int min{0}, max{0}; |
| 1126 | if (Utils::String::toNumber<int>(min, result[1]) && |
| 1127 | Utils::String::toNumber<int>(max, result[2])) |
| 1128 | return std::make_pair( |
| 1129 | units::make_unit<units::temperature::celsius_t>(min), |
| 1130 | units::make_unit<units::temperature::celsius_t>(max)); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | return {}; |
| 1135 | } |
| 1136 | |
| 1137 | bool isPowerProfileModeDataColumnar(std::vector<std::string> const &data) |
| 1138 | { |
| 1139 | if (data.empty()) |
| 1140 | return false; |
| 1141 | |
| 1142 | // The first line of the data contains the profile names and their indices: |
| 1143 | // 0 BOOTUP_DEFAULT* 1 3D_FULL_SCREEN 2 POWER_SAVING 3 VIDEO ... |
| 1144 | auto const &firstLine = data.front(); |
| 1145 | |
| 1146 | // Try to match at least two (index power_profile_name) pairs |
| 1147 | std::regex const regex(R"(^\s*\d+\s+\w+\s*\*{0,1}\s*\d+\s+\w+\*{0,1})", |
| 1148 | std::regex::icase); |
| 1149 | std::smatch result; |
| 1150 | return std::regex_search(firstLine, result, regex); |
| 1151 | } |
| 1152 | |
| 1153 | bool hasOverdriveClkVoltControl(std::vector<std::string> const &data) |
| 1154 | { |
| 1155 | std::regex const clkRegex(R"(^OD_\wCLK:)", std::regex::icase); |
| 1156 | std::smatch result; |
| 1157 | |
| 1158 | auto clkIt = std::find_if(data.cbegin(), data.cend(), |
| 1159 | [&](std::string const &line) { |
| 1160 | return std::regex_match(line, result, clkRegex); |
| 1161 | }); |
| 1162 | |
| 1163 | if (clkIt != data.cend() && std::next(clkIt) != data.cend()) { |
| 1164 | auto state = parseOverdriveClkVoltLine(*std::next(clkIt)); |
| 1165 | return state.has_value(); |
| 1166 | } |
| 1167 | |
| 1168 | return false; |
| 1169 | } |
| 1170 | |
| 1171 | bool hasOverdriveClkControl(std::vector<std::string> const &data) |
| 1172 | { |
| 1173 | std::regex const clkRegex(R"(^OD_\wCLK:)", std::regex::icase); |
| 1174 | std::smatch result; |
| 1175 | |
| 1176 | auto clkIt = std::find_if(data.cbegin(), data.cend(), |
| 1177 | [&](std::string const &line) { |
| 1178 | return std::regex_match(line, result, clkRegex); |
| 1179 | }); |
| 1180 | |
| 1181 | if (clkIt != data.cend() && std::next(clkIt) != data.cend()) { |
| 1182 | auto state = parseOverdriveClksLine(*std::next(clkIt)); |
| 1183 | return state.has_value(); |
| 1184 | } |
| 1185 | |
| 1186 | return false; |
| 1187 | } |
| 1188 | |
| 1189 | bool hasOverdriveClkOffsetControl(std::vector<std::string> const &data) |
| 1190 | { |
| 1191 | std::regex const clkRegex(R"(^OD_\wCLK_OFFSET:)", std::regex::icase); |
| 1192 | std::smatch result; |
| 1193 | |
| 1194 | auto clkIt = std::find_if(data.cbegin(), data.cend(), |
| 1195 | [&](std::string const &line) { |
| 1196 | return std::regex_match(line, result, clkRegex); |
| 1197 | }); |
| 1198 | |
| 1199 | if (clkIt != data.cend() && std::next(clkIt) != data.cend()) { |
| 1200 | auto state = parseOverdriveClkOffsetLine(*std::next(clkIt)); |
| 1201 | return state.has_value(); |
| 1202 | } |
| 1203 | |
| 1204 | return false; |
| 1205 | } |
| 1206 | |
| 1207 | bool hasOverdriveVoltCurveControl(std::vector<std::string> const &data) |
| 1208 | { |
| 1209 | auto curveIt = std::find_if( |
| 1210 | data.cbegin(), data.cend(), [&](std::string const &line) { |
| 1211 | return line.find("OD_VDDC_CURVE:") != std::string::npos; |
| 1212 | }); |
| 1213 | |
| 1214 | if (curveIt != data.cend() && std::next(curveIt) != data.cend()) { |
| 1215 | auto point = parseOverdriveClkVoltLine(*std::next(curveIt)); |
| 1216 | return point.has_value(); |
| 1217 | } |
| 1218 | |
| 1219 | return false; |
| 1220 | } |
| 1221 | |
| 1222 | bool hasOverdriveVoltOffsetControl(std::vector<std::string> const &data) |
| 1223 | { |
| 1224 | auto offsetIt = std::find_if( |
| 1225 | data.cbegin(), data.cend(), [&](std::string const &line) { |
| 1226 | return line.find("OD_VDDGFX_OFFSET:") != std::string::npos; |
| 1227 | }); |
| 1228 | |
| 1229 | return offsetIt != data.cend(); |
| 1230 | } |
| 1231 | |
| 1232 | bool hasOverdriveFanTargetTempControl(std::vector<std::string> const &data) |
| 1233 | { |
| 1234 | auto offsetIt = std::find_if( |
| 1235 | data.cbegin(), data.cend(), [&](std::string const &line) { |
| 1236 | return line.find("FAN_TARGET_TEMPERATURE:") != std::string::npos; |
| 1237 | }); |
| 1238 | |
| 1239 | return offsetIt != data.cend(); |
| 1240 | } |
| 1241 | |
| 1242 | bool hasOverdriveFanMinimumPWMControl(std::vector<std::string> const &data) |
| 1243 | { |
| 1244 | auto offsetIt = std::find_if( |
| 1245 | data.cbegin(), data.cend(), [&](std::string const &line) { |
| 1246 | return line.find("FAN_MINIMUM_PWM:") != std::string::npos; |
| 1247 | }); |
| 1248 | |
| 1249 | return offsetIt != data.cend(); |
| 1250 | } |
| 1251 | |
| 1252 | bool hasOverdriveFanAcousticTargetControl(std::vector<std::string> const &data) |
| 1253 | { |
| 1254 | auto offsetIt = std::find_if( |
| 1255 | data.cbegin(), data.cend(), [&](std::string const &line) { |
| 1256 | return line.find("OD_ACOUSTIC_TARGET:") != std::string::npos; |
| 1257 | }); |
| 1258 | |
| 1259 | return offsetIt != data.cend(); |
| 1260 | } |
| 1261 | |
| 1262 | bool hasOverdriveFanAcousticLimitControl(std::vector<std::string> const &data) |
| 1263 | { |
| 1264 | auto offsetIt = std::find_if( |
| 1265 | data.cbegin(), data.cend(), [&](std::string const &line) { |
| 1266 | return line.find("OD_ACOUSTIC_LIMIT:") != std::string::npos; |
| 1267 | }); |
| 1268 | |
| 1269 | return offsetIt != data.cend(); |
| 1270 | } |
| 1271 | |
| 1272 | } // namespace Utils::AMD |