15c7cc545SPatrick Venture /** 25c7cc545SPatrick Venture * Copyright 2017 Google Inc. 35c7cc545SPatrick Venture * 45c7cc545SPatrick Venture * Licensed under the Apache License, Version 2.0 (the "License"); 55c7cc545SPatrick Venture * you may not use this file except in compliance with the License. 65c7cc545SPatrick Venture * You may obtain a copy of the License at 75c7cc545SPatrick Venture * 85c7cc545SPatrick Venture * http://www.apache.org/licenses/LICENSE-2.0 95c7cc545SPatrick Venture * 105c7cc545SPatrick Venture * Unless required by applicable law or agreed to in writing, software 115c7cc545SPatrick Venture * distributed under the License is distributed on an "AS IS" BASIS, 125c7cc545SPatrick Venture * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135c7cc545SPatrick Venture * See the License for the specific language governing permissions and 145c7cc545SPatrick Venture * limitations under the License. 155c7cc545SPatrick Venture */ 165c7cc545SPatrick Venture 175c7cc545SPatrick Venture #include "pid/builder.hpp" 185c7cc545SPatrick Venture 19*da4a5dd1SPatrick Venture #include "conf.hpp" 20*da4a5dd1SPatrick Venture #include "pid/fancontroller.hpp" 21*da4a5dd1SPatrick Venture #include "pid/thermalcontroller.hpp" 22*da4a5dd1SPatrick Venture 235c7cc545SPatrick Venture #include <iostream> 245c7cc545SPatrick Venture #include <memory> 255c7cc545SPatrick Venture #include <sdbusplus/bus.hpp> 265c7cc545SPatrick Venture #include <unordered_map> 275c7cc545SPatrick Venture 285c7cc545SPatrick Venture static constexpr bool deferSignals = true; 295c7cc545SPatrick Venture static constexpr auto objectPath = "/xyz/openbmc_project/settings/fanctrl/zone"; 305c7cc545SPatrick Venture 315c7cc545SPatrick Venture static std::string GetControlPath(int64_t zone) 325c7cc545SPatrick Venture { 335c7cc545SPatrick Venture return std::string(objectPath) + std::to_string(zone); 345c7cc545SPatrick Venture } 355c7cc545SPatrick Venture 36*da4a5dd1SPatrick Venture std::unordered_map<int64_t, std::unique_ptr<PIDZone>> 37*da4a5dd1SPatrick Venture BuildZones(std::map<int64_t, PIDConf>& zonePids, 38*da4a5dd1SPatrick Venture std::map<int64_t, struct zone>& zoneConfigs, SensorManager& mgr, 395c7cc545SPatrick Venture sdbusplus::bus::bus& modeControlBus) 405c7cc545SPatrick Venture { 415c7cc545SPatrick Venture std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones; 425c7cc545SPatrick Venture 435c7cc545SPatrick Venture for (auto& zi : zonePids) 445c7cc545SPatrick Venture { 455c7cc545SPatrick Venture auto zoneId = static_cast<int64_t>(zi.first); 465c7cc545SPatrick Venture /* The above shouldn't be necessary but is, and I am having trouble 475c7cc545SPatrick Venture * locating my notes on why. If I recall correctly it was casting it 485c7cc545SPatrick Venture * down to a byte in at least some cases causing weird behaviors. 495c7cc545SPatrick Venture */ 505c7cc545SPatrick Venture 515c7cc545SPatrick Venture auto zoneConf = zoneConfigs.find(zoneId); 525c7cc545SPatrick Venture if (zoneConf == zoneConfigs.end()) 535c7cc545SPatrick Venture { 545c7cc545SPatrick Venture /* The Zone doesn't have a configuration, bail. */ 555c7cc545SPatrick Venture static constexpr auto err = 565c7cc545SPatrick Venture "Bailing during load, missing Zone Configuration"; 575c7cc545SPatrick Venture std::cerr << err << std::endl; 585c7cc545SPatrick Venture throw std::runtime_error(err); 595c7cc545SPatrick Venture } 605c7cc545SPatrick Venture 615c7cc545SPatrick Venture PIDConf& PIDConfig = zi.second; 625c7cc545SPatrick Venture 635c7cc545SPatrick Venture auto zone = std::make_unique<PIDZone>( 64*da4a5dd1SPatrick Venture zoneId, zoneConf->second.minthermalrpm, 65*da4a5dd1SPatrick Venture zoneConf->second.failsafepercent, mgr, modeControlBus, 66*da4a5dd1SPatrick Venture GetControlPath(zi.first).c_str(), deferSignals); 675c7cc545SPatrick Venture 685c7cc545SPatrick Venture std::cerr << "Zone Id: " << zone->getZoneId() << "\n"; 695c7cc545SPatrick Venture 705c7cc545SPatrick Venture // For each PID create a Controller and a Sensor. 715c7cc545SPatrick Venture for (auto& pit : PIDConfig) 725c7cc545SPatrick Venture { 735c7cc545SPatrick Venture std::vector<std::string> inputs; 745c7cc545SPatrick Venture std::string name = pit.first; 755c7cc545SPatrick Venture struct controller_info* info = &pit.second; 765c7cc545SPatrick Venture 775c7cc545SPatrick Venture std::cerr << "PID name: " << name << "\n"; 785c7cc545SPatrick Venture 795c7cc545SPatrick Venture /* 805c7cc545SPatrick Venture * TODO(venture): Need to check if input is known to the 815c7cc545SPatrick Venture * SensorManager. 825c7cc545SPatrick Venture */ 835c7cc545SPatrick Venture if (info->type == "fan") 845c7cc545SPatrick Venture { 855c7cc545SPatrick Venture for (auto i : info->inputs) 865c7cc545SPatrick Venture { 875c7cc545SPatrick Venture inputs.push_back(i); 885c7cc545SPatrick Venture zone->addFanInput(i); 895c7cc545SPatrick Venture } 905c7cc545SPatrick Venture 91*da4a5dd1SPatrick Venture auto pid = FanController::CreateFanPid(zone.get(), name, inputs, 925c7cc545SPatrick Venture info->info); 935c7cc545SPatrick Venture zone->addFanPID(std::move(pid)); 945c7cc545SPatrick Venture } 955c7cc545SPatrick Venture else if (info->type == "temp" || info->type == "margin") 965c7cc545SPatrick Venture { 975c7cc545SPatrick Venture for (auto i : info->inputs) 985c7cc545SPatrick Venture { 995c7cc545SPatrick Venture inputs.push_back(i); 1005c7cc545SPatrick Venture zone->addThermalInput(i); 1015c7cc545SPatrick Venture } 1025c7cc545SPatrick Venture 1035c7cc545SPatrick Venture auto pid = ThermalController::CreateThermalPid( 104*da4a5dd1SPatrick Venture zone.get(), name, inputs, info->setpoint, info->info); 1055c7cc545SPatrick Venture zone->addThermalPID(std::move(pid)); 1065c7cc545SPatrick Venture } 1075c7cc545SPatrick Venture 1085c7cc545SPatrick Venture std::cerr << "inputs: "; 1095c7cc545SPatrick Venture for (auto& i : inputs) 1105c7cc545SPatrick Venture { 1115c7cc545SPatrick Venture std::cerr << i << ", "; 1125c7cc545SPatrick Venture } 1135c7cc545SPatrick Venture std::cerr << "\n"; 1145c7cc545SPatrick Venture } 1155c7cc545SPatrick Venture 1165c7cc545SPatrick Venture zone->emit_object_added(); 1175c7cc545SPatrick Venture zones[zoneId] = std::move(zone); 1185c7cc545SPatrick Venture } 1195c7cc545SPatrick Venture 1205c7cc545SPatrick Venture return zones; 1215c7cc545SPatrick Venture } 122