scc 2025.09
SystemC components library
chi_rn_initiator.h
1/*
2 * Copyright 2021 Arteris IP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <chi/chi_tlm.h>
20#include <tlm/scc/pe/intor_if.h>
21#include <scc/ordered_semaphore.h>
22#include <scc/peq.h>
23#include <scc/sc_variable.h>
24#include <cci_configuration>
25#include <systemc>
26#include <tlm_utils/peq_with_get.h>
27#include <tuple>
28#include <unordered_map>
29
30namespace chi {
31namespace pe {
32
33enum channel_e {
34 REQ = 0,
35 WDAT,
36 SRSP,
37 CRSP,
38 RDAT,
39 SNP,
40 CH_CNT
41};
42
43class chi_rn_initiator_b :
44 public sc_core::sc_module,
45 public chi::chi_bw_transport_if<chi::chi_protocol_types>,
47{
48public:
49 using payload_type = chi::chi_protocol_types::tlm_payload_type;
50 using phase_type = chi::chi_protocol_types::tlm_phase_type;
51 using cb_function_t = std::function<void(chi::pe::channel_e, payload_type&)>;
52
53 sc_core::sc_in<bool> clk_i{"clk_i"};
54
55 sc_core::sc_export<tlm::scc::pe::intor_fw_b> fw_i{"fw_i"};
56
57 sc_core::sc_port<tlm::scc::pe::intor_bw_b, 1, sc_core::SC_ZERO_OR_MORE_BOUND> bw_o{"bw_o"};
58
59 void b_snoop(payload_type& trans, sc_core::sc_time& t) override;
60
61 tlm::tlm_sync_enum nb_transport_bw(payload_type& trans, phase_type& phase, sc_core::sc_time& t) override;
62
63 void invalidate_direct_mem_ptr(sc_dt::uint64 start_range, sc_dt::uint64 end_range) override;
64
65 size_t get_transferwith_in_bytes() const { return transfer_width_in_bytes; }
75 void transport(payload_type& trans, bool blocking) override;
82 void snoop_resp(payload_type& trans, bool sync = false) override;
83
84 chi_rn_initiator_b(sc_core::sc_module_name nm,
85 sc_core::sc_port_b<chi::chi_fw_transport_if<chi_protocol_types>>& port, size_t transfer_width);
86
87 virtual ~chi_rn_initiator_b();
88
89 chi_rn_initiator_b() = delete;
90
91 chi_rn_initiator_b(chi_rn_initiator_b const&) = delete;
92
93 chi_rn_initiator_b(chi_rn_initiator_b&&) = delete;
94
95 chi_rn_initiator_b& operator=(chi_rn_initiator_b const&) = delete;
96
97 chi_rn_initiator_b& operator=(chi_rn_initiator_b&&) = delete;
98
99 cci::cci_param<unsigned> src_id{"src_id", 1};
100
101 cci::cci_param<unsigned> tgt_id{"tgt_id", 0}; // home node id will be used as tgt_id in all transaction req
102
103 cci::cci_param<bool> data_interleaving{"data_interleaving", true};
104
105 cci::cci_param<bool> strict_income_order{"strict_income_order", false};
106
107 cci::cci_param<bool> use_legacy_mapping{"use_legacy_mapping", false};
108
109 cci::cci_param<unsigned> snp_req_credit_limit{"snp_req_credit_limit", std::numeric_limits<unsigned>::max()};
110
111 void add_protocol_cb(channel_e e, cb_function_t cb) {
112 assert(e < CH_CNT);
113 protocol_cb[e] = cb;
114 }
115protected:
116 void end_of_elaboration() override { clk_if = dynamic_cast<sc_core::sc_clock*>(clk_i.get_interface()); }
117
118 unsigned calculate_beats(payload_type& p) {
119 // sc_assert(p.get_data_length() > 0);
120 return p.get_data_length() < transfer_width_in_bytes ? 1 : p.get_data_length() / transfer_width_in_bytes;
121 }
122
123 void snoop_dispatch();
124
125 void snoop_handler(payload_type* trans);
126
127 const size_t transfer_width_in_bytes;
128
129 std::string instance_name;
130
131 scc::ordered_semaphore req_credits{"TxReqCredits", 0U, true}; // L-credits provided by completer(HN)
132
133 scc::sc_variable<unsigned> snp_counter{"SnpInFlight", 0};
134
135 scc::sc_variable<unsigned> snp_credit_sent{"SnpCreditGranted", 0};
136
137 void grant_credit(unsigned amount=1);
138
139 sc_core::sc_port_b<chi::chi_fw_transport_if<chi_protocol_types>>& socket_fw;
140
141 struct tx_state {
143 tx_state(std::string const& name)
144 : peq(sc_core::sc_gen_unique_name(name.c_str())) {}
145 };
146 std::unordered_map<uintptr_t, tx_state*> tx_state_by_trans;
147
148 std::vector<tx_state*> tx_state_pool;
149
150 std::unordered_map<unsigned, scc::ordered_semaphore> active_tx_by_id;
151
152 scc::ordered_semaphore strict_order_sem{1};
153
154 tlm_utils::peq_with_get<payload_type> snp_peq{"snp_peq"}, snp_dispatch_que{"snp_dispatch_que"};
155
156 unsigned thread_avail{0}, thread_active{0};
157
158 scc::ordered_semaphore req_order{1};
159
160 scc::ordered_semaphore req_chnl{1};
161
162 scc::ordered_semaphore wdat_chnl{1};
163
164 scc::ordered_semaphore prio_wdat_chnl{1};
165
166 scc::ordered_semaphore sresp_chnl{1};
167
168 scc::ordered_semaphore prio_sresp_chnl{1};
169
170 sc_core::sc_event any_tx_finished;
171
172 sc_core::sc_time clk_period{10, sc_core::SC_NS};
173
174private:
175 void send_wdata(payload_type& trans, chi::pe::chi_rn_initiator_b::tx_state* txs);
176 void handle_snoop_response(payload_type& trans, chi::pe::chi_rn_initiator_b::tx_state* txs);
177 void send_comp_ack(payload_type& trans, tx_state*& txs);
178 void clk_counter();
179 unsigned get_clk_cnt() { return m_clock_counter; }
180
181 void create_data_ext(payload_type& trans);
182 void send_packet(tlm::tlm_phase phase, payload_type& trans, chi::pe::chi_rn_initiator_b::tx_state* txs);
183 void exec_read_write_protocol(const unsigned int txn_id, payload_type& trans,
184 chi::pe::chi_rn_initiator_b::tx_state*& txs);
185 void exec_atomic_protocol(const unsigned int txn_id, payload_type& trans,
186 chi::pe::chi_rn_initiator_b::tx_state*& txs);
187 void send_cresp_response(payload_type& trans);
188 void update_data_extension(chi::chi_data_extension* data_ext, payload_type& trans);
189
190 unsigned m_clock_counter{0};
191 unsigned m_prev_clk_cnt{0};
192
193 sc_core::sc_clock* clk_if{nullptr};
194 uint64_t peq_cnt{0};
195
196 scc::sc_variable<unsigned> tx_waiting{"TxWaiting4Id", 0};
197 scc::sc_variable<unsigned> tx_waiting4crd{"TxWaiting4Credit", 0};
198 scc::sc_variable<unsigned> tx_outstanding{"TxOutstanding", 0};
199
200 std::array<cb_function_t, chi::pe::CH_CNT> protocol_cb;
201};
202
206template <unsigned int BUSWIDTH = 32, typename TYPES = chi::chi_protocol_types, int N = 1,
207 sc_core::sc_port_policy POL = sc_core::SC_ONE_OR_MORE_BOUND>
208class chi_rn_initiator : public chi_rn_initiator_b {
209public:
210 using base = chi_rn_initiator_b;
211
212 using payload_type = base::payload_type;
213 using phase_type = base::phase_type;
218 chi_rn_initiator(const sc_core::sc_module_name& nm, chi::chi_initiator_socket<BUSWIDTH, TYPES, N, POL>& socket_)
219 : chi_rn_initiator_b(nm, socket_.get_base_port(), BUSWIDTH)
220 , socket(socket_) {
221 socket(*this);
222 this->instance_name = socket.name();
223 }
224
225 chi_rn_initiator() = delete;
226
227 chi_rn_initiator(chi_rn_initiator const&) = delete;
228
230
231 chi_rn_initiator& operator=(chi_rn_initiator const&) = delete;
232
233 chi_rn_initiator& operator=(chi_rn_initiator&&) = delete;
234
235private:
237};
238
239} /* namespace pe */
240} /* namespace chi */
void transport(payload_type &trans, bool blocking) override
The forward transport function. It behaves blocking and is re-entrant.
void snoop_resp(payload_type &trans, bool sync=false) override
triggers a non-blocking snoop response if the snoop callback does not do so.
void b_snoop(payload_type &trans, sc_core::sc_time &t) override
snoop access to a snooped master
chi_rn_initiator(const sc_core::sc_module_name &nm, chi::chi_initiator_socket< BUSWIDTH, TYPES, N, POL > &socket_)
the constructor
The ordered_semaphore primitive channel class.
TLM2.0 components modeling CHI.
Definition chi_tlm.cpp:21
tlm::tlm_fw_transport_if< TYPES > chi_fw_transport_if
alias declaration for the forward interface
Definition chi_tlm.h:904
priority event queue
Definition peq.h:41
SystemC variable.
Definition sc_variable.h:87