xref: /openbmc/openpower-proc-control/extensions/phal/pdbg_utils.cpp (revision 4d5b5bfe01c5eb83823e2a92d4b1a26455eb87ee)
1 extern "C"
2 {
3 #include <libpdbg.h>
4 }
5 
6 #include "config.h"
7 
8 #include "extensions/phal/pdbg_utils.hpp"
9 #include "extensions/phal/phal_error.hpp"
10 
11 #include <fmt/format.h>
12 
13 #include <phosphor-logging/log.hpp>
14 
15 namespace openpower
16 {
17 namespace phal
18 {
19 
20 using namespace phosphor::logging;
21 
22 pdbg_target* getFsiTarget(struct pdbg_target* procTarget)
23 {
24 
25     struct pdbg_target* fsiTarget = nullptr;
26     pdbg_for_each_target("fsi", procTarget, fsiTarget)
27     {
28         // grab first one we find
29         break;
30     }
31     if (!fsiTarget)
32     {
33         log<level::ERR>(
34             "fsi path of target not found",
35             entry("PROC_TARGET_PATH=%s", pdbg_target_path(procTarget)));
36         return nullptr;
37     }
38 
39     return fsiTarget;
40 }
41 
42 uint32_t probeTarget(struct pdbg_target* procTarget)
43 {
44     struct pdbg_target* pibTarget = nullptr;
45     pdbg_for_each_target("pib", procTarget, pibTarget)
46     {
47         // grab first one we find
48         break;
49     }
50     if (!pibTarget)
51     {
52         log<level::ERR>(
53             "pib path of target not found",
54             entry("PROC_TARGET_PATH=%s", pdbg_target_path(procTarget)));
55         return -1;
56     }
57     // probe PIB and ensure it's enabled
58     if (PDBG_TARGET_ENABLED != pdbg_target_probe(pibTarget))
59     {
60         log<level::ERR>(
61             "probe on pib target failed",
62             entry("PIB_TARGET_PATH=%s", pdbg_target_path(pibTarget)));
63         return -1;
64     }
65     return 0;
66 }
67 
68 uint32_t getCFAM(struct pdbg_target* procTarget, const uint32_t reg,
69                  uint32_t& val)
70 {
71 
72     pdbg_target* fsiTarget = getFsiTarget(procTarget);
73     if (nullptr == fsiTarget)
74     {
75         log<level::ERR>("getCFAM: fsi path or target not found");
76         return -1;
77     }
78 
79     auto rc = probeTarget(procTarget);
80     if (rc)
81     {
82         // probe function logged details to journal
83         return rc;
84     }
85 
86     rc = fsi_read(fsiTarget, reg, &val);
87     if (rc)
88     {
89         log<level::ERR>(
90             "failed to read input cfam", entry("RC=%u", rc),
91             entry("CFAM=0x%X", reg),
92             entry("FSI_TARGET_PATH=%s", pdbg_target_path(fsiTarget)));
93         return rc;
94     }
95     return 0;
96 }
97 
98 uint32_t putCFAM(struct pdbg_target* procTarget, const uint32_t reg,
99                  const uint32_t val)
100 {
101     pdbg_target* fsiTarget = getFsiTarget(procTarget);
102     if (nullptr == fsiTarget)
103     {
104         log<level::ERR>("putCFAM: fsi path or target not found");
105         return -1;
106     }
107 
108     auto rc = probeTarget(procTarget);
109     if (rc)
110     {
111         // probe function logged details to journal
112         return rc;
113     }
114 
115     rc = fsi_write(fsiTarget, reg, val);
116     if (rc)
117     {
118         log<level::ERR>(
119             "failed to write input cfam", entry("RC=%u", rc),
120             entry("CFAM=0x%X", reg),
121             entry("FSI_TARGET_PATH=%s", pdbg_target_path(fsiTarget)));
122         return rc;
123     }
124     return 0;
125 }
126 
127 void setDevtreeEnv()
128 {
129     // PDBG_DTB environment variable set to CEC device tree path
130     if (setenv("PDBG_DTB", CEC_DEVTREE_RW_PATH, 1))
131     {
132         log<level::ERR>(
133             fmt::format("Failed to set PDBG_DTB: ({})", strerror(errno))
134                 .c_str());
135         throw std::runtime_error("Failed to set PDBG_DTB");
136     }
137 }
138 
139 void setPdataInfoDBEnv()
140 {
141     // PDATA_INFODB environment variable set to attributes tool  infodb path
142     static constexpr auto PDATA_INFODB_PATH =
143         "/usr/share/pdata/attributes_info.db";
144 
145     if (setenv("PDATA_INFODB", PDATA_INFODB_PATH, 1))
146     {
147         log<level::ERR>(
148             fmt::format("Failed to set PDATA_INFODB: ({})", strerror(errno))
149                 .c_str());
150         throw std::runtime_error("Failed to set PDATA_INFODB");
151     }
152 }
153 
154 } // namespace phal
155 } // namespace openpower
156