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 | 4ecdfaa | 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 | |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 34 | constexpr uint8_t gpuTempSensorId{0}; |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 35 | static constexpr double gpuTempSensorMaxReading = 127; |
| 36 | static constexpr double gpuTempSensorMinReading = -128; |
| 37 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 38 | NvidiaGpuTempSensor::NvidiaGpuTempSensor( |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 39 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 40 | mctp::MctpRequester& mctpRequester, const std::string& name, |
| 41 | const std::string& sensorConfiguration, const uint8_t eid, |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 42 | sdbusplus::asio::object_server& objectServer, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 43 | std::vector<thresholds::Threshold>&& thresholdData) : |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 44 | Sensor(escapeName(name), std::move(thresholdData), sensorConfiguration, |
| 45 | "temperature", false, true, gpuTempSensorMaxReading, |
| 46 | gpuTempSensorMinReading, conn), |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 47 | eid(eid), sensorId{gpuTempSensorId}, mctpRequester(mctpRequester), |
| 48 | objectServer(objectServer) |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 49 | { |
| 50 | std::string dbusPath = |
| 51 | sensorPathPrefix + "temperature/"s + escapeName(name); |
| 52 | |
| 53 | sensorInterface = objectServer.add_interface( |
| 54 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
| 55 | |
| 56 | for (const auto& threshold : thresholds) |
| 57 | { |
| 58 | std::string interface = thresholds::getInterface(threshold.level); |
| 59 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 60 | objectServer.add_interface(dbusPath, interface); |
| 61 | } |
| 62 | |
| 63 | association = objectServer.add_interface(dbusPath, association::interface); |
| 64 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 65 | setInitialProperties(sensor_paths::unitDegreesC); |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 66 | } |
| 67 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 68 | NvidiaGpuTempSensor::~NvidiaGpuTempSensor() |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 69 | { |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 70 | for (const auto& iface : thresholdInterfaces) |
| 71 | { |
| 72 | objectServer.remove_interface(iface); |
| 73 | } |
| 74 | objectServer.remove_interface(association); |
| 75 | objectServer.remove_interface(sensorInterface); |
| 76 | } |
| 77 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 78 | void NvidiaGpuTempSensor::checkThresholds() |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 79 | { |
| 80 | thresholds::checkThresholds(this); |
| 81 | } |
| 82 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 83 | void NvidiaGpuTempSensor::processResponse(int sendRecvMsgResult) |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 84 | { |
| 85 | if (sendRecvMsgResult != 0) |
| 86 | { |
| 87 | lg2::error( |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 88 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}", |
| 89 | "EID", eid, "SID", sensorId, "RC", sendRecvMsgResult); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | ocp::accelerator_management::CompletionCode cc{}; |
| 94 | uint16_t reasonCode = 0; |
| 95 | double tempValue = 0; |
| 96 | |
| 97 | auto rc = gpu::decodeGetTemperatureReadingResponse( |
| 98 | getTemperatureReadingResponse, cc, reasonCode, tempValue); |
| 99 | |
| 100 | if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 101 | { |
| 102 | lg2::error( |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 103 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : decode failed. " |
| 104 | "rc={RC}, cc={CC}, reasonCode={RESC}", |
| 105 | "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC", |
| 106 | reasonCode); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
| 110 | updateValue(tempValue); |
| 111 | } |
| 112 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 113 | void NvidiaGpuTempSensor::update() |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 114 | { |
| 115 | auto rc = gpu::encodeGetTemperatureReadingRequest( |
| 116 | 0, sensorId, getTemperatureReadingRequest); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 117 | |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 118 | if (rc != 0) |
| 119 | { |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame^] | 120 | lg2::error( |
| 121 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}", |
| 122 | "EID", eid, "SID", sensorId, "RC", rc); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | mctpRequester.sendRecvMsg( |
| 126 | eid, getTemperatureReadingRequest, getTemperatureReadingResponse, |
| 127 | [this](int sendRecvMsgResult) { processResponse(sendRecvMsgResult); }); |
| 128 | } |