| 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> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 28 | #include <span> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 29 | #include <string> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 30 | #include <system_error> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 31 | #include <utility> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
| 34 | using namespace std::literals; |
| 35 | |
| 36 | static constexpr double gpuTempSensorMaxReading = 127; |
| 37 | static constexpr double gpuTempSensorMinReading = -128; |
| 38 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 39 | NvidiaGpuTempSensor::NvidiaGpuTempSensor( |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 40 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 41 | mctp::MctpRequester& mctpRequester, const std::string& name, |
| Harshit Aghera | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 42 | const std::string& sensorConfiguration, const uint8_t eid, uint8_t sensorId, |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 43 | sdbusplus::asio::object_server& objectServer, |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 44 | std::vector<thresholds::Threshold>&& thresholdData) : |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 45 | Sensor(escapeName(name), std::move(thresholdData), sensorConfiguration, |
| 46 | "temperature", false, true, gpuTempSensorMaxReading, |
| 47 | gpuTempSensorMinReading, conn), |
| Harshit Aghera | ba138da | 2025-05-05 12:26:35 +0530 | [diff] [blame] | 48 | eid(eid), sensorId{sensorId}, mctpRequester(mctpRequester), |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 49 | objectServer(objectServer) |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 50 | { |
| 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 Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 66 | setInitialProperties(sensor_paths::unitDegreesC); |
| Harshit Aghera | 3f6bc73 | 2025-07-23 14:22:01 +0530 | [diff] [blame^] | 67 | |
| 68 | if (sensorId == gpuTLimitSensorId) |
| 69 | { |
| 70 | sensorTypeInterface = objectServer.add_interface( |
| 71 | dbusPath, "xyz.openbmc_project.Sensor.Type"); |
| 72 | |
| 73 | sensorTypeInterface->register_property( |
| 74 | "ReadingBasis", |
| 75 | "xyz.openbmc_project.Sensor.Type.ReadingBasisType.Headroom"s); |
| 76 | sensorTypeInterface->register_property( |
| 77 | "Implementation", |
| 78 | "xyz.openbmc_project.Sensor.Type.ImplementationType.Synthesized"s); |
| 79 | |
| 80 | if (!sensorTypeInterface->initialize()) |
| 81 | { |
| 82 | lg2::error( |
| 83 | "Error initializing Type Interface for Temperature Sensor for eid {EID} and sensor id {SID}", |
| 84 | "EID", eid, "SID", sensorId); |
| 85 | } |
| 86 | } |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 87 | } |
| 88 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 89 | NvidiaGpuTempSensor::~NvidiaGpuTempSensor() |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 90 | { |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 91 | for (const auto& iface : thresholdInterfaces) |
| 92 | { |
| 93 | objectServer.remove_interface(iface); |
| 94 | } |
| 95 | objectServer.remove_interface(association); |
| 96 | objectServer.remove_interface(sensorInterface); |
| Harshit Aghera | 3f6bc73 | 2025-07-23 14:22:01 +0530 | [diff] [blame^] | 97 | if (sensorTypeInterface) |
| 98 | { |
| 99 | objectServer.remove_interface(sensorTypeInterface); |
| 100 | } |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 103 | void NvidiaGpuTempSensor::checkThresholds() |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 104 | { |
| 105 | thresholds::checkThresholds(this); |
| 106 | } |
| 107 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 108 | void NvidiaGpuTempSensor::processResponse(const std::error_code& ec, |
| 109 | std::span<const uint8_t> buffer) |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 110 | { |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 111 | if (ec) |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 112 | { |
| 113 | lg2::error( |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 114 | "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] | 115 | "EID", eid, "SID", sensorId, "RC", ec.message()); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
| 119 | ocp::accelerator_management::CompletionCode cc{}; |
| 120 | uint16_t reasonCode = 0; |
| 121 | double tempValue = 0; |
| 122 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 123 | auto rc = gpu::decodeGetTemperatureReadingResponse(buffer, cc, reasonCode, |
| 124 | tempValue); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 125 | |
| 126 | if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 127 | { |
| 128 | lg2::error( |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 129 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : decode failed. " |
| 130 | "rc={RC}, cc={CC}, reasonCode={RESC}", |
| 131 | "EID", eid, "SID", sensorId, "RC", rc, "CC", cc, "RESC", |
| 132 | reasonCode); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
| 136 | updateValue(tempValue); |
| 137 | } |
| 138 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 139 | void NvidiaGpuTempSensor::update() |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 140 | { |
| 141 | auto rc = gpu::encodeGetTemperatureReadingRequest( |
| 142 | 0, sensorId, getTemperatureReadingRequest); |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 143 | |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 144 | if (rc != 0) |
| 145 | { |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 146 | lg2::error( |
| 147 | "Error updating Temperature Sensor for eid {EID} and sensor id {SID} : encode failed, rc={RC}", |
| 148 | "EID", eid, "SID", sensorId, "RC", rc); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | mctpRequester.sendRecvMsg( |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 152 | eid, getTemperatureReadingRequest, |
| Marc Olberding | fd4a377 | 2025-09-24 16:31:02 -0700 | [diff] [blame] | 153 | [weak{weak_from_this()}](const std::error_code& ec, |
| 154 | std::span<const uint8_t> buffer) { |
| 155 | std::shared_ptr<NvidiaGpuTempSensor> self = weak.lock(); |
| 156 | if (!self) |
| 157 | { |
| 158 | lg2::error("Invalid reference to NvidiaGpuTempSensor"); |
| 159 | return; |
| 160 | } |
| 161 | self->processResponse(ec, buffer); |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 162 | }); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 163 | } |