1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Power Interface (SCMI) based CPUFreq Interface driver
5 * Copyright (C) 2018-2021 ARM Ltd.
6 * Sudeep Holla <sudeep.holla@arm.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/clk-provider.h>
12 #include <linux/cpu.h>
13 #include <linux/cpufreq.h>
14 #include <linux/cpumask.h>
15 #include <linux/energy_model.h>
16 #include <linux/export.h>
17 #include <linux/module.h>
18 #include <linux/pm_opp.h>
19 #include <linux/slab.h>
20 #include <linux/scmi_protocol.h>
21 #include <linux/types.h>
26 struct device *cpu_dev;
27 cpumask_var_t opp_shared_cpus;
30 static struct scmi_protocol_handle *ph;
31 static const struct scmi_perf_proto_ops *perf_ops;
33 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
35 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
36 struct scmi_data *priv = policy->driver_data;
40 ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false);
47 * perf_ops->freq_set is not a synchronous, the actual OPP change will
48 * happen asynchronously and can get notified if the events are
49 * subscribed for by the SCMI firmware
52 scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
54 struct scmi_data *priv = policy->driver_data;
55 u64 freq = policy->freq_table[index].frequency;
57 return perf_ops->freq_set(ph, priv->domain_id, freq * 1000, false);
60 static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy,
61 unsigned int target_freq)
63 struct scmi_data *priv = policy->driver_data;
65 if (!perf_ops->freq_set(ph, priv->domain_id,
66 target_freq * 1000, true))
73 scmi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
75 int cpu, domain, tdomain;
76 struct device *tcpu_dev;
78 domain = perf_ops->device_domain_id(cpu_dev);
82 for_each_possible_cpu(cpu) {
83 if (cpu == cpu_dev->id)
86 tcpu_dev = get_cpu_device(cpu);
90 tdomain = perf_ops->device_domain_id(tcpu_dev);
91 if (tdomain == domain)
92 cpumask_set_cpu(cpu, cpumask);
98 static int __maybe_unused
99 scmi_get_cpu_power(unsigned long *power, unsigned long *KHz,
100 struct device *cpu_dev)
105 domain = perf_ops->device_domain_id(cpu_dev);
109 /* Get the power cost of the performance domain. */
111 ret = perf_ops->est_power_get(ph, domain, &Hz, power);
115 /* The EM framework specifies the frequency in KHz. */
121 static int scmi_cpufreq_init(struct cpufreq_policy *policy)
124 unsigned int latency;
125 struct device *cpu_dev;
126 struct scmi_data *priv;
127 struct cpufreq_frequency_table *freq_table;
129 cpu_dev = get_cpu_device(policy->cpu);
131 pr_err("failed to get cpu%d device\n", policy->cpu);
135 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
139 if (!zalloc_cpumask_var(&priv->opp_shared_cpus, GFP_KERNEL)) {
144 /* Obtain CPUs that share SCMI performance controls */
145 ret = scmi_get_sharing_cpus(cpu_dev, policy->cpus);
147 dev_warn(cpu_dev, "failed to get sharing cpumask\n");
148 goto out_free_cpumask;
152 * Obtain CPUs that share performance levels.
153 * The OPP 'sharing cpus' info may come from DT through an empty opp
154 * table and opp-shared.
156 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
157 if (ret || !cpumask_weight(priv->opp_shared_cpus)) {
159 * Either opp-table is not set or no opp-shared was found.
160 * Use the CPU mask from SCMI to designate CPUs sharing an OPP
163 cpumask_copy(priv->opp_shared_cpus, policy->cpus);
167 * A previous CPU may have marked OPPs as shared for a few CPUs, based on
168 * what OPP core provided. If the current CPU is part of those few, then
169 * there is no need to add OPPs again.
171 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
173 ret = perf_ops->device_opps_add(ph, cpu_dev);
175 dev_warn(cpu_dev, "failed to add opps to the device\n");
176 goto out_free_cpumask;
179 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
181 dev_err(cpu_dev, "%s: No OPPs for this device: %d\n",
188 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
190 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
196 priv->nr_opp = nr_opp;
199 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
201 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
205 priv->cpu_dev = cpu_dev;
206 priv->domain_id = perf_ops->device_domain_id(cpu_dev);
208 policy->driver_data = priv;
209 policy->freq_table = freq_table;
211 /* SCMI allows DVFS request for any domain from any CPU */
212 policy->dvfs_possible_from_any_cpu = true;
214 latency = perf_ops->transition_latency_get(ph, cpu_dev);
216 latency = CPUFREQ_ETERNAL;
218 policy->cpuinfo.transition_latency = latency;
220 policy->fast_switch_possible =
221 perf_ops->fast_switch_possible(ph, cpu_dev);
226 dev_pm_opp_remove_all_dynamic(cpu_dev);
229 free_cpumask_var(priv->opp_shared_cpus);
237 static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
239 struct scmi_data *priv = policy->driver_data;
241 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
242 dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
243 free_cpumask_var(priv->opp_shared_cpus);
249 static void scmi_cpufreq_register_em(struct cpufreq_policy *policy)
251 struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
252 bool power_scale_mw = perf_ops->power_scale_mw_get(ph);
253 struct scmi_data *priv = policy->driver_data;
256 * This callback will be called for each policy, but we don't need to
257 * register with EM every time. Despite not being part of the same
258 * policy, some CPUs may still share their perf-domains, and a CPU from
259 * another policy may already have registered with EM on behalf of CPUs
265 em_dev_register_perf_domain(get_cpu_device(policy->cpu), priv->nr_opp,
266 &em_cb, priv->opp_shared_cpus,
270 static struct cpufreq_driver scmi_cpufreq_driver = {
272 .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
273 CPUFREQ_NEED_INITIAL_FREQ_CHECK |
274 CPUFREQ_IS_COOLING_DEV,
275 .verify = cpufreq_generic_frequency_table_verify,
276 .attr = cpufreq_generic_attr,
277 .target_index = scmi_cpufreq_set_target,
278 .fast_switch = scmi_cpufreq_fast_switch,
279 .get = scmi_cpufreq_get_rate,
280 .init = scmi_cpufreq_init,
281 .exit = scmi_cpufreq_exit,
282 .register_em = scmi_cpufreq_register_em,
285 static int scmi_cpufreq_probe(struct scmi_device *sdev)
288 struct device *dev = &sdev->dev;
289 const struct scmi_handle *handle;
291 handle = sdev->handle;
296 perf_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PERF, &ph);
297 if (IS_ERR(perf_ops))
298 return PTR_ERR(perf_ops);
300 #ifdef CONFIG_COMMON_CLK
301 /* dummy clock provider as needed by OPP if clocks property is used */
302 if (of_find_property(dev->of_node, "#clock-cells", NULL))
303 devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL);
306 ret = cpufreq_register_driver(&scmi_cpufreq_driver);
308 dev_err(dev, "%s: registering cpufreq failed, err: %d\n",
315 static void scmi_cpufreq_remove(struct scmi_device *sdev)
317 cpufreq_unregister_driver(&scmi_cpufreq_driver);
320 static const struct scmi_device_id scmi_id_table[] = {
321 { SCMI_PROTOCOL_PERF, "cpufreq" },
324 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
326 static struct scmi_driver scmi_cpufreq_drv = {
327 .name = "scmi-cpufreq",
328 .probe = scmi_cpufreq_probe,
329 .remove = scmi_cpufreq_remove,
330 .id_table = scmi_id_table,
332 module_scmi_driver(scmi_cpufreq_drv);
334 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
335 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
336 MODULE_LICENSE("GPL v2");