xref: /openbmc/dbus-sensors/src/nvidia-gpu/NvidiaGpuDevice.cpp (revision b10a67b252ba8d899b88beb328f040ee9cc1e30f)
14ecdfaaaSHarshit Aghera /*
24ecdfaaaSHarshit Aghera  * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
34ecdfaaaSHarshit Aghera  * AFFILIATES. All rights reserved.
44ecdfaaaSHarshit Aghera  * SPDX-License-Identifier: Apache-2.0
54ecdfaaaSHarshit Aghera  */
64ecdfaaaSHarshit Aghera 
74ecdfaaaSHarshit Aghera #include "NvidiaGpuDevice.hpp"
84ecdfaaaSHarshit Aghera 
94ecdfaaaSHarshit Aghera #include "NvidiaDeviceDiscovery.hpp"
104ecdfaaaSHarshit Aghera #include "NvidiaGpuSensor.hpp"
114ecdfaaaSHarshit Aghera #include "Thresholds.hpp"
124ecdfaaaSHarshit Aghera #include "Utils.hpp"
134ecdfaaaSHarshit Aghera 
144ecdfaaaSHarshit Aghera #include <bits/basic_string.h>
154ecdfaaaSHarshit Aghera 
164ecdfaaaSHarshit Aghera #include <MctpRequester.hpp>
17775199d2SHarshit Aghera #include <NvidiaGpuEnergySensor.hpp>
18902c649bSHarshit Aghera #include <NvidiaGpuPowerSensor.hpp>
195e7deccdSHarshit Aghera #include <NvidiaGpuThresholds.hpp>
20bef4d418SHarshit Aghera #include <NvidiaGpuVoltageSensor.hpp>
214ecdfaaaSHarshit Aghera #include <boost/asio/io_context.hpp>
224ecdfaaaSHarshit Aghera #include <phosphor-logging/lg2.hpp>
234ecdfaaaSHarshit Aghera #include <sdbusplus/asio/connection.hpp>
244ecdfaaaSHarshit Aghera #include <sdbusplus/asio/object_server.hpp>
254ecdfaaaSHarshit Aghera 
264ecdfaaaSHarshit Aghera #include <chrono>
274ecdfaaaSHarshit Aghera #include <cstdint>
285e7deccdSHarshit Aghera #include <functional>
294ecdfaaaSHarshit Aghera #include <memory>
304ecdfaaaSHarshit Aghera #include <string>
315e7deccdSHarshit Aghera #include <utility>
324ecdfaaaSHarshit Aghera #include <vector>
334ecdfaaaSHarshit Aghera 
344ecdfaaaSHarshit Aghera GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name,
354ecdfaaaSHarshit Aghera                      const std::string& path,
364ecdfaaaSHarshit Aghera                      const std::shared_ptr<sdbusplus::asio::connection>& conn,
374ecdfaaaSHarshit Aghera                      uint8_t eid, boost::asio::io_context& io,
384ecdfaaaSHarshit Aghera                      mctp::MctpRequester& mctpRequester,
394ecdfaaaSHarshit Aghera                      sdbusplus::asio::object_server& objectServer) :
404ecdfaaaSHarshit Aghera     eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}),
414ecdfaaaSHarshit Aghera     waitTimer(io, std::chrono::steady_clock::duration(0)),
424ecdfaaaSHarshit Aghera     mctpRequester(mctpRequester), conn(conn), objectServer(objectServer),
434ecdfaaaSHarshit Aghera     configs(configs), name(escapeName(name)), path(path)
444ecdfaaaSHarshit Aghera {
454ecdfaaaSHarshit Aghera     makeSensors();
464ecdfaaaSHarshit Aghera }
474ecdfaaaSHarshit Aghera 
484ecdfaaaSHarshit Aghera void GpuDevice::makeSensors()
494ecdfaaaSHarshit Aghera {
504ecdfaaaSHarshit Aghera     tempSensor = std::make_shared<NvidiaGpuTempSensor>(
51ba138daeSHarshit Aghera         conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId,
52ba138daeSHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
53ba138daeSHarshit Aghera 
545e7deccdSHarshit Aghera     readThermalParameters(
555e7deccdSHarshit Aghera         eid,
565e7deccdSHarshit Aghera         std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId,
575e7deccdSHarshit Aghera                                     gpuTLimitCriticalThresholdId,
585e7deccdSHarshit Aghera                                     gpuTLimitHardshutDownThresholdId},
595e7deccdSHarshit Aghera         mctpRequester,
605e7deccdSHarshit Aghera         std::bind_front(&GpuDevice::processTLimitThresholds, this));
614ecdfaaaSHarshit Aghera 
62*b10a67b2SHarshit Aghera     dramTempSensor = std::make_shared<NvidiaGpuTempSensor>(
63*b10a67b2SHarshit Aghera         conn, mctpRequester, name + "_DRAM_0_TEMP_0", path, eid,
64*b10a67b2SHarshit Aghera         gpuDramTempSensorId, objectServer,
65*b10a67b2SHarshit Aghera         std::vector<thresholds::Threshold>{thresholds::Threshold{
66*b10a67b2SHarshit Aghera             thresholds::Level::CRITICAL, thresholds::Direction::HIGH, 95.0}});
67*b10a67b2SHarshit Aghera 
68902c649bSHarshit Aghera     powerSensor = std::make_shared<NvidiaGpuPowerSensor>(
69902c649bSHarshit Aghera         conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId,
70902c649bSHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
71902c649bSHarshit Aghera 
72775199d2SHarshit Aghera     energySensor = std::make_shared<NvidiaGpuEnergySensor>(
73775199d2SHarshit Aghera         conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId,
74775199d2SHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
75775199d2SHarshit Aghera 
76bef4d418SHarshit Aghera     voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>(
77bef4d418SHarshit Aghera         conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId,
78bef4d418SHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
79bef4d418SHarshit Aghera 
804ecdfaaaSHarshit Aghera     lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME",
814ecdfaaaSHarshit Aghera               name, "PATH", path);
824ecdfaaaSHarshit Aghera 
834ecdfaaaSHarshit Aghera     read();
844ecdfaaaSHarshit Aghera }
854ecdfaaaSHarshit Aghera 
865e7deccdSHarshit Aghera void GpuDevice::processTLimitThresholds(uint8_t rc,
875e7deccdSHarshit Aghera                                         const std::vector<int32_t>& thresholds)
885e7deccdSHarshit Aghera {
895e7deccdSHarshit Aghera     std::vector<thresholds::Threshold> tLimitThresholds{};
905e7deccdSHarshit Aghera     if (rc == 0)
915e7deccdSHarshit Aghera     {
925e7deccdSHarshit Aghera         tLimitThresholds = {
935e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::WARNING,
945e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
955e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[0])},
965e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::CRITICAL,
975e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
985e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[1])},
995e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::HARDSHUTDOWN,
1005e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
1015e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[2])}};
1025e7deccdSHarshit Aghera     }
1035e7deccdSHarshit Aghera 
1045e7deccdSHarshit Aghera     tLimitSensor = std::make_shared<NvidiaGpuTempSensor>(
1055e7deccdSHarshit Aghera         conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId,
1065e7deccdSHarshit Aghera         objectServer, std::move(tLimitThresholds));
1075e7deccdSHarshit Aghera }
1085e7deccdSHarshit Aghera 
1094ecdfaaaSHarshit Aghera void GpuDevice::read()
1104ecdfaaaSHarshit Aghera {
1114ecdfaaaSHarshit Aghera     tempSensor->update();
1125e7deccdSHarshit Aghera     if (tLimitSensor)
1135e7deccdSHarshit Aghera     {
114ba138daeSHarshit Aghera         tLimitSensor->update();
1155e7deccdSHarshit Aghera     }
116*b10a67b2SHarshit Aghera     dramTempSensor->update();
117902c649bSHarshit Aghera     powerSensor->update();
118775199d2SHarshit Aghera     energySensor->update();
119bef4d418SHarshit Aghera     voltageSensor->update();
1204ecdfaaaSHarshit Aghera 
1214ecdfaaaSHarshit Aghera     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
1224ecdfaaaSHarshit Aghera     waitTimer.async_wait([this](const boost::system::error_code& ec) {
1234ecdfaaaSHarshit Aghera         if (ec)
1244ecdfaaaSHarshit Aghera         {
1254ecdfaaaSHarshit Aghera             return;
1264ecdfaaaSHarshit Aghera         }
1274ecdfaaaSHarshit Aghera         read();
1284ecdfaaaSHarshit Aghera     });
1294ecdfaaaSHarshit Aghera }
130