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