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/cpu_cooling.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
;
25 struct thermal_cooling_device
*cdev
;
28 static const struct scmi_handle
*handle
;
30 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu
)
32 struct cpufreq_policy
*policy
= cpufreq_cpu_get_raw(cpu
);
33 struct scmi_perf_ops
*perf_ops
= handle
->perf_ops
;
34 struct scmi_data
*priv
= policy
->driver_data
;
38 ret
= perf_ops
->freq_get(handle
, priv
->domain_id
, &rate
, false);
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
50 scmi_cpufreq_set_target(struct cpufreq_policy
*policy
, unsigned int index
)
53 struct scmi_data
*priv
= policy
->driver_data
;
54 struct scmi_perf_ops
*perf_ops
= handle
->perf_ops
;
55 u64 freq
= policy
->freq_table
[index
].frequency
;
57 ret
= perf_ops
->freq_set(handle
, priv
->domain_id
, freq
* 1000, false);
59 arch_set_freq_scale(policy
->related_cpus
, freq
,
60 policy
->cpuinfo
.max_freq
);
64 static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy
*policy
,
65 unsigned int target_freq
)
67 struct scmi_data
*priv
= policy
->driver_data
;
68 struct scmi_perf_ops
*perf_ops
= handle
->perf_ops
;
70 if (!perf_ops
->freq_set(handle
, priv
->domain_id
,
71 target_freq
* 1000, true)) {
72 arch_set_freq_scale(policy
->related_cpus
, target_freq
,
73 policy
->cpuinfo
.max_freq
);
81 scmi_get_sharing_cpus(struct device
*cpu_dev
, struct cpumask
*cpumask
)
83 int cpu
, domain
, tdomain
;
84 struct device
*tcpu_dev
;
86 domain
= handle
->perf_ops
->device_domain_id(cpu_dev
);
90 for_each_possible_cpu(cpu
) {
91 if (cpu
== cpu_dev
->id
)
94 tcpu_dev
= get_cpu_device(cpu
);
98 tdomain
= handle
->perf_ops
->device_domain_id(tcpu_dev
);
99 if (tdomain
== domain
)
100 cpumask_set_cpu(cpu
, cpumask
);
106 static int scmi_cpufreq_init(struct cpufreq_policy
*policy
)
109 unsigned int latency
;
110 struct device
*cpu_dev
;
111 struct scmi_data
*priv
;
112 struct cpufreq_frequency_table
*freq_table
;
114 cpu_dev
= get_cpu_device(policy
->cpu
);
116 pr_err("failed to get cpu%d device\n", policy
->cpu
);
120 ret
= handle
->perf_ops
->device_opps_add(handle
, cpu_dev
);
122 dev_warn(cpu_dev
, "failed to add opps to the device\n");
126 ret
= scmi_get_sharing_cpus(cpu_dev
, policy
->cpus
);
128 dev_warn(cpu_dev
, "failed to get sharing cpumask\n");
132 ret
= dev_pm_opp_set_sharing_cpus(cpu_dev
, policy
->cpus
);
134 dev_err(cpu_dev
, "%s: failed to mark OPPs as shared: %d\n",
139 ret
= dev_pm_opp_get_opp_count(cpu_dev
);
141 dev_dbg(cpu_dev
, "OPP table is not ready, deferring probe\n");
146 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
152 ret
= dev_pm_opp_init_cpufreq_table(cpu_dev
, &freq_table
);
154 dev_err(cpu_dev
, "failed to init cpufreq table: %d\n", ret
);
158 priv
->cpu_dev
= cpu_dev
;
159 priv
->domain_id
= handle
->perf_ops
->device_domain_id(cpu_dev
);
161 policy
->driver_data
= priv
;
162 policy
->freq_table
= freq_table
;
164 /* SCMI allows DVFS request for any domain from any CPU */
165 policy
->dvfs_possible_from_any_cpu
= true;
167 latency
= handle
->perf_ops
->transition_latency_get(handle
, cpu_dev
);
169 latency
= CPUFREQ_ETERNAL
;
171 policy
->cpuinfo
.transition_latency
= latency
;
173 policy
->fast_switch_possible
= true;
179 dev_pm_opp_cpumask_remove_table(policy
->cpus
);
184 static int scmi_cpufreq_exit(struct cpufreq_policy
*policy
)
186 struct scmi_data
*priv
= policy
->driver_data
;
188 cpufreq_cooling_unregister(priv
->cdev
);
189 dev_pm_opp_free_cpufreq_table(priv
->cpu_dev
, &policy
->freq_table
);
191 dev_pm_opp_cpumask_remove_table(policy
->related_cpus
);
196 static void scmi_cpufreq_ready(struct cpufreq_policy
*policy
)
198 struct scmi_data
*priv
= policy
->driver_data
;
200 priv
->cdev
= of_cpufreq_cooling_register(policy
);
203 static struct cpufreq_driver scmi_cpufreq_driver
= {
205 .flags
= CPUFREQ_STICKY
| CPUFREQ_HAVE_GOVERNOR_PER_POLICY
|
206 CPUFREQ_NEED_INITIAL_FREQ_CHECK
,
207 .verify
= cpufreq_generic_frequency_table_verify
,
208 .attr
= cpufreq_generic_attr
,
209 .target_index
= scmi_cpufreq_set_target
,
210 .fast_switch
= scmi_cpufreq_fast_switch
,
211 .get
= scmi_cpufreq_get_rate
,
212 .init
= scmi_cpufreq_init
,
213 .exit
= scmi_cpufreq_exit
,
214 .ready
= scmi_cpufreq_ready
,
217 static int scmi_cpufreq_probe(struct scmi_device
*sdev
)
221 handle
= sdev
->handle
;
223 if (!handle
|| !handle
->perf_ops
)
226 ret
= cpufreq_register_driver(&scmi_cpufreq_driver
);
228 dev_err(&sdev
->dev
, "%s: registering cpufreq failed, err: %d\n",
235 static void scmi_cpufreq_remove(struct scmi_device
*sdev
)
237 cpufreq_unregister_driver(&scmi_cpufreq_driver
);
240 static const struct scmi_device_id scmi_id_table
[] = {
241 { SCMI_PROTOCOL_PERF
},
244 MODULE_DEVICE_TABLE(scmi
, scmi_id_table
);
246 static struct scmi_driver scmi_cpufreq_drv
= {
247 .name
= "scmi-cpufreq",
248 .probe
= scmi_cpufreq_probe
,
249 .remove
= scmi_cpufreq_remove
,
250 .id_table
= scmi_id_table
,
252 module_scmi_driver(scmi_cpufreq_drv
);
254 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
255 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver");
256 MODULE_LICENSE("GPL v2");