gem5 v24.0.0.0
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 "cpu/base.hh"
41#include "debug/Clint.hh"
42#include "mem/packet.hh"
43#include "mem/packet_access.hh"
44#include "params/Clint.hh"
45#include "sim/system.hh"
46
47namespace gem5
48{
49
50using namespace RiscvISA;
51
52Clint::Clint(const Params &params) :
53 BasicPioDevice(params, params.pio_size),
54 system(params.system),
55 nThread(params.num_threads),
56 signal(params.name + ".signal", 0, this),
57 registers(params.name + ".registers", params.pio_addr, this)
58{
59}
60
61void
63{
64 // Increment mtime
65 uint64_t& mtime = registers.mtime.get();
66 mtime++;
67
68 for (int context_id = 0; context_id < nThread; context_id++) {
69
70 auto tc = system->threads[context_id];
71
72 // Update misc reg file
73 ISA* isa = dynamic_cast<ISA*>(tc->getIsaPtr());
74 if (isa->rvType() == RV32) {
75 isa->setMiscRegNoEffect(MISCREG_TIME, bits(mtime, 31, 0));
76 isa->setMiscRegNoEffect(MISCREG_TIMEH, bits(mtime, 63, 32));
77 } else {
79 }
80
81 // Post timer interrupt
82 uint64_t mtimecmp = registers.mtimecmp[context_id].get();
83 if (mtime >= mtimecmp) {
84 if (mtime == mtimecmp) {
86 "MTIP posted - thread: %d, mtime: %d, mtimecmp: %d\n",
87 context_id, mtime, mtimecmp);
88 }
89 tc->getCpuPtr()->postInterrupt(tc->threadId(),
90 ExceptionCode::INT_TIMER_MACHINE, 0);
91 } else {
92 tc->getCpuPtr()->clearInterrupt(tc->threadId(),
93 ExceptionCode::INT_TIMER_MACHINE, 0);
94 }
95 }
96}
97
98void
100{
101 using namespace std::placeholders;
102
103 // Calculate reserved space size
104 const size_t reserved0_size = mtimecmpStart - clint->nThread * 4;
105 reserved.emplace_back("reserved0", reserved0_size);
106 const size_t reserved1_size = mtimeStart
107 - mtimecmpStart - clint->nThread * 8;
108 reserved.emplace_back("reserved1", reserved1_size);
109
110 // Sanity check
111 assert((int) clint->pioSize <= maxBankSize);
112
113 // Initialize registers
114 for (int i = 0; i < clint->nThread; i++) {
115 msip.emplace_back(std::string("msip") + std::to_string(i), 0);
116 mtimecmp.emplace_back(std::string("mtimecmp") + std::to_string(i), 0);
117 }
118
119 // Add registers to bank
120 for (int i = 0; i < clint->nThread; i++) {
121 auto read_cb = std::bind(&Clint::readMSIP, clint, _1, i);
122 msip[i].reader(read_cb);
123 auto write_cb = std::bind(&Clint::writeMSIP, clint, _1, _2, i);
124 msip[i].writer(write_cb);
126 }
128 for (int i = 0; i < clint->nThread; i++) {
130 }
132 mtime.readonly();
134}
135
136uint32_t
137Clint::readMSIP(Register32& reg, const int thread_id)
138{
139 // To avoid discrepancies if mip is externally set using remote_gdb etc.
140 auto tc = system->threads[thread_id];
141 RegVal mip = tc->readMiscReg(MISCREG_IP);
142 uint32_t msip = bits<uint32_t>(mip, ExceptionCode::INT_SOFTWARE_MACHINE);
143 reg.update(msip);
144 return reg.get();
145};
146
147void
148Clint::writeMSIP(Register32& reg, const uint32_t& data, const int thread_id)
149{
150 reg.update(data);
151 assert(data <= 1);
152 auto tc = system->threads[thread_id];
153 if (data > 0) {
154 DPRINTF(Clint, "MSIP posted - thread: %d\n", thread_id);
155 tc->getCpuPtr()->postInterrupt(tc->threadId(),
156 ExceptionCode::INT_SOFTWARE_MACHINE, 0);
157 } else {
158 DPRINTF(Clint, "MSIP cleared - thread: %d\n", thread_id);
159 tc->getCpuPtr()->clearInterrupt(tc->threadId(),
160 ExceptionCode::INT_SOFTWARE_MACHINE, 0);
161 }
162};
163
164Tick
166{
167 // Check for atomic operation
168 bool is_atomic = pkt->isAtomicOp() && pkt->cmd == MemCmd::SwapReq;
170 "Read request - addr: %#x, size: %#x, atomic:%d\n",
171 pkt->getAddr(), pkt->getSize(), is_atomic);
172
173 // Perform register read
174 registers.read(pkt->getAddr(), pkt->getPtr<void>(), pkt->getSize());
175
176 if (is_atomic) {
177 // Perform atomic operation
178 (*(pkt->getAtomicOp()))(pkt->getPtr<uint8_t>());
179 return write(pkt);
180 } else {
181 pkt->makeResponse();
182 return pioDelay;
183 }
184}
185
186Tick
188{
190 "Write request - addr: %#x, size: %#x\n",
191 pkt->getAddr(), pkt->getSize());
192
193 // Perform register write
194 registers.write(pkt->getAddr(), pkt->getPtr<void>(), pkt->getSize());
195
196 pkt->makeResponse();
197 return pioDelay;
198}
199
200void
206
207Port &
208Clint::getPort(const std::string &if_name, PortID idx)
209{
210 if (if_name == "int_pin")
211 return signal;
212 else
213 return BasicPioDevice::getPort(if_name, idx);
214}
215
216void
218{
219 for (auto const &reg: registers.msip) {
220 paramOut(cp, reg.name(), reg);
221 }
222 for (auto const &reg: registers.mtimecmp) {
223 paramOut(cp, reg.name(), reg);
224 }
225 paramOut(cp, "mtime", registers.mtime);
226}
227
228void
230{
231 for (auto &reg: registers.msip) {
232 paramIn(cp, reg.name(), reg);
233 }
234 for (auto &reg: registers.mtimecmp) {
235 paramIn(cp, reg.name(), reg);
236 }
237 paramIn(cp, "mtime", registers.mtime);
238}
239
240} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
const char data[]
void setMiscRegNoEffect(RegIndex idx, RegVal val) override
Definition isa.cc:662
Tick pioDelay
Delay that the device experinces on an access.
Definition io_device.hh:157
Addr pioSize
Size that the device's address range.
Definition io_device.hh:154
std::vector< Register64 > mtimecmp
Definition clint.hh:111
std::vector< RegisterRaz > reserved
Definition clint.hh:113
std::vector< Register32 > msip
Definition clint.hh:110
NOTE: This implementation of CLINT is based on the SiFive U54MC datasheet: https://sifive....
Definition clint.hh:71
void raiseInterruptPin(int id)
Timer tick callback.
Definition clint.cc:62
Clint(const Params &params)
Definition clint.cc:52
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition clint.cc:208
System * system
Definition clint.hh:74
void init() override
SimObject functions.
Definition clint.cc:201
int nThread
Definition clint.hh:75
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition clint.cc:187
ClintRegisters::Register32 Register32
Definition clint.hh:125
IntSinkPin< Clint > signal
Definition clint.hh:76
void writeMSIP(Register32 &reg, const uint32_t &data, const int thread_id)
Definition clint.cc:148
gem5::Clint::ClintRegisters registers
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition clint.cc:229
ClintParams Params
Definition clint.hh:79
Tick read(PacketPtr pkt) override
PioDevice interface functions.
Definition clint.cc:165
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition clint.cc:217
uint32_t readMSIP(Register32 &reg, const int thread_id)
Definition clint.cc:137
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition packet.hh:295
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:1022
virtual void read(Addr addr, void *buf, Addr bytes)
Definition reg_bank.hh:1029
virtual void write(Addr addr, const void *buf, Addr bytes)
Definition reg_bank.hh:1075
Threads threads
Definition system.hh:310
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition bitfield.hh:79
Bitfield< 7 > i
Definition misc_types.hh:67
constexpr enums::RiscvType RV32
Definition pcstate.hh:56
Bitfield< 5, 3 > reg
Definition types.hh:92
Bitfield< 15 > system
Definition misc.hh:1032
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
uint64_t RegVal
Definition types.hh:173
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
Declaration of the Packet class.
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:03 for gem5 by doxygen 1.11.0