Skip to contentSkip to conversation

Collection of fixes for Panfrost: Perfcnt, RPM, refactorings

PATCH Superseded Adrián Larumbe 3 participants 45 messages Sep 11 – Sep 23

Subsystems (3)

v9 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Adrián Larumbe

author maintainer
#0 lore raw

v9 cover Collection of fixes for Panfrost: Perfcnt, RPM, refactorings

This patch series began as an assortment of small fixes for some issues uncovered by Claude. Over time, it has evolved to include many other fixes for further problems revealed by Sashiko in successive revisions, and also refactorings and improvements suggested by maintainer discussions.

A subset of patches submitted in previous revisions was already merged after they had been reviewed, to avoid this series growing to unmanageable extents.

---
Changes in v9:
- Extended clk_(enable|disable) consolidation into other runtime callbacks.
- Fixed uAPI and perfcnt/reset interaction errors uncovered by Sashiko.
- Link to v8: https://patch.msgid.link/[email protected]

Changes in v8:
- Applied fixes for issues brought up by Sashiko.
- Rolled back some additions from v7: job stopping, IRQ consolidation.
- Applied RPM fixes before moving most driver init code into panfrost_device.c
- Brought back v6 patch that enables MMU IRQs before device is registered.
- Simplified perfcnt reset handling procedure, so that no retry is done in the kernel.
- Link to v7: https://patch.msgid.link/[email protected]

Changes in v7:
- Again fixed some minor issues reported by Sashiko.
- Made sure the new debugfs reset knob works even when runtime PM isn't enabled.
- Added new patch that handles job termination at driver teardown time.
- Link to v6: https://patch.msgid.link/[email protected]

Changes in v6:
- Grafted trivial already-reviwed patches from previous revisions into
their own series so that they could be merged into drm-misc-next.
- Added commits with multiple refactorings, code rearrangements and simplifications.
- Addressed all issues brought up by reviwers and Sashiko in the previous revision.
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- Addressed more issues uncovered by Sashiko.
- Discarded two patches to avoid dealing with too many pre-existing issues at once.
- Biggest change is the re-arrangement of the start sequence, made to look like Panthor.
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Fixed errors and issues uncovered by Sashiko in the previous revision.
- Changed the way RPM checks whether the device is initialised when resuming.
- Simplified perfcnt treatment of potential resets.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Applied some minor suggestions for the first couple of patches in the series.
- Moved shrinker initialisation into gem initialisation.
- Fixed RPM bugs by moving all clock and power initialisation into RPM resume.
- Added patch for reset sequence fixes and also a userspace knob to trigger it.
- Reworked perfcnt fix by having the reset sequence restore its initial state.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Fixed race conditions introduced by the previous revision, in the RPM and HWPerf commits specifically. These can be consulted at [1]
- Don't attempt to suspend the device unconditionally at the end of
probe(), and do it through autosuspend instead.
- Broke the RPM patch into one that fixes PM refcnt proper and another one for MMU enablement at device init time.
- Moved perfcnt GPU disable helper into its own commmit. Same for transplating shrinker initialisation and unplug.
- Added 'Fixes' and 'Reported-by' tags to all the relevant commits
- [1] https://sashiko.dev/#/patchset/20260526-claude-fixes-v1-0-16e92eaa4949%40collabora.com
- Link to v1: https://patch.msgid.link/[email protected]

To: Boris Brezillon <[email protected]>
To: Rob Herring <[email protected]>
To: Steven Price <[email protected]>
To: Adrián Larumbe <[email protected]>
To: Maarten Lankhorst <[email protected]>
To: Maxime Ripard <[email protected]>
To: Thomas Zimmermann <[email protected]>
To: David Airlie <[email protected]>
To: Simona Vetter <[email protected]>
To: Faith Ekstrand <[email protected]>
To: "Marty E. Plummer" <[email protected]>
To: Tomeu Vizoso <[email protected]>
To: Eric Anholt <[email protected]>
To: Alyssa Rosenzweig <[email protected]>
To: Robin Murphy <[email protected]>
To: Philipp Zabel <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: Neil Armstrong <[email protected]>

---
Adrián Larumbe (16):
drm/panfrost: Move shrinker initialization and unplug one level down drm/panfrost: Move lock and modparam initialisations into their subsystems drm/panfrost: Move debugfs initialisation to relevant subsystems drm/panfrost: Skip NULL checks for clock enable/disabling drm/panfrost: Consolidate device clock management and reset drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove drm/panfrost: Explicitly enable MMU interrupts at device init drm/panfrost: Move all DRM device initialisation into device_init() drm/panfrost: Add warning messages to fatal error conditions drm/panfrost: Add debugfs knob for manually triggering a GPU reset drm/panfrost: Move perfcnt GPU disable sequence into a helper drm/panfrost: Skip cache flush/invalidate when enabling perfcnt drm/panfrost: Avoid cache flush after perfcnt sample in fully coherent systems drm/panfrost: Introduce a reset lock
drm/panfrost: Fix races between perfcnt and reset sequence drm/panfrost: Bump driver minor to reflect new DUMP IOCTL req field

 drivers/gpu/drm/panfrost/panfrost_device.c       | 222 +++++++++++++--------
 drivers/gpu/drm/panfrost/panfrost_device.h       |   5 +
 drivers/gpu/drm/panfrost/panfrost_drv.c          | 198 +------------------
 drivers/gpu/drm/panfrost/panfrost_drv.h          |   9 -
 drivers/gpu/drm/panfrost/panfrost_gem.c          |  64 ++++--
 drivers/gpu/drm/panfrost/panfrost_gem.h          |  10 +-
 drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c |   9 +-
 drivers/gpu/drm/panfrost/panfrost_gpu.c          |   2 +
 drivers/gpu/drm/panfrost/panfrost_job.c          | 101 ++++++++++
 drivers/gpu/drm/panfrost/panfrost_job.h          |   4 +
 drivers/gpu/drm/panfrost/panfrost_mmu.c          |  14 +-
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c      | 242 +++++++++++++++--------
 drivers/gpu/drm/panfrost/panfrost_perfcnt.h      |   1 +
 include/uapi/drm/panfrost_drm.h                  |   8 +-
 14 files changed, 501 insertions(+), 388 deletions(-)

---
base-commit: 3ab04dcce43cd4fdaed94f08f4e6289be4921ea4
change-id: 20260523-claude-fixes-82b03a134a8c

Best regards,

Signature · 1 line
Adrián Larumbe <[email protected]>
author maintainer
#1 lore raw

PATCH v9 1/16 drm/panfrost: Move shrinker initialization and unplug one level down

Since the moment we call drm_dev_register() the device should be in a position to accept jobs, so it's best if the shrinker is already initialized by then.

On top of that, make shrinker functions take an panfrost_device pointer like other functions in the same sequence and rename them accordingly.

Essentially mimic the init/fini behaviour in Panthor.

---
 drivers/gpu/drm/panfrost/panfrost_device.c       |  8 +++++++-
 drivers/gpu/drm/panfrost/panfrost_drv.c          |  6 ------
 drivers/gpu/drm/panfrost/panfrost_gem.c          | 26 ++++++++++++++----------
 drivers/gpu/drm/panfrost/panfrost_gem.h          |  7 ++++---
 drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c |  8 ++------
 5 files changed, 28 insertions(+), 27 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +7 -1
index 485349faf251..05c40d5a20b5 100644
@@ -280,9 +280,14 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	if (err)
 		goto out_job;
 
-	panfrost_gem_init(pfdev);
+	err = panfrost_gem_init(pfdev);
+	if (err)
+		goto out_perfcnt;
 
 	return 0;
+
+out_perfcnt:
+	panfrost_perfcnt_fini(pfdev);
 out_job:
 	panfrost_jm_fini(pfdev);
 out_mmu:
@@ -304,6 +309,7 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 
 void panfrost_device_fini(struct panfrost_device *pfdev)
 {
+	panfrost_gem_fini(pfdev);
 	panfrost_perfcnt_fini(pfdev);
 	panfrost_jm_fini(pfdev);
 	panfrost_mmu_fini(pfdev);
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -6
index 96a66d673ba6..9882a3ede75f 100644
@@ -1003,14 +1003,9 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
-	err = panfrost_gem_shrinker_init(&pfdev->base);
-	if (err)
-		goto err_out2;
 
 	return 0;
 
-err_out2:
-	drm_dev_unregister(&pfdev->base);
 err_out1:
 	pm_runtime_disable(pfdev->base.dev);
 	panfrost_device_fini(pfdev);
@@ -1024,7 +1019,6 @@ static void panfrost_remove(struct platform_device *pdev)
 	struct panfrost_device *pfdev = platform_get_drvdata(pdev);
 
 	drm_dev_unregister(&pfdev->base);
-	panfrost_gem_shrinker_cleanup(&pfdev->base);
 
 	pm_runtime_get_sync(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
drivers/gpu/drm/panfrost/panfrost_gem.c +15 -11
index 3a7fce428898..f39c0f50ed4f 100644
@@ -15,20 +15,24 @@
 #include "panfrost_gem.h"
 #include "panfrost_mmu.h"
 
-void panfrost_gem_init(struct panfrost_device *pfdev)
+int panfrost_gem_init(struct panfrost_device *pfdev)
 {
-	int err;
+	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && panfrost_transparent_hugepage) {
+		int err = drm_gem_huge_mnt_create(&pfdev->base, "within_size");
+
+		if (drm_gem_get_huge_mnt(&pfdev->base))
+			drm_info(&pfdev->base, "Using Transparent Hugepage\n");
+		else if (err)
+			drm_warn(&pfdev->base, "Can't use Transparent Hugepage (%d)\n",
+				 err);
+	}
 
-	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
-	    !panfrost_transparent_hugepage)
-		return;
+	return panfrost_gem_shrinker_init(pfdev);
+}
 
-	err = drm_gem_huge_mnt_create(&pfdev->base, "within_size");
-	if (drm_gem_get_huge_mnt(&pfdev->base))
-		drm_info(&pfdev->base, "Using Transparent Hugepage\n");
-	else if (err)
-		drm_warn(&pfdev->base, "Can't use Transparent Hugepage (%d)\n",
-			 err);
+void panfrost_gem_fini(struct panfrost_device *pfdev)
+{
+	panfrost_gem_shrinker_fini(pfdev);
 }
 
 #ifdef CONFIG_DEBUG_FS
drivers/gpu/drm/panfrost/panfrost_gem.h +4 -3
index 79d4377019e9..5c823cdbd980 100644
@@ -129,7 +129,8 @@ drm_mm_node_to_panfrost_mapping(struct drm_mm_node *node)
 	return container_of(node, struct panfrost_gem_mapping, mmnode);
 }
 
-void panfrost_gem_init(struct panfrost_device *pfdev);
+int panfrost_gem_init(struct panfrost_device *pfdev);
+void panfrost_gem_fini(struct panfrost_device *pfdev);
 
 struct drm_gem_object *panfrost_gem_create_object(struct drm_device *dev, size_t size);
 
@@ -154,8 +155,8 @@ panfrost_gem_mapping_get(struct panfrost_gem_object *bo,
 void panfrost_gem_mapping_put(struct panfrost_gem_mapping *mapping);
 void panfrost_gem_teardown_mappings_locked(struct panfrost_gem_object *bo);
 
-int panfrost_gem_shrinker_init(struct drm_device *dev);
-void panfrost_gem_shrinker_cleanup(struct drm_device *dev);
+int panfrost_gem_shrinker_init(struct panfrost_device *pfdev);
+void panfrost_gem_shrinker_fini(struct panfrost_device *pfdev);
 
 void panfrost_gem_set_label(struct drm_gem_object *obj, const char *label);
 int panfrost_gem_sync(struct drm_gem_object *obj, u32 type,
drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c +2 -6
index 2fe967a90bcb..fefae87535d6 100644
@@ -95,10 +95,8 @@ panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  *
  * This function registers and sets up the panfrost shrinker.
  */
-int panfrost_gem_shrinker_init(struct drm_device *dev)
+int panfrost_gem_shrinker_init(struct panfrost_device *pfdev)
 {
-	struct panfrost_device *pfdev = to_panfrost_device(dev);
-
 	pfdev->shrinker = shrinker_alloc(0, "drm-panfrost");
 	if (!pfdev->shrinker)
 		return -ENOMEM;
@@ -118,10 +116,8 @@ int panfrost_gem_shrinker_init(struct drm_device *dev)
  *
  * This function unregisters the panfrost shrinker.
  */
-void panfrost_gem_shrinker_cleanup(struct drm_device *dev)
+void panfrost_gem_shrinker_fini(struct panfrost_device *pfdev)
 {
-	struct panfrost_device *pfdev = to_panfrost_device(dev);
-
 	if (pfdev->shrinker)
 		shrinker_free(pfdev->shrinker);
 }
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH v9 2/16 drm/panfrost: Move lock and modparam initialisations into their subsystems

In reply to Adrián Larumbe

The device init function feels a bit cluttered with things that should be done in the specific subsystems where those locks and lists are being used. Same goes for module parameters, it's best to limit them to the subsystems where they're relevant.

Then we can also discard panfrost_drv.h since the transparent_huge_page doesn't need to be accessed elsewhere. We can also simplify the creation of a mount point, because drm_gem_get_huge_mnt() would never return NULL if drm_gem_huge_mnt_create() returns 0 when kernel is built with support for transparent huge pages.

---
 drivers/gpu/drm/panfrost/panfrost_device.c       |  7 -------
 drivers/gpu/drm/panfrost/panfrost_drv.c          | 10 ----------
 drivers/gpu/drm/panfrost/panfrost_drv.h          |  9 ---------
 drivers/gpu/drm/panfrost/panfrost_gem.c          | 19 +++++++++++++------
 drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c |  3 +++
 drivers/gpu/drm/panfrost/panfrost_gpu.c          |  2 ++
 drivers/gpu/drm/panfrost/panfrost_job.c          |  1 +
 drivers/gpu/drm/panfrost/panfrost_mmu.c          |  3 +++
 8 files changed, 22 insertions(+), 32 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +0 -7
index 05c40d5a20b5..c1a2e93a2b9b 100644
@@ -216,13 +216,6 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 {
 	int err;
 
-	mutex_init(&pfdev->sched_lock);
-	INIT_LIST_HEAD(&pfdev->as_lru_list);
-
-	spin_lock_init(&pfdev->as_lock);
-
-	spin_lock_init(&pfdev->cycle_counter.lock);
-
 #ifdef CONFIG_DEBUG_FS
 	mutex_init(&pfdev->debugfs.gems_lock);
 	INIT_LIST_HEAD(&pfdev->debugfs.gems_list);
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -10
index 9882a3ede75f..59af79a534fa 100644
@@ -21,7 +21,6 @@
 #include <drm/drm_utils.h>
 
 #include "panfrost_device.h"
-#include "panfrost_drv.h"
 #include "panfrost_gem.h"
 #include "panfrost_mmu.h"
 #include "panfrost_job.h"
@@ -955,12 +954,6 @@ static const struct drm_driver panfrost_drm_driver = {
 #endif
 };
 
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-bool panfrost_transparent_hugepage = true;
-module_param_named(transparent_hugepage, panfrost_transparent_hugepage, bool, 0400);
-MODULE_PARM_DESC(transparent_hugepage, "Use a dedicated tmpfs mount point with Transparent Hugepage enabled (true = default)");
-#endif
-
 static int panfrost_probe(struct platform_device *pdev)
 {
 	struct panfrost_device *pfdev;
@@ -979,9 +972,6 @@ static int panfrost_probe(struct platform_device *pdev)
 
 	pfdev->coherent = device_get_dma_attr(&pdev->dev) == DEV_DMA_COHERENT;
 
-	mutex_init(&pfdev->shrinker_lock);
-	INIT_LIST_HEAD(&pfdev->shrinker_list);
-
 	err = panfrost_device_init(pfdev);
 	if (err) {
 		if (err != -EPROBE_DEFER)
drivers/gpu/drm/panfrost/panfrost_drv.h deleted +0 -9
deleted file mode 100644
index a2277ec61aab..000000000000
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 or MIT */
-/* Copyright 2025 Amazon.com, Inc. or its affiliates */
-
-#ifndef __PANFROST_DRV_H__
-#define __PANFROST_DRV_H__
-
-extern bool panfrost_transparent_hugepage;
-
-#endif
drivers/gpu/drm/panfrost/panfrost_gem.c +13 -6
index f39c0f50ed4f..383ac8cd5645 100644
@@ -11,20 +11,27 @@
 #include <drm/panfrost_drm.h>
 #include <drm/drm_print.h>
 #include "panfrost_device.h"
-#include "panfrost_drv.h"
 #include "panfrost_gem.h"
 #include "panfrost_mmu.h"
 
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+static bool panfrost_transparent_hugepage = true;
+module_param_named(transparent_hugepage, panfrost_transparent_hugepage, bool, 0400);
+MODULE_PARM_DESC(transparent_hugepage, "Use a dedicated tmpfs mount point with Transparent Hugepage enabled (true = default)");
+#else
+#define panfrost_transparent_hugepage false
+#endif
+
 int panfrost_gem_init(struct panfrost_device *pfdev)
 {
-	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && panfrost_transparent_hugepage) {
+	if (panfrost_transparent_hugepage) {
 		int err = drm_gem_huge_mnt_create(&pfdev->base, "within_size");
 
-		if (drm_gem_get_huge_mnt(&pfdev->base))
+		if (err)
+			drm_warn(&pfdev->base,
+				 "Can't use Transparent Hugepage (%d)\n", err);
+		else
 			drm_info(&pfdev->base, "Using Transparent Hugepage\n");
-		else if (err)
-			drm_warn(&pfdev->base, "Can't use Transparent Hugepage (%d)\n",
-				 err);
 	}
 
 	return panfrost_gem_shrinker_init(pfdev);
drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c +3 -0
index fefae87535d6..72cd7a165e6d 100644
@@ -97,6 +97,9 @@ panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  */
 int panfrost_gem_shrinker_init(struct panfrost_device *pfdev)
 {
+	mutex_init(&pfdev->shrinker_lock);
+	INIT_LIST_HEAD(&pfdev->shrinker_list);
+
 	pfdev->shrinker = shrinker_alloc(0, "drm-panfrost");
 	if (!pfdev->shrinker)
 		return -ENOMEM;
drivers/gpu/drm/panfrost/panfrost_gpu.c +2 -0
index 7d555e63e21a..8a15ccce08e9 100644
@@ -516,6 +516,8 @@ int panfrost_gpu_init(struct panfrost_device *pfdev)
 {
 	int err;
 
+	spin_lock_init(&pfdev->cycle_counter.lock);
+
 	err = panfrost_gpu_soft_reset(pfdev);
 	if (err)
 		return err;
drivers/gpu/drm/panfrost/panfrost_job.c +1 -0
index d59b4863b8ad..476c936b54e6 100644
@@ -874,6 +874,7 @@ int panfrost_jm_init(struct panfrost_device *pfdev)
 
 	INIT_WORK(&pfdev->reset.work, panfrost_reset_work);
 	spin_lock_init(&js->job_lock);
+	mutex_init(&pfdev->sched_lock);
 
 	js->irq = platform_get_irq_byname(to_platform_device(pfdev->base.dev), "job");
 	if (js->irq < 0)
drivers/gpu/drm/panfrost/panfrost_mmu.c +3 -0
index 4a3162c3b659..5c393ed6e310 100644
@@ -955,6 +955,9 @@ int panfrost_mmu_init(struct panfrost_device *pfdev)
 {
 	int err;
 
+	INIT_LIST_HEAD(&pfdev->as_lru_list);
+	spin_lock_init(&pfdev->as_lock);
+
 	pfdev->mmu_irq = platform_get_irq_byname(to_platform_device(pfdev->base.dev), "mmu");
 	if (pfdev->mmu_irq < 0)
 		return pfdev->mmu_irq;
Signature · 1 line
2.55.0
author maintainer
#6 lore raw

PATCH v9 3/16 drm/panfrost: Move debugfs initialisation to relevant subsystems

In reply to Adrián Larumbe

Because debugfs knobs are a device interface, and also the different knobs deal with specific subsystems, it's best to move their initialisations away from panfrost_drv.c and into the subsystem they provide information about.

---
 drivers/gpu/drm/panfrost/panfrost_device.c |   8 ++
 drivers/gpu/drm/panfrost/panfrost_device.h |   4 +
 drivers/gpu/drm/panfrost/panfrost_drv.c    | 129 +----------------------------
 drivers/gpu/drm/panfrost/panfrost_gem.c    |  29 ++++++-
 drivers/gpu/drm/panfrost/panfrost_gem.h    |   3 +-
 drivers/gpu/drm/panfrost/panfrost_job.c    |  99 ++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_job.h    |   4 +
 7 files changed, 144 insertions(+), 132 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +8 -0
index c1a2e93a2b9b..5bf528f41278 100644
@@ -561,3 +561,11 @@ EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = {
 	RUNTIME_PM_OPS(panfrost_device_runtime_suspend, panfrost_device_runtime_resume, NULL)
 	SYSTEM_SLEEP_PM_OPS(panfrost_device_suspend, panfrost_device_resume)
 };
+
+#ifdef CONFIG_DEBUG_FS
+void panfrost_device_debugfs_init(struct drm_minor *minor)
+{
+	panfrost_gem_debugfs_init(minor);
+	panfrost_sched_debugfs_init(minor);
+}
+#endif // CONFIG_DEBUG_FS
drivers/gpu/drm/panfrost/panfrost_device.h +4 -0
index 1fe1554f2652..a0b9a2145fc9 100644
@@ -341,4 +341,8 @@ panfrost_device_schedule_reset(struct panfrost_device *pfdev)
 	queue_work(pfdev->reset.wq, &pfdev->reset.work);
 }
 
+#ifdef CONFIG_DEBUG_FS
+void panfrost_device_debugfs_init(struct drm_minor *minor);
+#endif // CONFIG_DEBUG_FS
+
 #endif
drivers/gpu/drm/panfrost/panfrost_drv.c +1 -128

141 lines not on this page · raw message

drivers/gpu/drm/panfrost/panfrost_gem.c +27 -2
index 383ac8cd5645..518f24549b9d 100644
@@ -9,6 +9,7 @@
 #include <linux/dma-mapping.h>
 
 #include <drm/panfrost_drm.h>
+#include <drm/drm_debugfs.h>
 #include <drm/drm_print.h>
 #include "panfrost_device.h"
 #include "panfrost_gem.h"
@@ -735,8 +736,8 @@ static void panfrost_gem_debugfs_bo_print(struct panfrost_gem_object *bo,
 		totals->reclaimable += resident_size;
 }
 
-void panfrost_gem_debugfs_print_bos(struct panfrost_device *pfdev,
-				    struct seq_file *m)
+static void panfrost_gem_debugfs_print_bos(struct panfrost_device *pfdev,
+					   struct seq_file *m)
 {
 	struct gem_size_totals totals = {0};
 	struct panfrost_gem_object *bo;
@@ -756,4 +757,28 @@ void panfrost_gem_debugfs_print_bos(struct panfrost_device *pfdev,
 	seq_printf(m, "Total size: %zd, Total resident: %zd, Total reclaimable: %zd\n",
 		   totals.size, totals.resident, totals.reclaimable);
 }
+
+static int panfrost_gems_show(struct seq_file *m, void *data)
+{
+	struct drm_info_node *node = m->private;
+	struct panfrost_device *pfdev = to_panfrost_device(node->minor->dev);
+
+	panfrost_gem_debugfs_print_bos(pfdev, m);
+
+	return 0;
+}
+
+static struct drm_info_list panfrost_debugfs_list[] = {
+	{"gems",
+	 panfrost_gems_show, 0, NULL},
+};
+
+int panfrost_gem_debugfs_init(struct drm_minor *minor)
+{
+	drm_debugfs_create_files(panfrost_debugfs_list,
+				 ARRAY_SIZE(panfrost_debugfs_list),
+				 minor->debugfs_root, minor);
+
+	return 0;
+}
 #endif
drivers/gpu/drm/panfrost/panfrost_gem.h +1 -2
index 5c823cdbd980..3008afcf4bff 100644
@@ -164,8 +164,7 @@ int panfrost_gem_sync(struct drm_gem_object *obj, u32 type,
 void panfrost_gem_internal_set_label(struct drm_gem_object *obj, const char *label);
 
 #ifdef CONFIG_DEBUG_FS
-void panfrost_gem_debugfs_print_bos(struct panfrost_device *pfdev,
-				    struct seq_file *m);
+int panfrost_gem_debugfs_init(struct drm_minor *minor);
 #endif
 
 #endif /* __PANFROST_GEM_H__ */
drivers/gpu/drm/panfrost/panfrost_job.c +99 -0

108 lines not on this page · raw message

drivers/gpu/drm/panfrost/panfrost_job.h +4 -0
index c3f57e41a571..3e3b9717838f 100644
@@ -74,4 +74,8 @@ int panfrost_job_get_slot(struct panfrost_job *job);
 int panfrost_job_push(struct panfrost_job *job);
 void panfrost_job_put(struct panfrost_job *job);
 
+#ifdef CONFIG_DEBUG_FS
+void panfrost_sched_debugfs_init(struct drm_minor *minor);
+#endif
+
 #endif
Signature · 1 line
2.55.0
author maintainer
#7 lore raw

PATCH v9 4/16 drm/panfrost: Skip NULL checks for clock enable/disabling

In reply to Adrián Larumbe

The clk_* API can deal with NULL clock objects, so we can do away with NULL checks in client code.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +5 -9
index 5bf528f41278..b3a53504bd01 100644
@@ -510,11 +510,9 @@ static int panfrost_device_resume(struct device *dev)
 		if (ret)
 			goto err_clk;
 
-		if (pfdev->bus_clock) {
-			ret = clk_enable(pfdev->bus_clock);
-			if (ret)
-				goto err_bus_clk;
-		}
+		ret = clk_enable(pfdev->bus_clock);
+		if (ret)
+			goto err_bus_clk;
 	}
 
 	ret = pm_runtime_force_resume(dev);
@@ -524,7 +522,7 @@ static int panfrost_device_resume(struct device *dev)
 	return 0;
 
 err_resume:
-	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS) && pfdev->bus_clock)
+	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS))
 		clk_disable(pfdev->bus_clock);
 err_bus_clk:
 	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS))
@@ -545,9 +543,7 @@ static int panfrost_device_suspend(struct device *dev)
 		return ret;
 
 	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS)) {
-		if (pfdev->bus_clock)
-			clk_disable(pfdev->bus_clock);
-
+		clk_disable(pfdev->bus_clock);
 		clk_disable(pfdev->clock);
 	}
 
Signature · 1 line
2.55.0
author maintainer
#8 lore raw

PATCH v9 5/16 drm/panfrost: Consolidate device clock management and reset

In reply to Adrián Larumbe

Gather all clock enables and disables into a single function to avoid repetitions between driver init/fini and device resume/suspend, since these clocks are always handled in bulk.

Also do clk (un)prepares and dis/enables at the same time, since the clk_prepare_* family of functions can simply increase the refcnt of an already prepared clock.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 118 +++++++++++++----------------
 1 file changed, 52 insertions(+), 66 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +52 -66
index b3a53504bd01..2fcbd223204c 100644
@@ -34,10 +34,46 @@ static void panfrost_reset_fini(struct panfrost_device *pfdev)
 	reset_control_assert(pfdev->rstc);
 }
 
-static int panfrost_clk_init(struct panfrost_device *pfdev)
+static int panfrost_clk_enable(struct panfrost_device *pfdev, bool enable_ace_clock)
 {
 	int err;
+
+	err = clk_prepare_enable(pfdev->clock);
+	if (err)
+		return err;
+
+	err = clk_prepare_enable(pfdev->bus_clock);
+	if (err)
+		goto disable_clock;
+
+	if (enable_ace_clock) {
+		err = clk_prepare_enable(pfdev->bus_ace_clock);
+		if (err)
+			goto disable_bus_clock;
+	}
+
+	return 0;
+
+disable_bus_clock:
+	clk_disable_unprepare(pfdev->bus_clock);
+disable_clock:
+	clk_disable_unprepare(pfdev->clock);
+
+	return err;
+}
+
+static void panfrost_clk_disable(struct panfrost_device *pfdev, bool disable_ace_clock)
+{
+	if (disable_ace_clock)
+		clk_disable_unprepare(pfdev->bus_ace_clock);
+	clk_disable_unprepare(pfdev->bus_clock);
+	clk_disable_unprepare(pfdev->clock);
+}
+
+static int panfrost_clk_init(struct panfrost_device *pfdev)
+{
 	unsigned long rate;
+	int err;
 
 	pfdev->clock = devm_clk_get(pfdev->base.dev, NULL);
 	if (IS_ERR(pfdev->clock)) {
@@ -48,53 +84,31 @@ static int panfrost_clk_init(struct panfrost_device *pfdev)
 	rate = clk_get_rate(pfdev->clock);
 	dev_info(pfdev->base.dev, "clock rate = %lu\n", rate);
 
-	err = clk_prepare_enable(pfdev->clock);
-	if (err)
-		return err;
-
 	pfdev->bus_clock = devm_clk_get_optional(pfdev->base.dev, "bus");
 	if (IS_ERR(pfdev->bus_clock)) {
-		dev_err(pfdev->base.dev, "get bus_clock failed %ld\n",
-			PTR_ERR(pfdev->bus_clock));
 		err = PTR_ERR(pfdev->bus_clock);
-		goto disable_clock;
+		dev_err(pfdev->base.dev, "get bus_clock failed %d\n", err);
+		return err;
 	}
 
 	if (pfdev->bus_clock) {
 		rate = clk_get_rate(pfdev->bus_clock);
 		dev_info(pfdev->base.dev, "bus_clock rate = %lu\n", rate);
-
-		err = clk_prepare_enable(pfdev->bus_clock);
-		if (err)
-			goto disable_clock;
 	}
 
 	pfdev->bus_ace_clock = devm_clk_get_optional(pfdev->base.dev, "bus_ace");
 	if (IS_ERR(pfdev->bus_ace_clock)) {
 		err = PTR_ERR(pfdev->bus_ace_clock);
 		dev_err(pfdev->base.dev, "get bus_ace_clock failed %d\n", err);
-		goto disable_bus_clock;
+		return err;
 	}
 
-	err = clk_prepare_enable(pfdev->bus_ace_clock);
-	if (err)
-		goto disable_bus_clock;
-
-	return 0;
-
-disable_bus_clock:
-	clk_disable_unprepare(pfdev->bus_clock);
-disable_clock:
-	clk_disable_unprepare(pfdev->clock);
-
-	return err;
+	return panfrost_clk_enable(pfdev, true);
 }
 
 static void panfrost_clk_fini(struct panfrost_device *pfdev)
 {
-	clk_disable_unprepare(pfdev->bus_ace_clock);
-	clk_disable_unprepare(pfdev->bus_clock);
-	clk_disable_unprepare(pfdev->clock);
+	panfrost_clk_disable(pfdev, true);
 }
 
 static int panfrost_regulator_init(struct panfrost_device *pfdev)
@@ -436,34 +450,17 @@ static int panfrost_device_runtime_resume(struct device *dev)
 		if (ret)
 			return ret;
 
-		ret = clk_enable(pfdev->clock);
-		if (ret)
-			goto err_clk;
-
-		ret = clk_enable(pfdev->bus_clock);
-		if (ret)
-			goto err_bus_clk;
-
-		ret = clk_enable(pfdev->bus_ace_clock);
-		if (ret)
-			goto err_bus_ace_clk;
+		ret = panfrost_clk_enable(pfdev, true);
+		if (ret) {
+			reset_control_assert(pfdev->rstc);
+			return ret;
+		}
 	}
 
 	panfrost_device_reset(pfdev, true);
 	panfrost_devfreq_resume(pfdev);
 
 	return 0;
-
-err_bus_ace_clk:
-	if (pfdev->comp->pm_features & BIT(GPU_PM_RT))
-		clk_disable(pfdev->bus_clock);
-err_bus_clk:
-	if (pfdev->comp->pm_features & BIT(GPU_PM_RT))
-		clk_disable(pfdev->clock);
-err_clk:
-	if (pfdev->comp->pm_features & BIT(GPU_PM_RT))
-		reset_control_assert(pfdev->rstc);
-	return ret;
 }
 
 static int panfrost_device_runtime_suspend(struct device *dev)
@@ -480,9 +477,7 @@ static int panfrost_device_runtime_suspend(struct device *dev)
 	panfrost_gpu_power_off(pfdev);
 
 	if (pfdev->comp->pm_features & BIT(GPU_PM_RT)) {
-		clk_disable(pfdev->bus_ace_clock);
-		clk_disable(pfdev->bus_clock);
-		clk_disable(pfdev->clock);
+		panfrost_clk_disable(pfdev, true);
 		reset_control_assert(pfdev->rstc);
 	}
 
@@ -506,13 +501,9 @@ static int panfrost_device_resume(struct device *dev)
 	}
 
 	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS)) {
-		ret = clk_enable(pfdev->clock);
+		ret = panfrost_clk_enable(pfdev, false);
 		if (ret)
 			goto err_clk;
-
-		ret = clk_enable(pfdev->bus_clock);
-		if (ret)
-			goto err_bus_clk;
 	}
 
 	ret = pm_runtime_force_resume(dev);
@@ -523,10 +514,7 @@ static int panfrost_device_resume(struct device *dev)
 
 err_resume:
 	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS))
-		clk_disable(pfdev->bus_clock);
-err_bus_clk:
-	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS))
-		clk_disable(pfdev->clock);
+		clk_disable_unprepare(pfdev->bus_clock);
 err_clk:
 	if (pfdev->comp->pm_features & BIT(GPU_PM_VREG_OFF))
 		dev_pm_opp_set_opp(dev, NULL);
@@ -542,10 +530,8 @@ static int panfrost_device_suspend(struct device *dev)
 	if (ret)
 		return ret;
 
-	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS)) {
-		clk_disable(pfdev->bus_clock);
-		clk_disable(pfdev->clock);
-	}
+	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS))
+		panfrost_clk_disable(pfdev, false);
 
 	if (pfdev->comp->pm_features & BIT(GPU_PM_VREG_OFF))
 		dev_pm_opp_set_opp(dev, NULL);
Signature · 1 line
2.55.0
author maintainer
#12 lore raw

PATCH v9 6/16 drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove

In reply to Adrián Larumbe

During device probe(), failure to do a PM get() will leave the usage_count set to 0, which is the value assigned at device creation time. That means when the autosuspend delay expires, runtime suspend callback won't be invoked, so the device will remain powered on forever.

On top of that, failure to call PM put() during device unplug means Panfrost device's PM usage_count increases monotonically for every new module reload.

The outcome of both of the above meant that:

- Devfreq OPP transition notifications would be printed all the time, even when no jobs are being submitted. This quickly fills the kernel ring buffer with junk.
- Because MMU interrupts are only enabled when the device is reset, the very first job targeting the tiler heap BO after device probe() would always time out, since the driver's PM runtime resume callback would not be invoked.

To fix the above:
- Manually adjust the PM refcnt at device probe and removal time.
- Ensure pm_runtime_dont_use_autosuspend is called in the wind-down path.
- Call pm_runtime_put_autosuspend() when device is ready to accept jobs
- Move pm_runtime_set_suspended() before panfrost_device_fini() so that resource unwinding happens in the opposite order as initialisation.

Fixes 635430797d3f ("drm/panfrost: Rework runtime PM initialization")
Fixes 876b15d2c88d ("drm/panfrost: Fix module unload")
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/panfrost/panfrost_drv.c +8 -2
index 55fc22e8d4d4..a3eff77add55 100644
@@ -854,6 +854,7 @@ static int panfrost_probe(struct platform_device *pdev)
 
 	pm_runtime_set_active(pfdev->base.dev);
 	pm_runtime_mark_last_busy(pfdev->base.dev);
+	pm_runtime_get_noresume(pfdev->base.dev);
 	pm_runtime_enable(pfdev->base.dev);
 	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
 	pm_runtime_use_autosuspend(pfdev->base.dev);
@@ -866,13 +867,16 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
+	pm_runtime_put_autosuspend(pfdev->base.dev);
 
 	return 0;
 
 err_out1:
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);
 err_out0:
 	return err;
 }
@@ -884,9 +888,11 @@ static void panfrost_remove(struct platform_device *pdev)
 	drm_dev_unregister(&pfdev->base);
 
 	pm_runtime_get_sync(pfdev->base.dev);
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);
 }
 
 static ssize_t profiling_show(struct device *dev,
Signature · 1 line
2.55.0
author maintainer
#18 lore raw

PATCH v9 7/16 drm/panfrost: Explicitly enable MMU interrupts at device init

In reply to Adrián Larumbe

Because the device must be in a position to accept jobs between the time drm_dev_register() is called and autosuspend first kicks in, there's a very narrow window inbetween during which jobs targeting the tiler buffer object would time out, since the device's PM status is 'Active', but no MMU interrupts were enabled at device initialisation time.

Fixes 73e467f60acd ("drm/panfrost: Consolidate reset handling")
---
 drivers/gpu/drm/panfrost/panfrost_mmu.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/panfrost/panfrost_mmu.c +9 -2
index 5c393ed6e310..faa62643caf1 100644
@@ -336,6 +336,12 @@ void panfrost_mmu_as_put(struct panfrost_device *pfdev, struct panfrost_mmu *mmu
 	WARN_ON(atomic_read(&mmu->as_count) < 0);
 }
 
+static void panfrost_mmu_enable_interrupts(struct panfrost_device *pfdev)
+{
+	mmu_write(pfdev, MMU_INT_CLEAR, ~0);
+	mmu_write(pfdev, MMU_INT_MASK, ~0);
+}
+
 void panfrost_mmu_reset(struct panfrost_device *pfdev)
 {
 	struct panfrost_mmu *mmu, *mmu_tmp;
@@ -355,8 +361,7 @@ void panfrost_mmu_reset(struct panfrost_device *pfdev)
 
 	spin_unlock(&pfdev->as_lock);
 
-	mmu_write(pfdev, MMU_INT_CLEAR, ~0);
-	mmu_write(pfdev, MMU_INT_MASK, ~0);
+	panfrost_mmu_enable_interrupts(pfdev);
 }
 
 static size_t get_pgsize(u64 addr, size_t size, size_t *count)
@@ -973,6 +978,8 @@ int panfrost_mmu_init(struct panfrost_device *pfdev)
 		return err;
 	}
 
+	panfrost_mmu_enable_interrupts(pfdev);
+
 	return 0;
 }
 
Signature · 1 line
2.55.0
author maintainer
#20 lore raw

PATCH v9 8/16 drm/panfrost: Move all DRM device initialisation into device_init()

In reply to Adrián Larumbe

Ideally the probe() function will do as little as possible, and all device initialisation and registration should happen inside the panfrost device subsystem, just like it's done in Panthor. This also simplifies resource unwinding in the error path.

Do the same thing for DRM driver remove, as in, sweep most of the action into panfrost_device_fini(), just like we did for device probe.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 37 +++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_drv.c    | 47 +-----------------------------
 2 files changed, 38 insertions(+), 46 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +37 -0
index 2fcbd223204c..ec2b1d29a26c 100644
@@ -8,6 +8,7 @@
 #include <linux/pm_domain.h>
 #include <linux/pm_runtime.h>
 #include <linux/regulator/consumer.h>
+#include <drm/drm_drv.h>
 
 #include "panfrost_device.h"
 #include "panfrost_devfreq.h"
@@ -230,6 +231,12 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 {
 	int err;
 
+	pfdev->comp = of_device_get_match_data(pfdev->base.dev);
+	if (!pfdev->comp)
+		return -ENODEV;
+
+	pfdev->coherent = device_get_dma_attr(pfdev->base.dev) == DEV_DMA_COHERENT;
+
 #ifdef CONFIG_DEBUG_FS
 	mutex_init(&pfdev->debugfs.gems_lock);
 	INIT_LIST_HEAD(&pfdev->debugfs.gems_list);
@@ -291,8 +298,31 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	if (err)
 		goto out_perfcnt;
 
+	pm_runtime_set_active(pfdev->base.dev);
+	pm_runtime_mark_last_busy(pfdev->base.dev);
+	pm_runtime_get_noresume(pfdev->base.dev);
+	pm_runtime_enable(pfdev->base.dev);
+	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
+	pm_runtime_use_autosuspend(pfdev->base.dev);
+
+	/*
+	 * Register the DRM device with the core and the connectors with
+	 * sysfs
+	 */
+	err = drm_dev_register(&pfdev->base, 0);
+	if (err < 0)
+		goto err_disable_rpm;
+
+	pm_runtime_put_autosuspend(pfdev->base.dev);
+
 	return 0;
 
+err_disable_rpm:
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_disable(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
+	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_gem_fini(pfdev);
 out_perfcnt:
 	panfrost_perfcnt_fini(pfdev);
 out_job:
@@ -316,6 +346,13 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 
 void panfrost_device_fini(struct panfrost_device *pfdev)
 {
+	pm_runtime_get_sync(pfdev->base.dev);
+
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_disable(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
+	pm_runtime_set_suspended(pfdev->base.dev);
+
 	panfrost_gem_fini(pfdev);
 	panfrost_perfcnt_fini(pfdev);
 	panfrost_jm_fini(pfdev);
drivers/gpu/drm/panfrost/panfrost_drv.c +1 -46

65 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#22 lore raw

PATCH v9 9/16 drm/panfrost: Add warning messages to fatal error conditions

In reply to Adrián Larumbe

Rather than just failing silently, let's warn the user of device remove not being able to take an PM reference or the PM suspend path still reporting inflight jobs. Neither situation should ever happen.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +3 -2
index ec2b1d29a26c..d75566a45392 100644
@@ -9,6 +9,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/regulator/consumer.h>
 #include <drm/drm_drv.h>
+#include <drm/drm_print.h>
 
 #include "panfrost_device.h"
 #include "panfrost_devfreq.h"
@@ -346,7 +347,7 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 
 void panfrost_device_fini(struct panfrost_device *pfdev)
 {
-	pm_runtime_get_sync(pfdev->base.dev);
+	drm_WARN_ON(&pfdev->base, pm_runtime_get_sync(pfdev->base.dev) < 0);
 
 	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
@@ -504,7 +505,7 @@ static int panfrost_device_runtime_suspend(struct device *dev)
 {
 	struct panfrost_device *pfdev = dev_get_drvdata(dev);
 
-	if (!panfrost_jm_is_idle(pfdev))
+	if (drm_WARN_ON(&pfdev->base, !panfrost_jm_is_idle(pfdev)))
 		return -EBUSY;
 
 	panfrost_devfreq_suspend(pfdev);
Signature · 1 line
2.55.0
author maintainer
#23 lore raw

PATCH v9 10/16 drm/panfrost: Add debugfs knob for manually triggering a GPU reset

In reply to Adrián Larumbe

This will be of great help when testing potential races between the GPU reset sequence and other parts of the code accessing HW registers.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 37 ++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpu/drm/panfrost/panfrost_device.c +37 -0
index d75566a45392..0aee1d87a7ab 100644
@@ -2,6 +2,7 @@
 /* Copyright 2018 Marty E. Plummer <[email protected]> */
 /* Copyright 2019 Linaro, Ltd, Rob Herring <[email protected]> */
 
+#include <linux/debugfs.h>
 #include <linux/clk.h>
 #include <linux/reset.h>
 #include <linux/platform_device.h>
@@ -583,9 +584,45 @@ EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = {
 };
 
 #ifdef CONFIG_DEBUG_FS
+static int reset_get(void *data, u64 *val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
+
+	*val = atomic_read(&pfdev->reset.pending);
+	return 0;
+}
+
+static int reset_set(void *data, u64 val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
+	int ret = pm_runtime_get_if_active(pfdev->base.dev);
+
+	if (ret) {
+		panfrost_device_schedule_reset(pfdev);
+		flush_work(&pfdev->reset.work);
+		if (ret > 0)
+			pm_runtime_put(pfdev->base.dev);
+	}
+
+	return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(panfrost_reset_debugfs_fops,
+			 reset_get, reset_set,
+			 "0x%08llx\n");
+
+static void panfrost_reset_debugfs_init(struct drm_minor *minor)
+{
+	debugfs_create_file("reset", 0600, minor->debugfs_root,
+			    minor->dev, &panfrost_reset_debugfs_fops);
+}
+
 void panfrost_device_debugfs_init(struct drm_minor *minor)
 {
 	panfrost_gem_debugfs_init(minor);
 	panfrost_sched_debugfs_init(minor);
+	panfrost_reset_debugfs_init(minor);
 }
 #endif // CONFIG_DEBUG_FS
Signature · 1 line
2.55.0
author maintainer
#28 lore raw

PATCH v9 11/16 drm/panfrost: Move perfcnt GPU disable sequence into a helper

In reply to Adrián Larumbe

Just for the sake of avoiding repetition.

---
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 31 ++++++++++++-----------------
 1 file changed, 13 insertions(+), 18 deletions(-)
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +13 -18
index 7020c0192e18..f71534e741b6 100644
@@ -35,6 +35,16 @@ struct panfrost_perfcnt {
 	struct completion dump_comp;
 };
 
+static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev)
+{
+	gpu_write(pfdev, GPU_PERFCNT_CFG,
+		  GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_OFF));
+	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0x0);
+	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0x0);
+	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0x0);
+	gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
+}
+
 void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev)
 {
 	complete(&pfdev->perfcnt->dump_comp);
@@ -193,12 +203,7 @@ static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev,
 	if (user != perfcnt->user)
 		return -EINVAL;
 
-	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0x0);
-	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0x0);
-	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0x0);
-	gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
-	gpu_write(pfdev, GPU_PERFCNT_CFG,
-		  GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_OFF));
+	panfrost_perfcnt_hw_disable(pfdev);
 
 	perfcnt->user = NULL;
 	drm_gem_vunmap(&perfcnt->mapping->obj->base.base, &map);
@@ -327,12 +332,7 @@ int panfrost_perfcnt_init(struct panfrost_device *pfdev)
 	perfcnt->bosize = size;
 
 	/* Start with everything disabled. */
-	gpu_write(pfdev, GPU_PERFCNT_CFG,
-		  GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_OFF));
-	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0);
-	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0);
-	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0);
-	gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
+	panfrost_perfcnt_hw_disable(pfdev);
 
 	init_completion(&perfcnt->dump_comp);
 	mutex_init(&perfcnt->lock);
@@ -344,10 +344,5 @@ int panfrost_perfcnt_init(struct panfrost_device *pfdev)
 void panfrost_perfcnt_fini(struct panfrost_device *pfdev)
 {
 	/* Disable everything before leaving. */
-	gpu_write(pfdev, GPU_PERFCNT_CFG,
-		  GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_OFF));
-	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0);
-	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0);
-	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0);
-	gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
+	panfrost_perfcnt_hw_disable(pfdev);
 }
Signature · 1 line
2.55.0
author maintainer
#30 lore raw

PATCH v9 12/16 drm/panfrost: Skip cache flush/invalidate when enabling perfcnt

In reply to Adrián Larumbe

The GPU cache flush/invalidate operation is unnecessary. First off, the GPU doesn't read off the perfcnt sample buffer, only writes into it, so an invalidate doesn't make a difference. Then flushing GPU caches after each sample has been written is enough for the CPU to see updated values.

---
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +2 -13
index f71534e741b6..ffc77121070e 100644
@@ -124,21 +124,10 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 	panfrost_gem_internal_set_label(&bo->base, "Perfcnt sample buffer");
 
 	/*
-	 * Invalidate the cache and clear the counters to start from a fresh
-	 * state.
+	 * Clear the counters to start from a fresh state.
 	 */
-	reinit_completion(&pfdev->perfcnt->dump_comp);
-	gpu_write(pfdev, GPU_INT_CLEAR,
-		  GPU_IRQ_CLEAN_CACHES_COMPLETED |
-		  GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
+	gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
 	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_CLEAR);
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_CLEAN_INV_CACHES);
-	ret = wait_for_completion_timeout(&pfdev->perfcnt->dump_comp,
-					  msecs_to_jiffies(1000));
-	if (!ret) {
-		ret = -ETIMEDOUT;
-		goto err_vunmap;
-	}
 
 	ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu);
 	if (ret < 0)
Signature · 1 line
2.55.0
author maintainer
#32 lore raw

PATCH v9 13/16 drm/panfrost: Avoid cache flush after perfcnt sample in fully coherent systems

In reply to Adrián Larumbe

If the GPU and CPU are fully coherent, then there's no need to flush the GPU caches after a perfcnt sample is made available.

---
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +4 -1
index ffc77121070e..b3f71d7fd82a 100644
@@ -52,7 +52,10 @@ void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev)
 
 void panfrost_perfcnt_sample_done(struct panfrost_device *pfdev)
 {
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_CLEAN_CACHES);
+	if (pfdev->features.selected_coherency != COHERENCY_ACE)
+		gpu_write(pfdev, GPU_CMD, GPU_CMD_CLEAN_CACHES);
+	else
+		complete(&pfdev->perfcnt->dump_comp);
 }
 
 static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
Signature · 1 line
2.55.0
author maintainer
#34 lore raw

PATCH v9 14/16 drm/panfrost: Introduce a reset lock

In reply to Adrián Larumbe

So as to avoid accessing HW register while a reset is ongoing, a read/write semaphore that envelopes the reset sequence will help driver entry points avoid racing with it. For now, the only such racy entry point is the ioctl that returns the current GPU timestmap.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 2 ++
 drivers/gpu/drm/panfrost/panfrost_device.h | 1 +
 drivers/gpu/drm/panfrost/panfrost_drv.c    | 9 ++++++---
 drivers/gpu/drm/panfrost/panfrost_job.c    | 1 +
 4 files changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +2 -0
index 0aee1d87a7ab..6c65feae63aa 100644
@@ -469,6 +469,8 @@ bool panfrost_exception_needs_reset(const struct panfrost_device *pfdev,
 
 void panfrost_device_reset(struct panfrost_device *pfdev, bool enable_job_int)
 {
+	guard(rwsem_write)(&pfdev->reset.lock);
+
 	panfrost_gpu_soft_reset(pfdev);
 
 	panfrost_gpu_power_on(pfdev);
drivers/gpu/drm/panfrost/panfrost_device.h +1 -0
index a0b9a2145fc9..63452febb1cf 100644
@@ -165,6 +165,7 @@ struct panfrost_device {
 	struct {
 		struct workqueue_struct *wq;
 		struct work_struct work;
+		struct rw_semaphore lock;
 		atomic_t pending;
 	} reset;
 
drivers/gpu/drm/panfrost/panfrost_drv.c +6 -3
index f77780c72a1a..571a26b84126 100644
@@ -41,9 +41,12 @@ static int panfrost_ioctl_query_timestamp(struct panfrost_device *pfdev,
 	if (ret)
 		return ret;
 
-	panfrost_cycle_counter_get(pfdev);
-	*arg = panfrost_timestamp_read(pfdev);
-	panfrost_cycle_counter_put(pfdev);
+	/* We should not read timestamp register while the GPU  is being reset */
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		panfrost_cycle_counter_get(pfdev);
+		*arg = panfrost_timestamp_read(pfdev);
+		panfrost_cycle_counter_put(pfdev);
+	}
 
 	pm_runtime_put(pfdev->base.dev);
 	return 0;
drivers/gpu/drm/panfrost/panfrost_job.c +1 -0
index 00fb36c577df..c2b1f1087e21 100644
@@ -877,6 +877,7 @@ int panfrost_jm_init(struct panfrost_device *pfdev)
 	INIT_WORK(&pfdev->reset.work, panfrost_reset_work);
 	spin_lock_init(&js->job_lock);
 	mutex_init(&pfdev->sched_lock);
+	init_rwsem(&pfdev->reset.lock);
 
 	js->irq = platform_get_irq_byname(to_platform_device(pfdev->base.dev), "job");
 	if (js->irq < 0)
Signature · 1 line
2.55.0
author maintainer
#36 lore raw

PATCH v9 15/16 drm/panfrost: Fix races between perfcnt and reset sequence

In reply to Adrián Larumbe

Formerly, the reset sequence would race with panfrost_mmu_as_put() when tearing down a perfcnt session. On top of that, poking GPU registers to program a perfcnt session or obtaining a dump might lead to undefined behaviour when done at the same time a reset was ongoing.

Use the reset r/w semaphore to govern access to the hardware at reset time. On top of that, expand the DRM uAPI for the perfcnt DUMP operation so that userspace can be made aware of a reset having happened, because that means counters will go back to 0 and can no longer be accumulated to values previously kept in user space.

The new perfcnt-aware reset sequence also takes care to reestablish perfcnt to its original configuration if there was an enabled session, or else flags the current session as dead if that failed.

Fixes 73e467f60acd ("drm/panfrost: Consolidate reset handling")
Fixes 7786fd108777 ("drm/panfrost: Expose performance counters through unstable ioctls")
---
 drivers/gpu/drm/panfrost/panfrost_device.c  |   2 +
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 201 +++++++++++++++++++---------
 drivers/gpu/drm/panfrost/panfrost_perfcnt.h |   1 +
 include/uapi/drm/panfrost_drm.h             |   8 +-
 4 files changed, 151 insertions(+), 61 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +2 -0
index 6c65feae63aa..e774f61c642b 100644
@@ -479,6 +479,8 @@ void panfrost_device_reset(struct panfrost_device *pfdev, bool enable_job_int)
 	panfrost_jm_reset_interrupts(pfdev);
 	if (enable_job_int)
 		panfrost_jm_enable_interrupts(pfdev);
+
+	panfrost_perfcnt_reset(pfdev);
 }
 
 static int panfrost_device_runtime_resume(struct device *dev)
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +141 -60

286 lines not on this page · raw message

drivers/gpu/drm/panfrost/panfrost_perfcnt.h +1 -0
index 8bbcf5f5fb33..8b9bc704b634 100644
@@ -14,5 +14,6 @@ int panfrost_ioctl_perfcnt_enable(struct drm_device *dev, void *data,
 				  struct drm_file *file_priv);
 int panfrost_ioctl_perfcnt_dump(struct drm_device *dev, void *data,
 				struct drm_file *file_priv);
+void panfrost_perfcnt_reset(struct panfrost_device *pfdev);
 
 #endif
include/uapi/drm/panfrost_drm.h +7 -1
index 50d5337f35ef..97e001040543 100644
@@ -47,7 +47,7 @@ extern "C" {
  * them for anything but debugging purpose.
  */
 #define DRM_IOCTL_PANFROST_PERFCNT_ENABLE	DRM_IOW(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_ENABLE, struct drm_panfrost_perfcnt_enable)
-#define DRM_IOCTL_PANFROST_PERFCNT_DUMP		DRM_IOW(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_DUMP, struct drm_panfrost_perfcnt_dump)
+#define DRM_IOCTL_PANFROST_PERFCNT_DUMP		DRM_IOWR(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_DUMP, struct drm_panfrost_perfcnt_dump)
 
 #define PANFROST_JD_REQ_FS (1 << 0)
 #define PANFROST_JD_REQ_CYCLE_COUNT (1 << 1)
@@ -270,8 +270,14 @@ struct drm_panfrost_perfcnt_enable {
 	__u32 counterset;
 };
 
+/* Perfcnt dump state as influenced by a HW reset */
+#define PANFROST_PERFCNT_SESSION_DEAD                 (1 << 0)
+#define PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET (1 << 1)
+
 struct drm_panfrost_perfcnt_dump {
 	__u64 buf_ptr;
+	__u32 state;
+	__u32 pad;		/* MBZ */
 };
 
 /* madvise provides a way to tell the kernel in case a buffers contents
Signature · 1 line
2.55.0
author maintainer
#42 lore raw

PATCH v9 16/16 drm/panfrost: Bump driver minor to reflect new DUMP IOCTL req field

In reply to Adrián Larumbe

Expose expanded PERFCNT_DUMP IOCTL interface to user mode.

---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -1
index 571a26b84126..de9b1c115181 100644
@@ -808,6 +808,7 @@ static const struct file_operations panfrost_drm_driver_fops = {
  * - 1.6 - adds PANFROST_BO_MAP_WB, PANFROST_IOCTL_SYNC_BO,
  *	   PANFROST_IOCTL_QUERY_BO_INFO and
  *	   DRM_PANFROST_PARAM_SELECTED_COHERENCY
+ * - 1.7 - adds PERFCNT_DUMP req state field
  */
 static const struct drm_driver panfrost_drm_driver = {
 	.driver_features	= DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ,
@@ -820,7 +821,7 @@ static const struct drm_driver panfrost_drm_driver = {
 	.name			= "panfrost",
 	.desc			= "panfrost DRM",
 	.major			= 1,
-	.minor			= 6,
+	.minor			= 7,
 
 	.gem_create_object	= panfrost_gem_create_object,
 	.gem_prime_import	= panfrost_gem_prime_import,
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH v9 05/16] drm/panfrost: Consolidate device clock management and reset

In reply to Adrián Larumbe
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Missing clock disable/unprepare for `pfdev->clock` in the error path of…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing clock disable/unprepare for pfdev->clock in the error path of panfrost_device_resume(). --

commit 57e1cc8c0bd76260d5e27d81a245c42e7661aa0a
Author: Adrián Larumbe <[email protected]>

drm/panfrost: Consolidate device clock management and reset

This commit gathers clock enables and disables into a single function to avoid repetitions between driver init/fini and device resume/suspend. It also groups clk prepares and enables into the same function calls.

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_device.c +0 -0
index b3a53504bd016..2fcbd223204c4 100644

[ ... ]

Quoted 16 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -5
@@ -506,13 +501,9 @@ static int panfrost_device_resume(struct device *dev)
 	}
 
 	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS)) {
-		ret = clk_enable(pfdev->clock);
+		ret = panfrost_clk_enable(pfdev, false);
 		if (ret)
 			goto err_clk;
-
-		ret = clk_enable(pfdev->bus_clock);
-		if (ret)
-			goto err_bus_clk;
 	}
 
 	ret = pm_runtime_force_resume(dev);

drivers/gpu/drm/panfrost/panfrost_device.c static int panfrost_device_resume(struct device *dev) · line 518

Quoted 9 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -4
@@ -523,10 +514,7 @@ static int panfrost_device_resume(struct device *dev)
 
 err_resume:
 	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS))
-		clk_disable(pfdev->bus_clock);
-err_bus_clk:
-	if (pfdev->comp->pm_features & BIT(GPU_PM_CLK_DIS))
-		clk_disable(pfdev->clock);
+		clk_disable_unprepare(pfdev->bus_clock);
 err_clk:

[Severity: Medium]
When pm_runtime_force_resume() fails and jumps to err_resume, does this code leak the clock preparation and enable reference counts for pfdev->clock?

Because panfrost_clk_enable(pfdev, false) prepares and enables both pfdev->clock and pfdev->bus_clock, disabling only pfdev->bus_clock here seems to leave pfdev->clock enabled. Should panfrost_clk_disable(pfdev, false) be used on this path instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#33 lore raw

Re: [PATCH v9 13/16] drm/panfrost: Avoid cache flush after perfcnt sample in fully coherent systems

In reply to Adrián Larumbe
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Waking up the user thread before clearing the GPU interrupt introduces a race condition that can…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Waking up the user thread before clearing the GPU interrupt introduces a race condition that can clear the interrupt of a subsequent performance counter dump, causing it to time out. --

commit f36520c5a42efb4216abf8dab5756d0a6134f2a4
Author: Adrián Larumbe <[email protected]>

drm/panfrost: Avoid cache flush after perfcnt sample in fully coherent systems

This commit skips the GPU cache flush when the system is fully coherent (ACE). Instead of sending a cache clean command, it immediately signals completion of the performance counter sample.

drivers/gpu/drm/panfrost/panfrost_perfcnt.c void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev) · line 57

Quoted 12 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +3 -1
index ffc77121070e2..b3f71d7fd82a9 100644
@@ -52,7 +52,10 @@ void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev)
 
 void panfrost_perfcnt_sample_done(struct panfrost_device *pfdev)
 {
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_CLEAN_CACHES);
+	if (pfdev->features.selected_coherency != COHERENCY_ACE)
+		gpu_write(pfdev, GPU_CMD, GPU_CMD_CLEAN_CACHES);
+	else

[Severity: Medium]
Does waking up the user thread before clearing the GPU interrupt introduce a race condition that can drop subsequent sample interrupts?

If panfrost_perfcnt_sample_done() calls complete() and wakes up the user thread, the thread can return to userspace and immediately issue a new PERFCNT_DUMP ioctl, starting a new hardware sample.

If the IRQ handler on the original CPU is delayed or preempted (e.g., by a hypervisor or NMI) before it writes to the clear register:

drivers/gpu/drm/panfrost/panfrost_gpu.c:panfrost_gpu_irq_handler() {
    ...
    if (state & GPU_IRQ_PERFCNT_SAMPLE_COMPLETED)
        panfrost_perfcnt_sample_done(pfdev);
    
    ...
    gpu_write(pfdev, GPU_INT_CLEAR, state);
}

Could it write the original state (which has the completion bit set) to GPU_INT_CLEAR after the new sample has already started? This would clear the interrupt for the newly submitted sample, causing the new dump's wait to time out with -ETIMEDOUT.

Prior to this patch, the CLEAN_CACHES step prevented this race because the delayed completion meant the interrupt clear could not hit a new sample.

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +1 -0
+		complete(&pfdev->perfcnt->dump_comp);
 }
 
 static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#29 lore raw

Re: [PATCH v9 11/16] drm/panfrost: Move perfcnt GPU disable sequence into a helper

In reply to Adrián Larumbe
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Use-after-free of pfdev->perfcnt in GPU interrupt handler on device unbind due to devres cleanup order. --

commit 62f6a919f812a1a1e64197b8f8a6a6d6f4790287
Author: Adrián Larumbe <[email protected]>

drm/panfrost: Move perfcnt GPU disable sequence into a helper

This commit factors out the performance counter hardware disable sequence into a new helper function panfrost_perfcnt_hw_disable() to reduce repetition.

Quoted 11 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +3 -0
index 7020c0192e189..f71534e741b65 100644
@@ -35,6 +35,16 @@ struct panfrost_perfcnt {
 	struct completion dump_comp;
 };
 
+static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev)
+{
+	gpu_write(pfdev, GPU_PERFCNT_CFG,

drivers/gpu/drm/panfrost/panfrost_perfcnt.c struct panfrost_perfcnt { · line 50

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +7 -0
+		  GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_OFF));
+	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0x0);
+	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0x0);
+	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0x0);
+	gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
+}
+
 void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev)
 {
 	complete(&pfdev->perfcnt->dump_comp);

[Severity: High]
This isn't a bug introduced by this patch, but can this dereference of pfdev->perfcnt in panfrost_perfcnt_clean_cache_done() lead to a use-after-free during device unbind?

The allocation order in panfrost_device_init() establishes a devres LIFO free order that could cause this:

panfrost_device.c:panfrost_device_init() {
    ...
    err = panfrost_gpu_init(pfdev);
    // Registers GPU IRQ via devm_request_irq()
    ...
    err = panfrost_perfcnt_init(pfdev);
    // Allocates pfdev->perfcnt via devm_kzalloc()
    ...
}

Because devres cleans up in LIFO order, on device unbind, pfdev->perfcnt is freed before the GPU IRQ handler is unregistered.

Additionally, panfrost_device_fini() temporarily un-suspends the device, which unmasks the hardware interrupts via panfrost_device_reset(), and then calls panfrost_gpu_fini(). While panfrost_gpu_fini() powers off the hardware, it does not mask the interrupts.

If the shared IRQ line fires during this window, or if reading GPU_INT_STAT returns 0xFFFFFFFF because power domains are off, panfrost_gpu_irq_handler() will execute, pass the state & GPU_IRQ_CLEAN_CACHES_COMPLETED check, and call panfrost_perfcnt_clean_cache_done().

Would this result in a use-after-free when accessing pfdev->perfcnt->dump_comp?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#19 lore raw

Re: [PATCH v9 07/16] drm/panfrost: Explicitly enable MMU interrupts at device init

In reply to Adrián Larumbe
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the GPU reset sequence (panfrost_reset) synchronizes the Job Management (JM) IRQ but fails to mask or synchronize the MMU IRQ, leading to a TOCTOU data race that can corrupt the driver state for newly submitted jobs.
- [High] This problem wasn't introduced by this patch, but during driver unbind (panfrost_remove) or probe failure, clocks and power domains are disabled while shared IRQ handlers remain active, which can lead to kernel crashes if the shared IRQ line is asserted. --

commit 8161749d65dccd8c767c11da6233beaf16346d71
Author: Adrián Larumbe <[email protected]>

drm/panfrost: Explicitly enable MMU interrupts at device init

Because the device must be in a position to accept jobs between the time drm_dev_register() is called and autosuspend first kicks in, this commit enables MMU interrupts at device initialisation time. This avoids a narrow window where jobs targeting the tiler buffer object could time out.

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_mmu.c +0 -0
index 5c393ed6e3101..faa62643caf1d 100644

[ ... ]

drivers/gpu/drm/panfrost/panfrost_mmu.c void panfrost_mmu_reset(struct panfrost_device *pfdev) · line 364

Quoted 7 lines
drivers/gpu/drm/panfrost/panfrost_mmu.c +1 -2
@@ -355,8 +361,7 @@ void panfrost_mmu_reset(struct panfrost_device *pfdev)
 
 	spin_unlock(&pfdev->as_lock);
 
-	mmu_write(pfdev, MMU_INT_CLEAR, ~0);
-	mmu_write(pfdev, MMU_INT_MASK, ~0);
+	panfrost_mmu_enable_interrupts(pfdev);

[Severity: High]
This isn't a bug introduced by this patch, but does the GPU reset sequence have a data race here?

When panfrost_reset() in drivers/gpu/drm/panfrost/panfrost_job.c triggers a reset, it synchronizes the JM IRQ but fails to mask or synchronize the MMU IRQ:

drivers/gpu/drm/panfrost/panfrost_job.c:panfrost_reset() {
	...
	job_write(pfdev, JOB_INT_MASK, 0);
	synchronize_irq(pfdev->js->irq);
	...
}

Because the MMU IRQ isn't synchronized, panfrost_mmu_irq_handler_thread() can execute concurrently with panfrost_device_reset() and panfrost_mmu_reset().

Since panfrost_mmu_enable_interrupts() modifies the MMU_INT_MASK hardware register outside of pfdev->as_lock, could this race cause pfdev->as_faulty_mask and the hardware register to become inconsistent if a fault occurs exactly during reset, leading to timeouts for newly submitted jobs?

Quoted 2 lines
drivers/gpu/drm/panfrost/panfrost_mmu.c +0 -0
 }
 

[ ... ]

drivers/gpu/drm/panfrost/panfrost_mmu.c int panfrost_mmu_init(struct panfrost_device *pfdev) · line 982

Quoted 6 lines
drivers/gpu/drm/panfrost/panfrost_mmu.c +2 -0
@@ -973,6 +978,8 @@ int panfrost_mmu_init(struct panfrost_device *pfdev)
 		return err;
 	}
 
+	panfrost_mmu_enable_interrupts(pfdev);
+

[Severity: High]
This is a pre-existing issue, but could enabling interrupts here cause a bus error if the probe fails or during driver unbind?

If probe fails later in panfrost_device_init() in drivers/gpu/drm/panfrost/panfrost_device.c, or during module unload in panfrost_remove() in drivers/gpu/drm/panfrost/panfrost_drv.c, panfrost_mmu_fini() is called to clear the mask:

drivers/gpu/drm/panfrost/panfrost_mmu.c:panfrost_mmu_fini() {
	mmu_write(pfdev, MMU_INT_MASK, 0);
}

However, this doesn't set PANFROST_COMP_BIT_MMU in pfdev->is_suspended or call synchronize_irq(). The clocks and power domains are then disabled, but the devm-managed shared IRQ remains registered until the probe or remove function fully returns.

If the shared IRQ line is asserted during this teardown window, won't panfrost_mmu_irq_handler() bypass the is_suspended check and read MMU_INT_STAT without active clocks, resulting in a synchronous external abort?

Quoted 3 lines
drivers/gpu/drm/panfrost/panfrost_mmu.c +0 -0
 	return 0;
 }
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#35 lore raw

Re: [PATCH v9 14/16] drm/panfrost: Introduce a reset lock

In reply to Adrián Larumbe
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Unprotected hardware register access in panfrost_gpu_get_latest_flush_id() risks data corruption and bus errors during concurrent GPU resets.
- [High] Power leak in panfrost_ioctl_query_timestamp() due to incorrect PM runtime put API.
--

commit 8d0ec71108e67c1469d4f0da23df91f3320d12ae
Author: Adrián Larumbe <[email protected]>

drm/panfrost: Introduce a reset lock

So as to avoid accessing HW register while a reset is ongoing, a read/write semaphore that envelopes the reset sequence will help driver entry points avoid racing with it. For now, the only such racy entry point is the ioctl that returns the current GPU timestmap.

Quoted 2 lines

For now, the only such racy entry point is the ioctl that returns the current GPU timestmap.

[Severity: High]
This is a pre-existing issue, but does panfrost_ioctl_submit() in drivers/gpu/drm/panfrost/panfrost_drv.c also need to hold pfdev->reset.lock?

It calls panfrost_gpu_get_latest_flush_id() in drivers/gpu/drm/panfrost/panfrost_gpu.c, which reads the GPU_LATEST_FLUSH_ID hardware register directly:

panfrost_ioctl_submit()

  job->flush_id = panfrost_gpu_get_latest_flush_id(pfdev);
    flush_id = gpu_read(pfdev, GPU_LATEST_FLUSH_ID);

If a user calls panfrost_ioctl_submit() concurrently with a job timeout triggering panfrost_reset(), reading the hardware register during a GPU soft reset could return garbage data or trigger a bus error. A corrupted flush ID could cause the GPU to skip necessary cache flushes.

[ ... ]

Quoted 9 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -1
index f77780c72a1ae..571a26b841269 100644
@@ -41,9 +41,12 @@ static int panfrost_ioctl_query_timestamp(struct panfrost_device *pfdev,
 	if (ret)
 		return ret;
 
-	panfrost_cycle_counter_get(pfdev);

drivers/gpu/drm/panfrost/panfrost_drv.c static int panfrost_ioctl_query_timestamp(struct panfrost_device *pfdev, · line 51

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +6 -2
-	*arg = panfrost_timestamp_read(pfdev);
-	panfrost_cycle_counter_put(pfdev);
+	/* We should not read timestamp register while the GPU  is being reset */
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		panfrost_cycle_counter_get(pfdev);
+		*arg = panfrost_timestamp_read(pfdev);
+		panfrost_cycle_counter_put(pfdev);
+	}
 
 	pm_runtime_put(pfdev->base.dev);

[Severity: High]
This isn't a bug introduced by this patch, but does calling pm_runtime_put() in panfrost_ioctl_query_timestamp() cause the GPU power to remain on indefinitely?

Since the driver uses autosuspend (dev->power.use_autosuspend is true), the PM core's rpm_idle returns -EAGAIN and fails to schedule the suspend timer when pm_runtime_put() is called instead of pm_runtime_put_autosuspend(). Any unprivileged user calling the query IOCTL could trigger this.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#37 lore raw

Re: [PATCH v9 15/16] drm/panfrost: Fix races between perfcnt and reset sequence

In reply to Adrián Larumbe
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Mixing goto-based error handling with scoped_guard() cleanup in panfrost_perfcnt_enable_locked()…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Mixing goto-based error handling with scoped_guard() cleanup in panfrost_perfcnt_enable_locked() violates cleanup API semantics. --

commit 6a11ea2bc38694e85eb36147ae0e357afc1cc799
Author: Adrián Larumbe <[email protected]>

drm/panfrost: Fix races between perfcnt and reset sequence

This commit addresses concurrency issues between the reset sequence and perfcnt session teardown by governing hardware access with the reset r/w semaphore. It also expands the uAPI to notify userspace if a reset has occurred during a perfcnt session.

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +0 -0
index b3f71d7fd82a9..9847657179a57 100644

[ ... ]

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +2 -0
@@ -122,54 +203,31 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 	ret = drm_gem_vmap(&bo->base, &map);
 	if (ret)
 		goto err_put_mapping;
+
 	perfcnt->buf = map.vaddr;
+	perfcnt->counterset = counterset;
 
 	panfrost_gem_internal_set_label(&bo->base, "Perfcnt sample buffer");
 

[ ... ]

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev, · line 221

Quoted 11 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +9 -1
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		/*
+		 * Clear the counters to start from a fresh state.
+		 */
+		gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
+		gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_CLEAR);
 
-	gpu_write(pfdev, GPU_PERFCNT_CFG, cfg);
+		ret = panfrost_perfcnt_hw_enable(pfdev);
+		if (ret)
+			goto err_vunmap;

[Severity: Low]
Does this mix goto-based error handling with scope-based cleanup?

The kernel cleanup API guidelines state that we should never mix goto labels and cleanup helpers (like scoped_guard) in the same function. Jumping out of a scoped_guard block to an error label creates confusing ownership semantics. For a given routine, the documentation recommends converting all resources that need a goto cleanup to scope-based cleanup, or converting none of them.

Quoted 9 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +4 -2
-	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+		perfcnt->reset_happened = false;
+		perfcnt->owns_as_ref = true;
+		perfcnt->user = user;
+	}
 
 	/* The BO ref is retained by the mapping. */
 	drm_gem_object_put(&bo->base);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#13 lore raw

Re: [PATCH v9 06/16] drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove

In reply to Adrián Larumbe
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

New issues:
- [High] Setting PM state to suspended before hardware teardown completes causes bus errors.

Pre-existing issues:
- [High] Failure of pm_runtime_get_sync() is ignored in panfrost_remove(), which can lead to fatal MMIO accesses.
- [High] Concurrent IOCTLs during device removal can cause Use-After-Free or hardware faults.
- [High] Interrupt handlers are not synchronized before hardware clocks are disabled and scheduler structures are destroyed. --

commit fc7ca29ac5bbdcbf8ed388bccbba4ac531c7ed14
Author: Adrián Larumbe <[email protected]>

drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove

This patch fixes PM refcnt and autosuspend issues during device probe and removal in the panfrost driver. It ensures the PM usage count is properly managed so the device does not remain powered on forever or increase its refcnt monotonically.

Quoted 3 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -0

[ ... ]

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +1 -0
@@ -866,13 +867,16 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
+	pm_runtime_put_autosuspend(pfdev->base.dev);

drivers/gpu/drm/panfrost/panfrost_drv.c static int panfrost_probe(struct platform_device *pdev) · line 879

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +3 -1
 
 	return 0;
 
 err_out1:
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);

[Severity: High]
Does moving pm_runtime_set_suspended() before panfrost_device_fini() cause bus errors? pm_runtime_set_suspended() internally drops the PM runtime active reference on the PM domains via rpm_put_suppliers(). Because the PM domains may power off asynchronously or synchronously when their usage count drops, they can be disabled while panfrost_device_fini() is still executing.

Subsequently, functions like panfrost_jm_fini() and panfrost_mmu_fini() perform direct MMIO accesses on unclocked or powered-down hardware, which causes a synchronous external abort and a kernel panic. Note that this bug is not fixed in the subsequent patches of the series (e.g. Patch 8 just moves the buggy sequence into panfrost_device_fini).

drivers/gpu/drm/panfrost/panfrost_drv.c static void panfrost_remove(struct platform_device *pdev) · line 888

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -0
 err_out0:
 	return err;
 }
@@ -884,9 +888,11 @@ static void panfrost_remove(struct platform_device *pdev)
 	drm_dev_unregister(&pfdev->base);

[Severity: High]
This isn't a bug introduced by this patch, but does drm_dev_unregister() prevent concurrent IOCTLs from open file descriptors? drm_dev_unregister() removes the sysfs node but already-open file descriptors can still execute IOCTLs (e.g. panfrost_ioctl_submit).

Because Panfrost does not use drm_dev_unplug() and drm_dev_enter()/exit() in its IOCTL paths, a concurrent IOCTL can proceed, access the DRM scheduler, or write to MMIO while panfrost_device_fini() destroys the schedulers and disables clocks, causing a Use-After-Free or a kernel panic. Note that this missing synchronization remains unfixed at the end of the patch series.

drivers/gpu/drm/panfrost/panfrost_drv.c static void panfrost_remove(struct platform_device *pdev) · line 890

Quoted 2 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -0
 	pm_runtime_get_sync(pfdev->base.dev);

[Severity: High]
This isn't a bug introduced by this patch, but does ignoring the return value of pm_runtime_get_sync() lead to fatal MMIO accesses? If resuming fails, the device remains unclocked. Execution proceeds unconditionally into panfrost_device_fini(), which performs MMIO writes to the hardware. Writing to MMIO registers when the device is powered off causes a bus fault / SError, resulting in a kernel panic.

Although remove() cannot return an error, the code must check the return value and skip hardware accesses if resuming failed. Note that a follow-up patch (Patch 9) wraps this call in a drm_WARN_ON, but it still does not handle the error gracefully by aborting the hardware teardown steps, so the crash still occurs.

drivers/gpu/drm/panfrost/panfrost_drv.c static void panfrost_remove(struct platform_device *pdev) · line 895

Quoted 6 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +3 -1
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);

[Severity: High]
This isn't a bug introduced by this patch, but does this teardown sequence properly synchronize interrupts? panfrost_device_fini() calls panfrost_mmu_fini() and panfrost_jm_fini(), which mask interrupts via MMIO writes (e.g. mmu_write(pfdev, MMU_INT_MASK, 0)), but they do not call synchronize_irq().

If the IRQ handler thread is running concurrently on another CPU, it will continue executing and attempt to read MMIO registers with clocks disabled or access destroyed scheduler queues, leading to a UAF or kernel panic. This teardown synchronization issue remains unfixed at the end of the patch series.

Also, as noted in the panfrost_probe() error path, moving pm_runtime_set_suspended() before panfrost_device_fini() risks disabling PM domains while teardown is still ongoing, leading to bus errors.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
maintainer Reviewed-by on v9 1/16
#2 lore raw

Re: [PATCH v9 01/16] drm/panfrost: Move shrinker initialization and unplug one level down

In reply to Adrián Larumbe
Quoted 10 lines · On Sat, 12 Sep 2026 00:28:02 +0100 Adrián Larumbe…

Since the moment we call drm_dev_register() the device should be in a position to accept jobs, so it's best if the shrinker is already initialized by then.

On top of that, make shrinker functions take an panfrost_device pointer like other functions in the same sequence and rename them accordingly.

Essentially mimic the init/fini behaviour in Panthor.

One remark below.

Quoted 13 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c       |  8 +++++++-
 drivers/gpu/drm/panfrost/panfrost_drv.c          |  6 ------
 drivers/gpu/drm/panfrost/panfrost_gem.c          | 26 ++++++++++++++----------
 drivers/gpu/drm/panfrost/panfrost_gem.h          |  7 ++++---
 drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c |  8 ++------
 5 files changed, 28 insertions(+), 27 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +0 -0
index 485349faf251..05c40d5a20b5 100644

drivers/gpu/drm/panfrost/panfrost_device.c int panfrost_device_init(struct panfrost_device *pfdev) · line 283

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
@@ -280,9 +280,14 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	if (err)
 		goto out_job;
 
-	panfrost_gem_init(pfdev);
+	err = panfrost_gem_init(pfdev);

It feels weird to have the GEM subsystem initialized last when you consider the fact other subsystems might want to allocate GEMs in their _init() function. I know it's where the panfrost_gem_init() is right now, and that ultimately it doesn't prevent anyone from allocating GEMs, but I think it would make sense have this called before any of the other subsystem init functions, still.

Quoted 139 lines
drivers/gpu/drm/panfrost/panfrost_device.c +6 -0
+	if (err)
+		goto out_perfcnt;
 
 	return 0;
+
+out_perfcnt:
+	panfrost_perfcnt_fini(pfdev);
 out_job:
 	panfrost_jm_fini(pfdev);
 out_mmu:
@@ -304,6 +309,7 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 
 void panfrost_device_fini(struct panfrost_device *pfdev)
 {
+	panfrost_gem_fini(pfdev);
 	panfrost_perfcnt_fini(pfdev);
 	panfrost_jm_fini(pfdev);
 	panfrost_mmu_fini(pfdev);
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -6
index 96a66d673ba6..9882a3ede75f 100644
@@ -1003,14 +1003,9 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
-	err = panfrost_gem_shrinker_init(&pfdev->base);
-	if (err)
-		goto err_out2;
 
 	return 0;
 
-err_out2:
-	drm_dev_unregister(&pfdev->base);
 err_out1:
 	pm_runtime_disable(pfdev->base.dev);
 	panfrost_device_fini(pfdev);
@@ -1024,7 +1019,6 @@ static void panfrost_remove(struct platform_device *pdev)
 	struct panfrost_device *pfdev = platform_get_drvdata(pdev);
 
 	drm_dev_unregister(&pfdev->base);
-	panfrost_gem_shrinker_cleanup(&pfdev->base);
 
 	pm_runtime_get_sync(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
drivers/gpu/drm/panfrost/panfrost_gem.c +15 -11
index 3a7fce428898..f39c0f50ed4f 100644
@@ -15,20 +15,24 @@
 #include "panfrost_gem.h"
 #include "panfrost_mmu.h"
 
-void panfrost_gem_init(struct panfrost_device *pfdev)
+int panfrost_gem_init(struct panfrost_device *pfdev)
 {
-	int err;
+	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && panfrost_transparent_hugepage) {
+		int err = drm_gem_huge_mnt_create(&pfdev->base, "within_size");
+
+		if (drm_gem_get_huge_mnt(&pfdev->base))
+			drm_info(&pfdev->base, "Using Transparent Hugepage\n");
+		else if (err)
+			drm_warn(&pfdev->base, "Can't use Transparent Hugepage (%d)\n",
+				 err);
+	}
 
-	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
-	    !panfrost_transparent_hugepage)
-		return;
+	return panfrost_gem_shrinker_init(pfdev);
+}
 
-	err = drm_gem_huge_mnt_create(&pfdev->base, "within_size");
-	if (drm_gem_get_huge_mnt(&pfdev->base))
-		drm_info(&pfdev->base, "Using Transparent Hugepage\n");
-	else if (err)
-		drm_warn(&pfdev->base, "Can't use Transparent Hugepage (%d)\n",
-			 err);
+void panfrost_gem_fini(struct panfrost_device *pfdev)
+{
+	panfrost_gem_shrinker_fini(pfdev);
 }
 
 #ifdef CONFIG_DEBUG_FS
drivers/gpu/drm/panfrost/panfrost_gem.h +4 -3
index 79d4377019e9..5c823cdbd980 100644
@@ -129,7 +129,8 @@ drm_mm_node_to_panfrost_mapping(struct drm_mm_node *node)
 	return container_of(node, struct panfrost_gem_mapping, mmnode);
 }
 
-void panfrost_gem_init(struct panfrost_device *pfdev);
+int panfrost_gem_init(struct panfrost_device *pfdev);
+void panfrost_gem_fini(struct panfrost_device *pfdev);
 
 struct drm_gem_object *panfrost_gem_create_object(struct drm_device *dev, size_t size);
 
@@ -154,8 +155,8 @@ panfrost_gem_mapping_get(struct panfrost_gem_object *bo,
 void panfrost_gem_mapping_put(struct panfrost_gem_mapping *mapping);
 void panfrost_gem_teardown_mappings_locked(struct panfrost_gem_object *bo);
 
-int panfrost_gem_shrinker_init(struct drm_device *dev);
-void panfrost_gem_shrinker_cleanup(struct drm_device *dev);
+int panfrost_gem_shrinker_init(struct panfrost_device *pfdev);
+void panfrost_gem_shrinker_fini(struct panfrost_device *pfdev);
 
 void panfrost_gem_set_label(struct drm_gem_object *obj, const char *label);
 int panfrost_gem_sync(struct drm_gem_object *obj, u32 type,
drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c +2 -6
index 2fe967a90bcb..fefae87535d6 100644
@@ -95,10 +95,8 @@ panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  *
  * This function registers and sets up the panfrost shrinker.
  */
-int panfrost_gem_shrinker_init(struct drm_device *dev)
+int panfrost_gem_shrinker_init(struct panfrost_device *pfdev)
 {
-	struct panfrost_device *pfdev = to_panfrost_device(dev);
-
 	pfdev->shrinker = shrinker_alloc(0, "drm-panfrost");
 	if (!pfdev->shrinker)
 		return -ENOMEM;
@@ -118,10 +116,8 @@ int panfrost_gem_shrinker_init(struct drm_device *dev)
  *
  * This function unregisters the panfrost shrinker.
  */
-void panfrost_gem_shrinker_cleanup(struct drm_device *dev)
+void panfrost_gem_shrinker_fini(struct panfrost_device *pfdev)
 {
-	struct panfrost_device *pfdev = to_panfrost_device(dev);
-
 	if (pfdev->shrinker)
 		shrinker_free(pfdev->shrinker);
 }
maintainer Reviewed-by on v9 5/16
#10 lore raw

Re: [PATCH v9 05/16] drm/panfrost: Consolidate device clock management and reset

In reply to Adrián Larumbe
Quoted 9 lines · On Sat, 12 Sep 2026 00:28:06 +0100 Adrián Larumbe…

Gather all clock enables and disables into a single function to avoid repetitions between driver init/fini and device resume/suspend, since these clocks are always handled in bulk.

Also do clk (un)prepares and dis/enables at the same time, since the clk_prepare_* family of functions can simply increase the refcnt of an already prepared clock.

Two nits below.

Quoted 9 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c | 118 +++++++++++++----------------
 1 file changed, 52 insertions(+), 66 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +0 -0
index b3a53504bd01..2fcbd223204c 100644

drivers/gpu/drm/panfrost/panfrost_device.c static void panfrost_reset_fini(struct panfrost_device *pfdev) · line 37

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
@@ -34,10 +34,46 @@ static void panfrost_reset_fini(struct panfrost_device *pfdev)
 	reset_control_assert(pfdev->rstc);
 }
 
-static int panfrost_clk_init(struct panfrost_device *pfdev)
+static int panfrost_clk_enable(struct panfrost_device *pfdev, bool enable_ace_clock)

I don't want to have a flag added for each new clock we decide to keep enabled on RPM suspend, so how about s/enable_ace_clock/on_resume/? Also, since it's enabling more than one clk, I'd go s/panfrost_clk_enable/panfrost_clks_enable/.

Quoted 18 lines
drivers/gpu/drm/panfrost/panfrost_device.c +16 -0
 {
 	int err;
+
+	err = clk_prepare_enable(pfdev->clock);
+	if (err)
+		return err;
+
+	err = clk_prepare_enable(pfdev->bus_clock);
+	if (err)
+		goto disable_clock;
+
+	if (enable_ace_clock) {
+		err = clk_prepare_enable(pfdev->bus_ace_clock);
+		if (err)
+			goto disable_bus_clock;
+	}
+
+	return 0;

drivers/gpu/drm/panfrost/panfrost_device.c static void panfrost_reset_fini(struct panfrost_device *pfdev) · line 65

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_device.c +10 -0
+
+disable_bus_clock:
+	clk_disable_unprepare(pfdev->bus_clock);
+disable_clock:
+	clk_disable_unprepare(pfdev->clock);
+
+	return err;
+}
+
+static void panfrost_clk_disable(struct panfrost_device *pfdev, bool disable_ace_clock)

Similarly, I'd go s/disable_ace_clock/on_suspend/ and s/panfrost_clk_disable/panfrost_clks_disable/.

maintainer
#14 lore raw

Re: [PATCH v9 06/16] drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove

In reply to Adrián Larumbe
Quoted 50 lines · On Sat, 12 Sep 2026 00:28:07 +0100 Adrián Larumbe…

During device probe(), failure to do a PM get() will leave the usage_count set to 0, which is the value assigned at device creation time. That means when the autosuspend delay expires, runtime suspend callback won't be invoked, so the device will remain powered on forever.

On top of that, failure to call PM put() during device unplug means Panfrost device's PM usage_count increases monotonically for every new module reload.

The outcome of both of the above meant that:

- Devfreq OPP transition notifications would be printed all the time, even when no jobs are being submitted. This quickly fills the kernel ring buffer with junk.
- Because MMU interrupts are only enabled when the device is reset, the very first job targeting the tiler heap BO after device probe() would always time out, since the driver's PM runtime resume callback would not be invoked.

To fix the above:
- Manually adjust the PM refcnt at device probe and removal time.
- Ensure pm_runtime_dont_use_autosuspend is called in the wind-down path.
- Call pm_runtime_put_autosuspend() when device is ready to accept jobs
- Move pm_runtime_set_suspended() before panfrost_device_fini() so that resource unwinding happens in the opposite order as initialisation.

Fixes 635430797d3f ("drm/panfrost: Rework runtime PM initialization")
Fixes 876b15d2c88d ("drm/panfrost: Fix module unload")
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -0
index 55fc22e8d4d4..a3eff77add55 100644
@@ -854,6 +854,7 @@ static int panfrost_probe(struct platform_device *pdev)
 
 	pm_runtime_set_active(pfdev->base.dev);
 	pm_runtime_mark_last_busy(pfdev->base.dev);
+	pm_runtime_get_noresume(pfdev->base.dev);
 	pm_runtime_enable(pfdev->base.dev);
 	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
 	pm_runtime_use_autosuspend(pfdev->base.dev);
@@ -866,13 +867,16 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
+	pm_runtime_put_autosuspend(pfdev->base.dev);

drivers/gpu/drm/panfrost/panfrost_drv.c static int panfrost_probe(struct platform_device *pdev) · line 879

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +3 -1
 
 	return 0;
 
 err_out1:
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);

Not an issue per-se, because put_noidle() is a NOP, but I think it'd be easier to reason about with this order:

	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
  	pm_runtime_disable(pfdev->base.dev);
	panfrost_device_fini(pfdev);
  	pm_runtime_set_suspended(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);

This makes it clear that panfrost_device_fini() assumes the device is resumed when it's called and suspended when it returns.

drivers/gpu/drm/panfrost/panfrost_drv.c static void panfrost_remove(struct platform_device *pdev) · line 893

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -0
 err_out0:
 	return err;
 }
@@ -884,9 +888,11 @@ static void panfrost_remove(struct platform_device *pdev)
 	drm_dev_unregister(&pfdev->base);
 
 	pm_runtime_get_sync(pfdev->base.dev);
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);

Let's keep the order consistent with the probe path:

	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
	pm_runtime_disable(pfdev->base.dev);
	panfrost_device_fini(pfdev);
	pm_runtime_set_suspended(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);

I also think this deserves comments to explain the noresume/noidle dance (device_init/fini take care of clks internally, and when they return the device is resumed/suspended, so all we have to is update the state, and acquire a ref).

Quoted 7 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +1 -1
-	panfrost_device_fini(pfdev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);
 }
 
 static ssize_t profiling_show(struct device *dev,
maintainer Reviewed-by on v9 8/16
#21 lore raw

Re: [PATCH v9 08/16] drm/panfrost: Move all DRM device initialisation into device_init()

In reply to Adrián Larumbe
Quoted 9 lines · On Sat, 12 Sep 2026 00:28:09 +0100 Adrián Larumbe…

Ideally the probe() function will do as little as possible, and all device initialisation and registration should happen inside the panfrost device subsystem, just like it's done in Panthor. This also simplifies resource unwinding in the error path.

Do the same thing for DRM driver remove, as in, sweep most of the action into panfrost_device_fini(), just like we did for device probe.

Quoted 149 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c | 37 +++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_drv.c    | 47 +-----------------------------
 2 files changed, 38 insertions(+), 46 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +37 -0
index 2fcbd223204c..ec2b1d29a26c 100644
@@ -8,6 +8,7 @@
 #include <linux/pm_domain.h>
 #include <linux/pm_runtime.h>
 #include <linux/regulator/consumer.h>
+#include <drm/drm_drv.h>
 
 #include "panfrost_device.h"
 #include "panfrost_devfreq.h"
@@ -230,6 +231,12 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 {
 	int err;
 
+	pfdev->comp = of_device_get_match_data(pfdev->base.dev);
+	if (!pfdev->comp)
+		return -ENODEV;
+
+	pfdev->coherent = device_get_dma_attr(pfdev->base.dev) == DEV_DMA_COHERENT;
+
 #ifdef CONFIG_DEBUG_FS
 	mutex_init(&pfdev->debugfs.gems_lock);
 	INIT_LIST_HEAD(&pfdev->debugfs.gems_list);
@@ -291,8 +298,31 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	if (err)
 		goto out_perfcnt;
 
+	pm_runtime_set_active(pfdev->base.dev);
+	pm_runtime_mark_last_busy(pfdev->base.dev);
+	pm_runtime_get_noresume(pfdev->base.dev);
+	pm_runtime_enable(pfdev->base.dev);
+	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
+	pm_runtime_use_autosuspend(pfdev->base.dev);
+
+	/*
+	 * Register the DRM device with the core and the connectors with
+	 * sysfs
+	 */
+	err = drm_dev_register(&pfdev->base, 0);
+	if (err < 0)
+		goto err_disable_rpm;
+
+	pm_runtime_put_autosuspend(pfdev->base.dev);
+
 	return 0;
 
+err_disable_rpm:
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_disable(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
+	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_gem_fini(pfdev);
 out_perfcnt:
 	panfrost_perfcnt_fini(pfdev);
 out_job:
@@ -316,6 +346,13 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 
 void panfrost_device_fini(struct panfrost_device *pfdev)
 {
+	pm_runtime_get_sync(pfdev->base.dev);
+
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_disable(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
+	pm_runtime_set_suspended(pfdev->base.dev);
+
 	panfrost_gem_fini(pfdev);
 	panfrost_perfcnt_fini(pfdev);
 	panfrost_jm_fini(pfdev);
drivers/gpu/drm/panfrost/panfrost_drv.c +1 -46
index a3eff77add55..f77780c72a1a 100644
@@ -830,7 +830,6 @@ static const struct drm_driver panfrost_drm_driver = {
 static int panfrost_probe(struct platform_device *pdev)
 {
 	struct panfrost_device *pfdev;
-	int err;
 
 	pfdev = devm_drm_dev_alloc(&pdev->dev, &panfrost_drm_driver,
 				   struct panfrost_device, base);
@@ -839,46 +838,7 @@ static int panfrost_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, pfdev);
 
-	pfdev->comp = of_device_get_match_data(&pdev->dev);
-	if (!pfdev->comp)
-		return -ENODEV;
-
-	pfdev->coherent = device_get_dma_attr(&pdev->dev) == DEV_DMA_COHERENT;
-
-	err = panfrost_device_init(pfdev);
-	if (err) {
-		if (err != -EPROBE_DEFER)
-			dev_err(&pdev->dev, "Fatal error during GPU init\n");
-		goto err_out0;
-	}
-
-	pm_runtime_set_active(pfdev->base.dev);
-	pm_runtime_mark_last_busy(pfdev->base.dev);
-	pm_runtime_get_noresume(pfdev->base.dev);
-	pm_runtime_enable(pfdev->base.dev);
-	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
-	pm_runtime_use_autosuspend(pfdev->base.dev);
-
-	/*
-	 * Register the DRM device with the core and the connectors with
-	 * sysfs
-	 */
-	err = drm_dev_register(&pfdev->base, 0);
-	if (err < 0)
-		goto err_out1;
-
-	pm_runtime_put_autosuspend(pfdev->base.dev);
-
-	return 0;
-
-err_out1:
-	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
-	pm_runtime_disable(pfdev->base.dev);
-	pm_runtime_put_noidle(pfdev->base.dev);
-	pm_runtime_set_suspended(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
-err_out0:
-	return err;
+	return panfrost_device_init(pfdev);
 }
 
 static void panfrost_remove(struct platform_device *pdev)
@@ -887,11 +847,6 @@ static void panfrost_remove(struct platform_device *pdev)
 
 	drm_dev_unregister(&pfdev->base);
 
-	pm_runtime_get_sync(pfdev->base.dev);
-	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
-	pm_runtime_put_noidle(pfdev->base.dev);
-	pm_runtime_disable(pfdev->base.dev);
-	pm_runtime_set_suspended(pfdev->base.dev);
 	panfrost_device_fini(pfdev);
 }
 
maintainer Reviewed-by on v9 10/16
#24 lore raw

Re: [PATCH v9 10/16] drm/panfrost: Add debugfs knob for manually triggering a GPU reset

In reply to Adrián Larumbe
Quoted 4 lines · On Sat, 12 Sep 2026 00:28:11 +0100 Adrián Larumbe…

This will be of great help when testing potential races between the GPU reset sequence and other parts of the code accessing HW registers.

One comment below.

Quoted 27 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c | 37 ++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpu/drm/panfrost/panfrost_device.c +8 -0
index d75566a45392..0aee1d87a7ab 100644
@@ -2,6 +2,7 @@
 /* Copyright 2018 Marty E. Plummer <[email protected]> */
 /* Copyright 2019 Linaro, Ltd, Rob Herring <[email protected]> */
 
+#include <linux/debugfs.h>
 #include <linux/clk.h>
 #include <linux/reset.h>
 #include <linux/platform_device.h>
@@ -583,9 +584,45 @@ EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = {
 };
 
 #ifdef CONFIG_DEBUG_FS
+static int reset_get(void *data, u64 *val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
+
+	*val = atomic_read(&pfdev->reset.pending);
+	return 0;

drivers/gpu/drm/panfrost/panfrost_device.c EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = { · line 603

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_device.c +10 -0
+}
+
+static int reset_set(void *data, u64 val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
+	int ret = pm_runtime_get_if_active(pfdev->base.dev);
+
+	if (ret) {
+		panfrost_device_schedule_reset(pfdev);

It feels weird to schedule a reset if the device is suspended, because a suspend/resume cycle already acts as a reset. Any particular reason you'd want to do that? If not, I'd suggest going for:

	if (ret <= 0)
		return ret;

	panfrost_device_schedule_reset(pfdev);
	pm_runtime_put(pfdev->base.dev);
	return 0;
Quoted 26 lines
drivers/gpu/drm/panfrost/panfrost_device.c +19 -0
+		flush_work(&pfdev->reset.work);
+		if (ret > 0)
+			pm_runtime_put(pfdev->base.dev);
+	}
+
+	return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(panfrost_reset_debugfs_fops,
+			 reset_get, reset_set,
+			 "0x%08llx\n");
+
+static void panfrost_reset_debugfs_init(struct drm_minor *minor)
+{
+	debugfs_create_file("reset", 0600, minor->debugfs_root,
+			    minor->dev, &panfrost_reset_debugfs_fops);
+}
+
 void panfrost_device_debugfs_init(struct drm_minor *minor)
 {
 	panfrost_gem_debugfs_init(minor);
 	panfrost_sched_debugfs_init(minor);
+	panfrost_reset_debugfs_init(minor);
 }
 #endif // CONFIG_DEBUG_FS
maintainer Reviewed-by on v9 12/16
#31 lore raw

Re: [PATCH v9 12/16] drm/panfrost: Skip cache flush/invalidate when enabling perfcnt

In reply to Adrián Larumbe
Quoted 6 lines · On Sat, 12 Sep 2026 00:28:13 +0100 Adrián Larumbe…

The GPU cache flush/invalidate operation is unnecessary. First off, the GPU doesn't read off the perfcnt sample buffer, only writes into it, so an invalidate doesn't make a difference. Then flushing GPU caches after each sample has been written is enough for the CPU to see updated values.

Quoted 33 lines
---
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +2 -13
index f71534e741b6..ffc77121070e 100644
@@ -124,21 +124,10 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 	panfrost_gem_internal_set_label(&bo->base, "Perfcnt sample buffer");
 
 	/*
-	 * Invalidate the cache and clear the counters to start from a fresh
-	 * state.
+	 * Clear the counters to start from a fresh state.
 	 */
-	reinit_completion(&pfdev->perfcnt->dump_comp);
-	gpu_write(pfdev, GPU_INT_CLEAR,
-		  GPU_IRQ_CLEAN_CACHES_COMPLETED |
-		  GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
+	gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
 	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_CLEAR);
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_CLEAN_INV_CACHES);
-	ret = wait_for_completion_timeout(&pfdev->perfcnt->dump_comp,
-					  msecs_to_jiffies(1000));
-	if (!ret) {
-		ret = -ETIMEDOUT;
-		goto err_vunmap;
-	}
 
 	ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu);
 	if (ret < 0)
maintainer
#38 lore raw

Re: [PATCH v9 15/16] drm/panfrost: Fix races between perfcnt and reset sequence

In reply to Adrián Larumbe
Quoted 10 lines · On Sat, 12 Sep 2026 00:28:16 +0100 Adrián Larumbe…

Formerly, the reset sequence would race with panfrost_mmu_as_put() when tearing down a perfcnt session. On top of that, poking GPU registers to program a perfcnt session or obtaining a dump might lead to undefined behaviour when done at the same time a reset was ongoing.

Use the reset r/w semaphore to govern access to the hardware at reset time. On top of that, expand the DRM uAPI for the perfcnt DUMP operation so that userspace can be made aware of a reset having happened, because that means counters will go back to 0 and can no longer be accumulated to values previously kept in user space.

In GPU_PERFCNT_CFG_MODE_MANUAL mode (which is the one we use), internal counters are always cleared after each DUMP request. So, it's not so much that counters can't be accumulated after a RESET, it's more that we've lost data in the process, making this very sample inaccurate (counters lower than they should be).

Quoted 42 lines

The new perfcnt-aware reset sequence also takes care to reestablish perfcnt to its original configuration if there was an enabled session, or else flags the current session as dead if that failed.

Fixes 73e467f60acd ("drm/panfrost: Consolidate reset handling")
Fixes 7786fd108777 ("drm/panfrost: Expose performance counters through unstable ioctls")
---
 drivers/gpu/drm/panfrost/panfrost_device.c  |   2 +
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 201 +++++++++++++++++++---------
 drivers/gpu/drm/panfrost/panfrost_perfcnt.h |   1 +
 include/uapi/drm/panfrost_drm.h             |   8 +-
 4 files changed, 151 insertions(+), 61 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +2 -0
index 6c65feae63aa..e774f61c642b 100644
@@ -479,6 +479,8 @@ void panfrost_device_reset(struct panfrost_device *pfdev, bool enable_job_int)
 	panfrost_jm_reset_interrupts(pfdev);
 	if (enable_job_int)
 		panfrost_jm_enable_interrupts(pfdev);
+
+	panfrost_perfcnt_reset(pfdev);
 }
 
 static int panfrost_device_runtime_resume(struct device *dev)
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +1 -0
index b3f71d7fd82a..9847657179a5 100644
@@ -11,6 +11,7 @@
 #include <drm/drm_file.h>
 #include <drm/drm_gem_shmem_helper.h>
 #include <drm/panfrost_drm.h>
+#include <drm/drm_print.h>
 
 #include "panfrost_device.h"
 #include "panfrost_features.h"
@@ -28,11 +29,15 @@
 

drivers/gpu/drm/panfrost/panfrost_perfcnt.c line 39

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +3 -0
 struct panfrost_perfcnt {
 	struct panfrost_gem_mapping *mapping;
+	unsigned int counterset;
 	size_t bosize;
 	void *buf;
 	struct panfrost_file_priv *user;
 	struct mutex lock;
 	struct completion dump_comp;
+	bool reset_happened;
+	bool dump_finished;

Why not store the state flags directly instead of these dump_finished/reset_happened booleans?

Quoted 85 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +54 -12
+	bool owns_as_ref;
 };
 
 static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev)
@@ -47,36 +52,113 @@ static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev)
 
 void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev)
 {
+	pfdev->perfcnt->dump_finished = true;
 	complete(&pfdev->perfcnt->dump_comp);
 }
 
 void panfrost_perfcnt_sample_done(struct panfrost_device *pfdev)
 {
-	if (pfdev->features.selected_coherency != COHERENCY_ACE)
+	if (pfdev->features.selected_coherency != COHERENCY_ACE) {
 		gpu_write(pfdev, GPU_CMD, GPU_CMD_CLEAN_CACHES);
-	else
+	} else {
+		pfdev->perfcnt->dump_finished = true;
 		complete(&pfdev->perfcnt->dump_comp);
+	}
+}
+
+static int panfrost_perfcnt_hw_enable(struct panfrost_device *pfdev)
+{
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	u32 cfg, as;
+	int ret;
+
+	ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu);
+	if (ret < 0)
+		return ret;
+
+	as = ret;
+	cfg = GPU_PERFCNT_CFG_AS(as) |
+	      GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_MANUAL);
+
+	/*
+	 * Bifrost GPUs have 2 set of counters, but we're only interested by
+	 * the first one for now.
+	 */
+	if (panfrost_model_is_bifrost(pfdev))
+		cfg |= GPU_PERFCNT_CFG_SETSEL(perfcnt->counterset);
+
+	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0xffffffff);
+	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0xffffffff);
+	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0xffffffff);
+
+	/*
+	 * Due to PRLAM-8186 we need to disable the Tiler before we enable HW
+	 * counters.
+	 */
+	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
+		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
+	else
+		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+
+	gpu_write(pfdev, GPU_PERFCNT_CFG, cfg);
+
+	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
+		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+
+	return 0;
 }
 
-static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
+static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev, u32 *state)
 {
-	u64 gpuva;
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	u64 gpuva = perfcnt->mapping->mmnode.start << PAGE_SHIFT;
 	int ret;
 
-	reinit_completion(&pfdev->perfcnt->dump_comp);
-	gpuva = pfdev->perfcnt->mapping->mmnode.start << PAGE_SHIFT;
-	gpu_write(pfdev, GPU_PERFCNT_BASE_LO, lower_32_bits(gpuva));
-	gpu_write(pfdev, GPU_PERFCNT_BASE_HI, upper_32_bits(gpuva));
-	gpu_write(pfdev, GPU_INT_CLEAR,
-		  GPU_IRQ_CLEAN_CACHES_COMPLETED |
-		  GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_SAMPLE);
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		perfcnt->dump_finished = false;
+		*state = 0;

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) · line 129

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+
+		if (!perfcnt->owns_as_ref) {
+			*state = PANFROST_PERFCNT_SESSION_DEAD;
+			return -EIO;
+		}
+
+		if (perfcnt->reset_happened) {
+			*state = PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
+			perfcnt->reset_happened = false;
+		}
		*state = perfcnt->state;
		if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
			return -EIO;

		perfcnt->state = 0;
Quoted 26 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +20 -4
+
+		reinit_completion(&pfdev->perfcnt->dump_comp);
+
+		gpu_write(pfdev, GPU_PERFCNT_BASE_LO, lower_32_bits(gpuva));
+		gpu_write(pfdev, GPU_PERFCNT_BASE_HI, upper_32_bits(gpuva));
+		gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_CLEAN_CACHES_COMPLETED |
+						GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
+		gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_SAMPLE);
+	}
+
+	/*
+	 * Here we release the reset semaphore because perfcnt should not get in the way
+	 * of a HW reset. Besides, a legitimate reset might be issued during the wait.
+	 */
 	ret = wait_for_completion_interruptible_timeout(&pfdev->perfcnt->dump_comp,
 							msecs_to_jiffies(1000));
-	if (!ret)
-		ret = -ETIMEDOUT;
-	else if (ret > 0)
-		ret = 0;
+
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		/* Either sample finished or reset happened */
+		if (ret > 0) {
+			ret = perfcnt->dump_finished ? 0 :
+			      perfcnt->owns_as_ref ? -EAGAIN : -EIO;

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) · line 161

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+
+		} else if (!ret) {
+			ret = -ETIMEDOUT;
+		}
+
+		if (perfcnt->reset_happened)
+			*state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
+		if (!perfcnt->owns_as_ref)
+			*state |= PANFROST_PERFCNT_SESSION_DEAD;
+	}
	if (!ret)
		return -ETIMEDOUT;

	scoped_guard(rwsem_read, &pfdev->reset.lock) {
		u32 new_state = perfcnt->state;

		*state |= new_state;
		if (new_state & PANFROST_PERFCNT_SESSION_DEAD)
			return -EIO;

		perfcnt->state = 0;

		/* If we faced a reset during our SAMPLE, the user needs to try again. */
		if (perfcnt->state & PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET)
			return -EAGAIN;
	}

	return 0;
Quoted 63 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +9 -35
 	return ret;
 }
@@ -87,9 +169,8 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 {
 	struct panfrost_file_priv *user = file_priv->driver_priv;
 	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
-	struct iosys_map map;
 	struct drm_gem_shmem_object *bo;
-	u32 cfg, as;
+	struct iosys_map map;
 	int ret;
 
 	if (user == perfcnt->user)
@@ -122,54 +203,31 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 	ret = drm_gem_vmap(&bo->base, &map);
 	if (ret)
 		goto err_put_mapping;
+
 	perfcnt->buf = map.vaddr;
+	perfcnt->counterset = counterset;
 
 	panfrost_gem_internal_set_label(&bo->base, "Perfcnt sample buffer");
 
-	/*
-	 * Clear the counters to start from a fresh state.
-	 */
-	gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_CLEAR);
-
-	ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu);
-	if (ret < 0)
-		goto err_vunmap;
-
-	as = ret;
-	cfg = GPU_PERFCNT_CFG_AS(as) |
-	      GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_MANUAL);
-
-	/*
-	 * Bifrost GPUs have 2 set of counters, but we're only interested by
-	 * the first one for now.
-	 */
-	if (panfrost_model_is_bifrost(pfdev))
-		cfg |= GPU_PERFCNT_CFG_SETSEL(counterset);
-
-	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0xffffffff);
-	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0xffffffff);
-	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0xffffffff);
-
-	/*
-	 * Due to PRLAM-8186 we need to disable the Tiler before we enable HW
-	 * counters.
-	 */
-	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
-	else
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		/*
+		 * Clear the counters to start from a fresh state.
+		 */
+		gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
+		gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_CLEAR);

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev, · line 224

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +5 -3
 
-	gpu_write(pfdev, GPU_PERFCNT_CFG, cfg);
+		ret = panfrost_perfcnt_hw_enable(pfdev);
+		if (ret)
+			goto err_vunmap;
 
-	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+		perfcnt->reset_happened = false;
+		perfcnt->owns_as_ref = true;

This should probably be set in panfrost_perfcnt_hw_enable(), just after the panfrost_mmu_as_get() call.

Quoted 12 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +2 -2
+		perfcnt->user = user;
+	}
 
 	/* The BO ref is retained by the mapping. */
 	drm_gem_object_put(&bo->base);
 
-	perfcnt->user = user;
-
 	return 0;
 
 err_vunmap:

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev, · line 259

Quoted 8 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +4 -1
@@ -195,13 +253,16 @@ static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev,
 	if (user != perfcnt->user)
 		return -EINVAL;
 
-	panfrost_perfcnt_hw_disable(pfdev);
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		panfrost_perfcnt_hw_disable(pfdev);
+		if (perfcnt->owns_as_ref)
+			panfrost_mmu_as_put(pfdev, perfcnt->mapping->mmu);

Similarly, I think it'd be preferable to have this as_put() inside perfcnt_hw_disable().

Quoted 39 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +12 -3
+		perfcnt->user = NULL;
+	}
 
-	perfcnt->user = NULL;
 	drm_gem_vunmap(&perfcnt->mapping->obj->base.base, &map);
 	perfcnt->buf = NULL;
 	panfrost_gem_close(&perfcnt->mapping->obj->base.base, file_priv);
-	panfrost_mmu_as_put(pfdev, perfcnt->mapping->mmu);
 	panfrost_gem_mapping_put(perfcnt->mapping);
 	perfcnt->mapping = NULL;
 	pm_runtime_put_autosuspend(pfdev->base.dev);
@@ -249,13 +310,16 @@ int panfrost_ioctl_perfcnt_dump(struct drm_device *dev, void *data,
 	if (ret)
 		return ret;
 
+	if (req->pad)
+		return -EINVAL;
+
 	mutex_lock(&perfcnt->lock);
 	if (perfcnt->user != file_priv->driver_priv) {
 		ret = -EINVAL;
 		goto out;
 	}
 
-	ret = panfrost_perfcnt_dump_locked(pfdev);
+	ret = panfrost_perfcnt_dump_locked(pfdev, &req->state);
 	if (ret)
 		goto out;
 
@@ -338,3 +402,20 @@ void panfrost_perfcnt_fini(struct panfrost_device *pfdev)
 	/* Disable everything before leaving. */
 	panfrost_perfcnt_hw_disable(pfdev);
 }
+
+void panfrost_perfcnt_reset(struct panfrost_device *pfdev)
+{
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+
+	if (drm_WARN_ON(&pfdev->base, !perfcnt))

drivers/gpu/drm/panfrost/panfrost_perfcnt.c void panfrost_perfcnt_fini(struct panfrost_device *pfdev) · line 420

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+		return;
+
+	lockdep_assert_held(&pfdev->reset.lock);
+
+	if (!perfcnt->user)
+		return;
+
+	perfcnt->owns_as_ref = !panfrost_perfcnt_hw_enable(pfdev);
+	perfcnt->reset_happened = true;
+	complete(&perfcnt->dump_comp);
	/* All active AS are released during the MMU post_reset. */
	perfcnt->owns_as_ref = false;
	perfcnt->state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
	if (panfrost_perfcnt_hw_enable(pfdev))
		perfcnt->state |= PANFROST_PERFCNT_SESSION_DEAD;

	/* Unblock pending sample requests. */
	complete(&perfcnt->dump_comp);
Quoted 41 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +1 -0
+}
drivers/gpu/drm/panfrost/panfrost_perfcnt.h +1 -0
index 8bbcf5f5fb33..8b9bc704b634 100644
@@ -14,5 +14,6 @@ int panfrost_ioctl_perfcnt_enable(struct drm_device *dev, void *data,
 				  struct drm_file *file_priv);
 int panfrost_ioctl_perfcnt_dump(struct drm_device *dev, void *data,
 				struct drm_file *file_priv);
+void panfrost_perfcnt_reset(struct panfrost_device *pfdev);
 
 #endif
include/uapi/drm/panfrost_drm.h +7 -1
index 50d5337f35ef..97e001040543 100644
@@ -47,7 +47,7 @@ extern "C" {
  * them for anything but debugging purpose.
  */
 #define DRM_IOCTL_PANFROST_PERFCNT_ENABLE	DRM_IOW(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_ENABLE, struct drm_panfrost_perfcnt_enable)
-#define DRM_IOCTL_PANFROST_PERFCNT_DUMP		DRM_IOW(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_DUMP, struct drm_panfrost_perfcnt_dump)
+#define DRM_IOCTL_PANFROST_PERFCNT_DUMP		DRM_IOWR(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_DUMP, struct drm_panfrost_perfcnt_dump)
 
 #define PANFROST_JD_REQ_FS (1 << 0)
 #define PANFROST_JD_REQ_CYCLE_COUNT (1 << 1)
@@ -270,8 +270,14 @@ struct drm_panfrost_perfcnt_enable {
 	__u32 counterset;
 };
 
+/* Perfcnt dump state as influenced by a HW reset */
+#define PANFROST_PERFCNT_SESSION_DEAD                 (1 << 0)
+#define PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET (1 << 1)
+
 struct drm_panfrost_perfcnt_dump {
 	__u64 buf_ptr;
+	__u32 state;
+	__u32 pad;		/* MBZ */
 };
 
 /* madvise provides a way to tell the kernel in case a buffers contents
maintainer
#43 lore raw

Re: [PATCH v9 16/16] drm/panfrost: Bump driver minor to reflect new DUMP IOCTL req field

In reply to Adrián Larumbe
Quoted 1 line · On Sat, 12 Sep 2026 00:28:17 +0100 Adrián Larumbe…

Expose expanded PERFCNT_DUMP IOCTL interface to user mode.

I don't think it makes sense to have the version bump in a separate commit when the changes adding support for a uAPI extension are all in a single commit, so I'd squash this commit into patch 15.

Quoted 28 lines
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -1
index 571a26b84126..de9b1c115181 100644
@@ -808,6 +808,7 @@ static const struct file_operations panfrost_drm_driver_fops = {
  * - 1.6 - adds PANFROST_BO_MAP_WB, PANFROST_IOCTL_SYNC_BO,
  *	   PANFROST_IOCTL_QUERY_BO_INFO and
  *	   DRM_PANFROST_PARAM_SELECTED_COHERENCY
+ * - 1.7 - adds PERFCNT_DUMP req state field
  */
 static const struct drm_driver panfrost_drm_driver = {
 	.driver_features	= DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ,
@@ -820,7 +821,7 @@ static const struct drm_driver panfrost_drm_driver = {
 	.name			= "panfrost",
 	.desc			= "panfrost DRM",
 	.major			= 1,
-	.minor			= 6,
+	.minor			= 7,
 
 	.gem_create_object	= panfrost_gem_create_object,
 	.gem_prime_import	= panfrost_gem_prime_import,
author maintainer
#3 lore raw

Re: [PATCH v9 01/16] drm/panfrost: Move shrinker initialization and unplug one level down

In reply to Boris Brezillon
Quoted 34 lines · Boris Brezillon
Quoted 10 lines · On Sat, 12 Sep 2026 00:28:02 +0100 Adrián Larumbe…

Since the moment we call drm_dev_register() the device should be in a position to accept jobs, so it's best if the shrinker is already initialized by then.

On top of that, make shrinker functions take an panfrost_device pointer like other functions in the same sequence and rename them accordingly.

Essentially mimic the init/fini behaviour in Panthor.

One remark below.

Quoted 16 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c       |  8 +++++++-
 drivers/gpu/drm/panfrost/panfrost_drv.c          |  6 ------
 drivers/gpu/drm/panfrost/panfrost_gem.c          | 26 ++++++++++++++----------
 drivers/gpu/drm/panfrost/panfrost_gem.h          |  7 ++++---
 drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c |  8 ++------
 5 files changed, 28 insertions(+), 27 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +0 -0
index 485349faf251..05c40d5a20b5 100644
@@ -280,9 +280,14 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	if (err)
 		goto out_job;
 
Quoted 9 lines

drivers/gpu/drm/panfrost/panfrost_device.c int panfrost_device_init(struct panfrost_device *pfdev) · line 283

Quoted 2 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
-	panfrost_gem_init(pfdev);
+	err = panfrost_gem_init(pfdev);

It feels weird to have the GEM subsystem initialized last when you consider the fact other subsystems might want to allocate GEMs in their _init() function. I know it's where the panfrost_gem_init() is right now, and that ultimately it doesn't prevent anyone from allocating GEMs, but I think it would make sense have this called before any of the other subsystem init functions, still.

I think we discussed having panfrost_gem_init() be called before the other subsystem init functions, but then I checked panthor and saw it's also being called right after all the others there too. I think you're right that it's best to initialise gem first, because I wonder whether other subsystem init functions creating GEMs when the mount point for huge page-backed objects hasn't been created yet could lead to some sort of trouble.

However, given how much this patch series has already grown, I believe it'd be best to leave this change for a later one.

Quoted 131 lines
Quoted 131 lines
drivers/gpu/drm/panfrost/panfrost_device.c +6 -0
+	if (err)
+		goto out_perfcnt;
 
 	return 0;
+
+out_perfcnt:
+	panfrost_perfcnt_fini(pfdev);
 out_job:
 	panfrost_jm_fini(pfdev);
 out_mmu:
@@ -304,6 +309,7 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 
 void panfrost_device_fini(struct panfrost_device *pfdev)
 {
+	panfrost_gem_fini(pfdev);
 	panfrost_perfcnt_fini(pfdev);
 	panfrost_jm_fini(pfdev);
 	panfrost_mmu_fini(pfdev);
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -6
index 96a66d673ba6..9882a3ede75f 100644
@@ -1003,14 +1003,9 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
-	err = panfrost_gem_shrinker_init(&pfdev->base);
-	if (err)
-		goto err_out2;
 
 	return 0;
 
-err_out2:
-	drm_dev_unregister(&pfdev->base);
 err_out1:
 	pm_runtime_disable(pfdev->base.dev);
 	panfrost_device_fini(pfdev);
@@ -1024,7 +1019,6 @@ static void panfrost_remove(struct platform_device *pdev)
 	struct panfrost_device *pfdev = platform_get_drvdata(pdev);
 
 	drm_dev_unregister(&pfdev->base);
-	panfrost_gem_shrinker_cleanup(&pfdev->base);
 
 	pm_runtime_get_sync(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
drivers/gpu/drm/panfrost/panfrost_gem.c +15 -11
index 3a7fce428898..f39c0f50ed4f 100644
@@ -15,20 +15,24 @@
 #include "panfrost_gem.h"
 #include "panfrost_mmu.h"
 
-void panfrost_gem_init(struct panfrost_device *pfdev)
+int panfrost_gem_init(struct panfrost_device *pfdev)
 {
-	int err;
+	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && panfrost_transparent_hugepage) {
+		int err = drm_gem_huge_mnt_create(&pfdev->base, "within_size");
+
+		if (drm_gem_get_huge_mnt(&pfdev->base))
+			drm_info(&pfdev->base, "Using Transparent Hugepage\n");
+		else if (err)
+			drm_warn(&pfdev->base, "Can't use Transparent Hugepage (%d)\n",
+				 err);
+	}
 
-	if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
-	    !panfrost_transparent_hugepage)
-		return;
+	return panfrost_gem_shrinker_init(pfdev);
+}
 
-	err = drm_gem_huge_mnt_create(&pfdev->base, "within_size");
-	if (drm_gem_get_huge_mnt(&pfdev->base))
-		drm_info(&pfdev->base, "Using Transparent Hugepage\n");
-	else if (err)
-		drm_warn(&pfdev->base, "Can't use Transparent Hugepage (%d)\n",
-			 err);
+void panfrost_gem_fini(struct panfrost_device *pfdev)
+{
+	panfrost_gem_shrinker_fini(pfdev);
 }
 
 #ifdef CONFIG_DEBUG_FS
drivers/gpu/drm/panfrost/panfrost_gem.h +4 -3
index 79d4377019e9..5c823cdbd980 100644
@@ -129,7 +129,8 @@ drm_mm_node_to_panfrost_mapping(struct drm_mm_node *node)
 	return container_of(node, struct panfrost_gem_mapping, mmnode);
 }
 
-void panfrost_gem_init(struct panfrost_device *pfdev);
+int panfrost_gem_init(struct panfrost_device *pfdev);
+void panfrost_gem_fini(struct panfrost_device *pfdev);
 
 struct drm_gem_object *panfrost_gem_create_object(struct drm_device *dev, size_t size);
 
@@ -154,8 +155,8 @@ panfrost_gem_mapping_get(struct panfrost_gem_object *bo,
 void panfrost_gem_mapping_put(struct panfrost_gem_mapping *mapping);
 void panfrost_gem_teardown_mappings_locked(struct panfrost_gem_object *bo);
 
-int panfrost_gem_shrinker_init(struct drm_device *dev);
-void panfrost_gem_shrinker_cleanup(struct drm_device *dev);
+int panfrost_gem_shrinker_init(struct panfrost_device *pfdev);
+void panfrost_gem_shrinker_fini(struct panfrost_device *pfdev);
 
 void panfrost_gem_set_label(struct drm_gem_object *obj, const char *label);
 int panfrost_gem_sync(struct drm_gem_object *obj, u32 type,
drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c +1 -4
index 2fe967a90bcb..fefae87535d6 100644
@@ -95,10 +95,8 @@ panfrost_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  *
  * This function registers and sets up the panfrost shrinker.
  */
-int panfrost_gem_shrinker_init(struct drm_device *dev)
+int panfrost_gem_shrinker_init(struct panfrost_device *pfdev)
 {
-	struct panfrost_device *pfdev = to_panfrost_device(dev);
-
 	pfdev->shrinker = shrinker_alloc(0, "drm-panfrost");
 	if (!pfdev->shrinker)
 		return -ENOMEM;
@@ -118,10 +116,8 @@ int panfrost_gem_shrinker_init(struct drm_device *dev)
  *
  * This function unregisters the panfrost shrinker.
  */
-void panfrost_gem_shrinker_cleanup(struct drm_device *dev)
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c int panfrost_gem_shrinker_init(struct drm_device *dev) · line 123

Quoted 8 lines
drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c +1 -2
+void panfrost_gem_shrinker_fini(struct panfrost_device *pfdev)
 {
-	struct panfrost_device *pfdev = to_panfrost_device(dev);
-
 	if (pfdev->shrinker)
 		shrinker_free(pfdev->shrinker);
 }

Adrian Larumbe

author maintainer
#11 lore raw

Re: [PATCH v9 05/16] drm/panfrost: Consolidate device clock management and reset

In reply to Boris Brezillon
Quoted 29 lines · Boris Brezillon
Quoted 9 lines · On Sat, 12 Sep 2026 00:28:06 +0100 Adrián Larumbe…

Gather all clock enables and disables into a single function to avoid repetitions between driver init/fini and device resume/suspend, since these clocks are always handled in bulk.

Also do clk (un)prepares and dis/enables at the same time, since the clk_prepare_* family of functions can simply increase the refcnt of an already prepared clock.

Two nits below.

Quoted 12 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c | 118 +++++++++++++----------------
 1 file changed, 52 insertions(+), 66 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +0 -0
index b3a53504bd01..2fcbd223204c 100644
@@ -34,10 +34,46 @@ static void panfrost_reset_fini(struct panfrost_device *pfdev)
 	reset_control_assert(pfdev->rstc);
 }
 
Quoted 7 lines

drivers/gpu/drm/panfrost/panfrost_device.c static void panfrost_reset_fini(struct panfrost_device *pfdev) · line 37

Quoted 2 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
-static int panfrost_clk_init(struct panfrost_device *pfdev)
+static int panfrost_clk_enable(struct panfrost_device *pfdev, bool enable_ace_clock)

I don't want to have a flag added for each new clock we decide to keep enabled on RPM suspend, so how about s/enable_ace_clock/on_resume/? Also, since it's enabling more than one clk, I'd go s/panfrost_clk_enable/panfrost_clks_enable/.

I agree with you. I was thinking if new clocks also had different needs in the future, maybe we could pass an array of option clocks but at this point it's pure make-believe.

Quoted 23 lines
Quoted 23 lines
drivers/gpu/drm/panfrost/panfrost_device.c +21 -0
 {
 	int err;
+
+	err = clk_prepare_enable(pfdev->clock);
+	if (err)
+		return err;
+
+	err = clk_prepare_enable(pfdev->bus_clock);
+	if (err)
+		goto disable_clock;
+
+	if (enable_ace_clock) {
+		err = clk_prepare_enable(pfdev->bus_ace_clock);
+		if (err)
+			goto disable_bus_clock;
+	}
+
+	return 0;
+
+disable_bus_clock:
+	clk_disable_unprepare(pfdev->bus_clock);
+disable_clock:
+	clk_disable_unprepare(pfdev->clock);
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_device.c static void panfrost_reset_fini(struct panfrost_device *pfdev) · line 65

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_device.c +5 -0
+
+	return err;
+}
+
+static void panfrost_clk_disable(struct panfrost_device *pfdev, bool disable_ace_clock)

Similarly, I'd go s/disable_ace_clock/on_suspend/ and s/panfrost_clk_disable/panfrost_clks_disable/.

Noted.

Adrian Larumbe

author maintainer
#15 lore raw

Re: [PATCH v9 06/16] drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove

In reply to Boris Brezillon
Quoted 67 lines · Boris Brezillon
Quoted 50 lines · On Sat, 12 Sep 2026 00:28:07 +0100 Adrián Larumbe…

During device probe(), failure to do a PM get() will leave the usage_count set to 0, which is the value assigned at device creation time. That means when the autosuspend delay expires, runtime suspend callback won't be invoked, so the device will remain powered on forever.

On top of that, failure to call PM put() during device unplug means Panfrost device's PM usage_count increases monotonically for every new module reload.

The outcome of both of the above meant that:

- Devfreq OPP transition notifications would be printed all the time, even when no jobs are being submitted. This quickly fills the kernel ring buffer with junk.
- Because MMU interrupts are only enabled when the device is reset, the very first job targeting the tiler heap BO after device probe() would always time out, since the driver's PM runtime resume callback would not be invoked.

To fix the above:
- Manually adjust the PM refcnt at device probe and removal time.
- Ensure pm_runtime_dont_use_autosuspend is called in the wind-down path.
- Call pm_runtime_put_autosuspend() when device is ready to accept jobs
- Move pm_runtime_set_suspended() before panfrost_device_fini() so that resource unwinding happens in the opposite order as initialisation.

Fixes 635430797d3f ("drm/panfrost: Rework runtime PM initialization")
Fixes 876b15d2c88d ("drm/panfrost: Fix module unload")
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -0
index 55fc22e8d4d4..a3eff77add55 100644
@@ -854,6 +854,7 @@ static int panfrost_probe(struct platform_device *pdev)
 
 	pm_runtime_set_active(pfdev->base.dev);
 	pm_runtime_mark_last_busy(pfdev->base.dev);
+	pm_runtime_get_noresume(pfdev->base.dev);
 	pm_runtime_enable(pfdev->base.dev);
 	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
 	pm_runtime_use_autosuspend(pfdev->base.dev);
@@ -866,13 +867,16 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
+	pm_runtime_put_autosuspend(pfdev->base.dev);

drivers/gpu/drm/panfrost/panfrost_drv.c static int panfrost_probe(struct platform_device *pdev) · line 879

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +3 -1
 
 	return 0;
 
 err_out1:
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);

Not an issue per-se, because put_noidle() is a NOP, but I think it'd be easier to reason about with this order:

Quoted 8 lines
	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
  	pm_runtime_disable(pfdev->base.dev);
	panfrost_device_fini(pfdev);
  	pm_runtime_set_suspended(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);

This makes it clear that panfrost_device_fini() assumes the device is resumed when it's called and suspended when it returns.

Your suggestion feels more natural, which makes me wonder why I decided to move panfrost_device_fini() to the end of the block.

IIRC we chatted about where to place pm_runtime_put_noidle() and pm_runtime_set_suspended() in panfrost_device_init() when one of the subsystem initialisations failed. My intuition had been to leave them until the very bottom of it, in imitation of what you hinted here, but then it'd go against the notion that winding down must be done in the opposite order as initialisation. And so because panfrost_device_init() happens before all the pm_runtime_* incantations, I thought panfrost_device_fini() should be placed at the end in the error and driver remove paths.

That said, I think a more intuitive init order would be one that imitates what happens in Panthor when runtime PM is enabled:

	/* First thing done in pm_runtime_resume_and_get -> pm_runtime_get_active -> __pm_runtime_resume */
	pm_runtime_get_noresume(pfdev->base.dev);
	
	/* Now come subsystem initialisations */
	panfrost_gpu_init(pfdev);
	panfrost_mmu_init(pfdev);
	panfrost_jm_init(pfdev);
	panfrost_perfcnt_init(pfdev);
	panfrost_gem_init(pfdev);
	
	/* __pm_runtime_resume -> rpm_resume first invokes the resume callback and then
	 * updates runtime status to RPM_ACTIVE. */
	pm_runtime_set_active(pfdev->base.dev);
	
	pm_runtime_enable(ptdev->base.dev);
        pm_runtime_mark_last_busy(pfdev->base.dev);
	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
	pm_runtime_use_autosuspend(pfdev->base.dev);

And then in the error and device remove path would be almost just like you suggest.

        pm_runtime_dont_use_autosuspend(pfdev->base.dev);
	pm_runtime_disable(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);
	panfrost_device_fini(pfdev);
	pm_runtime_set_suspended(pfdev->base.dev);

The only difference is that pm_runtime_put_noidle() happens before device_fini because that's the same order as in __pm_runtime_suspend().

However, that means in the commit where I move all DRM initialisation into panfrost_device_init(), in order to keep things consistent, I should move pm_runtime_set_suspended() to the very end of the error path.

Quoted 19 lines

drivers/gpu/drm/panfrost/panfrost_drv.c static void panfrost_remove(struct platform_device *pdev) · line 893

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -0
 err_out0:
 	return err;
 }
@@ -884,9 +888,11 @@ static void panfrost_remove(struct platform_device *pdev)
 	drm_dev_unregister(&pfdev->base);
 
 	pm_runtime_get_sync(pfdev->base.dev);
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);

Let's keep the order consistent with the probe path:

	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
	pm_runtime_disable(pfdev->base.dev);
	panfrost_device_fini(pfdev);
	pm_runtime_set_suspended(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);
Quoted 4 lines

I also think this deserves comments to explain the noresume/noidle dance (device_init/fini take care of clks internally, and when they return the device is resumed/suspended, so all we have to is update the state, and acquire a ref).

I don't mind doing this, but I think the fact we're calling noresume/noidle functions already implies device power-up is being handled manually rather by having RPM invoke resume and suspend callbacks behind the scenes, so we've no choice other than adjusting PM user counter and status ourselves.

Quoted 7 lines

drivers/gpu/drm/panfrost/panfrost_drv.c static void panfrost_remove(struct platform_device *pdev) · line 898

Quoted 7 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +1 -1
-	panfrost_device_fini(pfdev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);
 }
 
 static ssize_t profiling_show(struct device *dev,

Adrian Larumbe

author maintainer
#25 lore raw

Re: [PATCH v9 10/16] drm/panfrost: Add debugfs knob for manually triggering a GPU reset

In reply to Boris Brezillon
Quoted 45 lines · Boris Brezillon
Quoted 4 lines · On Sat, 12 Sep 2026 00:28:11 +0100 Adrián Larumbe…

This will be of great help when testing potential races between the GPU reset sequence and other parts of the code accessing HW registers.

One comment below.

Quoted 33 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c | 37 ++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpu/drm/panfrost/panfrost_device.c +14 -0
index d75566a45392..0aee1d87a7ab 100644
@@ -2,6 +2,7 @@
 /* Copyright 2018 Marty E. Plummer <[email protected]> */
 /* Copyright 2019 Linaro, Ltd, Rob Herring <[email protected]> */
 
+#include <linux/debugfs.h>
 #include <linux/clk.h>
 #include <linux/reset.h>
 #include <linux/platform_device.h>
@@ -583,9 +584,45 @@ EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = {
 };
 
 #ifdef CONFIG_DEBUG_FS
+static int reset_get(void *data, u64 *val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
+
+	*val = atomic_read(&pfdev->reset.pending);
+	return 0;
+}
+
+static int reset_set(void *data, u64 val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_device.c EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = { · line 603

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_device.c +4 -0
+	int ret = pm_runtime_get_if_active(pfdev->base.dev);
+
+	if (ret) {
+		panfrost_device_schedule_reset(pfdev);

It feels weird to schedule a reset if the device is suspended, because a suspend/resume cycle already acts as a reset. Any particular reason you'd want to do that? If not, I'd suggest going for:

That's been my goal all along, for the knob to trigger a reset only when the device isn't suspended. pm_runtime_get_if_active() docs states that:

"""
Increment the runtime PM usage counter of @dev if its runtime PM status is %RPM_ACTIVE, in which case it returns 1. If the device is in a different state, 0 is returned. -EINVAL is returned if runtime PM is disabled for the device, in which case also the usage_count will remain unmodified. """

So (ret != 0) means the device either doesn't support RPM or else it was in the ACTIVE state. In both cases, that means the device is powered so we can proceed with the reset. However, I only decrement the usage counter when kernel RPM is enabled (ret > 1).

Quoted 25 lines
	if (ret <= 0)
		return ret;

	panfrost_device_schedule_reset(pfdev);
	pm_runtime_put(pfdev->base.dev);
	return 0;
Quoted 18 lines
drivers/gpu/drm/panfrost/panfrost_device.c +18 -0
+		flush_work(&pfdev->reset.work);
+		if (ret > 0)
+			pm_runtime_put(pfdev->base.dev);
+	}
+
+	return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(panfrost_reset_debugfs_fops,
+			 reset_get, reset_set,
+			 "0x%08llx\n");
+
+static void panfrost_reset_debugfs_init(struct drm_minor *minor)
+{
+	debugfs_create_file("reset", 0600, minor->debugfs_root,
+			    minor->dev, &panfrost_reset_debugfs_fops);
+}
+
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_device.c EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = { · line 628

Quoted 8 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -0
 void panfrost_device_debugfs_init(struct drm_minor *minor)
 {
 	panfrost_gem_debugfs_init(minor);
 	panfrost_sched_debugfs_init(minor);
+	panfrost_reset_debugfs_init(minor);
 }
 #endif // CONFIG_DEBUG_FS

Adrian Larumbe

author maintainer
#44 lore raw

Re: [PATCH v9 16/16] drm/panfrost: Bump driver minor to reflect new DUMP IOCTL req field

In reply to Boris Brezillon
Quoted 8 lines · Boris Brezillon
Quoted 1 line · On Sat, 12 Sep 2026 00:28:17 +0100 Adrián Larumbe…

Expose expanded PERFCNT_DUMP IOCTL interface to user mode.

I don't think it makes sense to have the version bump in a separate commit when the changes adding support for a uAPI extension are all in a single commit, so I'd squash this commit into patch 15.

Will do.

Quoted 25 lines
Quoted 25 lines
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -1
index 571a26b84126..de9b1c115181 100644
@@ -808,6 +808,7 @@ static const struct file_operations panfrost_drm_driver_fops = {
  * - 1.6 - adds PANFROST_BO_MAP_WB, PANFROST_IOCTL_SYNC_BO,
  *	   PANFROST_IOCTL_QUERY_BO_INFO and
  *	   DRM_PANFROST_PARAM_SELECTED_COHERENCY
+ * - 1.7 - adds PERFCNT_DUMP req state field
  */
 static const struct drm_driver panfrost_drm_driver = {
 	.driver_features	= DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ,
@@ -820,7 +821,7 @@ static const struct drm_driver panfrost_drm_driver = {
 	.name			= "panfrost",
 	.desc			= "panfrost DRM",
 	.major			= 1,
-	.minor			= 6,
+	.minor			= 7,
 
Quoted 3 lines

drivers/gpu/drm/panfrost/panfrost_drv.c static const struct drm_driver panfrost_drm_driver = { · line 827

Quoted 3 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +0 -0
 	.gem_create_object	= panfrost_gem_create_object,
 	.gem_prime_import	= panfrost_gem_prime_import,

Adrian Larumbe

author maintainer
#39 lore raw

Re: [PATCH v9 15/16] drm/panfrost: Fix races between perfcnt and reset sequence

In reply to Boris Brezillon
Quoted 14 lines · Boris Brezillon
Quoted 10 lines · On Sat, 12 Sep 2026 00:28:16 +0100 Adrián Larumbe…

Formerly, the reset sequence would race with panfrost_mmu_as_put() when tearing down a perfcnt session. On top of that, poking GPU registers to program a perfcnt session or obtaining a dump might lead to undefined behaviour when done at the same time a reset was ongoing.

Use the reset r/w semaphore to govern access to the hardware at reset time. On top of that, expand the DRM uAPI for the perfcnt DUMP operation so that userspace can be made aware of a reset having happened, because that means counters will go back to 0 and can no longer be accumulated to values previously kept in user space.

Quoted 5 lines

In GPU_PERFCNT_CFG_MODE_MANUAL mode (which is the one we use), internal counters are always cleared after each DUMP request. So, it's not so much that counters can't be accumulated after a RESET, it's more that we've lost data in the process, making this very sample inaccurate (counters lower than they should be).

I wasn't aware of this. I'll rephrase the commit message accordingly.

Quoted 42 lines
Quoted 42 lines

The new perfcnt-aware reset sequence also takes care to reestablish perfcnt to its original configuration if there was an enabled session, or else flags the current session as dead if that failed.

Fixes 73e467f60acd ("drm/panfrost: Consolidate reset handling")
Fixes 7786fd108777 ("drm/panfrost: Expose performance counters through unstable ioctls")
---
 drivers/gpu/drm/panfrost/panfrost_device.c  |   2 +
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 201 +++++++++++++++++++---------
 drivers/gpu/drm/panfrost/panfrost_perfcnt.h |   1 +
 include/uapi/drm/panfrost_drm.h             |   8 +-
 4 files changed, 151 insertions(+), 61 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +2 -0
index 6c65feae63aa..e774f61c642b 100644
@@ -479,6 +479,8 @@ void panfrost_device_reset(struct panfrost_device *pfdev, bool enable_job_int)
 	panfrost_jm_reset_interrupts(pfdev);
 	if (enable_job_int)
 		panfrost_jm_enable_interrupts(pfdev);
+
+	panfrost_perfcnt_reset(pfdev);
 }
 
 static int panfrost_device_runtime_resume(struct device *dev)
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +1 -0
index b3f71d7fd82a..9847657179a5 100644
@@ -11,6 +11,7 @@
 #include <drm/drm_file.h>
 #include <drm/drm_gem_shmem_helper.h>
 #include <drm/panfrost_drm.h>
+#include <drm/drm_print.h>
 
 #include "panfrost_device.h"
 #include "panfrost_features.h"
@@ -28,11 +29,15 @@
 
Quoted 13 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c line 39

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +3 -0
 struct panfrost_perfcnt {
 	struct panfrost_gem_mapping *mapping;
+	unsigned int counterset;
 	size_t bosize;
 	void *buf;
 	struct panfrost_file_priv *user;
 	struct mutex lock;
 	struct completion dump_comp;
+	bool reset_happened;
+	bool dump_finished;

Why not store the state flags directly instead of these dump_finished/reset_happened booleans?

I had thought about this, but didn't do it because up until right before sending the patch series, there were only two of them (reset_happened and owns_as_ref) and thought owns_as_ref being set to false implies reset_happened is true, so a state flags variable seemed like an overkill. However, after adding dump_finished, it'd be best to declare a single flags variable and the possible values as bitmap values.

Quoted 89 lines
Quoted 89 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +58 -12
+	bool owns_as_ref;
 };
 
 static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev)
@@ -47,36 +52,113 @@ static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev)
 
 void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev)
 {
+	pfdev->perfcnt->dump_finished = true;
 	complete(&pfdev->perfcnt->dump_comp);
 }
 
 void panfrost_perfcnt_sample_done(struct panfrost_device *pfdev)
 {
-	if (pfdev->features.selected_coherency != COHERENCY_ACE)
+	if (pfdev->features.selected_coherency != COHERENCY_ACE) {
 		gpu_write(pfdev, GPU_CMD, GPU_CMD_CLEAN_CACHES);
-	else
+	} else {
+		pfdev->perfcnt->dump_finished = true;
 		complete(&pfdev->perfcnt->dump_comp);
+	}
+}
+
+static int panfrost_perfcnt_hw_enable(struct panfrost_device *pfdev)
+{
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	u32 cfg, as;
+	int ret;
+
+	ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu);
+	if (ret < 0)
+		return ret;
+
+	as = ret;
+	cfg = GPU_PERFCNT_CFG_AS(as) |
+	      GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_MANUAL);
+
+	/*
+	 * Bifrost GPUs have 2 set of counters, but we're only interested by
+	 * the first one for now.
+	 */
+	if (panfrost_model_is_bifrost(pfdev))
+		cfg |= GPU_PERFCNT_CFG_SETSEL(perfcnt->counterset);
+
+	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0xffffffff);
+	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0xffffffff);
+	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0xffffffff);
+
+	/*
+	 * Due to PRLAM-8186 we need to disable the Tiler before we enable HW
+	 * counters.
+	 */
+	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
+		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
+	else
+		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+
+	gpu_write(pfdev, GPU_PERFCNT_CFG, cfg);
+
+	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
+		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+
+	return 0;
 }
 
-static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
+static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev, u32 *state)
 {
-	u64 gpuva;
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	u64 gpuva = perfcnt->mapping->mmnode.start << PAGE_SHIFT;
 	int ret;
 
-	reinit_completion(&pfdev->perfcnt->dump_comp);
-	gpuva = pfdev->perfcnt->mapping->mmnode.start << PAGE_SHIFT;
-	gpu_write(pfdev, GPU_PERFCNT_BASE_LO, lower_32_bits(gpuva));
-	gpu_write(pfdev, GPU_PERFCNT_BASE_HI, upper_32_bits(gpuva));
-	gpu_write(pfdev, GPU_INT_CLEAR,
-		  GPU_IRQ_CLEAN_CACHES_COMPLETED |
-		  GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_SAMPLE);
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		perfcnt->dump_finished = false;
+		*state = 0;
+
+		if (!perfcnt->owns_as_ref) {
+			*state = PANFROST_PERFCNT_SESSION_DEAD;
+			return -EIO;
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) · line 129

Quoted 6 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +6 -0
+		}
+
+		if (perfcnt->reset_happened) {
+			*state = PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
+			perfcnt->reset_happened = false;
+		}
		*state = perfcnt->state;

I'm thinking maybe we don't need to set the ioctl output state here. Even if there was a reset, if we recovered from it and didn't lose the AS reference, we might be able to go forward as usual. In that case, counters would be measured since the latest reset rather than the last successful dump, although maybe this is not an issue.

Quoted 4 lines
		if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
			return -EIO;

		perfcnt->state = 0;

I'll create a bitmap enum for this to work.

Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +25 -4
+
+		reinit_completion(&pfdev->perfcnt->dump_comp);
+
+		gpu_write(pfdev, GPU_PERFCNT_BASE_LO, lower_32_bits(gpuva));
+		gpu_write(pfdev, GPU_PERFCNT_BASE_HI, upper_32_bits(gpuva));
+		gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_CLEAN_CACHES_COMPLETED |
+						GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
+		gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_SAMPLE);
+	}
+
+	/*
+	 * Here we release the reset semaphore because perfcnt should not get in the way
+	 * of a HW reset. Besides, a legitimate reset might be issued during the wait.
+	 */
 	ret = wait_for_completion_interruptible_timeout(&pfdev->perfcnt->dump_comp,
 							msecs_to_jiffies(1000));
-	if (!ret)
-		ret = -ETIMEDOUT;
-	else if (ret > 0)
-		ret = 0;
+
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		/* Either sample finished or reset happened */
+		if (ret > 0) {
+			ret = perfcnt->dump_finished ? 0 :
+			      perfcnt->owns_as_ref ? -EAGAIN : -EIO;
+
+		} else if (!ret) {
+			ret = -ETIMEDOUT;
+		}
+
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) · line 161

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +5 -0
+		if (perfcnt->reset_happened)
+			*state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
+		if (!perfcnt->owns_as_ref)
+			*state |= PANFROST_PERFCNT_SESSION_DEAD;
+	}
	if (!ret)
		return -ETIMEDOUT;

I was doing this check inside the guard because, in the time between the completion is signalled and the semaphore locked, another reset could come right through. And then because I believed counter values were accumulated rather than reset between consecutive dumps, it was best to notify the user as soon as possible, even if that meant losing one legitimate frame.

Quoted 8 lines
	scoped_guard(rwsem_read, &pfdev->reset.lock) {
		u32 new_state = perfcnt->state;

		*state |= new_state;
		if (new_state & PANFROST_PERFCNT_SESSION_DEAD)
			return -EIO;

		perfcnt->state = 0;

I think this should not be here, because we're already resetting the state before running the PERFCNT_SAMPLE GPU command, and also we need to check it right below.

Quoted 4 lines
		/* If we faced a reset during our SAMPLE, the user needs to try again. */
		if (perfcnt->state & PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET)
			return -EAGAIN;
	}

The main reason I added the panfrost_perfcnt::dump_finished flag was that when a reset happens, the GPU IRQ handler might've been already running in response to a finished perfcnt sample command. However, the reset sequence might come off first and cancel the completion, leaving us with a ret > 0 despite sampling having succeeded.

I'm thinking rather than trying to sort of patch this up precariously as I did in this revision, I'll leave it the way you suggested and address the race between the GPU IRQ handler and the reset sequence in a future series.

Quoted 1 line
	return 0;

We still need to return -ERESTARTSYS when wait_for_completion_interruptible_timeout() is interrupted from UM.

Quoted 69 lines
Quoted 69 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +12 -36
 	return ret;
 }
@@ -87,9 +169,8 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 {
 	struct panfrost_file_priv *user = file_priv->driver_priv;
 	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
-	struct iosys_map map;
 	struct drm_gem_shmem_object *bo;
-	u32 cfg, as;
+	struct iosys_map map;
 	int ret;
 
 	if (user == perfcnt->user)
@@ -122,54 +203,31 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 	ret = drm_gem_vmap(&bo->base, &map);
 	if (ret)
 		goto err_put_mapping;
+
 	perfcnt->buf = map.vaddr;
+	perfcnt->counterset = counterset;
 
 	panfrost_gem_internal_set_label(&bo->base, "Perfcnt sample buffer");
 
-	/*
-	 * Clear the counters to start from a fresh state.
-	 */
-	gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_CLEAR);
-
-	ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu);
-	if (ret < 0)
-		goto err_vunmap;
-
-	as = ret;
-	cfg = GPU_PERFCNT_CFG_AS(as) |
-	      GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_MANUAL);
-
-	/*
-	 * Bifrost GPUs have 2 set of counters, but we're only interested by
-	 * the first one for now.
-	 */
-	if (panfrost_model_is_bifrost(pfdev))
-		cfg |= GPU_PERFCNT_CFG_SETSEL(counterset);
-
-	gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0xffffffff);
-	gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0xffffffff);
-	gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0xffffffff);
-
-	/*
-	 * Due to PRLAM-8186 we need to disable the Tiler before we enable HW
-	 * counters.
-	 */
-	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0);
-	else
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		/*
+		 * Clear the counters to start from a fresh state.
+		 */
+		gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
+		gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_CLEAR);
 
-	gpu_write(pfdev, GPU_PERFCNT_CFG, cfg);
+		ret = panfrost_perfcnt_hw_enable(pfdev);
+		if (ret)
+			goto err_vunmap;
 
Quoted 7 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev, · line 224

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +2 -2
-	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+		perfcnt->reset_happened = false;
+		perfcnt->owns_as_ref = true;

This should probably be set in panfrost_perfcnt_hw_enable(), just after the panfrost_mmu_as_get() call.

Will do in the next revision.

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +2 -2
+		perfcnt->user = user;
+	}
 
 	/* The BO ref is retained by the mapping. */
 	drm_gem_object_put(&bo->base);
 
-	perfcnt->user = user;
-
 	return 0;
 
 err_vunmap:
@@ -195,13 +253,16 @@ static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev,
 	if (user != perfcnt->user)
 		return -EINVAL;
 
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev, · line 259

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +4 -1
-	panfrost_perfcnt_hw_disable(pfdev);
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		panfrost_perfcnt_hw_disable(pfdev);
+		if (perfcnt->owns_as_ref)
+			panfrost_mmu_as_put(pfdev, perfcnt->mapping->mmu);

Similarly, I think it'd be preferable to have this as_put() inside perfcnt_hw_disable().

Agreed.

Quoted 44 lines
Quoted 44 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +17 -3
+		perfcnt->user = NULL;
+	}
 
-	perfcnt->user = NULL;
 	drm_gem_vunmap(&perfcnt->mapping->obj->base.base, &map);
 	perfcnt->buf = NULL;
 	panfrost_gem_close(&perfcnt->mapping->obj->base.base, file_priv);
-	panfrost_mmu_as_put(pfdev, perfcnt->mapping->mmu);
 	panfrost_gem_mapping_put(perfcnt->mapping);
 	perfcnt->mapping = NULL;
 	pm_runtime_put_autosuspend(pfdev->base.dev);
@@ -249,13 +310,16 @@ int panfrost_ioctl_perfcnt_dump(struct drm_device *dev, void *data,
 	if (ret)
 		return ret;
 
+	if (req->pad)
+		return -EINVAL;
+
 	mutex_lock(&perfcnt->lock);
 	if (perfcnt->user != file_priv->driver_priv) {
 		ret = -EINVAL;
 		goto out;
 	}
 
-	ret = panfrost_perfcnt_dump_locked(pfdev);
+	ret = panfrost_perfcnt_dump_locked(pfdev, &req->state);
 	if (ret)
 		goto out;
 
@@ -338,3 +402,20 @@ void panfrost_perfcnt_fini(struct panfrost_device *pfdev)
 	/* Disable everything before leaving. */
 	panfrost_perfcnt_hw_disable(pfdev);
 }
+
+void panfrost_perfcnt_reset(struct panfrost_device *pfdev)
+{
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+
+	if (drm_WARN_ON(&pfdev->base, !perfcnt))
+		return;
+
+	lockdep_assert_held(&pfdev->reset.lock);
+
+	if (!perfcnt->user)
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c void panfrost_perfcnt_fini(struct panfrost_device *pfdev) · line 420

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +5 -0
+		return;
+
+	perfcnt->owns_as_ref = !panfrost_perfcnt_hw_enable(pfdev);
+	perfcnt->reset_happened = true;
+	complete(&perfcnt->dump_comp);
	/* All active AS are released during the MMU post_reset. */
	perfcnt->owns_as_ref = false;

I'm guessing this should go away as soon as I move it into panfrost_perfcnt_hw_enable().

Quoted 45 lines
	perfcnt->state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
	if (panfrost_perfcnt_hw_enable(pfdev))
		perfcnt->state |= PANFROST_PERFCNT_SESSION_DEAD;

	/* Unblock pending sample requests. */
	complete(&perfcnt->dump_comp);
Quoted 39 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +1 -0
+}
drivers/gpu/drm/panfrost/panfrost_perfcnt.h +1 -0
index 8bbcf5f5fb33..8b9bc704b634 100644
@@ -14,5 +14,6 @@ int panfrost_ioctl_perfcnt_enable(struct drm_device *dev, void *data,
 				  struct drm_file *file_priv);
 int panfrost_ioctl_perfcnt_dump(struct drm_device *dev, void *data,
 				struct drm_file *file_priv);
+void panfrost_perfcnt_reset(struct panfrost_device *pfdev);
 
 #endif
include/uapi/drm/panfrost_drm.h +7 -1
index 50d5337f35ef..97e001040543 100644
@@ -47,7 +47,7 @@ extern "C" {
  * them for anything but debugging purpose.
  */
 #define DRM_IOCTL_PANFROST_PERFCNT_ENABLE	DRM_IOW(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_ENABLE, struct drm_panfrost_perfcnt_enable)
-#define DRM_IOCTL_PANFROST_PERFCNT_DUMP		DRM_IOW(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_DUMP, struct drm_panfrost_perfcnt_dump)
+#define DRM_IOCTL_PANFROST_PERFCNT_DUMP		DRM_IOWR(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_DUMP, struct drm_panfrost_perfcnt_dump)
 
 #define PANFROST_JD_REQ_FS (1 << 0)
 #define PANFROST_JD_REQ_CYCLE_COUNT (1 << 1)
@@ -270,8 +270,14 @@ struct drm_panfrost_perfcnt_enable {
 	__u32 counterset;
 };
 
+/* Perfcnt dump state as influenced by a HW reset */
+#define PANFROST_PERFCNT_SESSION_DEAD                 (1 << 0)
+#define PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET (1 << 1)
+
 struct drm_panfrost_perfcnt_dump {
 	__u64 buf_ptr;
+	__u32 state;
+	__u32 pad;		/* MBZ */
 };
 
Quoted 2 lines

include/uapi/drm/panfrost_drm.h struct drm_panfrost_perfcnt_enable { · line 283

Quoted 2 lines
include/uapi/drm/panfrost_drm.h +0 -0
 /* madvise provides a way to tell the kernel in case a buffers contents

Adrian Larumbe

maintainer
#4 lore raw

Re: [PATCH v9 01/16] drm/panfrost: Move shrinker initialization and unplug one level down

In reply to Adrián Larumbe
Quoted 52 lines · On Tue, 22 Sep 2026 20:48:49 +0100 Adrián Larumbe…
Quoted 34 lines · Boris Brezillon
Quoted 10 lines · On Sat, 12 Sep 2026 00:28:02 +0100 Adrián Larumbe…

Since the moment we call drm_dev_register() the device should be in a position to accept jobs, so it's best if the shrinker is already initialized by then.

On top of that, make shrinker functions take an panfrost_device pointer like other functions in the same sequence and rename them accordingly.

Essentially mimic the init/fini behaviour in Panthor.

One remark below.

Quoted 16 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c       |  8 +++++++-
 drivers/gpu/drm/panfrost/panfrost_drv.c          |  6 ------
 drivers/gpu/drm/panfrost/panfrost_gem.c          | 26 ++++++++++++++----------
 drivers/gpu/drm/panfrost/panfrost_gem.h          |  7 ++++---
 drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c |  8 ++------
 5 files changed, 28 insertions(+), 27 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +0 -0
index 485349faf251..05c40d5a20b5 100644
@@ -280,9 +280,14 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	if (err)
 		goto out_job;
 
Quoted 9 lines

drivers/gpu/drm/panfrost/panfrost_device.c int panfrost_device_init(struct panfrost_device *pfdev) · line 283

Quoted 2 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
-	panfrost_gem_init(pfdev);
+	err = panfrost_gem_init(pfdev);  

It feels weird to have the GEM subsystem initialized last when you consider the fact other subsystems might want to allocate GEMs in their _init() function. I know it's where the panfrost_gem_init() is right now, and that ultimately it doesn't prevent anyone from allocating GEMs, but I think it would make sense have this called before any of the other subsystem init functions, still.

I think we discussed having panfrost_gem_init() be called before the other subsystem init functions, but then I checked panthor and saw it's also being called right after all the others there too. I think you're right that it's best to initialise gem first, because I wonder whether other subsystem init functions creating GEMs when the mount point for huge page-backed objects hasn't been created yet could lead to some sort of trouble.

Quoted 2 lines

However, given how much this patch series has already grown, I believe it'd be best to leave this change for a later one.

Sounds good to me.

maintainer
#26 lore raw

Re: [PATCH v9 10/16] drm/panfrost: Add debugfs knob for manually triggering a GPU reset

In reply to Adrián Larumbe
Quoted 65 lines · On Tue, 22 Sep 2026 20:54:11 +0100 Adrián Larumbe…
Quoted 45 lines · Boris Brezillon
Quoted 4 lines · On Sat, 12 Sep 2026 00:28:11 +0100 Adrián Larumbe…

This will be of great help when testing potential races between the GPU reset sequence and other parts of the code accessing HW registers.

One comment below.

Quoted 33 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c | 37 ++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpu/drm/panfrost/panfrost_device.c +14 -0
index d75566a45392..0aee1d87a7ab 100644
@@ -2,6 +2,7 @@
 /* Copyright 2018 Marty E. Plummer <[email protected]> */
 /* Copyright 2019 Linaro, Ltd, Rob Herring <[email protected]> */
 
+#include <linux/debugfs.h>
 #include <linux/clk.h>
 #include <linux/reset.h>
 #include <linux/platform_device.h>
@@ -583,9 +584,45 @@ EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = {
 };
 
 #ifdef CONFIG_DEBUG_FS
+static int reset_get(void *data, u64 *val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
+
+	*val = atomic_read(&pfdev->reset.pending);
+	return 0;
+}
+
+static int reset_set(void *data, u64 val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_device.c EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = { · line 603

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_device.c +4 -0
+	int ret = pm_runtime_get_if_active(pfdev->base.dev);
+
+	if (ret) {
+		panfrost_device_schedule_reset(pfdev);  

It feels weird to schedule a reset if the device is suspended, because a suspend/resume cycle already acts as a reset. Any particular reason you'd want to do that? If not, I'd suggest going for:

That's been my goal all along, for the knob to trigger a reset only when the device isn't suspended. pm_runtime_get_if_active() docs states that:

"""
Increment the runtime PM usage counter of @dev if its runtime PM status is %RPM_ACTIVE, in which case it returns 1. If the device is in a different state, 0 is returned. -EINVAL is returned if runtime PM is disabled for the device, in which case also the usage_count will remain unmodified. """

Quoted 4 lines

So (ret != 0) means the device either doesn't support RPM or else it was in the ACTIVE state. In both cases, that means the device is powered so we can proceed with the reset. However, I only decrement the usage counter when kernel RPM is enabled (ret > 1).

Okay, I see. Can we have a comment explaining that? Also, I think I prefer an early bail out over the extra indentation level added here.

	if (!ret)
		return 0;

Oh, and we should probably make this knob inactive in the device removal path (with a disable_work()?), otherwise -EINVAL will be returned as soon as pm_runtime_disable() is called, and this might cause a reset to be scheduled, which we probably don't want, since it would interfere with our device_fini() logic.

Quoted 36 lines
Quoted 26 lines
	if (ret <= 0)
		return ret;

	panfrost_device_schedule_reset(pfdev);
	pm_runtime_put(pfdev->base.dev);
	return 0;
Quoted 18 lines
drivers/gpu/drm/panfrost/panfrost_device.c +18 -0
+		flush_work(&pfdev->reset.work);
+		if (ret > 0)
+			pm_runtime_put(pfdev->base.dev);
+	}
+
+	return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(panfrost_reset_debugfs_fops,
+			 reset_get, reset_set,
+			 "0x%08llx\n");
+
+static void panfrost_reset_debugfs_init(struct drm_minor *minor)
+{
+	debugfs_create_file("reset", 0600, minor->debugfs_root,
+			    minor->dev, &panfrost_reset_debugfs_fops);
+}
+
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_device.c EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = { · line 628

Quoted 8 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -0
 void panfrost_device_debugfs_init(struct drm_minor *minor)
 {
 	panfrost_gem_debugfs_init(minor);
 	panfrost_sched_debugfs_init(minor);
+	panfrost_reset_debugfs_init(minor);
 }
 #endif // CONFIG_DEBUG_FS

Adrian Larumbe

maintainer
#16 lore raw

Re: [PATCH v9 06/16] drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove

In reply to Adrián Larumbe
Quoted 88 lines · On Tue, 22 Sep 2026 20:51:48 +0100 Adrián Larumbe…
Quoted 67 lines · Boris Brezillon
Quoted 50 lines · On Sat, 12 Sep 2026 00:28:07 +0100 Adrián Larumbe…

During device probe(), failure to do a PM get() will leave the usage_count set to 0, which is the value assigned at device creation time. That means when the autosuspend delay expires, runtime suspend callback won't be invoked, so the device will remain powered on forever.

On top of that, failure to call PM put() during device unplug means Panfrost device's PM usage_count increases monotonically for every new module reload.

The outcome of both of the above meant that:

- Devfreq OPP transition notifications would be printed all the time, even when no jobs are being submitted. This quickly fills the kernel ring buffer with junk.
- Because MMU interrupts are only enabled when the device is reset, the very first job targeting the tiler heap BO after device probe() would always time out, since the driver's PM runtime resume callback would not be invoked.

To fix the above:
- Manually adjust the PM refcnt at device probe and removal time.
- Ensure pm_runtime_dont_use_autosuspend is called in the wind-down path.
- Call pm_runtime_put_autosuspend() when device is ready to accept jobs
- Move pm_runtime_set_suspended() before panfrost_device_fini() so that resource unwinding happens in the opposite order as initialisation.

Fixes 635430797d3f ("drm/panfrost: Rework runtime PM initialization")
Fixes 876b15d2c88d ("drm/panfrost: Fix module unload")
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -0
index 55fc22e8d4d4..a3eff77add55 100644
@@ -854,6 +854,7 @@ static int panfrost_probe(struct platform_device *pdev)
 
 	pm_runtime_set_active(pfdev->base.dev);
 	pm_runtime_mark_last_busy(pfdev->base.dev);
+	pm_runtime_get_noresume(pfdev->base.dev);
 	pm_runtime_enable(pfdev->base.dev);
 	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
 	pm_runtime_use_autosuspend(pfdev->base.dev);
@@ -866,13 +867,16 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
+	pm_runtime_put_autosuspend(pfdev->base.dev);

drivers/gpu/drm/panfrost/panfrost_drv.c static int panfrost_probe(struct platform_device *pdev) · line 879

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +3 -1
 
 	return 0;
 
 err_out1:
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);  

Not an issue per-se, because put_noidle() is a NOP, but I think it'd be easier to reason about with this order:

Quoted 8 lines
	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
  	pm_runtime_disable(pfdev->base.dev);
	panfrost_device_fini(pfdev);
  	pm_runtime_set_suspended(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);

This makes it clear that panfrost_device_fini() assumes the device is resumed when it's called and suspended when it returns.

Your suggestion feels more natural, which makes me wonder why I decided to move panfrost_device_fini() to the end of the block.

IIRC we chatted about where to place pm_runtime_put_noidle() and pm_runtime_set_suspended() in panfrost_device_init() when one of the subsystem initialisations failed. My intuition had been to leave them until the very bottom of it, in imitation of what you hinted here, but then it'd go against the notion that winding down must be done in the opposite order as initialisation. And so because panfrost_device_init() happens before all the pm_runtime_* incantations, I thought panfrost_device_fini() should be placed at the end in the error and driver remove paths.

Quoted 5 lines

That said, I think a more intuitive init order would be one that imitates what happens in Panthor when runtime PM is enabled:

	/* First thing done in pm_runtime_resume_and_get -> pm_runtime_get_active -> __pm_runtime_resume */
	pm_runtime_get_noresume(pfdev->base.dev);

Panthor is a bit different in that it doesn't use noresume/noidle() at all. What it does instead is teach the resume/suspend hooks about the uninitialized device use case, and it skips some operations when a resume/suspend() is done on an uninitialized dev. Panfrost, on the other hand, just assumes that clks are enabled manually before any of the subsystem _init() helpers are called. TLDR; this pm_runtime_get_noresume() can be moved after all the _init() without any downsides, and it keeps all PM related-init in just one place, which I find neat.

Quoted 19 lines
	
	/* Now come subsystem initialisations */
	panfrost_gpu_init(pfdev);
	panfrost_mmu_init(pfdev);
	panfrost_jm_init(pfdev);
	panfrost_perfcnt_init(pfdev);
	panfrost_gem_init(pfdev);
	
	/* __pm_runtime_resume -> rpm_resume first invokes the resume callback and then
	 * updates runtime status to RPM_ACTIVE. */
	pm_runtime_set_active(pfdev->base.dev);
	
	pm_runtime_enable(ptdev->base.dev);
        pm_runtime_mark_last_busy(pfdev->base.dev);
	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
	pm_runtime_use_autosuspend(pfdev->base.dev);

And then in the error and device remove path would be almost just like you suggest.

Quoted 8 lines
        pm_runtime_dont_use_autosuspend(pfdev->base.dev);
	pm_runtime_disable(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);
	panfrost_device_fini(pfdev);
	pm_runtime_set_suspended(pfdev->base.dev);

The only difference is that pm_runtime_put_noidle() happens before device_fini because that's the same order as in __pm_runtime_suspend().

I really think we should keep the init/fini order consisten, so put_noidle() should be after the _fini() calls. But as I said above, I'd probably have get_noresume() called after all the _init() functions instead, since we can afford it in Panfrost, and it helps keeping all the PM-related calls in just one block.

Quoted 4 lines

However, that means in the commit where I move all DRM initialisation into panfrost_device_init(), in order to keep things consistent, I should move pm_runtime_set_suspended() to the very end of the error path.

It probably doesn't matter much actually, because as soon as pm_runtime_disable() is called, nothing can change the RPM state anymore. So maybe scratch all of my comments, and we stick to what you had. It's just a transition state anyway, so, as long as it doesn't regress the probe/remove logic, it's probably fine.

Quoted 25 lines
Quoted 20 lines

drivers/gpu/drm/panfrost/panfrost_drv.c static void panfrost_remove(struct platform_device *pdev) · line 893

Quoted 11 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -0
 err_out0:
 	return err;
 }
@@ -884,9 +888,11 @@ static void panfrost_remove(struct platform_device *pdev)
 	drm_dev_unregister(&pfdev->base);
 
 	pm_runtime_get_sync(pfdev->base.dev);
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);  

Let's keep the order consistent with the probe path:

	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
	pm_runtime_disable(pfdev->base.dev);
	panfrost_device_fini(pfdev);
	pm_runtime_set_suspended(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);
Quoted 4 lines

I also think this deserves comments to explain the noresume/noidle dance (device_init/fini take care of clks internally, and when they return the device is resumed/suspended, so all we have to is update the state, and acquire a ref).

Quoted 4 lines

I don't mind doing this, but I think the fact we're calling noresume/noidle functions already implies device power-up is being handled manually rather by having RPM invoke resume and suspend callbacks behind the scenes, so we've no choice other than adjusting PM user counter and status ourselves.

Fair enough.

maintainer
#40 lore raw

Re: [PATCH v9 15/16] drm/panfrost: Fix races between perfcnt and reset sequence

In reply to Adrián Larumbe
Quoted 32 lines · On Wed, 23 Sep 2026 02:39:10 +0100 Adrián Larumbe…
Quoted 23 lines
Quoted 23 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -10
-static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
+static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev, u32 *state)
 {
-	u64 gpuva;
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	u64 gpuva = perfcnt->mapping->mmnode.start << PAGE_SHIFT;
 	int ret;
 
-	reinit_completion(&pfdev->perfcnt->dump_comp);
-	gpuva = pfdev->perfcnt->mapping->mmnode.start << PAGE_SHIFT;
-	gpu_write(pfdev, GPU_PERFCNT_BASE_LO, lower_32_bits(gpuva));
-	gpu_write(pfdev, GPU_PERFCNT_BASE_HI, upper_32_bits(gpuva));
-	gpu_write(pfdev, GPU_INT_CLEAR,
-		  GPU_IRQ_CLEAN_CACHES_COMPLETED |
-		  GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_SAMPLE);
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		perfcnt->dump_finished = false;
+		*state = 0;
+
+		if (!perfcnt->owns_as_ref) {
+			*state = PANFROST_PERFCNT_SESSION_DEAD;
+			return -EIO;
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) · line 129

Quoted 6 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +6 -0
+		}
+
+		if (perfcnt->reset_happened) {
+			*state = PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
+			perfcnt->reset_happened = false;
+		}  
		*state = perfcnt->state;  
Quoted 4 lines

I'm thinking maybe we don't need to set the ioctl output state here. Even if there was a reset, if we recovered from it and didn't lose the AS reference, we might be able to go forward as usual. In that case, counters would be measured since the latest reset rather than the last successful dump, although maybe this is not an issue.

I think userspace needs to know about INTERRUPTED_BY_RESET, at the very least. But honestly, I'd keep it simple and propagate the state regardless of the flags userspace might or might not care about.

Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +15 -4
+	/*
+	 * Here we release the reset semaphore because perfcnt should not get in the way
+	 * of a HW reset. Besides, a legitimate reset might be issued during the wait.
+	 */
 	ret = wait_for_completion_interruptible_timeout(&pfdev->perfcnt->dump_comp,
 							msecs_to_jiffies(1000));
-	if (!ret)
-		ret = -ETIMEDOUT;
-	else if (ret > 0)
-		ret = 0;
+
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		/* Either sample finished or reset happened */
+		if (ret > 0) {
+			ret = perfcnt->dump_finished ? 0 :
+			      perfcnt->owns_as_ref ? -EAGAIN : -EIO;
+
+		} else if (!ret) {
+			ret = -ETIMEDOUT;
+		}
+
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) · line 161

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +5 -0
+		if (perfcnt->reset_happened)
+			*state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
+		if (!perfcnt->owns_as_ref)
+			*state |= PANFROST_PERFCNT_SESSION_DEAD;
+	}  
	if (!ret)
		return -ETIMEDOUT;  
Quoted 2 lines

I was doing this check inside the guard because, in the time between the completion is signalled and the semaphore locked, another reset could come right through.

I wouldn't worry about late signaling to be honest, the timeout is 1sec, which is way more than I would expect a DUMP request to take in a normal situation. Now, we might want to propagate the state in that case too, so it might make sense to return ETIMEDOUT inside the scoped_guard, after the state has been copied.

Quoted 7 lines

And then because I believed counter values were accumulated rather than reset between consecutive dumps, it was best to notify the user as soon as possible, even if that meant losing one legitimate frame.

Quoted 3 lines
	scoped_guard(rwsem_read, &pfdev->reset.lock) {
		u32 new_state = perfcnt->state;
Quoted 8 lines
Quoted 5 lines
		*state |= new_state;
		if (new_state & PANFROST_PERFCNT_SESSION_DEAD)
			return -EIO;

		perfcnt->state = 0;  

I think this should not be here, because we're already resetting the state before running the PERFCNT_SAMPLE GPU command, and also we need to check it right below.

I think we want to reset in both places, especially if we propagate the state before the DUMP too, otherwise we might leave the INTERRUPTED_BY_RESET bit behind even though we already informed userspace about this event in the previous DUMP request. And yes, the following test should have been on new_state instead of perfcnt->state, indeed.

Quoted 6 lines
Quoted 5 lines
		/* If we faced a reset during our SAMPLE, the user needs to try again. */
		if (perfcnt->state & PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET)
			return -EAGAIN;
	}  
Quoted 8 lines

The main reason I added the panfrost_perfcnt::dump_finished flag was that when a reset happens, the GPU IRQ handler might've been already running in response to a finished perfcnt sample command. However, the reset sequence might come off first and cancel the completion, leaving us with a ret > 0 despite sampling having succeeded.

I'm thinking rather than trying to sort of patch this up precariously as I did in this revision, I'll leave it the way you suggested and address the race between the GPU IRQ handler and the reset sequence in a future series.

Yes, that's exactly what needs to be done: reset should synchronize IRQs, mask/suspend them while the reset sequence is happening, and unmask them when it's done. As for the completion of a DUMP request interrupted by a RESET, we want a ret > 0 (AKA no-timeout) in that case, but the perfcnt::state should be updated prior to that, so that this function knows exactly what to expect from this completion: if SESSION_INTERRUPTED_BY_RESET is set, it's likely that the DUMP didn't complete which is why I return EAGAIN in that case, but we can also have a dedicated flag for DUMP_COMPLETE if you want to be accurate (when set, DUMP is effective, when not set EAGAIN).

Quoted 5 lines
Quoted 2 lines
	return 0;  

We still need to return -ERESTARTSYS when wait_for_completion_interruptible_timeout() is interrupted from UM.

Oh, absolutely.

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+void panfrost_perfcnt_reset(struct panfrost_device *pfdev)
+{
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+
+	if (drm_WARN_ON(&pfdev->base, !perfcnt))
+		return;
+
+	lockdep_assert_held(&pfdev->reset.lock);
+
+	if (!perfcnt->user)
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_perfcnt.c void panfrost_perfcnt_fini(struct panfrost_device *pfdev) · line 420

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +5 -0
+		return;
+
+	perfcnt->owns_as_ref = !panfrost_perfcnt_hw_enable(pfdev);
+	perfcnt->reset_happened = true;
+	complete(&perfcnt->dump_comp);  
	/* All active AS are released during the MMU post_reset. */
	perfcnt->owns_as_ref = false;  
Quoted 1 line

I'm guessing this should go away as soon as I move it into panfrost_perfcnt_hw_enable().

If you reset it at the beginning of panfrost_perfcnt_hw_enable(), sure, but I think it's easier to assume that when panfrost_perfcnt_hw_enable() is called, the AS is not owned, and reset it here instead.

Quoted 8 lines
Quoted 8 lines
	perfcnt->state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
	if (panfrost_perfcnt_hw_enable(pfdev))
		perfcnt->state |= PANFROST_PERFCNT_SESSION_DEAD;

	/* Unblock pending sample requests. */
	complete(&perfcnt->dump_comp);  
Quoted 1 line

+}

author maintainer
#27 lore raw

Re: [PATCH v9 10/16] drm/panfrost: Add debugfs knob for manually triggering a GPU reset

In reply to Boris Brezillon
Quoted 74 lines · Boris Brezillon
Quoted 65 lines · On Tue, 22 Sep 2026 20:54:11 +0100 Adrián Larumbe…
Quoted 45 lines · Boris Brezillon
Quoted 4 lines · On Sat, 12 Sep 2026 00:28:11 +0100 Adrián Larumbe…

This will be of great help when testing potential races between the GPU reset sequence and other parts of the code accessing HW registers.

One comment below.

Quoted 33 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c | 37 ++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpu/drm/panfrost/panfrost_device.c +14 -0
index d75566a45392..0aee1d87a7ab 100644
@@ -2,6 +2,7 @@
 /* Copyright 2018 Marty E. Plummer <[email protected]> */
 /* Copyright 2019 Linaro, Ltd, Rob Herring <[email protected]> */
 
+#include <linux/debugfs.h>
 #include <linux/clk.h>
 #include <linux/reset.h>
 #include <linux/platform_device.h>
@@ -583,9 +584,45 @@ EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = {
 };
 
 #ifdef CONFIG_DEBUG_FS
+static int reset_get(void *data, u64 *val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
+
+	*val = atomic_read(&pfdev->reset.pending);
+	return 0;
+}
+
+static int reset_set(void *data, u64 val)
+{
+	struct panfrost_device *pfdev =
+		container_of(data, struct panfrost_device, base);
Quoted 8 lines

drivers/gpu/drm/panfrost/panfrost_device.c EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = { · line 603

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_device.c +4 -0
+	int ret = pm_runtime_get_if_active(pfdev->base.dev);
+
+	if (ret) {
+		panfrost_device_schedule_reset(pfdev);  

It feels weird to schedule a reset if the device is suspended, because a suspend/resume cycle already acts as a reset. Any particular reason you'd want to do that? If not, I'd suggest going for:

That's been my goal all along, for the knob to trigger a reset only when the device isn't suspended. pm_runtime_get_if_active() docs states that:

"""
Increment the runtime PM usage counter of @dev if its runtime PM status is %RPM_ACTIVE, in which case it returns 1. If the device is in a different state, 0 is returned. -EINVAL is returned if runtime PM is disabled for the device, in which case also the usage_count will remain unmodified. """

Quoted 4 lines

So (ret != 0) means the device either doesn't support RPM or else it was in the ACTIVE state. In both cases, that means the device is powered so we can proceed with the reset. However, I only decrement the usage counter when kernel RPM is enabled (ret > 1).

Quoted 5 lines

Okay, I see. Can we have a comment explaining that? Also, I think I prefer an early bail out over the extra indentation level added here.

	if (!ret)
		return 0;

Will do.

Quoted 5 lines

Oh, and we should probably make this knob inactive in the device removal path (with a disable_work()?), otherwise -EINVAL will be returned as soon as pm_runtime_disable() is called, and this might cause a reset to be scheduled, which we probably don't want, since it would interfere with our device_fini() logic.

I think replacing cancel_work_sync() with disable_work_sync() in panfrost_jm_fini() should be enough to this end. In that case, both queue_work() and flush_work() will do nothing and no PM reference will be taken, so the whole thing effective becomes a NOP.

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
	if (ret <= 0)
		return ret;

	panfrost_device_schedule_reset(pfdev);
	pm_runtime_put(pfdev->base.dev);
	return 0;
Quoted 20 lines
drivers/gpu/drm/panfrost/panfrost_device.c +18 -0
+		flush_work(&pfdev->reset.work);
+		if (ret > 0)
+			pm_runtime_put(pfdev->base.dev);
+	}
+
+	return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(panfrost_reset_debugfs_fops,
+			 reset_get, reset_set,
+			 "0x%08llx\n");
+
+static void panfrost_reset_debugfs_init(struct drm_minor *minor)
+{
+	debugfs_create_file("reset", 0600, minor->debugfs_root,
+			    minor->dev, &panfrost_reset_debugfs_fops);
+}
+
 void panfrost_device_debugfs_init(struct drm_minor *minor)
 {
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

drivers/gpu/drm/panfrost/panfrost_device.c EXPORT_GPL_DEV_PM_OPS(panfrost_pm_ops) = { · line 628

Quoted 6 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -0
 	panfrost_gem_debugfs_init(minor);
 	panfrost_sched_debugfs_init(minor);
+	panfrost_reset_debugfs_init(minor);
 }
 #endif // CONFIG_DEBUG_FS

Adrian Larumbe

Adrian Larumbe

author maintainer
#17 lore raw

Re: [PATCH v9 06/16] drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove

In reply to Boris Brezillon
Quoted 146 lines · Boris Brezillon
Quoted 88 lines · On Tue, 22 Sep 2026 20:51:48 +0100 Adrián Larumbe…
Quoted 67 lines · Boris Brezillon
Quoted 50 lines · On Sat, 12 Sep 2026 00:28:07 +0100 Adrián Larumbe…

During device probe(), failure to do a PM get() will leave the usage_count set to 0, which is the value assigned at device creation time. That means when the autosuspend delay expires, runtime suspend callback won't be invoked, so the device will remain powered on forever.

On top of that, failure to call PM put() during device unplug means Panfrost device's PM usage_count increases monotonically for every new module reload.

The outcome of both of the above meant that:

- Devfreq OPP transition notifications would be printed all the time, even when no jobs are being submitted. This quickly fills the kernel ring buffer with junk.
- Because MMU interrupts are only enabled when the device is reset, the very first job targeting the tiler heap BO after device probe() would always time out, since the driver's PM runtime resume callback would not be invoked.

To fix the above:
- Manually adjust the PM refcnt at device probe and removal time.
- Ensure pm_runtime_dont_use_autosuspend is called in the wind-down path.
- Call pm_runtime_put_autosuspend() when device is ready to accept jobs
- Move pm_runtime_set_suspended() before panfrost_device_fini() so that resource unwinding happens in the opposite order as initialisation.

Fixes 635430797d3f ("drm/panfrost: Rework runtime PM initialization")
Fixes 876b15d2c88d ("drm/panfrost: Fix module unload")
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -0
index 55fc22e8d4d4..a3eff77add55 100644
@@ -854,6 +854,7 @@ static int panfrost_probe(struct platform_device *pdev)
 
 	pm_runtime_set_active(pfdev->base.dev);
 	pm_runtime_mark_last_busy(pfdev->base.dev);
+	pm_runtime_get_noresume(pfdev->base.dev);
 	pm_runtime_enable(pfdev->base.dev);
 	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
 	pm_runtime_use_autosuspend(pfdev->base.dev);
@@ -866,13 +867,16 @@ static int panfrost_probe(struct platform_device *pdev)
 	if (err < 0)
 		goto err_out1;
 
+	pm_runtime_put_autosuspend(pfdev->base.dev);

drivers/gpu/drm/panfrost/panfrost_drv.c static int panfrost_probe(struct platform_device *pdev) · line 879

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +3 -1
 
 	return 0;
 
 err_out1:
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
-	panfrost_device_fini(pfdev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	panfrost_device_fini(pfdev);  

Not an issue per-se, because put_noidle() is a NOP, but I think it'd be easier to reason about with this order:

Quoted 8 lines
	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
  	pm_runtime_disable(pfdev->base.dev);
	panfrost_device_fini(pfdev);
  	pm_runtime_set_suspended(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);

This makes it clear that panfrost_device_fini() assumes the device is resumed when it's called and suspended when it returns.

Your suggestion feels more natural, which makes me wonder why I decided to move panfrost_device_fini() to the end of the block.

IIRC we chatted about where to place pm_runtime_put_noidle() and pm_runtime_set_suspended() in panfrost_device_init() when one of the subsystem initialisations failed. My intuition had been to leave them until the very bottom of it, in imitation of what you hinted here, but then it'd go against the notion that winding down must be done in the opposite order as initialisation. And so because panfrost_device_init() happens before all the pm_runtime_* incantations, I thought panfrost_device_fini() should be placed at the end in the error and driver remove paths.

Quoted 5 lines

That said, I think a more intuitive init order would be one that imitates what happens in Panthor when runtime PM is enabled:

	/* First thing done in pm_runtime_resume_and_get -> pm_runtime_get_active -> __pm_runtime_resume */
	pm_runtime_get_noresume(pfdev->base.dev);

Panthor is a bit different in that it doesn't use noresume/noidle() at all. What it does instead is teach the resume/suspend hooks about the uninitialized device use case, and it skips some operations when a resume/suspend() is done on an uninitialized dev. Panfrost, on the other hand, just assumes that clks are enabled manually before any of the subsystem _init() helpers are called. TLDR; this pm_runtime_get_noresume() can be moved after all the _init() without any downsides, and it keeps all PM related-init in just one place, which I find neat.

Quoted 19 lines
	
	/* Now come subsystem initialisations */
	panfrost_gpu_init(pfdev);
	panfrost_mmu_init(pfdev);
	panfrost_jm_init(pfdev);
	panfrost_perfcnt_init(pfdev);
	panfrost_gem_init(pfdev);
	
	/* __pm_runtime_resume -> rpm_resume first invokes the resume callback and then
	 * updates runtime status to RPM_ACTIVE. */
	pm_runtime_set_active(pfdev->base.dev);
	
	pm_runtime_enable(ptdev->base.dev);
        pm_runtime_mark_last_busy(pfdev->base.dev);
	pm_runtime_set_autosuspend_delay(pfdev->base.dev, 50); /* ~3 frames */
	pm_runtime_use_autosuspend(pfdev->base.dev);

And then in the error and device remove path would be almost just like you suggest.

Quoted 8 lines
        pm_runtime_dont_use_autosuspend(pfdev->base.dev);
	pm_runtime_disable(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);
	panfrost_device_fini(pfdev);
	pm_runtime_set_suspended(pfdev->base.dev);

The only difference is that pm_runtime_put_noidle() happens before device_fini because that's the same order as in __pm_runtime_suspend().

I really think we should keep the init/fini order consisten, so put_noidle() should be after the _fini() calls. But as I said above, I'd probably have get_noresume() called after all the _init() functions instead, since we can afford it in Panfrost, and it helps keeping all the PM-related calls in just one block.

Quoted 4 lines

However, that means in the commit where I move all DRM initialisation into panfrost_device_init(), in order to keep things consistent, I should move pm_runtime_set_suspended() to the very end of the error path.

Quoted 5 lines

It probably doesn't matter much actually, because as soon as pm_runtime_disable() is called, nothing can change the RPM state anymore. So maybe scratch all of my comments, and we stick to what you had. It's just a transition state anyway, so, as long as it doesn't regress the probe/remove logic, it's probably fine.

I think I'll leave it the way it is and then add a comment that explains why the manual adjustment of PM references and status.

Quoted 25 lines
Quoted 25 lines
Quoted 24 lines

drivers/gpu/drm/panfrost/panfrost_drv.c static void panfrost_remove(struct platform_device *pdev) · line 893

Quoted 11 lines
drivers/gpu/drm/panfrost/panfrost_drv.c +2 -0
 err_out0:
 	return err;
 }
@@ -884,9 +888,11 @@ static void panfrost_remove(struct platform_device *pdev)
 	drm_dev_unregister(&pfdev->base);
 
 	pm_runtime_get_sync(pfdev->base.dev);
+	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);  

Let's keep the order consistent with the probe path:

	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
	pm_runtime_disable(pfdev->base.dev);
	panfrost_device_fini(pfdev);
	pm_runtime_set_suspended(pfdev->base.dev);
	pm_runtime_put_noidle(pfdev->base.dev);

I also think this deserves comments to explain the noresume/noidle dance (device_init/fini take care of clks internally, and when they return the device is resumed/suspended, so all we have to is update the state, and acquire a ref).

Quoted 6 lines
Quoted 4 lines

I don't mind doing this, but I think the fact we're calling noresume/noidle functions already implies device power-up is being handled manually rather by having RPM invoke resume and suspend callbacks behind the scenes, so we've no choice other than adjusting PM user counter and status ourselves.

Fair enough.

Adrian Larumbe

author maintainer
#41 lore raw

Re: [PATCH v9 15/16] drm/panfrost: Fix races between perfcnt and reset sequence

In reply to Boris Brezillon
Quoted 35 lines · Boris Brezillon
Quoted 32 lines · On Wed, 23 Sep 2026 02:39:10 +0100 Adrián Larumbe…
Quoted 31 lines
Quoted 19 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +6 -10
-static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
+static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev, u32 *state)
 {
-	u64 gpuva;
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	u64 gpuva = perfcnt->mapping->mmnode.start << PAGE_SHIFT;
 	int ret;
 
-	reinit_completion(&pfdev->perfcnt->dump_comp);
-	gpuva = pfdev->perfcnt->mapping->mmnode.start << PAGE_SHIFT;
-	gpu_write(pfdev, GPU_PERFCNT_BASE_LO, lower_32_bits(gpuva));
-	gpu_write(pfdev, GPU_PERFCNT_BASE_HI, upper_32_bits(gpuva));
-	gpu_write(pfdev, GPU_INT_CLEAR,
-		  GPU_IRQ_CLEAN_CACHES_COMPLETED |
-		  GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
-	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_SAMPLE);
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		perfcnt->dump_finished = false;
+		*state = 0;

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) · line 129

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+
+		if (!perfcnt->owns_as_ref) {
+			*state = PANFROST_PERFCNT_SESSION_DEAD;
+			return -EIO;
+		}
+
+		if (perfcnt->reset_happened) {
+			*state = PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
+			perfcnt->reset_happened = false;
+		}  
		*state = perfcnt->state;  
Quoted 8 lines
Quoted 4 lines

I'm thinking maybe we don't need to set the ioctl output state here. Even if there was a reset, if we recovered from it and didn't lose the AS reference, we might be able to go forward as usual. In that case, counters would be measured since the latest reset rather than the last successful dump, although maybe this is not an issue.

I think userspace needs to know about INTERRUPTED_BY_RESET, at the very least. But honestly, I'd keep it simple and propagate the state regardless of the flags userspace might or might not care about.

Agreed.

Quoted 30 lines
Quoted 30 lines
Quoted 29 lines
Quoted 16 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -4
+	/*
+	 * Here we release the reset semaphore because perfcnt should not get in the way
+	 * of a HW reset. Besides, a legitimate reset might be issued during the wait.
+	 */
 	ret = wait_for_completion_interruptible_timeout(&pfdev->perfcnt->dump_comp,
 							msecs_to_jiffies(1000));
-	if (!ret)
-		ret = -ETIMEDOUT;
-	else if (ret > 0)
-		ret = 0;
+
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		/* Either sample finished or reset happened */
+		if (ret > 0) {
+			ret = perfcnt->dump_finished ? 0 :
+			      perfcnt->owns_as_ref ? -EAGAIN : -EIO;

drivers/gpu/drm/panfrost/panfrost_perfcnt.c static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) · line 161

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+
+		} else if (!ret) {
+			ret = -ETIMEDOUT;
+		}
+
+		if (perfcnt->reset_happened)
+			*state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
+		if (!perfcnt->owns_as_ref)
+			*state |= PANFROST_PERFCNT_SESSION_DEAD;
+	}  
	if (!ret)
		return -ETIMEDOUT;  
Quoted 8 lines
Quoted 2 lines

I was doing this check inside the guard because, in the time between the completion is signalled and the semaphore locked, another reset could come right through.

I wouldn't worry about late signaling to be honest, the timeout is 1sec, which is way more than I would expect a DUMP request to take in a normal situation. Now, we might want to propagate the state in that case too, so it might make sense to return ETIMEDOUT inside the scoped_guard, after the state has been copied.

On a second thought, I agree with you. Not only is the completion timeout much longer than the usual sample period, but also, in the event of a timeout (and no counters being copied back to UM), I don't think a perfcnt user would care much about the reset state.

Quoted 37 lines
Quoted 7 lines

And then because I believed counter values were accumulated rather than reset between consecutive dumps, it was best to notify the user as soon as possible, even if that meant losing one legitimate frame.

Quoted 3 lines
	scoped_guard(rwsem_read, &pfdev->reset.lock) {
		u32 new_state = perfcnt->state;
Quoted 8 lines
Quoted 5 lines
		*state |= new_state;
		if (new_state & PANFROST_PERFCNT_SESSION_DEAD)
			return -EIO;

		perfcnt->state = 0;  

I think this should not be here, because we're already resetting the state before running the PERFCNT_SAMPLE GPU command, and also we need to check it right below.

I think we want to reset in both places, especially if we propagate the state before the DUMP too, otherwise we might leave the INTERRUPTED_BY_RESET bit behind even though we already informed userspace about this event in the previous DUMP request. And yes, the following test should have been on new_state instead of perfcnt->state, indeed.

Quoted 6 lines
Quoted 5 lines
		/* If we faced a reset during our SAMPLE, the user needs to try again. */
		if (perfcnt->state & PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET)
			return -EAGAIN;
	}  
Quoted 8 lines

The main reason I added the panfrost_perfcnt::dump_finished flag was that when a reset happens, the GPU IRQ handler might've been already running in response to a finished perfcnt sample command. However, the reset sequence might come off first and cancel the completion, leaving us with a ret > 0 despite sampling having succeeded.

I'm thinking rather than trying to sort of patch this up precariously as I did in this revision, I'll leave it the way you suggested and address the race between the GPU IRQ handler and the reset sequence in a future series.

Quoted 10 lines

Yes, that's exactly what needs to be done: reset should synchronize IRQs, mask/suspend them while the reset sequence is happening, and unmask them when it's done. As for the completion of a DUMP request interrupted by a RESET, we want a ret > 0 (AKA no-timeout) in that case, but the perfcnt::state should be updated prior to that, so that this function knows exactly what to expect from this completion: if SESSION_INTERRUPTED_BY_RESET is set, it's likely that the DUMP didn't complete which is why I return EAGAIN in that case, but we can also have a dedicated flag for DUMP_COMPLETE if you want to be accurate (when set, DUMP is effective, when not set EAGAIN).

I'll leave dealing with the problem of synchronisation between reset and the IRQ handlers to a later patch series, so in the meantime, SESSION_INTERRUPTED_BY_RESET should always lead to EAGAIN being returned.

Quoted 28 lines
Quoted 5 lines
Quoted 2 lines
	return 0;  

We still need to return -ERESTARTSYS when wait_for_completion_interruptible_timeout() is interrupted from UM.

Oh, absolutely.

Quoted 20 lines
Quoted 19 lines
Quoted 6 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +5 -0
+void panfrost_perfcnt_reset(struct panfrost_device *pfdev)
+{
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+
+	if (drm_WARN_ON(&pfdev->base, !perfcnt))

drivers/gpu/drm/panfrost/panfrost_perfcnt.c void panfrost_perfcnt_fini(struct panfrost_device *pfdev) · line 420

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+		return;
+
+	lockdep_assert_held(&pfdev->reset.lock);
+
+	if (!perfcnt->user)
+		return;
+
+	perfcnt->owns_as_ref = !panfrost_perfcnt_hw_enable(pfdev);
+	perfcnt->reset_happened = true;
+	complete(&perfcnt->dump_comp);  
	/* All active AS are released during the MMU post_reset. */
	perfcnt->owns_as_ref = false;  
Quoted 6 lines
Quoted 1 line

I'm guessing this should go away as soon as I move it into panfrost_perfcnt_hw_enable().

If you reset it at the beginning of panfrost_perfcnt_hw_enable(), sure, but I think it's easier to assume that when panfrost_perfcnt_hw_enable() is called, the AS is not owned, and reset it here instead.

Noted.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
	perfcnt->state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
	if (panfrost_perfcnt_hw_enable(pfdev))
		perfcnt->state |= PANFROST_PERFCNT_SESSION_DEAD;

	/* Unblock pending sample requests. */
	complete(&perfcnt->dump_comp);  
Quoted 1 line

+}

Adrian Larumbe