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