1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PMU_H
3 #define __PMU_H
4 
5 #include <linux/bitmap.h>
6 #include <linux/compiler.h>
7 #include <linux/perf_event.h>
8 #include <linux/list.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include "parse-events.h"
12 #include "pmu-events/pmu-events.h"
13 #include "map_symbol.h"
14 #include "mem-events.h"
15 
16 struct evsel_config_term;
17 struct perf_cpu_map;
18 struct print_callbacks;
19 
20 enum {
21 	PERF_PMU_FORMAT_VALUE_CONFIG,
22 	PERF_PMU_FORMAT_VALUE_CONFIG1,
23 	PERF_PMU_FORMAT_VALUE_CONFIG2,
24 	PERF_PMU_FORMAT_VALUE_CONFIG3,
25 	PERF_PMU_FORMAT_VALUE_CONFIG_END,
26 };
27 
28 #define PERF_PMU_FORMAT_BITS 64
29 #define MAX_PMU_NAME_LEN 128
30 
31 struct perf_event_attr;
32 
33 struct perf_pmu_caps {
34 	char *name;
35 	char *value;
36 	struct list_head list;
37 };
38 
39 enum {
40 	PERF_PMU_TYPE_PE_START    = 0,
41 	PERF_PMU_TYPE_PE_END      = 0xFFFEFFFF,
42 	PERF_PMU_TYPE_HWMON_START = 0xFFFF0000,
43 	PERF_PMU_TYPE_HWMON_END   = 0xFFFFFFFD,
44 	PERF_PMU_TYPE_TOOL = 0xFFFFFFFE,
45 	PERF_PMU_TYPE_FAKE = 0xFFFFFFFF,
46 };
47 
48 /**
49  * struct perf_pmu
50  */
51 struct perf_pmu {
52 	/** @name: The name of the PMU such as "cpu". */
53 	const char *name;
54 	/**
55 	 * @alias_name: Optional alternate name for the PMU determined in
56 	 * architecture specific code.
57 	 */
58 	char *alias_name;
59 	/**
60 	 * @id: Optional PMU identifier read from
61 	 * <sysfs>/bus/event_source/devices/<name>/identifier.
62 	 */
63 	const char *id;
64 	/**
65 	 * @type: Perf event attributed type value, read from
66 	 * <sysfs>/bus/event_source/devices/<name>/type.
67 	 */
68 	__u32 type;
69 	/**
70 	 * @selectable: Can the PMU name be selected as if it were an event?
71 	 */
72 	bool selectable;
73 	/**
74 	 * @is_core: Is the PMU the core CPU PMU? Determined by the name being
75 	 * "cpu" or by the presence of
76 	 * <sysfs>/bus/event_source/devices/<name>/cpus. There may be >1 core
77 	 * PMU on systems like Intel hybrid.
78 	 */
79 	bool is_core;
80 	/**
81 	 * @is_uncore: Is the PMU not within the CPU core? Determined by the
82 	 * presence of <sysfs>/bus/event_source/devices/<name>/cpumask.
83 	 */
84 	bool is_uncore;
85 	/**
86 	 * @auxtrace: Are events auxiliary events? Determined in architecture
87 	 * specific code.
88 	 */
89 	bool auxtrace;
90 	/**
91 	 * @formats_checked: Only check PMU's formats are valid for
92 	 * perf_event_attr once.
93 	 */
94 	bool formats_checked;
95 	/** @config_masks_present: Are there config format values? */
96 	bool config_masks_present;
97 	/** @config_masks_computed: Set when masks are lazily computed. */
98 	bool config_masks_computed;
99 	/**
100 	 * @max_precise: Number of levels of :ppp precision supported by the
101 	 * PMU, read from
102 	 * <sysfs>/bus/event_source/devices/<name>/caps/max_precise.
103 	 */
104 	int max_precise;
105 	/**
106 	 * @perf_event_attr_init_default: Optional function to default
107 	 * initialize PMU specific parts of the perf_event_attr.
108 	 */
109 	void (*perf_event_attr_init_default)(const struct perf_pmu *pmu,
110 					     struct perf_event_attr *attr);
111 	/**
112 	 * @cpus: Empty or the contents of either of:
113 	 * <sysfs>/bus/event_source/devices/<name>/cpumask.
114 	 * <sysfs>/bus/event_source/devices/<cpu>/cpus.
115 	 */
116 	struct perf_cpu_map *cpus;
117 	/**
118 	 * @format: Holds the contents of files read from
119 	 * <sysfs>/bus/event_source/devices/<name>/format/. The contents specify
120 	 * which event parameter changes what config, config1 or config2 bits.
121 	 */
122 	struct list_head format;
123 	/**
124 	 * @aliases: List of struct perf_pmu_alias. Each alias corresponds to an
125 	 * event read from <sysfs>/bus/event_source/devices/<name>/events/ or
126 	 * from json events in pmu-events.c.
127 	 */
128 	struct list_head aliases;
129 	/**
130 	 * @events_table: The events table for json events in pmu-events.c.
131 	 */
132 	const struct pmu_events_table *events_table;
133 	/** @sysfs_aliases: Number of sysfs aliases loaded. */
134 	uint32_t sysfs_aliases;
135 	/** @cpu_json_aliases: Number of json event aliases loaded specific to the CPUID. */
136 	uint32_t cpu_json_aliases;
137 	/** @sys_json_aliases: Number of json event aliases loaded matching the PMU's identifier. */
138 	uint32_t sys_json_aliases;
139 	/**
140 	 * @cpu_common_json_aliases: Number of json events that overlapped with sysfs when
141 	 * loading all sysfs events.
142 	 */
143 	uint32_t cpu_common_json_aliases;
144 	/** @sysfs_aliases_loaded: Are sysfs aliases loaded from disk? */
145 	bool sysfs_aliases_loaded;
146 	/**
147 	 * @cpu_aliases_added: Have all json events table entries for the PMU
148 	 * been added?
149 	 */
150 	bool cpu_aliases_added;
151 	/** @caps_initialized: Has the list caps been initialized? */
152 	bool caps_initialized;
153 	/** @nr_caps: The length of the list caps. */
154 	u32 nr_caps;
155 	/**
156 	 * @caps: Holds the contents of files read from
157 	 * <sysfs>/bus/event_source/devices/<name>/caps/.
158 	 *
159 	 * The contents are pairs of the filename with the value of its
160 	 * contents, for example, max_precise (see above) may have a value of 3.
161 	 */
162 	struct list_head caps;
163 	/** @list: Element on pmus list in pmu.c. */
164 	struct list_head list;
165 
166 	/**
167 	 * @config_masks: Derived from the PMU's format data, bits that are
168 	 * valid within the config value.
169 	 */
170 	__u64 config_masks[PERF_PMU_FORMAT_VALUE_CONFIG_END];
171 
172 	/**
173 	 * @missing_features: Features to inhibit when events on this PMU are
174 	 * opened.
175 	 */
176 	struct {
177 		/**
178 		 * @exclude_guest: Disables perf_event_attr exclude_guest and
179 		 * exclude_host.
180 		 */
181 		bool exclude_guest;
182 		/**
183 		 * @checked: Are the missing features checked?
184 		 */
185 		bool checked;
186 	} missing_features;
187 
188 	/**
189 	 * @mem_events: List of the supported mem events
190 	 */
191 	struct perf_mem_event *mem_events;
192 };
193 
194 struct perf_pmu_info {
195 	const char *unit;
196 	double scale;
197 	bool per_pkg;
198 	bool snapshot;
199 };
200 
201 struct pmu_event_info {
202 	const struct perf_pmu *pmu;
203 	const char *name;
204 	const char* alias;
205 	const char *scale_unit;
206 	const char *desc;
207 	const char *long_desc;
208 	const char *encoding_desc;
209 	const char *topic;
210 	const char *pmu_name;
211 	const char *event_type_desc;
212 	const char *str;
213 	bool deprecated;
214 };
215 
216 typedef int (*pmu_event_callback)(void *state, struct pmu_event_info *info);
217 typedef int (*pmu_format_callback)(void *state, const char *name, int config,
218 				   const unsigned long *bits);
219 
220 void pmu_add_sys_aliases(struct perf_pmu *pmu);
221 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
222 		     struct parse_events_terms *head_terms,
223 		     bool apply_hardcoded,
224 		     struct parse_events_error *error);
225 int perf_pmu__config_terms(const struct perf_pmu *pmu,
226 			   struct perf_event_attr *attr,
227 			   struct parse_events_terms *terms,
228 			   bool zero, bool apply_hardcoded,
229 			   struct parse_events_error *error);
230 __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name);
231 int perf_pmu__format_type(struct perf_pmu *pmu, const char *name);
232 int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms,
233 			  struct perf_pmu_info *info, bool *rewrote_terms,
234 			  u64 *alternate_hw_config, struct parse_events_error *err);
235 int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb);
236 
237 void perf_pmu_format__set_value(void *format, int config, unsigned long *bits);
238 bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name);
239 int perf_pmu__for_each_format(struct perf_pmu *pmu, void *state, pmu_format_callback cb);
240 
241 bool is_pmu_core(const char *name);
242 bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu);
243 bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu);
244 bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name);
245 size_t perf_pmu__num_events(struct perf_pmu *pmu);
246 int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
247 			     void *state, pmu_event_callback cb);
248 bool perf_pmu__name_wildcard_match(const struct perf_pmu *pmu, const char *to_match);
249 bool perf_pmu__name_no_suffix_match(const struct perf_pmu *pmu, const char *to_match);
250 
251 /**
252  * perf_pmu_is_software - is the PMU a software PMU as in it uses the
253  *                        perf_sw_context in the kernel?
254  */
255 bool perf_pmu__is_software(const struct perf_pmu *pmu);
256 
257 FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name);
258 FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name);
259 
260 int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt, ...)
261 	__scanf(3, 4);
262 int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name,
263 			   const char *fmt, ...) __scanf(4, 5);
264 
265 bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name);
266 
267 int perf_pmu__test(void);
268 
269 void perf_pmu__arch_init(struct perf_pmu *pmu);
270 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu,
271 			       const struct pmu_events_table *table);
272 
273 bool pmu_uncore_identifier_match(const char *compat, const char *id);
274 
275 int perf_pmu__convert_scale(const char *scale, char **end, double *sval);
276 
277 int perf_pmu__caps_parse(struct perf_pmu *pmu);
278 
279 void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
280 				   const char *name, int config_num,
281 				   const char *config_name);
282 void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu);
283 
284 bool perf_pmu__wildcard_match(const struct perf_pmu *pmu, const char *wildcard_to_match);
285 
286 int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size);
287 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
288 				 const char *pmu_name, const char *filename);
289 int perf_pmu__event_source_devices_fd(void);
290 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags);
291 
292 struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name,
293 				  bool eager_load);
294 struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
295 void perf_pmu__delete(struct perf_pmu *pmu);
296 struct perf_pmu *perf_pmus__find_core_pmu(void);
297 
298 const char *perf_pmu__name_from_config(struct perf_pmu *pmu, u64 config);
299 bool perf_pmu__is_fake(const struct perf_pmu *pmu);
300 
301 #endif /* __PMU_H */
302