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