1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Power Interface (SCMI) based CPUFreq Interface driver 4 * 5 * Copyright (C) 2018-2021 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/pm_qos.h> 20 #include <linux/slab.h> 21 #include <linux/scmi_protocol.h> 22 #include <linux/types.h> 23 #include <linux/units.h> 24 25 struct scmi_data { 26 int domain_id; 27 int nr_opp; 28 struct device *cpu_dev; 29 cpumask_var_t opp_shared_cpus; 30 struct notifier_block limit_notify_nb; 31 struct freq_qos_request limits_freq_req; 32 }; 33 34 static struct scmi_protocol_handle *ph; 35 static const struct scmi_perf_proto_ops *perf_ops; 36 static struct cpufreq_driver scmi_cpufreq_driver; 37 38 static unsigned int scmi_cpufreq_get_rate(unsigned int cpu) 39 { 40 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 41 struct scmi_data *priv = policy->driver_data; 42 unsigned long rate; 43 int ret; 44 45 ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false); 46 if (ret) 47 return 0; 48 return rate / 1000; 49 } 50 51 /* 52 * perf_ops->freq_set is not a synchronous, the actual OPP change will 53 * happen asynchronously and can get notified if the events are 54 * subscribed for by the SCMI firmware 55 */ 56 static int 57 scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index) 58 { 59 struct scmi_data *priv = policy->driver_data; 60 u64 freq = policy->freq_table[index].frequency; 61 62 return perf_ops->freq_set(ph, priv->domain_id, freq * 1000, false); 63 } 64 65 static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy, 66 unsigned int target_freq) 67 { 68 struct scmi_data *priv = policy->driver_data; 69 unsigned long freq = target_freq; 70 71 if (!perf_ops->freq_set(ph, priv->domain_id, freq * 1000, true)) 72 return target_freq; 73 74 return 0; 75 } 76 77 static int scmi_cpu_domain_id(struct device *cpu_dev) 78 { 79 struct device_node *np = cpu_dev->of_node; 80 struct of_phandle_args domain_id; 81 int index; 82 83 if (of_parse_phandle_with_args(np, "clocks", "#clock-cells", 0, 84 &domain_id)) { 85 /* Find the corresponding index for power-domain "perf". */ 86 index = of_property_match_string(np, "power-domain-names", 87 "perf"); 88 if (index < 0) 89 return -EINVAL; 90 91 if (of_parse_phandle_with_args(np, "power-domains", 92 "#power-domain-cells", index, 93 &domain_id)) 94 return -EINVAL; 95 } 96 97 return domain_id.args[0]; 98 } 99 100 static int 101 scmi_get_sharing_cpus(struct device *cpu_dev, int domain, 102 struct cpumask *cpumask) 103 { 104 int cpu, tdomain; 105 struct device *tcpu_dev; 106 107 for_each_present_cpu(cpu) { 108 if (cpu == cpu_dev->id) 109 continue; 110 111 tcpu_dev = get_cpu_device(cpu); 112 if (!tcpu_dev) 113 continue; 114 115 tdomain = scmi_cpu_domain_id(tcpu_dev); 116 if (tdomain == domain) 117 cpumask_set_cpu(cpu, cpumask); 118 } 119 120 return 0; 121 } 122 123 static int __maybe_unused 124 scmi_get_cpu_power(struct device *cpu_dev, unsigned long *power, 125 unsigned long *KHz) 126 { 127 enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph); 128 unsigned long Hz; 129 int ret, domain; 130 131 domain = scmi_cpu_domain_id(cpu_dev); 132 if (domain < 0) 133 return domain; 134 135 /* Get the power cost of the performance domain. */ 136 Hz = *KHz * 1000; 137 ret = perf_ops->est_power_get(ph, domain, &Hz, power); 138 if (ret) 139 return ret; 140 141 /* Convert the power to uW if it is mW (ignore bogoW) */ 142 if (power_scale == SCMI_POWER_MILLIWATTS) 143 *power *= MICROWATT_PER_MILLIWATT; 144 145 /* The EM framework specifies the frequency in KHz. */ 146 *KHz = Hz / 1000; 147 148 return 0; 149 } 150 151 static int 152 scmi_get_rate_limit(u32 domain, bool has_fast_switch) 153 { 154 int ret, rate_limit; 155 156 if (has_fast_switch) { 157 /* 158 * Fast channels are used whenever available, 159 * so use their rate_limit value if populated. 160 */ 161 ret = perf_ops->fast_switch_rate_limit(ph, domain, 162 &rate_limit); 163 if (!ret && rate_limit) 164 return rate_limit; 165 } 166 167 ret = perf_ops->rate_limit_get(ph, domain, &rate_limit); 168 if (ret) 169 return 0; 170 171 return rate_limit; 172 } 173 174 static int scmi_limit_notify_cb(struct notifier_block *nb, unsigned long event, void *data) 175 { 176 struct scmi_data *priv = container_of(nb, struct scmi_data, limit_notify_nb); 177 struct scmi_perf_limits_report *limit_notify = data; 178 unsigned int limit_freq_khz; 179 int ret; 180 181 limit_freq_khz = limit_notify->range_max_freq / HZ_PER_KHZ; 182 183 ret = freq_qos_update_request(&priv->limits_freq_req, limit_freq_khz); 184 if (ret < 0) 185 pr_warn("failed to update freq constraint: %d\n", ret); 186 187 return NOTIFY_OK; 188 } 189 190 static int scmi_cpufreq_init(struct cpufreq_policy *policy) 191 { 192 int ret, nr_opp, domain; 193 unsigned int latency; 194 struct device *cpu_dev; 195 struct scmi_data *priv; 196 struct cpufreq_frequency_table *freq_table; 197 struct scmi_device *sdev = cpufreq_get_driver_data(); 198 199 cpu_dev = get_cpu_device(policy->cpu); 200 if (!cpu_dev) { 201 pr_err("failed to get cpu%d device\n", policy->cpu); 202 return -ENODEV; 203 } 204 205 domain = scmi_cpu_domain_id(cpu_dev); 206 if (domain < 0) 207 return domain; 208 209 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 210 if (!priv) 211 return -ENOMEM; 212 213 if (!zalloc_cpumask_var(&priv->opp_shared_cpus, GFP_KERNEL)) { 214 ret = -ENOMEM; 215 goto out_free_priv; 216 } 217 218 /* Obtain CPUs that share SCMI performance controls */ 219 ret = scmi_get_sharing_cpus(cpu_dev, domain, policy->cpus); 220 if (ret) { 221 dev_warn(cpu_dev, "failed to get sharing cpumask\n"); 222 goto out_free_cpumask; 223 } 224 225 /* 226 * Obtain CPUs that share performance levels. 227 * The OPP 'sharing cpus' info may come from DT through an empty opp 228 * table and opp-shared. 229 */ 230 ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->opp_shared_cpus); 231 if (ret || cpumask_empty(priv->opp_shared_cpus)) { 232 /* 233 * Either opp-table is not set or no opp-shared was found. 234 * Use the CPU mask from SCMI to designate CPUs sharing an OPP 235 * table. 236 */ 237 cpumask_copy(priv->opp_shared_cpus, policy->cpus); 238 } 239 240 /* 241 * A previous CPU may have marked OPPs as shared for a few CPUs, based on 242 * what OPP core provided. If the current CPU is part of those few, then 243 * there is no need to add OPPs again. 244 */ 245 nr_opp = dev_pm_opp_get_opp_count(cpu_dev); 246 if (nr_opp <= 0) { 247 ret = perf_ops->device_opps_add(ph, cpu_dev, domain); 248 if (ret) { 249 dev_warn(cpu_dev, "failed to add opps to the device\n"); 250 goto out_free_cpumask; 251 } 252 253 nr_opp = dev_pm_opp_get_opp_count(cpu_dev); 254 if (nr_opp <= 0) { 255 dev_err(cpu_dev, "%s: No OPPs for this device: %d\n", 256 __func__, nr_opp); 257 258 ret = -ENODEV; 259 goto out_free_opp; 260 } 261 262 ret = dev_pm_opp_set_sharing_cpus(cpu_dev, priv->opp_shared_cpus); 263 if (ret) { 264 dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n", 265 __func__, ret); 266 267 goto out_free_opp; 268 } 269 270 priv->nr_opp = nr_opp; 271 } 272 273 ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table); 274 if (ret) { 275 dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret); 276 goto out_free_opp; 277 } 278 279 priv->cpu_dev = cpu_dev; 280 priv->domain_id = domain; 281 282 policy->driver_data = priv; 283 policy->freq_table = freq_table; 284 285 /* SCMI allows DVFS request for any domain from any CPU */ 286 policy->dvfs_possible_from_any_cpu = true; 287 288 latency = perf_ops->transition_latency_get(ph, domain); 289 if (!latency) 290 latency = CPUFREQ_ETERNAL; 291 292 policy->cpuinfo.transition_latency = latency; 293 294 policy->fast_switch_possible = 295 perf_ops->fast_switch_possible(ph, domain); 296 297 policy->transition_delay_us = 298 scmi_get_rate_limit(domain, policy->fast_switch_possible); 299 300 ret = freq_qos_add_request(&policy->constraints, &priv->limits_freq_req, FREQ_QOS_MAX, 301 FREQ_QOS_MAX_DEFAULT_VALUE); 302 if (ret < 0) { 303 dev_err(cpu_dev, "failed to add qos limits request: %d\n", ret); 304 goto out_free_table; 305 } 306 307 priv->limit_notify_nb.notifier_call = scmi_limit_notify_cb; 308 ret = sdev->handle->notify_ops->event_notifier_register(sdev->handle, SCMI_PROTOCOL_PERF, 309 SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED, 310 &priv->domain_id, 311 &priv->limit_notify_nb); 312 if (ret) 313 dev_warn(&sdev->dev, 314 "failed to register for limits change notifier for domain %d\n", 315 priv->domain_id); 316 317 return 0; 318 319 out_free_table: 320 dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table); 321 out_free_opp: 322 dev_pm_opp_remove_all_dynamic(cpu_dev); 323 324 out_free_cpumask: 325 free_cpumask_var(priv->opp_shared_cpus); 326 327 out_free_priv: 328 kfree(priv); 329 330 return ret; 331 } 332 333 static void scmi_cpufreq_exit(struct cpufreq_policy *policy) 334 { 335 struct scmi_data *priv = policy->driver_data; 336 struct scmi_device *sdev = cpufreq_get_driver_data(); 337 338 sdev->handle->notify_ops->event_notifier_unregister(sdev->handle, SCMI_PROTOCOL_PERF, 339 SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED, 340 &priv->domain_id, 341 &priv->limit_notify_nb); 342 freq_qos_remove_request(&priv->limits_freq_req); 343 dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table); 344 dev_pm_opp_remove_all_dynamic(priv->cpu_dev); 345 free_cpumask_var(priv->opp_shared_cpus); 346 kfree(priv); 347 } 348 349 static void scmi_cpufreq_register_em(struct cpufreq_policy *policy) 350 { 351 struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power); 352 enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph); 353 struct scmi_data *priv = policy->driver_data; 354 bool em_power_scale = false; 355 356 /* 357 * This callback will be called for each policy, but we don't need to 358 * register with EM every time. Despite not being part of the same 359 * policy, some CPUs may still share their perf-domains, and a CPU from 360 * another policy may already have registered with EM on behalf of CPUs 361 * of this policy. 362 */ 363 if (!priv->nr_opp) 364 return; 365 366 if (power_scale == SCMI_POWER_MILLIWATTS 367 || power_scale == SCMI_POWER_MICROWATTS) 368 em_power_scale = true; 369 370 em_dev_register_perf_domain(get_cpu_device(policy->cpu), priv->nr_opp, 371 &em_cb, priv->opp_shared_cpus, 372 em_power_scale); 373 } 374 375 static struct cpufreq_driver scmi_cpufreq_driver = { 376 .name = "scmi", 377 .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | 378 CPUFREQ_NEED_INITIAL_FREQ_CHECK | 379 CPUFREQ_IS_COOLING_DEV, 380 .verify = cpufreq_generic_frequency_table_verify, 381 .target_index = scmi_cpufreq_set_target, 382 .fast_switch = scmi_cpufreq_fast_switch, 383 .get = scmi_cpufreq_get_rate, 384 .init = scmi_cpufreq_init, 385 .exit = scmi_cpufreq_exit, 386 .register_em = scmi_cpufreq_register_em, 387 .set_boost = cpufreq_boost_set_sw, 388 }; 389 390 static int scmi_cpufreq_probe(struct scmi_device *sdev) 391 { 392 int ret; 393 struct device *dev = &sdev->dev; 394 const struct scmi_handle *handle; 395 396 handle = sdev->handle; 397 398 if (!handle) 399 return -ENODEV; 400 401 scmi_cpufreq_driver.driver_data = sdev; 402 403 perf_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PERF, &ph); 404 if (IS_ERR(perf_ops)) 405 return PTR_ERR(perf_ops); 406 407 #ifdef CONFIG_COMMON_CLK 408 /* dummy clock provider as needed by OPP if clocks property is used */ 409 if (of_property_present(dev->of_node, "#clock-cells")) { 410 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, NULL); 411 if (ret) 412 return dev_err_probe(dev, ret, "%s: registering clock provider failed\n", __func__); 413 } 414 #endif 415 416 ret = cpufreq_register_driver(&scmi_cpufreq_driver); 417 if (ret) { 418 dev_err(dev, "%s: registering cpufreq failed, err: %d\n", 419 __func__, ret); 420 } 421 422 return ret; 423 } 424 425 static void scmi_cpufreq_remove(struct scmi_device *sdev) 426 { 427 cpufreq_unregister_driver(&scmi_cpufreq_driver); 428 } 429 430 static const struct scmi_device_id scmi_id_table[] = { 431 { SCMI_PROTOCOL_PERF, "cpufreq" }, 432 { }, 433 }; 434 MODULE_DEVICE_TABLE(scmi, scmi_id_table); 435 436 static struct scmi_driver scmi_cpufreq_drv = { 437 .name = "scmi-cpufreq", 438 .probe = scmi_cpufreq_probe, 439 .remove = scmi_cpufreq_remove, 440 .id_table = scmi_id_table, 441 }; 442 module_scmi_driver(scmi_cpufreq_drv); 443 444 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); 445 MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver"); 446 MODULE_LICENSE("GPL v2"); 447