2019-06-03 07:44:50 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-12-08 15:29:06 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Linaro Ltd.
|
|
|
|
* Author: Shannon Zhao <shannon.zhao@linaro.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/kvm.h>
|
|
|
|
#include <linux/kvm_host.h>
|
2022-01-27 16:17:57 +00:00
|
|
|
#include <linux/list.h>
|
2015-12-08 15:29:06 +08:00
|
|
|
#include <linux/perf_event.h>
|
2019-10-06 10:28:50 +01:00
|
|
|
#include <linux/perf/arm_pmu.h>
|
2016-01-11 21:35:32 +08:00
|
|
|
#include <linux/uaccess.h>
|
2015-12-08 15:29:06 +08:00
|
|
|
#include <asm/kvm_emulate.h>
|
|
|
|
#include <kvm/arm_pmu.h>
|
2016-02-26 19:29:19 +08:00
|
|
|
#include <kvm/arm_vgic.h>
|
2015-12-08 15:29:06 +08:00
|
|
|
|
2022-11-13 16:38:18 +00:00
|
|
|
#define PERF_ATTR_CFG1_COUNTER_64BIT BIT(0)
|
|
|
|
|
2022-01-27 16:17:57 +00:00
|
|
|
static LIST_HEAD(arm_pmus);
|
|
|
|
static DEFINE_MUTEX(arm_pmus_lock);
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc);
|
2022-11-13 16:38:24 +00:00
|
|
|
static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc);
|
2024-12-17 09:55:13 -08:00
|
|
|
static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc);
|
2019-06-17 20:01:05 +01:00
|
|
|
|
2025-03-05 12:26:33 -08:00
|
|
|
bool kvm_supports_guest_pmuv3(void)
|
|
|
|
{
|
|
|
|
guard(mutex)(&arm_pmus_lock);
|
|
|
|
return !list_empty(&arm_pmus);
|
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
static struct kvm_vcpu *kvm_pmc_to_vcpu(const struct kvm_pmc *pmc)
|
|
|
|
{
|
|
|
|
return container_of(pmc, struct kvm_vcpu, arch.pmu.pmc[pmc->idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kvm_pmc *kvm_vcpu_idx_to_pmc(struct kvm_vcpu *vcpu, int cnt_idx)
|
|
|
|
{
|
|
|
|
return &vcpu->arch.pmu.pmc[cnt_idx];
|
|
|
|
}
|
|
|
|
|
2023-08-18 21:39:45 -07:00
|
|
|
static u32 __kvm_pmu_event_mask(unsigned int pmuver)
|
2020-03-17 11:11:56 +00:00
|
|
|
{
|
2022-01-27 16:17:56 +00:00
|
|
|
switch (pmuver) {
|
2022-09-10 17:33:51 +01:00
|
|
|
case ID_AA64DFR0_EL1_PMUVer_IMP:
|
2020-03-17 11:11:56 +00:00
|
|
|
return GENMASK(9, 0);
|
2022-09-10 17:33:51 +01:00
|
|
|
case ID_AA64DFR0_EL1_PMUVer_V3P1:
|
|
|
|
case ID_AA64DFR0_EL1_PMUVer_V3P4:
|
|
|
|
case ID_AA64DFR0_EL1_PMUVer_V3P5:
|
|
|
|
case ID_AA64DFR0_EL1_PMUVer_V3P7:
|
2020-03-17 11:11:56 +00:00
|
|
|
return GENMASK(15, 0);
|
|
|
|
default: /* Shouldn't be here, just for sanity */
|
2022-01-27 16:17:56 +00:00
|
|
|
WARN_ONCE(1, "Unknown PMU version %d\n", pmuver);
|
2020-03-17 11:11:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 21:39:45 -07:00
|
|
|
static u32 kvm_pmu_event_mask(struct kvm *kvm)
|
|
|
|
{
|
2024-06-19 17:40:29 +00:00
|
|
|
u64 dfr0 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64DFR0_EL1);
|
2023-08-18 21:39:45 -07:00
|
|
|
u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, dfr0);
|
|
|
|
|
|
|
|
return __kvm_pmu_event_mask(pmuver);
|
|
|
|
}
|
|
|
|
|
2023-10-19 18:56:17 +00:00
|
|
|
u64 kvm_pmu_evtyper_mask(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
u64 mask = ARMV8_PMU_EXCLUDE_EL1 | ARMV8_PMU_EXCLUDE_EL0 |
|
|
|
|
kvm_pmu_event_mask(kvm);
|
|
|
|
|
2024-02-14 13:18:03 +00:00
|
|
|
if (kvm_has_feat(kvm, ID_AA64PFR0_EL1, EL2, IMP))
|
2023-10-19 18:56:17 +00:00
|
|
|
mask |= ARMV8_PMU_INCLUDE_EL2;
|
|
|
|
|
2024-02-14 13:18:03 +00:00
|
|
|
if (kvm_has_feat(kvm, ID_AA64PFR0_EL1, EL3, IMP))
|
2023-10-19 18:56:18 +00:00
|
|
|
mask |= ARMV8_PMU_EXCLUDE_NS_EL0 |
|
|
|
|
ARMV8_PMU_EXCLUDE_NS_EL1 |
|
|
|
|
ARMV8_PMU_EXCLUDE_EL3;
|
|
|
|
|
2023-10-19 18:56:17 +00:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2019-06-17 20:01:04 +01:00
|
|
|
/**
|
2022-11-13 16:38:32 +00:00
|
|
|
* kvm_pmc_is_64bit - determine if counter is 64bit
|
|
|
|
* @pmc: counter context
|
2019-06-17 20:01:04 +01:00
|
|
|
*/
|
2022-11-13 16:38:32 +00:00
|
|
|
static bool kvm_pmc_is_64bit(struct kvm_pmc *pmc)
|
2022-11-13 16:38:20 +00:00
|
|
|
{
|
2024-02-14 13:18:03 +00:00
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
return (pmc->idx == ARMV8_PMU_CYCLE_IDX ||
|
2024-02-14 13:18:03 +00:00
|
|
|
kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5));
|
2022-11-13 16:38:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
static bool kvm_pmc_has_64bit_overflow(struct kvm_pmc *pmc)
|
2019-06-17 20:01:04 +01:00
|
|
|
{
|
2024-10-25 18:23:51 +00:00
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
|
|
|
u64 val = kvm_vcpu_read_pmcr(vcpu);
|
|
|
|
|
|
|
|
if (kvm_pmu_counter_is_hyp(vcpu, pmc->idx))
|
|
|
|
return __vcpu_sys_reg(vcpu, MDCR_EL2) & MDCR_EL2_HLP;
|
2022-11-13 16:38:29 +00:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
return (pmc->idx < ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LP)) ||
|
|
|
|
(pmc->idx == ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LC));
|
2019-06-17 20:01:04 +01:00
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
static bool kvm_pmu_counter_can_chain(struct kvm_pmc *pmc)
|
2022-11-13 16:38:18 +00:00
|
|
|
{
|
2022-11-13 16:38:32 +00:00
|
|
|
return (!(pmc->idx & 1) && (pmc->idx + 1) < ARMV8_PMU_CYCLE_IDX &&
|
|
|
|
!kvm_pmc_has_64bit_overflow(pmc));
|
2019-06-17 20:01:05 +01:00
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:23 +00:00
|
|
|
static u32 counter_index_to_reg(u64 idx)
|
|
|
|
{
|
|
|
|
return (idx == ARMV8_PMU_CYCLE_IDX) ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 counter_index_to_evtreg(u64 idx)
|
|
|
|
{
|
|
|
|
return (idx == ARMV8_PMU_CYCLE_IDX) ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + idx;
|
|
|
|
}
|
|
|
|
|
2024-10-25 18:23:49 +00:00
|
|
|
static u64 kvm_pmc_read_evtreg(const struct kvm_pmc *pmc)
|
|
|
|
{
|
|
|
|
return __vcpu_sys_reg(kvm_pmc_to_vcpu(pmc), counter_index_to_evtreg(pmc->idx));
|
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
static u64 kvm_pmu_get_pmc_value(struct kvm_pmc *pmc)
|
2019-06-17 20:01:05 +01:00
|
|
|
{
|
2022-11-13 16:38:32 +00:00
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
2022-11-13 16:38:18 +00:00
|
|
|
u64 counter, reg, enabled, running;
|
2019-06-17 20:01:05 +01:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
reg = counter_index_to_reg(pmc->idx);
|
2022-11-13 16:38:18 +00:00
|
|
|
counter = __vcpu_sys_reg(vcpu, reg);
|
2019-06-17 20:01:05 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The real counter value is equal to the value of counter register plus
|
2015-12-08 15:29:06 +08:00
|
|
|
* the value perf event counts.
|
|
|
|
*/
|
|
|
|
if (pmc->perf_event)
|
|
|
|
counter += perf_event_read_value(pmc->perf_event, &enabled,
|
|
|
|
&running);
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
if (!kvm_pmc_is_64bit(pmc))
|
2019-06-17 20:01:04 +01:00
|
|
|
counter = lower_32_bits(counter);
|
|
|
|
|
|
|
|
return counter;
|
2015-12-08 15:29:06 +08:00
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
/**
|
|
|
|
* kvm_pmu_get_counter_value - get PMU counter value
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
* @select_idx: The counter index
|
|
|
|
*/
|
|
|
|
u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
|
2015-12-08 15:29:06 +08:00
|
|
|
{
|
2022-11-13 16:38:32 +00:00
|
|
|
return kvm_pmu_get_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx));
|
|
|
|
}
|
2022-11-13 16:38:24 +00:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
static void kvm_pmu_set_pmc_value(struct kvm_pmc *pmc, u64 val, bool force)
|
|
|
|
{
|
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
|
|
|
u64 reg;
|
2022-11-13 16:38:25 +00:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
kvm_pmu_release_perf_event(pmc);
|
|
|
|
|
|
|
|
reg = counter_index_to_reg(pmc->idx);
|
|
|
|
|
|
|
|
if (vcpu_mode_is_32bit(vcpu) && pmc->idx != ARMV8_PMU_CYCLE_IDX &&
|
2022-11-13 16:38:25 +00:00
|
|
|
!force) {
|
|
|
|
/*
|
|
|
|
* Even with PMUv3p5, AArch32 cannot write to the top
|
|
|
|
* 32bit of the counters. The only possible course of
|
|
|
|
* action is to use PMCR.P, which will reset them to
|
|
|
|
* 0 (the only use of the 'force' parameter).
|
|
|
|
*/
|
|
|
|
val = __vcpu_sys_reg(vcpu, reg) & GENMASK(63, 32);
|
|
|
|
val |= lower_32_bits(val);
|
|
|
|
}
|
|
|
|
|
2025-06-03 08:08:21 +01:00
|
|
|
__vcpu_assign_sys_reg(vcpu, reg, val);
|
2019-06-17 20:01:03 +01:00
|
|
|
|
|
|
|
/* Recreate the perf event to reflect the updated sample_period */
|
2022-11-13 16:38:32 +00:00
|
|
|
kvm_pmu_create_perf_event(pmc);
|
2015-12-08 15:29:06 +08:00
|
|
|
}
|
2015-09-08 12:26:13 +08:00
|
|
|
|
2022-11-13 16:38:25 +00:00
|
|
|
/**
|
|
|
|
* kvm_pmu_set_counter_value - set PMU counter value
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
* @select_idx: The counter index
|
|
|
|
* @val: The counter value
|
|
|
|
*/
|
|
|
|
void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
|
|
|
|
{
|
2022-11-13 16:38:32 +00:00
|
|
|
kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx), val, false);
|
2022-11-13 16:38:25 +00:00
|
|
|
}
|
|
|
|
|
2025-03-15 18:12:12 +09:00
|
|
|
/**
|
|
|
|
* kvm_pmu_set_counter_value_user - set PMU counter value from user
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
* @select_idx: The counter index
|
|
|
|
* @val: The counter value
|
|
|
|
*/
|
|
|
|
void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
|
|
|
|
{
|
|
|
|
kvm_pmu_release_perf_event(kvm_vcpu_idx_to_pmc(vcpu, select_idx));
|
2025-06-03 08:08:21 +01:00
|
|
|
__vcpu_assign_sys_reg(vcpu, counter_index_to_reg(select_idx), val);
|
2025-03-15 18:12:12 +09:00
|
|
|
kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
|
|
|
|
}
|
|
|
|
|
2019-06-17 20:01:02 +01:00
|
|
|
/**
|
|
|
|
* kvm_pmu_release_perf_event - remove the perf event
|
|
|
|
* @pmc: The PMU counter pointer
|
|
|
|
*/
|
|
|
|
static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
|
|
|
|
{
|
|
|
|
if (pmc->perf_event) {
|
|
|
|
perf_event_disable(pmc->perf_event);
|
|
|
|
perf_event_release_kernel(pmc->perf_event);
|
|
|
|
pmc->perf_event = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 14:27:25 +08:00
|
|
|
/**
|
|
|
|
* kvm_pmu_stop_counter - stop PMU counter
|
|
|
|
* @pmc: The PMU counter pointer
|
|
|
|
*
|
|
|
|
* If this counter has been configured to monitor some event, release it here.
|
|
|
|
*/
|
2022-11-13 16:38:32 +00:00
|
|
|
static void kvm_pmu_stop_counter(struct kvm_pmc *pmc)
|
2015-07-03 14:27:25 +08:00
|
|
|
{
|
2022-11-13 16:38:32 +00:00
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
2022-11-13 16:38:22 +00:00
|
|
|
u64 reg, val;
|
2015-07-03 14:27:25 +08:00
|
|
|
|
2019-06-17 20:01:05 +01:00
|
|
|
if (!pmc->perf_event)
|
|
|
|
return;
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
val = kvm_pmu_get_pmc_value(pmc);
|
2019-06-17 20:01:05 +01:00
|
|
|
|
2022-11-13 16:38:23 +00:00
|
|
|
reg = counter_index_to_reg(pmc->idx);
|
2019-06-17 20:01:05 +01:00
|
|
|
|
2025-06-03 08:08:21 +01:00
|
|
|
__vcpu_assign_sys_reg(vcpu, reg, val);
|
2019-10-03 18:02:08 +01:00
|
|
|
|
2019-06-17 20:01:05 +01:00
|
|
|
kvm_pmu_release_perf_event(pmc);
|
2015-07-03 14:27:25 +08:00
|
|
|
}
|
|
|
|
|
2019-07-18 08:15:10 +00:00
|
|
|
/**
|
|
|
|
* kvm_pmu_vcpu_init - assign pmu counter idx for cpu
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct kvm_pmu *pmu = &vcpu->arch.pmu;
|
|
|
|
|
2024-07-31 10:51:23 -06:00
|
|
|
for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++)
|
2019-07-18 08:15:10 +00:00
|
|
|
pmu->pmc[i].idx = i;
|
|
|
|
}
|
|
|
|
|
2015-09-11 15:18:05 +08:00
|
|
|
/**
|
|
|
|
* kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2024-07-31 10:51:23 -06:00
|
|
|
for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++)
|
2022-11-13 16:38:32 +00:00
|
|
|
kvm_pmu_release_perf_event(kvm_vcpu_idx_to_pmc(vcpu, i));
|
2020-09-24 12:07:04 +01:00
|
|
|
irq_work_sync(&vcpu->arch.pmu.overflow_work);
|
2015-09-11 15:18:05 +08:00
|
|
|
}
|
|
|
|
|
2024-11-19 16:52:30 -08:00
|
|
|
static u64 kvm_pmu_hyp_counter_mask(struct kvm_vcpu *vcpu)
|
2024-10-25 18:23:45 +00:00
|
|
|
{
|
2024-11-19 16:52:30 -08:00
|
|
|
unsigned int hpmn, n;
|
|
|
|
|
|
|
|
if (!vcpu_has_nv(vcpu))
|
|
|
|
return 0;
|
2024-10-25 18:23:45 +00:00
|
|
|
|
2024-11-19 16:52:30 -08:00
|
|
|
hpmn = SYS_FIELD_GET(MDCR_EL2, HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
|
2025-04-11 11:54:22 +01:00
|
|
|
n = vcpu->kvm->arch.nr_pmu_counters;
|
2024-11-19 16:52:30 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Programming HPMN to a value greater than PMCR_EL0.N is
|
|
|
|
* CONSTRAINED UNPREDICTABLE. Make the implementation choice that an
|
|
|
|
* UNKNOWN number of counters (in our case, zero) are reserved for EL2.
|
|
|
|
*/
|
|
|
|
if (hpmn >= n)
|
|
|
|
return 0;
|
2024-10-25 18:23:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Programming HPMN=0 is CONSTRAINED UNPREDICTABLE if FEAT_HPMN0 isn't
|
|
|
|
* implemented. Since KVM's ability to emulate HPMN=0 does not directly
|
|
|
|
* depend on hardware (all PMU registers are trapped), make the
|
|
|
|
* implementation choice that all counters are included in the second
|
|
|
|
* range reserved for EL2/EL3.
|
|
|
|
*/
|
2024-11-19 16:52:30 -08:00
|
|
|
return GENMASK(n - 1, hpmn);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int idx)
|
|
|
|
{
|
|
|
|
return kvm_pmu_hyp_counter_mask(vcpu) & BIT(idx);
|
2024-10-25 18:23:45 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 18:23:48 +00:00
|
|
|
u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
u64 mask = kvm_pmu_implemented_counter_mask(vcpu);
|
|
|
|
|
|
|
|
if (!vcpu_has_nv(vcpu) || vcpu_is_el2(vcpu))
|
|
|
|
return mask;
|
|
|
|
|
2024-11-19 16:52:30 -08:00
|
|
|
return mask & ~kvm_pmu_hyp_counter_mask(vcpu);
|
2024-10-25 18:23:48 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 18:23:47 +00:00
|
|
|
u64 kvm_pmu_implemented_counter_mask(struct kvm_vcpu *vcpu)
|
2015-09-08 12:26:13 +08:00
|
|
|
{
|
2023-12-11 16:13:14 +00:00
|
|
|
u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, kvm_vcpu_read_pmcr(vcpu));
|
2015-09-08 12:26:13 +08:00
|
|
|
|
|
|
|
if (val == 0)
|
|
|
|
return BIT(ARMV8_PMU_CYCLE_IDX);
|
|
|
|
else
|
|
|
|
return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
|
|
|
|
}
|
|
|
|
|
2024-12-17 09:55:13 -08:00
|
|
|
static void kvm_pmc_enable_perf_event(struct kvm_pmc *pmc)
|
2015-09-08 12:26:13 +08:00
|
|
|
{
|
2024-12-17 09:55:13 -08:00
|
|
|
if (!pmc->perf_event) {
|
|
|
|
kvm_pmu_create_perf_event(pmc);
|
2015-09-08 12:26:13 +08:00
|
|
|
return;
|
2024-12-17 09:55:13 -08:00
|
|
|
}
|
2015-09-08 12:26:13 +08:00
|
|
|
|
2024-12-17 09:55:13 -08:00
|
|
|
perf_event_enable(pmc->perf_event);
|
|
|
|
if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
|
|
|
|
kvm_debug("fail to enable perf event\n");
|
|
|
|
}
|
2019-06-17 20:01:05 +01:00
|
|
|
|
2024-12-17 09:55:13 -08:00
|
|
|
static void kvm_pmc_disable_perf_event(struct kvm_pmc *pmc)
|
|
|
|
{
|
|
|
|
if (pmc->perf_event)
|
|
|
|
perf_event_disable(pmc->perf_event);
|
2015-09-08 12:26:13 +08:00
|
|
|
}
|
|
|
|
|
2024-12-17 09:55:13 -08:00
|
|
|
void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val)
|
2015-09-08 12:26:13 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2025-03-15 18:12:11 +09:00
|
|
|
if (!val)
|
2015-09-08 12:26:13 +08:00
|
|
|
return;
|
|
|
|
|
2024-07-31 10:51:23 -06:00
|
|
|
for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++) {
|
2024-12-17 09:55:13 -08:00
|
|
|
struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
|
2022-11-13 16:38:32 +00:00
|
|
|
|
2015-09-08 12:26:13 +08:00
|
|
|
if (!(val & BIT(i)))
|
|
|
|
continue;
|
|
|
|
|
2024-12-17 09:55:13 -08:00
|
|
|
if (kvm_pmu_counter_is_enabled(pmc))
|
|
|
|
kvm_pmc_enable_perf_event(pmc);
|
|
|
|
else
|
|
|
|
kvm_pmc_disable_perf_event(pmc);
|
2015-09-08 12:26:13 +08:00
|
|
|
}
|
2024-12-17 09:55:13 -08:00
|
|
|
|
|
|
|
kvm_vcpu_pmu_restore_guest(vcpu);
|
2015-09-08 12:26:13 +08:00
|
|
|
}
|
2015-07-03 14:27:25 +08:00
|
|
|
|
2024-11-19 16:52:30 -08:00
|
|
|
/*
|
|
|
|
* Returns the PMU overflow state, which is true if there exists an event
|
|
|
|
* counter where the values of the global enable control, PMOVSSET_EL0[n], and
|
|
|
|
* PMINTENSET_EL1[n] are all 1.
|
|
|
|
*/
|
|
|
|
static bool kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
|
2015-09-08 15:03:26 +08:00
|
|
|
{
|
2024-11-19 16:52:30 -08:00
|
|
|
u64 reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
|
2015-09-08 15:03:26 +08:00
|
|
|
|
2024-11-19 16:52:30 -08:00
|
|
|
reg &= __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PMCR_EL0.E is the global enable control for event counters available
|
|
|
|
* to EL0 and EL1.
|
|
|
|
*/
|
|
|
|
if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E))
|
|
|
|
reg &= kvm_pmu_hyp_counter_mask(vcpu);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise, MDCR_EL2.HPME is the global enable control for event
|
|
|
|
* counters reserved for EL2.
|
|
|
|
*/
|
|
|
|
if (!(vcpu_read_sys_reg(vcpu, MDCR_EL2) & MDCR_EL2_HPME))
|
|
|
|
reg &= ~kvm_pmu_hyp_counter_mask(vcpu);
|
2015-09-08 15:03:26 +08:00
|
|
|
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
2017-07-01 18:26:54 +02:00
|
|
|
static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
|
2017-06-04 14:44:00 +02:00
|
|
|
{
|
|
|
|
struct kvm_pmu *pmu = &vcpu->arch.pmu;
|
2017-07-01 18:26:54 +02:00
|
|
|
bool overflow;
|
|
|
|
|
2024-11-19 16:52:30 -08:00
|
|
|
overflow = kvm_pmu_overflow_status(vcpu);
|
2017-06-04 14:44:00 +02:00
|
|
|
if (pmu->irq_level == overflow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pmu->irq_level = overflow;
|
|
|
|
|
|
|
|
if (likely(irqchip_in_kernel(vcpu->kvm))) {
|
2023-09-27 10:09:01 +01:00
|
|
|
int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu,
|
2017-07-01 18:26:54 +02:00
|
|
|
pmu->irq_num, overflow, pmu);
|
2017-06-04 14:44:00 +02:00
|
|
|
WARN_ON(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 12:51:52 +01:00
|
|
|
bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
struct kvm_pmu *pmu = &vcpu->arch.pmu;
|
|
|
|
struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
|
|
|
|
bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
|
|
|
|
|
|
|
|
if (likely(irqchip_in_kernel(vcpu->kvm)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return pmu->irq_level != run_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reflect the PMU overflow interrupt output level into the kvm_run structure
|
|
|
|
*/
|
|
|
|
void kvm_pmu_update_run(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
struct kvm_sync_regs *regs = &vcpu->run->s.regs;
|
|
|
|
|
|
|
|
/* Populate the timer bitmap for user space */
|
|
|
|
regs->device_irq_level &= ~KVM_ARM_DEV_PMU;
|
|
|
|
if (vcpu->arch.pmu.irq_level)
|
|
|
|
regs->device_irq_level |= KVM_ARM_DEV_PMU;
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:29:19 +08:00
|
|
|
/**
|
|
|
|
* kvm_pmu_flush_hwstate - flush pmu state to cpu
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
*
|
|
|
|
* Check if the PMU has overflowed while we were running in the host, and inject
|
|
|
|
* an interrupt if that was the case.
|
|
|
|
*/
|
|
|
|
void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
kvm_pmu_update_state(vcpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kvm_pmu_sync_hwstate - sync pmu state from cpu
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
*
|
|
|
|
* Check if the PMU has overflowed while we were running in the guest, and
|
|
|
|
* inject an interrupt if that was the case.
|
|
|
|
*/
|
|
|
|
void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
kvm_pmu_update_state(vcpu);
|
|
|
|
}
|
|
|
|
|
2024-01-17 15:07:10 -08:00
|
|
|
/*
|
2020-09-24 12:07:04 +01:00
|
|
|
* When perf interrupt is an NMI, we cannot safely notify the vcpu corresponding
|
|
|
|
* to the event.
|
|
|
|
* This is why we need a callback to do it once outside of the NMI context.
|
|
|
|
*/
|
|
|
|
static void kvm_pmu_perf_overflow_notify_vcpu(struct irq_work *work)
|
|
|
|
{
|
|
|
|
struct kvm_vcpu *vcpu;
|
|
|
|
|
2022-11-13 16:38:31 +00:00
|
|
|
vcpu = container_of(work, struct kvm_vcpu, arch.pmu.overflow_work);
|
2020-09-24 12:07:04 +01:00
|
|
|
kvm_vcpu_kick(vcpu);
|
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:18 +00:00
|
|
|
/*
|
|
|
|
* Perform an increment on any of the counters described in @mask,
|
|
|
|
* generating the overflow if required, and propagate it as a chained
|
|
|
|
* event if possible.
|
|
|
|
*/
|
|
|
|
static void kvm_pmu_counter_increment(struct kvm_vcpu *vcpu,
|
|
|
|
unsigned long mask, u32 event)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2023-10-20 21:40:43 +00:00
|
|
|
if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E))
|
2022-11-13 16:38:18 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Weed out disabled counters */
|
|
|
|
mask &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
|
|
|
|
|
|
|
|
for_each_set_bit(i, &mask, ARMV8_PMU_CYCLE_IDX) {
|
2022-11-13 16:38:32 +00:00
|
|
|
struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
|
2022-11-13 16:38:18 +00:00
|
|
|
u64 type, reg;
|
|
|
|
|
|
|
|
/* Filter on event type */
|
2022-11-13 16:38:23 +00:00
|
|
|
type = __vcpu_sys_reg(vcpu, counter_index_to_evtreg(i));
|
2022-11-13 16:38:18 +00:00
|
|
|
type &= kvm_pmu_event_mask(vcpu->kvm);
|
|
|
|
if (type != event)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Increment this counter */
|
2022-11-13 16:38:23 +00:00
|
|
|
reg = __vcpu_sys_reg(vcpu, counter_index_to_reg(i)) + 1;
|
2022-11-13 16:38:32 +00:00
|
|
|
if (!kvm_pmc_is_64bit(pmc))
|
2022-11-13 16:38:22 +00:00
|
|
|
reg = lower_32_bits(reg);
|
2025-06-03 08:08:21 +01:00
|
|
|
__vcpu_assign_sys_reg(vcpu, counter_index_to_reg(i), reg);
|
2022-11-13 16:38:18 +00:00
|
|
|
|
2022-11-13 16:38:21 +00:00
|
|
|
/* No overflow? move on */
|
2022-11-13 16:38:32 +00:00
|
|
|
if (kvm_pmc_has_64bit_overflow(pmc) ? reg : lower_32_bits(reg))
|
2022-11-13 16:38:18 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Mark overflow */
|
2025-06-03 08:08:22 +01:00
|
|
|
__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, BIT(i));
|
2022-11-13 16:38:18 +00:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
if (kvm_pmu_counter_can_chain(pmc))
|
2022-11-13 16:38:18 +00:00
|
|
|
kvm_pmu_counter_increment(vcpu, BIT(i + 1),
|
|
|
|
ARMV8_PMUV3_PERFCTR_CHAIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:20 +00:00
|
|
|
/* Compute the sample period for a given counter value */
|
2022-11-13 16:38:32 +00:00
|
|
|
static u64 compute_period(struct kvm_pmc *pmc, u64 counter)
|
2022-11-13 16:38:20 +00:00
|
|
|
{
|
|
|
|
u64 val;
|
|
|
|
|
2022-12-05 12:05:51 +00:00
|
|
|
if (kvm_pmc_is_64bit(pmc) && kvm_pmc_has_64bit_overflow(pmc))
|
|
|
|
val = (-counter) & GENMASK(63, 0);
|
|
|
|
else
|
2022-11-13 16:38:20 +00:00
|
|
|
val = (-counter) & GENMASK(31, 0);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2024-01-17 15:07:10 -08:00
|
|
|
/*
|
2017-07-01 18:26:54 +02:00
|
|
|
* When the perf event overflows, set the overflow status and inform the vcpu.
|
2016-02-26 19:29:19 +08:00
|
|
|
*/
|
|
|
|
static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
|
|
|
|
struct perf_sample_data *data,
|
|
|
|
struct pt_regs *regs)
|
|
|
|
{
|
|
|
|
struct kvm_pmc *pmc = perf_event->overflow_handler_context;
|
2019-10-06 10:28:50 +01:00
|
|
|
struct arm_pmu *cpu_pmu = to_arm_pmu(perf_event->pmu);
|
2016-02-26 19:29:19 +08:00
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
|
|
|
int idx = pmc->idx;
|
2019-10-06 10:28:50 +01:00
|
|
|
u64 period;
|
|
|
|
|
|
|
|
cpu_pmu->pmu.stop(perf_event, PERF_EF_UPDATE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset the sample period to the architectural limit,
|
|
|
|
* i.e. the point where the counter overflows.
|
|
|
|
*/
|
2022-11-13 16:38:32 +00:00
|
|
|
period = compute_period(pmc, local64_read(&perf_event->count));
|
2019-10-06 10:28:50 +01:00
|
|
|
|
|
|
|
local64_set(&perf_event->hw.period_left, 0);
|
|
|
|
perf_event->attr.sample_period = period;
|
|
|
|
perf_event->hw.sample_period = period;
|
2016-02-26 19:29:19 +08:00
|
|
|
|
2025-06-03 08:08:22 +01:00
|
|
|
__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, BIT(idx));
|
2017-07-01 18:26:54 +02:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
if (kvm_pmu_counter_can_chain(pmc))
|
2022-11-13 16:38:18 +00:00
|
|
|
kvm_pmu_counter_increment(vcpu, BIT(idx + 1),
|
|
|
|
ARMV8_PMUV3_PERFCTR_CHAIN);
|
|
|
|
|
2017-07-01 18:26:54 +02:00
|
|
|
if (kvm_pmu_overflow_status(vcpu)) {
|
|
|
|
kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
|
2020-09-24 12:07:04 +01:00
|
|
|
|
|
|
|
if (!in_nmi())
|
|
|
|
kvm_vcpu_kick(vcpu);
|
|
|
|
else
|
|
|
|
irq_work_queue(&vcpu->arch.pmu.overflow_work);
|
2017-07-01 18:26:54 +02:00
|
|
|
}
|
2019-10-06 10:28:50 +01:00
|
|
|
|
|
|
|
cpu_pmu->pmu.start(perf_event, PERF_EF_RELOAD);
|
2016-02-26 19:29:19 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 15:49:39 +08:00
|
|
|
/**
|
|
|
|
* kvm_pmu_software_increment - do software increment
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
* @val: the value guest writes to PMSWINC register
|
|
|
|
*/
|
|
|
|
void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
|
|
|
|
{
|
2022-11-13 16:38:18 +00:00
|
|
|
kvm_pmu_counter_increment(vcpu, val, ARMV8_PMUV3_PERFCTR_SW_INCR);
|
2015-09-08 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2015-10-28 12:10:30 +08:00
|
|
|
/**
|
|
|
|
* kvm_pmu_handle_pmcr - handle PMCR register
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
* @val: the value guest writes to PMCR register
|
|
|
|
*/
|
|
|
|
void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2022-11-24 10:44:59 +00:00
|
|
|
/* Fixup PMCR_EL0 to reconcile the PMU version and the LP bit */
|
2024-02-14 13:18:03 +00:00
|
|
|
if (!kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5))
|
2022-11-24 10:44:59 +00:00
|
|
|
val &= ~ARMV8_PMU_PMCR_LP;
|
|
|
|
|
2024-12-17 09:55:32 -08:00
|
|
|
/* Request a reload of the PMU to enable/disable affected counters */
|
|
|
|
if ((__vcpu_sys_reg(vcpu, PMCR_EL0) ^ val) & ARMV8_PMU_PMCR_E)
|
|
|
|
kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
|
|
|
|
|
2023-03-12 20:32:34 -07:00
|
|
|
/* The reset bits don't indicate any state, and shouldn't be saved. */
|
2025-06-03 08:08:21 +01:00
|
|
|
__vcpu_assign_sys_reg(vcpu, PMCR_EL0, (val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P)));
|
2022-11-24 10:44:59 +00:00
|
|
|
|
2015-10-28 12:10:30 +08:00
|
|
|
if (val & ARMV8_PMU_PMCR_C)
|
|
|
|
kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
|
|
|
|
|
|
|
|
if (val & ARMV8_PMU_PMCR_P) {
|
2024-12-17 09:56:11 -08:00
|
|
|
unsigned long mask = kvm_pmu_implemented_counter_mask(vcpu) &
|
|
|
|
~BIT(ARMV8_PMU_CYCLE_IDX);
|
|
|
|
|
2025-02-17 10:24:43 +00:00
|
|
|
if (!vcpu_is_el2(vcpu))
|
|
|
|
mask &= ~kvm_pmu_hyp_counter_mask(vcpu);
|
|
|
|
|
2020-01-24 15:25:35 +01:00
|
|
|
for_each_set_bit(i, &mask, 32)
|
2022-11-13 16:38:32 +00:00
|
|
|
kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, i), 0, true);
|
2015-10-28 12:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc)
|
2015-07-03 14:27:25 +08:00
|
|
|
{
|
2022-11-13 16:38:32 +00:00
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
2024-10-25 18:23:50 +00:00
|
|
|
unsigned int mdcr = __vcpu_sys_reg(vcpu, MDCR_EL2);
|
|
|
|
|
|
|
|
if (!(__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(pmc->idx)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (kvm_pmu_counter_is_hyp(vcpu, pmc->idx))
|
|
|
|
return mdcr & MDCR_EL2_HPME;
|
|
|
|
|
|
|
|
return kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E;
|
2015-07-03 14:27:25 +08:00
|
|
|
}
|
|
|
|
|
2024-10-25 18:23:49 +00:00
|
|
|
static bool kvm_pmc_counts_at_el0(struct kvm_pmc *pmc)
|
|
|
|
{
|
|
|
|
u64 evtreg = kvm_pmc_read_evtreg(pmc);
|
|
|
|
bool nsu = evtreg & ARMV8_PMU_EXCLUDE_NS_EL0;
|
|
|
|
bool u = evtreg & ARMV8_PMU_EXCLUDE_EL0;
|
|
|
|
|
|
|
|
return u == nsu;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool kvm_pmc_counts_at_el1(struct kvm_pmc *pmc)
|
|
|
|
{
|
|
|
|
u64 evtreg = kvm_pmc_read_evtreg(pmc);
|
|
|
|
bool nsk = evtreg & ARMV8_PMU_EXCLUDE_NS_EL1;
|
|
|
|
bool p = evtreg & ARMV8_PMU_EXCLUDE_EL1;
|
|
|
|
|
|
|
|
return p == nsk;
|
|
|
|
}
|
|
|
|
|
2024-10-25 18:23:52 +00:00
|
|
|
static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc)
|
|
|
|
{
|
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
|
|
|
u64 mdcr = __vcpu_sys_reg(vcpu, MDCR_EL2);
|
|
|
|
|
|
|
|
if (!kvm_pmu_counter_is_hyp(vcpu, pmc->idx) && (mdcr & MDCR_EL2_HPMD))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return kvm_pmc_read_evtreg(pmc) & ARMV8_PMU_INCLUDE_EL2;
|
|
|
|
}
|
|
|
|
|
2025-03-05 12:26:38 -08:00
|
|
|
static int kvm_map_pmu_event(struct kvm *kvm, unsigned int eventsel)
|
|
|
|
{
|
|
|
|
struct arm_pmu *pmu = kvm->arch.arm_pmu;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The CPU PMU likely isn't PMUv3; let the driver provide a mapping
|
|
|
|
* for the guest's PMUv3 event ID.
|
|
|
|
*/
|
|
|
|
if (unlikely(pmu->map_pmuv3_event))
|
|
|
|
return pmu->map_pmuv3_event(eventsel);
|
|
|
|
|
|
|
|
return eventsel;
|
|
|
|
}
|
|
|
|
|
2015-07-03 14:27:25 +08:00
|
|
|
/**
|
2019-06-17 20:01:03 +01:00
|
|
|
* kvm_pmu_create_perf_event - create a perf event for a counter
|
2022-11-13 16:38:32 +00:00
|
|
|
* @pmc: Counter context
|
2015-07-03 14:27:25 +08:00
|
|
|
*/
|
2022-11-13 16:38:32 +00:00
|
|
|
static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc)
|
2015-07-03 14:27:25 +08:00
|
|
|
{
|
2022-11-13 16:38:32 +00:00
|
|
|
struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
|
2022-01-27 16:17:56 +00:00
|
|
|
struct arm_pmu *arm_pmu = vcpu->kvm->arch.arm_pmu;
|
2015-07-03 14:27:25 +08:00
|
|
|
struct perf_event *event;
|
|
|
|
struct perf_event_attr attr;
|
2025-03-05 12:26:38 -08:00
|
|
|
int eventsel;
|
|
|
|
u64 evtreg;
|
2019-06-17 20:01:03 +01:00
|
|
|
|
2024-10-25 18:23:49 +00:00
|
|
|
evtreg = kvm_pmc_read_evtreg(pmc);
|
2015-07-03 14:27:25 +08:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
kvm_pmu_stop_counter(pmc);
|
2020-02-12 11:31:02 +00:00
|
|
|
if (pmc->idx == ARMV8_PMU_CYCLE_IDX)
|
|
|
|
eventsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
|
|
|
|
else
|
2024-10-25 18:23:49 +00:00
|
|
|
eventsel = evtreg & kvm_pmu_event_mask(vcpu->kvm);
|
2020-02-12 11:31:02 +00:00
|
|
|
|
2022-11-13 16:38:18 +00:00
|
|
|
/*
|
|
|
|
* Neither SW increment nor chained events need to be backed
|
|
|
|
* by a perf event.
|
|
|
|
*/
|
|
|
|
if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR ||
|
|
|
|
eventsel == ARMV8_PMUV3_PERFCTR_CHAIN)
|
2020-02-12 11:31:02 +00:00
|
|
|
return;
|
2015-07-03 14:27:25 +08:00
|
|
|
|
2020-02-12 11:31:02 +00:00
|
|
|
/*
|
|
|
|
* If we have a filter in place and that the event isn't allowed, do
|
|
|
|
* not install a perf event either.
|
|
|
|
*/
|
|
|
|
if (vcpu->kvm->arch.pmu_filter &&
|
|
|
|
!test_bit(eventsel, vcpu->kvm->arch.pmu_filter))
|
2015-09-08 15:49:39 +08:00
|
|
|
return;
|
|
|
|
|
2025-03-05 12:26:38 -08:00
|
|
|
/*
|
|
|
|
* Don't create an event if we're running on hardware that requires
|
|
|
|
* PMUv3 event translation and we couldn't find a valid mapping.
|
|
|
|
*/
|
|
|
|
eventsel = kvm_map_pmu_event(vcpu->kvm, eventsel);
|
|
|
|
if (eventsel < 0)
|
|
|
|
return;
|
|
|
|
|
2015-07-03 14:27:25 +08:00
|
|
|
memset(&attr, 0, sizeof(struct perf_event_attr));
|
2022-01-27 16:17:56 +00:00
|
|
|
attr.type = arm_pmu->pmu.type;
|
2015-07-03 14:27:25 +08:00
|
|
|
attr.size = sizeof(attr);
|
|
|
|
attr.pinned = 1;
|
2022-11-13 16:38:32 +00:00
|
|
|
attr.disabled = !kvm_pmu_counter_is_enabled(pmc);
|
2024-10-25 18:23:49 +00:00
|
|
|
attr.exclude_user = !kvm_pmc_counts_at_el0(pmc);
|
2015-07-03 14:27:25 +08:00
|
|
|
attr.exclude_hv = 1; /* Don't count EL2 events */
|
|
|
|
attr.exclude_host = 1; /* Don't count host events */
|
2020-02-12 11:31:02 +00:00
|
|
|
attr.config = eventsel;
|
2015-07-03 14:27:25 +08:00
|
|
|
|
2024-10-25 18:23:52 +00:00
|
|
|
/*
|
|
|
|
* Filter events at EL1 (i.e. vEL2) when in a hyp context based on the
|
|
|
|
* guest's EL2 filter.
|
|
|
|
*/
|
|
|
|
if (unlikely(is_hyp_ctxt(vcpu)))
|
|
|
|
attr.exclude_kernel = !kvm_pmc_counts_at_el2(pmc);
|
|
|
|
else
|
|
|
|
attr.exclude_kernel = !kvm_pmc_counts_at_el1(pmc);
|
|
|
|
|
2022-11-13 16:38:18 +00:00
|
|
|
/*
|
|
|
|
* If counting with a 64bit counter, advertise it to the perf
|
2022-11-13 16:38:20 +00:00
|
|
|
* code, carefully dealing with the initial sample period
|
|
|
|
* which also depends on the overflow.
|
2022-11-13 16:38:18 +00:00
|
|
|
*/
|
2022-11-13 16:38:32 +00:00
|
|
|
if (kvm_pmc_is_64bit(pmc))
|
2022-11-13 16:38:18 +00:00
|
|
|
attr.config1 |= PERF_ATTR_CFG1_COUNTER_64BIT;
|
2022-11-13 16:38:20 +00:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
attr.sample_period = compute_period(pmc, kvm_pmu_get_pmc_value(pmc));
|
2015-07-03 14:27:25 +08:00
|
|
|
|
2022-11-13 16:38:18 +00:00
|
|
|
event = perf_event_create_kernel_counter(&attr, -1, current,
|
2016-02-26 19:29:19 +08:00
|
|
|
kvm_pmu_perf_overflow, pmc);
|
2019-06-17 20:01:05 +01:00
|
|
|
|
2015-07-03 14:27:25 +08:00
|
|
|
if (IS_ERR(event)) {
|
|
|
|
pr_err_once("kvm: pmu event creation failed %ld\n",
|
|
|
|
PTR_ERR(event));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pmc->perf_event = event;
|
|
|
|
}
|
2016-01-11 22:46:15 +08:00
|
|
|
|
2019-06-17 20:01:03 +01:00
|
|
|
/**
|
|
|
|
* kvm_pmu_set_counter_event_type - set selected counter to monitor some event
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
* @data: The data guest writes to PMXEVTYPER_EL0
|
|
|
|
* @select_idx: The number of selected counter
|
|
|
|
*
|
|
|
|
* When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
|
|
|
|
* event with given hardware event number. Here we call perf_event API to
|
|
|
|
* emulate this action and create a kernel perf event for it.
|
|
|
|
*/
|
|
|
|
void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
|
|
|
|
u64 select_idx)
|
|
|
|
{
|
2022-11-13 16:38:32 +00:00
|
|
|
struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, select_idx);
|
2023-10-19 18:56:17 +00:00
|
|
|
u64 reg;
|
2020-03-17 11:11:56 +00:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
reg = counter_index_to_evtreg(pmc->idx);
|
2025-06-03 08:08:21 +01:00
|
|
|
__vcpu_assign_sys_reg(vcpu, reg, (data & kvm_pmu_evtyper_mask(vcpu->kvm)));
|
2019-06-17 20:01:05 +01:00
|
|
|
|
2022-11-13 16:38:32 +00:00
|
|
|
kvm_pmu_create_perf_event(pmc);
|
2019-06-17 20:01:03 +01:00
|
|
|
}
|
|
|
|
|
2021-09-19 14:09:49 +01:00
|
|
|
void kvm_host_pmu_init(struct arm_pmu *pmu)
|
|
|
|
{
|
2022-01-27 16:17:57 +00:00
|
|
|
struct arm_pmu_entry *entry;
|
|
|
|
|
2023-08-18 21:39:44 -07:00
|
|
|
/*
|
|
|
|
* Check the sanitised PMU version for the system, as KVM does not
|
|
|
|
* support implementations where PMUv3 exists on a subset of CPUs.
|
|
|
|
*/
|
|
|
|
if (!pmuv3_implemented(kvm_arm_pmu_get_pmuver_limit()))
|
2022-01-27 16:17:57 +00:00
|
|
|
return;
|
|
|
|
|
2025-03-05 12:26:34 -08:00
|
|
|
guard(mutex)(&arm_pmus_lock);
|
2022-01-27 16:17:57 +00:00
|
|
|
|
|
|
|
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
|
|
|
|
if (!entry)
|
2025-03-05 12:26:34 -08:00
|
|
|
return;
|
2022-01-27 16:17:57 +00:00
|
|
|
|
|
|
|
entry->arm_pmu = pmu;
|
|
|
|
list_add_tail(&entry->entry, &arm_pmus);
|
2021-09-19 14:09:49 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 16:17:56 +00:00
|
|
|
static struct arm_pmu *kvm_pmu_probe_armpmu(void)
|
2020-03-17 11:11:56 +00:00
|
|
|
{
|
2023-05-25 21:27:21 +00:00
|
|
|
struct arm_pmu_entry *entry;
|
2025-03-05 12:26:34 -08:00
|
|
|
struct arm_pmu *pmu;
|
2023-05-25 21:27:21 +00:00
|
|
|
int cpu;
|
2020-03-17 11:11:56 +00:00
|
|
|
|
2025-03-05 12:26:34 -08:00
|
|
|
guard(mutex)(&arm_pmus_lock);
|
2020-03-17 11:11:56 +00:00
|
|
|
|
2023-06-06 18:48:14 +00:00
|
|
|
/*
|
|
|
|
* It is safe to use a stale cpu to iterate the list of PMUs so long as
|
|
|
|
* the same value is used for the entirety of the loop. Given this, and
|
|
|
|
* the fact that no percpu data is used for the lookup there is no need
|
|
|
|
* to disable preemption.
|
|
|
|
*
|
|
|
|
* It is still necessary to get a valid cpu, though, to probe for the
|
|
|
|
* default PMU instance as userspace is not required to specify a PMU
|
|
|
|
* type. In order to uphold the preexisting behavior KVM selects the
|
2023-10-20 21:40:42 +00:00
|
|
|
* PMU instance for the core during vcpu init. A dependent use
|
|
|
|
* case would be a user with disdain of all things big.LITTLE that
|
|
|
|
* affines the VMM to a particular cluster of cores.
|
2023-06-06 18:48:14 +00:00
|
|
|
*
|
|
|
|
* In any case, userspace should just do the sane thing and use the UAPI
|
|
|
|
* to select a PMU type directly. But, be wary of the baggage being
|
|
|
|
* carried here.
|
|
|
|
*/
|
|
|
|
cpu = raw_smp_processor_id();
|
2023-05-25 21:27:21 +00:00
|
|
|
list_for_each_entry(entry, &arm_pmus, entry) {
|
2025-03-05 12:26:34 -08:00
|
|
|
pmu = entry->arm_pmu;
|
2020-03-17 11:11:56 +00:00
|
|
|
|
2025-03-05 12:26:34 -08:00
|
|
|
if (cpumask_test_cpu(cpu, &pmu->supported_cpus))
|
|
|
|
return pmu;
|
2020-03-17 11:11:56 +00:00
|
|
|
}
|
|
|
|
|
2025-03-05 12:26:34 -08:00
|
|
|
return NULL;
|
2020-03-17 11:11:56 +00:00
|
|
|
}
|
|
|
|
|
2025-03-05 12:26:30 -08:00
|
|
|
static u64 __compute_pmceid(struct arm_pmu *pmu, bool pmceid1)
|
|
|
|
{
|
|
|
|
u32 hi[2], lo[2];
|
|
|
|
|
|
|
|
bitmap_to_arr32(lo, pmu->pmceid_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
|
|
|
|
bitmap_to_arr32(hi, pmu->pmceid_ext_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
|
|
|
|
|
|
|
|
return ((u64)hi[pmceid1] << 32) | lo[pmceid1];
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 compute_pmceid0(struct arm_pmu *pmu)
|
|
|
|
{
|
|
|
|
u64 val = __compute_pmceid(pmu, 0);
|
|
|
|
|
2025-03-05 12:26:31 -08:00
|
|
|
/* always support SW_INCR */
|
|
|
|
val |= BIT(ARMV8_PMUV3_PERFCTR_SW_INCR);
|
2025-03-05 12:26:30 -08:00
|
|
|
/* always support CHAIN */
|
|
|
|
val |= BIT(ARMV8_PMUV3_PERFCTR_CHAIN);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 compute_pmceid1(struct arm_pmu *pmu)
|
|
|
|
{
|
|
|
|
u64 val = __compute_pmceid(pmu, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled
|
|
|
|
* as RAZ
|
|
|
|
*/
|
|
|
|
val &= ~(BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32) |
|
|
|
|
BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND - 32) |
|
|
|
|
BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND - 32));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:11:24 +00:00
|
|
|
u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
|
|
|
|
{
|
2025-03-05 12:26:30 -08:00
|
|
|
struct arm_pmu *cpu_pmu = vcpu->kvm->arch.arm_pmu;
|
2020-03-12 16:11:24 +00:00
|
|
|
unsigned long *bmap = vcpu->kvm->arch.pmu_filter;
|
|
|
|
u64 val, mask = 0;
|
2021-01-21 10:56:36 +00:00
|
|
|
int base, i, nr_events;
|
2020-03-12 16:11:24 +00:00
|
|
|
|
|
|
|
if (!pmceid1) {
|
2025-03-05 12:26:30 -08:00
|
|
|
val = compute_pmceid0(cpu_pmu);
|
2020-03-12 16:11:24 +00:00
|
|
|
base = 0;
|
|
|
|
} else {
|
2025-03-05 12:26:30 -08:00
|
|
|
val = compute_pmceid1(cpu_pmu);
|
2020-03-12 16:11:24 +00:00
|
|
|
base = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bmap)
|
|
|
|
return val;
|
|
|
|
|
2021-01-21 10:56:36 +00:00
|
|
|
nr_events = kvm_pmu_event_mask(vcpu->kvm) + 1;
|
|
|
|
|
2020-03-12 16:11:24 +00:00
|
|
|
for (i = 0; i < 32; i += 8) {
|
|
|
|
u64 byte;
|
|
|
|
|
|
|
|
byte = bitmap_get_value8(bmap, base + i);
|
|
|
|
mask |= byte << i;
|
2021-01-21 10:56:36 +00:00
|
|
|
if (nr_events >= (0x4000 + base + 32)) {
|
|
|
|
byte = bitmap_get_value8(bmap, 0x4000 + base + i);
|
|
|
|
mask |= byte << (32 + i);
|
|
|
|
}
|
2020-03-12 16:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return val & mask;
|
|
|
|
}
|
|
|
|
|
2023-10-20 21:40:46 +00:00
|
|
|
void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2024-10-25 18:23:47 +00:00
|
|
|
u64 mask = kvm_pmu_implemented_counter_mask(vcpu);
|
2023-10-20 21:40:46 +00:00
|
|
|
|
2025-06-03 08:08:22 +01:00
|
|
|
__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, &=, mask);
|
|
|
|
__vcpu_rmw_sys_reg(vcpu, PMINTENSET_EL1, &=, mask);
|
|
|
|
__vcpu_rmw_sys_reg(vcpu, PMCNTENSET_EL0, &=, mask);
|
2024-12-17 09:55:32 -08:00
|
|
|
|
|
|
|
kvm_pmu_reprogram_counter_mask(vcpu, mask);
|
2023-10-20 21:40:46 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 13:41:02 +02:00
|
|
|
int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
|
2016-01-11 21:35:32 +08:00
|
|
|
{
|
2020-11-26 14:49:16 +00:00
|
|
|
if (!vcpu->arch.pmu.created)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-09-26 18:51:47 -07:00
|
|
|
/*
|
2017-05-02 13:41:02 +02:00
|
|
|
* A valid interrupt configuration for the PMU is either to have a
|
|
|
|
* properly configured interrupt number and using an in-kernel
|
2017-05-16 19:53:50 +02:00
|
|
|
* irqchip, or to not have an in-kernel GIC and not set an IRQ.
|
2016-09-26 18:51:47 -07:00
|
|
|
*/
|
2017-05-16 19:53:50 +02:00
|
|
|
if (irqchip_in_kernel(vcpu->kvm)) {
|
|
|
|
int irq = vcpu->arch.pmu.irq_num;
|
|
|
|
/*
|
|
|
|
* If we are using an in-kernel vgic, at this point we know
|
|
|
|
* the vgic will be initialized, so we can check the PMU irq
|
|
|
|
* number against the dimensions of the vgic and make sure
|
|
|
|
* it's valid.
|
|
|
|
*/
|
|
|
|
if (!irq_is_ppi(irq) && !vgic_valid_spi(vcpu->kvm, irq))
|
|
|
|
return -EINVAL;
|
|
|
|
} else if (kvm_arm_pmu_irq_initialized(vcpu)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-05-02 13:41:02 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
if (irqchip_in_kernel(vcpu->kvm)) {
|
2017-05-04 13:32:53 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-05-02 13:41:02 +02:00
|
|
|
/*
|
|
|
|
* If using the PMU with an in-kernel virtual GIC
|
|
|
|
* implementation, we require the GIC to be already
|
|
|
|
* initialized when initializing the PMU.
|
|
|
|
*/
|
|
|
|
if (!vgic_initialized(vcpu->kvm))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (!kvm_arm_pmu_irq_initialized(vcpu))
|
|
|
|
return -ENXIO;
|
2017-05-04 13:32:53 +02:00
|
|
|
|
|
|
|
ret = kvm_vgic_set_owner(vcpu, vcpu->arch.pmu.irq_num,
|
|
|
|
&vcpu->arch.pmu);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-02 13:41:02 +02:00
|
|
|
}
|
2016-01-11 21:35:32 +08:00
|
|
|
|
2020-09-24 12:07:04 +01:00
|
|
|
init_irq_work(&vcpu->arch.pmu.overflow_work,
|
|
|
|
kvm_pmu_perf_overflow_notify_vcpu);
|
|
|
|
|
2017-05-02 13:41:02 +02:00
|
|
|
vcpu->arch.pmu.created = true;
|
2016-01-11 21:35:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:32:29 +07:00
|
|
|
/*
|
|
|
|
* For one VM the interrupt type must be same for each vcpu.
|
|
|
|
* As a PPI, the interrupt number is the same for all vcpus,
|
|
|
|
* while as an SPI it must be a separate number per vcpu.
|
|
|
|
*/
|
|
|
|
static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
|
2016-01-11 21:35:32 +08:00
|
|
|
{
|
2021-11-16 16:04:02 +00:00
|
|
|
unsigned long i;
|
2016-01-11 21:35:32 +08:00
|
|
|
struct kvm_vcpu *vcpu;
|
|
|
|
|
|
|
|
kvm_for_each_vcpu(i, vcpu, kvm) {
|
|
|
|
if (!kvm_arm_pmu_irq_initialized(vcpu))
|
|
|
|
continue;
|
|
|
|
|
2016-03-07 17:32:29 +07:00
|
|
|
if (irq_is_ppi(irq)) {
|
2016-01-11 21:35:32 +08:00
|
|
|
if (vcpu->arch.pmu.irq_num != irq)
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (vcpu->arch.pmu.irq_num == irq)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-20 21:40:44 +00:00
|
|
|
/**
|
|
|
|
* kvm_arm_pmu_get_max_counters - Return the max number of PMU counters.
|
|
|
|
* @kvm: The kvm pointer
|
|
|
|
*/
|
|
|
|
u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
struct arm_pmu *arm_pmu = kvm->arch.arm_pmu;
|
|
|
|
|
2025-03-05 12:30:21 -08:00
|
|
|
/*
|
|
|
|
* PMUv3 requires that all event counters are capable of counting any
|
|
|
|
* event, though the same may not be true of non-PMUv3 hardware.
|
|
|
|
*/
|
|
|
|
if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS))
|
|
|
|
return 1;
|
|
|
|
|
2023-10-20 21:40:44 +00:00
|
|
|
/*
|
2024-07-31 10:51:18 -06:00
|
|
|
* The arm_pmu->cntr_mask considers the fixed counter(s) as well.
|
|
|
|
* Ignore those and return only the general-purpose counters.
|
2023-10-20 21:40:44 +00:00
|
|
|
*/
|
2024-07-31 10:51:18 -06:00
|
|
|
return bitmap_weight(arm_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS);
|
2023-10-20 21:40:44 +00:00
|
|
|
}
|
|
|
|
|
2025-02-17 10:17:20 +00:00
|
|
|
static void kvm_arm_set_nr_counters(struct kvm *kvm, unsigned int nr)
|
|
|
|
{
|
|
|
|
kvm->arch.nr_pmu_counters = nr;
|
|
|
|
|
|
|
|
/* Reset MDCR_EL2.HPMN behind the vcpus' back... */
|
|
|
|
if (test_bit(KVM_ARM_VCPU_HAS_EL2, kvm->arch.vcpu_features)) {
|
|
|
|
struct kvm_vcpu *vcpu;
|
|
|
|
unsigned long i;
|
|
|
|
|
|
|
|
kvm_for_each_vcpu(i, vcpu, kvm) {
|
|
|
|
u64 val = __vcpu_sys_reg(vcpu, MDCR_EL2);
|
|
|
|
val &= ~MDCR_EL2_HPMN;
|
|
|
|
val |= FIELD_PREP(MDCR_EL2_HPMN, kvm->arch.nr_pmu_counters);
|
2025-06-03 08:08:21 +01:00
|
|
|
__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);
|
2025-02-17 10:17:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 21:40:41 +00:00
|
|
|
static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
|
|
|
|
{
|
|
|
|
lockdep_assert_held(&kvm->arch.config_lock);
|
|
|
|
|
|
|
|
kvm->arch.arm_pmu = arm_pmu;
|
2025-02-17 10:17:20 +00:00
|
|
|
kvm_arm_set_nr_counters(kvm, kvm_arm_pmu_get_max_counters(kvm));
|
2023-10-20 21:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kvm_arm_set_default_pmu - No PMU set, get the default one.
|
|
|
|
* @kvm: The kvm pointer
|
|
|
|
*
|
|
|
|
* The observant among you will notice that the supported_cpus
|
|
|
|
* mask does not get updated for the default PMU even though it
|
|
|
|
* is quite possible the selected instance supports only a
|
|
|
|
* subset of cores in the system. This is intentional, and
|
|
|
|
* upholds the preexisting behavior on heterogeneous systems
|
|
|
|
* where vCPUs can be scheduled on any core but the guest
|
|
|
|
* counters could stop working.
|
|
|
|
*/
|
2023-10-20 21:40:42 +00:00
|
|
|
int kvm_arm_set_default_pmu(struct kvm *kvm)
|
2023-10-20 21:40:41 +00:00
|
|
|
{
|
|
|
|
struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu();
|
|
|
|
|
|
|
|
if (!arm_pmu)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
kvm_arm_set_pmu(kvm, arm_pmu);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_SET_PMU attribute
When KVM creates an event and there are more than one PMUs present on the
system, perf_init_event() will go through the list of available PMUs and
will choose the first one that can create the event. The order of the PMUs
in this list depends on the probe order, which can change under various
circumstances, for example if the order of the PMU nodes change in the DTB
or if asynchronous driver probing is enabled on the kernel command line
(with the driver_async_probe=armv8-pmu option).
Another consequence of this approach is that on heteregeneous systems all
virtual machines that KVM creates will use the same PMU. This might cause
unexpected behaviour for userspace: when a VCPU is executing on the
physical CPU that uses this default PMU, PMU events in the guest work
correctly; but when the same VCPU executes on another CPU, PMU events in
the guest will suddenly stop counting.
Fortunately, perf core allows user to specify on which PMU to create an
event by using the perf_event_attr->type field, which is used by
perf_init_event() as an index in the radix tree of available PMUs.
Add the KVM_ARM_VCPU_PMU_V3_CTRL(KVM_ARM_VCPU_PMU_V3_SET_PMU) VCPU
attribute to allow userspace to specify the arm_pmu that KVM will use when
creating events for that VCPU. KVM will make no attempt to run the VCPU on
the physical CPUs that share the PMU, leaving it up to userspace to manage
the VCPU threads' affinity accordingly.
To ensure that KVM doesn't expose an asymmetric system to the guest, the
PMU set for one VCPU will be used by all other VCPUs. Once a VCPU has run,
the PMU cannot be changed in order to avoid changing the list of available
events for a VCPU, or to change the semantics of existing events.
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220127161759.53553-6-alexandru.elisei@arm.com
2022-01-27 16:17:58 +00:00
|
|
|
static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)
|
|
|
|
{
|
|
|
|
struct kvm *kvm = vcpu->kvm;
|
|
|
|
struct arm_pmu_entry *entry;
|
|
|
|
struct arm_pmu *arm_pmu;
|
|
|
|
int ret = -ENXIO;
|
|
|
|
|
2023-03-27 16:47:46 +00:00
|
|
|
lockdep_assert_held(&kvm->arch.config_lock);
|
KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_SET_PMU attribute
When KVM creates an event and there are more than one PMUs present on the
system, perf_init_event() will go through the list of available PMUs and
will choose the first one that can create the event. The order of the PMUs
in this list depends on the probe order, which can change under various
circumstances, for example if the order of the PMU nodes change in the DTB
or if asynchronous driver probing is enabled on the kernel command line
(with the driver_async_probe=armv8-pmu option).
Another consequence of this approach is that on heteregeneous systems all
virtual machines that KVM creates will use the same PMU. This might cause
unexpected behaviour for userspace: when a VCPU is executing on the
physical CPU that uses this default PMU, PMU events in the guest work
correctly; but when the same VCPU executes on another CPU, PMU events in
the guest will suddenly stop counting.
Fortunately, perf core allows user to specify on which PMU to create an
event by using the perf_event_attr->type field, which is used by
perf_init_event() as an index in the radix tree of available PMUs.
Add the KVM_ARM_VCPU_PMU_V3_CTRL(KVM_ARM_VCPU_PMU_V3_SET_PMU) VCPU
attribute to allow userspace to specify the arm_pmu that KVM will use when
creating events for that VCPU. KVM will make no attempt to run the VCPU on
the physical CPUs that share the PMU, leaving it up to userspace to manage
the VCPU threads' affinity accordingly.
To ensure that KVM doesn't expose an asymmetric system to the guest, the
PMU set for one VCPU will be used by all other VCPUs. Once a VCPU has run,
the PMU cannot be changed in order to avoid changing the list of available
events for a VCPU, or to change the semantics of existing events.
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220127161759.53553-6-alexandru.elisei@arm.com
2022-01-27 16:17:58 +00:00
|
|
|
mutex_lock(&arm_pmus_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(entry, &arm_pmus, entry) {
|
|
|
|
arm_pmu = entry->arm_pmu;
|
|
|
|
if (arm_pmu->pmu.type == pmu_id) {
|
2023-04-04 15:40:39 +00:00
|
|
|
if (kvm_vm_has_ran_once(kvm) ||
|
KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_SET_PMU attribute
When KVM creates an event and there are more than one PMUs present on the
system, perf_init_event() will go through the list of available PMUs and
will choose the first one that can create the event. The order of the PMUs
in this list depends on the probe order, which can change under various
circumstances, for example if the order of the PMU nodes change in the DTB
or if asynchronous driver probing is enabled on the kernel command line
(with the driver_async_probe=armv8-pmu option).
Another consequence of this approach is that on heteregeneous systems all
virtual machines that KVM creates will use the same PMU. This might cause
unexpected behaviour for userspace: when a VCPU is executing on the
physical CPU that uses this default PMU, PMU events in the guest work
correctly; but when the same VCPU executes on another CPU, PMU events in
the guest will suddenly stop counting.
Fortunately, perf core allows user to specify on which PMU to create an
event by using the perf_event_attr->type field, which is used by
perf_init_event() as an index in the radix tree of available PMUs.
Add the KVM_ARM_VCPU_PMU_V3_CTRL(KVM_ARM_VCPU_PMU_V3_SET_PMU) VCPU
attribute to allow userspace to specify the arm_pmu that KVM will use when
creating events for that VCPU. KVM will make no attempt to run the VCPU on
the physical CPUs that share the PMU, leaving it up to userspace to manage
the VCPU threads' affinity accordingly.
To ensure that KVM doesn't expose an asymmetric system to the guest, the
PMU set for one VCPU will be used by all other VCPUs. Once a VCPU has run,
the PMU cannot be changed in order to avoid changing the list of available
events for a VCPU, or to change the semantics of existing events.
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220127161759.53553-6-alexandru.elisei@arm.com
2022-01-27 16:17:58 +00:00
|
|
|
(kvm->arch.pmu_filter && kvm->arch.arm_pmu != arm_pmu)) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-20 21:40:41 +00:00
|
|
|
kvm_arm_set_pmu(kvm, arm_pmu);
|
2022-01-27 16:17:59 +00:00
|
|
|
cpumask_copy(kvm->arch.supported_cpus, &arm_pmu->supported_cpus);
|
KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_SET_PMU attribute
When KVM creates an event and there are more than one PMUs present on the
system, perf_init_event() will go through the list of available PMUs and
will choose the first one that can create the event. The order of the PMUs
in this list depends on the probe order, which can change under various
circumstances, for example if the order of the PMU nodes change in the DTB
or if asynchronous driver probing is enabled on the kernel command line
(with the driver_async_probe=armv8-pmu option).
Another consequence of this approach is that on heteregeneous systems all
virtual machines that KVM creates will use the same PMU. This might cause
unexpected behaviour for userspace: when a VCPU is executing on the
physical CPU that uses this default PMU, PMU events in the guest work
correctly; but when the same VCPU executes on another CPU, PMU events in
the guest will suddenly stop counting.
Fortunately, perf core allows user to specify on which PMU to create an
event by using the perf_event_attr->type field, which is used by
perf_init_event() as an index in the radix tree of available PMUs.
Add the KVM_ARM_VCPU_PMU_V3_CTRL(KVM_ARM_VCPU_PMU_V3_SET_PMU) VCPU
attribute to allow userspace to specify the arm_pmu that KVM will use when
creating events for that VCPU. KVM will make no attempt to run the VCPU on
the physical CPUs that share the PMU, leaving it up to userspace to manage
the VCPU threads' affinity accordingly.
To ensure that KVM doesn't expose an asymmetric system to the guest, the
PMU set for one VCPU will be used by all other VCPUs. Once a VCPU has run,
the PMU cannot be changed in order to avoid changing the list of available
events for a VCPU, or to change the semantics of existing events.
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220127161759.53553-6-alexandru.elisei@arm.com
2022-01-27 16:17:58 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&arm_pmus_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2025-04-09 11:53:15 +01:00
|
|
|
static int kvm_arm_pmu_v3_set_nr_counters(struct kvm_vcpu *vcpu, unsigned int n)
|
|
|
|
{
|
|
|
|
struct kvm *kvm = vcpu->kvm;
|
|
|
|
|
|
|
|
if (!kvm->arch.arm_pmu)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (n > kvm_arm_pmu_get_max_counters(kvm))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
kvm_arm_set_nr_counters(kvm, n);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-11 21:35:32 +08:00
|
|
|
int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
|
|
|
|
{
|
2022-01-27 16:17:54 +00:00
|
|
|
struct kvm *kvm = vcpu->kvm;
|
|
|
|
|
2023-03-27 16:47:46 +00:00
|
|
|
lockdep_assert_held(&kvm->arch.config_lock);
|
|
|
|
|
2020-11-12 18:13:27 +00:00
|
|
|
if (!kvm_vcpu_has_pmu(vcpu))
|
2020-03-12 17:27:36 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (vcpu->arch.pmu.created)
|
|
|
|
return -EBUSY;
|
|
|
|
|
2016-01-11 21:35:32 +08:00
|
|
|
switch (attr->attr) {
|
|
|
|
case KVM_ARM_VCPU_PMU_V3_IRQ: {
|
|
|
|
int __user *uaddr = (int __user *)(long)attr->addr;
|
|
|
|
int irq;
|
|
|
|
|
2022-01-27 16:17:54 +00:00
|
|
|
if (!irqchip_in_kernel(kvm))
|
2017-05-02 13:41:02 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-01-11 21:35:32 +08:00
|
|
|
if (get_user(irq, uaddr))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2016-03-07 17:32:29 +07:00
|
|
|
/* The PMU overflow interrupt can be a PPI or a valid SPI. */
|
2017-05-16 19:53:50 +02:00
|
|
|
if (!(irq_is_ppi(irq) || irq_is_spi(irq)))
|
2016-03-07 17:32:29 +07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2022-01-27 16:17:54 +00:00
|
|
|
if (!pmu_irq_is_valid(kvm, irq))
|
2016-01-11 21:35:32 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (kvm_arm_pmu_irq_initialized(vcpu))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
kvm_debug("Set kvm ARM PMU irq: %d\n", irq);
|
|
|
|
vcpu->arch.pmu.irq_num = irq;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-12 11:31:02 +00:00
|
|
|
case KVM_ARM_VCPU_PMU_V3_FILTER: {
|
2023-08-18 21:39:45 -07:00
|
|
|
u8 pmuver = kvm_arm_pmu_get_pmuver_limit();
|
2020-02-12 11:31:02 +00:00
|
|
|
struct kvm_pmu_event_filter __user *uaddr;
|
|
|
|
struct kvm_pmu_event_filter filter;
|
|
|
|
int nr_events;
|
|
|
|
|
2023-08-18 21:39:45 -07:00
|
|
|
/*
|
|
|
|
* Allow userspace to specify an event filter for the entire
|
|
|
|
* event range supported by PMUVer of the hardware, rather
|
|
|
|
* than the guest's PMUVer for KVM backward compatibility.
|
|
|
|
*/
|
|
|
|
nr_events = __kvm_pmu_event_mask(pmuver) + 1;
|
2020-02-12 11:31:02 +00:00
|
|
|
|
|
|
|
uaddr = (struct kvm_pmu_event_filter __user *)(long)attr->addr;
|
|
|
|
|
|
|
|
if (copy_from_user(&filter, uaddr, sizeof(filter)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (((u32)filter.base_event + filter.nevents) > nr_events ||
|
|
|
|
(filter.action != KVM_PMU_EVENT_ALLOW &&
|
|
|
|
filter.action != KVM_PMU_EVENT_DENY))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2023-04-21 09:43:38 +01:00
|
|
|
if (kvm_vm_has_ran_once(kvm))
|
2022-01-27 16:17:54 +00:00
|
|
|
return -EBUSY;
|
2020-02-12 11:31:02 +00:00
|
|
|
|
2022-01-27 16:17:54 +00:00
|
|
|
if (!kvm->arch.pmu_filter) {
|
|
|
|
kvm->arch.pmu_filter = bitmap_alloc(nr_events, GFP_KERNEL_ACCOUNT);
|
2023-03-27 16:47:46 +00:00
|
|
|
if (!kvm->arch.pmu_filter)
|
2020-02-12 11:31:02 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The default depends on the first applied filter.
|
|
|
|
* If it allows events, the default is to deny.
|
|
|
|
* Conversely, if the first filter denies a set of
|
|
|
|
* events, the default is to allow.
|
|
|
|
*/
|
|
|
|
if (filter.action == KVM_PMU_EVENT_ALLOW)
|
2022-01-27 16:17:54 +00:00
|
|
|
bitmap_zero(kvm->arch.pmu_filter, nr_events);
|
2020-02-12 11:31:02 +00:00
|
|
|
else
|
2022-01-27 16:17:54 +00:00
|
|
|
bitmap_fill(kvm->arch.pmu_filter, nr_events);
|
2020-02-12 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (filter.action == KVM_PMU_EVENT_ALLOW)
|
2022-01-27 16:17:54 +00:00
|
|
|
bitmap_set(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
|
2020-02-12 11:31:02 +00:00
|
|
|
else
|
2022-01-27 16:17:54 +00:00
|
|
|
bitmap_clear(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
|
2020-02-12 11:31:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_SET_PMU attribute
When KVM creates an event and there are more than one PMUs present on the
system, perf_init_event() will go through the list of available PMUs and
will choose the first one that can create the event. The order of the PMUs
in this list depends on the probe order, which can change under various
circumstances, for example if the order of the PMU nodes change in the DTB
or if asynchronous driver probing is enabled on the kernel command line
(with the driver_async_probe=armv8-pmu option).
Another consequence of this approach is that on heteregeneous systems all
virtual machines that KVM creates will use the same PMU. This might cause
unexpected behaviour for userspace: when a VCPU is executing on the
physical CPU that uses this default PMU, PMU events in the guest work
correctly; but when the same VCPU executes on another CPU, PMU events in
the guest will suddenly stop counting.
Fortunately, perf core allows user to specify on which PMU to create an
event by using the perf_event_attr->type field, which is used by
perf_init_event() as an index in the radix tree of available PMUs.
Add the KVM_ARM_VCPU_PMU_V3_CTRL(KVM_ARM_VCPU_PMU_V3_SET_PMU) VCPU
attribute to allow userspace to specify the arm_pmu that KVM will use when
creating events for that VCPU. KVM will make no attempt to run the VCPU on
the physical CPUs that share the PMU, leaving it up to userspace to manage
the VCPU threads' affinity accordingly.
To ensure that KVM doesn't expose an asymmetric system to the guest, the
PMU set for one VCPU will be used by all other VCPUs. Once a VCPU has run,
the PMU cannot be changed in order to avoid changing the list of available
events for a VCPU, or to change the semantics of existing events.
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220127161759.53553-6-alexandru.elisei@arm.com
2022-01-27 16:17:58 +00:00
|
|
|
case KVM_ARM_VCPU_PMU_V3_SET_PMU: {
|
|
|
|
int __user *uaddr = (int __user *)(long)attr->addr;
|
|
|
|
int pmu_id;
|
|
|
|
|
|
|
|
if (get_user(pmu_id, uaddr))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return kvm_arm_pmu_v3_set_pmu(vcpu, pmu_id);
|
|
|
|
}
|
2025-04-09 11:53:15 +01:00
|
|
|
case KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS: {
|
|
|
|
unsigned int __user *uaddr = (unsigned int __user *)(long)attr->addr;
|
|
|
|
unsigned int n;
|
|
|
|
|
|
|
|
if (get_user(n, uaddr))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return kvm_arm_pmu_v3_set_nr_counters(vcpu, n);
|
|
|
|
}
|
2016-01-11 21:35:32 +08:00
|
|
|
case KVM_ARM_VCPU_PMU_V3_INIT:
|
|
|
|
return kvm_arm_pmu_v3_init(vcpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
|
|
|
|
{
|
|
|
|
switch (attr->attr) {
|
|
|
|
case KVM_ARM_VCPU_PMU_V3_IRQ: {
|
|
|
|
int __user *uaddr = (int __user *)(long)attr->addr;
|
|
|
|
int irq;
|
|
|
|
|
2017-05-02 13:41:02 +02:00
|
|
|
if (!irqchip_in_kernel(vcpu->kvm))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-11-13 16:39:44 +00:00
|
|
|
if (!kvm_vcpu_has_pmu(vcpu))
|
2016-01-11 21:35:32 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (!kvm_arm_pmu_irq_initialized(vcpu))
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
irq = vcpu->arch.pmu.irq_num;
|
|
|
|
return put_user(irq, uaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
|
|
|
|
{
|
|
|
|
switch (attr->attr) {
|
|
|
|
case KVM_ARM_VCPU_PMU_V3_IRQ:
|
|
|
|
case KVM_ARM_VCPU_PMU_V3_INIT:
|
2020-02-12 11:31:02 +00:00
|
|
|
case KVM_ARM_VCPU_PMU_V3_FILTER:
|
KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_SET_PMU attribute
When KVM creates an event and there are more than one PMUs present on the
system, perf_init_event() will go through the list of available PMUs and
will choose the first one that can create the event. The order of the PMUs
in this list depends on the probe order, which can change under various
circumstances, for example if the order of the PMU nodes change in the DTB
or if asynchronous driver probing is enabled on the kernel command line
(with the driver_async_probe=armv8-pmu option).
Another consequence of this approach is that on heteregeneous systems all
virtual machines that KVM creates will use the same PMU. This might cause
unexpected behaviour for userspace: when a VCPU is executing on the
physical CPU that uses this default PMU, PMU events in the guest work
correctly; but when the same VCPU executes on another CPU, PMU events in
the guest will suddenly stop counting.
Fortunately, perf core allows user to specify on which PMU to create an
event by using the perf_event_attr->type field, which is used by
perf_init_event() as an index in the radix tree of available PMUs.
Add the KVM_ARM_VCPU_PMU_V3_CTRL(KVM_ARM_VCPU_PMU_V3_SET_PMU) VCPU
attribute to allow userspace to specify the arm_pmu that KVM will use when
creating events for that VCPU. KVM will make no attempt to run the VCPU on
the physical CPUs that share the PMU, leaving it up to userspace to manage
the VCPU threads' affinity accordingly.
To ensure that KVM doesn't expose an asymmetric system to the guest, the
PMU set for one VCPU will be used by all other VCPUs. Once a VCPU has run,
the PMU cannot be changed in order to avoid changing the list of available
events for a VCPU, or to change the semantics of existing events.
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220127161759.53553-6-alexandru.elisei@arm.com
2022-01-27 16:17:58 +00:00
|
|
|
case KVM_ARM_VCPU_PMU_V3_SET_PMU:
|
2025-04-09 11:53:15 +01:00
|
|
|
case KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS:
|
2020-11-12 18:13:27 +00:00
|
|
|
if (kvm_vcpu_has_pmu(vcpu))
|
2016-01-11 21:35:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
2022-11-13 16:38:26 +00:00
|
|
|
|
|
|
|
u8 kvm_arm_pmu_get_pmuver_limit(void)
|
|
|
|
{
|
2025-03-05 12:26:35 -08:00
|
|
|
unsigned int pmuver;
|
2022-11-13 16:38:26 +00:00
|
|
|
|
2025-03-05 12:26:35 -08:00
|
|
|
pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer,
|
|
|
|
read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1));
|
|
|
|
|
2025-03-05 12:26:37 -08:00
|
|
|
/*
|
|
|
|
* Spoof a barebones PMUv3 implementation if the system supports IMPDEF
|
|
|
|
* traps of the PMUv3 sysregs
|
|
|
|
*/
|
|
|
|
if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS))
|
|
|
|
return ID_AA64DFR0_EL1_PMUVer_IMP;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise, treat IMPLEMENTATION DEFINED functionality as
|
|
|
|
* unimplemented
|
|
|
|
*/
|
2025-03-05 12:26:35 -08:00
|
|
|
if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return min(pmuver, ID_AA64DFR0_EL1_PMUVer_V3P5);
|
2022-11-13 16:38:26 +00:00
|
|
|
}
|
2023-10-20 21:40:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* kvm_vcpu_read_pmcr - Read PMCR_EL0 register for the vCPU
|
|
|
|
* @vcpu: The vcpu pointer
|
|
|
|
*/
|
|
|
|
u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2023-12-11 16:13:14 +00:00
|
|
|
u64 pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
|
2025-04-11 12:02:20 +01:00
|
|
|
u64 n = vcpu->kvm->arch.nr_pmu_counters;
|
2023-10-20 21:40:44 +00:00
|
|
|
|
2025-04-11 12:02:20 +01:00
|
|
|
if (vcpu_has_nv(vcpu) && !vcpu_is_el2(vcpu))
|
|
|
|
n = FIELD_GET(MDCR_EL2_HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
|
|
|
|
|
|
|
|
return u64_replace_bits(pmcr, n, ARMV8_PMU_PMCR_N);
|
2023-10-20 21:40:43 +00:00
|
|
|
}
|
2024-10-25 18:25:59 +00:00
|
|
|
|
|
|
|
void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
bool reprogrammed = false;
|
|
|
|
unsigned long mask;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mask = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
|
|
|
|
for_each_set_bit(i, &mask, 32) {
|
|
|
|
struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only need to reconfigure events where the filter is
|
|
|
|
* different at EL1 vs. EL2, as we're multiplexing the true EL1
|
|
|
|
* event filter bit for nested.
|
|
|
|
*/
|
|
|
|
if (kvm_pmc_counts_at_el1(pmc) == kvm_pmc_counts_at_el2(pmc))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
kvm_pmu_create_perf_event(pmc);
|
|
|
|
reprogrammed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reprogrammed)
|
|
|
|
kvm_vcpu_pmu_restore_guest(vcpu);
|
|
|
|
}
|