xref: /openbmc/phosphor-pid-control/pid/builder.cpp (revision 46a755fce8dc0bdd9c0c5ea09d55d3e5494f335f)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2017 Google Inc
3 
4 #include "pid/builder.hpp"
5 
6 #include "conf.hpp"
7 #include "manager.hpp"
8 #include "pid/controller.hpp"
9 #include "pid/fancontroller.hpp"
10 #include "pid/stepwisecontroller.hpp"
11 #include "pid/thermalcontroller.hpp"
12 #include "pid/zone.hpp"
13 #include "pid/zone_interface.hpp"
14 #include "util.hpp"
15 
16 #include <sdbusplus/bus.hpp>
17 
18 #include <cstdint>
19 #include <iostream>
20 #include <map>
21 #include <memory>
22 #include <stdexcept>
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 
28 namespace pid_control
29 {
30 
31 static constexpr bool deferSignals = true;
32 static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
33 
getControlPath(int64_t zone)34 static std::string getControlPath(int64_t zone)
35 {
36     return std::string(objectPath) + std::to_string(zone);
37 }
38 
getPidControlPath(int64_t zone,const std::string & pidname)39 static std::string getPidControlPath(int64_t zone, const std::string& pidname)
40 {
41     return std::string(objectPath) + std::to_string(zone) + "/" + pidname;
42 }
43 
buildZones(const std::map<int64_t,conf::PIDConf> & zonePids,std::map<int64_t,conf::ZoneConfig> & zoneConfigs,SensorManager & mgr,sdbusplus::bus_t & modeControlBus)44 std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> buildZones(
45     const std::map<int64_t, conf::PIDConf>& zonePids,
46     std::map<int64_t, conf::ZoneConfig>& zoneConfigs, SensorManager& mgr,
47     sdbusplus::bus_t& modeControlBus)
48 {
49     std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
50 
51     for (const auto& [zoneId, pidConfig] : zonePids)
52     {
53         /* The above shouldn't be necessary but is, and I am having trouble
54          * locating my notes on why.  If I recall correctly it was casting it
55          * down to a byte in at least some cases causing weird behaviors.
56          */
57         auto zoneConf = zoneConfigs.find(zoneId);
58         if (zoneConf == zoneConfigs.end())
59         {
60             /* The Zone doesn't have a configuration, bail. */
61             static constexpr auto err =
62                 "Bailing during load, missing Zone Configuration";
63             std::cerr << err << std::endl;
64             throw std::runtime_error(err);
65         }
66 
67         auto zone = std::make_shared<DbusPidZone>(
68             zoneId, zoneConf->second.minThermalOutput,
69             zoneConf->second.failsafePercent, zoneConf->second.cycleTime, mgr,
70             modeControlBus, getControlPath(zoneId).c_str(), deferSignals,
71             zoneConf->second.accumulateSetPoint);
72 
73         std::cerr << "Zone Id: " << zone->getZoneID() << "\n";
74 
75         // For each PID create a Controller and a Sensor.
76         for (const auto& [name, info] : pidConfig)
77         {
78             std::vector<pid_control::conf::SensorInput> inputs;
79             std::cerr << "PID name: " << name << "\n";
80 
81             /*
82              * TODO(venture): Need to check if input is known to the
83              * SensorManager.
84              */
85             if (info.type == "fan")
86             {
87                 for (const auto& i : info.inputs)
88                 {
89                     inputs.push_back(i);
90                     zone->addFanInput(i.name, i.missingIsAcceptable);
91                 }
92 
93                 auto pid = FanController::createFanPid(
94                     zone.get(), name, splitNames(inputs), info.pidInfo);
95                 zone->addFanPID(std::move(pid));
96                 zone->addPidFailSafePercent(splitNames(inputs),
97                                             info.failSafePercent);
98             }
99             else if (isThermalType(info.type))
100             {
101                 for (const auto& i : info.inputs)
102                 {
103                     inputs.push_back(i);
104                     zone->addThermalInput(i.name, i.missingIsAcceptable);
105                 }
106 
107                 auto pid = ThermalController::createThermalPid(
108                     zone.get(), name, inputs, info.setpoint, info.pidInfo,
109                     getThermalType(info.type));
110 
111                 zone->addThermalPID(std::move(pid));
112                 zone->addPidControlProcess(
113                     name, info.type, info.setpoint, modeControlBus,
114                     getPidControlPath(zoneId, name), deferSignals);
115                 zone->addPidFailSafePercent(splitNames(inputs),
116                                             info.failSafePercent);
117             }
118             else if (info.type == "stepwise")
119             {
120                 for (const auto& i : info.inputs)
121                 {
122                     inputs.push_back(i);
123                     zone->addThermalInput(i.name, i.missingIsAcceptable);
124                 }
125                 auto stepwise = StepwiseController::createStepwiseController(
126                     zone.get(), name, splitNames(inputs), info.stepwiseInfo);
127                 zone->addThermalPID(std::move(stepwise));
128                 zone->addPidControlProcess(
129                     name, info.type, info.setpoint, modeControlBus,
130                     getPidControlPath(zoneId, name), deferSignals);
131                 zone->addPidFailSafePercent(splitNames(inputs),
132                                             info.failSafePercent);
133             }
134 
135             std::cerr << "inputs: ";
136             for (const auto& i : inputs)
137             {
138                 std::cerr << i.name;
139                 if (i.convertTempToMargin)
140                 {
141                     std::cerr << "[" << i.convertMarginZero << "]";
142                 }
143                 if (i.missingIsAcceptable)
144                 {
145                     std::cerr << "?";
146                 }
147                 std::cerr << ", ";
148             }
149             std::cerr << "\n";
150         }
151 
152         zone->emit_object_added();
153         zones[zoneId] = std::move(zone);
154     }
155 
156     return zones;
157 }
158 
159 } // namespace pid_control
160