2 * Copyright (c) 2013, 2015, 2017-2018, 2019 ARM Limited
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.
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.
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.
38 #ifndef __DEV_ARM_GENERIC_TIMER_HH__
39 #define __DEV_ARM_GENERIC_TIMER_HH__
41 #include "arch/arm/isa_device.hh"
42 #include "arch/arm/system.hh"
43 #include "base/bitunion.hh"
44 #include "dev/arm/base_gic.hh"
45 #include "sim/core.hh"
46 #include "sim/sim_object.hh"
49 /// This module implements the global system counter and the local per-CPU
50 /// architected timers as specified by the ARM Generic Timer extension (ARM
51 /// ARM, Issue C, Chapter 17).
54 class GenericTimerParams;
55 class GenericTimerMemParams;
57 /// Global system counter. It is shared by the architected timers.
58 /// @todo: implement memory-mapped controls
59 class SystemCounter : public Serializable
62 /// Counter frequency (as specified by CNTFRQ).
64 /// Frequency modes table with all possible frequencies for the counter
65 std::vector<uint32_t> _freqTable;
66 /// Cached copy of the counter period (inverse of the frequency).
68 /// Tick when the counter was reset.
71 /// Kernel event stream control register
73 /// Hypervisor event stream control register
76 /// Maximum architectural number of frequency table entries
77 static constexpr size_t MAX_FREQ_ENTRIES = 1004;
80 SystemCounter(std::vector<uint32_t> &freqs);
82 /// Returns the current value of the physical counter.
83 uint64_t value() const
86 return 0; // Counter is still off.
87 return (curTick() - _resetTick) / _period;
90 /// Returns the counter frequency.
91 uint32_t freq() const { return _freq; }
92 /// Sets the counter frequency.
93 /// @param freq frequency in Hz.
94 void setFreq(uint32_t freq);
96 /// Returns the counter period.
97 Tick period() const { return _period; }
99 void setKernelControl(uint32_t val) { _regCntkctl = val; }
100 uint32_t getKernelControl() { return _regCntkctl; }
102 void setHypControl(uint32_t val) { _regCnthctl = val; }
103 uint32_t getHypControl() { return _regCnthctl; }
105 void serialize(CheckpointOut &cp) const override;
106 void unserialize(CheckpointIn &cp) override;
110 SystemCounter(const SystemCounter &c);
113 /// Per-CPU architected timer.
114 class ArchTimer : public Serializable, public Drainable
117 /// Control register.
118 BitUnion32(ArchTimerCtrl)
122 EndBitUnion(ArchTimerCtrl)
124 /// Name of this timer.
125 const std::string _name;
127 /// Pointer to parent class.
130 SystemCounter &_systemCounter;
132 ArmInterruptPin * const _interrupt;
134 /// Value of the control register ({CNTP/CNTHP/CNTV}_CTL).
135 ArchTimerCtrl _control;
136 /// Programmed limit value for the upcounter ({CNTP/CNTHP/CNTV}_CVAL).
137 uint64_t _counterLimit;
138 /// Offset relative to the physical timer (CNTVOFF)
142 * Timer settings or the offset has changed, re-evaluate
143 * trigger condition and raise interrupt if necessary.
145 void updateCounter();
147 /// Called when the upcounter reaches the programmed value.
148 void counterLimitReached();
149 EventFunctionWrapper _counterLimitReachedEvent;
151 virtual bool scheduleEvents() { return true; }
154 ArchTimer(const std::string &name,
156 SystemCounter &sysctr,
157 ArmInterruptPin *interrupt);
159 /// Returns the timer name.
160 std::string name() const { return _name; }
162 /// Returns the CompareValue view of the timer.
163 uint64_t compareValue() const { return _counterLimit; }
164 /// Sets the CompareValue view of the timer.
165 void setCompareValue(uint64_t val);
167 /// Returns the TimerValue view of the timer.
168 uint32_t timerValue() const { return _counterLimit - value(); }
169 /// Sets the TimerValue view of the timer.
170 void setTimerValue(uint32_t val);
172 /// Sets the control register.
173 uint32_t control() const { return _control; }
174 void setControl(uint32_t val);
176 uint64_t offset() const { return _offset; }
177 void setOffset(uint64_t val);
179 /// Returns the value of the counter which this timer relies on.
180 uint64_t value() const;
183 void serialize(CheckpointOut &cp) const override;
184 void unserialize(CheckpointIn &cp) override;
187 DrainState drain() override;
188 void drainResume() override;
192 ArchTimer(const ArchTimer &t);
195 class ArchTimerKvm : public ArchTimer
201 ArchTimerKvm(const std::string &name,
204 SystemCounter &sysctr,
205 ArmInterruptPin *interrupt)
206 : ArchTimer(name, parent, sysctr, interrupt), system(system) {}
209 // For ArchTimer's in a GenericTimerISA with Kvm execution about
210 // to begin, skip rescheduling the event.
211 // Otherwise, we should reschedule the event (if necessary).
212 bool scheduleEvents() override {
213 return !system.validKvmEnvironment();
217 class GenericTimer : public ClockedObject
220 const GenericTimerParams * params() const;
222 GenericTimer(GenericTimerParams *p);
224 void serialize(CheckpointOut &cp) const override;
225 void unserialize(CheckpointIn &cp) override;
228 void setMiscReg(int misc_reg, unsigned cpu, RegVal val);
229 RegVal readMiscReg(int misc_reg, unsigned cpu);
233 CoreTimers(GenericTimer &parent, ArmSystem &system, unsigned cpu,
234 ArmInterruptPin *_irqPhysS, ArmInterruptPin *_irqPhysNS,
235 ArmInterruptPin *_irqVirt, ArmInterruptPin *_irqHyp)
236 : irqPhysS(_irqPhysS),
237 irqPhysNS(_irqPhysNS),
240 physS(csprintf("%s.phys_s_timer%d", parent.name(), cpu),
241 system, parent, parent.systemCounter,
243 // This should really be phys_timerN, but we are stuck with
244 // arch_timer for backwards compatibility.
245 physNS(csprintf("%s.arch_timer%d", parent.name(), cpu),
246 system, parent, parent.systemCounter,
248 virt(csprintf("%s.virt_timer%d", parent.name(), cpu),
249 system, parent, parent.systemCounter,
251 hyp(csprintf("%s.hyp_timer%d", parent.name(), cpu),
252 system, parent, parent.systemCounter,
256 ArmInterruptPin const *irqPhysS;
257 ArmInterruptPin const *irqPhysNS;
258 ArmInterruptPin const *irqVirt;
259 ArmInterruptPin const *irqHyp;
268 CoreTimers(const CoreTimers &c);
271 CoreTimers &getTimers(int cpu_id);
272 void createTimers(unsigned cpus);
275 SystemCounter systemCounter;
277 /// Per-CPU physical architected timers.
278 std::vector<std::unique_ptr<CoreTimers>> timers;
280 protected: // Configuration
281 /// ARM system containing this timer
285 class GenericTimerISA : public ArmISA::BaseISADevice
288 GenericTimerISA(GenericTimer &_parent, unsigned _cpu)
289 : parent(_parent), cpu(_cpu) {}
291 void setMiscReg(int misc_reg, RegVal val) override;
292 RegVal readMiscReg(int misc_reg) override;
295 GenericTimer &parent;
299 class GenericTimerMem : public PioDevice
302 GenericTimerMem(GenericTimerMemParams *p);
304 void serialize(CheckpointOut &cp) const override;
305 void unserialize(CheckpointIn &cp) override;
308 AddrRangeList getAddrRanges() const override { return addrRanges; }
309 Tick read(PacketPtr pkt) override;
310 Tick write(PacketPtr pkt) override;
313 uint64_t ctrlRead(Addr addr, size_t size) const;
314 void ctrlWrite(Addr addr, size_t size, uint64_t value);
316 uint64_t timerRead(Addr addr, size_t size) const;
317 void timerWrite(Addr addr, size_t size, uint64_t value);
319 protected: // Registers
320 static const Addr CTRL_CNTFRQ = 0x000;
321 static const Addr CTRL_CNTNSAR = 0x004;
322 static const Addr CTRL_CNTTIDR = 0x008;
323 static const Addr CTRL_CNTACR_BASE = 0x040;
324 static const Addr CTRL_CNTVOFF_LO_BASE = 0x080;
325 static const Addr CTRL_CNTVOFF_HI_BASE = 0x084;
327 static const Addr TIMER_CNTPCT_LO = 0x000;
328 static const Addr TIMER_CNTPCT_HI = 0x004;
329 static const Addr TIMER_CNTVCT_LO = 0x008;
330 static const Addr TIMER_CNTVCT_HI = 0x00C;
331 static const Addr TIMER_CNTFRQ = 0x010;
332 static const Addr TIMER_CNTEL0ACR = 0x014;
333 static const Addr TIMER_CNTVOFF_LO = 0x018;
334 static const Addr TIMER_CNTVOFF_HI = 0x01C;
335 static const Addr TIMER_CNTP_CVAL_LO = 0x020;
336 static const Addr TIMER_CNTP_CVAL_HI = 0x024;
337 static const Addr TIMER_CNTP_TVAL = 0x028;
338 static const Addr TIMER_CNTP_CTL = 0x02C;
339 static const Addr TIMER_CNTV_CVAL_LO = 0x030;
340 static const Addr TIMER_CNTV_CVAL_HI = 0x034;
341 static const Addr TIMER_CNTV_TVAL = 0x038;
342 static const Addr TIMER_CNTV_CTL = 0x03C;
345 const AddrRange ctrlRange;
346 const AddrRange timerRange;
347 const AddrRangeList addrRanges;
351 SystemCounter systemCounter;
356 #endif // __DEV_ARM_GENERIC_TIMER_HH__