Skip to content

Commit c471956

Browse files
Jie Zhanvireshk
authored andcommitted
cppc_cpufreq: Use desired perf if feedback ctrs are 0 or unchanged
The CPPC performance feedback counters could be 0 or unchanged when the target cpu is in a low-power idle state, e.g. power-gated or clock-gated. When the counters are 0, cppc_cpufreq_get_rate() returns 0 KHz, which makes cpufreq_online() get a false error and fail to generate a cpufreq policy. When the counters are unchanged, the existing cppc_perf_from_fbctrs() returns a cached desired perf, but some platforms may update the real frequency back to the desired perf reg. For the above cases in cppc_cpufreq_get_rate(), get the latest desired perf from the CPPC reg to reflect the frequency because some platforms may update the actual frequency back there; if failed, use the cached desired perf. Fixes: 6a4fec4 ("cpufreq: cppc: cppc_cpufreq_get_rate() returns zero in all error cases.") Signed-off-by: Jie Zhan <[email protected]> Reviewed-by: Zeng Heng <[email protected]> Reviewed-by: Ionela Voinescu <[email protected]> Reviewed-by: Huisong Li <[email protected]> Signed-off-by: Viresh Kumar <[email protected]>
1 parent dfb6e2f commit c471956

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

drivers/cpufreq/cppc_cpufreq.c

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ static void cppc_scale_freq_workfn(struct kthread_work *work)
118118

119119
perf = cppc_perf_from_fbctrs(cpu_data, &cppc_fi->prev_perf_fb_ctrs,
120120
&fb_ctrs);
121+
if (!perf)
122+
return;
123+
121124
cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
122125

123126
perf <<= SCHED_CAPACITY_SHIFT;
@@ -724,13 +727,31 @@ static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
724727
delta_delivered = get_delta(fb_ctrs_t1->delivered,
725728
fb_ctrs_t0->delivered);
726729

727-
/* Check to avoid divide-by zero and invalid delivered_perf */
730+
/*
731+
* Avoid divide-by zero and unchanged feedback counters.
732+
* Leave it for callers to handle.
733+
*/
728734
if (!delta_reference || !delta_delivered)
729-
return cpu_data->perf_ctrls.desired_perf;
735+
return 0;
730736

731737
return (reference_perf * delta_delivered) / delta_reference;
732738
}
733739

740+
static int cppc_get_perf_ctrs_sample(int cpu,
741+
struct cppc_perf_fb_ctrs *fb_ctrs_t0,
742+
struct cppc_perf_fb_ctrs *fb_ctrs_t1)
743+
{
744+
int ret;
745+
746+
ret = cppc_get_perf_ctrs(cpu, fb_ctrs_t0);
747+
if (ret)
748+
return ret;
749+
750+
udelay(2); /* 2usec delay between sampling */
751+
752+
return cppc_get_perf_ctrs(cpu, fb_ctrs_t1);
753+
}
754+
734755
static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
735756
{
736757
struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
@@ -746,18 +767,32 @@ static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
746767

747768
cpufreq_cpu_put(policy);
748769

749-
ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
750-
if (ret)
751-
return 0;
752-
753-
udelay(2); /* 2usec delay between sampling */
754-
755-
ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
756-
if (ret)
757-
return 0;
770+
ret = cppc_get_perf_ctrs_sample(cpu, &fb_ctrs_t0, &fb_ctrs_t1);
771+
if (ret) {
772+
if (ret == -EFAULT)
773+
/* Any of the associated CPPC regs is 0. */
774+
goto out_invalid_counters;
775+
else
776+
return 0;
777+
}
758778

759779
delivered_perf = cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0,
760780
&fb_ctrs_t1);
781+
if (!delivered_perf)
782+
goto out_invalid_counters;
783+
784+
return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf);
785+
786+
out_invalid_counters:
787+
/*
788+
* Feedback counters could be unchanged or 0 when a cpu enters a
789+
* low-power idle state, e.g. clock-gated or power-gated.
790+
* Use desired perf for reflecting frequency. Get the latest register
791+
* value first as some platforms may update the actual delivered perf
792+
* there; if failed, resort to the cached desired perf.
793+
*/
794+
if (cppc_get_desired_perf(cpu, &delivered_perf))
795+
delivered_perf = cpu_data->perf_ctrls.desired_perf;
761796

762797
return cppc_perf_to_khz(&cpu_data->perf_caps, delivered_perf);
763798
}

0 commit comments

Comments
 (0)