blob: 941b500b80872f80cf3369236c2d1371867939d5 [file] [log] [blame]
Zane Shelleyf4bd5ff2020-11-05 22:26:04 -06001
2#include <assert.h>
3
4#include <hei_main.hpp>
5#include <util/pdbg.hpp>
6#include <util/trace.hpp>
7
8#include <filesystem>
9#include <fstream>
10#include <map>
11#include <vector>
12
13namespace fs = std::filesystem;
14
15namespace analyzer
16{
17
18//------------------------------------------------------------------------------
19
20void __getChipDataFiles(std::map<libhei::ChipType_t, fs::path>& o_files)
21{
22 o_files.clear();
23
24 auto directory = "/usr/share/openpower-libhei/";
25
26 for (const auto& entry : fs::directory_iterator(directory))
27 {
28 auto path = entry.path();
29
30 std::ifstream file{path, std::ios::binary};
31 if (!file.good())
32 {
33 trace::err("Unable to open file: %s", path.string().c_str());
34 continue;
35 }
36
37 // The first 8-bytes is the file keyword and the next 4-bytes is the
38 // chip type.
39 libhei::FileKeyword_t keyword;
40 libhei::ChipType_t chipType;
41
42 const size_t sz_keyword = sizeof(keyword);
43 const size_t sz_chipType = sizeof(chipType);
44 const size_t sz_buffer = sz_keyword + sz_chipType;
45
46 // Read the keyword and chip type from the file.
47 char buffer[sz_buffer];
48 file.read(buffer, sz_buffer);
49 if (!file.good())
50 {
51 trace::err("Unable to read file: %s", path.string().c_str());
52 continue;
53 }
54
55 // Get the keyword.
56 memcpy(&keyword, &buffer[0], sz_keyword);
57 keyword = be64toh(keyword);
58
59 // Ensure the keyword value is correct.
60 if (libhei::KW_CHIPDATA != keyword)
61 {
62 trace::err("Invalid chip data file: %s", path.string().c_str());
63 continue;
64 }
65
66 // Get the chip type.
67 memcpy(&chipType, &buffer[sz_keyword], sz_chipType);
68 chipType = be32toh(chipType);
69
70 // Trace each legitimate chip data file for debug.
71 trace::inf("File found: type=0x%0" PRIx32 " path=%s", chipType,
72 path.string().c_str());
73
74 // So far, so good. Add the entry.
75 auto ret = o_files.emplace(chipType, path);
76 assert(ret.second); // Should not have duplicate entries
77 }
78}
79
80//------------------------------------------------------------------------------
81
82void __initialize(const fs::path& i_path)
83{
84 // Get file size.
85 const auto sz_buffer = fs::file_size(i_path);
86
87 // Create a buffer large enough to hold the entire file.
88 std::vector<char> buffer(sz_buffer);
89
90 // Open the chip data file.
91 std::ifstream file{i_path, std::ios::binary};
92 assert(file.good()); // We've opened it once before, so it should open now.
93
94 // Read the entire file into the buffer.
95 file.read(buffer.data(), sz_buffer);
96 assert(file.good()); // Again, this should be readable.
97
98 // This is not necessary, but it frees up memory before calling the memory
99 // intensive initialize() function.
100 file.close();
101
102 // Initialize the isolator with this chip data file.
103 libhei::initialize(buffer.data(), sz_buffer);
104}
105
106//------------------------------------------------------------------------------
107
108void initializeIsolator(const std::vector<libhei::Chip>& i_chips)
109{
110 // Find all of the existing chip data files.
111 std::map<libhei::ChipType_t, fs::path> files;
112 __getChipDataFiles(files);
113
114 // Keep track of models/levels that have already been initialized.
115 std::map<libhei::ChipType_t, unsigned int> initTypes;
116
117 for (const auto& chip : i_chips)
118 {
119 auto chipType = chip.getType();
120
121 // Trace each chip for debug.
122 trace::inf("Chip found: type=0x%0" PRIx32 " chip=%s", chipType,
123 util::pdbg::getPath(chip));
124
125 if (0 == chipType)
126 {
127 // This is a special case. It means the model/level attributes have
128 // not been initialized in the devtree. This is possible on the
129 // epoch IPL where an attention occurs before Hostboot is able to
130 // update the devtree information on the BMC.
131
132 // TODO: Consider logging a PEL. Just skip for now.
133 continue;
134 }
135
136 // Mark this chip type as initialized (or will be if it hasn't been).
137 auto ret = initTypes.emplace(chipType, 1);
138 if (!ret.second)
139 {
140 // This type has already been initialized. Nothing more to do.
141 continue;
142 }
143
144 // Get the file for this chip.
145 auto itr = files.find(chipType);
146
147 // Ensure a chip data file exist for this chip.
148 assert(files.end() != itr);
149
150 // Initialize this chip type.
151 __initialize(itr->second);
152 }
153}
154
155//------------------------------------------------------------------------------
156
157} // namespace analyzer