Skip to contentSkip to conversation

drm/amdgpu: add UAPI to support profiler

PATCH Superseded James Zhu 3 participants 20 messages Feb 20 – Mar 18

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

James Zhu

author
#0 lore raw

PATCH 1/17 drm/amdgpu: add UAPI to support profiler

which contains all profilers' kernel driver changes.

---
 include/uapi/drm/amdgpu_drm.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/uapi/drm/amdgpu_drm.h +16 -0
index 1a961f62724e..dfc98d6d98a8 100644
@@ -58,6 +58,7 @@ extern "C" {
 #define DRM_AMDGPU_USERQ_SIGNAL		0x17
 #define DRM_AMDGPU_USERQ_WAIT		0x18
 #define DRM_AMDGPU_GEM_LIST_HANDLES	0x19
+#define DRM_AMDGPU_PROFILER			0x20
 
 #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)
@@ -79,6 +80,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_USERQ_SIGNAL	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_SIGNAL, struct drm_amdgpu_userq_signal)
 #define DRM_IOCTL_AMDGPU_USERQ_WAIT	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_WAIT, struct drm_amdgpu_userq_wait)
 #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_PROFILER	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILER, struct drm_amdgpu_profiler_args)
 
 /**
  * DOC: memory domains
@@ -1708,6 +1710,20 @@ struct drm_color_ctm_3x4 {
 	__u64 matrix[12];
 };
 
+/*
+ * Supported Profiler Operations
+ */
+enum drm_amdgpu_profiler_ops {
+	AMDGPU_PROFILER_VERSION = 0,
+};
+
+struct drm_amdgpu_profiler_args {
+	__u32 op;				/* amdgpu_profiler_op */
+	union {
+		__u32 version;		/* lower 16 bit: minor. higher 16 bit: major */
+	};
+};
+
 #if defined(__cplusplus)
 }
 #endif
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/17 drm/amdgpu: add UAPI to support profiler/SPM

which does stream performance monitoring.

---
 include/uapi/drm/amdgpu_drm.h | 93 +++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
include/uapi/drm/amdgpu_drm.h +93 -0
index dfc98d6d98a8..83f116d61df9 100644
@@ -1710,17 +1710,110 @@ struct drm_color_ctm_3x4 {
 	__u64 matrix[12];
 };
 
+/*
+ * Supported SPM (Stream Performance Monitor) Operations
+ */
+/**
+ * drm_amdgpu_spm_op - SPM ioctl operations
+ *
+ * @AMDGPU_SPM_OP_ACQUIRE: acquire exclusive access to SPM
+ * @AMDGPU_SPM_OP_RELEASE: release exclusive access to SPM
+ * @AMDGPU_SPM_OP_SET_DEST_BUF: set or unset destination buffer for SPM streaming
+ */
+enum drm_amdgpu_spm_op {
+	AMDGPU_SPM_OP_ACQUIRE,
+	AMDGPU_SPM_OP_RELEASE,
+	AMDGPU_SPM_OP_SET_DEST_BUF
+};
+
+/**
+ * drm_amdgpu_spm_args - Arguments for SPM ioctl
+ *
+ * @op[in]:            specifies the operation to perform
+ * @dst_buf[in]:       used for the address of the destination buffer
+ *                      in @AMDGPU_SPM_SET_DEST_BUFFER
+ * @buf_size[in]:      size of the destination buffer
+ * @timeout[in/out]:   [in]: timeout in milliseconds, [out]: amount of time left
+ *                      `in the timeout window
+ * @bytes_copied[out]: total amount of data that was copied to the previous dest_buf
+ * @has_data_loss:     total count for sub-block which has data loss
+ *
+ * This ioctl performs different functions depending on the @op parameter.
+ *
+ * AMDGPU_SPM_OP_ACQUIRE
+ * ------------------------
+ *
+ * Acquires exclusive access of SPM on the specified for the calling process.
+ * This must be called before using AMDGPU_SPM_OP_SET_DEST_BUF.
+ *
+ * AMDGPU_SPM_OP_RELEASE
+ * ------------------------
+ *
+ * Releases exclusive access of SPM on the specified for the calling process,
+ * which allows another process to acquire it in the future.
+ *
+ * AMDGPU_SPM_OP_SET_DEST_BUF
+ * -----------------------------
+ *
+ * If @dst_buf is NULL, the destination buffer address is unset and copying of counters
+ * is stopped.
+ *
+ * If @dst_buf is not NULL, it specifies the pointer to a new destination buffer.
+ * @buf_size specifies the size of the buffer.
+ *
+ * If @timeout is non-0, the call will wait for up to @timeout ms for the previous
+ * buffer to be filled. If previous buffer to be filled before timeout, the @timeout
+ * will be updated value with the time remaining. If the timeout is exceeded, the function
+ * copies any partial data available into the previous user buffer and returns success.
+ * The amount of valid data in the previous user buffer is indicated by @bytes_copied.
+ *
+ * If @timeout is 0, the function immediately replaces the previous destination buffer
+ * without waiting for the previous buffer to be filled. That means the previous buffer
+ * may only be partially filled, and @bytes_copied will indicate how much data has been
+ * copied to it.
+ *
+ * If data was lost, e.g. due to a ring buffer overflow, @has_data_loss will be non-0.
+ *
+ * Returns negative error code on failure, 0 on success.
+ */
+struct drm_amdgpu_spm_args {
+	__u64 dest_buf;
+	__u32 buf_size;
+	__u32 op;
+	__u32 timeout;
+	__u32 bytes_copied;
+	__u32 has_data_loss;
+	__u32 pad;
+};
+
+/**
+ * drm_amdgpu_spm_buffer_header - SPM Buffer header for drm_amdgpu_spm_args->dest_buf
+ *
+ * @version        [out]: spm version
+ * @bytes_copied   [out]: amount of data for each sub-block
+ * @has_data_loss: [out]: boolean indicating whether data was lost for each sub-block
+ *                        (e.g. due to a ring-buffer overflow)
+ */
+struct drm_amdgpu_spm_buffer_header {
+	__u32 version; /* 0-23: minor 24-31: major */
+	__u32 bytes_copied;
+	__u32 has_data_loss;
+	__u32 reserved[5];
+};
+
 /*
  * Supported Profiler Operations
  */
 enum drm_amdgpu_profiler_ops {
 	AMDGPU_PROFILER_VERSION = 0,
+	AMDGPU_PROFILER_SPM,
 };
 
 struct drm_amdgpu_profiler_args {
 	__u32 op;				/* amdgpu_profiler_op */
 	union {
 		__u32 version;		/* lower 16 bit: minor. higher 16 bit: major */
+		struct drm_amdgpu_spm_args spm;
 	};
 };
 
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 3/17 drm/amdgpu: add profiler manager initialization and release

In reply to James Zhu

when render node open and close.

---
 drivers/gpu/drm/amd/amdgpu/Makefile          |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu.h          |  3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c   |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c      |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c      |  4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 89 ++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h | 51 +++++++++++
 7 files changed, 151 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 +2 -1
index ebe08947c5a3..835900318353 100644
@@ -67,7 +67,8 @@ amdgpu-y += amdgpu_device.o amdgpu_doorbell_mgr.o amdgpu_kms.o \
 	amdgpu_fw_attestation.o amdgpu_securedisplay.o \
 	amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o amdgpu_lsdma.o \
 	amdgpu_ring_mux.o amdgpu_xcp.o amdgpu_seq64.o amdgpu_aca.o amdgpu_dev_coredump.o \
-	amdgpu_cper.o amdgpu_userq_fence.o amdgpu_eviction_fence.o amdgpu_ip.o
+	amdgpu_cper.o amdgpu_userq_fence.o amdgpu_eviction_fence.o amdgpu_ip.o \
+	amdgpu_profiler.o
 
 amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -0
index 314a498c5726..bf3ad584dd41 100644
@@ -115,6 +115,7 @@
 #include "amdgpu_seq64.h"
 #include "amdgpu_reg_state.h"
 #include "amdgpu_userq.h"
+#include "amdgpu_profiler.h"
 #include "amdgpu_eviction_fence.h"
 #if defined(CONFIG_DRM_AMD_ISP)
 #include "amdgpu_isp.h"
@@ -1319,6 +1320,8 @@ struct amdgpu_device {
 	bool                            userq_halt_for_enforce_isolation;
 	struct amdgpu_uid *uid_info;
 
+	struct amdgpu_profiler_mgr      prof_mgr;
+
 	/* KFD
 	 * Must be last --ends in a flexible-array member.
 	 */
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index 962380af4ee9..e96333efbcf0 100644
@@ -4557,6 +4557,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,
 	mutex_init(&adev->gfx.userq_sch_mutex);
 	mutex_init(&adev->gfx.workload_profile_mutex);
 	mutex_init(&adev->vcn.workload_profile_mutex);
+	mutex_init(&adev->prof_mgr.mutex);
 
 	amdgpu_device_init_apu_flags(adev);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index cee90f9e58a9..ed58931de804 100644
@@ -2936,6 +2936,7 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)
 		fpriv->evf_mgr.fd_closing = true;
 		amdgpu_eviction_fence_destroy(&fpriv->evf_mgr);
 		amdgpu_userq_mgr_fini(&fpriv->userq_mgr);
+		amdgpu_profiler_mgr_fini(fpriv_to_prof_mgr(fpriv), fpriv);
 		drm_dev_exit(idx);
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4 -0
index 75976ca00ccf..6ea4d52c8634 100644
@@ -1573,6 +1573,10 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 	if (r)
 		DRM_WARN("Can't setup usermode queues, use legacy workload submission only\n");
 
+	r = amdgpu_profiler_mgr_init(fpriv_to_prof_mgr(fpriv), fpriv);
+	if (r)
+		DRM_WARN("Can't setup profiler\n");
+
 	r = amdgpu_eviction_fence_init(&fpriv->evf_mgr);
 	if (r)
 		goto error_vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c new file +89 -0

89 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 4/17 drm/amdgpu: implement profiler ioctl

In reply to James Zhu

operation AMDGPU_PROFILER_VERSION.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c      |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 25 ++++++++++++++++++++
 2 files changed, 26 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index ed58931de804..18d83f0c47a7 100644
@@ -3045,6 +3045,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_SIGNAL, amdgpu_userq_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	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_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 +25 -0
index 98c04ab01132..5e4a1c36198c 100644
@@ -32,6 +32,31 @@
 #define AMDGPU_PROFILER_VERSION_MAJOR 1
 #define AMDGPU_PROFILER_VERSION_MINOR 1
 
+int amdgpu_profiler_ioctl(
+		struct drm_device *dev,
+		void __user *data,
+		struct drm_file *filp)
+{
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_device *adev = fpriv_to_adev(fpriv);
+	struct drm_amdgpu_profiler_args *args = data;
+
+	dev_dbg(adev->dev, "Profiler IOCTL op %d on render node %d xcp %d",
+		args->op, adev->ddev.render->index, AMDGPU_XCP_ID(fpriv->xcp_id));
+
+	switch (args->op) {
+	case AMDGPU_PROFILER_VERSION:
+		args->version = AMDGPU_PROFILER_VERSION_MAJOR << 16 |
+					AMDGPU_PROFILER_VERSION_MINOR;
+		return 0;
+
+	default:
+		dev_err(adev->dev, "Invalid option: %i", args->op);
+		return -EINVAL;
+	}
+	return -EINVAL;
+}
+
 int amdgpu_profiler_mgr_init(
 		struct amdgpu_profiler_mgr *prof_mgr,
 		struct amdgpu_fpriv *fpriv)
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 5/17 drm/amdgpu: add amdgpu_vm_alloc_gtt_mem and amdgpu_vm_free_gtt_mem

In reply to James Zhu

to support GTT memory allocation and free.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 89 ++++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  5 ++
 2 files changed, 94 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +89 -0
index 1fab953e9a03..4c05483e90d0 100644
@@ -3207,3 +3207,92 @@ void amdgpu_vm_print_task_info(struct amdgpu_device *adev,
 		task_info->process_name, task_info->tgid,
 		task_info->task.comm, task_info->task.pid);
 }
+
+int amdgpu_vm_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
+				void **mem_obj, uint64_t *gpu_addr,
+				void **cpu_ptr, bool cp_mqd_gfx9, bool is_uswc_mode)
+{
+	struct amdgpu_bo *bo = NULL;
+	struct amdgpu_bo_param bp;
+	int r;
+	void *cpu_ptr_tmp = NULL;
+
+	memset(&bp, 0, sizeof(bp));
+	bp.size = size;
+	bp.byte_align = PAGE_SIZE;
+	bp.domain = AMDGPU_GEM_DOMAIN_GTT;
+	if (is_uswc_mode)
+		bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
+	else
+		bp.flags = 0;
+	bp.type = ttm_bo_type_kernel;
+	bp.resv = NULL;
+	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
+
+	if (cp_mqd_gfx9)
+		bp.flags |= AMDGPU_GEM_CREATE_CP_MQD_GFX9;
+
+	r = amdgpu_bo_create(adev, &bp, &bo);
+	if (r) {
+		dev_err(adev->dev,
+			"failed to allocate BO for amdkfd (%d)\n", r);
+		return r;
+	}
+
+	/* map the buffer */
+	r = amdgpu_bo_reserve(bo, true);
+	if (r) {
+		dev_err(adev->dev, "(%d) failed to reserve bo for amdkfd\n", r);
+		goto allocate_mem_reserve_bo_failed;
+	}
+
+	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+	if (r) {
+		dev_err(adev->dev, "(%d) failed to pin bo for amdkfd\n", r);
+		goto allocate_mem_pin_bo_failed;
+	}
+
+	r = amdgpu_ttm_alloc_gart(&bo->tbo);
+	if (r) {
+		dev_err(adev->dev, "%p bind failed\n", bo);
+		goto allocate_mem_kmap_bo_failed;
+	}
+
+	r = amdgpu_bo_kmap(bo, &cpu_ptr_tmp);
+	if (r) {
+		dev_err(adev->dev,
+			"(%d) failed to map bo to kernel for amdkfd\n", r);
+		goto allocate_mem_kmap_bo_failed;
+	}
+
+	*mem_obj = bo;
+	*gpu_addr = amdgpu_bo_gpu_offset(bo);
+	*cpu_ptr = cpu_ptr_tmp;
+
+	amdgpu_bo_unreserve(bo);
+
+	return 0;
+
+allocate_mem_kmap_bo_failed:
+	amdgpu_bo_unpin(bo);
+allocate_mem_pin_bo_failed:
+	amdgpu_bo_unreserve(bo);
+allocate_mem_reserve_bo_failed:
+	amdgpu_bo_unref(&bo);
+
+	return r;
+}
+
+void amdgpu_vm_free_gtt_mem(struct amdgpu_device *adev, void **mem_obj)
+{
+	struct amdgpu_bo **bo = (struct amdgpu_bo **) mem_obj;
+
+	if (!bo || !*bo)
+		return;
+
+	(void)amdgpu_bo_reserve(*bo, true);
+	amdgpu_bo_kunmap(*bo);
+	amdgpu_bo_unpin(*bo);
+	amdgpu_bo_unreserve(*bo);
+	amdgpu_bo_unref(bo);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +5 -0
index fe9f759fdbe9..854cfaf0cd42 100644
@@ -715,4 +715,9 @@ void amdgpu_vm_print_task_info(struct amdgpu_device *adev,
 #define amdgpu_vm_bo_va_for_each_invalid_mapping(bo_va, mapping) \
 		list_for_each_entry(mapping, &(bo_va)->invalids, list)
 
+int amdgpu_vm_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
+				void **mem_obj, uint64_t *gpu_addr,
+				void **cpu_ptr, bool mqd_gfx9, bool is_uswc_mode);
+void amdgpu_vm_free_gtt_mem(struct amdgpu_device *adev, void **mem_obj);
+
 #endif
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 6/17 drm/amdgpu: add RLC SPM interface to

In reply to James Zhu

support stream performance monitor.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 10 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.c | 72 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.h |  6 +++
 3 files changed, 88 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +10 -0
index eebad3378352..685ebbacf14a 100644
@@ -164,6 +164,15 @@ struct amdgpu_kiq {
 	void			*mqd_backup;
 };
 
+struct spm_funcs {
+	void (*start)(struct amdgpu_device *adev, int xcc_id);
+	void (*stop)(struct amdgpu_device *adev, int xcc_id);
+	void (*set_rdptr)(struct amdgpu_device *adev, int xcc_id, u32 rptr);
+	void (*set_spm_perfmon_ring_buf)(struct amdgpu_device *adev, int xcc_id, u64 gpu_rptr, u32 size);
+	/* Packet sizes */
+	int set_spm_config_size;
+};
+
 /*
  * GFX configurations
  */
@@ -416,6 +425,7 @@ struct amdgpu_gfx {
 	struct amdgpu_mec_bitmap	mec_bitmap[AMDGPU_MAX_GC_INSTANCES];
 	struct amdgpu_kiq		kiq[AMDGPU_MAX_GC_INSTANCES];
 	struct amdgpu_imu		imu;
+	const struct spm_funcs		*spmfuncs;
 	bool				rs64_enable; /* firmware format */
 	const struct firmware		*me_fw;	/* ME firmware */
 	uint32_t			me_fw_version;
drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.c +72 -0

75 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.h +6 -0
index 3e2d2e333907..b526f256ad30 100644
@@ -361,4 +361,10 @@ void amdgpu_gfx_rlc_fini(struct amdgpu_device *adev);
 int amdgpu_gfx_rlc_init_microcode(struct amdgpu_device *adev,
 				  uint16_t version_major,
 				  uint16_t version_minor);
+void amdgpu_rlc_spm_cntl(struct amdgpu_device *adev, int xcc_id, bool cntl);
+int amdgpu_rlc_spm_acquire(struct amdgpu_device *adev, int xcc_id,
+		struct amdgpu_vm *vm, u64 gpu_addr, u32 size);
+void amdgpu_rlc_spm_release(struct amdgpu_device *adev, int xcc_id, struct amdgpu_vm *vm);
+void amdgpu_rlc_spm_set_rdptr(struct amdgpu_device *adev, int xcc_id, u32 rptr);
+void amdgpu_rlc_spm_interrupt(struct amdgpu_device *adev, int xcc_id);
 #endif
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH 7/17 drm/amdgpu: add profiler/spm manager initialization and release

In reply to James Zhu

when render node open and close.

---
 drivers/gpu/drm/amd/amdgpu/Makefile          |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c |  3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h | 21 +++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c      | 62 ++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h      | 33 +++++++++++
 5 files changed, 120 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
index 835900318353..e50c0b9ab4b9 100644
@@ -68,7 +68,7 @@ amdgpu-y += amdgpu_device.o amdgpu_doorbell_mgr.o amdgpu_kms.o \
 	amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o amdgpu_lsdma.o \
 	amdgpu_ring_mux.o amdgpu_xcp.o amdgpu_seq64.o amdgpu_aca.o amdgpu_dev_coredump.o \
 	amdgpu_cper.o amdgpu_userq_fence.o amdgpu_eviction_fence.o amdgpu_ip.o \
-	amdgpu_profiler.o
+	amdgpu_profiler.o amdgpu_spm.o
 
 amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
 
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c +3 -0
index 5e4a1c36198c..05ecb8e3855a 100644
@@ -82,6 +82,7 @@ int amdgpu_profiler_mgr_init(
 	mutex_init(&prof_xcp_mgr->mutex);
 	prof_xcp_mgr->xcp_id = fpriv->xcp_id;
 
+	amdgpu_spm_mgr_init(&prof_xcp_mgr->spm_mgr);
 	prof_xcp_mgr->is_init = true;
 
 	mutex_unlock(&prof_mgr->mutex);
@@ -93,6 +94,8 @@ static void amdgpu_profiler_mgr_release(struct kref *ref)
 	struct amdgpu_profiler_xcp_mgr *prof_xcp_mgr =
 		container_of(ref, struct amdgpu_profiler_xcp_mgr, ref);
 
+	amdgpu_spm_mgr_fini(&prof_xcp_mgr->spm_mgr);
+
 	mutex_destroy(&prof_xcp_mgr->mutex);
 	prof_xcp_mgr->is_init = false;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h +21 -0
index 3f4d969ea0ea..369833f6e684 100644
@@ -25,15 +25,36 @@
 #ifndef AMDGPU_PROFILER_H_
 #define AMDGPU_PROFILER_H_
 
+#include "amdgpu_spm.h"
+
 #define AMDGPU_XCP_ID(x) (x == AMDGPU_XCP_NO_PARTITION ? 0 : x)
 #define fpriv_to_prof_mgr(fpriv) (&fpriv->userq_mgr.adev->prof_mgr)
 #define fpriv_to_adev(fpriv) fpriv->userq_mgr.adev
 
+#define prof_mgr_to_adev(x) \
+	container_of(x, struct amdgpu_device, prof_mgr)
+
+#define to_prof_xcp_mgr(x, y) \
+	container_of(x, struct amdgpu_profiler_xcp_mgr, y)
+
+#define xcp_to_prof_mgr(x, y) \
+	container_of(x, struct amdgpu_profiler_mgr, y)
+
+#define mgr_to_adev(x, y) \
+({	struct amdgpu_profiler_xcp_mgr *prof_xcp_mgr = to_prof_xcp_mgr(x, y); \
+	struct amdgpu_profiler_mgr *prof_mgr = \
+		xcp_to_prof_mgr(prof_xcp_mgr, prof_xcp_mgr[prof_xcp_mgr->xcp_id]);\
+	prof_mgr_to_adev(prof_mgr); })
+
 struct amdgpu_profiler_xcp_mgr {
 	struct mutex                   mutex;
 	uint32_t                       xcp_id;
 	bool                           is_init;
 	struct kref                    ref;
+
+	union {
+		struct amdgpu_spm_mgr      spm_mgr;
+	};
 };
 
 struct amdgpu_profiler_mgr {
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c new file +62 -0

62 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h new file +33 -0
new file mode 100644
index 000000000000..e8a44fc1d278
@@ -0,0 +1,33 @@
+/*
+ * 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_SPM_H_
+#define AMDGPU_SPM_H_
+
+struct amdgpu_spm_mgr {
+	struct drm_file *file;
+};
+
+int amdgpu_spm_mgr_init(struct amdgpu_spm_mgr *spm_mgr);
+void amdgpu_spm_mgr_fini(struct amdgpu_spm_mgr *spm_mgr);
+
+#endif
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH 8/17 drm/amdgpu: add profiler/spm interrupt handler

In reply to James Zhu

for RLC SPM interface.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.c |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 18 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h |  1 +
 3 files changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.c +1 -1
index 08d62183b2e3..531d6370bb94 100644
@@ -618,5 +618,5 @@ void amdgpu_rlc_spm_release(struct amdgpu_device *adev, int xcc_id, struct amdgp
 void amdgpu_rlc_spm_interrupt(struct amdgpu_device *adev, int xcc_id)
 {
 	if (adev->kfd.dev)
-		return;	/* TODO: */
+		amdgpu_spm_interrupt(adev, xcc_id);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +18 -0
index 181aca333151..619ff4ce0773 100644
@@ -60,3 +60,21 @@ void amdgpu_spm_mgr_fini(struct amdgpu_spm_mgr *spm_mgr)
 		spm_mgr->file = NULL;
 	}
 }
+
+void amdgpu_spm_interrupt(struct amdgpu_device *adev, int xcc_id)
+{
+	uint8_t  xcp_id;
+	struct amdgpu_spm_mgr *spm_mgr;
+
+	xcp_id = adev->xcp_mgr ?
+		fls(amdgpu_xcp_get_partition(adev->xcp_mgr, AMDGPU_XCP_GFX, xcc_id)) - 1 : 0;
+
+	spm_mgr = &(adev->prof_mgr.prof_xcp_mgr[xcp_id].spm_mgr);
+
+	if (!spm_mgr) {
+		dev_dbg(adev->dev, "%s XCP ID = %d", __func__, xcp_id);
+		return;
+	}
+
+	/* TODO */
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +1 -0
index e8a44fc1d278..1460b87dc55d 100644
@@ -29,5 +29,6 @@ struct amdgpu_spm_mgr {
 
 int amdgpu_spm_mgr_init(struct amdgpu_spm_mgr *spm_mgr);
 void amdgpu_spm_mgr_fini(struct amdgpu_spm_mgr *spm_mgr);
+void amdgpu_spm_interrupt(struct amdgpu_device *adev, int xcc_id);
 
 #endif
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH 9/17 drm/amdgpu: add profiler/spm ioctl

In reply to James Zhu

for stream performance monitor.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c |  3 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c      | 14 ++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h      |  3 +++
 3 files changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c +3 -0
index 05ecb8e3855a..b760b0720591 100644
@@ -50,6 +50,9 @@ int amdgpu_profiler_ioctl(
 					AMDGPU_PROFILER_VERSION_MINOR;
 		return 0;
 
+	case AMDGPU_PROFILER_SPM:
+		return amdgpu_spm_ioctl(dev, &args->spm, filp);
+
 	default:
 		dev_err(adev->dev, "Invalid option: %i", args->op);
 		return -EINVAL;
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +14 -0
index 619ff4ce0773..e7d16359551f 100644
@@ -47,6 +47,20 @@ static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *f
 	return 0;
 }
 
+int amdgpu_spm_ioctl(struct drm_device *dev, void __user *data,
+		       struct drm_file *filp)
+{
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_device *adev = fpriv_to_adev(fpriv);
+	struct drm_amdgpu_spm_args *args = data;
+
+	switch (args->op) {
+	default:
+		dev_err(adev->dev, "Invalid option: %i\n", args->op);
+		return -EINVAL;
+	}
+}
+
 int amdgpu_spm_mgr_init(struct amdgpu_spm_mgr *spm_mgr)
 {
 	amdgpu_spm_init_device(spm_mgr);
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +3 -0
index 1460b87dc55d..3728f227bd61 100644
@@ -27,6 +27,9 @@ struct amdgpu_spm_mgr {
 	struct drm_file *file;
 };
 
+int amdgpu_spm_ioctl(struct drm_device *dev, void __user *data,
+		       struct drm_file *filp);
+
 int amdgpu_spm_mgr_init(struct amdgpu_spm_mgr *spm_mgr);
 void amdgpu_spm_mgr_fini(struct amdgpu_spm_mgr *spm_mgr);
 void amdgpu_spm_interrupt(struct amdgpu_device *adev, int xcc_id);
Signature · 1 line
2.34.1
author
#9 lore raw

PATCH 10/17 drm/amdgpu: add profiler/spm operation AMDGPU_SPM_OP_ACQUIRE

In reply to James Zhu

skeleton to provide ring buffer for SPM collecting data, and create a work queue to dump data to user buffer.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c      | 127 ++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h      |   6 +
 3 files changed, 133 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h +1 -0
index 369833f6e684..703a2a9bb8b8 100644
@@ -27,6 +27,7 @@
 
 #include "amdgpu_spm.h"
 
+#define AMDGPU_XCC_MASK(adev) ((1U << NUM_XCC(adev->gfx.xcc_mask)) - 1)
 #define AMDGPU_XCP_ID(x) (x == AMDGPU_XCP_NO_PARTITION ? 0 : x)
 #define fpriv_to_prof_mgr(fpriv) (&fpriv->userq_mgr.adev->prof_mgr)
 #define fpriv_to_adev(fpriv) fpriv->userq_mgr.adev
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +126 -1

152 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +6 -0
index 3728f227bd61..ee4f03e2f6cf 100644
@@ -25,6 +25,12 @@
 
 struct amdgpu_spm_mgr {
 	struct drm_file *file;
+
+	struct task_struct *lead_thread;
+
+	/* spm data */
+	struct amdgpu_spm_cntr *spm_cntr;
+	struct work_struct spm_work;
 };
 
 int amdgpu_spm_ioctl(struct drm_device *dev, void __user *data,
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH 11/17 drm/amdgpu: add profiler/spm operation AMDGPU_SPM_OP_SET_DEST_BUF

In reply to James Zhu

to provide user buffer for SPM collecting data, start a work queue and SPM HW to dump data, wait for buffer filled, and wake up to return data back to user.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 223 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h |   1 +
 2 files changed, 223 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +222 -1
index 1ac3668c9744..4b2c7f7b494b 100644
@@ -29,15 +29,35 @@
  * 0.1 - Initial revision
  */
 
+#define AMDGPU_SPM_MAJOR_VERSION	0
+#define AMDGPU_SPM_MINOR_VERSION	1
+
+struct spm_user_buf {
+	uint64_t __user *user_addr;
+	u32 ubufsize;
+};
+
 struct amdgpu_spm_base {
+	struct spm_user_buf ubuf;
 	u64    gpu_addr;
 	u32    ring_size;
+	u32    ring_rptr;
+	u32    size_copied;
+	u32    has_data_loss;
 	u32    *cpu_addr;
 	void   *spm_obj;
+	bool   has_user_buf;
+	bool   is_user_buf_filled;
+	bool   is_spm_started;
 };
+
 struct amdgpu_spm_cntr {
 	struct amdgpu_spm_base spm[MAX_XCP];
+	int spm_use_cnt;
 	struct mutex spm_worker_mutex;
+	wait_queue_head_t spm_buf_wq;
+	u32   have_users_buf_cnt;
+	bool  are_users_buf_filled;
 };
 static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *filp);
 static void _amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, int inst, struct drm_file *filp);
@@ -133,12 +153,16 @@ static int amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *f
 		ret = _amdgpu_spm_acquire(spm_mgr, inst, filp);
 		if (ret)
 			goto acquire_spm_failure;
+		spm_mgr->spm_cntr->spm_use_cnt++;
 	}
 
+	spm_mgr->spm_cntr->have_users_buf_cnt = 0;
 	mutex_init(&spm_mgr->spm_cntr->spm_worker_mutex);
 
+	init_waitqueue_head(&spm_mgr->spm_cntr->spm_buf_wq);
 	INIT_WORK(&spm_mgr->spm_work, amdgpu_spm_work);
 
+	spin_lock_init(&spm_mgr->spm_irq_lock);
 	spm_mgr->file = filp;
 
 	goto out;
@@ -166,6 +190,196 @@ static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *f
 	return 0;
 }
 
+static int spm_update_dest_info(struct amdgpu_spm_mgr *spm_mgr,
+				int inst, struct drm_amdgpu_spm_args *user_spm_data,
+				struct drm_amdgpu_spm_args *user_spm_ptr)
+{
+	struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]);
+	int ret = 0;
+
+	mutex_lock(&spm_mgr->spm_cntr->spm_worker_mutex);
+	if (spm->has_user_buf) {
+		struct drm_amdgpu_spm_buffer_header spm_header;
+		uint64_t __user *user_address;
+
+		user_spm_ptr->bytes_copied += spm->size_copied;
+		user_spm_ptr->has_data_loss += spm->has_data_loss;
+
+		memset(&spm_header, 0, sizeof(spm_header));
+		user_address = (uint64_t *)((uint64_t)spm->ubuf.user_addr - sizeof(spm_header));
+		spm_header.version = AMDGPU_SPM_MAJOR_VERSION << 24 |
+					AMDGPU_SPM_MINOR_VERSION;
+		spm_header.bytes_copied = spm->size_copied;
+		spm_header.has_data_loss = spm->has_data_loss;
+		spm->has_user_buf = false;
+		spm_mgr->spm_cntr->have_users_buf_cnt--;
+
+		ret = copy_to_user(user_address, &spm_header, sizeof(spm_header));
+		if (ret) {
+			ret = -EFAULT;
+			goto out;
+		}
+	}
+	if (user_spm_data->dest_buf) {
+		spm->ubuf.user_addr = (uint64_t *)user_spm_data->dest_buf;
+		spm->ubuf.ubufsize = user_spm_data->buf_size;
+		/* reserve space for drm_amdgpu_spm_buffer_header */
+		spm->ubuf.user_addr = (uint64_t *)((uint64_t)spm->ubuf.user_addr +
+					sizeof(struct drm_amdgpu_spm_buffer_header));
+		spm->ubuf.ubufsize -= sizeof(struct drm_amdgpu_spm_buffer_header);
+		spm->has_data_loss = false;
+		spm->size_copied = 0;
+		spm->is_user_buf_filled = false;
+		spm->has_user_buf = true;
+		spm_mgr->spm_cntr->are_users_buf_filled = false;
+		spm_mgr->spm_cntr->have_users_buf_cnt++;
+	}
+out:
+	mutex_unlock(&spm_mgr->spm_cntr->spm_worker_mutex);
+	return ret;
+}
+
+static int spm_wait_for_fill_awake(struct amdgpu_spm_cntr *spm_cntr,
+			struct drm_amdgpu_spm_args *user_spm_data)
+{
+	int ret = 0;
+
+	long timeout = msecs_to_jiffies(user_spm_data->timeout);
+	long start_jiffies = jiffies;
+
+	ret = wait_event_interruptible_timeout(spm_cntr->spm_buf_wq,
+				 (READ_ONCE(spm_cntr->are_users_buf_filled) == true),
+				 timeout);
+
+	switch (ret) {
+	case -ERESTARTSYS:
+		/* Subtract elapsed time from timeout so we wait that much
+		 * less when the call gets restarted.
+		 */
+		timeout -= (jiffies - start_jiffies);
+		if (timeout <= 0) {
+			ret = -ETIME;
+			timeout = 0;
+			pr_debug("[%s] interrupted by signal\n", __func__);
+		}
+		break;
+
+	case 0:
+	default:
+		timeout = ret;
+		ret = 0;
+		break;
+	}
+	user_spm_data->timeout = jiffies_to_msecs(timeout);
+
+	return ret;
+}
+
+static int amdgpu_set_dest_buffer(struct amdgpu_spm_mgr *spm_mgr, void __user *data)
+{
+	struct amdgpu_device *adev = mgr_to_adev(spm_mgr, spm_mgr);
+	struct drm_amdgpu_spm_args user_spm_data, *user_spm_ptr;
+	struct amdgpu_spm_cntr *spm_cntr;
+	bool need_schedule = false;
+	unsigned long flags;
+	u32 ubufsize;
+	int ret = 0;
+	int inst;
+
+	dev_dbg(adev->dev, "SPM start to set new destination buffer.");
+	mutex_lock(&(to_prof_xcp_mgr(spm_mgr, spm_mgr)->mutex));
+	spm_cntr = spm_mgr->spm_cntr;
+	if (spm_cntr == NULL) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	user_spm_ptr = (struct drm_amdgpu_spm_args *) data;
+	ubufsize = user_spm_ptr->buf_size / spm_cntr->spm_use_cnt;
+	ubufsize = rounddown(ubufsize, 32);
+
+	if (ubufsize  <= sizeof(struct drm_amdgpu_spm_buffer_header)) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	memcpy(&user_spm_data, user_spm_ptr, sizeof(user_spm_data));
+	user_spm_data.buf_size = ubufsize;
+
+	if (user_spm_data.timeout && spm_cntr->have_users_buf_cnt &&
+	    !READ_ONCE(spm_cntr->are_users_buf_filled)) {
+		dev_dbg(adev->dev, "SPM waiting for fill awake, timeout = %d ms.",
+				user_spm_data.timeout);
+		ret = spm_wait_for_fill_awake(spm_cntr, &user_spm_data);
+		if (ret == -ETIME) {
+			/* Copy (partial) data to user buffer after a timeout */
+			schedule_work(&spm_mgr->spm_work);
+			flush_work(&spm_mgr->spm_work);
+			/* This is not an error */
+			ret = 0;
+		} else if (ret) {
+			/* handle other errors normally, including -ERESTARTSYS */
+			goto out;
+		}
+	} else if (!user_spm_data.timeout && spm_cntr->have_users_buf_cnt) {
+		/* Copy (partial) data to user buffer */
+		schedule_work(&spm_mgr->spm_work);
+		flush_work(&spm_mgr->spm_work);
+	}
+
+	user_spm_ptr->bytes_copied = 0;
+	user_spm_ptr->has_data_loss = 0;
+	for_each_inst(inst, AMDGPU_XCC_MASK(adev)) {
+		struct amdgpu_spm_base *spm = &(spm_cntr->spm[inst]);
+
+		if (spm->has_user_buf || user_spm_data.dest_buf) {
+			/* Get info about filled space in previous output buffer.
+			 * Setup new dest buf if provided.
+			 */
+			ret = spm_update_dest_info(spm_mgr, inst, &user_spm_data, user_spm_ptr);
+			if (ret)
+				goto out;
+		}
+
+		if (user_spm_data.dest_buf) {
+			/* Start SPM if necessary*/
+			if (spm->is_spm_started == false) {
+				amdgpu_rlc_spm_cntl(adev, inst, 1);
+				spin_lock_irqsave(&spm_mgr->spm_irq_lock, flags);
+				spm->is_spm_started = true;
+				/* amdgpu_rlc_spm_cntl() will reset SPM and
+				 * wptr will become 0, adjust rptr accordingly.
+				 */
+				spm->ring_rptr = 0;
+				spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
+			} else {
+				/* If SPM was already started, there may already
+				 * be data in the ring-buffer that needs to be read.
+				 */
+				need_schedule = true;
+			}
+			user_spm_data.dest_buf += ubufsize;
+		} else {
+			amdgpu_rlc_spm_cntl(adev, inst, 0);
+			spin_lock_irqsave(&spm_mgr->spm_irq_lock, flags);
+			spm->is_spm_started = false;
+			/* amdgpu_rlc_spm_cntl() will reset SPM and wptr will become 0.
+			 * Adjust rptr accordingly
+			 */
+			spm->ring_rptr = 0;
+			spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
+		}
+	}
+
+out:
+	mutex_unlock(&(to_prof_xcp_mgr(spm_mgr, spm_mgr)->mutex));
+	if (need_schedule)
+		schedule_work(&spm_mgr->spm_work);
+
+	dev_dbg(adev->dev, "SPM finish to set new destination buffer, ret = %d.", ret);
+	return ret;
+}
+
 int amdgpu_spm_ioctl(struct drm_device *dev, void __user *data,
 		       struct drm_file *filp)
 {
@@ -180,6 +394,9 @@ int amdgpu_spm_ioctl(struct drm_device *dev, void __user *data,
 	case AMDGPU_SPM_OP_ACQUIRE:
 		return  amdgpu_spm_acquire(spm_mgr, filp);
 
+	case AMDGPU_SPM_OP_SET_DEST_BUF:
+		return  amdgpu_set_dest_buffer(spm_mgr, data);
+
 	default:
 		dev_err(adev->dev, "Invalid option: %i\n", args->op);
 		return -EINVAL;
@@ -203,6 +420,7 @@ void amdgpu_spm_mgr_fini(struct amdgpu_spm_mgr *spm_mgr)
 void amdgpu_spm_interrupt(struct amdgpu_device *adev, int xcc_id)
 {
 	uint8_t  xcp_id;
+	unsigned long flags;
 	struct amdgpu_spm_mgr *spm_mgr;
 
 	xcp_id = adev->xcp_mgr ?
@@ -215,5 +433,8 @@ void amdgpu_spm_interrupt(struct amdgpu_device *adev, int xcc_id)
 		return;
 	}
 
-	/* TODO */
+	spin_lock_irqsave(&spm_mgr->spm_irq_lock, flags);
+	if (spm_mgr->spm_cntr && spm_mgr->spm_cntr->spm[xcc_id].is_spm_started)
+		schedule_work(&spm_mgr->spm_work);
+	spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +1 -0
index ee4f03e2f6cf..075ad7eaad01 100644
@@ -31,6 +31,7 @@ struct amdgpu_spm_mgr {
 	/* spm data */
 	struct amdgpu_spm_cntr *spm_cntr;
 	struct work_struct spm_work;
+	spinlock_t spm_irq_lock;
 };
 
 int amdgpu_spm_ioctl(struct drm_device *dev, void __user *data,
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH 12/17 drm/amdgpu: add profiler/spm operation AMDGPU_SPM_OP_RELEASE

In reply to James Zhu

to stop SPM HW, flush SPM work queue, wake up all waiting queue and free SPM ring buffer

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 52 +++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +49 -3
index 4b2c7f7b494b..d84814069846 100644
@@ -180,14 +180,57 @@ static int amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *f
 
 static void _amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, int inst, struct drm_file *filp)
 {
-	/* TODO: */
+	struct amdgpu_device *adev = mgr_to_adev(spm_mgr, spm_mgr);
+	struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]);
+	unsigned long flags;
+
+	if (!spm->ring_size)
+		return;
+	amdgpu_rlc_spm_release(adev, inst, drm_priv_to_vm(filp));
+	amdgpu_vm_free_gtt_mem(adev, &(spm->spm_obj));
+
+	spin_lock_irqsave(&spm_mgr->spm_irq_lock, flags);
+	memset(spm, 0, sizeof(*spm));
+	spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
 
+	--spm_mgr->spm_cntr->spm_use_cnt;
 }
 
 static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *filp)
 {
-	/* TODO */
-	return 0;
+	struct amdgpu_device *adev = mgr_to_adev(spm_mgr, spm_mgr);
+	unsigned long flags;
+	int inst;
+	int ret = 0;
+
+	mutex_lock(&(to_prof_xcp_mgr(spm_mgr, spm_mgr)->mutex));
+	if (!spm_mgr->spm_cntr) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	for_each_inst(inst, AMDGPU_XCC_MASK(adev)) {
+		spin_lock_irqsave(&spm_mgr->spm_irq_lock, flags);
+		spm_mgr->spm_cntr->spm[inst].is_spm_started = false;
+		spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
+		amdgpu_rlc_spm_cntl(adev, inst, 0);
+	}
+	flush_work(&spm_mgr->spm_work);
+	wake_up_all(&spm_mgr->spm_cntr->spm_buf_wq);
+
+	for_each_inst(inst, AMDGPU_XCC_MASK(adev))
+		_amdgpu_spm_release(spm_mgr, inst, filp);
+	amdgpu_vmid_free_reserved(adev, drm_priv_to_vm(filp), AMDGPU_GFXHUB(0));
+
+	spin_lock_irqsave(&spm_mgr->spm_irq_lock, flags);
+	mutex_destroy(&(spm_mgr->spm_cntr->spm_worker_mutex));
+	kfree(spm_mgr->spm_cntr);
+	spm_mgr->spm_cntr = NULL;
+	spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
+
+out:
+	mutex_unlock(&(to_prof_xcp_mgr(spm_mgr, spm_mgr)->mutex));
+	return ret;
 }
 
 static int spm_update_dest_info(struct amdgpu_spm_mgr *spm_mgr,
@@ -394,6 +437,9 @@ int amdgpu_spm_ioctl(struct drm_device *dev, void __user *data,
 	case AMDGPU_SPM_OP_ACQUIRE:
 		return  amdgpu_spm_acquire(spm_mgr, filp);
 
+	case AMDGPU_SPM_OP_RELEASE:
+		return  amdgpu_spm_release(spm_mgr, filp);
+
 	case AMDGPU_SPM_OP_SET_DEST_BUF:
 		return  amdgpu_set_dest_buffer(spm_mgr, data);
 
Signature · 1 line
2.34.1
author
#12 lore raw

PATCH 13/17 drm/amdgpu: implement data dump from spm ring buffer

In reply to James Zhu

to user buffer, wake up spm buffer waiting queue to return data to user.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 118 +++++++++++++++++++++++-
 1 file changed, 117 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +117 -1
index d84814069846..7acbd966cd3e 100644
@@ -59,20 +59,136 @@ struct amdgpu_spm_cntr {
 	u32   have_users_buf_cnt;
 	bool  are_users_buf_filled;
 };
+
 static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *filp);
 static void _amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, int inst, struct drm_file *filp);
 
+static int amdgpu_spm_data_copy(struct amdgpu_spm_mgr *spm_mgr, u32 size_to_copy, int inst)
+{
+	struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]);
+	uint64_t __user *user_address;
+	uint64_t *ring_buf;
+	u32 user_buf_space_left;
+	int ret = 0;
+
+	if (spm->ubuf.user_addr == NULL)
+		return -EFAULT;
+
+	user_address = (uint64_t *)((uint64_t)spm->ubuf.user_addr + spm->size_copied);
+	/* From RLC spec, ring_rptr = 0 points to spm->cpu_addr + 0x20 */
+	ring_buf =  (uint64_t *)((uint64_t)spm->cpu_addr + spm->ring_rptr + 0x20);
+
+	if (user_address == NULL)
+		return -EFAULT;
+
+	user_buf_space_left = spm->ubuf.ubufsize - spm->size_copied;
+
+	if (size_to_copy < user_buf_space_left) {
+		ret = copy_to_user(user_address, ring_buf, size_to_copy);
+		if (ret) {
+			spm->has_data_loss = true;
+			return -EFAULT;
+		}
+		spm->size_copied += size_to_copy;
+		spm->ring_rptr += size_to_copy;
+	} else {
+		ret = copy_to_user(user_address, ring_buf, user_buf_space_left);
+		if (ret) {
+			spm->has_data_loss = true;
+			return -EFAULT;
+		}
+
+		spm->size_copied = spm->ubuf.ubufsize;
+		spm->ring_rptr += user_buf_space_left;
+		spm->is_user_buf_filled = true;
+	}
+
+	return ret;
+}
+
+static int amdgpu_spm_read_ring_buffer(struct amdgpu_spm_mgr *spm_mgr, int inst)
+{
+	struct amdgpu_device *adev = mgr_to_adev(spm_mgr, spm_mgr);
+	struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]);
+	u32 overflow_size = 0;
+	u32 size_to_copy;
+	int ret = 0;
+	u32 ring_wptr;
+
+	ring_wptr = READ_ONCE(spm->cpu_addr[0]);
+
+	/* SPM might stall if we cannot copy data out of SPM ringbuffer.
+	 * spm->has_data_loss is only a hint here since stall is only a
+	 * possibility and data loss might not happen. But it is a useful
+	 * hint for user mode profiler to take extra actions.
+	 */
+	if (!spm->has_user_buf || spm->is_user_buf_filled) {
+		spm->has_data_loss = true;
+		/* set flag due to there is no flag setup
+		 * when read ring buffer timeout.
+		 */
+		if (!spm->is_user_buf_filled)
+			spm->is_user_buf_filled = true;
+		dev_dbg(adev->dev, "[SPM#%d] [%d|%d] rptr:0x%x--wptr:0x%x", inst,
+			spm->has_user_buf, spm->is_user_buf_filled, spm->ring_rptr, ring_wptr);
+		goto exit;
+	}
+
+	if (spm->ring_rptr == ring_wptr)
+		goto exit;
+
+	if (ring_wptr > spm->ring_rptr) {
+		size_to_copy = ring_wptr - spm->ring_rptr;
+		ret = amdgpu_spm_data_copy(spm_mgr, size_to_copy, inst);
+	} else {
+		size_to_copy = spm->ring_size - spm->ring_rptr;
+		ret = amdgpu_spm_data_copy(spm_mgr, size_to_copy, inst);
+
+		/* correct counter start point */
+		if (spm->ring_size == spm->ring_rptr) {
+			if (ring_wptr == 0) {
+				/* reset rptr to start point of ring buffer */
+				spm->ring_rptr = ring_wptr;
+				goto exit;
+			}
+			spm->ring_rptr = 0;
+			size_to_copy = ring_wptr - spm->ring_rptr;
+			if (!ret)
+				ret = amdgpu_spm_data_copy(spm_mgr, size_to_copy, inst);
+		}
+	}
+
+exit:
+	amdgpu_rlc_spm_set_rdptr(adev, inst, spm->ring_rptr);
+	return ret;
+}
 
 static void amdgpu_spm_work(struct work_struct *work)
 {
 	struct amdgpu_spm_mgr *spm_mgr = container_of(work, struct amdgpu_spm_mgr, spm_work);
+	struct amdgpu_device *adev = mgr_to_adev(spm_mgr, spm_mgr);
 	struct mm_struct *mm = NULL;
 
 	mm = get_task_mm(spm_mgr->lead_thread);
 	if (mm) {
 		kthread_use_mm(mm);
 		{ /* attach mm */
-			/* TODO: dump spm ring buffer to user buffer */
+			int inst;
+
+			mutex_lock(&spm_mgr->spm_cntr->spm_worker_mutex);
+			WRITE_ONCE(spm_mgr->spm_cntr->are_users_buf_filled, false);
+			for_each_inst(inst, AMDGPU_XCC_MASK(adev)) {
+				struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]);
+
+				amdgpu_spm_read_ring_buffer(spm_mgr, inst);
+				if (spm->is_user_buf_filled)
+					WRITE_ONCE(spm_mgr->spm_cntr->are_users_buf_filled, true);
+			}
+			if (READ_ONCE(spm_mgr->spm_cntr->are_users_buf_filled)) {
+				pr_debug("SPM wake up buffer work queue.");
+				wake_up(&spm_mgr->spm_cntr->spm_buf_wq);
+			}
+			mutex_unlock(&spm_mgr->spm_cntr->spm_worker_mutex);
 		} /* detach mm */
 		kthread_unuse_mm(mm);
 		/* release the mm structure */
Signature · 1 line
2.34.1
author
#13 lore raw

PATCH 14/17 drm/amdgpu: workaround for spm ring buffer overflow

In reply to James Zhu

reserve space to avoid page fault and data loss.`

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 41 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h |  2 ++
 2 files changed, 43 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +41 -0
index 7acbd966cd3e..f09c237cc8f7 100644
@@ -60,9 +60,24 @@ struct amdgpu_spm_cntr {
 	bool  are_users_buf_filled;
 };
 
+/* used to detect SPM overflow */
+#define SPM_OVERFLOW_MAGIC        0xBEEFABCDDEADABCD
+
 static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *filp);
 static void _amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, int inst, struct drm_file *filp);
 
+static void amdgpu_spm_preset(struct amdgpu_spm_base *spm, u32 size)
+{
+	uint64_t *overflow_ptr, *overflow_end_ptr;
+
+	overflow_ptr = (uint64_t *)((uint64_t)spm->cpu_addr
+				+ spm->ring_size + 0x20);
+	overflow_end_ptr = overflow_ptr + (size >> 3);
+	/* SPM data filling is 0x20 alignment */
+	for ( ;  overflow_ptr < overflow_end_ptr; overflow_ptr += 4)
+		*overflow_ptr = SPM_OVERFLOW_MAGIC;
+}
+
 static int amdgpu_spm_data_copy(struct amdgpu_spm_mgr *spm_mgr, u32 size_to_copy, int inst)
 {
 	struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]);
@@ -141,6 +156,22 @@ static int amdgpu_spm_read_ring_buffer(struct amdgpu_spm_mgr *spm_mgr, int inst)
 		size_to_copy = ring_wptr - spm->ring_rptr;
 		ret = amdgpu_spm_data_copy(spm_mgr, size_to_copy, inst);
 	} else {
+		uint64_t *ring_start, *ring_end;
+
+		ring_start = (uint64_t *)((uint64_t)spm->cpu_addr + 0x20);
+		ring_end = ring_start + (spm->ring_size >> 3);
+		for ( ; overflow_size < spm_mgr->spm_overflow_reserved; overflow_size += 0x20) {
+			uint64_t *overflow_ptr = ring_end + (overflow_size >> 3);
+
+			if (*overflow_ptr == SPM_OVERFLOW_MAGIC)
+				break;
+		}
+		if (overflow_size)
+			dev_dbg(adev->dev,
+				"SPM ring buffer overflow size 0x%x", overflow_size);
+		/* move overflow counters into ring buffer to avoid data loss */
+		memcpy(ring_start, ring_end, overflow_size);
+
 		size_to_copy = spm->ring_size - spm->ring_rptr;
 		ret = amdgpu_spm_data_copy(spm_mgr, size_to_copy, inst);
 
@@ -159,6 +190,7 @@ static int amdgpu_spm_read_ring_buffer(struct amdgpu_spm_mgr *spm_mgr, int inst)
 	}
 
 exit:
+	amdgpu_spm_preset(spm, overflow_size);
 	amdgpu_rlc_spm_set_rdptr(adev, inst, spm->ring_rptr);
 	return ret;
 }
@@ -198,6 +230,12 @@ static void amdgpu_spm_work(struct work_struct *work)
 
 static void amdgpu_spm_init_device(struct amdgpu_spm_mgr *spm_mgr)
 {
+	struct amdgpu_device *adev = mgr_to_adev(spm_mgr, spm_mgr);
+
+	/* pre-gfx11 spm has a hardware bug to cause overflow */
+	if (adev->ip_versions[GC_HWIP][0] < IP_VERSION(11, 0, 1))
+		spm_mgr->spm_overflow_reserved = 0x400;
+
 	spm_mgr->spm_cntr = NULL;
 }
 
@@ -224,6 +262,8 @@ static int _amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, int inst, struct
 	if (ret)
 		goto out;
 
+	/* reserve space to fix spm overflow */
+	spm->ring_size -= spm_mgr->spm_overflow_reserved;
 	ret = amdgpu_rlc_spm_acquire(adev, inst, drm_priv_to_vm(filp),
 			spm->gpu_addr, spm->ring_size);
 
@@ -236,6 +276,7 @@ static int _amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, int inst, struct
 	if (ret)
 		goto rlc_spm_acquire_failure;
 
+	amdgpu_spm_preset(spm, spm_mgr->spm_overflow_reserved);
 	goto out;
 
 rlc_spm_acquire_failure:
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +2 -0
index 075ad7eaad01..f3d812fa4e2b 100644
@@ -32,6 +32,8 @@ struct amdgpu_spm_mgr {
 	struct amdgpu_spm_cntr *spm_cntr;
 	struct work_struct spm_work;
 	spinlock_t spm_irq_lock;
+	/* reserve space to fix spm overflow */
+	u32    spm_overflow_reserved;
 };
 
 int amdgpu_spm_ioctl(struct drm_device *dev, void __user *data,
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH 15/17 drm/amdgu: fix hw stall issue

In reply to James Zhu

When register RLC_SPM_PERFMON_CNTL/PERFMON_RING_MODE is set to 0x11,SPM HW fires warning interrupt when rptr reaches RLC_SPM_SEGMENT_THRESHOLD, and stalls when rptr reaches the end of ring buffer. But the HW bug causes that both stall and interrupt arise when rptr reaches RLC_SPM_SEGMENT_THRESHOLD, and this means unexpecetd data loss with early SPM HW stall arise when interrupt received. this fix uses polling mode instead to avoid expected SPM HW early stall.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 88 ++++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h |  1 +
 2 files changed, 88 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +87 -1
index f09c237cc8f7..4cfb3c49b9b8 100644
@@ -49,6 +49,7 @@ struct amdgpu_spm_base {
 	bool   has_user_buf;
 	bool   is_user_buf_filled;
 	bool   is_spm_started;
+	u32    warned_ring_rptr;
 };
 
 struct amdgpu_spm_cntr {
@@ -66,6 +67,77 @@ struct amdgpu_spm_cntr {
 static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *filp);
 static void _amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, int inst, struct drm_file *filp);
 
+static int amdgpu_spm_monitor_thread(void *param)
+{
+	struct amdgpu_spm_mgr *spm_mgr = param;
+	struct amdgpu_device *adev = mgr_to_adev(spm_mgr, spm_mgr);
+
+	allow_signal(SIGKILL);
+	while (!kthread_should_stop() &&
+			!signal_pending(spm_mgr->spm_monitor_thread) && spm_mgr->spm_cntr) {
+		bool need_schedule = false;
+		u32 inst;
+
+		usleep_range(1, 11);
+
+		if (!mutex_trylock(&spm_mgr->spm_cntr->spm_worker_mutex))
+			continue;
+
+		for_each_inst(inst, AMDGPU_XCC_MASK(adev)) {
+			struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]);
+			u32 warned_ring_rptr;
+			u32 ring_size;
+			u32 ring_rptr;
+			u32 ring_wptr;
+
+			if (!spm->is_spm_started)
+				continue;
+
+			ring_size = spm->ring_size;
+			ring_rptr = spm->ring_rptr;
+			warned_ring_rptr = spm->warned_ring_rptr;
+			ring_wptr = READ_ONCE(spm->cpu_addr[0]);
+
+			if (need_schedule || (ring_rptr != warned_ring_rptr &&
+				(ring_size + ring_wptr - ring_rptr) % ring_size >
+					(ring_size >> 1))) {
+				spm->warned_ring_rptr = ring_rptr;
+				if (!need_schedule) {
+					dev_dbg(adev->dev,
+						"[SPM#%d] soft interrupt rptr:0x%08x--wptr:0x%08x",
+						 inst, ring_rptr, ring_wptr);
+					need_schedule = true;
+				}
+			}
+		}
+		mutex_unlock(&spm_mgr->spm_cntr->spm_worker_mutex);
+		if (need_schedule)
+			schedule_work(&spm_mgr->spm_work);
+	}
+	spm_mgr->spm_monitor_thread = NULL;
+	return 0;
+}
+
+static int amdgpu_spm_monitor_thread_start(struct amdgpu_spm_mgr *spm_mgr)
+{
+	struct amdgpu_device *adev = mgr_to_adev(spm_mgr, spm_mgr);
+	char thread_name[16];
+	int ret = 0;
+
+	snprintf(thread_name, 16, "spm_%d", adev->ddev.render->index);
+	spm_mgr->spm_monitor_thread =
+		kthread_run(amdgpu_spm_monitor_thread, spm_mgr, thread_name);
+
+	if (IS_ERR(spm_mgr->spm_monitor_thread)) {
+		ret = PTR_ERR(spm_mgr->spm_monitor_thread);
+		spm_mgr->spm_monitor_thread = NULL;
+		dev_dbg(adev->dev, "Failed to create spm monitor thread %s with ret = %d.",
+			thread_name, ret);
+	}
+
+	return ret;
+}
+
 static void amdgpu_spm_preset(struct amdgpu_spm_base *spm, u32 size)
 {
 	uint64_t *overflow_ptr, *overflow_end_ptr;
@@ -152,6 +224,7 @@ static int amdgpu_spm_read_ring_buffer(struct amdgpu_spm_mgr *spm_mgr, int inst)
 	if (spm->ring_rptr == ring_wptr)
 		goto exit;
 
+	spm->warned_ring_rptr = spm->ring_rptr;
 	if (ring_wptr > spm->ring_rptr) {
 		size_to_copy = ring_wptr - spm->ring_rptr;
 		ret = amdgpu_spm_data_copy(spm_mgr, size_to_copy, inst);
@@ -277,6 +350,7 @@ static int _amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, int inst, struct
 		goto rlc_spm_acquire_failure;
 
 	amdgpu_spm_preset(spm, spm_mgr->spm_overflow_reserved);
+	spm->warned_ring_rptr = ~0;
 	goto out;
 
 rlc_spm_acquire_failure:
@@ -320,6 +394,7 @@ static int amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *f
 	INIT_WORK(&spm_mgr->spm_work, amdgpu_spm_work);
 
 	spin_lock_init(&spm_mgr->spm_irq_lock);
+	spm_mgr->spm_monitor_thread = NULL;
 	spm_mgr->file = filp;
 
 	goto out;
@@ -366,6 +441,9 @@ static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *f
 		goto out;
 	}
 
+	if (spm_mgr->spm_monitor_thread)
+		kthread_stop(spm_mgr->spm_monitor_thread);
+
 	for_each_inst(inst, AMDGPU_XCC_MASK(adev)) {
 		spin_lock_irqsave(&spm_mgr->spm_irq_lock, flags);
 		spm_mgr->spm_cntr->spm[inst].is_spm_started = false;
@@ -551,7 +629,10 @@ static int amdgpu_set_dest_buffer(struct amdgpu_spm_mgr *spm_mgr, void __user *d
 				 * wptr will become 0, adjust rptr accordingly.
 				 */
 				spm->ring_rptr = 0;
+				spm->warned_ring_rptr = ~0;
 				spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
+				if (!spm_mgr->spm_monitor_thread)
+					amdgpu_spm_monitor_thread_start(spm_mgr);
 			} else {
 				/* If SPM was already started, there may already
 				 * be data in the ring-buffer that needs to be read.
@@ -567,7 +648,10 @@ static int amdgpu_set_dest_buffer(struct amdgpu_spm_mgr *spm_mgr, void __user *d
 			 * Adjust rptr accordingly
 			 */
 			spm->ring_rptr = 0;
+			spm->warned_ring_rptr = ~0;
 			spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
+			if (spm_mgr->spm_monitor_thread)
+				kthread_stop(spm_mgr->spm_monitor_thread);
 		}
 	}
 
@@ -638,6 +722,8 @@ void amdgpu_spm_interrupt(struct amdgpu_device *adev, int xcc_id)
 
 	spin_lock_irqsave(&spm_mgr->spm_irq_lock, flags);
 	if (spm_mgr->spm_cntr && spm_mgr->spm_cntr->spm[xcc_id].is_spm_started)
-		schedule_work(&spm_mgr->spm_work);
+		spm_mgr->spm_cntr->spm[xcc_id].has_data_loss = true;
 	spin_unlock_irqrestore(&spm_mgr->spm_irq_lock, flags);
+
+	dev_dbg(adev->dev, "[SPM#%d:%d] ring buffer stall.", xcp_id, xcc_id);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +1 -0
index f3d812fa4e2b..aca627b6c2de 100644
@@ -24,6 +24,7 @@
 #define AMDGPU_SPM_H_
 
 struct amdgpu_spm_mgr {
+	struct task_struct *spm_monitor_thread;
 	struct drm_file *file;
 
 	struct task_struct *lead_thread;
Signature · 1 line
2.34.1
author
#15 lore raw

PATCH 16/17 drm/amdgpu: add profiler/spm support for gfx9

In reply to James Zhu

with spm function interface and spm irq.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |   1 +
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 135 ++++++++++++++++++++++++
 2 files changed, 136 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +1 -0
index 685ebbacf14a..cae88f6ed3bd 100644
@@ -469,6 +469,7 @@ struct amdgpu_gfx {
 	struct amdgpu_irq_src		priv_inst_irq;
 	struct amdgpu_irq_src		bad_op_irq;
 	struct amdgpu_irq_src		cp_ecc_error_irq;
+	struct amdgpu_irq_src		spm_irq;
 	struct amdgpu_irq_src		sq_irq;
 	struct amdgpu_irq_src		rlc_gc_fed_irq;
 	struct sq_work			sq_work;
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +135 -0

183 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#16 lore raw

PATCH 17/17 drm/amdgpu: add profiler/spm support for gfx9_4_3

In reply to James Zhu

with spm function interface and spm irq.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 194 ++++++++++++++++++++++--
 1 file changed, 183 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +183 -11
index 44b07785bf9c..29fd5e2413da 100644
@@ -169,6 +169,8 @@ static void gfx_v9_4_3_set_gds_init(struct amdgpu_device *adev);
 static void gfx_v9_4_3_set_rlc_funcs(struct amdgpu_device *adev);
 static int gfx_v9_4_3_get_cu_info(struct amdgpu_device *adev,
 				struct amdgpu_cu_info *cu_info);
+static void gfx_v9_4_3_update_spm_vmid_internal(struct amdgpu_device *adev,
+					      int xcc_id, unsigned int vmid);
 static void gfx_v9_4_3_xcc_set_safe_mode(struct amdgpu_device *adev, int xcc_id);
 static void gfx_v9_4_3_xcc_unset_safe_mode(struct amdgpu_device *adev, int xcc_id);
 
@@ -1065,6 +1067,13 @@ static int gfx_v9_4_3_sw_init(struct amdgpu_ip_block *ip_block)
 
 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
 
+	/* SPM */
+	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_RLC,
+			      GFX_9_0__SRCID__RLC_STRM_PERF_MONITOR_INTERRUPT,
+			      &adev->gfx.spm_irq);
+	if (r)
+		return r;
+
 	/* EOP Event */
 	r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_EOP_INTERRUPT, &adev->gfx.eop_irq);
 	if (r)
@@ -1453,10 +1462,14 @@ static void gfx_v9_4_3_init_rlcg_reg_access_ctrl(struct amdgpu_device *adev)
 
 static int gfx_v9_4_3_rlc_init(struct amdgpu_device *adev)
 {
-	/* init spm vmid with 0xf */
-	if (adev->gfx.rlc.funcs->update_spm_vmid)
-		adev->gfx.rlc.funcs->update_spm_vmid(adev, 0, NULL, 0xf);
+	int i, num_xcc;
+
+	if (amdgpu_sriov_vf(adev))
+		return 0;
 
+	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+	for (i = 0; i < num_xcc; i++)
+		adev->gfx.rlc.funcs->update_spm_vmid(adev, i, NULL, 0xf);
 	return 0;
 }
 
@@ -1631,14 +1644,15 @@ static int gfx_v9_4_3_xcc_rlc_resume(struct amdgpu_device *adev, int xcc_id)
 {
 	int r;
 
+	gfx_v9_4_3_xcc_rlc_stop(adev, xcc_id);
 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
-		gfx_v9_4_3_xcc_rlc_stop(adev, xcc_id);
 		/* legacy rlc firmware loading */
 		r = gfx_v9_4_3_xcc_rlc_load_microcode(adev, xcc_id);
 		if (r)
 			return r;
-		gfx_v9_4_3_xcc_rlc_start(adev, xcc_id);
 	}
+	gfx_v9_4_3_update_spm_vmid_internal(adev, xcc_id, 0xf);
+	gfx_v9_4_3_xcc_rlc_start(adev, xcc_id);
 
 	amdgpu_gfx_rlc_enter_safe_mode(adev, xcc_id);
 	/* disable CG */
@@ -1666,28 +1680,38 @@ static int gfx_v9_4_3_rlc_resume(struct amdgpu_device *adev)
 	return 0;
 }
 
-static void gfx_v9_4_3_update_spm_vmid(struct amdgpu_device *adev,
-					      int inst, struct amdgpu_ring *ring, unsigned int vmid)
+static void gfx_v9_4_3_update_spm_vmid_internal(struct amdgpu_device *adev,
+					      int xcc_id, unsigned int vmid)
 {
 	u32 reg, pre_data, data;
 
-	reg = SOC15_REG_OFFSET(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL);
+	reg = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL);
 	if (amdgpu_sriov_is_pp_one_vf(adev) && !amdgpu_sriov_runtime(adev))
 		pre_data = RREG32_NO_KIQ(reg);
 	else
-		pre_data = RREG32(reg);
+		pre_data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL);
 
 	data =	pre_data & (~RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK);
 	data |= (vmid & RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK) << RLC_SPM_MC_CNTL__RLC_SPM_VMID__SHIFT;
 
 	if (pre_data != data) {
 		if (amdgpu_sriov_is_pp_one_vf(adev) && !amdgpu_sriov_runtime(adev)) {
-			WREG32_SOC15_NO_KIQ(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL, data);
+			WREG32_SOC15_NO_KIQ(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL, data);
 		} else
-			WREG32_SOC15(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL, data);
+			WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL, data);
 	}
 }
 
+static void gfx_v9_4_3_update_spm_vmid(struct amdgpu_device *adev, int xcc_id,
+		struct amdgpu_ring *ring, unsigned int vmid)
+{
+	amdgpu_gfx_off_ctrl(adev, false);
+
+	gfx_v9_4_3_update_spm_vmid_internal(adev, xcc_id, vmid);
+
+	amdgpu_gfx_off_ctrl(adev, true);
+}
+
 static const struct soc15_reg_rlcg rlcg_access_gc_9_4_3[] = {
 	{SOC15_REG_ENTRY(GC, 0, regGRBM_GFX_INDEX)},
 	{SOC15_REG_ENTRY(GC, 0, regSQ_IND_INDEX)},
@@ -2373,6 +2397,7 @@ static int gfx_v9_4_3_hw_fini(struct amdgpu_ip_block *ip_block)
 	int i, num_xcc;
 
 	amdgpu_irq_put(adev, &adev->gfx.priv_reg_irq, 0);
+	amdgpu_irq_put(adev, &adev->gfx.spm_irq, 0);
 	amdgpu_irq_put(adev, &adev->gfx.priv_inst_irq, 0);
 	amdgpu_irq_put(adev, &adev->gfx.bad_op_irq, 0);
 
@@ -2507,12 +2532,112 @@ static void gfx_v9_4_3_ring_emit_gds_switch(struct amdgpu_ring *ring,
 				   (1 << (oa_size + oa_base)) - (1 << oa_base));
 }
 
+static void gfx_v9_4_3_spm_start(struct amdgpu_device *adev, int xcc_id)
+{
+	struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+	uint32_t data = 0;
+
+	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_PERFMON_CNTL);
+	data |= RLC_SPM_PERFMON_CNTL__PERFMON_RING_MODE_MASK;
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_PERFMON_CNTL), data);
+
+	data = REG_SET_FIELD(0, CP_PERFMON_CNTL, SPM_PERFMON_STATE,
+			CP_PERFMON_STATE_DISABLE_AND_RESET);
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+	/* When SPM is reset, RLC automatically resets wptr to 0.
+	 * Manually reset rptr to match this.
+	 */
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), 0);
+
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_INT_CNTL), 1);
+
+	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL);
+	data |= RLC_CLK_CNTL__RLC_SPM_CLK_CNTL_MASK;
+	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL, data);
+}
+
+static void gfx_v9_4_3_spm_stop(struct amdgpu_device *adev, int xcc_id)
+{
+	struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+	uint32_t data = 0;
+
+	data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL);
+	data &= (~RLC_CLK_CNTL__RLC_SPM_CLK_CNTL_MASK);
+	WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL, data);
+
+	data = REG_SET_FIELD(0, CP_PERFMON_CNTL, SPM_PERFMON_STATE,
+			CP_PERFMON_STATE_STOP_COUNTING);
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+	data = REG_SET_FIELD(0, CP_PERFMON_CNTL, PERFMON_STATE,
+			CP_PERFMON_STATE_DISABLE_AND_RESET);
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+	/* When SPM is reset, RLC automatically resets wptr to 0.
+	 * Manually reset rptr to match this.
+	 */
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), 0);
+}
+
+static void gfx_v9_4_3_spm_set_rdptr(struct amdgpu_device *adev, int xcc_id,  u32 rptr)
+{
+	struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), rptr);
+}
+
+static void gfx_v9_4_3_set_spm_perfmon_ring_buf(struct amdgpu_device *adev,
+					    int xcc_id, u64 gpu_addr, u32 size)
+{
+	struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false, SOC15_REG_OFFSET(GC, 0,
+			regRLC_SPM_PERFMON_RING_BASE_LO), lower_32_bits(gpu_addr));
+
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, 0,
+				regRLC_SPM_PERFMON_RING_BASE_HI), upper_32_bits(gpu_addr));
+
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id),
+				regRLC_SPM_PERFMON_RING_SIZE), size);
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id),
+				regRLC_SPM_SEGMENT_THRESHOLD), 0x1);
+
+	gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+			SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), 0);
+}
+
+static const struct spm_funcs gfx_v9_4_3_spm_funcs = {
+	.start = &gfx_v9_4_3_spm_start,
+	.stop = &gfx_v9_4_3_spm_stop,
+	.set_rdptr = &gfx_v9_4_3_spm_set_rdptr,
+	.set_spm_perfmon_ring_buf = &gfx_v9_4_3_set_spm_perfmon_ring_buf,
+	.set_spm_config_size = 30,
+};
+
+static void gfx_v9_4_3_set_spm_funcs(struct amdgpu_device *adev)
+{
+	adev->gfx.spmfuncs = &gfx_v9_4_3_spm_funcs;
+}
+
 static int gfx_v9_4_3_early_init(struct amdgpu_ip_block *ip_block)
 {
 	struct amdgpu_device *adev = ip_block->adev;
 
 	adev->gfx.num_compute_rings = min(amdgpu_gfx_get_num_kcq(adev),
 					  AMDGPU_MAX_COMPUTE_RINGS);
+	gfx_v9_4_3_set_spm_funcs(adev);
 	gfx_v9_4_3_set_kiq_pm4_funcs(adev);
 	gfx_v9_4_3_set_ring_funcs(adev);
 	gfx_v9_4_3_set_irq_funcs(adev);
@@ -2534,6 +2659,10 @@ static int gfx_v9_4_3_late_init(struct amdgpu_ip_block *ip_block)
 	if (r)
 		return r;
 
+	r = amdgpu_irq_get(adev, &adev->gfx.spm_irq, 0);
+	if (r)
+		return r;
+
 	r = amdgpu_irq_get(adev, &adev->gfx.priv_inst_irq, 0);
 	if (r)
 		return r;
@@ -3404,6 +3533,41 @@ static void gfx_v9_4_3_emit_mem_sync(struct amdgpu_ring *ring)
 	amdgpu_ring_write(ring, 0x0000000A); /* POLL_INTERVAL */
 }
 
+static int gfx_v9_4_3_spm_set_interrupt_state(struct amdgpu_device *adev,
+					     struct amdgpu_irq_src *src,
+					     unsigned int type,
+					     enum amdgpu_interrupt_state state)
+{
+	int i, num_xcc;
+
+	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+	for (i = 0; i < num_xcc; i++) {
+		switch (state) {
+		case AMDGPU_IRQ_STATE_DISABLE:
+			WREG32_SOC15(GC, GET_INST(GC, i), regRLC_SPM_INT_CNTL, 0);
+			break;
+		case AMDGPU_IRQ_STATE_ENABLE:
+			WREG32_SOC15(GC, GET_INST(GC, i), regRLC_SPM_INT_CNTL, 1);
+			break;
+		default:
+			break;
+		}
+	}
+	return 0;
+}
+
+static int gfx_v9_4_3_spm_irq(struct amdgpu_device *adev,
+			     struct amdgpu_irq_src *source,
+			     struct amdgpu_iv_entry *entry)
+{
+	int xcc_id;
+
+	xcc_id = gfx_v9_4_3_ih_to_xcc_inst(adev, entry->node_id);
+
+	amdgpu_rlc_spm_interrupt(adev, xcc_id);
+	return 0;
+}
+
 static void gfx_v9_4_3_emit_wave_limit_cs(struct amdgpu_ring *ring,
 					uint32_t pipe, bool enable)
 {
@@ -4831,11 +4995,19 @@ static const struct amdgpu_irq_src_funcs gfx_v9_4_3_priv_inst_irq_funcs = {
 	.process = gfx_v9_4_3_priv_inst_irq,
 };
 
+static const struct amdgpu_irq_src_funcs gfx_v9_4_3_spm_irq_funcs = {
+	.set = gfx_v9_4_3_spm_set_interrupt_state,
+	.process = gfx_v9_4_3_spm_irq,
+};
+
 static void gfx_v9_4_3_set_irq_funcs(struct amdgpu_device *adev)
 {
 	adev->gfx.eop_irq.num_types = AMDGPU_CP_IRQ_LAST;
 	adev->gfx.eop_irq.funcs = &gfx_v9_4_3_eop_irq_funcs;
 
+	adev->gfx.spm_irq.num_types = 1;
+	adev->gfx.spm_irq.funcs = &gfx_v9_4_3_spm_irq_funcs;
+
 	adev->gfx.priv_reg_irq.num_types = 1;
 	adev->gfx.priv_reg_irq.funcs = &gfx_v9_4_3_priv_reg_irq_funcs;
 
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH 17/17] drm/amdgpu: add profiler/spm support for gfx9_4_3

Have skimmed this patch set. Generally looks normal; another hardware block with the usual functionality.
If these pass basic tests, they're probably fine or at least not harmful.
Some general comments:
- The commit descriptions aren't very descriptive. I'd at least like to see documentation of the ioctl interface and a brief notice of what SPM is somewhere, and some more words on what each patch is doing
- Patch 5/17 adds a way to allocate GTT-type memory through amdgpu_vm. This seems sort of out of place with the rest of the patch set. Why is it here? And why can't we just use the existing amdgpu_gtt_mgr?
- Patch 14/17 reserves extra space and puts magic numbers in it to avoid a page fault. This seems like a workaround for something; at very least I'd like the commit description to describe the problem this is solving in detail. I'd also prefer if there was a solution that didn't involve poisoning entries with magic numbers and checking possibly invalid memory.

Thanks,
David

________________________________________
From: amd-gfx <[email protected]> on behalf of James Zhu <[email protected]>
Sent: Friday, February 20, 2026 2:22 PM
To: [email protected]; Deucher, Alexander
Cc: Ma, Bing; Zhu, James
Subject: [PATCH 17/17] drm/amdgpu: add profiler/spm support for gfx9_4_3

with spm function interface and spm irq.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 194 ++++++++++++++++++++++--
 1 file changed, 183 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +183 -11
index 44b07785bf9c..29fd5e2413da 100644
@@ -169,6 +169,8 @@ static void gfx_v9_4_3_set_gds_init(struct amdgpu_device *adev);
 static void gfx_v9_4_3_set_rlc_funcs(struct amdgpu_device *adev);
 static int gfx_v9_4_3_get_cu_info(struct amdgpu_device *adev,
                                struct amdgpu_cu_info *cu_info);
+static void gfx_v9_4_3_update_spm_vmid_internal(struct amdgpu_device *adev,
+                                             int xcc_id, unsigned int vmid);
 static void gfx_v9_4_3_xcc_set_safe_mode(struct amdgpu_device *adev, int xcc_id);
 static void gfx_v9_4_3_xcc_unset_safe_mode(struct amdgpu_device *adev, int xcc_id);
 
@@ -1065,6 +1067,13 @@ static int gfx_v9_4_3_sw_init(struct amdgpu_ip_block *ip_block)
 
        num_xcc = NUM_XCC(adev->gfx.xcc_mask);
 
+       /* SPM */
+       r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_RLC,
+                             GFX_9_0__SRCID__RLC_STRM_PERF_MONITOR_INTERRUPT,
+                             &adev->gfx.spm_irq);
+       if (r)
+               return r;
+
        /* EOP Event */
        r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_EOP_INTERRUPT, &adev->gfx.eop_irq);
        if (r)
@@ -1453,10 +1462,14 @@ static void gfx_v9_4_3_init_rlcg_reg_access_ctrl(struct amdgpu_device *adev)
 
 static int gfx_v9_4_3_rlc_init(struct amdgpu_device *adev)
 {
-       /* init spm vmid with 0xf */
-       if (adev->gfx.rlc.funcs->update_spm_vmid)
-               adev->gfx.rlc.funcs->update_spm_vmid(adev, 0, NULL, 0xf);
+       int i, num_xcc;
+
+       if (amdgpu_sriov_vf(adev))
+               return 0;
 
+       num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+       for (i = 0; i < num_xcc; i++)
+               adev->gfx.rlc.funcs->update_spm_vmid(adev, i, NULL, 0xf);
        return 0;
 }
 
@@ -1631,14 +1644,15 @@ static int gfx_v9_4_3_xcc_rlc_resume(struct amdgpu_device *adev, int xcc_id)
 {
        int r;
 
+       gfx_v9_4_3_xcc_rlc_stop(adev, xcc_id);
        if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
-               gfx_v9_4_3_xcc_rlc_stop(adev, xcc_id);
                /* legacy rlc firmware loading */
                r = gfx_v9_4_3_xcc_rlc_load_microcode(adev, xcc_id);
                if (r)
                        return r;
-               gfx_v9_4_3_xcc_rlc_start(adev, xcc_id);
        }
+       gfx_v9_4_3_update_spm_vmid_internal(adev, xcc_id, 0xf);
+       gfx_v9_4_3_xcc_rlc_start(adev, xcc_id);
 
        amdgpu_gfx_rlc_enter_safe_mode(adev, xcc_id);
        /* disable CG */
@@ -1666,28 +1680,38 @@ static int gfx_v9_4_3_rlc_resume(struct amdgpu_device *adev)
        return 0;
 }
 
-static void gfx_v9_4_3_update_spm_vmid(struct amdgpu_device *adev,
-                                             int inst, struct amdgpu_ring *ring, unsigned int vmid)
+static void gfx_v9_4_3_update_spm_vmid_internal(struct amdgpu_device *adev,
+                                             int xcc_id, unsigned int vmid)
 {
        u32 reg, pre_data, data;
 
-       reg = SOC15_REG_OFFSET(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL);
+       reg = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL);
        if (amdgpu_sriov_is_pp_one_vf(adev) && !amdgpu_sriov_runtime(adev))
                pre_data = RREG32_NO_KIQ(reg);
        else
-               pre_data = RREG32(reg);
+               pre_data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL);
 
        data =  pre_data & (~RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK);
        data |= (vmid & RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK) << RLC_SPM_MC_CNTL__RLC_SPM_VMID__SHIFT;
 
        if (pre_data != data) {
                if (amdgpu_sriov_is_pp_one_vf(adev) && !amdgpu_sriov_runtime(adev)) {
-                       WREG32_SOC15_NO_KIQ(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL, data);
+                       WREG32_SOC15_NO_KIQ(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL, data);
                } else
-                       WREG32_SOC15(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL, data);
+                       WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL, data);
        }
 }
 
+static void gfx_v9_4_3_update_spm_vmid(struct amdgpu_device *adev, int xcc_id,
+               struct amdgpu_ring *ring, unsigned int vmid)
+{
+       amdgpu_gfx_off_ctrl(adev, false);
+
+       gfx_v9_4_3_update_spm_vmid_internal(adev, xcc_id, vmid);
+
+       amdgpu_gfx_off_ctrl(adev, true);
+}
+
 static const struct soc15_reg_rlcg rlcg_access_gc_9_4_3[] = {
        {SOC15_REG_ENTRY(GC, 0, regGRBM_GFX_INDEX)},
        {SOC15_REG_ENTRY(GC, 0, regSQ_IND_INDEX)},
@@ -2373,6 +2397,7 @@ static int gfx_v9_4_3_hw_fini(struct amdgpu_ip_block *ip_block)
        int i, num_xcc;
 
        amdgpu_irq_put(adev, &adev->gfx.priv_reg_irq, 0);
+       amdgpu_irq_put(adev, &adev->gfx.spm_irq, 0);
        amdgpu_irq_put(adev, &adev->gfx.priv_inst_irq, 0);
        amdgpu_irq_put(adev, &adev->gfx.bad_op_irq, 0);
 
@@ -2507,12 +2532,112 @@ static void gfx_v9_4_3_ring_emit_gds_switch(struct amdgpu_ring *ring,
                                   (1 << (oa_size + oa_base)) - (1 << oa_base));
 }
 
+static void gfx_v9_4_3_spm_start(struct amdgpu_device *adev, int xcc_id)
+{
+       struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+       uint32_t data = 0;
+
+       data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_PERFMON_CNTL);
+       data |= RLC_SPM_PERFMON_CNTL__PERFMON_RING_MODE_MASK;
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_PERFMON_CNTL), data);
+
+       data = REG_SET_FIELD(0, CP_PERFMON_CNTL, SPM_PERFMON_STATE,
+                       CP_PERFMON_STATE_DISABLE_AND_RESET);
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+       /* When SPM is reset, RLC automatically resets wptr to 0.
+        * Manually reset rptr to match this.
+        */
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), 0);
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_INT_CNTL), 1);
+
+       data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL);
+       data |= RLC_CLK_CNTL__RLC_SPM_CLK_CNTL_MASK;
+       WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL, data);
+}
+
+static void gfx_v9_4_3_spm_stop(struct amdgpu_device *adev, int xcc_id)
+{
+       struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+       uint32_t data = 0;
+
+       data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL);
+       data &= (~RLC_CLK_CNTL__RLC_SPM_CLK_CNTL_MASK);
+       WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL, data);
+
+       data = REG_SET_FIELD(0, CP_PERFMON_CNTL, SPM_PERFMON_STATE,
+                       CP_PERFMON_STATE_STOP_COUNTING);
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+       data = REG_SET_FIELD(0, CP_PERFMON_CNTL, PERFMON_STATE,
+                       CP_PERFMON_STATE_DISABLE_AND_RESET);
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+       /* When SPM is reset, RLC automatically resets wptr to 0.
+        * Manually reset rptr to match this.
+        */
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), 0);
+}
+
+static void gfx_v9_4_3_spm_set_rdptr(struct amdgpu_device *adev, int xcc_id,  u32 rptr)
+{
+       struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), rptr);
+}
+
+static void gfx_v9_4_3_set_spm_perfmon_ring_buf(struct amdgpu_device *adev,
+                                           int xcc_id, u64 gpu_addr, u32 size)
+{
+       struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false, SOC15_REG_OFFSET(GC, 0,
+                       regRLC_SPM_PERFMON_RING_BASE_LO), lower_32_bits(gpu_addr));
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, 0,
+                               regRLC_SPM_PERFMON_RING_BASE_HI), upper_32_bits(gpu_addr));
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id),
+                               regRLC_SPM_PERFMON_RING_SIZE), size);
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id),
+                               regRLC_SPM_SEGMENT_THRESHOLD), 0x1);
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), 0);
+}
+
+static const struct spm_funcs gfx_v9_4_3_spm_funcs = {
+       .start = &gfx_v9_4_3_spm_start,
+       .stop = &gfx_v9_4_3_spm_stop,
+       .set_rdptr = &gfx_v9_4_3_spm_set_rdptr,
+       .set_spm_perfmon_ring_buf = &gfx_v9_4_3_set_spm_perfmon_ring_buf,
+       .set_spm_config_size = 30,
+};
+
+static void gfx_v9_4_3_set_spm_funcs(struct amdgpu_device *adev)
+{
+       adev->gfx.spmfuncs = &gfx_v9_4_3_spm_funcs;
+}
+
 static int gfx_v9_4_3_early_init(struct amdgpu_ip_block *ip_block)
 {
        struct amdgpu_device *adev = ip_block->adev;
 
        adev->gfx.num_compute_rings = min(amdgpu_gfx_get_num_kcq(adev),
                                          AMDGPU_MAX_COMPUTE_RINGS);
+       gfx_v9_4_3_set_spm_funcs(adev);
        gfx_v9_4_3_set_kiq_pm4_funcs(adev);
        gfx_v9_4_3_set_ring_funcs(adev);
        gfx_v9_4_3_set_irq_funcs(adev);
@@ -2534,6 +2659,10 @@ static int gfx_v9_4_3_late_init(struct amdgpu_ip_block *ip_block)
        if (r)
                return r;
 
+       r = amdgpu_irq_get(adev, &adev->gfx.spm_irq, 0);
+       if (r)
+               return r;
+
        r = amdgpu_irq_get(adev, &adev->gfx.priv_inst_irq, 0);
        if (r)
                return r;
@@ -3404,6 +3533,41 @@ static void gfx_v9_4_3_emit_mem_sync(struct amdgpu_ring *ring)
        amdgpu_ring_write(ring, 0x0000000A); /* POLL_INTERVAL */
 }
 
+static int gfx_v9_4_3_spm_set_interrupt_state(struct amdgpu_device *adev,
+                                            struct amdgpu_irq_src *src,
+                                            unsigned int type,
+                                            enum amdgpu_interrupt_state state)
+{
+       int i, num_xcc;
+
+       num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+       for (i = 0; i < num_xcc; i++) {
+               switch (state) {
+               case AMDGPU_IRQ_STATE_DISABLE:
+                       WREG32_SOC15(GC, GET_INST(GC, i), regRLC_SPM_INT_CNTL, 0);
+                       break;
+               case AMDGPU_IRQ_STATE_ENABLE:
+                       WREG32_SOC15(GC, GET_INST(GC, i), regRLC_SPM_INT_CNTL, 1);
+                       break;
+               default:
+                       break;
+               }
+       }
+       return 0;
+}
+
+static int gfx_v9_4_3_spm_irq(struct amdgpu_device *adev,
+                            struct amdgpu_irq_src *source,
+                            struct amdgpu_iv_entry *entry)
+{
+       int xcc_id;
+
+       xcc_id = gfx_v9_4_3_ih_to_xcc_inst(adev, entry->node_id);
+
+       amdgpu_rlc_spm_interrupt(adev, xcc_id);
+       return 0;
+}
+
 static void gfx_v9_4_3_emit_wave_limit_cs(struct amdgpu_ring *ring,
                                        uint32_t pipe, bool enable)
 {
@@ -4831,11 +4995,19 @@ static const struct amdgpu_irq_src_funcs gfx_v9_4_3_priv_inst_irq_funcs = {
        .process = gfx_v9_4_3_priv_inst_irq,
 };
 
+static const struct amdgpu_irq_src_funcs gfx_v9_4_3_spm_irq_funcs = {
+       .set = gfx_v9_4_3_spm_set_interrupt_state,
+       .process = gfx_v9_4_3_spm_irq,
+};
+
 static void gfx_v9_4_3_set_irq_funcs(struct amdgpu_device *adev)
 {
        adev->gfx.eop_irq.num_types = AMDGPU_CP_IRQ_LAST;
        adev->gfx.eop_irq.funcs = &gfx_v9_4_3_eop_irq_funcs;
 
+       adev->gfx.spm_irq.num_types = 1;
+       adev->gfx.spm_irq.funcs = &gfx_v9_4_3_spm_irq_funcs;
+
        adev->gfx.priv_reg_irq.num_types = 1;
        adev->gfx.priv_reg_irq.funcs = &gfx_v9_4_3_priv_reg_irq_funcs;
 

--
2.34.1

#18 lore raw

Re: [PATCH 17/17] drm/amdgpu: add profiler/spm support for gfx9_4_3

Hi David,

Thanks for review!  My answer is in-line.

Best Regards!

James zhu

Quoted 4 lines · David

Have skimmed this patch set. Generally looks normal; another hardware block with the usual functionality.
If these pass basic tests, they're probably fine or at least not harmful.
Some general comments:
- The commit descriptions aren't very descriptive. I'd at least like to see documentation of the ioctl interface and a brief notice of what SPM is somewhere, and some more words on what each patch is doing

[JZ] Sure I will add more descriptions.

Quoted 1 line

- Patch 5/17 adds a way to allocate GTT-type memory through amdgpu_vm. This seems sort of out of place with the rest of the patch set. Why is it here? And why can't we just use the existing amdgpu_gtt_mgr?

[JZ] I don't think we have existing API to invoke. Originally is it from amdgpu_amdkfd_alloc_kernel_mem

Quoted 1 line

- Patch 14/17 reserves extra space and puts magic numbers in it to avoid a page fault. This seems like a workaround for something; at very least I'd like the commit description to describe the problem this is solving in detail. I'd also prefer if there was a solution that didn't involve poisoning entries with magic numbers and checking possibly invalid memory.\

[JZ] Yes, it is HW bug. So basically the solution is WA. I will add more description. This solution worked out with other teams' engineer together. the memory space was  allocated properly. But HW's wrong logical for ring buffer wrapping around caused overflow, and overwrote this allocated adjacent space with unexpected data randomly.

Quoted 322 lines

Thanks,
David

________________________________________
From: amd-gfx<[email protected]> on behalf of James Zhu<[email protected]>
Sent: Friday, February 20, 2026 2:22 PM
To:[email protected]; Deucher, Alexander
Cc: Ma, Bing; Zhu, James
Subject: [PATCH 17/17] drm/amdgpu: add profiler/spm support for gfx9_4_3

with spm function interface and spm irq.

---
  drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 194 ++++++++++++++++++++++--
  1 file changed, 183 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +183 -12
index 44b07785bf9c..29fd5e2413da 100644
@@ -169,6 +169,8 @@ static void gfx_v9_4_3_set_gds_init(struct amdgpu_device *adev);
  static void gfx_v9_4_3_set_rlc_funcs(struct amdgpu_device *adev);
  static int gfx_v9_4_3_get_cu_info(struct amdgpu_device *adev,
                                 struct amdgpu_cu_info *cu_info);
+static void gfx_v9_4_3_update_spm_vmid_internal(struct amdgpu_device *adev,
+                                             int xcc_id, unsigned int vmid);
  static void gfx_v9_4_3_xcc_set_safe_mode(struct amdgpu_device *adev, int xcc_id);
  static void gfx_v9_4_3_xcc_unset_safe_mode(struct amdgpu_device *adev, int xcc_id);
 
@@ -1065,6 +1067,13 @@ static int gfx_v9_4_3_sw_init(struct amdgpu_ip_block *ip_block)
 
         num_xcc = NUM_XCC(adev->gfx.xcc_mask);
 
+       /* SPM */
+       r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_RLC,
+                             GFX_9_0__SRCID__RLC_STRM_PERF_MONITOR_INTERRUPT,
+                             &adev->gfx.spm_irq);
+       if (r)
+               return r;
+
         /* EOP Event */
         r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_9_0__SRCID__CP_EOP_INTERRUPT, &adev->gfx.eop_irq);
         if (r)
@@ -1453,10 +1462,14 @@ static void gfx_v9_4_3_init_rlcg_reg_access_ctrl(struct amdgpu_device *adev)
 
  static int gfx_v9_4_3_rlc_init(struct amdgpu_device *adev)
  {
-       /* init spm vmid with 0xf */
-       if (adev->gfx.rlc.funcs->update_spm_vmid)
-               adev->gfx.rlc.funcs->update_spm_vmid(adev, 0, NULL, 0xf);
+       int i, num_xcc;
+
+       if (amdgpu_sriov_vf(adev))
+               return 0;
 
+       num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+       for (i = 0; i < num_xcc; i++)
+               adev->gfx.rlc.funcs->update_spm_vmid(adev, i, NULL, 0xf);
         return 0;
  }
 
@@ -1631,14 +1644,15 @@ static int gfx_v9_4_3_xcc_rlc_resume(struct amdgpu_device *adev, int xcc_id)
  {
         int r;
 
+       gfx_v9_4_3_xcc_rlc_stop(adev, xcc_id);
         if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
-               gfx_v9_4_3_xcc_rlc_stop(adev, xcc_id);
                 /* legacy rlc firmware loading */
                 r = gfx_v9_4_3_xcc_rlc_load_microcode(adev, xcc_id);
                 if (r)
                         return r;
-               gfx_v9_4_3_xcc_rlc_start(adev, xcc_id);
         }
+       gfx_v9_4_3_update_spm_vmid_internal(adev, xcc_id, 0xf);
+       gfx_v9_4_3_xcc_rlc_start(adev, xcc_id);
 
         amdgpu_gfx_rlc_enter_safe_mode(adev, xcc_id);
         /* disable CG */
@@ -1666,28 +1680,38 @@ static int gfx_v9_4_3_rlc_resume(struct amdgpu_device *adev)
         return 0;
  }
 
-static void gfx_v9_4_3_update_spm_vmid(struct amdgpu_device *adev,
-                                             int inst, struct amdgpu_ring *ring, unsigned int vmid)
+static void gfx_v9_4_3_update_spm_vmid_internal(struct amdgpu_device *adev,
+                                             int xcc_id, unsigned int vmid)
  {
         u32 reg, pre_data, data;
 
-       reg = SOC15_REG_OFFSET(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL);
+       reg = SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL);
         if (amdgpu_sriov_is_pp_one_vf(adev) && !amdgpu_sriov_runtime(adev))
                 pre_data = RREG32_NO_KIQ(reg);
         else
-               pre_data = RREG32(reg);
+               pre_data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL);
 
         data =  pre_data & (~RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK);
         data |= (vmid & RLC_SPM_MC_CNTL__RLC_SPM_VMID_MASK) << RLC_SPM_MC_CNTL__RLC_SPM_VMID__SHIFT;
 
         if (pre_data != data) {
                 if (amdgpu_sriov_is_pp_one_vf(adev) && !amdgpu_sriov_runtime(adev)) {
-                       WREG32_SOC15_NO_KIQ(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL, data);
+                       WREG32_SOC15_NO_KIQ(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL, data);
                 } else
-                       WREG32_SOC15(GC, GET_INST(GC, inst), regRLC_SPM_MC_CNTL, data);
+                       WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_MC_CNTL, data);
         }
  }
 
+static void gfx_v9_4_3_update_spm_vmid(struct amdgpu_device *adev, int xcc_id,
+               struct amdgpu_ring *ring, unsigned int vmid)
+{
+       amdgpu_gfx_off_ctrl(adev, false);
+
+       gfx_v9_4_3_update_spm_vmid_internal(adev, xcc_id, vmid);
+
+       amdgpu_gfx_off_ctrl(adev, true);
+}
+
  static const struct soc15_reg_rlcg rlcg_access_gc_9_4_3[] = {
         {SOC15_REG_ENTRY(GC, 0, regGRBM_GFX_INDEX)},
         {SOC15_REG_ENTRY(GC, 0, regSQ_IND_INDEX)},
@@ -2373,6 +2397,7 @@ static int gfx_v9_4_3_hw_fini(struct amdgpu_ip_block *ip_block)
         int i, num_xcc;
 
         amdgpu_irq_put(adev, &adev->gfx.priv_reg_irq, 0);
+       amdgpu_irq_put(adev, &adev->gfx.spm_irq, 0);
         amdgpu_irq_put(adev, &adev->gfx.priv_inst_irq, 0);
         amdgpu_irq_put(adev, &adev->gfx.bad_op_irq, 0);
 
@@ -2507,12 +2532,112 @@ static void gfx_v9_4_3_ring_emit_gds_switch(struct amdgpu_ring *ring,
                                    (1 << (oa_size + oa_base)) - (1 << oa_base));
  }
 
+static void gfx_v9_4_3_spm_start(struct amdgpu_device *adev, int xcc_id)
+{
+       struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+       uint32_t data = 0;
+
+       data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_SPM_PERFMON_CNTL);
+       data |= RLC_SPM_PERFMON_CNTL__PERFMON_RING_MODE_MASK;
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_PERFMON_CNTL), data);
+
+       data = REG_SET_FIELD(0, CP_PERFMON_CNTL, SPM_PERFMON_STATE,
+                       CP_PERFMON_STATE_DISABLE_AND_RESET);
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+       /* When SPM is reset, RLC automatically resets wptr to 0.
+        * Manually reset rptr to match this.
+        */
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), 0);
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_INT_CNTL), 1);
+
+       data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL);
+       data |= RLC_CLK_CNTL__RLC_SPM_CLK_CNTL_MASK;
+       WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL, data);
+}
+
+static void gfx_v9_4_3_spm_stop(struct amdgpu_device *adev, int xcc_id)
+{
+       struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+       uint32_t data = 0;
+
+       data = RREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL);
+       data &= (~RLC_CLK_CNTL__RLC_SPM_CLK_CNTL_MASK);
+       WREG32_SOC15(GC, GET_INST(GC, xcc_id), regRLC_CLK_CNTL, data);
+
+       data = REG_SET_FIELD(0, CP_PERFMON_CNTL, SPM_PERFMON_STATE,
+                       CP_PERFMON_STATE_STOP_COUNTING);
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+       data = REG_SET_FIELD(0, CP_PERFMON_CNTL, PERFMON_STATE,
+                       CP_PERFMON_STATE_DISABLE_AND_RESET);
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), data);
+
+       /* When SPM is reset, RLC automatically resets wptr to 0.
+        * Manually reset rptr to match this.
+        */
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), 0);
+}
+
+static void gfx_v9_4_3_spm_set_rdptr(struct amdgpu_device *adev, int xcc_id,  u32 rptr)
+{
+       struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regRLC_SPM_RING_RDPTR), rptr);
+}
+
+static void gfx_v9_4_3_set_spm_perfmon_ring_buf(struct amdgpu_device *adev,
+                                           int xcc_id, u64 gpu_addr, u32 size)
+{
+       struct amdgpu_ring *kiq_ring = &adev->gfx.kiq[xcc_id].ring;
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false, SOC15_REG_OFFSET(GC, 0,
+                       regRLC_SPM_PERFMON_RING_BASE_LO), lower_32_bits(gpu_addr));
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, 0,
+                               regRLC_SPM_PERFMON_RING_BASE_HI), upper_32_bits(gpu_addr));
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id),
+                               regRLC_SPM_PERFMON_RING_SIZE), size);
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id),
+                               regRLC_SPM_SEGMENT_THRESHOLD), 0x1);
+
+       gfx_v9_4_3_write_data_to_reg(kiq_ring, 0, false,
+                       SOC15_REG_OFFSET(GC, GET_INST(GC, xcc_id), regCP_PERFMON_CNTL), 0);
+}
+
+static const struct spm_funcs gfx_v9_4_3_spm_funcs = {
+       .start = &gfx_v9_4_3_spm_start,
+       .stop = &gfx_v9_4_3_spm_stop,
+       .set_rdptr = &gfx_v9_4_3_spm_set_rdptr,
+       .set_spm_perfmon_ring_buf = &gfx_v9_4_3_set_spm_perfmon_ring_buf,
+       .set_spm_config_size = 30,
+};
+
+static void gfx_v9_4_3_set_spm_funcs(struct amdgpu_device *adev)
+{
+       adev->gfx.spmfuncs = &gfx_v9_4_3_spm_funcs;
+}
+
  static int gfx_v9_4_3_early_init(struct amdgpu_ip_block *ip_block)
  {
         struct amdgpu_device *adev = ip_block->adev;
 
         adev->gfx.num_compute_rings = min(amdgpu_gfx_get_num_kcq(adev),
                                           AMDGPU_MAX_COMPUTE_RINGS);
+       gfx_v9_4_3_set_spm_funcs(adev);
         gfx_v9_4_3_set_kiq_pm4_funcs(adev);
         gfx_v9_4_3_set_ring_funcs(adev);
         gfx_v9_4_3_set_irq_funcs(adev);
@@ -2534,6 +2659,10 @@ static int gfx_v9_4_3_late_init(struct amdgpu_ip_block *ip_block)
         if (r)
                 return r;
 
+       r = amdgpu_irq_get(adev, &adev->gfx.spm_irq, 0);
+       if (r)
+               return r;
+
         r = amdgpu_irq_get(adev, &adev->gfx.priv_inst_irq, 0);
         if (r)
                 return r;
@@ -3404,6 +3533,41 @@ static void gfx_v9_4_3_emit_mem_sync(struct amdgpu_ring *ring)
         amdgpu_ring_write(ring, 0x0000000A); /* POLL_INTERVAL */
  }
 
+static int gfx_v9_4_3_spm_set_interrupt_state(struct amdgpu_device *adev,
+                                            struct amdgpu_irq_src *src,
+                                            unsigned int type,
+                                            enum amdgpu_interrupt_state state)
+{
+       int i, num_xcc;
+
+       num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+       for (i = 0; i < num_xcc; i++) {
+               switch (state) {
+               case AMDGPU_IRQ_STATE_DISABLE:
+                       WREG32_SOC15(GC, GET_INST(GC, i), regRLC_SPM_INT_CNTL, 0);
+                       break;
+               case AMDGPU_IRQ_STATE_ENABLE:
+                       WREG32_SOC15(GC, GET_INST(GC, i), regRLC_SPM_INT_CNTL, 1);
+                       break;
+               default:
+                       break;
+               }
+       }
+       return 0;
+}
+
+static int gfx_v9_4_3_spm_irq(struct amdgpu_device *adev,
+                            struct amdgpu_irq_src *source,
+                            struct amdgpu_iv_entry *entry)
+{
+       int xcc_id;
+
+       xcc_id = gfx_v9_4_3_ih_to_xcc_inst(adev, entry->node_id);
+
+       amdgpu_rlc_spm_interrupt(adev, xcc_id);
+       return 0;
+}
+
  static void gfx_v9_4_3_emit_wave_limit_cs(struct amdgpu_ring *ring,
                                         uint32_t pipe, bool enable)
  {
@@ -4831,11 +4995,19 @@ static const struct amdgpu_irq_src_funcs gfx_v9_4_3_priv_inst_irq_funcs = {
         .process = gfx_v9_4_3_priv_inst_irq,
  };
 
+static const struct amdgpu_irq_src_funcs gfx_v9_4_3_spm_irq_funcs = {
+       .set = gfx_v9_4_3_spm_set_interrupt_state,
+       .process = gfx_v9_4_3_spm_irq,
+};
+
  static void gfx_v9_4_3_set_irq_funcs(struct amdgpu_device *adev)
  {
         adev->gfx.eop_irq.num_types = AMDGPU_CP_IRQ_LAST;
         adev->gfx.eop_irq.funcs = &gfx_v9_4_3_eop_irq_funcs;
 
+       adev->gfx.spm_irq.num_types = 1;
+       adev->gfx.spm_irq.funcs = &gfx_v9_4_3_spm_irq_funcs;
+
         adev->gfx.priv_reg_irq.num_types = 1;
         adev->gfx.priv_reg_irq.funcs = &gfx_v9_4_3_priv_reg_irq_funcs;
 
--

2.34.1

#19 lore raw
In reply to James Zhu

Ping ...

Thanks!

James Zhu

Quoted 48 lines · James Zhu

which contains all profilers' kernel driver changes.

---
  include/uapi/drm/amdgpu_drm.h | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
include/uapi/drm/amdgpu_drm.h +16 -0
index 1a961f62724e..dfc98d6d98a8 100644
@@ -58,6 +58,7 @@ extern "C" {
  #define DRM_AMDGPU_USERQ_SIGNAL		0x17
  #define DRM_AMDGPU_USERQ_WAIT		0x18
  #define DRM_AMDGPU_GEM_LIST_HANDLES	0x19
+#define DRM_AMDGPU_PROFILER			0x20
 
  #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)
@@ -79,6 +80,7 @@ extern "C" {
  #define DRM_IOCTL_AMDGPU_USERQ_SIGNAL	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_SIGNAL, struct drm_amdgpu_userq_signal)
  #define DRM_IOCTL_AMDGPU_USERQ_WAIT	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_WAIT, struct drm_amdgpu_userq_wait)
  #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_PROFILER	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILER, struct drm_amdgpu_profiler_args)
 
  /**
   * DOC: memory domains
@@ -1708,6 +1710,20 @@ struct drm_color_ctm_3x4 {
  	__u64 matrix[12];
  };
 
+/*
+ * Supported Profiler Operations
+ */
+enum drm_amdgpu_profiler_ops {
+	AMDGPU_PROFILER_VERSION = 0,
+};
+
+struct drm_amdgpu_profiler_args {
+	__u32 op;				/* amdgpu_profiler_op */
+	union {
+		__u32 version;		/* lower 16 bit: minor. higher 16 bit: major */
+	};
+};
+
  #if defined(__cplusplus)
  }
  #endif