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