blob: 830c2fb5e941155552303a405d2180ad87da83d2 [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 Ventureb748b682020-08-16 08:33:53 -070029#include <cstdint>
Patrick Venture5c7cc542018-06-11 14:29:38 -070030#include <iostream>
31#include <memory>
Patrick Ventureb748b682020-08-16 08:33:53 -070032#include <string>
Patrick Venture5c7cc542018-06-11 14:29:38 -070033#include <unordered_map>
Patrick Ventureb748b682020-08-16 08:33:53 -070034#include <vector>
Patrick Venture5c7cc542018-06-11 14:29:38 -070035
Patrick Venturea0764872020-08-08 07:48:43 -070036namespace pid_control
37{
38
Patrick Venture5c7cc542018-06-11 14:29:38 -070039static constexpr bool deferSignals = true;
40static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
41
Patrick Venture7af157b2018-10-30 11:24:40 -070042static std::string getControlPath(int64_t zone)
Patrick Venture5c7cc542018-06-11 14:29:38 -070043{
44 return std::string(objectPath) + std::to_string(zone);
45}
46
ykchiu7c6d35d2023-05-10 17:01:46 +080047static std::string getPidControlPath(int64_t zone, std::string pidname)
48{
49 return std::string(objectPath) + std::to_string(zone) + "/" + pidname;
50}
51
Johnathan Mantey5301aae2020-09-28 11:06:58 -070052std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>>
Patrick Venture7e3f8ab2020-08-03 11:10:43 -070053 buildZones(const std::map<int64_t, conf::PIDConf>& zonePids,
Patrick Venture1df9e872020-10-08 15:35:01 -070054 std::map<int64_t, conf::ZoneConfig>& zoneConfigs,
Patrick Williamsb228bc32022-07-22 19:26:56 -050055 SensorManager& mgr, sdbusplus::bus_t& modeControlBus)
Patrick Venture5c7cc542018-06-11 14:29:38 -070056{
Johnathan Mantey5301aae2020-09-28 11:06:58 -070057 std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
Patrick Venture5c7cc542018-06-11 14:29:38 -070058
Patrick Venture2a50eda2020-08-16 08:26:35 -070059 for (const auto& [zoneId, pidConfig] : zonePids)
Patrick Venture5c7cc542018-06-11 14:29:38 -070060 {
Patrick Venture5c7cc542018-06-11 14:29:38 -070061 /* The above shouldn't be necessary but is, and I am having trouble
62 * locating my notes on why. If I recall correctly it was casting it
63 * down to a byte in at least some cases causing weird behaviors.
64 */
Patrick Venture5c7cc542018-06-11 14:29:38 -070065 auto zoneConf = zoneConfigs.find(zoneId);
66 if (zoneConf == zoneConfigs.end())
67 {
68 /* The Zone doesn't have a configuration, bail. */
69 static constexpr auto err =
70 "Bailing during load, missing Zone Configuration";
71 std::cerr << err << std::endl;
72 throw std::runtime_error(err);
73 }
74
Johnathan Mantey5301aae2020-09-28 11:06:58 -070075 auto zone = std::make_shared<DbusPidZone>(
James Feist3484bed2019-02-25 13:28:18 -080076 zoneId, zoneConf->second.minThermalOutput,
Bonnie Lo0e8fc392022-10-05 10:20:55 +080077 zoneConf->second.failsafePercent, zoneConf->second.cycleTime, mgr,
78 modeControlBus, getControlPath(zoneId).c_str(), deferSignals);
Patrick Venture5c7cc542018-06-11 14:29:38 -070079
Patrick Venture0bbeaf82018-10-30 18:50:31 -070080 std::cerr << "Zone Id: " << zone->getZoneID() << "\n";
Patrick Venture5c7cc542018-06-11 14:29:38 -070081
82 // For each PID create a Controller and a Sensor.
Patrick Venture2a50eda2020-08-16 08:26:35 -070083 for (const auto& [name, info] : pidConfig)
Patrick Venture5c7cc542018-06-11 14:29:38 -070084 {
85 std::vector<std::string> inputs;
Patrick Venture5c7cc542018-06-11 14:29:38 -070086 std::cerr << "PID name: " << name << "\n";
87
88 /*
89 * TODO(venture): Need to check if input is known to the
90 * SensorManager.
91 */
Patrick Venture2a50eda2020-08-16 08:26:35 -070092 if (info.type == "fan")
Patrick Venture5c7cc542018-06-11 14:29:38 -070093 {
Patrick Venture2a50eda2020-08-16 08:26:35 -070094 for (const auto& i : info.inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -070095 {
96 inputs.push_back(i);
97 zone->addFanInput(i);
98 }
99
Patrick Venture563a3562018-10-30 09:31:26 -0700100 auto pid = FanController::createFanPid(zone.get(), name, inputs,
Patrick Venture2a50eda2020-08-16 08:26:35 -0700101 info.pidInfo);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700102 zone->addFanPID(std::move(pid));
ykchiu9fe3a3c2023-05-11 13:43:54 +0800103 zone->addPidFailSafePercent(name, info.failSafePercent);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700104 }
Patrick Venture2a50eda2020-08-16 08:26:35 -0700105 else if (isThermalType(info.type))
Patrick Venture5c7cc542018-06-11 14:29:38 -0700106 {
Patrick Venture2a50eda2020-08-16 08:26:35 -0700107 for (const auto& i : info.inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700108 {
109 inputs.push_back(i);
110 zone->addThermalInput(i);
111 }
112
Patrick Venture563a3562018-10-30 09:31:26 -0700113 auto pid = ThermalController::createThermalPid(
Patrick Venture2a50eda2020-08-16 08:26:35 -0700114 zone.get(), name, inputs, info.setpoint, info.pidInfo,
115 getThermalType(info.type));
James Feist22c257a2018-08-31 14:07:12 -0700116
Patrick Venture5c7cc542018-06-11 14:29:38 -0700117 zone->addThermalPID(std::move(pid));
ykchiu7c6d35d2023-05-10 17:01:46 +0800118 zone->addPidControlProcess(name, modeControlBus,
119 getPidControlPath(zoneId, name),
120 deferSignals);
ykchiu9fe3a3c2023-05-11 13:43:54 +0800121 zone->addPidFailSafePercent(name, info.failSafePercent);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700122 }
Patrick Venture2a50eda2020-08-16 08:26:35 -0700123 else if (info.type == "stepwise")
James Feist22c257a2018-08-31 14:07:12 -0700124 {
Patrick Venture2a50eda2020-08-16 08:26:35 -0700125 for (const auto& i : info.inputs)
James Feist22c257a2018-08-31 14:07:12 -0700126 {
127 inputs.push_back(i);
128 zone->addThermalInput(i);
129 }
Patrick Venture563a3562018-10-30 09:31:26 -0700130 auto stepwise = StepwiseController::createStepwiseController(
Patrick Venture2a50eda2020-08-16 08:26:35 -0700131 zone.get(), name, inputs, info.stepwiseInfo);
James Feist22c257a2018-08-31 14:07:12 -0700132 zone->addThermalPID(std::move(stepwise));
ykchiu7c6d35d2023-05-10 17:01:46 +0800133 zone->addPidControlProcess(name, modeControlBus,
134 getPidControlPath(zoneId, name),
135 deferSignals);
ykchiu9fe3a3c2023-05-11 13:43:54 +0800136 zone->addPidFailSafePercent(name, info.failSafePercent);
James Feist22c257a2018-08-31 14:07:12 -0700137 }
Patrick Venture5c7cc542018-06-11 14:29:38 -0700138
139 std::cerr << "inputs: ";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700140 for (const auto& i : inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700141 {
142 std::cerr << i << ", ";
143 }
144 std::cerr << "\n";
145 }
146
147 zone->emit_object_added();
148 zones[zoneId] = std::move(zone);
149 }
150
151 return zones;
152}
Patrick Venturea0764872020-08-08 07:48:43 -0700153
154} // namespace pid_control