Harshit Aghera | fa2a5b9 | 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 | 128c91d | 2025-05-27 14:20:24 +0530 | [diff] [blame] | 17 | #include <NvidiaGpuEnergySensor.hpp> |
Harshit Aghera | c8dab72 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 18 | #include <NvidiaGpuPowerSensor.hpp> |
Harshit Aghera | c20108d | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 19 | #include <NvidiaGpuThresholds.hpp> |
Harshit Aghera | b55847f | 2025-05-27 14:53:56 +0530 | [diff] [blame] | 20 | #include <NvidiaGpuVoltageSensor.hpp> |
Harshit Aghera | fa2a5b9 | 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> |
| 28 | #include <memory> |
| 29 | #include <string> |
Harshit Aghera | c20108d | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 30 | #include <utility> |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name, |
| 34 | const std::string& path, |
| 35 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 36 | uint8_t eid, boost::asio::io_context& io, |
| 37 | mctp::MctpRequester& mctpRequester, |
| 38 | sdbusplus::asio::object_server& objectServer) : |
| 39 | eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}), |
| 40 | waitTimer(io, std::chrono::steady_clock::duration(0)), |
| 41 | mctpRequester(mctpRequester), conn(conn), objectServer(objectServer), |
| 42 | configs(configs), name(escapeName(name)), path(path) |
| 43 | { |
| 44 | makeSensors(); |
| 45 | } |
| 46 | |
| 47 | void GpuDevice::makeSensors() |
| 48 | { |
| 49 | tempSensor = std::make_shared<NvidiaGpuTempSensor>( |
Harshit Aghera | 0e1718c | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 50 | conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId, |
| 51 | objectServer, std::vector<thresholds::Threshold>{}); |
| 52 | |
Harshit Aghera | c20108d | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 53 | readThermalParameters( |
| 54 | eid, |
| 55 | std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId, |
| 56 | gpuTLimitCriticalThresholdId, |
| 57 | gpuTLimitHardshutDownThresholdId}, |
| 58 | mctpRequester, [this](uint8_t rc, std::vector<int32_t> thresholds) { |
| 59 | std::vector<thresholds::Threshold> tLimitThresholds{}; |
| 60 | if (rc == 0) |
| 61 | { |
| 62 | tLimitThresholds = { |
| 63 | thresholds::Threshold{thresholds::Level::WARNING, |
| 64 | thresholds::Direction::LOW, |
| 65 | static_cast<double>(thresholds[0])}, |
| 66 | thresholds::Threshold{thresholds::Level::CRITICAL, |
| 67 | thresholds::Direction::LOW, |
| 68 | static_cast<double>(thresholds[1])}, |
| 69 | thresholds::Threshold{thresholds::Level::HARDSHUTDOWN, |
| 70 | thresholds::Direction::LOW, |
| 71 | static_cast<double>(thresholds[2])}}; |
| 72 | } |
| 73 | |
| 74 | tLimitSensor = std::make_shared<NvidiaGpuTempSensor>( |
| 75 | conn, mctpRequester, name + "_TEMP_1", path, eid, |
| 76 | gpuTLimitSensorId, objectServer, std::move(tLimitThresholds)); |
| 77 | }); |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 78 | |
Harshit Aghera | c8dab72 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 79 | powerSensor = std::make_shared<NvidiaGpuPowerSensor>( |
| 80 | conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId, |
| 81 | objectServer, std::vector<thresholds::Threshold>{}); |
| 82 | |
Harshit Aghera | 128c91d | 2025-05-27 14:20:24 +0530 | [diff] [blame] | 83 | energySensor = std::make_shared<NvidiaGpuEnergySensor>( |
| 84 | conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId, |
| 85 | objectServer, std::vector<thresholds::Threshold>{}); |
| 86 | |
Harshit Aghera | b55847f | 2025-05-27 14:53:56 +0530 | [diff] [blame] | 87 | voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>( |
| 88 | conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId, |
| 89 | objectServer, std::vector<thresholds::Threshold>{}); |
| 90 | |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 91 | lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME", |
| 92 | name, "PATH", path); |
| 93 | |
| 94 | read(); |
| 95 | } |
| 96 | |
| 97 | void GpuDevice::read() |
| 98 | { |
| 99 | tempSensor->update(); |
Harshit Aghera | c20108d | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 100 | if (tLimitSensor) |
| 101 | { |
| 102 | tLimitSensor->update(); |
| 103 | } |
Harshit Aghera | c8dab72 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 104 | powerSensor->update(); |
Harshit Aghera | 128c91d | 2025-05-27 14:20:24 +0530 | [diff] [blame] | 105 | energySensor->update(); |
Harshit Aghera | b55847f | 2025-05-27 14:53:56 +0530 | [diff] [blame] | 106 | voltageSensor->update(); |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 107 | |
| 108 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); |
| 109 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 110 | if (ec) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | read(); |
| 115 | }); |
| 116 | } |