blob: 39b29d6a8952f4e0ea63453b6a56ff5c78b6c8a8 [file] [log] [blame]
Patrick Venture5c7cc542018-06-11 14:29:38 -07001/**
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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070019#include "conf.hpp"
James Feist22c257a2018-08-31 14:07:12 -070020#include "pid/controller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070021#include "pid/fancontroller.hpp"
James Feist22c257a2018-08-31 14:07:12 -070022#include "pid/stepwisecontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023#include "pid/thermalcontroller.hpp"
Patrick Venture7a98c192020-08-12 08:35:16 -070024#include "pid/zone.hpp"
25#include "pid/zone_interface.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026
Patrick Venturea83a3ec2020-08-04 09:52:05 -070027#include <sdbusplus/bus.hpp>
28
Patrick Venture5c7cc542018-06-11 14:29:38 -070029#include <iostream>
30#include <memory>
Patrick Venture5c7cc542018-06-11 14:29:38 -070031#include <unordered_map>
32
Patrick Venturea0764872020-08-08 07:48:43 -070033namespace pid_control
34{
35
Patrick Venture5c7cc542018-06-11 14:29:38 -070036static constexpr bool deferSignals = true;
37static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
38
Patrick Venture7af157b2018-10-30 11:24:40 -070039static std::string getControlPath(int64_t zone)
Patrick Venture5c7cc542018-06-11 14:29:38 -070040{
41 return std::string(objectPath) + std::to_string(zone);
42}
43
Patrick Venture7a98c192020-08-12 08:35:16 -070044std::unordered_map<int64_t, std::unique_ptr<ZoneInterface>>
Patrick Venture7e3f8ab2020-08-03 11:10:43 -070045 buildZones(const std::map<int64_t, conf::PIDConf>& zonePids,
James Feistf81f2882019-02-26 11:26:36 -080046 std::map<int64_t, struct conf::ZoneConfig>& zoneConfigs,
Patrick Venturef3252312018-10-30 08:42:53 -070047 SensorManager& mgr, sdbusplus::bus::bus& modeControlBus)
Patrick Venture5c7cc542018-06-11 14:29:38 -070048{
Patrick Venture7a98c192020-08-12 08:35:16 -070049 std::unordered_map<int64_t, std::unique_ptr<ZoneInterface>> zones;
Patrick Venture5c7cc542018-06-11 14:29:38 -070050
Patrick Venture2a50eda2020-08-16 08:26:35 -070051 for (const auto& [zoneId, pidConfig] : zonePids)
Patrick Venture5c7cc542018-06-11 14:29:38 -070052 {
Patrick Venture5c7cc542018-06-11 14:29:38 -070053 /* 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 */
Patrick Venture5c7cc542018-06-11 14:29:38 -070057 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
Patrick Venture597ebd62020-08-11 08:48:19 -070067 auto zone = std::make_unique<DbusPidZone>(
James Feist3484bed2019-02-25 13:28:18 -080068 zoneId, zoneConf->second.minThermalOutput,
Patrick Venture8e2fdb32019-02-11 09:39:59 -080069 zoneConf->second.failsafePercent, mgr, modeControlBus,
Patrick Venture2a50eda2020-08-16 08:26:35 -070070 getControlPath(zoneId).c_str(), deferSignals);
Patrick Venture5c7cc542018-06-11 14:29:38 -070071
Patrick Venture0bbeaf82018-10-30 18:50:31 -070072 std::cerr << "Zone Id: " << zone->getZoneID() << "\n";
Patrick Venture5c7cc542018-06-11 14:29:38 -070073
74 // For each PID create a Controller and a Sensor.
Patrick Venture2a50eda2020-08-16 08:26:35 -070075 for (const auto& [name, info] : pidConfig)
Patrick Venture5c7cc542018-06-11 14:29:38 -070076 {
77 std::vector<std::string> inputs;
Patrick Venture5c7cc542018-06-11 14:29:38 -070078 std::cerr << "PID name: " << name << "\n";
79
80 /*
81 * TODO(venture): Need to check if input is known to the
82 * SensorManager.
83 */
Patrick Venture2a50eda2020-08-16 08:26:35 -070084 if (info.type == "fan")
Patrick Venture5c7cc542018-06-11 14:29:38 -070085 {
Patrick Venture2a50eda2020-08-16 08:26:35 -070086 for (const auto& i : info.inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -070087 {
88 inputs.push_back(i);
89 zone->addFanInput(i);
90 }
91
Patrick Venture563a3562018-10-30 09:31:26 -070092 auto pid = FanController::createFanPid(zone.get(), name, inputs,
Patrick Venture2a50eda2020-08-16 08:26:35 -070093 info.pidInfo);
Patrick Venture5c7cc542018-06-11 14:29:38 -070094 zone->addFanPID(std::move(pid));
95 }
Patrick Venture2a50eda2020-08-16 08:26:35 -070096 else if (isThermalType(info.type))
Patrick Venture5c7cc542018-06-11 14:29:38 -070097 {
Patrick Venture2a50eda2020-08-16 08:26:35 -070098 for (const auto& i : info.inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -070099 {
100 inputs.push_back(i);
101 zone->addThermalInput(i);
102 }
103
Patrick Venture563a3562018-10-30 09:31:26 -0700104 auto pid = ThermalController::createThermalPid(
Patrick Venture2a50eda2020-08-16 08:26:35 -0700105 zone.get(), name, inputs, info.setpoint, info.pidInfo,
106 getThermalType(info.type));
James Feist22c257a2018-08-31 14:07:12 -0700107
Patrick Venture5c7cc542018-06-11 14:29:38 -0700108 zone->addThermalPID(std::move(pid));
109 }
Patrick Venture2a50eda2020-08-16 08:26:35 -0700110 else if (info.type == "stepwise")
James Feist22c257a2018-08-31 14:07:12 -0700111 {
Patrick Venture2a50eda2020-08-16 08:26:35 -0700112 for (const auto& i : info.inputs)
James Feist22c257a2018-08-31 14:07:12 -0700113 {
114 inputs.push_back(i);
115 zone->addThermalInput(i);
116 }
Patrick Venture563a3562018-10-30 09:31:26 -0700117 auto stepwise = StepwiseController::createStepwiseController(
Patrick Venture2a50eda2020-08-16 08:26:35 -0700118 zone.get(), name, inputs, info.stepwiseInfo);
James Feist22c257a2018-08-31 14:07:12 -0700119 zone->addThermalPID(std::move(stepwise));
120 }
Patrick Venture5c7cc542018-06-11 14:29:38 -0700121
122 std::cerr << "inputs: ";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700123 for (const auto& i : inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700124 {
125 std::cerr << i << ", ";
126 }
127 std::cerr << "\n";
128 }
129
130 zone->emit_object_added();
131 zones[zoneId] = std::move(zone);
132 }
133
134 return zones;
135}
Patrick Venturea0764872020-08-08 07:48:43 -0700136
137} // namespace pid_control