199d6bdf3SSudeep Holla // SPDX-License-Identifier: GPL-2.0
299d6bdf3SSudeep Holla /*
399d6bdf3SSudeep Holla * System Control and Power Interface (SCMI) based CPUFreq Interface driver
499d6bdf3SSudeep Holla *
5eb1d35c6SCristian Marussi * Copyright (C) 2018-2021 ARM Ltd.
699d6bdf3SSudeep Holla * Sudeep Holla <sudeep.holla@arm.com>
799d6bdf3SSudeep Holla */
899d6bdf3SSudeep Holla
999d6bdf3SSudeep Holla #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1099d6bdf3SSudeep Holla
118410e7f3SSudeep Holla #include <linux/clk-provider.h>
1299d6bdf3SSudeep Holla #include <linux/cpu.h>
1399d6bdf3SSudeep Holla #include <linux/cpufreq.h>
1499d6bdf3SSudeep Holla #include <linux/cpumask.h>
153c429851SQuentin Perret #include <linux/energy_model.h>
1699d6bdf3SSudeep Holla #include <linux/export.h>
1799d6bdf3SSudeep Holla #include <linux/module.h>
1899d6bdf3SSudeep Holla #include <linux/pm_opp.h>
1999d6bdf3SSudeep Holla #include <linux/slab.h>
2099d6bdf3SSudeep Holla #include <linux/scmi_protocol.h>
2199d6bdf3SSudeep Holla #include <linux/types.h>
22ae6ccaa6SLukasz Luba #include <linux/units.h>
2399d6bdf3SSudeep Holla
2499d6bdf3SSudeep Holla struct scmi_data {
2599d6bdf3SSudeep Holla int domain_id;
2637f18831SViresh Kumar int nr_opp;
2799d6bdf3SSudeep Holla struct device *cpu_dev;
2837f18831SViresh Kumar cpumask_var_t opp_shared_cpus;
2999d6bdf3SSudeep Holla };
3099d6bdf3SSudeep Holla
31eb1d35c6SCristian Marussi static struct scmi_protocol_handle *ph;
32eb1d35c6SCristian Marussi static const struct scmi_perf_proto_ops *perf_ops;
3399d6bdf3SSudeep Holla
scmi_cpufreq_get_rate(unsigned int cpu)3499d6bdf3SSudeep Holla static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
3599d6bdf3SSudeep Holla {
3699d6bdf3SSudeep Holla struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
3799d6bdf3SSudeep Holla struct scmi_data *priv = policy->driver_data;
3899d6bdf3SSudeep Holla unsigned long rate;
3999d6bdf3SSudeep Holla int ret;
4099d6bdf3SSudeep Holla
41eb1d35c6SCristian Marussi ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false);
4299d6bdf3SSudeep Holla if (ret)
4399d6bdf3SSudeep Holla return 0;
4499d6bdf3SSudeep Holla return rate / 1000;
4599d6bdf3SSudeep Holla }
4699d6bdf3SSudeep Holla
4799d6bdf3SSudeep Holla /*
4899d6bdf3SSudeep Holla * perf_ops->freq_set is not a synchronous, the actual OPP change will
4999d6bdf3SSudeep Holla * happen asynchronously and can get notified if the events are
5099d6bdf3SSudeep Holla * subscribed for by the SCMI firmware
5199d6bdf3SSudeep Holla */
5299d6bdf3SSudeep Holla static int
scmi_cpufreq_set_target(struct cpufreq_policy * policy,unsigned int index)5399d6bdf3SSudeep Holla scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
5499d6bdf3SSudeep Holla {
5599d6bdf3SSudeep Holla struct scmi_data *priv = policy->driver_data;
560e141d1cSQuentin Perret u64 freq = policy->freq_table[index].frequency;
5799d6bdf3SSudeep Holla
58eb1d35c6SCristian Marussi return perf_ops->freq_set(ph, priv->domain_id, freq * 1000, false);
5999d6bdf3SSudeep Holla }
6099d6bdf3SSudeep Holla
scmi_cpufreq_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)6102f208c5SSudeep Holla static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy,
6202f208c5SSudeep Holla unsigned int target_freq)
6302f208c5SSudeep Holla {
6402f208c5SSudeep Holla struct scmi_data *priv = policy->driver_data;
65*ea37096aSJagadeesh Kona unsigned long freq = target_freq;
6602f208c5SSudeep Holla
67*ea37096aSJagadeesh Kona if (!perf_ops->freq_set(ph, priv->domain_id, freq * 1000, true))
6802f208c5SSudeep Holla return target_freq;
6902f208c5SSudeep Holla
7002f208c5SSudeep Holla return 0;
7102f208c5SSudeep Holla }
7202f208c5SSudeep Holla
7399d6bdf3SSudeep Holla static int
scmi_get_sharing_cpus(struct device * cpu_dev,struct cpumask * cpumask)7499d6bdf3SSudeep Holla scmi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
7599d6bdf3SSudeep Holla {
7699d6bdf3SSudeep Holla int cpu, domain, tdomain;
7799d6bdf3SSudeep Holla struct device *tcpu_dev;
7899d6bdf3SSudeep Holla
79eb1d35c6SCristian Marussi domain = perf_ops->device_domain_id(cpu_dev);
8099d6bdf3SSudeep Holla if (domain < 0)
8199d6bdf3SSudeep Holla return domain;
8299d6bdf3SSudeep Holla
8399d6bdf3SSudeep Holla for_each_possible_cpu(cpu) {
8499d6bdf3SSudeep Holla if (cpu == cpu_dev->id)
8599d6bdf3SSudeep Holla continue;
8699d6bdf3SSudeep Holla
8799d6bdf3SSudeep Holla tcpu_dev = get_cpu_device(cpu);
8899d6bdf3SSudeep Holla if (!tcpu_dev)
8999d6bdf3SSudeep Holla continue;
9099d6bdf3SSudeep Holla
91eb1d35c6SCristian Marussi tdomain = perf_ops->device_domain_id(tcpu_dev);
9299d6bdf3SSudeep Holla if (tdomain == domain)
9399d6bdf3SSudeep Holla cpumask_set_cpu(cpu, cpumask);
9499d6bdf3SSudeep Holla }
9599d6bdf3SSudeep Holla
9699d6bdf3SSudeep Holla return 0;
9799d6bdf3SSudeep Holla }
9899d6bdf3SSudeep Holla
993c429851SQuentin Perret static int __maybe_unused
scmi_get_cpu_power(struct device * cpu_dev,unsigned long * power,unsigned long * KHz)10075a3a99aSLukasz Luba scmi_get_cpu_power(struct device *cpu_dev, unsigned long *power,
10175a3a99aSLukasz Luba unsigned long *KHz)
1023c429851SQuentin Perret {
103f3ac888fSLukasz Luba enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph);
1043c429851SQuentin Perret unsigned long Hz;
1053c429851SQuentin Perret int ret, domain;
1063c429851SQuentin Perret
107eb1d35c6SCristian Marussi domain = perf_ops->device_domain_id(cpu_dev);
1083c429851SQuentin Perret if (domain < 0)
1093c429851SQuentin Perret return domain;
1103c429851SQuentin Perret
1113c429851SQuentin Perret /* Get the power cost of the performance domain. */
1123c429851SQuentin Perret Hz = *KHz * 1000;
113eb1d35c6SCristian Marussi ret = perf_ops->est_power_get(ph, domain, &Hz, power);
1143c429851SQuentin Perret if (ret)
1153c429851SQuentin Perret return ret;
1163c429851SQuentin Perret
117f3ac888fSLukasz Luba /* Convert the power to uW if it is mW (ignore bogoW) */
118f3ac888fSLukasz Luba if (power_scale == SCMI_POWER_MILLIWATTS)
119ae6ccaa6SLukasz Luba *power *= MICROWATT_PER_MILLIWATT;
120ae6ccaa6SLukasz Luba
1213c429851SQuentin Perret /* The EM framework specifies the frequency in KHz. */
1223c429851SQuentin Perret *KHz = Hz / 1000;
1233c429851SQuentin Perret
1243c429851SQuentin Perret return 0;
1253c429851SQuentin Perret }
1263c429851SQuentin Perret
scmi_cpufreq_init(struct cpufreq_policy * policy)12799d6bdf3SSudeep Holla static int scmi_cpufreq_init(struct cpufreq_policy *policy)
12899d6bdf3SSudeep Holla {
1293c429851SQuentin Perret int ret, nr_opp;
13099d6bdf3SSudeep Holla unsigned int latency;
13199d6bdf3SSudeep Holla struct device *cpu_dev;
13299d6bdf3SSudeep Holla struct scmi_data *priv;
13399d6bdf3SSudeep Holla struct cpufreq_frequency_table *freq_table;
13499d6bdf3SSudeep Holla
13599d6bdf3SSudeep Holla cpu_dev = get_cpu_device(policy->cpu);
13699d6bdf3SSudeep Holla if (!cpu_dev) {
13799d6bdf3SSudeep Holla pr_err("failed to get cpu%d device\n", policy->cpu);
13899d6bdf3SSudeep Holla return -ENODEV;
13999d6bdf3SSudeep Holla }
14099d6bdf3SSudeep Holla
14137f18831SViresh Kumar priv = kzalloc(sizeof(*priv), GFP_KERNEL);
14237f18831SViresh Kumar if (!priv)
143f7d63588SLukasz Luba return -ENOMEM;
14499d6bdf3SSudeep Holla
14537f18831SViresh Kumar if (!zalloc_cpumask_var(&priv->opp_shared_cpus, GFP_KERNEL)) {
14637f18831SViresh Kumar ret = -ENOMEM;
14737f18831SViresh Kumar goto out_free_priv;
14837f18831SViresh Kumar }
14937f18831SViresh Kumar
15080a064dbSNicola Mazzucato /* Obtain CPUs that share SCMI performance controls */
15199d6bdf3SSudeep Holla ret = scmi_get_sharing_cpus(cpu_dev, policy->cpus);
15299d6bdf3SSudeep Holla if (ret) {
15399d6bdf3SSudeep Holla dev_warn(cpu_dev, "failed to get sharing cpumask\n");
15480a064dbSNicola Mazzucato goto out_free_cpumask;
15599d6bdf3SSudeep Holla }
15699d6bdf3SSudeep Holla
15780a064dbSNicola Mazzucato /*
15880a064dbSNicola Mazzucato * Obtain CPUs that share performance levels.
15980a064dbSNicola Mazzucato * The OPP 'sharing cpus' info may come from DT through an empty opp
16080a064dbSNicola Mazzucato * table and opp-shared.
16180a064dbSNicola Mazzucato */
16237f18831SViresh Kumar ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
163b48cd0d1SYury Norov if (ret || cpumask_empty(priv->opp_shared_cpus)) {
16480a064dbSNicola Mazzucato /*
16580a064dbSNicola Mazzucato * Either opp-table is not set or no opp-shared was found.
16680a064dbSNicola Mazzucato * Use the CPU mask from SCMI to designate CPUs sharing an OPP
16780a064dbSNicola Mazzucato * table.
16880a064dbSNicola Mazzucato */
16937f18831SViresh Kumar cpumask_copy(priv->opp_shared_cpus, policy->cpus);
17080a064dbSNicola Mazzucato }
17180a064dbSNicola Mazzucato
17280a064dbSNicola Mazzucato /*
17380a064dbSNicola Mazzucato * A previous CPU may have marked OPPs as shared for a few CPUs, based on
17480a064dbSNicola Mazzucato * what OPP core provided. If the current CPU is part of those few, then
17580a064dbSNicola Mazzucato * there is no need to add OPPs again.
17680a064dbSNicola Mazzucato */
17780a064dbSNicola Mazzucato nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
17880a064dbSNicola Mazzucato if (nr_opp <= 0) {
179eb1d35c6SCristian Marussi ret = perf_ops->device_opps_add(ph, cpu_dev);
18099d6bdf3SSudeep Holla if (ret) {
18180a064dbSNicola Mazzucato dev_warn(cpu_dev, "failed to add opps to the device\n");
18280a064dbSNicola Mazzucato goto out_free_cpumask;
18399d6bdf3SSudeep Holla }
18499d6bdf3SSudeep Holla
1853c429851SQuentin Perret nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
1863c429851SQuentin Perret if (nr_opp <= 0) {
18771a37cd6SNicola Mazzucato dev_err(cpu_dev, "%s: No OPPs for this device: %d\n",
188b791c7f9SChristophe JAILLET __func__, nr_opp);
18971a37cd6SNicola Mazzucato
19071a37cd6SNicola Mazzucato ret = -ENODEV;
19199d6bdf3SSudeep Holla goto out_free_opp;
19299d6bdf3SSudeep Holla }
19399d6bdf3SSudeep Holla
19437f18831SViresh Kumar ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
19580a064dbSNicola Mazzucato if (ret) {
19680a064dbSNicola Mazzucato dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
19780a064dbSNicola Mazzucato __func__, ret);
19880a064dbSNicola Mazzucato
19980a064dbSNicola Mazzucato goto out_free_opp;
20080a064dbSNicola Mazzucato }
20180a064dbSNicola Mazzucato
20237f18831SViresh Kumar priv->nr_opp = nr_opp;
20399d6bdf3SSudeep Holla }
20499d6bdf3SSudeep Holla
20599d6bdf3SSudeep Holla ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
20699d6bdf3SSudeep Holla if (ret) {
20799d6bdf3SSudeep Holla dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
20837f18831SViresh Kumar goto out_free_opp;
20999d6bdf3SSudeep Holla }
21099d6bdf3SSudeep Holla
21199d6bdf3SSudeep Holla priv->cpu_dev = cpu_dev;
212eb1d35c6SCristian Marussi priv->domain_id = perf_ops->device_domain_id(cpu_dev);
21399d6bdf3SSudeep Holla
21499d6bdf3SSudeep Holla policy->driver_data = priv;
215d983af98SViresh Kumar policy->freq_table = freq_table;
21699d6bdf3SSudeep Holla
21799d6bdf3SSudeep Holla /* SCMI allows DVFS request for any domain from any CPU */
21899d6bdf3SSudeep Holla policy->dvfs_possible_from_any_cpu = true;
21999d6bdf3SSudeep Holla
220eb1d35c6SCristian Marussi latency = perf_ops->transition_latency_get(ph, cpu_dev);
22199d6bdf3SSudeep Holla if (!latency)
22299d6bdf3SSudeep Holla latency = CPUFREQ_ETERNAL;
22399d6bdf3SSudeep Holla
22499d6bdf3SSudeep Holla policy->cpuinfo.transition_latency = latency;
22599d6bdf3SSudeep Holla
226fb357127SNicola Mazzucato policy->fast_switch_possible =
227eb1d35c6SCristian Marussi perf_ops->fast_switch_possible(ph, cpu_dev);
2283c429851SQuentin Perret
22999d6bdf3SSudeep Holla return 0;
23099d6bdf3SSudeep Holla
23199d6bdf3SSudeep Holla out_free_opp:
2321690d8bbSViresh Kumar dev_pm_opp_remove_all_dynamic(cpu_dev);
23399d6bdf3SSudeep Holla
23480a064dbSNicola Mazzucato out_free_cpumask:
23537f18831SViresh Kumar free_cpumask_var(priv->opp_shared_cpus);
23637f18831SViresh Kumar
23737f18831SViresh Kumar out_free_priv:
23837f18831SViresh Kumar kfree(priv);
23980a064dbSNicola Mazzucato
24099d6bdf3SSudeep Holla return ret;
24199d6bdf3SSudeep Holla }
24299d6bdf3SSudeep Holla
scmi_cpufreq_exit(struct cpufreq_policy * policy)24399d6bdf3SSudeep Holla static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
24499d6bdf3SSudeep Holla {
24599d6bdf3SSudeep Holla struct scmi_data *priv = policy->driver_data;
24699d6bdf3SSudeep Holla
24799d6bdf3SSudeep Holla dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
2481690d8bbSViresh Kumar dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
24937f18831SViresh Kumar free_cpumask_var(priv->opp_shared_cpus);
2508cbd468bSYangtao Li kfree(priv);
25199d6bdf3SSudeep Holla
25299d6bdf3SSudeep Holla return 0;
25399d6bdf3SSudeep Holla }
25499d6bdf3SSudeep Holla
scmi_cpufreq_register_em(struct cpufreq_policy * policy)25537f18831SViresh Kumar static void scmi_cpufreq_register_em(struct cpufreq_policy *policy)
25637f18831SViresh Kumar {
25737f18831SViresh Kumar struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
258f3ac888fSLukasz Luba enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph);
25937f18831SViresh Kumar struct scmi_data *priv = policy->driver_data;
260f3ac888fSLukasz Luba bool em_power_scale = false;
26137f18831SViresh Kumar
26237f18831SViresh Kumar /*
26337f18831SViresh Kumar * This callback will be called for each policy, but we don't need to
26437f18831SViresh Kumar * register with EM every time. Despite not being part of the same
26537f18831SViresh Kumar * policy, some CPUs may still share their perf-domains, and a CPU from
26637f18831SViresh Kumar * another policy may already have registered with EM on behalf of CPUs
26737f18831SViresh Kumar * of this policy.
26837f18831SViresh Kumar */
26937f18831SViresh Kumar if (!priv->nr_opp)
27037f18831SViresh Kumar return;
27137f18831SViresh Kumar
272f3ac888fSLukasz Luba if (power_scale == SCMI_POWER_MILLIWATTS
273f3ac888fSLukasz Luba || power_scale == SCMI_POWER_MICROWATTS)
274f3ac888fSLukasz Luba em_power_scale = true;
275f3ac888fSLukasz Luba
27637f18831SViresh Kumar em_dev_register_perf_domain(get_cpu_device(policy->cpu), priv->nr_opp,
27737f18831SViresh Kumar &em_cb, priv->opp_shared_cpus,
278f3ac888fSLukasz Luba em_power_scale);
27937f18831SViresh Kumar }
28037f18831SViresh Kumar
28199d6bdf3SSudeep Holla static struct cpufreq_driver scmi_cpufreq_driver = {
28299d6bdf3SSudeep Holla .name = "scmi",
2835ae4a4b4SViresh Kumar .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
2845da7af9aSAmit Kucheria CPUFREQ_NEED_INITIAL_FREQ_CHECK |
2855da7af9aSAmit Kucheria CPUFREQ_IS_COOLING_DEV,
28699d6bdf3SSudeep Holla .verify = cpufreq_generic_frequency_table_verify,
28799d6bdf3SSudeep Holla .attr = cpufreq_generic_attr,
28899d6bdf3SSudeep Holla .target_index = scmi_cpufreq_set_target,
28902f208c5SSudeep Holla .fast_switch = scmi_cpufreq_fast_switch,
29099d6bdf3SSudeep Holla .get = scmi_cpufreq_get_rate,
29199d6bdf3SSudeep Holla .init = scmi_cpufreq_init,
29299d6bdf3SSudeep Holla .exit = scmi_cpufreq_exit,
29337f18831SViresh Kumar .register_em = scmi_cpufreq_register_em,
29499d6bdf3SSudeep Holla };
29599d6bdf3SSudeep Holla
scmi_cpufreq_probe(struct scmi_device * sdev)29699d6bdf3SSudeep Holla static int scmi_cpufreq_probe(struct scmi_device *sdev)
29799d6bdf3SSudeep Holla {
29899d6bdf3SSudeep Holla int ret;
2998410e7f3SSudeep Holla struct device *dev = &sdev->dev;
300eb1d35c6SCristian Marussi const struct scmi_handle *handle;
30199d6bdf3SSudeep Holla
30299d6bdf3SSudeep Holla handle = sdev->handle;
30399d6bdf3SSudeep Holla
304eb1d35c6SCristian Marussi if (!handle)
30599d6bdf3SSudeep Holla return -ENODEV;
30699d6bdf3SSudeep Holla
307eb1d35c6SCristian Marussi perf_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PERF, &ph);
308eb1d35c6SCristian Marussi if (IS_ERR(perf_ops))
309eb1d35c6SCristian Marussi return PTR_ERR(perf_ops);
310eb1d35c6SCristian Marussi
311f943849fSSudeep Holla #ifdef CONFIG_COMMON_CLK
3128410e7f3SSudeep Holla /* dummy clock provider as needed by OPP if clocks property is used */
3135dd08ac0SAlexandra Diupina if (of_property_present(dev->of_node, "#clock-cells")) {
3145dd08ac0SAlexandra Diupina ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL);
3155dd08ac0SAlexandra Diupina if (ret)
3165dd08ac0SAlexandra Diupina return dev_err_probe(dev, ret, "%s: registering clock provider failed\n", __func__);
3175dd08ac0SAlexandra Diupina }
318f943849fSSudeep Holla #endif
3198410e7f3SSudeep Holla
32099d6bdf3SSudeep Holla ret = cpufreq_register_driver(&scmi_cpufreq_driver);
32199d6bdf3SSudeep Holla if (ret) {
322f943849fSSudeep Holla dev_err(dev, "%s: registering cpufreq failed, err: %d\n",
32399d6bdf3SSudeep Holla __func__, ret);
32499d6bdf3SSudeep Holla }
32599d6bdf3SSudeep Holla
32699d6bdf3SSudeep Holla return ret;
32799d6bdf3SSudeep Holla }
32899d6bdf3SSudeep Holla
scmi_cpufreq_remove(struct scmi_device * sdev)32999d6bdf3SSudeep Holla static void scmi_cpufreq_remove(struct scmi_device *sdev)
33099d6bdf3SSudeep Holla {
33199d6bdf3SSudeep Holla cpufreq_unregister_driver(&scmi_cpufreq_driver);
33299d6bdf3SSudeep Holla }
33399d6bdf3SSudeep Holla
33499d6bdf3SSudeep Holla static const struct scmi_device_id scmi_id_table[] = {
33512b76626SSudeep Holla { SCMI_PROTOCOL_PERF, "cpufreq" },
33699d6bdf3SSudeep Holla { },
33799d6bdf3SSudeep Holla };
33899d6bdf3SSudeep Holla MODULE_DEVICE_TABLE(scmi, scmi_id_table);
33999d6bdf3SSudeep Holla
34099d6bdf3SSudeep Holla static struct scmi_driver scmi_cpufreq_drv = {
34199d6bdf3SSudeep Holla .name = "scmi-cpufreq",
34299d6bdf3SSudeep Holla .probe = scmi_cpufreq_probe,
34399d6bdf3SSudeep Holla .remove = scmi_cpufreq_remove,
34499d6bdf3SSudeep Holla .id_table = scmi_id_table,
34599d6bdf3SSudeep Holla };
34699d6bdf3SSudeep Holla module_scmi_driver(scmi_cpufreq_drv);
34799d6bdf3SSudeep Holla
34899d6bdf3SSudeep Holla MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
34999d6bdf3SSudeep Holla MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
35099d6bdf3SSudeep Holla MODULE_LICENSE("GPL v2");
351