scc 2026.07
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 <cci_configuration>
20#include <chi/chi_tlm.h>
21#include <scc/ordered_semaphore.h>
22#include <scc/peq.h>
23#include <scc/sc_variable.h>
24#include <systemc>
25#include <tlm/scc/pe/intor_if.h>
26#include <tlm_utils/peq_with_get.h>
27#include <tuple>
28#include <unordered_map>
29
30namespace chi {
31namespace pe {
32
33enum channel_e { REQ = 0, WDAT, SRSP, CRSP, RDAT, SNP, CH_CNT };
34
35class chi_rn_initiator_b : public sc_core::sc_module,
36 public chi::chi_bw_transport_if<chi::chi_protocol_types>,
38public:
39 using payload_type = chi::chi_protocol_types::tlm_payload_type;
40 using phase_type = chi::chi_protocol_types::tlm_phase_type;
41 using cb_function_t = std::function<void(chi::pe::channel_e, payload_type&)>;
42
43 sc_core::sc_in<bool> clk_i{"clk_i"};
44
45 sc_core::sc_export<tlm::scc::pe::intor_fw_b> fw_i{"fw_i"};
46
47 sc_core::sc_port<tlm::scc::pe::intor_bw_b, 1, sc_core::SC_ZERO_OR_MORE_BOUND> bw_o{"bw_o"};
48
49 void b_snoop(payload_type& trans, sc_core::sc_time& t) override;
50
51 tlm::tlm_sync_enum nb_transport_bw(payload_type& trans, phase_type& phase, sc_core::sc_time& t) override;
52
53 void invalidate_direct_mem_ptr(sc_dt::uint64 start_range, sc_dt::uint64 end_range) override;
54
55 size_t get_transferwith_in_bytes() const { return transfer_width_in_bytes; }
65 void transport(payload_type& trans, bool blocking) override;
72 void snoop_resp(payload_type& trans, bool sync = false) override;
73
74 chi_rn_initiator_b(sc_core::sc_module_name nm, sc_core::sc_port_b<chi::chi_fw_transport_if<chi_protocol_types>>& port,
75 size_t transfer_width);
76
77 virtual ~chi_rn_initiator_b();
78
79 chi_rn_initiator_b() = delete;
80
81 chi_rn_initiator_b(chi_rn_initiator_b const&) = delete;
82
83 chi_rn_initiator_b(chi_rn_initiator_b&&) = delete;
84
85 chi_rn_initiator_b& operator=(chi_rn_initiator_b const&) = delete;
86
87 chi_rn_initiator_b& operator=(chi_rn_initiator_b&&) = delete;
88
89 cci::cci_param<unsigned> src_id{"src_id", 1};
90
91 cci::cci_param<unsigned> tgt_id{"tgt_id", 0}; // home node id will be used as tgt_id in all transaction req
92
93 cci::cci_param<bool> data_interleaving{"data_interleaving", true};
94
95 cci::cci_param<bool> strict_income_order{"strict_income_order", false};
96
97 cci::cci_param<bool> use_legacy_mapping{"use_legacy_mapping", false};
98
99 cci::cci_param<unsigned> snp_req_limit{"snp_req_limit", std::numeric_limits<unsigned>::max()};
100
101 cci::cci_param<unsigned> snp_req_credit_limit{"snp_req_credit_limit", std::numeric_limits<unsigned>::max()};
102
103 cci::cci_param<unsigned> cresp_req_credit_limit{"cresp_credit_limit", std::numeric_limits<unsigned>::max()};
104
105 cci::cci_param<unsigned> rdat_req_credit_limit{"rdat_credit_limit", std::numeric_limits<unsigned>::max()};
106
107 void add_protocol_cb(channel_e e, cb_function_t cb) {
108 assert(e < CH_CNT);
109 protocol_cb[e] = cb;
110 }
111
112 unsigned get_credit_count(channel_e e);
113
114protected:
115 void end_of_elaboration() override { clk_if = dynamic_cast<sc_core::sc_clock*>(clk_i.get_interface()); }
116
117 unsigned calculate_beats(payload_type& p) {
118 // sc_assert(p.get_data_length() > 0);
119 return p.get_data_length() < transfer_width_in_bytes ? 1 : p.get_data_length() / transfer_width_in_bytes;
120 }
121
122 void snoop_dispatch();
123
124 void snoop_handler(payload_type* trans);
125
126 const size_t transfer_width_in_bytes;
127
128 std::string instance_name;
129
130 scc::ordered_semaphore ReceivedReqCreditCounter{"ReceivedReqCreditCounter", 0U, true}; // L-credits provided by completer(HN)
131 scc::ordered_semaphore ReceivedWdatCreditCounter{"ReceivedWdatCreditCounter", 0U, true}; // L-credits provided by completer(HN)
132 scc::ordered_semaphore ReceivedSrespCreditCounter{"ReceivedSrespCreditCounter", 0U, true}; // L-credits provided by completer(HN)
133
134 scc::sc_variable<unsigned> snp_counter{"SnpInFlight", 0};
135
136 scc::sc_variable<unsigned> ProvidedSnpCreditCounter{"ProvidedSnpCreditCounter", 0};
137 scc::sc_variable<unsigned> ProvidedCrespCreditCounter{"ProvidedCrespCreditCounter", 0};
138 scc::sc_variable<unsigned> ProvidedRdatCreditCounter{"ProvidedRdatCreditCounter", 0};
139
140 void grant_credit(credit_type_e type, unsigned amount = 1);
141
142 sc_core::sc_port_b<chi::chi_fw_transport_if<chi_protocol_types>>& socket_fw;
143
144 struct tx_state {
146 tx_state(std::string const& name)
147 : peq(sc_core::sc_gen_unique_name(name.c_str())) {}
148 };
149 std::unordered_map<uintptr_t, tx_state*> tx_state_by_trans;
150
151 std::vector<tx_state*> tx_state_pool;
152
153 std::unordered_map<unsigned, scc::ordered_semaphore> active_tx_by_id;
154
155 scc::ordered_semaphore strict_order_sem{1};
156
157 tlm_utils::peq_with_get<payload_type> snp_peq{"snp_peq"}, snp_dispatch_que{"snp_dispatch_que"};
158
159 unsigned thread_avail{0}, thread_active{0};
160
161 scc::ordered_semaphore req_order{1};
162
163 scc::ordered_semaphore req_chnl{1};
164
165 scc::ordered_semaphore wdat_chnl{1};
166
167 scc::ordered_semaphore prio_wdat_chnl{1};
168
169 scc::ordered_semaphore sresp_chnl{1};
170
171 scc::ordered_semaphore prio_sresp_chnl{1};
172
173 sc_core::sc_event any_tx_finished;
174
175 sc_core::sc_time clk_period{10, sc_core::SC_NS};
176
177private:
178 void send_wdata(payload_type& trans, chi::pe::chi_rn_initiator_b::tx_state* txs);
179 void handle_snoop_response(payload_type& trans, chi::pe::chi_rn_initiator_b::tx_state* txs);
180 void send_comp_ack(payload_type& trans, tx_state*& txs);
181 void clk_counter();
182 unsigned get_clk_cnt() { return m_clock_counter; }
183
184 void create_data_ext(payload_type& trans);
185 void send_packet(tlm::tlm_phase phase, payload_type& trans, chi::pe::chi_rn_initiator_b::tx_state* txs);
186 void exec_read_write_protocol(const unsigned int txn_id, payload_type& trans, chi::pe::chi_rn_initiator_b::tx_state*& txs);
187 void exec_atomic_protocol(const unsigned int txn_id, payload_type& trans, chi::pe::chi_rn_initiator_b::tx_state*& txs);
188 void finish_cresp_response(payload_type& trans);
189 void update_data_extension(chi::chi_data_extension* data_ext, payload_type& trans);
190
191 unsigned m_clock_counter{0};
192 unsigned m_prev_clk_cnt{0};
193
194 sc_core::sc_clock* clk_if{nullptr};
195 uint64_t peq_cnt{0};
196
197 scc::sc_variable<unsigned> tx_waiting{"TxWaiting4Id", 0};
198 scc::sc_variable<unsigned> tx_waiting4crd{"TxWaiting4Credit", 0};
199 scc::sc_variable<unsigned> tx_outstanding{"TxOutstanding", 0};
200
201 std::array<cb_function_t, chi::pe::CH_CNT> protocol_cb;
202};
203
207template <unsigned int BUSWIDTH = 32, typename TYPES = chi::chi_protocol_types, int N = 1,
208 sc_core::sc_port_policy POL = sc_core::SC_ONE_OR_MORE_BOUND>
209class chi_rn_initiator : public chi_rn_initiator_b {
210public:
211 using base = chi_rn_initiator_b;
212
213 using payload_type = base::payload_type;
214 using phase_type = base::phase_type;
219 chi_rn_initiator(const sc_core::sc_module_name& nm, chi::chi_initiator_socket<BUSWIDTH, TYPES, N, POL>& socket_)
220 : chi_rn_initiator_b(nm, socket_.get_base_port(), BUSWIDTH)
221 , socket(socket_) {
222 socket(*this);
223 this->instance_name = socket.name();
224 }
225
226 chi_rn_initiator() = delete;
227
228 chi_rn_initiator(chi_rn_initiator const&) = delete;
229
231
232 chi_rn_initiator& operator=(chi_rn_initiator const&) = delete;
233
234 chi_rn_initiator& operator=(chi_rn_initiator&&) = delete;
235
236private:
238};
239
240} /* namespace pe */
241} /* 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:894
priority event queue
Definition peq.h:42
SystemC variable.
Definition sc_variable.h:88