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