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 
22 struct scmi_data {
23 	int domain_id;
24 	struct device *cpu_dev;
25 };
26 
27 static const struct scmi_handle *handle;
28 
29 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
30 {
31 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
32 	const 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  */
48 static int
49 scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
50 {
51 	struct scmi_data *priv = policy->driver_data;
52 	const struct scmi_perf_ops *perf_ops = handle->perf_ops;
53 	u64 freq = policy->freq_table[index].frequency;
54 
55 	return perf_ops->freq_set(handle, priv->domain_id, freq * 1000, false);
56 }
57 
58 static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy,
59 					     unsigned int target_freq)
60 {
61 	struct scmi_data *priv = policy->driver_data;
62 	const struct scmi_perf_ops *perf_ops = handle->perf_ops;
63 
64 	if (!perf_ops->freq_set(handle, priv->domain_id,
65 				target_freq * 1000, true))
66 		return target_freq;
67 
68 	return 0;
69 }
70 
71 static int
72 scmi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
73 {
74 	int cpu, domain, tdomain;
75 	struct device *tcpu_dev;
76 
77 	domain = handle->perf_ops->device_domain_id(cpu_dev);
78 	if (domain < 0)
79 		return domain;
80 
81 	for_each_possible_cpu(cpu) {
82 		if (cpu == cpu_dev->id)
83 			continue;
84 
85 		tcpu_dev = get_cpu_device(cpu);
86 		if (!tcpu_dev)
87 			continue;
88 
89 		tdomain = handle->perf_ops->device_domain_id(tcpu_dev);
90 		if (tdomain == domain)
91 			cpumask_set_cpu(cpu, cpumask);
92 	}
93 
94 	return 0;
95 }
96 
97 static int __maybe_unused
98 scmi_get_cpu_power(unsigned long *power, unsigned long *KHz,
99 		   struct device *cpu_dev)
100 {
101 	unsigned long Hz;
102 	int ret, domain;
103 
104 	domain = handle->perf_ops->device_domain_id(cpu_dev);
105 	if (domain < 0)
106 		return domain;
107 
108 	/* Get the power cost of the performance domain. */
109 	Hz = *KHz * 1000;
110 	ret = handle->perf_ops->est_power_get(handle, domain, &Hz, power);
111 	if (ret)
112 		return ret;
113 
114 	/* The EM framework specifies the frequency in KHz. */
115 	*KHz = Hz / 1000;
116 
117 	return 0;
118 }
119 
120 static int scmi_cpufreq_init(struct cpufreq_policy *policy)
121 {
122 	int ret, nr_opp;
123 	unsigned int latency;
124 	struct device *cpu_dev;
125 	struct scmi_data *priv;
126 	struct cpufreq_frequency_table *freq_table;
127 	struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
128 
129 	cpu_dev = get_cpu_device(policy->cpu);
130 	if (!cpu_dev) {
131 		pr_err("failed to get cpu%d device\n", policy->cpu);
132 		return -ENODEV;
133 	}
134 
135 	ret = handle->perf_ops->device_opps_add(handle, cpu_dev);
136 	if (ret) {
137 		dev_warn(cpu_dev, "failed to add opps to the device\n");
138 		return ret;
139 	}
140 
141 	ret = scmi_get_sharing_cpus(cpu_dev, policy->cpus);
142 	if (ret) {
143 		dev_warn(cpu_dev, "failed to get sharing cpumask\n");
144 		return ret;
145 	}
146 
147 	ret = dev_pm_opp_set_sharing_cpus(cpu_dev, policy->cpus);
148 	if (ret) {
149 		dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n",
150 			__func__, ret);
151 		return ret;
152 	}
153 
154 	nr_opp = dev_pm_opp_get_opp_count(cpu_dev);
155 	if (nr_opp <= 0) {
156 		dev_dbg(cpu_dev, "OPP table is not ready, deferring probe\n");
157 		ret = -EPROBE_DEFER;
158 		goto out_free_opp;
159 	}
160 
161 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
162 	if (!priv) {
163 		ret = -ENOMEM;
164 		goto out_free_opp;
165 	}
166 
167 	ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table);
168 	if (ret) {
169 		dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret);
170 		goto out_free_priv;
171 	}
172 
173 	priv->cpu_dev = cpu_dev;
174 	priv->domain_id = handle->perf_ops->device_domain_id(cpu_dev);
175 
176 	policy->driver_data = priv;
177 	policy->freq_table = freq_table;
178 
179 	/* SCMI allows DVFS request for any domain from any CPU */
180 	policy->dvfs_possible_from_any_cpu = true;
181 
182 	latency = handle->perf_ops->transition_latency_get(handle, cpu_dev);
183 	if (!latency)
184 		latency = CPUFREQ_ETERNAL;
185 
186 	policy->cpuinfo.transition_latency = latency;
187 
188 	policy->fast_switch_possible =
189 		handle->perf_ops->fast_switch_possible(handle, cpu_dev);
190 
191 	em_dev_register_perf_domain(cpu_dev, nr_opp, &em_cb, policy->cpus);
192 
193 	return 0;
194 
195 out_free_priv:
196 	kfree(priv);
197 out_free_opp:
198 	dev_pm_opp_remove_all_dynamic(cpu_dev);
199 
200 	return ret;
201 }
202 
203 static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
204 {
205 	struct scmi_data *priv = policy->driver_data;
206 
207 	dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
208 	dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
209 	kfree(priv);
210 
211 	return 0;
212 }
213 
214 static struct cpufreq_driver scmi_cpufreq_driver = {
215 	.name	= "scmi",
216 	.flags	= CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
217 		  CPUFREQ_NEED_INITIAL_FREQ_CHECK |
218 		  CPUFREQ_IS_COOLING_DEV,
219 	.verify	= cpufreq_generic_frequency_table_verify,
220 	.attr	= cpufreq_generic_attr,
221 	.target_index	= scmi_cpufreq_set_target,
222 	.fast_switch	= scmi_cpufreq_fast_switch,
223 	.get	= scmi_cpufreq_get_rate,
224 	.init	= scmi_cpufreq_init,
225 	.exit	= scmi_cpufreq_exit,
226 };
227 
228 static int scmi_cpufreq_probe(struct scmi_device *sdev)
229 {
230 	int ret;
231 
232 	handle = sdev->handle;
233 
234 	if (!handle || !handle->perf_ops)
235 		return -ENODEV;
236 
237 	ret = cpufreq_register_driver(&scmi_cpufreq_driver);
238 	if (ret) {
239 		dev_err(&sdev->dev, "%s: registering cpufreq failed, err: %d\n",
240 			__func__, ret);
241 	}
242 
243 	return ret;
244 }
245 
246 static void scmi_cpufreq_remove(struct scmi_device *sdev)
247 {
248 	cpufreq_unregister_driver(&scmi_cpufreq_driver);
249 }
250 
251 static const struct scmi_device_id scmi_id_table[] = {
252 	{ SCMI_PROTOCOL_PERF, "cpufreq" },
253 	{ },
254 };
255 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
256 
257 static struct scmi_driver scmi_cpufreq_drv = {
258 	.name		= "scmi-cpufreq",
259 	.probe		= scmi_cpufreq_probe,
260 	.remove		= scmi_cpufreq_remove,
261 	.id_table	= scmi_id_table,
262 };
263 module_scmi_driver(scmi_cpufreq_drv);
264 
265 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
266 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
267 MODULE_LICENSE("GPL v2");
268