Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

arm64: cpufeature: Extract capped perfmon fields

When emulating ID registers there is often a need to cap the version
bits of a feature such that the guest will not use features that the
host is not aware of. For example, when KVM mediates access to the PMU
by emulating register accesses.

Let's add a helper that extracts a performance monitors ID field and
caps the version to a given value.

Fields that identify the version of the Performance Monitors Extension
do not follow the standard ID scheme, and instead follow the scheme
described in ARM DDI 0487E.a page D13-2825 "Alternative ID scheme used
for the Performance Monitors Extension version". The value 0xF means an
IMPLEMENTATION DEFINED PMU is present, and values 0x0-OxE can be treated
the same as an unsigned field with 0x0 meaning no PMU is present.

Signed-off-by: Andrew Murray <andrew.murray@arm.com>
Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com>
[Mark: rework to handle perfmon fields]
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Will Deacon <will@kernel.org>

authored by

Andrew Murray and committed by
Will Deacon
8e35aa64 29227d6e

+23
+23
arch/arm64/include/asm/cpufeature.h
··· 447 447 return cpuid_feature_extract_unsigned_field_width(features, field, 4); 448 448 } 449 449 450 + /* 451 + * Fields that identify the version of the Performance Monitors Extension do 452 + * not follow the standard ID scheme. See ARM DDI 0487E.a page D13-2825, 453 + * "Alternative ID scheme used for the Performance Monitors Extension version". 454 + */ 455 + static inline u64 __attribute_const__ 456 + cpuid_feature_cap_perfmon_field(u64 features, int field, u64 cap) 457 + { 458 + u64 val = cpuid_feature_extract_unsigned_field(features, field); 459 + u64 mask = GENMASK_ULL(field + 3, field); 460 + 461 + /* Treat IMPLEMENTATION DEFINED functionality as unimplemented */ 462 + if (val == 0xf) 463 + val = 0; 464 + 465 + if (val > cap) { 466 + features &= ~mask; 467 + features |= (cap << field) & mask; 468 + } 469 + 470 + return features; 471 + } 472 + 450 473 static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp) 451 474 { 452 475 return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);