Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

16.4 Activity Monitors (AMU)

The Activity Monitors Extension is a second, separate set of per-PE counters. It is built for always-on activity accounting by firmware and power-management software (frequency invariance, power and thermal control, MPMM-style throttling), not for profiling. FEAT_AMUv1 is optional from v8.4, and FEAT_AMUv1p1 (virtual offsets and auxiliary-counter restriction) is optional from v8.6. Both are reported by ID_AA64PFR0_EL1.AMU (0b0001 v1, 0b0010 v1p1). An external, memory-mapped view (FEAT_AMU_EXT32/EXT64) can exist next to the system registers.

Counter groups

Group 0: architected counters. Their events are fixed by the architecture, and AMEVTYPER0<n>_EL0 is read-only:

CounterevtCountEvent
AMEVCNTR00_EL00x0011processor frequency cycles (CPU_CYCLES)
AMEVCNTR01_EL00x4004constant frequency cycles (CNT_CYCLES), at the System Counter rate
AMEVCNTR02_EL00x0008instructions retired (INST_RETIRED)
AMEVCNTR03_EL00x4005memory stall cycles (STALL_BACKEND_MEM)

Group 1: auxiliary counters. There are up to 16, AMEVCNTR1<n>_EL0, whose events in AMEVTYPER1<n>_EL0 are IMPLEMENTATION DEFINED. An event may be fixed in hardware, in which case the type register is read-only. With v1p1, AMCG1IDR_EL0 reports which auxiliary counters exist (bits [15:0]) and which have a virtual offset register (bits [31:16]), because the implemented set need not be contiguous.

All counters are 64 bits wide (AMCFGR_EL0.SIZE == 0b111111). They have no overflow interrupt: at gigahertz rates, 64 bits do not wrap within the lifetime of the part.

Configuration and identification registers

  • AMCFGR_EL0: N [7:0] is the total number of counters minus one, SIZE [13:8] the counter width minus one, HDBG [24] reports that AMCR_EL0.HDBG is supported, and NCG [31:28] is the number of groups minus one.
  • AMCGCR_EL0: CG0NC [7:0] and CG1NC [15:8] give the number of counters in group 0 (4 in AMUv1) and in group 1.
  • AMCR_EL0: HDBG [10] stops counting while the PE is halted in Debug state (16.2). CG1RZ [17] (v1p1) makes system-register reads of the group 1 counters return zero at every EL except the highest implemented one.
  • AMCNTENSET0/CLR0_EL0 and AMCNTENSET1/CLR1_EL0: per-counter enables.

Who controls what

The AMU is controlled only from the highest implemented EL, which on most systems is EL3. MSR to AMCNTENSET0_EL0 and AMEVCNTR0<n>_EL0 is UNDEFINED below that EL, and so are the other enable, control and counter writes. Lower ELs can only read, subject to these traps:

ControlEffect
CPTR_EL3.TAM [30]1: EL2, EL1 and EL0 accesses to all AMU registers trap to EL3 (EC 0x18). Must be cleared for Linux to use the AMU
CPTR_EL2.TAM [30]1: EL1/EL0 accesses trap to EL2
AMUSERENR_EL0.EN [0]0: EL0 accesses trap to EL1 (or to EL2 if HCR_EL2.TGE == 1)
HAFGRTR_EL2 (FEAT_FGT)per-counter fine-grained read traps to EL2

Counter writes have one more trap: writing a counter while it is enabled has UNPREDICTABLE results. The architected counters reset to 0 on an AMU reset, which the architecture does not tie to a PE Warm reset. Firmware therefore cannot assume that the counters are zero, or even disabled, when it starts.

AMUv1p1 virtual offsets

A guest that reads the AMU to compute its own frequency or utilisation should see counts relative to its timeline. FEAT_AMUv1p1 adds AMEVCNTVOFF0<n>_EL2 (for n = 0, 2, 3) and AMEVCNTVOFF1<n>_EL2. When

  • SCR_EL3.AMVOFFEN [35] == 1, and
  • HCR_EL2.AMVOFFEN == 1, and
  • EL2 is enabled in the current Security state and HCR_EL2.{E2H,TGE} is not {1,1},

a read from EL1 or EL0 returns physical count − offset. Reads at EL2 and EL3 always return the physical count. Counter 1 has no offset register of its own: the constant-frequency counter uses CNTVOFF_EL2, keeping it in step with the guest’s virtual counter (4.7). With SCR_EL3.AMVOFFEN == 0, EL2 accesses to the offset registers trap to EL3 and the offsets read as zero for the subtraction. That is the safe default when EL3 does not context-switch them.

TF-A: enabling, restricting and saving

TF-A’s AMU support is in lib/extensions/amu/aarch64/amu.c, built with ENABLE_FEAT_AMU, ENABLE_FEAT_AMUv1p1, ENABLE_AMU_AUXILIARY_COUNTERS and AMU_RESTRICT_COUNTERS (default 1). The current code does the following (structure as of recent TF-A releases):

  • Per world: it clears CPTR_EL3.TAM, so that lower ELs can read the counters.
  • Per CPU context: on v1p1 parts it sets SCR_EL3.AMVOFFEN in the context’s saved SCR_EL3, so that EL2 accesses to the offset registers do not trap and a hypervisor can own the offsets.
  • Per CPU at EL3 init: it enables all four architected counters (AMCNTENSET0_EL0). If auxiliary counters are configured, it enables the platform-specified ones. On v1p1 parts with AMU_RESTRICT_COUNTERS, it sets AMCR_EL0.CG1RZ, so that IMPLEMENTATION DEFINED auxiliary events read as zero outside EL3. It leaves HDBG at 0.
  • When EL2 is unused: it clears CPTR_EL2.TAM and, on v1p1 parts, HCR_EL2.AMVOFFEN, so that no virtual offset is applied.
  • Across power-down: the counters are in the Core power domain and are lost on power-down. TF-A subscribes to the PSCI psci_suspend_pwrdown_start and psci_suspend_pwrdown_finish events. On the way down, it disables the counters first (followed by an ISB), so that the saved values form one consistent snapshot, and then saves group 0 and the implemented group 1 counters. On the way up, it disables the counters if the AMU was not reset, writes the saved values, and re-enables the counters.
  • Across world switches: the instructions-retired and memory-stall counters (AMEVCNTR02/03_EL0) are saved and restored per Security state by the context-management code. Normal-world software therefore does not see instructions retired by Secure-world firmware, while the two cycle counters stay global.

Bring-up note: Linux’s frequency-invariance code (arch/arm64/kernel/topology.c) samples counters 0 and 1 on each scheduler tick and assumes they are monotonic across idle. When AMU save/restore is missing or broken, counter 0 restarts from an UNKNOWN value after the first CPU_SUSPEND power-down, and the frequency scale factor jumps. If EL3 leaves CPTR_EL3.TAM = 1, the kernel’s first AMU read traps to EL3 as an unexpected system-register trap. Depending on the TF-A version and configuration this ends in a panic or an exception injected back to the kernel, so check TAM first when an early AMU read hangs.

AMU versus PMU

PMU (16.3)AMU
Purposeprofiling and debugalways-on activity accounting
Owner of configurationEL0–EL3, with prohibition by higher ELshighest EL only. Lower ELs can only read
Event choiceany of dozens of common events plus IMPLEMENTATION DEFINED events, per counterfixed group 0, IMPLEMENTATION DEFINED group 1
EL/Security filteringPMEVTYPER filter bits and MDCR_EL3.SPMEnone in the counter. Firmware swaps counter values per world
Width / overflow32 or 64-bit, overflow interrupt64-bit, no interrupt
Freeze in Debug stateno dedicated control bit (see the PMU chapter of DDI0487 for Debug-state counting rules)AMCR_EL0.HDBG
VirtualisationMDCR_EL2.HPMN partition, trap-and-emulatev1p1 offsets subtracted in hardware
Typical consumerperf, validationcpufreq/schedutil, SCMI power agents, TF-A MPMM

The most important difference is that the AMU counts everything the PE does, whatever the Exception level or Security state, because there is no filter. That is what makes it trustworthy for power management, and also what makes it a side-channel concern. TF-A therefore restricts group 1 by default and per-world-swaps the instruction and stall counts.

Security note: group 0 INST_RETIRED and STALL_BACKEND_MEM counted across an SMC reveal how much work, and how much memory stall, the Secure world performed for a given request, and a cache-timing attacker can use that. Auxiliary events can be finer-grained still. In review, check that AMU_RESTRICT_COUNTERS stays enabled, that the per-world swap of counters 2 and 3 is in place for every world, including Realm under RME, and that no Secure Partition is given AMU access it does not need.

Validation note: five properties are worth automating.

  • (1) Monotonicity across CPU_SUSPEND power-down: read AMEVCNTR00/01_EL0 before and after and require after ≥ before. A failure is missing or misordered save/restore.
  • (2) Ratio sanity: over a fixed interval, ΔAMEVCNTR01 / ΔCNTPCT_EL0 must equal 1 while the core is running. Any drift means counter 1 is not on the System Counter clock.
  • (3) ΔAMEVCNTR00 / ΔAMEVCNTR01 must match the configured DVFS ratio.
  • (4) With CG1RZ = 1, group 1 reads at EL1 must return 0 while the memory-mapped view still counts.
  • (5) With v1p1, a write to AMEVCNTVOFF00_EL2 must shift EL1 reads by exactly the offset and leave EL2 reads unchanged.

Checkpoint

Q16.4.1 After a CPU_SUSPEND power-down and resume, Linux’s frequency-invariance code sees AMEVCNTR00_EL0 go backwards. TF-A’s AMU save/restore is enabled. Which ordering bug in a platform’s own port is most consistent with this?

  • A. The restore wrote the counters after ISB rather than before
  • B. AMCR_EL0.HDBG was left at 0
  • C. The save path read the counters without disabling them first, or the restore path wrote them while they were enabled. Either makes the written value UNPREDICTABLE or inconsistent
  • D. SCR_EL3.AMVOFFEN was 0, so EL1 reads subtracted a stale offset
Answer

C. Writes to an enabled AMU counter are UNPREDICTABLE, and the AMU may not have been reset with the PE, so its enable state at resume cannot be assumed. TF-A disables the counters before saving and before restoring for exactly this reason. Option D is backwards: with AMVOFFEN == 0 the offsets are treated as zero, so no subtraction happens. HDBG matters only in halting Debug state, which rules out B.

Read more: Arm ARM (DDI0487), AMEVCNTR0<n>_EL0, AMCNTENSET0_EL0; TF-A lib/extensions/amu/aarch64/amu.c

Q16.4.2 A hypervisor at EL2 wants a guest’s reads of AMEVCNTR01_EL0 to exclude time when the guest was descheduled. Which register does it adjust?

  • A. AMEVCNTVOFF01_EL2
  • B. CNTVOFF_EL2, because the constant-frequency counter’s virtual offset is the generic timer virtual offset
  • C. AMEVTYPER01_EL0, to change the event to a guest-only event
  • D. None: the AMU cannot be virtualised, so the read must be trapped with CPTR_EL2.TAM and emulated
Answer

B. AMEVCNTVOFF0<n>_EL2 exists only for n = 0, 2 and 3, and accessing n = 1 is UNDEFINED, which rules out A. The constant-frequency counter is defined to use CNTVOFF_EL2, so it stays consistent with CNTVCT_EL0. Trap-and-emulate (D) works, but it is unnecessary with FEAT_AMUv1p1. Group 0 event types are read-only, which rules out C.

Read more: Arm ARM (DDI0487), AMEVCNTVOFF0<n>_EL2, SCR_EL3.AMVOFFEN, HCR_EL2.AMVOFFEN

Q16.4.3 A team proposes using the AMU instead of the PMU to measure the instruction count of a Normal-world benchmark “because the AMU cannot be disabled by the OS”. What is the main flaw in that reasoning?

  • A. AMU counters have no EL or Security filter, so the raw count includes EL2/EL3/Secure work, and firmware may swap counters 2 and 3 per world, so the result depends on firmware policy rather than on the benchmark
  • B. The AMU’s INST_RETIRED counts speculative instructions
  • C. EL1 can reprogram AMEVTYPER02_EL0 to a different event
  • D. AMU counters are only 32 bits wide and wrap during long benchmarks
Answer

A. The AMU counts everything the PE does. Whether Normal-world reads include Secure-world instructions depends on what EL3 context-switches, and current TF-A swaps counters 2 and 3 per world. It is an accounting tool, not a filtered profiler. Group 0 types are architecturally fixed, which rules out C, and the counters are 64-bit, which rules out D.

Read more: Arm ARM (DDI0487), “The Activity Monitors Extension”, AMEVTYPER0<n>_EL0