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" |
| 24 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 25 | #include <sdbusplus/bus.hpp> |
| 26 | |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <memory> |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
| 30 | |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 31 | static constexpr bool deferSignals = true; |
| 32 | static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; |
| 33 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 34 | static std::string getControlPath(int64_t zone) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 35 | { |
| 36 | return std::string(objectPath) + std::to_string(zone); |
| 37 | } |
| 38 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 39 | std::unordered_map<int64_t, std::unique_ptr<PIDZone>> |
Patrick Venture | 7e3f8ab | 2020-08-03 11:10:43 -0700 | [diff] [blame] | 40 | buildZones(const std::map<int64_t, conf::PIDConf>& zonePids, |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 41 | std::map<int64_t, struct conf::ZoneConfig>& zoneConfigs, |
Patrick Venture | f325231 | 2018-10-30 08:42:53 -0700 | [diff] [blame] | 42 | SensorManager& mgr, sdbusplus::bus::bus& modeControlBus) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 43 | { |
| 44 | std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones; |
| 45 | |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 46 | for (const auto& zi : zonePids) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 47 | { |
| 48 | auto zoneId = static_cast<int64_t>(zi.first); |
| 49 | /* The above shouldn't be necessary but is, and I am having trouble |
| 50 | * locating my notes on why. If I recall correctly it was casting it |
| 51 | * down to a byte in at least some cases causing weird behaviors. |
| 52 | */ |
| 53 | |
| 54 | auto zoneConf = zoneConfigs.find(zoneId); |
| 55 | if (zoneConf == zoneConfigs.end()) |
| 56 | { |
| 57 | /* The Zone doesn't have a configuration, bail. */ |
| 58 | static constexpr auto err = |
| 59 | "Bailing during load, missing Zone Configuration"; |
| 60 | std::cerr << err << std::endl; |
| 61 | throw std::runtime_error(err); |
| 62 | } |
| 63 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 64 | const conf::PIDConf& pidConfig = zi.second; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 65 | |
| 66 | auto zone = std::make_unique<PIDZone>( |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 67 | zoneId, zoneConf->second.minThermalOutput, |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame] | 68 | zoneConf->second.failsafePercent, mgr, modeControlBus, |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 69 | getControlPath(zi.first).c_str(), deferSignals); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 70 | |
Patrick Venture | 0bbeaf8 | 2018-10-30 18:50:31 -0700 | [diff] [blame] | 71 | std::cerr << "Zone Id: " << zone->getZoneID() << "\n"; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 72 | |
| 73 | // For each PID create a Controller and a Sensor. |
Patrick Venture | adc9e86 | 2018-10-23 09:26:44 -0700 | [diff] [blame] | 74 | for (const auto& pit : pidConfig) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 75 | { |
| 76 | std::vector<std::string> inputs; |
| 77 | std::string name = pit.first; |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 78 | const struct conf::ControllerInfo* info = &pit.second; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 79 | |
| 80 | std::cerr << "PID name: " << name << "\n"; |
| 81 | |
| 82 | /* |
| 83 | * TODO(venture): Need to check if input is known to the |
| 84 | * SensorManager. |
| 85 | */ |
| 86 | if (info->type == "fan") |
| 87 | { |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 88 | for (const auto& i : info->inputs) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 89 | { |
| 90 | inputs.push_back(i); |
| 91 | zone->addFanInput(i); |
| 92 | } |
| 93 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 94 | auto pid = FanController::createFanPid(zone.get(), name, inputs, |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 95 | info->pidInfo); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 96 | zone->addFanPID(std::move(pid)); |
| 97 | } |
Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 98 | else if (isThermalType(info->type)) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 99 | { |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 100 | for (const auto& i : info->inputs) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 101 | { |
| 102 | inputs.push_back(i); |
| 103 | zone->addThermalInput(i); |
| 104 | } |
| 105 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 106 | auto pid = ThermalController::createThermalPid( |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 107 | zone.get(), name, inputs, info->setpoint, info->pidInfo, |
Patrick Venture | e94bdc4 | 2018-11-15 18:41:21 -0800 | [diff] [blame] | 108 | getThermalType(info->type)); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 109 | |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 110 | zone->addThermalPID(std::move(pid)); |
| 111 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 112 | else if (info->type == "stepwise") |
| 113 | { |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 114 | for (const auto& i : info->inputs) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 115 | { |
| 116 | inputs.push_back(i); |
| 117 | zone->addThermalInput(i); |
| 118 | } |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 119 | auto stepwise = StepwiseController::createStepwiseController( |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 120 | zone.get(), name, inputs, info->stepwiseInfo); |
| 121 | zone->addThermalPID(std::move(stepwise)); |
| 122 | } |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 123 | |
| 124 | std::cerr << "inputs: "; |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 125 | for (const auto& i : inputs) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 126 | { |
| 127 | std::cerr << i << ", "; |
| 128 | } |
| 129 | std::cerr << "\n"; |
| 130 | } |
| 131 | |
| 132 | zone->emit_object_added(); |
| 133 | zones[zoneId] = std::move(zone); |
| 134 | } |
| 135 | |
| 136 | return zones; |
| 137 | } |