1ed7fb7aeSJayanth Othayoth /**
2ed7fb7aeSJayanth Othayoth  * Copyright © 2022 IBM Corporation
3ed7fb7aeSJayanth Othayoth  *
4ed7fb7aeSJayanth Othayoth  * Licensed under the Apache License, Version 2.0 (the "License");
5ed7fb7aeSJayanth Othayoth  * you may not use this file except in compliance with the License.
6ed7fb7aeSJayanth Othayoth  * You may obtain a copy of the License at
7ed7fb7aeSJayanth Othayoth  *
8ed7fb7aeSJayanth Othayoth  *     http://www.apache.org/licenses/LICENSE-2.0
9ed7fb7aeSJayanth Othayoth  *
10ed7fb7aeSJayanth Othayoth  * Unless required by applicable law or agreed to in writing, software
11ed7fb7aeSJayanth Othayoth  * distributed under the License is distributed on an "AS IS" BASIS,
12ed7fb7aeSJayanth Othayoth  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ed7fb7aeSJayanth Othayoth  * See the License for the specific language governing permissions and
14ed7fb7aeSJayanth Othayoth  * limitations under the License.
15ed7fb7aeSJayanth Othayoth  */
16ed7fb7aeSJayanth Othayoth 
17ed7fb7aeSJayanth Othayoth #include "extensions/phal/clock_logger.hpp"
18ed7fb7aeSJayanth Othayoth 
19ed7fb7aeSJayanth Othayoth #include "util.hpp"
20ed7fb7aeSJayanth Othayoth 
21ed7fb7aeSJayanth Othayoth #include <attributes_info.H>
22ed7fb7aeSJayanth Othayoth #include <libphal.H>
23ed7fb7aeSJayanth Othayoth 
24ed7fb7aeSJayanth Othayoth #include <phosphor-logging/lg2.hpp>
25ed7fb7aeSJayanth Othayoth #include <sdeventplus/event.hpp>
26ed7fb7aeSJayanth Othayoth #include <sdeventplus/utility/timer.hpp>
27ed7fb7aeSJayanth Othayoth 
28ed7fb7aeSJayanth Othayoth #include <chrono>
29ed7fb7aeSJayanth Othayoth 
30ed7fb7aeSJayanth Othayoth using namespace openpower::pel;
31ed7fb7aeSJayanth Othayoth 
32ed7fb7aeSJayanth Othayoth PHOSPHOR_LOG2_USING;
33ed7fb7aeSJayanth Othayoth 
34ed7fb7aeSJayanth Othayoth namespace openpower::phal::clock
35ed7fb7aeSJayanth Othayoth {
36ed7fb7aeSJayanth Othayoth constexpr auto CLOCK_DAILY_LOGGER_TIMEOUT_IN_HOUR = 24;
37ed7fb7aeSJayanth Othayoth 
Manager(const sdeventplus::Event & event)38ed7fb7aeSJayanth Othayoth Manager::Manager(const sdeventplus::Event& event) :
39ed7fb7aeSJayanth Othayoth     _event(event), timer(event, std::bind(&Manager::timerExpired, this))
40ed7fb7aeSJayanth Othayoth 
41ed7fb7aeSJayanth Othayoth {
42ed7fb7aeSJayanth Othayoth     try
43ed7fb7aeSJayanth Othayoth     {
44ed7fb7aeSJayanth Othayoth         // pdbg initialisation
45ed7fb7aeSJayanth Othayoth         openpower::phal::pdbg::init();
46ed7fb7aeSJayanth Othayoth 
47ed7fb7aeSJayanth Othayoth         // Create clock data log.
48ed7fb7aeSJayanth Othayoth         createClockDataLog();
49ed7fb7aeSJayanth Othayoth     }
50ed7fb7aeSJayanth Othayoth     catch (const std::exception& e)
51ed7fb7aeSJayanth Othayoth     {
52ed7fb7aeSJayanth Othayoth         error("Clock Data Log exception ({ERROR})", "ERROR", e);
53ed7fb7aeSJayanth Othayoth     }
54ed7fb7aeSJayanth Othayoth 
55ed7fb7aeSJayanth Othayoth     addTimer();
56ed7fb7aeSJayanth Othayoth }
57ed7fb7aeSJayanth Othayoth 
addTimer()58ed7fb7aeSJayanth Othayoth void Manager::addTimer()
59ed7fb7aeSJayanth Othayoth {
60ed7fb7aeSJayanth Othayoth     // Set timer for 24 hours.
61ed7fb7aeSJayanth Othayoth     timer.restart(std::chrono::hours(CLOCK_DAILY_LOGGER_TIMEOUT_IN_HOUR));
62ed7fb7aeSJayanth Othayoth }
63ed7fb7aeSJayanth Othayoth 
timerExpired()64ed7fb7aeSJayanth Othayoth void Manager::timerExpired()
65ed7fb7aeSJayanth Othayoth {
66ed7fb7aeSJayanth Othayoth     info("Clock daily logging started");
67ed7fb7aeSJayanth Othayoth 
68ed7fb7aeSJayanth Othayoth     try
69ed7fb7aeSJayanth Othayoth     {
70ed7fb7aeSJayanth Othayoth         // Create clock data log.
71ed7fb7aeSJayanth Othayoth         createClockDataLog();
72ed7fb7aeSJayanth Othayoth     }
73ed7fb7aeSJayanth Othayoth     catch (const std::exception& e)
74ed7fb7aeSJayanth Othayoth     {
75ed7fb7aeSJayanth Othayoth         error("createClockDataLog exception ({ERROR})", "ERROR", e);
76ed7fb7aeSJayanth Othayoth     }
77ed7fb7aeSJayanth Othayoth }
78ed7fb7aeSJayanth Othayoth 
createClockDataLog()79ed7fb7aeSJayanth Othayoth void Manager::createClockDataLog()
80ed7fb7aeSJayanth Othayoth {
81ed7fb7aeSJayanth Othayoth     // Data logger storage
82ed7fb7aeSJayanth Othayoth     FFDCData clockDataLog;
83ed7fb7aeSJayanth Othayoth 
84ed7fb7aeSJayanth Othayoth     struct pdbg_target* procTarget;
85ed7fb7aeSJayanth Othayoth     ATTR_HWAS_STATE_Type hwasState;
86ed7fb7aeSJayanth Othayoth     pdbg_for_each_class_target("proc", procTarget)
87ed7fb7aeSJayanth Othayoth     {
88ed7fb7aeSJayanth Othayoth         if (DT_GET_PROP(ATTR_HWAS_STATE, procTarget, hwasState))
89ed7fb7aeSJayanth Othayoth         {
90ed7fb7aeSJayanth Othayoth             error("{TARGET} Could not read HWAS_STATE attribute", "TARGET",
91ed7fb7aeSJayanth Othayoth                   pdbg_target_path(procTarget));
92ed7fb7aeSJayanth Othayoth             continue;
93ed7fb7aeSJayanth Othayoth         }
94ed7fb7aeSJayanth Othayoth         if (!hwasState.present)
95ed7fb7aeSJayanth Othayoth         {
96ed7fb7aeSJayanth Othayoth             continue;
97ed7fb7aeSJayanth Othayoth         }
98ed7fb7aeSJayanth Othayoth 
99ed7fb7aeSJayanth Othayoth         auto index = std::to_string(pdbg_target_index(procTarget));
100ed7fb7aeSJayanth Othayoth 
101ed7fb7aeSJayanth Othayoth         // update functional State
102ed7fb7aeSJayanth Othayoth         std::string funState = "Non Functional";
103ed7fb7aeSJayanth Othayoth 
104ed7fb7aeSJayanth Othayoth         if (hwasState.functional)
105ed7fb7aeSJayanth Othayoth         {
106ed7fb7aeSJayanth Othayoth             funState = "Functional";
107ed7fb7aeSJayanth Othayoth         }
108ed7fb7aeSJayanth Othayoth         std::stringstream ssState;
109ed7fb7aeSJayanth Othayoth         ssState << "Proc" << index;
110ed7fb7aeSJayanth Othayoth         clockDataLog.push_back(std::make_pair(ssState.str(), funState));
111ed7fb7aeSJayanth Othayoth 
112ed7fb7aeSJayanth Othayoth         // update location code information
113ed7fb7aeSJayanth Othayoth         ATTR_LOCATION_CODE_Type locationCode;
114ed7fb7aeSJayanth Othayoth         memset(&locationCode, '\0', sizeof(locationCode));
115ed7fb7aeSJayanth Othayoth         try
116ed7fb7aeSJayanth Othayoth         {
117ed7fb7aeSJayanth Othayoth             openpower::phal::pdbg::getLocationCode(procTarget, locationCode);
118ed7fb7aeSJayanth Othayoth         }
119ed7fb7aeSJayanth Othayoth         catch (const std::exception& e)
120ed7fb7aeSJayanth Othayoth         {
121ed7fb7aeSJayanth Othayoth             error("getLocationCode on {TARGET} thrown exception ({ERROR})",
122ed7fb7aeSJayanth Othayoth                   "TARGET", pdbg_target_path(procTarget), "ERROR", e);
123ed7fb7aeSJayanth Othayoth         }
124ed7fb7aeSJayanth Othayoth         std::stringstream ssLoc;
125ed7fb7aeSJayanth Othayoth         ssLoc << "Proc" << index << " Location Code";
126ed7fb7aeSJayanth Othayoth         clockDataLog.push_back(std::make_pair(ssLoc.str(), locationCode));
127ed7fb7aeSJayanth Othayoth 
128ed7fb7aeSJayanth Othayoth         // Update Processor EC level
129ed7fb7aeSJayanth Othayoth         ATTR_EC_Type ecVal = 0;
130ed7fb7aeSJayanth Othayoth         if (DT_GET_PROP(ATTR_EC, procTarget, ecVal))
131ed7fb7aeSJayanth Othayoth         {
132ed7fb7aeSJayanth Othayoth             error("Could not read ATTR_EC  attribute");
133ed7fb7aeSJayanth Othayoth         }
134ed7fb7aeSJayanth Othayoth         std::stringstream ssEC;
135ed7fb7aeSJayanth Othayoth         ssEC << "Proc" << index << " EC";
136ed7fb7aeSJayanth Othayoth 
137ed7fb7aeSJayanth Othayoth         std::stringstream ssECVal;
138ed7fb7aeSJayanth Othayoth         ssECVal << "0x" << std::setfill('0') << std::setw(10) << std::hex
139ed7fb7aeSJayanth Othayoth                 << (uint16_t)ecVal;
140ed7fb7aeSJayanth Othayoth         clockDataLog.push_back(std::make_pair(ssEC.str(), ssECVal.str()));
14191bf93e0SJayanth Othayoth 
14291bf93e0SJayanth Othayoth         // Add CFAM register information.
14391bf93e0SJayanth Othayoth         addCFAMData(procTarget, clockDataLog);
144ed7fb7aeSJayanth Othayoth     }
145ed7fb7aeSJayanth Othayoth 
1465ffe7fb7SJayanth Othayoth     // Add clock register information
1475ffe7fb7SJayanth Othayoth     addClockRegData(clockDataLog);
1485ffe7fb7SJayanth Othayoth 
149ed7fb7aeSJayanth Othayoth     openpower::pel::createPEL("org.open_power.PHAL.Info.ClockDailyLog",
150*be14ec2dSMatt Spinler                               clockDataLog, Severity::Informational);
151ed7fb7aeSJayanth Othayoth }
152ed7fb7aeSJayanth Othayoth 
addCFAMData(struct pdbg_target * proc,openpower::pel::FFDCData & clockDataLog)15391bf93e0SJayanth Othayoth void Manager::addCFAMData(struct pdbg_target* proc,
15491bf93e0SJayanth Othayoth                           openpower::pel::FFDCData& clockDataLog)
15591bf93e0SJayanth Othayoth {
15691bf93e0SJayanth Othayoth     // collect Processor CFAM register data
15791bf93e0SJayanth Othayoth     const std::vector<int> procCFAMAddr = {
15891bf93e0SJayanth Othayoth         0x1007, 0x2804, 0x2810, 0x2813, 0x2814, 0x2815, 0x2816, 0x281D, 0x281E};
15991bf93e0SJayanth Othayoth 
16091bf93e0SJayanth Othayoth     auto index = std::to_string(pdbg_target_index(proc));
16191bf93e0SJayanth Othayoth 
16291bf93e0SJayanth Othayoth     for (int addr : procCFAMAddr)
16391bf93e0SJayanth Othayoth     {
16491bf93e0SJayanth Othayoth         auto val = 0xDEADBEEF;
16591bf93e0SJayanth Othayoth         try
16691bf93e0SJayanth Othayoth         {
16791bf93e0SJayanth Othayoth             val = openpower::phal::pdbg::getCFAM(proc, addr);
16891bf93e0SJayanth Othayoth         }
16991bf93e0SJayanth Othayoth         catch (const std::exception& e)
17091bf93e0SJayanth Othayoth         {
17191bf93e0SJayanth Othayoth             error("getCFAM on {TARGET} thrown exception({ERROR}): Addr ({REG})",
17291bf93e0SJayanth Othayoth                   "TARGET", pdbg_target_path(proc), "ERROR", e, "REG", addr);
17391bf93e0SJayanth Othayoth         }
17491bf93e0SJayanth Othayoth         std::stringstream ssData;
17591bf93e0SJayanth Othayoth         ssData << "0x" << std::setfill('0') << std::setw(8) << std::hex << val;
17691bf93e0SJayanth Othayoth         std::stringstream ssAddr;
17791bf93e0SJayanth Othayoth         ssAddr << "Proc" << index << " REG 0x" << std::hex << addr;
17891bf93e0SJayanth Othayoth         // update the data
17991bf93e0SJayanth Othayoth         clockDataLog.push_back(make_pair(ssAddr.str(), ssData.str()));
18091bf93e0SJayanth Othayoth     }
18191bf93e0SJayanth Othayoth }
18291bf93e0SJayanth Othayoth 
addClockRegData(openpower::pel::FFDCData & clockDataLog)1835ffe7fb7SJayanth Othayoth void Manager::addClockRegData(openpower::pel::FFDCData& clockDataLog)
1845ffe7fb7SJayanth Othayoth {
1855ffe7fb7SJayanth Othayoth     info("Adding clock register information to daily logger");
1865ffe7fb7SJayanth Othayoth 
1875ffe7fb7SJayanth Othayoth     struct pdbg_target* clockTarget;
1885ffe7fb7SJayanth Othayoth     pdbg_for_each_class_target("oscrefclk", clockTarget)
1895ffe7fb7SJayanth Othayoth     {
1905ffe7fb7SJayanth Othayoth         ATTR_HWAS_STATE_Type hwasState;
1915ffe7fb7SJayanth Othayoth         if (DT_GET_PROP(ATTR_HWAS_STATE, clockTarget, hwasState))
1925ffe7fb7SJayanth Othayoth         {
1935ffe7fb7SJayanth Othayoth             error("({TARGET}) Could not read HWAS_STATE attribute", "TARGET",
1945ffe7fb7SJayanth Othayoth                   pdbg_target_path(clockTarget));
1955ffe7fb7SJayanth Othayoth             continue;
1965ffe7fb7SJayanth Othayoth         }
1975ffe7fb7SJayanth Othayoth 
1985ffe7fb7SJayanth Othayoth         if (!hwasState.present)
1995ffe7fb7SJayanth Othayoth         {
2005ffe7fb7SJayanth Othayoth             continue;
2015ffe7fb7SJayanth Othayoth         }
2025ffe7fb7SJayanth Othayoth 
2035ffe7fb7SJayanth Othayoth         std::string funState = "Non Functional";
2045ffe7fb7SJayanth Othayoth 
2055ffe7fb7SJayanth Othayoth         if (hwasState.functional)
2065ffe7fb7SJayanth Othayoth         {
2075ffe7fb7SJayanth Othayoth             funState = "Functional";
2085ffe7fb7SJayanth Othayoth         }
2095ffe7fb7SJayanth Othayoth 
2105ffe7fb7SJayanth Othayoth         auto index = std::to_string(pdbg_target_index(clockTarget));
2115ffe7fb7SJayanth Othayoth 
2125ffe7fb7SJayanth Othayoth         std::stringstream ssState;
2135ffe7fb7SJayanth Othayoth         ssState << "Clock" << index;
2145ffe7fb7SJayanth Othayoth         clockDataLog.push_back(std::make_pair(ssState.str(), funState));
2155ffe7fb7SJayanth Othayoth 
2165ffe7fb7SJayanth Othayoth         // Add clcok device path information
2175ffe7fb7SJayanth Othayoth         std::stringstream ssName;
2185ffe7fb7SJayanth Othayoth         ssName << "Clock" << index << " path";
2195ffe7fb7SJayanth Othayoth         clockDataLog.push_back(
2205ffe7fb7SJayanth Othayoth             std::make_pair(ssName.str(), pdbg_target_path(clockTarget)));
2215ffe7fb7SJayanth Othayoth 
2225ffe7fb7SJayanth Othayoth         auto status = pdbg_target_probe(clockTarget);
2235ffe7fb7SJayanth Othayoth         if (status != PDBG_TARGET_ENABLED)
2245ffe7fb7SJayanth Othayoth         {
2255ffe7fb7SJayanth Othayoth             continue;
2265ffe7fb7SJayanth Othayoth         }
2275ffe7fb7SJayanth Othayoth 
2285ffe7fb7SJayanth Othayoth         // Update Buffer with clock I2C register data.
2295ffe7fb7SJayanth Othayoth         auto constexpr I2C_READ_SIZE = 0x08;
2305ffe7fb7SJayanth Othayoth         auto constexpr I2C_ADDR_MAX = 0xFF;
2315ffe7fb7SJayanth Othayoth 
2325ffe7fb7SJayanth Othayoth         for (auto addr = 0; addr <= I2C_ADDR_MAX; addr += I2C_READ_SIZE)
2335ffe7fb7SJayanth Othayoth         {
2345ffe7fb7SJayanth Othayoth             std::stringstream ssData;
2355ffe7fb7SJayanth Othayoth 
2365ffe7fb7SJayanth Othayoth             uint8_t data[0x8];
2375ffe7fb7SJayanth Othayoth             auto i2cRc = i2c_read(clockTarget, 0, addr, I2C_READ_SIZE, data);
2385ffe7fb7SJayanth Othayoth             if (i2cRc)
2395ffe7fb7SJayanth Othayoth             {
2405ffe7fb7SJayanth Othayoth                 error("({TARGET}) I2C read error({ERROR}) reported {ADDRESS} ",
2415ffe7fb7SJayanth Othayoth                       "TARGET", pdbg_target_path(clockTarget), "ERROR", i2cRc,
2425ffe7fb7SJayanth Othayoth                       "ADDRESS", addr);
2435ffe7fb7SJayanth Othayoth                 continue;
2445ffe7fb7SJayanth Othayoth             }
2455ffe7fb7SJayanth Othayoth 
2465ffe7fb7SJayanth Othayoth             for (auto i = 0; i < I2C_READ_SIZE; i++)
2475ffe7fb7SJayanth Othayoth             {
2485ffe7fb7SJayanth Othayoth                 ssData << " " << std::hex << std::setfill('0') << std::setw(2)
2495ffe7fb7SJayanth Othayoth                        << (uint16_t)data[i];
2505ffe7fb7SJayanth Othayoth             }
2515ffe7fb7SJayanth Othayoth             std::stringstream ssAddr;
2525ffe7fb7SJayanth Othayoth             ssAddr << "Clock" << index << "_0x" << std::hex << std::setfill('0')
2535ffe7fb7SJayanth Othayoth                    << std::setw(2) << addr;
2545ffe7fb7SJayanth Othayoth             clockDataLog.push_back(make_pair(ssAddr.str(), ssData.str()));
2555ffe7fb7SJayanth Othayoth         }
2565ffe7fb7SJayanth Othayoth     }
2575ffe7fb7SJayanth Othayoth }
2585ffe7fb7SJayanth Othayoth 
259ed7fb7aeSJayanth Othayoth } // namespace openpower::phal::clock
260