blob: 4bba49c22643dca7e3c8bd53ce71f1c1ce9cea3b [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
Marc Olberdingd0125c92025-10-08 14:37:19 -07009#include "NvidiaGpuThresholds.hpp"
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053010#include "Thresholds.hpp"
11#include "Utils.hpp"
12
Marc Olberdingd0125c92025-10-08 14:37:19 -070013#include <Inventory.hpp>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053014#include <MctpRequester.hpp>
Marc Olberdingd0125c92025-10-08 14:37:19 -070015#include <NvidiaDeviceDiscovery.hpp>
Harshit Aghera775199d2025-05-27 14:20:24 +053016#include <NvidiaGpuEnergySensor.hpp>
Rohit PAI0a888262025-06-11 08:52:29 +053017#include <NvidiaGpuMctpVdm.hpp>
Harshit Aghera6b712322025-07-31 19:25:12 +053018#include <NvidiaGpuPowerPeakReading.hpp>
Harshit Aghera902c6492025-05-08 15:57:42 +053019#include <NvidiaGpuPowerSensor.hpp>
Marc Olberdingd0125c92025-10-08 14:37:19 -070020#include <NvidiaGpuSensor.hpp>
Harshit Agherabef4d412025-05-27 14:53:56 +053021#include <NvidiaGpuVoltageSensor.hpp>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053022#include <boost/asio/io_context.hpp>
23#include <phosphor-logging/lg2.hpp>
24#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26
27#include <chrono>
28#include <cstdint>
Harshit Aghera5e7decc2025-05-07 16:20:16 +053029#include <functional>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053030#include <memory>
31#include <string>
Harshit Aghera5e7decc2025-05-07 16:20:16 +053032#include <utility>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053033#include <vector>
34
35GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name,
36 const std::string& path,
37 const std::shared_ptr<sdbusplus::asio::connection>& conn,
38 uint8_t eid, boost::asio::io_context& io,
39 mctp::MctpRequester& mctpRequester,
40 sdbusplus::asio::object_server& objectServer) :
41 eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}),
42 waitTimer(io, std::chrono::steady_clock::duration(0)),
43 mctpRequester(mctpRequester), conn(conn), objectServer(objectServer),
44 configs(configs), name(escapeName(name)), path(path)
45{
Rohit PAI0a888262025-06-11 08:52:29 +053046 inventory = std::make_shared<Inventory>(
Rohit PAIada6baa2025-07-01 18:26:19 +053047 conn, objectServer, name, mctpRequester,
48 gpu::DeviceIdentification::DEVICE_GPU, eid, io);
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 Aghera6b712322025-07-31 19:25:12 +053076 peakPower = std::make_shared<NvidiaGpuPowerPeakReading>(
77 mctpRequester, name + "_Power_0", eid, gpuPeakPowerSensorId,
78 objectServer);
79
Harshit Aghera775199d2025-05-27 14:20:24 +053080 energySensor = std::make_shared<NvidiaGpuEnergySensor>(
81 conn, mctpRequester, name + "_Energy_0", path, eid, gpuEnergySensorId,
82 objectServer, std::vector<thresholds::Threshold>{});
83
Harshit Agherabef4d412025-05-27 14:53:56 +053084 voltageSensor = std::make_shared<NvidiaGpuVoltageSensor>(
85 conn, mctpRequester, name + "_Voltage_0", path, eid, gpuVoltageSensorId,
86 objectServer, std::vector<thresholds::Threshold>{});
87
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053088 lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME",
89 name, "PATH", path);
90
91 read();
92}
93
Harshit Aghera5e7decc2025-05-07 16:20:16 +053094void GpuDevice::processTLimitThresholds(uint8_t rc,
95 const std::vector<int32_t>& thresholds)
96{
97 std::vector<thresholds::Threshold> tLimitThresholds{};
98 if (rc == 0)
99 {
100 tLimitThresholds = {
101 thresholds::Threshold{thresholds::Level::WARNING,
102 thresholds::Direction::LOW,
103 static_cast<double>(thresholds[0])},
104 thresholds::Threshold{thresholds::Level::CRITICAL,
105 thresholds::Direction::LOW,
106 static_cast<double>(thresholds[1])},
107 thresholds::Threshold{thresholds::Level::HARDSHUTDOWN,
108 thresholds::Direction::LOW,
109 static_cast<double>(thresholds[2])}};
110 }
111
112 tLimitSensor = std::make_shared<NvidiaGpuTempSensor>(
113 conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId,
114 objectServer, std::move(tLimitThresholds));
115}
116
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530117void GpuDevice::read()
118{
119 tempSensor->update();
Harshit Aghera5e7decc2025-05-07 16:20:16 +0530120 if (tLimitSensor)
121 {
122 tLimitSensor->update();
123 }
Harshit Agherab10a67b2025-05-27 12:19:29 +0530124 dramTempSensor->update();
Harshit Aghera902c6492025-05-08 15:57:42 +0530125 powerSensor->update();
Harshit Aghera6b712322025-07-31 19:25:12 +0530126 peakPower->update();
Harshit Aghera775199d2025-05-27 14:20:24 +0530127 energySensor->update();
Harshit Agherabef4d412025-05-27 14:53:56 +0530128 voltageSensor->update();
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530129
130 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
131 waitTimer.async_wait([this](const boost::system::error_code& ec) {
132 if (ec)
133 {
134 return;
135 }
136 read();
137 });
Marc Olberdingd0125c92025-10-08 14:37:19 -0700138};