blob: a2d2d3c46a3a54b095a1e74e09b0ea5e6847fff9 [file] [log] [blame]
Harshit Aghera6b712322025-07-31 19:25:12 +05301/*
Ed Tanousb5e823f2025-10-09 20:28:42 -04002 * SPDX-FileCopyrightText: Copyright Copyright OpenBMC Authors
Harshit Aghera6b712322025-07-31 19:25:12 +05303 * 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 Olberdingd0125c92025-10-08 14:37:19 -070022#include <span>
Harshit Aghera6b712322025-07-31 19:25:12 +053023#include <string>
Marc Olberdingd0125c92025-10-08 14:37:19 -070024#include <system_error>
Harshit Aghera6b712322025-07-31 19:25:12 +053025
26using namespace std::literals;
27
28NvidiaGpuPowerPeakReading::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
49NvidiaGpuPowerPeakReading::~NvidiaGpuPowerPeakReading()
50{
51 objectServer.remove_interface(telemetryReportInterface);
52}
53
Marc Olberdingd0125c92025-10-08 14:37:19 -070054void NvidiaGpuPowerPeakReading::processResponse(const std::error_code& ec,
55 std::span<const uint8_t> buffer)
Harshit Aghera6b712322025-07-31 19:25:12 +053056{
Marc Olberdingd0125c92025-10-08 14:37:19 -070057 if (ec)
Harshit Aghera6b712322025-07-31 19:25:12 +053058 {
59 lg2::error(
60 "Error updating Peak Power Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}",
Marc Olberdingd0125c92025-10-08 14:37:19 -070061 "EID", eid, "SID", sensorId, "RC", ec.message());
Harshit Aghera6b712322025-07-31 19:25:12 +053062 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 Olberdingd0125c92025-10-08 14:37:19 -070070 gpu::decodeGetPowerDrawResponse(buffer, cc, reasonCode, peakPower);
Harshit Aghera6b712322025-07-31 19:25:12 +053071
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
88void 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 Olberdingd0125c92025-10-08 14:37:19 -0700102 eid, request,
103 [this](const std::error_code& ec, std::span<const uint8_t> buffer) {
104 processResponse(ec, buffer);
105 });
Harshit Aghera6b712322025-07-31 19:25:12 +0530106}