Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include "NvidiaGpuDevice.hpp" |
| 8 | |
| 9 | #include "NvidiaDeviceDiscovery.hpp" |
| 10 | #include "NvidiaGpuSensor.hpp" |
| 11 | #include "Thresholds.hpp" |
| 12 | #include "Utils.hpp" |
| 13 | |
| 14 | #include <bits/basic_string.h> |
| 15 | |
| 16 | #include <MctpRequester.hpp> |
Harshit Aghera | 775199d | 2025-05-27 14:20:24 +0530 | [diff] [blame] | 17 | #include <NvidiaGpuEnergySensor.hpp> |
Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 18 | #include <NvidiaGpuPowerSensor.hpp> |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 19 | #include <NvidiaGpuThresholds.hpp> |
Harshit Aghera | bef4d41 | 2025-05-27 14:53:56 +0530 | [diff] [blame] | 20 | #include <NvidiaGpuVoltageSensor.hpp> |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 21 | #include <boost/asio/io_context.hpp> |
| 22 | #include <phosphor-logging/lg2.hpp> |
| 23 | #include <sdbusplus/asio/connection.hpp> |
| 24 | #include <sdbusplus/asio/object_server.hpp> |
| 25 | |
| 26 | #include <chrono> |
| 27 | #include <cstdint> |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 28 | #include <functional> |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 29 | #include <memory> |
| 30 | #include <string> |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 31 | #include <utility> |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
| 34 | GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name, |
| 35 | const std::string& path, |
| 36 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 37 | uint8_t eid, boost::asio::io_context& io, |
| 38 | mctp::MctpRequester& mctpRequester, |
| 39 | sdbusplus::asio::object_server& objectServer) : |
| 40 | eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}), |
| 41 | waitTimer(io, std::chrono::steady_clock::duration(0)), |
| 42 | mctpRequester(mctpRequester), conn(conn), objectServer(objectServer), |
| 43 | configs(configs), name(escapeName(name)), path(path) |
| 44 | { |
| 45 | makeSensors(); |
| 46 | } |
| 47 | |
| 48 | void GpuDevice::makeSensors() |
| 49 | { |
| 50 | tempSensor = std::make_shared<NvidiaGpuTempSensor>( |
Harshit Aghera | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 51 | conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId, |
| 52 | objectServer, std::vector<thresholds::Threshold>{}); |
| 53 | |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 54 | readThermalParameters( |
| 55 | eid, |
| 56 | std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId, |
| 57 | gpuTLimitCriticalThresholdId, |
| 58 | gpuTLimitHardshutDownThresholdId}, |
| 59 | mctpRequester, |
| 60 | std::bind_front(&GpuDevice::processTLimitThresholds, this)); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 61 | |
Harshit Aghera | b10a67b | 2025-05-27 12:19:29 +0530 | [diff] [blame] | 62 | dramTempSensor = std::make_shared<NvidiaGpuTempSensor>( |
| 63 | conn, mctpRequester, name + "_DRAM_0_TEMP_0", path, eid, |
| 64 | gpuDramTempSensorId, objectServer, |
| 65 | std::vector<thresholds::Threshold>{thresholds::Threshold{ |
| 66 | thresholds::Level::CRITICAL, thresholds::Direction::HIGH, 95.0}}); |
| 67 | |
Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 68 | powerSensor = std::make_shared<NvidiaGpuPowerSensor>( |
| 69 | conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId, |
| 70 | objectServer, std::vector<thresholds::Threshold>{}); |
| 71 | |
Harshit Aghera | 775199d | 2025-05-27 14:20:24 +0530 | [diff] [blame] | 72 | energySensor = std::make_shared<NvidiaGpuEnergySensor>( |
| 73 | conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId, |
| 74 | objectServer, std::vector<thresholds::Threshold>{}); |
| 75 | |
Harshit Aghera | bef4d41 | 2025-05-27 14:53:56 +0530 | [diff] [blame] | 76 | voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>( |
| 77 | conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId, |
| 78 | objectServer, std::vector<thresholds::Threshold>{}); |
| 79 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 80 | lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME", |
| 81 | name, "PATH", path); |
| 82 | |
| 83 | read(); |
| 84 | } |
| 85 | |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 86 | void GpuDevice::processTLimitThresholds(uint8_t rc, |
| 87 | const std::vector<int32_t>& thresholds) |
| 88 | { |
| 89 | std::vector<thresholds::Threshold> tLimitThresholds{}; |
| 90 | if (rc == 0) |
| 91 | { |
| 92 | tLimitThresholds = { |
| 93 | thresholds::Threshold{thresholds::Level::WARNING, |
| 94 | thresholds::Direction::LOW, |
| 95 | static_cast<double>(thresholds[0])}, |
| 96 | thresholds::Threshold{thresholds::Level::CRITICAL, |
| 97 | thresholds::Direction::LOW, |
| 98 | static_cast<double>(thresholds[1])}, |
| 99 | thresholds::Threshold{thresholds::Level::HARDSHUTDOWN, |
| 100 | thresholds::Direction::LOW, |
| 101 | static_cast<double>(thresholds[2])}}; |
| 102 | } |
| 103 | |
| 104 | tLimitSensor = std::make_shared<NvidiaGpuTempSensor>( |
| 105 | conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId, |
| 106 | objectServer, std::move(tLimitThresholds)); |
| 107 | } |
| 108 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 109 | void GpuDevice::read() |
| 110 | { |
| 111 | tempSensor->update(); |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 112 | if (tLimitSensor) |
| 113 | { |
| 114 | tLimitSensor->update(); |
| 115 | } |
Harshit Aghera | b10a67b | 2025-05-27 12:19:29 +0530 | [diff] [blame] | 116 | dramTempSensor->update(); |
Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 117 | powerSensor->update(); |
Harshit Aghera | 775199d | 2025-05-27 14:20:24 +0530 | [diff] [blame] | 118 | energySensor->update(); |
Harshit Aghera | bef4d41 | 2025-05-27 14:53:56 +0530 | [diff] [blame] | 119 | voltageSensor->update(); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 120 | |
| 121 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); |
| 122 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 123 | if (ec) |
| 124 | { |
| 125 | return; |
| 126 | } |
| 127 | read(); |
| 128 | }); |
| 129 | } |