104 using CpuTimeSyncStrategy::CpuTimeSyncStrategy;
106 void on_construct()
override;
107 void on_after_cpu_created()
override;
108 void on_before_end_of_elaboration()
override;
109 void on_end_of_simulation()
override;
110 void on_destroy()
override;
111 void on_halt_pre(
bool val)
override;
112 void on_halt_post()
override;
113 void on_reset_finish()
override;
114 void on_kick_notify()
override;
115 void on_qk_start()
override;
116 void on_arm_deadline()
override;
117 sc_core::sc_time get_local_time(int64_t vclock_now, sc_core::sc_time sc_t)
override;
118 void set_local_time(
const sc_core::sc_time& t)
override;
130 using CpuTimeSyncStrategy::CpuTimeSyncStrategy;
132 void on_end_of_elaboration()
override;
133 void on_destroy()
override;
137 inline bool mcips_enabled()
const {
return m_inst.is_mcips_enabled(); }
144 class QemuCpuHintTlmExtension :
public ::QemuCpuHintTlmExtension
147 void free()
override { }
151 std::shared_ptr<qemu::Timer> m_deadline_timer;
155 std::string m_cpu_type;
158 sc_core::sc_event_or_list m_external_ev;
159 sc_core::sc_process_handle m_sc_thread;
161 std::atomic<bool> m_signaled;
162 std::mutex m_signaled_lock;
163 std::condition_variable m_signaled_cond;
165 std::shared_ptr<gs::tlm_quantumkeeper_extended> m_qk;
166 std::atomic<bool> m_finished =
false;
167 std::atomic<bool> m_started =
false;
168 enum { none, start_reset, hold_reset, finish_reset } m_resetting = none;
171 std::mutex m_can_delete;
173 cci::cci_param<uint64_t> m_insn_per_second;
175 uint64_t m_quantum_ns;
178 std::unique_ptr<CpuTimeSyncStrategy> m_time_sync;
193 std::atomic<int> m_async_work_outstanding{ 0 };
194 std::mutex m_async_work_mutex;
195 std::condition_variable m_async_work_cv;
196 static constexpr int ASYNC_WORK_TIMEOUT_MS = 500;
204 qemu::Cpu::AsyncJobFn make_tracked_async_job(qemu::Cpu::AsyncJobFn job)
207 std::lock_guard<std::mutex> lock(m_async_work_mutex);
208 m_async_work_outstanding++;
210 if (m_finished)
return {};
211 return [
this, job = std::move(job)]()
mutable {
223 std::lock_guard<std::mutex> lock(m_async_work_mutex);
224 m_async_work_outstanding--;
226 m_async_work_cv.notify_all();
233 void create_quantum_keeper()
235 m_qk = m_inst.create_quantum_keeper();
238 SCP_FATAL(()) <<
"qbox : Sync policy unknown";
248 void set_coroutine_mode()
250 switch (m_qk->get_thread_type()) {
251 case gs::SyncPolicy::SYSTEMC_THREAD:
255 case gs::SyncPolicy::OS_THREAD:
256 m_coroutines =
false;
278 assert(!m_coroutines);
279 if (m_inst.get_tcg_mode() != QemuInstance::TCG_SINGLE) {
280 std::lock_guard<std::mutex> lock(m_signaled_lock);
282 m_signaled_cond.notify_all();
284 std::lock_guard<std::mutex> lock(m_inst.g_signaled_lock);
285 m_inst.g_signaled =
true;
286 m_inst.g_signaled_cond.notify_all();
294 void watch_external_ev()
308 SCP_TRACE(())(
"QEMU deadline KICK callback");
310 if (!m_finished) m_qemu_kick_ev.async_notify();
322 void deadline_timer_cb()
324 SCP_TRACE(())(
"QEMU deadline timer callback");
329 rearm_deadline_timer();
332 int64_t now = m_inst.get().get_virtual_clock();
333 sc_core::sc_time sc_t = sc_core::sc_time_stamp();
334 if (sc_core::sc_time(now, sc_core::SC_NS) > sc_t) {
335 m_qk->set(sc_core::sc_time(now, sc_core::SC_NS) - sc_t);
351 SCP_TRACE(())(
"Wait for work");
353 if (m_finished)
return;
356 m_on_sysc.run_on_sysc([
this]() { wait(m_external_ev); });
358 if (m_inst.get_tcg_mode() != QemuInstance::TCG_SINGLE) {
359 std::unique_lock<std::mutex> lock(m_signaled_lock);
360 m_signaled_cond.wait(lock, [
this] {
return m_signaled || m_finished; });
363 std::unique_lock<std::mutex> lock(m_inst.g_signaled_lock);
364 m_inst.g_signaled_cond.wait(lock, [
this] {
return m_inst.g_signaled || m_finished; });
365 m_inst.g_signaled =
false;
368 if (m_finished)
return;
369 SCP_TRACE(())(
"Have work, running CPU");
376 void rearm_deadline_timer()
380 m_deadline_timer->mod(m_inst.get().get_virtual_clock() + m_quantum_ns);
387 void prepare_run_cpu()
395 SCP_TRACE(())(
"Prepare run");
396 if (m_inst.get_tcg_mode() == QemuInstance::TCG_SINGLE) {
397 while (!m_inst.can_run() && !m_finished) {
401 while (!m_cpu.can_run() && !m_finished) {
402 if (!m_coroutines && !m_inst.is_tcg_enabled()) {
406 SCP_TRACE(())(
"Stopping QK (accelerator)");
415 m_cpu.set_soft_stopped(
false);
422 m_inst.get().lock_iothread();
430 auto last_vclock = m_inst.get().get_virtual_clock();
436 for (
int i = 0; i < m_inst.number_devices(); i++) {
437 if ((m_inst.get().get_virtual_clock() == last_vclock) && (m_cpu.can_run())) {
447 void sync_with_kernel()
449 int64_t now = m_inst.get().get_virtual_clock();
451 m_cpu.set_soft_stopped(
true);
453 m_inst.get().unlock_iothread();
454 if (m_finished)
return;
458 sc_core::sc_time sc_t = sc_core::sc_time_stamp();
459 if (sc_core::sc_time(now, sc_core::SC_NS) > sc_t) {
460 m_qk->set(sc_core::sc_time(now, sc_core::SC_NS) - sc_t);
471 void end_of_loop_cb()
473 SCP_TRACE(())(
"End of loop");
474 if (m_finished)
return;
480 if (!m_started)
return;
483 m_inst.get().coroutine_yield();
485 std::lock_guard<std::mutex> lock(m_can_delete);
494 void mainloop_thread_coroutine()
496 m_cpu.register_thread();
498 for (; !m_finished;) {
506 cci::cci_param<unsigned int> p_gdb_port;
509 QemuInitiatorSocket<> socket;
510 TargetSignalSocket<bool> halt;
511 TargetSignalSocket<bool> reset;
513 QemuCpu(
const sc_core::sc_module_name& name, QemuInstance& inst,
const std::string& type_name,
514 const char* cpu_type =
nullptr)
515 :
QemuDevice(name, inst, cpu_type == nullptr ? (type_name +
"-cpu").c_str() : type_name.c_str())
516 , m_cpu_type(cpu_type == nullptr ?
"" : cpu_type)
519 , m_qemu_kick_ev(false)
521 , p_gdb_port(
"gdb_port", 0,
"Wait for gdb connection on TCP port <gdb_port>")
522 , socket(
"mem", *this, inst)
523 , m_insn_per_second(
"insn_per_second", 1'000'000'000,
"number of instructions per second in mcips mode")
524 , m_coroutines(false)
526 using namespace std::placeholders;
528 if (mcips_enabled()) {
529 m_time_sync = std::make_unique<McipsSync>(*
this);
531 m_time_sync = std::make_unique<QuantumKeeperSync>(*
this);
534 auto haltcb = std::bind(&QemuCpu::halt_cb,
this, _1);
535 halt.register_value_changed_cb(haltcb);
536 auto resetcb = std::bind(&QemuCpu::reset_cb,
this, _1);
537 reset.register_value_changed_cb(resetcb);
539 m_time_sync->on_construct();
540 m_inst.add_dev(
this);
542 m_start_reset_done_ev.async_detach_suspending();
555 std::unique_lock<std::mutex> lock(m_async_work_mutex);
556 if (!m_async_work_cv.wait_for(lock, std::chrono::milliseconds(ASYNC_WORK_TIMEOUT_MS),
557 [
this] { return m_async_work_outstanding == 0; })) {
558 SCP_WARN(()) <<
"Timeout waiting for " << m_async_work_outstanding.load()
559 <<
" outstanding async work(s) to complete during destruction";
565 m_time_sync->on_destroy();
566 m_inst.del_dev(
this);
571 void end_of_simulation()
override
573 if (m_finished)
return;
576 if (!m_cpu.valid()) {
597 m_inst.get().lock_iothread();
599 m_cpu.clear_callbacks();
603 m_inst.get().unlock_iothread();
608 m_time_sync->on_end_of_simulation();
618 m_inst.get().unlock_iothread();
620 m_cpu.set_unplug(
true);
626 bool can_run()
override {
return m_cpu.can_run(); }
628 void retrieve_qemu_cpu()
630 if (m_cpu_type.empty()) {
631 m_cpu = qemu::Cpu(m_dev);
636 m_cpu = qemu::Cpu(m_dev.get_prop_link(
"cpu"));
638 m_time_sync->on_after_cpu_created();
640 m_cpu.set_soft_stopped(
true);
641 m_time_sync->on_before_end_of_elaboration();
643 m_cpu_hint_ext.set_cpu(m_cpu);
646 void before_end_of_elaboration()
override
648 QemuDevice::before_end_of_elaboration();
650 if (m_cpu_type.empty()) {
653 socket.init(m_dev,
"memory");
656 void halt_cb(
const bool& val)
658 SCP_TRACE(())(
"Halt : {}", val);
660 m_time_sync->on_halt_pre(val);
661 m_inst.get().lock_iothread();
663 m_inst.get().unlock_iothread();
664 m_time_sync->on_halt_post();
669 void reset_cb(
const bool& val)
672 if (m_finished)
return;
675 if (m_resetting != none)
return;
676 SCP_WARN(())(
"Start reset");
677 m_resetting = start_reset;
678 m_cpu.async_safe_run(make_tracked_async_job([
this] {
680 m_resetting = hold_reset;
681 m_start_reset_done_ev.async_notify();
684 if (m_resetting == none)
return;
685 while (m_resetting == start_reset) {
686 SCP_WARN(())(
"Hold reset");
695 m_inst.get().lock_iothread();
697 m_inst.get().unlock_iothread();
698 sc_core::wait(m_start_reset_done_ev);
700 m_inst.get().lock_iothread();
702 m_inst.get().unlock_iothread();
704 m_time_sync->on_reset_finish();
705 SCP_WARN(())(
"Finished reset");
708 m_time_sync->on_kick_notify();
710 virtual void end_of_elaboration()
override
712 QemuDevice::end_of_elaboration();
714 if (!m_cpu_type.empty()) {
718 m_time_sync->on_end_of_elaboration();
719 if (!p_gdb_port.is_default_value()) {
720 std::stringstream ss;
721 SCP_INFO(()) <<
"Starting gdb server on TCP port " << p_gdb_port;
722 ss <<
"tcp::" << p_gdb_port;
723 m_inst.get().start_gdb_server(ss.str());
727 virtual void start_of_simulation()
override
729 m_quantum_ns = int64_t(tlm_utils::tlm_quantumkeeper::get_global_quantum().to_seconds() * 1e9);
731 QemuDevice::start_of_simulation();
732 if (m_inst.get_tcg_mode() == QemuInstance::TCG_SINGLE) {
733 if (m_inst.can_run()) {
734 m_time_sync->on_qk_start();
736 }
else if (!m_coroutines) {
745 m_time_sync->on_qk_start();
758 m_time_sync->on_qk_start();
768 m_inst.get().lock_iothread();
769 m_cpu.set_soft_stopped(
false);
770 m_time_sync->on_arm_deadline();
772 m_inst.get().unlock_iothread();
779 m_cpu.set_vcpu_dirty(
true);
783 virtual void initiator_customize_tlm_payload(TlmPayload& payload)
override
786 payload.set_extension(&m_cpu_hint_ext);
789 virtual void initiator_tidy_tlm_payload(TlmPayload& payload)
override { payload.clear_extension(&m_cpu_hint_ext); }
795 virtual sc_core::sc_time initiator_get_local_time()
override
797 if (m_finished)
return sc_core::SC_ZERO_TIME;
799 int64_t vclock_now = m_inst.get().get_virtual_clock();
800 sc_core::sc_time sc_t = sc_core::sc_time_stamp();
801 return m_time_sync->get_local_time(vclock_now, sc_t);
808 virtual void initiator_set_local_time(
const sc_core::sc_time& t)
override
810 if (m_finished)
return;
811 m_time_sync->set_local_time(t);
815 virtual void initiator_async_run(qemu::Cpu::AsyncJobFn job)
override
817 if (!m_finished) m_cpu.async_run(make_tracked_async_job(std::move(job)));