blob: a13bcfe26ba1a347ab62aff579cca12fc028a46f [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
Rohit PAI0a888262025-06-11 08:52:29 +05309#include "Inventory.hpp"
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053010#include "NvidiaDeviceDiscovery.hpp"
11#include "NvidiaGpuSensor.hpp"
12#include "Thresholds.hpp"
13#include "Utils.hpp"
14
15#include <bits/basic_string.h>
16
17#include <MctpRequester.hpp>
Harshit Aghera775199d2025-05-27 14:20:24 +053018#include <NvidiaGpuEnergySensor.hpp>
Rohit PAI0a888262025-06-11 08:52:29 +053019#include <NvidiaGpuMctpVdm.hpp>
Harshit Aghera902c6492025-05-08 15:57:42 +053020#include <NvidiaGpuPowerSensor.hpp>
Harshit Aghera5e7decc2025-05-07 16:20:16 +053021#include <NvidiaGpuThresholds.hpp>
Harshit Agherabef4d412025-05-27 14:53:56 +053022#include <NvidiaGpuVoltageSensor.hpp>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053023#include <boost/asio/io_context.hpp>
24#include <phosphor-logging/lg2.hpp>
25#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27
28#include <chrono>
29#include <cstdint>
Harshit Aghera5e7decc2025-05-07 16:20:16 +053030#include <functional>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053031#include <memory>
32#include <string>
Harshit Aghera5e7decc2025-05-07 16:20:16 +053033#include <utility>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053034#include <vector>
35
36GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name,
37 const std::string& path,
38 const std::shared_ptr<sdbusplus::asio::connection>& conn,
39 uint8_t eid, boost::asio::io_context& io,
40 mctp::MctpRequester& mctpRequester,
41 sdbusplus::asio::object_server& objectServer) :
42 eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}),
43 waitTimer(io, std::chrono::steady_clock::duration(0)),
44 mctpRequester(mctpRequester), conn(conn), objectServer(objectServer),
45 configs(configs), name(escapeName(name)), path(path)
46{
Rohit PAI0a888262025-06-11 08:52:29 +053047 inventory = std::make_shared<Inventory>(
Rohit PAIada6baa2025-07-01 18:26:19 +053048 conn, objectServer, name, mctpRequester,
49 gpu::DeviceIdentification::DEVICE_GPU, eid, io);
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053050 makeSensors();
51}
52
53void GpuDevice::makeSensors()
54{
55 tempSensor = std::make_shared<NvidiaGpuTempSensor>(
Harshit Agheraba138da2025-05-05 12:26:35 +053056 conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId,
57 objectServer, std::vector<thresholds::Threshold>{});
58
Harshit Aghera5e7decc2025-05-07 16:20:16 +053059 readThermalParameters(
60 eid,
61 std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId,
62 gpuTLimitCriticalThresholdId,
63 gpuTLimitHardshutDownThresholdId},
64 mctpRequester,
65 std::bind_front(&GpuDevice::processTLimitThresholds, this));
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053066
Harshit Agherab10a67b2025-05-27 12:19:29 +053067 dramTempSensor = std::make_shared<NvidiaGpuTempSensor>(
68 conn, mctpRequester, name + "_DRAM_0_TEMP_0", path, eid,
69 gpuDramTempSensorId, objectServer,
70 std::vector<thresholds::Threshold>{thresholds::Threshold{
71 thresholds::Level::CRITICAL, thresholds::Direction::HIGH, 95.0}});
72
Harshit Aghera902c6492025-05-08 15:57:42 +053073 powerSensor = std::make_shared<NvidiaGpuPowerSensor>(
74 conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId,
75 objectServer, std::vector<thresholds::Threshold>{});
76
Harshit Aghera775199d2025-05-27 14:20:24 +053077 energySensor = std::make_shared<NvidiaGpuEnergySensor>(
78 conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId,
79 objectServer, std::vector<thresholds::Threshold>{});
80
Harshit Agherabef4d412025-05-27 14:53:56 +053081 voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>(
82 conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId,
83 objectServer, std::vector<thresholds::Threshold>{});
84
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053085 lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME",
86 name, "PATH", path);
87
88 read();
89}
90
Harshit Aghera5e7decc2025-05-07 16:20:16 +053091void GpuDevice::processTLimitThresholds(uint8_t rc,
92 const std::vector<int32_t>& thresholds)
93{
94 std::vector<thresholds::Threshold> tLimitThresholds{};
95 if (rc == 0)
96 {
97 tLimitThresholds = {
98 thresholds::Threshold{thresholds::Level::WARNING,
99 thresholds::Direction::LOW,
100 static_cast<double>(thresholds[0])},
101 thresholds::Threshold{thresholds::Level::CRITICAL,
102 thresholds::Direction::LOW,
103 static_cast<double>(thresholds[1])},
104 thresholds::Threshold{thresholds::Level::HARDSHUTDOWN,
105 thresholds::Direction::LOW,
106 static_cast<double>(thresholds[2])}};
107 }
108
109 tLimitSensor = std::make_shared<NvidiaGpuTempSensor>(
110 conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId,
111 objectServer, std::move(tLimitThresholds));
112}
113
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530114void GpuDevice::read()
115{
116 tempSensor->update();
Harshit Aghera5e7decc2025-05-07 16:20:16 +0530117 if (tLimitSensor)
118 {
119 tLimitSensor->update();
120 }
Harshit Agherab10a67b2025-05-27 12:19:29 +0530121 dramTempSensor->update();
Harshit Aghera902c6492025-05-08 15:57:42 +0530122 powerSensor->update();
Harshit Aghera775199d2025-05-27 14:20:24 +0530123 energySensor->update();
Harshit Agherabef4d412025-05-27 14:53:56 +0530124 voltageSensor->update();
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530125
126 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
127 waitTimer.async_wait([this](const boost::system::error_code& ec) {
128 if (ec)
129 {
130 return;
131 }
132 read();
133 });
134}