gem5  v22.0.0.1
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 
47 namespace gem5
48 {
49 
50 using namespace RiscvISA;
51 
52 Clint::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 
61 void
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  isa->setMiscRegNoEffect(MISCREG_TIME, mtime);
75 
76  // Post timer interrupt
77  uint64_t mtimecmp = registers.mtimecmp[context_id].get();
78  if (mtime >= mtimecmp) {
79  if (mtime == mtimecmp) {
80  DPRINTF(Clint,
81  "MTIP posted - thread: %d, mtime: %d, mtimecmp: %d\n",
82  context_id, mtime, mtimecmp);
83  }
84  tc->getCpuPtr()->postInterrupt(tc->threadId(),
86  } else {
87  tc->getCpuPtr()->clearInterrupt(tc->threadId(),
89  }
90  }
91 }
92 
93 void
95 {
96  using namespace std::placeholders;
97 
98  // Calculate reserved space size
99  const size_t reserved0_size = mtimecmpStart - clint->nThread * 4;
100  reserved.emplace_back("reserved0", reserved0_size);
101  const size_t reserved1_size = mtimeStart
102  - mtimecmpStart - clint->nThread * 8;
103  reserved.emplace_back("reserved1", reserved1_size);
104 
105  // Sanity check
106  assert((int) clint->pioSize <= maxBankSize);
107 
108  // Initialize registers
109  for (int i = 0; i < clint->nThread; i++) {
110  msip.emplace_back(std::string("msip") + std::to_string(i), 0);
111  mtimecmp.emplace_back(std::string("mtimecmp") + std::to_string(i), 0);
112  }
113 
114  // Add registers to bank
115  for (int i = 0; i < clint->nThread; i++) {
116  auto read_cb = std::bind(&Clint::readMSIP, clint, _1, i);
117  msip[i].reader(read_cb);
118  auto write_cb = std::bind(&Clint::writeMSIP, clint, _1, _2, i);
119  msip[i].writer(write_cb);
120  addRegister(msip[i]);
121  }
122  addRegister(reserved[0]);
123  for (int i = 0; i < clint->nThread; i++) {
125  }
126  addRegister(reserved[1]);
127  mtime.readonly();
129 }
130 
131 uint32_t
132 Clint::readMSIP(Register32& reg, const int thread_id)
133 {
134  // To avoid discrepancies if mip is externally set using remote_gdb etc.
135  auto tc = system->threads[thread_id];
136  RegVal mip = tc->readMiscReg(MISCREG_IP);
137  uint32_t msip = bits<uint32_t>(mip, ExceptionCode::INT_SOFTWARE_MACHINE);
138  reg.update(msip);
139  return reg.get();
140 };
141 
142 void
143 Clint::writeMSIP(Register32& reg, const uint32_t& data, const int thread_id)
144 {
145  reg.update(data);
146  assert(data <= 1);
147  auto tc = system->threads[thread_id];
148  if (data > 0) {
149  DPRINTF(Clint, "MSIP posted - thread: %d\n", thread_id);
150  tc->getCpuPtr()->postInterrupt(tc->threadId(),
152  } else {
153  DPRINTF(Clint, "MSIP cleared - thread: %d\n", thread_id);
154  tc->getCpuPtr()->clearInterrupt(tc->threadId(),
156  }
157 };
158 
159 Tick
161 {
162  // Check for atomic operation
163  bool is_atomic = pkt->isAtomicOp() && pkt->cmd == MemCmd::SwapReq;
164  DPRINTF(Clint,
165  "Read request - addr: %#x, size: %#x, atomic:%d\n",
166  pkt->getAddr(), pkt->getSize(), is_atomic);
167 
168  // Perform register read
169  registers.read(pkt->getAddr(), pkt->getPtr<void>(), pkt->getSize());
170 
171  if (is_atomic) {
172  // Perform atomic operation
173  (*(pkt->getAtomicOp()))(pkt->getPtr<uint8_t>());
174  return write(pkt);
175  } else {
176  pkt->makeResponse();
177  return pioDelay;
178  }
179 }
180 
181 Tick
183 {
184  DPRINTF(Clint,
185  "Write request - addr: %#x, size: %#x\n",
186  pkt->getAddr(), pkt->getSize());
187 
188  // Perform register write
189  registers.write(pkt->getAddr(), pkt->getPtr<void>(), pkt->getSize());
190 
191  pkt->makeResponse();
192  return pioDelay;
193 }
194 
195 void
197 {
198  registers.init();
200 }
201 
202 Port &
203 Clint::getPort(const std::string &if_name, PortID idx)
204 {
205  if (if_name == "int_pin")
206  return signal;
207  else
208  return BasicPioDevice::getPort(if_name, idx);
209 }
210 
211 void
213 {
214  for (auto const &reg: registers.msip) {
215  paramOut(cp, reg.name(), reg);
216  }
217  for (auto const &reg: registers.mtimecmp) {
218  paramOut(cp, reg.name(), reg);
219  }
220  paramOut(cp, "mtime", registers.mtime);
221 }
222 
223 void
225 {
226  for (auto &reg: registers.msip) {
227  paramIn(cp, reg.name(), reg);
228  }
229  for (auto &reg: registers.mtimecmp) {
230  paramIn(cp, reg.name(), reg);
231  }
232  paramIn(cp, "mtime", registers.mtime);
233 }
234 
235 } // namespace gem5
gem5::PortID
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:245
gem5::Packet::isAtomicOp
bool isAtomicOp() const
Definition: packet.hh:829
gem5::Clint::nThread
int nThread
Definition: clint.hh:75
gem5::RegVal
uint64_t RegVal
Definition: types.hh:173
system.hh
gem5::Clint::ClintRegisters::mtime
Register64 mtime
Definition: clint.hh:112
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::MemCmd::SwapReq
@ SwapReq
Definition: packet.hh:119
gem5::Clint::write
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: clint.cc:182
gem5::Clint::writeMSIP
void writeMSIP(Register32 &reg, const uint32_t &data, const int thread_id)
Definition: clint.cc:143
gem5::Clint::ClintRegisters::init
void init()
Definition: clint.cc:94
gem5::Clint::registers
gem5::Clint::ClintRegisters registers
gem5::ArmISA::ISA
Definition: isa.hh:68
gem5::Clint::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: clint.cc:224
gem5::Clint::signal
IntSinkPin< Clint > signal
Definition: clint.hh:76
gem5::CheckpointIn
Definition: serialize.hh:68
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
gem5::RegisterBank::Register::get
const Data & get() const
Definition: reg_bank.hh:692
gem5::RegisterBank::write
virtual void write(Addr addr, const void *buf, Addr bytes)
Definition: reg_bank.hh:884
gem5::X86ISA::system
Bitfield< 15 > system
Definition: misc.hh:997
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
clint.hh
gem5::Clint::ClintRegisters::reserved
std::vector< RegisterRaz > reserved
Definition: clint.hh:113
gem5::Clint::init
void init() override
SimObject functions.
Definition: clint.cc:196
gem5::Clint::system
System * system
Definition: clint.hh:74
gem5::Clint::ClintRegisters::mtimecmp
std::vector< Register64 > mtimecmp
Definition: clint.hh:111
packet.hh
gem5::PioDevice::init
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: io_device.cc:59
gem5::ArmISA::ISA::setMiscRegNoEffect
void setMiscRegNoEffect(int misc_reg, RegVal val)
Definition: isa.cc:1089
gem5::PioDevice::Params
PioDeviceParams Params
Definition: io_device.hh:134
gem5::Clint::Clint
Clint(const Params &params)
Definition: clint.cc:52
gem5::Packet::getAtomicOp
AtomicOpFunctor * getAtomicOp() const
Accessor function to atomic op.
Definition: packet.hh:828
gem5::Clint
NOTE: This implementation of CLINT is based on the SiFive U54MC datasheet: https://sifive....
Definition: clint.hh:70
gem5::Clint::getPort
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition: clint.cc:203
gem5::RiscvISA::INT_TIMER_MACHINE
@ INT_TIMER_MACHINE
Definition: faults.hh:92
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
gem5::Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:291
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::Clint::ClintRegisters::maxBankSize
const Addr maxBankSize
Definition: clint.hh:108
gem5::Clint::read
Tick read(PacketPtr pkt) override
PioDevice interface functions.
Definition: clint.cc:160
gem5::BasicPioDevice::pioDelay
Tick pioDelay
Delay that the device experinces on an access.
Definition: io_device.hh:157
gem5::Clint::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: clint.cc:212
gem5::Packet::cmd
MemCmd cmd
The command field of the packet.
Definition: packet.hh:369
gem5::Clint::ClintRegisters::mtimecmpStart
const Addr mtimecmpStart
Definition: clint.hh:106
gem5::RiscvISA::INT_SOFTWARE_MACHINE
@ INT_SOFTWARE_MACHINE
Definition: faults.hh:89
name
const std::string & name()
Definition: trace.cc:49
packet_access.hh
gem5::RegisterBank::addRegister
void addRegister(RegisterBase &reg)
Definition: reg_bank.hh:820
gem5::X86ISA::reg
Bitfield< 5, 3 > reg
Definition: types.hh:92
gem5::RegisterBank::read
virtual void read(Addr addr, void *buf, Addr bytes)
Definition: reg_bank.hh:827
gem5::RegisterBank::Register::readonly
constexpr This & readonly()
Definition: reg_bank.hh:606
gem5::paramOut
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
Definition: types.cc:40
base.hh
gem5::Port
Ports are used to interface objects to each other.
Definition: port.hh:61
gem5::System::threads
Threads threads
Definition: system.hh:314
gem5::Clint::ClintRegisters::clint
Clint * clint
Definition: clint.hh:119
gem5::Packet::makeResponse
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:1044
gem5::paramIn
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
Definition: types.cc:72
gem5::RegisterBank::Register
Definition: reg_bank.hh:507
gem5::RiscvISA::MISCREG_TIME
@ MISCREG_TIME
Definition: misc.hh:75
gem5::BasicPioDevice::pioSize
Addr pioSize
Size that the device's address range.
Definition: io_device.hh:154
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
gem5::Clint::readMSIP
uint32_t readMSIP(Register32 &reg, const int thread_id)
Definition: clint.cc:132
gem5::Packet::getAddr
Addr getAddr() const
Definition: packet.hh:790
gem5::Clint::raiseInterruptPin
void raiseInterruptPin(int id)
Timer tick callback.
Definition: clint.cc:62
gem5::Clint::ClintRegisters::mtimeStart
const Addr mtimeStart
Definition: clint.hh:107
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::Clint::ClintRegisters::msip
std::vector< Register32 > msip
Definition: clint.hh:110
gem5::BasicPioDevice
Definition: io_device.hh:147
gem5::RiscvISA::MISCREG_IP
@ MISCREG_IP
Definition: misc.hh:72
gem5::Packet::getSize
unsigned getSize() const
Definition: packet.hh:800
gem5::PioDevice::getPort
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
gem5::Packet::getPtr
T * getPtr()
get a pointer to the data ptr.
Definition: packet.hh:1197

Generated on Sat Jun 18 2022 08:12:25 for gem5 by doxygen 1.8.17