| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 1 | /* |
| Ed Tanous | b5e823f | 2025-10-09 20:28:42 -0400 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include "NvidiaGpuSensor.hpp" |
| 7 | |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 8 | #include "SensorPaths.hpp" |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 9 | #include "Thresholds.hpp" |
| 10 | #include "Utils.hpp" |
| 11 | #include "sensor.hpp" |
| 12 | |
| 13 | #include <bits/basic_string.h> |
| 14 | |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 15 | #include <MctpRequester.hpp> |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 16 | #include <NvidiaDeviceDiscovery.hpp> |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 17 | #include <NvidiaGpuMctpVdm.hpp> |
| 18 | #include <OcpMctpVdm.hpp> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 19 | #include <phosphor-logging/lg2.hpp> |
| 20 | #include <sdbusplus/asio/connection.hpp> |
| 21 | #include <sdbusplus/asio/object_server.hpp> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 22 | |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 23 | #include <cstddef> |
| 24 | #include <cstdint> |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 25 | #include <functional> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 26 | #include <memory> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 27 | #include <span> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 28 | #include <string> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 29 | #include <system_error> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 30 | #include <utility> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | using namespace std::literals; |
| 34 | |
| 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, |
| Harshit Aghera | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 41 | const std::string& sensorConfiguration, const uint8_t eid, uint8_t sensorId, |
| 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 | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 47 | eid(eid), sensorId{sensorId}, mctpRequester(mctpRequester), |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 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 | 3f6bc73 | 2025-07-23 14:22:01 +0530 | [diff] [blame] | 66 | |
| 67 | if (sensorId == gpuTLimitSensorId) |
| 68 | { |
| 69 | sensorTypeInterface = objectServer.add_interface( |
| 70 | dbusPath, "xyz.openbmc_project.Sensor.Type"); |
| 71 | |
| 72 | sensorTypeInterface->register_property( |
| 73 | "ReadingBasis", |
| 74 | "xyz.openbmc_project.Sensor.Type.ReadingBasisType.Headroom"s); |
| 75 | sensorTypeInterface->register_property( |
| 76 | "Implementation", |
| 77 | "xyz.openbmc_project.Sensor.Type.ImplementationType.Synthesized"s); |
| 78 | |
| 79 | if (!sensorTypeInterface->initialize()) |
| 80 | { |
| 81 | lg2::error( |
| 82 | "Error initializing Type Interface for Temperature Sensor for eid {EID} and sensor id {SID}", |
| 83 | "EID", eid, "SID", sensorId); |
| 84 | } |
| 85 | } |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 86 | } |
| 87 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 88 | NvidiaGpuTempSensor::~NvidiaGpuTempSensor() |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 89 | { |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 90 | for (const auto& iface : thresholdInterfaces) |
| 91 | { |
| 92 | objectServer.remove_interface(iface); |
| 93 | } |
| 94 | objectServer.remove_interface(association); |
| 95 | objectServer.remove_interface(sensorInterface); |
| Harshit Aghera | 3f6bc73 | 2025-07-23 14:22:01 +0530 | [diff] [blame] | 96 | if (sensorTypeInterface) |
| 97 | { |
| 98 | objectServer.remove_interface(sensorTypeInterface); |
| 99 | } |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 102 | void NvidiaGpuTempSensor::checkThresholds() |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 103 | { |
| 104 | thresholds::checkThresholds(this); |
| 105 | } |
| 106 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 107 | void NvidiaGpuTempSensor::processResponse(const std::error_code& ec, |
| 108 | std::span<const uint8_t> buffer) |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 109 | { |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 110 | if (ec) |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 111 | { |
| 112 | lg2::error( |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 113 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : sending message over MCTP failed, rc={RC}", |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 114 | "EID", eid, "SID", sensorId, "RC", ec.message()); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | ocp::accelerator_management::CompletionCode cc{}; |
| 119 | uint16_t reasonCode = 0; |
| 120 | double tempValue = 0; |
| 121 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 122 | auto rc = gpu::decodeGetTemperatureReadingResponse(buffer, cc, reasonCode, |
| 123 | tempValue); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 124 | |
| 125 | if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 126 | { |
| 127 | lg2::error( |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 128 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : decode failed. " |
| 129 | "rc={RC}, cc={CC}, reasonCode={RESC}", |
| 130 | "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC", |
| 131 | reasonCode); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
| 135 | updateValue(tempValue); |
| 136 | } |
| 137 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 138 | void NvidiaGpuTempSensor::update() |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 139 | { |
| 140 | auto rc = gpu::encodeGetTemperatureReadingRequest( |
| 141 | 0, sensorId, getTemperatureReadingRequest); |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 142 | |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 143 | if (rc != 0) |
| 144 | { |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 145 | lg2::error( |
| 146 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}", |
| 147 | "EID", eid, "SID", sensorId, "RC", rc); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | mctpRequester.sendRecvMsg( |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 151 | eid, getTemperatureReadingRequest, |
| Marc Olberding | fd4a377 | 2025-09-24 16:31:02 -0700 | [diff] [blame] | 152 | [weak{weak_from_this()}](const std::error_code& ec, |
| 153 | std::span<const uint8_t> buffer) { |
| 154 | std::shared_ptr<NvidiaGpuTempSensor> self = weak.lock(); |
| 155 | if (!self) |
| 156 | { |
| 157 | lg2::error("Invalid reference to NvidiaGpuTempSensor"); |
| 158 | return; |
| 159 | } |
| 160 | self->processResponse(ec, buffer); |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 161 | }); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 162 | } |