| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "fancurveprovider.h" |
| 5 | |
| 6 | #include "../fanmodeprovider.h" |
| 7 | #include "common/fileutils.h" |
| 8 | #include "common/stringutils.h" |
| 9 | #include "core/info/amd/gpuinfoodfanctrl.h" |
| 10 | #include "core/info/igpuinfo.h" |
| 11 | #include "core/sysfsdatasource.h" |
| 12 | #include "fancurve.h" |
| 13 | #include <filesystem> |
| 14 | #include <memory> |
| 15 | #include <spdlog/spdlog.h> |
| 16 | #include <string> |
| 17 | |
| 18 | std::vector<std::unique_ptr<IControl>> |
| 19 | AMD::FanCurveProvider::provideGPUControls(IGPUInfo const &gpuInfo, |
| 20 | ISWInfo const &) const |
| 21 | { |
| 22 | std::vector<std::unique_ptr<IControl>> controls; |
| 23 | |
| 24 | if (!(gpuInfo.vendor() == Vendor::AMD && |
| 25 | !gpuInfo.hasCapability(GPUInfoOdFanCtrl::ID))) |
| 26 | return {}; |
| 27 | |
| 28 | auto path = Utils::File::findHWMonXDirectory(gpuInfo.path().sys / "hwmon"); |
| 29 | if (!path) |
| 30 | return {}; |
| 31 | |
| 32 | auto pwmEnable = path.value() / "pwm1_enable"; |
| 33 | auto pwm = path.value() / "pwm1"; |
| 34 | auto tempInput = path.value() / "temp1_input"; |
| 35 | auto tempCrit = path.value() / "temp1_crit"; |
| 36 | if (!(Utils::File::isSysFSEntryValid(pwm) && |
| 37 | Utils::File::isSysFSEntryValid(pwmEnable) && |
| 38 | Utils::File::isSysFSEntryValid(tempInput) && |
| 39 | Utils::File::isSysFSEntryValid(tempCrit))) |
| 40 | return {}; |
| 41 | |
| 42 | int tempCritValue{0}; |
| 43 | Utils::String::toNumber(tempCritValue, |
| 44 | Utils::File::readFileLines(tempCrit).front()); |
| 45 | tempCritValue = (tempCritValue > 0 && |
| 46 | tempCritValue < 150000) // check bogus values, see #103 |
| 47 | ? tempCritValue / 1000 |
| 48 | : 90; |
| 49 | |
| 50 | unsigned int value; |
| 51 | |
| 52 | auto pwmEnableLines = Utils::File::readFileLines(pwmEnable); |
| 53 | if (!Utils::String::toNumber<unsigned int>(value, pwmEnableLines.front())) { |
| 54 | SPDLOG_WARN("Unknown data format on {}", pwmEnable.string()); |
| 55 | SPDLOG_DEBUG(pwmEnableLines.front()); |
| 56 | return {}; |
| 57 | } |
| 58 | |
| 59 | auto pwmLines = Utils::File::readFileLines(pwm); |
| 60 | if (!Utils::String::toNumber<unsigned int>(value, pwmLines.front())) { |
| 61 | SPDLOG_WARN("Unknown data format on {}", pwm.string()); |
| 62 | SPDLOG_DEBUG(pwmLines.front()); |
| 63 | return {}; |
| 64 | } |
| 65 | |
| 66 | int tempInputValue; |
| 67 | auto tempInputLines = Utils::File::readFileLines(tempInput); |
| 68 | if (!Utils::String::toNumber<int>(tempInputValue, tempInputLines.front())) { |
| 69 | SPDLOG_WARN("Unknown data format on {}", tempInput.string()); |
| 70 | SPDLOG_DEBUG(tempInputLines.front()); |
| 71 | return {}; |
| 72 | } |
| 73 | |
| 74 | controls.emplace_back(std::make_unique<AMD::FanCurve>( |
| 75 | std::make_unique<SysFSDataSource<unsigned int>>( |
| 76 | pwmEnable, |
| 77 | [](std::string const &data, unsigned int &output) { |
| 78 | Utils::String::toNumber<unsigned int>(output, data); |
| 79 | }), |
| 80 | std::make_unique<SysFSDataSource<unsigned int>>( |
| 81 | pwm, |
| 82 | [](std::string const &data, unsigned int &output) { |
| 83 | Utils::String::toNumber<unsigned int>(output, data); |
| 84 | }), |
| 85 | std::make_unique<SysFSDataSource<int>>( |
| 86 | tempInput, |
| 87 | [](std::string const &data, int &output) { |
| 88 | int value; |
| 89 | Utils::String::toNumber<int>(value, data); |
| 90 | output = value / 1000; |
| 91 | }), |
| 92 | units::temperature::celsius_t(0), |
| 93 | units::temperature::celsius_t(tempCritValue))); |
| 94 | |
| 95 | return controls; |
| 96 | } |
| 97 | |
| 98 | bool const AMD::FanCurveProvider::registered_ = |
| 99 | AMD::FanModeProvider::registerProvider( |
| 100 | std::make_unique<AMD::FanCurveProvider>()); |