xref: /linux/drivers/gpu/drm/xe/xe_pmu.c (revision fb7399cf2d0b33825b8039f95c45395c7deba25c)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2025 Intel Corporation
4  */
5 
6 #include <drm/drm_drv.h>
7 #include <linux/device.h>
8 
9 #include "xe_device.h"
10 #include "xe_force_wake.h"
11 #include "xe_gt_idle.h"
12 #include "xe_guc_engine_activity.h"
13 #include "xe_guc_pc.h"
14 #include "xe_hw_engine.h"
15 #include "xe_pm.h"
16 #include "xe_pmu.h"
17 #include "xe_sriov_pf_helpers.h"
18 
19 /**
20  * DOC: Xe PMU (Performance Monitoring Unit)
21  *
22  * Expose events/counters like GT-C6 residency, GT frequency and per-class-engine
23  * activity to user land via the perf interface. Events are per device.
24  *
25  * All events are listed in sysfs:
26  *
27  *     $ ls -ld /sys/bus/event_source/devices/xe_*
28  *     $ ls /sys/bus/event_source/devices/xe_0000_00_02.0/events/
29  *     $ ls /sys/bus/event_source/devices/xe_0000_00_02.0/format/
30  *
31  * The following format parameters are available to read events,
32  * but only few are valid with each event:
33  *
34  *	gt[60:63]		Selects gt for the event
35  *	engine_class[20:27]	Selects engine-class for event
36  *	engine_instance[12:19]	Selects the engine-instance for the event
37  *	function[44:59]		Selects the function of the event (SRIOV enabled)
38  *
39  * For engine specific events (engine-*), gt, engine_class and engine_instance parameters must be
40  * set as populated by DRM_XE_DEVICE_QUERY_ENGINES and function if SRIOV is enabled.
41  *
42  * For gt specific events (gt-*) gt parameter must be passed. All other parameters will be 0.
43  *
44  * The standard perf tool can be used to grep for a certain event as well.
45  * Example:
46  *
47  *     $ perf list | grep gt-c6
48  *
49  * To sample a specific event for a GT at regular intervals:
50  *
51  *     $ perf stat -e <event_name,gt=> -I <interval>
52  */
53 
54 #define XE_PMU_EVENT_GT_MASK			GENMASK_ULL(63, 60)
55 #define XE_PMU_EVENT_FUNCTION_MASK		GENMASK_ULL(59, 44)
56 #define XE_PMU_EVENT_ENGINE_CLASS_MASK		GENMASK_ULL(27, 20)
57 #define XE_PMU_EVENT_ENGINE_INSTANCE_MASK	GENMASK_ULL(19, 12)
58 #define XE_PMU_EVENT_ID_MASK			GENMASK_ULL(11, 0)
59 
60 static unsigned int config_to_event_id(u64 config)
61 {
62 	return FIELD_GET(XE_PMU_EVENT_ID_MASK, config);
63 }
64 
65 static unsigned int config_to_function_id(u64 config)
66 {
67 	return FIELD_GET(XE_PMU_EVENT_FUNCTION_MASK, config);
68 }
69 
70 static unsigned int config_to_engine_class(u64 config)
71 {
72 	return FIELD_GET(XE_PMU_EVENT_ENGINE_CLASS_MASK, config);
73 }
74 
75 static unsigned int config_to_engine_instance(u64 config)
76 {
77 	return FIELD_GET(XE_PMU_EVENT_ENGINE_INSTANCE_MASK, config);
78 }
79 
80 static unsigned int config_to_gt_id(u64 config)
81 {
82 	return FIELD_GET(XE_PMU_EVENT_GT_MASK, config);
83 }
84 
85 #define XE_PMU_EVENT_GT_C6_RESIDENCY		0x01
86 #define XE_PMU_EVENT_ENGINE_ACTIVE_TICKS	0x02
87 #define XE_PMU_EVENT_ENGINE_TOTAL_TICKS		0x03
88 #define XE_PMU_EVENT_GT_ACTUAL_FREQUENCY	0x04
89 #define XE_PMU_EVENT_GT_REQUESTED_FREQUENCY	0x05
90 
91 static struct xe_gt *event_to_gt(struct perf_event *event)
92 {
93 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
94 	u64 gt = config_to_gt_id(event->attr.config);
95 
96 	return xe_device_get_gt(xe, gt);
97 }
98 
99 static struct xe_hw_engine *event_to_hwe(struct perf_event *event)
100 {
101 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
102 	struct drm_xe_engine_class_instance eci;
103 	u64 config = event->attr.config;
104 	struct xe_hw_engine *hwe;
105 
106 	eci.engine_class = config_to_engine_class(config);
107 	eci.engine_instance = config_to_engine_instance(config);
108 	eci.gt_id = config_to_gt_id(config);
109 
110 	hwe = xe_hw_engine_lookup(xe, eci);
111 	if (!hwe || xe_hw_engine_is_reserved(hwe))
112 		return NULL;
113 
114 	return hwe;
115 }
116 
117 static bool is_engine_event(u64 config)
118 {
119 	unsigned int event_id = config_to_event_id(config);
120 
121 	return (event_id == XE_PMU_EVENT_ENGINE_TOTAL_TICKS ||
122 		event_id == XE_PMU_EVENT_ENGINE_ACTIVE_TICKS);
123 }
124 
125 static bool is_gt_frequency_event(struct perf_event *event)
126 {
127 	u32 id = config_to_event_id(event->attr.config);
128 
129 	return id == XE_PMU_EVENT_GT_ACTUAL_FREQUENCY ||
130 	       id == XE_PMU_EVENT_GT_REQUESTED_FREQUENCY;
131 }
132 
133 static bool event_gt_forcewake(struct perf_event *event)
134 {
135 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
136 	u64 config = event->attr.config;
137 	struct xe_gt *gt;
138 	unsigned int *fw_ref;
139 
140 	if (!is_engine_event(config) && !is_gt_frequency_event(event))
141 		return true;
142 
143 	gt = xe_device_get_gt(xe, config_to_gt_id(config));
144 
145 	fw_ref = kzalloc(sizeof(*fw_ref), GFP_KERNEL);
146 	if (!fw_ref)
147 		return false;
148 
149 	*fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
150 	if (!*fw_ref) {
151 		kfree(fw_ref);
152 		return false;
153 	}
154 
155 	event->pmu_private = fw_ref;
156 
157 	return true;
158 }
159 
160 static bool event_supported(struct xe_pmu *pmu, unsigned int gt,
161 			    unsigned int id)
162 {
163 	if (gt >= XE_MAX_GT_PER_TILE)
164 		return false;
165 
166 	return id < sizeof(pmu->supported_events) * BITS_PER_BYTE &&
167 		pmu->supported_events & BIT_ULL(id);
168 }
169 
170 static bool event_param_valid(struct perf_event *event)
171 {
172 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
173 	unsigned int engine_class, engine_instance, function_id;
174 	u64 config = event->attr.config;
175 	struct xe_gt *gt;
176 
177 	gt = xe_device_get_gt(xe, config_to_gt_id(config));
178 	if (!gt)
179 		return false;
180 
181 	engine_class = config_to_engine_class(config);
182 	engine_instance = config_to_engine_instance(config);
183 	function_id = config_to_function_id(config);
184 
185 	switch (config_to_event_id(config)) {
186 	case XE_PMU_EVENT_GT_C6_RESIDENCY:
187 	case XE_PMU_EVENT_GT_ACTUAL_FREQUENCY:
188 	case XE_PMU_EVENT_GT_REQUESTED_FREQUENCY:
189 		if (engine_class || engine_instance || function_id)
190 			return false;
191 		break;
192 	case XE_PMU_EVENT_ENGINE_ACTIVE_TICKS:
193 	case XE_PMU_EVENT_ENGINE_TOTAL_TICKS:
194 		if (!event_to_hwe(event))
195 			return false;
196 
197 		/* PF(0) and total vfs when SRIOV is enabled */
198 		if (IS_SRIOV_PF(xe)) {
199 			if (function_id > xe_sriov_pf_get_totalvfs(xe))
200 				return false;
201 		} else if (function_id) {
202 			return false;
203 		}
204 
205 		break;
206 	}
207 
208 	return true;
209 }
210 
211 static void xe_pmu_event_destroy(struct perf_event *event)
212 {
213 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
214 	struct xe_gt *gt;
215 	unsigned int *fw_ref = event->pmu_private;
216 
217 	if (fw_ref) {
218 		gt = xe_device_get_gt(xe, config_to_gt_id(event->attr.config));
219 		xe_force_wake_put(gt_to_fw(gt), *fw_ref);
220 		kfree(fw_ref);
221 		event->pmu_private = NULL;
222 	}
223 
224 	drm_WARN_ON(&xe->drm, event->parent);
225 	xe_pm_runtime_put(xe);
226 	drm_dev_put(&xe->drm);
227 }
228 
229 static int xe_pmu_event_init(struct perf_event *event)
230 {
231 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
232 	struct xe_pmu *pmu = &xe->pmu;
233 	unsigned int id, gt;
234 
235 	if (!pmu->registered)
236 		return -ENODEV;
237 
238 	if (event->attr.type != event->pmu->type)
239 		return -ENOENT;
240 
241 	/* unsupported modes and filters */
242 	if (event->attr.sample_period) /* no sampling */
243 		return -EINVAL;
244 
245 	if (event->cpu < 0)
246 		return -EINVAL;
247 
248 	gt = config_to_gt_id(event->attr.config);
249 	id = config_to_event_id(event->attr.config);
250 	if (!event_supported(pmu, gt, id))
251 		return -ENOENT;
252 
253 	if (has_branch_stack(event))
254 		return -EOPNOTSUPP;
255 
256 	if (!event_param_valid(event))
257 		return -ENOENT;
258 
259 	if (!event->parent) {
260 		drm_dev_get(&xe->drm);
261 		xe_pm_runtime_get(xe);
262 		if (!event_gt_forcewake(event)) {
263 			xe_pm_runtime_put(xe);
264 			drm_dev_put(&xe->drm);
265 			return -EINVAL;
266 		}
267 		event->destroy = xe_pmu_event_destroy;
268 	}
269 
270 	return 0;
271 }
272 
273 static u64 read_engine_events(struct xe_gt *gt, struct perf_event *event)
274 {
275 	struct xe_hw_engine *hwe;
276 	unsigned int function_id;
277 	u64 config, val = 0;
278 
279 	config = event->attr.config;
280 	function_id = config_to_function_id(config);
281 
282 	hwe = event_to_hwe(event);
283 	if (config_to_event_id(config) == XE_PMU_EVENT_ENGINE_ACTIVE_TICKS)
284 		val = xe_guc_engine_activity_active_ticks(&gt->uc.guc, hwe, function_id);
285 	else
286 		val = xe_guc_engine_activity_total_ticks(&gt->uc.guc, hwe, function_id);
287 
288 	return val;
289 }
290 
291 static u64 __xe_pmu_event_read(struct perf_event *event)
292 {
293 	struct xe_gt *gt = event_to_gt(event);
294 
295 	if (!gt)
296 		return 0;
297 
298 	switch (config_to_event_id(event->attr.config)) {
299 	case XE_PMU_EVENT_GT_C6_RESIDENCY:
300 		return xe_gt_idle_residency_msec(&gt->gtidle);
301 	case XE_PMU_EVENT_ENGINE_ACTIVE_TICKS:
302 	case XE_PMU_EVENT_ENGINE_TOTAL_TICKS:
303 		return read_engine_events(gt, event);
304 	case XE_PMU_EVENT_GT_ACTUAL_FREQUENCY:
305 		return xe_guc_pc_get_act_freq(&gt->uc.guc.pc);
306 	case XE_PMU_EVENT_GT_REQUESTED_FREQUENCY:
307 		return xe_guc_pc_get_cur_freq_fw(&gt->uc.guc.pc);
308 	}
309 
310 	return 0;
311 }
312 
313 static void xe_pmu_event_update(struct perf_event *event)
314 {
315 	struct hw_perf_event *hwc = &event->hw;
316 	u64 prev, new;
317 
318 	prev = local64_read(&hwc->prev_count);
319 	do {
320 		new = __xe_pmu_event_read(event);
321 	} while (!local64_try_cmpxchg(&hwc->prev_count, &prev, new));
322 
323 	/*
324 	 * GT frequency is not a monotonically increasing counter, so add the
325 	 * instantaneous value instead.
326 	 */
327 	if (is_gt_frequency_event(event))
328 		local64_add(new, &event->count);
329 	else
330 		local64_add(new - prev, &event->count);
331 }
332 
333 static void xe_pmu_event_read(struct perf_event *event)
334 {
335 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
336 	struct xe_pmu *pmu = &xe->pmu;
337 
338 	if (!pmu->registered) {
339 		event->hw.state = PERF_HES_STOPPED;
340 		return;
341 	}
342 
343 	xe_pmu_event_update(event);
344 }
345 
346 static void xe_pmu_enable(struct perf_event *event)
347 {
348 	/*
349 	 * Store the current counter value so we can report the correct delta
350 	 * for all listeners. Even when the event was already enabled and has
351 	 * an existing non-zero value.
352 	 */
353 	local64_set(&event->hw.prev_count, __xe_pmu_event_read(event));
354 }
355 
356 static void xe_pmu_event_start(struct perf_event *event, int flags)
357 {
358 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
359 	struct xe_pmu *pmu = &xe->pmu;
360 
361 	if (!pmu->registered)
362 		return;
363 
364 	xe_pmu_enable(event);
365 	event->hw.state = 0;
366 }
367 
368 static void xe_pmu_event_stop(struct perf_event *event, int flags)
369 {
370 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
371 	struct xe_pmu *pmu = &xe->pmu;
372 
373 	if (pmu->registered)
374 		if (flags & PERF_EF_UPDATE)
375 			xe_pmu_event_update(event);
376 
377 	event->hw.state = PERF_HES_STOPPED;
378 }
379 
380 static int xe_pmu_event_add(struct perf_event *event, int flags)
381 {
382 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
383 	struct xe_pmu *pmu = &xe->pmu;
384 
385 	if (!pmu->registered)
386 		return -ENODEV;
387 
388 	if (flags & PERF_EF_START)
389 		xe_pmu_event_start(event, flags);
390 
391 	return 0;
392 }
393 
394 static void xe_pmu_event_del(struct perf_event *event, int flags)
395 {
396 	xe_pmu_event_stop(event, PERF_EF_UPDATE);
397 }
398 
399 PMU_FORMAT_ATTR(gt,			"config:60-63");
400 PMU_FORMAT_ATTR(function,		"config:44-59");
401 PMU_FORMAT_ATTR(engine_class,		"config:20-27");
402 PMU_FORMAT_ATTR(engine_instance,	"config:12-19");
403 PMU_FORMAT_ATTR(event,			"config:0-11");
404 
405 static struct attribute *pmu_format_attrs[] = {
406 	&format_attr_event.attr,
407 	&format_attr_engine_class.attr,
408 	&format_attr_engine_instance.attr,
409 	&format_attr_function.attr,
410 	&format_attr_gt.attr,
411 	NULL,
412 };
413 
414 static const struct attribute_group pmu_format_attr_group = {
415 	.name = "format",
416 	.attrs = pmu_format_attrs,
417 };
418 
419 static ssize_t event_attr_show(struct device *dev,
420 			       struct device_attribute *attr, char *buf)
421 {
422 	struct perf_pmu_events_attr *pmu_attr =
423 		container_of(attr, struct perf_pmu_events_attr, attr);
424 
425 	return sprintf(buf, "event=%#04llx\n", pmu_attr->id);
426 }
427 
428 #define XE_EVENT_ATTR(name_, v_, id_)					\
429 	PMU_EVENT_ATTR(name_, pmu_event_ ## v_, id_, event_attr_show)
430 
431 #define XE_EVENT_ATTR_UNIT(name_, v_, unit_)				\
432 	PMU_EVENT_ATTR_STRING(name_.unit, pmu_event_unit_ ## v_, unit_)
433 
434 #define XE_EVENT_ATTR_GROUP(v_, id_, ...)				\
435 	static struct attribute *pmu_attr_ ##v_[] = {			\
436 		__VA_ARGS__,						\
437 		NULL							\
438 	};								\
439 	static umode_t is_visible_##v_(struct kobject *kobj,		\
440 				       struct attribute *attr, int idx) \
441 	{								\
442 		struct perf_pmu_events_attr *pmu_attr;			\
443 		struct xe_pmu *pmu;					\
444 									\
445 		pmu_attr = container_of(attr, typeof(*pmu_attr), attr.attr); \
446 		pmu = container_of(dev_get_drvdata(kobj_to_dev(kobj)),	\
447 				   typeof(*pmu), base);			\
448 									\
449 		return event_supported(pmu, 0, id_) ? attr->mode : 0;	\
450 	}								\
451 	static const struct attribute_group pmu_group_ ##v_ = {		\
452 		.name = "events",					\
453 		.attrs = pmu_attr_ ## v_,				\
454 		.is_visible = is_visible_ ## v_,			\
455 	}
456 
457 #define XE_EVENT_ATTR_SIMPLE(name_, v_, id_, unit_)			\
458 	XE_EVENT_ATTR(name_, v_, id_)					\
459 	XE_EVENT_ATTR_UNIT(name_, v_, unit_)				\
460 	XE_EVENT_ATTR_GROUP(v_, id_, &pmu_event_ ##v_.attr.attr,	\
461 			    &pmu_event_unit_ ##v_.attr.attr)
462 
463 #define XE_EVENT_ATTR_NOUNIT(name_, v_, id_)				\
464 	XE_EVENT_ATTR(name_, v_, id_)					\
465 	XE_EVENT_ATTR_GROUP(v_, id_, &pmu_event_ ##v_.attr.attr)
466 
467 XE_EVENT_ATTR_SIMPLE(gt-c6-residency, gt_c6_residency, XE_PMU_EVENT_GT_C6_RESIDENCY, "ms");
468 XE_EVENT_ATTR_NOUNIT(engine-active-ticks, engine_active_ticks, XE_PMU_EVENT_ENGINE_ACTIVE_TICKS);
469 XE_EVENT_ATTR_NOUNIT(engine-total-ticks, engine_total_ticks, XE_PMU_EVENT_ENGINE_TOTAL_TICKS);
470 XE_EVENT_ATTR_SIMPLE(gt-actual-frequency, gt_actual_frequency,
471 		     XE_PMU_EVENT_GT_ACTUAL_FREQUENCY, "MHz");
472 XE_EVENT_ATTR_SIMPLE(gt-requested-frequency, gt_requested_frequency,
473 		     XE_PMU_EVENT_GT_REQUESTED_FREQUENCY, "MHz");
474 
475 static struct attribute *pmu_empty_event_attrs[] = {
476 	/* Empty - all events are added as groups with .attr_update() */
477 	NULL,
478 };
479 
480 static const struct attribute_group pmu_events_attr_group = {
481 	.name = "events",
482 	.attrs = pmu_empty_event_attrs,
483 };
484 
485 static const struct attribute_group *pmu_events_attr_update[] = {
486 	&pmu_group_gt_c6_residency,
487 	&pmu_group_engine_active_ticks,
488 	&pmu_group_engine_total_ticks,
489 	&pmu_group_gt_actual_frequency,
490 	&pmu_group_gt_requested_frequency,
491 	NULL,
492 };
493 
494 static void set_supported_events(struct xe_pmu *pmu)
495 {
496 	struct xe_device *xe = container_of(pmu, typeof(*xe), pmu);
497 	struct xe_gt *gt = xe_device_get_gt(xe, 0);
498 
499 	if (!xe->info.skip_guc_pc) {
500 		pmu->supported_events |= BIT_ULL(XE_PMU_EVENT_GT_C6_RESIDENCY);
501 		pmu->supported_events |= BIT_ULL(XE_PMU_EVENT_GT_ACTUAL_FREQUENCY);
502 		pmu->supported_events |= BIT_ULL(XE_PMU_EVENT_GT_REQUESTED_FREQUENCY);
503 	}
504 
505 	if (xe_guc_engine_activity_supported(&gt->uc.guc)) {
506 		pmu->supported_events |= BIT_ULL(XE_PMU_EVENT_ENGINE_ACTIVE_TICKS);
507 		pmu->supported_events |= BIT_ULL(XE_PMU_EVENT_ENGINE_TOTAL_TICKS);
508 	}
509 }
510 
511 /**
512  * xe_pmu_unregister() - Remove/cleanup PMU registration
513  * @arg: Ptr to pmu
514  */
515 static void xe_pmu_unregister(void *arg)
516 {
517 	struct xe_pmu *pmu = arg;
518 	struct xe_device *xe = container_of(pmu, typeof(*xe), pmu);
519 
520 	if (!pmu->registered)
521 		return;
522 
523 	pmu->registered = false;
524 
525 	perf_pmu_unregister(&pmu->base);
526 	kfree(pmu->name);
527 }
528 
529 /**
530  * xe_pmu_register() - Define basic PMU properties for Xe and add event callbacks.
531  * @pmu: the PMU object
532  *
533  * Returns 0 on success and an appropriate error code otherwise
534  */
535 int xe_pmu_register(struct xe_pmu *pmu)
536 {
537 	struct xe_device *xe = container_of(pmu, typeof(*xe), pmu);
538 	static const struct attribute_group *attr_groups[] = {
539 		&pmu_format_attr_group,
540 		&pmu_events_attr_group,
541 		NULL
542 	};
543 	int ret = -ENOMEM;
544 	char *name;
545 
546 	BUILD_BUG_ON(XE_MAX_GT_PER_TILE != XE_PMU_MAX_GT);
547 
548 	if (IS_SRIOV_VF(xe))
549 		return 0;
550 
551 	name = kasprintf(GFP_KERNEL, "xe_%s",
552 			 dev_name(xe->drm.dev));
553 	if (!name)
554 		goto err;
555 
556 	/* tools/perf reserves colons as special. */
557 	strreplace(name, ':', '_');
558 
559 	pmu->name		= name;
560 	pmu->base.attr_groups	= attr_groups;
561 	pmu->base.attr_update	= pmu_events_attr_update;
562 	pmu->base.scope		= PERF_PMU_SCOPE_SYS_WIDE;
563 	pmu->base.module	= THIS_MODULE;
564 	pmu->base.task_ctx_nr	= perf_invalid_context;
565 	pmu->base.event_init	= xe_pmu_event_init;
566 	pmu->base.add		= xe_pmu_event_add;
567 	pmu->base.del		= xe_pmu_event_del;
568 	pmu->base.start		= xe_pmu_event_start;
569 	pmu->base.stop		= xe_pmu_event_stop;
570 	pmu->base.read		= xe_pmu_event_read;
571 
572 	set_supported_events(pmu);
573 
574 	ret = perf_pmu_register(&pmu->base, pmu->name, -1);
575 	if (ret)
576 		goto err_name;
577 
578 	pmu->registered = true;
579 
580 	return devm_add_action_or_reset(xe->drm.dev, xe_pmu_unregister, pmu);
581 
582 err_name:
583 	kfree(name);
584 err:
585 	drm_err(&xe->drm, "Failed to register PMU (ret=%d)!\n", ret);
586 
587 	return ret;
588 }
589