qualcomm/qbox
Loading...
Searching...
No Matches
sync_window.h
1/*
2 * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <systemc>
8#include <tlm>
9#include <cci_configuration>
10#include <tlm_utils/tlm_quantumkeeper.h>
11#include <scp/report.h>
12#include <mutex>
13#include <type_traits>
14#include <runonsysc.h>
15
16#ifdef SC_HAS_OB_EVENT
17#include <sysc/utils/sc_ob_event.h>
18#else
19#include "observer_event.h"
20typedef gs::observer_event sc_ob_event;
21#endif
22
23namespace sc_core {
24
31 virtual sc_core::sc_time quantum() = 0;
32 virtual bool keep_alive() = 0;
33};
35 sc_core::sc_time quantum() { return tlm_utils::tlm_quantumkeeper::get_global_quantum(); }
36 bool keep_alive() { return true; }
37};
39 sc_core::sc_time quantum()
40 {
41 return sc_core::sc_pending_activity() ? sc_core::sc_time_to_pending_activity() : sc_core::SC_ZERO_TIME;
42 }
43 bool keep_alive() { return false; }
44};
45
51template <class sc_sync_policy = sc_sync_policy_in_sync>
52class sc_sync_window : public sc_core::sc_module, public sc_core::sc_prim_channel
53{
54 static_assert(std::is_base_of<sc_sync_policy_base, sc_sync_policy>::value,
55 "sc_sync_policy must derive from sc_sync_policy_base");
56
57 sc_core::sc_event m_sweep_ev;
58 sc_ob_event m_step_ev;
59 sc_event m_update_ev;
60 std::mutex m_mutex;
61 sc_sync_policy policy;
62 gs::runonsysc m_sc; // run attach/detach_suspending on the SystemC thread
63
64public:
65 struct window {
66 sc_core::sc_time from;
67 sc_core::sc_time to;
68 bool operator==(const window& other) const { return other.to == to && other.from == from; }
69 };
70
71 static inline const struct window zero_window = { sc_core::SC_ZERO_TIME, sc_core::SC_ZERO_TIME };
72 static inline const struct window open_window = { sc_core::SC_ZERO_TIME, sc_core::sc_max_time() };
73
74private:
75 window m_window;
76 window m_incomming_window; // used to hold the window values coming in from
77 // the other side.
78 bool m_is_attached; // Track attachment state
79
80 std::function<void(const window&)> m_other_async_set_window_fn;
81
82 void do_other_async_set_window_fn(window w)
83 {
84 if (m_other_async_set_window_fn) {
85 auto now = sc_core::sc_time_stamp();
86 m_other_async_set_window_fn(w);
87 }
88 }
89
90 /* Handle suspending/resuming at the end of the window, also inform other side if we reach end of window */
91 void step_helper()
92 {
93 auto now = sc_core::sc_time_stamp();
94 auto to = m_window.to;
95 SCP_INFO(()) << "End window check"
96 << " their window " << m_window.from << " " << to;
97
98 if (now >= to) {
99 sc_core::sc_unsuspend_all(); // such that pending activity is valid if it's needed below.
100
101 /* We should suspend at this point, and wait for the other side to catch up */
102 SCP_TRACE(()) << "Suspend at: " << now << " quantum: " << policy.quantum();
103 do_other_async_set_window_fn({ now, now + policy.quantum() });
104
105 if (!policy.keep_alive()) async_attach_suspending();
106 sc_core::sc_suspend_all();
107
108 } else {
109 sc_core::sc_unsuspend_all();
110 if (!policy.keep_alive()) async_detach_suspending();
111
112 /* Re-notify event - maybe presumably moved */
113 m_step_ev.notify(to - now);
114 }
115 }
116
117 /*
118 * Handle all sweep requests, once we arrive at a sweep point, tell the other side.
119 */
120 void sweep_helper()
121 {
122 auto now = sc_core::sc_time_stamp();
123
124 SCP_INFO(()) << "Done sweep"
125 << " their window " << m_window.from << " - " << m_window.to;
126
127 do_other_async_set_window_fn({ now, now + policy.quantum() });
128 }
129
130 /* Manage the Sync aync update */
131 void update()
132 {
133 std::lock_guard<std::mutex> lg(m_mutex);
134 // Now we are on our thread, it's safe to update our window.
135 m_window = m_incomming_window;
136 auto now = sc_core::sc_time_stamp();
137 SCP_INFO(()) << "Update"
138 << " their window " << m_window.from << " - " << m_window.to;
139
140 if (m_window.from > now) {
141 m_sweep_ev.notify(m_window.from - now); // Try to move time forward.
142 } else {
143 m_sweep_ev.cancel(); // no need to fire event.
144 }
145 /* let stepper handle suspend/resume, must time notify */
146 m_update_ev.notify(sc_core::SC_ZERO_TIME);
147 }
148
149public:
150 SCP_LOGGER();
151
152 /* API call from other pair, presumably in a different thread.
153 * The internal window wil be updated atomically.
154 *
155 * Input: window - Window to set for sync. Sweep till the 'from' and step to the 'to'.
156 */
157 void async_set_window(const window& w)
158 {
159 /* Only accept updated windows so we dont re-send redundant updates */
160 std::lock_guard<std::mutex> lg(m_mutex);
161 // Check if window is attached before allowing window updates
162 if (!m_is_attached) {
163 SCP_FATAL(()) << "Cannot set window on detached sync_window. "
164 << "Window must be attached before calling async_set_window(). ";
165 }
166 m_incomming_window = w;
167 if (!(w == m_window)) {
168 SCP_INFO(()) << "sync " << w.from << " " << w.to;
169 async_request_update();
170 }
171 }
176 bool is_attached() const { return m_is_attached; }
183 void attach()
184 {
185 /* Run on the SystemC thread; wait=false so a BQL-holding caller can't deadlock. */
186 m_sc.run_on_sysc([this] { async_attach_suspending(); }, false);
187 m_is_attached = true;
188 }
189
199 void detach(const sc_core::sc_time now = sc_core::SC_ZERO_TIME)
200 {
201 async_set_window({ now, sc_core::sc_max_time() }); // setting open window before detaching, since
202 // async_set_window required an attached window.
203 m_sc.run_on_sysc([this] { async_detach_suspending(); }, false);
204 m_is_attached = false;
205 }
206 void bind(sc_sync_window* other)
207 {
208 if (m_other_async_set_window_fn) {
209 SC_REPORT_WARNING("sc_sync_window",
210 "m_other_async_set_window_fn was already registered or other "
211 "sc_sync_window was already bound!");
212 }
213 m_other_async_set_window_fn = std::bind(&sc_sync_window::async_set_window, other, std::placeholders::_1);
214 }
215 void register_sync_cb(std::function<void(const window&)> fn)
216 {
217 if (m_other_async_set_window_fn) {
218 SC_REPORT_WARNING("sc_sync_window",
219 "m_other_async_set_window_fn was already registered or other "
220 "sc_sync_window was already bound!");
221 }
222 m_other_async_set_window_fn = fn;
223 }
224 SC_CTOR (sc_sync_window) : m_window({sc_core::SC_ZERO_TIME, policy.quantum()}), m_is_attached(false)
225 {
226 SCP_TRACE(())("Constructor");
227 SC_METHOD(sweep_helper);
228 dont_initialize();
229 sensitive << m_sweep_ev;
230
231 SC_METHOD(step_helper);
232 dont_initialize();
233 sensitive << m_step_ev << m_update_ev;
234
235 m_step_ev.notify(sc_core::SC_ZERO_TIME);
236
237 this->attach(); // This will set m_is_attached to true
238 }
239};
240
241} // namespace sc_core
Definition observer_event.h:17
Definition runonsysc.h:23
windowed synchronizer, template SYNC_POLICY gives the quantum (dynamically) and specifies if the wind...
Definition sync_window.h:53
void attach()
Attach to the primitive channel's suspend/resume mechanism for time synchronization.
Definition sync_window.h:183
void detach(const sc_core::sc_time now=sc_core::SC_ZERO_TIME)
Detach from the primitive channel's suspend/resume mechanism and fully open the time window.
Definition sync_window.h:199
bool is_attached() const
Check if the sync_window is currently attached.
Definition sync_window.h:176
Base class which gives the quantum (dynamically) and specifies if the window should stay open indefin...
Definition sync_window.h:30
Definition sync_window.h:38
Definition sync_window.h:34
Definition sync_window.h:65