blob: 26fb1c4b74b89368cf1b64807dc2b867e73410a8 [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>
Marc Olberdingd0125c92025-10-08 14:37:19 -070028#include <span>
Harshit Agherad837b562025-04-21 19:50:10 +053029#include <string>
Marc Olberdingd0125c92025-10-08 14:37:19 -070030#include <system_error>
Harshit Agherad837b562025-04-21 19:50:10 +053031#include <utility>
Harshit Agherad837b562025-04-21 19:50:10 +053032#include <vector>
33
34using namespace std::literals;
35
36static constexpr double gpuTempSensorMaxReading = 127;
37static constexpr double gpuTempSensorMinReading = -128;
38
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053039NvidiaGpuTempSensor::NvidiaGpuTempSensor(
Harshit Agherad837b562025-04-21 19:50:10 +053040 std::shared_ptr<sdbusplus::asio::connection>& conn,
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053041 mctp::MctpRequester& mctpRequester, const std::string& name,
Harshit Agheraba138da2025-05-05 12:26:35 +053042 const std::string& sensorConfiguration, const uint8_t eid, uint8_t sensorId,
Harshit Agherad837b562025-04-21 19:50:10 +053043 sdbusplus::asio::object_server& objectServer,
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053044 std::vector<thresholds::Threshold>&& thresholdData) :
Harshit Agherad837b562025-04-21 19:50:10 +053045 Sensor(escapeName(name), std::move(thresholdData), sensorConfiguration,
46 "temperature", false, true, gpuTempSensorMaxReading,
47 gpuTempSensorMinReading, conn),
Harshit Agheraba138da2025-05-05 12:26:35 +053048 eid(eid), sensorId{sensorId}, mctpRequester(mctpRequester),
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053049 objectServer(objectServer)
Harshit Agherad837b562025-04-21 19:50:10 +053050{
51 std::string dbusPath =
52 sensorPathPrefix + "temperature/"s + escapeName(name);
53
54 sensorInterface = objectServer.add_interface(
55 dbusPath, "xyz.openbmc_project.Sensor.Value");
56
57 for (const auto& threshold : thresholds)
58 {
59 std::string interface = thresholds::getInterface(threshold.level);
60 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
61 objectServer.add_interface(dbusPath, interface);
62 }
63
64 association = objectServer.add_interface(dbusPath, association::interface);
65
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053066 setInitialProperties(sensor_paths::unitDegreesC);
Harshit Agherad837b562025-04-21 19:50:10 +053067}
68
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053069NvidiaGpuTempSensor::~NvidiaGpuTempSensor()
Harshit Agherad837b562025-04-21 19:50:10 +053070{
Harshit Agherad837b562025-04-21 19:50:10 +053071 for (const auto& iface : thresholdInterfaces)
72 {
73 objectServer.remove_interface(iface);
74 }
75 objectServer.remove_interface(association);
76 objectServer.remove_interface(sensorInterface);
77}
78
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053079void NvidiaGpuTempSensor::checkThresholds()
Harshit Agherad837b562025-04-21 19:50:10 +053080{
81 thresholds::checkThresholds(this);
82}
83
Marc Olberdingd0125c92025-10-08 14:37:19 -070084void NvidiaGpuTempSensor::processResponse(const std::error_code& ec,
85 std::span<const uint8_t> buffer)
Harshit Aghera560e6af2025-04-21 20:04:56 +053086{
Marc Olberdingd0125c92025-10-08 14:37:19 -070087 if (ec)
Harshit Aghera560e6af2025-04-21 20:04:56 +053088 {
89 lg2::error(
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053090 "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}",
Marc Olberdingd0125c92025-10-08 14:37:19 -070091 "EID", eid, "SID", sensorId, "RC", ec.message());
Harshit Aghera560e6af2025-04-21 20:04:56 +053092 return;
93 }
94
95 ocp::accelerator_management::CompletionCode cc{};
96 uint16_t reasonCode = 0;
97 double tempValue = 0;
98
Marc Olberdingd0125c92025-10-08 14:37:19 -070099 auto rc = gpu::decodeGetTemperatureReadingResponse(buffer, cc, reasonCode,
100 tempValue);
Harshit Aghera560e6af2025-04-21 20:04:56 +0530101
102 if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS)
103 {
104 lg2::error(
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530105 "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : decode failed. "
106 "rc={RC}, cc={CC}, reasonCode={RESC}",
107 "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC",
108 reasonCode);
Harshit Aghera560e6af2025-04-21 20:04:56 +0530109 return;
110 }
111
112 updateValue(tempValue);
113}
114
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530115void NvidiaGpuTempSensor::update()
Harshit Aghera560e6af2025-04-21 20:04:56 +0530116{
117 auto rc = gpu::encodeGetTemperatureReadingRequest(
118 0, sensorId, getTemperatureReadingRequest);
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530119
Harshit Aghera560e6af2025-04-21 20:04:56 +0530120 if (rc != 0)
121 {
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +0530122 lg2::error(
123 "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}",
124 "EID", eid, "SID", sensorId, "RC", rc);
Harshit Aghera560e6af2025-04-21 20:04:56 +0530125 }
126
127 mctpRequester.sendRecvMsg(
Marc Olberdingd0125c92025-10-08 14:37:19 -0700128 eid, getTemperatureReadingRequest,
129 [this](const std::error_code& ec, std::span<const uint8_t> buffer) {
130 processResponse(ec, buffer);
131 });
Harshit Aghera560e6af2025-04-21 20:04:56 +0530132}