1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2022, Athira Rajeev, IBM Corp.
4 */
5
6 #include <stdio.h>
7 #include <sys/prctl.h>
8 #include <limits.h>
9 #include "../event.h"
10 #include "../sampling_tests/misc.h"
11
12 /*
13 * Testcase to ensure that using invalid event in generic
14 * event for PERF_TYPE_HARDWARE should fail
15 */
16
generic_events_valid_test(void)17 static int generic_events_valid_test(void)
18 {
19 struct event event;
20
21 /* Check for platform support for the test */
22 SKIP_IF(platform_check_for_tests());
23
24 /* generic events is different in compat_mode */
25 SKIP_IF(check_for_generic_compat_pmu());
26
27 /*
28 * Invalid generic events in power10:
29 * - PERF_COUNT_HW_BUS_CYCLES
30 * - PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
31 * - PERF_COUNT_HW_STALLED_CYCLES_BACKEND
32 * - PERF_COUNT_HW_REF_CPU_CYCLES
33 */
34 if (PVR_VER(mfspr(SPRN_PVR)) == POWER10) {
35 event_init_opts(&event, PERF_COUNT_HW_CPU_CYCLES, PERF_TYPE_HARDWARE, "event");
36 FAIL_IF(event_open(&event));
37 event_close(&event);
38
39 event_init_opts(&event, PERF_COUNT_HW_INSTRUCTIONS,
40 PERF_TYPE_HARDWARE, "event");
41 FAIL_IF(event_open(&event));
42 event_close(&event);
43
44 event_init_opts(&event, PERF_COUNT_HW_CACHE_REFERENCES,
45 PERF_TYPE_HARDWARE, "event");
46 FAIL_IF(event_open(&event));
47 event_close(&event);
48
49 event_init_opts(&event, PERF_COUNT_HW_CACHE_MISSES, PERF_TYPE_HARDWARE, "event");
50 FAIL_IF(event_open(&event));
51 event_close(&event);
52
53 event_init_opts(&event, PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
54 PERF_TYPE_HARDWARE, "event");
55 FAIL_IF(event_open(&event));
56 event_close(&event);
57
58 event_init_opts(&event, PERF_COUNT_HW_BRANCH_MISSES, PERF_TYPE_HARDWARE, "event");
59 FAIL_IF(event_open(&event));
60 event_close(&event);
61
62 event_init_opts(&event, PERF_COUNT_HW_BUS_CYCLES, PERF_TYPE_HARDWARE, "event");
63 FAIL_IF(!event_open(&event));
64
65 event_init_opts(&event, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND,
66 PERF_TYPE_HARDWARE, "event");
67 FAIL_IF(!event_open(&event));
68
69 event_init_opts(&event, PERF_COUNT_HW_STALLED_CYCLES_BACKEND,
70 PERF_TYPE_HARDWARE, "event");
71 FAIL_IF(!event_open(&event));
72
73 event_init_opts(&event, PERF_COUNT_HW_REF_CPU_CYCLES, PERF_TYPE_HARDWARE, "event");
74 FAIL_IF(!event_open(&event));
75 } else if (PVR_VER(mfspr(SPRN_PVR)) == POWER9) {
76 /*
77 * Invalid generic events in power9:
78 * - PERF_COUNT_HW_BUS_CYCLES
79 * - PERF_COUNT_HW_REF_CPU_CYCLES
80 */
81 event_init_opts(&event, PERF_COUNT_HW_CPU_CYCLES, PERF_TYPE_HARDWARE, "event");
82 FAIL_IF(event_open(&event));
83 event_close(&event);
84
85 event_init_opts(&event, PERF_COUNT_HW_INSTRUCTIONS, PERF_TYPE_HARDWARE, "event");
86 FAIL_IF(event_open(&event));
87 event_close(&event);
88
89 event_init_opts(&event, PERF_COUNT_HW_CACHE_REFERENCES,
90 PERF_TYPE_HARDWARE, "event");
91 FAIL_IF(event_open(&event));
92 event_close(&event);
93
94 event_init_opts(&event, PERF_COUNT_HW_CACHE_MISSES, PERF_TYPE_HARDWARE, "event");
95 FAIL_IF(event_open(&event));
96 event_close(&event);
97
98 event_init_opts(&event, PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
99 PERF_TYPE_HARDWARE, "event");
100 FAIL_IF(event_open(&event));
101 event_close(&event);
102
103 event_init_opts(&event, PERF_COUNT_HW_BRANCH_MISSES, PERF_TYPE_HARDWARE, "event");
104 FAIL_IF(event_open(&event));
105 event_close(&event);
106
107 event_init_opts(&event, PERF_COUNT_HW_BUS_CYCLES, PERF_TYPE_HARDWARE, "event");
108 FAIL_IF(!event_open(&event));
109
110 event_init_opts(&event, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND,
111 PERF_TYPE_HARDWARE, "event");
112 FAIL_IF(event_open(&event));
113 event_close(&event);
114
115 event_init_opts(&event, PERF_COUNT_HW_STALLED_CYCLES_BACKEND,
116 PERF_TYPE_HARDWARE, "event");
117 FAIL_IF(event_open(&event));
118 event_close(&event);
119
120 event_init_opts(&event, PERF_COUNT_HW_REF_CPU_CYCLES, PERF_TYPE_HARDWARE, "event");
121 FAIL_IF(!event_open(&event));
122 }
123
124 return 0;
125 }
126
main(void)127 int main(void)
128 {
129 return test_harness(generic_events_valid_test, "generic_events_valid_test");
130 }
131