@@ -10,14 +10,18 @@
#define pr_fmt(fmt) "CPPC Cpufreq:" fmt
+#include <linux/arch_topology.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/cpu.h>
#include <linux/cpufreq.h>
#include <linux/dmi.h>
+#include <linux/irq_work.h>
+#include <linux/kthread.h>
#include <linux/time.h>
#include <linux/vmalloc.h>
+#include <uapi/linux/sched/types.h>
#include <asm/unaligned.h>
@@ -39,6 +43,17 @@
static struct cppc_cpudata **all_cpu_data;
static bool boost_supported;
+struct cppc_freq_invariance {
+ struct kthread_worker *worker;
+ struct irq_work irq_work;
+ struct kthread_work work;
+ struct cppc_perf_fb_ctrs prev_perf_fb_ctrs;
+ unsigned int max_freq;
+};
+static DEFINE_PER_CPU(struct cppc_freq_invariance, cppc_f_i);
+
+static bool scale_freq_tick_registered;
+
struct cppc_workaround_oem_info {
char oem_id[ACPI_OEM_ID_SIZE + 1];
char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
@@ -274,6 +289,7 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
*/
policy->cpuinfo.min_freq = cppc_cpufreq_perf_to_khz(cpu, cpu->perf_caps.lowest_perf);
policy->cpuinfo.max_freq = cppc_cpufreq_perf_to_khz(cpu, cpu->perf_caps.nominal_perf);
+ per_cpu(cppc_f_i, cpu_num).max_freq = policy->cpuinfo.max_freq;
policy->transition_delay_us = cppc_cpufreq_get_transition_delay_us(cpu_num);
policy->shared_type = cpu->shared_type;
@@ -287,6 +303,7 @@ static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
if (unlikely(i == policy->cpu))
continue;
+ per_cpu(cppc_f_i, i).max_freq = policy->cpuinfo.max_freq;
memcpy(&all_cpu_data[i]->perf_caps, &cpu->perf_caps,
sizeof(cpu->perf_caps));
}
@@ -372,7 +389,7 @@ static unsigned int cppc_cpufreq_get_rate(unsigned int cpunum)
static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
{
struct cppc_cpudata *cpudata;
- int ret;
+ int ret, i;
if (!boost_supported) {
pr_err("BOOST not supported by CPU or firmware\n");
@@ -388,6 +405,9 @@ static int cppc_cpufreq_set_boost(struct cpufreq_policy *policy, int state)
cpudata->perf_caps.nominal_perf);
policy->cpuinfo.max_freq = policy->max;
+ for_each_cpu(i, policy->related_cpus)
+ per_cpu(cppc_f_i, i).max_freq = policy->cpuinfo.max_freq;
+
ret = freq_qos_update_request(policy->max_freq_req, policy->max);
if (ret < 0)
return ret;
@@ -448,6 +468,117 @@ static void cppc_check_hisi_workaround(void)
acpi_put_table(tbl);
}
+static void cppc_scale_freq_tick_workfn(struct kthread_work *work)
+{
+ struct cppc_freq_invariance *cppc_fi;
+ struct cppc_perf_fb_ctrs fb_ctrs = {0};
+ int cpu = raw_smp_processor_id();
+ struct cppc_cpudata *cpudata = all_cpu_data[cpu];
+ u64 rate;
+
+ cppc_fi = container_of(work, struct cppc_freq_invariance, work);
+
+ if (cppc_get_perf_ctrs(cpu, &fb_ctrs)) {
+ pr_info("%s: cppc_get_perf_ctrs() failed\n", __func__);
+ return;
+ }
+
+ rate = cppc_get_rate_from_fbctrs(cpudata, cppc_fi->prev_perf_fb_ctrs, fb_ctrs);
+ cppc_fi->prev_perf_fb_ctrs = fb_ctrs;
+
+ rate <<= SCHED_CAPACITY_SHIFT;
+ per_cpu(freq_scale, cpu) = div64_u64(rate, cppc_fi->max_freq);
+}
+
+static void cppc_irq_work(struct irq_work *irq_work)
+{
+ struct cppc_freq_invariance *cppc_fi;
+
+ cppc_fi = container_of(irq_work, struct cppc_freq_invariance, irq_work);
+ kthread_queue_work(cppc_fi->worker, &cppc_fi->work);
+}
+
+static void cppc_scale_freq_tick(void)
+{
+ struct cppc_freq_invariance *cppc_fi = &per_cpu(cppc_f_i, raw_smp_processor_id());
+
+ /*
+ * cppc_get_perf_ctrs() can potentially sleep, call that from the right
+ * context.
+ */
+ irq_work_queue(&cppc_fi->irq_work);
+}
+
+static void cppc_freq_invariance_exit(void)
+{
+ struct cppc_freq_invariance *cppc_fi;
+ int i;
+
+ for_each_possible_cpu(i) {
+ cppc_fi = &per_cpu(cppc_f_i, i);
+ if (cppc_fi->worker) {
+ irq_work_sync(&cppc_fi->irq_work);
+ kthread_destroy_worker(cppc_fi->worker);
+ cppc_fi->worker = NULL;
+ }
+ }
+}
+
+extern bool amu_counters_supported(void);
+
+static void __init cppc_freq_invariance_init(void)
+{
+ struct cppc_perf_fb_ctrs fb_ctrs = {0};
+ struct cppc_freq_invariance *cppc_fi;
+ struct sched_attr attr = {
+ .size = sizeof(struct sched_attr),
+ .sched_policy = SCHED_DEADLINE,
+ .sched_nice = 0,
+ .sched_priority = 0,
+ /*
+ * Fake (unused) bandwidth; workaround to "fix"
+ * priority inheritance.
+ */
+ .sched_runtime = 1000000,
+ .sched_deadline = 10000000,
+ .sched_period = 10000000,
+ };
+ struct kthread_worker *worker;
+ int i, ret;
+
+ /* Lets use the AMU counters if they are supported */
+ if (amu_counters_supported())
+ return;
+
+ for_each_possible_cpu(i) {
+ cppc_fi = &per_cpu(cppc_f_i, i);
+
+ kthread_init_work(&cppc_fi->work, cppc_scale_freq_tick_workfn);
+ init_irq_work(&cppc_fi->irq_work, cppc_irq_work);
+ worker = kthread_create_worker_on_cpu(i, 0, "cppc:%d", i);
+ if (IS_ERR(worker))
+ return cppc_freq_invariance_exit();
+
+ cppc_fi->worker = worker;
+ ret = sched_setattr_nocheck(worker->task, &attr);
+ if (ret) {
+ pr_warn("%s: failed to set SCHED_DEADLINE\n", __func__);
+ return cppc_freq_invariance_exit();
+ }
+
+ ret = cppc_get_perf_ctrs(i, &fb_ctrs);
+ if (!ret)
+ per_cpu(cppc_fi->prev_perf_fb_ctrs, i) = fb_ctrs;
+ }
+
+ /* Register for freq-invariance */
+ if (cppc_cpufreq_driver.get != hisi_cppc_cpufreq_get_rate &&
+ !topology_set_scale_freq_tick(cppc_scale_freq_tick, cpu_possible_mask)) {
+ scale_freq_tick_registered = true;
+ pr_info("Registered cppc_scale_freq_tick()\n");
+ }
+}
+
static int __init cppc_cpufreq_init(void)
{
int i, ret = 0;
@@ -483,6 +614,8 @@ static int __init cppc_cpufreq_init(void)
if (ret)
goto out;
+ cppc_freq_invariance_init();
+
return ret;
out:
@@ -503,6 +636,9 @@ static void __exit cppc_cpufreq_exit(void)
struct cppc_cpudata *cpu;
int i;
+ if (scale_freq_tick_registered)
+ cppc_freq_invariance_exit();
+
cpufreq_unregister_driver(&cppc_cpufreq_driver);
for_each_possible_cpu(i) {
@@ -5143,6 +5143,7 @@ int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr)
{
return __sched_setscheduler(p, attr, false, true);
}
+EXPORT_SYMBOL_GPL(sched_setattr_nocheck);
/**
* sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
The Frequency Invariance Engine (FIE) is providing a frequency scaling correction factor that helps achieve more accurate load-tracking. Normally, this scaling factor can be obtained directly with the help of the cpufreq drivers as they know the exact frequency the hardware is running at. But that isn't the case for CPPC driver. Another way of obtaining that is using the AMU counter support, which is already present in kernel, but that hardware is optional for platforms and few of the platforms lack it. This patch thus obtains this scaling factor using the existing logic present in the cppc driver. This checks if the AMU counters are available or not on the platform, if not it goes ahead and registers itself for frequency invariance, else it lets AMU stuff come in later and take it over. Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> --- drivers/cpufreq/cppc_cpufreq.c | 138 ++++++++++++++++++++++++++++++++- kernel/sched/core.c | 1 + 2 files changed, 138 insertions(+), 1 deletion(-) -- 2.25.0.rc1.19.g042ed3e048af