Path: blob/master/tools/testing/selftests/kvm/lib/x86/pmu.c
49657 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (C) 2023, Tencent, Inc.3*/45#include <stdint.h>67#include <linux/kernel.h>89#include "kvm_util.h"10#include "processor.h"11#include "pmu.h"1213const uint64_t intel_pmu_arch_events[] = {14INTEL_ARCH_CPU_CYCLES,15INTEL_ARCH_INSTRUCTIONS_RETIRED,16INTEL_ARCH_REFERENCE_CYCLES,17INTEL_ARCH_LLC_REFERENCES,18INTEL_ARCH_LLC_MISSES,19INTEL_ARCH_BRANCHES_RETIRED,20INTEL_ARCH_BRANCHES_MISPREDICTED,21INTEL_ARCH_TOPDOWN_SLOTS,22INTEL_ARCH_TOPDOWN_BE_BOUND,23INTEL_ARCH_TOPDOWN_BAD_SPEC,24INTEL_ARCH_TOPDOWN_FE_BOUND,25INTEL_ARCH_TOPDOWN_RETIRING,26INTEL_ARCH_LBR_INSERTS,27};28kvm_static_assert(ARRAY_SIZE(intel_pmu_arch_events) == NR_INTEL_ARCH_EVENTS);2930const uint64_t amd_pmu_zen_events[] = {31AMD_ZEN_CORE_CYCLES,32AMD_ZEN_INSTRUCTIONS_RETIRED,33AMD_ZEN_BRANCHES_RETIRED,34AMD_ZEN_BRANCHES_MISPREDICTED,35};36kvm_static_assert(ARRAY_SIZE(amd_pmu_zen_events) == NR_AMD_ZEN_EVENTS);3738/*39* For Intel Atom CPUs, the PMU events "Instruction Retired" or40* "Branch Instruction Retired" may be overcounted for some certain41* instructions, like FAR CALL/JMP, RETF, IRET, VMENTRY/VMEXIT/VMPTRLD42* and complex SGX/SMX/CSTATE instructions/flows.43*44* The detailed information can be found in the errata (section SRF7):45* https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/sierra-forest/xeon-6700-series-processor-with-e-cores-specification-update/errata-details/46*47* For the Atom platforms before Sierra Forest (including Sierra Forest),48* Both 2 events "Instruction Retired" and "Branch Instruction Retired" would49* be overcounted on these certain instructions, but for Clearwater Forest50* only "Instruction Retired" event is overcounted on these instructions.51*/52static uint64_t get_pmu_errata(void)53{54if (!this_cpu_is_intel())55return 0;5657if (this_cpu_family() != 0x6)58return 0;5960switch (this_cpu_model()) {61case 0xDD: /* Clearwater Forest */62return BIT_ULL(INSTRUCTIONS_RETIRED_OVERCOUNT);63case 0xAF: /* Sierra Forest */64case 0x4D: /* Avaton, Rangely */65case 0x5F: /* Denverton */66case 0x86: /* Jacobsville */67return BIT_ULL(INSTRUCTIONS_RETIRED_OVERCOUNT) |68BIT_ULL(BRANCHES_RETIRED_OVERCOUNT);69default:70return 0;71}72}7374uint64_t pmu_errata_mask;7576void kvm_init_pmu_errata(void)77{78pmu_errata_mask = get_pmu_errata();79}808182