Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 1 | /** |
| 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 Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 19 | #include "conf.hpp" |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 20 | #include "pid/controller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 21 | #include "pid/fancontroller.hpp" |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 22 | #include "pid/stepwisecontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 23 | #include "pid/thermalcontroller.hpp" |
Patrick Venture | 7a98c19 | 2020-08-12 08:35:16 -0700 | [diff] [blame] | 24 | #include "pid/zone.hpp" |
| 25 | #include "pid/zone_interface.hpp" |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 26 | #include "util.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 27 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 28 | #include <sdbusplus/bus.hpp> |
| 29 | |
Patrick Venture | b748b68 | 2020-08-16 08:33:53 -0700 | [diff] [blame] | 30 | #include <cstdint> |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <memory> |
Patrick Venture | b748b68 | 2020-08-16 08:33:53 -0700 | [diff] [blame] | 33 | #include <string> |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 34 | #include <unordered_map> |
Patrick Venture | b748b68 | 2020-08-16 08:33:53 -0700 | [diff] [blame] | 35 | #include <vector> |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 36 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 37 | namespace pid_control |
| 38 | { |
| 39 | |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 40 | static constexpr bool deferSignals = true; |
| 41 | static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; |
| 42 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 43 | static std::string getControlPath(int64_t zone) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 44 | { |
| 45 | return std::string(objectPath) + std::to_string(zone); |
| 46 | } |
| 47 | |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 48 | static std::string getPidControlPath(int64_t zone, std::string pidname) |
| 49 | { |
| 50 | return std::string(objectPath) + std::to_string(zone) + "/" + pidname; |
| 51 | } |
| 52 | |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 53 | std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> |
Patrick Venture | 7e3f8ab | 2020-08-03 11:10:43 -0700 | [diff] [blame] | 54 | buildZones(const std::map<int64_t, conf::PIDConf>& zonePids, |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 55 | std::map<int64_t, conf::ZoneConfig>& zoneConfigs, |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 56 | SensorManager& mgr, sdbusplus::bus_t& modeControlBus) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 57 | { |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 58 | std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 59 | |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 60 | for (const auto& [zoneId, pidConfig] : zonePids) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 61 | { |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 62 | /* 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 Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 66 | 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 Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 76 | auto zone = std::make_shared<DbusPidZone>( |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 77 | zoneId, zoneConf->second.minThermalOutput, |
Bonnie Lo | 0e8fc39 | 2022-10-05 10:20:55 +0800 | [diff] [blame] | 78 | zoneConf->second.failsafePercent, zoneConf->second.cycleTime, mgr, |
Delphine CC Chiu | 9788963 | 2023-11-06 11:32:46 +0800 | [diff] [blame] | 79 | modeControlBus, getControlPath(zoneId).c_str(), deferSignals, |
| 80 | zoneConf->second.accumulateSetPoint); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 81 | |
Patrick Venture | 0bbeaf8 | 2018-10-30 18:50:31 -0700 | [diff] [blame] | 82 | std::cerr << "Zone Id: " << zone->getZoneID() << "\n"; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 83 | |
| 84 | // For each PID create a Controller and a Sensor. |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 85 | for (const auto& [name, info] : pidConfig) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 86 | { |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 87 | std::vector<pid_control::conf::SensorInput> inputs; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 88 | std::cerr << "PID name: " << name << "\n"; |
| 89 | |
| 90 | /* |
| 91 | * TODO(venture): Need to check if input is known to the |
| 92 | * SensorManager. |
| 93 | */ |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 94 | if (info.type == "fan") |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 95 | { |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 96 | for (const auto& i : info.inputs) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 97 | { |
| 98 | inputs.push_back(i); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 99 | zone->addFanInput(i.name, i.missingIsAcceptable); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 102 | auto pid = FanController::createFanPid( |
| 103 | zone.get(), name, splitNames(inputs), info.pidInfo); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 104 | zone->addFanPID(std::move(pid)); |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 105 | zone->addPidFailSafePercent(splitNames(inputs), |
| 106 | info.failSafePercent); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 107 | } |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 108 | else if (isThermalType(info.type)) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 109 | { |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 110 | for (const auto& i : info.inputs) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 111 | { |
| 112 | inputs.push_back(i); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 113 | zone->addThermalInput(i.name, i.missingIsAcceptable); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 116 | auto pid = ThermalController::createThermalPid( |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 117 | zone.get(), name, inputs, info.setpoint, info.pidInfo, |
| 118 | getThermalType(info.type)); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 119 | |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 120 | zone->addThermalPID(std::move(pid)); |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 121 | zone->addPidControlProcess( |
| 122 | name, info.type, info.setpoint, modeControlBus, |
| 123 | getPidControlPath(zoneId, name), deferSignals); |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 124 | zone->addPidFailSafePercent(splitNames(inputs), |
| 125 | info.failSafePercent); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 126 | } |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 127 | else if (info.type == "stepwise") |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 128 | { |
Patrick Venture | 2a50eda | 2020-08-16 08:26:35 -0700 | [diff] [blame] | 129 | for (const auto& i : info.inputs) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 130 | { |
| 131 | inputs.push_back(i); |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 132 | zone->addThermalInput(i.name, i.missingIsAcceptable); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 133 | } |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 134 | auto stepwise = StepwiseController::createStepwiseController( |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 135 | zone.get(), name, splitNames(inputs), info.stepwiseInfo); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 136 | zone->addThermalPID(std::move(stepwise)); |
Harvey Wu | 3718006 | 2023-10-02 09:42:50 +0800 | [diff] [blame] | 137 | zone->addPidControlProcess( |
| 138 | name, info.type, info.setpoint, modeControlBus, |
| 139 | getPidControlPath(zoneId, name), deferSignals); |
Harvey Wu | 92f9f3c | 2023-11-07 09:23:35 +0800 | [diff] [blame] | 140 | zone->addPidFailSafePercent(splitNames(inputs), |
| 141 | info.failSafePercent); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 142 | } |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 143 | |
| 144 | std::cerr << "inputs: "; |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 145 | for (const auto& i : inputs) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 146 | { |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 147 | std::cerr << i.name; |
| 148 | if (i.convertTempToMargin) |
| 149 | { |
| 150 | std::cerr << "[" << i.convertMarginZero << "]"; |
| 151 | } |
Josh Lehan | 3f0f7bc | 2023-02-13 01:45:29 -0800 | [diff] [blame] | 152 | if (i.missingIsAcceptable) |
| 153 | { |
| 154 | std::cerr << "?"; |
| 155 | } |
Josh Lehan | 31058fd | 2023-01-13 11:06:16 -0800 | [diff] [blame] | 156 | std::cerr << ", "; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 157 | } |
| 158 | std::cerr << "\n"; |
| 159 | } |
| 160 | |
| 161 | zone->emit_object_added(); |
| 162 | zones[zoneId] = std::move(zone); |
| 163 | } |
| 164 | |
| 165 | return zones; |
| 166 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 167 | |
| 168 | } // namespace pid_control |