gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
timer_sp804.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 ARM Limited
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
39
40#include <cassert>
41
42#include "base/intmath.hh"
43#include "base/logging.hh"
44#include "base/trace.hh"
45#include "debug/Checkpoint.hh"
46#include "debug/Timer.hh"
47#include "dev/arm/base_gic.hh"
48#include "mem/packet.hh"
49#include "mem/packet_access.hh"
50#include "params/Sp804.hh"
51
52namespace gem5
53{
54
56 : AmbaPioDevice(p, 0x1000),
57 timer0(name() + ".timer0", this, p.int0->get(), p.clock0),
58 timer1(name() + ".timer1", this, p.int1->get(), p.clock1)
59{
60}
61
62Sp804::Timer::Timer(std::string __name, Sp804 *_parent,
63 ArmInterruptPin *_interrupt, Tick _clock)
64 : _name(__name), parent(_parent), interrupt(_interrupt),
65 clock(_clock), control(0x20),
66 rawInt(false), pendingInt(false), loadValue(0xffffffff),
67 zeroEvent([this]{ counterAtZero(); }, name())
68{
69}
70
71
72Tick
74{
75 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
76 assert(pkt->getSize() == 4);
77 Addr daddr = pkt->getAddr() - pioAddr;
78 DPRINTF(Timer, "Reading from DualTimer at offset: %#x\n", daddr);
79
80 if (daddr < Timer::Size)
81 timer0.read(pkt, daddr);
82 else if ((daddr - Timer::Size) < Timer::Size)
83 timer1.read(pkt, daddr - Timer::Size);
84 else if (!readId(pkt, ambaId, pioAddr))
85 panic("Tried to read SP804 at offset %#x that doesn't exist\n", daddr);
86 pkt->makeAtomicResponse();
87 return pioDelay;
88}
89
90
91void
93{
94 switch(daddr) {
95 case LoadReg:
96 pkt->setLE<uint32_t>(loadValue);
97 break;
98 case CurrentReg:
99 DPRINTF(Timer, "Event schedule for %d, clock=%d, prescale=%d\n",
100 zeroEvent.when(), clock, control.timerPrescale);
101 Tick time;
102 time = zeroEvent.when() - curTick();
103 time = (time / clock) >> (4 * control.timerPrescale);
104 DPRINTF(Timer, "-- returning counter at %d\n", time);
105 pkt->setLE<uint32_t>(time);
106 break;
107 case ControlReg:
108 pkt->setLE<uint32_t>(control);
109 break;
110 case RawISR:
111 pkt->setLE<uint32_t>(rawInt);
112 break;
113 case MaskedISR:
114 pkt->setLE<uint32_t>(pendingInt);
115 break;
116 case BGLoad:
117 pkt->setLE<uint32_t>(loadValue);
118 break;
119 default:
120 panic("Tried to read SP804 timer at offset %#x\n", daddr);
121 break;
122 }
123 DPRINTF(Timer, "Reading %#x from Timer at offset: %#x\n",
124 pkt->getLE<uint32_t>(), daddr);
125}
126
127Tick
129{
130 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
131 assert(pkt->getSize() == 4);
132 Addr daddr = pkt->getAddr() - pioAddr;
133 DPRINTF(Timer, "Writing to DualTimer at offset: %#x\n", daddr);
134
135 if (daddr < Timer::Size)
136 timer0.write(pkt, daddr);
137 else if ((daddr - Timer::Size) < Timer::Size)
138 timer1.write(pkt, daddr - Timer::Size);
139 else if (!readId(pkt, ambaId, pioAddr))
140 panic("Tried to write SP804 at offset %#x that doesn't exist\n", daddr);
141 pkt->makeAtomicResponse();
142 return pioDelay;
143}
144
145void
147{
148 DPRINTF(Timer, "Writing %#x to Timer at offset: %#x\n",
149 pkt->getLE<uint32_t>(), daddr);
150 switch (daddr) {
151 case LoadReg:
152 loadValue = pkt->getLE<uint32_t>();
154 break;
155 case CurrentReg:
156 // Spec says this value can't be written, but linux writes it anyway
157 break;
158 case ControlReg:
159 bool old_enable;
160 old_enable = control.timerEnable;
161 control = pkt->getLE<uint32_t>();
162 if ((old_enable == 0) && control.timerEnable)
164 break;
165 case IntClear:
166 rawInt = false;
167 if (pendingInt) {
168 pendingInt = false;
169 DPRINTF(Timer, "Clearing interrupt\n");
170 interrupt->clear();
171 }
172 break;
173 case BGLoad:
174 loadValue = pkt->getLE<uint32_t>();
175 break;
176 default:
177 panic("Tried to write SP804 timer at offset %#x\n", daddr);
178 break;
179 }
180}
181
182void
184{
185 DPRINTF(Timer, "Resetting counter with value %#x\n", val);
186 if (!control.timerEnable)
187 return;
188
189 Tick time = clock << (4 * control.timerPrescale);
190 if (control.timerSize)
191 time *= val;
192 else
193 time *= bits(val,15,0);
194
195 if (zeroEvent.scheduled()) {
196 DPRINTF(Timer, "-- Event was already schedule, de-scheduling\n");
197 parent->deschedule(zeroEvent);
198 }
199 parent->schedule(zeroEvent, curTick() + time);
200 DPRINTF(Timer, "-- Scheduling new event for: %d\n", curTick() + time);
201}
202
203void
205{
206 if (!control.timerEnable)
207 return;
208
209 DPRINTF(Timer, "Counter reached zero\n");
210
211 rawInt = true;
212 bool old_pending = pendingInt;
213 if (control.intEnable)
214 pendingInt = true;
215 if (pendingInt && !old_pending) {
216 DPRINTF(Timer, "-- Causing interrupt\n");
217 interrupt->raise();
218 }
219
220 if (control.oneShot)
221 return;
222
223 // Free-running
224 if (control.timerMode == 0)
225 restartCounter(0xffffffff);
226 else
228}
229
230void
232{
233 DPRINTF(Checkpoint, "Serializing Arm Sp804\n");
234
235 uint32_t control_serial = control;
236 SERIALIZE_SCALAR(control_serial);
237
241
242 bool is_in_event = zeroEvent.scheduled();
243 SERIALIZE_SCALAR(is_in_event);
244
245 Tick event_time;
246 if (is_in_event){
247 event_time = zeroEvent.when();
248 SERIALIZE_SCALAR(event_time);
249 }
250}
251
252void
254{
255 DPRINTF(Checkpoint, "Unserializing Arm Sp804\n");
256
257 uint32_t control_serial;
258 UNSERIALIZE_SCALAR(control_serial);
259 control = control_serial;
260
264
265 bool is_in_event;
266 UNSERIALIZE_SCALAR(is_in_event);
267
268 Tick event_time;
269 if (is_in_event){
270 UNSERIALIZE_SCALAR(event_time);
271 parent->schedule(zeroEvent, event_time);
272 }
273}
274
275
276
277void
279{
280 timer0.serializeSection(cp, "timer0");
281 timer1.serializeSection(cp, "timer1");
282}
283
284void
286{
287 timer0.unserializeSection(cp, "timer0");
288 timer1.unserializeSection(cp, "timer1");
289}
290
291} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
Base class for ARM GIC implementations.
bool readId(PacketPtr pkt, uint64_t amba_id, Addr pio_addr)
AmbaPioDevice(const Params &p, Addr pio_size)
Generic representation of an Arm interrupt pin.
Definition base_gic.hh:200
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
Addr pioSize
Size that the device's address range.
Definition io_device.hh:154
const std::string _name
Definition named.hh:54
Addr getAddr() const
Definition packet.hh:807
void setLE(T v)
Set the value in the data pointer to v as little endian.
unsigned getSize() const
Definition packet.hh:817
void makeAtomicResponse()
Definition packet.hh:1074
T getLE() const
Get the data in the packet byte swapped from little endian to host endian.
EndBitUnion(CTRL) protected Sp804 * parent
Pointer to parent class.
Timer(std::string __name, Sp804 *parent, ArmInterruptPin *_interrupt, Tick clock)
CTRL control
Control register as specified above.
void counterAtZero()
Called when the counter reaches 0.
uint32_t loadValue
Value to load into counter when periodic mode reaches 0.
bool pendingInt
If an interrupt is currently pending.
void restartCounter(uint32_t val)
Restart the counter ticking at val.
const Tick clock
Number of ticks in a clock input.
EventFunctionWrapper zeroEvent
void serialize(CheckpointOut &cp) const override
Serialize an object.
void write(PacketPtr pkt, Addr daddr)
Handle write for a single timer.
bool rawInt
If timer has caused an interrupt.
void read(PacketPtr pkt, Addr daddr)
Handle read for a single timer.
ArmInterruptPin *const interrupt
Pointer to the interrupt pin.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Sp804Params Params
void serialize(CheckpointOut &cp) const override
Serialize an object.
Timer timer0
Timers that do the actual work.
Sp804(const Params &p)
The constructor for RealView just registers itself with the MMU.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Tick read(PacketPtr pkt) override
Handle a read to the device.
Tick write(PacketPtr pkt) override
All writes are simply ignored.
bool int1
Definition common.h:43
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition bitfield.hh:79
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:220
Bitfield< 0 > p
Bitfield< 63 > val
Definition misc.hh:804
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
Tick curTick()
The universal simulation clock.
Definition cur_tick.hh:46
std::ostream CheckpointOut
Definition serialize.hh:66
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
Declaration of the Packet class.
#define UNSERIALIZE_SCALAR(scalar)
Definition serialize.hh:575
#define SERIALIZE_SCALAR(scalar)
Definition serialize.hh:568
This implements the dual Sp804 timer block.
const std::string & name()
Definition trace.cc:48

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