gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
clint.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Huawei International
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include "dev/riscv/clint.hh"
39
40#include "arch/riscv/system.hh"
41#include "cpu/base.hh"
42#include "debug/Clint.hh"
43#include "mem/packet.hh"
44#include "mem/packet_access.hh"
45#include "params/Clint.hh"
46#include "sim/system.hh"
47
48namespace gem5
49{
50
51using namespace RiscvISA;
52
54 BasicPioDevice(params, params.pio_size),
56 nThread(params.num_threads),
57 signal(params.name + ".signal", 0, this, INT_RTC),
58 reset(params.name + ".reset"),
59 resetMtimecmp(params.reset_mtimecmp),
60 registers(params.name + ".registers", params.pio_addr, this,
61 params.mtimecmp_reset_value)
62{
63 reset.onChange([this](const bool& new_val){
64 if (new_val) {
65 doReset();
66 }
67 });
68}
69
70void
72{
73 // Increment mtime when received RTC signal
74 uint64_t& mtime = registers.mtime.get();
75 if (id == INT_RTC) {
76 mtime++;
77 }
78
79 for (int context_id = 0; context_id < nThread; context_id++) {
80
81 auto tc = system->threads[context_id];
82
83 // Post timer interrupt
84 uint64_t mtimecmp = registers.mtimecmp[context_id].get();
85 if (mtime >= mtimecmp) {
86 if (mtime == mtimecmp) {
88 "MTIP posted - thread: %d, mtime: %d, mtimecmp: %d\n",
89 context_id, mtime, mtimecmp);
90 }
91 tc->getCpuPtr()->postInterrupt(tc->threadId(),
92 ExceptionCode::INT_TIMER_MACHINE, 0);
93 } else {
94 tc->getCpuPtr()->clearInterrupt(tc->threadId(),
95 ExceptionCode::INT_TIMER_MACHINE, 0);
96 }
97 }
98}
99
100void
102{
103 using namespace std::placeholders;
104
105 // Sanity check
106 assert(clint->pioSize >= minBankSize);
107
108 // Calculate reserved space size
109 const size_t reserved0_size = mtimecmpStart - clint->nThread * 4;
110 reserved.emplace_back("reserved0", reserved0_size);
111 const size_t reserved1_size = mtimeStart
112 - mtimecmpStart - clint->nThread * 8;
113 reserved.emplace_back("reserved1", reserved1_size);
114 const size_t reserved2_size = clint->pioSize - minBankSize;
115 if (reserved2_size > 0) {
116 reserved.emplace_back("reserved2", reserved2_size);
117 }
118
119 // Initialize registers
120 for (int i = 0; i < clint->nThread; i++) {
121 msip.emplace_back(std::string("msip") + std::to_string(i), 0);
122 mtimecmp.emplace_back(
123 std::string("mtimecmp") + std::to_string(i), mtimecmpResetValue);
124 }
125
126 // Add registers to bank
127 for (int i = 0; i < clint->nThread; i++) {
128 auto write_cb = std::bind(&Clint::writeMSIP, clint, _1, _2, i);
129 msip[i].writeable(0x1);
130 msip[i].writer(write_cb);
132 }
134 for (int i = 0; i < clint->nThread; i++) {
136 }
138 mtime.readonly();
140 if (reserved2_size > 0) {
142 }
143}
144
145void
146Clint::writeMSIP(Register32& reg, const uint32_t& data, const int thread_id)
147{
148 reg.update(data);
149 updateMSIP(thread_id);
150};
151
152Tick
154{
155 // Check for atomic operation
156 bool is_atomic = pkt->isAtomicOp() && pkt->cmd == MemCmd::SwapReq;
158 "Read request - addr: %#x, size: %#x, atomic:%d\n",
159 pkt->getAddr(), pkt->getSize(), is_atomic);
160
161 // Perform register read
162 registers.read(pkt->getAddr(), pkt->getPtr<void>(), pkt->getSize());
163
164 if (is_atomic) {
165 // Perform atomic operation
166 (*(pkt->getAtomicOp()))(pkt->getPtr<uint8_t>());
167 return write(pkt);
168 } else {
169 pkt->makeResponse();
170 return pioDelay;
171 }
172}
173
174Tick
176{
178 "Write request - addr: %#x, size: %#x\n",
179 pkt->getAddr(), pkt->getSize());
180
181 // Perform register write
182 registers.write(pkt->getAddr(), pkt->getPtr<void>(), pkt->getSize());
183
184 pkt->makeResponse();
185 return pioDelay;
186}
187
188void
190{
191 registers.init();
193
194 RiscvSystem *rv_sys = dynamic_cast<RiscvSystem *>(system);
195 if (rv_sys != nullptr) {
196 rv_sys->setClint(this);
197 } else {
198 warn("Set Clint to RiscvSystem failed.");
199 }
200}
201
202Port &
203Clint::getPort(const std::string &if_name, PortID idx)
204{
205 if (if_name == "int_pin")
206 return signal;
207 else if (if_name == "reset")
208 return reset;
209 else
210 return BasicPioDevice::getPort(if_name, idx);
211}
212
213void
215{
216 for (auto const &reg: registers.msip) {
217 paramOut(cp, reg.name(), reg);
218 }
219 for (auto const &reg: registers.mtimecmp) {
220 paramOut(cp, reg.name(), reg);
221 }
222 paramOut(cp, "mtime", registers.mtime);
223}
224
225void
227{
228 for (auto &reg: registers.msip) {
229 paramIn(cp, reg.name(), reg);
230 }
231 for (auto &reg: registers.mtimecmp) {
232 paramIn(cp, reg.name(), reg);
233 }
234 paramIn(cp, "mtime", registers.mtime);
235}
236
237void
238Clint::updateMSIP(const int thread_id)
239{
240 auto tc = system->threads[thread_id];
241 if (registers.msip[thread_id].get()) {
242 DPRINTF(Clint, "MSIP posted - thread: %d\n", thread_id);
243 tc->getCpuPtr()->postInterrupt(tc->threadId(),
244 ExceptionCode::INT_SOFTWARE_MACHINE, 0);
245 } else {
246 DPRINTF(Clint, "MSIP cleared - thread: %d\n", thread_id);
247 tc->getCpuPtr()->clearInterrupt(tc->threadId(),
248 ExceptionCode::INT_SOFTWARE_MACHINE, 0);
249 }
250}
251
252void
254 registers.mtime.reset();
255 for (int i = 0; i < nThread; i++) {
256 // According to the spec, the mtimecmp is in unknown state
257 // Assume we will change the mtimecmp registers to specify value
258 // if the mtimecmp registers accept the reset signal.
259 if (resetMtimecmp) {
260 registers.mtimecmp[i].reset();
261 }
262 registers.msip[i].reset();
263 updateMSIP(i);
264 }
265 // We need to update the mtip interrupt bits when reset
267}
268
269} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
const char data[]
BasicPioDevice(const Params &p, Addr size)
Definition io_device.cc:75
Tick pioDelay
Delay that the device experinces on an access.
Definition io_device.hh:157
std::vector< Register64 > mtimecmp
Definition clint.hh:121
std::vector< RegisterRaz > reserved
Definition clint.hh:123
std::vector< Register32 > msip
Definition clint.hh:120
void updateMSIP(const int thread_id)
Software Interrupt.
Definition clint.cc:238
void raiseInterruptPin(int id)
Timer tick callback.
Definition clint.cc:71
Clint(const Params &params)
Definition clint.cc:53
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition clint.cc:203
System * system
Definition clint.hh:74
void init() override
SimObject functions.
Definition clint.cc:189
int nThread
Definition clint.hh:75
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition clint.cc:175
SignalSinkPort< bool > reset
Definition clint.hh:77
ClintRegisters::Register32 Register32
Definition clint.hh:138
IntSinkPin< Clint > signal
Definition clint.hh:76
void writeMSIP(Register32 &reg, const uint32_t &data, const int thread_id)
Definition clint.cc:146
gem5::Clint::ClintRegisters registers
@ INT_RESET
Definition clint.hh:98
void doReset()
Definition clint.cc:253
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition clint.cc:226
bool resetMtimecmp
Definition clint.hh:78
ClintParams Params
Definition clint.hh:81
Tick read(PacketPtr pkt) override
PioDevice interface functions.
Definition clint.cc:153
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition clint.cc:214
Addr getAddr() const
Definition packet.hh:807
bool isAtomicOp() const
Definition packet.hh:846
void makeResponse()
Take a request packet and modify it in place to be suitable for returning as a response to that reque...
Definition packet.hh:1062
T * getPtr()
get a pointer to the data ptr.
Definition packet.hh:1225
unsigned getSize() const
Definition packet.hh:817
AtomicOpFunctor * getAtomicOp() const
Accessor function to atomic op.
Definition packet.hh:845
MemCmd cmd
The command field of the packet.
Definition packet.hh:372
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition io_device.cc:67
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition io_device.cc:59
Ports are used to interface objects to each other.
Definition port.hh:62
void addRegister(RegisterAdder reg)
Definition reg_bank.hh:1024
void setClint(Clint *clint)
Sets the pointer to the CLINT.
Definition system.hh:68
const Params & params() const
#define warn(...)
Definition logging.hh:288
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 5, 3 > reg
Definition types.hh:92
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::ostream CheckpointOut
Definition serialize.hh:66
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
Definition types.cc:40
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
Definition types.cc:72
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition types.hh:245
uint64_t Tick
Tick count type.
Definition types.hh:58
Packet * PacketPtr
Declaration of the Packet class.
const std::string & name()
Definition trace.cc:48

Generated on Mon Oct 27 2025 04:13:02 for gem5 by doxygen 1.14.0