4.6 The Generic Timer
The Generic Timer has two halves: one system counter, a free-running count of system time that lives in an always-on domain outside the PEs, and per-PE timers, comparators built into each PE that read that count and raise a PPI. A system can also expose memory-mapped timer frames that raise SPIs and keep working while PEs are off. This section covers the programmer’s model. How to read the count without being caught by speculation, and the v8.6 changes, are in 4.7. Virtualizing the timer for guests is in 11.4.
The counter views
| Register | Value | Notes |
|---|---|---|
CNTPCT_EL0 | Physical count | The system count as broadcast to this PE (minus CNTPOFF_EL2 at EL0/EL1 when FEAT_ECV’s physical offset is enabled, see 4.7) |
CNTVCT_EL0 | Physical count − CNTVOFF_EL2 | The offset applies to reads from EL1 and EL3, from EL0 unless HCR_EL2.{E2H,TGE} = {1,1}, and from EL2 only when E2H = 0. When EL2 is not implemented the offset is zero |
CNTFRQ_EL0 | Counter frequency in Hz | Informational only; see below |
The counter is at least 56 bits wide, with a roll-over time of no less than 40 years at its frequency. From Armv8.6 it is 64 bits and counts at 1 GHz (4.7). It must count monotonically and give a uniform view of time across all PEs. That requirement is easy on one die and much harder across chiplets.
CNTFRQ_EL0 does not set the frequency. It is a register that firmware writes so that software can read the frequency. Only the highest implemented EL can write it (EL3 in a TF-A system), and its reset value is architecturally UNKNOWN. Firmware takes the real value from the platform (the frequency of the entry in the CNTFID<n> table that it selected, see below) and writes it on every PE, including on warm boot if the register is not retained. A wrong CNTFRQ_EL0 does not stop timers. It makes every derived time value wrong by a constant factor: delays too short or too long, and a Linux clocksource with the wrong rate.
The per-PE timers
Each timer has the same three registers: CVAL (64-bit compare value), TVAL (32-bit signed down-counter view) and CTL.
| Timer | Registers | Counter compared | Present | Typical INTID (BSA) |
|---|---|---|---|---|
| EL1 physical | CNTP_CTL_EL0, CNTP_CVAL_EL0, CNTP_TVAL_EL0 | physical | always | 30 |
| EL1 virtual | CNTV_*_EL0 | virtual | always | 27 |
| Secure physical (EL3 / Secure EL1) | CNTPS_*_EL1 | physical | with EL3 | 29 |
| Non-secure EL2 physical | CNTHP_*_EL2 | physical | with EL2 | 26 |
| Non-secure EL2 virtual | CNTHV_*_EL2 | virtual | FEAT_VHE | 28 |
| Secure EL2 physical | CNTHPS_*_EL2 | physical | FEAT_SEL2 | see BSA |
| Secure EL2 virtual | CNTHVS_*_EL2 | virtual | FEAT_SEL2 | see BSA |
The INTIDs are the Arm BSA recommendations, not architecture. Read them from the device tree or the ACPI GTDT rather than hard-coding them. The architecture defines each timer’s output as a PPI towards its own PE, so each PE has a private instance of every timer. With RME, the EL2 timers in Realm state and their naming are given in DDI0487’s Generic Timer chapter.
With HCR_EL2.E2H = 1, EL2 accesses to CNTP_*_EL0 and CNTV_*_EL0 are redirected to the EL2 timers (CNTHP_*, CNTHV_*). EL2 reaches the EL1 timers through the *_EL02 aliases. Code that runs at both EL1 and EL2 must allow for this.
CTL, CVAL and TVAL
CNTx_CTL holds ENABLE (bit 0), IMASK (bit 1) and ISTATUS (bit 2, read-only).
- The timer condition is met when
CVAL ≤ count, an unsigned 64-bit comparison against the counter the timer uses (physical or virtual). ISTATUSreflects the condition whileENABLE = 1. The interrupt output is asserted whileENABLE = 1,ISTATUS = 1andIMASK = 0.- Writing
TVALsetsCVAL = count + SignExtend(TVAL). ReadingTVALreturns(CVAL − count)[31:0], which goes negative after the deadline passes. BecauseTVALis 32 bits, a 1 GHz counter cannot be programmed more than about 2.1 s ahead throughTVAL. ProgramCVALdirectly instead (Linux switched toCVALfor this reason).
The output is level-sensitive. Acknowledging and EOIing the PPI does not clear it, because the condition is still true. The handler must move CVAL forward, set IMASK, or clear ENABLE before EOI, or it re-enters immediately. Configure the PPI as level in GICR_ICFGR1 if that bit is programmable (4.5).
// Arm the EL1 physical timer to fire 'x1' ticks from now, using CVAL
mrs x0, CNTPCT_EL0 // see 4.7 for the ordering needed here
add x0, x0, x1
msr CNTP_CVAL_EL0, x0
mov x0, #1 // ENABLE=1, IMASK=0
msr CNTP_CTL_EL0, x0
isb // make the new timer state take effect before continuing
Who may access what
| Control | Effect |
|---|---|
CNTKCTL_EL1.EL0PCTEN (bit 0) / EL0VCTEN (bit 1) | EL0 reads of CNTPCT_EL0 / CNTVCT_EL0 (and CNTFRQ_EL0) |
CNTKCTL_EL1.EL0VTEN (bit 8) / EL0PTEN (bit 9) | EL0 access to the virtual / physical timer registers |
CNTHCTL_EL2.EL1PCTEN / EL1PCEN (bits 0/1 when E2H = 0; bits 10/11 as EL1PCTEN/EL1PTEN when E2H = 1) | EL1 and EL0 access to the physical counter and physical timer. A hypervisor clears these so guests use the virtual timer |
SCR_EL3.ST (bit 11) | When 0, Secure EL1 accesses to CNTPS_*_EL1 trap to EL3. When Secure EL2 is enabled, it behaves as 1 |
FEAT_ECV adds more CNTHCTL_EL2 traps (EL1TVT, EL1TVCT, EL1NVPCT, EL1NVVCT), covered in 4.7.
Security note:
CNTPSis the Secure world’s own timer, so its existence or behaviour cannot be influenced by Non-secure software. OP-TEE and Secure Partitions that need a timeout should use it (or a Secure memory-mapped frame), not the Non-secure physical timer, which the normal world can reprogram. LeavingSCR_EL3.ST = 1hands the Secure physical timer to Secure EL1. That is a deliberate TF-A choice, and the timer must still be configured as Group 0 or Secure Group 1 in the GIC.
CNTVOFF_EL2
CNTVOFF_EL2 is a 64-bit offset subtracted to form the virtual count. It is per PE: each PE has its own CNTVOFF_EL2, and a hypervisor must keep them identical for one VM, or a vCPU that migrates sees time jump. If EL2 is implemented but firmware enters EL1 directly, CNTVOFF_EL2 resets to an UNKNOWN value. EL3 must write it, normally to 0, before dropping to a Non-secure EL1 that uses the virtual counter. TF-A writes it to 0 as part of setting up an unused EL2 before the first exit to Non-secure EL1. Missing this gives a Linux kernel with a random but internally consistent clock on each core, and different values across cores.
The event stream
Each PE can generate a periodic event that sets its event register and so wakes WFE:
CNTKCTL_EL1.EVNTEN(bit 2),EVNTDIR(bit 3) andEVNTI(bits [7:4]) generate an event from the virtual count.EVNTI = nselects counter bit n, andEVNTDIRchooses whether a 0→1 or a 1→0 transition of that bit triggers it. An event is produced every 2^(n+1) counts.CNTHCTL_EL2has the same fields, applied to the physical count, for EL2’s use.- FEAT_ECV’s
EVNTISselects counter bits [23:8] instead of [15:0], which is needed at 1 GHz (4.7).
The event stream bounds the time any WFE can sleep. Linux enables it (about 10 kHz) so that WFE-based polling loops cannot hang if the expected SEV or exclusive-monitor clear never happens. Firmware spin-locks or hold pens that wait with WFE should not rely on the event stream unless they enable it themselves; they should rely on an explicit SEV (17.4).
The memory-mapped frames
The system counter and system-level timers are controlled through memory-mapped frames (DDI0487 part I). Base addresses are platform-defined.
CNTControlBase, the counter control frame. In a system with two Security states it is accessible only through the Secure physical address space.
| Offset | Register | Purpose |
|---|---|---|
0x000 | CNTCR | EN (bit 0) counter enable, HDBG (bit 1) halt on debug, SCEN (bit 2) scaling enable with FEAT_CNTSC, FCREQ [17:8] selects the frequency-table entry |
0x004 | CNTSR | Status, including FCACK (the acknowledged frequency entry) and DBGH |
0x008 | CNTCV | The 64-bit count value; writable to set the initial count |
0x010 | CNTSCR | Scale value (FEAT_CNTSC, 4.7) |
0x020+ | CNTFID<n> | Frequency modes table; CNTFID0 is the base frequency, the list ends with a zero entry |
CNTReadBase gives a read-only view of CNTCV for the Non-secure world.
CNTCTLBase, the timer control frame, has CNTFRQ (the frequency value, Secure write), CNTNSAR (which frames Non-secure software can use), CNTTIDR (which frames exist and have a virtual timer), CNTACR<n> (per-frame access controls) and CNTVOFF<n> (virtual offset for frame n).
CNTBaseN and optionally CNTEL0BaseN are the timer frames. Each has its own CNTPCT, CNTVCT, CNTFRQ, CNTP_CVAL/TVAL/CTL and CNTV_*, with the same semantics as the system-register timers, but each signals an SPI and is shared by the whole system. They serve as wake-up timers for cores whose local timers are powered off (the Linux “broadcast” timer, and the BSA requirement for a system wake-up timer).
CNTCR.EN: the timer that never ticks
CNTCR.EN resets to 0. With it clear, the system counter does not count, CNTPCT_EL0 returns the same value on every read, and no timer condition ever changes. Reads still succeed, so nothing faults. On many SoCs the boot ROM or TF-A platform code enables it, typically with FCREQ = 0 (select CNTFID0). A new platform port that forgets it (18.3) hangs in the first udelay() or never takes a scheduler tick.
/* TF-A style: enable the system counter at the base frequency */
#define CNTCR_OFF 0x000
#define CNTCR_EN (1U << 0)
#define CNTCR_FCREQ(x) ((uint32_t)(x) << 8)
mmio_write_32(PLAT_CNTCONTROL_BASE + CNTCR_OFF, CNTCR_FCREQ(0) | CNTCR_EN);
write_cntfrq_el0(mmio_read_32(PLAT_CNTCONTROL_BASE + 0x020)); /* CNTFID0 */
Bring-up note: Check four things in order: two reads of
CNTPCT_EL0differ (CNTCR.EN),CNTFRQ_EL0matches the frequency of the clock that actually feeds the counter (measure against a wall clock or a known UART baud),CNTVOFF_EL2is 0 on all cores if no hypervisor is running, and the timer PPI is Group 1 NS, enabled, level, and not masked by PMR on each core.
Checkpoint
Q4.6.1 A bare-metal EL1 timer handler acknowledges INTID 30 through ICC_IAR1_EL1, toggles an LED, writes ICC_EOIR1_EL1 and returns. The handler runs back-to-back forever after the first tick. What is wrong?
- A. The PPI is configured as edge-triggered, so a new edge is latched at every EOI
- B. The timer output is level-sensitive and stays asserted while
ISTATUS = 1andIMASK = 0. The handler must advanceCVAL(or mask or disable the timer) before EOI - C.
ICC_EOIR1_EL1only drops priority; withEOImode = 0a separate write toICC_DIR_EL1is needed - D.
CNTKCTL_EL1.EVNTENis set, so the event stream retriggers the interrupt
Answer
B. The timer condition CVAL ≤ count remains true after the deadline, so the PE’s timer keeps asserting its interrupt signal, and the GIC makes it pending again as soon as it is deactivated. Moving CVAL forward clears ISTATUS. An edge configuration (A) would give the opposite symptom, a single tick. With EOImode = 0 (C), ICC_EOIR1_EL1 performs both priority drop and deactivation. The event stream (D) generates WFE wake-up events, not interrupts.
Read more: Arm ARM (DDI0487), The Generic Timer: Operation of the timer, CNTP_CTL_EL0
Q4.6.2 TF-A boots Linux directly at Non-secure EL1 on a core that implements EL2. On each core, CNTPCT_EL0 values agree to within a few ticks, but CNTVCT_EL0 differs by large, apparently random amounts between cores, and Linux reports clock jumps on migration. Which firmware omission explains this?
- A.
CNTFRQ_EL0was not written on the secondary cores - B.
CNTKCTL_EL1.EL0VCTENis 0 - C.
CNTCR.ENwas set after the secondaries came up - D.
CNTVOFF_EL2was never initialised, so each PE subtracts its own UNKNOWN reset value
Answer
D. The virtual count is the physical count minus the per-PE CNTVOFF_EL2. When EL2 exists but is unused, EL3 must write it (normally 0) on every PE, or each core has its own arbitrary offset. A missing CNTFRQ_EL0 (A) misreports the rate but does not change the count. A late CNTCR.EN (C) would delay counting for all PEs equally, because they share one system counter. B only affects EL0 access.
Read more: Arm ARM (DDI0487), CNTVOFF_EL2 and CNTVCT_EL0
Q4.6.3 A platform wants a timeout to fire while every core in a cluster is powered off. Which timer can do this, and why?
- A. A timer in a memory-mapped
CNTBaseNframe, because it is in the system’s always-on domain and signals an SPI that can wake a core through the GIC - B.
CNTPS_*_EL1, because the Secure physical timer is kept powered throughCPU_OFF - C.
CNTHP_*_EL2, because EL2 timer state is saved in the Redistributor - D. Any per-PE timer, as long as
CNTKCTL_EL1.EVNTENis set so the event stream keeps the core alive
Answer
A. All CNTP/CNTV/CNTPS/CNTHP-class timers are part of the PE and lose their state when the PE’s power domain is off. The memory-mapped timer frames are system components that compare against the always-on system counter and raise SPIs, which the GIC can turn into a wake request. B is the most tempting wrong answer, but the Secure world does not change which power domain the timer is in. The event stream (D) is also part of the PE and only generates WFE events.
Read more: Arm ARM (DDI0487), The Generic Timer memory-mapped components (CNTBaseN, CNTCTLBase); Arm BSA, system wake-up timer