2 * Copyright (c) 2013, 2015, 2017-2018,2020 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 "dev/arm/base_gic.hh"
44 #include "dev/arm/generic_timer_miscregs_types.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:
51 /// Arm ARM (ARM DDI 0487E.a)
52 /// D11.1.2 - The system counter
53 /// D11.2 - The AArch64 view of the Generic Timer
54 /// G6.2 - The AArch32 view of the Generic Timer
55 /// I2 - System Level Implementation of the Generic Timer
58 class SystemCounterParams;
59 class GenericTimerParams;
60 class GenericTimerFrameParams;
61 class GenericTimerMemParams;
63 /// Abstract class for elements whose events depend on the counting speed
64 /// of the System Counter
65 class SystemCounterListener : public Serializable
68 /// Called from the SystemCounter when a change in counting speed occurred
69 /// Events should be rescheduled properly inside this member function
70 virtual void notify(void) = 0;
73 /// Global system counter. It is shared by the architected and memory-mapped
75 class SystemCounter : public SimObject
78 /// Indicates if the counter is enabled
80 /// Counter frequency (as specified by CNTFRQ).
82 /// Counter value (as specified in CNTCV).
84 /// Value increment in each counter cycle
86 /// Frequency modes table with all possible frequencies for the counter
87 std::vector<uint32_t> _freqTable;
88 /// Currently selected entry in the table, its contents should match _freq
89 size_t _activeFreqEntry;
90 /// Cached copy of the counter period (inverse of the frequency).
92 /// Counter cycle start Tick when the counter status affecting
93 /// its value has been updated
96 /// Listeners to changes in counting speed
97 std::vector<SystemCounterListener *> _listeners;
99 /// Maximum architectural number of frequency table entries
100 static constexpr size_t MAX_FREQ_ENTRIES = 1004;
103 SystemCounter(SystemCounterParams *const p);
105 /// Validates a System Counter reference
106 /// @param sys_cnt System counter reference to validate
107 static void validateCounterRef(SystemCounter *sys_cnt);
109 /// Indicates if the counter is enabled.
110 bool enabled() const { return _enabled; }
111 /// Returns the counter frequency.
112 uint32_t freq() const { return _freq; }
113 /// Updates and returns the counter value.
115 /// Returns the value increment
116 uint64_t increment() const { return _increment; }
117 /// Returns a reference to the frequency modes table.
118 std::vector<uint32_t>& freqTable() { return _freqTable; }
119 /// Returns the currently active frequency table entry.
120 size_t activeFreqEntry() const { return _activeFreqEntry; }
121 /// Returns the counter period.
122 Tick period() const { return _period; }
124 /// Enables the counter after a CNTCR.EN == 1
126 /// Disables the counter after a CNTCR.EN == 0
129 /// Schedules a counter frequency update after a CNTCR.FCREQ == 1
130 /// This complies with frequency transitions as per the architecture
131 /// @param new_freq_entry Index in CNTFID of the new freq
132 void freqUpdateSchedule(size_t new_freq_entry);
134 /// Sets the value explicitly from writes to CNTCR.CNTCV
135 void setValue(uint64_t new_value);
137 /// Called from System Counter Listeners to register
138 void registerListener(SystemCounterListener *listener);
140 /// Returns the tick at which a certain counter value is reached
141 Tick whenValue(uint64_t target_val);
142 Tick whenValue(uint64_t cur_val, uint64_t target_val) const;
144 void serialize(CheckpointOut &cp) const override;
145 void unserialize(CheckpointIn &cp) override;
149 SystemCounter(const SystemCounter &c);
151 /// Frequency update event handling
152 EventFunctionWrapper _freqUpdateEvent;
153 size_t _nextFreqEntry;
154 /// Callback for the frequency update
155 void freqUpdateCallback();
157 /// Updates the counter value.
158 void updateValue(void);
160 /// Updates the update tick, normalizes to the lower cycle start tick
161 void updateTick(void);
163 /// Notifies counting speed changes to listeners
164 void notifyListeners(void) const;
167 /// Per-CPU architected timer.
168 class ArchTimer : public SystemCounterListener, public Drainable
171 /// Control register.
172 BitUnion32(ArchTimerCtrl)
176 EndBitUnion(ArchTimerCtrl)
178 /// Name of this timer.
179 const std::string _name;
181 /// Pointer to parent class.
184 SystemCounter &_systemCounter;
186 ArmInterruptPin * const _interrupt;
188 /// Value of the control register ({CNTP/CNTHP/CNTV}_CTL).
189 ArchTimerCtrl _control;
190 /// Programmed limit value for the upcounter ({CNTP/CNTHP/CNTV}_CVAL).
191 uint64_t _counterLimit;
192 /// Offset relative to the physical timer (CNTVOFF)
196 * Timer settings or the offset has changed, re-evaluate
197 * trigger condition and raise interrupt if necessary.
199 void updateCounter();
201 /// Called when the upcounter reaches the programmed value.
202 void counterLimitReached();
203 EventFunctionWrapper _counterLimitReachedEvent;
205 virtual bool scheduleEvents() { return true; }
208 ArchTimer(const std::string &name,
210 SystemCounter &sysctr,
211 ArmInterruptPin *interrupt);
213 /// Returns the timer name.
214 std::string name() const { return _name; }
216 /// Returns the CompareValue view of the timer.
217 uint64_t compareValue() const { return _counterLimit; }
218 /// Sets the CompareValue view of the timer.
219 void setCompareValue(uint64_t val);
221 /// Returns the TimerValue view of the timer.
222 uint32_t timerValue() const { return _counterLimit - value(); }
223 /// Sets the TimerValue view of the timer.
224 void setTimerValue(uint32_t val);
226 /// Sets the control register.
227 uint32_t control() const { return _control; }
228 void setControl(uint32_t val);
230 uint64_t offset() const { return _offset; }
231 void setOffset(uint64_t val);
233 /// Returns the value of the counter which this timer relies on.
234 uint64_t value() const;
235 Tick whenValue(uint64_t target_val) {
236 return _systemCounter.whenValue(value(), target_val);
239 void notify(void) override;
242 void serialize(CheckpointOut &cp) const override;
243 void unserialize(CheckpointIn &cp) override;
246 DrainState drain() override;
247 void drainResume() override;
251 ArchTimer(const ArchTimer &t);
254 class ArchTimerKvm : public ArchTimer
260 ArchTimerKvm(const std::string &name,
263 SystemCounter &sysctr,
264 ArmInterruptPin *interrupt)
265 : ArchTimer(name, parent, sysctr, interrupt), system(system) {}
268 // For ArchTimer's in a GenericTimerISA with Kvm execution about
269 // to begin, skip rescheduling the event.
270 // Otherwise, we should reschedule the event (if necessary).
271 bool scheduleEvents() override {
272 return !system.validKvmEnvironment();
276 class GenericTimer : public SimObject
279 const GenericTimerParams * params() const;
281 GenericTimer(GenericTimerParams *const p);
283 void serialize(CheckpointOut &cp) const override;
284 void unserialize(CheckpointIn &cp) override;
287 void setMiscReg(int misc_reg, unsigned cpu, RegVal val);
288 RegVal readMiscReg(int misc_reg, unsigned cpu);
291 class CoreTimers : public SystemCounterListener
294 CoreTimers(GenericTimer &_parent, ArmSystem &system, unsigned cpu,
295 ArmInterruptPin *_irqPhysS, ArmInterruptPin *_irqPhysNS,
296 ArmInterruptPin *_irqVirt, ArmInterruptPin *_irqHyp)
298 threadContext(system.getThreadContext(cpu)),
300 irqPhysNS(_irqPhysNS),
303 physS(csprintf("%s.phys_s_timer%d", parent.name(), cpu),
304 system, parent, parent.systemCounter,
306 // This should really be phys_timerN, but we are stuck with
307 // arch_timer for backwards compatibility.
308 physNS(csprintf("%s.arch_timer%d", parent.name(), cpu),
309 system, parent, parent.systemCounter,
311 virt(csprintf("%s.virt_timer%d", parent.name(), cpu),
312 system, parent, parent.systemCounter,
314 hyp(csprintf("%s.hyp_timer%d", parent.name(), cpu),
315 system, parent, parent.systemCounter,
318 EventFunctionWrapper([this]{ physEventStreamCallback(); },
319 csprintf("%s.phys_event_gen%d", parent.name(), cpu)), 0, 0
322 EventFunctionWrapper([this]{ virtEventStreamCallback(); },
323 csprintf("%s.virt_event_gen%d", parent.name(), cpu)), 0, 0
329 /// System counter frequency as visible from this core
332 /// Kernel control register
335 /// Hypervisor control register
338 /// Generic Timer parent reference
339 GenericTimer &parent;
341 /// Thread (HW) context associated to this PE implementation
342 ThreadContext *threadContext;
344 ArmInterruptPin const *irqPhysS;
345 ArmInterruptPin const *irqPhysNS;
346 ArmInterruptPin const *irqVirt;
347 ArmInterruptPin const *irqHyp;
354 // Event Stream. Events are generated based on a configurable
355 // transitionBit over the counter value. transitionTo indicates
356 // the transition direction (0->1 or 1->0)
359 EventFunctionWrapper event;
360 uint8_t transitionTo;
361 uint8_t transitionBit;
364 eventTargetValue(uint64_t val) const
366 uint64_t bit_val = bits(val, transitionBit);
367 uint64_t ret_val = mbits(val, 63, transitionBit);
368 uint64_t incr_val = 1 << transitionBit;
369 if (bit_val == transitionTo)
371 return ret_val + incr_val;
375 EventStream physEvStream;
376 EventStream virtEvStream;
377 void physEventStreamCallback();
378 void virtEventStreamCallback();
379 void eventStreamCallback() const;
380 void schedNextEvent(EventStream &ev_stream, ArchTimer &timer);
382 void notify(void) override;
384 void serialize(CheckpointOut &cp) const override;
385 void unserialize(CheckpointIn &cp) override;
389 CoreTimers(const CoreTimers &c);
392 CoreTimers &getTimers(int cpu_id);
393 void createTimers(unsigned cpus);
395 /// System counter reference.
396 SystemCounter &systemCounter;
398 /// Per-CPU physical architected timers.
399 std::vector<std::unique_ptr<CoreTimers>> timers;
401 protected: // Configuration
402 /// ARM system containing this timer
405 void handleStream(CoreTimers::EventStream *ev_stream,
406 ArchTimer *timer, RegVal old_cnt_ctl, RegVal cnt_ctl);
409 class GenericTimerISA : public ArmISA::BaseISADevice
412 GenericTimerISA(GenericTimer &_parent, unsigned _cpu)
413 : parent(_parent), cpu(_cpu) {}
415 void setMiscReg(int misc_reg, RegVal val) override;
416 RegVal readMiscReg(int misc_reg) override;
419 GenericTimer &parent;
423 class GenericTimerFrame : public PioDevice
426 GenericTimerFrame(GenericTimerFrameParams *const p);
428 void serialize(CheckpointOut &cp) const override;
429 void unserialize(CheckpointIn &cp) override;
431 /// Indicates if this frame implements a virtual timer
432 bool hasVirtualTimer() const;
434 /// Returns the virtual offset for this frame if a virtual timer is
436 uint64_t getVirtOffset() const;
438 /// Sets the virtual offset for this frame's virtual timer after
439 /// a write to CNTVOFF
440 void setVirtOffset(uint64_t new_offset);
442 /// Indicates if this frame implements a second EL0 view
443 bool hasEl0View() const;
445 /// Returns the access bits for this frame
446 uint8_t getAccessBits() const;
448 /// Updates the access bits after a write to CNTCTLBase.CNTACR
449 void setAccessBits(uint8_t data);
451 /// Indicates if non-secure accesses are allowed to this frame
452 bool hasNonSecureAccess() const;
454 /// Allows non-secure accesses after an enabling write to
455 /// CNTCTLBase.CNTNSAR
456 void setNonSecureAccess();
458 /// Indicates if CNTVOFF is readable for this frame
459 bool hasReadableVoff() const;
462 AddrRangeList getAddrRanges() const override;
463 Tick read(PacketPtr pkt) override;
464 Tick write(PacketPtr pkt) override;
467 /// CNTBase/CNTEL0Base (Memory-mapped timer frame)
468 uint64_t timerRead(Addr addr, size_t size, bool is_sec, bool to_el0) const;
469 void timerWrite(Addr addr, size_t size, uint64_t data, bool is_sec,
471 const AddrRange timerRange;
472 AddrRange timerEl0Range;
474 static const Addr TIMER_CNTPCT_LO = 0x00;
475 static const Addr TIMER_CNTPCT_HI = 0x04;
476 static const Addr TIMER_CNTVCT_LO = 0x08;
477 static const Addr TIMER_CNTVCT_HI = 0x0c;
478 static const Addr TIMER_CNTFRQ = 0x10;
479 static const Addr TIMER_CNTEL0ACR = 0x14;
480 static const Addr TIMER_CNTVOFF_LO = 0x18;
481 static const Addr TIMER_CNTVOFF_HI = 0x1c;
482 static const Addr TIMER_CNTP_CVAL_LO = 0x20;
483 static const Addr TIMER_CNTP_CVAL_HI = 0x24;
484 static const Addr TIMER_CNTP_TVAL = 0x28;
485 static const Addr TIMER_CNTP_CTL = 0x2c;
486 static const Addr TIMER_CNTV_CVAL_LO = 0x30;
487 static const Addr TIMER_CNTV_CVAL_HI = 0x34;
488 static const Addr TIMER_CNTV_TVAL = 0x38;
489 static const Addr TIMER_CNTV_CTL = 0x3c;
491 /// All MMIO ranges GenericTimerFrame responds to
492 AddrRangeList addrRanges;
494 /// System counter reference.
495 SystemCounter &systemCounter;
497 /// Physical and virtual timers
501 /// Reports access properties of the CNTBase register frame elements
502 BitUnion8(AccessBits)
509 EndBitUnion(AccessBits)
510 AccessBits accessBits;
512 // Reports access properties of the CNTEL0Base register frame elements
513 BitUnion16(AccessBitsEl0)
518 EndBitUnion(AccessBitsEl0)
519 AccessBitsEl0 accessBitsEl0;
521 /// Reports whether non-secure accesses are allowed to this frame
522 bool nonSecureAccess;
527 class GenericTimerMem : public PioDevice
530 GenericTimerMem(GenericTimerMemParams *const p);
532 /// Validates a Generic Timer register frame address range
533 /// @param base_addr Range of the register frame
534 static void validateFrameRange(const AddrRange &range);
536 /// Validates an MMIO access permissions
537 /// @param sys System reference where the acces is being made
538 /// @param is_sec If the access is to secure memory
539 static bool validateAccessPerm(ArmSystem &sys, bool is_sec);
542 AddrRangeList getAddrRanges() const override;
543 Tick read(PacketPtr pkt) override;
544 Tick write(PacketPtr pkt) override;
547 /// CNTControlBase (System counter control frame)
548 uint64_t counterCtrlRead(Addr addr, size_t size, bool is_sec) const;
549 void counterCtrlWrite(Addr addr, size_t size, uint64_t data, bool is_sec);
550 const AddrRange counterCtrlRange;
553 Bitfield<17,8> fcreq;
560 Bitfield<31,8> fcack;
563 static const Addr COUNTER_CTRL_CNTCR = 0x00;
564 static const Addr COUNTER_CTRL_CNTSR = 0x04;
565 static const Addr COUNTER_CTRL_CNTCV_LO = 0x08;
566 static const Addr COUNTER_CTRL_CNTCV_HI = 0x0c;
567 static const Addr COUNTER_CTRL_CNTSCR = 0x10;
568 static const Addr COUNTER_CTRL_CNTID = 0x1c;
569 static const Addr COUNTER_CTRL_CNTFID = 0x20;
571 /// CNTReadBase (System counter status frame)
572 uint64_t counterStatusRead(Addr addr, size_t size) const;
573 void counterStatusWrite(Addr addr, size_t size, uint64_t data);
574 const AddrRange counterStatusRange;
576 static const Addr COUNTER_STATUS_CNTCV_LO = 0x00;
577 static const Addr COUNTER_STATUS_CNTCV_HI = 0x04;
579 /// CNTCTLBase (Memory-mapped timer global control frame)
580 uint64_t timerCtrlRead(Addr addr, size_t size, bool is_sec) const;
581 void timerCtrlWrite(Addr addr, size_t size, uint64_t data, bool is_sec);
582 const AddrRange timerCtrlRange;
584 /// ID register for reporting features of implemented timer frames
587 static const Addr TIMER_CTRL_CNTFRQ = 0x00;
588 static const Addr TIMER_CTRL_CNTNSAR = 0x04;
589 static const Addr TIMER_CTRL_CNTTIDR = 0x08;
590 static const Addr TIMER_CTRL_CNTACR = 0x40;
591 static const Addr TIMER_CTRL_CNTVOFF_LO = 0x80;
592 static const Addr TIMER_CTRL_CNTVOFF_HI = 0x84;
594 /// All MMIO ranges GenericTimerMem responds to
595 const AddrRangeList addrRanges;
597 /// System counter reference.
598 SystemCounter &systemCounter;
600 /// Maximum architectural number of memory-mapped timer frames
601 static constexpr size_t MAX_TIMER_FRAMES = 8;
603 /// Timer frame references
604 std::vector<GenericTimerFrame *> frames;
609 #endif // __DEV_ARM_GENERIC_TIMER_HH__