xref: /openbmc/dbus-sensors/src/nvidia-gpu/NvidiaGpuDevice.cpp (revision 902c649b631ada141ee5910f7ee2a45026d5b084)
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*902c649bSHarshit Aghera #include <NvidiaGpuPowerSensor.hpp>
185e7deccdSHarshit Aghera #include <NvidiaGpuThresholds.hpp>
194ecdfaaaSHarshit Aghera #include <boost/asio/io_context.hpp>
204ecdfaaaSHarshit Aghera #include <phosphor-logging/lg2.hpp>
214ecdfaaaSHarshit Aghera #include <sdbusplus/asio/connection.hpp>
224ecdfaaaSHarshit Aghera #include <sdbusplus/asio/object_server.hpp>
234ecdfaaaSHarshit Aghera 
244ecdfaaaSHarshit Aghera #include <chrono>
254ecdfaaaSHarshit Aghera #include <cstdint>
265e7deccdSHarshit Aghera #include <functional>
274ecdfaaaSHarshit Aghera #include <memory>
284ecdfaaaSHarshit Aghera #include <string>
295e7deccdSHarshit Aghera #include <utility>
304ecdfaaaSHarshit Aghera #include <vector>
314ecdfaaaSHarshit Aghera 
324ecdfaaaSHarshit Aghera GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name,
334ecdfaaaSHarshit Aghera                      const std::string& path,
344ecdfaaaSHarshit Aghera                      const std::shared_ptr<sdbusplus::asio::connection>& conn,
354ecdfaaaSHarshit Aghera                      uint8_t eid, boost::asio::io_context& io,
364ecdfaaaSHarshit Aghera                      mctp::MctpRequester& mctpRequester,
374ecdfaaaSHarshit Aghera                      sdbusplus::asio::object_server& objectServer) :
384ecdfaaaSHarshit Aghera     eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}),
394ecdfaaaSHarshit Aghera     waitTimer(io, std::chrono::steady_clock::duration(0)),
404ecdfaaaSHarshit Aghera     mctpRequester(mctpRequester), conn(conn), objectServer(objectServer),
414ecdfaaaSHarshit Aghera     configs(configs), name(escapeName(name)), path(path)
424ecdfaaaSHarshit Aghera {
434ecdfaaaSHarshit Aghera     makeSensors();
444ecdfaaaSHarshit Aghera }
454ecdfaaaSHarshit Aghera 
464ecdfaaaSHarshit Aghera void GpuDevice::makeSensors()
474ecdfaaaSHarshit Aghera {
484ecdfaaaSHarshit Aghera     tempSensor = std::make_shared<NvidiaGpuTempSensor>(
49ba138daeSHarshit Aghera         conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId,
50ba138daeSHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
51ba138daeSHarshit Aghera 
525e7deccdSHarshit Aghera     readThermalParameters(
535e7deccdSHarshit Aghera         eid,
545e7deccdSHarshit Aghera         std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId,
555e7deccdSHarshit Aghera                                     gpuTLimitCriticalThresholdId,
565e7deccdSHarshit Aghera                                     gpuTLimitHardshutDownThresholdId},
575e7deccdSHarshit Aghera         mctpRequester,
585e7deccdSHarshit Aghera         std::bind_front(&GpuDevice::processTLimitThresholds, this));
594ecdfaaaSHarshit Aghera 
60*902c649bSHarshit Aghera     powerSensor = std::make_shared<NvidiaGpuPowerSensor>(
61*902c649bSHarshit Aghera         conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId,
62*902c649bSHarshit Aghera         objectServer, std::vector<thresholds::Threshold>{});
63*902c649bSHarshit Aghera 
644ecdfaaaSHarshit Aghera     lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME",
654ecdfaaaSHarshit Aghera               name, "PATH", path);
664ecdfaaaSHarshit Aghera 
674ecdfaaaSHarshit Aghera     read();
684ecdfaaaSHarshit Aghera }
694ecdfaaaSHarshit Aghera 
705e7deccdSHarshit Aghera void GpuDevice::processTLimitThresholds(uint8_t rc,
715e7deccdSHarshit Aghera                                         const std::vector<int32_t>& thresholds)
725e7deccdSHarshit Aghera {
735e7deccdSHarshit Aghera     std::vector<thresholds::Threshold> tLimitThresholds{};
745e7deccdSHarshit Aghera     if (rc == 0)
755e7deccdSHarshit Aghera     {
765e7deccdSHarshit Aghera         tLimitThresholds = {
775e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::WARNING,
785e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
795e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[0])},
805e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::CRITICAL,
815e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
825e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[1])},
835e7deccdSHarshit Aghera             thresholds::Threshold{thresholds::Level::HARDSHUTDOWN,
845e7deccdSHarshit Aghera                                   thresholds::Direction::LOW,
855e7deccdSHarshit Aghera                                   static_cast<double>(thresholds[2])}};
865e7deccdSHarshit Aghera     }
875e7deccdSHarshit Aghera 
885e7deccdSHarshit Aghera     tLimitSensor = std::make_shared<NvidiaGpuTempSensor>(
895e7deccdSHarshit Aghera         conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId,
905e7deccdSHarshit Aghera         objectServer, std::move(tLimitThresholds));
915e7deccdSHarshit Aghera }
925e7deccdSHarshit Aghera 
934ecdfaaaSHarshit Aghera void GpuDevice::read()
944ecdfaaaSHarshit Aghera {
954ecdfaaaSHarshit Aghera     tempSensor->update();
965e7deccdSHarshit Aghera     if (tLimitSensor)
975e7deccdSHarshit Aghera     {
98ba138daeSHarshit Aghera         tLimitSensor->update();
995e7deccdSHarshit Aghera     }
100*902c649bSHarshit Aghera     powerSensor->update();
1014ecdfaaaSHarshit Aghera 
1024ecdfaaaSHarshit Aghera     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
1034ecdfaaaSHarshit Aghera     waitTimer.async_wait([this](const boost::system::error_code& ec) {
1044ecdfaaaSHarshit Aghera         if (ec)
1054ecdfaaaSHarshit Aghera         {
1064ecdfaaaSHarshit Aghera             return;
1074ecdfaaaSHarshit Aghera         }
1084ecdfaaaSHarshit Aghera         read();
1094ecdfaaaSHarshit Aghera     });
1104ecdfaaaSHarshit Aghera }
111