All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sumit Gupta <sumitg@nvidia.com>
To: <viresh.kumar@linaro.org>, <rjw@rjwysocki.net>,
	<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>
Cc: <ksitaraman@nvidia.com>, <bbasu@nvidia.com>, <sumitg@nvidia.com>
Subject: [Patch 1/2] cpufreq: tegra194: get consistent cpuinfo_cur_freq
Date: Wed, 16 Sep 2020 22:41:16 +0530	[thread overview]
Message-ID: <1600276277-7290-2-git-send-email-sumitg@nvidia.com> (raw)
In-Reply-To: <1600276277-7290-1-git-send-email-sumitg@nvidia.com>

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 <sumitg@nvidia.com>
---
 drivers/cpufreq/tegra194-cpufreq.c | 66 ++++++++++++++++++++++++++++++++------
 1 file changed, 57 insertions(+), 9 deletions(-)

diff --git a/drivers/cpufreq/tegra194-cpufreq.c b/drivers/cpufreq/tegra194-cpufreq.c
index e1d931c..d5b608d 100644
--- a/drivers/cpufreq/tegra194-cpufreq.c
+++ b/drivers/cpufreq/tegra194-cpufreq.c
@@ -180,9 +180,65 @@ 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)
+{
+	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 cpufreq_frequency_table *table, *pos;
+	struct cpufreq_policy policy;
+	unsigned int rate;
+	u64 ndiv;
+	int err;
+
+	cpufreq_get_policy(&policy, cpu);
+	table = policy.freq_table;
+
+	/* reconstruct actual cpu freq using counters*/
+	rate = tegra194_get_speed_common(cpu, US_DELAY);
+
+	/* get last written ndiv value*/
+	err = smp_call_function_single(cpu, get_cpu_ndiv, &ndiv, true);
+	if (err) {
+		pr_err("cpufreq: Failed to get ndiv for CPU%d, ret:%d\n",
+		       cpu, err);
+		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, table) {
+		if (pos->driver_data != ndiv)
+			continue;
+
+		if (abs(pos->frequency - rate) > 115200) {
+			pr_warn("cpufreq: high delta (%d) on CPU%d\n",
+				abs(pos->frequency - rate), cpu);
+			pr_warn("cpufreq: cur:%u, set:%u, set ndiv:%llu\n",
+				rate, pos->frequency, ndiv);
+		} else {
+			rate = pos->frequency;
+		}
+		break;
+	}
+	return rate;
 }
 
 static int tegra194_cpufreq_init(struct cpufreq_policy *policy)
@@ -209,14 +265,6 @@ static int tegra194_cpufreq_init(struct cpufreq_policy *policy)
 	return 0;
 }
 
-static void set_cpu_ndiv(void *data)
-{
-	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 int tegra194_cpufreq_set_target(struct cpufreq_policy *policy,
 				       unsigned int index)
 {
-- 
2.7.4


WARNING: multiple messages have this Message-ID (diff)
From: Sumit Gupta <sumitg@nvidia.com>
To: <viresh.kumar@linaro.org>, <rjw@rjwysocki.net>,
	<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>
Cc: ksitaraman@nvidia.com, sumitg@nvidia.com, bbasu@nvidia.com
Subject: [Patch 1/2] cpufreq: tegra194: get consistent cpuinfo_cur_freq
Date: Wed, 16 Sep 2020 22:41:16 +0530	[thread overview]
Message-ID: <1600276277-7290-2-git-send-email-sumitg@nvidia.com> (raw)
In-Reply-To: <1600276277-7290-1-git-send-email-sumitg@nvidia.com>

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 <sumitg@nvidia.com>
---
 drivers/cpufreq/tegra194-cpufreq.c | 66 ++++++++++++++++++++++++++++++++------
 1 file changed, 57 insertions(+), 9 deletions(-)

diff --git a/drivers/cpufreq/tegra194-cpufreq.c b/drivers/cpufreq/tegra194-cpufreq.c
index e1d931c..d5b608d 100644
--- a/drivers/cpufreq/tegra194-cpufreq.c
+++ b/drivers/cpufreq/tegra194-cpufreq.c
@@ -180,9 +180,65 @@ 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)
+{
+	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 cpufreq_frequency_table *table, *pos;
+	struct cpufreq_policy policy;
+	unsigned int rate;
+	u64 ndiv;
+	int err;
+
+	cpufreq_get_policy(&policy, cpu);
+	table = policy.freq_table;
+
+	/* reconstruct actual cpu freq using counters*/
+	rate = tegra194_get_speed_common(cpu, US_DELAY);
+
+	/* get last written ndiv value*/
+	err = smp_call_function_single(cpu, get_cpu_ndiv, &ndiv, true);
+	if (err) {
+		pr_err("cpufreq: Failed to get ndiv for CPU%d, ret:%d\n",
+		       cpu, err);
+		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, table) {
+		if (pos->driver_data != ndiv)
+			continue;
+
+		if (abs(pos->frequency - rate) > 115200) {
+			pr_warn("cpufreq: high delta (%d) on CPU%d\n",
+				abs(pos->frequency - rate), cpu);
+			pr_warn("cpufreq: cur:%u, set:%u, set ndiv:%llu\n",
+				rate, pos->frequency, ndiv);
+		} else {
+			rate = pos->frequency;
+		}
+		break;
+	}
+	return rate;
 }
 
 static int tegra194_cpufreq_init(struct cpufreq_policy *policy)
@@ -209,14 +265,6 @@ static int tegra194_cpufreq_init(struct cpufreq_policy *policy)
 	return 0;
 }
 
-static void set_cpu_ndiv(void *data)
-{
-	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 int tegra194_cpufreq_set_target(struct cpufreq_policy *policy,
 				       unsigned int index)
 {
-- 
2.7.4


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2020-09-16 17:19 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-16 17:11 [Patch 0/2] Tegra194 cpufreq driver misc changes Sumit Gupta
2020-09-16 17:11 ` Sumit Gupta
2020-09-16 17:11 ` Sumit Gupta [this message]
2020-09-16 17:11   ` [Patch 1/2] cpufreq: tegra194: get consistent cpuinfo_cur_freq Sumit Gupta
2020-09-17  8:36   ` Jon Hunter
2020-09-17  8:36     ` Jon Hunter
2020-09-17  8:44     ` Jon Hunter
2020-09-17  8:44       ` Jon Hunter
2020-09-23  8:34       ` Jon Hunter
2020-09-23  8:34         ` Jon Hunter
2020-10-05  4:34     ` Viresh Kumar
2020-10-05  4:34       ` Viresh Kumar
2020-10-05  9:12       ` Jon Hunter
2020-10-05  9:12         ` Jon Hunter
2020-09-24  8:56   ` Thierry Reding
2020-09-24  8:56     ` Thierry Reding
2020-10-05  4:46   ` Viresh Kumar
2020-10-05  4:46     ` Viresh Kumar
2020-10-05 18:40     ` Sumit Gupta
2020-10-05 18:40       ` Sumit Gupta
2020-09-16 17:11 ` [Patch 2/2] cpufreq: tegra194: Fix unlisted boot freq warning Sumit Gupta
2020-09-16 17:11   ` Sumit Gupta
2020-09-17  8:38   ` Jon Hunter
2020-09-17  8:38     ` Jon Hunter
2020-09-17  8:45     ` Jon Hunter
2020-09-17  8:45       ` Jon Hunter
2020-09-24  8:56   ` Thierry Reding
2020-09-24  8:56     ` Thierry Reding
2020-10-05  4:54   ` Viresh Kumar
2020-10-05  4:54     ` Viresh Kumar
2020-10-05 18:54     ` Sumit Gupta
2020-10-05 18:54       ` Sumit Gupta
2020-10-06  5:38       ` Viresh Kumar
2020-10-06  5:38         ` Viresh Kumar
2020-10-08 11:11         ` Sumit Gupta
2020-10-08 11:11           ` Sumit Gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1600276277-7290-2-git-send-email-sumitg@nvidia.com \
    --to=sumitg@nvidia.com \
    --cc=bbasu@nvidia.com \
    --cc=jonathanh@nvidia.com \
    --cc=ksitaraman@nvidia.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=rjw@rjwysocki.net \
    --cc=thierry.reding@gmail.com \
    --cc=viresh.kumar@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.