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