blob: bae0e818035981924e60ed1497c4a263d54e8c82 [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"
Josh Lehan31058fd2023-01-13 11:06:16 -080026#include "util.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070027
Patrick Venturea83a3ec2020-08-04 09:52:05 -070028#include <sdbusplus/bus.hpp>
29
Patrick Ventureb748b682020-08-16 08:33:53 -070030#include <cstdint>
Patrick Venture5c7cc542018-06-11 14:29:38 -070031#include <iostream>
32#include <memory>
Patrick Ventureb748b682020-08-16 08:33:53 -070033#include <string>
Patrick Venture5c7cc542018-06-11 14:29:38 -070034#include <unordered_map>
Patrick Ventureb748b682020-08-16 08:33:53 -070035#include <vector>
Patrick Venture5c7cc542018-06-11 14:29:38 -070036
Patrick Venturea0764872020-08-08 07:48:43 -070037namespace pid_control
38{
39
Patrick Venture5c7cc542018-06-11 14:29:38 -070040static constexpr bool deferSignals = true;
41static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
42
Patrick Venture7af157b2018-10-30 11:24:40 -070043static std::string getControlPath(int64_t zone)
Patrick Venture5c7cc542018-06-11 14:29:38 -070044{
45 return std::string(objectPath) + std::to_string(zone);
46}
47
ykchiu7c6d35d2023-05-10 17:01:46 +080048static std::string getPidControlPath(int64_t zone, std::string pidname)
49{
50 return std::string(objectPath) + std::to_string(zone) + "/" + pidname;
51}
52
Johnathan Mantey5301aae2020-09-28 11:06:58 -070053std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>>
Patrick Venture7e3f8ab2020-08-03 11:10:43 -070054 buildZones(const std::map<int64_t, conf::PIDConf>& zonePids,
Patrick Venture1df9e872020-10-08 15:35:01 -070055 std::map<int64_t, conf::ZoneConfig>& zoneConfigs,
Patrick Williamsb228bc32022-07-22 19:26:56 -050056 SensorManager& mgr, sdbusplus::bus_t& modeControlBus)
Patrick Venture5c7cc542018-06-11 14:29:38 -070057{
Johnathan Mantey5301aae2020-09-28 11:06:58 -070058 std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
Patrick Venture5c7cc542018-06-11 14:29:38 -070059
Patrick Venture2a50eda2020-08-16 08:26:35 -070060 for (const auto& [zoneId, pidConfig] : zonePids)
Patrick Venture5c7cc542018-06-11 14:29:38 -070061 {
Patrick Venture5c7cc542018-06-11 14:29:38 -070062 /* 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 */
Patrick Venture5c7cc542018-06-11 14:29:38 -070066 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
Johnathan Mantey5301aae2020-09-28 11:06:58 -070076 auto zone = std::make_shared<DbusPidZone>(
James Feist3484bed2019-02-25 13:28:18 -080077 zoneId, zoneConf->second.minThermalOutput,
Bonnie Lo0e8fc392022-10-05 10:20:55 +080078 zoneConf->second.failsafePercent, zoneConf->second.cycleTime, mgr,
79 modeControlBus, getControlPath(zoneId).c_str(), deferSignals);
Patrick Venture5c7cc542018-06-11 14:29:38 -070080
Patrick Venture0bbeaf82018-10-30 18:50:31 -070081 std::cerr << "Zone Id: " << zone->getZoneID() << "\n";
Patrick Venture5c7cc542018-06-11 14:29:38 -070082
83 // For each PID create a Controller and a Sensor.
Patrick Venture2a50eda2020-08-16 08:26:35 -070084 for (const auto& [name, info] : pidConfig)
Patrick Venture5c7cc542018-06-11 14:29:38 -070085 {
Josh Lehan31058fd2023-01-13 11:06:16 -080086 std::vector<pid_control::conf::SensorInput> inputs;
Patrick Venture5c7cc542018-06-11 14:29:38 -070087 std::cerr << "PID name: " << name << "\n";
88
89 /*
90 * TODO(venture): Need to check if input is known to the
91 * SensorManager.
92 */
Patrick Venture2a50eda2020-08-16 08:26:35 -070093 if (info.type == "fan")
Patrick Venture5c7cc542018-06-11 14:29:38 -070094 {
Patrick Venture2a50eda2020-08-16 08:26:35 -070095 for (const auto& i : info.inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -070096 {
97 inputs.push_back(i);
Josh Lehan31058fd2023-01-13 11:06:16 -080098 zone->addFanInput(i.name);
Patrick Venture5c7cc542018-06-11 14:29:38 -070099 }
100
Josh Lehan31058fd2023-01-13 11:06:16 -0800101 auto pid = FanController::createFanPid(
102 zone.get(), name, splitNames(inputs), info.pidInfo);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700103 zone->addFanPID(std::move(pid));
ykchiu9fe3a3c2023-05-11 13:43:54 +0800104 zone->addPidFailSafePercent(name, info.failSafePercent);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700105 }
Patrick Venture2a50eda2020-08-16 08:26:35 -0700106 else if (isThermalType(info.type))
Patrick Venture5c7cc542018-06-11 14:29:38 -0700107 {
Patrick Venture2a50eda2020-08-16 08:26:35 -0700108 for (const auto& i : info.inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700109 {
110 inputs.push_back(i);
Josh Lehan31058fd2023-01-13 11:06:16 -0800111 zone->addThermalInput(i.name);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700112 }
113
Patrick Venture563a3562018-10-30 09:31:26 -0700114 auto pid = ThermalController::createThermalPid(
Patrick Venture2a50eda2020-08-16 08:26:35 -0700115 zone.get(), name, inputs, info.setpoint, info.pidInfo,
116 getThermalType(info.type));
James Feist22c257a2018-08-31 14:07:12 -0700117
Patrick Venture5c7cc542018-06-11 14:29:38 -0700118 zone->addThermalPID(std::move(pid));
Harvey Wu37180062023-10-02 09:42:50 +0800119 zone->addPidControlProcess(
120 name, info.type, info.setpoint, modeControlBus,
121 getPidControlPath(zoneId, name), deferSignals);
ykchiu9fe3a3c2023-05-11 13:43:54 +0800122 zone->addPidFailSafePercent(name, info.failSafePercent);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700123 }
Patrick Venture2a50eda2020-08-16 08:26:35 -0700124 else if (info.type == "stepwise")
James Feist22c257a2018-08-31 14:07:12 -0700125 {
Patrick Venture2a50eda2020-08-16 08:26:35 -0700126 for (const auto& i : info.inputs)
James Feist22c257a2018-08-31 14:07:12 -0700127 {
128 inputs.push_back(i);
Josh Lehan31058fd2023-01-13 11:06:16 -0800129 zone->addThermalInput(i.name);
James Feist22c257a2018-08-31 14:07:12 -0700130 }
Patrick Venture563a3562018-10-30 09:31:26 -0700131 auto stepwise = StepwiseController::createStepwiseController(
Josh Lehan31058fd2023-01-13 11:06:16 -0800132 zone.get(), name, splitNames(inputs), info.stepwiseInfo);
James Feist22c257a2018-08-31 14:07:12 -0700133 zone->addThermalPID(std::move(stepwise));
Harvey Wu37180062023-10-02 09:42:50 +0800134 zone->addPidControlProcess(
135 name, info.type, info.setpoint, modeControlBus,
136 getPidControlPath(zoneId, name), deferSignals);
ykchiu9fe3a3c2023-05-11 13:43:54 +0800137 zone->addPidFailSafePercent(name, info.failSafePercent);
James Feist22c257a2018-08-31 14:07:12 -0700138 }
Patrick Venture5c7cc542018-06-11 14:29:38 -0700139
140 std::cerr << "inputs: ";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700141 for (const auto& i : inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700142 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800143 std::cerr << i.name;
144 if (i.convertTempToMargin)
145 {
146 std::cerr << "[" << i.convertMarginZero << "]";
147 }
148 std::cerr << ", ";
Patrick Venture5c7cc542018-06-11 14:29:38 -0700149 }
150 std::cerr << "\n";
151 }
152
153 zone->emit_object_added();
154 zones[zoneId] = std::move(zone);
155 }
156
157 return zones;
158}
Patrick Venturea0764872020-08-08 07:48:43 -0700159
160} // namespace pid_control