blob: d13366204a6607341f58be7835b1ec9269fff695 [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,
Delphine CC Chiu97889632023-11-06 11:32:46 +080079 modeControlBus, getControlPath(zoneId).c_str(), deferSignals,
80 zoneConf->second.accumulateSetPoint);
Patrick Venture5c7cc542018-06-11 14:29:38 -070081
Patrick Venture0bbeaf82018-10-30 18:50:31 -070082 std::cerr << "Zone Id: " << zone->getZoneID() << "\n";
Patrick Venture5c7cc542018-06-11 14:29:38 -070083
84 // For each PID create a Controller and a Sensor.
Patrick Venture2a50eda2020-08-16 08:26:35 -070085 for (const auto& [name, info] : pidConfig)
Patrick Venture5c7cc542018-06-11 14:29:38 -070086 {
Josh Lehan31058fd2023-01-13 11:06:16 -080087 std::vector<pid_control::conf::SensorInput> inputs;
Patrick Venture5c7cc542018-06-11 14:29:38 -070088 std::cerr << "PID name: " << name << "\n";
89
90 /*
91 * TODO(venture): Need to check if input is known to the
92 * SensorManager.
93 */
Patrick Venture2a50eda2020-08-16 08:26:35 -070094 if (info.type == "fan")
Patrick Venture5c7cc542018-06-11 14:29:38 -070095 {
Patrick Venture2a50eda2020-08-16 08:26:35 -070096 for (const auto& i : info.inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -070097 {
98 inputs.push_back(i);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -080099 zone->addFanInput(i.name, i.missingIsAcceptable);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700100 }
101
Josh Lehan31058fd2023-01-13 11:06:16 -0800102 auto pid = FanController::createFanPid(
103 zone.get(), name, splitNames(inputs), info.pidInfo);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700104 zone->addFanPID(std::move(pid));
ykchiu9fe3a3c2023-05-11 13:43:54 +0800105 zone->addPidFailSafePercent(name, info.failSafePercent);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700106 }
Patrick Venture2a50eda2020-08-16 08:26:35 -0700107 else if (isThermalType(info.type))
Patrick Venture5c7cc542018-06-11 14:29:38 -0700108 {
Patrick Venture2a50eda2020-08-16 08:26:35 -0700109 for (const auto& i : info.inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700110 {
111 inputs.push_back(i);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800112 zone->addThermalInput(i.name, i.missingIsAcceptable);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700113 }
114
Patrick Venture563a3562018-10-30 09:31:26 -0700115 auto pid = ThermalController::createThermalPid(
Patrick Venture2a50eda2020-08-16 08:26:35 -0700116 zone.get(), name, inputs, info.setpoint, info.pidInfo,
117 getThermalType(info.type));
James Feist22c257a2018-08-31 14:07:12 -0700118
Patrick Venture5c7cc542018-06-11 14:29:38 -0700119 zone->addThermalPID(std::move(pid));
Harvey Wu37180062023-10-02 09:42:50 +0800120 zone->addPidControlProcess(
121 name, info.type, info.setpoint, modeControlBus,
122 getPidControlPath(zoneId, name), deferSignals);
ykchiu9fe3a3c2023-05-11 13:43:54 +0800123 zone->addPidFailSafePercent(name, info.failSafePercent);
Patrick Venture5c7cc542018-06-11 14:29:38 -0700124 }
Patrick Venture2a50eda2020-08-16 08:26:35 -0700125 else if (info.type == "stepwise")
James Feist22c257a2018-08-31 14:07:12 -0700126 {
Patrick Venture2a50eda2020-08-16 08:26:35 -0700127 for (const auto& i : info.inputs)
James Feist22c257a2018-08-31 14:07:12 -0700128 {
129 inputs.push_back(i);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800130 zone->addThermalInput(i.name, i.missingIsAcceptable);
James Feist22c257a2018-08-31 14:07:12 -0700131 }
Patrick Venture563a3562018-10-30 09:31:26 -0700132 auto stepwise = StepwiseController::createStepwiseController(
Josh Lehan31058fd2023-01-13 11:06:16 -0800133 zone.get(), name, splitNames(inputs), info.stepwiseInfo);
James Feist22c257a2018-08-31 14:07:12 -0700134 zone->addThermalPID(std::move(stepwise));
Harvey Wu37180062023-10-02 09:42:50 +0800135 zone->addPidControlProcess(
136 name, info.type, info.setpoint, modeControlBus,
137 getPidControlPath(zoneId, name), deferSignals);
ykchiu9fe3a3c2023-05-11 13:43:54 +0800138 zone->addPidFailSafePercent(name, info.failSafePercent);
James Feist22c257a2018-08-31 14:07:12 -0700139 }
Patrick Venture5c7cc542018-06-11 14:29:38 -0700140
141 std::cerr << "inputs: ";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700142 for (const auto& i : inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700143 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800144 std::cerr << i.name;
145 if (i.convertTempToMargin)
146 {
147 std::cerr << "[" << i.convertMarginZero << "]";
148 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800149 if (i.missingIsAcceptable)
150 {
151 std::cerr << "?";
152 }
Josh Lehan31058fd2023-01-13 11:06:16 -0800153 std::cerr << ", ";
Patrick Venture5c7cc542018-06-11 14:29:38 -0700154 }
155 std::cerr << "\n";
156 }
157
158 zone->emit_object_added();
159 zones[zoneId] = std::move(zone);
160 }
161
162 return zones;
163}
Patrick Venturea0764872020-08-08 07:48:43 -0700164
165} // namespace pid_control