Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 3 | #include "conf.hpp" |
| 4 | #include "controller.hpp" |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 5 | #include "pidcontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 6 | #include "sensors/manager.hpp" |
| 7 | #include "sensors/sensor.hpp" |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 8 | #include "tuning.hpp" |
Patrick Venture | 1a15379 | 2020-08-11 08:41:47 -0700 | [diff] [blame] | 9 | #include "zone_interface.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 10 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
| 12 | #include <sdbusplus/server.hpp> |
| 13 | #include <xyz/openbmc_project/Control/Mode/server.hpp> |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Debug/Pid/ThermalPower/server.hpp> |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Debug/Pid/Zone/server.hpp> |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Object/Enable/server.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 17 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 18 | #include <fstream> |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 19 | #include <iostream> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <memory> |
| 22 | #include <set> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 26 | template <typename... T> |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 27 | using ServerObject = typename sdbusplus::server::object_t<T...>; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 28 | using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode; |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 29 | using DebugZoneInterface = |
| 30 | sdbusplus::xyz::openbmc_project::Debug::Pid::server::Zone; |
| 31 | using ModeObject = ServerObject<ModeInterface, DebugZoneInterface>; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 32 | using ProcessInterface = |
| 33 | sdbusplus::xyz::openbmc_project::Object::server::Enable; |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 34 | using DebugThermalPowerInterface = |
| 35 | sdbusplus::xyz::openbmc_project::Debug::Pid::server::ThermalPower; |
| 36 | using ProcessObject = |
| 37 | ServerObject<ProcessInterface, DebugThermalPowerInterface>; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 38 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 39 | namespace pid_control |
| 40 | { |
| 41 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 42 | /* |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 43 | * The DbusPidZone inherits from the Mode object so that it can listen for |
| 44 | * control mode changes. It primarily holds all PID loops and holds the sensor |
| 45 | * value cache that's used per iteration of the PID loops. |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 46 | */ |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 47 | class DbusPidZone : public ZoneInterface, public ModeObject |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 48 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 49 | public: |
Patrick Venture | 597ebd6 | 2020-08-11 08:48:19 -0700 | [diff] [blame] | 50 | DbusPidZone(int64_t zone, double minThermalOutput, double failSafePercent, |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 51 | conf::CycleTime cycleTime, const SensorManager& mgr, |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 52 | sdbusplus::bus_t& bus, const char* objPath, bool defer, |
| 53 | bool accumulateSetPoint) : |
Patrick Williams | d8c5a45 | 2022-04-07 16:08:56 -0500 | [diff] [blame] | 54 | ModeObject(bus, objPath, |
| 55 | defer ? ModeObject::action::defer_emit |
| 56 | : ModeObject::action::emit_object_added), |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 57 | _zoneId(zone), _maximumSetPoint(), |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 58 | _accumulateSetPoint(accumulateSetPoint), |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 59 | _minThermalOutputSetPt(minThermalOutput), |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 60 | _zoneFailSafePercent(failSafePercent), _cycleTime(cycleTime), _mgr(mgr) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 61 | { |
Patrick Venture | de79ee0 | 2019-05-08 14:50:00 -0700 | [diff] [blame] | 62 | if (loggingEnabled) |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 63 | { |
Patrick Venture | 89002db | 2019-05-08 15:02:55 -0700 | [diff] [blame] | 64 | _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log"); |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 65 | } |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 66 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 67 | |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 68 | bool getManualMode(void) const override; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 69 | /* Could put lock around this since it's accessed from two threads, but |
| 70 | * only one reader/one writer. |
| 71 | */ |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 72 | |
| 73 | bool getRedundantWrite(void) const override; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 74 | void setManualMode(bool mode); |
| 75 | bool getFailSafeMode(void) const override; |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 76 | void markSensorMissing(const std::string& name); |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 77 | bool getAccSetPoint(void) const override; |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 78 | |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 79 | int64_t getZoneID(void) const override; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 80 | void addSetPoint(double setPoint, const std::string& name) override; |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 81 | double getMaxSetPointRequest(void) const override; |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 82 | void addRPMCeiling(double ceiling) override; |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 83 | void clearSetPoints(void) override; |
| 84 | void clearRPMCeilings(void) override; |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 85 | double getFailSafePercent(void) override; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 86 | double getMinThermalSetPoint(void) const; |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 87 | uint64_t getCycleIntervalTime(void) const override; |
| 88 | uint64_t getUpdateThermalsCycle(void) const override; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 89 | |
Patrick Venture | 2d8e785 | 2018-10-30 19:14:07 -0700 | [diff] [blame] | 90 | Sensor* getSensor(const std::string& name) override; |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 91 | void determineMaxSetPointRequest(void) override; |
| 92 | void updateFanTelemetry(void) override; |
| 93 | void updateSensors(void) override; |
| 94 | void initializeCache(void) override; |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 95 | void setOutputCache(std::string_view, const ValueCacheEntry&) override; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 96 | void dumpCache(void); |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 97 | |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 98 | void processFans(void) override; |
| 99 | void processThermals(void) override; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 100 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 101 | void addFanPID(std::unique_ptr<Controller> pid); |
| 102 | void addThermalPID(std::unique_ptr<Controller> pid); |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 103 | double getCachedValue(const std::string& name) override; |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 104 | ValueCacheEntry getCachedValues(const std::string& name) override; |
| 105 | |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 106 | void addFanInput(const std::string& fan, bool missingAcceptable); |
| 107 | void addThermalInput(const std::string& therm, bool missingAcceptable); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 108 | |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 109 | void initializeLog(void) override; |
| 110 | void writeLog(const std::string& value) override; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 111 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 112 | /* Method for setting the manual mode over dbus */ |
| 113 | bool manual(bool value) override; |
| 114 | /* Method for reading whether in fail-safe mode over dbus */ |
| 115 | bool failSafe() const override; |
Harvey Wu | cc0232a | 2023-02-09 14:58:55 +0800 | [diff] [blame] | 116 | /* Method for recording the maximum SetPoint PID config name */ |
| 117 | std::string leader() const override; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 118 | /* Method for control process for each loop at runtime */ |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 119 | void addPidControlProcess(std::string name, std::string type, |
| 120 | double setpoint, sdbusplus::bus_t& bus, |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 121 | std::string objPath, bool defer); |
| 122 | bool isPidProcessEnabled(std::string name); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 123 | |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 124 | void addPidFailSafePercent(std::vector<std::string> inputs, double percent); |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 125 | |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 126 | void updateThermalPowerDebugInterface(std::string pidName, |
| 127 | std::string leader, double input, |
| 128 | double output) override; |
| 129 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 130 | private: |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 131 | template <bool fanSensorLogging> |
| 132 | void processSensorInputs(const std::vector<std::string>& sensorInputs, |
| 133 | std::chrono::high_resolution_clock::time_point now) |
| 134 | { |
| 135 | for (const auto& sensorInput : sensorInputs) |
| 136 | { |
| 137 | auto sensor = _mgr.getSensor(sensorInput); |
| 138 | ReadReturn r = sensor->read(); |
| 139 | _cachedValuesByName[sensorInput] = {r.value, r.unscaled}; |
| 140 | int64_t timeout = sensor->getTimeout(); |
| 141 | std::chrono::high_resolution_clock::time_point then = r.updated; |
| 142 | |
| 143 | auto duration = |
| 144 | std::chrono::duration_cast<std::chrono::seconds>(now - then) |
| 145 | .count(); |
| 146 | auto period = std::chrono::seconds(timeout).count(); |
| 147 | /* |
| 148 | * TODO(venture): We should check when these were last read. |
| 149 | * However, these are the fans, so if I'm not getting updated values |
| 150 | * for them... what should I do? |
| 151 | */ |
| 152 | if constexpr (fanSensorLogging) |
| 153 | { |
| 154 | if (loggingEnabled) |
| 155 | { |
| 156 | const auto& v = _cachedValuesByName[sensorInput]; |
| 157 | _log << "," << v.scaled << "," << v.unscaled; |
| 158 | const auto& p = _cachedFanOutputs[sensorInput]; |
| 159 | _log << "," << p.scaled << "," << p.unscaled; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (debugEnabled) |
| 164 | { |
| 165 | std::cerr << sensorInput << " sensor reading: " << r.value |
| 166 | << "\n"; |
| 167 | } |
| 168 | |
| 169 | // check if fan fail. |
| 170 | if (sensor->getFailed()) |
| 171 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 172 | markSensorMissing(sensorInput); |
| 173 | |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 174 | if (debugEnabled) |
| 175 | { |
| 176 | std::cerr << sensorInput << " sensor get failed\n"; |
| 177 | } |
| 178 | } |
| 179 | else if (timeout != 0 && duration >= period) |
| 180 | { |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 181 | markSensorMissing(sensorInput); |
| 182 | |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 183 | if (debugEnabled) |
| 184 | { |
| 185 | std::cerr << sensorInput << " sensor timeout\n"; |
| 186 | } |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | // Check if it's in there: remove it. |
| 191 | auto kt = _failSafeSensors.find(sensorInput); |
| 192 | if (kt != _failSafeSensors.end()) |
| 193 | { |
| 194 | if (debugEnabled) |
| 195 | { |
| 196 | std::cerr << sensorInput |
| 197 | << " is erased from failsafe sensor set\n"; |
| 198 | } |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 199 | |
Tom Tung | df1f183 | 2022-11-14 19:26:52 +0800 | [diff] [blame] | 200 | _failSafeSensors.erase(kt); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 206 | std::ofstream _log; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 207 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 208 | const int64_t _zoneId; |
Patrick Venture | f7a2dd5 | 2019-07-16 14:31:13 -0700 | [diff] [blame] | 209 | double _maximumSetPoint = 0; |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 210 | std::string _maximumSetPointName; |
| 211 | std::string _maximumSetPointNamePrev; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 212 | bool _manualMode = false; |
Josh Lehan | a4146eb | 2020-10-01 11:49:09 -0700 | [diff] [blame] | 213 | bool _redundantWrite = false; |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 214 | bool _accumulateSetPoint = false; |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 215 | const double _minThermalOutputSetPt; |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 216 | // Zone fail safe Percent setting by configuration. |
| 217 | const double _zoneFailSafePercent; |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 218 | const conf::CycleTime _cycleTime; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 219 | |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 220 | std::map<std::string, double> _failSafeSensors; |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 221 | std::set<std::string> _missingAcceptable; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 222 | |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 223 | std::map<std::string, double> _SetPoints; |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 224 | std::vector<double> _RPMCeilings; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 225 | std::vector<std::string> _fanInputs; |
| 226 | std::vector<std::string> _thermalInputs; |
Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 227 | std::map<std::string, ValueCacheEntry> _cachedValuesByName; |
| 228 | std::map<std::string, ValueCacheEntry> _cachedFanOutputs; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 229 | const SensorManager& _mgr; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 230 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 231 | std::vector<std::unique_ptr<Controller>> _fans; |
| 232 | std::vector<std::unique_ptr<Controller>> _thermals; |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 233 | |
| 234 | std::map<std::string, std::unique_ptr<ProcessObject>> _pidsControlProcess; |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 235 | /* |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 236 | * <key = sensor name, value = sensor failsafe percent> |
| 237 | * sensor fail safe Percent setting by each pid controller configuration. |
ykchiu | 9fe3a3c | 2023-05-11 13:43:54 +0800 | [diff] [blame] | 238 | */ |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 239 | std::map<std::string, double> _sensorFailSafePercent; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 240 | }; |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 241 | |
| 242 | } // namespace pid_control |