Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [RESEND PATCH v3 0/3] drm/xe/forcewake: add delayed release for engine-cycle queries
@ 2026-12-01 23:07 Zongyao Bai
  2026-12-01 23:07 ` [RESEND PATCH v3 1/3] drm/xe/forcewake: add delayed release with hysteresis Zongyao Bai
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Zongyao Bai @ 2026-12-01 23:07 UTC (permalink / raw)
  To: intel-xe; +Cc: jia.yao, rodrigo.vivi, Zongyao Bai

RESEND: add the per-patch changelog to the commit messages, no code
change.

Engine-cycle queries are issued repeatedly by profilers such as VTune
and PTI to correlate CPU and GPU timestamps, for example through Level
Zero zeDeviceGetGlobalTimestamps(). Each query wakes the engine
forcewake domain and puts it to sleep again, and waiting for both
acknowledgments dominates the query latency.

This series adds an opt-in delayed release to forcewake. After the
final reference to a domain is dropped with xe_force_wake_put_delay(),
the domain stays awake for a short hold time, and a get within that
time reuses it without another wake request or ACK wait. The domain is
put to sleep from a delayed work in process context, so the sleep ACK
is waited for as in a regular put. The work sleeps only domains whose
hold time expired and that are still unreferenced, and requeues itself
for the rest. The hold time is 5 ms, and never less than two jiffies.

Delayed release must not outlive the device being powered off, reset
or removed. System and runtime suspend flush it: the pending work is
canceled and the held domains are put to sleep, while delayed release
stays available after resume. GT shutdown, device removal and
post-registration probe failure stop it: the work is disabled for good,
and a later delayed put puts the domain to sleep right away.

Only engine-cycle queries opt in. All other forcewake users keep
releasing immediately.

v3 (all from Sashiko review):
- xe_pm_runtime_get_if_active() only returns true/false, so the delayed
  release work could not tell "suspend in progress" from "runtime PM
  disabled" and returned early in both. Use pm_runtime_get_if_active()
  instead and do not drop the work while a runtime suspend is in
  progress. The work never resumes the device and handles three cases:
  a. runtime PM active: put the expired domains to sleep as before.
  b. runtime suspend in progress: requeue and retry later.
     b1. If the suspend completes, xe_force_wake_flush() cancels the
         work and settles the delayed domains.
     b2. If it aborts, the device returns to RPM_ACTIVE and the next
         run puts the expired domains to sleep.
  c. runtime PM disabled (e.g. CONFIG_PM=n): the device stays powered,
     so release the domains without a runtime PM reference.

- Do not count domains in delayed release as held in
  xe_force_wake_assert_held(), since they are awake with no reference
  and may be put to sleep by the delayed release work at any time.


v2 (all suggested by Rodrigo Vivi):
- Define the hold time in milliseconds and convert it with
  msecs_to_jiffies(), keeping it at least two jiffies. (Rodrigo)
- Pass the hold time to __xe_force_wake_put() instead of a bool, with
  0 meaning an immediate release. (Rodrigo)
- Replace the queue_delayed_work()/delayed_work_pending() check with a
  delayed_enabled flag protected by the forcewake lock and cleared by
  xe_force_wake_stop(). (Rodrigo)
- Use xe_pm_runtime_get_if_active() in the delayed work instead of
  asserting that the device is not runtime suspended. (Rodrigo)
- Rebase on drm-xe-next (4af7fae27dc6).

Zongyao Bai (3):
  drm/xe/forcewake: add delayed release with hysteresis
  drm/xe/forcewake: flush delayed release at power and teardown
    boundaries
  drm/xe/query: use delayed forcewake release for engine-cycle queries

 drivers/gpu/drm/xe/xe_device.c           |  11 +
 drivers/gpu/drm/xe/xe_force_wake.c       | 245 ++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_force_wake.h       |  42 +++-
 drivers/gpu/drm/xe/xe_force_wake_types.h |  48 +++++
 drivers/gpu/drm/xe/xe_gt.c               |  15 +-
 drivers/gpu/drm/xe/xe_query.c            |   2 +-
 6 files changed, 330 insertions(+), 33 deletions(-)

-- 
2.43.0


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

* [RESEND PATCH v3 1/3] drm/xe/forcewake: add delayed release with hysteresis
  2026-12-01 23:07 [RESEND PATCH v3 0/3] drm/xe/forcewake: add delayed release for engine-cycle queries Zongyao Bai
@ 2026-12-01 23:07 ` Zongyao Bai
  2026-12-01 23:30   ` sashiko-bot
  2026-12-01 23:07 ` [RESEND PATCH v3 2/3] drm/xe/forcewake: flush delayed release at power and teardown boundaries Zongyao Bai
  2026-12-01 23:07 ` [RESEND PATCH v3 3/3] drm/xe/query: use delayed forcewake release for engine-cycle queries Zongyao Bai
  2 siblings, 1 reply; 6+ messages in thread
From: Zongyao Bai @ 2026-12-01 23:07 UTC (permalink / raw)
  To: intel-xe; +Cc: jia.yao, rodrigo.vivi, Zongyao Bai

Add xe_force_wake_put_delay(), which keeps a domain awake for a short hold
time after its last reference is dropped instead of putting it to sleep
immediately. A xe_force_wake_get() within that time reuses the awake domain
without a wake request and ACK wait, which makes repeated short accesses
cheaper.

The domain is put to sleep from a delayed work, in process context, so
the sleep ACK can be waited for in the same way as a regular put. The work
sleeps only the domains whose hold time expired and which still have no
reference, and requeues itself for the rest. The hold time is 5 ms, but
never less than two jiffies. The work does not resume the device. If a
runtime suspend is in progress it retries later.

Delayed release can be disabled for good under the forcewake lock, after
which a put_delay() releases the domain right away.

v3:
- Use pm_runtime_get_if_active() in the delayed release work, so a
  runtime suspend in progress requeues the work and disabled runtime
  PM releases the domains without a reference. (Sashiko)
- Do not count domains in delayed release as held in
  xe_force_wake_assert_held(). (Sashiko)

v2:
- Define the hold time in milliseconds, at least two jiffies. (Rodrigo)
- Pass the hold time to __xe_force_wake_put() instead of a bool, with
  0 meaning an immediate release. (Rodrigo)
- Replace the queue_delayed_work()/delayed_work_pending() check with a
  delayed_enabled flag protected by the forcewake lock. (Rodrigo)
- Use xe_pm_runtime_get_if_active() in the delayed work instead of
  asserting that the device is not runtime suspended. (Rodrigo)

Suggested-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Zongyao Bai <zongyao.bai@intel.com>
---
 drivers/gpu/drm/xe/xe_force_wake.c       | 201 ++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_force_wake.h       |  24 ++-
 drivers/gpu/drm/xe/xe_force_wake_types.h |  19 +++
 drivers/gpu/drm/xe/xe_gt.c               |   5 +-
 4 files changed, 221 insertions(+), 28 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_force_wake.c b/drivers/gpu/drm/xe/xe_force_wake.c
index 197e2197bd0a..1b69e8e1e8eb 100644
--- a/drivers/gpu/drm/xe/xe_force_wake.c
+++ b/drivers/gpu/drm/xe/xe_force_wake.c
@@ -6,16 +6,29 @@
 #include "xe_force_wake.h"
 
 #include <drm/drm_util.h>
+#include <linux/device.h>
+#include <linux/pm_runtime.h>
+#include <linux/workqueue.h>
 
 #include "regs/xe_gt_regs.h"
 #include "regs/xe_reg_defs.h"
 #include "xe_gt.h"
 #include "xe_gt_printk.h"
 #include "xe_mmio.h"
+#include "xe_pm.h"
 #include "xe_sriov.h"
 
 #define XE_FORCE_WAKE_ACK_TIMEOUT_MS	50
 
+/* Hold time of a delayed release. */
+#define XE_FORCE_WAKE_HOLD_MS		5
+
+/* Keep the hold at two jiffies or more, so it spans a full tick on any HZ. */
+static unsigned long xe_force_wake_hold_jiffies(unsigned int hold_ms)
+{
+	return max(msecs_to_jiffies(hold_ms), 2UL);
+}
+
 static const char *str_wake_sleep(bool wake)
 {
 	return wake ? "wake" : "sleep";
@@ -42,12 +55,33 @@ static void init_domain(struct xe_force_wake *fw,
 	mark_domain_initialized(fw, id);
 }
 
-void xe_force_wake_init_gt(struct xe_gt *gt, struct xe_force_wake *fw)
+static void xe_force_wake_disable_delayed(struct xe_force_wake *fw)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&fw->lock, flags);
+	fw->delayed_enabled = false;
+	spin_unlock_irqrestore(&fw->lock, flags);
+
+	/* Not under fw->lock, the worker takes it. */
+	disable_delayed_work_sync(&fw->delayed_release_work);
+}
+
+static void xe_force_wake_disable_work(void *arg)
+{
+	xe_force_wake_disable_delayed(arg);
+}
+
+static void xe_force_wake_delayed_release_work(struct work_struct *work);
+
+int xe_force_wake_init_gt(struct xe_gt *gt, struct xe_force_wake *fw)
 {
 	struct xe_device *xe = gt_to_xe(gt);
 
 	fw->gt = gt;
 	spin_lock_init(&fw->lock);
+	INIT_DELAYED_WORK(&fw->delayed_release_work, xe_force_wake_delayed_release_work);
+	fw->delayed_enabled = true;
 
 	if (xe->info.graphics_verx100 >= 1270) {
 		init_domain(fw, XE_FW_DOMAIN_ID_GT,
@@ -58,6 +92,8 @@ void xe_force_wake_init_gt(struct xe_gt *gt, struct xe_force_wake *fw)
 			    FORCEWAKE_GT,
 			    FORCEWAKE_ACK_GT);
 	}
+
+	return devm_add_action_or_reset(xe->drm.dev, xe_force_wake_disable_work, fw);
 }
 
 void xe_force_wake_init_engines(struct xe_gt *gt, struct xe_force_wake *fw)
@@ -187,8 +223,13 @@ unsigned int __must_check xe_force_wake_get(struct xe_force_wake *fw,
 	spin_lock_irqsave(&fw->lock, flags);
 	for_each_fw_domain_masked(domain, ref_rqst, fw, tmp) {
 		if (!domain->ref++) {
-			awake_rqst |= BIT(domain->id);
-			domain_wake(gt, domain);
+			if (fw->delayed_domains & BIT(domain->id)) {
+				/* Still awake from a delayed release: reuse it. */
+				fw->delayed_domains &= ~BIT(domain->id);
+			} else {
+				awake_rqst |= BIT(domain->id);
+				domain_wake(gt, domain);
+			}
 		}
 		ref_incr |= BIT(domain->id);
 	}
@@ -212,23 +253,46 @@ unsigned int __must_check xe_force_wake_get(struct xe_force_wake *fw,
 	return ref_incr;
 }
 
-/**
- * xe_force_wake_put - Decrement the refcount and put domain to sleep if refcount becomes 0
- * @fw: Pointer to the force wake structure
- * @fw_ref: return of xe_force_wake_get()
- *
- * This function reduces the reference counts for domains in fw_ref. If
- * refcount for any of the specified domain reaches 0, it puts the domain to sleep
- * and waits for acknowledgment for domain to sleep within 50 milisec timeout.
- * Warns in case of timeout of ack from domain.
+/*
+ * Put the @mask domains to sleep and wait for the acknowledgment. Must be
+ * called with fw->lock held. Returns the mask of domains that failed to
+ * acknowledge.
  */
-void xe_force_wake_put(struct xe_force_wake *fw, unsigned int fw_ref)
+static unsigned int xe_force_wake_sleep_locked(struct xe_force_wake *fw,
+					       unsigned int mask)
 {
 	struct xe_gt *gt = fw->gt;
 	struct xe_force_wake_domain *domain;
-	unsigned int tmp, sleep = 0;
+	unsigned int tmp, ack_fail = 0;
+
+	lockdep_assert_held(&fw->lock);
+
+	for_each_fw_domain_masked(domain, mask, fw, tmp)
+		domain_sleep(gt, domain);
+
+	for_each_fw_domain_masked(domain, mask, fw, tmp) {
+		if (domain_sleep_wait(gt, domain) == 0)
+			fw->awake_domains &= ~BIT(domain->id);
+		else
+			ack_fail |= BIT(domain->id);
+	}
+
+	return ack_fail;
+}
+
+static void xe_force_wake_warn_sleep_fail(struct xe_gt *gt, unsigned int ack_fail)
+{
+	xe_gt_WARN(gt, ack_fail, "Forcewake domain%s %#x failed to acknowledge sleep request\n",
+		   str_plural(hweight_long(ack_fail)), ack_fail);
+}
+
+static void __xe_force_wake_put(struct xe_force_wake *fw, unsigned int fw_ref,
+				unsigned int hold_ms)
+{
+	struct xe_gt *gt = fw->gt;
+	struct xe_force_wake_domain *domain;
+	unsigned int tmp, sleep = 0, ack_fail;
 	unsigned long flags;
-	int ack_fail = 0;
 
 	/*
 	 * Avoid unnecessary lock and unlock when the function is called
@@ -244,21 +308,110 @@ void xe_force_wake_put(struct xe_force_wake *fw, unsigned int fw_ref)
 	for_each_fw_domain_masked(domain, fw_ref, fw, tmp) {
 		xe_gt_assert(gt, domain->ref);
 
-		if (!--domain->ref) {
+		if (--domain->ref)
+			continue;
+
+		if (hold_ms && fw->delayed_enabled) {
+			unsigned long hold = xe_force_wake_hold_jiffies(hold_ms);
+
+			domain->hold_until = jiffies + hold;
+			fw->delayed_domains |= BIT(domain->id);
+			/* No-op if pending, the worker requeues for the latest hold. */
+			queue_delayed_work(system_percpu_wq,
+					   &fw->delayed_release_work, hold);
+		} else {
 			sleep |= BIT(domain->id);
-			domain_sleep(gt, domain);
 		}
 	}
-	for_each_fw_domain_masked(domain, sleep, fw, tmp) {
-		if (domain_sleep_wait(gt, domain) == 0)
-			fw->awake_domains &= ~BIT(domain->id);
-		else
-			ack_fail |= BIT(domain->id);
+	ack_fail = xe_force_wake_sleep_locked(fw, sleep);
+	spin_unlock_irqrestore(&fw->lock, flags);
+
+	xe_force_wake_warn_sleep_fail(gt, ack_fail);
+}
+
+/**
+ * xe_force_wake_put - Decrement the refcount and put domain to sleep if refcount becomes 0
+ * @fw: Pointer to the force wake structure
+ * @fw_ref: return of xe_force_wake_get()
+ *
+ * This function reduces the reference counts for domains in fw_ref. If
+ * refcount for any of the specified domain reaches 0, it puts the domain to sleep
+ * and waits for acknowledgment for domain to sleep within 50 milisec timeout.
+ * Warns in case of timeout of ack from domain.
+ */
+void xe_force_wake_put(struct xe_force_wake *fw, unsigned int fw_ref)
+{
+	__xe_force_wake_put(fw, fw_ref, 0);
+}
+
+/**
+ * xe_force_wake_put_delay - Decrement the refcount, delaying the sleep request
+ * @fw: Pointer to the force wake structure
+ * @fw_ref: return of xe_force_wake_get()
+ *
+ * Like xe_force_wake_put(), but a domain whose refcount reaches 0 is kept
+ * awake for a short hold time instead of being put to sleep right away. An
+ * xe_force_wake_get() during that time reuses the awake domain without a
+ * wake request, which makes repeated short accesses cheaper. The domain is
+ * put to sleep by a worker once the hold time expires without a new user.
+ *
+ * Meant for opt-in hot paths that are expected to be repeated shortly after.
+ */
+void xe_force_wake_put_delay(struct xe_force_wake *fw, unsigned int fw_ref)
+{
+	__xe_force_wake_put(fw, fw_ref, XE_FORCE_WAKE_HOLD_MS);
+}
+
+/*
+ * Put the delayed domains whose hold time expired to sleep, and requeue for
+ * the remaining ones.
+ */
+static void xe_force_wake_delayed_release_work(struct work_struct *work)
+{
+	struct xe_force_wake *fw = container_of(to_delayed_work(work),
+						struct xe_force_wake, delayed_release_work);
+	struct xe_gt *gt = fw->gt;
+	struct xe_device *xe = gt_to_xe(gt);
+	struct xe_force_wake_domain *domain;
+	unsigned int tmp, expired = 0, ack_fail;
+	unsigned long flags, next = 0;
+	bool remaining = false;
+	int rpm;
+
+	/*
+	 * Never resume from here. If a runtime suspend is in progress, retry
+	 * later. Without runtime PM the device stays powered.
+	 */
+	rpm = pm_runtime_get_if_active(xe->drm.dev);
+	if (!rpm) {
+		queue_delayed_work(system_percpu_wq, &fw->delayed_release_work,
+				   xe_force_wake_hold_jiffies(XE_FORCE_WAKE_HOLD_MS));
+		return;
+	}
+
+	spin_lock_irqsave(&fw->lock, flags);
+	for_each_fw_domain_masked(domain, fw->delayed_domains, fw, tmp) {
+		if (time_before(jiffies, domain->hold_until)) {
+			if (!remaining || time_before(domain->hold_until, next))
+				next = domain->hold_until;
+			remaining = true;
+			continue;
+		}
+
+		expired |= BIT(domain->id);
+		fw->delayed_domains &= ~BIT(domain->id);
 	}
+
+	ack_fail = xe_force_wake_sleep_locked(fw, expired);
+
+	if (remaining)
+		queue_delayed_work(system_percpu_wq, &fw->delayed_release_work,
+				   next - jiffies);
 	spin_unlock_irqrestore(&fw->lock, flags);
 
-	xe_gt_WARN(gt, ack_fail, "Forcewake domain%s %#x failed to acknowledge sleep request\n",
-		   str_plural(hweight_long(ack_fail)), ack_fail);
+	xe_force_wake_warn_sleep_fail(gt, ack_fail);
+	if (rpm > 0)
+		xe_pm_runtime_put(xe);
 }
 
 const char *xe_force_wake_domain_to_str(enum xe_force_wake_domain_id id)
diff --git a/drivers/gpu/drm/xe/xe_force_wake.h b/drivers/gpu/drm/xe/xe_force_wake.h
index e2721f205d6c..fc504af9d219 100644
--- a/drivers/gpu/drm/xe/xe_force_wake.h
+++ b/drivers/gpu/drm/xe/xe_force_wake.h
@@ -11,13 +11,14 @@
 
 struct xe_gt;
 
-void xe_force_wake_init_gt(struct xe_gt *gt,
-			   struct xe_force_wake *fw);
+int xe_force_wake_init_gt(struct xe_gt *gt,
+			  struct xe_force_wake *fw);
 void xe_force_wake_init_engines(struct xe_gt *gt,
 				struct xe_force_wake *fw);
 unsigned int __must_check xe_force_wake_get(struct xe_force_wake *fw,
 					    enum xe_force_wake_domains domains);
 void xe_force_wake_put(struct xe_force_wake *fw, unsigned int fw_ref);
+void xe_force_wake_put_delay(struct xe_force_wake *fw, unsigned int fw_ref);
 
 const char *xe_force_wake_domain_to_str(enum xe_force_wake_domain_id id);
 
@@ -53,7 +54,8 @@ xe_force_wake_assert_held(struct xe_force_wake *fw,
 			  enum xe_force_wake_domains domain)
 {
 	xe_gt_assert(fw->gt, domain != XE_FORCEWAKE_ALL);
-	xe_gt_assert(fw->gt, fw->awake_domains & domain);
+	/* A domain in delayed release is awake but may sleep at any time. */
+	xe_gt_assert(fw->gt, fw->awake_domains & ~fw->delayed_domains & domain);
 }
 
 /**
@@ -103,6 +105,22 @@ DEFINE_CLASS(xe_force_wake, struct xe_force_wake_ref,
 #define xe_with_force_wake(ref, fw, domains) \
 	__xe_with_force_wake(ref, fw, domains, __UNIQUE_ID(done))
 
+/*
+ * Same as the xe_force_wake class, but the domains are released with
+ * xe_force_wake_put_delay().
+ */
+DEFINE_CLASS(xe_force_wake_delay, struct xe_force_wake_ref,
+	     xe_force_wake_put_delay(_T.fw, _T.domains),
+	     xe_force_wake_constructor(fw, domains),
+	     struct xe_force_wake *fw, unsigned int domains);
+
+#define __xe_with_force_wake_delay(ref, fw, domains, done) \
+	for (CLASS(xe_force_wake_delay, ref)(fw, domains), *(done) = NULL; \
+	     !(done); (done) = (void *)1)
+
+#define xe_with_force_wake_delay(ref, fw, domains) \
+	__xe_with_force_wake_delay(ref, fw, domains, __UNIQUE_ID(done))
+
 /*
  * Used when xe_force_wake_constructor() has already been called by another
  * function and the current function is responsible for releasing the forcewake
diff --git a/drivers/gpu/drm/xe/xe_force_wake_types.h b/drivers/gpu/drm/xe/xe_force_wake_types.h
index 14b7b86e801b..95bcc9ec2d74 100644
--- a/drivers/gpu/drm/xe/xe_force_wake_types.h
+++ b/drivers/gpu/drm/xe/xe_force_wake_types.h
@@ -8,6 +8,7 @@
 
 #include <linux/mutex.h>
 #include <linux/types.h>
+#include <linux/workqueue.h>
 
 #include "regs/xe_reg_defs.h"
 
@@ -82,6 +83,11 @@ struct xe_force_wake_domain {
 	u32 mask;
 	/** @ref: domain reference */
 	u32 ref;
+	/**
+	 * @hold_until: jiffies value until which a delayed release keeps the
+	 * domain awake. Protected by &xe_force_wake.lock.
+	 */
+	unsigned long hold_until;
 };
 
 /**
@@ -101,6 +107,19 @@ struct xe_force_wake {
 	spinlock_t lock;
 	/** @awake_domains: mask of all domains awake */
 	unsigned int awake_domains;
+	/**
+	 * @delayed_domains: mask of unreferenced domains that are kept awake
+	 * until @delayed_release_work puts them to sleep. A subset of
+	 * @awake_domains.
+	 */
+	unsigned int delayed_domains;
+	/**
+	 * @delayed_enabled: whether xe_force_wake_put_delay() may keep a domain
+	 * awake. Protected by @lock.
+	 */
+	bool delayed_enabled;
+	/** @delayed_release_work: puts expired @delayed_domains to sleep */
+	struct delayed_work delayed_release_work;
 	/** @initialized_domains: mask of all initialized domains */
 	unsigned int initialized_domains;
 	/** @domains: force wake domains */
diff --git a/drivers/gpu/drm/xe/xe_gt.c b/drivers/gpu/drm/xe/xe_gt.c
index b751b5044f3c..98c4464d9cd0 100644
--- a/drivers/gpu/drm/xe/xe_gt.c
+++ b/drivers/gpu/drm/xe/xe_gt.c
@@ -487,7 +487,10 @@ int xe_gt_init_early(struct xe_gt *gt)
 
 	xe_wa_process_gt_oob(gt);
 
-	xe_force_wake_init_gt(gt, gt_to_fw(gt));
+	err = xe_force_wake_init_gt(gt, gt_to_fw(gt));
+	if (err)
+		return err;
+
 	spin_lock_init(&gt->global_invl_lock);
 
 	err = xe_gt_tlb_inval_init_early(gt);
-- 
2.43.0


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

* [RESEND PATCH v3 2/3] drm/xe/forcewake: flush delayed release at power and teardown boundaries
  2026-12-01 23:07 [RESEND PATCH v3 0/3] drm/xe/forcewake: add delayed release for engine-cycle queries Zongyao Bai
  2026-12-01 23:07 ` [RESEND PATCH v3 1/3] drm/xe/forcewake: add delayed release with hysteresis Zongyao Bai
@ 2026-12-01 23:07 ` Zongyao Bai
  2026-12-01 23:24   ` sashiko-bot
  2026-12-01 23:07 ` [RESEND PATCH v3 3/3] drm/xe/query: use delayed forcewake release for engine-cycle queries Zongyao Bai
  2 siblings, 1 reply; 6+ messages in thread
From: Zongyao Bai @ 2026-12-01 23:07 UTC (permalink / raw)
  To: intel-xe; +Cc: jia.yao, rodrigo.vivi, Zongyao Bai

Add xe_force_wake_flush(), which cancels the delayed release work and
puts the domains it was holding to sleep. Use it, through a scoped class
that flushes after the final put, in system suspend and runtime suspend,
before the device is powered off. Delayed release stays enabled for use
after resume.

Add xe_force_wake_stop(), which also disables the work for good. Use it
in GT shutdown, after the reset, and on device removal and
post-registration probe failure. A put_delay() after the stop puts the
domain to sleep right away, so nothing is left awake after the stop.

v2:
- Clear delayed_enabled in xe_force_wake_stop() instead of only
  disabling the work. (Rodrigo)

Suggested-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Zongyao Bai <zongyao.bai@intel.com>
---
 drivers/gpu/drm/xe/xe_device.c           | 11 ++++++
 drivers/gpu/drm/xe/xe_force_wake.c       | 44 ++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_force_wake.h       | 18 ++++++++++
 drivers/gpu/drm/xe/xe_force_wake_types.h | 29 ++++++++++++++++
 drivers/gpu/drm/xe/xe_gt.c               | 10 +++---
 5 files changed, 108 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c
index 273e2c45fea9..f6d769900e43 100644
--- a/drivers/gpu/drm/xe/xe_device.c
+++ b/drivers/gpu/drm/xe/xe_device.c
@@ -897,6 +897,15 @@ static void xe_uid_probe(struct xe_device *xe)
 		xe->device_uid = xe_mmio_read64_2x32(xe_root_tile_mmio(xe), CRI_DEVICE_UID);
 }
 
+static void xe_device_force_wake_stop(struct xe_device *xe)
+{
+	struct xe_gt *gt;
+	u8 id;
+
+	for_each_gt(gt, xe, id)
+		xe_force_wake_stop(gt_to_fw(gt));
+}
+
 int xe_device_probe(struct xe_device *xe)
 {
 	struct xe_tile *tile;
@@ -1112,6 +1121,7 @@ int xe_device_probe(struct xe_device *xe)
 err_unregister_display:
 	xe_display_unregister(xe);
 	drm_dev_unregister(&xe->drm);
+	xe_device_force_wake_stop(xe);
 
 	return err;
 }
@@ -1121,6 +1131,7 @@ void xe_device_remove(struct xe_device *xe)
 	xe_display_unregister(xe);
 
 	drm_dev_unplug(&xe->drm);
+	xe_device_force_wake_stop(xe);
 
 	xe_bo_pci_dev_remove_all(xe);
 }
diff --git a/drivers/gpu/drm/xe/xe_force_wake.c b/drivers/gpu/drm/xe/xe_force_wake.c
index 1b69e8e1e8eb..e5dcfcdf6605 100644
--- a/drivers/gpu/drm/xe/xe_force_wake.c
+++ b/drivers/gpu/drm/xe/xe_force_wake.c
@@ -414,6 +414,50 @@ static void xe_force_wake_delayed_release_work(struct work_struct *work)
 		xe_pm_runtime_put(xe);
 }
 
+static void xe_force_wake_settle_delayed(struct xe_force_wake *fw)
+{
+	unsigned int ack_fail;
+	unsigned long flags;
+
+	spin_lock_irqsave(&fw->lock, flags);
+	ack_fail = xe_force_wake_sleep_locked(fw, fw->delayed_domains);
+	fw->delayed_domains = 0;
+	spin_unlock_irqrestore(&fw->lock, flags);
+
+	xe_force_wake_warn_sleep_fail(fw->gt, ack_fail);
+}
+
+/**
+ * xe_force_wake_flush - Put all delayed domains to sleep now
+ * @fw: Pointer to the force wake structure
+ *
+ * Cancel the pending delayed release and put the domains it was holding awake
+ * to sleep right away. Delayed release stays enabled, so it can be used again
+ * after resume.
+ *
+ * Used before powering the device off, once no forcewake reference is held
+ * and no new delayed release can start.
+ */
+void xe_force_wake_flush(struct xe_force_wake *fw)
+{
+	cancel_delayed_work_sync(&fw->delayed_release_work);
+	xe_force_wake_settle_delayed(fw);
+}
+
+/**
+ * xe_force_wake_stop - Stop delayed release for good
+ * @fw: Pointer to the force wake structure
+ *
+ * Like xe_force_wake_flush(), but also disable delayed release, so it cannot
+ * start again. Used on device removal and shutdown, once no forcewake
+ * user that may release with xe_force_wake_put_delay() is left.
+ */
+void xe_force_wake_stop(struct xe_force_wake *fw)
+{
+	xe_force_wake_disable_delayed(fw);
+	xe_force_wake_settle_delayed(fw);
+}
+
 const char *xe_force_wake_domain_to_str(enum xe_force_wake_domain_id id)
 {
 	switch (id) {
diff --git a/drivers/gpu/drm/xe/xe_force_wake.h b/drivers/gpu/drm/xe/xe_force_wake.h
index fc504af9d219..481416bd6c60 100644
--- a/drivers/gpu/drm/xe/xe_force_wake.h
+++ b/drivers/gpu/drm/xe/xe_force_wake.h
@@ -19,6 +19,8 @@ unsigned int __must_check xe_force_wake_get(struct xe_force_wake *fw,
 					    enum xe_force_wake_domains domains);
 void xe_force_wake_put(struct xe_force_wake *fw, unsigned int fw_ref);
 void xe_force_wake_put_delay(struct xe_force_wake *fw, unsigned int fw_ref);
+void xe_force_wake_flush(struct xe_force_wake *fw);
+void xe_force_wake_stop(struct xe_force_wake *fw);
 
 const char *xe_force_wake_domain_to_str(enum xe_force_wake_domain_id id);
 
@@ -105,6 +107,22 @@ DEFINE_CLASS(xe_force_wake, struct xe_force_wake_ref,
 #define xe_with_force_wake(ref, fw, domains) \
 	__xe_with_force_wake(ref, fw, domains, __UNIQUE_ID(done))
 
+/*
+ * Same as the xe_force_wake class, but flush the delayed release after the
+ * domains are put. For paths that power the device off.
+ */
+static inline void xe_force_wake_put_and_flush(struct xe_force_wake *fw,
+					       unsigned int fw_ref)
+{
+	xe_force_wake_put(fw, fw_ref);
+	xe_force_wake_flush(fw);
+}
+
+DEFINE_CLASS(xe_force_wake_flush, struct xe_force_wake_ref,
+	     xe_force_wake_put_and_flush(_T.fw, _T.domains),
+	     xe_force_wake_constructor(fw, domains),
+	     struct xe_force_wake *fw, unsigned int domains);
+
 /*
  * Same as the xe_force_wake class, but the domains are released with
  * xe_force_wake_put_delay().
diff --git a/drivers/gpu/drm/xe/xe_force_wake_types.h b/drivers/gpu/drm/xe/xe_force_wake_types.h
index 95bcc9ec2d74..5ad289670319 100644
--- a/drivers/gpu/drm/xe/xe_force_wake_types.h
+++ b/drivers/gpu/drm/xe/xe_force_wake_types.h
@@ -99,6 +99,35 @@ struct xe_force_wake_domain {
  * Currently only used for GT power domains (where the term "forcewake" is used
  * in the hardware documentation), although the interface could be extended to
  * power wells in other parts of the hardware in the future.
+ *
+ * Delayed-release state flow of a domain, protected by @lock:
+ *
+ *  +------------+                                  +--------------+
+ *  |   ASLEEP   | ------- get: wake -------------> |  REFERENCED  |
+ *  |  ref == 0  | <------ final put: sleep ------- |   ref > 0    |
+ *  +------------+                                  +--------------+
+ *        ^                                           |         ^
+ *        |                         final put_delay   |         |
+ *        |                                           |         | get: reuse
+ *        |                                           |         | no MMIO
+ *        |                                           v         |
+ *        |                                         +--------------+
+ *        |                                         | DELAYED HOLD |
+ *        +---- worker after @hold_until, --------- |   ref == 0   |
+ *              or flush/stop: sleep                +--------------+
+ *                                                    |          ^
+ *                                                    |          |
+ *                                                    +----------+
+ *                                         worker before @hold_until: requeue
+ *
+ * Once @delayed_enabled is cleared, a final put_delay behaves like a final
+ * put and DELAYED HOLD becomes unreachable.
+ *
+ * ASLEEP:       neither @awake_domains nor @delayed_domains has the domain.
+ * REFERENCED:   @awake_domains has the domain, @delayed_domains does not.
+ * DELAYED HOLD: both masks have the domain; @delayed_release_work is
+ *               pending or running. Each final put_delay moves
+ *               @hold_until forward.
  */
 struct xe_force_wake {
 	/** @gt: back pointers to GT */
diff --git a/drivers/gpu/drm/xe/xe_gt.c b/drivers/gpu/drm/xe/xe_gt.c
index 98c4464d9cd0..2be58bb09180 100644
--- a/drivers/gpu/drm/xe/xe_gt.c
+++ b/drivers/gpu/drm/xe/xe_gt.c
@@ -1008,7 +1008,7 @@ int xe_gt_suspend(struct xe_gt *gt)
 	xe_gt_dbg(gt, "suspending\n");
 	xe_gt_sanitize(gt);
 
-	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
+	CLASS(xe_force_wake_flush, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL)) {
 		xe_gt_err(gt, "suspend failed (%pe)\n", ERR_PTR(-ETIMEDOUT));
 		return -ETIMEDOUT;
@@ -1031,8 +1031,10 @@ int xe_gt_suspend(struct xe_gt *gt)
 
 void xe_gt_shutdown(struct xe_gt *gt)
 {
-	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
-	do_gt_reset(gt);
+	xe_with_force_wake(fw_ref, gt_to_fw(gt), XE_FORCEWAKE_ALL)
+		do_gt_reset(gt);
+
+	xe_force_wake_stop(gt_to_fw(gt));
 }
 
 /**
@@ -1100,7 +1102,7 @@ int xe_gt_runtime_suspend(struct xe_gt *gt)
 {
 	xe_gt_dbg(gt, "runtime suspending\n");
 
-	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
+	CLASS(xe_force_wake_flush, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL)) {
 		xe_gt_err(gt, "runtime suspend failed (%pe)\n", ERR_PTR(-ETIMEDOUT));
 		return -ETIMEDOUT;
-- 
2.43.0


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

* [RESEND PATCH v3 3/3] drm/xe/query: use delayed forcewake release for engine-cycle queries
  2026-12-01 23:07 [RESEND PATCH v3 0/3] drm/xe/forcewake: add delayed release for engine-cycle queries Zongyao Bai
  2026-12-01 23:07 ` [RESEND PATCH v3 1/3] drm/xe/forcewake: add delayed release with hysteresis Zongyao Bai
  2026-12-01 23:07 ` [RESEND PATCH v3 2/3] drm/xe/forcewake: flush delayed release at power and teardown boundaries Zongyao Bai
@ 2026-12-01 23:07 ` Zongyao Bai
  2 siblings, 0 replies; 6+ messages in thread
From: Zongyao Bai @ 2026-12-01 23:07 UTC (permalink / raw)
  To: intel-xe; +Cc: jia.yao, rodrigo.vivi, Zongyao Bai

Engine-cycle queries are issued repeatedly by profilers such as VTune and
PTI to correlate CPU and GPU timestamps, for example through Level Zero
zeDeviceGetGlobalTimestamps(). Each query wakes the engine forcewake
domain and puts it to sleep again, and waiting for both acknowledgments
dominates the latency.

Release the domain with xe_force_wake_put_delay() so that a burst of
queries reuses the awake domain instead of waking it again. Other forcewake
users keep releasing immediately.

Signed-off-by: Zongyao Bai <zongyao.bai@intel.com>
---
 drivers/gpu/drm/xe/xe_query.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/xe/xe_query.c b/drivers/gpu/drm/xe/xe_query.c
index dc975f595368..86c4e1dfc101 100644
--- a/drivers/gpu/drm/xe/xe_query.c
+++ b/drivers/gpu/drm/xe/xe_query.c
@@ -157,7 +157,7 @@ query_engine_cycles(struct xe_device *xe,
 
 	fw_domain = xe_hw_engine_to_fw_domain(hwe);
 
-	xe_with_force_wake(fw_ref, gt_to_fw(gt), fw_domain) {
+	xe_with_force_wake_delay(fw_ref, gt_to_fw(gt), fw_domain) {
 		if (!xe_force_wake_ref_has_domain(fw_ref.domains, fw_domain))
 			return -EIO;
 
-- 
2.43.0


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

* Re: [RESEND PATCH v3 2/3] drm/xe/forcewake: flush delayed release at power and teardown boundaries
  2026-12-01 23:07 ` [RESEND PATCH v3 2/3] drm/xe/forcewake: flush delayed release at power and teardown boundaries Zongyao Bai
@ 2026-12-01 23:24   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-01 23:24 UTC (permalink / raw)
  To: Zongyao Bai, intel-xe

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/xe: missing xe_force_wake_flush() in xe_gt_flr_prepare()
  before PCIe FLR
--

commit 9c538d813afa70e3cd0f25e79eefb0e7519a510c
Author: Zongyao Bai <zongyao.bai@intel.com>

drm/xe/forcewake: flush delayed release at power and teardown boundaries

This commit adds xe_force_wake_flush() and xe_force_wake_stop() to cancel
delayed forcewake releases and put held domains to sleep across suspend,
shutdown, and device removal.

> diff --git a/drivers/gpu/drm/xe/xe_gt.c b/drivers/gpu/drm/xe/xe_gt.c
> index 1d2fbb8a0de8d..9c8c315f63b24 100644
> --- a/drivers/gpu/drm/xe/xe_gt.c
> +++ b/drivers/gpu/drm/xe/xe_gt.c
> @@ -1058,7 +1058,7 @@ int xe_gt_suspend(struct xe_gt *gt)
>  	xe_gt_dbg(gt, "suspending\n");
>  	xe_gt_sanitize(gt);
>  
> -	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
> +	CLASS(xe_force_wake_flush, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
>  	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL)) {
>  		xe_gt_err(gt, "suspend failed (%pe)\n", ERR_PTR(-ETIMEDOUT));
>  		return -ETIMEDOUT;

[Severity: Medium]
Should xe_gt_flr_prepare() also call xe_force_wake_flush(gt_to_fw(gt))?

When xe_pci_reset_prepare() calls xe_device_suspend(xe, false, true), the
flr branch calls xe_gt_flr_prepare() instead of xe_gt_suspend():

drivers/gpu/drm/xe/xe_device.c:xe_device_suspend() {
	...
	if (flr) {
		for_each_gt(gt, xe, id)
			xe_gt_flr_prepare(gt);
	} else {
		...
		for_each_gt(gt, xe, id) {
			err = xe_gt_suspend(gt);
	...
}

If a forcewake domain is in delayed hold when a PCIe FLR starts,
delayed_release_work can fire while MMIO is disabled during the reset and
trigger sleep ACK timeout warnings, or fw->delayed_domains can stay set
after the hardware FLR resets domains to sleep, causing
xe_force_wake_get() during post-FLR xe_device_resume() to reuse the
domain without waking it.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201230744.680448-1-zongyao.bai@intel.com?part=2

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

* Re: [RESEND PATCH v3 1/3] drm/xe/forcewake: add delayed release with hysteresis
  2026-12-01 23:07 ` [RESEND PATCH v3 1/3] drm/xe/forcewake: add delayed release with hysteresis Zongyao Bai
@ 2026-12-01 23:30   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-01 23:30 UTC (permalink / raw)
  To: Zongyao Bai, intel-xe

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/xe/forcewake: unflushed delayed release across PCIe FLR
  in xe_gt_flr_prepare()
--

commit 2e2963541af2cb3833279db65dd42d66d442d570
Author: Zongyao Bai <zongyao.bai@intel.com>

drm/xe/forcewake: add delayed release with hysteresis

This commit adds xe_force_wake_put_delay() and a delayed work item to
keep forcewake domains awake for a short hysteresis window after their
last reference is dropped.

> diff --git a/drivers/gpu/drm/xe/xe_force_wake.c b/drivers/gpu/drm/xe/xe_force_wake.c
> index 197e2197bd0a3..1b69e8e1e8eb1 100644
> --- a/drivers/gpu/drm/xe/xe_force_wake.c
> +++ b/drivers/gpu/drm/xe/xe_force_wake.c
[ ... ]
> @@ -187,8 +223,13 @@ unsigned int __must_check xe_force_wake_get(struct xe_force_wake *fw,
>  	spin_lock_irqsave(&fw->lock, flags);
>  	for_each_fw_domain_masked(domain, ref_rqst, fw, tmp) {
>  		if (!domain->ref++) {
> -			awake_rqst |= BIT(domain->id);
> -			domain_wake(gt, domain);
> +			if (fw->delayed_domains & BIT(domain->id)) {
> +				/* Still awake from a delayed release: reuse it. */
> +				fw->delayed_domains &= ~BIT(domain->id);
> +			} else {
> +				awake_rqst |= BIT(domain->id);
> +				domain_wake(gt, domain);
> +			}
>  		}
>  		ref_incr |= BIT(domain->id);
>  	}

[Severity: Medium]
What happens to domains in fw->delayed_domains across a PCIe FLR?

Even with commit "drm/xe/forcewake: flush delayed release at power and
teardown boundaries" flushing delayed domains in xe_gt_suspend() and
xe_gt_runtime_suspend(), xe_pci_reset_prepare() calls
xe_device_suspend(xe, false, true) with flr = true, which runs
xe_gt_flr_prepare() and skips xe_gt_suspend().

If a domain is in fw->delayed_domains when an FLR starts,
fw->delayed_release_work can fire during the reset and time out waiting
for a sleep ACK on a resetting device, or BIT(domain->id) can remain set
in fw->delayed_domains into xe_pci_reset_done() -> xe_gt_resume() so that
xe_force_wake_get() skips domain_wake() on a domain that hardware reset
to sleep.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201230744.680448-1-zongyao.bai@intel.com?part=1

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

end of thread, other threads:[~2026-12-01 23:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 23:07 [RESEND PATCH v3 0/3] drm/xe/forcewake: add delayed release for engine-cycle queries Zongyao Bai
2026-12-01 23:07 ` [RESEND PATCH v3 1/3] drm/xe/forcewake: add delayed release with hysteresis Zongyao Bai
2026-12-01 23:30   ` sashiko-bot
2026-12-01 23:07 ` [RESEND PATCH v3 2/3] drm/xe/forcewake: flush delayed release at power and teardown boundaries Zongyao Bai
2026-12-01 23:24   ` sashiko-bot
2026-12-01 23:07 ` [RESEND PATCH v3 3/3] drm/xe/query: use delayed forcewake release for engine-cycle queries Zongyao Bai

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