perf parse-events: Create two hybrid raw events
[linux-2.6-microblaze.git] / tools / perf / util / parse-events-hybrid.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/err.h>
3 #include <linux/zalloc.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <sys/param.h>
9 #include "evlist.h"
10 #include "evsel.h"
11 #include "parse-events.h"
12 #include "parse-events-hybrid.h"
13 #include "debug.h"
14 #include "pmu.h"
15 #include "pmu-hybrid.h"
16 #include "perf.h"
17
18 static void config_hybrid_attr(struct perf_event_attr *attr,
19                                int type, int pmu_type)
20 {
21         /*
22          * attr.config layout for type PERF_TYPE_HARDWARE and
23          * PERF_TYPE_HW_CACHE
24          *
25          * PERF_TYPE_HARDWARE:                 0xEEEEEEEE000000AA
26          *                                     AA: hardware event ID
27          *                                     EEEEEEEE: PMU type ID
28          * PERF_TYPE_HW_CACHE:                 0xEEEEEEEE00DDCCBB
29          *                                     BB: hardware cache ID
30          *                                     CC: hardware cache op ID
31          *                                     DD: hardware cache op result ID
32          *                                     EEEEEEEE: PMU type ID
33          * If the PMU type ID is 0, the PERF_TYPE_RAW will be applied.
34          */
35         attr->type = type;
36         attr->config = attr->config | ((__u64)pmu_type << PERF_PMU_TYPE_SHIFT);
37 }
38
39 static int create_event_hybrid(__u32 config_type, int *idx,
40                                struct list_head *list,
41                                struct perf_event_attr *attr, char *name,
42                                struct list_head *config_terms,
43                                struct perf_pmu *pmu)
44 {
45         struct evsel *evsel;
46         __u32 type = attr->type;
47         __u64 config = attr->config;
48
49         config_hybrid_attr(attr, config_type, pmu->type);
50         evsel = parse_events__add_event_hybrid(list, idx, attr, name,
51                                                pmu, config_terms);
52         if (evsel)
53                 evsel->pmu_name = strdup(pmu->name);
54         else
55                 return -ENOMEM;
56
57         attr->type = type;
58         attr->config = config;
59         return 0;
60 }
61
62 static int add_hw_hybrid(struct parse_events_state *parse_state,
63                          struct list_head *list, struct perf_event_attr *attr,
64                          char *name, struct list_head *config_terms)
65 {
66         struct perf_pmu *pmu;
67         int ret;
68
69         perf_pmu__for_each_hybrid_pmu(pmu) {
70                 ret = create_event_hybrid(PERF_TYPE_HARDWARE,
71                                           &parse_state->idx, list, attr, name,
72                                           config_terms, pmu);
73                 if (ret)
74                         return ret;
75         }
76
77         return 0;
78 }
79
80 static int create_raw_event_hybrid(int *idx, struct list_head *list,
81                                    struct perf_event_attr *attr, char *name,
82                                    struct list_head *config_terms,
83                                    struct perf_pmu *pmu)
84 {
85         struct evsel *evsel;
86
87         attr->type = pmu->type;
88         evsel = parse_events__add_event_hybrid(list, idx, attr, name,
89                                                pmu, config_terms);
90         if (evsel)
91                 evsel->pmu_name = strdup(pmu->name);
92         else
93                 return -ENOMEM;
94
95         return 0;
96 }
97
98 static int add_raw_hybrid(struct parse_events_state *parse_state,
99                           struct list_head *list, struct perf_event_attr *attr,
100                           char *name, struct list_head *config_terms)
101 {
102         struct perf_pmu *pmu;
103         int ret;
104
105         perf_pmu__for_each_hybrid_pmu(pmu) {
106                 ret = create_raw_event_hybrid(&parse_state->idx, list, attr,
107                                               name, config_terms, pmu);
108                 if (ret)
109                         return ret;
110         }
111
112         return 0;
113 }
114
115 int parse_events__add_numeric_hybrid(struct parse_events_state *parse_state,
116                                      struct list_head *list,
117                                      struct perf_event_attr *attr,
118                                      char *name, struct list_head *config_terms,
119                                      bool *hybrid)
120 {
121         *hybrid = false;
122         if (attr->type == PERF_TYPE_SOFTWARE)
123                 return 0;
124
125         if (!perf_pmu__has_hybrid())
126                 return 0;
127
128         *hybrid = true;
129         if (attr->type != PERF_TYPE_RAW) {
130                 return add_hw_hybrid(parse_state, list, attr, name,
131                                      config_terms);
132         }
133
134         return add_raw_hybrid(parse_state, list, attr, name,
135                               config_terms);
136 }
137
138 int parse_events__add_cache_hybrid(struct list_head *list, int *idx,
139                                    struct perf_event_attr *attr, char *name,
140                                    struct list_head *config_terms,
141                                    bool *hybrid)
142 {
143         struct perf_pmu *pmu;
144         int ret;
145
146         *hybrid = false;
147         if (!perf_pmu__has_hybrid())
148                 return 0;
149
150         *hybrid = true;
151         perf_pmu__for_each_hybrid_pmu(pmu) {
152                 ret = create_event_hybrid(PERF_TYPE_HW_CACHE, idx, list,
153                                           attr, name, config_terms, pmu);
154                 if (ret)
155                         return ret;
156         }
157
158         return 0;
159 }