| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuinfopm.h" |
| 5 | |
| 6 | #include "common/fileutils.h" |
| 7 | #include "core/info/infoproviderregistry.h" |
| 8 | #include <spdlog/spdlog.h> |
| 9 | #include <utility> |
| 10 | |
| 11 | namespace AMD { |
| 12 | class GPUInfoPMLegacyDataSource |
| 13 | : public IDataSource<std::string, std::filesystem::path const> |
| 14 | { |
| 15 | public: |
| 16 | std::string source() const override |
| 17 | { |
| 18 | return "power_method"; |
| 19 | } |
| 20 | |
| 21 | bool read(std::string &data, std::filesystem::path const &path) override |
| 22 | { |
| 23 | auto const filePath = path / source(); |
| 24 | if (Utils::File::isFilePathValid(filePath)) { |
| 25 | auto const lines = Utils::File::readFileLines(filePath); |
| 26 | if (!lines.empty()) { |
| 27 | data = lines.front(); |
| 28 | return true; |
| 29 | } |
| 30 | else { |
| 31 | SPDLOG_WARN("Cannot retrieve device power_method from {}", |
| 32 | filePath.c_str()); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return false; |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | class GPUInfoPMDPMDataSource |
| 41 | : public IDataSource<std::string, std::filesystem::path const> |
| 42 | { |
| 43 | public: |
| 44 | std::string source() const override |
| 45 | { |
| 46 | return "power_dpm_force_performance_level"; |
| 47 | } |
| 48 | |
| 49 | bool read(std::string &, std::filesystem::path const &path) override |
| 50 | { |
| 51 | // newer gpus uses 'power_dpm_force_performance_level' file |
| 52 | auto const filePath = path / source(); |
| 53 | if (Utils::File::isSysFSEntryValid(filePath)) |
| 54 | return true; |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | }; |
| 59 | } // namespace AMD |
| 60 | |
| 61 | AMD::GPUInfoPM::GPUInfoPM( |
| 62 | std::vector<std::shared_ptr<IDataSource<std::string, std::filesystem::path const>>> |
| 63 | &&dataSources) noexcept |
| 64 | : dataSources_(std::move(dataSources)) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | std::vector<std::pair<std::string, std::string>> |
| 69 | AMD::GPUInfoPM::provideInfo(Vendor, int, IGPUInfo::Path const &, |
| 70 | IHWIDTranslator const &) const |
| 71 | { |
| 72 | return {}; |
| 73 | } |
| 74 | |
| 75 | std::vector<std::string> |
| 76 | AMD::GPUInfoPM::provideCapabilities(Vendor vendor, int, |
| 77 | IGPUInfo::Path const &path) const |
| 78 | { |
| 79 | std::vector<std::string> caps; |
| 80 | |
| 81 | if (vendor == Vendor::AMD) { |
| 82 | for (auto &dataSource : dataSources_) { |
| 83 | std::string data; |
| 84 | if (dataSource->read(data, path.sys)) { |
| 85 | // Older gpus have 'power_method' file |
| 86 | if (dataSource->source() == "power_method") { |
| 87 | if (data == "dynpm" || data == "profile") |
| 88 | caps.emplace_back(GPUInfoPM::Legacy); |
| 89 | else if (data == "dpm") |
| 90 | caps.emplace_back(GPUInfoPM::Radeon); |
| 91 | } |
| 92 | // Newer gpus uses 'power_dpm_force_performance_level' file |
| 93 | else if (dataSource->source() == "power_dpm_force_performance_level") { |
| 94 | caps.emplace_back(GPUInfoPM::Amdgpu); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return caps; |
| 101 | } |
| 102 | |
| 103 | bool AMD::GPUInfoPM::registered_ = |
| 104 | InfoProviderRegistry::add(std::make_unique<AMD::GPUInfoPM>( |
| 105 | std::vector< |
| 106 | std::shared_ptr<IDataSource<std::string, std::filesystem::path const>>>( |
| 107 | {std::make_shared<AMD::GPUInfoPMLegacyDataSource>(), |
| 108 | std::make_shared<AMD::GPUInfoPMDPMDataSource>()}))); |