gem5  v21.2.1.1
lupio_tmr.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 The Regents of the University of California
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "dev/lupio/lupio_tmr.hh"
30 
31 #include "cpu/base.hh"
32 #include "debug/LupioTMR.hh"
33 #include "mem/packet_access.hh"
34 #include "params/LupioTMR.hh"
35 
36 // Specific fields for CTRL
37 #define LUPIO_TMR_IRQE 0x1
38 #define LUPIO_TMR_PRDC 0x2
39 
40 // Specific fields for STAT
41 #define LUPIO_TMR_EXPD 0x1
42 
43 namespace gem5
44 {
45 
46 LupioTMR::LupioTMR(const Params &params) :
47  BasicPioDevice(params, params.pio_size),
48  system(params.system),
49  nThread(params.num_threads),
50  intType(params.int_type)
51 {
52  timers.resize(nThread);
53 
54  for (int cpu = 0; cpu < nThread; cpu++) {
55  timers[cpu].tmrEvent = new EventFunctionWrapper(
56  [=]{
57  lupioTMRCallback(cpu);
58  }, name()+"done"
59  );
60  }
61 
62  DPRINTF(LupioTMR, "LupioTMR initalized\n");
63 }
64 
66 {
67  for (int cpu = 0; cpu < nThread; cpu++) {
68  delete timers[cpu].tmrEvent;
69  }
70 }
71 
72 void
74 {
75  auto tc = system->threads[cpu];
76  // post an interrupt
77  if (level) {
78  tc->getCpuPtr()->postInterrupt(tc->threadId(), intType, 0);
79  } else {
80  // clear the interrupt
81  tc->getCpuPtr()->clearInterrupt(tc->threadId(), intType, 0);
82  }
83 }
84 
85 uint64_t
87 {
88  return curTick() / sim_clock::as_int::ns;
89 }
90 
91 void
93 {
94  // Start the timer
95  timers[cpu].startTime = curTick();
96 
97  // Schedule the timer to fire at the number of ticks stored
98  // in the reload register from the current tick
99  if (!timers[cpu].tmrEvent->scheduled()) {
100  // Convert the reload value to ticks from nanoseconds
101  schedule(*(timers[cpu].tmrEvent),
102  (timers[cpu].reload * sim_clock::as_int::ns) + curTick());
103  }
104 }
105 
106 void
108 {
109  // Signal expiration
110  timers[cpu].expired = true;
111  if (timers[cpu].ie) {
112  updateIRQ(1, cpu);
113  }
114 
115  // If periodic timer, reload
116  if (timers[cpu].pd && timers[cpu].reload) {
117  lupioTMRSet(cpu);
118  }
119 }
120 
121 uint64_t
122 LupioTMR::lupioTMRRead(uint8_t addr, int size)
123 {
124  uint32_t r = 0;
125 
126  size_t cpu = addr >> LUPIO_TMR_MAX;
127  size_t reg = (addr >> 2) & (LUPIO_TMR_MAX - 1);
128 
129  switch (reg) {
130  case LUPIO_TMR_TIME:
132  DPRINTF(LupioTMR, "Read LUPIO_TMR_TME: %d\n", r);
133  break;
134  case LUPIO_TMR_LOAD:
135  r = timers[cpu].reload;
136  DPRINTF(LupioTMR, "Read LUPIO_TMR_LOAD: %d\n", r);
137  break;
138  case LUPIO_TMR_STAT:
139  if (timers[cpu].expired) {
140  r |= LUPIO_TMR_EXPD;
141  }
142 
143  // Acknowledge expiration
144  timers[cpu].expired = false;
145  DPRINTF(LupioTMR, "Read LUPIO_TMR_STAT: %d\n", r);
146  updateIRQ(0, cpu);
147  break;
148 
149  default:
150  panic("Unexpected read to the LupioTMR device at address %#llx!",
151  addr);
152  break;
153  }
154  return r;
155 }
156 
157 void
158 LupioTMR::lupioTMRWrite(uint8_t addr, uint64_t val64, int size)
159 {
160  uint32_t val = val64;
161 
162  size_t cpu = addr >> LUPIO_TMR_MAX;
163  size_t reg = (addr >> 2) & (LUPIO_TMR_MAX - 1);
164 
165  switch (reg) {
166  case LUPIO_TMR_LOAD:
167  timers[cpu].reload = val;
168  DPRINTF(LupioTMR, "Write LUPIO_TMR_LOAD: %d\n",
169  timers[cpu].reload);
170  break;
171 
172  case LUPIO_TMR_CTRL:
173  timers[cpu].ie = val & LUPIO_TMR_IRQE;
174  timers[cpu].pd = val & LUPIO_TMR_PRDC;
175  DPRINTF(LupioTMR, "Write LUPIO_TMR_CTRL\n");
176 
177  // Stop current timer if any
178  if (curTick() < timers[cpu].startTime +
179  (timers[cpu].reload * sim_clock::as_int::ns) &&
180  (timers[cpu].tmrEvent)->scheduled()) {
181  deschedule(*(timers[cpu].tmrEvent));
182  }
183 
184  // If reload isn't 0, start a new one
185  if (timers[cpu].reload) {
186  lupioTMRSet(cpu);
187  }
188  break;
189 
190  default:
191  panic("Unexpected write to the LupioTMR device at address %#llx!",
192  addr);
193  break;
194  }
195 }
196 
197 Tick
199 {
200  Addr tmr_addr = pkt->getAddr() - pioAddr;
201 
203  "Read request - addr: %#x, size: %#x\n", tmr_addr, pkt->getSize());
204 
205  uint64_t read_val = lupioTMRRead(tmr_addr, pkt->getSize());
206  DPRINTF(LupioTMR, "Packet Read: %#x\n", read_val);
207  pkt->setUintX(read_val, byteOrder);
208  pkt->makeResponse();
209 
210  return pioDelay;
211 }
212 
213 Tick
215 {
216  Addr tmr_addr = pkt->getAddr() - pioAddr;
217 
218  DPRINTF(LupioTMR, "Write register %#x value %#x\n", tmr_addr,
219  pkt->getUintX(byteOrder));
220 
221  lupioTMRWrite(tmr_addr, pkt->getUintX(byteOrder), pkt->getSize());
222  DPRINTF(LupioTMR, "Packet Write Value: %d\n", pkt->getUintX(byteOrder));
223 
224  pkt->makeResponse();
225 
226  return pioDelay;
227 }
228 } // namespace gem5
gem5::X86ISA::level
Bitfield< 20 > level
Definition: intmessage.hh:51
gem5::curTick
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
gem5::BasicPioDevice::pioAddr
Addr pioAddr
Address that the device listens to.
Definition: io_device.hh:151
gem5::Packet::getUintX
uint64_t getUintX(ByteOrder endian) const
Get the data in the packet byte swapped from the specified endianness and zero-extended to 64 bits.
Definition: packet.cc:334
gem5::LupioTMR::~LupioTMR
~LupioTMR()
Definition: lupio_tmr.cc:65
lupio_tmr.hh
gem5::LupioTMR::lupioTMRCurrentTime
uint64_t lupioTMRCurrentTime()
Return the simulated time.
Definition: lupio_tmr.cc:86
sc_dt::int_type
int64 int_type
Definition: sc_nbdefs.hh:240
gem5::LupioTMR::updateIRQ
void updateIRQ(int level, int cpu)
Post or clear timer interrupts.
Definition: lupio_tmr.cc:73
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:775
gem5::X86ISA::system
Bitfield< 15 > system
Definition: misc.hh:1003
gem5::EventManager::schedule
void schedule(Event &event, Tick when)
Definition: eventq.hh:1019
gem5::LupioTMR::system
System * system
Definition: lupio_tmr.hh:50
gem5::LupioTMR::LUPIO_TMR_MAX
@ LUPIO_TMR_MAX
Definition: lupio_tmr.hh:63
gem5::PioDevice::Params
PioDeviceParams Params
Definition: io_device.hh:134
gem5::LupioTMR::write
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: lupio_tmr.cc:214
gem5::LupioTMR::lupioTMRRead
uint64_t lupioTMRRead(const uint8_t addr, int size)
Function to return data pertaining to the timer, such as the simulated time in ticks.
Definition: lupio_tmr.cc:122
LUPIO_TMR_PRDC
#define LUPIO_TMR_PRDC
Definition: lupio_tmr.cc:38
LUPIO_TMR_IRQE
#define LUPIO_TMR_IRQE
Definition: lupio_tmr.cc:37
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
gem5::LupioTMR::LUPIO_TMR_CTRL
@ LUPIO_TMR_CTRL
Definition: lupio_tmr.hh:59
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:283
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::BasicPioDevice::pioDelay
Tick pioDelay
Delay that the device experinces on an access.
Definition: io_device.hh:157
gem5::LupioTMR::LUPIO_TMR_TIME
@ LUPIO_TMR_TIME
Definition: lupio_tmr.hh:57
gem5::LupioTMR::LUPIO_TMR_STAT
@ LUPIO_TMR_STAT
Definition: lupio_tmr.hh:60
gem5::LupioTMR
LupioTMR: A virtual timer device which provides a real time counter, as well as a configurable timer ...
Definition: lupio_tmr.hh:46
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::LupioTMR::lupioTMRCallback
void lupioTMRCallback(int cpu)
Process the timer's event.
Definition: lupio_tmr.cc:107
gem5::LupioTMR::LUPIO_TMR_LOAD
@ LUPIO_TMR_LOAD
Definition: lupio_tmr.hh:58
packet_access.hh
gem5::EventManager::deschedule
void deschedule(Event &event)
Definition: eventq.hh:1028
gem5::X86ISA::reg
Bitfield< 5, 3 > reg
Definition: types.hh:92
gem5::LupioTMR::read
Tick read(PacketPtr pkt) override
Implement BasicPioDevice virtual functions.
Definition: lupio_tmr.cc:198
gem5::EventFunctionWrapper
Definition: eventq.hh:1115
gem5::LupioTMR::timers
std::vector< LupioTimer > timers
Definition: lupio_tmr.hh:76
gem5::LupioTMR::LupioTMR
LupioTMR(const Params &params)
Definition: lupio_tmr.cc:46
base.hh
gem5::System::threads
Threads threads
Definition: system.hh:314
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:1031
gem5::LupioTMR::lupioTMRSet
void lupioTMRSet(int cpu)
Schedule the next timer event.
Definition: lupio_tmr.cc:92
gem5::LupioTMR::nThread
int nThread
Definition: lupio_tmr.hh:51
gem5::MipsISA::r
r
Definition: pra_constants.hh:98
gem5::Packet::getAddr
Addr getAddr() const
Definition: packet.hh:781
gem5::MipsISA::ie
Bitfield< 0 > ie
Definition: pra_constants.hh:142
gem5::LupioTMR::lupioTMRWrite
void lupioTMRWrite(const uint8_t addr, uint64_t val64, int size)
Function to launch or stop the timer depending on the load value.
Definition: lupio_tmr.cc:158
gem5::LupioTMR::intType
int intType
Definition: lupio_tmr.hh:52
gem5::LupioTMR::byteOrder
const ByteOrder byteOrder
Definition: lupio_tmr.hh:49
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: tlb.cc:60
gem5::BasicPioDevice
Definition: io_device.hh:147
gem5::Packet::setUintX
void setUintX(uint64_t w, ByteOrder endian)
Set the value in the word w after truncating it to the length of the packet and then byteswapping it ...
Definition: packet.cc:351
LUPIO_TMR_EXPD
#define LUPIO_TMR_EXPD
Definition: lupio_tmr.cc:41
gem5::Packet::getSize
unsigned getSize() const
Definition: packet.hh:791
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84
gem5::sim_clock::as_int::ns
Tick ns
nanosecond
Definition: core.cc:71

Generated on Wed May 4 2022 12:13:57 for gem5 by doxygen 1.8.17