1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2014, Michael Ellerman, IBM Corp.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
9 
10 #include "ebb.h"
11 
12 
13 /*
14  * Test of counting cycles while manipulating the user accessible bits in MMCR2.
15  */
16 
17 /* We use two values because the first freezes PMC1 and so we would get no EBBs */
18 #define MMCR2_EXPECTED_1 0x4020100804020000UL /* (FC1P|FC2P|FC3P|FC4P|FC5P|FC6P) */
19 #define MMCR2_EXPECTED_2 0x0020100804020000UL /* (     FC2P|FC3P|FC4P|FC5P|FC6P) */
20 
21 
22 int cycles_with_mmcr2(void)
23 {
24 	struct event event;
25 	uint64_t val, expected[2], actual;
26 	int i;
27 	bool bad_mmcr2;
28 
29 	SKIP_IF(!ebb_is_supported());
30 
31 	event_init_named(&event, 0x1001e, "cycles");
32 	event_leader_ebb_init(&event);
33 
34 	event.attr.exclude_kernel = 1;
35 	event.attr.exclude_hv = 1;
36 	event.attr.exclude_idle = 1;
37 
38 	FAIL_IF(event_open(&event));
39 
40 	ebb_enable_pmc_counting(1);
41 	setup_ebb_handler(standard_ebb_callee);
42 	ebb_global_enable();
43 
44 	FAIL_IF(ebb_event_enable(&event));
45 
46 	mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
47 
48 	/* XXX Set of MMCR2 must be after enable */
49 	expected[0] = MMCR2_EXPECTED_1;
50 	expected[1] = MMCR2_EXPECTED_2;
51 	i = 0;
52 	bad_mmcr2 = false;
53 	actual = 0;
54 
55 	/* Make sure we loop until we take at least one EBB */
56 	while ((ebb_state.stats.ebb_count < 20 && !bad_mmcr2) ||
57 		ebb_state.stats.ebb_count < 1)
58 	{
59 		mtspr(SPRN_MMCR2, expected[i % 2]);
60 
61 		FAIL_IF(core_busy_loop());
62 
63 		val = mfspr(SPRN_MMCR2);
64 		if (val != expected[i % 2]) {
65 			bad_mmcr2 = true;
66 			actual = val;
67 		}
68 
69 		i++;
70 	}
71 
72 	ebb_global_disable();
73 	ebb_freeze_pmcs();
74 
75 	dump_ebb_state();
76 
77 	event_close(&event);
78 
79 	FAIL_IF(ebb_state.stats.ebb_count == 0);
80 
81 	if (bad_mmcr2)
82 		printf("Bad MMCR2 value seen is 0x%lx\n", actual);
83 
84 	FAIL_IF(bad_mmcr2);
85 
86 	return 0;
87 }
88 
89 int main(void)
90 {
91 	return test_harness(cycles_with_mmcr2, "cycles_with_mmcr2");
92 }
93