Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 1 | /* |
| 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 Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame^] | 17 | #include <NvidiaGpuPowerSensor.hpp> |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 18 | #include <NvidiaGpuThresholds.hpp> |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 19 | #include <boost/asio/io_context.hpp> |
| 20 | #include <phosphor-logging/lg2.hpp> |
| 21 | #include <sdbusplus/asio/connection.hpp> |
| 22 | #include <sdbusplus/asio/object_server.hpp> |
| 23 | |
| 24 | #include <chrono> |
| 25 | #include <cstdint> |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 26 | #include <functional> |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <string> |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 29 | #include <utility> |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
| 32 | GpuDevice::GpuDevice(const SensorConfigs& configs, const std::string& name, |
| 33 | const std::string& path, |
| 34 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 35 | uint8_t eid, boost::asio::io_context& io, |
| 36 | mctp::MctpRequester& mctpRequester, |
| 37 | sdbusplus::asio::object_server& objectServer) : |
| 38 | eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}), |
| 39 | waitTimer(io, std::chrono::steady_clock::duration(0)), |
| 40 | mctpRequester(mctpRequester), conn(conn), objectServer(objectServer), |
| 41 | configs(configs), name(escapeName(name)), path(path) |
| 42 | { |
| 43 | makeSensors(); |
| 44 | } |
| 45 | |
| 46 | void GpuDevice::makeSensors() |
| 47 | { |
| 48 | tempSensor = std::make_shared<NvidiaGpuTempSensor>( |
Harshit Aghera | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 49 | conn, mctpRequester, name + "_TEMP_0", path, eid, gpuTempSensorId, |
| 50 | objectServer, std::vector<thresholds::Threshold>{}); |
| 51 | |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 52 | readThermalParameters( |
| 53 | eid, |
| 54 | std::vector<gpuThresholdId>{gpuTLimitWarnringThresholdId, |
| 55 | gpuTLimitCriticalThresholdId, |
| 56 | gpuTLimitHardshutDownThresholdId}, |
| 57 | mctpRequester, |
| 58 | std::bind_front(&GpuDevice::processTLimitThresholds, this)); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 59 | |
Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame^] | 60 | powerSensor = std::make_shared<NvidiaGpuPowerSensor>( |
| 61 | conn, mctpRequester, name + "_Power_0", path, eid, gpuPowerSensorId, |
| 62 | objectServer, std::vector<thresholds::Threshold>{}); |
| 63 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 64 | lg2::info("Added GPU {NAME} Sensors with chassis path: {PATH}.", "NAME", |
| 65 | name, "PATH", path); |
| 66 | |
| 67 | read(); |
| 68 | } |
| 69 | |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 70 | void GpuDevice::processTLimitThresholds(uint8_t rc, |
| 71 | const std::vector<int32_t>& thresholds) |
| 72 | { |
| 73 | std::vector<thresholds::Threshold> tLimitThresholds{}; |
| 74 | if (rc == 0) |
| 75 | { |
| 76 | tLimitThresholds = { |
| 77 | thresholds::Threshold{thresholds::Level::WARNING, |
| 78 | thresholds::Direction::LOW, |
| 79 | static_cast<double>(thresholds[0])}, |
| 80 | thresholds::Threshold{thresholds::Level::CRITICAL, |
| 81 | thresholds::Direction::LOW, |
| 82 | static_cast<double>(thresholds[1])}, |
| 83 | thresholds::Threshold{thresholds::Level::HARDSHUTDOWN, |
| 84 | thresholds::Direction::LOW, |
| 85 | static_cast<double>(thresholds[2])}}; |
| 86 | } |
| 87 | |
| 88 | tLimitSensor = std::make_shared<NvidiaGpuTempSensor>( |
| 89 | conn, mctpRequester, name + "_TEMP_1", path, eid, gpuTLimitSensorId, |
| 90 | objectServer, std::move(tLimitThresholds)); |
| 91 | } |
| 92 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 93 | void GpuDevice::read() |
| 94 | { |
| 95 | tempSensor->update(); |
Harshit Aghera | 5e7decc | 2025-05-07 16:20:16 +0530 | [diff] [blame] | 96 | if (tLimitSensor) |
| 97 | { |
| 98 | tLimitSensor->update(); |
| 99 | } |
Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame^] | 100 | powerSensor->update(); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 101 | |
| 102 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); |
| 103 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 104 | if (ec) |
| 105 | { |
| 106 | return; |
| 107 | } |
| 108 | read(); |
| 109 | }); |
| 110 | } |