Skip to contentSkip to conversation

drm/panthor: Do not change the GPU clock rate while suspended

PATCH Superseded Arun Iyengar 4 participants 4 messages Nov 10

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/panthor: Do not change the GPU clock rate while suspended

From the start of panthor_device_suspend() until panthor_device_resume() has enabled the clocks again, the GPU clocks are off or about to be, and the GPU power domain may be down. devfreq can still call ->target() in that window: a write to min_freq or max_freq in sysfs, a thermal cooling state change or a PM QoS request all end up in update_devfreq() whatever the device state.

With the RK3588 GPU core clock switched to the firmware-managed SCMI_CLK_GPU (needed to reach the PVTPLL rates of the upper OPPs), such a call is fatal. To set a PVTPLL rate TF-A programs registers in the GPU GRF, which sit in the GPU power domain, and with the domain off that access ends in an asynchronous SError and a kernel panic, for example after

echo 1000000000 > /sys/class/devfreq/fb000000.gpu/min_freq

while the GPU is runtime suspended.

Only apply a rate to the hardware while the device is resuming or active, plus the suspend frequency that devfreq_suspend_device() applies from the suspend path while the GPU is still powered. Other requests are still accepted and recorded by devfreq, which applies them on resume or at the next governor update.

The RFC this is based on (Jonas Karlman's panthor SCMI clock change, carried in Armbian's linux-rockchip pull request 380) checked the runtime PM status instead. That
status is still RPM_SUSPENDING after panthor_devfreq_suspend() has run, which leaves a window where the rate can be raised again right before the power domain goes down. The devfreq suspend count cannot be used
either: panthor's first runtime resume calls devfreq_resume_device() without a matching suspend, so the count is -1 while the GPU is active and 0 while it is suspended.

---
Tested on an Orange Pi 5 (RK3588S) with the GPU core clock switched to SCMI_CLK_GPU and a 200 MHz opp-suspend (the configuration from the RFC, not yet in mainline): writing min_freq while the GPU was runtime suspended no longer ends in an SError, the request takes effect at the next job (1 GHz measured), max_freq likewise; 200 suspend/resume cycles with short GPU jobs and a 3 minute sustained load ran without faults. With the mainline device tree (CRU clock, no suspend OPP) the SError is not reachable and the guard only skips rate changes while the GPU is powered down.

This patch was developed with Claude (Anthropic) as a coding assistant:
it wrote the code and the commit message, and I reviewed and tested it as described above.

---
 drivers/gpu/drm/panthor/panthor_devfreq.c | 42 ++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)
drivers/gpu/drm/panthor/panthor_devfreq.c +39 -3
index 2249b41ca4af..cab86e9c9d46 100644
@@ -60,20 +60,56 @@ static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq)
 	pdevfreq->time_last_update = now;
 }
 
+/*
+ * Whether a new rate can be applied to the hardware now.
+ *
+ * From the start of panthor_device_suspend() until panthor_device_resume()
+ * has enabled the clocks again, the GPU clocks are about to be disabled
+ * or are off, and the GPU power domain may be down. devfreq can still
+ * call ->target() in that window: sysfs
+ * min_freq/max_freq writes, thermal cooling and PM QoS requests all end
+ * up in update_devfreq() whatever the device state.
+ *
+ * Changing the rate then is pointless, and harmful where the core clock
+ * is managed by firmware: on RK3588, setting SCMI_CLK_GPU to a PVTPLL
+ * rate with the GPU power domain off makes TF-A access the powered-down
+ * GPU GRF, which ends in an asynchronous SError. So while the device is
+ * suspending only the suspend frequency, applied by
+ * devfreq_suspend_device(), reaches the hardware, and nothing does
+ * while it is suspended. devfreq records the other requests: they are
+ * applied on resume or at the next governor update.
+ */
+static bool panthor_devfreq_can_set_rate(struct panthor_device *ptdev,
+					 unsigned long freq)
+{
+	struct devfreq *devfreq = ptdev->devfreq ? ptdev->devfreq->devfreq : NULL;
+
+	switch (atomic_read(&ptdev->pm.state)) {
+	case PANTHOR_DEVICE_PM_STATE_SUSPENDED:
+		return false;
+	case PANTHOR_DEVICE_PM_STATE_SUSPENDING:
+		return devfreq && devfreq->suspend_freq &&
+		       freq == devfreq->suspend_freq;
+	default:
+		return true;
+	}
+}
+
 static int panthor_devfreq_target(struct device *dev, unsigned long *freq,
 				  u32 flags)
 {
+	struct panthor_device *ptdev = dev_get_drvdata(dev);
 	struct dev_pm_opp *opp;
-	int err;
 
 	opp = devfreq_recommended_opp(dev, freq, flags);
 	if (IS_ERR(opp))
 		return PTR_ERR(opp);
 	dev_pm_opp_put(opp);
 
-	err = dev_pm_opp_set_rate(dev, *freq);
+	if (!panthor_devfreq_can_set_rate(ptdev, *freq))
+		return 0;
 
-	return err;
+	return dev_pm_opp_set_rate(dev, *freq);
 }
 
 static void panthor_devfreq_reset(struct panthor_devfreq *pdevfreq)

---
base-commit: 8deaade4a5a4ff635df91e246e42139ab2b33f6d
change-id: 20261110-panthor-suspend-clk-11d5604fe5be

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/panthor: skipped rates in panthor_devfreq_target() lost across resume - [Medium] drm/panthor…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/panthor: skipped rates in panthor_devfreq_target() lost across resume
- [Medium] drm/panthor: lockless pm.state check in panthor_devfreq_can_set_rate() races
- [Low] drm/panthor: missing Fixes trailer on panthor_devfreq_target() bug fix
--

commit b3aaf374222f607db90947d06a6fffd9f1ae68b2
Author: Arun Iyengar <[email protected]>

Quoted 36 lines

drm/panthor: Do not change the GPU clock rate while suspended

From the start of panthor_device_suspend() until panthor_device_resume() has enabled the clocks again, the GPU clocks are off or about to be, and the GPU power domain may be down. devfreq can still call ->target() in that window: a write to min_freq or max_freq in sysfs, a thermal cooling state change or a PM QoS request all end up in update_devfreq() whatever the device state.

With the RK3588 GPU core clock switched to the firmware-managed SCMI_CLK_GPU (needed to reach the PVTPLL rates of the upper OPPs), such a call is fatal. To set a PVTPLL rate TF-A programs registers in the GPU GRF, which sit in the GPU power domain, and with the domain off that access ends in an asynchronous SError and a kernel panic, for example after

echo 1000000000 > /sys/class/devfreq/fb000000.gpu/min_freq

while the GPU is runtime suspended.

Only apply a rate to the hardware while the device is resuming or active, plus the suspend frequency that devfreq_suspend_device() applies from the suspend path while the GPU is still powered. Other requests are still accepted and recorded by devfreq, which applies them on resume or at the next governor update.

The RFC this is based on (Jonas Karlman's panthor SCMI clock change, carried in Armbian's linux-rockchip pull request 380) checked the runtime PM status instead. That
status is still RPM_SUSPENDING after panthor_devfreq_suspend() has run, which leaves a window where the rate can be raised again right before the power domain goes down. The devfreq suspend count cannot be used
either: panthor's first runtime resume calls devfreq_resume_device() without a matching suspend, so the count is -1 while the GPU is active and 0 while it is suspended.

Quoted 2 lines

[Severity: Low]
This fixes a crash when panthor_devfreq_target() is called while the device is suspended. Should it include:

Fixes fac9b22df4b1 ("drm/panthor: Add the devfreq logical block")
Quoted 32 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +24 -0
index 2249b41ca4afe..cab86e9c9d46a 100644
@@ -60,20 +60,56 @@ static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq)
 	pdevfreq->time_last_update = now;
 }
 
+/*
+ * Whether a new rate can be applied to the hardware now.
+ *
+ * From the start of panthor_device_suspend() until panthor_device_resume()
+ * has enabled the clocks again, the GPU clocks are about to be disabled
+ * or are off, and the GPU power domain may be down. devfreq can still
+ * call ->target() in that window: sysfs
+ * min_freq/max_freq writes, thermal cooling and PM QoS requests all end
+ * up in update_devfreq() whatever the device state.
+ *
+ * Changing the rate then is pointless, and harmful where the core clock
+ * is managed by firmware: on RK3588, setting SCMI_CLK_GPU to a PVTPLL
+ * rate with the GPU power domain off makes TF-A access the powered-down
+ * GPU GRF, which ends in an asynchronous SError. So while the device is
+ * suspending only the suspend frequency, applied by
+ * devfreq_suspend_device(), reaches the hardware, and nothing does
+ * while it is suspended. devfreq records the other requests: they are
+ * applied on resume or at the next governor update.
+ */
+static bool panthor_devfreq_can_set_rate(struct panthor_device *ptdev,
+					 unsigned long freq)
+{
+	struct devfreq *devfreq = ptdev->devfreq ? ptdev->devfreq->devfreq : NULL;
+

drivers/gpu/drm/panthor/panthor_devfreq.c static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) · line 96

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +10 -0
+	switch (atomic_read(&ptdev->pm.state)) {
+	case PANTHOR_DEVICE_PM_STATE_SUSPENDED:
+		return false;
+	case PANTHOR_DEVICE_PM_STATE_SUSPENDING:
+		return devfreq && devfreq->suspend_freq &&
+		       freq == devfreq->suspend_freq;
+	default:
+		return true;
+	}
+}

[Severity: Medium]
Can panthor_devfreq_can_set_rate() still allow dev_pm_opp_set_rate() to run while the GPU clocks are disabled or being turned off?

Neither panthor_device_suspend() nor panthor_device_resume() synchronizes ptdev->pm.state transitions and clock enable/disable with devfreq->lock:

CPU0 CPU1
panthor_device_suspend()
panthor_devfreq_suspend(ptdev)

                                      update_devfreq()
                                        panthor_devfreq_target()
                                          panthor_devfreq_can_set_rate()
                                            /* state is SUSPENDING and
                                             * freq == suspend_freq -> true
                                             */

clk_disable_unprepare(clks.coregroup) clk_disable_unprepare(clks.stacks) clk_disable_unprepare(clks.core)
atomic_set(..., SUSPENDED)
/* genpd powers down GPU */
dev_pm_opp_set_rate()
<-- accesses powered-down GPU GRF

The same race exists when panthor_devfreq_can_set_rate() sees PANTHOR_DEVICE_PM_STATE_ACTIVE right before panthor_device_suspend() disables the clocks (when suspend_freq is 0 and polling is stopped, devfreq_suspend_device() does not take devfreq->lock). Likewise, panthor_device_resume() sets PANTHOR_DEVICE_PM_STATE_RESUMING before enabling clks.core, clks.stacks and clks.coregroup, and leaves it set across clk_disable_unprepare() on the err_suspend_devfreq error path, where the default branch in panthor_devfreq_can_set_rate() returns true while the clocks are off.

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +2 -1
+
 static int panthor_devfreq_target(struct device *dev, unsigned long *freq,
 				  u32 flags)
 {
+	struct panthor_device *ptdev = dev_get_drvdata(dev);
 	struct dev_pm_opp *opp;
-	int err;
 
 	opp = devfreq_recommended_opp(dev, freq, flags);
 	if (IS_ERR(opp))

drivers/gpu/drm/panthor/panthor_devfreq.c static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) · line 113

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +3 -2
 		return PTR_ERR(opp);
 	dev_pm_opp_put(opp);
 
-	err = dev_pm_opp_set_rate(dev, *freq);
+	if (!panthor_devfreq_can_set_rate(ptdev, *freq))
+		return 0;
 
-	return err;
+	return dev_pm_opp_set_rate(dev, *freq);
 }

[Severity: High]
Does returning 0 from panthor_devfreq_target() without saving *freq cause frequency updates made while suspended to be lost across resume?

In devfreq_set_target(), devfreq updates devfreq->previous_freq = new_freq, but only records devfreq->resume_freq when devfreq->suspend_freq is non-zero:

drivers/devfreq/devfreq.c:devfreq_set_target() {
    ...
	devfreq->previous_freq = new_freq;

	if (devfreq->suspend_freq)
		devfreq->resume_freq = new_freq;
    ...
}

When opp-suspend is not defined in DT (devfreq->suspend_freq == 0), devfreq->resume_freq stays 0 and devfreq_resume_device() does not call devfreq_set_target() on resume. With non-polling governors (performance, powersave, userspace), DEVFREQ_GOV_RESUME is a no-op, so any min_freq, max_freq, set_freq or governor change made while runtime-suspended updates devfreq->previous_freq without ever programming the hardware clock on resume. With simple_ondemand, devfreq_monitor_resume() overwrites devfreq->previous_freq with clk_get_rate() (the stale pre-suspend clock rate) and only queues polling work for polling_ms (50 ms) later, so resumed jobs run at the stale rate for the first 50 ms or across short job bursts.

Even when devfreq->suspend_freq is non-zero, panthor_device_suspend() sets PANTHOR_DEVICE_PM_STATE_SUSPENDING before panthor_sched_suspend(), panthor_fw_suspend(), panthor_mmu_suspend(), panthor_gpu_suspend() and panthor_pwr_suspend(). Any rate change skipped during that window has its devfreq->resume_freq overwritten when panthor_devfreq_suspend() -> devfreq_suspend_device() calls devfreq_set_target(devfreq, devfreq->suspend_freq, 0), so devfreq_resume_device() restores suspend_freq instead of the requested rate.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Arun Iyengar
Quoted 16 lines · On Tue, Nov 10, 2026 at 07:15:00AM +0000, Arun Iyengar via B4 Relay wrote:

From: Arun Iyengar <[email protected]>

From the start of panthor_device_suspend() until panthor_device_resume() has enabled the clocks again, the GPU clocks are off or about to be, and the GPU power domain may be down. devfreq can still call ->target() in that window: a write to min_freq or max_freq in sysfs, a thermal cooling state change or a PM QoS request all end up in update_devfreq() whatever the device state.

With the RK3588 GPU core clock switched to the firmware-managed SCMI_CLK_GPU (needed to reach the PVTPLL rates of the upper OPPs), such a call is fatal. To set a PVTPLL rate TF-A programs registers in the GPU GRF, which sit in the GPU power domain, and with the domain off that access ends in an asynchronous SError and a kernel panic, for example after

Quoted 3 lines

echo 1000000000 > /sys/class/devfreq/fb000000.gpu/min_freq

while the GPU is runtime suspended.

Maybe what needs to be done is to disable the clock when the power domain goes down.

Quoted 53 lines

Only apply a rate to the hardware while the device is resuming or active, plus the suspend frequency that devfreq_suspend_device() applies from the suspend path while the GPU is still powered. Other requests are still accepted and recorded by devfreq, which applies them on resume or at the next governor update.

The RFC this is based on (Jonas Karlman's panthor SCMI clock change, carried in Armbian's linux-rockchip pull request 380) checked the runtime PM status instead. That
status is still RPM_SUSPENDING after panthor_devfreq_suspend() has run, which leaves a window where the rate can be raised again right before the power domain goes down. The devfreq suspend count cannot be used
either: panthor's first runtime resume calls devfreq_resume_device() without a matching suspend, so the count is -1 while the GPU is active and 0 while it is suspended.

---
Tested on an Orange Pi 5 (RK3588S) with the GPU core clock switched to SCMI_CLK_GPU and a 200 MHz opp-suspend (the configuration from the RFC, not yet in mainline): writing min_freq while the GPU was runtime suspended no longer ends in an SError, the request takes effect at the next job (1 GHz measured), max_freq likewise; 200 suspend/resume cycles with short GPU jobs and a 3 minute sustained load ran without faults. With the mainline device tree (CRU clock, no suspend OPP) the SError is not reachable and the guard only skips rate changes while the GPU is powered down.

This patch was developed with Claude (Anthropic) as a coding assistant:
it wrote the code and the commit message, and I reviewed and tested it as described above.

---
 drivers/gpu/drm/panthor/panthor_devfreq.c | 42 ++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)
drivers/gpu/drm/panthor/panthor_devfreq.c +8 -0
index 2249b41ca4af..cab86e9c9d46 100644
@@ -60,20 +60,56 @@ static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq)
 	pdevfreq->time_last_update = now;
 }
 
+/*
+ * Whether a new rate can be applied to the hardware now.
+ *
+ * From the start of panthor_device_suspend() until panthor_device_resume()
+ * has enabled the clocks again, the GPU clocks are about to be disabled
+ * or are off, and the GPU power domain may be down. devfreq can still
+ * call ->target() in that window: sysfs
+ * min_freq/max_freq writes, thermal cooling and PM QoS requests all end

drivers/gpu/drm/panthor/panthor_devfreq.c static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) · line 80

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +10 -0
+ * up in update_devfreq() whatever the device state.
+ *
+ * Changing the rate then is pointless, and harmful where the core clock
+ * is managed by firmware: on RK3588, setting SCMI_CLK_GPU to a PVTPLL
+ * rate with the GPU power domain off makes TF-A access the powered-down
+ * GPU GRF, which ends in an asynchronous SError. So while the device is
+ * suspending only the suspend frequency, applied by
+ * devfreq_suspend_device(), reaches the hardware, and nothing does
+ * while it is suspended. devfreq records the other requests: they are
+ * applied on resume or at the next governor update.

Can this comment be rewritten to be less verbose?

Quoted 22 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +18 -0
+ */
+static bool panthor_devfreq_can_set_rate(struct panthor_device *ptdev,
+					 unsigned long freq)
+{
+	struct devfreq *devfreq = ptdev->devfreq ? ptdev->devfreq->devfreq : NULL;
+
+	switch (atomic_read(&ptdev->pm.state)) {
+	case PANTHOR_DEVICE_PM_STATE_SUSPENDED:
+		return false;
+	case PANTHOR_DEVICE_PM_STATE_SUSPENDING:
+		return devfreq && devfreq->suspend_freq &&
+		       freq == devfreq->suspend_freq;
+	default:
+		return true;
+	}
+}
+
 static int panthor_devfreq_target(struct device *dev, unsigned long *freq,
 				  u32 flags)
 {
+	struct panthor_device *ptdev = dev_get_drvdata(dev);
 	struct dev_pm_opp *opp;

drivers/gpu/drm/panthor/panthor_devfreq.c static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) · line 110

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +2 -2
-	int err;
 
 	opp = devfreq_recommended_opp(dev, freq, flags);
 	if (IS_ERR(opp))
 		return PTR_ERR(opp);
 	dev_pm_opp_put(opp);
 
-	err = dev_pm_opp_set_rate(dev, *freq);
+	if (!panthor_devfreq_can_set_rate(ptdev, *freq))
+		return 0;

Hmm, why not return an error here? -EINVAL?

Quoted 11 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +1 -2
-	return err;
+	return dev_pm_opp_set_rate(dev, *freq);
 }
 
 static void panthor_devfreq_reset(struct panthor_devfreq *pdevfreq)
 
---

base-commit: 8deaade4a5a4ff635df91e246e42139ab2b33f6d
change-id: 20261110-panthor-suspend-clk-11d5604fe5be

Quoted 5 lines

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>

Best regards,
Liviu

Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
maintainer
#3 lore raw
In reply to Arun Iyengar
Quoted 77 lines · On Tue, 10 Nov 2026 07:15:00 +0000 Arun Iyengar via B4 Relay…

From: Arun Iyengar <[email protected]>

From the start of panthor_device_suspend() until panthor_device_resume() has enabled the clocks again, the GPU clocks are off or about to be, and the GPU power domain may be down. devfreq can still call ->target() in that window: a write to min_freq or max_freq in sysfs, a thermal cooling state change or a PM QoS request all end up in update_devfreq() whatever the device state.

With the RK3588 GPU core clock switched to the firmware-managed SCMI_CLK_GPU (needed to reach the PVTPLL rates of the upper OPPs), such a call is fatal. To set a PVTPLL rate TF-A programs registers in the GPU GRF, which sit in the GPU power domain, and with the domain off that access ends in an asynchronous SError and a kernel panic, for example after

echo 1000000000 > /sys/class/devfreq/fb000000.gpu/min_freq

while the GPU is runtime suspended.

Only apply a rate to the hardware while the device is resuming or active, plus the suspend frequency that devfreq_suspend_device() applies from the suspend path while the GPU is still powered. Other requests are still accepted and recorded by devfreq, which applies them on resume or at the next governor update.

The RFC this is based on (Jonas Karlman's panthor SCMI clock change, carried in Armbian's linux-rockchip pull request 380) checked the runtime PM status instead. That
status is still RPM_SUSPENDING after panthor_devfreq_suspend() has run, which leaves a window where the rate can be raised again right before the power domain goes down. The devfreq suspend count cannot be used
either: panthor's first runtime resume calls devfreq_resume_device() without a matching suspend, so the count is -1 while the GPU is active and 0 while it is suspended.

---
Tested on an Orange Pi 5 (RK3588S) with the GPU core clock switched to SCMI_CLK_GPU and a 200 MHz opp-suspend (the configuration from the RFC, not yet in mainline): writing min_freq while the GPU was runtime suspended no longer ends in an SError, the request takes effect at the next job (1 GHz measured), max_freq likewise; 200 suspend/resume cycles with short GPU jobs and a 3 minute sustained load ran without faults. With the mainline device tree (CRU clock, no suspend OPP) the SError is not reachable and the guard only skips rate changes while the GPU is powered down.

This patch was developed with Claude (Anthropic) as a coding assistant:
it wrote the code and the commit message, and I reviewed and tested it as described above.

---
 drivers/gpu/drm/panthor/panthor_devfreq.c | 42 ++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)
drivers/gpu/drm/panthor/panthor_devfreq.c +13 -0
index 2249b41ca4af..cab86e9c9d46 100644
@@ -60,20 +60,56 @@ static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq)
 	pdevfreq->time_last_update = now;
 }
 
+/*
+ * Whether a new rate can be applied to the hardware now.
+ *
+ * From the start of panthor_device_suspend() until panthor_device_resume()
+ * has enabled the clocks again, the GPU clocks are about to be disabled
+ * or are off, and the GPU power domain may be down. devfreq can still
+ * call ->target() in that window: sysfs
+ * min_freq/max_freq writes, thermal cooling and PM QoS requests all end
+ * up in update_devfreq() whatever the device state.
+ *
+ * Changing the rate then is pointless, and harmful where the core clock
+ * is managed by firmware: on RK3588, setting SCMI_CLK_GPU to a PVTPLL
+ * rate with the GPU power domain off makes TF-A access the powered-down

drivers/gpu/drm/panthor/panthor_devfreq.c static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) · line 85

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +10 -0
+ * GPU GRF, which ends in an asynchronous SError. So while the device is
+ * suspending only the suspend frequency, applied by
+ * devfreq_suspend_device(), reaches the hardware, and nothing does
+ * while it is suspended. devfreq records the other requests: they are
+ * applied on resume or at the next governor update.
+ */
+static bool panthor_devfreq_can_set_rate(struct panthor_device *ptdev,
+					 unsigned long freq)
+{
+	struct devfreq *devfreq = ptdev->devfreq ? ptdev->devfreq->devfreq : NULL;

I'd rather use devfreq::suspend_count to detect if the devfreq component is suspended, since it gives us a better granularity than panthor_device::pm::state.

drivers/gpu/drm/panthor/panthor_devfreq.c static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) · line 93

Quoted 8 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +8 -0
+
+	switch (atomic_read(&ptdev->pm.state)) {
+	case PANTHOR_DEVICE_PM_STATE_SUSPENDED:
+		return false;
+	case PANTHOR_DEVICE_PM_STATE_SUSPENDING:
+		return devfreq && devfreq->suspend_freq &&
+		       freq == devfreq->suspend_freq;
+	default:

When PANTHOR_DEVICE_PM_STATE_SUSPENDING is set, not all HW components have been clocked/put out of reset. It's probably okay for power domains, because those deps are described through device links, and the dependencies are resumed by PM core before the panthor PM hooks are called, but this is fragile, and that's another reason I think we shouldn't rely on panthor_device::pm::state for that, but on devfreq::suspend_count (or any other internal panthor_devfreq state that tracks the panthor_devfreq component status).

Quoted 9 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +5 -0
+		return true;
+	}
+}
+
 static int panthor_devfreq_target(struct device *dev, unsigned long *freq,
 				  u32 flags)
 {
+	struct panthor_device *ptdev = dev_get_drvdata(dev);
 	struct dev_pm_opp *opp;

drivers/gpu/drm/panthor/panthor_devfreq.c static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) · line 110

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +2 -2
-	int err;
 
 	opp = devfreq_recommended_opp(dev, freq, flags);
 	if (IS_ERR(opp))
 		return PTR_ERR(opp);
 	dev_pm_opp_put(opp);
 
-	err = dev_pm_opp_set_rate(dev, *freq);
+	if (!panthor_devfreq_can_set_rate(ptdev, *freq))
+		return 0;

If you return zero here, the new frequency is assumed to be applied, and it messes up with the devfreq state (::previous_freq for instance) that was set on devfreq_suspend_device(). If we assume that rate changes should be rejected while we're suspended, this should probably return an error.

drivers/gpu/drm/panthor/panthor_devfreq.c static void panthor_devfreq_update_utilization(struct panthor_devfreq *pdevfreq) · line 112

Quoted 3 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +1 -1
-	return err;
+	return dev_pm_opp_set_rate(dev, *freq);

There's another race between devfreq_suspend_device(), which will restore the ::resume_freq, and a concurrent devfreq_update_target() call that's here to set a new rate. Using devfreq::suspend_count won't help, because this atomic is decremented before the resume_freq is applied, and the devfreq lock is only taken around the devfreq_set_target() call.

Honestly, it feels like we're trying to address the problem at the wrong level. I would expect governors to stop calling devfreq_update_target() between the DEVFREQ_GOV_SUSPEND and DEVFREQ_GOV_RESUME events (which should probably be sent before suspend_count is incremented and the suspend freq is applied). And I'd also expect some kind of locking/synchronization to prevent races between devfreq_update_target() and devfreq_{suspend,resume}_device().

Quoted 13 lines
drivers/gpu/drm/panthor/panthor_devfreq.c +0 -1
 }
 
 static void panthor_devfreq_reset(struct panthor_devfreq *pdevfreq)
 
---

base-commit: 8deaade4a5a4ff635df91e246e42139ab2b33f6d
change-id: 20261110-panthor-suspend-clk-11d5604fe5be

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>