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