blob: 94082e98b1803a71a392cd4f15028977a8739cbe [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001#pragma once
2
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "conf.hpp"
4#include "controller.hpp"
James Feist22c257a2018-08-31 14:07:12 -07005#include "pidcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include "sensors/manager.hpp"
7#include "sensors/sensor.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07008
Patrick Ventured8012182018-03-08 08:21:38 -08009#include <fstream>
10#include <map>
11#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070012#include <sdbusplus/bus.hpp>
13#include <sdbusplus/server.hpp>
Patrick Ventured8012182018-03-08 08:21:38 -080014#include <set>
15#include <string>
16#include <vector>
Patrick Venture1248b152018-10-30 19:09:54 -070017#include <xyz/openbmc_project/Control/Mode/server.hpp>
Patrick Ventured8012182018-03-08 08:21:38 -080018
Patrick Ventured8012182018-03-08 08:21:38 -080019template <typename... T>
20using ServerObject = typename sdbusplus::server::object::object<T...>;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070021using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
Patrick Ventured8012182018-03-08 08:21:38 -080022using ModeObject = ServerObject<ModeInterface>;
23
Patrick Venturea58197c2018-06-11 15:29:45 -070024class ZoneInterface
25{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026 public:
27 virtual ~ZoneInterface() = default;
Patrick Venturea58197c2018-06-11 15:29:45 -070028
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029 virtual double getCachedValue(const std::string& name) = 0;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080030 virtual void addRPMSetPoint(double setpoint) = 0;
James Feist608304d2019-02-25 10:01:42 -080031 virtual void addRPMCeiling(double ceiling) = 0;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080032 virtual double getMaxRPMRequest() const = 0;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070033 virtual bool getFailSafeMode() const = 0;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080034 virtual double getFailSafePercent() const = 0;
Patrick Venture2d8e7852018-10-30 19:14:07 -070035 virtual Sensor* getSensor(const std::string& name) = 0;
Patrick Venturea58197c2018-06-11 15:29:45 -070036};
37
Patrick Ventured8012182018-03-08 08:21:38 -080038/*
39 * The PIDZone inherits from the Mode object so that it can listen for control
40 * mode changes. It primarily holds all PID loops and holds the sensor value
41 * cache that's used per iteration of the PID loops.
42 */
Patrick Venturea58197c2018-06-11 15:29:45 -070043class PIDZone : public ZoneInterface, public ModeObject
Patrick Ventured8012182018-03-08 08:21:38 -080044{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045 public:
James Feist3484bed2019-02-25 13:28:18 -080046 PIDZone(int64_t zone, double minThermalOutput, double failSafePercent,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070047 const SensorManager& mgr, sdbusplus::bus::bus& bus,
48 const char* objPath, bool defer) :
49 ModeObject(bus, objPath, defer),
James Feist3484bed2019-02-25 13:28:18 -080050 _zoneId(zone), _maximumRPMSetPt(),
51 _minThermalOutputSetPt(minThermalOutput),
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052 _failSafePercent(failSafePercent), _mgr(mgr)
53 {
Patrick Ventured8012182018-03-08 08:21:38 -080054#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070055 _log.open("/tmp/swampd.log");
Patrick Ventured8012182018-03-08 08:21:38 -080056#endif
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057 }
Patrick Ventured8012182018-03-08 08:21:38 -080058
Patrick Venture5f59c0f2018-11-11 12:55:14 -080059 double getMaxRPMRequest(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 bool getManualMode(void) const;
Patrick Ventured8012182018-03-08 08:21:38 -080061
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070062 /* Could put lock around this since it's accessed from two threads, but
63 * only one reader/one writer.
64 */
65 void setManualMode(bool mode);
66 bool getFailSafeMode(void) const override;
Patrick Venture0bbeaf82018-10-30 18:50:31 -070067 int64_t getZoneID(void) const;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080068 void addRPMSetPoint(double setpoint) override;
James Feist608304d2019-02-25 10:01:42 -080069 void addRPMCeiling(double ceiling) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070070 void clearRPMSetPoints(void);
James Feist608304d2019-02-25 10:01:42 -080071 void clearRPMCeilings(void);
Patrick Venture5f59c0f2018-11-11 12:55:14 -080072 double getFailSafePercent(void) const override;
73 double getMinThermalRPMSetpoint(void) const;
Patrick Ventured8012182018-03-08 08:21:38 -080074
Patrick Venture2d8e7852018-10-30 19:14:07 -070075 Sensor* getSensor(const std::string& name) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070076 void determineMaxRPMRequest(void);
77 void updateFanTelemetry(void);
78 void updateSensors(void);
79 void initializeCache(void);
80 void dumpCache(void);
Patrick Venture563a3562018-10-30 09:31:26 -070081 void processFans(void);
82 void processThermals(void);
Patrick Ventured8012182018-03-08 08:21:38 -080083
James Feist22c257a2018-08-31 14:07:12 -070084 void addFanPID(std::unique_ptr<Controller> pid);
85 void addThermalPID(std::unique_ptr<Controller> pid);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070086 double getCachedValue(const std::string& name) override;
Patrick Venturec399f6f2018-10-30 19:24:47 -070087 void addFanInput(const std::string& fan);
88 void addThermalInput(const std::string& therm);
Patrick Ventured8012182018-03-08 08:21:38 -080089
90#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070091 void initializeLog(void);
92 std::ofstream& getLogHandle(void);
Patrick Ventured8012182018-03-08 08:21:38 -080093#endif
94
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070095 /* Method for setting the manual mode over dbus */
96 bool manual(bool value) override;
97 /* Method for reading whether in fail-safe mode over dbus */
98 bool failSafe() const override;
Patrick Ventured8012182018-03-08 08:21:38 -080099
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700100 private:
Patrick Ventured8012182018-03-08 08:21:38 -0800101#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700102 std::ofstream _log;
Patrick Ventured8012182018-03-08 08:21:38 -0800103#endif
104
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700105 const int64_t _zoneId;
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800106 double _maximumRPMSetPt = 0;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700107 bool _manualMode = false;
James Feist3484bed2019-02-25 13:28:18 -0800108 const double _minThermalOutputSetPt;
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800109 const double _failSafePercent;
Patrick Ventured8012182018-03-08 08:21:38 -0800110
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700111 std::set<std::string> _failSafeSensors;
Patrick Ventured8012182018-03-08 08:21:38 -0800112
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800113 std::vector<double> _RPMSetPoints;
James Feist608304d2019-02-25 10:01:42 -0800114 std::vector<double> _RPMCeilings;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700115 std::vector<std::string> _fanInputs;
116 std::vector<std::string> _thermalInputs;
117 std::map<std::string, double> _cachedValuesByName;
118 const SensorManager& _mgr;
Patrick Ventured8012182018-03-08 08:21:38 -0800119
James Feist22c257a2018-08-31 14:07:12 -0700120 std::vector<std::unique_ptr<Controller>> _fans;
121 std::vector<std::unique_ptr<Controller>> _thermals;
Patrick Ventured8012182018-03-08 08:21:38 -0800122};