gem5  v22.1.0.0
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 % LUPIO_TMR_MAX;
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 % LUPIO_TMR_MAX;
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
#define DPRINTF(x,...)
Definition: trace.hh:186
Addr pioAddr
Address that the device listens to.
Definition: io_device.hh:151
Tick pioDelay
Delay that the device experinces on an access.
Definition: io_device.hh:157
LupioTMR: A virtual timer device which provides a real time counter, as well as a configurable timer ...
Definition: lupio_tmr.hh:47
void updateIRQ(int level, int cpu)
Post or clear timer interrupts.
Definition: lupio_tmr.cc:73
const ByteOrder byteOrder
Definition: lupio_tmr.hh:49
std::vector< LupioTimer > timers
Definition: lupio_tmr.hh:76
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: lupio_tmr.cc:214
void lupioTMRCallback(int cpu)
Process the timer's event.
Definition: lupio_tmr.cc:107
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
System * system
Definition: lupio_tmr.hh:50
LupioTMR(const Params &params)
Definition: lupio_tmr.cc:46
void lupioTMRSet(int cpu)
Schedule the next timer event.
Definition: lupio_tmr.cc:92
Tick read(PacketPtr pkt) override
Implement BasicPioDevice virtual functions.
Definition: lupio_tmr.cc:198
uint64_t lupioTMRCurrentTime()
Return the simulated time.
Definition: lupio_tmr.cc:86
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
virtual std::string name() const
Definition: named.hh:47
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:294
Addr getAddr() const
Definition: packet.hh:805
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:361
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:1059
unsigned getSize() const
Definition: packet.hh:815
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:352
PioDeviceParams Params
Definition: io_device.hh:134
Threads threads
Definition: system.hh:313
void deschedule(Event &event)
Definition: eventq.hh:1028
void schedule(Event &event, Tick when)
Definition: eventq.hh:1019
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
#define LUPIO_TMR_IRQE
Definition: lupio_tmr.cc:37
#define LUPIO_TMR_PRDC
Definition: lupio_tmr.cc:38
#define LUPIO_TMR_EXPD
Definition: lupio_tmr.cc:41
Bitfield< 0 > ie
Bitfield< 5 > r
Definition: pagetable.hh:60
Bitfield< 5, 3 > reg
Definition: types.hh:92
Bitfield< 15 > system
Definition: misc.hh:1004
Bitfield< 63 > val
Definition: misc.hh:776
Bitfield< 20 > level
Definition: intmessage.hh:51
Bitfield< 3 > addr
Definition: types.hh:84
Tick ns
nanosecond
Definition: core.cc:71
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
uint64_t Tick
Tick count type.
Definition: types.hh:58
int64 int_type
Definition: sc_nbdefs.hh:206

Generated on Wed Dec 21 2022 10:22:34 for gem5 by doxygen 1.9.1