Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / cpufreq / scmi-cpufreq.c
blob491a0a24fb1ed52e024141af887362c7e650695b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * System Control and Power Interface (SCMI) based CPUFreq Interface driver
5 * Copyright (C) 2018 ARM Ltd.
6 * Sudeep Holla <sudeep.holla@arm.com>
7 */
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>
23 struct scmi_data {
24 int domain_id;
25 struct device *cpu_dev;
28 static const struct scmi_handle *handle;
30 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
32 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
33 const struct scmi_perf_ops *perf_ops = handle->perf_ops;
34 struct scmi_data *priv = policy->driver_data;
35 unsigned long rate;
36 int ret;
38 ret = perf_ops->freq_get(handle, priv->domain_id, &rate, false);
39 if (ret)
40 return 0;
41 return rate / 1000;
45 * perf_ops->freq_set is not a synchronous, the actual OPP change will
46 * happen asynchronously and can get notified if the events are
47 * subscribed for by the SCMI firmware
49 static int
50 scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
52 struct scmi_data *priv = policy->driver_data;
53 const struct scmi_perf_ops *perf_ops = handle->perf_ops;
54 u64 freq = policy->freq_table[index].frequency;
56 return perf_ops->freq_set(handle, priv->domain_id, freq * 1000, false);
59 static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy,
60 unsigned int target_freq)
62 struct scmi_data *priv = policy->driver_data;
63 const struct scmi_perf_ops *perf_ops = handle->perf_ops;
65 if (!perf_ops->freq_set(handle, priv->domain_id,
66 target_freq * 1000, true))
67 return target_freq;
69 return 0;
72 static int
73 scmi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
75 int cpu, domain, tdomain;
76 struct device *tcpu_dev;
78 domain = handle->perf_ops->device_domain_id(cpu_dev);
79 if (domain < 0)
80 return domain;
82 for_each_possible_cpu(cpu) {
83 if (cpu == cpu_dev->id)
84 continue;
86 tcpu_dev = get_cpu_device(cpu);
87 if (!tcpu_dev)
88 continue;
90 tdomain = handle->perf_ops->device_domain_id(tcpu_dev);
91 if (tdomain == domain)
92 cpumask_set_cpu(cpu, cpumask);
95 return 0;
98 static int __maybe_unused
99 scmi_get_cpu_power(unsigned long *power, unsigned long *KHz,
100 struct device *cpu_dev)
102 unsigned long Hz;
103 int ret, domain;
105 domain = handle->perf_ops->device_domain_id(cpu_dev);
106 if (domain < 0)
107 return domain;
109 /* Get the power cost of the performance domain. */
110 Hz = *KHz * 1000;
111 ret = handle->perf_ops->est_power_get(handle, domain, &Hz, power);
112 if (ret)
113 return ret;
115 /* The EM framework specifies the frequency in KHz. */
116 *KHz = Hz / 1000;
118 return 0;
121 static int scmi_cpufreq_init(struct cpufreq_policy *policy)
123 int ret, nr_opp;
124 unsigned int latency;
125 struct device *cpu_dev;
126 struct scmi_data *priv;
127 struct cpufreq_frequency_table *freq_table;
128 struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
129 bool power_scale_mw;
131 cpu_dev = get_cpu_device(policy->cpu);
132 if (!cpu_dev) {
133 pr_err("failed to get cpu%d device\n", policy->cpu);
134 return -ENODEV;
137 ret = handle->perf_ops->device_opps_add(handle, cpu_dev);
138 if (ret) {
139 dev_warn(cpu_dev, "failed to add opps to the device\n");
140 return ret;
143 ret = scmi_get_sharing_cpus(cpu_dev, policy->cpus);
144 if (ret) {
145 dev_warn(cpu_dev, "failed to get sharing cpumask\n");
146 return ret;
149 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
150 if (ret) {
151 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
152 __func__, ret);
153 return ret;
156 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
157 if (nr_opp <= 0) {
158 dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
159 ret = -EPROBE_DEFER;
160 goto out_free_opp;
163 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
164 if (!priv) {
165 ret = -ENOMEM;
166 goto out_free_opp;
169 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
170 if (ret) {
171 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
172 goto out_free_priv;
175 priv->cpu_dev = cpu_dev;
176 priv->domain_id = handle->perf_ops->device_domain_id(cpu_dev);
178 policy->driver_data = priv;
179 policy->freq_table = freq_table;
181 /* SCMI allows DVFS request for any domain from any CPU */
182 policy->dvfs_possible_from_any_cpu = true;
184 latency = handle->perf_ops->transition_latency_get(handle, cpu_dev);
185 if (!latency)
186 latency = CPUFREQ_ETERNAL;
188 policy->cpuinfo.transition_latency = latency;
190 policy->fast_switch_possible =
191 handle->perf_ops->fast_switch_possible(handle, cpu_dev);
193 power_scale_mw = handle->perf_ops->power_scale_mw_get(handle);
194 em_dev_register_perf_domain(cpu_dev, nr_opp, &em_cb, policy->cpus,
195 power_scale_mw);
197 return 0;
199 out_free_priv:
200 kfree(priv);
201 out_free_opp:
202 dev_pm_opp_remove_all_dynamic(cpu_dev);
204 return ret;
207 static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
209 struct scmi_data *priv = policy->driver_data;
211 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
212 dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
213 kfree(priv);
215 return 0;
218 static struct cpufreq_driver scmi_cpufreq_driver = {
219 .name = "scmi",
220 .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
221 CPUFREQ_NEED_INITIAL_FREQ_CHECK |
222 CPUFREQ_IS_COOLING_DEV,
223 .verify = cpufreq_generic_frequency_table_verify,
224 .attr = cpufreq_generic_attr,
225 .target_index = scmi_cpufreq_set_target,
226 .fast_switch = scmi_cpufreq_fast_switch,
227 .get = scmi_cpufreq_get_rate,
228 .init = scmi_cpufreq_init,
229 .exit = scmi_cpufreq_exit,
232 static int scmi_cpufreq_probe(struct scmi_device *sdev)
234 int ret;
235 struct device *dev = &sdev->dev;
237 handle = sdev->handle;
239 if (!handle || !handle->perf_ops)
240 return -ENODEV;
242 #ifdef CONFIG_COMMON_CLK
243 /* dummy clock provider as needed by OPP if clocks property is used */
244 if (of_find_property(dev->of_node, "#clock-cells", NULL))
245 devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL);
246 #endif
248 ret = cpufreq_register_driver(&scmi_cpufreq_driver);
249 if (ret) {
250 dev_err(dev, "%s: registering cpufreq failed, err: %d\n",
251 __func__, ret);
254 return ret;
257 static void scmi_cpufreq_remove(struct scmi_device *sdev)
259 cpufreq_unregister_driver(&scmi_cpufreq_driver);
262 static const struct scmi_device_id scmi_id_table[] = {
263 { SCMI_PROTOCOL_PERF, "cpufreq" },
264 { },
266 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
268 static struct scmi_driver scmi_cpufreq_drv = {
269 .name = "scmi-cpufreq",
270 .probe = scmi_cpufreq_probe,
271 .remove = scmi_cpufreq_remove,
272 .id_table = scmi_id_table,
274 module_scmi_driver(scmi_cpufreq_drv);
276 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
277 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
278 MODULE_LICENSE("GPL v2");