xref: /openbmc/dbus-sensors/src/nvidia-gpu/NvidiaGpuDevice.cpp (revision bef4d4182d361c67482d33264a0d15ead6f4da34)
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>
20*bef4d418SHarshit 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 
62902c649bSHarshit Aghera     powerSensor = std::make_shared<NvidiaGpuPowerSensor>(
63902c649bSHarshit Aghera         conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId,
64902c649bSHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
65902c649bSHarshit Aghera 
66775199d2SHarshit Aghera     energySensor = std::make_shared<NvidiaGpuEnergySensor>(
67775199d2SHarshit Aghera         conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId,
68775199d2SHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
69775199d2SHarshit Aghera 
70*bef4d418SHarshit Aghera     voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>(
71*bef4d418SHarshit Aghera         conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId,
72*bef4d418SHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
73*bef4d418SHarshit Aghera 
744ecdfaaaSHarshit Aghera     lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME",
754ecdfaaaSHarshit Aghera               name, "PATH", path);
764ecdfaaaSHarshit Aghera 
774ecdfaaaSHarshit Aghera     read();
784ecdfaaaSHarshit Aghera }
794ecdfaaaSHarshit Aghera 
805e7deccdSHarshit Aghera void GpuDevice::processTLimitThresholds(uint8_t rc,
815e7deccdSHarshit Aghera                                         const std::vector<int32_t>& thresholds)
825e7deccdSHarshit Aghera {
835e7deccdSHarshit Aghera     std::vector<thresholds::Threshold> tLimitThresholds{};
845e7deccdSHarshit Aghera     if (rc == 0)
855e7deccdSHarshit Aghera     {
865e7deccdSHarshit Aghera         tLimitThresholds = {
875e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::WARNING,
885e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
895e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[0])},
905e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::CRITICAL,
915e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
925e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[1])},
935e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::HARDSHUTDOWN,
945e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
955e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[2])}};
965e7deccdSHarshit Aghera     }
975e7deccdSHarshit Aghera 
985e7deccdSHarshit Aghera     tLimitSensor = std::make_shared<NvidiaGpuTempSensor>(
995e7deccdSHarshit Aghera         conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId,
1005e7deccdSHarshit Aghera         objectServer, std::move(tLimitThresholds));
1015e7deccdSHarshit Aghera }
1025e7deccdSHarshit Aghera 
1034ecdfaaaSHarshit Aghera void GpuDevice::read()
1044ecdfaaaSHarshit Aghera {
1054ecdfaaaSHarshit Aghera     tempSensor->update();
1065e7deccdSHarshit Aghera     if (tLimitSensor)
1075e7deccdSHarshit Aghera     {
108ba138daeSHarshit Aghera         tLimitSensor->update();
1095e7deccdSHarshit Aghera     }
110902c649bSHarshit Aghera     powerSensor->update();
111775199d2SHarshit Aghera     energySensor->update();
112*bef4d418SHarshit Aghera     voltageSensor->update();
1134ecdfaaaSHarshit Aghera 
1144ecdfaaaSHarshit Aghera     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
1154ecdfaaaSHarshit Aghera     waitTimer.async_wait([this](const boost::system::error_code& ec) {
1164ecdfaaaSHarshit Aghera         if (ec)
1174ecdfaaaSHarshit Aghera         {
1184ecdfaaaSHarshit Aghera             return;
1194ecdfaaaSHarshit Aghera         }
1204ecdfaaaSHarshit Aghera         read();
1214ecdfaaaSHarshit Aghera     });
1224ecdfaaaSHarshit Aghera }
123