Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 3 | * AFFILIATES. All rights reserved. |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include "NvidiaGpuSensor.hpp" |
| 8 | |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 9 | #include "SensorPaths.hpp" |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 10 | #include "Thresholds.hpp" |
| 11 | #include "Utils.hpp" |
| 12 | #include "sensor.hpp" |
| 13 | |
| 14 | #include <bits/basic_string.h> |
| 15 | |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 16 | #include <MctpRequester.hpp> |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 17 | #include <NvidiaDeviceDiscovery.hpp> |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 18 | #include <NvidiaGpuMctpVdm.hpp> |
| 19 | #include <OcpMctpVdm.hpp> |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 20 | #include <phosphor-logging/lg2.hpp> |
| 21 | #include <sdbusplus/asio/connection.hpp> |
| 22 | #include <sdbusplus/asio/object_server.hpp> |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 23 | |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 24 | #include <cstddef> |
| 25 | #include <cstdint> |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 26 | #include <functional> |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <string> |
| 29 | #include <utility> |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
| 32 | using namespace std::literals; |
| 33 | |
| 34 | static constexpr double gpuTempSensorMaxReading = 127; |
| 35 | static constexpr double gpuTempSensorMinReading = -128; |
| 36 | |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 37 | NvidiaGpuTempSensor::NvidiaGpuTempSensor( |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 38 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 39 | mctp::MctpRequester& mctpRequester, const std::string& name, |
Harshit Aghera | 0e1718c | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 40 | const std::string& sensorConfiguration, const uint8_t eid, uint8_t sensorId, |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 41 | sdbusplus::asio::object_server& objectServer, |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 42 | std::vector<thresholds::Threshold>&& thresholdData) : |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 43 | Sensor(escapeName(name), std::move(thresholdData), sensorConfiguration, |
| 44 | "temperature", false, true, gpuTempSensorMaxReading, |
| 45 | gpuTempSensorMinReading, conn), |
Harshit Aghera | 0e1718c | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 46 | eid(eid), sensorId{sensorId}, mctpRequester(mctpRequester), |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 47 | objectServer(objectServer) |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 48 | { |
| 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 Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 64 | // Sensor values are only updated when the difference between the new and |
| 65 | // previous value exceeds the hysteresisPublish threshold. This threshold |
| 66 | // defaults to ((max - min) * 0.0001). Since this sensor lacks defined |
| 67 | // min/max values, theoretical limits are used instead, creating a large |
| 68 | // hysteresisPublish value that blocks D-Bus updates. Setting |
| 69 | // hysteresisPublish to 0 forces all sensor value changes to be published |
| 70 | // to D-Bus. |
| 71 | hysteresisPublish = 0; |
| 72 | |
| 73 | setInitialProperties(sensor_paths::unitDegreesC); |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 74 | } |
| 75 | |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 76 | NvidiaGpuTempSensor::~NvidiaGpuTempSensor() |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 77 | { |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 78 | for (const auto& iface : thresholdInterfaces) |
| 79 | { |
| 80 | objectServer.remove_interface(iface); |
| 81 | } |
| 82 | objectServer.remove_interface(association); |
| 83 | objectServer.remove_interface(sensorInterface); |
| 84 | } |
| 85 | |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 86 | void NvidiaGpuTempSensor::checkThresholds() |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 87 | { |
| 88 | thresholds::checkThresholds(this); |
| 89 | } |
| 90 | |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 91 | void NvidiaGpuTempSensor::processResponse(int sendRecvMsgResult) |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 92 | { |
| 93 | if (sendRecvMsgResult != 0) |
| 94 | { |
| 95 | lg2::error( |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 96 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}", |
| 97 | "EID", eid, "SID", sensorId, "RC", sendRecvMsgResult); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
| 101 | ocp::accelerator_management::CompletionCode cc{}; |
| 102 | uint16_t reasonCode = 0; |
| 103 | double tempValue = 0; |
| 104 | |
| 105 | auto rc = gpu::decodeGetTemperatureReadingResponse( |
| 106 | getTemperatureReadingResponse, cc, reasonCode, tempValue); |
| 107 | |
| 108 | if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 109 | { |
| 110 | lg2::error( |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 111 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : decode failed. " |
| 112 | "rc={RC}, cc={CC}, reasonCode={RESC}", |
| 113 | "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC", |
| 114 | reasonCode); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | updateValue(tempValue); |
| 119 | } |
| 120 | |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 121 | void NvidiaGpuTempSensor::update() |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 122 | { |
| 123 | auto rc = gpu::encodeGetTemperatureReadingRequest( |
| 124 | 0, sensorId, getTemperatureReadingRequest); |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 125 | |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 126 | if (rc != 0) |
| 127 | { |
Harshit Aghera | fa2a5b9 | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 128 | lg2::error( |
| 129 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}", |
| 130 | "EID", eid, "SID", sensorId, "RC", rc); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | mctpRequester.sendRecvMsg( |
| 134 | eid, getTemperatureReadingRequest, getTemperatureReadingResponse, |
| 135 | [this](int sendRecvMsgResult) { processResponse(sendRecvMsgResult); }); |
| 136 | } |