blob: d7ad8461ed457ea6ddfd7be7167d62c5e0cbb1be [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>(
48 conn, objectServer, name, gpu::DeviceIdentification::DEVICE_GPU);
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053049 makeSensors();
50}
51
52void GpuDevice::makeSensors()
53{
54 tempSensor = std::make_shared<NvidiaGpuTempSensor>(
Harshit Agheraba138da2025-05-05 12:26:35 +053055 conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId,
56 objectServer, std::vector<thresholds::Threshold>{});
57
Harshit Aghera5e7decc2025-05-07 16:20:16 +053058 readThermalParameters(
59 eid,
60 std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId,
61 gpuTLimitCriticalThresholdId,
62 gpuTLimitHardshutDownThresholdId},
63 mctpRequester,
64 std::bind_front(&GpuDevice::processTLimitThresholds, this));
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053065
Harshit Agherab10a67b2025-05-27 12:19:29 +053066 dramTempSensor = std::make_shared<NvidiaGpuTempSensor>(
67 conn, mctpRequester, name + "_DRAM_0_TEMP_0", path, eid,
68 gpuDramTempSensorId, objectServer,
69 std::vector<thresholds::Threshold>{thresholds::Threshold{
70 thresholds::Level::CRITICAL, thresholds::Direction::HIGH, 95.0}});
71
Harshit Aghera902c6492025-05-08 15:57:42 +053072 powerSensor = std::make_shared<NvidiaGpuPowerSensor>(
73 conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId,
74 objectServer, std::vector<thresholds::Threshold>{});
75
Harshit Aghera775199d2025-05-27 14:20:24 +053076 energySensor = std::make_shared<NvidiaGpuEnergySensor>(
77 conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId,
78 objectServer, std::vector<thresholds::Threshold>{});
79
Harshit Agherabef4d412025-05-27 14:53:56 +053080 voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>(
81 conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId,
82 objectServer, std::vector<thresholds::Threshold>{});
83
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053084 lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME",
85 name, "PATH", path);
86
87 read();
88}
89
Harshit Aghera5e7decc2025-05-07 16:20:16 +053090void GpuDevice::processTLimitThresholds(uint8_t rc,
91 const std::vector<int32_t>& thresholds)
92{
93 std::vector<thresholds::Threshold> tLimitThresholds{};
94 if (rc == 0)
95 {
96 tLimitThresholds = {
97 thresholds::Threshold{thresholds::Level::WARNING,
98 thresholds::Direction::LOW,
99 static_cast<double>(thresholds[0])},
100 thresholds::Threshold{thresholds::Level::CRITICAL,
101 thresholds::Direction::LOW,
102 static_cast<double>(thresholds[1])},
103 thresholds::Threshold{thresholds::Level::HARDSHUTDOWN,
104 thresholds::Direction::LOW,
105 static_cast<double>(thresholds[2])}};
106 }
107
108 tLimitSensor = std::make_shared<NvidiaGpuTempSensor>(
109 conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId,
110 objectServer, std::move(tLimitThresholds));
111}
112
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530113void GpuDevice::read()
114{
115 tempSensor->update();
Harshit Aghera5e7decc2025-05-07 16:20:16 +0530116 if (tLimitSensor)
117 {
118 tLimitSensor->update();
119 }
Harshit Agherab10a67b2025-05-27 12:19:29 +0530120 dramTempSensor->update();
Harshit Aghera902c6492025-05-08 15:57:42 +0530121 powerSensor->update();
Harshit Aghera775199d2025-05-27 14:20:24 +0530122 energySensor->update();
Harshit Agherabef4d412025-05-27 14:53:56 +0530123 voltageSensor->update();
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530124
125 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
126 waitTimer.async_wait([this](const boost::system::error_code& ec) {
127 if (ec)
128 {
129 return;
130 }
131 read();
132 });
133}