From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA4FEC433DF for ; Mon, 12 Oct 2020 05:22:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4E481206B2 for ; Mon, 12 Oct 2020 05:22:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="IbqtigIm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726648AbgJLFWb (ORCPT ); Mon, 12 Oct 2020 01:22:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726120AbgJLFWa (ORCPT ); Mon, 12 Oct 2020 01:22:30 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3EDFC0613CE for ; Sun, 11 Oct 2020 22:22:30 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id r10so13158525pgb.10 for ; Sun, 11 Oct 2020 22:22:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=qmE6A/EKe0hTQJfNjsT3sfUiMsmf4sO+YlCAtCElHT4=; b=IbqtigImICgHWY7+4MtxnpiWYffaixBuI0p65m8WBT8s7qhDIXWrbGYHkKZjiM28Tx bFYDc9N1MkHT4hxFizyql+D9mXX2HnRLdLSTL5f9lD0RinlMPoDcTXX0mZcIF9ejzQP2 OXnqCfY/Flb/48/k6Iz6aagtF95LbJYC12SFH5u2bQFL27fTMvwjSZw1aJATc4ryp3Qb s5GAKfnrrOWjoM/AXxV1OWpP3ijsH2zZdjcG/exb35au79LUjxNgqD8Vz5AOQclFEeRK oB03PoZ6pHCkCGeDUwzWPJLHA7BLUxtR5Z/u8BIU+m/cqWdykNXR2MvqXHQA6VcCsVou eT2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=qmE6A/EKe0hTQJfNjsT3sfUiMsmf4sO+YlCAtCElHT4=; b=d84nLTMnK+lWmcDaIqxHv6nnVV/ZiOWnso032rGyYu5mTCmxjU9SQm8E4e+ZTcyYjw 2Sm6zAXCUSw0vfozO/U/7zlPrvaDAGKbhAKnidY4Amwd3pAtFdDQVBcHVt+A/ppZLErH v0IZVK7KT0o2YLtAD8HOPFlOtDjVoqNvFQD2FgPw9loXsJsfm7PxgR9IwEdB85+7bdge 7tQ7mElWZOsan4x7f76LqVqnFjiCmhDNO8P6r2sRFwdL3N6DOYvApIc2baJN4vDgWRLg QoCp6pS8dIWP0D6p3ooGAxn1Z2pAOpKjJ7ALER6BLsWd9paKDFQDb6PRXngLDrHaIx32 Dihw== X-Gm-Message-State: AOAM532ZccOxtdbbOtNSdAPRc71GolYoclxsrmIn06Q5972JE2KOf/ks ZsIqeym+ugm2w1IeI41KcGRH+g== X-Google-Smtp-Source: ABdhPJzKiRNtosO8NGGmlAvudBt+rTXzZbJH1MOqFeWC9h7MElpsoLs64t0rf2Oe+W2vSevTayY3ew== X-Received: by 2002:a17:90b:11d1:: with SMTP id gv17mr15654111pjb.3.1602480150206; Sun, 11 Oct 2020 22:22:30 -0700 (PDT) Received: from localhost ([122.181.54.133]) by smtp.gmail.com with ESMTPSA id b6sm21266225pjq.42.2020.10.11.22.22.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Oct 2020 22:22:29 -0700 (PDT) Date: Mon, 12 Oct 2020 10:52:27 +0530 From: Viresh Kumar To: Sumit Gupta Cc: rjw@rjwysocki.net, sudeep.holla@arm.com, thierry.reding@gmail.com, jonathanh@nvidia.com, linux-pm@vger.kernel.org, linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, ksitaraman@nvidia.com, bbasu@nvidia.com Subject: Re: [PATCH v2 1/2] cpufreq: tegra194: get consistent cpuinfo_cur_freq Message-ID: <20201012052227.x3bigztr7fit4jdz@vireshk-i7> References: <1602162066-26442-1-git-send-email-sumitg@nvidia.com> <1602162066-26442-2-git-send-email-sumitg@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1602162066-26442-2-git-send-email-sumitg@nvidia.com> User-Agent: NeoMutt/20180716-391-311a52 Precedence: bulk List-ID: X-Mailing-List: linux-tegra@vger.kernel.org On 08-10-20, 18:31, Sumit Gupta wrote: > Frequency returned by 'cpuinfo_cur_freq' using counters is not fixed > and keeps changing slightly. This change returns a consistent value > from freq_table. If the reconstructed frequency has acceptable delta > from the last written value, then return the frequency corresponding > to the last written ndiv value from freq_table. Otherwise, print a > warning and return the reconstructed freq. > > Signed-off-by: Sumit Gupta > --- > drivers/cpufreq/tegra194-cpufreq.c | 71 +++++++++++++++++++++++++++++++++----- > 1 file changed, 62 insertions(+), 9 deletions(-) > > diff --git a/drivers/cpufreq/tegra194-cpufreq.c b/drivers/cpufreq/tegra194-cpufreq.c > index e1d931c..d250e49 100644 > --- a/drivers/cpufreq/tegra194-cpufreq.c > +++ b/drivers/cpufreq/tegra194-cpufreq.c > @@ -180,9 +180,70 @@ static unsigned int tegra194_get_speed_common(u32 cpu, u32 delay) > return (rate_mhz * KHZ); /* in KHz */ > } > > +static void get_cpu_ndiv(void *ndiv) > +{ > + u64 ndiv_val; > + > + asm volatile("mrs %0, s3_0_c15_c0_4" : "=r" (ndiv_val) : ); > + > + *(u64 *)ndiv = ndiv_val; > +} > + > +static void set_cpu_ndiv(void *data) You weren't required to do this unnecessary change. > +{ > + struct cpufreq_frequency_table *tbl = data; > + u64 ndiv_val = (u64)tbl->driver_data; > + > + asm volatile("msr s3_0_c15_c0_4, %0" : : "r" (ndiv_val)); > +} > + > static unsigned int tegra194_get_speed(u32 cpu) > { > - return tegra194_get_speed_common(cpu, US_DELAY); > + struct tegra194_cpufreq_data *data = cpufreq_get_driver_data(); > + struct cpufreq_frequency_table *pos; > + unsigned int rate; > + u64 ndiv; > + int ret; > + u32 cl; > + > + if (!cpu_online(cpu)) This isn't required. The CPU is guaranteed to be online here. > + return -EINVAL; > + > + smp_call_function_single(cpu, get_cpu_cluster, &cl, true); > + > + if (cl >= data->num_clusters) Is it really possible here ? I meant you must have already checked this at cpufreq-init level already. Else mark it unlikely at least. > + return -EINVAL; > + > + /* reconstruct actual cpu freq using counters */ > + rate = tegra194_get_speed_common(cpu, US_DELAY); > + > + /* get last written ndiv value */ > + ret = smp_call_function_single(cpu, get_cpu_ndiv, &ndiv, true); > + if (ret) { What exactly can fail here ? get_cpu_ndiv() can't fail. Do we really need this check ? What about WARN_ON_ONCE() ? > + pr_err("cpufreq: Failed to get ndiv for CPU%d, ret:%d\n", > + cpu, ret); > + return rate; > + } > + > + /* > + * If the reconstructed frequency has acceptable delta from > + * the last written value, then return freq corresponding > + * to the last written ndiv value from freq_table. This is > + * done to return consistent value. > + */ > + cpufreq_for_each_valid_entry(pos, data->tables[cl]) { > + if (pos->driver_data != ndiv) > + continue; > + > + if (abs(pos->frequency - rate) > 115200) { where does this 115200 comes from ? Strange that it matches tty's baud rate :) This is 115 MHz, right ? Isn't that too big of a delta ? > + pr_warn("cpufreq: cpu%d,cur:%u,set:%u,set ndiv:%llu\n", > + cpu, rate, pos->frequency, ndiv); > + } else { > + rate = pos->frequency; > + } > + break; > + } > + return rate; > } -- viresh