1e0c6a2d9SShawn McCarney /** 2e0c6a2d9SShawn McCarney * Copyright © 2020 IBM Corporation 3e0c6a2d9SShawn McCarney * 4e0c6a2d9SShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License"); 5e0c6a2d9SShawn McCarney * you may not use this file except in compliance with the License. 6e0c6a2d9SShawn McCarney * You may obtain a copy of the License at 7e0c6a2d9SShawn McCarney * 8e0c6a2d9SShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0 9e0c6a2d9SShawn McCarney * 10e0c6a2d9SShawn McCarney * Unless required by applicable law or agreed to in writing, software 11e0c6a2d9SShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS, 12e0c6a2d9SShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e0c6a2d9SShawn McCarney * See the License for the specific language governing permissions and 14e0c6a2d9SShawn McCarney * limitations under the License. 15e0c6a2d9SShawn McCarney */ 1629e9e385SMatthew Barth #pragma once 1729e9e385SMatthew Barth 18cee2e20aSShawn McCarney #include "compatible_system_types_finder.hpp" 19b464c8bdSShawn McCarney #include "services.hpp" 20e0c6a2d9SShawn McCarney #include "system.hpp" 21e0c6a2d9SShawn McCarney 2229e9e385SMatthew Barth #include <interfaces/manager_interface.hpp> 2329e9e385SMatthew Barth #include <sdbusplus/bus.hpp> 2429e9e385SMatthew Barth #include <sdbusplus/server/object.hpp> 25f2bcf1f9SMatthew Barth #include <sdeventplus/event.hpp> 267cbc5536SMatthew Barth #include <sdeventplus/source/signal.hpp> 27f2bcf1f9SMatthew Barth #include <sdeventplus/utility/timer.hpp> 2829e9e385SMatthew Barth 29e0c6a2d9SShawn McCarney #include <filesystem> 30e0c6a2d9SShawn McCarney #include <memory> 31e0c6a2d9SShawn McCarney #include <string> 32e0c6a2d9SShawn McCarney #include <vector> 33bbc7c583SMatthew Barth 3429e9e385SMatthew Barth namespace phosphor::power::regulators 3529e9e385SMatthew Barth { 3629e9e385SMatthew Barth 37f2bcf1f9SMatthew Barth using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; 38f2bcf1f9SMatthew Barth 397354ce62SPatrick Williams using ManagerObject = sdbusplus::server::object_t< 4029e9e385SMatthew Barth phosphor::power::regulators::interface::ManagerInterface>; 4129e9e385SMatthew Barth 4229e9e385SMatthew Barth class Manager : public ManagerObject 4329e9e385SMatthew Barth { 4429e9e385SMatthew Barth public: 4529e9e385SMatthew Barth Manager() = delete; 4629e9e385SMatthew Barth Manager(const Manager&) = delete; 4729e9e385SMatthew Barth Manager(Manager&&) = delete; 4829e9e385SMatthew Barth Manager& operator=(const Manager&) = delete; 4929e9e385SMatthew Barth Manager& operator=(Manager&&) = delete; 5029e9e385SMatthew Barth ~Manager() = default; 5129e9e385SMatthew Barth 5229e9e385SMatthew Barth /** 5329e9e385SMatthew Barth * Constructor 5429e9e385SMatthew Barth * Creates a manager over the regulators. 5529e9e385SMatthew Barth * 56b464c8bdSShawn McCarney * @param bus the D-Bus bus 57e0c6a2d9SShawn McCarney * @param event the sdevent event 5829e9e385SMatthew Barth */ 597354ce62SPatrick Williams Manager(sdbusplus::bus_t& bus, const sdeventplus::Event& event); 6029e9e385SMatthew Barth 6129e9e385SMatthew Barth /** 62d9c8be57SShawn McCarney * Implements the D-Bus "configure" method. 63d9c8be57SShawn McCarney * 64d9c8be57SShawn McCarney * Configures all the voltage regulators in the system. 65d9c8be57SShawn McCarney * 66d9c8be57SShawn McCarney * This method should be called when the system is being powered on. It 67d9c8be57SShawn McCarney * needs to occur before the regulators have been enabled/turned on. 6829e9e385SMatthew Barth */ 6929e9e385SMatthew Barth void configure() override; 7029e9e385SMatthew Barth 7129e9e385SMatthew Barth /** 72d9c8be57SShawn McCarney * Implements the D-Bus "monitor" method. 7329e9e385SMatthew Barth * 74d9c8be57SShawn McCarney * Sets whether regulator monitoring is enabled. 75d9c8be57SShawn McCarney * 76d9c8be57SShawn McCarney * When monitoring is enabled: 77d9c8be57SShawn McCarney * - regulator sensors will be read and published on D-Bus 78d9c8be57SShawn McCarney * - phase fault detection will be performed 79d9c8be57SShawn McCarney * 80d9c8be57SShawn McCarney * Regulator monitoring should be enabled when the system is being powered 81d9c8be57SShawn McCarney * on. It needs to occur after the regulators have been configured and 82d9c8be57SShawn McCarney * enabled/turned on. 83d9c8be57SShawn McCarney * 84d9c8be57SShawn McCarney * Regulator monitoring should be disabled when the system is being powered 85d9c8be57SShawn McCarney * off. It needs to occur before the regulators have been disabled/turned 86d9c8be57SShawn McCarney * off. 87d9c8be57SShawn McCarney * 88d9c8be57SShawn McCarney * Regulator monitoring can also be temporarily disabled and then re-enabled 89d9c8be57SShawn McCarney * while the system is powered on. This allows other applications or tools 90d9c8be57SShawn McCarney * to temporarily communicate with the regulators for testing or debug. 91d9c8be57SShawn McCarney * Monitoring should be disabled for only short periods of time; other 92d9c8be57SShawn McCarney * applications, such as fan control, may be dependent on regulator sensors. 93d9c8be57SShawn McCarney * 94d9c8be57SShawn McCarney * @param enable true if monitoring should be enabled, false if it should be 95d9c8be57SShawn McCarney * disabled 9629e9e385SMatthew Barth */ 9729e9e385SMatthew Barth void monitor(bool enable) override; 9829e9e385SMatthew Barth 99f2bcf1f9SMatthew Barth /** 100cee2e20aSShawn McCarney * Callback that is called when a list of compatible system types is found. 101cee2e20aSShawn McCarney * 102cee2e20aSShawn McCarney * @param types Compatible system types for the current system ordered from 103cee2e20aSShawn McCarney * most to least specific 104cee2e20aSShawn McCarney */ 105cee2e20aSShawn McCarney void compatibleSystemTypesFound(const std::vector<std::string>& types); 106cee2e20aSShawn McCarney 107cee2e20aSShawn McCarney /** 108c8cbeac2SShawn McCarney * Phase fault detection timer expired callback function. 109c8cbeac2SShawn McCarney */ 110c8cbeac2SShawn McCarney void phaseFaultTimerExpired(); 111c8cbeac2SShawn McCarney 112c8cbeac2SShawn McCarney /** 113c8cbeac2SShawn McCarney * Sensor monitoring timer expired callback function. 114c8cbeac2SShawn McCarney */ 115c8cbeac2SShawn McCarney void sensorTimerExpired(); 116c8cbeac2SShawn McCarney 117c8cbeac2SShawn McCarney /** 118e0c6a2d9SShawn McCarney * Callback function to handle receiving a HUP signal 1197cbc5536SMatthew Barth * to reload the configuration data. 1207cbc5536SMatthew Barth * 121e0c6a2d9SShawn McCarney * @param sigSrc sd_event_source signal wrapper 122e0c6a2d9SShawn McCarney * @param sigInfo signal info on signal fd 1237cbc5536SMatthew Barth */ 1247cbc5536SMatthew Barth void sighupHandler(sdeventplus::source::Signal& sigSrc, 1257cbc5536SMatthew Barth const struct signalfd_siginfo* sigInfo); 1267cbc5536SMatthew Barth 12729e9e385SMatthew Barth private: 12829e9e385SMatthew Barth /** 1299bd94d36SShawn McCarney * Clear any cached data or error history related to hardware devices. 1309bd94d36SShawn McCarney * 1319bd94d36SShawn McCarney * This method should be called when the system is powering on (booting). 1329bd94d36SShawn McCarney * While the system was powered off, hardware could have been added, 1339bd94d36SShawn McCarney * removed, or replaced. 1349bd94d36SShawn McCarney */ 1359bd94d36SShawn McCarney void clearHardwareData(); 1369bd94d36SShawn McCarney 1379bd94d36SShawn McCarney /** 138e0c6a2d9SShawn McCarney * Finds the JSON configuration file. 139e0c6a2d9SShawn McCarney * 140d9c8be57SShawn McCarney * Looks for a configuration file based on the list of compatible system 141589c181aSShawn McCarney * types. If no file is found, looks for a file with the default name. 142e0c6a2d9SShawn McCarney * 143589c181aSShawn McCarney * Looks for the file in the test directory and standard directory. 144e0c6a2d9SShawn McCarney * 145589c181aSShawn McCarney * Throws an exception if an operating system error occurs while checking 146d9c8be57SShawn McCarney * for the existence of a file. 147e0c6a2d9SShawn McCarney * 148589c181aSShawn McCarney * @return absolute path to config file, or an empty path if none found 149e0c6a2d9SShawn McCarney */ 150e0c6a2d9SShawn McCarney std::filesystem::path findConfigFile(); 151e0c6a2d9SShawn McCarney 152e0c6a2d9SShawn McCarney /** 1538acaf547SShawn McCarney * Returns whether the JSON configuration file has been loaded. 1548acaf547SShawn McCarney * 1558acaf547SShawn McCarney * @return true if config file loaded, false otherwise 1568acaf547SShawn McCarney */ isConfigFileLoaded() const1578acaf547SShawn McCarney bool isConfigFileLoaded() const 1588acaf547SShawn McCarney { 1598acaf547SShawn McCarney // If System object exists, the config file has been loaded 1608acaf547SShawn McCarney return (system != nullptr); 1618acaf547SShawn McCarney } 1628acaf547SShawn McCarney 1638acaf547SShawn McCarney /** 164d9c8be57SShawn McCarney * Returns whether the system is currently powered on. 165d9c8be57SShawn McCarney * 166d9c8be57SShawn McCarney * @return true if system is powered on, false otherwise 167d9c8be57SShawn McCarney */ 168d9c8be57SShawn McCarney bool isSystemPoweredOn(); 169d9c8be57SShawn McCarney 170d9c8be57SShawn McCarney /** 171e0c6a2d9SShawn McCarney * Loads the JSON configuration file. 172e0c6a2d9SShawn McCarney * 173589c181aSShawn McCarney * Looks for the config file using findConfigFile(). 174e0c6a2d9SShawn McCarney * 175e0c6a2d9SShawn McCarney * If the config file is found, it is parsed and the resulting information 176589c181aSShawn McCarney * is stored in the system data member. If parsing fails, an error is 177589c181aSShawn McCarney * logged. 178e0c6a2d9SShawn McCarney */ 179e0c6a2d9SShawn McCarney void loadConfigFile(); 180e0c6a2d9SShawn McCarney 181e0c6a2d9SShawn McCarney /** 1828acaf547SShawn McCarney * Waits until the JSON configuration file has been loaded. 1838acaf547SShawn McCarney * 1848acaf547SShawn McCarney * If the config file has not yet been loaded, waits until one of the 1858acaf547SShawn McCarney * following occurs: 1868acaf547SShawn McCarney * - config file is loaded 1878acaf547SShawn McCarney * - maximum amount of time to wait has elapsed 1888acaf547SShawn McCarney */ 1898acaf547SShawn McCarney void waitUntilConfigFileLoaded(); 1908acaf547SShawn McCarney 1918acaf547SShawn McCarney /** 192b464c8bdSShawn McCarney * The D-Bus bus 193e0c6a2d9SShawn McCarney */ 1947354ce62SPatrick Williams sdbusplus::bus_t& bus; 195e0c6a2d9SShawn McCarney 196e0c6a2d9SShawn McCarney /** 197e0c6a2d9SShawn McCarney * Event to loop on 198e0c6a2d9SShawn McCarney */ 199*e1a07595SJayanth Othayoth const sdeventplus::Event& eventLoop [[maybe_unused]]; 200e0c6a2d9SShawn McCarney 201e0c6a2d9SShawn McCarney /** 202b464c8bdSShawn McCarney * System services like error logging and the journal. 203b464c8bdSShawn McCarney */ 204b464c8bdSShawn McCarney BMCServices services; 205b464c8bdSShawn McCarney 206b464c8bdSShawn McCarney /** 207cee2e20aSShawn McCarney * Object that finds the compatible system types for the current system. 208cee2e20aSShawn McCarney */ 209cee2e20aSShawn McCarney std::unique_ptr<util::CompatibleSystemTypesFinder> compatSysTypesFinder; 210cee2e20aSShawn McCarney 211cee2e20aSShawn McCarney /** 212c8cbeac2SShawn McCarney * Event timer used to initiate phase fault detection. 213e0c6a2d9SShawn McCarney */ 214c8cbeac2SShawn McCarney Timer phaseFaultTimer; 215c8cbeac2SShawn McCarney 216c8cbeac2SShawn McCarney /** 217c8cbeac2SShawn McCarney * Event timer used to initiate sensor monitoring. 218c8cbeac2SShawn McCarney */ 219c8cbeac2SShawn McCarney Timer sensorTimer; 220e0c6a2d9SShawn McCarney 221e0c6a2d9SShawn McCarney /** 222d9c8be57SShawn McCarney * Indicates whether regulator monitoring is enabled. 223d9c8be57SShawn McCarney */ 224d9c8be57SShawn McCarney bool isMonitoringEnabled{false}; 225d9c8be57SShawn McCarney 226d9c8be57SShawn McCarney /** 227589c181aSShawn McCarney * List of compatible system types for the current system. 228589c181aSShawn McCarney * 229589c181aSShawn McCarney * Used to find the JSON configuration file. 230e0c6a2d9SShawn McCarney */ 231589c181aSShawn McCarney std::vector<std::string> compatibleSystemTypes{}; 232e0c6a2d9SShawn McCarney 233e0c6a2d9SShawn McCarney /** 234e0c6a2d9SShawn McCarney * Computer system being controlled and monitored by the BMC. 235e0c6a2d9SShawn McCarney * 236e0c6a2d9SShawn McCarney * Contains the information loaded from the JSON configuration file. 237e0c6a2d9SShawn McCarney * Contains nullptr if the configuration file has not been loaded. 238e0c6a2d9SShawn McCarney */ 239e0c6a2d9SShawn McCarney std::unique_ptr<System> system{}; 24029e9e385SMatthew Barth }; 24129e9e385SMatthew Barth 24229e9e385SMatthew Barth } // namespace phosphor::power::regulators 243