blob: 839dfe4cc9d3ed95efa80a4883fcf9e8107e59ad [file] [log] [blame]
Harshit Aghera6b712322025-07-31 19:25:12 +05301/*
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 Olberdingd0125c92025-10-08 14:37:19 -070023#include <span>
Harshit Aghera6b712322025-07-31 19:25:12 +053024#include <string>
Marc Olberdingd0125c92025-10-08 14:37:19 -070025#include <system_error>
Harshit Aghera6b712322025-07-31 19:25:12 +053026
27using namespace std::literals;
28
29NvidiaGpuPowerPeakReading::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
50NvidiaGpuPowerPeakReading::~NvidiaGpuPowerPeakReading()
51{
52 objectServer.remove_interface(telemetryReportInterface);
53}
54
Marc Olberdingd0125c92025-10-08 14:37:19 -070055void NvidiaGpuPowerPeakReading::processResponse(const std::error_code& ec,
56 std::span<const uint8_t> buffer)
Harshit Aghera6b712322025-07-31 19:25:12 +053057{
Marc Olberdingd0125c92025-10-08 14:37:19 -070058 if (ec)
Harshit Aghera6b712322025-07-31 19:25:12 +053059 {
60 lg2::error(
61 "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 -070062 "EID", eid, "SID", sensorId, "RC", ec.message());
Harshit Aghera6b712322025-07-31 19:25:12 +053063 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 Olberdingd0125c92025-10-08 14:37:19 -070071 gpu::decodeGetPowerDrawResponse(buffer, cc, reasonCode, peakPower);
Harshit Aghera6b712322025-07-31 19:25:12 +053072
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
89void 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 Olberdingd0125c92025-10-08 14:37:19 -0700103 eid, request,
104 [this](const std::error_code& ec, std::span<const uint8_t> buffer) {
105 processResponse(ec, buffer);
106 });
Harshit Aghera6b712322025-07-31 19:25:12 +0530107}