Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp676227pxb; Mon, 7 Feb 2022 23:03:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJxXIC1V9dFrlbajd7Xvsbt6LuJYHp4f8flrLeIJMnvQTVkt6djZ/KsH7vcEt367pWwXzi7u X-Received: by 2002:a17:902:ba98:: with SMTP id k24mr3129421pls.44.1644303791289; Mon, 07 Feb 2022 23:03:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644303791; cv=none; d=google.com; s=arc-20160816; b=q4IQvK6IOu8B9sZoU3yal1j2eN6kwUNOTV618W4HaAaTbcb/JAVbv/GU9ZHjRENZK+ wpez3PRi8PVQWJVUmXbKbpzfJoDqamaVrmCXP5sItLbZZ2+LD1lX2ZQy4wcRNhBdx40o lxZdyrLD+LwCvg8i/YA8KvICdWCxrx3bUL5NJrmb9NeLw2op2zsCxV62Z0TuODJiyMA8 l9ZbLo6MUhWl01+hO3SsIHNxocWIWyyVHkvUc0U4FrdrEPY8w1LCU+x4DIHnIFoGtdk2 8snuOnsx8Bd8gSPkc4dwdMMEwRUkPHJ4dQdJ4SagZnJHginsVo98wNncfUAD/aByDDsb RD9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=tzUcaIEImdF9ZfeiorQex0NTCKZ9P1s8gIbYQ0VJVoc=; b=c5wjv9k7fkP/o+yo2TyexTXaSl0xbh753gXX+5dRKXcCd88JOGxwKOd2+kh3lqOfb9 TfpkQdykfa+1e0gVltroMdfYQeBn/v+A2zMG2hIQ6NPljru6EYrkPxtEOPz6ROdrETJt jckcAbVBn5NXKI1aJNZ0KiJxVlVsSmfGF/pcwoUsj/xLNA4jCF6ivTwu7cjguHP4ga7y ExLdAoaWiVB5d3GSeu5hMs4TXQ8GqAapIau6Oi8mEu5giR4ylRP8NWS1wuxJktOqP3fj 8rnZCmCCdeX5iZtyIMKntxKvvskb+A7P7nKwiOdfdRG8q90NoKCOUxdwuJKX2e7QTozd 9MhQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j71si1207216pgd.8.2022.02.07.23.02.56; Mon, 07 Feb 2022 23:03:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377360AbiBDSIK (ORCPT + 99 others); Fri, 4 Feb 2022 13:08:10 -0500 Received: from mail-yb1-f175.google.com ([209.85.219.175]:37841 "EHLO mail-yb1-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231759AbiBDSII (ORCPT ); Fri, 4 Feb 2022 13:08:08 -0500 Received: by mail-yb1-f175.google.com with SMTP id v190so4871265ybv.4; Fri, 04 Feb 2022 10:08:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=tzUcaIEImdF9ZfeiorQex0NTCKZ9P1s8gIbYQ0VJVoc=; b=NsZqPZL8uJuFdoMjcUUp4iJgY6/VrG5x9EE3Jkjvcuy8Hr/SCjQsudMRKQ7tfX/bvL eiX8D9u/QCN57l94RegmLWgz7JFWNtPuQ7DHyRl7y3hTQyod3YPTjyExBsFxUr2H4sXG pjqGUZl89ERsrmIpxu5Tan51y/TWLLYRjicW6W+57eLVMqPG8+l9uB0ZGgrpIYQSFyVq P63b4WpaxIfe0qXmyrWWsbf0n7mvdZSIasdwkGKTL93RmdTpDr9XAMEMi7fRS+OPoaN8 8UG9GnGjkK1ApGgCqszVjBezMgbFZ/8q5QgMugon4Xv54IdyUgxYz2Id/uqN9ogU7CjJ IPnQ== X-Gm-Message-State: AOAM532PW08/SXIk5T5YNv4EdTw/MR3Qu6lM5jK5XoNo49tGX6/M/1Fv 3LtwBr+37Yk2exAiaibsn1/c4jhOz8wV64OfchQ= X-Received: by 2002:a25:7e81:: with SMTP id z123mr359595ybc.466.1643998088291; Fri, 04 Feb 2022 10:08:08 -0800 (PST) MIME-Version: 1.0 References: <20220111155419.943980-1-Pierre.Gondois@arm.com> In-Reply-To: <20220111155419.943980-1-Pierre.Gondois@arm.com> From: "Rafael J. Wysocki" Date: Fri, 4 Feb 2022 19:07:57 +0100 Message-ID: Subject: Re: [PATCH v2] cpufreq: CPPC: Fix performance/frequency conversion To: Pierre Gondois Cc: Linux Kernel Mailing List , Ionela Voinescu , Lukasz Luba , Morten Rasmussen , "Rafael J. Wysocki" , Viresh Kumar , Linux PM Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 11, 2022 at 4:54 PM Pierre Gondois wrote: > > 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 > Suggested-by: Morten Rasmussen > Signed-off-by: Pierre Gondois > --- > drivers/cpufreq/cppc_cpufreq.c | 43 +++++++++++++++++----------------- > 1 file changed, 21 insertions(+), 22 deletions(-) > > diff --git a/drivers/cpufreq/cppc_cpufreq.c b/drivers/cpufreq/cppc_cpufreq.c > index db17196266e4..5024d9af2e6e 100644 > --- a/drivers/cpufreq/cppc_cpufreq.c > +++ b/drivers/cpufreq/cppc_cpufreq.c > @@ -303,52 +303,48 @@ static u64 cppc_get_dmi_max_khz(void) > > /* > * If CPPC lowest_freq and nominal_freq registers are exposed then we can > - * use them to convert perf to freq and vice versa > - * > - * If the perf/freq point lies between Nominal and Lowest, we can treat > - * (Low perf, Low freq) and (Nom Perf, Nom freq) as 2D co-ordinates of a line > - * and extrapolate the rest > - * For perf/freq > Nominal, we use the ratio perf:freq at Nominal for conversion > + * use them to convert perf to freq and vice versa. The conversion is > + * extrapolated as an affine function passing by the 2 points: > + * - (Low perf, Low freq) > + * - (Nominal perf, Nominal perf) > */ > static unsigned int cppc_cpufreq_perf_to_khz(struct cppc_cpudata *cpu_data, > unsigned int perf) > { > struct cppc_perf_caps *caps = &cpu_data->perf_caps; > + s64 retval, offset = 0; > static u64 max_khz; > u64 mul, div; > > if (caps->lowest_freq && caps->nominal_freq) { > - if (perf >= caps->nominal_perf) { > - mul = caps->nominal_freq; > - div = caps->nominal_perf; > - } else { > - mul = caps->nominal_freq - caps->lowest_freq; > - div = caps->nominal_perf - caps->lowest_perf; > - } > + mul = caps->nominal_freq - caps->lowest_freq; > + div = caps->nominal_perf - caps->lowest_perf; > + offset = caps->nominal_freq - (u64)caps->nominal_perf * mul / div; Since mult is a u64, the other operands need not be cast to u64 explicitly AFAICS. Moreover, it might be better to use div64_u64() instead of the plain integer division. > } else { > if (!max_khz) > max_khz = cppc_get_dmi_max_khz(); > mul = max_khz; > div = caps->highest_perf; > } > - return (u64)perf * mul / div; > + > + retval = offset + (u64)perf * mul / div; > + if (retval >= 0) > + return retval; > + return 0; > } > > static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data, > unsigned int freq) > { > struct cppc_perf_caps *caps = &cpu_data->perf_caps; > + s64 retval, offset = 0; > static u64 max_khz; > u64 mul, div; > > if (caps->lowest_freq && caps->nominal_freq) { > - if (freq >= caps->nominal_freq) { > - mul = caps->nominal_perf; > - div = caps->nominal_freq; > - } else { > - mul = caps->lowest_perf; > - div = caps->lowest_freq; > - } > + mul = caps->nominal_perf - caps->lowest_perf; > + div = caps->nominal_freq - caps->lowest_freq; > + offset = caps->nominal_perf - (u64)caps->nominal_freq * mul / div; > } else { > if (!max_khz) > max_khz = cppc_get_dmi_max_khz(); > @@ -356,7 +352,10 @@ static unsigned int cppc_cpufreq_khz_to_perf(struct cppc_cpudata *cpu_data, > div = max_khz; > } > > - return (u64)freq * mul / div; > + retval = offset + (u64)freq * mul / div; > + if (retval >= 0) > + return retval; > + return 0; > } > > static int cppc_cpufreq_set_target(struct cpufreq_policy *policy, > -- > 2.25.1 >