| 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); |
| Marc Olberding | ac92073 | 2025-09-28 21:56:54 -0700 | [diff] [blame^] | 49 | } |
| 50 | |
| 51 | void GpuDevice::init() |
| 52 | { |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 53 | makeSensors(); |
| Marc Olberding | ac92073 | 2025-09-28 21:56:54 -0700 | [diff] [blame^] | 54 | inventory->init(); |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void GpuDevice::makeSensors() |
| 58 | { |
| 59 | tempSensor = std::make_shared<NvidiaGpuTempSensor>( |
| Harshit Aghera | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 60 | conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId, |
| 61 | objectServer, std::vector<thresholds::Threshold>{}); |
| 62 | |
| Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 63 | readThermalParameters( |
| 64 | eid, |
| 65 | std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId, |
| 66 | gpuTLimitCriticalThresholdId, |
| 67 | gpuTLimitHardshutDownThresholdId}, |
| 68 | mctpRequester, |
| 69 | std::bind_front(&GpuDevice::processTLimitThresholds, this)); |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 70 | |
| Harshit Aghera | b10a67b | 2025-05-27 12:19:29 +0530 | [diff] [blame] | 71 | dramTempSensor = std::make_shared<NvidiaGpuTempSensor>( |
| 72 | conn, mctpRequester, name + "_DRAM_0_TEMP_0", path, eid, |
| 73 | gpuDramTempSensorId, objectServer, |
| 74 | std::vector<thresholds::Threshold>{thresholds::Threshold{ |
| 75 | thresholds::Level::CRITICAL, thresholds::Direction::HIGH, 95.0}}); |
| 76 | |
| Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 77 | powerSensor = std::make_shared<NvidiaGpuPowerSensor>( |
| 78 | conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId, |
| 79 | objectServer, std::vector<thresholds::Threshold>{}); |
| 80 | |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 81 | peakPower = std::make_shared<NvidiaGpuPowerPeakReading>( |
| 82 | mctpRequester, name + "_Power_0", eid, gpuPeakPowerSensorId, |
| 83 | objectServer); |
| 84 | |
| Harshit Aghera | 775199d | 2025-05-27 14:20:24 +0530 | [diff] [blame] | 85 | energySensor = std::make_shared<NvidiaGpuEnergySensor>( |
| 86 | conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId, |
| 87 | objectServer, std::vector<thresholds::Threshold>{}); |
| 88 | |
| Harshit Aghera | bef4d41 | 2025-05-27 14:53:56 +0530 | [diff] [blame] | 89 | voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>( |
| 90 | conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId, |
| 91 | objectServer, std::vector<thresholds::Threshold>{}); |
| 92 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 93 | lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME", |
| 94 | name, "PATH", path); |
| 95 | |
| 96 | read(); |
| 97 | } |
| 98 | |
| Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 99 | void GpuDevice::processTLimitThresholds(uint8_t rc, |
| 100 | const std::vector<int32_t>& thresholds) |
| 101 | { |
| 102 | std::vector<thresholds::Threshold> tLimitThresholds{}; |
| 103 | if (rc == 0) |
| 104 | { |
| 105 | tLimitThresholds = { |
| 106 | thresholds::Threshold{thresholds::Level::WARNING, |
| 107 | thresholds::Direction::LOW, |
| 108 | static_cast<double>(thresholds[0])}, |
| 109 | thresholds::Threshold{thresholds::Level::CRITICAL, |
| 110 | thresholds::Direction::LOW, |
| 111 | static_cast<double>(thresholds[1])}, |
| 112 | thresholds::Threshold{thresholds::Level::HARDSHUTDOWN, |
| 113 | thresholds::Direction::LOW, |
| 114 | static_cast<double>(thresholds[2])}}; |
| 115 | } |
| 116 | |
| 117 | tLimitSensor = std::make_shared<NvidiaGpuTempSensor>( |
| 118 | conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId, |
| 119 | objectServer, std::move(tLimitThresholds)); |
| 120 | } |
| 121 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 122 | void GpuDevice::read() |
| 123 | { |
| 124 | tempSensor->update(); |
| Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 125 | if (tLimitSensor) |
| 126 | { |
| 127 | tLimitSensor->update(); |
| 128 | } |
| Harshit Aghera | b10a67b | 2025-05-27 12:19:29 +0530 | [diff] [blame] | 129 | dramTempSensor->update(); |
| Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 130 | powerSensor->update(); |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 131 | peakPower->update(); |
| Harshit Aghera | 775199d | 2025-05-27 14:20:24 +0530 | [diff] [blame] | 132 | energySensor->update(); |
| Harshit Aghera | bef4d41 | 2025-05-27 14:53:56 +0530 | [diff] [blame] | 133 | voltageSensor->update(); |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 134 | |
| 135 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); |
| 136 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 137 | if (ec) |
| 138 | { |
| 139 | return; |
| 140 | } |
| 141 | read(); |
| 142 | }); |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 143 | }; |