xref: /linux/tools/testing/selftests/powerpc/pmu/sampling_tests/mmcra_bhrb_cond_test.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2022, Kajol Jain, IBM Corp.
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #include "../event.h"
10 #include "misc.h"
11 #include "utils.h"
12 
13 extern void thirty_two_instruction_loop(int loops);
14 
15 /* Instructions */
16 #define EventCode 0x500fa
17 
18 /* ifm field for conditional branch mode */
19 #define IFM_COND_BRANCH 0x3
20 
21 /*
22  * A perf sampling test for mmcra
23  * field: ifm for bhrb cond call.
24  */
mmcra_bhrb_cond_test(void)25 static int mmcra_bhrb_cond_test(void)
26 {
27 	struct event event;
28 	u64 *intr_regs;
29 
30 	/*
31 	 * Check for platform support for the test.
32 	 * This test is only aplicable on power10
33 	 */
34 	SKIP_IF(check_pvr_for_sampling_tests());
35 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_1));
36 
37 	 /* Init the event for the sampling test */
38 	event_init_sampling(&event, EventCode);
39 	event.attr.sample_regs_intr = platform_extended_mask;
40 	event.attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
41 	event.attr.branch_sample_type = PERF_SAMPLE_BRANCH_COND;
42 	event.attr.exclude_kernel = 1;
43 
44 	FAIL_IF(event_open(&event));
45 	event.mmap_buffer = event_sample_buf_mmap(event.fd, 1);
46 
47 	FAIL_IF(event_enable(&event));
48 
49 	/* workload to make the event overflow */
50 	thirty_two_instruction_loop(10000);
51 
52 	FAIL_IF(event_disable(&event));
53 
54 	intr_regs = get_intr_regs(&event, event.mmap_buffer);
55 
56 	/* Check for intr_regs */
57 	FAIL_IF(!intr_regs);
58 
59 	/* Verify that ifm bit is set properly in MMCRA */
60 	FAIL_IF(get_mmcra_ifm(get_reg_value(intr_regs, "MMCRA"), 5) != IFM_COND_BRANCH);
61 
62 	event_close(&event);
63 	return 0;
64 }
65 
main(void)66 int main(void)
67 {
68 	return test_harness(mmcra_bhrb_cond_test, "mmcra_bhrb_cond_test");
69 }
70