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