Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.15.
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * System Control and Power Interface (SCMI) based CPUFreq Interface driver
  4 *
  5 * Copyright (C) 2018 ARM Ltd.
  6 * Sudeep Holla <sudeep.holla@arm.com>
  7 */
  8
  9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 10
 11#include <linux/cpu.h>
 12#include <linux/cpufreq.h>
 13#include <linux/cpumask.h>
 14#include <linux/energy_model.h>
 15#include <linux/export.h>
 16#include <linux/module.h>
 17#include <linux/pm_opp.h>
 18#include <linux/slab.h>
 19#include <linux/scmi_protocol.h>
 20#include <linux/types.h>
 21
 22struct scmi_data {
 23	int domain_id;
 24	struct device *cpu_dev;
 25};
 26
 27static const struct scmi_handle *handle;
 28
 29static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
 30{
 31	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
 32	struct scmi_perf_ops *perf_ops = handle->perf_ops;
 33	struct scmi_data *priv = policy->driver_data;
 34	unsigned long rate;
 35	int ret;
 36
 37	ret = perf_ops->freq_get(handle, priv->domain_id, &rate, false);
 38	if (ret)
 39		return 0;
 40	return rate / 1000;
 41}
 42
 43/*
 44 * perf_ops->freq_set is not a synchronous, the actual OPP change will
 45 * happen asynchronously and can get notified if the events are
 46 * subscribed for by the SCMI firmware
 47 */
 48static int
 49scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
 50{
 51	int ret;
 52	struct scmi_data *priv = policy->driver_data;
 53	struct scmi_perf_ops *perf_ops = handle->perf_ops;
 54	u64 freq = policy->freq_table[index].frequency;
 55
 56	ret = perf_ops->freq_set(handle, priv->domain_id, freq * 1000, false);
 57	if (!ret)
 58		arch_set_freq_scale(policy->related_cpus, freq,
 59				    policy->cpuinfo.max_freq);
 60	return ret;
 61}
 62
 63static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy,
 64					     unsigned int target_freq)
 65{
 66	struct scmi_data *priv = policy->driver_data;
 67	struct scmi_perf_ops *perf_ops = handle->perf_ops;
 68
 69	if (!perf_ops->freq_set(handle, priv->domain_id,
 70				target_freq * 1000, true)) {
 71		arch_set_freq_scale(policy->related_cpus, target_freq,
 72				    policy->cpuinfo.max_freq);
 73		return target_freq;
 74	}
 75
 76	return 0;
 77}
 78
 79static int
 80scmi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
 81{
 82	int cpu, domain, tdomain;
 83	struct device *tcpu_dev;
 84
 85	domain = handle->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 = handle->perf_ops->device_domain_id(tcpu_dev);
 98		if (tdomain == domain)
 99			cpumask_set_cpu(cpu, cpumask);
100	}
101
102	return 0;
103}
104
105static int __maybe_unused
106scmi_get_cpu_power(unsigned long *power, unsigned long *KHz,
107		   struct device *cpu_dev)
108{
109	unsigned long Hz;
110	int ret, domain;
111
112	domain = handle->perf_ops->device_domain_id(cpu_dev);
113	if (domain < 0)
114		return domain;
115
116	/* Get the power cost of the performance domain. */
117	Hz = *KHz * 1000;
118	ret = handle->perf_ops->est_power_get(handle, domain, &Hz, power);
119	if (ret)
120		return ret;
121
122	/* The EM framework specifies the frequency in KHz. */
123	*KHz = Hz / 1000;
124
125	return 0;
126}
127
128static int scmi_cpufreq_init(struct cpufreq_policy *policy)
129{
130	int ret, nr_opp;
131	unsigned int latency;
132	struct device *cpu_dev;
133	struct scmi_data *priv;
134	struct cpufreq_frequency_table *freq_table;
135	struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
136
137	cpu_dev = get_cpu_device(policy->cpu);
138	if (!cpu_dev) {
139		pr_err("failed to get cpu%d device\n", policy->cpu);
140		return -ENODEV;
141	}
142
143	ret = handle->perf_ops->device_opps_add(handle, cpu_dev);
144	if (ret) {
145		dev_warn(cpu_dev, "failed to add opps to the device\n");
146		return ret;
147	}
148
149	ret = scmi_get_sharing_cpus(cpu_dev, policy->cpus);
150	if (ret) {
151		dev_warn(cpu_dev, "failed to get sharing cpumask\n");
152		return ret;
153	}
154
155	ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
156	if (ret) {
157		dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
158			__func__, ret);
159		return ret;
160	}
161
162	nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
163	if (nr_opp <= 0) {
164		dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
165		ret = -EPROBE_DEFER;
166		goto out_free_opp;
167	}
168
169	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
170	if (!priv) {
171		ret = -ENOMEM;
172		goto out_free_opp;
173	}
174
175	ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
176	if (ret) {
177		dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
178		goto out_free_priv;
179	}
180
181	priv->cpu_dev = cpu_dev;
182	priv->domain_id = handle->perf_ops->device_domain_id(cpu_dev);
183
184	policy->driver_data = priv;
185	policy->freq_table = freq_table;
186
187	/* SCMI allows DVFS request for any domain from any CPU */
188	policy->dvfs_possible_from_any_cpu = true;
189
190	latency = handle->perf_ops->transition_latency_get(handle, cpu_dev);
191	if (!latency)
192		latency = CPUFREQ_ETERNAL;
193
194	policy->cpuinfo.transition_latency = latency;
195
196	policy->fast_switch_possible =
197		handle->perf_ops->fast_switch_possible(handle, cpu_dev);
198
199	em_dev_register_perf_domain(cpu_dev, nr_opp, &em_cb, policy->cpus);
200
201	return 0;
202
203out_free_priv:
204	kfree(priv);
205out_free_opp:
206	dev_pm_opp_remove_all_dynamic(cpu_dev);
207
208	return ret;
209}
210
211static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
212{
213	struct scmi_data *priv = policy->driver_data;
214
215	dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
216	dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
217	kfree(priv);
218
219	return 0;
220}
221
222static struct cpufreq_driver scmi_cpufreq_driver = {
223	.name	= "scmi",
224	.flags	= CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
225		  CPUFREQ_NEED_INITIAL_FREQ_CHECK |
226		  CPUFREQ_IS_COOLING_DEV,
227	.verify	= cpufreq_generic_frequency_table_verify,
228	.attr	= cpufreq_generic_attr,
229	.target_index	= scmi_cpufreq_set_target,
230	.fast_switch	= scmi_cpufreq_fast_switch,
231	.get	= scmi_cpufreq_get_rate,
232	.init	= scmi_cpufreq_init,
233	.exit	= scmi_cpufreq_exit,
234};
235
236static int scmi_cpufreq_probe(struct scmi_device *sdev)
237{
238	int ret;
239
240	handle = sdev->handle;
241
242	if (!handle || !handle->perf_ops)
243		return -ENODEV;
244
245	ret = cpufreq_register_driver(&scmi_cpufreq_driver);
246	if (ret) {
247		dev_err(&sdev->dev, "%s: registering cpufreq failed, err: %d\n",
248			__func__, ret);
249	}
250
251	return ret;
252}
253
254static void scmi_cpufreq_remove(struct scmi_device *sdev)
255{
256	cpufreq_unregister_driver(&scmi_cpufreq_driver);
257}
258
259static const struct scmi_device_id scmi_id_table[] = {
260	{ SCMI_PROTOCOL_PERF, "cpufreq" },
261	{ },
262};
263MODULE_DEVICE_TABLE(scmi, scmi_id_table);
264
265static struct scmi_driver scmi_cpufreq_drv = {
266	.name		= "scmi-cpufreq",
267	.probe		= scmi_cpufreq_probe,
268	.remove		= scmi_cpufreq_remove,
269	.id_table	= scmi_id_table,
270};
271module_scmi_driver(scmi_cpufreq_drv);
272
273MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
274MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
275MODULE_LICENSE("GPL v2");