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