[PATCH v3] cpufreq/amd-pstate: Cache the max frequency in cpudata

Mario Limonciello posted 1 patch 6 days, 21 hours ago
drivers/cpufreq/amd-pstate.c | 27 +++++++++------------------
drivers/cpufreq/amd-pstate.h |  2 ++
2 files changed, 11 insertions(+), 18 deletions(-)
[PATCH v3] cpufreq/amd-pstate: Cache the max frequency in cpudata
Posted by Mario Limonciello 6 days, 21 hours ago
From: "Mario Limonciello (AMD)" <superm1@kernel.org>

The value of maximum frequency is fixed and never changes. Doing
calculations every time based off of perf is unnecessary.

Signed-off-by: Mario Limonciello (AMD) <superm1@kernel.org>
---
v3:
 * rebase on Gautham's CPPC perf priority series
 * add kdoc
---
 drivers/cpufreq/amd-pstate.c | 27 +++++++++------------------
 drivers/cpufreq/amd-pstate.h |  2 ++
 2 files changed, 11 insertions(+), 18 deletions(-)

diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index ed9fd4155a256..f207252eb5f5f 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -826,15 +826,13 @@ static void amd_pstate_adjust_perf(unsigned int cpu,
 static int amd_pstate_cpu_boost_update(struct cpufreq_policy *policy, bool on)
 {
 	struct amd_cpudata *cpudata = policy->driver_data;
-	union perf_cached perf = READ_ONCE(cpudata->perf);
-	u32 nominal_freq, max_freq;
+	u32 nominal_freq;
 	int ret = 0;
 
 	nominal_freq = READ_ONCE(cpudata->nominal_freq);
-	max_freq = perf_to_freq(perf, cpudata->nominal_freq, perf.highest_perf);
 
 	if (on)
-		policy->cpuinfo.max_freq = max_freq;
+		policy->cpuinfo.max_freq = cpudata->max_freq;
 	else if (policy->cpuinfo.max_freq > nominal_freq)
 		policy->cpuinfo.max_freq = nominal_freq;
 
@@ -1021,13 +1019,15 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
 
 	WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
 
+	/* max_freq is calculated according to (nominal_freq * highest_perf)/nominal_perf */
 	max_freq = perf_to_freq(perf, nominal_freq, perf.highest_perf);
+	WRITE_ONCE(cpudata->max_freq, max_freq);
+
 	lowest_nonlinear_freq = perf_to_freq(perf, nominal_freq, perf.lowest_nonlinear_perf);
 	WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
 
 	/**
 	 * Below values need to be initialized correctly, otherwise driver will fail to load
-	 * max_freq is calculated according to (nominal_freq * highest_perf)/nominal_perf
 	 * lowest_nonlinear_freq is a value between [min_freq, nominal_freq]
 	 * Check _CPC in ACPI table objects if any values are incorrect
 	 */
@@ -1090,9 +1090,7 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
 	policy->cpuinfo.min_freq = policy->min = perf_to_freq(perf,
 							      cpudata->nominal_freq,
 							      perf.lowest_perf);
-	policy->cpuinfo.max_freq = policy->max = perf_to_freq(perf,
-							      cpudata->nominal_freq,
-							      perf.highest_perf);
+	policy->cpuinfo.max_freq = policy->max = cpudata->max_freq;
 
 	policy->driver_data = cpudata;
 	ret = amd_pstate_cppc_enable(policy);
@@ -1167,14 +1165,9 @@ static void amd_pstate_cpu_exit(struct cpufreq_policy *policy)
 static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
 					char *buf)
 {
-	struct amd_cpudata *cpudata;
-	union perf_cached perf;
-
-	cpudata = policy->driver_data;
-	perf = READ_ONCE(cpudata->perf);
+	struct amd_cpudata *cpudata = policy->driver_data;
 
-	return sysfs_emit(buf, "%u\n",
-			  perf_to_freq(perf, cpudata->nominal_freq, perf.highest_perf));
+	return sysfs_emit(buf, "%u\n", cpudata->max_freq);
 }
 
 static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
@@ -1702,9 +1695,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	policy->cpuinfo.min_freq = policy->min = perf_to_freq(perf,
 							      cpudata->nominal_freq,
 							      perf.lowest_perf);
-	policy->cpuinfo.max_freq = policy->max = perf_to_freq(perf,
-							      cpudata->nominal_freq,
-							      perf.highest_perf);
+	policy->cpuinfo.max_freq = policy->max = cpudata->max_freq;
 	policy->driver_data = cpudata;
 
 	ret = amd_pstate_cppc_enable(policy);
diff --git a/drivers/cpufreq/amd-pstate.h b/drivers/cpufreq/amd-pstate.h
index faead0b19a8a4..32b8b26ce388f 100644
--- a/drivers/cpufreq/amd-pstate.h
+++ b/drivers/cpufreq/amd-pstate.h
@@ -73,6 +73,7 @@ struct amd_aperf_mperf {
  * @min_limit_freq: Cached value of policy->min (in khz)
  * @max_limit_freq: Cached value of policy->max (in khz)
  * @nominal_freq: the frequency (in khz) that mapped to nominal_perf
+ * @max_freq: in ideal conditions the maximum frequency (in khz) possible frequency
  * @lowest_nonlinear_freq: the frequency (in khz) that mapped to lowest_nonlinear_perf
  * @floor_freq: Cached value of the user requested floor_freq
  * @cur: Difference of Aperf/Mperf/tsc count between last and current sample
@@ -103,6 +104,7 @@ struct amd_cpudata {
 	u32	min_limit_freq;
 	u32	max_limit_freq;
 	u32	nominal_freq;
+	u32	max_freq;
 	u32	lowest_nonlinear_freq;
 	u32	floor_freq;
 
-- 
2.53.0
Re: [PATCH v3] cpufreq/amd-pstate: Cache the max frequency in cpudata
Posted by Gautham R. Shenoy 2 days, 8 hours ago
Hello Mario,

On Thu, Mar 26, 2026 at 02:36:20PM -0500, Mario Limonciello wrote:
> From: "Mario Limonciello (AMD)" <superm1@kernel.org>
> 
> The value of maximum frequency is fixed and never changes. Doing
> calculations every time based off of perf is unnecessary.
> 
> Signed-off-by: Mario Limonciello (AMD) <superm1@kernel.org>

This one looks good to me. Thanks for fixing this.

Reviewed-by: Gautham R. Shenoy <gautham.shenoy@amd.com>

-- 
Thanks and Regards
gautham.


> ---
> v3:
>  * rebase on Gautham's CPPC perf priority series
>  * add kdoc
> ---
>  drivers/cpufreq/amd-pstate.c | 27 +++++++++------------------
>  drivers/cpufreq/amd-pstate.h |  2 ++
>  2 files changed, 11 insertions(+), 18 deletions(-)
> 
> diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
> index ed9fd4155a256..f207252eb5f5f 100644
> --- a/drivers/cpufreq/amd-pstate.c
> +++ b/drivers/cpufreq/amd-pstate.c
> @@ -826,15 +826,13 @@ static void amd_pstate_adjust_perf(unsigned int cpu,
>  static int amd_pstate_cpu_boost_update(struct cpufreq_policy *policy, bool on)
>  {
>  	struct amd_cpudata *cpudata = policy->driver_data;
> -	union perf_cached perf = READ_ONCE(cpudata->perf);
> -	u32 nominal_freq, max_freq;
> +	u32 nominal_freq;
>  	int ret = 0;
>  
>  	nominal_freq = READ_ONCE(cpudata->nominal_freq);
> -	max_freq = perf_to_freq(perf, cpudata->nominal_freq, perf.highest_perf);
>  
>  	if (on)
> -		policy->cpuinfo.max_freq = max_freq;
> +		policy->cpuinfo.max_freq = cpudata->max_freq;
>  	else if (policy->cpuinfo.max_freq > nominal_freq)
>  		policy->cpuinfo.max_freq = nominal_freq;
>  
> @@ -1021,13 +1019,15 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
>  
>  	WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
>  
> +	/* max_freq is calculated according to (nominal_freq * highest_perf)/nominal_perf */
>  	max_freq = perf_to_freq(perf, nominal_freq, perf.highest_perf);
> +	WRITE_ONCE(cpudata->max_freq, max_freq);
> +
>  	lowest_nonlinear_freq = perf_to_freq(perf, nominal_freq, perf.lowest_nonlinear_perf);
>  	WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
>  
>  	/**
>  	 * Below values need to be initialized correctly, otherwise driver will fail to load
> -	 * max_freq is calculated according to (nominal_freq * highest_perf)/nominal_perf
>  	 * lowest_nonlinear_freq is a value between [min_freq, nominal_freq]
>  	 * Check _CPC in ACPI table objects if any values are incorrect
>  	 */
> @@ -1090,9 +1090,7 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
>  	policy->cpuinfo.min_freq = policy->min = perf_to_freq(perf,
>  							      cpudata->nominal_freq,
>  							      perf.lowest_perf);
> -	policy->cpuinfo.max_freq = policy->max = perf_to_freq(perf,
> -							      cpudata->nominal_freq,
> -							      perf.highest_perf);
> +	policy->cpuinfo.max_freq = policy->max = cpudata->max_freq;
>  
>  	policy->driver_data = cpudata;
>  	ret = amd_pstate_cppc_enable(policy);
> @@ -1167,14 +1165,9 @@ static void amd_pstate_cpu_exit(struct cpufreq_policy *policy)
>  static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
>  					char *buf)
>  {
> -	struct amd_cpudata *cpudata;
> -	union perf_cached perf;
> -
> -	cpudata = policy->driver_data;
> -	perf = READ_ONCE(cpudata->perf);
> +	struct amd_cpudata *cpudata = policy->driver_data;
>  
> -	return sysfs_emit(buf, "%u\n",
> -			  perf_to_freq(perf, cpudata->nominal_freq, perf.highest_perf));
> +	return sysfs_emit(buf, "%u\n", cpudata->max_freq);
>  }
>  
>  static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
> @@ -1702,9 +1695,7 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
>  	policy->cpuinfo.min_freq = policy->min = perf_to_freq(perf,
>  							      cpudata->nominal_freq,
>  							      perf.lowest_perf);
> -	policy->cpuinfo.max_freq = policy->max = perf_to_freq(perf,
> -							      cpudata->nominal_freq,
> -							      perf.highest_perf);
> +	policy->cpuinfo.max_freq = policy->max = cpudata->max_freq;
>  	policy->driver_data = cpudata;
>  
>  	ret = amd_pstate_cppc_enable(policy);
> diff --git a/drivers/cpufreq/amd-pstate.h b/drivers/cpufreq/amd-pstate.h
> index faead0b19a8a4..32b8b26ce388f 100644
> --- a/drivers/cpufreq/amd-pstate.h
> +++ b/drivers/cpufreq/amd-pstate.h
> @@ -73,6 +73,7 @@ struct amd_aperf_mperf {
>   * @min_limit_freq: Cached value of policy->min (in khz)
>   * @max_limit_freq: Cached value of policy->max (in khz)
>   * @nominal_freq: the frequency (in khz) that mapped to nominal_perf
> + * @max_freq: in ideal conditions the maximum frequency (in khz) possible frequency
>   * @lowest_nonlinear_freq: the frequency (in khz) that mapped to lowest_nonlinear_perf
>   * @floor_freq: Cached value of the user requested floor_freq
>   * @cur: Difference of Aperf/Mperf/tsc count between last and current sample
> @@ -103,6 +104,7 @@ struct amd_cpudata {
>  	u32	min_limit_freq;
>  	u32	max_limit_freq;
>  	u32	nominal_freq;
> +	u32	max_freq;
>  	u32	lowest_nonlinear_freq;
>  	u32	floor_freq;
>  
> -- 
> 2.53.0
>
Re: [PATCH v3] cpufreq/amd-pstate: Cache the max frequency in cpudata
Posted by Mario Limonciello 2 days, 5 hours ago

On 3/31/26 4:12 AM, Gautham R. Shenoy wrote:
> Hello Mario,
> 
> On Thu, Mar 26, 2026 at 02:36:20PM -0500, Mario Limonciello wrote:
>> From: "Mario Limonciello (AMD)" <superm1@kernel.org>
>>
>> The value of maximum frequency is fixed and never changes. Doing
>> calculations every time based off of perf is unnecessary.
>>
>> Signed-off-by: Mario Limonciello (AMD) <superm1@kernel.org>
> 
> This one looks good to me. Thanks for fixing this.
> 
> Reviewed-by: Gautham R. Shenoy <gautham.shenoy@amd.com>
> 

Thanks, I've applied it.