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