Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 1 | #include <analyzer/ras-data/ras-data-parser.hpp> |
| 2 | #include <util/data_file.hpp> |
| 3 | #include <util/trace.hpp> |
| 4 | |
| 5 | #include <filesystem> |
| 6 | #include <fstream> |
| 7 | #include <string> |
| 8 | |
| 9 | namespace fs = std::filesystem; |
| 10 | |
| 11 | namespace analyzer |
| 12 | { |
| 13 | |
| 14 | //------------------------------------------------------------------------------ |
| 15 | |
| 16 | std::shared_ptr<Resolution> |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 17 | RasDataParser::getResolution(const libhei::Signature& i_signature) |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 18 | { |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 19 | const auto data = iv_dataFiles.at(i_signature.getChip().getType()); |
| 20 | |
| 21 | const auto action = parseSignature(data, i_signature); |
| 22 | |
| 23 | return parseAction(data, action); |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | //------------------------------------------------------------------------------ |
| 27 | |
| 28 | void RasDataParser::initDataFiles() |
| 29 | { |
| 30 | iv_dataFiles.clear(); // initially empty |
| 31 | |
| 32 | // Get the RAS data schema files from the package `schema` subdirectory. |
| 33 | fs::path schemaDir{PACKAGE_DIR "schema"}; |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 34 | auto schemaRegex = R"(ras-data-schema-v[0-9]{2}\.json)"; |
| 35 | std::vector<fs::path> schemaPaths; |
| 36 | util::findFiles(schemaDir, schemaRegex, schemaPaths); |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 37 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 38 | // Parse each of the schema files. |
| 39 | std::map<unsigned int, nlohmann::json> schemaFiles; |
| 40 | for (const auto& path : schemaPaths) |
| 41 | { |
| 42 | // Trace each data file for debug. |
| 43 | trace::inf("File found: path=%s", path.string().c_str()); |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 44 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 45 | // Open the file. |
| 46 | std::ifstream file{path}; |
| 47 | assert(file.good()); // The file must be readable. |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 48 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 49 | // Parse the JSON. |
| 50 | auto schema = nlohmann::json::parse(file); |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 51 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 52 | // Get the schema version. |
| 53 | auto version = schema.at("version").get<unsigned int>(); |
| 54 | |
| 55 | // Keep track of the schemas. |
| 56 | auto ret = schemaFiles.emplace(version, schema); |
| 57 | assert(ret.second); // Should not have duplicate entries |
| 58 | } |
| 59 | |
| 60 | // Get the RAS data files from the package `data` subdirectory. |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 61 | fs::path dataDir{PACKAGE_DIR "ras-data"}; |
| 62 | std::vector<fs::path> dataPaths; |
| 63 | util::findFiles(dataDir, R"(.*\.json)", dataPaths); |
| 64 | |
| 65 | // Parse each of the data files. |
| 66 | for (const auto& path : dataPaths) |
| 67 | { |
| 68 | // Trace each data file for debug. |
| 69 | trace::inf("File found: path=%s", path.string().c_str()); |
| 70 | |
| 71 | // Open the file. |
| 72 | std::ifstream file{path}; |
| 73 | assert(file.good()); // The file must be readable. |
| 74 | |
| 75 | // Parse the JSON. |
| 76 | const auto data = nlohmann::json::parse(file); |
| 77 | |
Zane Shelley | ee54c99 | 2021-08-08 17:46:48 -0500 | [diff] [blame] | 78 | // Get the data version. |
| 79 | auto version = data.at("version").get<unsigned int>(); |
| 80 | |
| 81 | // Get the schema for this file. |
| 82 | auto schema = schemaFiles.at(version); |
| 83 | |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 84 | // Validate the data against the schema. |
| 85 | assert(util::validateJson(schema, data)); |
| 86 | |
| 87 | // Get the chip model/EC level from the data. The value is currently |
| 88 | // stored as a string representation of the hex value. So it will have |
| 89 | // to be converted to an integer. |
| 90 | libhei::ChipType_t chipType = |
| 91 | std::stoul(data.at("model_ec").get<std::string>(), 0, 16); |
| 92 | |
| 93 | // So far, so good. Add the entry. |
| 94 | auto ret = iv_dataFiles.emplace(chipType, data); |
| 95 | assert(ret.second); // Should not have duplicate entries |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | //------------------------------------------------------------------------------ |
| 100 | |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 101 | std::string RasDataParser::parseSignature(const nlohmann::json& i_data, |
| 102 | const libhei::Signature& i_signature) |
| 103 | { |
| 104 | // Get the signature keys. All are hex (lower case) with no prefix. |
| 105 | char buf[5]; |
| 106 | sprintf(buf, "%04x", i_signature.getId()); |
| 107 | std::string id{buf}; |
| 108 | |
| 109 | sprintf(buf, "%02x", i_signature.getBit()); |
| 110 | std::string bit{buf}; |
| 111 | |
| 112 | sprintf(buf, "%02x", i_signature.getInstance()); |
| 113 | std::string inst{buf}; |
| 114 | |
| 115 | // Return the action. |
| 116 | return i_data.at("signatures").at(id).at(bit).at(inst).get<std::string>(); |
| 117 | } |
| 118 | |
| 119 | //------------------------------------------------------------------------------ |
| 120 | |
Zane Shelley | 5d63cef | 2021-09-17 18:10:17 -0500 | [diff] [blame] | 121 | std::tuple<callout::BusType, std::string> |
| 122 | RasDataParser::parseBus(const nlohmann::json& i_data, |
| 123 | const std::string& i_name) |
| 124 | { |
| 125 | auto bus = i_data.at("buses").at(i_name); |
| 126 | |
| 127 | // clang-format off |
| 128 | static const std::map<std::string, callout::BusType> m = |
| 129 | { |
| 130 | {"SMP_BUS", callout::BusType::SMP_BUS}, |
| 131 | {"OMI_BUS", callout::BusType::OMI_BUS}, |
| 132 | }; |
| 133 | // clang-format on |
| 134 | |
| 135 | auto busType = m.at(bus.at("type").get<std::string>()); |
| 136 | |
| 137 | std::string unitPath{}; // default empty if unit does not exist |
| 138 | if (bus.contains("unit")) |
| 139 | { |
Zane Shelley | 3e8f295 | 2022-01-18 13:29:30 -0600 | [diff] [blame] | 140 | auto unit = bus.at("unit").get<std::string>(); |
| 141 | unitPath = i_data.at("units").at(unit).get<std::string>(); |
Zane Shelley | 5d63cef | 2021-09-17 18:10:17 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return std::make_tuple(busType, unitPath); |
| 145 | } |
| 146 | |
| 147 | //------------------------------------------------------------------------------ |
| 148 | |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 149 | std::shared_ptr<Resolution> |
| 150 | RasDataParser::parseAction(const nlohmann::json& i_data, |
| 151 | const std::string& i_action) |
| 152 | { |
| 153 | auto o_list = std::make_shared<ResolutionList>(); |
| 154 | |
| 155 | // This function will be called recursively and we want to prevent cyclic |
| 156 | // recursion. |
| 157 | static std::vector<std::string> stack; |
| 158 | assert(stack.end() == std::find(stack.begin(), stack.end(), i_action)); |
| 159 | stack.push_back(i_action); |
| 160 | |
| 161 | // Iterate the action list and apply the changes. |
| 162 | for (const auto& a : i_data.at("actions").at(i_action)) |
| 163 | { |
| 164 | auto type = a.at("type").get<std::string>(); |
| 165 | |
| 166 | if ("action" == type) |
| 167 | { |
| 168 | auto name = a.at("name").get<std::string>(); |
| 169 | |
| 170 | o_list->push(parseAction(i_data, name)); |
| 171 | } |
| 172 | else if ("callout_self" == type) |
| 173 | { |
| 174 | auto priority = a.at("priority").get<std::string>(); |
| 175 | auto guard = a.at("guard").get<bool>(); |
| 176 | |
Zane Shelley | e4bfb47 | 2021-08-10 12:47:32 -0500 | [diff] [blame] | 177 | std::string path{}; // Must be empty to callout the chip. |
| 178 | |
| 179 | o_list->push(std::make_shared<HardwareCalloutResolution>( |
| 180 | path, getPriority(priority), guard)); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 181 | } |
| 182 | else if ("callout_unit" == type) |
| 183 | { |
| 184 | auto name = a.at("name").get<std::string>(); |
| 185 | auto priority = a.at("priority").get<std::string>(); |
| 186 | auto guard = a.at("guard").get<bool>(); |
| 187 | |
Zane Shelley | e4bfb47 | 2021-08-10 12:47:32 -0500 | [diff] [blame] | 188 | auto path = i_data.at("units").at(name).get<std::string>(); |
| 189 | |
| 190 | o_list->push(std::make_shared<HardwareCalloutResolution>( |
| 191 | path, getPriority(priority), guard)); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 192 | } |
| 193 | else if ("callout_connected" == type) |
| 194 | { |
| 195 | auto name = a.at("name").get<std::string>(); |
| 196 | auto priority = a.at("priority").get<std::string>(); |
| 197 | auto guard = a.at("guard").get<bool>(); |
| 198 | |
Zane Shelley | 5d63cef | 2021-09-17 18:10:17 -0500 | [diff] [blame] | 199 | auto busData = parseBus(i_data, name); |
| 200 | |
| 201 | o_list->push(std::make_shared<ConnectedCalloutResolution>( |
| 202 | std::get<0>(busData), std::get<1>(busData), |
| 203 | getPriority(priority), guard)); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 204 | } |
| 205 | else if ("callout_bus" == type) |
| 206 | { |
Zane Shelley | 4757a7b | 2021-09-20 22:23:38 -0500 | [diff] [blame] | 207 | auto name = a.at("name").get<std::string>(); |
| 208 | auto priority = a.at("priority").get<std::string>(); |
| 209 | auto guard = a.at("guard").get<bool>(); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 210 | |
Zane Shelley | 4757a7b | 2021-09-20 22:23:38 -0500 | [diff] [blame] | 211 | auto busData = parseBus(i_data, name); |
| 212 | |
| 213 | o_list->push(std::make_shared<BusCalloutResolution>( |
| 214 | std::get<0>(busData), std::get<1>(busData), |
| 215 | getPriority(priority), guard)); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 216 | } |
| 217 | else if ("callout_clock" == type) |
| 218 | { |
Zane Shelley | 84721d9 | 2021-09-08 13:30:27 -0500 | [diff] [blame] | 219 | auto name = a.at("name").get<std::string>(); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 220 | auto priority = a.at("priority").get<std::string>(); |
| 221 | auto guard = a.at("guard").get<bool>(); |
| 222 | |
Zane Shelley | 84721d9 | 2021-09-08 13:30:27 -0500 | [diff] [blame] | 223 | // clang-format off |
| 224 | static const std::map<std::string, callout::ClockType> m = |
| 225 | { |
| 226 | {"OSC_REF_CLOCK_0", callout::ClockType::OSC_REF_CLOCK_0}, |
| 227 | {"OSC_REF_CLOCK_1", callout::ClockType::OSC_REF_CLOCK_1}, |
| 228 | }; |
| 229 | // clang-format on |
| 230 | |
| 231 | o_list->push(std::make_shared<ClockCalloutResolution>( |
| 232 | m.at(name), getPriority(priority), guard)); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 233 | } |
| 234 | else if ("callout_procedure" == type) |
| 235 | { |
| 236 | auto name = a.at("name").get<std::string>(); |
| 237 | auto priority = a.at("priority").get<std::string>(); |
| 238 | |
| 239 | // clang-format off |
Zane Shelley | c85716c | 2021-08-17 10:54:06 -0500 | [diff] [blame] | 240 | static const std::map<std::string, callout::Procedure> m = |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 241 | { |
Zane Shelley | c85716c | 2021-08-17 10:54:06 -0500 | [diff] [blame] | 242 | {"LEVEL2", callout::Procedure::NEXTLVL}, |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 243 | }; |
| 244 | // clang-format on |
| 245 | |
| 246 | o_list->push(std::make_shared<ProcedureCalloutResolution>( |
| 247 | m.at(name), getPriority(priority))); |
| 248 | } |
| 249 | else if ("callout_part" == type) |
| 250 | { |
| 251 | auto name = a.at("name").get<std::string>(); |
| 252 | auto priority = a.at("priority").get<std::string>(); |
| 253 | |
Zane Shelley | a413477 | 2022-01-10 17:22:44 -0600 | [diff] [blame] | 254 | // clang-format off |
| 255 | static const std::map<std::string, callout::PartType> m = |
| 256 | { |
| 257 | {"PNOR", callout::PartType::PNOR}, |
| 258 | }; |
| 259 | // clang-format on |
| 260 | |
| 261 | o_list->push(std::make_shared<PartCalloutResolution>( |
| 262 | m.at(name), getPriority(priority))); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 263 | } |
| 264 | else if ("plugin" == type) |
| 265 | { |
| 266 | auto name = a.at("name").get<std::string>(); |
Zane Shelley | e13a9f9 | 2021-12-16 21:19:11 -0600 | [diff] [blame] | 267 | auto inst = a.at("instance").get<unsigned int>(); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 268 | |
Zane Shelley | e13a9f9 | 2021-12-16 21:19:11 -0600 | [diff] [blame] | 269 | o_list->push(std::make_shared<PluginResolution>(name, inst)); |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 270 | } |
| 271 | else |
| 272 | { |
| 273 | throw std::logic_error("Unsupported action type: " + type); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Done with this action pop it off the stack. |
| 278 | stack.pop_back(); |
| 279 | |
| 280 | return o_list; |
| 281 | } |
| 282 | |
| 283 | //------------------------------------------------------------------------------ |
| 284 | |
Zane Shelley | c85716c | 2021-08-17 10:54:06 -0500 | [diff] [blame] | 285 | callout::Priority RasDataParser::getPriority(const std::string& i_priority) |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 286 | { |
| 287 | // clang-format off |
Zane Shelley | c85716c | 2021-08-17 10:54:06 -0500 | [diff] [blame] | 288 | static const std::map<std::string, callout::Priority> m = |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 289 | { |
Zane Shelley | c85716c | 2021-08-17 10:54:06 -0500 | [diff] [blame] | 290 | {"HIGH", callout::Priority::HIGH}, |
| 291 | {"MED", callout::Priority::MED}, |
| 292 | {"MED_A", callout::Priority::MED_A}, |
| 293 | {"MED_B", callout::Priority::MED_B}, |
| 294 | {"MED_C", callout::Priority::MED_C}, |
| 295 | {"LOW", callout::Priority::LOW}, |
Zane Shelley | 7698b30 | 2021-08-09 16:00:03 -0500 | [diff] [blame] | 296 | }; |
| 297 | // clang-format on |
| 298 | |
| 299 | return m.at(i_priority); |
| 300 | } |
| 301 | |
| 302 | //------------------------------------------------------------------------------ |
| 303 | |
Zane Shelley | a9b4434 | 2021-08-08 17:15:52 -0500 | [diff] [blame] | 304 | } // namespace analyzer |