Add support for memory preserving reboot

Memory preserving reboot is executed when an error encountered
in the hypervisor. There are two SBE chip ops to be executed to
initiate and complete this reboot. The sbe_enter_mpipl chip op
initiates the powering off process and sbe_continue_mpipl restarts
the host in the memory preserving way.

Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
Change-Id: I5b40398921e3856645bb3f292e49a1de12659f7e
diff --git a/Makefile.am b/Makefile.am
index d1842cb..f9bb234 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -33,7 +33,8 @@
                                  $(PHOSPHOR_DBUS_INTERFACES_LIBS) \
                                  $(SDBUSPLUS_LIBS) \
                                  -lstdc++fs \
-                                 -lgpiodcxx
+                                 -lgpiodcxx \
+                                 -lpthread
 
 openpower_proc_control_CXXFLAGS = $(PHOSPHOR_LOGGING_CFLAGS) \
                                   $(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
diff --git a/procedures/p9/enter_mpreboot.cpp b/procedures/p9/enter_mpreboot.cpp
new file mode 100644
index 0000000..c8ead0c
--- /dev/null
+++ b/procedures/p9/enter_mpreboot.cpp
@@ -0,0 +1,76 @@
+/**
+ * Copyright (C) 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <libpdbg.h>
+
+#include <phosphor-logging/log.hpp>
+#include <thread>
+#include <vector>
+
+namespace openpower
+{
+namespace p9
+{
+
+/**
+ * @brief Calls sbe_enter_mpipl on the SBE in the provided target.
+ * @return void
+ */
+void sbeEnterMpReboot(struct pdbg_target* tgt)
+{
+    using namespace phosphor::logging;
+    int error = 0;
+    if ((error = sbe_mpipl_enter(tgt)) < 0)
+    {
+        log<level::ERR>("Failed to initiate memory preserving reboot");
+        // TODO Create a PEL in the future for this failure case.
+        throw std::system_error(error, std::generic_category(),
+                                "Failed to initiate memory preserving reboot");
+    }
+}
+
+/**
+ * @brief initiate memory preserving reboot on each SBE.
+ * @return void
+ */
+void enterMpReboot()
+{
+    using namespace phosphor::logging;
+    struct pdbg_target* target;
+    std::vector<std::thread> threads;
+    pdbg_targets_init(NULL);
+
+    log<level::INFO>("Starting memory preserving reboot");
+    pdbg_for_each_class_target("pib", target)
+    {
+        if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
+        {
+            continue;
+        }
+        std::thread t(sbeEnterMpReboot, target);
+        threads.push_back(std::move(t));
+    }
+
+    for (auto& t : threads)
+    {
+        t.join();
+    }
+}
+
+REGISTER_PROCEDURE("enterMpReboot", enterMpReboot);
+
+} // namespace p9
+} // namespace openpower
diff --git a/procedures/p9/start_host_mpreboot.cpp b/procedures/p9/start_host_mpreboot.cpp
new file mode 100644
index 0000000..192f6ca
--- /dev/null
+++ b/procedures/p9/start_host_mpreboot.cpp
@@ -0,0 +1,112 @@
+/**
+ * Copyright (C) 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "cfam_access.hpp"
+#include "ext_interface.hpp"
+#include "p9_cfam.hpp"
+#include "registration.hpp"
+#include "targeting.hpp"
+
+#include <libpdbg.h>
+
+#include <phosphor-logging/log.hpp>
+namespace openpower
+{
+namespace p9
+{
+
+using namespace openpower::cfam::access;
+using namespace openpower::cfam::p9;
+using namespace openpower::targeting;
+
+/**
+ * @brief Continue with memory preserving reboot.
+ * @return void
+ */
+void startHostMpReboot()
+{
+    using namespace phosphor::logging;
+
+    Targeting targets;
+    const auto& master = *(targets.begin());
+
+    log<level::INFO>("Running P9 procedure startHostMpReboot",
+                     entry("NUM_PROCS=%d", targets.size()));
+
+    // Ensure asynchronous clock mode is set
+    writeReg(master, P9_LL_MODE_REG, 0x00000001);
+
+    // Clock mux select override
+    for (const auto& t : targets)
+    {
+        writeRegWithMask(t, P9_ROOT_CTRL8, 0x0000000C, 0x0000000C);
+    }
+
+    // Enable P9 checkstop to be reported to the BMC
+
+    // Setup FSI2PIB to report checkstop
+    writeReg(master, P9_FSI_A_SI1S, 0x20000000);
+
+    // Enable Xstop/ATTN interrupt
+    writeReg(master, P9_FSI2PIB_TRUE_MASK, 0x60000000);
+
+    // Arm it
+    writeReg(master, P9_FSI2PIB_INTERRUPT, 0xFFFFFFFF);
+
+    // Kick off the SBE to start the boot
+
+    // Choose seeprom side to boot from
+    cfam_data_t sbeSide = 0;
+    if (getBootCount() > 0)
+    {
+        sbeSide = 0;
+        log<level::INFO>("Setting SBE seeprom side to 0",
+                         entry("SBE_SIDE_SELECT=%d", 0));
+    }
+    else
+    {
+        sbeSide = 0x00004000;
+        log<level::INFO>("Setting SBE seeprom side to 1",
+                         entry("SBE_SIDE_SELECT=%d", 1));
+    }
+    // Bit 17 of the ctrl status reg indicates sbe seeprom boot side
+    // 0 -> Side 0, 1 -> Side 1
+    writeRegWithMask(master, P9_SBE_CTRL_STATUS, sbeSide, 0x00004000);
+
+    // Call enter mpipl
+    pdbg_targets_init(NULL);
+    struct pdbg_target* target;
+    pdbg_for_each_class_target("pib", target)
+    {
+        if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED)
+        {
+            continue;
+        }
+
+        int error = 0;
+        if ((error = sbe_mpipl_continue(target)) < 0)
+        {
+            log<level::ERR>("Failed to execute sbe_mpipl_contiue");
+            throw std::system_error(error, std::generic_category(),
+                                    "Failed to continue with mp reboot");
+        }
+        break;
+    }
+}
+
+REGISTER_PROCEDURE("startHostMpReboot", startHostMpReboot);
+
+} // namespace p9
+} // namespace openpower