Skip to contentSkip to conversation

Collection of fixes for Panfrost: Perfcnt, RPM, refactorings

PATCH Superseded Adrián Larumbe 4 participants 51 messages Sep 29 – Oct 7

Subsystems (3)

v12 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Adrián Larumbe

author maintainer
#0 lore raw

v12 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 v12:
- Fixed perfcnt and driver init bugs uncovered by Sashiko in v11.
- Fixed stale commit message for PM fixes.
- Link to v11: https://patch.msgid.link/[email protected]

Changes in v11:
- Fixed bugs about user_count and PM device status at driver init and remove time.
- Documented new uAPI for perfcnt dump ioctl that depends on reset status.
- Fixed issues about internal perfcnt bookkeeping in case of reset.
- Link to v10: https://patch.msgid.link/[email protected]

Changes in v10:
- Improved the logic for the debugfs reset knob.
- Changed perfcnt reset race commit message to reflect true HW behaviour.
- Improved logic of perfcnt reset handling with a state variable instead of booleans.
- Added comments explaning how we handle PM status and references at device init time.
- Fix RM resume clock disable bug uncovered by Sashiko and also changed function naming.
- Link to v9: https://patch.msgid.link/[email protected]

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 (15):
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

 drivers/gpu/drm/panfrost/panfrost_device.c       | 239 +++++++++++------
 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          | 103 +++++++-
 drivers/gpu/drm/panfrost/panfrost_job.h          |   4 +
 drivers/gpu/drm/panfrost/panfrost_mmu.c          |  14 +-
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c      | 317 +++++++++++++++--------
 drivers/gpu/drm/panfrost/panfrost_perfcnt.h      |   1 +
 include/uapi/drm/panfrost_drm.h                  |  29 ++-
 14 files changed, 590 insertions(+), 414 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 v12 1/15 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
maintainer Reviewed-by on v12 1/15
#2 lore raw

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

Quoted 11 lines · 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.

Quoted 157 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 +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);
 }
author maintainer
#3 lore raw

PATCH v12 2/15 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
maintainer Reviewed-by on v12 2/15
#4 lore raw

Re: [PATCH v12 02/15] drm/panfrost: Move lock and modparam initialisations into their subsystems

Quoted 5 lines · 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.

Quoted 8 lines

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.

Quoted 171 lines
---
 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;
author maintainer
#5 lore raw

PATCH v12 3/15 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
maintainer Reviewed-by on v12 3/15
#6 lore raw

Re: [PATCH v12 03/15] drm/panfrost: Move debugfs initialisation to relevant subsystems

Quoted 22 lines · 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 +0 -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)

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_device.c +8 -0
 	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

NIT: I'd usually try to avoid #ifdefs like this by making the header files define a no-op dummy inline function when !CONFIG_DEBUG_FS. It saves polluting the code with a potentially large number of #ifdefs.

But I have to admit this isn't too bad and a definite improvement, so it's up to you - either way:

Thanks,
Steve

Quoted 354 lines
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
index 59af79a534fa..55fc22e8d4d4 100644
@@ -792,133 +792,6 @@ static const struct file_operations panfrost_drm_driver_fops = {
 	.show_fdinfo = drm_show_fdinfo,
 };
 
-#ifdef CONFIG_DEBUG_FS
-static int panthor_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 void show_panfrost_jm_ctx(struct panfrost_jm_ctx *jm_ctx, u32 handle,
-				 struct seq_file *m)
-{
-	struct drm_device *ddev = ((struct drm_info_node *)m->private)->minor->dev;
-	const char *prio = "UNKNOWN";
-
-	static const char * const prios[] = {
-		[DRM_SCHED_PRIORITY_HIGH] = "HIGH",
-		[DRM_SCHED_PRIORITY_NORMAL] = "NORMAL",
-		[DRM_SCHED_PRIORITY_LOW] = "LOW",
-	};
-
-	if (jm_ctx->slot_entity[0].priority !=
-	    jm_ctx->slot_entity[1].priority)
-		drm_warn(ddev, "Slot priorities should be the same in a single context");
-
-	if (jm_ctx->slot_entity[0].priority < ARRAY_SIZE(prios))
-		prio = prios[jm_ctx->slot_entity[0].priority];
-
-	seq_printf(m, " JM context %u: priority %s\n", handle, prio);
-}
-
-static int show_file_jm_ctxs(struct panfrost_file_priv *pfile,
-			     struct seq_file *m)
-{
-	struct panfrost_jm_ctx *jm_ctx;
-	unsigned long i;
-
-	xa_lock(&pfile->jm_ctxs);
-	xa_for_each(&pfile->jm_ctxs, i, jm_ctx) {
-		jm_ctx = panfrost_jm_ctx_get(jm_ctx);
-		xa_unlock(&pfile->jm_ctxs);
-		show_panfrost_jm_ctx(jm_ctx, i, m);
-		panfrost_jm_ctx_put(jm_ctx);
-		xa_lock(&pfile->jm_ctxs);
-	}
-	xa_unlock(&pfile->jm_ctxs);
-
-	return 0;
-}
-
-static struct drm_info_list panthor_debugfs_list[] = {
-	{"gems",
-	 panthor_gems_show, 0, NULL},
-};
-
-static int panthor_gems_debugfs_init(struct drm_minor *minor)
-{
-	drm_debugfs_create_files(panthor_debugfs_list,
-				 ARRAY_SIZE(panthor_debugfs_list),
-				 minor->debugfs_root, minor);
-
-	return 0;
-}
-
-static int show_each_file(struct seq_file *m, void *arg)
-{
-	struct drm_info_node *node = (struct drm_info_node *)m->private;
-	struct drm_device *ddev = node->minor->dev;
-	int (*show)(struct panfrost_file_priv *, struct seq_file *) =
-		node->info_ent->data;
-	struct drm_file *file;
-	int ret;
-
-	ret = mutex_lock_interruptible(&ddev->filelist_mutex);
-	if (ret)
-		return ret;
-
-	list_for_each_entry(file, &ddev->filelist, lhead) {
-		struct task_struct *task;
-		struct panfrost_file_priv *pfile = file->driver_priv;
-		struct pid *pid;
-
-		/*
-		 * Although we have a valid reference on file->pid, that does
-		 * not guarantee that the task_struct who called get_pid() is
-		 * still alive (e.g. get_pid(current) => fork() => exit()).
-		 * Therefore, we need to protect this ->comm access using RCU.
-		 */
-		rcu_read_lock();
-		pid = rcu_dereference(file->pid);
-		task = pid_task(pid, PIDTYPE_TGID);
-		seq_printf(m, "client_id %8llu pid %8d command %s:\n",
-			   file->client_id, pid_nr(pid),
-			   task ? task->comm : "<unknown>");
-		rcu_read_unlock();
-
-		ret = show(pfile, m);
-		if (ret < 0)
-			break;
-
-		seq_puts(m, "\n");
-	}
-
-	mutex_unlock(&ddev->filelist_mutex);
-	return ret;
-}
-
-static struct drm_info_list panfrost_sched_debugfs_list[] = {
-	{ "sched_ctxs", show_each_file, 0, show_file_jm_ctxs },
-};
-
-static void panfrost_sched_debugfs_init(struct drm_minor *minor)
-{
-	drm_debugfs_create_files(panfrost_sched_debugfs_list,
-				 ARRAY_SIZE(panfrost_sched_debugfs_list),
-				 minor->debugfs_root, minor);
-}
-
-static void panfrost_debugfs_init(struct drm_minor *minor)
-{
-	panthor_gems_debugfs_init(minor);
-	panfrost_sched_debugfs_init(minor);
-}
-#endif
-
 /*
  * Panfrost driver version:
  * - 1.0 - initial interface
@@ -950,7 +823,7 @@ static const struct drm_driver panfrost_drm_driver = {
 	.gem_prime_import	= panfrost_gem_prime_import,
 	.gem_prime_import_sg_table = panfrost_gem_prime_import_sg_table,
 #ifdef CONFIG_DEBUG_FS
-	.debugfs_init = panfrost_debugfs_init,
+	.debugfs_init = panfrost_device_debugfs_init,
 #endif
 };
 
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
index 476c936b54e6..00fb36c577df 100644
@@ -8,6 +8,8 @@
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/dma-resv.h>
+#include <drm/drm_debugfs.h>
+#include <drm/drm_print.h>
 #include <drm/gpu_scheduler.h>
 #include <drm/panfrost_drm.h>
 
@@ -1138,3 +1140,100 @@ int panfrost_jm_ctx_destroy(struct drm_file *file, u32 handle)
 	panfrost_jm_ctx_put(jm_ctx);
 	return 0;
 }
+
+#ifdef CONFIG_DEBUG_FS
+static void show_panfrost_jm_ctx(struct panfrost_jm_ctx *jm_ctx, u32 handle,
+				 struct seq_file *m)
+{
+	struct drm_device *ddev = ((struct drm_info_node *)m->private)->minor->dev;
+	const char *prio = "UNKNOWN";
+
+	static const char * const prios[] = {
+		[DRM_SCHED_PRIORITY_HIGH] = "HIGH",
+		[DRM_SCHED_PRIORITY_NORMAL] = "NORMAL",
+		[DRM_SCHED_PRIORITY_LOW] = "LOW",
+	};
+
+	if (jm_ctx->slot_entity[0].priority !=
+	    jm_ctx->slot_entity[1].priority)
+		drm_warn(ddev, "Slot priorities should be the same in a single context");
+
+	if (jm_ctx->slot_entity[0].priority < ARRAY_SIZE(prios))
+		prio = prios[jm_ctx->slot_entity[0].priority];
+
+	seq_printf(m, " JM context %u: priority %s\n", handle, prio);
+}
+
+static int show_file_jm_ctxs(struct panfrost_file_priv *pfile,
+			     struct seq_file *m)
+{
+	struct panfrost_jm_ctx *jm_ctx;
+	unsigned long i;
+
+	xa_lock(&pfile->jm_ctxs);
+	xa_for_each(&pfile->jm_ctxs, i, jm_ctx) {
+		jm_ctx = panfrost_jm_ctx_get(jm_ctx);
+		xa_unlock(&pfile->jm_ctxs);
+		show_panfrost_jm_ctx(jm_ctx, i, m);
+		panfrost_jm_ctx_put(jm_ctx);
+		xa_lock(&pfile->jm_ctxs);
+	}
+	xa_unlock(&pfile->jm_ctxs);
+
+	return 0;
+}
+
+static int show_each_file(struct seq_file *m, void *arg)
+{
+	struct drm_info_node *node = (struct drm_info_node *)m->private;
+	struct drm_device *ddev = node->minor->dev;
+	int (*show)(struct panfrost_file_priv *, struct seq_file *) =
+		node->info_ent->data;
+	struct drm_file *file;
+	int ret;
+
+	ret = mutex_lock_interruptible(&ddev->filelist_mutex);
+	if (ret)
+		return ret;
+
+	list_for_each_entry(file, &ddev->filelist, lhead) {
+		struct task_struct *task;
+		struct panfrost_file_priv *pfile = file->driver_priv;
+		struct pid *pid;
+
+		/*
+		 * Although we have a valid reference on file->pid, that does
+		 * not guarantee that the task_struct who called get_pid() is
+		 * still alive (e.g. get_pid(current) => fork() => exit()).
+		 * Therefore, we need to protect this ->comm access using RCU.
+		 */
+		rcu_read_lock();
+		pid = rcu_dereference(file->pid);
+		task = pid_task(pid, PIDTYPE_TGID);
+		seq_printf(m, "client_id %8llu pid %8d command %s:\n",
+			   file->client_id, pid_nr(pid),
+			   task ? task->comm : "<unknown>");
+		rcu_read_unlock();
+
+		ret = show(pfile, m);
+		if (ret < 0)
+			break;
+
+		seq_puts(m, "\n");
+	}
+
+	mutex_unlock(&ddev->filelist_mutex);
+	return ret;
+}
+
+static struct drm_info_list panfrost_sched_debugfs_list[] = {
+	{ "sched_ctxs", show_each_file, 0, show_file_jm_ctxs },
+};
+
+void panfrost_sched_debugfs_init(struct drm_minor *minor)
+{
+	drm_debugfs_create_files(panfrost_sched_debugfs_list,
+				 ARRAY_SIZE(panfrost_sched_debugfs_list),
+				 minor->debugfs_root, minor);
+}
+#endif
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
author maintainer
#7 lore raw

Re: [PATCH v12 03/15] drm/panfrost: Move debugfs initialisation to relevant subsystems

Quoted 39 lines · Steven Price
Quoted 22 lines · 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 +0 -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)

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_device.c +8 -0
 	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

NIT: I'd usually try to avoid #ifdefs like this by making the header files define a no-op dummy inline function when !CONFIG_DEBUG_FS. It saves polluting the code with a potentially large number of #ifdefs.

Quoted 7 lines

But I have to admit this isn't too bad and a definite improvement, so it's up to you - either way:

Thanks,
Steve

I see most libraries doing this and agree it would help at removing some clutter. However, I think I'd rather take care of this in a future patch series.

author maintainer
#8 lore raw

PATCH v12 4/15 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
maintainer Reviewed-by on v12 4/15
#9 lore raw

Re: [PATCH v12 04/15] drm/panfrost: Skip NULL checks for clock enable/disabling

Quoted 5 lines · Adrián Larumbe

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

Quoted 44 lines
---
 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);
 	}
 
author maintainer
#10 lore raw

PATCH v12 5/15 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..9f2b1967a398 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_clks_enable(struct panfrost_device *pfdev, bool on_resume)
 {
 	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 (on_resume) {
+		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_clks_disable(struct panfrost_device *pfdev, bool on_suspend)
+{
+	if (on_suspend)
+		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_clks_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_clks_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_clks_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_clks_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_clks_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);
+		panfrost_clks_disable(pfdev, false);
 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_clks_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
#11 lore raw

Re: [PATCH v12 05/15] drm/panfrost: Consolidate device clock management and reset

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] clk: slow clk_prepare on runtime resume due to unprepare in suspend --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] clk: slow clk_prepare on runtime resume due to unprepare in suspend --

commit 9d5414aa4af3f7dd6f34ed2bea83b5c9b25db769
Author: Adrián Larumbe <[email protected]>
Subject: drm/panfrost: Consolidate device clock management and reset

This commit gathers clock enables and disables into a single function to avoid repetition between driver init/fini and device resume/suspend. It also combines prepare/unprepare with enable/disable, under the assumption that clock preparation can simply increase the reference count of an already prepared clock.

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

[ ... ]

Quoted 23 lines
drivers/gpu/drm/panfrost/panfrost_device.c +5 -11
@@ -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_clks_enable(pfdev, true);
+		if (ret) {
+			reset_control_assert(pfdev->rstc);
+			return ret;
+		}
 	}
 
 	panfrost_device_reset(pfdev, true);

[ ... ]

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -3
@@ -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_clks_disable(pfdev, true);
 		reset_control_assert(pfdev->rstc);
 	}

[Severity: Medium]
Does this introduce a performance regression by fully unpreparing the clocks on every idle state?

Previously, this code only called clk_disable() during runtime suspend, leaving the clock prepared. Now panfrost_clks_disable() uses clk_disable_unprepare(), reducing the prepare refcount to 0.

When the device wakes up, panfrost_device_runtime_resume() calls panfrost_clks_enable() which uses clk_prepare_enable(). Because the clock was unprepared here in suspend, the resume path will pay the penalty of executing the full sleep-capable prepare sequence from a cold state instead of just a fast atomic-safe enable.

The commit message notes that "the clk_prepare_* family of functions can simply increase the refcnt of an already prepared clock". However, since the clocks are fully unprepared in this runtime suspend path, they won't be already prepared on resume.

Was it intended to fully unprepare the clocks on every runtime suspend?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer Reviewed-by on v12 5/15
#12 lore raw

Re: [PATCH v12 05/15] drm/panfrost: Consolidate device clock management and reset

In reply to Adrián Larumbe
Quoted 10 lines · 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.

Quoted 207 lines
---
 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..9f2b1967a398 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_clks_enable(struct panfrost_device *pfdev, bool on_resume)
 {
 	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 (on_resume) {
+		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_clks_disable(struct panfrost_device *pfdev, bool on_suspend)
+{
+	if (on_suspend)
+		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_clks_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_clks_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_clks_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_clks_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_clks_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);
+		panfrost_clks_disable(pfdev, false);
 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_clks_disable(pfdev, false);
 
 	if (pfdev->comp->pm_features & BIT(GPU_PM_VREG_OFF))
 		dev_pm_opp_set_opp(dev, NULL);
author maintainer
#13 lore raw

PATCH v12 6/15 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

Fixes 635430797d3f ("drm/panfrost: Rework runtime PM initialization")
Fixes 876b15d2c88d ("drm/panfrost: Fix module unload")
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/gpu/drm/panfrost/panfrost_drv.c +11 -0
index 55fc22e8d4d4..907d4a14a0b5 100644
@@ -852,6 +852,11 @@ static int panfrost_probe(struct platform_device *pdev)
 		goto err_out0;
 	}
 
+	/* The reason we must manually set the PM status and usage counter is
+	 * we have just powered the device up but did not go through the PM
+	 * runtime resume callback, so we need to update these ourselves.
+	 */
+	pm_runtime_get_noresume(pfdev->base.dev);
 	pm_runtime_set_active(pfdev->base.dev);
 	pm_runtime_mark_last_busy(pfdev->base.dev);
 	pm_runtime_enable(pfdev->base.dev);
@@ -866,13 +871,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_set_suspended(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 err_out0:
 	return err;
 }
@@ -884,9 +892,12 @@ 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_disable(pfdev->base.dev);
 	panfrost_device_fini(pfdev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
+
 }
 
 static ssize_t profiling_show(struct device *dev,
Signature · 1 line
2.55.0
maintainer Reviewed-by on v12 6/15
#14 lore raw

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

Quoted 19 lines · 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.

Quoted 8 lines

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

Fixes 635430797d3f ("drm/panfrost: Rework runtime PM initialization")
Fixes 876b15d2c88d ("drm/panfrost: Fix module unload")
Quoted 51 lines
---
 drivers/gpu/drm/panfrost/panfrost_drv.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/gpu/drm/panfrost/panfrost_drv.c +11 -0
index 55fc22e8d4d4..907d4a14a0b5 100644
@@ -852,6 +852,11 @@ static int panfrost_probe(struct platform_device *pdev)
 		goto err_out0;
 	}
 
+	/* The reason we must manually set the PM status and usage counter is
+	 * we have just powered the device up but did not go through the PM
+	 * runtime resume callback, so we need to update these ourselves.
+	 */
+	pm_runtime_get_noresume(pfdev->base.dev);
 	pm_runtime_set_active(pfdev->base.dev);
 	pm_runtime_mark_last_busy(pfdev->base.dev);
 	pm_runtime_enable(pfdev->base.dev);
@@ -866,13 +871,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_set_suspended(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 err_out0:
 	return err;
 }
@@ -884,9 +892,12 @@ 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_disable(pfdev->base.dev);
 	panfrost_device_fini(pfdev);
 	pm_runtime_set_suspended(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
+
 }
 
 static ssize_t profiling_show(struct device *dev,
author maintainer
#15 lore raw

PATCH v12 7/15 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
maintainer Reviewed-by on v12 7/15
#16 lore raw

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

Quoted 9 lines · 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")
Quoted 41 lines
---
 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;
 }
 
author maintainer
#17 lore raw

PATCH v12 8/15 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 | 49 ++++++++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_drv.c    | 52 +-----------------------------
 2 files changed, 50 insertions(+), 51 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +49 -0
index 9f2b1967a398..c6bf3d0663df 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"
@@ -228,8 +229,15 @@ static int panfrost_pm_domain_init(struct panfrost_device *pfdev)
 
 int panfrost_device_init(struct panfrost_device *pfdev)
 {
+	bool device_initialised = false;
 	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 +299,35 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	if (err)
 		goto out_perfcnt;
 
+	device_initialised = true;
+
+	/* The reason we must manually set the PM status and usage counter is
+	 * we have just powered the device up but did not go through the PM
+	 * runtime resume callback, so we need to update these ourselves.
+	 */
+	pm_runtime_get_noresume(pfdev->base.dev);
+	pm_runtime_set_active(pfdev->base.dev);
+	pm_runtime_mark_last_busy(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);
+	panfrost_gem_fini(pfdev);
 out_perfcnt:
 	panfrost_perfcnt_fini(pfdev);
 out_job:
@@ -311,11 +346,22 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	panfrost_reset_fini(pfdev);
 out_pm_domain:
 	panfrost_pm_domain_fini(pfdev);
+
+	if (device_initialised) {
+		pm_runtime_set_suspended(pfdev->base.dev);
+		pm_runtime_put_noidle(pfdev->base.dev);
+	}
+
 	return err;
 }
 
 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);
+
 	panfrost_gem_fini(pfdev);
 	panfrost_perfcnt_fini(pfdev);
 	panfrost_jm_fini(pfdev);
@@ -326,6 +372,9 @@ void panfrost_device_fini(struct panfrost_device *pfdev)
 	panfrost_clk_fini(pfdev);
 	panfrost_reset_fini(pfdev);
 	panfrost_pm_domain_fini(pfdev);
+
+	pm_runtime_set_suspended(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 }
 
 #define PANFROST_EXCEPTION(id) \
drivers/gpu/drm/panfrost/panfrost_drv.c +1 -51

71 lines not on this page · raw message

Signature · 1 line
2.55.0
maintainer Reviewed-by on v12 8/15
#18 lore raw

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

Quoted 10 lines · 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 181 lines
---
 drivers/gpu/drm/panfrost/panfrost_device.c | 49 ++++++++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_drv.c    | 52 +-----------------------------
 2 files changed, 50 insertions(+), 51 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +49 -0
index 9f2b1967a398..c6bf3d0663df 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"
@@ -228,8 +229,15 @@ static int panfrost_pm_domain_init(struct panfrost_device *pfdev)
 
 int panfrost_device_init(struct panfrost_device *pfdev)
 {
+	bool device_initialised = false;
 	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 +299,35 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	if (err)
 		goto out_perfcnt;
 
+	device_initialised = true;
+
+	/* The reason we must manually set the PM status and usage counter is
+	 * we have just powered the device up but did not go through the PM
+	 * runtime resume callback, so we need to update these ourselves.
+	 */
+	pm_runtime_get_noresume(pfdev->base.dev);
+	pm_runtime_set_active(pfdev->base.dev);
+	pm_runtime_mark_last_busy(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);
+	panfrost_gem_fini(pfdev);
 out_perfcnt:
 	panfrost_perfcnt_fini(pfdev);
 out_job:
@@ -311,11 +346,22 @@ int panfrost_device_init(struct panfrost_device *pfdev)
 	panfrost_reset_fini(pfdev);
 out_pm_domain:
 	panfrost_pm_domain_fini(pfdev);
+
+	if (device_initialised) {
+		pm_runtime_set_suspended(pfdev->base.dev);
+		pm_runtime_put_noidle(pfdev->base.dev);
+	}
+
 	return err;
 }
 
 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);
+
 	panfrost_gem_fini(pfdev);
 	panfrost_perfcnt_fini(pfdev);
 	panfrost_jm_fini(pfdev);
@@ -326,6 +372,9 @@ void panfrost_device_fini(struct panfrost_device *pfdev)
 	panfrost_clk_fini(pfdev);
 	panfrost_reset_fini(pfdev);
 	panfrost_pm_domain_fini(pfdev);
+
+	pm_runtime_set_suspended(pfdev->base.dev);
+	pm_runtime_put_noidle(pfdev->base.dev);
 }
 
 #define PANFROST_EXCEPTION(id) \
drivers/gpu/drm/panfrost/panfrost_drv.c +1 -51
index 907d4a14a0b5..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,50 +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;
-	}
-
-	/* The reason we must manually set the PM status and usage counter is
-	 * we have just powered the device up but did not go through the PM
-	 * runtime resume callback, so we need to update these ourselves.
-	 */
-	pm_runtime_get_noresume(pfdev->base.dev);
-	pm_runtime_set_active(pfdev->base.dev);
-	pm_runtime_mark_last_busy(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);
-	panfrost_device_fini(pfdev);
-	pm_runtime_set_suspended(pfdev->base.dev);
-	pm_runtime_put_noidle(pfdev->base.dev);
-err_out0:
-	return err;
+	return panfrost_device_init(pfdev);
 }
 
 static void panfrost_remove(struct platform_device *pdev)
@@ -891,13 +847,7 @@ 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_disable(pfdev->base.dev);
 	panfrost_device_fini(pfdev);
-	pm_runtime_set_suspended(pfdev->base.dev);
-	pm_runtime_put_noidle(pfdev->base.dev);
-
 }
 
 static ssize_t profiling_show(struct device *dev,
author maintainer
#19 lore raw

PATCH v12 9/15 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 c6bf3d0663df..09a5752a3f40 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"
@@ -357,7 +358,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);
@@ -516,7 +517,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
maintainer
#20 lore raw

Re: [PATCH v12 09/15] drm/panfrost: Add warning messages to fatal error conditions

Quoted 23 lines · 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 +1 -0
index c6bf3d0663df..09a5752a3f40 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"

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

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
@@ -357,7 +358,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);

This seems fine.

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

Quoted 9 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
 	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
@@ -516,7 +517,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)))

I'm a bit wary that this might be something that user space can trigger. My AI says:

The runtime-suspend WARN can be reached by ordinary userspace job submissions. The DRM scheduler increments credit_count before calling Panfrost’s job runner (drivers/gpu/drm/scheduler/sched_main.c:1044). Panfrost takes the job’s PM reference later in hardware submission (drivers/gpu/drm/panfrost/panfrost_job.c:213). If autosuspend runs in that interval, the new WARN (drivers/gpu/drm/panfrost/panfrost_device.c:525) sees the credit and fires, even though this is a timing race rather than a broken job. Repeated submissions near the autosuspend boundary could therefore produce repeated stack traces. The PM core treats the resulting -EBUSY as a transient failure.

Now I have to admit I don't trust it that much - but I'd want a convincing argument on why panfrost_jm_is_idle() will never be false here.

Thanks,
Steve

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_device.c +0 -0
 		return -EBUSY;
 
 	panfrost_devfreq_suspend(pfdev);
author maintainer
#21 lore raw

Re: [PATCH v12 09/15] drm/panfrost: Add warning messages to fatal error conditions

Quoted 57 lines · Steven Price
Quoted 23 lines · 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 +1 -0
index c6bf3d0663df..09a5752a3f40 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"

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

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
@@ -357,7 +358,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);

This seems fine.

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

Quoted 8 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
 	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
@@ -516,7 +517,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)))

I'm a bit wary that this might be something that user space can trigger. My AI says:

The runtime-suspend WARN can be reached by ordinary userspace job submissions. The DRM scheduler increments credit_count before calling Panfrost’s job runner (drivers/gpu/drm/scheduler/sched_main.c:1044). Panfrost takes the job’s PM reference later in hardware submission (drivers/gpu/drm/panfrost/panfrost_job.c:213). If autosuspend runs in that interval, the new WARN (drivers/gpu/drm/panfrost/panfrost_device.c:525) sees the credit and fires, even though this is a timing race rather than a broken job. Repeated submissions near the autosuspend boundary could therefore produce repeated stack traces. The PM core treats the resulting -EBUSY as a transient failure.

Quoted 2 lines

Now I have to admit I don't trust it that much - but I'd want a convincing argument on why panfrost_jm_is_idle() will never be false here.

You're right. I was in the belief that autosuspend kicking in was proof of no inflight or pending jobs present in the scheduler queues, so I came to treat this check as things having gone awry.

I guess its value lies in the ability of the PM runtime suspend handler to cancel itself at an autosuspend event, like you said.

However, it just made me wonder: what would happen in the event that autosuspend kicks in and runs panfrost_device_runtime_suspend() right at the same time that a scheduler job is picked up by drm_sched_run_job_work(), but hasn't yet reached the statement where it does an atomic increment on the credit_count? I guess nothing, because panfrost_job_hw_submit() is getting a PM reference before accessing any HW registers, and that should take care of dealing with any ongoing autosuspend events.

In that case I'll just delete that warning. However, I'd say it's bad practice to have DRM drivers access the internal state of the DRM scheduler. At present, only Panfrost and Etnaviv poke it in their RPM suspend handlers, and I've been wondering whether we should get rid of this check altogether, or else maybe ask the scheduler maintainers whether it makes sense to have a non-racy way to query the presence of pending jobs in their queues?

Quoted 2 lines

Thanks,
Steve

maintainer
#22 lore raw

Re: [PATCH v12 09/15] drm/panfrost: Add warning messages to fatal error conditions

Quoted 76 lines · Adrián Larumbe
Quoted 57 lines · Steven Price
Quoted 23 lines · 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 +1 -0
index c6bf3d0663df..09a5752a3f40 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"

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

Quoted 5 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
@@ -357,7 +358,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);

This seems fine.

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

Quoted 8 lines
drivers/gpu/drm/panfrost/panfrost_device.c +1 -1
 	pm_runtime_dont_use_autosuspend(pfdev->base.dev);
 	pm_runtime_disable(pfdev->base.dev);
@@ -516,7 +517,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)))

I'm a bit wary that this might be something that user space can trigger. My AI says:

The runtime-suspend WARN can be reached by ordinary userspace job submissions. The DRM scheduler increments credit_count before calling Panfrost’s job runner (drivers/gpu/drm/scheduler/sched_main.c:1044). Panfrost takes the job’s PM reference later in hardware submission (drivers/gpu/drm/panfrost/panfrost_job.c:213). If autosuspend runs in that interval, the new WARN (drivers/gpu/drm/panfrost/panfrost_device.c:525) sees the credit and fires, even though this is a timing race rather than a broken job. Repeated submissions near the autosuspend boundary could therefore produce repeated stack traces. The PM core treats the resulting -EBUSY as a transient failure.

Quoted 2 lines

Now I have to admit I don't trust it that much - but I'd want a convincing argument on why panfrost_jm_is_idle() will never be false here.

You're right. I was in the belief that autosuspend kicking in was proof of no inflight or pending jobs present in the scheduler queues, so I came to treat this check as things having gone awry.

I guess its value lies in the ability of the PM runtime suspend handler to cancel itself at an autosuspend event, like you said.

However, it just made me wonder: what would happen in the event that autosuspend kicks in and runs panfrost_device_runtime_suspend() right at the same time that a scheduler job is picked up by drm_sched_run_job_work(), but hasn't yet reached the statement where it does an atomic increment on the credit_count? I guess nothing, because panfrost_job_hw_submit() is getting a PM reference before accessing any HW registers, and that should take care of dealing with any ongoing autosuspend events.

Quoted 6 lines

In that case I'll just delete that warning. However, I'd say it's bad practice to have DRM drivers access the internal state of the DRM scheduler. At present, only Panfrost and Etnaviv poke it in their RPM suspend handlers, and I've been wondering whether we should get rid of this check altogether, or else maybe ask the scheduler maintainers whether it makes sense to have a non-racy way to query the presence of pending jobs in their queues?

Yes, I'm not sure whether we actually need that check. As you say there's still a race where panfrost_jm_is_idle() returns true, but afterwards credit_count is incremented. I think this is safe - panfrost_job_hw_submit() takes a PM reference which will cause the GPU to be woken up again. So we could just drop the panfrost_jm_is_idle() function completely.

Whether that has any performance impact - i.e. do we often race the autosuspend operation? - I've no idea. Presumably you didn't hit it when you had the WARN in place. So if you'd prefer to just remove the code then that's fine by me.

Thanks,
Steve

author maintainer
#23 lore raw

PATCH v12 10/15 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.

We must also disable the reset work item rather than simply cancelling it, to prevent the knob from triggering another reset when the device is being removed.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 42 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_job.c    |  2 +-
 2 files changed, 43 insertions(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_device.c +42 -0
index 09a5752a3f40..94d2de341838 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>
@@ -595,9 +596,50 @@ 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)
+		return 0;
+
+	panfrost_device_schedule_reset(pfdev);
+	flush_work(&pfdev->reset.work);
+
+	/* ret < 0 means runtime PM for the device is disabled, so we
+	 * only need to return the PM reference in the opposite case
+	 */
+	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
drivers/gpu/drm/panfrost/panfrost_job.c +1 -1
index 00fb36c577df..ab65786bf63b 100644
@@ -932,7 +932,7 @@ void panfrost_jm_fini(struct panfrost_device *pfdev)
 		drm_sched_fini(&js->queue[j].sched);
 	}
 
-	cancel_work_sync(&pfdev->reset.work);
+	disable_work_sync(&pfdev->reset.work);
 	destroy_workqueue(pfdev->reset.wq);
 }
 
Signature · 1 line
2.55.0
maintainer Reviewed-by on v12 10/15
#24 lore raw

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

Quoted 49 lines · 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.

We must also disable the reset work item rather than simply cancelling it, to prevent the knob from triggering another reset when the device is being removed.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 42 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_job.c    |  2 +-
 2 files changed, 43 insertions(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_device.c +20 -0
index 09a5752a3f40..94d2de341838 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>
@@ -595,9 +596,50 @@ 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)
+		return 0;
+
+	panfrost_device_schedule_reset(pfdev);

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_device.c +10 -0
+	flush_work(&pfdev->reset.work);
+
+	/* ret < 0 means runtime PM for the device is disabled, so we
+	 * only need to return the PM reference in the opposite case
+	 */
+	if (ret > 0)
+		pm_runtime_put(pfdev->base.dev);
+
+	return 0;
+}

NIT: If this wasn't debugfs I'd be complaining that you're ignoring 'val' and so this isn't very extensible. But hey, it's debugfs... so:

Quoted 32 lines
drivers/gpu/drm/panfrost/panfrost_device.c +12 -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
drivers/gpu/drm/panfrost/panfrost_job.c +1 -1
index 00fb36c577df..ab65786bf63b 100644
@@ -932,7 +932,7 @@ void panfrost_jm_fini(struct panfrost_device *pfdev)
 		drm_sched_fini(&js->queue[j].sched);
 	}
 
-	cancel_work_sync(&pfdev->reset.work);
+	disable_work_sync(&pfdev->reset.work);
 	destroy_workqueue(pfdev->reset.wq);
 }
 
author maintainer
#25 lore raw

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

Quoted 65 lines · Steven Price
Quoted 49 lines · 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.

We must also disable the reset work item rather than simply cancelling it, to prevent the knob from triggering another reset when the device is being removed.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 42 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_job.c    |  2 +-
 2 files changed, 43 insertions(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_device.c +20 -0
index 09a5752a3f40..94d2de341838 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>
@@ -595,9 +596,50 @@ 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)
+		return 0;
+
+	panfrost_device_schedule_reset(pfdev);

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_device.c +10 -0
+	flush_work(&pfdev->reset.work);
+
+	/* ret < 0 means runtime PM for the device is disabled, so we
+	 * only need to return the PM reference in the opposite case
+	 */
+	if (ret > 0)
+		pm_runtime_put(pfdev->base.dev);
+
+	return 0;
+}

NIT: If this wasn't debugfs I'd be complaining that you're ignoring 'val' and so this isn't very extensible. But hey, it's debugfs... so:

Quoted 1 line

I looked into the available debugfs attribute definition macros and none of them provide a wrapper that avoid passing the input value when the attribute is to be understood as an action rather than a device property. My understanding is that because debugfs doesn't become part of the device's uAPI, we can do pretty much whatever we want with it. However, on a second thought, maybe in the future we'll want to extend the knob so that it performs resets in different ways, and would want to keep backwards compatibility with UM binaries that expect certain input values to stand for specific actions.

I'll change it so that all value other than '1' are returned with -EINVAL.

maintainer
#26 lore raw

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

Quoted 76 lines · Adrián Larumbe
Quoted 65 lines · Steven Price
Quoted 49 lines · 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.

We must also disable the reset work item rather than simply cancelling it, to prevent the knob from triggering another reset when the device is being removed.

---
 drivers/gpu/drm/panfrost/panfrost_device.c | 42 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/panfrost/panfrost_job.c    |  2 +-
 2 files changed, 43 insertions(+), 1 deletion(-)
drivers/gpu/drm/panfrost/panfrost_device.c +20 -0
index 09a5752a3f40..94d2de341838 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>
@@ -595,9 +596,50 @@ 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)
+		return 0;
+
+	panfrost_device_schedule_reset(pfdev);

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_device.c +10 -0
+	flush_work(&pfdev->reset.work);
+
+	/* ret < 0 means runtime PM for the device is disabled, so we
+	 * only need to return the PM reference in the opposite case
+	 */
+	if (ret > 0)
+		pm_runtime_put(pfdev->base.dev);
+
+	return 0;
+}

NIT: If this wasn't debugfs I'd be complaining that you're ignoring 'val' and so this isn't very extensible. But hey, it's debugfs... so:

Quoted 1 line

I looked into the available debugfs attribute definition macros and none of them provide a wrapper that avoid passing the input value when the attribute is to be understood as an action rather than a device property. My understanding is that because debugfs doesn't become part of the device's uAPI, we can do pretty much whatever we want with it. However, on a second thought, maybe in the future we'll want to extend the knob so that it performs resets in different ways, and would want to keep backwards compatibility with UM binaries that expect certain input values to stand for specific actions.

Quoted 1 line

I'll change it so that all value other than '1' are returned with -EINVAL.

That would be my preference, but like you say this is debugfs so we don't consider it part of uAPI. Mostly it seems a bit odd that you could read the value, get 0 (i.e. no reset), write it back and that would trigger a reset. But equally this isn't the sort of file which you'd expect a read-modify-write operation to work on, so it's not a big deal.

Thanks,
Steve

author maintainer
#27 lore raw

PATCH v12 11/15 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
#28 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] drm/panfrost: inverted hardware disable sequence risks spurious dump --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] drm/panfrost: inverted hardware disable sequence risks spurious dump --

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

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

This commit introduces a helper function to avoid repetition of the hardware performance counter disable sequence.

Quoted 7 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +0 -0
index 7020c0192e189..f71534e741b65 100644
@@ -35,6 +35,16 @@ struct panfrost_perfcnt {
 	struct completion dump_comp;
 };

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +9 -0
 
+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);
+}

[Severity: High]
Does this sequence introduce a risk of a spurious hardware dump?

The new helper turns off the GPU_PERFCNT_CFG register before disabling the individual block counters, which reverses the previous safe sequence in panfrost_perfcnt_disable_locked() where the EN registers were cleared first.

Modifying the CFG register while TILER_EN is still active might trigger a spurious dump. This seems similar to hardware erratum PRLAM-8186, which requires TILER_EN to be off before modifying the CFG register to enable counters.

[ ... ]

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +0 -0
@@ -193,12 +203,7 @@ static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev,
 	if (user != perfcnt->user)
 		return -EINVAL;
 

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +1 -6
-	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);

[Severity: High]
Could a spurious hardware dump here result in memory corruption?

Because the inverted hardware disable sequence is invoked while TILER_EN is active, a spurious hardware dump would cause the GPU to write to the performance counter buffer asynchronously.

Since panfrost_perfcnt_disable_locked() immediately tears down and unmaps this memory via drm_gem_vunmap(), could the GPU perform a DMA write-after-free, leading to an IOMMU fault or memory corruption?

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

PATCH v12 12/15 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
maintainer
#30 lore raw

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

Quoted 2 lines · 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

I don't think this is entirely true. The GPU performance counter unit only writes the counters that are enabled, counters that share a cache line but are not enabled are not written by the performance counter unit, but if the L2 contains that cache line then the write can hit in the L2 and dirty the entire line including stale data where the unwritten cache line is.

The upshot is that if the CPU has cleared a block of memory which the GPU happens to have cached, then the "unused" counters may end up showing the old data before the CPU cleared it (if they share a cache line with an active counter).

I have to admit it's probably somewhat academic given that Panfrost doesn't expose the ability to control which counters are enabled...

Is there a good reason for this patch (i.e. have you seen a performance problem with doing the invalidate)? Otherwise I'd prefer we keep to the safe route rather than trying to over optimise cache maintenance.

Obviously in the fully coherent case the invalidate could be skipped (as in the next patch).

Thanks,
Steve

Quoted 38 lines

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)
maintainer
#31 lore raw

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

Quoted 10 lines · On Fri, 2 Oct 2026 16:14:38 +0100 Steven Price <[email protected]> wrote:
Quoted 2 lines · 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

I don't think this is entirely true. The GPU performance counter unit only writes the counters that are enabled, counters that share a cache line but are not enabled are not written by the performance counter unit, but if the L2 contains that cache line then the write can hit in the L2 and dirty the entire line including stale data where the unwritten cache line is.

I don't think this can happen though, because _enable_locked() is creating a BO (and its GPU mapping) just before enabling the perfcnt block, meaning the buffer is known to have no dirty cacheline pointing to it until the first dump happens. And we do flush and invalidate GPU caches after each dump, so again, we should be covered.

Quoted 5 lines

The upshot is that if the CPU has cleared a block of memory which the GPU happens to have cached, then the "unused" counters may end up showing the old data before the CPU cleared it (if they share a cache line with an active counter).

I agree, but that's not a case we can hit in the enable path. I think I mentioned the commit message was misleading, and that we should instead talk about the fact the GPU is not supposed to have cached anything up until the first SAMPLE following a the ENABLE step.

Quoted 7 lines

I have to admit it's probably somewhat academic given that Panfrost doesn't expose the ability to control which counters are enabled...

Is there a good reason for this patch (i.e. have you seen a performance problem with doing the invalidate)? Otherwise I'd prefer we keep to the safe route rather than trying to over optimise cache maintenance.

I think I was the one suggesting dropping this flush so that panfrost_perfcnt_hw_enable() (in the last patch) has one less fallible operation. Besides, I find it confusing to have a cache flush+inval in a path where the GPU is not supposed to have accessed the buffer yet (or later on, when we re-enable after a RESET, in a path where the GPU has been reset and the caches are known to be empty).

Quoted 46 lines

Obviously in the fully coherent case the invalidate could be skipped (as in the next patch).

Thanks,
Steve

Quoted 38 lines

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)
maintainer
#32 lore raw

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

Quoted 14 lines · Boris Brezillon
Quoted 10 lines · On Fri, 2 Oct 2026 16:14:38 +0100 Steven Price <[email protected]> wrote:
Quoted 2 lines · 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

I don't think this is entirely true. The GPU performance counter unit only writes the counters that are enabled, counters that share a cache line but are not enabled are not written by the performance counter unit, but if the L2 contains that cache line then the write can hit in the L2 and dirty the entire line including stale data where the unwritten cache line is.

Quoted 5 lines

I don't think this can happen though, because _enable_locked() is creating a BO (and its GPU mapping) just before enabling the perfcnt block, meaning the buffer is known to have no dirty cacheline pointing to it until the first dump happens. And we do flush and invalidate GPU caches after each dump, so again, we should be covered.

The situation isn't actually a dirty cache line at the start, but a stale one. We start off with the memory matching a clean line in the GPU's cache. But because we don't have coherency the clean line can stay even if it's inconsistent with everything else.

CPU | GPU | Memory
----------------+-----------------------+--------------------
| clean line | matches GPU cache
----------------+-----------------------+--------------------
CPU allocates new buffer and writes zeros
----------------+-----------------------+--------------------

Dirty cache line| stale clean line      | unknown (cache line
                |                       | might be evicted)
----------------+-----------------------+--------------------
   CPU cleans its own cache to memory
----------------+-----------------------+--------------------
Potential clean | stale clean line      | Matches CPU
cache line      |                       |
----------------+-----------------------+--------------------
   Start dump without invaliding GPU
----------------+-----------------------+--------------------
Potential clean | GPU writes data, and  | Unknown
cache line      | hits in the clean line|
                | even though it's stale|

----------------+-----------------------+--------------------
CPU flushes the GPU's cache and invalidates it's own
----------------+-----------------------+--------------------
No-cache line | writes out clean line | Matches GPU

Of course for the GPU to have ended up with that stale clean cache line means that the physical memory was previously used for something else on the GPU, so the newly allocated BO has to reuse memory from a previous BO that the GPU has accessed. And it's all "unlikely" due to the small size of the GPU's cache.

Quoted 19 lines
Quoted 5 lines

The upshot is that if the CPU has cleared a block of memory which the GPU happens to have cached, then the "unused" counters may end up showing the old data before the CPU cleared it (if they share a cache line with an active counter).

I agree, but that's not a case we can hit in the enable path. I think I mentioned the commit message was misleading, and that we should instead talk about the fact the GPU is not supposed to have cached anything up until the first SAMPLE following a the ENABLE step.

Quoted 7 lines

I have to admit it's probably somewhat academic given that Panfrost doesn't expose the ability to control which counters are enabled...

Is there a good reason for this patch (i.e. have you seen a performance problem with doing the invalidate)? Otherwise I'd prefer we keep to the safe route rather than trying to over optimise cache maintenance.

Quoted 6 lines

I think I was the one suggesting dropping this flush so that panfrost_perfcnt_hw_enable() (in the last patch) has one less fallible operation. Besides, I find it confusing to have a cache flush+inval in a path where the GPU is not supposed to have accessed the buffer yet (or later on, when we re-enable after a RESET, in a path where the GPU has been reset and the caches are known to be empty).

So I agree this Should Be Safe™ because of how the driver is currently using the performance counters. If we really want to drop the invalidate then I think we need a comment explaining the logic. My worry is that someone extends this in the future (e.g. allow selecting which counters to enable) and breaks assumptions without them being documented.

We normally do perform an invalidate when the GPU first touches a buffer (e.g. for a BO) - it's just normally more implicit because it's done as part of the job manager(/command stream).

Also "caches known to be empty" is a dangerous thing to assume on anything that could involve speculation - AFAIK Mali doesn't really perform any form of speculation, but I'm not 100% sure on that. Certainly with CPUs you don't get such a luxury of knowing what it might have populated in the caches.

Thanks,
Steve

Quoted 47 lines
Quoted 46 lines

Obviously in the fully coherent case the invalidate could be skipped (as in the next patch).

Thanks,
Steve

Quoted 38 lines

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)
maintainer
#33 lore raw

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

Quoted 51 lines · On Mon, 5 Oct 2026 16:06:02 +0100 Steven Price <[email protected]> wrote:
Quoted 14 lines · Boris Brezillon
Quoted 10 lines · On Fri, 2 Oct 2026 16:14:38 +0100 Steven Price <[email protected]> wrote:
Quoted 2 lines · 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

I don't think this is entirely true. The GPU performance counter unit only writes the counters that are enabled, counters that share a cache line but are not enabled are not written by the performance counter unit, but if the L2 contains that cache line then the write can hit in the L2 and dirty the entire line including stale data where the unwritten cache line is.

Quoted 5 lines

I don't think this can happen though, because _enable_locked() is creating a BO (and its GPU mapping) just before enabling the perfcnt block, meaning the buffer is known to have no dirty cacheline pointing to it until the first dump happens. And we do flush and invalidate GPU caches after each dump, so again, we should be covered.

The situation isn't actually a dirty cache line at the start, but a stale one. We start off with the memory matching a clean line in the GPU's cache. But because we don't have coherency the clean line can stay even if it's inconsistent with everything else.

CPU | GPU | Memory
----------------+-----------------------+--------------------
| clean line | matches GPU cache
----------------+-----------------------+--------------------
CPU allocates new buffer and writes zeros
----------------+-----------------------+--------------------

Dirty cache line| stale clean line      | unknown (cache line
                |                       | might be evicted)
----------------+-----------------------+--------------------
   CPU cleans its own cache to memory
----------------+-----------------------+--------------------
Potential clean | stale clean line      | Matches CPU
cache line      |                       |
----------------+-----------------------+--------------------
   Start dump without invaliding GPU
----------------+-----------------------+--------------------
Potential clean | GPU writes data, and  | Unknown
cache line      | hits in the clean line|
                | even though it's stale|

----------------+-----------------------+--------------------
CPU flushes the GPU's cache and invalidates it's own
----------------+-----------------------+--------------------
No-cache line | writes out clean line | Matches GPU

Quoted 5 lines

Of course for the GPU to have ended up with that stale clean cache line means that the physical memory was previously used for something else on the GPU, so the newly allocated BO has to reuse memory from a previous BO that the GPU has accessed. And it's all "unlikely" due to the small size of the GPU's cache.

Hm, I'd say it's actually impossible because every unmap operation is followed by a flush+inval of the GPU L2 and LSC, so for this stale clean line to exist on the GPU side when a physical page is GPU-mapped again, it would take a bug in the unmap logic or in the MMU HW, I think. Am I missing something?

Quoted 38 lines
Quoted 21 lines
Quoted 7 lines

The upshot is that if the CPU has cleared a block of memory which the GPU happens to have cached, then the "unused" counters may end up showing the old data before the CPU cleared it (if they share a cache line with an active counter).

I agree, but that's not a case we can hit in the enable path. I think I mentioned the commit message was misleading, and that we should instead talk about the fact the GPU is not supposed to have cached anything up until the first SAMPLE following a the ENABLE step.

Quoted 7 lines

I have to admit it's probably somewhat academic given that Panfrost doesn't expose the ability to control which counters are enabled...

Is there a good reason for this patch (i.e. have you seen a performance problem with doing the invalidate)? Otherwise I'd prefer we keep to the safe route rather than trying to over optimise cache maintenance.

Quoted 6 lines

I think I was the one suggesting dropping this flush so that panfrost_perfcnt_hw_enable() (in the last patch) has one less fallible operation. Besides, I find it confusing to have a cache flush+inval in a path where the GPU is not supposed to have accessed the buffer yet (or later on, when we re-enable after a RESET, in a path where the GPU has been reset and the caches are known to be empty).

So I agree this Should Be Safe™ because of how the driver is currently using the performance counters. If we really want to drop the invalidate then I think we need a comment explaining the logic. My worry is that someone extends this in the future (e.g. allow selecting which counters to enable) and breaks assumptions without them being documented.

We normally do perform an invalidate when the GPU first touches a buffer (e.g. for a BO) - it's just normally more implicit because it's done as part of the job manager(/command stream).

Quoted 5 lines

Also "caches known to be empty" is a dangerous thing to assume on anything that could involve speculation - AFAIK Mali doesn't really perform any form of speculation, but I'm not 100% sure on that. Certainly with CPUs you don't get such a luxury of knowing what it might have populated in the caches.

I suppose speculation pre-populating the caches with stale data would be covered by the flush+inval we do after a map operation (this is currently done in the unlock path regardless of the VM op, so both map and unmap get it). I don't know, maybe the goal is to relax the flushing policy around VM modifications in the future, but if things stay as they are now, we can assume that a fresh BO being GPU-mapped guarantees that no cacheline points to it until the first GPU access. But maybe I'm missing something else...

maintainer
#34 lore raw

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

Quoted 60 lines · Boris Brezillon
Quoted 51 lines · On Mon, 5 Oct 2026 16:06:02 +0100 Steven Price <[email protected]> wrote:
Quoted 14 lines · Boris Brezillon
Quoted 10 lines · On Fri, 2 Oct 2026 16:14:38 +0100 Steven Price <[email protected]> wrote:
Quoted 2 lines · 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

I don't think this is entirely true. The GPU performance counter unit only writes the counters that are enabled, counters that share a cache line but are not enabled are not written by the performance counter unit, but if the L2 contains that cache line then the write can hit in the L2 and dirty the entire line including stale data where the unwritten cache line is.

Quoted 5 lines

I don't think this can happen though, because _enable_locked() is creating a BO (and its GPU mapping) just before enabling the perfcnt block, meaning the buffer is known to have no dirty cacheline pointing to it until the first dump happens. And we do flush and invalidate GPU caches after each dump, so again, we should be covered.

The situation isn't actually a dirty cache line at the start, but a stale one. We start off with the memory matching a clean line in the GPU's cache. But because we don't have coherency the clean line can stay even if it's inconsistent with everything else.

CPU | GPU | Memory
----------------+-----------------------+--------------------
| clean line | matches GPU cache
----------------+-----------------------+--------------------
CPU allocates new buffer and writes zeros
----------------+-----------------------+--------------------

Dirty cache line| stale clean line      | unknown (cache line
                |                       | might be evicted)
----------------+-----------------------+--------------------
   CPU cleans its own cache to memory
----------------+-----------------------+--------------------
Potential clean | stale clean line      | Matches CPU
cache line      |                       |
----------------+-----------------------+--------------------
   Start dump without invaliding GPU
----------------+-----------------------+--------------------
Potential clean | GPU writes data, and  | Unknown
cache line      | hits in the clean line|
                | even though it's stale|

----------------+-----------------------+--------------------
CPU flushes the GPU's cache and invalidates it's own
----------------+-----------------------+--------------------
No-cache line | writes out clean line | Matches GPU

Quoted 5 lines

Of course for the GPU to have ended up with that stale clean cache line means that the physical memory was previously used for something else on the GPU, so the newly allocated BO has to reuse memory from a previous BO that the GPU has accessed. And it's all "unlikely" due to the small size of the GPU's cache.

Quoted 5 lines

Hm, I'd say it's actually impossible because every unmap operation is followed by a flush+inval of the GPU L2 and LSC, so for this stale clean line to exist on the GPU side when a physical page is GPU-mapped again, it would take a bug in the unmap logic or in the MMU HW, I think. Am I missing something?

Ah, yes that's true :) Although there's no need for us to do an invalidate on the unmap path...

Quoted 44 lines
Quoted 38 lines
Quoted 21 lines
Quoted 7 lines

The upshot is that if the CPU has cleared a block of memory which the GPU happens to have cached, then the "unused" counters may end up showing the old data before the CPU cleared it (if they share a cache line with an active counter).

I agree, but that's not a case we can hit in the enable path. I think I mentioned the commit message was misleading, and that we should instead talk about the fact the GPU is not supposed to have cached anything up until the first SAMPLE following a the ENABLE step.

Quoted 7 lines

I have to admit it's probably somewhat academic given that Panfrost doesn't expose the ability to control which counters are enabled...

Is there a good reason for this patch (i.e. have you seen a performance problem with doing the invalidate)? Otherwise I'd prefer we keep to the safe route rather than trying to over optimise cache maintenance.

Quoted 6 lines

I think I was the one suggesting dropping this flush so that panfrost_perfcnt_hw_enable() (in the last patch) has one less fallible operation. Besides, I find it confusing to have a cache flush+inval in a path where the GPU is not supposed to have accessed the buffer yet (or later on, when we re-enable after a RESET, in a path where the GPU has been reset and the caches are known to be empty).

So I agree this Should Be Safe™ because of how the driver is currently using the performance counters. If we really want to drop the invalidate then I think we need a comment explaining the logic. My worry is that someone extends this in the future (e.g. allow selecting which counters to enable) and breaks assumptions without them being documented.

We normally do perform an invalidate when the GPU first touches a buffer (e.g. for a BO) - it's just normally more implicit because it's done as part of the job manager(/command stream).

Quoted 5 lines

Also "caches known to be empty" is a dangerous thing to assume on anything that could involve speculation - AFAIK Mali doesn't really perform any form of speculation, but I'm not 100% sure on that. Certainly with CPUs you don't get such a luxury of knowing what it might have populated in the caches.

Quoted 8 lines

I suppose speculation pre-populating the caches with stale data would be covered by the flush+inval we do after a map operation (this is currently done in the unlock path regardless of the VM op, so both map and unmap get it). I don't know, maybe the goal is to relax the flushing policy around VM modifications in the future, but if things stay as they are now, we can assume that a fresh BO being GPU-mapped guarantees that no cacheline points to it until the first GPU access. But maybe I'm missing something else...

I have to admit I'm coming from experience on CPUs - there speculation means a CPU can load a cache line at basically any point. So the argument that a line cannot be in a cache almost never holds. GPUs (at least Mali GPUs) haven't quite got to the stage of CPUs.

Thanks,
Steve

maintainer
#35 lore raw

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

Quoted 61 lines · On Mon, 5 Oct 2026 17:05:14 +0100 Steven Price <[email protected]> wrote:
Quoted 60 lines · Boris Brezillon
Quoted 56 lines · On Mon, 5 Oct 2026 16:06:02 +0100 Steven Price <[email protected]> wrote:
Quoted 14 lines · Boris Brezillon
Quoted 10 lines · On Fri, 2 Oct 2026 16:14:38 +0100 Steven Price <[email protected]> wrote:
Quoted 2 lines · 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

I don't think this is entirely true. The GPU performance counter unit only writes the counters that are enabled, counters that share a cache line but are not enabled are not written by the performance counter unit, but if the L2 contains that cache line then the write can hit in the L2 and dirty the entire line including stale data where the unwritten cache line is.

Quoted 5 lines

I don't think this can happen though, because _enable_locked() is creating a BO (and its GPU mapping) just before enabling the perfcnt block, meaning the buffer is known to have no dirty cacheline pointing to it until the first dump happens. And we do flush and invalidate GPU caches after each dump, so again, we should be covered.

The situation isn't actually a dirty cache line at the start, but a stale one. We start off with the memory matching a clean line in the GPU's cache. But because we don't have coherency the clean line can stay even if it's inconsistent with everything else.

CPU | GPU | Memory
----------------+-----------------------+--------------------
| clean line | matches GPU cache
----------------+-----------------------+--------------------
CPU allocates new buffer and writes zeros
----------------+-----------------------+--------------------

Dirty cache line| stale clean line      | unknown (cache line
                |                       | might be evicted)
----------------+-----------------------+--------------------
   CPU cleans its own cache to memory
----------------+-----------------------+--------------------
Potential clean | stale clean line      | Matches CPU
cache line      |                       |
----------------+-----------------------+--------------------
   Start dump without invaliding GPU
----------------+-----------------------+--------------------
Potential clean | GPU writes data, and  | Unknown
cache line      | hits in the clean line|
                | even though it's stale|

----------------+-----------------------+--------------------
CPU flushes the GPU's cache and invalidates it's own
----------------+-----------------------+--------------------
No-cache line | writes out clean line | Matches GPU

Of course for the GPU to have ended up with that stale clean cache line means that the physical memory was previously used for something else on the GPU, so the newly allocated BO has to reuse memory from a previous BO that the GPU has accessed. And it's all "unlikely" due to the small size of the GPU's cache.

Quoted 8 lines
Quoted 5 lines

Hm, I'd say it's actually impossible because every unmap operation is followed by a flush+inval of the GPU L2 and LSC, so for this stale clean line to exist on the GPU side when a physical page is GPU-mapped again, it would take a bug in the unmap logic or in the MMU HW, I think. Am I missing something?

Ah, yes that's true :) Although there's no need for us to do an invalidate on the unmap path...

My bad, I'm confusing Panfrost and Panthor here. There's indeed no L2 flush+inval on unmap in Panfrost. We do have an L2 flush+inval on map (FLUSH_PT) that should cover this case though.

author maintainer
#36 lore raw

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

Quoted 11 lines · Boris Brezillon
Quoted 4 lines · On Mon, 5 Oct 2026 17:05:14 +0100 Steven Price <[email protected]> wrote:

On 05/10/2026 16:37, Boris Brezillon wrote:

Ah, yes that's true :) Although there's no need for us to do an invalidate on the unmap path...

My bad, I'm confusing Panfrost and Panthor here. There's indeed no L2 flush+inval on unmap in Panfrost. We do have an L2 flush+inval on map (FLUSH_PT) that should cover this case though.

But we're doing panfrost_mmu_flush_range() both at the end of maps and unmaps, so I wonder whether when you say we do L2 flush+inval on map, what you meant was when submit a new job, we force a flush+invalidate for the AS the job runs against.

author maintainer
#37 lore raw

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

In reply to Steven Price
Quoted 6 lines · Steven Price
Quoted 4 lines · Boris Brezillon

On Mon, 5 Oct 2026 16:06:02 +0100
Steven Price <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines

Hm, I'd say it's actually impossible because every unmap operation is followed by a flush+inval of the GPU L2 and LSC, so for this stale clean line to exist on the GPU side when a physical page is GPU-mapped again, it would take a bug in the unmap logic or in the MMU HW, I think. Am I missing something?

Ah, yes that's true :) Although there's no need for us to do an invalidate on the unmap path...

Does that mean the panfrost_mmu_flush_range() we do at the end of panfrost_mmu_unmap() is unnecessary? Is it because, as you said in a previous message, cache lines for BOs referenced in a CS are always invalidates before anything else is executed?

If we keep the invalidate at the end of the mmu path, then there would be no need to mention that all counters being enabled is the ultimate reason why no initial flush/invalidate is needed in the perfcnt enable path.

Quoted 18 lines
Quoted 10 lines
Quoted 1 line

I suppose speculation pre-populating the caches with stale data would be covered by the flush+inval we do after a map operation (this is currently done in the unlock path regardless of the VM op, so both map and unmap get it). I don't know, maybe the goal is to relax the flushing policy around VM modifications in the future, but if things stay as they are now, we can assume that a fresh BO being GPU-mapped guarantees that no cacheline points to it until the first GPU access. But maybe I'm missing something else...

I have to admit I'm coming from experience on CPUs - there speculation means a CPU can load a cache line at basically any point. So the argument that a line cannot be in a cache almost never holds. GPUs (at least Mali GPUs) haven't quite got to the stage of CPUs.

Thanks,
Steve

maintainer
#38 lore raw

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

Quoted 13 lines · Adrián Larumbe
Quoted 12 lines · Steven Price
Quoted 9 lines · Boris Brezillon

On Mon, 5 Oct 2026 16:06:02 +0100
Steven Price <[email protected]> wrote:

Hm, I'd say it's actually impossible because every unmap operation is followed by a flush+inval of the GPU L2 and LSC, so for this stale clean line to exist on the GPU side when a physical page is GPU-mapped again, it would take a bug in the unmap logic or in the MMU HW, I think. Am I missing something?

Quoted 6 lines
Quoted 2 lines

Ah, yes that's true :) Although there's no need for us to do an invalidate on the unmap path...

Does that mean the panfrost_mmu_flush_range() we do at the end of panfrost_mmu_unmap() is unnecessary? Is it because, as you said in a previous message, cache lines for BOs referenced in a CS are always invalidates before anything else is executed?

Sorry, I wasn't very clear on the wording here - we don't need an *invalidate* but we do need a *clean*. Midgard/Bifrost hardware doesn't really give us much control over cache operations - we tend to just clean everything and blow away the cache (because the GPU's caches are "small" it's not worth the complexity).

Quoted 3 lines

If we keep the invalidate at the end of the mmu path, then there would be no need to mention that all counters being enabled is the ultimate reason why no initial flush/invalidate is needed in the perfcnt enable path.

Yes I think relying on all counters being enabled is a bad design. We can justify the change based on the existing flushes/invalidates we're doing.

Quoted 20 lines
Quoted 18 lines
Quoted 10 lines
Quoted 1 line

I suppose speculation pre-populating the caches with stale data would be covered by the flush+inval we do after a map operation (this is currently done in the unlock path regardless of the VM op, so both map and unmap get it). I don't know, maybe the goal is to relax the flushing policy around VM modifications in the future, but if things stay as they are now, we can assume that a fresh BO being GPU-mapped guarantees that no cacheline points to it until the first GPU access. But maybe I'm missing something else...

I have to admit I'm coming from experience on CPUs - there speculation means a CPU can load a cache line at basically any point. So the argument that a line cannot be in a cache almost never holds. GPUs (at least Mali GPUs) haven't quite got to the stage of CPUs.

Thanks,
Steve

author maintainer
#39 lore raw

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

In reply to Steven Price
Quoted 23 lines · Steven Price
Quoted 9 lines · Boris Brezillon

On Fri, 2 Oct 2026 16:14:38 +0100
Steven Price <[email protected]> wrote:

I don't think this can happen though, because _enable_locked() is creating a BO (and its GPU mapping) just before enabling the perfcnt block, meaning the buffer is known to have no dirty cacheline pointing to it until the first dump happens. And we do flush and invalidate GPU caches after each dump, so again, we should be covered.

The situation isn't actually a dirty cache line at the start, but a stale one. We start off with the memory matching a clean line in the GPU's cache. But because we don't have coherency the clean line can stay even if it's inconsistent with everything else.

CPU | GPU | Memory
----------------+-----------------------+--------------------
| clean line | matches GPU cache
----------------+-----------------------+--------------------
CPU allocates new buffer and writes zeros
----------------+-----------------------+--------------------

Quoted 4 lines
Dirty cache line| stale clean line      | unknown (cache line
                |                       | might be evicted)

----------------+-----------------------+--------------------
CPU cleans its own cache to memory

We're not doing this explicitly for BO memory from the CPU. AFAIK the only way to do that manually is calling dma_sync_single_for_device() on physical addresses. I suppose this means the reason why the CPU can see up-to-date dump data is because a GPU cache flush also invalidates CPU caches for the same lines?

Quoted 36 lines

----------------+-----------------------+--------------------

Potential clean | stale clean line      | Matches CPU
cache line      |                       |
----------------+-----------------------+--------------------
   Start dump without invaliding GPU
----------------+-----------------------+--------------------
Potential clean | GPU writes data, and  | Unknown
cache line      | hits in the clean line|
                | even though it's stale|

----------------+-----------------------+--------------------
CPU flushes the GPU's cache and invalidates it's own
----------------+-----------------------+--------------------
No-cache line | writes out clean line | Matches GPU

Of course for the GPU to have ended up with that stale clean cache line means that the physical memory was previously used for something else on the GPU, so the newly allocated BO has to reuse memory from a previous BO that the GPU has accessed. And it's all "unlikely" due to the small size of the GPU's cache.

Quoted 8 lines
Quoted 1 line

I agree, but that's not a case we can hit in the enable path. I think I mentioned the commit message was misleading, and that we should instead talk about the fact the GPU is not supposed to have cached anything up until the first SAMPLE following a the ENABLE step.

Quoted 6 lines

I think I was the one suggesting dropping this flush so that panfrost_perfcnt_hw_enable() (in the last patch) has one less fallible operation. Besides, I find it confusing to have a cache flush+inval in a path where the GPU is not supposed to have accessed the buffer yet (or later on, when we re-enable after a RESET, in a path where the GPU has been reset and the caches are known to be empty).

Quoted 5 lines

So I agree this Should Be Safe™ because of how the driver is currently using the performance counters. If we really want to drop the invalidate then I think we need a comment explaining the logic. My worry is that someone extends this in the future (e.g. allow selecting which counters to enable) and breaks assumptions without them being documentead.

Perfcnt ioctl's are currently marked as unstable, but even so, adding the initial flush/invalidate back in case anyone ever wanted to redefine the uAPI to add support for fine-grained selection of counters shouldn't be much of an issue. It's just that, as things stand now, it simplifies error handling quite a bit, and I guess it's a reasonable trade-off.

I'll add a comment explaining why it's safe not to do the initial flush/invalidate in a new revision.

Quoted 3 lines

We normally do perform an invalidate when the GPU first touches a buffer (e.g. for a BO) - it's just normally more implicit because it's done as part of the job manager(/command stream).

Does this mean instructions that are part of the command stream passed to the submit ioctl can trigger a flush/invalidate without CPU intervention?

Quoted 9 lines

Also "caches known to be empty" is a dangerous thing to assume on anything that could involve speculation - AFAIK Mali doesn't really perform any form of speculation, but I'm not 100% sure on that. Certainly with CPUs you don't get such a luxury of knowing what it might have populated in the caches.

Thanks,
Steve

maintainer
#40 lore raw

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

Quoted 29 lines · Adrián Larumbe
Quoted 23 lines · Steven Price
Quoted 9 lines · Boris Brezillon

On Fri, 2 Oct 2026 16:14:38 +0100
Steven Price <[email protected]> wrote:

I don't think this can happen though, because _enable_locked() is creating a BO (and its GPU mapping) just before enabling the perfcnt block, meaning the buffer is known to have no dirty cacheline pointing to it until the first dump happens. And we do flush and invalidate GPU caches after each dump, so again, we should be covered.

The situation isn't actually a dirty cache line at the start, but a stale one. We start off with the memory matching a clean line in the GPU's cache. But because we don't have coherency the clean line can stay even if it's inconsistent with everything else.

CPU | GPU | Memory
----------------+-----------------------+--------------------
| clean line | matches GPU cache
----------------+-----------------------+--------------------
CPU allocates new buffer and writes zeros
----------------+-----------------------+--------------------

Quoted 4 lines
Dirty cache line| stale clean line      | unknown (cache line
                |                       | might be evicted)

----------------+-----------------------+--------------------
CPU cleans its own cache to memory

Quoted 4 lines

We're not doing this explicitly for BO memory from the CPU. AFAIK the only way to do that manually is calling dma_sync_single_for_device() on physical addresses. I suppose this means the reason why the CPU can see up-to-date dump data is because a GPU cache flush also invalidates CPU caches for the same lines?

Without coherency between the CPU and GPU then when sharing a buffer with the GPU there needs to be cache maintenance (otherwise the GPU might see stale data which is still in RAM, which would be a security hole). This is because a previous user could have mapped the data cached. I believe this is done internally within the DMA framework.

For reading the data written by the GPU, again there needs to be cache maintenance - a flush on the GPU and an invalidate on the CPU. However I think we map the data on the CPU as write-combining (i.e. uncached) so there's no need for an explicit invalidate on the CPU - the reads will bypass the cache.

Quoted 51 lines
Quoted 36 lines

----------------+-----------------------+--------------------

Potential clean | stale clean line      | Matches CPU
cache line      |                       |
----------------+-----------------------+--------------------
   Start dump without invaliding GPU
----------------+-----------------------+--------------------
Potential clean | GPU writes data, and  | Unknown
cache line      | hits in the clean line|
                | even though it's stale|

----------------+-----------------------+--------------------
CPU flushes the GPU's cache and invalidates it's own
----------------+-----------------------+--------------------
No-cache line | writes out clean line | Matches GPU

Of course for the GPU to have ended up with that stale clean cache line means that the physical memory was previously used for something else on the GPU, so the newly allocated BO has to reuse memory from a previous BO that the GPU has accessed. And it's all "unlikely" due to the small size of the GPU's cache.

Quoted 8 lines
Quoted 1 line

I agree, but that's not a case we can hit in the enable path. I think I mentioned the commit message was misleading, and that we should instead talk about the fact the GPU is not supposed to have cached anything up until the first SAMPLE following a the ENABLE step.

Quoted 6 lines

I think I was the one suggesting dropping this flush so that panfrost_perfcnt_hw_enable() (in the last patch) has one less fallible operation. Besides, I find it confusing to have a cache flush+inval in a path where the GPU is not supposed to have accessed the buffer yet (or later on, when we re-enable after a RESET, in a path where the GPU has been reset and the caches are known to be empty).

Quoted 5 lines

So I agree this Should Be Safe™ because of how the driver is currently using the performance counters. If we really want to drop the invalidate then I think we need a comment explaining the logic. My worry is that someone extends this in the future (e.g. allow selecting which counters to enable) and breaks assumptions without them being documentead.

Perfcnt ioctl's are currently marked as unstable, but even so, adding the initial flush/invalidate back in case anyone ever wanted to redefine the uAPI to add support for fine-grained selection of counters shouldn't be much of an issue. It's just that, as things stand now, it simplifies error handling quite a bit, and I guess it's a reasonable trade-off.

I'll add a comment explaining why it's safe not to do the initial flush/invalidate in a new revision.

Quoted 6 lines
Quoted 3 lines

We normally do perform an invalidate when the GPU first touches a buffer (e.g. for a BO) - it's just normally more implicit because it's done as part of the job manager(/command stream).

Does this mean instructions that are part of the command stream passed to the submit ioctl can trigger a flush/invalidate without CPU intervention?

Sorry, I shouldn't have mentioned command streams - that's a Panthor thing. For Panfrost the flush/invalidate is done as part of the job submission (FLUSH_PT). With the Panthor and the MCU we then have a FLUSH_CACHE2 instruction that can be part of the command stream - so yes you can trigger (some forms of) a cache flush during the middle of a command stream without CPU intervention.

Thanks,
Steve

Quoted 11 lines
Quoted 9 lines

Also "caches known to be empty" is a dangerous thing to assume on anything that could involve speculation - AFAIK Mali doesn't really perform any form of speculation, but I'm not 100% sure on that. Certainly with CPUs you don't get such a luxury of knowing what it might have populated in the caches.

Thanks,
Steve

author maintainer
#41 lore raw

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

In reply to Steven Price
Quoted 11 lines · Steven Price
Quoted 2 lines · 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

I don't think this is entirely true. The GPU performance counter unit only writes the counters that are enabled, counters that share a cache line but are not enabled are not written by the performance counter unit, but if the L2 contains that cache line then the write can hit in the L2 and dirty the entire line including stale data where the unwritten cache line is.

From the perspective of UM, is it bad if offsets into the perfcnt buffer that stand for counters they did not enable contain undefined values?

Quoted 11 lines

The upshot is that if the CPU has cleared a block of memory which the GPU happens to have cached, then the "unused" counters may end up showing the old data before the CPU cleared it (if they share a cache line with an active counter).

I have to admit it's probably somewhat academic given that Panfrost doesn't expose the ability to control which counters are enabled...

Is there a good reason for this patch (i.e. have you seen a performance problem with doing the invalidate)? Otherwise I'd prefer we keep to the safe route rather than trying to over optimise cache maintenance.

Like Boris said in a later reply, it was mostly about simplifying error handling in the perfcnt enable path. We saw this initial clean being done and thought it wasn't necessary.

Quoted 5 lines

Obviously in the fully coherent case the invalidate could be skipped (as in the next patch).

Thanks,
Steve

maintainer
#42 lore raw

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

Quoted 13 lines · Adrián Larumbe
Quoted 11 lines · Steven Price
Quoted 2 lines · 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

I don't think this is entirely true. The GPU performance counter unit only writes the counters that are enabled, counters that share a cache line but are not enabled are not written by the performance counter unit, but if the L2 contains that cache line then the write can hit in the L2 and dirty the entire line including stale data where the unwritten cache line is.

Quoted 2 lines

From the perspective of UM, is it bad if offsets into the perfcnt buffer that stand for counters they did not enable contain undefined values?

There's two things to consider here:

* Does the UM care? The UM could write canary values to check that the GPU has actually filled in the counters. Replacing them with "undefined" values would break this behaviour. I'm not sure whether this is done - but it can be used to check that the hardware is actually writing the counter as expected.

* Is it a security hole? If the GPU cache was from when this BO was used by another context then this is obviously bad. As Boris pointed out this shouldn't be possible in the current driver if we're invalidating on unmap.

Quoted 8 lines
Quoted 8 lines

The upshot is that if the CPU has cleared a block of memory which the GPU happens to have cached, then the "unused" counters may end up showing the old data before the CPU cleared it (if they share a cache line with an active counter).

I have to admit it's probably somewhat academic given that Panfrost doesn't expose the ability to control which counters are enabled...

Quoted 7 lines
Quoted 3 lines

Is there a good reason for this patch (i.e. have you seen a performance problem with doing the invalidate)? Otherwise I'd prefer we keep to the safe route rather than trying to over optimise cache maintenance.

Like Boris said in a later reply, it was mostly about simplifying error handling in the perfcnt enable path. We saw this initial clean being done and thought it wasn't necessary.

I think we need at least a comment explaining why this is safe. It does seem that as things currently stand we don't actually need it though.

Thanks,
Steve

Quoted 7 lines
Quoted 5 lines

Obviously in the fully coherent case the invalidate could be skipped (as in the next patch).

Thanks,
Steve

author maintainer
#43 lore raw

PATCH v12 13/15 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
maintainer Reviewed-by on v12 13/15
#44 lore raw

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

Quoted 5 lines · 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.

Quoted 21 lines
---
 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)
author maintainer
#45 lore raw

PATCH v12 14/15 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 94d2de341838..80d8f6aed090 100644
@@ -481,6 +481,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 ab65786bf63b..4ddf4b722ddf 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
maintainer Reviewed-by on v12 14/15
#46 lore raw

Re: [PATCH v12 14/15] drm/panfrost: Introduce a reset lock

Quoted 4 lines · 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.

Typo: s/timestmap/timestamp

Quoted 3 lines
Quoted 65 lines
---
 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 94d2de341838..80d8f6aed090 100644
@@ -481,6 +481,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 ab65786bf63b..4ddf4b722ddf 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)
author maintainer
#47 lore raw

PATCH v12 15/15 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 when disabling and re-enabling perfcnt configuration, and also in the sections inside the 'enable'and 'dump' ioctls where device registers are being accessed.

On top of that, expand the DRM uAPI for the perfcnt DUMP operation so that user space can be made aware of a reset having happened, whether it succeeded or failed to restore the original configuration. UM needs to know about this condition because counter data is inaccurate after a reset, so the best approach might be simply to try again.

Finally, update driver uAPI documentation to explain the meaning of the new perfcnt dump ioctl's state flags, and bump DRM driver minor number to reflect the new DUMP IOCTL req field.

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_drv.c     |   3 +-
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 272 +++++++++++++++++++---------
 drivers/gpu/drm/panfrost/panfrost_perfcnt.h |   1 +
 include/uapi/drm/panfrost_drm.h             |  29 ++-
 5 files changed, 222 insertions(+), 85 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +2 -0
index 80d8f6aed090..47a869232e10 100644
@@ -491,6 +491,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_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,
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +189 -83

379 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 +28 -1
index 50d5337f35ef..7831c48c59c0 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,35 @@ struct drm_panfrost_perfcnt_enable {
 	__u32 counterset;
 };
 
+/*
+ * The next two flags describe the state of a perfcnt dump request
+ * as influenced by a device reset. They are the only values the
+ * perfcnt_dump ioctl state field can take on.
+ * Only certain state and ioctl retval combinations are legitimate.
+ */
+
+/* A new perfcnt_enable ioctl should be issued before requesting
+ * more dumps, because a HW reset failed to recreate perfcnt's
+ * original state. Otherwise further perfcnt_dump's will fail.
+ * This flag being set means ioctl's retval is always -EIO.
+ */
+#define PANFROST_PERFCNT_SESSION_DEAD (1 << 0)
+
+/* A HW reset happened before or during a sample request, and
+ * perfcnt's internal state was successfully restored. There
+ * are two possible outcomes depending on the ioctl's retval:
+ *	0: A reset happened before a dump was requested, but did
+ * nonetheless succeed. Counter values are relative to last reset.
+ *	-EAGAIN: A reset happened when a counter values sampling
+ * request was ongoing. Values are undefined so a new dump ioctl
+ * should be issued.
+ */
+#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
#48 lore raw

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

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] drm/panfrost: perfcnt state flag lost when dump ioctl is interrupted by signal - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] drm/panfrost: perfcnt state flag lost when dump ioctl is interrupted by signal
- [Low] drm/panfrost: goto mixing with scoped_guard in panfrost_perfcnt_enable_locked --

commit fdd119adb59d54bffaacf2ad45f065092d48a919
Author: Adrián Larumbe <[email protected]>
Subject: drm/panfrost: Fix races between perfcnt and reset sequence

This commit uses the reset r/w semaphore when disabling and re-enabling perfcnt configuration, and in the enable/dump ioctls where device registers are being accessed. It also expands the DRM uAPI for the perfcnt dump operation so that user space is aware of hardware resets. Finally, it bumps the DRM driver minor number.

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

[ ... ]

Quoted 1 line
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +0 -0
@@ -58,43 +70,161 @@ void panfrost_perfcnt_sample_done(struct panfrost_device *pfdev)

[ ... ]

Quoted 11 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +3 -5
+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));

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +6 -4
-	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) {
+		*state = perfcnt->state;
+		if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
+			return -EIO;
+
+		perfcnt->state = 0;

[Severity: Medium]
If wait_for_completion_interruptible_timeout() is interrupted by a signal, it returns -ERESTARTSYS. The syscall will be restarted, but the internal state (perfcnt->state) has been cleared here and is never restored on the error path.

Does this mean the restarted ioctl will read a clean state and lose the reset flag that was present before the wait?

Quoted 48 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +39 -2
+
+		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));
+
+	/* A reset might come through in the gap between the completion returning and the following
+	 * check, but because no sample was produced, we don't care to relay the state back to UM.
+	 */
 	if (!ret)
-		ret = -ETIMEDOUT;
-	else if (ret > 0)
+		return -ETIMEDOUT;
+
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		*state |= perfcnt->state;
+
+		/* UM must re-enable their session before requesting new dumps. */
+		if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
+			return -EIO;
+
+		/* 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;
+
+		/* Only when we know no re-eanble or re-dump is required, we can afford
+		 * to reset the internal state. Otherwise it must be kept so that later
+		 * ioctls know about error situations in this DUMP and work around it.
+		 */
+		perfcnt->state = 0;
+	}
+
+	if (ret > 0)
 		ret = 0;
 
 	return ret;
 }

[ ... ]

Quoted 1 line
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +0 -0
@@ -122,54 +252,30 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,

[ ... ]

Quoted 7 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +0 -5
 	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);

[ ... ]

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

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);
 
-		ret = panfrost_perfcnt_hw_enable(pfdev);
+		ret = panfrost_perfcnt_hw_enable(pfdev);
+		if (ret)
+			goto err_vunmap;

[Severity: Low]
Are we mixing goto-based cleanup with scope-based cleanup helpers here?

The Linux kernel cleanup subsystem guidelines explicitly forbid mixing goto-based error handling with __free() or guard() cleanups in the same function to prevent ownership confusion.

Should this function be updated to use either scope-based cleanups consistently, or traditional goto-based cleanups?

Quoted 9 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +3 -2
-	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+		perfcnt->user = user;
+		perfcnt->state = 0;
+	}
 
 	/* 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
maintainer
#49 lore raw

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

In reply to Adrián Larumbe
Quoted 216 lines · 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 when disabling and re-enabling perfcnt configuration, and also in the sections inside the 'enable'and 'dump' ioctls where device registers are being accessed.

On top of that, expand the DRM uAPI for the perfcnt DUMP operation so that user space can be made aware of a reset having happened, whether it succeeded or failed to restore the original configuration. UM needs to know about this condition because counter data is inaccurate after a reset, so the best approach might be simply to try again.

Finally, update driver uAPI documentation to explain the meaning of the new perfcnt dump ioctl's state flags, and bump DRM driver minor number to reflect the new DUMP IOCTL req field.

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_drv.c     |   3 +-
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 272 +++++++++++++++++++---------
 drivers/gpu/drm/panfrost/panfrost_perfcnt.h |   1 +
 include/uapi/drm/panfrost_drm.h             |  29 ++-
 5 files changed, 222 insertions(+), 85 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +2 -0
index 80d8f6aed090..47a869232e10 100644
@@ -491,6 +491,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_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,
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +90 -12
index b3f71d7fd82a..13521e078ce7 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright 2019 Collabora Ltd */
 
+#include "asm-generic/errno-base.h"
 #include <linux/completion.h>
 #include <linux/iopoll.h>
 #include <linux/iosys-map.h>
@@ -11,6 +12,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,21 +30,31 @@
 
 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;
+	u32 state;
+	bool owns_as_ref;
 };
 
 static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev)
 {
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+
 	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);
+
+	if (perfcnt->owns_as_ref) {
+		panfrost_mmu_as_put(pfdev, perfcnt->mapping->mmu);
+		perfcnt->owns_as_ref = false;
+	}
 }
 
 void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev)
@@ -58,43 +70,161 @@ void panfrost_perfcnt_sample_done(struct panfrost_device *pfdev)
 		complete(&pfdev->perfcnt->dump_comp);
 }
 
-static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
+static int panfrost_perfcnt_hw_enable(struct panfrost_device *pfdev)
+{
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	u32 cfg, as;
+	int ret;
+
+	lockdep_assert_held(&pfdev->reset.lock);
+	drm_WARN_ON(&pfdev->base, perfcnt->owns_as_ref);
+
+	ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu);
+	if (ret < 0)
+		return ret;
+
+	perfcnt->owns_as_ref = true;
+
+	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, 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) {
+		*state = perfcnt->state;
+		if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
+			return -EIO;
+
+		perfcnt->state = 0;
+
+		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));
+
+	/* A reset might come through in the gap between the completion returning and the following
+	 * check, but because no sample was produced, we don't care to relay the state back to UM.
+	 */
 	if (!ret)
-		ret = -ETIMEDOUT;
-	else if (ret > 0)
+		return -ETIMEDOUT;
+
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		*state |= perfcnt->state;

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+
+		/* UM must re-enable their session before requesting new dumps. */
+		if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
+			return -EIO;
+
+		/* 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;
+
+		/* Only when we know no re-eanble or re-dump is required, we can afford

Typo: s/re-eanble/re-enable/

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

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +4 -0
+		 * to reset the internal state. Otherwise it must be kept so that later
+		 * ioctls know about error situations in this DUMP and work around it.
+		 */
+		perfcnt->state = 0;

Am I missing something or does the above comment say we only get here when perfcnt->state is already 0? If either PANFROST_PERFCNT_SESSION_DEAD or PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET is set then we return early. So unless there's some other flag we can't get to this line unless it's already 0...

It's Friday afternoon so I might well be missing something - perhaps it will all make sense next week?

Thanks,
Steve

Quoted 287 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +85 -71
+	}
+
+	if (ret > 0)
 		ret = 0;
 
 	return ret;
 }
 
+static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev,
+					   struct drm_file *file_priv)
+{
+	struct panfrost_file_priv *user = file_priv->driver_priv;
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	struct iosys_map map = IOSYS_MAP_INIT_VADDR(perfcnt->buf);
+
+	if (user != perfcnt->user)
+		return -EINVAL;
+
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		panfrost_perfcnt_hw_disable(pfdev);
+		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_gem_mapping_put(perfcnt->mapping);
+	perfcnt->mapping = NULL;
+	pm_runtime_put_autosuspend(pfdev->base.dev);
+
+	return 0;
+}
+
 static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 					  struct drm_file *file_priv,
 					  unsigned int counterset)
 {
 	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)
+	if (perfcnt->user == user) {
+		scoped_guard(rwsem_read, &pfdev->reset.lock) {
+			if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD) {
+				ret = panfrost_perfcnt_hw_enable(pfdev);
+				if (!ret)
+					perfcnt->state = 0;
+				return ret;
+			}
+		}
+
 		return 0;
-	else if (perfcnt->user)
+	}
+
+	if (perfcnt->user)
 		return -EBUSY;
 
 	ret = pm_runtime_get_sync(pfdev->base.dev);
@@ -122,54 +252,30 @@ 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;
 
-	if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186))
-		gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff);
+		perfcnt->user = user;
+		perfcnt->state = 0;
+	}
 
 	/* The BO ref is retained by the mapping. */
 	drm_gem_object_put(&bo->base);
 
-	perfcnt->user = user;
-
 	return 0;
 
 err_vunmap:
@@ -185,30 +291,6 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev,
 	return ret;
 }
 
-static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev,
-					   struct drm_file *file_priv)
-{
-	struct panfrost_file_priv *user = file_priv->driver_priv;
-	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
-	struct iosys_map map = IOSYS_MAP_INIT_VADDR(perfcnt->buf);
-
-	if (user != perfcnt->user)
-		return -EINVAL;
-
-	panfrost_perfcnt_hw_disable(pfdev);
-
-	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);
-
-	return 0;
-}
-
 int panfrost_ioctl_perfcnt_enable(struct drm_device *dev, void *data,
 				  struct drm_file *file_priv)
 {
@@ -249,13 +331,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;
 
@@ -322,14 +407,13 @@ int panfrost_perfcnt_init(struct panfrost_device *pfdev)
 		return -ENOMEM;
 
 	perfcnt->bosize = size;
+	pfdev->perfcnt = perfcnt;
+	mutex_init(&perfcnt->lock);
+	init_completion(&perfcnt->dump_comp);
 
 	/* Start with everything disabled. */
 	panfrost_perfcnt_hw_disable(pfdev);
 
-	init_completion(&perfcnt->dump_comp);
-	mutex_init(&perfcnt->lock);
-	pfdev->perfcnt = perfcnt;
-
 	return 0;
 }
 
@@ -338,3 +422,25 @@ 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)
+		return;
+
+	/* 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);
+}
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 +28 -1
index 50d5337f35ef..7831c48c59c0 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,35 @@ struct drm_panfrost_perfcnt_enable {
 	__u32 counterset;
 };
 
+/*
+ * The next two flags describe the state of a perfcnt dump request
+ * as influenced by a device reset. They are the only values the
+ * perfcnt_dump ioctl state field can take on.
+ * Only certain state and ioctl retval combinations are legitimate.
+ */
+
+/* A new perfcnt_enable ioctl should be issued before requesting
+ * more dumps, because a HW reset failed to recreate perfcnt's
+ * original state. Otherwise further perfcnt_dump's will fail.
+ * This flag being set means ioctl's retval is always -EIO.
+ */
+#define PANFROST_PERFCNT_SESSION_DEAD (1 << 0)
+
+/* A HW reset happened before or during a sample request, and
+ * perfcnt's internal state was successfully restored. There
+ * are two possible outcomes depending on the ioctl's retval:
+ *	0: A reset happened before a dump was requested, but did
+ * nonetheless succeed. Counter values are relative to last reset.
+ *	-EAGAIN: A reset happened when a counter values sampling
+ * request was ongoing. Values are undefined so a new dump ioctl
+ * should be issued.
+ */
+#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
author maintainer
#50 lore raw

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

Quoted 231 lines · Steven Price
Quoted 216 lines · 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 when disabling and re-enabling perfcnt configuration, and also in the sections inside the 'enable'and 'dump' ioctls where device registers are being accessed.

On top of that, expand the DRM uAPI for the perfcnt DUMP operation so that user space can be made aware of a reset having happened, whether it succeeded or failed to restore the original configuration. UM needs to know about this condition because counter data is inaccurate after a reset, so the best approach might be simply to try again.

Finally, update driver uAPI documentation to explain the meaning of the new perfcnt dump ioctl's state flags, and bump DRM driver minor number to reflect the new DUMP IOCTL req field.

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_drv.c     |   3 +-
 drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 272 +++++++++++++++++++---------
 drivers/gpu/drm/panfrost/panfrost_perfcnt.h |   1 +
 include/uapi/drm/panfrost_drm.h             |  29 ++-
 5 files changed, 222 insertions(+), 85 deletions(-)
drivers/gpu/drm/panfrost/panfrost_device.c +2 -0
index 80d8f6aed090..47a869232e10 100644
@@ -491,6 +491,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_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,
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +90 -12
index b3f71d7fd82a..13521e078ce7 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright 2019 Collabora Ltd */
 
+#include "asm-generic/errno-base.h"
 #include <linux/completion.h>
 #include <linux/iopoll.h>
 #include <linux/iosys-map.h>
@@ -11,6 +12,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,21 +30,31 @@
 
 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;
+	u32 state;
+	bool owns_as_ref;
 };
 
 static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev)
 {
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+
 	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);
+
+	if (perfcnt->owns_as_ref) {
+		panfrost_mmu_as_put(pfdev, perfcnt->mapping->mmu);
+		perfcnt->owns_as_ref = false;
+	}
 }
 
 void panfrost_perfcnt_clean_cache_done(struct panfrost_device *pfdev)
@@ -58,43 +70,161 @@ void panfrost_perfcnt_sample_done(struct panfrost_device *pfdev)
 		complete(&pfdev->perfcnt->dump_comp);
 }
 
-static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
+static int panfrost_perfcnt_hw_enable(struct panfrost_device *pfdev)
+{
+	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
+	u32 cfg, as;
+	int ret;
+
+	lockdep_assert_held(&pfdev->reset.lock);
+	drm_WARN_ON(&pfdev->base, perfcnt->owns_as_ref);
+
+	ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu);
+	if (ret < 0)
+		return ret;
+
+	perfcnt->owns_as_ref = true;
+
+	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, 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) {
+		*state = perfcnt->state;
+		if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
+			return -EIO;
+
+		perfcnt->state = 0;
+
+		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));
+
+	/* A reset might come through in the gap between the completion returning and the following
+	 * check, but because no sample was produced, we don't care to relay the state back to UM.
+	 */
 	if (!ret)
-		ret = -ETIMEDOUT;
-	else if (ret > 0)
+		return -ETIMEDOUT;
+
+	scoped_guard(rwsem_read, &pfdev->reset.lock) {
+		*state |= perfcnt->state;

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

Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +10 -0
+
+		/* UM must re-enable their session before requesting new dumps. */
+		if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
+			return -EIO;
+
+		/* 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;
+
+		/* Only when we know no re-eanble or re-dump is required, we can afford

Typo: s/re-eanble/re-enable/

Quoted 11 lines

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

Quoted 4 lines
drivers/gpu/drm/panfrost/panfrost_perfcnt.c +4 -0
+		 * to reset the internal state. Otherwise it must be kept so that later
+		 * ioctls know about error situations in this DUMP and work around it.
+		 */
+		perfcnt->state = 0;

Am I missing something or does the above comment say we only get here when perfcnt->state is already 0? If either PANFROST_PERFCNT_SESSION_DEAD or PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET is set then we return early. So unless there's some other flag we can't get to this line unless it's already 0...

You're right, this is pretty much a NOP. This was me trying to fix an issue with a previous revision in a way that only added cruft.
Doing nothing after checks for both flags would be the right thing:
- If PANFROST_PERFCNT_SESSION_DEAD is set, then we need to go through the enable ioctl() so that the state variable will be reset and perfcnt configuration reestablished.
- If PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET is set, then another dump should be attempted, and when getting back to the beginning of this function, state will be again reset. By then, UM should've taken note of a reset having happened, so there's no trouble in resetting it after a new dump.

Quoted 5 lines

It's Friday afternoon so I might well be missing something - perhaps it will all make sense next week?

Thanks,
Steve