blob: bac935d2e3e330dd23123e0a742b56545d9c69ae [file] [log] [blame]
Harshit Aghera902c6492025-05-08 15:57:42 +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 "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
33using namespace std::literals;
34
Harshit Aghera5e4d3052025-06-19 11:28:38 +053035static constexpr double gpuPowerSensorMaxReading = 5000;
Harshit Aghera902c6492025-05-08 15:57:42 +053036static constexpr double gpuPowerSensorMinReading =
37 std::numeric_limits<uint32_t>::min();
38
39NvidiaGpuPowerSensor::NvidiaGpuPowerSensor(
40 std::shared_ptr<sdbusplus::asio::connection>& conn,
41 mctp::MctpRequester& mctpRequester, const std::string& name,
42 const std::string& sensorConfiguration, uint8_t eid, uint8_t sensorId,
43 sdbusplus::asio::object_server& objectServer,
44 std::vector<thresholds::Threshold>&& thresholdData) :
45 Sensor(escapeName(name), std::move(thresholdData), sensorConfiguration,
46 "power", false, true, gpuPowerSensorMaxReading,
47 gpuPowerSensorMinReading, conn),
48 eid(eid), sensorId{sensorId},
Ed Tanousaba6fca2025-09-29 13:53:20 -070049
Harshit Aghera902c6492025-05-08 15:57:42 +053050 mctpRequester(mctpRequester), objectServer(objectServer)
51
52{
53 std::string dbusPath = sensorPathPrefix + "power/"s + escapeName(name);
54
55 sensorInterface = objectServer.add_interface(
56 dbusPath, "xyz.openbmc_project.Sensor.Value");
57
58 for (const auto& threshold : thresholds)
59 {
60 std::string interface = thresholds::getInterface(threshold.level);
61 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
62 objectServer.add_interface(dbusPath, interface);
63 }
64
65 association = objectServer.add_interface(dbusPath, association::interface);
66
67 setInitialProperties(sensor_paths::unitWatts);
68}
69
70NvidiaGpuPowerSensor::~NvidiaGpuPowerSensor()
71{
72 for (const auto& iface : thresholdInterfaces)
73 {
74 objectServer.remove_interface(iface);
75 }
76 objectServer.remove_interface(association);
77 objectServer.remove_interface(sensorInterface);
78}
79
80void NvidiaGpuPowerSensor::checkThresholds()
81{
82 thresholds::checkThresholds(this);
83}
84
85void NvidiaGpuPowerSensor::processResponse(int sendRecvMsgResult)
86{
87 if (sendRecvMsgResult != 0)
88 {
89 lg2::error(
90 "Error updating Power Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}",
91 "EID", eid, "SID", sensorId, "RC", sendRecvMsgResult);
92 return;
93 }
94
95 ocp::accelerator_management::CompletionCode cc{};
96 uint16_t reasonCode = 0;
97 uint32_t power = 0;
98
99 const int rc =
Harshit Aghera6b712322025-07-31 19:25:12 +0530100 gpu::decodeGetPowerDrawResponse(response, cc, reasonCode, power);
Harshit Aghera902c6492025-05-08 15:57:42 +0530101
102 if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS)
103 {
104 lg2::error(
105 "Error updating Power Sensor eid {EID} and sensor id {SID} : decode failed, rc={RC}, cc={CC}, reasonCode={RESC}",
106 "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC",
107 reasonCode);
108 return;
109 }
110
111 // Reading from the device is in milliwatts and unit set on the dbus
112 // is watts.
113 updateValue(power / 1000.0);
114}
115
116void NvidiaGpuPowerSensor::update()
117{
Harshit Aghera6b712322025-07-31 19:25:12 +0530118 const int rc = gpu::encodeGetPowerDrawRequest(
119 gpu::PlatformEnvironmentalCommands::GET_CURRENT_POWER_DRAW, 0, sensorId,
120 averagingInterval, request);
Harshit Aghera902c6492025-05-08 15:57:42 +0530121
122 if (rc != 0)
123 {
124 lg2::error(
125 "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}",
126 "EID", eid, "SID", sensorId, "RC", rc);
127 }
128
129 mctpRequester.sendRecvMsg(
130 eid, request, response,
131 [this](int sendRecvMsgResult) { processResponse(sendRecvMsgResult); });
132}