Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 16 | #pragma once |
| 17 | |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 18 | #include "services.hpp" |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 19 | #include "system.hpp" |
| 20 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 21 | #include <interfaces/manager_interface.hpp> |
| 22 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 23 | #include <sdbusplus/bus/match.hpp> |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 24 | #include <sdbusplus/server/object.hpp> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 25 | #include <sdeventplus/event.hpp> |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 26 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 27 | #include <sdeventplus/utility/timer.hpp> |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 28 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 29 | #include <filesystem> |
| 30 | #include <memory> |
| 31 | #include <string> |
| 32 | #include <vector> |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 33 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 34 | namespace phosphor::power::regulators |
| 35 | { |
| 36 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 37 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 38 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 39 | using ManagerObject = sdbusplus::server::object_t< |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 40 | phosphor::power::regulators::interface::ManagerInterface>; |
| 41 | |
| 42 | class Manager : public ManagerObject |
| 43 | { |
| 44 | public: |
| 45 | Manager() = delete; |
| 46 | Manager(const Manager&) = delete; |
| 47 | Manager(Manager&&) = delete; |
| 48 | Manager& operator=(const Manager&) = delete; |
| 49 | Manager& operator=(Manager&&) = delete; |
| 50 | ~Manager() = default; |
| 51 | |
| 52 | /** |
| 53 | * Constructor |
| 54 | * Creates a manager over the regulators. |
| 55 | * |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 56 | * @param bus the D-Bus bus |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 57 | * @param event the sdevent event |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 58 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 59 | Manager(sdbusplus::bus_t& bus, const sdeventplus::Event& event); |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 60 | |
| 61 | /** |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 62 | * Implements the D-Bus "configure" method. |
| 63 | * |
| 64 | * Configures all the voltage regulators in the system. |
| 65 | * |
| 66 | * This method should be called when the system is being powered on. It |
| 67 | * needs to occur before the regulators have been enabled/turned on. |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 68 | */ |
| 69 | void configure() override; |
| 70 | |
| 71 | /** |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 72 | * Callback function to handle interfacesAdded D-Bus signals |
| 73 | * |
| 74 | * @param msg Expanded sdbusplus message data |
| 75 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 76 | void interfacesAddedHandler(sdbusplus::message_t& msg); |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 77 | |
| 78 | /** |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 79 | * Implements the D-Bus "monitor" method. |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 80 | * |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 81 | * Sets whether regulator monitoring is enabled. |
| 82 | * |
| 83 | * When monitoring is enabled: |
| 84 | * - regulator sensors will be read and published on D-Bus |
| 85 | * - phase fault detection will be performed |
| 86 | * |
| 87 | * Regulator monitoring should be enabled when the system is being powered |
| 88 | * on. It needs to occur after the regulators have been configured and |
| 89 | * enabled/turned on. |
| 90 | * |
| 91 | * Regulator monitoring should be disabled when the system is being powered |
| 92 | * off. It needs to occur before the regulators have been disabled/turned |
| 93 | * off. |
| 94 | * |
| 95 | * Regulator monitoring can also be temporarily disabled and then re-enabled |
| 96 | * while the system is powered on. This allows other applications or tools |
| 97 | * to temporarily communicate with the regulators for testing or debug. |
| 98 | * Monitoring should be disabled for only short periods of time; other |
| 99 | * applications, such as fan control, may be dependent on regulator sensors. |
| 100 | * |
| 101 | * @param enable true if monitoring should be enabled, false if it should be |
| 102 | * disabled |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 103 | */ |
| 104 | void monitor(bool enable) override; |
| 105 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 106 | /** |
Shawn McCarney | c8cbeac | 2021-09-10 15:42:56 -0500 | [diff] [blame] | 107 | * Phase fault detection timer expired callback function. |
| 108 | */ |
| 109 | void phaseFaultTimerExpired(); |
| 110 | |
| 111 | /** |
| 112 | * Sensor monitoring timer expired callback function. |
| 113 | */ |
| 114 | void sensorTimerExpired(); |
| 115 | |
| 116 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 117 | * Callback function to handle receiving a HUP signal |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 118 | * to reload the configuration data. |
| 119 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 120 | * @param sigSrc sd_event_source signal wrapper |
| 121 | * @param sigInfo signal info on signal fd |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 122 | */ |
| 123 | void sighupHandler(sdeventplus::source::Signal& sigSrc, |
| 124 | const struct signalfd_siginfo* sigInfo); |
| 125 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 126 | private: |
| 127 | /** |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 128 | * Clear any cached data or error history related to hardware devices. |
| 129 | * |
| 130 | * This method should be called when the system is powering on (booting). |
| 131 | * While the system was powered off, hardware could have been added, |
| 132 | * removed, or replaced. |
| 133 | */ |
| 134 | void clearHardwareData(); |
| 135 | |
| 136 | /** |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 137 | * Finds the list of compatible system types using D-Bus methods. |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 138 | * |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 139 | * This list is used to find the correct JSON configuration file for the |
| 140 | * current system. |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 141 | * |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 142 | * Note that some systems do not support the D-Bus compatible interface. |
| 143 | * |
| 144 | * If a list of compatible system types is found, it is stored in the |
| 145 | * compatibleSystemTypes data member. |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 146 | */ |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 147 | void findCompatibleSystemTypes(); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Finds the JSON configuration file. |
| 151 | * |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 152 | * Looks for a configuration file based on the list of compatible system |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 153 | * types. If no file is found, looks for a file with the default name. |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 154 | * |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 155 | * Looks for the file in the test directory and standard directory. |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 156 | * |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 157 | * Throws an exception if an operating system error occurs while checking |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 158 | * for the existence of a file. |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 159 | * |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 160 | * @return absolute path to config file, or an empty path if none found |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 161 | */ |
| 162 | std::filesystem::path findConfigFile(); |
| 163 | |
| 164 | /** |
Shawn McCarney | 8acaf54 | 2021-03-30 10:38:37 -0500 | [diff] [blame] | 165 | * Returns whether the JSON configuration file has been loaded. |
| 166 | * |
| 167 | * @return true if config file loaded, false otherwise |
| 168 | */ |
| 169 | bool isConfigFileLoaded() const |
| 170 | { |
| 171 | // If System object exists, the config file has been loaded |
| 172 | return (system != nullptr); |
| 173 | } |
| 174 | |
| 175 | /** |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 176 | * Returns whether the system is currently powered on. |
| 177 | * |
| 178 | * @return true if system is powered on, false otherwise |
| 179 | */ |
| 180 | bool isSystemPoweredOn(); |
| 181 | |
| 182 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 183 | * Loads the JSON configuration file. |
| 184 | * |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 185 | * Looks for the config file using findConfigFile(). |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 186 | * |
| 187 | * If the config file is found, it is parsed and the resulting information |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 188 | * is stored in the system data member. If parsing fails, an error is |
| 189 | * logged. |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 190 | */ |
| 191 | void loadConfigFile(); |
| 192 | |
| 193 | /** |
Shawn McCarney | 8acaf54 | 2021-03-30 10:38:37 -0500 | [diff] [blame] | 194 | * Waits until the JSON configuration file has been loaded. |
| 195 | * |
| 196 | * If the config file has not yet been loaded, waits until one of the |
| 197 | * following occurs: |
| 198 | * - config file is loaded |
| 199 | * - maximum amount of time to wait has elapsed |
| 200 | */ |
| 201 | void waitUntilConfigFileLoaded(); |
| 202 | |
| 203 | /** |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 204 | * The D-Bus bus |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 205 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 206 | sdbusplus::bus_t& bus; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 207 | |
| 208 | /** |
| 209 | * Event to loop on |
| 210 | */ |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 211 | const sdeventplus::Event& eventLoop; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 212 | |
| 213 | /** |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 214 | * System services like error logging and the journal. |
| 215 | */ |
| 216 | BMCServices services; |
| 217 | |
| 218 | /** |
Shawn McCarney | c8cbeac | 2021-09-10 15:42:56 -0500 | [diff] [blame] | 219 | * Event timer used to initiate phase fault detection. |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 220 | */ |
Shawn McCarney | c8cbeac | 2021-09-10 15:42:56 -0500 | [diff] [blame] | 221 | Timer phaseFaultTimer; |
| 222 | |
| 223 | /** |
| 224 | * Event timer used to initiate sensor monitoring. |
| 225 | */ |
| 226 | Timer sensorTimer; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 227 | |
| 228 | /** |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 229 | * List of D-Bus signal matches |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 230 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 231 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> signals{}; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 232 | |
| 233 | /** |
Shawn McCarney | d9c8be5 | 2021-05-18 10:07:53 -0500 | [diff] [blame] | 234 | * Indicates whether regulator monitoring is enabled. |
| 235 | */ |
| 236 | bool isMonitoringEnabled{false}; |
| 237 | |
| 238 | /** |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 239 | * List of compatible system types for the current system. |
| 240 | * |
| 241 | * Used to find the JSON configuration file. |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 242 | */ |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 243 | std::vector<std::string> compatibleSystemTypes{}; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * Computer system being controlled and monitored by the BMC. |
| 247 | * |
| 248 | * Contains the information loaded from the JSON configuration file. |
| 249 | * Contains nullptr if the configuration file has not been loaded. |
| 250 | */ |
| 251 | std::unique_ptr<System> system{}; |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | } // namespace phosphor::power::regulators |