xref: /kvm-unit-tests/x86/pmu.c (revision 95a9408860fc8dacb73e9b302fb96536f91d5ccf)
1a9f8b16fSGleb Natapov 
2a9f8b16fSGleb Natapov #include "x86/msr.h"
3a9f8b16fSGleb Natapov #include "x86/processor.h"
49f17508dSLike Xu #include "x86/pmu.h"
5a9f8b16fSGleb Natapov #include "x86/apic-defs.h"
6a9f8b16fSGleb Natapov #include "x86/apic.h"
7a9f8b16fSGleb Natapov #include "x86/desc.h"
8a9f8b16fSGleb Natapov #include "x86/isr.h"
9*95a94088SNicholas Piggin #include "vmalloc.h"
10dcda215bSPaolo Bonzini #include "alloc.h"
11a9f8b16fSGleb Natapov 
12a9f8b16fSGleb Natapov #include "libcflat.h"
13a9f8b16fSGleb Natapov #include <stdint.h>
14a9f8b16fSGleb Natapov 
15a9f8b16fSGleb Natapov #define N 1000000
16a9f8b16fSGleb Natapov 
1720cf9147SJim Mattson // These values match the number of instructions and branches in the
1820cf9147SJim Mattson // assembly block in check_emulated_instr().
1920cf9147SJim Mattson #define EXPECTED_INSTR 17
2020cf9147SJim Mattson #define EXPECTED_BRNCH 5
2120cf9147SJim Mattson 
22a9f8b16fSGleb Natapov typedef struct {
23a9f8b16fSGleb Natapov 	uint32_t ctr;
24006b089dSLike Xu 	uint64_t config;
25a9f8b16fSGleb Natapov 	uint64_t count;
26a9f8b16fSGleb Natapov 	int idx;
27a9f8b16fSGleb Natapov } pmu_counter_t;
28a9f8b16fSGleb Natapov 
29a9f8b16fSGleb Natapov struct pmu_event {
30797d79a2SThomas Huth 	const char *name;
31a9f8b16fSGleb Natapov 	uint32_t unit_sel;
32a9f8b16fSGleb Natapov 	int min;
33a9f8b16fSGleb Natapov 	int max;
347c648ce2SLike Xu } intel_gp_events[] = {
35a9f8b16fSGleb Natapov 	{"core cycles", 0x003c, 1*N, 50*N},
36a9f8b16fSGleb Natapov 	{"instructions", 0x00c0, 10*N, 10.2*N},
37290f4213SJim Mattson 	{"ref cycles", 0x013c, 1*N, 30*N},
38290f4213SJim Mattson 	{"llc references", 0x4f2e, 1, 2*N},
39a9f8b16fSGleb Natapov 	{"llc misses", 0x412e, 1, 1*N},
40a9f8b16fSGleb Natapov 	{"branches", 0x00c4, 1*N, 1.1*N},
41a9f8b16fSGleb Natapov 	{"branch misses", 0x00c5, 0, 0.1*N},
42b883751aSLike Xu }, amd_gp_events[] = {
43b883751aSLike Xu 	{"core cycles", 0x0076, 1*N, 50*N},
44b883751aSLike Xu 	{"instructions", 0x00c0, 10*N, 10.2*N},
45b883751aSLike Xu 	{"branches", 0x00c2, 1*N, 1.1*N},
46b883751aSLike Xu 	{"branch misses", 0x00c3, 0, 0.1*N},
47a9f8b16fSGleb Natapov }, fixed_events[] = {
48a9f8b16fSGleb Natapov 	{"fixed 1", MSR_CORE_PERF_FIXED_CTR0, 10*N, 10.2*N},
49a9f8b16fSGleb Natapov 	{"fixed 2", MSR_CORE_PERF_FIXED_CTR0 + 1, 1*N, 30*N},
500ef1f6a8SPaolo Bonzini 	{"fixed 3", MSR_CORE_PERF_FIXED_CTR0 + 2, 0.1*N, 30*N}
51a9f8b16fSGleb Natapov };
52a9f8b16fSGleb Natapov 
53a9f8b16fSGleb Natapov char *buf;
54a9f8b16fSGleb Natapov 
557c648ce2SLike Xu static struct pmu_event *gp_events;
567c648ce2SLike Xu static unsigned int gp_events_size;
577c648ce2SLike Xu 
587db17e21SThomas Huth static inline void loop(void)
59a9f8b16fSGleb Natapov {
60a9f8b16fSGleb Natapov 	unsigned long tmp, tmp2, tmp3;
61a9f8b16fSGleb Natapov 
62a9f8b16fSGleb Natapov 	asm volatile("1: mov (%1), %2; add $64, %1; nop; nop; nop; nop; nop; nop; nop; loop 1b"
63a9f8b16fSGleb Natapov 			: "=c"(tmp), "=r"(tmp2), "=r"(tmp3): "0"(N), "1"(buf));
64a9f8b16fSGleb Natapov 
65a9f8b16fSGleb Natapov }
66a9f8b16fSGleb Natapov 
67a9f8b16fSGleb Natapov volatile uint64_t irq_received;
68a9f8b16fSGleb Natapov 
69a9f8b16fSGleb Natapov static void cnt_overflow(isr_regs_t *regs)
70a9f8b16fSGleb Natapov {
71a9f8b16fSGleb Natapov 	irq_received++;
72a9f8b16fSGleb Natapov 	apic_write(APIC_EOI, 0);
73a9f8b16fSGleb Natapov }
74a9f8b16fSGleb Natapov 
75a9f8b16fSGleb Natapov static bool check_irq(void)
76a9f8b16fSGleb Natapov {
77a9f8b16fSGleb Natapov 	int i;
78a9f8b16fSGleb Natapov 	irq_received = 0;
79787f0aebSMaxim Levitsky 	sti();
80a9f8b16fSGleb Natapov 	for (i = 0; i < 100000 && !irq_received; i++)
81a9f8b16fSGleb Natapov 		asm volatile("pause");
82787f0aebSMaxim Levitsky 	cli();
83a9f8b16fSGleb Natapov 	return irq_received;
84a9f8b16fSGleb Natapov }
85a9f8b16fSGleb Natapov 
86a9f8b16fSGleb Natapov static bool is_gp(pmu_counter_t *evt)
87a9f8b16fSGleb Natapov {
88b883751aSLike Xu 	if (!pmu.is_intel)
89b883751aSLike Xu 		return true;
90b883751aSLike Xu 
9122f2901aSLike Xu 	return evt->ctr < MSR_CORE_PERF_FIXED_CTR0 ||
9222f2901aSLike Xu 		evt->ctr >= MSR_IA32_PMC0;
93a9f8b16fSGleb Natapov }
94a9f8b16fSGleb Natapov 
95a9f8b16fSGleb Natapov static int event_to_global_idx(pmu_counter_t *cnt)
96a9f8b16fSGleb Natapov {
97b883751aSLike Xu 	if (pmu.is_intel)
98cda64e80SLike Xu 		return cnt->ctr - (is_gp(cnt) ? pmu.msr_gp_counter_base :
99a9f8b16fSGleb Natapov 			(MSR_CORE_PERF_FIXED_CTR0 - FIXED_CNT_INDEX));
100b883751aSLike Xu 
101b883751aSLike Xu 	if (pmu.msr_gp_counter_base == MSR_F15H_PERF_CTR0)
102b883751aSLike Xu 		return (cnt->ctr - pmu.msr_gp_counter_base) / 2;
103b883751aSLike Xu 	else
104b883751aSLike Xu 		return cnt->ctr - pmu.msr_gp_counter_base;
105a9f8b16fSGleb Natapov }
106a9f8b16fSGleb Natapov 
107a9f8b16fSGleb Natapov static struct pmu_event* get_counter_event(pmu_counter_t *cnt)
108a9f8b16fSGleb Natapov {
109a9f8b16fSGleb Natapov 	if (is_gp(cnt)) {
110a9f8b16fSGleb Natapov 		int i;
111a9f8b16fSGleb Natapov 
1127c648ce2SLike Xu 		for (i = 0; i < gp_events_size; i++)
113a9f8b16fSGleb Natapov 			if (gp_events[i].unit_sel == (cnt->config & 0xffff))
114a9f8b16fSGleb Natapov 				return &gp_events[i];
115a9f8b16fSGleb Natapov 	} else
116a9f8b16fSGleb Natapov 		return &fixed_events[cnt->ctr - MSR_CORE_PERF_FIXED_CTR0];
117a9f8b16fSGleb Natapov 
118a9f8b16fSGleb Natapov 	return (void*)0;
119a9f8b16fSGleb Natapov }
120a9f8b16fSGleb Natapov 
121a9f8b16fSGleb Natapov static void global_enable(pmu_counter_t *cnt)
122a9f8b16fSGleb Natapov {
12362ba5036SLike Xu 	if (!this_cpu_has_perf_global_ctrl())
12462ba5036SLike Xu 		return;
12562ba5036SLike Xu 
126a9f8b16fSGleb Natapov 	cnt->idx = event_to_global_idx(cnt);
1278a2866d1SLike Xu 	wrmsr(pmu.msr_global_ctl, rdmsr(pmu.msr_global_ctl) | BIT_ULL(cnt->idx));
128a9f8b16fSGleb Natapov }
129a9f8b16fSGleb Natapov 
130a9f8b16fSGleb Natapov static void global_disable(pmu_counter_t *cnt)
131a9f8b16fSGleb Natapov {
13262ba5036SLike Xu 	if (!this_cpu_has_perf_global_ctrl())
13362ba5036SLike Xu 		return;
13462ba5036SLike Xu 
1358a2866d1SLike Xu 	wrmsr(pmu.msr_global_ctl, rdmsr(pmu.msr_global_ctl) & ~BIT_ULL(cnt->idx));
136a9f8b16fSGleb Natapov }
137a9f8b16fSGleb Natapov 
138e9e7577bSLike Xu static void __start_event(pmu_counter_t *evt, uint64_t count)
139a9f8b16fSGleb Natapov {
140e9e7577bSLike Xu     evt->count = count;
141a9f8b16fSGleb Natapov     wrmsr(evt->ctr, evt->count);
142cda64e80SLike Xu     if (is_gp(evt)) {
143cda64e80SLike Xu 	    wrmsr(MSR_GP_EVENT_SELECTx(event_to_global_idx(evt)),
144a9f8b16fSGleb Natapov 		  evt->config | EVNTSEL_EN);
145cda64e80SLike Xu     } else {
146a9f8b16fSGleb Natapov 	    uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
147a9f8b16fSGleb Natapov 	    int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
148a9f8b16fSGleb Natapov 	    uint32_t usrospmi = 0;
149a9f8b16fSGleb Natapov 
150a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_OS)
151a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 0);
152a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_USR)
153a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 1);
154a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_INT)
155a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 3); // PMI on overflow
156a9f8b16fSGleb Natapov 	    ctrl = (ctrl & ~(0xf << shift)) | (usrospmi << shift);
157a9f8b16fSGleb Natapov 	    wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl);
158a9f8b16fSGleb Natapov     }
159a9f8b16fSGleb Natapov     global_enable(evt);
1605a2cb3e6SLike Xu     apic_write(APIC_LVTPC, PMI_VECTOR);
161a9f8b16fSGleb Natapov }
162a9f8b16fSGleb Natapov 
163e9e7577bSLike Xu static void start_event(pmu_counter_t *evt)
164e9e7577bSLike Xu {
165e9e7577bSLike Xu 	__start_event(evt, 0);
166e9e7577bSLike Xu }
167e9e7577bSLike Xu 
168a9f8b16fSGleb Natapov static void stop_event(pmu_counter_t *evt)
169a9f8b16fSGleb Natapov {
170a9f8b16fSGleb Natapov 	global_disable(evt);
171cda64e80SLike Xu 	if (is_gp(evt)) {
172cda64e80SLike Xu 		wrmsr(MSR_GP_EVENT_SELECTx(event_to_global_idx(evt)),
173a9f8b16fSGleb Natapov 		      evt->config & ~EVNTSEL_EN);
174cda64e80SLike Xu 	} else {
175a9f8b16fSGleb Natapov 		uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
176a9f8b16fSGleb Natapov 		int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
177a9f8b16fSGleb Natapov 		wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl & ~(0xf << shift));
178a9f8b16fSGleb Natapov 	}
179a9f8b16fSGleb Natapov 	evt->count = rdmsr(evt->ctr);
180a9f8b16fSGleb Natapov }
181a9f8b16fSGleb Natapov 
1828554261fSLike Xu static noinline void measure_many(pmu_counter_t *evt, int count)
183a9f8b16fSGleb Natapov {
184a9f8b16fSGleb Natapov 	int i;
185a9f8b16fSGleb Natapov 	for (i = 0; i < count; i++)
186a9f8b16fSGleb Natapov 		start_event(&evt[i]);
187a9f8b16fSGleb Natapov 	loop();
188a9f8b16fSGleb Natapov 	for (i = 0; i < count; i++)
189a9f8b16fSGleb Natapov 		stop_event(&evt[i]);
190a9f8b16fSGleb Natapov }
191a9f8b16fSGleb Natapov 
1928554261fSLike Xu static void measure_one(pmu_counter_t *evt)
1938554261fSLike Xu {
1948554261fSLike Xu 	measure_many(evt, 1);
1958554261fSLike Xu }
1968554261fSLike Xu 
197e9e7577bSLike Xu static noinline void __measure(pmu_counter_t *evt, uint64_t count)
198e9e7577bSLike Xu {
199e9e7577bSLike Xu 	__start_event(evt, count);
200e9e7577bSLike Xu 	loop();
201e9e7577bSLike Xu 	stop_event(evt);
202e9e7577bSLike Xu }
203e9e7577bSLike Xu 
204a9f8b16fSGleb Natapov static bool verify_event(uint64_t count, struct pmu_event *e)
205a9f8b16fSGleb Natapov {
206290f4213SJim Mattson 	// printf("%d <= %ld <= %d\n", e->min, count, e->max);
207a9f8b16fSGleb Natapov 	return count >= e->min  && count <= e->max;
208a9f8b16fSGleb Natapov 
209a9f8b16fSGleb Natapov }
210a9f8b16fSGleb Natapov 
211a9f8b16fSGleb Natapov static bool verify_counter(pmu_counter_t *cnt)
212a9f8b16fSGleb Natapov {
213a9f8b16fSGleb Natapov 	return verify_event(cnt->count, get_counter_event(cnt));
214a9f8b16fSGleb Natapov }
215a9f8b16fSGleb Natapov 
216a9f8b16fSGleb Natapov static void check_gp_counter(struct pmu_event *evt)
217a9f8b16fSGleb Natapov {
218a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
219a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel,
220a9f8b16fSGleb Natapov 	};
221a9f8b16fSGleb Natapov 	int i;
222a9f8b16fSGleb Natapov 
223cda64e80SLike Xu 	for (i = 0; i < pmu.nr_gp_counters; i++) {
224cda64e80SLike Xu 		cnt.ctr = MSR_GP_COUNTERx(i);
2258554261fSLike Xu 		measure_one(&cnt);
226a299895bSThomas Huth 		report(verify_event(cnt.count, evt), "%s-%d", evt->name, i);
227a9f8b16fSGleb Natapov 	}
228a9f8b16fSGleb Natapov }
229a9f8b16fSGleb Natapov 
230a9f8b16fSGleb Natapov static void check_gp_counters(void)
231a9f8b16fSGleb Natapov {
232a9f8b16fSGleb Natapov 	int i;
233a9f8b16fSGleb Natapov 
2347c648ce2SLike Xu 	for (i = 0; i < gp_events_size; i++)
2352719b92cSYang Weijiang 		if (pmu_gp_counter_is_available(i))
236a9f8b16fSGleb Natapov 			check_gp_counter(&gp_events[i]);
237a9f8b16fSGleb Natapov 		else
238a9f8b16fSGleb Natapov 			printf("GP event '%s' is disabled\n",
239a9f8b16fSGleb Natapov 					gp_events[i].name);
240a9f8b16fSGleb Natapov }
241a9f8b16fSGleb Natapov 
242a9f8b16fSGleb Natapov static void check_fixed_counters(void)
243a9f8b16fSGleb Natapov {
244a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
245a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR,
246a9f8b16fSGleb Natapov 	};
247a9f8b16fSGleb Natapov 	int i;
248a9f8b16fSGleb Natapov 
249414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
250a9f8b16fSGleb Natapov 		cnt.ctr = fixed_events[i].unit_sel;
2518554261fSLike Xu 		measure_one(&cnt);
2522719b92cSYang Weijiang 		report(verify_event(cnt.count, &fixed_events[i]), "fixed-%d", i);
253a9f8b16fSGleb Natapov 	}
254a9f8b16fSGleb Natapov }
255a9f8b16fSGleb Natapov 
256a9f8b16fSGleb Natapov static void check_counters_many(void)
257a9f8b16fSGleb Natapov {
258a9f8b16fSGleb Natapov 	pmu_counter_t cnt[10];
259a9f8b16fSGleb Natapov 	int i, n;
260a9f8b16fSGleb Natapov 
261414ee7d1SSean Christopherson 	for (i = 0, n = 0; n < pmu.nr_gp_counters; i++) {
2622719b92cSYang Weijiang 		if (!pmu_gp_counter_is_available(i))
263a9f8b16fSGleb Natapov 			continue;
264a9f8b16fSGleb Natapov 
265cda64e80SLike Xu 		cnt[n].ctr = MSR_GP_COUNTERx(n);
2664ac45293SWei Huang 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
2677c648ce2SLike Xu 			gp_events[i % gp_events_size].unit_sel;
268a9f8b16fSGleb Natapov 		n++;
269a9f8b16fSGleb Natapov 	}
270414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
271a9f8b16fSGleb Natapov 		cnt[n].ctr = fixed_events[i].unit_sel;
272a9f8b16fSGleb Natapov 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR;
273a9f8b16fSGleb Natapov 		n++;
274a9f8b16fSGleb Natapov 	}
275a9f8b16fSGleb Natapov 
2768554261fSLike Xu 	measure_many(cnt, n);
277a9f8b16fSGleb Natapov 
278a9f8b16fSGleb Natapov 	for (i = 0; i < n; i++)
279a9f8b16fSGleb Natapov 		if (!verify_counter(&cnt[i]))
280a9f8b16fSGleb Natapov 			break;
281a9f8b16fSGleb Natapov 
282a299895bSThomas Huth 	report(i == n, "all counters");
283a9f8b16fSGleb Natapov }
284a9f8b16fSGleb Natapov 
2857ec3b67aSLike Xu static uint64_t measure_for_overflow(pmu_counter_t *cnt)
2867ec3b67aSLike Xu {
2877ec3b67aSLike Xu 	__measure(cnt, 0);
2887ec3b67aSLike Xu 	/*
2897ec3b67aSLike Xu 	 * To generate overflow, i.e. roll over to '0', the initial count just
2907ec3b67aSLike Xu 	 * needs to be preset to the negative expected count.  However, as per
2917ec3b67aSLike Xu 	 * Intel's SDM, the preset count needs to be incremented by 1 to ensure
2927ec3b67aSLike Xu 	 * the overflow interrupt is generated immediately instead of possibly
2937ec3b67aSLike Xu 	 * waiting for the overflow to propagate through the counter.
2947ec3b67aSLike Xu 	 */
2957ec3b67aSLike Xu 	assert(cnt->count > 1);
2967ec3b67aSLike Xu 	return 1 - cnt->count;
2977ec3b67aSLike Xu }
2987ec3b67aSLike Xu 
299a9f8b16fSGleb Natapov static void check_counter_overflow(void)
300a9f8b16fSGleb Natapov {
3017ec3b67aSLike Xu 	uint64_t overflow_preset;
302a9f8b16fSGleb Natapov 	int i;
303a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
304cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(0),
305a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */,
306a9f8b16fSGleb Natapov 	};
3077ec3b67aSLike Xu 	overflow_preset = measure_for_overflow(&cnt);
308a9f8b16fSGleb Natapov 
309a9f8b16fSGleb Natapov 	/* clear status before test */
31062ba5036SLike Xu 	if (this_cpu_has_perf_global_status())
3118a2866d1SLike Xu 		pmu_clear_global_status();
312a9f8b16fSGleb Natapov 
3135bba1769SAndrew Jones 	report_prefix_push("overflow");
3145bba1769SAndrew Jones 
315cda64e80SLike Xu 	for (i = 0; i < pmu.nr_gp_counters + 1; i++) {
316a9f8b16fSGleb Natapov 		uint64_t status;
317a9f8b16fSGleb Natapov 		int idx;
31833cfc1b0SNadav Amit 
3197ec3b67aSLike Xu 		cnt.count = overflow_preset;
320cda64e80SLike Xu 		if (pmu_use_full_writes())
321414ee7d1SSean Christopherson 			cnt.count &= (1ull << pmu.gp_counter_width) - 1;
32233cfc1b0SNadav Amit 
323414ee7d1SSean Christopherson 		if (i == pmu.nr_gp_counters) {
324b883751aSLike Xu 			if (!pmu.is_intel)
325b883751aSLike Xu 				break;
326b883751aSLike Xu 
327a9f8b16fSGleb Natapov 			cnt.ctr = fixed_events[0].unit_sel;
3287ec3b67aSLike Xu 			cnt.count = measure_for_overflow(&cnt);
329cda64e80SLike Xu 			cnt.count &= (1ull << pmu.gp_counter_width) - 1;
330cda64e80SLike Xu 		} else {
331cda64e80SLike Xu 			cnt.ctr = MSR_GP_COUNTERx(i);
33233cfc1b0SNadav Amit 		}
33333cfc1b0SNadav Amit 
334a9f8b16fSGleb Natapov 		if (i % 2)
335a9f8b16fSGleb Natapov 			cnt.config |= EVNTSEL_INT;
336a9f8b16fSGleb Natapov 		else
337a9f8b16fSGleb Natapov 			cnt.config &= ~EVNTSEL_INT;
338a9f8b16fSGleb Natapov 		idx = event_to_global_idx(&cnt);
339e9e7577bSLike Xu 		__measure(&cnt, cnt.count);
340b883751aSLike Xu 		if (pmu.is_intel)
341a299895bSThomas Huth 			report(cnt.count == 1, "cntr-%d", i);
342b883751aSLike Xu 		else
343b883751aSLike Xu 			report(cnt.count == 0xffffffffffff || cnt.count < 7, "cntr-%d", i);
34462ba5036SLike Xu 
34562ba5036SLike Xu 		if (!this_cpu_has_perf_global_status())
34662ba5036SLike Xu 			continue;
34762ba5036SLike Xu 
3488a2866d1SLike Xu 		status = rdmsr(pmu.msr_global_status);
349a299895bSThomas Huth 		report(status & (1ull << idx), "status-%d", i);
3508a2866d1SLike Xu 		wrmsr(pmu.msr_global_status_clr, status);
3518a2866d1SLike Xu 		status = rdmsr(pmu.msr_global_status);
352a299895bSThomas Huth 		report(!(status & (1ull << idx)), "status clear-%d", i);
353a299895bSThomas Huth 		report(check_irq() == (i % 2), "irq-%d", i);
354a9f8b16fSGleb Natapov 	}
3555bba1769SAndrew Jones 
3565bba1769SAndrew Jones 	report_prefix_pop();
357a9f8b16fSGleb Natapov }
358a9f8b16fSGleb Natapov 
359a9f8b16fSGleb Natapov static void check_gp_counter_cmask(void)
360a9f8b16fSGleb Natapov {
361a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
362cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(0),
363a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */,
364a9f8b16fSGleb Natapov 	};
365a9f8b16fSGleb Natapov 	cnt.config |= (0x2 << EVNTSEL_CMASK_SHIFT);
3668554261fSLike Xu 	measure_one(&cnt);
367a299895bSThomas Huth 	report(cnt.count < gp_events[1].min, "cmask");
368a9f8b16fSGleb Natapov }
369a9f8b16fSGleb Natapov 
370ca1b9de9SNadav Amit static void do_rdpmc_fast(void *ptr)
371ca1b9de9SNadav Amit {
372ca1b9de9SNadav Amit 	pmu_counter_t *cnt = ptr;
373ca1b9de9SNadav Amit 	uint32_t idx = (uint32_t)cnt->idx | (1u << 31);
374ca1b9de9SNadav Amit 
375ca1b9de9SNadav Amit 	if (!is_gp(cnt))
376ca1b9de9SNadav Amit 		idx |= 1 << 30;
377ca1b9de9SNadav Amit 
378ca1b9de9SNadav Amit 	cnt->count = rdpmc(idx);
379ca1b9de9SNadav Amit }
380ca1b9de9SNadav Amit 
381ca1b9de9SNadav Amit 
382a9f8b16fSGleb Natapov static void check_rdpmc(void)
383a9f8b16fSGleb Natapov {
38422f2901aSLike Xu 	uint64_t val = 0xff0123456789ull;
385ca1b9de9SNadav Amit 	bool exc;
386a9f8b16fSGleb Natapov 	int i;
387a9f8b16fSGleb Natapov 
3885bba1769SAndrew Jones 	report_prefix_push("rdpmc");
3895bba1769SAndrew Jones 
390414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_gp_counters; i++) {
39133cfc1b0SNadav Amit 		uint64_t x;
392ca1b9de9SNadav Amit 		pmu_counter_t cnt = {
393cda64e80SLike Xu 			.ctr = MSR_GP_COUNTERx(i),
394ca1b9de9SNadav Amit 			.idx = i
395ca1b9de9SNadav Amit 		};
39633cfc1b0SNadav Amit 
39733cfc1b0SNadav Amit 	        /*
39822f2901aSLike Xu 	         * Without full-width writes, only the low 32 bits are writable,
39922f2901aSLike Xu 	         * and the value is sign-extended.
40033cfc1b0SNadav Amit 	         */
401cda64e80SLike Xu 		if (pmu.msr_gp_counter_base == MSR_IA32_PERFCTR0)
40233cfc1b0SNadav Amit 			x = (uint64_t)(int64_t)(int32_t)val;
40322f2901aSLike Xu 		else
40422f2901aSLike Xu 			x = (uint64_t)(int64_t)val;
40533cfc1b0SNadav Amit 
40633cfc1b0SNadav Amit 		/* Mask according to the number of supported bits */
407414ee7d1SSean Christopherson 		x &= (1ull << pmu.gp_counter_width) - 1;
40833cfc1b0SNadav Amit 
409cda64e80SLike Xu 		wrmsr(MSR_GP_COUNTERx(i), val);
410a299895bSThomas Huth 		report(rdpmc(i) == x, "cntr-%d", i);
411ca1b9de9SNadav Amit 
412ca1b9de9SNadav Amit 		exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt);
413ca1b9de9SNadav Amit 		if (exc)
414ca1b9de9SNadav Amit 			report_skip("fast-%d", i);
415ca1b9de9SNadav Amit 		else
416a299895bSThomas Huth 			report(cnt.count == (u32)val, "fast-%d", i);
417a9f8b16fSGleb Natapov 	}
418414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
419414ee7d1SSean Christopherson 		uint64_t x = val & ((1ull << pmu.fixed_counter_width) - 1);
420ca1b9de9SNadav Amit 		pmu_counter_t cnt = {
421ca1b9de9SNadav Amit 			.ctr = MSR_CORE_PERF_FIXED_CTR0 + i,
422ca1b9de9SNadav Amit 			.idx = i
423ca1b9de9SNadav Amit 		};
42433cfc1b0SNadav Amit 
4253f914933SLike Xu 		wrmsr(MSR_PERF_FIXED_CTRx(i), x);
426a299895bSThomas Huth 		report(rdpmc(i | (1 << 30)) == x, "fixed cntr-%d", i);
427ca1b9de9SNadav Amit 
428ca1b9de9SNadav Amit 		exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt);
429ca1b9de9SNadav Amit 		if (exc)
430ca1b9de9SNadav Amit 			report_skip("fixed fast-%d", i);
431ca1b9de9SNadav Amit 		else
432a299895bSThomas Huth 			report(cnt.count == (u32)x, "fixed fast-%d", i);
433a9f8b16fSGleb Natapov 	}
4345bba1769SAndrew Jones 
4355bba1769SAndrew Jones 	report_prefix_pop();
436a9f8b16fSGleb Natapov }
437a9f8b16fSGleb Natapov 
438ddade902SEric Hankland static void check_running_counter_wrmsr(void)
439ddade902SEric Hankland {
44059ca1413SEric Hankland 	uint64_t status;
44122f2901aSLike Xu 	uint64_t count;
442ddade902SEric Hankland 	pmu_counter_t evt = {
443cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(0),
444ddade902SEric Hankland 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
445ddade902SEric Hankland 	};
446ddade902SEric Hankland 
44759ca1413SEric Hankland 	report_prefix_push("running counter wrmsr");
44859ca1413SEric Hankland 
449ddade902SEric Hankland 	start_event(&evt);
450ddade902SEric Hankland 	loop();
451cda64e80SLike Xu 	wrmsr(MSR_GP_COUNTERx(0), 0);
452ddade902SEric Hankland 	stop_event(&evt);
45359ca1413SEric Hankland 	report(evt.count < gp_events[1].min, "cntr");
45459ca1413SEric Hankland 
45559ca1413SEric Hankland 	/* clear status before overflow test */
45662ba5036SLike Xu 	if (this_cpu_has_perf_global_status())
4578a2866d1SLike Xu 		pmu_clear_global_status();
45859ca1413SEric Hankland 
45959ca1413SEric Hankland 	start_event(&evt);
46022f2901aSLike Xu 
46122f2901aSLike Xu 	count = -1;
462cda64e80SLike Xu 	if (pmu_use_full_writes())
463414ee7d1SSean Christopherson 		count &= (1ull << pmu.gp_counter_width) - 1;
46422f2901aSLike Xu 
465cda64e80SLike Xu 	wrmsr(MSR_GP_COUNTERx(0), count);
46622f2901aSLike Xu 
46759ca1413SEric Hankland 	loop();
46859ca1413SEric Hankland 	stop_event(&evt);
46962ba5036SLike Xu 
47062ba5036SLike Xu 	if (this_cpu_has_perf_global_status()) {
4718a2866d1SLike Xu 		status = rdmsr(pmu.msr_global_status);
4728a2866d1SLike Xu 		report(status & 1, "status msr bit");
47362ba5036SLike Xu 	}
47459ca1413SEric Hankland 
47559ca1413SEric Hankland 	report_prefix_pop();
476ddade902SEric Hankland }
477ddade902SEric Hankland 
47820cf9147SJim Mattson static void check_emulated_instr(void)
47920cf9147SJim Mattson {
48020cf9147SJim Mattson 	uint64_t status, instr_start, brnch_start;
4818b547cc2SLike Xu 	uint64_t gp_counter_width = (1ull << pmu.gp_counter_width) - 1;
482b883751aSLike Xu 	unsigned int branch_idx = pmu.is_intel ? 5 : 2;
48320cf9147SJim Mattson 	pmu_counter_t brnch_cnt = {
484cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(0),
48520cf9147SJim Mattson 		/* branch instructions */
486b883751aSLike Xu 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[branch_idx].unit_sel,
48720cf9147SJim Mattson 	};
48820cf9147SJim Mattson 	pmu_counter_t instr_cnt = {
489cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(1),
49020cf9147SJim Mattson 		/* instructions */
49120cf9147SJim Mattson 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
49220cf9147SJim Mattson 	};
49320cf9147SJim Mattson 	report_prefix_push("emulated instruction");
49420cf9147SJim Mattson 
49562ba5036SLike Xu 	if (this_cpu_has_perf_global_status())
4968a2866d1SLike Xu 		pmu_clear_global_status();
49720cf9147SJim Mattson 
49820cf9147SJim Mattson 	start_event(&brnch_cnt);
49920cf9147SJim Mattson 	start_event(&instr_cnt);
50020cf9147SJim Mattson 
50120cf9147SJim Mattson 	brnch_start = -EXPECTED_BRNCH;
50220cf9147SJim Mattson 	instr_start = -EXPECTED_INSTR;
5038b547cc2SLike Xu 	wrmsr(MSR_GP_COUNTERx(0), brnch_start & gp_counter_width);
5048b547cc2SLike Xu 	wrmsr(MSR_GP_COUNTERx(1), instr_start & gp_counter_width);
50520cf9147SJim Mattson 	// KVM_FEP is a magic prefix that forces emulation so
50620cf9147SJim Mattson 	// 'KVM_FEP "jne label\n"' just counts as a single instruction.
50720cf9147SJim Mattson 	asm volatile(
50820cf9147SJim Mattson 		"mov $0x0, %%eax\n"
50920cf9147SJim Mattson 		"cmp $0x0, %%eax\n"
51020cf9147SJim Mattson 		KVM_FEP "jne label\n"
51120cf9147SJim Mattson 		KVM_FEP "jne label\n"
51220cf9147SJim Mattson 		KVM_FEP "jne label\n"
51320cf9147SJim Mattson 		KVM_FEP "jne label\n"
51420cf9147SJim Mattson 		KVM_FEP "jne label\n"
51520cf9147SJim Mattson 		"mov $0xa, %%eax\n"
51620cf9147SJim Mattson 		"cpuid\n"
51720cf9147SJim Mattson 		"mov $0xa, %%eax\n"
51820cf9147SJim Mattson 		"cpuid\n"
51920cf9147SJim Mattson 		"mov $0xa, %%eax\n"
52020cf9147SJim Mattson 		"cpuid\n"
52120cf9147SJim Mattson 		"mov $0xa, %%eax\n"
52220cf9147SJim Mattson 		"cpuid\n"
52320cf9147SJim Mattson 		"mov $0xa, %%eax\n"
52420cf9147SJim Mattson 		"cpuid\n"
52520cf9147SJim Mattson 		"label:\n"
52620cf9147SJim Mattson 		:
52720cf9147SJim Mattson 		:
52820cf9147SJim Mattson 		: "eax", "ebx", "ecx", "edx");
52920cf9147SJim Mattson 
53062ba5036SLike Xu 	if (this_cpu_has_perf_global_ctrl())
5318a2866d1SLike Xu 		wrmsr(pmu.msr_global_ctl, 0);
53220cf9147SJim Mattson 
53320cf9147SJim Mattson 	stop_event(&brnch_cnt);
53420cf9147SJim Mattson 	stop_event(&instr_cnt);
53520cf9147SJim Mattson 
53620cf9147SJim Mattson 	// Check that the end count - start count is at least the expected
53720cf9147SJim Mattson 	// number of instructions and branches.
53820cf9147SJim Mattson 	report(instr_cnt.count - instr_start >= EXPECTED_INSTR,
53920cf9147SJim Mattson 	       "instruction count");
54020cf9147SJim Mattson 	report(brnch_cnt.count - brnch_start >= EXPECTED_BRNCH,
54120cf9147SJim Mattson 	       "branch count");
54262ba5036SLike Xu 	if (this_cpu_has_perf_global_status()) {
54320cf9147SJim Mattson 		// Additionally check that those counters overflowed properly.
5448a2866d1SLike Xu 		status = rdmsr(pmu.msr_global_status);
5454070b9c6SLike Xu 		report(status & 1, "branch counter overflow");
5464070b9c6SLike Xu 		report(status & 2, "instruction counter overflow");
54762ba5036SLike Xu 	}
54820cf9147SJim Mattson 
54920cf9147SJim Mattson 	report_prefix_pop();
55020cf9147SJim Mattson }
55120cf9147SJim Mattson 
552006b089dSLike Xu #define XBEGIN_STARTED (~0u)
553006b089dSLike Xu static void check_tsx_cycles(void)
554006b089dSLike Xu {
555006b089dSLike Xu 	pmu_counter_t cnt;
556006b089dSLike Xu 	unsigned int i, ret = 0;
557006b089dSLike Xu 
558006b089dSLike Xu 	if (!this_cpu_has(X86_FEATURE_RTM))
559006b089dSLike Xu 		return;
560006b089dSLike Xu 
561006b089dSLike Xu 	report_prefix_push("TSX cycles");
562006b089dSLike Xu 
563006b089dSLike Xu 	for (i = 0; i < pmu.nr_gp_counters; i++) {
564006b089dSLike Xu 		cnt.ctr = MSR_GP_COUNTERx(i);
565006b089dSLike Xu 
566006b089dSLike Xu 		if (i == 2) {
567d4ae0a71SThomas Huth 			/* Transactional cycles committed only on gp counter 2 */
568006b089dSLike Xu 			cnt.config = EVNTSEL_OS | EVNTSEL_USR | 0x30000003c;
569006b089dSLike Xu 		} else {
570006b089dSLike Xu 			/* Transactional cycles */
571006b089dSLike Xu 			cnt.config = EVNTSEL_OS | EVNTSEL_USR | 0x10000003c;
572006b089dSLike Xu 		}
573006b089dSLike Xu 
574006b089dSLike Xu 		start_event(&cnt);
575006b089dSLike Xu 
576006b089dSLike Xu 		asm volatile("xbegin 1f\n\t"
577006b089dSLike Xu 				"1:\n\t"
578006b089dSLike Xu 				: "+a" (ret) :: "memory");
579006b089dSLike Xu 
580006b089dSLike Xu 		/* Generate a non-canonical #GP to trigger ABORT. */
581006b089dSLike Xu 		if (ret == XBEGIN_STARTED)
582006b089dSLike Xu 			*(int *)NONCANONICAL = 0;
583006b089dSLike Xu 
584006b089dSLike Xu 		stop_event(&cnt);
585006b089dSLike Xu 
586006b089dSLike Xu 		report(cnt.count > 0, "gp cntr-%d with a value of %" PRId64 "", i, cnt.count);
587006b089dSLike Xu 	}
588006b089dSLike Xu 
589006b089dSLike Xu 	report_prefix_pop();
590006b089dSLike Xu }
591006b089dSLike Xu 
59222f2901aSLike Xu static void check_counters(void)
59322f2901aSLike Xu {
59400dca75cSLike Xu 	if (is_fep_available())
59500dca75cSLike Xu 		check_emulated_instr();
59600dca75cSLike Xu 
59722f2901aSLike Xu 	check_gp_counters();
59822f2901aSLike Xu 	check_fixed_counters();
59922f2901aSLike Xu 	check_rdpmc();
60022f2901aSLike Xu 	check_counters_many();
60122f2901aSLike Xu 	check_counter_overflow();
60222f2901aSLike Xu 	check_gp_counter_cmask();
60322f2901aSLike Xu 	check_running_counter_wrmsr();
604006b089dSLike Xu 	check_tsx_cycles();
60522f2901aSLike Xu }
60622f2901aSLike Xu 
60722f2901aSLike Xu static void do_unsupported_width_counter_write(void *index)
60822f2901aSLike Xu {
60922f2901aSLike Xu 	wrmsr(MSR_IA32_PMC0 + *((int *) index), 0xffffff0123456789ull);
61022f2901aSLike Xu }
61122f2901aSLike Xu 
61222f2901aSLike Xu static void check_gp_counters_write_width(void)
61322f2901aSLike Xu {
61422f2901aSLike Xu 	u64 val_64 = 0xffffff0123456789ull;
6154b74c718SThomas Huth 	u64 val_32 = val_64 & ((1ull << 32) - 1);
616414ee7d1SSean Christopherson 	u64 val_max_width = val_64 & ((1ull << pmu.gp_counter_width) - 1);
61722f2901aSLike Xu 	int i;
61822f2901aSLike Xu 
61922f2901aSLike Xu 	/*
62022f2901aSLike Xu 	 * MSR_IA32_PERFCTRn supports 64-bit writes,
62122f2901aSLike Xu 	 * but only the lowest 32 bits are valid.
62222f2901aSLike Xu 	 */
623414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_gp_counters; i++) {
62422f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_32);
62522f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
62622f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
62722f2901aSLike Xu 
62822f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_max_width);
62922f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
63022f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
63122f2901aSLike Xu 
63222f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_64);
63322f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
63422f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
63522f2901aSLike Xu 	}
63622f2901aSLike Xu 
63722f2901aSLike Xu 	/*
6384340720eSLike Xu 	 * MSR_IA32_PMCn supports writing values up to GP counter width,
63922f2901aSLike Xu 	 * and only the lowest bits of GP counter width are valid.
64022f2901aSLike Xu 	 */
641414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_gp_counters; i++) {
64222f2901aSLike Xu 		wrmsr(MSR_IA32_PMC0 + i, val_32);
64322f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
64422f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
64522f2901aSLike Xu 
64622f2901aSLike Xu 		wrmsr(MSR_IA32_PMC0 + i, val_max_width);
64722f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_max_width);
64822f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_max_width);
64922f2901aSLike Xu 
65022f2901aSLike Xu 		report(test_for_exception(GP_VECTOR,
65122f2901aSLike Xu 			do_unsupported_width_counter_write, &i),
65222f2901aSLike Xu 		"writing unsupported width to MSR_IA32_PMC%d raises #GP", i);
65322f2901aSLike Xu 	}
65422f2901aSLike Xu }
65522f2901aSLike Xu 
656290f4213SJim Mattson /*
657290f4213SJim Mattson  * Per the SDM, reference cycles are currently implemented using the
658290f4213SJim Mattson  * core crystal clock, TSC, or bus clock. Calibrate to the TSC
659290f4213SJim Mattson  * frequency to set reasonable expectations.
660290f4213SJim Mattson  */
661290f4213SJim Mattson static void set_ref_cycle_expectations(void)
662290f4213SJim Mattson {
663290f4213SJim Mattson 	pmu_counter_t cnt = {
664290f4213SJim Mattson 		.ctr = MSR_IA32_PERFCTR0,
6657c648ce2SLike Xu 		.config = EVNTSEL_OS | EVNTSEL_USR | intel_gp_events[2].unit_sel,
666290f4213SJim Mattson 	};
667290f4213SJim Mattson 	uint64_t tsc_delta;
668290f4213SJim Mattson 	uint64_t t0, t1, t2, t3;
669290f4213SJim Mattson 
6702719b92cSYang Weijiang 	/* Bit 2 enumerates the availability of reference cycles events. */
671414ee7d1SSean Christopherson 	if (!pmu.nr_gp_counters || !pmu_gp_counter_is_available(2))
672290f4213SJim Mattson 		return;
673290f4213SJim Mattson 
67462ba5036SLike Xu 	if (this_cpu_has_perf_global_ctrl())
6758a2866d1SLike Xu 		wrmsr(pmu.msr_global_ctl, 0);
676290f4213SJim Mattson 
677290f4213SJim Mattson 	t0 = fenced_rdtsc();
678290f4213SJim Mattson 	start_event(&cnt);
679290f4213SJim Mattson 	t1 = fenced_rdtsc();
680290f4213SJim Mattson 
681290f4213SJim Mattson 	/*
682290f4213SJim Mattson 	 * This loop has to run long enough to dominate the VM-exit
683290f4213SJim Mattson 	 * costs for playing with the PMU MSRs on start and stop.
684290f4213SJim Mattson 	 *
685290f4213SJim Mattson 	 * On a 2.6GHz Ice Lake, with the TSC frequency at 104 times
686290f4213SJim Mattson 	 * the core crystal clock, this function calculated a guest
687290f4213SJim Mattson 	 * TSC : ref cycles ratio of around 105 with ECX initialized
688290f4213SJim Mattson 	 * to one billion.
689290f4213SJim Mattson 	 */
690290f4213SJim Mattson 	asm volatile("loop ." : "+c"((int){1000000000ull}));
691290f4213SJim Mattson 
692290f4213SJim Mattson 	t2 = fenced_rdtsc();
693290f4213SJim Mattson 	stop_event(&cnt);
694290f4213SJim Mattson 	t3 = fenced_rdtsc();
695290f4213SJim Mattson 
696290f4213SJim Mattson 	tsc_delta = ((t2 - t1) + (t3 - t0)) / 2;
697290f4213SJim Mattson 
698290f4213SJim Mattson 	if (!tsc_delta)
699290f4213SJim Mattson 		return;
700290f4213SJim Mattson 
7017c648ce2SLike Xu 	intel_gp_events[2].min = (intel_gp_events[2].min * cnt.count) / tsc_delta;
7027c648ce2SLike Xu 	intel_gp_events[2].max = (intel_gp_events[2].max * cnt.count) / tsc_delta;
703290f4213SJim Mattson }
704290f4213SJim Mattson 
70585c21181SLike Xu static void check_invalid_rdpmc_gp(void)
70685c21181SLike Xu {
70785c21181SLike Xu 	uint64_t val;
70885c21181SLike Xu 
70985c21181SLike Xu 	report(rdpmc_safe(64, &val) == GP_VECTOR,
71085c21181SLike Xu 	       "Expected #GP on RDPMC(64)");
71185c21181SLike Xu }
71285c21181SLike Xu 
713a9f8b16fSGleb Natapov int main(int ac, char **av)
714a9f8b16fSGleb Natapov {
715a9f8b16fSGleb Natapov 	setup_vm();
7165a2cb3e6SLike Xu 	handle_irq(PMI_VECTOR, cnt_overflow);
717dcda215bSPaolo Bonzini 	buf = malloc(N*64);
718a9f8b16fSGleb Natapov 
71985c21181SLike Xu 	check_invalid_rdpmc_gp();
72085c21181SLike Xu 
721b883751aSLike Xu 	if (pmu.is_intel) {
722414ee7d1SSean Christopherson 		if (!pmu.version) {
72303041e97SLike Xu 			report_skip("No Intel Arch PMU is detected!");
72432b9603cSRadim Krčmář 			return report_summary();
725a9f8b16fSGleb Natapov 		}
7267c648ce2SLike Xu 		gp_events = (struct pmu_event *)intel_gp_events;
7277c648ce2SLike Xu 		gp_events_size = sizeof(intel_gp_events)/sizeof(intel_gp_events[0]);
728b883751aSLike Xu 		report_prefix_push("Intel");
729290f4213SJim Mattson 		set_ref_cycle_expectations();
730b883751aSLike Xu 	} else {
731b883751aSLike Xu 		gp_events_size = sizeof(amd_gp_events)/sizeof(amd_gp_events[0]);
732b883751aSLike Xu 		gp_events = (struct pmu_event *)amd_gp_events;
733b883751aSLike Xu 		report_prefix_push("AMD");
734b883751aSLike Xu 	}
735290f4213SJim Mattson 
736414ee7d1SSean Christopherson 	printf("PMU version:         %d\n", pmu.version);
737414ee7d1SSean Christopherson 	printf("GP counters:         %d\n", pmu.nr_gp_counters);
738414ee7d1SSean Christopherson 	printf("GP counter width:    %d\n", pmu.gp_counter_width);
739414ee7d1SSean Christopherson 	printf("Mask length:         %d\n", pmu.gp_counter_mask_length);
740414ee7d1SSean Christopherson 	printf("Fixed counters:      %d\n", pmu.nr_fixed_counters);
741414ee7d1SSean Christopherson 	printf("Fixed counter width: %d\n", pmu.fixed_counter_width);
7420ef1f6a8SPaolo Bonzini 
7435a2cb3e6SLike Xu 	apic_write(APIC_LVTPC, PMI_VECTOR);
744a9f8b16fSGleb Natapov 
745afa714b2SPaolo Bonzini 	check_counters();
74620cf9147SJim Mattson 
747879e7f07SLike Xu 	if (pmu_has_full_writes()) {
748cda64e80SLike Xu 		pmu.msr_gp_counter_base = MSR_IA32_PMC0;
749cda64e80SLike Xu 
75022f2901aSLike Xu 		report_prefix_push("full-width writes");
75122f2901aSLike Xu 		check_counters();
75222f2901aSLike Xu 		check_gp_counters_write_width();
753d7714e16SLike Xu 		report_prefix_pop();
75422f2901aSLike Xu 	}
755a9f8b16fSGleb Natapov 
756b883751aSLike Xu 	if (!pmu.is_intel) {
757b883751aSLike Xu 		report_prefix_push("K7");
758b883751aSLike Xu 		pmu.nr_gp_counters = AMD64_NUM_COUNTERS;
759b883751aSLike Xu 		pmu.msr_gp_counter_base = MSR_K7_PERFCTR0;
760b883751aSLike Xu 		pmu.msr_gp_event_select_base = MSR_K7_EVNTSEL0;
761b883751aSLike Xu 		check_counters();
762b883751aSLike Xu 		report_prefix_pop();
763b883751aSLike Xu 	}
764b883751aSLike Xu 
765f3cdd159SJan Kiszka 	return report_summary();
766a9f8b16fSGleb Natapov }
767