1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Power Interface (SCMI) based CPUFreq Interface driver
5 * Copyright (C) 2018 ARM Ltd.
6 * Sudeep Holla <sudeep.holla@arm.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
24 struct device
*cpu_dev
;
27 static const struct scmi_handle
*handle
;
29 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu
)
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
;
37 ret
= perf_ops
->freq_get(handle
, priv
->domain_id
, &rate
, false);
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
49 scmi_cpufreq_set_target(struct cpufreq_policy
*policy
, unsigned int index
)
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
;
56 ret
= perf_ops
->freq_set(handle
, priv
->domain_id
, freq
* 1000, false);
58 arch_set_freq_scale(policy
->related_cpus
, freq
,
59 policy
->cpuinfo
.max_freq
);
63 static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy
*policy
,
64 unsigned int target_freq
)
66 struct scmi_data
*priv
= policy
->driver_data
;
67 struct scmi_perf_ops
*perf_ops
= handle
->perf_ops
;
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
);
80 scmi_get_sharing_cpus(struct device
*cpu_dev
, struct cpumask
*cpumask
)
82 int cpu
, domain
, tdomain
;
83 struct device
*tcpu_dev
;
85 domain
= handle
->perf_ops
->device_domain_id(cpu_dev
);
89 for_each_possible_cpu(cpu
) {
90 if (cpu
== cpu_dev
->id
)
93 tcpu_dev
= get_cpu_device(cpu
);
97 tdomain
= handle
->perf_ops
->device_domain_id(tcpu_dev
);
98 if (tdomain
== domain
)
99 cpumask_set_cpu(cpu
, cpumask
);
105 static int __maybe_unused
106 scmi_get_cpu_power(unsigned long *power
, unsigned long *KHz
, int cpu
)
108 struct device
*cpu_dev
= get_cpu_device(cpu
);
113 pr_err("failed to get cpu%d device\n", cpu
);
117 domain
= handle
->perf_ops
->device_domain_id(cpu_dev
);
121 /* Get the power cost of the performance domain. */
123 ret
= handle
->perf_ops
->est_power_get(handle
, domain
, &Hz
, power
);
127 /* The EM framework specifies the frequency in KHz. */
133 static int scmi_cpufreq_init(struct cpufreq_policy
*policy
)
136 unsigned int latency
;
137 struct device
*cpu_dev
;
138 struct scmi_data
*priv
;
139 struct cpufreq_frequency_table
*freq_table
;
140 struct em_data_callback em_cb
= EM_DATA_CB(scmi_get_cpu_power
);
142 cpu_dev
= get_cpu_device(policy
->cpu
);
144 pr_err("failed to get cpu%d device\n", policy
->cpu
);
148 ret
= handle
->perf_ops
->device_opps_add(handle
, cpu_dev
);
150 dev_warn(cpu_dev
, "failed to add opps to the device\n");
154 ret
= scmi_get_sharing_cpus(cpu_dev
, policy
->cpus
);
156 dev_warn(cpu_dev
, "failed to get sharing cpumask\n");
160 ret
= dev_pm_opp_set_sharing_cpus(cpu_dev
, policy
->cpus
);
162 dev_err(cpu_dev
, "%s: failed to mark OPPs as shared: %d\n",
167 nr_opp
= dev_pm_opp_get_opp_count(cpu_dev
);
169 dev_dbg(cpu_dev
, "OPP table is not ready, deferring probe\n");
174 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
180 ret
= dev_pm_opp_init_cpufreq_table(cpu_dev
, &freq_table
);
182 dev_err(cpu_dev
, "failed to init cpufreq table: %d\n", ret
);
186 priv
->cpu_dev
= cpu_dev
;
187 priv
->domain_id
= handle
->perf_ops
->device_domain_id(cpu_dev
);
189 policy
->driver_data
= priv
;
190 policy
->freq_table
= freq_table
;
192 /* SCMI allows DVFS request for any domain from any CPU */
193 policy
->dvfs_possible_from_any_cpu
= true;
195 latency
= handle
->perf_ops
->transition_latency_get(handle
, cpu_dev
);
197 latency
= CPUFREQ_ETERNAL
;
199 policy
->cpuinfo
.transition_latency
= latency
;
201 policy
->fast_switch_possible
= true;
203 em_register_perf_domain(policy
->cpus
, nr_opp
, &em_cb
);
210 dev_pm_opp_remove_all_dynamic(cpu_dev
);
215 static int scmi_cpufreq_exit(struct cpufreq_policy
*policy
)
217 struct scmi_data
*priv
= policy
->driver_data
;
219 dev_pm_opp_free_cpufreq_table(priv
->cpu_dev
, &policy
->freq_table
);
220 dev_pm_opp_remove_all_dynamic(priv
->cpu_dev
);
226 static struct cpufreq_driver scmi_cpufreq_driver
= {
228 .flags
= CPUFREQ_STICKY
| CPUFREQ_HAVE_GOVERNOR_PER_POLICY
|
229 CPUFREQ_NEED_INITIAL_FREQ_CHECK
|
230 CPUFREQ_IS_COOLING_DEV
,
231 .verify
= cpufreq_generic_frequency_table_verify
,
232 .attr
= cpufreq_generic_attr
,
233 .target_index
= scmi_cpufreq_set_target
,
234 .fast_switch
= scmi_cpufreq_fast_switch
,
235 .get
= scmi_cpufreq_get_rate
,
236 .init
= scmi_cpufreq_init
,
237 .exit
= scmi_cpufreq_exit
,
240 static int scmi_cpufreq_probe(struct scmi_device
*sdev
)
244 handle
= sdev
->handle
;
246 if (!handle
|| !handle
->perf_ops
)
249 ret
= cpufreq_register_driver(&scmi_cpufreq_driver
);
251 dev_err(&sdev
->dev
, "%s: registering cpufreq failed, err: %d\n",
258 static void scmi_cpufreq_remove(struct scmi_device
*sdev
)
260 cpufreq_unregister_driver(&scmi_cpufreq_driver
);
263 static const struct scmi_device_id scmi_id_table
[] = {
264 { SCMI_PROTOCOL_PERF
},
267 MODULE_DEVICE_TABLE(scmi
, scmi_id_table
);
269 static struct scmi_driver scmi_cpufreq_drv
= {
270 .name
= "scmi-cpufreq",
271 .probe
= scmi_cpufreq_probe
,
272 .remove
= scmi_cpufreq_remove
,
273 .id_table
= scmi_id_table
,
275 module_scmi_driver(scmi_cpufreq_drv
);
277 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
278 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
279 MODULE_LICENSE("GPL v2");