PHAL: clock daily data logger base infrastructure support

To support system clock issues debug added support to log clock
specific register information every 24 hours and report externally.

This commit provides support to base infrastructure to log informational
logs every 24 hours and at the beginning of application start.

Tested:

"User Header": {
    "Section Version":          "1",
    "Sub-section type":         "0",
    "Log Committed by":         "0x2000",
    "Subsystem":                "CEC Hardware - Clock",
    "Event Scope":              "Entire Platform",
    "Event Severity":           "Informational Event",
    "Event Type":               "Miscellaneous, Informational Only",
    "Action Flags": [
                                "Service Action Required",
                                "Report Externally",
                                "HMC Call Home"
    ],
    "Host Transmission":        "Not Sent",
    "HMC Transmission":         "Not Sent"
},
"Primary SRC": {
    "Section Version":          "1",
    "Sub-section type":         "1",
    "Created by":               "0x3000",
    "SRC Version":              "0x02",
    "SRC Format":               "0x55",
    "Virtual Progress SRC":     "False",
    "I5/OS Service Event Bit":  "False",
    "Hypervisor Dump Initiated":"False",
    "Backplane CCIN":           "2E33",
    "Terminate FW Error":       "False",
    "Deconfigured":             "False",
    "Guarded":                  "False",
    "Error Details": {
        "Message":              "Informational error to house clock debug info"
    },
    "Valid Word Count":         "0x09",
    "Reference Code":           "BD58300A",
    "Hex Word 2":               "00080055",
    "Hex Word 3":               "2E330010",
    "Hex Word 4":               "00000000",
    "Hex Word 5":               "00000000",
    "Hex Word 6":               "00000000",
    "Hex Word 7":               "00000000",
    "Hex Word 8":               "00000000",
    "Hex Word 9":               "00000000"
}

Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
Change-Id: I91cffe17ffe7d38b1127f89e61484c597b9b378b
diff --git a/extensions/phal/clock_logger.cpp b/extensions/phal/clock_logger.cpp
new file mode 100644
index 0000000..4bda427
--- /dev/null
+++ b/extensions/phal/clock_logger.cpp
@@ -0,0 +1,159 @@
+/**
+ * Copyright © 2022 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 "extensions/phal/clock_logger.hpp"
+
+#include "util.hpp"
+
+#include <attributes_info.H>
+#include <libphal.H>
+
+#include <phosphor-logging/lg2.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/utility/timer.hpp>
+
+#include <chrono>
+
+using namespace openpower::pel;
+
+PHOSPHOR_LOG2_USING;
+
+namespace openpower::phal::clock
+{
+constexpr auto CLOCK_DAILY_LOGGER_TIMEOUT_IN_HOUR = 24;
+
+Manager::Manager(const sdeventplus::Event& event) :
+    _event(event), timer(event, std::bind(&Manager::timerExpired, this))
+
+{
+    try
+    {
+        // pdbg initialisation
+        openpower::phal::pdbg::init();
+
+        // Create clock data log.
+        createClockDataLog();
+    }
+    catch (const std::exception& e)
+    {
+        error("Clock Data Log exception ({ERROR})", "ERROR", e);
+    }
+
+    addTimer();
+}
+
+void Manager::addTimer()
+{
+    // Set timer for 24 hours.
+    timer.restart(std::chrono::hours(CLOCK_DAILY_LOGGER_TIMEOUT_IN_HOUR));
+}
+
+void Manager::timerExpired()
+{
+    info("Clock daily logging started");
+
+    try
+    {
+        // Create clock data log.
+        createClockDataLog();
+    }
+    catch (const std::exception& e)
+    {
+        error("createClockDataLog exception ({ERROR})", "ERROR", e);
+    }
+}
+
+void Manager::createClockDataLog()
+{
+    // check chassis power state.
+    auto powerState = openpower::util::getChassisPowerState();
+
+    if (powerState != "xyz.openbmc_project.State.Chassis.PowerState.On")
+    {
+        warning("The chassis power state({POWERSTATE}) is not ON, Skipping "
+                "clock data "
+                "logging",
+                "POWERSTATE", powerState);
+        return;
+    }
+
+    // Data logger storage
+    FFDCData clockDataLog;
+
+    struct pdbg_target* procTarget;
+    ATTR_HWAS_STATE_Type hwasState;
+    pdbg_for_each_class_target("proc", procTarget)
+    {
+        if (DT_GET_PROP(ATTR_HWAS_STATE, procTarget, hwasState))
+        {
+            error("{TARGET} Could not read HWAS_STATE attribute", "TARGET",
+                  pdbg_target_path(procTarget));
+            continue;
+        }
+        if (!hwasState.present)
+        {
+            continue;
+        }
+
+        auto index = std::to_string(pdbg_target_index(procTarget));
+
+        // update functional State
+        std::string funState = "Non Functional";
+
+        if (hwasState.functional)
+        {
+            funState = "Functional";
+        }
+        std::stringstream ssState;
+        ssState << "Proc" << index;
+        clockDataLog.push_back(std::make_pair(ssState.str(), funState));
+
+        // update location code information
+        ATTR_LOCATION_CODE_Type locationCode;
+        memset(&locationCode, '\0', sizeof(locationCode));
+        try
+        {
+            openpower::phal::pdbg::getLocationCode(procTarget, locationCode);
+        }
+        catch (const std::exception& e)
+        {
+            error("getLocationCode on {TARGET} thrown exception ({ERROR})",
+                  "TARGET", pdbg_target_path(procTarget), "ERROR", e);
+        }
+        std::stringstream ssLoc;
+        ssLoc << "Proc" << index << " Location Code";
+        clockDataLog.push_back(std::make_pair(ssLoc.str(), locationCode));
+
+        // Update Processor EC level
+        ATTR_EC_Type ecVal = 0;
+        if (DT_GET_PROP(ATTR_EC, procTarget, ecVal))
+        {
+            error("Could not read ATTR_EC  attribute");
+        }
+        std::stringstream ssEC;
+        ssEC << "Proc" << index << " EC";
+
+        std::stringstream ssECVal;
+        ssECVal << "0x" << std::setfill('0') << std::setw(10) << std::hex
+                << (uint16_t)ecVal;
+        clockDataLog.push_back(std::make_pair(ssEC.str(), ssECVal.str()));
+    }
+
+    openpower::pel::createPEL("org.open_power.PHAL.Info.ClockDailyLog",
+                              clockDataLog);
+}
+
+} // namespace openpower::phal::clock
diff --git a/extensions/phal/clock_logger.hpp b/extensions/phal/clock_logger.hpp
new file mode 100644
index 0000000..a9982d1
--- /dev/null
+++ b/extensions/phal/clock_logger.hpp
@@ -0,0 +1,77 @@
+/**
+ * Copyright © 2022 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.
+ */
+#pragma once
+
+#include "extensions/phal/create_pel.hpp"
+
+#include <fmt/format.h>
+
+#include <sdeventplus/utility/timer.hpp>
+
+#include <chrono>
+
+namespace openpower::phal::clock
+{
+
+/* Dbus event timer */
+using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
+
+/**
+ * @class Manager - Represents the clock daily management functions
+ *
+ */
+class Manager
+{
+  public:
+    Manager() = delete;
+    Manager(const Manager&) = delete;
+    Manager(Manager&&) = delete;
+    Manager& operator=(const Manager&) = delete;
+    Manager& operator=(Manager&&) = delete;
+    ~Manager() = default;
+
+    /**
+     * Constructor
+     * Starts the functions required to capture clock daily logger data.
+     *
+     * @param[in] event - sdeventplus event loop
+     */
+    Manager(const sdeventplus::Event& event);
+
+    /**
+     * @brief Add a dbus timer
+     */
+    void addTimer();
+
+    /**
+     * @brief Callback when a timer expires
+     */
+    void timerExpired();
+
+    /**
+     * @brief utility function to create clock data log
+     */
+    void createClockDataLog();
+
+  private:
+    /* The sdeventplus even loop to use */
+    sdeventplus::Event _event;
+
+    /** @brief Timer used for LEDs lamp test period */
+    sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
+};
+
+} // namespace openpower::phal::clock
diff --git a/extensions/phal/clock_logger_main.cpp b/extensions/phal/clock_logger_main.cpp
new file mode 100644
index 0000000..7d85764
--- /dev/null
+++ b/extensions/phal/clock_logger_main.cpp
@@ -0,0 +1,46 @@
+/**
+ * Copyright © 2022 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 "extensions/phal/clock_logger.hpp"
+
+#include <fmt/format.h>
+
+#include <phosphor-logging/lg2.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdeventplus/source/event.hpp>
+
+#include <cstdlib>
+
+PHOSPHOR_LOG2_USING;
+;
+
+int main()
+{
+    try
+    {
+        info("Clock daily logger started");
+        auto bus = sdbusplus::bus::new_default();
+        auto event = sdeventplus::Event::get_default();
+        openpower::phal::clock::Manager manager(event);
+        bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
+        return event.loop();
+    }
+    catch (const std::exception& ex)
+    {
+        error("exception during application load: ({ERROR})", "ERROR", ex);
+        throw;
+    }
+}
diff --git a/meson.build b/meson.build
index 76bc8fe..f8880b2 100644
--- a/meson.build
+++ b/meson.build
@@ -196,6 +196,26 @@
        ],
        install: true
    )
+
+   executable(
+       'openpower-clock-data-logger',
+       [
+            'extensions/phal/clock_logger_main.cpp',
+            'extensions/phal/clock_logger.cpp',
+            'extensions/phal/create_pel.cpp',
+            'util.cpp',
+       ],
+       dependencies: [
+             cxx.find_library('dtree'),
+             cxx.find_library('pdbg'),
+             cxx.find_library('phal'),
+             dependency('libdt-api'),
+             dependency('phosphor-logging'),
+             dependency('sdbusplus'),
+             dependency('sdeventplus'),
+       ],
+       install: true
+)
 endif
 
 unit_files = [