qualcomm/qbox
Loading...
Searching...
No Matches
reg_router.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 _GREENSOCS_BASE_COMPONENTS_REG_ROUTER_H
8#define _GREENSOCS_BASE_COMPONENTS_REG_ROUTER_H
9
10#include <cinttypes>
11#include <iomanip>
12#include <cci_configuration>
13#include <systemc>
14#include <tlm>
15#include <scp/report.h>
16#include <scp/helpers.h>
17#include <tlm_utils/multi_passthrough_initiator_socket.h>
18#include <tlm_utils/multi_passthrough_target_socket.h>
19#include <cciutils.h>
20#include <module_factory_registery.h>
21#include <module_factory_container.h>
22#include <tlm_sockets_buswidth.h>
23#include <router_if.h>
24#include <vector>
25#include <memory>
26#include <map>
27#include <functional>
28
29#define ENABLE_MODULE_NAME_DBG_INFO 1
30namespace gs {
31
32template <unsigned int BUSWIDTH = DEFAULT_TLM_BUSWIDTH>
33class reg_router : public sc_core::sc_module, public gs::router_if<BUSWIDTH>
34{
35 SCP_LOGGER(());
36 SCP_LOGGER((DMI), "dmi");
37
39 using initiator_socket_type = typename gs::router_if<BUSWIDTH>::template multi_passthrough_initiator_socket_spying<
40 reg_router<BUSWIDTH>>;
41 using gs::router_if<BUSWIDTH>::bound_targets;
42
43 void register_boundto(std::string s) override
44 {
45 s = gs::router_if<BUSWIDTH>::nameFromSocket(s);
46 std::shared_ptr<target_info> ti_ptr = std::make_shared<target_info>(); // Create shared_ptr
47 ti_ptr->name = s;
48 ti_ptr->index = bound_targets.size();
49 SCP_DEBUG(()) << "Connecting : " << ti_ptr->name;
50 auto tmp = name();
51 int i;
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); // Add shared_ptr
56 }
57
58public:
59 void b_transport(int id, tlm::tlm_generic_payload& trans, sc_core::sc_time& delay)
60 {
61 sc_dt::uint64 addr = trans.get_address();
62
63 std::shared_ptr<target_info> ti = decode_address(trans); // Use shared_ptr
64
65 if (!ti) {
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);
68 return;
69 }
70
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);
79#endif
80
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);
88 }
89 if (trans.get_response_status() >= tlm::TLM_OK_RESPONSE) {
90 do_callbacks(trans, delay);
91 }
92 if (cb_targets.size() > 0) {
93 trans.set_dmi_allowed(false);
94 }
95 }
96
97 unsigned int transport_dbg(int id, tlm::tlm_generic_payload& trans)
98 {
99 sc_dt::uint64 addr = trans.get_address();
100 // transport_dbg transactions should only be handled by register memory
101 std::shared_ptr<target_info> ti = decode_address(trans); // Use shared_ptr
102
103 if (!ti) {
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);
106 return 0;
107 }
108
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);
117#endif
118
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);
123 return ret;
124 }
125
126 bool get_direct_mem_ptr(int id, tlm::tlm_generic_payload& trans, tlm::tlm_dmi& dmi_data)
127 {
128 SCP_TRACE(()) << "[REG-MEM] call get_direct_mem_ptr (not allowed) [txn]: " << scp::scp_txn_tostring(trans);
129 return false;
130 }
131
132 std::string txn_to_str(tlm::tlm_generic_payload& trans, bool is_callback = false, bool found_callback = false)
133 {
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]: ";
142 }
143 } else if (found_callback) {
144 ss << "[REG-MEM] (" << cmd << " TO REG-MEM BEFORE " << cmd << " CB) [txn]: ";
145 } else {
146 ss << "[REG-MEM] (NO CB FOUND) [txn]: ";
147 }
148
149 ss << scp::scp_txn_tostring(trans);
150 return ss.str();
151 }
152
153 std::string txn_command_str(const tlm::tlm_generic_payload& trans)
154 {
155 std::string cmd;
156 switch (trans.get_command()) {
157 case tlm::TLM_READ_COMMAND:
158 cmd = "READ";
159 break;
160 case tlm::TLM_WRITE_COMMAND:
161 cmd = "WRITE";
162 break;
163 case tlm::TLM_IGNORE_COMMAND:
164 cmd = "IGNORE";
165 break;
166 default:
167 cmd = "UNKNOWN";
168 break;
169 }
170 return cmd;
171 }
172
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)
175 {
176 static const std::string empty;
177 if (mod_addr_name_map.empty()) {
178 return empty;
179 }
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;
185 mod_found = true;
186 return search.first->second.second;
187 }
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;
192 mod_found = true;
193 return expected_node->second.second;
194 } else {
195 mod_found = false;
196 }
197 } else {
198 mod_found = false;
199 }
200 return empty;
201 }
202#endif
203
204 // Priority semantics match router.h: lower numeric value = higher priority
205 // (0 is strongest, default when unset). Callbacks fan out to all peers at
206 // the strongest priority; equal-priority mem targets are a fatal collision.
207 bool do_callbacks(tlm::tlm_generic_payload& trans, sc_core::sc_time& delay)
208 {
209 if (cb_targets.empty()) {
210 return false;
211 }
212
213 sc_dt::uint64 addr = trans.get_address();
214
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) {
221 claimants.clear();
222 claimants.push_back(cand);
223 best_prio = cand->priority;
224 } else if (cand->priority == best_prio) {
225 claimants.push_back(cand);
226 }
227 }
228
229 if (claimants.empty()) return false;
230
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();
241 break;
242 }
243 }
244 return true;
245 }
246
247 std::shared_ptr<target_info> decode_address(tlm::tlm_generic_payload& trans) override
248 {
249 lazy_initialize();
250
251 sc_dt::uint64 addr = trans.get_address();
252
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)
257 best = ti;
258 else if (ti->priority == best->priority && ti != best)
259 SCP_FATAL(()) << "Two targets with equal priority both claim address 0x" << std::hex << addr;
260 }
261 return best;
262 }
263
264protected:
265 virtual void before_end_of_elaboration() override
266 {
267 if (!lazy_init) lazy_initialize();
268 }
269
270protected:
271 static std::vector<sc_dt::uint64> parse_u64_csv(const std::string& s)
272 {
273 std::vector<sc_dt::uint64> out;
274 size_t i = 0;
275 while (i < s.size()) {
276 while (i < s.size() && (s[i] == ' ' || s[i] == ',')) ++i;
277 if (i >= s.size()) break;
278 size_t consumed = 0;
279 sc_dt::uint64 v = std::stoull(s.substr(i), &consumed, 0);
280 out.push_back(v);
281 i += consumed;
282 }
283 return out;
284 }
285
286 // Walk a's element grid and return true iff any byte of an element is claimed by b.
287 // O(prod(a.counts) * elem_size) at bind time; fine at elaboration.
288 static bool banks_element_overlap(const std::shared_ptr<target_info>& a, const std::shared_ptr<target_info>& b)
289 {
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);
293 while (true) {
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;
298 }
299 size_t k = s.size();
300 while (k-- > 0) {
301 if (++idx[k] < c[k]) break;
302 idx[k] = 0;
303 if (k == 0) return false;
304 }
305 if (k == (size_t)-1) return false;
306 }
307 }
308
309 void lazy_initialize() override
310 {
311 if (initialized) return;
312 initialized = true;
313
314 for (auto& ti_ptr : bound_targets) { // Iterate over shared_ptrs
315 std::string name = ti_ptr->name;
316 std::string src;
317
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);
325
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";
333 }
334
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) " : "");
341
342 if (ti_ptr->is_callback) {
343 cb_targets.insert(std::make_pair(ti_ptr->address, ti_ptr));
344 } else {
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;
352
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;
359 }
360 }
361 }
362 mem_targets.push_back(ti_ptr);
363 }
364 }
365 }
366
367public:
368 explicit reg_router(
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")
377 , m_broker(broker)
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)
381 {
382 SCP_DEBUG(()) << "reg_router constructed";
383
384 target_socket.register_b_transport(this, &reg_router::b_transport);
385 target_socket.register_transport_dbg(this, &reg_router::transport_dbg);
386 target_socket.register_get_direct_mem_ptr(this, &reg_router::get_direct_mem_ptr);
387 // memory model doesn't implement invalidate_direct_mem_ptr so no need to register it
388 SCP_DEBUG((DMI)) << "reg_router Initializing DMI SCP reporting";
389 }
390
391 reg_router() = delete;
392
393 reg_router(const reg_router&) = delete;
394
395 ~reg_router() = default;
396
397public:
398 // make the sockets public for binding
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;
403
404private:
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;
410};
411} // namespace gs
412
413extern "C" void module_register();
414#endif
Definition router_if.h:25
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10
Definition router_if.h:50