blob: a72a812582b5ce555cbe43686d20ba6a97352c3a [file] [log] [blame]
Andrew Geissler65c01012021-06-15 14:03:34 -05001extern "C"
2{
3#include "libpdbg.h"
4}
5
6#include "common_utils.hpp"
7#include "p10_cfam.hpp"
8#include "procedures/phal/common_utils.hpp"
9#include "registration.hpp"
10
11#include <phosphor-logging/log.hpp>
12
13#include <cstdio>
14#include <fstream>
15#include <memory>
16
17namespace openpower
18{
19namespace phal
20{
21
22using namespace openpower::cfam::p10;
23using namespace phosphor::logging;
24
25/**
26 * This is the backup plan to ensuring the host is not running before the
27 * BMC issues a power off to the system. Prior to this procedure being called,
28 * the BMC has tried all other communication mechanisms to talk with the host
29 * and they have failed. The design is that the host firmware will write the
30 * value 0xA5000001 to Mailbox scratch register 12 when they are up and running
31 * to a point where communication to the BMC is no longer required to function.
32 * On a power off or shutdown this register is cleared by the host and BMC
33 * firmware. If the BMC sees the 0xA5000001 pattern in the scratch register
34 * then it assumes the host is running and will leave power on to the system.
35 */
36void checkHostRunning()
37{
38 struct pdbg_target* procTarget;
39
40 try
41 {
42 phal_init();
43 }
44 catch (std::exception& ex)
45 {
46 // This should "never" happen so just throw the exception and let
47 // our systemd error handling process this
48 log<level::ERR>("Exception raised during init PHAL",
49 entry("EXCEPTION=%s", ex.what()));
50 throw std::runtime_error("PHAL initialization failed");
51 }
52
53 pdbg_for_each_class_target("proc", procTarget)
54 {
55 // Only check the primary proc
56 if (!isPrimaryProc(procTarget))
57 {
58 continue;
59 }
60
61 uint32_t val = 0;
62 constexpr uint32_t HOST_RUNNING_INDICATION = 0xA5000001;
63 auto rc = getCFAM(procTarget, P10_SCRATCH_REG_12, val);
64 if ((rc == 0) && (val != HOST_RUNNING_INDICATION))
65 {
66 log<level::INFO>("CFAM read indicates host is not running",
67 entry("CFAM=0x%X", val));
68 return;
69 }
70
71 if (rc != 0)
72 {
73 // On error, we have to assume host is up so just fall through
74 // to code below
75 log<level::ERR>("CFAM read error, assume host is running");
76 }
77 else if (val == HOST_RUNNING_INDICATION)
78 {
79 // This is not good. Normal communication path to host did not work
80 // but CFAM indicates host is running.
81 log<level::ERR>("CFAM read indicates host is running");
82 }
83
84 // TODO - Create Error
85
86 // Create file for host instance and create in filesystem to
87 // indicate to services that host is running.
88 // This file is cleared by the phosphor-state-manager once the host
89 // start target completes.
90 constexpr auto HOST_RUNNING_FILE = "/run/openbmc/host@%d-on";
91 auto size = std::snprintf(nullptr, 0, HOST_RUNNING_FILE, 0);
92 size++; // null
93 std::unique_ptr<char[]> buf(new char[size]);
94 std::snprintf(buf.get(), size, HOST_RUNNING_FILE, 0);
95 std::ofstream outfile(buf.get());
96 outfile.close();
97 return;
98 }
99
100 // We should "never" make it here. If we did it implies no primary processor
101 // was found. Once again, rely on systemd recovery if this happens
102 log<level::ERR>("No primary processor found in checkHostRunning");
103 throw std::runtime_error("No primary processor found in checkHostRunning");
104}
105
106REGISTER_PROCEDURE("checkHostRunning", checkHostRunning)
107
108} // namespace phal
109} // namespace openpower