gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
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
43namespace gem5
44{
45
46LupioTMR::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 [=]{
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
72void
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
85uint64_t
90
91void
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
106void
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
121uint64_t
122LupioTMR::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
157void
158LupioTMR::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
197Tick
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
213Tick
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:210
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
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
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:1062
unsigned getSize() const
Definition packet.hh:817
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:310
void deschedule(Event &event)
Definition eventq.hh:1021
void schedule(Event &event, Tick when)
Definition eventq.hh:1012
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
#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, 3 > reg
Definition types.hh:92
Bitfield< 15 > system
Definition misc.hh:1032
Bitfield< 63 > val
Definition misc.hh:804
Bitfield< 20 > level
Definition intmessage.hh:51
Bitfield< 3 > addr
Definition types.hh:84
Tick ns
nanosecond
Definition core.cc:68
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
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
const std::string & name()
Definition trace.cc:48

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