blob: d21104e6f9ec34515bc368c157f81be8537edbe7 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Wei Huang25462f72015-06-19 15:45:05 +02002/*
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 Huang25462f72015-06-19 15:45:05 +020010 */
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 Upton03a8871a2019-11-13 16:17:20 -080018#include "nested.h"
Wei Huang25462f72015-06-19 15:45:05 +020019#include "pmu.h"
20
Like Xu27461da32020-05-29 15:43:45 +080021#define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0)
22
Wei Huang25462f72015-06-19 15:45:05 +020023static 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 Xu98dd2f12020-12-30 16:19:16 +080032 [7] = { 0x00, 0x03, PERF_COUNT_HW_REF_CPU_CYCLES },
Wei Huang25462f72015-06-19 15:45:05 +020033};
34
35/* mapping between fixed pmc index and intel_arch_events array */
36static int fixed_pmc_events[] = {1, 0, 7};
37
38static 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 Xub35e5542019-10-27 18:52:43 +080052 __set_bit(INTEL_PMC_IDX_FIXED + i, pmu->pmc_in_use);
Wei Huang25462f72015-06-19 15:45:05 +020053 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. */
60static 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
71static 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
89static unsigned intel_find_fixed_event(int idx)
90{
Marios Pomonis66061742019-12-11 12:47:53 -080091 u32 event;
92 size_t size = ARRAY_SIZE(fixed_pmc_events);
93
94 if (idx >= size)
Wei Huang25462f72015-06-19 15:45:05 +020095 return PERF_COUNT_HW_MAX;
96
Marios Pomonis66061742019-12-11 12:47:53 -080097 event = fixed_pmc_events[array_index_nospec(idx, size)];
98 return intel_arch_events[event].event_type;
Wei Huang25462f72015-06-19 15:45:05 +020099}
100
Andrea Gelminibb3541f2016-05-21 14:14:44 +0200101/* check if a PMC is enabled by comparing it with globl_ctrl bits. */
Wei Huang25462f72015-06-19 15:45:05 +0200102static 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
109static 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 Xu98ff80f2019-10-27 18:52:40 +0800122static int intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
Wei Huang25462f72015-06-19 15:45:05 +0200123{
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 Xu98ff80f2019-10-27 18:52:40 +0800133static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
134 unsigned int idx, u64 *mask)
Wei Huang25462f72015-06-19 15:45:05 +0200135{
136 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
137 bool fixed = idx & (1u << 30);
138 struct kvm_pmc *counters;
Marios Pomonis66061742019-12-11 12:47:53 -0800139 unsigned int num_counters;
Wei Huang25462f72015-06-19 15:45:05 +0200140
141 idx &= ~(3u << 30);
Marios Pomonis66061742019-12-11 12:47:53 -0800142 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 Huang25462f72015-06-19 15:45:05 +0200150 return NULL;
Paolo Bonzini0e6f4672019-05-20 17:20:40 +0200151 *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
Marios Pomonis66061742019-12-11 12:47:53 -0800152 return &counters[array_index_nospec(idx, num_counters)];
Wei Huang25462f72015-06-19 15:45:05 +0200153}
154
Paolo Bonzinia7557532021-02-02 09:32:35 -0500155static inline u64 vcpu_get_perf_capabilities(struct kvm_vcpu *vcpu)
Like Xu27461da32020-05-29 15:43:45 +0800156{
157 if (!guest_cpuid_has(vcpu, X86_FEATURE_PDCM))
Paolo Bonzinia7557532021-02-02 09:32:35 -0500158 return 0;
Like Xu27461da32020-05-29 15:43:45 +0800159
Paolo Bonzinia7557532021-02-02 09:32:35 -0500160 return vcpu->arch.perf_capabilities;
161}
162
163static inline bool fw_writes_is_enabled(struct kvm_vcpu *vcpu)
164{
165 return (vcpu_get_perf_capabilities(vcpu) & PMU_CAP_FW_WRITES) != 0;
Like Xu27461da32020-05-29 15:43:45 +0800166}
167
168static 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 Bonzini9c9520c2021-02-02 09:36:08 -0500176bool 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 Huang25462f72015-06-19 15:45:05 +0200186static 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 Xu27461da32020-05-29 15:43:45 +0800201 get_fixed_pmc(pmu, msr) || get_fw_gp_pmc(pmu, msr);
Wei Huang25462f72015-06-19 15:45:05 +0200202 break;
203 }
204
205 return ret;
206}
207
Like Xuc900c152019-10-27 18:52:41 +0800208static 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 Wangcbd71752020-05-29 15:43:44 +0800220static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Wei Huang25462f72015-06-19 15:45:05 +0200221{
222 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
223 struct kvm_pmc *pmc;
Wei Wangcbd71752020-05-29 15:43:44 +0800224 u32 msr = msr_info->index;
Wei Huang25462f72015-06-19 15:45:05 +0200225
226 switch (msr) {
227 case MSR_CORE_PERF_FIXED_CTR_CTRL:
Wei Wangcbd71752020-05-29 15:43:44 +0800228 msr_info->data = pmu->fixed_ctr_ctrl;
Wei Huang25462f72015-06-19 15:45:05 +0200229 return 0;
230 case MSR_CORE_PERF_GLOBAL_STATUS:
Wei Wangcbd71752020-05-29 15:43:44 +0800231 msr_info->data = pmu->global_status;
Wei Huang25462f72015-06-19 15:45:05 +0200232 return 0;
233 case MSR_CORE_PERF_GLOBAL_CTRL:
Wei Wangcbd71752020-05-29 15:43:44 +0800234 msr_info->data = pmu->global_ctrl;
Wei Huang25462f72015-06-19 15:45:05 +0200235 return 0;
236 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
Wei Wangcbd71752020-05-29 15:43:44 +0800237 msr_info->data = pmu->global_ovf_ctrl;
Wei Huang25462f72015-06-19 15:45:05 +0200238 return 0;
239 default:
Like Xu27461da32020-05-29 15:43:45 +0800240 if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
241 (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
Paolo Bonzini0e6f4672019-05-20 17:20:40 +0200242 u64 val = pmc_read_counter(pmc);
Wei Wangcbd71752020-05-29 15:43:44 +0800243 msr_info->data =
244 val & pmu->counter_bitmask[KVM_PMC_GP];
Paolo Bonzini0e6f4672019-05-20 17:20:40 +0200245 return 0;
246 } else if ((pmc = get_fixed_pmc(pmu, msr))) {
247 u64 val = pmc_read_counter(pmc);
Wei Wangcbd71752020-05-29 15:43:44 +0800248 msr_info->data =
249 val & pmu->counter_bitmask[KVM_PMC_FIXED];
Wei Huang25462f72015-06-19 15:45:05 +0200250 return 0;
251 } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
Wei Wangcbd71752020-05-29 15:43:44 +0800252 msr_info->data = pmc->eventsel;
Wei Huang25462f72015-06-19 15:45:05 +0200253 return 0;
254 }
255 }
256
257 return 1;
258}
259
260static 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 Upton9477f442019-11-13 16:17:15 -0800285 if (kvm_valid_perf_global_ctrl(pmu, data)) {
Wei Huang25462f72015-06-19 15:45:05 +0200286 global_ctrl_changed(pmu, data);
287 return 0;
288 }
289 break;
290 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
Luwei Kangc715eb92019-02-18 19:26:08 -0500291 if (!(data & pmu->global_ovf_ctrl_mask)) {
Wei Huang25462f72015-06-19 15:45:05 +0200292 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 Xu27461da32020-05-29 15:43:45 +0800299 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 Hankland4400cf52020-01-27 13:22:56 -0800306 data = (s64)(s32)data;
307 pmc->counter += data - pmc_read_counter(pmc);
Eric Hankland168d9182020-02-21 18:34:13 -0800308 if (pmc->perf_event)
309 perf_event_period(pmc->perf_event,
310 get_sample_period(pmc, data));
Paolo Bonzini2924b522019-05-20 17:34:30 +0200311 return 0;
312 } else if ((pmc = get_fixed_pmc(pmu, msr))) {
Eric Hankland4400cf52020-01-27 13:22:56 -0800313 pmc->counter += data - pmc_read_counter(pmc);
Eric Hankland168d9182020-02-21 18:34:13 -0800314 if (pmc->perf_event)
315 perf_event_period(pmc->perf_event,
316 get_sample_period(pmc, data));
Wei Huang25462f72015-06-19 15:45:05 +0200317 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
331static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
332{
333 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Paolo Bonzini9c9520c2021-02-02 09:36:08 -0500334 struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
335
Jim Mattsone1fba492019-09-30 16:38:54 -0700336 struct x86_pmu_capability x86_pmu;
Wei Huang25462f72015-06-19 15:45:05 +0200337 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 Mattsone1fba492019-09-30 16:38:54 -0700358 perf_get_x86_pmu_capability(&x86_pmu);
359
Wei Huang25462f72015-06-19 15:45:05 +0200360 pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
Jim Mattsone1fba492019-09-30 16:38:54 -0700361 x86_pmu.num_counters_gp);
Like Xue61ab2a2021-01-18 10:58:00 +0800362 eax.split.bit_width = min_t(int, eax.split.bit_width, x86_pmu.bit_width_gp);
Wei Huang25462f72015-06-19 15:45:05 +0200363 pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1;
Like Xue61ab2a2021-01-18 10:58:00 +0800364 eax.split.mask_length = min_t(int, eax.split.mask_length, x86_pmu.events_mask_len);
Wei Huang25462f72015-06-19 15:45:05 +0200365 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 Mattsone1fba492019-09-30 16:38:54 -0700373 x86_pmu.num_counters_fixed);
Like Xue61ab2a2021-01-18 10:58:00 +0800374 edx.split.bit_width_fixed = min_t(int,
375 edx.split.bit_width_fixed, x86_pmu.bit_width_fixed);
Wei Huang25462f72015-06-19 15:45:05 +0200376 pmu->counter_bitmask[KVM_PMC_FIXED] =
377 ((u64)1 << edx.split.bit_width_fixed) - 1;
378 }
379
Radim Krčmář34b0dad2017-05-18 19:37:31 +0200380 pmu->global_ctrl = ((1ull << pmu->nr_arch_gp_counters) - 1) |
Wei Huang25462f72015-06-19 15:45:05 +0200381 (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED);
382 pmu->global_ctrl_mask = ~pmu->global_ctrl;
Luwei Kangc715eb92019-02-18 19:26:08 -0500383 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 Christophersona1bead22020-03-02 15:57:00 -0800386 if (vmx_pt_mode_is_host_guest())
Luwei Kangc715eb92019-02-18 19:26:08 -0500387 pmu->global_ovf_ctrl_mask &=
388 ~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
Wei Huang25462f72015-06-19 15:45:05 +0200389
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 Xub35e5542019-10-27 18:52:43 +0800395
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 Upton03a8871a2019-11-13 16:17:20 -0800400
401 nested_vmx_pmu_entry_exit_ctls_update(vcpu);
Paolo Bonzini9c9520c2021-02-02 09:36:08 -0500402
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 Huang25462f72015-06-19 15:45:05 +0200407}
408
409static void intel_pmu_init(struct kvm_vcpu *vcpu)
410{
411 int i;
412 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Paolo Bonzini9c9520c2021-02-02 09:36:08 -0500413 struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
Wei Huang25462f72015-06-19 15:45:05 +0200414
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 Xua6da0d72019-10-27 18:52:42 +0800419 pmu->gp_counters[i].current_config = 0;
Wei Huang25462f72015-06-19 15:45:05 +0200420 }
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 Xua6da0d72019-10-27 18:52:42 +0800426 pmu->fixed_counters[i].current_config = 0;
Wei Huang25462f72015-06-19 15:45:05 +0200427 }
Paolo Bonzinia7557532021-02-02 09:32:35 -0500428
429 vcpu->arch.perf_capabilities = vmx_get_perf_capabilities();
Paolo Bonzini9c9520c2021-02-02 09:36:08 -0500430 lbr_desc->records.nr = 0;
Wei Huang25462f72015-06-19 15:45:05 +0200431}
432
433static void intel_pmu_reset(struct kvm_vcpu *vcpu)
434{
435 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Like Xu4d1a082d2019-07-17 10:51:18 +0800436 struct kvm_pmc *pmc = NULL;
Wei Huang25462f72015-06-19 15:45:05 +0200437 int i;
438
439 for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) {
Like Xu4d1a082d2019-07-17 10:51:18 +0800440 pmc = &pmu->gp_counters[i];
Wei Huang25462f72015-06-19 15:45:05 +0200441
442 pmc_stop_counter(pmc);
443 pmc->counter = pmc->eventsel = 0;
444 }
445
Like Xu4d1a082d2019-07-17 10:51:18 +0800446 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 Huang25462f72015-06-19 15:45:05 +0200452
453 pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status =
454 pmu->global_ovf_ctrl = 0;
455}
456
457struct 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 Xu98ff80f2019-10-27 18:52:40 +0800462 .rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,
Like Xuc900c152019-10-27 18:52:41 +0800463 .msr_idx_to_pmc = intel_msr_idx_to_pmc,
Like Xu98ff80f2019-10-27 18:52:40 +0800464 .is_valid_rdpmc_ecx = intel_is_valid_rdpmc_ecx,
Wei Huang25462f72015-06-19 15:45:05 +0200465 .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};