blob: d52c040fb45f17c86696c7c7773ca397c928dd6d [file] [log] [blame]
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -05001/**
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 Barth29e9e382020-01-23 13:40:49 -060016#pragma once
17
Shawn McCarneycee2e202024-08-02 16:38:33 -050018#include "compatible_system_types_finder.hpp"
Shawn McCarneyb464c8b2020-07-16 17:51:38 -050019#include "services.hpp"
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050020#include "system.hpp"
21
Matthew Barth29e9e382020-01-23 13:40:49 -060022#include <interfaces/manager_interface.hpp>
23#include <sdbusplus/bus.hpp>
24#include <sdbusplus/server/object.hpp>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060025#include <sdeventplus/event.hpp>
Matthew Barth7cbc5532020-01-29 15:13:13 -060026#include <sdeventplus/source/signal.hpp>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060027#include <sdeventplus/utility/timer.hpp>
Matthew Barth29e9e382020-01-23 13:40:49 -060028
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050029#include <filesystem>
30#include <memory>
31#include <string>
32#include <vector>
Matthew Barthbbc7c582020-02-03 15:55:15 -060033
Matthew Barth29e9e382020-01-23 13:40:49 -060034namespace phosphor::power::regulators
35{
36
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060037using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
38
Patrick Williams7354ce62022-07-22 19:26:56 -050039using ManagerObject = sdbusplus::server::object_t<
Matthew Barth29e9e382020-01-23 13:40:49 -060040 phosphor::power::regulators::interface::ManagerInterface>;
41
42class 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 McCarneyb464c8b2020-07-16 17:51:38 -050056 * @param bus the D-Bus bus
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050057 * @param event the sdevent event
Matthew Barth29e9e382020-01-23 13:40:49 -060058 */
Patrick Williams7354ce62022-07-22 19:26:56 -050059 Manager(sdbusplus::bus_t& bus, const sdeventplus::Event& event);
Matthew Barth29e9e382020-01-23 13:40:49 -060060
61 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050062 * 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 Barth29e9e382020-01-23 13:40:49 -060068 */
69 void configure() override;
70
71 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050072 * Implements the D-Bus "monitor" method.
Matthew Barth29e9e382020-01-23 13:40:49 -060073 *
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050074 * Sets whether regulator monitoring is enabled.
75 *
76 * When monitoring is enabled:
77 * - regulator sensors will be read and published on D-Bus
78 * - phase fault detection will be performed
79 *
80 * Regulator monitoring should be enabled when the system is being powered
81 * on. It needs to occur after the regulators have been configured and
82 * enabled/turned on.
83 *
84 * Regulator monitoring should be disabled when the system is being powered
85 * off. It needs to occur before the regulators have been disabled/turned
86 * off.
87 *
88 * Regulator monitoring can also be temporarily disabled and then re-enabled
89 * while the system is powered on. This allows other applications or tools
90 * to temporarily communicate with the regulators for testing or debug.
91 * Monitoring should be disabled for only short periods of time; other
92 * applications, such as fan control, may be dependent on regulator sensors.
93 *
94 * @param enable true if monitoring should be enabled, false if it should be
95 * disabled
Matthew Barth29e9e382020-01-23 13:40:49 -060096 */
97 void monitor(bool enable) override;
98
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060099 /**
Shawn McCarneycee2e202024-08-02 16:38:33 -0500100 * Callback that is called when a list of compatible system types is found.
101 *
102 * @param types Compatible system types for the current system ordered from
103 * most to least specific
104 */
105 void compatibleSystemTypesFound(const std::vector<std::string>& types);
106
107 /**
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500108 * Phase fault detection timer expired callback function.
109 */
110 void phaseFaultTimerExpired();
111
112 /**
113 * Sensor monitoring timer expired callback function.
114 */
115 void sensorTimerExpired();
116
117 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500118 * Callback function to handle receiving a HUP signal
Matthew Barth7cbc5532020-01-29 15:13:13 -0600119 * to reload the configuration data.
120 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500121 * @param sigSrc sd_event_source signal wrapper
122 * @param sigInfo signal info on signal fd
Matthew Barth7cbc5532020-01-29 15:13:13 -0600123 */
124 void sighupHandler(sdeventplus::source::Signal& sigSrc,
125 const struct signalfd_siginfo* sigInfo);
126
Matthew Barth29e9e382020-01-23 13:40:49 -0600127 private:
128 /**
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600129 * Clear any cached data or error history related to hardware devices.
130 *
131 * This method should be called when the system is powering on (booting).
132 * While the system was powered off, hardware could have been added,
133 * removed, or replaced.
134 */
135 void clearHardwareData();
136
137 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500138 * Finds the JSON configuration file.
139 *
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500140 * Looks for a configuration file based on the list of compatible system
Shawn McCarney589c1812021-01-14 12:13:26 -0600141 * types. If no file is found, looks for a file with the default name.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500142 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600143 * Looks for the file in the test directory and standard directory.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500144 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600145 * Throws an exception if an operating system error occurs while checking
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500146 * for the existence of a file.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500147 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600148 * @return absolute path to config file, or an empty path if none found
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500149 */
150 std::filesystem::path findConfigFile();
151
152 /**
Shawn McCarney8acaf542021-03-30 10:38:37 -0500153 * Returns whether the JSON configuration file has been loaded.
154 *
155 * @return true if config file loaded, false otherwise
156 */
157 bool isConfigFileLoaded() const
158 {
159 // If System object exists, the config file has been loaded
160 return (system != nullptr);
161 }
162
163 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500164 * Returns whether the system is currently powered on.
165 *
166 * @return true if system is powered on, false otherwise
167 */
168 bool isSystemPoweredOn();
169
170 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500171 * Loads the JSON configuration file.
172 *
Shawn McCarney589c1812021-01-14 12:13:26 -0600173 * Looks for the config file using findConfigFile().
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500174 *
175 * If the config file is found, it is parsed and the resulting information
Shawn McCarney589c1812021-01-14 12:13:26 -0600176 * is stored in the system data member. If parsing fails, an error is
177 * logged.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500178 */
179 void loadConfigFile();
180
181 /**
Shawn McCarney8acaf542021-03-30 10:38:37 -0500182 * Waits until the JSON configuration file has been loaded.
183 *
184 * If the config file has not yet been loaded, waits until one of the
185 * following occurs:
186 * - config file is loaded
187 * - maximum amount of time to wait has elapsed
188 */
189 void waitUntilConfigFileLoaded();
190
191 /**
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500192 * The D-Bus bus
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500193 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500194 sdbusplus::bus_t& bus;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500195
196 /**
197 * Event to loop on
198 */
Jayanth Othayothe1a07592024-12-06 11:54:09 -0600199 const sdeventplus::Event& eventLoop [[maybe_unused]];
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500200
201 /**
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500202 * System services like error logging and the journal.
203 */
204 BMCServices services;
205
206 /**
Shawn McCarneycee2e202024-08-02 16:38:33 -0500207 * Object that finds the compatible system types for the current system.
208 */
209 std::unique_ptr<util::CompatibleSystemTypesFinder> compatSysTypesFinder;
210
211 /**
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500212 * Event timer used to initiate phase fault detection.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500213 */
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500214 Timer phaseFaultTimer;
215
216 /**
217 * Event timer used to initiate sensor monitoring.
218 */
219 Timer sensorTimer;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500220
221 /**
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500222 * Indicates whether regulator monitoring is enabled.
223 */
224 bool isMonitoringEnabled{false};
225
226 /**
Shawn McCarney589c1812021-01-14 12:13:26 -0600227 * List of compatible system types for the current system.
228 *
229 * Used to find the JSON configuration file.
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500230 */
Shawn McCarney589c1812021-01-14 12:13:26 -0600231 std::vector<std::string> compatibleSystemTypes{};
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500232
233 /**
234 * Computer system being controlled and monitored by the BMC.
235 *
236 * Contains the information loaded from the JSON configuration file.
237 * Contains nullptr if the configuration file has not been loaded.
238 */
239 std::unique_ptr<System> system{};
Matthew Barth29e9e382020-01-23 13:40:49 -0600240};
241
242} // namespace phosphor::power::regulators