blob: 077d272920d31ba729607c57a81f4b91c48896bf [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:
Patrick Venture5f59c0f2018-11-11 12:55:14 -080046 PIDZone(int64_t zone, double minThermalRpm, 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),
50 _zoneId(zone), _maximumRPMSetPt(), _minThermalRpmSetPt(minThermalRpm),
51 _failSafePercent(failSafePercent), _mgr(mgr)
52 {
Patrick Ventured8012182018-03-08 08:21:38 -080053#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070054 _log.open("/tmp/swampd.log");
Patrick Ventured8012182018-03-08 08:21:38 -080055#endif
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070056 }
Patrick Ventured8012182018-03-08 08:21:38 -080057
Patrick Venture5f59c0f2018-11-11 12:55:14 -080058 double getMaxRPMRequest(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070059 bool getManualMode(void) const;
Patrick Ventured8012182018-03-08 08:21:38 -080060
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070061 /* Could put lock around this since it's accessed from two threads, but
62 * only one reader/one writer.
63 */
64 void setManualMode(bool mode);
65 bool getFailSafeMode(void) const override;
Patrick Venture0bbeaf82018-10-30 18:50:31 -070066 int64_t getZoneID(void) const;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080067 void addRPMSetPoint(double setpoint) override;
James Feist608304d2019-02-25 10:01:42 -080068 void addRPMCeiling(double ceiling) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070069 void clearRPMSetPoints(void);
James Feist608304d2019-02-25 10:01:42 -080070 void clearRPMCeilings(void);
Patrick Venture5f59c0f2018-11-11 12:55:14 -080071 double getFailSafePercent(void) const override;
72 double getMinThermalRPMSetpoint(void) const;
Patrick Ventured8012182018-03-08 08:21:38 -080073
Patrick Venture2d8e7852018-10-30 19:14:07 -070074 Sensor* getSensor(const std::string& name) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070075 void determineMaxRPMRequest(void);
76 void updateFanTelemetry(void);
77 void updateSensors(void);
78 void initializeCache(void);
79 void dumpCache(void);
Patrick Venture563a3562018-10-30 09:31:26 -070080 void processFans(void);
81 void processThermals(void);
Patrick Ventured8012182018-03-08 08:21:38 -080082
James Feist22c257a2018-08-31 14:07:12 -070083 void addFanPID(std::unique_ptr<Controller> pid);
84 void addThermalPID(std::unique_ptr<Controller> pid);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070085 double getCachedValue(const std::string& name) override;
Patrick Venturec399f6f2018-10-30 19:24:47 -070086 void addFanInput(const std::string& fan);
87 void addThermalInput(const std::string& therm);
Patrick Ventured8012182018-03-08 08:21:38 -080088
89#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070090 void initializeLog(void);
91 std::ofstream& getLogHandle(void);
Patrick Ventured8012182018-03-08 08:21:38 -080092#endif
93
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070094 /* Method for setting the manual mode over dbus */
95 bool manual(bool value) override;
96 /* Method for reading whether in fail-safe mode over dbus */
97 bool failSafe() const override;
Patrick Ventured8012182018-03-08 08:21:38 -080098
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070099 private:
Patrick Ventured8012182018-03-08 08:21:38 -0800100#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700101 std::ofstream _log;
Patrick Ventured8012182018-03-08 08:21:38 -0800102#endif
103
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700104 const int64_t _zoneId;
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800105 double _maximumRPMSetPt = 0;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700106 bool _manualMode = false;
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800107 const double _minThermalRpmSetPt;
108 const double _failSafePercent;
Patrick Ventured8012182018-03-08 08:21:38 -0800109
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700110 std::set<std::string> _failSafeSensors;
Patrick Ventured8012182018-03-08 08:21:38 -0800111
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800112 std::vector<double> _RPMSetPoints;
James Feist608304d2019-02-25 10:01:42 -0800113 std::vector<double> _RPMCeilings;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700114 std::vector<std::string> _fanInputs;
115 std::vector<std::string> _thermalInputs;
116 std::map<std::string, double> _cachedValuesByName;
117 const SensorManager& _mgr;
Patrick Ventured8012182018-03-08 08:21:38 -0800118
James Feist22c257a2018-08-31 14:07:12 -0700119 std::vector<std::unique_ptr<Controller>> _fans;
120 std::vector<std::unique_ptr<Controller>> _thermals;
Patrick Ventured8012182018-03-08 08:21:38 -0800121};