| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "memfreq.h" |
| 5 | |
| 6 | #include "../gpusensorprovider.h" |
| 7 | #include "../graphitemprofilepart.h" |
| 8 | #include "../graphitemxmlparser.h" |
| 9 | #include "../sensor.h" |
| 10 | #include "common/fileutils.h" |
| 11 | #include "core/components/amdutils.h" |
| 12 | #include "core/devfsdatasource.h" |
| 13 | #include "core/info/igpuinfo.h" |
| 14 | #include "core/info/vendor.h" |
| 15 | #include "core/iprofilepart.h" |
| 16 | #include "core/iprofilepartxmlparser.h" |
| 17 | #include "core/profilepartprovider.h" |
| 18 | #include "core/profilepartxmlparserprovider.h" |
| 19 | #include <algorithm> |
| 20 | #include <filesystem> |
| 21 | #include <memory> |
| 22 | #include <optional> |
| 23 | #include <string> |
| 24 | #include <units.h> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace AMD::MemFreq { |
| 29 | |
| 30 | class Provider final : public IGPUSensorProvider::IProvider |
| 31 | { |
| 32 | public: |
| 33 | std::vector<std::unique_ptr<ISensor>> |
| 34 | provideGPUSensors(IGPUInfo const &gpuInfo, ISWInfo const &) const override |
| 35 | { |
| 36 | if (gpuInfo.vendor() != Vendor::AMD) |
| 37 | return {}; |
| 38 | |
| 39 | std::unique_ptr<ISensor> sensor; |
| 40 | |
| 41 | auto driver = gpuInfo.info(IGPUInfo::Keys::driver); |
| 42 | if (driver == "amdgpu") |
| 43 | sensor = createAMDGPUSensor(gpuInfo); |
| 44 | else if (driver == "radeon") |
| 45 | sensor = createRadeonSensor(gpuInfo); |
| 46 | |
| 47 | if (!sensor) |
| 48 | return {}; |
| 49 | |
| 50 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 51 | sensors.emplace_back(std::move(sensor)); |
| 52 | |
| 53 | return sensors; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | std::unique_ptr<ISensor> createAMDGPUSensor(IGPUInfo const &gpuInfo) const |
| 58 | { |
| 59 | #if defined(AMDGPU_INFO_SENSOR_GFX_MCLK) |
| 60 | std::optional< |
| 61 | std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>> |
| 62 | range; |
| 63 | |
| 64 | // get range from dpm mclk states (4.6+) |
| 65 | auto dpmData = |
| 66 | Utils::File::readFileLines(gpuInfo.path().sys / "pp_dpm_mclk"); |
| 67 | auto memStates = Utils::AMD::parseDPMStates(dpmData); |
| 68 | if (memStates.has_value() && !memStates->empty()) |
| 69 | range = {memStates->front().second, memStates->back().second}; |
| 70 | |
| 71 | // expand range using dpm sclk states |
| 72 | dpmData = Utils::File::readFileLines(gpuInfo.path().sys / "pp_dpm_sclk"); |
| 73 | auto gpuStates = Utils::AMD::parseDPMStates(dpmData); |
| 74 | if (gpuStates.has_value() && !gpuStates->empty()) { |
| 75 | if (range.has_value()) |
| 76 | range = {std::min(gpuStates->front().second, range->first), |
| 77 | std::max(gpuStates->back().second, range->second)}; |
| 78 | else // memory clock range not available |
| 79 | range = {gpuStates->front().second, gpuStates->back().second}; |
| 80 | } |
| 81 | |
| 82 | std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources; |
| 83 | dataSources.emplace_back(std::make_unique<DevFSDataSource<unsigned int>>( |
| 84 | gpuInfo.path().dev, [](int fd) { |
| 85 | unsigned int value; |
| 86 | bool success = Utils::AMD::readAMDGPUInfoSensor( |
| 87 | fd, &value, AMDGPU_INFO_SENSOR_GFX_MCLK); |
| 88 | return success ? value : 0; |
| 89 | })); |
| 90 | |
| 91 | return std::make_unique<Sensor<units::frequency::megahertz_t, unsigned int>>( |
| 92 | AMD::MemFreq::ItemID, std::move(dataSources), std::move(range)); |
| 93 | #else |
| 94 | return {}; |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | std::unique_ptr<ISensor> createRadeonSensor(IGPUInfo const &gpuInfo) const |
| 99 | { |
| 100 | #if defined(RADEON_INFO_CURRENT_GPU_MCLK) |
| 101 | std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources; |
| 102 | dataSources.emplace_back(std::make_unique<DevFSDataSource<unsigned int>>( |
| 103 | gpuInfo.path().dev, [](int fd) { |
| 104 | unsigned int value; |
| 105 | bool success = Utils::AMD::readRadeonInfoSensor( |
| 106 | fd, &value, RADEON_INFO_CURRENT_GPU_MCLK); |
| 107 | return success ? value : 0; |
| 108 | })); |
| 109 | |
| 110 | return std::make_unique<Sensor<units::frequency::megahertz_t, unsigned int>>( |
| 111 | AMD::MemFreq::ItemID, std::move(dataSources)); |
| 112 | #else |
| 113 | return {}; |
| 114 | #endif |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | static bool register_() |
| 119 | { |
| 120 | GPUSensorProvider::registerProvider(std::make_unique<AMD::MemFreq::Provider>()); |
| 121 | |
| 122 | ProfilePartProvider::registerProvider(AMD::MemFreq::ItemID, []() { |
| 123 | return std::make_unique<GraphItemProfilePart>(AMD::MemFreq::ItemID, |
| 124 | "orchid"); |
| 125 | }); |
| 126 | |
| 127 | ProfilePartXMLParserProvider::registerProvider(AMD::MemFreq::ItemID, []() { |
| 128 | return std::make_unique<GraphItemXMLParser>(AMD::MemFreq::ItemID); |
| 129 | }); |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | static bool const registered_ = register_(); |
| 135 | |
| 136 | } // namespace AMD::MemFreq |