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