qualcomm/qbox
Loading...
Searching...
No Matches
riscv32.h
1/*
2 * This file is part of libqbox
3 * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#pragma once
9
10#include <string>
11#include <functional>
12
13#include <cci_configuration>
14#include <libqemu-cxx/target/riscv.h>
15
16#include <libgssync.h>
17#include <module_factory_registery.h>
18
19#include <cpu.h>
20#include <ports/qemu-target-signal-socket.h>
21#include <ports/qemu-initiator-signal-socket.h>
22
23class QemuCpuRiscv32 : public QemuCpu
24{
25protected:
26 uint64_t m_hartid;
27 gs::async_event m_irq_ev;
28
29 void mip_update_cb(uint32_t value)
30 {
31 if (value) {
32 m_irq_ev.notify();
33 }
34 }
35
36public:
37 // IRQ input sockets - sc_vector of 32 IRQs
38 sc_core::sc_vector<QemuTargetSignalSocket> irq_in;
39
40 // CCI parameters for RISC-V CPU configuration
41 cci::cci_param<uint64_t> p_hartid;
42 cci::cci_param<bool> p_debug;
43 cci::cci_param<bool> p_pmp;
44 cci::cci_param<bool> p_mmu;
45 cci::cci_param<std::string> p_priv_spec;
46 cci::cci_param<uint64_t> p_cbom_blocksize;
47 cci::cci_param<uint64_t> p_cbop_blocksize;
48 cci::cci_param<uint64_t> p_cboz_blocksize;
49 cci::cci_param<uint64_t> p_mvendorid;
50 cci::cci_param<uint64_t> p_mimpid;
51 cci::cci_param<uint64_t> p_marchid;
52 cci::cci_param<uint64_t> p_resetvec;
53 cci::cci_param<bool> p_smrnmi;
54 cci::cci_param<bool> p_smaia;
55 cci::cci_param<bool> p_ssaia;
56 QemuCpuRiscv32(const sc_core::sc_module_name& name, QemuInstance& inst, const char* model, uint64_t hartid)
57 : QemuCpu(name, inst, std::string(model) + "-riscv")
58 , m_hartid(hartid)
59 /*
60 * We have no choice but to attach-suspend here. This is fixable but
61 * non-trivial. It means that the SystemC kernel will never starve...
62 */
63 , m_irq_ev(true)
64 // Initialize IRQ vector with 32 sockets
65 , irq_in("irq_in", 32)
66 // Initialize CCI parameters with default values
67 , p_hartid("hartid", hartid, "Hardware thread ID")
68 , p_debug("debug", true, "Enable debug support")
69 , p_pmp("pmp", false, "Enable Physical Memory Protection")
70 , p_mmu("mmu", true, "Enable Memory Management Unit")
71 , p_priv_spec("priv_spec", "v1.12.0", "Privilege specification version")
72 , p_cbom_blocksize("cbom_blocksize", 64, "Cache block management operation block size")
73 , p_cbop_blocksize("cbop_blocksize", 64, "Cache block prefetch operation block size")
74 , p_cboz_blocksize("cboz_blocksize", 64, "Cache block zero operation block size")
75 , p_mvendorid("mvendorid", 0, "Vendor ID")
76 , p_mimpid("mimpid", 0, "Implementation ID")
77 , p_marchid("marchid", 0, "Architecture ID")
78 , p_resetvec("resetvec", 0x0, "Reset vector address")
79 , p_smrnmi("smrnmi", false, "Enable Smrnmi (RNMI M-mode CSRs 0x740-0x744)")
80 , p_smaia("smaia", false, "Enable Smaia (AIA M-mode CSRs)")
81 , p_ssaia("ssaia", false, "Enable Ssaia (AIA S-mode CSRs)")
82 {
83 m_external_ev |= m_irq_ev;
84 for (auto& irq : irq_in) {
85 m_external_ev |= irq->default_event();
86 }
87 }
88
89 void before_end_of_elaboration()
90 {
91 QemuCpu::before_end_of_elaboration();
92
93 qemu::CpuRiscv32 cpu(get_qemu_dev());
94 cpu.set_prop_int("hartid", p_hartid);
95
96 // Set properties from CCI parameters
97 cpu.set_prop_int("resetvec", p_resetvec);
98 cpu.set_prop_bool("debug", p_debug);
99 cpu.set_prop_bool("pmp", p_pmp);
100 cpu.set_prop_bool("mmu", p_mmu);
101 cpu.set_prop_str("priv_spec", p_priv_spec.get_value().c_str());
102
103 if (p_smrnmi) {
104 cpu.set_prop_bool("smrnmi", true);
105 }
106 if (p_smaia) {
107 cpu.set_prop_bool("smaia", true);
108 }
109 if (p_ssaia) {
110 cpu.set_prop_bool("ssaia", true);
111 }
112
113 // Cache block operation sizes
114 cpu.set_prop_int("cbom_blocksize", p_cbom_blocksize);
115 cpu.set_prop_int("cbop_blocksize", p_cbop_blocksize);
116 cpu.set_prop_int("cboz_blocksize", p_cboz_blocksize);
117
118 // Vendor ID settings
119 cpu.set_prop_int("mvendorid", p_mvendorid);
120 cpu.set_prop_int("mimpid", p_mimpid);
121 cpu.set_prop_int("marchid", p_marchid);
122
123 cpu.set_mip_update_callback(std::bind(&QemuCpuRiscv32::mip_update_cb, this, std::placeholders::_1));
124 }
125
126 void end_of_elaboration() override
127 {
128 QemuCpu::end_of_elaboration();
129
130 // Initialize IRQ sockets with GPIO pin numbers 0-31
131 for (int i = 0; i < 32; i++) {
132 irq_in[i].init(m_dev, i);
133 }
134
135 // Register reset handler - needed for proper reset behavior when system reset is requested
136 qemu::CpuRiscv32 cpu(get_qemu_dev());
137 cpu.register_reset();
138 }
139};
140
141class cpu_riscv32 : public QemuCpuRiscv32
142{
143public:
144 static constexpr qemu::Target ARCH = qemu::Target::RISCV32;
145
146 cpu_riscv32(const sc_core::sc_module_name& name, sc_core::sc_object* o, uint64_t hartid)
147 : cpu_riscv32(name, *(dynamic_cast<QemuInstance*>(o)), hartid)
148 {
149 }
150 cpu_riscv32(const sc_core::sc_module_name& n, QemuInstance& inst, uint64_t hartid)
151 : QemuCpuRiscv32(n, inst, "rv32", hartid)
152 {
153 }
154 // Constructor for test framework compatibility (uses hartid=0 by default)
155 cpu_riscv32(const sc_core::sc_module_name& n, QemuInstance& inst): QemuCpuRiscv32(n, inst, "rv32", 0) {}
156};
157
158extern "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