Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC 0/2] drm/xe/pmu: Don't let open PMU events block S0ix and GT C6
@ 2026-12-02 20:51 Michal Hlavac
  2026-12-02 20:51 ` [PATCH RFC 1/2] drm/xe/pmu: Release forcewake across system suspend Michal Hlavac
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Michal Hlavac @ 2026-12-02 20:51 UTC (permalink / raw)
  To: Matthew Brost, Thomas Hellström, Rodrigo Vivi
  Cc: intel-xe, Riana Tauro, Vinay Belgaumkar, Umesh Nerlige Ramappa,
	Lucas De Marchi, Michal Hlavac

Engine activity and GT frequency events of the xe PMU take GT forcewake
when they are created and hold it until they are destroyed. A monitoring
tool that keeps such events open keeps the GT out of C6 for as long as
it runs, also across system suspend. ksystemstats in KDE Plasma 6.8 does
that as soon as a GPU sensor is displayed.

On a Dell XPS 14 DA14260 (Panther Lake, suspend-to-idle only) this means
that the platform never enters S0ix: after a 45 s suspend
/sys/power/suspend_stats/last_hw_sleep is 0 and intel_pmc_core reports
"CPU did not enter Package C10". The problem is tracked in [1] and [2].

This series contains two ways to fix it, and I would like to ask which
one you prefer.

Patch 1 is the small fix. It keeps the forcewake, but lets all events of
a GT share one reference and releases it across system suspend. What the
events report does not change. The GT still never enters C6 while events
are open.

Patch 2 removes the forcewake from the PMU. The two registers that need
an awake GT (GPM timestamp, RPNSWREQ) are only used if the GT is in C0
before and after the read. The GT then idles normally with events open,
and the series as a whole removes more lines than it adds. It has two
costs:

 - gt-requested-frequency reads as 0 while the GT is in C6. Until now it
   always returned a request, because the event kept the GT awake.
 - It assumes that these registers read correctly without forcewake
   while the GT reports C0, and that the GT does not go through C6 and
   back to C0 between the two status checks. I could only verify the
   first assumption on Panther Lake.

The patches are stacked so that the series applies as a whole: patch 2
removes what patch 1 adds, and the result of both is the same as patch 2
on its own. If patch 2 is acceptable, patch 1 can be dropped or kept as
the part that is suitable for stable. If it is not, patch 1 stands
alone. I did not add stable tags for that reason.

Testing, all on the machine above with ksystemstats holding the events
open, on v7.3.6 with the patches applied (they apply with line offsets;
the two hooks in xe_pm.c needed adjusting by hand):

                               hw sleep in 45 s   gt-c6 residency,
                               of s2idle          idle desktop
  unpatched                    0 s                does not advance
  patch 1 (forcewake kept)     43.8 s             does not advance
  patches 1 + 2 (no forcewake) 43.7 s             ~9.7 s per 10 s

The last row was measured with patch 2 applied directly to v7.3.6,
which gives the same source as both patches together.

With patch 2 the reported engine utilization is 1-2 % when idle and
about 60 % under glxgears, the same as with patch 1, and
gt-requested-frequency follows gt-actual-frequency under load. On
drm-xe-next the series is compile-tested only (x86_64, W=1).

Not tested: other platforms, discrete GPUs, SR-IOV, S3, and the IGT
xe_pmu tests.

While testing I saw that engine-active-ticks can overshoot and step
back under load. That happens with and without this series, and I will
report it separately.

The analysis, the code and the text of this series were written with
the help of an AI coding assistant (Claude Code, model Claude Fable
5.2). I reviewed the result and ran the hardware tests myself.

[1] https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/9633
[2] https://bugs.kde.org/show_bug.cgi?id=527160

---
Michal Hlavac (2):
      drm/xe/pmu: Release forcewake across system suspend
      drm/xe/pmu: Don't hold forcewake while events are open

 drivers/gpu/drm/xe/xe_guc_engine_activity.c | 34 +++++++++++++-----
 drivers/gpu/drm/xe/xe_guc_pc.c              | 22 ++++++++++++
 drivers/gpu/drm/xe/xe_guc_pc.h              |  1 +
 drivers/gpu/drm/xe/xe_pmu.c                 | 55 +++--------------------------
 4 files changed, 52 insertions(+), 60 deletions(-)
---
base-commit: c6fe3bf08f392a35849e4eedba17ebacdd8d122d
change-id: 20261202-xe-pmu-forcewake-rfc-fc39c68818f1

Best regards,
--  
Michal Hlavac <miso@hlavki.eu>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH RFC 1/2] drm/xe/pmu: Release forcewake across system suspend
  2026-12-02 20:51 [PATCH RFC 0/2] drm/xe/pmu: Don't let open PMU events block S0ix and GT C6 Michal Hlavac
@ 2026-12-02 20:51 ` Michal Hlavac
  2026-12-02 20:51 ` [PATCH RFC 2/2] drm/xe/pmu: Don't hold forcewake while events are open Michal Hlavac
  2026-12-03 14:02 ` ✗ LGCI.VerificationFailed: failure for drm/xe/pmu: Don't let open PMU events block S0ix and GT C6 Patchwork
  2 siblings, 0 replies; 5+ messages in thread
From: Michal Hlavac @ 2026-12-02 20:51 UTC (permalink / raw)
  To: Matthew Brost, Thomas Hellström, Rodrigo Vivi
  Cc: intel-xe, Riana Tauro, Vinay Belgaumkar, Umesh Nerlige Ramappa,
	Lucas De Marchi, Michal Hlavac

Engine activity and GT frequency events take GT forcewake when they are
created and hold it until they are destroyed, because it can't be taken
from the read callback. Nothing drops these references on system
suspend, so a monitoring tool that keeps such events open (for example
ksystemstats in KDE Plasma 6.8) leaves the GT awake for the whole
suspend. On suspend-to-idle the package then never leaves C0 and the
platform never enters S0ix.

On a Dell XPS 14 DA14260 (Panther Lake) with ksystemstats holding
engine and frequency events open, a 45 s s2idle cycle ends with
/sys/power/suspend_stats/last_hw_sleep = 0 and intel_pmc_core reports
"CPU did not enter Package C10". With this change the same cycle
reaches 44 s of hardware sleep.

Let all events on a GT share one forcewake reference instead of taking
one each, drop it at the end of xe_pm_suspend() and take it again in
xe_pm_resume().

Open events still keep the GT out of C6 while the system is running.

Fixes: c7f2b8bfca78 ("drm/xe/xe_pmu: Acquire forcewake on event init for engine events")
Fixes: 89f306dc6dfd ("drm/xe/pmu: Add GT frequency events")
Link: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/9633
Link: https://bugs.kde.org/show_bug.cgi?id=527160
Signed-off-by: Michal Hlavac <miso@hlavki.eu>
---
 drivers/gpu/drm/xe/xe_pm.c        |  13 +++--
 drivers/gpu/drm/xe/xe_pmu.c       | 117 ++++++++++++++++++++++++++++++--------
 drivers/gpu/drm/xe/xe_pmu.h       |   4 ++
 drivers/gpu/drm/xe/xe_pmu_types.h |  17 +++++-
 4 files changed, 121 insertions(+), 30 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c
index f73f68bd4..a0e912e49 100644
--- a/drivers/gpu/drm/xe/xe_pm.c
+++ b/drivers/gpu/drm/xe/xe_pm.c
@@ -24,6 +24,7 @@
 #include "xe_irq.h"
 #include "xe_late_bind_fw.h"
 #include "xe_pcode.h"
+#include "xe_pmu.h"
 #include "xe_printk.h"
 #include "xe_pxp.h"
 #include "xe_sriov_vf_ccs.h"
@@ -194,10 +195,12 @@ int xe_pm_suspend(struct xe_device *xe, bool hibernate)
 	trace_xe_pm_suspend(xe, __builtin_return_address(0));
 
 	err = xe_device_suspend(xe, hibernate, false);
-	if (err)
+	if (err) {
 		drm_dbg(&xe->drm, "Device suspend failed %d\n", err);
-	else
+	} else {
+		xe_pmu_suspend(&xe->pmu);
 		drm_dbg(&xe->drm, "Device suspended\n");
+	}
 
 	xe_pm_block_end_signalling();
 	return err;
@@ -218,10 +221,12 @@ int xe_pm_resume(struct xe_device *xe)
 	trace_xe_pm_resume(xe, __builtin_return_address(0));
 
 	err = xe_device_resume(xe, false);
-	if (err)
+	if (err) {
 		drm_dbg(&xe->drm, "Device resume failed %d\n", err);
-	else
+	} else {
+		xe_pmu_resume(&xe->pmu);
 		drm_dbg(&xe->drm, "Device resumed\n");
+	}
 
 	xe_pm_block_end_signalling();
 	return err;
diff --git a/drivers/gpu/drm/xe/xe_pmu.c b/drivers/gpu/drm/xe/xe_pmu.c
index ce1b110f7..f1f3f4068 100644
--- a/drivers/gpu/drm/xe/xe_pmu.c
+++ b/drivers/gpu/drm/xe/xe_pmu.c
@@ -130,33 +130,59 @@ static bool is_gt_frequency_event(struct perf_event *event)
 	       id == XE_PMU_EVENT_GT_REQUESTED_FREQUENCY;
 }
 
-static bool event_gt_forcewake(struct perf_event *event)
+static bool event_needs_forcewake(struct perf_event *event)
+{
+	return is_engine_event(event->attr.config) || is_gt_frequency_event(event);
+}
+
+/*
+ * Forcewake can't be taken in the read callback, which runs under a raw
+ * spinlock, so it is held for as long as an event that needs it exists. All
+ * such events on a GT share one reference, which xe_pmu_suspend() drops and
+ * xe_pmu_resume() takes again.
+ */
+static bool event_gt_forcewake_get(struct perf_event *event)
 {
 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
-	u64 config = event->attr.config;
-	struct xe_gt *gt;
-	unsigned int *fw_ref;
+	struct xe_gt *gt = event_to_gt(event);
+	struct xe_pmu *pmu = &xe->pmu;
+	u8 id = gt->info.id;
 
-	if (!is_engine_event(config) && !is_gt_frequency_event(event))
+	if (!event_needs_forcewake(event))
 		return true;
 
-	gt = xe_device_get_gt(xe, config_to_gt_id(config));
-
-	fw_ref = kzalloc_obj(*fw_ref);
-	if (!fw_ref)
-		return false;
+	guard(mutex)(&pmu->fw_lock);
 
-	*fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
-	if (!*fw_ref) {
-		kfree(fw_ref);
-		return false;
+	if (!pmu->fw_ref[id]) {
+		pmu->fw_ref[id] = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
+		if (!pmu->fw_ref[id])
+			return false;
 	}
 
-	event->pmu_private = fw_ref;
+	pmu->fw_count[id]++;
 
 	return true;
 }
 
+static void event_gt_forcewake_put(struct perf_event *event)
+{
+	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
+	struct xe_gt *gt = event_to_gt(event);
+	struct xe_pmu *pmu = &xe->pmu;
+	u8 id = gt->info.id;
+
+	if (!event_needs_forcewake(event))
+		return;
+
+	guard(mutex)(&pmu->fw_lock);
+
+	if (--pmu->fw_count[id])
+		return;
+
+	xe_force_wake_put(gt_to_fw(gt), pmu->fw_ref[id]);
+	pmu->fw_ref[id] = 0;
+}
+
 static bool event_supported(struct xe_pmu *pmu, unsigned int gt_id,
 			    unsigned int id)
 {
@@ -214,15 +240,8 @@ static bool event_param_valid(struct perf_event *event)
 static void xe_pmu_event_destroy(struct perf_event *event)
 {
 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
-	struct xe_gt *gt;
-	unsigned int *fw_ref = event->pmu_private;
 
-	if (fw_ref) {
-		gt = xe_device_get_gt(xe, config_to_gt_id(event->attr.config));
-		xe_force_wake_put(gt_to_fw(gt), *fw_ref);
-		kfree(fw_ref);
-		event->pmu_private = NULL;
-	}
+	event_gt_forcewake_put(event);
 
 	drm_WARN_ON(&xe->drm, event->parent);
 	xe_pm_runtime_put(xe);
@@ -262,7 +281,7 @@ static int xe_pmu_event_init(struct perf_event *event)
 	if (!event->parent) {
 		drm_dev_get(&xe->drm);
 		xe_pm_runtime_get(xe);
-		if (!event_gt_forcewake(event)) {
+		if (!event_gt_forcewake_get(event)) {
 			xe_pm_runtime_put(xe);
 			drm_dev_put(&xe->drm);
 			return -EINVAL;
@@ -520,6 +539,52 @@ static void set_supported_events(struct xe_pmu *pmu)
 	}
 }
 
+/**
+ * xe_pmu_suspend() - Drop the forcewake held for PMU events
+ * @pmu: the PMU object
+ *
+ * Called on system suspend, so that events left open by userspace do not keep
+ * the GTs awake while the system sleeps.
+ */
+void xe_pmu_suspend(struct xe_pmu *pmu)
+{
+	struct xe_device *xe = container_of(pmu, typeof(*xe), pmu);
+	struct xe_gt *gt;
+	u8 id;
+
+	if (!pmu->registered)
+		return;
+
+	guard(mutex)(&pmu->fw_lock);
+
+	for_each_gt(gt, xe, id) {
+		xe_force_wake_put(gt_to_fw(gt), pmu->fw_ref[id]);
+		pmu->fw_ref[id] = 0;
+	}
+}
+
+/**
+ * xe_pmu_resume() - Take the forcewake needed by PMU events again
+ * @pmu: the PMU object
+ *
+ * Called on system resume, once the GTs are back up.
+ */
+void xe_pmu_resume(struct xe_pmu *pmu)
+{
+	struct xe_device *xe = container_of(pmu, typeof(*xe), pmu);
+	struct xe_gt *gt;
+	u8 id;
+
+	if (!pmu->registered)
+		return;
+
+	guard(mutex)(&pmu->fw_lock);
+
+	for_each_gt(gt, xe, id)
+		if (pmu->fw_count[id])
+			pmu->fw_ref[id] = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
+}
+
 /**
  * xe_pmu_unregister() - Remove/cleanup PMU registration
  * @arg: Ptr to pmu
@@ -555,11 +620,13 @@ int xe_pmu_register(struct xe_pmu *pmu)
 	int ret = -ENOMEM;
 	char *name;
 
-	BUILD_BUG_ON(XE_MAX_GT_PER_TILE != XE_PMU_MAX_GT);
+	BUILD_BUG_ON(XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE != XE_PMU_MAX_GT);
 
 	if (IS_SRIOV_VF(xe))
 		return 0;
 
+	mutex_init(&pmu->fw_lock);
+
 	name = kasprintf(GFP_KERNEL, "xe_%s",
 			 dev_name(xe->drm.dev));
 	if (!name)
diff --git a/drivers/gpu/drm/xe/xe_pmu.h b/drivers/gpu/drm/xe/xe_pmu.h
index 60c37126f..912e033c1 100644
--- a/drivers/gpu/drm/xe/xe_pmu.h
+++ b/drivers/gpu/drm/xe/xe_pmu.h
@@ -10,8 +10,12 @@
 
 #if IS_ENABLED(CONFIG_PERF_EVENTS)
 int xe_pmu_register(struct xe_pmu *pmu);
+void xe_pmu_suspend(struct xe_pmu *pmu);
+void xe_pmu_resume(struct xe_pmu *pmu);
 #else
 static inline int xe_pmu_register(struct xe_pmu *pmu) { return 0; }
+static inline void xe_pmu_suspend(struct xe_pmu *pmu) {}
+static inline void xe_pmu_resume(struct xe_pmu *pmu) {}
 #endif
 
 #endif
diff --git a/drivers/gpu/drm/xe/xe_pmu_types.h b/drivers/gpu/drm/xe/xe_pmu_types.h
index f5ba4d566..5a38cdf3c 100644
--- a/drivers/gpu/drm/xe/xe_pmu_types.h
+++ b/drivers/gpu/drm/xe/xe_pmu_types.h
@@ -6,10 +6,12 @@
 #ifndef _XE_PMU_TYPES_H_
 #define _XE_PMU_TYPES_H_
 
+#include <linux/mutex_types.h>
 #include <linux/perf_event.h>
 #include <linux/spinlock_types.h>
 
-#define XE_PMU_MAX_GT 2
+/* XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE */
+#define XE_PMU_MAX_GT 4
 
 /**
  * struct xe_pmu - PMU related data per Xe device
@@ -34,6 +36,19 @@ struct xe_pmu {
 	 * @supported_events: Bitmap of supported events, indexed by event id
 	 */
 	u64 supported_events;
+	/**
+	 * @fw_lock: Protects @fw_count and @fw_ref.
+	 */
+	struct mutex fw_lock;
+	/**
+	 * @fw_count: Number of events that need forcewake, per GT.
+	 */
+	unsigned int fw_count[XE_PMU_MAX_GT];
+	/**
+	 * @fw_ref: Forcewake reference shared by those events, per GT. Zero
+	 * while the device is suspended.
+	 */
+	unsigned int fw_ref[XE_PMU_MAX_GT];
 };
 
 #endif

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH RFC 2/2] drm/xe/pmu: Don't hold forcewake while events are open
  2026-12-02 20:51 [PATCH RFC 0/2] drm/xe/pmu: Don't let open PMU events block S0ix and GT C6 Michal Hlavac
  2026-12-02 20:51 ` [PATCH RFC 1/2] drm/xe/pmu: Release forcewake across system suspend Michal Hlavac
@ 2026-12-02 20:51 ` Michal Hlavac
  2026-12-03 13:45   ` sashiko-bot
  2026-12-03 14:02 ` ✗ LGCI.VerificationFailed: failure for drm/xe/pmu: Don't let open PMU events block S0ix and GT C6 Patchwork
  2 siblings, 1 reply; 5+ messages in thread
From: Michal Hlavac @ 2026-12-02 20:51 UTC (permalink / raw)
  To: Matthew Brost, Thomas Hellström, Rodrigo Vivi
  Cc: intel-xe, Riana Tauro, Vinay Belgaumkar, Umesh Nerlige Ramappa,
	Lucas De Marchi, Michal Hlavac

Engine activity and GT frequency events hold GT forcewake for as long
as they are open. A monitoring tool that keeps such events open (for
example ksystemstats in KDE Plasma 6.8) therefore keeps the GT out of
C6 for as long as it runs, and the forcewake has to be dropped and
taken again around system suspend.

The forcewake is there for three register reads in the read callback,
which can't take it itself. None of them needs the GT to be forced
awake:

 - The engine activity counters read the GPM timestamp only while GuC
   reports the engine as running, and then the GT is normally awake.
   The engine can go idle and the GT enter C6 at any time though, so
   only use the timestamp if the GT is in C0 before and after the
   read, and keep the previous value otherwise.
 - The actual frequency is documented to read as 0 in C6 and is
   already read without forcewake by the act_freq sysfs attribute.
 - The requested frequency can be read the same way as the timestamp.
   Report 0 if the GT is not in C0.

With that, drop the forcewake handling from the PMU, including the
suspend and resume hooks. The runtime PM reference held by open events
stays as it is.

This changes what gt-requested-frequency reports: it used to always
return a request, because the event itself kept the GT awake. It now
reads as 0 while the GT is in C6, like gt-actual-frequency does, and
like both frequencies do in i915 while the GT is parked.

Tested on a Dell XPS 14 DA14260 (Panther Lake) with ksystemstats
holding engine and frequency events open. The gt-c6 residency of both
GTs now advances by about 9.7 s per 10 s on an idle desktop, where it
did not advance at all. A 45 s s2idle cycle still reaches 44 s of
hardware sleep. The reported engine utilization is 1-2 % when idle and
about 60 % with glxgears running, before and after a suspend cycle, and
gt-requested-frequency follows gt-actual-frequency under load.

Fixes: c7f2b8bfca78 ("drm/xe/xe_pmu: Acquire forcewake on event init for engine events")
Fixes: 89f306dc6dfd ("drm/xe/pmu: Add GT frequency events")
Link: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/9633
Link: https://bugs.kde.org/show_bug.cgi?id=527160
Signed-off-by: Michal Hlavac <miso@hlavki.eu>
---
 drivers/gpu/drm/xe/xe_guc_engine_activity.c |  34 ++++++--
 drivers/gpu/drm/xe/xe_guc_pc.c              |  22 +++++
 drivers/gpu/drm/xe/xe_guc_pc.h              |   1 +
 drivers/gpu/drm/xe/xe_pm.c                  |  13 +--
 drivers/gpu/drm/xe/xe_pmu.c                 | 124 ++--------------------------
 drivers/gpu/drm/xe/xe_pmu.h                 |   4 -
 drivers/gpu/drm/xe/xe_pmu_types.h           |  17 +---
 7 files changed, 58 insertions(+), 157 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_guc_engine_activity.c b/drivers/gpu/drm/xe/xe_guc_engine_activity.c
index 729ce8ac1..bb978c141 100644
--- a/drivers/gpu/drm/xe/xe_guc_engine_activity.c
+++ b/drivers/gpu/drm/xe/xe_guc_engine_activity.c
@@ -14,6 +14,7 @@
 #include "xe_guc.h"
 #include "xe_guc_engine_activity.h"
 #include "xe_guc_ct.h"
+#include "xe_guc_pc.h"
 #include "xe_hw_engine.h"
 #include "xe_map.h"
 #include "xe_mmio.h"
@@ -170,18 +171,36 @@ static u64 cpu_ns_to_guc_tsc_tick(ktime_t ns, u32 freq)
 #define read_metadata_record(xe_, map_, field_) \
 	xe_map_rd_field(xe_, map_, 0, struct guc_engine_activity_metadata, field_)
 
+/*
+ * The PMU reads the engine activity without forcewake, and the GPM timestamp
+ * can only be read while the GT is awake. The GT is normally awake while an
+ * engine is running, but the engine can go idle and let the GT enter C6 at any
+ * time. Only trust the timestamp if the GT was in C0 before and after reading
+ * it.
+ */
+static bool read_gpm_timestamp(struct xe_guc *guc, u32 *gpm_ts)
+{
+	struct xe_gt *gt = guc_to_gt(guc);
+
+	if (xe_guc_pc_c_status(&guc->pc) != GT_IDLE_C0)
+		return false;
+
+	*gpm_ts = lower_32_bits(xe_mmio_read64_2x32(&gt->mmio, MISC_STATUS_0) >>
+				guc->engine_activity.gpm_timestamp_shift);
+
+	return xe_guc_pc_c_status(&guc->pc) == GT_IDLE_C0;
+}
+
 static u64 get_engine_active_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe,
 				   unsigned int index)
 {
 	struct engine_activity *ea = hw_engine_to_engine_activity(hwe, index);
 	struct guc_engine_activity *cached_activity = &ea->activity;
 	struct guc_engine_activity_metadata *cached_metadata = &ea->metadata;
-	struct xe_guc_engine_activity *engine_activity = &guc->engine_activity;
 	struct iosys_map activity_map, metadata_map;
 	struct xe_device *xe =  guc_to_xe(guc);
-	struct xe_gt *gt = guc_to_gt(guc);
-	u32 last_update_tick, global_change_num;
-	u64 active_ticks, gpm_ts;
+	u32 last_update_tick, global_change_num, gpm_ts;
+	u64 active_ticks;
 	u16 change_num;
 
 	activity_map = engine_activity_map(guc, hwe, index);
@@ -216,11 +235,8 @@ static u64 get_engine_active_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe,
 	cached_activity->active_ticks = active_ticks;
 
 update:
-	if (ea->running) {
-		gpm_ts = xe_mmio_read64_2x32(&gt->mmio, MISC_STATUS_0) >>
-			 engine_activity->gpm_timestamp_shift;
-		ea->active = lower_32_bits(gpm_ts) - cached_activity->last_update_tick;
-	}
+	if (ea->running && read_gpm_timestamp(guc, &gpm_ts))
+		ea->active = gpm_ts - cached_activity->last_update_tick;
 
 	trace_xe_guc_engine_activity(xe, ea, hwe->name, hwe->instance);
 
diff --git a/drivers/gpu/drm/xe/xe_guc_pc.c b/drivers/gpu/drm/xe/xe_guc_pc.c
index 9558d7196..8e37ce22c 100644
--- a/drivers/gpu/drm/xe/xe_guc_pc.c
+++ b/drivers/gpu/drm/xe/xe_guc_pc.c
@@ -515,6 +515,28 @@ u32 xe_guc_pc_get_cur_freq_fw(struct xe_guc_pc *pc)
 	return get_cur_freq(gt);
 }
 
+/**
+ * xe_guc_pc_get_cur_freq_if_awake - Get requested frequency without waking the GT
+ * @pc: The GuC PC
+ *
+ * For callers that can't take forcewake. The request register can only be
+ * read while the GT is awake, so only trust it if the GT was in C0 before and
+ * after the read.
+ *
+ * Returns: the requested frequency, or 0 if the GT is not in C0.
+ */
+u32 xe_guc_pc_get_cur_freq_if_awake(struct xe_guc_pc *pc)
+{
+	u32 freq;
+
+	if (xe_guc_pc_c_status(pc) != GT_IDLE_C0)
+		return 0;
+
+	freq = get_cur_freq(pc_to_gt(pc));
+
+	return xe_guc_pc_c_status(pc) == GT_IDLE_C0 ? freq : 0;
+}
+
 /**
  * xe_guc_pc_get_cur_freq - Get Current requested frequency
  * @pc: The GuC PC
diff --git a/drivers/gpu/drm/xe/xe_guc_pc.h b/drivers/gpu/drm/xe/xe_guc_pc.h
index 1025a2b15..9c4496a1d 100644
--- a/drivers/gpu/drm/xe/xe_guc_pc.h
+++ b/drivers/gpu/drm/xe/xe_guc_pc.h
@@ -21,6 +21,7 @@ int xe_guc_pc_action_unset_param(struct xe_guc_pc *pc, u8 id);
 u32 xe_guc_pc_get_act_freq(struct xe_guc_pc *pc);
 int xe_guc_pc_get_cur_freq(struct xe_guc_pc *pc, u32 *freq);
 u32 xe_guc_pc_get_cur_freq_fw(struct xe_guc_pc *pc);
+u32 xe_guc_pc_get_cur_freq_if_awake(struct xe_guc_pc *pc);
 u32 xe_guc_pc_get_rp0_freq(struct xe_guc_pc *pc);
 u32 xe_guc_pc_get_rpa_freq(struct xe_guc_pc *pc);
 u32 xe_guc_pc_get_rpe_freq(struct xe_guc_pc *pc);
diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c
index a0e912e49..f73f68bd4 100644
--- a/drivers/gpu/drm/xe/xe_pm.c
+++ b/drivers/gpu/drm/xe/xe_pm.c
@@ -24,7 +24,6 @@
 #include "xe_irq.h"
 #include "xe_late_bind_fw.h"
 #include "xe_pcode.h"
-#include "xe_pmu.h"
 #include "xe_printk.h"
 #include "xe_pxp.h"
 #include "xe_sriov_vf_ccs.h"
@@ -195,12 +194,10 @@ int xe_pm_suspend(struct xe_device *xe, bool hibernate)
 	trace_xe_pm_suspend(xe, __builtin_return_address(0));
 
 	err = xe_device_suspend(xe, hibernate, false);
-	if (err) {
+	if (err)
 		drm_dbg(&xe->drm, "Device suspend failed %d\n", err);
-	} else {
-		xe_pmu_suspend(&xe->pmu);
+	else
 		drm_dbg(&xe->drm, "Device suspended\n");
-	}
 
 	xe_pm_block_end_signalling();
 	return err;
@@ -221,12 +218,10 @@ int xe_pm_resume(struct xe_device *xe)
 	trace_xe_pm_resume(xe, __builtin_return_address(0));
 
 	err = xe_device_resume(xe, false);
-	if (err) {
+	if (err)
 		drm_dbg(&xe->drm, "Device resume failed %d\n", err);
-	} else {
-		xe_pmu_resume(&xe->pmu);
+	else
 		drm_dbg(&xe->drm, "Device resumed\n");
-	}
 
 	xe_pm_block_end_signalling();
 	return err;
diff --git a/drivers/gpu/drm/xe/xe_pmu.c b/drivers/gpu/drm/xe/xe_pmu.c
index f1f3f4068..44ae0db64 100644
--- a/drivers/gpu/drm/xe/xe_pmu.c
+++ b/drivers/gpu/drm/xe/xe_pmu.c
@@ -7,7 +7,6 @@
 #include <linux/device.h>
 
 #include "xe_device.h"
-#include "xe_force_wake.h"
 #include "xe_gt_idle.h"
 #include "xe_guc_engine_activity.h"
 #include "xe_guc_pc.h"
@@ -41,6 +40,9 @@
  *
  * For gt specific events (gt-*) gt parameter must be passed. All other parameters will be 0.
  *
+ * Open events don't keep the GT awake. The frequency events (gt-actual-frequency and
+ * gt-requested-frequency) read as 0 while the GT is in C6.
+ *
  * The standard perf tool can be used to grep for a certain event as well.
  * Example:
  *
@@ -114,14 +116,6 @@ static struct xe_hw_engine *event_to_hwe(struct perf_event *event)
 	return hwe;
 }
 
-static bool is_engine_event(u64 config)
-{
-	unsigned int event_id = config_to_event_id(config);
-
-	return (event_id == XE_PMU_EVENT_ENGINE_TOTAL_TICKS ||
-		event_id == XE_PMU_EVENT_ENGINE_ACTIVE_TICKS);
-}
-
 static bool is_gt_frequency_event(struct perf_event *event)
 {
 	u32 id = config_to_event_id(event->attr.config);
@@ -130,59 +124,6 @@ static bool is_gt_frequency_event(struct perf_event *event)
 	       id == XE_PMU_EVENT_GT_REQUESTED_FREQUENCY;
 }
 
-static bool event_needs_forcewake(struct perf_event *event)
-{
-	return is_engine_event(event->attr.config) || is_gt_frequency_event(event);
-}
-
-/*
- * Forcewake can't be taken in the read callback, which runs under a raw
- * spinlock, so it is held for as long as an event that needs it exists. All
- * such events on a GT share one reference, which xe_pmu_suspend() drops and
- * xe_pmu_resume() takes again.
- */
-static bool event_gt_forcewake_get(struct perf_event *event)
-{
-	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
-	struct xe_gt *gt = event_to_gt(event);
-	struct xe_pmu *pmu = &xe->pmu;
-	u8 id = gt->info.id;
-
-	if (!event_needs_forcewake(event))
-		return true;
-
-	guard(mutex)(&pmu->fw_lock);
-
-	if (!pmu->fw_ref[id]) {
-		pmu->fw_ref[id] = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
-		if (!pmu->fw_ref[id])
-			return false;
-	}
-
-	pmu->fw_count[id]++;
-
-	return true;
-}
-
-static void event_gt_forcewake_put(struct perf_event *event)
-{
-	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
-	struct xe_gt *gt = event_to_gt(event);
-	struct xe_pmu *pmu = &xe->pmu;
-	u8 id = gt->info.id;
-
-	if (!event_needs_forcewake(event))
-		return;
-
-	guard(mutex)(&pmu->fw_lock);
-
-	if (--pmu->fw_count[id])
-		return;
-
-	xe_force_wake_put(gt_to_fw(gt), pmu->fw_ref[id]);
-	pmu->fw_ref[id] = 0;
-}
-
 static bool event_supported(struct xe_pmu *pmu, unsigned int gt_id,
 			    unsigned int id)
 {
@@ -241,8 +182,6 @@ static void xe_pmu_event_destroy(struct perf_event *event)
 {
 	struct xe_device *xe = container_of(event->pmu, typeof(*xe), pmu.base);
 
-	event_gt_forcewake_put(event);
-
 	drm_WARN_ON(&xe->drm, event->parent);
 	xe_pm_runtime_put(xe);
 	drm_dev_put(&xe->drm);
@@ -281,11 +220,6 @@ static int xe_pmu_event_init(struct perf_event *event)
 	if (!event->parent) {
 		drm_dev_get(&xe->drm);
 		xe_pm_runtime_get(xe);
-		if (!event_gt_forcewake_get(event)) {
-			xe_pm_runtime_put(xe);
-			drm_dev_put(&xe->drm);
-			return -EINVAL;
-		}
 		event->destroy = xe_pmu_event_destroy;
 	}
 
@@ -326,7 +260,7 @@ static u64 __xe_pmu_event_read(struct perf_event *event)
 	case XE_PMU_EVENT_GT_ACTUAL_FREQUENCY:
 		return xe_guc_pc_get_act_freq(&gt->uc.guc.pc);
 	case XE_PMU_EVENT_GT_REQUESTED_FREQUENCY:
-		return xe_guc_pc_get_cur_freq_fw(&gt->uc.guc.pc);
+		return xe_guc_pc_get_cur_freq_if_awake(&gt->uc.guc.pc);
 	}
 
 	return 0;
@@ -539,52 +473,6 @@ static void set_supported_events(struct xe_pmu *pmu)
 	}
 }
 
-/**
- * xe_pmu_suspend() - Drop the forcewake held for PMU events
- * @pmu: the PMU object
- *
- * Called on system suspend, so that events left open by userspace do not keep
- * the GTs awake while the system sleeps.
- */
-void xe_pmu_suspend(struct xe_pmu *pmu)
-{
-	struct xe_device *xe = container_of(pmu, typeof(*xe), pmu);
-	struct xe_gt *gt;
-	u8 id;
-
-	if (!pmu->registered)
-		return;
-
-	guard(mutex)(&pmu->fw_lock);
-
-	for_each_gt(gt, xe, id) {
-		xe_force_wake_put(gt_to_fw(gt), pmu->fw_ref[id]);
-		pmu->fw_ref[id] = 0;
-	}
-}
-
-/**
- * xe_pmu_resume() - Take the forcewake needed by PMU events again
- * @pmu: the PMU object
- *
- * Called on system resume, once the GTs are back up.
- */
-void xe_pmu_resume(struct xe_pmu *pmu)
-{
-	struct xe_device *xe = container_of(pmu, typeof(*xe), pmu);
-	struct xe_gt *gt;
-	u8 id;
-
-	if (!pmu->registered)
-		return;
-
-	guard(mutex)(&pmu->fw_lock);
-
-	for_each_gt(gt, xe, id)
-		if (pmu->fw_count[id])
-			pmu->fw_ref[id] = xe_force_wake_get(gt_to_fw(gt), XE_FW_GT);
-}
-
 /**
  * xe_pmu_unregister() - Remove/cleanup PMU registration
  * @arg: Ptr to pmu
@@ -620,13 +508,11 @@ int xe_pmu_register(struct xe_pmu *pmu)
 	int ret = -ENOMEM;
 	char *name;
 
-	BUILD_BUG_ON(XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE != XE_PMU_MAX_GT);
+	BUILD_BUG_ON(XE_MAX_GT_PER_TILE != XE_PMU_MAX_GT);
 
 	if (IS_SRIOV_VF(xe))
 		return 0;
 
-	mutex_init(&pmu->fw_lock);
-
 	name = kasprintf(GFP_KERNEL, "xe_%s",
 			 dev_name(xe->drm.dev));
 	if (!name)
diff --git a/drivers/gpu/drm/xe/xe_pmu.h b/drivers/gpu/drm/xe/xe_pmu.h
index 912e033c1..60c37126f 100644
--- a/drivers/gpu/drm/xe/xe_pmu.h
+++ b/drivers/gpu/drm/xe/xe_pmu.h
@@ -10,12 +10,8 @@
 
 #if IS_ENABLED(CONFIG_PERF_EVENTS)
 int xe_pmu_register(struct xe_pmu *pmu);
-void xe_pmu_suspend(struct xe_pmu *pmu);
-void xe_pmu_resume(struct xe_pmu *pmu);
 #else
 static inline int xe_pmu_register(struct xe_pmu *pmu) { return 0; }
-static inline void xe_pmu_suspend(struct xe_pmu *pmu) {}
-static inline void xe_pmu_resume(struct xe_pmu *pmu) {}
 #endif
 
 #endif
diff --git a/drivers/gpu/drm/xe/xe_pmu_types.h b/drivers/gpu/drm/xe/xe_pmu_types.h
index 5a38cdf3c..f5ba4d566 100644
--- a/drivers/gpu/drm/xe/xe_pmu_types.h
+++ b/drivers/gpu/drm/xe/xe_pmu_types.h
@@ -6,12 +6,10 @@
 #ifndef _XE_PMU_TYPES_H_
 #define _XE_PMU_TYPES_H_
 
-#include <linux/mutex_types.h>
 #include <linux/perf_event.h>
 #include <linux/spinlock_types.h>
 
-/* XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE */
-#define XE_PMU_MAX_GT 4
+#define XE_PMU_MAX_GT 2
 
 /**
  * struct xe_pmu - PMU related data per Xe device
@@ -36,19 +34,6 @@ struct xe_pmu {
 	 * @supported_events: Bitmap of supported events, indexed by event id
 	 */
 	u64 supported_events;
-	/**
-	 * @fw_lock: Protects @fw_count and @fw_ref.
-	 */
-	struct mutex fw_lock;
-	/**
-	 * @fw_count: Number of events that need forcewake, per GT.
-	 */
-	unsigned int fw_count[XE_PMU_MAX_GT];
-	/**
-	 * @fw_ref: Forcewake reference shared by those events, per GT. Zero
-	 * while the device is suspended.
-	 */
-	unsigned int fw_ref[XE_PMU_MAX_GT];
 };
 
 #endif

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH RFC 2/2] drm/xe/pmu: Don't hold forcewake while events are open
  2026-12-02 20:51 ` [PATCH RFC 2/2] drm/xe/pmu: Don't hold forcewake while events are open Michal Hlavac
@ 2026-12-03 13:45   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-03 13:45 UTC (permalink / raw)
  To: Michal Hlavac; +Cc: intel-xe

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/xe/pmu: racy C0 checks allow unforced and torn GT MMIO
  reads
- [Medium] drm/xe/pmu: get_engine_active_ticks() clears active before C0
  check
--

commit a6af47d823d7e8c9d019a4ccda5e046a28740376
Author: Michal Hlavac <miso@hlavki.eu>

drm/xe/pmu: Don't hold forcewake while events are open

This commit drops GT forcewake tracking from the Xe PMU and its
suspend/resume hooks so open engine activity and frequency events allow the
GT to enter C6.  Instead, it guards the GPM timestamp and requested-frequency
MMIO reads with before-and-after GT_IDLE_C0 status checks.

Link: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/9633
Link: https://bugs.kde.org/show_bug.cgi?id=527160

> diff --git a/drivers/gpu/drm/xe/xe_guc_engine_activity.c b/drivers/gpu/drm/xe/xe_guc_engine_activity.c
> index 729ce8ac140b6..bb978c141b68b 100644
> --- a/drivers/gpu/drm/xe/xe_guc_engine_activity.c
> +++ b/drivers/gpu/drm/xe/xe_guc_engine_activity.c

[ ... ]

> @@ -170,18 +171,36 @@ static u64 cpu_ns_to_guc_tsc_tick(ktime_t ns, u32 freq)
>  #define read_metadata_record(xe_, map_, field_) \
>  	xe_map_rd_field(xe_, map_, 0, struct guc_engine_activity_metadata, field_)
>  
> +/*
> + * The PMU reads the engine activity without forcewake, and the GPM timestamp
> + * can only be read while the GT is awake. The GT is normally awake while an
> + * engine is running, but the engine can go idle and let the GT enter C6 at any
> + * time. Only trust the timestamp if the GT was in C0 before and after reading
> + * it.
> + */
> +static bool read_gpm_timestamp(struct xe_guc *guc, u32 *gpm_ts)
> +{
> +	struct xe_gt *gt = guc_to_gt(guc);
> +
> +	if (xe_guc_pc_c_status(&guc->pc) != GT_IDLE_C0)
> +		return false;
> +
> +	*gpm_ts = lower_32_bits(xe_mmio_read64_2x32(&gt->mmio, MISC_STATUS_0) >>
> +				guc->engine_activity.gpm_timestamp_shift);
> +
> +	return xe_guc_pc_c_status(&guc->pc) == GT_IDLE_C0;
> +}

[Severity: Medium]
Can read_gpm_timestamp() return a bogus or torn MISC_STATUS_0 value across a
C0 -> C6 -> C0 transition?

MISC_STATUS_0 is in the FORCEWAKE_GT domain, and xe_guc_pc_c_status() only
samples the instantaneous C-state rather than pinning forcewake or checking
a monotonic residency counter.  xe_mmio_read64_2x32() issues at least three
sequential MMIO reads, so even with local IRQs disabled in the perf callback,
the GT can drop into C6 and be woken back to C0 by another CPU in between:

CPU0 (perf read)                  GT / CPU1
xe_guc_pc_c_status() == C0
xe_mmio_read64_2x32():
  oldudw = read(udw)              (GT in C0, real upper dword)
                                  engine goes idle -> GT enters C6
  ldw = read(ldw)                 <-- unforced read in C6 (0 or ~0)
                                  CPU1 submits work -> GT wakes to C0
  udw = read(udw)                 (matches oldudw, loop exits)
xe_guc_pc_c_status() == C0        <-- returns true with torn gpm_ts

In get_engine_active_ticks(), subtracting non-zero last_update_tick from a
bogus or torn gpm_ts wraps u32 arithmetic into a spike up to U32_MAX ticks
in ea->active.  If that happens during xe_pmu_enable(), the next
xe_pmu_event_update() adds unsigned new - prev and permanently underflows
event->count to ~U64_MAX.

xe_guc_pc_get_cur_freq_if_awake() has the same C0 -> C6 -> C0 race on
FORCEWAKE_GT-gated RPNSWREQ, which can report 0 or 8517 MHz while in C0.

[ ... ]

> @@ -216,11 +235,8 @@ static u64 get_engine_active_ticks(struct xe_guc *guc, struct xe_hw_engine *hwe,
>  	cached_activity->active_ticks = active_ticks;
>  
>  update:
> -	if (ea->running) {
> -		gpm_ts = xe_mmio_read64_2x32(&gt->mmio, MISC_STATUS_0) >>
> -			 engine_activity->gpm_timestamp_shift;
> -		ea->active = lower_32_bits(gpm_ts) - cached_activity->last_update_tick;
> -	}
> +	if (ea->running && read_gpm_timestamp(guc, &gpm_ts))
> +		ea->active = gpm_ts - cached_activity->last_update_tick;
>  
>  	trace_xe_guc_engine_activity(xe, ea, hwe->name, hwe->instance);

[Severity: Medium]
Can get_engine_active_ticks() return a smaller total than the previous call
when read_gpm_timestamp() fails?

Just above update:, when change_num advances, get_engine_active_ticks()
updates ea->total and unconditionally zeroes ea->active.  Previously,
forcewake kept the GT in C0 so ea->active was always recomputed while
ea->running was true; now a C6 transition leaves ea->active at 0 instead of
keeping the previous value:

CPU0 (PMU read)                   GuC / GT
read change_num=1, last=1000
                                  checkpoint at 1400: change_num=2,
                                  active_ticks=400, last=1400
read MISC_STATUS_0 at 1500
  ea->active = 500, returns 500
...next read...
read change_num=2:
  ea->total = 400, ea->active = 0
                                  engine finishes -> GT enters C6
read_gpm_timestamp() == false
returns 400 (< 500)               <-- counter goes backwards

xe_pmu_event_update() adds unsigned new - prev directly to event->count, so
a backward step underflows event->count to ~U64_MAX if event->count is
smaller than the drop (for example right after xe_pmu_enable()).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-xe-pmu-forcewake-rfc-v1-0-d048943a313f@hlavki.eu?part=2

^ permalink raw reply	[flat|nested] 5+ messages in thread

* ✗ LGCI.VerificationFailed: failure for drm/xe/pmu: Don't let open PMU events block S0ix and GT C6
  2026-12-02 20:51 [PATCH RFC 0/2] drm/xe/pmu: Don't let open PMU events block S0ix and GT C6 Michal Hlavac
  2026-12-02 20:51 ` [PATCH RFC 1/2] drm/xe/pmu: Release forcewake across system suspend Michal Hlavac
  2026-12-02 20:51 ` [PATCH RFC 2/2] drm/xe/pmu: Don't hold forcewake while events are open Michal Hlavac
@ 2026-12-03 14:02 ` Patchwork
  2 siblings, 0 replies; 5+ messages in thread
From: Patchwork @ 2026-12-03 14:02 UTC (permalink / raw)
  To: Michal Hlavac; +Cc: intel-xe

== Series Details ==

Series: drm/xe/pmu: Don't let open PMU events block S0ix and GT C6
URL   : https://patchwork.freedesktop.org/series/180417/
State : failure

== Summary ==

Series author address 'miso@hlavki.eu' is not on the allowlist, which prevents CI from being automatically triggered.
If you want CI to run for this series, ask Patchwork project owners to click 'retest' on the series in Patchwork.
Exception occurred during validation, bailing out!
Build URL: http://intel-gfx-ci-public.igk.intel.com:8080/job/xe_pw_trigger/1357854/ (on master)



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-03 14:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 20:51 [PATCH RFC 0/2] drm/xe/pmu: Don't let open PMU events block S0ix and GT C6 Michal Hlavac
2026-12-02 20:51 ` [PATCH RFC 1/2] drm/xe/pmu: Release forcewake across system suspend Michal Hlavac
2026-12-02 20:51 ` [PATCH RFC 2/2] drm/xe/pmu: Don't hold forcewake while events are open Michal Hlavac
2026-12-03 13:45   ` sashiko-bot
2026-12-03 14:02 ` ✗ LGCI.VerificationFailed: failure for drm/xe/pmu: Don't let open PMU events block S0ix and GT C6 Patchwork

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox