Skip to contentSkip to conversation

amdgpu: implement Performance Monitor Counter support

PATCH Superseded Zhu Lingshan 1 participant 14 messages Oct 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover amdgpu: implement Performance Monitor Counter support

This series implements PMC support for amdgpu. Including two parts:
1) PMC Enablement
2) Profiler Reservation

The user space should take the reservation, then set PMC.

Changes from v1:
1) remove per-IP set_mqd_perfcount, use init_mqd with modify == true instead
2) squash amdgpu_userq_xa_find into the caller patch
3) keep CWSR registers when modify mqd for GFX12
4) minor improvements

Zhu Lingshan (13):
drm/amdgpu: implement PMC ioctl framework drm/amdgpu: add device-wide PMC config drm/amdgpu: introduce PMC ioctl handler drm/amdgpu: implement reservation mechanism for PMC drm/amdgpu: set device-wide PMC enablement indicator drm/amdgpu/gfx11: set mqd->perfcount from mqd_prop drm/amdgpu/gfx12: set mqd->perfcount from mqd_prop drm/amdgpu/gfx12.1: set mqd->perfcount from mqd_prop drm/amdgpu/gfx11: set pmc_supported for GFX11 family drm/amdgpu/gfx12: set pmc_supported for GFX12 family drm/amdgpu/gfx12: keep CWSR registers when modify mqd drm/amdgpu: implement device PMC handler drm/amdgpu: set userq perf state before publish it

 drivers/gpu/drm/amd/amdgpu/Makefile          |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu.h          |   9 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c   |   3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c      |   4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h      |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 183 +++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h |  38 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c    | 164 ++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h    |   2 +
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c       |   3 +
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c       |   9 +-
 drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c       |   1 +
 include/uapi/drm/amdgpu_drm.h                |  36 ++++
 13 files changed, 451 insertions(+), 5 deletions(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/13 drm/amdgpu: implement PMC ioctl framework

This commit implements basic ioctl framework for PMC, including basic operations.

This commit also implements AMDGPU_PROFILER_OP_VERSION which report profiler version to the user space.

---
 drivers/gpu/drm/amd/amdgpu/Makefile          |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c      |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 44 ++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h | 33 +++++++++++++++
 include/uapi/drm/amdgpu_drm.h                | 36 ++++++++++++++++
 5 files changed, 116 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index ec199715ee76..7ce3d682eb53 100644
@@ -72,7 +72,7 @@ amdgpu-y += amdgpu_device.o amdgpu_reg_access.o amdgpu_doorbell_mgr.o amdgpu_kms
 	amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o amdgpu_lsdma.o amdgpu_lockdep.o \
 	amdgpu_ring_mux.o amdgpu_xcp.o amdgpu_seq64.o amdgpu_dev_coredump.o \
 	amdgpu_cper.o amdgpu_userq_fence.o amdgpu_eviction_fence.o amdgpu_ip.o \
-	amdgpu_wb.o amdgpu_trap.o amdgpu_events.o
+	amdgpu_wb.o amdgpu_trap.o amdgpu_events.o amdgpu_profiler.o
 
 amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index a93ee106ab92..61ce43dcad68 100644
@@ -55,6 +55,7 @@
 #include "amdgpu_trap.h"
 #include "amdgpu_userq_fence.h"
 #include "../amdxcp/amdgpu_xcp_drv.h"
+#include "amdgpu_profiler.h"
 
 /*
  * KMS wrapper.
@@ -3096,6 +3097,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_WAIT, amdgpu_userq_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_HANDLES, amdgpu_gem_list_handles_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_PROC_OPTIONS, amdgpu_proc_options_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_PROFILER, amdgpu_profiler_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 
 static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c new file +44 -0

44 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h new file +33 -0
new file mode 100644
index 000000000000..c27b5f9b3ff0
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright 2026 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __AMDGPU_PROFILER_H__
+#define __AMDGPU_PROFILER_H__
+
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
+
+int amdgpu_profiler_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
+
+#endif /* __AMDGPU_PROFILER_H__ */
include/uapi/drm/amdgpu_drm.h +36 -0
index 9e5fa6f53e59..b4f160d1ded4 100644
@@ -60,6 +60,7 @@ extern "C" {
 #define DRM_AMDGPU_GEM_LIST_HANDLES	0x19
 #define DRM_AMDGPU_PROC_OPTIONS		0x1A
 #define DRM_AMDGPU_TRAP			0x1B
+#define DRM_AMDGPU_PROFILER		0x1C
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -83,6 +84,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)
 #define DRM_IOCTL_AMDGPU_PROC_OPTIONS	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_PROC_OPTIONS, struct drm_amdgpu_proc_options)
 #define DRM_IOCTL_AMDGPU_TRAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_TRAP, union drm_amdgpu_trap)
+#define DRM_IOCTL_AMDGPU_PROFILER	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILER, struct drm_amdgpu_profiler_args)
 
 /**
  * DOC: memory domains
@@ -1821,6 +1823,40 @@ struct drm_amdgpu_proc_options {
 	} kfd_sigbus_delay;
 };
 
+#define AMDGPU_PROFILER_VERSION_NUM	1
+
+enum amdgpu_profiler_ops {
+	AMDGPU_PROFILER_OP_VERSION = 1,
+	AMDGPU_PROFILER_OP_RESERVE = 2,
+	AMDGPU_PROFILER_OP_PMC = 3,
+};
+
+enum amdgpu_profiler_reserve_ops {
+	/* release the device reservation, only the reservation owner do this */
+	AMDGPU_PROFILER_RELEASE = 0,
+	/* reserve the device profiler */
+	AMDGPU_PROFILER_RESERVE = 1,
+	/* Clear the device profiler reservation, in case the reservation
+	 * owner can no longer release the reservation(e.g., it crashes),
+	 * This operation does not take the reservation nor touch profiler state
+	 */
+	AMDGPU_PROFILER_CLEAR_RESERVATION = 2,
+};
+
+struct drm_amdgpu_profiler_args {
+	/* enum amdgpu_profiler_ops */
+	__u8 op;
+	__u8 pad[3];
+	union {
+		/* AMDGPU_PROFILER_OP_RESERVE: enum amdgpu_profiler_reserve_ops */
+		__u8 reserve;
+		/* AMDGPU_PROFILER_OP_PMC: 1 enable PMC, 0 disable PMC */
+		__u8 perfcount_enable;
+		/* AMDGPU_PROFILER_OP_VERSION: AMDGPU_PROFILER_VERSION_NUM */
+		__u32 version;
+	};
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/13 drm/amdgpu: add device-wide PMC config

In reply to Zhu Lingshan

This commit implements a device-wide PMC config
for amdgpu, which including the profiling owner,
and the profiling enablement indicator.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        | 9 +++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 3 +++
 2 files changed, 12 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +9 -0
index e3763c197138..f1db089921a7 100644
@@ -620,6 +620,13 @@ enum amdgpu_enforce_isolation_mode {
 	AMDGPU_ENFORCE_ISOLATION_NO_CLEANER_SHADER = 3,
 };
 
+struct amdgpu_pmc_profiler {
+	struct mutex		lock;
+	struct drm_file		*owner;
+	bool			supported;
+	bool			enable;
+};
+
 struct amdgpu_device {
 	struct device			*dev;
 	struct pci_dev			*pdev;
@@ -818,6 +825,8 @@ struct amdgpu_device {
 	 */
 	struct xarray userq_doorbell_xa;
 
+	struct amdgpu_pmc_profiler	pmc_profiler;
+
 	/* df */
 	struct amdgpu_df                df;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -0
index 380989f4c965..c466f67c6556 100644
@@ -3673,6 +3673,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,
 	mutex_init(&adev->pm.stable_pstate_ctx_lock);
 	mutex_init(&adev->benchmark_mutex);
 	mutex_init(&adev->gfx.reset_sem_mutex);
+	mutex_init(&adev->pmc_profiler.lock);
 
 	/* Associate locks with lockdep classes for ordering validation */
 	amdgpu_lockdep_set_class(adev);
@@ -4244,6 +4245,8 @@ void amdgpu_device_fini_sw(struct amdgpu_device *adev)
 	kfree(adev->pci_state);
 	kfree(adev->pcie_reset_ctx.swds_pcistate);
 	kfree(adev->pcie_reset_ctx.swus_pcistate);
+
+	mutex_destroy(&adev->pmc_profiler.lock);
 }
 
 /**
Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 3/13 drm/amdgpu: introduce PMC ioctl handler

In reply to Zhu Lingshan

This commit introduces ioctl handler for PMC.

In this commit, the handler only performs
basic checkings, including permission and whether
user queues are enabled.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 35 +++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c +34 -1
index 04902696adab..134660a4a4d3 100644
@@ -23,9 +23,40 @@
  */
 
 #include <linux/errno.h>
+#include <linux/capability.h>
 #include <drm/amdgpu_drm.h>
+#include "amdgpu.h"
+#include "amdgpu_userq.h"
 #include "amdgpu_profiler.h"
 
+static int amdgpu_profiler_check_access(struct drm_device *dev)
+{
+	if (!perfmon_capable())
+		return -EPERM;
+
+	if (!amdgpu_userq_enabled(dev))
+		return -EOPNOTSUPP;
+
+	return 0;
+}
+
+static int amdgpu_profiler_pmc(struct drm_device *dev, struct drm_file *filp,
+			       bool enable)
+{
+	int ret;
+	struct amdgpu_device *adev;
+
+	adev = drm_to_adev(dev);
+	if (!adev->pmc_profiler.supported)
+		return -EOPNOTSUPP;
+
+	ret = amdgpu_profiler_check_access(dev);
+	if (ret)
+		return ret;
+
+	return -EOPNOTSUPP;
+}
+
 int amdgpu_profiler_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 {
 	struct drm_amdgpu_profiler_args *args = data;
@@ -37,7 +68,9 @@ int amdgpu_profiler_ioctl(struct drm_device *dev, void *data, struct drm_file *f
 	case AMDGPU_PROFILER_OP_RESERVE:
 		return -EOPNOTSUPP;
 	case AMDGPU_PROFILER_OP_PMC:
-		return -EOPNOTSUPP;
+		if (args->perfcount_enable > 1)
+			return -EINVAL;
+		return amdgpu_profiler_pmc(dev, filp, args->perfcount_enable);
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.53.0
#4 lore raw

PATCH v2 4/13 drm/amdgpu: implement reservation mechanism for PMC

In reply to Zhu Lingshan

This commit implements reserving (take the profiling ownership) and releasing (release the profiling ownership) of PMC.

A reservation can also be cleared by AMDGPU_PROFILER_CLEAR_RESERVATION in case the owner can no longer release it, e.g., it crashes.

Releasing is separated from reserving path by design, because the ownership needs to be kept during set_perfcount to avoid racings.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c      |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 75 +++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h |  3 +
 3 files changed, 78 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index 61ce43dcad68..e441eb171c1b 100644
@@ -2988,6 +2988,7 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
 		amdgpu_evf_mgr_flush_suspend(&fpriv->evf_mgr);
 		amdgpu_userq_mgr_fini(&fpriv->userq_mgr);
 		amdgpu_evf_mgr_fini(&fpriv->evf_mgr);
+		amdgpu_profiler_release_device(drm_to_adev(dev), file_priv);
 		drm_dev_exit(idx);
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c +74 -1

93 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h +3 -0
index c27b5f9b3ff0..048ce84f0ef2 100644
@@ -28,6 +28,9 @@
 #include <drm/drm_device.h>
 #include <drm/drm_file.h>
 
+struct amdgpu_device;
+
+int amdgpu_profiler_release_device(struct amdgpu_device *adev, struct drm_file *filp);
 int amdgpu_profiler_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
 
 #endif /* __AMDGPU_PROFILER_H__ */
Signature · 1 line
2.53.0
#5 lore raw

PATCH v2 5/13 drm/amdgpu: set device-wide PMC enablement indicator

In reply to Zhu Lingshan

This commit introduces a new function amdgpu_profiler_set_perfcount which sets the device-wide PMC enablement indicator.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c      |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 32 +++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h |  2 ++
 3 files changed, 34 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index e441eb171c1b..f10d6ac49bd7 100644
@@ -2988,6 +2988,7 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
 		amdgpu_evf_mgr_flush_suspend(&fpriv->evf_mgr);
 		amdgpu_userq_mgr_fini(&fpriv->userq_mgr);
 		amdgpu_evf_mgr_fini(&fpriv->evf_mgr);
+		amdgpu_profiler_set_perfcount(drm_to_adev(dev), file_priv, false);
 		amdgpu_profiler_release_device(drm_to_adev(dev), file_priv);
 		drm_dev_exit(idx);
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c +31 -1
index a15149720d52..276dc75f0e7d 100644
@@ -79,6 +79,36 @@ static void amdgpu_profiler_clear_reservation(struct amdgpu_device *adev)
 	mutex_unlock(&adev->pmc_profiler.lock);
 }
 
+int amdgpu_profiler_set_perfcount(struct amdgpu_device *adev,
+				  struct drm_file *filp, bool enable)
+{
+	int ret = 0;
+
+	mutex_lock(&adev->pmc_profiler.lock);
+
+	if (adev->pmc_profiler.owner != filp) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	if (amdgpu_in_reset(adev)) {
+		ret = -EIO;
+		goto out;
+	}
+
+	if (adev->userq_halt_for_enforce_isolation) {
+		ret = -EAGAIN;
+		goto out;
+	}
+
+	adev->pmc_profiler.enable = enable;
+
+out:
+	mutex_unlock(&adev->pmc_profiler.lock);
+
+	return ret;
+}
+
 static int amdgpu_profiler_check_access(struct drm_device *dev)
 {
 	if (!perfmon_capable())
@@ -127,7 +157,7 @@ static int amdgpu_profiler_pmc(struct drm_device *dev, struct drm_file *filp,
 	if (ret)
 		return ret;
 
-	return -EOPNOTSUPP;
+	return amdgpu_profiler_set_perfcount(adev, filp, enable);
 }
 
 int amdgpu_profiler_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h +2 -0
index 048ce84f0ef2..532aa7881191 100644
@@ -30,6 +30,8 @@
 
 struct amdgpu_device;
 
+int amdgpu_profiler_set_perfcount(struct amdgpu_device *adev, struct drm_file *filp,
+				  bool enable);
 int amdgpu_profiler_release_device(struct amdgpu_device *adev, struct drm_file *filp);
 int amdgpu_profiler_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
 
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 6/13 drm/amdgpu/gfx11: set mqd->perfcount from mqd_prop

In reply to Zhu Lingshan

This commit sets GFX11 mqd->compute_perfcount_enable from a given amdgpu_mqd_prop

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h | 2 ++
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 1 +
 2 files changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +2 -0
index 373c52b7022d..823ec6ed2ecb 100644
@@ -480,6 +480,8 @@ struct amdgpu_mqd_prop {
 	uint32_t ctx_save_area_size;
 	uint32_t ctl_stack_size;
 	bool modify;
+	/* PMC compute_perfcount_enable that programmed into this queue's MQD */
+	bool perfcount_enable;
 };
 
 struct amdgpu_mqd {
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +1 -0
index 4b5faecf13b9..e5ff9005d204 100644
@@ -4326,6 +4326,7 @@ static int gfx_v11_0_compute_mqd_init(struct amdgpu_device *adev, void *m,
 
 	mqd->header = 0xC0310800;
 	mqd->compute_pipelinestat_enable = 0x00000001;
+	mqd->compute_perfcount_enable = prop->perfcount_enable;
 	mqd->compute_static_thread_mgmt_se0 = 0xffffffff;
 	mqd->compute_static_thread_mgmt_se1 = 0xffffffff;
 	mqd->compute_static_thread_mgmt_se2 = 0xffffffff;
Signature · 1 line
2.53.0
#7 lore raw

PATCH v2 7/13 drm/amdgpu/gfx12: set mqd->perfcount from mqd_prop

In reply to Zhu Lingshan

This commit sets GFX12 mqd->compute_perfcount_enable from a given amdgpu_mqd_prop

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +1 -0
index 398e9c463ec6..655b1784860d 100644
@@ -3163,6 +3163,7 @@ static int gfx_v12_0_compute_mqd_init(struct amdgpu_device *adev, void *m,
 
 	mqd->header = 0xC0310800;
 	mqd->compute_pipelinestat_enable = 0x00000001;
+	mqd->compute_perfcount_enable = prop->perfcount_enable;
 	mqd->compute_static_thread_mgmt_se0 = 0xffffffff;
 	mqd->compute_static_thread_mgmt_se1 = 0xffffffff;
 	mqd->compute_static_thread_mgmt_se2 = 0xffffffff;
Signature · 1 line
2.53.0
#8 lore raw

PATCH v2 8/13 drm/amdgpu/gfx12.1: set mqd->perfcount from mqd_prop

In reply to Zhu Lingshan

This commit sets GFX12.1 mqd->compute_perfcount_enable from a given amdgpu_mqd_prop

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c +1 -0
index b19a4956c222..b1caf97f4166 100644
@@ -2170,6 +2170,7 @@ static int gfx_v12_1_compute_mqd_init(struct amdgpu_device *adev, void *m,
 
 	mqd->header = 0xC0310800;
 	mqd->compute_pipelinestat_enable = 0x00000001;
+	mqd->compute_perfcount_enable = prop->perfcount_enable;
 	mqd->compute_static_thread_mgmt_se0 = 0xffffffff;
 	mqd->compute_static_thread_mgmt_se1 = 0xffffffff;
 	mqd->compute_static_thread_mgmt_se2 = 0xffffffff;
Signature · 1 line
2.53.0
#9 lore raw

PATCH v2 9/13 drm/amdgpu/gfx11: set pmc_supported for GFX11 family

In reply to Zhu Lingshan

Some GFX11 models are capable of user queues and have mqd->compute_perfcount_enable, therefore they support PMC.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +2 -0
index e5ff9005d204..87875dbb6432 100644
@@ -1652,6 +1652,7 @@ static int gfx_v11_0_sw_init(struct amdgpu_ip_block *ip_block)
 		    adev->mes.fw_version[0] >= 120) {
 			adev->userq_funcs[AMDGPU_HW_IP_GFX] = &userq_mes_funcs;
 			adev->userq_funcs[AMDGPU_HW_IP_COMPUTE] = &userq_mes_funcs;
+			adev->pmc_profiler.supported = true;
 		}
 		break;
 	case IP_VERSION(11, 0, 1):
@@ -1664,6 +1665,7 @@ static int gfx_v11_0_sw_init(struct amdgpu_ip_block *ip_block)
 		if (0 && !adev->gfx.disable_uq) {
 			adev->userq_funcs[AMDGPU_HW_IP_GFX] = &userq_mes_funcs;
 			adev->userq_funcs[AMDGPU_HW_IP_COMPUTE] = &userq_mes_funcs;
+			adev->pmc_profiler.supported = true;
 		}
 		break;
 	default:
Signature · 1 line
2.53.0
#10 lore raw

PATCH v2 10/13 drm/amdgpu/gfx12: set pmc_supported for GFX12 family

In reply to Zhu Lingshan

Some GFX12 models are capable of user queues and have mqd->compute_perfcount_enable, therefore they support PMC.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +1 -0
index 655b1784860d..fe6e9ac7b170 100644
@@ -1439,6 +1439,7 @@ static int gfx_v12_0_sw_init(struct amdgpu_ip_block *ip_block)
 		    adev->mes.fw_version[0] >= 123) {
 			adev->userq_funcs[AMDGPU_HW_IP_GFX] = &userq_mes_funcs;
 			adev->userq_funcs[AMDGPU_HW_IP_COMPUTE] = &userq_mes_funcs;
+			adev->pmc_profiler.supported = true;
 		}
 		break;
 	default:
Signature · 1 line
2.53.0
#11 lore raw

PATCH v2 11/13 drm/amdgpu/gfx12: keep CWSR registers when modify mqd

In reply to Zhu Lingshan

This commit keeps CWSR registers when init_mqd
with modify == true

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +5 -2
index fe6e9ac7b170..e49c1f55e34d 100644
@@ -3313,8 +3313,11 @@ static int gfx_v12_0_compute_mqd_init(struct amdgpu_device *adev, void *m,
 			upper_32_bits(prop->ctx_save_area_addr);
 		mqd->cp_hqd_ctx_save_size = prop->ctx_save_area_size;
 		mqd->cp_hqd_cntl_stack_size = prop->ctl_stack_size;
-		mqd->cp_hqd_cntl_stack_offset = prop->ctl_stack_size;
-		mqd->cp_hqd_wg_state_offset = prop->ctl_stack_size;
+		/* MODIFY keeps the firmware-saved CWSR offsets; otherwise reset them */
+		if (!prop->modify) {
+			mqd->cp_hqd_cntl_stack_offset = prop->ctl_stack_size;
+			mqd->cp_hqd_wg_state_offset = prop->ctl_stack_size;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.53.0
#12 lore raw

PATCH v2 12/13 drm/amdgpu: implement device PMC handler

In reply to Zhu Lingshan

This commit implements device PMC handler

This commit introduces a new helper amdgpu_userq_xa_find. This helper finds a queue from a XArray which has the lowest index that is at least the given index, and hold its kref.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c    | 134 +++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h    |   2 +
 3 files changed, 140 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c +4 -1
index 276dc75f0e7d..41826f4e7dac 100644
@@ -106,7 +106,10 @@ int amdgpu_profiler_set_perfcount(struct amdgpu_device *adev,
 out:
 	mutex_unlock(&adev->pmc_profiler.lock);
 
-	return ret;
+	if (ret)
+		return ret;
+
+	return amdgpu_set_perfcount(adev, enable);
 }
 
 static int amdgpu_profiler_check_access(struct drm_device *dev)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +134 -0

146 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index 3e19bc6103a0..8f95b2c338f7 100644
@@ -178,6 +178,8 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 						  u32 idx);
 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 						   u32 idx);
+
+int amdgpu_set_perfcount(struct amdgpu_device *adev, bool enable);
 void amdgpu_userq_reset_work(struct work_struct *work);
 void amdgpu_userq_pre_reset(struct amdgpu_device *adev);
 int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost);
Signature · 1 line
2.53.0
#13 lore raw

PATCH v2 13/13 drm/amdgpu: set userq perf state before publish it

In reply to Zhu Lingshan

This commit sets the perf state of a user queue
before publish it.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 30 +++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +28 -2
index b4a2164068e3..140f1bbbc39f 100644
@@ -686,6 +686,33 @@ static int amdgpu_userq_priority_permit(struct drm_file *filp,
 	return -EACCES;
 }
 
+static int amdgpu_userq_attach(struct amdgpu_device *adev,
+			       struct amdgpu_usermode_queue *queue,
+			       u64 index)
+{
+	struct amdgpu_mqd *mqd_funcs = &adev->mqds[queue->queue_type];
+	int r = 0;
+
+	mutex_lock(&adev->pmc_profiler.lock);
+
+	if (adev->pmc_profiler.enable &&
+	    queue->queue_type == AMDGPU_HW_IP_COMPUTE) {
+		queue->userq_prop->perfcount_enable = true;
+		r = mqd_funcs->init_mqd(adev, queue->mqd.cpu_ptr,
+					queue->userq_prop);
+	}
+
+	if (r)
+		drm_dbg_driver(adev_to_drm(adev), "Failed to init mqd for user queue %llu\n", index);
+	else
+		r = xa_insert_irq(&adev->userq_doorbell_xa, index, queue,
+				  GFP_KERNEL);
+
+	mutex_unlock(&adev->pmc_profiler.lock);
+
+	return r;
+}
+
 static int
 amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 {
@@ -785,8 +812,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	/* Update VM owner at userq submit-time for page-fault attribution. */
 	amdgpu_vm_set_task_info(&fpriv->vm);
 
-	r = xa_insert_irq(&adev->userq_doorbell_xa, index, queue,
-			  GFP_KERNEL);
+	r = amdgpu_userq_attach(adev, queue, index);
 	if (r)
 		goto clean_mqd;
 
Signature · 1 line
2.53.0