1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2022, Athira Rajeev, 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 #define MALLOC_SIZE     (0x10000 * 10)  /* Ought to be enough .. */
14 
15 /* The data cache was reloaded from local core's L3 due to a demand load */
16 #define EventCode 0x21c040
17 
18 /*
19  * A perf sampling test for mmcr1
20  * fields : pmcxsel, unit, cache.
21  */
22 static int mmcr1_sel_unit_cache(void)
23 {
24 	struct event event;
25 	u64 *intr_regs;
26 	char *p;
27 	int i;
28 
29 	/* Check for platform support for the test */
30 	SKIP_IF(check_pvr_for_sampling_tests());
31 
32 	p = malloc(MALLOC_SIZE);
33 	FAIL_IF(!p);
34 
35 	/* Init the event for the sampling test */
36 	event_init_sampling(&event, EventCode);
37 	event.attr.sample_regs_intr = platform_extended_mask;
38 	event.attr.sample_period = 1;
39 	FAIL_IF(event_open(&event));
40 	event.mmap_buffer = event_sample_buf_mmap(event.fd, 1);
41 
42 	event_enable(&event);
43 
44 	/* workload to make the event overflow */
45 	for (i = 0; i < MALLOC_SIZE; i += 0x10000)
46 		p[i] = i;
47 
48 	event_disable(&event);
49 
50 	/* Check for sample count */
51 	FAIL_IF(!collect_samples(event.mmap_buffer));
52 
53 	intr_regs = get_intr_regs(&event, event.mmap_buffer);
54 
55 	/* Check for intr_regs */
56 	FAIL_IF(!intr_regs);
57 
58 	/*
59 	 * Verify that  pmcxsel, unit and cache field of MMCR1
60 	 * match with corresponding event code fields
61 	 */
62 	FAIL_IF(EV_CODE_EXTRACT(event.attr.config, pmcxsel) !=
63 			get_mmcr1_pmcxsel(get_reg_value(intr_regs, "MMCR1"), 1));
64 	FAIL_IF(EV_CODE_EXTRACT(event.attr.config, unit) !=
65 			get_mmcr1_unit(get_reg_value(intr_regs, "MMCR1"), 1));
66 	FAIL_IF(EV_CODE_EXTRACT(event.attr.config, cache) !=
67 			get_mmcr1_cache(get_reg_value(intr_regs, "MMCR1"), 1));
68 
69 	free(p);
70 	event_close(&event);
71 	return 0;
72 }
73 
74 int main(void)
75 {
76 	FAIL_IF(test_harness(mmcr1_sel_unit_cache, "mmcr1_sel_unit_cache"));
77 }
78