drm/amdgpu/vcn: fix video profile race condition (v3)
Subsystems (2)
Alex Deucher
maintainerPATCH 1/1 drm/amdgpu/vcn: fix video profile race condition (v3)
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handler
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +20 -20
@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags &AMD_PG_SUPPORT_VCN_DPG && - !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||
@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ +if (adev->vcn.workload_profile_active && !total_fences && + !atomic_read (&adev->vcn.total_submission_cnt)) { r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , false);if (r)
@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO,
@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );
@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt);schedule_delayed_work (&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT );
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1 -0
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Signature · 1 line
2.50.1
Hi Alex,
The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the begin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.
Thanks,
David
Quoted 79 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profile handling")Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +18
-12@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags &AMD_PG_SUPPORT_VCN_DPG && - !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ +if (adev->vcn.workload_profile_active && !total_fences && + !atomic_read (&adev->vcn.total_submission_cnt)) { r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , false);if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) · line 476
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) · line 480
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);
move to here:
atomic_inc(&adev->vcn.total_submission_cnt); I think this should work for multiple instances.
David
Quoted 30 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO,@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt);schedule_delayed_work (&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT );drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 108 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that
it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 79 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profile handling")Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +18
-12@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags &AMD_PG_SUPPORT_VCN_DPG && - !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ +if (adev->vcn.workload_profile_active && !total_fences && + !atomic_read (&adev->vcn.total_submission_cnt)) { r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , false);if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) · line 476
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) · line 480
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);I think this should work for multiple instances.
Why does this need to be protected by the mutex?
Alex
Quoted 33 lines
David
Quoted 30 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO,@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt);schedule_delayed_work (&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT );drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 108 lines · Alex Deucher
Quoted 107 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that
it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 79 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profile handling")Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +18
-12@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags &AMD_PG_SUPPORT_VCN_DPG && - !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ +if (adev->vcn.workload_profile_active && !total_fences && + !atomic_read (&adev->vcn.total_submission_cnt)) { r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , false);if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) · line 476
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);
Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);I think this should work for multiple instances.
Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 33 lines
Alex
Quoted 31 lines
David
Quoted 30 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO,@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt);schedule_delayed_work (&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT );drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and begin_use and using flags to track power state could help us totally avoid this situation.
Regards,
Sathish
Quoted 160 lines · David
Quoted 117 lines · Alex Deucher
Quoted 116 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that
it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 87 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profile handling")Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c| 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h| 1 +2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)struct
amdgpu_vcn_inst*vcn_inst=
container_of(work, struct amdgpu_vcn_inst, idle_work.work); structamdgpu_device*adev =vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0}; - unsigned int i =vcn_inst->inst, j;
+ unsigned inttotal_fences= 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (
adev->vcn.harvest_config& (1 << i))
+ if (adev->vcn.harvest_config& (1 <<vcn_inst->inst)) return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i <adev->vcn.num_vcn_inst; ++i) {
+ structamdgpu_vcn_inst*v = &adev->vcn.inst[i]; +
+ for (j = 0; j <v->num_enc_rings; ++j)
+ fence[i] +=amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] +=amdgpu_fence_count_emitted(&v->ring_dec); +total_fences+= fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (
adev->pg_flags&AMD_PG_SUPPORT_VCN_DPG&&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
structdpg_pause_statenew_state;if (fence[i] ||
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)else
new_state.fw_based =VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst,
&new_state); +vcn_inst->pause_dpg_mode(vcn_inst, &new_state); }- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */mutex_lock(&vcn_inst->vcn_pg_lock);vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);mutex_unlock(&vcn_inst->vcn_pg_lock);mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active&&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO, false);
if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) int r = 0; atomic_inc(&vcn_inst->total_submission_cnt); + atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); - /* We can safely return early here because we've cancelled the - * the delayed work so there is no one else to set it to false - * and we don't care if someone else sets it to true. - */ - if (adev->vcn.workload_profile_active) - goto pg_lock; - mutex_lock(&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 35 lines
Alex
Quoted 33 lines
David
Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO,@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock: mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn { uint16_t inst_mask; uint8_t num_inst_per_aid; + atomic_t total_submission_cnt; /* IP reg dump */ uint32_t *ip_dump;
Quoted 11 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 4 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Quoted 176 lines
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 168 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
From 8599112cfc94e8bcf472db38627fd9b3261b107a Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Tue, 12 Aug 2025 11:38:09 -0400
Subject: [PATCH 1/2] drm/amdgpu/vcn: fix video profile race condition (v4)
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Split the into two work handlers, one for global state and one for per instance state.
v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handler
v4: split into two handlers, one global and one per instance
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 56 +++++++++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 4 ++ drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c | 3 ++ drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 2 + drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 3 ++ drivers/gpu/drm/amd/amdgpu/vcn_v4_0_3.c | 3 ++ drivers/gpu/drm/amd/amdgpu/vcn_v4_0_5.c | 3 ++ drivers/gpu/drm/amd/amdgpu/vcn_v5_0_0.c | 3 ++ drivers/gpu/drm/amd/amdgpu/vcn_v5_0_1.c | 3 ++ 9 files changed, 63 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +39 -17
@@ -91,7 +91,7 @@ MODULE_FIRMWARE(FIRMWARE_VCN4_0_6_1);MODULE_FIRMWARE (FIRMWARE_VCN5_0_0 );MODULE_FIRMWARE (FIRMWARE_VCN5_0_1 ); -static void amdgpu_vcn_idle_work_handler (struct work_struct *work); +static void amdgpu_vcn_instance_work_handler (struct work_struct *work);static void amdgpu_vcn_reg_dump_fini (struct amdgpu_device *adev);int amdgpu_vcn_early_init (struct amdgpu_device *adev,int i)
@@ -137,7 +137,7 @@ int amdgpu_vcn_sw_init(struct amdgpu_device *adev, int i)mutex_init (&adev->vcn.inst[i].vcn_pg_lock);mutex_init (&adev->vcn.inst[i].engine_reset_mutex);atomic_set (&adev->vcn.inst[i].total_submission_cnt,0 ); -INIT_DELAYED_WORK (&adev->vcn.inst[i].idle_work, amdgpu_vcn_idle_work_handler); +INIT_DELAYED_WORK (&adev->vcn.inst[i].idle_work, amdgpu_vcn_instance_work_handler);atomic_set (&adev->vcn.inst[i].dpg_enc_submission_cnt,0 );if ((adev->firmware.load_type ==AMDGPU_FW_LOAD_PSP ) && (adev->pg_flags &AMD_PG_SUPPORT_VCN_DPG ))
@@ -408,43 +408,61 @@ int amdgpu_vcn_resume(struct amdgpu_device *adev, int i)return 0 ; } -static void amdgpu_vcn_idle_work_handler (struct work_struct *work) +static void amdgpu_vcn_instance_work_handler (struct work_struct *work) {struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; -int r =0 ; +unsigned int fences =0 ; +unsigned int i; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < vcn_inst->num_enc_rings; ++i) + fences +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[i]); + fences +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec);/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags &AMD_PG_SUPPORT_VCN_DPG && - !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; -if (fence[i] || +if (fences ||unlikely (atomic_read (&vcn_inst->dpg_enc_submission_cnt))) new_state.fw_based =VCN_DPG_STATE__PAUSE ;else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) {mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock); -mutex_lock (&adev->vcn.workload_profile_mutex); + }else { +schedule_delayed_work (&vcn_inst->idle_work,VCN_IDLE_TIMEOUT ); + } +} + +void amdgpu_vcn_global_work_handler (struct work_struct *work) +{ +struct amdgpu_device *adev = + container_of(work, struct amdgpu_device, vcn.global_work.work); +unsigned int total_fences =0 ; +unsigned int i, j; +int r =0 ; + +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + total_fences +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + total_fences +=amdgpu_fence_count_emitted (&v->ring_dec); + } + +if (!total_fences && !atomic_read (&adev->vcn.total_submission_cnt)) {if (adev->vcn.workload_profile_active) { r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , false);
@@ -454,7 +472,7 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) }mutex_unlock (&adev->vcn.workload_profile_mutex); }else { -schedule_delayed_work (&vcn_inst->idle_work,VCN_IDLE_TIMEOUT ); +schedule_delayed_work (&adev->vcn.global_work,VCN_IDLE_TIMEOUT ); } }
@@ -464,8 +482,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)struct amdgpu_vcn_inst *vcn_inst = &adev->vcn.inst[ring->me];int r =0 ; +atomic_inc (&adev->vcn.total_submission_cnt);atomic_inc (&vcn_inst->total_submission_cnt); +cancel_delayed_work_sync (&adev->vcn.global_work);cancel_delayed_work_sync (&vcn_inst->idle_work);/* We can safely return early here because we've cancelled the
@@ -525,8 +545,10 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring) !adev->vcn.inst[ring->me].using_unified_queue)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +schedule_delayed_work (&adev->vcn.global_work,VCN_IDLE_TIMEOUT );schedule_delayed_work (&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT ); }
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +4 -0
@@ -352,6 +352,8 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt; +struct delayed_work global_work;/* IP reg dump */ uint32_t *ip_dump;
@@ -565,4 +567,6 @@ int amdgpu_vcn_reg_dump_init(struct amdgpu_device *adev, const struct amdgpu_hwip_reg_entry *reg, u32 count);void amdgpu_vcn_dump_ip_state (struct amdgpu_ip_block *ip_block);void amdgpu_vcn_print_ip_state (struct amdgpu_ip_block *ip_block,struct drm_printer *p); +void amdgpu_vcn_global_work_handler (struct work_struct *work); +#endif
drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c +3 -0
@@ -139,6 +139,8 @@ static int vcn_v2_0_sw_init(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;volatile struct amdgpu_fw_shared *fw_shared; +INIT_DELAYED_WORK (&adev->vcn.global_work, amdgpu_vcn_global_work_handler); +/* VCN DEC TRAP */ r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VCN, VCN_2_0__SRCID__UVD_SYSTEM_MESSAGE_INTERRUPT,
@@ -322,6 +324,7 @@ static int vcn_v2_0_hw_fini(struct amdgpu_ip_block *ip_block)struct amdgpu_vcn_inst *vinst = adev->vcn.inst;cancel_delayed_work_sync (&vinst->idle_work); +cancel_delayed_work_sync (&adev->vcn.global_work); if ((adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) || (vinst->cur_state != AMD_PG_STATE_GATE &&
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c +2 -0
@@ -177,6 +177,7 @@ static int vcn_v3_0_sw_init(struct amdgpu_ip_block *ip_block)int vcn_doorbell_index =0 ;struct amdgpu_device *adev = ip_block->adev; +INIT_DELAYED_WORK (&adev->vcn.global_work, amdgpu_vcn_global_work_handler);/* * Note: doorbell assignment is fixed for SRIOV multiple VCN engines * Formula:
@@ -444,6 +445,7 @@ static int vcn_v3_0_hw_fini(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i; +cancel_delayed_work_sync (&adev->vcn.global_work);for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *vinst = &adev->vcn.inst[i];
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c +3 -0
@@ -184,6 +184,8 @@ static int vcn_v4_0_sw_init(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i, r; +INIT_DELAYED_WORK (&adev->vcn.global_work, amdgpu_vcn_global_work_handler); +for (i =0 ; i < adev->vcn.num_vcn_inst; i++) {if (adev->vcn.harvest_config & (1 << i))continue ;
@@ -370,6 +372,7 @@ static int vcn_v4_0_hw_fini(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i; +cancel_delayed_work_sync (&adev->vcn.global_work);for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *vinst = &adev->vcn.inst[i];
drivers/gpu/drm/amd/amdgpu/vcn_v4_0_3.c +3 -0
@@ -161,6 +161,8 @@ static int vcn_v4_0_3_sw_init(struct amdgpu_ip_block *ip_block)struct amdgpu_ring *ring;int i, r, vcn_inst; +INIT_DELAYED_WORK (&adev->vcn.global_work, amdgpu_vcn_global_work_handler); +/* VCN DEC TRAP */ r =amdgpu_irq_add_id (adev,SOC15_IH_CLIENTID_VCN ,VCN_4_0__SRCID__UVD_ENC_GENERAL_PURPOSE , &adev->vcn.inst->irq);
@@ -373,6 +375,7 @@ static int vcn_v4_0_3_hw_fini(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i; +cancel_delayed_work_sync (&adev->vcn.global_work);for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *vinst = &adev->vcn.inst[i];
drivers/gpu/drm/amd/amdgpu/vcn_v4_0_5.c +3 -0
@@ -148,6 +148,8 @@ static int vcn_v4_0_5_sw_init(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i, r; +INIT_DELAYED_WORK (&adev->vcn.global_work, amdgpu_vcn_global_work_handler); +for (i =0 ; i < adev->vcn.num_vcn_inst; i++) {volatile struct amdgpu_vcn4_fw_shared *fw_shared;
@@ -320,6 +322,7 @@ static int vcn_v4_0_5_hw_fini(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i; +cancel_delayed_work_sync (&adev->vcn.global_work);for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *vinst = &adev->vcn.inst[i];
drivers/gpu/drm/amd/amdgpu/vcn_v5_0_0.c +3 -0
@@ -128,6 +128,8 @@ static int vcn_v5_0_0_sw_init(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i, r; +INIT_DELAYED_WORK (&adev->vcn.global_work, amdgpu_vcn_global_work_handler); +for (i =0 ; i < adev->vcn.num_vcn_inst; i++) {volatile struct amdgpu_vcn5_fw_shared *fw_shared;
@@ -283,6 +285,7 @@ static int vcn_v5_0_0_hw_fini(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i; +cancel_delayed_work_sync (&adev->vcn.global_work);for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *vinst = &adev->vcn.inst[i];
drivers/gpu/drm/amd/amdgpu/vcn_v5_0_1.c +3 -0
@@ -142,6 +142,8 @@ static int vcn_v5_0_1_sw_init(struct amdgpu_ip_block *ip_block)struct amdgpu_ring *ring;int i, r, vcn_inst; +INIT_DELAYED_WORK (&adev->vcn.global_work, amdgpu_vcn_global_work_handler); +/* VCN UNIFIED TRAP */ r =amdgpu_irq_add_id (adev,SOC15_IH_CLIENTID_VCN ,VCN_5_0__SRCID__UVD_ENC_GENERAL_PURPOSE , &adev->vcn.inst->irq);
@@ -318,6 +320,7 @@ static int vcn_v5_0_1_hw_fini(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i; +cancel_delayed_work_sync (&adev->vcn.global_work);for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *vinst = &adev->vcn.inst[i];
Signature · 1 line
2.50.1
From c67e8ec11ed1a80bdd48889edf2f0542ce7c39a5 Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Wed, 13 Aug 2025 16:03:20 -0400
Subject: [PATCH 2/2] drm/amdgpu/vcn2.5: switch to the global work handler
All of the idle work on vcn2.5 is global, so use the new global work handler rather than abusing a single instance of the per instance work handler.
--- drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 28 ++++++++++++--------------- 1 file changed, 12 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +12 -16
@@ -109,11 +109,10 @@ static int amdgpu_ih_clientid_vcns[] = {SOC15_IH_CLIENTID_VCN1 }; -static void vcn_v2_5_idle_work_handler (struct work_struct *work) +static void vcn_v2_5_global_work_handler (struct work_struct *work) { -struct amdgpu_vcn_inst *vcn_inst = - container_of(work, struct amdgpu_vcn_inst, idle_work.work); -struct amdgpu_device *adev = vcn_inst->adev; +struct amdgpu_device *adev = + container_of(work, struct amdgpu_device, vcn.global_work.work);unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 };unsigned int i, j;int r =0 ;
@@ -146,7 +145,7 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work) } -if (!fences && !atomic_read (&adev->vcn.inst[0 ].total_submission_cnt)) { +if (!fences && !atomic_read (&adev->vcn.total_submission_cnt)) {amdgpu_device_ip_set_powergating_state (adev,AMD_IP_BLOCK_TYPE_VCN ,AMD_PG_STATE_GATE );mutex_lock (&adev->vcn.workload_profile_mutex);
@@ -159,7 +158,7 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work) }mutex_unlock (&adev->vcn.workload_profile_mutex); }else { -schedule_delayed_work (&adev->vcn.inst[0 ].idle_work,VCN_IDLE_TIMEOUT ); +schedule_delayed_work (&adev->vcn.global_work,VCN_IDLE_TIMEOUT ); } }
@@ -169,9 +168,9 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring)struct amdgpu_vcn_inst *v = &adev->vcn.inst[ring->me];int r =0 ; -atomic_inc (&adev->vcn.inst[0 ].total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt); -cancel_delayed_work_sync (&adev->vcn.inst[0 ].idle_work); +cancel_delayed_work_sync (&adev->vcn.global_work);/* We can safely return early here because we've cancelled the * the delayed work so there is no one else to set it to false
@@ -230,10 +229,9 @@ static void vcn_v2_5_ring_end_use(struct amdgpu_ring *ring) !adev->vcn.inst[ring->me].using_unified_queue)atomic_dec (&adev->vcn.inst[ring->me].dpg_enc_submission_cnt); -atomic_dec (&adev->vcn.inst[0 ].total_submission_cnt); +atomic_dec (&adev->vcn.total_submission_cnt); -schedule_delayed_work (&adev->vcn.inst[0 ].idle_work, -VCN_IDLE_TIMEOUT ); +schedule_delayed_work (&adev->vcn.global_work,VCN_IDLE_TIMEOUT ); }/**
@@ -299,6 +297,8 @@ static int vcn_v2_5_sw_init(struct amdgpu_ip_block *ip_block)int i, j, r;struct amdgpu_device *adev = ip_block->adev; +INIT_DELAYED_WORK (&adev->vcn.global_work, vcn_v2_5_global_work_handler); +for (j =0 ; j < adev->vcn.num_vcn_inst; j++) {volatile struct amdgpu_fw_shared *fw_shared;
@@ -328,9 +328,6 @@ static int vcn_v2_5_sw_init(struct amdgpu_ip_block *ip_block)if (r)return r; -/* Override the work func */ - adev->vcn.inst[j].idle_work.work.func = vcn_v2_5_idle_work_handler; -amdgpu_vcn_setup_ucode (adev, j); r =amdgpu_vcn_resume (adev, j);
@@ -533,14 +530,13 @@ static int vcn_v2_5_hw_fini(struct amdgpu_ip_block *ip_block)struct amdgpu_device *adev = ip_block->adev;int i; +cancel_delayed_work_sync (&adev->vcn.global_work);for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *vinst = &adev->vcn.inst[i];if (adev->vcn.harvest_config & (1 << i))continue ; -cancel_delayed_work_sync (&vinst->idle_work); -if ((adev->pg_flags &AMD_PG_SUPPORT_VCN_DPG ) || (vinst->cur_state !=AMD_PG_STATE_GATE &&RREG32_SOC15 (VCN , i, mmUVD_STATUS)))
Signature · 1 line
2.50.1
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts. inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- as begin_use increaments vcn.total_submission_cnt and end_use decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between jobs , can be 0.
- both of the above conditions do not mean video decode is complete on inst1, it is actively decoding.
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new begin_use on any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.
Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
Sathish
Quoted 187 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 16 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.
How can there be active decoding without an outstanding fence? In that case, total_fences (fences from both instances) would be non-0.
Alex
Quoted 205 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 187 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 28 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.
Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.
I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
and commited to ring (inst1) , this doesn't mean decode has stopped on inst1 right (I am saying if timing of inst0 idle work coincides with this),
Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
Regards,
Sathish
Quoted 206 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 34 lines · Sathishkumar
Quoted 28 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.
Quoted 8 lines
I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
and commited to ring (inst1) , this doesn't mean decode has stopped on inst1 right (I am saying if timing of inst0 idle work coincides with this),
Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)
...later
fence signals
...later
work handler
In begin_use we increment the global and per instance submission. This protects the power gating and profile until end_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete. For power gating, we only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.
Alex
Quoted 211 lines
Regards,
Sathish
Quoted 206 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 52 lines · Alex Deucher
Quoted 31 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.Quoted 8 lines
I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
and commited to ring (inst1) , this doesn't mean decode has stopped on inst1 right (I am saying if timing of inst0 idle work coincides with this),
Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handler
Quoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.
Is a next begin_use always guaranteed to be run before current job fence signals ?
if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 217 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 209 lines
Regards,
Sathish
Quoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)
Quoted 54 lines · Alex Deucher
Quoted 34 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence,
between jobs , can be 0.
- both of the above conditions do not mean video decode is
complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handler
Quoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.
Is a next begin_use always guaranteed to be run before current job fence signals ?
if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 216 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 208 lines
Regards,
Sathish
Quoted 204 lines
Alex
Quoted 202 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should have
used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 125 lines · Alex Deucher
Quoted 124 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 98 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] || @@ -436,18 +442,18 @@static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based =VCN_DPG_STATE__UNPAUSE;drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +2
-5- adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { + if (!fence[vcn_inst->inst] &&!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE); mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +2
-1-if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO, false); if (r) @@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) int r = 0; atomic_inc(&vcn_inst->total_submission_cnt);Quoted 1 line
+
atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7-/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 35 lines
Alex
Quoted 33 lines
David
Quoted 32 lines
if (!adev->vcn.workload_profile_active) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submission _cnt);
atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission_c nt);
+atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
indexb3fb1d0e43fc9..febc3ce8641ff100644drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +0
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h struct amdgpu_vcn { · line 358
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
From 0730094e8c2fd7134fed38a1d0aee74c456358d2 Mon Sep 17 00:00:00 2001
From: Lijo Lazar <[email protected]>
Date: Thu, 14 Aug 2025 13:52:50 +0530
Subject: [PATCH] drm/amdgpu: Check vcn state before profile switch
Before switching video profile, check power state of all VCN instances. Before submission, power state is changed and a check is done for vidoe profile. If already active, it skips switching; otherwise, it switches to video profile. When it gets to idle state, power state of the current instance is set to gated. After that a check is done if all instances are in gated state. If so, video profile is disabled.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 83 ++++++++++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 5 +- drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 27 +------- 3 files changed, 60 insertions(+), 55 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +54 -29
@@ -410,6 +410,57 @@ int amdgpu_vcn_resume(struct amdgpu_device *adev, int i)return 0 ; } +int amdgpu_vcn_get_profile (struct amdgpu_device *adev) +{ +int r; + +if (adev->vcn.workload_profile_active) +return 0 ; + +mutex_lock (&adev->vcn.workload_profile_mutex); + + r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , + true); +if (r) +dev_warn (adev->dev, +"(%d) failed to enable video power profile mode\n" , r); +else + adev->vcn.workload_profile_active = true; +mutex_unlock (&adev->vcn.workload_profile_mutex); + +return r; +} + +int amdgpu_vcn_put_profile (struct amdgpu_device *adev) +{ +bool pg = true; +int r, i; + +mutex_lock (&adev->vcn.workload_profile_mutex); +for (i =0 ; i < adev->vcn.num_vcn_inst; i++) { +if (adev->vcn.inst[i].cur_state !=AMD_PG_STATE_GATE ) { + pg = false; +break ; + } + } + +if (pg) { + r =amdgpu_dpm_switch_power_profile ( + adev,PP_SMC_POWER_PROFILE_VIDEO , false); +if (r) +dev_warn ( + adev->dev, +"(%d) failed to disable video power profile mode\n" , + r); +else + adev->vcn.workload_profile_active = false; + } + +mutex_unlock (&adev->vcn.workload_profile_mutex); + +return r; +} +static void amdgpu_vcn_idle_work_handler (struct work_struct *work) { struct amdgpu_vcn_inst *vcn_inst =
@@ -417,7 +468,6 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)struct amdgpu_device *adev = vcn_inst->adev;unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 };unsigned int i = vcn_inst->inst, j; -int r =0 ;if (adev->vcn.harvest_config & (1 << i))return ;
@@ -446,15 +496,8 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock); -mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); - adev->vcn.workload_profile_active = false; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); +amdgpu_vcn_put_profile (adev); + }else {schedule_delayed_work (&vcn_inst->idle_work,VCN_IDLE_TIMEOUT ); }
@@ -464,32 +507,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) {struct amdgpu_device *adev = ring->adev;struct amdgpu_vcn_inst *vcn_inst = &adev->vcn.inst[ring->me]; -int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt);cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; - -mutex_lock (&adev->vcn.workload_profile_mutex); -if (!adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - true); -if (r) -dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); - adev->vcn.workload_profile_active = true; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); - -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE ); +amdgpu_vcn_get_profile (adev);/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +4 -1
@@ -362,7 +362,7 @@ struct amdgpu_vcn {bool per_inst_fw;unsigned fw_version; -bool workload_profile_active; +bool workload_profile_active;struct mutex workload_profile_mutex;u32 reg_count;const struct amdgpu_hwip_reg_entry *reg_list;
@@ -565,4 +565,7 @@ int amdgpu_vcn_reg_dump_init(struct amdgpu_device *adev, const struct amdgpu_hwip_reg_entry *reg, u32 count);void amdgpu_vcn_dump_ip_state (struct amdgpu_ip_block *ip_block);void amdgpu_vcn_print_ip_state (struct amdgpu_ip_block *ip_block,struct drm_printer *p); +int amdgpu_vcn_get_profile (struct amdgpu_device *adev); +int amdgpu_vcn_put_profile (struct amdgpu_device *adev); +#endif
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +2 -25
@@ -116,7 +116,6 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work)struct amdgpu_device *adev = vcn_inst->adev;unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 };unsigned int i, j; -int r =0 ;for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *v = &adev->vcn.inst[i];
@@ -149,15 +148,7 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work)if (!fences && !atomic_read (&adev->vcn.inst[0 ].total_submission_cnt)) {amdgpu_device_ip_set_powergating_state (adev,AMD_IP_BLOCK_TYPE_VCN ,AMD_PG_STATE_GATE ); -mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); - adev->vcn.workload_profile_active = false; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); +amdgpu_vcn_put_profile (adev); }else {schedule_delayed_work (&adev->vcn.inst[0 ].idle_work,VCN_IDLE_TIMEOUT ); }
@@ -167,7 +158,6 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring) {struct amdgpu_device *adev = ring->adev;struct amdgpu_vcn_inst *v = &adev->vcn.inst[ring->me]; -int r =0 ;atomic_inc (&adev->vcn.inst[0 ].total_submission_cnt);
@@ -177,23 +167,10 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring)* the delayed work so there is no one else to set it to false * and we don't care if someone else sets it to true. */ -if (adev->vcn.workload_profile_active) - goto pg_lock; - -mutex_lock (&adev->vcn.workload_profile_mutex); -if (!adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - true); -if (r) -dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); - adev->vcn.workload_profile_active = true; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); - -pg_lock:mutex_lock (&adev->vcn.inst[0 ].vcn_pg_lock);amdgpu_device_ip_set_powergating_state (adev,AMD_IP_BLOCK_TYPE_VCN ,AMD_PG_STATE_UNGATE ); +amdgpu_vcn_get_profile (adev);/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
Signature · 1 line
2.49.0
Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by workload_profile_mutex
every where, it can still change while you are holding workload_profile_mutex and checking it.
Regards,
Sathish
Quoted 304 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence,
between jobs , can be 0.
- both of the above conditions do not mean video decode is
complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 222 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 214 lines
Regards,
Sathish
Quoted 210 lines
Alex
Quoted 208 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 192 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 185 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should have
used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 173 lines · David
Quoted 125 lines · Alex Deucher
Quoted 124 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 98 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] || @@ -436,18 +442,18 @@static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based =VCN_DPG_STATE__UNPAUSE;drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +2
-5- adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { + if (!fence[vcn_inst->inst] &&!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE); mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +2
-1-if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO, false); if (r) @@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) int r = 0; atomic_inc(&vcn_inst->total_submission_cnt);Quoted 1 line
+
atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7-/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 41 lines
Alex
Quoted 39 lines
David
Quoted 38 lines
if (!adev->vcn.workload_profile_active) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submission _cnt);
atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission_c nt);
+atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
indexb3fb1d0e43fc9..febc3ce8641ff100644drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)
Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by workload_profile_mutex
every where, it can still change while you are holding workload_profile_mutex and checking it.
Regards,
Sathish
Quoted 313 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence,
between jobs , can be 0.
- both of the above conditions do not mean video decode is
complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 227 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 219 lines
Regards,
Sathish
Quoted 215 lines
Alex
Quoted 213 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 197 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 190 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should have
used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 178 lines · David
Quoted 128 lines · Alex Deucher
Quoted 127 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 101 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workloadprofile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +12
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = + &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] || @@ -436,18 +442,18 @@static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based =VCN_DPG_STATE__UNPAUSE;drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3
-5- adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, + &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { + if (!fence[vcn_inst->inst] &&!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE); mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +2
-1-if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO, false); if (r) @@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) int r = 0; atomic_inc(&vcn_inst->total_submission_cnt);Quoted 1 line
+
atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7-/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 43 lines
Alex
Quoted 41 lines
David
Quoted 40 lines
if (!adev->vcn.workload_profile_active) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring) atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submissio n_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission_ c
nt);
+atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--gita/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
indexb3fb1d0e43fc9..febc3ce8641ff100644drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)
[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)
Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by workload_profile_mutex
every where, it can still change while you are holding workload_profile_mutex and checking it.
Regards,
Sathish
Quoted 307 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence,
between jobs , can be 0.
- both of the above conditions do not mean video decode is
complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 221 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 213 lines
Regards,
Sathish
Quoted 209 lines
Alex
Quoted 207 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 191 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 184 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should have
used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 172 lines · David
Quoted 128 lines · Alex Deucher
Quoted 127 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 101 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workloadprofile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +12
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = + &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] || @@ -436,18 +442,18 @@static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based =VCN_DPG_STATE__UNPAUSE;drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3
-5- adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, + &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { + if (!fence[vcn_inst->inst] &&!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE); mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +2
-1-if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO, false); if (r) @@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) int r = 0; atomic_inc(&vcn_inst->total_submission_cnt);Quoted 1 line
+
atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7-/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
if (!adev->vcn.workload_profile_active) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring) atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submissio n_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission_ c
nt);
+atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--gita/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
indexb3fb1d0e43fc9..febc3ce8641ff100644drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +0
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h struct amdgpu_vcn { · line 358
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
From 55d853b2e43b62acb83463777b0d89918eab995b Mon Sep 17 00:00:00 2001
From: Lijo Lazar <[email protected]>
Date: Thu, 14 Aug 2025 13:52:50 +0530
Subject: [PATCH] drm/amdgpu: Check vcn state before profile switch
Before switching video profile, check power state of all VCN instances. Before submission, power state is changed and a check is done for vidoe profile. If already active, it skips switching; otherwise, it switches to video profile. When it gets to idle state, power state of the current instance is set to gated. After that a check is done if all instances are in gated state. If so, video profile is disabled.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 85 ++++++++++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 5 +- drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 27 +------- 3 files changed, 62 insertions(+), 55 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +56 -29
@@ -410,6 +410,59 @@ int amdgpu_vcn_resume(struct amdgpu_device *adev, int i)return 0 ; } +int amdgpu_vcn_get_profile (struct amdgpu_device *adev) +{ +int r; + + +mutex_lock (&adev->vcn.workload_profile_mutex); + +if (adev->vcn.workload_profile_active) { +mutex_unlock (&adev->vcn.workload_profile_mutex); +return 0 ; + } + r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , + true); +if (r) +dev_warn (adev->dev, +"(%d) failed to enable video power profile mode\n" , r); +else + adev->vcn.workload_profile_active = true; +mutex_unlock (&adev->vcn.workload_profile_mutex); + +return r; +} + +int amdgpu_vcn_put_profile (struct amdgpu_device *adev) +{ +bool pg = true; +int r, i; + +mutex_lock (&adev->vcn.workload_profile_mutex); +for (i =0 ; i < adev->vcn.num_vcn_inst; i++) { +if (adev->vcn.inst[i].cur_state !=AMD_PG_STATE_GATE ) { + pg = false; +break ; + } + } + +if (pg) { + r =amdgpu_dpm_switch_power_profile ( + adev,PP_SMC_POWER_PROFILE_VIDEO , false); +if (r) +dev_warn ( + adev->dev, +"(%d) failed to disable video power profile mode\n" , + r); +else + adev->vcn.workload_profile_active = false; + } + +mutex_unlock (&adev->vcn.workload_profile_mutex); + +return r; +} +static void amdgpu_vcn_idle_work_handler (struct work_struct *work) { struct amdgpu_vcn_inst *vcn_inst =
@@ -417,7 +470,6 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)struct amdgpu_device *adev = vcn_inst->adev;unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 };unsigned int i = vcn_inst->inst, j; -int r =0 ;if (adev->vcn.harvest_config & (1 << i))return ;
@@ -446,15 +498,8 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock); -mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); - adev->vcn.workload_profile_active = false; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); +amdgpu_vcn_put_profile (adev); + }else {schedule_delayed_work (&vcn_inst->idle_work,VCN_IDLE_TIMEOUT ); }
@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) {struct amdgpu_device *adev = ring->adev;struct amdgpu_vcn_inst *vcn_inst = &adev->vcn.inst[ring->me]; -int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt);cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; - -mutex_lock (&adev->vcn.workload_profile_mutex); -if (!adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - true); -if (r) -dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); - adev->vcn.workload_profile_active = true; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); - -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE ); +amdgpu_vcn_get_profile (adev);/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +4 -1
@@ -362,7 +362,7 @@ struct amdgpu_vcn {bool per_inst_fw;unsigned fw_version; -bool workload_profile_active; +bool workload_profile_active;struct mutex workload_profile_mutex;u32 reg_count;const struct amdgpu_hwip_reg_entry *reg_list;
@@ -565,4 +565,7 @@ int amdgpu_vcn_reg_dump_init(struct amdgpu_device *adev, const struct amdgpu_hwip_reg_entry *reg, u32 count);void amdgpu_vcn_dump_ip_state (struct amdgpu_ip_block *ip_block);void amdgpu_vcn_print_ip_state (struct amdgpu_ip_block *ip_block,struct drm_printer *p); +int amdgpu_vcn_get_profile (struct amdgpu_device *adev); +int amdgpu_vcn_put_profile (struct amdgpu_device *adev); +#endif
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +2 -25
@@ -116,7 +116,6 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work)struct amdgpu_device *adev = vcn_inst->adev;unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 };unsigned int i, j; -int r =0 ;for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *v = &adev->vcn.inst[i];
@@ -149,15 +148,7 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work)if (!fences && !atomic_read (&adev->vcn.inst[0 ].total_submission_cnt)) {amdgpu_device_ip_set_powergating_state (adev,AMD_IP_BLOCK_TYPE_VCN ,AMD_PG_STATE_GATE ); -mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); - adev->vcn.workload_profile_active = false; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); +amdgpu_vcn_put_profile (adev); }else {schedule_delayed_work (&adev->vcn.inst[0 ].idle_work,VCN_IDLE_TIMEOUT ); }
@@ -167,7 +158,6 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring) {struct amdgpu_device *adev = ring->adev;struct amdgpu_vcn_inst *v = &adev->vcn.inst[ring->me]; -int r =0 ;atomic_inc (&adev->vcn.inst[0 ].total_submission_cnt);
@@ -177,23 +167,10 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring)* the delayed work so there is no one else to set it to false * and we don't care if someone else sets it to true. */ -if (adev->vcn.workload_profile_active) - goto pg_lock; - -mutex_lock (&adev->vcn.workload_profile_mutex); -if (!adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - true); -if (r) -dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); - adev->vcn.workload_profile_active = true; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); - -pg_lock:mutex_lock (&adev->vcn.inst[0 ].vcn_pg_lock);amdgpu_device_ip_set_powergating_state (adev,AMD_IP_BLOCK_TYPE_VCN ,AMD_PG_STATE_UNGATE ); +amdgpu_vcn_get_profile (adev);/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */ if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
Signature · 1 line
2.49.0
Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
This is closer, but the thing is, IMO we shouldn't have to use 2 locks and go into nested locking, we can do with just one global lock.
Power_state of each instance, and global workload_profile_active are inter-related, they need to be guarded together,
nested could work , but why nested if single lock is enough ? nested complicates it.
Regards,
Sathish
Quoted 350 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Regards,
Sathish
Quoted 312 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence,
between jobs , can be 0.
- both of the above conditions do not mean video decode is
complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 227 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 219 lines
Regards,
Sathish
Quoted 215 lines
Alex
Quoted 213 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 197 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 190 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should have
used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 178 lines · David
Quoted 128 lines · Alex Deucher
Quoted 127 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 101 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workloadprofile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +12
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = + &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] || @@ -436,18 +442,18 @@static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based =VCN_DPG_STATE__UNPAUSE;drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3
-5- adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, + &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { + if (!fence[vcn_inst->inst] &&!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE); mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +2
-1-if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO, false); if (r) @@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) int r = 0; atomic_inc(&vcn_inst->total_submission_cnt);Quoted 1 line
+
atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7-/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 43 lines
Alex
Quoted 41 lines
David
Quoted 40 lines
if (!adev->vcn.workload_profile_active) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring) atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submissio n_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission_ c
nt);
+atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--gita/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
indexb3fb1d0e43fc9..febc3ce8641ff100644drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
[Public]
There is no need for nested lock. It only needs to follow the order
set instance power_state
set profile (this takes a global lock and hence instance power state will be visible to whichever thread that gets the work profile lock).
You are seeing nested lock just because I added the code just after power state setting.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)
Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
This is closer, but the thing is, IMO we shouldn't have to use 2 locks and go into nested locking, we can do with just one global lock.
Power_state of each instance, and global workload_profile_active are inter-related, they need to be guarded together,
nested could work , but why nested if single lock is enough ? nested complicates it.
Regards,
Sathish
Quoted 359 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Regards,
Sathish
Quoted 313 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted
fence, between jobs , can be 0.
- both of the above conditions do not mean video decode is
complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 228 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 220 lines
Regards,
Sathish
Quoted 216 lines
Alex
Quoted 214 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 198 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 191 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should have
used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 179 lines · David
Quoted 127 lines · Alex Deucher
Quoted 126 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 100 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workloadprofile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +12
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = + &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] || @@ -436,18 +442,18 @@static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based =VCN_DPG_STATE__UNPAUSE;drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3
-5- adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, + &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { + if (!fence[vcn_inst->inst] &&!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE); mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +2
-1-if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO, false); if (r) @@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) int r = 0; atomic_inc(&vcn_inst->total_submission_cnt);Quoted 1 line
+
atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7-/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 45 lines
Alex
Quoted 43 lines
David
Quoted 42 lines
if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev, PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring) atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submissi o
n_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission _
c
nt);
+atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--gita/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
indexb3fb1d0e43fc9..febc3ce8641ff100644drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 7 lines · Lijo
[Public]
There is no need for nested lock. It only needs to follow the order
set instancepower_state
set profile (this takes a global lock and hence instance power state will be visible to whichever thread that gets the work profile lock).You are seeing nested lock just because I added the code just after power state setting.
Pasting your code from the file for ref :
@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE);
+ amdgpu_vcn_get_profile(adev);
vcn_pg_lock isn't released here yet right ? And in-case you intend to only order the locks, then still there is an un-necessary OFF followed by ON, but yes that is acceptable,
May be you want to move that vcn_pg_lock after amdgpu_vcn_get_profile to protect concurrent dpg_state access in begin_use.
The concern is, this patch access power_state that is protected by some other mutex lock hoping it reflects right values also when holding powerprofile_lock.
Or
Have shared a patch with global workload_profile_mutex that simplifies this handling, and renamed pg_lock -> dpg_lock and used
that only for dpg_state changes per instance.
Regards,
Sathish
Quoted 384 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
This is closer, but the thing is, IMO we shouldn't have to use 2 locks and go into nested locking, we can do with just one global lock.
Power_stateof each instance, and globalworkload_profile_activeare inter-related, they need to be guarded together,nested could work , but why nested if single lock is enough ? nested complicates it.
Regards,
Sathish
Quoted 358 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Regards,
Sathish
Quoted 313 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted
fence, between jobs , can be 0.
- both of the above conditions do not mean video decode is
complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 228 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 220 lines
Regards,
Sathish
Quoted 216 lines
Alex
Quoted 214 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 198 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 191 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should have
used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 179 lines · David
Quoted 127 lines · Alex Deucher
Quoted 126 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 101 lines · Alex Deucher
If there are multiple instances of the VCN running, we may
end up switching the video profile while another instance
is active because we only take into account the current
instance's submissions. Look at all outstanding fences for
the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload
profile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20 deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct
amdgpu_vcn_inst, idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 << vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v =
+ &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4
and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] || @@ -436,18 +442,18 @@
static void amdgpu_vcn_idle_work_handler(struct work_struct
*work)
else
new_state.fw_based =
VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst,
+ &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, false);
if (r) @@ -467,16 +473,10 @@
void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);
- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-
mutex_lock(&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 45 lines
Alex
Quoted 43 lines
David
Quoted 42 lines
if (!adev->vcn.workload_profile_active) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void
amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submissi
o
n
_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission
_
c
nt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
[Public]
The request profile can be moved outside the pg_lock in begin_use as in the attached patch. It needs set power state -> set profile order.
This is the premise -
Let's say there are two threads, begin_use thread and idle_work threads. begin_use and idle_work will need the workprofile mutex to request a profile.
Case 1) Idle thread gets the lock first.
a) Idle thread sees vinst power state as PG_UNGATE, no harm done. It exits without requesting power profile change. begin_use thread gets the lock next, it sees profile as active and continues.
b) Idle thread sees vinst power state as PG_GATE, it will make workprofile_active to false and exit. Now when begin_use thread gets the mutex next, it's guaranteed to see the workprofile_active as false, hence it will request the profile.
Case 2) begin_use thread gets the lock first.
a) Workload profile is active, hence it doesn't do anything and exits. The change made by begin_use thread to vinst power state (state = on) will now be visible to idle thread which gets the lock next. It will do nothing and exit.
b) Workload profile is inactive, hence it requests a profile change. Again, the change made by begin_use thread to vinst power state will now be visible to idle thread which gets the lock next. It will do nothing and exit.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 6:18 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)
Quoted 7 lines · Lijo
[Public]
There is no need for nested lock. It only needs to follow the order
set instancepower_state
set profile (this takes a global lock and hence instance power state will be visible to whichever thread that gets the work profile lock).You are seeing nested lock just because I added the code just after power state setting.
Pasting your code from the file for ref :
@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE);+ amdgpu_vcn_get_profile(adev);
vcn_pg_lock isn't released here yet right ? And in-case you intend to only order the locks, then still there is an un-necessary OFF followed by ON, but yes that is acceptable,
May be you want to move that vcn_pg_lock after amdgpu_vcn_get_profile to protect concurrent dpg_state access in begin_use.
The concern is, this patch access power_state that is protected by some other mutex lock hoping it reflects right values also when holding powerprofile_lock.
Or
Have shared a patch with global workload_profile_mutex that simplifies this handling, and renamed pg_lock -> dpg_lock and used
that only for dpg_state changes per instance.
Regards,
Sathish
Quoted 389 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
This is closer, but the thing is, IMO we shouldn't have to use 2 locks and go into nested locking, we can do with just one global lock.
Power_stateof each instance, and globalworkload_profile_activeare inter-related, they need to be guarded together,nested could work , but why nested if single lock is enough ? nested complicates it.
Regards,
Sathish
Quoted 359 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Regards,
Sathish
Quoted 314 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition
(v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding,
inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total
submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted
fence, between jobs , can be 0.
- both of the above conditions do not mean video decode
is complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 228 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 220 lines
Regards,
Sathish
Quoted 216 lines
Alex
Quoted 214 lines
Whereas if instances are powered off we are sure idle time is
past and it is powered off, no possible way of active video
decode, when all instances are off we can safely assume no
active decode and global lock protects it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf,
but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock
for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 198 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 191 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should
have used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 179 lines · David
Quoted 130 lines · Alex Deucher
Quoted 129 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 104 lines · Alex Deucher
If there are multiple instances of the VCN running, we may
end up switching the video profile while another instance
is active because we only take into account the current
instance's submissions. Look at all outstanding fences
for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload
profile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20
deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct
amdgpu_vcn_inst, idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 <<
+ vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v =
+ &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4
and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] || @@ -436,18 +442,18 @@
static void amdgpu_vcn_idle_work_handler(struct
work_struct
*work)
else
new_state.fw_based =
VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst,
+ &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, false);
if (r) @@ -467,16 +473,10 @@
void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);
- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-Quoted 5 lines
Quoted 4 lines
Quoted 1 line
mutex_lock(&adev->vcn.workload_profile_mutex);
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 41 lines
Alex
Quoted 39 lines
David
Quoted 38 lines
if (!adev->vcn.workload_profile_active) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void
amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submiss
i
o
n
_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submissio
n
_
c
nt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
From a556a9802bfa3cb95c0a94a6771380d680c13e1c Mon Sep 17 00:00:00 2001
From: Lijo Lazar <[email protected]>
Date: Thu, 14 Aug 2025 13:52:50 +0530
Subject: [PATCH] drm/amdgpu: Check vcn state before profile switch
Before switching video profile, check power state of all VCN instances. Before submission, power state is changed and a check is done for vidoe profile. If already active, it skips switching; otherwise, it switches to video profile. When it gets to idle state, power state of the current instance is set to gated. After that a check is done if all instances are in gated state. If so, video profile is disabled.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 85 ++++++++++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 5 +- drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 27 +------- 3 files changed, 62 insertions(+), 55 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +56 -29
@@ -410,6 +410,59 @@ int amdgpu_vcn_resume(struct amdgpu_device *adev, int i)return 0 ; } +int amdgpu_vcn_get_profile (struct amdgpu_device *adev) +{ +int r; + + +mutex_lock (&adev->vcn.workload_profile_mutex); + +if (adev->vcn.workload_profile_active) { +mutex_unlock (&adev->vcn.workload_profile_mutex); +return 0 ; + } + r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , + true); +if (r) +dev_warn (adev->dev, +"(%d) failed to enable video power profile mode\n" , r); +else + adev->vcn.workload_profile_active = true; +mutex_unlock (&adev->vcn.workload_profile_mutex); + +return r; +} + +int amdgpu_vcn_put_profile (struct amdgpu_device *adev) +{ +bool pg = true; +int r, i; + +mutex_lock (&adev->vcn.workload_profile_mutex); +for (i =0 ; i < adev->vcn.num_vcn_inst; i++) { +if (adev->vcn.inst[i].cur_state !=AMD_PG_STATE_GATE ) { + pg = false; +break ; + } + } + +if (pg) { + r =amdgpu_dpm_switch_power_profile ( + adev,PP_SMC_POWER_PROFILE_VIDEO , false); +if (r) +dev_warn ( + adev->dev, +"(%d) failed to disable video power profile mode\n" , + r); +else + adev->vcn.workload_profile_active = false; + } + +mutex_unlock (&adev->vcn.workload_profile_mutex); + +return r; +} +static void amdgpu_vcn_idle_work_handler (struct work_struct *work) { struct amdgpu_vcn_inst *vcn_inst =
@@ -417,7 +470,6 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)struct amdgpu_device *adev = vcn_inst->adev;unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 };unsigned int i = vcn_inst->inst, j; -int r =0 ;if (adev->vcn.harvest_config & (1 << i))return ;
@@ -446,15 +498,8 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock); -mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); - adev->vcn.workload_profile_active = false; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); +amdgpu_vcn_put_profile (adev); + }else {schedule_delayed_work (&vcn_inst->idle_work,VCN_IDLE_TIMEOUT ); }
@@ -464,30 +509,11 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) {struct amdgpu_device *adev = ring->adev;struct amdgpu_vcn_inst *vcn_inst = &adev->vcn.inst[ring->me]; -int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt);cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; - -mutex_lock (&adev->vcn.workload_profile_mutex); -if (!adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - true); -if (r) -dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); - adev->vcn.workload_profile_active = true; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); - -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );
@@ -515,6 +541,7 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) vcn_inst->pause_dpg_mode (vcn_inst, &new_state); }mutex_unlock (&vcn_inst->vcn_pg_lock); +amdgpu_vcn_get_profile (adev); }void amdgpu_vcn_ring_end_use (struct amdgpu_ring *ring)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +4 -1
@@ -362,7 +362,7 @@ struct amdgpu_vcn {bool per_inst_fw;unsigned fw_version; -bool workload_profile_active; +bool workload_profile_active;struct mutex workload_profile_mutex;u32 reg_count;const struct amdgpu_hwip_reg_entry *reg_list;
@@ -565,4 +565,7 @@ int amdgpu_vcn_reg_dump_init(struct amdgpu_device *adev, const struct amdgpu_hwip_reg_entry *reg, u32 count);void amdgpu_vcn_dump_ip_state (struct amdgpu_ip_block *ip_block);void amdgpu_vcn_print_ip_state (struct amdgpu_ip_block *ip_block,struct drm_printer *p); +int amdgpu_vcn_get_profile (struct amdgpu_device *adev); +int amdgpu_vcn_put_profile (struct amdgpu_device *adev); +#endif
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +2 -25
@@ -116,7 +116,6 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work)struct amdgpu_device *adev = vcn_inst->adev;unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 };unsigned int i, j; -int r =0 ;for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) {struct amdgpu_vcn_inst *v = &adev->vcn.inst[i];
@@ -149,15 +148,7 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work)if (!fences && !atomic_read (&adev->vcn.inst[0 ].total_submission_cnt)) {amdgpu_device_ip_set_powergating_state (adev,AMD_IP_BLOCK_TYPE_VCN ,AMD_PG_STATE_GATE ); -mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); - adev->vcn.workload_profile_active = false; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); +amdgpu_vcn_put_profile (adev); }else {schedule_delayed_work (&adev->vcn.inst[0 ].idle_work,VCN_IDLE_TIMEOUT ); }
@@ -167,7 +158,6 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring) {struct amdgpu_device *adev = ring->adev;struct amdgpu_vcn_inst *v = &adev->vcn.inst[ring->me]; -int r =0 ;atomic_inc (&adev->vcn.inst[0 ].total_submission_cnt);
@@ -177,20 +167,6 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring)* the delayed work so there is no one else to set it to false * and we don't care if someone else sets it to true. */ -if (adev->vcn.workload_profile_active) - goto pg_lock; - -mutex_lock (&adev->vcn.workload_profile_mutex); -if (!adev->vcn.workload_profile_active) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - true); -if (r) -dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); - adev->vcn.workload_profile_active = true; - } -mutex_unlock (&adev->vcn.workload_profile_mutex); - -pg_lock:mutex_lock (&adev->vcn.inst[0 ].vcn_pg_lock);amdgpu_device_ip_set_powergating_state (adev,AMD_IP_BLOCK_TYPE_VCN ,AMD_PG_STATE_UNGATE );
@@ -218,6 +194,7 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring) v->pause_dpg_mode (v, &new_state); }mutex_unlock (&adev->vcn.inst[0 ].vcn_pg_lock); +amdgpu_vcn_get_profile (adev); }static void vcn_v2_5_ring_end_use (struct amdgpu_ring *ring)
Signature · 1 line
2.49.0
amdgpu_vcn_idle_work_handler():
if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) { ----------- could it be possible a context switch here to amdgpu_vcn_ring_begin_use()? if it could then AMD_PG_STATE_GATE will be set by mistake.
David
Quoted 457 lines · Lijo
[Public]
The request profile can be moved outside the
pg_lockinbegin_useas in the attached patch. It needs set power state -> set profile order.This is the premise -
Let's say there are two threads,
begin_usethread andidle_workthreads.begin_useandidle_workwill need the workprofile mutex to request a profile.Case 1) Idle thread gets the lock first.
a) Idle thread sees vinst power state asPG_UNGATE, no harm done. It exits without requesting power profile change.begin_usethread gets the lock next, it sees profile as active and continues.
b) Idle thread sees vinst power state asPG_GATE, it will makeworkprofile_activeto false and exit. Now whenbegin_usethread gets the mutex next, it's guaranteed to see theworkprofile_activeas false, hence it will request the profile.Case 2)
begin_usethread gets the lock first.
a) Workload profile is active, hence it doesn't do anything and exits. The change made bybegin_usethread to vinst power state (state = on) will now be visible to idle thread which gets the lock next. It will do nothing and exit.
b) Workload profile is inactive, hence it requests a profile change. Again, the change made bybegin_usethread to vinst power state will now be visible to idle thread which gets the lock next. It will do nothing and exit.Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 6:18 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 7 lines · Lijo
[Public]
There is no need for nested lock. It only needs to follow the order
set instancepower_state
set profile (this takes a global lock and hence instance power state will be visible to whichever thread that gets the work profile lock).You are seeing nested lock just because I added the code just after power state setting.
Pasting your code from the file for ref :
(quoted diff) +1
-1@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) -pg_lock: mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE); + amdgpu_vcn_get_profile(adev); vcn_pg_lock isn't released here yet right ? And in-case you intend to only order the locks, then still there is an un-necessary OFF followed by ON, but yes that is acceptable,May be you want to move that
vcn_pg_lockafteramdgpu_vcn_get_profileto protect concurrentdpg_stateaccess inbegin_use.The concern is, this patch access
power_statethat is protected by some other mutex lock hoping it reflects right values also when holdingpowerprofile_lock.Or
Have shared a patch with global
workload_profile_mutexthat simplifies this handling, and renamedpg_lock->dpg_lockand usedthat only for
dpg_statechanges per instance.Regards,
Sathish
Quoted 394 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
This is closer, but the thing is, IMO we shouldn't have to use 2 locks and go into nested locking, we can do with just one global lock.
Power_stateof each instance, and globalworkload_profile_activeare inter-related, they need to be guarded together,nested could work , but why nested if single lock is enough ? nested complicates it.
Regards,
Sathish
Quoted 365 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Regards,
Sathish
Quoted 320 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition
(v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding,
inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total
submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted
fence, between jobs , can be 0.
- both of the above conditions do not mean video decode
is complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 234 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 226 lines
Regards,
Sathish
Quoted 222 lines
Alex
Quoted 220 lines
Whereas if instances are powered off we are sure idle time is
past and it is powered off, no possible way of active video
decode, when all instances are off we can safely assume no
active decode and global lock protects it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf,
but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock
for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 204 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 197 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should
have used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 185 lines · David
Quoted 130 lines · Alex Deucher
Quoted 129 lines · David
Hi Alex,
The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the begin_use().
The suggestion of moving it inside the lock (which I prefer
in case someone adds more before the lock and not reviewed
thoroughly)
- up to you to decide.Reviewed-by: David (Ming Qiang) Wu <[email protected]>
Thanks,
David
On 8/13/2025 9:45 AM, Alex Deucher wrote:
If there are multiple instances of the VCN running, we may
end up switching the video profile while another instance
is active because we only take into account the current
instance's submissions. Look at all outstanding fences
for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload
profile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20
deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct
amdgpu_vcn_inst, idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 <<
+ vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v =
+ &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4
and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] || @@ -436,18 +442,18 @@
static void amdgpu_vcn_idle_work_handler(struct
work_struct
*work)
else
new_state.fw_based =
VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst,
+ &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, false);
if (r) @@ -467,16 +473,10 @@
void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
cancel_delayed_work_sync(&vcn_inst->idle_work);- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-Quoted 5 lines
Quoted 4 lines
mutex_lock(&adev->vcn.workload_profile_mutex);
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 47 lines
Alex
Quoted 45 lines
David
if (!adev->vcn.workload_profile_active) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void
amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submiss
i
o
n
_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submissio
n
_
c
nt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
[AMD Official Use Only - AMD Internal Distribution Only]
I didn't fully understand the question.
For the same instance, begin_thread will set the power state only after cancelling any idle worker for the instance. If idle worker is already under progress, then it needs to complete as that's a cancel_sync (it's the existing logic).
Basically, by the time begin_thread sets the PG state, no idle worker for the same vcn instance would be active. If it's about context switch to another vcn instance's begin_thread, I think that won't be a problem.
Thanks,
Lijo
________________________________
From: Wu, David <[email protected]>
Sent: Thursday, August 14, 2025 11:14:26 PM
To: Lazar, Lijo <[email protected]>; Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)
amdgpu_vcn_idle_work_handler():
if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {----------- could it be possible a context switch here to amdgpu_vcn_ring_begin_use()?
if it could then AMD_PG_STATE_GATE will be set by mistake.
David
Quoted 457 lines · Lijo
[Public]
The request profile can be moved outside the
pg_lockinbegin_useas in the attached patch. It needs set power state -> set profile order.This is the premise -
Let's say there are two threads,
begin_usethread andidle_workthreads.begin_useandidle_workwill need the workprofile mutex to request a profile.Case 1) Idle thread gets the lock first.
a) Idle thread sees vinst power state asPG_UNGATE, no harm done. It exits without requesting power profile change.begin_usethread gets the lock next, it sees profile as active and continues.
b) Idle thread sees vinst power state asPG_GATE, it will makeworkprofile_activeto false and exit. Now whenbegin_usethread gets the mutex next, it's guaranteed to see theworkprofile_activeas false, hence it will request the profile.Case 2)
begin_usethread gets the lock first.
a) Workload profile is active, hence it doesn't do anything and exits. The change made bybegin_usethread to vinst power state (state = on) will now be visible to idle thread which gets the lock next. It will do nothing and exit.
b) Workload profile is inactive, hence it requests a profile change. Again, the change made bybegin_usethread to vinst power state will now be visible to idle thread which gets the lock next. It will do nothing and exit.Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 6:18 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 7 lines · Lijo
[Public]
There is no need for nested lock. It only needs to follow the order
set instancepower_state
set profile (this takes a global lock and hence instance power state will be visible to whichever thread that gets the work profile lock).You are seeing nested lock just because I added the code just after power state setting.
Pasting your code from the file for ref :
(quoted diff) +1
-1@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) -pg_lock: mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE); + amdgpu_vcn_get_profile(adev); vcn_pg_lock isn't released here yet right ? And in-case you intend to only order the locks, then still there is an un-necessary OFF followed by ON, but yes that is acceptable,May be you want to move that
vcn_pg_lockafteramdgpu_vcn_get_profileto protect concurrentdpg_stateaccess inbegin_use.The concern is, this patch access
power_statethat is protected by some other mutex lock hoping it reflects right values also when holdingpowerprofile_lock.Or
Have shared a patch with global
workload_profile_mutexthat simplifies this handling, and renamedpg_lock->dpg_lockand usedthat only for
dpg_statechanges per instance.Regards,
Sathish
Quoted 394 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
This is closer, but the thing is, IMO we shouldn't have to use 2 locks and go into nested locking, we can do with just one global lock.
Power_stateof each instance, and globalworkload_profile_activeare inter-related, they need to be guarded together,nested could work , but why nested if single lock is enough ? nested complicates it.
Regards,
Sathish
Quoted 365 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Regards,
Sathish
Quoted 320 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition
(v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding,
inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total
submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted
fence, between jobs , can be 0.
- both of the above conditions do not mean video decode
is complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 234 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 226 lines
Regards,
Sathish
Quoted 222 lines
Alex
Quoted 220 lines
Whereas if instances are powered off we are sure idle time is
past and it is powered off, no possible way of active video
decode, when all instances are off we can safely assume no
active decode and global lock protects it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf,
but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock
for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 204 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 197 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should
have used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 185 lines · David
Quoted 130 lines · Alex Deucher
Quoted 129 lines · David
Hi Alex,
The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the begin_use().
The suggestion of moving it inside the lock (which I prefer
in case someone adds more before the lock and not reviewed
thoroughly)
- up to you to decide.Reviewed-by: David (Ming Qiang) Wu <[email protected]>
Thanks,
David
On 8/13/2025 9:45 AM, Alex Deucher wrote:
If there are multiple instances of the VCN running, we may
end up switching the video profile while another instance
is active because we only take into account the current
instance's submissions. Look at all outstanding fences
for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload
profile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20
deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct
amdgpu_vcn_inst, idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 <<
+ vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v =
+ &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4
and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] || @@ -436,18 +442,18 @@
static void amdgpu_vcn_idle_work_handler(struct
work_struct
*work)
else
new_state.fw_based =
VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst,
+ &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, false);
if (r) @@ -467,16 +473,10 @@
void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
cancel_delayed_work_sync(&vcn_inst->idle_work);- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-Quoted 5 lines
Quoted 4 lines
mutex_lock(&adev->vcn.workload_profile_mutex);
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 47 lines
Alex
Quoted 45 lines
David
if (!adev->vcn.workload_profile_active) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void
amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submiss
i
o
n
_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submissio
n
_
c
nt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
hmm.. it is my concern for the same instance. but I got it now. Your patch is good. Thanks,
David
Quoted 525 lines · Lijo
[AMD Official Use Only - AMD Internal Distribution Only]
I didn't fully understand the question.
For the same instance,
begin_threadwill set the power state only after cancelling any idle worker for the instance. If idle worker is already under progress, then it needs to complete as that's acancel_sync(it's the existing logic).Basically, by the time
begin_threadsets the PG state, no idle worker for the same vcn instance would be active. If it's about context switch to another vcn instance'sbegin_thread, I think that won't be a problem.Thanks,
Lijo
------------------------------------------------------------------------
*From:* Wu, David <[email protected]>
*Sent:* Thursday, August 14, 2025 11:14:26 PM
*To:* Lazar, Lijo <[email protected]>; Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]> *Cc:* Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected] <[email protected]> *Subject:* Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)amdgpu_vcn_idle_work_handler():
if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
----------- could it be possible a context switch here toamdgpu_vcn_ring_begin_use()?
if it could thenAMD_PG_STATE_GATEwill be set by mistake.David
Quoted 3 lines · Lijo
[Public]
The request profile can be moved outside the
pg_lockinbegin_useasin the attached patch. It needs set power state -> set profile order.
Quoted 4 lines
This is the premise -
Let's say there are two threads,
begin_usethread andidle_workthreads.
begin_useandidle_workwill need the workprofile mutex to request a profile.Quoted 3 lines
Case 1) Idle thread gets the lock first.
a) Idle thread sees vinst power state asPG_UNGATE, no harmdone. It exits without requesting power profile change.
begin_usethread gets the lock next, it sees profile as active and continues.Quoted 1 line
b) Idle thread sees vinst power state as
PG_GATE, it willmake
workprofile_activeto false and exit. Now whenbegin_usethread gets the mutex next, it's guaranteed to see theworkprofile_activeas false, hence it will request the profile.Quoted 3 lines
Case 2)
begin_usethread gets the lock first.
a) Workload profile is active, hence it doesn't do anythingand exits. The change made by
begin_usethread to vinst power state (state = on) will now be visible to idle thread which gets the lock next. It will do nothing and exit.Quoted 1 line
b) Workload profile is inactive, hence it requests a
profile change. Again, the change made by
begin_usethread to vinst power state will now be visible to idle thread which gets the lock next. It will do nothing and exit.Quoted 8 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 6:18 PM
To: Lazar, Lijo <[email protected]>; Alex DeucherQuoted 1 line
Cc Wu, David <[email protected]>; Deucher, Alexander<[email protected]>; [email protected]
Quoted 1 line
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race
condition (v3)
Quoted 8 lines · Lijo
Quoted 5 lines
[Public]
There is no need for nested lock. It only needs to follow the order set instance
power_state
set profile (this takes a global lock and hence instancepower state will be visible to whichever thread that gets the work profile lock).
Quoted 2 lines
Quoted 2 lines
You are seeing nested lock just because I added the code just after
power state setting.
Quoted 12 lines
Pasting your code from the file for ref :
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) -pg_lock: mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE); + amdgpu_vcn_get_profile(adev);Quoted 1 line
vcn_pg_lockisn't released here yet right ? And in-case you intendto only order the locks, then still there is an un-necessary OFF followed by ON, but yes that is acceptable,
Quoted 2 lines
May be you want to move that
vcn_pg_lockafter
amdgpu_vcn_get_profileto protect concurrentdpg_stateaccess inbegin_use.Quoted 2 lines
The concern is, this patch access
power_statethat is protected bysome other mutex lock hoping it reflects right values also when holding
powerprofile_lock.Quoted 4 lines
Or
Have shared a patch with global
workload_profile_mutexthatsimplifies this handling, and renamed
pg_lock-> dpg_lock and usedQuoted 25 lines
that only for
dpg_statechanges per instance.Regards,
Sathish
Quoted 18 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the
concern more about having to take the lock for every begin?
Quoted 1 line
Quoted 1 line
This is closer, but the thing is, IMO we shouldn't have to use 2
locks and go into nested locking, we can do with just one global lock.
Quoted 5 lines
Quoted 5 lines
Power_stateof each instance, and globalworkload_profile_activeare inter-related, they need to be guarded together,nested could work , but why nested if single lock is enough ?
nested complicates it.
Quoted 19 lines
Quoted 19 lines
Regards,
Sathish
Quoted 14 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
[Public]
That is not required I think. The power profile is set by an
instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked.
What about something like attached?
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Quoted 39 lines
Quoted 39 lines
Quoted 39 lines
Regards,
Sathish
Quoted 34 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition
(v3)Quoted 19 lines · Alex Deucher
Quoted 17 lines · Sathishkumar
Quoted 16 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 13 lines · Alex Deucher
Quoted 11 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Can you explain how this can happen? I'm not seeing it.
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts. inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's
between the jobs,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
- as
begin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between jobs , can be 0.
- both of the above conditions do not mean video decode is complete on inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) wouldbe non-0.
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 10 lines
I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile until end_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before currentjob fence signals ?
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job
fence signals.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 3 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences allinstances.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.
Alex
Quoted 2 lines
Regards,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Sathish
Quoted 6 lines
Alex
Quoted 4 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance.Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.
Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the
handlers as such.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Regards,
SathishQuoted 3 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per
instance.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
See the attached patches.
Alex
Quoted 3 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the
situation David is trying to bring out.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid thissituation.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Regards,
Sathish
Quoted 2 lines · David
Quoted 1 line · Alex Deucher
On Wed, Aug 13, 2025 at 12:39 PM Wu, David
Quoted 4 lines · <[email protected]> wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Hi Alex,
The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the
begin_use().Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
The suggestion of moving it inside the lock (which I prefer
in case someone adds more before the lock and not reviewed
thoroughly)
- up to you to decide.Reviewed-by: David (Ming Qiang) Wu <[email protected]>
Thanks,
David
On 8/13/2025 9:45 AM, Alex Deucher wrote:
If there are multiple instances of the VCN running, we may
end up switching the video profile while another instance
is active because we only take into account the current
instance's submissions. Look at all outstanding fences
for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload
profile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20
deletions(-)Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.cQuoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct
amdgpu_vcn_inst, idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0,fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 <<
+ vcn_inst->inst))
return;Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v =
+ &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_dec);Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4
and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] || @@ -436,18 +442,18 @@
static void amdgpu_vcn_idle_work_handler(struct
work_struct
*work)
else
new_state.fw_based =
VCN_DPG_STATE__UNPAUSE;Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst,
+ &new_state);
}- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_dec);Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
- fences += fence[i];
-
- if (!fences &&!atomic_read(&vcn_inst->total_submission_cnt)) {
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCNinstances */
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, false);
if (r) @@ -467,16 +473,10 @@
void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
int r = 0;Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
cancel_delayed_work_sync(&vcn_inst->idle_work);- /* We can safely return early here because we've
cancelled the
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
- * the delayed work so there is no one else to
set it to false
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 38 lines
Quoted 11 lines
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-mutex_lock(&adev->vcn.workload_profile_mutex);
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.
Why does this need to be protected by the mutex?hmm.. OK - no need and it is actually better before the mutex. David
Quoted 25 lines
Alex
David
if (!adev->vcn.workload_profile_active) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void
amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submiss
i
o
n
_cnt);Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
atomic_dec(&ring->adev->vcn.inst[ring->me].total_submissio
n
_
c
nt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
VCN_IDLE_TIMEOUT); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
Hi Lijo and Alex,
I prefer Lijo's method as it should not cause unexpected power profile change for the active session. Liji, could you make some adjustments for your patch such as removing extra blank line and function declaration such as using "void" instead of "int" for the new functions (should they be static?)
Thanks,
David
Quoted 522 lines · David Wu
hmm.. it is my concern for the same instance. but I got it now. Your patch is good. Thanks,
DavidQuoted 518 lines · Lijo
[AMD Official Use Only - AMD Internal Distribution Only]
I didn't fully understand the question.
For the same instance,
begin_threadwill set the power state only after cancelling any idle worker for the instance. If idle worker is already under progress, then it needs to complete as that's acancel_sync(it's the existing logic).Basically, by the time
begin_threadsets the PG state, no idle worker for the same vcn instance would be active. If it's about context switch to another vcn instance'sbegin_thread, I think that won't be a problem.Thanks,
Lijo
------------------------------------------------------------------------
*From:* Wu, David <[email protected]>
*Sent:* Thursday, August 14, 2025 11:14:26 PM
*To:* Lazar, Lijo <[email protected]>; Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]> *Cc:* Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected] <[email protected]> *Subject:* Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)amdgpu_vcn_idle_work_handler():
if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
----------- could it be possible a context switch here toamdgpu_vcn_ring_begin_use()?
if it could thenAMD_PG_STATE_GATEwill be set by mistake.David
Quoted 3 lines · Lijo
[Public]
The request profile can be moved outside the
pg_lockinbegin_useas in the attached patch. It needs set power state -> set profile order.
Quoted 4 lines
This is the premise -
Let's say there are two threads,
begin_usethread andidle_workthreads.
begin_useandidle_workwill need the workprofile mutex to request a profile.Quoted 3 lines
Case 1) Idle thread gets the lock first.
a) Idle thread sees vinst power state asPG_UNGATE, noharm done. It exits without requesting power profile change.
begin_usethread gets the lock next, it sees profile as active and continues.Quoted 1 line
b) Idle thread sees vinst power state as
PG_GATE, it willmake
workprofile_activeto false and exit. Now whenbegin_usethread gets the mutex next, it's guaranteed to see theworkprofile_activeas false, hence it will request the profile.Quoted 3 lines
Case 2)
begin_usethread gets the lock first.
a) Workload profile is active, hence it doesn't doanything and exits. The change made by
begin_usethread to vinst power state (state = on) will now be visible to idle thread which gets the lock next. It will do nothing and exit.Quoted 1 line
b) Workload profile is inactive, hence it requests a
profile change. Again, the change made by
begin_usethread to vinst power state will now be visible to idle thread which gets the lock next. It will do nothing and exit.Quoted 8 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 6:18 PM
To: Lazar, Lijo <[email protected]>; Alex DeucherQuoted 1 line
Cc Wu, David <[email protected]>; Deucher, Alexander<[email protected]>; [email protected]
Quoted 1 line
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race
condition (v3)
Quoted 8 lines · Lijo
Quoted 5 lines
[Public]
There is no need for nested lock. It only needs to follow the order set instance
power_state
set profile (this takes a global lock and hence instancepower state will be visible to whichever thread that gets the work profile lock).
Quoted 2 lines
Quoted 2 lines
You are seeing nested lock just because I added the code just
after power state setting.
Quoted 12 lines
Pasting your code from the file for ref :
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) -pg_lock: mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE); + amdgpu_vcn_get_profile(adev);Quoted 1 line
vcn_pg_lockisn't released here yet right ? And in-case you intendto only order the locks, then still there is an un-necessary OFF followed by ON, but yes that is acceptable,
Quoted 2 lines
May be you want to move that
vcn_pg_lockafter
amdgpu_vcn_get_profileto protect concurrentdpg_stateaccess inbegin_use.Quoted 2 lines
The concern is, this patch access
power_statethat is protected bysome other mutex lock hoping it reflects right values also when holding
powerprofile_lock.Quoted 4 lines
Or
Have shared a patch with global
workload_profile_mutexthatsimplifies this handling, and renamed
pg_lock-> dpg_lock and usedQuoted 25 lines
that only for
dpg_statechanges per instance.Regards,
Sathish
Quoted 18 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the
concern more about having to take the lock for every begin?
Quoted 1 line
Quoted 1 line
This is closer, but the thing is, IMO we shouldn't have to use 2
locks and go into nested locking, we can do with just one global lock.
Quoted 5 lines
Quoted 5 lines
Power_stateof each instance, and globalworkload_profile_activeare inter-related, they need to be guarded together,nested could work , but why nested if single lock is enough ?
nested complicates it.
Quoted 19 lines
Quoted 19 lines
Regards,
Sathish
Quoted 14 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
[Public]
That is not required I think. The power profile is set by an
instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked.
What about something like attached?
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Quoted 39 lines
Quoted 39 lines
Quoted 39 lines
Regards,
Sathish
Quoted 34 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition
(v3)Quoted 19 lines · Alex Deucher
Quoted 17 lines · Sathishkumar
Quoted 16 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 13 lines · Alex Deucher
Quoted 11 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Can you explain how this can happen? I'm not seeing it.
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts. inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's
between the jobs,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
- as
begin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between jobs , can be 0.
- both of the above conditions do not mean video decode is complete on inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) wouldbe non-0.
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 10 lines
I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile until end_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before currentjob fence signals ?
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job
fence signals.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 3 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences allinstances.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.
Alex
Quoted 2 lines
Regards,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Sathish
Quoted 6 lines
Alex
Quoted 4 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance.Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.
Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the
handlers as such.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Regards,
SathishQuoted 3 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per
instance.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
See the attached patches.
Alex
Quoted 3 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the
situation David is trying to bring out.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoidthis situation.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Regards,
Sathish
Quoted 2 lines · David
On 8/13/2025 12:51 PM, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 12:39 PM Wu, DavidQuoted 4 lines · <[email protected]> wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Hi Alex,
The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the
begin_use().Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
Quoted 125 lines
The suggestion of moving it inside the lock (which I prefer
in case someone adds more before the lock and not reviewed
thoroughly)
- up to you to decide.Reviewed-by: David (Ming Qiang) Wu <[email protected]>
Thanks,
David
On 8/13/2025 9:45 AM, Alex Deucher wrote:
If there are multiple instances of the VCN running, we may
end up switching the video profile while another instance
is active because we only take into account the current
instance's submissions. Look at all outstanding fences
for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload
profile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20
deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct
amdgpu_vcn_inst, idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 <<
+ vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v =
+ &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4
and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] || @@ -436,18 +442,18 @@
static void amdgpu_vcn_idle_work_handler(struct
work_struct
*work)
else
new_state.fw_based =
VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst,
+ &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, false);
if (r) @@ -467,16 +473,10 @@
void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
cancel_delayed_work_sync(&vcn_inst->idle_work);- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
mutex_lock(&adev->vcn.workload_profile_mutex);
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.
Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before themutex.
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
Quoted 48 lines
David
AlexDavid
if (!adev->vcn.workload_profile_active) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void
amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submiss
i
o
n
_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submissio
n
_
c
nt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
Quoted 6 lines · David Wu
Hi Lijo and Alex,
I prefer Lijo's method as it should not cause unexpected power profile change for the active session. Liji, could you make some adjustments for your patch such as removing extra blank line and function declaration such as using "void" instead of "int" for the new functions (should they be static?)
I still don't understand how you can get a case where the profile would change for the active session with my patch. That said, I think Lijo's patch is nicer because it avoids the extra atomic.
Alex
Quoted 496 lines
Thanks,
David
On 2025-08-14 14:42, David Wu wrote:
hmm.. it is my concern for the same instance. but I got it now. Your patch is good. Thanks,
David
On 2025-08-14 14:06, Lazar, Lijo wrote:[AMD Official Use Only - AMD Internal Distribution Only]
I didn't fully understand the question.
For the same instance,
begin_threadwill set the power state only after cancelling any idle worker for the instance. If idle worker is already under progress, then it needs to complete as that's acancel_sync(it's the existing logic).Basically, by the time
begin_threadsets the PG state, no idle worker for the same vcn instance would be active. If it's about context switch to another vcn instance'sbegin_thread, I think that won't be a problem.Thanks,
Lijo
________________________________
From: Wu, David <[email protected]>
Sent: Thursday, August 14, 2025 11:14:26 PM
To: Lazar, Lijo <[email protected]>; Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)
amdgpu_vcn_idle_work_handler():if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {----------- could it be possible a context switch here to
amdgpu_vcn_ring_begin_use()?
if it could thenAMD_PG_STATE_GATEwill be set by mistake.David
Quoted 457 lines · Lijo
[Public]
The request profile can be moved outside the
pg_lockinbegin_useas in the attached patch. It needs set power state -> set profile order.This is the premise -
Let's say there are two threads,
begin_usethread andidle_workthreads.begin_useandidle_workwill need the workprofile mutex to request a profile.Case 1) Idle thread gets the lock first.
a) Idle thread sees vinst power state asPG_UNGATE, no harm done. It exits without requesting power profile change.begin_usethread gets the lock next, it sees profile as active and continues.
b) Idle thread sees vinst power state asPG_GATE, it will makeworkprofile_activeto false and exit. Now whenbegin_usethread gets the mutex next, it's guaranteed to see theworkprofile_activeas false, hence it will request the profile.Case 2)
begin_usethread gets the lock first.
a) Workload profile is active, hence it doesn't do anything and exits. The change made bybegin_usethread to vinst power state (state = on) will now be visible to idle thread which gets the lock next. It will do nothing and exit.
b) Workload profile is inactive, hence it requests a profile change. Again, the change made bybegin_usethread to vinst power state will now be visible to idle thread which gets the lock next. It will do nothing and exit.Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 6:18 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 7 lines · Lijo
[Public]
There is no need for nested lock. It only needs to follow the order
set instancepower_state
set profile (this takes a global lock and hence instance power state will be visible to whichever thread that gets the work profile lock).You are seeing nested lock just because I added the code just after power state setting.
Pasting your code from the file for ref :
(quoted diff) +1
-1@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) -pg_lock: mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE); + amdgpu_vcn_get_profile(adev); vcn_pg_lock isn't released here yet right ? And in-case you intend to only order the locks, then still there is an un-necessary OFF followed by ON, but yes that is acceptable,May be you want to move that
vcn_pg_lockafteramdgpu_vcn_get_profileto protect concurrentdpg_stateaccess inbegin_use.The concern is, this patch access
power_statethat is protected by some other mutex lock hoping it reflects right values also when holdingpowerprofile_lock.Or
Have shared a patch with global
workload_profile_mutexthat simplifies this handling, and renamedpg_lock->dpg_lockand usedthat only for
dpg_statechanges per instance.Regards,
Sathish
Quoted 394 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
This is closer, but the thing is, IMO we shouldn't have to use 2 locks and go into nested locking, we can do with just one global lock.
Power_stateof each instance, and globalworkload_profile_activeare inter-related, they need to be guarded together,nested could work , but why nested if single lock is enough ? nested complicates it.
Regards,
Sathish
Quoted 365 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Regards,
Sathish
Quoted 320 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition
(v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding,
inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total
submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted
fence, between jobs , can be 0.
- both of the above conditions do not mean video decode
is complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 234 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 226 lines
Regards,
Sathish
Quoted 222 lines
Alex
Quoted 220 lines
Whereas if instances are powered off we are sure idle time is
past and it is powered off, no possible way of active video
decode, when all instances are off we can safely assume no
active decode and global lock protects it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf,
but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock
for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 204 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 197 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should
have used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 185 lines · David
Quoted 135 lines · Alex Deucher
On Wed, Aug 13, 2025 at 12:39 PM Wu, David <[email protected]> wrote:
Hi Alex,The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the begin_use().
The suggestion of moving it inside the lock (which I prefer
in case someone adds more before the lock and not reviewed
thoroughly)
- up to you to decide.Reviewed-by: David (Ming Qiang) Wu <[email protected]>
Thanks,
David
On 8/13/2025 9:45 AM, Alex Deucher wrote:
If there are multiple instances of the VCN running, we may
end up switching the video profile while another instance
is active because we only take into account the current
instance's submissions. Look at all outstanding fences
for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload
profile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20
deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct
amdgpu_vcn_inst, idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 <<
+ vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v =
+ &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4
and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] || @@ -436,18 +442,18 @@
static void amdgpu_vcn_idle_work_handler(struct
work_struct
*work)
else
new_state.fw_based =
VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst,
+ &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, false);
if (r) @@ -467,16 +473,10 @@
void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
cancel_delayed_work_sync(&vcn_inst->idle_work);- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-mutex_lock(&adev->vcn.workload_profile_mutex);
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.
Why does this need to be protected by the mutex?hmm.. OK - no need and it is actually better before the mutex. David
Quoted 47 lines
Alex
David
if (!adev->vcn.workload_profile_active) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void
amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submiss
i
o
n
_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submissio
n
_
c
nt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
Sundararaju, Sathishkumar
Reviewed-byHi Lijo,
Quoted 26 lines · Sathishkumar
Quoted 7 lines · Lijo
[Public]
There is no need for nested lock. It only needs to follow the order
set instancepower_state
set profile (this takes a global lock and hence instance power state will be visible to whichever thread that gets the work profile lock).You are seeing nested lock just because I added the code just after power state setting.
Pasting your code from the file for ref :
(quoted diff) +1
-1@@ -464,32 +509,14 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) -pg_lock: mutex_lock(&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE); + amdgpu_vcn_get_profile(adev); vcn_pg_lock isn't released here yet right ? And in-case you intend to only order the locks, then still there is an un-necessary OFF followed by ON, but yes that is acceptable,May be you want to move that
vcn_pg_lockafteramdgpu_vcn_get_profileto protect concurrentdpg_stateaccess inbegin_use.
Quoted 1 line
The concern is, this patch access
power_statethat is protected by some other mutex lock hoping it reflects right values also when holdingpowerprofile_lock.
Thanks for taking time to explain this personally to me , I get it now, your patch should work fine. This above is a wrong hypothetical issue I imagined.
amdgpu_vcn_get_profile() and amdgpu_vcn_put_profile can be void return type.
And please remove the typo to avoid nested locking.
With above changes, your patch is :-
And there is other thread which also has Alex's (v3) fix for this issue, leave it to Alex/yourself and other review participants to decide for the best approach, I am inclined with your approach, as it is centered around pg_state.
Regards,
Sathish
Quoted 403 lines
Or
Have shared a patch with global
workload_profile_mutexthat simplifies this handling, and renamedpg_lock-> dpg_lock and usedthat only for
dpg_statechanges per instance.Regards,
Sathish
Quoted 392 lines
Thanks,
Lijo-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 5:23 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 3 lines · Lijo
[Public]
I see your point now. Attached should work, I guess. Is the concern more about having to take the lock for every begin?
This is closer, but the thing is, IMO we shouldn't have to use 2 locks and go into nested locking, we can do with just one global lock.
Power_stateof each instance, and globalworkload_profile_activeare inter-related, they need to be guarded together,nested could work , but why nested if single lock is enough ? nested complicates it.
Regards,
Sathish
Quoted 366 lines
Thanks,
Lijo-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Lazar, Lijo
Sent: Thursday, August 14, 2025 2:55 PM
To: Sundararaju, Sathishkumar <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)[Public]
That is not required I think. The power profile is set by an instance *after* setting itself to power on. Also, it's switched back after changing its power state to off. If idle worker is run by another instance, it won't be seeing the inst0 as power gated and won't change power profile.
Thanks,
Lijo
-----Original Message-----
From: Sundararaju, Sathishkumar <[email protected]>
Sent: Thursday, August 14, 2025 2:41 PM
To: Lazar, Lijo <[email protected]>; Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Hi Lijo,
Quoted 3 lines · Lijo
[Public]
We already have a per instance power state that can be tracked. What about something like attached?
This also has concurrent access of the power state , vcn.inst[i].cur_state is not protected by
workload_profile_mutexevery where, it can still change while you are holding
workload_profile_mutexand checking it.Regards,
Sathish
Quoted 321 lines
Thanks,
Lijo
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Sundararaju, Sathishkumar
Sent: Thursday, August 14, 2025 4:43 AM
To: Alex Deucher <[email protected]>
Cc: Wu, David <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu/vcn: fix video profile race condition (v3)Quoted 53 lines · Alex Deucher
Quoted 33 lines · Sathishkumar
Quoted 27 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 7 lines
Quoted 6 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 10 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding, that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_usedecreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between jobs , can be 0.
- both of the above conditions do not mean video decode is complete on inst1, it is actively decoding.Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I mean on inst1 the job scheduled is already complete, so 0 outstanding fences, newer job is yet to be scheduled
Quoted 7 lines
and commited to ring (inst1) , this doesn't mean decode has stopped on
inst1 right (I am saying if timing of inst0 idle work coincides with this),Or am I wrong in assuming this ? Can't this ever happen ? Please correct my understanding here.
The flow looks like:
begin_use(inst)emit_fence(inst)end_use(inst)...later
fence signals
...later
work handlerQuoted 5 lines
In
begin_usewe increment the global and per instance submission. This protects the power gating and profile untilend_use. In end use we decrement the submissions because we don't need to protect anything any more as we have the fence that was submitted via the ring. That fence won't signal until the job is complete.Is a next
begin_usealways guaranteed to be run before current job fence signals ?if not then both total submission and total fence are zero , example delayed job/packet submissions
from user space, or next job schedule happens after current job fence signals.
if this is never possible then (v3) is perfect.
Regards,
Sathish
Quoted 236 lines
For power gating, we
only care about the submission count and fences for that instance, for the profile, we care about submission count and fences all instances. Once the fences have signalled, the outstanding fences will be 0 and there won't be any active work.Alex
Quoted 228 lines
Regards,
Sathish
Quoted 224 lines
Alex
Quoted 222 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of active video decode, when all instances are off we can safely assume no active decode and global lock protects it against new
begin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.
Regards,
SathishQuoted 206 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 199 lines
can be a sure shot indication of break in a video decode, the
mistake in my patch was using per instance mutex, I should have
used a common global mutex, then that covers the situation David is trying to bring out.Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 187 lines · David
Quoted 134 lines · Alex Deucher
Quoted 133 lines · David
Hi Alex,
The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the begin_use().
The suggestion of moving it inside the lock (which I prefer
in case someone adds more before the lock and not reviewed
thoroughly)
- up to you to decide.Reviewed-by: David (Ming Qiang) Wu <[email protected]>
Thanks,
David
On 8/13/2025 9:45 AM, Alex Deucher wrote:
If there are multiple instances of the VCN running, we may
end up switching the video profile while another instance
is active because we only take into account the current
instance's submissions. Look at all outstanding fences for
the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload
profile
handling")
Reviewed-by: Sathishkumar S
<[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20 deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct
amdgpu_vcn_inst, idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 << vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v =
+ &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4
and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] || @@ -436,18 +442,18 @@
static void amdgpu_vcn_idle_work_handler(struct work_struct
*work)
else
new_state.fw_based =
VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst,
+ &new_state);
}- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN
instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, false);
if (r) @@ -467,16 +473,10 @@
void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
cancel_delayed_work_sync(&vcn_inst->idle_work);- /* We can safely return early here because we've
cancelled the
- * the delayed work so there is no one else to set
it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-
mutex_lock(&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 46 lines
Alex
Quoted 44 lines
David
if (!adev->vcn.workload_profile_active) {
r =
amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO, @@ -487,7 +487,6 @@ void
amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst,
AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct
amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submissi
o
n
_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission
_
c
nt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT); diff
--git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
Quoted 28 lines · Alex Deucher
Quoted 15 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.
Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.
I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on. We should avoid this glitch. This requires the idle handler sets/clears a flag for done for this instance as Sathish's original patch. When all instances set/clear the flag then we can safely power off.
David
Quoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 30 lines · David
Quoted 28 lines · Alex Deucher
Quoted 26 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.
Quoted 7 lines
Quoted 2 lines
How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.
How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
Alex
Quoted 212 lines
We should avoid this glitch. This requires the idle handler sets/clears a flag for
done for this instance as Sathish's original patch. When all instances set/clear the
flag then we can safely power off.
DavidQuoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 31 lines · Alex Deucher
Quoted 30 lines · David
Quoted 29 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.
Quoted 8 lines
Quoted 5 lines
I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences and no jobs so it turns off the power - but just ms later a new job is submitted from the same decode session which could be mpv player as it does not need to submit jobs without delays. This will turn on the power.
David
Quoted 213 lines
Alex
Quoted 211 lines
We should avoid this glitch. This requires the idle handler sets/clears a flag for
done for this instance as Sathish's original patch. When all instances set/clear the
flag then we can safely power off.
DavidQuoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 41 lines · David
Quoted 31 lines · Alex Deucher
Quoted 30 lines · David
Quoted 29 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.Quoted 8 lines
Quoted 5 lines
I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
Quoted 8 lines
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences
and no jobs so it turns off the power - but just ms later a new job is submitted
from the same decode session which could be mpv player as it does not need to
submit jobs without delays. This will turn on the power.
I'm not following. Every submission will start with begin_use().
Alex
Quoted 215 lines
David
Quoted 213 lines
Alex
Quoted 211 lines
We should avoid this glitch. This requires the idle handler sets/clears a flag for
done for this instance as Sathish's original patch. When all instances set/clear the
flag then we can safely power off.
DavidQuoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 49 lines · Alex Deucher
Quoted 40 lines · David
Quoted 31 lines · Alex Deucher
Quoted 30 lines · David
Quoted 29 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.Quoted 8 lines
Quoted 5 lines
I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
Quoted 8 lines
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences
and no jobs so it turns off the power - but just ms later a new job is submitted
from the same decode session which could be mpv player as it does not need to
submit jobs without delays. This will turn on the power.
Quoted 1 line
I'm not following. Every submission will start with
begin_use().
yes - it does - it can power on vcn but this happens in the middle of a decode session which has 10s timeout to call its own idle handler - in fact the other instance's idle handler will power off vcn because it does not know it needs to wait until the decoding session times out.
Quoted 216 lines
Alex
Quoted 214 lines
David
Quoted 213 lines
Alex
Quoted 211 lines
We should avoid this glitch. This requires the idle handler sets/clears a flag for
done for this instance as Sathish's original patch. When all instances set/clear the
flag then we can safely power off.
DavidQuoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 92 lines · Alex Deucher
If there are multiple instances of the VCN running, we may end up switching the video profile while another instance is active because we only take into account the current instance's submissions. Look at all outstanding fences for the video profile.
v2: drop early exit in
begin_use()
v3: handle possible race betweenbegin_use()work handlerFixes 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profilehandling")
Reviewed-by Sathishkumar S <[email protected]> (v1)Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 + 2 files changed, 21 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +11
-5@@ -415,19 +415,25 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) struct amdgpu_vcn_inst *vcn_inst = container_of(work, struct amdgpu_vcn_inst, idle_work.work);struct amdgpu_device *adev = vcn_inst->adev; -unsigned int fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; -unsigned int i = vcn_inst->inst, j; +unsigned int total_fences =0 , fence[AMDGPU_MAX_VCN_INSTANCES ] = {0 }; +unsigned int i, j;int r =0 ; -if (adev->vcn.harvest_config & (1 << i)) +if (adev->vcn.harvest_config & (1 << vcn_inst->inst))return ; -for (j =0 ; j < adev->vcn.inst[i].num_enc_rings; ++j) - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_enc[j]); +for (i =0 ; i < adev->vcn.num_vcn_inst; ++i) { +struct amdgpu_vcn_inst *v = &adev->vcn.inst[i]; + +for (j =0 ; j < v->num_enc_rings; ++j) + fence[i] +=amdgpu_fence_count_emitted (&v->ring_enc[j]); + fence[i] +=amdgpu_fence_count_emitted (&v->ring_dec); + total_fences += fence[i]; + }/* Only set DPG pause for VCN3 or below, VCN4 and above will be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +6
-7- !adev->vcn.inst[i].using_unified_queue) { + !vcn_inst->using_unified_queue) {struct dpg_pause_state new_state; if (fence[i] ||@@ -436,18 +442,18 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work) else new_state.fw_based =VCN_DPG_STATE__UNPAUSE ; - adev->vcn.inst[i].pause_dpg_mode (vcn_inst, &new_state); + vcn_inst->pause_dpg_mode (vcn_inst, &new_state); } - fence[i] +=amdgpu_fence_count_emitted (&vcn_inst->ring_dec); - fences += fence[i]; - -if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { +if (!fence[vcn_inst->inst] && !atomic_read (&vcn_inst->total_submission_cnt)) { +/* This is specific to this instance */ mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE );mutex_unlock (&vcn_inst->vcn_pg_lock);mutex_lock (&adev->vcn.workload_profile_mutex); -if (adev->vcn.workload_profile_active) { +/* This is global and depends on all VCN instances */ + if (adev->vcn.workload_profile_active &&!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,PP_SMC_POWER_PROFILE_VIDEO,false); if (r)drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c void amdgpu_vcn_ring_begin_use(struct · line 477
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-0@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)int r =0 ;atomic_inc (&vcn_inst->total_submission_cnt); +atomic_inc (&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-7cancel_delayed_work_sync (&vcn_inst->idle_work); -/* We can safely return early here because we've cancelled the -* the delayed work so there is no one else to set it to false -* and we don't care if someone else sets it to true. -*/ -if (adev->vcn.workload_profile_active) - goto pg_lock; -mutex_lock (&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0
-0if (!adev->vcn.workload_profile_active) { r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1
-1@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring) }mutex_unlock (&adev->vcn.workload_profile_mutex); -pg_lock:mutex_lock (&vcn_inst->vcn_pg_lock); vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_UNGATE );@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)atomic_dec (&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec (&ring->adev->vcn.inst[ring->me].total_submission_cnt); +atomic_dec (&ring->adev->vcn.total_submission_cnt); schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,VCN_IDLE_TIMEOUT);drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +1
-0@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;uint8_t num_inst_per_aid; +atomic_t total_submission_cnt;/* IP reg dump */ uint32_t *ip_dump;
Quoted 51 lines · David Wu
Quoted 49 lines · Alex Deucher
Quoted 48 lines · David
Quoted 31 lines · Alex Deucher
Quoted 30 lines · David
Quoted 29 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.Quoted 8 lines
Quoted 5 lines
I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences
and no jobs so it turns off the power - but just ms later a new job is submitted
from the same decode session which could be mpv player as it does not need to
submit jobs without delays. This will turn on the power.
Quoted 6 lines
Quoted 1 line
I'm not following. Every submission will start with
begin_use().yes - it does - it can power on vcn but this happens in the middle of a decode session which has 10s timeout to call its own idle handler - in fact the other instance's idle handler will power off vcn because it does not know it needs
to wait until the decoding session times out.
I don't follow. If there are no outstanding fences, there is no reason to not power down the VCN instance and disable the video profile. If there are still outstanding fences, then the VCN instance those fences are associated with will stay on and the video profile will stay enabled. If the engine hangs and eventually gets reset, the fence will be signalled and then there will be no outstanding fences so the idle handler will eventually disable the power profile. The idle handler will keep getting rescheduled as long as there is still oustanding work.
Alex
Quoted 216 lines
Quoted 216 lines
Alex
Quoted 214 lines
David
Quoted 213 lines
Alex
Quoted 211 lines
We should avoid this glitch. This requires the idle handler sets/clears a flag for
done for this instance as Sathish's original patch. When all instances set/clear the
flag then we can safely power off.
DavidQuoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of
total_submission_cntshould work - in that it is unlikely to have a context switch right after thebegin_use(). The suggestion of moving it inside the lock (which I prefer in case someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by David (Ming Qiang) Wu <[email protected]>Thanks,
DavidQuoted 97 lines · Alex Deucher
If there are multiple instances of the VCN running,
we may end up switching the video profile while another
instance is active because we only take into account
the current instance's submissions. Look at all
outstanding fences for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profile
handling")
Reviewed-by: Sathishkumar S <[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20 deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct amdgpu_vcn_inst,
idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 << vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v = &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4 and above will
be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] ||
@@ -436,18 +442,18 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based = VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst, &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,
false);
if (r)
@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct
amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);move this addition down inside the mutex lock
Quoted 10 lines
cancel_delayed_work_sync(&vcn_inst->idle_work);
- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-
mutex_lock(&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
Quoted 34 lines
if (!adev->vcn.workload_profile_active) {
r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,
@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct
amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission_cnt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
Quoted 57 lines · Alex Deucher
Quoted 50 lines · David Wu
Quoted 49 lines · Alex Deucher
Quoted 48 lines · David
Quoted 31 lines · Alex Deucher
Quoted 30 lines · David
Quoted 29 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.Quoted 8 lines
Quoted 5 lines
I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences
and no jobs so it turns off the power - but just ms later a new job is submitted
from the same decode session which could be mpv player as it does not need to
submit jobs without delays. This will turn on the power.Quoted 6 lines
Quoted 1 line
I'm not following. Every submission will start with
begin_use().yes - it does - it can power on vcn but this happens in the middle of a decode session which has 10s timeout to call its own idle handler - in fact the other instance's idle handler will power off vcn because it does not know it needs
to wait until the decoding session times out.
Quoted 9 lines
I don't follow. If there are no outstanding fences, there is no reason to not power down the VCN instance and disable the video profile. If there are still outstanding fences, then the VCN instance those fences are associated with will stay on and the video profile will stay enabled. If the engine hangs and eventually gets reset, the fence will be signalled and then there will be no outstanding fences so the idle handler will eventually disable the power profile. The idle handler will keep getting rescheduled as long as there is still oustanding work.
inst0 and inst1:
inst0 sends jobA, then ends jobA and no more job submitted in 500ms and
job queue is empty - at this point inst1's idle handler sees no outstanding fences/jobs then power off. However inst0 starts to submit job after 500ms - inst0' idle handler has not started/scheduled to run but inst1's has finished already which
does not know inst0 has not timed out or called its own idle handler. This violates the logic for idle handler's timeout condition. (i.e 10s timeout designed but timed out in 500ms) all this means it powered down too early for inst0.
Quoted 219 lines
Alex
Quoted 216 lines
Quoted 216 lines
Alex
Quoted 214 lines
David
Quoted 213 lines
Alex
Quoted 211 lines
We should avoid this glitch. This requires the idle handler sets/clears a flag for
done for this instance as Sathish's original patch. When all instances set/clear the
flag then we can safely power off.
DavidQuoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 123 lines · Alex Deucher
Quoted 122 lines · David
Hi Alex,
The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the begin_use().
The suggestion of moving it inside the lock (which I prefer in case
someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by: David (Ming Qiang) Wu <[email protected]>
Thanks,
David
On 8/13/2025 9:45 AM, Alex Deucher wrote:
If there are multiple instances of the VCN running,
we may end up switching the video profile while another
instance is active because we only take into account
the current instance's submissions. Look at all
outstanding fences for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profile
handling")
Reviewed-by: Sathishkumar S <[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20 deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct amdgpu_vcn_inst,
idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 << vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v = &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4 and above will
be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] ||
@@ -436,18 +442,18 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based = VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst, &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,
false);
if (r)
@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct
amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
cancel_delayed_work_sync(&vcn_inst->idle_work);- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-
mutex_lock(&adev->vcn.workload_profile_mutex);Quoted 4 lines
Quoted 3 lines
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.Why does this need to be protected by the mutex?
hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
Quoted 35 lines
David
if (!adev->vcn.workload_profile_active) {
r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,
@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct
amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission_cnt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
Quoted 69 lines · David Wu
Quoted 57 lines · Alex Deucher
Quoted 50 lines · David Wu
Quoted 49 lines · Alex Deucher
Quoted 48 lines · David
Quoted 31 lines · Alex Deucher
Quoted 30 lines · David
Quoted 29 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.Quoted 8 lines
Quoted 5 lines
I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences
and no jobs so it turns off the power - but just ms later a new job is submitted
from the same decode session which could be mpv player as it does not need to
submit jobs without delays. This will turn on the power.Quoted 6 lines
Quoted 1 line
I'm not following. Every submission will start with
begin_use().yes - it does - it can power on vcn but this happens in the middle of a decode session which has 10s timeout to call its own idle handler - in fact the other instance's idle handler will power off vcn because it does not know it needs
to wait until the decoding session times out.Quoted 9 lines
I don't follow. If there are no outstanding fences, there is no reason to not power down the VCN instance and disable the video profile. If there are still outstanding fences, then the VCN instance those fences are associated with will stay on and the video profile will stay enabled. If the engine hangs and eventually gets reset, the fence will be signalled and then there will be no outstanding fences so the idle handler will eventually disable the power profile. The idle handler will keep getting rescheduled as long as there is still oustanding work.
Quoted 12 lines
inst0 and inst1:
inst0 sends jobA, then ends jobA and no more job submitted in 500ms and job queue is empty - at this point inst1's idle handler sees no outstanding fences/jobs
then power off. However inst0 starts to submit job after 500ms - inst0' idle handler
has not started/scheduled to run but inst1's has finished already which does not know inst0 has not timed out or called its own idle handler. This violates the
logic for idle handler's timeout condition. (i.e 10s timeout designed but timed out in 500ms)
all this means it powered down too early for inst0.
I still don't follow. Here's a sample flow. Job comes in on inst 0 and then slightly later on inst 1.
Inst 0 job submission
Inst 0 calls begin_use(). This cancels the current inst worker thread. It enables the video profile and ungates the instance. IBs and fence packets get submitted to instance 0 of the engine Inst 0 calls end_use(). This schedules the worker thread for VCN_IDLE_TIMEOUT jiffies in the future.
Inst 1 job submission:
Inst 1 calls begin_use(). This cancels the current inst worker thread. It sees the video profile is enabled and ungates the instance.
IBs and fence packets get submitted to instance 1 of the engine Inst 1 calls end_use(). This schedules the worker thread for VCN_IDLE_TIMEOUT jiffies in the future.
inst 0 work hander runs. Sees outstanding fences on inst 0; skips powergating inst 0, skips disabling video profile. Schedules the worker thread for VCN_IDLE_TIMEOUT jiffies in the future.
inst 0 IB completes and fence signals
inst 1 IB completes and fence signals
inst 1 work hander runs. Sees no outstanding fences on inst 1. powergates inst 1. Check if there are any outstanding fences on other instances. Sees the no fences from inst 0 so disables the video profile.
inst 0 work hander runs. Sees no outstanding fences on inst 0. powergates inst 0. Check if there are any outstanding fences on other instances. Sees the no fences from inst 1, sees that video profile is already disabled.
You can insert additional job submissions anywhere you want in the timeline.
Alex
Quoted 219 lines
Quoted 219 lines
Alex
Quoted 216 lines
Quoted 216 lines
Alex
Quoted 214 lines
David
Quoted 213 lines
Alex
Quoted 211 lines
We should avoid this glitch. This requires the idle handler sets/clears a flag for
done for this instance as Sathish's original patch. When all instances set/clear the
flag then we can safely power off.
DavidQuoted 205 lines
Alex
Quoted 203 lines
Whereas if instances are powered off we are sure idle time is past and it is powered off, no possible way of
active video decode, when all instances are off we can safely assume no active decode and global lock protects
it against newbegin_useon any instance. But the only distant concern is global common locks w.r.t perf, but we
are already having a global workprofile mutex , so there shouldn't be any drop in perf, with just one single
global lock for all instances.Just sending out a patch with this fix, will leave it to you to decide the right method. If you think outstanding total fences
can never be 0 during decode, then your previous version (v3) itself is good, there is no real benefit of splitting the handlers as such.Regards,
SathishQuoted 186 lines
If it is possible, maybe it would be easier to just split the profile and powergating into separate handlers. The profile one would be global and the powergating one would be per instance. See the attached patches.
Alex
Quoted 179 lines
can be a sure shot indication of break in a video decode, the mistake in my patch was using per instance mutex, I should have used a common global mutex, then that covers the situation David is trying to bring out.
Using one global vcn.pg_lock for idle and
begin_useand using flags to track power state could help us totally avoid this situation.Regards,
Sathish
Quoted 167 lines · David
Quoted 127 lines · Alex Deucher
On Wed, Aug 13, 2025 at 12:39 PM Wu, David <[email protected]> wrote:
Hi Alex,The addition of total_submission_cnt should work - in that
it is unlikely to have a context switch right after the begin_use().
The suggestion of moving it inside the lock (which I prefer in case
someone
adds more before the lock and not reviewed thoroughly)
- up to you to decide.Reviewed-by: David (Ming Qiang) Wu <[email protected]>
Thanks,
David
On 8/13/2025 9:45 AM, Alex Deucher wrote:
If there are multiple instances of the VCN running,
we may end up switching the video profile while another
instance is active because we only take into account
the current instance's submissions. Look at all
outstanding fences for the video profile.v2: drop early exit in begin_use()
v3: handle possible race between begin_use() work handlerFixes: 3b669df92c85 ("drm/amdgpu/vcn: adjust workload profile
handling")
Reviewed-by: Sathishkumar S <[email protected]> (v1)
Signed-off-by: Alex Deucher <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 40
++++++++++++-------------
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 1 +
2 files changed, 21 insertions(+), 20 deletions(-)diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index 9a76e11d1c184..593c1ddf8819b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -415,19 +415,25 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
struct amdgpu_vcn_inst *vcn_inst =
container_of(work, struct amdgpu_vcn_inst,
idle_work.work);
struct amdgpu_device *adev = vcn_inst->adev;
- unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
- unsigned int i = vcn_inst->inst, j;
+ unsigned int total_fences = 0,
fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
+ unsigned int i, j;
int r = 0;- if (adev->vcn.harvest_config & (1 << i))
+ if (adev->vcn.harvest_config & (1 << vcn_inst->inst))
return;- for (j = 0; j < adev->vcn.inst[i].num_enc_rings; ++j)
- fence[i] +=
amdgpu_fence_count_emitted(&vcn_inst->ring_enc[j]);
+ for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+ struct amdgpu_vcn_inst *v = &adev->vcn.inst[i];
+
+ for (j = 0; j < v->num_enc_rings; ++j)
+ fence[i] +=
amdgpu_fence_count_emitted(&v->ring_enc[j]);
+ fence[i] += amdgpu_fence_count_emitted(&v->ring_dec);
+ total_fences += fence[i];
+ }/* Only set DPG pause for VCN3 or below, VCN4 and above will
be handled by FW */
if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG &&
- !adev->vcn.inst[i].using_unified_queue) {
+ !vcn_inst->using_unified_queue) {
struct dpg_pause_state new_state;if (fence[i] ||
@@ -436,18 +442,18 @@ static void
amdgpu_vcn_idle_work_handler(struct work_struct *work)
else
new_state.fw_based = VCN_DPG_STATE__UNPAUSE;- adev->vcn.inst[i].pause_dpg_mode(vcn_inst, &new_state);
+ vcn_inst->pause_dpg_mode(vcn_inst, &new_state);
}- fence[i] += amdgpu_fence_count_emitted(&vcn_inst->ring_dec);
- fences += fence[i];
-
- if (!fences && !atomic_read(&vcn_inst->total_submission_cnt)) {
+ if (!fence[vcn_inst->inst] &&
!atomic_read(&vcn_inst->total_submission_cnt)) {
+ /* This is specific to this instance */
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_GATE);
mutex_unlock(&vcn_inst->vcn_pg_lock);
mutex_lock(&adev->vcn.workload_profile_mutex);
- if (adev->vcn.workload_profile_active) {
+ /* This is global and depends on all VCN instances */
+ if (adev->vcn.workload_profile_active &&
!total_fences &&
+ !atomic_read(&adev->vcn.total_submission_cnt)) {
r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,
false);
if (r)
@@ -467,16 +473,10 @@ void amdgpu_vcn_ring_begin_use(struct
amdgpu_ring *ring)
int r = 0;atomic_inc(&vcn_inst->total_submission_cnt);
+ atomic_inc(&adev->vcn.total_submission_cnt);
move this addition down inside the mutex lock
cancel_delayed_work_sync(&vcn_inst->idle_work);- /* We can safely return early here because we've cancelled the
- * the delayed work so there is no one else to set it to false
- * and we don't care if someone else sets it to true.
- */
- if (adev->vcn.workload_profile_active)
- goto pg_lock;
-
mutex_lock(&adev->vcn.workload_profile_mutex);
move to here:
atomic_inc(&adev->vcn.total_submission_cnt);
I think this should work for multiple instances.
Why does this need to be protected by the mutex?hmm.. OK - no need and it is actually better before the mutex. David
Quoted 37 lines
Alex
David
if (!adev->vcn.workload_profile_active) {
r = amdgpu_dpm_switch_power_profile(adev,
PP_SMC_POWER_PROFILE_VIDEO,
@@ -487,7 +487,6 @@ void amdgpu_vcn_ring_begin_use(struct
amdgpu_ring *ring)
}
mutex_unlock(&adev->vcn.workload_profile_mutex);-pg_lock:
mutex_lock(&vcn_inst->vcn_pg_lock);
vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE);@@ -528,6 +527,7 @@ void amdgpu_vcn_ring_end_use(struct amdgpu_ring
*ring)
atomic_dec(&ring->adev->vcn.inst[ring->me].dpg_enc_submission_cnt);atomic_dec(&ring->adev->vcn.inst[ring->me].total_submission_cnt);
+ atomic_dec(&ring->adev->vcn.total_submission_cnt);schedule_delayed_work(&ring->adev->vcn.inst[ring->me].idle_work,
VCN_IDLE_TIMEOUT);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
index b3fb1d0e43fc9..febc3ce8641ff 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h
@@ -352,6 +352,7 @@ struct amdgpu_vcn {uint16_t inst_mask;
uint8_t num_inst_per_aid;
+ atomic_t total_submission_cnt;/* IP reg dump */
uint32_t *ip_dump;
Quoted 103 lines · Alex Deucher
Quoted 68 lines · David Wu
Quoted 57 lines · Alex Deucher
Quoted 50 lines · David Wu
Quoted 49 lines · Alex Deucher
Quoted 48 lines · David
Quoted 31 lines · Alex Deucher
Quoted 30 lines · David
Quoted 29 lines · Alex Deucher
Quoted 14 lines · Sathishkumar
Quoted 7 lines · Alex Deucher
Quoted 5 lines · Sathishkumar
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
Quoted 6 lines
Quoted 5 lines
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Quoted 11 lines
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.Quoted 8 lines
Quoted 5 lines
I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences
and no jobs so it turns off the power - but just ms later a new job is submitted
from the same decode session which could be mpv player as it does not need to
submit jobs without delays. This will turn on the power.Quoted 6 lines
Quoted 1 line
I'm not following. Every submission will start with
begin_use().yes - it does - it can power on vcn but this happens in the middle of a decode session which has 10s timeout to call its own idle handler - in fact the other instance's idle handler will power off vcn because it does not know it needs
to wait until the decoding session times out.Quoted 9 lines
I don't follow. If there are no outstanding fences, there is no reason to not power down the VCN instance and disable the video profile. If there are still outstanding fences, then the VCN instance those fences are associated with will stay on and the video profile will stay enabled. If the engine hangs and eventually gets reset, the fence will be signalled and then there will be no outstanding fences so the idle handler will eventually disable the power profile. The idle handler will keep getting rescheduled as long as there is still oustanding work.
Quoted 12 lines
inst0 and inst1:
inst0 sends jobA, then ends jobA and no more job submitted in 500ms and job queue is empty - at this point inst1's idle handler sees no outstanding fences/jobs
then power off. However inst0 starts to submit job after 500ms - inst0' idle handler
has not started/scheduled to run but inst1's has finished already which does not know inst0 has not timed out or called its own idle handler. This violates the
logic for idle handler's timeout condition. (i.e 10s timeout designed but timed out in 500ms)
all this means it powered down too early for inst0.I still don't follow. Here's a sample flow. Job comes in on inst 0 and then slightly later on inst 1.
Inst 0 job submission
Inst 0 callsbegin_use(). This cancels the current inst worker thread. It enables the video profile and ungates the instance. IBs and fence packets get submitted to instance 0 of the engine Inst 0 callsend_use(). This schedules the worker thread forVCN_IDLE_TIMEOUTjiffies in the future.Inst 1 job submission:
Inst 1 callsbegin_use(). This cancels the current inst worker thread. It sees the video profile is enabled and ungates the instance.
IBs and fence packets get submitted to instance 1 of the engine Inst 1 callsend_use(). This schedules the worker thread forVCN_IDLE_TIMEOUTjiffies in the future.inst 0 work hander runs. Sees outstanding fences on inst 0; skips powergating inst 0, skips disabling video profile. Schedules the worker thread for
VCN_IDLE_TIMEOUTjiffies in the future.
Quoted 8 lines
inst 0 IB completes and fence signals
inst 1 IB completes and fence signals
inst 1 work hander runs. Sees no outstanding fences on inst 1. powergates inst 1. Check if there are any outstanding fences on other instances. Sees the no fences from inst 0 so disables the video profile.
now there are jobs coming from inst0, so inst 0 idle handler won't run.
Quoted 4 lines
inst 0 work hander runs. Sees no outstanding fences on inst 0. powergates inst 0. Check if there are any outstanding fences on other instances. Sees the no fences from inst 1, sees that video profile is already disabled.
inst 0 work handler runs? could or could not - right? depends on if there are more jobs for inst0 and also if VCN_IDLE_TIMEOUT jiffies has passed for inst0. There is possibly a point in the sequence that inst0 stops submit jobs but its idle handler has not run yet. Should we wait until all instances have finished their idle handlers? if not then we will run into a power OFF(by inst1) -> ON(by inst0) for the active instance(inst0, expected ON). (active - I mean those have not timed out in VCN_IDLE_TIMEOUT jiffies and still can submit jobs in any time.)
if this could happen then we powered off too early. (I did not say we cannot do it but it is not expected)
Quoted 5 lines
You can insert additional job submissions anywhere you want in the timeline.
Alex
Quoted 1 line
Quoted 1 line
Quoted 138 lines · David Wu
On 2025-08-14 14:00, Alex Deucher wrote:
On Thu, Aug 14, 2025 at 12:44 PM David Wu <[email protected]> wrote:
On 2025-08-14 12:01, Alex Deucher wrote:
On Thu, Aug 14, 2025 at 11:35 AM David Wu <[email protected]> wrote:
On 2025-08-14 08:56, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 7:06 PM Wu, David <[email protected]> wrote:
On 8/13/2025 6:11 PM, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 5:47 PM Wu, David <[email protected]> wrote:
On 8/13/2025 5:03 PM, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 4:58 PM Sundararaju, Sathishkumar <[email protected]> wrote:
On 8/14/2025 1:35 AM, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 2:23 PM Sundararaju, Sathishkumar <[email protected]> wrote:
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences
and no jobs so it turns off the power - but just ms later a new job is submitted
from the same decode session which could be mpv player as it does not need to
submit jobs without delays. This will turn on the power.I'm not following. Every submission will start with
begin_use().yes - it does - it can power on vcn but this happens in the middle of a decode session which has 10s timeout to call its own idle handler - in fact the other instance's idle handler will power off vcn because it does not know it needs
to wait until the decoding session times out.I don't follow. If there are no outstanding fences, there is no reason to not power down the VCN instance and disable the video profile. If there are still outstanding fences, then the VCN instance those fences are associated with will stay on and the video profile will stay enabled. If the engine hangs and eventually gets reset, the fence will be signalled and then there will be no outstanding fences so the idle handler will eventually disable the power profile. The idle handler will keep getting rescheduled as long as there is still oustanding work.
inst0 and inst1:
inst0 sends jobA, then ends jobA and no more job submitted in 500ms and job queue is empty - at this point inst1's idle handler sees no outstanding fences/jobs
then power off. However inst0 starts to submit job after 500ms - inst0' idle handler
has not started/scheduled to run but inst1's has finished already which does not know inst0 has not timed out or called its own idle handler. This violates the
logic for idle handler's timeout condition. (i.e 10s timeout designed but timed out in 500ms)
all this means it powered down too early for inst0.I still don't follow. Here's a sample flow. Job comes in on inst 0 and then slightly later on inst 1.
Inst 0 job submission
Inst 0 callsbegin_use(). This cancels the current inst worker thread. It enables the video profile and ungates the instance. IBs and fence packets get submitted to instance 0 of the engine Inst 0 callsend_use(). This schedules the worker thread forVCN_IDLE_TIMEOUTjiffies in the future.Inst 1 job submission:
Inst 1 callsbegin_use(). This cancels the current inst worker thread. It sees the video profile is enabled and ungates the instance.
IBs and fence packets get submitted to instance 1 of the engine Inst 1 callsend_use(). This schedules the worker thread forVCN_IDLE_TIMEOUTjiffies in the future.inst 0 work hander runs. Sees outstanding fences on inst 0; skips powergating inst 0, skips disabling video profile. Schedules the worker thread for
VCN_IDLE_TIMEOUTjiffies in the future.inst 0 IB completes and fence signals
inst 1 IB completes and fence signals
inst 1 work hander runs. Sees no outstanding fences on inst 1. powergates inst 1. Check if there are any outstanding fences on other instances. Sees the no fences from inst 0 so disables the video profile.
now there are jobs coming from inst0, so inst 0 idle handler won't run.
Quoted 8 lines
inst 0 work hander runs. Sees no outstanding fences on inst 0. powergates inst 0. Check if there are any outstanding fences on other instances. Sees the no fences from inst 1, sees that video profile is already disabled.
inst 0 work handler runs? could or could not - right? depends on if there are more jobs for inst0 and also if
VCN_IDLE_TIMEOUTjiffies has passed for inst0. There is possibly a point in the sequence that inst0 stops submit jobs but its idle handler has not run yet. Should we wait until all instances have finished their idle handlers?
The work handler will run unless it is cancelled in begin_use(), but in that case, it will get scheduled again in end_use(). If it runs, but there is still outstanding work (fences), then it will get scheduled to run again in the work handler. All we are about from the power and video profile perspective is whether or not there are still outstanding fences. Once all fences are completed on all instances, we can disable the video profile regardless of whether then the idle handlers have run or not because the hw is already idle.
Quoted 4 lines
if not then we will run into a power OFF(by inst1) -> ON(by inst0) for the active
instance(inst0, expected ON). (active - I mean those have not timed out inVCN_IDLE_TIMEOUTjiffies and still can submit jobs in any time.)
if this could happen then we powered off too early. (I did not say we cannot do it but it is not expected)
VCN_IDLE_TIMEOUT is not a timeout. We could call it something else. VCN_IDLE_CHECK_PERIOD is a better description. It just runs in the future to check if all of the fences are signalled. If they are not signalled, we schedule the worker to run again further in the future. If you reduce the time, it will check more often, you can potentially save more power, but you add more CPU overhead because the handler may run more often.
Alex
Quoted 5 lines
You can insert additional job submissions anywhere you want in the timeline.
Alex
after discussion with Alex offline it is clear now as the power profile switch is fine and should not affect the user experience (such as mpv playback).
Alex, please correct me if my understanding is incorrect. This patch (v3) is also good.
Thanks,
David
Quoted 174 lines · Alex Deucher
Quoted 137 lines · David Wu
On 2025-08-14 14:00, Alex Deucher wrote:
On Thu, Aug 14, 2025 at 12:44 PM David Wu <[email protected]> wrote:
On 2025-08-14 12:01, Alex Deucher wrote:
On Thu, Aug 14, 2025 at 11:35 AM David Wu <[email protected]> wrote:
On 2025-08-14 08:56, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 7:06 PM Wu, David <[email protected]> wrote:
On 8/13/2025 6:11 PM, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 5:47 PM Wu, David <[email protected]> wrote:
On 8/13/2025 5:03 PM, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 4:58 PM Sundararaju, Sathishkumar <[email protected]> wrote:
On 8/14/2025 1:35 AM, Alex Deucher wrote:
On Wed, Aug 13, 2025 at 2:23 PM Sundararaju, Sathishkumar <[email protected]> wrote:
Hi Alex, Hi David,
I see David's concern but his suggestion yet wont solve the problem, neither the current form , reason :-
The emitted fence count and total submission count are fast transients which frequently become 0 in between video decodes (between jobs) even with the atomics and locks there can be a switch of video power profile, in the current form of patch that window is minimized, but still can happen if stress tested. But power state of any instance becoming zero
Can you explain how this can happen? I'm not seeing it.
Consider this situation, inst0 and inst1 actively decoding, inst0 decode completes, delayed idle work starts.
inst0 idle handler can read 0 total fences and 0 total submission count, even if inst1 is actively decoding,
that's between the jobs,
- asbegin_useincreaments vcn.total_submission_cnt andend_use
decreaments vcn.total_submission_cnt that can be 0.
- if outstanding fences are cleared and no new emitted fence, between
jobs , can be 0.
- both of the above conditions do not mean video decode is complete on
inst1, it is actively decoding.How can there be active decoding without an outstanding fence? In that case,
total_fences(fences from both instances) would be non-0.I think it should be non-0.
I do see a hiccup possible - i.e the power switching from ON to OFF then ON in the
middle of decoding, i.e inst0 idle handler turns it off then inst1 turns it on.How would that happen? As long as there submission cnt is non-0 and there are outstanding fences on any instance, the video profile will stay active.
there could be no jobs but it doesn't timeout yet and new jobs will come in any ms - note all fences are done at this time. The idle handler sees no fences
and no jobs so it turns off the power - but just ms later a new job is submitted
from the same decode session which could be mpv player as it does not need to
submit jobs without delays. This will turn on the power.I'm not following. Every submission will start with
begin_use().yes - it does - it can power on vcn but this happens in the middle of a decode session which has 10s timeout to call its own idle handler - in fact the other instance's idle handler will power off vcn because it does not know it needs
to wait until the decoding session times out.I don't follow. If there are no outstanding fences, there is no reason to not power down the VCN instance and disable the video profile. If there are still outstanding fences, then the VCN instance those fences are associated with will stay on and the video profile will stay enabled. If the engine hangs and eventually gets reset, the fence will be signalled and then there will be no outstanding fences so the idle handler will eventually disable the power profile. The idle handler will keep getting rescheduled as long as there is still oustanding work.
inst0 and inst1:
inst0 sends jobA, then ends jobA and no more job submitted in 500ms and job queue is empty - at this point inst1's idle handler sees no outstanding fences/jobs
then power off. However inst0 starts to submit job after 500ms - inst0' idle handler
has not started/scheduled to run but inst1's has finished already which does not know inst0 has not timed out or called its own idle handler. This violates the
logic for idle handler's timeout condition. (i.e 10s timeout designed but timed out in 500ms)
all this means it powered down too early for inst0.I still don't follow. Here's a sample flow. Job comes in on inst 0 and then slightly later on inst 1.
Inst 0 job submission
Inst 0 callsbegin_use(). This cancels the current inst worker thread. It enables the video profile and ungates the instance. IBs and fence packets get submitted to instance 0 of the engine Inst 0 callsend_use(). This schedules the worker thread forVCN_IDLE_TIMEOUTjiffies in the future.Inst 1 job submission:
Inst 1 callsbegin_use(). This cancels the current inst worker thread. It sees the video profile is enabled and ungates the instance.
IBs and fence packets get submitted to instance 1 of the engine Inst 1 callsend_use(). This schedules the worker thread forVCN_IDLE_TIMEOUTjiffies in the future.inst 0 work hander runs. Sees outstanding fences on inst 0; skips powergating inst 0, skips disabling video profile. Schedules the worker thread for
VCN_IDLE_TIMEOUTjiffies in the future.inst 0 IB completes and fence signals
inst 1 IB completes and fence signals
inst 1 work hander runs. Sees no outstanding fences on inst 1. powergates inst 1. Check if there are any outstanding fences on other instances. Sees the no fences from inst 0 so disables the video profile.
now there are jobs coming from inst0, so inst 0 idle handler won't run.
Quoted 8 lines
inst 0 work hander runs. Sees no outstanding fences on inst 0. powergates inst 0. Check if there are any outstanding fences on other instances. Sees the no fences from inst 1, sees that video profile is already disabled.
inst 0 work handler runs? could or could not - right? depends on if there are more jobs for inst0 and also if
VCN_IDLE_TIMEOUTjiffies has passed for inst0. There is possibly a point in the sequence that inst0 stops submit jobs but its idle handler has not run yet. Should we wait until all instances have finished their idle handlers?The work handler will run unless it is cancelled in
begin_use(), but in that case, it will get scheduled again inend_use(). If it runs, but there is still outstanding work (fences), then it will get scheduled to run again in the work handler. All we are about from the power and video profile perspective is whether or not there are still outstanding fences. Once all fences are completed on all instances, we can disable the video profile regardless of whether then the idle handlers have run or not because the hw is already idle.Quoted 4 lines
if not then we will run into a power OFF(by inst1) -> ON(by inst0) for the active
instance(inst0, expected ON). (active - I mean those have not timed out inVCN_IDLE_TIMEOUTjiffies and still can submit jobs in any time.)
if this could happen then we powered off too early. (I did not say we cannot do it but it is not expected)
VCN_IDLE_TIMEOUTis not a timeout. We could call it something else.VCN_IDLE_CHECK_PERIODis a better description. It just runs in the future to check if all of the fences are signalled. If they are not signalled, we schedule the worker to run again further in the future. If you reduce the time, it will check more often, you can potentially save more power, but you add more CPU overhead because the handler may run more often.Alex
Quoted 5 lines
You can insert additional job submissions anywhere you want in the timeline.
Alex