linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Gautham R. Shenoy" <gautham.shenoy@amd.com>
To: Dhananjay Ugwekar <dhananjay.ugwekar@amd.com>
Cc: mario.limonciello@amd.com, rafael@kernel.org,
	viresh.kumar@linaro.org, linux-kernel@vger.kernel.org,
	linux-pm@vger.kernel.org
Subject: Re: [PATCH 07/12] cpufreq/amd-pstate: Modularize perf<->freq conversion
Date: Mon, 10 Feb 2025 15:21:40 +0530	[thread overview]
Message-ID: <Z6nMLKNBkVgYlnNb@BLRRASHENOY1.amd.com> (raw)
In-Reply-To: <20250205112523.201101-8-dhananjay.ugwekar@amd.com>

Hello Dhananjay,

On Wed, Feb 05, 2025 at 11:25:18AM +0000, Dhananjay Ugwekar wrote:
> Delegate the perf<->frequency conversion to helper functions to reduce
> code duplication, and improve readability.


This is better than using the max_freq/max_perf as the baseline since
these values can change when boost is enabled/disabled.

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

-- 
Thanks and Regards
gautham.

> 
> Signed-off-by: Dhananjay Ugwekar <dhananjay.ugwekar@amd.com>
> ---
>  drivers/cpufreq/amd-pstate.c | 57 +++++++++++++++++++-----------------
>  1 file changed, 30 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
> index dd4f23fa2587..346fac646eba 100644
> --- a/drivers/cpufreq/amd-pstate.c
> +++ b/drivers/cpufreq/amd-pstate.c
> @@ -142,6 +142,20 @@ static struct quirk_entry quirk_amd_7k62 = {
>  	.lowest_freq = 550,
>  };
>  
> +static inline u8 freq_to_perf(struct amd_cpudata *cpudata, unsigned int freq_val)
> +{
> +	u8 perf_val = DIV_ROUND_UP_ULL((u64)freq_val * cpudata->nominal_perf,
> +					cpudata->nominal_freq);
> +
> +	return clamp_t(u8, perf_val, cpudata->lowest_perf, cpudata->highest_perf);
> +}
> +
> +static inline u32 perf_to_freq(struct amd_cpudata *cpudata, u8 perf_val)
> +{
> +	return DIV_ROUND_UP_ULL((u64)cpudata->nominal_freq * perf_val,
> +				cpudata->nominal_perf);
> +}
> +
>  static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
>  {
>  	/**
> @@ -534,14 +548,12 @@ static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
>  static void amd_pstate_update(struct amd_cpudata *cpudata, u8 min_perf,
>  			      u8 des_perf, u8 max_perf, bool fast_switch, int gov_flags)
>  {
> -	unsigned long max_freq;
>  	struct cpufreq_policy *policy = cpufreq_cpu_get(cpudata->cpu);
>  	u8 nominal_perf = READ_ONCE(cpudata->nominal_perf);
>  
>  	des_perf = clamp_t(u8, des_perf, min_perf, max_perf);
>  
> -	max_freq = READ_ONCE(cpudata->max_limit_freq);
> -	policy->cur = div_u64(des_perf * max_freq, max_perf);
> +	policy->cur = perf_to_freq(cpudata, des_perf);
>  
>  	if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
>  		min_perf = des_perf;
> @@ -591,14 +603,11 @@ static int amd_pstate_verify(struct cpufreq_policy_data *policy_data)
>  
>  static int amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
>  {
> -	u8 max_limit_perf, min_limit_perf, max_perf;
> -	u32 max_freq;
> +	u8 max_limit_perf, min_limit_perf;
>  	struct amd_cpudata *cpudata = policy->driver_data;
>  
> -	max_perf = READ_ONCE(cpudata->highest_perf);
> -	max_freq = READ_ONCE(cpudata->max_freq);
> -	max_limit_perf = div_u64(policy->max * max_perf, max_freq);
> -	min_limit_perf = div_u64(policy->min * max_perf, max_freq);
> +	max_limit_perf = freq_to_perf(cpudata, policy->max);
> +	min_limit_perf = freq_to_perf(cpudata, policy->min);
>  
>  	if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
>  		min_limit_perf = min(cpudata->nominal_perf, max_limit_perf);
> @@ -616,21 +625,15 @@ static int amd_pstate_update_freq(struct cpufreq_policy *policy,
>  {
>  	struct cpufreq_freqs freqs;
>  	struct amd_cpudata *cpudata = policy->driver_data;
> -	u8 des_perf, cap_perf;
> -
> -	if (!cpudata->max_freq)
> -		return -ENODEV;
> +	u8 des_perf;
>  
>  	if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
>  		amd_pstate_update_min_max_limit(policy);
>  
> -	cap_perf = READ_ONCE(cpudata->highest_perf);
> -
>  	freqs.old = policy->cur;
>  	freqs.new = target_freq;
>  
> -	des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
> -				     cpudata->max_freq);
> +	des_perf = freq_to_perf(cpudata, target_freq);
>  
>  	WARN_ON(fast_switch && !policy->fast_switch_enabled);
>  	/*
> @@ -904,7 +907,6 @@ static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
>  {
>  	int ret;
>  	u32 min_freq, max_freq;
> -	u8 highest_perf, nominal_perf, lowest_nonlinear_perf;
>  	u32 nominal_freq, lowest_nonlinear_freq;
>  	struct cppc_perf_caps cppc_perf;
>  
> @@ -922,16 +924,17 @@ 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);
> -	max_freq = div_u64((u64)highest_perf * nominal_freq, nominal_perf);
> +	min_freq *= 1000;
> +	nominal_freq *= 1000;
> +
> +	WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
> +	WRITE_ONCE(cpudata->min_freq, min_freq);
> +
> +	max_freq = perf_to_freq(cpudata, cpudata->highest_perf);
> +	lowest_nonlinear_freq = perf_to_freq(cpudata, cpudata->lowest_nonlinear_perf);
>  
> -	lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
> -	lowest_nonlinear_freq = div_u64((u64)nominal_freq * lowest_nonlinear_perf, nominal_perf);
> -	WRITE_ONCE(cpudata->min_freq, min_freq * 1000);
> -	WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq * 1000);
> -	WRITE_ONCE(cpudata->nominal_freq, nominal_freq * 1000);
> -	WRITE_ONCE(cpudata->max_freq, max_freq * 1000);
> +	WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
> +	WRITE_ONCE(cpudata->max_freq, max_freq);
>  
>  	/**
>  	 * Below values need to be initialized correctly, otherwise driver will fail to load
> -- 
> 2.34.1
> 

  parent reply	other threads:[~2025-02-10  9:51 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-05 11:25 [PATCH 00/12] cpufreq/amd-pstate: Fixes and optimizations Dhananjay Ugwekar
2025-02-05 11:25 ` [PATCH 01/12] cpufreq/amd-pstate: Remove the goto label in amd_pstate_update_limits Dhananjay Ugwekar
2025-02-05 17:40   ` Mario Limonciello
2025-02-10  5:12   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 02/12] cpufreq/amd-pstate: Fix max_perf updation with schedutil Dhananjay Ugwekar
2025-02-05 17:40   ` Mario Limonciello
2025-02-10  5:13   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 03/12] cpufreq/amd-pstate: Modify the min_perf calculation in adjust_perf callback Dhananjay Ugwekar
2025-02-05 17:45   ` Mario Limonciello
2025-02-10  9:45   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 04/12] cpufreq/amd-pstate: Remove the redundant des_perf clamping in adjust_perf Dhananjay Ugwekar
2025-02-05 17:45   ` Mario Limonciello
2025-02-10  9:46   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 05/12] cpufreq/amd-pstate: Pass min/max_limit_perf as min/max_perf to amd_pstate_update Dhananjay Ugwekar
2025-02-05 17:45   ` Mario Limonciello
2025-02-10  9:47   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 06/12] cpufreq/amd-pstate: Convert all perf values to u8 Dhananjay Ugwekar
2025-02-05 17:46   ` Mario Limonciello
2025-02-10  9:48   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 07/12] cpufreq/amd-pstate: Modularize perf<->freq conversion Dhananjay Ugwekar
2025-02-05 17:46   ` Mario Limonciello
2025-02-10  9:51   ` Gautham R. Shenoy [this message]
2025-02-05 11:25 ` [PATCH 08/12] cpufreq/amd-pstate: Remove the unnecessary cpufreq_update_policy call Dhananjay Ugwekar
2025-02-05 17:46   ` Mario Limonciello
2025-02-10  9:52   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 09/12] cpufreq/amd-pstate: Fix cpufreq_policy ref counting Dhananjay Ugwekar
2025-02-05 17:48   ` Mario Limonciello
2025-02-10 10:52   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 10/12] cpufreq/amd-pstate: Add missing NULL ptr check in amd_pstate_update Dhananjay Ugwekar
2025-02-05 17:48   ` Mario Limonciello
2025-02-10 10:58   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 11/12] cpufreq/amd-pstate: Use scope based cleanup for cpufreq_policy refs Dhananjay Ugwekar
2025-02-05 17:50   ` Mario Limonciello
2025-02-10 11:11   ` Gautham R. Shenoy
2025-02-05 11:25 ` [PATCH 12/12] cpufreq/amd-pstate: Remove the unncecessary driver_lock in amd_pstate_update_limits Dhananjay Ugwekar
2025-02-05 17:50   ` Mario Limonciello
2025-02-10 11:12   ` Gautham R. Shenoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z6nMLKNBkVgYlnNb@BLRRASHENOY1.amd.com \
    --to=gautham.shenoy@amd.com \
    --cc=dhananjay.ugwekar@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=rafael@kernel.org \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).