blob: b76d134eccfce327589eef59534b4ea1f7e9299a [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"
24
Patrick Venturea83a3ec2020-08-04 09:52:05 -070025#include <sdbusplus/bus.hpp>
26
Patrick Venture5c7cc542018-06-11 14:29:38 -070027#include <iostream>
28#include <memory>
Patrick Venture5c7cc542018-06-11 14:29:38 -070029#include <unordered_map>
30
Patrick Venturea0764872020-08-08 07:48:43 -070031namespace pid_control
32{
33
Patrick Venture5c7cc542018-06-11 14:29:38 -070034static constexpr bool deferSignals = true;
35static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
36
Patrick Venture7af157b2018-10-30 11:24:40 -070037static std::string getControlPath(int64_t zone)
Patrick Venture5c7cc542018-06-11 14:29:38 -070038{
39 return std::string(objectPath) + std::to_string(zone);
40}
41
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042std::unordered_map<int64_t, std::unique_ptr<PIDZone>>
Patrick Venture7e3f8ab2020-08-03 11:10:43 -070043 buildZones(const std::map<int64_t, conf::PIDConf>& zonePids,
James Feistf81f2882019-02-26 11:26:36 -080044 std::map<int64_t, struct conf::ZoneConfig>& zoneConfigs,
Patrick Venturef3252312018-10-30 08:42:53 -070045 SensorManager& mgr, sdbusplus::bus::bus& modeControlBus)
Patrick Venture5c7cc542018-06-11 14:29:38 -070046{
47 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
48
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070049 for (const auto& zi : zonePids)
Patrick Venture5c7cc542018-06-11 14:29:38 -070050 {
51 auto zoneId = static_cast<int64_t>(zi.first);
52 /* The above shouldn't be necessary but is, and I am having trouble
53 * locating my notes on why. If I recall correctly it was casting it
54 * down to a byte in at least some cases causing weird behaviors.
55 */
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
James Feistf81f2882019-02-26 11:26:36 -080067 const conf::PIDConf& pidConfig = zi.second;
Patrick Venture5c7cc542018-06-11 14:29:38 -070068
69 auto zone = std::make_unique<PIDZone>(
James Feist3484bed2019-02-25 13:28:18 -080070 zoneId, zoneConf->second.minThermalOutput,
Patrick Venture8e2fdb32019-02-11 09:39:59 -080071 zoneConf->second.failsafePercent, mgr, modeControlBus,
Patrick Venture7af157b2018-10-30 11:24:40 -070072 getControlPath(zi.first).c_str(), deferSignals);
Patrick Venture5c7cc542018-06-11 14:29:38 -070073
Patrick Venture0bbeaf82018-10-30 18:50:31 -070074 std::cerr << "Zone Id: " << zone->getZoneID() << "\n";
Patrick Venture5c7cc542018-06-11 14:29:38 -070075
76 // For each PID create a Controller and a Sensor.
Patrick Ventureadc9e862018-10-23 09:26:44 -070077 for (const auto& pit : pidConfig)
Patrick Venture5c7cc542018-06-11 14:29:38 -070078 {
79 std::vector<std::string> inputs;
80 std::string name = pit.first;
James Feistf81f2882019-02-26 11:26:36 -080081 const struct conf::ControllerInfo* info = &pit.second;
Patrick Venture5c7cc542018-06-11 14:29:38 -070082
83 std::cerr << "PID name: " << name << "\n";
84
85 /*
86 * TODO(venture): Need to check if input is known to the
87 * SensorManager.
88 */
89 if (info->type == "fan")
90 {
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070091 for (const auto& i : info->inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -070092 {
93 inputs.push_back(i);
94 zone->addFanInput(i);
95 }
96
Patrick Venture563a3562018-10-30 09:31:26 -070097 auto pid = FanController::createFanPid(zone.get(), name, inputs,
James Feist22c257a2018-08-31 14:07:12 -070098 info->pidInfo);
Patrick Venture5c7cc542018-06-11 14:29:38 -070099 zone->addFanPID(std::move(pid));
100 }
Patrick Venturee94bdc42018-11-15 18:41:21 -0800101 else if (isThermalType(info->type))
Patrick Venture5c7cc542018-06-11 14:29:38 -0700102 {
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700103 for (const auto& i : info->inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700104 {
105 inputs.push_back(i);
106 zone->addThermalInput(i);
107 }
108
Patrick Venture563a3562018-10-30 09:31:26 -0700109 auto pid = ThermalController::createThermalPid(
James Feist734f9532018-11-15 12:13:18 -0800110 zone.get(), name, inputs, info->setpoint, info->pidInfo,
Patrick Venturee94bdc42018-11-15 18:41:21 -0800111 getThermalType(info->type));
James Feist22c257a2018-08-31 14:07:12 -0700112
Patrick Venture5c7cc542018-06-11 14:29:38 -0700113 zone->addThermalPID(std::move(pid));
114 }
James Feist22c257a2018-08-31 14:07:12 -0700115 else if (info->type == "stepwise")
116 {
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700117 for (const auto& i : info->inputs)
James Feist22c257a2018-08-31 14:07:12 -0700118 {
119 inputs.push_back(i);
120 zone->addThermalInput(i);
121 }
Patrick Venture563a3562018-10-30 09:31:26 -0700122 auto stepwise = StepwiseController::createStepwiseController(
James Feist22c257a2018-08-31 14:07:12 -0700123 zone.get(), name, inputs, info->stepwiseInfo);
124 zone->addThermalPID(std::move(stepwise));
125 }
Patrick Venture5c7cc542018-06-11 14:29:38 -0700126
127 std::cerr << "inputs: ";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700128 for (const auto& i : inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700129 {
130 std::cerr << i << ", ";
131 }
132 std::cerr << "\n";
133 }
134
135 zone->emit_object_added();
136 zones[zoneId] = std::move(zone);
137 }
138
139 return zones;
140}
Patrick Venturea0764872020-08-08 07:48:43 -0700141
142} // namespace pid_control