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