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