From: liberodark Subject: [PATCH] cpufreq: spacemit: k3: per-cluster DVFS via cpufreq-dt (v4) Multi-clock OPP support for the K3 X100 clusters (PLL3/PLL4 switching, 614 MHz - 2.4 GHz) hooked into cpufreq-dt. v4: review fixes : - cache the PLL clock handles at first transition instead of of_clk_get_by_name() on every frequency change (reference + memory leak in the hot path), drop the dead opp-hz/microvolt scan and its leaked of_node reference - serialize the cross-cluster PLL manipulation with a mutex (two policies can transition concurrently) - policy notifier: only act on CPUFREQ_CREATE_POLICY, check _find_opp_table() result, drop the opp_table reference - fold the redundant nested turbo condition Signed-off-by: liberodark --- --- a/arch/riscv/boot/dts/spacemit/k3.dtsi +++ b/arch/riscv/boot/dts/spacemit/k3.dtsi @@ -187,9 +187,135 @@ #size-cells = <0>; timebase-frequency = <24000000>; + clst_core_opp_table0_x100: opp_table0_x100 { + compatible = "operating-points-v2"; + opp-shared; + + clocks = <&pll CLK_PLL3>, <&pll CLK_PLL4>, <&pll CLK_PLL3_D1>; + clock-names = "pll_clst0", "pll_clst1", "pll_src"; + + opp2400000000 { + opp-hz = /bits/ 64 <2400000000>, /bits/ 64 <2400000000>; + opp-microvolt = <1000000 1000000 1004000>; + clock-latency-ns = <200000>; + }; + + opp2300000000 { + opp-hz = /bits/ 64 <2300000000>, /bits/ 64 <2300000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp2200000000 { + opp-hz = /bits/ 64 <2200000000>, /bits/ 64 <2200000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp2150000000 { + opp-hz = /bits/ 64 <2150000000>, /bits/ 64 <2150000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp2100000000 { + opp-hz = /bits/ 64 <2100000000>, /bits/ 64 <2100000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp2000000000 { + opp-hz = /bits/ 64 <2000000000>, /bits/ 64 <2000000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp1900000000 { + opp-hz = /bits/ 64 <1900000000>, /bits/ 64 <1900000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp1850000000 { + opp-hz = /bits/ 64 <1850000000>, /bits/ 64 <1850000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp1800000000 { + opp-hz = /bits/ 64 <1800000000>, /bits/ 64 <1800000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp1700000000 { + opp-hz = /bits/ 64 <1700000000>, /bits/ 64 <1700000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp1600000000 { + opp-hz = /bits/ 64 <1600000000>, /bits/ 64 <1600000000>; + opp-microvolt = <880000 880000 884000>; + clock-latency-ns = <200000>; + }; + + opp1500000000 { + opp-hz = /bits/ 64 <1500000000>, /bits/ 64 <1500000000>; + opp-microvolt = <850000 850000 854000>; + clock-latency-ns = <200000>; + }; + + opp1400000000 { + opp-hz = /bits/ 64 <1400000000>, /bits/ 64 <1400000000>; + opp-microvolt = <850000 850000 854000>; + clock-latency-ns = <200000>; + }; + + opp1300000000 { + opp-hz = /bits/ 64 <1300000000>, /bits/ 64 <1300000000>; + opp-microvolt = <850000 850000 854000>; + clock-latency-ns = <200000>; + }; + + opp1200000000 { + opp-hz = /bits/ 64 <1200000000>, /bits/ 64 <1200000000>; + opp-microvolt = <850000 850000 854000>; + clock-latency-ns = <200000>; + }; + + opp1100000000 { + opp-hz = /bits/ 64 <1100000000>, /bits/ 64 <1100000000>; + opp-microvolt = <850000 850000 854000>; + clock-latency-ns = <200000>; + }; + + opp1000000000 { + opp-hz = /bits/ 64 <1000000000>, /bits/ 64 <1000000000>; + opp-microvolt = <800000 800000 804000>; + clock-latency-ns = <200000>; + }; + + opp819000000 { + opp-hz = /bits/ 64 <819200000>, /bits/ 64 <819200000>; + opp-microvolt = <800000 800000 804000>; + clock-latency-ns = <200000>; + }; + + opp614400000 { + opp-hz = /bits/ 64 <614400000>, /bits/ 64 <614400000>; + opp-microvolt = <800000 800000 804000>; + clock-latency-ns = <200000>; + }; + }; + cpu_0: cpu@0 { compatible = "spacemit,x100", "riscv"; model = "Spacemit(R) X100"; + clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, + <&syscon_apmu CLK_APMU_CPU_C1_CORE>; + clock-names = "cls0", "cls1"; + operating-points-v2 = <&clst_core_opp_table0_x100>; device_type = "cpu"; reg = <0>; riscv,isa-base = "rv64i"; @@ -232,6 +358,10 @@ cpu_1: cpu@1 { compatible = "spacemit,x100", "riscv"; model = "Spacemit(R) X100"; + clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, + <&syscon_apmu CLK_APMU_CPU_C1_CORE>; + clock-names = "cls0", "cls1"; + operating-points-v2 = <&clst_core_opp_table0_x100>; device_type = "cpu"; reg = <1>; riscv,isa-base = "rv64i"; @@ -274,6 +404,10 @@ cpu_2: cpu@2 { compatible = "spacemit,x100", "riscv"; model = "Spacemit(R) X100"; + clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, + <&syscon_apmu CLK_APMU_CPU_C1_CORE>; + clock-names = "cls0", "cls1"; + operating-points-v2 = <&clst_core_opp_table0_x100>; device_type = "cpu"; reg = <2>; riscv,isa-base = "rv64i"; @@ -316,6 +450,10 @@ cpu_3: cpu@3 { compatible = "spacemit,x100", "riscv"; model = "Spacemit(R) X100"; + clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, + <&syscon_apmu CLK_APMU_CPU_C1_CORE>; + clock-names = "cls0", "cls1"; + operating-points-v2 = <&clst_core_opp_table0_x100>; device_type = "cpu"; reg = <3>; riscv,isa-base = "rv64i"; @@ -358,6 +496,10 @@ cpu_4: cpu@4 { compatible = "spacemit,x100", "riscv"; model = "Spacemit(R) X100"; + clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, + <&syscon_apmu CLK_APMU_CPU_C1_CORE>; + clock-names = "cls0", "cls1"; + operating-points-v2 = <&clst_core_opp_table0_x100>; device_type = "cpu"; reg = <4>; riscv,isa-base = "rv64i"; @@ -400,6 +542,10 @@ cpu_5: cpu@5 { compatible = "spacemit,x100", "riscv"; model = "Spacemit(R) X100"; + clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, + <&syscon_apmu CLK_APMU_CPU_C1_CORE>; + clock-names = "cls0", "cls1"; + operating-points-v2 = <&clst_core_opp_table0_x100>; device_type = "cpu"; reg = <5>; riscv,isa-base = "rv64i"; @@ -442,6 +588,10 @@ cpu_6: cpu@6 { compatible = "spacemit,x100", "riscv"; model = "Spacemit(R) X100"; + clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, + <&syscon_apmu CLK_APMU_CPU_C1_CORE>; + clock-names = "cls0", "cls1"; + operating-points-v2 = <&clst_core_opp_table0_x100>; device_type = "cpu"; reg = <6>; riscv,isa-base = "rv64i"; @@ -484,6 +634,10 @@ cpu_7: cpu@7 { compatible = "spacemit,x100", "riscv"; model = "Spacemit(R) X100"; + clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, + <&syscon_apmu CLK_APMU_CPU_C1_CORE>; + clock-names = "cls0", "cls1"; + operating-points-v2 = <&clst_core_opp_table0_x100>; device_type = "cpu"; reg = <7>; riscv,isa-base = "rv64i"; --- a/drivers/cpufreq/cpufreq-dt.c +++ b/drivers/cpufreq/cpufreq-dt.c @@ -24,6 +24,7 @@ #include "cpufreq-dt.h" +#ifndef CONFIG_SPACEMIT_K3_CPUFREQ struct private_data { struct list_head node; @@ -33,9 +34,11 @@ bool have_static_opps; int opp_token; }; +#endif static LIST_HEAD(priv_list); +#ifndef CONFIG_SPACEMIT_K3_CPUFREQ static struct private_data *cpufreq_dt_find_data(int cpu) { struct private_data *priv; @@ -47,6 +50,24 @@ return NULL; } +#else +struct private_data *cpufreq_dt_find_data(int cpu) +{ + struct private_data *priv; + + list_for_each_entry(priv, &priv_list, node) { + if (cpumask_test_cpu(cpu, priv->cpus)) + return priv; + } + + return NULL; +} + +void cpufreq_dt_add_data(struct private_data *priv) +{ + list_add(&priv->node, &priv_list); +} +#endif static int set_target(struct cpufreq_policy *policy, unsigned int index) { --- a/drivers/cpufreq/cpufreq-dt.h +++ b/drivers/cpufreq/cpufreq-dt.h @@ -24,4 +24,20 @@ struct platform_device *cpufreq_dt_pdev_register(struct device *dev); + +#ifdef CONFIG_SPACEMIT_K3_CPUFREQ +struct private_data { + struct list_head node; + + cpumask_var_t cpus; + struct device *cpu_dev; + struct cpufreq_frequency_table *freq_table; + bool have_static_opps; + int opp_token; +}; + +struct private_data *cpufreq_dt_find_data(int cpu); +void cpufreq_dt_add_data(struct private_data *priv); +#endif + #endif /* __CPUFREQ_DT_H__ */ --- a/drivers/cpufreq/Kconfig +++ b/drivers/cpufreq/Kconfig @@ -204,6 +204,18 @@ comment "CPU frequency scaling drivers" +config SPACEMIT_K3_CPUFREQ + bool "SpacemiT K3 CPU frequency scaling" + depends on ARCH_SPACEMIT && OF && COMMON_CLK + select CPUFREQ_DT + select CPUFREQ_DT_PLATDEV + select PM_OPP + help + CPU frequency scaling support for the SpacemiT K3 SoC. Works + with cpufreq-dt; a transition notifier reprograms the cluster + PLLs for turbo frequencies. Voltage is left at the firmware + defaults (no regulator scaling). + config CPUFREQ_DT tristate "Generic DT based cpufreq driver" depends on HAVE_CLK && OF --- a/drivers/cpufreq/Makefile +++ b/drivers/cpufreq/Makefile @@ -15,6 +15,7 @@ obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET) += cpufreq_governor_attr_set.o obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o +obj-$(CONFIG_SPACEMIT_K3_CPUFREQ) += spacemit-k3-cpufreq.o obj-$(CONFIG_CPUFREQ_DT_RUST) += rcpufreq_dt.o obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o obj-$(CONFIG_CPUFREQ_VIRT) += virtual-cpufreq.o --- a/drivers/opp/core.c +++ b/drivers/opp/core.c @@ -94,6 +94,16 @@ static bool assert_single_clk(struct opp_table *opp_table, unsigned int __always_unused index) { + /* + * Multi-clock OPP tables driven by dev_pm_opp_config_clks_simple + * keep every clock at the rate of the same OPP entry, so + * frequency-based lookups can safely use the first rate. + */ + if (IS_ENABLED(CONFIG_SPACEMIT_K3_CPUFREQ) && + opp_table->clk_count > 1 && + opp_table->config_clks == dev_pm_opp_config_clks_simple) + return true; + return !WARN_ON(opp_table->clk_count > 1); } --- /dev/null +++ b/drivers/cpufreq/spacemit-k3-cpufreq.c @@ -0,0 +1,329 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../opp/opp.h" +#include "cpufreq-dt.h" + +#define TURBO0_FREQUENCY (1000000000) +#define STABLE_FREQUENCY (819200000) + +/* PLL handles cached at first transition (see spacemit_processor_notifier) */ +static DEFINE_MUTEX(spacemit_pll_lock); +static bool spacemit_plls_cached; +static struct clk *spacemit_pll_clst0, *spacemit_pll_clst1; +static struct clk *spacemit_pll_src, *spacemit_clt_pll_src; + +static int spacemit_processor_notifier(struct notifier_block *nb, + unsigned long event, void *data) +{ + int cpu; + struct device *cpu_dev; + struct cpufreq_freqs *freqs = (struct cpufreq_freqs *)data; + struct cpufreq_policy *policy = ( struct cpufreq_policy *)freqs->policy; + struct opp_table *opp_table; + struct clk *pll_clst0, *pll_clst1, *pll_src, *clt_pll_src; + int i; + + cpu = cpumask_first(policy->related_cpus); + cpu_dev = get_cpu_device(cpu); + opp_table = _find_opp_table(cpu_dev); + if (IS_ERR_OR_NULL(opp_table)) + return NOTIFY_DONE; + + /* + * Get the pll clk handles once and cache them: this notifier runs + * on every frequency transition, per-call of_clk_get_by_name() + * would leak a reference each time. + */ + mutex_lock(&spacemit_pll_lock); + if (!spacemit_plls_cached) { + spacemit_pll_clst0 = of_clk_get_by_name(opp_table->np, "pll_clst0"); + spacemit_pll_clst1 = of_clk_get_by_name(opp_table->np, "pll_clst1"); + spacemit_pll_src = of_clk_get_by_name(opp_table->np, "pll_src"); + spacemit_clt_pll_src = of_clk_get_by_name(opp_table->np, "clt_pll_src"); + spacemit_plls_cached = true; + } + pll_clst0 = spacemit_pll_clst0; + pll_clst1 = spacemit_pll_clst1; + pll_src = spacemit_pll_src; + clt_pll_src = spacemit_clt_pll_src; + + if (event == CPUFREQ_PRECHANGE) { + + if (freqs->new * 1000 > TURBO0_FREQUENCY) { + if (freqs->old * 1000 > TURBO0_FREQUENCY) { + for (i = 0; i < opp_table->clk_count; ++i) + clk_set_rate(opp_table->clks[i], STABLE_FREQUENCY); + } + + /* 2.4G */ + if (!IS_ERR(pll_clst0)) + clk_set_rate(pll_clst0, freqs->new * 1000); + if (!IS_ERR(pll_clst1)) + clk_set_rate(pll_clst1, freqs->new * 1000); + } + } + + if (!IS_ERR(clt_pll_src) && !IS_ERR(pll_src)) + clk_set_parent(clt_pll_src, pll_src); + + if (event == CPUFREQ_POSTCHANGE) { + /* TODO */ + } + + mutex_unlock(&spacemit_pll_lock); + dev_pm_opp_put_opp_table(opp_table); + + return 0; +} +static struct notifier_block spacemit_processor_notifier_block = { + .notifier_call = spacemit_processor_notifier, +}; + +static int spacemit_policy_notifier(struct notifier_block *nb, + unsigned long event, void *data) +{ + int cpu; + struct device *cpu_dev; + struct cpufreq_policy *policy = data; + struct opp_table *opp_table; + + if (event != CPUFREQ_CREATE_POLICY) + return NOTIFY_DONE; + + cpu = cpumask_first(policy->related_cpus); + cpu_dev = get_cpu_device(cpu); + opp_table = _find_opp_table(cpu_dev); + if (IS_ERR_OR_NULL(opp_table)) + return NOTIFY_DONE; + + if (policy->clk) + clk_put(policy->clk); + + /* cover the policy->clk & opp_table->clk which has been set before */ + policy->clk = opp_table->clks[0]; + opp_table->clk = opp_table->clks[0]; + + dev_pm_opp_put_opp_table(opp_table); + + return 0; +} + +static struct notifier_block spacemit_policy_notifier_block = { + .notifier_call = spacemit_policy_notifier, +}; + +static int spacemit_init_cpufreq_table(struct device *dev, + struct cpufreq_frequency_table **table) +{ + struct cpufreq_frequency_table *freq_table; + struct dev_pm_opp *opp; + unsigned long freq; + int i, count; + + count = dev_pm_opp_get_opp_count(dev); + if (count <= 0) + return count ? count : -ENOENT; + + freq_table = kcalloc(count + 1, sizeof(*freq_table), GFP_KERNEL); + if (!freq_table) + return -ENOMEM; + + for (i = 0, freq = 0; i < count; i++, freq++) { + opp = dev_pm_opp_find_freq_ceil_indexed(dev, &freq, 0); + if (IS_ERR(opp)) { + kfree(freq_table); + return PTR_ERR(opp); + } + freq_table[i].driver_data = i; + freq_table[i].frequency = freq / 1000; + dev_pm_opp_put(opp); + } + + freq_table[i].driver_data = i; + freq_table[i].frequency = CPUFREQ_TABLE_END; + + *table = freq_table; + return 0; +} + +static int spacemit_dt_cpufreq_pre_early_init(struct device *dev, int cpu) +{ + struct private_data *priv; + struct device *cpu_dev; + const char *reg_name[] = { "clst", NULL }; + const char *clk_name[] = { "cls0", "cls1", NULL }; + struct dev_pm_opp_config config = { + .regulator_names = reg_name, + .clk_names = clk_name, + .config_clks = dev_pm_opp_config_clks_simple, + }; + int ret; + + /* Check if this CPU is already covered by some other policy */ + if (cpufreq_dt_find_data(cpu)) + return 0; + + cpu_dev = get_cpu_device(cpu); + if (!cpu_dev) + return -EPROBE_DEFER; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + if (!alloc_cpumask_var(&priv->cpus, GFP_KERNEL)) + return -ENOMEM; + + cpumask_set_cpu(cpu, priv->cpus); + priv->cpu_dev = cpu_dev; + + /* + * Mainline DT does not wire the cluster regulator (clst-supply): + * keep the firmware-set voltage and only scale frequency. This is + * safe (never under-volted for the max OPP set by firmware). + */ + config.regulator_names = NULL; + /* + * OPP layer will be taking care of regulators now, but it needs to know + * the name of the regulator first. + */ + priv->opp_token = dev_pm_opp_set_config(cpu_dev, &config); + if (priv->opp_token < 0) { + ret = -EPROBE_DEFER; + goto free_cpumask; + } + + /* Get OPP-sharing information from "operating-points-v2" bindings */ + ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->cpus); + if (ret) + goto out; + + /* + * Initialize OPP tables for all priv->cpus. They will be shared by + * all CPUs which have marked their CPUs shared with OPP bindings. + * + * For platforms not using operating-points-v2 bindings, we do this + * before updating priv->cpus. Otherwise, we will end up creating + * duplicate OPPs for the CPUs. + * + * OPPs might be populated at runtime, don't fail for error here unless + * it is -EPROBE_DEFER. + */ + ret = dev_pm_opp_of_cpumask_add_table(priv->cpus); + if (!ret) { + priv->have_static_opps = true; + } else if (ret == -EPROBE_DEFER) { + goto out; + } + + /* + * The OPP table must be initialized, statically or dynamically, by this + * point. + */ + ret = dev_pm_opp_get_opp_count(cpu_dev); + if (ret <= 0) { + dev_err(cpu_dev, "OPP table can't be empty\n"); + ret = -ENODEV; + goto out; + } + + /* + * dev_pm_opp_init_cpufreq_table() asserts a single clock per OPP + * table; build the table manually using the indexed helpers which + * support multi-clock OPP entries (cls0 + cls1). + */ + ret = spacemit_init_cpufreq_table(cpu_dev, &priv->freq_table); + if (ret) { + dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret); + goto out; + } + + cpufreq_dt_add_data(priv); + + return 0; + +out: + if (priv->have_static_opps) + dev_pm_opp_of_cpumask_remove_table(priv->cpus); + dev_pm_opp_put_regulators(priv->opp_token); +free_cpumask: + free_cpumask_var(priv->cpus); + return ret; +} + +static int spacemit_dt_cpufreq_pre_probe(struct platform_device *pdev) +{ + int cpu; + + if (strncmp(pdev->name, "cpufreq-dt", 10) != 0) + return 0; + + for_each_possible_cpu(cpu) + spacemit_dt_cpufreq_pre_early_init(&pdev->dev, cpu); + + return 0; +} + +static int __device_notifier_call(struct notifier_block *nb, + unsigned long event, void *dev) +{ + struct platform_device *pdev = to_platform_device(dev); + + switch (event) { + case BUS_NOTIFY_REMOVED_DEVICE: + break; + case BUS_NOTIFY_UNBOUND_DRIVER: + break; + case BUS_NOTIFY_BIND_DRIVER: + /* here */ + spacemit_dt_cpufreq_pre_probe(pdev); + break; + case BUS_NOTIFY_ADD_DEVICE: + break; + default: + break; + } + + return NOTIFY_DONE; +} + +static struct notifier_block spacemit_platform_nb = { + .notifier_call = __device_notifier_call, +}; + +static int __init spacemit_processor_driver_init(void) +{ + int ret; + + ret = cpufreq_register_notifier(&spacemit_processor_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); + if (ret) { + pr_err("register cpufreq notifier failed\n"); + return -EINVAL; + } + + ret = cpufreq_register_notifier(&spacemit_policy_notifier_block, CPUFREQ_POLICY_NOTIFIER); + if (ret) { + pr_err("register cpufreq notifier failed\n"); + return -EINVAL; + } + + bus_register_notifier(&platform_bus_type, &spacemit_platform_nb); + + return 0; +} +arch_initcall(spacemit_processor_driver_init);