qualcomm/qbox
Loading...
Searching...
No Matches
riscv64.h
1/*
2 * This file is part of libqbox
3 * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 * Author: GreenSocs 2021
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#pragma once
10
11#include <string>
12#include <functional>
13
14#include <cci_configuration>
15#include <libqemu-cxx/target/riscv.h>
16
17#include <libgssync.h>
18#include <module_factory_registery.h>
19
20#include <cpu.h>
21
22class QemuCpuRiscv64 : public QemuCpu
23{
24public:
25 sc_core::sc_vector<QemuTargetSignalSocket> irq_in;
26
27 // CCI parameters for RISC-V CPU configuration
28 cci::cci_param<uint64_t> p_resetvec;
29 cci::cci_param<bool> p_start_powered_off;
30 cci::cci_param<bool> p_pmp;
31 cci::cci_param<bool> p_smaia;
32 cci::cci_param<bool> p_ssaia;
33
34protected:
35 uint64_t m_hartid;
36 gs::async_event m_irq_ev;
37
38 void mip_update_cb(uint32_t value)
39 {
40 if (value) {
41 m_irq_ev.notify();
42 }
43 }
44
45public:
46 QemuCpuRiscv64(const sc_core::sc_module_name& name, QemuInstance& inst, const char* model, uint64_t hartid)
47 : QemuCpu(name, inst, std::string(model) + "-riscv")
48 , m_hartid(hartid)
49 /*
50 * We have no choice but to attach-suspend here. This is fixable but
51 * non-trivial. It means that the SystemC kernel will never starve...
52 */
53 , m_irq_ev(true)
54 , irq_in("irq_in", 32)
55 // Initialize CCI parameters with default values
56 , p_resetvec("resetvec", 0x0, "Reset vector address")
57 , p_start_powered_off("start_powered_off", false, "Start and reset the CPU in powered-off state")
58 , p_pmp("pmp", false, "Enable Physical Memory Protection")
59 , p_smaia("smaia", false, "Enable Smaia (AIA M-mode CSRs)")
60 , p_ssaia("ssaia", false, "Enable Ssaia (AIA S-mode CSRs)")
61 {
62 m_external_ev |= m_irq_ev;
63 for (auto& irq : irq_in) {
64 m_external_ev |= irq->default_event();
65 }
66 }
67
68 void before_end_of_elaboration()
69 {
70 QemuCpu::before_end_of_elaboration();
71
72 qemu::CpuRiscv64 cpu(get_qemu_dev());
73 cpu.set_prop_int("hartid", m_hartid);
74
75 // Set properties from CCI parameters
76 cpu.set_prop_int("resetvec", p_resetvec);
77 cpu.set_prop_bool("start-powered-off", p_start_powered_off);
78 cpu.set_prop_bool("pmp", p_pmp);
79 if (p_smaia) {
80 cpu.set_prop_bool("smaia", true);
81 }
82 if (p_ssaia) {
83 cpu.set_prop_bool("ssaia", true);
84 }
85
86 cpu.set_mip_update_callback(std::bind(&QemuCpuRiscv64::mip_update_cb, this, std::placeholders::_1));
87 }
88
89 void end_of_elaboration() override
90 {
91 QemuCpu::end_of_elaboration();
92
93 // Initialize IRQ sockets with GPIO pin numbers 0-31
94 for (int i = 0; i < 32; i++) {
95 irq_in[i].init(m_dev, i);
96 }
97
98 // Register reset handler - needed for proper reset behavior when system reset is requested
99 qemu::CpuRiscv32 cpu(get_qemu_dev());
100 cpu.register_reset();
101 }
102};
103
104class cpu_riscv64 : public QemuCpuRiscv64
105{
106public:
107 cpu_riscv64(const sc_core::sc_module_name& name, sc_core::sc_object* o, uint64_t hartid)
108 : cpu_riscv64(name, *(dynamic_cast<QemuInstance*>(o)), hartid)
109 {
110 }
111 cpu_riscv64(const sc_core::sc_module_name& n, QemuInstance& inst, uint64_t hartid)
112 : QemuCpuRiscv64(n, inst, "rv64", hartid)
113 {
114 }
115};
116
117extern "C" void module_register();
This class encapsulates a libqemu-cxx qemu::LibQemu instance. It handles QEMU parameters and instance...
Definition qemu-instance.h:89
Definition async_event.h:22
Definition riscv.h:30
Definition riscv.h:38