blob: 65fad1bcc0a541c8d8f63eeb2aaba7c99cfbafd1 [file] [log] [blame]
James Feist139cb572018-09-10 15:26:18 -07001/*
2// Copyright (c) 2018 Intel Corporation
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 <fcntl.h>
18#include <linux/peci-ioctl.h>
19
20#include <CPUSensor.hpp>
21#include <Utils.hpp>
22#include <VariantVisitors.hpp>
23#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/container/flat_set.hpp>
26#include <boost/date_time/posix_time/posix_time.hpp>
27#include <boost/process/child.hpp>
28#include <experimental/filesystem>
29#include <fstream>
30#include <regex>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
34static constexpr bool DEBUG = false;
35
36enum State
37{
38 OFF, // host powered down
39 ON, // host powered on
40 READY // host powered on and mem test passed - fully ready
41};
42
43struct CPUConfig
44{
45 CPUConfig(const int& address, const std::string& overlayName,
46 const State& st) :
47 addr(address),
48 ovName(overlayName), state(st)
49 {
50 }
51 int addr;
52 std::string ovName;
53 State state;
54
55 bool operator<(const CPUConfig& rhs) const
56 {
57 return (ovName < rhs.ovName);
58 }
59};
60
61static constexpr const char* DT_OVERLAY = "/usr/bin/dtoverlay";
62static constexpr const char* OVERLAY_DIR = "/tmp/overlays";
Yoo, Jae Hyunf033e672018-10-17 12:07:54 -070063static constexpr const char* PECI_DEV = "/dev/peci-0";
James Feist139cb572018-09-10 15:26:18 -070064static constexpr const unsigned int RANK_NUM_MAX = 8;
65
66namespace fs = std::experimental::filesystem;
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -070067namespace variant_ns = sdbusplus::message::variant_ns;
James Feist139cb572018-09-10 15:26:18 -070068static constexpr const char* CONFIG_PREFIX =
69 "xyz.openbmc_project.Configuration.";
70static constexpr std::array<const char*, 3> SENSOR_TYPES = {
71 "SkylakeCPU", "BroadwellCPU", "HaswellCPU"};
72
73const static std::regex ILLEGAL_NAME_REGEX("[^A-Za-z0-9_]");
74
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -070075bool createSensors(
James Feist139cb572018-09-10 15:26:18 -070076 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
77 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
78 sensors,
79 boost::container::flat_set<CPUConfig>& configs,
80 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
81{
82 bool available = false;
83 for (CPUConfig cpu : configs)
84 {
85 if (cpu.state != State::OFF)
86 {
87 available = true;
88 break;
89 }
90 }
91 if (!available)
92 {
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -070093 return false;
James Feist139cb572018-09-10 15:26:18 -070094 }
95
96 // use new data the first time, then refresh
97 ManagedObjectType sensorConfigurations;
98 bool useCache = false;
99 for (const char* type : SENSOR_TYPES)
100 {
101 if (!getSensorConfiguration(CONFIG_PREFIX + std::string(type),
102 dbusConnection, sensorConfigurations,
103 useCache))
104 {
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700105 return false;
James Feist139cb572018-09-10 15:26:18 -0700106 }
107 useCache = true;
108 }
109
110 std::vector<fs::path> oemNamePaths;
111 if (!find_files(fs::path(R"(/sys/bus/peci/devices)"),
112 R"(peci\d+/\d+-.+/of_node/oemname1$)", oemNamePaths, 2))
113 {
114 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700115 return true;
James Feist139cb572018-09-10 15:26:18 -0700116 }
117
118 for (fs::path& oemNamePath : oemNamePaths)
119 {
120 std::ifstream nameFile(oemNamePath);
121 if (!nameFile.good())
122 {
123 std::cerr << "Failure reading " << oemNamePath << "\n";
124 continue;
125 }
126 std::string oemName;
127 std::getline(nameFile, oemName);
128 nameFile.close();
129 if (!oemName.size())
130 {
131 // shouldn't have an empty name file
132 continue;
133 }
134 oemName.pop_back(); // remove trailing null
135 if (DEBUG)
136 std::cout << "Checking: " << oemNamePath << ": " << oemName << "\n";
137
138 const SensorData* sensorData = nullptr;
139 const std::string* interfacePath = nullptr;
140 for (const std::pair<sdbusplus::message::object_path, SensorData>&
141 sensor : sensorConfigurations)
142 {
143 if (!boost::ends_with(sensor.first.str, oemName))
144 {
145 continue;
146 }
147 sensorData = &(sensor.second);
148 interfacePath = &(sensor.first.str);
149 break;
150 }
151 if (sensorData == nullptr)
152 {
153 std::cerr << "failed to find match for " << oemName << "\n";
154 continue;
155 }
156 const std::pair<std::string, boost::container::flat_map<
157 std::string, BasicVariantType>>*
158 baseConfiguration = nullptr;
159 std::string sensorObjectType;
160 for (const char* type : SENSOR_TYPES)
161 {
162 sensorObjectType = CONFIG_PREFIX + std::string(type);
163 auto sensorBase = sensorData->find(sensorObjectType);
164 if (sensorBase != sensorData->end())
165 {
166 baseConfiguration = &(*sensorBase);
167 break;
168 }
169 }
170
171 if (baseConfiguration == nullptr)
172 {
173 std::cerr << "error finding base configuration for" << oemName
174 << "\n";
175 continue;
176 }
177
178 auto findCpuId = baseConfiguration->second.find("CpuID");
179 if (findCpuId == baseConfiguration->second.end())
180 {
181 std::cerr << "could not determine CPU ID for " << oemName << "\n";
182 continue;
183 }
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -0700184 int cpuId = variant_ns::visit(VariantToIntVisitor(), findCpuId->second);
James Feist139cb572018-09-10 15:26:18 -0700185
186 auto directory = oemNamePath.parent_path().parent_path();
187 std::vector<fs::path> inputPaths;
188 if (!find_files(fs::path(directory),
189 R"(peci-.+/hwmon/hwmon\d+/temp\d+_input$)", inputPaths,
190 0))
191 {
192 std::cerr << "No temperature sensors in system\n";
193 continue;
194 }
195
196 // iterate through all found temp sensors
197 for (auto& inputPath : inputPaths)
198 {
199 auto inputPathStr = inputPath.string();
200 auto labelPath =
201 boost::replace_all_copy(inputPathStr, "input", "label");
202 std::ifstream labelFile(labelPath);
203 if (!labelFile.good())
204 {
205 std::cerr << "Failure reading " << labelPath << "\n";
206 continue;
207 }
208 std::string label;
209 std::getline(labelFile, label);
210 labelFile.close();
211 std::string sensorName = label + " CPU" + std::to_string(cpuId);
212 std::vector<thresholds::Threshold> sensorThresholds;
213 std::string labelHead = label.substr(0, label.find(" "));
Yoo, Jae Hyunac18e142018-10-09 16:38:58 -0700214 ParseThresholdsFromConfig(*sensorData, sensorThresholds,
215 &labelHead);
James Feist139cb572018-09-10 15:26:18 -0700216 if (!sensorThresholds.size())
217 {
218 if (!ParseThresholdsFromAttr(sensorThresholds, inputPathStr,
219 CPUSensor::SENSOR_SCALE_FACTOR))
220 {
Yoo, Jae Hyunac18e142018-10-09 16:38:58 -0700221 std::cerr << "error populating thresholds for "
222 << sensorName << "\n";
James Feist139cb572018-09-10 15:26:18 -0700223 }
224 }
225 sensors[sensorName] = std::make_unique<CPUSensor>(
226 inputPathStr, sensorObjectType, objectServer, dbusConnection,
227 io, sensorName, std::move(sensorThresholds), *interfacePath);
228 if (DEBUG)
229 std::cout << "Mapped: " << inputPath << " to " << sensorName
230 << "\n";
231 }
232 }
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700233
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700234 std::cout << "Sensor creation completed\n";
235
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700236 return true;
James Feist139cb572018-09-10 15:26:18 -0700237}
238
239void reloadOverlay(const std::string& overlay)
240{
241 boost::process::child c1(DT_OVERLAY, "-d", OVERLAY_DIR, "-r", overlay);
242 c1.wait();
243 if (c1.exit_code())
244 {
245 if (DEBUG)
246 {
247 std::cout << "DTOverlay unload error with file " << overlay
248 << ". error: " << c1.exit_code() << "\n";
249 }
250
251 /* fall through anyway */
252 }
253
254 boost::process::child c2(DT_OVERLAY, "-d", OVERLAY_DIR, overlay);
255 c2.wait();
256 if (c2.exit_code())
257 {
258 std::cerr << "DTOverlay load error with file " << overlay
259 << ". error: " << c2.exit_code() << "\n";
260 return;
261 }
262}
263
264void detectCpu(boost::asio::deadline_timer& timer, boost::asio::io_service& io,
265 sdbusplus::asio::object_server& objectServer,
266 boost::container::flat_map<std::string,
267 std::unique_ptr<CPUSensor>>& sensors,
268 boost::container::flat_set<CPUConfig>& configs,
269 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
270{
271 auto file = open(PECI_DEV, O_RDWR);
272 if (file < 0)
273 {
274 std::cerr << "unable to open " << PECI_DEV << "\n";
275 std::exit(EXIT_FAILURE);
276 }
277
278 size_t rescanDelaySeconds = 0;
279 bool keepPinging = false;
280 for (CPUConfig& config : configs)
281 {
282 State state;
283 struct peci_ping_msg msg;
284 msg.addr = config.addr;
285 if (!ioctl(file, PECI_IOC_PING, &msg))
286 {
287 bool dimmReady = false;
288 for (unsigned int rank = 0; rank < RANK_NUM_MAX; rank++)
289 {
290 struct peci_rd_pkg_cfg_msg msg;
291 msg.addr = config.addr;
292 msg.index = MBX_INDEX_DDR_DIMM_TEMP;
293 msg.param = rank;
294 msg.rx_len = 4;
295 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
296 {
297 if (msg.pkg_config[0] || msg.pkg_config[1] ||
298 msg.pkg_config[2])
299 {
300 dimmReady = true;
301 break;
302 }
303 }
304 else
305 {
306 break;
307 }
308 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700309
James Feist139cb572018-09-10 15:26:18 -0700310 if (dimmReady)
311 {
312 state = State::READY;
313 }
314 else
315 {
316 state = State::ON;
317 }
318 }
319 else
320 {
321 state = State::OFF;
322 }
323
324 if (config.state != state)
325 {
326 if (config.state == State::OFF)
327 {
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700328 std::cout << config.ovName << " is detected\n";
James Feist139cb572018-09-10 15:26:18 -0700329 reloadOverlay(config.ovName);
330 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700331 if (state == State::READY)
332 {
333 std::cout << "DIMM(s) on " << config.ovName
334 << " is/are detected\n";
335 }
James Feist139cb572018-09-10 15:26:18 -0700336 config.state = state;
337 }
338
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700339 if (config.state != State::OFF)
340 {
341 if (config.state == State::ON)
342 {
343 rescanDelaySeconds = 1;
344 }
345 else
346 {
347 rescanDelaySeconds = 5;
348 }
349 }
350
351 if (config.state != State::READY)
James Feist139cb572018-09-10 15:26:18 -0700352 {
353 keepPinging = true;
354 }
355
356 if (DEBUG)
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700357 std::cout << config.ovName << ", state: " << config.state << "\n";
James Feist139cb572018-09-10 15:26:18 -0700358 }
359
360 close(file);
361
362 if (rescanDelaySeconds)
363 {
364 std::this_thread::sleep_for(std::chrono::seconds(rescanDelaySeconds));
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700365 if (!createSensors(io, objectServer, sensors, configs, dbusConnection))
366 {
367 keepPinging = true;
368 }
James Feist139cb572018-09-10 15:26:18 -0700369 }
370
371 if (keepPinging)
372 {
373 timer.expires_from_now(boost::posix_time::seconds(1));
374 timer.async_wait([&](const boost::system::error_code& ec) {
375 if (ec == boost::asio::error::operation_aborted)
376 {
377 /* we were canceled*/
378 return;
379 }
380 else if (ec)
381 {
382 std::cerr << "timer error\n";
383 return;
384 }
385 detectCpu(timer, io, objectServer, sensors, configs,
386 dbusConnection);
387 });
388 }
389}
390
391void getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
392 boost::container::flat_set<CPUConfig>& configs)
393{
394 ManagedObjectType sensorConfigurations;
395 bool useCache = false;
396 // use new data the first time, then refresh
397 for (const char* type : SENSOR_TYPES)
398 {
399 if (!getSensorConfiguration(CONFIG_PREFIX + std::string(type),
400 systemBus, sensorConfigurations, useCache))
401 {
James Feist139cb572018-09-10 15:26:18 -0700402 return;
403 }
404 useCache = true;
405 }
406
407 // check PECI client addresses and DT overlay names from CPU configuration
408 // before starting ping operation
409 for (const char* type : SENSOR_TYPES)
410 {
411 for (const std::pair<sdbusplus::message::object_path, SensorData>&
412 sensor : sensorConfigurations)
413 {
414 for (const std::pair<
415 std::string,
416 boost::container::flat_map<std::string, BasicVariantType>>&
417 config : sensor.second)
418 {
419 if ((CONFIG_PREFIX + std::string(type)) != config.first)
420 {
421 continue;
422 }
423
424 auto findAddress = config.second.find("Address");
425 if (findAddress == config.second.end())
426 {
427 continue;
428 }
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -0700429 int addr = variant_ns::visit(VariantToIntVisitor(),
430 findAddress->second);
James Feist139cb572018-09-10 15:26:18 -0700431
432 auto findName = config.second.find("Name");
433 if (findName == config.second.end())
434 {
435 continue;
436 }
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -0700437 std::string nameRaw = variant_ns::visit(
James Feist139cb572018-09-10 15:26:18 -0700438 VariantToStringVisitor(), findName->second);
439 std::string name =
440 std::regex_replace(nameRaw, ILLEGAL_NAME_REGEX, "_");
441 std::string overlayName = name + "_" + type;
442
443 if (DEBUG)
444 {
445 std::cout << "addr: " << addr << "\n";
446 std::cout << "name: " << name << "\n";
447 std::cout << "type: " << type << "\n";
448 std::cout << "overlayName: " << overlayName << "\n";
449 }
450
451 configs.emplace(addr, overlayName, State::OFF);
452 }
453 }
454 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700455
456 std::cout << "CPU configs parsed\n";
James Feist139cb572018-09-10 15:26:18 -0700457}
458
459int main(int argc, char** argv)
460{
461 boost::asio::io_service io;
462 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
463 boost::container::flat_set<CPUConfig> configs;
464
465 systemBus->request_name("xyz.openbmc_project.CPUSensor");
466 sdbusplus::asio::object_server objectServer(systemBus);
467 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> sensors;
468 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
469 boost::asio::deadline_timer pingTimer(io);
470 getCpuConfig(systemBus, configs);
471 if (configs.size())
472 {
473 detectCpu(pingTimer, io, objectServer, sensors, configs, systemBus);
474 }
475
476 boost::asio::deadline_timer filterTimer(io);
477 std::function<void(sdbusplus::message::message&)> eventHandler =
478 [&](sdbusplus::message::message& message) {
479 if (message.is_method_error())
480 {
481 std::cerr << "callback method error\n";
482 return;
483 }
484 // this implicitly cancels the timer
485 filterTimer.expires_from_now(boost::posix_time::seconds(1));
486
487 filterTimer.async_wait([&](const boost::system::error_code& ec) {
488 if (ec == boost::asio::error::operation_aborted)
489 {
490 /* we were canceled*/
491 return;
492 }
493 else if (ec)
494 {
495 std::cerr << "timer error\n";
496 return;
497 }
498
499 getCpuConfig(systemBus, configs);
500
501 if (configs.size())
502 {
503 detectCpu(pingTimer, io, objectServer, sensors, configs,
504 systemBus);
505 }
506 });
507 };
508
509 for (const char* type : SENSOR_TYPES)
510 {
511 auto match = std::make_unique<sdbusplus::bus::match::match>(
512 static_cast<sdbusplus::bus::bus&>(*systemBus),
513 "type='signal',member='PropertiesChanged',path_namespace='" +
514 std::string(INVENTORY_PATH) + "',arg0namespace='" +
515 CONFIG_PREFIX + type + "'",
516 eventHandler);
517 matches.emplace_back(std::move(match));
518 }
519
520 io.run();
521}