blob: 708df407291d355432cde504fdf3f03fd7434ce3 [file] [log] [blame]
Chirag Sharmaa2576932020-12-05 23:17:41 -06001extern "C"
2{
3#include <libpdbg.h>
4}
Andrew Geissler5c3f9252021-06-10 10:53:05 -05005#include "attributes_info.H"
Chirag Sharmaa2576932020-12-05 23:17:41 -06006
7#include "phalerror/phal_error.hpp"
8#include "procedures/phal/common_utils.hpp"
9
Jayanth Othayoth583a9ed2021-06-29 05:42:45 -050010#include <fmt/format.h>
Chirag Sharmaa2576932020-12-05 23:17:41 -060011#include <libekb.H>
12
13#include <phosphor-logging/log.hpp>
14
15namespace openpower
16{
17namespace phal
18{
19
20using namespace phosphor::logging;
21
22void phal_init(enum ipl_mode mode)
23{
24 // TODO: Setting boot error callback should not be in common code
25 // because, we wont get proper reason in PEL for failure.
26 // So, need to make code like caller of this function pass error
27 // handling callback.
28 // add callback methods for debug traces and for boot failures
29 openpower::pel::addBootErrorCallbacks();
30
Jayanth Othayoth583a9ed2021-06-29 05:42:45 -050031 // PDBG_DTB environment variable set to CEC device tree path
32 static constexpr auto PDBG_DTB_PATH =
33 "/var/lib/phosphor-software-manager/pnor/rw/DEVTREE";
34
35 if (setenv("PDBG_DTB", PDBG_DTB_PATH, 1))
36 {
37 log<level::ERR>(
38 fmt::format("Failed to set PDBG_DTB: ({})", strerror(errno))
39 .c_str());
40 throw std::runtime_error("Failed to set PDBG_DTB");
41 }
42
Chirag Sharmaa2576932020-12-05 23:17:41 -060043 if (!pdbg_targets_init(NULL))
44 {
45 log<level::ERR>("pdbg_targets_init failed");
46 throw std::runtime_error("pdbg target initialization failed");
47 }
48
49 if (libekb_init())
50 {
51 log<level::ERR>("libekb_init failed");
52 throw std::runtime_error("libekb initialization failed");
53 }
54
55 if (ipl_init(mode) != 0)
56 {
57 log<level::ERR>("ipl_init failed");
58 throw std::runtime_error("libipl initialization failed");
59 }
60}
61
Andrew Geissler5c3f9252021-06-10 10:53:05 -050062bool isPrimaryProc(struct pdbg_target* procTarget)
63{
64 ATTR_PROC_MASTER_TYPE_Type type;
65
66 // Get processor type (Primary or Secondary)
67 if (DT_GET_PROP(ATTR_PROC_MASTER_TYPE, procTarget, type))
68 {
69 log<level::ERR>("Attribute [ATTR_PROC_MASTER_TYPE] get failed");
70 throw std::runtime_error(
71 "Attribute [ATTR_PROC_MASTER_TYPE] get failed");
72 }
73
74 /* Attribute value 0 corresponds to primary processor */
75 if (type == ENUM_ATTR_PROC_MASTER_TYPE_ACTING_MASTER)
76 {
77 return true;
78 }
79 else
80 {
81 return false;
82 }
83}
84
Andrew Geissler65c01012021-06-15 14:03:34 -050085uint32_t getCFAM(struct pdbg_target* procTarget, const uint32_t reg,
86 uint32_t& val)
87{
88 auto procIdx = pdbg_target_index(procTarget);
89 char path[16];
90 sprintf(path, "/proc%d/pib", procIdx);
91
92 pdbg_target* pibTarget = pdbg_target_from_path(nullptr, path);
93 if (nullptr == pibTarget)
94 {
95 log<level::ERR>("pib path of target not found",
96 entry("TARGET_PATH=%s", path));
97 return -1;
98 }
99
100 // probe PIB and ensure it's enabled
101 if (PDBG_TARGET_ENABLED != pdbg_target_probe(pibTarget))
102 {
103 log<level::ERR>("probe on pib target failed");
104 return -1;
105 }
106
107 // now build FSI path and read the input reg
108 sprintf(path, "/proc%d/fsi", procIdx);
109 pdbg_target* fsiTarget = pdbg_target_from_path(nullptr, path);
110 if (nullptr == fsiTarget)
111 {
112 log<level::ERR>("fsi path or target not found");
113 return -1;
114 }
115
116 auto rc = fsi_read(fsiTarget, reg, &val);
117 if (rc)
118 {
119 log<level::ERR>("failed to read input cfam", entry("RC=%u", rc),
120 entry("CFAM=0x%X", reg), entry("TARGET_PATH=%s", path));
121 return rc;
122 }
123 return 0;
124}
125
Chirag Sharmaa2576932020-12-05 23:17:41 -0600126} // namespace phal
127} // namespace openpower