Skip to contentSkip to conversation

CPPC_CPUFREQ improvements for Tegra241

PATCH In review Sumit Gupta 4 participants 13 messages Apr 18, 2023 – Apr 26, 2023

Subsystems (5)
lore
Share & tools
lore

Sumit Gupta

author reviewer
#0 lore raw

cover CPPC_CPUFREQ improvements for Tegra241

This patch series contains improvements and changes needed to get a stable value of current CPU frequency from "cpuinfo_cur_freq" sysfs node in Tegra241 SoC. All the CPU's in Tegra241 implement the ARM Activity Monitor Unit (AMU). On reading the sysfs node, the frequency value is re-constructed using AMU counters.

Sanjay Chandrashekara (1):
cpufreq: use correct unit when verify cur freq

Sumit Gupta (5):
cpufreq: CPPC: make workaround apply code generic irqchip/gicv3: Export arm_smccc_get_soc_id_xx funcs
cpufreq: CPPC: update sampling window for Tegra241
arm64: cpufeature: Export get_cpu_with_amu_feat func
cpufreq: CPPC: use wq to read amu counters on target cpu

 arch/arm64/kernel/cpufeature.c |  1 +
 drivers/cpufreq/cppc_cpufreq.c | 99 +++++++++++++++++++++++++++++-----
 drivers/cpufreq/cpufreq.c      |  2 +-
 drivers/firmware/smccc/smccc.c |  2 +
 4 files changed, 89 insertions(+), 15 deletions(-)
Signature · 1 line
2.17.1
author reviewer
#1 lore raw

PATCH 1/6 cpufreq: use correct unit when verify cur freq

From: Sanjay Chandrashekara <[email protected]>

cpufreq_verify_current_freq checks if the frequency returned by the hardware has a slight delta with the valid frequency value last set and returns "policy->cur" if the delta is within "1 MHz". In the comparison, "policy->cur" is in "kHz" but it's compared against HZ_PER_MHZ. So, the comparison range becomes "1 GHz". Fix this by comparing against KHZ_PER_MHZ instead of HZ_PER_MHZ.

Fixes f55ae08c8987 ("cpufreq: Avoid unnecessary frequency updates due to mismatch")
[ sumit gupta: Commit message update ]
---
 drivers/cpufreq/cpufreq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +1 -1
index 8b0509f89f1b..6b52ebe5a890 100644
@@ -1732,7 +1732,7 @@ static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b
 		 * MHz. In such cases it is better to avoid getting into
 		 * unnecessary frequency updates.
 		 */
-		if (abs(policy->cur - new_freq) < HZ_PER_MHZ)
+		if (abs(policy->cur - new_freq) < KHZ_PER_MHZ)
 			return policy->cur;
 
 		cpufreq_out_of_sync(policy, new_freq);
Signature · 1 line
2.17.1
author reviewer
#5 lore raw

PATCH 2/6 cpufreq: CPPC: make workaround apply code generic

In reply to Sumit Gupta

Expand the code which applies SoC workarounds to make it generic and easy to reuse.

---
 drivers/cpufreq/cppc_cpufreq.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/cpufreq/cppc_cpufreq.c +15 -4
index 022e3555407c..15c2cbb7a50e 100644
@@ -43,10 +43,13 @@ static LIST_HEAD(cpu_data_list);
 
 static bool boost_supported;
 
+static void cppc_check_hisi_workaround(void);
+
 struct cppc_workaround_oem_info {
 	char oem_id[ACPI_OEM_ID_SIZE + 1];
 	char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
 	u32 oem_revision;
+	void (*apply_wa_func)(void);
 };
 
 static struct cppc_workaround_oem_info wa_info[] = {
@@ -54,10 +57,12 @@ static struct cppc_workaround_oem_info wa_info[] = {
 		.oem_id		= "HISI  ",
 		.oem_table_id	= "HIP07   ",
 		.oem_revision	= 0,
+		.apply_wa_func	= cppc_check_hisi_workaround,
 	}, {
 		.oem_id		= "HISI  ",
 		.oem_table_id	= "HIP08   ",
 		.oem_revision	= 0,
+		.apply_wa_func	= cppc_check_hisi_workaround,
 	}
 };
 
@@ -938,6 +943,13 @@ static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
 }
 
 static void cppc_check_hisi_workaround(void)
+{
+	/* Overwrite the get() callback */
+	cppc_cpufreq_driver.get = hisi_cppc_cpufreq_get_rate;
+	fie_disabled = FIE_DISABLED;
+}
+
+static void cppc_apply_workarounds(void)
 {
 	struct acpi_table_header *tbl;
 	acpi_status status = AE_OK;
@@ -951,9 +963,8 @@ static void cppc_check_hisi_workaround(void)
 		if (!memcmp(wa_info[i].oem_id, tbl->oem_id, ACPI_OEM_ID_SIZE) &&
 		    !memcmp(wa_info[i].oem_table_id, tbl->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
 		    wa_info[i].oem_revision == tbl->oem_revision) {
-			/* Overwrite the get() callback */
-			cppc_cpufreq_driver.get = hisi_cppc_cpufreq_get_rate;
-			fie_disabled = FIE_DISABLED;
+			/* call work around function which matched from the table */
+			wa_info[i].apply_wa_func();
 			break;
 		}
 	}
@@ -968,7 +979,7 @@ static int __init cppc_cpufreq_init(void)
 	if (!acpi_cpc_valid())
 		return -ENODEV;
 
-	cppc_check_hisi_workaround();
+	cppc_apply_workarounds();
 	cppc_freq_invariance_init();
 	populate_efficiency_class();
 
Signature · 1 line
2.17.1
author reviewer
#6 lore raw

PATCH 3/6 irqchip/gicv3: Export arm_smccc_get_soc_id_xx funcs

In reply to Sumit Gupta

Export arm_smccc_get_soc_id_version() function which is needed in CPPC_CPUFREQ to check and apply workaround for Tegra241 SoC. Also, exporting arm_smccc_get_soc_id_revision() function as it might be needed in future.

---
 drivers/firmware/smccc/smccc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/firmware/smccc/smccc.c +2 -0
index db818f9dcb8e..b4224da46988 100644
@@ -64,11 +64,13 @@ s32 arm_smccc_get_soc_id_version(void)
 {
 	return smccc_soc_id_version;
 }
+EXPORT_SYMBOL_GPL(arm_smccc_get_soc_id_version);
 
 s32 arm_smccc_get_soc_id_revision(void)
 {
 	return smccc_soc_id_revision;
 }
+EXPORT_SYMBOL_GPL(arm_smccc_get_soc_id_revision);
 
 static int __init smccc_devices_init(void)
 {
Signature · 1 line
2.17.1
author reviewer
#8 lore raw

PATCH 4/6 cpufreq: CPPC: update sampling window for Tegra241

In reply to Sumit Gupta

In Tegra241, the Activity Monitor Unit's (AMU) constant counter (i.e. reference clock counter) increment happens in bursts and not incremented in the steps of one. For example reference counter may increment by '0x20' every '32' periods of ARM periphclk. This quantization of the reference counter is a source of error when reconstructing the frequency from the AMU counter data. To fix, increase the observation time interval so the error percentage becomes less.

---
 drivers/cpufreq/cppc_cpufreq.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/cpufreq/cppc_cpufreq.c +28 -1
index 15c2cbb7a50e..5e6a132a525e 100644
@@ -43,12 +43,17 @@ static LIST_HEAD(cpu_data_list);
 
 static bool boost_supported;
 
+/* default 2usec delay between sampling */
+static unsigned int sampling_delay_us = 2;
+
 static void cppc_check_hisi_workaround(void);
+static void cppc_nvidia_workaround(void);
 
 struct cppc_workaround_oem_info {
 	char oem_id[ACPI_OEM_ID_SIZE + 1];
 	char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
 	u32 oem_revision;
+	u32 smcc_soc_id;
 	void (*apply_wa_func)(void);
 };
 
@@ -63,6 +68,10 @@ static struct cppc_workaround_oem_info wa_info[] = {
 		.oem_table_id	= "HIP08   ",
 		.oem_revision	= 0,
 		.apply_wa_func	= cppc_check_hisi_workaround,
+	}, {
+		.oem_id         = "NVIDIA",
+		.smcc_soc_id	= 0x036b0241, /* JEP106 code for NVIDIA T241 chip (036b:0241) */
+		.apply_wa_func	= cppc_nvidia_workaround,
 	}
 };
 
@@ -856,7 +865,7 @@ static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
 	if (ret)
 		return ret;
 
-	udelay(2); /* 2usec delay between sampling */
+	udelay(sampling_delay_us);
 
 	ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
 	if (ret)
@@ -942,6 +951,11 @@ static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
 	return cppc_cpufreq_perf_to_khz(cpu_data, desired_perf);
 }
 
+static void cppc_nvidia_workaround(void)
+{
+	sampling_delay_us = 25;
+}
+
 static void cppc_check_hisi_workaround(void)
 {
 	/* Overwrite the get() callback */
@@ -953,8 +967,21 @@ static void cppc_apply_workarounds(void)
 {
 	struct acpi_table_header *tbl;
 	acpi_status status = AE_OK;
+	s32 soc_id;
 	int i;
 
+#ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
+	for (i = 0; i < ARRAY_SIZE(wa_info); i++) {
+		if (wa_info[i].smcc_soc_id) {
+			soc_id = arm_smccc_get_soc_id_version();
+			if (wa_info[i].smcc_soc_id == soc_id) {
+				wa_info[i].apply_wa_func();
+				return;
+			}
+		}
+	}
+#endif
+
 	status = acpi_get_table(ACPI_SIG_PCCT, 0, &tbl);
 	if (ACPI_FAILURE(status) || !tbl)
 		return;
Signature · 1 line
2.17.1
author reviewer
#9 lore raw

PATCH 5/6 arm64: cpufeature: Export get_cpu_with_amu_feat func

In reply to Sumit Gupta

Export the get_cpu_with_amu_feat() function for use by "cppc_cpufreq" to check if any CPU implements ARM's Activity Monitor Unit (AMU). If AMU is available, then for re-constructing the current CPU freq from its counters, queue work on target CPU to read the counters in a single call instead of reading them in separate smp calls.

---
 arch/arm64/kernel/cpufeature.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/kernel/cpufeature.c +1 -0
index d9345e9c0226..ec31da7043eb 100644
@@ -1917,6 +1917,7 @@ int get_cpu_with_amu_feat(void)
 {
 	return cpumask_any(&amu_cpus);
 }
+EXPORT_SYMBOL_GPL(get_cpu_with_amu_feat);
 
 static void cpu_amu_enable(struct arm64_cpu_capabilities const *cap)
 {
Signature · 1 line
2.17.1
author reviewer
#10 lore raw

PATCH 6/6 cpufreq: CPPC: use wq to read amu counters on target cpu

In reply to Sumit Gupta

ARM cores which implement the Activity Monitor Unit (AMU) use Functional Fixed Hardware (FFH) to map AMU counters to Delivered_Counter and Reference_Counter registers. Each sysreg is read separately with a smp_call_function_single call. So, total four IPI's are used, one per register. Due to this, the AMU's core counter and constant counter sampling can happen at a non-consistent time interval if an IPI is handled late. This results in unstable frequency value from "cpuinfo_cur_req" node sometimes. To fix, queue work on target CPU to read all counters synchronously in sequence. This helps to remove the inter-IPI latency and make sure that both the counters are sampled at a close time interval.
Without this change we observed that the re-generated value of CPU Frequency from AMU counters sometimes deviates by ~25% as the counters are read at non-determenistic time. Currently, kept the change specific to Tegra241. It can be applied to other SoC's having AMU if same issue is observed.

---
 drivers/cpufreq/cppc_cpufreq.c | 53 +++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 10 deletions(-)
drivers/cpufreq/cppc_cpufreq.c +43 -10
index 5e6a132a525e..52b93ac6225e 100644
@@ -46,6 +46,8 @@ static bool boost_supported;
 /* default 2usec delay between sampling */
 static unsigned int sampling_delay_us = 2;
 
+static bool get_rate_use_wq;
+
 static void cppc_check_hisi_workaround(void);
 static void cppc_nvidia_workaround(void);
 
@@ -99,6 +101,12 @@ struct cppc_freq_invariance {
 static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
 static struct kthread_worker *kworker_fie;
 
+struct feedback_ctrs {
+	u32 cpu;
+	struct cppc_perf_fb_ctrs fb_ctrs_t0;
+	struct cppc_perf_fb_ctrs fb_ctrs_t1;
+};
+
 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu);
 static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
 				 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
@@ -851,28 +859,44 @@ static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
 	return (reference_perf * delta_delivered) / delta_reference;
 }
 
+static int cppc_get_perf_ctrs_sync(void *fb_ctrs)
+{
+	struct feedback_ctrs *ctrs = fb_ctrs;
+	int ret;
+
+	ret = cppc_get_perf_ctrs(ctrs->cpu, &(ctrs->fb_ctrs_t0));
+	if (ret)
+		return ret;
+
+	udelay(sampling_delay_us);
+
+	ret = cppc_get_perf_ctrs(ctrs->cpu, &(ctrs->fb_ctrs_t1));
+	if (ret)
+		return ret;
+
+	return ret;
+}
+
 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
 {
-	struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
 	struct cppc_cpudata *cpu_data = policy->driver_data;
+	struct feedback_ctrs fb_ctrs = {0};
 	u64 delivered_perf;
 	int ret;
 
 	cpufreq_cpu_put(policy);
+	fb_ctrs.cpu = cpu;
 
-	ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
-	if (ret)
-		return ret;
-
-	udelay(sampling_delay_us);
-
-	ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
+	if (get_rate_use_wq)
+		ret = smp_call_on_cpu(cpu, cppc_get_perf_ctrs_sync, &fb_ctrs, false);
+	else
+		ret = cppc_get_perf_ctrs_sync(&fb_ctrs);
 	if (ret)
 		return ret;
 
-	delivered_perf = cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0,
-					       &fb_ctrs_t1);
+	delivered_perf = cppc_perf_from_fbctrs(cpu_data, &(fb_ctrs.fb_ctrs_t0),
+					       &(fb_ctrs.fb_ctrs_t1));
 
 	return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
 }
@@ -953,7 +977,16 @@ static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
 
 static void cppc_nvidia_workaround(void)
 {
+	int cpu;
+
 	sampling_delay_us = 25;
+
+#ifdef CONFIG_ARM64_AMU_EXTN
+	cpu = get_cpu_with_amu_feat();
+
+	if (cpu < nr_cpu_ids)
+		get_rate_use_wq = true;
+#endif
 }
 
 static void cppc_check_hisi_workaround(void)
Signature · 1 line
2.17.1
#2 lore raw

Re: [Patch 1/6] cpufreq: use correct unit when verify cur freq

In reply to Sumit Gupta
Quoted 23 lines · Sumit Gupta

From: Sanjay Chandrashekara <[email protected]>

cpufreq_verify_current_freq checks if the frequency returned by the hardware has a slight delta with the valid frequency value last set and returns "policy->cur" if the delta is within "1 MHz". In the comparison, "policy->cur" is in "kHz" but it's compared against HZ_PER_MHZ. So, the comparison range becomes "1 GHz". Fix this by comparing against KHZ_PER_MHZ instead of HZ_PER_MHZ.

Fixes f55ae08c8987 ("cpufreq: Avoid unnecessary frequency updates due to mismatch")
[ sumit gupta: Commit message update ]
---
 drivers/cpufreq/cpufreq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +0 -0
index 8b0509f89f1b..6b52ebe5a890 100644

drivers/cpufreq/cpufreq.c static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b

Quoted 9 lines
drivers/cpufreq/cpufreq.c +1 -2
@@ -1732,7 +1732,7 @@ static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b
                 * MHz. In such cases it is better to avoid getting into
                 * unnecessary frequency updates.
                 */
-               if (abs(policy->cur - new_freq) < HZ_PER_MHZ)
+               if (abs(policy->cur - new_freq) < KHZ_PER_MHZ)
                        return policy->cur;
 
                cpufreq_out_of_sync(policy, new_freq);
--

So this is a fix that can be applied separately from the rest of the series, isn't it?

author reviewer
#3 lore raw

Re: [Patch 1/6] cpufreq: use correct unit when verify cur freq

Quoted 34 lines · Rafael J. Wysocki

External email: Use caution opening links or attachments

Quoted 30 lines · Sumit Gupta

From: Sanjay Chandrashekara <[email protected]>

cpufreq_verify_current_freq checks if the frequency returned by the hardware has a slight delta with the valid frequency value last set and returns "policy->cur" if the delta is within "1 MHz". In the comparison, "policy->cur" is in "kHz" but it's compared against HZ_PER_MHZ. So, the comparison range becomes "1 GHz". Fix this by comparing against KHZ_PER_MHZ instead of HZ_PER_MHZ.

Fixes f55ae08c8987 ("cpufreq: Avoid unnecessary frequency updates due to mismatch")
[ sumit gupta: Commit message update ]
---
  drivers/cpufreq/cpufreq.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +1 -1
index 8b0509f89f1b..6b52ebe5a890 100644
@@ -1732,7 +1732,7 @@ static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b
                  * MHz. In such cases it is better to avoid getting into
                  * unnecessary frequency updates.
                  */
-               if (abs(policy->cur - new_freq) < HZ_PER_MHZ)
+               if (abs(policy->cur - new_freq) < KHZ_PER_MHZ)
                         return policy->cur;
 
Quoted 5 lines

drivers/cpufreq/cpufreq.c static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b

Quoted 2 lines
drivers/cpufreq/cpufreq.c +0 -1
                 cpufreq_out_of_sync(policy, new_freq);
--

So this is a fix that can be applied separately from the rest of the series, isn't it?

Yes.

Thank you,
Sumit Gupta

#4 lore raw

Re: [Patch 1/6] cpufreq: use correct unit when verify cur freq

Quoted 38 lines · Sumit Gupta
Quoted 34 lines · Rafael J. Wysocki

External email: Use caution opening links or attachments

Quoted 30 lines · Sumit Gupta

From: Sanjay Chandrashekara <[email protected]>

cpufreq_verify_current_freq checks if the frequency returned by the hardware has a slight delta with the valid frequency value last set and returns "policy->cur" if the delta is within "1 MHz". In the comparison, "policy->cur" is in "kHz" but it's compared against HZ_PER_MHZ. So, the comparison range becomes "1 GHz". Fix this by comparing against KHZ_PER_MHZ instead of HZ_PER_MHZ.

Fixes f55ae08c8987 ("cpufreq: Avoid unnecessary frequency updates due to mismatch")
[ sumit gupta: Commit message update ]
---
  drivers/cpufreq/cpufreq.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +1 -1
index 8b0509f89f1b..6b52ebe5a890 100644
@@ -1732,7 +1732,7 @@ static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b
                  * MHz. In such cases it is better to avoid getting into
                  * unnecessary frequency updates.
                  */
-               if (abs(policy->cur - new_freq) < HZ_PER_MHZ)
+               if (abs(policy->cur - new_freq) < KHZ_PER_MHZ)
                         return policy->cur;
 
Quoted 7 lines
Quoted 5 lines

drivers/cpufreq/cpufreq.c static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, b

Quoted 2 lines
drivers/cpufreq/cpufreq.c +0 -1
                 cpufreq_out_of_sync(policy, new_freq);
--

So this is a fix that can be applied separately from the rest of the series, isn't it?

Yes.

So applied as 6.4 material.

#11 lore raw

Re: [Patch 6/6] cpufreq: CPPC: use wq to read amu counters on target cpu

In reply to Sumit Gupta

Hi Sumit,

Thank you for the patches!

Quoted 10 lines · Sumit Gupta

ARM cores which implement the Activity Monitor Unit (AMU) use Functional Fixed Hardware (FFH) to map AMU counters to Delivered_Counter and Reference_Counter registers. Each sysreg is read separately with a smp_call_function_single call. So, total four IPI's are used, one per register. Due to this, the AMU's core counter and constant counter sampling can happen at a non-consistent time interval if an IPI is handled late. This results in unstable frequency value from "cpuinfo_cur_req" node sometimes. To fix, queue work on target CPU to read all counters synchronously in

Quoted 9 lines

sequence. This helps to remove the inter-IPI latency and make sure that both the counters are sampled at a close time interval.
Without this change we observed that the re-generated value of CPU Frequency from AMU counters sometimes deviates by ~25% as the counters are read at non-determenistic time. Currently, kept the change specific to Tegra241. It can be applied to other SoC's having AMU if same issue is observed.

To be honest I never liked the need for IPIs but it was the most generic solution I could find for an FFH implementation that does not assume a dependency between different reads, which is usecase specific.

Also, for any kind of caching of the counters I'd have to introduce some logic that would assume we'd always have two consecutive reads - one for the cycle counter and one for the constant counter, and there should be no update between them. And then there's the problem of potentially returning the same values if there's no update between two sets of reads.

The only feasible idea based on caching would be to piggy back on the frequency invariance engine (FIE) which computes a performance scale factor on the tick which can be translated to frequency. But the frequency obtained would be an average frequency for the past 4ms, which can in turn be at up to 4ms old (or more, if the CPU was idle).

Would something like this work for you?

This could also help with a similar issue described at [1] - not an IPI related issue, but an issue with similar symptoms.

[1] https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ionela.

Quoted 106 lines
---
 drivers/cpufreq/cppc_cpufreq.c | 53 +++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 10 deletions(-)
drivers/cpufreq/cppc_cpufreq.c +43 -10
index 5e6a132a525e..52b93ac6225e 100644
@@ -46,6 +46,8 @@ static bool boost_supported;
 /* default 2usec delay between sampling */
 static unsigned int sampling_delay_us = 2;
 
+static bool get_rate_use_wq;
+
 static void cppc_check_hisi_workaround(void);
 static void cppc_nvidia_workaround(void);
 
@@ -99,6 +101,12 @@ struct cppc_freq_invariance {
 static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
 static struct kthread_worker *kworker_fie;
 
+struct feedback_ctrs {
+	u32 cpu;
+	struct cppc_perf_fb_ctrs fb_ctrs_t0;
+	struct cppc_perf_fb_ctrs fb_ctrs_t1;
+};
+
 static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu);
 static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
 				 struct cppc_perf_fb_ctrs *fb_ctrs_t0,
@@ -851,28 +859,44 @@ static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
 	return (reference_perf * delta_delivered) / delta_reference;
 }
 
+static int cppc_get_perf_ctrs_sync(void *fb_ctrs)
+{
+	struct feedback_ctrs *ctrs = fb_ctrs;
+	int ret;
+
+	ret = cppc_get_perf_ctrs(ctrs->cpu, &(ctrs->fb_ctrs_t0));
+	if (ret)
+		return ret;
+
+	udelay(sampling_delay_us);
+
+	ret = cppc_get_perf_ctrs(ctrs->cpu, &(ctrs->fb_ctrs_t1));
+	if (ret)
+		return ret;
+
+	return ret;
+}
+
 static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
 {
-	struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
 	struct cppc_cpudata *cpu_data = policy->driver_data;
+	struct feedback_ctrs fb_ctrs = {0};
 	u64 delivered_perf;
 	int ret;
 
 	cpufreq_cpu_put(policy);
+	fb_ctrs.cpu = cpu;
 
-	ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
-	if (ret)
-		return ret;
-
-	udelay(sampling_delay_us);
-
-	ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
+	if (get_rate_use_wq)
+		ret = smp_call_on_cpu(cpu, cppc_get_perf_ctrs_sync, &fb_ctrs, false);
+	else
+		ret = cppc_get_perf_ctrs_sync(&fb_ctrs);
 	if (ret)
 		return ret;
 
-	delivered_perf = cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0,
-					       &fb_ctrs_t1);
+	delivered_perf = cppc_perf_from_fbctrs(cpu_data, &(fb_ctrs.fb_ctrs_t0),
+					       &(fb_ctrs.fb_ctrs_t1));
 
 	return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
 }
@@ -953,7 +977,16 @@ static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
 
 static void cppc_nvidia_workaround(void)
 {
+	int cpu;
+
 	sampling_delay_us = 25;
+
+#ifdef CONFIG_ARM64_AMU_EXTN
+	cpu = get_cpu_with_amu_feat();
+
+	if (cpu < nr_cpu_ids)
+		get_rate_use_wq = true;
+#endif
 }
 
 static void cppc_check_hisi_workaround(void)
Signature · 1 line
2.17.1
author reviewer
#12 lore raw

Re: [Patch 6/6] cpufreq: CPPC: use wq to read amu counters on target cpu

Hi Ionela,

Thankyou for the inputs.

Quoted 46 lines · Ionela Voinescu

External email: Use caution opening links or attachments

Hi Sumit,

Thank you for the patches!

Quoted 10 lines · Sumit Gupta

ARM cores which implement the Activity Monitor Unit (AMU) use Functional Fixed Hardware (FFH) to map AMU counters to Delivered_Counter and Reference_Counter registers. Each sysreg is read separately with a smp_call_function_single call. So, total four IPI's are used, one per register. Due to this, the AMU's core counter and constant counter sampling can happen at a non-consistent time interval if an IPI is handled late. This results in unstable frequency value from "cpuinfo_cur_req" node sometimes. To fix, queue work on target CPU to read all counters synchronously in

Quoted 9 lines

sequence. This helps to remove the inter-IPI latency and make sure that both the counters are sampled at a close time interval.
Without this change we observed that the re-generated value of CPU Frequency from AMU counters sometimes deviates by ~25% as the counters are read at non-determenistic time. Currently, kept the change specific to Tegra241. It can be applied to other SoC's having AMU if same issue is observed.

To be honest I never liked the need for IPIs but it was the most generic solution I could find for an FFH implementation that does not assume a dependency between different reads, which is usecase specific.

Also, for any kind of caching of the counters I'd have to introduce some logic that would assume we'd always have two consecutive reads - one for the cycle counter and one for the constant counter, and there should be no update between them. And then there's the problem of potentially returning the same values if there's no update between two sets of reads.

The only feasible idea based on caching would be to piggy back on the frequency invariance engine (FIE) which computes a performance scale factor on the tick which can be translated to frequency. But the frequency obtained would be an average frequency for the past 4ms, which can in turn be at up to 4ms old (or more, if the CPU was idle).

Would something like this work for you?

Quoted 8 lines

This could also help with a similar issue described at [1] - not an IPI related issue, but an issue with similar symptoms.

[1] https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ionela.

I think yes that will help as it will increase the time period and also remove the IPI's ?

One thing I am not sure is whether there can be any impact when CPU is IDLE w.r.t. the delta between the frequency set and get from the counters.

Thank you,
Sumit Gupta

Quoted 106 lines
Quoted 106 lines
---
  drivers/cpufreq/cppc_cpufreq.c | 53 +++++++++++++++++++++++++++-------
  1 file changed, 43 insertions(+), 10 deletions(-)
drivers/cpufreq/cppc_cpufreq.c +43 -11
index 5e6a132a525e..52b93ac6225e 100644
@@ -46,6 +46,8 @@ static bool boost_supported;
  /* default 2usec delay between sampling */
  static unsigned int sampling_delay_us = 2;
 
+static bool get_rate_use_wq;
+
  static void cppc_check_hisi_workaround(void);
  static void cppc_nvidia_workaround(void);
 
@@ -99,6 +101,12 @@ struct cppc_freq_invariance {
  static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_freq_inv);
  static struct kthread_worker *kworker_fie;
 
+struct feedback_ctrs {
+     u32 cpu;
+     struct cppc_perf_fb_ctrs fb_ctrs_t0;
+     struct cppc_perf_fb_ctrs fb_ctrs_t1;
+};
+
  static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu);
  static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
                                struct cppc_perf_fb_ctrs *fb_ctrs_t0,
@@ -851,28 +859,44 @@ static int cppc_perf_from_fbctrs(struct cppc_cpudata *cpu_data,
       return (reference_perf * delta_delivered) / delta_reference;
  }
 
+static int cppc_get_perf_ctrs_sync(void *fb_ctrs)
+{
+     struct feedback_ctrs *ctrs = fb_ctrs;
+     int ret;
+
+     ret = cppc_get_perf_ctrs(ctrs->cpu, &(ctrs->fb_ctrs_t0));
+     if (ret)
+             return ret;
+
+     udelay(sampling_delay_us);
+
+     ret = cppc_get_perf_ctrs(ctrs->cpu, &(ctrs->fb_ctrs_t1));
+     if (ret)
+             return ret;
+
+     return ret;
+}
+
  static unsigned int cppc_cpufreq_get_rate(unsigned int cpu)
  {
-     struct cppc_perf_fb_ctrs fb_ctrs_t0 = {0}, fb_ctrs_t1 = {0};
       struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
       struct cppc_cpudata *cpu_data = policy->driver_data;
+     struct feedback_ctrs fb_ctrs = {0};
       u64 delivered_perf;
       int ret;
 
       cpufreq_cpu_put(policy);
+     fb_ctrs.cpu = cpu;
 
-     ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t0);
-     if (ret)
-             return ret;
-
-     udelay(sampling_delay_us);
-
-     ret = cppc_get_perf_ctrs(cpu, &fb_ctrs_t1);
+     if (get_rate_use_wq)
+             ret = smp_call_on_cpu(cpu, cppc_get_perf_ctrs_sync, &fb_ctrs, false);
+     else
+             ret = cppc_get_perf_ctrs_sync(&fb_ctrs);
       if (ret)
               return ret;
 
-     delivered_perf = cppc_perf_from_fbctrs(cpu_data, &fb_ctrs_t0,
-                                            &fb_ctrs_t1);
+     delivered_perf = cppc_perf_from_fbctrs(cpu_data, &(fb_ctrs.fb_ctrs_t0),
+                                            &(fb_ctrs.fb_ctrs_t1));
 
       return cppc_cpufreq_perf_to_khz(cpu_data, delivered_perf);
  }
@@ -953,7 +977,16 @@ static unsigned int hisi_cppc_cpufreq_get_rate(unsigned int cpu)
 
  static void cppc_nvidia_workaround(void)
  {
+     int cpu;
+
       sampling_delay_us = 25;
+
+#ifdef CONFIG_ARM64_AMU_EXTN
+     cpu = get_cpu_with_amu_feat();
+
+     if (cpu < nr_cpu_ids)
+             get_rate_use_wq = true;
+#endif
  }
 
  static void cppc_check_hisi_workaround(void)
--

2.17.1

#7 lore raw

Re: [Patch 3/6] irqchip/gicv3: Export arm_smccc_get_soc_id_xx funcs

In reply to Sumit Gupta
Quoted 6 lines · Sumit Gupta

Export arm_smccc_get_soc_id_version() function which is needed in CPPC_CPUFREQ to check and apply workaround for Tegra241 SoC. Also, exporting arm_smccc_get_soc_id_revision() function as it might be needed in future.

The commit subject seems off, maybe you re-used the last one that was done in the tree (35727af2b15d irqchip/gicv3: Workaround for NVIDIA erratum T241-FABRIC-4), it should rather be: "firmware: smccc: Export arm_smccc_get_soc_id_xx funcs"

Signature · 1 line
Florian