blob: 2c14a1c733bdb9f216bfc11dbf2dd655f000be47 [file] [log] [blame]
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +05301/*
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
9#include "NvidiaDeviceDiscovery.hpp"
10#include "NvidiaGpuSensor.hpp"
11#include "Thresholds.hpp"
12#include "Utils.hpp"
13
14#include <bits/basic_string.h>
15
16#include <MctpRequester.hpp>
Harshit Aghera775199d2025-05-27 14:20:24 +053017#include <NvidiaGpuEnergySensor.hpp>
Harshit Aghera902c6492025-05-08 15:57:42 +053018#include <NvidiaGpuPowerSensor.hpp>
Harshit Aghera5e7decc2025-05-07 16:20:16 +053019#include <NvidiaGpuThresholds.hpp>
Harshit Agherabef4d412025-05-27 14:53:56 +053020#include <NvidiaGpuVoltageSensor.hpp>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053021#include <boost/asio/io_context.hpp>
22#include <phosphor-logging/lg2.hpp>
23#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25
26#include <chrono>
27#include <cstdint>
Harshit Aghera5e7decc2025-05-07 16:20:16 +053028#include <functional>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053029#include <memory>
30#include <string>
Harshit Aghera5e7decc2025-05-07 16:20:16 +053031#include <utility>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053032#include <vector>
33
34GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name,
35 const std::string& path,
36 const std::shared_ptr<sdbusplus::asio::connection>& conn,
37 uint8_t eid, boost::asio::io_context& io,
38 mctp::MctpRequester& mctpRequester,
39 sdbusplus::asio::object_server& objectServer) :
40 eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}),
41 waitTimer(io, std::chrono::steady_clock::duration(0)),
42 mctpRequester(mctpRequester), conn(conn), objectServer(objectServer),
43 configs(configs), name(escapeName(name)), path(path)
44{
45 makeSensors();
46}
47
48void GpuDevice::makeSensors()
49{
50 tempSensor = std::make_shared<NvidiaGpuTempSensor>(
Harshit Agheraba138da2025-05-05 12:26:35 +053051 conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId,
52 objectServer, std::vector<thresholds::Threshold>{});
53
Harshit Aghera5e7decc2025-05-07 16:20:16 +053054 readThermalParameters(
55 eid,
56 std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId,
57 gpuTLimitCriticalThresholdId,
58 gpuTLimitHardshutDownThresholdId},
59 mctpRequester,
60 std::bind_front(&GpuDevice::processTLimitThresholds, this));
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053061
Harshit Aghera902c6492025-05-08 15:57:42 +053062 powerSensor = std::make_shared<NvidiaGpuPowerSensor>(
63 conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId,
64 objectServer, std::vector<thresholds::Threshold>{});
65
Harshit Aghera775199d2025-05-27 14:20:24 +053066 energySensor = std::make_shared<NvidiaGpuEnergySensor>(
67 conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId,
68 objectServer, std::vector<thresholds::Threshold>{});
69
Harshit Agherabef4d412025-05-27 14:53:56 +053070 voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>(
71 conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId,
72 objectServer, std::vector<thresholds::Threshold>{});
73
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053074 lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME",
75 name, "PATH", path);
76
77 read();
78}
79
Harshit Aghera5e7decc2025-05-07 16:20:16 +053080void GpuDevice::processTLimitThresholds(uint8_t rc,
81 const std::vector<int32_t>& thresholds)
82{
83 std::vector<thresholds::Threshold> tLimitThresholds{};
84 if (rc == 0)
85 {
86 tLimitThresholds = {
87 thresholds::Threshold{thresholds::Level::WARNING,
88 thresholds::Direction::LOW,
89 static_cast<double>(thresholds[0])},
90 thresholds::Threshold{thresholds::Level::CRITICAL,
91 thresholds::Direction::LOW,
92 static_cast<double>(thresholds[1])},
93 thresholds::Threshold{thresholds::Level::HARDSHUTDOWN,
94 thresholds::Direction::LOW,
95 static_cast<double>(thresholds[2])}};
96 }
97
98 tLimitSensor = std::make_shared<NvidiaGpuTempSensor>(
99 conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId,
100 objectServer, std::move(tLimitThresholds));
101}
102
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530103void GpuDevice::read()
104{
105 tempSensor->update();
Harshit Aghera5e7decc2025-05-07 16:20:16 +0530106 if (tLimitSensor)
107 {
108 tLimitSensor->update();
109 }
Harshit Aghera902c6492025-05-08 15:57:42 +0530110 powerSensor->update();
Harshit Aghera775199d2025-05-27 14:20:24 +0530111 energySensor->update();
Harshit Agherabef4d412025-05-27 14:53:56 +0530112 voltageSensor->update();
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530113
114 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
115 waitTimer.async_wait([this](const boost::system::error_code& ec) {
116 if (ec)
117 {
118 return;
119 }
120 read();
121 });
122}