Skip to contentSkip to conversation

cpufreq/amd-pstate: Refactor max frequency calculation

PATCH Superseded Naresh Solanki 2 participants 5 messages Dec 18, 2024 – Dec 19, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq/amd-pstate: Refactor max frequency calculation

Refactor to calculate max-freq more accurately.

---
 drivers/cpufreq/amd-pstate.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cpufreq/amd-pstate.c +2 -3
index d7630bab2516..78a2cbd14952 100644
@@ -892,7 +892,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
 	u64 numerator;
 	u32 nominal_perf, nominal_freq;
 	u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
-	u32 boost_ratio, lowest_nonlinear_ratio;
+	u32 lowest_nonlinear_ratio;
 	struct cppc_perf_caps cppc_perf;
 
 	ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
@@ -914,8 +914,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
 	ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
 	if (ret)
 		return ret;
-	boost_ratio = div_u64(numerator << SCHED_CAPACITY_SHIFT, nominal_perf);
-	max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
+	max_freq = div_u64(numerator * nominal_freq * 1000, nominal_perf);
 
 	lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
 	lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
Signature · 1 line
2.42.0
#1 lore raw
Quoted 1 line · Naresh Solanki

Refactor to calculate max-freq more accurately.

Can you add some more detail about what you're finding? What was it before, what is it now, why is it more accurate?

Quoted 20 lines
---
  drivers/cpufreq/amd-pstate.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cpufreq/amd-pstate.c +1 -1
index d7630bab2516..78a2cbd14952 100644
@@ -892,7 +892,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
  	u64 numerator;
  	u32 nominal_perf, nominal_freq;
  	u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
-	u32 boost_ratio, lowest_nonlinear_ratio;
+	u32 lowest_nonlinear_ratio;
  	struct cppc_perf_caps cppc_perf;
 
  	ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);

drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 917

Quoted 6 lines
drivers/cpufreq/amd-pstate.c +1 -2
@@ -914,8 +914,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
  	ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
  	if (ret)
  		return ret;
-	boost_ratio = div_u64(numerator << SCHED_CAPACITY_SHIFT, nominal_perf);
-	max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
+	max_freq = div_u64(numerator * nominal_freq * 1000, nominal_perf);

This doesn't apply currently, because of some changes in the superm1.git/linux-next branch; specifically:

https://git.kernel.org/superm1/c/68cb0e77b6439

I haven't sent this out to linux-pm yet so it could be in linux-next, but will be doing that soon. So can you please rebase on that branch if this change still makes sense?

Quoted 3 lines
drivers/cpufreq/amd-pstate.c +0 -0
  	lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
  	lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
#2 lore raw

Hi ,

Quoted 6 lines · Mario Limonciello
Quoted 1 line · Naresh Solanki

Refactor to calculate max-freq more accurately.

Can you add some more detail about what you're finding? What was it before, what is it now, why is it more accurate?

Sure.
The previous approach had some roundoff error due to division compute when calculating boost ratio.
Which intern affected max-freq calculation resulting in reporting lower value In my Glinda SoC board,
See below calculations with below values:
max_perf = 208
nominal_perf = 100
normal_freq = 2600

How linux kernel calculates:
freq = ( (max_perf * 1024 / nominal_perf) * normal_freq) / 1024 freq = 5405 // Integer arithmatic.

With the changes
freq = (max_perf * normal_freq) / nominal_perf
freq = 5408

Quoted 31 lines
Quoted 21 lines
---
  drivers/cpufreq/amd-pstate.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cpufreq/amd-pstate.c +1 -1
index d7630bab2516..78a2cbd14952 100644
@@ -892,7 +892,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
      u64 numerator;
      u32 nominal_perf, nominal_freq;
      u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
-     u32 boost_ratio, lowest_nonlinear_ratio;
+     u32 lowest_nonlinear_ratio;
      struct cppc_perf_caps cppc_perf;
 
      ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);

drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 917

Quoted 6 lines
drivers/cpufreq/amd-pstate.c +1 -2
@@ -914,8 +914,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
      ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
      if (ret)
              return ret;
-     boost_ratio = div_u64(numerator << SCHED_CAPACITY_SHIFT, nominal_perf);
-     max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
+     max_freq = div_u64(numerator * nominal_freq * 1000, nominal_perf);

This doesn't apply currently, because of some changes in the superm1.git/linux-next branch; specifically:

Quoted 5 lines

https://git.kernel.org/superm1/c/68cb0e77b6439

I haven't sent this out to linux-pm yet so it could be in linux-next, but will be doing that soon. So can you please rebase on that branch if this change still makes sense?

Sure. Will do

Regards,
Naresh Solanki

Quoted 5 lines
Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0 -0
      lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
      lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
#3 lore raw
Quoted 20 lines · Naresh Solanki

Hi ,

Quoted 6 lines · Mario Limonciello
Quoted 1 line · Naresh Solanki

Refactor to calculate max-freq more accurately.

Can you add some more detail about what you're finding? What was it before, what is it now, why is it more accurate?

Sure.
The previous approach had some roundoff error due to division compute when calculating boost ratio.
Which intern affected max-freq calculation resulting in reporting lower value In my Glinda SoC board,
See below calculations with below values:
max_perf = 208
nominal_perf = 100
normal_freq = 2600

Quoted 9 lines

How linux kernel calculates:
freq = ( (max_perf * 1024 / nominal_perf) * normal_freq) / 1024 freq = 5405 // Integer arithmatic.

With the changes
freq = (max_perf * normal_freq) / nominal_perf
freq = 5408

Ah, thanks! Please include some of this info in the commit message for the next version.

Quoted 32 lines
Quoted 32 lines
Quoted 22 lines
---
   drivers/cpufreq/amd-pstate.c | 5 ++---
   1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cpufreq/amd-pstate.c +1 -1
index d7630bab2516..78a2cbd14952 100644
@@ -892,7 +892,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
       u64 numerator;
       u32 nominal_perf, nominal_freq;
       u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
-     u32 boost_ratio, lowest_nonlinear_ratio;
+     u32 lowest_nonlinear_ratio;
       struct cppc_perf_caps cppc_perf;
 
       ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);

drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 917

Quoted 6 lines
drivers/cpufreq/amd-pstate.c +1 -2
@@ -914,8 +914,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
       ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
       if (ret)
               return ret;
-     boost_ratio = div_u64(numerator << SCHED_CAPACITY_SHIFT, nominal_perf);
-     max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
+     max_freq = div_u64(numerator * nominal_freq * 1000, nominal_perf);

This doesn't apply currently, because of some changes in the superm1.git/linux-next branch; specifically:

Quoted 7 lines
Quoted 5 lines

https://git.kernel.org/superm1/c/68cb0e77b6439

I haven't sent this out to linux-pm yet so it could be in linux-next, but will be doing that soon. So can you please rebase on that branch if this change still makes sense?

Sure. Will do

Thanks!

Quoted 7 lines

Regards,
Naresh Solanki

Quoted 5 lines
Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0 -0
       lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
       lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
#4 lore raw

Hi Mario

Quoted 26 lines · Mario Limonciello
Quoted 24 lines · Naresh Solanki

Hi ,

Quoted 6 lines · Mario Limonciello
Quoted 1 line · Naresh Solanki

Refactor to calculate max-freq more accurately.

Can you add some more detail about what you're finding? What was it before, what is it now, why is it more accurate?

Sure.
The previous approach had some roundoff error due to division compute when calculating boost ratio.
Which intern affected max-freq calculation resulting in reporting lower value In my Glinda SoC board,
See below calculations with below values:
max_perf = 208
nominal_perf = 100
normal_freq = 2600

How linux kernel calculates:
freq = ( (max_perf * 1024 / nominal_perf) * normal_freq) / 1024 freq = 5405 // Integer arithmatic.

Quoted 8 lines
Quoted 5 lines

With the changes
freq = (max_perf * normal_freq) / nominal_perf
freq = 5408

Ah, thanks! Please include some of this info in the commit message for the next version.

Sure

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 23 lines
---
   drivers/cpufreq/amd-pstate.c | 5 ++---
   1 file changed, 2 insertions(+), 3 deletions(-)
drivers/cpufreq/amd-pstate.c +1 -1
index d7630bab2516..78a2cbd14952 100644
@@ -892,7 +892,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
       u64 numerator;
       u32 nominal_perf, nominal_freq;
       u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
-     u32 boost_ratio, lowest_nonlinear_ratio;
+     u32 lowest_nonlinear_ratio;
       struct cppc_perf_caps cppc_perf;
 
       ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);

drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 917

Quoted 6 lines
drivers/cpufreq/amd-pstate.c +1 -2
@@ -914,8 +914,7 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
       ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
       if (ret)
               return ret;
-     boost_ratio = div_u64(numerator << SCHED_CAPACITY_SHIFT, nominal_perf);
-     max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
+     max_freq = div_u64(numerator * nominal_freq * 1000, nominal_perf);

This doesn't apply currently, because of some changes in the superm1.git/linux-next branch; specifically:

https://git.kernel.org/superm1/c/68cb0e77b6439

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I haven't sent this out to linux-pm yet so it could be in linux-next, but will be doing that soon. So can you please rebase on that branch if this change still makes sense?

Sure. Will do

Thanks!

Regards,
Naresh

Quoted 9 lines
Quoted 8 lines

Regards,
Naresh Solanki

Quoted 5 lines
Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0 -0
       lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
       lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,