Skip to content

Commit ec1c7ad

Browse files
pierregondoisvireshk
authored andcommitted
cpufreq: CPPC: Fix performance/frequency conversion
CPUfreq governors request CPU frequencies using information on current CPU usage. The CPPC driver converts them to performance requests. Frequency targets are computed as: target_freq = (util / cpu_capacity) * max_freq target_freq is then clamped between [policy->min, policy->max]. The CPPC driver converts performance values to frequencies (and vice-versa) using cppc_cpufreq_perf_to_khz() and cppc_cpufreq_khz_to_perf(). These functions both use two different factors depending on the range of the input value. For cppc_cpufreq_khz_to_perf(): - (NOMINAL_PERF / NOMINAL_FREQ) or - (LOWEST_PERF / LOWEST_FREQ) and for cppc_cpufreq_perf_to_khz(): - (NOMINAL_FREQ / NOMINAL_PERF) or - ((NOMINAL_PERF - LOWEST_FREQ) / (NOMINAL_PERF - LOWEST_PERF)) This means: 1- the functions are not inverse for some values: (perf_to_khz(khz_to_perf(x)) != x) 2- cppc_cpufreq_perf_to_khz(LOWEST_PERF) can sometimes give a different value from LOWEST_FREQ due to integer approximation 3- it is implied that performance and frequency are proportional (NOMINAL_FREQ / NOMINAL_PERF) == (LOWEST_PERF / LOWEST_FREQ) This patch changes the conversion functions to an affine function. This fixes the 3 points above. Suggested-by: Lukasz Luba <lukasz.luba@arm.com> Suggested-by: Morten Rasmussen <morten.rasmussen@arm.com> Signed-off-by: Pierre Gondois <Pierre.Gondois@arm.com> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
1 parent bc8b0c2 commit ec1c7ad

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

drivers/cpufreq/cppc_cpufreq.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -303,60 +303,59 @@ static u64 cppc_get_dmi_max_khz(void)
303303

304304
/*
305305
* If CPPC lowest_freq and nominal_freq registers are exposed then we can
306-
* use them to convert perf to freq and vice versa
307-
*
308-
* If the perf/freq point lies between Nominal and Lowest, we can treat
309-
* (Low perf, Low freq) and (Nom Perf, Nom freq) as 2D co-ordinates of a line
310-
* and extrapolate the rest
311-
* For perf/freq > Nominal, we use the ratio perf:freq at Nominal for conversion
306+
* use them to convert perf to freq and vice versa. The conversion is
307+
* extrapolated as an affine function passing by the 2 points:
308+
* - (Low perf, Low freq)
309+
* - (Nominal perf, Nominal perf)
312310
*/
313311
static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu_data,
314312
unsigned int perf)
315313
{
316314
struct cppc_perf_caps *caps = &cpu_data->perf_caps;
315+
s64 retval, offset = 0;
317316
static u64 max_khz;
318317
u64 mul, div;
319318

320319
if (caps->lowest_freq && caps->nominal_freq) {
321-
if (perf >= caps->nominal_perf) {
322-
mul = caps->nominal_freq;
323-
div = caps->nominal_perf;
324-
} else {
325-
mul = caps->nominal_freq - caps->lowest_freq;
326-
div = caps->nominal_perf - caps->lowest_perf;
327-
}
320+
mul = caps->nominal_freq - caps->lowest_freq;
321+
div = caps->nominal_perf - caps->lowest_perf;
322+
offset = caps->nominal_freq - div64_u64(caps->nominal_perf * mul, div);
328323
} else {
329324
if (!max_khz)
330325
max_khz = cppc_get_dmi_max_khz();
331326
mul = max_khz;
332327
div = caps->highest_perf;
333328
}
334-
return (u64)perf * mul / div;
329+
330+
retval = offset + div64_u64(perf * mul, div);
331+
if (retval >= 0)
332+
return retval;
333+
return 0;
335334
}
336335

337336
static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data,
338337
unsigned int freq)
339338
{
340339
struct cppc_perf_caps *caps = &cpu_data->perf_caps;
340+
s64 retval, offset = 0;
341341
static u64 max_khz;
342342
u64 mul, div;
343343

344344
if (caps->lowest_freq && caps->nominal_freq) {
345-
if (freq >= caps->nominal_freq) {
346-
mul = caps->nominal_perf;
347-
div = caps->nominal_freq;
348-
} else {
349-
mul = caps->lowest_perf;
350-
div = caps->lowest_freq;
351-
}
345+
mul = caps->nominal_perf - caps->lowest_perf;
346+
div = caps->nominal_freq - caps->lowest_freq;
347+
offset = caps->nominal_perf - div64_u64(caps->nominal_freq * mul, div);
352348
} else {
353349
if (!max_khz)
354350
max_khz = cppc_get_dmi_max_khz();
355351
mul = caps->highest_perf;
356352
div = max_khz;
357353
}
358354

359-
return (u64)freq * mul / div;
355+
retval = offset + div64_u64(freq * mul, div);
356+
if (retval >= 0)
357+
return retval;
358+
return 0;
360359
}
361360

362361
static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,

0 commit comments

Comments
 (0)