1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * System Control and Power Interface (SCMI) based CPUFreq Interface driver
4 *
5 * Copyright (C) 2018-2021 ARM Ltd.
6 * Sudeep Holla <sudeep.holla@arm.com>
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
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>
22 #include <linux/units.h>
23
24 struct scmi_data {
25 int domain_id;
26 int nr_opp;
27 struct device *cpu_dev;
28 cpumask_var_t opp_shared_cpus;
29 };
30
31 static struct scmi_protocol_handle *ph;
32 static const struct scmi_perf_proto_ops *perf_ops;
33
scmi_cpufreq_get_rate(unsigned int cpu)34 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
35 {
36 struct cpufreq_policy *policy;
37 struct scmi_data *priv;
38 unsigned long rate;
39 int ret;
40
41 policy = cpufreq_cpu_get_raw(cpu);
42 if (unlikely(!policy))
43 return 0;
44
45 priv = policy->driver_data;
46
47 ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false);
48 if (ret)
49 return 0;
50 return rate / 1000;
51 }
52
53 /*
54 * perf_ops->freq_set is not a synchronous, the actual OPP change will
55 * happen asynchronously and can get notified if the events are
56 * subscribed for by the SCMI firmware
57 */
58 static int
scmi_cpufreq_set_target(struct cpufreq_policy * policy,unsigned int index)59 scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
60 {
61 struct scmi_data *priv = policy->driver_data;
62 u64 freq = policy->freq_table[index].frequency;
63
64 return perf_ops->freq_set(ph, priv->domain_id, freq * 1000, false);
65 }
66
scmi_cpufreq_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)67 static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy,
68 unsigned int target_freq)
69 {
70 struct scmi_data *priv = policy->driver_data;
71 unsigned long freq = target_freq;
72
73 if (!perf_ops->freq_set(ph, priv->domain_id, freq * 1000, true))
74 return target_freq;
75
76 return 0;
77 }
78
79 static int
scmi_get_sharing_cpus(struct device * cpu_dev,struct cpumask * cpumask)80 scmi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
81 {
82 int cpu, domain, tdomain;
83 struct device *tcpu_dev;
84
85 domain = perf_ops->device_domain_id(cpu_dev);
86 if (domain < 0)
87 return domain;
88
89 for_each_possible_cpu(cpu) {
90 if (cpu == cpu_dev->id)
91 continue;
92
93 tcpu_dev = get_cpu_device(cpu);
94 if (!tcpu_dev)
95 continue;
96
97 tdomain = perf_ops->device_domain_id(tcpu_dev);
98 if (tdomain == domain)
99 cpumask_set_cpu(cpu, cpumask);
100 }
101
102 return 0;
103 }
104
105 static int __maybe_unused
scmi_get_cpu_power(struct device * cpu_dev,unsigned long * power,unsigned long * KHz)106 scmi_get_cpu_power(struct device *cpu_dev, unsigned long *power,
107 unsigned long *KHz)
108 {
109 enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph);
110 unsigned long Hz;
111 int ret, domain;
112
113 domain = perf_ops->device_domain_id(cpu_dev);
114 if (domain < 0)
115 return domain;
116
117 /* Get the power cost of the performance domain. */
118 Hz = *KHz * 1000;
119 ret = perf_ops->est_power_get(ph, domain, &Hz, power);
120 if (ret)
121 return ret;
122
123 /* Convert the power to uW if it is mW (ignore bogoW) */
124 if (power_scale == SCMI_POWER_MILLIWATTS)
125 *power *= MICROWATT_PER_MILLIWATT;
126
127 /* The EM framework specifies the frequency in KHz. */
128 *KHz = Hz / 1000;
129
130 return 0;
131 }
132
scmi_cpufreq_init(struct cpufreq_policy * policy)133 static int scmi_cpufreq_init(struct cpufreq_policy *policy)
134 {
135 int ret, nr_opp;
136 unsigned int latency;
137 struct device *cpu_dev;
138 struct scmi_data *priv;
139 struct cpufreq_frequency_table *freq_table;
140
141 cpu_dev = get_cpu_device(policy->cpu);
142 if (!cpu_dev) {
143 pr_err("failed to get cpu%d device\n", policy->cpu);
144 return -ENODEV;
145 }
146
147 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
148 if (!priv)
149 return -ENOMEM;
150
151 if (!zalloc_cpumask_var(&priv->opp_shared_cpus, GFP_KERNEL)) {
152 ret = -ENOMEM;
153 goto out_free_priv;
154 }
155
156 /* Obtain CPUs that share SCMI performance controls */
157 ret = scmi_get_sharing_cpus(cpu_dev, policy->cpus);
158 if (ret) {
159 dev_warn(cpu_dev, "failed to get sharing cpumask\n");
160 goto out_free_cpumask;
161 }
162
163 /*
164 * Obtain CPUs that share performance levels.
165 * The OPP 'sharing cpus' info may come from DT through an empty opp
166 * table and opp-shared.
167 */
168 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
169 if (ret || cpumask_empty(priv->opp_shared_cpus)) {
170 /*
171 * Either opp-table is not set or no opp-shared was found.
172 * Use the CPU mask from SCMI to designate CPUs sharing an OPP
173 * table.
174 */
175 cpumask_copy(priv->opp_shared_cpus, policy->cpus);
176 }
177
178 /*
179 * A previous CPU may have marked OPPs as shared for a few CPUs, based on
180 * what OPP core provided. If the current CPU is part of those few, then
181 * there is no need to add OPPs again.
182 */
183 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
184 if (nr_opp <= 0) {
185 ret = perf_ops->device_opps_add(ph, cpu_dev);
186 if (ret) {
187 dev_warn(cpu_dev, "failed to add opps to the device\n");
188 goto out_free_cpumask;
189 }
190
191 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
192 if (nr_opp <= 0) {
193 dev_err(cpu_dev, "%s: No OPPs for this device: %d\n",
194 __func__, nr_opp);
195
196 ret = -ENODEV;
197 goto out_free_opp;
198 }
199
200 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
201 if (ret) {
202 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
203 __func__, ret);
204
205 goto out_free_opp;
206 }
207
208 priv->nr_opp = nr_opp;
209 }
210
211 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
212 if (ret) {
213 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
214 goto out_free_opp;
215 }
216
217 priv->cpu_dev = cpu_dev;
218 priv->domain_id = perf_ops->device_domain_id(cpu_dev);
219
220 policy->driver_data = priv;
221 policy->freq_table = freq_table;
222
223 /* SCMI allows DVFS request for any domain from any CPU */
224 policy->dvfs_possible_from_any_cpu = true;
225
226 latency = perf_ops->transition_latency_get(ph, cpu_dev);
227 if (!latency)
228 latency = CPUFREQ_ETERNAL;
229
230 policy->cpuinfo.transition_latency = latency;
231
232 policy->fast_switch_possible =
233 perf_ops->fast_switch_possible(ph, cpu_dev);
234
235 return 0;
236
237 out_free_opp:
238 dev_pm_opp_remove_all_dynamic(cpu_dev);
239
240 out_free_cpumask:
241 free_cpumask_var(priv->opp_shared_cpus);
242
243 out_free_priv:
244 kfree(priv);
245
246 return ret;
247 }
248
scmi_cpufreq_exit(struct cpufreq_policy * policy)249 static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
250 {
251 struct scmi_data *priv = policy->driver_data;
252
253 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
254 dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
255 free_cpumask_var(priv->opp_shared_cpus);
256 kfree(priv);
257
258 return 0;
259 }
260
scmi_cpufreq_register_em(struct cpufreq_policy * policy)261 static void scmi_cpufreq_register_em(struct cpufreq_policy *policy)
262 {
263 struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
264 enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph);
265 struct scmi_data *priv = policy->driver_data;
266 bool em_power_scale = false;
267
268 /*
269 * This callback will be called for each policy, but we don't need to
270 * register with EM every time. Despite not being part of the same
271 * policy, some CPUs may still share their perf-domains, and a CPU from
272 * another policy may already have registered with EM on behalf of CPUs
273 * of this policy.
274 */
275 if (!priv->nr_opp)
276 return;
277
278 if (power_scale == SCMI_POWER_MILLIWATTS
279 || power_scale == SCMI_POWER_MICROWATTS)
280 em_power_scale = true;
281
282 em_dev_register_perf_domain(get_cpu_device(policy->cpu), priv->nr_opp,
283 &em_cb, priv->opp_shared_cpus,
284 em_power_scale);
285 }
286
287 static struct cpufreq_driver scmi_cpufreq_driver = {
288 .name = "scmi",
289 .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
290 CPUFREQ_NEED_INITIAL_FREQ_CHECK |
291 CPUFREQ_IS_COOLING_DEV,
292 .verify = cpufreq_generic_frequency_table_verify,
293 .attr = cpufreq_generic_attr,
294 .target_index = scmi_cpufreq_set_target,
295 .fast_switch = scmi_cpufreq_fast_switch,
296 .get = scmi_cpufreq_get_rate,
297 .init = scmi_cpufreq_init,
298 .exit = scmi_cpufreq_exit,
299 .register_em = scmi_cpufreq_register_em,
300 };
301
scmi_dev_used_by_cpus(struct device * scmi_dev)302 static bool scmi_dev_used_by_cpus(struct device *scmi_dev)
303 {
304 struct device_node *scmi_np = dev_of_node(scmi_dev);
305 struct device_node *cpu_np, *np;
306 struct device *cpu_dev;
307 int cpu, idx;
308
309 if (!scmi_np)
310 return false;
311
312 for_each_possible_cpu(cpu) {
313 cpu_dev = get_cpu_device(cpu);
314 if (!cpu_dev)
315 continue;
316
317 cpu_np = dev_of_node(cpu_dev);
318
319 np = of_parse_phandle(cpu_np, "clocks", 0);
320 of_node_put(np);
321
322 if (np == scmi_np)
323 return true;
324
325 idx = of_property_match_string(cpu_np, "power-domain-names", "perf");
326 np = of_parse_phandle(cpu_np, "power-domains", idx);
327 of_node_put(np);
328
329 if (np == scmi_np)
330 return true;
331 }
332
333 return false;
334 }
335
scmi_cpufreq_probe(struct scmi_device * sdev)336 static int scmi_cpufreq_probe(struct scmi_device *sdev)
337 {
338 int ret;
339 struct device *dev = &sdev->dev;
340 const struct scmi_handle *handle;
341
342 handle = sdev->handle;
343
344 if (!handle || !scmi_dev_used_by_cpus(dev))
345 return -ENODEV;
346
347 perf_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PERF, &ph);
348 if (IS_ERR(perf_ops))
349 return PTR_ERR(perf_ops);
350
351 #ifdef CONFIG_COMMON_CLK
352 /* dummy clock provider as needed by OPP if clocks property is used */
353 if (of_property_present(dev->of_node, "#clock-cells")) {
354 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL);
355 if (ret)
356 return dev_err_probe(dev, ret, "%s: registering clock provider failed\n", __func__);
357 }
358 #endif
359
360 ret = cpufreq_register_driver(&scmi_cpufreq_driver);
361 if (ret) {
362 dev_err(dev, "%s: registering cpufreq failed, err: %d\n",
363 __func__, ret);
364 }
365
366 return ret;
367 }
368
scmi_cpufreq_remove(struct scmi_device * sdev)369 static void scmi_cpufreq_remove(struct scmi_device *sdev)
370 {
371 cpufreq_unregister_driver(&scmi_cpufreq_driver);
372 }
373
374 static const struct scmi_device_id scmi_id_table[] = {
375 { SCMI_PROTOCOL_PERF, "cpufreq" },
376 { },
377 };
378 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
379
380 static struct scmi_driver scmi_cpufreq_drv = {
381 .name = "scmi-cpufreq",
382 .probe = scmi_cpufreq_probe,
383 .remove = scmi_cpufreq_remove,
384 .id_table = scmi_id_table,
385 };
386 module_scmi_driver(scmi_cpufreq_drv);
387
388 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
389 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
390 MODULE_LICENSE("GPL v2");
391