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 /*
19  * A perf sampling test for mmcra
20  * field: bhrb_disable.
21  */
mmcra_bhrb_disable_no_branch_test(void)22 static int mmcra_bhrb_disable_no_branch_test(void)
23 {
24 	struct event event;
25 	u64 *intr_regs;
26 
27 	/*
28 	 * Check for platform support for the test.
29 	 * This test is only aplicable on power10
30 	 */
31 	SKIP_IF(check_pvr_for_sampling_tests());
32 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_1));
33 
34 	 /* Init the event for the sampling test */
35 	event_init_sampling(&event, EventCode);
36 	event.attr.sample_regs_intr = platform_extended_mask;
37 	event.attr.exclude_kernel = 1;
38 
39 	FAIL_IF(event_open(&event));
40 	event.mmap_buffer = event_sample_buf_mmap(event.fd, 1);
41 
42 	FAIL_IF(event_enable(&event));
43 
44 	/* workload to make the event overflow */
45 	thirty_two_instruction_loop(10000);
46 
47 	FAIL_IF(event_disable(&event));
48 
49 	intr_regs = get_intr_regs(&event, event.mmap_buffer);
50 
51 	/* Check for intr_regs */
52 	FAIL_IF(!intr_regs);
53 
54 	/* Verify that bhrb_disable bit is set in MMCRA for non-branch samples */
55 	FAIL_IF(!get_mmcra_bhrb_disable(get_reg_value(intr_regs, "MMCRA"), 5));
56 
57 	event_close(&event);
58 	return 0;
59 }
60 
main(void)61 int main(void)
62 {
63 	return test_harness(mmcra_bhrb_disable_no_branch_test, "mmcra_bhrb_disable_no_branch_test");
64 }
65