drm/amdgpu: add UAPI to support profiler
Subsystems (3)
PATCH v4 1/17 drm/amdgpu: add UAPI to support profiler
introduce a new DRM ioctl DRM_AMDGPU_PROFILER as a unified entry point for GPU hardware profiling features. The ioctl takes a drm_amdgpu_profiler_args structure which carries an operation code and a per-operation payload union.
The initial operation defined is AMDGPU_PROFILER_VERSION, which allows user space to query the profiler UAPI version. The version field encodes the major version in the upper 16 bits and the minor version in the lower 16 bits.
Subsequent patches will extend this ioctl to dispatch sub-operations for SPM (Stream Performance Monitor) and PCS (PC Sampling) profiling.
--- include/uapi/drm/amdgpu_drm.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
include/uapi/drm/amdgpu_drm.h +17 -0
@@ -59,6 +59,7 @@ extern "C" {#define DRM_AMDGPU_USERQ_WAIT 0x18#define DRM_AMDGPU_GEM_LIST_HANDLES 0x19#define DRM_AMDGPU_CWSR 0x20 +#define DRM_AMDGPU_PROFILER 0x21#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)
@@ -82,6 +83,7 @@ extern "C" {#define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)#define DRM_IOCTL_AMDGPU_CWSR \ DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_CWSR, union drm_amdgpu_cwsr) +#define DRM_IOCTL_AMDGPU_PROFILER DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_PROFILER, struct drm_amdgpu_profiler_args)/** * DOC: memory domains
@@ -1789,6 +1791,21 @@ union drm_amdgpu_cwsr {#define AMDGPU_FAMILY_GC_11_5_4 154/* GC 11.5.4 */ #define AMDGPU_FAMILY_GC_12_0_0 152/* GC 12.0.0 */ +/* +* Supported Profiler Operations +*/ +enum drm_amdgpu_profiler_ops { +AMDGPU_PROFILER_VERSION =0 , +}; + +struct drm_amdgpu_profiler_args { +__u32 op;/* drm_amdgpu_profiler_ops */ +__u32 pad; +union { +__u32 version;/* lower 16 bit: minor. higher 16 bit: major */ + }; +}; +#if defined(__cplusplus) }#endif
Signature · 1 line
2.34.1
PATCH v4 2/17 drm/amdgpu: add profiler manager initialization and release
to introduce the profiler manager infrastructure needed to support per-process GPU hardware profiling features such as SPM and PCS.
Two new files are added:
- amdgpu_profiler.c/.h: define struct amdgpu_profiler_mgr, which holds an array of per-XCP partition managers (amdgpu_profiler_xcp_mgr), each reference-counted via kref and protected by a dedicated mutex.
A device-level struct amdgpu_profiler_mgr prof_mgr is embedded in struct amdgpu_device and its top-level mutex is initialized during amdgpu_device_init().
The per-XCP profiler manager is initialized lazily on the first amdgpu_driver_open_kms() call for a given XCP partition:
- If the XCP manager is already initialized, only the kref is incremented.
- On file close (amdgpu_drm_release()), the kref is decremented and the per-XCP manager is destroyed (mutex destroyed, is_initialized cleared) when the last user releases it.
Non-partitioned devices uses XCP partition 0. Initialization is skipped when fpriv->xcp_id is AMDGPU_XCP_NO_PARTITION, which indicates the device has not yet been fully initialized.
-v3: rename is_init to is_initialized in amdgpu_profiler_xcp_mgr -v4: fix mistake
--- drivers/gpu/drm/amd/amdgpu/Makefile | 2 +- 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 | 5 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 89 ++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h | 48 +++++++++++ 7 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h
drivers/gpu/drm/amd/amdgpu/Makefile +1 -1
@@ -72,7 +72,7 @@ amdgpu-y += amdgpu_device.o amdgpu_reg_access.o amdgpu_doorbell_mgr.o amdgpu_kms amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o amdgpu_lsdma.o \ amdgpu_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_events.o \ - amdgpu_cwsr.o + amdgpu_cwsr.o amdgpu_profiler.o amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -0
@@ -110,6 +110,7 @@#include "amdgpu_seq64.h" #include "amdgpu_reg_state.h" #include "amdgpu_userq.h" +#include "amdgpu_profiler.h" #include "amdgpu_eviction_fence.h" #include "amdgpu_ip.h" #if defined(CONFIG_DRM_AMD_ISP )
@@ -1207,6 +1208,8 @@ struct amdgpu_device {struct amdgpu_uma_carveout_info uma_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
@@ -3754,6 +3754,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
@@ -2971,6 +2971,7 @@ static int amdgpu_drm_release(struct inode *inode, struct file *filp)amdgpu_evf_mgr_flush_suspend (&fpriv->evf_mgr);amdgpu_userq_mgr_fini (&fpriv->userq_mgr);amdgpu_evf_mgr_fini (&fpriv->evf_mgr); +amdgpu_profiler_mgr_fini (fpriv_to_prof_mgr (fpriv), fpriv);drm_dev_exit (idx); }
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +5 -0
@@ -1628,6 +1628,11 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)dev_dbg (adev->dev,"cwsr trap not enabled" ); } + r =amdgpu_profiler_mgr_init (fpriv_to_prof_mgr (fpriv), fpriv); +if (r) +DRM_WARN ("Can't setup profiler\n" ); + +amdgpu_ctx_mgr_init (&fpriv->ctx_mgr, adev); file_priv->driver_priv = fpriv;
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 +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 3/17 drm/amdgpu: implement profiler ioctl
In reply to James Zhuwith wire up the DRM_AMDGPU_PROFILER ioctl introduced in the UAPI patch by registering amdgpu_profiler_ioctl() as its handler with DRM_AUTH|DRM_RENDER_ALLOW permissions.
Implement the first operation AMDGPU_PROFILER_VERSION, which returns the profiler UAPI version to user space by packing the major version into bits 31:16 and the minor version into bits 15:0 of the args->version field. Unknown operations return -EINVAL.
A helper macro AMDGPU_XCP_ID() is added to normalize the XCP partition ID for display purposes: AMDGPU_XCP_NO_PARTITION (device not yet initialized) is mapped to 0, while valid partition IDs are passed through unchanged.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 25 ++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h | 3 +++ 3 files changed, 29 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -3080,6 +3080,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
@@ -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 *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_dbg (adev->dev,"Invalid option: %i" , args->op); + } + +return -EINVAL ; +} +int amdgpu_profiler_mgr_init (struct amdgpu_profiler_mgr *prof_mgr,struct amdgpu_fpriv *fpriv)
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h +3 -0
@@ -25,6 +25,7 @@#ifndef AMDGPU_PROFILER_H_ #define AMDGPU_PROFILER_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)
@@ -40,6 +41,8 @@ struct amdgpu_profiler_mgr {struct amdgpu_profiler_xcp_mgr prof_xcp_mgr[MAX_XCP ]; }; +int amdgpu_profiler_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp);int amdgpu_profiler_mgr_init (struct amdgpu_profiler_mgr *prof_mgr,struct amdgpu_fpriv *fpriv); void amdgpu_profiler_mgr_fini(struct amdgpu_profiler_mgr *prof_mgr,
Signature · 1 line
2.34.1
PATCH v4 4/17 drm/amdgpu: add UAPI to support profiler/SPM
In reply to James Zhuto extend the DRM_AMDGPU_PROFILER ioctl to support Stream Performance Monitor (SPM) hardware, which streams GPU performance counter data continuously into a ring buffer for offline analysis.
A new operation AMDGPU_PROFILER_SPM is added to drm_amdgpu_profiler_ops, dispatching through the existing drm_amdgpu_profiler_args union via the new drm_amdgpu_spm_args member.
Three SPM sub-operations are defined in drm_amdgpu_spm_op:
AMDGPU_SPM_OP_ACQUIRE (0):
Acquires exclusive access to SPM on the caller's XCP partition. Must be called before SET_DEST_BUF. Returns -EBUSY if another process already holds SPM.
AMDGPU_SPM_OP_RELEASE (1):
Releases exclusive SPM access, stopping the hardware and freeing the ring buffer, allowing another process to acquire SPM.
AMDGPU_SPM_OP_SET_DEST_BUF (2):
Controls the destination buffer for SPM counter streaming:
- dest_buf=NULL: unsets the destination buffer and stops copying.
- dest_buf!=NULL: sets a new user-space destination buffer of buf_size bytes. If timeout>0, the call blocks up to timeout ms waiting for the previous buffer to be filled; on return, timeout is updated with the remaining time. If timeout expires, any partial data is flushed and the call returns success. If timeout=0, the previous buffer is replaced immediately without waiting. bytes_copied reports how much data was written to the previous buffer. has_data_loss is non-zero if ring buffer overflow occurred.
A drm_amdgpu_spm_buffer_header structure is prepended to each dest_buf by the kernel, containing the SPM version (major in bits 31:24, minor in bits 23:0), bytes_copied, and has_data_loss for that buffer.
--- include/uapi/drm/amdgpu_drm.h | 94 +++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+)
include/uapi/drm/amdgpu_drm.h +94 -0
@@ -1791,11 +1791,104 @@ union drm_amdgpu_cwsr {#define AMDGPU_FAMILY_GC_11_5_4 154/* GC 11.5.4 */ #define AMDGPU_FAMILY_GC_12_0_0 152/* GC 12.0.0 */ + +/* +* 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_OP_SET_DEST_BUF +* @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: non-zero if data was lost (e.g. due to ring buffer overflow), +* zero otherwise +* +* This ioctl performs different functions depending on the @op parameter. +* +* AMDGPU_SPM_OP_ACQUIRE +* ------------------------ +* +* Acquires exclusive access of SPM on the specified XCP 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 XCP 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; +}; + +/** +* 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 {
@@ -1803,6 +1896,7 @@ struct drm_amdgpu_profiler_args {__u32 pad;union {__u32 version;/* lower 16 bit: minor. higher 16 bit: major */ +struct drm_amdgpu_spm_args spm; }; };
Signature · 1 line
2.34.1
PATCH v4 5/17 drm/amdgpu: add amdgpu_bo_alloc_gtt_mem and amdgpu_bo_free_gtt_mem
In reply to James Zhutwo helper functions for allocating and freeing kernel-internal GTT (Graphics Translation Table) buffer objects, intended for use by the SPM ring buffer allocation path.
amdgpu_bo_alloc_gtt_mem():
Allocates a TTM kernel BO of the requested size in GTT domain, PAGE_SIZE aligned. The bo_ptr_size is set to sizeof(struct amdgpu_bo). After creation the BO is reserved, pinned to GTT, GART-bound via amdgpu_ttm_alloc_gart(), and kernel-mapped via amdgpu_bo_kmap(). On success, the caller receives the opaque BO handle (*mem_obj), the GPU virtual address (*gpu_addr), and the kernel virtual address (*cpu_ptr). All intermediate steps are rolled back cleanly on any failure via a chained goto error path.
Two optional flags control BO creation:
- mqd_gfx9=true: sets AMDGPU_GEM_CREATE_CP_MQD_GFX9, required for GFX9 MQD-compatible allocations.
- is_uswc_mode=true: sets AMDGPU_GEM_CREATE_CPU_GTT_USWC, enabling uncached speculative write-combining CPU access to the GTT buffer.
amdgpu_bo_free_gtt_mem():
Reverses the allocation by reserving the BO, unmapping (kunmap), unpinning, unreserving, and dropping the last reference (unref). Handles NULL gracefully and logs an error if reservation fails.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 97 ++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_object.h | 5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 1 - 3 files changed, 101 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +97 -0
@@ -1701,4 +1701,101 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m)return size; } + +int amdgpu_bo_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) +{ +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 (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 amdgpu (%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 amdgpu\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 amdgpu\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 GTT for amdgpu" , 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_bo_free_gtt_mem (struct amdgpu_device *adev,void **mem_obj) +{ +struct amdgpu_bo **bo = (struct amdgpu_bo **) mem_obj; +int r; + +if (!bo || !*bo) +return ; + + r =amdgpu_bo_reserve (*bo, true); +if (r) { +dev_err (adev->dev, +"(%d) failed to reserve bo to free" , r); +return ; + } + +amdgpu_bo_kunmap (*bo); +amdgpu_bo_unpin (*bo); +amdgpu_bo_unreserve (*bo); +amdgpu_bo_unref (bo); +} +#endif
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -1
@@ -353,5 +353,8 @@ void amdgpu_debugfs_sa_init(struct amdgpu_device *adev);bool amdgpu_bo_support_uswc (u64 bo_flags); - +int amdgpu_bo_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_bo_free_gtt_mem (struct amdgpu_device *adev,void **mem_obj);#endif
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -1
@@ -747,5 +747,4 @@ void amdgpu_vm_print_task_info(struct amdgpu_device *adev, list_for_each_entry(mapping, &(bo_va)->valids, list)#define amdgpu_vm_bo_va_for_each_invalid_mapping (bo_va, mapping) \ list_for_each_entry(mapping, &(bo_va)->invalids, list) -#endif
Signature · 1 line
2.34.1
PATCH v4 6/17 drm/amdgpu: add RLC SPM interface to
In reply to James Zhuto introduce the hardware-agnostic RLC (Run List Controller) SPM interface layer that sits between the generic SPM manager and IP-specific register programming.
A new function table struct amdgpu_spm_funcs is added to amdgpu_gfx.h, with the following callbacks to be implemented per IP version:
- start(adev, xcc_id): enable SPM hardware and interrupt
- stop(adev, xcc_id): disable SPM hardware and interrupt
- set_rdptr(adev, xcc_id, rptr): update the SPM ring read pointer
- set_spm_perfmon_ring_buf(adev, xcc_id, gpu_addr, size): configure the SPM ring buffer base address and size registers
- set_spm_config_size: KIQ ring space (in DWORDs) needed per operation
A pointer to the active function table is stored in adev->gfx.spmfuncs. All RLC SPM functions are no-ops when spmfuncs is NULL.
Five generic RLC SPM functions are implemented in amdgpu_rlc.c:
amdgpu_rlc_spm_acquire(adev, xcc_id, vm, gpu_addr, size):
Sets up SPM for a specific XCP instance. Allocates a reserved VMID
on the GFX hub for the caller's VM, initializes the SPM VMID to 0x0
(from the default 0xf), then programs the ring buffer base address
and size via the KIQ ring. On failure, reverts the VMID to 0xf and
frees the reserved VMID. amdgpu_rlc_spm_release(adev, xcc_id, vm):
Stops the SPM stream via the KIQ ring, reverts the SPM VMID to 0xf, and frees the reserved VMID.
amdgpu_rlc_spm_cntl(adev, xcc_id, cntl):
Starts (cntl=true) or stops (cntl=false) the SPM hardware by dispatching start()/stop() through the KIQ ring under the KIQ ring_lock spinlock.
amdgpu_rlc_spm_set_rdptr(adev, xcc_id, rptr):
Advances the SPM ring read pointer via the KIQ ring, informing the hardware that the CPU has consumed data up to rptr.
amdgpu_rlc_spm_interrupt(adev, xcc_id):
SPM interrupt handler stub.
All KIQ ring operations are serialized under the per-XCC KIQ ring_lock spinlock.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 11 +++ drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.c | 90 +++++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.h | 6 ++ 3 files changed, 107 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +11 -0
@@ -169,6 +169,16 @@ struct amdgpu_kiq {void *mqd_backup; }; +struct amdgpu_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 */
@@ -423,6 +433,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 amdgpu_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 +90 -0
93 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.h +6 -0
@@ -374,4 +374,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
PATCH v4 7/17 drm/amdgpu: add profiler/spm manager initialization and release
In reply to James Zhuto introduce the SPM (Stream Performance Monitor) manager subsystem as a new compilation unit (amdgpu_spm.c / amdgpu_spm.h) and integrate it into the per-XCP profiler manager lifecycle.
struct amdgpu_spm_mgr is defined in amdgpu_spm.h, currently holding only a drm_file pointer (the owning file handle). It is embedded as a member of a union inside struct amdgpu_profiler_xcp_mgr, alongside future profiler subsystems (e.g. PCS).
The SPM manager lifecycle is tied to the profiler XCP manager:
- amdgpu_spm_mgr_init() is called from amdgpu_profiler_mgr_init() when a new XCP profiler context is first created (i.e. the first open on that XCP partition). It calls the internal amdgpu_spm_init_device() stub (TODO: device-level SPM hardware initialization).
- amdgpu_spm_mgr_fini() is called from amdgpu_profiler_mgr_release() (the kref release callback) when the last file handle referencing this XCP profiler context is closed. If an active file handle is recorded in spm_mgr->file, it triggers amdgpu_spm_release_device(), which calls the internal amdgpu_spm_release() stub (TODO: SPM hardware teardown and resource cleanup).
A helper macro prof_mgr_to_adev() is added to amdgpu_profiler.h to retrieve the parent amdgpu_device from an amdgpu_profiler_mgr pointer via container_of().
-v3: rename is_init to is_initialized in amdgpu_profiler_xcp_mgr -v4: fix mistake
--- drivers/gpu/drm/amd/amdgpu/Makefile | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c | 3 + drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h | 9 +++ drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 62 ++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h | 34 +++++++++++ 5 files changed, 109 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
@@ -72,7 +72,7 @@ amdgpu-y += amdgpu_device.o amdgpu_reg_access.o amdgpu_doorbell_mgr.o amdgpu_kms amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o amdgpu_lsdma.o \ amdgpu_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_events.o \ - amdgpu_cwsr.o amdgpu_profiler.o + amdgpu_cwsr.o amdgpu_profiler.o amdgpu_spm.o amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.c +3 -0
@@ -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_initialized = 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_initialized = false; }
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h +9 -0
@@ -25,15 +25,24 @@#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) +struct amdgpu_profiler_xcp_mgr {struct mutex mutex;uint32_t xcp_id;bool is_initialized;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 +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ +/* +* Copyright 2026 Advanced Micro Devices, Inc. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR +* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef AMDGPU_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
PATCH v4 8/17 drm/amdgpu: add profiler/spm interrupt handler
In reply to James Zhuto complete the amdgpu_rlc_spm_interrupt() stub by wiring it to the new SPM-layer interrupt handler amdgpu_spm_interrupt().
amdgpu_rlc_spm_interrupt(adev, xcc_id) is the entry point called by IP-specific IRQ process callbacks (e.g. gfx_v9_0_spm_irq, gfx_v9_4_3_spm_irq) when the RLC fires a Stream Performance Monitor interrupt. It guards against unconfigured hardware by returning early if adev->gfx.spmfuncs is NULL, then delegates to amdgpu_spm_interrupt().
amdgpu_spm_interrupt(adev, xcc_id) is added to amdgpu_spm.c as the SPM-manager-level interrupt handler. The implementation is currently a stub (TODO) and will be completed in a later patch to schedule the SPM work queue for draining the ring buffer when the hardware signals that data is available or the ring is near full.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.c | 5 ++++- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 5 +++++ drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h | 1 + 3 files changed, 10 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_rlc.c +4 -1
@@ -671,5 +671,8 @@ void amdgpu_rlc_spm_release(struct amdgpu_device *adev, int xcc_id, struct amdgpvoid amdgpu_rlc_spm_interrupt (struct amdgpu_device *adev,int xcc_id) { -/* TODO: */ +if (!adev->gfx.spmfuncs) +return ; + +amdgpu_spm_interrupt (adev, xcc_id); }
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +5 -0
@@ -60,3 +60,8 @@ 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) +{ +/* TODO */ +}
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +1 -0
@@ -30,5 +30,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
PATCH v4 9/17 drm/amdgpu: add profiler/spm ioctl
In reply to James Zhuto wire the AMDGPU_PROFILER_SPM operation into the profiler ioctl dispatch path, and introduce amdgpu_spm_ioctl() as the dedicated SPM sub-ioctl handler.
In amdgpu_profiler_ioctl(), the new AMDGPU_PROFILER_SPM case extracts the embedded drm_amdgpu_spm_args from the profiler args union and forwards it to amdgpu_spm_ioctl(dev, &args->spm, filp).
amdgpu_spm_ioctl() resolves the per-device amdgpu_device from the drm_file's driver_priv and switches on args->op. At this point only the default error path is present, returning -EINVAL for all unknown operations. Subsequent patches will add cases for AMDGPU_SPM_OP_ACQUIRE, AMDGPU_SPM_OP_SET_DEST_BUF, and AMDGPU_SPM_OP_RELEASE.
--- 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
@@ -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_dbg (adev->dev,"Invalid option: %i" , args->op); }
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +14 -0
@@ -47,6 +47,20 @@ static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *freturn 0 ; } +int amdgpu_spm_ioctl (struct drm_device *dev,void *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_dbg (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
@@ -28,6 +28,9 @@ struct amdgpu_spm_mgr {struct drm_file *file; }; +int amdgpu_spm_ioctl (struct drm_device *dev,void *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
PATCH v4 10/17 drm/amdgpu: add profiler/spm operation AMDGPU_SPM_OP_ACQUIRE
In reply to James Zhuto implement AMDGPU_SPM_OP_ACQUIRE, which grants a process exclusive access to the SPM hardware on its XCP partition and allocates the per-XCC ring buffers used for streaming performance counter data.
Data structures:
struct amdgpu_spm_base (amdgpu_spm.h):
Holds the per-XCC ring buffer state: GTT BO handle (spm_obj), GPU virtual address (gpu_addr), kernel virtual address (cpu_addr), and effective ring size (ring_size).
struct amdgpu_spm_cntr (amdgpu_spm.h):
Contains an array of amdgpu_spm_base[MAX_XCP] (one per XCC) and a spm_worker_mutex to serialize worker operations. Allocated on first ACQUIRE and freed on RELEASE.
struct amdgpu_spm_mgr (amdgpu_spm.h):
Extended with lead_thread (the acquiring process's thread group leader), spm_cntr pointer, and spm_work work_struct for the deferred ring buffer drain worker.
ACQUIRE flow (amdgpu_spm_acquire):
1. Takes the per-XCP prof_xcp_mgr->mutex to serialize concurrent ACQUIRE attempts.
2. Returns -EBUSY if spm_cntr is already allocated (another process holds SPM).
3. Allocates spm_cntr via kzalloc and records current->group_leader as lead_thread.
4. For each XCC in AMDGPU_XCC_MASK(adev), calls _amdgpu_spm_acquire():
- Allocates a 4 MiB GTT ring buffer via amdgpu_bo_alloc_gtt_mem().
- Programs the ring buffer into the RLC hardware via amdgpu_rlc_spm_acquire(), which also reserves a VMID for the caller's VM.
- Subtracts 0x20 bytes (8 DWORDs) from ring_size to exclude the hardware-defined metadata area at the end of the buffer.
- On failure, frees the GTT BO and clears the spm_base entry.
5. Initializes spm_worker_mutex and INIT_WORK for amdgpu_spm_work.
6. Records the drm_file pointer in spm_mgr->file.
7. On any per-XCC failure, rolls back all already-acquired XCCs via _amdgpu_spm_release() (stub, TODO) and frees spm_cntr.
amdgpu_spm_work (work_struct handler):
Attaches the lead_thread's mm_struct via kthread_use_mm() to enable user-space copy operations, then detaches and releases the mm. The actual ring buffer drain to user space is a TODO for a later patch.
Three new navigation macros are added in amdgpu_profiler.h:
- to_prof_xcp_mgr(x, y): container_of from a member y to amdgpu_profiler_xcp_mgr.
- xcp_to_prof_mgr(x, y): container_of from prof_xcp_mgr[] element to amdgpu_profiler_mgr.
- mgr_to_adev(x, y): compound statement combining the two above to reach amdgpu_device from any embedded manager pointer.
AMDGPU_XCC_MASK(adev) is added using GENMASK() to safely produce a bitmask of active XCC instances from NUM_XCC(adev->gfx.xcc_mask).
In amdgpu_spm_ioctl(), the per-XCP spm_mgr is now resolved from fpriv using AMDGPU_XCP_ID() before dispatching to sub-operation handlers.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h | 13 +++ drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 114 ++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h | 17 +++ 3 files changed, 143 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_profiler.h +13 -0
@@ -27,6 +27,7 @@#include "amdgpu_spm.h" +#define AMDGPU_XCC_MASK (adev) GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0)#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)
@@ -34,6 +35,18 @@#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;
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +113 -1
138 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +17 -0
@@ -24,8 +24,25 @@#ifndef AMDGPU_SPM_H_ #define AMDGPU_SPM_H_ +struct amdgpu_spm_base { +u64 gpu_addr; +u32 ring_size; +u32 *cpu_addr; +void *spm_obj; +}; +struct amdgpu_spm_cntr { +struct amdgpu_spm_base spm[MAX_XCP ]; +struct mutex spm_worker_mutex; +}; +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 *data,
Signature · 1 line
2.34.1
PATCH v4 11/17 drm/amdgpu: add profiler/spm operation AMDGPU_SPM_OP_SET_DEST_BUF
In reply to James Zhuto implement AMDGPU_SPM_OP_SET_DEST_BUF, which registers a user-space destination buffer for SPM data streaming, optionally waits for the previous buffer to be filled, and starts or stops the SPM hardware per XCC instance.
Data structure additions (amdgpu_spm.h):
struct spm_user_buf:
Tracks the current user-space destination buffer per XCC: the adjusted user_addr (offset past the drm_amdgpu_spm_buffer_header) and remaining ubufsize in bytes.
amdgpu_spm_base extended with:
ubuf (spm_user_buf), ring_rptr, size_copied, has_data_loss, has_user_buf (buffer registered), is_user_buf_filled (buffer full), is_spm_started (SPM HW running).
amdgpu_spm_cntr extended with:
spm_use_cnt (number of active XCC ring buffers), have_users_buf_cnt (number of XCCs with a registered user buffer), are_users_buf_filled (true when all registered buffers are full), spm_buf_wq (wait queue head for blocking on buffer fill).
amdgpu_spm_mgr extended with:
spm_irq_lock (spinlock protecting is_spm_started and ring_rptr against concurrent IRQ handler access).
SET_DEST_BUF flow (amdgpu_set_dest_buffer):
1. Validates spm_cntr is allocated and spm_use_cnt > 0.
2. Splits the caller-supplied buf_size evenly across all active XCCs (spm_use_cnt), rounded down to 32-byte alignment. Returns -EINVAL if the per-XCC slice cannot accommodate a drm_amdgpu_spm_buffer_header
3. If timeout > 0 and a previous buffer is still being filled, blocks on spm_buf_wq via wait_event_interruptible_timeout(). On -ERESTARTSYS the remaining timeout is computed and returned so the syscall can be transparently restarted by user space. On -ETIME (timeout expired), the work queue is flushed immediately to capture any partial data, and the call returns success. If timeout == 0 and a previous buffer exists, the work queue is flushed immediately without waiting.
4. For each XCC in AMDGPU_XCC_MASK(adev), calls spm_update_dest_info():
- If a previous user buffer was registered (has_user_buf), writes the drm_amdgpu_spm_buffer_header (SPM version, bytes_copied, has_data_loss to the reserved header slot just before ubuf.user_addr via copy_to_user(), accumulates bytes_copied and has_data_loss into the ioctl output args, and decrements have_users_buf_cnt.
- If a new dest_buf is provided, advances the internal user_addr past the header, records ubufsize, resets size_copied, has_data_loss and is_user_buf_filled, sets has_user_buf, and increments have_users_buf_cnt.
5. If dest_buf != NULL and the XCC's SPM is not yet started:
calls amdgpu_rlc_spm_cntl(start), resets ring_rptr to 0 under spm_irq_lock (since amdgpu_rlc_spm_cntl() resets wptr to 0), and sets is_spm_started. If SPM was already running, sets need_schedule to drain existing ring data after the mutex is released.
6. If dest_buf == NULL: stops the SPM hardware via amdgpu_rlc_spm_cntl(stop), clears is_spm_started and ring_rptr under spm_irq_lock.
7. After releasing the XCP mutex, schedules the work queue if need_schedule is set.
amdgpu_spm_interrupt() stub is implemented: resolves the xcp_id from xcc_id using amdgpu_xcp_get_partition() (defaulting to 0 on non- partitioned devices), looks up the corresponding spm_mgr, and under spm_irq_lock schedules the work queue if SPM is active on that XCC.
-v3: add _spm_start/_spm_stop to improve code organization and readability
--- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 244 +++++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h | 18 ++ 2 files changed, 260 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +242 -2
@@ -29,6 +29,9 @@* 0.1 - Initial revision */ +#define AMDGPU_SPM_MAJOR_VERSION 0 +#define AMDGPU_SPM_MINOR_VERSION 1 +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);
@@ -120,12 +123,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;
@@ -141,6 +148,46 @@ static int amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *freturn ret; } +static int _spm_start (struct amdgpu_spm_mgr *spm_mgr, +struct amdgpu_device *adev,int inst) +{ +struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]); +unsigned long flags; +int ret =0 ; + +/* Start hardware SPM first */ +amdgpu_rlc_spm_cntl (adev, inst,1 ); + +/* amdgpu_amdkfd_rlc_spm_cntl() will reset SPM and +* wptr will become 0, adjust rptr accordingly. +*/ +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); + +return ret; +} + +static void _spm_stop (struct amdgpu_spm_mgr *spm_mgr, +struct amdgpu_device *adev,int inst) +{ +struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]); +unsigned long flags; + +amdgpu_rlc_spm_cntl (adev, inst,0 ); +spin_lock_irqsave (&spm_mgr->spm_irq_lock, flags); + spm_mgr->spm_cntr->spm[inst].is_spm_started = false; +/* amdgpu_amdkfd_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); +} +static void _amdgpu_spm_release (struct amdgpu_spm_mgr *spm_mgr,int inst,struct drm_file *filp) {/* TODO: */
@@ -153,6 +200,184 @@ static int amdgpu_spm_release(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *freturn 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 =0 ; + 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); +unsigned 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 *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; +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 || + !spm_cntr->spm_use_cnt) { + 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) { + ret =_spm_start (spm_mgr, adev, inst); +if (ret) + goto out; + }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 { +_spm_stop (spm_mgr, adev, inst); + } + } + +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 *data,struct drm_file *filp) {
@@ -167,10 +392,13 @@ int amdgpu_spm_ioctl(struct drm_device *dev, void *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_dbg (adev->dev,"Invalid option: %i\n" , args->op); -return -EINVAL ; } +return -EINVAL ; }int amdgpu_spm_mgr_init (struct amdgpu_spm_mgr *spm_mgr)
@@ -189,5 +417,17 @@ void amdgpu_spm_mgr_fini(struct amdgpu_spm_mgr *spm_mgr)void amdgpu_spm_interrupt (struct amdgpu_device *adev,int xcc_id) { -/* TODO */ +uint8_t xcp_id; +unsigned long flags; +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); + +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 +18 -0
@@ -24,15 +24,32 @@#ifndef AMDGPU_SPM_H_ #define AMDGPU_SPM_H_ +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; }; struct amdgpu_spm_mgr {
@@ -43,6 +60,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 *data,
Signature · 1 line
2.34.1
PATCH v4 12/17 drm/amdgpu: add profiler/spm operation AMDGPU_SPM_OP_RELEASE
In reply to James Zhuto implement AMDGPU_SPM_OP_RELEASE, which stops the SPM hardware, drains the work queue, unblocks any waiting SET_DEST_BUF callers, and frees all per-XCC ring buffer resources acquired by AMDGPU_SPM_OP_ACQUIRE.
_amdgpu_spm_release(spm_mgr, inst, filp):
Per-XCC teardown helper. Skips silently if the ring buffer was never allocated (ring_size == 0). Otherwise:
- Calls amdgpu_rlc_spm_release() to stop the RLC hardware, revert
the SPM VMID to 0xf, and free the reserved VMID on the GFX hub.
- Calls amdgpu_bo_free_gtt_mem() to unpin, unmap, and drop the 4 MiB GTT ring buffer BO.
- Under spm_irq_lock, zeroes the entire amdgpu_spm_base entry to prevent stale pointer access from any concurrent IRQ path.
- Decrements spm_use_cnt.
amdgpu_spm_release(spm_mgr, filp):
1. Takes the per-XCP prof_xcp_mgr->mutex to serialize against concurrent ACQUIRE or SET_DEST_BUF calls.
2. Returns -EPERM if the caller's drm_file does not match
spm_mgr->file, preventing one process from releasing another's
SPM session. 3. Returns -EINVAL if SPM was never acquired (spm_cntr == NULL).
4. For each XCC in AMDGPU_XCC_MASK(adev): clears is_spm_started under spm_irq_lock, then stops the hardware via amdgpu_rlc_spm_cntl(stop).
5. Calls flush_work() to wait for any in-progress ring drain to complete before freeing buffers.
6. Calls wake_up_all() on spm_buf_wq to unblock any thread sleeping in SET_DEST_BUF with a pending timeout.
7. Calls _amdgpu_spm_release() for each XCC to free per-XCC resources.
8. Calls amdgpu_vmid_free_reserved() to release the GFX hub VMID reservation on hub 0.
9. Destroys spm_worker_mutex, frees spm_cntr, and sets spm_cntr = NULL under spm_irq_lock to prevent the IRQ handler from scheduling new work after the memory is freed.
-v3: use _spm_stop
--- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 56 +++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +53 -3
@@ -190,14 +190,61 @@ static void _spm_stop(struct amdgpu_spm_mgr *spm_mgr,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_bo_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->file != filp) { + ret = -EPERM ; + goto out; + } +if (!spm_mgr->spm_cntr) { + ret = -EINVAL ; + goto out; + } + +/* Stop monitor thread and hardware SPM for all instances. +* Monitor thread is shared across instances and stopped only once. +*/ +for_each_inst (inst,AMDGPU_XCC_MASK (adev)) +_spm_stop (spm_mgr, adev, inst); + +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 )); + +mutex_destroy (&(spm_mgr->spm_cntr->spm_worker_mutex)); +kfree (spm_mgr->spm_cntr); +spin_lock_irqsave (&spm_mgr->spm_irq_lock, flags); + 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,
@@ -392,6 +439,9 @@ int amdgpu_spm_ioctl(struct drm_device *dev, void *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
PATCH v4 13/17 drm/amdgpu: implement data dump from spm ring buffer
In reply to James Zhuto complete the amdgpu_spm_work() stub by implementing the full ring buffer drain pipeline: reading the hardware write pointer, copying counter data from the GTT ring buffer to the user-space destination buffer, advancing the hardware read pointer, and waking up caller once any XCC buffers are filled.
amdgpu_spm_data_copy(spm_mgr, size_to_copy, inst):
Copies size_to_copy bytes from the kernel-mapped GTT ring buffer to the user-space destination buffer for one XCC instance.
- Per the RLC hardware spec, ring_rptr=0 maps to cpu_addr + 0x20
(the ring data starts 0x20 bytes past the base of the BO).
- The destination user_address is computed as ubuf.user_addr + size_copied (continuing from where the previous copy left off).
- If size_to_copy fits within the remaining user buffer space (ubufsize - size_copied): copies the full amount, advances size_copied and ring_rptr by size_to_copy.
- If size_to_copy would overflow the user buffer: copies only the remaining space (user_buf_space_left), sets size_copied to ubufsize, advances ring_rptr, and marks is_user_buf_filled = true.
- Sets has_data_loss = 1 and returns -EFAULT on copy_to_user() failure.
amdgpu_spm_read_ring_buffer(spm_mgr, inst):
Reads the hardware wptr from cpu_addr[0] via READ_ONCE() followed by dma_rmb() to ensure ring buffer data written by the GPU is visible to the CPU before processing.
- If no user buffer is registered (has_user_buf=false) or the current buffer is already full (is_user_buf_filled=true): sets has_data_loss=1, force-sets is_user_buf_filled=true (to handle the polling timeout path where the flag was not previously set), and exits. This signals to the user that the SPM hardware may stall.
- If rptr == wptr: no new data, exits immediately.
- Linear case (wptr > rptr): calls amdgpu_spm_data_copy() for the contiguous segment.
- Wrap-around case (wptr < rptr): calls amdgpu_spm_data_copy() for the tail segment (rptr → ring_end). If rptr reaches ring_end exactly:
if wptr == 0, resets rptr to 0 and exits; otherwise resets rptr to 0 and calls amdgpu_spm_data_copy() for the wrapped head segment (0 → wptr), skipping the second copy if the first failed.
- On exit, calls amdgpu_rlc_spm_set_rdptr() to inform the hardware of the updated rptr, preventing ring buffer stalls.
amdgpu_spm_work() (work_struct handler, previously a stub):
Attaches the lead_thread's mm via kthread_use_mm() to permit copy_to_user() calls, then under spm_worker_mutex:
- Resets are_users_buf_filled to false via WRITE_ONCE().
- Calls amdgpu_spm_read_ring_buffer() for each active XCC.
- If any XCC's is_user_buf_filled is true, sets are_users_buf_filled via WRITE_ONCE() and calls wake_up() on spm_buf_wq to unblock any thread waiting in SET_DEST_BUF.
- Releases spm_worker_mutex before calling wake_up() to avoid unnecessary lock contention on the waiter's wakeup path.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 117 +++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +116 -1
@@ -35,16 +35,131 @@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 ); + + 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 =1 ; +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 =1 ; +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 size_to_copy; +int ret =0 ; +u32 ring_wptr; + + ring_wptr =READ_ONCE (spm->cpu_addr[0 ]); +dma_rmb (); + +/* 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 =1 ; +/* 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)) { +mutex_unlock (&spm_mgr->spm_cntr->spm_worker_mutex); +pr_debug ("SPM wake up buffer work queue." ); +wake_up (&spm_mgr->spm_cntr->spm_buf_wq); + }else { +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
PATCH v4 14/17 drm/amdgpu: workaround for spm ring buffer overflow
In reply to James Zhureserve space to avoid page fault and data loss.
A hardware bug on GFX IP versions earlier than 11.0.1 causes the RLC SPM hardware to write slightly beyond the end of the declared ring buffer when the ring wraps around. This can corrupt adjacent memory and cause page faults or silent data loss.
The workaround reserves an extra 0x400 bytes (1 KiB) of guard space immediately after the ring buffer's declared end, pads it with a known sentinel value (SPM_OVERFLOW_MAGIC = 0xBEEFABCDDEADABCDULL), and recovers any counter data that spilled into this region before it is overwritten by the next wrap-around. The overflow size varies, the overflow data is valid data, and the wrap-around write starts from cpu_addr + overflow_size + 0x20 instead of cpu_addr + 0x20.
Implementation:amdgpu_spm_init_device():
Detects affected hardware via ip_versions[GC_HWIP][0] < IP_VERSION(12, 0, 0) and sets spm_overflow_reserved = 0x400. This field is zero on unaffected hardware, making the entire workaround a no-op for GFX11+.
_amdgpu_spm_acquire():
Before calling amdgpu_rlc_spm_acquire(), subtracts spm_overflow_reserved from ring_size so the hardware is told a smaller ring size, leaving the guard region beyond the hardware's declared ring end. After the 0x20-byte metadata subtraction, calls amdgpu_spm_preset() to fill the guard region with SPM_OVERFLOW_MAGIC.
amdgpu_spm_preset(spm, size):
Writes SPM_OVERFLOW_MAGIC to the guard region starting at cpu_addr + ring_size + 0x20 for 'size' bytes. Writes are 0x20-byte (32-byte) stride aligned, matching the SPM hardware's fill granularity.
amdgpu_spm_read_ring_buffer() wrap-around path:
When wptr < rptr (ring has wrapped), scans the guard region in 0x20- byte steps to measure how many bytes of overflow data were written by the hardware (overflow_size): stops at the first 64-bit word still containing SPM_OVERFLOW_MAGIC (unwritten). If overflow_size > 0, logs a debug message and copies the overflow data back to ring_start (cpu_addr + 0x20) via memcpy() to make it available for normal copy processing. After the ring drain completes, calls amdgpu_spm_preset() to re-arm the guard region with SPM_OVERFLOW_MAGIC for the next wrap-around.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 42 +++++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h | 2 ++ 2 files changed, 44 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +42 -0
@@ -32,9 +32,24 @@#define AMDGPU_SPM_MAJOR_VERSION 0#define AMDGPU_SPM_MINOR_VERSION 1 +/* used to detect SPM overflow */ +#define SPM_OVERFLOW_MAGIC 0xBEEFABCDDEADABCDULL +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]);
@@ -79,6 +94,7 @@ 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;
@@ -110,6 +126,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);
@@ -128,6 +160,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; }
@@ -169,6 +202,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 (12 ,0 ,0 )) + spm_mgr->spm_overflow_reserved =0x400 ; + spm_mgr->spm_cntr =NULL ; }
@@ -194,6 +233,8 @@ static int _amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, int inst, structif (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);if (ret)
@@ -204,6 +245,7 @@ static int _amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, int inst, struct* and are instead part of the Meta data area. */ spm->ring_size -=0x20 ; +amdgpu_spm_preset (spm, spm_mgr->spm_overflow_reserved); goto out;
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h +2 -0
@@ -61,6 +61,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 *data,
Signature · 1 line
2.34.1
PATCH v4 15/17 drm/amdgpu: workaround for hw stall issue
In reply to James ZhuWhen RLC_SPM_PERFMON_CNTL.PERFMON_RING_MODE is set to 0x11, the SPM hardware is designed to fire a warning interrupt when the ring fill level reaches RLC_SPM_SEGMENT_THRESHOLD, and stall only when the ring is completely full. A hardware bug causes both the stall and the warning interrupt to trigger simultaneously at RLC_SPM_SEGMENT_THRESHOLD, resulting in an unexpected early hardware stall at interrupt time, which causes data loss before the ring buffer is actually full.
This patch replaces interrupt-driven ring drain scheduling with a software polling monitor thread to avoid triggering the premature stall.
Software polling monitor (amdgpu_spm_monitor_thread):
A kthread named "spm_<render_index>" polls each active XCC instance in a tight loop with usleep_range(1, 11) (1-11 us per iteration). On each iteration, it attempts to acquire spm_worker_mutex with mutex_trylock() (skipping if contended) and for each active XCC checks whether the amount of unconsumed data in the ring exceeds half the ring capacity, computed as:
(ring_size + ring_wptr - ring_rptr) % ring_size > (ring_size >> 1)
This condition fires only once per rptr advancement (guarded by warned_ring_rptr != ring_rptr) to avoid redundant scheduling. When triggered, schedules amdgpu_spm_work to drain the ring and logs a "soft interrupt" debug message with the current rptr and wptr.
warned_ring_rptr (u32, added to amdgpu_spm_base):
Tracks the rptr value at which the last soft interrupt was issued. Initialized to ~0U (U32_MAX) on acquire and on each SPM start/stop to ensure the first polling check always passes. Updated to the current rptr at the start of each ring buffer read to suppress duplicate scheduling for the same rptr position.
Monitor thread lifecycle:
- Started lazily on the first SET_DEST_BUF call that starts the SPM hardware (dest_buf != NULL, is_spm_started transitions false→true). Only one thread is created per SPM session regardless of XCC count.
- Stopped on SET_DEST_BUF with dest_buf=NULL (SPM stopped) and on AMDGPU_SPM_OP_RELEASE. spm_monitor_thread is initialized to NULL in amdgpu_spm_acquire() and set back to NULL by the thread itself on exit.
amdgpu_spm_interrupt():
- schedules amdgpu_spm_work directly. But when detects affected hardware via ip_versions[GC_HWIP][0] < IP_VERSION(12, 0, 0), the polling thread is responsible for all ring drain scheduling in this mode. the hardware interrupt only sets has_data_loss=1 under spm_irq_lock (indicating the ring reached the stall threshold) and logs a debug message.
-v3: signal_pending using current instead of spm_mgr->spm_monitor_thread to
fix race condition issue
use _spm_start/_spm_stop
--- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c | 102 +++++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_spm.h | 2 + 2 files changed, 102 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_spm.c +100 -2
@@ -38,6 +38,77 @@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 (current) && 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;
@@ -122,6 +193,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);
@@ -246,6 +318,7 @@ static int _amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, int inst, struct*/ spm->ring_size -=0x20 ;amdgpu_spm_preset (spm, spm_mgr->spm_overflow_reserved); + spm->warned_ring_rptr = ~0 ; goto out;
@@ -290,6 +363,7 @@ static int amdgpu_spm_acquire(struct amdgpu_spm_mgr *spm_mgr, struct drm_file *fINIT_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;
@@ -324,8 +398,21 @@ static int _spm_start(struct amdgpu_spm_mgr *spm_mgr,* 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) { + ret =amdgpu_spm_monitor_thread_start (spm_mgr); +if (ret) { +/* Thread failed to start, revert state */ +dev_dbg (adev->dev, +"Failed to start SPM monitor thread, ret = %d\n" , ret); +spin_lock_irqsave (&spm_mgr->spm_irq_lock, flags); + spm->is_spm_started = false; +spin_unlock_irqrestore (&spm_mgr->spm_irq_lock, flags); +amdgpu_rlc_spm_cntl (adev, inst,0 ); + } + }return ret; }
@@ -335,6 +422,9 @@ static void _spm_stop(struct amdgpu_spm_mgr *spm_mgr,struct amdgpu_spm_base *spm = &(spm_mgr->spm_cntr->spm[inst]);unsigned long flags; +if (spm_mgr->spm_monitor_thread) +kthread_stop (spm_mgr->spm_monitor_thread); +amdgpu_rlc_spm_cntl (adev, inst,0 );spin_lock_irqsave (&spm_mgr->spm_irq_lock, flags); spm_mgr->spm_cntr->spm[inst].is_spm_started = false;
@@ -342,6 +432,7 @@ static void _spm_stop(struct amdgpu_spm_mgr *spm_mgr,* Adjust rptr accordingly */ spm->ring_rptr =0 ; + spm->warned_ring_rptr = ~0 ;spin_unlock_irqrestore (&spm_mgr->spm_irq_lock, flags); }
@@ -633,8 +724,15 @@ void amdgpu_spm_interrupt(struct amdgpu_device *adev, int xcc_id) spm_mgr = &(adev->prof_mgr.prof_xcp_mgr[xcp_id].spm_mgr); +/* watermark threshold is triggered */ 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); +if (spm_mgr->spm_cntr && spm_mgr->spm_cntr->spm[xcc_id].is_spm_started) { +if (adev->ip_versions[GC_HWIP ][0 ] <IP_VERSION (12 ,0 ,0 )) + spm_mgr->spm_cntr->spm[xcc_id].has_data_loss =1 ; +else +schedule_work (&spm_mgr->spm_work); + }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 +2 -0
@@ -41,6 +41,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 {
@@ -53,6 +54,7 @@ struct amdgpu_spm_cntr { };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
PATCH v4 16/17 drm/amdgpu: add profiler/spm support for gfx9
In reply to James Zhuwith implementation of amdgpu_spm_funcs callbacks for the GFX 9.0 IP block and wire up the RLC Stream Performance Monitor interrupt GFX_9_0__SRCID__RLC_STRM_PERF_MONITOR_INTERRUPT into the IRQ framework.
SPM function table (gfx_v9_0_spm_funcs):
gfx_v9_0_spm_start(adev, 0):
Programs the KIQ ring to:
- Read-modify-write RLC_SPM_PERFMON_CNTL to set PERFMON_RING_MODE (0x1 = stall-on-full with interrupt at segment threshold).
- Write CP_PERFMON_CNTL.SPM_PERFMON_STATE = CP_PERFMON_STATE_DISABLE_AND_RESET to reset the SPM counters and clear wptr to 0; manually resets RLC_SPM_RING_RDPTR to 0 to match.
- Writes RLC_SPM_INT_CNTL = 1 to enable the SPM interrupt.
gfx_v9_0_spm_stop(adev, 0):
Programs the KIQ ring to:
- Set CP_PERFMON_CNTL.SPM_PERFMON_STATE = STOP_COUNTING.
- Set CP_PERFMON_CNTL.PERFMON_STATE = DISABLE_AND_RESET.
- Reset RLC_SPM_RING_RDPTR to 0 (wptr is also reset by hardware).
gfx_v9_0_spm_set_rdptr(adev, 0, rptr):
Writes rptr to RLC_SPM_RING_RDPTR via KIQ ring.
gfx_v9_0_set_spm_perfmon_ring_buf(adev, 0, gpu_addr, size):
Programs the ring buffer via KIQ ring:
- RLC_SPM_PERFMON_RING_BASE_LO/HI with the GPU address.
- RLC_SPM_PERFMON_RING_SIZE with the buffer size.
- RLC_SPM_SEGMENT_THRESHOLD = 1.
- CP_PERFMON_CNTL = 0 (clear any leftover perfmon state). set_spm_config_size = 30 DWORDs per KIQ ring operation.
gfx_v9_0_spm_funcs is registered in gfx_v9_0_set_spm_funcs(), called from gfx_v9_0_early_init() before KIQ and ring function setup.
SPM IRQ handling:
The SPM interrupt source is registered in gfx_v9_0_sw_init() via amdgpu_irq_add_id() with client SOC15_IH_CLIENTID_RLC and source ID GFX_9_0__SRCID__RLC_STRM_PERF_MONITOR_INTERRUPT, connected to adev->gfx.spm_irq.
gfx_v9_0_spm_set_interrupt_state(): enables or disables the SPM interrupt by writing RLC_SPM_INT_CNTL = 1/0 via direct MMIO.
gfx_v9_0_spm_irq(): called from the interrupt handler, invokes amdgpu_rlc_spm_interrupt(adev, 0) for XCC 0 (gfx9 is single-XCC).
amdgpu_irq_get(spm_irq) is called in gfx_v9_0_late_init() to enable the interrupt at the IRQ framework level. amdgpu_irq_put(spm_irq) is called in gfx_v9_0_hw_fini() to disable it.
--- 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
@@ -477,6 +477,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 amdgpu_irq_src rlc_poison_irq;
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
PATCH v4 17/17 drm/amdgpu: add profiler/spm support for gfx9_4_3
In reply to James Zhuwith implementation of amdgpu_spm_funcs callbacks for the GFX 9.4.3 IP block and wire up the per-XCC RLC Stream Performance Monitor interrupt.
SPM function table (gfx_v9_4_3_spm_funcs):
gfx_v9_4_3_spm_start(adev, xcc_id):
Uses kiq[xcc_id].ring for the per-XCC KIQ ring. Programs:
- Read-modify-write RLC_SPM_PERFMON_CNTL to set PERFMON_RING_MODE via MMIO read + KIQ write.
- CP_PERFMON_CNTL.SPM_PERFMON_STATE = DISABLE_AND_RESET via KIQ.
- RLC_SPM_RING_RDPTR = 0 via KIQ to match the hardware wptr reset.
- RLC_SPM_INT_CNTL = 1 via KIQ to enable the SPM interrupt.
- Read-modify-write RLC_CLK_CNTL to set RLC_SPM_CLK_CNTL_MASK via direct MMIO, gating the SPM clock domain on.
gfx_v9_4_3_spm_stop(adev, xcc_id):
Uses kiq[xcc_id].ring. Programs:
- Clear RLC_CLK_CNTL.RLC_SPM_CLK_CNTL_MASK via direct MMIO.
- CP_PERFMON_CNTL.SPM_PERFMON_STATE = STOP_COUNTING via KIQ.
- CP_PERFMON_CNTL.PERFMON_STATE = DISABLE_AND_RESET via KIQ.
- RLC_SPM_RING_RDPTR = 0 via KIQ.
gfx_v9_4_3_spm_set_rdptr(adev, xcc_id, rptr):
Writes rptr to RLC_SPM_RING_RDPTR via kiq[xcc_id].ring.
gfx_v9_4_3_set_spm_perfmon_ring_buf(adev, xcc_id, gpu_addr, size):
Programs via kiq[xcc_id].ring:
- RLC_SPM_PERFMON_RING_BASE_LO/HI with the GPU address (currently uses GC instance 0 for base address registers).
- RLC_SPM_PERFMON_RING_SIZE and RLC_SPM_SEGMENT_THRESHOLD = 1 using GET_INST(GC, xcc_id) addressing.
- CP_PERFMON_CNTL = 0.
set_spm_config_size = 30 DWORDs per KIQ ring operation.
gfx_v9_4_3_set_spm_funcs() registers gfx_v9_4_3_spm_funcs into adev->gfx.spmfuncs from gfx_v9_4_3_early_init().
SPM IRQ handling:
The SPM interrupt source is registered in gfx_v9_4_3_sw_init() for client SOC15_IH_CLIENTID_RLC with source ID GFX_9_0__SRCID__RLC_STRM_PERF_MONITOR_INTERRUPT.
gfx_v9_4_3_spm_set_interrupt_state(): iterates over all XCC instances and writes regRLC_SPM_INT_CNTL = 0/1 for each via WREG32_SOC15 with GET_INST(GC, i) to enable or disable the per-XCC interrupt.
gfx_v9_4_3_spm_irq(): resolves xcc_id from the interrupt entry's node_id via gfx_v9_4_3_ih_to_xcc_inst(), then calls amdgpu_rlc_spm_interrupt(adev, xcc_id) to dispatch to the correct XCP's SPM manager.
update_spm_vmid refactoring:
gfx_v9_4_3_update_spm_vmid_internal(adev, xcc_id, vmid) is extracted from the existing gfx_v9_4_3_update_spm_vmid() as a lockless helper that performs the read-modify-write on RLC_SPM_MC_CNTL.RLC_SPM_VMID using GET_INST(GC, xcc_id) addressing. The original RREG32() path is replaced with RREG32_SOC15() for correctness on multi-instance GC.
gfx_v9_4_3_update_spm_vmid() now wraps the internal helper with amdgpu_gfx_off_ctrl(false/true) to prevent GFX power gating during the VMID register update, and is used for runtime vmid changes.
gfx_v9_4_3_rlc_init() is updated to initialize the SPM VMID to 0xf for all XCC instances (instead of only XCC 0), and now skips the operation for SR-IOV VF or when update_spm_vmid is NULL.
gfx_v9_4_3_xcc_rlc_resume() is restructured to unconditionally call rlc_stop() before firmware loading and rlc_start() after, regardless of firmware load type. gfx_v9_4_3_update_spm_vmid_internal() is called between stop and start to ensure the SPM VMID is correctly initialized to 0xf before the RLC is started for both legacy and PSP firmware loading paths.
-v3: move RLC_CLK_CNTL__RLC_SPM_CLK_CNTL set/reset to rlc_init/rlc_fini
from spm_start and spm_stop, only apply on non-SRIOV, since this register is only for PF.
--- drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 203 ++++++++++++++++++++++-- 1 file changed, 192 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +192 -11
@@ -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);
@@ -1066,6 +1068,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)
@@ -1454,10 +1463,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 ; }
@@ -1628,22 +1641,39 @@ static int gfx_v9_4_3_xcc_rlc_load_microcode(struct amdgpu_device *adev,return 0 ; } +static void gfx_v9_4_3_xcc_rlc_spm_clk_enable (struct amdgpu_device *adev,bool enable,int xcc_id) +{ +uint32_t data =0 ; + + data =RREG32_SOC15 (GC ,GET_INST (GC , xcc_id), regRLC_CLK_CNTL); + +if (enable) + data |=RLC_CLK_CNTL__RLC_SPM_CLK_CNTL_MASK ; +else + data &= (~RLC_CLK_CNTL__RLC_SPM_CLK_CNTL_MASK ); + +WREG32_SOC15 (GC ,GET_INST (GC , xcc_id), regRLC_CLK_CNTL, data); +} +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 */ WREG32_SOC15 (GC ,GET_INST (GC , xcc_id), regRLC_CGCG_CGLS_CTRL,0 ); + +gfx_v9_4_3_xcc_rlc_spm_clk_enable (adev, true, xcc_id);gfx_v9_4_3_xcc_init_pg (adev, xcc_id);amdgpu_gfx_rlc_exit_safe_mode (adev, xcc_id);
@@ -1667,28 +1697,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)},
@@ -2347,6 +2387,7 @@ static void gfx_v9_4_3_xcc_fini(struct amdgpu_device *adev, int xcc_id) }gfx_v9_4_3_xcc_kcq_fini_register (adev, xcc_id); +gfx_v9_4_3_xcc_rlc_spm_clk_enable (adev, false, xcc_id);gfx_v9_4_3_xcc_cp_compute_enable (adev, false, xcc_id); }
@@ -2447,6 +2488,7 @@ static int gfx_v9_4_3_hw_fini(struct amdgpu_ip_block *ip_block)gfx_v9_4_3_perf_monitor_ptl_init (adev, false);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 );
@@ -2590,12 +2632,104 @@ 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 =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 ); +} + +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 =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 ,GET_INST (GC , xcc_id), + 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 ,GET_INST (GC , xcc_id), + 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 amdgpu_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);
@@ -2617,6 +2751,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;
@@ -3477,6 +3615,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) {
@@ -4904,11 +5077,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