xref: /openbmc/dbus-sensors/src/nvidia-gpu/NvidiaGpuDevice.cpp (revision 775199d2599981d2c4460c7c1e91a607829e9f0c)
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>
17*775199d2SHarshit Aghera #include <NvidiaGpuEnergySensor.hpp>
18902c649bSHarshit Aghera #include <NvidiaGpuPowerSensor.hpp>
195e7deccdSHarshit Aghera #include <NvidiaGpuThresholds.hpp>
204ecdfaaaSHarshit Aghera #include <boost/asio/io_context.hpp>
214ecdfaaaSHarshit Aghera #include <phosphor-logging/lg2.hpp>
224ecdfaaaSHarshit Aghera #include <sdbusplus/asio/connection.hpp>
234ecdfaaaSHarshit Aghera #include <sdbusplus/asio/object_server.hpp>
244ecdfaaaSHarshit Aghera 
254ecdfaaaSHarshit Aghera #include <chrono>
264ecdfaaaSHarshit Aghera #include <cstdint>
275e7deccdSHarshit Aghera #include <functional>
284ecdfaaaSHarshit Aghera #include <memory>
294ecdfaaaSHarshit Aghera #include <string>
305e7deccdSHarshit Aghera #include <utility>
314ecdfaaaSHarshit Aghera #include <vector>
324ecdfaaaSHarshit Aghera 
334ecdfaaaSHarshit Aghera GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name,
344ecdfaaaSHarshit Aghera                      const std::string& path,
354ecdfaaaSHarshit Aghera                      const std::shared_ptr<sdbusplus::asio::connection>& conn,
364ecdfaaaSHarshit Aghera                      uint8_t eid, boost::asio::io_context& io,
374ecdfaaaSHarshit Aghera                      mctp::MctpRequester& mctpRequester,
384ecdfaaaSHarshit Aghera                      sdbusplus::asio::object_server& objectServer) :
394ecdfaaaSHarshit Aghera     eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}),
404ecdfaaaSHarshit Aghera     waitTimer(io, std::chrono::steady_clock::duration(0)),
414ecdfaaaSHarshit Aghera     mctpRequester(mctpRequester), conn(conn), objectServer(objectServer),
424ecdfaaaSHarshit Aghera     configs(configs), name(escapeName(name)), path(path)
434ecdfaaaSHarshit Aghera {
444ecdfaaaSHarshit Aghera     makeSensors();
454ecdfaaaSHarshit Aghera }
464ecdfaaaSHarshit Aghera 
474ecdfaaaSHarshit Aghera void GpuDevice::makeSensors()
484ecdfaaaSHarshit Aghera {
494ecdfaaaSHarshit Aghera     tempSensor = std::make_shared<NvidiaGpuTempSensor>(
50ba138daeSHarshit Aghera         conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId,
51ba138daeSHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
52ba138daeSHarshit Aghera 
535e7deccdSHarshit Aghera     readThermalParameters(
545e7deccdSHarshit Aghera         eid,
555e7deccdSHarshit Aghera         std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId,
565e7deccdSHarshit Aghera                                     gpuTLimitCriticalThresholdId,
575e7deccdSHarshit Aghera                                     gpuTLimitHardshutDownThresholdId},
585e7deccdSHarshit Aghera         mctpRequester,
595e7deccdSHarshit Aghera         std::bind_front(&GpuDevice::processTLimitThresholds, this));
604ecdfaaaSHarshit Aghera 
61902c649bSHarshit Aghera     powerSensor = std::make_shared<NvidiaGpuPowerSensor>(
62902c649bSHarshit Aghera         conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId,
63902c649bSHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
64902c649bSHarshit Aghera 
65*775199d2SHarshit Aghera     energySensor = std::make_shared<NvidiaGpuEnergySensor>(
66*775199d2SHarshit Aghera         conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId,
67*775199d2SHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
68*775199d2SHarshit Aghera 
694ecdfaaaSHarshit Aghera     lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME",
704ecdfaaaSHarshit Aghera               name, "PATH", path);
714ecdfaaaSHarshit Aghera 
724ecdfaaaSHarshit Aghera     read();
734ecdfaaaSHarshit Aghera }
744ecdfaaaSHarshit Aghera 
755e7deccdSHarshit Aghera void GpuDevice::processTLimitThresholds(uint8_t rc,
765e7deccdSHarshit Aghera                                         const std::vector<int32_t>& thresholds)
775e7deccdSHarshit Aghera {
785e7deccdSHarshit Aghera     std::vector<thresholds::Threshold> tLimitThresholds{};
795e7deccdSHarshit Aghera     if (rc == 0)
805e7deccdSHarshit Aghera     {
815e7deccdSHarshit Aghera         tLimitThresholds = {
825e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::WARNING,
835e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
845e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[0])},
855e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::CRITICAL,
865e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
875e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[1])},
885e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::HARDSHUTDOWN,
895e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
905e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[2])}};
915e7deccdSHarshit Aghera     }
925e7deccdSHarshit Aghera 
935e7deccdSHarshit Aghera     tLimitSensor = std::make_shared<NvidiaGpuTempSensor>(
945e7deccdSHarshit Aghera         conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId,
955e7deccdSHarshit Aghera         objectServer, std::move(tLimitThresholds));
965e7deccdSHarshit Aghera }
975e7deccdSHarshit Aghera 
984ecdfaaaSHarshit Aghera void GpuDevice::read()
994ecdfaaaSHarshit Aghera {
1004ecdfaaaSHarshit Aghera     tempSensor->update();
1015e7deccdSHarshit Aghera     if (tLimitSensor)
1025e7deccdSHarshit Aghera     {
103ba138daeSHarshit Aghera         tLimitSensor->update();
1045e7deccdSHarshit Aghera     }
105902c649bSHarshit Aghera     powerSensor->update();
106*775199d2SHarshit Aghera     energySensor->update();
1074ecdfaaaSHarshit Aghera 
1084ecdfaaaSHarshit Aghera     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
1094ecdfaaaSHarshit Aghera     waitTimer.async_wait([this](const boost::system::error_code& ec) {
1104ecdfaaaSHarshit Aghera         if (ec)
1114ecdfaaaSHarshit Aghera         {
1124ecdfaaaSHarshit Aghera             return;
1134ecdfaaaSHarshit Aghera         }
1144ecdfaaaSHarshit Aghera         read();
1154ecdfaaaSHarshit Aghera     });
1164ecdfaaaSHarshit Aghera }
117