drm/amdgpu: Add Peak Tops Limiter (PTL)
Subsystems (5)
v2 cover drm/amdgpu: Add Peak Tops Limiter (PTL)
This series adds support for Peak Tops Limiter (PTL) on AMD GFX 9.4.4 GPUs. PTL is a hardware feature that limits peak computational throughput to stay within power and thermal constraints.
v1->v2:
- Rework patch set based on review feedback (Alex)
- Consolidate PTL structure and interface definitions
- Drop patches that have been merged into earlier patches
- Add explicit PSP response status check in psp_ptl_invoke()
- Add GFX idle wait before PTL state transition to prevent CP hang
- Unify refcount logic for PTL disable/enable
- Rework profiler IOCTL interface based on review feedback (James)
Benjamin Welton (1):
Add kfd_ioctl_profiler to contain profiler kernel driver changes
Perry Yuan (12):
drm/amdgpu: add new performance monitor PSP interfaces drm/amdgpu: add psp interfaces for peak tops limiter driver drm/amdgpu: add PTL enable/query gfx control support for GC 9.4.4 drm/amdkfd: add kgd control interface for ptl Documentation/amdgpu: Add documentation for Peak Tops Limiter (PTL)
sysfs interface
drm/amdgpu: add sysfs for Peak Tops Limiter (PTL) drm/amdkfd: Add PTL control IOCTL Option and unify refcount logic drm/amdkfd: suspend scheduler during PTL re-enabling drm/amdgpu: Track PTL disable requests by source drm/amdgpu: add amdgpu.ptl module parameter for PTL control drm/amdgpu: add new data types F8 and Vector for PTL drm/amdgpu: Wait for GFX idle before PTL state transition
Documentation/gpu/amdgpu/index.rst | 1 + Documentation/gpu/amdgpu/ptl.rst | 94 +++++ drivers/gpu/drm/amd/amdgpu/amdgpu.h | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 + .../drm/amd/amdgpu/amdgpu_amdkfd_gc_9_4_3.c | 13 +- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 3 + drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 13 + drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 349 ++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h | 27 +- drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 62 ++++ drivers/gpu/drm/amd/amdgpu/psp_gfx_if.h | 17 + drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 219 +++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_device.c | 4 + .../drm/amd/amdkfd/kfd_device_queue_manager.c | 25 ++ .../drm/amd/amdkfd/kfd_device_queue_manager.h | 2 + .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c | 16 +- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c | 14 +- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c | 8 +- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c | 15 +- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c | 11 + drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 16 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 15 + drivers/gpu/drm/amd/include/amdgpu_ptl.h | 61 +++ .../gpu/drm/amd/include/kgd_kfd_interface.h | 6 + include/uapi/linux/kfd_ioctl.h | 36 ++ 25 files changed, 1020 insertions(+), 10 deletions(-) create mode 100644 Documentation/gpu/amdgpu/ptl.rst create mode 100644 drivers/gpu/drm/amd/include/amdgpu_ptl.h
Signature · 1 line
2.34.1
PATCH v2 1/13 Add kfd_ioctl_profiler to contain profiler kernel driver changes
From: Benjamin Welton <[email protected]>
kfd_ioctl_profiler takes a similar approach to that of kfd_ioctl_dbg_trap (which contains debugger related IOCTL services) where kfd_ioctl_profiler will contain all profiler related IOCTL services. The IOCTL is designed to be expanded as needed to support additional profiler functionality.
The current functionality of the IOCTL is to allow for profilers which need PMC counters from GPU devices to both signal to other profilers that may be on the system that the device has active PMC profiling taking place on it (multiple PMC profilers on the same device can result in corrupted counter data) and to setup the device to allow for the collection of SQ PMC data on all queues on the device.
For PMC data for the SQ block (such as SQ_WAVES) to be available to a profiler, mmPERFCOUNT_ENABLE must be set on the queues. When profiling a single process, the profiler can inject PM4 packets into each queue to turn on PERFCOUNT_ENABLE. When profiling system wide, the profiler does not have this option and must have a way to turn on profiling for queues in which it cannot inject packets into directly.
Accomplishing this requires a few steps:
1. Checking if the user has the necessary permissions to profile system wide on the device. This check uses the same check that linux perf uses to determine if a user has the necessary permissions to profile at this scope (primarily if the process has CAP_SYS_PERFMON or is root).
2. Locking the device for profiling. This is done by setting a lock bit on the device struct and storing the process that locked the device.
3. Iterating all queues on the device and issuing an MQD Update to enable perfcounting on the queues.
4. Actions to cleanup if the process exits or releases the lock.
The IOCTL also contains a link to the existing PC Sampling IOCTL as well. This is per a suggestion that we should potentially remove the PC Sampling IOCTL to have it be a part of the profiler IOCTL. This is a future change. In addition, we do expect to expand the profiler IOCTL to include additional profiler functionality in the future (which necessitates the use of a version number).
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 82 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_device.c | 4 + .../drm/amd/amdkfd/kfd_device_queue_manager.c | 25 ++++++ .../drm/amd/amdkfd/kfd_device_queue_manager.h | 2 + .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c | 16 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c | 14 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c | 8 +- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c | 15 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c | 11 +++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 7 ++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 11 +++ include/uapi/linux/kfd_ioctl.h | 29 +++++++ 12 files changed, 216 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +82 -0
@@ -21,6 +21,7 @@* OTHER DEALINGS IN THE SOFTWARE. */ +#include <linux/capability.h> #include <linux/device.h> #include <linux/err.h> #include <linux/fs.h>
@@ -3204,6 +3205,84 @@ static int kfd_ioctl_create_process(struct file *filep, struct kfd_process *p, vreturn 0 ; } +static inline uint32_t profile_lock_device (struct kfd_process *p, +uint32_t gpu_id,uint32_t op) +{ +struct kfd_process_device *pdd; +struct kfd_dev *kfd; +int status = -EINVAL ; + +if (!p) +return -EINVAL ; + +mutex_lock (&p->mutex); + pdd =kfd_process_device_data_by_id (p, gpu_id); +mutex_unlock (&p->mutex); + +if (!pdd || !pdd->dev || !pdd->dev->kfd) +return -EINVAL ; + + kfd = pdd->dev->kfd; + +mutex_lock (&kfd->profiler_lock); +if (op ==1 ) { +if (!kfd->profiler_process) { + kfd->profiler_process = p; + status =0 ; + }else if (kfd->profiler_process == p) { + status = -EALREADY ; + }else { + status = -EBUSY ; + } + }else if (op ==0 && kfd->profiler_process == p) { + kfd->profiler_process =NULL ; + status =0 ; + } +mutex_unlock (&kfd->profiler_lock); + +return status; +} + +static inline int kfd_profiler_pmc (struct kfd_process *p, +struct kfd_ioctl_pmc_settings *args) +{ +struct kfd_process_device *pdd; +struct device_queue_manager *dqm; +int status; + +/* Check if we have the correct permissions. */ +if (!perfmon_capable ()) +return -EPERM ; + +/* Lock/Unlock the device based on the parameter given in OP */ + status =profile_lock_device (p, args->gpu_id, args->lock); +if (status !=0 ) +return status; + +/* Enable/disable perfcount if requested */ +mutex_lock (&p->mutex); + pdd =kfd_process_device_data_by_id (p, args->gpu_id); + dqm = pdd->dev->dqm; +mutex_unlock (&p->mutex); + + dqm->ops.set_perfcount (dqm, args->perfcount_enable); +return status; +} + +static int kfd_ioctl_profiler (struct file *filep,struct kfd_process *p,void *data) +{ +struct kfd_ioctl_profiler_args *args = data; + +switch (args->op) { +case KFD_IOC_PROFILER_VERSION : + args->version =KFD_IOC_PROFILER_VERSION_NUM ; +return 0 ; +case KFD_IOC_PROFILER_PMC : +return kfd_profiler_pmc (p, &args->pmc); + } +return -EINVAL ; +} +#define AMDKFD_IOCTL_DEF (ioctl, _func, _flags) \ [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \ .cmd_drv = 0, .name = #ioctl}
@@ -3325,6 +3404,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {AMDKFD_IOCTL_DEF (AMDKFD_IOC_CREATE_PROCESS , kfd_ioctl_create_process,0 ), + +AMDKFD_IOCTL_DEF (AMDKFD_IOC_PROFILER , + kfd_ioctl_profiler, 0), };#define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +4 -0
@@ -936,6 +936,9 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,svm_range_set_max_pages (kfd->adev); + kfd->profiler_process =NULL ; +mutex_init (&kfd->profiler_lock); + kfd->init_complete = true;dev_info (kfd_device,"added device %x:%x\n" , kfd->adev->pdev->vendor, kfd->adev->pdev->device);
@@ -971,6 +974,7 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd)ida_destroy (&kfd->doorbell_ida);kfd_gtt_sa_fini (kfd);amdgpu_amdkfd_free_kernel_mem (kfd->adev, &kfd->gtt_mem); +mutex_destroy (&kfd->profiler_lock); }kfree (kfd);
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +25 -0
@@ -305,6 +305,29 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,return r; } +static void set_perfcount (struct device_queue_manager *dqm,int enable) +{ +struct device_process_node *cur; +struct qcm_process_device *qpd; +struct queue *q; +struct mqd_update_info minfo = {0 }; + +if (!dqm) +return ; + + minfo.update_flag = (enable ==1 ?UPDATE_FLAG_PERFCOUNT_ENABLE : +UPDATE_FLAG_PERFCOUNT_DISABLE ); +dqm_lock (dqm); +list_for_each_entry (cur, &dqm->queues, list) { + qpd = cur->qpd; +list_for_each_entry (q, &qpd->queues_list, list) { +pqm_update_mqd (qpd->pqm, q->properties.queue_id, + &minfo); + } + } +dqm_unlock (dqm); +} +static int remove_all_kfd_queues_mes (struct device_queue_manager *dqm) {struct device_process_node *cur;
@@ -2967,6 +2990,7 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev) dqm->ops.reset_queues = reset_queues_cpsch; dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; dqm->ops.checkpoint_mqd = checkpoint_mqd; + dqm->ops.set_perfcount = set_perfcount;break ;case KFD_SCHED_POLICY_NO_HWS :/* initialize dqm for no cp scheduling */
@@ -2987,6 +3011,7 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev) dqm->ops.get_wave_state = get_wave_state; dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; dqm->ops.checkpoint_mqd = checkpoint_mqd; + dqm->ops.set_perfcount = set_perfcount;break ;default :dev_err (dev->adev->dev,"Invalid scheduling policy %d\n" , dqm->sched_policy);
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h +2 -0
@@ -200,6 +200,8 @@ struct device_queue_manager_ops {const struct queue *q, void *mqd, void *ctl_stack); +void (*set_perfcount)(struct device_queue_manager *dqm, +int enable); };struct device_queue_manager_asic_ops {
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +14 -2
@@ -124,10 +124,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,*/ m->cp_hqd_hq_scheduler0 =1 <<14 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=
@@ -142,6 +141,12 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; + +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;
@@ -221,6 +226,13 @@ static void update_mqd(struct mqd_manager *mm, void *mqd,if (mm->dev->kfd->cwsr_enabled) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + } +update_cu_mask (mm, mqd, minfo);set_priority (m, q);
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +12 -2
@@ -164,10 +164,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,if (amdgpu_amdkfd_have_atomics_support (mm->dev->adev)) m->cp_hqd_hq_status0 |=1 <<29 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=
@@ -182,6 +181,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;
@@ -259,6 +263,12 @@ static void update_mqd(struct mqd_manager *mm, void *mqd, }if (mm->dev->kfd->cwsr_enabled) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + }update_cu_mask (mm, mqd, minfo);set_priority (m, q);
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c +6 -2
@@ -139,10 +139,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,if (amdgpu_amdkfd_have_atomics_support (mm->dev->adev)) m->cp_hqd_hq_status0 |=1 <<29 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=
@@ -157,6 +156,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +13 -2
@@ -218,10 +218,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; -if (q->tba_addr) { +if (q->tba_addr) m->compute_pgm_rsrc2 |= (1 <<COMPUTE_PGM_RSRC2__TRAP_PRESENT__SHIFT ); - }if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) { m->cp_hqd_persistent_state |=
@@ -236,6 +235,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;
@@ -318,6 +322,13 @@ static void update_mqd(struct mqd_manager *mm, void *mqd,if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + } +if (KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,4 ,3 ) &&KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,4 ,4 ) &&KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,5 ,0 ))
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +11 -0
@@ -149,6 +149,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;
@@ -231,6 +236,12 @@ static void __update_mqd(struct mqd_manager *mm, void *mqd, m->cp_hqd_ctx_save_control = atc_bit <<CP_HQD_CTX_SAVE_CONTROL__ATC__SHIFT | mtype <<CP_HQD_CTX_SAVE_CONTROL__MTYPE__SHIFT ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + }update_cu_mask (mm, mqd, minfo);set_priority (m, q);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +7 -0
@@ -384,6 +384,11 @@ struct kfd_dev {int kfd_dev_lock;atomic_t kfd_processes_count; + +/* Lock for profiler process */ +struct mutex profiler_lock; +/* Process currently holding the lock */ +struct kfd_process *profiler_process; };enum kfd_mempool {
@@ -556,6 +561,8 @@ enum mqd_update_flag {UPDATE_FLAG_DBG_WA_ENABLE =1 ,UPDATE_FLAG_DBG_WA_DISABLE =2 ,UPDATE_FLAG_IS_GWS =4 ,/* quirk for gfx9 IP */ +UPDATE_FLAG_PERFCOUNT_ENABLE =5 , + UPDATE_FLAG_PERFCOUNT_DISABLE = 6, }; struct mqd_update_info {
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11 -0
@@ -1110,6 +1110,16 @@ static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)kfd_process_device_free_bos (p->pdds[i]); } +static void kfd_process_profiler_release (struct kfd_process *p,struct kfd_process_device *pdd) +{ +mutex_lock (&pdd->dev->kfd->profiler_lock); +if (pdd->dev->kfd->profiler_process == p) { + pdd->qpd.dqm->ops.set_perfcount (pdd->qpd.dqm,0 ); + pdd->dev->kfd->profiler_process =NULL ; + } +mutex_unlock (&pdd->dev->kfd->profiler_lock); +} +static void kfd_process_destroy_pdds (struct kfd_process *p) {int i;
@@ -1121,6 +1131,7 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)pr_debug ("Releasing pdd (topology id %d, for pid %d)\n" , pdd->dev->id, p->lead_thread->pid); +kfd_process_profiler_release (p, pdd);kfd_process_device_destroy_cwsr_dgpu (pdd);kfd_process_device_destroy_ib_mem (pdd);
include/uapi/linux/kfd_ioctl.h +29 -0
@@ -1558,6 +1558,29 @@ struct kfd_ioctl_dbg_trap_args { }; }; +#define KFD_IOC_PROFILER_VERSION_NUM 1 +enum kfd_profiler_ops { +KFD_IOC_PROFILER_PMC =0 , +KFD_IOC_PROFILER_VERSION =2 , +}; + +/** +* Enables/Disables GPU Specific profiler settings +*/ +struct kfd_ioctl_pmc_settings { +__u32 gpu_id;/* This is the user_gpu_id */ +__u32 lock;/* Lock GPU for Profiling */ +__u32 perfcount_enable;/* Force Perfcount Enable for queues on GPU */ +}; + +struct kfd_ioctl_profiler_args { +__u32 op;/* kfd_profiler_op */ +union { +struct kfd_ioctl_pmc_settings pmc; +__u32 version;/* KFD_IOC_PROFILER_VERSION_NUM */ + }; +}; +#define AMDKFD_IOCTL_BASE 'K'#define AMDKFD_IO (nr) _IO(AMDKFD_IOCTL_BASE, nr)#define AMDKFD_IOR (nr, type) _IOR(AMDKFD_IOCTL_BASE, nr, type)
@@ -1684,4 +1707,10 @@ struct kfd_ioctl_dbg_trap_args {#define AMDKFD_COMMAND_START 0x01#define AMDKFD_COMMAND_END 0x28 +#define AMDKFD_IOC_PROFILER \ + AMDKFD_IOWR(0x86, struct kfd_ioctl_profiler_args) + +#define AMDKFD_COMMAND_START_2 0x80 +#define AMDKFD_COMMAND_END_2 0x87 +#endif
Signature · 1 line
2.34.1
Re: [PATCH v2 01/13] Add kfd_ioctl_profiler to contain profiler kernel driver changes
Quoted 45 lines · Perry Yuan
From: Benjamin Welton <[email protected]>
kfd_ioctl_profilertakes a similar approach to that ofkfd_ioctl_dbg_trap(which contains debugger related IOCTL services) wherekfd_ioctl_profilerwill contain all profiler related IOCTL services. The IOCTL is designed to be expanded as needed to support additional profiler functionality.The current functionality of the IOCTL is to allow for profilers which need PMC counters from GPU devices to both signal to other profilers that may be on the system that the device has active PMC profiling taking place on it (multiple PMC profilers on the same device can result in corrupted counter data) and to setup the device to allow for the collection of SQ PMC data on all queues on the device.
For PMC data for the SQ block (such as
SQ_WAVES) to be available to a profiler,mmPERFCOUNT_ENABLEmust be set on the queues. When profiling a single process, the profiler can inject PM4 packets into each queue to turn onPERFCOUNT_ENABLE. When profiling system wide, the profiler does not have this option and must have a way to turn on profiling for queues in which it cannot inject packets into directly.Accomplishing this requires a few steps:
1. Checking if the user has the necessary permissions to profile system wide on the device. This check uses the same check that linux perf uses to determine if a user has the necessary permissions to profile at this scope (primarily if the process has
CAP_SYS_PERFMONor is root).2. Locking the device for profiling. This is done by setting a lock bit on the device struct and storing the process that locked the device.
3. Iterating all queues on the device and issuing an MQD Update to enable perfcounting on the queues.
4. Actions to cleanup if the process exits or releases the lock.
The IOCTL also contains a link to the existing PC Sampling IOCTL as well. This is per a suggestion that we should potentially remove the PC Sampling IOCTL to have it be a part of the profiler IOCTL. This is a future change. In addition, we do expect to expand the profiler IOCTL to include additional profiler functionality in the future (which necessitates the use of a version number).
Quoted 4 lines
Signed-off-by Benjamin Welton <[email protected]>Signed-off-by Perry Yuan <[email protected]>Acked-by Kent Russell <[email protected]>Reviewed-by Yifan Zhang <[email protected]>
Please include the link to the usermode stuff that uses this new IOCTL.
Alex
Quoted 492 lines
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 82 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_device.c | 4 + .../drm/amd/amdkfd/kfd_device_queue_manager.c | 25 ++++++ .../drm/amd/amdkfd/kfd_device_queue_manager.h | 2 + .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c | 16 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c | 14 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c | 8 +- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c | 15 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c | 11 +++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 7 ++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 11 +++ include/uapi/linux/kfd_ioctl.h | 29 +++++++ 12 files changed, 216 insertions(+), 8 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +82
-0@@ -21,6 +21,7 @@* OTHER DEALINGS IN THE SOFTWARE. */ +#include <linux/capability.h> #include <linux/device.h> #include <linux/err.h> #include <linux/fs.h> @@ -3204,6 +3205,84 @@ static int kfd_ioctl_create_process(struct file *filep, struct kfd_process *p, vreturn 0 ; } +static inline uint32_t profile_lock_device (struct kfd_process *p, +uint32_t gpu_id,uint32_t op) +{ +struct kfd_process_device *pdd; +struct kfd_dev *kfd; +int status = -EINVAL ; + +if (!p) +return -EINVAL ; + +mutex_lock (&p->mutex); + pdd =kfd_process_device_data_by_id (p, gpu_id); +mutex_unlock (&p->mutex); + +if (!pdd || !pdd->dev || !pdd->dev->kfd) +return -EINVAL ; + + kfd = pdd->dev->kfd; + +mutex_lock (&kfd->profiler_lock); +if (op ==1 ) { +if (!kfd->profiler_process) { + kfd->profiler_process = p; + status =0 ; + }else if (kfd->profiler_process == p) { + status = -EALREADY ; + }else { + status = -EBUSY ; + } + }else if (op ==0 && kfd->profiler_process == p) { + kfd->profiler_process =NULL ; + status =0 ; + } +mutex_unlock (&kfd->profiler_lock); + +return status; +} + +static inline int kfd_profiler_pmc (struct kfd_process *p, +struct kfd_ioctl_pmc_settings *args) +{ +struct kfd_process_device *pdd; +struct device_queue_manager *dqm; +int status; + +/* Check if we have the correct permissions. */ +if (!perfmon_capable ()) +return -EPERM ; + +/* Lock/Unlock the device based on the parameter given in OP */ + status =profile_lock_device (p, args->gpu_id, args->lock); +if (status !=0 ) +return status; + +/* Enable/disable perfcount if requested */ +mutex_lock (&p->mutex); + pdd =kfd_process_device_data_by_id (p, args->gpu_id); + dqm = pdd->dev->dqm; +mutex_unlock (&p->mutex); + + dqm->ops.set_perfcount (dqm, args->perfcount_enable); +return status; +} + +static int kfd_ioctl_profiler (struct file *filep,struct kfd_process *p,void *data) +{ +struct kfd_ioctl_profiler_args *args = data; + +switch (args->op) { +case KFD_IOC_PROFILER_VERSION : + args->version =KFD_IOC_PROFILER_VERSION_NUM ; +return 0 ; +case KFD_IOC_PROFILER_PMC : +return kfd_profiler_pmc (p, &args->pmc); + } +return -EINVAL ; +} +#define AMDKFD_IOCTL_DEF (ioctl, _func, _flags) \ [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \ .cmd_drv = 0, .name = #ioctl}@@ -3325,6 +3404,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {AMDKFD_IOCTL_DEF (AMDKFD_IOC_CREATE_PROCESS , kfd_ioctl_create_process,0 ), + +AMDKFD_IOCTL_DEF (AMDKFD_IOC_PROFILER , + kfd_ioctl_profiler, 0), };#define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)drivers/gpu/drm/amd/amdkfd/kfd_device.c +4
-0@@ -936,6 +936,9 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,svm_range_set_max_pages (kfd->adev); + kfd->profiler_process =NULL ; +mutex_init (&kfd->profiler_lock); + kfd->init_complete = true;dev_info (kfd_device,"added device %x:%x\n" , kfd->adev->pdev->vendor, kfd->adev->pdev->device);@@ -971,6 +974,7 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd)ida_destroy (&kfd->doorbell_ida);kfd_gtt_sa_fini (kfd);amdgpu_amdkfd_free_kernel_mem (kfd->adev, &kfd->gtt_mem); +mutex_destroy (&kfd->profiler_lock); }kfree (kfd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +25
-0@@ -305,6 +305,29 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,return r; } +static void set_perfcount (struct device_queue_manager *dqm,int enable) +{ +struct device_process_node *cur; +struct qcm_process_device *qpd; +struct queue *q; +struct mqd_update_info minfo = {0 }; + +if (!dqm) +return ; + + minfo.update_flag = (enable ==1 ?UPDATE_FLAG_PERFCOUNT_ENABLE : +UPDATE_FLAG_PERFCOUNT_DISABLE ); +dqm_lock (dqm); +list_for_each_entry (cur, &dqm->queues, list) { + qpd = cur->qpd; +list_for_each_entry (q, &qpd->queues_list, list) { +pqm_update_mqd (qpd->pqm, q->properties.queue_id, + &minfo); + } + } +dqm_unlock (dqm); +} +static int remove_all_kfd_queues_mes (struct device_queue_manager *dqm) {struct device_process_node *cur;@@ -2967,6 +2990,7 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev) dqm->ops.reset_queues = reset_queues_cpsch; dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; dqm->ops.checkpoint_mqd = checkpoint_mqd; + dqm->ops.set_perfcount = set_perfcount;break ;case KFD_SCHED_POLICY_NO_HWS :/* initialize dqm for no cp scheduling */ @@ -2987,6 +3011,7 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev) dqm->ops.get_wave_state = get_wave_state; dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; dqm->ops.checkpoint_mqd = checkpoint_mqd; + dqm->ops.set_perfcount = set_perfcount;break ;default :dev_err (dev->adev->dev,"Invalid scheduling policy %d\n" , dqm->sched_policy);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h +2
-0@@ -200,6 +200,8 @@ struct device_queue_manager_ops {const struct queue *q, void *mqd, void *ctl_stack); +void (*set_perfcount)(struct device_queue_manager *dqm, +int enable); };struct device_queue_manager_asic_ops {drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +14
-2@@ -124,10 +124,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,*/ m->cp_hqd_hq_scheduler0 =1 <<14 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=@@ -142,6 +141,12 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; + +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -221,6 +226,13 @@ static void update_mqd(struct mqd_manager *mm, void *mqd,if (mm->dev->kfd->cwsr_enabled) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + } +update_cu_mask (mm, mqd, minfo);set_priority (m, q);drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +12
-2@@ -164,10 +164,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,if (amdgpu_amdkfd_have_atomics_support (mm->dev->adev)) m->cp_hqd_hq_status0 |=1 <<29 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=@@ -182,6 +181,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -259,6 +263,12 @@ static void update_mqd(struct mqd_manager *mm, void *mqd, }if (mm->dev->kfd->cwsr_enabled) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + }update_cu_mask (mm, mqd, minfo);set_priority (m, q);drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c +6
-2@@ -139,10 +139,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,if (amdgpu_amdkfd_have_atomics_support (mm->dev->adev)) m->cp_hqd_hq_status0 |=1 <<29 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=@@ -157,6 +156,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +13
-2@@ -218,10 +218,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; -if (q->tba_addr) { +if (q->tba_addr) m->compute_pgm_rsrc2 |= (1 <<COMPUTE_PGM_RSRC2__TRAP_PRESENT__SHIFT ); - }if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) { m->cp_hqd_persistent_state |=@@ -236,6 +235,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -318,6 +322,13 @@ static void update_mqd(struct mqd_manager *mm, void *mqd,if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + } +if (KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,4 ,3 ) &&KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,4 ,4 ) &&KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,5 ,0 ))drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +11
-0@@ -149,6 +149,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -231,6 +236,12 @@ static void __update_mqd(struct mqd_manager *mm, void *mqd, m->cp_hqd_ctx_save_control = atc_bit <<CP_HQD_CTX_SAVE_CONTROL__ATC__SHIFT | mtype <<CP_HQD_CTX_SAVE_CONTROL__MTYPE__SHIFT ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + }update_cu_mask (mm, mqd, minfo);set_priority (m, q);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +7
-0@@ -384,6 +384,11 @@ struct kfd_dev {int kfd_dev_lock;atomic_t kfd_processes_count; + +/* Lock for profiler process */ +struct mutex profiler_lock; +/* Process currently holding the lock */ +struct kfd_process *profiler_process; };enum kfd_mempool {@@ -556,6 +561,8 @@ enum mqd_update_flag {UPDATE_FLAG_DBG_WA_ENABLE =1 ,UPDATE_FLAG_DBG_WA_DISABLE =2 ,UPDATE_FLAG_IS_GWS =4 ,/* quirk for gfx9 IP */ +UPDATE_FLAG_PERFCOUNT_ENABLE =5 , + UPDATE_FLAG_PERFCOUNT_DISABLE = 6, }; struct mqd_update_info {drivers/gpu/drm/amd/amdkfd/kfd_process.c +11
-0@@ -1110,6 +1110,16 @@ static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)kfd_process_device_free_bos (p->pdds[i]); } +static void kfd_process_profiler_release (struct kfd_process *p,struct kfd_process_device *pdd) +{ +mutex_lock (&pdd->dev->kfd->profiler_lock); +if (pdd->dev->kfd->profiler_process == p) { + pdd->qpd.dqm->ops.set_perfcount (pdd->qpd.dqm,0 ); + pdd->dev->kfd->profiler_process =NULL ; + } +mutex_unlock (&pdd->dev->kfd->profiler_lock); +} +static void kfd_process_destroy_pdds (struct kfd_process *p) {int i;@@ -1121,6 +1131,7 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)pr_debug ("Releasing pdd (topology id %d, for pid %d)\n" , pdd->dev->id, p->lead_thread->pid); +kfd_process_profiler_release (p, pdd);kfd_process_device_destroy_cwsr_dgpu (pdd);kfd_process_device_destroy_ib_mem (pdd);include/uapi/linux/kfd_ioctl.h +29
-1@@ -1558,6 +1558,29 @@ struct kfd_ioctl_dbg_trap_args { }; }; +#define KFD_IOC_PROFILER_VERSION_NUM 1 +enum kfd_profiler_ops { +KFD_IOC_PROFILER_PMC =0 , +KFD_IOC_PROFILER_VERSION =2 , +}; + +/** +* Enables/Disables GPU Specific profiler settings +*/ +struct kfd_ioctl_pmc_settings { +__u32 gpu_id;/* This is the user_gpu_id */ +__u32 lock;/* Lock GPU for Profiling */ +__u32 perfcount_enable;/* Force Perfcount Enable for queues on GPU */ +}; + +struct kfd_ioctl_profiler_args { +__u32 op;/* kfd_profiler_op */ +union { +struct kfd_ioctl_pmc_settings pmc; +__u32 version;/* KFD_IOC_PROFILER_VERSION_NUM */ + }; +}; +#define AMDKFD_IOCTL_BASE 'K'#define AMDKFD_IO (nr) _IO(AMDKFD_IOCTL_BASE, nr)#define AMDKFD_IOR (nr, type) _IOR(AMDKFD_IOCTL_BASE, nr, type)@@ -1684,4 +1707,10 @@ struct kfd_ioctl_dbg_trap_args {#define AMDKFD_COMMAND_START 0x01#define AMDKFD_COMMAND_END 0x28 +#define AMDKFD_IOC_PROFILER \ + AMDKFD_IOWR(0x86, struct kfd_ioctl_profiler_args) + +#define AMDKFD_COMMAND_START_2 0x80 +#define AMDKFD_COMMAND_END_2 0x87 +#endif --2.34.1
Alex Deucher
Acked-byRe: [PATCH v2 01/13] Add kfd_ioctl_profiler to contain profiler kernel driver changes
Quoted 47 lines · Alex Deucher
Quoted 45 lines · Perry Yuan
From: Benjamin Welton <[email protected]>
kfd_ioctl_profilertakes a similar approach to that ofkfd_ioctl_dbg_trap(which contains debugger related IOCTL services) wherekfd_ioctl_profilerwill contain all profiler related IOCTL services. The IOCTL is designed to be expanded as needed to support additional profiler functionality.The current functionality of the IOCTL is to allow for profilers which need PMC counters from GPU devices to both signal to other profilers that may be on the system that the device has active PMC profiling taking place on it (multiple PMC profilers on the same device can result in corrupted counter data) and to setup the device to allow for the collection of SQ PMC data on all queues on the device.
For PMC data for the SQ block (such as
SQ_WAVES) to be available to a profiler,mmPERFCOUNT_ENABLEmust be set on the queues. When profiling a single process, the profiler can inject PM4 packets into each queue to turn onPERFCOUNT_ENABLE. When profiling system wide, the profiler does not have this option and must have a way to turn on profiling for queues in which it cannot inject packets into directly.Accomplishing this requires a few steps:
1. Checking if the user has the necessary permissions to profile system wide on the device. This check uses the same check that linux perf uses to determine if a user has the necessary permissions to profile at this scope (primarily if the process has
CAP_SYS_PERFMONor is root).2. Locking the device for profiling. This is done by setting a lock bit on the device struct and storing the process that locked the device.
3. Iterating all queues on the device and issuing an MQD Update to enable perfcounting on the queues.
4. Actions to cleanup if the process exits or releases the lock.
The IOCTL also contains a link to the existing PC Sampling IOCTL as well. This is per a suggestion that we should potentially remove the PC Sampling IOCTL to have it be a part of the profiler IOCTL. This is a future change. In addition, we do expect to expand the profiler IOCTL to include additional profiler functionality in the future (which necessitates the use of a version number).
Quoted 6 lines
Quoted 4 lines
Signed-off-by Benjamin Welton <[email protected]>Signed-off-by Perry Yuan <[email protected]>Acked-by Kent Russell <[email protected]>Reviewed-by Yifan Zhang <[email protected]>Please include the link to the usermode stuff that uses this new IOCTL.
For all of the patches which add or modify IOCTLs, we need a link to the userspace which uses the interface. With that fixed, the series is:
Quoted 495 lines
Alex
Quoted 492 lines
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 82 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_device.c | 4 + .../drm/amd/amdkfd/kfd_device_queue_manager.c | 25 ++++++ .../drm/amd/amdkfd/kfd_device_queue_manager.h | 2 + .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c | 16 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c | 14 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c | 8 +- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c | 15 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c | 11 +++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 7 ++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 11 +++ include/uapi/linux/kfd_ioctl.h | 29 +++++++ 12 files changed, 216 insertions(+), 8 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +82
-0@@ -21,6 +21,7 @@* OTHER DEALINGS IN THE SOFTWARE. */ +#include <linux/capability.h> #include <linux/device.h> #include <linux/err.h> #include <linux/fs.h> @@ -3204,6 +3205,84 @@ static int kfd_ioctl_create_process(struct file *filep, struct kfd_process *p, vreturn 0 ; } +static inline uint32_t profile_lock_device (struct kfd_process *p, +uint32_t gpu_id,uint32_t op) +{ +struct kfd_process_device *pdd; +struct kfd_dev *kfd; +int status = -EINVAL ; + +if (!p) +return -EINVAL ; + +mutex_lock (&p->mutex); + pdd =kfd_process_device_data_by_id (p, gpu_id); +mutex_unlock (&p->mutex); + +if (!pdd || !pdd->dev || !pdd->dev->kfd) +return -EINVAL ; + + kfd = pdd->dev->kfd; + +mutex_lock (&kfd->profiler_lock); +if (op ==1 ) { +if (!kfd->profiler_process) { + kfd->profiler_process = p; + status =0 ; + }else if (kfd->profiler_process == p) { + status = -EALREADY ; + }else { + status = -EBUSY ; + } + }else if (op ==0 && kfd->profiler_process == p) { + kfd->profiler_process =NULL ; + status =0 ; + } +mutex_unlock (&kfd->profiler_lock); + +return status; +} + +static inline int kfd_profiler_pmc (struct kfd_process *p, +struct kfd_ioctl_pmc_settings *args) +{ +struct kfd_process_device *pdd; +struct device_queue_manager *dqm; +int status; + +/* Check if we have the correct permissions. */ +if (!perfmon_capable ()) +return -EPERM ; + +/* Lock/Unlock the device based on the parameter given in OP */ + status =profile_lock_device (p, args->gpu_id, args->lock); +if (status !=0 ) +return status; + +/* Enable/disable perfcount if requested */ +mutex_lock (&p->mutex); + pdd =kfd_process_device_data_by_id (p, args->gpu_id); + dqm = pdd->dev->dqm; +mutex_unlock (&p->mutex); + + dqm->ops.set_perfcount (dqm, args->perfcount_enable); +return status; +} + +static int kfd_ioctl_profiler (struct file *filep,struct kfd_process *p,void *data) +{ +struct kfd_ioctl_profiler_args *args = data; + +switch (args->op) { +case KFD_IOC_PROFILER_VERSION : + args->version =KFD_IOC_PROFILER_VERSION_NUM ; +return 0 ; +case KFD_IOC_PROFILER_PMC : +return kfd_profiler_pmc (p, &args->pmc); + } +return -EINVAL ; +} +#define AMDKFD_IOCTL_DEF (ioctl, _func, _flags) \ [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \ .cmd_drv = 0, .name = #ioctl}@@ -3325,6 +3404,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {AMDKFD_IOCTL_DEF (AMDKFD_IOC_CREATE_PROCESS , kfd_ioctl_create_process,0 ), + +AMDKFD_IOCTL_DEF (AMDKFD_IOC_PROFILER , + kfd_ioctl_profiler, 0), };#define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)drivers/gpu/drm/amd/amdkfd/kfd_device.c +4
-0@@ -936,6 +936,9 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,svm_range_set_max_pages (kfd->adev); + kfd->profiler_process =NULL ; +mutex_init (&kfd->profiler_lock); + kfd->init_complete = true;dev_info (kfd_device,"added device %x:%x\n" , kfd->adev->pdev->vendor, kfd->adev->pdev->device);@@ -971,6 +974,7 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd)ida_destroy (&kfd->doorbell_ida);kfd_gtt_sa_fini (kfd);amdgpu_amdkfd_free_kernel_mem (kfd->adev, &kfd->gtt_mem); +mutex_destroy (&kfd->profiler_lock); }kfree (kfd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +25
-0@@ -305,6 +305,29 @@ static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,return r; } +static void set_perfcount (struct device_queue_manager *dqm,int enable) +{ +struct device_process_node *cur; +struct qcm_process_device *qpd; +struct queue *q; +struct mqd_update_info minfo = {0 }; + +if (!dqm) +return ; + + minfo.update_flag = (enable ==1 ?UPDATE_FLAG_PERFCOUNT_ENABLE : +UPDATE_FLAG_PERFCOUNT_DISABLE ); +dqm_lock (dqm); +list_for_each_entry (cur, &dqm->queues, list) { + qpd = cur->qpd; +list_for_each_entry (q, &qpd->queues_list, list) { +pqm_update_mqd (qpd->pqm, q->properties.queue_id, + &minfo); + } + } +dqm_unlock (dqm); +} +static int remove_all_kfd_queues_mes (struct device_queue_manager *dqm) {struct device_process_node *cur;@@ -2967,6 +2990,7 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev) dqm->ops.reset_queues = reset_queues_cpsch; dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; dqm->ops.checkpoint_mqd = checkpoint_mqd; + dqm->ops.set_perfcount = set_perfcount;break ;case KFD_SCHED_POLICY_NO_HWS :/* initialize dqm for no cp scheduling */ @@ -2987,6 +3011,7 @@ struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev) dqm->ops.get_wave_state = get_wave_state; dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; dqm->ops.checkpoint_mqd = checkpoint_mqd; + dqm->ops.set_perfcount = set_perfcount;break ;default :dev_err (dev->adev->dev,"Invalid scheduling policy %d\n" , dqm->sched_policy);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h +2
-0@@ -200,6 +200,8 @@ struct device_queue_manager_ops {const struct queue *q, void *mqd, void *ctl_stack); +void (*set_perfcount)(struct device_queue_manager *dqm, +int enable); };struct device_queue_manager_asic_ops {drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +14
-2@@ -124,10 +124,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,*/ m->cp_hqd_hq_scheduler0 =1 <<14 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=@@ -142,6 +141,12 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; + +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -221,6 +226,13 @@ static void update_mqd(struct mqd_manager *mm, void *mqd,if (mm->dev->kfd->cwsr_enabled) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + } +update_cu_mask (mm, mqd, minfo);set_priority (m, q);drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +12
-2@@ -164,10 +164,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,if (amdgpu_amdkfd_have_atomics_support (mm->dev->adev)) m->cp_hqd_hq_status0 |=1 <<29 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=@@ -182,6 +181,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -259,6 +263,12 @@ static void update_mqd(struct mqd_manager *mm, void *mqd, }if (mm->dev->kfd->cwsr_enabled) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + }update_cu_mask (mm, mqd, minfo);set_priority (m, q);drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c +6
-2@@ -139,10 +139,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd,if (amdgpu_amdkfd_have_atomics_support (mm->dev->adev)) m->cp_hqd_hq_status0 |=1 <<29 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |=@@ -157,6 +156,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +13
-2@@ -218,10 +218,9 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; -if (q->tba_addr) { +if (q->tba_addr) m->compute_pgm_rsrc2 |= (1 <<COMPUTE_PGM_RSRC2__TRAP_PRESENT__SHIFT ); - }if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) { m->cp_hqd_persistent_state |=@@ -236,6 +235,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -318,6 +322,13 @@ static void update_mqd(struct mqd_manager *mm, void *mqd,if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + } +if (KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,4 ,3 ) &&KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,4 ,4 ) &&KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,5 ,0 ))drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +11
-0@@ -149,6 +149,11 @@ static void init_mqd(struct mqd_manager *mm, void **mqd, m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -231,6 +236,12 @@ static void __update_mqd(struct mqd_manager *mm, void *mqd, m->cp_hqd_ctx_save_control = atc_bit <<CP_HQD_CTX_SAVE_CONTROL__ATC__SHIFT | mtype <<CP_HQD_CTX_SAVE_CONTROL__MTYPE__SHIFT ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; +else if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_DISABLE ) + m->compute_perfcount_enable =0 ; + }update_cu_mask (mm, mqd, minfo);set_priority (m, q);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +7
-0@@ -384,6 +384,11 @@ struct kfd_dev {int kfd_dev_lock;atomic_t kfd_processes_count; + +/* Lock for profiler process */ +struct mutex profiler_lock; +/* Process currently holding the lock */ +struct kfd_process *profiler_process; };enum kfd_mempool {@@ -556,6 +561,8 @@ enum mqd_update_flag {UPDATE_FLAG_DBG_WA_ENABLE =1 ,UPDATE_FLAG_DBG_WA_DISABLE =2 ,UPDATE_FLAG_IS_GWS =4 ,/* quirk for gfx9 IP */ +UPDATE_FLAG_PERFCOUNT_ENABLE =5 , + UPDATE_FLAG_PERFCOUNT_DISABLE = 6, }; struct mqd_update_info {drivers/gpu/drm/amd/amdkfd/kfd_process.c +11
-0@@ -1110,6 +1110,16 @@ static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)kfd_process_device_free_bos (p->pdds[i]); } +static void kfd_process_profiler_release (struct kfd_process *p,struct kfd_process_device *pdd) +{ +mutex_lock (&pdd->dev->kfd->profiler_lock); +if (pdd->dev->kfd->profiler_process == p) { + pdd->qpd.dqm->ops.set_perfcount (pdd->qpd.dqm,0 ); + pdd->dev->kfd->profiler_process =NULL ; + } +mutex_unlock (&pdd->dev->kfd->profiler_lock); +} +static void kfd_process_destroy_pdds (struct kfd_process *p) {int i;@@ -1121,6 +1131,7 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)pr_debug ("Releasing pdd (topology id %d, for pid %d)\n" , pdd->dev->id, p->lead_thread->pid); +kfd_process_profiler_release (p, pdd);kfd_process_device_destroy_cwsr_dgpu (pdd);kfd_process_device_destroy_ib_mem (pdd);include/uapi/linux/kfd_ioctl.h +29
-1@@ -1558,6 +1558,29 @@ struct kfd_ioctl_dbg_trap_args { }; }; +#define KFD_IOC_PROFILER_VERSION_NUM 1 +enum kfd_profiler_ops { +KFD_IOC_PROFILER_PMC =0 , +KFD_IOC_PROFILER_VERSION =2 , +}; + +/** +* Enables/Disables GPU Specific profiler settings +*/ +struct kfd_ioctl_pmc_settings { +__u32 gpu_id;/* This is the user_gpu_id */ +__u32 lock;/* Lock GPU for Profiling */ +__u32 perfcount_enable;/* Force Perfcount Enable for queues on GPU */ +}; + +struct kfd_ioctl_profiler_args { +__u32 op;/* kfd_profiler_op */ +union { +struct kfd_ioctl_pmc_settings pmc; +__u32 version;/* KFD_IOC_PROFILER_VERSION_NUM */ + }; +}; +#define AMDKFD_IOCTL_BASE 'K'#define AMDKFD_IO (nr) _IO(AMDKFD_IOCTL_BASE, nr)#define AMDKFD_IOR (nr, type) _IOR(AMDKFD_IOCTL_BASE, nr, type)@@ -1684,4 +1707,10 @@ struct kfd_ioctl_dbg_trap_args {#define AMDKFD_COMMAND_START 0x01#define AMDKFD_COMMAND_END 0x28 +#define AMDKFD_IOC_PROFILER \ + AMDKFD_IOWR(0x86, struct kfd_ioctl_profiler_args) + +#define AMDKFD_COMMAND_START_2 0x80 +#define AMDKFD_COMMAND_END_2 0x87 +#endif --2.34.1
RE: [PATCH v2 01/13] Add kfd_ioctl_profiler to contain profiler kernel driver changes
[AMD Official Use Only - AMD Internal Distribution Only]
Hi Alex,
Quoted 63 lines
-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Monday, February 9, 2026 11:56 PM
To: Yuan, Perry <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Zhang, Yifan <[email protected]>
Subject: Re: [PATCH v2 01/13] Addkfd_ioctl_profilerto contain profiler kernel driver changesQuoted 35 lines · Alex Deucher
Quoted 33 lines · Perry Yuan
From: Benjamin Welton <[email protected]>
kfd_ioctl_profilertakes a similar approach to that ofkfd_ioctl_dbg_trap(which contains debugger related IOCTL services) wherekfd_ioctl_profilerwill contain all profiler related IOCTL services. The IOCTL is designed to be expanded as needed to support additional profiler functionality.The current functionality of the IOCTL is to allow for profilers which need PMC counters from GPU devices to both signal to other profilers that may be on the system that the device has active PMC profiling taking place on it (multiple PMC profilers on the same device can result in corrupted counter data) and to setup the device to allow for the collection of SQ PMC data on all queues on the device.
For PMC data for the SQ block (such as
SQ_WAVES) to be available to a profiler,mmPERFCOUNT_ENABLEmust be set on the queues. When profiling a single process, the profiler can inject PM4 packets into each queue to turn onPERFCOUNT_ENABLE. When profiling system wide, the profiler does not have this option and must have a way to turn on profiling for queues in which it cannot inject packets into directly.Accomplishing this requires a few steps:
1. Checking if the user has the necessary permissions to profile system wide on the device. This check uses the same check that linux perf uses to determine if a user has the necessary permissions to profile at this scope (primarily if the process has
CAP_SYS_PERFMONor is root).2. Locking the device for profiling. This is done by setting a lock bit on the device struct and storing the process that locked the device.
Quoted 8 lines
Quoted 8 lines
3. Iterating all queues on the device and issuing an MQD Update to enable perfcounting on the queues.
4. Actions to cleanup if the process exits or releases the lock.
The IOCTL also contains a link to the existing PC Sampling IOCTL as well. This is per a suggestion that we should potentially remove the PC Sampling IOCTL to have it be a part of the profiler IOCTL. This is a future
change.
Quoted 9 lines
Quoted 8 lines
In addition, we do expect to expand the profiler IOCTL to include additional profiler functionality in the future (which necessitates the use of a version number).
Signed-off-by Benjamin Welton <[email protected]>Signed-off-by Perry Yuan <[email protected]>Acked-by Kent Russell <[email protected]>Reviewed-by Yifan Zhang <[email protected]>
Quoted 6 lines
Quoted 1 line
Please include the link to the usermode stuff that uses this new IOCTL.
For all of the patches which add or modify IOCTLs, we need a link to the userspace which uses the interface. With that fixed, the series
is:Acked-by Alex Deucher <[email protected]>
Thank you, will check and add it.
Quoted 531 lines
Quoted 28 lines
Alex
Quoted 24 lines
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 82 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_device.c | 4 + .../drm/amd/amdkfd/kfd_device_queue_manager.c | 25 ++++++ .../drm/amd/amdkfd/kfd_device_queue_manager.h | 2 + .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c | 16 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c | 14 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c | 8 +- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c | 15 +++- .../gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c | 11 +++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 7 ++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 11 +++ include/uapi/linux/kfd_ioctl.h | 29 +++++++ 12 files changed, 216 insertions(+), 8 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0
-0@@ -21,6 +21,7 @@* OTHER DEALINGS IN THE SOFTWARE. */ Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_create_process(struct file *filep,
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1
-0+#include <linux/capability.h> #include <linux/device.h> #include <linux/err.h> #include <linux/fs.h> @@ -3204,6 +3205,84 @@ static int kfd_ioctl_create_process(struct file *filep,struct
kfd_process*p, vQuoted 117 lines
Quoted 117 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +82
-0return 0 ; } +static inline uint32_t profile_lock_device (struct kfd_process *p, +uint32_t gpu_id,uint32_t +op) { +struct kfd_process_device *pdd; +struct kfd_dev *kfd; +int status = -EINVAL ; + +if (!p) +return -EINVAL ; + +mutex_lock (&p->mutex); + pdd =kfd_process_device_data_by_id (p, gpu_id); +mutex_unlock (&p->mutex); + +if (!pdd || !pdd->dev || !pdd->dev->kfd) +return -EINVAL ; + + kfd = pdd->dev->kfd; + +mutex_lock (&kfd->profiler_lock); +if (op ==1 ) { +if (!kfd->profiler_process) { + kfd->profiler_process = p; + status =0 ; + }else if (kfd->profiler_process == p) { + status = -EALREADY ; + }else { + status = -EBUSY ; + } + }else if (op ==0 && kfd->profiler_process == p) { + kfd->profiler_process =NULL ; + status =0 ; + } +mutex_unlock (&kfd->profiler_lock); + +return status; +} + +static inline int kfd_profiler_pmc (struct kfd_process *p, +struct kfd_ioctl_pmc_settings +*args) { +struct kfd_process_device *pdd; +struct device_queue_manager *dqm; +int status; + +/* Check if we have the correct permissions. */ +if (!perfmon_capable ()) +return -EPERM ; + +/* Lock/Unlock the device based on the parameter given in OP */ + status =profile_lock_device (p, args->gpu_id, args->lock); +if (status !=0 ) +return status; + +/* Enable/disable perfcount if requested */ +mutex_lock (&p->mutex); + pdd =kfd_process_device_data_by_id (p, args->gpu_id); + dqm = pdd->dev->dqm; +mutex_unlock (&p->mutex); + + dqm->ops.set_perfcount (dqm, args->perfcount_enable); +return status; +} + +static int kfd_ioctl_profiler (struct file *filep,struct +kfd_process *p,void *data) { +struct kfd_ioctl_profiler_args *args = data; + +switch (args->op) { +case KFD_IOC_PROFILER_VERSION : + args->version =KFD_IOC_PROFILER_VERSION_NUM ; +return 0 ; +case KFD_IOC_PROFILER_PMC : +return kfd_profiler_pmc (p, &args->pmc); + } +return -EINVAL ; +} +#define AMDKFD_IOCTL_DEF (ioctl, _func, _flags) \ [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func, .flags = _flags, \ .cmd_drv = 0, .name = #ioctl} @@ -3325,6 +3404,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {AMDKFD_IOCTL_DEF (AMDKFD_IOC_CREATE_PROCESS , kfd_ioctl_create_process,0 ), + +AMDKFD_IOCTL_DEF (AMDKFD_IOC_PROFILER , + kfd_ioctl_profiler,0 ), };#define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)drivers/gpu/drm/amd/amdkfd/kfd_device.c +3
-0@@ -936,6 +936,9 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,svm_range_set_max_pages (kfd->adev); + kfd->profiler_process =NULL ; +mutex_init (&kfd->profiler_lock); + kfd->init_complete = true;dev_info (kfd_device,"added device %x:%x\n" , kfd->adev->pdev->vendor, kfd->adev->pdev->device); @@ -971,6 +974,7 @@ void
kgd2kfd_device_exit(struct kfd_dev *kfd)ida_destroy(&kfd->doorbell_ida); kfd_gtt_sa_fini(kfd); amdgpu_amdkfd_free_kernel_mem(kfd->adev, &kfd->gtt_mem); + mutex_destroy(&kfd->profiler_lock); }Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static int remove_queue_mes(struct
Quoted 7 lines
kfree(kfd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -305,6 +305,29 @@ static int remove_queue_mes(struct
device_queue_manager*dqm, struct queue *q,Quoted 14 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-0return r; } +static void set_perfcount(struct device_queue_manager *dqm, intdrivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +10
-0+enable) { +struct device_process_node *cur; +struct qcm_process_device *qpd; +struct queue *q; +struct mqd_update_info minfo = {0 }; + +if (!dqm) +return ; + + minfo.update_flag = (enable == 1 ?
UPDATE_FLAG_PERFCOUNT_ENABLE:Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +8
-0+ UPDATE_FLAG_PERFCOUNT_DISABLE); +dqm_lock (dqm); +list_for_each_entry (cur, &dqm->queues, list) { + qpd = cur->qpd; +list_for_each_entry (q, &qpd->queues_list, list) { +pqm_update_mqd (qpd->pqm, q->properties.queue_id, + &minfo); + }Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4
-0+ } +dqm_unlock (dqm); +} +static int remove_all_kfd_queues_mes (struct device_queue_manager *dqm) { struct device_process_node *cur; @@ -2967,6 +2990,7 @@struct
device_queue_manager*device_queue_manager_init(structkfd_node*dev)
Quoted 8 lines
Quoted 8 lines
dqm->ops.reset_queues = reset_queues_cpsch; dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; dqm->ops.checkpoint_mqd = checkpoint_mqd; + dqm->ops.set_perfcount = set_perfcount; break; case KFD_SCHED_POLICY_NO_HWS: /* initialize dqm for no cp scheduling */ @@ -2987,6+3011,7 @@ struct
device_queue_manager*device_queue_manager_init(struct
kfd_node*dev)Quoted 21 lines
Quoted 21 lines
dqm->ops.get_wave_state = get_wave_state; dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info; dqm->ops.checkpoint_mqd = checkpoint_mqd; + dqm->ops.set_perfcount = set_perfcount; break; default: dev_err(dev->adev->dev, "Invalid scheduling policy%d\n",
dqm->sched_policy); diff --gita/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.hb/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.hindexef07e44916f8..74a3bcec3e4e100644drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h +2
-0@@ -200,6 +200,8 @@ struct device_queue_manager_ops {const struct queue *q, void *mqd, void *ctl_stack); +void (*set_perfcount)(struct device_queue_manager *dqm, +int enable); };Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c static void init_mqd(struct mqd_manager *mm, void
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h +0
-0struct device_queue_manager_asic_ops { diff --git
a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.cb/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.cindex97055f808d4a..993d60a24d50100644drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +0
-0@@ -124,10 +124,9 @@ static void init_mqd(struct mqd_manager *mm, void**mqd,
Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +1
-2*/ m->cp_hqd_hq_scheduler0 =1 <<14 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |= @@ -142,6 +141,12 @@static void init_mqd(struct
mqd_manager*mm, void **mqd,m->cp_hqd_wg_state_offset = q->ctl_stack_size; }Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c static void update_mqd(struct mqd_manager *mm,
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +6
-0+mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; + +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -221,6 +226,13 @@ static void update_mqd(struct mqd_manager *mm,void *mqd,
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c static void update_mqd(struct mqd_manager *mm, void *mqd,
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +4
-0if (mm->dev->kfd->cwsr_enabled) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; + else if (minfo->update_flag ==
UPDATE_FLAG_PERFCOUNT_DISABLE)Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +3
-0+ m->compute_perfcount_enable =0 ; + } +update_cu_mask (mm, mqd, minfo);set_priority (m, q);Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c static void init_mqd(struct mqd_manager *mm, void
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +0
-0@@ -164,10 +164,9 @@ static void init_mqd(struct mqd_manager *mm, void**mqd,
Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +1
-2if (amdgpu_amdkfd_have_atomics_support (mm->dev->adev)) m->cp_hqd_hq_status0 |=1 <<29 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |= @@ -182,6 +181,11 @@static void init_mqd(struct
mqd_manager*mm, void **mqd,m->cp_hqd_wg_state_offset = q->ctl_stack_size; }Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c static void update_mqd(struct mqd_manager *mm,
Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +5
-0+mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -259,6 +263,12 @@ static void update_mqd(struct mqd_manager *mm,void *mqd,
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c static void update_mqd(struct mqd_manager *mm, void *mqd,
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +4
-0}if (mm->dev->kfd->cwsr_enabled) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; + else if (minfo->update_flag ==
UPDATE_FLAG_PERFCOUNT_DISABLE)Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c static void init_mqd(struct mqd_manager *mm, void
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +2
-0+ m->compute_perfcount_enable =0 ; + }update_cu_mask (mm, mqd, minfo);set_priority (m, q);drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c +0
-0@@ -139,10 +139,9 @@ static void init_mqd(struct mqd_manager *mm, void**mqd,
Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c +1
-2if (amdgpu_amdkfd_have_atomics_support (mm->dev->adev)) m->cp_hqd_hq_status0 |=1 <<29 ; -if (q->format ==KFD_QUEUE_FORMAT_AQL ) { +if (q->format ==KFD_QUEUE_FORMAT_AQL ) m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; - }if (mm->dev->kfd->cwsr_enabled) { m->cp_hqd_persistent_state |= @@ -157,6 +156,11 @@static void init_mqd(struct
mqd_manager*mm, void **mqd,m->cp_hqd_wg_state_offset = q->ctl_stack_size; }Quoted 14 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static void init_mqd(struct mqd_manager *mm, void
Quoted 14 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c +5
-0+mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0
-0@@ -218,10 +218,9 @@ static void init_mqd(struct mqd_manager *mm, void**mqd,
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static void init_mqd(struct mqd_manager *mm, void **mqd,
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +1
-2m->cp_hqd_aql_control =1 <<CP_HQD_AQL_CONTROL__CONTROL0__SHIFT ; -if (q->tba_addr) { +if (q->tba_addr) m->compute_pgm_rsrc2 |= (1 <<COMPUTE_PGM_RSRC2__TRAP_PRESENT__SHIFT ); - }if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address){
Quoted 5 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0
-0m->cp_hqd_persistent_state |= @@ -236,6 +235,11 @@static void init_mqd(struct
mqd_manager*mm, void **mqd,m->cp_hqd_wg_state_offset = q->ctl_stack_size; }Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static void update_mqd(struct mqd_manager *mm,
Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +5
-0+mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -318,6 +322,13 @@ static void update_mqd(struct mqd_manager *mm,void *mqd,
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static void update_mqd(struct mqd_manager *mm, void *mqd,
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +4
-0if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) m->cp_hqd_ctx_save_control =0 ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; + else if (minfo->update_flag ==
UPDATE_FLAG_PERFCOUNT_DISABLE)Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +3
-0+ m->compute_perfcount_enable =0 ; + } + if (KFD_GC_VERSION(mm->dev) != IP_VERSION(9, 4, 3) &&Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c static void init_mqd(struct mqd_manager *mm, void
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0
-1KFD_GC_VERSION (mm->dev) !=IP_VERSION (9 ,4 ,4 ) && KFD_GC_VERSION(mm->dev) != IP_VERSION(9, 5, 0)) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c
b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c
index09483f0862d4..e8967f5e3892100644drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +0
-0@@ -149,6 +149,11 @@ static void init_mqd(struct mqd_manager *mm, void**mqd,
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c static void __update_mqd(struct mqd_manager *mm,
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +5
-0m->cp_hqd_wg_state_offset = q->ctl_stack_size; } +mutex_lock (&mm->dev->kfd->profiler_lock); +if (mm->dev->kfd->profiler_process !=NULL ) + m->compute_perfcount_enable =1 ; +mutex_unlock (&mm->dev->kfd->profiler_lock); + *mqd = m;if (gart_addr) *gart_addr = addr;@@ -231,6 +236,12 @@ static void __update_mqd(struct mqd_manager *mm,void *mqd,
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c static void __update_mqd(struct mqd_manager *mm, void *mqd,
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +4
-0m->cp_hqd_ctx_save_control = atc_bit <<CP_HQD_CTX_SAVE_CONTROL__ATC__SHIFT | mtype <<CP_HQD_CTX_SAVE_CONTROL__MTYPE__SHIFT ; +if (minfo) { +if (minfo->update_flag ==UPDATE_FLAG_PERFCOUNT_ENABLE ) + m->compute_perfcount_enable =1 ; + else if (minfo->update_flag ==
UPDATE_FLAG_PERFCOUNT_DISABLE)Quoted 30 lines
Quoted 30 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +2
-0+ m->compute_perfcount_enable =0 ; + }update_cu_mask (mm, mqd, minfo);set_priority(m, q);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +7
-0@@ -384,6 +384,11 @@ struct kfd_dev {int kfd_dev_lock;atomic_t kfd_processes_count; + +/* Lock for profiler process */ +struct mutex profiler_lock; +/* Process currently holding the lock */ +struct kfd_process *profiler_process; };enum kfd_mempool {@@ -556,6 +561,8 @@ enum mqd_update_flag {UPDATE_FLAG_DBG_WA_ENABLE =1 ,UPDATE_FLAG_DBG_WA_DISABLE =2 ,UPDATE_FLAG_IS_GWS =4 ,/* quirk for gfx9 IP */ +UPDATE_FLAG_PERFCOUNT_ENABLE =5 , + UPDATE_FLAG_PERFCOUNT_DISABLE = 6, };Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c static void
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +0
-0struct mqd_update_info {drivers/gpu/drm/amd/amdkfd/kfd_process.c +0
-0@@ -1110,6 +1110,16 @@ static void
kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)Quoted 33 lines
Quoted 33 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +11
-0kfd_process_device_free_bos (p->pdds[i]); } +static void kfd_process_profiler_release (struct kfd_process *p, +struct kfd_process_device *pdd) { +mutex_lock (&pdd->dev->kfd->profiler_lock); +if (pdd->dev->kfd->profiler_process == p) { + pdd->qpd.dqm->ops.set_perfcount (pdd->qpd.dqm,0 ); + pdd->dev->kfd->profiler_process =NULL ; + } +mutex_unlock (&pdd->dev->kfd->profiler_lock); +} +static void kfd_process_destroy_pdds (struct kfd_process *p) {int i;@@ -1121,6 +1131,7 @@ static void kfd_process_destroy_pdds(struct kfd_process *p) pr_debug("Releasing pdd (topology id %d, for pid %d)\n", pdd->dev->id, p->lead_thread->pid); +kfd_process_profiler_release (p, pdd);kfd_process_device_destroy_cwsr_dgpu (pdd);kfd_process_device_destroy_ib_mem (pdd);(quoted diff) +0
-0
b/include/uapi/linux/kfd_ioctl.hindexe72359370857..a8b2a18d07cf100644include/uapi/linux/kfd_ioctl.h +0
-0@@ -1558,6 +1558,29 @@ struct kfd_ioctl_dbg_trap_args { }; };Quoted 10 lines
include/uapi/linux/kfd_ioctl.h struct kfd_ioctl_dbg_trap_args {
Quoted 10 lines
include/uapi/linux/kfd_ioctl.h +10
-0+#define KFD_IOC_PROFILER_VERSION_NUM 1 enum kfd_profiler_ops { +KFD_IOC_PROFILER_PMC =0 , + KFD_IOC_PROFILER_VERSION = 2, }; + +/** +* Enables/Disables GPU Specific profiler settings */ struct +kfd_ioctl_pmc_settings { +__u32 gpu_id;/* This is the user_gpu_id */ +__u32 lock;/* Lock GPU for Profiling */ +__u32 perfcount_enable;/* Force Perfcount Enable for queues on GPU */
Quoted 7 lines
include/uapi/linux/kfd_ioctl.h struct kfd_ioctl_dbg_trap_args {
Quoted 7 lines
include/uapi/linux/kfd_ioctl.h +7
-0+}; + +struct kfd_ioctl_profiler_args { +__u32 op;/* kfd_profiler_op */ +union { +struct kfd_ioctl_pmc_settings pmc; +__u32 version;/*
KFD_IOC_PROFILER_VERSION_NUM*/Quoted 6 lines
include/uapi/linux/kfd_ioctl.h struct kfd_ioctl_dbg_trap_args {
Quoted 6 lines
include/uapi/linux/kfd_ioctl.h +3
-0+ }; +}; +#define AMDKFD_IOCTL_BASE 'K'#define AMDKFD_IO (nr) _IO(AMDKFD_IOCTL_BASE, nr)#define AMDKFD_IOR (nr, type) _IOR(AMDKFD_IOCTL_BASE, nr,type)
Quoted 14 lines
Quoted 14 lines
include/uapi/linux/kfd_ioctl.h +6
-1@@ -1684,4 +1707,10 @@ struct kfd_ioctl_dbg_trap_args {#define AMDKFD_COMMAND_START 0x01#define AMDKFD_COMMAND_END 0x28 +#define AMDKFD_IOC_PROFILER \ + AMDKFD_IOWR(0x86, struct kfd_ioctl_profiler_args) + +#define AMDKFD_COMMAND_START_2 0x80 +#define AMDKFD_COMMAND_END_2 0x87 +#endif --2.34.1
PATCH v2 2/13 drm/amdgpu: add new performance monitor PSP interfaces
In reply to Perry YuanIntroduce new psp interfaces and structures for performance monitoring hardware control.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h | 21 +++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/psp_gfx_if.h | 17 +++++++++++++++++ 2 files changed, 38 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +21 -0
@@ -359,6 +359,27 @@ struct spirom_bo { };#endif +enum psp_ptl_cmd { +PSP_PTL_PERF_MON_QUERY =0xA0000000 , +PSP_PTL_PERF_MON_SET =0xA0000001 , +}; + +enum psp_ptl_format_type { +GFX_FTYPE_I8 =0x00000000 , +GFX_FTYPE_F16 =0x00000001 , +GFX_FTYPE_BF16 =0x00000002 , +GFX_FTYPE_F32 =0x00000003 , +GFX_FTYPE_F64 =0x00000004 , +GFX_FTYPE_INVALID =0xFFFFFFFF , +}; + +struct psp_ptl_perf_req { +enum psp_ptl_cmd req; +uint32_t ptl_state; +uint32_t pref_format1; +uint32_t pref_format2; +}; +struct psp_context {struct amdgpu_device *adev;struct psp_ring km_ring;
drivers/gpu/drm/amd/amdgpu/psp_gfx_if.h +17 -0
@@ -107,6 +107,7 @@ enum psp_gfx_cmd_idGFX_CMD_ID_CONFIG_SQ_PERFMON =0x00000046 ,/* Config CGTT_SQ_CLK_CTRL */ /* Dynamic memory partitioninig (NPS mode change)*/ GFX_CMD_ID_FB_NPS_MODE =0x00000048 ,/* Configure memory partitioning mode */ +GFX_CMD_ID_PERF_HW =0x0000004C ,/* performance monitor */ GFX_CMD_ID_FB_FW_RESERV_ADDR =0x00000050 ,/* Query FW reservation addr */ GFX_CMD_ID_FB_FW_RESERV_EXT_ADDR = 0x00000051, /* Query FW reservation extended addr */ };
@@ -373,6 +374,13 @@ struct psp_gfx_cmd_fb_memory_part {uint32_t resvd; }; +struct psp_gfx_cmd_req_perf_hw { +uint32_t req; +uint32_t ptl_state; +uint32_t pref_format1; +uint32_t pref_format2; +}; +/* All GFX ring buffer commands. */ union psp_gfx_commands {
@@ -389,6 +397,7 @@ union psp_gfx_commandsstruct psp_gfx_cmd_sriov_spatial_part cmd_spatial_part;struct psp_gfx_cmd_config_sq_perfmon config_sq_perfmon;struct psp_gfx_cmd_fb_memory_part cmd_memory_part; +struct psp_gfx_cmd_req_perf_hw cmd_req_perf_hw; };struct psp_gfx_uresp_reserved
@@ -415,12 +424,20 @@ struct psp_gfx_uresp_fw_reserve_info {uint32_t reserve_size; }; +struct psp_gfx_uresp_perf_hw { +uint32_t resp; +uint32_t ptl_state; +uint32_t pref_format1; +uint32_t pref_format2; +}; +/* Union of command-specific responses for GPCOM ring. */ union psp_gfx_uresp {struct psp_gfx_uresp_reserved reserved;struct psp_gfx_uresp_bootcfg boot_cfg;struct psp_gfx_uresp_fwar_db_info fwar_db_info;struct psp_gfx_uresp_fw_reserve_info fw_reserve_info; +struct psp_gfx_uresp_perf_hw perf_hw_info; };/* Structure of GFX Response buffer.
Signature · 1 line
2.34.1
PATCH v2 3/13 drm/amdgpu: add psp interfaces for peak tops limiter driver
In reply to Perry YuanIntroduce a Peak Tops Limiter (PTL) driver that dynamically caps engine frequency to ensure delivered TOPS never exceeds a defined TOPS_limit. This initial implementation provides core data structures and kernel-space interfaces (set/get, enable/disable) to manage PTL state.
PTL performs a firmware handshake to initialize its state and update predefined format types. It supports updating these format types at runtime while user-space tools automatically switch PTL state, and also allows explicitly switching PTL state via newly added commands.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 + drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 112 ++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h | 3 +- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 1 + drivers/gpu/drm/amd/include/amdgpu_ptl.h | 48 ++++++++ .../gpu/drm/amd/include/kgd_kfd_interface.h | 1 + 7 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/amd/include/amdgpu_ptl.h
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -0
@@ -38,6 +38,8 @@#include "amdgpu_vm.h" #include "amdgpu_xcp.h" #include "kfd_topology.h" +#include "amdgpu_ptl.h" +extern uint64_t amdgpu_amdkfd_total_mem_size;enum TLB_FLUSH_TYPE {
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -4440,6 +4440,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,mutex_init (&adev->virt.vf_errors.lock);hash_init (adev->mn_hash);mutex_init (&adev->psp.mutex); +mutex_init (&adev->psp.ptl.mutex);mutex_init (&adev->notifier_lock);mutex_init (&adev->pm.stable_pstate_ctx_lock);mutex_init (&adev->benchmark_mutex);
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +112 -0
124 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +2 -1
@@ -31,6 +31,7 @@#include "ta_ras_if.h" #include "ta_rap_if.h" #include "ta_secureDisplay_if.h" +#include "amdgpu_ptl.h" #define PSP_FENCE_BUFFER_SIZE 0x1000#define PSP_CMD_BUFFER_SIZE 0x1000
@@ -470,6 +471,7 @@ struct psp_context {#if defined(CONFIG_DEBUG_FS )struct spirom_bo *spirom_dump_trip;#endif +struct amdgpu_ptl ptl; };struct amdgpu_psp_funcs {
@@ -653,5 +655,4 @@ void amdgpu_psp_debugfs_init(struct amdgpu_device *adev);int amdgpu_psp_get_fw_type (struct amdgpu_firmware_info *ucode,enum psp_gfx_fw_type *type); -#endif
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -0
@@ -44,6 +44,7 @@#include "kfd_smi_events.h" #include "amdgpu_dma_buf.h" #include "kfd_debug.h" +#include "amdgpu_ptl.h" static long kfd_ioctl (struct file *,unsigned int ,unsigned long );static int kfd_open (struct inode *,struct file *);
drivers/gpu/drm/amd/include/amdgpu_ptl.h new file +48 -0
48 lines not on this page · raw message
drivers/gpu/drm/amd/include/kgd_kfd_interface.h +1 -0
@@ -33,6 +33,7 @@#include <linux/dma-fence.h> #include "amdgpu_irq.h" #include "amdgpu_gfx.h" +#include "amdgpu_ptl.h" struct pci_dev ;struct amdgpu_device ;
Signature · 1 line
2.34.1
PATCH v2 4/13 drm/amdgpu: add PTL enable/query gfx control support for GC 9.4.4
In reply to Perry YuanIntroduce hardware detection, runtime state tracking and a kgd->ptl_ctrl() callback to enable/disable/query PTL via the PSP performance-monitor interface (commands 0xA0000000/1). The driver now exposes PTL capability to KFD and keeps the software state in sync with the hardware.
--- drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +34 -0
@@ -2370,11 +2370,43 @@ static int gfx_v9_4_3_hw_init(struct amdgpu_ip_block *ip_block)return r; } +static int gfx_v9_4_3_perf_monitor_ptl_init (struct amdgpu_device *adev,bool enable) +{ +struct amdgpu_ptl *ptl = &adev->psp.ptl; +uint32_t ptl_state = enable ?1 :0 ; +uint32_t fmt1, fmt2; +int r; + +if (!adev->psp.funcs) +return -EOPNOTSUPP ; + +if (!ptl->hw_supported) { + fmt1 =GFX_FTYPE_I8 ; + fmt2 =GFX_FTYPE_BF16 ; + }else { + fmt1 = ptl->fmt1; + fmt2 = ptl->fmt2; + } + +/* initialize PTL with default formats: GFX_FTYPE_I8 & GFX_FTYPE_BF16 */ + r =amdgpu_ptl_perf_monitor_ctrl (adev,PSP_PTL_PERF_MON_SET , &ptl_state, + &fmt1, &fmt2); +if (r) +return r; + + ptl->hw_supported = true; + +return 0 ; +} +static int gfx_v9_4_3_hw_fini (struct amdgpu_ip_block *ip_block) {struct amdgpu_device *adev = ip_block->adev;int i, num_xcc; +if (adev->psp.ptl.hw_supported) +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.priv_inst_irq,0 );amdgpu_irq_put (adev, &adev->gfx.bad_op_irq,0 );
@@ -2549,6 +2581,8 @@ static int gfx_v9_4_3_late_init(struct amdgpu_ip_block *ip_block) adev->gfx.ras->enable_watchdog_timer) adev->gfx.ras->enable_watchdog_timer (adev); +gfx_v9_4_3_perf_monitor_ptl_init (adev, true); +return 0 ; }
Signature · 1 line
2.34.1
PATCH v2 5/13 drm/amdkfd: add kgd control interface for ptl
In reply to Perry YuanAdd kgd->ptl_ctrl() callback so KFD can query/enable/disable PTL state through the PSP performance monitor interface.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gc_9_4_3.c | 13 ++++++++++++- drivers/gpu/drm/amd/include/kgd_kfd_interface.h | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gc_9_4_3.c +12 -1
@@ -520,6 +520,16 @@ static uint32_t kgd_gfx_v9_4_3_hqd_sdma_get_doorbell(struct amdgpu_device *adev,return is_active ? doorbell_off >>2 :0 ; } +static uint32_t kgd_v9_4_3_ptl_ctrl (struct amdgpu_device *adev, +uint32_t cmd, +uint32_t *ptl_state, +enum amdgpu_ptl_fmt *fmt1, +enum amdgpu_ptl_fmt *fmt2) +{ +return amdgpu_ptl_perf_monitor_ctrl (adev, cmd, + ptl_state, fmt1, fmt2); +} +const struct kfd2kgd_calls gc_9_4_3_kfd2kgd = { .program_sh_mem_settings = kgd_gfx_v9_program_sh_mem_settings, .set_pasid_vmid_mapping = kgd_gfx_v9_4_3_set_pasid_vmid_mapping,
@@ -555,5 +565,6 @@ const struct kfd2kgd_calls gc_9_4_3_kfd2kgd = { .clear_address_watch = kgd_gfx_v9_4_3_clear_address_watch, .hqd_get_pq_addr = kgd_gfx_v9_hqd_get_pq_addr, .hqd_reset = kgd_gfx_v9_hqd_reset, - .hqd_sdma_get_doorbell = kgd_gfx_v9_4_3_hqd_sdma_get_doorbell + .hqd_sdma_get_doorbell = kgd_gfx_v9_4_3_hqd_sdma_get_doorbell, + .ptl_ctrl = kgd_v9_4_3_ptl_ctrl };
drivers/gpu/drm/amd/include/kgd_kfd_interface.h +5 -0
@@ -334,6 +334,11 @@ struct kfd2kgd_calls { uint32_t inst, unsigned int utimeout);uint32_t (*hqd_sdma_get_doorbell)(struct amdgpu_device *adev,int engine,int queue); +uint32_t (*ptl_ctrl)(struct amdgpu_device *adev, +uint32_t cmd, +uint32_t *ptl_state, +enum amdgpu_ptl_fmt *fmt1, + enum amdgpu_ptl_fmt *fmt2); };#endif /* KGD_KFD_INTERFACE_H_INCLUDED */
Signature · 1 line
2.34.1
PATCH v2 6/13 Documentation/amdgpu: Add documentation for Peak Tops Limiter (PTL) sysfs interface
In reply to Perry YuanThe PTL (Peak Tops Limiter) feature exposes per-GPU sysfs files under /sys/class/drm/cardX/device/ptl/ to allow users to enable or disable PTL, configure preferred data formats, and query supported formats. The usage of these sysfs files is not always obvious, so add documentation to describe their purpose and provide concrete usage examples.
V3 changes:
* format show will display preferred formats instead of N/A (Alex)
--- Documentation/gpu/amdgpu/index.rst | 1 + Documentation/gpu/amdgpu/ptl.rst | 94 ++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 Documentation/gpu/amdgpu/ptl.rst
Documentation/gpu/amdgpu/index.rst +1 -0
@@ -23,3 +23,4 @@ Next (GCN), Radeon DNA (RDNA), and Compute DNA (CDNA) architectures. debugfs process-isolation amdgpu-glossary + ptl
Documentation/gpu/amdgpu/ptl.rst new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 7/13 drm/amdgpu: add sysfs for Peak Tops Limiter (PTL)
In reply to Perry YuanAdd per-GPU sysfs files under /sys/class/drm/cardX/device/ptl to control the Peak Tops Limiter (PTL) feature. Exposes ptl_enable (enable/disable PTL), ptl_format (set/query preferred formats), and ptl_supported_formats (list supported formats)
Example usage
-------------
Query PTL status:
cat /sys/class/drm/card1/device/ptl/ptl_enable
Enable PTL:
sudo bash -c "echo 1 > /sys/class/drm/card1/device/ptl/ptl_enable"
Disable PTL:
sudo bash -c "echo 0 > /sys/class/drm/card1/device/ptl/ptl_enable"
Set PTL preferred formats:
`sudo bash -c "echo I8,F32 > /sys/class/drm/card1/device/ptl/ptl_format"`
Query supported formats:
cat /sys/class/drm/card1/device/ptl/ptl_supported_formats
v3 changes:
* move N/A to previous format in format show(Alex)
* fix format check for format store(Alex)
* drop the ptl declarations into amdgpu_ptl.h(Alex)
v2 changes:
* add usage commands in commit info (Alex)
* move amdgpu_ptl_fmt into kgd_kfd_interface.h (Alex)
--- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 2 + drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 191 +++++++++++++++++++++ drivers/gpu/drm/amd/include/amdgpu_ptl.h | 4 + 3 files changed, 197 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
@@ -4337,6 +4337,7 @@ static int amdgpu_device_sys_interface_init(struct amdgpu_device *adev)amdgpu_reg_state_sysfs_init (adev);amdgpu_xcp_sysfs_init (adev);amdgpu_uma_sysfs_init (adev); +amdgpu_ptl_sysfs_init (adev);return r; }
@@ -4353,6 +4354,7 @@ static void amdgpu_device_sys_interface_fini(struct amdgpu_device *adev)amdgpu_reg_state_sysfs_fini (adev);amdgpu_xcp_sysfs_fini (adev);amdgpu_uma_sysfs_fini (adev); +amdgpu_ptl_sysfs_fini (adev); }/**
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +191 -0
209 lines not on this page · raw message
drivers/gpu/drm/amd/include/amdgpu_ptl.h +4 -0
@@ -45,4 +45,8 @@ struct amdgpu_ptl {int amdgpu_ptl_perf_monitor_ctrl (struct amdgpu_device *adev,u32 req_code,u32 *ptl_state,u32 *fmt1,u32 *fmt2); +int amdgpu_ptl_sysfs_init (struct amdgpu_device *adev); +void amdgpu_ptl_sysfs_fini (struct amdgpu_device *adev); + +extern const struct attribute_group amdgpu_ptl_attr_group;#endif /* __AMDGPU_PTL_H__ */
Signature · 1 line
2.34.1
PATCH v2 8/13 drm/amdkfd: Add PTL control IOCTL Option and unify refcount logic
In reply to Perry YuanIntroduce a new IOCTL option to allow userspace explicit control over the Peak Tops Limiter (PTL) state for profiling
--- drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 2 + drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 102 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 9 ++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 4 + drivers/gpu/drm/amd/include/amdgpu_ptl.h | 2 + include/uapi/linux/kfd_ioctl.h | 7 ++ 6 files changed, 126 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +2 -0
@@ -2396,6 +2396,8 @@ static int gfx_v9_4_3_perf_monitor_ptl_init(struct amdgpu_device *adev, bool ena ptl->hw_supported = true; +atomic_set (&ptl->disable_ref,0 ); +return 0 ; }
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +102 -0
126 lines not on this page · raw message
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +9 -0
@@ -872,6 +872,8 @@ struct kfd_process_device {bool has_reset_queue;u32 pasid; +/* Indicates this process has requested PTL stay disabled */ +bool ptl_disable_req; };#define qpd_to_pdd (x) container_of(x, struct kfd_process_device, qpd)
@@ -1607,6 +1609,13 @@ static inline bool kfd_is_first_node(struct kfd_node *node)return (node == node->kfd->nodes[0 ]); } +/* PTL support */ +int kfd_ptl_control (struct kfd_process_device *pdd,bool enable); +int kfd_ptl_disable_request (struct kfd_process_device *pdd, +struct kfd_process *p); +int kfd_ptl_disable_release (struct kfd_process_device *pdd, +struct kfd_process *p); +/* Debugfs */ #if defined(CONFIG_DEBUG_FS )
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -0
@@ -1132,6 +1132,10 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)pr_debug ("Releasing pdd (topology id %d, for pid %d)\n" , pdd->dev->id, p->lead_thread->pid);kfd_process_profiler_release (p, pdd); + +if (pdd->ptl_disable_req) +kfd_ptl_disable_release (pdd, p); +kfd_process_device_destroy_cwsr_dgpu (pdd);kfd_process_device_destroy_ib_mem (pdd);
drivers/gpu/drm/amd/include/amdgpu_ptl.h +2 -0
@@ -39,6 +39,8 @@ struct amdgpu_ptl {enum amdgpu_ptl_fmt fmt2;bool enabled;bool hw_supported; +/* PTL disable reference counting */ +atomic_t disable_ref;struct mutex mutex; };
include/uapi/linux/kfd_ioctl.h +7 -0
@@ -1562,6 +1562,7 @@ struct kfd_ioctl_dbg_trap_args {enum kfd_profiler_ops {KFD_IOC_PROFILER_PMC =0 ,KFD_IOC_PROFILER_VERSION =2 , +KFD_IOC_PROFILER_PTL_CONTROL =3 , };/**
@@ -1573,10 +1574,16 @@ struct kfd_ioctl_pmc_settings {__u32 perfcount_enable;/* Force Perfcount Enable for queues on GPU */ }; +struct kfd_ioctl_ptl_control { +__u32 gpu_id;/* user_gpu_id */ +__u32 enable;/* set 1 to enable PTL, set 0 to disable PTL */ +}; +struct kfd_ioctl_profiler_args {__u32 op;/* kfd_profiler_op */ union {struct kfd_ioctl_pmc_settings pmc; +struct kfd_ioctl_ptl_control ptl;__u32 version;/* KFD_IOC_PROFILER_VERSION_NUM */ }; };
Signature · 1 line
2.34.1
PATCH v2 9/13 drm/amdkfd: suspend scheduler during PTL re-enabling
In reply to Perry YuanStop the scheduler before releasing the PTL disable request to ensure the GPU is quiescent during the PTL state transition. This prevents potential queue preemption failures and GPU resets caused by modifying PTL state while waves are executing
v1->v2:
only stop/start the scheduler when the PTL state actually needs to transition(Yifan)
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 30 ++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +28 -2
@@ -1780,10 +1780,17 @@ static int kfd_ptl_control(struct kfd_process_device *pdd, bool enable)if (!pdd->dev->kfd2kgd || !pdd->dev->kfd2kgd->ptl_ctrl)return -EOPNOTSUPP ; +if (adev->kfd.init_complete) +amdgpu_amdkfd_stop_sched (adev, pdd->dev->node_id); + ret = pdd->dev->kfd2kgd->ptl_ctrl (adev,PSP_PTL_PERF_MON_SET , &ptl_state, &pref_format1, &pref_format2); + +if (adev->kfd.init_complete) +amdgpu_amdkfd_start_sched (adev, pdd->dev->node_id); +return ret; }
@@ -3310,6 +3317,7 @@ static inline uint32_t profile_lock_device(struct kfd_process *p,struct kfd_process_device *pdd;struct kfd_dev *kfd;int status = -EINVAL ; +struct amdgpu_ptl *ptl;if (!p)return -EINVAL ;
@@ -3322,13 +3330,22 @@ static inline uint32_t profile_lock_device(struct kfd_process *p,return -EINVAL ; kfd = pdd->dev->kfd; + ptl = &pdd->dev->adev->psp.ptl;mutex_lock (&kfd->profiler_lock);if (op ==1 ) {if (!kfd->profiler_process) { kfd->profiler_process = p; status =0 ; -kfd_ptl_disable_request (pdd, p); +mutex_unlock (&kfd->profiler_lock); +if (ptl->hw_supported) { + status =kfd_ptl_disable_request (pdd, p); +if (status !=0 ) +dev_err (kfd_device, +"Failed to lock device %d for profiling, error %d\n" , + gpu_id, status); + } +return status; }else if (kfd->profiler_process == p) { status = -EALREADY ; }else {
@@ -3337,7 +3354,16 @@ static inline uint32_t profile_lock_device(struct kfd_process *p, }else if (op ==0 && kfd->profiler_process == p) { kfd->profiler_process =NULL ; status =0 ; -kfd_ptl_disable_release (pdd, p); +mutex_unlock (&kfd->profiler_lock); + +if (ptl->hw_supported) { + status =kfd_ptl_disable_release (pdd, p); +if (status) +dev_err (kfd_device, +"Failed to unlock device %d for profiling, error %d\n" , + gpu_id, status); + } +return status; }mutex_unlock (&kfd->profiler_lock);
Signature · 1 line
2.34.1
PATCH v2 10/13 drm/amdgpu: Track PTL disable requests by source
In reply to Perry YuanUse a bitmap to track PTL disable requests from sysfs and profiler. PTL is only re-enabled once all sources have released their disable requests, avoiding premature enablement.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 26 ++++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h | 1 + drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 3 +++ drivers/gpu/drm/amd/include/amdgpu_ptl.h | 6 ++++++ 4 files changed, 36 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +26 -0
@@ -1314,6 +1314,21 @@ int amdgpu_ptl_perf_monitor_ctrl(struct amdgpu_device *adev, u32 req_code, ptl->fmt2 == ptl_fmt2)return 0 ; +/* If enabling PTL, check disable bitmap */ +if (req_code ==PSP_PTL_PERF_MON_SET && *ptl_state ==1 ) { +if (!bitmap_empty (ptl->disable_bitmap, +AMDGPU_PTL_DISABLE_MAX )) { +dev_dbg (adev->dev, +"PTL enable blocked: SYSFS=%d, PROFILER=%d (ref=%d)\n" , +test_bit (AMDGPU_PTL_DISABLE_SYSFS , + ptl->disable_bitmap), +test_bit (AMDGPU_PTL_DISABLE_PROFILER , + ptl->disable_bitmap), +atomic_read (&ptl->disable_ref)); +return 0 ; + } + } +return psp_ptl_invoke (psp, req_code, ptl_state, &ptl_fmt1, &ptl_fmt2); }
@@ -1353,6 +1368,7 @@ static ssize_t ptl_enable_store(struct device *dev,uint32_t ptl_state, fmt1, fmt2;int ret;bool enable; +bool bit_changed = false;mutex_lock (&ptl->mutex);if (sysfs_streq (buf,"enabled" ) ||sysfs_streq (buf,"1" )) {
@@ -1368,14 +1384,24 @@ static ssize_t ptl_enable_store(struct device *dev, fmt2 = ptl->fmt2; ptl_state = enable ?1 :0 ; +if (enable) + bit_changed =test_and_clear_bit (AMDGPU_PTL_DISABLE_SYSFS , + ptl->disable_bitmap); + ret =amdgpu_ptl_perf_monitor_ctrl (adev,PSP_PTL_PERF_MON_SET , &ptl_state, &fmt1, &fmt2);if (ret) {dev_err (adev->dev,"Failed to set PTL err = %d\n" , ret); +if (enable && bit_changed) +set_bit (AMDGPU_PTL_DISABLE_SYSFS , ptl->disable_bitmap);mutex_unlock (&ptl->mutex);return ret; } +if (!enable) +set_bit (AMDGPU_PTL_DISABLE_SYSFS , ptl->disable_bitmap); +mutex_unlock (&ptl->mutex); +return count; }
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +1 -0
@@ -31,6 +31,7 @@#include "ta_ras_if.h" #include "ta_rap_if.h" #include "ta_secureDisplay_if.h" +#include <linux/bitops.h> #include "amdgpu_ptl.h" #define PSP_FENCE_BUFFER_SIZE 0x1000
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3 -0
@@ -1815,6 +1815,7 @@ int kfd_ptl_disable_request(struct kfd_process_device *pdd, goto out; } } +set_bit (AMDGPU_PTL_DISABLE_PROFILER , ptl->disable_bitmap); pdd->ptl_disable_req = true; out:
@@ -1835,9 +1836,11 @@ int kfd_ptl_disable_release(struct kfd_process_device *pdd, goto out;if (atomic_dec_return (&ptl->disable_ref) ==0 ) { +clear_bit (AMDGPU_PTL_DISABLE_PROFILER , ptl->disable_bitmap); ret =kfd_ptl_control (pdd, true);if (ret) {atomic_inc (&ptl->disable_ref); +set_bit (AMDGPU_PTL_DISABLE_PROFILER , ptl->disable_bitmap);dev_warn (adev->dev,"Failed to enable PTL on release: %d\n" , ret); goto out; }
drivers/gpu/drm/amd/include/amdgpu_ptl.h +6 -0
@@ -34,6 +34,11 @@ enum amdgpu_ptl_fmt { AMDGPU_PTL_FMT_INVALID = 7, }; +enum amdgpu_ptl_disable_source { +AMDGPU_PTL_DISABLE_SYSFS =0 , +AMDGPU_PTL_DISABLE_PROFILER , +AMDGPU_PTL_DISABLE_MAX , +};struct amdgpu_ptl {enum amdgpu_ptl_fmt fmt1;enum amdgpu_ptl_fmt fmt2;
@@ -42,6 +47,7 @@ struct amdgpu_ptl {/* PTL disable reference counting */ atomic_t disable_ref;struct mutex mutex; +DECLARE_BITMAP (disable_bitmap,AMDGPU_PTL_DISABLE_MAX ); }; int amdgpu_ptl_perf_monitor_ctrl(struct amdgpu_device *adev, u32 req_code,
Signature · 1 line
2.34.1
PATCH v2 11/13 drm/amdgpu: add amdgpu.ptl module parameter for PTL control
In reply to Perry YuanAdd a new kernel module parameter 'amdgpu.ptl' to allow users to enable or disable PTL feature at driver loading time.
Parameter values:
*) 0 or -1: disable PTL (default)
*) 1: enable PTL
*) 2: permanently disable PTL
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 1 + drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 13 +++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 12 ++++++++++ drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 28 +++++++++++++++++++++++- drivers/gpu/drm/amd/include/amdgpu_ptl.h | 1 + 5 files changed, 54 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
@@ -268,6 +268,7 @@ extern int amdgpu_rebar;extern int amdgpu_wbrf;extern int amdgpu_user_queue; +extern int amdgpu_ptl;extern uint amdgpu_hdmi_hpd_debounce_delay_ms;
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +13 -0
@@ -248,6 +248,7 @@ int amdgpu_umsch_mm_fwlog;int amdgpu_rebar =-1 ;/* auto */ int amdgpu_user_queue =-1 ;uint amdgpu_hdmi_hpd_debounce_delay_ms; +int amdgpu_ptl =-1 ;/* auto */ DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0, "DRM_UT_CORE",
@@ -1134,6 +1135,18 @@ module_param_named(user_queue, amdgpu_user_queue, int, 0444);MODULE_PARM_DESC (hdmi_hpd_debounce_delay_ms,"HDMI HPD disconnect debounce delay in milliseconds (0 to disable (by default), 1500 is common)" );module_param_named (hdmi_hpd_debounce_delay_ms, amdgpu_hdmi_hpd_debounce_delay_ms, uint,0644 ); +/** +* DOC: ptl (int) +* Enable PTL feature at boot time. Possible values: +* +* - -1 = auto (ASIC specific default) +* - 0 = disable PTL (default) +* - 1 = enable PTL +* - 2 = permanently disable PTL (cannot be re-enabled at runtime) +*/ +MODULE_PARM_DESC (ptl,"Enable PTL (-1 = auto, 0 = disable (default), 1 = enable, 2 = permanently disable)" ); +module_param_named (ptl, amdgpu_ptl, int,0444 ); +/* These devices are not supported by amdgpu. * They are supported by the mach64, r128, radeon drivers */
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +12 -0
@@ -1296,6 +1296,9 @@ int amdgpu_ptl_perf_monitor_ctrl(struct amdgpu_device *adev, u32 req_code, psp = &adev->psp; ptl = &psp->ptl; +if (ptl->permanently_disabled && *ptl_state ==1 ) +return 0 ; +if (amdgpu_ip_version (adev,GC_HWIP ,0 ) !=IP_VERSION (9 ,4 ,4 ) || psp->sos.fw_version <0x0036081a )return -EOPNOTSUPP ;
@@ -1380,6 +1383,12 @@ static ssize_t ptl_enable_store(struct device *dev,return -EINVAL ; } +/* Block enable when permanently disabled */ +if (ptl->permanently_disabled) { +mutex_unlock (&ptl->mutex); +return -EPERM ; + } + fmt1 = ptl->fmt1; fmt2 = ptl->fmt2; ptl_state = enable ?1 :0 ;
@@ -1411,6 +1420,9 @@ static ssize_t ptl_enable_show(struct device *dev, struct device_attribute *attrstruct amdgpu_device *adev =drm_to_adev (ddev);struct amdgpu_ptl *ptl = &adev->psp.ptl; +if (ptl->permanently_disabled) +return sysfs_emit (buf,"permanently disabled\n" ); +return sysfs_emit (buf,"%s\n" , ptl->enabled ?"enabled" :"disabled" ); }
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +27 -1
@@ -2401,6 +2401,32 @@ static int gfx_v9_4_3_perf_monitor_ptl_init(struct amdgpu_device *adev, bool enareturn 0 ; } +static int gfx_v9_4_3_ptl_hw_init (struct amdgpu_device *adev) +{ +struct amdgpu_ptl *ptl = &adev->psp.ptl; +bool enable; + +switch (amdgpu_ptl) { +case 1 : + enable = true; +break ; +case 2 : +/* Permanently disabled - cannot be re-enabled */ + enable = false; + ptl->permanently_disabled = true; +break ; +case -1 : +case 0 : +default : + enable = false; +break ; + } + +gfx_v9_4_3_perf_monitor_ptl_init (adev, enable ?1 :0 ); + +return 0 ; +} +static int gfx_v9_4_3_hw_fini (struct amdgpu_ip_block *ip_block) {struct amdgpu_device *adev = ip_block->adev;
@@ -2583,7 +2609,7 @@ static int gfx_v9_4_3_late_init(struct amdgpu_ip_block *ip_block) adev->gfx.ras->enable_watchdog_timer) adev->gfx.ras->enable_watchdog_timer (adev); -gfx_v9_4_3_perf_monitor_ptl_init (adev, true); +gfx_v9_4_3_ptl_hw_init (adev);return 0 ; }
drivers/gpu/drm/amd/include/amdgpu_ptl.h +1 -0
@@ -44,6 +44,7 @@ struct amdgpu_ptl {enum amdgpu_ptl_fmt fmt2;bool enabled;bool hw_supported; +bool permanently_disabled;/* PTL disable reference counting */ atomic_t disable_ref;struct mutex mutex;
Signature · 1 line
2.34.1
PATCH v2 12/13 drm/amdgpu: add new data types F8 and Vector for PTL
In reply to Perry YuanAdd F8 and VECTOR to amdgpu_ptl_fmt and PSP format mapping. Update PTL format strings and GFX format enum to keep PSP/KFD in sync.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c | 8 ++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h | 2 ++ drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +8 -0
@@ -58,6 +58,8 @@ static const char * const amdgpu_ptl_fmt_str[] = { [AMDGPU_PTL_FMT_BF16] = "BF16", [AMDGPU_PTL_FMT_F32] = "F32", [AMDGPU_PTL_FMT_F64] = "F64", + [AMDGPU_PTL_FMT_F8] = "F8", + [AMDGPU_PTL_FMT_VECTOR] = "VECTOR", [AMDGPU_PTL_FMT_INVALID] = "INVALID", };
@@ -1232,6 +1234,12 @@ static int psp_ptl_fmt_verify(struct psp_context *psp, enum amdgpu_ptl_fmt fmt,case AMDGPU_PTL_FMT_F64 : *ptl_fmt =GFX_FTYPE_F64 ;break ; +case AMDGPU_PTL_FMT_F8 : + *ptl_fmt =GFX_FTYPE_F8 ; +break ; +case AMDGPU_PTL_FMT_VECTOR : + *ptl_fmt =GFX_FTYPE_VECTOR ; +break ;default :return -EINVAL ; }
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +2 -0
@@ -372,6 +372,8 @@ enum psp_ptl_format_type {GFX_FTYPE_BF16 =0x00000002 ,GFX_FTYPE_F32 =0x00000003 ,GFX_FTYPE_F64 =0x00000004 , +GFX_FTYPE_F8 =0x00000005 , +GFX_FTYPE_VECTOR =0x00000006 , GFX_FTYPE_INVALID = 0xFFFFFFFF, };
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_3.c +2 -2
@@ -2381,8 +2381,8 @@ static int gfx_v9_4_3_perf_monitor_ptl_init(struct amdgpu_device *adev, bool enareturn -EOPNOTSUPP ;if (!ptl->hw_supported) { - fmt1 =GFX_FTYPE_I8 ; - fmt2 =GFX_FTYPE_BF16 ; + fmt1 =GFX_FTYPE_VECTOR ; + fmt2 =GFX_FTYPE_F8 ; }else { fmt1 = ptl->fmt1; fmt2 = ptl->fmt2;
Signature · 1 line
2.34.1
PATCH v2 13/13 drm/amdgpu: Wait for GFX idle before PTL state transition
In reply to Perry YuanEnsure GFX engine is idle before switching PTL state to prevent register access violations and CP hang. This addresses the race condition where in-flight GPU commands could conflict with PTL state changes.
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +5 -0
@@ -1783,6 +1783,11 @@ static int kfd_ptl_control(struct kfd_process_device *pdd, bool enable)if (adev->kfd.init_complete)amdgpu_amdkfd_stop_sched (adev, pdd->dev->node_id); +/* Wait for GFX to be idle before PTL operation */ + ret =amdgpu_device_ip_wait_for_idle (adev,AMD_IP_BLOCK_TYPE_GFX ); +if (ret) +return -ETIMEDOUT ; + ret = pdd->dev->kfd2kgd->ptl_ctrl(adev, PSP_PTL_PERF_MON_SET, &ptl_state, &pref_format1,
Signature · 1 line
2.34.1