gem5 [DEVELOP-FOR-25.1]
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#include "sim/system.hh"
36
37// Specific fields for CTRL
38#define LUPIO_TMR_IRQE 0x1
39#define LUPIO_TMR_PRDC 0x2
40
41// Specific fields for STAT
42#define LUPIO_TMR_EXPD 0x1
43
44namespace gem5
45{
46
48 BasicPioDevice(params, params.pio_size),
50 nThread(params.num_threads),
51 intType(params.int_type)
52{
53 timers.resize(nThread);
54
55 for (int cpu = 0; cpu < nThread; cpu++) {
56 timers[cpu].tmrEvent = new EventFunctionWrapper(
57 [=]{
59 }, name()+"done"
60 );
61 }
62
63 DPRINTF(LupioTMR, "LupioTMR initalized\n");
64}
65
67{
68 for (int cpu = 0; cpu < nThread; cpu++) {
69 delete timers[cpu].tmrEvent;
70 }
71}
72
73void
75{
76 auto tc = system->threads[cpu];
77 // post an interrupt
78 if (level) {
79 tc->getCpuPtr()->postInterrupt(tc->threadId(), intType, 0);
80 } else {
81 // clear the interrupt
82 tc->getCpuPtr()->clearInterrupt(tc->threadId(), intType, 0);
83 }
84}
85
86uint64_t
91
92void
94{
95 // Start the timer
96 timers[cpu].startTime = curTick();
97
98 // Schedule the timer to fire at the number of ticks stored
99 // in the reload register from the current tick
100 if (!timers[cpu].tmrEvent->scheduled()) {
101 // Convert the reload value to ticks from nanoseconds
102 schedule(*(timers[cpu].tmrEvent),
103 (timers[cpu].reload * sim_clock::as_int::ns) + curTick());
104 }
105}
106
107void
109{
110 // Signal expiration
111 timers[cpu].expired = true;
112 if (timers[cpu].ie) {
113 updateIRQ(1, cpu);
114 }
115
116 // If periodic timer, reload
117 if (timers[cpu].pd && timers[cpu].reload) {
118 lupioTMRSet(cpu);
119 }
120}
121
122uint64_t
123LupioTMR::lupioTMRRead(uint8_t addr, int size)
124{
125 uint32_t r = 0;
126
127 size_t cpu = addr / LUPIO_TMR_MAX;
128 size_t reg = addr % LUPIO_TMR_MAX;
129
130 switch (reg) {
131 case LUPIO_TMR_TIME:
133 DPRINTF(LupioTMR, "Read LUPIO_TMR_TME: %d\n", r);
134 break;
135 case LUPIO_TMR_LOAD:
136 r = timers[cpu].reload;
137 DPRINTF(LupioTMR, "Read LUPIO_TMR_LOAD: %d\n", r);
138 break;
139 case LUPIO_TMR_STAT:
140 if (timers[cpu].expired) {
141 r |= LUPIO_TMR_EXPD;
142 }
143
144 // Acknowledge expiration
145 timers[cpu].expired = false;
146 DPRINTF(LupioTMR, "Read LUPIO_TMR_STAT: %d\n", r);
147 updateIRQ(0, cpu);
148 break;
149
150 default:
151 panic("Unexpected read to the LupioTMR device at address %#llx!",
152 addr);
153 break;
154 }
155 return r;
156}
157
158void
159LupioTMR::lupioTMRWrite(uint8_t addr, uint64_t val64, int size)
160{
161 uint32_t val = val64;
162
163 size_t cpu = addr / LUPIO_TMR_MAX;
164 size_t reg = addr % LUPIO_TMR_MAX;
165
166 switch (reg) {
167 case LUPIO_TMR_LOAD:
168 timers[cpu].reload = val;
169 DPRINTF(LupioTMR, "Write LUPIO_TMR_LOAD: %d\n",
170 timers[cpu].reload);
171 break;
172
173 case LUPIO_TMR_CTRL:
174 timers[cpu].ie = val & LUPIO_TMR_IRQE;
175 timers[cpu].pd = val & LUPIO_TMR_PRDC;
176 DPRINTF(LupioTMR, "Write LUPIO_TMR_CTRL\n");
177
178 // Stop current timer if any
179 if (curTick() < timers[cpu].startTime +
180 (timers[cpu].reload * sim_clock::as_int::ns) &&
181 (timers[cpu].tmrEvent)->scheduled()) {
182 deschedule(*(timers[cpu].tmrEvent));
183 }
184
185 // If reload isn't 0, start a new one
186 if (timers[cpu].reload) {
187 lupioTMRSet(cpu);
188 }
189 break;
190
191 default:
192 panic("Unexpected write to the LupioTMR device at address %#llx!",
193 addr);
194 break;
195 }
196}
197
198Tick
200{
201 Addr tmr_addr = pkt->getAddr() - pioAddr;
202
204 "Read request - addr: %#x, size: %#x\n", tmr_addr, pkt->getSize());
205
206 uint64_t read_val = lupioTMRRead(tmr_addr, pkt->getSize());
207 DPRINTF(LupioTMR, "Packet Read: %#x\n", read_val);
208 pkt->setUintX(read_val, byteOrder);
209 pkt->makeResponse();
210
211 return pioDelay;
212}
213
214Tick
216{
217 Addr tmr_addr = pkt->getAddr() - pioAddr;
218
219 DPRINTF(LupioTMR, "Write register %#x value %#x\n", tmr_addr,
220 pkt->getUintX(byteOrder));
221
222 lupioTMRWrite(tmr_addr, pkt->getUintX(byteOrder), pkt->getSize());
223 DPRINTF(LupioTMR, "Packet Write Value: %d\n", pkt->getUintX(byteOrder));
224
225 pkt->makeResponse();
226
227 return pioDelay;
228}
229} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
Addr pioAddr
Address that the device listens to.
Definition io_device.hh:151
BasicPioDevice(const Params &p, Addr size)
Definition io_device.cc:75
Tick pioDelay
Delay that the device experinces on an access.
Definition io_device.hh:157
void updateIRQ(int level, int cpu)
Post or clear timer interrupts.
Definition lupio_tmr.cc:74
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:215
void lupioTMRCallback(int cpu)
Process the timer's event.
Definition lupio_tmr.cc:108
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:123
System * system
Definition lupio_tmr.hh:50
LupioTMR(const Params &params)
Definition lupio_tmr.cc:47
void lupioTMRSet(int cpu)
Schedule the next timer event.
Definition lupio_tmr.cc:93
Tick read(PacketPtr pkt) override
Implement BasicPioDevice virtual functions.
Definition lupio_tmr.cc:199
uint64_t lupioTMRCurrentTime()
Return the simulated time.
Definition lupio_tmr.cc:87
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:159
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
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:220
const Params & params() const
#define LUPIO_TMR_IRQE
Definition lupio_tmr.cc:38
#define LUPIO_TMR_PRDC
Definition lupio_tmr.cc:39
#define LUPIO_TMR_EXPD
Definition lupio_tmr.cc:42
Bitfield< 0 > ie
Bitfield< 5, 3 > reg
Definition types.hh:92
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 Arm Limited 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
Packet * PacketPtr
const std::string & name()
Definition trace.cc:48

Generated on Mon Oct 27 2025 04:13:02 for gem5 by doxygen 1.14.0