blob: 38530484b1c72ab3f0e14f07ba70f085027f8acb [file] [log] [blame]
Harshit Agherad837b562025-04-21 19:50:10 +05301/*
2 * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
Harshit Aghera560e6af2025-04-21 20:04:56 +05303 * AFFILIATES. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
Harshit Agherad837b562025-04-21 19:50:10 +05305 */
6
7#include "NvidiaGpuSensor.hpp"
8
Harshit Aghera560e6af2025-04-21 20:04:56 +05309#include "SensorPaths.hpp"
Harshit Agherad837b562025-04-21 19:50:10 +053010#include "Thresholds.hpp"
11#include "Utils.hpp"
12#include "sensor.hpp"
13
14#include <bits/basic_string.h>
15
Harshit Aghera560e6af2025-04-21 20:04:56 +053016#include <MctpRequester.hpp>
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053017#include <NvidiaDeviceDiscovery.hpp>
Harshit Aghera560e6af2025-04-21 20:04:56 +053018#include <NvidiaGpuMctpVdm.hpp>
19#include <OcpMctpVdm.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053020#include <phosphor-logging/lg2.hpp>
21#include <sdbusplus/asio/connection.hpp>
22#include <sdbusplus/asio/object_server.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053023
Harshit Agherad837b562025-04-21 19:50:10 +053024#include <cstddef>
25#include <cstdint>
Harshit Aghera560e6af2025-04-21 20:04:56 +053026#include <functional>
Harshit Agherad837b562025-04-21 19:50:10 +053027#include <memory>
28#include <string>
29#include <utility>
Harshit Agherad837b562025-04-21 19:50:10 +053030#include <vector>
31
32using namespace std::literals;
33
34static constexpr double gpuTempSensorMaxReading = 127;
35static constexpr double gpuTempSensorMinReading = -128;
36
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053037NvidiaGpuTempSensor::NvidiaGpuTempSensor(
Harshit Agherad837b562025-04-21 19:50:10 +053038 std::shared_ptr<sdbusplus::asio::connection>& conn,
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053039 mctp::MctpRequester& mctpRequester, const std::string& name,
Harshit Agheraba138da2025-05-05 12:26:35 +053040 const std::string& sensorConfiguration, const uint8_t eid, uint8_t sensorId,
Harshit Agherad837b562025-04-21 19:50:10 +053041 sdbusplus::asio::object_server& objectServer,
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053042 std::vector<thresholds::Threshold>&& thresholdData) :
Harshit Agherad837b562025-04-21 19:50:10 +053043 Sensor(escapeName(name), std::move(thresholdData), sensorConfiguration,
44 "temperature", false, true, gpuTempSensorMaxReading,
45 gpuTempSensorMinReading, conn),
Harshit Agheraba138da2025-05-05 12:26:35 +053046 eid(eid), sensorId{sensorId}, mctpRequester(mctpRequester),
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053047 objectServer(objectServer)
Harshit Agherad837b562025-04-21 19:50:10 +053048{
49 std::string dbusPath =
50 sensorPathPrefix + "temperature/"s + escapeName(name);
51
52 sensorInterface = objectServer.add_interface(
53 dbusPath, "xyz.openbmc_project.Sensor.Value");
54
55 for (const auto& threshold : thresholds)
56 {
57 std::string interface = thresholds::getInterface(threshold.level);
58 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
59 objectServer.add_interface(dbusPath, interface);
60 }
61
62 association = objectServer.add_interface(dbusPath, association::interface);
63
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053064 setInitialProperties(sensor_paths::unitDegreesC);
Harshit Agherad837b562025-04-21 19:50:10 +053065}
66
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053067NvidiaGpuTempSensor::~NvidiaGpuTempSensor()
Harshit Agherad837b562025-04-21 19:50:10 +053068{
Harshit Agherad837b562025-04-21 19:50:10 +053069 for (const auto& iface : thresholdInterfaces)
70 {
71 objectServer.remove_interface(iface);
72 }
73 objectServer.remove_interface(association);
74 objectServer.remove_interface(sensorInterface);
75}
76
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053077void NvidiaGpuTempSensor::checkThresholds()
Harshit Agherad837b562025-04-21 19:50:10 +053078{
79 thresholds::checkThresholds(this);
80}
81
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053082void NvidiaGpuTempSensor::processResponse(int sendRecvMsgResult)
Harshit Aghera560e6af2025-04-21 20:04:56 +053083{
84 if (sendRecvMsgResult != 0)
85 {
86 lg2::error(
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053087 "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}",
88 "EID", eid, "SID", sensorId, "RC", sendRecvMsgResult);
Harshit Aghera560e6af2025-04-21 20:04:56 +053089 return;
90 }
91
92 ocp::accelerator_management::CompletionCode cc{};
93 uint16_t reasonCode = 0;
94 double tempValue = 0;
95
96 auto rc = gpu::decodeGetTemperatureReadingResponse(
97 getTemperatureReadingResponse, cc, reasonCode, tempValue);
98
99 if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS)
100 {
101 lg2::error(
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530102 "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : decode failed. "
103 "rc={RC}, cc={CC}, reasonCode={RESC}",
104 "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC",
105 reasonCode);
Harshit Aghera560e6af2025-04-21 20:04:56 +0530106 return;
107 }
108
109 updateValue(tempValue);
110}
111
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530112void NvidiaGpuTempSensor::update()
Harshit Aghera560e6af2025-04-21 20:04:56 +0530113{
114 auto rc = gpu::encodeGetTemperatureReadingRequest(
115 0, sensorId, getTemperatureReadingRequest);
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530116
Harshit Aghera560e6af2025-04-21 20:04:56 +0530117 if (rc != 0)
118 {
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530119 lg2::error(
120 "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}",
121 "EID", eid, "SID", sensorId, "RC", rc);
Harshit Aghera560e6af2025-04-21 20:04:56 +0530122 }
123
124 mctpRequester.sendRecvMsg(
125 eid, getTemperatureReadingRequest, getTemperatureReadingResponse,
126 [this](int sendRecvMsgResult) { processResponse(sendRecvMsgResult); });
127}