xref: /openbmc/phosphor-pid-control/pid/builder.cpp (revision 734f953519fbb21a277313152399971744df158f)
1 /**
2  * Copyright 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "pid/builder.hpp"
18 
19 #include "conf.hpp"
20 #include "pid/controller.hpp"
21 #include "pid/fancontroller.hpp"
22 #include "pid/stepwisecontroller.hpp"
23 #include "pid/thermalcontroller.hpp"
24 
25 #include <iostream>
26 #include <memory>
27 #include <sdbusplus/bus.hpp>
28 #include <unordered_map>
29 
30 static constexpr bool deferSignals = true;
31 static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
32 
33 static std::string getControlPath(int64_t zone)
34 {
35     return std::string(objectPath) + std::to_string(zone);
36 }
37 
38 std::unordered_map<int64_t, std::unique_ptr<PIDZone>>
39     buildZones(std::map<int64_t, PIDConf>& zonePids,
40                std::map<int64_t, struct ZoneConfig>& zoneConfigs,
41                SensorManager& mgr, sdbusplus::bus::bus& modeControlBus)
42 {
43     std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
44 
45     for (const auto& zi : zonePids)
46     {
47         auto zoneId = static_cast<int64_t>(zi.first);
48         /* The above shouldn't be necessary but is, and I am having trouble
49          * locating my notes on why.  If I recall correctly it was casting it
50          * down to a byte in at least some cases causing weird behaviors.
51          */
52 
53         auto zoneConf = zoneConfigs.find(zoneId);
54         if (zoneConf == zoneConfigs.end())
55         {
56             /* The Zone doesn't have a configuration, bail. */
57             static constexpr auto err =
58                 "Bailing during load, missing Zone Configuration";
59             std::cerr << err << std::endl;
60             throw std::runtime_error(err);
61         }
62 
63         const PIDConf& pidConfig = zi.second;
64 
65         auto zone = std::make_unique<PIDZone>(
66             zoneId, zoneConf->second.minthermalrpm,
67             zoneConf->second.failsafepercent, mgr, modeControlBus,
68             getControlPath(zi.first).c_str(), deferSignals);
69 
70         std::cerr << "Zone Id: " << zone->getZoneID() << "\n";
71 
72         // For each PID create a Controller and a Sensor.
73         for (const auto& pit : pidConfig)
74         {
75             std::vector<std::string> inputs;
76             std::string name = pit.first;
77             const struct ControllerInfo* info = &pit.second;
78 
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);
91                 }
92 
93                 auto pid = FanController::createFanPid(zone.get(), name, inputs,
94                                                        info->pidInfo);
95                 zone->addFanPID(std::move(pid));
96             }
97             else if (info->type == "temp" || info->type == "margin")
98             {
99                 for (const auto& i : info->inputs)
100                 {
101                     inputs.push_back(i);
102                     zone->addThermalInput(i);
103                 }
104 
105                 ThermalType type;
106                 if (info->type == "temp")
107                 {
108                     type = ThermalType::absolute;
109                 }
110                 else
111                 {
112                     type = ThermalType::margin;
113                 }
114 
115                 auto pid = ThermalController::createThermalPid(
116                     zone.get(), name, inputs, info->setpoint, info->pidInfo,
117                     type);
118 
119                 zone->addThermalPID(std::move(pid));
120             }
121             else if (info->type == "stepwise")
122             {
123                 for (const auto& i : info->inputs)
124                 {
125                     inputs.push_back(i);
126                     zone->addThermalInput(i);
127                 }
128                 auto stepwise = StepwiseController::createStepwiseController(
129                     zone.get(), name, inputs, info->stepwiseInfo);
130                 zone->addThermalPID(std::move(stepwise));
131             }
132 
133             std::cerr << "inputs: ";
134             for (const auto& i : inputs)
135             {
136                 std::cerr << i << ", ";
137             }
138             std::cerr << "\n";
139         }
140 
141         zone->emit_object_added();
142         zones[zoneId] = std::move(zone);
143     }
144 
145     return zones;
146 }
147