Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 2 | /* |
| 3 | * KVM PMU support for Intel CPUs |
| 4 | * |
| 5 | * Copyright 2011 Red Hat, Inc. and/or its affiliates. |
| 6 | * |
| 7 | * Authors: |
| 8 | * Avi Kivity <avi@redhat.com> |
| 9 | * Gleb Natapov <gleb@redhat.com> |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 10 | */ |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/kvm_host.h> |
| 13 | #include <linux/perf_event.h> |
| 14 | #include <asm/perf_event.h> |
| 15 | #include "x86.h" |
| 16 | #include "cpuid.h" |
| 17 | #include "lapic.h" |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 18 | #include "nested.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 19 | #include "pmu.h" |
| 20 | |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 21 | #define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0) |
| 22 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 23 | static struct kvm_event_hw_type_mapping intel_arch_events[] = { |
| 24 | /* Index must match CPUID 0x0A.EBX bit vector */ |
| 25 | [0] = { 0x3c, 0x00, PERF_COUNT_HW_CPU_CYCLES }, |
| 26 | [1] = { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS }, |
| 27 | [2] = { 0x3c, 0x01, PERF_COUNT_HW_BUS_CYCLES }, |
| 28 | [3] = { 0x2e, 0x4f, PERF_COUNT_HW_CACHE_REFERENCES }, |
| 29 | [4] = { 0x2e, 0x41, PERF_COUNT_HW_CACHE_MISSES }, |
| 30 | [5] = { 0xc4, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, |
| 31 | [6] = { 0xc5, 0x00, PERF_COUNT_HW_BRANCH_MISSES }, |
Like Xu | 98dd2f1 | 2020-12-30 16:19:16 +0800 | [diff] [blame] | 32 | [7] = { 0x00, 0x03, PERF_COUNT_HW_REF_CPU_CYCLES }, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /* mapping between fixed pmc index and intel_arch_events array */ |
| 36 | static int fixed_pmc_events[] = {1, 0, 7}; |
| 37 | |
| 38 | static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) |
| 39 | { |
| 40 | int i; |
| 41 | |
| 42 | for (i = 0; i < pmu->nr_arch_fixed_counters; i++) { |
| 43 | u8 new_ctrl = fixed_ctrl_field(data, i); |
| 44 | u8 old_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i); |
| 45 | struct kvm_pmc *pmc; |
| 46 | |
| 47 | pmc = get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + i); |
| 48 | |
| 49 | if (old_ctrl == new_ctrl) |
| 50 | continue; |
| 51 | |
Like Xu | b35e554 | 2019-10-27 18:52:43 +0800 | [diff] [blame] | 52 | __set_bit(INTEL_PMC_IDX_FIXED + i, pmu->pmc_in_use); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 53 | reprogram_fixed_counter(pmc, new_ctrl, i); |
| 54 | } |
| 55 | |
| 56 | pmu->fixed_ctr_ctrl = data; |
| 57 | } |
| 58 | |
| 59 | /* function is called when global control register has been updated. */ |
| 60 | static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data) |
| 61 | { |
| 62 | int bit; |
| 63 | u64 diff = pmu->global_ctrl ^ data; |
| 64 | |
| 65 | pmu->global_ctrl = data; |
| 66 | |
| 67 | for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX) |
| 68 | reprogram_counter(pmu, bit); |
| 69 | } |
| 70 | |
| 71 | static unsigned intel_find_arch_event(struct kvm_pmu *pmu, |
| 72 | u8 event_select, |
| 73 | u8 unit_mask) |
| 74 | { |
| 75 | int i; |
| 76 | |
| 77 | for (i = 0; i < ARRAY_SIZE(intel_arch_events); i++) |
| 78 | if (intel_arch_events[i].eventsel == event_select |
| 79 | && intel_arch_events[i].unit_mask == unit_mask |
| 80 | && (pmu->available_event_types & (1 << i))) |
| 81 | break; |
| 82 | |
| 83 | if (i == ARRAY_SIZE(intel_arch_events)) |
| 84 | return PERF_COUNT_HW_MAX; |
| 85 | |
| 86 | return intel_arch_events[i].event_type; |
| 87 | } |
| 88 | |
| 89 | static unsigned intel_find_fixed_event(int idx) |
| 90 | { |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 91 | u32 event; |
| 92 | size_t size = ARRAY_SIZE(fixed_pmc_events); |
| 93 | |
| 94 | if (idx >= size) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 95 | return PERF_COUNT_HW_MAX; |
| 96 | |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 97 | event = fixed_pmc_events[array_index_nospec(idx, size)]; |
| 98 | return intel_arch_events[event].event_type; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 101 | /* check if a PMC is enabled by comparing it with globl_ctrl bits. */ |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 102 | static bool intel_pmc_is_enabled(struct kvm_pmc *pmc) |
| 103 | { |
| 104 | struct kvm_pmu *pmu = pmc_to_pmu(pmc); |
| 105 | |
| 106 | return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl); |
| 107 | } |
| 108 | |
| 109 | static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx) |
| 110 | { |
| 111 | if (pmc_idx < INTEL_PMC_IDX_FIXED) |
| 112 | return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + pmc_idx, |
| 113 | MSR_P6_EVNTSEL0); |
| 114 | else { |
| 115 | u32 idx = pmc_idx - INTEL_PMC_IDX_FIXED; |
| 116 | |
| 117 | return get_fixed_pmc(pmu, idx + MSR_CORE_PERF_FIXED_CTR0); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* returns 0 if idx's corresponding MSR exists; otherwise returns 1. */ |
Like Xu | 98ff80f | 2019-10-27 18:52:40 +0800 | [diff] [blame] | 122 | static int intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 123 | { |
| 124 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 125 | bool fixed = idx & (1u << 30); |
| 126 | |
| 127 | idx &= ~(3u << 30); |
| 128 | |
| 129 | return (!fixed && idx >= pmu->nr_arch_gp_counters) || |
| 130 | (fixed && idx >= pmu->nr_arch_fixed_counters); |
| 131 | } |
| 132 | |
Like Xu | 98ff80f | 2019-10-27 18:52:40 +0800 | [diff] [blame] | 133 | static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, |
| 134 | unsigned int idx, u64 *mask) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 135 | { |
| 136 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 137 | bool fixed = idx & (1u << 30); |
| 138 | struct kvm_pmc *counters; |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 139 | unsigned int num_counters; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 140 | |
| 141 | idx &= ~(3u << 30); |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 142 | if (fixed) { |
| 143 | counters = pmu->fixed_counters; |
| 144 | num_counters = pmu->nr_arch_fixed_counters; |
| 145 | } else { |
| 146 | counters = pmu->gp_counters; |
| 147 | num_counters = pmu->nr_arch_gp_counters; |
| 148 | } |
| 149 | if (idx >= num_counters) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 150 | return NULL; |
Paolo Bonzini | 0e6f467 | 2019-05-20 17:20:40 +0200 | [diff] [blame] | 151 | *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP]; |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 152 | return &counters[array_index_nospec(idx, num_counters)]; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Paolo Bonzini | a755753 | 2021-02-02 09:32:35 -0500 | [diff] [blame] | 155 | static inline u64 vcpu_get_perf_capabilities(struct kvm_vcpu *vcpu) |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 156 | { |
| 157 | if (!guest_cpuid_has(vcpu, X86_FEATURE_PDCM)) |
Paolo Bonzini | a755753 | 2021-02-02 09:32:35 -0500 | [diff] [blame] | 158 | return 0; |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 159 | |
Paolo Bonzini | a755753 | 2021-02-02 09:32:35 -0500 | [diff] [blame] | 160 | return vcpu->arch.perf_capabilities; |
| 161 | } |
| 162 | |
| 163 | static inline bool fw_writes_is_enabled(struct kvm_vcpu *vcpu) |
| 164 | { |
| 165 | return (vcpu_get_perf_capabilities(vcpu) & PMU_CAP_FW_WRITES) != 0; |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr) |
| 169 | { |
| 170 | if (!fw_writes_is_enabled(pmu_to_vcpu(pmu))) |
| 171 | return NULL; |
| 172 | |
| 173 | return get_gp_pmc(pmu, msr, MSR_IA32_PMC0); |
| 174 | } |
| 175 | |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame^] | 176 | bool intel_pmu_lbr_is_compatible(struct kvm_vcpu *vcpu) |
| 177 | { |
| 178 | /* |
| 179 | * As a first step, a guest could only enable LBR feature if its |
| 180 | * cpu model is the same as the host because the LBR registers |
| 181 | * would be pass-through to the guest and they're model specific. |
| 182 | */ |
| 183 | return boot_cpu_data.x86_model == guest_cpuid_model(vcpu); |
| 184 | } |
| 185 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 186 | static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) |
| 187 | { |
| 188 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 189 | int ret; |
| 190 | |
| 191 | switch (msr) { |
| 192 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 193 | case MSR_CORE_PERF_GLOBAL_STATUS: |
| 194 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 195 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
| 196 | ret = pmu->version > 1; |
| 197 | break; |
| 198 | default: |
| 199 | ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) || |
| 200 | get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) || |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 201 | get_fixed_pmc(pmu, msr) || get_fw_gp_pmc(pmu, msr); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
Like Xu | c900c15 | 2019-10-27 18:52:41 +0800 | [diff] [blame] | 208 | static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr) |
| 209 | { |
| 210 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 211 | struct kvm_pmc *pmc; |
| 212 | |
| 213 | pmc = get_fixed_pmc(pmu, msr); |
| 214 | pmc = pmc ? pmc : get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0); |
| 215 | pmc = pmc ? pmc : get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0); |
| 216 | |
| 217 | return pmc; |
| 218 | } |
| 219 | |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 220 | static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 221 | { |
| 222 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 223 | struct kvm_pmc *pmc; |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 224 | u32 msr = msr_info->index; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 225 | |
| 226 | switch (msr) { |
| 227 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 228 | msr_info->data = pmu->fixed_ctr_ctrl; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 229 | return 0; |
| 230 | case MSR_CORE_PERF_GLOBAL_STATUS: |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 231 | msr_info->data = pmu->global_status; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 232 | return 0; |
| 233 | case MSR_CORE_PERF_GLOBAL_CTRL: |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 234 | msr_info->data = pmu->global_ctrl; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 235 | return 0; |
| 236 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 237 | msr_info->data = pmu->global_ovf_ctrl; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 238 | return 0; |
| 239 | default: |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 240 | if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || |
| 241 | (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) { |
Paolo Bonzini | 0e6f467 | 2019-05-20 17:20:40 +0200 | [diff] [blame] | 242 | u64 val = pmc_read_counter(pmc); |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 243 | msr_info->data = |
| 244 | val & pmu->counter_bitmask[KVM_PMC_GP]; |
Paolo Bonzini | 0e6f467 | 2019-05-20 17:20:40 +0200 | [diff] [blame] | 245 | return 0; |
| 246 | } else if ((pmc = get_fixed_pmc(pmu, msr))) { |
| 247 | u64 val = pmc_read_counter(pmc); |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 248 | msr_info->data = |
| 249 | val & pmu->counter_bitmask[KVM_PMC_FIXED]; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 250 | return 0; |
| 251 | } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 252 | msr_info->data = pmc->eventsel; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return 1; |
| 258 | } |
| 259 | |
| 260 | static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
| 261 | { |
| 262 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 263 | struct kvm_pmc *pmc; |
| 264 | u32 msr = msr_info->index; |
| 265 | u64 data = msr_info->data; |
| 266 | |
| 267 | switch (msr) { |
| 268 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 269 | if (pmu->fixed_ctr_ctrl == data) |
| 270 | return 0; |
| 271 | if (!(data & 0xfffffffffffff444ull)) { |
| 272 | reprogram_fixed_counters(pmu, data); |
| 273 | return 0; |
| 274 | } |
| 275 | break; |
| 276 | case MSR_CORE_PERF_GLOBAL_STATUS: |
| 277 | if (msr_info->host_initiated) { |
| 278 | pmu->global_status = data; |
| 279 | return 0; |
| 280 | } |
| 281 | break; /* RO MSR */ |
| 282 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 283 | if (pmu->global_ctrl == data) |
| 284 | return 0; |
Oliver Upton | 9477f44 | 2019-11-13 16:17:15 -0800 | [diff] [blame] | 285 | if (kvm_valid_perf_global_ctrl(pmu, data)) { |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 286 | global_ctrl_changed(pmu, data); |
| 287 | return 0; |
| 288 | } |
| 289 | break; |
| 290 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
Luwei Kang | c715eb9 | 2019-02-18 19:26:08 -0500 | [diff] [blame] | 291 | if (!(data & pmu->global_ovf_ctrl_mask)) { |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 292 | if (!msr_info->host_initiated) |
| 293 | pmu->global_status &= ~data; |
| 294 | pmu->global_ovf_ctrl = data; |
| 295 | return 0; |
| 296 | } |
| 297 | break; |
| 298 | default: |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 299 | if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || |
| 300 | (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) { |
| 301 | if ((msr & MSR_PMC_FULL_WIDTH_BIT) && |
| 302 | (data & ~pmu->counter_bitmask[KVM_PMC_GP])) |
| 303 | return 1; |
| 304 | if (!msr_info->host_initiated && |
| 305 | !(msr & MSR_PMC_FULL_WIDTH_BIT)) |
Eric Hankland | 4400cf5 | 2020-01-27 13:22:56 -0800 | [diff] [blame] | 306 | data = (s64)(s32)data; |
| 307 | pmc->counter += data - pmc_read_counter(pmc); |
Eric Hankland | 168d918 | 2020-02-21 18:34:13 -0800 | [diff] [blame] | 308 | if (pmc->perf_event) |
| 309 | perf_event_period(pmc->perf_event, |
| 310 | get_sample_period(pmc, data)); |
Paolo Bonzini | 2924b52 | 2019-05-20 17:34:30 +0200 | [diff] [blame] | 311 | return 0; |
| 312 | } else if ((pmc = get_fixed_pmc(pmu, msr))) { |
Eric Hankland | 4400cf5 | 2020-01-27 13:22:56 -0800 | [diff] [blame] | 313 | pmc->counter += data - pmc_read_counter(pmc); |
Eric Hankland | 168d918 | 2020-02-21 18:34:13 -0800 | [diff] [blame] | 314 | if (pmc->perf_event) |
| 315 | perf_event_period(pmc->perf_event, |
| 316 | get_sample_period(pmc, data)); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 317 | return 0; |
| 318 | } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { |
| 319 | if (data == pmc->eventsel) |
| 320 | return 0; |
| 321 | if (!(data & pmu->reserved_bits)) { |
| 322 | reprogram_gp_counter(pmc, data); |
| 323 | return 0; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return 1; |
| 329 | } |
| 330 | |
| 331 | static void intel_pmu_refresh(struct kvm_vcpu *vcpu) |
| 332 | { |
| 333 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame^] | 334 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
| 335 | |
Jim Mattson | e1fba49 | 2019-09-30 16:38:54 -0700 | [diff] [blame] | 336 | struct x86_pmu_capability x86_pmu; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 337 | struct kvm_cpuid_entry2 *entry; |
| 338 | union cpuid10_eax eax; |
| 339 | union cpuid10_edx edx; |
| 340 | |
| 341 | pmu->nr_arch_gp_counters = 0; |
| 342 | pmu->nr_arch_fixed_counters = 0; |
| 343 | pmu->counter_bitmask[KVM_PMC_GP] = 0; |
| 344 | pmu->counter_bitmask[KVM_PMC_FIXED] = 0; |
| 345 | pmu->version = 0; |
| 346 | pmu->reserved_bits = 0xffffffff00200000ull; |
| 347 | |
| 348 | entry = kvm_find_cpuid_entry(vcpu, 0xa, 0); |
| 349 | if (!entry) |
| 350 | return; |
| 351 | eax.full = entry->eax; |
| 352 | edx.full = entry->edx; |
| 353 | |
| 354 | pmu->version = eax.split.version_id; |
| 355 | if (!pmu->version) |
| 356 | return; |
| 357 | |
Jim Mattson | e1fba49 | 2019-09-30 16:38:54 -0700 | [diff] [blame] | 358 | perf_get_x86_pmu_capability(&x86_pmu); |
| 359 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 360 | pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters, |
Jim Mattson | e1fba49 | 2019-09-30 16:38:54 -0700 | [diff] [blame] | 361 | x86_pmu.num_counters_gp); |
Like Xu | e61ab2a | 2021-01-18 10:58:00 +0800 | [diff] [blame] | 362 | eax.split.bit_width = min_t(int, eax.split.bit_width, x86_pmu.bit_width_gp); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 363 | pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1; |
Like Xu | e61ab2a | 2021-01-18 10:58:00 +0800 | [diff] [blame] | 364 | eax.split.mask_length = min_t(int, eax.split.mask_length, x86_pmu.events_mask_len); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 365 | pmu->available_event_types = ~entry->ebx & |
| 366 | ((1ull << eax.split.mask_length) - 1); |
| 367 | |
| 368 | if (pmu->version == 1) { |
| 369 | pmu->nr_arch_fixed_counters = 0; |
| 370 | } else { |
| 371 | pmu->nr_arch_fixed_counters = |
| 372 | min_t(int, edx.split.num_counters_fixed, |
Jim Mattson | e1fba49 | 2019-09-30 16:38:54 -0700 | [diff] [blame] | 373 | x86_pmu.num_counters_fixed); |
Like Xu | e61ab2a | 2021-01-18 10:58:00 +0800 | [diff] [blame] | 374 | edx.split.bit_width_fixed = min_t(int, |
| 375 | edx.split.bit_width_fixed, x86_pmu.bit_width_fixed); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 376 | pmu->counter_bitmask[KVM_PMC_FIXED] = |
| 377 | ((u64)1 << edx.split.bit_width_fixed) - 1; |
| 378 | } |
| 379 | |
Radim Krčmář | 34b0dad | 2017-05-18 19:37:31 +0200 | [diff] [blame] | 380 | pmu->global_ctrl = ((1ull << pmu->nr_arch_gp_counters) - 1) | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 381 | (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED); |
| 382 | pmu->global_ctrl_mask = ~pmu->global_ctrl; |
Luwei Kang | c715eb9 | 2019-02-18 19:26:08 -0500 | [diff] [blame] | 383 | pmu->global_ovf_ctrl_mask = pmu->global_ctrl_mask |
| 384 | & ~(MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF | |
| 385 | MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD); |
Sean Christopherson | a1bead2 | 2020-03-02 15:57:00 -0800 | [diff] [blame] | 386 | if (vmx_pt_mode_is_host_guest()) |
Luwei Kang | c715eb9 | 2019-02-18 19:26:08 -0500 | [diff] [blame] | 387 | pmu->global_ovf_ctrl_mask &= |
| 388 | ~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 389 | |
| 390 | entry = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 391 | if (entry && |
| 392 | (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) && |
| 393 | (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) |
| 394 | pmu->reserved_bits ^= HSW_IN_TX|HSW_IN_TX_CHECKPOINTED; |
Like Xu | b35e554 | 2019-10-27 18:52:43 +0800 | [diff] [blame] | 395 | |
| 396 | bitmap_set(pmu->all_valid_pmc_idx, |
| 397 | 0, pmu->nr_arch_gp_counters); |
| 398 | bitmap_set(pmu->all_valid_pmc_idx, |
| 399 | INTEL_PMC_MAX_GENERIC, pmu->nr_arch_fixed_counters); |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 400 | |
| 401 | nested_vmx_pmu_entry_exit_ctls_update(vcpu); |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame^] | 402 | |
| 403 | if (intel_pmu_lbr_is_compatible(vcpu)) |
| 404 | x86_perf_get_lbr(&lbr_desc->records); |
| 405 | else |
| 406 | lbr_desc->records.nr = 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static void intel_pmu_init(struct kvm_vcpu *vcpu) |
| 410 | { |
| 411 | int i; |
| 412 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame^] | 413 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 414 | |
| 415 | for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { |
| 416 | pmu->gp_counters[i].type = KVM_PMC_GP; |
| 417 | pmu->gp_counters[i].vcpu = vcpu; |
| 418 | pmu->gp_counters[i].idx = i; |
Like Xu | a6da0d7 | 2019-10-27 18:52:42 +0800 | [diff] [blame] | 419 | pmu->gp_counters[i].current_config = 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) { |
| 423 | pmu->fixed_counters[i].type = KVM_PMC_FIXED; |
| 424 | pmu->fixed_counters[i].vcpu = vcpu; |
| 425 | pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED; |
Like Xu | a6da0d7 | 2019-10-27 18:52:42 +0800 | [diff] [blame] | 426 | pmu->fixed_counters[i].current_config = 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 427 | } |
Paolo Bonzini | a755753 | 2021-02-02 09:32:35 -0500 | [diff] [blame] | 428 | |
| 429 | vcpu->arch.perf_capabilities = vmx_get_perf_capabilities(); |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame^] | 430 | lbr_desc->records.nr = 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static void intel_pmu_reset(struct kvm_vcpu *vcpu) |
| 434 | { |
| 435 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Like Xu | 4d1a082d | 2019-07-17 10:51:18 +0800 | [diff] [blame] | 436 | struct kvm_pmc *pmc = NULL; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 437 | int i; |
| 438 | |
| 439 | for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { |
Like Xu | 4d1a082d | 2019-07-17 10:51:18 +0800 | [diff] [blame] | 440 | pmc = &pmu->gp_counters[i]; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 441 | |
| 442 | pmc_stop_counter(pmc); |
| 443 | pmc->counter = pmc->eventsel = 0; |
| 444 | } |
| 445 | |
Like Xu | 4d1a082d | 2019-07-17 10:51:18 +0800 | [diff] [blame] | 446 | for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) { |
| 447 | pmc = &pmu->fixed_counters[i]; |
| 448 | |
| 449 | pmc_stop_counter(pmc); |
| 450 | pmc->counter = 0; |
| 451 | } |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 452 | |
| 453 | pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = |
| 454 | pmu->global_ovf_ctrl = 0; |
| 455 | } |
| 456 | |
| 457 | struct kvm_pmu_ops intel_pmu_ops = { |
| 458 | .find_arch_event = intel_find_arch_event, |
| 459 | .find_fixed_event = intel_find_fixed_event, |
| 460 | .pmc_is_enabled = intel_pmc_is_enabled, |
| 461 | .pmc_idx_to_pmc = intel_pmc_idx_to_pmc, |
Like Xu | 98ff80f | 2019-10-27 18:52:40 +0800 | [diff] [blame] | 462 | .rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc, |
Like Xu | c900c15 | 2019-10-27 18:52:41 +0800 | [diff] [blame] | 463 | .msr_idx_to_pmc = intel_msr_idx_to_pmc, |
Like Xu | 98ff80f | 2019-10-27 18:52:40 +0800 | [diff] [blame] | 464 | .is_valid_rdpmc_ecx = intel_is_valid_rdpmc_ecx, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 465 | .is_valid_msr = intel_is_valid_msr, |
| 466 | .get_msr = intel_pmu_get_msr, |
| 467 | .set_msr = intel_pmu_set_msr, |
| 468 | .refresh = intel_pmu_refresh, |
| 469 | .init = intel_pmu_init, |
| 470 | .reset = intel_pmu_reset, |
| 471 | }; |