Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +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 "NvidiaGpuPowerSensor.hpp" |
| 8 | |
| 9 | #include "MctpRequester.hpp" |
| 10 | #include "SensorPaths.hpp" |
| 11 | #include "Thresholds.hpp" |
| 12 | #include "Utils.hpp" |
| 13 | #include "sensor.hpp" |
| 14 | |
| 15 | #include <bits/basic_string.h> |
| 16 | |
| 17 | #include <NvidiaDeviceDiscovery.hpp> |
| 18 | #include <NvidiaGpuMctpVdm.hpp> |
| 19 | #include <OcpMctpVdm.hpp> |
| 20 | #include <phosphor-logging/lg2.hpp> |
| 21 | #include <sdbusplus/asio/connection.hpp> |
| 22 | #include <sdbusplus/asio/object_server.hpp> |
| 23 | |
| 24 | #include <cstddef> |
| 25 | #include <cstdint> |
| 26 | #include <functional> |
| 27 | #include <limits> |
| 28 | #include <memory> |
| 29 | #include <string> |
| 30 | #include <utility> |
| 31 | #include <vector> |
| 32 | |
| 33 | using namespace std::literals; |
| 34 | |
| 35 | // GPU Power Sensor Averaging Interval in seconds, 0 implies default |
| 36 | constexpr uint8_t gpuPowerAveragingIntervalInSec{0}; |
| 37 | |
Harshit Aghera | 5e4d305 | 2025-06-19 11:28:38 +0530 | [diff] [blame] | 38 | static constexpr double gpuPowerSensorMaxReading = 5000; |
Harshit Aghera | 902c649 | 2025-05-08 15:57:42 +0530 | [diff] [blame] | 39 | static constexpr double gpuPowerSensorMinReading = |
| 40 | std::numeric_limits<uint32_t>::min(); |
| 41 | |
| 42 | NvidiaGpuPowerSensor::NvidiaGpuPowerSensor( |
| 43 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 44 | mctp::MctpRequester& mctpRequester, const std::string& name, |
| 45 | const std::string& sensorConfiguration, uint8_t eid, uint8_t sensorId, |
| 46 | sdbusplus::asio::object_server& objectServer, |
| 47 | std::vector<thresholds::Threshold>&& thresholdData) : |
| 48 | Sensor(escapeName(name), std::move(thresholdData), sensorConfiguration, |
| 49 | "power", false, true, gpuPowerSensorMaxReading, |
| 50 | gpuPowerSensorMinReading, conn), |
| 51 | eid(eid), sensorId{sensorId}, |
| 52 | averagingInterval{gpuPowerAveragingIntervalInSec}, |
| 53 | mctpRequester(mctpRequester), objectServer(objectServer) |
| 54 | |
| 55 | { |
| 56 | std::string dbusPath = sensorPathPrefix + "power/"s + escapeName(name); |
| 57 | |
| 58 | sensorInterface = objectServer.add_interface( |
| 59 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
| 60 | |
| 61 | for (const auto& threshold : thresholds) |
| 62 | { |
| 63 | std::string interface = thresholds::getInterface(threshold.level); |
| 64 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 65 | objectServer.add_interface(dbusPath, interface); |
| 66 | } |
| 67 | |
| 68 | association = objectServer.add_interface(dbusPath, association::interface); |
| 69 | |
| 70 | setInitialProperties(sensor_paths::unitWatts); |
| 71 | } |
| 72 | |
| 73 | NvidiaGpuPowerSensor::~NvidiaGpuPowerSensor() |
| 74 | { |
| 75 | for (const auto& iface : thresholdInterfaces) |
| 76 | { |
| 77 | objectServer.remove_interface(iface); |
| 78 | } |
| 79 | objectServer.remove_interface(association); |
| 80 | objectServer.remove_interface(sensorInterface); |
| 81 | } |
| 82 | |
| 83 | void NvidiaGpuPowerSensor::checkThresholds() |
| 84 | { |
| 85 | thresholds::checkThresholds(this); |
| 86 | } |
| 87 | |
| 88 | void NvidiaGpuPowerSensor::processResponse(int sendRecvMsgResult) |
| 89 | { |
| 90 | if (sendRecvMsgResult != 0) |
| 91 | { |
| 92 | lg2::error( |
| 93 | "Error updating Power Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}", |
| 94 | "EID", eid, "SID", sensorId, "RC", sendRecvMsgResult); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | ocp::accelerator_management::CompletionCode cc{}; |
| 99 | uint16_t reasonCode = 0; |
| 100 | uint32_t power = 0; |
| 101 | |
| 102 | const int rc = |
| 103 | gpu::decodeGetCurrentPowerDrawResponse(response, cc, reasonCode, power); |
| 104 | |
| 105 | if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 106 | { |
| 107 | lg2::error( |
| 108 | "Error updating Power Sensor eid {EID} and sensor id {SID} : decode failed, rc={RC}, cc={CC}, reasonCode={RESC}", |
| 109 | "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC", |
| 110 | reasonCode); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | // Reading from the device is in milliwatts and unit set on the dbus |
| 115 | // is watts. |
| 116 | updateValue(power / 1000.0); |
| 117 | } |
| 118 | |
| 119 | void NvidiaGpuPowerSensor::update() |
| 120 | { |
| 121 | const int rc = gpu::encodeGetCurrentPowerDrawRequest( |
| 122 | 0, sensorId, averagingInterval, request); |
| 123 | |
| 124 | if (rc != 0) |
| 125 | { |
| 126 | lg2::error( |
| 127 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}", |
| 128 | "EID", eid, "SID", sensorId, "RC", rc); |
| 129 | } |
| 130 | |
| 131 | mctpRequester.sendRecvMsg( |
| 132 | eid, request, response, |
| 133 | [this](int sendRecvMsgResult) { processResponse(sendRecvMsgResult); }); |
| 134 | } |