Skip to contentSkip to conversation

drm/amdgpu/gfx: fix ref counting for ring based profile handling

PATCH Superseded Alex Deucher 4 participants 7 messages Mar 11, 2025 – Mar 12, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

PATCH 1/3 drm/amdgpu/gfx: fix ref counting for ring based profile handling

Only increment the power profile on the first submission. Since the decrement may end up being pushed out as new submissions come in, we only need to increment it once.

Fixes 1443dd3c67f6 ("drm/amd/pm: fix and simplify workload handling”)
Cc Yang Wang <[email protected]>
Cc Kenneth Feng <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 28 ++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +21 -7
index 984e6ff6e4632..90396aa8ec9f6 100644
@@ -2142,12 +2142,25 @@ void amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring *ring)
 		amdgpu_gfx_kfd_sch_ctrl(adev, idx, true);
 }
 
+static unsigned int
+amdgpu_gfx_get_kernel_ring_fence_counts(struct amdgpu_device *adev)
+{
+	unsigned int i, fences = 0;
+
+	for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
+		fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
+	for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
+		fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+
+	return fences;
+}
+
 void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
 {
 	struct amdgpu_device *adev =
 		container_of(work, struct amdgpu_device, gfx.idle_work.work);
 	enum PP_SMC_POWER_PROFILE profile;
-	u32 i, fences = 0;
+	unsigned int fences = 0;
 	int r;
 
 	if (adev->gfx.num_gfx_rings)
@@ -2155,10 +2168,8 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
 	else
 		profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-	for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
-		fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
-	for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
-		fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+	fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
+
 	if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)) {
 		r = amdgpu_dpm_switch_power_profile(adev, profile, false);
 		if (r)
@@ -2174,6 +2185,7 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
 	enum PP_SMC_POWER_PROFILE profile;
+	unsigned int fences = 0;
 	int r;
 
 	if (adev->gfx.num_gfx_rings)
@@ -2181,15 +2193,17 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
 	else
 		profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-	atomic_inc(&adev->gfx.total_submission_cnt);
+	fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
 
-	if (!cancel_delayed_work_sync(&adev->gfx.idle_work)) {
+	if (!cancel_delayed_work_sync(&adev->gfx.idle_work) && !fences &&
+	    !atomic_read(&adev->gfx.total_submission_cnt)) {
 		r = amdgpu_dpm_switch_power_profile(adev, profile, true);
 		if (r)
 			dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
 				 profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
 				 "fullscreen 3D" : "compute");
 	}
+	atomic_inc(&adev->gfx.total_submission_cnt);
 }
 
 void amdgpu_gfx_profile_ring_end_use(struct amdgpu_ring *ring)
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH 2/3 drm/amdgpu/vcn: fix ref counting for ring based profile handling

Only increment the power profile on the first submission. Since the decrement may end up being pushed out as new submissions come in, we only need to increment it once.

Fixes 1443dd3c67f6 ("drm/amd/pm: fix and simplify workload handling”)
Cc Yang Wang <[email protected]>
Cc Kenneth Feng <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +8 -2
index 8d8b39e6d197a..1954e276799bb 100644
@@ -451,17 +451,23 @@ 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];
+	unsigned int fences = 0, i;
 	int r = 0;
 
-	atomic_inc(&vcn_inst->total_submission_cnt);
+	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);
 
-	if (!cancel_delayed_work_sync(&vcn_inst->idle_work)) {
+	if (!cancel_delayed_work_sync(&vcn_inst->idle_work) && !fences &&
+	    !atomic_read(&vcn_inst->total_submission_cnt)) {
 		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);
 	}
 
+	atomic_inc(&vcn_inst->total_submission_cnt);
+
 	mutex_lock(&vcn_inst->vcn_pg_lock);
 	vcn_inst->set_pg_state(vcn_inst, AMD_PG_STATE_UNGATE);
 
Signature · 1 line
2.48.1
author maintainer
#2 lore raw

PATCH 3/3 drm/amdgpu/vcn2.5: fix ref counting for ring based profile handling

In reply to Alex Deucher

Only increment the power profile on the first submission. Since the decrement may end up being pushed out as new submissions come in, we only need to increment it once.

Fixes 1443dd3c67f6 ("drm/amd/pm: fix and simplify workload handling”)
Cc Yang Wang <[email protected]>
Cc Kenneth Feng <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +16 -2
index ff03436698a4f..2248c4400b459 100644
@@ -160,17 +160,31 @@ 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];
+	unsigned int fences = 0;
+	unsigned int i, j;
 	int r = 0;
 
-	atomic_inc(&adev->vcn.inst[0].total_submission_cnt);
+	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
+		struct amdgpu_vcn_inst *v = &adev->vcn.inst[i];
+
+		if (adev->vcn.harvest_config & (1 << i))
+			continue;
+
+		for (j = 0; j < v->num_enc_rings; ++j)
+			fences += amdgpu_fence_count_emitted(&v->ring_enc[j]);
+		fences += amdgpu_fence_count_emitted(&v->ring_dec);
+	}
 
-	if (!cancel_delayed_work_sync(&adev->vcn.inst[0].idle_work)) {
+	if (!cancel_delayed_work_sync(&adev->vcn.inst[0].idle_work) && !fences &&
+	    !atomic_read(&adev->vcn.inst[0].total_submission_cnt)) {
 		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);
 	}
 
+	atomic_inc(&adev->vcn.inst[0].total_submission_cnt);
+
 	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);
Signature · 1 line
2.48.1
#3 lore raw
In reply to Alex Deucher

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Alex,
I tested this patch. After the desktop is launched, at a certain time, the workload is set to 3d fullscreen twice, then The idle worker can't set it back to bootup default.
Is it expected?
Thanks.

-----Original Message-----
From: Deucher, Alexander <[email protected]>
Sent: Tuesday, March 11, 2025 10:17 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Feng, Kenneth <[email protected]>
Subject: [PATCH 1/3] drm/amdgpu/gfx: fix ref counting for ring based profile handling

Only increment the power profile on the first submission.
Since the decrement may end up being pushed out as new submissions come in, we only need to increment it once.

Fixes 1443dd3c67f6 ("drm/amd/pm: fix and simplify workload handling”)
Cc Yang Wang <[email protected]>
Cc Kenneth Feng <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 28 ++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +20 -7
index 984e6ff6e4632..90396aa8ec9f6 100644
@@ -2142,12 +2142,25 @@ void amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring *ring)
                amdgpu_gfx_kfd_sch_ctrl(adev, idx, true);  }
 
+static unsigned int
+amdgpu_gfx_get_kernel_ring_fence_counts(struct amdgpu_device *adev) {
+       unsigned int i, fences = 0;
+
+       for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
+               fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
+       for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
+               fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+
+       return fences;
+}
+
 void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)  {
        struct amdgpu_device *adev =
                container_of(work, struct amdgpu_device, gfx.idle_work.work);
        enum PP_SMC_POWER_PROFILE profile;
-       u32 i, fences = 0;
+       unsigned int fences = 0;
        int r;
 
        if (adev->gfx.num_gfx_rings)
@@ -2155,10 +2168,8 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
        else
                profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-       for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
-               fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
-       for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
-               fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+       fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
+
        if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)) {
                r = amdgpu_dpm_switch_power_profile(adev, profile, false);
                if (r)
@@ -2174,6 +2185,7 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)  {
        struct amdgpu_device *adev = ring->adev;
        enum PP_SMC_POWER_PROFILE profile;
+       unsigned int fences = 0;
        int r;
 
        if (adev->gfx.num_gfx_rings)
@@ -2181,15 +2193,17 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
        else
                profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-       atomic_inc(&adev->gfx.total_submission_cnt);
+       fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
 
-       if (!cancel_delayed_work_sync(&adev->gfx.idle_work)) {
+       if (!cancel_delayed_work_sync(&adev->gfx.idle_work) && !fences &&
+           !atomic_read(&adev->gfx.total_submission_cnt)) {
                r = amdgpu_dpm_switch_power_profile(adev, profile, true);
                if (r)
                        dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
                                 profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
                                 "fullscreen 3D" : "compute");
        }
+       atomic_inc(&adev->gfx.total_submission_cnt);
 }
 
 void amdgpu_gfx_profile_ring_end_use(struct amdgpu_ring *ring)

--
2.48.1

#4 lore raw
Quoted 7 lines · Kenneth

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Alex,
I tested this patch. After the desktop is launched, at a certain time, the workload is set to 3d fullscreen twice, then The idle worker can't set it back to bootup default.
Is it expected?

Not expected. It was looking correct in my testing, but I think I have a better solution.

Alex

Quoted 92 lines

Thanks.

-----Original Message-----
From: Deucher, Alexander <[email protected]>
Sent: Tuesday, March 11, 2025 10:17 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Feng, Kenneth <[email protected]>
Subject: [PATCH 1/3] drm/amdgpu/gfx: fix ref counting for ring based profile handling

Only increment the power profile on the first submission.
Since the decrement may end up being pushed out as new submissions come in, we only need to increment it once.

Fixes 1443dd3c67f6 ("drm/amd/pm: fix and simplify workload handling”)
Cc Yang Wang <[email protected]>
Cc Kenneth Feng <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 28 ++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +20 -8
index 984e6ff6e4632..90396aa8ec9f6 100644
@@ -2142,12 +2142,25 @@ void amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring *ring)
                amdgpu_gfx_kfd_sch_ctrl(adev, idx, true);  }
 
+static unsigned int
+amdgpu_gfx_get_kernel_ring_fence_counts(struct amdgpu_device *adev) {
+       unsigned int i, fences = 0;
+
+       for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
+               fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
+       for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
+               fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+
+       return fences;
+}
+
 void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)  {
        struct amdgpu_device *adev =
                container_of(work, struct amdgpu_device, gfx.idle_work.work);
        enum PP_SMC_POWER_PROFILE profile;
-       u32 i, fences = 0;
+       unsigned int fences = 0;
        int r;
 
        if (adev->gfx.num_gfx_rings)
@@ -2155,10 +2168,8 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
        else
                profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-       for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
-               fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
-       for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
-               fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+       fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
+
        if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)) {
                r = amdgpu_dpm_switch_power_profile(adev, profile, false);
                if (r)
@@ -2174,6 +2185,7 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)  {
        struct amdgpu_device *adev = ring->adev;
        enum PP_SMC_POWER_PROFILE profile;
+       unsigned int fences = 0;
        int r;
 
        if (adev->gfx.num_gfx_rings)
@@ -2181,15 +2193,17 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
        else
                profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-       atomic_inc(&adev->gfx.total_submission_cnt);
+       fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
 
-       if (!cancel_delayed_work_sync(&adev->gfx.idle_work)) {
+       if (!cancel_delayed_work_sync(&adev->gfx.idle_work) && !fences &&
+           !atomic_read(&adev->gfx.total_submission_cnt)) {
                r = amdgpu_dpm_switch_power_profile(adev, profile, true);
                if (r)
                        dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
                                 profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
                                 "fullscreen 3D" : "compute");
        }
+       atomic_inc(&adev->gfx.total_submission_cnt);
 }
 
 void amdgpu_gfx_profile_ring_end_use(struct amdgpu_ring *ring)
--

2.48.1

#5 lore raw
In reply to Alex Deucher
Quoted 65 lines · Alex Deucher

Only increment the power profile on the first submission. Since the decrement may end up being pushed out as new submissions come in, we only need to increment it once.

Fixes 1443dd3c67f6 ("drm/amd/pm: fix and simplify workload handling”)
Cc Yang Wang <[email protected]>
Cc Kenneth Feng <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 28 ++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +17 -5
index 984e6ff6e4632..90396aa8ec9f6 100644
@@ -2142,12 +2142,25 @@ void amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring *ring)
 		amdgpu_gfx_kfd_sch_ctrl(adev, idx, true);
 }
 
+static unsigned int
+amdgpu_gfx_get_kernel_ring_fence_counts(struct amdgpu_device *adev)
+{
+	unsigned int i, fences = 0;
+
+	for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
+		fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
+	for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
+		fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+
+	return fences;
+}
+
 void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
 {
 	struct amdgpu_device *adev =
 		container_of(work, struct amdgpu_device, gfx.idle_work.work);
 	enum PP_SMC_POWER_PROFILE profile;
-	u32 i, fences = 0;
+	unsigned int fences = 0;
 	int r;
 
 	if (adev->gfx.num_gfx_rings)
@@ -2155,10 +2168,8 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
 	else
 		profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-	for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
-		fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
-	for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
-		fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+	fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
+
 	if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)) {
 		r = amdgpu_dpm_switch_power_profile(adev, profile, false);
 		if (r)
@@ -2174,6 +2185,7 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
 	enum PP_SMC_POWER_PROFILE profile;
+	unsigned int fences = 0;
 	int r;
 
 	if (adev->gfx.num_gfx_rings)

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring) · line 2199

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
@@ -2181,15 +2193,17 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
 	else
 		profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-	atomic_inc(&adev->gfx.total_submission_cnt);
+	fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
 
-	if (!cancel_delayed_work_sync(&adev->gfx.idle_work)) {
+	if (!cancel_delayed_work_sync(&adev->gfx.idle_work) && !fences &&
+	    !atomic_read(&adev->gfx.total_submission_cnt)) {

Should this check be restricted to !fences && !atomic_read(&adev->gfx.total_submission_cnt). If the work has already started execution, cancel_delayed_work_sync will wait for completion and will return true. In that case, it could happen that idle work would have already called amdgpu_dpm_switch_power_profile(adev, profile, false) since submission count increment is moved down.

Wondering if this needs to be split like below -

1) cancel_delayed_work_sync(&adev->gfx.idle_work);

2) Take fence/submission count

3) if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)

Thanks,
Lijo

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +1 -0
 		r = amdgpu_dpm_switch_power_profile(adev, profile, true);
 		if (r)
 			dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
 				 profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
 				 "fullscreen 3D" : "compute");
 	}
+	atomic_inc(&adev->gfx.total_submission_cnt);
 }
 
 void amdgpu_gfx_profile_ring_end_use(struct amdgpu_ring *ring)
#6 lore raw
Quoted 86 lines · Lijo
Quoted 65 lines · Alex Deucher

Only increment the power profile on the first submission. Since the decrement may end up being pushed out as new submissions come in, we only need to increment it once.

Fixes 1443dd3c67f6 ("drm/amd/pm: fix and simplify workload handling”)
Cc Yang Wang <[email protected]>
Cc Kenneth Feng <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 28 ++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +17 -5
index 984e6ff6e4632..90396aa8ec9f6 100644
@@ -2142,12 +2142,25 @@ void amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring *ring)
              amdgpu_gfx_kfd_sch_ctrl(adev, idx, true);
 }
 
+static unsigned int
+amdgpu_gfx_get_kernel_ring_fence_counts(struct amdgpu_device *adev)
+{
+     unsigned int i, fences = 0;
+
+     for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
+             fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
+     for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
+             fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+
+     return fences;
+}
+
 void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
 {
      struct amdgpu_device *adev =
              container_of(work, struct amdgpu_device, gfx.idle_work.work);
      enum PP_SMC_POWER_PROFILE profile;
-     u32 i, fences = 0;
+     unsigned int fences = 0;
      int r;
 
      if (adev->gfx.num_gfx_rings)
@@ -2155,10 +2168,8 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
      else
              profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-     for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
-             fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
-     for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
-             fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
+     fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
+
      if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)) {
              r = amdgpu_dpm_switch_power_profile(adev, profile, false);
              if (r)
@@ -2174,6 +2185,7 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
 {
      struct amdgpu_device *adev = ring->adev;
      enum PP_SMC_POWER_PROFILE profile;
+     unsigned int fences = 0;
      int r;
 
      if (adev->gfx.num_gfx_rings)

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring) · line 2199

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
@@ -2181,15 +2193,17 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
      else
              profile = PP_SMC_POWER_PROFILE_COMPUTE;
 
-     atomic_inc(&adev->gfx.total_submission_cnt);
+     fences = amdgpu_gfx_get_kernel_ring_fence_counts(adev);
 
-     if (!cancel_delayed_work_sync(&adev->gfx.idle_work)) {
+     if (!cancel_delayed_work_sync(&adev->gfx.idle_work) && !fences &&
+         !atomic_read(&adev->gfx.total_submission_cnt)) {

Should this check be restricted to !fences && !atomic_read(&adev->gfx.total_submission_cnt). If the work has already started execution, cancel_delayed_work_sync will wait for completion and will return true. In that case, it could happen that idle work would have already called amdgpu_dpm_switch_power_profile(adev, profile, false) since submission count increment is moved down.

Quoted 7 lines

Wondering if this needs to be split like below -

1) cancel_delayed_work_sync(&adev->gfx.idle_work);

2) Take fence/submission count

3) if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)

I think it will be easier with just a mutex and a flag. Will send out a new patch set momentarily.

Alex

Quoted 15 lines

Thanks,
Lijo

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +1 -0
              r = amdgpu_dpm_switch_power_profile(adev, profile, true);
              if (r)
                      dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
                               profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
                               "fullscreen 3D" : "compute");
      }
+     atomic_inc(&adev->gfx.total_submission_cnt);
 }
 
 void amdgpu_gfx_profile_ring_end_use(struct amdgpu_ring *ring)