Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0 |
| 4 | */ |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "Thresholds.hpp" |
| 9 | #include "sensor.hpp" |
| 10 | |
| 11 | #include <boost/asio/io_context.hpp> |
| 12 | #include <boost/asio/steady_timer.hpp> |
| 13 | #include <boost/container/flat_map.hpp> |
| 14 | #include <sdbusplus/asio/connection.hpp> |
| 15 | #include <sdbusplus/asio/object_server.hpp> |
| 16 | #include <sdbusplus/message.hpp> |
| 17 | |
| 18 | #include <cstdint> |
| 19 | #include <map> |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | #include <utility> |
| 23 | #include <variant> |
| 24 | #include <vector> |
| 25 | |
| 26 | constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/"; |
| 27 | constexpr const char* sensorType = "NvidiaMctpVdm"; |
| 28 | |
| 29 | using getSubTreeRet = std::vector< |
| 30 | std::pair<std::string, |
| 31 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 32 | using GpuSensorConfigMap = |
| 33 | std::map<std::string, std::variant<std::string, bool, uint32_t, uint8_t, |
| 34 | int64_t, std::vector<uint8_t>>>; |
| 35 | |
| 36 | /** |
| 37 | * @struct DeviceInfo |
| 38 | * @brief Contains information about a device |
| 39 | */ |
| 40 | struct DeviceInfo |
| 41 | { |
| 42 | uint8_t deviceType; |
| 43 | uint8_t instanceId; |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * @struct GpuTempSensor |
| 48 | * @brief Implements a GPU temperature sensor that monitors temperature values |
| 49 | * @details Inherits from Sensor base class and enables shared pointer |
| 50 | * management via std::enable_shared_from_this |
| 51 | */ |
| 52 | struct GpuTempSensor : |
| 53 | public Sensor, |
| 54 | public std::enable_shared_from_this<GpuTempSensor> |
| 55 | { |
| 56 | public: |
| 57 | /** |
| 58 | * @brief Constructor for GpuTempSensor |
| 59 | * @param conn D-Bus connection |
| 60 | * @param io Boost ASIO I/O context for asynchronous operations |
| 61 | * @param mctpRequester MCTP protocol requester for GPU communication |
| 62 | * @param name Name of the sensor |
| 63 | * @param sensorConfiguration Configuration string for the sensor |
| 64 | * @param objectServer D-Bus object server |
| 65 | * @param thresholdData Vector of threshold configurations |
| 66 | * @param pollRate How often to poll for new readings |
| 67 | * @param deviceInfo Information about the GPU device |
| 68 | * @param verbose Whether to enable verbose logging |
| 69 | */ |
| 70 | GpuTempSensor(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 71 | boost::asio::io_context& io, const std::string& name, |
| 72 | const std::string& sensorConfiguration, |
| 73 | sdbusplus::asio::object_server& objectServer, |
| 74 | std::vector<thresholds::Threshold>&& thresholdData); |
| 75 | |
| 76 | /** |
| 77 | * @brief Destructor |
| 78 | */ |
| 79 | ~GpuTempSensor() override; |
| 80 | |
| 81 | /** |
| 82 | * @brief Check if any thresholds have been crossed |
| 83 | * @details Overrides the base class method to implement GPU-specific |
| 84 | * threshold checking |
| 85 | */ |
| 86 | void checkThresholds() override; |
| 87 | |
| 88 | private: |
| 89 | /** |
| 90 | * @brief Initialize the sensor |
| 91 | */ |
| 92 | void init(); |
| 93 | |
| 94 | /** |
| 95 | * @brief Discover available GPUs on the system |
| 96 | */ |
| 97 | void discoverGpus(); |
| 98 | |
| 99 | /** |
| 100 | * @brief Process MCTP endpoints discovered on the system |
| 101 | * |
| 102 | * @param[in] ec Error code from the D-Bus method call |
| 103 | * @param[in] ret Object tree results containing MCTP endpoint information |
| 104 | */ |
| 105 | void processMctpEndpoints(const boost::system::error_code& ec, |
| 106 | const getSubTreeRet& ret); |
| 107 | |
| 108 | /** |
| 109 | * @brief Process configuration properties for MCTP endpoints |
| 110 | * |
| 111 | * @param[in] ec Error code from the D-Bus properties method call |
| 112 | * @param[in] configs Map of configuration properties for the endpoint |
| 113 | */ |
| 114 | void processEndpointConfigs(const boost::system::error_code& ec, |
| 115 | const GpuSensorConfigMap& configs); |
| 116 | |
| 117 | /** |
| 118 | * @brief Timer for scheduling sensor reads |
| 119 | */ |
| 120 | boost::asio::steady_timer waitTimer; |
| 121 | |
| 122 | /** |
| 123 | * @brief D-Bus connection |
| 124 | */ |
| 125 | std::shared_ptr<sdbusplus::asio::connection> conn; |
| 126 | |
| 127 | /** |
| 128 | * @brief D-Bus object server |
| 129 | */ |
| 130 | sdbusplus::asio::object_server& objectServer; |
| 131 | }; |
| 132 | |
| 133 | /** |
| 134 | * @brief Create GPU temperature sensors |
| 135 | * @param io Boost ASIO I/O context |
| 136 | * @param objectServer D-Bus object server |
| 137 | * @param sensors Map to store created sensors |
| 138 | * @param dbusConnection D-Bus connection |
| 139 | */ |
| 140 | void createSensors( |
| 141 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 142 | boost::container::flat_map<std::string, std::shared_ptr<GpuTempSensor>>& |
| 143 | sensors, |
| 144 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection); |
| 145 | |
| 146 | /** |
| 147 | * @brief Handle D-Bus interface removal events |
| 148 | * @param message D-Bus message containing interface removal information |
| 149 | * @param sensors Map of GPU temperature sensors to check for removal |
| 150 | */ |
| 151 | void interfaceRemoved( |
| 152 | sdbusplus::message_t& message, |
| 153 | boost::container::flat_map<std::string, std::shared_ptr<GpuTempSensor>>& |
| 154 | sensors); |