xref: /kvm-unit-tests/x86/pmu.c (revision d4ae0a719d12f3927aa831b0c2c534a854a44845)
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"
9dcda215bSPaolo Bonzini #include "alloc.h"
10a9f8b16fSGleb Natapov 
11a9f8b16fSGleb Natapov #include "libcflat.h"
12a9f8b16fSGleb Natapov #include <stdint.h>
13a9f8b16fSGleb Natapov 
14a9f8b16fSGleb Natapov #define N 1000000
15a9f8b16fSGleb Natapov 
1620cf9147SJim Mattson // These values match the number of instructions and branches in the
1720cf9147SJim Mattson // assembly block in check_emulated_instr().
1820cf9147SJim Mattson #define EXPECTED_INSTR 17
1920cf9147SJim Mattson #define EXPECTED_BRNCH 5
2020cf9147SJim Mattson 
21a9f8b16fSGleb Natapov typedef struct {
22a9f8b16fSGleb Natapov 	uint32_t ctr;
23006b089dSLike Xu 	uint64_t config;
24a9f8b16fSGleb Natapov 	uint64_t count;
25a9f8b16fSGleb Natapov 	int idx;
26a9f8b16fSGleb Natapov } pmu_counter_t;
27a9f8b16fSGleb Natapov 
28a9f8b16fSGleb Natapov struct pmu_event {
29797d79a2SThomas Huth 	const char *name;
30a9f8b16fSGleb Natapov 	uint32_t unit_sel;
31a9f8b16fSGleb Natapov 	int min;
32a9f8b16fSGleb Natapov 	int max;
337c648ce2SLike Xu } intel_gp_events[] = {
34a9f8b16fSGleb Natapov 	{"core cycles", 0x003c, 1*N, 50*N},
35a9f8b16fSGleb Natapov 	{"instructions", 0x00c0, 10*N, 10.2*N},
36290f4213SJim Mattson 	{"ref cycles", 0x013c, 1*N, 30*N},
37290f4213SJim Mattson 	{"llc references", 0x4f2e, 1, 2*N},
38a9f8b16fSGleb Natapov 	{"llc misses", 0x412e, 1, 1*N},
39a9f8b16fSGleb Natapov 	{"branches", 0x00c4, 1*N, 1.1*N},
40a9f8b16fSGleb Natapov 	{"branch misses", 0x00c5, 0, 0.1*N},
41b883751aSLike Xu }, amd_gp_events[] = {
42b883751aSLike Xu 	{"core cycles", 0x0076, 1*N, 50*N},
43b883751aSLike Xu 	{"instructions", 0x00c0, 10*N, 10.2*N},
44b883751aSLike Xu 	{"branches", 0x00c2, 1*N, 1.1*N},
45b883751aSLike Xu 	{"branch misses", 0x00c3, 0, 0.1*N},
46a9f8b16fSGleb Natapov }, fixed_events[] = {
47a9f8b16fSGleb Natapov 	{"fixed 1", MSR_CORE_PERF_FIXED_CTR0, 10*N, 10.2*N},
48a9f8b16fSGleb Natapov 	{"fixed 2", MSR_CORE_PERF_FIXED_CTR0 + 1, 1*N, 30*N},
490ef1f6a8SPaolo Bonzini 	{"fixed 3", MSR_CORE_PERF_FIXED_CTR0 + 2, 0.1*N, 30*N}
50a9f8b16fSGleb Natapov };
51a9f8b16fSGleb Natapov 
52a9f8b16fSGleb Natapov char *buf;
53a9f8b16fSGleb Natapov 
547c648ce2SLike Xu static struct pmu_event *gp_events;
557c648ce2SLike Xu static unsigned int gp_events_size;
567c648ce2SLike Xu 
577db17e21SThomas Huth static inline void loop(void)
58a9f8b16fSGleb Natapov {
59a9f8b16fSGleb Natapov 	unsigned long tmp, tmp2, tmp3;
60a9f8b16fSGleb Natapov 
61a9f8b16fSGleb Natapov 	asm volatile("1: mov (%1), %2; add $64, %1; nop; nop; nop; nop; nop; nop; nop; loop 1b"
62a9f8b16fSGleb Natapov 			: "=c"(tmp), "=r"(tmp2), "=r"(tmp3): "0"(N), "1"(buf));
63a9f8b16fSGleb Natapov 
64a9f8b16fSGleb Natapov }
65a9f8b16fSGleb Natapov 
66a9f8b16fSGleb Natapov volatile uint64_t irq_received;
67a9f8b16fSGleb Natapov 
68a9f8b16fSGleb Natapov static void cnt_overflow(isr_regs_t *regs)
69a9f8b16fSGleb Natapov {
70a9f8b16fSGleb Natapov 	irq_received++;
71a9f8b16fSGleb Natapov 	apic_write(APIC_EOI, 0);
72a9f8b16fSGleb Natapov }
73a9f8b16fSGleb Natapov 
74a9f8b16fSGleb Natapov static bool check_irq(void)
75a9f8b16fSGleb Natapov {
76a9f8b16fSGleb Natapov 	int i;
77a9f8b16fSGleb Natapov 	irq_received = 0;
78787f0aebSMaxim Levitsky 	sti();
79a9f8b16fSGleb Natapov 	for (i = 0; i < 100000 && !irq_received; i++)
80a9f8b16fSGleb Natapov 		asm volatile("pause");
81787f0aebSMaxim Levitsky 	cli();
82a9f8b16fSGleb Natapov 	return irq_received;
83a9f8b16fSGleb Natapov }
84a9f8b16fSGleb Natapov 
85a9f8b16fSGleb Natapov static bool is_gp(pmu_counter_t *evt)
86a9f8b16fSGleb Natapov {
87b883751aSLike Xu 	if (!pmu.is_intel)
88b883751aSLike Xu 		return true;
89b883751aSLike Xu 
9022f2901aSLike Xu 	return evt->ctr < MSR_CORE_PERF_FIXED_CTR0 ||
9122f2901aSLike Xu 		evt->ctr >= MSR_IA32_PMC0;
92a9f8b16fSGleb Natapov }
93a9f8b16fSGleb Natapov 
94a9f8b16fSGleb Natapov static int event_to_global_idx(pmu_counter_t *cnt)
95a9f8b16fSGleb Natapov {
96b883751aSLike Xu 	if (pmu.is_intel)
97cda64e80SLike Xu 		return cnt->ctr - (is_gp(cnt) ? pmu.msr_gp_counter_base :
98a9f8b16fSGleb Natapov 			(MSR_CORE_PERF_FIXED_CTR0 - FIXED_CNT_INDEX));
99b883751aSLike Xu 
100b883751aSLike Xu 	if (pmu.msr_gp_counter_base == MSR_F15H_PERF_CTR0)
101b883751aSLike Xu 		return (cnt->ctr - pmu.msr_gp_counter_base) / 2;
102b883751aSLike Xu 	else
103b883751aSLike Xu 		return cnt->ctr - pmu.msr_gp_counter_base;
104a9f8b16fSGleb Natapov }
105a9f8b16fSGleb Natapov 
106a9f8b16fSGleb Natapov static struct pmu_event* get_counter_event(pmu_counter_t *cnt)
107a9f8b16fSGleb Natapov {
108a9f8b16fSGleb Natapov 	if (is_gp(cnt)) {
109a9f8b16fSGleb Natapov 		int i;
110a9f8b16fSGleb Natapov 
1117c648ce2SLike Xu 		for (i = 0; i < gp_events_size; i++)
112a9f8b16fSGleb Natapov 			if (gp_events[i].unit_sel == (cnt->config & 0xffff))
113a9f8b16fSGleb Natapov 				return &gp_events[i];
114a9f8b16fSGleb Natapov 	} else
115a9f8b16fSGleb Natapov 		return &fixed_events[cnt->ctr - MSR_CORE_PERF_FIXED_CTR0];
116a9f8b16fSGleb Natapov 
117a9f8b16fSGleb Natapov 	return (void*)0;
118a9f8b16fSGleb Natapov }
119a9f8b16fSGleb Natapov 
120a9f8b16fSGleb Natapov static void global_enable(pmu_counter_t *cnt)
121a9f8b16fSGleb Natapov {
12262ba5036SLike Xu 	if (!this_cpu_has_perf_global_ctrl())
12362ba5036SLike Xu 		return;
12462ba5036SLike Xu 
125a9f8b16fSGleb Natapov 	cnt->idx = event_to_global_idx(cnt);
1268a2866d1SLike Xu 	wrmsr(pmu.msr_global_ctl, rdmsr(pmu.msr_global_ctl) | BIT_ULL(cnt->idx));
127a9f8b16fSGleb Natapov }
128a9f8b16fSGleb Natapov 
129a9f8b16fSGleb Natapov static void global_disable(pmu_counter_t *cnt)
130a9f8b16fSGleb Natapov {
13162ba5036SLike Xu 	if (!this_cpu_has_perf_global_ctrl())
13262ba5036SLike Xu 		return;
13362ba5036SLike Xu 
1348a2866d1SLike Xu 	wrmsr(pmu.msr_global_ctl, rdmsr(pmu.msr_global_ctl) & ~BIT_ULL(cnt->idx));
135a9f8b16fSGleb Natapov }
136a9f8b16fSGleb Natapov 
137e9e7577bSLike Xu static void __start_event(pmu_counter_t *evt, uint64_t count)
138a9f8b16fSGleb Natapov {
139e9e7577bSLike Xu     evt->count = count;
140a9f8b16fSGleb Natapov     wrmsr(evt->ctr, evt->count);
141cda64e80SLike Xu     if (is_gp(evt)) {
142cda64e80SLike Xu 	    wrmsr(MSR_GP_EVENT_SELECTx(event_to_global_idx(evt)),
143a9f8b16fSGleb Natapov 		  evt->config | EVNTSEL_EN);
144cda64e80SLike Xu     } else {
145a9f8b16fSGleb Natapov 	    uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
146a9f8b16fSGleb Natapov 	    int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
147a9f8b16fSGleb Natapov 	    uint32_t usrospmi = 0;
148a9f8b16fSGleb Natapov 
149a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_OS)
150a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 0);
151a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_USR)
152a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 1);
153a9f8b16fSGleb Natapov 	    if (evt->config & EVNTSEL_INT)
154a9f8b16fSGleb Natapov 		    usrospmi |= (1 << 3); // PMI on overflow
155a9f8b16fSGleb Natapov 	    ctrl = (ctrl & ~(0xf << shift)) | (usrospmi << shift);
156a9f8b16fSGleb Natapov 	    wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl);
157a9f8b16fSGleb Natapov     }
158a9f8b16fSGleb Natapov     global_enable(evt);
1595a2cb3e6SLike Xu     apic_write(APIC_LVTPC, PMI_VECTOR);
160a9f8b16fSGleb Natapov }
161a9f8b16fSGleb Natapov 
162e9e7577bSLike Xu static void start_event(pmu_counter_t *evt)
163e9e7577bSLike Xu {
164e9e7577bSLike Xu 	__start_event(evt, 0);
165e9e7577bSLike Xu }
166e9e7577bSLike Xu 
167a9f8b16fSGleb Natapov static void stop_event(pmu_counter_t *evt)
168a9f8b16fSGleb Natapov {
169a9f8b16fSGleb Natapov 	global_disable(evt);
170cda64e80SLike Xu 	if (is_gp(evt)) {
171cda64e80SLike Xu 		wrmsr(MSR_GP_EVENT_SELECTx(event_to_global_idx(evt)),
172a9f8b16fSGleb Natapov 		      evt->config & ~EVNTSEL_EN);
173cda64e80SLike Xu 	} else {
174a9f8b16fSGleb Natapov 		uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
175a9f8b16fSGleb Natapov 		int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
176a9f8b16fSGleb Natapov 		wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl & ~(0xf << shift));
177a9f8b16fSGleb Natapov 	}
178a9f8b16fSGleb Natapov 	evt->count = rdmsr(evt->ctr);
179a9f8b16fSGleb Natapov }
180a9f8b16fSGleb Natapov 
1818554261fSLike Xu static noinline void measure_many(pmu_counter_t *evt, int count)
182a9f8b16fSGleb Natapov {
183a9f8b16fSGleb Natapov 	int i;
184a9f8b16fSGleb Natapov 	for (i = 0; i < count; i++)
185a9f8b16fSGleb Natapov 		start_event(&evt[i]);
186a9f8b16fSGleb Natapov 	loop();
187a9f8b16fSGleb Natapov 	for (i = 0; i < count; i++)
188a9f8b16fSGleb Natapov 		stop_event(&evt[i]);
189a9f8b16fSGleb Natapov }
190a9f8b16fSGleb Natapov 
1918554261fSLike Xu static void measure_one(pmu_counter_t *evt)
1928554261fSLike Xu {
1938554261fSLike Xu 	measure_many(evt, 1);
1948554261fSLike Xu }
1958554261fSLike Xu 
196e9e7577bSLike Xu static noinline void __measure(pmu_counter_t *evt, uint64_t count)
197e9e7577bSLike Xu {
198e9e7577bSLike Xu 	__start_event(evt, count);
199e9e7577bSLike Xu 	loop();
200e9e7577bSLike Xu 	stop_event(evt);
201e9e7577bSLike Xu }
202e9e7577bSLike Xu 
203a9f8b16fSGleb Natapov static bool verify_event(uint64_t count, struct pmu_event *e)
204a9f8b16fSGleb Natapov {
205290f4213SJim Mattson 	// printf("%d <= %ld <= %d\n", e->min, count, e->max);
206a9f8b16fSGleb Natapov 	return count >= e->min  && count <= e->max;
207a9f8b16fSGleb Natapov 
208a9f8b16fSGleb Natapov }
209a9f8b16fSGleb Natapov 
210a9f8b16fSGleb Natapov static bool verify_counter(pmu_counter_t *cnt)
211a9f8b16fSGleb Natapov {
212a9f8b16fSGleb Natapov 	return verify_event(cnt->count, get_counter_event(cnt));
213a9f8b16fSGleb Natapov }
214a9f8b16fSGleb Natapov 
215a9f8b16fSGleb Natapov static void check_gp_counter(struct pmu_event *evt)
216a9f8b16fSGleb Natapov {
217a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
218a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel,
219a9f8b16fSGleb Natapov 	};
220a9f8b16fSGleb Natapov 	int i;
221a9f8b16fSGleb Natapov 
222cda64e80SLike Xu 	for (i = 0; i < pmu.nr_gp_counters; i++) {
223cda64e80SLike Xu 		cnt.ctr = MSR_GP_COUNTERx(i);
2248554261fSLike Xu 		measure_one(&cnt);
225a299895bSThomas Huth 		report(verify_event(cnt.count, evt), "%s-%d", evt->name, i);
226a9f8b16fSGleb Natapov 	}
227a9f8b16fSGleb Natapov }
228a9f8b16fSGleb Natapov 
229a9f8b16fSGleb Natapov static void check_gp_counters(void)
230a9f8b16fSGleb Natapov {
231a9f8b16fSGleb Natapov 	int i;
232a9f8b16fSGleb Natapov 
2337c648ce2SLike Xu 	for (i = 0; i < gp_events_size; i++)
2342719b92cSYang Weijiang 		if (pmu_gp_counter_is_available(i))
235a9f8b16fSGleb Natapov 			check_gp_counter(&gp_events[i]);
236a9f8b16fSGleb Natapov 		else
237a9f8b16fSGleb Natapov 			printf("GP event '%s' is disabled\n",
238a9f8b16fSGleb Natapov 					gp_events[i].name);
239a9f8b16fSGleb Natapov }
240a9f8b16fSGleb Natapov 
241a9f8b16fSGleb Natapov static void check_fixed_counters(void)
242a9f8b16fSGleb Natapov {
243a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
244a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR,
245a9f8b16fSGleb Natapov 	};
246a9f8b16fSGleb Natapov 	int i;
247a9f8b16fSGleb Natapov 
248414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
249a9f8b16fSGleb Natapov 		cnt.ctr = fixed_events[i].unit_sel;
2508554261fSLike Xu 		measure_one(&cnt);
2512719b92cSYang Weijiang 		report(verify_event(cnt.count, &fixed_events[i]), "fixed-%d", i);
252a9f8b16fSGleb Natapov 	}
253a9f8b16fSGleb Natapov }
254a9f8b16fSGleb Natapov 
255a9f8b16fSGleb Natapov static void check_counters_many(void)
256a9f8b16fSGleb Natapov {
257a9f8b16fSGleb Natapov 	pmu_counter_t cnt[10];
258a9f8b16fSGleb Natapov 	int i, n;
259a9f8b16fSGleb Natapov 
260414ee7d1SSean Christopherson 	for (i = 0, n = 0; n < pmu.nr_gp_counters; i++) {
2612719b92cSYang Weijiang 		if (!pmu_gp_counter_is_available(i))
262a9f8b16fSGleb Natapov 			continue;
263a9f8b16fSGleb Natapov 
264cda64e80SLike Xu 		cnt[n].ctr = MSR_GP_COUNTERx(n);
2654ac45293SWei Huang 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
2667c648ce2SLike Xu 			gp_events[i % gp_events_size].unit_sel;
267a9f8b16fSGleb Natapov 		n++;
268a9f8b16fSGleb Natapov 	}
269414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
270a9f8b16fSGleb Natapov 		cnt[n].ctr = fixed_events[i].unit_sel;
271a9f8b16fSGleb Natapov 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR;
272a9f8b16fSGleb Natapov 		n++;
273a9f8b16fSGleb Natapov 	}
274a9f8b16fSGleb Natapov 
2758554261fSLike Xu 	measure_many(cnt, n);
276a9f8b16fSGleb Natapov 
277a9f8b16fSGleb Natapov 	for (i = 0; i < n; i++)
278a9f8b16fSGleb Natapov 		if (!verify_counter(&cnt[i]))
279a9f8b16fSGleb Natapov 			break;
280a9f8b16fSGleb Natapov 
281a299895bSThomas Huth 	report(i == n, "all counters");
282a9f8b16fSGleb Natapov }
283a9f8b16fSGleb Natapov 
2847ec3b67aSLike Xu static uint64_t measure_for_overflow(pmu_counter_t *cnt)
2857ec3b67aSLike Xu {
2867ec3b67aSLike Xu 	__measure(cnt, 0);
2877ec3b67aSLike Xu 	/*
2887ec3b67aSLike Xu 	 * To generate overflow, i.e. roll over to '0', the initial count just
2897ec3b67aSLike Xu 	 * needs to be preset to the negative expected count.  However, as per
2907ec3b67aSLike Xu 	 * Intel's SDM, the preset count needs to be incremented by 1 to ensure
2917ec3b67aSLike Xu 	 * the overflow interrupt is generated immediately instead of possibly
2927ec3b67aSLike Xu 	 * waiting for the overflow to propagate through the counter.
2937ec3b67aSLike Xu 	 */
2947ec3b67aSLike Xu 	assert(cnt->count > 1);
2957ec3b67aSLike Xu 	return 1 - cnt->count;
2967ec3b67aSLike Xu }
2977ec3b67aSLike Xu 
298a9f8b16fSGleb Natapov static void check_counter_overflow(void)
299a9f8b16fSGleb Natapov {
3007ec3b67aSLike Xu 	uint64_t overflow_preset;
301a9f8b16fSGleb Natapov 	int i;
302a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
303cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(0),
304a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */,
305a9f8b16fSGleb Natapov 	};
3067ec3b67aSLike Xu 	overflow_preset = measure_for_overflow(&cnt);
307a9f8b16fSGleb Natapov 
308a9f8b16fSGleb Natapov 	/* clear status before test */
30962ba5036SLike Xu 	if (this_cpu_has_perf_global_status())
3108a2866d1SLike Xu 		pmu_clear_global_status();
311a9f8b16fSGleb Natapov 
3125bba1769SAndrew Jones 	report_prefix_push("overflow");
3135bba1769SAndrew Jones 
314cda64e80SLike Xu 	for (i = 0; i < pmu.nr_gp_counters + 1; i++) {
315a9f8b16fSGleb Natapov 		uint64_t status;
316a9f8b16fSGleb Natapov 		int idx;
31733cfc1b0SNadav Amit 
3187ec3b67aSLike Xu 		cnt.count = overflow_preset;
319cda64e80SLike Xu 		if (pmu_use_full_writes())
320414ee7d1SSean Christopherson 			cnt.count &= (1ull << pmu.gp_counter_width) - 1;
32133cfc1b0SNadav Amit 
322414ee7d1SSean Christopherson 		if (i == pmu.nr_gp_counters) {
323b883751aSLike Xu 			if (!pmu.is_intel)
324b883751aSLike Xu 				break;
325b883751aSLike Xu 
326a9f8b16fSGleb Natapov 			cnt.ctr = fixed_events[0].unit_sel;
3277ec3b67aSLike Xu 			cnt.count = measure_for_overflow(&cnt);
328cda64e80SLike Xu 			cnt.count &= (1ull << pmu.gp_counter_width) - 1;
329cda64e80SLike Xu 		} else {
330cda64e80SLike Xu 			cnt.ctr = MSR_GP_COUNTERx(i);
33133cfc1b0SNadav Amit 		}
33233cfc1b0SNadav Amit 
333a9f8b16fSGleb Natapov 		if (i % 2)
334a9f8b16fSGleb Natapov 			cnt.config |= EVNTSEL_INT;
335a9f8b16fSGleb Natapov 		else
336a9f8b16fSGleb Natapov 			cnt.config &= ~EVNTSEL_INT;
337a9f8b16fSGleb Natapov 		idx = event_to_global_idx(&cnt);
338e9e7577bSLike Xu 		__measure(&cnt, cnt.count);
339b883751aSLike Xu 		if (pmu.is_intel)
340a299895bSThomas Huth 			report(cnt.count == 1, "cntr-%d", i);
341b883751aSLike Xu 		else
342b883751aSLike Xu 			report(cnt.count == 0xffffffffffff || cnt.count < 7, "cntr-%d", i);
34362ba5036SLike Xu 
34462ba5036SLike Xu 		if (!this_cpu_has_perf_global_status())
34562ba5036SLike Xu 			continue;
34662ba5036SLike Xu 
3478a2866d1SLike Xu 		status = rdmsr(pmu.msr_global_status);
348a299895bSThomas Huth 		report(status & (1ull << idx), "status-%d", i);
3498a2866d1SLike Xu 		wrmsr(pmu.msr_global_status_clr, status);
3508a2866d1SLike Xu 		status = rdmsr(pmu.msr_global_status);
351a299895bSThomas Huth 		report(!(status & (1ull << idx)), "status clear-%d", i);
352a299895bSThomas Huth 		report(check_irq() == (i % 2), "irq-%d", i);
353a9f8b16fSGleb Natapov 	}
3545bba1769SAndrew Jones 
3555bba1769SAndrew Jones 	report_prefix_pop();
356a9f8b16fSGleb Natapov }
357a9f8b16fSGleb Natapov 
358a9f8b16fSGleb Natapov static void check_gp_counter_cmask(void)
359a9f8b16fSGleb Natapov {
360a9f8b16fSGleb Natapov 	pmu_counter_t cnt = {
361cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(0),
362a9f8b16fSGleb Natapov 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel /* instructions */,
363a9f8b16fSGleb Natapov 	};
364a9f8b16fSGleb Natapov 	cnt.config |= (0x2 << EVNTSEL_CMASK_SHIFT);
3658554261fSLike Xu 	measure_one(&cnt);
366a299895bSThomas Huth 	report(cnt.count < gp_events[1].min, "cmask");
367a9f8b16fSGleb Natapov }
368a9f8b16fSGleb Natapov 
369ca1b9de9SNadav Amit static void do_rdpmc_fast(void *ptr)
370ca1b9de9SNadav Amit {
371ca1b9de9SNadav Amit 	pmu_counter_t *cnt = ptr;
372ca1b9de9SNadav Amit 	uint32_t idx = (uint32_t)cnt->idx | (1u << 31);
373ca1b9de9SNadav Amit 
374ca1b9de9SNadav Amit 	if (!is_gp(cnt))
375ca1b9de9SNadav Amit 		idx |= 1 << 30;
376ca1b9de9SNadav Amit 
377ca1b9de9SNadav Amit 	cnt->count = rdpmc(idx);
378ca1b9de9SNadav Amit }
379ca1b9de9SNadav Amit 
380ca1b9de9SNadav Amit 
381a9f8b16fSGleb Natapov static void check_rdpmc(void)
382a9f8b16fSGleb Natapov {
38322f2901aSLike Xu 	uint64_t val = 0xff0123456789ull;
384ca1b9de9SNadav Amit 	bool exc;
385a9f8b16fSGleb Natapov 	int i;
386a9f8b16fSGleb Natapov 
3875bba1769SAndrew Jones 	report_prefix_push("rdpmc");
3885bba1769SAndrew Jones 
389414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_gp_counters; i++) {
39033cfc1b0SNadav Amit 		uint64_t x;
391ca1b9de9SNadav Amit 		pmu_counter_t cnt = {
392cda64e80SLike Xu 			.ctr = MSR_GP_COUNTERx(i),
393ca1b9de9SNadav Amit 			.idx = i
394ca1b9de9SNadav Amit 		};
39533cfc1b0SNadav Amit 
39633cfc1b0SNadav Amit 	        /*
39722f2901aSLike Xu 	         * Without full-width writes, only the low 32 bits are writable,
39822f2901aSLike Xu 	         * and the value is sign-extended.
39933cfc1b0SNadav Amit 	         */
400cda64e80SLike Xu 		if (pmu.msr_gp_counter_base == MSR_IA32_PERFCTR0)
40133cfc1b0SNadav Amit 			x = (uint64_t)(int64_t)(int32_t)val;
40222f2901aSLike Xu 		else
40322f2901aSLike Xu 			x = (uint64_t)(int64_t)val;
40433cfc1b0SNadav Amit 
40533cfc1b0SNadav Amit 		/* Mask according to the number of supported bits */
406414ee7d1SSean Christopherson 		x &= (1ull << pmu.gp_counter_width) - 1;
40733cfc1b0SNadav Amit 
408cda64e80SLike Xu 		wrmsr(MSR_GP_COUNTERx(i), val);
409a299895bSThomas Huth 		report(rdpmc(i) == x, "cntr-%d", i);
410ca1b9de9SNadav Amit 
411ca1b9de9SNadav Amit 		exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt);
412ca1b9de9SNadav Amit 		if (exc)
413ca1b9de9SNadav Amit 			report_skip("fast-%d", i);
414ca1b9de9SNadav Amit 		else
415a299895bSThomas Huth 			report(cnt.count == (u32)val, "fast-%d", i);
416a9f8b16fSGleb Natapov 	}
417414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
418414ee7d1SSean Christopherson 		uint64_t x = val & ((1ull << pmu.fixed_counter_width) - 1);
419ca1b9de9SNadav Amit 		pmu_counter_t cnt = {
420ca1b9de9SNadav Amit 			.ctr = MSR_CORE_PERF_FIXED_CTR0 + i,
421ca1b9de9SNadav Amit 			.idx = i
422ca1b9de9SNadav Amit 		};
42333cfc1b0SNadav Amit 
4243f914933SLike Xu 		wrmsr(MSR_PERF_FIXED_CTRx(i), x);
425a299895bSThomas Huth 		report(rdpmc(i | (1 << 30)) == x, "fixed cntr-%d", i);
426ca1b9de9SNadav Amit 
427ca1b9de9SNadav Amit 		exc = test_for_exception(GP_VECTOR, do_rdpmc_fast, &cnt);
428ca1b9de9SNadav Amit 		if (exc)
429ca1b9de9SNadav Amit 			report_skip("fixed fast-%d", i);
430ca1b9de9SNadav Amit 		else
431a299895bSThomas Huth 			report(cnt.count == (u32)x, "fixed fast-%d", i);
432a9f8b16fSGleb Natapov 	}
4335bba1769SAndrew Jones 
4345bba1769SAndrew Jones 	report_prefix_pop();
435a9f8b16fSGleb Natapov }
436a9f8b16fSGleb Natapov 
437ddade902SEric Hankland static void check_running_counter_wrmsr(void)
438ddade902SEric Hankland {
43959ca1413SEric Hankland 	uint64_t status;
44022f2901aSLike Xu 	uint64_t count;
441ddade902SEric Hankland 	pmu_counter_t evt = {
442cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(0),
443ddade902SEric Hankland 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
444ddade902SEric Hankland 	};
445ddade902SEric Hankland 
44659ca1413SEric Hankland 	report_prefix_push("running counter wrmsr");
44759ca1413SEric Hankland 
448ddade902SEric Hankland 	start_event(&evt);
449ddade902SEric Hankland 	loop();
450cda64e80SLike Xu 	wrmsr(MSR_GP_COUNTERx(0), 0);
451ddade902SEric Hankland 	stop_event(&evt);
45259ca1413SEric Hankland 	report(evt.count < gp_events[1].min, "cntr");
45359ca1413SEric Hankland 
45459ca1413SEric Hankland 	/* clear status before overflow test */
45562ba5036SLike Xu 	if (this_cpu_has_perf_global_status())
4568a2866d1SLike Xu 		pmu_clear_global_status();
45759ca1413SEric Hankland 
45859ca1413SEric Hankland 	start_event(&evt);
45922f2901aSLike Xu 
46022f2901aSLike Xu 	count = -1;
461cda64e80SLike Xu 	if (pmu_use_full_writes())
462414ee7d1SSean Christopherson 		count &= (1ull << pmu.gp_counter_width) - 1;
46322f2901aSLike Xu 
464cda64e80SLike Xu 	wrmsr(MSR_GP_COUNTERx(0), count);
46522f2901aSLike Xu 
46659ca1413SEric Hankland 	loop();
46759ca1413SEric Hankland 	stop_event(&evt);
46862ba5036SLike Xu 
46962ba5036SLike Xu 	if (this_cpu_has_perf_global_status()) {
4708a2866d1SLike Xu 		status = rdmsr(pmu.msr_global_status);
4718a2866d1SLike Xu 		report(status & 1, "status msr bit");
47262ba5036SLike Xu 	}
47359ca1413SEric Hankland 
47459ca1413SEric Hankland 	report_prefix_pop();
475ddade902SEric Hankland }
476ddade902SEric Hankland 
47720cf9147SJim Mattson static void check_emulated_instr(void)
47820cf9147SJim Mattson {
47920cf9147SJim Mattson 	uint64_t status, instr_start, brnch_start;
4808b547cc2SLike Xu 	uint64_t gp_counter_width = (1ull << pmu.gp_counter_width) - 1;
481b883751aSLike Xu 	unsigned int branch_idx = pmu.is_intel ? 5 : 2;
48220cf9147SJim Mattson 	pmu_counter_t brnch_cnt = {
483cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(0),
48420cf9147SJim Mattson 		/* branch instructions */
485b883751aSLike Xu 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[branch_idx].unit_sel,
48620cf9147SJim Mattson 	};
48720cf9147SJim Mattson 	pmu_counter_t instr_cnt = {
488cda64e80SLike Xu 		.ctr = MSR_GP_COUNTERx(1),
48920cf9147SJim Mattson 		/* instructions */
49020cf9147SJim Mattson 		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
49120cf9147SJim Mattson 	};
49220cf9147SJim Mattson 	report_prefix_push("emulated instruction");
49320cf9147SJim Mattson 
49462ba5036SLike Xu 	if (this_cpu_has_perf_global_status())
4958a2866d1SLike Xu 		pmu_clear_global_status();
49620cf9147SJim Mattson 
49720cf9147SJim Mattson 	start_event(&brnch_cnt);
49820cf9147SJim Mattson 	start_event(&instr_cnt);
49920cf9147SJim Mattson 
50020cf9147SJim Mattson 	brnch_start = -EXPECTED_BRNCH;
50120cf9147SJim Mattson 	instr_start = -EXPECTED_INSTR;
5028b547cc2SLike Xu 	wrmsr(MSR_GP_COUNTERx(0), brnch_start & gp_counter_width);
5038b547cc2SLike Xu 	wrmsr(MSR_GP_COUNTERx(1), instr_start & gp_counter_width);
50420cf9147SJim Mattson 	// KVM_FEP is a magic prefix that forces emulation so
50520cf9147SJim Mattson 	// 'KVM_FEP "jne label\n"' just counts as a single instruction.
50620cf9147SJim Mattson 	asm volatile(
50720cf9147SJim Mattson 		"mov $0x0, %%eax\n"
50820cf9147SJim Mattson 		"cmp $0x0, %%eax\n"
50920cf9147SJim Mattson 		KVM_FEP "jne label\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 		"mov $0xa, %%eax\n"
51520cf9147SJim Mattson 		"cpuid\n"
51620cf9147SJim Mattson 		"mov $0xa, %%eax\n"
51720cf9147SJim Mattson 		"cpuid\n"
51820cf9147SJim Mattson 		"mov $0xa, %%eax\n"
51920cf9147SJim Mattson 		"cpuid\n"
52020cf9147SJim Mattson 		"mov $0xa, %%eax\n"
52120cf9147SJim Mattson 		"cpuid\n"
52220cf9147SJim Mattson 		"mov $0xa, %%eax\n"
52320cf9147SJim Mattson 		"cpuid\n"
52420cf9147SJim Mattson 		"label:\n"
52520cf9147SJim Mattson 		:
52620cf9147SJim Mattson 		:
52720cf9147SJim Mattson 		: "eax", "ebx", "ecx", "edx");
52820cf9147SJim Mattson 
52962ba5036SLike Xu 	if (this_cpu_has_perf_global_ctrl())
5308a2866d1SLike Xu 		wrmsr(pmu.msr_global_ctl, 0);
53120cf9147SJim Mattson 
53220cf9147SJim Mattson 	stop_event(&brnch_cnt);
53320cf9147SJim Mattson 	stop_event(&instr_cnt);
53420cf9147SJim Mattson 
53520cf9147SJim Mattson 	// Check that the end count - start count is at least the expected
53620cf9147SJim Mattson 	// number of instructions and branches.
53720cf9147SJim Mattson 	report(instr_cnt.count - instr_start >= EXPECTED_INSTR,
53820cf9147SJim Mattson 	       "instruction count");
53920cf9147SJim Mattson 	report(brnch_cnt.count - brnch_start >= EXPECTED_BRNCH,
54020cf9147SJim Mattson 	       "branch count");
54162ba5036SLike Xu 	if (this_cpu_has_perf_global_status()) {
54220cf9147SJim Mattson 		// Additionally check that those counters overflowed properly.
5438a2866d1SLike Xu 		status = rdmsr(pmu.msr_global_status);
5444070b9c6SLike Xu 		report(status & 1, "branch counter overflow");
5454070b9c6SLike Xu 		report(status & 2, "instruction counter overflow");
54662ba5036SLike Xu 	}
54720cf9147SJim Mattson 
54820cf9147SJim Mattson 	report_prefix_pop();
54920cf9147SJim Mattson }
55020cf9147SJim Mattson 
551006b089dSLike Xu #define XBEGIN_STARTED (~0u)
552006b089dSLike Xu static void check_tsx_cycles(void)
553006b089dSLike Xu {
554006b089dSLike Xu 	pmu_counter_t cnt;
555006b089dSLike Xu 	unsigned int i, ret = 0;
556006b089dSLike Xu 
557006b089dSLike Xu 	if (!this_cpu_has(X86_FEATURE_RTM))
558006b089dSLike Xu 		return;
559006b089dSLike Xu 
560006b089dSLike Xu 	report_prefix_push("TSX cycles");
561006b089dSLike Xu 
562006b089dSLike Xu 	for (i = 0; i < pmu.nr_gp_counters; i++) {
563006b089dSLike Xu 		cnt.ctr = MSR_GP_COUNTERx(i);
564006b089dSLike Xu 
565006b089dSLike Xu 		if (i == 2) {
566*d4ae0a71SThomas Huth 			/* Transactional cycles committed only on gp counter 2 */
567006b089dSLike Xu 			cnt.config = EVNTSEL_OS | EVNTSEL_USR | 0x30000003c;
568006b089dSLike Xu 		} else {
569006b089dSLike Xu 			/* Transactional cycles */
570006b089dSLike Xu 			cnt.config = EVNTSEL_OS | EVNTSEL_USR | 0x10000003c;
571006b089dSLike Xu 		}
572006b089dSLike Xu 
573006b089dSLike Xu 		start_event(&cnt);
574006b089dSLike Xu 
575006b089dSLike Xu 		asm volatile("xbegin 1f\n\t"
576006b089dSLike Xu 				"1:\n\t"
577006b089dSLike Xu 				: "+a" (ret) :: "memory");
578006b089dSLike Xu 
579006b089dSLike Xu 		/* Generate a non-canonical #GP to trigger ABORT. */
580006b089dSLike Xu 		if (ret == XBEGIN_STARTED)
581006b089dSLike Xu 			*(int *)NONCANONICAL = 0;
582006b089dSLike Xu 
583006b089dSLike Xu 		stop_event(&cnt);
584006b089dSLike Xu 
585006b089dSLike Xu 		report(cnt.count > 0, "gp cntr-%d with a value of %" PRId64 "", i, cnt.count);
586006b089dSLike Xu 	}
587006b089dSLike Xu 
588006b089dSLike Xu 	report_prefix_pop();
589006b089dSLike Xu }
590006b089dSLike Xu 
59122f2901aSLike Xu static void check_counters(void)
59222f2901aSLike Xu {
59300dca75cSLike Xu 	if (is_fep_available())
59400dca75cSLike Xu 		check_emulated_instr();
59500dca75cSLike Xu 
59622f2901aSLike Xu 	check_gp_counters();
59722f2901aSLike Xu 	check_fixed_counters();
59822f2901aSLike Xu 	check_rdpmc();
59922f2901aSLike Xu 	check_counters_many();
60022f2901aSLike Xu 	check_counter_overflow();
60122f2901aSLike Xu 	check_gp_counter_cmask();
60222f2901aSLike Xu 	check_running_counter_wrmsr();
603006b089dSLike Xu 	check_tsx_cycles();
60422f2901aSLike Xu }
60522f2901aSLike Xu 
60622f2901aSLike Xu static void do_unsupported_width_counter_write(void *index)
60722f2901aSLike Xu {
60822f2901aSLike Xu 	wrmsr(MSR_IA32_PMC0 + *((int *) index), 0xffffff0123456789ull);
60922f2901aSLike Xu }
61022f2901aSLike Xu 
61122f2901aSLike Xu static void check_gp_counters_write_width(void)
61222f2901aSLike Xu {
61322f2901aSLike Xu 	u64 val_64 = 0xffffff0123456789ull;
6144b74c718SThomas Huth 	u64 val_32 = val_64 & ((1ull << 32) - 1);
615414ee7d1SSean Christopherson 	u64 val_max_width = val_64 & ((1ull << pmu.gp_counter_width) - 1);
61622f2901aSLike Xu 	int i;
61722f2901aSLike Xu 
61822f2901aSLike Xu 	/*
61922f2901aSLike Xu 	 * MSR_IA32_PERFCTRn supports 64-bit writes,
62022f2901aSLike Xu 	 * but only the lowest 32 bits are valid.
62122f2901aSLike Xu 	 */
622414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_gp_counters; i++) {
62322f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_32);
62422f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
62522f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
62622f2901aSLike Xu 
62722f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_max_width);
62822f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
62922f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
63022f2901aSLike Xu 
63122f2901aSLike Xu 		wrmsr(MSR_IA32_PERFCTR0 + i, val_64);
63222f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
63322f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
63422f2901aSLike Xu 	}
63522f2901aSLike Xu 
63622f2901aSLike Xu 	/*
6374340720eSLike Xu 	 * MSR_IA32_PMCn supports writing values up to GP counter width,
63822f2901aSLike Xu 	 * and only the lowest bits of GP counter width are valid.
63922f2901aSLike Xu 	 */
640414ee7d1SSean Christopherson 	for (i = 0; i < pmu.nr_gp_counters; i++) {
64122f2901aSLike Xu 		wrmsr(MSR_IA32_PMC0 + i, val_32);
64222f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_32);
64322f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_32);
64422f2901aSLike Xu 
64522f2901aSLike Xu 		wrmsr(MSR_IA32_PMC0 + i, val_max_width);
64622f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PMC0 + i) == val_max_width);
64722f2901aSLike Xu 		assert(rdmsr(MSR_IA32_PERFCTR0 + i) == val_max_width);
64822f2901aSLike Xu 
64922f2901aSLike Xu 		report(test_for_exception(GP_VECTOR,
65022f2901aSLike Xu 			do_unsupported_width_counter_write, &i),
65122f2901aSLike Xu 		"writing unsupported width to MSR_IA32_PMC%d raises #GP", i);
65222f2901aSLike Xu 	}
65322f2901aSLike Xu }
65422f2901aSLike Xu 
655290f4213SJim Mattson /*
656290f4213SJim Mattson  * Per the SDM, reference cycles are currently implemented using the
657290f4213SJim Mattson  * core crystal clock, TSC, or bus clock. Calibrate to the TSC
658290f4213SJim Mattson  * frequency to set reasonable expectations.
659290f4213SJim Mattson  */
660290f4213SJim Mattson static void set_ref_cycle_expectations(void)
661290f4213SJim Mattson {
662290f4213SJim Mattson 	pmu_counter_t cnt = {
663290f4213SJim Mattson 		.ctr = MSR_IA32_PERFCTR0,
6647c648ce2SLike Xu 		.config = EVNTSEL_OS | EVNTSEL_USR | intel_gp_events[2].unit_sel,
665290f4213SJim Mattson 	};
666290f4213SJim Mattson 	uint64_t tsc_delta;
667290f4213SJim Mattson 	uint64_t t0, t1, t2, t3;
668290f4213SJim Mattson 
6692719b92cSYang Weijiang 	/* Bit 2 enumerates the availability of reference cycles events. */
670414ee7d1SSean Christopherson 	if (!pmu.nr_gp_counters || !pmu_gp_counter_is_available(2))
671290f4213SJim Mattson 		return;
672290f4213SJim Mattson 
67362ba5036SLike Xu 	if (this_cpu_has_perf_global_ctrl())
6748a2866d1SLike Xu 		wrmsr(pmu.msr_global_ctl, 0);
675290f4213SJim Mattson 
676290f4213SJim Mattson 	t0 = fenced_rdtsc();
677290f4213SJim Mattson 	start_event(&cnt);
678290f4213SJim Mattson 	t1 = fenced_rdtsc();
679290f4213SJim Mattson 
680290f4213SJim Mattson 	/*
681290f4213SJim Mattson 	 * This loop has to run long enough to dominate the VM-exit
682290f4213SJim Mattson 	 * costs for playing with the PMU MSRs on start and stop.
683290f4213SJim Mattson 	 *
684290f4213SJim Mattson 	 * On a 2.6GHz Ice Lake, with the TSC frequency at 104 times
685290f4213SJim Mattson 	 * the core crystal clock, this function calculated a guest
686290f4213SJim Mattson 	 * TSC : ref cycles ratio of around 105 with ECX initialized
687290f4213SJim Mattson 	 * to one billion.
688290f4213SJim Mattson 	 */
689290f4213SJim Mattson 	asm volatile("loop ." : "+c"((int){1000000000ull}));
690290f4213SJim Mattson 
691290f4213SJim Mattson 	t2 = fenced_rdtsc();
692290f4213SJim Mattson 	stop_event(&cnt);
693290f4213SJim Mattson 	t3 = fenced_rdtsc();
694290f4213SJim Mattson 
695290f4213SJim Mattson 	tsc_delta = ((t2 - t1) + (t3 - t0)) / 2;
696290f4213SJim Mattson 
697290f4213SJim Mattson 	if (!tsc_delta)
698290f4213SJim Mattson 		return;
699290f4213SJim Mattson 
7007c648ce2SLike Xu 	intel_gp_events[2].min = (intel_gp_events[2].min * cnt.count) / tsc_delta;
7017c648ce2SLike Xu 	intel_gp_events[2].max = (intel_gp_events[2].max * cnt.count) / tsc_delta;
702290f4213SJim Mattson }
703290f4213SJim Mattson 
70485c21181SLike Xu static void check_invalid_rdpmc_gp(void)
70585c21181SLike Xu {
70685c21181SLike Xu 	uint64_t val;
70785c21181SLike Xu 
70885c21181SLike Xu 	report(rdpmc_safe(64, &val) == GP_VECTOR,
70985c21181SLike Xu 	       "Expected #GP on RDPMC(64)");
71085c21181SLike Xu }
71185c21181SLike Xu 
712a9f8b16fSGleb Natapov int main(int ac, char **av)
713a9f8b16fSGleb Natapov {
714a9f8b16fSGleb Natapov 	setup_vm();
7155a2cb3e6SLike Xu 	handle_irq(PMI_VECTOR, cnt_overflow);
716dcda215bSPaolo Bonzini 	buf = malloc(N*64);
717a9f8b16fSGleb Natapov 
71885c21181SLike Xu 	check_invalid_rdpmc_gp();
71985c21181SLike Xu 
720b883751aSLike Xu 	if (pmu.is_intel) {
721414ee7d1SSean Christopherson 		if (!pmu.version) {
72203041e97SLike Xu 			report_skip("No Intel Arch PMU is detected!");
72332b9603cSRadim Krčmář 			return report_summary();
724a9f8b16fSGleb Natapov 		}
7257c648ce2SLike Xu 		gp_events = (struct pmu_event *)intel_gp_events;
7267c648ce2SLike Xu 		gp_events_size = sizeof(intel_gp_events)/sizeof(intel_gp_events[0]);
727b883751aSLike Xu 		report_prefix_push("Intel");
728290f4213SJim Mattson 		set_ref_cycle_expectations();
729b883751aSLike Xu 	} else {
730b883751aSLike Xu 		gp_events_size = sizeof(amd_gp_events)/sizeof(amd_gp_events[0]);
731b883751aSLike Xu 		gp_events = (struct pmu_event *)amd_gp_events;
732b883751aSLike Xu 		report_prefix_push("AMD");
733b883751aSLike Xu 	}
734290f4213SJim Mattson 
735414ee7d1SSean Christopherson 	printf("PMU version:         %d\n", pmu.version);
736414ee7d1SSean Christopherson 	printf("GP counters:         %d\n", pmu.nr_gp_counters);
737414ee7d1SSean Christopherson 	printf("GP counter width:    %d\n", pmu.gp_counter_width);
738414ee7d1SSean Christopherson 	printf("Mask length:         %d\n", pmu.gp_counter_mask_length);
739414ee7d1SSean Christopherson 	printf("Fixed counters:      %d\n", pmu.nr_fixed_counters);
740414ee7d1SSean Christopherson 	printf("Fixed counter width: %d\n", pmu.fixed_counter_width);
7410ef1f6a8SPaolo Bonzini 
7425a2cb3e6SLike Xu 	apic_write(APIC_LVTPC, PMI_VECTOR);
743a9f8b16fSGleb Natapov 
744afa714b2SPaolo Bonzini 	check_counters();
74520cf9147SJim Mattson 
746879e7f07SLike Xu 	if (pmu_has_full_writes()) {
747cda64e80SLike Xu 		pmu.msr_gp_counter_base = MSR_IA32_PMC0;
748cda64e80SLike Xu 
74922f2901aSLike Xu 		report_prefix_push("full-width writes");
75022f2901aSLike Xu 		check_counters();
75122f2901aSLike Xu 		check_gp_counters_write_width();
752d7714e16SLike Xu 		report_prefix_pop();
75322f2901aSLike Xu 	}
754a9f8b16fSGleb Natapov 
755b883751aSLike Xu 	if (!pmu.is_intel) {
756b883751aSLike Xu 		report_prefix_push("K7");
757b883751aSLike Xu 		pmu.nr_gp_counters = AMD64_NUM_COUNTERS;
758b883751aSLike Xu 		pmu.msr_gp_counter_base = MSR_K7_PERFCTR0;
759b883751aSLike Xu 		pmu.msr_gp_event_select_base = MSR_K7_EVNTSEL0;
760b883751aSLike Xu 		check_counters();
761b883751aSLike Xu 		report_prefix_pop();
762b883751aSLike Xu 	}
763b883751aSLike Xu 
764f3cdd159SJan Kiszka 	return report_summary();
765a9f8b16fSGleb Natapov }
766