qualcomm/qbox
Loading...
Searching...
No Matches
reg_model_maker.h
1/*
2 * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _REG_MODEL_MAKER_H
8#define _REG_MODEL_MAKER_H
9
10#include <systemc>
11#include <cci_configuration>
12#include <scp/report.h>
13#include <rapidjson/include/rapidjson/document.h>
14#include <cciutils.h>
15#include <zip_loader.h>
16#include <gs_memory.h>
17#include <router.h>
18#include <reg_router.h>
19#include <registers.h>
20#include <tlm_sockets_buswidth.h>
21#include <ports/initiator-signal-socket.h>
22#include <cstdlib>
23#include <set>
24#include <string>
25#include <memory>
26#include <vector>
27#include <limits>
28#include <map>
29#include <list>
30#include <algorithm>
31
32namespace gs {
33
34class json_zip_archive
35{
36 SCP_LOGGER((), "json_zip_archive");
37
38 std::set<std::string> m_loaded;
39 std::vector<uint8_t> m_bin_file_data;
40 zip_t* m_archive;
41 bool m_base_address_captured;
42 uint64_t m_base_address;
43 uint64_t m_parent_model_base_addr;
44 bool m_is_platform_top;
45 bool m_is_platform_model;
46 uint64_t m_max_size;
47 std::shared_ptr<gs::router<>> m_router;
48 std::shared_ptr<gs::gs_memory<>> m_memory;
49 std::string m_archive_name;
50
51public:
52 json_zip_archive(zip_t* archive);
53
54 zip_t* get_zip_archive_ptr();
55
56 std::string get_archive_name();
57
58 uint64_t getNumber(rapidjson::Value& v)
59 {
60 if (v.IsNumber()) {
61 return v.GetUint64();
62 }
63 if (v.IsString()) {
64 const char* p = v.GetString();
65 return std::strtoull(p, nullptr, 0);
66 }
67 SCP_FATAL(())("JSON Number neither number, nor string!");
68 return 0;
69 }
70
71 std::vector<std::string> get_file_names_in_zip_archive();
72
73 std::string get_json_topname();
74
75 std::string get_bin_file_name();
76
77 uint64_t get_bin_file_size();
78
79 rapidjson::Document& get_json(std::string file, rapidjson::Document& d);
80
81private:
82 template <class T>
83 void cci_set(cci::cci_broker_handle broker, std::string n, T value)
84 {
85 if (!broker.has_preset_value(n)) {
86 SCP_DEBUG(())("set {} to {}", n, value);
87 broker.set_preset_cci_value(n, cci::cci_value(value));
88 } else {
89 SCP_FATAL(())("Trying to re-set a value? {}", n);
90 }
91 }
92
93public:
94 bool json_read_cci(cci::cci_broker_handle broker, std::string sc_name, bool is_platform_model = false,
95 std::shared_ptr<gs::router<>> _router = nullptr,
96 std::shared_ptr<gs::gs_memory<>> _memory = nullptr, bool is_platform = false,
97 std::string _path = "", rapidjson::Value* _node = nullptr, bool top = true)
98 {
99 m_is_platform_model = is_platform_model;
100 m_is_platform_top = is_platform;
101 std::string module_path = _path.empty() ? get_json_topname() : _path;
102
103 rapidjson::Document json_doc;
104 rapidjson::Value& node = _node ? *_node : get_json(module_path + ".json", json_doc);
105
106 if (node.IsNull()) return false;
107
108 sc_name.erase(remove_if(sc_name.begin(), sc_name.end(), isspace), sc_name.end());
109 if (m_loaded.count(sc_name)) return true;
110 m_loaded.insert(sc_name);
111
112 SCP_INFO(())
113 ("JSON read into sc object {} from JSON node {}", sc_name, node["NAME"].GetString());
114
115 if (node.HasMember("ADDRESS") && !m_base_address_captured) {
116 m_router = _router;
117 m_memory = _memory;
118 uint64_t bin_file_size = get_bin_file_size();
119 uint64_t size = 0;
120 if (node.HasMember("SIZE")) size = getNumber(node["SIZE"]);
121 m_max_size = std::max(size, bin_file_size);
122 SCP_TRACE(())("Setting {}.reg_memory", sc_name);
123 m_base_address = getNumber(node["ADDRESS"]);
124 m_parent_model_base_addr = m_base_address;
125 if (!is_platform) {
126 std::string parent_mod_name = sc_name.substr(0, sc_name.find_last_of('.'));
127 if (broker.has_preset_value(parent_mod_name + ".target_socket.address")) {
128 broker.get_preset_cci_value(parent_mod_name + ".target_socket.address")
129 .template try_get<uint64_t>(m_parent_model_base_addr);
130 } else {
131 SCP_WARN(())
132 ("{} is not connected to a router, or not configured properly", parent_mod_name);
133 }
134 }
135 m_base_address_captured = true;
136 }
137 if (node.HasMember("MODULES")) {
138 auto mods = node["MODULES"].GetArray();
139 for (auto& mod : mods) {
140 if (mod.IsObject()) {
141 if (!mod.HasMember("INST_HIER")) {
142 SCP_FATAL(())
143 ("Missing INST_HIER entry while parsing JSON config file in archive: {}", get_archive_name());
144 }
145 std::string mod_hier_name = mod["INST_HIER"].GetString();
146 std::string mod_name = mod["NAME"].GetString();
147 std::string sn = sc_name + "." +
148 (is_platform ? mod_hier_name.substr(mod_hier_name.find_last_of(".") + 1)
149 : mod_name);
150 json_read_cci(broker, sn, is_platform_model, _router, _memory, is_platform, module_path, &mod,
151 false);
152 if (mod.HasMember("OFFSET")) {
153 cci_set(broker, sn + ".target_socket.chained", false);
154 }
155 }
156 }
157 }
158 if (node.HasMember("MEMORY") && !is_platform_model) {
159 uint64_t address = getNumber(node["ADDRESS"]);
160 auto mems = node["MEMORY"].GetArray();
161 for (auto& m : mems) {
162 std::string sn = sc_name + "." + m["NAME"].GetString();
163 uint64_t mem_address = 0;
164 if (m.HasMember("OFFSET"))
165 mem_address = getNumber(m["OFFSET"]) + (is_platform ? address : 0);
166 else
167 mem_address = getNumber(m["ADDRESS"]) + (is_platform ? address : 0);
168 uint64_t mem_size = (m.HasMember("SIZE") ? getNumber(m["SIZE"]) : 0);
169 m_max_size = std::max(m_max_size, (mem_address + mem_size - (is_platform ? m_base_address : 0)));
170 cci_set(broker, sn + ".target_socket.address", mem_address);
171 cci_set(broker, sn + ".target_socket.size", mem_size);
172 cci_set(broker, sn + ".target_socket.relative_addresses", true);
173 cci_set(broker, sn + ".target_socket.priority", 0);
174 }
175 }
176 if (node.HasMember("ADDRESS")) {
177 uint64_t mod_size = 0;
178 uint64_t mod_address = getNumber(node["ADDRESS"]);
179 if (node.HasMember("SIZE")) mod_size = getNumber(node["SIZE"]);
180 m_max_size = std::max(m_max_size, (mod_address + mod_size - m_base_address));
181 cci_set(broker, sc_name + ".target_socket.address", mod_address);
182 cci_set(broker, sc_name + ".target_socket.size", mod_size);
183 cci_set(broker, sc_name + ".target_socket.relative_addresses", true);
184 if (!is_platform) cci_set(broker, sc_name + ".router.lazy_init", true);
185 }
186 if (top) {
187 if (!is_platform_model && !is_platform) {
188 cci_set(broker, sc_name + ".reg_memory.target_socket.address", 0);
189 cci_set(broker, sc_name + ".reg_memory.target_socket.size", m_max_size);
190 cci_set(broker, sc_name + ".reg_memory.target_socket.relative_addresses", true);
191 }
192 cci_set(broker, sc_name + ".reg_router.target_socket.address",
193 (is_platform_model || is_platform) ? m_parent_model_base_addr : 0);
194 cci_set(broker, sc_name + ".reg_router.target_socket.size", m_max_size);
195 cci_set(broker, sc_name + ".reg_router.target_socket.priority", 1024);
196 cci_set(broker, sc_name + ".reg_router.target_socket.relative_addresses", true);
197 cci_set(broker, sc_name + ".reg_router.lazy_init", true);
198 }
199 return true;
200 }
201
202 uint64_t get_base_address() const { return m_base_address; }
203 uint64_t get_parent_module_address() { return m_parent_model_base_addr; }
204 bool is_platform_top() { return m_is_platform_top; }
205 bool is_platform_model() { return m_is_platform_model; }
206 void set_memory(std::shared_ptr<gs::gs_memory<>> _memory) { m_memory = _memory; }
207 void set_router(std::shared_ptr<gs::router<>> _router) { m_router = _router; }
208 std::shared_ptr<gs::gs_memory<>> get_memory() { return m_memory; }
209 std::shared_ptr<gs::router<>> get_router() { return m_router; }
210};
211
212class base_components
213{
214 SCP_LOGGER((), "json_module_base_components");
215
216public:
217 base_components(const std::string& name, json_zip_archive& jza, cci::cci_broker_handle broker,
218 gs::router<>* _router, gs::gs_memory<>* _reg_memory)
219 : m_router(nullptr)
220 , m_reg_memory(nullptr)
221 , m_reg_router(nullptr)
222 , m_reset("reset")
223 , m_is_platform_mod(_router && _reg_memory)
224 , m_is_platform_top(jza.is_platform_top())
225 {
226 SCP_LOGGER_NAME().features[0] = "json_module_base_components";
227 SCP_TRACE(())("base_components Constructor");
228 if (!m_is_platform_mod) {
229 // use raw pointer as m_router & m_reg_memory may not be created/managed by base_components
230 if (!m_is_platform_top) {
231 m_reg_memory = std::make_shared<gs::gs_memory<>>("reg_memory");
232 m_router = std::make_shared<gs::router<>>("router");
233 m_reset.bind(m_reg_memory->reset);
234 bin_file_name = jza.get_bin_file_name();
235 zip_archive_name = jza.get_archive_name();
236 zip_archive_ptr = jza.get_zip_archive_ptr();
237 m_reg_memory->load.zip_file_load(zip_archive_ptr, zip_archive_name, 0, bin_file_name);
238 } else {
239 m_router = jza.get_router();
240 m_reg_memory = jza.get_memory();
241 }
242 } else {
243 std::string mod_name = std::string(_router->name());
244 std::string container_name = mod_name.substr(0, mod_name.find_last_of("."));
246 gs::find_sc_obj(nullptr, container_name));
247 if (!container) {
248 SCP_ERR(())
249 ("parent module: {} is not a container)", container_name);
250 }
251 m_router = std::dynamic_pointer_cast<gs::router<>>(
252 container->find_module_by_name(std::string(_router->name())));
253 m_reg_memory = std::dynamic_pointer_cast<gs::gs_memory<>>(
254 container->find_module_by_name(std::string(_reg_memory->name())));
255 }
256 sc_assert(m_router);
257 sc_assert(m_reg_memory);
258 }
259 void insert_mod_addr_name_pair(const std::pair<uint64_t, std::pair<uint64_t, std::string>>& item)
260 {
261 m_mod_addr_name_map.insert(item);
262 }
263 void init_reg_router()
264 {
265 if (m_reg_memory && !m_reg_router) {
266 m_reg_router = std::make_unique<gs::reg_router<>>("reg_router", m_mod_addr_name_map);
267 m_reg_router->initiator_socket(m_reg_memory->socket);
268 if (!m_is_platform_mod) m_router->initiator_socket(m_reg_router->target_socket);
269 }
270 }
271 ~base_components() {}
272 void reset(bool value)
273 {
274 if (m_reg_memory && !m_is_platform_mod && !m_is_platform_top) {
275 m_reset->write(value);
276 if (value) {
277 m_reg_memory->load.zip_file_load(zip_archive_ptr, zip_archive_name, 0, bin_file_name);
278 }
279 }
280 }
281
282 std::shared_ptr<gs::router<>> m_router;
283 std::shared_ptr<gs::gs_memory<>> m_reg_memory;
284 std::unique_ptr<gs::reg_router<>> m_reg_router;
285
286private:
287 InitiatorSignalSocket<bool> m_reset;
288 std::map<uint64_t, std::pair<uint64_t, std::string>> m_mod_addr_name_map;
289 bool m_is_platform_mod;
290 bool m_is_platform_top;
291 std::string bin_file_name;
292 std::string zip_archive_name;
293 zip_t* zip_archive_ptr;
294};
295
296class json_module_base : public virtual sc_core::sc_module
297{
298 SCP_LOGGER();
299
300protected:
301 json_zip_archive& m_jza;
302 cci::cci_broker_handle m_broker;
303 std::shared_ptr<base_components> m_sh_comp;
304 sc_core::sc_vector<gs::gs_memory<>> memories;
305 sc_core::sc_vector<json_module_base> sub_modules;
306 uint32_t m_bound_regs_num;
307 bool m_is_platform_mod;
308
309protected:
310 template <class T>
311 void cci_set(std::string n, T value)
312 {
313 auto handle = m_broker.get_param_handle(n);
314 if (handle.is_valid())
315 handle.set_cci_value(cci::cci_value(value));
316 else if (!m_broker.has_preset_value(n)) {
317 m_broker.set_preset_cci_value(n, cci::cci_value(value));
318 } else {
319 SCP_FATAL(())("Trying to re-set a value? {}", n);
320 }
321 }
322
323 template <typename T>
324 T cci_get(std::string n)
325 {
326 T ret;
327 if (!m_broker.get_preset_cci_value(n).template try_get<T>(ret)) {
328 SCP_FATAL(()) << "Unable to get parameter " << n << "\nIs your .lua file up-to-date?";
329 };
330 m_broker.lock_preset_value(n);
331 return ret;
332 }
333
334public:
335 ~json_module_base() = default;
336
337 json_module_base(sc_core::sc_module_name _name, json_zip_archive& jza, gs::router<>* _router = nullptr,
338 gs::gs_memory<>* _reg_memory = nullptr, const std::shared_ptr<base_components>& sh_comp = nullptr,
339 rapidjson::Value* _node = nullptr, std::string _path = "", bool top = true)
340 : sc_core::sc_module(_name)
341 , m_jza(jza)
342 , m_broker(cci::cci_get_broker())
343 , m_sh_comp(sh_comp)
344 , m_bound_regs_num(0)
345 , m_is_platform_mod(false)
346 {
347 SCP_TRACE(())("Constructor of {}", sc_core::sc_module::name());
348 std::string n = std::string(sc_core::sc_module::name());
349 std::string module_path = _path.empty() ? jza.get_json_topname() : _path;
350 rapidjson::Document json_doc;
351 rapidjson::Value& node = _node ? *_node : jza.get_json(module_path + ".json", json_doc);
352
353 if (!m_sh_comp) {
354 m_is_platform_mod = jza.is_platform_model();
355 if (m_is_platform_mod && !_router && !_reg_memory) {
356 SCP_FATAL(())
357 ("Platform model is instantiated without router and reg_memory arguments, is your conf.lua updated?");
358 }
359 m_sh_comp = std::make_shared<base_components>(std::string(sc_core::sc_module::name()), jza, m_broker,
360 _router, _reg_memory);
361 }
362 if (m_sh_comp && jza.is_platform_top() && node.HasMember("ADDRESS") && node.HasMember("SIZE") &&
363 node.HasMember("INST_HIER")) {
364 std::string mod_hier_name = node["INST_HIER"].GetString();
365 std::string mod_name = mod_hier_name.substr(mod_hier_name.find_last_of(".") + 1);
366 m_sh_comp->insert_mod_addr_name_pair(std::make_pair<uint64_t, std::pair<uint64_t, std::string>>(
367 (jza.getNumber(node["ADDRESS"]) - jza.get_base_address()) +
368 (m_is_platform_mod ? jza.get_parent_module_address() : 0),
369 std::make_pair<uint64_t, std::string>(jza.getNumber(node["SIZE"]), std::move(mod_name))));
370 }
371 if (!m_is_platform_mod && node.HasMember("MEMORY")) {
372 auto mems = node["MEMORY"].GetArray();
373 memories.init(mems.Size(), [&, mems](const char* name, size_t n) -> gs::gs_memory<>* {
374 return new gs::gs_memory<>(mems[n]["NAME"].GetString());
375 });
376 }
377 for (auto& m : memories) {
378 m_sh_comp->m_router->initiator_socket(m.socket);
379 }
380 if (node.HasMember("MODULES")) {
381 auto mods = node["MODULES"].GetArray();
382 sub_modules.init(mods.Size(), [&, mods](const char* name, size_t n) -> json_module_base* {
383 if (!mods[n].IsObject()) {
384 SCP_FATAL(())("JSON file for json_module_base must be fully self contained");
385 }
386 std::string mod_hier_name = mods[n]["INST_HIER"].GetString();
387 std::string mod_name = mods[n]["NAME"].GetString();
388 mod_hier_name.erase(remove_if(mod_hier_name.begin(), mod_hier_name.end(), isspace),
389 mod_hier_name.end());
390 mod_name.erase(remove_if(mod_name.begin(), mod_name.end(), isspace), mod_name.end());
391 std::string used_mod_name = (jza.is_platform_top()
392 ? mod_hier_name.substr(mod_hier_name.find_last_of(".") + 1)
393 : mod_name);
394 return new json_module_base(used_mod_name.c_str(), jza, _router, _reg_memory, m_sh_comp, &mods[n],
395 module_path, false);
396 });
397 }
398 if (top) {
399 m_sh_comp->init_reg_router();
400 }
401 }
402
403 template <class TYPE>
404 void bind_reg(gs::gs_register<TYPE>& reg, uint32_t priority = 0)
405 {
406 std::string path = reg.get_path();
407 std::string self_full_name = std::string(sc_core::sc_module::name());
408 std::string self_name = self_full_name.substr(self_full_name.find_last_of(".") + 1);
409 std::string parent = path.substr(self_name.length() + 1);
410 if (parent == reg.get_regname()) {
411 parent = "";
412 } else {
413 parent = parent.substr(0, parent.find_last_of("."));
414 }
415 SCP_TRACE(())
416 ("Binding {} in {}", reg.get_regname(),
417 std::string(sc_core::sc_module::name()) + ((!parent.empty()) ? "." + parent : ""));
418 reg.initiator_socket.bind(m_sh_comp->m_reg_memory->socket);
419 std::string mod_address_str = std::string(sc_core::sc_module::name()) + "." +
420 ((!parent.empty()) ? parent + "." : "") + "target_socket.address";
421 std::string reg_target_socket_name = std::string(this->name()) + "." + ((!parent.empty()) ? parent + "." : "") +
422 reg.get_regname() + ".target_socket";
423 uint64_t parent_address = m_jza.get_parent_module_address();
424 uint64_t mod_address = cci_get<uint64_t>(mod_address_str) - m_jza.get_base_address();
425 std::string reg_address_str = reg_target_socket_name + ".address";
426 uint64_t new_reg_offset = reg.get_offset() + mod_address + (m_jza.is_platform_model() ? parent_address : 0);
427 cci_set<uint64_t>(reg_address_str, new_reg_offset);
428 if constexpr (gs::is_gs_bank<TYPE>::value) {
429 auto encode_dims = [](const std::vector<uint64_t>& values) {
430 std::string result;
431 for (size_t i = 0; i < values.size(); ++i) {
432 if (i != 0) result += ',';
433 result += std::to_string(values[i]);
434 }
435 return result;
436 };
437 cci_set<std::string>(reg_target_socket_name + ".dim_strides", encode_dims(reg.get_dim_strides()));
438 cci_set<std::string>(reg_target_socket_name + ".dim_counts", encode_dims(reg.get_dim_counts()));
439 }
440 if (priority != 0) cci_set<uint32_t>(reg_target_socket_name + ".priority", priority);
441 m_sh_comp->m_reg_router->initiator_socket.bind(reg);
442 m_sh_comp->m_reg_router->rename_last(reg_target_socket_name);
443 m_bound_regs_num++;
444 }
445
446 uint32_t get_num_of_bound_regs() const { return m_bound_regs_num; }
447
448 void log_end_of_binding_msg(const std::string& gen_name)
449 {
450 SCP_DEBUG(()) << gen_name << ": Constructed and bound " << m_bound_regs_num << " registers.";
451 }
452
453 void reset(bool value) { m_sh_comp->reset(value); }
454};
455
456class json_module : public json_module_base
457{
458public:
459 tlm_utils::multi_passthrough_target_socket<json_module, DEFAULT_TLM_BUSWIDTH> target_socket;
460
461 ~json_module() = default;
462
463 json_module(sc_core::sc_module_name _name, json_zip_archive& jza, gs::router<>* _router = nullptr,
464 gs::gs_memory<>* _reg_memory = nullptr, const std::shared_ptr<base_components>& sh_comp = nullptr,
465 rapidjson::Value* _node = nullptr, std::string _path = "", bool top = true)
466 : json_module_base(_name, jza, _router, _reg_memory, sh_comp, _node, _path, top), target_socket("target_socket")
467 {
468 if (!_router && !_reg_memory) {
469 target_socket.bind(m_sh_comp->m_router->target_socket);
470 } else {
471 target_socket.bind(m_sh_comp->m_reg_router->target_socket);
472 }
473 }
474};
475
476class json_module_top : public json_module_base
477{
478private:
479 bool is_json_loaded_ok(json_zip_archive& jza, cci::cci_broker_handle broker, std::string sc_name,
480 std::shared_ptr<gs::router<>> _router, std::shared_ptr<gs::gs_memory<>> _memory,
481 std::string _path, rapidjson::Value* _node)
482 {
483 bool loaded_ok = jza.json_read_cci(broker, sc_name, false, _router, _memory, true, _path, _node, true);
484 sc_assert(loaded_ok);
485 return true;
486 }
487
488public:
489 ~json_module_top() = default;
490
491 json_module_top(sc_core::sc_module_name _name, json_zip_archive& jza, cci::cci_broker_handle broker,
492 std::string sc_name, std::shared_ptr<gs::router<>> _router = nullptr,
493 std::shared_ptr<gs::gs_memory<>> _memory = nullptr, std::string _path = "",
494 rapidjson::Value* _node = nullptr)
495 : json_module_base(_name, jza, nullptr, nullptr, nullptr, nullptr, "",
496 is_json_loaded_ok(jza, broker, sc_name, _router, _memory, _path, _node))
497 {
498 }
499};
500
501} // namespace gs
502
503#define TXN(T) tlm::tlm_generic_payload &FIRST_ARG(T, _txn), sc_core::sc_time &delay
504
505#endif // _REG_MODEL_MAKER_H
Definition module_factory_container.h:53
A gs_memory component that can add memory to a virtual platform project.
Definition gs_memory.h:59
Class that encapsulates a 'register' that proxies it's data via a tlm interface, and uses callbacks (...
Definition registers.h:556
Definition reg_model_maker.h:35
A SystemC TLM router module for transaction routing based on address.
Definition router.h:106
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10
sc_core::sc_object * find_sc_obj(sc_core::sc_object *m, std::string name, bool test=false)
Helper function to find a sc_object by fully qualified name throws SC_ERROR if nothing found.
Definition cciutils.cc:18