| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 1 | /* |
| Ed Tanous | b5e823f | 2025-10-09 20:28:42 -0400 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright Copyright OpenBMC Authors |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | */ |
| 5 | |
| 6 | #include "NvidiaGpuPowerPeakReading.hpp" |
| 7 | |
| 8 | #include "MctpRequester.hpp" |
| 9 | #include "Utils.hpp" |
| 10 | |
| 11 | #include <bits/basic_string.h> |
| 12 | |
| 13 | #include <NvidiaDeviceDiscovery.hpp> |
| 14 | #include <NvidiaGpuMctpVdm.hpp> |
| 15 | #include <OcpMctpVdm.hpp> |
| 16 | #include <phosphor-logging/lg2.hpp> |
| 17 | #include <sdbusplus/asio/object_server.hpp> |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <functional> |
| 21 | #include <memory> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 22 | #include <span> |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 23 | #include <string> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 24 | #include <system_error> |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 25 | |
| 26 | using namespace std::literals; |
| 27 | |
| 28 | NvidiaGpuPowerPeakReading::NvidiaGpuPowerPeakReading( |
| 29 | mctp::MctpRequester& mctpRequester, const std::string& name, uint8_t eid, |
| 30 | uint8_t sensorId, sdbusplus::asio::object_server& objectServer) : |
| 31 | eid(eid), sensorId{sensorId}, mctpRequester(mctpRequester), |
| 32 | objectServer(objectServer) |
| 33 | { |
| 34 | std::string dbusPath = sensorPathPrefix + "power/"s + escapeName(name); |
| 35 | |
| 36 | telemetryReportInterface = objectServer.add_interface( |
| 37 | dbusPath, "xyz.openbmc_project.Telemetry.Report"); |
| 38 | |
| 39 | std::get<0>(readings) = 0; |
| 40 | // Reading from the device is in milliwatts and unit set on the dbus |
| 41 | // is watts. |
| 42 | std::get<1>(readings).emplace_back("PeakReading", "", 0.0, 0); |
| 43 | |
| 44 | telemetryReportInterface->register_property("Readings", readings); |
| 45 | |
| 46 | telemetryReportInterface->initialize(); |
| 47 | } |
| 48 | |
| 49 | NvidiaGpuPowerPeakReading::~NvidiaGpuPowerPeakReading() |
| 50 | { |
| 51 | objectServer.remove_interface(telemetryReportInterface); |
| 52 | } |
| 53 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 54 | void NvidiaGpuPowerPeakReading::processResponse(const std::error_code& ec, |
| 55 | std::span<const uint8_t> buffer) |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 56 | { |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 57 | if (ec) |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 58 | { |
| 59 | lg2::error( |
| 60 | "Error updating Peak Power Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}", |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 61 | "EID", eid, "SID", sensorId, "RC", ec.message()); |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | ocp::accelerator_management::CompletionCode cc{}; |
| 66 | uint16_t reasonCode = 0; |
| 67 | uint32_t peakPower = 0; |
| 68 | |
| 69 | const int rc = |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 70 | gpu::decodeGetPowerDrawResponse(buffer, cc, reasonCode, peakPower); |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 71 | |
| 72 | if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 73 | { |
| 74 | lg2::error( |
| 75 | "Error updating Peak Power Sensor eid {EID} and sensor id {SID} : decode failed, rc={RC}, cc={CC}, reasonCode={RESC}", |
| 76 | "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC", |
| 77 | reasonCode); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Reading from the device is in milliwatts and unit set on the dbus |
| 82 | // is watts. |
| 83 | std::get<2>(std::get<1>(readings)[0]) = peakPower / 1000.0; |
| 84 | |
| 85 | telemetryReportInterface->set_property("Readings", readings); |
| 86 | } |
| 87 | |
| 88 | void NvidiaGpuPowerPeakReading::update() |
| 89 | { |
| 90 | const int rc = gpu::encodeGetPowerDrawRequest( |
| 91 | gpu::PlatformEnvironmentalCommands::GET_MAX_OBSERVED_POWER, 0, sensorId, |
| 92 | averagingInterval, request); |
| 93 | |
| 94 | if (rc != 0) |
| 95 | { |
| 96 | lg2::error( |
| 97 | "Error updating Peak Power Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}", |
| 98 | "EID", eid, "SID", sensorId, "RC", rc); |
| 99 | } |
| 100 | |
| 101 | mctpRequester.sendRecvMsg( |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 102 | eid, request, |
| 103 | [this](const std::error_code& ec, std::span<const uint8_t> buffer) { |
| 104 | processResponse(ec, buffer); |
| 105 | }); |
| Harshit Aghera | 6b71232 | 2025-07-31 19:25:12 +0530 | [diff] [blame] | 106 | } |