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/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
23 struct scmi_data {
24 int domain_id;
25 struct device *cpu_dev;
26 };
27
28 static struct scmi_protocol_handle *ph;
29 static const struct scmi_perf_proto_ops *perf_ops;
30
scmi_cpufreq_get_rate(unsigned int cpu)31 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu)
32 {
33 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
34 struct scmi_data *priv = policy->driver_data;
35 unsigned long rate;
36 int ret;
37
38 ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false);
39 if (ret)
40 return 0;
41 return rate / 1000;
42 }
43
44 /*
45 * perf_ops->freq_set is not a synchronous, the actual OPP change will
46 * happen asynchronously and can get notified if the events are
47 * subscribed for by the SCMI firmware
48 */
49 static int
scmi_cpufreq_set_target(struct cpufreq_policy * policy,unsigned int index)50 scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index)
51 {
52 struct scmi_data *priv = policy->driver_data;
53 u64 freq = policy->freq_table[index].frequency;
54
55 return perf_ops->freq_set(ph, priv->domain_id, freq * 1000, false);
56 }
57
scmi_cpufreq_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)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
63 if (!perf_ops->freq_set(ph, priv->domain_id,
64 target_freq * 1000, true))
65 return target_freq;
66
67 return 0;
68 }
69
70 static int
scmi_get_sharing_cpus(struct device * cpu_dev,struct cpumask * cpumask)71 scmi_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask)
72 {
73 int cpu, domain, tdomain;
74 struct device *tcpu_dev;
75
76 domain = perf_ops->device_domain_id(cpu_dev);
77 if (domain < 0)
78 return domain;
79
80 for_each_possible_cpu(cpu) {
81 if (cpu == cpu_dev->id)
82 continue;
83
84 tcpu_dev = get_cpu_device(cpu);
85 if (!tcpu_dev)
86 continue;
87
88 tdomain = perf_ops->device_domain_id(tcpu_dev);
89 if (tdomain == domain)
90 cpumask_set_cpu(cpu, cpumask);
91 }
92
93 return 0;
94 }
95
96 static int __maybe_unused
scmi_get_cpu_power(unsigned long * power,unsigned long * KHz,struct device * cpu_dev)97 scmi_get_cpu_power(unsigned long *power, unsigned long *KHz,
98 struct device *cpu_dev)
99 {
100 unsigned long Hz;
101 int ret, domain;
102
103 domain = perf_ops->device_domain_id(cpu_dev);
104 if (domain < 0)
105 return domain;
106
107 /* Get the power cost of the performance domain. */
108 Hz = *KHz * 1000;
109 ret = perf_ops->est_power_get(ph, domain, &Hz, power);
110 if (ret)
111 return ret;
112
113 /* The EM framework specifies the frequency in KHz. */
114 *KHz = Hz / 1000;
115
116 return 0;
117 }
118
scmi_cpufreq_init(struct cpufreq_policy * policy)119 static int scmi_cpufreq_init(struct cpufreq_policy *policy)
120 {
121 int ret, nr_opp;
122 unsigned int latency;
123 struct device *cpu_dev;
124 struct scmi_data *priv;
125 struct cpufreq_frequency_table *freq_table;
126 struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power);
127 bool power_scale_mw;
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 = perf_ops->device_opps_add(ph, 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 = 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 = perf_ops->transition_latency_get(ph, cpu_dev);
183 if (!latency)
184 latency = CPUFREQ_ETERNAL;
185
186 policy->cpuinfo.transition_latency = latency;
187
188 policy->fast_switch_possible =
189 perf_ops->fast_switch_possible(ph, cpu_dev);
190
191 power_scale_mw = perf_ops->power_scale_mw_get(ph);
192 em_dev_register_perf_domain(cpu_dev, nr_opp, &em_cb, policy->cpus,
193 power_scale_mw);
194
195 return 0;
196
197 out_free_priv:
198 kfree(priv);
199 out_free_opp:
200 dev_pm_opp_remove_all_dynamic(cpu_dev);
201
202 return ret;
203 }
204
scmi_cpufreq_exit(struct cpufreq_policy * policy)205 static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
206 {
207 struct scmi_data *priv = policy->driver_data;
208
209 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
210 dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
211 kfree(priv);
212
213 return 0;
214 }
215
216 static struct cpufreq_driver scmi_cpufreq_driver = {
217 .name = "scmi",
218 .flags = CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
219 CPUFREQ_NEED_INITIAL_FREQ_CHECK |
220 CPUFREQ_IS_COOLING_DEV,
221 .verify = cpufreq_generic_frequency_table_verify,
222 .attr = cpufreq_generic_attr,
223 .target_index = scmi_cpufreq_set_target,
224 .fast_switch = scmi_cpufreq_fast_switch,
225 .get = scmi_cpufreq_get_rate,
226 .init = scmi_cpufreq_init,
227 .exit = scmi_cpufreq_exit,
228 };
229
scmi_cpufreq_probe(struct scmi_device * sdev)230 static int scmi_cpufreq_probe(struct scmi_device *sdev)
231 {
232 int ret;
233 struct device *dev = &sdev->dev;
234 const struct scmi_handle *handle;
235
236 handle = sdev->handle;
237
238 if (!handle)
239 return -ENODEV;
240
241 perf_ops = handle->devm_get_protocol(sdev, SCMI_PROTOCOL_PERF, &ph);
242 if (IS_ERR(perf_ops))
243 return PTR_ERR(perf_ops);
244
245 #ifdef CONFIG_COMMON_CLK
246 /* dummy clock provider as needed by OPP if clocks property is used */
247 if (of_property_present(dev->of_node, "#clock-cells")) {
248 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL);
249 if (ret)
250 return dev_err_probe(dev, ret, "%s: registering clock provider failed\n", __func__);
251 }
252 #endif
253
254 ret = cpufreq_register_driver(&scmi_cpufreq_driver);
255 if (ret) {
256 dev_err(dev, "%s: registering cpufreq failed, err: %d\n",
257 __func__, ret);
258 }
259
260 return ret;
261 }
262
scmi_cpufreq_remove(struct scmi_device * sdev)263 static void scmi_cpufreq_remove(struct scmi_device *sdev)
264 {
265 cpufreq_unregister_driver(&scmi_cpufreq_driver);
266 }
267
268 static const struct scmi_device_id scmi_id_table[] = {
269 { SCMI_PROTOCOL_PERF, "cpufreq" },
270 { },
271 };
272 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
273
274 static struct scmi_driver scmi_cpufreq_drv = {
275 .name = "scmi-cpufreq",
276 .probe = scmi_cpufreq_probe,
277 .remove = scmi_cpufreq_remove,
278 .id_table = scmi_id_table,
279 };
280 module_scmi_driver(scmi_cpufreq_drv);
281
282 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
283 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
284 MODULE_LICENSE("GPL v2");
285