* [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace
@ 2026-11-20 10:40 Christian Gmeiner
2026-11-20 10:40 ` [PATCH 1/7] drm/etnaviv: Don't set up a GPU on resume that is not initialized Christian Gmeiner
` (6 more replies)
0 siblings, 7 replies; 9+ messages in thread
From: Christian Gmeiner @ 2026-11-20 10:40 UTC (permalink / raw)
To: Lucas Stach, Russell King, Christian Gmeiner, David Airlie,
Simona Vetter, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: kernel-dev, etnaviv, dri-devel, linux-kernel, Christian Gmeiner,
stable, stable+noautosel
Userspace that converts GPU cycle counts to time, like Mesa, needs the
core clock rate a job ran at. The thermal cooling device can change
this rate at any time, and userspace has no way to learn about it.
This series adds a CLOCK_HISTORY ioctl that returns the last clock
rate changes of a GPU with their timestamps. Userspace compares them
with the fence timestamp of a job and knows the rate the job ran at.
A trace event reports the same changes.
For this a job must run at a single rate. Today the cooling device
changes the clock while a job is running, which can also hang the GPU.
On a GC7000 rev 6009 I toggled the cooling state every 200 ms during
glmark2 runs and got 25 hang recoveries in 24 runs. With this series I
got none in 34 runs. So the first patches fix the cooling device:
- Patch 1 fixes the GPU state after a failed init and after unbind.
Patch 4 uses this state to decide if the clock can be changed.
- Patches 2-4 change the order of the cooling device registration,
skip requests that keep the clock and close a race with runtime
suspend.
- Patch 5 makes a clock change wait for the queued jobs.
- Patch 6 adds the trace event.
- Patch 7 keeps the last 256 changes in a ring buffer and adds the
ioctl to read them.
Tested on GC7000 rev 6214 (i.MX8MQ), and with backports to 6.6 on
GC7000 rev 6009 (i.MX8QM) and to 6.12 on GC2000 rev 5108 (i.MX6Q,
FSCALE path).
Link to the Mesa MR implementing the userspace side:
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/46538
Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
---
Christian Gmeiner (7):
drm/etnaviv: Don't set up a GPU on resume that is not initialized
drm/etnaviv: Register the cooling device last and unregister it first
drm/etnaviv: Skip cooling requests that keep the clock
drm/etnaviv: Don't change the clock of a suspended GPU
drm/etnaviv: Change the GPU clock between jobs
drm/etnaviv: Add a GPU clock change trace event
drm/etnaviv: Add an ioctl to read back GPU clock changes
drivers/gpu/drm/etnaviv/Makefile | 5 +-
drivers/gpu/drm/etnaviv/etnaviv_drv.c | 20 ++-
drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 240 ++++++++++++++++++++++---
drivers/gpu/drm/etnaviv/etnaviv_gpu.h | 12 ++
drivers/gpu/drm/etnaviv/etnaviv_trace.h | 62 +++++++
drivers/gpu/drm/etnaviv/etnaviv_trace_points.c | 9 +
include/uapi/drm/etnaviv_drm.h | 62 ++++++-
7 files changed, 378 insertions(+), 32 deletions(-)
---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261116-etnaviv-gpu-freq-trace-52e421b3f72d
Best regards,
--
Christian Gmeiner <cgmeiner@igalia.com>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 1/7] drm/etnaviv: Don't set up a GPU on resume that is not initialized
2026-11-20 10:40 [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace Christian Gmeiner
@ 2026-11-20 10:40 ` Christian Gmeiner
2026-11-20 10:53 ` sashiko-bot
2026-11-20 10:40 ` [PATCH 2/7] drm/etnaviv: Register the cooling device last and unregister it first Christian Gmeiner
` (5 subsequent siblings)
6 siblings, 1 reply; 9+ messages in thread
From: Christian Gmeiner @ 2026-11-20 10:40 UTC (permalink / raw)
To: Lucas Stach, Russell King, Christian Gmeiner, David Airlie,
Simona Vetter, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: kernel-dev, etnaviv, dri-devel, linux-kernel, Christian Gmeiner,
stable
From: Christian Gmeiner <cgmeiner@igalia.com>
etnaviv_gpu_init(..) can fail, for example for a VG core with FE2.0,
which is ignored on purpose. The device stays bound and is runtime
suspended and resumed later.
etnaviv_gpu_rpm_suspend(..) sets the state to identified without
looking at the old state. So the next resume programs the clock and
runs etnaviv_gpu_hw_init(..) on a GPU that is not initialized. An
unbound GPU keeps the identified state as well, so the next bind sets
it up before it is reset.
Go back to the unknown state when etnaviv_gpu_init(..) fails and in
etnaviv_gpu_unbind(..), and keep it in etnaviv_gpu_rpm_suspend(..).
Seen on an i.MX6Q with its ignored GC355, and with its GC320 on every
bind after the first.
Fixes: 647d817d8071 ("drm/etnaviv: better track GPU state")
Cc: stable@vger.kernel.org # 6.6+
Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
---
drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
index 6a4aab75ebba..2a15ae6135f5 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
@@ -951,6 +951,8 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
return 0;
fail:
+ /* the GPU is not used, a resume must not set it up */
+ gpu->state = ETNA_GPU_STATE_UNKNOWN;
pm_runtime_mark_last_busy(gpu->dev);
pm_put:
pm_runtime_put_autosuspend(gpu->dev);
@@ -1874,6 +1876,9 @@ static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
etnaviv_gpu_clk_disable(gpu);
}
+ /* the GPU is not initialized any more, a resume must not set it up */
+ gpu->state = ETNA_GPU_STATE_UNKNOWN;
+
if (gpu->mmu_context)
etnaviv_iommu_context_put(gpu->mmu_context);
@@ -2019,7 +2024,8 @@ static int etnaviv_gpu_rpm_suspend(struct device *dev)
etnaviv_gpu_hw_suspend(gpu);
- gpu->state = ETNA_GPU_STATE_IDENTIFIED;
+ if (gpu->state != ETNA_GPU_STATE_UNKNOWN)
+ gpu->state = ETNA_GPU_STATE_IDENTIFIED;
return etnaviv_gpu_clk_disable(gpu);
}
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 2/7] drm/etnaviv: Register the cooling device last and unregister it first
2026-11-20 10:40 [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace Christian Gmeiner
2026-11-20 10:40 ` [PATCH 1/7] drm/etnaviv: Don't set up a GPU on resume that is not initialized Christian Gmeiner
@ 2026-11-20 10:40 ` Christian Gmeiner
2026-11-20 10:40 ` [PATCH 3/7] drm/etnaviv: Skip cooling requests that keep the clock Christian Gmeiner
` (4 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Christian Gmeiner @ 2026-11-20 10:40 UTC (permalink / raw)
To: Lucas Stach, Russell King, Christian Gmeiner, David Airlie,
Simona Vetter, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: kernel-dev, etnaviv, dri-devel, linux-kernel, Christian Gmeiner,
stable
From: Christian Gmeiner <cgmeiner@igalia.com>
etnaviv_gpu_bind(..) registers the cooling device before the workqueue,
the scheduler and the clocks are set up, and etnaviv_gpu_unbind(..)
unregisters it after they are gone. So a cooling request can come in
while the GPU is not set up yet or already torn down.
With CONFIG_PM the GPU is runtime suspended at these points and the
request only stores the new state. Without CONFIG_PM
pm_runtime_suspended(..) always returns false and the request accesses
a GPU whose clocks are off.
Register the cooling device as the last step of bind and unregister it
as the first step of unbind. A following patch makes cooling requests
wait for the queued jobs and needs this order too.
Fixes: bcdfb5e56dc5 ("drm/etnaviv: add etnaviv cooling device")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
---
drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 42 ++++++++++++++++++-----------------
1 file changed, 22 insertions(+), 20 deletions(-)
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
index 2a15ae6135f5..2a56bdf2510a 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
@@ -1808,19 +1808,9 @@ static int etnaviv_gpu_bind(struct device *dev, struct device *master,
struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
int ret;
- if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) {
- gpu->cooling = thermal_of_cooling_device_register(dev->of_node, 0,
- dev_name(dev),
- gpu, &cooling_ops);
- if (IS_ERR(gpu->cooling))
- return PTR_ERR(gpu->cooling);
- }
-
gpu->wq = alloc_ordered_workqueue(dev_name(dev), 0);
- if (!gpu->wq) {
- ret = -ENOMEM;
- goto out_thermal;
- }
+ if (!gpu->wq)
+ return -ENOMEM;
ret = etnaviv_sched_init(gpu);
if (ret)
@@ -1840,20 +1830,32 @@ static int etnaviv_gpu_bind(struct device *dev, struct device *master,
INIT_WORK(&gpu->sync_point_work, sync_point_worker);
init_waitqueue_head(&gpu->fence_event);
+ if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) {
+ gpu->cooling = thermal_of_cooling_device_register(dev->of_node, 0,
+ dev_name(dev),
+ gpu, &cooling_ops);
+ if (IS_ERR(gpu->cooling)) {
+ ret = PTR_ERR(gpu->cooling);
+ gpu->cooling = NULL;
+ goto out_clk;
+ }
+ }
+
priv->gpu[priv->num_gpus++] = gpu;
return 0;
+out_clk:
+ gpu->drm = NULL;
+ if (!IS_ENABLED(CONFIG_PM))
+ etnaviv_gpu_clk_disable(gpu);
+
out_sched:
etnaviv_sched_fini(gpu);
out_workqueue:
destroy_workqueue(gpu->wq);
-out_thermal:
- if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
- thermal_cooling_device_unregister(gpu->cooling);
-
return ret;
}
@@ -1864,6 +1866,10 @@ static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
DBG("%s", dev_name(gpu->dev));
+ if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
+ thermal_cooling_device_unregister(gpu->cooling);
+ gpu->cooling = NULL;
+
destroy_workqueue(gpu->wq);
etnaviv_sched_fini(gpu);
@@ -1887,10 +1893,6 @@ static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
gpu->drm = NULL;
xa_destroy(&gpu->user_fences);
-
- if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL))
- thermal_cooling_device_unregister(gpu->cooling);
- gpu->cooling = NULL;
}
static const struct component_ops gpu_ops = {
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/7] drm/etnaviv: Skip cooling requests that keep the clock
2026-11-20 10:40 [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace Christian Gmeiner
2026-11-20 10:40 ` [PATCH 1/7] drm/etnaviv: Don't set up a GPU on resume that is not initialized Christian Gmeiner
2026-11-20 10:40 ` [PATCH 2/7] drm/etnaviv: Register the cooling device last and unregister it first Christian Gmeiner
@ 2026-11-20 10:40 ` Christian Gmeiner
2026-11-20 10:40 ` [PATCH 4/7] drm/etnaviv: Don't change the clock of a suspended GPU Christian Gmeiner
` (3 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Christian Gmeiner @ 2026-11-20 10:40 UTC (permalink / raw)
To: Lucas Stach, Russell King, Christian Gmeiner, David Airlie,
Simona Vetter, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: kernel-dev, etnaviv, dri-devel, linux-kernel, Christian Gmeiner
From: Christian Gmeiner <cgmeiner@igalia.com>
The thermal core does not compare a new cooling state with the current
one. The fair_share governor sets the state on every zone update, and
the step_wise and bang_bang governors and sysfs can repeat a state as
well. Return early in this case instead of programming the same clock
again.
Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
---
drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
index 2a56bdf2510a..0a28e21feb8b 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
@@ -1786,6 +1786,11 @@ etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
struct etnaviv_gpu *gpu = cdev->devdata;
mutex_lock(&gpu->lock);
+ if (state == gpu->freq_scale) {
+ mutex_unlock(&gpu->lock);
+ return 0;
+ }
+
gpu->freq_scale = state;
if (!pm_runtime_suspended(gpu->dev))
etnaviv_gpu_update_clock(gpu);
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 4/7] drm/etnaviv: Don't change the clock of a suspended GPU
2026-11-20 10:40 [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace Christian Gmeiner
` (2 preceding siblings ...)
2026-11-20 10:40 ` [PATCH 3/7] drm/etnaviv: Skip cooling requests that keep the clock Christian Gmeiner
@ 2026-11-20 10:40 ` Christian Gmeiner
2026-11-20 10:40 ` [PATCH 5/7] drm/etnaviv: Change the GPU clock between jobs Christian Gmeiner
` (2 subsequent siblings)
6 siblings, 0 replies; 9+ messages in thread
From: Christian Gmeiner @ 2026-11-20 10:40 UTC (permalink / raw)
To: Lucas Stach, Russell King, Christian Gmeiner, David Airlie,
Simona Vetter, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: kernel-dev, etnaviv, dri-devel, linux-kernel, Christian Gmeiner,
stable
From: Christian Gmeiner <cgmeiner@igalia.com>
pm_runtime_suspended(..) only looks at the runtime PM status and takes
no reference. It returns false while a suspend or resume is in flight,
and nothing stops a suspend from starting right after the check. So the
cooling device can write the clock while the GPU turns its clocks off.
Check gpu->state under gpu->lock instead. The state is RESET or higher
only after the runtime resume or the init reset has programmed the
clock. A GPU that is suspended or not reset yet only stores the new
scale, and the next resume or the init reset applies it.
Take gpu->lock in every place that sets the state below RESET:
etnaviv_gpu_rpm_suspend(..), etnaviv_gpu_unbind(..) and the failure
path of etnaviv_gpu_init(..). etnaviv_gpu_init(..) also keeps
the lock from its state change until the init reset is done. Otherwise
a cooling request during the reset is not applied until the next
resume.
Fixes: bcdfb5e56dc5 ("drm/etnaviv: add etnaviv cooling device")
Cc: stable@vger.kernel.org # 6.6+
Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
---
drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
index 0a28e21feb8b..c819f5fc4418 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
@@ -886,9 +886,12 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
(gpu->identity.minor_features10 & chipMinorFeatures10_SECURITY_AHB))
gpu->sec_mode = ETNA_SEC_KERNEL;
+ /* the cooling device may change the clock from here on */
+ mutex_lock(&gpu->lock);
gpu->state = ETNA_GPU_STATE_IDENTIFIED;
-
ret = etnaviv_hw_reset(gpu);
+ mutex_unlock(&gpu->lock);
+
if (ret) {
dev_err(gpu->dev, "GPU reset failed\n");
goto fail;
@@ -952,7 +955,9 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
fail:
/* the GPU is not used, a resume must not set it up */
+ mutex_lock(&gpu->lock);
gpu->state = ETNA_GPU_STATE_UNKNOWN;
+ mutex_unlock(&gpu->lock);
pm_runtime_mark_last_busy(gpu->dev);
pm_put:
pm_runtime_put_autosuspend(gpu->dev);
@@ -1792,7 +1797,8 @@ etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
}
gpu->freq_scale = state;
- if (!pm_runtime_suspended(gpu->dev))
+ /* a suspended or not yet reset GPU picks up freq_scale later */
+ if (gpu->state >= ETNA_GPU_STATE_RESET)
etnaviv_gpu_update_clock(gpu);
mutex_unlock(&gpu->lock);
@@ -1888,7 +1894,9 @@ static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
}
/* the GPU is not initialized any more, a resume must not set it up */
+ mutex_lock(&gpu->lock);
gpu->state = ETNA_GPU_STATE_UNKNOWN;
+ mutex_unlock(&gpu->lock);
if (gpu->mmu_context)
etnaviv_iommu_context_put(gpu->mmu_context);
@@ -2031,8 +2039,10 @@ static int etnaviv_gpu_rpm_suspend(struct device *dev)
etnaviv_gpu_hw_suspend(gpu);
+ mutex_lock(&gpu->lock);
if (gpu->state != ETNA_GPU_STATE_UNKNOWN)
gpu->state = ETNA_GPU_STATE_IDENTIFIED;
+ mutex_unlock(&gpu->lock);
return etnaviv_gpu_clk_disable(gpu);
}
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5/7] drm/etnaviv: Change the GPU clock between jobs
2026-11-20 10:40 [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace Christian Gmeiner
` (3 preceding siblings ...)
2026-11-20 10:40 ` [PATCH 4/7] drm/etnaviv: Don't change the clock of a suspended GPU Christian Gmeiner
@ 2026-11-20 10:40 ` Christian Gmeiner
2026-11-20 10:40 ` [PATCH 6/7] drm/etnaviv: Add a GPU clock change trace event Christian Gmeiner
2026-11-20 10:40 ` [PATCH 7/7] drm/etnaviv: Add an ioctl to read back GPU clock changes Christian Gmeiner
6 siblings, 0 replies; 9+ messages in thread
From: Christian Gmeiner @ 2026-11-20 10:40 UTC (permalink / raw)
To: Lucas Stach, Russell King, Christian Gmeiner, David Airlie,
Simona Vetter, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: kernel-dev, etnaviv, dri-devel, linux-kernel, Christian Gmeiner,
stable+noautosel
From: Christian Gmeiner <cgmeiner@igalia.com>
Thermal throttling changes the core clock while a job is running. This
can hang the GPU. On a GC7000 rev 6009 I toggled the cooling state
every 200 ms during glmark2 runs and got 25 hang recoveries in 24 runs.
With this change I got none in 34 runs.
Hold back new jobs in etnaviv_gpu_submit(..) and wait for the queued
ones before changing the clock. A hang recovery counts the dropped
jobs as completed, so it ends the wait too. If the GPU suspends while
we wait, the resume applies the new scale.
The wait gives up after one job timeout. Then the clock changes while
a job is running, as it did before. The timeout is needed because a
hang recovery stops the scheduler, which waits for the run_job work,
and this work can be the job held back in etnaviv_gpu_submit(..).
drm_sched_wqueue_stop(..) is no alternative. The hang recovery uses
the same pause_submit flag and resubmits jobs without looking at it.
A cooling request now blocks until the GPU is drained, up to one job
timeout, and with it the thermal zone if a governor made the request.
The held back submit blocks the scheduler workqueue, which also cleans
up finished jobs. So the IRQ handler wakes the waiting clock change
itself.
Fixes: bcdfb5e56dc5 ("drm/etnaviv: add etnaviv cooling device")
Cc: <stable+noautosel@kernel.org> # needs store_release_wake_up(..)
Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
---
drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 51 ++++++++++++++++++++++++++++++++++-
drivers/gpu/drm/etnaviv/etnaviv_gpu.h | 1 +
2 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
index c819f5fc4418..9b112d053abd 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
@@ -1452,6 +1452,14 @@ struct dma_fence *etnaviv_gpu_submit(struct etnaviv_gem_submit *submit)
mutex_lock(&gpu->lock);
+ /* a clock change waits for the queued jobs, don't add new ones */
+ while (gpu->clock_change_pending) {
+ mutex_unlock(&gpu->lock);
+ wait_var_event(&gpu->clock_change_pending,
+ !READ_ONCE(gpu->clock_change_pending));
+ mutex_lock(&gpu->lock);
+ }
+
gpu_fence = etnaviv_gpu_fence_alloc(gpu);
if (!gpu_fence) {
for (i = 0; i < nr_events; i++)
@@ -1542,6 +1550,13 @@ void etnaviv_gpu_recover_hang(struct etnaviv_gem_submit *submit)
event_free(gpu, i);
spin_unlock(&gpu->event_spinlock);
+ /*
+ * The dropped jobs never complete, count them as completed so a clock
+ * change does not wait for them. Resubmitted jobs get new fences.
+ */
+ WRITE_ONCE(gpu->completed_fence, gpu->next_fence);
+ wake_up_all(&gpu->fence_event);
+
etnaviv_gpu_hw_init(gpu);
mutex_unlock(&gpu->lock);
@@ -1652,12 +1667,20 @@ static irqreturn_t irq_handler(int irq, void *data)
* we can end up processing event 0 first, then 1.
*/
if (fence_after(fence->seqno, gpu->completed_fence))
- gpu->completed_fence = fence->seqno;
+ WRITE_ONCE(gpu->completed_fence, fence->seqno);
dma_fence_signal_timestamp(fence, now);
event_free(gpu, event);
}
+ /*
+ * A clock change waits on fence_event for the queued jobs.
+ * Job cleanup wakes it, but a submit held back for the clock
+ * change blocks the ordered scheduler workqueue and with it
+ * the cleanup. So wake it from here as well.
+ */
+ wake_up_all(&gpu->fence_event);
+
ret = IRQ_HANDLED;
}
@@ -1789,6 +1812,7 @@ etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
unsigned long state)
{
struct etnaviv_gpu *gpu = cdev->devdata;
+ u32 fence;
mutex_lock(&gpu->lock);
if (state == gpu->freq_scale) {
@@ -1798,8 +1822,33 @@ etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
gpu->freq_scale = state;
/* a suspended or not yet reset GPU picks up freq_scale later */
+ if (gpu->state < ETNA_GPU_STATE_RESET) {
+ mutex_unlock(&gpu->lock);
+ return 0;
+ }
+
+ /*
+ * Change the clock between jobs: hold back new jobs and wait for the
+ * queued ones, as a job that runs across a clock change can hang the
+ * GPU. The wait must be bounded: a hang recovery stops the scheduler,
+ * which waits for the job held back in etnaviv_gpu_submit(), and that
+ * job waits for the clock change. So give up after one job timeout and
+ * change the clock anyway.
+ */
+ WRITE_ONCE(gpu->clock_change_pending, true);
+ fence = gpu->next_fence;
+ mutex_unlock(&gpu->lock);
+
+ if (!wait_event_timeout(gpu->fence_event,
+ !fence_after(fence, READ_ONCE(gpu->completed_fence)),
+ gpu->sched.timeout))
+ dev_dbg(gpu->dev, "clock change while jobs are running\n");
+
+ mutex_lock(&gpu->lock);
+ /* the GPU may have suspended while waiting, the resume applies it */
if (gpu->state >= ETNA_GPU_STATE_RESET)
etnaviv_gpu_update_clock(gpu);
+ store_release_wake_up(&gpu->clock_change_pending, false);
mutex_unlock(&gpu->lock);
return 0;
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.h b/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
index e89d591d7404..dddeabce6eb4 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
@@ -136,6 +136,7 @@ struct etnaviv_gpu {
u32 next_fence;
u32 completed_fence;
wait_queue_head_t fence_event;
+ bool clock_change_pending;
u64 fence_context;
spinlock_t fence_spinlock;
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 6/7] drm/etnaviv: Add a GPU clock change trace event
2026-11-20 10:40 [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace Christian Gmeiner
` (4 preceding siblings ...)
2026-11-20 10:40 ` [PATCH 5/7] drm/etnaviv: Change the GPU clock between jobs Christian Gmeiner
@ 2026-11-20 10:40 ` Christian Gmeiner
2026-11-20 10:40 ` [PATCH 7/7] drm/etnaviv: Add an ioctl to read back GPU clock changes Christian Gmeiner
6 siblings, 0 replies; 9+ messages in thread
From: Christian Gmeiner @ 2026-11-20 10:40 UTC (permalink / raw)
To: Lucas Stach, Russell King, Christian Gmeiner, David Airlie,
Simona Vetter, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: kernel-dev, etnaviv, dri-devel, linux-kernel, Christian Gmeiner
From: Christian Gmeiner <cgmeiner@igalia.com>
The GPU core clock changes via the thermal cooling device, on runtime
resume and after a GPU reset. Userspace that converts GPU cycle counts
to time (like the Mesa GPU tracepoints) does not see these changes.
Add a trace event to etnaviv_gpu_update_clock(..), which all three
paths use. It fires when the rate changes and has the old and the new
rate, the time right before and after the switch, and flags. The
clk_set_rate trace events do not cover this: they do not fire on the
FSCALE path and they do not tell the reason for a change.
The flags tell where the change was applied: on resume, on reset, or
without a flag on a running GPU. A change that was made while jobs
were still running is flagged as busy. This replaces the debug message
for this case. The flags are in the uAPI header, as tools need their
values to decode the event.
Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
---
drivers/gpu/drm/etnaviv/Makefile | 5 ++-
drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 43 ++++++++++++++----
drivers/gpu/drm/etnaviv/etnaviv_gpu.h | 2 +
drivers/gpu/drm/etnaviv/etnaviv_trace.h | 62 ++++++++++++++++++++++++++
drivers/gpu/drm/etnaviv/etnaviv_trace_points.c | 9 ++++
include/uapi/drm/etnaviv_drm.h | 19 ++++++++
6 files changed, 131 insertions(+), 9 deletions(-)
diff --git a/drivers/gpu/drm/etnaviv/Makefile b/drivers/gpu/drm/etnaviv/Makefile
index 903101e8751a..4fb9b83f2808 100644
--- a/drivers/gpu/drm/etnaviv/Makefile
+++ b/drivers/gpu/drm/etnaviv/Makefile
@@ -15,6 +15,9 @@ etnaviv-y := \
etnaviv_mmu.o \
etnaviv_perfmon.o \
etnaviv_flop_reset.o \
- etnaviv_sched.o
+ etnaviv_sched.o \
+ etnaviv_trace_points.o
obj-$(CONFIG_DRM_ETNAVIV) += etnaviv.o
+
+CFLAGS_etnaviv_trace_points.o := -I$(src)
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
index 9b112d053abd..03075f289c69 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
@@ -25,6 +25,7 @@
#include "etnaviv_mmu.h"
#include "etnaviv_perfmon.h"
#include "etnaviv_sched.h"
+#include "etnaviv_trace.h"
#include "common.xml.h"
#include "state.xml.h"
#include "state_hi.xml.h"
@@ -505,14 +506,24 @@ static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
}
-static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu)
+/* begin_ns is when the switch, or the restart that includes it, began */
+static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu, u32 flags,
+ u64 begin_ns)
{
+ unsigned long rate_before = gpu->core_rate;
+ bool scaled = gpu->freq_scale != gpu->applied_scale;
+ u64 end_ns;
+
if (gpu->identity.minor_features2 &
chipMinorFeatures2_DYNAMIC_FREQUENCY_SCALING) {
clk_set_rate(gpu->clk_core,
gpu->base_rate_core >> gpu->freq_scale);
+ end_ns = ktime_get_ns();
clk_set_rate(gpu->clk_shader,
gpu->base_rate_shader >> gpu->freq_scale);
+ /* a rate read back may call firmware, do it only on a change */
+ if (!gpu->core_rate || scaled)
+ gpu->core_rate = clk_get_rate(gpu->clk_core);
} else {
unsigned int fscale = 1 << (6 - gpu->freq_scale);
u32 clock = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL);
@@ -520,6 +531,16 @@ static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu)
clock &= ~VIVS_HI_CLOCK_CONTROL_FSCALE_VAL__MASK;
clock |= VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale);
etnaviv_gpu_load_clock(gpu, clock);
+ end_ns = ktime_get_ns();
+ gpu->core_rate = gpu->base_rate_core / 64 * fscale;
+ }
+ gpu->applied_scale = gpu->freq_scale;
+
+ /* a resume or reset that keeps the rate is not worth a report */
+ if (scaled || rate_before != gpu->core_rate) {
+ trace_etnaviv_gpu_clock_change(gpu->dev, flags, begin_ns,
+ end_ns, rate_before,
+ gpu->core_rate);
}
/*
@@ -536,9 +557,11 @@ static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
u32 control, idle;
unsigned long timeout;
bool failed = true;
+ u64 reset_ns;
/* We hope that the GPU resets in under one second */
timeout = jiffies + msecs_to_jiffies(1000);
+ reset_ns = ktime_get_ns();
while (time_is_after_jiffies(timeout)) {
unsigned int fscale = 1 << (6 - gpu->freq_scale);
@@ -638,7 +661,7 @@ static int etnaviv_hw_reset(struct etnaviv_gpu *gpu)
}
/* We rely on the GPU running, so program the clock */
- etnaviv_gpu_update_clock(gpu);
+ etnaviv_gpu_update_clock(gpu, ETNA_CLOCK_RESET, reset_ns);
gpu->state = ETNA_GPU_STATE_RESET;
gpu->exec_state = -1;
@@ -1771,7 +1794,7 @@ static void etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu)
gpu->exec_state = -1;
}
-static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
+static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu, u64 resume_ns)
{
int ret;
@@ -1779,7 +1802,7 @@ static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu)
if (ret)
return ret;
- etnaviv_gpu_update_clock(gpu);
+ etnaviv_gpu_update_clock(gpu, ETNA_CLOCK_RESUME, resume_ns);
etnaviv_gpu_hw_init(gpu);
mutex_unlock(&gpu->lock);
@@ -1812,6 +1835,7 @@ etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
unsigned long state)
{
struct etnaviv_gpu *gpu = cdev->devdata;
+ u32 flags = 0;
u32 fence;
mutex_lock(&gpu->lock);
@@ -1833,7 +1857,7 @@ etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
* GPU. The wait must be bounded: a hang recovery stops the scheduler,
* which waits for the job held back in etnaviv_gpu_submit(), and that
* job waits for the clock change. So give up after one job timeout and
- * change the clock anyway.
+ * change the clock anyway. Such a change is reported as busy.
*/
WRITE_ONCE(gpu->clock_change_pending, true);
fence = gpu->next_fence;
@@ -1842,12 +1866,12 @@ etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev,
if (!wait_event_timeout(gpu->fence_event,
!fence_after(fence, READ_ONCE(gpu->completed_fence)),
gpu->sched.timeout))
- dev_dbg(gpu->dev, "clock change while jobs are running\n");
+ flags |= ETNA_CLOCK_BUSY;
mutex_lock(&gpu->lock);
/* the GPU may have suspended while waiting, the resume applies it */
if (gpu->state >= ETNA_GPU_STATE_RESET)
- etnaviv_gpu_update_clock(gpu);
+ etnaviv_gpu_update_clock(gpu, flags, ktime_get_ns());
store_release_wake_up(&gpu->clock_change_pending, false);
mutex_unlock(&gpu->lock);
@@ -1955,6 +1979,8 @@ static void etnaviv_gpu_unbind(struct device *dev, struct device *master,
gpu->drm = NULL;
xa_destroy(&gpu->user_fences);
+
+ gpu->core_rate = 0;
}
static const struct component_ops gpu_ops = {
@@ -2099,6 +2125,7 @@ static int etnaviv_gpu_rpm_suspend(struct device *dev)
static int etnaviv_gpu_rpm_resume(struct device *dev)
{
struct etnaviv_gpu *gpu = dev_get_drvdata(dev);
+ u64 resume_ns = ktime_get_ns();
int ret;
ret = etnaviv_gpu_clk_enable(gpu);
@@ -2107,7 +2134,7 @@ static int etnaviv_gpu_rpm_resume(struct device *dev)
/* Re-initialise the basic hardware state */
if (gpu->state == ETNA_GPU_STATE_IDENTIFIED) {
- ret = etnaviv_gpu_hw_resume(gpu);
+ ret = etnaviv_gpu_hw_resume(gpu, resume_ns);
if (ret) {
etnaviv_gpu_clk_disable(gpu);
return ret;
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.h b/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
index dddeabce6eb4..c8d537c3dee7 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
@@ -163,8 +163,10 @@ struct etnaviv_gpu {
struct reset_control *rst;
unsigned int freq_scale;
+ unsigned int applied_scale;
unsigned int fe_waitcycles;
unsigned long base_rate_core;
+ unsigned long core_rate;
unsigned long base_rate_shader;
};
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_trace.h b/drivers/gpu/drm/etnaviv/etnaviv_trace.h
new file mode 100644
index 000000000000..503015e4c973
--- /dev/null
+++ b/drivers/gpu/drm/etnaviv/etnaviv_trace.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2026 Mecha Systems Pvt Ltd
+ */
+
+#if !defined(_ETNAVIV_TRACE_H_) || defined(TRACE_HEADER_MULTI_READ)
+#define _ETNAVIV_TRACE_H_
+
+#include <linux/device.h>
+#include <linux/tracepoint.h>
+#include <linux/types.h>
+
+#include <drm/etnaviv_drm.h>
+
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM etnaviv
+#define TRACE_INCLUDE_FILE etnaviv_trace
+
+/*
+ * flags are ETNA_CLOCK_x, see the uAPI header for when the event fires.
+ * begin_ns and end_ns are CLOCK_MONOTONIC. The record itself carries the
+ * trace clock, which can differ from it by the time spent in suspend.
+ */
+TRACE_EVENT(etnaviv_gpu_clock_change,
+ TP_PROTO(struct device *dev, u32 flags, u64 begin_ns, u64 end_ns,
+ unsigned long rate_before, unsigned long rate_after),
+ TP_ARGS(dev, flags, begin_ns, end_ns, rate_before, rate_after),
+
+ TP_STRUCT__entry(
+ __string(dev, dev_name(dev))
+ __field(u32, flags)
+ __field(u64, begin_ns)
+ __field(u64, end_ns)
+ __field(unsigned long, rate_before)
+ __field(unsigned long, rate_after)
+ ),
+
+ TP_fast_assign(
+ __assign_str(dev);
+ __entry->flags = flags;
+ __entry->begin_ns = begin_ns;
+ __entry->end_ns = end_ns;
+ __entry->rate_before = rate_before;
+ __entry->rate_after = rate_after;
+ ),
+
+ TP_printk("dev=%s flags=%s begin_ns=%llu end_ns=%llu rate_before=%lu rate_after=%lu",
+ __get_str(dev),
+ __print_flags(__entry->flags, "|",
+ { ETNA_CLOCK_RESUME, "RESUME" },
+ { ETNA_CLOCK_RESET, "RESET" },
+ { ETNA_CLOCK_BUSY, "BUSY" }),
+ __entry->begin_ns, __entry->end_ns,
+ __entry->rate_before, __entry->rate_after)
+);
+
+#endif /* _ETNAVIV_TRACE_H_ */
+
+/* This part must be outside protection */
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+#include <trace/define_trace.h>
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_trace_points.c b/drivers/gpu/drm/etnaviv/etnaviv_trace_points.c
new file mode 100644
index 000000000000..bd039899252c
--- /dev/null
+++ b/drivers/gpu/drm/etnaviv/etnaviv_trace_points.c
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2026 Mecha Systems Pvt Ltd
+ */
+
+#ifndef __CHECKER__
+#define CREATE_TRACE_POINTS
+#include "etnaviv_trace.h"
+#endif
diff --git a/include/uapi/drm/etnaviv_drm.h b/include/uapi/drm/etnaviv_drm.h
index af024d90453d..83c58d94d36b 100644
--- a/include/uapi/drm/etnaviv_drm.h
+++ b/include/uapi/drm/etnaviv_drm.h
@@ -265,6 +265,25 @@ struct drm_etnaviv_pm_signal {
char name[64]; /* out, name of domain */
};
+/*
+ * Core clock changes, reported by the kernel for userspace that converts
+ * GPU cycle counts to CLOCK_MONOTONIC. These are the flags of the
+ * etnaviv_gpu_clock_change trace event.
+ *
+ * Every report is a rate change: a new cooling state, or the first clock
+ * setup after the GPU is bound. RESUME and RESET are set if the change was
+ * applied while the GPU was resumed or reset, without them it was applied
+ * to a running GPU. A resume or reset that keeps the rate is not reported,
+ * and neither is a cooling request that a resume has already applied.
+ *
+ * A change is made between jobs, so every job runs at a single rate.
+ * ETNA_CLOCK_BUSY marks the exception: jobs were still running when the
+ * kernel gave up waiting for them.
+ */
+#define ETNA_CLOCK_RESUME 0x0001
+#define ETNA_CLOCK_RESET 0x0002
+#define ETNA_CLOCK_BUSY 0x0004
+
#define DRM_ETNAVIV_GET_PARAM 0x00
/* placeholder:
#define DRM_ETNAVIV_SET_PARAM 0x01
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 7/7] drm/etnaviv: Add an ioctl to read back GPU clock changes
2026-11-20 10:40 [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace Christian Gmeiner
` (5 preceding siblings ...)
2026-11-20 10:40 ` [PATCH 6/7] drm/etnaviv: Add a GPU clock change trace event Christian Gmeiner
@ 2026-11-20 10:40 ` Christian Gmeiner
6 siblings, 0 replies; 9+ messages in thread
From: Christian Gmeiner @ 2026-11-20 10:40 UTC (permalink / raw)
To: Lucas Stach, Russell King, Christian Gmeiner, David Airlie,
Simona Vetter, Maarten Lankhorst, Maxime Ripard,
Thomas Zimmermann
Cc: kernel-dev, etnaviv, dri-devel, linux-kernel, Christian Gmeiner
From: Christian Gmeiner <cgmeiner@igalia.com>
Userspace that converts GPU cycle counts to time needs the core clock
rate a job ran at. The trace event has this information, but only a
privileged tracer can read it.
Keep the last 256 clock changes per GPU in a ring buffer, 10 KiB per
GPU. A record is written together with the trace event. It has the
same times and flags, the rate after the change and a sequence number.
The ring only fills as fast as the thermal governor changes the
cooling state.
Add an ioctl that returns the records newer than a given sequence
number. It also returns the newest sequence number the kernel has, so
a reader knows when to call again. A reader that has fallen behind
sees a gap in the sequence numbers. The caller passes the size of its
record struct, so the record can grow later without a new ioctl. The
first clock setup after bind is recorded too, so a new process can
read the current rate.
Bump the minor version so userspace can check for the ioctl.
Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
---
drivers/gpu/drm/etnaviv/etnaviv_drv.c | 20 ++++++++-
drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 83 +++++++++++++++++++++++++++++++++++
drivers/gpu/drm/etnaviv/etnaviv_gpu.h | 9 ++++
include/uapi/drm/etnaviv_drm.h | 43 +++++++++++++++++-
4 files changed, 153 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_drv.c b/drivers/gpu/drm/etnaviv/etnaviv_drv.c
index ec1352c1b09f..317362137e4a 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_drv.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_drv.c
@@ -473,6 +473,23 @@ static int etnaviv_ioctl_pm_query_sig(struct drm_device *dev, void *data,
return etnaviv_pm_query_sig(gpu, args);
}
+static int etnaviv_ioctl_clock_history(struct drm_device *dev, void *data,
+ struct drm_file *file)
+{
+ struct etnaviv_drm_private *priv = dev->dev_private;
+ struct drm_etnaviv_clock_history *args = data;
+ struct etnaviv_gpu *gpu;
+
+ if (args->pipe >= ETNA_MAX_PIPES)
+ return -EINVAL;
+
+ gpu = priv->gpu[args->pipe];
+ if (!gpu)
+ return -ENXIO;
+
+ return etnaviv_gpu_clock_history(gpu, args);
+}
+
static const struct drm_ioctl_desc etnaviv_ioctls[] = {
#define ETNA_IOCTL(n, func, flags) \
DRM_IOCTL_DEF_DRV(ETNAVIV_##n, etnaviv_ioctl_##func, flags)
@@ -487,6 +504,7 @@ static const struct drm_ioctl_desc etnaviv_ioctls[] = {
ETNA_IOCTL(GEM_WAIT, gem_wait, DRM_RENDER_ALLOW),
ETNA_IOCTL(PM_QUERY_DOM, pm_query_dom, DRM_RENDER_ALLOW),
ETNA_IOCTL(PM_QUERY_SIG, pm_query_sig, DRM_RENDER_ALLOW),
+ ETNA_IOCTL(CLOCK_HISTORY, clock_history, DRM_RENDER_ALLOW),
};
static void etnaviv_show_fdinfo(struct drm_printer *p, struct drm_file *file)
@@ -515,7 +533,7 @@ static const struct drm_driver etnaviv_drm_driver = {
.name = "etnaviv",
.desc = "etnaviv DRM",
.major = 1,
- .minor = 4,
+ .minor = 5,
};
/*
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
index 03075f289c69..2af1af33287a 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
@@ -506,6 +506,80 @@ static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock)
gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock);
}
+static void etnaviv_gpu_record_clock(struct etnaviv_gpu *gpu, u32 flags,
+ u64 begin_ns, u64 end_ns,
+ unsigned long rate)
+{
+ spin_lock(&gpu->clock_lock);
+ gpu->clock_seq++;
+ gpu->clock_records[gpu->clock_seq % ETNA_NR_CLOCK_RECORDS] =
+ (struct drm_etnaviv_clock_record){
+ .begin_ns = begin_ns,
+ .end_ns = end_ns,
+ .rate = rate,
+ .seq = gpu->clock_seq,
+ .flags = flags,
+ };
+ spin_unlock(&gpu->clock_lock);
+}
+
+static u64 etnaviv_gpu_clock_oldest(u64 last)
+{
+ if (last > ETNA_NR_CLOCK_RECORDS)
+ return last - ETNA_NR_CLOCK_RECORDS + 1;
+
+ return 1;
+}
+
+int etnaviv_gpu_clock_history(struct etnaviv_gpu *gpu,
+ struct drm_etnaviv_clock_history *args)
+{
+ u64 last, start;
+ u32 size, n = 0, i;
+
+ if (args->flags)
+ return -EINVAL;
+
+ /* records can grow, a reader gets the part of each one it knows */
+ if (args->record_size <
+ offsetofend(struct drm_etnaviv_clock_record, pad))
+ return -EINVAL;
+ size = min_t(u32, args->record_size,
+ sizeof(struct drm_etnaviv_clock_record));
+
+ spin_lock(&gpu->clock_lock);
+ last = gpu->clock_seq;
+ spin_unlock(&gpu->clock_lock);
+
+ if (args->since_seq < last) {
+ start = max(args->since_seq + 1, etnaviv_gpu_clock_oldest(last));
+ n = min_t(u64, last - start + 1, args->count);
+ }
+
+ for (i = 0; i < n; i++) {
+ void __user *dst = u64_to_user_ptr(args->records +
+ (u64)i * args->record_size);
+ struct drm_etnaviv_clock_record record;
+
+ spin_lock(&gpu->clock_lock);
+ record = gpu->clock_records[(start + i) % ETNA_NR_CLOCK_RECORDS];
+ spin_unlock(&gpu->clock_lock);
+
+ /* overwritten since clock_seq was read */
+ if (record.seq != start + i)
+ break;
+
+ if (copy_to_user(dst, &record, size))
+ return -EFAULT;
+ }
+
+ args->count = i;
+ args->record_size = size;
+ args->last_seq = last;
+
+ return 0;
+}
+
/* begin_ns is when the switch, or the restart that includes it, began */
static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu, u32 flags,
u64 begin_ns)
@@ -538,6 +612,8 @@ static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu, u32 flags,
/* a resume or reset that keeps the rate is not worth a report */
if (scaled || rate_before != gpu->core_rate) {
+ etnaviv_gpu_record_clock(gpu, flags, begin_ns, end_ns,
+ gpu->core_rate);
trace_etnaviv_gpu_clock_change(gpu->dev, flags, begin_ns,
end_ns, rate_before,
gpu->core_rate);
@@ -2008,8 +2084,15 @@ static int etnaviv_gpu_platform_probe(struct platform_device *pdev)
gpu->dev = dev;
mutex_init(&gpu->lock);
+ spin_lock_init(&gpu->clock_lock);
mutex_init(&gpu->sched_lock);
+ gpu->clock_records = devm_kcalloc(dev, ETNA_NR_CLOCK_RECORDS,
+ sizeof(*gpu->clock_records),
+ GFP_KERNEL);
+ if (!gpu->clock_records)
+ return -ENOMEM;
+
/* Map registers: */
gpu->mmio = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(gpu->mmio))
diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.h b/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
index c8d537c3dee7..ac619f3020bb 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.h
@@ -96,6 +96,7 @@ struct clk;
struct reset_control;
#define ETNA_NR_EVENTS 30
+#define ETNA_NR_CLOCK_RECORDS 256
enum etnaviv_gpu_state {
ETNA_GPU_STATE_UNKNOWN = 0,
@@ -168,6 +169,11 @@ struct etnaviv_gpu {
unsigned long base_rate_core;
unsigned long core_rate;
unsigned long base_rate_shader;
+
+ /* protects clock_seq and clock_records */
+ spinlock_t clock_lock;
+ u64 clock_seq;
+ struct drm_etnaviv_clock_record *clock_records;
};
static inline void gpu_write(struct etnaviv_gpu *gpu, u32 reg, u32 data)
@@ -237,4 +243,7 @@ void etnaviv_gpu_start_fe(struct etnaviv_gpu *gpu, u32 address, u16 prefetch);
extern struct platform_driver etnaviv_gpu_driver;
+int etnaviv_gpu_clock_history(struct etnaviv_gpu *gpu,
+ struct drm_etnaviv_clock_history *args);
+
#endif /* __ETNAVIV_GPU_H__ */
diff --git a/include/uapi/drm/etnaviv_drm.h b/include/uapi/drm/etnaviv_drm.h
index 83c58d94d36b..2f039836e7a9 100644
--- a/include/uapi/drm/etnaviv_drm.h
+++ b/include/uapi/drm/etnaviv_drm.h
@@ -284,6 +284,45 @@ struct drm_etnaviv_pm_signal {
#define ETNA_CLOCK_RESET 0x0002
#define ETNA_CLOCK_BUSY 0x0004
+/*
+ * The kernel also records the clock changes. The switch happens between
+ * begin_ns and end_ns. For RESUME and RESET the window covers the whole
+ * restart, from before the first reset attempt or the clock enable. That
+ * is wider than needed, but no job runs in it.
+ *
+ * The first record is written when the GPU is initialized after it is
+ * bound. So a GPU has at least one record, unless its core clock rate is
+ * unknown.
+ *
+ * DRM_IOCTL_ETNAVIV_CLOCK_HISTORY returns the records after since_seq, oldest
+ * first. It can return fewer than requested even if more exist, call it again
+ * until the last returned seq reaches last_seq. A reader that fell behind
+ * sees a gap between since_seq and the first seq returned.
+ *
+ * record_size is the stride of the records array and at least the size of
+ * the record below. So that the record can grow, the kernel writes the
+ * smaller of record_size and its own record size to each element and returns
+ * that size in record_size.
+ */
+struct drm_etnaviv_clock_record {
+ __u64 begin_ns; /* CLOCK_MONOTONIC right before the switch */
+ __u64 end_ns; /* CLOCK_MONOTONIC right after the switch */
+ __u64 rate; /* core clock rate after the switch, in Hz */
+ __u64 seq; /* increments by one per record, starts at 1 */
+ __u32 flags; /* ETNA_CLOCK_x */
+ __u32 pad; /* out, zero */
+};
+
+struct drm_etnaviv_clock_history {
+ __u32 pipe; /* in */
+ __u32 count; /* in, capacity of records, out, records returned */
+ __u32 record_size; /* in, stride of records, out, bytes written each */
+ __u32 flags; /* in, must be zero */
+ __u64 since_seq; /* in, return records with seq > since_seq */
+ __u64 last_seq; /* out, newest record */
+ __u64 records; /* in, pointer to struct drm_etnaviv_clock_record */
+};
+
#define DRM_ETNAVIV_GET_PARAM 0x00
/* placeholder:
#define DRM_ETNAVIV_SET_PARAM 0x01
@@ -298,7 +337,8 @@ struct drm_etnaviv_pm_signal {
#define DRM_ETNAVIV_GEM_WAIT 0x09
#define DRM_ETNAVIV_PM_QUERY_DOM 0x0a
#define DRM_ETNAVIV_PM_QUERY_SIG 0x0b
-#define DRM_ETNAVIV_NUM_IOCTLS 0x0c
+#define DRM_ETNAVIV_CLOCK_HISTORY 0x0c
+#define DRM_ETNAVIV_NUM_IOCTLS 0x0d
#define DRM_IOCTL_ETNAVIV_GET_PARAM DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_GET_PARAM, struct drm_etnaviv_param)
#define DRM_IOCTL_ETNAVIV_GEM_NEW DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_GEM_NEW, struct drm_etnaviv_gem_new)
@@ -311,6 +351,7 @@ struct drm_etnaviv_pm_signal {
#define DRM_IOCTL_ETNAVIV_GEM_WAIT DRM_IOW(DRM_COMMAND_BASE + DRM_ETNAVIV_GEM_WAIT, struct drm_etnaviv_gem_wait)
#define DRM_IOCTL_ETNAVIV_PM_QUERY_DOM DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_PM_QUERY_DOM, struct drm_etnaviv_pm_domain)
#define DRM_IOCTL_ETNAVIV_PM_QUERY_SIG DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_PM_QUERY_SIG, struct drm_etnaviv_pm_signal)
+#define DRM_IOCTL_ETNAVIV_CLOCK_HISTORY DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_CLOCK_HISTORY, struct drm_etnaviv_clock_history)
#if defined(__cplusplus)
}
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 1/7] drm/etnaviv: Don't set up a GPU on resume that is not initialized
2026-11-20 10:40 ` [PATCH 1/7] drm/etnaviv: Don't set up a GPU on resume that is not initialized Christian Gmeiner
@ 2026-11-20 10:53 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-20 10:53 UTC (permalink / raw)
To: Christian Gmeiner; +Cc: dri-devel
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] drm/etnaviv: dangling pointer in
etnaviv_flop_reset_ppu_init() [1]
--
commit e39a29696b51cde581759634e80e80a231bf7a60
Author: Christian Gmeiner <cgmeiner@igalia.com>
drm/etnaviv: Don't set up a GPU on resume that is not initialized
This commit resets gpu->state to ETNA_GPU_STATE_UNKNOWN when
etnaviv_gpu_init() fails or during etnaviv_gpu_unbind(), and preserves
that state across runtime suspend so uninitialized GPUs are not set up
on resume.
> diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
> index 6a4aab75ebba5..2a15ae6135f5e 100644
> --- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
> +++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
> @@ -951,6 +951,8 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
> return 0;
>
> fail:
> + /* the GPU is not used, a resume must not set it up */
> + gpu->state = ETNA_GPU_STATE_UNKNOWN;
> pm_runtime_mark_last_busy(gpu->dev);
> pm_put:
> pm_runtime_put_autosuspend(gpu->dev);
[Severity: Critical]
This problem wasn't introduced by this patch, but can a failure in
etnaviv_flop_reset_ppu_init() during etnaviv_gpu_init() leave a dangling
pointer in priv->flop_reset_data_ppu [1]?
drivers/gpu/drm/etnaviv/etnaviv_flop_reset.c:etnaviv_flop_reset_ppu_init() {
...
int ret = etnaviv_cmdbuf_init(priv->cmdbuf_suballoc,
priv->flop_reset_data_ppu, buffer_size);
if (ret) {
kfree(priv->flop_reset_data_ppu);
return ret;
}
...
}
Because load_gpu() ignores errors from etnaviv_gpu_init() and keeps the
DRM device bound, a subsequent GPU core sees a non-NULL
priv->flop_reset_data_ppu, skips initialization, and dereferences the
freed command buffer in etnaviv_flop_reset_ppu_run() (use-after-free).
On device unbind, etnaviv_unbind() also passes the dangling pointer to
etnaviv_cmdbuf_free() and kfree(), causing a double free.
[1] https://sashiko.dev/#/bug/linux-4d0f1113-6871-425a-a512-96f1b1788e33
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-etnaviv-gpu-freq-trace-v1-0-445fc6eacddf@igalia.com?part=1
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-20 10:53 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 10:40 [PATCH 0/7] drm/etnaviv: Report GPU clock changes to userspace Christian Gmeiner
2026-11-20 10:40 ` [PATCH 1/7] drm/etnaviv: Don't set up a GPU on resume that is not initialized Christian Gmeiner
2026-11-20 10:53 ` sashiko-bot
2026-11-20 10:40 ` [PATCH 2/7] drm/etnaviv: Register the cooling device last and unregister it first Christian Gmeiner
2026-11-20 10:40 ` [PATCH 3/7] drm/etnaviv: Skip cooling requests that keep the clock Christian Gmeiner
2026-11-20 10:40 ` [PATCH 4/7] drm/etnaviv: Don't change the clock of a suspended GPU Christian Gmeiner
2026-11-20 10:40 ` [PATCH 5/7] drm/etnaviv: Change the GPU clock between jobs Christian Gmeiner
2026-11-20 10:40 ` [PATCH 6/7] drm/etnaviv: Add a GPU clock change trace event Christian Gmeiner
2026-11-20 10:40 ` [PATCH 7/7] drm/etnaviv: Add an ioctl to read back GPU clock changes Christian Gmeiner
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.