33class reg_router :
public sc_core::sc_module,
public gs::router_if<BUSWIDTH>
36 SCP_LOGGER((DMI),
"dmi");
40 reg_router<BUSWIDTH>>;
43 void register_boundto(std::string s)
override
45 s = gs::router_if<BUSWIDTH>::nameFromSocket(s);
46 std::shared_ptr<target_info> ti_ptr = std::make_shared<target_info>();
48 ti_ptr->index = bound_targets.size();
49 SCP_DEBUG(()) <<
"Connecting : " << ti_ptr->name;
52 for (i = 0; i < s.length(); i++)
53 if (s[i] != tmp[i])
break;
54 ti_ptr->shortname = s.substr(i);
55 bound_targets.push_back(ti_ptr);
59 void b_transport(
int id, tlm::tlm_generic_payload& trans, sc_core::sc_time& delay)
61 sc_dt::uint64 addr = trans.get_address();
63 std::shared_ptr<target_info> ti = decode_address(trans);
66 SCP_WARN(())(
"Attempt to access unknown location in register memory at offset 0x{:x}", addr);
67 trans.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
71#ifdef ENABLE_MODULE_NAME_DBG_INFO
72 uint64_t mod_addr = 0;
73 bool mod_found =
false;
74 const auto& mod_name = get_module_name(trans, mod_addr, mod_found);
75 if (!mod_name.empty())
76 SCP_DEBUG(()) <<
"b_transport: " << txn_command_str(trans) <<
" to Register at address: 0x" << std::hex
77 << trans.get_address() <<
" which belongs to Module: [" << mod_name <<
"]";
78 if (m_pre_b_transport_callback) m_pre_b_transport_callback(mod_found, mod_addr);
81 bool found_cb = do_callbacks(trans, delay);
82 if (trans.get_response_status() >= tlm::TLM_INCOMPLETE_RESPONSE) {
83 SCP_TRACEALL(()) <<
"call b_transport: " << txn_to_str(trans,
false, found_cb);
84 if (ti->use_offset) trans.set_address(addr - ti->address);
85 initiator_socket[ti->index]->b_transport(trans, delay);
86 if (ti->use_offset) trans.set_address(addr);
87 SCP_TRACEALL(()) <<
"b_transport returned: " << txn_to_str(trans,
false, found_cb);
89 if (trans.get_response_status() >= tlm::TLM_OK_RESPONSE) {
90 do_callbacks(trans, delay);
92 if (cb_targets.size() > 0) {
93 trans.set_dmi_allowed(
false);
97 unsigned int transport_dbg(
int id, tlm::tlm_generic_payload& trans)
99 sc_dt::uint64 addr = trans.get_address();
101 std::shared_ptr<target_info> ti = decode_address(trans);
104 SCP_WARN(())(
"transport_dbg: Attempt to access unknown location in register memory at offset 0x{:x}", addr);
105 trans.set_response_status(tlm::TLM_ADDRESS_ERROR_RESPONSE);
109#ifdef ENABLE_MODULE_NAME_DBG_INFO
110 uint64_t mod_addr = 0;
111 bool mod_found =
false;
112 const auto& mod_name = get_module_name(trans, mod_addr, mod_found);
113 if (!mod_name.empty())
114 SCP_DEBUG(()) <<
"transport_dbg: " << txn_command_str(trans) <<
" to Register at address: 0x" << std::hex
115 << trans.get_address() <<
" which belongs to Module: [" << mod_name <<
"]";
116 if (m_pre_b_transport_callback) m_pre_b_transport_callback(mod_found, mod_addr);
119 if (ti->use_offset) trans.set_address(addr - ti->address);
120 SCP_TRACEALL(()) <<
"[REG-MEM] call transport_dbg [txn]: " << scp::scp_txn_tostring(trans);
121 unsigned int ret = initiator_socket[ti->index]->transport_dbg(trans);
122 if (ti->use_offset) trans.set_address(addr);
126 bool get_direct_mem_ptr(
int id, tlm::tlm_generic_payload& trans, tlm::tlm_dmi& dmi_data)
128 SCP_TRACE(()) <<
"[REG-MEM] call get_direct_mem_ptr (not allowed) [txn]: " << scp::scp_txn_tostring(trans);
132 std::string txn_to_str(tlm::tlm_generic_payload& trans,
bool is_callback =
false,
bool found_callback =
false)
134 std::stringstream ss;
135 std::string cmd = txn_command_str(trans);
136 if (is_callback && found_callback) {
137 if (cmd !=
"IGNORE" && cmd !=
"UNKNOWN") {
138 if ((trans.get_response_status() == tlm::TLM_INCOMPLETE_RESPONSE))
139 ss <<
"[PRE-" << cmd <<
"] Register callbacks [txn]: ";
140 else if ((trans.get_response_status() == tlm::TLM_OK_RESPONSE))
141 ss <<
"[POST-" << cmd <<
"] Register callbacks [txn]: ";
143 }
else if (found_callback) {
144 ss <<
"[REG-MEM] (" << cmd <<
" TO REG-MEM BEFORE " << cmd <<
" CB) [txn]: ";
146 ss <<
"[REG-MEM] (NO CB FOUND) [txn]: ";
149 ss << scp::scp_txn_tostring(trans);
153 std::string txn_command_str(
const tlm::tlm_generic_payload& trans)
156 switch (trans.get_command()) {
157 case tlm::TLM_READ_COMMAND:
160 case tlm::TLM_WRITE_COMMAND:
163 case tlm::TLM_IGNORE_COMMAND:
173#ifdef ENABLE_MODULE_NAME_DBG_INFO
174 const std::string& get_module_name(
const tlm::tlm_generic_payload& trans, uint64_t& mod_addr,
bool& mod_found)
176 static const std::string empty;
177 if (mod_addr_name_map.empty()) {
180 uint64_t addr = trans.get_address();
181 auto search = mod_addr_name_map.equal_range(addr);
182 if (search.first != mod_addr_name_map.end() && search.first->first == addr) {
183 if ((addr - search.first->first) < search.first->second.first) {
184 mod_addr = search.first->first;
186 return search.first->second.second;
188 }
else if (search.second != mod_addr_name_map.begin()) {
189 auto expected_node = std::prev(search.second);
190 if ((addr - expected_node->first) < expected_node->second.first) {
191 mod_addr = expected_node->first;
193 return expected_node->second.second;
207 bool do_callbacks(tlm::tlm_generic_payload& trans, sc_core::sc_time& delay)
209 if (cb_targets.empty()) {
213 sc_dt::uint64 addr = trans.get_address();
215 std::vector<std::shared_ptr<target_info>> claimants;
216 uint32_t best_prio = 0;
217 for (
auto& kv : cb_targets) {
218 const auto& cand = kv.second;
219 if (!cand->claims(addr))
continue;
220 if (claimants.empty() || cand->priority < best_prio) {
222 claimants.push_back(cand);
223 best_prio = cand->priority;
224 }
else if (cand->priority == best_prio) {
225 claimants.push_back(cand);
229 if (claimants.empty())
return false;
231 for (
const auto& ti : claimants) {
232 SCP_TRACEALL(()) <<
"call b_transport (fan-out prio " << best_prio
233 <<
"): " << txn_to_str(trans,
true,
true);
234 if (ti->use_offset) trans.set_address(addr - ti->address);
235 initiator_socket[ti->index]->b_transport(trans, delay);
236 if (ti->use_offset) trans.set_address(addr);
237 SCP_TRACEALL(()) <<
"b_transport returned : " << txn_to_str(trans,
true,
true);
238 if (trans.get_response_status() <= tlm::TLM_GENERIC_ERROR_RESPONSE) {
239 SCP_WARN(()) <<
"Accesing register callback at addr: " << std::hex << addr <<
" (target " << ti->name
240 <<
") returned with: " << trans.get_response_string();
247 std::shared_ptr<target_info> decode_address(tlm::tlm_generic_payload& trans)
override
251 sc_dt::uint64 addr = trans.get_address();
253 std::shared_ptr<target_info> best;
254 for (
auto& ti : mem_targets) {
255 if (!ti->claims(addr))
continue;
256 if (!best || ti->priority < best->priority)
258 else if (ti->priority == best->priority && ti != best)
259 SCP_FATAL(()) <<
"Two targets with equal priority both claim address 0x" << std::hex << addr;
265 virtual void before_end_of_elaboration()
override
267 if (!lazy_init) lazy_initialize();
271 static std::vector<sc_dt::uint64> parse_u64_csv(
const std::string& s)
273 std::vector<sc_dt::uint64> out;
275 while (i < s.size()) {
276 while (i < s.size() && (s[i] ==
' ' || s[i] ==
',')) ++i;
277 if (i >= s.size())
break;
279 sc_dt::uint64 v = std::stoull(s.substr(i), &consumed, 0);
288 static bool banks_element_overlap(
const std::shared_ptr<target_info>& a,
const std::shared_ptr<target_info>& b)
290 const auto& s = a->dim_strides.empty() ? std::vector<sc_dt::uint64>{ a->stride } : a->dim_strides;
291 const auto& c = a->dim_counts.empty() ? std::vector<sc_dt::uint64>{ 1 } : a->dim_counts;
292 std::vector<size_t> idx(s.size(), 0);
294 sc_dt::uint64 off = 0;
295 for (
size_t k = 0; k < s.size(); k++) off += idx[k] * s[k];
296 for (sc_dt::uint64
byte = 0;
byte < a->elem_size;
byte++) {
297 if (b->claims(a->address + off +
byte))
return true;
301 if (++idx[k] < c[k])
break;
303 if (k == 0)
return false;
305 if (k == (
size_t)-1)
return false;
309 void lazy_initialize()
override
311 if (initialized)
return;
314 for (
auto& ti_ptr : bound_targets) {
315 std::string name = ti_ptr->name;
318 ti_ptr->address = gs::cci_get<uint64_t>(m_broker, name +
".address");
319 ti_ptr->size = gs::cci_get<uint64_t>(m_broker, name +
".size");
320 ti_ptr->use_offset = gs::cci_get_d<bool>(m_broker, name +
".relative_addresses",
true);
321 ti_ptr->is_callback = gs::cci_get_d<bool>(m_broker, name +
".is_callback",
false);
322 ti_ptr->priority = gs::cci_get_d<uint32_t>(m_broker, name +
".priority", 0);
323 ti_ptr->stride = gs::cci_get_d<uint64_t>(m_broker, name +
".stride", 0);
324 ti_ptr->elem_size = gs::cci_get_d<uint64_t>(m_broker, name +
".elem_size", 0);
326 std::string strides_str = gs::cci_get_d<std::string>(m_broker, name +
".dim_strides", std::string());
327 std::string counts_str = gs::cci_get_d<std::string>(m_broker, name +
".dim_counts", std::string());
328 if (!strides_str.empty() && !counts_str.empty()) {
329 ti_ptr->dim_strides = parse_u64_csv(strides_str);
330 ti_ptr->dim_counts = parse_u64_csv(counts_str);
331 if (ti_ptr->dim_strides.size() != ti_ptr->dim_counts.size())
332 SCP_FATAL(()) << name <<
" has mismatched dim_strides / dim_counts";
335 SCP_INFO(()) <<
"Address map " << ti_ptr->name +
" at" <<
" address " <<
"0x" << std::hex << ti_ptr->address
336 <<
" size " <<
"0x" << std::hex << ti_ptr->size
337 << (ti_ptr->stride ?
" stride 0x" + std::to_string(ti_ptr->stride) :
"")
338 << (ti_ptr->dim_strides.empty() ?
"" : (
" dims=" + std::to_string(ti_ptr->dim_strides.size())))
339 << (ti_ptr->use_offset ?
" (with relative address) " :
"")
340 << (ti_ptr->is_callback ?
" (callback) " :
"");
342 if (ti_ptr->is_callback) {
343 cb_targets.insert(std::make_pair(ti_ptr->address, ti_ptr));
345 if (!mem_targets.empty()) {
346 for (
const auto& mem : mem_targets) {
347 bool overlaps_range = ((ti_ptr->address >= mem->address) &&
348 ((ti_ptr->address - mem->address) < mem->size)) ||
349 ((ti_ptr->address < mem->address) &&
350 ((ti_ptr->address + ti_ptr->size) > mem->address));
351 if (!overlaps_range)
continue;
353 bool ti_gapped = ti_ptr->stride != 0 || !ti_ptr->dim_strides.empty();
354 bool mem_gapped = mem->stride != 0 || !mem->dim_strides.empty();
355 if (!ti_gapped || !mem_gapped) {
356 SCP_WARN(()) << ti_ptr->name <<
" overlaps with " << mem->name;
357 }
else if (banks_element_overlap(ti_ptr, mem)) {
358 SCP_WARN(()) << ti_ptr->name <<
" element footprint overlaps with " << mem->name;
362 mem_targets.push_back(ti_ptr);
369 const sc_core::sc_module_name& nm,
370 const std::map<uint64_t, std::pair<uint64_t, std::string>>& p_mod_addr_name_map =
371 std::map<uint64_t, std::pair<uint64_t, std::string>>(),
372 const std::function<
void(
bool, uint64_t)>& pre_b_transport_callback = std::function<
void(
bool, uint64_t)>(),
373 cci::cci_broker_handle broker = cci::cci_get_broker())
374 : sc_core::sc_module(nm)
375 , initiator_socket(
"initiator_socket", [&](std::string s) ->
void { register_boundto(s); })
376 , target_socket(
"target_socket")
378 , lazy_init(
"lazy_init",
false,
"Initialize the reg_router lazily (eg. during simulation rather than BEOL)")
379 , mod_addr_name_map(p_mod_addr_name_map)
380 , m_pre_b_transport_callback(pre_b_transport_callback)
382 SCP_DEBUG(()) <<
"reg_router constructed";
384 target_socket.register_b_transport(
this, ®_router::b_transport);
385 target_socket.register_transport_dbg(
this, ®_router::transport_dbg);
386 target_socket.register_get_direct_mem_ptr(
this, ®_router::get_direct_mem_ptr);
388 SCP_DEBUG((DMI)) <<
"reg_router Initializing DMI SCP reporting";
391 reg_router() =
delete;
393 reg_router(
const reg_router&) =
delete;
395 ~reg_router() =
default;
399 initiator_socket_type initiator_socket;
400 tlm_utils::multi_passthrough_target_socket<reg_router<BUSWIDTH>, BUSWIDTH> target_socket;
401 cci::cci_broker_handle m_broker;
402 cci::cci_param<bool> lazy_init;
405 std::vector<std::shared_ptr<target_info>> mem_targets;
406 std::multimap<sc_dt::uint64, std::shared_ptr<target_info>> cb_targets;
407 std::map<uint64_t, std::pair<uint64_t, std::string>> mod_addr_name_map;
408 std::function<void(
bool, uint64_t)> m_pre_b_transport_callback;
409 bool initialized =
false;