cpufreq/amd-pstate: Refactor max frequency calculation
Subsystems (2)
PATCH v2 1/1 cpufreq/amd-pstate: Refactor max frequency calculation
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf = 208nominal_perf = 100nominal_freq = 2600 MHz
The Linux kernel previously calculated the frequency as:
freq = ((max_perf * 1024 / nominal_perf) * nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.
With the updated formula:
freq = (max_perf * nominal_freq) / nominal_perf
freq = 5408 MHz
This change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Changes in V2:
1. Rebase on superm1.git/linux-next branch
--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/cpufreq/amd-pstate.c +4 -5
@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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);
@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ); + max_freq =div_u64 ((u64 )highest_perf * nominal_freq, 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
Mario Limonciello
maintainerQuoted 16 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.
Quoted 8 lines
With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Signed-off-by Naresh Solanki <[email protected]>
Thanks, this makes sense to me.
But looking at it, we should have the same problem with lowest nonlinear freq as it goes through the same conversion process. Would you mind fixing that one too?
Gautham, Perry,
Is there something non-obvious I'm missing about why it was done this way? It looks like it's been there since the start.
Quoted 36 lines
Changes in V2:
1. Rebase on superm1.git/linux-next branch--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +4
-5@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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);@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ); + max_freq =div_u64 ((u64 )highest_perf * nominal_freq, nominal_perf); lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
Hi Mario,
Quoted 25 lines · Mario Limonciello
Quoted 23 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Quoted 7 lines
Quoted 1 line
Signed-off-by Naresh Solanki <[email protected]>Thanks, this makes sense to me.
But looking at it, we should have the same problem with lowest nonlinear freq as it goes through the same conversion process. Would you mind fixing that one too?
Sure. Somehow my eyes missed that.
Also observed that current calculations yields zero for lowest_nonlinear_freq. After fixing that, it reported frequency 2002 & 1404 depending on the core.
On a side note, I'm also observing that the highest_perf is set to 196 which should not have been the case as I do have cores with value 208. Seems like amd_get_boost_ratio_numerator needs some addressing for that.
Regards,
Naresh
Quoted 43 lines
Gautham, Perry,
Is there something non-obvious I'm missing about why it was done this way? It looks like it's been there since the start.
Quoted 36 lines
Changes in V2:
1. Rebase on superm1.git/linux-next branch--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +4
-5@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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);@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ); + max_freq =div_u64 ((u64 )highest_perf * nominal_freq, nominal_perf); lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
Hi Mario,
Quoted 32 lines · Naresh Solanki
Hi Mario,
Quoted 27 lines · Mario Limonciello
Quoted 24 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Signed-off-by Naresh Solanki <[email protected]>
Quoted 7 lines
Quoted 5 lines
Thanks, this makes sense to me.
But looking at it, we should have the same problem with lowest nonlinear freq as it goes through the same conversion process. Would you mind fixing that one too?
Sure. Somehow my eyes missed that.
Also observed that current calculations yields zero forlowest_nonlinear_freq.
Sorry I was wrong. it's not zero. Its roundoff version.
Quoted 51 lines
After fixing that, it reported frequency 2002 & 1404 depending on the core.
On a side note, I'm also observing that the
highest_perfis set to 196 which should not have been the case as I do have cores with value 208. Seems likeamd_get_boost_ratio_numeratorneeds some addressing for that.Regards,
NareshQuoted 43 lines
Gautham, Perry,
Is there something non-obvious I'm missing about why it was done this way? It looks like it's been there since the start.
Quoted 36 lines
Changes in V2:
1. Rebase on superm1.git/linux-next branch--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +4
-5@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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);@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ); + max_freq =div_u64 ((u64 )highest_perf * nominal_freq, nominal_perf); lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
Mario Limonciello
maintainerQuoted 36 lines · Naresh Solanki
Hi Mario,
Quoted 32 lines · Naresh Solanki
Hi Mario,
Quoted 27 lines · Mario Limonciello
Quoted 24 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Signed-off-by Naresh Solanki <[email protected]>
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
Thanks, this makes sense to me.
But looking at it, we should have the same problem with lowest nonlinear freq as it goes through the same conversion process. Would you mind fixing that one too?
Sure. Somehow my eyes missed that.
Also observed that current calculations yields zero forlowest_nonlinear_freq.Sorry I was wrong. it's not zero. Its roundoff version.
Quoted 1 line
After fixing that, it reported frequency 2002 & 1404 depending on the core.
Mmm, I wouldn't expect that. Is your APU heterogenous? Or is this a BIOS bug?
Both with your v3 patch in place and your patch not in place can you send me the report generated from:
https://gitlab.freedesktop.org/drm/amd/-/blob/master/scripts/amd-pstate-triage.py
Quoted 4 lines
Quoted 4 lines
On a side note, I'm also observing that the
highest_perfis set to 196 which should not have been the case as I do have cores with value 208. Seems likeamd_get_boost_ratio_numeratorneeds some addressing for that.
Ah this is something that is quite confusing about how AMD client CPUs behave. There is a feature called "Preferred cores" that uses the highest performance value to indicate rankings of the cores. This means that you can't use the value in this register to calculate the frequency, you have to use the pre-defined scaling factor.
The scaling factor is listed in arch/x86/kernel/acpi/cppc.c and the correct one is fetched for this calculation.
Quoted 46 lines
Quoted 46 lines
Regards,
NareshQuoted 43 lines
Gautham, Perry,
Is there something non-obvious I'm missing about why it was done this way? It looks like it's been there since the start.
Quoted 36 lines
Changes in V2:
1. Rebase on superm1.git/linux-next branch--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +4
-5@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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);@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ); + max_freq =div_u64 ((u64 )highest_perf * nominal_freq, nominal_perf); lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
Hi Mario
Based on linux-next:
2024-12-20 11:02:53,078 INFO: 🔋 Kernel sysfs files
+---------+----------------+----------------------+----------------+--------------------+--------------------+---------------------------------+------------+---------+
| CPU # | CPU Min Freq | CPU Nonlinear Freq | CPU Max Freq |
Scaling Min Freq | Scaling Max Freq | Energy Performance Preference
| Prefcore | Boost |
|---------+----------------+----------------------+----------------+--------------------+--------------------+---------------------------------+------------+---------|
| 0 | 599000 | 2000000 | 5095000 |
2000000 | 5095000 | performance
| 208 | 1 |
| 1 | 599000 | 2000000 | 5095000 |
2000000 | 5095000 | performance
| 196 | 1 |
| 2 | 599000 | 2000000 | 5095000 |
2000000 | 5095000 | performance
| 202 | 1 |
| 3 | 599000 | 2000000 | 5095000 |
2000000 | 5095000 | performance
| 208 | 1 |
| 4 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 5 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 6 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 7 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 8 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 9 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 10 | 599000 | 2000000 | 5095000 |
2000000 | 5095000 | performance
| 208 | 1 |
| 11 | 599000 | 2000000 | 5095000 |
2000000 | 5095000 | performance
| 196 | 1 |
| 12 | 599000 | 2000000 | 5095000 |
2000000 | 5095000 | performance
| 202 | 1 |
| 13 | 599000 | 2000000 | 5095000 |
2000000 | 5095000 | performance
| 208 | 1 |
| 14 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 15 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 16 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 17 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 18 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
| 19 | 599000 | 1401000 | 5095000 |
1401000 | 5095000 | performance
| 128 | 1 |
+---------+----------------+----------------------+----------------+--------------------+--------------------+---------------------------------+------------+---------+Based on V3 changes:
+---------+----------------+----------------------+----------------+--------------------+--------------------+---------------------------------+------------+---------+
| CPU # | CPU Min Freq | CPU Nonlinear Freq | CPU Max Freq |
Scaling Min Freq | Scaling Max Freq | Energy Performance Preference
| Prefcore | Boost |
|---------+----------------+----------------------+----------------+--------------------+--------------------+---------------------------------+------------+---------|
| 0 | 599000 | 2002000 | 5096000 |
2002000 | 5096000 | performance
| 208 | 1 |
| 1 | 599000 | 2002000 | 5096000 |
2002000 | 5096000 | performance
| 196 | 1 |
| 2 | 599000 | 2002000 | 5096000 |
2002000 | 5096000 | performance
| 202 | 1 |
| 3 | 599000 | 2002000 | 5096000 |
2002000 | 5096000 | performance
| 208 | 1 |
| 4 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 5 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 6 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 7 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 8 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 9 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 10 | 599000 | 2002000 | 5096000 |
2002000 | 5096000 | performance
| 208 | 1 |
| 11 | 599000 | 2002000 | 5096000 |
2002000 | 5096000 | performance
| 196 | 1 |
| 12 | 599000 | 2002000 | 5096000 |
2002000 | 5096000 | performance
| 202 | 1 |
| 13 | 599000 | 2002000 | 5096000 |
2002000 | 5096000 | performance
| 208 | 1 |
| 14 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 15 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 16 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 17 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 18 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
| 19 | 599000 | 1404000 | 5096000 |
1404000 | 5096000 | performance
| 128 | 1 |
+---------+----------------+----------------------+----------------+--------------------+--------------------+---------------------------------+------------+---------+Regards,
Naresh
Regards,
Naresh Solanki
9elements GmbH, Kortumstraße 19-21, 44787 Bochum, Germany
Email: [email protected] Mobile: +91 9538631477
Sitz der Gesellschaft: Bochum
Handelsregister: Amtsgericht Bochum, HRB 17519
Geschäftsführung: Sebastian Deutsch, Eray Basar
Datenschutzhinweise nach Art. 13 DSGVO
Quoted 117 lines · Mario Limonciello
Quoted 36 lines · Naresh Solanki
Hi Mario,
Quoted 32 lines · Naresh Solanki
Hi Mario,
Quoted 27 lines · Mario Limonciello
Quoted 24 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Signed-off-by Naresh Solanki <[email protected]>Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
Thanks, this makes sense to me.
But looking at it, we should have the same problem with lowest nonlinear freq as it goes through the same conversion process. Would you mind fixing that one too?
Sure. Somehow my eyes missed that.
Also observed that current calculations yields zero forlowest_nonlinear_freq.Sorry I was wrong. it's not zero. Its roundoff version.
Quoted 1 line
After fixing that, it reported frequency 2002 & 1404 depending on the core.
Mmm, I wouldn't expect that. Is your APU heterogenous? Or is this a BIOS bug?
Both with your v3 patch in place and your patch not in place can you send me the report generated from:
https://gitlab.freedesktop.org/drm/amd/-/blob/master/scripts/amd-pstate-triage.pyQuoted 4 lines
Quoted 4 lines
On a side note, I'm also observing that the
highest_perfis set to 196 which should not have been the case as I do have cores with value 208. Seems likeamd_get_boost_ratio_numeratorneeds some addressing for that.Ah this is something that is quite confusing about how AMD client CPUs behave. There is a feature called "Preferred cores" that uses the highest performance value to indicate rankings of the cores. This means that you can't use the value in this register to calculate the frequency, you have to use the pre-defined scaling factor.
The scaling factor is listed in
arch/x86/kernel/acpi/cppc.cand the correct one is fetched for this calculation.Quoted 46 lines
Quoted 46 lines
Regards,
NareshQuoted 43 lines
Gautham, Perry,
Is there something non-obvious I'm missing about why it was done this way? It looks like it's been there since the start.
Quoted 36 lines
Changes in V2:
1. Rebase on superm1.git/linux-next branch--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +4
-5@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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);@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT ); + max_freq =div_u64 ((u64 )highest_perf * nominal_freq, nominal_perf); lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
Quoted 48 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Signed-off-by Naresh Solanki <[email protected]>Changes in V2:
1. Rebase on superm1.git/linux-next branch--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +2
-2@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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 933
Quoted 8 lines
drivers/cpufreq/amd-pstate.c +1
-3@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT );
The patch looks obviously correct to me. And the suggested method would work because nominal_freq is larger than the nominal_perf and thus scaling is really necessary.
Besides, before this patch, there was another obvious issue that we were computing the boost_ratio when we should have been computing the ratio of nominal_freq and nominal_perf and then multiplied this with max_perf without losing precision.
This is just one instance, but it can be generalized so that any freq --> perf and perf --> freq can be computed without loss of precision.
We need two things:
1. The mult_factor should be computed as a ratio of nominal_freq and nominal_perf (and vice versa) as they are always known.
2. Use DIV64_U64_ROUND_UP instead of div64() which rounds up instead of rounding down.
So if we have the shifts defined as follows:
#define PERF_SHIFT 12UL //shift used for freq --> perf conversion
#define FREQ_SHIFT 10UL //shift used for perf --> freq conversion.
And in amd_pstate_init_freq() code, we initialize the two global variables:
u64 freq_mult_factor = DIV64_U64_ROUND_UP(nominal_freq << FREQ_SHIFT, nominal_perf); u64 perf_mult_factor = DIV64_U64_ROUND_UP(nominal_perf << PERF_SHIFT, nominal_freq);
.. and have a couple of helper functions:
/* perf to freq conversion */
static inline unsigned int perf_to_freq(perf)
{
return (perf * freq_mult_factor) >> FREQ_SHIFT;
}/* freq to perf conversion */
static inline unsigned int freq_to_perf(freq)
{
return (freq * perf_mult_factor) >> PERF_SHIFT;
}drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 932
Quoted 1 line
drivers/cpufreq/amd-pstate.c +1
-0+ max_freq =div_u64 ((u64 )highest_perf * nominal_freq, nominal_perf);
Then,
max_freq = perf_to_freq(highest_perf); min_freq = perf_to_freq(lowest_non_linear_perf);
and so on.
This should just work.
drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 935
Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0
-0lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
--
Thanks and Regards
gautham.
Quoted 80 lines · Gautham R. Shenoy
Quoted 48 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Signed-off-by Naresh Solanki <[email protected]>Changes in V2:
1. Rebase on superm1.git/linux-next branch--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +2
-2@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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 933
Quoted 8 lines
drivers/cpufreq/amd-pstate.c +1
-3@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT );The patch looks obviously correct to me. And the suggested method would work because
nominal_freqis larger than thenominal_perfand thus scaling is really necessary.Besides, before this patch, there was another obvious issue that we were computing the
boost_ratiowhen we should have been computing the ratio ofnominal_freqandnominal_perfand then multiplied this withmax_perfwithout losing precision.This is just one instance, but it can be generalized so that any freq --> perf and perf --> freq can be computed without loss of precision.
We need two things:
1. The
mult_factorshould be computed as a ratio ofnominal_freqandnominal_perf(and vice versa) as they are always known.2. Use
DIV64_U64_ROUND_UPinstead ofdiv64()which rounds up instead of rounding down.So if we have the shifts defined as follows:
Quoted 7 lines
#define
PERF_SHIFT12UL //shift used for freq --> perf conversion
#defineFREQ_SHIFT10UL //shift used for perf --> freq conversion.And in
amd_pstate_init_freq()code, we initialize the two global variables:u64
freq_mult_factor=DIV64_U64_ROUND_UP(nominal_freq << FREQ_SHIFT, nominal_perf); u64perf_mult_factor=DIV64_U64_ROUND_UP(nominal_perf << PERF_SHIFT, nominal_freq);
I like this approach, but can we assume the nominal freq/perf values to be the same for all CPUs, otherwise we would need to make these factors a per-CPU or per-domain(where all CPUs within a "domain" have the same nominal_freq/perf). At which point the benefit of caching these ratios might diminish.
Thoughts, Gautham, Mario?
Thanks,
Dhananjay
Quoted 37 lines
.. and have a couple of helper functions:
/* perf to freq conversion */
static inline unsigned intperf_to_freq(perf){ return (perf * freq_mult_factor) >> FREQ_SHIFT; }/* freq to perf conversion */
static inline unsigned intfreq_to_perf(freq){ return (freq * perf_mult_factor) >> PERF_SHIFT; }drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 932
Quoted 1 line
drivers/cpufreq/amd-pstate.c +1
-0+ max_freq =div_u64 ((u64 )highest_perf * nominal_freq, nominal_perf);Then,
max_freq = perf_to_freq(highest_perf); min_freq = perf_to_freq(lowest_non_linear_perf);and so on.
This should just work.
drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 935
Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0
-0lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,--
Thanks and Regards
gautham.
Mario Limonciello
maintainerQuoted 89 lines · Dhananjay Ugwekar
Quoted 80 lines · Gautham R. Shenoy
Quoted 48 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Signed-off-by Naresh Solanki <[email protected]>Changes in V2:
1. Rebase on superm1.git/linux-next branch--- drivers/cpufreq/amd-pstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +2
-2@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {int ret;u32 min_freq, max_freq; -u32 nominal_perf, nominal_freq; +u32 highest_perf, 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 933
Quoted 8 lines
drivers/cpufreq/amd-pstate.c +1
-3@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)else nominal_freq = cppc_perf.nominal_freq; + highest_perf =READ_ONCE (cpudata->highest_perf); nominal_perf =READ_ONCE (cpudata->nominal_perf); - - boost_ratio =div_u64 (cpudata->highest_perf <<SCHED_CAPACITY_SHIFT , nominal_perf); - max_freq = (nominal_freq * boost_ratio >>SCHED_CAPACITY_SHIFT );The patch looks obviously correct to me. And the suggested method would work because
nominal_freqis larger than thenominal_perfand thus scaling is really necessary.Besides, before this patch, there was another obvious issue that we were computing the
boost_ratiowhen we should have been computing the ratio ofnominal_freqandnominal_perfand then multiplied this withmax_perfwithout losing precision.This is just one instance, but it can be generalized so that any freq --> perf and perf --> freq can be computed without loss of precision.
We need two things:
1. The
mult_factorshould be computed as a ratio ofnominal_freqandnominal_perf(and vice versa) as they are always known.2. Use
DIV64_U64_ROUND_UPinstead ofdiv64()which rounds up instead of rounding down.So if we have the shifts defined as follows:
Quoted 7 lines
#define
PERF_SHIFT12UL //shift used for freq --> perf conversion
#defineFREQ_SHIFT10UL //shift used for perf --> freq conversion.And in
amd_pstate_init_freq()code, we initialize the two global variables:u64
freq_mult_factor=DIV64_U64_ROUND_UP(nominal_freq << FREQ_SHIFT, nominal_perf); u64perf_mult_factor=DIV64_U64_ROUND_UP(nominal_perf << PERF_SHIFT, nominal_freq);
Quoted 6 lines
I like this approach, but can we assume the nominal freq/perf values to be the same for all CPUs, otherwise we would need to make these factors a per-CPU or per-domain(where all CPUs within a "domain" have the same nominal_freq/perf). At which point the benefit of caching these ratios might diminish.
Thoughts, Gautham, Mario?
No; in this day of heterogeneous designs I don't think that you can make that assumption, so yes if we had helpers they would have to apply to a group of CPUs, and I agree at that point the caching isn't very beneficial anymore.
If the main argument is to make it easier to follow we could have some macros though?
Quoted 42 lines
Thanks,
DhananjayQuoted 37 lines
.. and have a couple of helper functions:
/* perf to freq conversion */
static inline unsigned intperf_to_freq(perf){ return (perf * freq_mult_factor) >> FREQ_SHIFT; }/* freq to perf conversion */
static inline unsigned intfreq_to_perf(freq){ return (freq * perf_mult_factor) >> PERF_SHIFT; }drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 932
Quoted 1 line
drivers/cpufreq/amd-pstate.c +1
-0+ max_freq =div_u64 ((u64 )highest_perf * nominal_freq, nominal_perf);Then,
max_freq = perf_to_freq(highest_perf); min_freq = perf_to_freq(lowest_non_linear_perf);and so on.
This should just work.
drivers/cpufreq/amd-pstate.c static int amd_pstate_init_freq(struct amd_cpudata *cpudata) · line 935
Quoted 4 lines
drivers/cpufreq/amd-pstate.c +0
-0lowest_nonlinear_perf =READ_ONCE (cpudata->lowest_nonlinear_perf); lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,--
Thanks and Regards
gautham.
Quoted 93 lines · Mario Limonciello
Quoted 92 lines · Dhananjay Ugwekar
Quoted 78 lines · Gautham R. Shenoy
Quoted 47 lines · Naresh Solanki
The previous approach introduced roundoff errors during division when calculating the boost ratio. This, in turn, affected the maximum frequency calculation, often resulting in reporting lower frequency values.
For example, on the Glinda SoC based board with the following parameters:
max_perf= 208nominal_perf= 100nominal_freq= 2600 MHzThe Linux kernel previously calculated the frequency as:
freq = ((max_perf* 1024 /nominal_perf) *nominal_freq) / 1024 freq = 5405 MHz // Integer arithmetic.With the updated formula:
freq = (max_perf*nominal_freq) /nominal_perf
freq = 5408 MHzThis change ensures more accurate frequency calculations by eliminating unnecessary shifts and divisions, thereby improving precision.
Signed-off-by Naresh Solanki <[email protected]>Changes in V2:
1. Rebase on superm1.git/linux-next branch
---
drivers/cpufreq/amd-pstate.c| 9 ++++-----1 file changed, 4 insertions(+), 5 deletions(-)drivers/cpufreq/amd-pstate.c +2
-2@@ -908,9 +908,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata) { int ret; u32 min_freq, max_freq; - u32 nominal_perf, nominal_freq; + u32 highest_perf, 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);@@ -927,10 +927,9 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)Quoted 7 lines
else
nominal_freq= cppc_perf.nominal_freq;
+highest_perf=READ_ONCE(cpudata->highest_perf);
nominal_perf=READ_ONCE(cpudata->nominal_perf);
-
-boost_ratio=div_u64(cpudata->highest_perf << SCHED_CAPACITY_SHIFT, nominal_perf); -max_freq= (nominal_freq*boost_ratio>>SCHED_CAPACITY_SHIFT);The patch looks obviously correct to me. And the suggested method would work because
nominal_freqis larger than thenominal_perfand thus scaling is really necessary.Besides, before this patch, there was another obvious issue that we were computing the
boost_ratiowhen we should have been computing the ratio ofnominal_freqandnominal_perfand then multiplied this withmax_perfwithout losing precision.This is just one instance, but it can be generalized so that any freq --> perf and perf --> freq can be computed without loss of precision.
We need two things:
1. The
mult_factorshould be computed as a ratio ofnominal_freqandnominal_perf(and vice versa) as they are always known.2. Use
DIV64_U64_ROUND_UPinstead ofdiv64()which rounds up instead of rounding down.So if we have the shifts defined as follows:
Quoted 7 lines
#define PERF_SHIFT 12UL //shift used for freq --> perf conversion #define FREQ_SHIFT 10UL //shift used for perf --> freq conversion.
And in
amd_pstate_init_freq()code, we initialize the two global variables:u64
freq_mult_factor=DIV64_U64_ROUND_UP(nominal_freq << FREQ_SHIFT, nominal_perf); u64perf_mult_factor=DIV64_U64_ROUND_UP(nominal_perf << PERF_SHIFT, nominal_freq);I like this approach, but can we assume the nominal freq/perf values to be the same for all CPUs, otherwise we would need to make these factors a per-CPU or per-domain(where all CPUs within a "domain" have the same nominal_freq/perf). At which point the benefit of caching these ratios might diminish.
Quoted 5 lines
Quoted 1 line
Thoughts, Gautham, Mario?
No; in this day of heterogeneous designs I don't think that you can make that assumption, so yes if we had helpers they would have to apply to a group of CPUs, and I agree at that point the caching isn't very beneficial anymore.
If the main argument is to make it easier to follow we could have some macros though?
Agreed, I'm working on the helper functions patchset, will post it shortly.
Quoted 43 lines
Quoted 42 lines
Thanks,
DhananjayQuoted 36 lines
.. and have a couple of helper functions:
/* perf to freq conversion */
static inline unsigned intperf_to_freq(perf){
return (perf *freq_mult_factor) >>FREQ_SHIFT; }/* freq to perf conversion */
static inline unsigned intfreq_to_perf(freq){
return (freq *perf_mult_factor) >>PERF_SHIFT; }Quoted 1 line
+
max_freq=div_u64((u64)highest_perf * nominal_freq, nominal_perf);Then,
max_freq=perf_to_freq(highest_perf);min_freq=perf_to_freq(lowest_non_linear_perf);and so on.
This should just work.
Quoted 3 lines
lowest_nonlinear_perf=READ_ONCE(cpudata->lowest_nonlinear_perf);lowest_nonlinear_ratio= div_u64(lowest_nonlinear_perf <<SCHED_CAPACITY_SHIFT, --Signature · 2 lines
Thanks and Regards gautham.