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 <sys/ioctl.h>
9 
10 #include "ebb.h"
11 
12 
13 /*
14  * Test counting multiple events using EBBs.
15  */
16 int multi_counter(void)
17 {
18 	struct event events[6];
19 	int i, group_fd;
20 
21 	SKIP_IF(!ebb_is_supported());
22 
23 	event_init_named(&events[0], 0x1001C, "PM_CMPLU_STALL_THRD");
24 	event_init_named(&events[1], 0x2D016, "PM_CMPLU_STALL_FXU");
25 	event_init_named(&events[2], 0x30006, "PM_CMPLU_STALL_OTHER_CMPL");
26 	event_init_named(&events[3], 0x4000A, "PM_CMPLU_STALL");
27 	event_init_named(&events[4], 0x600f4, "PM_RUN_CYC");
28 	event_init_named(&events[5], 0x500fa, "PM_RUN_INST_CMPL");
29 
30 	event_leader_ebb_init(&events[0]);
31 	for (i = 1; i < 6; i++)
32 		event_ebb_init(&events[i]);
33 
34 	group_fd = -1;
35 	for (i = 0; i < 6; i++) {
36 		events[i].attr.exclude_kernel = 1;
37 		events[i].attr.exclude_hv = 1;
38 		events[i].attr.exclude_idle = 1;
39 
40 		FAIL_IF(event_open_with_group(&events[i], group_fd));
41 		if (group_fd == -1)
42 			group_fd = events[0].fd;
43 	}
44 
45 	ebb_enable_pmc_counting(1);
46 	ebb_enable_pmc_counting(2);
47 	ebb_enable_pmc_counting(3);
48 	ebb_enable_pmc_counting(4);
49 	ebb_enable_pmc_counting(5);
50 	ebb_enable_pmc_counting(6);
51 	setup_ebb_handler(standard_ebb_callee);
52 
53 	FAIL_IF(ioctl(events[0].fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP));
54 	FAIL_IF(event_read(&events[0]));
55 
56 	ebb_global_enable();
57 
58 	mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
59 	mtspr(SPRN_PMC2, pmc_sample_period(sample_period));
60 	mtspr(SPRN_PMC3, pmc_sample_period(sample_period));
61 	mtspr(SPRN_PMC4, pmc_sample_period(sample_period));
62 	mtspr(SPRN_PMC5, pmc_sample_period(sample_period));
63 	mtspr(SPRN_PMC6, pmc_sample_period(sample_period));
64 
65 	while (ebb_state.stats.ebb_count < 50) {
66 		FAIL_IF(core_busy_loop());
67 		FAIL_IF(ebb_check_mmcr0());
68 	}
69 
70 	ebb_global_disable();
71 	ebb_freeze_pmcs();
72 
73 	count_pmc(1, sample_period);
74 	count_pmc(2, sample_period);
75 	count_pmc(3, sample_period);
76 	count_pmc(4, sample_period);
77 	count_pmc(5, sample_period);
78 	count_pmc(6, sample_period);
79 
80 	dump_ebb_state();
81 
82 	for (i = 0; i < 6; i++)
83 		event_close(&events[i]);
84 
85 	FAIL_IF(ebb_state.stats.ebb_count == 0);
86 
87 	return 0;
88 }
89 
90 int main(void)
91 {
92 	return test_harness(multi_counter, "multi_counter");
93 }
94