blob: 58ef8df06fc3056f0f10cc1e756809343af4329d [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"
24
Patrick Venture5c7cc542018-06-11 14:29:38 -070025#include <iostream>
26#include <memory>
27#include <sdbusplus/bus.hpp>
28#include <unordered_map>
29
Patrick Venture5c7cc542018-06-11 14:29:38 -070030static constexpr bool deferSignals = true;
31static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone";
32
Patrick Venture7af157b2018-10-30 11:24:40 -070033static std::string getControlPath(int64_t zone)
Patrick Venture5c7cc542018-06-11 14:29:38 -070034{
35 return std::string(objectPath) + std::to_string(zone);
36}
37
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070038std::unordered_map<int64_t, std::unique_ptr<PIDZone>>
Patrick Venture7af157b2018-10-30 11:24:40 -070039 buildZones(std::map<int64_t, PIDConf>& zonePids,
Patrick Venturef3252312018-10-30 08:42:53 -070040 std::map<int64_t, struct ZoneConfig>& zoneConfigs,
41 SensorManager& mgr, sdbusplus::bus::bus& modeControlBus)
Patrick Venture5c7cc542018-06-11 14:29:38 -070042{
43 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones;
44
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070045 for (const auto& zi : zonePids)
Patrick Venture5c7cc542018-06-11 14:29:38 -070046 {
47 auto zoneId = static_cast<int64_t>(zi.first);
48 /* The above shouldn't be necessary but is, and I am having trouble
49 * locating my notes on why. If I recall correctly it was casting it
50 * down to a byte in at least some cases causing weird behaviors.
51 */
52
53 auto zoneConf = zoneConfigs.find(zoneId);
54 if (zoneConf == zoneConfigs.end())
55 {
56 /* The Zone doesn't have a configuration, bail. */
57 static constexpr auto err =
58 "Bailing during load, missing Zone Configuration";
59 std::cerr << err << std::endl;
60 throw std::runtime_error(err);
61 }
62
Patrick Ventureadc9e862018-10-23 09:26:44 -070063 const PIDConf& pidConfig = zi.second;
Patrick Venture5c7cc542018-06-11 14:29:38 -070064
65 auto zone = std::make_unique<PIDZone>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070066 zoneId, zoneConf->second.minthermalrpm,
67 zoneConf->second.failsafepercent, mgr, modeControlBus,
Patrick Venture7af157b2018-10-30 11:24:40 -070068 getControlPath(zi.first).c_str(), deferSignals);
Patrick Venture5c7cc542018-06-11 14:29:38 -070069
70 std::cerr << "Zone Id: " << zone->getZoneId() << "\n";
71
72 // For each PID create a Controller and a Sensor.
Patrick Ventureadc9e862018-10-23 09:26:44 -070073 for (const auto& pit : pidConfig)
Patrick Venture5c7cc542018-06-11 14:29:38 -070074 {
75 std::vector<std::string> inputs;
76 std::string name = pit.first;
Patrick Venturef3252312018-10-30 08:42:53 -070077 const struct ControllerInfo* info = &pit.second;
Patrick Venture5c7cc542018-06-11 14:29:38 -070078
79 std::cerr << "PID name: " << name << "\n";
80
81 /*
82 * TODO(venture): Need to check if input is known to the
83 * SensorManager.
84 */
85 if (info->type == "fan")
86 {
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070087 for (const auto& i : info->inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -070088 {
89 inputs.push_back(i);
90 zone->addFanInput(i);
91 }
92
Patrick Venture563a3562018-10-30 09:31:26 -070093 auto pid = FanController::createFanPid(zone.get(), name, inputs,
James Feist22c257a2018-08-31 14:07:12 -070094 info->pidInfo);
Patrick Venture5c7cc542018-06-11 14:29:38 -070095 zone->addFanPID(std::move(pid));
96 }
97 else if (info->type == "temp" || info->type == "margin")
98 {
Patrick Venture4a2dc4d2018-10-23 09:02:55 -070099 for (const auto& i : info->inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700100 {
101 inputs.push_back(i);
102 zone->addThermalInput(i);
103 }
104
Patrick Venture563a3562018-10-30 09:31:26 -0700105 auto pid = ThermalController::createThermalPid(
James Feist22c257a2018-08-31 14:07:12 -0700106 zone.get(), name, inputs, info->setpoint, info->pidInfo);
107
Patrick Venture5c7cc542018-06-11 14:29:38 -0700108 zone->addThermalPID(std::move(pid));
109 }
James Feist22c257a2018-08-31 14:07:12 -0700110 else if (info->type == "stepwise")
111 {
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700112 for (const auto& i : info->inputs)
James Feist22c257a2018-08-31 14:07:12 -0700113 {
114 inputs.push_back(i);
115 zone->addThermalInput(i);
116 }
Patrick Venture563a3562018-10-30 09:31:26 -0700117 auto stepwise = StepwiseController::createStepwiseController(
James Feist22c257a2018-08-31 14:07:12 -0700118 zone.get(), name, inputs, info->stepwiseInfo);
119 zone->addThermalPID(std::move(stepwise));
120 }
Patrick Venture5c7cc542018-06-11 14:29:38 -0700121
122 std::cerr << "inputs: ";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700123 for (const auto& i : inputs)
Patrick Venture5c7cc542018-06-11 14:29:38 -0700124 {
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}