1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * System Control and Management 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/of.h>
19 #include <linux/pm_opp.h>
20 #include <linux/pm_qos.h>
21 #include <linux/slab.h>
22 #include <linux/scmi_protocol.h>
23 #include <linux/types.h>
24 #include <linux/units.h>
25
26 struct scmi_data {
27 int domain_id;
28 int nr_opp;
29 struct device *cpu_dev;
30 cpumask_var_t opp_shared_cpus;
31 struct notifier_block limit_notify_nb;
32 struct freq_qos_request limits_freq_req;
33 };
34
35 static struct scmi_protocol_handle *ph;
36 static const struct scmi_perf_proto_ops *perf_ops;
37 static struct cpufreq_driver scmi_cpufreq_driver;
38
scmi_cpufreq_get_rate(unsigned int cpu)39 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
40 {
41 struct cpufreq_policy *policy;
42 struct scmi_data *priv;
43 unsigned long rate;
44 int ret;
45
46 policy = cpufreq_cpu_get_raw(cpu);
47 if (unlikely(!policy))
48 return 0;
49
50 priv = policy->driver_data;
51
52 ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false);
53 if (ret)
54 return 0;
55 return rate / 1000;
56 }
57
58 /*
59 * perf_ops->freq_set is not a synchronous, the actual OPP change will
60 * happen asynchronously and can get notified if the events are
61 * subscribed for by the SCMI firmware
62 */
63 static int
scmi_cpufreq_set_target(struct cpufreq_policy * policy,unsigned int index)64 scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
65 {
66 struct scmi_data *priv = policy->driver_data;
67 u64 freq = policy->freq_table[index].frequency;
68
69 return perf_ops->freq_set(ph, priv->domain_id, freq * 1000, false);
70 }
71
scmi_cpufreq_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)72 static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy,
73 unsigned int target_freq)
74 {
75 struct scmi_data *priv = policy->driver_data;
76 unsigned long freq = target_freq;
77
78 if (!perf_ops->freq_set(ph, priv->domain_id, freq * 1000, true))
79 return target_freq;
80
81 return 0;
82 }
83
scmi_cpu_domain_id(struct device * cpu_dev)84 static int scmi_cpu_domain_id(struct device *cpu_dev)
85 {
86 struct device_node *np = cpu_dev->of_node;
87 struct of_phandle_args domain_id;
88 int index;
89
90 if (of_parse_phandle_with_args(np, "clocks", "#clock-cells", 0,
91 &domain_id)) {
92 /* Find the corresponding index for power-domain "perf". */
93 index = of_property_match_string(np, "power-domain-names",
94 "perf");
95 if (index < 0)
96 return -EINVAL;
97
98 if (of_parse_phandle_with_args(np, "power-domains",
99 "#power-domain-cells", index,
100 &domain_id))
101 return -EINVAL;
102 }
103
104 of_node_put(domain_id.np);
105 return domain_id.args[0];
106 }
107
108 static int
scmi_get_sharing_cpus(struct device * cpu_dev,int domain,struct cpumask * cpumask)109 scmi_get_sharing_cpus(struct device *cpu_dev, int domain,
110 struct cpumask *cpumask)
111 {
112 int cpu, tdomain;
113 struct device *tcpu_dev;
114
115 for_each_present_cpu(cpu) {
116 if (cpu == cpu_dev->id)
117 continue;
118
119 tcpu_dev = get_cpu_device(cpu);
120 if (!tcpu_dev)
121 continue;
122
123 tdomain = scmi_cpu_domain_id(tcpu_dev);
124 if (tdomain == domain)
125 cpumask_set_cpu(cpu, cpumask);
126 }
127
128 return 0;
129 }
130
131 static int __maybe_unused
scmi_get_cpu_power(struct device * cpu_dev,unsigned long * power,unsigned long * KHz)132 scmi_get_cpu_power(struct device *cpu_dev, unsigned long *power,
133 unsigned long *KHz)
134 {
135 enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph);
136 unsigned long Hz;
137 int ret, domain;
138
139 domain = scmi_cpu_domain_id(cpu_dev);
140 if (domain < 0)
141 return domain;
142
143 /* Get the power cost of the performance domain. */
144 Hz = *KHz * 1000;
145 ret = perf_ops->est_power_get(ph, domain, &Hz, power);
146 if (ret)
147 return ret;
148
149 /* Convert the power to uW if it is mW (ignore bogoW) */
150 if (power_scale == SCMI_POWER_MILLIWATTS)
151 *power *= MICROWATT_PER_MILLIWATT;
152
153 /* The EM framework specifies the frequency in KHz. */
154 *KHz = Hz / 1000;
155
156 return 0;
157 }
158
159 static int
scmi_get_rate_limit(u32 domain,bool has_fast_switch)160 scmi_get_rate_limit(u32 domain, bool has_fast_switch)
161 {
162 int ret, rate_limit;
163
164 if (has_fast_switch) {
165 /*
166 * Fast channels are used whenever available,
167 * so use their rate_limit value if populated.
168 */
169 ret = perf_ops->fast_switch_rate_limit(ph, domain,
170 &rate_limit);
171 if (!ret && rate_limit)
172 return rate_limit;
173 }
174
175 ret = perf_ops->rate_limit_get(ph, domain, &rate_limit);
176 if (ret)
177 return 0;
178
179 return rate_limit;
180 }
181
scmi_limit_notify_cb(struct notifier_block * nb,unsigned long event,void * data)182 static int scmi_limit_notify_cb(struct notifier_block *nb, unsigned long event, void *data)
183 {
184 struct scmi_data *priv = container_of(nb, struct scmi_data, limit_notify_nb);
185 struct scmi_perf_limits_report *limit_notify = data;
186 unsigned int limit_freq_khz;
187 int ret;
188
189 limit_freq_khz = limit_notify->range_max_freq / HZ_PER_KHZ;
190
191 ret = freq_qos_update_request(&priv->limits_freq_req, limit_freq_khz);
192 if (ret < 0)
193 pr_warn("failed to update freq constraint: %d\n", ret);
194
195 return NOTIFY_OK;
196 }
197
scmi_cpufreq_init(struct cpufreq_policy * policy)198 static int scmi_cpufreq_init(struct cpufreq_policy *policy)
199 {
200 int ret, nr_opp, domain;
201 unsigned int latency;
202 struct device *cpu_dev;
203 struct scmi_data *priv;
204 struct cpufreq_frequency_table *freq_table;
205 struct scmi_device *sdev = cpufreq_get_driver_data();
206
207 cpu_dev = get_cpu_device(policy->cpu);
208 if (!cpu_dev) {
209 pr_err("failed to get cpu%d device\n", policy->cpu);
210 return -ENODEV;
211 }
212
213 domain = scmi_cpu_domain_id(cpu_dev);
214 if (domain < 0)
215 return domain;
216
217 priv = kzalloc_obj(*priv);
218 if (!priv)
219 return -ENOMEM;
220
221 if (!zalloc_cpumask_var(&priv->opp_shared_cpus, GFP_KERNEL)) {
222 ret = -ENOMEM;
223 goto out_free_priv;
224 }
225
226 /* Obtain CPUs that share SCMI performance controls */
227 ret = scmi_get_sharing_cpus(cpu_dev, domain, policy->cpus);
228 if (ret) {
229 dev_warn(cpu_dev, "failed to get sharing cpumask\n");
230 goto out_free_cpumask;
231 }
232
233 /*
234 * Obtain CPUs that share performance levels.
235 * The OPP 'sharing cpus' info may come from DT through an empty opp
236 * table and opp-shared.
237 */
238 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
239 if (ret || cpumask_empty(priv->opp_shared_cpus)) {
240 /*
241 * Either opp-table is not set or no opp-shared was found.
242 * Use the CPU mask from SCMI to designate CPUs sharing an OPP
243 * table.
244 */
245 cpumask_copy(priv->opp_shared_cpus, policy->cpus);
246 }
247
248 /*
249 * A previous CPU may have marked OPPs as shared for a few CPUs, based on
250 * what OPP core provided. If the current CPU is part of those few, then
251 * there is no need to add OPPs again.
252 */
253 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
254 if (nr_opp <= 0) {
255 ret = perf_ops->device_opps_add(ph, cpu_dev, domain);
256 if (ret) {
257 dev_warn(cpu_dev, "failed to add opps to the device\n");
258 goto out_free_cpumask;
259 }
260
261 nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
262 if (nr_opp <= 0) {
263 dev_err(cpu_dev, "%s: No OPPs for this device: %d\n",
264 __func__, nr_opp);
265
266 ret = -ENODEV;
267 goto out_free_opp;
268 }
269
270 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->opp_shared_cpus);
271 if (ret) {
272 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
273 __func__, ret);
274
275 goto out_free_opp;
276 }
277
278 priv->nr_opp = nr_opp;
279 }
280
281 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
282 if (ret) {
283 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
284 goto out_free_opp;
285 }
286
287 priv->cpu_dev = cpu_dev;
288 priv->domain_id = domain;
289
290 policy->driver_data = priv;
291 policy->freq_table = freq_table;
292
293 /* SCMI allows DVFS request for any domain from any CPU */
294 policy->dvfs_possible_from_any_cpu = true;
295
296 latency = perf_ops->transition_latency_get(ph, domain);
297 if (!latency)
298 latency = CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS;
299
300 policy->cpuinfo.transition_latency = latency;
301
302 policy->fast_switch_possible =
303 perf_ops->fast_switch_possible(ph, domain);
304
305 policy->transition_delay_us =
306 scmi_get_rate_limit(domain, policy->fast_switch_possible);
307
308 ret = freq_qos_add_request(&policy->constraints, &priv->limits_freq_req, FREQ_QOS_MAX,
309 FREQ_QOS_MAX_DEFAULT_VALUE);
310 if (ret < 0) {
311 dev_err(cpu_dev, "failed to add qos limits request: %d\n", ret);
312 goto out_free_table;
313 }
314
315 priv->limit_notify_nb.notifier_call = scmi_limit_notify_cb;
316 ret = sdev->handle->notify_ops->event_notifier_register(sdev->handle, SCMI_PROTOCOL_PERF,
317 SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED,
318 &priv->domain_id,
319 &priv->limit_notify_nb);
320 if (ret)
321 dev_warn(&sdev->dev,
322 "failed to register for limits change notifier for domain %d\n",
323 priv->domain_id);
324
325 return 0;
326
327 out_free_table:
328 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table);
329 out_free_opp:
330 dev_pm_opp_remove_all_dynamic(cpu_dev);
331
332 out_free_cpumask:
333 free_cpumask_var(priv->opp_shared_cpus);
334
335 out_free_priv:
336 kfree(priv);
337
338 return ret;
339 }
340
scmi_cpufreq_exit(struct cpufreq_policy * policy)341 static void scmi_cpufreq_exit(struct cpufreq_policy *policy)
342 {
343 struct scmi_data *priv = policy->driver_data;
344 struct scmi_device *sdev = cpufreq_get_driver_data();
345
346 sdev->handle->notify_ops->event_notifier_unregister(sdev->handle, SCMI_PROTOCOL_PERF,
347 SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED,
348 &priv->domain_id,
349 &priv->limit_notify_nb);
350 freq_qos_remove_request(&priv->limits_freq_req);
351 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
352 dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
353 free_cpumask_var(priv->opp_shared_cpus);
354 kfree(priv);
355 }
356
scmi_cpufreq_register_em(struct cpufreq_policy * policy)357 static void scmi_cpufreq_register_em(struct cpufreq_policy *policy)
358 {
359 struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
360 enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph);
361 struct scmi_data *priv = policy->driver_data;
362 bool em_power_scale = false;
363
364 /*
365 * This callback will be called for each policy, but we don't need to
366 * register with EM every time. Despite not being part of the same
367 * policy, some CPUs may still share their perf-domains, and a CPU from
368 * another policy may already have registered with EM on behalf of CPUs
369 * of this policy.
370 */
371 if (!priv->nr_opp)
372 return;
373
374 if (power_scale == SCMI_POWER_MILLIWATTS
375 || power_scale == SCMI_POWER_MICROWATTS)
376 em_power_scale = true;
377
378 em_dev_register_perf_domain(get_cpu_device(policy->cpu), priv->nr_opp,
379 &em_cb, priv->opp_shared_cpus,
380 em_power_scale);
381 }
382
383 static struct cpufreq_driver scmi_cpufreq_driver = {
384 .name = "scmi",
385 .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
386 CPUFREQ_NEED_INITIAL_FREQ_CHECK |
387 CPUFREQ_IS_COOLING_DEV,
388 .verify = cpufreq_generic_frequency_table_verify,
389 .target_index = scmi_cpufreq_set_target,
390 .fast_switch = scmi_cpufreq_fast_switch,
391 .get = scmi_cpufreq_get_rate,
392 .init = scmi_cpufreq_init,
393 .exit = scmi_cpufreq_exit,
394 .register_em = scmi_cpufreq_register_em,
395 .set_boost = cpufreq_boost_set_sw,
396 };
397
scmi_dev_used_by_cpus(struct device * scmi_dev)398 static bool scmi_dev_used_by_cpus(struct device *scmi_dev)
399 {
400 struct device_node *scmi_np = dev_of_node(scmi_dev);
401 struct device_node *cpu_np, *np;
402 struct device *cpu_dev;
403 int cpu, idx;
404
405 if (!scmi_np)
406 return false;
407
408 for_each_possible_cpu(cpu) {
409 cpu_dev = get_cpu_device(cpu);
410 if (!cpu_dev)
411 continue;
412
413 cpu_np = dev_of_node(cpu_dev);
414
415 np = of_parse_phandle(cpu_np, "clocks", 0);
416 of_node_put(np);
417
418 if (np == scmi_np)
419 return true;
420
421 idx = of_property_match_string(cpu_np, "power-domain-names", "perf");
422 np = of_parse_phandle(cpu_np, "power-domains", idx);
423 of_node_put(np);
424
425 if (np == scmi_np)
426 return true;
427 }
428
429 /*
430 * Older Broadcom STB chips had a "clocks" property for CPU node(s)
431 * that did not match the SCMI performance protocol node, if we got
432 * there, it means we had such an older Device Tree, therefore return
433 * true to preserve backwards compatibility.
434 */
435 if (of_machine_is_compatible("brcm,brcmstb"))
436 return true;
437
438 return false;
439 }
440
scmi_cpufreq_probe(struct scmi_device * sdev)441 static int scmi_cpufreq_probe(struct scmi_device *sdev)
442 {
443 int ret;
444 struct device *dev = &sdev->dev;
445 const struct scmi_handle *handle;
446
447 handle = sdev->handle;
448
449 if (!handle || !scmi_dev_used_by_cpus(dev))
450 return -ENODEV;
451
452 scmi_cpufreq_driver.driver_data = sdev;
453
454 perf_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PERF, &ph);
455 if (IS_ERR(perf_ops))
456 return PTR_ERR(perf_ops);
457
458 #ifdef CONFIG_COMMON_CLK
459 /* dummy clock provider as needed by OPP if clocks property is used */
460 if (of_property_present(dev->of_node, "#clock-cells")) {
461 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL);
462 if (ret)
463 return dev_err_probe(dev, ret, "%s: registering clock provider failed\n", __func__);
464 }
465 #endif
466
467 ret = cpufreq_register_driver(&scmi_cpufreq_driver);
468 if (ret) {
469 dev_err(dev, "%s: registering cpufreq failed, err: %d\n",
470 __func__, ret);
471 }
472
473 return ret;
474 }
475
scmi_cpufreq_remove(struct scmi_device * sdev)476 static void scmi_cpufreq_remove(struct scmi_device *sdev)
477 {
478 cpufreq_unregister_driver(&scmi_cpufreq_driver);
479 }
480
481 static const struct scmi_device_id scmi_id_table[] = {
482 { SCMI_PROTOCOL_PERF, "cpufreq" },
483 { },
484 };
485 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
486
487 static struct scmi_driver scmi_cpufreq_drv = {
488 .name = "scmi-cpufreq",
489 .probe = scmi_cpufreq_probe,
490 .remove = scmi_cpufreq_remove,
491 .id_table = scmi_id_table,
492 };
493 module_scmi_driver(scmi_cpufreq_drv);
494
495 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
496 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
497 MODULE_LICENSE("GPL v2");
498