diff mbox series

[v9,3/8] cpufreq: amd-pstate: Unify computation of {max,min,nominal,lowest_nonlinear}_freq

Message ID a30932a44090a025c73ea0fc81fe0d998e9b6dae.1710836407.git.perry.yuan@amd.com (mailing list archive)
State Superseded, archived
Headers show
Series AMD Pstate Fixes And Enhancements | expand

Commit Message

Yuan, Perry March 19, 2024, 8:28 a.m. UTC
Currently the amd_get_{min, max, nominal, lowest_nonlinear}_freq()
helpers computes the values of min_freq, max_freq, nominal_freq and
lowest_nominal_freq respectively afresh from
cppc_get_perf_caps(). This is not necessary as there are fields in
cpudata to cache these values.

To simplify this, add a single helper function named
amd_pstate_init_freq() which computes all these frequencies at once, and
caches it in cpudata.

Use the cached values everywhere else in the code.

Co-developed-by: Gautham R. Shenoy <gautham.shenoy@amd.com>
Signed-off-by: Gautham R. Shenoy <gautham.shenoy@amd.com>
Signed-off-by: Perry Yuan <perry.yuan@amd.com>
---
 drivers/cpufreq/amd-pstate.c | 126 ++++++++++++++++-------------------
 1 file changed, 59 insertions(+), 67 deletions(-)

Comments

Meng, Li (Jassmine) March 25, 2024, 2:45 a.m. UTC | #1
[AMD Official Use Only - General]

> -----Original Message-----
> From: Yuan, Perry <Perry.Yuan@amd.com>
> Sent: Tuesday, March 19, 2024 4:29 PM
> To: rafael.j.wysocki@intel.com; Limonciello, Mario
> <Mario.Limonciello@amd.com>; viresh.kumar@linaro.org; Shenoy, Gautham
> Ranjal <gautham.shenoy@amd.com>; Petkov, Borislav
> <Borislav.Petkov@amd.com>; Huang, Ray <Ray.Huang@amd.com>
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Huang, Shimmer
> <Shimmer.Huang@amd.com>; oleksandr@natalenko.name; Du, Xiaojian
> <Xiaojian.Du@amd.com>; Meng, Li (Jassmine) <Li.Meng@amd.com>; linux-
> pm@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: [PATCH v9 3/8] cpufreq: amd-pstate: Unify computation of
> {max,min,nominal,lowest_nonlinear}_freq
>
> Currently the amd_get_{min, max, nominal, lowest_nonlinear}_freq()
> helpers computes the values of min_freq, max_freq, nominal_freq and
> lowest_nominal_freq respectively afresh from cppc_get_perf_caps(). This is
> not necessary as there are fields in cpudata to cache these values.
>
> To simplify this, add a single helper function named
> amd_pstate_init_freq() which computes all these frequencies at once, and
> caches it in cpudata.
>
> Use the cached values everywhere else in the code.
>
> Co-developed-by: Gautham R. Shenoy <gautham.shenoy@amd.com>
> Signed-off-by: Gautham R. Shenoy <gautham.shenoy@amd.com>
> Signed-off-by: Perry Yuan <perry.yuan@amd.com>
> ---
>  drivers/cpufreq/amd-pstate.c | 126 ++++++++++++++++-------------------
>  1 file changed, 59 insertions(+), 67 deletions(-)
>
> diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
> index 2015c9fcc3c9..ba1baa6733e6 100644
> --- a/drivers/cpufreq/amd-pstate.c
> +++ b/drivers/cpufreq/amd-pstate.c
> @@ -606,74 +606,22 @@ static void amd_pstate_adjust_perf(unsigned int
> cpu,
>
>  static int amd_get_min_freq(struct amd_cpudata *cpudata)  {
> -     struct cppc_perf_caps cppc_perf;
> -
> -     int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
> -     if (ret)
> -             return ret;
> -
> -     /* Switch to khz */
> -     return cppc_perf.lowest_freq * 1000;
> +     return READ_ONCE(cpudata->min_freq);
>  }
>
>  static int amd_get_max_freq(struct amd_cpudata *cpudata)  {
> -     struct cppc_perf_caps cppc_perf;
> -     u32 max_perf, max_freq, nominal_freq, nominal_perf;
> -     u64 boost_ratio;
> -
> -     int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
> -     if (ret)
> -             return ret;
> -
> -     nominal_freq = cppc_perf.nominal_freq;
> -     nominal_perf = READ_ONCE(cpudata->nominal_perf);
> -     max_perf = READ_ONCE(cpudata->highest_perf);
> -
> -     boost_ratio = div_u64(max_perf << SCHED_CAPACITY_SHIFT,
> -                           nominal_perf);
> -
> -     max_freq = nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT;
> -
> -     /* Switch to khz */
> -     return max_freq * 1000;
> +     return READ_ONCE(cpudata->max_freq);
>  }
>
>  static int amd_get_nominal_freq(struct amd_cpudata *cpudata)  {
> -     struct cppc_perf_caps cppc_perf;
> -
> -     int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
> -     if (ret)
> -             return ret;
> -
> -     /* Switch to khz */
> -     return cppc_perf.nominal_freq * 1000;
> +     return READ_ONCE(cpudata->nominal_freq);
>  }
>
>  static int amd_get_lowest_nonlinear_freq(struct amd_cpudata *cpudata)  {
> -     struct cppc_perf_caps cppc_perf;
> -     u32 lowest_nonlinear_freq, lowest_nonlinear_perf,
> -         nominal_freq, nominal_perf;
> -     u64 lowest_nonlinear_ratio;
> -
> -     int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
> -     if (ret)
> -             return ret;
> -
> -     nominal_freq = cppc_perf.nominal_freq;
> -     nominal_perf = READ_ONCE(cpudata->nominal_perf);
> -
> -     lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf;
> -
> -     lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf <<
> SCHED_CAPACITY_SHIFT,
> -                                      nominal_perf);
> -
> -     lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >>
> SCHED_CAPACITY_SHIFT;
> -
> -     /* Switch to khz */
> -     return lowest_nonlinear_freq * 1000;
> +     return READ_ONCE(cpudata->lowest_nonlinear_freq);
>  }
>
>  static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
> @@ -828,6 +776,53 @@ static void amd_pstate_update_limits(unsigned int
> cpu)
>       mutex_unlock(&amd_pstate_driver_lock);
>  }
>
> +/**
> + * amd_pstate_init_freq: Initialize the max_freq, min_freq,
> + *                       nominal_freq and lowest_nonlinear_freq for
> + *                       the @cpudata object.
> + *
> + *  Requires: highest_perf, lowest_perf, nominal_perf and
> + *            lowest_nonlinear_perf members of @cpudata to be
> + *            initialized.
> + *
> + *  Returns 0 on success, non-zero value on failure.
> + */
> +static int amd_pstate_init_freq(struct amd_cpudata *cpudata) {
> +     int ret;
> +     u32 min_freq;
> +     u32 highest_perf, max_freq;
> +     u32 nominal_perf, nominal_freq;
> +     u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
> +     u32 boost_ratio, lowest_nonlinear_ratio;
> +     struct cppc_perf_caps cppc_perf;
> +
> +
> +     ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
> +     if (ret)
> +             return ret;
> +
> +     min_freq = cppc_perf.lowest_freq * 1000;
> +     nominal_freq = cppc_perf.nominal_freq * 1000;
> +     nominal_perf = READ_ONCE(cpudata->nominal_perf);
> +
> +     highest_perf = READ_ONCE(cpudata->highest_perf);
> +     boost_ratio = div_u64(highest_perf << SCHED_CAPACITY_SHIFT,
> nominal_perf);
> +     max_freq = nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT;
> +
> +     lowest_nonlinear_perf = READ_ONCE(cpudata-
> >lowest_nonlinear_perf);
> +     lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf <<
> SCHED_CAPACITY_SHIFT,
> +                                      nominal_perf);
> +     lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >>
> +SCHED_CAPACITY_SHIFT;
> +
> +     WRITE_ONCE(cpudata->min_freq, min_freq);
> +     WRITE_ONCE(cpudata->lowest_nonlinear_freq,
> lowest_nonlinear_freq);
> +     WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
> +     WRITE_ONCE(cpudata->max_freq, max_freq);
> +
> +     return 0;
> +}
> +
>  static int amd_pstate_cpu_init(struct cpufreq_policy *policy)  {
>       int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
> @@ -855,6 +850,10 @@ static int amd_pstate_cpu_init(struct cpufreq_policy
> *policy)
>       if (ret)
>               goto free_cpudata1;
>
> +     ret = amd_pstate_init_freq(cpudata);
> +     if (ret)
> +             goto free_cpudata1;
> +
>       min_freq = amd_get_min_freq(cpudata);
>       max_freq = amd_get_max_freq(cpudata);
>       nominal_freq = amd_get_nominal_freq(cpudata); @@ -896,13
> +895,8 @@ static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
>               goto free_cpudata2;
>       }
>
> -     /* Initial processor data capability frequencies */
> -     cpudata->max_freq = max_freq;
> -     cpudata->min_freq = min_freq;
>       cpudata->max_limit_freq = max_freq;
>       cpudata->min_limit_freq = min_freq;
> -     cpudata->nominal_freq = nominal_freq;
> -     cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
>
>       policy->driver_data = cpudata;
>
> @@ -1317,6 +1311,10 @@ static int amd_pstate_epp_cpu_init(struct
> cpufreq_policy *policy)
>       if (ret)
>               goto free_cpudata1;
>
> +     ret = amd_pstate_init_freq(cpudata);
> +     if (ret)
> +             goto free_cpudata1;
> +
>       min_freq = amd_get_min_freq(cpudata);
>       max_freq = amd_get_max_freq(cpudata);
>       nominal_freq = amd_get_nominal_freq(cpudata); @@ -1333,12
> +1331,6 @@ static int amd_pstate_epp_cpu_init(struct cpufreq_policy
> *policy)
>       /* It will be updated by governor */
>       policy->cur = policy->cpuinfo.min_freq;
>
> -     /* Initial processor data capability frequencies */
> -     cpudata->max_freq = max_freq;
> -     cpudata->min_freq = min_freq;
> -     cpudata->nominal_freq = nominal_freq;
> -     cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
> -
>       policy->driver_data = cpudata;
>
>       cpudata->epp_cached = amd_pstate_get_epp(cpudata, 0);
> --
> 2.34.1
[Meng, Li (Jassmine)]
Reviewed-by: Li Meng < li.meng@amd.com>
diff mbox series

Patch

diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c
index 2015c9fcc3c9..ba1baa6733e6 100644
--- a/drivers/cpufreq/amd-pstate.c
+++ b/drivers/cpufreq/amd-pstate.c
@@ -606,74 +606,22 @@  static void amd_pstate_adjust_perf(unsigned int cpu,
 
 static int amd_get_min_freq(struct amd_cpudata *cpudata)
 {
-	struct cppc_perf_caps cppc_perf;
-
-	int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
-	if (ret)
-		return ret;
-
-	/* Switch to khz */
-	return cppc_perf.lowest_freq * 1000;
+	return READ_ONCE(cpudata->min_freq);
 }
 
 static int amd_get_max_freq(struct amd_cpudata *cpudata)
 {
-	struct cppc_perf_caps cppc_perf;
-	u32 max_perf, max_freq, nominal_freq, nominal_perf;
-	u64 boost_ratio;
-
-	int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
-	if (ret)
-		return ret;
-
-	nominal_freq = cppc_perf.nominal_freq;
-	nominal_perf = READ_ONCE(cpudata->nominal_perf);
-	max_perf = READ_ONCE(cpudata->highest_perf);
-
-	boost_ratio = div_u64(max_perf << SCHED_CAPACITY_SHIFT,
-			      nominal_perf);
-
-	max_freq = nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT;
-
-	/* Switch to khz */
-	return max_freq * 1000;
+	return READ_ONCE(cpudata->max_freq);
 }
 
 static int amd_get_nominal_freq(struct amd_cpudata *cpudata)
 {
-	struct cppc_perf_caps cppc_perf;
-
-	int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
-	if (ret)
-		return ret;
-
-	/* Switch to khz */
-	return cppc_perf.nominal_freq * 1000;
+	return READ_ONCE(cpudata->nominal_freq);
 }
 
 static int amd_get_lowest_nonlinear_freq(struct amd_cpudata *cpudata)
 {
-	struct cppc_perf_caps cppc_perf;
-	u32 lowest_nonlinear_freq, lowest_nonlinear_perf,
-	    nominal_freq, nominal_perf;
-	u64 lowest_nonlinear_ratio;
-
-	int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
-	if (ret)
-		return ret;
-
-	nominal_freq = cppc_perf.nominal_freq;
-	nominal_perf = READ_ONCE(cpudata->nominal_perf);
-
-	lowest_nonlinear_perf = cppc_perf.lowest_nonlinear_perf;
-
-	lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
-					 nominal_perf);
-
-	lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT;
-
-	/* Switch to khz */
-	return lowest_nonlinear_freq * 1000;
+	return READ_ONCE(cpudata->lowest_nonlinear_freq);
 }
 
 static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
@@ -828,6 +776,53 @@  static void amd_pstate_update_limits(unsigned int cpu)
 	mutex_unlock(&amd_pstate_driver_lock);
 }
 
+/**
+ * amd_pstate_init_freq: Initialize the max_freq, min_freq,
+ *                       nominal_freq and lowest_nonlinear_freq for
+ *                       the @cpudata object.
+ *
+ *  Requires: highest_perf, lowest_perf, nominal_perf and
+ *            lowest_nonlinear_perf members of @cpudata to be
+ *            initialized.
+ *
+ *  Returns 0 on success, non-zero value on failure.
+ */
+static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
+{
+	int ret;
+	u32 min_freq;
+	u32 highest_perf, max_freq;
+	u32 nominal_perf, nominal_freq;
+	u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
+	u32 boost_ratio, lowest_nonlinear_ratio;
+	struct cppc_perf_caps cppc_perf;
+
+
+	ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
+	if (ret)
+		return ret;
+
+	min_freq = cppc_perf.lowest_freq * 1000;
+	nominal_freq = cppc_perf.nominal_freq * 1000;
+	nominal_perf = READ_ONCE(cpudata->nominal_perf);
+
+	highest_perf = READ_ONCE(cpudata->highest_perf);
+	boost_ratio = div_u64(highest_perf << SCHED_CAPACITY_SHIFT, nominal_perf);
+	max_freq = nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT;
+
+	lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
+	lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
+					 nominal_perf);
+	lowest_nonlinear_freq = nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT;
+
+	WRITE_ONCE(cpudata->min_freq, min_freq);
+	WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
+	WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
+	WRITE_ONCE(cpudata->max_freq, max_freq);
+
+	return 0;
+}
+
 static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
 {
 	int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret;
@@ -855,6 +850,10 @@  static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
 	if (ret)
 		goto free_cpudata1;
 
+	ret = amd_pstate_init_freq(cpudata);
+	if (ret)
+		goto free_cpudata1;
+
 	min_freq = amd_get_min_freq(cpudata);
 	max_freq = amd_get_max_freq(cpudata);
 	nominal_freq = amd_get_nominal_freq(cpudata);
@@ -896,13 +895,8 @@  static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
 		goto free_cpudata2;
 	}
 
-	/* Initial processor data capability frequencies */
-	cpudata->max_freq = max_freq;
-	cpudata->min_freq = min_freq;
 	cpudata->max_limit_freq = max_freq;
 	cpudata->min_limit_freq = min_freq;
-	cpudata->nominal_freq = nominal_freq;
-	cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
 
 	policy->driver_data = cpudata;
 
@@ -1317,6 +1311,10 @@  static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	if (ret)
 		goto free_cpudata1;
 
+	ret = amd_pstate_init_freq(cpudata);
+	if (ret)
+		goto free_cpudata1;
+
 	min_freq = amd_get_min_freq(cpudata);
 	max_freq = amd_get_max_freq(cpudata);
 	nominal_freq = amd_get_nominal_freq(cpudata);
@@ -1333,12 +1331,6 @@  static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
 	/* It will be updated by governor */
 	policy->cur = policy->cpuinfo.min_freq;
 
-	/* Initial processor data capability frequencies */
-	cpudata->max_freq = max_freq;
-	cpudata->min_freq = min_freq;
-	cpudata->nominal_freq = nominal_freq;
-	cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq;
-
 	policy->driver_data = cpudata;
 
 	cpudata->epp_cached = amd_pstate_get_epp(cpudata, 0);