PHAL: refactoring common_utils

Moved pdbg specific utils function into new file.
This will help to avoid adding additional phal library
dependency for apps , which are not really consumed
phal repos.

Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
Change-Id: Ibabd8096afe8402551d1314f0ff5b89cac891aad
diff --git a/extensions/phal/common_utils.cpp b/extensions/phal/common_utils.cpp
index d9749db..fb90e88 100644
--- a/extensions/phal/common_utils.cpp
+++ b/extensions/phal/common_utils.cpp
@@ -1,10 +1,8 @@
-extern "C"
-{
-#include <libpdbg.h>
-}
+#include "extensions/phal/common_utils.hpp"
+
 #include "attributes_info.H"
 
-#include "extensions/phal/common_utils.hpp"
+#include "extensions/phal/pdbg_utils.hpp"
 #include "extensions/phal/phal_error.hpp"
 
 #include <fmt/format.h>
@@ -73,125 +71,5 @@
     }
 }
 
-pdbg_target* getFsiTarget(struct pdbg_target* procTarget)
-{
-
-    struct pdbg_target* fsiTarget = nullptr;
-    pdbg_for_each_target("fsi", procTarget, fsiTarget)
-    {
-        // grab first one we find
-        break;
-    }
-    if (!fsiTarget)
-    {
-        log<level::ERR>(
-            "fsi path of target not found",
-            entry("PROC_TARGET_PATH=%s", pdbg_target_path(procTarget)));
-        return nullptr;
-    }
-
-    return fsiTarget;
-}
-
-uint32_t probeTarget(struct pdbg_target* procTarget)
-{
-    struct pdbg_target* pibTarget = nullptr;
-    pdbg_for_each_target("pib", procTarget, pibTarget)
-    {
-        // grab first one we find
-        break;
-    }
-    if (!pibTarget)
-    {
-        log<level::ERR>(
-            "pib path of target not found",
-            entry("PROC_TARGET_PATH=%s", pdbg_target_path(procTarget)));
-        return -1;
-    }
-    // probe PIB and ensure it's enabled
-    if (PDBG_TARGET_ENABLED != pdbg_target_probe(pibTarget))
-    {
-        log<level::ERR>(
-            "probe on pib target failed",
-            entry("PIB_TARGET_PATH=%s", pdbg_target_path(pibTarget)));
-        return -1;
-    }
-    return 0;
-}
-
-uint32_t getCFAM(struct pdbg_target* procTarget, const uint32_t reg,
-                 uint32_t& val)
-{
-
-    pdbg_target* fsiTarget = getFsiTarget(procTarget);
-    if (nullptr == fsiTarget)
-    {
-        log<level::ERR>("getCFAM: fsi path or target not found");
-        return -1;
-    }
-
-    auto rc = probeTarget(procTarget);
-    if (rc)
-    {
-        // probe function logged details to journal
-        return rc;
-    }
-
-    rc = fsi_read(fsiTarget, reg, &val);
-    if (rc)
-    {
-        log<level::ERR>(
-            "failed to read input cfam", entry("RC=%u", rc),
-            entry("CFAM=0x%X", reg),
-            entry("FSI_TARGET_PATH=%s", pdbg_target_path(fsiTarget)));
-        return rc;
-    }
-    return 0;
-}
-
-uint32_t putCFAM(struct pdbg_target* procTarget, const uint32_t reg,
-                 const uint32_t val)
-{
-    pdbg_target* fsiTarget = getFsiTarget(procTarget);
-    if (nullptr == fsiTarget)
-    {
-        log<level::ERR>("putCFAM: fsi path or target not found");
-        return -1;
-    }
-
-    auto rc = probeTarget(procTarget);
-    if (rc)
-    {
-        // probe function logged details to journal
-        return rc;
-    }
-
-    rc = fsi_write(fsiTarget, reg, val);
-    if (rc)
-    {
-        log<level::ERR>(
-            "failed to write input cfam", entry("RC=%u", rc),
-            entry("CFAM=0x%X", reg),
-            entry("FSI_TARGET_PATH=%s", pdbg_target_path(fsiTarget)));
-        return rc;
-    }
-    return 0;
-}
-
-void setDevtreeEnv()
-{
-    // PDBG_DTB environment variable set to CEC device tree path
-    static constexpr auto PDBG_DTB_PATH =
-        "/var/lib/phosphor-software-manager/pnor/rw/DEVTREE";
-
-    if (setenv("PDBG_DTB", PDBG_DTB_PATH, 1))
-    {
-        log<level::ERR>(
-            fmt::format("Failed to set PDBG_DTB: ({})", strerror(errno))
-                .c_str());
-        throw std::runtime_error("Failed to set PDBG_DTB");
-    }
-}
-
 } // namespace phal
 } // namespace openpower
diff --git a/extensions/phal/common_utils.hpp b/extensions/phal/common_utils.hpp
index ffd5977..f4e3596 100644
--- a/extensions/phal/common_utils.hpp
+++ b/extensions/phal/common_utils.hpp
@@ -31,57 +31,5 @@
  */
 bool isPrimaryProc(struct pdbg_target* procTarget);
 
-/**
- *  @brief  Read the input CFAM register
- *
- *  @param[in]  procTarget - Processor target to perform the operation on
- *  @param[in]  reg - The register address to read
- *  @param[out] val - The value read from the register
- *
- *  @return 0 on success, non-0 on failure
- */
-uint32_t getCFAM(struct pdbg_target* procTarget, const uint32_t reg,
-                 uint32_t& val);
-
-/**
- *  @brief  Write the input CFAM register
- *
- *  @param[in]  procTarget - Processor target to perform the operation on
- *  @param[in]  reg - The register address to write
- *  @param[out] val - The value to write to the register
- *
- *  @return 0 on success, non-0 on failure
- */
-uint32_t putCFAM(struct pdbg_target* procTarget, const uint32_t reg,
-                 const uint32_t val);
-
-/**
- *  @brief  Helper function to find FSI target needed for FSI operations
- *
- *  @param[in]  procTarget - Processor target to find the FSI target on
- *
- *  @return Valid pointer to FSI target on success, nullptr on failure
- */
-pdbg_target* getFsiTarget(struct pdbg_target* procTarget);
-
-/**
- *  @brief  Helper function to probe the processor target
- *
- *  The probe call only has to happen once per application start so ensure
- *  this function only probes once no matter how many times it's called.
- *
- *  @param[in]  procTarget - Processor target to probe
- *
- *  @return 0 on success, non-0 on failure
- */
-uint32_t probeTarget(struct pdbg_target* procTarget);
-
-/**
- * @brief Helper function to set PDBG_DTB
- *
- * PDBG_DTB environment variable set to CEC device tree path
- */
-void setDevtreeEnv();
-
 } // namespace phal
 } // namespace openpower
diff --git a/extensions/phal/pdbg_utils.cpp b/extensions/phal/pdbg_utils.cpp
new file mode 100644
index 0000000..748b73c
--- /dev/null
+++ b/extensions/phal/pdbg_utils.cpp
@@ -0,0 +1,141 @@
+extern "C"
+{
+#include <libpdbg.h>
+}
+
+#include "extensions/phal/pdbg_utils.hpp"
+#include "extensions/phal/phal_error.hpp"
+
+#include <fmt/format.h>
+
+#include <phosphor-logging/log.hpp>
+
+namespace openpower
+{
+namespace phal
+{
+
+using namespace phosphor::logging;
+
+pdbg_target* getFsiTarget(struct pdbg_target* procTarget)
+{
+
+    struct pdbg_target* fsiTarget = nullptr;
+    pdbg_for_each_target("fsi", procTarget, fsiTarget)
+    {
+        // grab first one we find
+        break;
+    }
+    if (!fsiTarget)
+    {
+        log<level::ERR>(
+            "fsi path of target not found",
+            entry("PROC_TARGET_PATH=%s", pdbg_target_path(procTarget)));
+        return nullptr;
+    }
+
+    return fsiTarget;
+}
+
+uint32_t probeTarget(struct pdbg_target* procTarget)
+{
+    struct pdbg_target* pibTarget = nullptr;
+    pdbg_for_each_target("pib", procTarget, pibTarget)
+    {
+        // grab first one we find
+        break;
+    }
+    if (!pibTarget)
+    {
+        log<level::ERR>(
+            "pib path of target not found",
+            entry("PROC_TARGET_PATH=%s", pdbg_target_path(procTarget)));
+        return -1;
+    }
+    // probe PIB and ensure it's enabled
+    if (PDBG_TARGET_ENABLED != pdbg_target_probe(pibTarget))
+    {
+        log<level::ERR>(
+            "probe on pib target failed",
+            entry("PIB_TARGET_PATH=%s", pdbg_target_path(pibTarget)));
+        return -1;
+    }
+    return 0;
+}
+
+uint32_t getCFAM(struct pdbg_target* procTarget, const uint32_t reg,
+                 uint32_t& val)
+{
+
+    pdbg_target* fsiTarget = getFsiTarget(procTarget);
+    if (nullptr == fsiTarget)
+    {
+        log<level::ERR>("getCFAM: fsi path or target not found");
+        return -1;
+    }
+
+    auto rc = probeTarget(procTarget);
+    if (rc)
+    {
+        // probe function logged details to journal
+        return rc;
+    }
+
+    rc = fsi_read(fsiTarget, reg, &val);
+    if (rc)
+    {
+        log<level::ERR>(
+            "failed to read input cfam", entry("RC=%u", rc),
+            entry("CFAM=0x%X", reg),
+            entry("FSI_TARGET_PATH=%s", pdbg_target_path(fsiTarget)));
+        return rc;
+    }
+    return 0;
+}
+
+uint32_t putCFAM(struct pdbg_target* procTarget, const uint32_t reg,
+                 const uint32_t val)
+{
+    pdbg_target* fsiTarget = getFsiTarget(procTarget);
+    if (nullptr == fsiTarget)
+    {
+        log<level::ERR>("putCFAM: fsi path or target not found");
+        return -1;
+    }
+
+    auto rc = probeTarget(procTarget);
+    if (rc)
+    {
+        // probe function logged details to journal
+        return rc;
+    }
+
+    rc = fsi_write(fsiTarget, reg, val);
+    if (rc)
+    {
+        log<level::ERR>(
+            "failed to write input cfam", entry("RC=%u", rc),
+            entry("CFAM=0x%X", reg),
+            entry("FSI_TARGET_PATH=%s", pdbg_target_path(fsiTarget)));
+        return rc;
+    }
+    return 0;
+}
+
+void setDevtreeEnv()
+{
+    // PDBG_DTB environment variable set to CEC device tree path
+    static constexpr auto PDBG_DTB_PATH =
+        "/var/lib/phosphor-software-manager/pnor/rw/DEVTREE";
+
+    if (setenv("PDBG_DTB", PDBG_DTB_PATH, 1))
+    {
+        log<level::ERR>(
+            fmt::format("Failed to set PDBG_DTB: ({})", strerror(errno))
+                .c_str());
+        throw std::runtime_error("Failed to set PDBG_DTB");
+    }
+}
+
+} // namespace phal
+} // namespace openpower
diff --git a/extensions/phal/pdbg_utils.hpp b/extensions/phal/pdbg_utils.hpp
new file mode 100644
index 0000000..3c1ef32
--- /dev/null
+++ b/extensions/phal/pdbg_utils.hpp
@@ -0,0 +1,68 @@
+#pragma once
+
+#include <libipl.H>
+
+extern "C"
+{
+#include <libpdbg.h>
+}
+
+namespace openpower
+{
+namespace phal
+{
+
+/**
+ *  @brief  Read the input CFAM register
+ *
+ *  @param[in]  procTarget - Processor target to perform the operation on
+ *  @param[in]  reg - The register address to read
+ *  @param[out] val - The value read from the register
+ *
+ *  @return 0 on success, non-0 on failure
+ */
+uint32_t getCFAM(struct pdbg_target* procTarget, const uint32_t reg,
+                 uint32_t& val);
+
+/**
+ *  @brief  Write the input CFAM register
+ *
+ *  @param[in]  procTarget - Processor target to perform the operation on
+ *  @param[in]  reg - The register address to write
+ *  @param[out] val - The value to write to the register
+ *
+ *  @return 0 on success, non-0 on failure
+ */
+uint32_t putCFAM(struct pdbg_target* procTarget, const uint32_t reg,
+                 const uint32_t val);
+
+/**
+ *  @brief  Helper function to find FSI target needed for FSI operations
+ *
+ *  @param[in]  procTarget - Processor target to find the FSI target on
+ *
+ *  @return Valid pointer to FSI target on success, nullptr on failure
+ */
+pdbg_target* getFsiTarget(struct pdbg_target* procTarget);
+
+/**
+ *  @brief  Helper function to probe the processor target
+ *
+ *  The probe call only has to happen once per application start so ensure
+ *  this function only probes once no matter how many times it's called.
+ *
+ *  @param[in]  procTarget - Processor target to probe
+ *
+ *  @return 0 on success, non-0 on failure
+ */
+uint32_t probeTarget(struct pdbg_target* procTarget);
+
+/**
+ * @brief Helper function to set PDBG_DTB
+ *
+ * PDBG_DTB environment variable set to CEC device tree path
+ */
+void setDevtreeEnv();
+
+} // namespace phal
+} // namespace openpower
diff --git a/meson.build b/meson.build
index 891b6b2..b16f045 100644
--- a/meson.build
+++ b/meson.build
@@ -66,6 +66,7 @@
         'procedures/phal/set_SPI_mux.cpp',
         'procedures/phal/proc_pre_poweroff.cpp',
         'extensions/phal/common_utils.cpp',
+        'extensions/phal/pdbg_utils.cpp',
         'procedures/phal/check_host_running.cpp',
         'extensions/phal/create_pel.cpp',
         'extensions/phal/phal_error.cpp',
diff --git a/procedures/phal/check_host_running.cpp b/procedures/phal/check_host_running.cpp
index 6366e56..2ae3fba 100644
--- a/procedures/phal/check_host_running.cpp
+++ b/procedures/phal/check_host_running.cpp
@@ -5,6 +5,7 @@
 
 #include "extensions/phal/common_utils.hpp"
 #include "extensions/phal/create_pel.hpp"
+#include "extensions/phal/pdbg_utils.hpp"
 #include "p10_cfam.hpp"
 #include "registration.hpp"